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