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>
37 #include <machine/cpufunc.h>
41 #include <dev/vt/hw/vga/vt_vga_reg.h>
46 static int probe_keyboard(void);
48 static void vidc_probe(struct console *cp);
49 static int vidc_init(int arg);
50 static void vidc_putchar(int c);
51 static int vidc_getchar(void);
52 static int vidc_ischar(void);
54 static int vidc_started;
55 static uint16_t *vgatext;
57 static tf_bell_t vidc_cons_bell;
58 static tf_cursor_t vidc_text_cursor;
59 static tf_putchar_t vidc_text_putchar;
60 static tf_fill_t vidc_text_fill;
61 static tf_copy_t vidc_text_copy;
62 static tf_param_t vidc_text_param;
63 static tf_respond_t vidc_cons_respond;
65 static teken_funcs_t tf = {
66 .tf_bell = vidc_cons_bell,
67 .tf_cursor = vidc_text_cursor,
68 .tf_putchar = vidc_text_putchar,
69 .tf_fill = vidc_text_fill,
70 .tf_copy = vidc_text_copy,
71 .tf_param = vidc_text_param,
72 .tf_respond = vidc_cons_respond,
83 static struct text_pixel *buffer;
88 * Between console's palette and VGA's one:
89 * - blue and red are swapped (1 <-> 4)
90 * - yellow and cyan are swapped (3 <-> 6)
92 static const int cons_to_vga_colors[NCOLORS] = {
93 0, 4, 2, 6, 1, 5, 3, 7,
94 8, 12, 10, 14, 9, 13, 11, 15
101 static uint8_t keybuf[KEYBUFSZ]; /* keybuf for extended codes */
103 struct console vidconsole = {
104 .c_name = "vidconsole",
105 .c_desc = "internal video/keyboard",
107 .c_probe = vidc_probe,
109 .c_out = vidc_putchar,
110 .c_in = vidc_getchar,
111 .c_ready = vidc_ischar
115 vga_get_reg(int reg, int index)
117 return (inb(reg + index));
121 vga_get_atr(int reg, int i)
125 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
126 outb(reg + VGA_AC_WRITE, i);
127 ret = inb(reg + VGA_AC_READ);
129 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
135 vga_set_atr(int reg, int i, int v)
137 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
138 outb(reg + VGA_AC_WRITE, i);
139 outb(reg + VGA_AC_WRITE, v);
141 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
145 vga_set_indexed(int reg, int indexreg, int datareg, uint8_t index, uint8_t val)
147 outb(reg + indexreg, index);
148 outb(reg + datareg, val);
152 vga_get_indexed(int reg, int indexreg, int datareg, uint8_t index)
154 outb(reg + indexreg, index);
155 return (inb(reg + datareg));
159 vga_get_crtc(int reg, int i)
161 return (vga_get_indexed(reg, VGA_CRTC_ADDRESS, VGA_CRTC_DATA, i));
165 vga_set_crtc(int reg, int i, int v)
167 vga_set_indexed(reg, VGA_CRTC_ADDRESS, VGA_CRTC_DATA, i, v);
171 vidc_text_set_cursor(teken_unit_t row, teken_unit_t col, bool visible)
176 msl = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_MAX_SCAN_LINE) & 0x1f;
177 s = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START) & 0xC0;
178 e = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END);
180 if (visible == true) {
181 addr = row * TEXT_COLS + col;
182 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH, addr >> 8);
183 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW,
189 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START, s);
190 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END, e);
194 vidc_text_get_cursor(teken_unit_t *row, teken_unit_t *col)
198 addr = (vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH) << 8) +
199 vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW);
201 *row = addr / TEXT_COLS;
202 *col = addr % TEXT_COLS;
209 vidc_cons_bell(void *s __unused)
214 vidc_text_cursor(void *s __unused, const teken_pos_t *p)
216 teken_unit_t row, col;
218 if (p->tp_col == TEXT_COLS)
223 if (p->tp_row == TEXT_ROWS)
228 vidc_text_set_cursor(row, col, true);
232 * Binary searchable table for Unicode to CP437 conversion.
235 uint16_t unicode_base;
240 static const struct unicp437 cp437table[] = {
241 { 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
242 { 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
243 { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
244 { 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
245 { 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
246 { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
247 { 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
248 { 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
249 { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
250 { 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
251 { 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 },
252 { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
253 { 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 },
254 { 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 },
255 { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
256 { 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
257 { 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
258 { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
259 { 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
260 { 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
261 { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
262 { 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
263 { 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
264 { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
265 { 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
266 { 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
267 { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
268 { 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
269 { 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
270 { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
271 { 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
272 { 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
273 { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
274 { 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
275 { 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
276 { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
277 { 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
278 { 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
279 { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
280 { 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
281 { 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
282 { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
283 { 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
284 { 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
285 { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
286 { 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
287 { 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
288 { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
289 { 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
290 { 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
291 { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
292 { 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
293 { 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
294 { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
295 { 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
296 { 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
297 { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
298 { 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
299 { 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
300 { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
301 { 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
302 { 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
303 { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
304 { 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
305 { 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
306 { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
307 { 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
308 { 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
309 { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
310 { 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
311 { 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
312 { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
313 { 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
314 { 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
315 { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
316 { 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
317 { 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 },
318 { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
319 { 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
320 { 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
321 { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
322 { 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
323 { 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
324 { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
325 { 0x266c, 0x0e, 0x00 }
329 vga_get_cp437(teken_char_t c)
334 max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
336 if (c < cp437table[0].unicode_base ||
337 c > cp437table[max].unicode_base + cp437table[max].length)
341 mid = (min + max) / 2;
342 if (c < cp437table[mid].unicode_base)
344 else if (c > cp437table[mid].unicode_base +
345 cp437table[mid].length)
348 return (c - cp437table[mid].unicode_base +
349 cp437table[mid].cp437_base);
356 vidc_text_printchar(const teken_pos_t *p)
360 struct text_pixel *px;
361 teken_color_t fg, bg, tmp;
367 px = buffer + p->tp_col + p->tp_row * tp.tp_col;
368 fg = teken_256to16(px->a.ta_fgcolor);
369 bg = teken_256to16(px->a.ta_bgcolor);
370 if (px->a.ta_format & TF_BOLD)
372 if (px->a.ta_format & TF_BLINK)
375 if (px->a.ta_format & TF_REVERSE) {
381 attr = (cons_to_vga_colors[bg & 0xf] << 4) |
382 cons_to_vga_colors[fg & 0xf];
383 addr = (struct cgatext *)vgatext + p->tp_col + p->tp_row * tp.tp_col;
384 addr->ch = vga_get_cp437(px->c);
389 vidc_text_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
390 const teken_attr_t *a)
394 idx = p->tp_col + p->tp_row * tp.tp_col;
397 vidc_text_printchar(p);
401 vidc_text_fill(void *s, const teken_rect_t *r, teken_char_t c,
402 const teken_attr_t *a)
405 teken_unit_t row, col;
407 vidc_text_get_cursor(&row, &col);
408 vidc_text_set_cursor(row, col, false);
409 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row;
411 for (p.tp_col = r->tr_begin.tp_col;
412 p.tp_col < r->tr_end.tp_col; p.tp_col++)
413 vidc_text_putchar(s, &p, c, a);
414 vidc_text_set_cursor(row, col, true);
418 vidc_same_pixel(struct text_pixel *px1, struct text_pixel *px2)
420 if (px1->c != px2->c)
423 if (px1->a.ta_format != px2->a.ta_format)
425 if (px1->a.ta_fgcolor != px2->a.ta_fgcolor)
427 if (px1->a.ta_bgcolor != px2->a.ta_bgcolor)
434 vidc_text_copy(void *ptr __unused, const teken_rect_t *r, const teken_pos_t *p)
437 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
439 teken_unit_t row, col;
442 * Copying is a little tricky. We must make sure we do it in
443 * correct order, to make sure we don't overwrite our own data.
446 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
447 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
449 vidc_text_get_cursor(&row, &col);
450 vidc_text_set_cursor(row, col, false);
451 if (p->tp_row < r->tr_begin.tp_row) {
452 /* Copy from bottom to top. */
453 for (y = 0; y < nrow; y++) {
454 d.tp_row = p->tp_row + y;
455 s.tp_row = r->tr_begin.tp_row + y;
456 drow = d.tp_row * tp.tp_col;
457 srow = s.tp_row * tp.tp_col;
458 for (x = 0; x < ncol; x++) {
459 d.tp_col = p->tp_col + x;
460 s.tp_col = r->tr_begin.tp_col + x;
462 if (!vidc_same_pixel(
463 &buffer[d.tp_col + drow],
464 &buffer[s.tp_col + srow])) {
465 buffer[d.tp_col + drow] =
466 buffer[s.tp_col + srow];
467 vidc_text_printchar(&d);
472 /* Copy from top to bottom. */
473 if (p->tp_col < r->tr_begin.tp_col) {
474 /* Copy from right to left. */
475 for (y = nrow - 1; y >= 0; y--) {
476 d.tp_row = p->tp_row + y;
477 s.tp_row = r->tr_begin.tp_row + y;
478 drow = d.tp_row * tp.tp_col;
479 srow = s.tp_row * tp.tp_col;
480 for (x = 0; x < ncol; x++) {
481 d.tp_col = p->tp_col + x;
482 s.tp_col = r->tr_begin.tp_col + x;
484 if (!vidc_same_pixel(
485 &buffer[d.tp_col + drow],
486 &buffer[s.tp_col + srow])) {
487 buffer[d.tp_col + drow] =
488 buffer[s.tp_col + srow];
489 vidc_text_printchar(&d);
494 /* Copy from left to right. */
495 for (y = nrow - 1; y >= 0; y--) {
496 d.tp_row = p->tp_row + y;
497 s.tp_row = r->tr_begin.tp_row + y;
498 drow = d.tp_row * tp.tp_col;
499 srow = s.tp_row * tp.tp_col;
500 for (x = ncol - 1; x >= 0; x--) {
501 d.tp_col = p->tp_col + x;
502 s.tp_col = r->tr_begin.tp_col + x;
504 if (!vidc_same_pixel(
505 &buffer[d.tp_col + drow],
506 &buffer[s.tp_col + srow])) {
507 buffer[d.tp_col + drow] =
508 buffer[s.tp_col + srow];
509 vidc_text_printchar(&d);
515 vidc_text_set_cursor(row, col, true);
519 vidc_text_param(void *s __unused, int cmd, unsigned int value)
521 teken_unit_t row, col;
524 case TP_SETLOCALCURSOR:
526 * 0 means normal (usually block), 1 means hidden, and
527 * 2 means blinking (always block) for compatibility with
528 * syscons. We don't support any changes except hiding,
529 * so must map 2 to 0.
531 value = (value == 1) ? 0 : 1;
534 vidc_text_get_cursor(&row, &col);
536 vidc_text_set_cursor(row, col, true);
538 vidc_text_set_cursor(row, col, false);
541 /* Not yet implemented */
550 vidc_cons_respond(void *s __unused, const void *buf __unused,
556 vidc_probe(struct console *cp)
559 /* look for a keyboard */
561 if (probe_keyboard())
565 cp->c_flags |= C_PRESENTIN;
568 /* XXX for now, always assume we can do BIOS screen output */
569 cp->c_flags |= C_PRESENTOUT;
573 color_name_to_teken(const char *name, int *val)
575 if (strcasecmp(name, "black") == 0) {
579 if (strcasecmp(name, "red") == 0) {
583 if (strcasecmp(name, "green") == 0) {
587 if (strcasecmp(name, "brown") == 0) {
591 if (strcasecmp(name, "blue") == 0) {
595 if (strcasecmp(name, "magenta") == 0) {
599 if (strcasecmp(name, "cyan") == 0) {
603 if (strcasecmp(name, "white") == 0) {
611 vidc_set_colors(struct env_var *ev, int flags, const void *value)
616 const teken_attr_t *ap;
622 if (color_name_to_teken(value, &val)) {
623 snprintf(buf, sizeof (buf), "%d", val);
629 val = (int)strtol(value, &end, 0);
630 if (errno != 0 || *end != '\0') {
631 printf("Allowed values are either ansi color name or "
632 "number from range [0-7].\n");
638 ap = teken_get_defattr(&teken);
640 if (strcmp(ev->ev_name, "teken.fg_color") == 0) {
641 /* is it already set? */
642 if (ap->ta_fgcolor == val)
646 if (strcmp(ev->ev_name, "teken.bg_color") == 0) {
647 /* is it already set? */
648 if (ap->ta_bgcolor == val)
653 /* Improve visibility */
654 if (a.ta_bgcolor == TC_WHITE)
655 a.ta_bgcolor |= TC_LIGHT;
657 env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL);
658 teken_set_defattr(&teken, &a);
665 const teken_attr_t *a;
669 if (vidc_started && arg == 0)
675 * Check Miscellaneous Output Register (Read at 3CCh, Write at 3C2h)
676 * for bit 1 (Input/Output Address Select), which means
677 * color/graphics adapter.
679 if (vga_get_reg(VGA_REG_BASE, VGA_GEN_MISC_OUTPUT_R) & VGA_GEN_MO_IOA)
680 vgatext = (uint16_t *)PTOV(VGA_TXT_BASE);
682 vgatext = (uint16_t *)PTOV(VGA_MEM_BASE + VGA_MEM_SIZE);
684 /* set 16bit colors */
685 val = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
686 val &= ~VGA_AC_MC_BI;
687 val &= ~VGA_AC_MC_ELG;
688 vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, val);
690 tp.tp_row = TEXT_ROWS;
691 tp.tp_col = TEXT_COLS;
692 buffer = malloc(tp.tp_row * tp.tp_col * sizeof(*buffer));
696 snprintf(env, sizeof (env), "%u", tp.tp_row);
697 setenv("LINES", env, 1);
698 snprintf(env, sizeof (env), "%u", tp.tp_col);
699 setenv("COLUMNS", env, 1);
701 teken_init(&teken, &tf, NULL);
702 teken_set_winsize(&teken, &tp);
703 a = teken_get_defattr(&teken);
705 snprintf(env, sizeof(env), "%d", a->ta_fgcolor);
706 env_setenv("teken.fg_color", EV_VOLATILE, env, vidc_set_colors,
708 snprintf(env, sizeof(env), "%d", a->ta_bgcolor);
709 env_setenv("teken.bg_color", EV_VOLATILE, env, vidc_set_colors,
712 for (int row = 0; row < tp.tp_row; row++)
713 for (int col = 0; col < tp.tp_col; col++) {
714 buffer[col + row * tp.tp_col].c = ' ';
715 buffer[col + row * tp.tp_col].a = *a;
718 for (int i = 0; i < 10 && vidc_ischar(); i++)
719 (void) vidc_getchar();
721 return (0); /* XXX reinit? */
725 vidc_biosputchar(int c)
730 v86.eax = 0xe00 | (c & 0xff);
738 unsigned char ch = c;
741 teken_input(&teken, &ch, sizeof (ch));
751 for (i = 0; i < KEYBUFSZ; i++) {
752 if (keybuf[i] != 0) {
764 if ((v86.eax & 0xff) != 0) {
765 return (v86.eax & 0xff);
769 switch (v86.eax & 0xff00) {
770 case 0x4800: /* up */
773 return (0x1b); /* esc */
774 case 0x4b00: /* left */
777 return (0x1b); /* esc */
778 case 0x4d00: /* right */
781 return (0x1b); /* esc */
782 case 0x5000: /* down */
785 return (0x1b); /* esc */
799 for (i = 0; i < KEYBUFSZ; i++) {
800 if (keybuf[i] != 0) {
809 return (!V86_ZR(v86.efl));
814 #define PROBE_MAXRETRY 5
815 #define PROBE_MAXWAIT 400
816 #define IO_DUMMY 0x84
817 #define IO_KBD 0x060 /* 8042 Keyboard */
819 /* selected defines from kbdio.h */
820 #define KBD_STATUS_PORT 4 /* status port, read */
821 #define KBD_DATA_PORT 0 /* data port, read/write
822 * also used as keyboard command
823 * and mouse command port
825 #define KBDC_ECHO 0x00ee
826 #define KBDS_ANY_BUFFER_FULL 0x0001
827 #define KBDS_INPUT_BUFFER_FULL 0x0002
828 #define KBD_ECHO 0x00ee
830 /* 7 microsec delay necessary for some keyboard controllers */
835 * I know this is broken, but no timer is available yet at this stage...
836 * See also comments in `delay1ms()'.
838 inb(IO_DUMMY); inb(IO_DUMMY);
839 inb(IO_DUMMY); inb(IO_DUMMY);
840 inb(IO_DUMMY); inb(IO_DUMMY);
844 * This routine uses an inb to an unused port, the time to execute that
845 * inb is approximately 1.25uS. This value is pretty constant across
846 * all CPU's and all buses, with the exception of some PCI implentations
847 * that do not forward this I/O address to the ISA bus as they know it
848 * is not a valid ISA bus address, those machines execute this inb in
861 * We use the presence/absence of a keyboard to determine whether the internal
862 * console can be used for input.
864 * Perform a simple test on the keyboard; issue the ECHO command and see
865 * if the right answer is returned. We don't do anything as drastic as
866 * full keyboard reset; it will be too troublesome and take too much time.
871 int retry = PROBE_MAXRETRY;
875 while (--retry >= 0) {
876 /* flush any noise */
877 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
879 inb(IO_KBD + KBD_DATA_PORT);
883 /* wait until the controller can accept a command */
884 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
885 if (((i = inb(IO_KBD + KBD_STATUS_PORT))
886 & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL))
889 if (i & KBDS_ANY_BUFFER_FULL) {
891 inb(IO_KBD + KBD_DATA_PORT);
898 /* send the ECHO command */
899 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
901 /* wait for a response */
902 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
903 if (inb(IO_KBD + KBD_STATUS_PORT) &
904 KBDS_ANY_BUFFER_FULL)
912 i = inb(IO_KBD + KBD_DATA_PORT);
913 #ifdef PROBE_KBD_BEBUG
914 printf("probe_keyboard: got 0x%x.\n", i);
917 /* got the right answer */
924 #endif /* KEYBOARD_PROBE */