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