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