xref: /freebsd/sys/dev/usb/input/ukbd.c (revision ee6eac62f7525e948d3b693e4eef182f84625852)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 
5 /*-
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 /*
37  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38  */
39 
40 #include "opt_compat.h"
41 #include "opt_kbd.h"
42 #include "opt_ukbd.h"
43 
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/module.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sx.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
61 #include <sys/priv.h>
62 #include <sys/kdb.h>
63 
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbhid.h>
68 
69 #define	USB_DEBUG_VAR ukbd_debug
70 #include <dev/usb/usb_debug.h>
71 
72 #include <dev/usb/quirk/usb_quirk.h>
73 
74 #include <sys/ioccom.h>
75 #include <sys/filio.h>
76 #include <sys/tty.h>
77 #include <sys/kbio.h>
78 
79 #include <dev/kbd/kbdreg.h>
80 
81 /* the initial key map, accent map and fkey strings */
82 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
83 #define	KBD_DFLT_KEYMAP
84 #include "ukbdmap.h"
85 #endif
86 
87 /* the following file must be included after "ukbdmap.h" */
88 #include <dev/kbd/kbdtables.h>
89 
90 #ifdef USB_DEBUG
91 static int ukbd_debug = 0;
92 static int ukbd_no_leds = 0;
93 
94 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
95 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
96     &ukbd_debug, 0, "Debug level");
97 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RW,
98     &ukbd_no_leds, 0, "Disables setting of keyboard leds");
99 
100 TUNABLE_INT("hw.usb.ukbd.debug", &ukbd_debug);
101 TUNABLE_INT("hw.usb.ukbd.no_leds", &ukbd_no_leds);
102 #endif
103 
104 #define	UKBD_EMULATE_ATSCANCODE	       1
105 #define	UKBD_DRIVER_NAME          "ukbd"
106 #define	UKBD_NMOD                     8	/* units */
107 #define	UKBD_NKEYCODE                 6	/* units */
108 #define	UKBD_IN_BUF_SIZE  (2*(UKBD_NMOD + (2*UKBD_NKEYCODE)))	/* bytes */
109 #define	UKBD_IN_BUF_FULL  (UKBD_IN_BUF_SIZE / 2)	/* bytes */
110 #define	UKBD_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
111 
112 struct ukbd_data {
113 	uint8_t	modifiers;
114 #define	MOD_CONTROL_L	0x01
115 #define	MOD_CONTROL_R	0x10
116 #define	MOD_SHIFT_L	0x02
117 #define	MOD_SHIFT_R	0x20
118 #define	MOD_ALT_L	0x04
119 #define	MOD_ALT_R	0x40
120 #define	MOD_WIN_L	0x08
121 #define	MOD_WIN_R	0x80
122 	uint8_t	reserved;
123 	uint8_t	keycode[UKBD_NKEYCODE];
124 	uint8_t exten[8];
125 };
126 
127 enum {
128 	UKBD_INTR_DT,
129 	UKBD_CTRL_LED,
130 	UKBD_N_TRANSFER,
131 };
132 
133 struct ukbd_softc {
134 	keyboard_t sc_kbd;
135 	keymap_t sc_keymap;
136 	accentmap_t sc_accmap;
137 	fkeytab_t sc_fkeymap[UKBD_NFKEY];
138 	struct hid_location sc_loc_apple_eject;
139 	struct hid_location sc_loc_apple_fn;
140 	struct usb_callout sc_callout;
141 	struct ukbd_data sc_ndata;
142 	struct ukbd_data sc_odata;
143 
144 	struct thread *sc_poll_thread;
145 	struct usb_device *sc_udev;
146 	struct usb_interface *sc_iface;
147 	struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
148 
149 	uint32_t sc_ntime[UKBD_NKEYCODE];
150 	uint32_t sc_otime[UKBD_NKEYCODE];
151 	uint32_t sc_input[UKBD_IN_BUF_SIZE];	/* input buffer */
152 	uint32_t sc_time_ms;
153 	uint32_t sc_composed_char;	/* composed char code, if non-zero */
154 #ifdef UKBD_EMULATE_ATSCANCODE
155 	uint32_t sc_buffered_char[2];
156 #endif
157 	uint32_t sc_flags;		/* flags */
158 #define	UKBD_FLAG_COMPOSE	0x0001
159 #define	UKBD_FLAG_POLLING	0x0002
160 #define	UKBD_FLAG_SET_LEDS	0x0004
161 #define	UKBD_FLAG_ATTACHED	0x0010
162 #define	UKBD_FLAG_GONE		0x0020
163 #define	UKBD_FLAG_APPLE_EJECT	0x0040
164 #define	UKBD_FLAG_APPLE_FN	0x0080
165 #define	UKBD_FLAG_APPLE_SWAP	0x0100
166 #define	UKBD_FLAG_TIMER_RUNNING	0x0200
167 
168 	int	sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
169 	int	sc_state;		/* shift/lock key state */
170 	int	sc_accents;		/* accent key index (> 0) */
171 	int	sc_poll_tick_last;
172 
173 	uint16_t sc_inputs;
174 	uint16_t sc_inputhead;
175 	uint16_t sc_inputtail;
176 
177 	uint8_t	sc_leds;		/* store for async led requests */
178 	uint8_t	sc_iface_index;
179 	uint8_t	sc_iface_no;
180 	uint8_t sc_kbd_id;
181 	uint8_t sc_led_id;
182 	uint8_t sc_poll_detected;
183 };
184 
185 #define	KEY_ERROR	  0x01
186 
187 #define	KEY_PRESS	  0
188 #define	KEY_RELEASE	  0x400
189 #define	KEY_INDEX(c)	  ((c) & 0xFF)
190 
191 #define	SCAN_PRESS	  0
192 #define	SCAN_RELEASE	  0x80
193 #define	SCAN_PREFIX_E0	  0x100
194 #define	SCAN_PREFIX_E1	  0x200
195 #define	SCAN_PREFIX_CTL	  0x400
196 #define	SCAN_PREFIX_SHIFT 0x800
197 #define	SCAN_PREFIX	(SCAN_PREFIX_E0  | SCAN_PREFIX_E1 | \
198 			 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
199 #define	SCAN_CHAR(c)	((c) & 0x7f)
200 
201 struct ukbd_mods {
202 	uint32_t mask, key;
203 };
204 
205 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
206 	{MOD_CONTROL_L, 0xe0},
207 	{MOD_CONTROL_R, 0xe4},
208 	{MOD_SHIFT_L, 0xe1},
209 	{MOD_SHIFT_R, 0xe5},
210 	{MOD_ALT_L, 0xe2},
211 	{MOD_ALT_R, 0xe6},
212 	{MOD_WIN_L, 0xe3},
213 	{MOD_WIN_R, 0xe7},
214 };
215 
216 #define	NN 0				/* no translation */
217 /*
218  * Translate USB keycodes to AT keyboard scancodes.
219  */
220 /*
221  * FIXME: Mac USB keyboard generates:
222  * 0x53: keypad NumLock/Clear
223  * 0x66: Power
224  * 0x67: keypad =
225  * 0x68: F13
226  * 0x69: F14
227  * 0x6a: F15
228  */
229 static const uint8_t ukbd_trtab[256] = {
230 	0, 0, 0, 0, 30, 48, 46, 32,	/* 00 - 07 */
231 	18, 33, 34, 35, 23, 36, 37, 38,	/* 08 - 0F */
232 	50, 49, 24, 25, 16, 19, 31, 20,	/* 10 - 17 */
233 	22, 47, 17, 45, 21, 44, 2, 3,	/* 18 - 1F */
234 	4, 5, 6, 7, 8, 9, 10, 11,	/* 20 - 27 */
235 	28, 1, 14, 15, 57, 12, 13, 26,	/* 28 - 2F */
236 	27, 43, 43, 39, 40, 41, 51, 52,	/* 30 - 37 */
237 	53, 58, 59, 60, 61, 62, 63, 64,	/* 38 - 3F */
238 	65, 66, 67, 68, 87, 88, 92, 70,	/* 40 - 47 */
239 	104, 102, 94, 96, 103, 99, 101, 98,	/* 48 - 4F */
240 	97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
241 	89, 79, 80, 81, 75, 76, 77, 71,	/* 58 - 5F */
242 	72, 73, 82, 83, 86, 107, 122, NN,	/* 60 - 67 */
243 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 68 - 6F */
244 	NN, NN, NN, NN, 115, 108, 111, 113,	/* 70 - 77 */
245 	109, 110, 112, 118, 114, 116, 117, 119,	/* 78 - 7F */
246 	121, 120, NN, NN, NN, NN, NN, 123,	/* 80 - 87 */
247 	124, 125, 126, 127, 128, NN, NN, NN,	/* 88 - 8F */
248 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 90 - 97 */
249 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 98 - 9F */
250 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A0 - A7 */
251 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A8 - AF */
252 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B0 - B7 */
253 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B8 - BF */
254 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C0 - C7 */
255 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C8 - CF */
256 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D0 - D7 */
257 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D8 - DF */
258 	29, 42, 56, 105, 90, 54, 93, 106,	/* E0 - E7 */
259 	NN, NN, NN, NN, NN, NN, NN, NN,	/* E8 - EF */
260 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F0 - F7 */
261 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F8 - FF */
262 };
263 
264 /* prototypes */
265 static void	ukbd_timeout(void *);
266 static void	ukbd_set_leds(struct ukbd_softc *, uint8_t);
267 static int	ukbd_set_typematic(keyboard_t *, int);
268 #ifdef UKBD_EMULATE_ATSCANCODE
269 static int	ukbd_key2scan(struct ukbd_softc *, int, int, int);
270 #endif
271 static uint32_t	ukbd_read_char(keyboard_t *, int);
272 static void	ukbd_clear_state(keyboard_t *);
273 static int	ukbd_ioctl(keyboard_t *, u_long, caddr_t);
274 static int	ukbd_enable(keyboard_t *);
275 static int	ukbd_disable(keyboard_t *);
276 static void	ukbd_interrupt(struct ukbd_softc *);
277 static int	ukbd_is_polling(struct ukbd_softc *);
278 static int	ukbd_polls_other_thread(struct ukbd_softc *);
279 static void	ukbd_event_keyinput(struct ukbd_softc *);
280 
281 static device_probe_t ukbd_probe;
282 static device_attach_t ukbd_attach;
283 static device_detach_t ukbd_detach;
284 static device_resume_t ukbd_resume;
285 
286 static uint8_t
287 ukbd_any_key_pressed(struct ukbd_softc *sc)
288 {
289 	uint8_t i;
290 	uint8_t j;
291 
292 	for (j = i = 0; i < UKBD_NKEYCODE; i++)
293 		j |= sc->sc_odata.keycode[i];
294 
295 	return (j ? 1 : 0);
296 }
297 
298 static void
299 ukbd_start_timer(struct ukbd_softc *sc)
300 {
301 	sc->sc_flags |= UKBD_FLAG_TIMER_RUNNING;
302 	usb_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
303 }
304 
305 static void
306 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
307 {
308 	mtx_assert(&Giant, MA_OWNED);
309 
310 	DPRINTF("0x%02x (%d) %s\n", key, key,
311 	    (key & KEY_RELEASE) ? "released" : "pressed");
312 
313 	if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
314 		sc->sc_input[sc->sc_inputtail] = key;
315 		++(sc->sc_inputs);
316 		++(sc->sc_inputtail);
317 		if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
318 			sc->sc_inputtail = 0;
319 		}
320 	} else {
321 		DPRINTF("input buffer is full\n");
322 	}
323 }
324 
325 static void
326 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
327 {
328 	DPRINTFN(2, "polling\n");
329 
330 	/* update stats about last polling event */
331 	sc->sc_poll_tick_last = ticks;
332 	sc->sc_poll_detected = 1;
333 
334 	if (kdb_active == 0) {
335 		while (sc->sc_inputs == 0) {
336 			/* make sure the USB code gets a chance to run */
337 			pause("UKBD", 1);
338 
339 			/* check if we should wait */
340 			if (!wait)
341 				break;
342 		}
343 		return;		/* Only poll if KDB is active */
344 	}
345 
346 	while (sc->sc_inputs == 0) {
347 
348 		usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
349 
350 		/* Delay-optimised support for repetition of keys */
351 
352 		if (ukbd_any_key_pressed(sc)) {
353 			/* a key is pressed - need timekeeping */
354 			DELAY(1000);
355 
356 			/* 1 millisecond has passed */
357 			sc->sc_time_ms += 1;
358 		}
359 
360 		ukbd_interrupt(sc);
361 
362 		if (!wait)
363 			break;
364 	}
365 }
366 
367 static int32_t
368 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
369 {
370 	int32_t c;
371 
372 	mtx_assert(&Giant, MA_OWNED);
373 
374 	if (sc->sc_inputs == 0) {
375 		/* start transfer, if not already started */
376 		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
377 	}
378 
379 	if (ukbd_polls_other_thread(sc))
380 		return (-1);
381 
382 	if (sc->sc_flags & UKBD_FLAG_POLLING)
383 		ukbd_do_poll(sc, wait);
384 
385 	if (sc->sc_inputs == 0) {
386 		c = -1;
387 	} else {
388 		c = sc->sc_input[sc->sc_inputhead];
389 		--(sc->sc_inputs);
390 		++(sc->sc_inputhead);
391 		if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
392 			sc->sc_inputhead = 0;
393 		}
394 	}
395 	return (c);
396 }
397 
398 static void
399 ukbd_interrupt(struct ukbd_softc *sc)
400 {
401 	uint32_t n_mod;
402 	uint32_t o_mod;
403 	uint32_t now = sc->sc_time_ms;
404 	uint32_t dtime;
405 	uint8_t key;
406 	uint8_t i;
407 	uint8_t j;
408 
409 	if (sc->sc_ndata.keycode[0] == KEY_ERROR)
410 		return;
411 
412 	n_mod = sc->sc_ndata.modifiers;
413 	o_mod = sc->sc_odata.modifiers;
414 	if (n_mod != o_mod) {
415 		for (i = 0; i < UKBD_NMOD; i++) {
416 			if ((n_mod & ukbd_mods[i].mask) !=
417 			    (o_mod & ukbd_mods[i].mask)) {
418 				ukbd_put_key(sc, ukbd_mods[i].key |
419 				    ((n_mod & ukbd_mods[i].mask) ?
420 				    KEY_PRESS : KEY_RELEASE));
421 			}
422 		}
423 	}
424 	/* Check for released keys. */
425 	for (i = 0; i < UKBD_NKEYCODE; i++) {
426 		key = sc->sc_odata.keycode[i];
427 		if (key == 0) {
428 			continue;
429 		}
430 		for (j = 0; j < UKBD_NKEYCODE; j++) {
431 			if (sc->sc_ndata.keycode[j] == 0) {
432 				continue;
433 			}
434 			if (key == sc->sc_ndata.keycode[j]) {
435 				goto rfound;
436 			}
437 		}
438 		ukbd_put_key(sc, key | KEY_RELEASE);
439 rfound:	;
440 	}
441 
442 	/* Check for pressed keys. */
443 	for (i = 0; i < UKBD_NKEYCODE; i++) {
444 		key = sc->sc_ndata.keycode[i];
445 		if (key == 0) {
446 			continue;
447 		}
448 		sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
449 		for (j = 0; j < UKBD_NKEYCODE; j++) {
450 			if (sc->sc_odata.keycode[j] == 0) {
451 				continue;
452 			}
453 			if (key == sc->sc_odata.keycode[j]) {
454 
455 				/* key is still pressed */
456 
457 				sc->sc_ntime[i] = sc->sc_otime[j];
458 				dtime = (sc->sc_otime[j] - now);
459 
460 				if (!(dtime & 0x80000000)) {
461 					/* time has not elapsed */
462 					goto pfound;
463 				}
464 				sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
465 				break;
466 			}
467 		}
468 		ukbd_put_key(sc, key | KEY_PRESS);
469 
470 		/*
471                  * If any other key is presently down, force its repeat to be
472                  * well in the future (100s).  This makes the last key to be
473                  * pressed do the autorepeat.
474                  */
475 		for (j = 0; j != UKBD_NKEYCODE; j++) {
476 			if (j != i)
477 				sc->sc_ntime[j] = now + (100 * 1000);
478 		}
479 pfound:	;
480 	}
481 
482 	sc->sc_odata = sc->sc_ndata;
483 
484 	memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
485 
486 	ukbd_event_keyinput(sc);
487 }
488 
489 static void
490 ukbd_event_keyinput(struct ukbd_softc *sc)
491 {
492 	int c;
493 
494 	if (ukbd_is_polling(sc))
495 		return;
496 
497 	if (sc->sc_inputs == 0)
498 		return;
499 
500 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
501 	    KBD_IS_BUSY(&sc->sc_kbd)) {
502 		/* let the callback function process the input */
503 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
504 		    sc->sc_kbd.kb_callback.kc_arg);
505 	} else {
506 		/* read and discard the input, no one is waiting for it */
507 		do {
508 			c = ukbd_read_char(&sc->sc_kbd, 0);
509 		} while (c != NOKEY);
510 	}
511 }
512 
513 static void
514 ukbd_timeout(void *arg)
515 {
516 	struct ukbd_softc *sc = arg;
517 
518 	mtx_assert(&Giant, MA_OWNED);
519 
520 	sc->sc_time_ms += 25;	/* milliseconds */
521 
522 	ukbd_interrupt(sc);
523 
524 	/* Make sure any leftover key events gets read out */
525 	ukbd_event_keyinput(sc);
526 
527 	if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
528 		ukbd_start_timer(sc);
529 	} else {
530 		sc->sc_flags &= ~UKBD_FLAG_TIMER_RUNNING;
531 	}
532 }
533 
534 static uint8_t
535 ukbd_apple_fn(uint8_t keycode) {
536 	switch (keycode) {
537 	case 0x28: return 0x49; /* RETURN -> INSERT */
538 	case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
539 	case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
540 	case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
541 	case 0x52: return 0x4b; /* UP ARROW -> PGUP */
542 	case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
543 	default: return keycode;
544 	}
545 }
546 
547 static uint8_t
548 ukbd_apple_swap(uint8_t keycode) {
549 	switch (keycode) {
550 	case 0x35: return 0x64;
551 	case 0x64: return 0x35;
552 	default: return keycode;
553 	}
554 }
555 
556 static void
557 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
558 {
559 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
560 	struct usb_page_cache *pc;
561 	uint8_t i;
562 	uint8_t offset;
563 	uint8_t id;
564 	uint8_t apple_fn;
565 	uint8_t apple_eject;
566 	int len;
567 
568 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
569 	pc = usbd_xfer_get_frame(xfer, 0);
570 
571 	switch (USB_GET_STATE(xfer)) {
572 	case USB_ST_TRANSFERRED:
573 		DPRINTF("actlen=%d bytes\n", len);
574 
575 		if (len == 0) {
576 			DPRINTF("zero length data\n");
577 			goto tr_setup;
578 		}
579 
580 		if (sc->sc_kbd_id != 0) {
581 			/* check and remove HID ID byte */
582 			usbd_copy_out(pc, 0, &id, 1);
583 			if (id != sc->sc_kbd_id) {
584 				DPRINTF("wrong HID ID\n");
585 				goto tr_setup;
586 			}
587 			offset = 1;
588 			len--;
589 		} else {
590 			offset = 0;
591 		}
592 
593 		if (len > sizeof(sc->sc_ndata)) {
594 			len = sizeof(sc->sc_ndata);
595 		}
596 
597 		if (len) {
598 			memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
599 			usbd_copy_out(pc, offset, &sc->sc_ndata, len);
600 
601 			if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
602 			    hid_get_data((uint8_t *)&sc->sc_ndata,
603 				len, &sc->sc_loc_apple_eject))
604 				apple_eject = 1;
605 			else
606 				apple_eject = 0;
607 
608 			if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
609 			    hid_get_data((uint8_t *)&sc->sc_ndata,
610 				len, &sc->sc_loc_apple_fn))
611 				apple_fn = 1;
612 			else
613 				apple_fn = 0;
614 #ifdef USB_DEBUG
615 			DPRINTF("apple_eject=%u apple_fn=%u\n",
616 			    apple_eject, apple_fn);
617 
618 			if (sc->sc_ndata.modifiers) {
619 				DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
620 			}
621 			for (i = 0; i < UKBD_NKEYCODE; i++) {
622 				if (sc->sc_ndata.keycode[i]) {
623 					DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
624 				}
625 			}
626 #endif					/* USB_DEBUG */
627 
628 			if (apple_fn) {
629 				for (i = 0; i < UKBD_NKEYCODE; i++) {
630 					sc->sc_ndata.keycode[i] =
631 					    ukbd_apple_fn(sc->sc_ndata.keycode[i]);
632 				}
633 			}
634 
635 			if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
636 				for (i = 0; i < UKBD_NKEYCODE; i++) {
637 					sc->sc_ndata.keycode[i] =
638 					    ukbd_apple_swap(sc->sc_ndata.keycode[i]);
639 				}
640 			}
641 
642 			ukbd_interrupt(sc);
643 
644 			if (!(sc->sc_flags & UKBD_FLAG_TIMER_RUNNING)) {
645 				if (ukbd_any_key_pressed(sc)) {
646 					ukbd_start_timer(sc);
647 				}
648 			}
649 		}
650 	case USB_ST_SETUP:
651 tr_setup:
652 		if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
653 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
654 			usbd_transfer_submit(xfer);
655 		} else {
656 			DPRINTF("input queue is full!\n");
657 		}
658 		break;
659 
660 	default:			/* Error */
661 		DPRINTF("error=%s\n", usbd_errstr(error));
662 
663 		if (error != USB_ERR_CANCELLED) {
664 			/* try to clear stall first */
665 			usbd_xfer_set_stall(xfer);
666 			goto tr_setup;
667 		}
668 		break;
669 	}
670 }
671 
672 static void
673 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
674 {
675 	struct usb_device_request req;
676 	struct usb_page_cache *pc;
677 	uint8_t buf[2];
678 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
679 
680 #ifdef USB_DEBUG
681 	if (ukbd_no_leds)
682 		return;
683 #endif
684 
685 	switch (USB_GET_STATE(xfer)) {
686 	case USB_ST_TRANSFERRED:
687 	case USB_ST_SETUP:
688 		if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
689 			sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
690 
691 			req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
692 			req.bRequest = UR_SET_REPORT;
693 			USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
694 			req.wIndex[0] = sc->sc_iface_no;
695 			req.wIndex[1] = 0;
696 			req.wLength[1] = 0;
697 
698 			/* check if we need to prefix an ID byte */
699 			if (sc->sc_led_id != 0) {
700 				req.wLength[0] = 2;
701 				buf[0] = sc->sc_led_id;
702 				buf[1] = sc->sc_leds;
703 			} else {
704 				req.wLength[0] = 1;
705 				buf[0] = sc->sc_leds;
706 				buf[1] = 0;
707 			}
708 
709 			pc = usbd_xfer_get_frame(xfer, 0);
710 			usbd_copy_in(pc, 0, &req, sizeof(req));
711 			pc = usbd_xfer_get_frame(xfer, 1);
712 			usbd_copy_in(pc, 0, buf, sizeof(buf));
713 
714 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
715 			usbd_xfer_set_frame_len(xfer, 1, req.wLength[0]);
716 			usbd_xfer_set_frames(xfer, 2);
717 			usbd_transfer_submit(xfer);
718 		}
719 		break;
720 
721 	default:			/* Error */
722 		DPRINTFN(1, "error=%s\n", usbd_errstr(error));
723 		break;
724 	}
725 }
726 
727 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
728 
729 	[UKBD_INTR_DT] = {
730 		.type = UE_INTERRUPT,
731 		.endpoint = UE_ADDR_ANY,
732 		.direction = UE_DIR_IN,
733 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
734 		.bufsize = 0,	/* use wMaxPacketSize */
735 		.callback = &ukbd_intr_callback,
736 	},
737 
738 	[UKBD_CTRL_LED] = {
739 		.type = UE_CONTROL,
740 		.endpoint = 0x00,	/* Control pipe */
741 		.direction = UE_DIR_ANY,
742 		.bufsize = sizeof(struct usb_device_request) + 8,
743 		.callback = &ukbd_set_leds_callback,
744 		.timeout = 1000,	/* 1 second */
745 	},
746 };
747 
748 /* A match on these entries will load ukbd */
749 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
750 	{USB_IFACE_CLASS(UICLASS_HID),
751 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
752 	 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
753 };
754 
755 static int
756 ukbd_probe(device_t dev)
757 {
758 	keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
759 	struct usb_attach_arg *uaa = device_get_ivars(dev);
760 	void *d_ptr;
761 	int error;
762 	uint16_t d_len;
763 
764 	DPRINTFN(11, "\n");
765 
766 	if (sw == NULL) {
767 		return (ENXIO);
768 	}
769 	if (uaa->usb_mode != USB_MODE_HOST) {
770 		return (ENXIO);
771 	}
772 
773 	if (uaa->info.bInterfaceClass != UICLASS_HID)
774 		return (ENXIO);
775 
776 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
777 	    (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
778 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
779 			return (ENXIO);
780 		else
781 			return (BUS_PROBE_DEFAULT);
782 	}
783 
784 	error = usbd_req_get_hid_desc(uaa->device, NULL,
785 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
786 
787 	if (error)
788 		return (ENXIO);
789 
790 	/*
791 	 * NOTE: we currently don't support USB mouse and USB keyboard
792 	 * on the same USB endpoint.
793 	 */
794 	if (hid_is_collection(d_ptr, d_len,
795 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
796 		/* most likely a mouse */
797 		error = ENXIO;
798 	} else if (hid_is_collection(d_ptr, d_len,
799 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
800 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
801 			error = ENXIO;
802 		else
803 			error = BUS_PROBE_DEFAULT;
804 	} else
805 		error = ENXIO;
806 
807 	free(d_ptr, M_TEMP);
808 	return (error);
809 }
810 
811 static int
812 ukbd_attach(device_t dev)
813 {
814 	struct ukbd_softc *sc = device_get_softc(dev);
815 	struct usb_attach_arg *uaa = device_get_ivars(dev);
816 	int32_t unit = device_get_unit(dev);
817 	keyboard_t *kbd = &sc->sc_kbd;
818 	void *hid_ptr = NULL;
819 	usb_error_t err;
820 	uint32_t flags;
821 	uint16_t n;
822 	uint16_t hid_len;
823 
824 	kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
825 
826 	kbd->kb_data = (void *)sc;
827 
828 	device_set_usb_desc(dev);
829 
830 	sc->sc_udev = uaa->device;
831 	sc->sc_iface = uaa->iface;
832 	sc->sc_iface_index = uaa->info.bIfaceIndex;
833 	sc->sc_iface_no = uaa->info.bIfaceNum;
834 	sc->sc_mode = K_XLATE;
835 
836 	usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
837 
838 	err = usbd_transfer_setup(uaa->device,
839 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
840 	    UKBD_N_TRANSFER, sc, &Giant);
841 
842 	if (err) {
843 		DPRINTF("error=%s\n", usbd_errstr(err));
844 		goto detach;
845 	}
846 	/* setup default keyboard maps */
847 
848 	sc->sc_keymap = key_map;
849 	sc->sc_accmap = accent_map;
850 	for (n = 0; n < UKBD_NFKEY; n++) {
851 		sc->sc_fkeymap[n] = fkey_tab[n];
852 	}
853 
854 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
855 	    sc->sc_fkeymap, UKBD_NFKEY);
856 
857 	KBD_FOUND_DEVICE(kbd);
858 
859 	ukbd_clear_state(kbd);
860 
861 	/*
862 	 * FIXME: set the initial value for lock keys in "sc_state"
863 	 * according to the BIOS data?
864 	 */
865 	KBD_PROBE_DONE(kbd);
866 
867 	/*
868 	 * Set boot protocol if we need the quirk.
869 	 */
870 	if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) {
871 		err = usbd_req_set_protocol(sc->sc_udev, NULL,
872 			sc->sc_iface_index, 0);
873 		if (err != USB_ERR_NORMAL_COMPLETION) {
874 			DPRINTF("set protocol error=%s\n", usbd_errstr(err));
875 			goto detach;
876 		}
877 	}
878 
879 	/* figure out if there is an ID byte in the data */
880 	err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
881 	    &hid_len, M_TEMP, uaa->info.bIfaceIndex);
882 	if (err == 0) {
883 		uint8_t apple_keys = 0;
884 		uint8_t temp_id;
885 
886 		/* investigate if this is an Apple Keyboard */
887 		if (hid_locate(hid_ptr, hid_len,
888 		    HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
889 		    hid_input, 0, &sc->sc_loc_apple_eject, &flags,
890 		    &temp_id)) {
891 			if (flags & HIO_VARIABLE)
892 				sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
893 				    UKBD_FLAG_APPLE_SWAP;
894 			DPRINTFN(1, "Found Apple eject-key\n");
895 			apple_keys = 1;
896 			sc->sc_kbd_id = temp_id;
897 		}
898 		if (hid_locate(hid_ptr, hid_len,
899 		    HID_USAGE2(0xFFFF, 0x0003),
900 		    hid_input, 0, &sc->sc_loc_apple_fn, &flags,
901 		    &temp_id)) {
902 			if (flags & HIO_VARIABLE)
903 				sc->sc_flags |= UKBD_FLAG_APPLE_FN;
904 			DPRINTFN(1, "Found Apple FN-key\n");
905 			apple_keys = 1;
906 			sc->sc_kbd_id = temp_id;
907 		}
908 		if (apple_keys == 0) {
909 			/*
910 			 * Assume the first HID ID contains the
911 			 * keyboard data
912 			 */
913 			hid_report_size(hid_ptr, hid_len,
914 			    hid_input, &sc->sc_kbd_id);
915 		}
916 
917 		/* investigate if we need an ID-byte for the leds */
918 		hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
919 
920 		free(hid_ptr, M_TEMP);
921 	}
922 
923 	/* ignore if SETIDLE fails, hence it is not crucial */
924 	err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
925 
926 	mtx_lock(&Giant);
927 
928 	ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
929 
930 	KBD_INIT_DONE(kbd);
931 
932 	mtx_unlock(&Giant);
933 
934 	if (kbd_register(kbd) < 0) {
935 		goto detach;
936 	}
937 	KBD_CONFIG_DONE(kbd);
938 
939 	ukbd_enable(kbd);
940 
941 #ifdef KBD_INSTALL_CDEV
942 	if (kbd_attach(kbd)) {
943 		goto detach;
944 	}
945 #endif
946 	sc->sc_flags |= UKBD_FLAG_ATTACHED;
947 
948 	if (bootverbose) {
949 		genkbd_diag(kbd, bootverbose);
950 	}
951 	/* lock keyboard mutex */
952 
953 	mtx_lock(&Giant);
954 
955 	/* start the keyboard */
956 
957 	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
958 
959 	mtx_unlock(&Giant);
960 	return (0);			/* success */
961 
962 detach:
963 	ukbd_detach(dev);
964 	return (ENXIO);			/* error */
965 }
966 
967 static int
968 ukbd_detach(device_t dev)
969 {
970 	struct ukbd_softc *sc = device_get_softc(dev);
971 	int error;
972 
973 	DPRINTF("\n");
974 
975 	mtx_lock(&Giant);
976 
977 	sc->sc_flags |= UKBD_FLAG_GONE;
978 
979 	usb_callout_stop(&sc->sc_callout);
980 
981 	ukbd_disable(&sc->sc_kbd);
982 
983 #ifdef KBD_INSTALL_CDEV
984 	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
985 		error = kbd_detach(&sc->sc_kbd);
986 		if (error) {
987 			/* usb attach cannot return an error */
988 			device_printf(dev, "WARNING: kbd_detach() "
989 			    "returned non-zero! (ignored)\n");
990 		}
991 	}
992 #endif
993 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
994 		error = kbd_unregister(&sc->sc_kbd);
995 		if (error) {
996 			/* usb attach cannot return an error */
997 			device_printf(dev, "WARNING: kbd_unregister() "
998 			    "returned non-zero! (ignored)\n");
999 		}
1000 	}
1001 	sc->sc_kbd.kb_flags = 0;
1002 
1003 	mtx_unlock(&Giant);
1004 
1005 	usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1006 
1007 	usb_callout_drain(&sc->sc_callout);
1008 
1009 	DPRINTF("%s: disconnected\n",
1010 	    device_get_nameunit(dev));
1011 
1012 	return (0);
1013 }
1014 
1015 static int
1016 ukbd_resume(device_t dev)
1017 {
1018 	struct ukbd_softc *sc = device_get_softc(dev);
1019 
1020 	mtx_lock(&Giant);
1021 
1022 	ukbd_clear_state(&sc->sc_kbd);
1023 
1024 	mtx_unlock(&Giant);
1025 
1026 	return (0);
1027 }
1028 
1029 /* early keyboard probe, not supported */
1030 static int
1031 ukbd_configure(int flags)
1032 {
1033 	return (0);
1034 }
1035 
1036 /* detect a keyboard, not used */
1037 static int
1038 ukbd__probe(int unit, void *arg, int flags)
1039 {
1040 	return (ENXIO);
1041 }
1042 
1043 /* reset and initialize the device, not used */
1044 static int
1045 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1046 {
1047 	return (ENXIO);
1048 }
1049 
1050 /* test the interface to the device, not used */
1051 static int
1052 ukbd_test_if(keyboard_t *kbd)
1053 {
1054 	return (0);
1055 }
1056 
1057 /* finish using this keyboard, not used */
1058 static int
1059 ukbd_term(keyboard_t *kbd)
1060 {
1061 	return (ENXIO);
1062 }
1063 
1064 /* keyboard interrupt routine, not used */
1065 static int
1066 ukbd_intr(keyboard_t *kbd, void *arg)
1067 {
1068 	return (0);
1069 }
1070 
1071 /* lock the access to the keyboard, not used */
1072 static int
1073 ukbd_lock(keyboard_t *kbd, int lock)
1074 {
1075 	return (1);
1076 }
1077 
1078 /*
1079  * Enable the access to the device; until this function is called,
1080  * the client cannot read from the keyboard.
1081  */
1082 static int
1083 ukbd_enable(keyboard_t *kbd)
1084 {
1085 	if (!mtx_owned(&Giant)) {
1086 		/* XXX cludge */
1087 		int retval;
1088 		mtx_lock(&Giant);
1089 		retval = ukbd_enable(kbd);
1090 		mtx_unlock(&Giant);
1091 		return (retval);
1092 	}
1093 	KBD_ACTIVATE(kbd);
1094 	return (0);
1095 }
1096 
1097 /* disallow the access to the device */
1098 static int
1099 ukbd_disable(keyboard_t *kbd)
1100 {
1101 	if (!mtx_owned(&Giant)) {
1102 		/* XXX cludge */
1103 		int retval;
1104 		mtx_lock(&Giant);
1105 		retval = ukbd_disable(kbd);
1106 		mtx_unlock(&Giant);
1107 		return (retval);
1108 	}
1109 	KBD_DEACTIVATE(kbd);
1110 	return (0);
1111 }
1112 
1113 /* check if data is waiting */
1114 static int
1115 ukbd_check(keyboard_t *kbd)
1116 {
1117 	struct ukbd_softc *sc = kbd->kb_data;
1118 
1119 	if (!KBD_IS_ACTIVE(kbd))
1120 		return (0);
1121 
1122 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1123 		if (!mtx_owned(&Giant)) {
1124 			/* XXX cludge */
1125 			int retval;
1126 			mtx_lock(&Giant);
1127 			retval = ukbd_check(kbd);
1128 			mtx_unlock(&Giant);
1129 			return (retval);
1130 		}
1131 	} else {
1132 		/* XXX the keyboard layer requires Giant */
1133 		if (!mtx_owned(&Giant))
1134 			return (0);
1135 	}
1136 
1137 	/* check if key belongs to this thread */
1138 	if (ukbd_polls_other_thread(sc))
1139 		return (0);
1140 
1141 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1142 		ukbd_do_poll(sc, 0);
1143 
1144 #ifdef UKBD_EMULATE_ATSCANCODE
1145 	if (sc->sc_buffered_char[0]) {
1146 		return (1);
1147 	}
1148 #endif
1149 	if (sc->sc_inputs > 0) {
1150 		return (1);
1151 	}
1152 	return (0);
1153 }
1154 
1155 /* check if char is waiting */
1156 static int
1157 ukbd_check_char(keyboard_t *kbd)
1158 {
1159 	struct ukbd_softc *sc = kbd->kb_data;
1160 
1161 	if (!KBD_IS_ACTIVE(kbd))
1162 		return (0);
1163 
1164 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1165 		if (!mtx_owned(&Giant)) {
1166 			/* XXX cludge */
1167 			int retval;
1168 			mtx_lock(&Giant);
1169 			retval = ukbd_check_char(kbd);
1170 			mtx_unlock(&Giant);
1171 			return (retval);
1172 		}
1173 	} else {
1174 		/* XXX the keyboard layer requires Giant */
1175 		if (!mtx_owned(&Giant))
1176 			return (0);
1177 	}
1178 
1179 	/* check if key belongs to this thread */
1180 	if (ukbd_polls_other_thread(sc))
1181 		return (0);
1182 
1183 	if ((sc->sc_composed_char > 0) &&
1184 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1185 		return (1);
1186 	}
1187 	return (ukbd_check(kbd));
1188 }
1189 
1190 
1191 /* read one byte from the keyboard if it's allowed */
1192 static int
1193 ukbd_read(keyboard_t *kbd, int wait)
1194 {
1195 	struct ukbd_softc *sc = kbd->kb_data;
1196 	int32_t usbcode;
1197 
1198 #ifdef UKBD_EMULATE_ATSCANCODE
1199 	uint32_t keycode;
1200 	uint32_t scancode;
1201 
1202 #endif
1203 	if (!KBD_IS_ACTIVE(kbd))
1204 		return (-1);
1205 
1206 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1207 		if (!mtx_owned(&Giant)) {
1208 			/* XXX cludge */
1209 			int retval;
1210 			mtx_lock(&Giant);
1211 			retval = ukbd_read(kbd, wait);
1212 			mtx_unlock(&Giant);
1213 			return (retval);
1214 		}
1215 	} else {
1216 		/* XXX the keyboard layer requires Giant */
1217 		if (!mtx_owned(&Giant))
1218 			return (-1);
1219 	}
1220 
1221 	/* check if key belongs to this thread */
1222 	if (ukbd_polls_other_thread(sc))
1223 		return (-1);
1224 
1225 #ifdef UKBD_EMULATE_ATSCANCODE
1226 	if (sc->sc_buffered_char[0]) {
1227 		scancode = sc->sc_buffered_char[0];
1228 		if (scancode & SCAN_PREFIX) {
1229 			sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1230 			return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1231 		}
1232 		sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1233 		sc->sc_buffered_char[1] = 0;
1234 		return (scancode);
1235 	}
1236 #endif					/* UKBD_EMULATE_ATSCANCODE */
1237 
1238 	/* XXX */
1239 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1240 	if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1241 		return (-1);
1242 
1243 	++(kbd->kb_count);
1244 
1245 #ifdef UKBD_EMULATE_ATSCANCODE
1246 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1247 	if (keycode == NN) {
1248 		return -1;
1249 	}
1250 	return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1251 	    (usbcode & KEY_RELEASE)));
1252 #else					/* !UKBD_EMULATE_ATSCANCODE */
1253 	return (usbcode);
1254 #endif					/* UKBD_EMULATE_ATSCANCODE */
1255 }
1256 
1257 /* read char from the keyboard */
1258 static uint32_t
1259 ukbd_read_char(keyboard_t *kbd, int wait)
1260 {
1261 	struct ukbd_softc *sc = kbd->kb_data;
1262 	uint32_t action;
1263 	uint32_t keycode;
1264 	int32_t usbcode;
1265 
1266 #ifdef UKBD_EMULATE_ATSCANCODE
1267 	uint32_t scancode;
1268 
1269 #endif
1270 
1271 	if (!KBD_IS_ACTIVE(kbd))
1272 		return (NOKEY);
1273 
1274 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1275 		if (!mtx_owned(&Giant)) {
1276 			/* XXX cludge */
1277 			int retval;
1278 			mtx_lock(&Giant);
1279 			retval = ukbd_read_char(kbd, wait);
1280 			mtx_unlock(&Giant);
1281 			return (retval);
1282 		}
1283 	} else {
1284 		/* XXX the keyboard layer requires Giant */
1285 		if (!mtx_owned(&Giant))
1286 			return (NOKEY);
1287 	}
1288 
1289 	/* check if key belongs to this thread */
1290 	if (ukbd_polls_other_thread(sc))
1291 		return (NOKEY);
1292 
1293 next_code:
1294 
1295 	/* do we have a composed char to return ? */
1296 
1297 	if ((sc->sc_composed_char > 0) &&
1298 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1299 
1300 		action = sc->sc_composed_char;
1301 		sc->sc_composed_char = 0;
1302 
1303 		if (action > 0xFF) {
1304 			goto errkey;
1305 		}
1306 		goto done;
1307 	}
1308 #ifdef UKBD_EMULATE_ATSCANCODE
1309 
1310 	/* do we have a pending raw scan code? */
1311 
1312 	if (sc->sc_mode == K_RAW) {
1313 		scancode = sc->sc_buffered_char[0];
1314 		if (scancode) {
1315 			if (scancode & SCAN_PREFIX) {
1316 				sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1317 				return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1318 			}
1319 			sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1320 			sc->sc_buffered_char[1] = 0;
1321 			return (scancode);
1322 		}
1323 	}
1324 #endif					/* UKBD_EMULATE_ATSCANCODE */
1325 
1326 	/* see if there is something in the keyboard port */
1327 	/* XXX */
1328 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1329 	if (usbcode == -1) {
1330 		return (NOKEY);
1331 	}
1332 	++kbd->kb_count;
1333 
1334 #ifdef UKBD_EMULATE_ATSCANCODE
1335 	/* USB key index -> key code -> AT scan code */
1336 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1337 	if (keycode == NN) {
1338 		return (NOKEY);
1339 	}
1340 	/* return an AT scan code for the K_RAW mode */
1341 	if (sc->sc_mode == K_RAW) {
1342 		return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1343 		    (usbcode & KEY_RELEASE)));
1344 	}
1345 #else					/* !UKBD_EMULATE_ATSCANCODE */
1346 
1347 	/* return the byte as is for the K_RAW mode */
1348 	if (sc->sc_mode == K_RAW) {
1349 		return (usbcode);
1350 	}
1351 	/* USB key index -> key code */
1352 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1353 	if (keycode == NN) {
1354 		return (NOKEY);
1355 	}
1356 #endif					/* UKBD_EMULATE_ATSCANCODE */
1357 
1358 	switch (keycode) {
1359 	case 0x38:			/* left alt (compose key) */
1360 		if (usbcode & KEY_RELEASE) {
1361 			if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1362 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1363 
1364 				if (sc->sc_composed_char > 0xFF) {
1365 					sc->sc_composed_char = 0;
1366 				}
1367 			}
1368 		} else {
1369 			if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1370 				sc->sc_flags |= UKBD_FLAG_COMPOSE;
1371 				sc->sc_composed_char = 0;
1372 			}
1373 		}
1374 		break;
1375 		/* XXX: I don't like these... */
1376 	case 0x5c:			/* print screen */
1377 		if (sc->sc_flags & ALTS) {
1378 			keycode = 0x54;	/* sysrq */
1379 		}
1380 		break;
1381 	case 0x68:			/* pause/break */
1382 		if (sc->sc_flags & CTLS) {
1383 			keycode = 0x6c;	/* break */
1384 		}
1385 		break;
1386 	}
1387 
1388 	/* return the key code in the K_CODE mode */
1389 	if (usbcode & KEY_RELEASE) {
1390 		keycode |= SCAN_RELEASE;
1391 	}
1392 	if (sc->sc_mode == K_CODE) {
1393 		return (keycode);
1394 	}
1395 	/* compose a character code */
1396 	if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1397 		switch (keycode) {
1398 			/* key pressed, process it */
1399 		case 0x47:
1400 		case 0x48:
1401 		case 0x49:		/* keypad 7,8,9 */
1402 			sc->sc_composed_char *= 10;
1403 			sc->sc_composed_char += keycode - 0x40;
1404 			goto check_composed;
1405 
1406 		case 0x4B:
1407 		case 0x4C:
1408 		case 0x4D:		/* keypad 4,5,6 */
1409 			sc->sc_composed_char *= 10;
1410 			sc->sc_composed_char += keycode - 0x47;
1411 			goto check_composed;
1412 
1413 		case 0x4F:
1414 		case 0x50:
1415 		case 0x51:		/* keypad 1,2,3 */
1416 			sc->sc_composed_char *= 10;
1417 			sc->sc_composed_char += keycode - 0x4E;
1418 			goto check_composed;
1419 
1420 		case 0x52:		/* keypad 0 */
1421 			sc->sc_composed_char *= 10;
1422 			goto check_composed;
1423 
1424 			/* key released, no interest here */
1425 		case SCAN_RELEASE | 0x47:
1426 		case SCAN_RELEASE | 0x48:
1427 		case SCAN_RELEASE | 0x49:	/* keypad 7,8,9 */
1428 		case SCAN_RELEASE | 0x4B:
1429 		case SCAN_RELEASE | 0x4C:
1430 		case SCAN_RELEASE | 0x4D:	/* keypad 4,5,6 */
1431 		case SCAN_RELEASE | 0x4F:
1432 		case SCAN_RELEASE | 0x50:
1433 		case SCAN_RELEASE | 0x51:	/* keypad 1,2,3 */
1434 		case SCAN_RELEASE | 0x52:	/* keypad 0 */
1435 			goto next_code;
1436 
1437 		case 0x38:		/* left alt key */
1438 			break;
1439 
1440 		default:
1441 			if (sc->sc_composed_char > 0) {
1442 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1443 				sc->sc_composed_char = 0;
1444 				goto errkey;
1445 			}
1446 			break;
1447 		}
1448 	}
1449 	/* keycode to key action */
1450 	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1451 	    (keycode & SCAN_RELEASE),
1452 	    &sc->sc_state, &sc->sc_accents);
1453 	if (action == NOKEY) {
1454 		goto next_code;
1455 	}
1456 done:
1457 	return (action);
1458 
1459 check_composed:
1460 	if (sc->sc_composed_char <= 0xFF) {
1461 		goto next_code;
1462 	}
1463 errkey:
1464 	return (ERRKEY);
1465 }
1466 
1467 /* some useful control functions */
1468 static int
1469 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1470 {
1471 	/* translate LED_XXX bits into the device specific bits */
1472 	static const uint8_t ledmap[8] = {
1473 		0, 2, 1, 3, 4, 6, 5, 7,
1474 	};
1475 	struct ukbd_softc *sc = kbd->kb_data;
1476 	int i;
1477 
1478 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1479     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1480 	int ival;
1481 
1482 #endif
1483 	if (!mtx_owned(&Giant)) {
1484 		/*
1485 		 * XXX big problem: If scroll lock is pressed and "printf()"
1486 		 * is called, the CPU will get here, to un-scroll lock the
1487 		 * keyboard. But if "printf()" acquires the "Giant" lock,
1488 		 * there will be a locking order reversal problem, so the
1489 		 * keyboard system must get out of "Giant" first, before the
1490 		 * CPU can proceed here ...
1491 		 */
1492 		switch (cmd) {
1493 		case KDGKBMODE:
1494 		case KDSKBMODE:
1495 			/* workaround for Geli */
1496 			mtx_lock(&Giant);
1497 			i = ukbd_ioctl(kbd, cmd, arg);
1498 			mtx_unlock(&Giant);
1499 			return (i);
1500 		default:
1501 			return (EINVAL);
1502 		}
1503 	}
1504 
1505 	switch (cmd) {
1506 	case KDGKBMODE:		/* get keyboard mode */
1507 		*(int *)arg = sc->sc_mode;
1508 		break;
1509 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1510     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1511 	case _IO('K', 7):
1512 		ival = IOCPARM_IVAL(arg);
1513 		arg = (caddr_t)&ival;
1514 		/* FALLTHROUGH */
1515 #endif
1516 	case KDSKBMODE:		/* set keyboard mode */
1517 		switch (*(int *)arg) {
1518 		case K_XLATE:
1519 			if (sc->sc_mode != K_XLATE) {
1520 				/* make lock key state and LED state match */
1521 				sc->sc_state &= ~LOCK_MASK;
1522 				sc->sc_state |= KBD_LED_VAL(kbd);
1523 			}
1524 			/* FALLTHROUGH */
1525 		case K_RAW:
1526 		case K_CODE:
1527 			if (sc->sc_mode != *(int *)arg) {
1528 				if (ukbd_is_polling(sc) == 0)
1529 					ukbd_clear_state(kbd);
1530 				sc->sc_mode = *(int *)arg;
1531 			}
1532 			break;
1533 		default:
1534 			return (EINVAL);
1535 		}
1536 		break;
1537 
1538 	case KDGETLED:			/* get keyboard LED */
1539 		*(int *)arg = KBD_LED_VAL(kbd);
1540 		break;
1541 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1542     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1543 	case _IO('K', 66):
1544 		ival = IOCPARM_IVAL(arg);
1545 		arg = (caddr_t)&ival;
1546 		/* FALLTHROUGH */
1547 #endif
1548 	case KDSETLED:			/* set keyboard LED */
1549 		/* NOTE: lock key state in "sc_state" won't be changed */
1550 		if (*(int *)arg & ~LOCK_MASK) {
1551 			return (EINVAL);
1552 		}
1553 		i = *(int *)arg;
1554 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1555 		if (sc->sc_mode == K_XLATE &&
1556 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1557 			if (i & ALKED)
1558 				i |= CLKED;
1559 			else
1560 				i &= ~CLKED;
1561 		}
1562 		if (KBD_HAS_DEVICE(kbd)) {
1563 			ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1564 		}
1565 		KBD_LED_VAL(kbd) = *(int *)arg;
1566 		break;
1567 	case KDGKBSTATE:		/* get lock key state */
1568 		*(int *)arg = sc->sc_state & LOCK_MASK;
1569 		break;
1570 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1571     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1572 	case _IO('K', 20):
1573 		ival = IOCPARM_IVAL(arg);
1574 		arg = (caddr_t)&ival;
1575 		/* FALLTHROUGH */
1576 #endif
1577 	case KDSKBSTATE:		/* set lock key state */
1578 		if (*(int *)arg & ~LOCK_MASK) {
1579 			return (EINVAL);
1580 		}
1581 		sc->sc_state &= ~LOCK_MASK;
1582 		sc->sc_state |= *(int *)arg;
1583 
1584 		/* set LEDs and quit */
1585 		return (ukbd_ioctl(kbd, KDSETLED, arg));
1586 
1587 	case KDSETREPEAT:		/* set keyboard repeat rate (new
1588 					 * interface) */
1589 		if (!KBD_HAS_DEVICE(kbd)) {
1590 			return (0);
1591 		}
1592 		if (((int *)arg)[1] < 0) {
1593 			return (EINVAL);
1594 		}
1595 		if (((int *)arg)[0] < 0) {
1596 			return (EINVAL);
1597 		}
1598 		if (((int *)arg)[0] < 200)	/* fastest possible value */
1599 			kbd->kb_delay1 = 200;
1600 		else
1601 			kbd->kb_delay1 = ((int *)arg)[0];
1602 		kbd->kb_delay2 = ((int *)arg)[1];
1603 		return (0);
1604 
1605 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1606     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1607 	case _IO('K', 67):
1608 		ival = IOCPARM_IVAL(arg);
1609 		arg = (caddr_t)&ival;
1610 		/* FALLTHROUGH */
1611 #endif
1612 	case KDSETRAD:			/* set keyboard repeat rate (old
1613 					 * interface) */
1614 		return (ukbd_set_typematic(kbd, *(int *)arg));
1615 
1616 	case PIO_KEYMAP:		/* set keyboard translation table */
1617 	case PIO_KEYMAPENT:		/* set keyboard translation table
1618 					 * entry */
1619 	case PIO_DEADKEYMAP:		/* set accent key translation table */
1620 		sc->sc_accents = 0;
1621 		/* FALLTHROUGH */
1622 	default:
1623 		return (genkbd_commonioctl(kbd, cmd, arg));
1624 	}
1625 
1626 	return (0);
1627 }
1628 
1629 /* clear the internal state of the keyboard */
1630 static void
1631 ukbd_clear_state(keyboard_t *kbd)
1632 {
1633 	struct ukbd_softc *sc = kbd->kb_data;
1634 
1635 	if (!mtx_owned(&Giant)) {
1636 		/* XXX cludge */
1637 		mtx_lock(&Giant);
1638 		ukbd_clear_state(kbd);
1639 		mtx_unlock(&Giant);
1640 		return;
1641 	}
1642 
1643 	sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1644 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
1645 	sc->sc_accents = 0;
1646 	sc->sc_composed_char = 0;
1647 #ifdef UKBD_EMULATE_ATSCANCODE
1648 	sc->sc_buffered_char[0] = 0;
1649 	sc->sc_buffered_char[1] = 0;
1650 #endif
1651 	memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1652 	memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1653 	memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1654 	memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1655 }
1656 
1657 /* save the internal state, not used */
1658 static int
1659 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1660 {
1661 	return (len == 0) ? 1 : -1;
1662 }
1663 
1664 /* set the internal state, not used */
1665 static int
1666 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1667 {
1668 	return (EINVAL);
1669 }
1670 
1671 static int
1672 ukbd_is_polling(struct ukbd_softc *sc)
1673 {
1674 	int delta;
1675 
1676 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1677 		return (1);	/* polling */
1678 
1679 	delta = ticks - sc->sc_poll_tick_last;
1680 	if ((delta < 0) || (delta >= hz)) {
1681 		sc->sc_poll_detected = 0;
1682 		return (0);		/* not polling */
1683 	}
1684 
1685 	return (sc->sc_poll_detected);
1686 }
1687 
1688 static int
1689 ukbd_polls_other_thread(struct ukbd_softc *sc)
1690 {
1691 	return (ukbd_is_polling(sc) &&
1692 	    (sc->sc_poll_thread != curthread));
1693 }
1694 
1695 static int
1696 ukbd_poll(keyboard_t *kbd, int on)
1697 {
1698 	struct ukbd_softc *sc = kbd->kb_data;
1699 
1700 	if (!mtx_owned(&Giant)) {
1701 		/* XXX cludge */
1702 		int retval;
1703 		mtx_lock(&Giant);
1704 		retval = ukbd_poll(kbd, on);
1705 		mtx_unlock(&Giant);
1706 		return (retval);
1707 	}
1708 
1709 	if (on) {
1710 		sc->sc_flags |= UKBD_FLAG_POLLING;
1711 		sc->sc_poll_thread = curthread;
1712 	} else {
1713 		sc->sc_flags &= ~UKBD_FLAG_POLLING;
1714 		ukbd_start_timer(sc);	/* start timer */
1715 	}
1716 	return (0);
1717 }
1718 
1719 /* local functions */
1720 
1721 static void
1722 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1723 {
1724 	DPRINTF("leds=0x%02x\n", leds);
1725 
1726 	sc->sc_leds = leds;
1727 	sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1728 
1729 	/* start transfer, if not already started */
1730 
1731 	usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1732 }
1733 
1734 static int
1735 ukbd_set_typematic(keyboard_t *kbd, int code)
1736 {
1737 	static const int delays[] = {250, 500, 750, 1000};
1738 	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1739 		68, 76, 84, 92, 100, 110, 118, 126,
1740 		136, 152, 168, 184, 200, 220, 236, 252,
1741 	272, 304, 336, 368, 400, 440, 472, 504};
1742 
1743 	if (code & ~0x7f) {
1744 		return (EINVAL);
1745 	}
1746 	kbd->kb_delay1 = delays[(code >> 5) & 3];
1747 	kbd->kb_delay2 = rates[code & 0x1f];
1748 	return (0);
1749 }
1750 
1751 #ifdef UKBD_EMULATE_ATSCANCODE
1752 static int
1753 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1754 {
1755 	static const int scan[] = {
1756 		/* 89 */
1757 		0x11c,	/* Enter */
1758 		/* 90-99 */
1759 		0x11d,	/* Ctrl-R */
1760 		0x135,	/* Divide */
1761 		0x137 | SCAN_PREFIX_SHIFT,	/* PrintScreen */
1762 		0x138,	/* Alt-R */
1763 		0x147,	/* Home */
1764 		0x148,	/* Up */
1765 		0x149,	/* PageUp */
1766 		0x14b,	/* Left */
1767 		0x14d,	/* Right */
1768 		0x14f,	/* End */
1769 		/* 100-109 */
1770 		0x150,	/* Down */
1771 		0x151,	/* PageDown */
1772 		0x152,	/* Insert */
1773 		0x153,	/* Delete */
1774 		0x146,	/* XXX Pause/Break */
1775 		0x15b,	/* Win_L(Super_L) */
1776 		0x15c,	/* Win_R(Super_R) */
1777 		0x15d,	/* Application(Menu) */
1778 
1779 		/* SUN TYPE 6 USB KEYBOARD */
1780 		0x168,	/* Sun Type 6 Help */
1781 		0x15e,	/* Sun Type 6 Stop */
1782 		/* 110 - 119 */
1783 		0x15f,	/* Sun Type 6 Again */
1784 		0x160,	/* Sun Type 6 Props */
1785 		0x161,	/* Sun Type 6 Undo */
1786 		0x162,	/* Sun Type 6 Front */
1787 		0x163,	/* Sun Type 6 Copy */
1788 		0x164,	/* Sun Type 6 Open */
1789 		0x165,	/* Sun Type 6 Paste */
1790 		0x166,	/* Sun Type 6 Find */
1791 		0x167,	/* Sun Type 6 Cut */
1792 		0x125,	/* Sun Type 6 Mute */
1793 		/* 120 - 128 */
1794 		0x11f,	/* Sun Type 6 VolumeDown */
1795 		0x11e,	/* Sun Type 6 VolumeUp */
1796 		0x120,	/* Sun Type 6 PowerDown */
1797 
1798 		/* Japanese 106/109 keyboard */
1799 		0x73,	/* Keyboard Intl' 1 (backslash / underscore) */
1800 		0x70,	/* Keyboard Intl' 2 (Katakana / Hiragana) */
1801 		0x7d,	/* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1802 		0x79,	/* Keyboard Intl' 4 (Henkan) */
1803 		0x7b,	/* Keyboard Intl' 5 (Muhenkan) */
1804 		0x5c,	/* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1805 	};
1806 
1807 	if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1808 		code = scan[code - 89];
1809 	}
1810 	/* Pause/Break */
1811 	if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1812 		code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1813 	}
1814 	if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1815 		code &= ~SCAN_PREFIX_SHIFT;
1816 	}
1817 	code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1818 
1819 	if (code & SCAN_PREFIX) {
1820 		if (code & SCAN_PREFIX_CTL) {
1821 			/* Ctrl */
1822 			sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1823 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1824 		} else if (code & SCAN_PREFIX_SHIFT) {
1825 			/* Shift */
1826 			sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1827 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1828 		} else {
1829 			sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1830 			sc->sc_buffered_char[1] = 0;
1831 		}
1832 		return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1833 	}
1834 	return (code);
1835 
1836 }
1837 
1838 #endif					/* UKBD_EMULATE_ATSCANCODE */
1839 
1840 static keyboard_switch_t ukbdsw = {
1841 	.probe = &ukbd__probe,
1842 	.init = &ukbd_init,
1843 	.term = &ukbd_term,
1844 	.intr = &ukbd_intr,
1845 	.test_if = &ukbd_test_if,
1846 	.enable = &ukbd_enable,
1847 	.disable = &ukbd_disable,
1848 	.read = &ukbd_read,
1849 	.check = &ukbd_check,
1850 	.read_char = &ukbd_read_char,
1851 	.check_char = &ukbd_check_char,
1852 	.ioctl = &ukbd_ioctl,
1853 	.lock = &ukbd_lock,
1854 	.clear_state = &ukbd_clear_state,
1855 	.get_state = &ukbd_get_state,
1856 	.set_state = &ukbd_set_state,
1857 	.get_fkeystr = &genkbd_get_fkeystr,
1858 	.poll = &ukbd_poll,
1859 	.diag = &genkbd_diag,
1860 };
1861 
1862 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1863 
1864 static int
1865 ukbd_driver_load(module_t mod, int what, void *arg)
1866 {
1867 	switch (what) {
1868 	case MOD_LOAD:
1869 		kbd_add_driver(&ukbd_kbd_driver);
1870 		break;
1871 	case MOD_UNLOAD:
1872 		kbd_delete_driver(&ukbd_kbd_driver);
1873 		break;
1874 	}
1875 	return (0);
1876 }
1877 
1878 static devclass_t ukbd_devclass;
1879 
1880 static device_method_t ukbd_methods[] = {
1881 	DEVMETHOD(device_probe, ukbd_probe),
1882 	DEVMETHOD(device_attach, ukbd_attach),
1883 	DEVMETHOD(device_detach, ukbd_detach),
1884 	DEVMETHOD(device_resume, ukbd_resume),
1885 	{0, 0}
1886 };
1887 
1888 static driver_t ukbd_driver = {
1889 	.name = "ukbd",
1890 	.methods = ukbd_methods,
1891 	.size = sizeof(struct ukbd_softc),
1892 };
1893 
1894 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1895 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
1896 MODULE_VERSION(ukbd, 1);
1897