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