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