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