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