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