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