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