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