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