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