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