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