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