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