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