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>
31 __FBSDID("$FreeBSD$");
34 #include <bootstrap.h>
36 #include <machine/psl.h>
40 #include <machine/cpufunc.h>
42 static int probe_keyboard(void);
44 static void vidc_probe(struct console *cp);
45 static int vidc_init(int arg);
46 static void vidc_putchar(int c);
47 static int vidc_getchar(void);
48 static int vidc_ischar(void);
50 static int vidc_started;
52 void get_pos(int *x, int *y);
56 #define DEFAULT_FGCOLOR 7
57 #define DEFAULT_BGCOLOR 0
61 void vidc_term_emu(int c);
62 void curs_move(int *_x, int *_y, int x, int y);
63 void write_char(int c, int fg, int bg);
64 void scroll_up(int rows, int fg, int bg);
69 static int args[MAXARGS], argc;
70 static int fg_c, bg_c, curx, cury;
75 struct console vidconsole = {
77 "internal video/keyboard",
87 vidc_probe(struct console *cp)
90 /* look for a keyboard */
96 cp->c_flags |= C_PRESENTIN;
99 /* XXX for now, always assume we can do BIOS screen output */
100 cp->c_flags |= C_PRESENTOUT;
108 if (vidc_started && arg == 0)
112 /* Init terminal emulator */
114 get_pos(&curx, &cury);
115 curs_move(&curx, &cury, curx, cury);
116 fg_c = DEFAULT_FGCOLOR;
117 bg_c = DEFAULT_BGCOLOR;
119 for (i = 0; i < 10 && vidc_ischar(); i++)
120 (void)vidc_getchar();
121 return (0); /* XXX reinit? */
125 vidc_biosputchar(int c)
130 v86.eax = 0xe00 | (c & 0xff);
136 vidc_rawputchar(int c)
145 get_pos(&curx, %cury);
148 n = 8 - ((curx + 8) % 8);
149 for (i = 0; i < n; i++)
150 vidc_rawputchar(' ');
155 /* Emulate AH=0eh (teletype output) */
162 curs_move(&curx, &cury, curx, cury);
167 scroll_up(1, fg_c, bg_c);
170 curs_move(&curx, &cury, curx, cury);
176 curs_move(&curx, &cury, curx, cury);
177 /* write_char(' ', fg_c, bg_c); XXX destructive(!) */
182 write_char(c, fg_c, bg_c);
190 scroll_up(1, fg_c, bg_c);
194 curs_move(&curx, &cury, curx, cury);
199 /* Get cursor position on the screen. Result is in edx. Sets
200 * curx and cury appropriately.
203 get_pos(int *x, int *y)
211 *x = v86.edx & 0x00ff;
212 *y = (v86.edx & 0xff00) >> 8;
217 /* Move cursor to x rows and y cols (0-based). */
219 curs_move(int *_x, int *_y, int x, int y)
226 v86.edx = ((0x00ff & y) << 8) + (0x00ff & x);
230 /* If there is ctrl char at this position, cursor would be invisible.
231 * Make it a space instead.
238 #define isvisible(c) (((c) >= 32) && ((c) < 255))
239 if (!isvisible(v86.eax & 0x00ff)) {
240 write_char(' ', fg_c, bg_c);
244 /* Scroll up the whole window by a number of rows. If rows==0,
245 * clear the window. fg and bg are attributes for the new lines
246 * inserted in the window.
249 scroll_up(int rows, int fgcol, int bgcol)
256 v86.eax = 0x0600 + (0x00ff & rows);
257 v86.ebx = (bgcol << 12) + (fgcol << 8);
263 /* Write character and attribute at cursor position. */
265 write_char(int c, int fgcol, int bgcol)
270 v86.eax = 0x0900 + (0x00ff & c);
271 v86.ebx = (bgcol << 4) + fgcol;
276 /**************************************************************/
278 * Screen manipulation functions. They use accumulated data in
279 * args[] and argc variables.
283 /* Clear display from current position to end of screen */
288 get_pos(&curx, &cury);
293 v86.ebx = (bg_c << 4) + fg_c;
294 v86.ecx = (cury << 8) + curx;
295 v86.edx = (cury << 8) + 79;
305 v86.ebx = (bg_c << 4) + fg_c;
306 v86.ecx = (cury << 8) + 0;
307 v86.edx = (24 << 8) + 79;
312 /* Absolute cursor move to args[0] rows and args[1] columns
313 * (the coordinates are 1-based).
323 curs_move(&curx, &cury, args[1], args[0]);
327 /* Home cursor (left top corner) */
333 args[0] = args[1] = 1;
337 /* Clear internal state of the terminal emulation code */
346 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
354 vidc_rawputchar('\033');
356 vidc_rawputchar(esc);
357 for (i = 0; i <= argc; ++i) {
358 sprintf(buf, "%d", args[i]);
361 vidc_rawputchar(*ch++);
375 args[argc] += c - '0';
378 /* Emulate basic capabilities of cons25 terminal */
382 static int ansi_col[] = {
383 0, 4, 2, 6, 1, 5, 3, 7,
416 if (argc < 0) /* XXX */
418 else if (argc + 1 >= MAXARGS)
439 fg_c = DEFAULT_FGCOLOR;
440 bg_c = DEFAULT_BGCOLOR;
442 for (i = 0; i <= argc; ++i) {
444 case 0: /* back to normal */
445 fg_c = DEFAULT_FGCOLOR;
446 bg_c = DEFAULT_BGCOLOR;
451 case 4: /* underline */
455 case 7: /* reverse */
460 case 22: /* normal intensity */
463 case 24: /* not underline */
464 case 25: /* not blinking */
467 case 30: case 31: case 32: case 33:
468 case 34: case 35: case 36: case 37:
469 fg_c = ansi_col[args[i] - 30];
471 case 39: /* normal */
472 fg_c = DEFAULT_FGCOLOR;
474 case 40: case 41: case 42: case 43:
475 case 44: case 45: case 46: case 47:
476 bg_c = ansi_col[args[i] - 40];
478 case 49: /* normal */
479 bg_c = DEFAULT_BGCOLOR;
520 return (v86.eax & 0xff);
534 return (!V86_ZR(v86.efl));
539 #define PROBE_MAXRETRY 5
540 #define PROBE_MAXWAIT 400
541 #define IO_DUMMY 0x84
542 #define IO_KBD 0x060 /* 8042 Keyboard */
544 /* selected defines from kbdio.h */
545 #define KBD_STATUS_PORT 4 /* status port, read */
546 #define KBD_DATA_PORT 0 /* data port, read/write
547 * also used as keyboard command
548 * and mouse command port
550 #define KBDC_ECHO 0x00ee
551 #define KBDS_ANY_BUFFER_FULL 0x0001
552 #define KBDS_INPUT_BUFFER_FULL 0x0002
553 #define KBD_ECHO 0x00ee
555 /* 7 microsec delay necessary for some keyboard controllers */
560 * I know this is broken, but no timer is available yet at this stage...
561 * See also comments in `delay1ms()'.
563 inb(IO_DUMMY); inb(IO_DUMMY);
564 inb(IO_DUMMY); inb(IO_DUMMY);
565 inb(IO_DUMMY); inb(IO_DUMMY);
569 * This routine uses an inb to an unused port, the time to execute that
570 * inb is approximately 1.25uS. This value is pretty constant across
571 * all CPU's and all buses, with the exception of some PCI implentations
572 * that do not forward this I/O address to the ISA bus as they know it
573 * is not a valid ISA bus address, those machines execute this inb in
586 * We use the presence/absence of a keyboard to determine whether the internal
587 * console can be used for input.
589 * Perform a simple test on the keyboard; issue the ECHO command and see
590 * if the right answer is returned. We don't do anything as drastic as
591 * full keyboard reset; it will be too troublesome and take too much time.
596 int retry = PROBE_MAXRETRY;
600 while (--retry >= 0) {
601 /* flush any noise */
602 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
604 inb(IO_KBD + KBD_DATA_PORT);
608 /* wait until the controller can accept a command */
609 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
610 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
611 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
613 if (i & KBDS_ANY_BUFFER_FULL) {
615 inb(IO_KBD + KBD_DATA_PORT);
622 /* send the ECHO command */
623 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
625 /* wait for a response */
626 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
627 if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL)
635 i = inb(IO_KBD + KBD_DATA_PORT);
636 #ifdef PROBE_KBD_BEBUG
637 printf("probe_keyboard: got 0x%x.\n", i);
640 /* got the right answer */
647 #endif /* KEYBOARD_PROBE */