syscons.c revision 74118
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 74118 2001-03-11 22:51:05Z ache $
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		(scp->cursor_pos != scp->cursor_oldpos) &&
1729		(and_region(&s, &e, scp->cursor_pos, scp->cursor_pos)
1730		 || and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos)))) {
1731	    sc_remove_mouse_image(scp);
1732	    if (scp->end >= scp->xsize*scp->ysize)
1733		scp->end = scp->xsize*scp->ysize - 1;
1734	}
1735    }
1736#endif /* !SC_NO_CUTPASTE */
1737
1738#if 1
1739    /* debug: XXX */
1740    if (scp->end >= scp->xsize*scp->ysize) {
1741	printf("scrn_update(): scp->end %d > size_of_screen!!\n", scp->end);
1742	scp->end = scp->xsize*scp->ysize - 1;
1743    }
1744    if (scp->start < 0) {
1745	printf("scrn_update(): scp->start %d < 0\n", scp->start);
1746	scp->start = 0;
1747    }
1748#endif
1749
1750    /* update screen image */
1751    if (scp->start <= scp->end)  {
1752	if (scp->mouse_cut_end >= 0) {
1753	    /* there is a marked region for cut & paste */
1754	    if (scp->mouse_cut_start <= scp->mouse_cut_end) {
1755		start = scp->mouse_cut_start;
1756		end = scp->mouse_cut_end;
1757	    } else {
1758		start = scp->mouse_cut_end;
1759		end = scp->mouse_cut_start - 1;
1760	    }
1761	    s = start;
1762	    e = end;
1763	    /* does the cut-mark region overlap with the update region? */
1764	    if (and_region(&s, &e, scp->start, scp->end)) {
1765		(*scp->rndr->draw)(scp, s, e - s + 1, TRUE);
1766		s = 0;
1767		e = start - 1;
1768		if (and_region(&s, &e, scp->start, scp->end))
1769		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1770		s = end + 1;
1771		e = scp->xsize*scp->ysize - 1;
1772		if (and_region(&s, &e, scp->start, scp->end))
1773		    (*scp->rndr->draw)(scp, s, e - s + 1, FALSE);
1774	    } else {
1775		(*scp->rndr->draw)(scp, scp->start,
1776				   scp->end - scp->start + 1, FALSE);
1777	    }
1778	} else {
1779	    (*scp->rndr->draw)(scp, scp->start,
1780			       scp->end - scp->start + 1, FALSE);
1781	}
1782    }
1783
1784    /* we are not to show the cursor and the mouse pointer... */
1785    if (!show_cursor) {
1786        scp->end = 0;
1787        scp->start = scp->xsize*scp->ysize - 1;
1788	--scp->sc->videoio_in_progress;
1789	return;
1790    }
1791
1792    /* update cursor image */
1793    if (scp->status & CURSOR_ENABLED) {
1794	s = scp->start;
1795	e = scp->end;
1796        /* did cursor move since last time ? */
1797        if (scp->cursor_pos != scp->cursor_oldpos) {
1798            /* do we need to remove old cursor image ? */
1799            if (!and_region(&s, &e, scp->cursor_oldpos, scp->cursor_oldpos))
1800                sc_remove_cursor_image(scp);
1801            sc_draw_cursor_image(scp);
1802        } else {
1803            if (and_region(&s, &e, scp->cursor_pos, scp->cursor_pos))
1804		/* cursor didn't move, but has been overwritten */
1805		sc_draw_cursor_image(scp);
1806	    else if (scp->sc->flags & SC_BLINK_CURSOR)
1807		/* if it's a blinking cursor, update it */
1808		(*scp->rndr->blink_cursor)(scp, scp->cursor_pos,
1809					   sc_inside_cutmark(scp,
1810					       scp->cursor_pos));
1811        }
1812    }
1813
1814#ifndef SC_NO_CUTPASTE
1815    /* update "pseudo" mouse pointer image */
1816    if (scp->sc->flags & SC_MOUSE_ENABLED) {
1817	if (!(scp->status & (MOUSE_VISIBLE | MOUSE_HIDDEN))) {
1818	    scp->status &= ~MOUSE_MOVED;
1819	    sc_draw_mouse_image(scp);
1820	}
1821    }
1822#endif /* SC_NO_CUTPASTE */
1823
1824    scp->end = 0;
1825    scp->start = scp->xsize*scp->ysize - 1;
1826
1827    --scp->sc->videoio_in_progress;
1828}
1829
1830#if NSPLASH > 0
1831static int
1832scsplash_callback(int event, void *arg)
1833{
1834    sc_softc_t *sc;
1835    int error;
1836
1837    sc = (sc_softc_t *)arg;
1838
1839    switch (event) {
1840    case SPLASH_INIT:
1841	if (add_scrn_saver(scsplash_saver) == 0) {
1842	    sc->flags &= ~SC_SAVER_FAILED;
1843	    run_scrn_saver = TRUE;
1844	    if (cold && !(boothowto & (RB_VERBOSE | RB_CONFIG))) {
1845		scsplash_stick(TRUE);
1846		(*current_saver)(sc, TRUE);
1847	    }
1848	}
1849	return 0;
1850
1851    case SPLASH_TERM:
1852	if (current_saver == scsplash_saver) {
1853	    scsplash_stick(FALSE);
1854	    error = remove_scrn_saver(scsplash_saver);
1855	    if (error)
1856		return error;
1857	}
1858	return 0;
1859
1860    default:
1861	return EINVAL;
1862    }
1863}
1864
1865static void
1866scsplash_saver(sc_softc_t *sc, int show)
1867{
1868    static int busy = FALSE;
1869    scr_stat *scp;
1870
1871    if (busy)
1872	return;
1873    busy = TRUE;
1874
1875    scp = sc->cur_scp;
1876    if (show) {
1877	if (!(sc->flags & SC_SAVER_FAILED)) {
1878	    if (!(sc->flags & SC_SCRN_BLANKED))
1879		set_scrn_saver_mode(scp, -1, NULL, 0);
1880	    switch (splash(sc->adp, TRUE)) {
1881	    case 0:		/* succeeded */
1882		break;
1883	    case EAGAIN:	/* try later */
1884		restore_scrn_saver_mode(scp, FALSE);
1885		sc_touch_scrn_saver();		/* XXX */
1886		break;
1887	    default:
1888		sc->flags |= SC_SAVER_FAILED;
1889		scsplash_stick(FALSE);
1890		restore_scrn_saver_mode(scp, TRUE);
1891		printf("scsplash_saver(): failed to put up the image\n");
1892		break;
1893	    }
1894	}
1895    } else if (!sticky_splash) {
1896	if ((sc->flags & SC_SCRN_BLANKED) && (splash(sc->adp, FALSE) == 0))
1897	    restore_scrn_saver_mode(scp, TRUE);
1898    }
1899    busy = FALSE;
1900}
1901
1902static int
1903add_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1904{
1905#if 0
1906    int error;
1907
1908    if (current_saver != none_saver) {
1909	error = remove_scrn_saver(current_saver);
1910	if (error)
1911	    return error;
1912    }
1913#endif
1914    if (current_saver != none_saver)
1915	return EBUSY;
1916
1917    run_scrn_saver = FALSE;
1918    saver_mode = CONS_LKM_SAVER;
1919    current_saver = this_saver;
1920    return 0;
1921}
1922
1923static int
1924remove_scrn_saver(void (*this_saver)(sc_softc_t *, int))
1925{
1926    if (current_saver != this_saver)
1927	return EINVAL;
1928
1929#if 0
1930    /*
1931     * In order to prevent `current_saver' from being called by
1932     * the timeout routine `scrn_timer()' while we manipulate
1933     * the saver list, we shall set `current_saver' to `none_saver'
1934     * before stopping the current saver, rather than blocking by `splXX()'.
1935     */
1936    current_saver = none_saver;
1937    if (scrn_blanked)
1938        stop_scrn_saver(this_saver);
1939#endif
1940
1941    /* unblank all blanked screens */
1942    wait_scrn_saver_stop(NULL);
1943    if (scrn_blanked)
1944	return EBUSY;
1945
1946    current_saver = none_saver;
1947    return 0;
1948}
1949
1950static int
1951set_scrn_saver_mode(scr_stat *scp, int mode, u_char *pal, int border)
1952{
1953    int s;
1954
1955    /* assert(scp == scp->sc->cur_scp) */
1956    s = spltty();
1957    if (!ISGRAPHSC(scp))
1958	sc_remove_cursor_image(scp);
1959    scp->splash_save_mode = scp->mode;
1960    scp->splash_save_status = scp->status & (GRAPHICS_MODE | PIXEL_MODE);
1961    scp->status &= ~(GRAPHICS_MODE | PIXEL_MODE);
1962    scp->status |= (UNKNOWN_MODE | SAVER_RUNNING);
1963    scp->sc->flags |= SC_SCRN_BLANKED;
1964    ++scrn_blanked;
1965    splx(s);
1966    if (mode < 0)
1967	return 0;
1968    scp->mode = mode;
1969    if (set_mode(scp) == 0) {
1970	if (scp->sc->adp->va_info.vi_flags & V_INFO_GRAPHICS)
1971	    scp->status |= GRAPHICS_MODE;
1972#ifndef SC_NO_PALETTE_LOADING
1973	if (pal != NULL)
1974	    load_palette(scp->sc->adp, pal);
1975#endif
1976	sc_set_border(scp, border);
1977	return 0;
1978    } else {
1979	s = spltty();
1980	scp->mode = scp->splash_save_mode;
1981	scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
1982	scp->status |= scp->splash_save_status;
1983	splx(s);
1984	return 1;
1985    }
1986}
1987
1988static int
1989restore_scrn_saver_mode(scr_stat *scp, int changemode)
1990{
1991    int mode;
1992    int status;
1993    int s;
1994
1995    /* assert(scp == scp->sc->cur_scp) */
1996    s = spltty();
1997    mode = scp->mode;
1998    status = scp->status;
1999    scp->mode = scp->splash_save_mode;
2000    scp->status &= ~(UNKNOWN_MODE | SAVER_RUNNING);
2001    scp->status |= scp->splash_save_status;
2002    scp->sc->flags &= ~SC_SCRN_BLANKED;
2003    if (!changemode) {
2004	if (!ISGRAPHSC(scp))
2005	    sc_draw_cursor_image(scp);
2006	--scrn_blanked;
2007	splx(s);
2008	return 0;
2009    }
2010    if (set_mode(scp) == 0) {
2011#ifndef SC_NO_PALETTE_LOADING
2012	load_palette(scp->sc->adp, scp->sc->palette);
2013#endif
2014	--scrn_blanked;
2015	splx(s);
2016	return 0;
2017    } else {
2018	scp->mode = mode;
2019	scp->status = status;
2020	splx(s);
2021	return 1;
2022    }
2023}
2024
2025static void
2026stop_scrn_saver(sc_softc_t *sc, void (*saver)(sc_softc_t *, int))
2027{
2028    (*saver)(sc, FALSE);
2029    run_scrn_saver = FALSE;
2030    /* the screen saver may have chosen not to stop after all... */
2031    if (sc->flags & SC_SCRN_BLANKED)
2032	return;
2033
2034    mark_all(sc->cur_scp);
2035    if (sc->delayed_next_scr)
2036	sc_switch_scr(sc, sc->delayed_next_scr - 1);
2037    wakeup((caddr_t)&scrn_blanked);
2038}
2039
2040static int
2041wait_scrn_saver_stop(sc_softc_t *sc)
2042{
2043    int error = 0;
2044
2045    while (scrn_blanked > 0) {
2046	run_scrn_saver = FALSE;
2047	if (sc && !(sc->flags & SC_SCRN_BLANKED)) {
2048	    error = 0;
2049	    break;
2050	}
2051	error = tsleep((caddr_t)&scrn_blanked, PZERO | PCATCH, "scrsav", 0);
2052	if ((error != 0) && (error != ERESTART))
2053	    break;
2054    }
2055    run_scrn_saver = FALSE;
2056    return error;
2057}
2058#endif /* NSPLASH */
2059
2060void
2061sc_touch_scrn_saver(void)
2062{
2063    scsplash_stick(FALSE);
2064    run_scrn_saver = FALSE;
2065}
2066
2067int
2068sc_switch_scr(sc_softc_t *sc, u_int next_scr)
2069{
2070    struct tty *tp;
2071    int s;
2072
2073    DPRINTF(5, ("sc0: sc_switch_scr() %d ", next_scr + 1));
2074
2075    /* delay switch if the screen is blanked or being updated */
2076    if ((sc->flags & SC_SCRN_BLANKED) || sc->write_in_progress
2077	|| sc->blink_in_progress || sc->videoio_in_progress) {
2078	sc->delayed_next_scr = next_scr + 1;
2079	sc_touch_scrn_saver();
2080	DPRINTF(5, ("switch delayed\n"));
2081	return 0;
2082    }
2083
2084    s = spltty();
2085
2086    /* we are in the middle of the vty switching process... */
2087    if (sc->switch_in_progress
2088	&& (sc->cur_scp->smode.mode == VT_PROCESS)
2089	&& sc->cur_scp->proc) {
2090	if (sc->cur_scp->proc != pfind(sc->cur_scp->pid)) {
2091	    /*
2092	     * The controlling process has died!!.  Do some clean up.
2093	     * NOTE:`cur_scp->proc' and `cur_scp->smode.mode'
2094	     * are not reset here yet; they will be cleared later.
2095	     */
2096	    DPRINTF(5, ("cur_scp controlling process %d died, ",
2097	       sc->cur_scp->pid));
2098	    if (sc->cur_scp->status & SWITCH_WAIT_REL) {
2099		/*
2100		 * Force the previous switch to finish, but return now
2101		 * with error.
2102		 */
2103		DPRINTF(5, ("reset WAIT_REL, "));
2104		sc->cur_scp->status &= ~SWITCH_WAIT_REL;
2105		s = do_switch_scr(sc, s);
2106		splx(s);
2107		DPRINTF(5, ("finishing previous switch\n"));
2108		return EINVAL;
2109	    } else if (sc->cur_scp->status & SWITCH_WAIT_ACQ) {
2110		/* let's assume screen switch has been completed. */
2111		DPRINTF(5, ("reset WAIT_ACQ, "));
2112		sc->cur_scp->status &= ~SWITCH_WAIT_ACQ;
2113		sc->switch_in_progress = 0;
2114	    } else {
2115		/*
2116	 	 * We are in between screen release and acquisition, and
2117		 * reached here via scgetc() or scrn_timer() which has
2118		 * interrupted exchange_scr(). Don't do anything stupid.
2119		 */
2120		DPRINTF(5, ("waiting nothing, "));
2121	    }
2122	} else {
2123	    /*
2124	     * The controlling process is alive, but not responding...
2125	     * It is either buggy or it may be just taking time.
2126	     * The following code is a gross kludge to cope with this
2127	     * problem for which there is no clean solution. XXX
2128	     */
2129	    if (sc->cur_scp->status & SWITCH_WAIT_REL) {
2130		switch (sc->switch_in_progress++) {
2131		case 1:
2132		    break;
2133		case 2:
2134		    DPRINTF(5, ("sending relsig again, "));
2135		    signal_vt_rel(sc->cur_scp);
2136		    break;
2137		case 3:
2138		    break;
2139		case 4:
2140		default:
2141		    /*
2142		     * Act as if the controlling program returned
2143		     * VT_FALSE.
2144		     */
2145		    DPRINTF(5, ("force reset WAIT_REL, "));
2146		    sc->cur_scp->status &= ~SWITCH_WAIT_REL;
2147		    sc->switch_in_progress = 0;
2148		    splx(s);
2149		    DPRINTF(5, ("act as if VT_FALSE was seen\n"));
2150		    return EINVAL;
2151		}
2152	    } else if (sc->cur_scp->status & SWITCH_WAIT_ACQ) {
2153		switch (sc->switch_in_progress++) {
2154		case 1:
2155		    break;
2156		case 2:
2157		    DPRINTF(5, ("sending acqsig again, "));
2158		    signal_vt_acq(sc->cur_scp);
2159		    break;
2160		case 3:
2161		    break;
2162		case 4:
2163		default:
2164		     /* clear the flag and finish the previous switch */
2165		    DPRINTF(5, ("force reset WAIT_ACQ, "));
2166		    sc->cur_scp->status &= ~SWITCH_WAIT_ACQ;
2167		    sc->switch_in_progress = 0;
2168		    break;
2169		}
2170	    }
2171	}
2172    }
2173
2174    /*
2175     * Return error if an invalid argument is given, or vty switch
2176     * is still in progress.
2177     */
2178    if ((next_scr < sc->first_vty) || (next_scr >= sc->first_vty + sc->vtys)
2179	|| sc->switch_in_progress) {
2180	splx(s);
2181	sc_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2182	DPRINTF(5, ("error 1\n"));
2183	return EINVAL;
2184    }
2185
2186    /*
2187     * Don't allow switching away from the graphics mode vty
2188     * if the switch mode is VT_AUTO, unless the next vty is the same
2189     * as the current or the current vty has been closed (but showing).
2190     */
2191    tp = VIRTUAL_TTY(sc, sc->cur_scp->index);
2192    if ((sc->cur_scp->index != next_scr)
2193	&& (tp->t_state & TS_ISOPEN)
2194	&& (sc->cur_scp->smode.mode == VT_AUTO)
2195	&& ISGRAPHSC(sc->cur_scp)) {
2196	splx(s);
2197	sc_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2198	DPRINTF(5, ("error, graphics mode\n"));
2199	return EINVAL;
2200    }
2201
2202    /*
2203     * Is the wanted vty open? Don't allow switching to a closed vty.
2204     * Note that we always allow the user to switch to the kernel
2205     * console even if it is closed.
2206     */
2207    if ((sc_console == NULL) || (next_scr != sc_console->index)) {
2208	tp = VIRTUAL_TTY(sc, next_scr);
2209	if ((tp == NULL) || !(tp->t_state & TS_ISOPEN)) {
2210	    splx(s);
2211	    sc_bell(sc->cur_scp, bios_value.bell_pitch, BELL_DURATION);
2212	    DPRINTF(5, ("error 2, requested vty isn't open!\n"));
2213	    return EINVAL;
2214	}
2215    }
2216
2217    /* this is the start of vty switching process... */
2218    ++sc->switch_in_progress;
2219    sc->delayed_next_scr = 0;
2220    sc->old_scp = sc->cur_scp;
2221    sc->new_scp = SC_STAT(SC_DEV(sc, next_scr));
2222    if (sc->new_scp == sc->old_scp) {
2223	sc->switch_in_progress = 0;
2224	wakeup((caddr_t)&sc->new_scp->smode);
2225	splx(s);
2226	DPRINTF(5, ("switch done (new == old)\n"));
2227	return 0;
2228    }
2229
2230    /* has controlling process died? */
2231    vt_proc_alive(sc->old_scp);
2232    vt_proc_alive(sc->new_scp);
2233
2234    /* wait for the controlling process to release the screen, if necessary */
2235    if (signal_vt_rel(sc->old_scp)) {
2236	splx(s);
2237	return 0;
2238    }
2239
2240    /* go set up the new vty screen */
2241    splx(s);
2242    exchange_scr(sc);
2243    s = spltty();
2244
2245    /* wake up processes waiting for this vty */
2246    wakeup((caddr_t)&sc->cur_scp->smode);
2247
2248    /* wait for the controlling process to acknowledge, if necessary */
2249    if (signal_vt_acq(sc->cur_scp)) {
2250	splx(s);
2251	return 0;
2252    }
2253
2254    sc->switch_in_progress = 0;
2255    if (sc->unit == sc_console_unit)
2256	cons_unavail = FALSE;
2257    splx(s);
2258    DPRINTF(5, ("switch done\n"));
2259
2260    return 0;
2261}
2262
2263static int
2264do_switch_scr(sc_softc_t *sc, int s)
2265{
2266    vt_proc_alive(sc->new_scp);
2267
2268    splx(s);
2269    exchange_scr(sc);
2270    s = spltty();
2271    /* sc->cur_scp == sc->new_scp */
2272    wakeup((caddr_t)&sc->cur_scp->smode);
2273
2274    /* wait for the controlling process to acknowledge, if necessary */
2275    if (!signal_vt_acq(sc->cur_scp)) {
2276	sc->switch_in_progress = 0;
2277	if (sc->unit == sc_console_unit)
2278	    cons_unavail = FALSE;
2279    }
2280
2281    return s;
2282}
2283
2284static int
2285vt_proc_alive(scr_stat *scp)
2286{
2287    if (scp->proc) {
2288	if (scp->proc == pfind(scp->pid))
2289	    return TRUE;
2290	scp->proc = NULL;
2291	scp->smode.mode = VT_AUTO;
2292	DPRINTF(5, ("vt controlling process %d died\n", scp->pid));
2293    }
2294    return FALSE;
2295}
2296
2297static int
2298signal_vt_rel(scr_stat *scp)
2299{
2300    if (scp->smode.mode != VT_PROCESS)
2301	return FALSE;
2302    scp->status |= SWITCH_WAIT_REL;
2303    PROC_LOCK(scp->proc);
2304    psignal(scp->proc, scp->smode.relsig);
2305    PROC_UNLOCK(scp->proc);
2306    DPRINTF(5, ("sending relsig to %d\n", scp->pid));
2307    return TRUE;
2308}
2309
2310static int
2311signal_vt_acq(scr_stat *scp)
2312{
2313    if (scp->smode.mode != VT_PROCESS)
2314	return FALSE;
2315    if (scp->sc->unit == sc_console_unit)
2316	cons_unavail = TRUE;
2317    scp->status |= SWITCH_WAIT_ACQ;
2318    PROC_LOCK(scp->proc);
2319    psignal(scp->proc, scp->smode.acqsig);
2320    PROC_UNLOCK(scp->proc);
2321    DPRINTF(5, ("sending acqsig to %d\n", scp->pid));
2322    return TRUE;
2323}
2324
2325static void
2326exchange_scr(sc_softc_t *sc)
2327{
2328    scr_stat *scp;
2329
2330    /* save the current state of video and keyboard */
2331    sc_move_cursor(sc->old_scp, sc->old_scp->xpos, sc->old_scp->ypos);
2332    if (!ISGRAPHSC(sc->old_scp))
2333	sc_remove_cursor_image(sc->old_scp);
2334    if (sc->old_scp->kbd_mode == K_XLATE)
2335	save_kbd_state(sc->old_scp);
2336
2337    /* set up the video for the new screen */
2338    scp = sc->cur_scp = sc->new_scp;
2339    if (sc->old_scp->mode != scp->mode || ISUNKNOWNSC(sc->old_scp))
2340	set_mode(scp);
2341    else
2342	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2343		    (void *)sc->adp->va_window, FALSE);
2344    scp->status |= MOUSE_HIDDEN;
2345    sc_move_cursor(scp, scp->xpos, scp->ypos);
2346    if (!ISGRAPHSC(scp))
2347	sc_set_cursor_image(scp);
2348#ifndef SC_NO_PALETTE_LOADING
2349    if (ISGRAPHSC(sc->old_scp))
2350	load_palette(sc->adp, sc->palette);
2351#endif
2352    sc_set_border(scp, scp->border);
2353
2354    /* set up the keyboard for the new screen */
2355    if (sc->old_scp->kbd_mode != scp->kbd_mode)
2356	kbd_ioctl(sc->kbd, KDSKBMODE, (caddr_t)&scp->kbd_mode);
2357    update_kbd_state(scp, scp->status, LOCK_MASK);
2358
2359    mark_all(scp);
2360}
2361
2362void
2363sc_puts(scr_stat *scp, u_char *buf, int len)
2364{
2365#if NSPLASH > 0
2366    /* make screensaver happy */
2367    if (!sticky_splash && scp == scp->sc->cur_scp)
2368	run_scrn_saver = FALSE;
2369#endif
2370
2371    if (scp->tsw)
2372	(*scp->tsw->te_puts)(scp, buf, len);
2373
2374    if (scp->sc->delayed_next_scr)
2375	sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
2376}
2377
2378void
2379sc_draw_cursor_image(scr_stat *scp)
2380{
2381    /* assert(scp == scp->sc->cur_scp); */
2382    ++scp->sc->videoio_in_progress;
2383    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos,
2384			      scp->sc->flags & SC_BLINK_CURSOR, TRUE,
2385			      sc_inside_cutmark(scp, scp->cursor_pos));
2386    scp->cursor_oldpos = scp->cursor_pos;
2387    --scp->sc->videoio_in_progress;
2388}
2389
2390void
2391sc_remove_cursor_image(scr_stat *scp)
2392{
2393    /* assert(scp == scp->sc->cur_scp); */
2394    ++scp->sc->videoio_in_progress;
2395    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos,
2396			      scp->sc->flags & SC_BLINK_CURSOR, FALSE,
2397			      sc_inside_cutmark(scp, scp->cursor_oldpos));
2398    --scp->sc->videoio_in_progress;
2399}
2400
2401static void
2402update_cursor_image(scr_stat *scp)
2403{
2404    int blink;
2405
2406    if (scp->sc->flags & SC_CHAR_CURSOR) {
2407	scp->cursor_base = imax(0, scp->sc->cursor_base);
2408	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2409    } else {
2410	scp->cursor_base = 0;
2411	scp->cursor_height = scp->font_size;
2412    }
2413    blink = scp->sc->flags & SC_BLINK_CURSOR;
2414
2415    /* assert(scp == scp->sc->cur_scp); */
2416    ++scp->sc->videoio_in_progress;
2417    (*scp->rndr->draw_cursor)(scp, scp->cursor_oldpos, blink, FALSE,
2418			      sc_inside_cutmark(scp, scp->cursor_pos));
2419    (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height, blink);
2420    (*scp->rndr->draw_cursor)(scp, scp->cursor_pos, blink, TRUE,
2421			      sc_inside_cutmark(scp, scp->cursor_pos));
2422    --scp->sc->videoio_in_progress;
2423}
2424
2425void
2426sc_set_cursor_image(scr_stat *scp)
2427{
2428    if (scp->sc->flags & SC_CHAR_CURSOR) {
2429	scp->cursor_base = imax(0, scp->sc->cursor_base);
2430	scp->cursor_height = imin(scp->sc->cursor_height, scp->font_size);
2431    } else {
2432	scp->cursor_base = 0;
2433	scp->cursor_height = scp->font_size;
2434    }
2435
2436    /* assert(scp == scp->sc->cur_scp); */
2437    ++scp->sc->videoio_in_progress;
2438    (*scp->rndr->set_cursor)(scp, scp->cursor_base, scp->cursor_height,
2439			     scp->sc->flags & SC_BLINK_CURSOR);
2440    --scp->sc->videoio_in_progress;
2441}
2442
2443static void
2444scinit(int unit, int flags)
2445{
2446    /*
2447     * When syscons is being initialized as the kernel console, malloc()
2448     * is not yet functional, because various kernel structures has not been
2449     * fully initialized yet.  Therefore, we need to declare the following
2450     * static buffers for the console.  This is less than ideal,
2451     * but is necessry evil for the time being.  XXX
2452     */
2453    static scr_stat main_console;
2454    static dev_t main_devs[MAXCONS];
2455    static struct tty main_tty;
2456    static u_short sc_buffer[ROW*COL];	/* XXX */
2457#ifndef SC_NO_FONT_LOADING
2458    static u_char font_8[256*8];
2459    static u_char font_14[256*14];
2460    static u_char font_16[256*16];
2461#endif
2462
2463    sc_softc_t *sc;
2464    scr_stat *scp;
2465    video_adapter_t *adp;
2466    int col;
2467    int row;
2468    int i;
2469
2470    /* one time initialization */
2471    if (init_done == COLD)
2472	sc_get_bios_values(&bios_value);
2473    init_done = WARM;
2474
2475    /*
2476     * Allocate resources.  Even if we are being called for the second
2477     * time, we must allocate them again, because they might have
2478     * disappeared...
2479     */
2480    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2481    adp = NULL;
2482    if (sc->adapter >= 0) {
2483	vid_release(sc->adp, (void *)&sc->adapter);
2484	adp = sc->adp;
2485	sc->adp = NULL;
2486    }
2487    if (sc->keyboard >= 0) {
2488	DPRINTF(5, ("sc%d: releasing kbd%d\n", unit, sc->keyboard));
2489	i = kbd_release(sc->kbd, (void *)&sc->keyboard);
2490	DPRINTF(5, ("sc%d: kbd_release returned %d\n", unit, i));
2491	if (sc->kbd != NULL) {
2492	    DPRINTF(5, ("sc%d: kbd != NULL!, index:%d, unit:%d, flags:0x%x\n",
2493		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2494	}
2495	sc->kbd = NULL;
2496    }
2497    sc->adapter = vid_allocate("*", unit, (void *)&sc->adapter);
2498    sc->adp = vid_get_adapter(sc->adapter);
2499    /* assert((sc->adapter >= 0) && (sc->adp != NULL)) */
2500    sc->keyboard = kbd_allocate("*", unit, (void *)&sc->keyboard,
2501				sckbdevent, sc);
2502    DPRINTF(1, ("sc%d: keyboard %d\n", unit, sc->keyboard));
2503    sc->kbd = kbd_get_keyboard(sc->keyboard);
2504    if (sc->kbd != NULL) {
2505	DPRINTF(1, ("sc%d: kbd index:%d, unit:%d, flags:0x%x\n",
2506		unit, sc->kbd->kb_index, sc->kbd->kb_unit, sc->kbd->kb_flags));
2507    }
2508
2509    if (!(sc->flags & SC_INIT_DONE) || (adp != sc->adp)) {
2510
2511	sc->initial_mode = sc->adp->va_initial_mode;
2512
2513#ifndef SC_NO_FONT_LOADING
2514	if (flags & SC_KERNEL_CONSOLE) {
2515	    sc->font_8 = font_8;
2516	    sc->font_14 = font_14;
2517	    sc->font_16 = font_16;
2518	} else if (sc->font_8 == NULL) {
2519	    /* assert(sc_malloc) */
2520	    sc->font_8 = malloc(sizeof(font_8), M_DEVBUF, M_WAITOK);
2521	    sc->font_14 = malloc(sizeof(font_14), M_DEVBUF, M_WAITOK);
2522	    sc->font_16 = malloc(sizeof(font_16), M_DEVBUF, M_WAITOK);
2523	}
2524#endif
2525
2526	/* extract the hardware cursor location and hide the cursor for now */
2527	(*vidsw[sc->adapter]->read_hw_cursor)(sc->adp, &col, &row);
2528	(*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, -1, -1);
2529
2530	/* set up the first console */
2531	sc->first_vty = unit*MAXCONS;
2532	sc->vtys = MAXCONS;		/* XXX: should be configurable */
2533	if (flags & SC_KERNEL_CONSOLE) {
2534	    sc->dev = main_devs;
2535	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
2536	    sc->dev[0]->si_tty = &main_tty;
2537	    ttyregister(&main_tty);
2538	    scp = &main_console;
2539	    init_scp(sc, sc->first_vty, scp);
2540	    sc_vtb_init(&scp->vtb, VTB_MEMORY, scp->xsize, scp->ysize,
2541			(void *)sc_buffer, FALSE);
2542	    if (sc_init_emulator(scp, SC_DFLT_TERM))
2543		sc_init_emulator(scp, "*");
2544	    (*scp->tsw->te_default_attr)(scp,
2545					 kernel_default.std_color,
2546					 kernel_default.rev_color);
2547	} else {
2548	    /* assert(sc_malloc) */
2549	    sc->dev = malloc(sizeof(dev_t)*sc->vtys, M_DEVBUF, M_WAITOK|M_ZERO);
2550	    sc->dev[0] = makedev(CDEV_MAJOR, unit*MAXCONS);
2551	    sc->dev[0]->si_tty = ttymalloc(sc->dev[0]->si_tty);
2552	    scp = alloc_scp(sc, sc->first_vty);
2553	}
2554	SC_STAT(sc->dev[0]) = scp;
2555	sc->cur_scp = scp;
2556
2557	/* copy screen to temporary buffer */
2558	sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
2559		    (void *)scp->sc->adp->va_window, FALSE);
2560	if (ISTEXTSC(scp))
2561	    sc_vtb_copy(&scp->scr, 0, &scp->vtb, 0, scp->xsize*scp->ysize);
2562
2563	/* move cursors to the initial positions */
2564	if (col >= scp->xsize)
2565	    col = 0;
2566	if (row >= scp->ysize)
2567	    row = scp->ysize - 1;
2568	scp->xpos = col;
2569	scp->ypos = row;
2570	scp->cursor_pos = scp->cursor_oldpos = row*scp->xsize + col;
2571	if (bios_value.cursor_end < scp->font_size)
2572	    sc->cursor_base = scp->font_size - bios_value.cursor_end - 1;
2573	else
2574	    sc->cursor_base = 0;
2575	i = bios_value.cursor_end - bios_value.cursor_start + 1;
2576	sc->cursor_height = imin(i, scp->font_size);
2577#ifndef SC_NO_SYSMOUSE
2578	sc_mouse_move(scp, scp->xpixel/2, scp->ypixel/2);
2579#endif
2580	if (!ISGRAPHSC(scp)) {
2581    	    sc_set_cursor_image(scp);
2582    	    sc_draw_cursor_image(scp);
2583	}
2584
2585	/* save font and palette */
2586#ifndef SC_NO_FONT_LOADING
2587	sc->fonts_loaded = 0;
2588	if (ISFONTAVAIL(sc->adp->va_flags)) {
2589#ifdef SC_DFLT_FONT
2590	    bcopy(dflt_font_8, sc->font_8, sizeof(dflt_font_8));
2591	    bcopy(dflt_font_14, sc->font_14, sizeof(dflt_font_14));
2592	    bcopy(dflt_font_16, sc->font_16, sizeof(dflt_font_16));
2593	    sc->fonts_loaded = FONT_16 | FONT_14 | FONT_8;
2594	    if (scp->font_size < 14) {
2595		sc_load_font(scp, 0, 8, sc->font_8, 0, 256);
2596	    } else if (scp->font_size >= 16) {
2597		sc_load_font(scp, 0, 16, sc->font_16, 0, 256);
2598	    } else {
2599		sc_load_font(scp, 0, 14, sc->font_14, 0, 256);
2600	    }
2601#else /* !SC_DFLT_FONT */
2602	    if (scp->font_size < 14) {
2603		sc_save_font(scp, 0, 8, sc->font_8, 0, 256);
2604		sc->fonts_loaded = FONT_8;
2605	    } else if (scp->font_size >= 16) {
2606		sc_save_font(scp, 0, 16, sc->font_16, 0, 256);
2607		sc->fonts_loaded = FONT_16;
2608	    } else {
2609		sc_save_font(scp, 0, 14, sc->font_14, 0, 256);
2610		sc->fonts_loaded = FONT_14;
2611	    }
2612#endif /* SC_DFLT_FONT */
2613	    /* FONT KLUDGE: always use the font page #0. XXX */
2614	    sc_show_font(scp, 0);
2615	}
2616#endif /* !SC_NO_FONT_LOADING */
2617
2618#ifndef SC_NO_PALETTE_LOADING
2619	save_palette(sc->adp, sc->palette);
2620#endif
2621
2622#if NSPLASH > 0
2623	if (!(sc->flags & SC_SPLASH_SCRN) && (flags & SC_KERNEL_CONSOLE)) {
2624	    /* we are ready to put up the splash image! */
2625	    splash_init(sc->adp, scsplash_callback, sc);
2626	    sc->flags |= SC_SPLASH_SCRN;
2627	}
2628#endif /* NSPLASH */
2629    }
2630
2631    /* the rest is not necessary, if we have done it once */
2632    if (sc->flags & SC_INIT_DONE)
2633	return;
2634
2635    /* initialize mapscrn arrays to a one to one map */
2636    for (i = 0; i < sizeof(sc->scr_map); i++)
2637	sc->scr_map[i] = sc->scr_rmap[i] = i;
2638
2639    sc->flags |= SC_INIT_DONE;
2640}
2641
2642#if __i386__
2643static void
2644scterm(int unit, int flags)
2645{
2646    sc_softc_t *sc;
2647    scr_stat *scp;
2648
2649    sc = sc_get_softc(unit, flags & SC_KERNEL_CONSOLE);
2650    if (sc == NULL)
2651	return;			/* shouldn't happen */
2652
2653#if NSPLASH > 0
2654    /* this console is no longer available for the splash screen */
2655    if (sc->flags & SC_SPLASH_SCRN) {
2656	splash_term(sc->adp);
2657	sc->flags &= ~SC_SPLASH_SCRN;
2658    }
2659#endif /* NSPLASH */
2660
2661#if 0 /* XXX */
2662    /* move the hardware cursor to the upper-left corner */
2663    (*vidsw[sc->adapter]->set_hw_cursor)(sc->adp, 0, 0);
2664#endif
2665
2666    /* release the keyboard and the video card */
2667    if (sc->keyboard >= 0)
2668	kbd_release(sc->kbd, &sc->keyboard);
2669    if (sc->adapter >= 0)
2670	vid_release(sc->adp, &sc->adapter);
2671
2672    /* stop the terminal emulator, if any */
2673    scp = SC_STAT(sc->dev[0]);
2674    if (scp->tsw)
2675	(*scp->tsw->te_term)(scp, &scp->ts);
2676    if (scp->ts != NULL)
2677	free(scp->ts, M_DEVBUF);
2678
2679    /* clear the structure */
2680    if (!(flags & SC_KERNEL_CONSOLE)) {
2681	/* XXX: We need delete_dev() for this */
2682	free(sc->dev, M_DEVBUF);
2683#if 0
2684	/* XXX: We need a ttyunregister for this */
2685	free(sc->tty, M_DEVBUF);
2686#endif
2687#ifndef SC_NO_FONT_LOADING
2688	free(sc->font_8, M_DEVBUF);
2689	free(sc->font_14, M_DEVBUF);
2690	free(sc->font_16, M_DEVBUF);
2691#endif
2692	/* XXX vtb, history */
2693    }
2694    bzero(sc, sizeof(*sc));
2695    sc->keyboard = -1;
2696    sc->adapter = -1;
2697}
2698#endif
2699
2700static void
2701scshutdown(void *arg, int howto)
2702{
2703    /* assert(sc_console != NULL) */
2704
2705    sc_touch_scrn_saver();
2706    if (!cold && sc_console
2707	&& sc_console->sc->cur_scp->smode.mode == VT_AUTO
2708	&& sc_console->smode.mode == VT_AUTO)
2709	sc_switch_scr(sc_console->sc, sc_console->index);
2710    shutdown_in_progress = TRUE;
2711}
2712
2713int
2714sc_clean_up(scr_stat *scp)
2715{
2716#if NSPLASH > 0
2717    int error;
2718#endif /* NSPLASH */
2719
2720    sc_touch_scrn_saver();
2721#if NSPLASH > 0
2722    if ((error = wait_scrn_saver_stop(scp->sc)))
2723	return error;
2724#endif /* NSPLASH */
2725    scp->status |= MOUSE_HIDDEN;
2726    sc_remove_mouse_image(scp);
2727    sc_remove_cutmarking(scp);
2728    return 0;
2729}
2730
2731void
2732sc_alloc_scr_buffer(scr_stat *scp, int wait, int discard)
2733{
2734    sc_vtb_t new;
2735    sc_vtb_t old;
2736
2737    old = scp->vtb;
2738    sc_vtb_init(&new, VTB_MEMORY, scp->xsize, scp->ysize, NULL, wait);
2739    if (!discard && (old.vtb_flags & VTB_VALID)) {
2740	/* retain the current cursor position and buffer contants */
2741	scp->cursor_oldpos = scp->cursor_pos;
2742	/*
2743	 * This works only if the old buffer has the same size as or larger
2744	 * than the new one. XXX
2745	 */
2746	sc_vtb_copy(&old, 0, &new, 0, scp->xsize*scp->ysize);
2747	scp->vtb = new;
2748    } else {
2749	scp->vtb = new;
2750	sc_vtb_destroy(&old);
2751    }
2752
2753#ifndef SC_NO_SYSMOUSE
2754    /* move the mouse cursor at the center of the screen */
2755    sc_mouse_move(scp, scp->xpixel / 2, scp->ypixel / 2);
2756#endif
2757}
2758
2759static scr_stat
2760*alloc_scp(sc_softc_t *sc, int vty)
2761{
2762    scr_stat *scp;
2763
2764    /* assert(sc_malloc) */
2765
2766    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2767    init_scp(sc, vty, scp);
2768
2769    sc_alloc_scr_buffer(scp, TRUE, TRUE);
2770    if (sc_init_emulator(scp, SC_DFLT_TERM))
2771	sc_init_emulator(scp, "*");
2772
2773#ifndef SC_NO_CUTPASTE
2774    if (ISMOUSEAVAIL(sc->adp->va_flags))
2775	sc_alloc_cut_buffer(scp, TRUE);
2776#endif
2777
2778#ifndef SC_NO_HISTORY
2779    sc_alloc_history_buffer(scp, 0, 0, TRUE);
2780#endif
2781
2782    return scp;
2783}
2784
2785static void
2786init_scp(sc_softc_t *sc, int vty, scr_stat *scp)
2787{
2788    video_info_t info;
2789
2790    bzero(scp, sizeof(*scp));
2791
2792    scp->index = vty;
2793    scp->sc = sc;
2794    scp->status = 0;
2795    scp->mode = sc->initial_mode;
2796    (*vidsw[sc->adapter]->get_info)(sc->adp, scp->mode, &info);
2797    if (info.vi_flags & V_INFO_GRAPHICS) {
2798	scp->status |= GRAPHICS_MODE;
2799	scp->xpixel = info.vi_width;
2800	scp->ypixel = info.vi_height;
2801	scp->xsize = info.vi_width/8;
2802	scp->ysize = info.vi_height/info.vi_cheight;
2803	scp->font_size = 0;
2804	scp->font = NULL;
2805    } else {
2806	scp->xsize = info.vi_width;
2807	scp->ysize = info.vi_height;
2808	scp->xpixel = scp->xsize*8;
2809	scp->ypixel = scp->ysize*info.vi_cheight;
2810	if (info.vi_cheight < 14) {
2811	    scp->font_size = 8;
2812#ifndef SC_NO_FONT_LOADING
2813	    scp->font = sc->font_8;
2814#else
2815	    scp->font = NULL;
2816#endif
2817	} else if (info.vi_cheight >= 16) {
2818	    scp->font_size = 16;
2819#ifndef SC_NO_FONT_LOADING
2820	    scp->font = sc->font_16;
2821#else
2822	    scp->font = NULL;
2823#endif
2824	} else {
2825	    scp->font_size = 14;
2826#ifndef SC_NO_FONT_LOADING
2827	    scp->font = sc->font_14;
2828#else
2829	    scp->font = NULL;
2830#endif
2831	}
2832    }
2833    sc_vtb_init(&scp->vtb, VTB_MEMORY, 0, 0, NULL, FALSE);
2834    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, 0, 0, NULL, FALSE);
2835    scp->xoff = scp->yoff = 0;
2836    scp->xpos = scp->ypos = 0;
2837    scp->start = scp->xsize * scp->ysize - 1;
2838    scp->end = 0;
2839    scp->tsw = NULL;
2840    scp->ts = NULL;
2841    scp->rndr = NULL;
2842    scp->border = BG_BLACK;
2843    scp->cursor_base = sc->cursor_base;
2844    scp->cursor_height = imin(sc->cursor_height, scp->font_size);
2845    scp->mouse_cut_start = scp->xsize*scp->ysize;
2846    scp->mouse_cut_end = -1;
2847    scp->mouse_signal = 0;
2848    scp->mouse_pid = 0;
2849    scp->mouse_proc = NULL;
2850    scp->kbd_mode = K_XLATE;
2851    scp->bell_pitch = bios_value.bell_pitch;
2852    scp->bell_duration = BELL_DURATION;
2853    scp->status |= (bios_value.shift_state & NLKED);
2854    scp->status |= CURSOR_ENABLED | MOUSE_HIDDEN;
2855    scp->pid = 0;
2856    scp->proc = NULL;
2857    scp->smode.mode = VT_AUTO;
2858    scp->history = NULL;
2859    scp->history_pos = 0;
2860    scp->history_size = 0;
2861}
2862
2863int
2864sc_init_emulator(scr_stat *scp, char *name)
2865{
2866    sc_term_sw_t *sw;
2867    sc_rndr_sw_t *rndr;
2868    void *p;
2869    int error;
2870
2871    if (name == NULL)	/* if no name is given, use the current emulator */
2872	sw = scp->tsw;
2873    else		/* ...otherwise find the named emulator */
2874	sw = sc_term_match(name);
2875    if (sw == NULL)
2876	return EINVAL;
2877
2878    rndr = NULL;
2879    if (strcmp(sw->te_renderer, "*") != 0) {
2880	rndr = sc_render_match(scp, sw->te_renderer,
2881			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2882    }
2883    if (rndr == NULL) {
2884	rndr = sc_render_match(scp, scp->sc->adp->va_name,
2885			       scp->status & (GRAPHICS_MODE | PIXEL_MODE));
2886	if (rndr == NULL)
2887	    return ENODEV;
2888    }
2889
2890    if (sw == scp->tsw) {
2891	error = (*sw->te_init)(scp, &scp->ts, SC_TE_WARM_INIT);
2892	scp->rndr = rndr;
2893	sc_clear_screen(scp);
2894	/* assert(error == 0); */
2895	return error;
2896    }
2897
2898    if (sc_malloc && (sw->te_size > 0))
2899	p = malloc(sw->te_size, M_DEVBUF, M_NOWAIT);
2900    else
2901	p = NULL;
2902    error = (*sw->te_init)(scp, &p, SC_TE_COLD_INIT);
2903    if (error)
2904	return error;
2905
2906    if (scp->tsw)
2907	(*scp->tsw->te_term)(scp, &scp->ts);
2908    if (scp->ts != NULL)
2909	free(scp->ts, M_DEVBUF);
2910    scp->tsw = sw;
2911    scp->ts = p;
2912    scp->rndr = rndr;
2913
2914    /* XXX */
2915    (*sw->te_default_attr)(scp, user_default.std_color, user_default.rev_color);
2916    sc_clear_screen(scp);
2917
2918    return 0;
2919}
2920
2921/*
2922 * scgetc(flags) - get character from keyboard.
2923 * If flags & SCGETC_CN, then avoid harmful side effects.
2924 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
2925 * return NOKEY if there is nothing there.
2926 */
2927static u_int
2928scgetc(sc_softc_t *sc, u_int flags)
2929{
2930    scr_stat *scp;
2931#ifndef SC_NO_HISTORY
2932    struct tty *tp;
2933#endif
2934    u_int c;
2935    int this_scr;
2936    int f;
2937    int i;
2938
2939    if (sc->kbd == NULL)
2940	return NOKEY;
2941
2942next_code:
2943#if 1
2944    /* I don't like this, but... XXX */
2945    if (flags & SCGETC_CN)
2946	sccnupdate(sc->cur_scp);
2947#endif
2948    scp = sc->cur_scp;
2949    /* first see if there is something in the keyboard port */
2950    for (;;) {
2951	c = kbd_read_char(sc->kbd, !(flags & SCGETC_NONBLOCK));
2952	if (c == ERRKEY) {
2953	    if (!(flags & SCGETC_CN))
2954		sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
2955	} else if (c == NOKEY)
2956	    return c;
2957	else
2958	    break;
2959    }
2960
2961    /* make screensaver happy */
2962    if (!(c & RELKEY))
2963	sc_touch_scrn_saver();
2964
2965    if (!(flags & SCGETC_CN))
2966	random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD);
2967
2968    if (scp->kbd_mode != K_XLATE)
2969	return KEYCHAR(c);
2970
2971    /* if scroll-lock pressed allow history browsing */
2972    if (!ISGRAPHSC(scp) && scp->history && scp->status & SLKED) {
2973
2974	scp->status &= ~CURSOR_ENABLED;
2975	sc_remove_cursor_image(scp);
2976
2977#ifndef SC_NO_HISTORY
2978	if (!(scp->status & BUFFER_SAVED)) {
2979	    scp->status |= BUFFER_SAVED;
2980	    sc_hist_save(scp);
2981	}
2982	switch (c) {
2983	/* FIXME: key codes */
2984	case SPCLKEY | FKEY | F(49):  /* home key */
2985	    sc_remove_cutmarking(scp);
2986	    sc_hist_home(scp);
2987	    goto next_code;
2988
2989	case SPCLKEY | FKEY | F(57):  /* end key */
2990	    sc_remove_cutmarking(scp);
2991	    sc_hist_end(scp);
2992	    goto next_code;
2993
2994	case SPCLKEY | FKEY | F(50):  /* up arrow key */
2995	    sc_remove_cutmarking(scp);
2996	    if (sc_hist_up_line(scp))
2997		if (!(flags & SCGETC_CN))
2998		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
2999	    goto next_code;
3000
3001	case SPCLKEY | FKEY | F(58):  /* down arrow key */
3002	    sc_remove_cutmarking(scp);
3003	    if (sc_hist_down_line(scp))
3004		if (!(flags & SCGETC_CN))
3005		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3006	    goto next_code;
3007
3008	case SPCLKEY | FKEY | F(51):  /* page up key */
3009	    sc_remove_cutmarking(scp);
3010	    for (i=0; i<scp->ysize; i++)
3011	    if (sc_hist_up_line(scp)) {
3012		if (!(flags & SCGETC_CN))
3013		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3014		break;
3015	    }
3016	    goto next_code;
3017
3018	case SPCLKEY | FKEY | F(59):  /* page down key */
3019	    sc_remove_cutmarking(scp);
3020	    for (i=0; i<scp->ysize; i++)
3021	    if (sc_hist_down_line(scp)) {
3022		if (!(flags & SCGETC_CN))
3023		    sc_bell(scp, bios_value.bell_pitch, BELL_DURATION);
3024		break;
3025	    }
3026	    goto next_code;
3027	}
3028#endif /* SC_NO_HISTORY */
3029    }
3030
3031    /*
3032     * Process and consume special keys here.  Return a plain char code
3033     * or a char code with the META flag or a function key code.
3034     */
3035    if (c & RELKEY) {
3036	/* key released */
3037	/* goto next_code */
3038    } else {
3039	/* key pressed */
3040	if (c & SPCLKEY) {
3041	    c &= ~SPCLKEY;
3042	    switch (KEYCHAR(c)) {
3043	    /* LOCKING KEYS */
3044	    case NLK: case CLK: case ALK:
3045		break;
3046	    case SLK:
3047		kbd_ioctl(sc->kbd, KDGKBSTATE, (caddr_t)&f);
3048		if (f & SLKED) {
3049		    scp->status |= SLKED;
3050		} else {
3051		    if (scp->status & SLKED) {
3052			scp->status &= ~SLKED;
3053#ifndef SC_NO_HISTORY
3054			if (scp->status & BUFFER_SAVED) {
3055			    if (!sc_hist_restore(scp))
3056				sc_remove_cutmarking(scp);
3057			    scp->status &= ~BUFFER_SAVED;
3058			    scp->status |= CURSOR_ENABLED;
3059			    sc_draw_cursor_image(scp);
3060			}
3061			tp = VIRTUAL_TTY(sc, scp->index);
3062			if (tp->t_state & TS_ISOPEN)
3063			    scstart(tp);
3064#endif
3065		    }
3066		}
3067		break;
3068
3069	    case PASTE:
3070#ifndef SC_NO_CUTPASTE
3071	    /* XXX need to set MOUSE_VISIBLE flag 'cause sc_mouse_paste() */
3072	    /* and sc_paste() will not operate without it. */
3073		i = scp->status;
3074		scp->status |= MOUSE_VISIBLE;
3075		sc_mouse_paste(scp);
3076		scp->status = i;
3077#endif
3078		break;
3079
3080	    /* NON-LOCKING KEYS */
3081	    case NOP:
3082	    case LSH:  case RSH:  case LCTR: case RCTR:
3083	    case LALT: case RALT: case ASH:  case META:
3084		break;
3085
3086	    case BTAB:
3087		if (!(sc->flags & SC_SCRN_BLANKED))
3088		    return c;
3089		break;
3090
3091	    case SPSC:
3092#if NSPLASH > 0
3093		/* force activatation/deactivation of the screen saver */
3094		if (!(sc->flags & SC_SCRN_BLANKED)) {
3095		    run_scrn_saver = TRUE;
3096		    sc->scrn_time_stamp -= scrn_blank_time;
3097		}
3098		if (cold) {
3099		    /*
3100		     * While devices are being probed, the screen saver need
3101		     * to be invoked explictly. XXX
3102		     */
3103		    if (sc->flags & SC_SCRN_BLANKED) {
3104			scsplash_stick(FALSE);
3105			stop_scrn_saver(sc, current_saver);
3106		    } else {
3107			if (!ISGRAPHSC(scp)) {
3108			    scsplash_stick(TRUE);
3109			    (*current_saver)(sc, TRUE);
3110			}
3111		    }
3112		}
3113#endif /* NSPLASH */
3114		break;
3115
3116	    case RBT:
3117#ifndef SC_DISABLE_REBOOT
3118		shutdown_nice(0);
3119#endif
3120		break;
3121
3122	    case HALT:
3123#ifndef SC_DISABLE_REBOOT
3124		shutdown_nice(RB_HALT);
3125#endif
3126		break;
3127
3128	    case PDWN:
3129#ifndef SC_DISABLE_REBOOT
3130		shutdown_nice(RB_HALT|RB_POWEROFF);
3131#endif
3132		break;
3133
3134#ifdef DEV_APM
3135	    case SUSP:
3136		apm_suspend(PMST_SUSPEND);
3137		break;
3138	    case STBY:
3139		apm_suspend(PMST_STANDBY);
3140		break;
3141#else
3142	    case SUSP:
3143	    case STBY:
3144		break;
3145#endif
3146
3147	    case DBG:
3148#ifndef SC_DISABLE_DDBKEY
3149#ifdef DDB
3150		Debugger("manual escape to debugger");
3151#else
3152		printf("No debugger in kernel\n");
3153#endif
3154#else /* SC_DISABLE_DDBKEY */
3155		/* do nothing */
3156#endif /* SC_DISABLE_DDBKEY */
3157		break;
3158
3159	    case PNC:
3160		if (enable_panic_key)
3161			panic("Forced by the panic key");
3162		break;
3163
3164	    case NEXT:
3165		this_scr = scp->index;
3166		for (i = (this_scr - sc->first_vty + 1)%sc->vtys;
3167			sc->first_vty + i != this_scr;
3168			i = (i + 1)%sc->vtys) {
3169		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3170		    if (tp && tp->t_state & TS_ISOPEN) {
3171			sc_switch_scr(scp->sc, sc->first_vty + i);
3172			break;
3173		    }
3174		}
3175		break;
3176
3177	    case PREV:
3178		this_scr = scp->index;
3179		for (i = (this_scr - sc->first_vty + sc->vtys - 1)%sc->vtys;
3180			sc->first_vty + i != this_scr;
3181			i = (i + sc->vtys - 1)%sc->vtys) {
3182		    struct tty *tp = VIRTUAL_TTY(sc, sc->first_vty + i);
3183		    if (tp && tp->t_state & TS_ISOPEN) {
3184			sc_switch_scr(scp->sc, sc->first_vty + i);
3185			break;
3186		    }
3187		}
3188		break;
3189
3190	    default:
3191		if (KEYCHAR(c) >= F_SCR && KEYCHAR(c) <= L_SCR) {
3192		    sc_switch_scr(scp->sc, sc->first_vty + KEYCHAR(c) - F_SCR);
3193		    break;
3194		}
3195		/* assert(c & FKEY) */
3196		if (!(sc->flags & SC_SCRN_BLANKED))
3197		    return c;
3198		break;
3199	    }
3200	    /* goto next_code */
3201	} else {
3202	    /* regular keys (maybe MKEY is set) */
3203	    if (!(sc->flags & SC_SCRN_BLANKED))
3204		return c;
3205	}
3206    }
3207
3208    goto next_code;
3209}
3210
3211int
3212scmmap(dev_t dev, vm_offset_t offset, int nprot)
3213{
3214    scr_stat *scp;
3215
3216    scp = SC_STAT(dev);
3217    if (scp != scp->sc->cur_scp)
3218	return -1;
3219    return (*vidsw[scp->sc->adapter]->mmap)(scp->sc->adp, offset, nprot);
3220}
3221
3222static int
3223save_kbd_state(scr_stat *scp)
3224{
3225    int state;
3226    int error;
3227
3228    error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3229    if (error == ENOIOCTL)
3230	error = ENODEV;
3231    if (error == 0) {
3232	scp->status &= ~LOCK_MASK;
3233	scp->status |= state;
3234    }
3235    return error;
3236}
3237
3238static int
3239update_kbd_state(scr_stat *scp, int new_bits, int mask)
3240{
3241    int state;
3242    int error;
3243
3244    if (mask != LOCK_MASK) {
3245	error = kbd_ioctl(scp->sc->kbd, KDGKBSTATE, (caddr_t)&state);
3246	if (error == ENOIOCTL)
3247	    error = ENODEV;
3248	if (error)
3249	    return error;
3250	state &= ~mask;
3251	state |= new_bits & mask;
3252    } else {
3253	state = new_bits & LOCK_MASK;
3254    }
3255    error = kbd_ioctl(scp->sc->kbd, KDSKBSTATE, (caddr_t)&state);
3256    if (error == ENOIOCTL)
3257	error = ENODEV;
3258    return error;
3259}
3260
3261static int
3262update_kbd_leds(scr_stat *scp, int which)
3263{
3264    int error;
3265
3266    which &= LOCK_MASK;
3267    error = kbd_ioctl(scp->sc->kbd, KDSETLED, (caddr_t)&which);
3268    if (error == ENOIOCTL)
3269	error = ENODEV;
3270    return error;
3271}
3272
3273int
3274set_mode(scr_stat *scp)
3275{
3276    video_info_t info;
3277
3278    /* reject unsupported mode */
3279    if ((*vidsw[scp->sc->adapter]->get_info)(scp->sc->adp, scp->mode, &info))
3280	return 1;
3281
3282    /* if this vty is not currently showing, do nothing */
3283    if (scp != scp->sc->cur_scp)
3284	return 0;
3285
3286    /* setup video hardware for the given mode */
3287    (*vidsw[scp->sc->adapter]->set_mode)(scp->sc->adp, scp->mode);
3288    sc_vtb_init(&scp->scr, VTB_FRAMEBUFFER, scp->xsize, scp->ysize,
3289		(void *)scp->sc->adp->va_window, FALSE);
3290
3291#ifndef SC_NO_FONT_LOADING
3292    /* load appropriate font */
3293    if (!(scp->status & GRAPHICS_MODE)) {
3294	if (!(scp->status & PIXEL_MODE) && ISFONTAVAIL(scp->sc->adp->va_flags)) {
3295	    if (scp->font_size < 14) {
3296		if (scp->sc->fonts_loaded & FONT_8)
3297		    sc_load_font(scp, 0, 8, scp->sc->font_8, 0, 256);
3298	    } else if (scp->font_size >= 16) {
3299		if (scp->sc->fonts_loaded & FONT_16)
3300		    sc_load_font(scp, 0, 16, scp->sc->font_16, 0, 256);
3301	    } else {
3302		if (scp->sc->fonts_loaded & FONT_14)
3303		    sc_load_font(scp, 0, 14, scp->sc->font_14, 0, 256);
3304	    }
3305	    /*
3306	     * FONT KLUDGE:
3307	     * This is an interim kludge to display correct font.
3308	     * Always use the font page #0 on the video plane 2.
3309	     * Somehow we cannot show the font in other font pages on
3310	     * some video cards... XXX
3311	     */
3312	    sc_show_font(scp, 0);
3313	}
3314	mark_all(scp);
3315    }
3316#endif /* !SC_NO_FONT_LOADING */
3317
3318    sc_set_border(scp, scp->border);
3319    sc_set_cursor_image(scp);
3320
3321    return 0;
3322}
3323
3324void
3325sc_set_border(scr_stat *scp, int color)
3326{
3327    ++scp->sc->videoio_in_progress;
3328    (*scp->rndr->draw_border)(scp, color);
3329    --scp->sc->videoio_in_progress;
3330}
3331
3332#ifndef SC_NO_FONT_LOADING
3333void
3334sc_load_font(scr_stat *scp, int page, int size, u_char *buf,
3335	     int base, int count)
3336{
3337    sc_softc_t *sc;
3338
3339    sc = scp->sc;
3340    sc->font_loading_in_progress = TRUE;
3341    (*vidsw[sc->adapter]->load_font)(sc->adp, page, size, buf, base, count);
3342    sc->font_loading_in_progress = FALSE;
3343}
3344
3345void
3346sc_save_font(scr_stat *scp, int page, int size, u_char *buf,
3347	     int base, int count)
3348{
3349    sc_softc_t *sc;
3350
3351    sc = scp->sc;
3352    sc->font_loading_in_progress = TRUE;
3353    (*vidsw[sc->adapter]->save_font)(sc->adp, page, size, buf, base, count);
3354    sc->font_loading_in_progress = FALSE;
3355}
3356
3357void
3358sc_show_font(scr_stat *scp, int page)
3359{
3360    (*vidsw[scp->sc->adapter]->show_font)(scp->sc->adp, page);
3361}
3362#endif /* !SC_NO_FONT_LOADING */
3363
3364void
3365sc_paste(scr_stat *scp, u_char *p, int count)
3366{
3367    struct tty *tp;
3368    u_char *rmap;
3369
3370    if (scp->status & MOUSE_VISIBLE) {
3371	tp = VIRTUAL_TTY(scp->sc, scp->sc->cur_scp->index);
3372	if (!(tp->t_state & TS_ISOPEN))
3373	    return;
3374	rmap = scp->sc->scr_rmap;
3375	for (; count > 0; --count)
3376	    (*linesw[tp->t_line].l_rint)(rmap[*p++], tp);
3377    }
3378}
3379
3380void
3381sc_bell(scr_stat *scp, int pitch, int duration)
3382{
3383    if (cold || shutdown_in_progress)
3384	return;
3385
3386    if (scp != scp->sc->cur_scp && (scp->sc->flags & SC_QUIET_BELL))
3387	return;
3388
3389    if (scp->sc->flags & SC_VISUAL_BELL) {
3390	if (scp->sc->blink_in_progress)
3391	    return;
3392	scp->sc->blink_in_progress = 3;
3393	if (scp != scp->sc->cur_scp)
3394	    scp->sc->blink_in_progress += 2;
3395	blink_screen(scp->sc->cur_scp);
3396    } else {
3397	if (scp != scp->sc->cur_scp)
3398	    pitch *= 2;
3399	sysbeep(pitch, duration);
3400    }
3401}
3402
3403static void
3404blink_screen(void *arg)
3405{
3406    scr_stat *scp = arg;
3407    struct tty *tp;
3408
3409    if (ISGRAPHSC(scp) || (scp->sc->blink_in_progress <= 1)) {
3410	scp->sc->blink_in_progress = 0;
3411    	mark_all(scp);
3412	tp = VIRTUAL_TTY(scp->sc, scp->index);
3413	if (tp->t_state & TS_ISOPEN)
3414	    scstart(tp);
3415	if (scp->sc->delayed_next_scr)
3416	    sc_switch_scr(scp->sc, scp->sc->delayed_next_scr - 1);
3417    }
3418    else {
3419	(*scp->rndr->draw)(scp, 0, scp->xsize*scp->ysize,
3420			   scp->sc->blink_in_progress & 1);
3421	scp->sc->blink_in_progress--;
3422	timeout(blink_screen, scp, hz / 10);
3423    }
3424}
3425