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