xref: /illumos-gate/usr/src/boot/i386/libi386/vidconsole.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 
32 #include <stand.h>
33 #include <bootstrap.h>
34 #include <sys/tem_impl.h>
35 #include <sys/visual_io.h>
36 #include <sys/multiboot2.h>
37 #include <btxv86.h>
38 #include <machine/psl.h>
39 #include <machine/metadata.h>
40 #include "libi386.h"
41 #include "vbe.h"
42 #include <gfx_fb.h>
43 #include <rbx.h>
44 #include <sys/vgareg.h>
45 #include <sys/vgasubr.h>
46 #include <machine/cpufunc.h>
47 #include <stdbool.h>
48 
49 #if KEYBOARD_PROBE
50 
51 static int	probe_keyboard(void);
52 #endif
53 static void	vidc_probe(struct console *cp);
54 static int	vidc_init(struct console *cp, int arg);
55 static void	vidc_putchar(struct console *cp, int c);
56 static int	vidc_getchar(struct console *cp);
57 static int	vidc_ischar(struct console *cp);
58 static int	vidc_ioctl(struct console *cp, int cmd, void *data);
59 static void	vidc_biosputchar(int c);
60 static void	vidc_devinfo(struct console *cp);
61 
62 static int vidc_vbe_devinit(struct vis_devinit *);
63 static void vidc_cons_cursor(struct vis_conscursor *);
64 static int vidc_vbe_cons_put_cmap(struct vis_cmap *);
65 
66 static int vidc_text_devinit(struct vis_devinit *);
67 static int vidc_text_cons_clear(struct vis_consclear *);
68 static void vidc_text_cons_copy(struct vis_conscopy *);
69 static void vidc_text_cons_display(struct vis_consdisplay *);
70 static void vidc_text_set_cursor(screen_pos_t, screen_pos_t, bool);
71 static void vidc_text_get_cursor(screen_pos_t *, screen_pos_t *);
72 static int vidc_text_cons_put_cmap(struct vis_cmap *);
73 
74 static int vidc_started;
75 static uint16_t	*vgatext;
76 
77 /* mode change callback and argument from tem */
78 static vis_modechg_cb_t modechg_cb;
79 static struct vis_modechg_arg *modechg_arg;
80 static tem_vt_state_t tem;
81 
82 /* RGB colors for 8-bit depth */
83 struct paletteentry pe8[CMAP_SIZE];
84 
85 #define	KEYBUFSZ	10
86 #define	DEFAULT_FGCOLOR	7
87 #define	DEFAULT_BGCOLOR	0
88 
89 static uint8_t	keybuf[KEYBUFSZ];	/* keybuf for extended codes */
90 
91 struct console text = {
92 	.c_name = "text",
93 	.c_desc = "internal video/keyboard",
94 	.c_flags = 0,
95 	.c_probe = vidc_probe,
96 	.c_init = vidc_init,
97 	.c_out = vidc_putchar,
98 	.c_in = vidc_getchar,
99 	.c_ready = vidc_ischar,
100 	.c_ioctl = vidc_ioctl,
101 	.c_devinfo = vidc_devinfo,
102 	.c_private = NULL
103 };
104 
105 static struct vis_identifier fb_ident = { "vidc_fb" };
106 static struct vis_identifier text_ident = { "vidc_text" };
107 
108 struct visual_ops fb_ops = {
109 	.ident = &fb_ident,
110 	.kdsetmode = NULL,
111 	.devinit = vidc_vbe_devinit,
112 	.cons_copy = gfx_fb_cons_copy,
113 	.cons_display = gfx_fb_cons_display,
114 	.cons_cursor = vidc_cons_cursor,
115 	.cons_clear = gfx_fb_cons_clear,
116 	.cons_put_cmap = vidc_vbe_cons_put_cmap
117 };
118 
119 struct visual_ops text_ops = {
120 	.ident = &text_ident,
121 	.kdsetmode = NULL,
122 	.devinit = vidc_text_devinit,
123 	.cons_copy = vidc_text_cons_copy,
124 	.cons_display = vidc_text_cons_display,
125 	.cons_cursor = vidc_cons_cursor,
126 	.cons_clear = vidc_text_cons_clear,
127 	.cons_put_cmap = vidc_text_cons_put_cmap
128 };
129 
130 /*
131  * platform specific functions for tem
132  */
133 int
134 plat_stdout_is_framebuffer(void)
135 {
136 	if (vbe_available() && VBE_VALID_MODE(vbe_get_mode())) {
137 		return (1);
138 	}
139 	return (0);
140 }
141 
142 void
143 plat_tem_hide_prom_cursor(void)
144 {
145 	vidc_text_set_cursor(0, 0, false);
146 }
147 
148 void
149 plat_tem_get_prom_pos(uint32_t *row, uint32_t *col)
150 {
151 	screen_pos_t x, y;
152 
153 	if (plat_stdout_is_framebuffer()) {
154 		*row = 0;
155 		*col = 0;
156 	} else {
157 		vidc_text_get_cursor(&y, &x);
158 		*row = (uint32_t)y;
159 		*col = (uint32_t)x;
160 	}
161 }
162 
163 /*
164  * plat_tem_get_prom_size() is supposed to return screen size
165  * in chars. Return real data for text mode and TEM defaults for graphical
166  * mode, so the tem can compute values based on default and font.
167  */
168 void
169 plat_tem_get_prom_size(size_t *height, size_t *width)
170 {
171 	if (plat_stdout_is_framebuffer()) {
172 		*height = TEM_DEFAULT_ROWS;
173 		*width = TEM_DEFAULT_COLS;
174 	} else {
175 		*height = TEXT_ROWS;
176 		*width = TEXT_COLS;
177 	}
178 }
179 
180 void
181 plat_cons_update_mode(int mode __unused)
182 {
183 	struct vis_devinit devinit;
184 
185 	if (tem == NULL)	/* tem is not set up */
186 		return;
187 
188 	if (plat_stdout_is_framebuffer()) {
189 		devinit.version = VIS_CONS_REV;
190 		devinit.width = gfx_fb.framebuffer_common.framebuffer_width;
191 		devinit.height = gfx_fb.framebuffer_common.framebuffer_height;
192 		devinit.depth = gfx_fb.framebuffer_common.framebuffer_bpp;
193 		devinit.linebytes = gfx_fb.framebuffer_common.framebuffer_pitch;
194 		devinit.color_map = gfx_fb_color_map;
195 		devinit.mode = VIS_PIXEL;
196 		text.c_private = &fb_ops;
197 	} else {
198 		devinit.version = VIS_CONS_REV;
199 		devinit.width = TEXT_COLS;
200 		devinit.height = TEXT_ROWS;
201 		devinit.depth = 4;
202 		devinit.linebytes = TEXT_COLS;
203 		devinit.color_map = NULL;
204 		devinit.mode = VIS_TEXT;
205 		text.c_private = &text_ops;
206 	}
207 
208 	modechg_cb(modechg_arg, &devinit);
209 }
210 
211 static int
212 vidc_vbe_devinit(struct vis_devinit *devinit)
213 {
214 	if (plat_stdout_is_framebuffer() == 0)
215 		return (1);
216 
217 	devinit->version = VIS_CONS_REV;
218 	devinit->width = gfx_fb.framebuffer_common.framebuffer_width;
219 	devinit->height = gfx_fb.framebuffer_common.framebuffer_height;
220 	devinit->depth = gfx_fb.framebuffer_common.framebuffer_bpp;
221 	devinit->linebytes = gfx_fb.framebuffer_common.framebuffer_pitch;
222 	devinit->color_map = gfx_fb_color_map;
223 	devinit->mode = VIS_PIXEL;
224 
225 	modechg_cb = devinit->modechg_cb;
226 	modechg_arg = devinit->modechg_arg;
227 
228 	return (0);
229 }
230 
231 static int
232 vidc_text_devinit(struct vis_devinit *devinit)
233 {
234 	if (plat_stdout_is_framebuffer())
235 		return (1);
236 
237 	devinit->version = VIS_CONS_REV;
238 	devinit->width = TEXT_COLS;
239 	devinit->height = TEXT_ROWS;
240 	devinit->depth = 4;
241 	devinit->linebytes = TEXT_COLS;
242 	devinit->color_map = NULL;
243 	devinit->mode = VIS_TEXT;
244 
245 	modechg_cb = devinit->modechg_cb;
246 	modechg_arg = devinit->modechg_arg;
247 
248 	return (0);
249 }
250 
251 static int
252 vidc_text_cons_clear(struct vis_consclear *ca)
253 {
254 	uint16_t val;
255 	int i;
256 
257 	val = (solaris_color_to_pc_color[ca->bg_color.four & 0xf] << 4) |
258 	    DEFAULT_FGCOLOR;
259 	val = (val << 8) | ' ';
260 
261 	for (i = 0; i < TEXT_ROWS * TEXT_COLS; i++)
262 		vgatext[i] = val;
263 
264 	return (0);
265 }
266 
267 static void
268 vidc_text_cons_copy(struct vis_conscopy *ma)
269 {
270 	uint16_t  *from;
271 	uint16_t *to;
272 	int cnt;
273 	screen_size_t chars_per_row;
274 	uint16_t *to_row_start;
275 	uint16_t *from_row_start;
276 	screen_size_t rows_to_move;
277 	uint16_t *base;
278 
279 	/*
280 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
281 	 * damage caused by broken-ness or maliciousness above.
282 	 */
283 	if (ma->s_col < 0 || ma->s_col >= TEXT_COLS ||
284 	    ma->s_row < 0 || ma->s_row >= TEXT_ROWS ||
285 	    ma->e_col < 0 || ma->e_col >= TEXT_COLS ||
286 	    ma->e_row < 0 || ma->e_row >= TEXT_ROWS ||
287 	    ma->t_col < 0 || ma->t_col >= TEXT_COLS ||
288 	    ma->t_row < 0 || ma->t_row >= TEXT_ROWS ||
289 	    ma->s_col > ma->e_col ||
290 	    ma->s_row > ma->e_row)
291 		return;
292 
293 	/*
294 	 * Remember we're going to copy shorts because each
295 	 * character/attribute pair is 16 bits.
296 	 */
297 	chars_per_row = ma->e_col - ma->s_col + 1;
298 	rows_to_move = ma->e_row - ma->s_row + 1;
299 
300 	/* More sanity checks. */
301 	if (ma->t_row + rows_to_move > TEXT_ROWS ||
302 	    ma->t_col + chars_per_row > TEXT_COLS)
303 		return;
304 
305 	base = vgatext;
306 
307 	to_row_start = base + ((ma->t_row * TEXT_COLS) + ma->t_col);
308 	from_row_start = base + ((ma->s_row * TEXT_COLS) + ma->s_col);
309 
310 	if (to_row_start < from_row_start) {
311 		while (rows_to_move-- > 0) {
312 			to = to_row_start;
313 			from = from_row_start;
314 			to_row_start += TEXT_COLS;
315 			from_row_start += TEXT_COLS;
316 			for (cnt = chars_per_row; cnt-- > 0; )
317 				*to++ = *from++;
318 		}
319 	} else {
320 		/*
321 		 * Offset to the end of the region and copy backwards.
322 		 */
323 		cnt = rows_to_move * TEXT_COLS + chars_per_row;
324 		to_row_start += cnt;
325 		from_row_start += cnt;
326 
327 		while (rows_to_move-- > 0) {
328 			to_row_start -= TEXT_COLS;
329 			from_row_start -= TEXT_COLS;
330 			to = to_row_start;
331 			from = from_row_start;
332 			for (cnt = chars_per_row; cnt-- > 0; )
333 				*--to = *--from;
334 		}
335 	}
336 }
337 
338 /*
339  * Binary searchable table for Unicode to CP437 conversion.
340  */
341 struct unicp437 {
342 	uint16_t	unicode_base;
343 	uint8_t		cp437_base;
344 	uint8_t		length;
345 };
346 
347 static const struct unicp437 cp437table[] = {
348 	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
349 	{ 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
350 	{ 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
351 	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
352 	{ 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
353 	{ 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
354 	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
355 	{ 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
356 	{ 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
357 	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
358 	{ 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 },
359 	{ 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
360 	{ 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 },
361 	{ 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 },
362 	{ 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
363 	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
364 	{ 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
365 	{ 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
366 	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
367 	{ 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
368 	{ 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
369 	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
370 	{ 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
371 	{ 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
372 	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
373 	{ 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
374 	{ 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
375 	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
376 	{ 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
377 	{ 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
378 	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
379 	{ 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
380 	{ 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
381 	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
382 	{ 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
383 	{ 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
384 	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
385 	{ 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
386 	{ 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
387 	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
388 	{ 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
389 	{ 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
390 	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
391 	{ 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
392 	{ 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
393 	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
394 	{ 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
395 	{ 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
396 	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
397 	{ 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
398 	{ 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
399 	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
400 	{ 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
401 	{ 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
402 	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
403 	{ 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
404 	{ 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
405 	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
406 	{ 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
407 	{ 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
408 	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
409 	{ 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
410 	{ 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
411 	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
412 	{ 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
413 	{ 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
414 	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
415 	{ 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
416 	{ 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
417 	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
418 	{ 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
419 	{ 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
420 	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
421 	{ 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
422 	{ 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
423 	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
424 	{ 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 },
425 	{ 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
426 	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
427 	{ 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
428 	{ 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
429 	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
430 	{ 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
431 	{ 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
432 	{ 0x266c, 0x0e, 0x00 }
433 };
434 
435 static uint8_t
436 vga_get_cp437(tem_char_t c)
437 {
438 	int min, mid, max;
439 
440 	min = 0;
441 	max = (sizeof (cp437table) / sizeof (struct unicp437)) - 1;
442 
443 	if (c < cp437table[0].unicode_base ||
444 	    c > cp437table[max].unicode_base + cp437table[max].length)
445 		return ('?');
446 
447 	while (max >= min) {
448 		mid = (min + max) / 2;
449 		if (c < cp437table[mid].unicode_base)
450 			max = mid - 1;
451 		else if (c > cp437table[mid].unicode_base +
452 		    cp437table[mid].length)
453 			min = mid + 1;
454 		else
455 			return (c - cp437table[mid].unicode_base +
456 			    cp437table[mid].cp437_base);
457 	}
458 
459 	return ('?');
460 }
461 
462 static void
463 vidc_text_cons_display(struct vis_consdisplay *da)
464 {
465 	int i;
466 	uint8_t attr;
467 	tem_char_t *data;
468 	struct cgatext {
469 		uint8_t ch;
470 		uint8_t attr;
471 	} *addr;
472 
473 	data = (tem_char_t *)da->data;
474 	attr = (solaris_color_to_pc_color[da->bg_color.four & 0xf] << 4) |
475 	    solaris_color_to_pc_color[da->fg_color.four & 0xf];
476 	addr = (struct cgatext *)vgatext + (da->row * TEXT_COLS + da->col);
477 
478 	for (i = 0; i < da->width; i++) {
479 		addr[i].ch = vga_get_cp437(data[i]);
480 		addr[i].attr = attr;
481 	}
482 }
483 
484 static void
485 vidc_text_set_cursor(screen_pos_t row, screen_pos_t col, bool visible)
486 {
487 	uint16_t addr;
488 	uint8_t msl, s, e;
489 
490 	msl = vga_get_crtc(VGA_REG_ADDR, VGA_CRTC_MAX_S_LN) & 0x1f;
491 	s = vga_get_crtc(VGA_REG_ADDR, VGA_CRTC_CSSL) & 0xC0;
492 	e = vga_get_crtc(VGA_REG_ADDR, VGA_CRTC_CESL);
493 
494 	if (visible) {
495 		addr = row * TEXT_COLS + col;
496 		vga_set_crtc(VGA_REG_ADDR, VGA_CRTC_CLAH, addr >> 8);
497 		vga_set_crtc(VGA_REG_ADDR, VGA_CRTC_CLAL, addr & 0xff);
498 		e = msl;
499 	} else {
500 		s |= (1<<5);
501 	}
502 	vga_set_crtc(VGA_REG_ADDR, VGA_CRTC_CSSL, s);
503 	vga_set_crtc(VGA_REG_ADDR, VGA_CRTC_CESL, e);
504 }
505 
506 static void
507 vidc_text_get_cursor(screen_pos_t *row, screen_pos_t *col)
508 {
509 	uint16_t addr;
510 
511 	addr = (vga_get_crtc(VGA_REG_ADDR, VGA_CRTC_CLAH) << 8) +
512 	    vga_get_crtc(VGA_REG_ADDR, VGA_CRTC_CLAL);
513 
514 	*row = addr / TEXT_COLS;
515 	*col = addr % TEXT_COLS;
516 }
517 
518 static void
519 vidc_cons_cursor(struct vis_conscursor *cc)
520 {
521 	switch (cc->action) {
522 	case VIS_HIDE_CURSOR:
523 		if (plat_stdout_is_framebuffer())
524 			gfx_fb_display_cursor(cc);
525 		else
526 			vidc_text_set_cursor(cc->row, cc->col, false);
527 		break;
528 	case VIS_DISPLAY_CURSOR:
529 		if (plat_stdout_is_framebuffer())
530 			gfx_fb_display_cursor(cc);
531 		else
532 			vidc_text_set_cursor(cc->row, cc->col, true);
533 		break;
534 	case VIS_GET_CURSOR:
535 		if (plat_stdout_is_framebuffer()) {
536 			cc->row = 0;
537 			cc->col = 0;
538 		} else {
539 			vidc_text_get_cursor(&cc->row, &cc->col);
540 		}
541 		break;
542 	}
543 }
544 
545 static int
546 vidc_vbe_cons_put_cmap(struct vis_cmap *cm)
547 {
548 	int i, rc;
549 	rgb_t rgb;
550 	uint32_t c;
551 
552 	rc = 0;
553 
554 	/*
555 	 * we need to set position and size for rgb_color_map()
556 	 * to be able to work.
557 	 */
558 	gfx_fb.u.fb2.framebuffer_red_field_position = 16;
559 	gfx_fb.u.fb2.framebuffer_green_field_position = 8;
560 	gfx_fb.u.fb2.framebuffer_blue_field_position = 0;
561 	gfx_fb.u.fb2.framebuffer_red_mask_size = palette_format;
562 	gfx_fb.u.fb2.framebuffer_green_mask_size = palette_format;
563 	gfx_fb.u.fb2.framebuffer_blue_mask_size = palette_format;
564 
565 	rgb.red.pos = gfx_fb.u.fb2.framebuffer_red_field_position;
566 	rgb.red.size = gfx_fb.u.fb2.framebuffer_red_mask_size;
567 
568 	rgb.green.pos = gfx_fb.u.fb2.framebuffer_green_field_position;
569 	rgb.green.size = gfx_fb.u.fb2.framebuffer_green_mask_size;
570 
571 	rgb.blue.pos = gfx_fb.u.fb2.framebuffer_blue_field_position;
572 	rgb.blue.size = gfx_fb.u.fb2.framebuffer_blue_mask_size;
573 
574 	for (i = cm->index; i < NCMAP && rc == 0; i++) {
575 		int idx;
576 
577 		/* Pick RGB from cmap4_to_24 */
578 		c = rgb_color_map(&rgb, i, 0);
579 		/* The first 16 colors need to be in VGA color order. */
580 		if (i < NCOLORS)
581 			idx = solaris_color_to_pc_color[i];
582 		else
583 			idx = i;
584 
585 		pe8[i].Red = (c >> rgb.red.pos) & ((1 << rgb.red.size) - 1);
586 		pe8[i].Green =
587 		    (c >> rgb.green.pos) & ((1 << rgb.green.size) - 1);
588 		pe8[i].Blue =
589 		    (c >> rgb.blue.pos) & ((1 << rgb.blue.size) - 1);
590 		pe8[i].Reserved = 0;
591 		rc = vbe_set_palette(&pe8[i], idx);
592 	}
593 	return (rc);
594 }
595 
596 static int
597 vidc_text_cons_put_cmap(struct vis_cmap *cm __unused)
598 {
599 	return (1);
600 }
601 
602 static int
603 vidc_ioctl(struct console *cp, int cmd, void *data)
604 {
605 	struct visual_ops *ops = cp->c_private;
606 
607 	switch (cmd) {
608 	case VIS_GETIDENTIFIER:
609 		memmove(data, ops->ident, sizeof (struct vis_identifier));
610 		break;
611 	case VIS_DEVINIT:
612 		return (ops->devinit(data));
613 	case VIS_CONSCLEAR:
614 		return (ops->cons_clear(data));
615 	case VIS_CONSCOPY:
616 		ops->cons_copy(data);
617 		break;
618 	case VIS_CONSDISPLAY:
619 		ops->cons_display(data);
620 		break;
621 	case VIS_CONSCURSOR:
622 		ops->cons_cursor(data);
623 		break;
624 	case VIS_PUTCMAP:
625 		ops->cons_put_cmap(data);
626 		break;
627 	case VIS_GETCMAP:
628 	default:
629 		return (EINVAL);
630 	}
631 	return (0);
632 }
633 
634 static void
635 vidc_probe(struct console *cp)
636 {
637 
638 	/* look for a keyboard */
639 #if KEYBOARD_PROBE
640 	if (probe_keyboard())
641 #endif
642 	{
643 		cp->c_flags |= C_PRESENTIN;
644 	}
645 
646 	/* XXX for now, always assume we can do BIOS screen output */
647 	cp->c_flags |= C_PRESENTOUT;
648 }
649 
650 /*
651  * Binary searchable table for CP437 to Unicode conversion.
652  */
653 struct cp437uni {
654 	uint8_t		cp437_base;
655 	uint16_t	unicode_base;
656 	uint8_t		length;
657 };
658 
659 static const struct cp437uni cp437unitable[] = {
660 	{   0, 0x0000, 0 }, {   1, 0x263A, 1 }, {   3, 0x2665, 1 },
661 	{   5, 0x2663, 0 }, {   6, 0x2660, 0 }, {   7, 0x2022, 0 },
662 	{   8, 0x25D8, 0 }, {   9, 0x25CB, 0 }, {  10, 0x25D9, 0 },
663 	{  11, 0x2642, 0 }, {  12, 0x2640, 0 }, {  13, 0x266A, 1 },
664 	{  15, 0x263C, 0 }, {  16, 0x25BA, 0 }, {  17, 0x25C4, 0 },
665 	{  18, 0x2195, 0 }, {  19, 0x203C, 0 }, {  20, 0x00B6, 0 },
666 	{  21, 0x00A7, 0 }, {  22, 0x25AC, 0 }, {  23, 0x21A8, 0 },
667 	{  24, 0x2191, 0 }, {  25, 0x2193, 0 }, {  26, 0x2192, 0 },
668 	{  27, 0x2190, 0 }, {  28, 0x221F, 0 }, {  29, 0x2194, 0 },
669 	{  30, 0x25B2, 0 }, {  31, 0x25BC, 0 }, {  32, 0x0020, 0x5e },
670 	{ 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 },
671 	{ 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 },
672 	{ 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 },
673 	{ 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 },
674 	{ 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 },
675 	{ 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 },
676 	{ 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 },
677 	{ 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 },
678 	{ 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 },
679 	{ 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 },
680 	{ 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 },
681 	{ 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 },
682 	{ 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 },
683 	{ 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 },
684 	{ 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 },
685 	{ 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 },
686 	{ 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 },
687 	{ 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 },
688 	{ 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 },
689 	{ 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 },
690 	{ 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 },
691 	{ 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 },
692 	{ 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 },
693 	{ 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 },
694 	{ 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 },
695 	{ 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 },
696 	{ 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 },
697 	{ 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 },
698 	{ 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 },
699 	{ 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 },
700 	{ 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 },
701 	{ 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 },
702 	{ 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 },
703 	{ 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 },
704 	{ 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 },
705 	{ 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 },
706 	{ 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 },
707 	{ 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 },
708 	{ 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 },
709 	{ 255, 0x00A0, 0 }
710 };
711 
712 static uint16_t
713 vga_cp437_to_uni(uint8_t c)
714 {
715 	int min, mid, max;
716 
717 	min = 0;
718 	max = (sizeof (cp437unitable) / sizeof (struct cp437uni)) - 1;
719 
720 	while (max >= min) {
721 		mid = (min + max) / 2;
722 		if (c < cp437unitable[mid].cp437_base)
723 			max = mid - 1;
724 		else if (c > cp437unitable[mid].cp437_base +
725 		    cp437unitable[mid].length)
726 			min = mid + 1;
727 		else
728 			return (c - cp437unitable[mid].cp437_base +
729 			    cp437unitable[mid].unicode_base);
730 	}
731 
732 	return ('?');
733 }
734 
735 /*
736  * install font for text mode, borrowed from gfxp_vgatext.c
737  */
738 static void
739 vidc_install_font(tem_modechg_cb_arg_t arg __unused)
740 {
741 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
742 	const uchar_t *from;
743 	uchar_t volatile *to;
744 	uint16_t c;
745 	int i, j, s;
746 	int bpc, f_offset;
747 
748 	if (plat_stdout_is_framebuffer())
749 		return;
750 
751 	/* Sync-reset the sequencer registers */
752 	vga_set_seq(VGA_REG_ADDR, 0x00, 0x01);
753 	/*
754 	 *  enable write to plane2, since fonts
755 	 * could only be loaded into plane2
756 	 */
757 	vga_set_seq(VGA_REG_ADDR, 0x02, 0x04);
758 	/*
759 	 *  sequentially access data in the bit map being
760 	 * selected by MapMask register (index 0x02)
761 	 */
762 	vga_set_seq(VGA_REG_ADDR, 0x04, 0x07);
763 	/* Sync-reset ended, and allow the sequencer to operate */
764 	vga_set_seq(VGA_REG_ADDR, 0x00, 0x03);
765 
766 	/*
767 	 *  select plane 2 on Read Mode 0
768 	 */
769 	vga_set_grc(VGA_REG_ADDR, 0x04, 0x02);
770 	/*
771 	 *  system addresses sequentially access data, follow
772 	 * Memory Mode register bit 2 in the sequencer
773 	 */
774 	vga_set_grc(VGA_REG_ADDR, 0x05, 0x00);
775 	/*
776 	 * set range of host memory addresses decoded by VGA
777 	 * hardware -- A0000h-BFFFFh (128K region)
778 	 */
779 	vga_set_grc(VGA_REG_ADDR, 0x06, 0x00);
780 
781 	/*
782 	 * This assumes 8x16 characters, which yield the traditional 80x25
783 	 * screen.  It really should support other character heights.
784 	 */
785 	bpc = 16;
786 	s = 0;		/* font slot, maybe should use tunable there. */
787 	f_offset = s * 8 * 1024;
788 	for (i = 0; i < 256; i++) {
789 		c = vga_cp437_to_uni(i);
790 		from = font_lookup(&tems.ts_font, c);
791 		to = (unsigned char *)PTOV(VGA_MEM_ADDR) + f_offset +
792 		    i * 0x20;
793 		for (j = 0; j < bpc; j++)
794 			*to++ = *from++;
795 	}
796 
797 	/* Sync-reset the sequencer registers */
798 	vga_set_seq(VGA_REG_ADDR, 0x00, 0x01);
799 	/* enable write to plane 0 and 1 */
800 	vga_set_seq(VGA_REG_ADDR, 0x02, 0x03);
801 	/*
802 	 * enable character map selection
803 	 * and odd/even addressing
804 	 */
805 	vga_set_seq(VGA_REG_ADDR, 0x04, 0x03);
806 	/*
807 	 * select font map
808 	 */
809 	vga_set_seq(VGA_REG_ADDR, 0x03, fsreg[s]);
810 	/* Sync-reset ended, and allow the sequencer to operate */
811 	vga_set_seq(VGA_REG_ADDR, 0x00, 0x03);
812 
813 	/* restore graphic registers */
814 
815 	/* select plane 0 */
816 	vga_set_grc(VGA_REG_ADDR, 0x04, 0x00);
817 	/* enable odd/even addressing mode */
818 	vga_set_grc(VGA_REG_ADDR, 0x05, 0x10);
819 	/*
820 	 * range of host memory addresses decoded by VGA
821 	 * hardware -- B8000h-BFFFFh (32K region)
822 	 */
823 	vga_set_grc(VGA_REG_ADDR, 0x06, 0x0e);
824 	/* enable all color plane */
825 	vga_set_atr(VGA_REG_ADDR, 0x12, 0x0f);
826 }
827 
828 static int
829 vidc_init(struct console *cp, int arg)
830 {
831 	int i, rc;
832 
833 	if (vidc_started && arg == 0)
834 		return (0);
835 
836 	vidc_started = 1;
837 	vbe_init();
838 
839 	/*
840 	 * Check Miscellaneous Output Register (Read at 3CCh, Write at 3C2h)
841 	 * for bit 1 (Input/Output Address Select), which means
842 	 * color/graphics adapter.
843 	 */
844 	if (vga_get_reg(VGA_REG_ADDR, VGA_MISC_R) & VGA_MISC_IOA_SEL)
845 		vgatext = (uint16_t *)PTOV(VGA_MEM_ADDR + VGA_COLOR_BASE);
846 	else
847 		vgatext = (uint16_t *)PTOV(VGA_MEM_ADDR + VGA_MONO_BASE);
848 
849 	/* set 16bit colors */
850 	i = vga_get_atr(VGA_REG_ADDR, VGA_ATR_MODE);
851 	i &= ~VGA_ATR_MODE_BLINK;
852 	i &= ~VGA_ATR_MODE_9WIDE;
853 	vga_set_atr(VGA_REG_ADDR, VGA_ATR_MODE, i);
854 
855 	plat_tem_hide_prom_cursor();
856 
857 	memset(keybuf, 0, KEYBUFSZ);
858 
859 	/* default to text mode */
860 	cp->c_private = &text_ops;
861 
862 	if (OPT_CHECK(RBX_TEXT_MODE) == 0 && vbe_available()) {
863 		rc = vbe_default_mode();
864 		/* if rc is not legal VBE mode, use text mode */
865 		if (VBE_VALID_MODE(rc)) {
866 			if (vbe_set_mode(rc) == 0)
867 				cp->c_private = &fb_ops;
868 			else
869 				bios_set_text_mode(VGA_TEXT_MODE);
870 		}
871 	}
872 
873 	gfx_framework_init();
874 	/* set up callback before calling tem_info_init(). */
875 	tem_register_modechg_cb(vidc_install_font, (tem_modechg_cb_arg_t)cp);
876 	rc = tem_info_init(cp);
877 
878 	if (rc != 0) {
879 		bios_set_text_mode(3);
880 		cp->c_private = &text_ops;
881 		rc = tem_info_init(cp); /* try again */
882 	}
883 	if (rc == 0 && tem == NULL) {
884 		tem = tem_init();
885 		if (tem != NULL)
886 			tem_activate(tem, true);
887 	}
888 
889 	for (i = 0; i < 10 && vidc_ischar(cp); i++)
890 		(void) vidc_getchar(cp);
891 
892 	return (0);
893 }
894 
895 static void
896 vidc_biosputchar(int c)
897 {
898 	v86.ctl = 0;
899 	v86.addr = 0x10;
900 	v86.eax = 0xe00 | (c & 0xff);
901 	v86.ebx = 0x7;
902 	v86int();
903 }
904 
905 static void
906 vidc_putchar(struct console *cp __unused, int c)
907 {
908 	uint8_t buf = c;
909 
910 	/* make sure we have some console output, support for panic() */
911 	if (tem == NULL)
912 		vidc_biosputchar(c);
913 	else
914 		tem_write(tem, &buf, sizeof (buf));
915 }
916 
917 static int
918 vidc_getchar(struct console *cp)
919 {
920 	int i, c;
921 
922 	for (i = 0; i < KEYBUFSZ; i++) {
923 		if (keybuf[i] != 0) {
924 			c = keybuf[i];
925 			keybuf[i] = 0;
926 			return (c);
927 		}
928 	}
929 
930 	if (vidc_ischar(cp)) {
931 		v86.ctl = 0;
932 		v86.addr = 0x16;
933 		v86.eax = 0x0;
934 		v86int();
935 		if ((v86.eax & 0xff) != 0) {
936 			return (v86.eax & 0xff);
937 		}
938 
939 		/* extended keys */
940 		switch (v86.eax & 0xff00) {
941 		case 0x4800:	/* up */
942 			keybuf[0] = '[';
943 			keybuf[1] = 'A';
944 			return (0x1b);	/* esc */
945 		case 0x4b00:	/* left */
946 			keybuf[0] = '[';
947 			keybuf[1] = 'D';
948 			return (0x1b);	/* esc */
949 		case 0x4d00:	/* right */
950 			keybuf[0] = '[';
951 			keybuf[1] = 'C';
952 			return (0x1b);	/* esc */
953 		case 0x5000:	/* down */
954 			keybuf[0] = '[';
955 			keybuf[1] = 'B';
956 			return (0x1b);	/* esc */
957 		default:
958 			return (-1);
959 		}
960 	} else {
961 		return (-1);
962 	}
963 }
964 
965 static int
966 vidc_ischar(struct console *cp __unused)
967 {
968 	int i;
969 
970 	for (i = 0; i < KEYBUFSZ; i++) {
971 		if (keybuf[i] != 0) {
972 			return (1);
973 		}
974 	}
975 
976 	v86.ctl = V86_FLAGS;
977 	v86.addr = 0x16;
978 	v86.eax = 0x100;
979 	v86int();
980 	return (!V86_ZR(v86.efl));
981 }
982 
983 static void
984 vidc_devinfo(struct console *cp __unused)
985 {
986 	if (plat_stdout_is_framebuffer()) {
987 		printf("\tVESA %ux%ux%u framebuffer mode %#x",
988 		    gfx_fb.framebuffer_common.framebuffer_width,
989 		    gfx_fb.framebuffer_common.framebuffer_height,
990 		    gfx_fb.framebuffer_common.framebuffer_bpp,
991 		    vbe_get_mode());
992 	} else {
993 		printf("\tVGA %ux%u text mode", TEXT_COLS, TEXT_ROWS);
994 	}
995 }
996 
997 #if KEYBOARD_PROBE
998 
999 #define	PROBE_MAXRETRY	5
1000 #define	PROBE_MAXWAIT	400
1001 #define	IO_DUMMY	0x84
1002 #define	IO_KBD		0x060		/* 8042 Keyboard */
1003 
1004 /* selected defines from kbdio.h */
1005 #define	KBD_STATUS_PORT		4	/* status port, read */
1006 /* data port, read/write also used as keyboard command and mouse command port */
1007 #define	KBD_DATA_PORT		0
1008 #define	KBDC_ECHO		0x00ee
1009 #define	KBDS_ANY_BUFFER_FULL	0x0001
1010 #define	KBDS_INPUT_BUFFER_FULL	0x0002
1011 #define	KBD_ECHO		0x00ee
1012 
1013 /* 7 microsec delay necessary for some keyboard controllers */
1014 static void
1015 delay7(void)
1016 {
1017 	/*
1018 	 * I know this is broken, but no timer is available yet at this stage...
1019 	 * See also comments in `delay1ms()'.
1020 	 */
1021 	inb(IO_DUMMY); inb(IO_DUMMY);
1022 	inb(IO_DUMMY); inb(IO_DUMMY);
1023 	inb(IO_DUMMY); inb(IO_DUMMY);
1024 }
1025 
1026 /*
1027  * This routine uses an inb to an unused port, the time to execute that
1028  * inb is approximately 1.25uS.  This value is pretty constant across
1029  * all CPU's and all buses, with the exception of some PCI implentations
1030  * that do not forward this I/O address to the ISA bus as they know it
1031  * is not a valid ISA bus address, those machines execute this inb in
1032  * 60 nS :-(.
1033  *
1034  */
1035 static void
1036 delay1ms(void)
1037 {
1038 	int i = 800;
1039 	while (--i >= 0)
1040 		(void) inb(0x84);
1041 }
1042 
1043 /*
1044  * We use the presence/absence of a keyboard to determine whether the internal
1045  * console can be used for input.
1046  *
1047  * Perform a simple test on the keyboard; issue the ECHO command and see
1048  * if the right answer is returned. We don't do anything as drastic as
1049  * full keyboard reset; it will be too troublesome and take too much time.
1050  */
1051 static int
1052 probe_keyboard(void)
1053 {
1054 	int retry = PROBE_MAXRETRY;
1055 	int wait;
1056 	int i;
1057 
1058 	while (--retry >= 0) {
1059 		/* flush any noise */
1060 		while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
1061 			delay7();
1062 			inb(IO_KBD + KBD_DATA_PORT);
1063 			delay1ms();
1064 		}
1065 
1066 		/* wait until the controller can accept a command */
1067 		for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1068 			if (((i = inb(IO_KBD + KBD_STATUS_PORT)) &
1069 			    (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL))
1070 			    == 0)
1071 				break;
1072 			if (i & KBDS_ANY_BUFFER_FULL) {
1073 				delay7();
1074 				inb(IO_KBD + KBD_DATA_PORT);
1075 			}
1076 			delay1ms();
1077 		}
1078 		if (wait <= 0)
1079 			continue;
1080 
1081 		/* send the ECHO command */
1082 		outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO);
1083 
1084 		/* wait for a response */
1085 		for (wait = PROBE_MAXWAIT; wait > 0; --wait) {
1086 			if (inb(IO_KBD + KBD_STATUS_PORT) &
1087 			    KBDS_ANY_BUFFER_FULL)
1088 				break;
1089 			delay1ms();
1090 		}
1091 		if (wait <= 0)
1092 			continue;
1093 
1094 		delay7();
1095 		i = inb(IO_KBD + KBD_DATA_PORT);
1096 #ifdef PROBE_KBD_BEBUG
1097 		printf("probe_keyboard: got 0x%x.\n", i);
1098 #endif
1099 		if (i == KBD_ECHO) {
1100 			/* got the right answer */
1101 			return (1);
1102 		}
1103 	}
1104 
1105 	return (0);
1106 }
1107 #endif /* KEYBOARD_PROBE */
1108