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