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