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