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