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