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