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