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