syscons.c revision 16564
1/*-
2 * Copyright (c) 1992-1996 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 *    in this position and unchanged.
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 withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *  $Id: syscons.c,v 1.151 1996/06/17 17:21:35 wpaul Exp $
29 */
30
31#include "sc.h"
32#include "apm.h"
33#include "opt_ddb.h"
34
35#if NSC > 0
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
39#include <sys/ioctl.h>
40#include <sys/proc.h>
41#include <sys/signalvar.h>
42#include <sys/tty.h>
43#include <sys/uio.h>
44#include <sys/callout.h>
45#include <sys/kernel.h>
46#include <sys/syslog.h>
47#include <sys/errno.h>
48#include <sys/malloc.h>
49#include <sys/devconf.h>
50#ifdef	DEVFS
51#include <sys/devfsext.h>
52#endif
53
54#include <machine/clock.h>
55#include <machine/cons.h>
56#include <machine/console.h>
57#include <machine/psl.h>
58#include <machine/frame.h>
59#include <machine/pc/display.h>
60#include <machine/apm_bios.h>
61#include <machine/random.h>
62
63#include <vm/vm.h>
64#include <vm/vm_param.h>
65#include <vm/pmap.h>
66
67#include <i386/isa/isa.h>
68#include <i386/isa/isa_device.h>
69#include <i386/isa/timerreg.h>
70#include <i386/isa/kbdtables.h>
71#include <i386/isa/syscons.h>
72
73#if !defined(MAXCONS)
74#define MAXCONS 16
75#endif
76
77
78/* this may break on older VGA's but is usefull on real 32 bit systems */
79#define bcopyw  bcopy
80
81static default_attr user_default = {
82    (FG_LIGHTGREY | BG_BLACK) << 8,
83    (FG_BLACK | BG_LIGHTGREY) << 8
84};
85
86static default_attr kernel_default = {
87    (FG_WHITE | BG_BLACK) << 8,
88    (FG_BLACK | BG_LIGHTGREY) << 8
89};
90
91static  scr_stat    	main_console;
92static  scr_stat    	*console[MAXCONS];
93#ifdef DEVFS
94static	void		*sc_devfs_token[MAXCONS];
95#endif
96	scr_stat    	*cur_console;
97static  scr_stat    	*new_scp, *old_scp;
98static  term_stat   	kernel_console;
99static  default_attr    *current_default;
100static  char        	init_done = FALSE;
101static  int     	configuration = 0;
102static  char        	switch_in_progress = FALSE;
103static  char        	blink_in_progress = FALSE;
104static  char        	write_in_progress = FALSE;
105	u_int       	crtc_addr = MONO_BASE;
106static  char        	crtc_vga = FALSE;
107static  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
108static  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
109static  char        	*font_8 = NULL, *font_14 = NULL, *font_16 = NULL;
110static  int     	fonts_loaded = 0;
111	char        	palette[3*256];
112static  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
113static  int     	delayed_next_scr = FALSE;
114static  long        	scrn_blank_time = 0;    /* screen saver timeout value */
115	int     	scrn_blanked = FALSE;   /* screen saver active flag */
116static  long       	scrn_time_stamp;
117	u_char      	scr_map[256];
118static  char        	*video_mode_ptr = NULL;
119static	char *cut_buffer;
120static  u_short mouse_and_mask[16] = {
121	0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
122	0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
123};
124static  u_short mouse_or_mask[16] = {
125	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
126	0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
127};
128
129static void    none_saver(int blank) { }
130
131void    (*current_saver) __P((int blank)) = none_saver;
132
133static int scattach(struct isa_device *dev);
134static int scparam(struct tty *tp, struct termios *t);
135static int scprobe(struct isa_device *dev);
136static void scstart(struct tty *tp);
137
138/* OS specific stuff */
139#ifdef not_yet_done
140#define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
141struct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
142struct  tty         	*sccons[MAXCONS+1];
143#else
144#define VIRTUAL_TTY(x)  &sccons[x]
145#define CONSOLE_TTY 	&sccons[MAXCONS]
146static struct tty     	sccons[MAXCONS+1];
147#endif
148#define MONO_BUF    	pa_to_va(0xB0000)
149#define CGA_BUF     	pa_to_va(0xB8000)
150u_short         	*Crtat;
151
152#define WRAPHIST(scp, pointer, offset)\
153    ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
154    + (offset)) % (scp->history_size)))
155
156struct  isa_driver scdriver = {
157    scprobe, scattach, "sc", 1
158};
159
160static	d_open_t	scopen;
161static	d_close_t	scclose;
162static	d_read_t	scread;
163static	d_write_t	scwrite;
164static	d_ioctl_t	scioctl;
165static	d_devtotty_t	scdevtotty;
166static	d_mmap_t	scmmap;
167
168#define CDEV_MAJOR 12
169static	struct cdevsw	scdevsw = {
170	scopen,		scclose,	scread,		scwrite,
171	scioctl,	nullstop,	noreset,	scdevtotty,
172	ttselect,	scmmap,		nostrategy,	"sc",	NULL,	-1 };
173
174/*
175 * Calculate hardware attributes word using logical attributes mask and
176 * hardware colors
177 */
178
179static int
180mask2attr(struct term_stat *term)
181{
182    int attr, mask = term->attr_mask;
183
184    if (mask & REVERSE_ATTR) {
185	attr = ((mask & FOREGROUND_CHANGED) ?
186		((term->cur_color & 0xF000) >> 4) :
187		(term->rev_color & 0x0F00)) |
188	       ((mask & BACKGROUND_CHANGED) ?
189		((term->cur_color & 0x0F00) << 4) :
190		(term->rev_color & 0xF000));
191    } else
192	attr = term->cur_color;
193
194    /* XXX: underline mapping for Hercules adapter can be better */
195    if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
196	attr ^= 0x0800;
197    if (mask & BLINK_ATTR)
198	attr ^= 0x8000;
199
200    return attr;
201}
202
203static int
204scprobe(struct isa_device *dev)
205{
206    int i, j, retries = 5;
207    unsigned char val;
208
209    /* Enable interrupts and keyboard controller */
210    kbd_wait();
211    outb(KB_STAT, KB_WRITE);
212    kbd_wait();
213    outb(KB_DATA, KB_MODE);
214
215    /* flush any noise in the buffer */
216    while (inb(KB_STAT) & KB_BUF_FULL) {
217	DELAY(25);
218	(void) inb(KB_DATA);
219    }
220
221    /* Reset keyboard hardware */
222    while (retries--) {
223	kbd_wait();
224	outb(KB_DATA, KB_RESET);
225	for (i=0; i<10000; i++) {
226	    DELAY(100);
227	    val = inb(KB_DATA);
228	    if (val == KB_ACK || val == KB_ECHO)
229		goto gotres;
230	    if (val == KB_RESEND)
231		break;
232	}
233    }
234gotres:
235    if (retries < 0)
236	printf("scprobe: keyboard won't accept RESET command\n");
237    else {
238	i = 10;			/* At most 10 retries. */
239gotack:
240	DELAY(25);
241	j = 1000;		/* Wait at most 50 ms (supposedly). */
242	while ((inb(KB_STAT) & KB_BUF_FULL) == 0 && --j > 0) DELAY(50);
243	DELAY(25);
244	val = inb(KB_DATA);
245	if (val == KB_ACK && --i > 0)
246	    goto gotack;
247	if (val != KB_RESET_DONE)
248	    printf("scprobe: keyboard RESET failed (result = 0x%02x)\n", val);
249    }
250#ifdef XT_KEYBOARD
251    kbd_wait();
252    outb(KB_DATA, 0xF0);
253    kbd_wait();
254    outb(KB_DATA, 1);
255    kbd_wait();
256#endif /* XT_KEYBOARD */
257    return (IO_KBDSIZE);
258}
259
260static struct kern_devconf kdc_sc[NSC] = {
261    0, 0, 0,        		/* filled in by dev_attach */
262    "sc", 0, { MDDT_ISA, 0, "tty" },
263    isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
264    &kdc_isa0,      		/* parent */
265    0,          		/* parentdata */
266    DC_BUSY,        		/* the console is almost always busy */
267    "Graphics console",
268    DC_CLS_DISPLAY		/* class */
269};
270
271static inline void
272sc_registerdev(struct isa_device *id)
273{
274    if(id->id_unit)
275	kdc_sc[id->id_unit] = kdc_sc[0];
276    kdc_sc[id->id_unit].kdc_unit = id->id_unit;
277    kdc_sc[id->id_unit].kdc_isa = id;
278    dev_attach(&kdc_sc[id->id_unit]);
279}
280
281#if NAPM > 0
282static int
283scresume(void *dummy)
284{
285	shfts = 0;
286	ctls = 0;
287	alts = 0;
288	agrs = 0;
289	metas = 0;
290	return 0;
291}
292#endif
293
294/*
295 * These functions need to be before calls to them so they can be inlined.
296 */
297static inline void
298draw_cursor(scr_stat *scp, int show)
299{
300    if (show && !(scp->status & CURSOR_SHOWN)) {
301	u_short cursor_image = *(Crtat + (scp->cursor_pos - scp->scr_buf));
302
303	scp->cursor_saveunder = cursor_image;
304	if (configuration & CHAR_CURSOR) {
305	    set_destructive_cursor(scp, FALSE);
306	    cursor_image = (cursor_image & 0xff00) | DEAD_CHAR;
307	}
308	else {
309	    if ((cursor_image & 0x7000) == 0x7000) {
310		cursor_image &= 0x8fff;
311		if(!(cursor_image & 0x0700))
312		    cursor_image |= 0x0700;
313	    } else {
314		cursor_image |= 0x7000;
315		if ((cursor_image & 0x0700) == 0x0700)
316		    cursor_image &= 0xf0ff;
317	    }
318	}
319	*(Crtat + (scp->cursor_pos - scp->scr_buf)) = cursor_image;
320	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
321	scp->status |= CURSOR_SHOWN;
322    }
323    if (!show && (scp->status & CURSOR_SHOWN)) {
324	*(Crtat + (scp->cursor_pos - scp->scr_buf)) = scp->cursor_saveunder;
325	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
326	scp->status &= ~CURSOR_SHOWN;
327    }
328}
329
330static inline void
331move_crsr(scr_stat *scp, int x, int y)
332{
333    if (x < 0)
334	x = 0;
335    if (y < 0)
336	y = 0;
337    if (x >= scp->xsize)
338	x = scp->xsize-1;
339    if (y >= scp->ysize)
340	y = scp->ysize-1;
341    scp->xpos = x;
342    scp->ypos = y;
343    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
344    scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
345    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
346}
347
348static int
349scattach(struct isa_device *dev)
350{
351    scr_stat	*scp;
352#ifdef DEVFS
353    int		vc;
354#endif
355
356    scinit();
357    configuration = dev->id_flags;
358
359    scp = console[0];
360
361    if (crtc_vga) {
362    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
363	font_8 = (char *)malloc(8*256, M_DEVBUF, M_NOWAIT);
364	font_14 = (char *)malloc(14*256, M_DEVBUF, M_NOWAIT);
365	font_16 = (char *)malloc(16*256, M_DEVBUF, M_NOWAIT);
366	copy_font(SAVE, FONT_16, font_16);
367	fonts_loaded = FONT_16;
368	scp->font = FONT_16;
369	save_palette();
370    }
371
372    scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
373				     M_DEVBUF, M_NOWAIT);
374    /* copy screen to buffer */
375    bcopyw(Crtat, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
376    scp->cursor_pos = scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
377    scp->mouse_pos = scp->scr_buf;
378
379    /* initialize history buffer & pointers */
380    scp->history_head = scp->history_pos = scp->history =
381	(u_short *)malloc(scp->history_size*sizeof(u_short),
382			  M_DEVBUF, M_NOWAIT);
383    bzero(scp->history_head, scp->history_size*sizeof(u_short));
384
385    /* initialize cursor stuff */
386    draw_cursor(scp, TRUE);
387    if (crtc_vga && (configuration & CHAR_CURSOR))
388	set_destructive_cursor(scp, TRUE);
389
390    /* get screen update going */
391    scrn_timer();
392
393    update_leds(scp->status);
394    sc_registerdev(dev);
395
396    printf("sc%d: ", dev->id_unit);
397    if (crtc_vga)
398	if (crtc_addr == MONO_BASE)
399	    printf("VGA mono");
400	else
401	    printf("VGA color");
402    else
403	if (crtc_addr == MONO_BASE)
404	    printf("MDA/hercules");
405	else
406	    printf("CGA/EGA");
407    printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, configuration);
408
409#if NAPM > 0
410    scp->r_hook.ah_fun = scresume;
411    scp->r_hook.ah_arg = NULL;
412    scp->r_hook.ah_name = "system keyboard";
413    scp->r_hook.ah_order = APM_MID_ORDER;
414    apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
415#endif
416
417    {
418    dev_t dev = makedev(CDEV_MAJOR, 0);
419
420    cdevsw_add(&dev, &scdevsw, NULL);
421    }
422#ifdef DEVFS
423    for (vc = 0; vc < MAXCONS; vc++)
424        sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR,
425					      UID_ROOT, GID_WHEEL, 0600,
426					      "ttyv%n", vc);
427#endif
428
429    return 0;
430}
431
432struct tty
433*scdevtotty(dev_t dev)
434{
435    int unit = minor(dev);
436
437    if (!init_done)
438	return(NULL);
439    if (unit > MAXCONS || unit < 0)
440	return(NULL);
441    if (unit == MAXCONS)
442	return CONSOLE_TTY;
443    return VIRTUAL_TTY(unit);
444}
445
446static scr_stat
447*get_scr_stat(dev_t dev)
448{
449    int unit = minor(dev);
450
451    if (unit > MAXCONS || unit < 0)
452	return(NULL);
453    if (unit == MAXCONS)
454	return console[0];
455    return console[unit];
456}
457
458static int
459get_scr_num()
460{
461    int i = 0;
462
463    while ((i < MAXCONS) && (cur_console != console[i]))
464	i++;
465    return i < MAXCONS ? i : 0;
466}
467
468int
469scopen(dev_t dev, int flag, int mode, struct proc *p)
470{
471    struct tty *tp = scdevtotty(dev);
472
473    if (!tp)
474	return(ENXIO);
475
476    tp->t_oproc = scstart;
477    tp->t_param = scparam;
478    tp->t_dev = dev;
479    if (!(tp->t_state & TS_ISOPEN)) {
480	ttychars(tp);
481	tp->t_iflag = TTYDEF_IFLAG;
482	tp->t_oflag = TTYDEF_OFLAG;
483	tp->t_cflag = TTYDEF_CFLAG;
484	tp->t_lflag = TTYDEF_LFLAG;
485	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
486	scparam(tp, &tp->t_termios);
487	ttsetwater(tp);
488	(*linesw[tp->t_line].l_modem)(tp, 1);
489    }
490    else
491	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
492	    return(EBUSY);
493    if (!console[minor(dev)])
494	console[minor(dev)] = alloc_scp();
495    return((*linesw[tp->t_line].l_open)(dev, tp));
496}
497
498int
499scclose(dev_t dev, int flag, int mode, struct proc *p)
500{
501    struct tty *tp = scdevtotty(dev);
502    struct scr_stat *scp;
503
504    if (!tp)
505	return(ENXIO);
506    if (minor(dev) < MAXCONS) {
507	scp = get_scr_stat(tp->t_dev);
508	if (scp->status & SWITCH_WAIT_ACQ)
509	    wakeup((caddr_t)&scp->smode);
510#if not_yet_done
511	if (scp == &main_console) {
512	    scp->pid = 0;
513	    scp->proc = NULL;
514	    scp->smode.mode = VT_AUTO;
515	}
516	else {
517	    free(scp->scr_buf, M_DEVBUF);
518	    free(scp->history, M_DEVBUF);
519	    free(scp, M_DEVBUF);
520	    console[minor(dev)] = NULL;
521	}
522#else
523	scp->pid = 0;
524	scp->proc = NULL;
525	scp->smode.mode = VT_AUTO;
526#endif
527    }
528    (*linesw[tp->t_line].l_close)(tp, flag);
529    ttyclose(tp);
530    return(0);
531}
532
533int
534scread(dev_t dev, struct uio *uio, int flag)
535{
536    struct tty *tp = scdevtotty(dev);
537
538    if (!tp)
539	return(ENXIO);
540    return((*linesw[tp->t_line].l_read)(tp, uio, flag));
541}
542
543int
544scwrite(dev_t dev, struct uio *uio, int flag)
545{
546    struct tty *tp = scdevtotty(dev);
547
548    if (!tp)
549	return(ENXIO);
550    return((*linesw[tp->t_line].l_write)(tp, uio, flag));
551}
552
553void
554scintr(int unit)
555{
556    static struct tty *cur_tty;
557    int c, len;
558    u_char *cp;
559
560    /* make screensaver happy */
561    scrn_time_stamp = time.tv_sec;
562    if (scrn_blanked) {
563	(*current_saver)(FALSE);
564	cur_console->start = 0;
565	cur_console->end = cur_console->xsize * cur_console->ysize;
566    }
567
568    c = scgetc(1);
569
570    cur_tty = VIRTUAL_TTY(get_scr_num());
571    if (!(cur_tty->t_state & TS_ISOPEN))
572	if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
573	    return;
574
575    switch (c & 0xff00) {
576    case 0x0000: /* normal key */
577	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
578	break;
579    case NOKEY: /* nothing there */
580	break;
581    case FKEY:  /* function key, return string */
582	if (cp = get_fstr((u_int)c, (u_int *)&len)) {
583	    while (len-- >  0)
584		(*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
585	}
586	break;
587    case MKEY:  /* meta is active, prepend ESC */
588	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
589	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
590	break;
591    case BKEY:  /* backtab fixed sequence (esc [ Z) */
592	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
593	(*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
594	(*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
595	break;
596    }
597}
598
599static int
600scparam(struct tty *tp, struct termios *t)
601{
602    tp->t_ispeed = t->c_ispeed;
603    tp->t_ospeed = t->c_ospeed;
604    tp->t_cflag = t->c_cflag;
605    return 0;
606}
607
608int
609scioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
610{
611    int i, error;
612    struct tty *tp;
613    struct trapframe *fp;
614    scr_stat *scp;
615
616    tp = scdevtotty(dev);
617    if (!tp)
618	return ENXIO;
619    scp = get_scr_stat(tp->t_dev);
620
621    switch (cmd) {  		/* process console hardware related ioctl's */
622
623    case GIO_ATTR:      	/* get current attributes */
624	*(int*)data = scp->term.cur_attr;
625	return 0;
626
627    case GIO_COLOR:     	/* is this a color console ? */
628	if (crtc_addr == COLOR_BASE)
629	    *(int*)data = 1;
630	else
631	    *(int*)data = 0;
632	return 0;
633
634    case CONS_CURRENT:  	/* get current adapter type */
635	if (crtc_vga)
636	    *(int*)data = KD_VGA;
637	else
638	    if (crtc_addr == MONO_BASE)
639		*(int*)data = KD_MONO;
640	    else
641		*(int*)data = KD_CGA;
642	return 0;
643
644    case CONS_GET:      	/* get current video mode */
645	*(int*)data = scp->mode;
646	return 0;
647
648    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
649	scrn_blank_time = *(int*)data;
650	return 0;
651
652    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
653	if ((*(int*)data) & 0x01)
654	    configuration |= BLINK_CURSOR;
655	else
656	    configuration &= ~BLINK_CURSOR;
657	if ((*(int*)data) & 0x02) {
658	    configuration |= CHAR_CURSOR;
659	    set_destructive_cursor(scp, TRUE);
660	} else
661	    configuration &= ~CHAR_CURSOR;
662	return 0;
663
664    case CONS_BELLTYPE: 	/* set bell type sound/visual */
665	if (*data)
666	    configuration |= VISUAL_BELL;
667	else
668	    configuration &= ~VISUAL_BELL;
669	return 0;
670
671    case CONS_HISTORY:  	/* set history size */
672	if (*data) {
673	    free(scp->history, M_DEVBUF);
674	    scp->history_size = *(int*)data;
675	    if (scp->history_size < scp->ysize)
676		scp->history = NULL;
677	    else {
678		scp->history_size *= scp->xsize;
679		scp->history_head = scp->history_pos = scp->history =
680		    (u_short *)malloc(scp->history_size*sizeof(u_short),
681				      M_DEVBUF, M_WAITOK);
682		bzero(scp->history_head, scp->history_size*sizeof(u_short));
683	    }
684	    return 0;
685	}
686	else
687	    return EINVAL;
688
689    case CONS_MOUSECTL:		/* control mouse arrow */
690    {
691	mouse_info_t *mouse = (mouse_info_t*)data;
692	int fontsize;
693	scr_stat *mscp = cur_console;
694
695	switch (mscp->font) {
696	default:
697	case FONT_8:
698	    fontsize = 8; break;
699	case FONT_14:
700	    fontsize = 14; break;
701	case FONT_16:
702	    fontsize = 16; break;
703	}
704	switch (mouse->operation) {
705	case MOUSE_SHOW:
706	    if (!(mscp->status & MOUSE_ENABLED)) {
707		mscp->mouse_oldpos = Crtat + (mscp->mouse_pos - mscp->scr_buf);
708		mscp->status |= MOUSE_ENABLED;
709		mark_all(mscp);
710	    }
711	    else
712		return EINVAL;
713	    break;
714
715	case MOUSE_HIDE:
716	    if (mscp->status & MOUSE_ENABLED) {
717		mscp->status &= ~MOUSE_ENABLED;
718		mark_all(mscp);
719	    }
720	    else
721		return EINVAL;
722	    break;
723
724	case MOUSE_CUT_START:
725	    if (mscp->status & MOUSE_ENABLED) {
726    		if (mscp->mouse_cut_start != NULL)
727		    reverse_mouse_cut(mscp, 0);
728		mscp->mouse_cut_start = mscp->mouse_cut_end = mscp->mouse_pos;
729		*mscp->mouse_cut_start = (*mscp->mouse_cut_start & 0x88ff) |
730				       (*mscp->mouse_cut_start & 0x7000) >> 4 |
731				       (*mscp->mouse_cut_start & 0x0700) << 4;
732		cut_buffer[0] = *mscp->mouse_cut_start & 0xff;
733		cut_buffer[1] = 0;
734		mscp->status |= MOUSE_CUTTING;
735		/* delete all other screens cut markings */
736		for (i=0; i<MAXCONS; i++) {
737		    if (console[i] == NULL || console[i] == mscp)
738			continue;
739		    reverse_mouse_cut(console[i], 0);
740		    console[i]->mouse_cut_start=console[i]->mouse_cut_end=NULL;
741		}
742		mark_for_update(mscp, mscp->mouse_cut_start - mscp->scr_buf);
743	    }
744	    else
745		return EINVAL;
746	    break;
747
748	case MOUSE_CUT_END:
749	    if (mscp->status & MOUSE_ENABLED) {
750		mscp->status &= ~MOUSE_CUTTING;
751	    }
752	    else
753		return EINVAL;
754	    break;
755
756	case MOUSE_CUT_RESET:
757	    if (mscp->status & MOUSE_ENABLED) {
758		mscp->status &= ~MOUSE_CUTTING;
759		reverse_mouse_cut(mscp, 0);
760		mscp->mouse_cut_start = mscp->mouse_cut_end = NULL;
761	    }
762	    else
763		return EINVAL;
764	    break;
765
766	case MOUSE_RETURN_CUTBUFFER:
767	    if (mscp->status & MOUSE_ENABLED) {
768		struct tty *tp;
769		char *ptr = cut_buffer;
770
771    		tp = VIRTUAL_TTY(get_scr_num());
772		while (*ptr)
773		    (*linesw[tp->t_line].l_rint)(*ptr++, tp);
774	    }
775	    else
776		return EINVAL;
777	    break;
778
779	case MOUSE_MOVEABS:
780	    mscp->mouse_xpos = mouse->x;
781	    mscp->mouse_ypos = mouse->y;
782	    goto set_mouse_pos;
783
784	case MOUSE_MOVEREL:
785	    mscp->mouse_xpos += mouse->x;
786	    mscp->mouse_ypos += mouse->y;
787set_mouse_pos:
788	    /*
789	     * the margins imposed here are not ideal, we loose
790	     * a couble of pixels on the borders..
791	     */
792	    if (mscp->mouse_xpos < 0)
793		mscp->mouse_xpos = 0;
794	    if (mscp->mouse_ypos < 0)
795		mscp->mouse_ypos = 0;
796	    if (mscp->mouse_xpos > (mscp->xsize*8-2))
797		mscp->mouse_xpos = (mscp->xsize*8)-2;
798	    if (mscp->mouse_ypos > (mscp->ysize*fontsize)-2)
799		mscp->mouse_ypos = (mscp->ysize*fontsize)-2;
800
801	    mscp->mouse_pos = mscp->scr_buf +
802		((mscp->mouse_ypos/fontsize)*mscp->xsize + mscp->mouse_xpos/8);
803
804	    if ((mscp->status&MOUSE_ENABLED) && (mscp->status&MOUSE_CUTTING)) {
805		int s = splclock();
806		reverse_mouse_cut(mscp, 0);
807		mscp->mouse_cut_end = mscp->mouse_pos;
808		reverse_mouse_cut(mscp, 1);
809    		mark_all(mscp);
810		splx(s);
811	    }
812	    break;
813
814	case MOUSE_GETPOS:
815	    mouse->x = mscp->mouse_xpos;
816	    mouse->y = mscp->mouse_ypos;
817	    return 0;
818
819	default:
820	    return EINVAL;
821	}
822	/* make screensaver happy */
823	if (scp == cur_console) {
824	    scrn_time_stamp = time.tv_sec;
825	    if (scrn_blanked) {
826		(*current_saver)(FALSE);
827		cur_console->start = 0;
828		cur_console->end = cur_console->xsize * cur_console->ysize;
829	    }
830	}
831	return 0;
832    }
833
834    case CONS_GETINFO:  	/* get current (virtual) console info */
835    {
836	vid_info_t *ptr = (vid_info_t*)data;
837	if (ptr->size == sizeof(struct vid_info)) {
838	    ptr->m_num = get_scr_num();
839	    ptr->mv_col = scp->xpos;
840	    ptr->mv_row = scp->ypos;
841	    ptr->mv_csz = scp->xsize;
842	    ptr->mv_rsz = scp->ysize;
843	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
844	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
845	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
846	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
847	    ptr->mv_grfc.fore = 0;      /* not supported */
848	    ptr->mv_grfc.back = 0;      /* not supported */
849	    ptr->mv_ovscan = scp->border;
850	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
851	    return 0;
852	}
853	return EINVAL;
854    }
855
856    case CONS_GETVERS:  	/* get version number */
857	*(int*)data = 0x200;    /* version 2.0 */
858	return 0;
859
860    /* VGA TEXT MODES */
861    case SW_VGA_C40x25:
862    case SW_VGA_C80x25: case SW_VGA_M80x25:
863    case SW_VGA_C80x30: case SW_VGA_M80x30:
864    case SW_VGA_C80x50: case SW_VGA_M80x50:
865    case SW_VGA_C80x60: case SW_VGA_M80x60:
866    case SW_B40x25:     case SW_C40x25:
867    case SW_B80x25:     case SW_C80x25:
868    case SW_ENH_B40x25: case SW_ENH_C40x25:
869    case SW_ENH_B80x25: case SW_ENH_C80x25:
870    case SW_ENH_B80x43: case SW_ENH_C80x43:
871
872	if (!crtc_vga || video_mode_ptr == NULL)
873	    return ENXIO;
874	switch (cmd & 0xff) {
875	case M_VGA_C80x60: case M_VGA_M80x60:
876	    if (!(fonts_loaded & FONT_8))
877		return EINVAL;
878	    scp->xsize = 80;
879	    scp->ysize = 60;
880	    break;
881	case M_VGA_C80x50: case M_VGA_M80x50:
882	    if (!(fonts_loaded & FONT_8))
883		return EINVAL;
884	    scp->xsize = 80;
885	    scp->ysize = 50;
886	    break;
887	case M_ENH_B80x43: case M_ENH_C80x43:
888	    if (!(fonts_loaded & FONT_8))
889		return EINVAL;
890	    scp->xsize = 80;
891	    scp->ysize = 43;
892	    break;
893	case M_VGA_C80x30: case M_VGA_M80x30:
894	    scp->xsize = 80;
895	    scp->ysize = 30;
896	    break;
897	default:
898	    if ((cmd & 0xff) > M_VGA_CG320)
899		return EINVAL;
900	    else
901		scp->xsize = *(video_mode_ptr+((cmd&0xff)*64));
902		scp->ysize = *(video_mode_ptr+((cmd&0xff)*64)+1)+1;
903	    break;
904	}
905	scp->mode = cmd & 0xff;
906	scp->status &= ~UNKNOWN_MODE;
907	free(scp->scr_buf, M_DEVBUF);
908	scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
909					 M_DEVBUF, M_WAITOK);
910	free(cut_buffer, M_DEVBUF);
911    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
912	cut_buffer[0] = 0x00;
913	if (scp == cur_console)
914	    set_mode(scp);
915	clear_screen(scp);
916	if (tp->t_winsize.ws_col != scp->xsize
917	    || tp->t_winsize.ws_row != scp->ysize) {
918	    tp->t_winsize.ws_col = scp->xsize;
919	    tp->t_winsize.ws_row = scp->ysize;
920	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
921	}
922	return 0;
923
924    /* GRAPHICS MODES */
925    case SW_BG320:     case SW_BG640:
926    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
927    case SW_CG640x350: case SW_ENH_CG640:
928    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
929
930	if (!crtc_vga || video_mode_ptr == NULL)
931	    return ENXIO;
932	scp->mode = cmd & 0xFF;
933	scp->status |= UNKNOWN_MODE;    /* graphics mode */
934	scp->xsize = (*(video_mode_ptr + (scp->mode*64))) * 8;
935	scp->ysize = (*(video_mode_ptr + (scp->mode*64) + 1) + 1) *
936		     (*(video_mode_ptr + (scp->mode*64) + 2));
937	set_mode(scp);
938	/* clear_graphics();*/
939
940	if (tp->t_winsize.ws_xpixel != scp->xsize
941	    || tp->t_winsize.ws_ypixel != scp->ysize) {
942	    tp->t_winsize.ws_xpixel = scp->xsize;
943	    tp->t_winsize.ws_ypixel = scp->ysize;
944	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
945	}
946	return 0;
947
948    case VT_SETMODE:    	/* set screen switcher mode */
949	bcopy(data, &scp->smode, sizeof(struct vt_mode));
950	if (scp->smode.mode == VT_PROCESS) {
951	    scp->proc = p;
952	    scp->pid = scp->proc->p_pid;
953	}
954	return 0;
955
956    case VT_GETMODE:    	/* get screen switcher mode */
957	bcopy(&scp->smode, data, sizeof(struct vt_mode));
958	return 0;
959
960    case VT_RELDISP:    	/* screen switcher ioctl */
961	switch(*data) {
962	case VT_FALSE:  	/* user refuses to release screen, abort */
963	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
964		old_scp->status &= ~SWITCH_WAIT_REL;
965		switch_in_progress = FALSE;
966		return 0;
967	    }
968	    return EINVAL;
969
970	case VT_TRUE:   	/* user has released screen, go on */
971	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
972		scp->status &= ~SWITCH_WAIT_REL;
973		exchange_scr();
974		if (new_scp->smode.mode == VT_PROCESS) {
975		    new_scp->status |= SWITCH_WAIT_ACQ;
976		    psignal(new_scp->proc, new_scp->smode.acqsig);
977		}
978		else
979		    switch_in_progress = FALSE;
980		return 0;
981	    }
982	    return EINVAL;
983
984	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
985	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
986		scp->status &= ~SWITCH_WAIT_ACQ;
987		switch_in_progress = FALSE;
988		return 0;
989	    }
990	    return EINVAL;
991
992	default:
993	    return EINVAL;
994	}
995	/* NOT REACHED */
996
997    case VT_OPENQRY:    	/* return free virtual console */
998	for (i = 0; i < MAXCONS; i++) {
999	    tp = VIRTUAL_TTY(i);
1000	    if (!(tp->t_state & TS_ISOPEN)) {
1001		*data = i + 1;
1002		return 0;
1003	    }
1004	}
1005	return EINVAL;
1006
1007    case VT_ACTIVATE:   	/* switch to screen *data */
1008	return switch_scr(scp, (*data) - 1);
1009
1010    case VT_WAITACTIVE: 	/* wait for switch to occur */
1011	if (*data > MAXCONS || *data < 0)
1012	    return EINVAL;
1013	if (minor(dev) == (*data) - 1)
1014	    return 0;
1015	if (*data == 0) {
1016	    if (scp == cur_console)
1017		return 0;
1018	}
1019	else
1020	    scp = console[(*data) - 1];
1021	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
1022			     "waitvt", 0)) == ERESTART) ;
1023	return error;
1024
1025    case VT_GETACTIVE:
1026	*data = get_scr_num()+1;
1027	return 0;
1028
1029    case KDENABIO:      	/* allow io operations */
1030	error = suser(p->p_ucred, &p->p_acflag);
1031	if (error != 0)
1032	    return error;
1033	fp = (struct trapframe *)p->p_md.md_regs;
1034	fp->tf_eflags |= PSL_IOPL;
1035	return 0;
1036
1037    case KDDISABIO:     	/* disallow io operations (default) */
1038	fp = (struct trapframe *)p->p_md.md_regs;
1039	fp->tf_eflags &= ~PSL_IOPL;
1040	return 0;
1041
1042    case KDSETMODE:     	/* set current mode of this (virtual) console */
1043	switch (*data) {
1044	case KD_TEXT:   	/* switch to TEXT (known) mode */
1045	    /* restore fonts & palette ! */
1046	    if (crtc_vga) {
1047		if (fonts_loaded & FONT_8)
1048		    copy_font(LOAD, FONT_8, font_8);
1049		if (fonts_loaded & FONT_14)
1050		    copy_font(LOAD, FONT_14, font_14);
1051		if (fonts_loaded & FONT_16)
1052		    copy_font(LOAD, FONT_16, font_16);
1053		if (configuration & CHAR_CURSOR)
1054		    set_destructive_cursor(scp, TRUE);
1055		load_palette();
1056	    }
1057	    /* FALL THROUGH */
1058
1059	case KD_TEXT1:  	/* switch to TEXT (known) mode */
1060	    /* no restore fonts & palette */
1061	    scp->status &= ~UNKNOWN_MODE;
1062	    if (crtc_vga && video_mode_ptr)
1063		set_mode(scp);
1064	    clear_screen(scp);
1065	    return 0;
1066
1067	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
1068	    scp->status |= UNKNOWN_MODE;
1069	    return 0;
1070	default:
1071	    return EINVAL;
1072	}
1073	/* NOT REACHED */
1074
1075    case KDGETMODE:     	/* get current mode of this (virtual) console */
1076	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
1077	return 0;
1078
1079    case KDSBORDER:     	/* set border color of this (virtual) console */
1080	if (!crtc_vga)
1081	    return ENXIO;
1082	scp->border = *data;
1083	if (scp == cur_console)
1084	    set_border(scp->border);
1085	return 0;
1086
1087    case KDSKBSTATE:    	/* set keyboard state (locks) */
1088	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
1089	    scp->status &= ~LOCK_KEY_MASK;
1090	    scp->status |= *data;
1091	    if (scp == cur_console)
1092		update_leds(scp->status);
1093	    return 0;
1094	}
1095	return EINVAL;
1096
1097    case KDGKBSTATE:    	/* get keyboard state (locks) */
1098	*data = scp->status & LOCK_KEY_MASK;
1099	return 0;
1100
1101    case KDSETRAD:      	/* set keyboard repeat & delay rates */
1102	if (*data & 0x80)
1103	    return EINVAL;
1104	i = spltty();
1105	kbd_cmd(KB_SETRAD);
1106	kbd_cmd(*data);
1107	splx(i);
1108	return 0;
1109
1110    case KDSKBMODE:     	/* set keyboard mode */
1111	switch (*data) {
1112	case K_RAW: 		/* switch to RAW scancode mode */
1113	    scp->status |= KBD_RAW_MODE;
1114	    return 0;
1115
1116	case K_XLATE:   	/* switch to XLT ascii mode */
1117	    if (scp == cur_console && scp->status == KBD_RAW_MODE)
1118		shfts = ctls = alts = agrs = metas = 0;
1119	    scp->status &= ~KBD_RAW_MODE;
1120	    return 0;
1121	default:
1122	    return EINVAL;
1123	}
1124	/* NOT REACHED */
1125
1126    case KDGKBMODE:     	/* get keyboard mode */
1127	*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
1128	return 0;
1129
1130    case KDMKTONE:      	/* sound the bell */
1131	if (*(int*)data)
1132	    do_bell(scp, (*(int*)data)&0xffff,
1133		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1134	else
1135	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1136	return 0;
1137
1138    case KIOCSOUND:     	/* make tone (*data) hz */
1139	if (scp == cur_console) {
1140	    if (*(int*)data) {
1141		int pitch = TIMER_FREQ/(*(int*)data);
1142
1143		/* set command for counter 2, 2 byte write */
1144		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1145		    return EBUSY;
1146
1147		/* set pitch */
1148		outb(TIMER_CNTR2, pitch);
1149		outb(TIMER_CNTR2, (pitch>>8));
1150
1151		/* enable counter 2 output to speaker */
1152		outb(IO_PPI, inb(IO_PPI) | 3);
1153	    }
1154	    else {
1155		/* disable counter 2 output to speaker */
1156		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1157		release_timer2();
1158	    }
1159	}
1160	return 0;
1161
1162    case KDGKBTYPE:     	/* get keyboard type */
1163	*data = 0;  		/* type not known (yet) */
1164	return 0;
1165
1166    case KDSETLED:      	/* set keyboard LED status */
1167	if (*data >= 0 && *data <= LED_MASK) {
1168	    scp->status &= ~LED_MASK;
1169	    scp->status |= *data;
1170	    if (scp == cur_console)
1171		update_leds(scp->status);
1172	    return 0;
1173	}
1174	return EINVAL;
1175
1176    case KDGETLED:      	/* get keyboard LED status */
1177	*data = scp->status & LED_MASK;
1178	return 0;
1179
1180    case GETFKEY:       	/* get functionkey string */
1181	if (*(u_short*)data < n_fkey_tab) {
1182	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1183	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1184		  fkey_tab[ptr->keynum].len);
1185	    ptr->flen = fkey_tab[ptr->keynum].len;
1186	    return 0;
1187	}
1188	else
1189	    return EINVAL;
1190
1191    case SETFKEY:       	/* set functionkey string */
1192	if (*(u_short*)data < n_fkey_tab) {
1193	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1194	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1195		  min(ptr->flen, MAXFK));
1196	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1197	    return 0;
1198	}
1199	else
1200	    return EINVAL;
1201
1202    case GIO_SCRNMAP:   	/* get output translation table */
1203	bcopy(&scr_map, data, sizeof(scr_map));
1204	return 0;
1205
1206    case PIO_SCRNMAP:   	/* set output translation table */
1207	bcopy(data, &scr_map, sizeof(scr_map));
1208	return 0;
1209
1210    case GIO_KEYMAP:    	/* get keyboard translation table */
1211	bcopy(&key_map, data, sizeof(key_map));
1212	return 0;
1213
1214    case PIO_KEYMAP:    	/* set keyboard translation table */
1215	bcopy(data, &key_map, sizeof(key_map));
1216	return 0;
1217
1218    case PIO_FONT8x8:   	/* set 8x8 dot font */
1219	if (!crtc_vga)
1220	    return ENXIO;
1221	bcopy(data, font_8, 8*256);
1222	fonts_loaded |= FONT_8;
1223	copy_font(LOAD, FONT_8, font_8);
1224	if (configuration & CHAR_CURSOR)
1225	    set_destructive_cursor(scp, TRUE);
1226	return 0;
1227
1228    case GIO_FONT8x8:   	/* get 8x8 dot font */
1229	if (!crtc_vga)
1230	    return ENXIO;
1231	if (fonts_loaded & FONT_8) {
1232	    bcopy(font_8, data, 8*256);
1233	    return 0;
1234	}
1235	else
1236	    return ENXIO;
1237
1238    case PIO_FONT8x14:  	/* set 8x14 dot font */
1239	if (!crtc_vga)
1240	    return ENXIO;
1241	bcopy(data, font_14, 14*256);
1242	fonts_loaded |= FONT_14;
1243	copy_font(LOAD, FONT_14, font_14);
1244	if (configuration & CHAR_CURSOR)
1245	    set_destructive_cursor(scp, TRUE);
1246	return 0;
1247
1248    case GIO_FONT8x14:  	/* get 8x14 dot font */
1249	if (!crtc_vga)
1250	    return ENXIO;
1251	if (fonts_loaded & FONT_14) {
1252	    bcopy(font_14, data, 14*256);
1253	    return 0;
1254	}
1255	else
1256	    return ENXIO;
1257
1258    case PIO_FONT8x16:  	/* set 8x16 dot font */
1259	if (!crtc_vga)
1260	    return ENXIO;
1261	bcopy(data, font_16, 16*256);
1262	fonts_loaded |= FONT_16;
1263	copy_font(LOAD, FONT_16, font_16);
1264	if (configuration & CHAR_CURSOR)
1265	    set_destructive_cursor(scp, TRUE);
1266	return 0;
1267
1268    case GIO_FONT8x16:  	/* get 8x16 dot font */
1269	if (!crtc_vga)
1270	    return ENXIO;
1271	if (fonts_loaded & FONT_16) {
1272	    bcopy(font_16, data, 16*256);
1273	    return 0;
1274	}
1275	else
1276	    return ENXIO;
1277    default:
1278	break;
1279    }
1280
1281    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1282    if (error >= 0)
1283	return(error);
1284    error = ttioctl(tp, cmd, data, flag);
1285    if (error >= 0)
1286	return(error);
1287    return(ENOTTY);
1288}
1289
1290static void
1291scstart(struct tty *tp)
1292{
1293    struct clist *rbp;
1294    int s, len;
1295    u_char buf[PCBURST];
1296    scr_stat *scp = get_scr_stat(tp->t_dev);
1297
1298    /* XXX who repeats the call when the above flags are cleared? */
1299    if (scp->status & SLKED || blink_in_progress)
1300	return;
1301    s = spltty();
1302    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1303	tp->t_state |= TS_BUSY;
1304	rbp = &tp->t_outq;
1305	while (rbp->c_cc) {
1306	    len = q_to_b(rbp, buf, PCBURST);
1307	    splx(s);
1308	    ansi_put(scp, buf, len);
1309	    s = spltty();
1310	}
1311	tp->t_state &= ~TS_BUSY;
1312	ttwwakeup(tp);
1313    }
1314    splx(s);
1315}
1316
1317void
1318sccnprobe(struct consdev *cp)
1319{
1320    struct isa_device *dvp;
1321
1322    /*
1323     * Take control if we are the highest priority enabled display device.
1324     */
1325    dvp = find_display();
1326    if (dvp != NULL && dvp->id_driver != &scdriver) {
1327	cp->cn_pri = CN_DEAD;
1328	return;
1329    }
1330
1331    /* initialize required fields */
1332    cp->cn_dev = makedev(CDEV_MAJOR, MAXCONS);
1333    cp->cn_pri = CN_INTERNAL;
1334}
1335
1336void
1337sccninit(struct consdev *cp)
1338{
1339    scinit();
1340}
1341
1342void
1343sccnputc(dev_t dev, int c)
1344{
1345    u_char buf[1];
1346    int s;
1347    scr_stat *scp = console[0];
1348    term_stat save = scp->term;
1349
1350    scp->term = kernel_console;
1351    current_default = &kernel_default;
1352    if (scp->scr_buf == Crtat)
1353	draw_cursor(scp, FALSE);
1354    buf[0] = c;
1355    ansi_put(scp, buf, 1);
1356    kernel_console = scp->term;
1357    current_default = &user_default;
1358    scp->term = save;
1359    s = splclock();
1360    if (scp == cur_console) {
1361	if (scp->scr_buf != Crtat && (scp->start <= scp->end)) {
1362	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1363		   (1 + scp->end - scp->start) * sizeof(u_short));
1364	    scp->start = scp->xsize * scp->ysize;
1365	    scp->end = 0;
1366	    scp->status &= ~CURSOR_SHOWN;
1367	}
1368	draw_cursor(scp, TRUE);
1369    }
1370    splx(s);
1371}
1372
1373int
1374sccngetc(dev_t dev)
1375{
1376    int s = spltty();       /* block scintr while we poll */
1377    int c = scgetc(0);
1378    splx(s);
1379    return(c);
1380}
1381
1382int
1383sccncheckc(dev_t dev)
1384{
1385    return (scgetc(1) & 0xff);
1386}
1387
1388static void
1389scrn_timer()
1390{
1391    static int cursor_blinkrate;
1392    scr_stat *scp = cur_console;
1393
1394    /* should we just return ? */
1395    if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
1396	timeout((timeout_func_t)scrn_timer, 0, hz/10);
1397	return;
1398    }
1399
1400    if (!scrn_blanked) {
1401	/* update screen image */
1402	if (scp->start <= scp->end) {
1403	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1404		   (1 + scp->end - scp->start) * sizeof(u_short));
1405	    scp->status &= ~CURSOR_SHOWN;
1406	    scp->start = scp->xsize * scp->ysize;
1407	    scp->end = 0;
1408	}
1409	/* update "pseudo" mouse arrow */
1410	if (scp->status & MOUSE_ENABLED)
1411	    draw_mouse_image(scp);
1412
1413	/* update cursor image */
1414	if (scp->status & CURSOR_ENABLED)
1415	    draw_cursor(scp,
1416		!(configuration&BLINK_CURSOR) || !(cursor_blinkrate++&0x04));
1417    }
1418    if (scrn_blank_time && (time.tv_sec>scrn_time_stamp+scrn_blank_time))
1419	(*current_saver)(TRUE);
1420    timeout((timeout_func_t)scrn_timer, 0, hz/25);
1421}
1422
1423static void
1424clear_screen(scr_stat *scp)
1425{
1426    move_crsr(scp, 0, 0);
1427    fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1428	  scp->xsize * scp->ysize);
1429    mark_all(scp);
1430}
1431
1432static int
1433switch_scr(scr_stat *scp, u_int next_scr)
1434{
1435    if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
1436	switch_in_progress = FALSE;
1437
1438    if (next_scr >= MAXCONS || switch_in_progress ||
1439	(cur_console->smode.mode == VT_AUTO
1440	 && cur_console->status & UNKNOWN_MODE)) {
1441	do_bell(scp, BELL_PITCH, BELL_DURATION);
1442	return EINVAL;
1443    }
1444
1445    /* is the wanted virtual console open ? */
1446    if (next_scr) {
1447	struct tty *tp = VIRTUAL_TTY(next_scr);
1448	if (!(tp->t_state & TS_ISOPEN)) {
1449	    do_bell(scp, BELL_PITCH, BELL_DURATION);
1450	    return EINVAL;
1451	}
1452    }
1453    /* delay switch if actively updating screen */
1454    if (write_in_progress || blink_in_progress) {
1455	delayed_next_scr = next_scr+1;
1456	return 0;
1457    }
1458    switch_in_progress = TRUE;
1459    old_scp = cur_console;
1460    new_scp = console[next_scr];
1461    wakeup((caddr_t)&new_scp->smode);
1462    if (new_scp == old_scp) {
1463	switch_in_progress = FALSE;
1464	delayed_next_scr = FALSE;
1465	return 0;
1466    }
1467
1468    /* has controlling process died? */
1469    if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1470	old_scp->smode.mode = VT_AUTO;
1471    if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1472	new_scp->smode.mode = VT_AUTO;
1473
1474    /* check the modes and switch approbiatly */
1475    if (old_scp->smode.mode == VT_PROCESS) {
1476	old_scp->status |= SWITCH_WAIT_REL;
1477	psignal(old_scp->proc, old_scp->smode.relsig);
1478    }
1479    else {
1480	exchange_scr();
1481	if (new_scp->smode.mode == VT_PROCESS) {
1482	    new_scp->status |= SWITCH_WAIT_ACQ;
1483	    psignal(new_scp->proc, new_scp->smode.acqsig);
1484	}
1485	else
1486	    switch_in_progress = FALSE;
1487    }
1488    return 0;
1489}
1490
1491static void
1492exchange_scr(void)
1493{
1494    move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1495    cur_console = new_scp;
1496    if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
1497	if (crtc_vga && video_mode_ptr)
1498	    set_mode(new_scp);
1499    }
1500    move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1501    if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1502	if (fonts_loaded & FONT_8)
1503	    copy_font(LOAD, FONT_8, font_8);
1504	if (fonts_loaded & FONT_14)
1505	    copy_font(LOAD, FONT_14, font_14);
1506	if (fonts_loaded & FONT_16)
1507	    copy_font(LOAD, FONT_16, font_16);
1508	if (configuration & CHAR_CURSOR)
1509	    set_destructive_cursor(new_scp, TRUE);
1510	load_palette();
1511    }
1512    if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1513	shfts = ctls = alts = agrs = metas = 0;
1514    update_leds(new_scp->status);
1515    delayed_next_scr = FALSE;
1516    mark_all(new_scp);
1517}
1518
1519static void
1520scan_esc(scr_stat *scp, u_char c)
1521{
1522    static u_char ansi_col[16] =
1523	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1524    int i, n;
1525    u_short *src, *dst, count;
1526
1527    if (scp->term.esc == 1) {
1528	switch (c) {
1529
1530	case '[':   /* Start ESC [ sequence */
1531	    scp->term.esc = 2;
1532	    scp->term.last_param = -1;
1533	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1534		scp->term.param[i] = 1;
1535	    scp->term.num_param = 0;
1536	    return;
1537
1538	case 'M':   /* Move cursor up 1 line, scroll if at top */
1539	    if (scp->ypos > 0)
1540		move_crsr(scp, scp->xpos, scp->ypos - 1);
1541	    else {
1542		bcopyw(scp->scr_buf, scp->scr_buf + scp->xsize,
1543		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
1544		fillw(scp->term.cur_color | scr_map[0x20],
1545		      scp->scr_buf, scp->xsize);
1546    		mark_all(scp);
1547	    }
1548	    break;
1549#if notyet
1550	case 'Q':
1551	    scp->term.esc = 4;
1552	    break;
1553#endif
1554	case 'c':   /* Clear screen & home */
1555	    clear_screen(scp);
1556	    break;
1557	}
1558    }
1559    else if (scp->term.esc == 2) {
1560	if (c >= '0' && c <= '9') {
1561	    if (scp->term.num_param < MAX_ESC_PAR) {
1562	    if (scp->term.last_param != scp->term.num_param) {
1563		scp->term.last_param = scp->term.num_param;
1564		scp->term.param[scp->term.num_param] = 0;
1565	    }
1566	    else
1567		scp->term.param[scp->term.num_param] *= 10;
1568	    scp->term.param[scp->term.num_param] += c - '0';
1569	    return;
1570	    }
1571	}
1572	scp->term.num_param = scp->term.last_param + 1;
1573	switch (c) {
1574
1575	case ';':
1576	    if (scp->term.num_param < MAX_ESC_PAR)
1577		return;
1578	    break;
1579
1580	case '=':
1581	    scp->term.esc = 3;
1582	    scp->term.last_param = -1;
1583	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1584		scp->term.param[i] = 1;
1585	    scp->term.num_param = 0;
1586	    return;
1587
1588	case 'A':   /* up n rows */
1589	    n = scp->term.param[0]; if (n < 1) n = 1;
1590	    move_crsr(scp, scp->xpos, scp->ypos - n);
1591	    break;
1592
1593	case 'B':   /* down n rows */
1594	    n = scp->term.param[0]; if (n < 1) n = 1;
1595	    move_crsr(scp, scp->xpos, scp->ypos + n);
1596	    break;
1597
1598	case 'C':   /* right n columns */
1599	    n = scp->term.param[0]; if (n < 1) n = 1;
1600	    move_crsr(scp, scp->xpos + n, scp->ypos);
1601	    break;
1602
1603	case 'D':   /* left n columns */
1604	    n = scp->term.param[0]; if (n < 1) n = 1;
1605	    move_crsr(scp, scp->xpos - n, scp->ypos);
1606	    break;
1607
1608	case 'E':   /* cursor to start of line n lines down */
1609	    n = scp->term.param[0]; if (n < 1) n = 1;
1610	    move_crsr(scp, 0, scp->ypos + n);
1611	    break;
1612
1613	case 'F':   /* cursor to start of line n lines up */
1614	    n = scp->term.param[0]; if (n < 1) n = 1;
1615	    move_crsr(scp, 0, scp->ypos - n);
1616	    break;
1617
1618	case 'f':   /* Cursor move */
1619	case 'H':
1620	    if (scp->term.num_param == 0)
1621		move_crsr(scp, 0, 0);
1622	    else if (scp->term.num_param == 2)
1623		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
1624	    break;
1625
1626	case 'J':   /* Clear all or part of display */
1627	    if (scp->term.num_param == 0)
1628		n = 0;
1629	    else
1630		n = scp->term.param[0];
1631	    switch (n) {
1632	    case 0: /* clear form cursor to end of display */
1633		fillw(scp->term.cur_color | scr_map[0x20],
1634		      scp->cursor_pos,
1635		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
1636    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1637    		mark_for_update(scp, scp->xsize * scp->ysize);
1638		break;
1639	    case 1: /* clear from beginning of display to cursor */
1640		fillw(scp->term.cur_color | scr_map[0x20],
1641		      scp->scr_buf,
1642		      scp->cursor_pos - scp->scr_buf);
1643    		mark_for_update(scp, 0);
1644    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1645		break;
1646	    case 2: /* clear entire display */
1647		clear_screen(scp);
1648		break;
1649	    }
1650	    break;
1651
1652	case 'K':   /* Clear all or part of line */
1653	    if (scp->term.num_param == 0)
1654		n = 0;
1655	    else
1656		n = scp->term.param[0];
1657	    switch (n) {
1658	    case 0: /* clear form cursor to end of line */
1659		fillw(scp->term.cur_color | scr_map[0x20],
1660		      scp->cursor_pos,
1661		      scp->xsize - scp->xpos);
1662    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1663    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
1664				scp->xsize - scp->xpos);
1665		break;
1666	    case 1: /* clear from beginning of line to cursor */
1667		fillw(scp->term.cur_color | scr_map[0x20],
1668		      scp->cursor_pos - scp->xpos,
1669		      scp->xpos + 1);
1670    		mark_for_update(scp, scp->ypos * scp->xsize);
1671    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1672		break;
1673	    case 2: /* clear entire line */
1674		fillw(scp->term.cur_color | scr_map[0x20],
1675		      scp->cursor_pos - scp->xpos,
1676		      scp->xsize);
1677    		mark_for_update(scp, scp->ypos * scp->xsize);
1678    		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
1679		break;
1680	    }
1681	    break;
1682
1683	case 'L':   /* Insert n lines */
1684	    n = scp->term.param[0]; if (n < 1) n = 1;
1685	    if (n > scp->ysize - scp->ypos)
1686		n = scp->ysize - scp->ypos;
1687	    src = scp->scr_buf + scp->ypos * scp->xsize;
1688	    dst = src + n * scp->xsize;
1689	    count = scp->ysize - (scp->ypos + n);
1690	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1691	    fillw(scp->term.cur_color | scr_map[0x20], src,
1692		  n * scp->xsize);
1693	    mark_for_update(scp, scp->ypos * scp->xsize);
1694	    mark_for_update(scp, scp->xsize * scp->ysize);
1695	    break;
1696
1697	case 'M':   /* Delete n lines */
1698	    n = scp->term.param[0]; if (n < 1) n = 1;
1699	    if (n > scp->ysize - scp->ypos)
1700		n = scp->ysize - scp->ypos;
1701	    dst = scp->scr_buf + scp->ypos * scp->xsize;
1702	    src = dst + n * scp->xsize;
1703	    count = scp->ysize - (scp->ypos + n);
1704	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1705	    src = dst + count * scp->xsize;
1706	    fillw(scp->term.cur_color | scr_map[0x20], src,
1707		  n * scp->xsize);
1708	    mark_for_update(scp, scp->ypos * scp->xsize);
1709	    mark_for_update(scp, scp->xsize * scp->ysize);
1710	    break;
1711
1712	case 'P':   /* Delete n chars */
1713	    n = scp->term.param[0]; if (n < 1) n = 1;
1714	    if (n > scp->xsize - scp->xpos)
1715		n = scp->xsize - scp->xpos;
1716	    dst = scp->cursor_pos;
1717	    src = dst + n;
1718	    count = scp->xsize - (scp->xpos + n);
1719	    bcopyw(src, dst, count * sizeof(u_short));
1720	    src = dst + count;
1721	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1722	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1723	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1724	    break;
1725
1726	case '@':   /* Insert n chars */
1727	    n = scp->term.param[0]; if (n < 1) n = 1;
1728	    if (n > scp->xsize - scp->xpos)
1729		n = scp->xsize - scp->xpos;
1730	    src = scp->cursor_pos;
1731	    dst = src + n;
1732	    count = scp->xsize - (scp->xpos + n);
1733	    bcopyw(src, dst, count * sizeof(u_short));
1734	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1735	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1736	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1737	    break;
1738
1739	case 'S':   /* scroll up n lines */
1740	    n = scp->term.param[0]; if (n < 1)  n = 1;
1741	    if (n > scp->ysize)
1742		n = scp->ysize;
1743	    bcopyw(scp->scr_buf + (scp->xsize * n),
1744		   scp->scr_buf,
1745		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
1746	    fillw(scp->term.cur_color | scr_map[0x20],
1747		  scp->scr_buf + scp->xsize * (scp->ysize - n),
1748		  scp->xsize * n);
1749    	    mark_all(scp);
1750	    break;
1751
1752	case 'T':   /* scroll down n lines */
1753	    n = scp->term.param[0]; if (n < 1)  n = 1;
1754	    if (n > scp->ysize)
1755		n = scp->ysize;
1756	    bcopyw(scp->scr_buf,
1757		  scp->scr_buf + (scp->xsize * n),
1758		  scp->xsize * (scp->ysize - n) *
1759		  sizeof(u_short));
1760	    fillw(scp->term.cur_color | scr_map[0x20],
1761		  scp->scr_buf, scp->xsize * n);
1762    	    mark_all(scp);
1763	    break;
1764
1765	case 'X':   /* erase n characters in line */
1766	    n = scp->term.param[0]; if (n < 1)  n = 1;
1767	    if (n > scp->xsize - scp->xpos)
1768		n = scp->xsize - scp->xpos;
1769	    fillw(scp->term.cur_color | scr_map[0x20],
1770		  scp->cursor_pos, n);
1771	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1772	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
1773	    break;
1774
1775	case 'Z':   /* move n tabs backwards */
1776	    n = scp->term.param[0]; if (n < 1)  n = 1;
1777	    if ((i = scp->xpos & 0xf8) == scp->xpos)
1778		i -= 8*n;
1779	    else
1780		i -= 8*(n-1);
1781	    if (i < 0)
1782		i = 0;
1783	    move_crsr(scp, i, scp->ypos);
1784	    break;
1785
1786	case '`':   /* move cursor to column n */
1787	    n = scp->term.param[0]; if (n < 1)  n = 1;
1788	    move_crsr(scp, n - 1, scp->ypos);
1789	    break;
1790
1791	case 'a':   /* move cursor n columns to the right */
1792	    n = scp->term.param[0]; if (n < 1)  n = 1;
1793	    move_crsr(scp, scp->xpos + n, scp->ypos);
1794	    break;
1795
1796	case 'd':   /* move cursor to row n */
1797	    n = scp->term.param[0]; if (n < 1)  n = 1;
1798	    move_crsr(scp, scp->xpos, n - 1);
1799	    break;
1800
1801	case 'e':   /* move cursor n rows down */
1802	    n = scp->term.param[0]; if (n < 1)  n = 1;
1803	    move_crsr(scp, scp->xpos, scp->ypos + n);
1804	    break;
1805
1806	case 'm':   /* change attribute */
1807	    if (scp->term.num_param == 0) {
1808		scp->term.attr_mask = NORMAL_ATTR;
1809		scp->term.cur_attr =
1810		    scp->term.cur_color = scp->term.std_color;
1811		break;
1812	    }
1813	    for (i = 0; i < scp->term.num_param; i++) {
1814		switch (n = scp->term.param[i]) {
1815		case 0: /* back to normal */
1816		    scp->term.attr_mask = NORMAL_ATTR;
1817		    scp->term.cur_attr =
1818			scp->term.cur_color = scp->term.std_color;
1819		    break;
1820		case 1: /* bold */
1821		    scp->term.attr_mask |= BOLD_ATTR;
1822		    scp->term.cur_attr = mask2attr(&scp->term);
1823		    break;
1824		case 4: /* underline */
1825		    scp->term.attr_mask |= UNDERLINE_ATTR;
1826		    scp->term.cur_attr = mask2attr(&scp->term);
1827		    break;
1828		case 5: /* blink */
1829		    scp->term.attr_mask |= BLINK_ATTR;
1830		    scp->term.cur_attr = mask2attr(&scp->term);
1831		    break;
1832		case 7: /* reverse video */
1833		    scp->term.attr_mask |= REVERSE_ATTR;
1834		    scp->term.cur_attr = mask2attr(&scp->term);
1835		    break;
1836		case 30: case 31: /* set fg color */
1837		case 32: case 33: case 34:
1838		case 35: case 36: case 37:
1839		    scp->term.attr_mask |= FOREGROUND_CHANGED;
1840		    scp->term.cur_color =
1841			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
1842		    scp->term.cur_attr = mask2attr(&scp->term);
1843		    break;
1844		case 40: case 41: /* set bg color */
1845		case 42: case 43: case 44:
1846		case 45: case 46: case 47:
1847		    scp->term.attr_mask |= BACKGROUND_CHANGED;
1848		    scp->term.cur_color =
1849			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
1850		    scp->term.cur_attr = mask2attr(&scp->term);
1851		    break;
1852		}
1853	    }
1854	    break;
1855
1856	case 'x':
1857	    if (scp->term.num_param == 0)
1858		n = 0;
1859	    else
1860		n = scp->term.param[0];
1861	    switch (n) {
1862	    case 0:     /* reset attributes */
1863		scp->term.attr_mask = NORMAL_ATTR;
1864		scp->term.cur_attr =
1865		    scp->term.cur_color = scp->term.std_color =
1866		    current_default->std_color;
1867		scp->term.rev_color = current_default->rev_color;
1868		break;
1869	    case 1:     /* set ansi background */
1870		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
1871		scp->term.cur_color = scp->term.std_color =
1872		    (scp->term.std_color & 0x0F00) |
1873		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
1874		scp->term.cur_attr = mask2attr(&scp->term);
1875		break;
1876	    case 2:     /* set ansi foreground */
1877		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
1878		scp->term.cur_color = scp->term.std_color =
1879		    (scp->term.std_color & 0xF000) |
1880		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
1881		scp->term.cur_attr = mask2attr(&scp->term);
1882		break;
1883	    case 3:     /* set ansi attribute directly */
1884		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
1885		scp->term.cur_color = scp->term.std_color =
1886		    (scp->term.param[1]&0xFF)<<8;
1887		scp->term.cur_attr = mask2attr(&scp->term);
1888		break;
1889	    case 5:     /* set ansi reverse video background */
1890		scp->term.rev_color =
1891		    (scp->term.rev_color & 0x0F00) |
1892		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
1893		scp->term.cur_attr = mask2attr(&scp->term);
1894		break;
1895	    case 6:     /* set ansi reverse video foreground */
1896		scp->term.rev_color =
1897		    (scp->term.rev_color & 0xF000) |
1898		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
1899		scp->term.cur_attr = mask2attr(&scp->term);
1900		break;
1901	    case 7:     /* set ansi reverse video directly */
1902		scp->term.rev_color =
1903		    (scp->term.param[1]&0xFF)<<8;
1904		scp->term.cur_attr = mask2attr(&scp->term);
1905		break;
1906	    }
1907	    break;
1908
1909	case 'z':   /* switch to (virtual) console n */
1910	    if (scp->term.num_param == 1)
1911		switch_scr(scp, scp->term.param[0]);
1912	    break;
1913	}
1914    }
1915    else if (scp->term.esc == 3) {
1916	if (c >= '0' && c <= '9') {
1917	    if (scp->term.num_param < MAX_ESC_PAR) {
1918	    if (scp->term.last_param != scp->term.num_param) {
1919		scp->term.last_param = scp->term.num_param;
1920		scp->term.param[scp->term.num_param] = 0;
1921	    }
1922	    else
1923		scp->term.param[scp->term.num_param] *= 10;
1924	    scp->term.param[scp->term.num_param] += c - '0';
1925	    return;
1926	    }
1927	}
1928	scp->term.num_param = scp->term.last_param + 1;
1929	switch (c) {
1930
1931	case ';':
1932	    if (scp->term.num_param < MAX_ESC_PAR)
1933		return;
1934	    break;
1935
1936	case 'A':   /* set display border color */
1937	    if (scp->term.num_param == 1)
1938		scp->border=scp->term.param[0] & 0xff;
1939		if (scp == cur_console)
1940		    set_border(scp->border);
1941	    break;
1942
1943	case 'B':   /* set bell pitch and duration */
1944	    if (scp->term.num_param == 2) {
1945		scp->bell_pitch = scp->term.param[0];
1946		scp->bell_duration = scp->term.param[1]*10;
1947	    }
1948	    break;
1949
1950	case 'C':   /* set cursor type & shape */
1951	    if (scp->term.num_param == 1) {
1952		if (scp->term.param[0] & 0x01)
1953		    configuration |= BLINK_CURSOR;
1954		else
1955		    configuration &= ~BLINK_CURSOR;
1956		if (scp->term.param[0] & 0x02) {
1957		    configuration |= CHAR_CURSOR;
1958		    set_destructive_cursor(scp, TRUE);
1959		} else
1960		    configuration &= ~CHAR_CURSOR;
1961	    }
1962	    else if (scp->term.num_param == 2) {
1963		scp->cursor_start = scp->term.param[0] & 0x1F;
1964		scp->cursor_end = scp->term.param[1] & 0x1F;
1965		if (configuration & CHAR_CURSOR)
1966			set_destructive_cursor(scp, TRUE);
1967	    }
1968	    break;
1969
1970	case 'F':   /* set ansi foreground */
1971	    if (scp->term.num_param == 1) {
1972		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
1973		scp->term.cur_color = scp->term.std_color =
1974		    (scp->term.std_color & 0xF000)
1975		    | ((scp->term.param[0] & 0x0F) << 8);
1976		scp->term.cur_attr = mask2attr(&scp->term);
1977	    }
1978	    break;
1979
1980	case 'G':   /* set ansi background */
1981	    if (scp->term.num_param == 1) {
1982		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
1983		scp->term.cur_color = scp->term.std_color =
1984		    (scp->term.std_color & 0x0F00)
1985		    | ((scp->term.param[0] & 0x0F) << 12);
1986		scp->term.cur_attr = mask2attr(&scp->term);
1987	    }
1988	    break;
1989
1990	case 'H':   /* set ansi reverse video foreground */
1991	    if (scp->term.num_param == 1) {
1992		scp->term.rev_color =
1993		    (scp->term.rev_color & 0xF000)
1994		    | ((scp->term.param[0] & 0x0F) << 8);
1995		scp->term.cur_attr = mask2attr(&scp->term);
1996	    }
1997	    break;
1998
1999	case 'I':   /* set ansi reverse video background */
2000	    if (scp->term.num_param == 1) {
2001		scp->term.rev_color =
2002		    (scp->term.rev_color & 0x0F00)
2003		    | ((scp->term.param[0] & 0x0F) << 12);
2004		scp->term.cur_attr = mask2attr(&scp->term);
2005	    }
2006	    break;
2007	}
2008    }
2009    scp->term.esc = 0;
2010}
2011
2012static void
2013ansi_put(scr_stat *scp, u_char *buf, int len)
2014{
2015    u_char *ptr = buf;
2016
2017    if (scp->status & UNKNOWN_MODE)
2018	return;
2019
2020    /* make screensaver happy */
2021    if (scp == cur_console) {
2022	scrn_time_stamp = time.tv_sec;
2023	if (scrn_blanked) {
2024	    (*current_saver)(FALSE);
2025	    cur_console->start = 0;
2026	    cur_console->end = cur_console->xsize * cur_console->ysize;
2027	}
2028    }
2029    write_in_progress++;
2030outloop:
2031    if (scp->term.esc) {
2032	scan_esc(scp, *ptr++);
2033	len--;
2034    }
2035    else if (PRINTABLE(*ptr)) {     /* Print only printables */
2036 	int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
2037 	u_short cur_attr = scp->term.cur_attr;
2038 	u_short *cursor_pos = scp->cursor_pos;
2039	do {
2040	    /*
2041	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
2042	     * pointers in the following to volatile should have no effect,
2043	     * but in fact speeds up this inner loop from 26 to 18 cycles
2044	     * (+ cache misses) on i486's.
2045	     */
2046#define	UCVP(ucp)	((u_char volatile *)(ucp))
2047	    *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
2048	    ptr++;
2049	    cnt--;
2050	} while (cnt && PRINTABLE(*ptr));
2051	len -= (cursor_pos - scp->cursor_pos);
2052	scp->xpos += (cursor_pos - scp->cursor_pos);
2053	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2054	mark_for_update(scp, cursor_pos - scp->scr_buf);
2055	scp->cursor_pos = cursor_pos;
2056	if (scp->xpos >= scp->xsize) {
2057	    scp->xpos = 0;
2058	    scp->ypos++;
2059	}
2060    }
2061    else  {
2062	switch(*ptr) {
2063	case 0x07:
2064	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
2065	    break;
2066
2067	case 0x08:      /* non-destructive backspace */
2068	    if (scp->cursor_pos > scp->scr_buf) {
2069	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2070		scp->cursor_pos--;
2071	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2072		if (scp->xpos > 0)
2073		    scp->xpos--;
2074		else {
2075		    scp->xpos += scp->xsize - 1;
2076		    scp->ypos--;
2077		}
2078	    }
2079	    break;
2080
2081	case 0x09:  /* non-destructive tab */
2082	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2083	    scp->cursor_pos += (8 - scp->xpos % 8u);
2084	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2085	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
2086	        scp->xpos = 0;
2087	        scp->ypos++;
2088	    }
2089	    break;
2090
2091	case 0x0a:  /* newline, same pos */
2092	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2093	    scp->cursor_pos += scp->xsize;
2094	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2095	    scp->ypos++;
2096	    break;
2097
2098	case 0x0c:  /* form feed, clears screen */
2099	    clear_screen(scp);
2100	    break;
2101
2102	case 0x0d:  /* return, return to pos 0 */
2103	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2104	    scp->cursor_pos -= scp->xpos;
2105	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2106	    scp->xpos = 0;
2107	    break;
2108
2109	case 0x1b:  /* start escape sequence */
2110	    scp->term.esc = 1;
2111	    scp->term.num_param = 0;
2112	    break;
2113	}
2114	ptr++; len--;
2115    }
2116    /* do we have to scroll ?? */
2117    if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
2118	reverse_mouse_cut(scp, 0);
2119	scp->mouse_cut_start = scp->mouse_cut_end = NULL;
2120	if (scp->history) {
2121	    bcopyw(scp->scr_buf, scp->history_head,
2122		   scp->xsize * sizeof(u_short));
2123	    scp->history_head += scp->xsize;
2124	    if (scp->history_head + scp->xsize >
2125		scp->history + scp->history_size)
2126		scp->history_head = scp->history;
2127	}
2128	bcopyw(scp->scr_buf + scp->xsize, scp->scr_buf,
2129	       scp->xsize * (scp->ysize - 1) * sizeof(u_short));
2130	fillw(scp->term.cur_color | scr_map[0x20],
2131	      scp->scr_buf + scp->xsize * (scp->ysize - 1),
2132	      scp->xsize);
2133	scp->cursor_pos -= scp->xsize;
2134	scp->ypos--;
2135    	mark_all(scp);
2136    }
2137    if (len)
2138	goto outloop;
2139    write_in_progress--;
2140    if (delayed_next_scr)
2141	switch_scr(scp, delayed_next_scr - 1);
2142}
2143
2144static void
2145scinit(void)
2146{
2147    u_short volatile *cp;
2148    u_short was;
2149    unsigned hw_cursor;
2150    int i;
2151
2152    if (init_done)
2153	return;
2154    init_done = TRUE;
2155    /*
2156     * Finish defaulting crtc variables for a mono screen.  Crtat is a
2157     * bogus common variable so that it can be shared with pcvt, so it
2158     * can't be statically initialized.  XXX.
2159     */
2160     Crtat = (u_short *)MONO_BUF;
2161    /*
2162     * If CGA memory seems to work, switch to color.
2163     */
2164    cp = (u_short *)CGA_BUF;
2165    was = *cp;
2166    *cp = (u_short) 0xA55A;
2167    if (*cp == 0xA55A) {
2168	Crtat = (u_short *)cp;
2169	crtc_addr = COLOR_BASE;
2170    }
2171    *cp = was;
2172
2173    /*
2174     * Ensure a zero start address.  This is mainly to recover after
2175     * switching from pcvt using userconfig().  The registers are w/o
2176     * for old hardware so it's too hard to relocate the active screen
2177     * memory.
2178     */
2179    outb(crtc_addr, 12);
2180    outb(crtc_addr + 1, 0);
2181    outb(crtc_addr, 13);
2182    outb(crtc_addr + 1, 0);
2183
2184    /* extract cursor location */
2185    outb(crtc_addr, 14);
2186    hw_cursor = inb(crtc_addr + 1) << 8;
2187    outb(crtc_addr, 15);
2188    hw_cursor |= inb(crtc_addr + 1);
2189
2190    /* move hardware cursor out of the way */
2191    outb(crtc_addr, 14);
2192    outb(crtc_addr + 1, 0xff);
2193    outb(crtc_addr, 15);
2194    outb(crtc_addr + 1, 0xff);
2195
2196    /* is this a VGA or higher ? */
2197    outb(crtc_addr, 7);
2198    if (inb(crtc_addr) == 7) {
2199	u_long  pa;
2200	u_long  segoff;
2201
2202	crtc_vga = TRUE;
2203	/*
2204	 * Get the BIOS video mode pointer.
2205	 */
2206	segoff = *(u_long *)pa_to_va(0x4a8);
2207	pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2208	if (ISMAPPED(pa, sizeof(u_long))) {
2209	    segoff = *(u_long *)pa_to_va(pa);
2210	    pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2211	    if (ISMAPPED(pa, 64))
2212		video_mode_ptr = (char *)pa_to_va(pa);
2213	}
2214    }
2215    current_default = &user_default;
2216    console[0] = &main_console;
2217    init_scp(console[0]);
2218    console[0]->scr_buf = console[0]->mouse_pos =  Crtat;
2219    console[0]->cursor_pos = Crtat + hw_cursor;
2220    console[0]->xpos = hw_cursor % COL;
2221    console[0]->ypos = hw_cursor / COL;
2222    cur_console = console[0];
2223    for (i=1; i<MAXCONS; i++)
2224	console[i] = NULL;
2225    kernel_console.esc = 0;
2226    kernel_console.attr_mask = NORMAL_ATTR;
2227    kernel_console.cur_attr =
2228	kernel_console.cur_color = kernel_console.std_color =
2229	kernel_default.std_color;
2230    kernel_console.rev_color = kernel_default.rev_color;
2231    /* initialize mapscrn array to a one to one map */
2232    for (i=0; i<sizeof(scr_map); i++)
2233	scr_map[i] = i;
2234}
2235
2236static scr_stat
2237*alloc_scp()
2238{
2239    scr_stat *scp;
2240
2241    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2242    init_scp(scp);
2243    scp->scr_buf = scp->cursor_pos = scp->mouse_pos =
2244	(u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
2245			  M_DEVBUF, M_WAITOK);
2246    scp->history_head = scp->history_pos = scp->history =
2247	(u_short *)malloc(scp->history_size*sizeof(u_short),
2248			  M_DEVBUF, M_WAITOK);
2249    bzero(scp->history_head, scp->history_size*sizeof(u_short));
2250    if (crtc_vga && video_mode_ptr)
2251	set_mode(scp);
2252    clear_screen(scp);
2253    return scp;
2254}
2255
2256static void
2257init_scp(scr_stat *scp)
2258{
2259    scp->mode = M_VGA_C80x25;
2260    scp->font = FONT_16;
2261    scp->xsize = COL;
2262    scp->ysize = ROW;
2263    scp->start = COL * ROW;
2264    scp->end = 0;
2265    scp->term.esc = 0;
2266    scp->term.attr_mask = NORMAL_ATTR;
2267    scp->term.cur_attr =
2268	scp->term.cur_color = scp->term.std_color =
2269	current_default->std_color;
2270    scp->term.rev_color = current_default->rev_color;
2271    scp->border = BG_BLACK;
2272    scp->cursor_start = *(char *)pa_to_va(0x461);
2273    scp->cursor_end = *(char *)pa_to_va(0x460);
2274    scp->mouse_xpos = scp->mouse_ypos = 0;
2275    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
2276    scp->bell_pitch = BELL_PITCH;
2277    scp->bell_duration = BELL_DURATION;
2278    scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
2279    scp->status |= CURSOR_ENABLED;
2280    scp->pid = 0;
2281    scp->proc = NULL;
2282    scp->smode.mode = VT_AUTO;
2283    scp->history_head = scp->history_pos = scp->history = NULL;
2284    scp->history_size = HISTORY_SIZE;
2285}
2286
2287static u_char
2288*get_fstr(u_int c, u_int *len)
2289{
2290    u_int i;
2291
2292    if (!(c & FKEY))
2293	return(NULL);
2294    i = (c & 0xFF) - F_FN;
2295    if (i > n_fkey_tab)
2296	return(NULL);
2297    *len = fkey_tab[i].len;
2298    return(fkey_tab[i].str);
2299}
2300
2301static void
2302update_leds(int which)
2303{
2304    int s;
2305    static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
2306
2307    /* replace CAPS led with ALTGR led for ALTGR keyboards */
2308    if (key_map.n_keys > ALTGR_OFFSET) {
2309	if (which & ALKED)
2310	    which |= CLKED;
2311	else
2312	    which &= ~CLKED;
2313    }
2314    s = spltty();
2315    kbd_cmd(KB_SETLEDS);
2316    kbd_cmd(xlate_leds[which & LED_MASK]);
2317    splx(s);
2318}
2319
2320static void
2321history_to_screen(scr_stat *scp)
2322{
2323    int i;
2324
2325    for (i=0; i<scp->ysize; i++)
2326	bcopyw(scp->history + (((scp->history_pos - scp->history) +
2327	       scp->history_size-((i+1)*scp->xsize))%scp->history_size),
2328	       scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
2329	       scp->xsize * sizeof(u_short));
2330    mark_all(scp);
2331}
2332
2333static int
2334history_up_line(scr_stat *scp)
2335{
2336    if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
2337	scp->history_head) {
2338	scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
2339	history_to_screen(scp);
2340	return 0;
2341    }
2342    else
2343	return -1;
2344}
2345
2346static int
2347history_down_line(scr_stat *scp)
2348{
2349    if (scp->history_pos != scp->history_head) {
2350	scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
2351	history_to_screen(scp);
2352	return 0;
2353    }
2354    else
2355	return -1;
2356}
2357
2358/*
2359 * scgetc(noblock) - get character from keyboard.
2360 * If noblock = 0 wait until a key is pressed.
2361 * Else return NOKEY.
2362 */
2363u_int
2364scgetc(int noblock)
2365{
2366    u_char scancode, keycode;
2367    u_int state, action;
2368    struct key_t *key;
2369    static u_char esc_flag = 0, compose = 0;
2370    static u_int chr = 0;
2371
2372next_code:
2373    kbd_wait();
2374    /* First see if there is something in the keyboard port */
2375    if (inb(KB_STAT) & KB_BUF_FULL)
2376	scancode = inb(KB_DATA);
2377    else if (noblock)
2378	return(NOKEY);
2379    else
2380	goto next_code;
2381
2382    add_keyboard_randomness(scancode);
2383
2384    if (cur_console->status & KBD_RAW_MODE)
2385	return scancode;
2386
2387    keycode = scancode & 0x7F;
2388    switch (esc_flag) {
2389    case 0x00:      /* normal scancode */
2390	switch(scancode) {
2391	case 0xB8:  /* left alt  (compose key) */
2392	    if (compose) {
2393		compose = 0;
2394		if (chr > 255) {
2395		    do_bell(cur_console,
2396			BELL_PITCH, BELL_DURATION);
2397		    chr = 0;
2398		}
2399	    }
2400	    break;
2401	case 0x38:
2402	    if (!compose) {
2403		compose = 1;
2404		chr = 0;
2405	    }
2406	    break;
2407	case 0xE0:
2408	case 0xE1:
2409	    esc_flag = scancode;
2410	    goto next_code;
2411	}
2412	break;
2413    case 0xE0:      /* 0xE0 prefix */
2414	esc_flag = 0;
2415	switch (keycode) {
2416	case 0x1C:  /* right enter key */
2417	    keycode = 0x59;
2418	    break;
2419	case 0x1D:  /* right ctrl key */
2420	    keycode = 0x5A;
2421	    break;
2422	case 0x35:  /* keypad divide key */
2423	    keycode = 0x5B;
2424	    break;
2425	case 0x37:  /* print scrn key */
2426	    keycode = 0x5C;
2427	    break;
2428	case 0x38:  /* right alt key (alt gr) */
2429	    keycode = 0x5D;
2430	    break;
2431	case 0x47:  /* grey home key */
2432	    keycode = 0x5E;
2433	    break;
2434	case 0x48:  /* grey up arrow key */
2435	    keycode = 0x5F;
2436	    break;
2437	case 0x49:  /* grey page up key */
2438	    keycode = 0x60;
2439	    break;
2440	case 0x4B:  /* grey left arrow key */
2441	    keycode = 0x61;
2442	    break;
2443	case 0x4D:  /* grey right arrow key */
2444	    keycode = 0x62;
2445	    break;
2446	case 0x4F:  /* grey end key */
2447	    keycode = 0x63;
2448	    break;
2449	case 0x50:  /* grey down arrow key */
2450	    keycode = 0x64;
2451	    break;
2452	case 0x51:  /* grey page down key */
2453	    keycode = 0x65;
2454	    break;
2455	case 0x52:  /* grey insert key */
2456	    keycode = 0x66;
2457	    break;
2458	case 0x53:  /* grey delete key */
2459	    keycode = 0x67;
2460	    break;
2461
2462	/* the following 3 are only used on the MS "Natural" keyboard */
2463	case 0x5b:  /* left Window key */
2464	    keycode = 0x69;
2465	    break;
2466	case 0x5c:  /* right Window key */
2467	    keycode = 0x6a;
2468	    break;
2469	case 0x5d:  /* menu key */
2470	    keycode = 0x6b;
2471	    break;
2472	default:    /* ignore everything else */
2473	    goto next_code;
2474	}
2475	break;
2476    case 0xE1:      /* 0xE1 prefix */
2477	esc_flag = 0;
2478	if (keycode == 0x1D)
2479	    esc_flag = 0x1D;
2480	goto next_code;
2481	/* NOT REACHED */
2482    case 0x1D:      /* pause / break */
2483	esc_flag = 0;
2484	if (keycode != 0x45)
2485	    goto next_code;
2486	keycode = 0x68;
2487	break;
2488    }
2489
2490    /* if scroll-lock pressed allow history browsing */
2491    if (cur_console->history && cur_console->status & SLKED) {
2492	int i;
2493
2494	cur_console->status &= ~CURSOR_ENABLED;
2495	if (!(cur_console->status & BUFFER_SAVED)) {
2496	    cur_console->status |= BUFFER_SAVED;
2497	    cur_console->history_save = cur_console->history_head;
2498
2499	    /* copy screen into top of history buffer */
2500	    for (i=0; i<cur_console->ysize; i++) {
2501		bcopyw(cur_console->scr_buf + (cur_console->xsize * i),
2502		       cur_console->history_head,
2503		       cur_console->xsize * sizeof(u_short));
2504		cur_console->history_head += cur_console->xsize;
2505		if (cur_console->history_head + cur_console->xsize >
2506		    cur_console->history + cur_console->history_size)
2507		    cur_console->history_head=cur_console->history;
2508	    }
2509	    cur_console->history_pos = cur_console->history_head;
2510	    history_to_screen(cur_console);
2511	}
2512	switch (scancode) {
2513	case 0x47:  /* home key */
2514	    cur_console->history_pos = cur_console->history_head;
2515	    history_to_screen(cur_console);
2516	    goto next_code;
2517
2518	case 0x4F:  /* end key */
2519	    cur_console->history_pos =
2520		WRAPHIST(cur_console, cur_console->history_head,
2521			 cur_console->xsize*cur_console->ysize);
2522	    history_to_screen(cur_console);
2523	    goto next_code;
2524
2525	case 0x48:  /* up arrow key */
2526	    if (history_up_line(cur_console))
2527		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2528	    goto next_code;
2529
2530	case 0x50:  /* down arrow key */
2531	    if (history_down_line(cur_console))
2532		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2533	    goto next_code;
2534
2535	case 0x49:  /* page up key */
2536	    for (i=0; i<cur_console->ysize; i++)
2537	    if (history_up_line(cur_console)) {
2538		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2539		break;
2540	    }
2541	    goto next_code;
2542
2543	case 0x51:  /* page down key */
2544	    for (i=0; i<cur_console->ysize; i++)
2545	    if (history_down_line(cur_console)) {
2546		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2547		break;
2548	    }
2549	    goto next_code;
2550	}
2551    }
2552
2553    if (compose) {
2554	switch (scancode) {
2555	/* key pressed process it */
2556	case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
2557	    chr = (scancode - 0x40) + chr*10;
2558	    goto next_code;
2559	case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
2560	    chr = (scancode - 0x47) + chr*10;
2561	    goto next_code;
2562	case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
2563	    chr = (scancode - 0x4E) + chr*10;
2564	    goto next_code;
2565	case 0x52:              /* keypad 0 */
2566	    chr *= 10;
2567	    goto next_code;
2568
2569	/* key release, no interest here */
2570	case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
2571	case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
2572	case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
2573	case 0xD2:              /* keypad 0 */
2574	    goto next_code;
2575
2576	case 0x38:              /* left alt key */
2577	    break;
2578	default:
2579	    if (chr) {
2580		compose = chr = 0;
2581		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2582		goto next_code;
2583	    }
2584	    break;
2585	}
2586    }
2587
2588    state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
2589    if ((!agrs && (cur_console->status & ALKED))
2590	|| (agrs && !(cur_console->status & ALKED)))
2591	keycode += ALTGR_OFFSET;
2592    key = &key_map.key[keycode];
2593    if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
2594	 || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
2595	state ^= 1;
2596
2597    /* Check for make/break */
2598    action = key->map[state];
2599    if (scancode & 0x80) {      /* key released */
2600	if (key->spcl & 0x80) {
2601	    switch (action) {
2602	    case LSH:
2603		shfts &= ~1;
2604		break;
2605	    case RSH:
2606		shfts &= ~2;
2607		break;
2608	    case LCTR:
2609		ctls &= ~1;
2610		break;
2611	    case RCTR:
2612		ctls &= ~2;
2613		break;
2614	    case LALT:
2615		alts &= ~1;
2616		break;
2617	    case RALT:
2618		alts &= ~2;
2619		break;
2620	    case NLK:
2621		nlkcnt = 0;
2622		break;
2623	    case CLK:
2624		clkcnt = 0;
2625		break;
2626	    case SLK:
2627		slkcnt = 0;
2628		break;
2629	    case ASH:
2630		agrs = 0;
2631		break;
2632	    case ALK:
2633		alkcnt = 0;
2634		break;
2635	    case META:
2636		metas = 0;
2637		break;
2638	    }
2639	}
2640	if (chr && !compose) {
2641	    action = chr;
2642	    chr = 0;
2643	    return(action);
2644	}
2645    } else {
2646	/* key pressed */
2647	if (key->spcl & (0x80>>state)) {
2648	    switch (action) {
2649	    /* LOCKING KEYS */
2650	    case NLK:
2651		if (!nlkcnt) {
2652		    nlkcnt++;
2653		    if (cur_console->status & NLKED)
2654			cur_console->status &= ~NLKED;
2655		    else
2656			cur_console->status |= NLKED;
2657		    update_leds(cur_console->status);
2658		}
2659		break;
2660	    case CLK:
2661		if (!clkcnt) {
2662		    clkcnt++;
2663		    if (cur_console->status & CLKED)
2664			cur_console->status &= ~CLKED;
2665		    else
2666			cur_console->status |= CLKED;
2667		    update_leds(cur_console->status);
2668		}
2669		break;
2670	    case SLK:
2671		if (!slkcnt) {
2672		    slkcnt++;
2673		    if (cur_console->status & SLKED) {
2674			cur_console->status &= ~SLKED;
2675			if (cur_console->status & BUFFER_SAVED){
2676			    int i;
2677			    u_short *ptr = cur_console->history_save;
2678
2679			    for (i=0; i<cur_console->ysize; i++) {
2680				bcopyw(ptr,
2681				       cur_console->scr_buf +
2682				       (cur_console->xsize*i),
2683				       cur_console->xsize * sizeof(u_short));
2684				ptr += cur_console->xsize;
2685				if (ptr + cur_console->xsize >
2686				    cur_console->history +
2687				    cur_console->history_size)
2688				    ptr = cur_console->history;
2689			    }
2690			    cur_console->status &= ~BUFFER_SAVED;
2691			    cur_console->history_head=cur_console->history_save;
2692			    cur_console->status |= CURSOR_ENABLED;
2693			    mark_all(cur_console);
2694			}
2695			scstart(VIRTUAL_TTY(get_scr_num()));
2696		    }
2697		    else
2698			cur_console->status |= SLKED;
2699		    update_leds(cur_console->status);
2700		}
2701		break;
2702	    case ALK:
2703		if (!alkcnt) {
2704		    alkcnt++;
2705		    if (cur_console->status & ALKED)
2706			cur_console->status &= ~ALKED;
2707		    else
2708			cur_console->status |= ALKED;
2709		    update_leds(cur_console->status);
2710		}
2711		break;
2712
2713	    /* NON-LOCKING KEYS */
2714	    case NOP:
2715		break;
2716	    case RBT:
2717		shutdown_nice();
2718		break;
2719	    case SUSP:
2720#if NAPM > 0
2721		apm_suspend();
2722#endif
2723		break;
2724
2725	    case DBG:
2726#ifdef DDB          /* try to switch to console 0 */
2727		if (cur_console->smode.mode == VT_AUTO &&
2728		    console[0]->smode.mode == VT_AUTO)
2729		    switch_scr(cur_console, 0);
2730		Debugger("manual escape to debugger");
2731		return(NOKEY);
2732#else
2733		printf("No debugger in kernel\n");
2734#endif
2735		break;
2736	    case LSH:
2737		shfts |= 1;
2738		break;
2739	    case RSH:
2740		shfts |= 2;
2741		break;
2742	    case LCTR:
2743		ctls |= 1;
2744		break;
2745	    case RCTR:
2746		ctls |= 2;
2747		break;
2748	    case LALT:
2749		alts |= 1;
2750		break;
2751	    case RALT:
2752		alts |= 2;
2753		break;
2754	    case ASH:
2755		agrs = 1;
2756		break;
2757	    case META:
2758		metas = 1;
2759		break;
2760	    case NEXT:
2761		switch_scr(cur_console, (get_scr_num() + 1) % MAXCONS);
2762		break;
2763	    case BTAB:
2764		return(BKEY);
2765	    default:
2766		if (action >= F_SCR && action <= L_SCR) {
2767		    switch_scr(cur_console, action - F_SCR);
2768		    break;
2769		}
2770		if (action >= F_FN && action <= L_FN)
2771		    action |= FKEY;
2772		return(action);
2773	    }
2774	}
2775	else {
2776	    if (metas)
2777		action |= MKEY;
2778	    return(action);
2779	}
2780    }
2781    goto next_code;
2782}
2783
2784int
2785scmmap(dev_t dev, int offset, int nprot)
2786{
2787    if (offset > 0x20000 - PAGE_SIZE)
2788	return -1;
2789    return i386_btop((VIDEOMEM + offset));
2790}
2791
2792static void
2793kbd_wait(void)
2794{
2795    int i = 500;
2796
2797    while (i--) {
2798	if ((inb(KB_STAT) & KB_READY) == 0)
2799	    break;
2800	DELAY (25);
2801    }
2802}
2803
2804static void
2805kbd_cmd(u_char command)
2806{
2807    int i, retry = 5;
2808    do {
2809	kbd_wait();
2810	outb(KB_DATA, command);
2811	i = 50000;
2812	while (i--) {
2813	    if (inb(KB_STAT) & KB_BUF_FULL) {
2814		int val;
2815		DELAY(25);
2816		val = inb(KB_DATA);
2817		if (val == KB_ACK)
2818		    return;
2819		if (val == KB_RESEND)
2820		    break;
2821	    }
2822	}
2823    } while (retry--);
2824}
2825
2826static void
2827set_mode(scr_stat *scp)
2828{
2829    char *modetable;
2830    char special_modetable[64];
2831    int font_size;
2832
2833    if (scp != cur_console)
2834	return;
2835
2836    /* setup video hardware for the given mode */
2837    switch (scp->mode) {
2838    case M_VGA_M80x60:
2839	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2840	goto special_80x60;
2841
2842    case M_VGA_C80x60:
2843	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2844special_80x60:
2845	special_modetable[2]  = 0x08;
2846	special_modetable[19] = 0x47;
2847	goto special_480l;
2848
2849    case M_VGA_M80x30:
2850	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2851	goto special_80x30;
2852
2853    case M_VGA_C80x30:
2854	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2855special_80x30:
2856	special_modetable[19] = 0x4f;
2857special_480l:
2858	special_modetable[9] |= 0xc0;
2859	special_modetable[16] = 0x08;
2860	special_modetable[17] = 0x3e;
2861	special_modetable[26] = 0xea;
2862	special_modetable[28] = 0xdf;
2863	special_modetable[31] = 0xe7;
2864	special_modetable[32] = 0x04;
2865	modetable = special_modetable;
2866	goto setup_mode;
2867
2868    case M_ENH_B80x43:
2869	bcopyw(video_mode_ptr+(64*M_ENH_B80x25),&special_modetable, 64);
2870	goto special_80x43;
2871
2872    case M_ENH_C80x43:
2873	bcopyw(video_mode_ptr+(64*M_ENH_C80x25),&special_modetable, 64);
2874special_80x43:
2875	special_modetable[28] = 87;
2876	goto special_80x50;
2877
2878    case M_VGA_M80x50:
2879	bcopyw(video_mode_ptr+(64*M_VGA_M80x25),&special_modetable, 64);
2880	goto special_80x50;
2881
2882    case M_VGA_C80x50:
2883	bcopyw(video_mode_ptr+(64*M_VGA_C80x25),&special_modetable, 64);
2884special_80x50:
2885	special_modetable[2] = 8;
2886	special_modetable[19] = 7;
2887	modetable = special_modetable;
2888	goto setup_mode;
2889
2890    case M_VGA_C40x25: case M_VGA_C80x25:
2891    case M_VGA_M80x25:
2892    case M_B40x25:     case M_C40x25:
2893    case M_B80x25:     case M_C80x25:
2894    case M_ENH_B40x25: case M_ENH_C40x25:
2895    case M_ENH_B80x25: case M_ENH_C80x25:
2896
2897	modetable = video_mode_ptr + (scp->mode * 64);
2898setup_mode:
2899	set_vgaregs(modetable);
2900	font_size = *(modetable + 2);
2901
2902	/* set font type (size) */
2903	switch (font_size) {
2904	case 0x10:
2905	    outb(TSIDX, 0x03); outb(TSREG, 0x00);   /* font 0 */
2906	    scp->font = FONT_16;
2907	    break;
2908	case 0x0E:
2909	    outb(TSIDX, 0x03); outb(TSREG, 0x05);   /* font 1 */
2910	    scp->font = FONT_14;
2911	    break;
2912	default:
2913	case 0x08:
2914	    outb(TSIDX, 0x03); outb(TSREG, 0x0A);   /* font 2 */
2915	    scp->font = FONT_8;
2916	    break;
2917	}
2918	if (configuration & CHAR_CURSOR)
2919	    set_destructive_cursor(scp, TRUE);
2920	break;
2921
2922    case M_BG320:     case M_CG320:     case M_BG640:
2923    case M_CG320_D:   case M_CG640_E:
2924    case M_CG640x350: case M_ENH_CG640:
2925    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
2926
2927	set_vgaregs(video_mode_ptr + (scp->mode * 64));
2928	break;
2929
2930    default:
2931	/* call user defined function XXX */
2932	break;
2933    }
2934
2935    /* set border color for this (virtual) console */
2936    set_border(scp->border);
2937    return;
2938}
2939
2940void
2941set_border(int color)
2942{
2943    inb(crtc_addr+6);               /* reset flip-flop */
2944    outb(ATC, 0x11); outb(ATC, color);
2945    inb(crtc_addr+6);               /* reset flip-flop */
2946    outb(ATC, 0x20);                /* enable Palette */
2947}
2948
2949static void
2950set_vgaregs(char *modetable)
2951{
2952    int i, s = splhigh();
2953
2954    outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
2955    outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
2956    for (i=0; i<4; i++) {           		/* program sequencer */
2957	outb(TSIDX, i+1);
2958	outb(TSREG, modetable[i+5]);
2959    }
2960    outb(MISC, modetable[9]);       		/* set dot-clock */
2961    outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
2962    outb(crtc_addr, 0x11);
2963    outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
2964    for (i=0; i<25; i++) {          		/* program crtc */
2965	outb(crtc_addr, i);
2966	if (i == 14 || i == 15)     		/* no hardware cursor */
2967	    outb(crtc_addr+1, 0xff);
2968	else
2969	    outb(crtc_addr+1, modetable[i+10]);
2970    }
2971    inb(crtc_addr+6);           		/* reset flip-flop */
2972    for (i=0; i<20; i++) {          		/* program attribute ctrl */
2973	outb(ATC, i);
2974	outb(ATC, modetable[i+35]);
2975    }
2976    for (i=0; i<9; i++) {           		/* program graph data ctrl */
2977	outb(GDCIDX, i);
2978	outb(GDCREG, modetable[i+55]);
2979    }
2980    inb(crtc_addr+6);           		/* reset flip-flop */
2981    outb(ATC ,0x20);            		/* enable palette */
2982    splx(s);
2983}
2984
2985static void
2986set_font_mode()
2987{
2988    /* setup vga for loading fonts (graphics plane mode) */
2989    inb(crtc_addr+6);
2990    outb(ATC, 0x30); outb(ATC, 0x01);
2991#if SLOW_VGA
2992    outb(TSIDX, 0x02); outb(TSREG, 0x04);
2993    outb(TSIDX, 0x04); outb(TSREG, 0x06);
2994    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
2995    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
2996    outb(GDCIDX, 0x06); outb(GDCREG, 0x05);
2997#else
2998    outw(TSIDX, 0x0402);
2999    outw(TSIDX, 0x0604);
3000    outw(GDCIDX, 0x0204);
3001    outw(GDCIDX, 0x0005);
3002    outw(GDCIDX, 0x0506);               /* addr = a0000, 64kb */
3003#endif
3004}
3005
3006static void
3007set_normal_mode()
3008{
3009    int s = splhigh();
3010
3011    /* setup vga for normal operation mode again */
3012    inb(crtc_addr+6);
3013    outb(ATC, 0x30); outb(ATC, 0x0C);
3014#if SLOW_VGA
3015    outb(TSIDX, 0x02); outb(TSREG, 0x03);
3016    outb(TSIDX, 0x04); outb(TSREG, 0x02);
3017    outb(GDCIDX, 0x04); outb(GDCREG, 0x00);
3018    outb(GDCIDX, 0x05); outb(GDCREG, 0x10);
3019    if (crtc_addr == MONO_BASE) {
3020	outb(GDCIDX, 0x06); outb(GDCREG, 0x0A); /* addr = b0000, 32kb */
3021    }
3022    else {
3023	outb(GDCIDX, 0x06); outb(GDCREG, 0x0E); /* addr = b8000, 32kb */
3024    }
3025#else
3026    outw(TSIDX, 0x0302);
3027    outw(TSIDX, 0x0204);
3028    outw(GDCIDX, 0x0004);
3029    outw(GDCIDX, 0x1005);
3030    if (crtc_addr == MONO_BASE)
3031	outw(GDCIDX, 0x0A06);           /* addr = b0000, 32kb */
3032    else
3033	outw(GDCIDX, 0x0E06);           /* addr = b8000, 32kb */
3034#endif
3035    splx(s);
3036}
3037
3038static void
3039copy_font(int operation, int font_type, char* font_image)
3040{
3041    int ch, line, segment, fontsize;
3042    u_char val;
3043
3044    switch (font_type) {
3045    default:
3046    case FONT_8:
3047	segment = 0x8000;
3048	fontsize = 8;
3049	break;
3050    case FONT_14:
3051	segment = 0x4000;
3052	fontsize = 14;
3053	break;
3054    case FONT_16:
3055	segment = 0x0000;
3056	fontsize = 16;
3057	break;
3058    }
3059    outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
3060    outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
3061    set_font_mode();
3062    for (ch=0; ch < 256; ch++)
3063	for (line=0; line < fontsize; line++)
3064	if (operation)
3065	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
3066		    font_image[(ch*fontsize)+line];
3067	else
3068	    font_image[(ch*fontsize)+line] =
3069	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
3070    set_normal_mode();
3071    outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
3072}
3073
3074static void
3075set_destructive_cursor(scr_stat *scp, int force)
3076{
3077    u_char cursor[32];
3078    caddr_t address;
3079    int i, font_size;
3080    char *font_buffer;
3081    static u_char old_saveunder = DEAD_CHAR;
3082
3083    if (!force && (scp->cursor_saveunder & 0xFF) == old_saveunder)
3084	return;
3085    old_saveunder = force ? DEAD_CHAR : scp->cursor_saveunder & 0xFF;
3086    switch (scp->font) {
3087    default:
3088    case FONT_8:
3089	font_size = 8;
3090	font_buffer = font_8;
3091	address = (caddr_t)VIDEOMEM + 0x8000;
3092	break;
3093    case FONT_14:
3094	font_size = 14;
3095	font_buffer = font_14;
3096	address = (caddr_t)VIDEOMEM + 0x4000;
3097	break;
3098    case FONT_16:
3099	font_size = 16;
3100	font_buffer = font_16;
3101	address = (caddr_t)VIDEOMEM;
3102	break;
3103    }
3104    bcopyw(font_buffer + ((scp->cursor_saveunder & 0xff) * font_size),
3105	   cursor, font_size);
3106    for (i=0; i<32; i++)
3107	if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
3108	    (scp->cursor_start >= font_size && i == font_size - 1))
3109	    cursor[i] |= 0xff;
3110    while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3111    set_font_mode();
3112    bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
3113    set_normal_mode();
3114}
3115
3116static void
3117reverse_mouse_cut(scr_stat *scp, int cut)
3118{
3119    ushort *ptr;
3120    int i = 0;
3121
3122    if (scp->mouse_cut_start != NULL) {
3123	for (ptr = (scp->mouse_cut_start > scp->mouse_cut_end
3124		    ? scp->mouse_cut_end : scp->mouse_cut_start);
3125	     ptr <= (scp->mouse_cut_start > scp->mouse_cut_end
3126		     ? scp->mouse_cut_start : scp->mouse_cut_end);
3127	     ptr++) {
3128	    *ptr = (*ptr & 0x88ff) | (*ptr & 0x7000)>>4 | (*ptr & 0x0700)<<4;
3129	    if (cut)
3130		cut_buffer[i++] = *ptr & 0xff;
3131	}
3132	if (cut)
3133	    cut_buffer[i] = 0x00;
3134    }
3135}
3136
3137static void
3138draw_mouse_image(scr_stat *scp)
3139{
3140    caddr_t address;
3141    int i, font_size;
3142    char *font_buffer;
3143    u_short buffer[32];
3144    u_short xoffset, yoffset;
3145    u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
3146    u_short *ptr = scp->scr_buf + (scp->mouse_oldpos - Crtat);
3147
3148    xoffset = scp->mouse_xpos % 8;
3149    switch (scp->font) {
3150    default:
3151    case FONT_8:
3152	font_size = 8;
3153	font_buffer = font_8;
3154	yoffset = scp->mouse_ypos % 8;
3155	address = (caddr_t)VIDEOMEM + 0x8000;
3156	break;
3157    case FONT_14:
3158	font_size = 14;
3159	font_buffer = font_14;
3160	yoffset = scp->mouse_ypos % 14;
3161	address = (caddr_t)VIDEOMEM + 0x4000;
3162	break;
3163    case FONT_16:
3164	font_size = 16;
3165	font_buffer = font_16;
3166	yoffset = scp->mouse_ypos % 16;
3167	address = (caddr_t)VIDEOMEM;
3168	break;
3169    }
3170
3171    bcopyw(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
3172	   &scp->mouse_cursor[0], font_size);
3173    bcopyw(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
3174	   &scp->mouse_cursor[32], font_size);
3175    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
3176	   &scp->mouse_cursor[64], font_size);
3177    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
3178	   &scp->mouse_cursor[96], font_size);
3179
3180    for (i=0; i<font_size; i++) {
3181	buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
3182	buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
3183    }
3184    for (i=0; i<16; i++) {
3185	buffer[i+yoffset] =
3186	    ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
3187	    | (mouse_or_mask[i] >> xoffset);
3188    }
3189    for (i=0; i<font_size; i++) {
3190	scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
3191	scp->mouse_cursor[i+32] = buffer[i] & 0xff;
3192	scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
3193	scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
3194    }
3195
3196    if (crt_pos != scp->mouse_oldpos) {
3197	*(scp->mouse_oldpos) = scp->mouse_saveunder[0];
3198	*(scp->mouse_oldpos+1) = scp->mouse_saveunder[1];
3199	*(scp->mouse_oldpos+scp->xsize) = scp->mouse_saveunder[2];
3200	*(scp->mouse_oldpos+scp->xsize+1) = scp->mouse_saveunder[3];
3201    }
3202    scp->mouse_saveunder[0] = *(scp->mouse_pos);
3203    scp->mouse_saveunder[1] = *(scp->mouse_pos+1);
3204    scp->mouse_saveunder[2] = *(scp->mouse_pos+scp->xsize);
3205    scp->mouse_saveunder[3] = *(scp->mouse_pos+scp->xsize+1);
3206    if ((scp->cursor_pos == (ptr)) ||
3207	(scp->cursor_pos == (ptr+1)) ||
3208	(scp->cursor_pos == (ptr+scp->xsize)) ||
3209	(scp->cursor_pos == (ptr+scp->xsize+1)) ||
3210	(scp->cursor_pos == (scp->mouse_pos)) ||
3211	(scp->cursor_pos == (scp->mouse_pos+1)) ||
3212	(scp->cursor_pos == (scp->mouse_pos+scp->xsize)) ||
3213	(scp->cursor_pos == (scp->mouse_pos+scp->xsize+1)))
3214	scp->status &= ~CURSOR_SHOWN;
3215    scp->mouse_oldpos = crt_pos;
3216    while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3217    *(crt_pos) = (*(scp->mouse_pos)&0xff00)|0xd0;
3218    *(crt_pos+scp->xsize) = (*(scp->mouse_pos+scp->xsize)&0xff00)|0xd2;
3219    if (scp->mouse_xpos < (scp->xsize-1)*8) {
3220    	*(crt_pos+1) = (*(scp->mouse_pos+1)&0xff00)|0xd1;
3221    	*(crt_pos+scp->xsize+1) = (*(scp->mouse_pos+scp->xsize+1)&0xff00)|0xd3;
3222    }
3223    set_font_mode();
3224    bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + 0xd0 * 32, 128);
3225    set_normal_mode();
3226}
3227
3228static void
3229save_palette(void)
3230{
3231    int i;
3232
3233    outb(PALRADR, 0x00);
3234    for (i=0x00; i<0x300; i++)
3235	palette[i] = inb(PALDATA);
3236    inb(crtc_addr+6);           /* reset flip/flop */
3237}
3238
3239void
3240load_palette(void)
3241{
3242    int i;
3243
3244    outb(PIXMASK, 0xFF);            /* no pixelmask */
3245    outb(PALWADR, 0x00);
3246    for (i=0x00; i<0x300; i++)
3247	 outb(PALDATA, palette[i]);
3248    inb(crtc_addr+6);           /* reset flip/flop */
3249    outb(ATC, 0x20);            /* enable palette */
3250}
3251
3252static void
3253do_bell(scr_stat *scp, int pitch, int duration)
3254{
3255    if (configuration & VISUAL_BELL) {
3256	if (blink_in_progress)
3257	    return;
3258	blink_in_progress = 4;
3259	if (scp != cur_console)
3260	    blink_in_progress += 2;
3261	blink_screen(cur_console);
3262	timeout((timeout_func_t)blink_screen, cur_console, hz/10);
3263    } else {
3264	if (scp != cur_console)
3265	    pitch *= 2;
3266	sysbeep(pitch, duration);
3267    }
3268}
3269
3270static void
3271blink_screen(scr_stat *scp)
3272{
3273    if (blink_in_progress > 1) {
3274	if (blink_in_progress & 1)
3275	    fillw(kernel_default.std_color | scr_map[0x20],
3276		  Crtat, scp->xsize * scp->ysize);
3277	else
3278	    fillw(kernel_default.rev_color | scr_map[0x20],
3279		  Crtat, scp->xsize * scp->ysize);
3280	blink_in_progress--;
3281	timeout((timeout_func_t)blink_screen, scp, hz/10);
3282    }
3283    else {
3284	blink_in_progress = FALSE;
3285    	mark_all(scp);
3286	if (delayed_next_scr)
3287	    switch_scr(scp, delayed_next_scr - 1);
3288    }
3289}
3290
3291#endif /* NSC */
3292