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