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
28 * $FreeBSD: src/sys/boot/i386/libi386/vidconsole.c,v 1.19 2003/08/25 23:28:31 obrien Exp $
29 * $DragonFly: src/sys/boot/i386/libi386/Attic/vidconsole.c,v 1.4 2004/09/09 03:47:08 joerg Exp $
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(int arg);
45 static void vidc_putchar(int c);
46 static int vidc_getchar(void);
47 static int vidc_ischar(void);
49 static int vidc_started;
53 #define DEFAULT_FGCOLOR 7
54 #define DEFAULT_BGCOLOR 0
58 void vidc_term_emu(int c);
60 void curs_move(int x, int y);
61 void write_char(int c, int fg, int bg);
62 void scroll_up(int rows, int fg, int bg);
67 static int args[MAXARGS], argc;
68 static int fg_c, bg_c, curx, cury;
73 struct console vidconsole = {
75 "internal video/keyboard",
85 vidc_probe(struct console *cp)
88 /* look for a keyboard */
94 cp->c_flags |= C_PRESENTIN;
97 /* XXX for now, always assume we can do BIOS screen output */
98 cp->c_flags |= C_PRESENTOUT;
106 if (vidc_started && arg == 0)
110 /* Init terminal emulator */
113 curs_move(curx, cury);
114 fg_c = DEFAULT_FGCOLOR;
115 bg_c = DEFAULT_BGCOLOR;
117 for (i = 0; i < 10 && vidc_ischar(); i++)
118 (void)vidc_getchar();
119 return (0); /* XXX reinit? */
123 vidc_biosputchar(int c)
128 v86.eax = 0xe00 | (c & 0xff);
134 vidc_rawputchar(int c)
139 /* lame tab expansion */
140 for (i = 0; i < 8; i++)
141 vidc_rawputchar(' ');
146 /* Emulate AH=0eh (teletype output) */
153 curs_move(curx, cury);
158 scroll_up(1, fg_c, bg_c);
161 curs_move(curx, cury);
167 curs_move(curx, cury);
168 /* write_char(' ', fg_c, bg_c); XXX destructive(!) */
173 write_char(c, fg_c, bg_c);
181 scroll_up(1, fg_c, bg_c);
185 curs_move(curx, cury);
192 /* Get cursor position on the screen. Result is in edx. Sets
193 * curx and cury appropriately.
204 curx = v86.edx & 0x00ff;
205 cury = (v86.edx & 0xff00) >> 8;
208 /* Move cursor to x rows and y cols (0-based). */
210 curs_move(int x, int y)
217 v86.edx = ((0x00ff & y) << 8) + (0x00ff & x);
221 /* If there is ctrl char at this position, cursor would be invisible.
222 * Make it a space instead.
229 #define isvisible(c) (((c) >= 32) && ((c) < 255))
230 if (!isvisible(v86.eax & 0x00ff)) {
231 write_char(' ', fg_c, bg_c);
235 /* Scroll up the whole window by a number of rows. If rows==0,
236 * clear the window. fg and bg are attributes for the new lines
237 * inserted in the window.
240 scroll_up(int rows, int fgcol, int bgcol)
247 v86.eax = 0x0600 + (0x00ff & rows);
248 v86.ebx = (bgcol << 12) + (fgcol << 8);
254 /* Write character and attribute at cursor position. */
256 write_char(int c, int fgcol, int bgcol)
261 v86.eax = 0x0900 + (0x00ff & c);
262 v86.ebx = (bgcol << 4) + fgcol;
267 /**************************************************************/
269 * Screen manipulation functions. They use accumulated data in
270 * args[] and argc variables.
274 /* Clear display from current position to end of screen */
284 v86.ebx = (bg_c << 4) + fg_c;
285 v86.ecx = (cury << 8) + curx;
286 v86.edx = (cury << 8) + 79;
296 v86.ebx = (bg_c << 4) + fg_c;
297 v86.ecx = (cury << 8) + 0;
298 v86.edx = (24 << 8) + 79;
303 /* Absolute cursor move to args[0] rows and args[1] columns
304 * (the coordinates are 1-based).
314 curs_move(args[1], args[0]);
318 /* Home cursor (left top corner) */
324 args[0] = args[1] = 1;
328 /* Clear internal state of the terminal emulation code */
337 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
345 vidc_rawputchar('\033');
347 vidc_rawputchar(esc);
348 for (i = 0; i <= argc; ++i) {
349 sprintf(buf, "%d", args[i]);
352 vidc_rawputchar(*ch++);
366 args[argc] += c - '0';
369 /* Emulate basic capabilities of cons25 terminal */
373 static int ansi_col[] = {
374 0, 4, 2, 6, 1, 5, 3, 7,
407 if (argc < 0) /* XXX */
409 else if (argc + 1 >= MAXARGS)
430 fg_c = DEFAULT_FGCOLOR;
431 bg_c = DEFAULT_BGCOLOR;
433 for (i = 0; i <= argc; ++i) {
435 case 0: /* back to normal */
436 fg_c = DEFAULT_FGCOLOR;
437 bg_c = DEFAULT_BGCOLOR;
442 case 4: /* underline */
446 case 7: /* reverse */
451 case 30: case 31: case 32: case 33:
452 case 34: case 35: case 36: case 37:
453 fg_c = ansi_col[args[i] - 30];
455 case 39: /* normal */
456 fg_c = DEFAULT_FGCOLOR;
458 case 40: case 41: case 42: case 43:
459 case 44: case 45: case 46: case 47:
460 bg_c = ansi_col[args[i] - 40];
462 case 49: /* normal */
463 bg_c = DEFAULT_BGCOLOR;
504 return (v86.eax & 0xff);
518 return (!(v86.efl & PSL_Z));
523 #define PROBE_MAXRETRY 5
524 #define PROBE_MAXWAIT 400
525 #define IO_DUMMY 0x84
526 #define IO_KBD 0x060 /* 8042 Keyboard */
528 /* selected defines from kbdio.h */
529 #define KBD_STATUS_PORT 4 /* status port, read */
530 #define KBD_DATA_PORT 0 /* data port, read/write
531 * also used as keyboard command
532 * and mouse command port
534 #define KBDC_ECHO 0x00ee
535 #define KBDS_ANY_BUFFER_FULL 0x0001
536 #define KBDS_INPUT_BUFFER_FULL 0x0002
537 #define KBD_ECHO 0x00ee
539 /* 7 microsec delay necessary for some keyboard controllers */
544 * I know this is broken, but no timer is available yet at this stage...
545 * See also comments in `delay1ms()'.
547 inb(IO_DUMMY); inb(IO_DUMMY);
548 inb(IO_DUMMY); inb(IO_DUMMY);
549 inb(IO_DUMMY); inb(IO_DUMMY);
553 * This routine uses an inb to an unused port, the time to execute that
554 * inb is approximately 1.25uS. This value is pretty constant across
555 * all CPU's and all buses, with the exception of some PCI implentations
556 * that do not forward this I/O address to the ISA bus as they know it
557 * is not a valid ISA bus address, those machines execute this inb in
570 * We use the presence/absence of a keyboard to determine whether the internal
571 * console can be used for input.
573 * Perform a simple test on the keyboard; issue the ECHO command and see
574 * if the right answer is returned. We don't do anything as drastic as
575 * full keyboard reset; it will be too troublesome and take too much time.
580 int retry = PROBE_MAXRETRY;
584 while (--retry >= 0) {
585 /* flush any noise */
586 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
588 inb(IO_KBD + KBD_DATA_PORT);
592 /* wait until the controller can accept a command */
593 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
594 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
595 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
597 if (i & KBDS_ANY_BUFFER_FULL) {
599 inb(IO_KBD + KBD_DATA_PORT);
606 /* send the ECHO command */
607 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
609 /* wait for a response */
610 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
611 if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL)
619 i = inb(IO_KBD + KBD_DATA_PORT);
620 #ifdef PROBE_KBD_BEBUG
621 printf("probe_keyboard: got 0x%x.\n", i);
624 /* got the right answer */
631 #endif /* KEYBOARD_PROBE */