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