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