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