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;
54 #define DEFAULT_FGCOLOR 7
55 #define DEFAULT_BGCOLOR 0
59 void vidc_term_emu(int c);
61 void curs_move(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 int args[MAXARGS], argc;
69 static int fg_c, bg_c, curx, cury;
74 struct console vidconsole = {
76 "internal video/keyboard",
86 vidc_probe(struct console *cp)
89 /* look for a keyboard */
95 cp->c_flags |= C_PRESENTIN;
98 /* XXX for now, always assume we can do BIOS screen output */
99 cp->c_flags |= C_PRESENTOUT;
107 if (vidc_started && arg == 0)
111 /* Init terminal emulator */
114 curs_move(curx, cury);
115 fg_c = DEFAULT_FGCOLOR;
116 bg_c = DEFAULT_BGCOLOR;
118 for (i = 0; i < 10 && vidc_ischar(); i++)
119 (void)vidc_getchar();
120 return (0); /* XXX reinit? */
124 vidc_biosputchar(int c)
129 v86.eax = 0xe00 | (c & 0xff);
135 vidc_rawputchar(int c)
140 /* lame tab expansion */
141 for (i = 0; i < 8; i++)
142 vidc_rawputchar(' ');
147 /* Emulate AH=0eh (teletype output) */
154 curs_move(curx, cury);
159 scroll_up(1, fg_c, bg_c);
162 curs_move(curx, cury);
168 curs_move(curx, cury);
169 /* write_char(' ', fg_c, bg_c); XXX destructive(!) */
174 write_char(c, fg_c, bg_c);
182 scroll_up(1, fg_c, bg_c);
186 curs_move(curx, cury);
193 /* Get cursor position on the screen. Result is in edx. Sets
194 * curx and cury appropriately.
205 curx = v86.edx & 0x00ff;
206 cury = (v86.edx & 0xff00) >> 8;
209 /* Move cursor to x rows and y cols (0-based). */
211 curs_move(int x, int y)
218 v86.edx = ((0x00ff & y) << 8) + (0x00ff & x);
222 /* If there is ctrl char at this position, cursor would be invisible.
223 * Make it a space instead.
230 #define isvisible(c) (((c) >= 32) && ((c) < 255))
231 if (!isvisible(v86.eax & 0x00ff)) {
232 write_char(' ', fg_c, bg_c);
236 /* Scroll up the whole window by a number of rows. If rows==0,
237 * clear the window. fg and bg are attributes for the new lines
238 * inserted in the window.
241 scroll_up(int rows, int fgcol, int bgcol)
248 v86.eax = 0x0600 + (0x00ff & rows);
249 v86.ebx = (bgcol << 12) + (fgcol << 8);
255 /* Write character and attribute at cursor position. */
257 write_char(int c, int fgcol, int bgcol)
262 v86.eax = 0x0900 + (0x00ff & c);
263 v86.ebx = (bgcol << 4) + fgcol;
268 /**************************************************************/
270 * Screen manipulation functions. They use accumulated data in
271 * args[] and argc variables.
275 /* Clear display from current position to end of screen */
285 v86.ebx = (bg_c << 4) + fg_c;
286 v86.ecx = (cury << 8) + curx;
287 v86.edx = (cury << 8) + 79;
297 v86.ebx = (bg_c << 4) + fg_c;
298 v86.ecx = (cury << 8) + 0;
299 v86.edx = (24 << 8) + 79;
304 /* Absolute cursor move to args[0] rows and args[1] columns
305 * (the coordinates are 1-based).
315 curs_move(args[1], args[0]);
319 /* Home cursor (left top corner) */
325 args[0] = args[1] = 1;
329 /* Clear internal state of the terminal emulation code */
338 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
346 vidc_rawputchar('\033');
348 vidc_rawputchar(esc);
349 for (i = 0; i <= argc; ++i) {
350 sprintf(buf, "%d", args[i]);
353 vidc_rawputchar(*ch++);
367 args[argc] += c - '0';
370 /* Emulate basic capabilities of cons25 terminal */
374 static int ansi_col[] = {
375 0, 4, 2, 6, 1, 5, 3, 7,
408 if (argc < 0) /* XXX */
410 else if (argc + 1 >= MAXARGS)
431 fg_c = DEFAULT_FGCOLOR;
432 bg_c = DEFAULT_BGCOLOR;
434 for (i = 0; i <= argc; ++i) {
436 case 0: /* back to normal */
437 fg_c = DEFAULT_FGCOLOR;
438 bg_c = DEFAULT_BGCOLOR;
443 case 4: /* underline */
447 case 7: /* reverse */
452 case 30: case 31: case 32: case 33:
453 case 34: case 35: case 36: case 37:
454 fg_c = ansi_col[args[i] - 30];
456 case 39: /* normal */
457 fg_c = DEFAULT_FGCOLOR;
459 case 40: case 41: case 42: case 43:
460 case 44: case 45: case 46: case 47:
461 bg_c = ansi_col[args[i] - 40];
463 case 49: /* normal */
464 bg_c = DEFAULT_BGCOLOR;
505 return (v86.eax & 0xff);
519 return (!(v86.efl & PSL_Z));
524 #define PROBE_MAXRETRY 5
525 #define PROBE_MAXWAIT 400
526 #define IO_DUMMY 0x84
527 #define IO_KBD 0x060 /* 8042 Keyboard */
529 /* selected defines from kbdio.h */
530 #define KBD_STATUS_PORT 4 /* status port, read */
531 #define KBD_DATA_PORT 0 /* data port, read/write
532 * also used as keyboard command
533 * and mouse command port
535 #define KBDC_ECHO 0x00ee
536 #define KBDS_ANY_BUFFER_FULL 0x0001
537 #define KBDS_INPUT_BUFFER_FULL 0x0002
538 #define KBD_ECHO 0x00ee
540 /* 7 microsec delay necessary for some keyboard controllers */
545 * I know this is broken, but no timer is available yet at this stage...
546 * See also comments in `delay1ms()'.
548 inb(IO_DUMMY); inb(IO_DUMMY);
549 inb(IO_DUMMY); inb(IO_DUMMY);
550 inb(IO_DUMMY); inb(IO_DUMMY);
554 * This routine uses an inb to an unused port, the time to execute that
555 * inb is approximately 1.25uS. This value is pretty constant across
556 * all CPU's and all buses, with the exception of some PCI implentations
557 * that do not forward this I/O address to the ISA bus as they know it
558 * is not a valid ISA bus address, those machines execute this inb in
571 * We use the presence/absence of a keyboard to determine whether the internal
572 * console can be used for input.
574 * Perform a simple test on the keyboard; issue the ECHO command and see
575 * if the right answer is returned. We don't do anything as drastic as
576 * full keyboard reset; it will be too troublesome and take too much time.
581 int retry = PROBE_MAXRETRY;
585 while (--retry >= 0) {
586 /* flush any noise */
587 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
589 inb(IO_KBD + KBD_DATA_PORT);
593 /* wait until the controller can accept a command */
594 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
595 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
596 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
598 if (i & KBDS_ANY_BUFFER_FULL) {
600 inb(IO_KBD + KBD_DATA_PORT);
607 /* send the ECHO command */
608 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
610 /* wait for a response */
611 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
612 if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL)
620 i = inb(IO_KBD + KBD_DATA_PORT);
621 #ifdef PROBE_KBD_BEBUG
622 printf("probe_keyboard: got 0x%x.\n", i);
625 /* got the right answer */
632 #endif /* KEYBOARD_PROBE */