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