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