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