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