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