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