xref: /freebsd/sys/dev/atkbdc/atkbd.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_compat.h"
32 #include "opt_kbd.h"
33 #include "opt_atkbd.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/eventhandler.h>
40 #include <sys/proc.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 
47 #include <sys/kbio.h>
48 #include <dev/kbd/kbdreg.h>
49 #include <dev/atkbdc/atkbdreg.h>
50 #include <dev/atkbdc/atkbdcreg.h>
51 
52 typedef struct atkbd_state {
53 	KBDC		kbdc;		/* keyboard controller */
54 	int		ks_mode;	/* input mode (K_XLATE,K_RAW,K_CODE) */
55 	int		ks_flags;	/* flags */
56 #define COMPOSE		(1 << 0)
57 	int		ks_polling;
58 	int		ks_state;	/* shift/lock key state */
59 	int		ks_accents;	/* accent key index (> 0) */
60 	u_int		ks_composed_char; /* composed char code (> 0) */
61 	u_char		ks_prefix;	/* AT scan code prefix */
62 	struct callout	ks_timer;
63 } atkbd_state_t;
64 
65 static void		atkbd_timeout(void *arg);
66 static void		atkbd_shutdown_final(void *v);
67 static int		atkbd_reset(KBDC kbdc, int flags, int c);
68 
69 #define HAS_QUIRK(p, q)		(((atkbdc_softc_t *)(p))->quirks & q)
70 #define ALLOW_DISABLE_KBD(kbdc)	!HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
71 
72 #define DEFAULT_DELAY		0x1  /* 500ms */
73 #define DEFAULT_RATE		0x10 /* 14Hz */
74 
75 int
76 atkbd_probe_unit(device_t dev, int irq, int flags)
77 {
78 	keyboard_switch_t *sw;
79 	int args[2];
80 	int error;
81 
82 	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
83 	if (sw == NULL)
84 		return ENXIO;
85 
86 	args[0] = device_get_unit(device_get_parent(dev));
87 	args[1] = irq;
88 	error = (*sw->probe)(device_get_unit(dev), args, flags);
89 	if (error)
90 		return error;
91 	return 0;
92 }
93 
94 int
95 atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
96 {
97 	keyboard_switch_t *sw;
98 	atkbd_state_t *state;
99 	int args[2];
100 	int error;
101 	int unit;
102 
103 	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
104 	if (sw == NULL)
105 		return ENXIO;
106 
107 	/* reset, initialize and enable the device */
108 	unit = device_get_unit(dev);
109 	args[0] = device_get_unit(device_get_parent(dev));
110 	args[1] = irq;
111 	*kbd = NULL;
112 	error = (*sw->probe)(unit, args, flags);
113 	if (error)
114 		return error;
115 	error = (*sw->init)(unit, kbd, args, flags);
116 	if (error)
117 		return error;
118 	(*sw->enable)(*kbd);
119 
120 #ifdef KBD_INSTALL_CDEV
121 	/* attach a virtual keyboard cdev */
122 	error = kbd_attach(*kbd);
123 	if (error)
124 		return error;
125 #endif
126 
127 	/*
128 	 * This is a kludge to compensate for lost keyboard interrupts.
129 	 * A similar code used to be in syscons. See below. XXX
130 	 */
131 	state = (atkbd_state_t *)(*kbd)->kb_data;
132 	callout_init(&state->ks_timer, 0);
133 	atkbd_timeout(*kbd);
134 
135 	if (bootverbose)
136 		(*sw->diag)(*kbd, bootverbose);
137 
138 	EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
139 	    SHUTDOWN_PRI_DEFAULT);
140 
141 	return 0;
142 }
143 
144 static void
145 atkbd_timeout(void *arg)
146 {
147 	atkbd_state_t *state;
148 	keyboard_t *kbd;
149 	int s;
150 
151 	/*
152 	 * The original text of the following comments are extracted
153 	 * from syscons.c (1.287)
154 	 *
155 	 * With release 2.1 of the Xaccel server, the keyboard is left
156 	 * hanging pretty often. Apparently an interrupt from the
157 	 * keyboard is lost, and I don't know why (yet).
158 	 * This ugly hack calls the low-level interrupt routine if input
159 	 * is ready for the keyboard and conveniently hides the problem. XXX
160 	 *
161 	 * Try removing anything stuck in the keyboard controller; whether
162 	 * it's a keyboard scan code or mouse data. The low-level
163 	 * interrupt routine doesn't read the mouse data directly,
164 	 * but the keyboard controller driver will, as a side effect.
165 	 */
166 	/*
167 	 * And here is bde's original comment about this:
168 	 *
169 	 * This is necessary to handle edge triggered interrupts - if we
170 	 * returned when our IRQ is high due to unserviced input, then there
171 	 * would be no more keyboard IRQs until the keyboard is reset by
172 	 * external powers.
173 	 *
174 	 * The keyboard apparently unwedges the irq in most cases.
175 	 */
176 	s = spltty();
177 	kbd = (keyboard_t *)arg;
178 	if (kbdd_lock(kbd, TRUE)) {
179 		/*
180 		 * We have seen the lock flag is not set. Let's reset
181 		 * the flag early, otherwise the LED update routine fails
182 		 * which may want the lock during the interrupt routine.
183 		 */
184 		kbdd_lock(kbd, FALSE);
185 		if (kbdd_check_char(kbd))
186 			kbdd_intr(kbd, NULL);
187 	}
188 	splx(s);
189 	state = (atkbd_state_t *)kbd->kb_data;
190 	callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg);
191 }
192 
193 /* LOW-LEVEL */
194 
195 #define ATKBD_DEFAULT	0
196 
197 /* keyboard driver declaration */
198 static int		atkbd_configure(int flags);
199 static kbd_probe_t	atkbd_probe;
200 static kbd_init_t	atkbd_init;
201 static kbd_term_t	atkbd_term;
202 static kbd_intr_t	atkbd_intr;
203 static kbd_test_if_t	atkbd_test_if;
204 static kbd_enable_t	atkbd_enable;
205 static kbd_disable_t	atkbd_disable;
206 static kbd_read_t	atkbd_read;
207 static kbd_check_t	atkbd_check;
208 static kbd_read_char_t	atkbd_read_char;
209 static kbd_check_char_t	atkbd_check_char;
210 static kbd_ioctl_t	atkbd_ioctl;
211 static kbd_lock_t	atkbd_lock;
212 static kbd_clear_state_t atkbd_clear_state;
213 static kbd_get_state_t	atkbd_get_state;
214 static kbd_set_state_t	atkbd_set_state;
215 static kbd_poll_mode_t	atkbd_poll;
216 
217 static keyboard_switch_t atkbdsw = {
218 	atkbd_probe,
219 	atkbd_init,
220 	atkbd_term,
221 	atkbd_intr,
222 	atkbd_test_if,
223 	atkbd_enable,
224 	atkbd_disable,
225 	atkbd_read,
226 	atkbd_check,
227 	atkbd_read_char,
228 	atkbd_check_char,
229 	atkbd_ioctl,
230 	atkbd_lock,
231 	atkbd_clear_state,
232 	atkbd_get_state,
233 	atkbd_set_state,
234 	genkbd_get_fkeystr,
235 	atkbd_poll,
236 	genkbd_diag,
237 };
238 
239 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
240 
241 /* local functions */
242 static int		set_typematic(keyboard_t *kbd);
243 static int		setup_kbd_port(KBDC kbdc, int port, int intr);
244 static int		get_kbd_echo(KBDC kbdc);
245 static int		probe_keyboard(KBDC kbdc, int flags);
246 static int		init_keyboard(KBDC kbdc, int *type, int flags);
247 static int		write_kbd(KBDC kbdc, int command, int data);
248 static int		get_kbd_id(KBDC kbdc);
249 static int		typematic(int delay, int rate);
250 static int		typematic_delay(int delay);
251 static int		typematic_rate(int rate);
252 
253 /* local variables */
254 
255 /* the initial key map, accent map and fkey strings */
256 #ifdef ATKBD_DFLT_KEYMAP
257 #define KBD_DFLT_KEYMAP
258 #include "atkbdmap.h"
259 #endif
260 #include <dev/kbd/kbdtables.h>
261 
262 /* structures for the default keyboard */
263 static keyboard_t	default_kbd;
264 static atkbd_state_t	default_kbd_state;
265 static keymap_t		default_keymap;
266 static accentmap_t	default_accentmap;
267 static fkeytab_t	default_fkeytab[NUM_FKEYS];
268 
269 /*
270  * The back door to the keyboard driver!
271  * This function is called by the console driver, via the kbdio module,
272  * to tickle keyboard drivers when the low-level console is being initialized.
273  * Almost nothing in the kernel has been initialied yet.  Try to probe
274  * keyboards if possible.
275  * NOTE: because of the way the low-level console is initialized, this routine
276  * may be called more than once!!
277  */
278 static int
279 atkbd_configure(int flags)
280 {
281 	keyboard_t *kbd;
282 	int arg[2];
283 	int i;
284 
285 	/*
286 	 * Probe the keyboard controller, if not present or if the driver
287 	 * is disabled, unregister the keyboard if any.
288 	 */
289 	if (atkbdc_configure() != 0 ||
290 	    resource_disabled("atkbd", ATKBD_DEFAULT)) {
291 		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
292 		if (i >= 0) {
293 			kbd = kbd_get_keyboard(i);
294 			kbd_unregister(kbd);
295 			kbd->kb_flags &= ~KB_REGISTERED;
296 		}
297 		return 0;
298 	}
299 
300 	/* XXX: a kludge to obtain the device configuration flags */
301 	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
302 		flags |= i;
303 
304 	/* probe the default keyboard */
305 	arg[0] = -1;
306 	arg[1] = -1;
307 	kbd = NULL;
308 	if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
309 		return 0;
310 	if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
311 		return 0;
312 
313 	/* return the number of found keyboards */
314 	return 1;
315 }
316 
317 /* low-level functions */
318 
319 /* detect a keyboard */
320 static int
321 atkbd_probe(int unit, void *arg, int flags)
322 {
323 	KBDC kbdc;
324 	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
325 
326 	/* XXX */
327 	if (unit == ATKBD_DEFAULT) {
328 		if (KBD_IS_PROBED(&default_kbd))
329 			return 0;
330 	}
331 
332 	kbdc = atkbdc_open(data[0]);
333 	if (kbdc == NULL)
334 		return ENXIO;
335 	if (probe_keyboard(kbdc, flags)) {
336 		if (flags & KB_CONF_FAIL_IF_NO_KBD)
337 			return ENXIO;
338 	}
339 	return 0;
340 }
341 
342 /* reset and initialize the device */
343 static int
344 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
345 {
346 	keyboard_t *kbd;
347 	atkbd_state_t *state;
348 	keymap_t *keymap;
349 	accentmap_t *accmap;
350 	fkeytab_t *fkeymap;
351 	int fkeymap_size;
352 	int delay[2];
353 	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
354 	int error, needfree;
355 
356 	/* XXX */
357 	if (unit == ATKBD_DEFAULT) {
358 		*kbdp = kbd = &default_kbd;
359 		if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
360 			return 0;
361 		state = &default_kbd_state;
362 		keymap = &default_keymap;
363 		accmap = &default_accentmap;
364 		fkeymap = default_fkeytab;
365 		fkeymap_size =
366 			sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
367 		needfree = 0;
368 	} else if (*kbdp == NULL) {
369 		*kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
370 		state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
371 		/* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
372 		keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
373 		accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
374 		fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
375 		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
376 		needfree = 1;
377 		if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
378 		     || (accmap == NULL) || (fkeymap == NULL)) {
379 			error = ENOMEM;
380 			goto bad;
381 		}
382 	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
383 		return 0;
384 	} else {
385 		kbd = *kbdp;
386 		state = (atkbd_state_t *)kbd->kb_data;
387 		bzero(state, sizeof(*state));
388 		keymap = kbd->kb_keymap;
389 		accmap = kbd->kb_accentmap;
390 		fkeymap = kbd->kb_fkeytab;
391 		fkeymap_size = kbd->kb_fkeytab_size;
392 		needfree = 0;
393 	}
394 
395 	if (!KBD_IS_PROBED(kbd)) {
396 		state->kbdc = atkbdc_open(data[0]);
397 		if (state->kbdc == NULL) {
398 			error = ENXIO;
399 			goto bad;
400 		}
401 		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
402 				0, 0);
403 		bcopy(&key_map, keymap, sizeof(key_map));
404 		bcopy(&accent_map, accmap, sizeof(accent_map));
405 		bcopy(fkey_tab, fkeymap,
406 		    imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
407 		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
408 		kbd->kb_data = (void *)state;
409 
410 		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
411 			if (flags & KB_CONF_FAIL_IF_NO_KBD) {
412 				error = ENXIO;
413 				goto bad;
414 			}
415 		} else {
416 			KBD_FOUND_DEVICE(kbd);
417 		}
418 		atkbd_clear_state(kbd);
419 		state->ks_mode = K_XLATE;
420 		/*
421 		 * FIXME: set the initial value for lock keys in ks_state
422 		 * according to the BIOS data?
423 		 */
424 		KBD_PROBE_DONE(kbd);
425 	}
426 	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
427 		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
428 		if (KBD_HAS_DEVICE(kbd)
429 		    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
430 		    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
431 			kbd_unregister(kbd);
432 			error = ENXIO;
433 			goto bad;
434 		}
435 		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
436 		set_typematic(kbd);
437 		delay[0] = kbd->kb_delay1;
438 		delay[1] = kbd->kb_delay2;
439 		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
440 		KBD_INIT_DONE(kbd);
441 	}
442 	if (!KBD_IS_CONFIGURED(kbd)) {
443 		if (kbd_register(kbd) < 0) {
444 			error = ENXIO;
445 			goto bad;
446 		}
447 		KBD_CONFIG_DONE(kbd);
448 	}
449 
450 	return 0;
451 bad:
452 	if (needfree) {
453 		if (state != NULL)
454 			free(state, M_DEVBUF);
455 		if (keymap != NULL)
456 			free(keymap, M_DEVBUF);
457 		if (accmap != NULL)
458 			free(accmap, M_DEVBUF);
459 		if (fkeymap != NULL)
460 			free(fkeymap, M_DEVBUF);
461 		if (kbd != NULL) {
462 			free(kbd, M_DEVBUF);
463 			*kbdp = NULL;	/* insure ref doesn't leak to caller */
464 		}
465 	}
466 	return error;
467 }
468 
469 /* finish using this keyboard */
470 static int
471 atkbd_term(keyboard_t *kbd)
472 {
473 	atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
474 
475 	kbd_unregister(kbd);
476 	callout_drain(&state->ks_timer);
477 	return 0;
478 }
479 
480 /* keyboard interrupt routine */
481 static int
482 atkbd_intr(keyboard_t *kbd, void *arg)
483 {
484 	atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
485 	int delay[2];
486 	int c;
487 
488 	if (!KBD_HAS_DEVICE(kbd)) {
489 		/*
490 		 * The keyboard was not detected before;
491 		 * it must have been reconnected!
492 		 */
493 		init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
494 		KBD_FOUND_DEVICE(kbd);
495 		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
496 		set_typematic(kbd);
497 		delay[0] = kbd->kb_delay1;
498 		delay[1] = kbd->kb_delay2;
499 		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
500 	}
501 
502 	if (state->ks_polling)
503 		return 0;
504 
505 	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
506 		/* let the callback function to process the input */
507 		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
508 					    kbd->kb_callback.kc_arg);
509 	} else {
510 		/* read and discard the input; no one is waiting for input */
511 		do {
512 			c = atkbd_read_char(kbd, FALSE);
513 		} while (c != NOKEY);
514 	}
515 	return 0;
516 }
517 
518 /* test the interface to the device */
519 static int
520 atkbd_test_if(keyboard_t *kbd)
521 {
522 	int error;
523 	int s;
524 
525 	error = 0;
526 	empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
527 	s = spltty();
528 	if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
529 		error = EIO;
530 	else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
531 		error = EIO;
532 	splx(s);
533 
534 	return error;
535 }
536 
537 /*
538  * Enable the access to the device; until this function is called,
539  * the client cannot read from the keyboard.
540  */
541 static int
542 atkbd_enable(keyboard_t *kbd)
543 {
544 	int s;
545 
546 	s = spltty();
547 	KBD_ACTIVATE(kbd);
548 	splx(s);
549 	return 0;
550 }
551 
552 /* disallow the access to the device */
553 static int
554 atkbd_disable(keyboard_t *kbd)
555 {
556 	int s;
557 
558 	s = spltty();
559 	KBD_DEACTIVATE(kbd);
560 	splx(s);
561 	return 0;
562 }
563 
564 /* read one byte from the keyboard if it's allowed */
565 static int
566 atkbd_read(keyboard_t *kbd, int wait)
567 {
568 	int c;
569 
570 	if (wait)
571 		c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
572 	else
573 		c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
574 	if (c != -1)
575 		++kbd->kb_count;
576 	return (KBD_IS_ACTIVE(kbd) ? c : -1);
577 }
578 
579 /* check if data is waiting */
580 static int
581 atkbd_check(keyboard_t *kbd)
582 {
583 	if (!KBD_IS_ACTIVE(kbd))
584 		return FALSE;
585 	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
586 }
587 
588 /* read char from the keyboard */
589 static u_int
590 atkbd_read_char(keyboard_t *kbd, int wait)
591 {
592 	atkbd_state_t *state;
593 	u_int action;
594 	int scancode;
595 	int keycode;
596 
597 	state = (atkbd_state_t *)kbd->kb_data;
598 next_code:
599 	/* do we have a composed char to return? */
600 	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
601 		action = state->ks_composed_char;
602 		state->ks_composed_char = 0;
603 		if (action > UCHAR_MAX)
604 			return ERRKEY;
605 		return action;
606 	}
607 
608 	/* see if there is something in the keyboard port */
609 	if (wait) {
610 		do {
611 			scancode = read_kbd_data(state->kbdc);
612 		} while (scancode == -1);
613 	} else {
614 		scancode = read_kbd_data_no_wait(state->kbdc);
615 		if (scancode == -1)
616 			return NOKEY;
617 	}
618 	++kbd->kb_count;
619 
620 #if KBDIO_DEBUG >= 10
621 	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
622 #endif
623 
624 	/* return the byte as is for the K_RAW mode */
625 	if (state->ks_mode == K_RAW)
626 		return scancode;
627 
628 	/* translate the scan code into a keycode */
629 	keycode = scancode & 0x7F;
630 	switch (state->ks_prefix) {
631 	case 0x00:	/* normal scancode */
632 		switch(scancode) {
633 		case 0xB8:	/* left alt (compose key) released */
634 			if (state->ks_flags & COMPOSE) {
635 				state->ks_flags &= ~COMPOSE;
636 				if (state->ks_composed_char > UCHAR_MAX)
637 					state->ks_composed_char = 0;
638 			}
639 			break;
640 		case 0x38:	/* left alt (compose key) pressed */
641 			if (!(state->ks_flags & COMPOSE)) {
642 				state->ks_flags |= COMPOSE;
643 				state->ks_composed_char = 0;
644 			}
645 			break;
646 		case 0xE0:
647 		case 0xE1:
648 			state->ks_prefix = scancode;
649 			goto next_code;
650 		}
651 		break;
652 	case 0xE0:		/* 0xE0 prefix */
653 		state->ks_prefix = 0;
654 		switch (keycode) {
655 		case 0x1C:	/* right enter key */
656 			keycode = 0x59;
657 			break;
658 		case 0x1D:	/* right ctrl key */
659 			keycode = 0x5A;
660 			break;
661 		case 0x35:	/* keypad divide key */
662 			keycode = 0x5B;
663 			break;
664 		case 0x37:	/* print scrn key */
665 			keycode = 0x5C;
666 			break;
667 		case 0x38:	/* right alt key (alt gr) */
668 			keycode = 0x5D;
669 			break;
670 		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
671 			keycode = 0x68;
672 			break;
673 		case 0x47:	/* grey home key */
674 			keycode = 0x5E;
675 			break;
676 		case 0x48:	/* grey up arrow key */
677 			keycode = 0x5F;
678 			break;
679 		case 0x49:	/* grey page up key */
680 			keycode = 0x60;
681 			break;
682 		case 0x4B:	/* grey left arrow key */
683 			keycode = 0x61;
684 			break;
685 		case 0x4D:	/* grey right arrow key */
686 			keycode = 0x62;
687 			break;
688 		case 0x4F:	/* grey end key */
689 			keycode = 0x63;
690 			break;
691 		case 0x50:	/* grey down arrow key */
692 			keycode = 0x64;
693 			break;
694 		case 0x51:	/* grey page down key */
695 			keycode = 0x65;
696 			break;
697 		case 0x52:	/* grey insert key */
698 			keycode = 0x66;
699 			break;
700 		case 0x53:	/* grey delete key */
701 			keycode = 0x67;
702 			break;
703 			/* the following 3 are only used on the MS "Natural" keyboard */
704 		case 0x5b:	/* left Window key */
705 			keycode = 0x69;
706 			break;
707 		case 0x5c:	/* right Window key */
708 			keycode = 0x6a;
709 			break;
710 		case 0x5d:	/* menu key */
711 			keycode = 0x6b;
712 			break;
713 		case 0x5e:	/* power key */
714 			keycode = 0x6d;
715 			break;
716 		case 0x5f:	/* sleep key */
717 			keycode = 0x6e;
718 			break;
719 		case 0x63:	/* wake key */
720 			keycode = 0x6f;
721 			break;
722 		default:	/* ignore everything else */
723 			goto next_code;
724 		}
725 		break;
726    	case 0xE1:	/* 0xE1 prefix */
727 		/*
728 		 * The pause/break key on the 101 keyboard produces:
729 		 * E1-1D-45 E1-9D-C5
730 		 * Ctrl-pause/break produces:
731 		 * E0-46 E0-C6 (See above.)
732 		 */
733 		state->ks_prefix = 0;
734 		if (keycode == 0x1D)
735 			state->ks_prefix = 0x1D;
736 		goto next_code;
737 		/* NOT REACHED */
738    	case 0x1D:	/* pause / break */
739 		state->ks_prefix = 0;
740 		if (keycode != 0x45)
741 			goto next_code;
742 		keycode = 0x68;
743 		break;
744 	}
745 
746 	if (kbd->kb_type == KB_84) {
747 		switch (keycode) {
748 		case 0x37:	/* *(numpad)/print screen */
749 			if (state->ks_flags & SHIFTS)
750 				keycode = 0x5c;	/* print screen */
751 			break;
752 		case 0x45:	/* num lock/pause */
753 			if (state->ks_flags & CTLS)
754 				keycode = 0x68;	/* pause */
755 			break;
756 		case 0x46:	/* scroll lock/break */
757 			if (state->ks_flags & CTLS)
758 				keycode = 0x6c;	/* break */
759 			break;
760 		}
761 	} else if (kbd->kb_type == KB_101) {
762 		switch (keycode) {
763 		case 0x5c:	/* print screen */
764 			if (state->ks_flags & ALTS)
765 				keycode = 0x54;	/* sysrq */
766 			break;
767 		case 0x68:	/* pause/break */
768 			if (state->ks_flags & CTLS)
769 				keycode = 0x6c;	/* break */
770 			break;
771 		}
772 	}
773 
774 	/* return the key code in the K_CODE mode */
775 	if (state->ks_mode == K_CODE)
776 		return (keycode | (scancode & 0x80));
777 
778 	/* compose a character code */
779 	if (state->ks_flags & COMPOSE) {
780 		switch (keycode | (scancode & 0x80)) {
781 		/* key pressed, process it */
782 		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
783 			state->ks_composed_char *= 10;
784 			state->ks_composed_char += keycode - 0x40;
785 			if (state->ks_composed_char > UCHAR_MAX)
786 				return ERRKEY;
787 			goto next_code;
788 		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
789 			state->ks_composed_char *= 10;
790 			state->ks_composed_char += keycode - 0x47;
791 			if (state->ks_composed_char > UCHAR_MAX)
792 				return ERRKEY;
793 			goto next_code;
794 		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
795 			state->ks_composed_char *= 10;
796 			state->ks_composed_char += keycode - 0x4E;
797 			if (state->ks_composed_char > UCHAR_MAX)
798 				return ERRKEY;
799 			goto next_code;
800 		case 0x52:				/* keypad 0 */
801 			state->ks_composed_char *= 10;
802 			if (state->ks_composed_char > UCHAR_MAX)
803 				return ERRKEY;
804 			goto next_code;
805 
806 		/* key released, no interest here */
807 		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
808 		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
809 		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
810 		case 0xD2:				/* keypad 0 */
811 			goto next_code;
812 
813 		case 0x38:				/* left alt key */
814 			break;
815 
816 		default:
817 			if (state->ks_composed_char > 0) {
818 				state->ks_flags &= ~COMPOSE;
819 				state->ks_composed_char = 0;
820 				return ERRKEY;
821 			}
822 			break;
823 		}
824 	}
825 
826 	/* keycode to key action */
827 	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
828 				  &state->ks_state, &state->ks_accents);
829 	if (action == NOKEY)
830 		goto next_code;
831 	else
832 		return action;
833 }
834 
835 /* check if char is waiting */
836 static int
837 atkbd_check_char(keyboard_t *kbd)
838 {
839 	atkbd_state_t *state;
840 
841 	if (!KBD_IS_ACTIVE(kbd))
842 		return FALSE;
843 	state = (atkbd_state_t *)kbd->kb_data;
844 	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
845 		return TRUE;
846 	return kbdc_data_ready(state->kbdc);
847 }
848 
849 /* some useful control functions */
850 static int
851 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
852 {
853 	/* translate LED_XXX bits into the device specific bits */
854 	static u_char ledmap[8] = {
855 		0, 4, 2, 6, 1, 5, 3, 7,
856 	};
857 	atkbd_state_t *state = kbd->kb_data;
858 	int error;
859 	int s;
860 	int i;
861 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
862     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
863 	int ival;
864 #endif
865 
866 	s = spltty();
867 	switch (cmd) {
868 
869 	case KDGKBMODE:		/* get keyboard mode */
870 		*(int *)arg = state->ks_mode;
871 		break;
872 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
873     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
874 	case _IO('K', 7):
875 		ival = IOCPARM_IVAL(arg);
876 		arg = (caddr_t)&ival;
877 		/* FALLTHROUGH */
878 #endif
879 	case KDSKBMODE:		/* set keyboard mode */
880 		switch (*(int *)arg) {
881 		case K_XLATE:
882 			if (state->ks_mode != K_XLATE) {
883 				/* make lock key state and LED state match */
884 				state->ks_state &= ~LOCK_MASK;
885 				state->ks_state |= KBD_LED_VAL(kbd);
886 			}
887 			/* FALLTHROUGH */
888 		case K_RAW:
889 		case K_CODE:
890 			if (state->ks_mode != *(int *)arg) {
891 				atkbd_clear_state(kbd);
892 				state->ks_mode = *(int *)arg;
893 			}
894 			break;
895 		default:
896 			splx(s);
897 			return EINVAL;
898 		}
899 		break;
900 
901 	case KDGETLED:		/* get keyboard LED */
902 		*(int *)arg = KBD_LED_VAL(kbd);
903 		break;
904 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
905     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
906 	case _IO('K', 66):
907 		ival = IOCPARM_IVAL(arg);
908 		arg = (caddr_t)&ival;
909 		/* FALLTHROUGH */
910 #endif
911 	case KDSETLED:		/* set keyboard LED */
912 		/* NOTE: lock key state in ks_state won't be changed */
913 		if (*(int *)arg & ~LOCK_MASK) {
914 			splx(s);
915 			return EINVAL;
916 		}
917 		i = *(int *)arg;
918 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
919 		if (state->ks_mode == K_XLATE &&
920 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
921 			if (i & ALKED)
922 				i |= CLKED;
923 			else
924 				i &= ~CLKED;
925 		}
926 		if (KBD_HAS_DEVICE(kbd)) {
927 			error = write_kbd(state->kbdc, KBDC_SET_LEDS,
928 					  ledmap[i & LED_MASK]);
929 			if (error) {
930 				splx(s);
931 				return error;
932 			}
933 		}
934 		KBD_LED_VAL(kbd) = *(int *)arg;
935 		break;
936 
937 	case KDGKBSTATE:	/* get lock key state */
938 		*(int *)arg = state->ks_state & LOCK_MASK;
939 		break;
940 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
941     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
942 	case _IO('K', 20):
943 		ival = IOCPARM_IVAL(arg);
944 		arg = (caddr_t)&ival;
945 		/* FALLTHROUGH */
946 #endif
947 	case KDSKBSTATE:	/* set lock key state */
948 		if (*(int *)arg & ~LOCK_MASK) {
949 			splx(s);
950 			return EINVAL;
951 		}
952 		state->ks_state &= ~LOCK_MASK;
953 		state->ks_state |= *(int *)arg;
954 		splx(s);
955 		/* set LEDs and quit */
956 		return atkbd_ioctl(kbd, KDSETLED, arg);
957 
958 	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
959 		splx(s);
960 		if (!KBD_HAS_DEVICE(kbd))
961 			return 0;
962 		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
963 		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
964 		if (error == 0) {
965 			kbd->kb_delay1 = typematic_delay(i);
966 			kbd->kb_delay2 = typematic_rate(i);
967 		}
968 		return error;
969 
970 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
971     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
972 	case _IO('K', 67):
973 		ival = IOCPARM_IVAL(arg);
974 		arg = (caddr_t)&ival;
975 		/* FALLTHROUGH */
976 #endif
977 	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
978 		splx(s);
979 		if (!KBD_HAS_DEVICE(kbd))
980 			return 0;
981 		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
982 		if (error == 0) {
983 			kbd->kb_delay1 = typematic_delay(*(int *)arg);
984 			kbd->kb_delay2 = typematic_rate(*(int *)arg);
985 		}
986 		return error;
987 
988 	case PIO_KEYMAP:	/* set keyboard translation table */
989 	case OPIO_KEYMAP:	/* set keyboard translation table (compat) */
990 	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
991 	case PIO_DEADKEYMAP:	/* set accent key translation table */
992 		state->ks_accents = 0;
993 		/* FALLTHROUGH */
994 	default:
995 		splx(s);
996 		return genkbd_commonioctl(kbd, cmd, arg);
997 	}
998 
999 	splx(s);
1000 	return 0;
1001 }
1002 
1003 /* lock the access to the keyboard */
1004 static int
1005 atkbd_lock(keyboard_t *kbd, int lock)
1006 {
1007 	return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1008 }
1009 
1010 /* clear the internal state of the keyboard */
1011 static void
1012 atkbd_clear_state(keyboard_t *kbd)
1013 {
1014 	atkbd_state_t *state;
1015 
1016 	state = (atkbd_state_t *)kbd->kb_data;
1017 	state->ks_flags = 0;
1018 	state->ks_polling = 0;
1019 	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
1020 	state->ks_accents = 0;
1021 	state->ks_composed_char = 0;
1022 #if 0
1023 	state->ks_prefix = 0; /* XXX */
1024 #endif
1025 }
1026 
1027 /* save the internal state */
1028 static int
1029 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1030 {
1031 	if (len == 0)
1032 		return sizeof(atkbd_state_t);
1033 	if (len < sizeof(atkbd_state_t))
1034 		return -1;
1035 	bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1036 	return 0;
1037 }
1038 
1039 /* set the internal state */
1040 static int
1041 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1042 {
1043 	if (len < sizeof(atkbd_state_t))
1044 		return ENOMEM;
1045 	if (((atkbd_state_t *)kbd->kb_data)->kbdc
1046 		!= ((atkbd_state_t *)buf)->kbdc)
1047 		return ENOMEM;
1048 	bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1049 	return 0;
1050 }
1051 
1052 static int
1053 atkbd_poll(keyboard_t *kbd, int on)
1054 {
1055 	atkbd_state_t *state;
1056 	int s;
1057 
1058 	state = (atkbd_state_t *)kbd->kb_data;
1059 	s = spltty();
1060 	if (on)
1061 		++state->ks_polling;
1062 	else
1063 		--state->ks_polling;
1064 	splx(s);
1065 	return 0;
1066 }
1067 
1068 static void
1069 atkbd_shutdown_final(void *v)
1070 {
1071 #ifdef __sparc64__
1072 	keyboard_t *kbd = v;
1073 	KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1074 
1075 	/*
1076 	 * Turn off the translation in preparation for handing the keyboard
1077 	 * over to the OFW as the OBP driver doesn't use translation and
1078 	 * also doesn't disable it itself resulting in a broken keymap at
1079 	 * the boot prompt. Also disable the aux port and the interrupts as
1080 	 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1081 	 * disabling the interrupts doesn't cause real problems but the
1082 	 * responsiveness is a bit better when they are turned off.
1083 	 */
1084 	send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1085 	set_controller_command_byte(kbdc,
1086 	    KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1087 	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1088 	send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1089 #endif
1090 }
1091 
1092 static int
1093 atkbd_reset(KBDC kbdc, int flags, int c)
1094 {
1095 	/* reset keyboard hardware */
1096 	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1097 		/*
1098 		 * KEYBOARD ERROR
1099 		 * Keyboard reset may fail either because the keyboard
1100 		 * doen't exist, or because the keyboard doesn't pass
1101 		 * the self-test, or the keyboard controller on the
1102 		 * motherboard and the keyboard somehow fail to shake hands.
1103 		 * It is just possible, particularly in the last case,
1104 		 * that the keyboard controller may be left in a hung state.
1105 		 * test_controller() and test_kbd_port() appear to bring
1106 		 * the keyboard controller back (I don't know why and how,
1107 		 * though.)
1108 		 */
1109 		empty_both_buffers(kbdc, 10);
1110 		test_controller(kbdc);
1111 		test_kbd_port(kbdc);
1112 		/*
1113 		 * We could disable the keyboard port and interrupt... but,
1114 		 * the keyboard may still exist (see above).
1115 		 */
1116 		set_controller_command_byte(kbdc,
1117 		    ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1118 		if (bootverbose)
1119 			printf("atkbd: failed to reset the keyboard.\n");
1120 		return (EIO);
1121 	}
1122 	return (0);
1123 }
1124 
1125 /* local functions */
1126 
1127 static int
1128 set_typematic(keyboard_t *kbd)
1129 {
1130 	int val, error;
1131 	atkbd_state_t *state = kbd->kb_data;
1132 
1133 	val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
1134 	error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
1135 	if (error == 0) {
1136 		kbd->kb_delay1 = typematic_delay(val);
1137 		kbd->kb_delay2 = typematic_rate(val);
1138 	}
1139 
1140 	return (error);
1141 }
1142 
1143 static int
1144 setup_kbd_port(KBDC kbdc, int port, int intr)
1145 {
1146 	if (!set_controller_command_byte(kbdc,
1147 		KBD_KBD_CONTROL_BITS,
1148 		((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1149 		    | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1150 		return 1;
1151 	return 0;
1152 }
1153 
1154 static int
1155 get_kbd_echo(KBDC kbdc)
1156 {
1157 	/* enable the keyboard port, but disable the keyboard intr. */
1158 	if (setup_kbd_port(kbdc, TRUE, FALSE))
1159 		/* CONTROLLER ERROR: there is very little we can do... */
1160 		return ENXIO;
1161 
1162 	/* see if something is present */
1163 	write_kbd_command(kbdc, KBDC_ECHO);
1164 	if (read_kbd_data(kbdc) != KBD_ECHO) {
1165 		empty_both_buffers(kbdc, 10);
1166 		test_controller(kbdc);
1167 		test_kbd_port(kbdc);
1168 		return ENXIO;
1169 	}
1170 
1171 	/* enable the keyboard port and intr. */
1172 	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1173 		/*
1174 		 * CONTROLLER ERROR
1175 		 * This is serious; the keyboard intr is left disabled!
1176 		 */
1177 		return ENXIO;
1178 	}
1179 
1180 	return 0;
1181 }
1182 
1183 static int
1184 probe_keyboard(KBDC kbdc, int flags)
1185 {
1186 	/*
1187 	 * Don't try to print anything in this function.  The low-level
1188 	 * console may not have been initialized yet...
1189 	 */
1190 	int err;
1191 	int c;
1192 	int m;
1193 
1194 	if (!kbdc_lock(kbdc, TRUE)) {
1195 		/* driver error? */
1196 		return ENXIO;
1197 	}
1198 
1199 	/* temporarily block data transmission from the keyboard */
1200 	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1201 
1202 	/* flush any noise in the buffer */
1203 	empty_both_buffers(kbdc, 100);
1204 
1205 	/* save the current keyboard controller command byte */
1206 	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1207 	c = get_controller_command_byte(kbdc);
1208 	if (c == -1) {
1209 		/* CONTROLLER ERROR */
1210 		kbdc_set_device_mask(kbdc, m);
1211 		kbdc_lock(kbdc, FALSE);
1212 		return ENXIO;
1213 	}
1214 
1215 	/*
1216 	 * The keyboard may have been screwed up by the boot block.
1217 	 * We may just be able to recover from error by testing the controller
1218 	 * and the keyboard port. The controller command byte needs to be
1219 	 * saved before this recovery operation, as some controllers seem
1220 	 * to set the command byte to particular values.
1221 	 */
1222 	test_controller(kbdc);
1223 	if (!(flags & KB_CONF_NO_PROBE_TEST))
1224 		test_kbd_port(kbdc);
1225 
1226 	err = get_kbd_echo(kbdc);
1227 
1228 	/*
1229 	 * Even if the keyboard doesn't seem to be present (err != 0),
1230 	 * we shall enable the keyboard port and interrupt so that
1231 	 * the driver will be operable when the keyboard is attached
1232 	 * to the system later.  It is NOT recommended to hot-plug
1233 	 * the AT keyboard, but many people do so...
1234 	 */
1235 	kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1236 	setup_kbd_port(kbdc, TRUE, TRUE);
1237 #if 0
1238 	if (err == 0) {
1239 		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1240 	} else {
1241 		/* try to restore the command byte as before */
1242 		set_controller_command_byte(kbdc,
1243 		    ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1244 		kbdc_set_device_mask(kbdc, m);
1245 	}
1246 #endif
1247 
1248 	kbdc_lock(kbdc, FALSE);
1249 	return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
1250 }
1251 
1252 static int
1253 init_keyboard(KBDC kbdc, int *type, int flags)
1254 {
1255 	int codeset;
1256 	int id;
1257 	int c;
1258 
1259 	if (!kbdc_lock(kbdc, TRUE)) {
1260 		/* driver error? */
1261 		return EIO;
1262 	}
1263 
1264 	/* temporarily block data transmission from the keyboard */
1265 	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1266 
1267 	/* save the current controller command byte */
1268 	empty_both_buffers(kbdc, 200);
1269 	c = get_controller_command_byte(kbdc);
1270 	if (c == -1) {
1271 		/* CONTROLLER ERROR */
1272 		kbdc_lock(kbdc, FALSE);
1273 		printf("atkbd: unable to get the current command byte value.\n");
1274 		return EIO;
1275 	}
1276 	if (bootverbose)
1277 		printf("atkbd: the current kbd controller command byte %04x\n",
1278 		   c);
1279 #if 0
1280 	/* override the keyboard lock switch */
1281 	c |= KBD_OVERRIDE_KBD_LOCK;
1282 #endif
1283 
1284 	/* enable the keyboard port, but disable the keyboard intr. */
1285 	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1286 		/* CONTROLLER ERROR: there is very little we can do... */
1287 		printf("atkbd: unable to set the command byte.\n");
1288 		kbdc_lock(kbdc, FALSE);
1289 		return EIO;
1290 	}
1291 
1292 	if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1293 	    atkbd_reset(kbdc, flags, c)) {
1294 		kbdc_lock(kbdc, FALSE);
1295 		return EIO;
1296 	}
1297 
1298 	/*
1299 	 * Check if we have an XT keyboard before we attempt to reset it.
1300 	 * The procedure assumes that the keyboard and the controller have
1301 	 * been set up properly by BIOS and have not been messed up
1302 	 * during the boot process.
1303 	 */
1304 	codeset = -1;
1305 	if (flags & KB_CONF_ALT_SCANCODESET)
1306 		/* the user says there is a XT keyboard */
1307 		codeset = 1;
1308 #ifdef KBD_DETECT_XT_KEYBOARD
1309 	else if ((c & KBD_TRANSLATION) == 0) {
1310 		/* SET_SCANCODE_SET is not always supported; ignore error */
1311 		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1312 			== KBD_ACK)
1313 			codeset = read_kbd_data(kbdc);
1314 	}
1315 	if (bootverbose)
1316 		printf("atkbd: scancode set %d\n", codeset);
1317 #endif /* KBD_DETECT_XT_KEYBOARD */
1318 
1319 	*type = KB_OTHER;
1320 	id = get_kbd_id(kbdc);
1321 	switch(id) {
1322 	case 0x41ab:	/* 101/102/... Enhanced */
1323 	case 0x83ab:	/* ditto */
1324 	case 0x54ab:	/* SpaceSaver */
1325 	case 0x84ab:	/* ditto */
1326 #if 0
1327 	case 0x90ab:	/* 'G' */
1328 	case 0x91ab:	/* 'P' */
1329 	case 0x92ab:	/* 'A' */
1330 #endif
1331 		*type = KB_101;
1332 		break;
1333 	case -1:	/* AT 84 keyboard doesn't return ID */
1334 		*type = KB_84;
1335 		break;
1336 	default:
1337 		break;
1338 	}
1339 	if (bootverbose)
1340 		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1341 
1342 	if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1343 	    atkbd_reset(kbdc, flags, c)) {
1344 		kbdc_lock(kbdc, FALSE);
1345 		return EIO;
1346 	}
1347 
1348 	/*
1349 	 * Allow us to set the XT_KEYBD flag so that keyboards
1350 	 * such as those on the IBM ThinkPad laptop computers can be used
1351 	 * with the standard console driver.
1352 	 */
1353 	if (codeset == 1) {
1354 		if (send_kbd_command_and_data(kbdc,
1355 			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1356 			/* XT kbd doesn't need scan code translation */
1357 			c &= ~KBD_TRANSLATION;
1358 		} else {
1359 			/*
1360 			 * KEYBOARD ERROR
1361 			 * The XT kbd isn't usable unless the proper scan
1362 			 * code set is selected.
1363 			 */
1364 			set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1365 			    ? 0xff : KBD_KBD_CONTROL_BITS, c);
1366 			kbdc_lock(kbdc, FALSE);
1367 			printf("atkbd: unable to set the XT keyboard mode.\n");
1368 			return EIO;
1369 		}
1370 	}
1371 
1372 #if defined(__sparc64__)
1373 	if (send_kbd_command_and_data(
1374 		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1375 		printf("atkbd: can't set translation.\n");
1376 	}
1377 	c |= KBD_TRANSLATION;
1378 #endif
1379 
1380 	/*
1381 	 * Some keyboards require a SETLEDS command to be sent after
1382 	 * the reset command before they will send keystrokes to us
1383 	 */
1384 	if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
1385 	    send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
1386 		printf("atkbd: setleds failed\n");
1387 	}
1388 	if (!ALLOW_DISABLE_KBD(kbdc))
1389 	    send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1390 
1391 	/* enable the keyboard port and intr. */
1392 	if (!set_controller_command_byte(kbdc,
1393 		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1394 		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1395 		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1396 		/*
1397 		 * CONTROLLER ERROR
1398 		 * This is serious; we are left with the disabled
1399 		 * keyboard intr.
1400 		 */
1401 		set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1402 		    ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1403 			KBD_OVERRIDE_KBD_LOCK), c);
1404 		kbdc_lock(kbdc, FALSE);
1405 		printf("atkbd: unable to enable the keyboard port and intr.\n");
1406 		return EIO;
1407 	}
1408 
1409 	kbdc_lock(kbdc, FALSE);
1410 	return 0;
1411 }
1412 
1413 static int
1414 write_kbd(KBDC kbdc, int command, int data)
1415 {
1416 	int s;
1417 
1418 	/* prevent the timeout routine from polling the keyboard */
1419 	if (!kbdc_lock(kbdc, TRUE))
1420 		return EBUSY;
1421 
1422 	/* disable the keyboard and mouse interrupt */
1423 	s = spltty();
1424 #if 0
1425 	c = get_controller_command_byte(kbdc);
1426 	if ((c == -1)
1427 	    || !set_controller_command_byte(kbdc,
1428 		kbdc_get_device_mask(kbdc),
1429 		KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1430 		| KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1431 		/* CONTROLLER ERROR */
1432 		kbdc_lock(kbdc, FALSE);
1433 		splx(s);
1434 		return EIO;
1435 	}
1436 	/*
1437 	 * Now that the keyboard controller is told not to generate
1438 	 * the keyboard and mouse interrupts, call `splx()' to allow
1439 	 * the other tty interrupts. The clock interrupt may also occur,
1440 	 * but the timeout routine (`scrn_timer()') will be blocked
1441 	 * by the lock flag set via `kbdc_lock()'
1442 	 */
1443 	splx(s);
1444 #endif
1445 	if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1446 		send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1447 #if 0
1448 	/* restore the interrupts */
1449 	if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1450 	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1451 		/* CONTROLLER ERROR */
1452 	}
1453 #else
1454 	splx(s);
1455 #endif
1456 	kbdc_lock(kbdc, FALSE);
1457 
1458 	return 0;
1459 }
1460 
1461 static int
1462 get_kbd_id(KBDC kbdc)
1463 {
1464 	int id1, id2;
1465 
1466 	empty_both_buffers(kbdc, 10);
1467 	id1 = id2 = -1;
1468 	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1469 		return -1;
1470 
1471 	DELAY(10000); 	/* 10 msec delay */
1472 	id1 = read_kbd_data(kbdc);
1473 	if (id1 != -1)
1474 		id2 = read_kbd_data(kbdc);
1475 
1476 	if ((id1 == -1) || (id2 == -1)) {
1477 		empty_both_buffers(kbdc, 10);
1478 		test_controller(kbdc);
1479 		test_kbd_port(kbdc);
1480 		return -1;
1481 	}
1482 	return ((id2 << 8) | id1);
1483 }
1484 
1485 static int delays[] = { 250, 500, 750, 1000 };
1486 static int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
1487 			68,  76,  84,  92, 100, 110, 118, 126,
1488 		       136, 152, 168, 184, 200, 220, 236, 252,
1489 		       272, 304, 336, 368, 400, 440, 472, 504 };
1490 
1491 static int
1492 typematic_delay(int i)
1493 {
1494 	return delays[(i >> 5) & 3];
1495 }
1496 
1497 static int
1498 typematic_rate(int i)
1499 {
1500 	return rates[i & 0x1f];
1501 }
1502 
1503 static int
1504 typematic(int delay, int rate)
1505 {
1506 	int value;
1507 	int i;
1508 
1509 	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1510 		if (delay >= delays[i])
1511 			break;
1512 	}
1513 	value = i << 5;
1514 	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1515 		if (rate >= rates[i])
1516 			break;
1517 	}
1518 	value |= i;
1519 	return value;
1520 }
1521