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 $
30 * $DragonFly: src/sys/boot/pc32/libi386/vidconsole.c,v 1.2 2003/06/17 04:28:18 dillon Exp $
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;
55 void vidc_term_emu(int c);
57 void curs_move(int x, int y);
58 void write_char(int c, int fg, int bg);
59 void scroll_up(int rows, int fg, int bg);
68 static int args[2],argc,br;
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 */
115 curs_move(curx,cury);
119 for(i = 0; i < 10 && vidc_ischar(); i++)
120 (void)vidc_getchar();
121 return(0); /* XXX reinit? */
125 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.
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)
216 v86.edx = ((0x00ff & y)<<8)+(0x00ff & x);
220 /* If there is ctrl char at this position, cursor would be invisible.
221 * Make it a space instead.
228 #define isvisible(c) (((c)>32) && ((c)<255))
229 if(!isvisible(v86.eax & 0x00ff)) {
230 write_char(' ',fg_c,bg_c);
234 /* Scroll up the whole window by a number of rows. If rows==0,
235 * clear the window. fg and bg are attributes for the new lines
236 * inserted in the window.
239 scroll_up(int rows, int fgcol, int bgcol)
244 v86.eax = 0x0600+(0x00ff & rows);
245 v86.ebx = (bgcol<<12)+(fgcol<<8);
251 /* Write character and attribute at cursor position. */
253 write_char(int c, int fgcol, int bgcol)
257 v86.eax = 0x0900+(0x00ff & c);
258 v86.ebx = (bgcol<<4)+fgcol;
263 /* Calculate power of 10 */
275 /**************************************************************/
277 * Screen manipulation functions. They use accumulated data in
278 * args[] and argc variables.
282 /* Set background color */
289 /* Set foreground color */
297 /* Clear display from current position to end of screen */
305 v86.ebx = (bg_c<<4)+fg_c;
310 curs_move(curx,cury);
314 /* Absolute cursor move to args[0] rows and args[1] columns
315 * (the coordinates are 1-based).
320 if(args[0]>0) args[0]--;
321 if(args[1]>0) args[1]--;
322 curs_move(args[1],args[0]);
326 /* Home cursor (left top corner) */
335 /* Exit attribute mode (reset fore/back-ground colors to defaults) */
344 /* Clear internal state of the terminal emulation code */
355 /* Gracefully exit ESC-sequence processing in case of misunderstanding */
361 if(esc) vidc_rawputchar('\033');
362 if(br) vidc_rawputchar('[');
364 sprintf(buf,"%d",args[0]);
366 while(*ch) vidc_rawputchar(*ch++);
369 vidc_rawputchar(';');
370 sprintf(buf,"%d",args[1]);
372 while(*ch) vidc_rawputchar(*ch++);
379 /* Emulate basic capabilities of cons25 terminal */
393 /* Do ESC sequences processing */
396 /* ESC in ESC sequence - error */
400 /* Check if it's first char after ESC */
408 /* Emulate \E[H (cursor home) and
409 * \E%d;%dH (cursor absolute move) */
416 if(fg) args[0]+=pow10(dig)*3;
417 if(bg) args[0]+=pow10(dig)*4;
426 /* Emulate \EJ (clear to end of screen) */
432 /* perhaps args separator */
433 if(br && (argc>-1)) {
438 /* Change char attributes */
455 /* Carefully collect numeric arguments */
456 /* XXX this is ugly. */
462 /* in case we're in error... */
471 args[argc]=(int)(c-'0');
475 args[argc]=args[argc]*10+(int)(c-'0');
503 return(v86.eax & 0xff);
516 return(!(v86.efl & PSL_Z));
521 #define PROBE_MAXRETRY 5
522 #define PROBE_MAXWAIT 400
523 #define IO_DUMMY 0x84
524 #define IO_KBD 0x060 /* 8042 Keyboard */
526 /* selected defines from kbdio.h */
527 #define KBD_STATUS_PORT 4 /* status port, read */
528 #define KBD_DATA_PORT 0 /* data port, read/write
529 * also used as keyboard command
530 * and mouse command port
532 #define KBDC_ECHO 0x00ee
533 #define KBDS_ANY_BUFFER_FULL 0x0001
534 #define KBDS_INPUT_BUFFER_FULL 0x0002
535 #define KBD_ECHO 0x00ee
537 /* 7 microsec delay necessary for some keyboard controllers */
542 * I know this is broken, but no timer is available yet at this stage...
543 * See also comments in `delay1ms()'.
545 inb(IO_DUMMY); inb(IO_DUMMY);
546 inb(IO_DUMMY); inb(IO_DUMMY);
547 inb(IO_DUMMY); inb(IO_DUMMY);
551 * This routine uses an inb to an unused port, the time to execute that
552 * inb is approximately 1.25uS. This value is pretty constant across
553 * all CPU's and all buses, with the exception of some PCI implentations
554 * that do not forward this I/O address to the ISA bus as they know it
555 * is not a valid ISA bus address, those machines execute this inb in
568 * We use the presence/absence of a keyboard to determine whether the internal
569 * console can be used for input.
571 * Perform a simple test on the keyboard; issue the ECHO command and see
572 * if the right answer is returned. We don't do anything as drastic as
573 * full keyboard reset; it will be too troublesome and take too much time.
578 int retry = PROBE_MAXRETRY;
582 while (--retry >= 0) {
583 /* flush any noise */
584 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
586 inb(IO_KBD + KBD_DATA_PORT);
590 /* wait until the controller can accept a command */
591 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
592 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
593 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0)
595 if (i & KBDS_ANY_BUFFER_FULL) {
597 inb(IO_KBD + KBD_DATA_PORT);
604 /* send the ECHO command */
605 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
607 /* wait for a response */
608 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
609 if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL)
617 i = inb(IO_KBD + KBD_DATA_PORT);
618 #ifdef PROBE_KBD_BEBUG
619 printf("probe_keyboard: got 0x%x.\n", i);
622 /* got the right answer */
629 #endif /* KEYBOARD_PROBE */