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