Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / misc / syscons / syscons.c
1 /*-
2  * Copyright (c) 1992-1998 Søren Schmidt
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Sascha Wildner <saw@online.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/dev/syscons/syscons.c,v 1.336.2.17 2004/03/25 08:41:09 ru Exp $
32  * $DragonFly: src/sys/dev/misc/syscons/syscons.c,v 1.13 2004/09/19 02:11:56 dillon Exp $
33  */
34
35 #include "use_splash.h"
36 #include "opt_syscons.h"
37 #include "opt_ddb.h"
38 #ifdef __i386__
39 #include "use_apm.h"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
45 #include <sys/reboot.h>
46 #include <sys/conf.h>
47 #include <sys/proc.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/tty.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/cons.h>
54 #include <sys/random.h>
55
56 #include <machine/clock.h>
57 #include <machine/console.h>
58 #include <machine/psl.h>
59 #include <machine/pc/display.h>
60 #ifdef __i386__
61 #include <machine/apm_bios.h>
62 #include <machine/frame.h>
63 #endif
64
65 #include <dev/misc/kbd/kbdreg.h>
66 #include <dev/video/fb/fbreg.h>
67 #include <dev/video/fb/splashreg.h>
68 #include "syscons.h"
69
70 #define COLD 0
71 #define WARM 1
72
73 #define DEFAULT_BLANKTIME       (5*60)          /* 5 minutes */
74 #define MAX_BLANKTIME           (7*24*60*60)    /* 7 days!? */
75
76 #define KEYCODE_BS              0x0e            /* "<-- Backspace" key, XXX */
77
78 typedef struct default_attr {
79         int             std_color;              /* normal hardware color */
80         int             rev_color;              /* reverse hardware color */
81 } default_attr;
82
83 static default_attr user_default = {
84     SC_NORM_ATTR,
85     SC_NORM_REV_ATTR,
86 };
87
88 static default_attr kernel_default = {
89     SC_KERNEL_CONS_ATTR,
90     SC_KERNEL_CONS_REV_ATTR,
91 };
92
93 static  int             sc_console_unit = -1;
94 static  scr_stat        *sc_console;
95 static  struct tty      *sc_console_tty;
96 static  void            *kernel_console_ts;
97
98 static  char            init_done = COLD;
99 static  char            shutdown_in_progress = FALSE;
100 static  char            sc_malloc = FALSE;
101
102 static  int             saver_mode = CONS_NO_SAVER; /* LKM/user saver */
103 static  int             run_scrn_saver = FALSE; /* should run the saver? */
104 static  long            scrn_blank_time = 0;    /* screen saver timeout value */
105 #if NSPLASH > 0
106 static  int             scrn_blanked;           /* # of blanked screen */
107 static  int             sticky_splash = FALSE;
108
109 static  void            none_saver(sc_softc_t *sc, int blank) { }
110 static  void            (*current_saver)(sc_softc_t *, int) = none_saver;
111 #endif
112
113 #if !defined(SC_NO_FONT_LOADING) && defined(SC_DFLT_FONT)
114 #include "font.h"
115 #endif
116
117         d_ioctl_t       *sc_user_ioctl;
118
119 static  bios_values_t   bios_value;
120
121 static  int             enable_panic_key;
122 SYSCTL_INT(_machdep, OID_AUTO, enable_panic_key, CTLFLAG_RW, &enable_panic_key,
123            0, "");
124
125 #define SC_CONSOLECTL   255
126
127 #define VIRTUAL_TTY(sc, x) (SC_DEV((sc), (x)) != NULL ? \
128         SC_DEV((sc), (x))->si_tty : NULL)
129 #define ISTTYOPEN(tp)   ((tp) && ((tp)->t_state & TS_ISOPEN))
130
131 static  int             debugger;
132
133 /* prototypes */
134 static int scvidprobe(int unit, int flags, int cons);
135 static int sckbdprobe(int unit, int flags, int cons);
136 static void scmeminit(void *arg);
137 static int scdevtounit(dev_t dev);
138 static kbd_callback_func_t sckbdevent;
139 static int scparam(struct tty *tp, struct termios *t);
140 static void scstart(struct tty *tp);
141 static void scinit(int unit, int flags);
142 #if __i386__
143 static void scterm(int unit, int flags);
144 #endif
145 static void scshutdown(void *arg, int howto);
146 static u_int scgetc(sc_softc_t *sc, u_int flags);
147 #define SCGETC_CN       1
148 #define SCGETC_NONBLOCK 2
149 static int sccngetch(int flags);
150 static void sccnupdate(scr_stat *scp);
151 static scr_stat *alloc_scp(sc_softc_t *sc, int vty);
152 static void init_scp(sc_softc_t *sc, int vty, scr_stat *scp);
153 static timeout_t scrn_timer;
154 static int and_region(int *s1, int *e1, int s2, int e2);
155 static void scrn_update(scr_stat *scp, int show_cursor);
156
157 #if NSPLASH > 0
158 static int scsplash_callback(int event, void *arg);
159 static void scsplash_saver(sc_softc_t *sc, int show);
160 static int add_scrn_saver(void (*this_saver)(sc_softc_t *, int));
161 static int remove_scrn_saver(void (*this_saver)(sc_softc_t *, int));
162 static int set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border);
163 static int restore_scrn_saver_mode(scr_stat *scp, int changemode);
164 static void stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int));
165 static int wait_scrn_saver_stop(sc_softc_t *sc);
166 #define scsplash_stick(stick)           (sticky_splash = (stick))
167 #else /* !NSPLASH */
168 #define scsplash_stick(stick)
169 #endif /* NSPLASH */
170
171 static int do_switch_scr(sc_softc_t *sc, int s);
172 static int vt_proc_alive(scr_stat *scp);
173 static int signal_vt_rel(scr_stat *scp);
174 static int signal_vt_acq(scr_stat *scp);
175 static int finish_vt_rel(scr_stat *scp, int release, int *s);
176 static int finish_vt_acq(scr_stat *scp);
177 static void exchange_scr(sc_softc_t *sc);
178 static void update_cursor_image(scr_stat *scp);
179 static int save_kbd_state(scr_stat *scp);
180 static int update_kbd_state(scr_stat *scp, int state, int mask);
181 static int update_kbd_leds(scr_stat *scp, int which);
182 static timeout_t blink_screen;
183
184 #define CDEV_MAJOR      12
185
186 static cn_probe_t       sccnprobe;
187 static cn_init_t        sccninit;
188 static cn_getc_t        sccngetc;
189 static cn_checkc_t      sccncheckc;
190 static cn_putc_t        sccnputc;
191 static cn_dbctl_t       sccndbctl;
192 static cn_term_t        sccnterm;
193
194 #if __alpha__
195 void sccnattach(void);
196 #endif
197
198 CONS_DRIVER(sc, sccnprobe, sccninit, sccnterm, sccngetc, sccncheckc, sccnputc,
199             sccndbctl);
200
201 static  d_open_t        scopen;
202 static  d_close_t       scclose;
203 static  d_read_t        scread;
204 static  d_ioctl_t       scioctl;
205 static  d_mmap_t        scmmap;
206
207 static struct cdevsw sc_cdevsw = {
208         /* name */      "sc",
209         /* maj */       CDEV_MAJOR,
210         /* flags */     D_TTY | D_KQFILTER,
211         /* port */      NULL,
212         /* clone */     NULL,
213
214         /* open */      scopen,
215         /* close */     scclose,
216         /* read */      scread,
217         /* write */     ttywrite,
218         /* ioctl */     scioctl,
219         /* poll */      ttypoll,
220         /* mmap */      scmmap,
221         /* strategy */  nostrategy,
222         /* dump */      nodump,
223         /* psize */     nopsize,
224         /* kqfilter */  ttykqfilter
225 };
226
227 int
228 sc_probe_unit(int unit, int flags)
229 {
230     if (!scvidprobe(unit, flags, FALSE)) {
231         if (bootverbose)
232             printf("sc%d: no video adapter found.\n", unit);
233         return ENXIO;
234     }
235
236     /* syscons will be attached even when there is no keyboard */
237     sckbdprobe(unit, flags, FALSE);
238
239     return 0;
240 }
241
242 /* probe video adapters, return TRUE if found */ 
243 static int
244 scvidprobe(int unit, int flags, int cons)
245 {
246     /*
247      * Access the video adapter driver through the back door!
248      * Video adapter drivers need to be configured before syscons.
249      * However, when syscons is being probed as the low-level console,
250      * they have not been initialized yet.  We force them to initialize
251      * themselves here. XXX
252      */
253     vid_configure(cons ? VIO_PROBE_ONLY : 0);
254
255     return (vid_find_adapter("*", unit) >= 0);
256 }
257
258 /* probe the keyboard, return TRUE if found */
259 static int
260 sckbdprobe(int unit, int flags, int cons)
261 {
262     /* access the keyboard driver through the backdoor! */
263     kbd_configure(cons ? KB_CONF_PROBE_ONLY : 0);
264
265     return (kbd_find_keyboard("*", unit) >= 0);
266 }
267
268 static char
269 *adapter_name(video_adapter_t *adp)
270 {
271     static struct {
272         int type;
273         char *name[2];
274     } names[] = {
275         { KD_MONO,      { "MDA",        "MDA" } },
276         { KD_HERCULES,  { "Hercules",   "Hercules" } },
277         { KD_CGA,       { "CGA",        "CGA" } },
278         { KD_EGA,       { "EGA",        "EGA (mono)" } },
279         { KD_VGA,       { "VGA",        "VGA (mono)" } },
280         { KD_PC98,      { "PC-98x1",    "PC-98x1" } },
281         { KD_TGA,       { "TGA",        "TGA" } },
282         { -1,           { "Unknown",    "Unknown" } },
283     };
284     int i;
285
286     for (i = 0; names[i].type != -1; ++i)
287         if (names[i].type == adp->va_type)
288             break;
289     return names[i].name[(adp->va_flags & V_ADP_COLOR) ? 0 : 1];
290 }
291
292 int
293 sc_attach_unit(int unit, int flags)
294 {
295     sc_softc_t *sc;
296     scr_stat *scp;
297 #ifdef SC_PIXEL_MODE
298     video_info_t info;
299 #endif
300     int vc;
301     dev_t dev;
302
303     flags &= ~SC_KERNEL_CONSOLE;
304
305     if (sc_console_unit == unit) {
306         /*
307          * If this unit is being used as the system console, we need to
308          * adjust some variables and buffers before and after scinit().
309          */
310         /* assert(sc_console != NULL) */
311         flags |= SC_KERNEL_CONSOLE;
312         scmeminit(NULL);
313
314         scinit(unit, flags);
315
316         if (sc_console->tsw->te_size > 0) {
317             /* assert(sc_console->ts != NULL); */
318             kernel_console_ts = sc_console->ts;
319             sc_console->ts = malloc(sc_console->tsw->te_size,
320                                     M_DEVBUF, M_WAITOK);
321             bcopy(kernel_console_ts, sc_console->ts, sc_console->tsw->te_size);
322             (*sc_console->tsw->te_default_attr)(sc_console,
323                                                 user_default.std_color,
324                                                 user_default.rev_color);
325         }
326     } else {
327         scinit(unit, flags);
328     }
329
330     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
331     sc->config = flags;
332     callout_init(&sc->scrn_timer_ch);
333     scp = SC_STAT(sc->dev[0]);
334     if (sc_console == NULL)     /* sc_console_unit < 0 */
335         sc_console = scp;
336
337 #ifdef SC_PIXEL_MODE
338     if ((sc->config & SC_VESA800X600)
339         && ((*vidsw[sc->adapter]->get_info)(sc->adp, M_VESA_800x600, &info) == 0)) {
340 #if NSPLASH > 0
341         if (sc->flags & SC_SPLASH_SCRN)
342             splash_term(sc->adp);
343 #endif
344         sc_set_graphics_mode(scp, NULL, M_VESA_800x600);
345         sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
346         sc->initial_mode = M_VESA_800x600;
347 #if NSPLASH > 0
348         /* put up the splash again! */
349         if (sc->flags & SC_SPLASH_SCRN)
350             splash_init(sc->adp, scsplash_callback, sc);
351 #endif
352     }
353 #endif /* SC_PIXEL_MODE */
354
355     /* initialize cursor */
356     if (!ISGRAPHSC(scp))
357         update_cursor_image(scp);
358
359     /* get screen update going */
360     scrn_timer(sc);
361
362     /* set up the keyboard */
363     kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
364     update_kbd_state(scp, scp->status, LOCK_MASK);
365
366     printf("sc%d: %s <%d virtual consoles, flags=0x%x>\n",
367            unit, adapter_name(sc->adp), sc->vtys, sc->config);
368     if (bootverbose) {
369         printf("sc%d:", unit);
370         if (sc->adapter >= 0)
371             printf(" fb%d", sc->adapter);
372         if (sc->keyboard >= 0)
373             printf(", kbd%d", sc->keyboard);
374         if (scp->tsw)
375             printf(", terminal emulator: %s (%s)",
376                    scp->tsw->te_name, scp->tsw->te_desc);
377         printf("\n");
378     }
379
380     /* register a shutdown callback for the kernel console */
381     if (sc_console_unit == unit)
382         EVENTHANDLER_REGISTER(shutdown_pre_sync, scshutdown, 
383                               (void *)(uintptr_t)unit, SHUTDOWN_PRI_DEFAULT);
384
385     /* 
386      * create devices.  cdevsw_add() must be called to make devices under
387      * this major number available to userland.
388      */
389     cdevsw_add(&sc_cdevsw, ~(MAXCONS - 1), unit * MAXCONS);
390
391     for (vc = 0; vc < sc->vtys; vc++) {
392         dev = make_dev(&sc_cdevsw, vc + unit * MAXCONS,
393             UID_ROOT, GID_WHEEL, 0600, "ttyv%r", vc + unit * MAXCONS);
394         sc->dev[vc] = dev;
395         /*
396          * The first vty already has struct tty and scr_stat initialized
397          * in scinit().  The other vtys will have these structs when
398          * first opened.
399          */
400     }
401
402     cdevsw_add(&sc_cdevsw, -1, SC_CONSOLECTL);  /* XXX */
403     dev = make_dev(&sc_cdevsw, SC_CONSOLECTL,
404                    UID_ROOT, GID_WHEEL, 0600, "consolectl");
405     dev->si_tty = sc_console_tty = ttymalloc(sc_console_tty);
406     SC_STAT(dev) = sc_console;
407
408     return 0;
409 }
410
411 static void
412 scmeminit(void *arg)
413 {
414     if (sc_malloc)
415         return;
416     sc_malloc = TRUE;
417
418     /*
419      * As soon as malloc() becomes functional, we had better allocate
420      * various buffers for the kernel console.
421      */
422
423     if (sc_console_unit < 0)    /* sc_console == NULL */
424         return;
425
426     /* copy the temporary buffer to the final buffer */
427     sc_alloc_scr_buffer(sc_console, TRUE, FALSE);
428
429 #ifndef SC_NO_CUTPASTE
430     sc_alloc_cut_buffer(sc_console, TRUE);
431 #endif
432
433 #ifndef SC_NO_HISTORY
434     /* initialize history buffer & pointers */
435     sc_alloc_history_buffer(sc_console, 0, 0, TRUE);
436 #endif
437 }
438
439 /* XXX */
440 SYSINIT(sc_mem, SI_SUB_KMEM, SI_ORDER_ANY, scmeminit, NULL);
441
442 static int
443 scdevtounit(dev_t dev)
444 {
445     int vty = SC_VTY(dev);
446
447     if (vty == SC_CONSOLECTL)
448         return ((sc_console != NULL) ? sc_console->sc->unit : -1);
449     else if ((vty < 0) || (vty >= MAXCONS*sc_max_unit()))
450         return -1;
451     else
452         return vty/MAXCONS;
453 }
454
455 int
456 scopen(dev_t dev, int flag, int mode, struct thread *td)
457 {
458     int unit = scdevtounit(dev);
459     sc_softc_t *sc;
460     struct tty *tp;
461     scr_stat *scp;
462     keyarg_t key;
463     int error;
464
465     DPRINTF(5, ("scopen: dev:%d,%d, unit:%d, vty:%d\n",
466                 major(dev), minor(dev), unit, SC_VTY(dev)));
467
468     sc = sc_get_softc(unit, (sc_console_unit == unit) ? SC_KERNEL_CONSOLE : 0);
469     if (sc == NULL)
470         return ENXIO;
471
472     tp = dev->si_tty = ttymalloc(dev->si_tty);
473     tp->t_oproc = scstart;
474     tp->t_param = scparam;
475     tp->t_stop = nottystop;
476     tp->t_dev = dev;
477     if (!ISTTYOPEN(tp)) {
478         ttychars(tp);
479         /* Use the current setting of the <-- key as default VERASE. */  
480         /* If the Delete key is preferable, an stty is necessary     */
481         if (sc->kbd != NULL) {
482             key.keynum = KEYCODE_BS;
483             kbd_ioctl(sc->kbd, GIO_KEYMAPENT, (caddr_t)&key);
484             tp->t_cc[VERASE] = key.key.map[0];
485         }
486         tp->t_iflag = TTYDEF_IFLAG;
487         tp->t_oflag = TTYDEF_OFLAG;
488         tp->t_cflag = TTYDEF_CFLAG;
489         tp->t_lflag = TTYDEF_LFLAG;
490         tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
491         scparam(tp, &tp->t_termios);
492         (*linesw[tp->t_line].l_modem)(tp, 1);
493     }
494     else
495         if (tp->t_state & TS_XCLUDE && suser(td))
496             return(EBUSY);
497
498     error = (*linesw[tp->t_line].l_open)(dev, tp);
499
500     scp = SC_STAT(dev);
501     if (scp == NULL) {
502         scp = SC_STAT(dev) = alloc_scp(sc, SC_VTY(dev));
503         if (ISGRAPHSC(scp))
504             sc_set_pixel_mode(scp, NULL, COL, ROW, 16);
505     }
506     if (!tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
507         tp->t_winsize.ws_col = scp->xsize;
508         tp->t_winsize.ws_row = scp->ysize;
509     }
510
511     return error;
512 }
513
514 int
515 scclose(dev_t dev, int flag, int mode, struct thread *td)
516 {
517     struct tty *tp = dev->si_tty;
518     scr_stat *scp;
519     int s;
520
521     if (SC_VTY(dev) != SC_CONSOLECTL) {
522         scp = SC_STAT(tp->t_dev);
523         /* were we in the middle of the VT switching process? */
524         DPRINTF(5, ("sc%d: scclose(), ", scp->sc->unit));
525         s = spltty();
526         if ((scp == scp->sc->cur_scp) && (scp->sc->unit == sc_console_unit))
527             cons_unavail = FALSE;
528         if (finish_vt_rel(scp, TRUE, &s) == 0)  /* force release */
529             DPRINTF(5, ("reset WAIT_REL, "));
530         if (finish_vt_acq(scp) == 0)            /* force acknowledge */
531             DPRINTF(5, ("reset WAIT_ACQ, "));
532 #if not_yet_done
533         if (scp == &main_console) {
534             scp->pid = 0;
535             scp->proc = NULL;
536             scp->smode.mode = VT_AUTO;
537         }
538         else {
539             sc_vtb_destroy(&scp->vtb);
540             sc_vtb_destroy(&scp->scr);
541             sc_free_history_buffer(scp, scp->ysize);
542             SC_STAT(dev) = NULL;
543             free(scp, M_DEVBUF);
544         }
545 #else
546         scp->pid = 0;
547         scp->proc = NULL;
548         scp->smode.mode = VT_AUTO;
549 #endif
550         scp->kbd_mode = K_XLATE;
551         if (scp == scp->sc->cur_scp)
552             kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
553         DPRINTF(5, ("done.\n"));
554     }
555     spltty();
556     (*linesw[tp->t_line].l_close)(tp, flag);
557     ttyclose(tp);
558     spl0();
559     return(0);
560 }
561
562 int
563 scread(dev_t dev, struct uio *uio, int flag)
564 {
565     sc_touch_scrn_saver();
566     return ttyread(dev, uio, flag);
567 }
568
569 static int
570 sckbdevent(keyboard_t *thiskbd, int event, void *arg)
571 {
572     sc_softc_t *sc;
573     struct tty *cur_tty;
574     int c; 
575     size_t len;
576     u_char *cp;
577
578     sc = (sc_softc_t *)arg;
579     /* assert(thiskbd == sc->kbd) */
580
581     switch (event) {
582     case KBDIO_KEYINPUT:
583         break;
584     case KBDIO_UNLOADING:
585         sc->kbd = NULL;
586         sc->keyboard = -1;
587         kbd_release(thiskbd, (void *)&sc->keyboard);
588         return 0;
589     default:
590         return EINVAL;
591     }
592
593     /* 
594      * Loop while there is still input to get from the keyboard.
595      * I don't think this is nessesary, and it doesn't fix
596      * the Xaccel-2.1 keyboard hang, but it can't hurt.         XXX
597      */
598     while ((c = scgetc(sc, SCGETC_NONBLOCK)) != NOKEY) {
599
600         cur_tty = VIRTUAL_TTY(sc, sc->cur_scp->index);
601         if (!ISTTYOPEN(cur_tty)) {
602             cur_tty = sc_console_tty;
603             if (!ISTTYOPEN(cur_tty))
604                 continue;
605         }
606
607         if ((*sc->cur_scp->tsw->te_input)(sc->cur_scp, c, cur_tty))
608             continue;
609
610         switch (KEYFLAGS(c)) {
611         case 0x0000: /* normal key */
612             (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
613             break;
614         case FKEY:  /* function key, return string */
615             cp = kbd_get_fkeystr(thiskbd, KEYCHAR(c), &len);
616             if (cp != NULL) {
617                 while (len-- >  0)
618                     (*linesw[cur_tty->t_line].l_rint)(*cp++, cur_tty);
619             }
620             break;
621         case MKEY:  /* meta is active, prepend ESC */
622             (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
623             (*linesw[cur_tty->t_line].l_rint)(KEYCHAR(c), cur_tty);
624             break;
625         case BKEY:  /* backtab fixed sequence (esc [ Z) */
626             (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
627             (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
628             (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
629             break;
630         }
631     }
632
633     sc->cur_scp->status |= MOUSE_HIDDEN;
634
635     return 0;
636 }
637
638 static int
639 scparam(struct tty *tp, struct termios *t)
640 {
641     tp->t_ispeed = t->c_ispeed;
642     tp->t_ospeed = t->c_ospeed;
643     tp->t_cflag = t->c_cflag;
644     return 0;
645 }
646
647 int
648 scioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
649 {
650     int error;
651     int i;
652     struct tty *tp;
653     sc_softc_t *sc;
654     scr_stat *scp;
655     int s;
656     struct proc *p = td->td_proc;
657
658     KKASSERT(p);
659
660     tp = dev->si_tty;
661
662     /* If there is a user_ioctl function call that first */
663     if (sc_user_ioctl) {
664         error = (*sc_user_ioctl)(dev, cmd, data, flag, td);
665         if (error != ENOIOCTL)
666             return error;
667     }
668
669     error = sc_vid_ioctl(tp, cmd, data, flag, td);
670     if (error != ENOIOCTL)
671         return error;
672
673 #ifndef SC_NO_HISTORY
674     error = sc_hist_ioctl(tp, cmd, data, flag, td);
675     if (error != ENOIOCTL)
676         return error;
677 #endif
678
679 #ifndef SC_NO_SYSMOUSE
680     error = sc_mouse_ioctl(tp, cmd, data, flag, td);
681     if (error != ENOIOCTL)
682         return error;
683 #endif
684
685     scp = SC_STAT(tp->t_dev);
686     /* assert(scp != NULL) */
687     /* scp is sc_console, if SC_VTY(dev) == SC_CONSOLECTL. */
688     sc = scp->sc;
689
690     if (scp->tsw) {
691         error = (*scp->tsw->te_ioctl)(scp, tp, cmd, data, flag, td);
692         if (error != ENOIOCTL)
693             return error;
694     }
695
696     switch (cmd) {              /* process console hardware related ioctl's */
697
698     case GIO_ATTR:              /* get current attributes */
699         /* this ioctl is not processed here, but in the terminal emulator */
700         return ENOTTY;
701
702     case GIO_COLOR:             /* is this a color console ? */
703         *(int *)data = (sc->adp->va_flags & V_ADP_COLOR) ? 1 : 0;
704         return 0;
705
706     case CONS_BLANKTIME:        /* set screen saver timeout (0 = no saver) */
707         if (*(int *)data < 0 || *(int *)data > MAX_BLANKTIME)
708             return EINVAL;
709         s = spltty();
710         scrn_blank_time = *(int *)data;
711         run_scrn_saver = (scrn_blank_time != 0);
712         splx(s);
713         return 0;
714
715     case CONS_CURSORTYPE:       /* set cursor type blink/noblink */
716         s = spltty();
717         if (!ISGRAPHSC(sc->cur_scp))
718             sc_remove_cursor_image(sc->cur_scp);
719         if ((*(int*)data) & 0x01)
720             sc->flags |= SC_BLINK_CURSOR;
721         else
722             sc->flags &= ~SC_BLINK_CURSOR;
723         if ((*(int*)data) & 0x02) {
724             sc->flags |= SC_CHAR_CURSOR;
725         } else
726             sc->flags &= ~SC_CHAR_CURSOR;
727         /* 
728          * The cursor shape is global property; all virtual consoles
729          * are affected. Update the cursor in the current console...
730          */
731         if (!ISGRAPHSC(sc->cur_scp)) {
732             sc_set_cursor_image(sc->cur_scp);
733             sc_draw_cursor_image(sc->cur_scp);
734         }
735         splx(s);
736         return 0;
737
738     case CONS_BELLTYPE:         /* set bell type sound/visual */
739         if ((*(int *)data) & 0x01)
740             sc->flags |= SC_VISUAL_BELL;
741         else
742             sc->flags &= ~SC_VISUAL_BELL;
743         if ((*(int *)data) & 0x02)
744             sc->flags |= SC_QUIET_BELL;
745         else
746             sc->flags &= ~SC_QUIET_BELL;
747         return 0;
748
749     case CONS_GETINFO:          /* get current (virtual) console info */
750     {
751         vid_info_t *ptr = (vid_info_t*)data;
752         if (ptr->size == sizeof(struct vid_info)) {
753             ptr->m_num = sc->cur_scp->index;
754             ptr->font_size = scp->font_size;
755             ptr->mv_col = scp->xpos;
756             ptr->mv_row = scp->ypos;
757             ptr->mv_csz = scp->xsize;
758             ptr->mv_rsz = scp->ysize;
759             /*
760              * The following fields are filled by the terminal emulator. XXX
761              *
762              * ptr->mv_norm.fore
763              * ptr->mv_norm.back
764              * ptr->mv_rev.fore
765              * ptr->mv_rev.back
766              */
767             ptr->mv_grfc.fore = 0;      /* not supported */
768             ptr->mv_grfc.back = 0;      /* not supported */
769             ptr->mv_ovscan = scp->border;
770             if (scp == sc->cur_scp)
771                 save_kbd_state(scp);
772             ptr->mk_keylock = scp->status & LOCK_MASK;
773             return 0;
774         }
775         return EINVAL;
776     }
777
778     case CONS_GETVERS:          /* get version number */
779         *(int*)data = 0x200;    /* version 2.0 */
780         return 0;
781
782     case CONS_IDLE:             /* see if the screen has been idle */
783         /*
784          * When the screen is in the GRAPHICS_MODE or UNKNOWN_MODE,
785          * the user process may have been writing something on the
786          * screen and syscons is not aware of it. Declare the screen
787          * is NOT idle if it is in one of these modes. But there is
788          * an exception to it; if a screen saver is running in the 
789          * graphics mode in the current screen, we should say that the
790          * screen has been idle.
791          */
792         *(int *)data = (sc->flags & SC_SCRN_IDLE)
793                        && (!ISGRAPHSC(sc->cur_scp)
794                            || (sc->cur_scp->status & SAVER_RUNNING));
795         return 0;
796
797     case CONS_SAVERMODE:        /* set saver mode */
798         switch(*(int *)data) {
799         case CONS_NO_SAVER:
800         case CONS_USR_SAVER:
801             /* if a LKM screen saver is running, stop it first. */
802             scsplash_stick(FALSE);
803             saver_mode = *(int *)data;
804             s = spltty();
805 #if NSPLASH > 0
806             if ((error = wait_scrn_saver_stop(NULL))) {
807                 splx(s);
808                 return error;
809             }
810 #endif /* NSPLASH */
811             run_scrn_saver = TRUE;
812             if (saver_mode == CONS_USR_SAVER)
813                 scp->status |= SAVER_RUNNING;
814             else
815                 scp->status &= ~SAVER_RUNNING;
816             scsplash_stick(TRUE);
817             splx(s);
818             break;
819         case CONS_LKM_SAVER:
820             s = spltty();
821             if ((saver_mode == CONS_USR_SAVER) && (scp->status & SAVER_RUNNING))
822                 scp->status &= ~SAVER_RUNNING;
823             saver_mode = *(int *)data;
824             splx(s);
825             break;
826         default:
827             return EINVAL;
828         }
829         return 0;
830
831     case CONS_SAVERSTART:       /* immediately start/stop the screen saver */
832         /*
833          * Note that this ioctl does not guarantee the screen saver 
834          * actually starts or stops. It merely attempts to do so...
835          */
836         s = spltty();
837         run_scrn_saver = (*(int *)data != 0);
838         if (run_scrn_saver)
839             sc->scrn_time_stamp -= scrn_blank_time;
840         splx(s);
841         return 0;
842
843     case CONS_SCRSHOT:          /* get a screen shot */
844     {
845         scrshot_t *ptr = (scrshot_t*)data;
846         s = spltty();
847         if (ISGRAPHSC(scp)) {
848             splx(s);
849             return EOPNOTSUPP;
850         }
851         if (scp->xsize != ptr->xsize || scp->ysize != ptr->ysize) {
852             splx(s);
853             return EINVAL;
854         }
855         copyout ((void*)scp->vtb.vtb_buffer, ptr->buf,
856                  ptr->xsize * ptr->ysize * sizeof(u_int16_t));
857         splx(s);
858         return 0;
859     }
860
861     case VT_SETMODE:            /* set screen switcher mode */
862     {
863         struct vt_mode *mode;
864
865         mode = (struct vt_mode *)data;
866         DPRINTF(5, ("sc%d: VT_SETMODE ", sc->unit));
867         if (scp->smode.mode == VT_PROCESS) {
868             if (scp->proc == pfind(scp->pid) && scp->proc != p) {
869                 DPRINTF(5, ("error EPERM\n"));
870                 return EPERM;
871             }
872         }
873         s = spltty();
874         if (mode->mode == VT_AUTO) {
875             scp->smode.mode = VT_AUTO;
876             scp->proc = NULL;
877             scp->pid = 0;
878             DPRINTF(5, ("VT_AUTO, "));
879             if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
880                 cons_unavail = FALSE;
881             /* were we in the middle of the vty switching process? */
882             if (finish_vt_rel(scp, TRUE, &s) == 0)
883                 DPRINTF(5, ("reset WAIT_REL, "));
884             if (finish_vt_acq(scp) == 0)
885                 DPRINTF(5, ("reset WAIT_ACQ, "));
886         } else {
887             if (!ISSIGVALID(mode->relsig) || !ISSIGVALID(mode->acqsig)
888                 || !ISSIGVALID(mode->frsig)) {
889                 splx(s);
890                 DPRINTF(5, ("error EINVAL\n"));
891                 return EINVAL;
892             }
893             DPRINTF(5, ("VT_PROCESS %d, ", p->p_pid));
894             bcopy(data, &scp->smode, sizeof(struct vt_mode));
895             scp->proc = p;
896             scp->pid = scp->proc->p_pid;
897             if ((scp == sc->cur_scp) && (sc->unit == sc_console_unit))
898                 cons_unavail = TRUE;
899         }
900         splx(s);
901         DPRINTF(5, ("\n"));
902         return 0;
903     }
904
905     case VT_GETMODE:            /* get screen switcher mode */
906         bcopy(&scp->smode, data, sizeof(struct vt_mode));
907         return 0;
908
909     case VT_RELDISP:            /* screen switcher ioctl */
910         s = spltty();
911         /*
912          * This must be the current vty which is in the VT_PROCESS
913          * switching mode...
914          */
915         if ((scp != sc->cur_scp) || (scp->smode.mode != VT_PROCESS)) {
916             splx(s);
917             return EINVAL;
918         }
919         /* ...and this process is controlling it. */
920         if (scp->proc != p) {
921             splx(s);
922             return EPERM;
923         }
924         error = EINVAL;
925         switch(*(int *)data) {
926         case VT_FALSE:          /* user refuses to release screen, abort */
927             if ((error = finish_vt_rel(scp, FALSE, &s)) == 0)
928                 DPRINTF(5, ("sc%d: VT_FALSE\n", sc->unit));
929             break;
930         case VT_TRUE:           /* user has released screen, go on */
931             if ((error = finish_vt_rel(scp, TRUE, &s)) == 0)
932                 DPRINTF(5, ("sc%d: VT_TRUE\n", sc->unit));
933             break;
934         case VT_ACKACQ:         /* acquire acknowledged, switch completed */
935             if ((error = finish_vt_acq(scp)) == 0)
936                 DPRINTF(5, ("sc%d: VT_ACKACQ\n", sc->unit));
937             break;
938         default:
939             break;
940         }
941         splx(s);
942         return error;
943
944     case VT_OPENQRY:            /* return free virtual console */
945         for (i = sc->first_vty; i < sc->first_vty + sc->vtys; i++) {
946             tp = VIRTUAL_TTY(sc, i);
947             if (!ISTTYOPEN(tp)) {
948                 *(int *)data = i + 1;
949                 return 0;
950             }
951         }
952         return EINVAL;
953
954     case VT_ACTIVATE:           /* switch to screen *data */
955         i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
956         s = spltty();
957         sc_clean_up(sc->cur_scp);
958         splx(s);
959         return sc_switch_scr(sc, i);
960
961     case VT_WAITACTIVE:         /* wait for switch to occur */
962         i = (*(int *)data == 0) ? scp->index : (*(int *)data - 1);
963         if ((i < sc->first_vty) || (i >= sc->first_vty + sc->vtys))
964             return EINVAL;
965         s = spltty();
966         error = sc_clean_up(sc->cur_scp);
967         splx(s);
968         if (error)
969             return error;
970         scp = SC_STAT(SC_DEV(sc, i));
971         if (scp == scp->sc->cur_scp)
972             return 0;
973         while ((error=tsleep((caddr_t)&scp->smode, PCATCH,
974                              "waitvt", 0)) == ERESTART) ;
975         return error;
976
977     case VT_GETACTIVE:          /* get active vty # */
978         *(int *)data = sc->cur_scp->index + 1;
979         return 0;
980
981     case VT_GETINDEX:           /* get this vty # */
982         *(int *)data = scp->index + 1;
983         return 0;
984
985     case VT_LOCKSWITCH:         /* prevent vty switching */
986         if ((*(int *)data) & 0x01)
987             sc->flags |= SC_SCRN_VTYLOCK;
988         else
989             sc->flags &= ~SC_SCRN_VTYLOCK;
990         return 0;
991
992     case KDENABIO:              /* allow io operations */
993         error = suser(td);
994         if (error != 0)
995             return error;
996         if (securelevel > 0)
997             return EPERM;
998 #ifdef __i386__
999         p->p_md.md_regs->tf_eflags |= PSL_IOPL;
1000 #endif
1001         return 0;
1002
1003     case KDDISABIO:             /* disallow io operations (default) */
1004 #ifdef __i386__
1005         p->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
1006 #endif
1007         return 0;
1008
1009     case KDSKBSTATE:            /* set keyboard state (locks) */
1010         if (*(int *)data & ~LOCK_MASK)
1011             return EINVAL;
1012         scp->status &= ~LOCK_MASK;
1013         scp->status |= *(int *)data;
1014         if (scp == sc->cur_scp)
1015             update_kbd_state(scp, scp->status, LOCK_MASK);
1016         return 0;
1017
1018     case KDGKBSTATE:            /* get keyboard state (locks) */
1019         if (scp == sc->cur_scp)
1020             save_kbd_state(scp);
1021         *(int *)data = scp->status & LOCK_MASK;
1022         return 0;
1023
1024     case KDGETREPEAT:           /* get keyboard repeat & delay rates */
1025     case KDSETREPEAT:           /* set keyboard repeat & delay rates (new) */
1026         error = kbd_ioctl(sc->kbd, cmd, data);
1027         if (error == ENOIOCTL)
1028             error = ENODEV;
1029         return error;
1030
1031     case KDSETRAD:              /* set keyboard repeat & delay rates (old) */
1032         if (*(int *)data & ~0x7f)
1033             return EINVAL;
1034         error = kbd_ioctl(sc->kbd, cmd, data);
1035         if (error == ENOIOCTL)
1036             error = ENODEV;
1037         return error;
1038
1039     case KDSKBMODE:             /* set keyboard mode */
1040         switch (*(int *)data) {
1041         case K_XLATE:           /* switch to XLT ascii mode */
1042         case K_RAW:             /* switch to RAW scancode mode */
1043         case K_CODE:            /* switch to CODE mode */
1044             scp->kbd_mode = *(int *)data;
1045             if (scp == sc->cur_scp)
1046                 kbd_ioctl(sc->kbd, cmd, data);
1047             return 0;
1048         default:
1049             return EINVAL;
1050         }
1051         /* NOT REACHED */
1052
1053     case KDGKBMODE:             /* get keyboard mode */
1054         *(int *)data = scp->kbd_mode;
1055         return 0;
1056
1057     case KDGKBINFO:
1058         error = kbd_ioctl(sc->kbd, cmd, data);
1059         if (error == ENOIOCTL)
1060             error = ENODEV;
1061         return error;
1062
1063     case KDMKTONE:              /* sound the bell */
1064         if (*(int*)data)
1065             sc_bell(scp, (*(int*)data)&0xffff,
1066                     (((*(int*)data)>>16)&0xffff)*hz/1000);
1067         else
1068             sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1069         return 0;
1070
1071     case KIOCSOUND:             /* make tone (*data) hz */
1072         if (scp == sc->cur_scp) {
1073             if (*(int *)data)
1074                 return sc_tone(*(int *)data);
1075             else
1076                 return sc_tone(0);
1077         }
1078         return 0;
1079
1080     case KDGKBTYPE:             /* get keyboard type */
1081         error = kbd_ioctl(sc->kbd, cmd, data);
1082         if (error == ENOIOCTL) {
1083             /* always return something? XXX */
1084             *(int *)data = 0;
1085         }
1086         return 0;
1087
1088     case KDSETLED:              /* set keyboard LED status */
1089         if (*(int *)data & ~LED_MASK)   /* FIXME: LOCK_MASK? */
1090             return EINVAL;
1091         scp->status &= ~LED_MASK;
1092         scp->status |= *(int *)data;
1093         if (scp == sc->cur_scp)
1094             update_kbd_leds(scp, scp->status);
1095         return 0;
1096
1097     case KDGETLED:              /* get keyboard LED status */
1098         if (scp == sc->cur_scp)
1099             save_kbd_state(scp);
1100         *(int *)data = scp->status & LED_MASK;
1101         return 0;
1102
1103     case CONS_SETKBD:           /* set the new keyboard */
1104         {
1105             keyboard_t *newkbd;
1106
1107             s = spltty();
1108             newkbd = kbd_get_keyboard(*(int *)data);
1109             if (newkbd == NULL) {
1110                 splx(s);
1111                 return EINVAL;
1112             }
1113             error = 0;
1114             if (sc->kbd != newkbd) {
1115                 i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1116                                  (void *)&sc->keyboard, sckbdevent, sc);
1117                 /* i == newkbd->kb_index */
1118                 if (i >= 0) {
1119                     if (sc->kbd != NULL) {
1120                         save_kbd_state(sc->cur_scp);
1121                         kbd_release(sc->kbd, (void *)&sc->keyboard);
1122                     }
1123                     sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1124                     sc->keyboard = i;
1125                     kbd_ioctl(sc->kbd, KDSKBMODE,
1126                               (caddr_t)&sc->cur_scp->kbd_mode);
1127                     update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1128                                      LOCK_MASK);
1129                 } else {
1130                     error = EPERM;      /* XXX */
1131                 }
1132             }
1133             splx(s);
1134             return error;
1135         }
1136
1137     case CONS_RELKBD:           /* release the current keyboard */
1138         s = spltty();
1139         error = 0;
1140         if (sc->kbd != NULL) {
1141             save_kbd_state(sc->cur_scp);
1142             error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1143             if (error == 0) {
1144                 sc->kbd = NULL;
1145                 sc->keyboard = -1;
1146             }
1147         }
1148         splx(s);
1149         return error;
1150
1151     case CONS_GETTERM:          /* get the current terminal emulator info */
1152         {
1153             sc_term_sw_t *sw;
1154
1155             if (((term_info_t *)data)->ti_index == 0) {
1156                 sw = scp->tsw;
1157             } else {
1158                 sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1159             }
1160             if (sw != NULL) {
1161                 strncpy(((term_info_t *)data)->ti_name, sw->te_name, 
1162                         sizeof(((term_info_t *)data)->ti_name));
1163                 strncpy(((term_info_t *)data)->ti_desc, sw->te_desc, 
1164                         sizeof(((term_info_t *)data)->ti_desc));
1165                 ((term_info_t *)data)->ti_flags = 0;
1166                 return 0;
1167             } else {
1168                 ((term_info_t *)data)->ti_name[0] = '\0';
1169                 ((term_info_t *)data)->ti_desc[0] = '\0';
1170                 ((term_info_t *)data)->ti_flags = 0;
1171                 return EINVAL;
1172             }
1173         }
1174
1175     case CONS_SETTERM:          /* set the current terminal emulator */
1176         s = spltty();
1177         error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1178         /* FIXME: what if scp == sc_console! XXX */
1179         splx(s);
1180         return error;
1181
1182     case GIO_SCRNMAP:           /* get output translation table */
1183         bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1184         return 0;
1185
1186     case PIO_SCRNMAP:           /* set output translation table */
1187         bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1188         for (i=0; i<sizeof(sc->scr_map); i++) {
1189             sc->scr_rmap[sc->scr_map[i]] = i;
1190         }
1191         return 0;
1192
1193     case GIO_KEYMAP:            /* get keyboard translation table */
1194     case PIO_KEYMAP:            /* set keyboard translation table */
1195     case GIO_DEADKEYMAP:        /* get accent key translation table */
1196     case PIO_DEADKEYMAP:        /* set accent key translation table */
1197     case GETFKEY:               /* get function key string */
1198     case SETFKEY:               /* set function key string */
1199         error = kbd_ioctl(sc->kbd, cmd, data);
1200         if (error == ENOIOCTL)
1201             error = ENODEV;
1202         return error;
1203
1204 #ifndef SC_NO_FONT_LOADING
1205
1206     case PIO_FONT8x8:           /* set 8x8 dot font */
1207         if (!ISFONTAVAIL(sc->adp->va_flags))
1208             return ENXIO;
1209         bcopy(data, sc->font_8, 8*256);
1210         sc->fonts_loaded |= FONT_8;
1211         /*
1212          * FONT KLUDGE
1213          * Always use the font page #0. XXX
1214          * Don't load if the current font size is not 8x8.
1215          */
1216         if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1217             sc_load_font(sc->cur_scp, 0, 8, sc->font_8, 0, 256);
1218         return 0;
1219
1220     case GIO_FONT8x8:           /* get 8x8 dot font */
1221         if (!ISFONTAVAIL(sc->adp->va_flags))
1222             return ENXIO;
1223         if (sc->fonts_loaded & FONT_8) {
1224             bcopy(sc->font_8, data, 8*256);
1225             return 0;
1226         }
1227         else
1228             return ENXIO;
1229
1230     case PIO_FONT8x14:          /* set 8x14 dot font */
1231         if (!ISFONTAVAIL(sc->adp->va_flags))
1232             return ENXIO;
1233         bcopy(data, sc->font_14, 14*256);
1234         sc->fonts_loaded |= FONT_14;
1235         /*
1236          * FONT KLUDGE
1237          * Always use the font page #0. XXX
1238          * Don't load if the current font size is not 8x14.
1239          */
1240         if (ISTEXTSC(sc->cur_scp)
1241             && (sc->cur_scp->font_size >= 14)
1242             && (sc->cur_scp->font_size < 16))
1243             sc_load_font(sc->cur_scp, 0, 14, sc->font_14, 0, 256);
1244         return 0;
1245
1246     case GIO_FONT8x14:          /* get 8x14 dot font */
1247         if (!ISFONTAVAIL(sc->adp->va_flags))
1248             return ENXIO;
1249         if (sc->fonts_loaded & FONT_14) {
1250             bcopy(sc->font_14, data, 14*256);
1251             return 0;
1252         }
1253         else
1254             return ENXIO;
1255
1256     case PIO_FONT8x16:          /* set 8x16 dot font */
1257         if (!ISFONTAVAIL(sc->adp->va_flags))
1258             return ENXIO;
1259         bcopy(data, sc->font_16, 16*256);
1260         sc->fonts_loaded |= FONT_16;
1261         /*
1262          * FONT KLUDGE
1263          * Always use the font page #0. XXX
1264          * Don't load if the current font size is not 8x16.
1265          */
1266         if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1267             sc_load_font(sc->cur_scp, 0, 16, sc->font_16, 0, 256);
1268         return 0;
1269
1270     case GIO_FONT8x16:          /* get 8x16 dot font */
1271         if (!ISFONTAVAIL(sc->adp->va_flags))
1272             return ENXIO;
1273         if (sc->fonts_loaded & FONT_16) {
1274             bcopy(sc->font_16, data, 16*256);
1275             return 0;
1276         }
1277         else
1278             return ENXIO;
1279
1280 #endif /* SC_NO_FONT_LOADING */
1281
1282     default:
1283         break;
1284     }
1285
1286     error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
1287     if (error != ENOIOCTL)
1288         return(error);
1289     error = ttioctl(tp, cmd, data, flag);
1290     if (error != ENOIOCTL)
1291         return(error);
1292     return(ENOTTY);
1293 }
1294
1295 static void
1296 scstart(struct tty *tp)
1297 {
1298     struct clist *rbp;
1299     int s, len;
1300     u_char buf[PCBURST];
1301     scr_stat *scp = SC_STAT(tp->t_dev);
1302
1303     if (scp->status & SLKED ||
1304         (scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1305         return;
1306     s = spltty();
1307     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1308         tp->t_state |= TS_BUSY;
1309         rbp = &tp->t_outq;
1310         while (rbp->c_cc) {
1311             len = q_to_b(rbp, buf, PCBURST);
1312             splx(s);
1313             sc_puts(scp, buf, len);
1314             s = spltty();
1315         }
1316         tp->t_state &= ~TS_BUSY;
1317         ttwwakeup(tp);
1318     }
1319     splx(s);
1320 }
1321
1322 static void
1323 sccnprobe(struct consdev *cp)
1324 {
1325 #if __i386__
1326     int unit;
1327     int flags;
1328
1329     cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1330
1331     /* a video card is always required */
1332     if (!scvidprobe(unit, flags, TRUE))
1333         cp->cn_pri = CN_DEAD;
1334
1335     /* syscons will become console even when there is no keyboard */
1336     sckbdprobe(unit, flags, TRUE);
1337
1338     if (cp->cn_pri == CN_DEAD)
1339         return;
1340
1341     /* initialize required fields */
1342     cp->cn_dev = make_dev(&sc_cdevsw, SC_CONSOLECTL,
1343                    UID_ROOT, GID_WHEEL, 0600, "consolectl");
1344 #endif /* __i386__ */
1345
1346 #if __alpha__
1347     /*
1348      * alpha use sccnattach() rather than cnprobe()/cninit()/cnterm()
1349      * interface to install the console.  Always return CN_DEAD from
1350      * here.
1351      */
1352     cp->cn_pri = CN_DEAD;
1353 #endif /* __alpha__ */
1354 }
1355
1356 static void
1357 sccninit(struct consdev *cp)
1358 {
1359 #if __i386__
1360     int unit;
1361     int flags;
1362
1363     sc_get_cons_priority(&unit, &flags);
1364     scinit(unit, flags | SC_KERNEL_CONSOLE);
1365     sc_console_unit = unit;
1366     sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1367 #endif /* __i386__ */
1368
1369 #if __alpha__
1370     /* SHOULDN'T REACH HERE */
1371 #endif /* __alpha__ */
1372 }
1373
1374 static void
1375 sccnterm(struct consdev *cp)
1376 {
1377     /* we are not the kernel console any more, release everything */
1378
1379     if (sc_console_unit < 0)
1380         return;                 /* shouldn't happen */
1381
1382 #if __i386__
1383 #if 0 /* XXX */
1384     sc_clear_screen(sc_console);
1385     sccnupdate(sc_console);
1386 #endif
1387     scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1388     sc_console_unit = -1;
1389     sc_console = NULL;
1390 #endif /* __i386__ */
1391
1392 #if __alpha__
1393     /* do nothing XXX */
1394 #endif /* __alpha__ */
1395 }
1396
1397 #ifdef __alpha__
1398
1399 void
1400 sccnattach(void)
1401 {
1402     static struct consdev consdev;
1403     int unit;
1404     int flags;
1405
1406     bcopy(&sc_consdev, &consdev, sizeof(sc_consdev));
1407     consdev.cn_pri = sc_get_cons_priority(&unit, &flags);
1408
1409     /* a video card is always required */
1410     if (!scvidprobe(unit, flags, TRUE))
1411         consdev.cn_pri = CN_DEAD;
1412
1413     /* alpha doesn't allow the console being without a keyboard... Why? */
1414     if (!sckbdprobe(unit, flags, TRUE))
1415         consdev.cn_pri = CN_DEAD;
1416
1417     if (consdev.cn_pri == CN_DEAD)
1418         return;
1419
1420     scinit(unit, flags | SC_KERNEL_CONSOLE);
1421     sc_console_unit = unit;
1422     sc_console = SC_STAT(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1423     consdev.cn_dev = make_dev(&sc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
1424                                 "ttyv%r", 0);
1425     cn_tab = &consdev;
1426 }
1427
1428 #endif /* __alpha__ */
1429
1430 static void
1431 sccnputc(dev_t dev, int c)
1432 {
1433     u_char buf[1];
1434     scr_stat *scp = sc_console;
1435     void *save;
1436 #ifndef SC_NO_HISTORY
1437     struct tty *tp;
1438 #endif /* !SC_NO_HISTORY */
1439     int s;
1440
1441     /* assert(sc_console != NULL) */
1442
1443 #ifndef SC_NO_HISTORY
1444     if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1445         scp->status &= ~SLKED;
1446         update_kbd_state(scp, scp->status, SLKED);
1447         if (scp->status & BUFFER_SAVED) {
1448             if (!sc_hist_restore(scp))
1449                 sc_remove_cutmarking(scp);
1450             scp->status &= ~BUFFER_SAVED;
1451             scp->status |= CURSOR_ENABLED;
1452             sc_draw_cursor_image(scp);
1453         }
1454         tp = VIRTUAL_TTY(scp->sc, scp->index);
1455         if (ISTTYOPEN(tp))
1456             scstart(tp);
1457     }
1458 #endif /* !SC_NO_HISTORY */
1459
1460     save = scp->ts;
1461     if (kernel_console_ts != NULL)
1462         scp->ts = kernel_console_ts;
1463     buf[0] = c;
1464     sc_puts(scp, buf, 1);
1465     scp->ts = save;
1466
1467     s = spltty();       /* block sckbdevent and scrn_timer */
1468     sccnupdate(scp);
1469     splx(s);
1470 }
1471
1472 static int
1473 sccngetc(dev_t dev)
1474 {
1475     return sccngetch(0);
1476 }
1477
1478 static int
1479 sccncheckc(dev_t dev)
1480 {
1481     return sccngetch(SCGETC_NONBLOCK);
1482 }
1483
1484 static void
1485 sccndbctl(dev_t dev, int on)
1486 {
1487     /* assert(sc_console_unit >= 0) */
1488     /* try to switch to the kernel console screen */
1489     if (on && debugger == 0) {
1490         /*
1491          * TRY to make sure the screen saver is stopped, 
1492          * and the screen is updated before switching to 
1493          * the vty0.
1494          */
1495         scrn_timer(NULL);
1496         if (!cold
1497             && sc_console->sc->cur_scp->smode.mode == VT_AUTO
1498             && sc_console->smode.mode == VT_AUTO) {
1499             sc_console->sc->cur_scp->status |= MOUSE_HIDDEN;
1500             sc_switch_scr(sc_console->sc, sc_console->index);
1501         }
1502     }
1503     if (on)
1504         ++debugger;
1505     else
1506         --debugger;
1507 }
1508
1509 static int
1510 sccngetch(int flags)
1511 {
1512     static struct fkeytab fkey;
1513     static int fkeycp;
1514     scr_stat *scp;
1515     u_char *p;
1516     int cur_mode;
1517     int s = spltty();   /* block sckbdevent and scrn_timer while we poll */
1518     int c;
1519
1520     /* assert(sc_console != NULL) */
1521
1522     /* 
1523      * Stop the screen saver and update the screen if necessary.
1524      * What if we have been running in the screen saver code... XXX
1525      */
1526     sc_touch_scrn_saver();
1527     scp = sc_console->sc->cur_scp;      /* XXX */
1528     sccnupdate(scp);
1529
1530     if (fkeycp < fkey.len) {
1531         splx(s);
1532         return fkey.str[fkeycp++];
1533     }
1534
1535     if (scp->sc->kbd == NULL) {
1536         splx(s);
1537         return -1;
1538     }
1539
1540     /* 
1541      * Make sure the keyboard is accessible even when the kbd device
1542      * driver is disabled.
1543      */
1544     kbd_enable(scp->sc->kbd);
1545
1546     /* we shall always use the keyboard in the XLATE mode here */
1547     cur_mode = scp->kbd_mode;
1548     scp->kbd_mode = K_XLATE;
1549     kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1550
1551     kbd_poll(scp->sc->kbd, TRUE);
1552     c = scgetc(scp->sc, SCGETC_CN | flags);
1553     kbd_poll(scp->sc->kbd, FALSE);
1554
1555     scp->kbd_mode = cur_mode;
1556     kbd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1557     kbd_disable(scp->sc->kbd);
1558     splx(s);
1559
1560     switch (KEYFLAGS(c)) {
1561     case 0:     /* normal char */
1562         return KEYCHAR(c);
1563     case FKEY:  /* function key */
1564         p = kbd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1565         fkey.len = fkeycp;
1566         if ((p != NULL) && (fkey.len > 0)) {
1567             bcopy(p, fkey.str, fkey.len);
1568             fkeycp = 1;
1569             return fkey.str[0];
1570         }
1571         return c;       /* XXX */
1572     case NOKEY:
1573     case ERRKEY:
1574     default:
1575         return -1;
1576     }
1577     /* NOT REACHED */
1578 }
1579
1580 static void
1581 sccnupdate(scr_stat *scp)
1582 {
1583     /* this is a cut-down version of scrn_timer()... */
1584
1585     if (scp->sc->font_loading_in_progress || scp->sc->videoio_in_progress)
1586         return;
1587
1588     if (debugger > 0 || panicstr || shutdown_in_progress) {
1589         sc_touch_scrn_saver();
1590     } else if (scp != scp->sc->cur_scp) {
1591         return;
1592     }
1593
1594     if (!run_scrn_saver)
1595         scp->sc->flags &= ~SC_SCRN_IDLE;
1596 #if NSPLASH > 0
1597     if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1598         if (scp->sc->flags & SC_SCRN_BLANKED)
1599             stop_scrn_saver(scp->sc, current_saver);
1600 #endif /* NSPLASH */
1601
1602     if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1603         || scp->sc->switch_in_progress)
1604         return;
1605     /*
1606      * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1607      * when write_in_progress is non-zero.  XXX
1608      */
1609
1610     if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1611         scrn_update(scp, TRUE);
1612 }
1613
1614 static void
1615 scrn_timer(void *arg)
1616 {
1617     static int kbd_interval = 0;
1618     struct timeval tv;
1619     sc_softc_t *sc;
1620     scr_stat *scp;
1621     int again;
1622     int s;
1623
1624     again = (arg != NULL);
1625     if (arg != NULL)
1626         sc = (sc_softc_t *)arg;
1627     else if (sc_console != NULL)
1628         sc = sc_console->sc;
1629     else
1630         return;
1631
1632     /* don't do anything when we are performing some I/O operations */
1633     if (sc->font_loading_in_progress || sc->videoio_in_progress) {
1634         if (again)
1635             callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1636         return;
1637     }
1638     s = spltty();
1639
1640     if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1641         /* try to allocate a keyboard automatically */
1642         if (++kbd_interval >= 25) {
1643             sc->keyboard = kbd_allocate("*", -1, (void *)&sc->keyboard,
1644                                         sckbdevent, sc);
1645             if (sc->keyboard >= 0) {
1646                 sc->kbd = kbd_get_keyboard(sc->keyboard);
1647                 kbd_ioctl(sc->kbd, KDSKBMODE,
1648                           (caddr_t)&sc->cur_scp->kbd_mode);
1649                 update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1650                                  LOCK_MASK);
1651             }
1652             kbd_interval = 0;
1653         }
1654     }
1655
1656     /* find the vty to update */
1657     scp = sc->cur_scp;
1658
1659     /* should we stop the screen saver? */
1660     getmicrouptime(&tv);
1661     if (debugger > 0 || panicstr || shutdown_in_progress)
1662         sc_touch_scrn_saver();
1663     if (run_scrn_saver) {
1664         if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1665             sc->flags |= SC_SCRN_IDLE;
1666         else
1667             sc->flags &= ~SC_SCRN_IDLE;
1668     } else {
1669         sc->scrn_time_stamp = tv.tv_sec;
1670         sc->flags &= ~SC_SCRN_IDLE;
1671         if (scrn_blank_time > 0)
1672             run_scrn_saver = TRUE;
1673     }
1674 #if NSPLASH > 0
1675     if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1676         if (sc->flags & SC_SCRN_BLANKED)
1677             stop_scrn_saver(sc, current_saver);
1678 #endif /* NSPLASH */
1679
1680     /* should we just return ? */
1681     if (sc->blink_in_progress || sc->switch_in_progress
1682         || sc->write_in_progress) {
1683         if (again)
1684             callout_reset(&sc->scrn_timer_ch, hz / 10, scrn_timer, sc);
1685         splx(s);
1686         return;
1687     }
1688
1689     /* Update the screen */
1690     scp = sc->cur_scp;          /* cur_scp may have changed... */
1691     if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1692         scrn_update(scp, TRUE);
1693
1694 #if NSPLASH > 0
1695     /* should we activate the screen saver? */
1696     if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1697         if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1698             (*current_saver)(sc, TRUE);
1699 #endif /* NSPLASH */
1700
1701     if (again)
1702         callout_reset(&sc->scrn_timer_ch, hz / 25, scrn_timer, sc);
1703     splx(s);
1704 }
1705
1706 static int
1707 and_region(int *s1, int *e1, int s2, int e2)
1708 {
1709     if (*e1 < s2 || e2 < *s1)
1710         return FALSE;
1711     *s1 = imax(*s1, s2);
1712     *e1 = imin(*e1, e2);
1713     return TRUE;
1714 }
1715
1716 static void 
1717 scrn_update(scr_stat *scp, int show_cursor)
1718 {
1719     int start;
1720     int end;
1721     int s;
1722     int e;
1723
1724     /* assert(scp == scp->sc->cur_scp) */
1725
1726     ++scp->sc->videoio_in_progress;
1727
1728 #ifndef SC_NO_CUTPASTE
1729     /* remove the previous mouse pointer image if necessary */
1730     if (scp->status & MOUSE_VISIBLE) {
1731         s = scp->mouse_pos;
1732         e = scp->mouse_pos + scp->xsize + 1;
1733         if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1734             || and_region(&s, &e, scp->start, scp->end)
1735             || ((scp->status & CURSOR_ENABLED) && 
1736                 (scp->cursor_pos != scp->cursor_oldpos) &&
1737                 (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1738                  || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1739             sc_remove_mouse_image(scp);
1740             if (scp->end >= scp->xsize*scp->ysize)
1741                 scp->end = scp->xsize*scp->ysize - 1;
1742         }
1743     }
1744 #endif /* !SC_NO_CUTPASTE */
1745
1746 #if 1
1747     /* debug: XXX */
1748     if (scp->end >= scp->xsize*scp->ysize) {
1749         printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1750         scp->end = scp->xsize*scp->ysize - 1;
1751     }
1752     if (scp->start < 0) {
1753         printf("scrn_update(): scp->start %d < 0\n", scp->start);
1754         scp->start = 0;
1755     }
1756 #endif
1757
1758     /* update screen image */
1759     if (scp->start <= scp->end)  {
1760         if (scp->mouse_cut_end >= 0) {
1761             /* there is a marked region for cut & paste */
1762             if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1763                 start = scp->mouse_cut_start;
1764                 end = scp->mouse_cut_end;
1765             } else {
1766                 start = scp->mouse_cut_end;
1767                 end = scp->mouse_cut_start - 1;
1768             }
1769             s = start;
1770             e = end;
1771             /* does the cut-mark region overlap with the update region? */
1772             if (and_region(&s, &e, scp->start, scp->end)) {
1773                 (*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1774                 s = 0;
1775                 e = start - 1;
1776                 if (and_region(&s, &e, scp->start, scp->end))
1777                     (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1778                 s = end + 1;
1779                 e = scp->xsize*scp->ysize - 1;
1780                 if (and_region(&s, &e, scp->start, scp->end))
1781                     (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1782             } else {
1783                 (*scp->rndr->draw)(scp, scp->start,
1784                                    scp->end - scp->start + 1, FALSE);
1785             }
1786         } else {
1787             (*scp->rndr->draw)(scp, scp->start,
1788                                scp->end - scp->start + 1, FALSE);
1789         }
1790     }
1791
1792     /* we are not to show the cursor and the mouse pointer... */
1793     if (!show_cursor) {
1794         scp->end = 0;
1795         scp->start = scp->xsize*scp->ysize - 1;
1796         --scp->sc->videoio_in_progress;
1797         return;
1798     }
1799
1800     /* update cursor image */
1801     if (scp->status & CURSOR_ENABLED) {
1802         s = scp->start;
1803         e = scp->end;
1804         /* did cursor move since last time ? */
1805         if (scp->cursor_pos != scp->cursor_oldpos) {
1806             /* do we need to remove old cursor image ? */
1807             if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1808                 sc_remove_cursor_image(scp);
1809             sc_draw_cursor_image(scp);
1810         } else {
1811             if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1812                 /* cursor didn't move, but has been overwritten */
1813                 sc_draw_cursor_image(scp);
1814             else if (scp->sc->flags & SC_BLINK_CURSOR)
1815                 /* if it's a blinking cursor, update it */
1816                 (*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1817                                            sc_inside_cutmark(scp,
1818                                                scp->cursor_pos));
1819         }
1820     }
1821
1822 #ifndef SC_NO_CUTPASTE
1823     /* update "pseudo" mouse pointer image */
1824     if (scp->sc->flags & SC_MOUSE_ENABLED) {
1825         if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1826             scp->status &= ~MOUSE_MOVED;
1827             sc_draw_mouse_image(scp);
1828         }
1829     }
1830 #endif /* SC_NO_CUTPASTE */
1831
1832     scp->end = 0;
1833     scp->start = scp->xsize*scp->ysize - 1;
1834
1835     --scp->sc->videoio_in_progress;
1836 }
1837
1838 #if NSPLASH > 0
1839 static int
1840 scsplash_callback(int event, void *arg)
1841 {
1842     sc_softc_t *sc;
1843     int error;
1844
1845     sc = (sc_softc_t *)arg;
1846
1847     switch (event) {
1848     case SPLASH_INIT:
1849         if (add_scrn_saver(scsplash_saver) == 0) {
1850             sc->flags &= ~SC_SAVER_FAILED;
1851             run_scrn_saver = TRUE;
1852             if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
1853                 scsplash_stick(TRUE);
1854                 (*current_saver)(sc, TRUE);
1855             }
1856         }
1857         return 0;
1858
1859     case SPLASH_TERM:
1860         if (current_saver == scsplash_saver) {
1861             scsplash_stick(FALSE);
1862             error = remove_scrn_saver(scsplash_saver);
1863             if (error)
1864                 return error;
1865         }
1866         return 0;
1867
1868     default:
1869         return EINVAL;
1870     }
1871 }
1872
1873 static void
1874 scsplash_saver(sc_softc_t *sc, int show)
1875 {
1876     static int busy = FALSE;
1877     scr_stat *scp;
1878
1879     if (busy)
1880         return;
1881     busy = TRUE;
1882
1883     scp = sc->cur_scp;
1884     if (show) {
1885         if (!(sc->flags & SC_SAVER_FAILED)) {
1886             if (!(sc->flags & SC_SCRN_BLANKED))
1887                 set_scrn_saver_mode(scp, -1, NULL, 0);
1888             switch (splash(sc->adp, TRUE)) {
1889             case 0:             /* succeeded */
1890                 break;
1891             case EAGAIN:        /* try later */
1892                 restore_scrn_saver_mode(scp, FALSE);
1893                 sc_touch_scrn_saver();          /* XXX */
1894                 break;
1895             default:
1896                 sc->flags |= SC_SAVER_FAILED;
1897                 scsplash_stick(FALSE);
1898                 restore_scrn_saver_mode(scp, TRUE);
1899                 printf("scsplash_saver(): failed to put up the image\n");
1900                 break;
1901             }
1902         }
1903     } else if (!sticky_splash) {
1904         if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1905             restore_scrn_saver_mode(scp, TRUE);
1906     }
1907     busy = FALSE;
1908 }
1909
1910 static int
1911 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1912 {
1913 #if 0
1914     int error;
1915
1916     if (current_saver != none_saver) {
1917         error = remove_scrn_saver(current_saver);
1918         if (error)
1919             return error;
1920     }
1921 #endif
1922     if (current_saver != none_saver)
1923         return EBUSY;
1924
1925     run_scrn_saver = FALSE;
1926     saver_mode = CONS_LKM_SAVER;
1927     current_saver = this_saver;
1928     return 0;
1929 }
1930
1931 static int
1932 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1933 {
1934     if (current_saver != this_saver)
1935         return EINVAL;
1936
1937 #if 0
1938     /*
1939      * In order to prevent `current_saver' from being called by
1940      * the timeout routine `scrn_timer()' while we manipulate 
1941      * the saver list, we shall set `current_saver' to `none_saver' 
1942      * before stopping the current saver, rather than blocking by `splXX()'.
1943      */
1944     current_saver = none_saver;
1945     if (scrn_blanked)
1946         stop_scrn_saver(this_saver);
1947 #endif
1948
1949     /* unblank all blanked screens */
1950     wait_scrn_saver_stop(NULL);
1951     if (scrn_blanked)
1952         return EBUSY;
1953
1954     current_saver = none_saver;
1955     return 0;
1956 }
1957
1958 static int
1959 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
1960 {
1961     int s;
1962
1963     /* assert(scp == scp->sc->cur_scp) */
1964     s = spltty();
1965     if (!ISGRAPHSC(scp))
1966         sc_remove_cursor_image(scp);
1967     scp->splash_save_mode = scp->mode;
1968     scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
1969     scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
1970     scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
1971     scp->sc->flags |= SC_SCRN_BLANKED;
1972     ++scrn_blanked;
1973     splx(s);
1974     if (mode < 0)
1975         return 0;
1976     scp->mode = mode;
1977     if (set_mode(scp) == 0) {
1978         if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
1979             scp->status |= GRAPHICS_MODE;
1980 #ifndef SC_NO_PALETTE_LOADING
1981         if (pal != NULL)
1982             load_palette(scp->sc->adp, pal);
1983 #endif
1984         sc_set_border(scp, border);
1985         return 0;
1986     } else {
1987         s = spltty();
1988         scp->mode = scp->splash_save_mode;
1989         scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1990         scp->status |= scp->splash_save_status;
1991         splx(s);
1992         return 1;
1993     }
1994 }
1995
1996 static int
1997 restore_scrn_saver_mode(scr_stat *scp, int changemode)
1998 {
1999     int mode;
2000     int status;
2001     int s;
2002
2003     /* assert(scp == scp->sc->cur_scp) */
2004     s = spltty();
2005     mode = scp->mode;
2006     status = scp->status;
2007     scp->mode = scp->splash_save_mode;
2008     scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2009     scp->status |= scp->splash_save_status;
2010     scp->sc->flags &= ~SC_SCRN_BLANKED;
2011     if (!changemode) {
2012         if (!ISGRAPHSC(scp))
2013             sc_draw_cursor_image(scp);
2014         --scrn_blanked;
2015         splx(s);
2016         return 0;
2017     }
2018     if (set_mode(scp) == 0) {
2019 #ifndef SC_NO_PALETTE_LOADING
2020         load_palette(scp->sc->adp, scp->sc->palette);
2021 #endif
2022         --scrn_blanked;
2023         splx(s);
2024         return 0;
2025     } else {
2026         scp->mode = mode;
2027         scp->status = status;
2028         splx(s);
2029         return 1;
2030     }
2031 }
2032
2033 static void
2034 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2035 {
2036     (*saver)(sc, FALSE);
2037     run_scrn_saver = FALSE;
2038     /* the screen saver may have chosen not to stop after all... */
2039     if (sc->flags & SC_SCRN_BLANKED)
2040         return;
2041
2042     mark_all(sc->cur_scp);
2043     if (sc->delayed_next_scr)
2044         sc_switch_scr(sc, sc->delayed_next_scr - 1);
2045     wakeup((caddr_t)&scrn_blanked);
2046 }
2047
2048 static int
2049 wait_scrn_saver_stop(sc_softc_t *sc)
2050 {
2051     int error = 0;
2052
2053     while (scrn_blanked > 0) {
2054         run_scrn_saver = FALSE;
2055         if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2056             error = 0;
2057             break;
2058         }
2059         error = tsleep((caddr_t)&scrn_blanked, PCATCH, "scrsav", 0);
2060         if ((error != 0) && (error != ERESTART))
2061             break;
2062     }
2063     run_scrn_saver = FALSE;
2064     return error;
2065 }
2066 #endif /* NSPLASH */
2067
2068 void
2069 sc_touch_scrn_saver(void)
2070 {
2071     scsplash_stick(FALSE);
2072     run_scrn_saver = FALSE;
2073 }
2074
2075 int
2076 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2077 {
2078     scr_stat *cur_scp;
2079     struct tty *tp;
2080     int s;
2081
2082     DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2083
2084     /* prevent switch if previously requested */
2085     if (sc->flags & SC_SCRN_VTYLOCK) {
2086             sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2087                 sc->cur_scp->bell_duration);
2088             return EPERM;
2089     }
2090
2091     /* delay switch if the screen is blanked or being updated */
2092     if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2093         || sc->blink_in_progress || sc->videoio_in_progress) {
2094         sc->delayed_next_scr = next_scr + 1;
2095         sc_touch_scrn_saver();
2096         DPRINTF(5, ("switch delayed\n"));
2097         return 0;
2098     }
2099
2100     s = spltty();
2101     cur_scp = sc->cur_scp;
2102
2103     /* we are in the middle of the vty switching process... */
2104     if (sc->switch_in_progress
2105         && (cur_scp->smode.mode == VT_PROCESS)
2106         && cur_scp->proc) {
2107         if (cur_scp->proc != pfind(cur_scp->pid)) {
2108             /* 
2109              * The controlling process has died!!.  Do some clean up.
2110              * NOTE:`cur_scp->proc' and `cur_scp->smode.mode' 
2111              * are not reset here yet; they will be cleared later.
2112              */
2113             DPRINTF(5, ("cur_scp controlling process %d died, ",
2114                cur_scp->pid));
2115             if (cur_scp->status & SWITCH_WAIT_REL) {
2116                 /*
2117                  * Force the previous switch to finish, but return now 
2118                  * with error.
2119                  */
2120                 DPRINTF(5, ("reset WAIT_REL, "));
2121                 finish_vt_rel(cur_scp, TRUE, &s);
2122                 splx(s);
2123                 DPRINTF(5, ("finishing previous switch\n"));
2124                 return EINVAL;
2125             } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2126                 /* let's assume screen switch has been completed. */
2127                 DPRINTF(5, ("reset WAIT_ACQ, "));
2128                 finish_vt_acq(cur_scp);
2129             } else {
2130                 /* 
2131                  * We are in between screen release and acquisition, and
2132                  * reached here via scgetc() or scrn_timer() which has 
2133                  * interrupted exchange_scr(). Don't do anything stupid.
2134                  */
2135                 DPRINTF(5, ("waiting nothing, "));
2136             }
2137         } else {
2138             /*
2139              * The controlling process is alive, but not responding... 
2140              * It is either buggy or it may be just taking time.
2141              * The following code is a gross kludge to cope with this
2142              * problem for which there is no clean solution. XXX
2143              */
2144             if (cur_scp->status & SWITCH_WAIT_REL) {
2145                 switch (sc->switch_in_progress++) {
2146                 case 1:
2147                     break;
2148                 case 2:
2149                     DPRINTF(5, ("sending relsig again, "));
2150                     signal_vt_rel(cur_scp);
2151                     break;
2152                 case 3:
2153                     break;
2154                 case 4:
2155                 default:
2156                     /*
2157                      * Act as if the controlling program returned
2158                      * VT_FALSE.
2159                      */
2160                     DPRINTF(5, ("force reset WAIT_REL, "));
2161                     finish_vt_rel(cur_scp, FALSE, &s);
2162                     splx(s);
2163                     DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2164                     return EINVAL;
2165                 }
2166             } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2167                 switch (sc->switch_in_progress++) {
2168                 case 1:
2169                     break;
2170                 case 2:
2171                     DPRINTF(5, ("sending acqsig again, "));
2172                     signal_vt_acq(cur_scp);
2173                     break;
2174                 case 3:
2175                     break;
2176                 case 4:
2177                 default:
2178                      /* clear the flag and finish the previous switch */
2179                     DPRINTF(5, ("force reset WAIT_ACQ, "));
2180                     finish_vt_acq(cur_scp);
2181                     break;
2182                 }
2183             }
2184         }
2185     }
2186
2187     /*
2188      * Return error if an invalid argument is given, or vty switch
2189      * is still in progress.
2190      */
2191     if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2192         || sc->switch_in_progress) {
2193         splx(s);
2194         sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2195         DPRINTF(5, ("error 1\n"));
2196         return EINVAL;
2197     }
2198
2199     /*
2200      * Don't allow switching away from the graphics mode vty
2201      * if the switch mode is VT_AUTO, unless the next vty is the same 
2202      * as the current or the current vty has been closed (but showing).
2203      */
2204     tp = VIRTUAL_TTY(sc, cur_scp->index);
2205     if ((cur_scp->index != next_scr)
2206         && ISTTYOPEN(tp)
2207         && (cur_scp->smode.mode == VT_AUTO)
2208         && ISGRAPHSC(cur_scp)) {
2209         splx(s);
2210         sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2211         DPRINTF(5, ("error, graphics mode\n"));
2212         return EINVAL;
2213     }
2214
2215     /*
2216      * Is the wanted vty open? Don't allow switching to a closed vty.
2217      * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2218      * Note that we always allow the user to switch to the kernel 
2219      * console even if it is closed.
2220      */
2221     if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2222         tp = VIRTUAL_TTY(sc, next_scr);
2223         if (!ISTTYOPEN(tp)) {
2224             splx(s);
2225             sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2226             DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2227             return EINVAL;
2228         }
2229         if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2230             splx(s);
2231             DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2232             return EINVAL;
2233         }
2234     }
2235
2236     /* this is the start of vty switching process... */
2237     ++sc->switch_in_progress;
2238     sc->delayed_next_scr = 0;
2239     sc->old_scp = cur_scp;
2240     sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2241     if (sc->new_scp == sc->old_scp) {
2242         sc->switch_in_progress = 0;
2243         wakeup((caddr_t)&sc->new_scp->smode);
2244         splx(s);
2245         DPRINTF(5, ("switch done (new == old)\n"));
2246         return 0;
2247     }
2248
2249     /* has controlling process died? */
2250     vt_proc_alive(sc->old_scp);
2251     vt_proc_alive(sc->new_scp);
2252
2253     /* wait for the controlling process to release the screen, if necessary */
2254     if (signal_vt_rel(sc->old_scp)) {
2255         splx(s);
2256         return 0;
2257     }
2258
2259     /* go set up the new vty screen */
2260     splx(s);
2261     exchange_scr(sc);
2262     s = spltty();
2263
2264     /* wake up processes waiting for this vty */
2265     wakeup((caddr_t)&sc->cur_scp->smode);
2266
2267     /* wait for the controlling process to acknowledge, if necessary */
2268     if (signal_vt_acq(sc->cur_scp)) {
2269         splx(s);
2270         return 0;
2271     }
2272
2273     sc->switch_in_progress = 0;
2274     if (sc->unit == sc_console_unit)
2275         cons_unavail = FALSE;
2276     splx(s);
2277     DPRINTF(5, ("switch done\n"));
2278
2279     return 0;
2280 }
2281
2282 static int
2283 do_switch_scr(sc_softc_t *sc, int s)
2284 {
2285     vt_proc_alive(sc->new_scp);
2286
2287     splx(s);
2288     exchange_scr(sc);
2289     s = spltty();
2290     /* sc->cur_scp == sc->new_scp */
2291     wakeup((caddr_t)&sc->cur_scp->smode);
2292
2293     /* wait for the controlling process to acknowledge, if necessary */
2294     if (!signal_vt_acq(sc->cur_scp)) {
2295         sc->switch_in_progress = 0;
2296         if (sc->unit == sc_console_unit)
2297             cons_unavail = FALSE;
2298     }
2299
2300     return s;
2301 }
2302
2303 static int
2304 vt_proc_alive(scr_stat *scp)
2305 {
2306     if (scp->proc) {
2307         if (scp->proc == pfind(scp->pid))
2308             return TRUE;
2309         scp->proc = NULL;
2310         scp->smode.mode = VT_AUTO;
2311         DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2312     }
2313     return FALSE;
2314 }
2315
2316 static int
2317 signal_vt_rel(scr_stat *scp)
2318 {
2319     if (scp->smode.mode != VT_PROCESS)
2320         return FALSE;
2321     scp->status |= SWITCH_WAIT_REL;
2322     psignal(scp->proc, scp->smode.relsig);
2323     DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2324     return TRUE;
2325 }
2326
2327 static int
2328 signal_vt_acq(scr_stat *scp)
2329 {
2330     if (scp->smode.mode != VT_PROCESS)
2331         return FALSE;
2332     if (scp->sc->unit == sc_console_unit)
2333         cons_unavail = TRUE;
2334     scp->status |= SWITCH_WAIT_ACQ;
2335     psignal(scp->proc, scp->smode.acqsig);
2336     DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2337     return TRUE;
2338 }
2339
2340 static int
2341 finish_vt_rel(scr_stat *scp, int release, int *s)
2342 {
2343     if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2344         scp->status &= ~SWITCH_WAIT_REL;
2345         if (release)
2346             *s = do_switch_scr(scp->sc, *s);
2347         else
2348             scp->sc->switch_in_progress = 0;
2349         return 0;
2350     }
2351     return EINVAL;
2352 }
2353
2354 static int
2355 finish_vt_acq(scr_stat *scp)
2356 {
2357     if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2358         scp->status &= ~SWITCH_WAIT_ACQ;
2359         scp->sc->switch_in_progress = 0;
2360         return 0;
2361     }
2362     return EINVAL;
2363 }
2364
2365 static void
2366 exchange_scr(sc_softc_t *sc)
2367 {
2368     scr_stat *scp;
2369
2370     /* save the current state of video and keyboard */
2371     sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2372     if (!ISGRAPHSC(sc->old_scp))
2373         sc_remove_cursor_image(sc->old_scp);
2374     if (sc->old_scp->kbd_mode == K_XLATE)
2375         save_kbd_state(sc->old_scp);
2376
2377     /* set up the video for the new screen */
2378     scp = sc->cur_scp = sc->new_scp;
2379     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2380         set_mode(scp);
2381     else
2382         sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2383                     (void *)sc->adp->va_window, FALSE);
2384     scp->status |= MOUSE_HIDDEN;
2385     sc_move_cursor(scp, scp->xpos, scp->ypos);
2386     if (!ISGRAPHSC(scp))
2387         sc_set_cursor_image(scp);
2388 #ifndef SC_NO_PALETTE_LOADING
2389     if (ISGRAPHSC(sc->old_scp))
2390         load_palette(sc->adp, sc->palette);
2391 #endif
2392     sc_set_border(scp, scp->border);
2393
2394     /* set up the keyboard for the new screen */
2395     if (sc->old_scp->kbd_mode != scp->kbd_mode)
2396         kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2397     update_kbd_state(scp, scp->status, LOCK_MASK);
2398
2399     mark_all(scp);
2400 }
2401
2402 void
2403 sc_puts(scr_stat *scp, u_char *buf, int len)
2404 {
2405 #if NSPLASH > 0
2406     /* make screensaver happy */
2407     if (!sticky_splash && scp == scp->sc->cur_scp)
2408         run_scrn_saver = FALSE;
2409 #endif
2410
2411     if (scp->tsw)
2412         (*scp->tsw->te_puts)(scp, buf, len);
2413
2414     if (scp->sc->delayed_next_scr)
2415         sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2416 }
2417
2418 void
2419 sc_draw_cursor_image(scr_stat *scp)
2420 {
2421     /* assert(scp == scp->sc->cur_scp); */
2422     ++scp->sc->videoio_in_progress;
2423     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2424                               scp->sc->flags & SC_BLINK_CURSOR, TRUE,
2425                               sc_inside_cutmark(scp, scp->cursor_pos));
2426     scp->cursor_oldpos = scp->cursor_pos;
2427     --scp->sc->videoio_in_progress;
2428 }
2429
2430 void
2431 sc_remove_cursor_image(scr_stat *scp)
2432 {
2433     /* assert(scp == scp->sc->cur_scp); */
2434     ++scp->sc->videoio_in_progress;
2435     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2436                               scp->sc->flags & SC_BLINK_CURSOR, FALSE,
2437                               sc_inside_cutmark(scp, scp->cursor_oldpos));
2438     --scp->sc->videoio_in_progress;
2439 }
2440
2441 static void
2442 update_cursor_image(scr_stat *scp)
2443 {
2444     int blink;
2445
2446     if (scp->sc->flags & SC_CHAR_CURSOR) {
2447         scp->cursor_base = imax(0, scp->sc->cursor_base);
2448         scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2449     } else {
2450         scp->cursor_base = 0;
2451         scp->cursor_height = scp->font_size;
2452     }
2453     blink = scp->sc->flags & SC_BLINK_CURSOR;
2454
2455     /* assert(scp == scp->sc->cur_scp); */
2456     ++scp->sc->videoio_in_progress;
2457     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE, 
2458                               sc_inside_cutmark(scp, scp->cursor_pos));
2459     (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
2460     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE, 
2461                               sc_inside_cutmark(scp, scp->cursor_pos));
2462     --scp->sc->videoio_in_progress;
2463 }
2464
2465 void
2466 sc_set_cursor_image(scr_stat *scp)
2467 {
2468     if (scp->sc->flags & SC_CHAR_CURSOR) {
2469         scp->cursor_base = imax(0, scp->sc->cursor_base);
2470         scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2471     } else {
2472         scp->cursor_base = 0;
2473         scp->cursor_height = scp->font_size;
2474     }
2475
2476     /* assert(scp == scp->sc->cur_scp); */
2477     ++scp->sc->videoio_in_progress;
2478     (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
2479                              scp->sc->flags & SC_BLINK_CURSOR);
2480     --scp->sc->videoio_in_progress;
2481 }
2482
2483 static void
2484 scinit(int unit, int flags)
2485 {
2486     /*
2487      * When syscons is being initialized as the kernel console, malloc()
2488      * is not yet functional, because various kernel structures has not been
2489      * fully initialized yet.  Therefore, we need to declare the following
2490      * static buffers for the console.  This is less than ideal, 
2491      * but is necessry evil for the time being.  XXX
2492      */
2493     static scr_stat main_console;
2494     static dev_t main_devs[MAXCONS];
2495     static struct tty main_tty;
2496     static u_short sc_buffer[ROW*COL];  /* XXX */
2497 #ifndef SC_NO_FONT_LOADING
2498     static u_char font_8[256*8];
2499     static u_char font_14[256*14];
2500     static u_char font_16[256*16];
2501 #endif
2502
2503     sc_softc_t *sc;
2504     scr_stat *scp;
2505     video_adapter_t *adp;
2506     int col;
2507     int row;
2508     int i;
2509
2510     /* one time initialization */
2511     if (init_done == COLD)
2512         sc_get_bios_values(&bios_value);
2513     init_done = WARM;
2514
2515     /*
2516      * Allocate resources.  Even if we are being called for the second
2517      * time, we must allocate them again, because they might have 
2518      * disappeared...
2519      */
2520     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2521     adp = NULL;
2522     if (sc->adapter >= 0) {
2523         vid_release(sc->adp, (void *)&sc->adapter);
2524         adp = sc->adp;
2525         sc->adp = NULL;
2526     }
2527     if (sc->keyboard >= 0) {
2528         DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2529         i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2530         DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2531         if (sc->kbd != NULL) {
2532             DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2533                 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2534         }
2535         sc->kbd = NULL;
2536     }
2537     sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2538     sc->adp = vid_get_adapter(sc->adapter);
2539     /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2540     sc->keyboard = kbd_allocate("*", unit, (void *)&sc->keyboard,
2541                                 sckbdevent, sc);
2542     DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2543     sc->kbd = kbd_get_keyboard(sc->keyboard);
2544     if (sc->kbd != NULL) {
2545         DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2546                 unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2547     }
2548
2549     if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2550
2551         sc->initial_mode = sc->adp->va_initial_mode;
2552
2553 #ifndef SC_NO_FONT_LOADING
2554         if (flags & SC_KERNEL_CONSOLE) {
2555             sc->font_8 = font_8;
2556             sc->font_14 = font_14;
2557             sc->font_16 = font_16;
2558         } else if (sc->font_8 == NULL) {
2559             /* assert(sc_malloc) */
2560             sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2561             sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2562             sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2563         }
2564 #endif
2565
2566         /* extract the hardware cursor location and hide the cursor for now */
2567         (*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2568         (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2569
2570         /* set up the first console */
2571         sc->first_vty = unit*MAXCONS;
2572         sc->vtys = MAXCONS;             /* XXX: should be configurable */
2573         if (flags & SC_KERNEL_CONSOLE) {
2574             sc->dev = main_devs;
2575             sc->dev[0] = make_dev(&sc_cdevsw, unit*MAXCONS, UID_ROOT, 
2576                                 GID_WHEEL, 0600, "ttyv%r", unit*MAXCONS);
2577             sc->dev[0]->si_tty = &main_tty;
2578             ttyregister(&main_tty);
2579             scp = &main_console;
2580             init_scp(sc, sc->first_vty, scp);
2581             sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2582                         (void *)sc_buffer, FALSE);
2583             if (sc_init_emulator(scp, SC_DFLT_TERM))
2584                 sc_init_emulator(scp, "*");
2585             (*scp->tsw->te_default_attr)(scp,
2586                                          kernel_default.std_color,
2587                                          kernel_default.rev_color);
2588         } else {
2589             /* assert(sc_malloc) */
2590             sc->dev = malloc(sizeof(dev_t)*sc->vtys, M_DEVBUF, M_WAITOK);
2591             bzero(sc->dev, sizeof(dev_t)*sc->vtys);
2592             sc->dev[0] = make_dev(&sc_cdevsw, unit*MAXCONS, UID_ROOT, 
2593                                 GID_WHEEL, 0600, "ttyv%r", unit*MAXCONS);
2594             sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
2595             scp = alloc_scp(sc, sc->first_vty);
2596         }
2597         SC_STAT(sc->dev[0]) = scp;
2598         sc->cur_scp = scp;
2599
2600         /* copy screen to temporary buffer */
2601         sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2602                     (void *)scp->sc->adp->va_window, FALSE);
2603         if (ISTEXTSC(scp))
2604             sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2605
2606         /* move cursors to the initial positions */
2607         if (col >= scp->xsize)
2608             col = 0;
2609         if (row >= scp->ysize)
2610             row = scp->ysize - 1;
2611         scp->xpos = col;
2612         scp->ypos = row;
2613         scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2614         if (bios_value.cursor_end < scp->font_size)
2615             sc->cursor_base = scp->font_size - bios_value.cursor_end - 1;
2616         else
2617             sc->cursor_base = 0;
2618         i = bios_value.cursor_end - bios_value.cursor_start + 1;
2619         sc->cursor_height = imin(i, scp->font_size);
2620 #ifndef SC_NO_SYSMOUSE
2621         sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2622 #endif
2623         if (!ISGRAPHSC(scp)) {
2624             sc_set_cursor_image(scp);
2625             sc_draw_cursor_image(scp);
2626         }
2627
2628         /* save font and palette */
2629 #ifndef SC_NO_FONT_LOADING
2630         sc->fonts_loaded = 0;
2631         if (ISFONTAVAIL(sc->adp->va_flags)) {
2632 #ifdef SC_DFLT_FONT
2633             bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2634             bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2635             bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2636             sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2637             if (scp->font_size < 14) {
2638                 sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
2639             } else if (scp->font_size >= 16) {
2640                 sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
2641             } else {
2642                 sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
2643             }
2644 #else /* !SC_DFLT_FONT */
2645             if (scp->font_size < 14) {
2646                 sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
2647                 sc->fonts_loaded = FONT_8;
2648             } else if (scp->font_size >= 16) {
2649                 sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
2650                 sc->fonts_loaded = FONT_16;
2651             } else {
2652                 sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
2653                 sc->fonts_loaded = FONT_14;
2654             }
2655 #endif /* SC_DFLT_FONT */
2656             /* FONT KLUDGE: always use the font page #0. XXX */
2657             sc_show_font(scp, 0);
2658         }
2659 #endif /* !SC_NO_FONT_LOADING */
2660
2661 #ifndef SC_NO_PALETTE_LOADING
2662         save_palette(sc->adp, sc->palette);
2663 #endif
2664
2665 #if NSPLASH > 0
2666         if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
2667             /* we are ready to put up the splash image! */
2668             splash_init(sc->adp, scsplash_callback, sc);
2669             sc->flags |= SC_SPLASH_SCRN;
2670         }
2671 #endif /* NSPLASH */
2672     }
2673
2674     /* the rest is not necessary, if we have done it once */
2675     if (sc->flags & SC_INIT_DONE)
2676         return;
2677
2678     /* initialize mapscrn arrays to a one to one map */
2679     for (i = 0; i < sizeof(sc->scr_map); i++)
2680         sc->scr_map[i] = sc->scr_rmap[i] = i;
2681
2682     sc->flags |= SC_INIT_DONE;
2683 }
2684
2685 #if __i386__
2686 static void
2687 scterm(int unit, int flags)
2688 {
2689     sc_softc_t *sc;
2690     scr_stat *scp;
2691
2692     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2693     if (sc == NULL)
2694         return;                 /* shouldn't happen */
2695
2696 #if NSPLASH > 0
2697     /* this console is no longer available for the splash screen */
2698     if (sc->flags & SC_SPLASH_SCRN) {
2699         splash_term(sc->adp);
2700         sc->flags &= ~SC_SPLASH_SCRN;
2701     }
2702 #endif /* NSPLASH */
2703
2704 #if 0 /* XXX */
2705     /* move the hardware cursor to the upper-left corner */
2706     (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2707 #endif
2708
2709     /* release the keyboard and the video card */
2710     if (sc->keyboard >= 0)
2711         kbd_release(sc->kbd, &sc->keyboard);
2712     if (sc->adapter >= 0)
2713         vid_release(sc->adp, &sc->adapter);
2714
2715     /* stop the terminal emulator, if any */
2716     scp = SC_STAT(sc->dev[0]);
2717     if (scp->tsw)
2718         (*scp->tsw->te_term)(scp, &scp->ts);
2719     if (scp->ts != NULL)
2720         free(scp->ts, M_DEVBUF);
2721
2722     /* clear the structure */
2723     if (!(flags & SC_KERNEL_CONSOLE)) {
2724         /* XXX: We need delete_dev() for this */
2725         free(sc->dev, M_DEVBUF);
2726 #if 0
2727         /* XXX: We need a ttyunregister for this */
2728         free(sc->tty, M_DEVBUF);
2729 #endif
2730 #ifndef SC_NO_FONT_LOADING
2731         free(sc->font_8, M_DEVBUF);
2732         free(sc->font_14, M_DEVBUF);
2733         free(sc->font_16, M_DEVBUF);
2734 #endif
2735         /* XXX vtb, history */
2736     }
2737     bzero(sc, sizeof(*sc));
2738     sc->keyboard = -1;
2739     sc->adapter = -1;
2740 }
2741 #endif
2742
2743 static void
2744 scshutdown(void *arg, int howto)
2745 {
2746     /* assert(sc_console != NULL) */
2747
2748     sc_touch_scrn_saver();
2749     if (!cold && sc_console
2750         && sc_console->sc->cur_scp->smode.mode == VT_AUTO 
2751         && sc_console->smode.mode == VT_AUTO)
2752         sc_switch_scr(sc_console->sc, sc_console->index);
2753     shutdown_in_progress = TRUE;
2754 }
2755
2756 int
2757 sc_clean_up(scr_stat *scp)
2758 {
2759 #if NSPLASH > 0
2760     int error;
2761 #endif /* NSPLASH */
2762
2763     if (scp->sc->flags & SC_SCRN_BLANKED) {
2764         sc_touch_scrn_saver();
2765 #if NSPLASH > 0
2766         if ((error = wait_scrn_saver_stop(scp->sc)))
2767             return error;
2768 #endif /* NSPLASH */
2769     }
2770     scp->status |= MOUSE_HIDDEN;
2771     sc_remove_mouse_image(scp);
2772     sc_remove_cutmarking(scp);
2773     return 0;
2774 }
2775
2776 void
2777 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2778 {
2779     sc_vtb_t new;
2780     sc_vtb_t old;
2781
2782     old = scp->vtb;
2783     sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2784     if (!discard && (old.vtb_flags & VTB_VALID)) {
2785         /* retain the current cursor position and buffer contants */
2786         scp->cursor_oldpos = scp->cursor_pos;
2787         /* 
2788          * This works only if the old buffer has the same size as or larger 
2789          * than the new one. XXX
2790          */
2791         sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2792         scp->vtb = new;
2793     } else {
2794         scp->vtb = new;
2795         sc_vtb_destroy(&old);
2796     }
2797
2798 #ifndef SC_NO_SYSMOUSE
2799     /* move the mouse cursor at the center of the screen */
2800     sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2801 #endif
2802 }
2803
2804 static scr_stat
2805 *alloc_scp(sc_softc_t *sc, int vty)
2806 {
2807     scr_stat *scp;
2808
2809     /* assert(sc_malloc) */
2810
2811     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2812     init_scp(sc, vty, scp);
2813
2814     sc_alloc_scr_buffer(scp, TRUE, TRUE);
2815     if (sc_init_emulator(scp, SC_DFLT_TERM))
2816         sc_init_emulator(scp, "*");
2817
2818 #ifndef SC_NO_CUTPASTE
2819     sc_alloc_cut_buffer(scp, TRUE);
2820 #endif
2821
2822 #ifndef SC_NO_HISTORY
2823     sc_alloc_history_buffer(scp, 0, 0, TRUE);
2824 #endif
2825
2826     return scp;
2827 }
2828
2829 static void
2830 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
2831 {
2832     video_info_t info;
2833
2834     bzero(scp, sizeof(*scp));
2835
2836     scp->index = vty;
2837     scp->sc = sc;
2838     scp->status = 0;
2839     scp->mode = sc->initial_mode;
2840     callout_init(&scp->blink_screen_ch);
2841     (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
2842     if (info.vi_flags & V_INFO_GRAPHICS) {
2843         scp->status |= GRAPHICS_MODE;
2844         scp->xpixel = info.vi_width;
2845         scp->ypixel = info.vi_height;
2846         scp->xsize = info.vi_width/8;
2847         scp->ysize = info.vi_height/info.vi_cheight;
2848         scp->font_size = 0;
2849         scp->font = NULL;
2850     } else {
2851         scp->xsize = info.vi_width;
2852         scp->ysize = info.vi_height;
2853         scp->xpixel = scp->xsize*8;
2854         scp->ypixel = scp->ysize*info.vi_cheight;
2855         if (info.vi_cheight < 14) {
2856             scp->font_size = 8;
2857 #ifndef SC_NO_FONT_LOADING
2858             scp->font = sc->font_8;
2859 #else
2860             scp->font = NULL;
2861 #endif
2862         } else if (info.vi_cheight >= 16) {
2863             scp->font_size = 16;
2864 #ifndef SC_NO_FONT_LOADING
2865             scp->font = sc->font_16;
2866 #else
2867             scp->font = NULL;
2868 #endif
2869         } else {
2870             scp->font_size = 14;
2871 #ifndef SC_NO_FONT_LOADING
2872             scp->font = sc->font_14;
2873 #else
2874             scp->font = NULL;
2875 #endif
2876         }
2877     }
2878     sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
2879     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
2880     scp->xoff = scp->yoff = 0;
2881     scp->xpos = scp->ypos = 0;
2882     scp->start = scp->xsize * scp->ysize - 1;
2883     scp->end = 0;
2884     scp->tsw = NULL;
2885     scp->ts = NULL;
2886     scp->rndr = NULL;
2887     scp->border = BG_BLACK;
2888     scp->cursor_base = sc->cursor_base;
2889     scp->cursor_height = imin(sc->cursor_height, scp->font_size);
2890     scp->mouse_cut_start = scp->xsize*scp->ysize;
2891     scp->mouse_cut_end = -1;
2892     scp->mouse_signal = 0;
2893     scp->mouse_pid = 0;
2894     scp->mouse_proc = NULL;
2895     scp->kbd_mode = K_XLATE;
2896     scp->bell_pitch = bios_value.bell_pitch;
2897     scp->bell_duration = BELL_DURATION;
2898     scp->status |= (bios_value.shift_state & NLKED);
2899     scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
2900     scp->pid = 0;
2901     scp->proc = NULL;
2902     scp->smode.mode = VT_AUTO;
2903     scp->history = NULL;
2904     scp->history_pos = 0;
2905     scp->history_size = 0;
2906 }
2907
2908 int
2909 sc_init_emulator(scr_stat *scp, char *name)
2910 {
2911     sc_term_sw_t *sw;
2912     sc_rndr_sw_t *rndr;
2913     void *p;
2914     int error;
2915
2916     if (name == NULL)   /* if no name is given, use the current emulator */
2917         sw = scp->tsw;
2918     else                /* ...otherwise find the named emulator */
2919         sw = sc_term_match(name);
2920     if (sw == NULL)
2921         return EINVAL;
2922
2923     rndr = NULL;
2924     if (strcmp(sw->te_renderer, "*") != 0) {
2925         rndr = sc_render_match(scp, sw->te_renderer,
2926                                scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2927     }
2928     if (rndr == NULL) {
2929         rndr = sc_render_match(scp, scp->sc->adp->va_name,
2930                                scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2931         if (rndr == NULL)
2932             return ENODEV;
2933     }
2934
2935     if (sw == scp->tsw) {
2936         error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
2937         scp->rndr = rndr;
2938         scp->rndr->init(scp);
2939         sc_clear_screen(scp);
2940         /* assert(error == 0); */
2941         return error;
2942     }
2943
2944     if (sc_malloc && (sw->te_size > 0))
2945         p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
2946     else
2947         p = NULL;
2948     error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
2949     if (error)
2950         return error;
2951
2952     if (scp->tsw)
2953         (*scp->tsw->te_term)(scp, &scp->ts);
2954     if (scp->ts != NULL)
2955         free(scp->ts, M_DEVBUF);
2956     scp->tsw = sw;
2957     scp->ts = p;
2958     scp->rndr = rndr;
2959     scp->rndr->init(scp);
2960
2961     /* XXX */
2962     (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
2963     sc_clear_screen(scp);
2964
2965     return 0;
2966 }
2967
2968 /*
2969  * scgetc(flags) - get character from keyboard.
2970  * If flags & SCGETC_CN, then avoid harmful side effects.
2971  * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
2972  * return NOKEY if there is nothing there.
2973  */
2974 static u_int
2975 scgetc(sc_softc_t *sc, u_int flags)
2976 {
2977     scr_stat *scp;
2978 #ifndef SC_NO_HISTORY
2979     struct tty *tp;
2980 #endif
2981     u_int c;
2982     int this_scr;
2983     int f;
2984     int i;
2985
2986     if (sc->kbd == NULL)
2987         return NOKEY;
2988
2989 next_code:
2990 #if 1
2991     /* I don't like this, but... XXX */
2992     if (flags & SCGETC_CN)
2993         sccnupdate(sc->cur_scp);
2994 #endif
2995     scp = sc->cur_scp;
2996     /* first see if there is something in the keyboard port */
2997     for (;;) {
2998         c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
2999         if (c == ERRKEY) {
3000             if (!(flags & SCGETC_CN))
3001                 sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3002         } else if (c == NOKEY)
3003             return c;
3004         else
3005             break;
3006     }
3007
3008     /* make screensaver happy */
3009     if (!(c & RELKEY))
3010         sc_touch_scrn_saver();
3011
3012 #ifdef __i386__
3013     if (!(flags & SCGETC_CN))
3014         /* do the /dev/random device a favour */
3015         add_keyboard_randomness(c);
3016 #endif
3017
3018     if (scp->kbd_mode != K_XLATE)
3019         return KEYCHAR(c);
3020
3021     /* if scroll-lock pressed allow history browsing */
3022     if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3023
3024         scp->status &= ~CURSOR_ENABLED;
3025         sc_remove_cursor_image(scp);
3026
3027 #ifndef SC_NO_HISTORY
3028         if (!(scp->status & BUFFER_SAVED)) {
3029             scp->status |= BUFFER_SAVED;
3030             sc_hist_save(scp);
3031         }
3032         switch (c) {
3033         /* FIXME: key codes */
3034         case SPCLKEY | FKEY | F(49):  /* home key */
3035             sc_remove_cutmarking(scp);
3036             sc_hist_home(scp);
3037             goto next_code;
3038
3039         case SPCLKEY | FKEY | F(57):  /* end key */
3040             sc_remove_cutmarking(scp);
3041             sc_hist_end(scp);
3042             goto next_code;
3043
3044         case SPCLKEY | FKEY | F(50):  /* up arrow key */
3045             sc_remove_cutmarking(scp);
3046             if (sc_hist_up_line(scp))
3047                 if (!(flags & SCGETC_CN))
3048                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3049             goto next_code;
3050
3051         case SPCLKEY | FKEY | F(58):  /* down arrow key */
3052             sc_remove_cutmarking(scp);
3053             if (sc_hist_down_line(scp))
3054                 if (!(flags & SCGETC_CN))
3055                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3056             goto next_code;
3057
3058         case SPCLKEY | FKEY | F(51):  /* page up key */
3059             sc_remove_cutmarking(scp);
3060             for (i=0; i<scp->ysize; i++)
3061             if (sc_hist_up_line(scp)) {
3062                 if (!(flags & SCGETC_CN))
3063                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3064                 break;
3065             }
3066             goto next_code;
3067
3068         case SPCLKEY | FKEY | F(59):  /* page down key */
3069             sc_remove_cutmarking(scp);
3070             for (i=0; i<scp->ysize; i++)
3071             if (sc_hist_down_line(scp)) {
3072                 if (!(flags & SCGETC_CN))
3073                     sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3074                 break;
3075             }
3076             goto next_code;
3077         }
3078 #endif /* SC_NO_HISTORY */
3079     }
3080
3081     /* 
3082      * Process and consume special keys here.  Return a plain char code
3083      * or a char code with the META flag or a function key code.
3084      */
3085     if (c & RELKEY) {
3086         /* key released */
3087         /* goto next_code */
3088     } else {
3089         /* key pressed */
3090         if (c & SPCLKEY) {
3091             c &= ~SPCLKEY;
3092             switch (KEYCHAR(c)) {
3093             /* LOCKING KEYS */
3094             case NLK: case CLK: case ALK:
3095                 break;
3096             case SLK:
3097                 kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3098                 if (f & SLKED) {
3099                     scp->status |= SLKED;
3100                 } else {
3101                     if (scp->status & SLKED) {
3102                         scp->status &= ~SLKED;
3103 #ifndef SC_NO_HISTORY
3104                         if (scp->status & BUFFER_SAVED) {
3105                             if (!sc_hist_restore(scp))
3106                                 sc_remove_cutmarking(scp);
3107                             scp->status &= ~BUFFER_SAVED;
3108                             scp->status |= CURSOR_ENABLED;
3109                             sc_draw_cursor_image(scp);
3110                         }
3111                         tp = VIRTUAL_TTY(sc, scp->index);
3112                         if (ISTTYOPEN(tp))
3113                             scstart(tp);
3114 #endif
3115                     }
3116                 }
3117                 break;
3118
3119             /* NON-LOCKING KEYS */
3120             case NOP:
3121             case LSH:  case RSH:  case LCTR: case RCTR:
3122             case LALT: case RALT: case ASH:  case META:
3123                 break;
3124
3125             case BTAB:
3126                 if (!(sc->flags & SC_SCRN_BLANKED))
3127                     return c;
3128                 break;
3129
3130             case SPSC:
3131 #if NSPLASH > 0
3132                 /* force activatation/deactivation of the screen saver */
3133                 if (!(sc->flags & SC_SCRN_BLANKED)) {
3134                     run_scrn_saver = TRUE;
3135                     sc->scrn_time_stamp -= scrn_blank_time;
3136                 }
3137                 if (cold) {
3138                     /*
3139                      * While devices are being probed, the screen saver need
3140                      * to be invoked explictly. XXX
3141                      */
3142                     if (sc->flags & SC_SCRN_BLANKED) {
3143                         scsplash_stick(FALSE);
3144                         stop_scrn_saver(sc, current_saver);
3145                     } else {
3146                         if (!ISGRAPHSC(scp)) {
3147                             scsplash_stick(TRUE);
3148                             (*current_saver)(sc, TRUE);
3149                         }
3150                     }
3151                 }
3152 #endif /* NSPLASH */
3153                 break;
3154
3155             case RBT:
3156 #ifndef SC_DISABLE_REBOOT
3157                 shutdown_nice(0);
3158 #endif
3159                 break;
3160
3161             case HALT:
3162 #ifndef SC_DISABLE_REBOOT
3163                 shutdown_nice(RB_HALT);
3164 #endif
3165                 break;
3166
3167             case PDWN:
3168 #ifndef SC_DISABLE_REBOOT
3169                 shutdown_nice(RB_HALT|RB_POWEROFF);
3170 #endif
3171                 break;
3172
3173 #if NAPM > 0
3174             case SUSP:
3175                 apm_suspend(PMST_SUSPEND);
3176                 break;
3177             case STBY:
3178                 apm_suspend(PMST_STANDBY);
3179                 break;
3180 #else
3181             case SUSP:
3182             case STBY:
3183                 break;
3184 #endif
3185
3186             case DBG:
3187 #ifndef SC_DISABLE_DDBKEY
3188 #ifdef DDB
3189                 Debugger("manual escape to debugger");
3190 #else
3191                 printf("No debugger in kernel\n");
3192 #endif
3193 #else /* SC_DISABLE_DDBKEY */
3194                 /* do nothing */
3195 #endif /* SC_DISABLE_DDBKEY */
3196                 break;
3197
3198             case PNC:
3199                 if (enable_panic_key)
3200                         panic("Forced by the panic key");
3201                 break;
3202
3203             case NEXT:
3204                 this_scr = scp->index;
3205                 for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3206                         sc->first_vty + i != this_scr; 
3207                         i = (i + 1)%sc->vtys) {
3208                     struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3209                     if (ISTTYOPEN(tp)) {
3210                         sc_switch_scr(scp->sc, sc->first_vty + i);
3211                         break;
3212                     }
3213                 }
3214                 break;
3215
3216             case PREV:
3217                 this_scr = scp->index;
3218                 for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3219                         sc->first_vty + i != this_scr;
3220                         i = (i + sc->vtys - 1)%sc->vtys) {
3221                     struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3222                     if (ISTTYOPEN(tp)) {
3223                         sc_switch_scr(scp->sc, sc->first_vty + i);
3224                         break;
3225                     }
3226                 }
3227                 break;
3228
3229             default:
3230                 if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3231                     sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3232                     break;
3233                 }
3234                 /* assert(c & FKEY) */
3235                 if (!(sc->flags & SC_SCRN_BLANKED))
3236                     return c;
3237                 break;
3238             }
3239             /* goto next_code */
3240         } else {
3241             /* regular keys (maybe MKEY is set) */
3242             if (!(sc->flags & SC_SCRN_BLANKED))
3243                 return c;
3244         }
3245     }
3246
3247     goto next_code;
3248 }
3249
3250 int
3251 scmmap(dev_t dev, vm_offset_t offset, int nprot)
3252 {
3253     scr_stat *scp;
3254
3255     scp = SC_STAT(dev);
3256     if (scp != scp->sc->cur_scp)
3257         return -1;
3258     return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, nprot);
3259 }
3260
3261 static int
3262 save_kbd_state(scr_stat *scp)
3263 {
3264     int state;
3265     int error;
3266
3267     error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3268     if (error == ENOIOCTL)
3269         error = ENODEV;
3270     if (error == 0) {
3271         scp->status &= ~LOCK_MASK;
3272         scp->status |= state;
3273     }
3274     return error;
3275 }
3276
3277 static int
3278 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3279 {
3280     int state;
3281     int error;
3282
3283     if (mask != LOCK_MASK) {
3284         error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3285         if (error == ENOIOCTL)
3286             error = ENODEV;
3287         if (error)
3288             return error;
3289         state &= ~mask;
3290         state |= new_bits & mask;
3291     } else {
3292         state = new_bits & LOCK_MASK;
3293     }
3294     error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3295     if (error == ENOIOCTL)
3296         error = ENODEV;
3297     return error;
3298 }
3299
3300 static int
3301 update_kbd_leds(scr_stat *scp, int which)
3302 {
3303     int error;
3304
3305     which &= LOCK_MASK;
3306     error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3307     if (error == ENOIOCTL)
3308         error = ENODEV;
3309     return error;
3310 }
3311
3312 int
3313 set_mode(scr_stat *scp)
3314 {
3315     video_info_t info;
3316
3317     /* reject unsupported mode */
3318     if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3319         return 1;
3320
3321     /* if this vty is not currently showing, do nothing */
3322     if (scp != scp->sc->cur_scp)
3323         return 0;
3324
3325     /* setup video hardware for the given mode */
3326     (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3327     scp->rndr->init(scp);
3328     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3329                 (void *)scp->sc->adp->va_window, FALSE);
3330
3331 #ifndef SC_NO_FONT_LOADING
3332     /* load appropriate font */
3333     if (!(scp->status & GRAPHICS_MODE)) {
3334         if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3335             if (scp->font_size < 14) {
3336                 if (scp->sc->fonts_loaded & FONT_8)
3337                     sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3338             } else if (scp->font_size >= 16) {
3339                 if (scp->sc->fonts_loaded & FONT_16)
3340                     sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3341             } else {
3342                 if (scp->sc->fonts_loaded & FONT_14)
3343                     sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3344             }
3345             /*
3346              * FONT KLUDGE:
3347              * This is an interim kludge to display correct font.
3348              * Always use the font page #0 on the video plane 2.
3349              * Somehow we cannot show the font in other font pages on
3350              * some video cards... XXX
3351              */ 
3352             sc_show_font(scp, 0);
3353         }
3354         mark_all(scp);
3355     }
3356 #endif /* !SC_NO_FONT_LOADING */
3357
3358     sc_set_border(scp, scp->border);
3359     sc_set_cursor_image(scp);
3360
3361     return 0;
3362 }
3363
3364 void
3365 sc_set_border(scr_stat *scp, int color)
3366 {
3367     ++scp->sc->videoio_in_progress;
3368     (*scp->rndr->draw_border)(scp, color);
3369     --scp->sc->videoio_in_progress;
3370 }
3371
3372 #ifndef SC_NO_FONT_LOADING
3373 void
3374 sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3375              int base, int count)
3376 {
3377     sc_softc_t *sc;
3378
3379     sc = scp->sc;
3380     sc->font_loading_in_progress = TRUE;
3381     (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3382     sc->font_loading_in_progress = FALSE;
3383 }
3384
3385 void
3386 sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3387              int base, int count)
3388 {
3389     sc_softc_t *sc;
3390
3391     sc = scp->sc;
3392     sc->font_loading_in_progress = TRUE;
3393     (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3394     sc->font_loading_in_progress = FALSE;
3395 }
3396
3397 void
3398 sc_show_font(scr_stat *scp, int page)
3399 {
3400     (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3401 }
3402 #endif /* !SC_NO_FONT_LOADING */
3403
3404 void
3405 sc_paste(scr_stat *scp, u_char *p, int count) 
3406 {
3407     struct tty *tp;
3408     u_char *rmap;
3409
3410     if (scp->status & MOUSE_VISIBLE) {
3411         tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3412         if (!ISTTYOPEN(tp))
3413             return;
3414         rmap = scp->sc->scr_rmap;
3415         for (; count > 0; --count)
3416             (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3417     }
3418 }
3419
3420 void
3421 sc_bell(scr_stat *scp, int pitch, int duration)
3422 {
3423     if (cold || shutdown_in_progress)
3424         return;
3425
3426     if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3427         return;
3428
3429     if (scp->sc->flags & SC_VISUAL_BELL) {
3430         if (scp->sc->blink_in_progress)
3431             return;
3432         scp->sc->blink_in_progress = 3;
3433         if (scp != scp->sc->cur_scp)
3434             scp->sc->blink_in_progress += 2;
3435         blink_screen(scp->sc->cur_scp);
3436     } else if (duration != 0 && pitch != 0) {
3437         if (scp != scp->sc->cur_scp)
3438             pitch *= 2;
3439         sysbeep(pitch, duration);
3440     }
3441 }
3442
3443 static void
3444 blink_screen(void *arg)
3445 {
3446     scr_stat *scp = arg;
3447     struct tty *tp;
3448
3449     if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3450         scp->sc->blink_in_progress = 0;
3451         mark_all(scp);
3452         tp = VIRTUAL_TTY(scp->sc, scp->index);
3453         if (ISTTYOPEN(tp))
3454             scstart(tp);
3455         if (scp->sc->delayed_next_scr)
3456             sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3457     }
3458     else {
3459         (*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize, 
3460                            scp->sc->blink_in_progress & 1);
3461         scp->sc->blink_in_progress--;
3462         callout_reset(&scp->blink_screen_ch, hz / 10, blink_screen, scp);
3463     }
3464 }