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