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