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