xref: /freebsd/sys/dev/usb/input/ukbd.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 static int
749 ukbd_probe(device_t dev)
750 {
751 	keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
752 	struct usb_attach_arg *uaa = device_get_ivars(dev);
753 	void *d_ptr;
754 	int error;
755 	uint16_t d_len;
756 
757 	DPRINTFN(11, "\n");
758 
759 	if (sw == NULL) {
760 		return (ENXIO);
761 	}
762 	if (uaa->usb_mode != USB_MODE_HOST) {
763 		return (ENXIO);
764 	}
765 
766 	if (uaa->info.bInterfaceClass != UICLASS_HID)
767 		return (ENXIO);
768 
769 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
770 	    (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD)) {
771 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
772 			return (ENXIO);
773 		else
774 			return (BUS_PROBE_DEFAULT);
775 	}
776 
777 	error = usbd_req_get_hid_desc(uaa->device, NULL,
778 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
779 
780 	if (error)
781 		return (ENXIO);
782 
783 	/*
784 	 * NOTE: we currently don't support USB mouse and USB keyboard
785 	 * on the same USB endpoint.
786 	 */
787 	if (hid_is_collection(d_ptr, d_len,
788 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
789 		/* most likely a mouse */
790 		error = ENXIO;
791 	} else if (hid_is_collection(d_ptr, d_len,
792 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD))) {
793 		if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
794 			error = ENXIO;
795 		else
796 			error = BUS_PROBE_DEFAULT;
797 	} else
798 		error = ENXIO;
799 
800 	free(d_ptr, M_TEMP);
801 	return (error);
802 }
803 
804 static int
805 ukbd_attach(device_t dev)
806 {
807 	struct ukbd_softc *sc = device_get_softc(dev);
808 	struct usb_attach_arg *uaa = device_get_ivars(dev);
809 	int32_t unit = device_get_unit(dev);
810 	keyboard_t *kbd = &sc->sc_kbd;
811 	void *hid_ptr = NULL;
812 	usb_error_t err;
813 	uint32_t flags;
814 	uint16_t n;
815 	uint16_t hid_len;
816 
817 	kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
818 
819 	kbd->kb_data = (void *)sc;
820 
821 	device_set_usb_desc(dev);
822 
823 	sc->sc_udev = uaa->device;
824 	sc->sc_iface = uaa->iface;
825 	sc->sc_iface_index = uaa->info.bIfaceIndex;
826 	sc->sc_iface_no = uaa->info.bIfaceNum;
827 	sc->sc_mode = K_XLATE;
828 
829 	usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
830 
831 	err = usbd_transfer_setup(uaa->device,
832 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
833 	    UKBD_N_TRANSFER, sc, &Giant);
834 
835 	if (err) {
836 		DPRINTF("error=%s\n", usbd_errstr(err));
837 		goto detach;
838 	}
839 	/* setup default keyboard maps */
840 
841 	sc->sc_keymap = key_map;
842 	sc->sc_accmap = accent_map;
843 	for (n = 0; n < UKBD_NFKEY; n++) {
844 		sc->sc_fkeymap[n] = fkey_tab[n];
845 	}
846 
847 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
848 	    sc->sc_fkeymap, UKBD_NFKEY);
849 
850 	KBD_FOUND_DEVICE(kbd);
851 
852 	ukbd_clear_state(kbd);
853 
854 	/*
855 	 * FIXME: set the initial value for lock keys in "sc_state"
856 	 * according to the BIOS data?
857 	 */
858 	KBD_PROBE_DONE(kbd);
859 
860 	/*
861 	 * Set boot protocol if we need the quirk.
862 	 */
863 	if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO)) {
864 		err = usbd_req_set_protocol(sc->sc_udev, NULL,
865 			sc->sc_iface_index, 0);
866 		if (err != USB_ERR_NORMAL_COMPLETION) {
867 			DPRINTF("set protocol error=%s\n", usbd_errstr(err));
868 			goto detach;
869 		}
870 	}
871 
872 	/* figure out if there is an ID byte in the data */
873 	err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
874 	    &hid_len, M_TEMP, uaa->info.bIfaceIndex);
875 	if (err == 0) {
876 		uint8_t apple_keys = 0;
877 		uint8_t temp_id;
878 
879 		/* investigate if this is an Apple Keyboard */
880 		if (hid_locate(hid_ptr, hid_len,
881 		    HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
882 		    hid_input, 0, &sc->sc_loc_apple_eject, &flags,
883 		    &temp_id)) {
884 			if (flags & HIO_VARIABLE)
885 				sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
886 				    UKBD_FLAG_APPLE_SWAP;
887 			DPRINTFN(1, "Found Apple eject-key\n");
888 			apple_keys = 1;
889 			sc->sc_kbd_id = temp_id;
890 		}
891 		if (hid_locate(hid_ptr, hid_len,
892 		    HID_USAGE2(0xFFFF, 0x0003),
893 		    hid_input, 0, &sc->sc_loc_apple_fn, &flags,
894 		    &temp_id)) {
895 			if (flags & HIO_VARIABLE)
896 				sc->sc_flags |= UKBD_FLAG_APPLE_FN;
897 			DPRINTFN(1, "Found Apple FN-key\n");
898 			apple_keys = 1;
899 			sc->sc_kbd_id = temp_id;
900 		}
901 		if (apple_keys == 0) {
902 			/*
903 			 * Assume the first HID ID contains the
904 			 * keyboard data
905 			 */
906 			hid_report_size(hid_ptr, hid_len,
907 			    hid_input, &sc->sc_kbd_id);
908 		}
909 
910 		/* investigate if we need an ID-byte for the leds */
911 		hid_report_size(hid_ptr, hid_len, hid_output, &sc->sc_led_id);
912 
913 		free(hid_ptr, M_TEMP);
914 	}
915 
916 	/* ignore if SETIDLE fails, hence it is not crucial */
917 	err = usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
918 
919 	mtx_lock(&Giant);
920 
921 	ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
922 
923 	KBD_INIT_DONE(kbd);
924 
925 	mtx_unlock(&Giant);
926 
927 	if (kbd_register(kbd) < 0) {
928 		goto detach;
929 	}
930 	KBD_CONFIG_DONE(kbd);
931 
932 	ukbd_enable(kbd);
933 
934 #ifdef KBD_INSTALL_CDEV
935 	if (kbd_attach(kbd)) {
936 		goto detach;
937 	}
938 #endif
939 	sc->sc_flags |= UKBD_FLAG_ATTACHED;
940 
941 	if (bootverbose) {
942 		genkbd_diag(kbd, bootverbose);
943 	}
944 	/* lock keyboard mutex */
945 
946 	mtx_lock(&Giant);
947 
948 	/* start the keyboard */
949 
950 	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
951 
952 	mtx_unlock(&Giant);
953 	return (0);			/* success */
954 
955 detach:
956 	ukbd_detach(dev);
957 	return (ENXIO);			/* error */
958 }
959 
960 static int
961 ukbd_detach(device_t dev)
962 {
963 	struct ukbd_softc *sc = device_get_softc(dev);
964 	int error;
965 
966 	DPRINTF("\n");
967 
968 	mtx_lock(&Giant);
969 
970 	sc->sc_flags |= UKBD_FLAG_GONE;
971 
972 	usb_callout_stop(&sc->sc_callout);
973 
974 	ukbd_disable(&sc->sc_kbd);
975 
976 #ifdef KBD_INSTALL_CDEV
977 	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
978 		error = kbd_detach(&sc->sc_kbd);
979 		if (error) {
980 			/* usb attach cannot return an error */
981 			device_printf(dev, "WARNING: kbd_detach() "
982 			    "returned non-zero! (ignored)\n");
983 		}
984 	}
985 #endif
986 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
987 		error = kbd_unregister(&sc->sc_kbd);
988 		if (error) {
989 			/* usb attach cannot return an error */
990 			device_printf(dev, "WARNING: kbd_unregister() "
991 			    "returned non-zero! (ignored)\n");
992 		}
993 	}
994 	sc->sc_kbd.kb_flags = 0;
995 
996 	mtx_unlock(&Giant);
997 
998 	usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
999 
1000 	usb_callout_drain(&sc->sc_callout);
1001 
1002 	DPRINTF("%s: disconnected\n",
1003 	    device_get_nameunit(dev));
1004 
1005 	return (0);
1006 }
1007 
1008 static int
1009 ukbd_resume(device_t dev)
1010 {
1011 	struct ukbd_softc *sc = device_get_softc(dev);
1012 
1013 	mtx_lock(&Giant);
1014 
1015 	ukbd_clear_state(&sc->sc_kbd);
1016 
1017 	mtx_unlock(&Giant);
1018 
1019 	return (0);
1020 }
1021 
1022 /* early keyboard probe, not supported */
1023 static int
1024 ukbd_configure(int flags)
1025 {
1026 	return (0);
1027 }
1028 
1029 /* detect a keyboard, not used */
1030 static int
1031 ukbd__probe(int unit, void *arg, int flags)
1032 {
1033 	return (ENXIO);
1034 }
1035 
1036 /* reset and initialize the device, not used */
1037 static int
1038 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1039 {
1040 	return (ENXIO);
1041 }
1042 
1043 /* test the interface to the device, not used */
1044 static int
1045 ukbd_test_if(keyboard_t *kbd)
1046 {
1047 	return (0);
1048 }
1049 
1050 /* finish using this keyboard, not used */
1051 static int
1052 ukbd_term(keyboard_t *kbd)
1053 {
1054 	return (ENXIO);
1055 }
1056 
1057 /* keyboard interrupt routine, not used */
1058 static int
1059 ukbd_intr(keyboard_t *kbd, void *arg)
1060 {
1061 	return (0);
1062 }
1063 
1064 /* lock the access to the keyboard, not used */
1065 static int
1066 ukbd_lock(keyboard_t *kbd, int lock)
1067 {
1068 	return (1);
1069 }
1070 
1071 /*
1072  * Enable the access to the device; until this function is called,
1073  * the client cannot read from the keyboard.
1074  */
1075 static int
1076 ukbd_enable(keyboard_t *kbd)
1077 {
1078 	if (!mtx_owned(&Giant)) {
1079 		/* XXX cludge */
1080 		int retval;
1081 		mtx_lock(&Giant);
1082 		retval = ukbd_enable(kbd);
1083 		mtx_unlock(&Giant);
1084 		return (retval);
1085 	}
1086 	KBD_ACTIVATE(kbd);
1087 	return (0);
1088 }
1089 
1090 /* disallow the access to the device */
1091 static int
1092 ukbd_disable(keyboard_t *kbd)
1093 {
1094 	if (!mtx_owned(&Giant)) {
1095 		/* XXX cludge */
1096 		int retval;
1097 		mtx_lock(&Giant);
1098 		retval = ukbd_disable(kbd);
1099 		mtx_unlock(&Giant);
1100 		return (retval);
1101 	}
1102 	KBD_DEACTIVATE(kbd);
1103 	return (0);
1104 }
1105 
1106 /* check if data is waiting */
1107 static int
1108 ukbd_check(keyboard_t *kbd)
1109 {
1110 	struct ukbd_softc *sc = kbd->kb_data;
1111 
1112 	if (!KBD_IS_ACTIVE(kbd))
1113 		return (0);
1114 
1115 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1116 		if (!mtx_owned(&Giant)) {
1117 			/* XXX cludge */
1118 			int retval;
1119 			mtx_lock(&Giant);
1120 			retval = ukbd_check(kbd);
1121 			mtx_unlock(&Giant);
1122 			return (retval);
1123 		}
1124 	} else {
1125 		/* XXX the keyboard layer requires Giant */
1126 		if (!mtx_owned(&Giant))
1127 			return (0);
1128 	}
1129 
1130 	/* check if key belongs to this thread */
1131 	if (ukbd_polls_other_thread(sc))
1132 		return (0);
1133 
1134 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1135 		ukbd_do_poll(sc, 0);
1136 
1137 #ifdef UKBD_EMULATE_ATSCANCODE
1138 	if (sc->sc_buffered_char[0]) {
1139 		return (1);
1140 	}
1141 #endif
1142 	if (sc->sc_inputs > 0) {
1143 		return (1);
1144 	}
1145 	return (0);
1146 }
1147 
1148 /* check if char is waiting */
1149 static int
1150 ukbd_check_char(keyboard_t *kbd)
1151 {
1152 	struct ukbd_softc *sc = kbd->kb_data;
1153 
1154 	if (!KBD_IS_ACTIVE(kbd))
1155 		return (0);
1156 
1157 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1158 		if (!mtx_owned(&Giant)) {
1159 			/* XXX cludge */
1160 			int retval;
1161 			mtx_lock(&Giant);
1162 			retval = ukbd_check_char(kbd);
1163 			mtx_unlock(&Giant);
1164 			return (retval);
1165 		}
1166 	} else {
1167 		/* XXX the keyboard layer requires Giant */
1168 		if (!mtx_owned(&Giant))
1169 			return (0);
1170 	}
1171 
1172 	/* check if key belongs to this thread */
1173 	if (ukbd_polls_other_thread(sc))
1174 		return (0);
1175 
1176 	if ((sc->sc_composed_char > 0) &&
1177 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1178 		return (1);
1179 	}
1180 	return (ukbd_check(kbd));
1181 }
1182 
1183 
1184 /* read one byte from the keyboard if it's allowed */
1185 static int
1186 ukbd_read(keyboard_t *kbd, int wait)
1187 {
1188 	struct ukbd_softc *sc = kbd->kb_data;
1189 	int32_t usbcode;
1190 
1191 #ifdef UKBD_EMULATE_ATSCANCODE
1192 	uint32_t keycode;
1193 	uint32_t scancode;
1194 
1195 #endif
1196 	if (!KBD_IS_ACTIVE(kbd))
1197 		return (-1);
1198 
1199 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1200 		if (!mtx_owned(&Giant)) {
1201 			/* XXX cludge */
1202 			int retval;
1203 			mtx_lock(&Giant);
1204 			retval = ukbd_read(kbd, wait);
1205 			mtx_unlock(&Giant);
1206 			return (retval);
1207 		}
1208 	} else {
1209 		/* XXX the keyboard layer requires Giant */
1210 		if (!mtx_owned(&Giant))
1211 			return (-1);
1212 	}
1213 
1214 	/* check if key belongs to this thread */
1215 	if (ukbd_polls_other_thread(sc))
1216 		return (-1);
1217 
1218 #ifdef UKBD_EMULATE_ATSCANCODE
1219 	if (sc->sc_buffered_char[0]) {
1220 		scancode = sc->sc_buffered_char[0];
1221 		if (scancode & SCAN_PREFIX) {
1222 			sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1223 			return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1224 		}
1225 		sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1226 		sc->sc_buffered_char[1] = 0;
1227 		return (scancode);
1228 	}
1229 #endif					/* UKBD_EMULATE_ATSCANCODE */
1230 
1231 	/* XXX */
1232 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1233 	if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1234 		return (-1);
1235 
1236 	++(kbd->kb_count);
1237 
1238 #ifdef UKBD_EMULATE_ATSCANCODE
1239 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1240 	if (keycode == NN) {
1241 		return -1;
1242 	}
1243 	return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1244 	    (usbcode & KEY_RELEASE)));
1245 #else					/* !UKBD_EMULATE_ATSCANCODE */
1246 	return (usbcode);
1247 #endif					/* UKBD_EMULATE_ATSCANCODE */
1248 }
1249 
1250 /* read char from the keyboard */
1251 static uint32_t
1252 ukbd_read_char(keyboard_t *kbd, int wait)
1253 {
1254 	struct ukbd_softc *sc = kbd->kb_data;
1255 	uint32_t action;
1256 	uint32_t keycode;
1257 	int32_t usbcode;
1258 
1259 #ifdef UKBD_EMULATE_ATSCANCODE
1260 	uint32_t scancode;
1261 
1262 #endif
1263 
1264 	if (!KBD_IS_ACTIVE(kbd))
1265 		return (NOKEY);
1266 
1267 	if (sc->sc_flags & UKBD_FLAG_POLLING) {
1268 		if (!mtx_owned(&Giant)) {
1269 			/* XXX cludge */
1270 			int retval;
1271 			mtx_lock(&Giant);
1272 			retval = ukbd_read_char(kbd, wait);
1273 			mtx_unlock(&Giant);
1274 			return (retval);
1275 		}
1276 	} else {
1277 		/* XXX the keyboard layer requires Giant */
1278 		if (!mtx_owned(&Giant))
1279 			return (NOKEY);
1280 	}
1281 
1282 	/* check if key belongs to this thread */
1283 	if (ukbd_polls_other_thread(sc))
1284 		return (NOKEY);
1285 
1286 next_code:
1287 
1288 	/* do we have a composed char to return ? */
1289 
1290 	if ((sc->sc_composed_char > 0) &&
1291 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1292 
1293 		action = sc->sc_composed_char;
1294 		sc->sc_composed_char = 0;
1295 
1296 		if (action > 0xFF) {
1297 			goto errkey;
1298 		}
1299 		goto done;
1300 	}
1301 #ifdef UKBD_EMULATE_ATSCANCODE
1302 
1303 	/* do we have a pending raw scan code? */
1304 
1305 	if (sc->sc_mode == K_RAW) {
1306 		scancode = sc->sc_buffered_char[0];
1307 		if (scancode) {
1308 			if (scancode & SCAN_PREFIX) {
1309 				sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1310 				return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1311 			}
1312 			sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1313 			sc->sc_buffered_char[1] = 0;
1314 			return (scancode);
1315 		}
1316 	}
1317 #endif					/* UKBD_EMULATE_ATSCANCODE */
1318 
1319 	/* see if there is something in the keyboard port */
1320 	/* XXX */
1321 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1322 	if (usbcode == -1) {
1323 		return (NOKEY);
1324 	}
1325 	++kbd->kb_count;
1326 
1327 #ifdef UKBD_EMULATE_ATSCANCODE
1328 	/* USB key index -> key code -> AT scan code */
1329 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1330 	if (keycode == NN) {
1331 		return (NOKEY);
1332 	}
1333 	/* return an AT scan code for the K_RAW mode */
1334 	if (sc->sc_mode == K_RAW) {
1335 		return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1336 		    (usbcode & KEY_RELEASE)));
1337 	}
1338 #else					/* !UKBD_EMULATE_ATSCANCODE */
1339 
1340 	/* return the byte as is for the K_RAW mode */
1341 	if (sc->sc_mode == K_RAW) {
1342 		return (usbcode);
1343 	}
1344 	/* USB key index -> key code */
1345 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1346 	if (keycode == NN) {
1347 		return (NOKEY);
1348 	}
1349 #endif					/* UKBD_EMULATE_ATSCANCODE */
1350 
1351 	switch (keycode) {
1352 	case 0x38:			/* left alt (compose key) */
1353 		if (usbcode & KEY_RELEASE) {
1354 			if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1355 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1356 
1357 				if (sc->sc_composed_char > 0xFF) {
1358 					sc->sc_composed_char = 0;
1359 				}
1360 			}
1361 		} else {
1362 			if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1363 				sc->sc_flags |= UKBD_FLAG_COMPOSE;
1364 				sc->sc_composed_char = 0;
1365 			}
1366 		}
1367 		break;
1368 		/* XXX: I don't like these... */
1369 	case 0x5c:			/* print screen */
1370 		if (sc->sc_flags & ALTS) {
1371 			keycode = 0x54;	/* sysrq */
1372 		}
1373 		break;
1374 	case 0x68:			/* pause/break */
1375 		if (sc->sc_flags & CTLS) {
1376 			keycode = 0x6c;	/* break */
1377 		}
1378 		break;
1379 	}
1380 
1381 	/* return the key code in the K_CODE mode */
1382 	if (usbcode & KEY_RELEASE) {
1383 		keycode |= SCAN_RELEASE;
1384 	}
1385 	if (sc->sc_mode == K_CODE) {
1386 		return (keycode);
1387 	}
1388 	/* compose a character code */
1389 	if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1390 		switch (keycode) {
1391 			/* key pressed, process it */
1392 		case 0x47:
1393 		case 0x48:
1394 		case 0x49:		/* keypad 7,8,9 */
1395 			sc->sc_composed_char *= 10;
1396 			sc->sc_composed_char += keycode - 0x40;
1397 			goto check_composed;
1398 
1399 		case 0x4B:
1400 		case 0x4C:
1401 		case 0x4D:		/* keypad 4,5,6 */
1402 			sc->sc_composed_char *= 10;
1403 			sc->sc_composed_char += keycode - 0x47;
1404 			goto check_composed;
1405 
1406 		case 0x4F:
1407 		case 0x50:
1408 		case 0x51:		/* keypad 1,2,3 */
1409 			sc->sc_composed_char *= 10;
1410 			sc->sc_composed_char += keycode - 0x4E;
1411 			goto check_composed;
1412 
1413 		case 0x52:		/* keypad 0 */
1414 			sc->sc_composed_char *= 10;
1415 			goto check_composed;
1416 
1417 			/* key released, no interest here */
1418 		case SCAN_RELEASE | 0x47:
1419 		case SCAN_RELEASE | 0x48:
1420 		case SCAN_RELEASE | 0x49:	/* keypad 7,8,9 */
1421 		case SCAN_RELEASE | 0x4B:
1422 		case SCAN_RELEASE | 0x4C:
1423 		case SCAN_RELEASE | 0x4D:	/* keypad 4,5,6 */
1424 		case SCAN_RELEASE | 0x4F:
1425 		case SCAN_RELEASE | 0x50:
1426 		case SCAN_RELEASE | 0x51:	/* keypad 1,2,3 */
1427 		case SCAN_RELEASE | 0x52:	/* keypad 0 */
1428 			goto next_code;
1429 
1430 		case 0x38:		/* left alt key */
1431 			break;
1432 
1433 		default:
1434 			if (sc->sc_composed_char > 0) {
1435 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1436 				sc->sc_composed_char = 0;
1437 				goto errkey;
1438 			}
1439 			break;
1440 		}
1441 	}
1442 	/* keycode to key action */
1443 	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1444 	    (keycode & SCAN_RELEASE),
1445 	    &sc->sc_state, &sc->sc_accents);
1446 	if (action == NOKEY) {
1447 		goto next_code;
1448 	}
1449 done:
1450 	return (action);
1451 
1452 check_composed:
1453 	if (sc->sc_composed_char <= 0xFF) {
1454 		goto next_code;
1455 	}
1456 errkey:
1457 	return (ERRKEY);
1458 }
1459 
1460 /* some useful control functions */
1461 static int
1462 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1463 {
1464 	/* translate LED_XXX bits into the device specific bits */
1465 	static const uint8_t ledmap[8] = {
1466 		0, 2, 1, 3, 4, 6, 5, 7,
1467 	};
1468 	struct ukbd_softc *sc = kbd->kb_data;
1469 	int i;
1470 
1471 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1472     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1473 	int ival;
1474 
1475 #endif
1476 	if (!mtx_owned(&Giant)) {
1477 		/*
1478 		 * XXX big problem: If scroll lock is pressed and "printf()"
1479 		 * is called, the CPU will get here, to un-scroll lock the
1480 		 * keyboard. But if "printf()" acquires the "Giant" lock,
1481 		 * there will be a locking order reversal problem, so the
1482 		 * keyboard system must get out of "Giant" first, before the
1483 		 * CPU can proceed here ...
1484 		 */
1485 		switch (cmd) {
1486 		case KDGKBMODE:
1487 		case KDSKBMODE:
1488 			/* workaround for Geli */
1489 			mtx_lock(&Giant);
1490 			i = ukbd_ioctl(kbd, cmd, arg);
1491 			mtx_unlock(&Giant);
1492 			return (i);
1493 		default:
1494 			return (EINVAL);
1495 		}
1496 	}
1497 
1498 	switch (cmd) {
1499 	case KDGKBMODE:		/* get keyboard mode */
1500 		*(int *)arg = sc->sc_mode;
1501 		break;
1502 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1503     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1504 	case _IO('K', 7):
1505 		ival = IOCPARM_IVAL(arg);
1506 		arg = (caddr_t)&ival;
1507 		/* FALLTHROUGH */
1508 #endif
1509 	case KDSKBMODE:		/* set keyboard mode */
1510 		switch (*(int *)arg) {
1511 		case K_XLATE:
1512 			if (sc->sc_mode != K_XLATE) {
1513 				/* make lock key state and LED state match */
1514 				sc->sc_state &= ~LOCK_MASK;
1515 				sc->sc_state |= KBD_LED_VAL(kbd);
1516 			}
1517 			/* FALLTHROUGH */
1518 		case K_RAW:
1519 		case K_CODE:
1520 			if (sc->sc_mode != *(int *)arg) {
1521 				if (ukbd_is_polling(sc) == 0)
1522 					ukbd_clear_state(kbd);
1523 				sc->sc_mode = *(int *)arg;
1524 			}
1525 			break;
1526 		default:
1527 			return (EINVAL);
1528 		}
1529 		break;
1530 
1531 	case KDGETLED:			/* get keyboard LED */
1532 		*(int *)arg = KBD_LED_VAL(kbd);
1533 		break;
1534 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1535     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1536 	case _IO('K', 66):
1537 		ival = IOCPARM_IVAL(arg);
1538 		arg = (caddr_t)&ival;
1539 		/* FALLTHROUGH */
1540 #endif
1541 	case KDSETLED:			/* set keyboard LED */
1542 		/* NOTE: lock key state in "sc_state" won't be changed */
1543 		if (*(int *)arg & ~LOCK_MASK) {
1544 			return (EINVAL);
1545 		}
1546 		i = *(int *)arg;
1547 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1548 		if (sc->sc_mode == K_XLATE &&
1549 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1550 			if (i & ALKED)
1551 				i |= CLKED;
1552 			else
1553 				i &= ~CLKED;
1554 		}
1555 		if (KBD_HAS_DEVICE(kbd)) {
1556 			ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1557 		}
1558 		KBD_LED_VAL(kbd) = *(int *)arg;
1559 		break;
1560 	case KDGKBSTATE:		/* get lock key state */
1561 		*(int *)arg = sc->sc_state & LOCK_MASK;
1562 		break;
1563 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1564     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1565 	case _IO('K', 20):
1566 		ival = IOCPARM_IVAL(arg);
1567 		arg = (caddr_t)&ival;
1568 		/* FALLTHROUGH */
1569 #endif
1570 	case KDSKBSTATE:		/* set lock key state */
1571 		if (*(int *)arg & ~LOCK_MASK) {
1572 			return (EINVAL);
1573 		}
1574 		sc->sc_state &= ~LOCK_MASK;
1575 		sc->sc_state |= *(int *)arg;
1576 
1577 		/* set LEDs and quit */
1578 		return (ukbd_ioctl(kbd, KDSETLED, arg));
1579 
1580 	case KDSETREPEAT:		/* set keyboard repeat rate (new
1581 					 * interface) */
1582 		if (!KBD_HAS_DEVICE(kbd)) {
1583 			return (0);
1584 		}
1585 		if (((int *)arg)[1] < 0) {
1586 			return (EINVAL);
1587 		}
1588 		if (((int *)arg)[0] < 0) {
1589 			return (EINVAL);
1590 		}
1591 		if (((int *)arg)[0] < 200)	/* fastest possible value */
1592 			kbd->kb_delay1 = 200;
1593 		else
1594 			kbd->kb_delay1 = ((int *)arg)[0];
1595 		kbd->kb_delay2 = ((int *)arg)[1];
1596 		return (0);
1597 
1598 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1599     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1600 	case _IO('K', 67):
1601 		ival = IOCPARM_IVAL(arg);
1602 		arg = (caddr_t)&ival;
1603 		/* FALLTHROUGH */
1604 #endif
1605 	case KDSETRAD:			/* set keyboard repeat rate (old
1606 					 * interface) */
1607 		return (ukbd_set_typematic(kbd, *(int *)arg));
1608 
1609 	case PIO_KEYMAP:		/* set keyboard translation table */
1610 	case PIO_KEYMAPENT:		/* set keyboard translation table
1611 					 * entry */
1612 	case PIO_DEADKEYMAP:		/* set accent key translation table */
1613 		sc->sc_accents = 0;
1614 		/* FALLTHROUGH */
1615 	default:
1616 		return (genkbd_commonioctl(kbd, cmd, arg));
1617 	}
1618 
1619 	return (0);
1620 }
1621 
1622 /* clear the internal state of the keyboard */
1623 static void
1624 ukbd_clear_state(keyboard_t *kbd)
1625 {
1626 	struct ukbd_softc *sc = kbd->kb_data;
1627 
1628 	if (!mtx_owned(&Giant)) {
1629 		/* XXX cludge */
1630 		mtx_lock(&Giant);
1631 		ukbd_clear_state(kbd);
1632 		mtx_unlock(&Giant);
1633 		return;
1634 	}
1635 
1636 	sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1637 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
1638 	sc->sc_accents = 0;
1639 	sc->sc_composed_char = 0;
1640 #ifdef UKBD_EMULATE_ATSCANCODE
1641 	sc->sc_buffered_char[0] = 0;
1642 	sc->sc_buffered_char[1] = 0;
1643 #endif
1644 	memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1645 	memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
1646 	memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
1647 	memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
1648 }
1649 
1650 /* save the internal state, not used */
1651 static int
1652 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1653 {
1654 	return (len == 0) ? 1 : -1;
1655 }
1656 
1657 /* set the internal state, not used */
1658 static int
1659 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1660 {
1661 	return (EINVAL);
1662 }
1663 
1664 static int
1665 ukbd_is_polling(struct ukbd_softc *sc)
1666 {
1667 	int delta;
1668 
1669 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1670 		return (1);	/* polling */
1671 
1672 	delta = ticks - sc->sc_poll_tick_last;
1673 	if ((delta < 0) || (delta >= hz)) {
1674 		sc->sc_poll_detected = 0;
1675 		return (0);		/* not polling */
1676 	}
1677 
1678 	return (sc->sc_poll_detected);
1679 }
1680 
1681 static int
1682 ukbd_polls_other_thread(struct ukbd_softc *sc)
1683 {
1684 	return (ukbd_is_polling(sc) &&
1685 	    (sc->sc_poll_thread != curthread));
1686 }
1687 
1688 static int
1689 ukbd_poll(keyboard_t *kbd, int on)
1690 {
1691 	struct ukbd_softc *sc = kbd->kb_data;
1692 
1693 	if (!mtx_owned(&Giant)) {
1694 		/* XXX cludge */
1695 		int retval;
1696 		mtx_lock(&Giant);
1697 		retval = ukbd_poll(kbd, on);
1698 		mtx_unlock(&Giant);
1699 		return (retval);
1700 	}
1701 
1702 	if (on) {
1703 		sc->sc_flags |= UKBD_FLAG_POLLING;
1704 		sc->sc_poll_thread = curthread;
1705 	} else {
1706 		sc->sc_flags &= ~UKBD_FLAG_POLLING;
1707 		ukbd_start_timer(sc);	/* start timer */
1708 	}
1709 	return (0);
1710 }
1711 
1712 /* local functions */
1713 
1714 static void
1715 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1716 {
1717 	DPRINTF("leds=0x%02x\n", leds);
1718 
1719 	sc->sc_leds = leds;
1720 	sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1721 
1722 	/* start transfer, if not already started */
1723 
1724 	usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1725 }
1726 
1727 static int
1728 ukbd_set_typematic(keyboard_t *kbd, int code)
1729 {
1730 	static const int delays[] = {250, 500, 750, 1000};
1731 	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1732 		68, 76, 84, 92, 100, 110, 118, 126,
1733 		136, 152, 168, 184, 200, 220, 236, 252,
1734 	272, 304, 336, 368, 400, 440, 472, 504};
1735 
1736 	if (code & ~0x7f) {
1737 		return (EINVAL);
1738 	}
1739 	kbd->kb_delay1 = delays[(code >> 5) & 3];
1740 	kbd->kb_delay2 = rates[code & 0x1f];
1741 	return (0);
1742 }
1743 
1744 #ifdef UKBD_EMULATE_ATSCANCODE
1745 static int
1746 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1747 {
1748 	static const int scan[] = {
1749 		/* 89 */
1750 		0x11c,	/* Enter */
1751 		/* 90-99 */
1752 		0x11d,	/* Ctrl-R */
1753 		0x135,	/* Divide */
1754 		0x137 | SCAN_PREFIX_SHIFT,	/* PrintScreen */
1755 		0x138,	/* Alt-R */
1756 		0x147,	/* Home */
1757 		0x148,	/* Up */
1758 		0x149,	/* PageUp */
1759 		0x14b,	/* Left */
1760 		0x14d,	/* Right */
1761 		0x14f,	/* End */
1762 		/* 100-109 */
1763 		0x150,	/* Down */
1764 		0x151,	/* PageDown */
1765 		0x152,	/* Insert */
1766 		0x153,	/* Delete */
1767 		0x146,	/* XXX Pause/Break */
1768 		0x15b,	/* Win_L(Super_L) */
1769 		0x15c,	/* Win_R(Super_R) */
1770 		0x15d,	/* Application(Menu) */
1771 
1772 		/* SUN TYPE 6 USB KEYBOARD */
1773 		0x168,	/* Sun Type 6 Help */
1774 		0x15e,	/* Sun Type 6 Stop */
1775 		/* 110 - 119 */
1776 		0x15f,	/* Sun Type 6 Again */
1777 		0x160,	/* Sun Type 6 Props */
1778 		0x161,	/* Sun Type 6 Undo */
1779 		0x162,	/* Sun Type 6 Front */
1780 		0x163,	/* Sun Type 6 Copy */
1781 		0x164,	/* Sun Type 6 Open */
1782 		0x165,	/* Sun Type 6 Paste */
1783 		0x166,	/* Sun Type 6 Find */
1784 		0x167,	/* Sun Type 6 Cut */
1785 		0x125,	/* Sun Type 6 Mute */
1786 		/* 120 - 128 */
1787 		0x11f,	/* Sun Type 6 VolumeDown */
1788 		0x11e,	/* Sun Type 6 VolumeUp */
1789 		0x120,	/* Sun Type 6 PowerDown */
1790 
1791 		/* Japanese 106/109 keyboard */
1792 		0x73,	/* Keyboard Intl' 1 (backslash / underscore) */
1793 		0x70,	/* Keyboard Intl' 2 (Katakana / Hiragana) */
1794 		0x7d,	/* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
1795 		0x79,	/* Keyboard Intl' 4 (Henkan) */
1796 		0x7b,	/* Keyboard Intl' 5 (Muhenkan) */
1797 		0x5c,	/* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
1798 	};
1799 
1800 	if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1801 		code = scan[code - 89];
1802 	}
1803 	/* Pause/Break */
1804 	if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1805 		code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1806 	}
1807 	if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1808 		code &= ~SCAN_PREFIX_SHIFT;
1809 	}
1810 	code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1811 
1812 	if (code & SCAN_PREFIX) {
1813 		if (code & SCAN_PREFIX_CTL) {
1814 			/* Ctrl */
1815 			sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1816 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1817 		} else if (code & SCAN_PREFIX_SHIFT) {
1818 			/* Shift */
1819 			sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1820 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1821 		} else {
1822 			sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1823 			sc->sc_buffered_char[1] = 0;
1824 		}
1825 		return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1826 	}
1827 	return (code);
1828 
1829 }
1830 
1831 #endif					/* UKBD_EMULATE_ATSCANCODE */
1832 
1833 static keyboard_switch_t ukbdsw = {
1834 	.probe = &ukbd__probe,
1835 	.init = &ukbd_init,
1836 	.term = &ukbd_term,
1837 	.intr = &ukbd_intr,
1838 	.test_if = &ukbd_test_if,
1839 	.enable = &ukbd_enable,
1840 	.disable = &ukbd_disable,
1841 	.read = &ukbd_read,
1842 	.check = &ukbd_check,
1843 	.read_char = &ukbd_read_char,
1844 	.check_char = &ukbd_check_char,
1845 	.ioctl = &ukbd_ioctl,
1846 	.lock = &ukbd_lock,
1847 	.clear_state = &ukbd_clear_state,
1848 	.get_state = &ukbd_get_state,
1849 	.set_state = &ukbd_set_state,
1850 	.get_fkeystr = &genkbd_get_fkeystr,
1851 	.poll = &ukbd_poll,
1852 	.diag = &genkbd_diag,
1853 };
1854 
1855 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1856 
1857 static int
1858 ukbd_driver_load(module_t mod, int what, void *arg)
1859 {
1860 	switch (what) {
1861 	case MOD_LOAD:
1862 		kbd_add_driver(&ukbd_kbd_driver);
1863 		break;
1864 	case MOD_UNLOAD:
1865 		kbd_delete_driver(&ukbd_kbd_driver);
1866 		break;
1867 	}
1868 	return (0);
1869 }
1870 
1871 static devclass_t ukbd_devclass;
1872 
1873 static device_method_t ukbd_methods[] = {
1874 	DEVMETHOD(device_probe, ukbd_probe),
1875 	DEVMETHOD(device_attach, ukbd_attach),
1876 	DEVMETHOD(device_detach, ukbd_detach),
1877 	DEVMETHOD(device_resume, ukbd_resume),
1878 	{0, 0}
1879 };
1880 
1881 static driver_t ukbd_driver = {
1882 	.name = "ukbd",
1883 	.methods = ukbd_methods,
1884 	.size = sizeof(struct ukbd_softc),
1885 };
1886 
1887 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1888 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
1889 MODULE_VERSION(ukbd, 1);
1890