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