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 * From Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
29 * $FreeBSD: src/sys/boot/i386/libi386/vidconsole.c,v 1.12.2.2 2002/02/11 04:50:11 nyan 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;
54 void vidc_term_emu(int c);
56 void curs_move(int x, int y);
57 void write_char(int c, int fg, int bg);
58 void scroll_up(int rows, int fg, int bg);
67 static int args[2],argc,br;
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);
118 for(i = 0; i < 10 && vidc_ischar(); i++)
119 (void)vidc_getchar();
120 return(0); /* XXX reinit? */
124 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.
203 curx=v86.edx & 0x00ff;
204 cury=(v86.edx & 0xff00)>>8;
207 /* Move cursor to x rows and y cols (0-based). */
209 curs_move(int x, int y)
215 v86.edx = ((0x00ff & y)<<8)+(0x00ff & x);
219 /* If there is ctrl char at this position, cursor would be invisible.
220 * Make it a space instead.
227 #define isvisible(c) (((c)>32) && ((c)<255))
228 if(!isvisible(v86.eax & 0x00ff)) {
229 write_char(' ',fg_c,bg_c);
233 /* Scroll up the whole window by a number of rows. If rows==0,
234 * clear the window. fg and bg are attributes for the new lines
235 * inserted in the window.
238 scroll_up(int rows, int fgcol, int bgcol)
243 v86.eax = 0x0600+(0x00ff & rows);
244 v86.ebx = (bgcol<<12)+(fgcol<<8);
250 /* Write character and attribute at cursor position. */
252 write_char(int c, int fgcol, int bgcol)
256 v86.eax = 0x0900+(0x00ff & c);
257 v86.ebx = (bgcol<<4)+fgcol;
262 /* Calculate power of 10 */
274 /**************************************************************/
276 * Screen manipulation functions. They use accumulated data in
277 * args[] and argc variables.
281 /* Set background color */
288 /* Set foreground color */
296 /* Clear display from current position to end of screen */
304 v86.ebx = (bg_c<<4)+fg_c;
309 curs_move(curx,cury);
313 /* Absolute cursor move to args[0] rows and args[1] columns
314 * (the coordinates are 1-based).
319 if(args[0]>0) args[0]--;
320 if(args[1]>0) args[1]--;
321 curs_move(args[1],args[0]);
325 /* Home cursor (left top corner) */
334 /* Exit attribute mode (reset fore/back-ground colors to defaults) */
343 /* Clear internal state of the terminal emulation code */
354 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
360 if(esc) vidc_rawputchar('\033');
361 if(br) vidc_rawputchar('[');
363 sprintf(buf,"%d",args[0]);
365 while(*ch) vidc_rawputchar(*ch++);
368 vidc_rawputchar(';');
369 sprintf(buf,"%d",args[1]);
371 while(*ch) vidc_rawputchar(*ch++);
378 /* Emulate basic capabilities of cons25 terminal */
392 /* Do ESC sequences processing */
395 /* ESC in ESC sequence - error */
399 /* Check if it's first char after ESC */
407 /* Emulate \E[H (cursor home) and
408 * \E%d;%dH (cursor absolute move) */
415 if(fg) args[0]+=pow10(dig)*3;
416 if(bg) args[0]+=pow10(dig)*4;
425 /* Emulate \EJ (clear to end of screen) */
431 /* perhaps args separator */
432 if(br && (argc>-1)) {
437 /* Change char attributes */
454 /* Carefully collect numeric arguments */
455 /* XXX this is ugly. */
461 /* in case we're in error... */
470 args[argc]=(int)(c-'0');
474 args[argc]=args[argc]*10+(int)(c-'0');
502 return(v86.eax & 0xff);
515 return(!(v86.efl & PSL_Z));
520 #define PROBE_MAXRETRY 5
521 #define PROBE_MAXWAIT 400
522 #define IO_DUMMY 0x84
523 #define IO_KBD 0x060 /* 8042 Keyboard */
525 /* selected defines from kbdio.h */
526 #define KBD_STATUS_PORT 4 /* status port, read */
527 #define KBD_DATA_PORT 0 /* data port, read/write
528 * also used as keyboard command
529 * and mouse command port
531 #define KBDC_ECHO 0x00ee
532 #define KBDS_ANY_BUFFER_FULL 0x0001
533 #define KBDS_INPUT_BUFFER_FULL 0x0002
534 #define KBD_ECHO 0x00ee
536 /* 7 microsec delay necessary for some keyboard controllers */
541 * I know this is broken, but no timer is available yet at this stage...
542 * See also comments in `delay1ms()'.
544 inb(IO_DUMMY); inb(IO_DUMMY);
545 inb(IO_DUMMY); inb(IO_DUMMY);
546 inb(IO_DUMMY); inb(IO_DUMMY);
550 * This routine uses an inb to an unused port, the time to execute that
551 * inb is approximately 1.25uS. This value is pretty constant across
552 * all CPU's and all buses, with the exception of some PCI implentations
553 * that do not forward this I/O address to the ISA bus as they know it
554 * is not a valid ISA bus address, those machines execute this inb in
567 * We use the presence/absence of a keyboard to determine whether the internal
568 * console can be used for input.
570 * Perform a simple test on the keyboard; issue the ECHO command and see
571 * if the right answer is returned. We don't do anything as drastic as
572 * full keyboard reset; it will be too troublesome and take too much time.
577 int retry = PROBE_MAXRETRY;
581 while (--retry >= 0) {
582 /* flush any noise */
583 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
585 inb(IO_KBD + KBD_DATA_PORT);
589 /* wait until the controller can accept a command */
590 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
591 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
592 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
594 if (i & KBDS_ANY_BUFFER_FULL) {
596 inb(IO_KBD + KBD_DATA_PORT);
603 /* send the ECHO command */
604 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
606 /* wait for a response */
607 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
608 if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL)
616 i = inb(IO_KBD + KBD_DATA_PORT);
617 #ifdef PROBE_KBD_BEBUG
618 printf("probe_keyboard: got 0x%x.\n", i);
621 /* got the right answer */
628 #endif /* KEYBOARD_PROBE */