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