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