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