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