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