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