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