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