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