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