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