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