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