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