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