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