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