syscons.c revision 24338
1178476Sjb/*-
2178476Sjb * Copyright (c) 1992-1997 S�ren Schmidt
3178476Sjb * All rights reserved.
4178476Sjb *
5178476Sjb * Redistribution and use in source and binary forms, with or without
6178476Sjb * modification, are permitted provided that the following conditions
7178476Sjb * are met:
8178476Sjb * 1. Redistributions of source code must retain the above copyright
9178476Sjb *    notice, this list of conditions and the following disclaimer
10178476Sjb *    in this position and unchanged.
11178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
12178476Sjb *    notice, this list of conditions and the following disclaimer in the
13178476Sjb *    documentation and/or other materials provided with the distribution.
14178476Sjb * 3. The name of the author may not be used to endorse or promote products
15178476Sjb *    derived from this software withough specific prior written permission
16178476Sjb *
17178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18178476Sjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19178476Sjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20178476Sjb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21178476Sjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22178476Sjb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23178476Sjb * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24178476Sjb * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25178476Sjb * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26178476Sjb * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27178476Sjb *
28178476Sjb *  $Id: syscons.c,v 1.206 1997/03/24 11:24:07 bde Exp $
29178476Sjb */
30178476Sjb
31178476Sjb#include "sc.h"
32178476Sjb#include "apm.h"
33178476Sjb#include "opt_ddb.h"
34178476Sjb#include "opt_syscons.h"
35178476Sjb
36178476Sjb#if NSC > 0
37178476Sjb#include <sys/param.h>
38178476Sjb#include <sys/systm.h>
39178476Sjb#include <sys/conf.h>
40178476Sjb#include <sys/proc.h>
41178476Sjb#include <sys/signalvar.h>
42178476Sjb#include <sys/tty.h>
43178476Sjb#include <sys/uio.h>
44178476Sjb#include <sys/callout.h>
45178476Sjb#include <sys/kernel.h>
46178476Sjb#include <sys/syslog.h>
47178476Sjb#include <sys/errno.h>
48178476Sjb#include <sys/malloc.h>
49178476Sjb#ifdef	DEVFS
50178476Sjb#include <sys/devfsext.h>
51178476Sjb#endif
52178476Sjb
53178476Sjb#include <machine/clock.h>
54178476Sjb#include <machine/cons.h>
55178476Sjb#include <machine/console.h>
56178476Sjb#include <machine/md_var.h>
57178476Sjb#include <machine/psl.h>
58178476Sjb#include <machine/frame.h>
59178476Sjb#include <machine/pc/display.h>
60178476Sjb#include <machine/apm_bios.h>
61178476Sjb#include <machine/random.h>
62178476Sjb
63178476Sjb#include <vm/vm.h>
64178476Sjb#include <vm/vm_param.h>
65178476Sjb#include <vm/pmap.h>
66178476Sjb
67178476Sjb#include <i386/isa/isa.h>
68178476Sjb#include <i386/isa/isa_device.h>
69178476Sjb#include <i386/isa/timerreg.h>
70178476Sjb#include <i386/isa/kbdtables.h>
71178476Sjb#include <i386/isa/kbdio.h>
72178476Sjb#include <i386/isa/syscons.h>
73178476Sjb
74178476Sjb#if !defined(MAXCONS)
75178476Sjb#define MAXCONS 16
76178476Sjb#endif
77178476Sjb
78178476Sjb#define COLD 0
79178476Sjb#define WARM 1
80178476Sjb
81178476Sjb/* this may break on older VGA's but is useful on real 32 bit systems */
82178476Sjb#define bcopyw  bcopy
83178476Sjb
84178476Sjbstatic default_attr user_default = {
85178476Sjb    (FG_LIGHTGREY | BG_BLACK) << 8,
86178476Sjb    (FG_BLACK | BG_LIGHTGREY) << 8
87178476Sjb};
88178476Sjb
89178476Sjbstatic default_attr kernel_default = {
90178476Sjb    (FG_WHITE | BG_BLACK) << 8,
91178476Sjb    (FG_BLACK | BG_LIGHTGREY) << 8
92178476Sjb};
93178476Sjb
94178476Sjbstatic  scr_stat    	main_console;
95178476Sjbstatic  scr_stat    	*console[MAXCONS];
96178476Sjb#ifdef DEVFS
97178476Sjbstatic	void		*sc_devfs_token[MAXCONS];
98178476Sjb#endif
99178476Sjb	scr_stat    	*cur_console;
100178476Sjbstatic  scr_stat    	*new_scp, *old_scp;
101178476Sjbstatic  term_stat   	kernel_console;
102178476Sjbstatic  default_attr    *current_default;
103178476Sjbstatic  int     	flags = 0;
104178476Sjbstatic  int		sc_port = IO_KBD;
105178476Sjbstatic  KBDC		sc_kbdc = NULL;
106178476Sjbstatic  char        	init_done = COLD;
107178476Sjbstatic  u_short		sc_buffer[ROW*COL];
108178476Sjbstatic  char        	switch_in_progress = FALSE;
109178476Sjbstatic  char        	write_in_progress = FALSE;
110178476Sjbstatic  char        	blink_in_progress = FALSE;
111178476Sjbstatic  int        	blinkrate = 0;
112178476Sjb	u_int       	crtc_addr = MONO_BASE;
113178476Sjb	char        	crtc_vga = FALSE;
114178476Sjbstatic  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
115178476Sjbstatic  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
116178476Sjbstatic  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
117178476Sjbstatic  int     	delayed_next_scr = FALSE;
118178476Sjbstatic  long        	scrn_blank_time = 0;    /* screen saver timeout value */
119178476Sjb	int     	scrn_blanked = FALSE;   /* screen saver active flag */
120178476Sjbstatic  long       	scrn_time_stamp;
121178476Sjb	u_char      	scr_map[256];
122178476Sjb	u_char      	scr_rmap[256];
123178476Sjb	char        	*video_mode_ptr = NULL;
124178476Sjb	int     	fonts_loaded = 0;
125178476Sjb	char        	font_8[256*8];
126178476Sjb	char		font_14[256*14];
127178476Sjb	char		font_16[256*16];
128178476Sjb	char        	palette[256*3];
129178476Sjbstatic	char 		*cut_buffer;
130178476Sjbstatic  u_short 	mouse_and_mask[16] = {
131178476Sjb				0xc000, 0xe000, 0xf000, 0xf800,
132178476Sjb				0xfc00, 0xfe00, 0xff00, 0xff80,
133178476Sjb				0xfe00, 0x1e00, 0x1f00, 0x0f00,
134178476Sjb				0x0f00, 0x0000, 0x0000, 0x0000
135178476Sjb			};
136178476Sjbstatic  u_short 	mouse_or_mask[16] = {
137178476Sjb				0x0000, 0x4000, 0x6000, 0x7000,
138178476Sjb				0x7800, 0x7c00, 0x7e00, 0x6800,
139178476Sjb				0x0c00, 0x0c00, 0x0600, 0x0600,
140178476Sjb				0x0000, 0x0000, 0x0000, 0x0000
141178476Sjb			};
142178476Sjb
143178476Sjbstatic void    		none_saver(int blank) { }
144178476Sjbvoid    		(*current_saver)(int blank) = none_saver;
145178476Sjbint  			(*sc_user_ioctl)(dev_t dev, int cmd, caddr_t data,
146178476Sjb					 int flag, struct proc *p) = NULL;
147178476Sjb
148178476Sjb/* OS specific stuff */
149178476Sjb#ifdef not_yet_done
150178476Sjb#define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
151178476Sjbstruct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
152178476Sjbstruct  MOUSE_TTY 	(sccons[MAXCONS+1] = ttymalloc(sccons[MAXCONS+1]))
153178476Sjbstruct  tty         	*sccons[MAXCONS+2];
154178476Sjb#else
155178476Sjb#define VIRTUAL_TTY(x)  &sccons[x]
156178476Sjb#define CONSOLE_TTY 	&sccons[MAXCONS]
157178476Sjb#define MOUSE_TTY 	&sccons[MAXCONS+1]
158178476Sjbstatic struct tty     	sccons[MAXCONS+2];
159178476Sjb#endif
160178476Sjb#define SC_MOUSE 	128
161178476Sjb#define SC_CONSOLE	255
162178476Sjb#define MONO_BUF    	pa_to_va(0xB0000)
163178476Sjb#define CGA_BUF     	pa_to_va(0xB8000)
164178476Sjbu_short         	*Crtat;
165178476Sjbstatic const int	nsccons = MAXCONS+2;
166178476Sjb
167178476Sjb#define WRAPHIST(scp, pointer, offset)\
168178476Sjb    ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
169178476Sjb    + (offset)) % (scp->history_size)))
170178476Sjb#define ISSIGVALID(sig)	((sig) > 0 && (sig) < NSIG)
171178476Sjb
172178476Sjb/* prototypes */
173178476Sjbstatic int scattach(struct isa_device *dev);
174178476Sjbstatic int scparam(struct tty *tp, struct termios *t);
175178476Sjbstatic int scprobe(struct isa_device *dev);
176178476Sjbstatic void scstart(struct tty *tp);
177178476Sjbstatic void scmousestart(struct tty *tp);
178178476Sjbstatic void scinit(void);
179178476Sjbstatic u_int scgetc(u_int flags);
180178476Sjb#define SCGETC_CN	1
181178476Sjb#define SCGETC_NONBLOCK	2
182178476Sjbstatic scr_stat *get_scr_stat(dev_t dev);
183178476Sjbstatic scr_stat *alloc_scp(void);
184178476Sjbstatic void init_scp(scr_stat *scp);
185178476Sjbstatic int get_scr_num(void);
186178476Sjbstatic void scrn_timer(void);
187178476Sjbstatic void clear_screen(scr_stat *scp);
188178476Sjbstatic int switch_scr(scr_stat *scp, u_int next_scr);
189178476Sjbstatic void exchange_scr(void);
190178476Sjbstatic inline void move_crsr(scr_stat *scp, int x, int y);
191178476Sjbstatic void scan_esc(scr_stat *scp, u_char c);
192178476Sjbstatic void draw_cursor_image(scr_stat *scp);
193178476Sjbstatic void remove_cursor_image(scr_stat *scp);
194178476Sjbstatic void ansi_put(scr_stat *scp, u_char *buf, int len);
195178476Sjbstatic u_char *get_fstr(u_int c, u_int *len);
196178476Sjbstatic void history_to_screen(scr_stat *scp);
197178476Sjbstatic int history_up_line(scr_stat *scp);
198178476Sjbstatic int history_down_line(scr_stat *scp);
199178476Sjbstatic int mask2attr(struct term_stat *term);
200178476Sjbstatic void set_keyboard(int command, int data);
201178476Sjbstatic void update_leds(int which);
202178476Sjbstatic void set_vgaregs(char *modetable);
203178476Sjbstatic void set_font_mode(void);
204178476Sjbstatic void set_normal_mode(void);
205178476Sjbstatic void set_destructive_cursor(scr_stat *scp);
206178476Sjbstatic void set_mouse_pos(scr_stat *scp);
207178476Sjbstatic void mouse_cut_start(scr_stat *scp);
208178476Sjbstatic void mouse_cut_end(scr_stat *scp);
209178476Sjbstatic void mouse_paste(scr_stat *scp);
210178476Sjbstatic void draw_mouse_image(scr_stat *scp);
211178476Sjbstatic void remove_mouse_image(scr_stat *scp);
212178476Sjbstatic void draw_cutmarking(scr_stat *scp);
213178476Sjbstatic void remove_cutmarking(scr_stat *scp);
214178476Sjbstatic void save_palette(void);
215178476Sjbstatic void do_bell(scr_stat *scp, int pitch, int duration);
216178476Sjbstatic void blink_screen(scr_stat *scp);
217178476Sjb#ifdef SC_SPLASH_SCREEN
218178476Sjbstatic void toggle_splash_screen(scr_stat *scp);
219178476Sjb#endif
220178476Sjb
221178476Sjbstruct  isa_driver scdriver = {
222178476Sjb    scprobe, scattach, "sc", 1
223178476Sjb};
224178476Sjb
225178476Sjbstatic	d_open_t	scopen;
226178476Sjbstatic	d_close_t	scclose;
227178476Sjbstatic	d_read_t	scread;
228178476Sjbstatic	d_write_t	scwrite;
229178476Sjbstatic	d_ioctl_t	scioctl;
230178476Sjbstatic	d_devtotty_t	scdevtotty;
231178476Sjbstatic	d_mmap_t	scmmap;
232178476Sjb
233178476Sjb#define CDEV_MAJOR 12
234178476Sjbstatic	struct cdevsw	scdevsw = {
235178476Sjb	scopen,		scclose,	scread,		scwrite,
236178476Sjb	scioctl,	nullstop,	noreset,	scdevtotty,
237178476Sjb	ttselect,	scmmap,		nostrategy,	"sc",	NULL,	-1 };
238178476Sjb
239178476Sjb/*
240178476Sjb * These functions need to be before calls to them so they can be inlined.
241178476Sjb */
242178476Sjbstatic inline void
243178476Sjbdraw_cursor_image(scr_stat *scp)
244178476Sjb{
245178476Sjb    u_short cursor_image, *ptr = Crtat + (scp->cursor_pos - scp->scr_buf);
246178476Sjb
247178476Sjb    /* do we have a destructive cursor ? */
248178476Sjb    if (flags & CHAR_CURSOR) {
249178476Sjb	cursor_image = *scp->cursor_pos;
250178476Sjb	scp->cursor_saveunder = cursor_image;
251178476Sjb	/* modify cursor_image */
252178476Sjb	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
253178476Sjb	    set_destructive_cursor(scp);
254178476Sjb	    cursor_image &= 0xff00;
255178476Sjb	    cursor_image |= DEAD_CHAR;
256178476Sjb	}
257178476Sjb    }
258178476Sjb    else {
259178476Sjb	cursor_image = (*(ptr) & 0x00ff) | *(scp->cursor_pos) & 0xff00;
260178476Sjb	scp->cursor_saveunder = cursor_image;
261178476Sjb	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
262178476Sjb	    if ((cursor_image & 0x7000) == 0x7000) {
263178476Sjb		cursor_image &= 0x8fff;
264178476Sjb		if(!(cursor_image & 0x0700))
265178476Sjb		    cursor_image |= 0x0700;
266178476Sjb	    } else {
267178476Sjb		cursor_image |= 0x7000;
268178476Sjb		if ((cursor_image & 0x0700) == 0x0700)
269178476Sjb		    cursor_image &= 0xf0ff;
270178476Sjb	    }
271178476Sjb	}
272178476Sjb    }
273178476Sjb    *ptr = cursor_image;
274178476Sjb}
275178476Sjb
276178476Sjbstatic inline void
277178476Sjbremove_cursor_image(scr_stat *scp)
278178476Sjb{
279178476Sjb    *(Crtat + (scp->cursor_oldpos - scp->scr_buf)) = scp->cursor_saveunder;
280178476Sjb}
281178476Sjb
282178476Sjbstatic inline void
283178476Sjbmove_crsr(scr_stat *scp, int x, int y)
284178476Sjb{
285178476Sjb    if (x < 0)
286178476Sjb	x = 0;
287178476Sjb    if (y < 0)
288178476Sjb	y = 0;
289178476Sjb    if (x >= scp->xsize)
290178476Sjb	x = scp->xsize-1;
291178476Sjb    if (y >= scp->ysize)
292178476Sjb	y = scp->ysize-1;
293178476Sjb    scp->xpos = x;
294178476Sjb    scp->ypos = y;
295178476Sjb    scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
296178476Sjb}
297178476Sjb
298178476Sjbstatic int
299178476Sjbscprobe(struct isa_device *dev)
300178476Sjb{
301178476Sjb    int codeset;
302178476Sjb    int c = -1;
303178476Sjb    int m;
304178476Sjb
305178476Sjb    sc_port = dev->id_iobase;
306178476Sjb    sc_kbdc = kbdc_open(sc_port);
307178476Sjb
308178476Sjb    if (!kbdc_lock(sc_kbdc, TRUE)) {
309178476Sjb	/* driver error? */
310178476Sjb	printf("sc%d: unable to lock the controller.\n", dev->id_unit);
311178476Sjb        return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
312178476Sjb    }
313178476Sjb
314178476Sjb    /* discard anything left after UserConfig */
315178476Sjb    empty_both_buffers(sc_kbdc, 10);
316178476Sjb
317178476Sjb    /* save the current keyboard controller command byte */
318178476Sjb    m = kbdc_get_device_mask(sc_kbdc) & ~KBD_KBD_CONTROL_BITS;
319178476Sjb    c = get_controller_command_byte(sc_kbdc);
320178476Sjb    if (c == -1) {
321178476Sjb	/* CONTROLLER ERROR */
322178476Sjb	printf("sc%d: unable to get the current command byte value.\n",
323178476Sjb	    dev->id_unit);
324178476Sjb	goto fail;
325178476Sjb    }
326178476Sjb    if (bootverbose)
327178476Sjb	printf("sc%d: the current keyboard controller command byte %04x\n",
328178476Sjb	    dev->id_unit, c);
329178476Sjb#if 0
330178476Sjb    /* override the keyboard lock switch */
331178476Sjb    c |= KBD_OVERRIDE_KBD_LOCK;
332178476Sjb#endif
333178476Sjb
334178476Sjb    /*
335178476Sjb     * enable the keyboard port, but disable the keyboard intr.
336178476Sjb     * the aux port (mouse port) is disabled too.
337178476Sjb     */
338178476Sjb    if (!set_controller_command_byte(sc_kbdc,
339178476Sjb            KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
340178476Sjb            KBD_ENABLE_KBD_PORT | KBD_DISABLE_KBD_INT
341178476Sjb                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
342178476Sjb	/* CONTROLLER ERROR
343178476Sjb	 * there is very little we can do...
344178476Sjb	 */
345178476Sjb	printf("sc%d: unable to set the command byte.\n", dev->id_unit);
346178476Sjb	goto fail;
347178476Sjb     }
348178476Sjb
349178476Sjb     /*
350178476Sjb      * Check if we have an XT keyboard before we attempt to reset it.
351178476Sjb      * The procedure assumes that the keyboard and the controller have
352178476Sjb      * been set up properly by BIOS and have not been messed up
353178476Sjb      * during the boot process.
354178476Sjb      */
355178476Sjb     codeset = -1;
356178476Sjb     if (dev->id_flags & XT_KEYBD)
357178476Sjb	 /* the user says there is a XT keyboard */
358178476Sjb	 codeset = 1;
359178476Sjb#ifdef DETECT_XT_KEYBOARD
360178476Sjb     else if ((c & KBD_TRANSLATION) == 0) {
361178476Sjb	 /* SET_SCANCODE_SET is not always supported; ignore error */
362178476Sjb	 if (send_kbd_command_and_data(sc_kbdc, KBDC_SET_SCANCODE_SET, 0)
363178476Sjb		 == KBD_ACK)
364178476Sjb	     codeset = read_kbd_data(sc_kbdc);
365178476Sjb     }
366178476Sjb     if (bootverbose)
367178476Sjb         printf("sc%d: keyboard scancode set %d\n", dev->id_unit, codeset);
368178476Sjb#endif /* DETECT_XT_KEYBOARD */
369178476Sjb
370178476Sjb     /* reset keyboard hardware */
371178476Sjb     if (!reset_kbd(sc_kbdc)) {
372178476Sjb        /* KEYBOARD ERROR
373178476Sjb	 * Keyboard reset may fail either because the keyboard doen't exist,
374178476Sjb         * or because the keyboard doesn't pass the self-test, or the keyboard
375178476Sjb         * controller on the motherboard and the keyboard somehow fail to
376178476Sjb         * shake hands. It is just possible, particularly in the last case,
377178476Sjb         * that the keyoard controller may be left in a hung state.
378178476Sjb         * test_controller() and test_kbd_port() appear to bring the keyboard
379178476Sjb         * controller back (I don't know why and how, though.)
380178476Sjb	 */
381178476Sjb	empty_both_buffers(sc_kbdc, 10);
382178476Sjb	test_controller(sc_kbdc);
383178476Sjb	test_kbd_port(sc_kbdc);
384178476Sjb	/* We could disable the keyboard port and interrupt... but,
385178476Sjb	 * the keyboard may still exist (see above).
386178476Sjb	 */
387178476Sjb        if (bootverbose)
388178476Sjb	   printf("sc%d: failed to reset the keyboard.\n", dev->id_unit);
389178476Sjb	goto fail;
390178476Sjb    }
391178476Sjb
392178476Sjb    /*
393178476Sjb     * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
394178476Sjb     * such as those on the IBM ThinkPad laptop computers can be used
395178476Sjb     * with the standard console driver.
396178476Sjb     */
397178476Sjb    if (codeset == 1) {
398178476Sjb	if (send_kbd_command_and_data(
399178476Sjb	        sc_kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
400178476Sjb	    /* XT kbd doesn't need scan code translation */
401178476Sjb	    c &= ~KBD_TRANSLATION;
402178476Sjb	} else {
403178476Sjb	    /* KEYBOARD ERROR
404178476Sjb	     * The XT kbd isn't usable unless the proper scan code set
405178476Sjb	     * is selected.
406178476Sjb	     */
407178476Sjb	    printf("sc%d: unable to set the XT keyboard mode.\n", dev->id_unit);
408178476Sjb	    goto fail;
409178476Sjb	}
410178476Sjb    }
411178476Sjb    /* enable the keyboard port and intr. */
412178476Sjb    if (!set_controller_command_byte(sc_kbdc,
413178476Sjb            KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS | KBD_OVERRIDE_KBD_LOCK,
414178476Sjb	    (c & (KBD_AUX_CONTROL_BITS | KBD_OVERRIDE_KBD_LOCK))
415178476Sjb	        | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
416178476Sjb	/* CONTROLLER ERROR
417178476Sjb	 * This is serious; we are left with the disabled keyboard intr.
418178476Sjb	 */
419178476Sjb	printf("sc%d: unable to enable the keyboard port and intr.\n",
420178476Sjb	    dev->id_unit);
421178476Sjb	goto fail;
422178476Sjb    }
423178476Sjb
424178476Sjbsucceed:
425178476Sjb    kbdc_set_device_mask(sc_kbdc, m | KBD_KBD_CONTROL_BITS),
426178476Sjb    kbdc_lock(sc_kbdc, FALSE);
427178476Sjb    return (IO_KBDSIZE);
428178476Sjb
429178476Sjbfail:
430178476Sjb    if (c != -1)
431178476Sjb        /* try to restore the command byte as before, if possible */
432178476Sjb        set_controller_command_byte(sc_kbdc, 0xff, c);
433178476Sjb    kbdc_set_device_mask(sc_kbdc,
434178476Sjb        (dev->id_flags & DETECT_KBD) ? m : m | KBD_KBD_CONTROL_BITS);
435178476Sjb    kbdc_lock(sc_kbdc, FALSE);
436178476Sjb    return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
437178476Sjb}
438178476Sjb
439178476Sjb#if NAPM > 0
440178476Sjbstatic int
441178476Sjbscresume(void *dummy)
442178476Sjb{
443178476Sjb	shfts = ctls = alts = agrs = metas = 0;
444178476Sjb	return 0;
445178476Sjb}
446178476Sjb#endif
447178476Sjb
448178476Sjbstatic int
449178476Sjbscattach(struct isa_device *dev)
450178476Sjb{
451178476Sjb    scr_stat *scp;
452178476Sjb    dev_t cdev = makedev(CDEV_MAJOR, 0);
453178476Sjb#ifdef DEVFS
454178476Sjb    int vc;
455178476Sjb#endif
456178476Sjb
457178476Sjb    scinit();
458178476Sjb    flags = dev->id_flags;
459178476Sjb
460178476Sjb    scp = console[0];
461178476Sjb
462178476Sjb    if (crtc_vga) {
463178476Sjb    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
464178476Sjb    }
465178476Sjb
466178476Sjb    scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
467178476Sjb				     M_DEVBUF, M_NOWAIT);
468178476Sjb
469178476Sjb    /* copy temporary buffer to final buffer */
470178476Sjb    bcopyw(sc_buffer, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
471178476Sjb
472178476Sjb    scp->cursor_pos = scp->cursor_oldpos =
473178476Sjb	scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
474178476Sjb    scp->mouse_pos = scp->mouse_oldpos =
475178476Sjb	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
476178476Sjb	    		scp->mouse_xpos/8);
477178476Sjb
478178476Sjb    /* initialize history buffer & pointers */
479178476Sjb    scp->history_head = scp->history_pos = scp->history =
480178476Sjb	(u_short *)malloc(scp->history_size*sizeof(u_short),
481178476Sjb			  M_DEVBUF, M_NOWAIT);
482178476Sjb    bzero(scp->history_head, scp->history_size*sizeof(u_short));
483178476Sjb
484178476Sjb    /* initialize cursor stuff */
485178476Sjb    if (!(scp->status & UNKNOWN_MODE)) {
486178476Sjb    	draw_cursor_image(scp);
487178476Sjb    	if (crtc_vga && (flags & CHAR_CURSOR))
488178476Sjb	    set_destructive_cursor(scp);
489178476Sjb    }
490178476Sjb
491178476Sjb    /* get screen update going */
492178476Sjb    scrn_timer();
493178476Sjb
494178476Sjb    update_leds(scp->status);
495178476Sjb
496178476Sjb    printf("sc%d: ", dev->id_unit);
497178476Sjb    if (crtc_vga)
498178476Sjb	if (crtc_addr == MONO_BASE)
499178476Sjb	    printf("VGA mono");
500178476Sjb	else
501178476Sjb	    printf("VGA color");
502178476Sjb    else
503178476Sjb	if (crtc_addr == MONO_BASE)
504178476Sjb	    printf("MDA/hercules");
505178476Sjb	else
506178476Sjb	    printf("CGA/EGA");
507178476Sjb    printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, flags);
508178476Sjb
509178476Sjb#if NAPM > 0
510178476Sjb    scp->r_hook.ah_fun = scresume;
511178476Sjb    scp->r_hook.ah_arg = NULL;
512178476Sjb    scp->r_hook.ah_name = "system keyboard";
513178476Sjb    scp->r_hook.ah_order = APM_MID_ORDER;
514178476Sjb    apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
515178476Sjb#endif
516178476Sjb
517178476Sjb    cdevsw_add(&cdev, &scdevsw, NULL);
518178476Sjb
519178476Sjb#ifdef DEVFS
520178476Sjb    for (vc = 0; vc < MAXCONS; vc++)
521178476Sjb        sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR, UID_ROOT,
522178476Sjb					      GID_WHEEL, 0600, "ttyv%n", vc);
523178476Sjb#endif
524178476Sjb    return 0;
525178476Sjb}
526178476Sjb
527178476Sjbstruct tty
528178476Sjb*scdevtotty(dev_t dev)
529178476Sjb{
530178476Sjb    int unit = minor(dev);
531178476Sjb
532178476Sjb    if (init_done == COLD)
533178476Sjb	return(NULL);
534178476Sjb    if (unit == SC_CONSOLE)
535178476Sjb	return CONSOLE_TTY;
536178476Sjb    if (unit == SC_MOUSE)
537178476Sjb	return MOUSE_TTY;
538178476Sjb    if (unit >= MAXCONS || unit < 0)
539178476Sjb	return(NULL);
540178476Sjb    return VIRTUAL_TTY(unit);
541178476Sjb}
542178476Sjb
543178476Sjbint
544178476Sjbscopen(dev_t dev, int flag, int mode, struct proc *p)
545178476Sjb{
546178476Sjb    struct tty *tp = scdevtotty(dev);
547178476Sjb
548178476Sjb    if (!tp)
549178476Sjb	return(ENXIO);
550178476Sjb
551178476Sjb    tp->t_oproc = (minor(dev) == SC_MOUSE) ? scmousestart : scstart;
552178476Sjb    tp->t_param = scparam;
553178476Sjb    tp->t_dev = dev;
554178476Sjb    if (!(tp->t_state & TS_ISOPEN)) {
555178476Sjb	ttychars(tp);
556178476Sjb	tp->t_iflag = TTYDEF_IFLAG;
557178476Sjb	tp->t_oflag = TTYDEF_OFLAG;
558178476Sjb	tp->t_cflag = TTYDEF_CFLAG;
559178476Sjb	tp->t_lflag = TTYDEF_LFLAG;
560178476Sjb	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
561178476Sjb	scparam(tp, &tp->t_termios);
562178476Sjb	ttsetwater(tp);
563178476Sjb	(*linesw[tp->t_line].l_modem)(tp, 1);
564178476Sjb    }
565178476Sjb    else
566178476Sjb	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
567178476Sjb	    return(EBUSY);
568178476Sjb    if (minor(dev) < MAXCONS && !console[minor(dev)]) {
569178476Sjb	console[minor(dev)] = alloc_scp();
570178476Sjb    }
571178476Sjb    if (minor(dev)<MAXCONS && !tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
572178476Sjb	tp->t_winsize.ws_col = console[minor(dev)]->xsize;
573178476Sjb	tp->t_winsize.ws_row = console[minor(dev)]->ysize;
574178476Sjb    }
575178476Sjb    return ((*linesw[tp->t_line].l_open)(dev, tp));
576178476Sjb}
577178476Sjb
578178476Sjbint
579178476Sjbscclose(dev_t dev, int flag, int mode, struct proc *p)
580178476Sjb{
581178476Sjb    struct tty *tp = scdevtotty(dev);
582178476Sjb    struct scr_stat *scp;
583178476Sjb
584178476Sjb    if (!tp)
585178476Sjb	return(ENXIO);
586178476Sjb    if (minor(dev) < MAXCONS) {
587178476Sjb	scp = get_scr_stat(tp->t_dev);
588178476Sjb	if (scp->status & SWITCH_WAIT_ACQ)
589178476Sjb	    wakeup((caddr_t)&scp->smode);
590178476Sjb#if not_yet_done
591178476Sjb	if (scp == &main_console) {
592178476Sjb	    scp->pid = 0;
593178476Sjb	    scp->proc = NULL;
594178476Sjb	    scp->smode.mode = VT_AUTO;
595178476Sjb	}
596178476Sjb	else {
597178476Sjb	    free(scp->scr_buf, M_DEVBUF);
598178476Sjb	    free(scp->history, M_DEVBUF);
599178476Sjb	    free(scp, M_DEVBUF);
600178476Sjb	    console[minor(dev)] = NULL;
601178476Sjb	}
602178476Sjb#else
603178476Sjb	scp->pid = 0;
604178476Sjb	scp->proc = NULL;
605178476Sjb	scp->smode.mode = VT_AUTO;
606178476Sjb#endif
607178476Sjb    }
608178476Sjb    spltty();
609178476Sjb    (*linesw[tp->t_line].l_close)(tp, flag);
610178476Sjb    ttyclose(tp);
611178476Sjb    spl0();
612178476Sjb    return(0);
613178476Sjb}
614178476Sjb
615178476Sjbint
616178476Sjbscread(dev_t dev, struct uio *uio, int flag)
617178476Sjb{
618178476Sjb    struct tty *tp = scdevtotty(dev);
619178476Sjb
620178476Sjb    if (!tp)
621178476Sjb	return(ENXIO);
622178476Sjb    return((*linesw[tp->t_line].l_read)(tp, uio, flag));
623178476Sjb}
624178476Sjb
625178476Sjbint
626178476Sjbscwrite(dev_t dev, struct uio *uio, int flag)
627178476Sjb{
628178476Sjb    struct tty *tp = scdevtotty(dev);
629178476Sjb
630178476Sjb    if (!tp)
631178476Sjb	return(ENXIO);
632178476Sjb    return((*linesw[tp->t_line].l_write)(tp, uio, flag));
633178476Sjb}
634178476Sjb
635178476Sjbvoid
636178476Sjbscintr(int unit)
637178476Sjb{
638178476Sjb    static struct tty *cur_tty;
639178476Sjb    int c, len;
640178476Sjb    u_char *cp;
641178476Sjb
642178476Sjb    /* make screensaver happy */
643178476Sjb    scrn_time_stamp = time.tv_sec;
644178476Sjb    if (scrn_blanked) {
645178476Sjb	(*current_saver)(FALSE);
646178476Sjb	mark_all(cur_console);
647178476Sjb    }
648178476Sjb
649178476Sjb    /*
650178476Sjb     * Loop while there is still input to get from the keyboard.
651178476Sjb     * I don't think this is nessesary, and it doesn't fix
652178476Sjb     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
653178476Sjb     */
654178476Sjb    while ((c = scgetc(SCGETC_NONBLOCK)) != NOKEY) {
655178476Sjb
656178476Sjb	cur_tty = VIRTUAL_TTY(get_scr_num());
657178476Sjb	if (!(cur_tty->t_state & TS_ISOPEN))
658178476Sjb	    if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
659178476Sjb		continue;
660178476Sjb
661178476Sjb	switch (c & 0xff00) {
662178476Sjb	case 0x0000: /* normal key */
663178476Sjb	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
664178476Sjb	    break;
665178476Sjb	case FKEY:  /* function key, return string */
666178476Sjb	    if (cp = get_fstr((u_int)c, (u_int *)&len)) {
667178476Sjb	    	while (len-- >  0)
668178476Sjb		    (*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
669178476Sjb	    }
670178476Sjb	    break;
671178476Sjb	case MKEY:  /* meta is active, prepend ESC */
672178476Sjb	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
673178476Sjb	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
674178476Sjb	    break;
675178476Sjb	case BKEY:  /* backtab fixed sequence (esc [ Z) */
676178476Sjb	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
677178476Sjb	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
678178476Sjb	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
679178476Sjb	    break;
680178476Sjb	}
681178476Sjb    }
682178476Sjb
683178476Sjb    if (cur_console->status & MOUSE_ENABLED) {
684178476Sjb	cur_console->status &= ~MOUSE_VISIBLE;
685178476Sjb	remove_mouse_image(cur_console);
686178476Sjb    }
687178476Sjb}
688178476Sjb
689178476Sjbstatic int
690178476Sjbscparam(struct tty *tp, struct termios *t)
691178476Sjb{
692178476Sjb    tp->t_ispeed = t->c_ispeed;
693178476Sjb    tp->t_ospeed = t->c_ospeed;
694178476Sjb    tp->t_cflag = t->c_cflag;
695178476Sjb    return 0;
696178476Sjb}
697178476Sjb
698178476Sjbint
699178476Sjbscioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
700178476Sjb{
701178476Sjb    int error;
702178476Sjb    u_int i;
703178476Sjb    struct tty *tp;
704178476Sjb    struct trapframe *fp;
705178476Sjb    scr_stat *scp;
706178476Sjb
707178476Sjb    tp = scdevtotty(dev);
708178476Sjb    if (!tp)
709178476Sjb	return ENXIO;
710178476Sjb    scp = get_scr_stat(tp->t_dev);
711178476Sjb
712178476Sjb    /* If there is a user_ioctl function call that first */
713178476Sjb    if (sc_user_ioctl) {
714178476Sjb	if (error = (*sc_user_ioctl)(dev, cmd, data, flag, p))
715178476Sjb	    return error;
716178476Sjb    }
717178476Sjb
718178476Sjb    switch (cmd) {  		/* process console hardware related ioctl's */
719178476Sjb
720178476Sjb    case GIO_ATTR:      	/* get current attributes */
721178476Sjb	*(int*)data = (scp->term.cur_attr >> 8) & 0xFF;
722178476Sjb	return 0;
723178476Sjb
724178476Sjb    case GIO_COLOR:     	/* is this a color console ? */
725178476Sjb	if (crtc_addr == COLOR_BASE)
726178476Sjb	    *(int*)data = 1;
727178476Sjb	else
728178476Sjb	    *(int*)data = 0;
729178476Sjb	return 0;
730178476Sjb
731178476Sjb    case CONS_CURRENT:  	/* get current adapter type */
732178476Sjb	if (crtc_vga)
733178476Sjb	    *(int*)data = KD_VGA;
734178476Sjb	else
735178476Sjb	    if (crtc_addr == MONO_BASE)
736178476Sjb		*(int*)data = KD_MONO;
737178476Sjb	    else
738178476Sjb		*(int*)data = KD_CGA;
739178476Sjb	return 0;
740178476Sjb
741178476Sjb    case CONS_GET:      	/* get current video mode */
742178476Sjb	*(int*)data = scp->mode;
743178476Sjb	return 0;
744178476Sjb
745178476Sjb    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
746178476Sjb	scrn_blank_time = *(int*)data;
747178476Sjb	return 0;
748178476Sjb
749178476Sjb    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
750178476Sjb	if ((*(int*)data) & 0x01)
751178476Sjb	    flags |= BLINK_CURSOR;
752178476Sjb	else
753178476Sjb	    flags &= ~BLINK_CURSOR;
754178476Sjb	if ((*(int*)data) & 0x02) {
755178476Sjb	    if (!crtc_vga)
756178476Sjb		return ENXIO;
757178476Sjb	    flags |= CHAR_CURSOR;
758178476Sjb	    set_destructive_cursor(scp);
759178476Sjb	} else
760178476Sjb	    flags &= ~CHAR_CURSOR;
761178476Sjb	return 0;
762178476Sjb
763178476Sjb    case CONS_BELLTYPE: 	/* set bell type sound/visual */
764178476Sjb	if (*data)
765178476Sjb	    flags |= VISUAL_BELL;
766178476Sjb	else
767178476Sjb	    flags &= ~VISUAL_BELL;
768178476Sjb	return 0;
769178476Sjb
770178476Sjb    case CONS_HISTORY:  	/* set history size */
771178476Sjb	if (*data) {
772178476Sjb	    free(scp->history, M_DEVBUF);
773178476Sjb	    scp->history_size = *(int*)data;
774178476Sjb	    if (scp->history_size < scp->ysize)
775178476Sjb		scp->history = NULL;
776178476Sjb	    else {
777178476Sjb		scp->history_size *= scp->xsize;
778178476Sjb		scp->history_head = scp->history_pos = scp->history =
779178476Sjb		    (u_short *)malloc(scp->history_size*sizeof(u_short),
780178476Sjb				      M_DEVBUF, M_WAITOK);
781178476Sjb		bzero(scp->history_head, scp->history_size*sizeof(u_short));
782178476Sjb	    }
783178476Sjb	    return 0;
784178476Sjb	}
785178476Sjb	else
786178476Sjb	    return EINVAL;
787178476Sjb
788178476Sjb    case CONS_MOUSECTL:		/* control mouse arrow */
789178476Sjb    {
790178476Sjb	mouse_info_t *mouse = (mouse_info_t*)data;
791178476Sjb
792178476Sjb	if (!crtc_vga)
793178476Sjb	    return ENXIO;
794178476Sjb
795178476Sjb	switch (mouse->operation) {
796178476Sjb	case MOUSE_MODE:
797178476Sjb	    if (ISSIGVALID(mouse->u.mode.signal)) {
798178476Sjb		scp->mouse_signal = mouse->u.mode.signal;
799178476Sjb		scp->mouse_proc = p;
800178476Sjb		scp->mouse_pid = p->p_pid;
801178476Sjb	    }
802178476Sjb	    else {
803178476Sjb		scp->mouse_signal = 0;
804178476Sjb		scp->mouse_proc = NULL;
805178476Sjb		scp->mouse_pid = 0;
806178476Sjb	    }
807178476Sjb	    break;
808178476Sjb
809178476Sjb	case MOUSE_SHOW:
810178476Sjb	    if (!(scp->status & MOUSE_ENABLED)) {
811178476Sjb		scp->status |= (MOUSE_ENABLED | MOUSE_VISIBLE);
812178476Sjb		scp->mouse_oldpos = scp->mouse_pos;
813178476Sjb		mark_all(scp);
814178476Sjb	    }
815178476Sjb	    else
816178476Sjb		return EINVAL;
817178476Sjb	    break;
818178476Sjb
819178476Sjb	case MOUSE_HIDE:
820178476Sjb	    if (scp->status & MOUSE_ENABLED) {
821178476Sjb		scp->status &= ~(MOUSE_ENABLED | MOUSE_VISIBLE);
822178476Sjb		mark_all(scp);
823178476Sjb	    }
824178476Sjb	    else
825178476Sjb		return EINVAL;
826178476Sjb	    break;
827178476Sjb
828178476Sjb	case MOUSE_MOVEABS:
829178476Sjb	    scp->mouse_xpos = mouse->u.data.x;
830178476Sjb	    scp->mouse_ypos = mouse->u.data.y;
831178476Sjb	    set_mouse_pos(scp);
832178476Sjb	    break;
833178476Sjb
834178476Sjb	case MOUSE_MOVEREL:
835178476Sjb	    scp->mouse_xpos += mouse->u.data.x;
836178476Sjb	    scp->mouse_ypos += mouse->u.data.y;
837178476Sjb	    set_mouse_pos(scp);
838178476Sjb	    break;
839178476Sjb
840178476Sjb	case MOUSE_GETINFO:
841178476Sjb	    mouse->u.data.x = scp->mouse_xpos;
842178476Sjb	    mouse->u.data.y = scp->mouse_ypos;
843178476Sjb	    mouse->u.data.buttons = scp->mouse_buttons;
844178476Sjb	    break;
845178476Sjb
846178476Sjb	case MOUSE_ACTION:
847178476Sjb	    /* this should maybe only be settable from /dev/consolectl SOS */
848178476Sjb	    /* send out mouse event on /dev/sysmouse */
849178476Sjb	    if (cur_console->status & MOUSE_ENABLED)
850178476Sjb	    	cur_console->status |= MOUSE_VISIBLE;
851178476Sjb	    if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
852178476Sjb		u_char buf[5];
853178476Sjb		int i;
854178476Sjb
855178476Sjb		buf[0] = 0x80 | ((~mouse->u.data.buttons) & 0x07);
856178476Sjb		buf[1] = (mouse->u.data.x & 0x1fe >> 1);
857178476Sjb		buf[3] = (mouse->u.data.x & 0x1ff) - buf[1];
858178476Sjb		buf[2] = -(mouse->u.data.y & 0x1fe >> 1);
859178476Sjb		buf[4] = -(mouse->u.data.y & 0x1ff) - buf[2];
860178476Sjb		for (i=0; i<5; i++)
861178476Sjb	    		(*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[i],MOUSE_TTY);
862178476Sjb	    }
863178476Sjb	    cur_console->mouse_xpos += mouse->u.data.x;
864178476Sjb	    cur_console->mouse_ypos += mouse->u.data.y;
865178476Sjb	    if (cur_console->mouse_signal) {
866178476Sjb		cur_console->mouse_buttons = mouse->u.data.buttons;
867178476Sjb    		/* has controlling process died? */
868178476Sjb		if (cur_console->mouse_proc &&
869178476Sjb		    (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
870178476Sjb		    	cur_console->mouse_signal = 0;
871178476Sjb			cur_console->mouse_proc = NULL;
872178476Sjb			cur_console->mouse_pid = 0;
873178476Sjb		}
874178476Sjb		else
875178476Sjb		    psignal(cur_console->mouse_proc, cur_console->mouse_signal);
876178476Sjb	    }
877178476Sjb	    else {
878178476Sjb		/* process button presses */
879178476Sjb		if (cur_console->mouse_buttons != mouse->u.data.buttons) {
880178476Sjb		    cur_console->mouse_buttons = mouse->u.data.buttons;
881178476Sjb		    if (!(cur_console->status & UNKNOWN_MODE)) {
882178476Sjb			if (cur_console->mouse_buttons & LEFT_BUTTON)
883178476Sjb			    mouse_cut_start(cur_console);
884178476Sjb			else
885178476Sjb			    mouse_cut_end(cur_console);
886178476Sjb			if (cur_console->mouse_buttons & RIGHT_BUTTON ||
887178476Sjb			    cur_console->mouse_buttons & MIDDLE_BUTTON)
888178476Sjb			    mouse_paste(cur_console);
889178476Sjb		    }
890178476Sjb		}
891178476Sjb	    }
892178476Sjb	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0)
893178476Sjb		set_mouse_pos(cur_console);
894178476Sjb	    break;
895178476Sjb
896178476Sjb	default:
897178476Sjb	    return EINVAL;
898178476Sjb	}
899178476Sjb	/* make screensaver happy */
900178476Sjb	scrn_time_stamp = time.tv_sec;
901178476Sjb	if (scrn_blanked) {
902178476Sjb	    (*current_saver)(FALSE);
903178476Sjb	    mark_all(cur_console);
904178476Sjb	}
905178476Sjb	return 0;
906178476Sjb    }
907178476Sjb
908178476Sjb    case CONS_GETINFO:  	/* get current (virtual) console info */
909178476Sjb    {
910178476Sjb	vid_info_t *ptr = (vid_info_t*)data;
911178476Sjb	if (ptr->size == sizeof(struct vid_info)) {
912178476Sjb	    ptr->m_num = get_scr_num();
913178476Sjb	    ptr->mv_col = scp->xpos;
914178476Sjb	    ptr->mv_row = scp->ypos;
915178476Sjb	    ptr->mv_csz = scp->xsize;
916178476Sjb	    ptr->mv_rsz = scp->ysize;
917178476Sjb	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
918178476Sjb	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
919178476Sjb	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
920178476Sjb	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
921178476Sjb	    ptr->mv_grfc.fore = 0;      /* not supported */
922178476Sjb	    ptr->mv_grfc.back = 0;      /* not supported */
923178476Sjb	    ptr->mv_ovscan = scp->border;
924178476Sjb	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
925178476Sjb	    return 0;
926178476Sjb	}
927178476Sjb	return EINVAL;
928178476Sjb    }
929178476Sjb
930178476Sjb    case CONS_GETVERS:  	/* get version number */
931178476Sjb	*(int*)data = 0x200;    /* version 2.0 */
932178476Sjb	return 0;
933178476Sjb
934178476Sjb    /* VGA TEXT MODES */
935178476Sjb    case SW_VGA_C40x25:
936178476Sjb    case SW_VGA_C80x25: case SW_VGA_M80x25:
937178476Sjb    case SW_VGA_C80x30: case SW_VGA_M80x30:
938178476Sjb    case SW_VGA_C80x50: case SW_VGA_M80x50:
939178476Sjb    case SW_VGA_C80x60: case SW_VGA_M80x60:
940178476Sjb    case SW_B40x25:     case SW_C40x25:
941178476Sjb    case SW_B80x25:     case SW_C80x25:
942178476Sjb    case SW_ENH_B40x25: case SW_ENH_C40x25:
943178476Sjb    case SW_ENH_B80x25: case SW_ENH_C80x25:
944178476Sjb    case SW_ENH_B80x43: case SW_ENH_C80x43:
945178476Sjb
946178476Sjb	if (!crtc_vga || video_mode_ptr == NULL)
947178476Sjb	    return ENXIO;
948178476Sjb	switch (cmd & 0xff) {
949178476Sjb	case M_VGA_C80x60: case M_VGA_M80x60:
950178476Sjb	    if (!(fonts_loaded & FONT_8))
951178476Sjb		return EINVAL;
952178476Sjb	    scp->xsize = 80;
953178476Sjb	    scp->ysize = 60;
954178476Sjb	    break;
955178476Sjb	case M_VGA_C80x50: case M_VGA_M80x50:
956178476Sjb	    if (!(fonts_loaded & FONT_8))
957178476Sjb		return EINVAL;
958178476Sjb	    scp->xsize = 80;
959178476Sjb	    scp->ysize = 50;
960178476Sjb	    break;
961178476Sjb	case M_ENH_B80x43: case M_ENH_C80x43:
962178476Sjb	    if (!(fonts_loaded & FONT_8))
963178476Sjb		return EINVAL;
964178476Sjb	    scp->xsize = 80;
965178476Sjb	    scp->ysize = 43;
966178476Sjb	    break;
967178476Sjb	case M_VGA_C80x30: case M_VGA_M80x30:
968178476Sjb	    scp->xsize = 80;
969178476Sjb	    scp->ysize = 30;
970178476Sjb	    break;
971178476Sjb	default:
972178476Sjb	    if ((cmd & 0xff) > M_VGA_CG320)
973178476Sjb		return EINVAL;
974178476Sjb	    else
975178476Sjb		scp->xsize = *(video_mode_ptr+((cmd&0xff)*64));
976178476Sjb		scp->ysize = *(video_mode_ptr+((cmd&0xff)*64)+1)+1;
977178476Sjb	    break;
978178476Sjb	}
979178476Sjb	scp->mode = cmd & 0xff;
980178476Sjb	free(scp->scr_buf, M_DEVBUF);
981178476Sjb	scp->scr_buf = (u_short *)
982178476Sjb	    malloc(scp->xsize*scp->ysize*sizeof(u_short), M_DEVBUF, M_WAITOK);
983178476Sjb    	scp->cursor_pos = scp->cursor_oldpos =
984178476Sjb	    scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
985178476Sjb    	scp->mouse_pos = scp->mouse_oldpos =
986178476Sjb	    scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
987178476Sjb	    scp->mouse_xpos/8);
988178476Sjb	free(cut_buffer, M_DEVBUF);
989178476Sjb    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
990178476Sjb	cut_buffer[0] = 0x00;
991178476Sjb	if (scp == cur_console)
992178476Sjb	    set_mode(scp);
993178476Sjb	scp->status &= ~UNKNOWN_MODE;
994178476Sjb	clear_screen(scp);
995178476Sjb	if (tp->t_winsize.ws_col != scp->xsize
996178476Sjb	    || tp->t_winsize.ws_row != scp->ysize) {
997178476Sjb	    tp->t_winsize.ws_col = scp->xsize;
998178476Sjb	    tp->t_winsize.ws_row = scp->ysize;
999178476Sjb	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1000178476Sjb	}
1001178476Sjb	return 0;
1002178476Sjb
1003178476Sjb    /* GRAPHICS MODES */
1004178476Sjb    case SW_BG320:     case SW_BG640:
1005178476Sjb    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
1006178476Sjb    case SW_CG640x350: case SW_ENH_CG640:
1007178476Sjb    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
1008178476Sjb
1009178476Sjb	if (!crtc_vga || video_mode_ptr == NULL)
1010178476Sjb	    return ENXIO;
1011178476Sjb	scp->mode = cmd & 0xFF;
1012178476Sjb	scp->xpixel = (*(video_mode_ptr + (scp->mode*64))) * 8;
1013178476Sjb	scp->ypixel = (*(video_mode_ptr + (scp->mode*64) + 1) + 1) *
1014178476Sjb		     (*(video_mode_ptr + (scp->mode*64) + 2));
1015178476Sjb	if (scp == cur_console)
1016178476Sjb	    set_mode(scp);
1017178476Sjb	scp->status |= UNKNOWN_MODE;    /* graphics mode */
1018178476Sjb	/* clear_graphics();*/
1019178476Sjb
1020178476Sjb	if (tp->t_winsize.ws_xpixel != scp->xpixel
1021178476Sjb	    || tp->t_winsize.ws_ypixel != scp->ypixel) {
1022178476Sjb	    tp->t_winsize.ws_xpixel = scp->xpixel;
1023178476Sjb	    tp->t_winsize.ws_ypixel = scp->ypixel;
1024178476Sjb	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1025178476Sjb	}
1026178476Sjb	return 0;
1027178476Sjb
1028178476Sjb    case VT_SETMODE:    	/* set screen switcher mode */
1029178476Sjb    {
1030178476Sjb	struct vt_mode *mode;
1031178476Sjb
1032178476Sjb	mode = (struct vt_mode *)data;
1033178476Sjb	if (ISSIGVALID(mode->relsig) && ISSIGVALID(mode->acqsig) &&
1034178476Sjb	    ISSIGVALID(mode->frsig)) {
1035178476Sjb	    bcopy(data, &scp->smode, sizeof(struct vt_mode));
1036178476Sjb	    if (scp->smode.mode == VT_PROCESS) {
1037178476Sjb		scp->proc = p;
1038178476Sjb		scp->pid = scp->proc->p_pid;
1039178476Sjb	    }
1040178476Sjb	    return 0;
1041178476Sjb	} else
1042178476Sjb	    return EINVAL;
1043    }
1044
1045    case VT_GETMODE:    	/* get screen switcher mode */
1046	bcopy(&scp->smode, data, sizeof(struct vt_mode));
1047	return 0;
1048
1049    case VT_RELDISP:    	/* screen switcher ioctl */
1050	switch(*data) {
1051	case VT_FALSE:  	/* user refuses to release screen, abort */
1052	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1053		old_scp->status &= ~SWITCH_WAIT_REL;
1054		switch_in_progress = FALSE;
1055		return 0;
1056	    }
1057	    return EINVAL;
1058
1059	case VT_TRUE:   	/* user has released screen, go on */
1060	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1061		scp->status &= ~SWITCH_WAIT_REL;
1062		exchange_scr();
1063		if (new_scp->smode.mode == VT_PROCESS) {
1064		    new_scp->status |= SWITCH_WAIT_ACQ;
1065		    psignal(new_scp->proc, new_scp->smode.acqsig);
1066		}
1067		else
1068		    switch_in_progress = FALSE;
1069		return 0;
1070	    }
1071	    return EINVAL;
1072
1073	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
1074	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
1075		scp->status &= ~SWITCH_WAIT_ACQ;
1076		switch_in_progress = FALSE;
1077		return 0;
1078	    }
1079	    return EINVAL;
1080
1081	default:
1082	    return EINVAL;
1083	}
1084	/* NOT REACHED */
1085
1086    case VT_OPENQRY:    	/* return free virtual console */
1087	for (i = 0; i < MAXCONS; i++) {
1088	    tp = VIRTUAL_TTY(i);
1089	    if (!(tp->t_state & TS_ISOPEN)) {
1090		*data = i + 1;
1091		return 0;
1092	    }
1093	}
1094	return EINVAL;
1095
1096    case VT_ACTIVATE:   	/* switch to screen *data */
1097	return switch_scr(scp, (*data) - 1);
1098
1099    case VT_WAITACTIVE: 	/* wait for switch to occur */
1100	if (*data > MAXCONS || *data < 0)
1101	    return EINVAL;
1102	if (minor(dev) == (*data) - 1)
1103	    return 0;
1104	if (*data == 0) {
1105	    if (scp == cur_console)
1106		return 0;
1107	}
1108	else
1109	    scp = console[(*data) - 1];
1110	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
1111			     "waitvt", 0)) == ERESTART) ;
1112	return error;
1113
1114    case VT_GETACTIVE:
1115	*data = get_scr_num()+1;
1116	return 0;
1117
1118    case KDENABIO:      	/* allow io operations */
1119	error = suser(p->p_ucred, &p->p_acflag);
1120	if (error != 0)
1121	    return error;
1122	if (securelevel > 0)
1123	    return EPERM;
1124	fp = (struct trapframe *)p->p_md.md_regs;
1125	fp->tf_eflags |= PSL_IOPL;
1126	return 0;
1127
1128    case KDDISABIO:     	/* disallow io operations (default) */
1129	fp = (struct trapframe *)p->p_md.md_regs;
1130	fp->tf_eflags &= ~PSL_IOPL;
1131	return 0;
1132
1133    case KDSETMODE:     	/* set current mode of this (virtual) console */
1134	switch (*data) {
1135	case KD_TEXT:   	/* switch to TEXT (known) mode */
1136	    /* restore fonts & palette ! */
1137	    if (crtc_vga) {
1138		if (fonts_loaded & FONT_8)
1139		    copy_font(LOAD, FONT_8, font_8);
1140		if (fonts_loaded & FONT_14)
1141		    copy_font(LOAD, FONT_14, font_14);
1142		if (fonts_loaded & FONT_16)
1143		    copy_font(LOAD, FONT_16, font_16);
1144		if (flags & CHAR_CURSOR)
1145		    set_destructive_cursor(scp);
1146		load_palette(palette);
1147	    }
1148	    /* FALL THROUGH */
1149
1150	case KD_TEXT1:  	/* switch to TEXT (known) mode */
1151	    /* no restore fonts & palette */
1152	    if (crtc_vga && video_mode_ptr)
1153		set_mode(scp);
1154	    scp->status &= ~UNKNOWN_MODE;
1155	    clear_screen(scp);
1156	    return 0;
1157
1158	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
1159	    scp->status |= UNKNOWN_MODE;
1160	    return 0;
1161	default:
1162	    return EINVAL;
1163	}
1164	/* NOT REACHED */
1165
1166    case KDGETMODE:     	/* get current mode of this (virtual) console */
1167	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
1168	return 0;
1169
1170    case KDSBORDER:     	/* set border color of this (virtual) console */
1171	if (!crtc_vga)
1172	    return ENXIO;
1173	scp->border = *data;
1174	if (scp == cur_console)
1175	    set_border(scp->border);
1176	return 0;
1177
1178    case KDSKBSTATE:    	/* set keyboard state (locks) */
1179	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
1180	    scp->status &= ~LOCK_KEY_MASK;
1181	    scp->status |= *data;
1182	    if (scp == cur_console)
1183		update_leds(scp->status);
1184	    return 0;
1185	}
1186	return EINVAL;
1187
1188    case KDGKBSTATE:    	/* get keyboard state (locks) */
1189	*data = scp->status & LOCK_KEY_MASK;
1190	return 0;
1191
1192    case KDSETRAD:      	/* set keyboard repeat & delay rates */
1193	if (*data & 0x80)
1194	    return EINVAL;
1195	if (sc_kbdc != NULL)
1196	    set_keyboard(KBDC_SET_TYPEMATIC, *data);
1197	return 0;
1198
1199    case KDSKBMODE:     	/* set keyboard mode */
1200	switch (*data) {
1201	case K_RAW: 		/* switch to RAW scancode mode */
1202	    scp->status |= KBD_RAW_MODE;
1203	    return 0;
1204
1205	case K_XLATE:   	/* switch to XLT ascii mode */
1206	    if (scp == cur_console && scp->status & KBD_RAW_MODE)
1207		shfts = ctls = alts = agrs = metas = 0;
1208	    scp->status &= ~KBD_RAW_MODE;
1209	    return 0;
1210	default:
1211	    return EINVAL;
1212	}
1213	/* NOT REACHED */
1214
1215    case KDGKBMODE:     	/* get keyboard mode */
1216	*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
1217	return 0;
1218
1219    case KDMKTONE:      	/* sound the bell */
1220	if (*(int*)data)
1221	    do_bell(scp, (*(int*)data)&0xffff,
1222		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1223	else
1224	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1225	return 0;
1226
1227    case KIOCSOUND:     	/* make tone (*data) hz */
1228	if (scp == cur_console) {
1229	    if (*(int*)data) {
1230		int pitch = timer_freq / *(int*)data;
1231
1232		/* set command for counter 2, 2 byte write */
1233		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1234		    return EBUSY;
1235
1236		/* set pitch */
1237		outb(TIMER_CNTR2, pitch);
1238		outb(TIMER_CNTR2, (pitch>>8));
1239
1240		/* enable counter 2 output to speaker */
1241		outb(IO_PPI, inb(IO_PPI) | 3);
1242	    }
1243	    else {
1244		/* disable counter 2 output to speaker */
1245		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1246		release_timer2();
1247	    }
1248	}
1249	return 0;
1250
1251    case KDGKBTYPE:     	/* get keyboard type */
1252	*data = 0;  		/* type not known (yet) */
1253	return 0;
1254
1255    case KDSETLED:      	/* set keyboard LED status */
1256	if (*data >= 0 && *data <= LED_MASK) {
1257	    scp->status &= ~LED_MASK;
1258	    scp->status |= *data;
1259	    if (scp == cur_console)
1260		update_leds(scp->status);
1261	    return 0;
1262	}
1263	return EINVAL;
1264
1265    case KDGETLED:      	/* get keyboard LED status */
1266	*data = scp->status & LED_MASK;
1267	return 0;
1268
1269    case GETFKEY:       	/* get functionkey string */
1270	if (*(u_short*)data < n_fkey_tab) {
1271	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1272	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1273		  fkey_tab[ptr->keynum].len);
1274	    ptr->flen = fkey_tab[ptr->keynum].len;
1275	    return 0;
1276	}
1277	else
1278	    return EINVAL;
1279
1280    case SETFKEY:       	/* set functionkey string */
1281	if (*(u_short*)data < n_fkey_tab) {
1282	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1283	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1284		  min(ptr->flen, MAXFK));
1285	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1286	    return 0;
1287	}
1288	else
1289	    return EINVAL;
1290
1291    case GIO_SCRNMAP:   	/* get output translation table */
1292	bcopy(&scr_map, data, sizeof(scr_map));
1293	return 0;
1294
1295    case PIO_SCRNMAP:   	/* set output translation table */
1296	bcopy(data, &scr_map, sizeof(scr_map));
1297	for (i=0; i<sizeof(scr_map); i++)
1298	    scr_rmap[scr_map[i]] = i;
1299	return 0;
1300
1301    case GIO_KEYMAP:    	/* get keyboard translation table */
1302	bcopy(&key_map, data, sizeof(key_map));
1303	return 0;
1304
1305    case PIO_KEYMAP:    	/* set keyboard translation table */
1306	bcopy(data, &key_map, sizeof(key_map));
1307	return 0;
1308
1309    case PIO_FONT8x8:   	/* set 8x8 dot font */
1310	if (!crtc_vga)
1311	    return ENXIO;
1312	bcopy(data, font_8, 8*256);
1313	fonts_loaded |= FONT_8;
1314	copy_font(LOAD, FONT_8, font_8);
1315	if (flags & CHAR_CURSOR)
1316	    set_destructive_cursor(scp);
1317	return 0;
1318
1319    case GIO_FONT8x8:   	/* get 8x8 dot font */
1320	if (!crtc_vga)
1321	    return ENXIO;
1322	if (fonts_loaded & FONT_8) {
1323	    bcopy(font_8, data, 8*256);
1324	    return 0;
1325	}
1326	else
1327	    return ENXIO;
1328
1329    case PIO_FONT8x14:  	/* set 8x14 dot font */
1330	if (!crtc_vga)
1331	    return ENXIO;
1332	bcopy(data, font_14, 14*256);
1333	fonts_loaded |= FONT_14;
1334	copy_font(LOAD, FONT_14, font_14);
1335	if (flags & CHAR_CURSOR)
1336	    set_destructive_cursor(scp);
1337	return 0;
1338
1339    case GIO_FONT8x14:  	/* get 8x14 dot font */
1340	if (!crtc_vga)
1341	    return ENXIO;
1342	if (fonts_loaded & FONT_14) {
1343	    bcopy(font_14, data, 14*256);
1344	    return 0;
1345	}
1346	else
1347	    return ENXIO;
1348
1349    case PIO_FONT8x16:  	/* set 8x16 dot font */
1350	if (!crtc_vga)
1351	    return ENXIO;
1352	bcopy(data, font_16, 16*256);
1353	fonts_loaded |= FONT_16;
1354	copy_font(LOAD, FONT_16, font_16);
1355	if (flags & CHAR_CURSOR)
1356	    set_destructive_cursor(scp);
1357	return 0;
1358
1359    case GIO_FONT8x16:  	/* get 8x16 dot font */
1360	if (!crtc_vga)
1361	    return ENXIO;
1362	if (fonts_loaded & FONT_16) {
1363	    bcopy(font_16, data, 16*256);
1364	    return 0;
1365	}
1366	else
1367	    return ENXIO;
1368    default:
1369	break;
1370    }
1371
1372    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1373    if (error >= 0)
1374	return(error);
1375    error = ttioctl(tp, cmd, data, flag);
1376    if (error >= 0)
1377	return(error);
1378    return(ENOTTY);
1379}
1380
1381static void
1382scstart(struct tty *tp)
1383{
1384    struct clist *rbp;
1385    int s, len;
1386    u_char buf[PCBURST];
1387    scr_stat *scp = get_scr_stat(tp->t_dev);
1388
1389    if (scp->status & SLKED || blink_in_progress)
1390	return; /* XXX who repeats the call when the above flags are cleared? */
1391    s = spltty();
1392    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1393	tp->t_state |= TS_BUSY;
1394	rbp = &tp->t_outq;
1395	while (rbp->c_cc) {
1396	    len = q_to_b(rbp, buf, PCBURST);
1397	    splx(s);
1398	    ansi_put(scp, buf, len);
1399	    s = spltty();
1400	}
1401	tp->t_state &= ~TS_BUSY;
1402	ttwwakeup(tp);
1403    }
1404    splx(s);
1405}
1406
1407static void
1408scmousestart(struct tty *tp)
1409{
1410    struct clist *rbp;
1411    int s;
1412    u_char buf[PCBURST];
1413
1414    s = spltty();
1415    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1416	tp->t_state |= TS_BUSY;
1417	rbp = &tp->t_outq;
1418	while (rbp->c_cc) {
1419	    q_to_b(rbp, buf, PCBURST);
1420	}
1421	tp->t_state &= ~TS_BUSY;
1422	ttwwakeup(tp);
1423    }
1424    splx(s);
1425}
1426
1427void
1428sccnprobe(struct consdev *cp)
1429{
1430    struct isa_device *dvp;
1431
1432    /*
1433     * Take control if we are the highest priority enabled display device.
1434     */
1435    dvp = find_display();
1436    if (dvp == NULL || dvp->id_driver != &scdriver) {
1437	cp->cn_pri = CN_DEAD;
1438	return;
1439    }
1440
1441    /* initialize required fields */
1442    cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLE);
1443    cp->cn_pri = CN_INTERNAL;
1444
1445    sc_kbdc = kbdc_open(sc_port);
1446}
1447
1448void
1449sccninit(struct consdev *cp)
1450{
1451    scinit();
1452}
1453
1454void
1455sccnputc(dev_t dev, int c)
1456{
1457    u_char buf[1];
1458    int s;
1459    scr_stat *scp = console[0];
1460    term_stat save = scp->term;
1461
1462    scp->term = kernel_console;
1463    current_default = &kernel_default;
1464    if (scp == cur_console && !(scp->status & UNKNOWN_MODE))
1465	remove_cursor_image(scp);
1466    buf[0] = c;
1467    ansi_put(scp, buf, 1);
1468    kernel_console = scp->term;
1469    current_default = &user_default;
1470    scp->term = save;
1471    s = splclock();
1472    if (scp == cur_console && !(scp->status & UNKNOWN_MODE)) {
1473	if (/* timer not running && */ (scp->start <= scp->end)) {
1474	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1475		   (1 + scp->end - scp->start) * sizeof(u_short));
1476	    scp->start = scp->xsize * scp->ysize;
1477	    scp->end = 0;
1478	}
1479    	scp->cursor_oldpos = scp->cursor_pos;
1480	draw_cursor_image(scp);
1481    }
1482    splx(s);
1483}
1484
1485int
1486sccngetc(dev_t dev)
1487{
1488    int s = spltty();       /* block scintr while we poll */
1489    int c = scgetc(SCGETC_CN);
1490    splx(s);
1491    return(c);
1492}
1493
1494int
1495sccncheckc(dev_t dev)
1496{
1497    int c, s;
1498
1499    s = spltty();
1500    c = scgetc(SCGETC_CN | SCGETC_NONBLOCK);
1501    splx(s);
1502    return(c == NOKEY ? -1 : c);	/* c == -1 can't happen */
1503}
1504
1505static scr_stat
1506*get_scr_stat(dev_t dev)
1507{
1508    int unit = minor(dev);
1509
1510    if (unit == SC_CONSOLE)
1511	return console[0];
1512    if (unit >= MAXCONS || unit < 0)
1513	return(NULL);
1514    return console[unit];
1515}
1516
1517static int
1518get_scr_num()
1519{
1520    int i = 0;
1521
1522    while ((i < MAXCONS) && (cur_console != console[i]))
1523	i++;
1524    return i < MAXCONS ? i : 0;
1525}
1526
1527static void
1528scrn_timer()
1529{
1530    scr_stat *scp = cur_console;
1531    int s = spltty();
1532
1533    /*
1534     * With release 2.1 of the Xaccel server, the keyboard is left
1535     * hanging pretty often. Apparently an interrupt from the
1536     * keyboard is lost, and I don't know why (yet).
1537     * This ugly hack calls scintr if input is ready for the keyboard
1538     * and conveniently hides the problem.			XXX
1539     */
1540    /* Try removing anything stuck in the keyboard controller; whether
1541     * it's a keyboard scan code or mouse data. `scintr()' doesn't
1542     * read the mouse data directly, but `kbdio' routines will, as a
1543     * side effect.
1544     */
1545    if (kbdc_lock(sc_kbdc, TRUE)) {
1546	/*
1547	 * We have seen the lock flag is not set. Let's reset the flag early;
1548	 * otherwise `update_led()' failes which may want the lock
1549	 * during `scintr()'.
1550	 */
1551	kbdc_lock(sc_kbdc, FALSE);
1552	if (kbdc_data_ready(sc_kbdc))
1553	    scintr(0);
1554    }
1555
1556    /* should we just return ? */
1557    if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
1558	timeout((timeout_func_t)scrn_timer, 0, hz/10);
1559	splx(s);
1560	return;
1561    }
1562
1563    if (!scrn_blanked) {
1564	/* update screen image */
1565	if (scp->start <= scp->end) {
1566	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1567		   (1 + scp->end - scp->start) * sizeof(u_short));
1568	}
1569
1570	/* update "pseudo" mouse pointer image */
1571	if ((scp->status & MOUSE_VISIBLE) && crtc_vga) {
1572	    /* did mouse move since last time ? */
1573	    if (scp->status & MOUSE_MOVED) {
1574		/* do we need to remove old mouse pointer image ? */
1575		if (scp->mouse_cut_start != NULL ||
1576		    (scp->mouse_pos-scp->scr_buf) <= scp->start ||
1577		    (scp->mouse_pos+scp->xsize+1-scp->scr_buf) >= scp->end) {
1578		    remove_mouse_image(scp);
1579		}
1580		scp->status &= ~MOUSE_MOVED;
1581		draw_mouse_image(scp);
1582	    }
1583	    else {
1584		/* mouse didn't move, has it been overwritten ? */
1585		if ((scp->mouse_pos+scp->xsize+1-scp->scr_buf) >= scp->start &&
1586		    (scp->mouse_pos - scp->scr_buf) <= scp->end) {
1587		    draw_mouse_image(scp);
1588		}
1589	    }
1590	}
1591
1592	/* update cursor image */
1593	if (scp->status & CURSOR_ENABLED) {
1594	    /* did cursor move since last time ? */
1595	    if (scp->cursor_pos != scp->cursor_oldpos) {
1596		/* do we need to remove old cursor image ? */
1597		if ((scp->cursor_oldpos - scp->scr_buf) < scp->start ||
1598		    ((scp->cursor_oldpos - scp->scr_buf) > scp->end)) {
1599		    remove_cursor_image(scp);
1600		}
1601    		scp->cursor_oldpos = scp->cursor_pos;
1602		draw_cursor_image(scp);
1603	    }
1604	    else {
1605		/* cursor didn't move, has it been overwritten ? */
1606		if (scp->cursor_pos - scp->scr_buf >= scp->start &&
1607		    scp->cursor_pos - scp->scr_buf <= scp->end) {
1608		    	draw_cursor_image(scp);
1609		} else {
1610		    /* if its a blinking cursor, we may have to update it */
1611		    if (flags & BLINK_CURSOR)
1612			draw_cursor_image(scp);
1613		}
1614	    }
1615	    blinkrate++;
1616	}
1617
1618	if (scp->mouse_cut_start != NULL)
1619	    draw_cutmarking(scp);
1620
1621	scp->end = 0;
1622	scp->start = scp->xsize*scp->ysize;
1623    }
1624    if (scrn_blank_time && (time.tv_sec > scrn_time_stamp+scrn_blank_time))
1625	(*current_saver)(TRUE);
1626    timeout((timeout_func_t)scrn_timer, 0, hz/25);
1627    splx(s);
1628}
1629
1630static void
1631clear_screen(scr_stat *scp)
1632{
1633    move_crsr(scp, 0, 0);
1634    scp->cursor_oldpos = scp->cursor_pos;
1635    fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1636	  scp->xsize * scp->ysize);
1637    mark_all(scp);
1638    remove_cutmarking(scp);
1639}
1640
1641static int
1642switch_scr(scr_stat *scp, u_int next_scr)
1643{
1644    if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
1645	switch_in_progress = FALSE;
1646
1647    if (next_scr >= MAXCONS || switch_in_progress ||
1648	(cur_console->smode.mode == VT_AUTO
1649	 && cur_console->status & UNKNOWN_MODE)) {
1650	do_bell(scp, BELL_PITCH, BELL_DURATION);
1651	return EINVAL;
1652    }
1653
1654    /* is the wanted virtual console open ? */
1655    if (next_scr) {
1656	struct tty *tp = VIRTUAL_TTY(next_scr);
1657	if (!(tp->t_state & TS_ISOPEN)) {
1658	    do_bell(scp, BELL_PITCH, BELL_DURATION);
1659	    return EINVAL;
1660	}
1661    }
1662    /* delay switch if actively updating screen */
1663    if (write_in_progress || blink_in_progress) {
1664	delayed_next_scr = next_scr+1;
1665	return 0;
1666    }
1667    switch_in_progress = TRUE;
1668    old_scp = cur_console;
1669    new_scp = console[next_scr];
1670    wakeup((caddr_t)&new_scp->smode);
1671    if (new_scp == old_scp) {
1672	switch_in_progress = FALSE;
1673	delayed_next_scr = FALSE;
1674	return 0;
1675    }
1676
1677    /* has controlling process died? */
1678    if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1679	old_scp->smode.mode = VT_AUTO;
1680    if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1681	new_scp->smode.mode = VT_AUTO;
1682
1683    /* check the modes and switch appropriately */
1684    if (old_scp->smode.mode == VT_PROCESS) {
1685	old_scp->status |= SWITCH_WAIT_REL;
1686	psignal(old_scp->proc, old_scp->smode.relsig);
1687    }
1688    else {
1689	exchange_scr();
1690	if (new_scp->smode.mode == VT_PROCESS) {
1691	    new_scp->status |= SWITCH_WAIT_ACQ;
1692	    psignal(new_scp->proc, new_scp->smode.acqsig);
1693	}
1694	else
1695	    switch_in_progress = FALSE;
1696    }
1697    return 0;
1698}
1699
1700static void
1701exchange_scr(void)
1702{
1703    move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1704    cur_console = new_scp;
1705    if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
1706	if (crtc_vga && video_mode_ptr)
1707	    set_mode(new_scp);
1708    }
1709    move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1710    if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1711	if (flags & CHAR_CURSOR)
1712	    set_destructive_cursor(new_scp);
1713	load_palette(palette);
1714    }
1715    if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1716	shfts = ctls = alts = agrs = metas = 0;
1717    update_leds(new_scp->status);
1718    delayed_next_scr = FALSE;
1719    mark_all(new_scp);
1720}
1721
1722static void
1723scan_esc(scr_stat *scp, u_char c)
1724{
1725    static u_char ansi_col[16] =
1726	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1727    int i, n;
1728    u_short *src, *dst, count;
1729
1730    if (scp->term.esc == 1) {
1731	switch (c) {
1732
1733	case '7':   /* Save cursor position */
1734	    scp->saved_xpos = scp->xpos;
1735	    scp->saved_ypos = scp->ypos;
1736	    break;
1737
1738	case '8':   /* Restore saved cursor position */
1739	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
1740		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
1741	    break;
1742
1743	case '[':   /* Start ESC [ sequence */
1744	    scp->term.esc = 2;
1745	    scp->term.last_param = -1;
1746	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1747		scp->term.param[i] = 1;
1748	    scp->term.num_param = 0;
1749	    return;
1750
1751	case 'M':   /* Move cursor up 1 line, scroll if at top */
1752	    if (scp->ypos > 0)
1753		move_crsr(scp, scp->xpos, scp->ypos - 1);
1754	    else {
1755		bcopyw(scp->scr_buf, scp->scr_buf + scp->xsize,
1756		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
1757		fillw(scp->term.cur_color | scr_map[0x20],
1758		      scp->scr_buf, scp->xsize);
1759    		mark_all(scp);
1760	    }
1761	    break;
1762#if notyet
1763	case 'Q':
1764	    scp->term.esc = 4;
1765	    break;
1766#endif
1767	case 'c':   /* Clear screen & home */
1768	    clear_screen(scp);
1769	    break;
1770	}
1771    }
1772    else if (scp->term.esc == 2) {
1773	if (c >= '0' && c <= '9') {
1774	    if (scp->term.num_param < MAX_ESC_PAR) {
1775	    if (scp->term.last_param != scp->term.num_param) {
1776		scp->term.last_param = scp->term.num_param;
1777		scp->term.param[scp->term.num_param] = 0;
1778	    }
1779	    else
1780		scp->term.param[scp->term.num_param] *= 10;
1781	    scp->term.param[scp->term.num_param] += c - '0';
1782	    return;
1783	    }
1784	}
1785	scp->term.num_param = scp->term.last_param + 1;
1786	switch (c) {
1787
1788	case ';':
1789	    if (scp->term.num_param < MAX_ESC_PAR)
1790		return;
1791	    break;
1792
1793	case '=':
1794	    scp->term.esc = 3;
1795	    scp->term.last_param = -1;
1796	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1797		scp->term.param[i] = 1;
1798	    scp->term.num_param = 0;
1799	    return;
1800
1801	case 'A':   /* up n rows */
1802	    n = scp->term.param[0]; if (n < 1) n = 1;
1803	    move_crsr(scp, scp->xpos, scp->ypos - n);
1804	    break;
1805
1806	case 'B':   /* down n rows */
1807	    n = scp->term.param[0]; if (n < 1) n = 1;
1808	    move_crsr(scp, scp->xpos, scp->ypos + n);
1809	    break;
1810
1811	case 'C':   /* right n columns */
1812	    n = scp->term.param[0]; if (n < 1) n = 1;
1813	    move_crsr(scp, scp->xpos + n, scp->ypos);
1814	    break;
1815
1816	case 'D':   /* left n columns */
1817	    n = scp->term.param[0]; if (n < 1) n = 1;
1818	    move_crsr(scp, scp->xpos - n, scp->ypos);
1819	    break;
1820
1821	case 'E':   /* cursor to start of line n lines down */
1822	    n = scp->term.param[0]; if (n < 1) n = 1;
1823	    move_crsr(scp, 0, scp->ypos + n);
1824	    break;
1825
1826	case 'F':   /* cursor to start of line n lines up */
1827	    n = scp->term.param[0]; if (n < 1) n = 1;
1828	    move_crsr(scp, 0, scp->ypos - n);
1829	    break;
1830
1831	case 'f':   /* Cursor move */
1832	case 'H':
1833	    if (scp->term.num_param == 0)
1834		move_crsr(scp, 0, 0);
1835	    else if (scp->term.num_param == 2)
1836		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
1837	    break;
1838
1839	case 'J':   /* Clear all or part of display */
1840	    if (scp->term.num_param == 0)
1841		n = 0;
1842	    else
1843		n = scp->term.param[0];
1844	    switch (n) {
1845	    case 0: /* clear form cursor to end of display */
1846		fillw(scp->term.cur_color | scr_map[0x20],
1847		      scp->cursor_pos,
1848		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
1849    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1850    		mark_for_update(scp, scp->xsize * scp->ysize);
1851		remove_cutmarking(scp);
1852		break;
1853	    case 1: /* clear from beginning of display to cursor */
1854		fillw(scp->term.cur_color | scr_map[0x20],
1855		      scp->scr_buf,
1856		      scp->cursor_pos - scp->scr_buf);
1857    		mark_for_update(scp, 0);
1858    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1859		remove_cutmarking(scp);
1860		break;
1861	    case 2: /* clear entire display */
1862		fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1863		      scp->xsize * scp->ysize);
1864		mark_all(scp);
1865		remove_cutmarking(scp);
1866		break;
1867	    }
1868	    break;
1869
1870	case 'K':   /* Clear all or part of line */
1871	    if (scp->term.num_param == 0)
1872		n = 0;
1873	    else
1874		n = scp->term.param[0];
1875	    switch (n) {
1876	    case 0: /* clear form cursor to end of line */
1877		fillw(scp->term.cur_color | scr_map[0x20],
1878		      scp->cursor_pos,
1879		      scp->xsize - scp->xpos);
1880    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1881    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
1882				scp->xsize - scp->xpos);
1883		break;
1884	    case 1: /* clear from beginning of line to cursor */
1885		fillw(scp->term.cur_color | scr_map[0x20],
1886		      scp->cursor_pos - scp->xpos,
1887		      scp->xpos + 1);
1888    		mark_for_update(scp, scp->ypos * scp->xsize);
1889    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1890		break;
1891	    case 2: /* clear entire line */
1892		fillw(scp->term.cur_color | scr_map[0x20],
1893		      scp->cursor_pos - scp->xpos,
1894		      scp->xsize);
1895    		mark_for_update(scp, scp->ypos * scp->xsize);
1896    		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
1897		break;
1898	    }
1899	    break;
1900
1901	case 'L':   /* Insert n lines */
1902	    n = scp->term.param[0]; if (n < 1) n = 1;
1903	    if (n > scp->ysize - scp->ypos)
1904		n = scp->ysize - scp->ypos;
1905	    src = scp->scr_buf + scp->ypos * scp->xsize;
1906	    dst = src + n * scp->xsize;
1907	    count = scp->ysize - (scp->ypos + n);
1908	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1909	    fillw(scp->term.cur_color | scr_map[0x20], src,
1910		  n * scp->xsize);
1911	    mark_for_update(scp, scp->ypos * scp->xsize);
1912	    mark_for_update(scp, scp->xsize * scp->ysize);
1913	    break;
1914
1915	case 'M':   /* Delete n lines */
1916	    n = scp->term.param[0]; if (n < 1) n = 1;
1917	    if (n > scp->ysize - scp->ypos)
1918		n = scp->ysize - scp->ypos;
1919	    dst = scp->scr_buf + scp->ypos * scp->xsize;
1920	    src = dst + n * scp->xsize;
1921	    count = scp->ysize - (scp->ypos + n);
1922	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1923	    src = dst + count * scp->xsize;
1924	    fillw(scp->term.cur_color | scr_map[0x20], src,
1925		  n * scp->xsize);
1926	    mark_for_update(scp, scp->ypos * scp->xsize);
1927	    mark_for_update(scp, scp->xsize * scp->ysize);
1928	    break;
1929
1930	case 'P':   /* Delete n chars */
1931	    n = scp->term.param[0]; if (n < 1) n = 1;
1932	    if (n > scp->xsize - scp->xpos)
1933		n = scp->xsize - scp->xpos;
1934	    dst = scp->cursor_pos;
1935	    src = dst + n;
1936	    count = scp->xsize - (scp->xpos + n);
1937	    bcopyw(src, dst, count * sizeof(u_short));
1938	    src = dst + count;
1939	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1940	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1941	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1942	    break;
1943
1944	case '@':   /* Insert n chars */
1945	    n = scp->term.param[0]; if (n < 1) n = 1;
1946	    if (n > scp->xsize - scp->xpos)
1947		n = scp->xsize - scp->xpos;
1948	    src = scp->cursor_pos;
1949	    dst = src + n;
1950	    count = scp->xsize - (scp->xpos + n);
1951	    bcopyw(src, dst, count * sizeof(u_short));
1952	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1953	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1954	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1955	    break;
1956
1957	case 'S':   /* scroll up n lines */
1958	    n = scp->term.param[0]; if (n < 1)  n = 1;
1959	    if (n > scp->ysize)
1960		n = scp->ysize;
1961	    bcopyw(scp->scr_buf + (scp->xsize * n),
1962		   scp->scr_buf,
1963		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
1964	    fillw(scp->term.cur_color | scr_map[0x20],
1965		  scp->scr_buf + scp->xsize * (scp->ysize - n),
1966		  scp->xsize * n);
1967    	    mark_all(scp);
1968	    break;
1969
1970	case 'T':   /* scroll down n lines */
1971	    n = scp->term.param[0]; if (n < 1)  n = 1;
1972	    if (n > scp->ysize)
1973		n = scp->ysize;
1974	    bcopyw(scp->scr_buf,
1975		  scp->scr_buf + (scp->xsize * n),
1976		  scp->xsize * (scp->ysize - n) *
1977		  sizeof(u_short));
1978	    fillw(scp->term.cur_color | scr_map[0x20],
1979		  scp->scr_buf, scp->xsize * n);
1980    	    mark_all(scp);
1981	    break;
1982
1983	case 'X':   /* erase n characters in line */
1984	    n = scp->term.param[0]; if (n < 1)  n = 1;
1985	    if (n > scp->xsize - scp->xpos)
1986		n = scp->xsize - scp->xpos;
1987	    fillw(scp->term.cur_color | scr_map[0x20],
1988		  scp->cursor_pos, n);
1989	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1990	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
1991	    break;
1992
1993	case 'Z':   /* move n tabs backwards */
1994	    n = scp->term.param[0]; if (n < 1)  n = 1;
1995	    if ((i = scp->xpos & 0xf8) == scp->xpos)
1996		i -= 8*n;
1997	    else
1998		i -= 8*(n-1);
1999	    if (i < 0)
2000		i = 0;
2001	    move_crsr(scp, i, scp->ypos);
2002	    break;
2003
2004	case '`':   /* move cursor to column n */
2005	    n = scp->term.param[0]; if (n < 1)  n = 1;
2006	    move_crsr(scp, n - 1, scp->ypos);
2007	    break;
2008
2009	case 'a':   /* move cursor n columns to the right */
2010	    n = scp->term.param[0]; if (n < 1)  n = 1;
2011	    move_crsr(scp, scp->xpos + n, scp->ypos);
2012	    break;
2013
2014	case 'd':   /* move cursor to row n */
2015	    n = scp->term.param[0]; if (n < 1)  n = 1;
2016	    move_crsr(scp, scp->xpos, n - 1);
2017	    break;
2018
2019	case 'e':   /* move cursor n rows down */
2020	    n = scp->term.param[0]; if (n < 1)  n = 1;
2021	    move_crsr(scp, scp->xpos, scp->ypos + n);
2022	    break;
2023
2024	case 'm':   /* change attribute */
2025	    if (scp->term.num_param == 0) {
2026		scp->term.attr_mask = NORMAL_ATTR;
2027		scp->term.cur_attr =
2028		    scp->term.cur_color = scp->term.std_color;
2029		break;
2030	    }
2031	    for (i = 0; i < scp->term.num_param; i++) {
2032		switch (n = scp->term.param[i]) {
2033		case 0: /* back to normal */
2034		    scp->term.attr_mask = NORMAL_ATTR;
2035		    scp->term.cur_attr =
2036			scp->term.cur_color = scp->term.std_color;
2037		    break;
2038		case 1: /* bold */
2039		    scp->term.attr_mask |= BOLD_ATTR;
2040		    scp->term.cur_attr = mask2attr(&scp->term);
2041		    break;
2042		case 4: /* underline */
2043		    scp->term.attr_mask |= UNDERLINE_ATTR;
2044		    scp->term.cur_attr = mask2attr(&scp->term);
2045		    break;
2046		case 5: /* blink */
2047		    scp->term.attr_mask |= BLINK_ATTR;
2048		    scp->term.cur_attr = mask2attr(&scp->term);
2049		    break;
2050		case 7: /* reverse video */
2051		    scp->term.attr_mask |= REVERSE_ATTR;
2052		    scp->term.cur_attr = mask2attr(&scp->term);
2053		    break;
2054		case 30: case 31: /* set fg color */
2055		case 32: case 33: case 34:
2056		case 35: case 36: case 37:
2057		    scp->term.attr_mask |= FOREGROUND_CHANGED;
2058		    scp->term.cur_color =
2059			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
2060		    scp->term.cur_attr = mask2attr(&scp->term);
2061		    break;
2062		case 40: case 41: /* set bg color */
2063		case 42: case 43: case 44:
2064		case 45: case 46: case 47:
2065		    scp->term.attr_mask |= BACKGROUND_CHANGED;
2066		    scp->term.cur_color =
2067			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
2068		    scp->term.cur_attr = mask2attr(&scp->term);
2069		    break;
2070		}
2071	    }
2072	    break;
2073
2074	case 's':   /* Save cursor position */
2075	    scp->saved_xpos = scp->xpos;
2076	    scp->saved_ypos = scp->ypos;
2077	    break;
2078
2079	case 'u':   /* Restore saved cursor position */
2080	    if (scp->saved_xpos >= 0 && scp->saved_ypos >= 0)
2081		move_crsr(scp, scp->saved_xpos, scp->saved_ypos);
2082	    break;
2083
2084	case 'x':
2085	    if (scp->term.num_param == 0)
2086		n = 0;
2087	    else
2088		n = scp->term.param[0];
2089	    switch (n) {
2090	    case 0:     /* reset attributes */
2091		scp->term.attr_mask = NORMAL_ATTR;
2092		scp->term.cur_attr =
2093		    scp->term.cur_color = scp->term.std_color =
2094		    current_default->std_color;
2095		scp->term.rev_color = current_default->rev_color;
2096		break;
2097	    case 1:     /* set ansi background */
2098		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2099		scp->term.cur_color = scp->term.std_color =
2100		    (scp->term.std_color & 0x0F00) |
2101		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2102		scp->term.cur_attr = mask2attr(&scp->term);
2103		break;
2104	    case 2:     /* set ansi foreground */
2105		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2106		scp->term.cur_color = scp->term.std_color =
2107		    (scp->term.std_color & 0xF000) |
2108		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2109		scp->term.cur_attr = mask2attr(&scp->term);
2110		break;
2111	    case 3:     /* set ansi attribute directly */
2112		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
2113		scp->term.cur_color = scp->term.std_color =
2114		    (scp->term.param[1]&0xFF)<<8;
2115		scp->term.cur_attr = mask2attr(&scp->term);
2116		break;
2117	    case 5:     /* set ansi reverse video background */
2118		scp->term.rev_color =
2119		    (scp->term.rev_color & 0x0F00) |
2120		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2121		scp->term.cur_attr = mask2attr(&scp->term);
2122		break;
2123	    case 6:     /* set ansi reverse video foreground */
2124		scp->term.rev_color =
2125		    (scp->term.rev_color & 0xF000) |
2126		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2127		scp->term.cur_attr = mask2attr(&scp->term);
2128		break;
2129	    case 7:     /* set ansi reverse video directly */
2130		scp->term.rev_color =
2131		    (scp->term.param[1]&0xFF)<<8;
2132		scp->term.cur_attr = mask2attr(&scp->term);
2133		break;
2134	    }
2135	    break;
2136
2137	case 'z':   /* switch to (virtual) console n */
2138	    if (scp->term.num_param == 1)
2139		switch_scr(scp, scp->term.param[0]);
2140	    break;
2141	}
2142    }
2143    else if (scp->term.esc == 3) {
2144	if (c >= '0' && c <= '9') {
2145	    if (scp->term.num_param < MAX_ESC_PAR) {
2146	    if (scp->term.last_param != scp->term.num_param) {
2147		scp->term.last_param = scp->term.num_param;
2148		scp->term.param[scp->term.num_param] = 0;
2149	    }
2150	    else
2151		scp->term.param[scp->term.num_param] *= 10;
2152	    scp->term.param[scp->term.num_param] += c - '0';
2153	    return;
2154	    }
2155	}
2156	scp->term.num_param = scp->term.last_param + 1;
2157	switch (c) {
2158
2159	case ';':
2160	    if (scp->term.num_param < MAX_ESC_PAR)
2161		return;
2162	    break;
2163
2164	case 'A':   /* set display border color */
2165	    if (scp->term.num_param == 1)
2166		scp->border=scp->term.param[0] & 0xff;
2167		if (scp == cur_console)
2168		    set_border(scp->border);
2169	    break;
2170
2171	case 'B':   /* set bell pitch and duration */
2172	    if (scp->term.num_param == 2) {
2173		scp->bell_pitch = scp->term.param[0];
2174		scp->bell_duration = scp->term.param[1]*10;
2175	    }
2176	    break;
2177
2178	case 'C':   /* set cursor type & shape */
2179	    if (scp->term.num_param == 1) {
2180		if (scp->term.param[0] & 0x01)
2181		    flags |= BLINK_CURSOR;
2182		else
2183		    flags &= ~BLINK_CURSOR;
2184		if (scp->term.param[0] & 0x02) {
2185		    flags |= CHAR_CURSOR;
2186		    set_destructive_cursor(scp);
2187		} else
2188		    flags &= ~CHAR_CURSOR;
2189	    }
2190	    else if (scp->term.num_param == 2) {
2191		scp->cursor_start = scp->term.param[0] & 0x1F;
2192		scp->cursor_end = scp->term.param[1] & 0x1F;
2193		if (flags & CHAR_CURSOR)
2194			set_destructive_cursor(scp);
2195	    }
2196	    break;
2197
2198	case 'F':   /* set ansi foreground */
2199	    if (scp->term.num_param == 1) {
2200		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2201		scp->term.cur_color = scp->term.std_color =
2202		    (scp->term.std_color & 0xF000)
2203		    | ((scp->term.param[0] & 0x0F) << 8);
2204		scp->term.cur_attr = mask2attr(&scp->term);
2205	    }
2206	    break;
2207
2208	case 'G':   /* set ansi background */
2209	    if (scp->term.num_param == 1) {
2210		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2211		scp->term.cur_color = scp->term.std_color =
2212		    (scp->term.std_color & 0x0F00)
2213		    | ((scp->term.param[0] & 0x0F) << 12);
2214		scp->term.cur_attr = mask2attr(&scp->term);
2215	    }
2216	    break;
2217
2218	case 'H':   /* set ansi reverse video foreground */
2219	    if (scp->term.num_param == 1) {
2220		scp->term.rev_color =
2221		    (scp->term.rev_color & 0xF000)
2222		    | ((scp->term.param[0] & 0x0F) << 8);
2223		scp->term.cur_attr = mask2attr(&scp->term);
2224	    }
2225	    break;
2226
2227	case 'I':   /* set ansi reverse video background */
2228	    if (scp->term.num_param == 1) {
2229		scp->term.rev_color =
2230		    (scp->term.rev_color & 0x0F00)
2231		    | ((scp->term.param[0] & 0x0F) << 12);
2232		scp->term.cur_attr = mask2attr(&scp->term);
2233	    }
2234	    break;
2235	}
2236    }
2237    scp->term.esc = 0;
2238}
2239
2240static void
2241ansi_put(scr_stat *scp, u_char *buf, int len)
2242{
2243    u_char *ptr = buf;
2244
2245    /* make screensaver happy */
2246    if (scp == cur_console) {
2247	scrn_time_stamp = time.tv_sec;
2248	if (scrn_blanked) {
2249	    (*current_saver)(FALSE);
2250	    mark_all(scp);
2251	}
2252    }
2253    write_in_progress++;
2254outloop:
2255    if (scp->term.esc) {
2256	scan_esc(scp, *ptr++);
2257	len--;
2258    }
2259    else if (PRINTABLE(*ptr)) {     /* Print only printables */
2260 	int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
2261 	u_short cur_attr = scp->term.cur_attr;
2262 	u_short *cursor_pos = scp->cursor_pos;
2263	do {
2264	    /*
2265	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
2266	     * pointers in the following to volatile should have no effect,
2267	     * but in fact speeds up this inner loop from 26 to 18 cycles
2268	     * (+ cache misses) on i486's.
2269	     */
2270#define	UCVP(ucp)	((u_char volatile *)(ucp))
2271	    *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
2272	    ptr++;
2273	    cnt--;
2274	} while (cnt && PRINTABLE(*ptr));
2275	len -= (cursor_pos - scp->cursor_pos);
2276	scp->xpos += (cursor_pos - scp->cursor_pos);
2277	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2278	mark_for_update(scp, cursor_pos - scp->scr_buf);
2279	scp->cursor_pos = cursor_pos;
2280	if (scp->xpos >= scp->xsize) {
2281	    scp->xpos = 0;
2282	    scp->ypos++;
2283	}
2284    }
2285    else  {
2286	switch(*ptr) {
2287	case 0x07:
2288	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
2289	    break;
2290
2291	case 0x08:      /* non-destructive backspace */
2292	    if (scp->cursor_pos > scp->scr_buf) {
2293	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2294		scp->cursor_pos--;
2295	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2296		if (scp->xpos > 0)
2297		    scp->xpos--;
2298		else {
2299		    scp->xpos += scp->xsize - 1;
2300		    scp->ypos--;
2301		}
2302	    }
2303	    break;
2304
2305	case 0x09:  /* non-destructive tab */
2306	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2307	    scp->cursor_pos += (8 - scp->xpos % 8u);
2308	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2309	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
2310	        scp->xpos = 0;
2311	        scp->ypos++;
2312	    }
2313	    break;
2314
2315	case 0x0a:  /* newline, same pos */
2316	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2317	    scp->cursor_pos += scp->xsize;
2318	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2319	    scp->ypos++;
2320	    break;
2321
2322	case 0x0c:  /* form feed, clears screen */
2323	    clear_screen(scp);
2324	    break;
2325
2326	case 0x0d:  /* return, return to pos 0 */
2327	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2328	    scp->cursor_pos -= scp->xpos;
2329	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2330	    scp->xpos = 0;
2331	    break;
2332
2333	case 0x1b:  /* start escape sequence */
2334	    scp->term.esc = 1;
2335	    scp->term.num_param = 0;
2336	    break;
2337	}
2338	ptr++; len--;
2339    }
2340    /* do we have to scroll ?? */
2341    if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
2342	remove_cutmarking(scp);
2343	if (scp->history) {
2344	    bcopyw(scp->scr_buf, scp->history_head,
2345		   scp->xsize * sizeof(u_short));
2346	    scp->history_head += scp->xsize;
2347	    if (scp->history_head + scp->xsize >
2348		scp->history + scp->history_size)
2349		scp->history_head = scp->history;
2350	}
2351	bcopyw(scp->scr_buf + scp->xsize, scp->scr_buf,
2352	       scp->xsize * (scp->ysize - 1) * sizeof(u_short));
2353	fillw(scp->term.cur_color | scr_map[0x20],
2354	      scp->scr_buf + scp->xsize * (scp->ysize - 1),
2355	      scp->xsize);
2356	scp->cursor_pos -= scp->xsize;
2357	scp->ypos--;
2358    	mark_all(scp);
2359    }
2360    if (len)
2361	goto outloop;
2362    write_in_progress--;
2363    if (delayed_next_scr)
2364	switch_scr(scp, delayed_next_scr - 1);
2365}
2366
2367static void
2368scinit(void)
2369{
2370    u_short volatile *cp;
2371    u_short was;
2372    u_int hw_cursor;
2373    u_int i;
2374
2375    if (init_done != COLD)
2376	return;
2377    init_done = WARM;
2378    /*
2379     * Finish defaulting crtc variables for a mono screen.  Crtat is a
2380     * bogus common variable so that it can be shared with pcvt, so it
2381     * can't be statically initialized.  XXX.
2382     */
2383    Crtat = (u_short *)MONO_BUF;
2384    /*
2385     * If CGA memory seems to work, switch to color.
2386     */
2387    cp = (u_short *)CGA_BUF;
2388    was = *cp;
2389    *cp = (u_short) 0xA55A;
2390    if (*cp == 0xA55A) {
2391	Crtat = (u_short *)CGA_BUF;
2392	crtc_addr = COLOR_BASE;
2393    }
2394    *cp = was;
2395
2396    /*
2397     * Ensure a zero start address.  This is mainly to recover after
2398     * switching from pcvt using userconfig().  The registers are w/o
2399     * for old hardware so it's too hard to relocate the active screen
2400     * memory.
2401     */
2402    outb(crtc_addr, 12);
2403    outb(crtc_addr + 1, 0);
2404    outb(crtc_addr, 13);
2405    outb(crtc_addr + 1, 0);
2406
2407    /* extract cursor location */
2408    outb(crtc_addr, 14);
2409    hw_cursor = inb(crtc_addr + 1) << 8;
2410    outb(crtc_addr, 15);
2411    hw_cursor |= inb(crtc_addr + 1);
2412
2413    /*
2414     * Validate cursor location.  It may be off the screen.  Then we must
2415     * not use it for the initial buffer offset.
2416     */
2417    if (hw_cursor >= ROW * COL)
2418	hw_cursor = (ROW - 1) * COL;
2419
2420    /* move hardware cursor out of the way */
2421    outb(crtc_addr, 14);
2422    outb(crtc_addr + 1, 0xff);
2423    outb(crtc_addr, 15);
2424    outb(crtc_addr + 1, 0xff);
2425
2426    /* is this a VGA or higher ? */
2427    outb(crtc_addr, 7);
2428    if (inb(crtc_addr) == 7) {
2429	u_long  pa;
2430	u_long  segoff;
2431
2432	crtc_vga = TRUE;
2433
2434	/* Get the BIOS video mode pointer */
2435	segoff = *(u_long *)pa_to_va(0x4a8);
2436	pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2437	if (ISMAPPED(pa, sizeof(u_long))) {
2438	    segoff = *(u_long *)pa_to_va(pa);
2439	    pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2440	    if (ISMAPPED(pa, 64))
2441		video_mode_ptr = (char *)pa_to_va(pa);
2442	}
2443    }
2444    current_default = &user_default;
2445    console[0] = &main_console;
2446    init_scp(console[0]);
2447    cur_console = console[0];
2448
2449    /* copy screen to temporary buffer */
2450    bcopyw(Crtat, sc_buffer,
2451	   console[0]->xsize * console[0]->ysize * sizeof(u_short));
2452
2453    console[0]->scr_buf = console[0]->mouse_pos = sc_buffer;
2454    console[0]->cursor_pos = console[0]->cursor_oldpos = sc_buffer + hw_cursor;
2455    console[0]->xpos = hw_cursor % COL;
2456    console[0]->ypos = hw_cursor / COL;
2457    for (i=1; i<MAXCONS; i++)
2458	console[i] = NULL;
2459    kernel_console.esc = 0;
2460    kernel_console.attr_mask = NORMAL_ATTR;
2461    kernel_console.cur_attr =
2462	kernel_console.cur_color = kernel_console.std_color =
2463	kernel_default.std_color;
2464    kernel_console.rev_color = kernel_default.rev_color;
2465
2466    /* initialize mapscrn arrays to a one to one map */
2467    for (i=0; i<sizeof(scr_map); i++) {
2468	scr_map[i] = scr_rmap[i] = i;
2469    }
2470
2471    /* Save font and palette if VGA */
2472    if (crtc_vga) {
2473	copy_font(SAVE, FONT_16, font_16);
2474	fonts_loaded = FONT_16;
2475	save_palette();
2476    }
2477
2478#ifdef SC_SPLASH_SCREEN
2479    /*
2480     * Now put up a graphics image, and maybe cycle a
2481     * couble of palette entries for simple animation.
2482     */
2483    toggle_splash_screen(cur_console);
2484#endif
2485}
2486
2487static scr_stat
2488*alloc_scp()
2489{
2490    scr_stat *scp;
2491
2492    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2493    init_scp(scp);
2494    scp->scr_buf = scp->cursor_pos = scp->cursor_oldpos =
2495	(u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
2496			  M_DEVBUF, M_WAITOK);
2497    scp->mouse_pos = scp->mouse_oldpos =
2498	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
2499			scp->mouse_xpos/8);
2500    scp->history_head = scp->history_pos = scp->history =
2501	(u_short *)malloc(scp->history_size*sizeof(u_short),
2502			  M_DEVBUF, M_WAITOK);
2503    bzero(scp->history_head, scp->history_size*sizeof(u_short));
2504/* SOS
2505    if (crtc_vga && video_mode_ptr)
2506	set_mode(scp);
2507*/
2508    clear_screen(scp);
2509    return scp;
2510}
2511
2512static void
2513init_scp(scr_stat *scp)
2514{
2515    if (crtc_vga)
2516	if (crtc_addr == MONO_BASE)
2517	    scp->mode = M_VGA_M80x25;
2518	else
2519	    scp->mode = M_VGA_C80x25;
2520    else
2521	if (crtc_addr == MONO_BASE)
2522	    scp->mode = M_B80x25;
2523	else
2524	    scp->mode = M_C80x25;
2525
2526    scp->font_size = FONT_16;
2527    scp->xsize = COL;
2528    scp->ysize = ROW;
2529    scp->xpos = scp->ypos = 0;
2530    scp->saved_xpos = scp->saved_ypos = -1;
2531    scp->start = scp->xsize * scp->ysize;
2532    scp->end = 0;
2533    scp->term.esc = 0;
2534    scp->term.attr_mask = NORMAL_ATTR;
2535    scp->term.cur_attr =
2536	scp->term.cur_color = scp->term.std_color =
2537	current_default->std_color;
2538    scp->term.rev_color = current_default->rev_color;
2539    scp->border = BG_BLACK;
2540    scp->cursor_start = *(char *)pa_to_va(0x461);
2541    scp->cursor_end = *(char *)pa_to_va(0x460);
2542    scp->mouse_xpos = scp->xsize*8/2;
2543    scp->mouse_ypos = scp->ysize*scp->font_size/2;
2544    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
2545    scp->mouse_signal = 0;
2546    scp->mouse_pid = 0;
2547    scp->mouse_proc = NULL;
2548    scp->bell_pitch = BELL_PITCH;
2549    scp->bell_duration = BELL_DURATION;
2550    scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
2551    scp->status |= CURSOR_ENABLED;
2552    scp->pid = 0;
2553    scp->proc = NULL;
2554    scp->smode.mode = VT_AUTO;
2555    scp->history_head = scp->history_pos = scp->history = NULL;
2556    scp->history_size = HISTORY_SIZE;
2557}
2558
2559static u_char
2560*get_fstr(u_int c, u_int *len)
2561{
2562    u_int i;
2563
2564    if (!(c & FKEY))
2565	return(NULL);
2566    i = (c & 0xFF) - F_FN;
2567    if (i > n_fkey_tab)
2568	return(NULL);
2569    *len = fkey_tab[i].len;
2570    return(fkey_tab[i].str);
2571}
2572
2573static void
2574history_to_screen(scr_stat *scp)
2575{
2576    int i;
2577
2578    for (i=0; i<scp->ysize; i++)
2579	bcopyw(scp->history + (((scp->history_pos - scp->history) +
2580	       scp->history_size-((i+1)*scp->xsize))%scp->history_size),
2581	       scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
2582	       scp->xsize * sizeof(u_short));
2583    mark_all(scp);
2584}
2585
2586static int
2587history_up_line(scr_stat *scp)
2588{
2589    if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
2590	scp->history_head) {
2591	scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
2592	history_to_screen(scp);
2593	return 0;
2594    }
2595    else
2596	return -1;
2597}
2598
2599static int
2600history_down_line(scr_stat *scp)
2601{
2602    if (scp->history_pos != scp->history_head) {
2603	scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
2604	history_to_screen(scp);
2605	return 0;
2606    }
2607    else
2608	return -1;
2609}
2610
2611/*
2612 * scgetc(flags) - get character from keyboard.
2613 * If flags & SCGETC_CN, then avoid harmful side effects.
2614 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
2615 * return NOKEY if there is nothing there.
2616 */
2617static u_int
2618scgetc(u_int flags)
2619{
2620    struct key_t *key;
2621    u_char scancode, keycode;
2622    u_int state, action;
2623    int c;
2624    static u_char esc_flag = 0, compose = 0;
2625    static u_int chr = 0;
2626
2627next_code:
2628    /* first see if there is something in the keyboard port */
2629    if (flags & SCGETC_NONBLOCK) {
2630	c = read_kbd_data_no_wait(sc_kbdc);
2631	if (c == -1)
2632	    return(NOKEY);
2633    } else {
2634	do {
2635	    c = read_kbd_data(sc_kbdc);
2636	} while(c == -1);
2637    }
2638    scancode = (u_char)c;
2639
2640    /* do the /dev/random device a favour */
2641    if (!(flags & SCGETC_CN))
2642	add_keyboard_randomness(scancode);
2643
2644    if (cur_console->status & KBD_RAW_MODE)
2645	return scancode;
2646
2647    keycode = scancode & 0x7F;
2648    switch (esc_flag) {
2649    case 0x00:      /* normal scancode */
2650	switch(scancode) {
2651	case 0xB8:  /* left alt (compose key) */
2652	    if (compose) {
2653		compose = 0;
2654		if (chr > 255) {
2655		    do_bell(cur_console,
2656			BELL_PITCH, BELL_DURATION);
2657		    chr = 0;
2658		}
2659	    }
2660	    break;
2661	case 0x38:
2662	    if (!compose) {
2663		compose = 1;
2664		chr = 0;
2665	    }
2666	    break;
2667	case 0xE0:
2668	case 0xE1:
2669	    esc_flag = scancode;
2670	    goto next_code;
2671	}
2672	break;
2673    case 0xE0:      /* 0xE0 prefix */
2674	esc_flag = 0;
2675	switch (keycode) {
2676	case 0x1C:  /* right enter key */
2677	    keycode = 0x59;
2678	    break;
2679	case 0x1D:  /* right ctrl key */
2680	    keycode = 0x5A;
2681	    break;
2682	case 0x35:  /* keypad divide key */
2683	    keycode = 0x5B;
2684	    break;
2685	case 0x37:  /* print scrn key */
2686	    keycode = 0x5C;
2687	    break;
2688	case 0x38:  /* right alt key (alt gr) */
2689	    keycode = 0x5D;
2690	    break;
2691	case 0x47:  /* grey home key */
2692	    keycode = 0x5E;
2693	    break;
2694	case 0x48:  /* grey up arrow key */
2695	    keycode = 0x5F;
2696	    break;
2697	case 0x49:  /* grey page up key */
2698	    keycode = 0x60;
2699	    break;
2700	case 0x4B:  /* grey left arrow key */
2701	    keycode = 0x61;
2702	    break;
2703	case 0x4D:  /* grey right arrow key */
2704	    keycode = 0x62;
2705	    break;
2706	case 0x4F:  /* grey end key */
2707	    keycode = 0x63;
2708	    break;
2709	case 0x50:  /* grey down arrow key */
2710	    keycode = 0x64;
2711	    break;
2712	case 0x51:  /* grey page down key */
2713	    keycode = 0x65;
2714	    break;
2715	case 0x52:  /* grey insert key */
2716	    keycode = 0x66;
2717	    break;
2718	case 0x53:  /* grey delete key */
2719	    keycode = 0x67;
2720	    break;
2721
2722	/* the following 3 are only used on the MS "Natural" keyboard */
2723	case 0x5b:  /* left Window key */
2724	    keycode = 0x69;
2725	    break;
2726	case 0x5c:  /* right Window key */
2727	    keycode = 0x6a;
2728	    break;
2729	case 0x5d:  /* menu key */
2730	    keycode = 0x6b;
2731	    break;
2732	default:    /* ignore everything else */
2733	    goto next_code;
2734	}
2735	break;
2736    case 0xE1:      /* 0xE1 prefix */
2737	esc_flag = 0;
2738	if (keycode == 0x1D)
2739	    esc_flag = 0x1D;
2740	goto next_code;
2741	/* NOT REACHED */
2742    case 0x1D:      /* pause / break */
2743	esc_flag = 0;
2744	if (keycode != 0x45)
2745	    goto next_code;
2746	keycode = 0x68;
2747	break;
2748    }
2749
2750    /* if scroll-lock pressed allow history browsing */
2751    if (cur_console->history && cur_console->status & SLKED) {
2752	int i;
2753
2754	cur_console->status &= ~CURSOR_ENABLED;
2755	if (!(cur_console->status & BUFFER_SAVED)) {
2756	    cur_console->status |= BUFFER_SAVED;
2757	    cur_console->history_save = cur_console->history_head;
2758
2759	    /* copy screen into top of history buffer */
2760	    for (i=0; i<cur_console->ysize; i++) {
2761		bcopyw(cur_console->scr_buf + (cur_console->xsize * i),
2762		       cur_console->history_head,
2763		       cur_console->xsize * sizeof(u_short));
2764		cur_console->history_head += cur_console->xsize;
2765		if (cur_console->history_head + cur_console->xsize >
2766		    cur_console->history + cur_console->history_size)
2767		    cur_console->history_head=cur_console->history;
2768	    }
2769	    cur_console->history_pos = cur_console->history_head;
2770	    history_to_screen(cur_console);
2771	}
2772	switch (scancode) {
2773	case 0x47:  /* home key */
2774	    cur_console->history_pos = cur_console->history_head;
2775	    history_to_screen(cur_console);
2776	    goto next_code;
2777
2778	case 0x4F:  /* end key */
2779	    cur_console->history_pos =
2780		WRAPHIST(cur_console, cur_console->history_head,
2781			 cur_console->xsize*cur_console->ysize);
2782	    history_to_screen(cur_console);
2783	    goto next_code;
2784
2785	case 0x48:  /* up arrow key */
2786	    if (history_up_line(cur_console))
2787		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2788	    goto next_code;
2789
2790	case 0x50:  /* down arrow key */
2791	    if (history_down_line(cur_console))
2792		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2793	    goto next_code;
2794
2795	case 0x49:  /* page up key */
2796	    for (i=0; i<cur_console->ysize; i++)
2797	    if (history_up_line(cur_console)) {
2798		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2799		break;
2800	    }
2801	    goto next_code;
2802
2803	case 0x51:  /* page down key */
2804	    for (i=0; i<cur_console->ysize; i++)
2805	    if (history_down_line(cur_console)) {
2806		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2807		break;
2808	    }
2809	    goto next_code;
2810	}
2811    }
2812
2813    if (compose) {
2814	switch (scancode) {
2815	/* key pressed process it */
2816	case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
2817	    chr = (scancode - 0x40) + chr*10;
2818	    goto next_code;
2819	case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
2820	    chr = (scancode - 0x47) + chr*10;
2821	    goto next_code;
2822	case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
2823	    chr = (scancode - 0x4E) + chr*10;
2824	    goto next_code;
2825	case 0x52:              /* keypad 0 */
2826	    chr *= 10;
2827	    goto next_code;
2828
2829	/* key release, no interest here */
2830	case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
2831	case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
2832	case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
2833	case 0xD2:              /* keypad 0 */
2834	    goto next_code;
2835
2836	case 0x38:              /* left alt key */
2837	    break;
2838	default:
2839	    if (chr) {
2840		compose = chr = 0;
2841		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2842		goto next_code;
2843	    }
2844	    break;
2845	}
2846    }
2847
2848    state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
2849    if ((!agrs && (cur_console->status & ALKED))
2850	|| (agrs && !(cur_console->status & ALKED)))
2851	keycode += ALTGR_OFFSET;
2852    key = &key_map.key[keycode];
2853    if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
2854	 || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
2855	state ^= 1;
2856
2857    /* Check for make/break */
2858    action = key->map[state];
2859    if (scancode & 0x80) {      /* key released */
2860	if (key->spcl & 0x80) {
2861	    switch (action) {
2862	    case LSH:
2863		shfts &= ~1;
2864		break;
2865	    case RSH:
2866		shfts &= ~2;
2867		break;
2868	    case LCTR:
2869		ctls &= ~1;
2870		break;
2871	    case RCTR:
2872		ctls &= ~2;
2873		break;
2874	    case LALT:
2875		alts &= ~1;
2876		break;
2877	    case RALT:
2878		alts &= ~2;
2879		break;
2880	    case NLK:
2881		nlkcnt = 0;
2882		break;
2883	    case CLK:
2884		clkcnt = 0;
2885		break;
2886	    case SLK:
2887		slkcnt = 0;
2888		break;
2889	    case ASH:
2890		agrs = 0;
2891		break;
2892	    case ALK:
2893		alkcnt = 0;
2894		break;
2895	    case META:
2896		metas = 0;
2897		break;
2898	    }
2899	}
2900	if (chr && !compose) {
2901	    action = chr;
2902	    chr = 0;
2903	    return(action);
2904	}
2905    } else {
2906	/* key pressed */
2907	if (key->spcl & (0x80>>state)) {
2908	    switch (action) {
2909	    /* LOCKING KEYS */
2910	    case NLK:
2911#ifdef SC_SPLASH_SCREEN
2912		toggle_splash_screen(cur_console); /* SOS XXX */
2913#endif
2914		if (!nlkcnt) {
2915		    nlkcnt++;
2916		    if (cur_console->status & NLKED)
2917			cur_console->status &= ~NLKED;
2918		    else
2919			cur_console->status |= NLKED;
2920		    update_leds(cur_console->status);
2921		}
2922		break;
2923	    case CLK:
2924		if (!clkcnt) {
2925		    clkcnt++;
2926		    if (cur_console->status & CLKED)
2927			cur_console->status &= ~CLKED;
2928		    else
2929			cur_console->status |= CLKED;
2930		    update_leds(cur_console->status);
2931		}
2932		break;
2933	    case SLK:
2934		if (!slkcnt) {
2935		    slkcnt++;
2936		    if (cur_console->status & SLKED) {
2937			cur_console->status &= ~SLKED;
2938			if (cur_console->status & BUFFER_SAVED){
2939			    int i;
2940			    u_short *ptr = cur_console->history_save;
2941
2942			    for (i=0; i<cur_console->ysize; i++) {
2943				bcopyw(ptr,
2944				       cur_console->scr_buf +
2945				       (cur_console->xsize*i),
2946				       cur_console->xsize * sizeof(u_short));
2947				ptr += cur_console->xsize;
2948				if (ptr + cur_console->xsize >
2949				    cur_console->history +
2950				    cur_console->history_size)
2951				    ptr = cur_console->history;
2952			    }
2953			    cur_console->status &= ~BUFFER_SAVED;
2954			    cur_console->history_head=cur_console->history_save;
2955			    cur_console->status |= CURSOR_ENABLED;
2956			    mark_all(cur_console);
2957			}
2958			scstart(VIRTUAL_TTY(get_scr_num()));
2959		    }
2960		    else
2961			cur_console->status |= SLKED;
2962		    update_leds(cur_console->status);
2963		}
2964		break;
2965	    case ALK:
2966		if (!alkcnt) {
2967		    alkcnt++;
2968		    if (cur_console->status & ALKED)
2969			cur_console->status &= ~ALKED;
2970		    else
2971			cur_console->status |= ALKED;
2972		    update_leds(cur_console->status);
2973		}
2974		break;
2975
2976	    /* NON-LOCKING KEYS */
2977	    case NOP:
2978		break;
2979	    case SPSC:
2980#ifdef SC_SPLASH_SCREEN
2981		toggle_splash_screen(cur_console);
2982#endif
2983		break;
2984	    case RBT:
2985		shutdown_nice();
2986		break;
2987	    case SUSP:
2988#if NAPM > 0
2989		apm_suspend();
2990#endif
2991		break;
2992
2993	    case DBG:
2994#ifdef DDB          /* try to switch to console 0 */
2995		if (cur_console->smode.mode == VT_AUTO &&
2996		    console[0]->smode.mode == VT_AUTO)
2997		    switch_scr(cur_console, 0);
2998		Debugger("manual escape to debugger");
2999#else
3000		printf("No debugger in kernel\n");
3001#endif
3002		break;
3003	    case LSH:
3004		shfts |= 1;
3005		break;
3006	    case RSH:
3007		shfts |= 2;
3008		break;
3009	    case LCTR:
3010		ctls |= 1;
3011		break;
3012	    case RCTR:
3013		ctls |= 2;
3014		break;
3015	    case LALT:
3016		alts |= 1;
3017		break;
3018	    case RALT:
3019		alts |= 2;
3020		break;
3021	    case ASH:
3022		agrs = 1;
3023		break;
3024	    case META:
3025		metas = 1;
3026		break;
3027	    case NEXT:
3028		{
3029		int next, this = get_scr_num();
3030		for (next = this+1; next != this; next = (next+1)%MAXCONS) {
3031		    struct tty *tp = VIRTUAL_TTY(next);
3032		    if (tp->t_state & TS_ISOPEN) {
3033			switch_scr(cur_console, next);
3034			break;
3035		    }
3036		}
3037		}
3038		break;
3039	    case BTAB:
3040		return(BKEY);
3041	    default:
3042		if (action >= F_SCR && action <= L_SCR) {
3043		    switch_scr(cur_console, action - F_SCR);
3044		    break;
3045		}
3046		if (action >= F_FN && action <= L_FN)
3047		    action |= FKEY;
3048		return(action);
3049	    }
3050	}
3051	else {
3052	    if (metas)
3053		action |= MKEY;
3054	    return(action);
3055	}
3056    }
3057    goto next_code;
3058}
3059
3060int
3061scmmap(dev_t dev, int offset, int nprot)
3062{
3063    if (offset > 0x20000 - PAGE_SIZE)
3064	return -1;
3065    return i386_btop((VIDEOMEM + offset));
3066}
3067
3068/*
3069 * Calculate hardware attributes word using logical attributes mask and
3070 * hardware colors
3071 */
3072
3073static int
3074mask2attr(struct term_stat *term)
3075{
3076    int attr, mask = term->attr_mask;
3077
3078    if (mask & REVERSE_ATTR) {
3079	attr = ((mask & FOREGROUND_CHANGED) ?
3080		((term->cur_color & 0xF000) >> 4) :
3081		(term->rev_color & 0x0F00)) |
3082	       ((mask & BACKGROUND_CHANGED) ?
3083		((term->cur_color & 0x0F00) << 4) :
3084		(term->rev_color & 0xF000));
3085    } else
3086	attr = term->cur_color;
3087
3088    /* XXX: underline mapping for Hercules adapter can be better */
3089    if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
3090	attr ^= 0x0800;
3091    if (mask & BLINK_ATTR)
3092	attr ^= 0x8000;
3093
3094    return attr;
3095}
3096
3097static void
3098set_keyboard(int command, int data)
3099{
3100    int s;
3101    int c;
3102
3103    if (sc_kbdc == NULL)
3104	return;
3105
3106    /* prevent the timeout routine from polling the keyboard */
3107    if (!kbdc_lock(sc_kbdc, TRUE))
3108	return;
3109
3110    /* disable the keyboard and mouse interrupt */
3111    s = spltty();
3112#if 0
3113    c = get_controller_command_byte(sc_kbdc);
3114    if ((c == -1)
3115	|| !set_controller_command_byte(sc_kbdc,
3116            kbdc_get_device_mask(sc_kbdc),
3117            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
3118                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
3119	/* CONTROLLER ERROR */
3120        kbdc_lock(sc_kbdc, FALSE);
3121	splx(s);
3122	return;
3123    }
3124    /*
3125     * Now that the keyboard controller is told not to generate
3126     * the keyboard and mouse interrupts, call `splx()' to allow
3127     * the other tty interrupts. The clock interrupt may also occur,
3128     * but the timeout routine (`scrn_timer()') will be blocked
3129     * by the lock flag set via `kbdc_lock()'
3130     */
3131    splx(s);
3132#endif
3133
3134    if (send_kbd_command_and_data(sc_kbdc, command, data) != KBD_ACK)
3135        send_kbd_command(sc_kbdc, KBDC_ENABLE_KBD);
3136
3137#if 0
3138    /* restore the interrupts */
3139    if (!set_controller_command_byte(sc_kbdc,
3140            kbdc_get_device_mask(sc_kbdc),
3141	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
3142	/* CONTROLLER ERROR */
3143    }
3144#else
3145    splx(s);
3146#endif
3147    kbdc_lock(sc_kbdc, FALSE);
3148}
3149
3150static void
3151update_leds(int which)
3152{
3153    static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
3154
3155    /* replace CAPS led with ALTGR led for ALTGR keyboards */
3156    if (key_map.n_keys > ALTGR_OFFSET) {
3157	if (which & ALKED)
3158	    which |= CLKED;
3159	else
3160	    which &= ~CLKED;
3161    }
3162
3163    set_keyboard(KBDC_SET_LEDS, xlate_leds[which & LED_MASK]);
3164}
3165
3166void
3167set_mode(scr_stat *scp)
3168{
3169    char *modetable;
3170    char special_modetable[64];
3171
3172    if (scp != cur_console)
3173	return;
3174
3175    /* setup video hardware for the given mode */
3176    switch (scp->mode) {
3177    case M_VGA_M80x60:
3178	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3179	goto special_80x60;
3180
3181    case M_VGA_C80x60:
3182	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3183special_80x60:
3184	special_modetable[2]  = 0x08;
3185	special_modetable[19] = 0x47;
3186	goto special_480l;
3187
3188    case M_VGA_M80x30:
3189	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3190	goto special_80x30;
3191
3192    case M_VGA_C80x30:
3193	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3194special_80x30:
3195	special_modetable[19] = 0x4f;
3196special_480l:
3197	special_modetable[9] |= 0xc0;
3198	special_modetable[16] = 0x08;
3199	special_modetable[17] = 0x3e;
3200	special_modetable[26] = 0xea;
3201	special_modetable[28] = 0xdf;
3202	special_modetable[31] = 0xe7;
3203	special_modetable[32] = 0x04;
3204	modetable = special_modetable;
3205	goto setup_mode;
3206
3207    case M_ENH_B80x43:
3208	bcopyw(video_mode_ptr+(64*M_ENH_B80x25), &special_modetable, 64);
3209	goto special_80x43;
3210
3211    case M_ENH_C80x43:
3212	bcopyw(video_mode_ptr+(64*M_ENH_C80x25), &special_modetable, 64);
3213special_80x43:
3214	special_modetable[28] = 87;
3215	goto special_80x50;
3216
3217    case M_VGA_M80x50:
3218	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3219	goto special_80x50;
3220
3221    case M_VGA_C80x50:
3222	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3223special_80x50:
3224	special_modetable[2] = 8;
3225	special_modetable[19] = 7;
3226	modetable = special_modetable;
3227	goto setup_mode;
3228
3229    case M_VGA_C40x25: case M_VGA_C80x25:
3230    case M_VGA_M80x25:
3231    case M_B40x25:     case M_C40x25:
3232    case M_B80x25:     case M_C80x25:
3233    case M_ENH_B40x25: case M_ENH_C40x25:
3234    case M_ENH_B80x25: case M_ENH_C80x25:
3235
3236	modetable = video_mode_ptr + (scp->mode * 64);
3237setup_mode:
3238	set_vgaregs(modetable);
3239	scp->font_size = *(modetable + 2);
3240
3241	/* set font type (size) */
3242	if (scp->font_size < FONT_14) {
3243	    if (fonts_loaded & FONT_8)
3244		copy_font(LOAD, FONT_8, font_8);
3245	    outb(TSIDX, 0x03); outb(TSREG, 0x0A);   /* font 2 */
3246	} else if (scp->font_size >= FONT_16) {
3247	    if (fonts_loaded & FONT_16)
3248		copy_font(LOAD, FONT_16, font_16);
3249	    outb(TSIDX, 0x03); outb(TSREG, 0x00);   /* font 0 */
3250	} else {
3251	    if (fonts_loaded & FONT_14)
3252		copy_font(LOAD, FONT_14, font_14);
3253	    outb(TSIDX, 0x03); outb(TSREG, 0x05);   /* font 1 */
3254	}
3255	if (flags & CHAR_CURSOR)
3256	    set_destructive_cursor(scp);
3257	mark_all(scp);
3258	break;
3259
3260    case M_BG320:     case M_CG320:     case M_BG640:
3261    case M_CG320_D:   case M_CG640_E:
3262    case M_CG640x350: case M_ENH_CG640:
3263    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
3264
3265	set_vgaregs(video_mode_ptr + (scp->mode * 64));
3266	scp->font_size = FONT_NONE;
3267	break;
3268
3269    default:
3270	/* call user defined function XXX */
3271	break;
3272    }
3273
3274    /* set border color for this (virtual) console */
3275    set_border(scp->border);
3276    return;
3277}
3278
3279void
3280set_border(u_char color)
3281{
3282    inb(crtc_addr+6);               /* reset flip-flop */
3283    outb(ATC, 0x11); outb(ATC, color);
3284    inb(crtc_addr+6);               /* reset flip-flop */
3285    outb(ATC, 0x20);                /* enable Palette */
3286}
3287
3288static void
3289set_vgaregs(char *modetable)
3290{
3291    int i, s = splhigh();
3292
3293    outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
3294    outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
3295    for (i=0; i<4; i++) {           		/* program sequencer */
3296	outb(TSIDX, i+1);
3297	outb(TSREG, modetable[i+5]);
3298    }
3299    outb(MISC, modetable[9]);       		/* set dot-clock */
3300    outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
3301    outb(crtc_addr, 0x11);
3302    outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
3303    for (i=0; i<25; i++) {          		/* program crtc */
3304	outb(crtc_addr, i);
3305	if (i == 14 || i == 15)     		/* no hardware cursor */
3306	    outb(crtc_addr+1, 0xff);
3307	else
3308	    outb(crtc_addr+1, modetable[i+10]);
3309    }
3310    inb(crtc_addr+6);           		/* reset flip-flop */
3311    for (i=0; i<20; i++) {          		/* program attribute ctrl */
3312	outb(ATC, i);
3313	outb(ATC, modetable[i+35]);
3314    }
3315    for (i=0; i<9; i++) {           		/* program graph data ctrl */
3316	outb(GDCIDX, i);
3317	outb(GDCREG, modetable[i+55]);
3318    }
3319    inb(crtc_addr+6);           		/* reset flip-flop */
3320    outb(ATC, 0x20);            		/* enable palette */
3321    splx(s);
3322}
3323
3324static void
3325set_font_mode()
3326{
3327    int s = splhigh();
3328
3329    /* setup vga for loading fonts (graphics plane mode) */
3330    inb(crtc_addr+6);           		/* reset flip-flop */
3331    outb(ATC, 0x10); outb(ATC, 0x01);
3332    inb(crtc_addr+6);               		/* reset flip-flop */
3333    outb(ATC, 0x20);            		/* enable palette */
3334
3335#if SLOW_VGA
3336    outb(TSIDX, 0x02); outb(TSREG, 0x04);
3337    outb(TSIDX, 0x04); outb(TSREG, 0x06);
3338    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
3339    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
3340    outb(GDCIDX, 0x06); outb(GDCREG, 0x05);
3341#else
3342    outw(TSIDX, 0x0402);
3343    outw(TSIDX, 0x0604);
3344    outw(GDCIDX, 0x0204);
3345    outw(GDCIDX, 0x0005);
3346    outw(GDCIDX, 0x0506);               /* addr = a0000, 64kb */
3347#endif
3348    splx(s);
3349}
3350
3351static void
3352set_normal_mode()
3353{
3354    char *modetable;
3355    int s = splhigh();
3356
3357    switch (cur_console->mode) {
3358    case M_VGA_M80x60:
3359    case M_VGA_M80x50:
3360    case M_VGA_M80x30:
3361	modetable = video_mode_ptr + (64*M_VGA_M80x25);
3362	break;
3363
3364    case M_VGA_C80x60:
3365    case M_VGA_C80x50:
3366    case M_VGA_C80x30:
3367	modetable = video_mode_ptr + (64*M_VGA_C80x25);
3368	break;
3369
3370    case M_ENH_B80x43:
3371	modetable = video_mode_ptr + (64*M_ENH_B80x25);
3372	break;
3373
3374    case M_ENH_C80x43:
3375	modetable = video_mode_ptr + (64*M_ENH_C80x25);
3376	break;
3377
3378    case M_VGA_C40x25: case M_VGA_C80x25:
3379    case M_VGA_M80x25:
3380    case M_B40x25:     case M_C40x25:
3381    case M_B80x25:     case M_C80x25:
3382    case M_ENH_B40x25: case M_ENH_C40x25:
3383    case M_ENH_B80x25: case M_ENH_C80x25:
3384
3385    case M_BG320:     case M_CG320:     case M_BG640:
3386    case M_CG320_D:   case M_CG640_E:
3387    case M_CG640x350: case M_ENH_CG640:
3388    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
3389	modetable = video_mode_ptr + (cur_console->mode * 64);
3390	break;
3391
3392    default:
3393	modetable = video_mode_ptr + (64*M_VGA_C80x25);
3394    }
3395
3396    /* setup vga for normal operation mode again */
3397    inb(crtc_addr+6);           		/* reset flip-flop */
3398    outb(ATC, 0x10); outb(ATC, modetable[0x10+35]);
3399    inb(crtc_addr+6);               		/* reset flip-flop */
3400    outb(ATC, 0x20);            		/* enable palette */
3401#if SLOW_VGA
3402    outb(TSIDX, 0x02); outb(TSREG, modetable[0x02+4]);
3403    outb(TSIDX, 0x04); outb(TSREG, modetable[0x04+4]);
3404    outb(GDCIDX, 0x04); outb(GDCREG, modetable[0x04+55]);
3405    outb(GDCIDX, 0x05); outb(GDCREG, modetable[0x05+55]);
3406    outb(GDCIDX, 0x06); outb(GDCREG, modetable[0x06+55]);
3407    if (crtc_addr == MONO_BASE) {
3408	outb(GDCIDX, 0x06); outb(GDCREG,(modetable[0x06+55] & 0x03) | 0x08);
3409    }
3410    else {
3411	outb(GDCIDX, 0x06); outb(GDCREG,(modetable[0x06+55] & 0x03) | 0x0c);
3412    }
3413#else
3414    outw(TSIDX, 0x0002 | (modetable[0x02+4]<<8));
3415    outw(TSIDX, 0x0004 | (modetable[0x04+4]<<8));
3416    outw(GDCIDX, 0x0004 | (modetable[0x04+55]<<8));
3417    outw(GDCIDX, 0x0005 | (modetable[0x05+55]<<8));
3418    if (crtc_addr == MONO_BASE)
3419        outw(GDCIDX, 0x0006 | (((modetable[0x06+55] & 0x03) | 0x08)<<8));
3420    else
3421        outw(GDCIDX, 0x0006 | (((modetable[0x06+55] & 0x03) | 0x0c)<<8));
3422#endif
3423    splx(s);
3424}
3425
3426void
3427copy_font(int operation, int font_type, char* font_image)
3428{
3429    int ch, line, segment, fontsize;
3430    u_char val;
3431
3432    /* dont mess with console we dont know video mode on */
3433    if (cur_console->status & UNKNOWN_MODE)
3434	return;
3435
3436    switch (font_type) {
3437    default:
3438    case FONT_8:
3439	segment = 0x8000;
3440	fontsize = 8;
3441	break;
3442    case FONT_14:
3443	segment = 0x4000;
3444	fontsize = 14;
3445	break;
3446    case FONT_16:
3447	segment = 0x0000;
3448	fontsize = 16;
3449	break;
3450    }
3451    outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
3452    outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
3453    set_font_mode();
3454    for (ch=0; ch < 256; ch++)
3455	for (line=0; line < fontsize; line++)
3456	if (operation)
3457	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
3458		    font_image[(ch*fontsize)+line];
3459	else
3460	    font_image[(ch*fontsize)+line] =
3461	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
3462    set_normal_mode();
3463    outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
3464}
3465
3466static void
3467set_destructive_cursor(scr_stat *scp)
3468{
3469    u_char cursor[32];
3470    caddr_t address;
3471    int i;
3472    char *font_buffer;
3473
3474
3475    if (scp->font_size < FONT_14) {
3476	font_buffer = font_8;
3477	address = (caddr_t)VIDEOMEM + 0x8000;
3478    }
3479    else if (scp->font_size >= FONT_16) {
3480	font_buffer = font_16;
3481	address = (caddr_t)VIDEOMEM;
3482    }
3483    else {
3484	font_buffer = font_14;
3485	address = (caddr_t)VIDEOMEM + 0x4000;
3486    }
3487
3488    if (scp->status & MOUSE_VISIBLE) {
3489	if ((scp->cursor_saveunder & 0xff) == 0xd0)
3490    	    bcopyw(&scp->mouse_cursor[0], cursor, scp->font_size);
3491	else if ((scp->cursor_saveunder & 0xff) == 0xd1)
3492    	    bcopyw(&scp->mouse_cursor[32], cursor, scp->font_size);
3493	else if ((scp->cursor_saveunder & 0xff) == 0xd2)
3494    	    bcopyw(&scp->mouse_cursor[64], cursor, scp->font_size);
3495	else if ((scp->cursor_saveunder & 0xff) == 0xd3)
3496    	    bcopyw(&scp->mouse_cursor[96], cursor, scp->font_size);
3497	else
3498	    bcopyw(font_buffer+((scp->cursor_saveunder & 0xff)*scp->font_size),
3499 	       	   cursor, scp->font_size);
3500    }
3501    else
3502    	bcopyw(font_buffer + ((scp->cursor_saveunder & 0xff) * scp->font_size),
3503 	       cursor, scp->font_size);
3504    for (i=0; i<32; i++)
3505	if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
3506	    (scp->cursor_start >= scp->font_size && i == scp->font_size - 1))
3507	    cursor[i] |= 0xff;
3508#if 1
3509    while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3510#endif
3511    set_font_mode();
3512    bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
3513    set_normal_mode();
3514}
3515
3516static void
3517set_mouse_pos(scr_stat *scp)
3518{
3519    static int last_xpos = -1, last_ypos = -1;
3520    /*
3521     * the margins imposed here are not ideal, we loose
3522     * a couble of pixels on the borders..
3523     */
3524    if (scp->mouse_xpos < 0)
3525	scp->mouse_xpos = 0;
3526    if (scp->mouse_ypos < 0)
3527	scp->mouse_ypos = 0;
3528    if (scp->mouse_xpos > (scp->xsize*8)-2)
3529	scp->mouse_xpos = (scp->xsize*8)-2;
3530    if (scp->mouse_ypos > (scp->ysize*scp->font_size)-2)
3531	scp->mouse_ypos = (scp->ysize*scp->font_size)-2;
3532
3533    if (scp->status & UNKNOWN_MODE)
3534	return;
3535
3536    if (scp->mouse_xpos != last_xpos || scp->mouse_ypos != last_ypos) {
3537	scp->status |= MOUSE_MOVED;
3538
3539    	scp->mouse_pos = scp->scr_buf +
3540	    ((scp->mouse_ypos/scp->font_size)*scp->xsize + scp->mouse_xpos/8);
3541
3542	if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING)) {
3543	    u_short *ptr;
3544	    int i = 0;
3545
3546	    mark_for_update(scp, scp->mouse_cut_start - scp->scr_buf);
3547	    mark_for_update(scp, scp->mouse_cut_end - scp->scr_buf);
3548	    scp->mouse_cut_end = scp->mouse_pos;
3549	    for (ptr = (scp->mouse_cut_start > scp->mouse_cut_end
3550			? scp->mouse_cut_end : scp->mouse_cut_start);
3551		 ptr <= (scp->mouse_cut_start > scp->mouse_cut_end
3552			 ? scp->mouse_cut_start : scp->mouse_cut_end);
3553	    	 ptr++) {
3554	        cut_buffer[i++] = *ptr & 0xff;
3555	        if (((ptr - scp->scr_buf) % scp->xsize) == (scp->xsize - 1)) {
3556		    cut_buffer[i++] = '\n';
3557	        }
3558	    }
3559	    cut_buffer[i] = 0x00;
3560        }
3561    }
3562}
3563
3564static void
3565mouse_cut_start(scr_stat *scp)
3566{
3567    int i;
3568
3569    if (scp->status & MOUSE_VISIBLE) {
3570	if (scp->mouse_pos == scp->mouse_cut_start &&
3571	    scp->mouse_cut_start == scp->mouse_cut_end) {
3572	    cut_buffer[0] = 0x00;
3573	    remove_cutmarking(scp);
3574	}
3575	else {
3576	    scp->mouse_cut_start = scp->mouse_cut_end = scp->mouse_pos;
3577	    cut_buffer[0] = *scp->mouse_cut_start & 0xff;
3578	    cut_buffer[1] = 0x00;
3579	    scp->status |= MOUSE_CUTTING;
3580	}
3581    	mark_all(scp);
3582	/* delete all other screens cut markings */
3583	for (i=0; i<MAXCONS; i++) {
3584	    if (console[i] == NULL || console[i] == scp)
3585		continue;
3586	    remove_cutmarking(console[i]);
3587	}
3588    }
3589}
3590
3591static void
3592mouse_cut_end(scr_stat *scp)
3593{
3594    if (scp->status & MOUSE_VISIBLE) {
3595	scp->status &= ~MOUSE_CUTTING;
3596    }
3597}
3598
3599static void
3600mouse_paste(scr_stat *scp)
3601{
3602    if (scp->status & MOUSE_VISIBLE) {
3603	struct tty *tp;
3604	u_char *ptr = cut_buffer;
3605
3606	tp = VIRTUAL_TTY(get_scr_num());
3607	while (*ptr)
3608	    (*linesw[tp->t_line].l_rint)(scr_rmap[*ptr++], tp);
3609    }
3610}
3611
3612static void
3613draw_mouse_image(scr_stat *scp)
3614{
3615    caddr_t address;
3616    int i;
3617    char *font_buffer;
3618    u_short buffer[32];
3619    u_short xoffset, yoffset;
3620    u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
3621    int font_size = scp->font_size;
3622
3623    if (font_size < FONT_14) {
3624	font_buffer = font_8;
3625	address = (caddr_t)VIDEOMEM + 0x8000;
3626    }
3627    else if (font_size >= FONT_16) {
3628	font_buffer = font_16;
3629	address = (caddr_t)VIDEOMEM;
3630    }
3631    else {
3632	font_buffer = font_14;
3633	address = (caddr_t)VIDEOMEM + 0x4000;
3634    }
3635    xoffset = scp->mouse_xpos % 8;
3636    yoffset = scp->mouse_ypos % font_size;
3637
3638    /* prepare mousepointer char's bitmaps */
3639    bcopyw(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
3640	   &scp->mouse_cursor[0], font_size);
3641    bcopyw(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
3642	   &scp->mouse_cursor[32], font_size);
3643    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
3644	   &scp->mouse_cursor[64], font_size);
3645    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
3646	   &scp->mouse_cursor[96], font_size);
3647    for (i=0; i<font_size; i++) {
3648	buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
3649	buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
3650    }
3651
3652    /* now and-or in the mousepointer image */
3653    for (i=0; i<16; i++) {
3654	buffer[i+yoffset] =
3655	    ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
3656	    | (mouse_or_mask[i] >> xoffset);
3657    }
3658    for (i=0; i<font_size; i++) {
3659	scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
3660	scp->mouse_cursor[i+32] = buffer[i] & 0xff;
3661	scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
3662	scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
3663    }
3664
3665    scp->mouse_oldpos = scp->mouse_pos;
3666
3667    /* wait for vertical retrace to avoid jitter on some videocards */
3668#if 1
3669    while (!(inb(crtc_addr+6) & 0x08)) /* idle */ ;
3670#endif
3671    set_font_mode();
3672    bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + 0xd0 * 32, 128);
3673    set_normal_mode();
3674    *(crt_pos) = (*(scp->mouse_pos)&0xff00)|0xd0;
3675    *(crt_pos+scp->xsize) = (*(scp->mouse_pos+scp->xsize)&0xff00)|0xd2;
3676    if (scp->mouse_xpos < (scp->xsize-1)*8) {
3677    	*(crt_pos+1) = (*(scp->mouse_pos+1)&0xff00)|0xd1;
3678    	*(crt_pos+scp->xsize+1) = (*(scp->mouse_pos+scp->xsize+1)&0xff00)|0xd3;
3679    }
3680    mark_for_update(scp, scp->mouse_pos - scp->scr_buf);
3681    mark_for_update(scp, scp->mouse_pos + scp->xsize + 1 - scp->scr_buf);
3682}
3683
3684static void
3685remove_mouse_image(scr_stat *scp)
3686{
3687    u_short *crt_pos = Crtat + (scp->mouse_oldpos - scp->scr_buf);
3688
3689    *(crt_pos) = *(scp->mouse_oldpos);
3690    *(crt_pos+1) = *(scp->mouse_oldpos+1);
3691    *(crt_pos+scp->xsize) = *(scp->mouse_oldpos+scp->xsize);
3692    *(crt_pos+scp->xsize+1) = *(scp->mouse_oldpos+scp->xsize+1);
3693    mark_for_update(scp, scp->mouse_oldpos - scp->scr_buf);
3694    mark_for_update(scp, scp->mouse_oldpos + scp->xsize + 1 - scp->scr_buf);
3695}
3696
3697static void
3698draw_cutmarking(scr_stat *scp)
3699{
3700    u_short *ptr;
3701    u_short och, nch;
3702
3703    for (ptr=scp->scr_buf; ptr<=(scp->scr_buf+(scp->xsize*scp->ysize)); ptr++) {
3704	nch = och = *(Crtat + (ptr - scp->scr_buf));
3705	/* are we outside the selected area ? */
3706	if ( ptr < (scp->mouse_cut_start > scp->mouse_cut_end ?
3707	            scp->mouse_cut_end : scp->mouse_cut_start) ||
3708	     ptr > (scp->mouse_cut_start > scp->mouse_cut_end ?
3709	            scp->mouse_cut_start : scp->mouse_cut_end)) {
3710	    if (ptr != scp->cursor_pos)
3711		nch = (och & 0xff) | (*ptr & 0xff00);
3712	}
3713	else {
3714	    /* are we clear of the cursor image ? */
3715	    if (ptr != scp->cursor_pos)
3716		nch = (och & 0x88ff) | (*ptr & 0x7000)>>4 | (*ptr & 0x0700)<<4;
3717	    else {
3718		if (flags & CHAR_CURSOR)
3719		    nch = (och & 0x88ff)|(*ptr & 0x7000)>>4|(*ptr & 0x0700)<<4;
3720		else
3721		    if (!(flags & BLINK_CURSOR))
3722		        nch = (och & 0xff) | (*ptr & 0xff00);
3723	    }
3724	}
3725	if (nch != och)
3726	    *(Crtat + (ptr - scp->scr_buf)) = nch;
3727    }
3728}
3729
3730static void
3731remove_cutmarking(scr_stat *scp)
3732{
3733    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
3734    scp->status &= ~MOUSE_CUTTING;
3735    mark_all(scp);
3736}
3737
3738static void
3739save_palette(void)
3740{
3741    int i;
3742
3743    outb(PALRADR, 0x00);
3744    for (i=0x00; i<0x300; i++)
3745	palette[i] = inb(PALDATA);
3746    inb(crtc_addr+6);           /* reset flip/flop */
3747}
3748
3749void
3750load_palette(char *palette)
3751{
3752    int i;
3753
3754    outb(PIXMASK, 0xFF);            /* no pixelmask */
3755    outb(PALWADR, 0x00);
3756    for (i=0x00; i<0x300; i++)
3757	 outb(PALDATA, palette[i]);
3758    inb(crtc_addr+6);           /* reset flip/flop */
3759    outb(ATC, 0x20);            /* enable palette */
3760}
3761
3762static void
3763do_bell(scr_stat *scp, int pitch, int duration)
3764{
3765    if (flags & VISUAL_BELL) {
3766	if (blink_in_progress)
3767	    return;
3768	blink_in_progress = 4;
3769	if (scp != cur_console)
3770	    blink_in_progress += 2;
3771	blink_screen(cur_console);
3772	timeout((timeout_func_t)blink_screen, cur_console, hz/10);
3773    } else {
3774	if (scp != cur_console)
3775	    pitch *= 2;
3776	sysbeep(pitch, duration);
3777    }
3778}
3779
3780static void
3781blink_screen(scr_stat *scp)
3782{
3783    if (blink_in_progress > 1) {
3784	if (blink_in_progress & 1)
3785	    fillw(kernel_default.std_color | scr_map[0x20],
3786		  Crtat, scp->xsize * scp->ysize);
3787	else
3788	    fillw(kernel_default.rev_color | scr_map[0x20],
3789		  Crtat, scp->xsize * scp->ysize);
3790	blink_in_progress--;
3791	timeout((timeout_func_t)blink_screen, scp, hz/10);
3792    }
3793    else {
3794	blink_in_progress = FALSE;
3795    	mark_all(scp);
3796	if (delayed_next_scr)
3797	    switch_scr(scp, delayed_next_scr - 1);
3798    }
3799}
3800
3801#ifdef SC_SPLASH_SCREEN
3802static void
3803toggle_splash_screen(scr_stat *scp)
3804{
3805    static int toggle = 0;
3806    static u_char save_mode;
3807    int s = splhigh();
3808
3809    if (toggle) {
3810	scp->mode = save_mode;
3811	scp->status &= ~UNKNOWN_MODE;
3812	set_mode(scp);
3813	load_palette(palette);
3814	toggle = 0;
3815    }
3816    else {
3817	save_mode = scp->mode;
3818	scp->mode = M_VGA_CG320;
3819	scp->status |= UNKNOWN_MODE;
3820	set_mode(scp);
3821	/* load image */
3822	toggle = 1;
3823    }
3824    splx(s);
3825}
3826#endif
3827#endif /* NSC */
3828