xref: /freebsd/sys/dev/usb/input/ukbd.c (revision 0b37c1590418417c894529d371800dfac71ef887)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 
5 /*-
6  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
7  *
8  * Copyright (c) 1998 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Lennart Augustsson (lennart@augustsson.net) at
13  * Carlstedt Research & Technology.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 /*
39  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
40  */
41 
42 #include "opt_kbd.h"
43 #include "opt_ukbd.h"
44 #include "opt_evdev.h"
45 
46 #include <sys/stdint.h>
47 #include <sys/stddef.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/bus.h>
54 #include <sys/module.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sx.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
70 
71 #define	USB_DEBUG_VAR ukbd_debug
72 #include <dev/usb/usb_debug.h>
73 
74 #include <dev/usb/quirk/usb_quirk.h>
75 
76 #ifdef EVDEV_SUPPORT
77 #include <dev/evdev/input.h>
78 #include <dev/evdev/evdev.h>
79 #endif
80 
81 #include <sys/ioccom.h>
82 #include <sys/filio.h>
83 #include <sys/kbio.h>
84 
85 #include <dev/kbd/kbdreg.h>
86 
87 /* the initial key map, accent map and fkey strings */
88 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
89 #define	KBD_DFLT_KEYMAP
90 #include "ukbdmap.h"
91 #endif
92 
93 /* the following file must be included after "ukbdmap.h" */
94 #include <dev/kbd/kbdtables.h>
95 
96 #ifdef USB_DEBUG
97 static int ukbd_debug = 0;
98 static int ukbd_no_leds = 0;
99 static int ukbd_pollrate = 0;
100 
101 static SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB keyboard");
102 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RWTUN,
103     &ukbd_debug, 0, "Debug level");
104 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, no_leds, CTLFLAG_RWTUN,
105     &ukbd_no_leds, 0, "Disables setting of keyboard leds");
106 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, pollrate, CTLFLAG_RWTUN,
107     &ukbd_pollrate, 0, "Force this polling rate, 1-1000Hz");
108 #endif
109 
110 #define	UKBD_EMULATE_ATSCANCODE	       1
111 #define	UKBD_DRIVER_NAME          "ukbd"
112 #define	UKBD_NMOD                     8	/* units */
113 #define	UKBD_NKEYCODE                 6	/* units */
114 #define	UKBD_IN_BUF_SIZE  (2*(UKBD_NMOD + (2*UKBD_NKEYCODE)))	/* bytes */
115 #define	UKBD_IN_BUF_FULL  ((UKBD_IN_BUF_SIZE / 2) - 1)	/* bytes */
116 #define	UKBD_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
117 #define	UKBD_BUFFER_SIZE	      64	/* bytes */
118 
119 struct ukbd_data {
120 	uint16_t	modifiers;
121 #define	MOD_CONTROL_L	0x01
122 #define	MOD_CONTROL_R	0x10
123 #define	MOD_SHIFT_L	0x02
124 #define	MOD_SHIFT_R	0x20
125 #define	MOD_ALT_L	0x04
126 #define	MOD_ALT_R	0x40
127 #define	MOD_WIN_L	0x08
128 #define	MOD_WIN_R	0x80
129 /* internal */
130 #define	MOD_EJECT	0x0100
131 #define	MOD_FN		0x0200
132 	uint8_t	keycode[UKBD_NKEYCODE];
133 };
134 
135 enum {
136 	UKBD_INTR_DT_0,
137 	UKBD_INTR_DT_1,
138 	UKBD_CTRL_LED,
139 	UKBD_N_TRANSFER,
140 };
141 
142 struct ukbd_softc {
143 	keyboard_t sc_kbd;
144 	keymap_t sc_keymap;
145 	accentmap_t sc_accmap;
146 	fkeytab_t sc_fkeymap[UKBD_NFKEY];
147 	struct hid_location sc_loc_apple_eject;
148 	struct hid_location sc_loc_apple_fn;
149 	struct hid_location sc_loc_ctrl_l;
150 	struct hid_location sc_loc_ctrl_r;
151 	struct hid_location sc_loc_shift_l;
152 	struct hid_location sc_loc_shift_r;
153 	struct hid_location sc_loc_alt_l;
154 	struct hid_location sc_loc_alt_r;
155 	struct hid_location sc_loc_win_l;
156 	struct hid_location sc_loc_win_r;
157 	struct hid_location sc_loc_events;
158 	struct hid_location sc_loc_numlock;
159 	struct hid_location sc_loc_capslock;
160 	struct hid_location sc_loc_scrolllock;
161 	struct usb_callout sc_callout;
162 	struct ukbd_data sc_ndata;
163 	struct ukbd_data sc_odata;
164 
165 	struct thread *sc_poll_thread;
166 	struct usb_device *sc_udev;
167 	struct usb_interface *sc_iface;
168 	struct usb_xfer *sc_xfer[UKBD_N_TRANSFER];
169 #ifdef EVDEV_SUPPORT
170 	struct evdev_dev *sc_evdev;
171 #endif
172 
173 	sbintime_t sc_co_basetime;
174 	int	sc_delay;
175 	uint32_t sc_ntime[UKBD_NKEYCODE];
176 	uint32_t sc_otime[UKBD_NKEYCODE];
177 	uint32_t sc_input[UKBD_IN_BUF_SIZE];	/* input buffer */
178 	uint32_t sc_time_ms;
179 	uint32_t sc_composed_char;	/* composed char code, if non-zero */
180 #ifdef UKBD_EMULATE_ATSCANCODE
181 	uint32_t sc_buffered_char[2];
182 #endif
183 	uint32_t sc_flags;		/* flags */
184 #define	UKBD_FLAG_COMPOSE	0x00000001
185 #define	UKBD_FLAG_POLLING	0x00000002
186 #define	UKBD_FLAG_SET_LEDS	0x00000004
187 #define	UKBD_FLAG_ATTACHED	0x00000010
188 #define	UKBD_FLAG_GONE		0x00000020
189 
190 #define	UKBD_FLAG_HID_MASK	0x003fffc0
191 #define	UKBD_FLAG_APPLE_EJECT	0x00000040
192 #define	UKBD_FLAG_APPLE_FN	0x00000080
193 #define	UKBD_FLAG_APPLE_SWAP	0x00000100
194 #define	UKBD_FLAG_CTRL_L	0x00000400
195 #define	UKBD_FLAG_CTRL_R	0x00000800
196 #define	UKBD_FLAG_SHIFT_L	0x00001000
197 #define	UKBD_FLAG_SHIFT_R	0x00002000
198 #define	UKBD_FLAG_ALT_L		0x00004000
199 #define	UKBD_FLAG_ALT_R		0x00008000
200 #define	UKBD_FLAG_WIN_L		0x00010000
201 #define	UKBD_FLAG_WIN_R		0x00020000
202 #define	UKBD_FLAG_EVENTS	0x00040000
203 #define	UKBD_FLAG_NUMLOCK	0x00080000
204 #define	UKBD_FLAG_CAPSLOCK	0x00100000
205 #define	UKBD_FLAG_SCROLLLOCK 	0x00200000
206 
207 	int	sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
208 	int	sc_state;		/* shift/lock key state */
209 	int	sc_accents;		/* accent key index (> 0) */
210 	int	sc_polling;		/* polling recursion count */
211 	int	sc_led_size;
212 	int	sc_kbd_size;
213 
214 	uint16_t sc_inputs;
215 	uint16_t sc_inputhead;
216 	uint16_t sc_inputtail;
217 	uint16_t sc_modifiers;
218 
219 	uint8_t	sc_leds;		/* store for async led requests */
220 	uint8_t	sc_iface_index;
221 	uint8_t	sc_iface_no;
222 	uint8_t sc_id_apple_eject;
223 	uint8_t sc_id_apple_fn;
224 	uint8_t sc_id_ctrl_l;
225 	uint8_t sc_id_ctrl_r;
226 	uint8_t sc_id_shift_l;
227 	uint8_t sc_id_shift_r;
228 	uint8_t sc_id_alt_l;
229 	uint8_t sc_id_alt_r;
230 	uint8_t sc_id_win_l;
231 	uint8_t sc_id_win_r;
232 	uint8_t sc_id_event;
233 	uint8_t sc_id_numlock;
234 	uint8_t sc_id_capslock;
235 	uint8_t sc_id_scrolllock;
236 	uint8_t sc_id_events;
237 	uint8_t sc_kbd_id;
238 
239 	uint8_t sc_buffer[UKBD_BUFFER_SIZE];
240 };
241 
242 #define	KEY_ERROR	  0x01
243 
244 #define	KEY_PRESS	  0
245 #define	KEY_RELEASE	  0x400
246 #define	KEY_INDEX(c)	  ((c) & 0xFF)
247 
248 #define	SCAN_PRESS	  0
249 #define	SCAN_RELEASE	  0x80
250 #define	SCAN_PREFIX_E0	  0x100
251 #define	SCAN_PREFIX_E1	  0x200
252 #define	SCAN_PREFIX_CTL	  0x400
253 #define	SCAN_PREFIX_SHIFT 0x800
254 #define	SCAN_PREFIX	(SCAN_PREFIX_E0  | SCAN_PREFIX_E1 | \
255 			 SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
256 #define	SCAN_CHAR(c)	((c) & 0x7f)
257 
258 #define	UKBD_LOCK()	USB_MTX_LOCK(&Giant)
259 #define	UKBD_UNLOCK()	USB_MTX_UNLOCK(&Giant)
260 #define	UKBD_LOCK_ASSERT()	USB_MTX_ASSERT(&Giant, MA_OWNED)
261 
262 struct ukbd_mods {
263 	uint32_t mask, key;
264 };
265 
266 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
267 	{MOD_CONTROL_L, 0xe0},
268 	{MOD_CONTROL_R, 0xe4},
269 	{MOD_SHIFT_L, 0xe1},
270 	{MOD_SHIFT_R, 0xe5},
271 	{MOD_ALT_L, 0xe2},
272 	{MOD_ALT_R, 0xe6},
273 	{MOD_WIN_L, 0xe3},
274 	{MOD_WIN_R, 0xe7},
275 };
276 
277 #define	NN 0				/* no translation */
278 /*
279  * Translate USB keycodes to AT keyboard scancodes.
280  */
281 /*
282  * FIXME: Mac USB keyboard generates:
283  * 0x53: keypad NumLock/Clear
284  * 0x66: Power
285  * 0x67: keypad =
286  * 0x68: F13
287  * 0x69: F14
288  * 0x6a: F15
289  *
290  * USB Apple Keyboard JIS generates:
291  * 0x90: Kana
292  * 0x91: Eisu
293  */
294 static const uint8_t ukbd_trtab[256] = {
295 	0, 0, 0, 0, 30, 48, 46, 32,	/* 00 - 07 */
296 	18, 33, 34, 35, 23, 36, 37, 38,	/* 08 - 0F */
297 	50, 49, 24, 25, 16, 19, 31, 20,	/* 10 - 17 */
298 	22, 47, 17, 45, 21, 44, 2, 3,	/* 18 - 1F */
299 	4, 5, 6, 7, 8, 9, 10, 11,	/* 20 - 27 */
300 	28, 1, 14, 15, 57, 12, 13, 26,	/* 28 - 2F */
301 	27, 43, 43, 39, 40, 41, 51, 52,	/* 30 - 37 */
302 	53, 58, 59, 60, 61, 62, 63, 64,	/* 38 - 3F */
303 	65, 66, 67, 68, 87, 88, 92, 70,	/* 40 - 47 */
304 	104, 102, 94, 96, 103, 99, 101, 98,	/* 48 - 4F */
305 	97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
306 	89, 79, 80, 81, 75, 76, 77, 71,	/* 58 - 5F */
307 	72, 73, 82, 83, 86, 107, 122, NN,	/* 60 - 67 */
308 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 68 - 6F */
309 	NN, NN, NN, NN, 115, 108, 111, 113,	/* 70 - 77 */
310 	109, 110, 112, 118, 114, 116, 117, 119,	/* 78 - 7F */
311 	121, 120, NN, NN, NN, NN, NN, 123,	/* 80 - 87 */
312 	124, 125, 126, 127, 128, NN, NN, NN,	/* 88 - 8F */
313 	129, 130, NN, NN, NN, NN, NN, NN,	/* 90 - 97 */
314 	NN, NN, NN, NN, NN, NN, NN, NN,	/* 98 - 9F */
315 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A0 - A7 */
316 	NN, NN, NN, NN, NN, NN, NN, NN,	/* A8 - AF */
317 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B0 - B7 */
318 	NN, NN, NN, NN, NN, NN, NN, NN,	/* B8 - BF */
319 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C0 - C7 */
320 	NN, NN, NN, NN, NN, NN, NN, NN,	/* C8 - CF */
321 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D0 - D7 */
322 	NN, NN, NN, NN, NN, NN, NN, NN,	/* D8 - DF */
323 	29, 42, 56, 105, 90, 54, 93, 106,	/* E0 - E7 */
324 	NN, NN, NN, NN, NN, NN, NN, NN,	/* E8 - EF */
325 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F0 - F7 */
326 	NN, NN, NN, NN, NN, NN, NN, NN,	/* F8 - FF */
327 };
328 
329 static const uint8_t ukbd_boot_desc[] = {
330 	0x05, 0x01, 0x09, 0x06, 0xa1,
331 	0x01, 0x05, 0x07, 0x19, 0xe0,
332 	0x29, 0xe7, 0x15, 0x00, 0x25,
333 	0x01, 0x75, 0x01, 0x95, 0x08,
334 	0x81, 0x02, 0x95, 0x01, 0x75,
335 	0x08, 0x81, 0x01, 0x95, 0x03,
336 	0x75, 0x01, 0x05, 0x08, 0x19,
337 	0x01, 0x29, 0x03, 0x91, 0x02,
338 	0x95, 0x05, 0x75, 0x01, 0x91,
339 	0x01, 0x95, 0x06, 0x75, 0x08,
340 	0x15, 0x00, 0x26, 0xff, 0x00,
341 	0x05, 0x07, 0x19, 0x00, 0x2a,
342 	0xff, 0x00, 0x81, 0x00, 0xc0
343 };
344 
345 /* prototypes */
346 static void	ukbd_timeout(void *);
347 static void	ukbd_set_leds(struct ukbd_softc *, uint8_t);
348 static int	ukbd_set_typematic(keyboard_t *, int);
349 #ifdef UKBD_EMULATE_ATSCANCODE
350 static uint32_t	ukbd_atkeycode(int, int);
351 static int	ukbd_key2scan(struct ukbd_softc *, int, int, int);
352 #endif
353 static uint32_t	ukbd_read_char(keyboard_t *, int);
354 static void	ukbd_clear_state(keyboard_t *);
355 static int	ukbd_ioctl(keyboard_t *, u_long, caddr_t);
356 static int	ukbd_enable(keyboard_t *);
357 static int	ukbd_disable(keyboard_t *);
358 static void	ukbd_interrupt(struct ukbd_softc *);
359 static void	ukbd_event_keyinput(struct ukbd_softc *);
360 
361 static device_probe_t ukbd_probe;
362 static device_attach_t ukbd_attach;
363 static device_detach_t ukbd_detach;
364 static device_resume_t ukbd_resume;
365 
366 #ifdef EVDEV_SUPPORT
367 static evdev_event_t ukbd_ev_event;
368 
369 static const struct evdev_methods ukbd_evdev_methods = {
370 	.ev_event = ukbd_ev_event,
371 };
372 #endif
373 
374 static uint8_t
375 ukbd_any_key_pressed(struct ukbd_softc *sc)
376 {
377 	uint8_t i;
378 	uint8_t j;
379 
380 	for (j = i = 0; i < UKBD_NKEYCODE; i++)
381 		j |= sc->sc_odata.keycode[i];
382 
383 	return (j ? 1 : 0);
384 }
385 
386 static void
387 ukbd_start_timer(struct ukbd_softc *sc)
388 {
389 	sbintime_t delay, now, prec;
390 
391 	now = sbinuptime();
392 
393 	/* check if initial delay passed and fallback to key repeat delay */
394 	if (sc->sc_delay == 0)
395 		sc->sc_delay = sc->sc_kbd.kb_delay2;
396 
397 	/* compute timeout */
398 	delay = SBT_1MS * sc->sc_delay;
399 	sc->sc_co_basetime += delay;
400 
401 	/* check if we are running behind */
402 	if (sc->sc_co_basetime < now)
403 		sc->sc_co_basetime = now;
404 
405 	/* This is rarely called, so prefer precision to efficiency. */
406 	prec = qmin(delay >> 7, SBT_1MS * 10);
407 	usb_callout_reset_sbt(&sc->sc_callout, sc->sc_co_basetime, prec,
408 	    ukbd_timeout, sc, C_ABSOLUTE);
409 }
410 
411 static void
412 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
413 {
414 
415 	UKBD_LOCK_ASSERT();
416 
417 	DPRINTF("0x%02x (%d) %s\n", key, key,
418 	    (key & KEY_RELEASE) ? "released" : "pressed");
419 
420 #ifdef EVDEV_SUPPORT
421 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL) {
422 		evdev_push_event(sc->sc_evdev, EV_KEY,
423 		    evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE));
424 		evdev_sync(sc->sc_evdev);
425 	}
426 #endif
427 
428 	if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
429 		sc->sc_input[sc->sc_inputtail] = key;
430 		++(sc->sc_inputs);
431 		++(sc->sc_inputtail);
432 		if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
433 			sc->sc_inputtail = 0;
434 		}
435 	} else {
436 		DPRINTF("input buffer is full\n");
437 	}
438 }
439 
440 static void
441 ukbd_do_poll(struct ukbd_softc *sc, uint8_t wait)
442 {
443 
444 	UKBD_LOCK_ASSERT();
445 	KASSERT((sc->sc_flags & UKBD_FLAG_POLLING) != 0,
446 	    ("ukbd_do_poll called when not polling\n"));
447 	DPRINTFN(2, "polling\n");
448 
449 	if (USB_IN_POLLING_MODE_FUNC() == 0) {
450 		/*
451 		 * In this context the kernel is polling for input,
452 		 * but the USB subsystem works in normal interrupt-driven
453 		 * mode, so we just wait on the USB threads to do the job.
454 		 * Note that we currently hold the Giant, but it's also used
455 		 * as the transfer mtx, so we must release it while waiting.
456 		 */
457 		while (sc->sc_inputs == 0) {
458 			/*
459 			 * Give USB threads a chance to run.  Note that
460 			 * kern_yield performs DROP_GIANT + PICKUP_GIANT.
461 			 */
462 			kern_yield(PRI_UNCHANGED);
463 			if (!wait)
464 				break;
465 		}
466 		return;
467 	}
468 
469 	while (sc->sc_inputs == 0) {
470 
471 		usbd_transfer_poll(sc->sc_xfer, UKBD_N_TRANSFER);
472 
473 		/* Delay-optimised support for repetition of keys */
474 		if (ukbd_any_key_pressed(sc)) {
475 			/* a key is pressed - need timekeeping */
476 			DELAY(1000);
477 
478 			/* 1 millisecond has passed */
479 			sc->sc_time_ms += 1;
480 		}
481 
482 		ukbd_interrupt(sc);
483 
484 		if (!wait)
485 			break;
486 	}
487 }
488 
489 static int32_t
490 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
491 {
492 	int32_t c;
493 
494 	UKBD_LOCK_ASSERT();
495 	KASSERT((USB_IN_POLLING_MODE_FUNC() == 0) ||
496 	    (sc->sc_flags & UKBD_FLAG_POLLING) != 0,
497 	    ("not polling in kdb or panic\n"));
498 
499 	if (sc->sc_inputs == 0 &&
500 	    (sc->sc_flags & UKBD_FLAG_GONE) == 0) {
501 		/* start transfer, if not already started */
502 		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
503 		usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
504 	}
505 
506 	if (sc->sc_flags & UKBD_FLAG_POLLING)
507 		ukbd_do_poll(sc, wait);
508 
509 	if (sc->sc_inputs == 0) {
510 		c = -1;
511 	} else {
512 		c = sc->sc_input[sc->sc_inputhead];
513 		--(sc->sc_inputs);
514 		++(sc->sc_inputhead);
515 		if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
516 			sc->sc_inputhead = 0;
517 		}
518 	}
519 	return (c);
520 }
521 
522 static void
523 ukbd_interrupt(struct ukbd_softc *sc)
524 {
525 	uint32_t n_mod;
526 	uint32_t o_mod;
527 	uint32_t now = sc->sc_time_ms;
528 	int32_t dtime;
529 	uint8_t key;
530 	uint8_t i;
531 	uint8_t j;
532 
533 	UKBD_LOCK_ASSERT();
534 
535 	if (sc->sc_ndata.keycode[0] == KEY_ERROR)
536 		return;
537 
538 	n_mod = sc->sc_ndata.modifiers;
539 	o_mod = sc->sc_odata.modifiers;
540 	if (n_mod != o_mod) {
541 		for (i = 0; i < UKBD_NMOD; i++) {
542 			if ((n_mod & ukbd_mods[i].mask) !=
543 			    (o_mod & ukbd_mods[i].mask)) {
544 				ukbd_put_key(sc, ukbd_mods[i].key |
545 				    ((n_mod & ukbd_mods[i].mask) ?
546 				    KEY_PRESS : KEY_RELEASE));
547 			}
548 		}
549 	}
550 	/* Check for released keys. */
551 	for (i = 0; i < UKBD_NKEYCODE; i++) {
552 		key = sc->sc_odata.keycode[i];
553 		if (key == 0) {
554 			continue;
555 		}
556 		for (j = 0; j < UKBD_NKEYCODE; j++) {
557 			if (sc->sc_ndata.keycode[j] == 0) {
558 				continue;
559 			}
560 			if (key == sc->sc_ndata.keycode[j]) {
561 				goto rfound;
562 			}
563 		}
564 		ukbd_put_key(sc, key | KEY_RELEASE);
565 rfound:	;
566 	}
567 
568 	/* Check for pressed keys. */
569 	for (i = 0; i < UKBD_NKEYCODE; i++) {
570 		key = sc->sc_ndata.keycode[i];
571 		if (key == 0) {
572 			continue;
573 		}
574 		sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
575 		for (j = 0; j < UKBD_NKEYCODE; j++) {
576 			if (sc->sc_odata.keycode[j] == 0) {
577 				continue;
578 			}
579 			if (key == sc->sc_odata.keycode[j]) {
580 
581 				/* key is still pressed */
582 
583 				sc->sc_ntime[i] = sc->sc_otime[j];
584 				dtime = (sc->sc_otime[j] - now);
585 
586 				if (dtime > 0) {
587 					/* time has not elapsed */
588 					goto pfound;
589 				}
590 				sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
591 				break;
592 			}
593 		}
594 		if (j == UKBD_NKEYCODE) {
595 			/* New key - set initial delay and [re]start timer */
596 			sc->sc_co_basetime = sbinuptime();
597 			sc->sc_delay = sc->sc_kbd.kb_delay1;
598 			ukbd_start_timer(sc);
599 		}
600 		ukbd_put_key(sc, key | KEY_PRESS);
601 
602 		/*
603                  * If any other key is presently down, force its repeat to be
604                  * well in the future (100s).  This makes the last key to be
605                  * pressed do the autorepeat.
606                  */
607 		for (j = 0; j != UKBD_NKEYCODE; j++) {
608 			if (j != i)
609 				sc->sc_ntime[j] = now + (100 * 1000);
610 		}
611 pfound:	;
612 	}
613 
614 	sc->sc_odata = sc->sc_ndata;
615 
616 	memcpy(sc->sc_otime, sc->sc_ntime, sizeof(sc->sc_otime));
617 
618 	ukbd_event_keyinput(sc);
619 }
620 
621 static void
622 ukbd_event_keyinput(struct ukbd_softc *sc)
623 {
624 	int c;
625 
626 	UKBD_LOCK_ASSERT();
627 
628 	if ((sc->sc_flags & UKBD_FLAG_POLLING) != 0)
629 		return;
630 
631 	if (sc->sc_inputs == 0)
632 		return;
633 
634 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
635 	    KBD_IS_BUSY(&sc->sc_kbd)) {
636 		/* let the callback function process the input */
637 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
638 		    sc->sc_kbd.kb_callback.kc_arg);
639 	} else {
640 		/* read and discard the input, no one is waiting for it */
641 		do {
642 			c = ukbd_read_char(&sc->sc_kbd, 0);
643 		} while (c != NOKEY);
644 	}
645 }
646 
647 static void
648 ukbd_timeout(void *arg)
649 {
650 	struct ukbd_softc *sc = arg;
651 
652 	UKBD_LOCK_ASSERT();
653 
654 	sc->sc_time_ms += sc->sc_delay;
655 	sc->sc_delay = 0;
656 
657 	ukbd_interrupt(sc);
658 
659 	/* Make sure any leftover key events gets read out */
660 	ukbd_event_keyinput(sc);
661 
662 	if (ukbd_any_key_pressed(sc) || (sc->sc_inputs != 0)) {
663 		ukbd_start_timer(sc);
664 	}
665 }
666 
667 static uint8_t
668 ukbd_apple_fn(uint8_t keycode) {
669 	switch (keycode) {
670 	case 0x28: return 0x49; /* RETURN -> INSERT */
671 	case 0x2a: return 0x4c; /* BACKSPACE -> DEL */
672 	case 0x50: return 0x4a; /* LEFT ARROW -> HOME */
673 	case 0x4f: return 0x4d; /* RIGHT ARROW -> END */
674 	case 0x52: return 0x4b; /* UP ARROW -> PGUP */
675 	case 0x51: return 0x4e; /* DOWN ARROW -> PGDN */
676 	default: return keycode;
677 	}
678 }
679 
680 static uint8_t
681 ukbd_apple_swap(uint8_t keycode) {
682 	switch (keycode) {
683 	case 0x35: return 0x64;
684 	case 0x64: return 0x35;
685 	default: return keycode;
686 	}
687 }
688 
689 static void
690 ukbd_intr_callback(struct usb_xfer *xfer, usb_error_t error)
691 {
692 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
693 	struct usb_page_cache *pc;
694 	uint8_t i;
695 	uint8_t offset;
696 	uint8_t id;
697 	int len;
698 
699 	UKBD_LOCK_ASSERT();
700 
701 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
702 	pc = usbd_xfer_get_frame(xfer, 0);
703 
704 	switch (USB_GET_STATE(xfer)) {
705 	case USB_ST_TRANSFERRED:
706 		DPRINTF("actlen=%d bytes\n", len);
707 
708 		if (len == 0) {
709 			DPRINTF("zero length data\n");
710 			goto tr_setup;
711 		}
712 
713 		if (sc->sc_kbd_id != 0) {
714 			/* check and remove HID ID byte */
715 			usbd_copy_out(pc, 0, &id, 1);
716 			offset = 1;
717 			len--;
718 			if (len == 0) {
719 				DPRINTF("zero length data\n");
720 				goto tr_setup;
721 			}
722 		} else {
723 			offset = 0;
724 			id = 0;
725 		}
726 
727 		if (len > UKBD_BUFFER_SIZE)
728 			len = UKBD_BUFFER_SIZE;
729 
730 		/* get data */
731 		usbd_copy_out(pc, offset, sc->sc_buffer, len);
732 
733 		/* clear temporary storage */
734 		memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
735 
736 		/* scan through HID data */
737 		if ((sc->sc_flags & UKBD_FLAG_APPLE_EJECT) &&
738 		    (id == sc->sc_id_apple_eject)) {
739 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_eject))
740 				sc->sc_modifiers |= MOD_EJECT;
741 			else
742 				sc->sc_modifiers &= ~MOD_EJECT;
743 		}
744 		if ((sc->sc_flags & UKBD_FLAG_APPLE_FN) &&
745 		    (id == sc->sc_id_apple_fn)) {
746 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_apple_fn))
747 				sc->sc_modifiers |= MOD_FN;
748 			else
749 				sc->sc_modifiers &= ~MOD_FN;
750 		}
751 		if ((sc->sc_flags & UKBD_FLAG_CTRL_L) &&
752 		    (id == sc->sc_id_ctrl_l)) {
753 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_l))
754 			  sc->	sc_modifiers |= MOD_CONTROL_L;
755 			else
756 			  sc->	sc_modifiers &= ~MOD_CONTROL_L;
757 		}
758 		if ((sc->sc_flags & UKBD_FLAG_CTRL_R) &&
759 		    (id == sc->sc_id_ctrl_r)) {
760 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_ctrl_r))
761 				sc->sc_modifiers |= MOD_CONTROL_R;
762 			else
763 				sc->sc_modifiers &= ~MOD_CONTROL_R;
764 		}
765 		if ((sc->sc_flags & UKBD_FLAG_SHIFT_L) &&
766 		    (id == sc->sc_id_shift_l)) {
767 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_l))
768 				sc->sc_modifiers |= MOD_SHIFT_L;
769 			else
770 				sc->sc_modifiers &= ~MOD_SHIFT_L;
771 		}
772 		if ((sc->sc_flags & UKBD_FLAG_SHIFT_R) &&
773 		    (id == sc->sc_id_shift_r)) {
774 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_shift_r))
775 				sc->sc_modifiers |= MOD_SHIFT_R;
776 			else
777 				sc->sc_modifiers &= ~MOD_SHIFT_R;
778 		}
779 		if ((sc->sc_flags & UKBD_FLAG_ALT_L) &&
780 		    (id == sc->sc_id_alt_l)) {
781 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_l))
782 				sc->sc_modifiers |= MOD_ALT_L;
783 			else
784 				sc->sc_modifiers &= ~MOD_ALT_L;
785 		}
786 		if ((sc->sc_flags & UKBD_FLAG_ALT_R) &&
787 		    (id == sc->sc_id_alt_r)) {
788 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_alt_r))
789 				sc->sc_modifiers |= MOD_ALT_R;
790 			else
791 				sc->sc_modifiers &= ~MOD_ALT_R;
792 		}
793 		if ((sc->sc_flags & UKBD_FLAG_WIN_L) &&
794 		    (id == sc->sc_id_win_l)) {
795 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_l))
796 				sc->sc_modifiers |= MOD_WIN_L;
797 			else
798 				sc->sc_modifiers &= ~MOD_WIN_L;
799 		}
800 		if ((sc->sc_flags & UKBD_FLAG_WIN_R) &&
801 		    (id == sc->sc_id_win_r)) {
802 			if (hid_get_data(sc->sc_buffer, len, &sc->sc_loc_win_r))
803 				sc->sc_modifiers |= MOD_WIN_R;
804 			else
805 				sc->sc_modifiers &= ~MOD_WIN_R;
806 		}
807 
808 		sc->sc_ndata.modifiers = sc->sc_modifiers;
809 
810 		if ((sc->sc_flags & UKBD_FLAG_EVENTS) &&
811 		    (id == sc->sc_id_events)) {
812 			i = sc->sc_loc_events.count;
813 			if (i > UKBD_NKEYCODE)
814 				i = UKBD_NKEYCODE;
815 			if (i > len)
816 				i = len;
817 			while (i--) {
818 				sc->sc_ndata.keycode[i] =
819 				    hid_get_data(sc->sc_buffer + i, len - i,
820 				    &sc->sc_loc_events);
821 			}
822 		}
823 
824 #ifdef USB_DEBUG
825 		DPRINTF("modifiers = 0x%04x\n", (int)sc->sc_modifiers);
826 		for (i = 0; i < UKBD_NKEYCODE; i++) {
827 			if (sc->sc_ndata.keycode[i]) {
828 				DPRINTF("[%d] = 0x%02x\n",
829 				    (int)i, (int)sc->sc_ndata.keycode[i]);
830 			}
831 		}
832 #endif
833 		if (sc->sc_modifiers & MOD_FN) {
834 			for (i = 0; i < UKBD_NKEYCODE; i++) {
835 				sc->sc_ndata.keycode[i] =
836 				    ukbd_apple_fn(sc->sc_ndata.keycode[i]);
837 			}
838 		}
839 
840 		if (sc->sc_flags & UKBD_FLAG_APPLE_SWAP) {
841 			for (i = 0; i < UKBD_NKEYCODE; i++) {
842 				sc->sc_ndata.keycode[i] =
843 				    ukbd_apple_swap(sc->sc_ndata.keycode[i]);
844 			}
845 		}
846 
847 		ukbd_interrupt(sc);
848 
849 	case USB_ST_SETUP:
850 tr_setup:
851 		if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
852 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
853 			usbd_transfer_submit(xfer);
854 		} else {
855 			DPRINTF("input queue is full!\n");
856 		}
857 		break;
858 
859 	default:			/* Error */
860 		DPRINTF("error=%s\n", usbd_errstr(error));
861 
862 		if (error != USB_ERR_CANCELLED) {
863 			/* try to clear stall first */
864 			usbd_xfer_set_stall(xfer);
865 			goto tr_setup;
866 		}
867 		break;
868 	}
869 }
870 
871 static void
872 ukbd_set_leds_callback(struct usb_xfer *xfer, usb_error_t error)
873 {
874 	struct ukbd_softc *sc = usbd_xfer_softc(xfer);
875 	struct usb_device_request req;
876 	struct usb_page_cache *pc;
877 	uint8_t id;
878 	uint8_t any;
879 	int len;
880 
881 	UKBD_LOCK_ASSERT();
882 
883 #ifdef USB_DEBUG
884 	if (ukbd_no_leds)
885 		return;
886 #endif
887 
888 	switch (USB_GET_STATE(xfer)) {
889 	case USB_ST_TRANSFERRED:
890 	case USB_ST_SETUP:
891 		if (!(sc->sc_flags & UKBD_FLAG_SET_LEDS))
892 			break;
893 		sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
894 
895 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
896 		req.bRequest = UR_SET_REPORT;
897 		USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
898 		req.wIndex[0] = sc->sc_iface_no;
899 		req.wIndex[1] = 0;
900 		req.wLength[1] = 0;
901 
902 		memset(sc->sc_buffer, 0, UKBD_BUFFER_SIZE);
903 
904 		id = 0;
905 		any = 0;
906 
907 		/* Assumption: All led bits must be in the same ID. */
908 
909 		if (sc->sc_flags & UKBD_FLAG_NUMLOCK) {
910 			if (sc->sc_leds & NLKED) {
911 				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
912 				    &sc->sc_loc_numlock, 1);
913 			}
914 			id = sc->sc_id_numlock;
915 			any = 1;
916 		}
917 
918 		if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK) {
919 			if (sc->sc_leds & SLKED) {
920 				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
921 				    &sc->sc_loc_scrolllock, 1);
922 			}
923 			id = sc->sc_id_scrolllock;
924 			any = 1;
925 		}
926 
927 		if (sc->sc_flags & UKBD_FLAG_CAPSLOCK) {
928 			if (sc->sc_leds & CLKED) {
929 				hid_put_data_unsigned(sc->sc_buffer + 1, UKBD_BUFFER_SIZE - 1,
930 				    &sc->sc_loc_capslock, 1);
931 			}
932 			id = sc->sc_id_capslock;
933 			any = 1;
934 		}
935 
936 		/* if no leds, nothing to do */
937 		if (!any)
938 			break;
939 
940 #ifdef EVDEV_SUPPORT
941 		if (sc->sc_evdev != NULL)
942 			evdev_push_leds(sc->sc_evdev, sc->sc_leds);
943 #endif
944 
945 		/* range check output report length */
946 		len = sc->sc_led_size;
947 		if (len > (UKBD_BUFFER_SIZE - 1))
948 			len = (UKBD_BUFFER_SIZE - 1);
949 
950 		/* check if we need to prefix an ID byte */
951 		sc->sc_buffer[0] = id;
952 
953 		pc = usbd_xfer_get_frame(xfer, 1);
954 		if (id != 0) {
955 			len++;
956 			usbd_copy_in(pc, 0, sc->sc_buffer, len);
957 		} else {
958 			usbd_copy_in(pc, 0, sc->sc_buffer + 1, len);
959 		}
960 		req.wLength[0] = len;
961 		usbd_xfer_set_frame_len(xfer, 1, len);
962 
963 		DPRINTF("len=%d, id=%d\n", len, id);
964 
965 		/* setup control request last */
966 		pc = usbd_xfer_get_frame(xfer, 0);
967 		usbd_copy_in(pc, 0, &req, sizeof(req));
968 		usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
969 
970 		/* start data transfer */
971 		usbd_xfer_set_frames(xfer, 2);
972 		usbd_transfer_submit(xfer);
973 		break;
974 
975 	default:			/* Error */
976 		DPRINTFN(1, "error=%s\n", usbd_errstr(error));
977 		break;
978 	}
979 }
980 
981 static const struct usb_config ukbd_config[UKBD_N_TRANSFER] = {
982 
983 	[UKBD_INTR_DT_0] = {
984 		.type = UE_INTERRUPT,
985 		.endpoint = UE_ADDR_ANY,
986 		.direction = UE_DIR_IN,
987 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
988 		.bufsize = 0,	/* use wMaxPacketSize */
989 		.callback = &ukbd_intr_callback,
990 	},
991 
992 	[UKBD_INTR_DT_1] = {
993 		.type = UE_INTERRUPT,
994 		.endpoint = UE_ADDR_ANY,
995 		.direction = UE_DIR_IN,
996 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
997 		.bufsize = 0,	/* use wMaxPacketSize */
998 		.callback = &ukbd_intr_callback,
999 	},
1000 
1001 	[UKBD_CTRL_LED] = {
1002 		.type = UE_CONTROL,
1003 		.endpoint = 0x00,	/* Control pipe */
1004 		.direction = UE_DIR_ANY,
1005 		.bufsize = sizeof(struct usb_device_request) + UKBD_BUFFER_SIZE,
1006 		.callback = &ukbd_set_leds_callback,
1007 		.timeout = 1000,	/* 1 second */
1008 	},
1009 };
1010 
1011 /* A match on these entries will load ukbd */
1012 static const STRUCT_USB_HOST_ID __used ukbd_devs[] = {
1013 	{USB_IFACE_CLASS(UICLASS_HID),
1014 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
1015 	 USB_IFACE_PROTOCOL(UIPROTO_BOOT_KEYBOARD),},
1016 };
1017 
1018 static int
1019 ukbd_probe(device_t dev)
1020 {
1021 	keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
1022 	struct usb_attach_arg *uaa = device_get_ivars(dev);
1023 	void *d_ptr;
1024 	int error;
1025 	uint16_t d_len;
1026 
1027 	UKBD_LOCK_ASSERT();
1028 	DPRINTFN(11, "\n");
1029 
1030 	if (sw == NULL) {
1031 		return (ENXIO);
1032 	}
1033 	if (uaa->usb_mode != USB_MODE_HOST) {
1034 		return (ENXIO);
1035 	}
1036 
1037 	if (uaa->info.bInterfaceClass != UICLASS_HID)
1038 		return (ENXIO);
1039 
1040 	if (usb_test_quirk(uaa, UQ_KBD_IGNORE))
1041 		return (ENXIO);
1042 
1043 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
1044 	    (uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD))
1045 		return (BUS_PROBE_DEFAULT);
1046 
1047 	error = usbd_req_get_hid_desc(uaa->device, NULL,
1048 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
1049 
1050 	if (error)
1051 		return (ENXIO);
1052 
1053 	if (hid_is_keyboard(d_ptr, d_len)) {
1054 		if (hid_is_mouse(d_ptr, d_len)) {
1055 			/*
1056 			 * NOTE: We currently don't support USB mouse
1057 			 * and USB keyboard on the same USB endpoint.
1058 			 * Let "ums" driver win.
1059 			 */
1060 			error = ENXIO;
1061 		} else {
1062 			error = BUS_PROBE_DEFAULT;
1063 		}
1064 	} else {
1065 		error = ENXIO;
1066 	}
1067 	free(d_ptr, M_TEMP);
1068 	return (error);
1069 }
1070 
1071 static void
1072 ukbd_parse_hid(struct ukbd_softc *sc, const uint8_t *ptr, uint32_t len)
1073 {
1074 	uint32_t flags;
1075 
1076 	/* reset detected bits */
1077 	sc->sc_flags &= ~UKBD_FLAG_HID_MASK;
1078 
1079 	/* check if there is an ID byte */
1080 	sc->sc_kbd_size = hid_report_size(ptr, len,
1081 	    hid_input, &sc->sc_kbd_id);
1082 
1083 	/* investigate if this is an Apple Keyboard */
1084 	if (hid_locate(ptr, len,
1085 	    HID_USAGE2(HUP_CONSUMER, HUG_APPLE_EJECT),
1086 	    hid_input, 0, &sc->sc_loc_apple_eject, &flags,
1087 	    &sc->sc_id_apple_eject)) {
1088 		if (flags & HIO_VARIABLE)
1089 			sc->sc_flags |= UKBD_FLAG_APPLE_EJECT |
1090 			    UKBD_FLAG_APPLE_SWAP;
1091 		DPRINTFN(1, "Found Apple eject-key\n");
1092 	}
1093 	if (hid_locate(ptr, len,
1094 	    HID_USAGE2(0xFFFF, 0x0003),
1095 	    hid_input, 0, &sc->sc_loc_apple_fn, &flags,
1096 	    &sc->sc_id_apple_fn)) {
1097 		if (flags & HIO_VARIABLE)
1098 			sc->sc_flags |= UKBD_FLAG_APPLE_FN;
1099 		DPRINTFN(1, "Found Apple FN-key\n");
1100 	}
1101 	/* figure out some keys */
1102 	if (hid_locate(ptr, len,
1103 	    HID_USAGE2(HUP_KEYBOARD, 0xE0),
1104 	    hid_input, 0, &sc->sc_loc_ctrl_l, &flags,
1105 	    &sc->sc_id_ctrl_l)) {
1106 		if (flags & HIO_VARIABLE)
1107 			sc->sc_flags |= UKBD_FLAG_CTRL_L;
1108 		DPRINTFN(1, "Found left control\n");
1109 	}
1110 	if (hid_locate(ptr, len,
1111 	    HID_USAGE2(HUP_KEYBOARD, 0xE4),
1112 	    hid_input, 0, &sc->sc_loc_ctrl_r, &flags,
1113 	    &sc->sc_id_ctrl_r)) {
1114 		if (flags & HIO_VARIABLE)
1115 			sc->sc_flags |= UKBD_FLAG_CTRL_R;
1116 		DPRINTFN(1, "Found right control\n");
1117 	}
1118 	if (hid_locate(ptr, len,
1119 	    HID_USAGE2(HUP_KEYBOARD, 0xE1),
1120 	    hid_input, 0, &sc->sc_loc_shift_l, &flags,
1121 	    &sc->sc_id_shift_l)) {
1122 		if (flags & HIO_VARIABLE)
1123 			sc->sc_flags |= UKBD_FLAG_SHIFT_L;
1124 		DPRINTFN(1, "Found left shift\n");
1125 	}
1126 	if (hid_locate(ptr, len,
1127 	    HID_USAGE2(HUP_KEYBOARD, 0xE5),
1128 	    hid_input, 0, &sc->sc_loc_shift_r, &flags,
1129 	    &sc->sc_id_shift_r)) {
1130 		if (flags & HIO_VARIABLE)
1131 			sc->sc_flags |= UKBD_FLAG_SHIFT_R;
1132 		DPRINTFN(1, "Found right shift\n");
1133 	}
1134 	if (hid_locate(ptr, len,
1135 	    HID_USAGE2(HUP_KEYBOARD, 0xE2),
1136 	    hid_input, 0, &sc->sc_loc_alt_l, &flags,
1137 	    &sc->sc_id_alt_l)) {
1138 		if (flags & HIO_VARIABLE)
1139 			sc->sc_flags |= UKBD_FLAG_ALT_L;
1140 		DPRINTFN(1, "Found left alt\n");
1141 	}
1142 	if (hid_locate(ptr, len,
1143 	    HID_USAGE2(HUP_KEYBOARD, 0xE6),
1144 	    hid_input, 0, &sc->sc_loc_alt_r, &flags,
1145 	    &sc->sc_id_alt_r)) {
1146 		if (flags & HIO_VARIABLE)
1147 			sc->sc_flags |= UKBD_FLAG_ALT_R;
1148 		DPRINTFN(1, "Found right alt\n");
1149 	}
1150 	if (hid_locate(ptr, len,
1151 	    HID_USAGE2(HUP_KEYBOARD, 0xE3),
1152 	    hid_input, 0, &sc->sc_loc_win_l, &flags,
1153 	    &sc->sc_id_win_l)) {
1154 		if (flags & HIO_VARIABLE)
1155 			sc->sc_flags |= UKBD_FLAG_WIN_L;
1156 		DPRINTFN(1, "Found left GUI\n");
1157 	}
1158 	if (hid_locate(ptr, len,
1159 	    HID_USAGE2(HUP_KEYBOARD, 0xE7),
1160 	    hid_input, 0, &sc->sc_loc_win_r, &flags,
1161 	    &sc->sc_id_win_r)) {
1162 		if (flags & HIO_VARIABLE)
1163 			sc->sc_flags |= UKBD_FLAG_WIN_R;
1164 		DPRINTFN(1, "Found right GUI\n");
1165 	}
1166 	/* figure out event buffer */
1167 	if (hid_locate(ptr, len,
1168 	    HID_USAGE2(HUP_KEYBOARD, 0x00),
1169 	    hid_input, 0, &sc->sc_loc_events, &flags,
1170 	    &sc->sc_id_events)) {
1171 		if (flags & HIO_VARIABLE) {
1172 			DPRINTFN(1, "Ignoring keyboard event control\n");
1173 		} else {
1174 			sc->sc_flags |= UKBD_FLAG_EVENTS;
1175 			DPRINTFN(1, "Found keyboard event array\n");
1176 		}
1177 	}
1178 
1179 	/* figure out leds on keyboard */
1180 	sc->sc_led_size = hid_report_size(ptr, len,
1181 	    hid_output, NULL);
1182 
1183 	if (hid_locate(ptr, len,
1184 	    HID_USAGE2(HUP_LEDS, 0x01),
1185 	    hid_output, 0, &sc->sc_loc_numlock, &flags,
1186 	    &sc->sc_id_numlock)) {
1187 		if (flags & HIO_VARIABLE)
1188 			sc->sc_flags |= UKBD_FLAG_NUMLOCK;
1189 		DPRINTFN(1, "Found keyboard numlock\n");
1190 	}
1191 	if (hid_locate(ptr, len,
1192 	    HID_USAGE2(HUP_LEDS, 0x02),
1193 	    hid_output, 0, &sc->sc_loc_capslock, &flags,
1194 	    &sc->sc_id_capslock)) {
1195 		if (flags & HIO_VARIABLE)
1196 			sc->sc_flags |= UKBD_FLAG_CAPSLOCK;
1197 		DPRINTFN(1, "Found keyboard capslock\n");
1198 	}
1199 	if (hid_locate(ptr, len,
1200 	    HID_USAGE2(HUP_LEDS, 0x03),
1201 	    hid_output, 0, &sc->sc_loc_scrolllock, &flags,
1202 	    &sc->sc_id_scrolllock)) {
1203 		if (flags & HIO_VARIABLE)
1204 			sc->sc_flags |= UKBD_FLAG_SCROLLLOCK;
1205 		DPRINTFN(1, "Found keyboard scrolllock\n");
1206 	}
1207 }
1208 
1209 static int
1210 ukbd_attach(device_t dev)
1211 {
1212 	struct ukbd_softc *sc = device_get_softc(dev);
1213 	struct usb_attach_arg *uaa = device_get_ivars(dev);
1214 	int unit = device_get_unit(dev);
1215 	keyboard_t *kbd = &sc->sc_kbd;
1216 	void *hid_ptr = NULL;
1217 	usb_error_t err;
1218 	uint16_t n;
1219 	uint16_t hid_len;
1220 #ifdef EVDEV_SUPPORT
1221 	struct evdev_dev *evdev;
1222 	int i;
1223 #endif
1224 #ifdef USB_DEBUG
1225 	int rate;
1226 #endif
1227 	UKBD_LOCK_ASSERT();
1228 
1229 	kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
1230 
1231 	kbd->kb_data = (void *)sc;
1232 
1233 	device_set_usb_desc(dev);
1234 
1235 	sc->sc_udev = uaa->device;
1236 	sc->sc_iface = uaa->iface;
1237 	sc->sc_iface_index = uaa->info.bIfaceIndex;
1238 	sc->sc_iface_no = uaa->info.bIfaceNum;
1239 	sc->sc_mode = K_XLATE;
1240 
1241 	usb_callout_init_mtx(&sc->sc_callout, &Giant, 0);
1242 
1243 #ifdef UKBD_NO_POLLING
1244 	err = usbd_transfer_setup(uaa->device,
1245 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
1246 	    UKBD_N_TRANSFER, sc, &Giant);
1247 #else
1248 	/*
1249 	 * Setup the UKBD USB transfers one by one, so they are memory
1250 	 * independent which allows for handling panics triggered by
1251 	 * the keyboard driver itself, typically via CTRL+ALT+ESC
1252 	 * sequences. Or if the USB keyboard driver was processing a
1253 	 * key at the moment of panic.
1254 	 */
1255 	for (n = 0; n != UKBD_N_TRANSFER; n++) {
1256 		err = usbd_transfer_setup(uaa->device,
1257 		    &uaa->info.bIfaceIndex, sc->sc_xfer + n, ukbd_config + n,
1258 		    1, sc, &Giant);
1259 		if (err)
1260 			break;
1261 	}
1262 #endif
1263 
1264 	if (err) {
1265 		DPRINTF("error=%s\n", usbd_errstr(err));
1266 		goto detach;
1267 	}
1268 	/* setup default keyboard maps */
1269 
1270 	sc->sc_keymap = key_map;
1271 	sc->sc_accmap = accent_map;
1272 	for (n = 0; n < UKBD_NFKEY; n++) {
1273 		sc->sc_fkeymap[n] = fkey_tab[n];
1274 	}
1275 
1276 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
1277 	    sc->sc_fkeymap, UKBD_NFKEY);
1278 
1279 	KBD_FOUND_DEVICE(kbd);
1280 
1281 	ukbd_clear_state(kbd);
1282 
1283 	/*
1284 	 * FIXME: set the initial value for lock keys in "sc_state"
1285 	 * according to the BIOS data?
1286 	 */
1287 	KBD_PROBE_DONE(kbd);
1288 
1289 	/* get HID descriptor */
1290 	err = usbd_req_get_hid_desc(uaa->device, NULL, &hid_ptr,
1291 	    &hid_len, M_TEMP, uaa->info.bIfaceIndex);
1292 
1293 	if (err == 0) {
1294 		DPRINTF("Parsing HID descriptor of %d bytes\n",
1295 		    (int)hid_len);
1296 
1297 		ukbd_parse_hid(sc, hid_ptr, hid_len);
1298 
1299 		free(hid_ptr, M_TEMP);
1300 	}
1301 
1302 	/* check if we should use the boot protocol */
1303 	if (usb_test_quirk(uaa, UQ_KBD_BOOTPROTO) ||
1304 	    (err != 0) || (!(sc->sc_flags & UKBD_FLAG_EVENTS))) {
1305 
1306 		DPRINTF("Forcing boot protocol\n");
1307 
1308 		err = usbd_req_set_protocol(sc->sc_udev, NULL,
1309 			sc->sc_iface_index, 0);
1310 
1311 		if (err != 0) {
1312 			DPRINTF("Set protocol error=%s (ignored)\n",
1313 			    usbd_errstr(err));
1314 		}
1315 
1316 		ukbd_parse_hid(sc, ukbd_boot_desc, sizeof(ukbd_boot_desc));
1317 	}
1318 
1319 	/* ignore if SETIDLE fails, hence it is not crucial */
1320 	usbd_req_set_idle(sc->sc_udev, NULL, sc->sc_iface_index, 0, 0);
1321 
1322 	ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
1323 
1324 	KBD_INIT_DONE(kbd);
1325 
1326 	if (kbd_register(kbd) < 0) {
1327 		goto detach;
1328 	}
1329 	KBD_CONFIG_DONE(kbd);
1330 
1331 	ukbd_enable(kbd);
1332 
1333 #ifdef KBD_INSTALL_CDEV
1334 	if (kbd_attach(kbd)) {
1335 		goto detach;
1336 	}
1337 #endif
1338 
1339 #ifdef EVDEV_SUPPORT
1340 	evdev = evdev_alloc();
1341 	evdev_set_name(evdev, device_get_desc(dev));
1342 	evdev_set_phys(evdev, device_get_nameunit(dev));
1343 	evdev_set_id(evdev, BUS_USB, uaa->info.idVendor,
1344 	   uaa->info.idProduct, 0);
1345 	evdev_set_serial(evdev, usb_get_serial(uaa->device));
1346 	evdev_set_methods(evdev, kbd, &ukbd_evdev_methods);
1347 	evdev_support_event(evdev, EV_SYN);
1348 	evdev_support_event(evdev, EV_KEY);
1349 	if (sc->sc_flags & (UKBD_FLAG_NUMLOCK | UKBD_FLAG_CAPSLOCK |
1350 			    UKBD_FLAG_SCROLLLOCK))
1351 		evdev_support_event(evdev, EV_LED);
1352 	evdev_support_event(evdev, EV_REP);
1353 
1354 	for (i = 0x00; i <= 0xFF; i++)
1355 		evdev_support_key(evdev, evdev_hid2key(i));
1356 	if (sc->sc_flags & UKBD_FLAG_NUMLOCK)
1357 		evdev_support_led(evdev, LED_NUML);
1358 	if (sc->sc_flags & UKBD_FLAG_CAPSLOCK)
1359 		evdev_support_led(evdev, LED_CAPSL);
1360 	if (sc->sc_flags & UKBD_FLAG_SCROLLLOCK)
1361 		evdev_support_led(evdev, LED_SCROLLL);
1362 
1363 	if (evdev_register_mtx(evdev, &Giant))
1364 		evdev_free(evdev);
1365 	else
1366 		sc->sc_evdev = evdev;
1367 #endif
1368 
1369 	sc->sc_flags |= UKBD_FLAG_ATTACHED;
1370 
1371 	if (bootverbose) {
1372 		kbdd_diag(kbd, bootverbose);
1373 	}
1374 
1375 #ifdef USB_DEBUG
1376 	/* check for polling rate override */
1377 	rate = ukbd_pollrate;
1378 	if (rate > 0) {
1379 		if (rate > 1000)
1380 			rate = 1;
1381 		else
1382 			rate = 1000 / rate;
1383 
1384 		/* set new polling interval in ms */
1385 		usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_0], rate);
1386 		usbd_xfer_set_interval(sc->sc_xfer[UKBD_INTR_DT_1], rate);
1387 	}
1388 #endif
1389 	/* start the keyboard */
1390 	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_0]);
1391 	usbd_transfer_start(sc->sc_xfer[UKBD_INTR_DT_1]);
1392 
1393 	return (0);			/* success */
1394 
1395 detach:
1396 	ukbd_detach(dev);
1397 	return (ENXIO);			/* error */
1398 }
1399 
1400 static int
1401 ukbd_detach(device_t dev)
1402 {
1403 	struct ukbd_softc *sc = device_get_softc(dev);
1404 	int error;
1405 
1406 	UKBD_LOCK_ASSERT();
1407 
1408 	DPRINTF("\n");
1409 
1410 	sc->sc_flags |= UKBD_FLAG_GONE;
1411 
1412 	usb_callout_stop(&sc->sc_callout);
1413 
1414 	/* kill any stuck keys */
1415 	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1416 		/* stop receiving events from the USB keyboard */
1417 		usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_0]);
1418 		usbd_transfer_stop(sc->sc_xfer[UKBD_INTR_DT_1]);
1419 
1420 		/* release all leftover keys, if any */
1421 		memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
1422 
1423 		/* process releasing of all keys */
1424 		ukbd_interrupt(sc);
1425 	}
1426 
1427 	ukbd_disable(&sc->sc_kbd);
1428 
1429 #ifdef KBD_INSTALL_CDEV
1430 	if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
1431 		error = kbd_detach(&sc->sc_kbd);
1432 		if (error) {
1433 			/* usb attach cannot return an error */
1434 			device_printf(dev, "WARNING: kbd_detach() "
1435 			    "returned non-zero! (ignored)\n");
1436 		}
1437 	}
1438 #endif
1439 
1440 #ifdef EVDEV_SUPPORT
1441 	evdev_free(sc->sc_evdev);
1442 #endif
1443 
1444 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
1445 		error = kbd_unregister(&sc->sc_kbd);
1446 		if (error) {
1447 			/* usb attach cannot return an error */
1448 			device_printf(dev, "WARNING: kbd_unregister() "
1449 			    "returned non-zero! (ignored)\n");
1450 		}
1451 	}
1452 	sc->sc_kbd.kb_flags = 0;
1453 
1454 	usbd_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
1455 
1456 	usb_callout_drain(&sc->sc_callout);
1457 
1458 	DPRINTF("%s: disconnected\n",
1459 	    device_get_nameunit(dev));
1460 
1461 	return (0);
1462 }
1463 
1464 static int
1465 ukbd_resume(device_t dev)
1466 {
1467 	struct ukbd_softc *sc = device_get_softc(dev);
1468 
1469 	UKBD_LOCK_ASSERT();
1470 
1471 	ukbd_clear_state(&sc->sc_kbd);
1472 
1473 	return (0);
1474 }
1475 
1476 #ifdef EVDEV_SUPPORT
1477 static void
1478 ukbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
1479     int32_t value)
1480 {
1481 	keyboard_t *kbd = evdev_get_softc(evdev);
1482 
1483 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
1484 	    (type == EV_LED || type == EV_REP)) {
1485 		mtx_lock(&Giant);
1486 		kbd_ev_event(kbd, type, code, value);
1487 		mtx_unlock(&Giant);
1488 	}
1489 }
1490 #endif
1491 
1492 /* early keyboard probe, not supported */
1493 static int
1494 ukbd_configure(int flags)
1495 {
1496 	return (0);
1497 }
1498 
1499 /* detect a keyboard, not used */
1500 static int
1501 ukbd__probe(int unit, void *arg, int flags)
1502 {
1503 	return (ENXIO);
1504 }
1505 
1506 /* reset and initialize the device, not used */
1507 static int
1508 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
1509 {
1510 	return (ENXIO);
1511 }
1512 
1513 /* test the interface to the device, not used */
1514 static int
1515 ukbd_test_if(keyboard_t *kbd)
1516 {
1517 	return (0);
1518 }
1519 
1520 /* finish using this keyboard, not used */
1521 static int
1522 ukbd_term(keyboard_t *kbd)
1523 {
1524 	return (ENXIO);
1525 }
1526 
1527 /* keyboard interrupt routine, not used */
1528 static int
1529 ukbd_intr(keyboard_t *kbd, void *arg)
1530 {
1531 	return (0);
1532 }
1533 
1534 /* lock the access to the keyboard, not used */
1535 static int
1536 ukbd_lock(keyboard_t *kbd, int lock)
1537 {
1538 	return (1);
1539 }
1540 
1541 /*
1542  * Enable the access to the device; until this function is called,
1543  * the client cannot read from the keyboard.
1544  */
1545 static int
1546 ukbd_enable(keyboard_t *kbd)
1547 {
1548 
1549 	UKBD_LOCK();
1550 	KBD_ACTIVATE(kbd);
1551 	UKBD_UNLOCK();
1552 
1553 	return (0);
1554 }
1555 
1556 /* disallow the access to the device */
1557 static int
1558 ukbd_disable(keyboard_t *kbd)
1559 {
1560 
1561 	UKBD_LOCK();
1562 	KBD_DEACTIVATE(kbd);
1563 	UKBD_UNLOCK();
1564 
1565 	return (0);
1566 }
1567 
1568 /* check if data is waiting */
1569 /* Currently unused. */
1570 static int
1571 ukbd_check(keyboard_t *kbd)
1572 {
1573 	struct ukbd_softc *sc = kbd->kb_data;
1574 
1575 	UKBD_LOCK_ASSERT();
1576 
1577 	if (!KBD_IS_ACTIVE(kbd))
1578 		return (0);
1579 
1580 	if (sc->sc_flags & UKBD_FLAG_POLLING)
1581 		ukbd_do_poll(sc, 0);
1582 
1583 #ifdef UKBD_EMULATE_ATSCANCODE
1584 	if (sc->sc_buffered_char[0]) {
1585 		return (1);
1586 	}
1587 #endif
1588 	if (sc->sc_inputs > 0) {
1589 		return (1);
1590 	}
1591 	return (0);
1592 }
1593 
1594 /* check if char is waiting */
1595 static int
1596 ukbd_check_char_locked(keyboard_t *kbd)
1597 {
1598 	struct ukbd_softc *sc = kbd->kb_data;
1599 
1600 	UKBD_LOCK_ASSERT();
1601 
1602 	if (!KBD_IS_ACTIVE(kbd))
1603 		return (0);
1604 
1605 	if ((sc->sc_composed_char > 0) &&
1606 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1607 		return (1);
1608 	}
1609 	return (ukbd_check(kbd));
1610 }
1611 
1612 static int
1613 ukbd_check_char(keyboard_t *kbd)
1614 {
1615 	int result;
1616 
1617 	UKBD_LOCK();
1618 	result = ukbd_check_char_locked(kbd);
1619 	UKBD_UNLOCK();
1620 
1621 	return (result);
1622 }
1623 
1624 /* read one byte from the keyboard if it's allowed */
1625 /* Currently unused. */
1626 static int
1627 ukbd_read(keyboard_t *kbd, int wait)
1628 {
1629 	struct ukbd_softc *sc = kbd->kb_data;
1630 	int32_t usbcode;
1631 #ifdef UKBD_EMULATE_ATSCANCODE
1632 	uint32_t keycode;
1633 	uint32_t scancode;
1634 
1635 #endif
1636 
1637 	UKBD_LOCK_ASSERT();
1638 
1639 	if (!KBD_IS_ACTIVE(kbd))
1640 		return (-1);
1641 
1642 #ifdef UKBD_EMULATE_ATSCANCODE
1643 	if (sc->sc_buffered_char[0]) {
1644 		scancode = sc->sc_buffered_char[0];
1645 		if (scancode & SCAN_PREFIX) {
1646 			sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
1647 			return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1648 		}
1649 		sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1650 		sc->sc_buffered_char[1] = 0;
1651 		return (scancode);
1652 	}
1653 #endif					/* UKBD_EMULATE_ATSCANCODE */
1654 
1655 	/* XXX */
1656 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1657 	if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1))
1658 		return (-1);
1659 
1660 	++(kbd->kb_count);
1661 
1662 #ifdef UKBD_EMULATE_ATSCANCODE
1663 	keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.modifiers);
1664 	if (keycode == NN) {
1665 		return -1;
1666 	}
1667 	return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1668 	    (usbcode & KEY_RELEASE)));
1669 #else					/* !UKBD_EMULATE_ATSCANCODE */
1670 	return (usbcode);
1671 #endif					/* UKBD_EMULATE_ATSCANCODE */
1672 }
1673 
1674 /* read char from the keyboard */
1675 static uint32_t
1676 ukbd_read_char_locked(keyboard_t *kbd, int wait)
1677 {
1678 	struct ukbd_softc *sc = kbd->kb_data;
1679 	uint32_t action;
1680 	uint32_t keycode;
1681 	int32_t usbcode;
1682 #ifdef UKBD_EMULATE_ATSCANCODE
1683 	uint32_t scancode;
1684 #endif
1685 
1686 	UKBD_LOCK_ASSERT();
1687 
1688 	if (!KBD_IS_ACTIVE(kbd))
1689 		return (NOKEY);
1690 
1691 next_code:
1692 
1693 	/* do we have a composed char to return ? */
1694 
1695 	if ((sc->sc_composed_char > 0) &&
1696 	    (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
1697 
1698 		action = sc->sc_composed_char;
1699 		sc->sc_composed_char = 0;
1700 
1701 		if (action > 0xFF) {
1702 			goto errkey;
1703 		}
1704 		goto done;
1705 	}
1706 #ifdef UKBD_EMULATE_ATSCANCODE
1707 
1708 	/* do we have a pending raw scan code? */
1709 
1710 	if (sc->sc_mode == K_RAW) {
1711 		scancode = sc->sc_buffered_char[0];
1712 		if (scancode) {
1713 			if (scancode & SCAN_PREFIX) {
1714 				sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
1715 				return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1716 			}
1717 			sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
1718 			sc->sc_buffered_char[1] = 0;
1719 			return (scancode);
1720 		}
1721 	}
1722 #endif					/* UKBD_EMULATE_ATSCANCODE */
1723 
1724 	/* see if there is something in the keyboard port */
1725 	/* XXX */
1726 	usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1727 	if (usbcode == -1) {
1728 		return (NOKEY);
1729 	}
1730 	++kbd->kb_count;
1731 
1732 #ifdef UKBD_EMULATE_ATSCANCODE
1733 	/* USB key index -> key code -> AT scan code */
1734 	keycode = ukbd_atkeycode(usbcode, sc->sc_ndata.modifiers);
1735 	if (keycode == NN) {
1736 		return (NOKEY);
1737 	}
1738 	/* return an AT scan code for the K_RAW mode */
1739 	if (sc->sc_mode == K_RAW) {
1740 		return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1741 		    (usbcode & KEY_RELEASE)));
1742 	}
1743 #else					/* !UKBD_EMULATE_ATSCANCODE */
1744 
1745 	/* return the byte as is for the K_RAW mode */
1746 	if (sc->sc_mode == K_RAW) {
1747 		return (usbcode);
1748 	}
1749 	/* USB key index -> key code */
1750 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1751 	if (keycode == NN) {
1752 		return (NOKEY);
1753 	}
1754 #endif					/* UKBD_EMULATE_ATSCANCODE */
1755 
1756 	switch (keycode) {
1757 	case 0x38:			/* left alt (compose key) */
1758 		if (usbcode & KEY_RELEASE) {
1759 			if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1760 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1761 
1762 				if (sc->sc_composed_char > 0xFF) {
1763 					sc->sc_composed_char = 0;
1764 				}
1765 			}
1766 		} else {
1767 			if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1768 				sc->sc_flags |= UKBD_FLAG_COMPOSE;
1769 				sc->sc_composed_char = 0;
1770 			}
1771 		}
1772 		break;
1773 	}
1774 
1775 	/* return the key code in the K_CODE mode */
1776 	if (usbcode & KEY_RELEASE) {
1777 		keycode |= SCAN_RELEASE;
1778 	}
1779 	if (sc->sc_mode == K_CODE) {
1780 		return (keycode);
1781 	}
1782 	/* compose a character code */
1783 	if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1784 		switch (keycode) {
1785 			/* key pressed, process it */
1786 		case 0x47:
1787 		case 0x48:
1788 		case 0x49:		/* keypad 7,8,9 */
1789 			sc->sc_composed_char *= 10;
1790 			sc->sc_composed_char += keycode - 0x40;
1791 			goto check_composed;
1792 
1793 		case 0x4B:
1794 		case 0x4C:
1795 		case 0x4D:		/* keypad 4,5,6 */
1796 			sc->sc_composed_char *= 10;
1797 			sc->sc_composed_char += keycode - 0x47;
1798 			goto check_composed;
1799 
1800 		case 0x4F:
1801 		case 0x50:
1802 		case 0x51:		/* keypad 1,2,3 */
1803 			sc->sc_composed_char *= 10;
1804 			sc->sc_composed_char += keycode - 0x4E;
1805 			goto check_composed;
1806 
1807 		case 0x52:		/* keypad 0 */
1808 			sc->sc_composed_char *= 10;
1809 			goto check_composed;
1810 
1811 			/* key released, no interest here */
1812 		case SCAN_RELEASE | 0x47:
1813 		case SCAN_RELEASE | 0x48:
1814 		case SCAN_RELEASE | 0x49:	/* keypad 7,8,9 */
1815 		case SCAN_RELEASE | 0x4B:
1816 		case SCAN_RELEASE | 0x4C:
1817 		case SCAN_RELEASE | 0x4D:	/* keypad 4,5,6 */
1818 		case SCAN_RELEASE | 0x4F:
1819 		case SCAN_RELEASE | 0x50:
1820 		case SCAN_RELEASE | 0x51:	/* keypad 1,2,3 */
1821 		case SCAN_RELEASE | 0x52:	/* keypad 0 */
1822 			goto next_code;
1823 
1824 		case 0x38:		/* left alt key */
1825 			break;
1826 
1827 		default:
1828 			if (sc->sc_composed_char > 0) {
1829 				sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1830 				sc->sc_composed_char = 0;
1831 				goto errkey;
1832 			}
1833 			break;
1834 		}
1835 	}
1836 	/* keycode to key action */
1837 	action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1838 	    (keycode & SCAN_RELEASE),
1839 	    &sc->sc_state, &sc->sc_accents);
1840 	if (action == NOKEY) {
1841 		goto next_code;
1842 	}
1843 done:
1844 	return (action);
1845 
1846 check_composed:
1847 	if (sc->sc_composed_char <= 0xFF) {
1848 		goto next_code;
1849 	}
1850 errkey:
1851 	return (ERRKEY);
1852 }
1853 
1854 /* Currently wait is always false. */
1855 static uint32_t
1856 ukbd_read_char(keyboard_t *kbd, int wait)
1857 {
1858 	uint32_t keycode;
1859 
1860 	UKBD_LOCK();
1861 	keycode = ukbd_read_char_locked(kbd, wait);
1862 	UKBD_UNLOCK();
1863 
1864 	return (keycode);
1865 }
1866 
1867 /* some useful control functions */
1868 static int
1869 ukbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
1870 {
1871 	struct ukbd_softc *sc = kbd->kb_data;
1872 	int i;
1873 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1874     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1875 	int ival;
1876 
1877 #endif
1878 
1879 	UKBD_LOCK_ASSERT();
1880 
1881 	switch (cmd) {
1882 	case KDGKBMODE:		/* get keyboard mode */
1883 		*(int *)arg = sc->sc_mode;
1884 		break;
1885 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1886     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1887 	case _IO('K', 7):
1888 		ival = IOCPARM_IVAL(arg);
1889 		arg = (caddr_t)&ival;
1890 		/* FALLTHROUGH */
1891 #endif
1892 	case KDSKBMODE:		/* set keyboard mode */
1893 		switch (*(int *)arg) {
1894 		case K_XLATE:
1895 			if (sc->sc_mode != K_XLATE) {
1896 				/* make lock key state and LED state match */
1897 				sc->sc_state &= ~LOCK_MASK;
1898 				sc->sc_state |= KBD_LED_VAL(kbd);
1899 			}
1900 			/* FALLTHROUGH */
1901 		case K_RAW:
1902 		case K_CODE:
1903 			if (sc->sc_mode != *(int *)arg) {
1904 				if ((sc->sc_flags & UKBD_FLAG_POLLING) == 0)
1905 					ukbd_clear_state(kbd);
1906 				sc->sc_mode = *(int *)arg;
1907 			}
1908 			break;
1909 		default:
1910 			return (EINVAL);
1911 		}
1912 		break;
1913 
1914 	case KDGETLED:			/* get keyboard LED */
1915 		*(int *)arg = KBD_LED_VAL(kbd);
1916 		break;
1917 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1918     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1919 	case _IO('K', 66):
1920 		ival = IOCPARM_IVAL(arg);
1921 		arg = (caddr_t)&ival;
1922 		/* FALLTHROUGH */
1923 #endif
1924 	case KDSETLED:			/* set keyboard LED */
1925 		/* NOTE: lock key state in "sc_state" won't be changed */
1926 		if (*(int *)arg & ~LOCK_MASK)
1927 			return (EINVAL);
1928 
1929 		i = *(int *)arg;
1930 
1931 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1932 		if (sc->sc_mode == K_XLATE &&
1933 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1934 			if (i & ALKED)
1935 				i |= CLKED;
1936 			else
1937 				i &= ~CLKED;
1938 		}
1939 		if (KBD_HAS_DEVICE(kbd))
1940 			ukbd_set_leds(sc, i);
1941 
1942 		KBD_LED_VAL(kbd) = *(int *)arg;
1943 		break;
1944 	case KDGKBSTATE:		/* get lock key state */
1945 		*(int *)arg = sc->sc_state & LOCK_MASK;
1946 		break;
1947 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1948     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1949 	case _IO('K', 20):
1950 		ival = IOCPARM_IVAL(arg);
1951 		arg = (caddr_t)&ival;
1952 		/* FALLTHROUGH */
1953 #endif
1954 	case KDSKBSTATE:		/* set lock key state */
1955 		if (*(int *)arg & ~LOCK_MASK) {
1956 			return (EINVAL);
1957 		}
1958 		sc->sc_state &= ~LOCK_MASK;
1959 		sc->sc_state |= *(int *)arg;
1960 
1961 		/* set LEDs and quit */
1962 		return (ukbd_ioctl(kbd, KDSETLED, arg));
1963 
1964 	case KDSETREPEAT:		/* set keyboard repeat rate (new
1965 					 * interface) */
1966 		if (!KBD_HAS_DEVICE(kbd)) {
1967 			return (0);
1968 		}
1969 		/*
1970 		 * Convert negative, zero and tiny args to the same limits
1971 		 * as atkbd.  We could support delays of 1 msec, but
1972 		 * anything much shorter than the shortest atkbd value
1973 		 * of 250.34 is almost unusable as well as incompatible.
1974 		 */
1975 		kbd->kb_delay1 = imax(((int *)arg)[0], 250);
1976 		kbd->kb_delay2 = imax(((int *)arg)[1], 34);
1977 #ifdef EVDEV_SUPPORT
1978 		if (sc->sc_evdev != NULL)
1979 			evdev_push_repeats(sc->sc_evdev, kbd);
1980 #endif
1981 		return (0);
1982 
1983 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1984     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1985 	case _IO('K', 67):
1986 		ival = IOCPARM_IVAL(arg);
1987 		arg = (caddr_t)&ival;
1988 		/* FALLTHROUGH */
1989 #endif
1990 	case KDSETRAD:			/* set keyboard repeat rate (old
1991 					 * interface) */
1992 		return (ukbd_set_typematic(kbd, *(int *)arg));
1993 
1994 	case PIO_KEYMAP:		/* set keyboard translation table */
1995 	case OPIO_KEYMAP:		/* set keyboard translation table
1996 					 * (compat) */
1997 	case PIO_KEYMAPENT:		/* set keyboard translation table
1998 					 * entry */
1999 	case PIO_DEADKEYMAP:		/* set accent key translation table */
2000 		sc->sc_accents = 0;
2001 		/* FALLTHROUGH */
2002 	default:
2003 		return (genkbd_commonioctl(kbd, cmd, arg));
2004 	}
2005 
2006 	return (0);
2007 }
2008 
2009 static int
2010 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
2011 {
2012 	int result;
2013 
2014 	/*
2015 	 * XXX Check if someone is calling us from a critical section:
2016 	 */
2017 	if (curthread->td_critnest != 0)
2018 		return (EDEADLK);
2019 
2020 	/*
2021 	 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
2022 	 * context where printf(9) can be called, which among other things
2023 	 * includes interrupt filters and threads with any kinds of locks
2024 	 * already held.  For this reason it would be dangerous to acquire
2025 	 * the Giant here unconditionally.  On the other hand we have to
2026 	 * have it to handle the ioctl.
2027 	 * So we make our best effort to auto-detect whether we can grab
2028 	 * the Giant or not.  Blame syscons(4) for this.
2029 	 */
2030 	switch (cmd) {
2031 	case KDGKBSTATE:
2032 	case KDSKBSTATE:
2033 	case KDSETLED:
2034 		if (!mtx_owned(&Giant) && !USB_IN_POLLING_MODE_FUNC())
2035 			return (EDEADLK);	/* best I could come up with */
2036 		/* FALLTHROUGH */
2037 	default:
2038 		UKBD_LOCK();
2039 		result = ukbd_ioctl_locked(kbd, cmd, arg);
2040 		UKBD_UNLOCK();
2041 		return (result);
2042 	}
2043 }
2044 
2045 
2046 /* clear the internal state of the keyboard */
2047 static void
2048 ukbd_clear_state(keyboard_t *kbd)
2049 {
2050 	struct ukbd_softc *sc = kbd->kb_data;
2051 
2052 	UKBD_LOCK_ASSERT();
2053 
2054 	sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
2055 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
2056 	sc->sc_accents = 0;
2057 	sc->sc_composed_char = 0;
2058 #ifdef UKBD_EMULATE_ATSCANCODE
2059 	sc->sc_buffered_char[0] = 0;
2060 	sc->sc_buffered_char[1] = 0;
2061 #endif
2062 	memset(&sc->sc_ndata, 0, sizeof(sc->sc_ndata));
2063 	memset(&sc->sc_odata, 0, sizeof(sc->sc_odata));
2064 	memset(&sc->sc_ntime, 0, sizeof(sc->sc_ntime));
2065 	memset(&sc->sc_otime, 0, sizeof(sc->sc_otime));
2066 }
2067 
2068 /* save the internal state, not used */
2069 static int
2070 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
2071 {
2072 	return (len == 0) ? 1 : -1;
2073 }
2074 
2075 /* set the internal state, not used */
2076 static int
2077 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
2078 {
2079 	return (EINVAL);
2080 }
2081 
2082 static int
2083 ukbd_poll(keyboard_t *kbd, int on)
2084 {
2085 	struct ukbd_softc *sc = kbd->kb_data;
2086 
2087 	UKBD_LOCK();
2088 	/*
2089 	 * Keep a reference count on polling to allow recursive
2090 	 * cngrab() during a panic for example.
2091 	 */
2092 	if (on)
2093 		sc->sc_polling++;
2094 	else if (sc->sc_polling > 0)
2095 		sc->sc_polling--;
2096 
2097 	if (sc->sc_polling != 0) {
2098 		sc->sc_flags |= UKBD_FLAG_POLLING;
2099 		sc->sc_poll_thread = curthread;
2100 	} else {
2101 		sc->sc_flags &= ~UKBD_FLAG_POLLING;
2102 		sc->sc_delay = 0;
2103 	}
2104 	UKBD_UNLOCK();
2105 
2106 	return (0);
2107 }
2108 
2109 /* local functions */
2110 
2111 static void
2112 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
2113 {
2114 
2115 	UKBD_LOCK_ASSERT();
2116 	DPRINTF("leds=0x%02x\n", leds);
2117 
2118 	sc->sc_leds = leds;
2119 	sc->sc_flags |= UKBD_FLAG_SET_LEDS;
2120 
2121 	/* start transfer, if not already started */
2122 
2123 	usbd_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
2124 }
2125 
2126 static int
2127 ukbd_set_typematic(keyboard_t *kbd, int code)
2128 {
2129 #ifdef EVDEV_SUPPORT
2130 	struct ukbd_softc *sc = kbd->kb_data;
2131 #endif
2132 	static const int delays[] = {250, 500, 750, 1000};
2133 	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
2134 		68, 76, 84, 92, 100, 110, 118, 126,
2135 		136, 152, 168, 184, 200, 220, 236, 252,
2136 	272, 304, 336, 368, 400, 440, 472, 504};
2137 
2138 	if (code & ~0x7f) {
2139 		return (EINVAL);
2140 	}
2141 	kbd->kb_delay1 = delays[(code >> 5) & 3];
2142 	kbd->kb_delay2 = rates[code & 0x1f];
2143 #ifdef EVDEV_SUPPORT
2144 	if (sc->sc_evdev != NULL)
2145 		evdev_push_repeats(sc->sc_evdev, kbd);
2146 #endif
2147 	return (0);
2148 }
2149 
2150 #ifdef UKBD_EMULATE_ATSCANCODE
2151 static uint32_t
2152 ukbd_atkeycode(int usbcode, int shift)
2153 {
2154 	uint32_t keycode;
2155 
2156 	keycode = ukbd_trtab[KEY_INDEX(usbcode)];
2157 	/*
2158 	 * Translate Alt-PrintScreen to SysRq.
2159 	 *
2160 	 * Some or all AT keyboards connected through USB have already
2161 	 * mapped Alted PrintScreens to an unusual usbcode (0x8a).
2162 	 * ukbd_trtab translates this to 0x7e, and key2scan() would
2163 	 * translate that to 0x79 (Intl' 4).  Assume that if we have
2164 	 * an Alted 0x7e here then it actually is an Alted PrintScreen.
2165 	 *
2166 	 * The usual usbcode for all PrintScreens is 0x46.  ukbd_trtab
2167 	 * translates this to 0x5c, so the Alt check to classify 0x5c
2168 	 * is routine.
2169 	 */
2170 	if ((keycode == 0x5c || keycode == 0x7e) &&
2171 	    shift & (MOD_ALT_L | MOD_ALT_R))
2172 		return (0x54);
2173 	return (keycode);
2174 }
2175 
2176 static int
2177 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
2178 {
2179 	static const int scan[] = {
2180 		/* 89 */
2181 		0x11c,	/* Enter */
2182 		/* 90-99 */
2183 		0x11d,	/* Ctrl-R */
2184 		0x135,	/* Divide */
2185 		0x137,	/* PrintScreen */
2186 		0x138,	/* Alt-R */
2187 		0x147,	/* Home */
2188 		0x148,	/* Up */
2189 		0x149,	/* PageUp */
2190 		0x14b,	/* Left */
2191 		0x14d,	/* Right */
2192 		0x14f,	/* End */
2193 		/* 100-109 */
2194 		0x150,	/* Down */
2195 		0x151,	/* PageDown */
2196 		0x152,	/* Insert */
2197 		0x153,	/* Delete */
2198 		0x146,	/* Pause/Break */
2199 		0x15b,	/* Win_L(Super_L) */
2200 		0x15c,	/* Win_R(Super_R) */
2201 		0x15d,	/* Application(Menu) */
2202 
2203 		/* SUN TYPE 6 USB KEYBOARD */
2204 		0x168,	/* Sun Type 6 Help */
2205 		0x15e,	/* Sun Type 6 Stop */
2206 		/* 110 - 119 */
2207 		0x15f,	/* Sun Type 6 Again */
2208 		0x160,	/* Sun Type 6 Props */
2209 		0x161,	/* Sun Type 6 Undo */
2210 		0x162,	/* Sun Type 6 Front */
2211 		0x163,	/* Sun Type 6 Copy */
2212 		0x164,	/* Sun Type 6 Open */
2213 		0x165,	/* Sun Type 6 Paste */
2214 		0x166,	/* Sun Type 6 Find */
2215 		0x167,	/* Sun Type 6 Cut */
2216 		0x125,	/* Sun Type 6 Mute */
2217 		/* 120 - 130 */
2218 		0x11f,	/* Sun Type 6 VolumeDown */
2219 		0x11e,	/* Sun Type 6 VolumeUp */
2220 		0x120,	/* Sun Type 6 PowerDown */
2221 
2222 		/* Japanese 106/109 keyboard */
2223 		0x73,	/* Keyboard Intl' 1 (backslash / underscore) */
2224 		0x70,	/* Keyboard Intl' 2 (Katakana / Hiragana) */
2225 		0x7d,	/* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
2226 		0x79,	/* Keyboard Intl' 4 (Henkan) */
2227 		0x7b,	/* Keyboard Intl' 5 (Muhenkan) */
2228 		0x5c,	/* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
2229 		0x71,   /* Apple Keyboard JIS (Kana) */
2230 		0x72,   /* Apple Keyboard JIS (Eisu) */
2231 	};
2232 
2233 	if ((code >= 89) && (code < (int)(89 + nitems(scan)))) {
2234 		code = scan[code - 89];
2235 	}
2236 	/* PrintScreen */
2237 	if (code == 0x137 && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R |
2238 	    MOD_SHIFT_L | MOD_SHIFT_R)))) {
2239 		code |= SCAN_PREFIX_SHIFT;
2240 	}
2241 	/* Pause/Break */
2242 	if ((code == 0x146) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
2243 		code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
2244 	}
2245 	code |= (up ? SCAN_RELEASE : SCAN_PRESS);
2246 
2247 	if (code & SCAN_PREFIX) {
2248 		if (code & SCAN_PREFIX_CTL) {
2249 			/* Ctrl */
2250 			sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
2251 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
2252 		} else if (code & SCAN_PREFIX_SHIFT) {
2253 			/* Shift */
2254 			sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
2255 			sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
2256 		} else {
2257 			sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
2258 			sc->sc_buffered_char[1] = 0;
2259 		}
2260 		return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
2261 	}
2262 	return (code);
2263 
2264 }
2265 
2266 #endif					/* UKBD_EMULATE_ATSCANCODE */
2267 
2268 static keyboard_switch_t ukbdsw = {
2269 	.probe = &ukbd__probe,
2270 	.init = &ukbd_init,
2271 	.term = &ukbd_term,
2272 	.intr = &ukbd_intr,
2273 	.test_if = &ukbd_test_if,
2274 	.enable = &ukbd_enable,
2275 	.disable = &ukbd_disable,
2276 	.read = &ukbd_read,
2277 	.check = &ukbd_check,
2278 	.read_char = &ukbd_read_char,
2279 	.check_char = &ukbd_check_char,
2280 	.ioctl = &ukbd_ioctl,
2281 	.lock = &ukbd_lock,
2282 	.clear_state = &ukbd_clear_state,
2283 	.get_state = &ukbd_get_state,
2284 	.set_state = &ukbd_set_state,
2285 	.poll = &ukbd_poll,
2286 };
2287 
2288 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
2289 
2290 static int
2291 ukbd_driver_load(module_t mod, int what, void *arg)
2292 {
2293 	switch (what) {
2294 	case MOD_LOAD:
2295 		kbd_add_driver(&ukbd_kbd_driver);
2296 		break;
2297 	case MOD_UNLOAD:
2298 		kbd_delete_driver(&ukbd_kbd_driver);
2299 		break;
2300 	}
2301 	return (0);
2302 }
2303 
2304 static devclass_t ukbd_devclass;
2305 
2306 static device_method_t ukbd_methods[] = {
2307 	DEVMETHOD(device_probe, ukbd_probe),
2308 	DEVMETHOD(device_attach, ukbd_attach),
2309 	DEVMETHOD(device_detach, ukbd_detach),
2310 	DEVMETHOD(device_resume, ukbd_resume),
2311 
2312 	DEVMETHOD_END
2313 };
2314 
2315 static driver_t ukbd_driver = {
2316 	.name = "ukbd",
2317 	.methods = ukbd_methods,
2318 	.size = sizeof(struct ukbd_softc),
2319 };
2320 
2321 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
2322 MODULE_DEPEND(ukbd, usb, 1, 1, 1);
2323 #ifdef EVDEV_SUPPORT
2324 MODULE_DEPEND(ukbd, evdev, 1, 1, 1);
2325 #endif
2326 MODULE_VERSION(ukbd, 1);
2327 USB_PNP_HOST_INFO(ukbd_devs);
2328