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