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