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