syscons.c revision 15562
1160814Ssimon/*-
2296341Sdelphij * Copyright (c) 1992-1995 S�ren Schmidt
3160814Ssimon * All rights reserved.
4296341Sdelphij *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon * 1. Redistributions of source code must retain the above copyright
9160814Ssimon *    notice, this list of conditions and the following disclaimer
10160814Ssimon *    in this position and unchanged.
11160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
12160814Ssimon *    notice, this list of conditions and the following disclaimer in the
13160814Ssimon *    documentation and/or other materials provided with the distribution.
14296341Sdelphij * 3. The name of the author may not be used to endorse or promote products
15160814Ssimon *    derived from this software withough specific prior written permission
16160814Ssimon *
17160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18160814Ssimon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19160814Ssimon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20160814Ssimon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21160814Ssimon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23160814Ssimon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24160814Ssimon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25160814Ssimon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26160814Ssimon * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27160814Ssimon *
28160814Ssimon *  $Id: syscons.c,v 1.146 1996/05/01 03:58:21 bde Exp $
29160814Ssimon */
30160814Ssimon
31160814Ssimon#include "sc.h"
32160814Ssimon#include "apm.h"
33160814Ssimon#include "opt_ddb.h"
34160814Ssimon
35160814Ssimon#if NSC > 0
36160814Ssimon#include <sys/param.h>
37160814Ssimon#include <sys/systm.h>
38160814Ssimon#include <sys/conf.h>
39160814Ssimon#include <sys/ioctl.h>
40160814Ssimon#include <sys/proc.h>
41160814Ssimon#include <sys/signalvar.h>
42160814Ssimon#include <sys/tty.h>
43160814Ssimon#include <sys/uio.h>
44160814Ssimon#include <sys/callout.h>
45160814Ssimon#include <sys/kernel.h>
46160814Ssimon#include <sys/syslog.h>
47160814Ssimon#include <sys/errno.h>
48160814Ssimon#include <sys/malloc.h>
49160814Ssimon#include <sys/devconf.h>
50160814Ssimon#ifdef	DEVFS
51160814Ssimon#include <sys/devfsext.h>
52160814Ssimon#endif
53160814Ssimon
54160814Ssimon#include <machine/clock.h>
55160814Ssimon#include <machine/cons.h>
56160814Ssimon#include <machine/console.h>
57160814Ssimon#include <machine/psl.h>
58160814Ssimon#include <machine/frame.h>
59160814Ssimon#include <machine/pc/display.h>
60160814Ssimon#include <machine/apm_bios.h>
61160814Ssimon#include <machine/random.h>
62160814Ssimon
63160814Ssimon#include <vm/vm.h>
64160814Ssimon#include <vm/vm_param.h>
65296341Sdelphij#include <vm/pmap.h>
66160814Ssimon
67160814Ssimon#include <i386/isa/isa.h>
68160814Ssimon#include <i386/isa/isa_device.h>
69160814Ssimon#include <i386/isa/timerreg.h>
70160814Ssimon#include <i386/isa/kbdtables.h>
71160814Ssimon#include <i386/isa/syscons.h>
72296341Sdelphij
73160814Ssimon#if !defined(MAXCONS)
74160814Ssimon#define MAXCONS 16
75160814Ssimon#endif
76160814Ssimon
77160814Ssimon
78160814Ssimon/* this may break on older VGA's but is usefull on real 32 bit systems */
79296341Sdelphij#define bcopyw  bcopy
80160814Ssimon
81160814Ssimonstatic default_attr user_default = {
82160814Ssimon    (FG_LIGHTGREY | BG_BLACK) << 8,
83160814Ssimon    (FG_BLACK | BG_LIGHTGREY) << 8
84160814Ssimon};
85160814Ssimon
86160814Ssimonstatic default_attr kernel_default = {
87160814Ssimon    (FG_WHITE | BG_BLACK) << 8,
88160814Ssimon    (FG_BLACK | BG_LIGHTGREY) << 8
89160814Ssimon};
90160814Ssimon
91160814Ssimonstatic  scr_stat    	main_console;
92160814Ssimonstatic  scr_stat    	*console[MAXCONS];
93160814Ssimonstatic	void		*sc_devfs_token[MAXCONS];
94296341Sdelphij	scr_stat    	*cur_console;
95160814Ssimonstatic  scr_stat    	*new_scp, *old_scp;
96160814Ssimonstatic  term_stat   	kernel_console;
97296341Sdelphijstatic  default_attr    *current_default;
98160814Ssimonstatic  char        	init_done = FALSE;
99160814Ssimonstatic  int     	configuration = 0;
100160814Ssimonstatic  char        	switch_in_progress = FALSE;
101160814Ssimonstatic  char        	blink_in_progress = FALSE;
102160814Ssimonstatic  char        	write_in_progress = FALSE;
103160814Ssimon	u_int       	crtc_addr = MONO_BASE;
104160814Ssimonstatic  char        	crtc_vga = FALSE;
105160814Ssimonstatic  u_char      	shfts = 0, ctls = 0, alts = 0, agrs = 0, metas = 0;
106160814Ssimonstatic  u_char      	nlkcnt = 0, clkcnt = 0, slkcnt = 0, alkcnt = 0;
107160814Ssimonstatic  char        	*font_8 = NULL, *font_14 = NULL, *font_16 = NULL;
108160814Ssimonstatic  int     	fonts_loaded = 0;
109296341Sdelphij	char        	palette[3*256];
110160814Ssimonstatic  const u_int     n_fkey_tab = sizeof(fkey_tab) / sizeof(*fkey_tab);
111160814Ssimonstatic  int     	delayed_next_scr = FALSE;
112160814Ssimonstatic  long        	scrn_blank_time = 0;    /* screen saver timeout value */
113160814Ssimon	int     	scrn_blanked = FALSE;   /* screen saver active flag */
114160814Ssimonstatic  long       	scrn_time_stamp;
115160814Ssimon	u_char      	scr_map[256];
116160814Ssimonstatic  char        	*video_mode_ptr = NULL;
117160814Ssimon#if ASYNCH
118160814Ssimonstatic  u_char      	kbd_reply = 0;
119160814Ssimon#endif
120160814Ssimon
121160814Ssimonstatic  u_short mouse_and_mask[16] = {
122160814Ssimon	0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80,
123160814Ssimon	0xfe00, 0x1e00, 0x1f00, 0x0f00, 0x0f00, 0x0000, 0x0000, 0x0000
124160814Ssimon};
125160814Ssimonstatic  u_short mouse_or_mask[16] = {
126215697Ssimon	0x0000, 0x4000, 0x6000, 0x7000, 0x7800, 0x7c00, 0x7e00, 0x6800,
127160814Ssimon	0x0c00, 0x0c00, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, 0x0000
128215697Ssimon};
129296341Sdelphij
130296341Sdelphijstatic void    none_saver(int blank) { }
131296341Sdelphij
132296341Sdelphijvoid    (*current_saver) __P((int blank)) = none_saver;
133296341Sdelphij
134296341Sdelphijstatic int scattach(struct isa_device *dev);
135296341Sdelphijstatic int scparam(struct tty *tp, struct termios *t);
136296341Sdelphijstatic int scprobe(struct isa_device *dev);
137296341Sdelphijstatic void scstart(struct tty *tp);
138215697Ssimon
139215697Ssimon/* OS specific stuff */
140296341Sdelphij#ifdef not_yet_done
141296341Sdelphij#define VIRTUAL_TTY(x)  (sccons[x] = ttymalloc(sccons[x]))
142296341Sdelphijstruct  CONSOLE_TTY 	(sccons[MAXCONS] = ttymalloc(sccons[MAXCONS]))
143296341Sdelphijstruct  tty         	*sccons[MAXCONS+1];
144296341Sdelphij#else
145296341Sdelphij#define VIRTUAL_TTY(x)  &sccons[x]
146215697Ssimon#define CONSOLE_TTY 	&sccons[MAXCONS]
147215697Ssimonstatic struct tty     	sccons[MAXCONS+1];
148296341Sdelphij#endif
149296341Sdelphij#define MONO_BUF    	pa_to_va(0xB0000)
150296341Sdelphij#define CGA_BUF     	pa_to_va(0xB8000)
151296341Sdelphiju_short         	*Crtat;
152296341Sdelphij
153215697Ssimon#define WRAPHIST(scp, pointer, offset)\
154215697Ssimon    ((scp->history) + ((((pointer) - (scp->history)) + (scp->history_size)\
155296341Sdelphij    + (offset)) % (scp->history_size)))
156296341Sdelphij
157296341Sdelphijstruct  isa_driver scdriver = {
158296341Sdelphij    scprobe, scattach, "sc", 1
159215697Ssimon};
160160814Ssimon
161296341Sdelphijstatic	d_open_t	scopen;
162160814Ssimonstatic	d_close_t	scclose;
163296341Sdelphijstatic	d_read_t	scread;
164296341Sdelphijstatic	d_write_t	scwrite;
165296341Sdelphijstatic	d_ioctl_t	scioctl;
166160814Ssimonstatic	d_devtotty_t	scdevtotty;
167296341Sdelphijstatic	d_mmap_t	scmmap;
168296341Sdelphij
169296341Sdelphij#define CDEV_MAJOR 12
170296341Sdelphijstatic	struct cdevsw	scdevsw = {
171296341Sdelphij	scopen,		scclose,	scread,		scwrite,
172296341Sdelphij	scioctl,	nullstop,	noreset,	scdevtotty,
173160814Ssimon	ttselect,	scmmap,		nostrategy,	"sc",	NULL,	-1 };
174296341Sdelphij
175296341Sdelphij/*
176296341Sdelphij * Calculate hardware attributes word using logical attributes mask and
177296341Sdelphij * hardware colors
178296341Sdelphij */
179296341Sdelphij
180160814Ssimonstatic int
181296341Sdelphijmask2attr(struct term_stat *term)
182296341Sdelphij{
183296341Sdelphij    int attr, mask = term->attr_mask;
184160814Ssimon
185296341Sdelphij    if (mask & REVERSE_ATTR) {
186296341Sdelphij	attr = ((mask & FOREGROUND_CHANGED) ?
187296341Sdelphij		((term->cur_color & 0xF000) >> 4) :
188296341Sdelphij		(term->rev_color & 0x0F00)) |
189296341Sdelphij	       ((mask & BACKGROUND_CHANGED) ?
190296341Sdelphij		((term->cur_color & 0x0F00) << 4) :
191296341Sdelphij		(term->rev_color & 0xF000));
192160814Ssimon    } else
193296341Sdelphij	attr = term->cur_color;
194296341Sdelphij
195160814Ssimon    /* XXX: underline mapping for Hercules adapter can be better */
196296341Sdelphij    if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
197296341Sdelphij	attr ^= 0x0800;
198296341Sdelphij    if (mask & BLINK_ATTR)
199296341Sdelphij	attr ^= 0x8000;
200296341Sdelphij
201296341Sdelphij    return attr;
202296341Sdelphij}
203296341Sdelphij
204296341Sdelphijstatic int
205296341Sdelphijscprobe(struct isa_device *dev)
206296341Sdelphij{
207296341Sdelphij    int i, retries = 5;
208215697Ssimon    unsigned char val;
209296341Sdelphij
210215697Ssimon    /* Enable interrupts and keyboard controller */
211296341Sdelphij    kbd_wait();
212296341Sdelphij    outb(KB_STAT, KB_WRITE);
213172767Ssimon    kbd_wait();
214279264Sdelphij    outb(KB_DATA, KB_MODE);
215296341Sdelphij
216260404Sdelphij    /* flush any noise in the buffer */
217296341Sdelphij    while (inb(KB_STAT) & KB_BUF_FULL) {
218296341Sdelphij	DELAY(10);
219296341Sdelphij	(void) inb(KB_DATA);
220296341Sdelphij    }
221296341Sdelphij
222296341Sdelphij    /* Reset keyboard hardware */
223296341Sdelphij    while (retries--) {
224296341Sdelphij	kbd_wait();
225296341Sdelphij	outb(KB_DATA, KB_RESET);
226296341Sdelphij	for (i=0; i<100000; i++) {
227296341Sdelphij	    DELAY(10);
228296341Sdelphij	    val = inb(KB_DATA);
229160814Ssimon	    if (val == KB_ACK || val == KB_ECHO)
230279264Sdelphij		goto gotres;
231279264Sdelphij	    if (val == KB_RESEND)
232296341Sdelphij		break;
233296341Sdelphij	}
234296341Sdelphij    }
235296341Sdelphijgotres:
236296341Sdelphij    if (!retries)
237160814Ssimon	printf("scprobe: keyboard won't accept RESET command\n");
238296341Sdelphij    else {
239296341Sdelphijgotack:
240296341Sdelphij	DELAY(10);
241296341Sdelphij	while ((inb(KB_STAT) & KB_BUF_FULL) == 0) DELAY(10);
242296341Sdelphij	DELAY(10);
243160814Ssimon	val = inb(KB_DATA);
244296341Sdelphij	if (val == KB_ACK)
245296341Sdelphij	    goto gotack;
246296341Sdelphij	if (val != KB_RESET_DONE)
247296341Sdelphij	    printf("scprobe: keyboard RESET failed %02x\n", val);
248296341Sdelphij    }
249296341Sdelphij#ifdef XT_KEYBOARD
250296341Sdelphij    kbd_wait();
251296341Sdelphij    outb(KB_DATA, 0xF0);
252296341Sdelphij    kbd_wait();
253296341Sdelphij    outb(KB_DATA, 1);
254296341Sdelphij    kbd_wait();
255296341Sdelphij#endif /* XT_KEYBOARD */
256296341Sdelphij    return (IO_KBDSIZE);
257296341Sdelphij}
258279264Sdelphij
259160814Ssimonstatic struct kern_devconf kdc_sc[NSC] = {
260296341Sdelphij    0, 0, 0,        		/* filled in by dev_attach */
261296341Sdelphij    "sc", 0, { MDDT_ISA, 0, "tty" },
262296341Sdelphij    isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
263296341Sdelphij    &kdc_isa0,      		/* parent */
264279264Sdelphij    0,          		/* parentdata */
265296341Sdelphij    DC_BUSY,        		/* the console is almost always busy */
266296341Sdelphij    "Graphics console",
267296341Sdelphij    DC_CLS_DISPLAY		/* class */
268296341Sdelphij};
269296341Sdelphij
270160814Ssimonstatic inline void
271296341Sdelphijsc_registerdev(struct isa_device *id)
272296341Sdelphij{
273160814Ssimon    if(id->id_unit)
274296341Sdelphij	kdc_sc[id->id_unit] = kdc_sc[0];
275296341Sdelphij    kdc_sc[id->id_unit].kdc_unit = id->id_unit;
276160814Ssimon    kdc_sc[id->id_unit].kdc_isa = id;
277296341Sdelphij    dev_attach(&kdc_sc[id->id_unit]);
278296341Sdelphij}
279296341Sdelphij
280296341Sdelphij#if NAPM > 0
281160814Ssimonstatic int
282296341Sdelphijscresume(void *dummy)
283296341Sdelphij{
284296341Sdelphij	shfts = 0;
285296341Sdelphij	ctls = 0;
286205128Ssimon	alts = 0;
287296341Sdelphij	agrs = 0;
288296341Sdelphij	metas = 0;
289296341Sdelphij	return 0;
290296341Sdelphij}
291296341Sdelphij#endif
292205128Ssimon
293296341Sdelphijstatic int
294296341Sdelphijscattach(struct isa_device *dev)
295296341Sdelphij{
296296341Sdelphij    scr_stat	*scp;
297296341Sdelphij#ifdef DEVFS
298296341Sdelphij    int		vc;
299296341Sdelphij#endif
300296341Sdelphij
301296341Sdelphij    scinit();
302160814Ssimon    configuration = dev->id_flags;
303296341Sdelphij
304296341Sdelphij    scp = console[0];
305296341Sdelphij
306296341Sdelphij    if (crtc_vga) {
307296341Sdelphij	font_8 = (char *)malloc(8*256, M_DEVBUF, M_NOWAIT);
308296341Sdelphij	font_14 = (char *)malloc(14*256, M_DEVBUF, M_NOWAIT);
309296341Sdelphij	font_16 = (char *)malloc(16*256, M_DEVBUF, M_NOWAIT);
310296341Sdelphij	copy_font(SAVE, FONT_16, font_16);
311296341Sdelphij	fonts_loaded = FONT_16;
312296341Sdelphij	scp->font = FONT_16;
313296341Sdelphij	save_palette();
314296341Sdelphij    }
315296341Sdelphij
316296341Sdelphij    scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
317296341Sdelphij				     M_DEVBUF, M_NOWAIT);
318160814Ssimon    /* copy screen to buffer */
319296341Sdelphij    bcopyw(Crtat, scp->scr_buf, scp->xsize * scp->ysize * sizeof(u_short));
320296341Sdelphij    scp->cursor_pos = scp->scr_buf + scp->xpos + scp->ypos * scp->xsize;
321296341Sdelphij    scp->mouse_pos = scp->scr_buf;
322296341Sdelphij
323296341Sdelphij    /* initialize history buffer & pointers */
324296341Sdelphij    scp->history_head = scp->history_pos = scp->history =
325296341Sdelphij	(u_short *)malloc(scp->history_size*sizeof(u_short),
326160814Ssimon			  M_DEVBUF, M_NOWAIT);
327296341Sdelphij    bzero(scp->history_head, scp->history_size*sizeof(u_short));
328296341Sdelphij
329296341Sdelphij    /* initialize cursor stuff */
330160814Ssimon    draw_cursor(scp, TRUE);
331296341Sdelphij    if (crtc_vga && (configuration & CHAR_CURSOR))
332296341Sdelphij	set_destructive_cursor(scp, TRUE);
333296341Sdelphij
334296341Sdelphij    /* get screen update going */
335296341Sdelphij    scrn_timer();
336296341Sdelphij
337296341Sdelphij    update_leds(scp->status);
338296341Sdelphij    sc_registerdev(dev);
339160814Ssimon
340296341Sdelphij    printf("sc%d: ", dev->id_unit);
341296341Sdelphij    if (crtc_vga)
342296341Sdelphij	if (crtc_addr == MONO_BASE)
343296341Sdelphij	    printf("VGA mono");
344296341Sdelphij	else
345296341Sdelphij	    printf("VGA color");
346296341Sdelphij    else
347296341Sdelphij	if (crtc_addr == MONO_BASE)
348296341Sdelphij	    printf("MDA/hercules");
349172767Ssimon	else
350296341Sdelphij	    printf("CGA/EGA");
351296341Sdelphij    printf(" <%d virtual consoles, flags=0x%x>\n", MAXCONS, configuration);
352296341Sdelphij
353279264Sdelphij#if NAPM > 0
354296341Sdelphij    scp->r_hook.ah_fun = scresume;
355296341Sdelphij    scp->r_hook.ah_arg = NULL;
356296341Sdelphij    scp->r_hook.ah_name = "system keyboard";
357296341Sdelphij    scp->r_hook.ah_order = APM_MID_ORDER;
358296341Sdelphij    apm_hook_establish(APM_HOOK_RESUME , &scp->r_hook);
359296341Sdelphij#endif
360296341Sdelphij
361296341Sdelphij    {
362296341Sdelphij    dev_t dev = makedev(CDEV_MAJOR, 0);
363160814Ssimon
364296341Sdelphij    cdevsw_add(&dev, &scdevsw, NULL);
365296341Sdelphij    }
366296341Sdelphij#ifdef DEVFS
367296341Sdelphij    for (vc = 0; vc < MAXCONS; vc++)
368160814Ssimon        sc_devfs_token[vc] = devfs_add_devswf(&scdevsw, vc, DV_CHR,
369296341Sdelphij					      UID_ROOT, GID_WHEEL, 0600,
370296341Sdelphij					      "ttyv%n", vc);
371296341Sdelphij#endif
372296341Sdelphij
373296341Sdelphij    return 0;
374296341Sdelphij}
375296341Sdelphij
376296341Sdelphijstruct tty
377296341Sdelphij*scdevtotty(dev_t dev)
378296341Sdelphij{
379296341Sdelphij    int unit = minor(dev);
380296341Sdelphij
381296341Sdelphij    if (!init_done)
382296341Sdelphij	return(NULL);
383296341Sdelphij    if (unit > MAXCONS || unit < 0)
384296341Sdelphij	return(NULL);
385296341Sdelphij    if (unit == MAXCONS)
386296341Sdelphij	return CONSOLE_TTY;
387296341Sdelphij    return VIRTUAL_TTY(unit);
388296341Sdelphij}
389296341Sdelphij
390296341Sdelphijstatic scr_stat
391172767Ssimon*get_scr_stat(dev_t dev)
392296341Sdelphij{
393296341Sdelphij    int unit = minor(dev);
394296341Sdelphij
395296341Sdelphij    if (unit > MAXCONS || unit < 0)
396296341Sdelphij	return(NULL);
397172767Ssimon    if (unit == MAXCONS)
398296341Sdelphij	return console[0];
399296341Sdelphij    return console[unit];
400296341Sdelphij}
401296341Sdelphij
402296341Sdelphijstatic int
403296341Sdelphijget_scr_num()
404296341Sdelphij{
405296341Sdelphij    int i = 0;
406296341Sdelphij
407172767Ssimon    while ((i < MAXCONS) && (cur_console != console[i]))
408296341Sdelphij	i++;
409296341Sdelphij    return i < MAXCONS ? i : 0;
410296341Sdelphij}
411296341Sdelphij
412296341Sdelphijint
413296341Sdelphijscopen(dev_t dev, int flag, int mode, struct proc *p)
414296341Sdelphij{
415296341Sdelphij    struct tty *tp = scdevtotty(dev);
416296341Sdelphij
417296341Sdelphij    if (!tp)
418296341Sdelphij	return(ENXIO);
419296341Sdelphij
420296341Sdelphij    tp->t_oproc = scstart;
421296341Sdelphij    tp->t_param = scparam;
422296341Sdelphij    tp->t_dev = dev;
423296341Sdelphij    if (!(tp->t_state & TS_ISOPEN)) {
424172767Ssimon	ttychars(tp);
425296341Sdelphij	tp->t_iflag = TTYDEF_IFLAG;
426296341Sdelphij	tp->t_oflag = TTYDEF_OFLAG;
427172767Ssimon	tp->t_cflag = TTYDEF_CFLAG;
428296341Sdelphij	tp->t_lflag = TTYDEF_LFLAG;
429296341Sdelphij	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
430296341Sdelphij	scparam(tp, &tp->t_termios);
431296341Sdelphij	ttsetwater(tp);
432296341Sdelphij	(*linesw[tp->t_line].l_modem)(tp, 1);
433160814Ssimon    }
434296341Sdelphij    else
435296341Sdelphij	if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
436172767Ssimon	    return(EBUSY);
437296341Sdelphij    if (!console[minor(dev)])
438296341Sdelphij	console[minor(dev)] = alloc_scp();
439296341Sdelphij    return((*linesw[tp->t_line].l_open)(dev, tp));
440296341Sdelphij}
441296341Sdelphij
442296341Sdelphijint
443296341Sdelphijscclose(dev_t dev, int flag, int mode, struct proc *p)
444296341Sdelphij{
445296341Sdelphij    struct tty *tp = scdevtotty(dev);
446160814Ssimon    struct scr_stat *scp;
447296341Sdelphij
448296341Sdelphij    if (!tp)
449296341Sdelphij	return(ENXIO);
450296341Sdelphij    if (minor(dev) < MAXCONS) {
451160814Ssimon	scp = get_scr_stat(tp->t_dev);
452160814Ssimon	if (scp->status & SWITCH_WAIT_ACQ)
453296341Sdelphij	    wakeup((caddr_t)&scp->smode);
454296341Sdelphij#if not_yet_done
455296341Sdelphij	if (scp == &main_console) {
456296341Sdelphij	    scp->pid = 0;
457296341Sdelphij	    scp->proc = NULL;
458160814Ssimon	    scp->smode.mode = VT_AUTO;
459296341Sdelphij	}
460296341Sdelphij	else {
461296341Sdelphij	    free(scp->scr_buf, M_DEVBUF);
462296341Sdelphij	    free(scp->history, M_DEVBUF);
463296341Sdelphij	    free(scp, M_DEVBUF);
464296341Sdelphij	    console[minor(dev)] = NULL;
465296341Sdelphij	}
466296341Sdelphij#else
467296341Sdelphij	scp->pid = 0;
468296341Sdelphij	scp->proc = NULL;
469296341Sdelphij	scp->smode.mode = VT_AUTO;
470296341Sdelphij#endif
471296341Sdelphij    }
472296341Sdelphij    (*linesw[tp->t_line].l_close)(tp, flag);
473296341Sdelphij    ttyclose(tp);
474296341Sdelphij    return(0);
475172767Ssimon}
476296341Sdelphij
477296341Sdelphijint
478160814Ssimonscread(dev_t dev, struct uio *uio, int flag)
479296341Sdelphij{
480296341Sdelphij    struct tty *tp = scdevtotty(dev);
481296341Sdelphij
482296341Sdelphij    if (!tp)
483296341Sdelphij	return(ENXIO);
484296341Sdelphij    return((*linesw[tp->t_line].l_read)(tp, uio, flag));
485296341Sdelphij}
486296341Sdelphij
487160814Ssimonint
488284295Sdelphijscwrite(dev_t dev, struct uio *uio, int flag)
489284295Sdelphij{
490284295Sdelphij    struct tty *tp = scdevtotty(dev);
491284295Sdelphij
492284295Sdelphij    if (!tp)
493284295Sdelphij	return(ENXIO);
494296341Sdelphij    return((*linesw[tp->t_line].l_write)(tp, uio, flag));
495296341Sdelphij}
496160814Ssimon
497296341Sdelphijvoid
498296341Sdelphijscintr(int unit)
499296341Sdelphij{
500296341Sdelphij    static struct tty *cur_tty;
501296341Sdelphij    int c, len;
502296341Sdelphij    u_char *cp;
503296341Sdelphij
504296341Sdelphij    /* make screensaver happy */
505296341Sdelphij    scrn_time_stamp = time.tv_sec;
506296341Sdelphij    if (scrn_blanked) {
507172767Ssimon	(*current_saver)(FALSE);
508296341Sdelphij	cur_console->start = 0;
509296341Sdelphij	cur_console->end = cur_console->xsize * cur_console->ysize;
510296341Sdelphij    }
511296341Sdelphij
512172767Ssimon    c = scgetc(1);
513296341Sdelphij
514172767Ssimon    cur_tty = VIRTUAL_TTY(get_scr_num());
515296341Sdelphij    if (!(cur_tty->t_state & TS_ISOPEN))
516296341Sdelphij	if (!((cur_tty = CONSOLE_TTY)->t_state & TS_ISOPEN))
517296341Sdelphij	    return;
518160814Ssimon
519296341Sdelphij    switch (c & 0xff00) {
520296341Sdelphij    case 0x0000: /* normal key */
521160814Ssimon	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
522296341Sdelphij	break;
523296341Sdelphij    case NOKEY: /* nothing there */
524296341Sdelphij	break;
525296341Sdelphij    case FKEY:  /* function key, return string */
526296341Sdelphij	if (cp = get_fstr((u_int)c, (u_int *)&len)) {
527160814Ssimon	    while (len-- >  0)
528296341Sdelphij		(*linesw[cur_tty->t_line].l_rint)(*cp++ & 0xFF, cur_tty);
529296341Sdelphij	}
530296341Sdelphij	break;
531296341Sdelphij    case MKEY:  /* meta is active, prepend ESC */
532160814Ssimon	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
533296341Sdelphij	(*linesw[cur_tty->t_line].l_rint)(c & 0xFF, cur_tty);
534296341Sdelphij	break;
535296341Sdelphij    case BKEY:  /* backtab fixed sequence (esc [ Z) */
536160814Ssimon	(*linesw[cur_tty->t_line].l_rint)(0x1b, cur_tty);
537296341Sdelphij	(*linesw[cur_tty->t_line].l_rint)('[', cur_tty);
538296341Sdelphij	(*linesw[cur_tty->t_line].l_rint)('Z', cur_tty);
539296341Sdelphij	break;
540296341Sdelphij    }
541296341Sdelphij}
542160814Ssimon
543296341Sdelphijstatic int
544296341Sdelphijscparam(struct tty *tp, struct termios *t)
545296341Sdelphij{
546296341Sdelphij    tp->t_ispeed = t->c_ispeed;
547160814Ssimon    tp->t_ospeed = t->c_ospeed;
548296341Sdelphij    tp->t_cflag = t->c_cflag;
549296341Sdelphij    return 0;
550296341Sdelphij}
551296341Sdelphij
552296341Sdelphijint
553296341Sdelphijscioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
554296341Sdelphij{
555296341Sdelphij    int i, error;
556296341Sdelphij    struct tty *tp;
557296341Sdelphij    struct trapframe *fp;
558160814Ssimon    scr_stat *scp;
559296341Sdelphij
560296341Sdelphij    tp = scdevtotty(dev);
561296341Sdelphij    if (!tp)
562296341Sdelphij	return ENXIO;
563296341Sdelphij    scp = get_scr_stat(tp->t_dev);
564296341Sdelphij
565296341Sdelphij    switch (cmd) {  		/* process console hardware related ioctl's */
566296341Sdelphij
567296341Sdelphij    case GIO_ATTR:      	/* get current attributes */
568296341Sdelphij	*(int*)data = scp->term.cur_attr;
569296341Sdelphij	return 0;
570296341Sdelphij
571296341Sdelphij    case GIO_COLOR:     	/* is this a color console ? */
572160814Ssimon	if (crtc_addr == COLOR_BASE)
573296341Sdelphij	    *(int*)data = 1;
574296341Sdelphij	else
575160814Ssimon	    *(int*)data = 0;
576296341Sdelphij	return 0;
577296341Sdelphij
578296341Sdelphij    case CONS_CURRENT:  	/* get current adapter type */
579296341Sdelphij	if (crtc_vga)
580296341Sdelphij	    *(int*)data = KD_VGA;
581296341Sdelphij	else
582296341Sdelphij	    if (crtc_addr == MONO_BASE)
583296341Sdelphij		*(int*)data = KD_MONO;
584296341Sdelphij	    else
585296341Sdelphij		*(int*)data = KD_CGA;
586296341Sdelphij	return 0;
587160814Ssimon
588296341Sdelphij    case CONS_GET:      	/* get current video mode */
589306230Sdelphij	*(int*)data = scp->mode;
590306230Sdelphij	return 0;
591306230Sdelphij
592306230Sdelphij    case CONS_BLANKTIME:    	/* set screen saver timeout (0 = no saver) */
593172767Ssimon	scrn_blank_time = *(int*)data;
594306230Sdelphij	return 0;
595160814Ssimon
596306230Sdelphij    case CONS_CURSORTYPE:   	/* set cursor type blink/noblink */
597306230Sdelphij	if ((*(int*)data) & 0x01)
598306230Sdelphij	    configuration |= BLINK_CURSOR;
599306230Sdelphij	else
600306230Sdelphij	    configuration &= ~BLINK_CURSOR;
601306230Sdelphij	if ((*(int*)data) & 0x02) {
602306230Sdelphij	    configuration |= CHAR_CURSOR;
603306230Sdelphij	    set_destructive_cursor(scp, TRUE);
604306230Sdelphij	} else
605306230Sdelphij	    configuration &= ~CHAR_CURSOR;
606306230Sdelphij	return 0;
607296341Sdelphij
608296341Sdelphij    case CONS_BELLTYPE: 	/* set bell type sound/visual */
609296341Sdelphij	if (*data)
610160814Ssimon	    configuration |= VISUAL_BELL;
611296341Sdelphij	else
612296341Sdelphij	    configuration &= ~VISUAL_BELL;
613296341Sdelphij	return 0;
614160814Ssimon
615296341Sdelphij    case CONS_HISTORY:  	/* set history size */
616160814Ssimon	if (*data) {
617296341Sdelphij	    free(scp->history, M_DEVBUF);
618296341Sdelphij	    scp->history_size = *(int*)data;
619296341Sdelphij	    if (scp->history_size < scp->ysize)
620296341Sdelphij		scp->history = NULL;
621296341Sdelphij	    else {
622296341Sdelphij		scp->history_size *= scp->xsize;
623160814Ssimon		scp->history_head = scp->history_pos = scp->history =
624296341Sdelphij		    (u_short *)malloc(scp->history_size*sizeof(u_short),
625296341Sdelphij				      M_DEVBUF, M_WAITOK);
626160814Ssimon		bzero(scp->history_head, scp->history_size*sizeof(u_short));
627296341Sdelphij	    }
628296341Sdelphij	    return 0;
629296341Sdelphij	}
630296341Sdelphij	else
631160814Ssimon	    return EINVAL;
632296341Sdelphij
633296341Sdelphij    case CONS_MOUSECTL:		/* control mouse arrow */
634296341Sdelphij    {
635296341Sdelphij	mouse_info_t *mouse = (mouse_info_t*)data;
636296341Sdelphij	int fontsize;
637296341Sdelphij
638296341Sdelphij	switch (scp->font) {
639160814Ssimon	default:
640296341Sdelphij	case FONT_8:
641296341Sdelphij	    fontsize = 8; break;
642296341Sdelphij	case FONT_14:
643296341Sdelphij	    fontsize = 14; break;
644296341Sdelphij	case FONT_16:
645269686Sjkim	    fontsize = 16; break;
646296341Sdelphij	}
647296341Sdelphij	switch (mouse->operation) {
648296341Sdelphij	case MOUSE_SHOW:
649296341Sdelphij	    if (!(scp->status & MOUSE_ENABLED)) {
650296341Sdelphij		scp->mouse_oldpos = Crtat + (scp->mouse_pos - scp->scr_buf);
651296341Sdelphij		scp->status |= (UPDATE_MOUSE | MOUSE_ENABLED);
652296341Sdelphij	    }
653160814Ssimon	    else
654172767Ssimon		return EINVAL;
655296341Sdelphij	    break;
656296341Sdelphij
657296341Sdelphij	case MOUSE_HIDE:
658296341Sdelphij	    if (scp->status & MOUSE_ENABLED) {
659296341Sdelphij		scp->status &= ~MOUSE_ENABLED;
660296341Sdelphij		scp->status |= UPDATE_MOUSE;
661296341Sdelphij	    }
662215697Ssimon	    else
663296341Sdelphij		return EINVAL;
664296341Sdelphij	    break;
665296341Sdelphij
666215697Ssimon	case MOUSE_MOVEABS:
667296341Sdelphij	    scp->mouse_xpos = mouse->x;
668296341Sdelphij	    scp->mouse_ypos = mouse->y;
669215697Ssimon	    goto set_mouse_pos;
670296341Sdelphij
671296341Sdelphij	case MOUSE_MOVEREL:
672296341Sdelphij	    scp->mouse_xpos += mouse->x;
673296341Sdelphij	    scp->mouse_ypos += mouse->y;
674296341Sdelphijset_mouse_pos:
675215697Ssimon	    if (scp->mouse_xpos < 0)
676296341Sdelphij		scp->mouse_xpos = 0;
677296341Sdelphij	    if (scp->mouse_ypos < 0)
678296341Sdelphij		scp->mouse_ypos = 0;
679296341Sdelphij	    if (scp->mouse_xpos >= scp->xsize*8)
680296341Sdelphij		scp->mouse_xpos = (scp->xsize*8)-1;
681296341Sdelphij	    if (scp->mouse_ypos >= scp->ysize*fontsize)
682296341Sdelphij		scp->mouse_ypos = (scp->ysize*fontsize)-1;
683296341Sdelphij	    scp->mouse_pos = scp->scr_buf +
684296341Sdelphij		(scp->mouse_ypos/fontsize)*scp->xsize + scp->mouse_xpos/8;
685296341Sdelphij	    if (scp->status & MOUSE_ENABLED)
686296341Sdelphij		scp->status |= UPDATE_MOUSE;
687296341Sdelphij	    break;
688296341Sdelphij
689296341Sdelphij	case MOUSE_GETPOS:
690296341Sdelphij	    mouse->x = scp->mouse_xpos;
691215697Ssimon	    mouse->y = scp->mouse_ypos;
692296341Sdelphij	    return 0;
693296341Sdelphij
694296341Sdelphij	default:
695296341Sdelphij	    return EINVAL;
696296341Sdelphij	}
697296341Sdelphij	/* make screensaver happy */
698296341Sdelphij	if (scp == cur_console) {
699267103Sdelphij	    scrn_time_stamp = time.tv_sec;
700296341Sdelphij	    if (scrn_blanked) {
701296341Sdelphij		(*current_saver)(FALSE);
702296341Sdelphij		cur_console->start = 0;
703296341Sdelphij		cur_console->end = cur_console->xsize * cur_console->ysize;
704296341Sdelphij	    }
705296341Sdelphij	}
706296341Sdelphij	return 0;
707296341Sdelphij    }
708296341Sdelphij
709296341Sdelphij    case CONS_GETINFO:  	/* get current (virtual) console info */
710296341Sdelphij    {
711296341Sdelphij	vid_info_t *ptr = (vid_info_t*)data;
712215697Ssimon	if (ptr->size == sizeof(struct vid_info)) {
713296341Sdelphij	    ptr->m_num = get_scr_num();
714296341Sdelphij	    ptr->mv_col = scp->xpos;
715296341Sdelphij	    ptr->mv_row = scp->ypos;
716296341Sdelphij	    ptr->mv_csz = scp->xsize;
717296341Sdelphij	    ptr->mv_rsz = scp->ysize;
718296341Sdelphij	    ptr->mv_norm.fore = (scp->term.std_color & 0x0f00)>>8;
719296341Sdelphij	    ptr->mv_norm.back = (scp->term.std_color & 0xf000)>>12;
720296341Sdelphij	    ptr->mv_rev.fore = (scp->term.rev_color & 0x0f00)>>8;
721215697Ssimon	    ptr->mv_rev.back = (scp->term.rev_color & 0xf000)>>12;
722296341Sdelphij	    ptr->mv_grfc.fore = 0;      /* not supported */
723296341Sdelphij	    ptr->mv_grfc.back = 0;      /* not supported */
724215697Ssimon	    ptr->mv_ovscan = scp->border;
725296341Sdelphij	    ptr->mk_keylock = scp->status & LOCK_KEY_MASK;
726296341Sdelphij	    return 0;
727215697Ssimon	}
728296341Sdelphij	return EINVAL;
729296341Sdelphij    }
730296341Sdelphij
731296341Sdelphij    case CONS_GETVERS:  	/* get version number */
732215697Ssimon	*(int*)data = 0x200;    /* version 2.0 */
733296341Sdelphij	return 0;
734296341Sdelphij
735296341Sdelphij    /* VGA TEXT MODES */
736296341Sdelphij    case SW_VGA_C40x25:
737296341Sdelphij    case SW_VGA_C80x25: case SW_VGA_M80x25:
738296341Sdelphij    case SW_VGA_C80x30: case SW_VGA_M80x30:
739215697Ssimon    case SW_VGA_C80x50: case SW_VGA_M80x50:
740296341Sdelphij    case SW_VGA_C80x60: case SW_VGA_M80x60:
741296341Sdelphij    case SW_B40x25:     case SW_C40x25:
742296341Sdelphij    case SW_B80x25:     case SW_C80x25:
743296341Sdelphij    case SW_ENH_B40x25: case SW_ENH_C40x25:
744296341Sdelphij    case SW_ENH_B80x25: case SW_ENH_C80x25:
745296341Sdelphij    case SW_ENH_B80x43: case SW_ENH_C80x43:
746296341Sdelphij
747296341Sdelphij	if (!crtc_vga || video_mode_ptr == NULL)
748296341Sdelphij	    return ENXIO;
749215697Ssimon	switch (cmd & 0xff) {
750296341Sdelphij	case M_VGA_C80x60: case M_VGA_M80x60:
751215697Ssimon	    if (!(fonts_loaded & FONT_8))
752296341Sdelphij		return EINVAL;
753296341Sdelphij	    scp->xsize = 80;
754296341Sdelphij	    scp->ysize = 60;
755296341Sdelphij	    break;
756296341Sdelphij	case M_VGA_C80x50: case M_VGA_M80x50:
757296341Sdelphij	    if (!(fonts_loaded & FONT_8))
758215697Ssimon		return EINVAL;
759215697Ssimon	    scp->xsize = 80;
760296341Sdelphij	    scp->ysize = 50;
761296341Sdelphij	    break;
762172767Ssimon	case M_ENH_B80x43: case M_ENH_C80x43:
763296341Sdelphij	    if (!(fonts_loaded & FONT_8))
764296341Sdelphij		return EINVAL;
765296341Sdelphij	    scp->xsize = 80;
766296341Sdelphij	    scp->ysize = 43;
767296341Sdelphij	    break;
768160814Ssimon	case M_VGA_C80x30: case M_VGA_M80x30:
769296341Sdelphij	    scp->xsize = 80;
770296341Sdelphij	    scp->ysize = 30;
771160814Ssimon	    break;
772296341Sdelphij	default:
773296341Sdelphij	    if ((cmd & 0xff) > M_VGA_CG320)
774296341Sdelphij		return EINVAL;
775296341Sdelphij	    else
776296341Sdelphij		scp->xsize = *(video_mode_ptr+((cmd&0xff)*64));
777215697Ssimon		scp->ysize = *(video_mode_ptr+((cmd&0xff)*64)+1)+1;
778296341Sdelphij	    break;
779296341Sdelphij	}
780296341Sdelphij	scp->mode = cmd & 0xff;
781296341Sdelphij	scp->status &= ~UNKNOWN_MODE;   /* text mode */
782296341Sdelphij	free(scp->scr_buf, M_DEVBUF);
783296341Sdelphij	scp->scr_buf = (u_short *)malloc(scp->xsize*scp->ysize*sizeof(u_short),
784215697Ssimon					 M_DEVBUF, M_WAITOK);
785296341Sdelphij	if (scp == cur_console)
786296341Sdelphij	    set_mode(scp);
787296341Sdelphij	clear_screen(scp);
788296341Sdelphij	if (tp->t_winsize.ws_col != scp->xsize
789296341Sdelphij	    || tp->t_winsize.ws_row != scp->ysize) {
790296341Sdelphij	    tp->t_winsize.ws_col = scp->xsize;
791296341Sdelphij	    tp->t_winsize.ws_row = scp->ysize;
792296341Sdelphij	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
793296341Sdelphij	}
794296341Sdelphij	return 0;
795160814Ssimon
796296341Sdelphij    /* GRAPHICS MODES */
797296341Sdelphij    case SW_BG320:     case SW_BG640:
798296341Sdelphij    case SW_CG320:     case SW_CG320_D:   case SW_CG640_E:
799296341Sdelphij    case SW_CG640x350: case SW_ENH_CG640:
800296341Sdelphij    case SW_BG640x480: case SW_CG640x480: case SW_VGA_CG320:
801296341Sdelphij
802296341Sdelphij	if (!crtc_vga || video_mode_ptr == NULL)
803296341Sdelphij	    return ENXIO;
804296341Sdelphij	scp->mode = cmd & 0xFF;
805296341Sdelphij	scp->status |= UNKNOWN_MODE;    /* graphics mode */
806296341Sdelphij	scp->xsize = (*(video_mode_ptr + (scp->mode*64))) * 8;
807296341Sdelphij	scp->ysize = (*(video_mode_ptr + (scp->mode*64) + 1) + 1) *
808296341Sdelphij		     (*(video_mode_ptr + (scp->mode*64) + 2));
809160814Ssimon	set_mode(scp);
810296341Sdelphij	/* clear_graphics();*/
811296341Sdelphij
812269686Sjkim	if (tp->t_winsize.ws_xpixel != scp->xsize
813296341Sdelphij	    || tp->t_winsize.ws_ypixel != scp->ysize) {
814296341Sdelphij	    tp->t_winsize.ws_xpixel = scp->xsize;
815296341Sdelphij	    tp->t_winsize.ws_ypixel = scp->ysize;
816160814Ssimon	    pgsignal(tp->t_pgrp, SIGWINCH, 1);
817296341Sdelphij	}
818160814Ssimon	return 0;
819296341Sdelphij
820296341Sdelphij    case VT_SETMODE:    	/* set screen switcher mode */
821296341Sdelphij	bcopy(data, &scp->smode, sizeof(struct vt_mode));
822296341Sdelphij	if (scp->smode.mode == VT_PROCESS) {
823296341Sdelphij	    scp->proc = p;
824296341Sdelphij	    scp->pid = scp->proc->p_pid;
825296341Sdelphij	}
826296341Sdelphij	return 0;
827296341Sdelphij
828296341Sdelphij    case VT_GETMODE:    	/* get screen switcher mode */
829296341Sdelphij	bcopy(&scp->smode, data, sizeof(struct vt_mode));
830160814Ssimon	return 0;
831296341Sdelphij
832296341Sdelphij    case VT_RELDISP:    	/* screen switcher ioctl */
833296341Sdelphij	switch(*data) {
834172767Ssimon	case VT_FALSE:  	/* user refuses to release screen, abort */
835296341Sdelphij	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
836296341Sdelphij		old_scp->status &= ~SWITCH_WAIT_REL;
837296341Sdelphij		switch_in_progress = FALSE;
838296341Sdelphij		return 0;
839296341Sdelphij	    }
840296341Sdelphij	    return EINVAL;
841296341Sdelphij
842296341Sdelphij	case VT_TRUE:   	/* user has released screen, go on */
843296341Sdelphij	    if (scp == old_scp && (scp->status & SWITCH_WAIT_REL)) {
844296341Sdelphij		scp->status &= ~SWITCH_WAIT_REL;
845296341Sdelphij		exchange_scr();
846196474Ssimon		if (new_scp->smode.mode == VT_PROCESS) {
847296341Sdelphij		    new_scp->status |= SWITCH_WAIT_ACQ;
848172767Ssimon		    psignal(new_scp->proc, new_scp->smode.acqsig);
849296341Sdelphij		}
850296341Sdelphij		else
851296341Sdelphij		    switch_in_progress = FALSE;
852296341Sdelphij		return 0;
853296341Sdelphij	    }
854296341Sdelphij	    return EINVAL;
855160814Ssimon
856160814Ssimon	case VT_ACKACQ: 	/* acquire acknowledged, switch completed */
857160814Ssimon	    if (scp == new_scp && (scp->status & SWITCH_WAIT_ACQ)) {
858296341Sdelphij		scp->status &= ~SWITCH_WAIT_ACQ;
859296341Sdelphij		switch_in_progress = FALSE;
860296341Sdelphij		return 0;
861296341Sdelphij	    }
862296341Sdelphij	    return EINVAL;
863160814Ssimon
864296341Sdelphij	default:
865296341Sdelphij	    return EINVAL;
866296341Sdelphij	}
867296341Sdelphij	/* NOT REACHED */
868296341Sdelphij
869296341Sdelphij    case VT_OPENQRY:    	/* return free virtual console */
870296341Sdelphij	for (i = 0; i < MAXCONS; i++) {
871160814Ssimon	    tp = VIRTUAL_TTY(i);
872296341Sdelphij	    if (!(tp->t_state & TS_ISOPEN)) {
873296341Sdelphij		*data = i + 1;
874296341Sdelphij		return 0;
875296341Sdelphij	    }
876296341Sdelphij	}
877296341Sdelphij	return EINVAL;
878296341Sdelphij
879296341Sdelphij    case VT_ACTIVATE:   	/* switch to screen *data */
880296341Sdelphij	return switch_scr(scp, (*data) - 1);
881296341Sdelphij
882296341Sdelphij    case VT_WAITACTIVE: 	/* wait for switch to occur */
883296341Sdelphij	if (*data > MAXCONS || *data < 0)
884296341Sdelphij	    return EINVAL;
885296341Sdelphij	if (minor(dev) == (*data) - 1)
886160814Ssimon	    return 0;
887296341Sdelphij	if (*data == 0) {
888296341Sdelphij	    if (scp == cur_console)
889160814Ssimon		return 0;
890284295Sdelphij	}
891284295Sdelphij	else
892284295Sdelphij	    scp = console[(*data) - 1];
893284295Sdelphij	while ((error=tsleep((caddr_t)&scp->smode, PZERO|PCATCH,
894284295Sdelphij			     "waitvt", 0)) == ERESTART) ;
895284295Sdelphij	return error;
896284295Sdelphij
897284295Sdelphij    case VT_GETACTIVE:
898284295Sdelphij	*data = get_scr_num()+1;
899284295Sdelphij	return 0;
900284295Sdelphij
901284295Sdelphij    case KDENABIO:      	/* allow io operations */
902284295Sdelphij	error = suser(p->p_ucred, &p->p_acflag);
903284295Sdelphij	if (error != 0)
904296341Sdelphij	    return error;
905296341Sdelphij	fp = (struct trapframe *)p->p_md.md_regs;
906296341Sdelphij	fp->tf_eflags |= PSL_IOPL;
907296341Sdelphij	return 0;
908296341Sdelphij
909296341Sdelphij    case KDDISABIO:     	/* disallow io operations (default) */
910296341Sdelphij	fp = (struct trapframe *)p->p_md.md_regs;
911296341Sdelphij	fp->tf_eflags &= ~PSL_IOPL;
912296341Sdelphij	return 0;
913160814Ssimon
914296341Sdelphij    case KDSETMODE:     	/* set current mode of this (virtual) console */
915296341Sdelphij	switch (*data) {
916215697Ssimon	case KD_TEXT:   	/* switch to TEXT (known) mode */
917296341Sdelphij	    /* restore fonts & palette ! */
918296341Sdelphij	    if (crtc_vga) {
919296341Sdelphij		if (fonts_loaded & FONT_8)
920296341Sdelphij		    copy_font(LOAD, FONT_8, font_8);
921296341Sdelphij		if (fonts_loaded & FONT_14)
922296341Sdelphij		    copy_font(LOAD, FONT_14, font_14);
923296341Sdelphij		if (fonts_loaded & FONT_16)
924296341Sdelphij		    copy_font(LOAD, FONT_16, font_16);
925296341Sdelphij		if (configuration & CHAR_CURSOR)
926296341Sdelphij		    set_destructive_cursor(scp, TRUE);
927296341Sdelphij		load_palette();
928296341Sdelphij	    }
929160814Ssimon	    /* FALL THROUGH */
930296341Sdelphij
931296341Sdelphij	case KD_TEXT1:  	/* switch to TEXT (known) mode */
932296341Sdelphij	    /* no restore fonts & palette */
933160814Ssimon	    scp->status &= ~UNKNOWN_MODE;
934296341Sdelphij	    if (crtc_vga && video_mode_ptr)
935296341Sdelphij		set_mode(scp);
936296341Sdelphij	    clear_screen(scp);
937296341Sdelphij	    return 0;
938296341Sdelphij
939296341Sdelphij	case KD_GRAPHICS:	/* switch to GRAPHICS (unknown) mode */
940172767Ssimon	    scp->status |= UNKNOWN_MODE;
941296341Sdelphij	    return 0;
942296341Sdelphij	default:
943160814Ssimon	    return EINVAL;
944296341Sdelphij	}
945296341Sdelphij	/* NOT REACHED */
946296341Sdelphij
947160814Ssimon    case KDGETMODE:     	/* get current mode of this (virtual) console */
948296341Sdelphij	*data = (scp->status & UNKNOWN_MODE) ? KD_GRAPHICS : KD_TEXT;
949296341Sdelphij	return 0;
950296341Sdelphij
951296341Sdelphij    case KDSBORDER:     	/* set border color of this (virtual) console */
952296341Sdelphij	if (!crtc_vga)
953296341Sdelphij	    return ENXIO;
954296341Sdelphij	scp->border = *data;
955296341Sdelphij	if (scp == cur_console)
956296341Sdelphij	    set_border(scp->border);
957296341Sdelphij	return 0;
958296341Sdelphij
959296341Sdelphij    case KDSKBSTATE:    	/* set keyboard state (locks) */
960296341Sdelphij	if (*data >= 0 && *data <= LOCK_KEY_MASK) {
961296341Sdelphij	    scp->status &= ~LOCK_KEY_MASK;
962296341Sdelphij	    scp->status |= *data;
963296341Sdelphij	    if (scp == cur_console)
964296341Sdelphij		update_leds(scp->status);
965296341Sdelphij	    return 0;
966296341Sdelphij	}
967296341Sdelphij	return EINVAL;
968296341Sdelphij
969296341Sdelphij    case KDGKBSTATE:    	/* get keyboard state (locks) */
970296341Sdelphij	*data = scp->status & LOCK_KEY_MASK;
971296341Sdelphij	return 0;
972296341Sdelphij
973296341Sdelphij    case KDSETRAD:      	/* set keyboard repeat & delay rates */
974284295Sdelphij	if (*data & 0x80)
975160814Ssimon	    return EINVAL;
976296341Sdelphij	i = spltty();
977296341Sdelphij	kbd_cmd(KB_SETRAD);
978296341Sdelphij	kbd_cmd(*data);
979296341Sdelphij	splx(i);
980296341Sdelphij	return 0;
981296341Sdelphij
982296341Sdelphij    case KDSKBMODE:     	/* set keyboard mode */
983296341Sdelphij	switch (*data) {
984160814Ssimon	case K_RAW: 		/* switch to RAW scancode mode */
985296341Sdelphij	    scp->status |= KBD_RAW_MODE;
986296341Sdelphij	    return 0;
987296341Sdelphij
988172767Ssimon	case K_XLATE:   	/* switch to XLT ascii mode */
989296341Sdelphij	    if (scp == cur_console && scp->status == KBD_RAW_MODE)
990296341Sdelphij		shfts = ctls = alts = agrs = metas = 0;
991296341Sdelphij	    scp->status &= ~KBD_RAW_MODE;
992160814Ssimon	    return 0;
993160814Ssimon	default:
994296341Sdelphij	    return EINVAL;
995296341Sdelphij	}
996296341Sdelphij	/* NOT REACHED */
997296341Sdelphij
998160814Ssimon    case KDGKBMODE:     	/* get keyboard mode */
999296341Sdelphij	*data = (scp->status & KBD_RAW_MODE) ? K_RAW : K_XLATE;
1000296341Sdelphij	return 0;
1001296341Sdelphij
1002160814Ssimon    case KDMKTONE:      	/* sound the bell */
1003296341Sdelphij	if (*(int*)data)
1004296341Sdelphij	    do_bell(scp, (*(int*)data)&0xffff,
1005296341Sdelphij		    (((*(int*)data)>>16)&0xffff)*hz/1000);
1006296341Sdelphij	else
1007296341Sdelphij	    do_bell(scp, scp->bell_pitch, scp->bell_duration);
1008296341Sdelphij	return 0;
1009296341Sdelphij
1010160814Ssimon    case KIOCSOUND:     	/* make tone (*data) hz */
1011296341Sdelphij	if (scp == cur_console) {
1012296341Sdelphij	    if (*(int*)data) {
1013296341Sdelphij		int pitch = TIMER_FREQ/(*(int*)data);
1014296341Sdelphij
1015296341Sdelphij		/* set command for counter 2, 2 byte write */
1016296341Sdelphij		if (acquire_timer2(TIMER_16BIT|TIMER_SQWAVE))
1017296341Sdelphij		    return EBUSY;
1018296341Sdelphij
1019296341Sdelphij		/* set pitch */
1020296341Sdelphij		outb(TIMER_CNTR2, pitch);
1021296341Sdelphij		outb(TIMER_CNTR2, (pitch>>8));
1022296341Sdelphij
1023205128Ssimon		/* enable counter 2 output to speaker */
1024160814Ssimon		outb(IO_PPI, inb(IO_PPI) | 3);
1025296341Sdelphij	    }
1026296341Sdelphij	    else {
1027296341Sdelphij		/* disable counter 2 output to speaker */
1028296341Sdelphij		outb(IO_PPI, inb(IO_PPI) & 0xFC);
1029296341Sdelphij		release_timer2();
1030160814Ssimon	    }
1031160814Ssimon	}
1032296341Sdelphij	return 0;
1033296341Sdelphij
1034296341Sdelphij    case KDGKBTYPE:     	/* get keyboard type */
1035160814Ssimon	*data = 0;  		/* type not known (yet) */
1036296341Sdelphij	return 0;
1037296341Sdelphij
1038172767Ssimon    case KDSETLED:      	/* set keyboard LED status */
1039296341Sdelphij	if (*data >= 0 && *data <= LED_MASK) {
1040296341Sdelphij	    scp->status &= ~LED_MASK;
1041160814Ssimon	    scp->status |= *data;
1042296341Sdelphij	    if (scp == cur_console)
1043296341Sdelphij		update_leds(scp->status);
1044296341Sdelphij	    return 0;
1045160814Ssimon	}
1046296341Sdelphij	return EINVAL;
1047296341Sdelphij
1048296341Sdelphij    case KDGETLED:      	/* get keyboard LED status */
1049296341Sdelphij	*data = scp->status & LED_MASK;
1050296341Sdelphij	return 0;
1051296341Sdelphij
1052296341Sdelphij    case GETFKEY:       	/* get functionkey string */
1053296341Sdelphij	if (*(u_short*)data < n_fkey_tab) {
1054160814Ssimon	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1055160814Ssimon	    bcopy(&fkey_tab[ptr->keynum].str, ptr->keydef,
1056296341Sdelphij		  fkey_tab[ptr->keynum].len);
1057296341Sdelphij	    ptr->flen = fkey_tab[ptr->keynum].len;
1058160814Ssimon	    return 0;
1059296341Sdelphij	}
1060296341Sdelphij	else
1061296341Sdelphij	    return EINVAL;
1062296341Sdelphij
1063296341Sdelphij    case SETFKEY:       	/* set functionkey string */
1064160814Ssimon	if (*(u_short*)data < n_fkey_tab) {
1065296341Sdelphij	    fkeyarg_t *ptr = (fkeyarg_t*)data;
1066296341Sdelphij	    bcopy(ptr->keydef, &fkey_tab[ptr->keynum].str,
1067296341Sdelphij		  min(ptr->flen, MAXFK));
1068296341Sdelphij	    fkey_tab[ptr->keynum].len = min(ptr->flen, MAXFK);
1069296341Sdelphij	    return 0;
1070172767Ssimon	}
1071296341Sdelphij	else
1072160814Ssimon	    return EINVAL;
1073296341Sdelphij
1074296341Sdelphij    case GIO_SCRNMAP:   	/* get output translation table */
1075160814Ssimon	bcopy(&scr_map, data, sizeof(scr_map));
1076296341Sdelphij	return 0;
1077296341Sdelphij
1078160814Ssimon    case PIO_SCRNMAP:   	/* set output translation table */
1079296341Sdelphij	bcopy(data, &scr_map, sizeof(scr_map));
1080296341Sdelphij	return 0;
1081160814Ssimon
1082296341Sdelphij    case GIO_KEYMAP:    	/* get keyboard translation table */
1083296341Sdelphij	bcopy(&key_map, data, sizeof(key_map));
1084296341Sdelphij	return 0;
1085160814Ssimon
1086205128Ssimon    case PIO_KEYMAP:    	/* set keyboard translation table */
1087296341Sdelphij	bcopy(data, &key_map, sizeof(key_map));
1088296341Sdelphij	return 0;
1089296341Sdelphij
1090205128Ssimon    case PIO_FONT8x8:   	/* set 8x8 dot font */
1091296341Sdelphij	if (!crtc_vga)
1092296341Sdelphij	    return ENXIO;
1093296341Sdelphij	bcopy(data, font_8, 8*256);
1094296341Sdelphij	fonts_loaded |= FONT_8;
1095296341Sdelphij	copy_font(LOAD, FONT_8, font_8);
1096296341Sdelphij	if (configuration & CHAR_CURSOR)
1097296341Sdelphij	    set_destructive_cursor(scp, TRUE);
1098296341Sdelphij	return 0;
1099296341Sdelphij
1100205128Ssimon    case GIO_FONT8x8:   	/* get 8x8 dot font */
1101296341Sdelphij	if (!crtc_vga)
1102296341Sdelphij	    return ENXIO;
1103296341Sdelphij	if (fonts_loaded & FONT_8) {
1104160814Ssimon	    bcopy(font_8, data, 8*256);
1105296341Sdelphij	    return 0;
1106296341Sdelphij	}
1107296341Sdelphij	else
1108296341Sdelphij	    return ENXIO;
1109296341Sdelphij
1110160814Ssimon    case PIO_FONT8x14:  	/* set 8x14 dot font */
1111296341Sdelphij	if (!crtc_vga)
1112296341Sdelphij	    return ENXIO;
1113296341Sdelphij	bcopy(data, font_14, 14*256);
1114296341Sdelphij	fonts_loaded |= FONT_14;
1115296341Sdelphij	copy_font(LOAD, FONT_14, font_14);
1116296341Sdelphij	if (configuration & CHAR_CURSOR)
1117296341Sdelphij	    set_destructive_cursor(scp, TRUE);
1118296341Sdelphij	return 0;
1119238405Sjkim
1120296341Sdelphij    case GIO_FONT8x14:  	/* get 8x14 dot font */
1121296341Sdelphij	if (!crtc_vga)
1122296341Sdelphij	    return ENXIO;
1123296341Sdelphij	if (fonts_loaded & FONT_14) {
1124160814Ssimon	    bcopy(font_14, data, 14*256);
1125296341Sdelphij	    return 0;
1126296341Sdelphij	}
1127296341Sdelphij	else
1128296341Sdelphij	    return ENXIO;
1129296341Sdelphij
1130160814Ssimon    case PIO_FONT8x16:  	/* set 8x16 dot font */
1131296341Sdelphij	if (!crtc_vga)
1132296341Sdelphij	    return ENXIO;
1133296341Sdelphij	bcopy(data, font_16, 16*256);
1134296341Sdelphij	fonts_loaded |= FONT_16;
1135296341Sdelphij	copy_font(LOAD, FONT_16, font_16);
1136296341Sdelphij	if (configuration & CHAR_CURSOR)
1137296341Sdelphij	    set_destructive_cursor(scp, TRUE);
1138296341Sdelphij	return 0;
1139296341Sdelphij
1140296341Sdelphij    case GIO_FONT8x16:  	/* get 8x16 dot font */
1141296341Sdelphij	if (!crtc_vga)
1142296341Sdelphij	    return ENXIO;
1143296341Sdelphij	if (fonts_loaded & FONT_16) {
1144160814Ssimon	    bcopy(font_16, data, 16*256);
1145296341Sdelphij	    return 0;
1146160814Ssimon	}
1147296341Sdelphij	else
1148296341Sdelphij	    return ENXIO;
1149296341Sdelphij    default:
1150296341Sdelphij	break;
1151296341Sdelphij    }
1152160814Ssimon
1153296341Sdelphij    error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1154296341Sdelphij    if (error >= 0)
1155296341Sdelphij	return(error);
1156296341Sdelphij    error = ttioctl(tp, cmd, data, flag);
1157160814Ssimon    if (error >= 0)
1158296341Sdelphij	return(error);
1159296341Sdelphij    return(ENOTTY);
1160296341Sdelphij}
1161296341Sdelphij
1162296341Sdelphijstatic void
1163160814Ssimonscstart(struct tty *tp)
1164296341Sdelphij{
1165296341Sdelphij    struct clist *rbp;
1166296341Sdelphij    int s, len;
1167296341Sdelphij    u_char buf[PCBURST];
1168296341Sdelphij    scr_stat *scp = get_scr_stat(tp->t_dev);
1169296341Sdelphij
1170296341Sdelphij    /* XXX who repeats the call when the above flags are cleared? */
1171238405Sjkim    if (scp->status & SLKED || blink_in_progress)
1172296341Sdelphij	return;
1173296341Sdelphij    s = spltty();
1174238405Sjkim    if (!(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))) {
1175296341Sdelphij	tp->t_state |= TS_BUSY;
1176296341Sdelphij	rbp = &tp->t_outq;
1177238405Sjkim	while (rbp->c_cc) {
1178296341Sdelphij	    len = q_to_b(rbp, buf, PCBURST);
1179296341Sdelphij	    splx(s);
1180296341Sdelphij	    ansi_put(scp, buf, len);
1181296341Sdelphij	    s = spltty();
1182296341Sdelphij	}
1183296341Sdelphij	tp->t_state &= ~TS_BUSY;
1184296341Sdelphij	ttwwakeup(tp);
1185296341Sdelphij    }
1186296341Sdelphij    splx(s);
1187296341Sdelphij}
1188160814Ssimon
1189160814Ssimonvoid
1190296341Sdelphijsccnprobe(struct consdev *cp)
1191296341Sdelphij{
1192296341Sdelphij    struct isa_device *dvp;
1193296341Sdelphij
1194296341Sdelphij    /*
1195160814Ssimon     * Take control if we are the highest priority enabled display device.
1196160814Ssimon     */
1197296341Sdelphij    dvp = find_display();
1198296341Sdelphij    if (dvp != NULL && dvp->id_driver != &scdriver) {
1199160814Ssimon	cp->cn_pri = CN_DEAD;
1200296341Sdelphij	return;
1201296341Sdelphij    }
1202296341Sdelphij
1203296341Sdelphij    /* initialize required fields */
1204296341Sdelphij    cp->cn_dev = makedev(CDEV_MAJOR, MAXCONS);
1205296341Sdelphij    cp->cn_pri = CN_INTERNAL;
1206296341Sdelphij}
1207296341Sdelphij
1208296341Sdelphijvoid
1209296341Sdelphijsccninit(struct consdev *cp)
1210296341Sdelphij{
1211296341Sdelphij    scinit();
1212296341Sdelphij}
1213296341Sdelphij
1214160814Ssimonvoid
1215296341Sdelphijsccnputc(dev_t dev, int c)
1216296341Sdelphij{
1217296341Sdelphij    u_char buf[1];
1218296341Sdelphij    int s;
1219296341Sdelphij    scr_stat *scp = console[0];
1220296341Sdelphij    term_stat save = scp->term;
1221296341Sdelphij
1222160814Ssimon    scp->term = kernel_console;
1223296341Sdelphij    current_default = &kernel_default;
1224160814Ssimon    if (scp->scr_buf == Crtat)
1225296341Sdelphij	draw_cursor(scp, FALSE);
1226296341Sdelphij    buf[0] = c;
1227296341Sdelphij    ansi_put(scp, buf, 1);
1228296341Sdelphij    kernel_console = scp->term;
1229296341Sdelphij    current_default = &user_default;
1230296341Sdelphij    scp->term = save;
1231296341Sdelphij    s = splclock();		/* XXX stop scrn_timer */
1232296341Sdelphij    if (scp == cur_console) {
1233296341Sdelphij	if (scp->scr_buf != Crtat && (scp->start <= scp->end)) {
1234296341Sdelphij	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1235296341Sdelphij		   (1 + scp->end - scp->start) * sizeof(u_short));
1236160814Ssimon	    scp->start = scp->xsize * scp->ysize;
1237296341Sdelphij	    scp->end = 0;
1238296341Sdelphij	    scp->status &= ~CURSOR_SHOWN;
1239160814Ssimon	}
1240296341Sdelphij	draw_cursor(scp, TRUE);
1241296341Sdelphij    }
1242296341Sdelphij    splx(s);
1243296341Sdelphij}
1244296341Sdelphij
1245160814Ssimonint
1246296341Sdelphijsccngetc(dev_t dev)
1247296341Sdelphij{
1248296341Sdelphij    int s = spltty();       /* block scintr while we poll */
1249296341Sdelphij    int c = scgetc(0);
1250296341Sdelphij    splx(s);
1251160814Ssimon    return(c);
1252296341Sdelphij}
1253296341Sdelphij
1254296341Sdelphijint
1255160814Ssimonsccncheckc(dev_t dev)
1256296341Sdelphij{
1257160814Ssimon    return (scgetc(1) & 0xff);
1258296341Sdelphij}
1259296341Sdelphij
1260296341Sdelphijstatic void
1261296341Sdelphijscrn_timer()
1262296341Sdelphij{
1263296341Sdelphij    static int cursor_blinkrate;
1264296341Sdelphij    scr_stat *scp = cur_console;
1265296341Sdelphij
1266296341Sdelphij    /* should we just return ? */
1267160814Ssimon    if ((scp->status&UNKNOWN_MODE) || blink_in_progress || switch_in_progress) {
1268296341Sdelphij	timeout((timeout_func_t)scrn_timer, 0, hz/10);
1269296341Sdelphij	return;
1270296341Sdelphij    }
1271296341Sdelphij
1272296341Sdelphij    if (!scrn_blanked) {
1273296341Sdelphij	/* update screen image */
1274160814Ssimon	if (scp->start <= scp->end) {
1275296341Sdelphij	    bcopyw(scp->scr_buf + scp->start, Crtat + scp->start,
1276296341Sdelphij		   (1 + scp->end - scp->start) * sizeof(u_short));
1277296341Sdelphij	    scp->status &= ~CURSOR_SHOWN;
1278296341Sdelphij	    scp->start = scp->xsize * scp->ysize;
1279296341Sdelphij	    scp->end = 0;
1280296341Sdelphij	}
1281205128Ssimon	/* update "pseudo" mouse arrow */
1282296341Sdelphij	if ((scp->status & MOUSE_ENABLED) && (scp->status & UPDATE_MOUSE))
1283296341Sdelphij	    draw_mouse_image(scp);
1284296341Sdelphij
1285296341Sdelphij	/* update cursor image */
1286296341Sdelphij	if (scp->status & CURSOR_ENABLED)
1287296341Sdelphij	    draw_cursor(scp,
1288296341Sdelphij		!(configuration&BLINK_CURSOR) || !(cursor_blinkrate++&0x04));
1289296341Sdelphij    }
1290296341Sdelphij    if (scrn_blank_time && (time.tv_sec>scrn_time_stamp+scrn_blank_time))
1291160814Ssimon	(*current_saver)(TRUE);
1292296341Sdelphij    timeout((timeout_func_t)scrn_timer, 0, hz/25);
1293296341Sdelphij}
1294296341Sdelphij
1295296341Sdelphijstatic void
1296296341Sdelphijclear_screen(scr_stat *scp)
1297160814Ssimon{
1298296341Sdelphij    move_crsr(scp, 0, 0);
1299296341Sdelphij    fillw(scp->term.cur_color | scr_map[0x20], scp->scr_buf,
1300296341Sdelphij	  scp->xsize * scp->ysize);
1301160814Ssimon    mark_all(scp);
1302160814Ssimon}
1303296341Sdelphij
1304296341Sdelphijstatic int
1305296341Sdelphijswitch_scr(scr_stat *scp, u_int next_scr)
1306160814Ssimon{
1307160814Ssimon    if (switch_in_progress && (cur_console->proc != pfind(cur_console->pid)))
1308160814Ssimon	switch_in_progress = FALSE;
1309296341Sdelphij
1310296341Sdelphij    if (next_scr >= MAXCONS || switch_in_progress ||
1311296341Sdelphij	(cur_console->smode.mode == VT_AUTO
1312296341Sdelphij	 && cur_console->status & UNKNOWN_MODE)) {
1313296341Sdelphij	do_bell(scp, BELL_PITCH, BELL_DURATION);
1314296341Sdelphij	return EINVAL;
1315296341Sdelphij    }
1316296341Sdelphij
1317296341Sdelphij    /* is the wanted virtual console open ? */
1318296341Sdelphij    if (next_scr) {
1319160814Ssimon	struct tty *tp = VIRTUAL_TTY(next_scr);
1320296341Sdelphij	if (!(tp->t_state & TS_ISOPEN)) {
1321296341Sdelphij	    do_bell(scp, BELL_PITCH, BELL_DURATION);
1322296341Sdelphij	    return EINVAL;
1323296341Sdelphij	}
1324160814Ssimon    }
1325296341Sdelphij    /* delay switch if actively updating screen */
1326296341Sdelphij    if (write_in_progress || blink_in_progress) {
1327296341Sdelphij	delayed_next_scr = next_scr+1;
1328296341Sdelphij	return 0;
1329160814Ssimon    }
1330296341Sdelphij    switch_in_progress = TRUE;
1331296341Sdelphij    old_scp = cur_console;
1332296341Sdelphij    new_scp = console[next_scr];
1333296341Sdelphij    wakeup((caddr_t)&new_scp->smode);
1334296341Sdelphij    if (new_scp == old_scp) {
1335296341Sdelphij	switch_in_progress = FALSE;
1336160814Ssimon	delayed_next_scr = FALSE;
1337296341Sdelphij	return 0;
1338296341Sdelphij    }
1339160814Ssimon
1340296341Sdelphij    /* has controlling process died? */
1341296341Sdelphij    if (old_scp->proc && (old_scp->proc != pfind(old_scp->pid)))
1342296341Sdelphij	old_scp->smode.mode = VT_AUTO;
1343296341Sdelphij    if (new_scp->proc && (new_scp->proc != pfind(new_scp->pid)))
1344160814Ssimon	new_scp->smode.mode = VT_AUTO;
1345296341Sdelphij
1346296341Sdelphij    /* check the modes and switch approbiatly */
1347296341Sdelphij    if (old_scp->smode.mode == VT_PROCESS) {
1348160814Ssimon	old_scp->status |= SWITCH_WAIT_REL;
1349296341Sdelphij	psignal(old_scp->proc, old_scp->smode.relsig);
1350296341Sdelphij    }
1351296341Sdelphij    else {
1352296341Sdelphij	exchange_scr();
1353160814Ssimon	if (new_scp->smode.mode == VT_PROCESS) {
1354296341Sdelphij	    new_scp->status |= SWITCH_WAIT_ACQ;
1355296341Sdelphij	    psignal(new_scp->proc, new_scp->smode.acqsig);
1356296341Sdelphij	}
1357296341Sdelphij	else
1358296341Sdelphij	    switch_in_progress = FALSE;
1359296341Sdelphij    }
1360296341Sdelphij    return 0;
1361205128Ssimon}
1362296341Sdelphij
1363160814Ssimonstatic void
1364296341Sdelphijexchange_scr(void)
1365296341Sdelphij{
1366296341Sdelphij    move_crsr(old_scp, old_scp->xpos, old_scp->ypos);
1367296341Sdelphij    cur_console = new_scp;
1368296341Sdelphij    if (old_scp->mode != new_scp->mode || (old_scp->status & UNKNOWN_MODE)){
1369296341Sdelphij	if (crtc_vga && video_mode_ptr)
1370172767Ssimon	    set_mode(new_scp);
1371296341Sdelphij    }
1372296341Sdelphij    move_crsr(new_scp, new_scp->xpos, new_scp->ypos);
1373296341Sdelphij    if ((old_scp->status & UNKNOWN_MODE) && crtc_vga) {
1374296341Sdelphij	if (fonts_loaded & FONT_8)
1375296341Sdelphij	    copy_font(LOAD, FONT_8, font_8);
1376296341Sdelphij	if (fonts_loaded & FONT_14)
1377296341Sdelphij	    copy_font(LOAD, FONT_14, font_14);
1378160814Ssimon	if (fonts_loaded & FONT_16)
1379296341Sdelphij	    copy_font(LOAD, FONT_16, font_16);
1380296341Sdelphij	if (configuration & CHAR_CURSOR)
1381160814Ssimon	    set_destructive_cursor(new_scp, TRUE);
1382296341Sdelphij	load_palette();
1383296341Sdelphij    }
1384296341Sdelphij    if (old_scp->status & KBD_RAW_MODE || new_scp->status & KBD_RAW_MODE)
1385296341Sdelphij	shfts = ctls = alts = agrs = metas = 0;
1386296341Sdelphij    update_leds(new_scp->status);
1387296341Sdelphij    delayed_next_scr = FALSE;
1388172767Ssimon    bcopyw(new_scp->scr_buf, Crtat,
1389296341Sdelphij	   (new_scp->xsize*new_scp->ysize)*sizeof(u_short));
1390296341Sdelphij    new_scp->status &= ~CURSOR_SHOWN;
1391296341Sdelphij}
1392296341Sdelphij
1393296341Sdelphijstatic inline void
1394296341Sdelphijmove_crsr(scr_stat *scp, int x, int y)
1395296341Sdelphij{
1396160814Ssimon    if (x < 0 || y < 0 || x >= scp->xsize || y >= scp->ysize)
1397296341Sdelphij	return;
1398160814Ssimon    scp->xpos = x;
1399296341Sdelphij    scp->ypos = y;
1400296341Sdelphij    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1401296341Sdelphij    scp->cursor_pos = scp->scr_buf + scp->ypos * scp->xsize + scp->xpos;
1402172767Ssimon    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1403296341Sdelphij}
1404296341Sdelphij
1405296341Sdelphijstatic void
1406296341Sdelphijscan_esc(scr_stat *scp, u_char c)
1407296341Sdelphij{
1408296341Sdelphij    static u_char ansi_col[16] =
1409296341Sdelphij	{0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15};
1410296341Sdelphij    int i, n;
1411296341Sdelphij    u_short *src, *dst, count;
1412296341Sdelphij
1413296341Sdelphij    if (scp->term.esc == 1) {
1414296341Sdelphij	switch (c) {
1415296341Sdelphij
1416296341Sdelphij	case '[':   /* Start ESC [ sequence */
1417296341Sdelphij	    scp->term.esc = 2;
1418296341Sdelphij	    scp->term.last_param = -1;
1419296341Sdelphij	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1420160814Ssimon		scp->term.param[i] = 1;
1421160814Ssimon	    scp->term.num_param = 0;
1422160814Ssimon	    return;
1423296341Sdelphij
1424296341Sdelphij	case 'M':   /* Move cursor up 1 line, scroll if at top */
1425296341Sdelphij	    if (scp->ypos > 0)
1426296341Sdelphij		move_crsr(scp, scp->xpos, scp->ypos - 1);
1427160814Ssimon	    else {
1428296341Sdelphij		bcopyw(scp->scr_buf, scp->scr_buf + scp->xsize,
1429296341Sdelphij		       (scp->ysize - 1) * scp->xsize * sizeof(u_short));
1430296341Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1431296341Sdelphij		      scp->scr_buf, scp->xsize);
1432296341Sdelphij    		mark_all(scp);
1433296341Sdelphij	    }
1434172767Ssimon	    break;
1435160814Ssimon#if notyet
1436160814Ssimon	case 'Q':
1437296341Sdelphij	    scp->term.esc = 4;
1438296341Sdelphij	    break;
1439296341Sdelphij#endif
1440160814Ssimon	case 'c':   /* Clear screen & home */
1441296341Sdelphij	    clear_screen(scp);
1442296341Sdelphij	    break;
1443296341Sdelphij	}
1444172767Ssimon    }
1445296341Sdelphij    else if (scp->term.esc == 2) {
1446296341Sdelphij	if (c >= '0' && c <= '9') {
1447296341Sdelphij	    if (scp->term.num_param < MAX_ESC_PAR) {
1448160814Ssimon	    if (scp->term.last_param != scp->term.num_param) {
1449296341Sdelphij		scp->term.last_param = scp->term.num_param;
1450296341Sdelphij		scp->term.param[scp->term.num_param] = 0;
1451172767Ssimon	    }
1452296341Sdelphij	    else
1453296341Sdelphij		scp->term.param[scp->term.num_param] *= 10;
1454296341Sdelphij	    scp->term.param[scp->term.num_param] += c - '0';
1455172767Ssimon	    return;
1456296341Sdelphij	    }
1457296341Sdelphij	}
1458172767Ssimon	scp->term.num_param = scp->term.last_param + 1;
1459296341Sdelphij	switch (c) {
1460296341Sdelphij
1461296341Sdelphij	case ';':
1462296341Sdelphij	    if (scp->term.num_param < MAX_ESC_PAR)
1463296341Sdelphij		return;
1464160814Ssimon	    break;
1465296341Sdelphij
1466296341Sdelphij	case '=':
1467296341Sdelphij	    scp->term.esc = 3;
1468296341Sdelphij	    scp->term.last_param = -1;
1469160814Ssimon	    for (i = scp->term.num_param; i < MAX_ESC_PAR; i++)
1470160814Ssimon		scp->term.param[i] = 1;
1471160814Ssimon	    scp->term.num_param = 0;
1472296341Sdelphij	    return;
1473296341Sdelphij
1474296341Sdelphij	case 'A':   /* up n rows */
1475296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1476160814Ssimon	    move_crsr(scp, scp->xpos, scp->ypos - n);
1477296341Sdelphij	    break;
1478296341Sdelphij
1479296341Sdelphij	case 'B':   /* down n rows */
1480296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1481172767Ssimon	    move_crsr(scp, scp->xpos, scp->ypos + n);
1482296341Sdelphij	    break;
1483296341Sdelphij
1484296341Sdelphij	case 'C':   /* right n columns */
1485172767Ssimon	    n = scp->term.param[0]; if (n < 1) n = 1;
1486296341Sdelphij	    move_crsr(scp, scp->xpos + n, scp->ypos);
1487296341Sdelphij	    break;
1488238405Sjkim
1489238405Sjkim	case 'D':   /* left n columns */
1490296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1491296341Sdelphij	    move_crsr(scp, scp->xpos - n, scp->ypos);
1492238405Sjkim	    break;
1493296341Sdelphij
1494296341Sdelphij	case 'E':   /* cursor to start of line n lines down */
1495296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1496296341Sdelphij	    move_crsr(scp, 0, scp->ypos + n);
1497296341Sdelphij	    break;
1498238405Sjkim
1499296341Sdelphij	case 'F':   /* cursor to start of line n lines up */
1500296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1501296341Sdelphij	    move_crsr(scp, 0, scp->ypos - n);
1502296341Sdelphij	    break;
1503238405Sjkim
1504296341Sdelphij	case 'f':   /* Cursor move */
1505238405Sjkim	case 'H':
1506296341Sdelphij	    if (scp->term.num_param == 0)
1507238405Sjkim		move_crsr(scp, 0, 0);
1508296341Sdelphij	    else if (scp->term.num_param == 2)
1509296341Sdelphij		move_crsr(scp, scp->term.param[1] - 1, scp->term.param[0] - 1);
1510238405Sjkim	    break;
1511238405Sjkim
1512296341Sdelphij	case 'J':   /* Clear all or part of display */
1513296341Sdelphij	    if (scp->term.num_param == 0)
1514296341Sdelphij		n = 0;
1515296341Sdelphij	    else
1516296341Sdelphij		n = scp->term.param[0];
1517296341Sdelphij	    switch (n) {
1518238405Sjkim	    case 0: /* clear form cursor to end of display */
1519296341Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1520296341Sdelphij		      scp->cursor_pos,
1521296341Sdelphij		      scp->scr_buf + scp->xsize * scp->ysize - scp->cursor_pos);
1522296341Sdelphij    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1523264266Sdelphij    		mark_for_update(scp, scp->xsize * scp->ysize);
1524296341Sdelphij		break;
1525296341Sdelphij	    case 1: /* clear from beginning of display to cursor */
1526296341Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1527296341Sdelphij		      scp->scr_buf,
1528296341Sdelphij		      scp->cursor_pos - scp->scr_buf);
1529273399Sdelphij    		mark_for_update(scp, 0);
1530296341Sdelphij    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1531296341Sdelphij		break;
1532296341Sdelphij	    case 2: /* clear entire display */
1533296341Sdelphij		clear_screen(scp);
1534296341Sdelphij		break;
1535238405Sjkim	    }
1536296341Sdelphij	    break;
1537296341Sdelphij
1538296341Sdelphij	case 'K':   /* Clear all or part of line */
1539296341Sdelphij	    if (scp->term.num_param == 0)
1540296341Sdelphij		n = 0;
1541296341Sdelphij	    else
1542238405Sjkim		n = scp->term.param[0];
1543296341Sdelphij	    switch (n) {
1544296341Sdelphij	    case 0: /* clear form cursor to end of line */
1545264266Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1546296341Sdelphij		      scp->cursor_pos,
1547296341Sdelphij		      scp->xsize - scp->xpos);
1548296341Sdelphij    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1549296341Sdelphij    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf +
1550296341Sdelphij				scp->xsize - scp->xpos);
1551296341Sdelphij		break;
1552238405Sjkim	    case 1: /* clear from beginning of line to cursor */
1553296341Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1554296341Sdelphij		      scp->cursor_pos - scp->xpos,
1555296341Sdelphij		      scp->xpos + 1);
1556296341Sdelphij    		mark_for_update(scp, scp->ypos * scp->xsize);
1557296341Sdelphij    		mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1558296341Sdelphij		break;
1559296341Sdelphij	    case 2: /* clear entire line */
1560296341Sdelphij		fillw(scp->term.cur_color | scr_map[0x20],
1561296341Sdelphij		      scp->cursor_pos - scp->xpos,
1562296341Sdelphij		      scp->xsize);
1563238405Sjkim    		mark_for_update(scp, scp->ypos * scp->xsize);
1564296341Sdelphij    		mark_for_update(scp, (scp->ypos + 1) * scp->xsize);
1565238405Sjkim		break;
1566296341Sdelphij	    }
1567296341Sdelphij	    break;
1568296341Sdelphij
1569238405Sjkim	case 'L':   /* Insert n lines */
1570296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1571238405Sjkim	    if (n > scp->ysize - scp->ypos)
1572296341Sdelphij		n = scp->ysize - scp->ypos;
1573296341Sdelphij	    src = scp->scr_buf + scp->ypos * scp->xsize;
1574296341Sdelphij	    dst = src + n * scp->xsize;
1575296341Sdelphij	    count = scp->ysize - (scp->ypos + n);
1576238405Sjkim	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1577296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20], src,
1578296341Sdelphij		  n * scp->xsize);
1579296341Sdelphij	    mark_for_update(scp, scp->ypos * scp->xsize);
1580296341Sdelphij	    mark_for_update(scp, scp->xsize * scp->ysize);
1581296341Sdelphij	    break;
1582238405Sjkim
1583296341Sdelphij	case 'M':   /* Delete n lines */
1584296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1585296341Sdelphij	    if (n > scp->ysize - scp->ypos)
1586296341Sdelphij		n = scp->ysize - scp->ypos;
1587296341Sdelphij	    dst = scp->scr_buf + scp->ypos * scp->xsize;
1588296341Sdelphij	    src = dst + n * scp->xsize;
1589238405Sjkim	    count = scp->ysize - (scp->ypos + n);
1590296341Sdelphij	    bcopyw(src, dst, count * scp->xsize * sizeof(u_short));
1591296341Sdelphij	    src = dst + count * scp->xsize;
1592238405Sjkim	    fillw(scp->term.cur_color | scr_map[0x20], src,
1593296341Sdelphij		  n * scp->xsize);
1594296341Sdelphij	    mark_for_update(scp, scp->ypos * scp->xsize);
1595296341Sdelphij	    mark_for_update(scp, scp->xsize * scp->ysize);
1596296341Sdelphij	    break;
1597296341Sdelphij
1598296341Sdelphij	case 'P':   /* Delete n chars */
1599238405Sjkim	    n = scp->term.param[0]; if (n < 1) n = 1;
1600296341Sdelphij	    if (n > scp->xsize - scp->xpos)
1601296341Sdelphij		n = scp->xsize - scp->xpos;
1602296341Sdelphij	    dst = scp->cursor_pos;
1603296341Sdelphij	    src = dst + n;
1604296341Sdelphij	    count = scp->xsize - (scp->xpos + n);
1605296341Sdelphij	    bcopyw(src, dst, count * sizeof(u_short));
1606238405Sjkim	    src = dst + count;
1607296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1608296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1609296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1610296341Sdelphij	    break;
1611296341Sdelphij
1612238405Sjkim	case '@':   /* Insert n chars */
1613296341Sdelphij	    n = scp->term.param[0]; if (n < 1) n = 1;
1614296341Sdelphij	    if (n > scp->xsize - scp->xpos)
1615296341Sdelphij		n = scp->xsize - scp->xpos;
1616296341Sdelphij	    src = scp->cursor_pos;
1617296341Sdelphij	    dst = src + n;
1618238405Sjkim	    count = scp->xsize - (scp->xpos + n);
1619296341Sdelphij	    bcopyw(src, dst, count * sizeof(u_short));
1620296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20], src, n);
1621296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1622296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n + count);
1623296341Sdelphij	    break;
1624238405Sjkim
1625296341Sdelphij	case 'S':   /* scroll up n lines */
1626296341Sdelphij	    n = scp->term.param[0]; if (n < 1)  n = 1;
1627296341Sdelphij	    if (n > scp->ysize)
1628296341Sdelphij		n = scp->ysize;
1629296341Sdelphij	    bcopyw(scp->scr_buf + (scp->xsize * n),
1630296341Sdelphij		   scp->scr_buf,
1631296341Sdelphij		   scp->xsize * (scp->ysize - n) * sizeof(u_short));
1632296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20],
1633296341Sdelphij		  scp->scr_buf + scp->xsize * (scp->ysize - n),
1634296341Sdelphij		  scp->xsize * n);
1635296341Sdelphij    	    mark_all(scp);
1636296341Sdelphij	    break;
1637296341Sdelphij
1638296341Sdelphij	case 'T':   /* scroll down n lines */
1639296341Sdelphij	    n = scp->term.param[0]; if (n < 1)  n = 1;
1640296341Sdelphij	    if (n > scp->ysize)
1641296341Sdelphij		n = scp->ysize;
1642296341Sdelphij	    bcopyw(scp->scr_buf,
1643296341Sdelphij		  scp->scr_buf + (scp->xsize * n),
1644296341Sdelphij		  scp->xsize * (scp->ysize - n) *
1645296341Sdelphij		  sizeof(u_short));
1646296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20],
1647296341Sdelphij		  scp->scr_buf, scp->xsize * n);
1648296341Sdelphij    	    mark_all(scp);
1649296341Sdelphij	    break;
1650238405Sjkim
1651296341Sdelphij	case 'X':   /* erase n characters in line */
1652296341Sdelphij	    n = scp->term.param[0]; if (n < 1)  n = 1;
1653296341Sdelphij	    if (n > scp->xsize - scp->xpos)
1654296341Sdelphij		n = scp->xsize - scp->xpos;
1655296341Sdelphij	    fillw(scp->term.cur_color | scr_map[0x20],
1656296341Sdelphij		  scp->scr_buf + scp->xpos +
1657238405Sjkim		  ((scp->xsize*scp->ypos) * sizeof(u_short)), n);
1658296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf);
1659296341Sdelphij	    mark_for_update(scp, scp->cursor_pos - scp->scr_buf + n);
1660296341Sdelphij	    break;
1661238405Sjkim
1662296341Sdelphij	case 'Z':   /* move n tabs backwards */
1663296341Sdelphij	    n = scp->term.param[0]; if (n < 1)  n = 1;
1664238405Sjkim	    if ((i = scp->xpos & 0xf8) == scp->xpos)
1665296341Sdelphij		i -= 8*n;
1666296341Sdelphij	    else
1667238405Sjkim		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