]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/vidconsole.c
zfs: merge openzfs/zfs@e13538856
[FreeBSD/FreeBSD.git] / stand / i386 / libi386 / vidconsole.c
1 /*-
2  * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
3  * Copyright (c) 1997 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  *      Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp
28  */
29
30 #include <sys/cdefs.h>
31 #include <stand.h>
32 #include <sys/param.h>
33 #include <bootstrap.h>
34 #include <btxv86.h>
35 #include <gfx_fb.h>
36 #include <teken.h>
37 #include "vbe.h"
38
39 #include <dev/vt/hw/vga/vt_vga_reg.h>
40
41 #include "libi386.h"
42
43 #if KEYBOARD_PROBE
44 static int      probe_keyboard(void);
45 #endif
46 static void     vidc_probe(struct console *cp);
47 static int      vidc_init(int arg);
48 static void     vidc_putchar(int c);
49 static int      vidc_getchar(void);
50 static int      vidc_ischar(void);
51 static void     cons_draw_frame(teken_attr_t *);
52
53 static bool     vidc_started;
54 static uint16_t *vgatext;
55
56 static tf_bell_t        vidc_cons_bell;
57 static tf_cursor_t      vidc_text_cursor;
58 static tf_putchar_t     vidc_text_putchar;
59 static tf_fill_t        vidc_text_fill;
60 static tf_copy_t        vidc_text_copy;
61 static tf_param_t       vidc_text_param;
62 static tf_respond_t     vidc_cons_respond;
63  
64 static teken_funcs_t tf = {
65         .tf_bell        = vidc_cons_bell,
66         .tf_cursor      = vidc_text_cursor,
67         .tf_putchar     = vidc_text_putchar,
68         .tf_fill        = vidc_text_fill,
69         .tf_copy        = vidc_text_copy,
70         .tf_param       = vidc_text_param,
71         .tf_respond     = vidc_cons_respond,
72 };
73
74 static teken_funcs_t tfx = {
75         .tf_bell        = vidc_cons_bell,
76         .tf_cursor      = gfx_fb_cursor,
77         .tf_putchar     = gfx_fb_putchar,
78         .tf_fill        = gfx_fb_fill,
79         .tf_copy        = gfx_fb_copy,
80         .tf_param       = gfx_fb_param,
81         .tf_respond     = vidc_cons_respond,
82 };
83
84 #define KEYBUFSZ        10
85
86 static uint8_t  keybuf[KEYBUFSZ];       /* keybuf for extended codes */
87
88 struct console vidconsole = {
89         .c_name = "vidconsole",
90         .c_desc = "internal video/keyboard",
91         .c_flags = 0,
92         .c_probe = vidc_probe,
93         .c_init = vidc_init,
94         .c_out = vidc_putchar,
95         .c_in = vidc_getchar,
96         .c_ready = vidc_ischar
97 };
98
99 /*
100  * This function is used to mark a rectangular image area so the scrolling
101  * will know we need to copy the data from there.
102  */
103 void
104 term_image_display(teken_gfx_t *state, const teken_rect_t *r)
105 {
106         teken_pos_t p;
107         int idx;
108
109         if (screen_buffer == NULL)
110                 return;
111
112         for (p.tp_row = r->tr_begin.tp_row;
113             p.tp_row < r->tr_end.tp_row; p.tp_row++) {
114                 for (p.tp_col = r->tr_begin.tp_col;
115                     p.tp_col < r->tr_end.tp_col; p.tp_col++) {
116                         idx = p.tp_col + p.tp_row * state->tg_tp.tp_col;
117                         if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
118                                 return;
119                         screen_buffer[idx].a.ta_format |= TF_IMAGE;
120                 }
121         }
122 }
123
124 static void
125 vidc_text_set_cursor(teken_unit_t row, teken_unit_t col, bool visible)
126 {
127         uint16_t addr;
128         uint8_t msl, s, e;
129
130         msl = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_MAX_SCAN_LINE) & 0x1f;
131         s = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START) & 0xC0;
132         e = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END);
133
134         if (visible == true) {
135                 addr = row * TEXT_COLS + col;
136                 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH, addr >> 8);
137                 vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW,
138                     addr & 0xff);
139                 e = msl;
140         } else {
141                 s |= (1<<5);
142         }
143         vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START, s);
144         vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END, e);
145 }
146
147 static void
148 vidc_text_get_cursor(teken_unit_t *row, teken_unit_t *col)
149 {
150         uint16_t addr;
151
152         addr = (vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH) << 8) +
153             vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW);
154
155         *row = addr / TEXT_COLS;
156         *col = addr % TEXT_COLS;
157 }
158
159 /*
160  * Not implemented.
161  */
162 static void 
163 vidc_cons_bell(void *s __unused)
164 {
165 }
166
167 static void
168 vidc_text_cursor(void *s __unused, const teken_pos_t *p)
169 {
170         teken_unit_t row, col;
171
172         if (p->tp_col == TEXT_COLS)
173                 col = p->tp_col - 1;
174         else
175                 col = p->tp_col;
176
177         if (p->tp_row == TEXT_ROWS)
178                 row = p->tp_row - 1;
179         else
180                 row = p->tp_row;
181
182         vidc_text_set_cursor(row, col, true);
183 }
184
185 /*
186  * Binary searchable table for Unicode to CP437 conversion.
187  */
188 struct unicp437 {
189         uint16_t        unicode_base;
190         uint8_t         cp437_base;
191         uint8_t         length;
192 };
193
194 static const struct unicp437 cp437table[] = {
195         { 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
196         { 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
197         { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
198         { 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
199         { 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
200         { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
201         { 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
202         { 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
203         { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
204         { 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
205         { 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 },
206         { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
207         { 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 },
208         { 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 },
209         { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
210         { 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
211         { 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
212         { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
213         { 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
214         { 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
215         { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
216         { 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
217         { 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
218         { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
219         { 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
220         { 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
221         { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
222         { 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
223         { 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
224         { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
225         { 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
226         { 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
227         { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
228         { 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
229         { 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
230         { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
231         { 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
232         { 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
233         { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
234         { 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
235         { 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
236         { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
237         { 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
238         { 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
239         { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
240         { 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
241         { 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
242         { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
243         { 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
244         { 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
245         { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
246         { 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
247         { 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
248         { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
249         { 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
250         { 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
251         { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
252         { 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
253         { 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
254         { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
255         { 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
256         { 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
257         { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
258         { 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
259         { 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
260         { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
261         { 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
262         { 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
263         { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
264         { 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
265         { 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
266         { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
267         { 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
268         { 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
269         { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
270         { 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
271         { 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 },
272         { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
273         { 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
274         { 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
275         { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
276         { 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
277         { 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
278         { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
279         { 0x266c, 0x0e, 0x00 }
280 };
281
282 static uint8_t
283 vga_get_cp437(teken_char_t c)
284 {
285         int min, mid, max;
286
287         min = 0;
288         max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
289
290         if (c < cp437table[0].unicode_base ||
291             c > cp437table[max].unicode_base + cp437table[max].length)
292                 return ('?');
293
294         while (max >= min) {
295                 mid = (min + max) / 2;
296                 if (c < cp437table[mid].unicode_base)
297                         max = mid - 1;
298                 else if (c > cp437table[mid].unicode_base +
299                     cp437table[mid].length)
300                         min = mid + 1;
301                 else
302                         return (c - cp437table[mid].unicode_base +
303                             cp437table[mid].cp437_base);
304         }
305
306         return ('?');
307 }
308
309 static void
310 vidc_text_printchar(teken_gfx_t *state, const teken_pos_t *p)
311 {
312         int idx;
313         uint8_t attr;
314         struct text_pixel *px;
315         teken_color_t fg, bg, tmp;
316         struct cgatext {
317                 uint8_t ch;
318                 uint8_t attr;
319         } *addr;
320
321         idx = p->tp_col + p->tp_row * state->tg_tp.tp_col;
322         px = &screen_buffer[idx];
323         fg = teken_256to16(px->a.ta_fgcolor);
324         bg = teken_256to16(px->a.ta_bgcolor);
325         if (px->a.ta_format & TF_BOLD)
326                 fg |= TC_LIGHT;
327         if (px->a.ta_format & TF_BLINK)
328                 bg |= TC_LIGHT;
329
330         if (px->a.ta_format & TF_REVERSE) {
331                 tmp = fg;
332                 fg = bg;
333                 bg = tmp;
334         }
335
336         attr = (cmap[bg & 0xf] << 4) | cmap[fg & 0xf];
337         addr = (struct cgatext *)vgatext;
338         addr[idx].ch = vga_get_cp437(px->c);
339         addr[idx].attr = attr;
340 }
341
342 static void
343 vidc_text_putchar(void *s, const teken_pos_t *p, teken_char_t c,
344     const teken_attr_t *a)
345 {
346         teken_gfx_t *state = s;
347         int attr, idx;
348
349         idx = p->tp_col + p->tp_row * state->tg_tp.tp_col;
350         if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row)
351                 return;
352
353         screen_buffer[idx].c = c;
354         screen_buffer[idx].a = *a;
355
356         vidc_text_printchar(state, p);
357 }
358
359 static void
360 vidc_text_fill(void *arg, const teken_rect_t *r, teken_char_t c,
361     const teken_attr_t *a)
362 {
363         teken_gfx_t *state = arg;
364         teken_pos_t p;
365         teken_unit_t row, col;
366
367         vidc_text_get_cursor(&row, &col);
368         vidc_text_set_cursor(row, col, false);
369         for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row;
370             p.tp_row++)
371                 for (p.tp_col = r->tr_begin.tp_col;
372                     p.tp_col < r->tr_end.tp_col; p.tp_col++)
373                         vidc_text_putchar(state, &p, c, a);
374         vidc_text_set_cursor(row, col, true);
375 }
376
377 static void
378 vidc_text_copy(void *ptr, const teken_rect_t *r, const teken_pos_t *p)
379 {
380         teken_gfx_t *state = ptr;
381         int srow, drow;
382         int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
383         teken_pos_t d, s;
384         teken_unit_t row, col;
385
386         /*
387          * Copying is a little tricky. We must make sure we do it in
388          * correct order, to make sure we don't overwrite our own data.
389          */
390
391         nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
392         ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
393
394         vidc_text_get_cursor(&row, &col);
395         vidc_text_set_cursor(row, col, false);
396         if (p->tp_row < r->tr_begin.tp_row) {
397                 /* Copy from bottom to top. */
398                 for (y = 0; y < nrow; y++) {
399                         d.tp_row = p->tp_row + y;
400                         s.tp_row = r->tr_begin.tp_row + y;
401                         drow = d.tp_row * state->tg_tp.tp_col;
402                         srow = s.tp_row * state->tg_tp.tp_col;
403                         for (x = 0; x < ncol; x++) {
404                                 d.tp_col = p->tp_col + x;
405                                 s.tp_col = r->tr_begin.tp_col + x;
406
407                                 if (!is_same_pixel(
408                                     &screen_buffer[d.tp_col + drow],
409                                     &screen_buffer[s.tp_col + srow])) {
410                                         screen_buffer[d.tp_col + drow] =
411                                             screen_buffer[s.tp_col + srow];
412                                         vidc_text_printchar(state, &d);
413                                 }
414                         }
415                 }
416         } else {
417                 /* Copy from top to bottom. */
418                 if (p->tp_col < r->tr_begin.tp_col) {
419                         /* Copy from right to left. */
420                         for (y = nrow - 1; y >= 0; y--) {
421                                 d.tp_row = p->tp_row + y;
422                                 s.tp_row = r->tr_begin.tp_row + y;
423                                 drow = d.tp_row * state->tg_tp.tp_col;
424                                 srow = s.tp_row * state->tg_tp.tp_col;
425                                 for (x = 0; x < ncol; x++) {
426                                         d.tp_col = p->tp_col + x;
427                                         s.tp_col = r->tr_begin.tp_col + x;
428
429                                         if (!is_same_pixel(
430                                             &screen_buffer[d.tp_col + drow],
431                                             &screen_buffer[s.tp_col + srow])) {
432                                                 screen_buffer[d.tp_col + drow] =
433                                                     screen_buffer[s.tp_col + srow];
434                                                 vidc_text_printchar(state, &d);
435                                         }
436                                 }
437                         }
438                 } else {
439                         /* Copy from left to right. */
440                         for (y = nrow - 1; y >= 0; y--) {
441                                 d.tp_row = p->tp_row + y;
442                                 s.tp_row = r->tr_begin.tp_row + y;
443                                 drow = d.tp_row * state->tg_tp.tp_col;
444                                 srow = s.tp_row * state->tg_tp.tp_col;
445                                 for (x = ncol - 1; x >= 0; x--) {
446                                         d.tp_col = p->tp_col + x;
447                                         s.tp_col = r->tr_begin.tp_col + x;
448
449                                         if (!is_same_pixel(
450                                             &screen_buffer[d.tp_col + drow],
451                                             &screen_buffer[s.tp_col + srow])) {
452                                                 screen_buffer[d.tp_col + drow] =
453                                                     screen_buffer[s.tp_col + srow];
454                                                 vidc_text_printchar(state, &d);
455                                         }
456                                 }
457                         }
458                 }
459         }
460         vidc_text_set_cursor(row, col, true);
461 }
462
463 static void
464 vidc_text_param(void *arg, int cmd, unsigned int value)
465 {
466         teken_gfx_t *state = arg;
467         teken_unit_t row, col;
468
469         switch (cmd) {
470         case TP_SETLOCALCURSOR:
471                 /*
472                  * 0 means normal (usually block), 1 means hidden, and
473                  * 2 means blinking (always block) for compatibility with
474                  * syscons.  We don't support any changes except hiding,
475                  * so must map 2 to 0.
476                  */
477                 value = (value == 1) ? 0 : 1;
478                 /* FALLTHROUGH */
479         case TP_SHOWCURSOR:
480                 vidc_text_get_cursor(&row, &col);
481                 if (value != 0) {
482                         vidc_text_set_cursor(row, col, true);
483                         state->tg_cursor_visible = true;
484                 } else {
485                         vidc_text_set_cursor(row, col, false);
486                         state->tg_cursor_visible = false;
487                 }
488                 break;
489         default:
490                 /* Not yet implemented */
491                 break;
492         }
493 }
494
495 /*
496  * Not implemented.
497  */
498 static void
499 vidc_cons_respond(void *s __unused, const void *buf __unused,
500     size_t len __unused)
501 {
502 }
503
504 static void
505 vidc_probe(struct console *cp)
506 {
507     
508     /* look for a keyboard */
509 #if KEYBOARD_PROBE
510     if (probe_keyboard())
511 #endif
512     {
513         
514         cp->c_flags |= C_PRESENTIN;
515     }
516
517     /* XXX for now, always assume we can do BIOS screen output */
518     cp->c_flags |= C_PRESENTOUT;
519 }
520
521 static bool
522 color_name_to_teken(const char *name, int *val)
523 {
524         int light = 0;
525         if (strncasecmp(name, "light", 5) == 0) {
526                 name += 5;
527                 light = TC_LIGHT;
528         } else if (strncasecmp(name, "bright", 6) == 0) {
529                 name += 6;
530                 light = TC_LIGHT;
531         }
532         if (strcasecmp(name, "black") == 0) {
533                 *val = TC_BLACK | light;
534                 return (true);
535         }
536         if (strcasecmp(name, "red") == 0) {
537                 *val = TC_RED | light;
538                 return (true);
539         }
540         if (strcasecmp(name, "green") == 0) {
541                 *val = TC_GREEN | light;
542                 return (true);
543         }
544         if (strcasecmp(name, "yellow") == 0 || strcasecmp(name, "brown") == 0) {
545                 *val = TC_YELLOW | light;
546                 return (true);
547         }
548         if (strcasecmp(name, "blue") == 0) {
549                 *val = TC_BLUE | light;
550                 return (true);
551         }
552         if (strcasecmp(name, "magenta") == 0) {
553                 *val = TC_MAGENTA | light;
554                 return (true);
555         }
556         if (strcasecmp(name, "cyan") == 0) {
557                 *val = TC_CYAN | light;
558                 return (true);
559         }
560         if (strcasecmp(name, "white") == 0) {
561                 *val = TC_WHITE | light;
562                 return (true);
563         }
564         return (false);
565 }
566
567 static int
568 vidc_set_colors(struct env_var *ev, int flags, const void *value)
569 {
570         int val = 0;
571         char buf[3];
572         const void *evalue;
573         const teken_attr_t *ap;
574         teken_attr_t a;
575
576         if (value == NULL)
577                 return (CMD_OK);
578
579         if (color_name_to_teken(value, &val)) {
580                 snprintf(buf, sizeof (buf), "%d", val);
581                 evalue = buf;
582         } else {
583                 char *end;
584                 long lval;
585
586                 errno = 0;
587                 lval = strtol(value, &end, 0);
588                 if (errno != 0 || *end != '\0' || lval < 0 || lval > 15) {
589                         printf("Allowed values are either ansi color name or "
590                             "number from range [0-15].\n");
591                         return (CMD_OK);
592                 }
593                 val = (int)lval;
594                 evalue = value;
595         }
596
597         ap = teken_get_defattr(&gfx_state.tg_teken);
598         a = *ap;
599         if (strcmp(ev->ev_name, "teken.fg_color") == 0) {
600                 /* is it already set? */
601                 if (ap->ta_fgcolor == val)
602                         return (CMD_OK);
603                 a.ta_fgcolor = val;
604         }
605         if (strcmp(ev->ev_name, "teken.bg_color") == 0) {
606                 /* is it already set? */
607                 if (ap->ta_bgcolor == val)
608                         return (CMD_OK);
609                 a.ta_bgcolor = val;
610         }
611
612         /* Improve visibility */
613         if (a.ta_bgcolor == TC_WHITE)
614                 a.ta_bgcolor |= TC_LIGHT;
615
616         teken_set_defattr(&gfx_state.tg_teken, &a);
617         cons_draw_frame(&a);
618         env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL);
619         teken_input(&gfx_state.tg_teken, "\e[2J", 4);
620
621         return (CMD_OK);
622 }
623
624 static int
625 env_screen_nounset(struct env_var *ev __unused)
626 {
627         if (gfx_state.tg_fb_type == FB_TEXT)
628                 return (0);
629         return (EPERM);
630 }
631
632 static int
633 vidc_load_palette(void)
634 {
635         int i, roff, goff, boff, rc;
636
637         if (pe8 == NULL)
638                 pe8 = calloc(sizeof(*pe8), NCMAP);
639         if (pe8 == NULL)
640                 return (ENOMEM);
641
642         /* Generate VGA colors */
643         roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
644         goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
645         boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
646         rc = generate_cons_palette((uint32_t *)pe8, COLOR_FORMAT_RGB,
647             gfx_state.tg_fb.fb_mask_red >> roff, roff,
648             gfx_state.tg_fb.fb_mask_green >> goff, goff,
649             gfx_state.tg_fb.fb_mask_blue >> boff, boff);
650
651         if (rc == 0) {
652                 for (i = 0; i < NCMAP; i++) {
653                         int idx;
654
655                         if (i < NCOLORS)
656                                 idx = cons_to_vga_colors[i];
657                         else
658                                 idx = i;
659
660                         rc = vbe_set_palette(&pe8[i], idx);
661                         if (rc != 0)
662                                 break;
663                 }
664         }
665         return (rc);
666 }
667
668 static void
669 cons_draw_frame(teken_attr_t *a)
670 {
671         teken_attr_t attr = *a;
672         teken_color_t fg = a->ta_fgcolor;
673
674         attr.ta_fgcolor = attr.ta_bgcolor;
675         teken_set_defattr(&gfx_state.tg_teken, &attr);
676
677         gfx_fb_drawrect(0, 0, gfx_state.tg_fb.fb_width,
678             gfx_state.tg_origin.tp_row, 1);
679         gfx_fb_drawrect(0,
680             gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1,
681             gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1);
682         gfx_fb_drawrect(0, gfx_state.tg_origin.tp_row,
683             gfx_state.tg_origin.tp_col,
684             gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, 1);
685         gfx_fb_drawrect(
686             gfx_state.tg_fb.fb_width - gfx_state.tg_origin.tp_col - 1,
687             gfx_state.tg_origin.tp_row, gfx_state.tg_fb.fb_width,
688             gfx_state.tg_fb.fb_height, 1);
689
690         attr.ta_fgcolor = fg;
691         teken_set_defattr(&gfx_state.tg_teken, &attr);
692 }
693
694 /*
695  * Binary searchable table for CP437 to Unicode conversion.
696  */
697 struct cp437uni {
698         uint8_t         cp437_base;
699         uint16_t        unicode_base;
700         uint8_t         length;
701 };
702
703 static const struct cp437uni cp437unitable[] = {
704         {   0, 0x0000, 0 }, {   1, 0x263A, 1 }, {   3, 0x2665, 1 },
705         {   5, 0x2663, 0 }, {   6, 0x2660, 0 }, {   7, 0x2022, 0 },
706         {   8, 0x25D8, 0 }, {   9, 0x25CB, 0 }, {  10, 0x25D9, 0 },
707         {  11, 0x2642, 0 }, {  12, 0x2640, 0 }, {  13, 0x266A, 1 },
708         {  15, 0x263C, 0 }, {  16, 0x25BA, 0 }, {  17, 0x25C4, 0 },
709         {  18, 0x2195, 0 }, {  19, 0x203C, 0 }, {  20, 0x00B6, 0 },
710         {  21, 0x00A7, 0 }, {  22, 0x25AC, 0 }, {  23, 0x21A8, 0 },
711         {  24, 0x2191, 0 }, {  25, 0x2193, 0 }, {  26, 0x2192, 0 },
712         {  27, 0x2190, 0 }, {  28, 0x221F, 0 }, {  29, 0x2194, 0 },
713         {  30, 0x25B2, 0 }, {  31, 0x25BC, 0 }, {  32, 0x0020, 0x5e },
714         { 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 },
715         { 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 },
716         { 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 },
717         { 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 },
718         { 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 },
719         { 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 },
720         { 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 },
721         { 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 },
722         { 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 },
723         { 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 },
724         { 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 },
725         { 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 },
726         { 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 },
727         { 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 },
728         { 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 },
729         { 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 },
730         { 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 },
731         { 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 },
732         { 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 },
733         { 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 },
734         { 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 },
735         { 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 },
736         { 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 },
737         { 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 },
738         { 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 },
739         { 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 },
740         { 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 },
741         { 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 },
742         { 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 },
743         { 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 },
744         { 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 },
745         { 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 },
746         { 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 },
747         { 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 },
748         { 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 },
749         { 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 },
750         { 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 },
751         { 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 },
752         { 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 },
753         { 255, 0x00A0, 0 }
754 };
755
756 static uint16_t
757 vga_cp437_to_uni(uint8_t c)
758 {
759         int min, mid, max;
760
761         min = 0;
762         max = (sizeof(cp437unitable) / sizeof(struct cp437uni)) - 1;
763
764         while (max >= min) {
765                 mid = (min + max) / 2;
766                 if (c < cp437unitable[mid].cp437_base)
767                         max = mid - 1;
768                 else if (c > cp437unitable[mid].cp437_base +
769                     cp437unitable[mid].length)
770                         min = mid + 1;
771                 else
772                         return (c - cp437unitable[mid].cp437_base +
773                             cp437unitable[mid].unicode_base);
774         }
775
776         return ('?');
777 }
778
779 /*
780  * install font for text mode
781  */
782 static void
783 vidc_install_font(void)
784 {
785         uint8_t reg[7];
786         const uint8_t *from;
787         uint8_t volatile *to;
788         uint16_t c;
789         int i, j, s;
790         int bpc, f_offset;
791         teken_attr_t a = { 0 };
792
793         /* We can only program VGA registers. */
794         if (!vbe_is_vga())
795                 return;
796
797         if (gfx_state.tg_fb_type != FB_TEXT)
798                 return;
799
800         /* Sync-reset the sequencer registers */
801         vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR);
802
803         reg[0] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK);
804         reg[1] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE);
805         reg[2] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE);
806         reg[3] = vga_get_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT);
807         reg[4] = vga_get_grc(VGA_REG_BASE, VGA_GC_MODE);
808         reg[5] = vga_get_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS);
809         reg[6] = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
810
811         /* Screen off */
812         vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE,
813             reg[1] | VGA_SEQ_CM_SO);
814
815         /*
816          * enable write to plane2, since fonts
817          * could only be loaded into plane2
818          */
819         vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, VGA_SEQ_MM_EM2);
820         /*
821          * sequentially access data in the bit map being
822          * selected by MapMask register (index 0x02)
823          */
824         vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, 0x07);
825         /* Sync-reset ended, and allow the sequencer to operate */
826         vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET,
827             VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR);
828
829         /*
830          * select plane 2 on Read Mode 0
831          */
832         vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, 0x02);
833         /*
834          * system addresses sequentially access data, follow
835          * Memory Mode register bit 2 in the sequencer
836          */
837         vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, 0x00);
838         /*
839          * set range of host memory addresses decoded by VGA
840          * hardware -- A0000h-BFFFFh (128K region)
841          */
842         vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, 0x00);
843
844         /*
845          * This assumes 8x16 characters, which yield the traditional 80x25
846          * screen.
847          */
848         bpc = 16;
849         s = 0;  /* font slot, maybe should use tunable there. */
850         f_offset = s * 8 * 1024;
851         for (i = 0; i < 256; i++) {
852                 c = vga_cp437_to_uni(i);
853                 from = font_lookup(&gfx_state.tg_font, c, &a);
854                 to = (unsigned char *)ptov(VGA_MEM_BASE) + f_offset +
855                     i * 0x20;
856                 for (j = 0; j < bpc; j++)
857                         *to++ = *from++;
858         }
859
860         vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, reg[6]);
861
862         /* Sync-reset the sequencer registers */
863         vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR);
864         vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, reg[0]);
865         vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, reg[2]);
866         /* Sync-reset ended, and allow the sequencer to operate */
867         vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET,
868             VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR);
869
870         /* restore graphic registers */
871         vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, reg[3]);
872         vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, reg[4]);
873         vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, (reg[5] & 0x03) | 0x0c);
874
875         /* Screen on */
876         vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE, reg[1] & 0xdf);
877 }
878
879 bool
880 cons_update_mode(bool use_gfx_mode)
881 {
882         const teken_attr_t *a;
883         teken_attr_t attr;
884         char env[10], *ptr;
885         int format, roff, goff, boff;
886
887         /* vidc_init() is not called yet. */
888         if (!vidc_started)
889                 return (false);
890
891         gfx_state.tg_tp.tp_row = TEXT_ROWS;
892         gfx_state.tg_tp.tp_col = TEXT_COLS;
893
894         if (use_gfx_mode) {
895                 setup_font(&gfx_state, gfx_state.tg_fb.fb_height,
896                     gfx_state.tg_fb.fb_width);
897                 /* Point of origin in pixels. */
898                 gfx_state.tg_origin.tp_row = (gfx_state.tg_fb.fb_height -
899                     (gfx_state.tg_tp.tp_row * gfx_state.tg_font.vf_height)) / 2;
900                 gfx_state.tg_origin.tp_col = (gfx_state.tg_fb.fb_width -
901                     (gfx_state.tg_tp.tp_col * gfx_state.tg_font.vf_width)) / 2;
902
903                 gfx_state.tg_glyph_size = gfx_state.tg_font.vf_height *
904                     gfx_state.tg_font.vf_width * 4;
905                 free(gfx_state.tg_glyph);
906                 gfx_state.tg_glyph = malloc(gfx_state.tg_glyph_size);
907                 if (gfx_state.tg_glyph == NULL)
908                         return (false);
909                 gfx_state.tg_functions = &tfx;
910                 snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_height);
911                 env_setenv("screen.height", EV_VOLATILE | EV_NOHOOK, env,
912                     env_noset, env_screen_nounset);
913                 snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_width);
914                 env_setenv("screen.width", EV_VOLATILE | EV_NOHOOK, env,
915                     env_noset, env_screen_nounset);
916                 snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_bpp);
917                 env_setenv("screen.depth", EV_VOLATILE | EV_NOHOOK, env,
918                     env_noset, env_screen_nounset);
919         } else {
920                 /* Trigger loading of 8x16 font. */
921                 setup_font(&gfx_state,
922                     16 * gfx_state.tg_fb.fb_height,
923                     8 * gfx_state.tg_fb.fb_width);
924                 gfx_state.tg_functions = &tf;
925                 /* ensure the following are not set for text mode */
926                 unsetenv("screen.height");
927                 unsetenv("screen.width");
928                 unsetenv("screen.depth");
929                 vidc_install_font();
930         }
931
932         free(screen_buffer);
933         screen_buffer = malloc(gfx_state.tg_tp.tp_row * gfx_state.tg_tp.tp_col *
934             sizeof(*screen_buffer));
935         if (screen_buffer == NULL)
936                 return (false);
937
938         teken_init(&gfx_state.tg_teken, gfx_state.tg_functions, &gfx_state);
939
940         if (gfx_state.tg_ctype == CT_INDEXED)
941                 format = COLOR_FORMAT_VGA;
942         else
943                 format = COLOR_FORMAT_RGB;
944
945         roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1;
946         goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1;
947         boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1;
948         (void) generate_cons_palette(cmap, format,
949             gfx_state.tg_fb.fb_mask_red >> roff, roff,
950             gfx_state.tg_fb.fb_mask_green >> goff, goff,
951             gfx_state.tg_fb.fb_mask_blue >> boff, boff);
952
953         if (gfx_state.tg_ctype == CT_INDEXED && use_gfx_mode)
954                 vidc_load_palette();
955
956         teken_set_winsize(&gfx_state.tg_teken, &gfx_state.tg_tp);
957         a = teken_get_defattr(&gfx_state.tg_teken);
958         attr = *a;
959
960         /*
961          * On first run, we set up the vidc_set_colors()
962          * callback. If the env is already set, we
963          * pick up fg and bg color values from the environment.
964          */
965         ptr = getenv("teken.fg_color");
966         if (ptr != NULL) {
967                 attr.ta_fgcolor = strtol(ptr, NULL, 10);
968                 ptr = getenv("teken.bg_color");
969                 attr.ta_bgcolor = strtol(ptr, NULL, 10);
970
971                 teken_set_defattr(&gfx_state.tg_teken, &attr);
972         } else {
973                 snprintf(env, sizeof(env), "%d", attr.ta_fgcolor);
974                 env_setenv("teken.fg_color", EV_VOLATILE, env,
975                     vidc_set_colors, env_nounset);
976                 snprintf(env, sizeof(env), "%d", attr.ta_bgcolor);
977                 env_setenv("teken.bg_color", EV_VOLATILE, env,
978                     vidc_set_colors, env_nounset);
979         }
980
981         /* Improve visibility */
982         if (attr.ta_bgcolor == TC_WHITE)
983                 attr.ta_bgcolor |= TC_LIGHT;
984         teken_set_defattr(&gfx_state.tg_teken, &attr);
985
986         snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_row);
987         setenv("LINES", env, 1);
988         snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_col);
989         setenv("COLUMNS", env, 1);
990
991         /* Draw frame around terminal area. */
992         cons_draw_frame(&attr);
993         /* Erase display, this will also fill our screen buffer. */
994         teken_input(&gfx_state.tg_teken, "\e[2J", 4);
995         gfx_state.tg_functions->tf_param(&gfx_state, TP_SHOWCURSOR, 1);
996
997         return (true);
998 }
999
1000 static int
1001 vidc_init(int arg)
1002 {
1003         const teken_attr_t *a;
1004         int val;
1005         char env[8];
1006
1007         if (vidc_started && arg == 0)
1008                 return (0);
1009
1010         vidc_started = true;
1011         vbe_init();
1012
1013         /*
1014          * Check Miscellaneous Output Register (Read at 3CCh, Write at 3C2h)
1015          * for bit 1 (Input/Output Address Select), which means
1016          * color/graphics adapter.
1017          */
1018         if (vga_get_reg(VGA_REG_BASE, VGA_GEN_MISC_OUTPUT_R) & VGA_GEN_MO_IOA)
1019                 vgatext = (uint16_t *)PTOV(VGA_TXT_BASE);
1020         else
1021                 vgatext = (uint16_t *)PTOV(VGA_MEM_BASE + VGA_MEM_SIZE);
1022
1023         /* set 16bit colors */
1024         val = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
1025         val &= ~VGA_AC_MC_BI;
1026         val &= ~VGA_AC_MC_ELG;
1027         vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, val);
1028
1029 #if defined(FRAMEBUFFER_MODE)
1030         val = vbe_default_mode();
1031         /* if val is not legal VBE mode, use text mode */
1032         if (VBE_VALID_MODE(val)) {
1033                 if (vbe_set_mode(val) != 0)
1034                         bios_set_text_mode(VGA_TEXT_MODE);
1035         }
1036 #endif
1037
1038         gfx_framework_init();
1039
1040         if (!cons_update_mode(VBE_VALID_MODE(vbe_get_mode())))
1041                 return (1);
1042
1043         for (int i = 0; i < 10 && vidc_ischar(); i++)
1044                 (void) vidc_getchar();
1045
1046         return (0);     /* XXX reinit? */
1047 }
1048
1049 void
1050 vidc_biosputchar(int c)
1051 {
1052
1053     v86.ctl = 0;
1054     v86.addr = 0x10;
1055     v86.eax = 0xe00 | (c & 0xff);
1056     v86.ebx = 0x7;
1057     v86int();
1058 }
1059
1060 static void
1061 vidc_putchar(int c)
1062 {
1063         unsigned char ch = c;
1064
1065         if (screen_buffer != NULL)
1066                 teken_input(&gfx_state.tg_teken, &ch, sizeof (ch));
1067         else
1068                 vidc_biosputchar(c);
1069 }
1070
1071 static int
1072 vidc_getchar(void)
1073 {
1074         int i, c;
1075
1076         for (i = 0; i < KEYBUFSZ; i++) {
1077                 if (keybuf[i] != 0) {
1078                         c = keybuf[i];
1079                         keybuf[i] = 0;
1080                         return (c);
1081                 }
1082         }
1083
1084         if (vidc_ischar()) {
1085                 v86.ctl = 0;
1086                 v86.addr = 0x16;
1087                 v86.eax = 0x0;
1088                 v86int();
1089                 if ((v86.eax & 0xff) != 0) {
1090                         return (v86.eax & 0xff);
1091                 }
1092
1093                 /* extended keys */
1094                 switch (v86.eax & 0xff00) {
1095                 case 0x4800:    /* up */
1096                         keybuf[0] = '[';
1097                         keybuf[1] = 'A';
1098                         return (0x1b);  /* esc */
1099                 case 0x4b00:    /* left */
1100                         keybuf[0] = '[';
1101                         keybuf[1] = 'D';
1102                         return (0x1b);  /* esc */
1103                 case 0x4d00:    /* right */
1104                         keybuf[0] = '[';
1105                         keybuf[1] = 'C';
1106                         return (0x1b);  /* esc */
1107                 case 0x5000:    /* down */
1108                         keybuf[0] = '[';
1109                         keybuf[1] = 'B';
1110                         return (0x1b);  /* esc */
1111                 default:
1112                         return (-1);
1113                 }
1114         } else {
1115                 return (-1);
1116         }
1117 }
1118
1119 static int
1120 vidc_ischar(void)
1121 {
1122         int i;
1123
1124         for (i = 0; i < KEYBUFSZ; i++) {
1125                 if (keybuf[i] != 0) {
1126                         return (1);
1127                 }
1128         }
1129
1130         v86.ctl = V86_FLAGS;
1131         v86.addr = 0x16;
1132         v86.eax = 0x100;
1133         v86int();
1134         return (!V86_ZR(v86.efl));
1135 }
1136
1137 #if KEYBOARD_PROBE
1138
1139 #define PROBE_MAXRETRY  5
1140 #define PROBE_MAXWAIT   400
1141 #define IO_DUMMY        0x84
1142 #define IO_KBD          0x060           /* 8042 Keyboard */
1143
1144 /* selected defines from kbdio.h */
1145 #define KBD_STATUS_PORT         4       /* status port, read */
1146 #define KBD_DATA_PORT           0       /* data port, read/write
1147                                          * also used as keyboard command
1148                                          * and mouse command port 
1149                                          */
1150 #define KBDC_ECHO               0x00ee
1151 #define KBDS_ANY_BUFFER_FULL    0x0001
1152 #define KBDS_INPUT_BUFFER_FULL  0x0002
1153 #define KBD_ECHO                0x00ee
1154
1155 /* 7 microsec delay necessary for some keyboard controllers */
1156 static void
1157 delay7(void)
1158 {
1159         /*
1160          * I know this is broken, but no timer is available yet at this stage...
1161          * See also comments in `delay1ms()'.
1162          */
1163         inb(IO_DUMMY); inb(IO_DUMMY);
1164         inb(IO_DUMMY); inb(IO_DUMMY);
1165         inb(IO_DUMMY); inb(IO_DUMMY);
1166 }
1167
1168 /*
1169  * This routine uses an inb to an unused port, the time to execute that
1170  * inb is approximately 1.25uS.  This value is pretty constant across
1171  * all CPU's and all buses, with the exception of some PCI implentations
1172  * that do not forward this I/O address to the ISA bus as they know it
1173  * is not a valid ISA bus address, those machines execute this inb in
1174  * 60 nS :-(.
1175  *
1176  */
1177 static void
1178 delay1ms(void)
1179 {
1180         int i = 800;
1181         while (--i >= 0)
1182                 (void) inb(0x84);
1183 }
1184
1185 /*
1186  * We use the presence/absence of a keyboard to determine whether the internal
1187  * console can be used for input.
1188  *
1189  * Perform a simple test on the keyboard; issue the ECHO command and see
1190  * if the right answer is returned. We don't do anything as drastic as
1191  * full keyboard reset; it will be too troublesome and take too much time.
1192  */
1193 static int
1194 probe_keyboard(void)
1195 {
1196         int retry = PROBE_MAXRETRY;
1197         int wait;
1198         int i;
1199
1200         while (--retry >= 0) {
1201                 /* flush any noise */
1202                 while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
1203                         delay7();
1204                         inb(IO_KBD + KBD_DATA_PORT);
1205                         delay1ms();
1206                 }
1207
1208                 /* wait until the controller can accept a command */
1209                 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1210                         if (((i = inb(IO_KBD + KBD_STATUS_PORT)) 
1211                             & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL))
1212                             == 0)
1213                                 break;
1214                         if (i & KBDS_ANY_BUFFER_FULL) {
1215                                 delay7();
1216                                 inb(IO_KBD + KBD_DATA_PORT);
1217                         }
1218                         delay1ms();
1219                 }
1220                 if (wait <= 0)
1221                         continue;
1222
1223                 /* send the ECHO command */
1224                 outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
1225
1226                 /* wait for a response */
1227                 for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1228                         if (inb(IO_KBD + KBD_STATUS_PORT) &
1229                             KBDS_ANY_BUFFER_FULL)
1230                                 break;
1231                         delay1ms();
1232                 }
1233                 if (wait <= 0)
1234                         continue;
1235
1236                 delay7();
1237                 i = inb(IO_KBD + KBD_DATA_PORT);
1238 #ifdef PROBE_KBD_BEBUG
1239                 printf("probe_keyboard: got 0x%x.\n", i);
1240 #endif
1241                 if (i == KBD_ECHO) {
1242                         /* got the right answer */
1243                         return (1);
1244                 }
1245         }
1246
1247         return (0);
1248 }
1249 #endif /* KEYBOARD_PROBE */