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