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