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