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