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