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