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