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