xref: /freebsd/sys/dev/syscons/syscons.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 == scp->sc->cur_scp)
1075 	    return 0;
1076 	error = tsleep(&scp->smode, PZERO | PCATCH, "waitvt", 0);
1077 	return error;
1078 
1079     case VT_GETACTIVE:		/* get active vty # */
1080 	*(int *)data = sc->cur_scp->index + 1;
1081 	return 0;
1082 
1083     case VT_GETINDEX:		/* get this vty # */
1084 	*(int *)data = scp->index + 1;
1085 	return 0;
1086 
1087     case VT_LOCKSWITCH:		/* prevent vty switching */
1088 	if ((*(int *)data) & 0x01)
1089 	    sc->flags |= SC_SCRN_VTYLOCK;
1090 	else
1091 	    sc->flags &= ~SC_SCRN_VTYLOCK;
1092 	return 0;
1093 
1094     case KDENABIO:      	/* allow io operations */
1095 	error = priv_check(td, PRIV_IO);
1096 	if (error != 0)
1097 	    return error;
1098 	error = securelevel_gt(td->td_ucred, 0);
1099 	if (error != 0)
1100 		return error;
1101 #ifdef __i386__
1102 	td->td_frame->tf_eflags |= PSL_IOPL;
1103 #elif defined(__amd64__)
1104 	td->td_frame->tf_rflags |= PSL_IOPL;
1105 #endif
1106 	return 0;
1107 
1108     case KDDISABIO:     	/* disallow io operations (default) */
1109 #ifdef __i386__
1110 	td->td_frame->tf_eflags &= ~PSL_IOPL;
1111 #elif defined(__amd64__)
1112 	td->td_frame->tf_rflags &= ~PSL_IOPL;
1113 #endif
1114 	return 0;
1115 
1116 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1117     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1118     case _IO('K', 20):
1119 	ival = IOCPARM_IVAL(data);
1120 	data = (caddr_t)&ival;
1121 	/* FALLTHROUGH */
1122 #endif
1123     case KDSKBSTATE:    	/* set keyboard state (locks) */
1124 	if (*(int *)data & ~LOCK_MASK)
1125 	    return EINVAL;
1126 	scp->status &= ~LOCK_MASK;
1127 	scp->status |= *(int *)data;
1128 	if (scp == sc->cur_scp)
1129 	    update_kbd_state(scp, scp->status, LOCK_MASK);
1130 	return 0;
1131 
1132     case KDGKBSTATE:    	/* get keyboard state (locks) */
1133 	if (scp == sc->cur_scp)
1134 	    save_kbd_state(scp);
1135 	*(int *)data = scp->status & LOCK_MASK;
1136 	return 0;
1137 
1138     case KDGETREPEAT:      	/* get keyboard repeat & delay rates */
1139     case KDSETREPEAT:      	/* set keyboard repeat & delay rates (new) */
1140 	error = kbdd_ioctl(sc->kbd, cmd, data);
1141 	if (error == ENOIOCTL)
1142 	    error = ENODEV;
1143 	return error;
1144 
1145 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1146     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1147     case _IO('K', 67):
1148 	ival = IOCPARM_IVAL(data);
1149 	data = (caddr_t)&ival;
1150 	/* FALLTHROUGH */
1151 #endif
1152     case KDSETRAD:      	/* set keyboard repeat & delay rates (old) */
1153 	if (*(int *)data & ~0x7f)
1154 	    return EINVAL;
1155 	error = kbdd_ioctl(sc->kbd, KDSETRAD, data);
1156 	if (error == ENOIOCTL)
1157 	    error = ENODEV;
1158 	return error;
1159 
1160 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1161     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1162     case _IO('K', 7):
1163 	ival = IOCPARM_IVAL(data);
1164 	data = (caddr_t)&ival;
1165 	/* FALLTHROUGH */
1166 #endif
1167     case KDSKBMODE:     	/* set keyboard mode */
1168 	switch (*(int *)data) {
1169 	case K_XLATE:   	/* switch to XLT ascii mode */
1170 	case K_RAW: 		/* switch to RAW scancode mode */
1171 	case K_CODE: 		/* switch to CODE mode */
1172 	    scp->kbd_mode = *(int *)data;
1173 	    if (scp == sc->cur_scp)
1174 		kbdd_ioctl(sc->kbd, KDSKBMODE, data);
1175 	    return 0;
1176 	default:
1177 	    return EINVAL;
1178 	}
1179 	/* NOT REACHED */
1180 
1181     case KDGKBMODE:     	/* get keyboard mode */
1182 	*(int *)data = scp->kbd_mode;
1183 	return 0;
1184 
1185     case KDGKBINFO:
1186 	error = kbdd_ioctl(sc->kbd, cmd, data);
1187 	if (error == ENOIOCTL)
1188 	    error = ENODEV;
1189 	return error;
1190 
1191 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1192     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1193     case _IO('K', 8):
1194 	ival = IOCPARM_IVAL(data);
1195 	data = (caddr_t)&ival;
1196 	/* FALLTHROUGH */
1197 #endif
1198     case KDMKTONE:      	/* sound the bell */
1199 	if (*(int*)data)
1200 	    sc_bell(scp, (*(int*)data)&0xffff,
1201 		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1202 	else
1203 	    sc_bell(scp, scp->bell_pitch, scp->bell_duration);
1204 	return 0;
1205 
1206 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1207     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1208     case _IO('K', 63):
1209 	ival = IOCPARM_IVAL(data);
1210 	data = (caddr_t)&ival;
1211 	/* FALLTHROUGH */
1212 #endif
1213     case KIOCSOUND:     	/* make tone (*data) hz */
1214 	if (scp == sc->cur_scp) {
1215 	    if (*(int *)data)
1216 		return sc_tone(*(int *)data);
1217 	    else
1218 		return sc_tone(0);
1219 	}
1220 	return 0;
1221 
1222     case KDGKBTYPE:     	/* get keyboard type */
1223 	error = kbdd_ioctl(sc->kbd, cmd, data);
1224 	if (error == ENOIOCTL) {
1225 	    /* always return something? XXX */
1226 	    *(int *)data = 0;
1227 	}
1228 	return 0;
1229 
1230 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1231     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1232     case _IO('K', 66):
1233 	ival = IOCPARM_IVAL(data);
1234 	data = (caddr_t)&ival;
1235 	/* FALLTHROUGH */
1236 #endif
1237     case KDSETLED:      	/* set keyboard LED status */
1238 	if (*(int *)data & ~LED_MASK)	/* FIXME: LOCK_MASK? */
1239 	    return EINVAL;
1240 	scp->status &= ~LED_MASK;
1241 	scp->status |= *(int *)data;
1242 	if (scp == sc->cur_scp)
1243 	    update_kbd_leds(scp, scp->status);
1244 	return 0;
1245 
1246     case KDGETLED:      	/* get keyboard LED status */
1247 	if (scp == sc->cur_scp)
1248 	    save_kbd_state(scp);
1249 	*(int *)data = scp->status & LED_MASK;
1250 	return 0;
1251 
1252     case KBADDKBD:		/* add/remove keyboard to/from mux */
1253     case KBRELKBD:
1254 	error = kbdd_ioctl(sc->kbd, cmd, data);
1255 	if (error == ENOIOCTL)
1256 	    error = ENODEV;
1257 	return error;
1258 
1259 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1260     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1261     case _IO('c', 110):
1262 	ival = IOCPARM_IVAL(data);
1263 	data = (caddr_t)&ival;
1264 	/* FALLTHROUGH */
1265 #endif
1266     case CONS_SETKBD: 		/* set the new keyboard */
1267 	{
1268 	    keyboard_t *newkbd;
1269 
1270 	    s = spltty();
1271 	    newkbd = kbd_get_keyboard(*(int *)data);
1272 	    if (newkbd == NULL) {
1273 		splx(s);
1274 		return EINVAL;
1275 	    }
1276 	    error = 0;
1277 	    if (sc->kbd != newkbd) {
1278 		i = kbd_allocate(newkbd->kb_name, newkbd->kb_unit,
1279 				 (void *)&sc->keyboard, sckbdevent, sc);
1280 		/* i == newkbd->kb_index */
1281 		if (i >= 0) {
1282 		    if (sc->kbd != NULL) {
1283 			save_kbd_state(sc->cur_scp);
1284 			kbd_release(sc->kbd, (void *)&sc->keyboard);
1285 		    }
1286 		    sc->kbd = kbd_get_keyboard(i); /* sc->kbd == newkbd */
1287 		    sc->keyboard = i;
1288 		    kbdd_ioctl(sc->kbd, KDSKBMODE,
1289 			      (caddr_t)&sc->cur_scp->kbd_mode);
1290 		    update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1291 				     LOCK_MASK);
1292 		} else {
1293 		    error = EPERM;	/* XXX */
1294 		}
1295 	    }
1296 	    splx(s);
1297 	    return error;
1298 	}
1299 
1300     case CONS_RELKBD: 		/* release the current keyboard */
1301 	s = spltty();
1302 	error = 0;
1303 	if (sc->kbd != NULL) {
1304 	    save_kbd_state(sc->cur_scp);
1305 	    error = kbd_release(sc->kbd, (void *)&sc->keyboard);
1306 	    if (error == 0) {
1307 		sc->kbd = NULL;
1308 		sc->keyboard = -1;
1309 	    }
1310 	}
1311 	splx(s);
1312 	return error;
1313 
1314     case CONS_GETTERM:		/* get the current terminal emulator info */
1315 	{
1316 	    sc_term_sw_t *sw;
1317 
1318 	    if (((term_info_t *)data)->ti_index == 0) {
1319 		sw = scp->tsw;
1320 	    } else {
1321 		sw = sc_term_match_by_number(((term_info_t *)data)->ti_index);
1322 	    }
1323 	    if (sw != NULL) {
1324 		strncpy(((term_info_t *)data)->ti_name, sw->te_name,
1325 			sizeof(((term_info_t *)data)->ti_name));
1326 		strncpy(((term_info_t *)data)->ti_desc, sw->te_desc,
1327 			sizeof(((term_info_t *)data)->ti_desc));
1328 		((term_info_t *)data)->ti_flags = 0;
1329 		return 0;
1330 	    } else {
1331 		((term_info_t *)data)->ti_name[0] = '\0';
1332 		((term_info_t *)data)->ti_desc[0] = '\0';
1333 		((term_info_t *)data)->ti_flags = 0;
1334 		return EINVAL;
1335 	    }
1336 	}
1337 
1338     case CONS_SETTERM:		/* set the current terminal emulator */
1339 	s = spltty();
1340 	error = sc_init_emulator(scp, ((term_info_t *)data)->ti_name);
1341 	/* FIXME: what if scp == sc_console! XXX */
1342 	splx(s);
1343 	return error;
1344 
1345     case GIO_SCRNMAP:   	/* get output translation table */
1346 	bcopy(&sc->scr_map, data, sizeof(sc->scr_map));
1347 	return 0;
1348 
1349     case PIO_SCRNMAP:   	/* set output translation table */
1350 	bcopy(data, &sc->scr_map, sizeof(sc->scr_map));
1351 	for (i=0; i<sizeof(sc->scr_map); i++) {
1352 	    sc->scr_rmap[sc->scr_map[i]] = i;
1353 	}
1354 	return 0;
1355 
1356     case GIO_KEYMAP:		/* get keyboard translation table */
1357     case PIO_KEYMAP:		/* set keyboard translation table */
1358     case GIO_DEADKEYMAP:	/* get accent key translation table */
1359     case PIO_DEADKEYMAP:	/* set accent key translation table */
1360     case GETFKEY:		/* get function key string */
1361     case SETFKEY:		/* set function key string */
1362 	error = kbdd_ioctl(sc->kbd, cmd, data);
1363 	if (error == ENOIOCTL)
1364 	    error = ENODEV;
1365 	return error;
1366 
1367 #ifndef SC_NO_FONT_LOADING
1368 
1369     case PIO_FONT8x8:   	/* set 8x8 dot font */
1370 	if (!ISFONTAVAIL(sc->adp->va_flags))
1371 	    return ENXIO;
1372 	bcopy(data, sc->font_8, 8*256);
1373 	sc->fonts_loaded |= FONT_8;
1374 	/*
1375 	 * FONT KLUDGE
1376 	 * Always use the font page #0. XXX
1377 	 * Don't load if the current font size is not 8x8.
1378 	 */
1379 	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size < 14))
1380 	    sc_load_font(sc->cur_scp, 0, 8, 8, sc->font_8, 0, 256);
1381 	return 0;
1382 
1383     case GIO_FONT8x8:   	/* get 8x8 dot font */
1384 	if (!ISFONTAVAIL(sc->adp->va_flags))
1385 	    return ENXIO;
1386 	if (sc->fonts_loaded & FONT_8) {
1387 	    bcopy(sc->font_8, data, 8*256);
1388 	    return 0;
1389 	}
1390 	else
1391 	    return ENXIO;
1392 
1393     case PIO_FONT8x14:  	/* set 8x14 dot font */
1394 	if (!ISFONTAVAIL(sc->adp->va_flags))
1395 	    return ENXIO;
1396 	bcopy(data, sc->font_14, 14*256);
1397 	sc->fonts_loaded |= FONT_14;
1398 	/*
1399 	 * FONT KLUDGE
1400 	 * Always use the font page #0. XXX
1401 	 * Don't load if the current font size is not 8x14.
1402 	 */
1403 	if (ISTEXTSC(sc->cur_scp)
1404 	    && (sc->cur_scp->font_size >= 14)
1405 	    && (sc->cur_scp->font_size < 16))
1406 	    sc_load_font(sc->cur_scp, 0, 14, 8, sc->font_14, 0, 256);
1407 	return 0;
1408 
1409     case GIO_FONT8x14:  	/* get 8x14 dot font */
1410 	if (!ISFONTAVAIL(sc->adp->va_flags))
1411 	    return ENXIO;
1412 	if (sc->fonts_loaded & FONT_14) {
1413 	    bcopy(sc->font_14, data, 14*256);
1414 	    return 0;
1415 	}
1416 	else
1417 	    return ENXIO;
1418 
1419     case PIO_FONT8x16:  	/* set 8x16 dot font */
1420 	if (!ISFONTAVAIL(sc->adp->va_flags))
1421 	    return ENXIO;
1422 	bcopy(data, sc->font_16, 16*256);
1423 	sc->fonts_loaded |= FONT_16;
1424 	/*
1425 	 * FONT KLUDGE
1426 	 * Always use the font page #0. XXX
1427 	 * Don't load if the current font size is not 8x16.
1428 	 */
1429 	if (ISTEXTSC(sc->cur_scp) && (sc->cur_scp->font_size >= 16))
1430 	    sc_load_font(sc->cur_scp, 0, 16, 8, sc->font_16, 0, 256);
1431 	return 0;
1432 
1433     case GIO_FONT8x16:  	/* get 8x16 dot font */
1434 	if (!ISFONTAVAIL(sc->adp->va_flags))
1435 	    return ENXIO;
1436 	if (sc->fonts_loaded & FONT_16) {
1437 	    bcopy(sc->font_16, data, 16*256);
1438 	    return 0;
1439 	}
1440 	else
1441 	    return ENXIO;
1442 
1443 #endif /* SC_NO_FONT_LOADING */
1444 
1445     default:
1446 	break;
1447     }
1448 
1449     return (ttyioctl(dev, cmd, data, flag, td));
1450 }
1451 
1452 static void
1453 scstart(struct tty *tp)
1454 {
1455     struct clist *rbp;
1456     int s, len;
1457     u_char buf[PCBURST];
1458     scr_stat *scp = sc_get_stat(tp->t_dev);
1459 
1460     if (scp->status & SLKED ||
1461 	(scp == scp->sc->cur_scp && scp->sc->blink_in_progress))
1462 	return;
1463     s = spltty();
1464     if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1465 	tp->t_state |= TS_BUSY;
1466 	rbp = &tp->t_outq;
1467 	while (rbp->c_cc) {
1468 	    len = q_to_b(rbp, buf, PCBURST);
1469 	    splx(s);
1470 	    sc_puts(scp, buf, len);
1471 	    s = spltty();
1472 	}
1473 	tp->t_state &= ~TS_BUSY;
1474 	ttwwakeup(tp);
1475     }
1476     splx(s);
1477 }
1478 
1479 static void
1480 sc_cnprobe(struct consdev *cp)
1481 {
1482     int unit;
1483     int flags;
1484 
1485     cp->cn_pri = sc_get_cons_priority(&unit, &flags);
1486 
1487     /* a video card is always required */
1488     if (!scvidprobe(unit, flags, TRUE))
1489 	cp->cn_pri = CN_DEAD;
1490 
1491     /* syscons will become console even when there is no keyboard */
1492     sckbdprobe(unit, flags, TRUE);
1493 
1494     if (cp->cn_pri == CN_DEAD)
1495 	return;
1496 
1497     /* initialize required fields */
1498     sprintf(cp->cn_name, "consolectl");
1499 }
1500 
1501 static void
1502 sc_cninit(struct consdev *cp)
1503 {
1504     int unit;
1505     int flags;
1506 
1507     sc_get_cons_priority(&unit, &flags);
1508     scinit(unit, flags | SC_KERNEL_CONSOLE);
1509     sc_console_unit = unit;
1510     sc_console = sc_get_stat(sc_get_softc(unit, SC_KERNEL_CONSOLE)->dev[0]);
1511     sc_consptr = cp;
1512 }
1513 
1514 static void
1515 sc_cnterm(struct consdev *cp)
1516 {
1517     /* we are not the kernel console any more, release everything */
1518 
1519     if (sc_console_unit < 0)
1520 	return;			/* shouldn't happen */
1521 
1522 #if 0 /* XXX */
1523     sc_clear_screen(sc_console);
1524     sccnupdate(sc_console);
1525 #endif
1526 
1527     scterm(sc_console_unit, SC_KERNEL_CONSOLE);
1528     sc_console_unit = -1;
1529     sc_console = NULL;
1530 }
1531 
1532 static void
1533 sc_cnputc(struct consdev *cd, int c)
1534 {
1535     u_char buf[1];
1536     scr_stat *scp = sc_console;
1537     void *save;
1538 #ifndef SC_NO_HISTORY
1539     struct tty *tp;
1540 #endif /* !SC_NO_HISTORY */
1541     int s;
1542 
1543     /* assert(sc_console != NULL) */
1544 
1545 #ifndef SC_NO_HISTORY
1546     if (scp == scp->sc->cur_scp && scp->status & SLKED) {
1547 	scp->status &= ~SLKED;
1548 	update_kbd_state(scp, scp->status, SLKED);
1549 	if (scp->status & BUFFER_SAVED) {
1550 	    if (!sc_hist_restore(scp))
1551 		sc_remove_cutmarking(scp);
1552 	    scp->status &= ~BUFFER_SAVED;
1553 	    scp->status |= CURSOR_ENABLED;
1554 	    sc_draw_cursor_image(scp);
1555 	}
1556 	tp = VIRTUAL_TTY(scp->sc, scp->index);
1557 	if (ISTTYOPEN(tp))
1558 	    scstart(tp);
1559     }
1560 #endif /* !SC_NO_HISTORY */
1561 
1562     save = scp->ts;
1563     if (kernel_console_ts != NULL)
1564 	scp->ts = kernel_console_ts;
1565     buf[0] = c;
1566     sc_puts(scp, buf, 1);
1567     scp->ts = save;
1568 
1569     s = spltty();	/* block sckbdevent and scrn_timer */
1570     sccnupdate(scp);
1571     splx(s);
1572 }
1573 
1574 static int
1575 sc_cngetc(struct consdev *cd)
1576 {
1577     return sccngetch(SCGETC_NONBLOCK);
1578 }
1579 
1580 static int
1581 sccngetch(int flags)
1582 {
1583     static struct fkeytab fkey;
1584     static int fkeycp;
1585     scr_stat *scp;
1586     u_char *p;
1587     int cur_mode;
1588     int s = spltty();	/* block sckbdevent and scrn_timer while we poll */
1589     int c;
1590 
1591     /* assert(sc_console != NULL) */
1592 
1593     /*
1594      * Stop the screen saver and update the screen if necessary.
1595      * What if we have been running in the screen saver code... XXX
1596      */
1597     sc_touch_scrn_saver();
1598     scp = sc_console->sc->cur_scp;	/* XXX */
1599     sccnupdate(scp);
1600 
1601     if (fkeycp < fkey.len) {
1602 	splx(s);
1603 	return fkey.str[fkeycp++];
1604     }
1605 
1606     if (scp->sc->kbd == NULL) {
1607 	splx(s);
1608 	return -1;
1609     }
1610 
1611     /*
1612      * Make sure the keyboard is accessible even when the kbd device
1613      * driver is disabled.
1614      */
1615     kbdd_enable(scp->sc->kbd);
1616 
1617     /* we shall always use the keyboard in the XLATE mode here */
1618     cur_mode = scp->kbd_mode;
1619     scp->kbd_mode = K_XLATE;
1620     kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1621 
1622     kbdd_poll(scp->sc->kbd, TRUE);
1623     c = scgetc(scp->sc, SCGETC_CN | flags);
1624     kbdd_poll(scp->sc->kbd, FALSE);
1625 
1626     scp->kbd_mode = cur_mode;
1627     kbdd_ioctl(scp->sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
1628     kbdd_disable(scp->sc->kbd);
1629     splx(s);
1630 
1631     switch (KEYFLAGS(c)) {
1632     case 0:	/* normal char */
1633 	return KEYCHAR(c);
1634     case FKEY:	/* function key */
1635 	p = kbdd_get_fkeystr(scp->sc->kbd, KEYCHAR(c), (size_t *)&fkeycp);
1636 	fkey.len = fkeycp;
1637 	if ((p != NULL) && (fkey.len > 0)) {
1638 	    bcopy(p, fkey.str, fkey.len);
1639 	    fkeycp = 1;
1640 	    return fkey.str[0];
1641 	}
1642 	return c;	/* XXX */
1643     case NOKEY:
1644     case ERRKEY:
1645     default:
1646 	return -1;
1647     }
1648     /* NOT REACHED */
1649 }
1650 
1651 static void
1652 sccnupdate(scr_stat *scp)
1653 {
1654     /* this is a cut-down version of scrn_timer()... */
1655 
1656     if (scp->sc->font_loading_in_progress)
1657 	return;
1658 
1659     if (debugger > 0 || panicstr || shutdown_in_progress) {
1660 	sc_touch_scrn_saver();
1661     } else if (scp != scp->sc->cur_scp) {
1662 	return;
1663     }
1664 
1665     if (!run_scrn_saver)
1666 	scp->sc->flags &= ~SC_SCRN_IDLE;
1667 #ifdef DEV_SPLASH
1668     if ((saver_mode != CONS_LKM_SAVER) || !(scp->sc->flags & SC_SCRN_IDLE))
1669 	if (scp->sc->flags & SC_SCRN_BLANKED)
1670             stop_scrn_saver(scp->sc, current_saver);
1671 #endif
1672 
1673     if (scp != scp->sc->cur_scp || scp->sc->blink_in_progress
1674 	|| scp->sc->switch_in_progress)
1675 	return;
1676     /*
1677      * FIXME: unlike scrn_timer(), we call scrn_update() from here even
1678      * when write_in_progress is non-zero.  XXX
1679      */
1680 
1681     if (!ISGRAPHSC(scp) && !(scp->sc->flags & SC_SCRN_BLANKED))
1682 	scrn_update(scp, TRUE);
1683 }
1684 
1685 static void
1686 scrn_timer(void *arg)
1687 {
1688 #ifndef PC98
1689     static int kbd_interval = 0;
1690 #endif
1691     struct timeval tv;
1692     sc_softc_t *sc;
1693     scr_stat *scp;
1694     int again;
1695     int s;
1696 
1697     again = (arg != NULL);
1698     if (arg != NULL)
1699 	sc = (sc_softc_t *)arg;
1700     else if (sc_console != NULL)
1701 	sc = sc_console->sc;
1702     else
1703 	return;
1704 
1705     /* don't do anything when we are performing some I/O operations */
1706     if (sc->font_loading_in_progress) {
1707 	if (again)
1708 	    timeout(scrn_timer, sc, hz / 10);
1709 	return;
1710     }
1711     s = spltty();
1712 
1713 #ifndef PC98
1714     if ((sc->kbd == NULL) && (sc->config & SC_AUTODETECT_KBD)) {
1715 	/* try to allocate a keyboard automatically */
1716 	if (++kbd_interval >= 25) {
1717 	    sc->keyboard = sc_allocate_keyboard(sc, -1);
1718 	    if (sc->keyboard >= 0) {
1719 		sc->kbd = kbd_get_keyboard(sc->keyboard);
1720 		kbdd_ioctl(sc->kbd, KDSKBMODE,
1721 			  (caddr_t)&sc->cur_scp->kbd_mode);
1722 		update_kbd_state(sc->cur_scp, sc->cur_scp->status,
1723 				 LOCK_MASK);
1724 	    }
1725 	    kbd_interval = 0;
1726 	}
1727     }
1728 #endif /* PC98 */
1729 
1730     /* find the vty to update */
1731     scp = sc->cur_scp;
1732 
1733     /* should we stop the screen saver? */
1734     getmicrouptime(&tv);
1735     if (debugger > 0 || panicstr || shutdown_in_progress)
1736 	sc_touch_scrn_saver();
1737     if (run_scrn_saver) {
1738 	if (tv.tv_sec > sc->scrn_time_stamp + scrn_blank_time)
1739 	    sc->flags |= SC_SCRN_IDLE;
1740 	else
1741 	    sc->flags &= ~SC_SCRN_IDLE;
1742     } else {
1743 	sc->scrn_time_stamp = tv.tv_sec;
1744 	sc->flags &= ~SC_SCRN_IDLE;
1745 	if (scrn_blank_time > 0)
1746 	    run_scrn_saver = TRUE;
1747     }
1748 #ifdef DEV_SPLASH
1749     if ((saver_mode != CONS_LKM_SAVER) || !(sc->flags & SC_SCRN_IDLE))
1750 	if (sc->flags & SC_SCRN_BLANKED)
1751             stop_scrn_saver(sc, current_saver);
1752 #endif
1753 
1754     /* should we just return ? */
1755     if (sc->blink_in_progress || sc->switch_in_progress
1756 	|| sc->write_in_progress) {
1757 	if (again)
1758 	    timeout(scrn_timer, sc, hz / 10);
1759 	splx(s);
1760 	return;
1761     }
1762 
1763     /* Update the screen */
1764     scp = sc->cur_scp;		/* cur_scp may have changed... */
1765     if (!ISGRAPHSC(scp) && !(sc->flags & SC_SCRN_BLANKED))
1766 	scrn_update(scp, TRUE);
1767 
1768 #ifdef DEV_SPLASH
1769     /* should we activate the screen saver? */
1770     if ((saver_mode == CONS_LKM_SAVER) && (sc->flags & SC_SCRN_IDLE))
1771 	if (!ISGRAPHSC(scp) || (sc->flags & SC_SCRN_BLANKED))
1772 	    (*current_saver)(sc, TRUE);
1773 #endif
1774 
1775     if (again)
1776 	timeout(scrn_timer, sc, hz / 25);
1777     splx(s);
1778 }
1779 
1780 static int
1781 and_region(int *s1, int *e1, int s2, int e2)
1782 {
1783     if (*e1 < s2 || e2 < *s1)
1784 	return FALSE;
1785     *s1 = imax(*s1, s2);
1786     *e1 = imin(*e1, e2);
1787     return TRUE;
1788 }
1789 
1790 static void
1791 scrn_update(scr_stat *scp, int show_cursor)
1792 {
1793     int start;
1794     int end;
1795     int s;
1796     int e;
1797 
1798     /* assert(scp == scp->sc->cur_scp) */
1799 
1800     SC_VIDEO_LOCK(scp->sc);
1801 
1802 #ifndef SC_NO_CUTPASTE
1803     /* remove the previous mouse pointer image if necessary */
1804     if (scp->status & MOUSE_VISIBLE) {
1805 	s = scp->mouse_pos;
1806 	e = scp->mouse_pos + scp->xsize + 1;
1807 	if ((scp->status & (MOUSE_MOVED | MOUSE_HIDDEN))
1808 	    || and_region(&s, &e, scp->start, scp->end)
1809 	    || ((scp->status & CURSOR_ENABLED) &&
1810 		(scp->cursor_pos != scp->cursor_oldpos) &&
1811 		(and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1812 		 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1813 	    sc_remove_mouse_image(scp);
1814 	    if (scp->end >= scp->xsize*scp->ysize)
1815 		scp->end = scp->xsize*scp->ysize - 1;
1816 	}
1817     }
1818 #endif /* !SC_NO_CUTPASTE */
1819 
1820 #if 1
1821     /* debug: XXX */
1822     if (scp->end >= scp->xsize*scp->ysize) {
1823 	printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1824 	scp->end = scp->xsize*scp->ysize - 1;
1825     }
1826     if (scp->start < 0) {
1827 	printf("scrn_update(): scp->start %d < 0\n", scp->start);
1828 	scp->start = 0;
1829     }
1830 #endif
1831 
1832     /* update screen image */
1833     if (scp->start <= scp->end)  {
1834 	if (scp->mouse_cut_end >= 0) {
1835 	    /* there is a marked region for cut & paste */
1836 	    if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1837 		start = scp->mouse_cut_start;
1838 		end = scp->mouse_cut_end;
1839 	    } else {
1840 		start = scp->mouse_cut_end;
1841 		end = scp->mouse_cut_start - 1;
1842 	    }
1843 	    s = start;
1844 	    e = end;
1845 	    /* does the cut-mark region overlap with the update region? */
1846 	    if (and_region(&s, &e, scp->start, scp->end)) {
1847 		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1848 		s = 0;
1849 		e = start - 1;
1850 		if (and_region(&s, &e, scp->start, scp->end))
1851 		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1852 		s = end + 1;
1853 		e = scp->xsize*scp->ysize - 1;
1854 		if (and_region(&s, &e, scp->start, scp->end))
1855 		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1856 	    } else {
1857 		(*scp->rndr->draw)(scp, scp->start,
1858 				   scp->end - scp->start + 1, FALSE);
1859 	    }
1860 	} else {
1861 	    (*scp->rndr->draw)(scp, scp->start,
1862 			       scp->end - scp->start + 1, FALSE);
1863 	}
1864     }
1865 
1866     /* we are not to show the cursor and the mouse pointer... */
1867     if (!show_cursor) {
1868         scp->end = 0;
1869         scp->start = scp->xsize*scp->ysize - 1;
1870 	SC_VIDEO_UNLOCK(scp->sc);
1871 	return;
1872     }
1873 
1874     /* update cursor image */
1875     if (scp->status & CURSOR_ENABLED) {
1876 	s = scp->start;
1877 	e = scp->end;
1878         /* did cursor move since last time ? */
1879         if (scp->cursor_pos != scp->cursor_oldpos) {
1880             /* do we need to remove old cursor image ? */
1881             if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1882                 sc_remove_cursor_image(scp);
1883             sc_draw_cursor_image(scp);
1884         } else {
1885             if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1886 		/* cursor didn't move, but has been overwritten */
1887 		sc_draw_cursor_image(scp);
1888 	    else if (scp->curs_attr.flags & CONS_BLINK_CURSOR)
1889 		/* if it's a blinking cursor, update it */
1890 		(*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1891 					   sc_inside_cutmark(scp,
1892 					       scp->cursor_pos));
1893         }
1894     }
1895 
1896 #ifndef SC_NO_CUTPASTE
1897     /* update "pseudo" mouse pointer image */
1898     if (scp->sc->flags & SC_MOUSE_ENABLED) {
1899 	if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1900 	    scp->status &= ~MOUSE_MOVED;
1901 	    sc_draw_mouse_image(scp);
1902 	}
1903     }
1904 #endif /* SC_NO_CUTPASTE */
1905 
1906     scp->end = 0;
1907     scp->start = scp->xsize*scp->ysize - 1;
1908 
1909     SC_VIDEO_UNLOCK(scp->sc);
1910 }
1911 
1912 #ifdef DEV_SPLASH
1913 static int
1914 scsplash_callback(int event, void *arg)
1915 {
1916     sc_softc_t *sc;
1917     int error;
1918 
1919     sc = (sc_softc_t *)arg;
1920 
1921     switch (event) {
1922     case SPLASH_INIT:
1923 	if (add_scrn_saver(scsplash_saver) == 0) {
1924 	    sc->flags &= ~SC_SAVER_FAILED;
1925 	    run_scrn_saver = TRUE;
1926 	    if (cold && !(boothowto & RB_VERBOSE)) {
1927 		scsplash_stick(TRUE);
1928 		(*current_saver)(sc, TRUE);
1929 	    }
1930 	}
1931 	return 0;
1932 
1933     case SPLASH_TERM:
1934 	if (current_saver == scsplash_saver) {
1935 	    scsplash_stick(FALSE);
1936 	    error = remove_scrn_saver(scsplash_saver);
1937 	    if (error)
1938 		return error;
1939 	}
1940 	return 0;
1941 
1942     default:
1943 	return EINVAL;
1944     }
1945 }
1946 
1947 static void
1948 scsplash_saver(sc_softc_t *sc, int show)
1949 {
1950     static int busy = FALSE;
1951     scr_stat *scp;
1952 
1953     if (busy)
1954 	return;
1955     busy = TRUE;
1956 
1957     scp = sc->cur_scp;
1958     if (show) {
1959 	if (!(sc->flags & SC_SAVER_FAILED)) {
1960 	    if (!(sc->flags & SC_SCRN_BLANKED))
1961 		set_scrn_saver_mode(scp, -1, NULL, 0);
1962 	    switch (splash(sc->adp, TRUE)) {
1963 	    case 0:		/* succeeded */
1964 		break;
1965 	    case EAGAIN:	/* try later */
1966 		restore_scrn_saver_mode(scp, FALSE);
1967 		sc_touch_scrn_saver();		/* XXX */
1968 		break;
1969 	    default:
1970 		sc->flags |= SC_SAVER_FAILED;
1971 		scsplash_stick(FALSE);
1972 		restore_scrn_saver_mode(scp, TRUE);
1973 		printf("scsplash_saver(): failed to put up the image\n");
1974 		break;
1975 	    }
1976 	}
1977     } else if (!sticky_splash) {
1978 	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1979 	    restore_scrn_saver_mode(scp, TRUE);
1980     }
1981     busy = FALSE;
1982 }
1983 
1984 static int
1985 add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1986 {
1987 #if 0
1988     int error;
1989 
1990     if (current_saver != none_saver) {
1991 	error = remove_scrn_saver(current_saver);
1992 	if (error)
1993 	    return error;
1994     }
1995 #endif
1996     if (current_saver != none_saver)
1997 	return EBUSY;
1998 
1999     run_scrn_saver = FALSE;
2000     saver_mode = CONS_LKM_SAVER;
2001     current_saver = this_saver;
2002     return 0;
2003 }
2004 
2005 static int
2006 remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
2007 {
2008     if (current_saver != this_saver)
2009 	return EINVAL;
2010 
2011 #if 0
2012     /*
2013      * In order to prevent `current_saver' from being called by
2014      * the timeout routine `scrn_timer()' while we manipulate
2015      * the saver list, we shall set `current_saver' to `none_saver'
2016      * before stopping the current saver, rather than blocking by `splXX()'.
2017      */
2018     current_saver = none_saver;
2019     if (scrn_blanked)
2020         stop_scrn_saver(this_saver);
2021 #endif
2022 
2023     /* unblank all blanked screens */
2024     wait_scrn_saver_stop(NULL);
2025     if (scrn_blanked)
2026 	return EBUSY;
2027 
2028     current_saver = none_saver;
2029     return 0;
2030 }
2031 
2032 static int
2033 set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
2034 {
2035     int s;
2036 
2037     /* assert(scp == scp->sc->cur_scp) */
2038     s = spltty();
2039     if (!ISGRAPHSC(scp))
2040 	sc_remove_cursor_image(scp);
2041     scp->splash_save_mode = scp->mode;
2042     scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
2043     scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
2044     scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
2045     scp->sc->flags |= SC_SCRN_BLANKED;
2046     ++scrn_blanked;
2047     splx(s);
2048     if (mode < 0)
2049 	return 0;
2050     scp->mode = mode;
2051     if (set_mode(scp) == 0) {
2052 	if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
2053 	    scp->status |= GRAPHICS_MODE;
2054 #ifndef SC_NO_PALETTE_LOADING
2055 	if (pal != NULL)
2056 	    vidd_load_palette(scp->sc->adp, pal);
2057 #endif
2058 	sc_set_border(scp, border);
2059 	return 0;
2060     } else {
2061 	s = spltty();
2062 	scp->mode = scp->splash_save_mode;
2063 	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2064 	scp->status |= scp->splash_save_status;
2065 	splx(s);
2066 	return 1;
2067     }
2068 }
2069 
2070 static int
2071 restore_scrn_saver_mode(scr_stat *scp, int changemode)
2072 {
2073     int mode;
2074     int status;
2075     int s;
2076 
2077     /* assert(scp == scp->sc->cur_scp) */
2078     s = spltty();
2079     mode = scp->mode;
2080     status = scp->status;
2081     scp->mode = scp->splash_save_mode;
2082     scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2083     scp->status |= scp->splash_save_status;
2084     scp->sc->flags &= ~SC_SCRN_BLANKED;
2085     if (!changemode) {
2086 	if (!ISGRAPHSC(scp))
2087 	    sc_draw_cursor_image(scp);
2088 	--scrn_blanked;
2089 	splx(s);
2090 	return 0;
2091     }
2092     if (set_mode(scp) == 0) {
2093 #ifndef SC_NO_PALETTE_LOADING
2094 	vidd_load_palette(scp->sc->adp, scp->sc->palette);
2095 #endif
2096 	--scrn_blanked;
2097 	splx(s);
2098 	return 0;
2099     } else {
2100 	scp->mode = mode;
2101 	scp->status = status;
2102 	splx(s);
2103 	return 1;
2104     }
2105 }
2106 
2107 static void
2108 stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2109 {
2110     (*saver)(sc, FALSE);
2111     run_scrn_saver = FALSE;
2112     /* the screen saver may have chosen not to stop after all... */
2113     if (sc->flags & SC_SCRN_BLANKED)
2114 	return;
2115 
2116     mark_all(sc->cur_scp);
2117     if (sc->delayed_next_scr)
2118 	sc_switch_scr(sc, sc->delayed_next_scr - 1);
2119     if (debugger == 0)
2120 	wakeup(&scrn_blanked);
2121 }
2122 
2123 static int
2124 wait_scrn_saver_stop(sc_softc_t *sc)
2125 {
2126     int error = 0;
2127 
2128     while (scrn_blanked > 0) {
2129 	run_scrn_saver = FALSE;
2130 	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2131 	    error = 0;
2132 	    break;
2133 	}
2134 	error = tsleep(&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2135 	if ((error != 0) && (error != ERESTART))
2136 	    break;
2137     }
2138     run_scrn_saver = FALSE;
2139     return error;
2140 }
2141 #endif /* DEV_SPLASH */
2142 
2143 void
2144 sc_touch_scrn_saver(void)
2145 {
2146     scsplash_stick(FALSE);
2147     run_scrn_saver = FALSE;
2148 }
2149 
2150 int
2151 sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2152 {
2153     scr_stat *cur_scp;
2154     struct tty *tp;
2155     struct proc *p;
2156     int s;
2157 
2158     DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2159 
2160     if (sc->cur_scp == NULL)
2161 	return (0);
2162 
2163     /* prevent switch if previously requested */
2164     if (sc->flags & SC_SCRN_VTYLOCK) {
2165 	    sc_bell(sc->cur_scp, sc->cur_scp->bell_pitch,
2166 		sc->cur_scp->bell_duration);
2167 	    return EPERM;
2168     }
2169 
2170     /* delay switch if the screen is blanked or being updated */
2171     if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2172 	|| sc->blink_in_progress) {
2173 	sc->delayed_next_scr = next_scr + 1;
2174 	sc_touch_scrn_saver();
2175 	DPRINTF(5, ("switch delayed\n"));
2176 	return 0;
2177     }
2178     sc->delayed_next_scr = 0;
2179 
2180     s = spltty();
2181     cur_scp = sc->cur_scp;
2182 
2183     /* we are in the middle of the vty switching process... */
2184     if (sc->switch_in_progress
2185 	&& (cur_scp->smode.mode == VT_PROCESS)
2186 	&& cur_scp->proc) {
2187 	p = pfind(cur_scp->pid);
2188 	if (cur_scp->proc != p) {
2189 	    if (p)
2190 		PROC_UNLOCK(p);
2191 	    /*
2192 	     * The controlling process has died!!.  Do some clean up.
2193 	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2194 	     * are not reset here yet; they will be cleared later.
2195 	     */
2196 	    DPRINTF(5, ("cur_scp controlling process %d died, ",
2197 	       cur_scp->pid));
2198 	    if (cur_scp->status & SWITCH_WAIT_REL) {
2199 		/*
2200 		 * Force the previous switch to finish, but return now
2201 		 * with error.
2202 		 */
2203 		DPRINTF(5, ("reset WAIT_REL, "));
2204 		finish_vt_rel(cur_scp, TRUE, &s);
2205 		splx(s);
2206 		DPRINTF(5, ("finishing previous switch\n"));
2207 		return EINVAL;
2208 	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2209 		/* let's assume screen switch has been completed. */
2210 		DPRINTF(5, ("reset WAIT_ACQ, "));
2211 		finish_vt_acq(cur_scp);
2212 	    } else {
2213 		/*
2214 	 	 * We are in between screen release and acquisition, and
2215 		 * reached here via scgetc() or scrn_timer() which has
2216 		 * interrupted exchange_scr(). Don't do anything stupid.
2217 		 */
2218 		DPRINTF(5, ("waiting nothing, "));
2219 	    }
2220 	} else {
2221 	    if (p)
2222 		PROC_UNLOCK(p);
2223 	    /*
2224 	     * The controlling process is alive, but not responding...
2225 	     * It is either buggy or it may be just taking time.
2226 	     * The following code is a gross kludge to cope with this
2227 	     * problem for which there is no clean solution. XXX
2228 	     */
2229 	    if (cur_scp->status & SWITCH_WAIT_REL) {
2230 		switch (sc->switch_in_progress++) {
2231 		case 1:
2232 		    break;
2233 		case 2:
2234 		    DPRINTF(5, ("sending relsig again, "));
2235 		    signal_vt_rel(cur_scp);
2236 		    break;
2237 		case 3:
2238 		    break;
2239 		case 4:
2240 		default:
2241 		    /*
2242 		     * Act as if the controlling program returned
2243 		     * VT_FALSE.
2244 		     */
2245 		    DPRINTF(5, ("force reset WAIT_REL, "));
2246 		    finish_vt_rel(cur_scp, FALSE, &s);
2247 		    splx(s);
2248 		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2249 		    return EINVAL;
2250 		}
2251 	    } else if (cur_scp->status & SWITCH_WAIT_ACQ) {
2252 		switch (sc->switch_in_progress++) {
2253 		case 1:
2254 		    break;
2255 		case 2:
2256 		    DPRINTF(5, ("sending acqsig again, "));
2257 		    signal_vt_acq(cur_scp);
2258 		    break;
2259 		case 3:
2260 		    break;
2261 		case 4:
2262 		default:
2263 		     /* clear the flag and finish the previous switch */
2264 		    DPRINTF(5, ("force reset WAIT_ACQ, "));
2265 		    finish_vt_acq(cur_scp);
2266 		    break;
2267 		}
2268 	    }
2269 	}
2270     }
2271 
2272     /*
2273      * Return error if an invalid argument is given, or vty switch
2274      * is still in progress.
2275      */
2276     if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2277 	|| sc->switch_in_progress) {
2278 	splx(s);
2279 	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2280 	DPRINTF(5, ("error 1\n"));
2281 	return EINVAL;
2282     }
2283 
2284     /*
2285      * Don't allow switching away from the graphics mode vty
2286      * if the switch mode is VT_AUTO, unless the next vty is the same
2287      * as the current or the current vty has been closed (but showing).
2288      */
2289     tp = VIRTUAL_TTY(sc, cur_scp->index);
2290     if ((cur_scp->index != next_scr)
2291 	&& ISTTYOPEN(tp)
2292 	&& (cur_scp->smode.mode == VT_AUTO)
2293 	&& ISGRAPHSC(cur_scp)) {
2294 	splx(s);
2295 	sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2296 	DPRINTF(5, ("error, graphics mode\n"));
2297 	return EINVAL;
2298     }
2299 
2300     /*
2301      * Is the wanted vty open? Don't allow switching to a closed vty.
2302      * If we are in DDB, don't switch to a vty in the VT_PROCESS mode.
2303      * Note that we always allow the user to switch to the kernel
2304      * console even if it is closed.
2305      */
2306     if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2307 	tp = VIRTUAL_TTY(sc, next_scr);
2308 	if (!ISTTYOPEN(tp)) {
2309 	    splx(s);
2310 	    sc_bell(cur_scp, bios_value.bell_pitch, BELL_DURATION);
2311 	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2312 	    return EINVAL;
2313 	}
2314 	if ((debugger > 0) && (SC_STAT(tp->t_dev)->smode.mode == VT_PROCESS)) {
2315 	    splx(s);
2316 	    DPRINTF(5, ("error 3, requested vty is in the VT_PROCESS mode\n"));
2317 	    return EINVAL;
2318 	}
2319     }
2320 
2321     /* this is the start of vty switching process... */
2322     ++sc->switch_in_progress;
2323     sc->old_scp = cur_scp;
2324     sc->new_scp = sc_get_stat(SC_DEV(sc, next_scr));
2325     if (sc->new_scp == sc->old_scp) {
2326 	sc->switch_in_progress = 0;
2327 	/*
2328 	 * XXX wakeup() locks the scheduler lock which will hang if
2329 	 * the lock is in an in-between state, e.g., when we stop at
2330 	 * a breakpoint at fork_exit.  It has always been wrong to call
2331 	 * wakeup() when the debugger is active.  In RELENG_4, wakeup()
2332 	 * is supposed to be locked by splhigh(), but the debugger may
2333 	 * be invoked at splhigh().
2334 	 */
2335 	if (debugger == 0)
2336 	    wakeup(&sc->new_scp->smode);
2337 	splx(s);
2338 	DPRINTF(5, ("switch done (new == old)\n"));
2339 	return 0;
2340     }
2341 
2342     /* has controlling process died? */
2343     vt_proc_alive(sc->old_scp);
2344     vt_proc_alive(sc->new_scp);
2345 
2346     /* wait for the controlling process to release the screen, if necessary */
2347     if (signal_vt_rel(sc->old_scp)) {
2348 	splx(s);
2349 	return 0;
2350     }
2351 
2352     /* go set up the new vty screen */
2353     splx(s);
2354     exchange_scr(sc);
2355     s = spltty();
2356 
2357     /* wake up processes waiting for this vty */
2358     if (debugger == 0)
2359 	wakeup(&sc->cur_scp->smode);
2360 
2361     /* wait for the controlling process to acknowledge, if necessary */
2362     if (signal_vt_acq(sc->cur_scp)) {
2363 	splx(s);
2364 	return 0;
2365     }
2366 
2367     sc->switch_in_progress = 0;
2368     if (sc->unit == sc_console_unit)
2369 	cnavailable(sc_consptr,  TRUE);
2370     splx(s);
2371     DPRINTF(5, ("switch done\n"));
2372 
2373     return 0;
2374 }
2375 
2376 static int
2377 do_switch_scr(sc_softc_t *sc, int s)
2378 {
2379     vt_proc_alive(sc->new_scp);
2380 
2381     splx(s);
2382     exchange_scr(sc);
2383     s = spltty();
2384     /* sc->cur_scp == sc->new_scp */
2385     wakeup(&sc->cur_scp->smode);
2386 
2387     /* wait for the controlling process to acknowledge, if necessary */
2388     if (!signal_vt_acq(sc->cur_scp)) {
2389 	sc->switch_in_progress = 0;
2390 	if (sc->unit == sc_console_unit)
2391 	    cnavailable(sc_consptr,  TRUE);
2392     }
2393 
2394     return s;
2395 }
2396 
2397 static int
2398 vt_proc_alive(scr_stat *scp)
2399 {
2400     struct proc *p;
2401 
2402     if (scp->proc) {
2403 	if ((p = pfind(scp->pid)) != NULL)
2404 	    PROC_UNLOCK(p);
2405 	if (scp->proc == p)
2406 	    return TRUE;
2407 	scp->proc = NULL;
2408 	scp->smode.mode = VT_AUTO;
2409 	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2410     }
2411     return FALSE;
2412 }
2413 
2414 static int
2415 signal_vt_rel(scr_stat *scp)
2416 {
2417     if (scp->smode.mode != VT_PROCESS)
2418 	return FALSE;
2419     scp->status |= SWITCH_WAIT_REL;
2420     PROC_LOCK(scp->proc);
2421     psignal(scp->proc, scp->smode.relsig);
2422     PROC_UNLOCK(scp->proc);
2423     DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2424     return TRUE;
2425 }
2426 
2427 static int
2428 signal_vt_acq(scr_stat *scp)
2429 {
2430     if (scp->smode.mode != VT_PROCESS)
2431 	return FALSE;
2432     if (scp->sc->unit == sc_console_unit)
2433 	cnavailable(sc_consptr,  FALSE);
2434     scp->status |= SWITCH_WAIT_ACQ;
2435     PROC_LOCK(scp->proc);
2436     psignal(scp->proc, scp->smode.acqsig);
2437     PROC_UNLOCK(scp->proc);
2438     DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2439     return TRUE;
2440 }
2441 
2442 static int
2443 finish_vt_rel(scr_stat *scp, int release, int *s)
2444 {
2445     if (scp == scp->sc->old_scp && scp->status & SWITCH_WAIT_REL) {
2446 	scp->status &= ~SWITCH_WAIT_REL;
2447 	if (release)
2448 	    *s = do_switch_scr(scp->sc, *s);
2449 	else
2450 	    scp->sc->switch_in_progress = 0;
2451 	return 0;
2452     }
2453     return EINVAL;
2454 }
2455 
2456 static int
2457 finish_vt_acq(scr_stat *scp)
2458 {
2459     if (scp == scp->sc->new_scp && scp->status & SWITCH_WAIT_ACQ) {
2460 	scp->status &= ~SWITCH_WAIT_ACQ;
2461 	scp->sc->switch_in_progress = 0;
2462 	return 0;
2463     }
2464     return EINVAL;
2465 }
2466 
2467 static void
2468 exchange_scr(sc_softc_t *sc)
2469 {
2470     scr_stat *scp;
2471 
2472     /* save the current state of video and keyboard */
2473     sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2474     if (!ISGRAPHSC(sc->old_scp))
2475 	sc_remove_cursor_image(sc->old_scp);
2476     if (sc->old_scp->kbd_mode == K_XLATE)
2477 	save_kbd_state(sc->old_scp);
2478 
2479     /* set up the video for the new screen */
2480     scp = sc->cur_scp = sc->new_scp;
2481 #ifdef PC98
2482     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp) || ISUNKNOWNSC(sc->new_scp))
2483 #else
2484     if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2485 #endif
2486 	set_mode(scp);
2487 #ifndef __sparc64__
2488     else
2489 	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2490 		    (void *)sc->adp->va_window, FALSE);
2491 #endif
2492     scp->status |= MOUSE_HIDDEN;
2493     sc_move_cursor(scp, scp->xpos, scp->ypos);
2494     if (!ISGRAPHSC(scp))
2495 	sc_set_cursor_image(scp);
2496 #ifndef SC_NO_PALETTE_LOADING
2497     if (ISGRAPHSC(sc->old_scp))
2498 	vidd_load_palette(sc->adp, sc->palette);
2499 #endif
2500     sc_set_border(scp, scp->border);
2501 
2502     /* set up the keyboard for the new screen */
2503     if (sc->old_scp->kbd_mode != scp->kbd_mode)
2504 	kbdd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2505     update_kbd_state(scp, scp->status, LOCK_MASK);
2506 
2507     mark_all(scp);
2508 }
2509 
2510 void
2511 sc_puts(scr_stat *scp, u_char *buf, int len)
2512 {
2513     int need_unlock = 0;
2514 
2515 #ifdef DEV_SPLASH
2516     /* make screensaver happy */
2517     if (!sticky_splash && scp == scp->sc->cur_scp && !sc_saver_keyb_only)
2518 	run_scrn_saver = FALSE;
2519 #endif
2520 
2521     if (scp->tsw) {
2522 	if (!kdb_active && !mtx_owned(&scp->scr_lock)) {
2523 		need_unlock = 1;
2524 		mtx_lock_spin(&scp->scr_lock);
2525 	}
2526 	(*scp->tsw->te_puts)(scp, buf, len);
2527 	if (need_unlock)
2528 		mtx_unlock_spin(&scp->scr_lock);
2529     }
2530 
2531     if (scp->sc->delayed_next_scr)
2532 	sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2533 }
2534 
2535 void
2536 sc_draw_cursor_image(scr_stat *scp)
2537 {
2538     /* assert(scp == scp->sc->cur_scp); */
2539     SC_VIDEO_LOCK(scp->sc);
2540     (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2541 			      scp->curs_attr.flags & CONS_BLINK_CURSOR, TRUE,
2542 			      sc_inside_cutmark(scp, scp->cursor_pos));
2543     scp->cursor_oldpos = scp->cursor_pos;
2544     SC_VIDEO_UNLOCK(scp->sc);
2545 }
2546 
2547 void
2548 sc_remove_cursor_image(scr_stat *scp)
2549 {
2550     /* assert(scp == scp->sc->cur_scp); */
2551     SC_VIDEO_LOCK(scp->sc);
2552     (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2553 			      scp->curs_attr.flags & CONS_BLINK_CURSOR, FALSE,
2554 			      sc_inside_cutmark(scp, scp->cursor_oldpos));
2555     SC_VIDEO_UNLOCK(scp->sc);
2556 }
2557 
2558 static void
2559 update_cursor_image(scr_stat *scp)
2560 {
2561     /* assert(scp == scp->sc->cur_scp); */
2562     sc_remove_cursor_image(scp);
2563     sc_set_cursor_image(scp);
2564     sc_draw_cursor_image(scp);
2565 }
2566 
2567 void
2568 sc_set_cursor_image(scr_stat *scp)
2569 {
2570     scp->curs_attr.flags = scp->curr_curs_attr.flags;
2571     if (scp->curs_attr.flags & CONS_HIDDEN_CURSOR) {
2572 	/* hidden cursor is internally represented as zero-height underline */
2573 	scp->curs_attr.flags = CONS_CHAR_CURSOR;
2574 	scp->curs_attr.base = scp->curs_attr.height = 0;
2575     } else if (scp->curs_attr.flags & CONS_CHAR_CURSOR) {
2576 	scp->curs_attr.base = imin(scp->curr_curs_attr.base,
2577 				  scp->font_size - 1);
2578 	scp->curs_attr.height = imin(scp->curr_curs_attr.height,
2579 				    scp->font_size - scp->curs_attr.base);
2580     } else {	/* block cursor */
2581 	scp->curs_attr.base = 0;
2582 	scp->curs_attr.height = scp->font_size;
2583     }
2584 
2585     /* assert(scp == scp->sc->cur_scp); */
2586     SC_VIDEO_LOCK(scp->sc);
2587     (*scp->rndr->set_cursor)(scp, scp->curs_attr.base, scp->curs_attr.height,
2588 			     scp->curs_attr.flags & CONS_BLINK_CURSOR);
2589     SC_VIDEO_UNLOCK(scp->sc);
2590 }
2591 
2592 static void
2593 change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2594 {
2595     if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp))
2596 	sc_remove_cursor_image(scp);
2597 
2598     if (base >= 0)
2599 	scp->curr_curs_attr.base = base;
2600     if (height >= 0)
2601 	scp->curr_curs_attr.height = height;
2602     if (flags & CONS_RESET_CURSOR)
2603 	scp->curr_curs_attr = scp->dflt_curs_attr;
2604     else
2605 	scp->curr_curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2606 
2607     if ((scp == scp->sc->cur_scp) && !ISGRAPHSC(scp)) {
2608 	sc_set_cursor_image(scp);
2609 	sc_draw_cursor_image(scp);
2610     }
2611 }
2612 
2613 void
2614 sc_change_cursor_shape(scr_stat *scp, int flags, int base, int height)
2615 {
2616     sc_softc_t *sc;
2617     struct cdev *dev;
2618     int s;
2619     int i;
2620 
2621     s = spltty();
2622     if ((flags != -1) && (flags & CONS_LOCAL_CURSOR)) {
2623 	/* local (per vty) change */
2624 	change_cursor_shape(scp, flags, base, height);
2625 	splx(s);
2626 	return;
2627     }
2628 
2629     /* global change */
2630     sc = scp->sc;
2631     if (base >= 0)
2632 	sc->curs_attr.base = base;
2633     if (height >= 0)
2634 	sc->curs_attr.height = height;
2635     if (flags != -1) {
2636 	if (flags & CONS_RESET_CURSOR)
2637 	    sc->curs_attr = sc->dflt_curs_attr;
2638 	else
2639 	    sc->curs_attr.flags = flags & CONS_CURSOR_ATTRS;
2640     }
2641 
2642     for (i = sc->first_vty; i < sc->first_vty + sc->vtys; ++i) {
2643 	if ((dev = SC_DEV(sc, i)) == NULL)
2644 	    continue;
2645 	if ((scp = sc_get_stat(dev)) == NULL)
2646 	    continue;
2647 	scp->dflt_curs_attr = sc->curs_attr;
2648 	change_cursor_shape(scp, CONS_RESET_CURSOR, -1, -1);
2649     }
2650     splx(s);
2651 }
2652 
2653 static void
2654 scinit(int unit, int flags)
2655 {
2656 
2657     /*
2658      * When syscons is being initialized as the kernel console, malloc()
2659      * is not yet functional, because various kernel structures has not been
2660      * fully initialized yet.  Therefore, we need to declare the following
2661      * static buffers for the console.  This is less than ideal,
2662      * but is necessry evil for the time being.  XXX
2663      */
2664 #ifdef PC98
2665     static u_short sc_buffer[ROW*COL*2];/* XXX */
2666 #else
2667     static u_short sc_buffer[ROW*COL];	/* XXX */
2668 #endif
2669 #ifndef SC_NO_FONT_LOADING
2670     static u_char font_8[256*8];
2671     static u_char font_14[256*14];
2672     static u_char font_16[256*16];
2673 #endif
2674 
2675     sc_softc_t *sc;
2676     scr_stat *scp;
2677     video_adapter_t *adp;
2678     int col;
2679     int row;
2680     int i;
2681 
2682     /* one time initialization */
2683     if (init_done == COLD)
2684 	sc_get_bios_values(&bios_value);
2685     init_done = WARM;
2686 
2687     /*
2688      * Allocate resources.  Even if we are being called for the second
2689      * time, we must allocate them again, because they might have
2690      * disappeared...
2691      */
2692     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2693     if ((sc->flags & SC_INIT_DONE) == 0)
2694 	SC_VIDEO_LOCKINIT(sc);
2695 
2696     adp = NULL;
2697     if (sc->adapter >= 0) {
2698 	vid_release(sc->adp, (void *)&sc->adapter);
2699 	adp = sc->adp;
2700 	sc->adp = NULL;
2701     }
2702     if (sc->keyboard >= 0) {
2703 	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2704 	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2705 	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2706 	if (sc->kbd != NULL) {
2707 	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2708 		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2709 	}
2710 	sc->kbd = NULL;
2711     }
2712     sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2713     sc->adp = vid_get_adapter(sc->adapter);
2714     /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2715 
2716     sc->keyboard = sc_allocate_keyboard(sc, unit);
2717     DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2718 
2719     sc->kbd = kbd_get_keyboard(sc->keyboard);
2720     if (sc->kbd != NULL) {
2721 	DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2722 		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2723     }
2724 
2725     if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2726 
2727 	sc->initial_mode = sc->adp->va_initial_mode;
2728 
2729 #ifndef SC_NO_FONT_LOADING
2730 	if (flags & SC_KERNEL_CONSOLE) {
2731 	    sc->font_8 = font_8;
2732 	    sc->font_14 = font_14;
2733 	    sc->font_16 = font_16;
2734 	} else if (sc->font_8 == NULL) {
2735 	    /* assert(sc_malloc) */
2736 	    sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2737 	    sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2738 	    sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2739 	}
2740 #endif
2741 
2742 	/* extract the hardware cursor location and hide the cursor for now */
2743 	vidd_read_hw_cursor(sc->adp, &col, &row);
2744 	vidd_set_hw_cursor(sc->adp, -1, -1);
2745 
2746 	/* set up the first console */
2747 	sc->first_vty = unit*MAXCONS;
2748 	sc->vtys = MAXCONS;		/* XXX: should be configurable */
2749 	if (flags & SC_KERNEL_CONSOLE) {
2750 	    /*
2751 	     * Set up devs structure but don't use it yet, calling make_dev()
2752 	     * might panic kernel.  Wait for sc_attach_unit() to actually
2753 	     * create the devices.
2754 	     */
2755 	    sc->dev = main_devs;
2756 	    scp = &main_console;
2757 	    init_scp(sc, sc->first_vty, scp);
2758 	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2759 			(void *)sc_buffer, FALSE);
2760 	    if (sc_init_emulator(scp, SC_DFLT_TERM))
2761 		sc_init_emulator(scp, "*");
2762 	    (*scp->tsw->te_default_attr)(scp,
2763 					 kernel_default.std_color,
2764 					 kernel_default.rev_color);
2765 	} else {
2766 	    /* assert(sc_malloc) */
2767 	    sc->dev = malloc(sizeof(struct cdev *)*sc->vtys, M_DEVBUF, M_WAITOK|M_ZERO);
2768 	    sc->dev[0] = make_dev(&sc_cdevsw, unit * MAXCONS,
2769 	        UID_ROOT, GID_WHEEL, 0600, "ttyv%r", unit * MAXCONS);
2770 	    sc_alloc_tty(sc->dev[0]);
2771 	    scp = alloc_scp(sc, sc->first_vty);
2772 	    SC_STAT(sc->dev[0]) = scp;
2773 	}
2774 	sc->cur_scp = scp;
2775 
2776 #ifndef __sparc64__
2777 	/* copy screen to temporary buffer */
2778 	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2779 		    (void *)scp->sc->adp->va_window, FALSE);
2780 	if (ISTEXTSC(scp))
2781 	    sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2782 #endif
2783 
2784 	/* move cursors to the initial positions */
2785 	if (col >= scp->xsize)
2786 	    col = 0;
2787 	if (row >= scp->ysize)
2788 	    row = scp->ysize - 1;
2789 	scp->xpos = col;
2790 	scp->ypos = row;
2791 	scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2792 
2793 	if (bios_value.cursor_end < scp->font_size)
2794 	    sc->dflt_curs_attr.base = scp->font_size -
2795 					  bios_value.cursor_end - 1;
2796 	else
2797 	    sc->dflt_curs_attr.base = 0;
2798 	i = bios_value.cursor_end - bios_value.cursor_start + 1;
2799 	sc->dflt_curs_attr.height = imin(i, scp->font_size);
2800 	sc->dflt_curs_attr.flags = 0;
2801 	sc->curs_attr = sc->dflt_curs_attr;
2802 	scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
2803 
2804 #ifndef SC_NO_SYSMOUSE
2805 	sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2806 #endif
2807 	if (!ISGRAPHSC(scp)) {
2808     	    sc_set_cursor_image(scp);
2809     	    sc_draw_cursor_image(scp);
2810 	}
2811 
2812 	/* save font and palette */
2813 #ifndef SC_NO_FONT_LOADING
2814 	sc->fonts_loaded = 0;
2815 	if (ISFONTAVAIL(sc->adp->va_flags)) {
2816 #ifdef SC_DFLT_FONT
2817 	    bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2818 	    bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2819 	    bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2820 	    sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2821 	    if (scp->font_size < 14) {
2822 		sc_load_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2823 	    } else if (scp->font_size >= 16) {
2824 		sc_load_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2825 	    } else {
2826 		sc_load_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2827 	    }
2828 #else /* !SC_DFLT_FONT */
2829 	    if (scp->font_size < 14) {
2830 		sc_save_font(scp, 0, 8, 8, sc->font_8, 0, 256);
2831 		sc->fonts_loaded = FONT_8;
2832 	    } else if (scp->font_size >= 16) {
2833 		sc_save_font(scp, 0, 16, 8, sc->font_16, 0, 256);
2834 		sc->fonts_loaded = FONT_16;
2835 	    } else {
2836 		sc_save_font(scp, 0, 14, 8, sc->font_14, 0, 256);
2837 		sc->fonts_loaded = FONT_14;
2838 	    }
2839 #endif /* SC_DFLT_FONT */
2840 	    /* FONT KLUDGE: always use the font page #0. XXX */
2841 	    sc_show_font(scp, 0);
2842 	}
2843 #endif /* !SC_NO_FONT_LOADING */
2844 
2845 #ifndef SC_NO_PALETTE_LOADING
2846 	vidd_save_palette(sc->adp, sc->palette);
2847 #endif
2848 
2849 #ifdef DEV_SPLASH
2850 	if (!(sc->flags & SC_SPLASH_SCRN)) {
2851 	    /* we are ready to put up the splash image! */
2852 	    splash_init(sc->adp, scsplash_callback, sc);
2853 	    sc->flags |= SC_SPLASH_SCRN;
2854 	}
2855 #endif
2856     }
2857 
2858     /* the rest is not necessary, if we have done it once */
2859     if (sc->flags & SC_INIT_DONE)
2860 	return;
2861 
2862     /* initialize mapscrn arrays to a one to one map */
2863     for (i = 0; i < sizeof(sc->scr_map); i++)
2864 	sc->scr_map[i] = sc->scr_rmap[i] = i;
2865 #ifdef PC98
2866     sc->scr_map[0x5c] = (u_char)0xfc;	/* for backslash */
2867 #endif
2868 
2869     sc->flags |= SC_INIT_DONE;
2870 }
2871 
2872 static void
2873 scterm(int unit, int flags)
2874 {
2875     sc_softc_t *sc;
2876     scr_stat *scp;
2877 
2878     sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2879     if (sc == NULL)
2880 	return;			/* shouldn't happen */
2881 
2882 #ifdef DEV_SPLASH
2883     /* this console is no longer available for the splash screen */
2884     if (sc->flags & SC_SPLASH_SCRN) {
2885 	splash_term(sc->adp);
2886 	sc->flags &= ~SC_SPLASH_SCRN;
2887     }
2888 #endif
2889 
2890 #if 0 /* XXX */
2891     /* move the hardware cursor to the upper-left corner */
2892     vidd_set_hw_cursor(sc->adp, 0, 0);
2893 #endif
2894 
2895     /* release the keyboard and the video card */
2896     if (sc->keyboard >= 0)
2897 	kbd_release(sc->kbd, &sc->keyboard);
2898     if (sc->adapter >= 0)
2899 	vid_release(sc->adp, &sc->adapter);
2900 
2901     /* stop the terminal emulator, if any */
2902     scp = sc_get_stat(sc->dev[0]);
2903     if (scp->tsw)
2904 	(*scp->tsw->te_term)(scp, &scp->ts);
2905     if (scp->ts != NULL)
2906 	free(scp->ts, M_DEVBUF);
2907     mtx_destroy(&scp->scr_lock);
2908 
2909     /* clear the structure */
2910     if (!(flags & SC_KERNEL_CONSOLE)) {
2911 	/* XXX: We need delete_dev() for this */
2912 	free(sc->dev, M_DEVBUF);
2913 #if 0
2914 	/* XXX: We need a ttyunregister for this */
2915 	free(sc->tty, M_DEVBUF);
2916 #endif
2917 #ifndef SC_NO_FONT_LOADING
2918 	free(sc->font_8, M_DEVBUF);
2919 	free(sc->font_14, M_DEVBUF);
2920 	free(sc->font_16, M_DEVBUF);
2921 #endif
2922 	/* XXX vtb, history */
2923     }
2924     bzero(sc, sizeof(*sc));
2925     sc->keyboard = -1;
2926     sc->adapter = -1;
2927 }
2928 
2929 static void
2930 scshutdown(void *arg, int howto)
2931 {
2932     /* assert(sc_console != NULL) */
2933 
2934     sc_touch_scrn_saver();
2935     if (!cold && sc_console
2936 	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO
2937 	&& sc_console->smode.mode == VT_AUTO)
2938 	sc_switch_scr(sc_console->sc, sc_console->index);
2939     shutdown_in_progress = TRUE;
2940 }
2941 
2942 int
2943 sc_clean_up(scr_stat *scp)
2944 {
2945 #ifdef DEV_SPLASH
2946     int error;
2947 #endif
2948 
2949     if (scp->sc->flags & SC_SCRN_BLANKED) {
2950 	sc_touch_scrn_saver();
2951 #ifdef DEV_SPLASH
2952 	if ((error = wait_scrn_saver_stop(scp->sc)))
2953 	    return error;
2954 #endif
2955     }
2956     scp->status |= MOUSE_HIDDEN;
2957     sc_remove_mouse_image(scp);
2958     sc_remove_cutmarking(scp);
2959     return 0;
2960 }
2961 
2962 void
2963 sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2964 {
2965     sc_vtb_t new;
2966     sc_vtb_t old;
2967 
2968     old = scp->vtb;
2969     sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2970     if (!discard && (old.vtb_flags & VTB_VALID)) {
2971 	/* retain the current cursor position and buffer contants */
2972 	scp->cursor_oldpos = scp->cursor_pos;
2973 	/*
2974 	 * This works only if the old buffer has the same size as or larger
2975 	 * than the new one. XXX
2976 	 */
2977 	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2978 	scp->vtb = new;
2979     } else {
2980 	scp->vtb = new;
2981 	sc_vtb_destroy(&old);
2982     }
2983 
2984 #ifndef SC_NO_SYSMOUSE
2985     /* move the mouse cursor at the center of the screen */
2986     sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2987 #endif
2988 }
2989 
2990 static scr_stat
2991 *alloc_scp(sc_softc_t *sc, int vty)
2992 {
2993     scr_stat *scp;
2994 
2995     /* assert(sc_malloc) */
2996 
2997     scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2998     init_scp(sc, vty, scp);
2999 
3000     sc_alloc_scr_buffer(scp, TRUE, TRUE);
3001     if (sc_init_emulator(scp, SC_DFLT_TERM))
3002 	sc_init_emulator(scp, "*");
3003 
3004 #ifndef SC_NO_CUTPASTE
3005     sc_alloc_cut_buffer(scp, TRUE);
3006 #endif
3007 
3008 #ifndef SC_NO_HISTORY
3009     sc_alloc_history_buffer(scp, 0, 0, TRUE);
3010 #endif
3011 
3012     return scp;
3013 }
3014 
3015 static void
3016 init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
3017 {
3018     video_info_t info;
3019 
3020     bzero(scp, sizeof(*scp));
3021 
3022     scp->index = vty;
3023     scp->sc = sc;
3024     scp->status = 0;
3025     scp->mode = sc->initial_mode;
3026     vidd_get_info(sc->adp, scp->mode, &info);
3027     if (info.vi_flags & V_INFO_GRAPHICS) {
3028 	scp->status |= GRAPHICS_MODE;
3029 	scp->xpixel = info.vi_width;
3030 	scp->ypixel = info.vi_height;
3031 	scp->xsize = info.vi_width/info.vi_cwidth;
3032 	scp->ysize = info.vi_height/info.vi_cheight;
3033 	scp->font_size = 0;
3034 	scp->font = NULL;
3035     } else {
3036 	scp->xsize = info.vi_width;
3037 	scp->ysize = info.vi_height;
3038 	scp->xpixel = scp->xsize*info.vi_cwidth;
3039 	scp->ypixel = scp->ysize*info.vi_cheight;
3040 	scp->font_size = info.vi_cheight;
3041 	scp->font_width = info.vi_cwidth;
3042 	if (info.vi_cheight < 14) {
3043 #ifndef SC_NO_FONT_LOADING
3044 	    scp->font = sc->font_8;
3045 #else
3046 	    scp->font = NULL;
3047 #endif
3048 	} else if (info.vi_cheight >= 16) {
3049 #ifndef SC_NO_FONT_LOADING
3050 	    scp->font = sc->font_16;
3051 #else
3052 	    scp->font = NULL;
3053 #endif
3054 	} else {
3055 #ifndef SC_NO_FONT_LOADING
3056 	    scp->font = sc->font_14;
3057 #else
3058 	    scp->font = NULL;
3059 #endif
3060 	}
3061     }
3062     sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
3063 #ifndef __sparc64__
3064     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
3065 #endif
3066     scp->xoff = scp->yoff = 0;
3067     scp->xpos = scp->ypos = 0;
3068     scp->start = scp->xsize * scp->ysize - 1;
3069     scp->end = 0;
3070     scp->tsw = NULL;
3071     scp->ts = NULL;
3072     scp->rndr = NULL;
3073     scp->border = (SC_NORM_ATTR >> 4) & 0x0f;
3074     scp->curr_curs_attr = scp->dflt_curs_attr = sc->curs_attr;
3075     scp->mouse_cut_start = scp->xsize*scp->ysize;
3076     scp->mouse_cut_end = -1;
3077     scp->mouse_signal = 0;
3078     scp->mouse_pid = 0;
3079     scp->mouse_proc = NULL;
3080     scp->kbd_mode = K_XLATE;
3081     scp->bell_pitch = bios_value.bell_pitch;
3082     scp->bell_duration = BELL_DURATION;
3083     scp->status |= (bios_value.shift_state & NLKED);
3084     scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
3085     scp->pid = 0;
3086     scp->proc = NULL;
3087     scp->smode.mode = VT_AUTO;
3088     scp->history = NULL;
3089     scp->history_pos = 0;
3090     scp->history_size = 0;
3091 
3092     mtx_init(&scp->scr_lock, "scrlock", NULL, MTX_SPIN);
3093 }
3094 
3095 int
3096 sc_init_emulator(scr_stat *scp, char *name)
3097 {
3098     sc_term_sw_t *sw;
3099     sc_rndr_sw_t *rndr;
3100     void *p;
3101     int error;
3102 
3103     if (name == NULL)	/* if no name is given, use the current emulator */
3104 	sw = scp->tsw;
3105     else		/* ...otherwise find the named emulator */
3106 	sw = sc_term_match(name);
3107     if (sw == NULL)
3108 	return EINVAL;
3109 
3110     rndr = NULL;
3111     if (strcmp(sw->te_renderer, "*") != 0) {
3112 	rndr = sc_render_match(scp, sw->te_renderer,
3113 			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3114     }
3115     if (rndr == NULL) {
3116 	rndr = sc_render_match(scp, scp->sc->adp->va_name,
3117 			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
3118 	if (rndr == NULL)
3119 	    return ENODEV;
3120     }
3121 
3122     if (sw == scp->tsw) {
3123 	error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
3124 	scp->rndr = rndr;
3125 	scp->rndr->init(scp);
3126 	sc_clear_screen(scp);
3127 	/* assert(error == 0); */
3128 	return error;
3129     }
3130 
3131     if (sc_malloc && (sw->te_size > 0))
3132 	p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
3133     else
3134 	p = NULL;
3135     error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
3136     if (error)
3137 	return error;
3138 
3139     if (scp->tsw)
3140 	(*scp->tsw->te_term)(scp, &scp->ts);
3141     if (scp->ts != NULL)
3142 	free(scp->ts, M_DEVBUF);
3143     scp->tsw = sw;
3144     scp->ts = p;
3145     scp->rndr = rndr;
3146     scp->rndr->init(scp);
3147 
3148     /* XXX */
3149     (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
3150     sc_clear_screen(scp);
3151 
3152     return 0;
3153 }
3154 
3155 /*
3156  * scgetc(flags) - get character from keyboard.
3157  * If flags & SCGETC_CN, then avoid harmful side effects.
3158  * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
3159  * return NOKEY if there is nothing there.
3160  */
3161 static u_int
3162 scgetc(sc_softc_t *sc, u_int flags)
3163 {
3164     scr_stat *scp;
3165 #ifndef SC_NO_HISTORY
3166     struct tty *tp;
3167 #endif
3168     u_int c;
3169     int this_scr;
3170     int f;
3171     int i;
3172 
3173     if (sc->kbd == NULL)
3174 	return NOKEY;
3175 
3176 next_code:
3177 #if 1
3178     /* I don't like this, but... XXX */
3179     if (flags & SCGETC_CN)
3180 	sccnupdate(sc->cur_scp);
3181 #endif
3182     scp = sc->cur_scp;
3183     /* first see if there is something in the keyboard port */
3184     for (;;) {
3185 	c = kbdd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
3186 	if (c == ERRKEY) {
3187 	    if (!(flags & SCGETC_CN))
3188 		sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3189 	} else if (c == NOKEY)
3190 	    return c;
3191 	else
3192 	    break;
3193     }
3194 
3195     /* make screensaver happy */
3196     if (!(c & RELKEY))
3197 	sc_touch_scrn_saver();
3198 
3199     if (!(flags & SCGETC_CN))
3200 	random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD);
3201 
3202     if (scp->kbd_mode != K_XLATE)
3203 	return KEYCHAR(c);
3204 
3205     /* if scroll-lock pressed allow history browsing */
3206     if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
3207 
3208 	scp->status &= ~CURSOR_ENABLED;
3209 	sc_remove_cursor_image(scp);
3210 
3211 #ifndef SC_NO_HISTORY
3212 	if (!(scp->status & BUFFER_SAVED)) {
3213 	    scp->status |= BUFFER_SAVED;
3214 	    sc_hist_save(scp);
3215 	}
3216 	switch (c) {
3217 	/* FIXME: key codes */
3218 	case SPCLKEY | FKEY | F(49):  /* home key */
3219 	    sc_remove_cutmarking(scp);
3220 	    sc_hist_home(scp);
3221 	    goto next_code;
3222 
3223 	case SPCLKEY | FKEY | F(57):  /* end key */
3224 	    sc_remove_cutmarking(scp);
3225 	    sc_hist_end(scp);
3226 	    goto next_code;
3227 
3228 	case SPCLKEY | FKEY | F(50):  /* up arrow key */
3229 	    sc_remove_cutmarking(scp);
3230 	    if (sc_hist_up_line(scp))
3231 		if (!(flags & SCGETC_CN))
3232 		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3233 	    goto next_code;
3234 
3235 	case SPCLKEY | FKEY | F(58):  /* down arrow key */
3236 	    sc_remove_cutmarking(scp);
3237 	    if (sc_hist_down_line(scp))
3238 		if (!(flags & SCGETC_CN))
3239 		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3240 	    goto next_code;
3241 
3242 	case SPCLKEY | FKEY | F(51):  /* page up key */
3243 	    sc_remove_cutmarking(scp);
3244 	    for (i=0; i<scp->ysize; i++)
3245 	    if (sc_hist_up_line(scp)) {
3246 		if (!(flags & SCGETC_CN))
3247 		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3248 		break;
3249 	    }
3250 	    goto next_code;
3251 
3252 	case SPCLKEY | FKEY | F(59):  /* page down key */
3253 	    sc_remove_cutmarking(scp);
3254 	    for (i=0; i<scp->ysize; i++)
3255 	    if (sc_hist_down_line(scp)) {
3256 		if (!(flags & SCGETC_CN))
3257 		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3258 		break;
3259 	    }
3260 	    goto next_code;
3261 	}
3262 #endif /* SC_NO_HISTORY */
3263     }
3264 
3265     /*
3266      * Process and consume special keys here.  Return a plain char code
3267      * or a char code with the META flag or a function key code.
3268      */
3269     if (c & RELKEY) {
3270 	/* key released */
3271 	/* goto next_code */
3272     } else {
3273 	/* key pressed */
3274 	if (c & SPCLKEY) {
3275 	    c &= ~SPCLKEY;
3276 	    switch (KEYCHAR(c)) {
3277 	    /* LOCKING KEYS */
3278 	    case NLK: case CLK: case ALK:
3279 		break;
3280 	    case SLK:
3281 		kbdd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3282 		if (f & SLKED) {
3283 		    scp->status |= SLKED;
3284 		} else {
3285 		    if (scp->status & SLKED) {
3286 			scp->status &= ~SLKED;
3287 #ifndef SC_NO_HISTORY
3288 			if (scp->status & BUFFER_SAVED) {
3289 			    if (!sc_hist_restore(scp))
3290 				sc_remove_cutmarking(scp);
3291 			    scp->status &= ~BUFFER_SAVED;
3292 			    scp->status |= CURSOR_ENABLED;
3293 			    sc_draw_cursor_image(scp);
3294 			}
3295 			tp = VIRTUAL_TTY(sc, scp->index);
3296 			if (ISTTYOPEN(tp))
3297 			    scstart(tp);
3298 #endif
3299 		    }
3300 		}
3301 		break;
3302 
3303 	    case PASTE:
3304 #ifndef SC_NO_CUTPASTE
3305 		sc_mouse_paste(scp);
3306 #endif
3307 		break;
3308 
3309 	    /* NON-LOCKING KEYS */
3310 	    case NOP:
3311 	    case LSH:  case RSH:  case LCTR: case RCTR:
3312 	    case LALT: case RALT: case ASH:  case META:
3313 		break;
3314 
3315 	    case BTAB:
3316 		if (!(sc->flags & SC_SCRN_BLANKED))
3317 		    return c;
3318 		break;
3319 
3320 	    case SPSC:
3321 #ifdef DEV_SPLASH
3322 		/* force activatation/deactivation of the screen saver */
3323 		if (!(sc->flags & SC_SCRN_BLANKED)) {
3324 		    run_scrn_saver = TRUE;
3325 		    sc->scrn_time_stamp -= scrn_blank_time;
3326 		}
3327 		if (cold) {
3328 		    /*
3329 		     * While devices are being probed, the screen saver need
3330 		     * to be invoked explictly. XXX
3331 		     */
3332 		    if (sc->flags & SC_SCRN_BLANKED) {
3333 			scsplash_stick(FALSE);
3334 			stop_scrn_saver(sc, current_saver);
3335 		    } else {
3336 			if (!ISGRAPHSC(scp)) {
3337 			    scsplash_stick(TRUE);
3338 			    (*current_saver)(sc, TRUE);
3339 			}
3340 		    }
3341 		}
3342 #endif /* DEV_SPLASH */
3343 		break;
3344 
3345 	    case RBT:
3346 #ifndef SC_DISABLE_REBOOT
3347 		if (enable_reboot)
3348 			shutdown_nice(0);
3349 #endif
3350 		break;
3351 
3352 	    case HALT:
3353 #ifndef SC_DISABLE_REBOOT
3354 		if (enable_reboot)
3355 			shutdown_nice(RB_HALT);
3356 #endif
3357 		break;
3358 
3359 	    case PDWN:
3360 #ifndef SC_DISABLE_REBOOT
3361 		if (enable_reboot)
3362 			shutdown_nice(RB_HALT|RB_POWEROFF);
3363 #endif
3364 		break;
3365 
3366 	    case SUSP:
3367 		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
3368 		break;
3369 	    case STBY:
3370 		power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
3371 		break;
3372 
3373 	    case DBG:
3374 #ifndef SC_DISABLE_KDBKEY
3375 		if (enable_kdbkey)
3376 			kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
3377 #endif
3378 		break;
3379 
3380 	    case PNC:
3381 		if (enable_panic_key)
3382 			panic("Forced by the panic key");
3383 		break;
3384 
3385 	    case NEXT:
3386 		this_scr = scp->index;
3387 		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3388 			sc->first_vty + i != this_scr;
3389 			i = (i + 1)%sc->vtys) {
3390 		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3391 		    if (ISTTYOPEN(tp)) {
3392 			sc_switch_scr(scp->sc, sc->first_vty + i);
3393 			break;
3394 		    }
3395 		}
3396 		break;
3397 
3398 	    case PREV:
3399 		this_scr = scp->index;
3400 		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3401 			sc->first_vty + i != this_scr;
3402 			i = (i + sc->vtys - 1)%sc->vtys) {
3403 		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3404 		    if (ISTTYOPEN(tp)) {
3405 			sc_switch_scr(scp->sc, sc->first_vty + i);
3406 			break;
3407 		    }
3408 		}
3409 		break;
3410 
3411 	    default:
3412 		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3413 		    sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3414 		    break;
3415 		}
3416 		/* assert(c & FKEY) */
3417 		if (!(sc->flags & SC_SCRN_BLANKED))
3418 		    return c;
3419 		break;
3420 	    }
3421 	    /* goto next_code */
3422 	} else {
3423 	    /* regular keys (maybe MKEY is set) */
3424 	    if (!(sc->flags & SC_SCRN_BLANKED))
3425 		return c;
3426 	}
3427     }
3428 
3429     goto next_code;
3430 }
3431 
3432 static int
3433 scmmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
3434 {
3435     scr_stat *scp;
3436 
3437     scp = sc_get_stat(dev);
3438     if (scp != scp->sc->cur_scp)
3439 	return -1;
3440     return vidd_mmap(scp->sc->adp, offset, paddr, nprot);
3441 }
3442 
3443 static int
3444 save_kbd_state(scr_stat *scp)
3445 {
3446     int state;
3447     int error;
3448 
3449     error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3450     if (error == ENOIOCTL)
3451 	error = ENODEV;
3452     if (error == 0) {
3453 	scp->status &= ~LOCK_MASK;
3454 	scp->status |= state;
3455     }
3456     return error;
3457 }
3458 
3459 static int
3460 update_kbd_state(scr_stat *scp, int new_bits, int mask)
3461 {
3462     int state;
3463     int error;
3464 
3465     if (mask != LOCK_MASK) {
3466 	error = kbdd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3467 	if (error == ENOIOCTL)
3468 	    error = ENODEV;
3469 	if (error)
3470 	    return error;
3471 	state &= ~mask;
3472 	state |= new_bits & mask;
3473     } else {
3474 	state = new_bits & LOCK_MASK;
3475     }
3476     error = kbdd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3477     if (error == ENOIOCTL)
3478 	error = ENODEV;
3479     return error;
3480 }
3481 
3482 static int
3483 update_kbd_leds(scr_stat *scp, int which)
3484 {
3485     int error;
3486 
3487     which &= LOCK_MASK;
3488     error = kbdd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3489     if (error == ENOIOCTL)
3490 	error = ENODEV;
3491     return error;
3492 }
3493 
3494 int
3495 set_mode(scr_stat *scp)
3496 {
3497     video_info_t info;
3498 
3499     /* reject unsupported mode */
3500     if (vidd_get_info(scp->sc->adp, scp->mode, &info))
3501 	return 1;
3502 
3503     /* if this vty is not currently showing, do nothing */
3504     if (scp != scp->sc->cur_scp)
3505 	return 0;
3506 
3507     /* setup video hardware for the given mode */
3508     vidd_set_mode(scp->sc->adp, scp->mode);
3509     scp->rndr->init(scp);
3510 #ifndef __sparc64__
3511     sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3512 		(void *)scp->sc->adp->va_window, FALSE);
3513 #endif
3514 
3515 #ifndef SC_NO_FONT_LOADING
3516     /* load appropriate font */
3517     if (!(scp->status & GRAPHICS_MODE)) {
3518 	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3519 	    if (scp->font_size < 14) {
3520 		if (scp->sc->fonts_loaded & FONT_8)
3521 		    sc_load_font(scp, 0, 8, 8, scp->sc->font_8, 0, 256);
3522 	    } else if (scp->font_size >= 16) {
3523 		if (scp->sc->fonts_loaded & FONT_16)
3524 		    sc_load_font(scp, 0, 16, 8, scp->sc->font_16, 0, 256);
3525 	    } else {
3526 		if (scp->sc->fonts_loaded & FONT_14)
3527 		    sc_load_font(scp, 0, 14, 8, scp->sc->font_14, 0, 256);
3528 	    }
3529 	    /*
3530 	     * FONT KLUDGE:
3531 	     * This is an interim kludge to display correct font.
3532 	     * Always use the font page #0 on the video plane 2.
3533 	     * Somehow we cannot show the font in other font pages on
3534 	     * some video cards... XXX
3535 	     */
3536 	    sc_show_font(scp, 0);
3537 	}
3538 	mark_all(scp);
3539     }
3540 #endif /* !SC_NO_FONT_LOADING */
3541 
3542     sc_set_border(scp, scp->border);
3543     sc_set_cursor_image(scp);
3544 
3545     return 0;
3546 }
3547 
3548 void
3549 sc_set_border(scr_stat *scp, int color)
3550 {
3551     SC_VIDEO_LOCK(scp->sc);
3552     (*scp->rndr->draw_border)(scp, color);
3553     SC_VIDEO_UNLOCK(scp->sc);
3554 }
3555 
3556 #ifndef SC_NO_FONT_LOADING
3557 void
3558 sc_load_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3559 	     int base, int count)
3560 {
3561     sc_softc_t *sc;
3562 
3563     sc = scp->sc;
3564     sc->font_loading_in_progress = TRUE;
3565     vidd_load_font(sc->adp, page, size, width, buf, base, count);
3566     sc->font_loading_in_progress = FALSE;
3567 }
3568 
3569 void
3570 sc_save_font(scr_stat *scp, int page, int size, int width, u_char *buf,
3571 	     int base, int count)
3572 {
3573     sc_softc_t *sc;
3574 
3575     sc = scp->sc;
3576     sc->font_loading_in_progress = TRUE;
3577     vidd_save_font(sc->adp, page, size, width, buf, base, count);
3578     sc->font_loading_in_progress = FALSE;
3579 }
3580 
3581 void
3582 sc_show_font(scr_stat *scp, int page)
3583 {
3584     vidd_show_font(scp->sc->adp, page);
3585 }
3586 #endif /* !SC_NO_FONT_LOADING */
3587 
3588 void
3589 sc_paste(scr_stat *scp, u_char *p, int count)
3590 {
3591     struct tty *tp;
3592     u_char *rmap;
3593 
3594     tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3595     if (!ISTTYOPEN(tp))
3596 	return;
3597     rmap = scp->sc->scr_rmap;
3598     for (; count > 0; --count)
3599 	ttyld_rint(tp, rmap[*p++]);
3600 }
3601 
3602 void
3603 sc_bell(scr_stat *scp, int pitch, int duration)
3604 {
3605     if (cold || shutdown_in_progress || !enable_bell)
3606 	return;
3607 
3608     if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3609 	return;
3610 
3611     if (scp->sc->flags & SC_VISUAL_BELL) {
3612 	if (scp->sc->blink_in_progress)
3613 	    return;
3614 	scp->sc->blink_in_progress = 3;
3615 	if (scp != scp->sc->cur_scp)
3616 	    scp->sc->blink_in_progress += 2;
3617 	blink_screen(scp->sc->cur_scp);
3618     } else if (duration != 0 && pitch != 0) {
3619 	if (scp != scp->sc->cur_scp)
3620 	    pitch *= 2;
3621 	sysbeep(pitch, duration);
3622     }
3623 }
3624 
3625 static void
3626 blink_screen(void *arg)
3627 {
3628     scr_stat *scp = arg;
3629     struct tty *tp;
3630 
3631     if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3632 	scp->sc->blink_in_progress = 0;
3633     	mark_all(scp);
3634 	tp = VIRTUAL_TTY(scp->sc, scp->index);
3635 	if (ISTTYOPEN(tp))
3636 	    scstart(tp);
3637 	if (scp->sc->delayed_next_scr)
3638 	    sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3639     }
3640     else {
3641 	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3642 			   scp->sc->blink_in_progress & 1);
3643 	scp->sc->blink_in_progress--;
3644 	timeout(blink_screen, scp, hz / 10);
3645     }
3646 }
3647 
3648 /*
3649  * Until sc_attach_unit() gets called no dev structures will be available
3650  * to store the per-screen current status.  This is the case when the
3651  * kernel is initially booting and needs access to its console.  During
3652  * this early phase of booting the console's current status is kept in
3653  * one statically defined scr_stat structure, and any pointers to the
3654  * dev structures will be NULL.
3655  */
3656 
3657 static scr_stat *
3658 sc_get_stat(struct cdev *devptr)
3659 {
3660 	if (devptr == NULL)
3661 		return (&main_console);
3662 	return (SC_STAT(devptr));
3663 }
3664 
3665 /*
3666  * Allocate active keyboard. Try to allocate "kbdmux" keyboard first, and,
3667  * if found, add all non-busy keyboards to "kbdmux". Otherwise look for
3668  * any keyboard.
3669  */
3670 
3671 static int
3672 sc_allocate_keyboard(sc_softc_t *sc, int unit)
3673 {
3674 	int		 idx0, idx;
3675 	keyboard_t	*k0, *k;
3676 	keyboard_info_t	 ki;
3677 
3678 	idx0 = kbd_allocate("kbdmux", -1, (void *)&sc->keyboard, sckbdevent, sc);
3679 	if (idx0 != -1) {
3680 		k0 = kbd_get_keyboard(idx0);
3681 
3682 		for (idx = kbd_find_keyboard2("*", -1, 0);
3683 		     idx != -1;
3684 		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
3685 			k = kbd_get_keyboard(idx);
3686 
3687 			if (idx == idx0 || KBD_IS_BUSY(k))
3688 				continue;
3689 
3690 			bzero(&ki, sizeof(ki));
3691 			strcpy(ki.kb_name, k->kb_name);
3692 			ki.kb_unit = k->kb_unit;
3693 
3694 			kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
3695 		}
3696 	} else
3697 		idx0 = kbd_allocate("*", unit, (void *)&sc->keyboard, sckbdevent, sc);
3698 
3699 	return (idx0);
3700 }
3701