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