2 * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
3 * Copyright (c) 1997 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
30 #include <sys/cdefs.h>
33 #include <bootstrap.h>
35 #include <machine/psl.h>
39 #include <machine/cpufunc.h>
41 static int probe_keyboard(void);
43 static void vidc_probe(struct console
*cp
);
44 static int vidc_init(struct console
*cp
, int arg
);
45 static void vidc_putchar(struct console
*cp
, int c
);
46 static int vidc_getchar(struct console
*cp
);
47 static int vidc_ischar(struct console
*cp
);
49 static int vidc_started
;
54 #define DEFAULT_FGCOLOR 7
55 #define DEFAULT_BGCOLOR 0
59 void vidc_term_emu(int c
);
60 void get_pos(int *x
, int *y
);
61 void curs_move(int *_x
, int *_y
, int x
, int y
);
62 void write_char(int c
, int fg
, int bg
);
63 void scroll_up(int rows
, int fg
, int bg
);
68 static unsigned keybuf
[KEYBUFSZ
]; /* keybuf for extended codes */
69 static int args
[MAXARGS
], argc
;
70 static int fg_c
, bg_c
, curx
, cury
;
75 struct console text
= {
77 "internal video/keyboard",
88 vidc_probe(struct console
*cp
)
91 /* look for a keyboard */
97 cp
->c_flags
|= C_PRESENTIN
;
100 /* XXX for now, always assume we can do BIOS screen output */
101 cp
->c_flags
|= C_PRESENTOUT
;
105 vidc_init(struct console
*cp
, int arg
)
109 if (vidc_started
&& arg
== 0)
113 /* Init terminal emulator */
115 get_pos(&curx
, &cury
);
116 curs_move(&curx
, &cury
, curx
, cury
);
117 fg_c
= DEFAULT_FGCOLOR
;
118 bg_c
= DEFAULT_BGCOLOR
;
119 memset(keybuf
, 0, KEYBUFSZ
);
121 for (i
= 0; i
< 10 && vidc_ischar(cp
); i
++)
122 (void)vidc_getchar(cp
);
123 return (0); /* XXX reinit? */
127 vidc_biosputchar(int c
)
132 v86
.eax
= 0xe00 | (c
& 0xff);
138 vidc_rawputchar(int c
)
143 /* lame tab expansion */
144 for (i
= 0; i
< 8; i
++)
145 vidc_rawputchar(' ');
150 /* Emulate AH=0eh (teletype output) */
160 if ((text
.c_flags
& C_MODERAW
) == 0)
164 scroll_up(1, fg_c
, bg_c
);
176 curs_move(&curx
, &cury
, curx
, cury
);
178 if ((text
.c_flags
& C_MODERAW
) == 0) {
181 scroll_up(1, fg_c
, bg_c
);
183 curs_move(&curx
, &cury
, curx
, cury
);
186 write_char(c
, fg_c
, bg_c
);
188 if (text
.c_flags
& C_MODERAW
) {
192 scroll_up(1, fg_c
, bg_c
);
198 curs_move(&curx
, &cury
, curx
, cury
);
205 /* Get cursor position on the screen. Result is in edx. Sets
206 * curx and cury appropriately.
209 get_pos(int *x
, int *y
)
217 *x
= v86
.edx
& 0x00ff;
218 *y
= (v86
.edx
& 0xff00) >> 8;
221 /* Move cursor to x rows and y cols (0-based). */
223 curs_move(int *_x
, int *_y
, int x
, int y
)
230 v86
.edx
= ((0x00ff & y
) << 8) + (0x00ff & x
);
236 /* If there is ctrl char at this position, cursor would be invisible.
237 * Make it a space instead.
244 #define isvisible(c) (((c) >= 32) && ((c) < 255))
245 if (!isvisible(v86
.eax
& 0x00ff)) {
246 write_char(' ', fg_c
, bg_c
);
248 v86
.ctl
= 0; /* show normal underline cursor */
255 /* Scroll up the whole window by a number of rows. If rows==0,
256 * clear the window. fg and bg are attributes for the new lines
257 * inserted in the window.
260 scroll_up(int rows
, int fgcol
, int bgcol
)
266 v86
.eax
= 0x0600 + (0x00ff & rows
);
267 v86
.ebx
= (bgcol
<< 12) + (fgcol
<< 8);
273 /* Write character and attribute at cursor position. */
275 write_char(int c
, int fgcol
, int bgcol
)
280 v86
.eax
= 0x0900 + (0x00ff & c
);
281 v86
.ebx
= (bgcol
<< 4) + fgcol
;
286 /**************************************************************/
288 * Screen manipulation functions. They use accumulated data in
289 * args[] and argc variables.
293 /* Clear line from current position to end of line */
300 v86
.ebx
= (bg_c
<< 12) + (fg_c
<< 8);
301 if (direction
== 0) { /* from cursor to end */
302 v86
.ecx
= (cury
<< 8) + curx
;
303 v86
.edx
= (cury
<< 8) + 79;
304 } else if (direction
== 1) { /* from beginning to cursor */
305 v86
.ecx
= (cury
<< 8) + 0;
306 v86
.edx
= (cury
<< 8) + curx
;
307 } else if (direction
== 2) { /* entire line */
308 v86
.ecx
= (cury
<< 8) + 0;
309 v86
.edx
= (cury
<< 8) + 79;
316 /* Clear display from current position to end of screen */
321 get_pos(&curx
, &cury
);
326 v86
.ebx
= (bg_c
<< 12) + (fg_c
<< 8);
327 v86
.ecx
= (cury
<< 8) + curx
;
328 v86
.edx
= (cury
<< 8) + 79;
338 v86
.ebx
= (bg_c
<< 12) + (fg_c
<< 8);
339 v86
.ecx
= (cury
<< 8) + 0;
340 v86
.edx
= (24 << 8) + 79;
345 /* Absolute cursor move to args[0] rows and args[1] columns
346 * (the coordinates are 1-based).
356 curs_move(&curx
, &cury
, args
[1], args
[0]);
360 /* Home cursor (left top corner) */
366 args
[0] = args
[1] = 1;
370 /* Clear internal state of the terminal emulation code */
379 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
387 vidc_rawputchar('\033');
389 vidc_rawputchar(esc
);
390 for (i
= 0; i
<= argc
; ++i
) {
391 sprintf(buf
, "%d", args
[i
]);
394 vidc_rawputchar(*ch
++);
408 args
[argc
] += c
- '0';
411 /* Emulate basic capabilities of sun-color terminal */
415 static int ansi_col
[] = {
416 0, 4, 2, 6, 1, 5, 3, 7,
449 if (argc
< 0) /* XXX */
451 else if (argc
+ 1 >= MAXARGS
)
456 case 'A': /* UP = \E[%dA */
461 args
[0] = y
- args
[0] + 1;
466 case 'B': /* DO = \E[%dB */
471 args
[0] = y
+ args
[0] + 1;
476 case 'C': /* RI = \E[%dC */
480 args
[1] = args
[0] + 1;
486 case 'H': /* ho = \E[H */
494 case 'J': /* cd = \E[J */
518 fg_c
= DEFAULT_FGCOLOR
;
519 bg_c
= DEFAULT_BGCOLOR
;
521 for (i
= 0; i
<= argc
; ++i
) {
523 case 0: /* back to normal */
524 fg_c
= DEFAULT_FGCOLOR
;
525 bg_c
= DEFAULT_BGCOLOR
;
530 case 4: /* underline */
534 case 7: /* reverse */
539 case 30: case 31: case 32: case 33:
540 case 34: case 35: case 36: case 37:
541 fg_c
= ansi_col
[args
[i
] - 30];
543 case 39: /* normal */
544 fg_c
= DEFAULT_FGCOLOR
;
546 case 40: case 41: case 42: case 43:
547 case 44: case 45: case 46: case 47:
548 bg_c
= ansi_col
[args
[i
] - 40];
550 case 49: /* normal */
551 bg_c
= DEFAULT_BGCOLOR
;
574 vidc_putchar(struct console
*cp
, int c
)
584 vidc_getchar(struct console
*cp
)
588 for (i
= 0; i
< KEYBUFSZ
; i
++) {
589 if (keybuf
[i
] != 0) {
596 if (vidc_ischar(cp
)) {
601 if ((v86
.eax
& 0xff) != 0) {
602 return (v86
.eax
& 0xff);
606 switch (v86
.eax
& 0xff00) {
607 case 0x4800: /* up */
610 return (0x1b); /* esc */
611 case 0x4b00: /* left */
614 return (0x1b); /* esc */
615 case 0x4d00: /* right */
618 return (0x1b); /* esc */
619 case 0x5000: /* down */
622 return (0x1b); /* esc */
632 vidc_ischar(struct console
*cp
)
636 for (i
= 0; i
< KEYBUFSZ
; i
++) {
637 if (keybuf
[i
] != 0) {
646 return (!V86_ZR(v86
.efl
));
651 #define PROBE_MAXRETRY 5
652 #define PROBE_MAXWAIT 400
653 #define IO_DUMMY 0x84
654 #define IO_KBD 0x060 /* 8042 Keyboard */
656 /* selected defines from kbdio.h */
657 #define KBD_STATUS_PORT 4 /* status port, read */
658 #define KBD_DATA_PORT 0 /* data port, read/write
659 * also used as keyboard command
660 * and mouse command port
662 #define KBDC_ECHO 0x00ee
663 #define KBDS_ANY_BUFFER_FULL 0x0001
664 #define KBDS_INPUT_BUFFER_FULL 0x0002
665 #define KBD_ECHO 0x00ee
667 /* 7 microsec delay necessary for some keyboard controllers */
672 * I know this is broken, but no timer is available yet at this stage...
673 * See also comments in `delay1ms()'.
675 inb(IO_DUMMY
); inb(IO_DUMMY
);
676 inb(IO_DUMMY
); inb(IO_DUMMY
);
677 inb(IO_DUMMY
); inb(IO_DUMMY
);
681 * This routine uses an inb to an unused port, the time to execute that
682 * inb is approximately 1.25uS. This value is pretty constant across
683 * all CPU's and all buses, with the exception of some PCI implentations
684 * that do not forward this I/O address to the ISA bus as they know it
685 * is not a valid ISA bus address, those machines execute this inb in
698 * We use the presence/absence of a keyboard to determine whether the internal
699 * console can be used for input.
701 * Perform a simple test on the keyboard; issue the ECHO command and see
702 * if the right answer is returned. We don't do anything as drastic as
703 * full keyboard reset; it will be too troublesome and take too much time.
708 int retry
= PROBE_MAXRETRY
;
712 while (--retry
>= 0) {
713 /* flush any noise */
714 while (inb(IO_KBD
+ KBD_STATUS_PORT
) & KBDS_ANY_BUFFER_FULL
) {
716 inb(IO_KBD
+ KBD_DATA_PORT
);
720 /* wait until the controller can accept a command */
721 for (wait
= PROBE_MAXWAIT
; wait
> 0; --wait
) {
722 if (((i
= inb(IO_KBD
+ KBD_STATUS_PORT
))
723 & (KBDS_INPUT_BUFFER_FULL
| KBDS_ANY_BUFFER_FULL
)) == 0)
725 if (i
& KBDS_ANY_BUFFER_FULL
) {
727 inb(IO_KBD
+ KBD_DATA_PORT
);
734 /* send the ECHO command */
735 outb(IO_KBD
+ KBD_DATA_PORT
, KBDC_ECHO
);
737 /* wait for a response */
738 for (wait
= PROBE_MAXWAIT
; wait
> 0; --wait
) {
739 if (inb(IO_KBD
+ KBD_STATUS_PORT
) & KBDS_ANY_BUFFER_FULL
)
747 i
= inb(IO_KBD
+ KBD_DATA_PORT
);
748 #ifdef PROBE_KBD_BEBUG
749 printf("probe_keyboard: got 0x%x.\n", i
);
752 /* got the right answer */
759 #endif /* KEYBOARD_PROBE */