syscons.c revision 21805
1/*-
2 * Copyright (c) 1992-1997 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *  $FreeBSD: head/sys/dev/syscons/syscons.c 21805 1997-01-17 15:49:13Z sos $
29 */
30
31#include "sc.h"
32#include "apm.h"
33#include "opt_ddb.h"
34#include "opt_syscons.h"
35
36#if NSC > 0
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/conf.h>
40#include <sys/ioctl.h>
41#include <sys/proc.h>
42#include <sys/signalvar.h>
43#include <sys/tty.h>
44#include <sys/uio.h>
45#include <sys/callout.h>
46#include <sys/kernel.h>
47#include <sys/syslog.h>
48#include <sys/errno.h>
49#include <sys/malloc.h>
50#ifdef	DEVFS
51#include <sys/devfsext.h>
52#endif
53
54#include <machine/clock.h>
55#include <machine/cons.h>
56#include <machine/console.h>
57#include <machine/md_var.h>
58#include <machine/psl.h>
59#include <machine/frame.h>
60#include <machine/pc/display.h>
61#include <machine/apm_bios.h>
62#include <machine/random.h>
63
64#include <vm/vm.h>
65#include <vm/vm_param.h>
66#include <vm/pmap.h>
67
68#include <i386/isa/isa.h>
69#include <i386/isa/isa_device.h>
70#include <i386/isa/timerreg.h>
71#include <i386/isa/kbdtables.h>
72#include <i386/isa/kbdio.h>
73#include <i386/isa/syscons.h>
74
75#if !defined(MAXCONS)
76#define MAXCONS 16
77#endif
78
79#define COLD 0
80#define WARM 1
81
82/* this may break on older VGA's but is useful on real 32 bit systems */
83#define bcopyw  bcopy
84
85static default_attr user_default = {
86    (FG_LIGHTGREY | BG_BLACK) << 8,
87    (FG_BLACK | BG_LIGHTGREY) << 8
88};
89
90static default_attr kernel_default = {
91    (FG_WHITE | BG_BLACK) << 8,
92    (FG_BLACK | BG_LIGHTGREY) << 8
93};
94
95static  scr_stat    	main_console;
96static  scr_stat    	*console[MAXCONS];
97#ifdef DEVFS
98static	void		*sc_devfs_token[MAXCONS];
99#endif
100	scr_stat    	*cur_console;
101static  scr_stat    	*new_scp, *old_scp;
102static  term_stat   	kernel_console;
103static  default_attr    *current_default;
104static  int     	flags = 0;
105static  int		sc_port = IO_KBD;
106static  KBDC		sc_kbdc = NULL;
107static  char        	init_done = COLD;
108static  u_short		sc_buffer[ROW*COL];
109static  char        	switch_in_progress = FALSE;
110static  char        	write_in_progress = FALSE;
111static  char        	blink_in_progress = FALSE;
112static  int        	blinkrate = 0;
113	u_int       	crtc_addr = MONO_BASE;
114	char        	crtc_vga = FALSE;
115static  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
116static  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
117static  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
118static  int     	delayed_next_scr = FALSE;
119static  long        	scrn_blank_time = 0;    /* screen saver timeout value */
120	int     	scrn_blanked = FALSE;   /* screen saver active flag */
121static  long       	scrn_time_stamp;
122	u_char      	scr_map[256];
123	u_char      	scr_rmap[256];
124	char        	*video_mode_ptr = NULL;
125	int     	fonts_loaded = 0;
126	char        	font_8[256*8];
127	char		font_14[256*14];
128	char		font_16[256*16];
129	char        	palette[256*3];
130static	char 		*cut_buffer;
131static  u_short 	mouse_and_mask[16] = {
132				0xc000, 0xe000, 0xf000, 0xf800,
133				0xfc00, 0xfe00, 0xff00, 0xff80,
134				0xfe00, 0x1e00, 0x1f00, 0x0f00,
135				0x0f00, 0x0000, 0x0000, 0x0000
136			};
137static  u_short 	mouse_or_mask[16] = {
138				0x0000, 0x4000, 0x6000, 0x7000,
139				0x7800, 0x7c00, 0x7e00, 0x6800,
140				0x0c00, 0x0c00, 0x0600, 0x0600,
141				0x0000, 0x0000, 0x0000, 0x0000
142			};
143
144static void    		none_saver(int blank) { }
145void    		(*current_saver)(int blank) = none_saver;
146int  			(*sc_user_ioctl)(dev_t dev, int cmd, caddr_t data,
147					 int flag, struct proc *p) = NULL;
148
149/* OS specific stuff */
150#ifdef not_yet_done
151#define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
152struct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
153struct  MOUSE_TTY 	(sccons[MAXCONS+1] = ttymalloc(sccons[MAXCONS+1]))
154struct  tty         	*sccons[MAXCONS+2];
155#else
156#define VIRTUAL_TTY(x)  &sccons[x]
157#define CONSOLE_TTY 	&sccons[MAXCONS]
158#define MOUSE_TTY 	&sccons[MAXCONS+1]
159static struct tty     	sccons[MAXCONS+2];
160#endif
161#define SC_MOUSE 	128
162#define SC_CONSOLE	255
163#define MONO_BUF    	pa_to_va(0xB0000)
164#define CGA_BUF     	pa_to_va(0xB8000)
165u_short         	*Crtat;
166static const int	nsccons = MAXCONS+2;
167
168#define WRAPHIST(scp, pointer, offset)\
169    ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
170    + (offset)) % (scp->history_size)))
171
172/* prototypes */
173static int scattach(struct isa_device *dev);
174static int scparam(struct tty *tp, struct termios *t);
175static int scprobe(struct isa_device *dev);
176static void scstart(struct tty *tp);
177static void scmousestart(struct tty *tp);
178static void scinit(void);
179static u_int scgetc(u_int flags);
180#define SCGETC_CN	1
181#define SCGETC_NONBLOCK	2
182static scr_stat *get_scr_stat(dev_t dev);
183static scr_stat *alloc_scp(void);
184static void init_scp(scr_stat *scp);
185static int get_scr_num(void);
186static void scrn_timer(void);
187static void clear_screen(scr_stat *scp);
188static int switch_scr(scr_stat *scp, u_int next_scr);
189static void exchange_scr(void);
190static inline void move_crsr(scr_stat *scp, int x, int y);
191static void scan_esc(scr_stat *scp, u_char c);
192static void draw_cursor_image(scr_stat *scp);
193static void remove_cursor_image(scr_stat *scp);
194static void ansi_put(scr_stat *scp, u_char *buf, int len);
195static u_char *get_fstr(u_int c, u_int *len);
196static void history_to_screen(scr_stat *scp);
197static int history_up_line(scr_stat *scp);
198static int history_down_line(scr_stat *scp);
199static int mask2attr(struct term_stat *term);
200static void set_keyboard(int command, int data);
201static void update_leds(int which);
202static void set_vgaregs(char *modetable);
203static void set_font_mode(void);
204static void set_normal_mode(void);
205static void set_destructive_cursor(scr_stat *scp);
206static void set_mouse_pos(scr_stat *scp);
207static void mouse_cut_start(scr_stat *scp);
208static void mouse_cut_end(scr_stat *scp);
209static void mouse_paste(scr_stat *scp);
210static void draw_mouse_image(scr_stat *scp);
211static void remove_mouse_image(scr_stat *scp);
212static void draw_cutmarking(scr_stat *scp);
213static void remove_cutmarking(scr_stat *scp);
214static void save_palette(void);
215static void do_bell(scr_stat *scp, int pitch, int duration);
216static void blink_screen(scr_stat *scp);
217#ifdef SC_SPLASH_SCREEN
218static void toggle_splash_screen(scr_stat *scp);
219#endif
220
221struct  isa_driver scdriver = {
222    scprobe, scattach, "sc", 1
223};
224
225static	d_open_t	scopen;
226static	d_close_t	scclose;
227static	d_read_t	scread;
228static	d_write_t	scwrite;
229static	d_ioctl_t	scioctl;
230static	d_devtotty_t	scdevtotty;
231static	d_mmap_t	scmmap;
232
233#define CDEV_MAJOR 12
234static	struct cdevsw	scdevsw = {
235	scopen,		scclose,	scread,		scwrite,
236	scioctl,	nullstop,	noreset,	scdevtotty,
237	ttselect,	scmmap,		nostrategy,	"sc",	NULL,	-1 };
238
239/*
240 * These functions need to be before calls to them so they can be inlined.
241 */
242static inline void
243draw_cursor_image(scr_stat *scp)
244{
245    u_short cursor_image, *ptr = Crtat + (scp->cursor_pos - scp->scr_buf);
246
247    /* do we have a destructive cursor ? */
248    if (flags & CHAR_CURSOR) {
249	cursor_image = *scp->cursor_pos;
250	scp->cursor_saveunder = cursor_image;
251	/* modify cursor_image */
252	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
253	    set_destructive_cursor(scp);
254	    cursor_image &= 0xff00;
255	    cursor_image |= DEAD_CHAR;
256	}
257    }
258    else {
259	cursor_image = (*(ptr) & 0x00ff) | *(scp->cursor_pos) & 0xff00;
260	scp->cursor_saveunder = cursor_image;
261	if (!(flags & BLINK_CURSOR)||((flags & BLINK_CURSOR)&&(blinkrate & 4))){
262	    if ((cursor_image & 0x7000) == 0x7000) {
263		cursor_image &= 0x8fff;
264		if(!(cursor_image & 0x0700))
265		    cursor_image |= 0x0700;
266	    } else {
267		cursor_image |= 0x7000;
268		if ((cursor_image & 0x0700) == 0x0700)
269		    cursor_image &= 0xf0ff;
270	    }
271	}
272    }
273    *ptr = cursor_image;
274}
275
276static inline void
277remove_cursor_image(scr_stat *scp)
278{
279    *(Crtat + (scp->cursor_oldpos - scp->scr_buf)) = scp->cursor_saveunder;
280}
281
282static inline void
283move_crsr(scr_stat *scp, int x, int y)
284{
285    if (x < 0)
286	x = 0;
287    if (y < 0)
288	y = 0;
289    if (x >= scp->xsize)
290	x = scp->xsize-1;
291    if (y >= scp->ysize)
292	y = scp->ysize-1;
293    scp->xpos = x;
294    scp->ypos = y;
295    scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
296}
297
298static int
299scprobe(struct isa_device *dev)
300{
301    int codeset;
302    int c = -1;
303    int m;
304
305    sc_port = dev->id_iobase;
306    sc_kbdc = kbdc_open(sc_port);
307
308    if (!kbdc_lock(sc_kbdc, TRUE)) {
309	/* driver error? */
310	printf("sc%d: unable to lock the controller.\n", dev->id_unit);
311        return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
312    }
313
314    /* discard anything left after UserConfig */
315    empty_both_buffers(sc_kbdc, 10);
316
317    /* save the current keyboard controller command byte */
318    m = kbdc_get_device_mask(sc_kbdc) & ~KBD_KBD_CONTROL_BITS;
319    c = get_controller_command_byte(sc_kbdc);
320    if (c == -1) {
321	/* CONTROLLER ERROR */
322	printf("sc%d: unable to get the current command byte value.\n",
323	    dev->id_unit);
324	goto fail;
325    }
326    if (bootverbose)
327	printf("sc%d: the current keyboard controller command byte %04x\n",
328	    dev->id_unit, c);
329#if 0
330    /* override the keyboard lock switch */
331    c |= KBD_OVERRIDE_KBD_LOCK;
332#endif
333
334    /*
335     * enable the keyboard port, but disable the keyboard intr.
336     * the aux port (mouse port) is disabled too.
337     */
338    if (!set_controller_command_byte(sc_kbdc,
339            KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
340            KBD_ENABLE_KBD_PORT | KBD_DISABLE_KBD_INT
341                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
342	/* CONTROLLER ERROR
343	 * there is very little we can do...
344	 */
345	printf("sc%d: unable to set the command byte.\n", dev->id_unit);
346	goto fail;
347     }
348
349     /*
350      * Check if we have an XT keyboard before we attempt to reset it.
351      * The procedure assumes that the keyboard and the controller have
352      * been set up properly by BIOS and have not been messed up
353      * during the boot process.
354      */
355     codeset = -1;
356     if (dev->id_flags & XT_KEYBD)
357	 /* the user says there is a XT keyboard */
358	 codeset = 1;
359#ifdef DETECT_XT_KEYBOARD
360     else if ((c & KBD_TRANSLATION) == 0) {
361	 /* SET_SCANCODE_SET is not always supported; ignore error */
362	 if (send_kbd_command_and_data(sc_kbdc, KBDC_SET_SCANCODE_SET, 0)
363		 == KBD_ACK)
364	     codeset = read_kbd_data(sc_kbdc);
365     }
366     if (bootverbose)
367         printf("sc%d: keyboard scancode set %d\n", dev->id_unit, codeset);
368#endif /* DETECT_XT_KEYBOARD */
369
370     /* reset keyboard hardware */
371     if (!reset_kbd(sc_kbdc)) {
372        /* KEYBOARD ERROR
373	 * Keyboard reset may fail either because the keyboard doen't exist,
374         * or because the keyboard doesn't pass the self-test, or the keyboard
375         * controller on the motherboard and the keyboard somehow fail to
376         * shake hands. It is just possible, particularly in the last case,
377         * that the keyoard controller may be left in a hung state.
378         * test_controller() and test_kbd_port() appear to bring the keyboard
379         * controller back (I don't know why and how, though.)
380	 */
381	empty_both_buffers(sc_kbdc, 10);
382	test_controller(sc_kbdc);
383	test_kbd_port(sc_kbdc);
384	/* We could disable the keyboard port and interrupt... but,
385	 * the keyboard may still exist (see above).
386	 */
387        if (bootverbose)
388	   printf("sc%d: failed to reset the keyboard.\n", dev->id_unit);
389	goto fail;
390    }
391
392    /*
393     * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
394     * such as those on the IBM ThinkPad laptop computers can be used
395     * with the standard console driver.
396     */
397    if (codeset == 1) {
398	if (send_kbd_command_and_data(
399	        sc_kbdc, KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
400	    /* XT kbd doesn't need scan code translation */
401	    c &= ~KBD_TRANSLATION;
402	} else {
403	    /* KEYBOARD ERROR
404	     * The XT kbd isn't usable unless the proper scan code set
405	     * is selected.
406	     */
407	    printf("sc%d: unable to set the XT keyboard mode.\n", dev->id_unit);
408	    goto fail;
409	}
410    }
411    /* enable the keyboard port and intr. */
412    if (!set_controller_command_byte(sc_kbdc,
413            KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS | KBD_OVERRIDE_KBD_LOCK,
414	    (c & (KBD_AUX_CONTROL_BITS | KBD_OVERRIDE_KBD_LOCK))
415	        | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
416	/* CONTROLLER ERROR
417	 * This is serious; we are left with the disabled keyboard intr.
418	 */
419	printf("sc%d: unable to enable the keyboard port and intr.\n",
420	    dev->id_unit);
421	goto fail;
422    }
423
424succeed:
425    kbdc_set_device_mask(sc_kbdc, m | KBD_KBD_CONTROL_BITS),
426    kbdc_lock(sc_kbdc, FALSE);
427    return (IO_KBDSIZE);
428
429fail:
430    if (c != -1)
431        /* try to restore the command byte as before, if possible */
432        set_controller_command_byte(sc_kbdc, 0xff, c);
433    kbdc_set_device_mask(sc_kbdc,
434        (dev->id_flags & DETECT_KBD) ? m : m | KBD_KBD_CONTROL_BITS);
435    kbdc_lock(sc_kbdc, FALSE);
436    return ((dev->id_flags & DETECT_KBD) ? 0 : IO_KBDSIZE);
437}
438
439#if NAPM > 0
440static int
441scresume(void *dummy)
442{
443	shfts = ctls = alts = agrs = metas = 0;
444	return 0;
445}
446#endif
447
448static int
449scattach(struct isa_device *dev)
450{
451    scr_stat *scp;
452    dev_t cdev = makedev(CDEV_MAJOR, 0);
453#ifdef DEVFS
454    int vc;
455#endif
456
457    scinit();
458    flags = dev->id_flags;
459
460    scp = console[0];
461
462    if (crtc_vga) {
463    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
464    }
465
466    scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
467				     M_DEVBUF, M_NOWAIT);
468
469    /* copy temporary buffer to final buffer */
470    bcopyw(sc_buffer, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
471
472    scp->cursor_pos = scp->cursor_oldpos =
473	scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
474    scp->mouse_pos = scp->mouse_oldpos =
475	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
476	    		scp->mouse_xpos/8);
477
478    /* initialize history buffer & pointers */
479    scp->history_head = scp->history_pos = scp->history =
480	(u_short *)malloc(scp->history_size*sizeof(u_short),
481			  M_DEVBUF, M_NOWAIT);
482    bzero(scp->history_head, scp->history_size*sizeof(u_short));
483
484    /* initialize cursor stuff */
485    if (!(scp->status & UNKNOWN_MODE)) {
486    	draw_cursor_image(scp);
487    	if (crtc_vga && (flags & CHAR_CURSOR))
488	    set_destructive_cursor(scp);
489    }
490
491    /* get screen update going */
492    scrn_timer();
493
494    update_leds(scp->status);
495
496    printf("sc%d: ", dev->id_unit);
497    if (crtc_vga)
498	if (crtc_addr == MONO_BASE)
499	    printf("VGA mono");
500	else
501	    printf("VGA color");
502    else
503	if (crtc_addr == MONO_BASE)
504	    printf("MDA/hercules");
505	else
506	    printf("CGA/EGA");
507    printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, flags);
508
509#if NAPM > 0
510    scp->r_hook.ah_fun = scresume;
511    scp->r_hook.ah_arg = NULL;
512    scp->r_hook.ah_name = "system keyboard";
513    scp->r_hook.ah_order = APM_MID_ORDER;
514    apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
515#endif
516
517    cdevsw_add(&cdev, &scdevsw, NULL);
518
519#ifdef DEVFS
520    for (vc = 0; vc < MAXCONS; vc++)
521        sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR, UID_ROOT,
522					      GID_WHEEL, 0600, "ttyv%n", vc);
523#endif
524    return 0;
525}
526
527struct tty
528*scdevtotty(dev_t dev)
529{
530    int unit = minor(dev);
531
532    if (init_done == COLD)
533	return(NULL);
534    if (unit == SC_CONSOLE)
535	return CONSOLE_TTY;
536    if (unit == SC_MOUSE)
537	return MOUSE_TTY;
538    if (unit >= MAXCONS || unit < 0)
539	return(NULL);
540    return VIRTUAL_TTY(unit);
541}
542
543int
544scopen(dev_t dev, int flag, int mode, struct proc *p)
545{
546    struct tty *tp = scdevtotty(dev);
547
548    if (!tp)
549	return(ENXIO);
550
551    tp->t_oproc = (minor(dev) == SC_MOUSE) ? scmousestart : scstart;
552    tp->t_param = scparam;
553    tp->t_dev = dev;
554    if (!(tp->t_state & TS_ISOPEN)) {
555	ttychars(tp);
556	tp->t_iflag = TTYDEF_IFLAG;
557	tp->t_oflag = TTYDEF_OFLAG;
558	tp->t_cflag = TTYDEF_CFLAG;
559	tp->t_lflag = TTYDEF_LFLAG;
560	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
561	scparam(tp, &tp->t_termios);
562	ttsetwater(tp);
563	(*linesw[tp->t_line].l_modem)(tp, 1);
564    }
565    else
566	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
567	    return(EBUSY);
568    if (minor(dev) < MAXCONS && !console[minor(dev)]) {
569	console[minor(dev)] = alloc_scp();
570    }
571    if (minor(dev)<MAXCONS && !tp->t_winsize.ws_col && !tp->t_winsize.ws_row) {
572	tp->t_winsize.ws_col = console[minor(dev)]->xsize;
573	tp->t_winsize.ws_row = console[minor(dev)]->ysize;
574    }
575    return ((*linesw[tp->t_line].l_open)(dev, tp));
576}
577
578int
579scclose(dev_t dev, int flag, int mode, struct proc *p)
580{
581    struct tty *tp = scdevtotty(dev);
582    struct scr_stat *scp;
583
584    if (!tp)
585	return(ENXIO);
586    if (minor(dev) < MAXCONS) {
587	scp = get_scr_stat(tp->t_dev);
588	if (scp->status & SWITCH_WAIT_ACQ)
589	    wakeup((caddr_t)&scp->smode);
590#if not_yet_done
591	if (scp == &main_console) {
592	    scp->pid = 0;
593	    scp->proc = NULL;
594	    scp->smode.mode = VT_AUTO;
595	}
596	else {
597	    free(scp->scr_buf, M_DEVBUF);
598	    free(scp->history, M_DEVBUF);
599	    free(scp, M_DEVBUF);
600	    console[minor(dev)] = NULL;
601	}
602#else
603	scp->pid = 0;
604	scp->proc = NULL;
605	scp->smode.mode = VT_AUTO;
606#endif
607    }
608    spltty();
609    (*linesw[tp->t_line].l_close)(tp, flag);
610    ttyclose(tp);
611    spl0();
612    return(0);
613}
614
615int
616scread(dev_t dev, struct uio *uio, int flag)
617{
618    struct tty *tp = scdevtotty(dev);
619
620    if (!tp)
621	return(ENXIO);
622    return((*linesw[tp->t_line].l_read)(tp, uio, flag));
623}
624
625int
626scwrite(dev_t dev, struct uio *uio, int flag)
627{
628    struct tty *tp = scdevtotty(dev);
629
630    if (!tp)
631	return(ENXIO);
632    return((*linesw[tp->t_line].l_write)(tp, uio, flag));
633}
634
635void
636scintr(int unit)
637{
638    static struct tty *cur_tty;
639    int c, len;
640    u_char *cp;
641
642    /* make screensaver happy */
643    scrn_time_stamp = time.tv_sec;
644    if (scrn_blanked) {
645	(*current_saver)(FALSE);
646	mark_all(cur_console);
647    }
648
649    /*
650     * Loop while there is still input to get from the keyboard.
651     * I don't think this is nessesary, and it doesn't fix
652     * the Xaccel-2.1 keyboard hang, but it can't hurt.		XXX
653     */
654    while ((c = scgetc(SCGETC_NONBLOCK)) != NOKEY) {
655
656	cur_tty = VIRTUAL_TTY(get_scr_num());
657	if (!(cur_tty->t_state & TS_ISOPEN))
658	    if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
659		continue;
660
661	switch (c & 0xff00) {
662	case 0x0000: /* normal key */
663	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
664	    break;
665	case FKEY:  /* function key, return string */
666	    if (cp = get_fstr((u_int)c, (u_int *)&len)) {
667	    	while (len-- >  0)
668		    (*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
669	    }
670	    break;
671	case MKEY:  /* meta is active, prepend ESC */
672	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
673	    (*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
674	    break;
675	case BKEY:  /* backtab fixed sequence (esc [ Z) */
676	    (*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
677	    (*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
678	    (*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
679	    break;
680	}
681    }
682
683    if (cur_console->status & MOUSE_ENABLED) {
684	cur_console->status &= ~MOUSE_VISIBLE;
685	remove_mouse_image(cur_console);
686    }
687}
688
689static int
690scparam(struct tty *tp, struct termios *t)
691{
692    tp->t_ispeed = t->c_ispeed;
693    tp->t_ospeed = t->c_ospeed;
694    tp->t_cflag = t->c_cflag;
695    return 0;
696}
697
698int
699scioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
700{
701    int error;
702    u_int i;
703    struct tty *tp;
704    struct trapframe *fp;
705    scr_stat *scp;
706
707    tp = scdevtotty(dev);
708    if (!tp)
709	return ENXIO;
710    scp = get_scr_stat(tp->t_dev);
711
712    /* If there is a user_ioctl function call that first */
713    if (sc_user_ioctl) {
714	if (error = (*sc_user_ioctl)(dev, cmd, data, flag, p))
715	    return error;
716    }
717
718    switch (cmd) {  		/* process console hardware related ioctl's */
719
720    case GIO_ATTR:      	/* get current attributes */
721	*(int*)data = (scp->term.cur_attr >> 8) & 0xFF;
722	return 0;
723
724    case GIO_COLOR:     	/* is this a color console ? */
725	if (crtc_addr == COLOR_BASE)
726	    *(int*)data = 1;
727	else
728	    *(int*)data = 0;
729	return 0;
730
731    case CONS_CURRENT:  	/* get current adapter type */
732	if (crtc_vga)
733	    *(int*)data = KD_VGA;
734	else
735	    if (crtc_addr == MONO_BASE)
736		*(int*)data = KD_MONO;
737	    else
738		*(int*)data = KD_CGA;
739	return 0;
740
741    case CONS_GET:      	/* get current video mode */
742	*(int*)data = scp->mode;
743	return 0;
744
745    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
746	scrn_blank_time = *(int*)data;
747	return 0;
748
749    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
750	if ((*(int*)data) & 0x01)
751	    flags |= BLINK_CURSOR;
752	else
753	    flags &= ~BLINK_CURSOR;
754	if ((*(int*)data) & 0x02) {
755	    if (!crtc_vga)
756		return ENXIO;
757	    flags |= CHAR_CURSOR;
758	    set_destructive_cursor(scp);
759	} else
760	    flags &= ~CHAR_CURSOR;
761	return 0;
762
763    case CONS_BELLTYPE: 	/* set bell type sound/visual */
764	if (*data)
765	    flags |= VISUAL_BELL;
766	else
767	    flags &= ~VISUAL_BELL;
768	return 0;
769
770    case CONS_HISTORY:  	/* set history size */
771	if (*data) {
772	    free(scp->history, M_DEVBUF);
773	    scp->history_size = *(int*)data;
774	    if (scp->history_size < scp->ysize)
775		scp->history = NULL;
776	    else {
777		scp->history_size *= scp->xsize;
778		scp->history_head = scp->history_pos = scp->history =
779		    (u_short *)malloc(scp->history_size*sizeof(u_short),
780				      M_DEVBUF, M_WAITOK);
781		bzero(scp->history_head, scp->history_size*sizeof(u_short));
782	    }
783	    return 0;
784	}
785	else
786	    return EINVAL;
787
788    case CONS_MOUSECTL:		/* control mouse arrow */
789    {
790	mouse_info_t *mouse = (mouse_info_t*)data;
791
792	if (!crtc_vga)
793	    return ENXIO;
794
795	switch (mouse->operation) {
796	case MOUSE_MODE:
797	    if (mouse->u.mode.signal > 0 && mouse->u.mode.signal < NSIG) {
798		scp->mouse_signal = mouse->u.mode.signal;
799		scp->mouse_proc = p;
800		scp->mouse_pid = p->p_pid;
801	    }
802	    else {
803		scp->mouse_signal = 0;
804		scp->mouse_proc = NULL;
805		scp->mouse_pid = 0;
806	    }
807	    break;
808
809	case MOUSE_SHOW:
810	    if (!(scp->status & MOUSE_ENABLED)) {
811		scp->status |= (MOUSE_ENABLED | MOUSE_VISIBLE);
812		scp->mouse_oldpos = scp->mouse_pos;
813		mark_all(scp);
814	    }
815	    else
816		return EINVAL;
817	    break;
818
819	case MOUSE_HIDE:
820	    if (scp->status & MOUSE_ENABLED) {
821		scp->status &= ~(MOUSE_ENABLED | MOUSE_VISIBLE);
822		mark_all(scp);
823	    }
824	    else
825		return EINVAL;
826	    break;
827
828	case MOUSE_MOVEABS:
829	    scp->mouse_xpos = mouse->u.data.x;
830	    scp->mouse_ypos = mouse->u.data.y;
831	    set_mouse_pos(scp);
832	    break;
833
834	case MOUSE_MOVEREL:
835	    scp->mouse_xpos += mouse->u.data.x;
836	    scp->mouse_ypos += mouse->u.data.y;
837	    set_mouse_pos(scp);
838	    break;
839
840	case MOUSE_GETINFO:
841	    mouse->u.data.x = scp->mouse_xpos;
842	    mouse->u.data.y = scp->mouse_ypos;
843	    mouse->u.data.buttons = scp->mouse_buttons;
844	    break;
845
846	case MOUSE_ACTION:
847	    /* this should maybe only be settable from /dev/consolectl SOS */
848	    /* send out mouse event on /dev/sysmouse */
849	    if (cur_console->status & MOUSE_ENABLED)
850	    	cur_console->status |= MOUSE_VISIBLE;
851	    if ((MOUSE_TTY)->t_state & TS_ISOPEN) {
852		u_char buf[5];
853		int i;
854
855		buf[0] = 0x80 | ((~mouse->u.data.buttons) & 0x07);
856		buf[1] = (mouse->u.data.x & 0x1fe >> 1);
857		buf[3] = (mouse->u.data.x & 0x1ff) - buf[1];
858		buf[2] = -(mouse->u.data.y & 0x1fe >> 1);
859		buf[4] = -(mouse->u.data.y & 0x1ff) - buf[2];
860		for (i=0; i<5; i++)
861	    		(*linesw[(MOUSE_TTY)->t_line].l_rint)(buf[i],MOUSE_TTY);
862	    }
863	    cur_console->mouse_xpos += mouse->u.data.x;
864	    cur_console->mouse_ypos += mouse->u.data.y;
865	    if (cur_console->mouse_signal) {
866		cur_console->mouse_buttons = mouse->u.data.buttons;
867    		/* has controlling process died? */
868		if (cur_console->mouse_proc &&
869		    (cur_console->mouse_proc != pfind(cur_console->mouse_pid))){
870		    	cur_console->mouse_signal = 0;
871			cur_console->mouse_proc = NULL;
872			cur_console->mouse_pid = 0;
873		}
874		else
875		    psignal(cur_console->mouse_proc, cur_console->mouse_signal);
876	    }
877	    else {
878		/* process button presses */
879		if (cur_console->mouse_buttons != mouse->u.data.buttons) {
880		    cur_console->mouse_buttons = mouse->u.data.buttons;
881		    if (!(scp->status & UNKNOWN_MODE)) {
882			if (cur_console->mouse_buttons & LEFT_BUTTON)
883			    mouse_cut_start(cur_console);
884			else
885			    mouse_cut_end(cur_console);
886			if (cur_console->mouse_buttons & RIGHT_BUTTON ||
887			    cur_console->mouse_buttons & MIDDLE_BUTTON)
888			    mouse_paste(cur_console);
889		    }
890		}
891	    }
892	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0)
893		set_mouse_pos(cur_console);
894	    break;
895
896	default:
897	    return EINVAL;
898	}
899	/* make screensaver happy */
900	if (scp == cur_console) {
901	    scrn_time_stamp = time.tv_sec;
902	    if (scrn_blanked) {
903		(*current_saver)(FALSE);
904		mark_all(scp);
905	    }
906	}
907	return 0;
908    }
909
910    case CONS_GETINFO:  	/* get current (virtual) console info */
911    {
912	vid_info_t *ptr = (vid_info_t*)data;
913	if (ptr->size == sizeof(struct vid_info)) {
914	    ptr->m_num = get_scr_num();
915	    ptr->mv_col = scp->xpos;
916	    ptr->mv_row = scp->ypos;
917	    ptr->mv_csz = scp->xsize;
918	    ptr->mv_rsz = scp->ysize;
919	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
920	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
921	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
922	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
923	    ptr->mv_grfc.fore = 0;      /* not supported */
924	    ptr->mv_grfc.back = 0;      /* not supported */
925	    ptr->mv_ovscan = scp->border;
926	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
927	    return 0;
928	}
929	return EINVAL;
930    }
931
932    case CONS_GETVERS:  	/* get version number */
933	*(int*)data = 0x200;    /* version 2.0 */
934	return 0;
935
936    /* VGA TEXT MODES */
937    case SW_VGA_C40x25:
938    case SW_VGA_C80x25: case SW_VGA_M80x25:
939    case SW_VGA_C80x30: case SW_VGA_M80x30:
940    case SW_VGA_C80x50: case SW_VGA_M80x50:
941    case SW_VGA_C80x60: case SW_VGA_M80x60:
942    case SW_B40x25:     case SW_C40x25:
943    case SW_B80x25:     case SW_C80x25:
944    case SW_ENH_B40x25: case SW_ENH_C40x25:
945    case SW_ENH_B80x25: case SW_ENH_C80x25:
946    case SW_ENH_B80x43: case SW_ENH_C80x43:
947
948	if (!crtc_vga || video_mode_ptr == NULL)
949	    return ENXIO;
950	switch (cmd & 0xff) {
951	case M_VGA_C80x60: case M_VGA_M80x60:
952	    if (!(fonts_loaded & FONT_8))
953		return EINVAL;
954	    scp->xsize = 80;
955	    scp->ysize = 60;
956	    break;
957	case M_VGA_C80x50: case M_VGA_M80x50:
958	    if (!(fonts_loaded & FONT_8))
959		return EINVAL;
960	    scp->xsize = 80;
961	    scp->ysize = 50;
962	    break;
963	case M_ENH_B80x43: case M_ENH_C80x43:
964	    if (!(fonts_loaded & FONT_8))
965		return EINVAL;
966	    scp->xsize = 80;
967	    scp->ysize = 43;
968	    break;
969	case M_VGA_C80x30: case M_VGA_M80x30:
970	    scp->xsize = 80;
971	    scp->ysize = 30;
972	    break;
973	default:
974	    if ((cmd & 0xff) > M_VGA_CG320)
975		return EINVAL;
976	    else
977		scp->xsize = *(video_mode_ptr+((cmd&0xff)*64));
978		scp->ysize = *(video_mode_ptr+((cmd&0xff)*64)+1)+1;
979	    break;
980	}
981	scp->mode = cmd & 0xff;
982	free(scp->scr_buf, M_DEVBUF);
983	scp->scr_buf = (u_short *)
984	    malloc(scp->xsize*scp->ysize*sizeof(u_short), M_DEVBUF, M_WAITOK);
985    	scp->cursor_pos = scp->cursor_oldpos =
986	    scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
987    	scp->mouse_pos = scp->mouse_oldpos =
988	    scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
989	    scp->mouse_xpos/8);
990	free(cut_buffer, M_DEVBUF);
991    	cut_buffer = (char *)malloc(scp->xsize*scp->ysize, M_DEVBUF, M_NOWAIT);
992	cut_buffer[0] = 0x00;
993	if (scp == cur_console)
994	    set_mode(scp);
995	scp->status &= ~UNKNOWN_MODE;
996	clear_screen(scp);
997	if (tp->t_winsize.ws_col != scp->xsize
998	    || tp->t_winsize.ws_row != scp->ysize) {
999	    tp->t_winsize.ws_col = scp->xsize;
1000	    tp->t_winsize.ws_row = scp->ysize;
1001	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1002	}
1003	return 0;
1004
1005    /* GRAPHICS MODES */
1006    case SW_BG320:     case SW_BG640:
1007    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
1008    case SW_CG640x350: case SW_ENH_CG640:
1009    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
1010
1011	if (!crtc_vga || video_mode_ptr == NULL)
1012	    return ENXIO;
1013	scp->mode = cmd & 0xFF;
1014	scp->xpixel = (*(video_mode_ptr + (scp->mode*64))) * 8;
1015	scp->ypixel = (*(video_mode_ptr + (scp->mode*64) + 1) + 1) *
1016		     (*(video_mode_ptr + (scp->mode*64) + 2));
1017	if (scp == cur_console)
1018	    set_mode(scp);
1019	scp->status |= UNKNOWN_MODE;    /* graphics mode */
1020	/* clear_graphics();*/
1021
1022	if (tp->t_winsize.ws_xpixel != scp->xpixel
1023	    || tp->t_winsize.ws_ypixel != scp->ypixel) {
1024	    tp->t_winsize.ws_xpixel = scp->xpixel;
1025	    tp->t_winsize.ws_ypixel = scp->ypixel;
1026	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
1027	}
1028	return 0;
1029
1030    case VT_SETMODE:    	/* set screen switcher mode */
1031	bcopy(data, &scp->smode, sizeof(struct vt_mode));
1032	if (scp->smode.mode == VT_PROCESS) {
1033	    scp->proc = p;
1034	    scp->pid = scp->proc->p_pid;
1035	}
1036	return 0;
1037
1038    case VT_GETMODE:    	/* get screen switcher mode */
1039	bcopy(&scp->smode, data, sizeof(struct vt_mode));
1040	return 0;
1041
1042    case VT_RELDISP:    	/* screen switcher ioctl */
1043	switch(*data) {
1044	case VT_FALSE:  	/* user refuses to release screen, abort */
1045	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1046		old_scp->status &= ~SWITCH_WAIT_REL;
1047		switch_in_progress = FALSE;
1048		return 0;
1049	    }
1050	    return EINVAL;
1051
1052	case VT_TRUE:   	/* user has released screen, go on */
1053	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
1054		scp->status &= ~SWITCH_WAIT_REL;
1055		exchange_scr();
1056		if (new_scp->smode.mode == VT_PROCESS) {
1057		    new_scp->status |= SWITCH_WAIT_ACQ;
1058		    psignal(new_scp->proc, new_scp->smode.acqsig);
1059		}
1060		else
1061		    switch_in_progress = FALSE;
1062		return 0;
1063	    }
1064	    return EINVAL;
1065
1066	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
1067	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
1068		scp->status &= ~SWITCH_WAIT_ACQ;
1069		switch_in_progress = FALSE;
1070		return 0;
1071	    }
1072	    return EINVAL;
1073
1074	default:
1075	    return EINVAL;
1076	}
1077	/* NOT REACHED */
1078
1079    case VT_OPENQRY:    	/* return free virtual console */
1080	for (i = 0; i < MAXCONS; i++) {
1081	    tp = VIRTUAL_TTY(i);
1082	    if (!(tp->t_state & TS_ISOPEN)) {
1083		*data = i + 1;
1084		return 0;
1085	    }
1086	}
1087	return EINVAL;
1088
1089    case VT_ACTIVATE:   	/* switch to screen *data */
1090	return switch_scr(scp, (*data) - 1);
1091
1092    case VT_WAITACTIVE: 	/* wait for switch to occur */
1093	if (*data > MAXCONS || *data < 0)
1094	    return EINVAL;
1095	if (minor(dev) == (*data) - 1)
1096	    return 0;
1097	if (*data == 0) {
1098	    if (scp == cur_console)
1099		return 0;
1100	}
1101	else
1102	    scp = console[(*data) - 1];
1103	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
1104			     "waitvt", 0)) == ERESTART) ;
1105	return error;
1106
1107    case VT_GETACTIVE:
1108	*data = get_scr_num()+1;
1109	return 0;
1110
1111    case KDENABIO:      	/* allow io operations */
1112	error = suser(p->p_ucred, &p->p_acflag);
1113	if (error != 0)
1114	    return error;
1115	if (securelevel > 0)
1116	    return EPERM;
1117	fp = (struct trapframe *)p->p_md.md_regs;
1118	fp->tf_eflags |= PSL_IOPL;
1119	return 0;
1120
1121    case KDDISABIO:     	/* disallow io operations (default) */
1122	fp = (struct trapframe *)p->p_md.md_regs;
1123	fp->tf_eflags &= ~PSL_IOPL;
1124	return 0;
1125
1126    case KDSETMODE:     	/* set current mode of this (virtual) console */
1127	switch (*data) {
1128	case KD_TEXT:   	/* switch to TEXT (known) mode */
1129	    /* restore fonts & palette ! */
1130	    if (crtc_vga) {
1131		if (fonts_loaded & FONT_8)
1132		    copy_font(LOAD, FONT_8, font_8);
1133		if (fonts_loaded & FONT_14)
1134		    copy_font(LOAD, FONT_14, font_14);
1135		if (fonts_loaded & FONT_16)
1136		    copy_font(LOAD, FONT_16, font_16);
1137		if (flags & CHAR_CURSOR)
1138		    set_destructive_cursor(scp);
1139		load_palette(palette);
1140	    }
1141	    /* FALL THROUGH */
1142
1143	case KD_TEXT1:  	/* switch to TEXT (known) mode */
1144	    /* no restore fonts & palette */
1145	    if (crtc_vga && video_mode_ptr)
1146		set_mode(scp);
1147	    scp->status &= ~UNKNOWN_MODE;
1148	    clear_screen(scp);
1149	    return 0;
1150
1151	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
1152	    scp->status |= UNKNOWN_MODE;
1153	    return 0;
1154	default:
1155	    return EINVAL;
1156	}
1157	/* NOT REACHED */
1158
1159    case KDGETMODE:     	/* get current mode of this (virtual) console */
1160	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
1161	return 0;
1162
1163    case KDSBORDER:     	/* set border color of this (virtual) console */
1164	if (!crtc_vga)
1165	    return ENXIO;
1166	scp->border = *data;
1167	if (scp == cur_console)
1168	    set_border(scp->border);
1169	return 0;
1170
1171    case KDSKBSTATE:    	/* set keyboard state (locks) */
1172	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
1173	    scp->status &= ~LOCK_KEY_MASK;
1174	    scp->status |= *data;
1175	    if (scp == cur_console)
1176		update_leds(scp->status);
1177	    return 0;
1178	}
1179	return EINVAL;
1180
1181    case KDGKBSTATE:    	/* get keyboard state (locks) */
1182	*data = scp->status & LOCK_KEY_MASK;
1183	return 0;
1184
1185    case KDSETRAD:      	/* set keyboard repeat & delay rates */
1186	if (*data & 0x80)
1187	    return EINVAL;
1188	if (sc_kbdc != NULL)
1189	    set_keyboard(KBDC_SET_TYPEMATIC, *data);
1190	return 0;
1191
1192    case KDSKBMODE:     	/* set keyboard mode */
1193	switch (*data) {
1194	case K_RAW: 		/* switch to RAW scancode mode */
1195	    scp->status |= KBD_RAW_MODE;
1196	    return 0;
1197
1198	case K_XLATE:   	/* switch to XLT ascii mode */
1199	    if (scp == cur_console && scp->status == KBD_RAW_MODE)
1200		shfts = ctls = alts = agrs = metas = 0;
1201	    scp->status &= ~KBD_RAW_MODE;
1202	    return 0;
1203	default:
1204	    return EINVAL;
1205	}
1206	/* NOT REACHED */
1207
1208    case KDGKBMODE:     	/* get keyboard mode */
1209	*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
1210	return 0;
1211
1212    case KDMKTONE:      	/* sound the bell */
1213	if (*(int*)data)
1214	    do_bell(scp, (*(int*)data)&0xffff,
1215		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1216	else
1217	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1218	return 0;
1219
1220    case KIOCSOUND:     	/* make tone (*data) hz */
1221	if (scp == cur_console) {
1222	    if (*(int*)data) {
1223		int pitch = timer_freq / *(int*)data;
1224
1225		/* set command for counter 2, 2 byte write */
1226		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1227		    return EBUSY;
1228
1229		/* set pitch */
1230		outb(TIMER_CNTR2, pitch);
1231		outb(TIMER_CNTR2, (pitch>>8));
1232
1233		/* enable counter 2 output to speaker */
1234		outb(IO_PPI, inb(IO_PPI) | 3);
1235	    }
1236	    else {
1237		/* disable counter 2 output to speaker */
1238		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1239		release_timer2();
1240	    }
1241	}
1242	return 0;
1243
1244    case KDGKBTYPE:     	/* get keyboard type */
1245	*data = 0;  		/* type not known (yet) */
1246	return 0;
1247
1248    case KDSETLED:      	/* set keyboard LED status */
1249	if (*data >= 0 && *data <= LED_MASK) {
1250	    scp->status &= ~LED_MASK;
1251	    scp->status |= *data;
1252	    if (scp == cur_console)
1253		update_leds(scp->status);
1254	    return 0;
1255	}
1256	return EINVAL;
1257
1258    case KDGETLED:      	/* get keyboard LED status */
1259	*data = scp->status & LED_MASK;
1260	return 0;
1261
1262    case GETFKEY:       	/* get functionkey string */
1263	if (*(u_short*)data < n_fkey_tab) {
1264	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1265	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1266		  fkey_tab[ptr->keynum].len);
1267	    ptr->flen = fkey_tab[ptr->keynum].len;
1268	    return 0;
1269	}
1270	else
1271	    return EINVAL;
1272
1273    case SETFKEY:       	/* set functionkey string */
1274	if (*(u_short*)data < n_fkey_tab) {
1275	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1276	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1277		  min(ptr->flen, MAXFK));
1278	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1279	    return 0;
1280	}
1281	else
1282	    return EINVAL;
1283
1284    case GIO_SCRNMAP:   	/* get output translation table */
1285	bcopy(&scr_map, data, sizeof(scr_map));
1286	return 0;
1287
1288    case PIO_SCRNMAP:   	/* set output translation table */
1289	bcopy(data, &scr_map, sizeof(scr_map));
1290	for (i=0; i<sizeof(scr_map); i++)
1291	    scr_rmap[scr_map[i]] = i;
1292	return 0;
1293
1294    case GIO_KEYMAP:    	/* get keyboard translation table */
1295	bcopy(&key_map, data, sizeof(key_map));
1296	return 0;
1297
1298    case PIO_KEYMAP:    	/* set keyboard translation table */
1299	bcopy(data, &key_map, sizeof(key_map));
1300	return 0;
1301
1302    case PIO_FONT8x8:   	/* set 8x8 dot font */
1303	if (!crtc_vga)
1304	    return ENXIO;
1305	bcopy(data, font_8, 8*256);
1306	fonts_loaded |= FONT_8;
1307	copy_font(LOAD, FONT_8, font_8);
1308	if (flags & CHAR_CURSOR)
1309	    set_destructive_cursor(scp);
1310	return 0;
1311
1312    case GIO_FONT8x8:   	/* get 8x8 dot font */
1313	if (!crtc_vga)
1314	    return ENXIO;
1315	if (fonts_loaded & FONT_8) {
1316	    bcopy(font_8, data, 8*256);
1317	    return 0;
1318	}
1319	else
1320	    return ENXIO;
1321
1322    case PIO_FONT8x14:  	/* set 8x14 dot font */
1323	if (!crtc_vga)
1324	    return ENXIO;
1325	bcopy(data, font_14, 14*256);
1326	fonts_loaded |= FONT_14;
1327	copy_font(LOAD, FONT_14, font_14);
1328	if (flags & CHAR_CURSOR)
1329	    set_destructive_cursor(scp);
1330	return 0;
1331
1332    case GIO_FONT8x14:  	/* get 8x14 dot font */
1333	if (!crtc_vga)
1334	    return ENXIO;
1335	if (fonts_loaded & FONT_14) {
1336	    bcopy(font_14, data, 14*256);
1337	    return 0;
1338	}
1339	else
1340	    return ENXIO;
1341
1342    case PIO_FONT8x16:  	/* set 8x16 dot font */
1343	if (!crtc_vga)
1344	    return ENXIO;
1345	bcopy(data, font_16, 16*256);
1346	fonts_loaded |= FONT_16;
1347	copy_font(LOAD, FONT_16, font_16);
1348	if (flags & CHAR_CURSOR)
1349	    set_destructive_cursor(scp);
1350	return 0;
1351
1352    case GIO_FONT8x16:  	/* get 8x16 dot font */
1353	if (!crtc_vga)
1354	    return ENXIO;
1355	if (fonts_loaded & FONT_16) {
1356	    bcopy(font_16, data, 16*256);
1357	    return 0;
1358	}
1359	else
1360	    return ENXIO;
1361    default:
1362	break;
1363    }
1364
1365    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1366    if (error >= 0)
1367	return(error);
1368    error = ttioctl(tp, cmd, data, flag);
1369    if (error >= 0)
1370	return(error);
1371    return(ENOTTY);
1372}
1373
1374static void
1375scstart(struct tty *tp)
1376{
1377    struct clist *rbp;
1378    int s, len;
1379    u_char buf[PCBURST];
1380    scr_stat *scp = get_scr_stat(tp->t_dev);
1381
1382    if (scp->status & SLKED || blink_in_progress)
1383	return; /* XXX who repeats the call when the above flags are cleared? */
1384    s = spltty();
1385    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1386	tp->t_state |= TS_BUSY;
1387	rbp = &tp->t_outq;
1388	while (rbp->c_cc) {
1389	    len = q_to_b(rbp, buf, PCBURST);
1390	    splx(s);
1391	    ansi_put(scp, buf, len);
1392	    s = spltty();
1393	}
1394	tp->t_state &= ~TS_BUSY;
1395	ttwwakeup(tp);
1396    }
1397    splx(s);
1398}
1399
1400static void
1401scmousestart(struct tty *tp)
1402{
1403    struct clist *rbp;
1404    int s;
1405    u_char buf[PCBURST];
1406
1407    s = spltty();
1408    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1409	tp->t_state |= TS_BUSY;
1410	rbp = &tp->t_outq;
1411	while (rbp->c_cc) {
1412	    q_to_b(rbp, buf, PCBURST);
1413	}
1414	tp->t_state &= ~TS_BUSY;
1415	ttwwakeup(tp);
1416    }
1417    splx(s);
1418}
1419
1420void
1421sccnprobe(struct consdev *cp)
1422{
1423    struct isa_device *dvp;
1424
1425    /*
1426     * Take control if we are the highest priority enabled display device.
1427     */
1428    dvp = find_display();
1429    if (dvp == NULL || dvp->id_driver != &scdriver) {
1430	cp->cn_pri = CN_DEAD;
1431	return;
1432    }
1433
1434    /* initialize required fields */
1435    cp->cn_dev = makedev(CDEV_MAJOR, SC_CONSOLE);
1436    cp->cn_pri = CN_INTERNAL;
1437
1438    sc_kbdc = kbdc_open(sc_port);
1439}
1440
1441void
1442sccninit(struct consdev *cp)
1443{
1444    scinit();
1445}
1446
1447void
1448sccnputc(dev_t dev, int c)
1449{
1450    u_char buf[1];
1451    int s;
1452    scr_stat *scp = console[0];
1453    term_stat save = scp->term;
1454
1455    scp->term = kernel_console;
1456    current_default = &kernel_default;
1457    if (scp == cur_console && !(scp->status & UNKNOWN_MODE))
1458	remove_cursor_image(scp);
1459    buf[0] = c;
1460    ansi_put(scp, buf, 1);
1461    kernel_console = scp->term;
1462    current_default = &user_default;
1463    scp->term = save;
1464    s = splclock();
1465    if (scp == cur_console && !(scp->status & UNKNOWN_MODE)) {
1466	if (/* timer not running && */ (scp->start <= scp->end)) {
1467	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1468		   (1 + scp->end - scp->start) * sizeof(u_short));
1469	    scp->start = scp->xsize * scp->ysize;
1470	    scp->end = 0;
1471	}
1472    	scp->cursor_oldpos = scp->cursor_pos;
1473	draw_cursor_image(scp);
1474    }
1475    splx(s);
1476}
1477
1478int
1479sccngetc(dev_t dev)
1480{
1481    int s = spltty();       /* block scintr while we poll */
1482    int c = scgetc(SCGETC_CN);
1483    splx(s);
1484    return(c);
1485}
1486
1487int
1488sccncheckc(dev_t dev)
1489{
1490    int c, s;
1491
1492    s = spltty();
1493    c = scgetc(SCGETC_CN | SCGETC_NONBLOCK);
1494    splx(s);
1495    return(c == NOKEY ? -1 : c);	/* c == -1 can't happen */
1496}
1497
1498static scr_stat
1499*get_scr_stat(dev_t dev)
1500{
1501    int unit = minor(dev);
1502
1503    if (unit == SC_CONSOLE)
1504	return console[0];
1505    if (unit >= MAXCONS || unit < 0)
1506	return(NULL);
1507    return console[unit];
1508}
1509
1510static int
1511get_scr_num()
1512{
1513    int i = 0;
1514
1515    while ((i < MAXCONS) && (cur_console != console[i]))
1516	i++;
1517    return i < MAXCONS ? i : 0;
1518}
1519
1520static void
1521scrn_timer()
1522{
1523    scr_stat *scp = cur_console;
1524    int s = spltty();
1525
1526    /*
1527     * With release 2.1 of the Xaccel server, the keyboard is left
1528     * hanging pretty often. Apparently an interrupt from the
1529     * keyboard is lost, and I don't know why (yet).
1530     * This ugly hack calls scintr if input is ready for the keyboard
1531     * and conveniently hides the problem.			XXX
1532     */
1533    /* Try removing anything stuck in the keyboard controller; whether
1534     * it's a keyboard scan code or mouse data. `scintr()' doesn't
1535     * read the mouse data directly, but `kbdio' routines will, as a
1536     * side effect.
1537     */
1538    if (kbdc_lock(sc_kbdc, TRUE)) {
1539	/*
1540	 * We have seen the lock flag is not set. Let's reset the flag early;
1541	 * otherwise `update_led()' failes which may want the lock
1542	 * during `scintr()'.
1543	 */
1544	kbdc_lock(sc_kbdc, FALSE);
1545	if (kbdc_data_ready(sc_kbdc))
1546	    scintr(0);
1547    }
1548
1549    /* should we just return ? */
1550    if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
1551	timeout((timeout_func_t)scrn_timer, 0, hz/10);
1552	splx(s);
1553	return;
1554    }
1555
1556    if (!scrn_blanked) {
1557	/* update screen image */
1558	if (scp->start <= scp->end) {
1559	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1560		   (1 + scp->end - scp->start) * sizeof(u_short));
1561	}
1562
1563	/* update "pseudo" mouse pointer image */
1564	if ((scp->status & MOUSE_VISIBLE) && crtc_vga) {
1565	    /* did mouse move since last time ? */
1566	    if (scp->status & MOUSE_MOVED) {
1567		/* do we need to remove old mouse pointer image ? */
1568		if (scp->mouse_cut_start != NULL ||
1569		    (scp->mouse_pos-scp->scr_buf) <= scp->start ||
1570		    (scp->mouse_pos+scp->xsize+1-scp->scr_buf) >= scp->end) {
1571		    remove_mouse_image(scp);
1572		}
1573		scp->status &= ~MOUSE_MOVED;
1574		draw_mouse_image(scp);
1575	    }
1576	    else {
1577		/* mouse didn't move, has it been overwritten ? */
1578		if ((scp->mouse_pos+scp->xsize+1-scp->scr_buf) >= scp->start &&
1579		    (scp->mouse_pos - scp->scr_buf) <= scp->end) {
1580		    draw_mouse_image(scp);
1581		}
1582	    }
1583	}
1584
1585	/* update cursor image */
1586	if (scp->status & CURSOR_ENABLED) {
1587	    /* did cursor move since last time ? */
1588	    if (scp->cursor_pos != scp->cursor_oldpos) {
1589		/* do we need to remove old cursor image ? */
1590		if ((scp->cursor_oldpos - scp->scr_buf) < scp->start ||
1591		    ((scp->cursor_oldpos - scp->scr_buf) > scp->end)) {
1592		    remove_cursor_image(scp);
1593		}
1594    		scp->cursor_oldpos = scp->cursor_pos;
1595		draw_cursor_image(scp);
1596	    }
1597	    else {
1598		/* cursor didn't move, has it been overwritten ? */
1599		if (scp->cursor_pos - scp->scr_buf >= scp->start &&
1600		    scp->cursor_pos - scp->scr_buf <= scp->end) {
1601		    	draw_cursor_image(scp);
1602		} else {
1603		    /* if its a blinking cursor, we may have to update it */
1604		    if (flags & BLINK_CURSOR)
1605			draw_cursor_image(scp);
1606		}
1607	    }
1608	    blinkrate++;
1609	}
1610
1611	if (scp->mouse_cut_start != NULL)
1612	    draw_cutmarking(scp);
1613
1614	scp->end = 0;
1615	scp->start = scp->xsize*scp->ysize;
1616    }
1617    if (scrn_blank_time && (time.tv_sec > scrn_time_stamp+scrn_blank_time))
1618	(*current_saver)(TRUE);
1619    timeout((timeout_func_t)scrn_timer, 0, hz/25);
1620    splx(s);
1621}
1622
1623static void
1624clear_screen(scr_stat *scp)
1625{
1626    move_crsr(scp, 0, 0);
1627    scp->cursor_oldpos = scp->cursor_pos;
1628    fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1629	  scp->xsize * scp->ysize);
1630    mark_all(scp);
1631    remove_cutmarking(scp);
1632}
1633
1634static int
1635switch_scr(scr_stat *scp, u_int next_scr)
1636{
1637    if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
1638	switch_in_progress = FALSE;
1639
1640    if (next_scr >= MAXCONS || switch_in_progress ||
1641	(cur_console->smode.mode == VT_AUTO
1642	 && cur_console->status & UNKNOWN_MODE)) {
1643	do_bell(scp, BELL_PITCH, BELL_DURATION);
1644	return EINVAL;
1645    }
1646
1647    /* is the wanted virtual console open ? */
1648    if (next_scr) {
1649	struct tty *tp = VIRTUAL_TTY(next_scr);
1650	if (!(tp->t_state & TS_ISOPEN)) {
1651	    do_bell(scp, BELL_PITCH, BELL_DURATION);
1652	    return EINVAL;
1653	}
1654    }
1655    /* delay switch if actively updating screen */
1656    if (write_in_progress || blink_in_progress) {
1657	delayed_next_scr = next_scr+1;
1658	return 0;
1659    }
1660    switch_in_progress = TRUE;
1661    old_scp = cur_console;
1662    new_scp = console[next_scr];
1663    wakeup((caddr_t)&new_scp->smode);
1664    if (new_scp == old_scp) {
1665	switch_in_progress = FALSE;
1666	delayed_next_scr = FALSE;
1667	return 0;
1668    }
1669
1670    /* has controlling process died? */
1671    if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1672	old_scp->smode.mode = VT_AUTO;
1673    if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1674	new_scp->smode.mode = VT_AUTO;
1675
1676    /* check the modes and switch appropriately */
1677    if (old_scp->smode.mode == VT_PROCESS) {
1678	old_scp->status |= SWITCH_WAIT_REL;
1679	psignal(old_scp->proc, old_scp->smode.relsig);
1680    }
1681    else {
1682	exchange_scr();
1683	if (new_scp->smode.mode == VT_PROCESS) {
1684	    new_scp->status |= SWITCH_WAIT_ACQ;
1685	    psignal(new_scp->proc, new_scp->smode.acqsig);
1686	}
1687	else
1688	    switch_in_progress = FALSE;
1689    }
1690    return 0;
1691}
1692
1693static void
1694exchange_scr(void)
1695{
1696    move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1697    cur_console = new_scp;
1698    if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
1699	if (crtc_vga && video_mode_ptr)
1700	    set_mode(new_scp);
1701    }
1702    move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1703    if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1704	if (flags & CHAR_CURSOR)
1705	    set_destructive_cursor(new_scp);
1706	load_palette(palette);
1707    }
1708    if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1709	shfts = ctls = alts = agrs = metas = 0;
1710    update_leds(new_scp->status);
1711    delayed_next_scr = FALSE;
1712    mark_all(new_scp);
1713}
1714
1715static void
1716scan_esc(scr_stat *scp, u_char c)
1717{
1718    static u_char ansi_col[16] =
1719	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1720    int i, n;
1721    u_short *src, *dst, count;
1722
1723    if (scp->term.esc == 1) {
1724	switch (c) {
1725
1726	case '[':   /* Start ESC [ sequence */
1727	    scp->term.esc = 2;
1728	    scp->term.last_param = -1;
1729	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1730		scp->term.param[i] = 1;
1731	    scp->term.num_param = 0;
1732	    return;
1733
1734	case 'M':   /* Move cursor up 1 line, scroll if at top */
1735	    if (scp->ypos > 0)
1736		move_crsr(scp, scp->xpos, scp->ypos - 1);
1737	    else {
1738		bcopyw(scp->scr_buf, scp->scr_buf + scp->xsize,
1739		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
1740		fillw(scp->term.cur_color | scr_map[0x20],
1741		      scp->scr_buf, scp->xsize);
1742    		mark_all(scp);
1743	    }
1744	    break;
1745#if notyet
1746	case 'Q':
1747	    scp->term.esc = 4;
1748	    break;
1749#endif
1750	case 'c':   /* Clear screen & home */
1751	    clear_screen(scp);
1752	    break;
1753	}
1754    }
1755    else if (scp->term.esc == 2) {
1756	if (c >= '0' && c <= '9') {
1757	    if (scp->term.num_param < MAX_ESC_PAR) {
1758	    if (scp->term.last_param != scp->term.num_param) {
1759		scp->term.last_param = scp->term.num_param;
1760		scp->term.param[scp->term.num_param] = 0;
1761	    }
1762	    else
1763		scp->term.param[scp->term.num_param] *= 10;
1764	    scp->term.param[scp->term.num_param] += c - '0';
1765	    return;
1766	    }
1767	}
1768	scp->term.num_param = scp->term.last_param + 1;
1769	switch (c) {
1770
1771	case ';':
1772	    if (scp->term.num_param < MAX_ESC_PAR)
1773		return;
1774	    break;
1775
1776	case '=':
1777	    scp->term.esc = 3;
1778	    scp->term.last_param = -1;
1779	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1780		scp->term.param[i] = 1;
1781	    scp->term.num_param = 0;
1782	    return;
1783
1784	case 'A':   /* up n rows */
1785	    n = scp->term.param[0]; if (n < 1) n = 1;
1786	    move_crsr(scp, scp->xpos, scp->ypos - n);
1787	    break;
1788
1789	case 'B':   /* down n rows */
1790	    n = scp->term.param[0]; if (n < 1) n = 1;
1791	    move_crsr(scp, scp->xpos, scp->ypos + n);
1792	    break;
1793
1794	case 'C':   /* right n columns */
1795	    n = scp->term.param[0]; if (n < 1) n = 1;
1796	    move_crsr(scp, scp->xpos + n, scp->ypos);
1797	    break;
1798
1799	case 'D':   /* left n columns */
1800	    n = scp->term.param[0]; if (n < 1) n = 1;
1801	    move_crsr(scp, scp->xpos - n, scp->ypos);
1802	    break;
1803
1804	case 'E':   /* cursor to start of line n lines down */
1805	    n = scp->term.param[0]; if (n < 1) n = 1;
1806	    move_crsr(scp, 0, scp->ypos + n);
1807	    break;
1808
1809	case 'F':   /* cursor to start of line n lines up */
1810	    n = scp->term.param[0]; if (n < 1) n = 1;
1811	    move_crsr(scp, 0, scp->ypos - n);
1812	    break;
1813
1814	case 'f':   /* Cursor move */
1815	case 'H':
1816	    if (scp->term.num_param == 0)
1817		move_crsr(scp, 0, 0);
1818	    else if (scp->term.num_param == 2)
1819		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
1820	    break;
1821
1822	case 'J':   /* Clear all or part of display */
1823	    if (scp->term.num_param == 0)
1824		n = 0;
1825	    else
1826		n = scp->term.param[0];
1827	    switch (n) {
1828	    case 0: /* clear form cursor to end of display */
1829		fillw(scp->term.cur_color | scr_map[0x20],
1830		      scp->cursor_pos,
1831		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
1832    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1833    		mark_for_update(scp, scp->xsize * scp->ysize);
1834		break;
1835	    case 1: /* clear from beginning of display to cursor */
1836		fillw(scp->term.cur_color | scr_map[0x20],
1837		      scp->scr_buf,
1838		      scp->cursor_pos - scp->scr_buf);
1839    		mark_for_update(scp, 0);
1840    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1841		break;
1842	    case 2: /* clear entire display */
1843		clear_screen(scp);
1844		break;
1845	    }
1846	    break;
1847
1848	case 'K':   /* Clear all or part of line */
1849	    if (scp->term.num_param == 0)
1850		n = 0;
1851	    else
1852		n = scp->term.param[0];
1853	    switch (n) {
1854	    case 0: /* clear form cursor to end of line */
1855		fillw(scp->term.cur_color | scr_map[0x20],
1856		      scp->cursor_pos,
1857		      scp->xsize - scp->xpos);
1858    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1859    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
1860				scp->xsize - scp->xpos);
1861		break;
1862	    case 1: /* clear from beginning of line to cursor */
1863		fillw(scp->term.cur_color | scr_map[0x20],
1864		      scp->cursor_pos - scp->xpos,
1865		      scp->xpos + 1);
1866    		mark_for_update(scp, scp->ypos * scp->xsize);
1867    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1868		break;
1869	    case 2: /* clear entire line */
1870		fillw(scp->term.cur_color | scr_map[0x20],
1871		      scp->cursor_pos - scp->xpos,
1872		      scp->xsize);
1873    		mark_for_update(scp, scp->ypos * scp->xsize);
1874    		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
1875		break;
1876	    }
1877	    break;
1878
1879	case 'L':   /* Insert n lines */
1880	    n = scp->term.param[0]; if (n < 1) n = 1;
1881	    if (n > scp->ysize - scp->ypos)
1882		n = scp->ysize - scp->ypos;
1883	    src = scp->scr_buf + scp->ypos * scp->xsize;
1884	    dst = src + n * scp->xsize;
1885	    count = scp->ysize - (scp->ypos + n);
1886	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1887	    fillw(scp->term.cur_color | scr_map[0x20], src,
1888		  n * scp->xsize);
1889	    mark_for_update(scp, scp->ypos * scp->xsize);
1890	    mark_for_update(scp, scp->xsize * scp->ysize);
1891	    break;
1892
1893	case 'M':   /* Delete n lines */
1894	    n = scp->term.param[0]; if (n < 1) n = 1;
1895	    if (n > scp->ysize - scp->ypos)
1896		n = scp->ysize - scp->ypos;
1897	    dst = scp->scr_buf + scp->ypos * scp->xsize;
1898	    src = dst + n * scp->xsize;
1899	    count = scp->ysize - (scp->ypos + n);
1900	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1901	    src = dst + count * scp->xsize;
1902	    fillw(scp->term.cur_color | scr_map[0x20], src,
1903		  n * scp->xsize);
1904	    mark_for_update(scp, scp->ypos * scp->xsize);
1905	    mark_for_update(scp, scp->xsize * scp->ysize);
1906	    break;
1907
1908	case 'P':   /* Delete n chars */
1909	    n = scp->term.param[0]; if (n < 1) n = 1;
1910	    if (n > scp->xsize - scp->xpos)
1911		n = scp->xsize - scp->xpos;
1912	    dst = scp->cursor_pos;
1913	    src = dst + n;
1914	    count = scp->xsize - (scp->xpos + n);
1915	    bcopyw(src, dst, count * sizeof(u_short));
1916	    src = dst + count;
1917	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1918	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1919	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1920	    break;
1921
1922	case '@':   /* Insert n chars */
1923	    n = scp->term.param[0]; if (n < 1) n = 1;
1924	    if (n > scp->xsize - scp->xpos)
1925		n = scp->xsize - scp->xpos;
1926	    src = scp->cursor_pos;
1927	    dst = src + n;
1928	    count = scp->xsize - (scp->xpos + n);
1929	    bcopyw(src, dst, count * sizeof(u_short));
1930	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1931	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1932	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1933	    break;
1934
1935	case 'S':   /* scroll up n lines */
1936	    n = scp->term.param[0]; if (n < 1)  n = 1;
1937	    if (n > scp->ysize)
1938		n = scp->ysize;
1939	    bcopyw(scp->scr_buf + (scp->xsize * n),
1940		   scp->scr_buf,
1941		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
1942	    fillw(scp->term.cur_color | scr_map[0x20],
1943		  scp->scr_buf + scp->xsize * (scp->ysize - n),
1944		  scp->xsize * n);
1945    	    mark_all(scp);
1946	    break;
1947
1948	case 'T':   /* scroll down n lines */
1949	    n = scp->term.param[0]; if (n < 1)  n = 1;
1950	    if (n > scp->ysize)
1951		n = scp->ysize;
1952	    bcopyw(scp->scr_buf,
1953		  scp->scr_buf + (scp->xsize * n),
1954		  scp->xsize * (scp->ysize - n) *
1955		  sizeof(u_short));
1956	    fillw(scp->term.cur_color | scr_map[0x20],
1957		  scp->scr_buf, scp->xsize * n);
1958    	    mark_all(scp);
1959	    break;
1960
1961	case 'X':   /* erase n characters in line */
1962	    n = scp->term.param[0]; if (n < 1)  n = 1;
1963	    if (n > scp->xsize - scp->xpos)
1964		n = scp->xsize - scp->xpos;
1965	    fillw(scp->term.cur_color | scr_map[0x20],
1966		  scp->cursor_pos, n);
1967	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1968	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
1969	    break;
1970
1971	case 'Z':   /* move n tabs backwards */
1972	    n = scp->term.param[0]; if (n < 1)  n = 1;
1973	    if ((i = scp->xpos & 0xf8) == scp->xpos)
1974		i -= 8*n;
1975	    else
1976		i -= 8*(n-1);
1977	    if (i < 0)
1978		i = 0;
1979	    move_crsr(scp, i, scp->ypos);
1980	    break;
1981
1982	case '`':   /* move cursor to column n */
1983	    n = scp->term.param[0]; if (n < 1)  n = 1;
1984	    move_crsr(scp, n - 1, scp->ypos);
1985	    break;
1986
1987	case 'a':   /* move cursor n columns to the right */
1988	    n = scp->term.param[0]; if (n < 1)  n = 1;
1989	    move_crsr(scp, scp->xpos + n, scp->ypos);
1990	    break;
1991
1992	case 'd':   /* move cursor to row n */
1993	    n = scp->term.param[0]; if (n < 1)  n = 1;
1994	    move_crsr(scp, scp->xpos, n - 1);
1995	    break;
1996
1997	case 'e':   /* move cursor n rows down */
1998	    n = scp->term.param[0]; if (n < 1)  n = 1;
1999	    move_crsr(scp, scp->xpos, scp->ypos + n);
2000	    break;
2001
2002	case 'm':   /* change attribute */
2003	    if (scp->term.num_param == 0) {
2004		scp->term.attr_mask = NORMAL_ATTR;
2005		scp->term.cur_attr =
2006		    scp->term.cur_color = scp->term.std_color;
2007		break;
2008	    }
2009	    for (i = 0; i < scp->term.num_param; i++) {
2010		switch (n = scp->term.param[i]) {
2011		case 0: /* back to normal */
2012		    scp->term.attr_mask = NORMAL_ATTR;
2013		    scp->term.cur_attr =
2014			scp->term.cur_color = scp->term.std_color;
2015		    break;
2016		case 1: /* bold */
2017		    scp->term.attr_mask |= BOLD_ATTR;
2018		    scp->term.cur_attr = mask2attr(&scp->term);
2019		    break;
2020		case 4: /* underline */
2021		    scp->term.attr_mask |= UNDERLINE_ATTR;
2022		    scp->term.cur_attr = mask2attr(&scp->term);
2023		    break;
2024		case 5: /* blink */
2025		    scp->term.attr_mask |= BLINK_ATTR;
2026		    scp->term.cur_attr = mask2attr(&scp->term);
2027		    break;
2028		case 7: /* reverse video */
2029		    scp->term.attr_mask |= REVERSE_ATTR;
2030		    scp->term.cur_attr = mask2attr(&scp->term);
2031		    break;
2032		case 30: case 31: /* set fg color */
2033		case 32: case 33: case 34:
2034		case 35: case 36: case 37:
2035		    scp->term.attr_mask |= FOREGROUND_CHANGED;
2036		    scp->term.cur_color =
2037			(scp->term.cur_color&0xF000) | (ansi_col[(n-30)&7]<<8);
2038		    scp->term.cur_attr = mask2attr(&scp->term);
2039		    break;
2040		case 40: case 41: /* set bg color */
2041		case 42: case 43: case 44:
2042		case 45: case 46: case 47:
2043		    scp->term.attr_mask |= BACKGROUND_CHANGED;
2044		    scp->term.cur_color =
2045			(scp->term.cur_color&0x0F00) | (ansi_col[(n-40)&7]<<12);
2046		    scp->term.cur_attr = mask2attr(&scp->term);
2047		    break;
2048		}
2049	    }
2050	    break;
2051
2052	case 'x':
2053	    if (scp->term.num_param == 0)
2054		n = 0;
2055	    else
2056		n = scp->term.param[0];
2057	    switch (n) {
2058	    case 0:     /* reset attributes */
2059		scp->term.attr_mask = NORMAL_ATTR;
2060		scp->term.cur_attr =
2061		    scp->term.cur_color = scp->term.std_color =
2062		    current_default->std_color;
2063		scp->term.rev_color = current_default->rev_color;
2064		break;
2065	    case 1:     /* set ansi background */
2066		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2067		scp->term.cur_color = scp->term.std_color =
2068		    (scp->term.std_color & 0x0F00) |
2069		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2070		scp->term.cur_attr = mask2attr(&scp->term);
2071		break;
2072	    case 2:     /* set ansi foreground */
2073		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2074		scp->term.cur_color = scp->term.std_color =
2075		    (scp->term.std_color & 0xF000) |
2076		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2077		scp->term.cur_attr = mask2attr(&scp->term);
2078		break;
2079	    case 3:     /* set ansi attribute directly */
2080		scp->term.attr_mask &= ~(FOREGROUND_CHANGED|BACKGROUND_CHANGED);
2081		scp->term.cur_color = scp->term.std_color =
2082		    (scp->term.param[1]&0xFF)<<8;
2083		scp->term.cur_attr = mask2attr(&scp->term);
2084		break;
2085	    case 5:     /* set ansi reverse video background */
2086		scp->term.rev_color =
2087		    (scp->term.rev_color & 0x0F00) |
2088		    (ansi_col[(scp->term.param[1])&0x0F]<<12);
2089		scp->term.cur_attr = mask2attr(&scp->term);
2090		break;
2091	    case 6:     /* set ansi reverse video foreground */
2092		scp->term.rev_color =
2093		    (scp->term.rev_color & 0xF000) |
2094		    (ansi_col[(scp->term.param[1])&0x0F]<<8);
2095		scp->term.cur_attr = mask2attr(&scp->term);
2096		break;
2097	    case 7:     /* set ansi reverse video directly */
2098		scp->term.rev_color =
2099		    (scp->term.param[1]&0xFF)<<8;
2100		scp->term.cur_attr = mask2attr(&scp->term);
2101		break;
2102	    }
2103	    break;
2104
2105	case 'z':   /* switch to (virtual) console n */
2106	    if (scp->term.num_param == 1)
2107		switch_scr(scp, scp->term.param[0]);
2108	    break;
2109	}
2110    }
2111    else if (scp->term.esc == 3) {
2112	if (c >= '0' && c <= '9') {
2113	    if (scp->term.num_param < MAX_ESC_PAR) {
2114	    if (scp->term.last_param != scp->term.num_param) {
2115		scp->term.last_param = scp->term.num_param;
2116		scp->term.param[scp->term.num_param] = 0;
2117	    }
2118	    else
2119		scp->term.param[scp->term.num_param] *= 10;
2120	    scp->term.param[scp->term.num_param] += c - '0';
2121	    return;
2122	    }
2123	}
2124	scp->term.num_param = scp->term.last_param + 1;
2125	switch (c) {
2126
2127	case ';':
2128	    if (scp->term.num_param < MAX_ESC_PAR)
2129		return;
2130	    break;
2131
2132	case 'A':   /* set display border color */
2133	    if (scp->term.num_param == 1)
2134		scp->border=scp->term.param[0] & 0xff;
2135		if (scp == cur_console)
2136		    set_border(scp->border);
2137	    break;
2138
2139	case 'B':   /* set bell pitch and duration */
2140	    if (scp->term.num_param == 2) {
2141		scp->bell_pitch = scp->term.param[0];
2142		scp->bell_duration = scp->term.param[1]*10;
2143	    }
2144	    break;
2145
2146	case 'C':   /* set cursor type & shape */
2147	    if (scp->term.num_param == 1) {
2148		if (scp->term.param[0] & 0x01)
2149		    flags |= BLINK_CURSOR;
2150		else
2151		    flags &= ~BLINK_CURSOR;
2152		if (scp->term.param[0] & 0x02) {
2153		    flags |= CHAR_CURSOR;
2154		    set_destructive_cursor(scp);
2155		} else
2156		    flags &= ~CHAR_CURSOR;
2157	    }
2158	    else if (scp->term.num_param == 2) {
2159		scp->cursor_start = scp->term.param[0] & 0x1F;
2160		scp->cursor_end = scp->term.param[1] & 0x1F;
2161		if (flags & CHAR_CURSOR)
2162			set_destructive_cursor(scp);
2163	    }
2164	    break;
2165
2166	case 'F':   /* set ansi foreground */
2167	    if (scp->term.num_param == 1) {
2168		scp->term.attr_mask &= ~FOREGROUND_CHANGED;
2169		scp->term.cur_color = scp->term.std_color =
2170		    (scp->term.std_color & 0xF000)
2171		    | ((scp->term.param[0] & 0x0F) << 8);
2172		scp->term.cur_attr = mask2attr(&scp->term);
2173	    }
2174	    break;
2175
2176	case 'G':   /* set ansi background */
2177	    if (scp->term.num_param == 1) {
2178		scp->term.attr_mask &= ~BACKGROUND_CHANGED;
2179		scp->term.cur_color = scp->term.std_color =
2180		    (scp->term.std_color & 0x0F00)
2181		    | ((scp->term.param[0] & 0x0F) << 12);
2182		scp->term.cur_attr = mask2attr(&scp->term);
2183	    }
2184	    break;
2185
2186	case 'H':   /* set ansi reverse video foreground */
2187	    if (scp->term.num_param == 1) {
2188		scp->term.rev_color =
2189		    (scp->term.rev_color & 0xF000)
2190		    | ((scp->term.param[0] & 0x0F) << 8);
2191		scp->term.cur_attr = mask2attr(&scp->term);
2192	    }
2193	    break;
2194
2195	case 'I':   /* set ansi reverse video background */
2196	    if (scp->term.num_param == 1) {
2197		scp->term.rev_color =
2198		    (scp->term.rev_color & 0x0F00)
2199		    | ((scp->term.param[0] & 0x0F) << 12);
2200		scp->term.cur_attr = mask2attr(&scp->term);
2201	    }
2202	    break;
2203	}
2204    }
2205    scp->term.esc = 0;
2206}
2207
2208static void
2209ansi_put(scr_stat *scp, u_char *buf, int len)
2210{
2211    u_char *ptr = buf;
2212
2213    /* make screensaver happy */
2214    if (scp == cur_console) {
2215	scrn_time_stamp = time.tv_sec;
2216	if (scrn_blanked) {
2217	    (*current_saver)(FALSE);
2218	    mark_all(scp);
2219	}
2220    }
2221    write_in_progress++;
2222outloop:
2223    if (scp->term.esc) {
2224	scan_esc(scp, *ptr++);
2225	len--;
2226    }
2227    else if (PRINTABLE(*ptr)) {     /* Print only printables */
2228 	int cnt = len <= (scp->xsize-scp->xpos) ? len : (scp->xsize-scp->xpos);
2229 	u_short cur_attr = scp->term.cur_attr;
2230 	u_short *cursor_pos = scp->cursor_pos;
2231	do {
2232	    /*
2233	     * gcc-2.6.3 generates poor (un)sign extension code.  Casting the
2234	     * pointers in the following to volatile should have no effect,
2235	     * but in fact speeds up this inner loop from 26 to 18 cycles
2236	     * (+ cache misses) on i486's.
2237	     */
2238#define	UCVP(ucp)	((u_char volatile *)(ucp))
2239	    *cursor_pos++ = UCVP(scr_map)[*UCVP(ptr)] | cur_attr;
2240	    ptr++;
2241	    cnt--;
2242	} while (cnt && PRINTABLE(*ptr));
2243	len -= (cursor_pos - scp->cursor_pos);
2244	scp->xpos += (cursor_pos - scp->cursor_pos);
2245	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2246	mark_for_update(scp, cursor_pos - scp->scr_buf);
2247	scp->cursor_pos = cursor_pos;
2248	if (scp->xpos >= scp->xsize) {
2249	    scp->xpos = 0;
2250	    scp->ypos++;
2251	}
2252    }
2253    else  {
2254	switch(*ptr) {
2255	case 0x07:
2256	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
2257	    break;
2258
2259	case 0x08:      /* non-destructive backspace */
2260	    if (scp->cursor_pos > scp->scr_buf) {
2261	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2262		scp->cursor_pos--;
2263	    	mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2264		if (scp->xpos > 0)
2265		    scp->xpos--;
2266		else {
2267		    scp->xpos += scp->xsize - 1;
2268		    scp->ypos--;
2269		}
2270	    }
2271	    break;
2272
2273	case 0x09:  /* non-destructive tab */
2274	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2275	    scp->cursor_pos += (8 - scp->xpos % 8u);
2276	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2277	    if ((scp->xpos += (8 - scp->xpos % 8u)) >= scp->xsize) {
2278	        scp->xpos = 0;
2279	        scp->ypos++;
2280	    }
2281	    break;
2282
2283	case 0x0a:  /* newline, same pos */
2284	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2285	    scp->cursor_pos += scp->xsize;
2286	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2287	    scp->ypos++;
2288	    break;
2289
2290	case 0x0c:  /* form feed, clears screen */
2291	    clear_screen(scp);
2292	    break;
2293
2294	case 0x0d:  /* return, return to pos 0 */
2295	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2296	    scp->cursor_pos -= scp->xpos;
2297	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
2298	    scp->xpos = 0;
2299	    break;
2300
2301	case 0x1b:  /* start escape sequence */
2302	    scp->term.esc = 1;
2303	    scp->term.num_param = 0;
2304	    break;
2305	}
2306	ptr++; len--;
2307    }
2308    /* do we have to scroll ?? */
2309    if (scp->cursor_pos >= scp->scr_buf + scp->ysize * scp->xsize) {
2310	remove_cutmarking(scp);
2311	if (scp->history) {
2312	    bcopyw(scp->scr_buf, scp->history_head,
2313		   scp->xsize * sizeof(u_short));
2314	    scp->history_head += scp->xsize;
2315	    if (scp->history_head + scp->xsize >
2316		scp->history + scp->history_size)
2317		scp->history_head = scp->history;
2318	}
2319	bcopyw(scp->scr_buf + scp->xsize, scp->scr_buf,
2320	       scp->xsize * (scp->ysize - 1) * sizeof(u_short));
2321	fillw(scp->term.cur_color | scr_map[0x20],
2322	      scp->scr_buf + scp->xsize * (scp->ysize - 1),
2323	      scp->xsize);
2324	scp->cursor_pos -= scp->xsize;
2325	scp->ypos--;
2326    	mark_all(scp);
2327    }
2328    if (len)
2329	goto outloop;
2330    write_in_progress--;
2331    if (delayed_next_scr)
2332	switch_scr(scp, delayed_next_scr - 1);
2333}
2334
2335static void
2336scinit(void)
2337{
2338    u_short volatile *cp;
2339    u_short was;
2340    u_int hw_cursor;
2341    u_int i;
2342
2343    if (init_done != COLD)
2344	return;
2345    init_done = WARM;
2346    /*
2347     * Finish defaulting crtc variables for a mono screen.  Crtat is a
2348     * bogus common variable so that it can be shared with pcvt, so it
2349     * can't be statically initialized.  XXX.
2350     */
2351    Crtat = (u_short *)MONO_BUF;
2352    /*
2353     * If CGA memory seems to work, switch to color.
2354     */
2355    cp = (u_short *)CGA_BUF;
2356    was = *cp;
2357    *cp = (u_short) 0xA55A;
2358    if (*cp == 0xA55A) {
2359	Crtat = (u_short *)CGA_BUF;
2360	crtc_addr = COLOR_BASE;
2361    }
2362    *cp = was;
2363
2364    /*
2365     * Ensure a zero start address.  This is mainly to recover after
2366     * switching from pcvt using userconfig().  The registers are w/o
2367     * for old hardware so it's too hard to relocate the active screen
2368     * memory.
2369     */
2370    outb(crtc_addr, 12);
2371    outb(crtc_addr + 1, 0);
2372    outb(crtc_addr, 13);
2373    outb(crtc_addr + 1, 0);
2374
2375    /* extract cursor location */
2376    outb(crtc_addr, 14);
2377    hw_cursor = inb(crtc_addr + 1) << 8;
2378    outb(crtc_addr, 15);
2379    hw_cursor |= inb(crtc_addr + 1);
2380
2381    /*
2382     * Validate cursor location.  It may be off the screen.  Then we must
2383     * not use it for the initial buffer offset.
2384     */
2385    if (hw_cursor >= ROW * COL)
2386	hw_cursor = (ROW - 1) * COL;
2387
2388    /* move hardware cursor out of the way */
2389    outb(crtc_addr, 14);
2390    outb(crtc_addr + 1, 0xff);
2391    outb(crtc_addr, 15);
2392    outb(crtc_addr + 1, 0xff);
2393
2394    /* is this a VGA or higher ? */
2395    outb(crtc_addr, 7);
2396    if (inb(crtc_addr) == 7) {
2397	u_long  pa;
2398	u_long  segoff;
2399
2400	crtc_vga = TRUE;
2401
2402	/* Get the BIOS video mode pointer */
2403	segoff = *(u_long *)pa_to_va(0x4a8);
2404	pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2405	if (ISMAPPED(pa, sizeof(u_long))) {
2406	    segoff = *(u_long *)pa_to_va(pa);
2407	    pa = (((segoff & 0xffff0000) >> 12) + (segoff & 0xffff));
2408	    if (ISMAPPED(pa, 64))
2409		video_mode_ptr = (char *)pa_to_va(pa);
2410	}
2411    }
2412    current_default = &user_default;
2413    console[0] = &main_console;
2414    init_scp(console[0]);
2415    cur_console = console[0];
2416
2417    /* copy screen to temporary buffer */
2418    bcopyw(Crtat, sc_buffer,
2419	   console[0]->xsize * console[0]->ysize * sizeof(u_short));
2420
2421    /* Save font and palette if VGA */
2422    if (crtc_vga) {
2423	copy_font(SAVE, FONT_16, font_16);
2424	fonts_loaded = FONT_16;
2425	save_palette();
2426    }
2427
2428    console[0]->scr_buf = console[0]->mouse_pos = sc_buffer;
2429    console[0]->cursor_pos = console[0]->cursor_oldpos = sc_buffer + hw_cursor;
2430    console[0]->xpos = hw_cursor % COL;
2431    console[0]->ypos = hw_cursor / COL;
2432    for (i=1; i<MAXCONS; i++)
2433	console[i] = NULL;
2434    kernel_console.esc = 0;
2435    kernel_console.attr_mask = NORMAL_ATTR;
2436    kernel_console.cur_attr =
2437	kernel_console.cur_color = kernel_console.std_color =
2438	kernel_default.std_color;
2439    kernel_console.rev_color = kernel_default.rev_color;
2440
2441    /* initialize mapscrn arrays to a one to one map */
2442    for (i=0; i<sizeof(scr_map); i++) {
2443	scr_map[i] = scr_rmap[i] = i;
2444    }
2445
2446#ifdef SC_SPLASH_SCREEN
2447    /*
2448     * Now put up a graphics image, and maybe cycle a
2449     * couble of palette entries for simple animation.
2450     */
2451    toggle_splash_screen(cur_console);
2452#endif
2453}
2454
2455static scr_stat
2456*alloc_scp()
2457{
2458    scr_stat *scp;
2459
2460    scp = (scr_stat *)malloc(sizeof(scr_stat), M_DEVBUF, M_WAITOK);
2461    init_scp(scp);
2462    scp->scr_buf = scp->cursor_pos = scp->cursor_oldpos =
2463	(u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
2464			  M_DEVBUF, M_WAITOK);
2465    scp->mouse_pos = scp->mouse_oldpos =
2466	scp->scr_buf + ((scp->mouse_ypos/scp->font_size)*scp->xsize +
2467			scp->mouse_xpos/8);
2468    scp->history_head = scp->history_pos = scp->history =
2469	(u_short *)malloc(scp->history_size*sizeof(u_short),
2470			  M_DEVBUF, M_WAITOK);
2471    bzero(scp->history_head, scp->history_size*sizeof(u_short));
2472/* SOS
2473    if (crtc_vga && video_mode_ptr)
2474	set_mode(scp);
2475*/
2476    clear_screen(scp);
2477    return scp;
2478}
2479
2480static void
2481init_scp(scr_stat *scp)
2482{
2483    if (crtc_vga)
2484	if (crtc_addr == MONO_BASE)
2485	    scp->mode = M_VGA_M80x25;
2486	else
2487	    scp->mode = M_VGA_C80x25;
2488    else
2489	if (crtc_addr == MONO_BASE)
2490	    scp->mode = M_B80x25;
2491	else
2492	    scp->mode = M_C80x25;
2493
2494    scp->font_size = FONT_16;
2495    scp->xsize = COL;
2496    scp->ysize = ROW;
2497    scp->start = scp->xsize * scp->ysize;
2498    scp->end = 0;
2499    scp->term.esc = 0;
2500    scp->term.attr_mask = NORMAL_ATTR;
2501    scp->term.cur_attr =
2502	scp->term.cur_color = scp->term.std_color =
2503	current_default->std_color;
2504    scp->term.rev_color = current_default->rev_color;
2505    scp->border = BG_BLACK;
2506    scp->cursor_start = *(char *)pa_to_va(0x461);
2507    scp->cursor_end = *(char *)pa_to_va(0x460);
2508    scp->mouse_xpos = scp->xsize*8/2;
2509    scp->mouse_ypos = scp->ysize*scp->font_size/2;
2510    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
2511    scp->mouse_signal = 0;
2512    scp->mouse_pid = 0;
2513    scp->mouse_proc = NULL;
2514    scp->bell_pitch = BELL_PITCH;
2515    scp->bell_duration = BELL_DURATION;
2516    scp->status = (*(char *)pa_to_va(0x417) & 0x20) ? NLKED : 0;
2517    scp->status |= CURSOR_ENABLED;
2518    scp->pid = 0;
2519    scp->proc = NULL;
2520    scp->smode.mode = VT_AUTO;
2521    scp->history_head = scp->history_pos = scp->history = NULL;
2522    scp->history_size = HISTORY_SIZE;
2523}
2524
2525static u_char
2526*get_fstr(u_int c, u_int *len)
2527{
2528    u_int i;
2529
2530    if (!(c & FKEY))
2531	return(NULL);
2532    i = (c & 0xFF) - F_FN;
2533    if (i > n_fkey_tab)
2534	return(NULL);
2535    *len = fkey_tab[i].len;
2536    return(fkey_tab[i].str);
2537}
2538
2539static void
2540history_to_screen(scr_stat *scp)
2541{
2542    int i;
2543
2544    for (i=0; i<scp->ysize; i++)
2545	bcopyw(scp->history + (((scp->history_pos - scp->history) +
2546	       scp->history_size-((i+1)*scp->xsize))%scp->history_size),
2547	       scp->scr_buf + (scp->xsize * (scp->ysize-1 - i)),
2548	       scp->xsize * sizeof(u_short));
2549    mark_all(scp);
2550}
2551
2552static int
2553history_up_line(scr_stat *scp)
2554{
2555    if (WRAPHIST(scp, scp->history_pos, -(scp->xsize*scp->ysize)) !=
2556	scp->history_head) {
2557	scp->history_pos = WRAPHIST(scp, scp->history_pos, -scp->xsize);
2558	history_to_screen(scp);
2559	return 0;
2560    }
2561    else
2562	return -1;
2563}
2564
2565static int
2566history_down_line(scr_stat *scp)
2567{
2568    if (scp->history_pos != scp->history_head) {
2569	scp->history_pos = WRAPHIST(scp, scp->history_pos, scp->xsize);
2570	history_to_screen(scp);
2571	return 0;
2572    }
2573    else
2574	return -1;
2575}
2576
2577/*
2578 * scgetc(flags) - get character from keyboard.
2579 * If flags & SCGETC_CN, then avoid harmful side effects.
2580 * If flags & SCGETC_NONBLOCK, then wait until a key is pressed, else
2581 * return NOKEY if there is nothing there.
2582 */
2583static u_int
2584scgetc(u_int flags)
2585{
2586    struct key_t *key;
2587    u_char scancode, keycode;
2588    u_int state, action;
2589    int c;
2590    static u_char esc_flag = 0, compose = 0;
2591    static u_int chr = 0;
2592
2593next_code:
2594    /* first see if there is something in the keyboard port */
2595    if (flags & SCGETC_NONBLOCK) {
2596	c = read_kbd_data_no_wait(sc_kbdc);
2597	if (c == -1)
2598	    return(NOKEY);
2599    } else {
2600	do {
2601	    c = read_kbd_data(sc_kbdc);
2602	} while(c == -1);
2603    }
2604    scancode = (u_char)c;
2605
2606    /* do the /dev/random device a favour */
2607    if (!(flags & SCGETC_CN))
2608	add_keyboard_randomness(scancode);
2609
2610    if (cur_console->status & KBD_RAW_MODE)
2611	return scancode;
2612
2613    keycode = scancode & 0x7F;
2614    switch (esc_flag) {
2615    case 0x00:      /* normal scancode */
2616	switch(scancode) {
2617	case 0xB8:  /* left alt (compose key) */
2618	    if (compose) {
2619		compose = 0;
2620		if (chr > 255) {
2621		    do_bell(cur_console,
2622			BELL_PITCH, BELL_DURATION);
2623		    chr = 0;
2624		}
2625	    }
2626	    break;
2627	case 0x38:
2628	    if (!compose) {
2629		compose = 1;
2630		chr = 0;
2631	    }
2632	    break;
2633	case 0xE0:
2634	case 0xE1:
2635	    esc_flag = scancode;
2636	    goto next_code;
2637	}
2638	break;
2639    case 0xE0:      /* 0xE0 prefix */
2640	esc_flag = 0;
2641	switch (keycode) {
2642	case 0x1C:  /* right enter key */
2643	    keycode = 0x59;
2644	    break;
2645	case 0x1D:  /* right ctrl key */
2646	    keycode = 0x5A;
2647	    break;
2648	case 0x35:  /* keypad divide key */
2649	    keycode = 0x5B;
2650	    break;
2651	case 0x37:  /* print scrn key */
2652	    keycode = 0x5C;
2653	    break;
2654	case 0x38:  /* right alt key (alt gr) */
2655	    keycode = 0x5D;
2656	    break;
2657	case 0x47:  /* grey home key */
2658	    keycode = 0x5E;
2659	    break;
2660	case 0x48:  /* grey up arrow key */
2661	    keycode = 0x5F;
2662	    break;
2663	case 0x49:  /* grey page up key */
2664	    keycode = 0x60;
2665	    break;
2666	case 0x4B:  /* grey left arrow key */
2667	    keycode = 0x61;
2668	    break;
2669	case 0x4D:  /* grey right arrow key */
2670	    keycode = 0x62;
2671	    break;
2672	case 0x4F:  /* grey end key */
2673	    keycode = 0x63;
2674	    break;
2675	case 0x50:  /* grey down arrow key */
2676	    keycode = 0x64;
2677	    break;
2678	case 0x51:  /* grey page down key */
2679	    keycode = 0x65;
2680	    break;
2681	case 0x52:  /* grey insert key */
2682	    keycode = 0x66;
2683	    break;
2684	case 0x53:  /* grey delete key */
2685	    keycode = 0x67;
2686	    break;
2687
2688	/* the following 3 are only used on the MS "Natural" keyboard */
2689	case 0x5b:  /* left Window key */
2690	    keycode = 0x69;
2691	    break;
2692	case 0x5c:  /* right Window key */
2693	    keycode = 0x6a;
2694	    break;
2695	case 0x5d:  /* menu key */
2696	    keycode = 0x6b;
2697	    break;
2698	default:    /* ignore everything else */
2699	    goto next_code;
2700	}
2701	break;
2702    case 0xE1:      /* 0xE1 prefix */
2703	esc_flag = 0;
2704	if (keycode == 0x1D)
2705	    esc_flag = 0x1D;
2706	goto next_code;
2707	/* NOT REACHED */
2708    case 0x1D:      /* pause / break */
2709	esc_flag = 0;
2710	if (keycode != 0x45)
2711	    goto next_code;
2712	keycode = 0x68;
2713	break;
2714    }
2715
2716    /* if scroll-lock pressed allow history browsing */
2717    if (cur_console->history && cur_console->status & SLKED) {
2718	int i;
2719
2720	cur_console->status &= ~CURSOR_ENABLED;
2721	if (!(cur_console->status & BUFFER_SAVED)) {
2722	    cur_console->status |= BUFFER_SAVED;
2723	    cur_console->history_save = cur_console->history_head;
2724
2725	    /* copy screen into top of history buffer */
2726	    for (i=0; i<cur_console->ysize; i++) {
2727		bcopyw(cur_console->scr_buf + (cur_console->xsize * i),
2728		       cur_console->history_head,
2729		       cur_console->xsize * sizeof(u_short));
2730		cur_console->history_head += cur_console->xsize;
2731		if (cur_console->history_head + cur_console->xsize >
2732		    cur_console->history + cur_console->history_size)
2733		    cur_console->history_head=cur_console->history;
2734	    }
2735	    cur_console->history_pos = cur_console->history_head;
2736	    history_to_screen(cur_console);
2737	}
2738	switch (scancode) {
2739	case 0x47:  /* home key */
2740	    cur_console->history_pos = cur_console->history_head;
2741	    history_to_screen(cur_console);
2742	    goto next_code;
2743
2744	case 0x4F:  /* end key */
2745	    cur_console->history_pos =
2746		WRAPHIST(cur_console, cur_console->history_head,
2747			 cur_console->xsize*cur_console->ysize);
2748	    history_to_screen(cur_console);
2749	    goto next_code;
2750
2751	case 0x48:  /* up arrow key */
2752	    if (history_up_line(cur_console))
2753		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2754	    goto next_code;
2755
2756	case 0x50:  /* down arrow key */
2757	    if (history_down_line(cur_console))
2758		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2759	    goto next_code;
2760
2761	case 0x49:  /* page up key */
2762	    for (i=0; i<cur_console->ysize; i++)
2763	    if (history_up_line(cur_console)) {
2764		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2765		break;
2766	    }
2767	    goto next_code;
2768
2769	case 0x51:  /* page down key */
2770	    for (i=0; i<cur_console->ysize; i++)
2771	    if (history_down_line(cur_console)) {
2772		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2773		break;
2774	    }
2775	    goto next_code;
2776	}
2777    }
2778
2779    if (compose) {
2780	switch (scancode) {
2781	/* key pressed process it */
2782	case 0x47: case 0x48: case 0x49:    /* keypad 7,8,9 */
2783	    chr = (scancode - 0x40) + chr*10;
2784	    goto next_code;
2785	case 0x4B: case 0x4C: case 0x4D:    /* keypad 4,5,6 */
2786	    chr = (scancode - 0x47) + chr*10;
2787	    goto next_code;
2788	case 0x4F: case 0x50: case 0x51:    /* keypad 1,2,3 */
2789	    chr = (scancode - 0x4E) + chr*10;
2790	    goto next_code;
2791	case 0x52:              /* keypad 0 */
2792	    chr *= 10;
2793	    goto next_code;
2794
2795	/* key release, no interest here */
2796	case 0xC7: case 0xC8: case 0xC9:    /* keypad 7,8,9 */
2797	case 0xCB: case 0xCC: case 0xCD:    /* keypad 4,5,6 */
2798	case 0xCF: case 0xD0: case 0xD1:    /* keypad 1,2,3 */
2799	case 0xD2:              /* keypad 0 */
2800	    goto next_code;
2801
2802	case 0x38:              /* left alt key */
2803	    break;
2804	default:
2805	    if (chr) {
2806		compose = chr = 0;
2807		do_bell(cur_console, BELL_PITCH, BELL_DURATION);
2808		goto next_code;
2809	    }
2810	    break;
2811	}
2812    }
2813
2814    state = (shfts ? 1 : 0 ) | (2 * (ctls ? 1 : 0)) | (4 * (alts ? 1 : 0));
2815    if ((!agrs && (cur_console->status & ALKED))
2816	|| (agrs && !(cur_console->status & ALKED)))
2817	keycode += ALTGR_OFFSET;
2818    key = &key_map.key[keycode];
2819    if ( ((key->flgs & FLAG_LOCK_C) && (cur_console->status & CLKED))
2820	 || ((key->flgs & FLAG_LOCK_N) && (cur_console->status & NLKED)) )
2821	state ^= 1;
2822
2823    /* Check for make/break */
2824    action = key->map[state];
2825    if (scancode & 0x80) {      /* key released */
2826	if (key->spcl & 0x80) {
2827	    switch (action) {
2828	    case LSH:
2829		shfts &= ~1;
2830		break;
2831	    case RSH:
2832		shfts &= ~2;
2833		break;
2834	    case LCTR:
2835		ctls &= ~1;
2836		break;
2837	    case RCTR:
2838		ctls &= ~2;
2839		break;
2840	    case LALT:
2841		alts &= ~1;
2842		break;
2843	    case RALT:
2844		alts &= ~2;
2845		break;
2846	    case NLK:
2847		nlkcnt = 0;
2848		break;
2849	    case CLK:
2850		clkcnt = 0;
2851		break;
2852	    case SLK:
2853		slkcnt = 0;
2854		break;
2855	    case ASH:
2856		agrs = 0;
2857		break;
2858	    case ALK:
2859		alkcnt = 0;
2860		break;
2861	    case META:
2862		metas = 0;
2863		break;
2864	    }
2865	}
2866	if (chr && !compose) {
2867	    action = chr;
2868	    chr = 0;
2869	    return(action);
2870	}
2871    } else {
2872	/* key pressed */
2873	if (key->spcl & (0x80>>state)) {
2874	    switch (action) {
2875	    /* LOCKING KEYS */
2876	    case NLK:
2877#ifdef SC_SPLASH_SCREEN
2878		toggle_splash_screen(cur_console); /* SOS XXX */
2879#endif
2880		if (!nlkcnt) {
2881		    nlkcnt++;
2882		    if (cur_console->status & NLKED)
2883			cur_console->status &= ~NLKED;
2884		    else
2885			cur_console->status |= NLKED;
2886		    update_leds(cur_console->status);
2887		}
2888		break;
2889	    case CLK:
2890		if (!clkcnt) {
2891		    clkcnt++;
2892		    if (cur_console->status & CLKED)
2893			cur_console->status &= ~CLKED;
2894		    else
2895			cur_console->status |= CLKED;
2896		    update_leds(cur_console->status);
2897		}
2898		break;
2899	    case SLK:
2900		if (!slkcnt) {
2901		    slkcnt++;
2902		    if (cur_console->status & SLKED) {
2903			cur_console->status &= ~SLKED;
2904			if (cur_console->status & BUFFER_SAVED){
2905			    int i;
2906			    u_short *ptr = cur_console->history_save;
2907
2908			    for (i=0; i<cur_console->ysize; i++) {
2909				bcopyw(ptr,
2910				       cur_console->scr_buf +
2911				       (cur_console->xsize*i),
2912				       cur_console->xsize * sizeof(u_short));
2913				ptr += cur_console->xsize;
2914				if (ptr + cur_console->xsize >
2915				    cur_console->history +
2916				    cur_console->history_size)
2917				    ptr = cur_console->history;
2918			    }
2919			    cur_console->status &= ~BUFFER_SAVED;
2920			    cur_console->history_head=cur_console->history_save;
2921			    cur_console->status |= CURSOR_ENABLED;
2922			    mark_all(cur_console);
2923			}
2924			scstart(VIRTUAL_TTY(get_scr_num()));
2925		    }
2926		    else
2927			cur_console->status |= SLKED;
2928		    update_leds(cur_console->status);
2929		}
2930		break;
2931	    case ALK:
2932		if (!alkcnt) {
2933		    alkcnt++;
2934		    if (cur_console->status & ALKED)
2935			cur_console->status &= ~ALKED;
2936		    else
2937			cur_console->status |= ALKED;
2938		    update_leds(cur_console->status);
2939		}
2940		break;
2941
2942	    /* NON-LOCKING KEYS */
2943	    case NOP:
2944		break;
2945	    case SPSC:
2946#ifdef SC_SPLASH_SCREEN
2947		toggle_splash_screen(cur_console);
2948#endif
2949		break;
2950	    case RBT:
2951		shutdown_nice();
2952		break;
2953	    case SUSP:
2954#if NAPM > 0
2955		apm_suspend();
2956#endif
2957		break;
2958
2959	    case DBG:
2960#ifdef DDB          /* try to switch to console 0 */
2961		if (cur_console->smode.mode == VT_AUTO &&
2962		    console[0]->smode.mode == VT_AUTO)
2963		    switch_scr(cur_console, 0);
2964		Debugger("manual escape to debugger");
2965#else
2966		printf("No debugger in kernel\n");
2967#endif
2968		break;
2969	    case LSH:
2970		shfts |= 1;
2971		break;
2972	    case RSH:
2973		shfts |= 2;
2974		break;
2975	    case LCTR:
2976		ctls |= 1;
2977		break;
2978	    case RCTR:
2979		ctls |= 2;
2980		break;
2981	    case LALT:
2982		alts |= 1;
2983		break;
2984	    case RALT:
2985		alts |= 2;
2986		break;
2987	    case ASH:
2988		agrs = 1;
2989		break;
2990	    case META:
2991		metas = 1;
2992		break;
2993	    case NEXT:
2994		switch_scr(cur_console, (get_scr_num() + 1) % MAXCONS);
2995		break;
2996	    case BTAB:
2997		return(BKEY);
2998	    default:
2999		if (action >= F_SCR && action <= L_SCR) {
3000		    switch_scr(cur_console, action - F_SCR);
3001		    break;
3002		}
3003		if (action >= F_FN && action <= L_FN)
3004		    action |= FKEY;
3005		return(action);
3006	    }
3007	}
3008	else {
3009	    if (metas)
3010		action |= MKEY;
3011	    return(action);
3012	}
3013    }
3014    goto next_code;
3015}
3016
3017int
3018scmmap(dev_t dev, int offset, int nprot)
3019{
3020    if (offset > 0x20000 - PAGE_SIZE)
3021	return -1;
3022    return i386_btop((VIDEOMEM + offset));
3023}
3024
3025/*
3026 * Calculate hardware attributes word using logical attributes mask and
3027 * hardware colors
3028 */
3029
3030static int
3031mask2attr(struct term_stat *term)
3032{
3033    int attr, mask = term->attr_mask;
3034
3035    if (mask & REVERSE_ATTR) {
3036	attr = ((mask & FOREGROUND_CHANGED) ?
3037		((term->cur_color & 0xF000) >> 4) :
3038		(term->rev_color & 0x0F00)) |
3039	       ((mask & BACKGROUND_CHANGED) ?
3040		((term->cur_color & 0x0F00) << 4) :
3041		(term->rev_color & 0xF000));
3042    } else
3043	attr = term->cur_color;
3044
3045    /* XXX: underline mapping for Hercules adapter can be better */
3046    if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
3047	attr ^= 0x0800;
3048    if (mask & BLINK_ATTR)
3049	attr ^= 0x8000;
3050
3051    return attr;
3052}
3053
3054static void
3055set_keyboard(int command, int data)
3056{
3057    int s;
3058    int c;
3059
3060    if (sc_kbdc == NULL)
3061	return;
3062
3063    /* prevent the timeout routine from polling the keyboard */
3064    if (!kbdc_lock(sc_kbdc, TRUE))
3065	return;
3066
3067    /* disable the keyboard and mouse interrupt */
3068    s = spltty();
3069    c = get_controller_command_byte(sc_kbdc);
3070    if ((c == -1)
3071	|| !set_controller_command_byte(sc_kbdc,
3072            kbdc_get_device_mask(sc_kbdc),
3073            KBD_ENABLE_KBD_PORT | KBD_DISABLE_KBD_INT
3074                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
3075	/* CONTROLLER ERROR */
3076        kbdc_lock(sc_kbdc, FALSE);
3077	splx(s);
3078	return;
3079    }
3080    /*
3081     * Now that the keyboard controller is told not to generate
3082     * the keyboard and mouse interrupts, call `splx()' to allow
3083     * the other tty interrupts. The clock interrupt may also occur,
3084     * but the timeout routine (`scrn_timer()') will be blocked
3085     * by the lock flag set via `kbdc_lock()'
3086     */
3087    splx(s);
3088
3089    send_kbd_command_and_data(sc_kbdc, command, data);
3090
3091    /* restore the interrupts */
3092    if (!set_controller_command_byte(sc_kbdc,
3093            kbdc_get_device_mask(sc_kbdc),
3094	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
3095	/* CONTROLLER ERROR */
3096    }
3097    kbdc_lock(sc_kbdc, FALSE);
3098}
3099
3100static void
3101update_leds(int which)
3102{
3103    static u_char xlate_leds[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
3104
3105    /* replace CAPS led with ALTGR led for ALTGR keyboards */
3106    if (key_map.n_keys > ALTGR_OFFSET) {
3107	if (which & ALKED)
3108	    which |= CLKED;
3109	else
3110	    which &= ~CLKED;
3111    }
3112
3113    set_keyboard(KBDC_SET_LEDS, xlate_leds[which & LED_MASK]);
3114}
3115
3116void
3117set_mode(scr_stat *scp)
3118{
3119    char *modetable;
3120    char special_modetable[64];
3121
3122    if (scp != cur_console)
3123	return;
3124
3125    /* setup video hardware for the given mode */
3126    switch (scp->mode) {
3127    case M_VGA_M80x60:
3128	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3129	goto special_80x60;
3130
3131    case M_VGA_C80x60:
3132	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3133special_80x60:
3134	special_modetable[2]  = 0x08;
3135	special_modetable[19] = 0x47;
3136	goto special_480l;
3137
3138    case M_VGA_M80x30:
3139	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3140	goto special_80x30;
3141
3142    case M_VGA_C80x30:
3143	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3144special_80x30:
3145	special_modetable[19] = 0x4f;
3146special_480l:
3147	special_modetable[9] |= 0xc0;
3148	special_modetable[16] = 0x08;
3149	special_modetable[17] = 0x3e;
3150	special_modetable[26] = 0xea;
3151	special_modetable[28] = 0xdf;
3152	special_modetable[31] = 0xe7;
3153	special_modetable[32] = 0x04;
3154	modetable = special_modetable;
3155	goto setup_mode;
3156
3157    case M_ENH_B80x43:
3158	bcopyw(video_mode_ptr+(64*M_ENH_B80x25), &special_modetable, 64);
3159	goto special_80x43;
3160
3161    case M_ENH_C80x43:
3162	bcopyw(video_mode_ptr+(64*M_ENH_C80x25), &special_modetable, 64);
3163special_80x43:
3164	special_modetable[28] = 87;
3165	goto special_80x50;
3166
3167    case M_VGA_M80x50:
3168	bcopyw(video_mode_ptr+(64*M_VGA_M80x25), &special_modetable, 64);
3169	goto special_80x50;
3170
3171    case M_VGA_C80x50:
3172	bcopyw(video_mode_ptr+(64*M_VGA_C80x25), &special_modetable, 64);
3173special_80x50:
3174	special_modetable[2] = 8;
3175	special_modetable[19] = 7;
3176	modetable = special_modetable;
3177	goto setup_mode;
3178
3179    case M_VGA_C40x25: case M_VGA_C80x25:
3180    case M_VGA_M80x25:
3181    case M_B40x25:     case M_C40x25:
3182    case M_B80x25:     case M_C80x25:
3183    case M_ENH_B40x25: case M_ENH_C40x25:
3184    case M_ENH_B80x25: case M_ENH_C80x25:
3185
3186	modetable = video_mode_ptr + (scp->mode * 64);
3187setup_mode:
3188	set_vgaregs(modetable);
3189	scp->font_size = *(modetable + 2);
3190
3191	/* set font type (size) */
3192	if (scp->font_size < FONT_14) {
3193	    if (fonts_loaded & FONT_8)
3194		copy_font(LOAD, FONT_8, font_8);
3195	    outb(TSIDX, 0x03); outb(TSREG, 0x0A);   /* font 2 */
3196	} else if (scp->font_size >= FONT_16) {
3197	    if (fonts_loaded & FONT_16)
3198		copy_font(LOAD, FONT_16, font_16);
3199	    outb(TSIDX, 0x03); outb(TSREG, 0x00);   /* font 0 */
3200	} else {
3201	    if (fonts_loaded & FONT_14)
3202		copy_font(LOAD, FONT_14, font_14);
3203	    outb(TSIDX, 0x03); outb(TSREG, 0x05);   /* font 1 */
3204	}
3205	if (flags & CHAR_CURSOR)
3206	    set_destructive_cursor(scp);
3207	mark_all(scp);
3208	break;
3209
3210    case M_BG320:     case M_CG320:     case M_BG640:
3211    case M_CG320_D:   case M_CG640_E:
3212    case M_CG640x350: case M_ENH_CG640:
3213    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
3214
3215	set_vgaregs(video_mode_ptr + (scp->mode * 64));
3216	scp->font_size = FONT_NONE;
3217	break;
3218
3219    default:
3220	/* call user defined function XXX */
3221	break;
3222    }
3223
3224    /* set border color for this (virtual) console */
3225    set_border(scp->border);
3226    return;
3227}
3228
3229void
3230set_border(u_char color)
3231{
3232    inb(crtc_addr+6);               /* reset flip-flop */
3233    outb(ATC, 0x11); outb(ATC, color);
3234    inb(crtc_addr+6);               /* reset flip-flop */
3235    outb(ATC, 0x20);                /* enable Palette */
3236}
3237
3238static void
3239set_vgaregs(char *modetable)
3240{
3241    int i, s = splhigh();
3242
3243    outb(TSIDX, 0x00); outb(TSREG, 0x01);   	/* stop sequencer */
3244    outb(TSIDX, 0x07); outb(TSREG, 0x00);   	/* unlock registers */
3245    for (i=0; i<4; i++) {           		/* program sequencer */
3246	outb(TSIDX, i+1);
3247	outb(TSREG, modetable[i+5]);
3248    }
3249    outb(MISC, modetable[9]);       		/* set dot-clock */
3250    outb(TSIDX, 0x00); outb(TSREG, 0x03);   	/* start sequencer */
3251    outb(crtc_addr, 0x11);
3252    outb(crtc_addr+1, inb(crtc_addr+1) & 0x7F);
3253    for (i=0; i<25; i++) {          		/* program crtc */
3254	outb(crtc_addr, i);
3255	if (i == 14 || i == 15)     		/* no hardware cursor */
3256	    outb(crtc_addr+1, 0xff);
3257	else
3258	    outb(crtc_addr+1, modetable[i+10]);
3259    }
3260    inb(crtc_addr+6);           		/* reset flip-flop */
3261    for (i=0; i<20; i++) {          		/* program attribute ctrl */
3262	outb(ATC, i);
3263	outb(ATC, modetable[i+35]);
3264    }
3265    for (i=0; i<9; i++) {           		/* program graph data ctrl */
3266	outb(GDCIDX, i);
3267	outb(GDCREG, modetable[i+55]);
3268    }
3269    inb(crtc_addr+6);           		/* reset flip-flop */
3270    outb(ATC, 0x20);            		/* enable palette */
3271    splx(s);
3272}
3273
3274static void
3275set_font_mode()
3276{
3277    int s = splhigh();
3278
3279    /* setup vga for loading fonts (graphics plane mode) */
3280    inb(crtc_addr+6);           		/* reset flip-flop */
3281    outb(ATC, 0x10); outb(ATC, 0x01);
3282    inb(crtc_addr+6);               		/* reset flip-flop */
3283    outb(ATC, 0x20);            		/* enable palette */
3284
3285#if SLOW_VGA
3286    outb(TSIDX, 0x02); outb(TSREG, 0x04);
3287    outb(TSIDX, 0x04); outb(TSREG, 0x06);
3288    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
3289    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
3290    outb(GDCIDX, 0x06); outb(GDCREG, 0x05);
3291#else
3292    outw(TSIDX, 0x0402);
3293    outw(TSIDX, 0x0604);
3294    outw(GDCIDX, 0x0204);
3295    outw(GDCIDX, 0x0005);
3296    outw(GDCIDX, 0x0506);               /* addr = a0000, 64kb */
3297#endif
3298    splx(s);
3299}
3300
3301static void
3302set_normal_mode()
3303{
3304    char *modetable;
3305    int s = splhigh();
3306
3307    switch (cur_console->mode) {
3308    case M_VGA_M80x60:
3309    case M_VGA_M80x50:
3310    case M_VGA_M80x30:
3311	modetable = video_mode_ptr + (64*M_VGA_M80x25);
3312	break;
3313
3314    case M_VGA_C80x60:
3315    case M_VGA_C80x50:
3316    case M_VGA_C80x30:
3317	modetable = video_mode_ptr + (64*M_VGA_C80x25);
3318	break;
3319
3320    case M_ENH_B80x43:
3321	modetable = video_mode_ptr + (64*M_ENH_B80x25);
3322	break;
3323
3324    case M_ENH_C80x43:
3325	modetable = video_mode_ptr + (64*M_ENH_C80x25);
3326	break;
3327
3328    case M_VGA_C40x25: case M_VGA_C80x25:
3329    case M_VGA_M80x25:
3330    case M_B40x25:     case M_C40x25:
3331    case M_B80x25:     case M_C80x25:
3332    case M_ENH_B40x25: case M_ENH_C40x25:
3333    case M_ENH_B80x25: case M_ENH_C80x25:
3334
3335    case M_BG320:     case M_CG320:     case M_BG640:
3336    case M_CG320_D:   case M_CG640_E:
3337    case M_CG640x350: case M_ENH_CG640:
3338    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
3339	modetable = video_mode_ptr + (cur_console->mode * 64);
3340
3341    default:
3342	modetable = video_mode_ptr + (64*M_VGA_C80x25);
3343    }
3344
3345    /* setup vga for normal operation mode again */
3346    inb(crtc_addr+6);           		/* reset flip-flop */
3347    outb(ATC, 0x10); outb(ATC, modetable[0x10+35]);
3348    inb(crtc_addr+6);               		/* reset flip-flop */
3349    outb(ATC, 0x20);            		/* enable palette */
3350#if SLOW_VGA
3351    outb(TSIDX, 0x02); outb(TSREG, modetable[0x02+4]);
3352    outb(TSIDX, 0x04); outb(TSREG, modetable[0x04+4]);
3353    outb(GDCIDX, 0x04); outb(GDCREG, modetable[0x04+55]);
3354    outb(GDCIDX, 0x05); outb(GDCREG, modetable[0x05+55]);
3355    outb(GDCIDX, 0x06); outb(GDCREG, modetable[0x06+55]);
3356    if (crtc_addr == MONO_BASE) {
3357	outb(GDCIDX, 0x06); outb(GDCREG,(modetable[0x06+55] & 0x03) | 0x08);
3358    }
3359    else {
3360	outb(GDCIDX, 0x06); outb(GDCREG,(modetable[0x06+55] & 0x03) | 0x0c);
3361    }
3362#else
3363    outw(TSIDX, 0x0002 | (modetable[0x02+4]<<8));
3364    outw(TSIDX, 0x0004 | (modetable[0x04+4]<<8));
3365    outw(GDCIDX, 0x0004 | (modetable[0x04+55]<<8));
3366    outw(GDCIDX, 0x0005 | (modetable[0x05+55]<<8));
3367    if (crtc_addr == MONO_BASE)
3368        outw(GDCIDX, 0x0006 | (((modetable[0x06+55] & 0x03) | 0x08)<<8));
3369    else
3370        outw(GDCIDX, 0x0006 | (((modetable[0x06+55] & 0x03) | 0x0c)<<8));
3371#endif
3372    splx(s);
3373}
3374
3375void
3376copy_font(int operation, int font_type, char* font_image)
3377{
3378    int ch, line, segment, fontsize;
3379    u_char val;
3380
3381    /* dont mess with console we dont know video mode on */
3382    if (cur_console->status & UNKNOWN_MODE)
3383	return;
3384
3385    switch (font_type) {
3386    default:
3387    case FONT_8:
3388	segment = 0x8000;
3389	fontsize = 8;
3390	break;
3391    case FONT_14:
3392	segment = 0x4000;
3393	fontsize = 14;
3394	break;
3395    case FONT_16:
3396	segment = 0x0000;
3397	fontsize = 16;
3398	break;
3399    }
3400    outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
3401    outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
3402    set_font_mode();
3403    for (ch=0; ch < 256; ch++)
3404	for (line=0; line < fontsize; line++)
3405	if (operation)
3406	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line) =
3407		    font_image[(ch*fontsize)+line];
3408	else
3409	    font_image[(ch*fontsize)+line] =
3410	    *(char *)pa_to_va(VIDEOMEM+(segment)+(ch*32)+line);
3411    set_normal_mode();
3412    outb(TSIDX, 0x01); outb(TSREG, val & 0xDF); /* enable screen */
3413}
3414
3415static void
3416set_destructive_cursor(scr_stat *scp)
3417{
3418    u_char cursor[32];
3419    caddr_t address;
3420    int i;
3421    char *font_buffer;
3422
3423
3424    if (scp->font_size < FONT_14) {
3425	font_buffer = font_8;
3426	address = (caddr_t)VIDEOMEM + 0x8000;
3427    }
3428    else if (scp->font_size >= FONT_16) {
3429	font_buffer = font_16;
3430	address = (caddr_t)VIDEOMEM;
3431    }
3432    else {
3433	font_buffer = font_14;
3434	address = (caddr_t)VIDEOMEM + 0x4000;
3435    }
3436
3437    if (scp->status & MOUSE_VISIBLE) {
3438	if ((scp->cursor_saveunder & 0xff) == 0xd0)
3439    	    bcopyw(&scp->mouse_cursor[0], cursor, scp->font_size);
3440	else if ((scp->cursor_saveunder & 0xff) == 0xd1)
3441    	    bcopyw(&scp->mouse_cursor[32], cursor, scp->font_size);
3442	else if ((scp->cursor_saveunder & 0xff) == 0xd2)
3443    	    bcopyw(&scp->mouse_cursor[64], cursor, scp->font_size);
3444	else if ((scp->cursor_saveunder & 0xff) == 0xd3)
3445    	    bcopyw(&scp->mouse_cursor[96], cursor, scp->font_size);
3446	else
3447	    bcopyw(font_buffer+((scp->cursor_saveunder & 0xff)*scp->font_size),
3448 	       	   cursor, scp->font_size);
3449    }
3450    else
3451    	bcopyw(font_buffer + ((scp->cursor_saveunder & 0xff) * scp->font_size),
3452 	       cursor, scp->font_size);
3453    for (i=0; i<32; i++)
3454	if ((i >= scp->cursor_start && i <= scp->cursor_end) ||
3455	    (scp->cursor_start >= scp->font_size && i == scp->font_size - 1))
3456	    cursor[i] |= 0xff;
3457#if 0
3458    while (!(inb(crtc_addr+6) & 0x08)) /* wait for vertical retrace */ ;
3459#endif
3460    set_font_mode();
3461    bcopy(cursor, (char *)pa_to_va(address) + DEAD_CHAR * 32, 32);
3462    set_normal_mode();
3463}
3464
3465static void
3466set_mouse_pos(scr_stat *scp)
3467{
3468    static int last_xpos = -1, last_ypos = -1;
3469    /*
3470     * the margins imposed here are not ideal, we loose
3471     * a couble of pixels on the borders..
3472     */
3473    if (scp->mouse_xpos < 0)
3474	scp->mouse_xpos = 0;
3475    if (scp->mouse_ypos < 0)
3476	scp->mouse_ypos = 0;
3477    if (scp->mouse_xpos > (scp->xsize*8)-2)
3478	scp->mouse_xpos = (scp->xsize*8)-2;
3479    if (scp->mouse_ypos > (scp->ysize*scp->font_size)-2)
3480	scp->mouse_ypos = (scp->ysize*scp->font_size)-2;
3481
3482    if (scp->status & UNKNOWN_MODE)
3483	return;
3484
3485    if (scp->mouse_xpos != last_xpos || scp->mouse_ypos != last_ypos) {
3486	scp->status |= MOUSE_MOVED;
3487
3488    	scp->mouse_pos = scp->scr_buf +
3489	    ((scp->mouse_ypos/scp->font_size)*scp->xsize + scp->mouse_xpos/8);
3490
3491	if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING)) {
3492	    u_short *ptr;
3493	    int i = 0;
3494
3495	    mark_for_update(scp, scp->mouse_cut_start - scp->scr_buf);
3496	    mark_for_update(scp, scp->mouse_cut_end - scp->scr_buf);
3497	    scp->mouse_cut_end = scp->mouse_pos;
3498	    for (ptr = (scp->mouse_cut_start > scp->mouse_cut_end
3499			? scp->mouse_cut_end : scp->mouse_cut_start);
3500		 ptr <= (scp->mouse_cut_start > scp->mouse_cut_end
3501			 ? scp->mouse_cut_start : scp->mouse_cut_end);
3502	    	 ptr++) {
3503	        cut_buffer[i++] = *ptr & 0xff;
3504	        if (((ptr - scp->scr_buf) % scp->xsize) == (scp->xsize - 1)) {
3505		    cut_buffer[i++] = '\n';
3506	        }
3507	    }
3508	    cut_buffer[i] = 0x00;
3509        }
3510    }
3511}
3512
3513static void
3514mouse_cut_start(scr_stat *scp)
3515{
3516    int i;
3517
3518    if (scp->status & MOUSE_VISIBLE) {
3519	if (scp->mouse_pos == scp->mouse_cut_start &&
3520	    scp->mouse_cut_start == scp->mouse_cut_end) {
3521	    cut_buffer[0] = 0x00;
3522	    remove_cutmarking(scp);
3523	}
3524	else {
3525	    scp->mouse_cut_start = scp->mouse_cut_end = scp->mouse_pos;
3526	    cut_buffer[0] = *scp->mouse_cut_start & 0xff;
3527	    cut_buffer[1] = 0x00;
3528	    scp->status |= MOUSE_CUTTING;
3529	}
3530    	mark_all(scp);
3531	/* delete all other screens cut markings */
3532	for (i=0; i<MAXCONS; i++) {
3533	    if (console[i] == NULL || console[i] == scp)
3534		continue;
3535	    remove_cutmarking(console[i]);
3536	}
3537    }
3538}
3539
3540static void
3541mouse_cut_end(scr_stat *scp)
3542{
3543    if (scp->status & MOUSE_VISIBLE) {
3544	scp->status &= ~MOUSE_CUTTING;
3545    }
3546}
3547
3548static void
3549mouse_paste(scr_stat *scp)
3550{
3551    if (scp->status & MOUSE_VISIBLE) {
3552	struct tty *tp;
3553	u_char *ptr = cut_buffer;
3554
3555	tp = VIRTUAL_TTY(get_scr_num());
3556	while (*ptr)
3557	    (*linesw[tp->t_line].l_rint)(scr_rmap[*ptr++], tp);
3558    }
3559}
3560
3561static void
3562draw_mouse_image(scr_stat *scp)
3563{
3564    caddr_t address;
3565    int i;
3566    char *font_buffer;
3567    u_short buffer[32];
3568    u_short xoffset, yoffset;
3569    u_short *crt_pos = Crtat + (scp->mouse_pos - scp->scr_buf);
3570    int font_size = scp->font_size;
3571
3572    if (font_size < FONT_14) {
3573	font_buffer = font_8;
3574	address = (caddr_t)VIDEOMEM + 0x8000;
3575    }
3576    else if (font_size >= FONT_16) {
3577	font_buffer = font_16;
3578	address = (caddr_t)VIDEOMEM;
3579    }
3580    else {
3581	font_buffer = font_14;
3582	address = (caddr_t)VIDEOMEM + 0x4000;
3583    }
3584    xoffset = scp->mouse_xpos % 8;
3585    yoffset = scp->mouse_ypos % font_size;
3586
3587    /* prepare mousepointer char's bitmaps */
3588    bcopyw(font_buffer + ((*(scp->mouse_pos) & 0xff) * font_size),
3589	   &scp->mouse_cursor[0], font_size);
3590    bcopyw(font_buffer + ((*(scp->mouse_pos+1) & 0xff) * font_size),
3591	   &scp->mouse_cursor[32], font_size);
3592    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize) & 0xff) * font_size),
3593	   &scp->mouse_cursor[64], font_size);
3594    bcopyw(font_buffer + ((*(scp->mouse_pos+scp->xsize+1) & 0xff) * font_size),
3595	   &scp->mouse_cursor[96], font_size);
3596    for (i=0; i<font_size; i++) {
3597	buffer[i] = scp->mouse_cursor[i]<<8 | scp->mouse_cursor[i+32];
3598	buffer[i+font_size]=scp->mouse_cursor[i+64]<<8|scp->mouse_cursor[i+96];
3599    }
3600
3601    /* now and-or in the mousepointer image */
3602    for (i=0; i<16; i++) {
3603	buffer[i+yoffset] =
3604	    ( buffer[i+yoffset] & ~(mouse_and_mask[i] >> xoffset))
3605	    | (mouse_or_mask[i] >> xoffset);
3606    }
3607    for (i=0; i<font_size; i++) {
3608	scp->mouse_cursor[i] = (buffer[i] & 0xff00) >> 8;
3609	scp->mouse_cursor[i+32] = buffer[i] & 0xff;
3610	scp->mouse_cursor[i+64] = (buffer[i+font_size] & 0xff00) >> 8;
3611	scp->mouse_cursor[i+96] = buffer[i+font_size] & 0xff;
3612    }
3613
3614    scp->mouse_oldpos = scp->mouse_pos;
3615
3616    /* wait for vertical retrace to avoid jitter on some videocards */
3617#if 0
3618    while (!(inb(crtc_addr+6) & 0x08)) /* idle */ ;
3619#endif
3620    set_font_mode();
3621    bcopy(scp->mouse_cursor, (char *)pa_to_va(address) + 0xd0 * 32, 128);
3622    set_normal_mode();
3623    *(crt_pos) = (*(scp->mouse_pos)&0xff00)|0xd0;
3624    *(crt_pos+scp->xsize) = (*(scp->mouse_pos+scp->xsize)&0xff00)|0xd2;
3625    if (scp->mouse_xpos < (scp->xsize-1)*8) {
3626    	*(crt_pos+1) = (*(scp->mouse_pos+1)&0xff00)|0xd1;
3627    	*(crt_pos+scp->xsize+1) = (*(scp->mouse_pos+scp->xsize+1)&0xff00)|0xd3;
3628    }
3629    mark_for_update(scp, scp->mouse_pos - scp->scr_buf);
3630    mark_for_update(scp, scp->mouse_pos + scp->xsize + 1 - scp->scr_buf);
3631}
3632
3633static void
3634remove_mouse_image(scr_stat *scp)
3635{
3636    u_short *crt_pos = Crtat + (scp->mouse_oldpos - scp->scr_buf);
3637
3638    *(crt_pos) = *(scp->mouse_oldpos);
3639    *(crt_pos+1) = *(scp->mouse_oldpos+1);
3640    *(crt_pos+scp->xsize) = *(scp->mouse_oldpos+scp->xsize);
3641    *(crt_pos+scp->xsize+1) = *(scp->mouse_oldpos+scp->xsize+1);
3642    mark_for_update(scp, scp->mouse_oldpos - scp->scr_buf);
3643    mark_for_update(scp, scp->mouse_oldpos + scp->xsize + 1 - scp->scr_buf);
3644}
3645
3646static void
3647draw_cutmarking(scr_stat *scp)
3648{
3649    u_short *ptr;
3650    u_short och, nch;
3651
3652    for (ptr=scp->scr_buf; ptr<=(scp->scr_buf+(scp->xsize*scp->ysize)); ptr++) {
3653	nch = och = *(Crtat + (ptr - scp->scr_buf));
3654	/* are we outside the selected area ? */
3655	if ( ptr < (scp->mouse_cut_start > scp->mouse_cut_end ?
3656	            scp->mouse_cut_end : scp->mouse_cut_start) ||
3657	     ptr > (scp->mouse_cut_start > scp->mouse_cut_end ?
3658	            scp->mouse_cut_start : scp->mouse_cut_end)) {
3659	    if (ptr != scp->cursor_pos)
3660		nch = (och & 0xff) | (*ptr & 0xff00);
3661	}
3662	else {
3663	    /* are we clear of the cursor image ? */
3664	    if (ptr != scp->cursor_pos)
3665		nch = (och & 0x88ff) | (*ptr & 0x7000)>>4 | (*ptr & 0x0700)<<4;
3666	    else {
3667		if (flags & CHAR_CURSOR)
3668		    nch = (och & 0x88ff)|(*ptr & 0x7000)>>4|(*ptr & 0x0700)<<4;
3669		else
3670		    if (!(flags & BLINK_CURSOR))
3671		        nch = (och & 0xff) | (*ptr & 0xff00);
3672	    }
3673	}
3674	if (nch != och)
3675	    *(Crtat + (ptr - scp->scr_buf)) = nch;
3676    }
3677}
3678
3679static void
3680remove_cutmarking(scr_stat *scp)
3681{
3682    scp->mouse_cut_start = scp->mouse_cut_end = NULL;
3683    scp->status &= ~MOUSE_CUTTING;
3684    mark_all(scp);
3685}
3686
3687static void
3688save_palette(void)
3689{
3690    int i;
3691
3692    outb(PALRADR, 0x00);
3693    for (i=0x00; i<0x300; i++)
3694	palette[i] = inb(PALDATA);
3695    inb(crtc_addr+6);           /* reset flip/flop */
3696}
3697
3698void
3699load_palette(char *palette)
3700{
3701    int i;
3702
3703    outb(PIXMASK, 0xFF);            /* no pixelmask */
3704    outb(PALWADR, 0x00);
3705    for (i=0x00; i<0x300; i++)
3706	 outb(PALDATA, palette[i]);
3707    inb(crtc_addr+6);           /* reset flip/flop */
3708    outb(ATC, 0x20);            /* enable palette */
3709}
3710
3711static void
3712do_bell(scr_stat *scp, int pitch, int duration)
3713{
3714    if (flags & VISUAL_BELL) {
3715	if (blink_in_progress)
3716	    return;
3717	blink_in_progress = 4;
3718	if (scp != cur_console)
3719	    blink_in_progress += 2;
3720	blink_screen(cur_console);
3721	timeout((timeout_func_t)blink_screen, cur_console, hz/10);
3722    } else {
3723	if (scp != cur_console)
3724	    pitch *= 2;
3725	sysbeep(pitch, duration);
3726    }
3727}
3728
3729static void
3730blink_screen(scr_stat *scp)
3731{
3732    if (blink_in_progress > 1) {
3733	if (blink_in_progress & 1)
3734	    fillw(kernel_default.std_color | scr_map[0x20],
3735		  Crtat, scp->xsize * scp->ysize);
3736	else
3737	    fillw(kernel_default.rev_color | scr_map[0x20],
3738		  Crtat, scp->xsize * scp->ysize);
3739	blink_in_progress--;
3740	timeout((timeout_func_t)blink_screen, scp, hz/10);
3741    }
3742    else {
3743	blink_in_progress = FALSE;
3744    	mark_all(scp);
3745	if (delayed_next_scr)
3746	    switch_scr(scp, delayed_next_scr - 1);
3747    }
3748}
3749
3750#ifdef SC_SPLASH_SCREEN
3751static void
3752toggle_splash_screen(scr_stat *scp)
3753{
3754    static int toggle = 0;
3755    static u_char save_mode;
3756    int s = splhigh();
3757
3758    if (toggle) {
3759	scp->mode = save_mode;
3760	scp->status &= ~UNKNOWN_MODE;
3761	set_mode(scp);
3762	load_palette(palette);
3763	toggle = 0;
3764    }
3765    else {
3766	save_mode = scp->mode;
3767	scp->mode = M_VGA_CG320;
3768	scp->status |= UNKNOWN_MODE;
3769	set_mode(scp);
3770	/* load image */
3771	toggle = 1;
3772    }
3773    splx(s);
3774}
3775#endif
3776#endif /* NSC */
3777