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