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