]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/input/ukbd.c
Update ee(1) in the base system to version 1.5.0.
[FreeBSD/FreeBSD.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 <dev/usb/usb.h>
52 #include <dev/usb/usb_mfunc.h>
53 #include <dev/usb/usb_error.h>
54 #include <dev/usb/usbhid.h>
55
56 #define USB_DEBUG_VAR ukbd_debug
57
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_util.h>
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_transfer.h>
64 #include <dev/usb/usb_request.h>
65 #include <dev/usb/usb_dynamic.h>
66 #include <dev/usb/usb_hid.h>
67
68 #include <dev/usb/quirk/usb_quirk.h>
69
70 #include <sys/ioccom.h>
71 #include <sys/filio.h>
72 #include <sys/tty.h>
73 #include <sys/kbio.h>
74
75 #include <dev/kbd/kbdreg.h>
76
77 /* the initial key map, accent map and fkey strings */
78 #if defined(UKBD_DFLT_KEYMAP) && !defined(KLD_MODULE)
79 #define KBD_DFLT_KEYMAP
80 #include "ukbdmap.h"
81 #endif
82
83 /* the following file must be included after "ukbdmap.h" */
84 #include <dev/kbd/kbdtables.h>
85
86 #if USB_DEBUG
87 static int ukbd_debug = 0;
88
89 SYSCTL_NODE(_hw_usb, OID_AUTO, ukbd, CTLFLAG_RW, 0, "USB ukbd");
90 SYSCTL_INT(_hw_usb_ukbd, OID_AUTO, debug, CTLFLAG_RW,
91     &ukbd_debug, 0, "Debug level");
92 #endif
93
94 #define UPROTO_BOOT_KEYBOARD 1
95
96 #define UKBD_EMULATE_ATSCANCODE        1
97 #define UKBD_DRIVER_NAME          "ukbd"
98 #define UKBD_NMOD                     8 /* units */
99 #define UKBD_NKEYCODE                 6 /* units */
100 #define UKBD_IN_BUF_SIZE  (2*(UKBD_NMOD + (2*UKBD_NKEYCODE)))   /* bytes */
101 #define UKBD_IN_BUF_FULL  (UKBD_IN_BUF_SIZE / 2)        /* bytes */
102 #define UKBD_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))        /* units */
103
104 struct ukbd_data {
105         uint8_t modifiers;
106 #define MOD_CONTROL_L   0x01
107 #define MOD_CONTROL_R   0x10
108 #define MOD_SHIFT_L     0x02
109 #define MOD_SHIFT_R     0x20
110 #define MOD_ALT_L       0x04
111 #define MOD_ALT_R       0x40
112 #define MOD_WIN_L       0x08
113 #define MOD_WIN_R       0x80
114         uint8_t reserved;
115         uint8_t keycode[UKBD_NKEYCODE];
116 } __packed;
117
118 enum {
119         UKBD_INTR_DT,
120         UKBD_INTR_CS,
121         UKBD_CTRL_LED,
122         UKBD_N_TRANSFER = 3,
123 };
124
125 struct ukbd_softc {
126         keyboard_t sc_kbd;
127         keymap_t sc_keymap;
128         accentmap_t sc_accmap;
129         fkeytab_t sc_fkeymap[UKBD_NFKEY];
130         struct usb2_callout sc_callout;
131         struct ukbd_data sc_ndata;
132         struct ukbd_data sc_odata;
133
134         struct usb2_device *sc_udev;
135         struct usb2_interface *sc_iface;
136         struct usb2_xfer *sc_xfer[UKBD_N_TRANSFER];
137
138         uint32_t sc_ntime[UKBD_NKEYCODE];
139         uint32_t sc_otime[UKBD_NKEYCODE];
140         uint32_t sc_input[UKBD_IN_BUF_SIZE];    /* input buffer */
141         uint32_t sc_time_ms;
142         uint32_t sc_composed_char;      /* composed char code, if non-zero */
143 #ifdef UKBD_EMULATE_ATSCANCODE
144         uint32_t sc_buffered_char[2];
145 #endif
146         uint32_t sc_flags;              /* flags */
147 #define UKBD_FLAG_COMPOSE    0x0001
148 #define UKBD_FLAG_POLLING    0x0002
149 #define UKBD_FLAG_SET_LEDS   0x0004
150 #define UKBD_FLAG_INTR_STALL 0x0008
151 #define UKBD_FLAG_ATTACHED   0x0010
152 #define UKBD_FLAG_GONE       0x0020
153
154         int32_t sc_mode;                /* input mode (K_XLATE,K_RAW,K_CODE) */
155         int32_t sc_state;               /* shift/lock key state */
156         int32_t sc_accents;             /* accent key index (> 0) */
157
158         uint16_t sc_inputs;
159         uint16_t sc_inputhead;
160         uint16_t sc_inputtail;
161
162         uint8_t sc_leds;                /* store for async led requests */
163         uint8_t sc_iface_index;
164         uint8_t sc_iface_no;
165 };
166
167 #define KEY_ERROR         0x01
168
169 #define KEY_PRESS         0
170 #define KEY_RELEASE       0x400
171 #define KEY_INDEX(c)      ((c) & 0xFF)
172
173 #define SCAN_PRESS        0
174 #define SCAN_RELEASE      0x80
175 #define SCAN_PREFIX_E0    0x100
176 #define SCAN_PREFIX_E1    0x200
177 #define SCAN_PREFIX_CTL   0x400
178 #define SCAN_PREFIX_SHIFT 0x800
179 #define SCAN_PREFIX     (SCAN_PREFIX_E0  | SCAN_PREFIX_E1 | \
180                          SCAN_PREFIX_CTL | SCAN_PREFIX_SHIFT)
181 #define SCAN_CHAR(c)    ((c) & 0x7f)
182
183 struct ukbd_mods {
184         uint32_t mask, key;
185 };
186
187 static const struct ukbd_mods ukbd_mods[UKBD_NMOD] = {
188         {MOD_CONTROL_L, 0xe0},
189         {MOD_CONTROL_R, 0xe4},
190         {MOD_SHIFT_L, 0xe1},
191         {MOD_SHIFT_R, 0xe5},
192         {MOD_ALT_L, 0xe2},
193         {MOD_ALT_R, 0xe6},
194         {MOD_WIN_L, 0xe3},
195         {MOD_WIN_R, 0xe7},
196 };
197
198 #define NN 0                            /* no translation */
199 /*
200  * Translate USB keycodes to AT keyboard scancodes.
201  */
202 /*
203  * FIXME: Mac USB keyboard generates:
204  * 0x53: keypad NumLock/Clear
205  * 0x66: Power
206  * 0x67: keypad =
207  * 0x68: F13
208  * 0x69: F14
209  * 0x6a: F15
210  */
211 static const uint8_t ukbd_trtab[256] = {
212         0, 0, 0, 0, 30, 48, 46, 32,     /* 00 - 07 */
213         18, 33, 34, 35, 23, 36, 37, 38, /* 08 - 0F */
214         50, 49, 24, 25, 16, 19, 31, 20, /* 10 - 17 */
215         22, 47, 17, 45, 21, 44, 2, 3,   /* 18 - 1F */
216         4, 5, 6, 7, 8, 9, 10, 11,       /* 20 - 27 */
217         28, 1, 14, 15, 57, 12, 13, 26,  /* 28 - 2F */
218         27, 43, 43, 39, 40, 41, 51, 52, /* 30 - 37 */
219         53, 58, 59, 60, 61, 62, 63, 64, /* 38 - 3F */
220         65, 66, 67, 68, 87, 88, 92, 70, /* 40 - 47 */
221         104, 102, 94, 96, 103, 99, 101, 98,     /* 48 - 4F */
222         97, 100, 95, 69, 91, 55, 74, 78,/* 50 - 57 */
223         89, 79, 80, 81, 75, 76, 77, 71, /* 58 - 5F */
224         72, 73, 82, 83, 86, 107, 122, NN,       /* 60 - 67 */
225         NN, NN, NN, NN, NN, NN, NN, NN, /* 68 - 6F */
226         NN, NN, NN, NN, 115, 108, 111, 113,     /* 70 - 77 */
227         109, 110, 112, 118, 114, 116, 117, 119, /* 78 - 7F */
228         121, 120, NN, NN, NN, NN, NN, 115,      /* 80 - 87 */
229         112, 125, 121, 123, NN, NN, NN, NN,     /* 88 - 8F */
230         NN, NN, NN, NN, NN, NN, NN, NN, /* 90 - 97 */
231         NN, NN, NN, NN, NN, NN, NN, NN, /* 98 - 9F */
232         NN, NN, NN, NN, NN, NN, NN, NN, /* A0 - A7 */
233         NN, NN, NN, NN, NN, NN, NN, NN, /* A8 - AF */
234         NN, NN, NN, NN, NN, NN, NN, NN, /* B0 - B7 */
235         NN, NN, NN, NN, NN, NN, NN, NN, /* B8 - BF */
236         NN, NN, NN, NN, NN, NN, NN, NN, /* C0 - C7 */
237         NN, NN, NN, NN, NN, NN, NN, NN, /* C8 - CF */
238         NN, NN, NN, NN, NN, NN, NN, NN, /* D0 - D7 */
239         NN, NN, NN, NN, NN, NN, NN, NN, /* D8 - DF */
240         29, 42, 56, 105, 90, 54, 93, 106,       /* E0 - E7 */
241         NN, NN, NN, NN, NN, NN, NN, NN, /* E8 - EF */
242         NN, NN, NN, NN, NN, NN, NN, NN, /* F0 - F7 */
243         NN, NN, NN, NN, NN, NN, NN, NN, /* F8 - FF */
244 };
245
246 /* prototypes */
247 static void     ukbd_timeout(void *);
248 static void     ukbd_set_leds(struct ukbd_softc *, uint8_t);
249 static int      ukbd_set_typematic(keyboard_t *, int);
250 #ifdef UKBD_EMULATE_ATSCANCODE
251 static int      ukbd_key2scan(struct ukbd_softc *, int, int, int);
252 #endif
253 static uint32_t ukbd_read_char(keyboard_t *, int);
254 static void     ukbd_clear_state(keyboard_t *);
255 static int      ukbd_ioctl(keyboard_t *, u_long, caddr_t);
256 static int      ukbd_enable(keyboard_t *);
257 static int      ukbd_disable(keyboard_t *);
258 static void     ukbd_interrupt(struct ukbd_softc *);
259
260 static device_probe_t ukbd_probe;
261 static device_attach_t ukbd_attach;
262 static device_detach_t ukbd_detach;
263 static device_resume_t ukbd_resume;
264
265 static void
266 ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
267 {
268         mtx_assert(&Giant, MA_OWNED);
269
270         DPRINTF("0x%02x (%d) %s\n", key, key,
271             (key & KEY_RELEASE) ? "released" : "pressed");
272
273         if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
274                 sc->sc_input[sc->sc_inputtail] = key;
275                 ++(sc->sc_inputs);
276                 ++(sc->sc_inputtail);
277                 if (sc->sc_inputtail >= UKBD_IN_BUF_SIZE) {
278                         sc->sc_inputtail = 0;
279                 }
280         } else {
281                 DPRINTF("input buffer is full\n");
282         }
283 }
284
285 static int32_t
286 ukbd_get_key(struct ukbd_softc *sc, uint8_t wait)
287 {
288         int32_t c;
289
290         mtx_assert(&Giant, MA_OWNED);
291
292         if (sc->sc_inputs == 0) {
293                 /* start transfer, if not already started */
294                 usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
295         }
296         if (sc->sc_flags & UKBD_FLAG_POLLING) {
297                 DPRINTFN(2, "polling\n");
298
299                 while (sc->sc_inputs == 0) {
300
301                         usb2_do_poll(sc->sc_xfer, UKBD_N_TRANSFER);
302
303                         DELAY(1000);    /* delay 1 ms */
304
305                         sc->sc_time_ms++;
306
307                         /* support repetition of keys: */
308
309                         ukbd_interrupt(sc);
310
311                         if (!wait) {
312                                 break;
313                         }
314                 }
315         }
316         if (sc->sc_inputs == 0) {
317                 c = -1;
318         } else {
319                 c = sc->sc_input[sc->sc_inputhead];
320                 --(sc->sc_inputs);
321                 ++(sc->sc_inputhead);
322                 if (sc->sc_inputhead >= UKBD_IN_BUF_SIZE) {
323                         sc->sc_inputhead = 0;
324                 }
325         }
326         return (c);
327 }
328
329 static void
330 ukbd_interrupt(struct ukbd_softc *sc)
331 {
332         uint32_t n_mod;
333         uint32_t o_mod;
334         uint32_t now = sc->sc_time_ms;
335         uint32_t dtime;
336         uint32_t c;
337         uint8_t key;
338         uint8_t i;
339         uint8_t j;
340
341         if (sc->sc_ndata.keycode[0] == KEY_ERROR) {
342                 goto done;
343         }
344         n_mod = sc->sc_ndata.modifiers;
345         o_mod = sc->sc_odata.modifiers;
346         if (n_mod != o_mod) {
347                 for (i = 0; i < UKBD_NMOD; i++) {
348                         if ((n_mod & ukbd_mods[i].mask) !=
349                             (o_mod & ukbd_mods[i].mask)) {
350                                 ukbd_put_key(sc, ukbd_mods[i].key |
351                                     ((n_mod & ukbd_mods[i].mask) ?
352                                     KEY_PRESS : KEY_RELEASE));
353                         }
354                 }
355         }
356         /* Check for released keys. */
357         for (i = 0; i < UKBD_NKEYCODE; i++) {
358                 key = sc->sc_odata.keycode[i];
359                 if (key == 0) {
360                         continue;
361                 }
362                 for (j = 0; j < UKBD_NKEYCODE; j++) {
363                         if (sc->sc_ndata.keycode[j] == 0) {
364                                 continue;
365                         }
366                         if (key == sc->sc_ndata.keycode[j]) {
367                                 goto rfound;
368                         }
369                 }
370                 ukbd_put_key(sc, key | KEY_RELEASE);
371 rfound: ;
372         }
373
374         /* Check for pressed keys. */
375         for (i = 0; i < UKBD_NKEYCODE; i++) {
376                 key = sc->sc_ndata.keycode[i];
377                 if (key == 0) {
378                         continue;
379                 }
380                 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay1;
381                 for (j = 0; j < UKBD_NKEYCODE; j++) {
382                         if (sc->sc_odata.keycode[j] == 0) {
383                                 continue;
384                         }
385                         if (key == sc->sc_odata.keycode[j]) {
386
387                                 /* key is still pressed */
388
389                                 sc->sc_ntime[i] = sc->sc_otime[j];
390                                 dtime = (sc->sc_otime[j] - now);
391
392                                 if (!(dtime & 0x80000000)) {
393                                         /* time has not elapsed */
394                                         goto pfound;
395                                 }
396                                 sc->sc_ntime[i] = now + sc->sc_kbd.kb_delay2;
397                                 break;
398                         }
399                 }
400                 ukbd_put_key(sc, key | KEY_PRESS);
401
402                 /*
403                  * If any other key is presently down, force its repeat to be
404                  * well in the future (100s).  This makes the last key to be
405                  * pressed do the autorepeat.
406                  */
407                 for (j = 0; j != UKBD_NKEYCODE; j++) {
408                         if (j != i)
409                                 sc->sc_ntime[j] = now + (100 * 1000);
410                 }
411 pfound: ;
412         }
413
414         sc->sc_odata = sc->sc_ndata;
415
416         bcopy(sc->sc_ntime, sc->sc_otime, sizeof(sc->sc_otime));
417
418         if (sc->sc_inputs == 0) {
419                 goto done;
420         }
421         if (sc->sc_flags & UKBD_FLAG_POLLING) {
422                 goto done;
423         }
424         if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
425             KBD_IS_BUSY(&sc->sc_kbd)) {
426                 /* let the callback function process the input */
427                 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
428                     sc->sc_kbd.kb_callback.kc_arg);
429         } else {
430                 /* read and discard the input, no one is waiting for it */
431                 do {
432                         c = ukbd_read_char(&sc->sc_kbd, 0);
433                 } while (c != NOKEY);
434         }
435 done:
436         return;
437 }
438
439 static void
440 ukbd_timeout(void *arg)
441 {
442         struct ukbd_softc *sc = arg;
443
444         mtx_assert(&Giant, MA_OWNED);
445
446         if (!(sc->sc_flags & UKBD_FLAG_POLLING)) {
447                 sc->sc_time_ms += 25;   /* milliseconds */
448         }
449         ukbd_interrupt(sc);
450
451         usb2_callout_reset(&sc->sc_callout, hz / 40, &ukbd_timeout, sc);
452 }
453
454 static void
455 ukbd_clear_stall_callback(struct usb2_xfer *xfer)
456 {
457         struct ukbd_softc *sc = xfer->priv_sc;
458         struct usb2_xfer *xfer_other = sc->sc_xfer[UKBD_INTR_DT];
459
460         if (usb2_clear_stall_callback(xfer, xfer_other)) {
461                 DPRINTF("stall cleared\n");
462                 sc->sc_flags &= ~UKBD_FLAG_INTR_STALL;
463                 usb2_transfer_start(xfer_other);
464         }
465 }
466
467 static void
468 ukbd_intr_callback(struct usb2_xfer *xfer)
469 {
470         struct ukbd_softc *sc = xfer->priv_sc;
471         uint16_t len = xfer->actlen;
472         uint8_t i;
473
474         switch (USB_GET_STATE(xfer)) {
475         case USB_ST_TRANSFERRED:
476                 DPRINTF("actlen=%d bytes\n", len);
477
478                 if (len > sizeof(sc->sc_ndata)) {
479                         len = sizeof(sc->sc_ndata);
480                 }
481                 if (len) {
482                         bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
483                         usb2_copy_out(xfer->frbuffers, 0, &sc->sc_ndata, len);
484 #if USB_DEBUG
485                         if (sc->sc_ndata.modifiers) {
486                                 DPRINTF("mod: 0x%04x\n", sc->sc_ndata.modifiers);
487                         }
488                         for (i = 0; i < UKBD_NKEYCODE; i++) {
489                                 if (sc->sc_ndata.keycode[i]) {
490                                         DPRINTF("[%d] = %d\n", i, sc->sc_ndata.keycode[i]);
491                                 }
492                         }
493 #endif                                  /* USB_DEBUG */
494                         ukbd_interrupt(sc);
495                 }
496         case USB_ST_SETUP:
497                 if (sc->sc_flags & UKBD_FLAG_INTR_STALL) {
498                         usb2_transfer_start(sc->sc_xfer[UKBD_INTR_CS]);
499                         return;
500                 }
501                 if (sc->sc_inputs < UKBD_IN_BUF_FULL) {
502                         xfer->frlengths[0] = xfer->max_data_length;
503                         usb2_start_hardware(xfer);
504                 } else {
505                         DPRINTF("input queue is full!\n");
506                 }
507                 return;
508
509         default:                        /* Error */
510                 DPRINTF("error=%s\n", usb2_errstr(xfer->error));
511
512                 if (xfer->error != USB_ERR_CANCELLED) {
513                         /* try to clear stall first */
514                         sc->sc_flags |= UKBD_FLAG_INTR_STALL;
515                         usb2_transfer_start(sc->sc_xfer[UKBD_INTR_CS]);
516                 }
517                 return;
518         }
519 }
520
521 static void
522 ukbd_set_leds_callback(struct usb2_xfer *xfer)
523 {
524         struct usb2_device_request req;
525         uint8_t buf[1];
526         struct ukbd_softc *sc = xfer->priv_sc;
527
528         switch (USB_GET_STATE(xfer)) {
529         case USB_ST_TRANSFERRED:
530         case USB_ST_SETUP:
531                 if (sc->sc_flags & UKBD_FLAG_SET_LEDS) {
532                         sc->sc_flags &= ~UKBD_FLAG_SET_LEDS;
533
534                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
535                         req.bRequest = UR_SET_REPORT;
536                         USETW2(req.wValue, UHID_OUTPUT_REPORT, 0);
537                         req.wIndex[0] = sc->sc_iface_no;
538                         req.wIndex[1] = 0;
539                         USETW(req.wLength, 1);
540
541                         buf[0] = sc->sc_leds;
542
543                         usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
544                         usb2_copy_in(xfer->frbuffers + 1, 0, buf, sizeof(buf));
545
546                         xfer->frlengths[0] = sizeof(req);
547                         xfer->frlengths[1] = sizeof(buf);
548                         xfer->nframes = 2;
549                         usb2_start_hardware(xfer);
550                 }
551                 return;
552
553         default:                        /* Error */
554                 DPRINTFN(0, "error=%s\n", usb2_errstr(xfer->error));
555                 return;
556         }
557 }
558
559 static const struct usb2_config ukbd_config[UKBD_N_TRANSFER] = {
560
561         [UKBD_INTR_DT] = {
562                 .type = UE_INTERRUPT,
563                 .endpoint = UE_ADDR_ANY,
564                 .direction = UE_DIR_IN,
565                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
566                 .bufsize = 0,   /* use wMaxPacketSize */
567                 .callback = &ukbd_intr_callback,
568         },
569
570         [UKBD_INTR_CS] = {
571                 .type = UE_CONTROL,
572                 .endpoint = 0x00,       /* Control pipe */
573                 .direction = UE_DIR_ANY,
574                 .bufsize = sizeof(struct usb2_device_request),
575                 .callback = &ukbd_clear_stall_callback,
576                 .timeout = 1000,        /* 1 second */
577                 .interval = 50, /* 50ms */
578         },
579
580         [UKBD_CTRL_LED] = {
581                 .type = UE_CONTROL,
582                 .endpoint = 0x00,       /* Control pipe */
583                 .direction = UE_DIR_ANY,
584                 .bufsize = sizeof(struct usb2_device_request) + 1,
585                 .callback = &ukbd_set_leds_callback,
586                 .timeout = 1000,        /* 1 second */
587         },
588 };
589
590 static int
591 ukbd_probe(device_t dev)
592 {
593         keyboard_switch_t *sw = kbd_get_switch(UKBD_DRIVER_NAME);
594         struct usb2_attach_arg *uaa = device_get_ivars(dev);
595
596         DPRINTFN(11, "\n");
597
598         if (sw == NULL) {
599                 return (ENXIO);
600         }
601         if (uaa->usb_mode != USB_MODE_HOST) {
602                 return (ENXIO);
603         }
604         /* check that the keyboard speaks the boot protocol: */
605         if ((uaa->info.bInterfaceClass == UICLASS_HID)
606             && (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT)
607             && (uaa->info.bInterfaceProtocol == UPROTO_BOOT_KEYBOARD)) {
608                 if (usb2_test_quirk(uaa, UQ_KBD_IGNORE))
609                         return (ENXIO);
610                 else
611                         return (0);
612         }
613         return (ENXIO);
614 }
615
616 static int
617 ukbd_attach(device_t dev)
618 {
619         struct ukbd_softc *sc = device_get_softc(dev);
620         struct usb2_attach_arg *uaa = device_get_ivars(dev);
621         int32_t unit = device_get_unit(dev);
622         keyboard_t *kbd = &sc->sc_kbd;
623         usb2_error_t err;
624         uint16_t n;
625
626         mtx_assert(&Giant, MA_OWNED);
627
628         kbd_init_struct(kbd, UKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
629
630         kbd->kb_data = (void *)sc;
631
632         device_set_usb2_desc(dev);
633
634         sc->sc_udev = uaa->device;
635         sc->sc_iface = uaa->iface;
636         sc->sc_iface_index = uaa->info.bIfaceIndex;
637         sc->sc_iface_no = uaa->info.bIfaceNum;
638         sc->sc_mode = K_XLATE;
639         sc->sc_iface = uaa->iface;
640
641         usb2_callout_init_mtx(&sc->sc_callout, &Giant, 0);
642
643         err = usb2_transfer_setup(uaa->device,
644             &uaa->info.bIfaceIndex, sc->sc_xfer, ukbd_config,
645             UKBD_N_TRANSFER, sc, &Giant);
646
647         if (err) {
648                 DPRINTF("error=%s\n", usb2_errstr(err));
649                 goto detach;
650         }
651         /* setup default keyboard maps */
652
653         sc->sc_keymap = key_map;
654         sc->sc_accmap = accent_map;
655         for (n = 0; n < UKBD_NFKEY; n++) {
656                 sc->sc_fkeymap[n] = fkey_tab[n];
657         }
658
659         kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
660             sc->sc_fkeymap, UKBD_NFKEY);
661
662         KBD_FOUND_DEVICE(kbd);
663
664         ukbd_clear_state(kbd);
665
666         /*
667          * FIXME: set the initial value for lock keys in "sc_state"
668          * according to the BIOS data?
669          */
670         KBD_PROBE_DONE(kbd);
671
672         /* ignore if SETIDLE fails, hence it is not crucial */
673         err = usb2_req_set_idle(sc->sc_udev, &Giant, sc->sc_iface_index, 0, 0);
674
675         ukbd_ioctl(kbd, KDSETLED, (caddr_t)&sc->sc_state);
676
677         KBD_INIT_DONE(kbd);
678
679         if (kbd_register(kbd) < 0) {
680                 goto detach;
681         }
682         KBD_CONFIG_DONE(kbd);
683
684         ukbd_enable(kbd);
685
686 #ifdef KBD_INSTALL_CDEV
687         if (kbd_attach(kbd)) {
688                 goto detach;
689         }
690 #endif
691         sc->sc_flags |= UKBD_FLAG_ATTACHED;
692
693         if (bootverbose) {
694                 genkbd_diag(kbd, bootverbose);
695         }
696         /* lock keyboard mutex */
697
698         mtx_lock(&Giant);
699
700         /* start the keyboard */
701
702         usb2_transfer_start(sc->sc_xfer[UKBD_INTR_DT]);
703
704         /* start the timer */
705
706         ukbd_timeout(sc);
707         mtx_unlock(&Giant);
708         return (0);                     /* success */
709
710 detach:
711         ukbd_detach(dev);
712         return (ENXIO);                 /* error */
713 }
714
715 int
716 ukbd_detach(device_t dev)
717 {
718         struct ukbd_softc *sc = device_get_softc(dev);
719         int error;
720
721         mtx_assert(&Giant, MA_OWNED);
722
723         DPRINTF("\n");
724
725         if (sc->sc_flags & UKBD_FLAG_POLLING) {
726                 panic("cannot detach polled keyboard!\n");
727         }
728         sc->sc_flags |= UKBD_FLAG_GONE;
729
730         usb2_callout_stop(&sc->sc_callout);
731
732         ukbd_disable(&sc->sc_kbd);
733
734 #ifdef KBD_INSTALL_CDEV
735         if (sc->sc_flags & UKBD_FLAG_ATTACHED) {
736                 error = kbd_detach(&sc->sc_kbd);
737                 if (error) {
738                         /* usb attach cannot return an error */
739                         device_printf(dev, "WARNING: kbd_detach() "
740                             "returned non-zero! (ignored)\n");
741                 }
742         }
743 #endif
744         if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
745                 error = kbd_unregister(&sc->sc_kbd);
746                 if (error) {
747                         /* usb attach cannot return an error */
748                         device_printf(dev, "WARNING: kbd_unregister() "
749                             "returned non-zero! (ignored)\n");
750                 }
751         }
752         sc->sc_kbd.kb_flags = 0;
753
754         usb2_transfer_unsetup(sc->sc_xfer, UKBD_N_TRANSFER);
755
756         usb2_callout_drain(&sc->sc_callout);
757
758         DPRINTF("%s: disconnected\n",
759             device_get_nameunit(dev));
760
761         return (0);
762 }
763
764 static int
765 ukbd_resume(device_t dev)
766 {
767         struct ukbd_softc *sc = device_get_softc(dev);
768
769         mtx_assert(&Giant, MA_OWNED);
770
771         ukbd_clear_state(&sc->sc_kbd);
772
773         return (0);
774 }
775
776 /* early keyboard probe, not supported */
777 static int
778 ukbd_configure(int flags)
779 {
780         return (0);
781 }
782
783 /* detect a keyboard, not used */
784 static int
785 ukbd__probe(int unit, void *arg, int flags)
786 {
787         mtx_assert(&Giant, MA_OWNED);
788         return (ENXIO);
789 }
790
791 /* reset and initialize the device, not used */
792 static int
793 ukbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
794 {
795         mtx_assert(&Giant, MA_OWNED);
796         return (ENXIO);
797 }
798
799 /* test the interface to the device, not used */
800 static int
801 ukbd_test_if(keyboard_t *kbd)
802 {
803         mtx_assert(&Giant, MA_OWNED);
804         return (0);
805 }
806
807 /* finish using this keyboard, not used */
808 static int
809 ukbd_term(keyboard_t *kbd)
810 {
811         mtx_assert(&Giant, MA_OWNED);
812         return (ENXIO);
813 }
814
815 /* keyboard interrupt routine, not used */
816 static int
817 ukbd_intr(keyboard_t *kbd, void *arg)
818 {
819         mtx_assert(&Giant, MA_OWNED);
820         return (0);
821 }
822
823 /* lock the access to the keyboard, not used */
824 static int
825 ukbd_lock(keyboard_t *kbd, int lock)
826 {
827         mtx_assert(&Giant, MA_OWNED);
828         return (1);
829 }
830
831 /*
832  * Enable the access to the device; until this function is called,
833  * the client cannot read from the keyboard.
834  */
835 static int
836 ukbd_enable(keyboard_t *kbd)
837 {
838         mtx_assert(&Giant, MA_OWNED);
839         KBD_ACTIVATE(kbd);
840         return (0);
841 }
842
843 /* disallow the access to the device */
844 static int
845 ukbd_disable(keyboard_t *kbd)
846 {
847         mtx_assert(&Giant, MA_OWNED);
848         KBD_DEACTIVATE(kbd);
849         return (0);
850 }
851
852 /* check if data is waiting */
853 static int
854 ukbd_check(keyboard_t *kbd)
855 {
856         struct ukbd_softc *sc = kbd->kb_data;
857
858         if (!mtx_owned(&Giant)) {
859                 return (0);             /* XXX */
860         }
861         mtx_assert(&Giant, MA_OWNED);
862
863         if (!KBD_IS_ACTIVE(kbd)) {
864                 return (0);
865         }
866 #ifdef UKBD_EMULATE_ATSCANCODE
867         if (sc->sc_buffered_char[0]) {
868                 return (1);
869         }
870 #endif
871         if (sc->sc_inputs > 0) {
872                 return (1);
873         }
874         return (0);
875 }
876
877 /* check if char is waiting */
878 static int
879 ukbd_check_char(keyboard_t *kbd)
880 {
881         struct ukbd_softc *sc = kbd->kb_data;
882
883         if (!mtx_owned(&Giant)) {
884                 return (0);             /* XXX */
885         }
886         mtx_assert(&Giant, MA_OWNED);
887
888         if (!KBD_IS_ACTIVE(kbd)) {
889                 return (0);
890         }
891         if ((sc->sc_composed_char > 0) &&
892             (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
893                 return (1);
894         }
895         return (ukbd_check(kbd));
896 }
897
898
899 /* read one byte from the keyboard if it's allowed */
900 static int
901 ukbd_read(keyboard_t *kbd, int wait)
902 {
903         struct ukbd_softc *sc = kbd->kb_data;
904         int32_t usbcode;
905
906 #ifdef UKBD_EMULATE_ATSCANCODE
907         uint32_t keycode;
908         uint32_t scancode;
909
910 #endif
911
912         if (!mtx_owned(&Giant)) {
913                 return -1;              /* XXX */
914         }
915         mtx_assert(&Giant, MA_OWNED);
916
917 #ifdef UKBD_EMULATE_ATSCANCODE
918         if (sc->sc_buffered_char[0]) {
919                 scancode = sc->sc_buffered_char[0];
920                 if (scancode & SCAN_PREFIX) {
921                         sc->sc_buffered_char[0] &= ~SCAN_PREFIX;
922                         return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
923                 }
924                 sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
925                 sc->sc_buffered_char[1] = 0;
926                 return (scancode);
927         }
928 #endif                                  /* UKBD_EMULATE_ATSCANCODE */
929
930         /* XXX */
931         usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
932         if (!KBD_IS_ACTIVE(kbd) || (usbcode == -1)) {
933                 return -1;
934         }
935         ++(kbd->kb_count);
936
937 #ifdef UKBD_EMULATE_ATSCANCODE
938         keycode = ukbd_trtab[KEY_INDEX(usbcode)];
939         if (keycode == NN) {
940                 return -1;
941         }
942         return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
943             (usbcode & KEY_RELEASE)));
944 #else                                   /* !UKBD_EMULATE_ATSCANCODE */
945         return (usbcode);
946 #endif                                  /* UKBD_EMULATE_ATSCANCODE */
947 }
948
949 /* read char from the keyboard */
950 static uint32_t
951 ukbd_read_char(keyboard_t *kbd, int wait)
952 {
953         struct ukbd_softc *sc = kbd->kb_data;
954         uint32_t action;
955         uint32_t keycode;
956         int32_t usbcode;
957
958 #ifdef UKBD_EMULATE_ATSCANCODE
959         uint32_t scancode;
960
961 #endif
962         if (!mtx_owned(&Giant)) {
963                 return (NOKEY);         /* XXX */
964         }
965         mtx_assert(&Giant, MA_OWNED);
966
967 next_code:
968
969         /* do we have a composed char to return ? */
970
971         if ((sc->sc_composed_char > 0) &&
972             (!(sc->sc_flags & UKBD_FLAG_COMPOSE))) {
973
974                 action = sc->sc_composed_char;
975                 sc->sc_composed_char = 0;
976
977                 if (action > 0xFF) {
978                         goto errkey;
979                 }
980                 goto done;
981         }
982 #ifdef UKBD_EMULATE_ATSCANCODE
983
984         /* do we have a pending raw scan code? */
985
986         if (sc->sc_mode == K_RAW) {
987                 scancode = sc->sc_buffered_char[0];
988                 if (scancode) {
989                         if (scancode & SCAN_PREFIX) {
990                                 sc->sc_buffered_char[0] = (scancode & ~SCAN_PREFIX);
991                                 return ((scancode & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
992                         }
993                         sc->sc_buffered_char[0] = sc->sc_buffered_char[1];
994                         sc->sc_buffered_char[1] = 0;
995                         return (scancode);
996                 }
997         }
998 #endif                                  /* UKBD_EMULATE_ATSCANCODE */
999
1000         /* see if there is something in the keyboard port */
1001         /* XXX */
1002         usbcode = ukbd_get_key(sc, (wait == FALSE) ? 0 : 1);
1003         if (usbcode == -1) {
1004                 return (NOKEY);
1005         }
1006         ++kbd->kb_count;
1007
1008 #ifdef UKBD_EMULATE_ATSCANCODE
1009         /* USB key index -> key code -> AT scan code */
1010         keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1011         if (keycode == NN) {
1012                 return (NOKEY);
1013         }
1014         /* return an AT scan code for the K_RAW mode */
1015         if (sc->sc_mode == K_RAW) {
1016                 return (ukbd_key2scan(sc, keycode, sc->sc_ndata.modifiers,
1017                     (usbcode & KEY_RELEASE)));
1018         }
1019 #else                                   /* !UKBD_EMULATE_ATSCANCODE */
1020
1021         /* return the byte as is for the K_RAW mode */
1022         if (sc->sc_mode == K_RAW) {
1023                 return (usbcode);
1024         }
1025         /* USB key index -> key code */
1026         keycode = ukbd_trtab[KEY_INDEX(usbcode)];
1027         if (keycode == NN) {
1028                 return (NOKEY);
1029         }
1030 #endif                                  /* UKBD_EMULATE_ATSCANCODE */
1031
1032         switch (keycode) {
1033         case 0x38:                      /* left alt (compose key) */
1034                 if (usbcode & KEY_RELEASE) {
1035                         if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1036                                 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1037
1038                                 if (sc->sc_composed_char > 0xFF) {
1039                                         sc->sc_composed_char = 0;
1040                                 }
1041                         }
1042                 } else {
1043                         if (!(sc->sc_flags & UKBD_FLAG_COMPOSE)) {
1044                                 sc->sc_flags |= UKBD_FLAG_COMPOSE;
1045                                 sc->sc_composed_char = 0;
1046                         }
1047                 }
1048                 break;
1049                 /* XXX: I don't like these... */
1050         case 0x5c:                      /* print screen */
1051                 if (sc->sc_flags & ALTS) {
1052                         keycode = 0x54; /* sysrq */
1053                 }
1054                 break;
1055         case 0x68:                      /* pause/break */
1056                 if (sc->sc_flags & CTLS) {
1057                         keycode = 0x6c; /* break */
1058                 }
1059                 break;
1060         }
1061
1062         /* return the key code in the K_CODE mode */
1063         if (usbcode & KEY_RELEASE) {
1064                 keycode |= SCAN_RELEASE;
1065         }
1066         if (sc->sc_mode == K_CODE) {
1067                 return (keycode);
1068         }
1069         /* compose a character code */
1070         if (sc->sc_flags & UKBD_FLAG_COMPOSE) {
1071                 switch (keycode) {
1072                         /* key pressed, process it */
1073                 case 0x47:
1074                 case 0x48:
1075                 case 0x49:              /* keypad 7,8,9 */
1076                         sc->sc_composed_char *= 10;
1077                         sc->sc_composed_char += keycode - 0x40;
1078                         goto check_composed;
1079
1080                 case 0x4B:
1081                 case 0x4C:
1082                 case 0x4D:              /* keypad 4,5,6 */
1083                         sc->sc_composed_char *= 10;
1084                         sc->sc_composed_char += keycode - 0x47;
1085                         goto check_composed;
1086
1087                 case 0x4F:
1088                 case 0x50:
1089                 case 0x51:              /* keypad 1,2,3 */
1090                         sc->sc_composed_char *= 10;
1091                         sc->sc_composed_char += keycode - 0x4E;
1092                         goto check_composed;
1093
1094                 case 0x52:              /* keypad 0 */
1095                         sc->sc_composed_char *= 10;
1096                         goto check_composed;
1097
1098                         /* key released, no interest here */
1099                 case SCAN_RELEASE | 0x47:
1100                 case SCAN_RELEASE | 0x48:
1101                 case SCAN_RELEASE | 0x49:       /* keypad 7,8,9 */
1102                 case SCAN_RELEASE | 0x4B:
1103                 case SCAN_RELEASE | 0x4C:
1104                 case SCAN_RELEASE | 0x4D:       /* keypad 4,5,6 */
1105                 case SCAN_RELEASE | 0x4F:
1106                 case SCAN_RELEASE | 0x50:
1107                 case SCAN_RELEASE | 0x51:       /* keypad 1,2,3 */
1108                 case SCAN_RELEASE | 0x52:       /* keypad 0 */
1109                         goto next_code;
1110
1111                 case 0x38:              /* left alt key */
1112                         break;
1113
1114                 default:
1115                         if (sc->sc_composed_char > 0) {
1116                                 sc->sc_flags &= ~UKBD_FLAG_COMPOSE;
1117                                 sc->sc_composed_char = 0;
1118                                 goto errkey;
1119                         }
1120                         break;
1121                 }
1122         }
1123         /* keycode to key action */
1124         action = genkbd_keyaction(kbd, SCAN_CHAR(keycode),
1125             (keycode & SCAN_RELEASE),
1126             &sc->sc_state, &sc->sc_accents);
1127         if (action == NOKEY) {
1128                 goto next_code;
1129         }
1130 done:
1131         return (action);
1132
1133 check_composed:
1134         if (sc->sc_composed_char <= 0xFF) {
1135                 goto next_code;
1136         }
1137 errkey:
1138         return (ERRKEY);
1139 }
1140
1141 /* some useful control functions */
1142 static int
1143 ukbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1144 {
1145         /* translate LED_XXX bits into the device specific bits */
1146         static const uint8_t ledmap[8] = {
1147                 0, 2, 1, 3, 4, 6, 5, 7,
1148         };
1149         struct ukbd_softc *sc = kbd->kb_data;
1150         int i;
1151
1152 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1153     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1154         int ival;
1155
1156 #endif
1157         if (!mtx_owned(&Giant)) {
1158                 /*
1159                  * XXX big problem: If scroll lock is pressed and "printf()"
1160                  * is called, the CPU will get here, to un-scroll lock the
1161                  * keyboard. But if "printf()" acquires the "Giant" lock,
1162                  * there will be a locking order reversal problem, so the
1163                  * keyboard system must get out of "Giant" first, before the
1164                  * CPU can proceed here ...
1165                  */
1166                 return (EINVAL);
1167         }
1168         mtx_assert(&Giant, MA_OWNED);
1169
1170         switch (cmd) {
1171         case KDGKBMODE:         /* get keyboard mode */
1172                 *(int *)arg = sc->sc_mode;
1173                 break;
1174 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1175     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1176         case _IO('K', 7):
1177                 ival = IOCPARM_IVAL(arg);
1178                 arg = (caddr_t)&ival;
1179                 /* FALLTHROUGH */
1180 #endif
1181         case KDSKBMODE:         /* set keyboard mode */
1182                 switch (*(int *)arg) {
1183                 case K_XLATE:
1184                         if (sc->sc_mode != K_XLATE) {
1185                                 /* make lock key state and LED state match */
1186                                 sc->sc_state &= ~LOCK_MASK;
1187                                 sc->sc_state |= KBD_LED_VAL(kbd);
1188                         }
1189                         /* FALLTHROUGH */
1190                 case K_RAW:
1191                 case K_CODE:
1192                         if (sc->sc_mode != *(int *)arg) {
1193                                 ukbd_clear_state(kbd);
1194                                 sc->sc_mode = *(int *)arg;
1195                         }
1196                         break;
1197                 default:
1198                         return (EINVAL);
1199                 }
1200                 break;
1201
1202         case KDGETLED:                  /* get keyboard LED */
1203                 *(int *)arg = KBD_LED_VAL(kbd);
1204                 break;
1205 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1206     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1207         case _IO('K', 66):
1208                 ival = IOCPARM_IVAL(arg);
1209                 arg = (caddr_t)&ival;
1210                 /* FALLTHROUGH */
1211 #endif
1212         case KDSETLED:                  /* set keyboard LED */
1213                 /* NOTE: lock key state in "sc_state" won't be changed */
1214                 if (*(int *)arg & ~LOCK_MASK) {
1215                         return (EINVAL);
1216                 }
1217                 i = *(int *)arg;
1218                 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
1219                 if (sc->sc_mode == K_XLATE &&
1220                     kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1221                         if (i & ALKED)
1222                                 i |= CLKED;
1223                         else
1224                                 i &= ~CLKED;
1225                 }
1226                 if (KBD_HAS_DEVICE(kbd)) {
1227                         ukbd_set_leds(sc, ledmap[i & LED_MASK]);
1228                 }
1229                 KBD_LED_VAL(kbd) = *(int *)arg;
1230                 break;
1231         case KDGKBSTATE:                /* get lock key state */
1232                 *(int *)arg = sc->sc_state & LOCK_MASK;
1233                 break;
1234 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1235     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1236         case _IO('K', 20):
1237                 ival = IOCPARM_IVAL(arg);
1238                 arg = (caddr_t)&ival;
1239                 /* FALLTHROUGH */
1240 #endif
1241         case KDSKBSTATE:                /* set lock key state */
1242                 if (*(int *)arg & ~LOCK_MASK) {
1243                         return (EINVAL);
1244                 }
1245                 sc->sc_state &= ~LOCK_MASK;
1246                 sc->sc_state |= *(int *)arg;
1247
1248                 /* set LEDs and quit */
1249                 return (ukbd_ioctl(kbd, KDSETLED, arg));
1250
1251         case KDSETREPEAT:               /* set keyboard repeat rate (new
1252                                          * interface) */
1253                 if (!KBD_HAS_DEVICE(kbd)) {
1254                         return (0);
1255                 }
1256                 if (((int *)arg)[1] < 0) {
1257                         return (EINVAL);
1258                 }
1259                 if (((int *)arg)[0] < 0) {
1260                         return (EINVAL);
1261                 }
1262                 if (((int *)arg)[0] < 200)      /* fastest possible value */
1263                         kbd->kb_delay1 = 200;
1264                 else
1265                         kbd->kb_delay1 = ((int *)arg)[0];
1266                 kbd->kb_delay2 = ((int *)arg)[1];
1267                 return (0);
1268
1269 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1270     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1271         case _IO('K', 67):
1272                 ival = IOCPARM_IVAL(arg);
1273                 arg = (caddr_t)&ival;
1274                 /* FALLTHROUGH */
1275 #endif
1276         case KDSETRAD:                  /* set keyboard repeat rate (old
1277                                          * interface) */
1278                 return (ukbd_set_typematic(kbd, *(int *)arg));
1279
1280         case PIO_KEYMAP:                /* set keyboard translation table */
1281         case PIO_KEYMAPENT:             /* set keyboard translation table
1282                                          * entry */
1283         case PIO_DEADKEYMAP:            /* set accent key translation table */
1284                 sc->sc_accents = 0;
1285                 /* FALLTHROUGH */
1286         default:
1287                 return (genkbd_commonioctl(kbd, cmd, arg));
1288         }
1289
1290         return (0);
1291 }
1292
1293 /* clear the internal state of the keyboard */
1294 static void
1295 ukbd_clear_state(keyboard_t *kbd)
1296 {
1297         struct ukbd_softc *sc = kbd->kb_data;
1298
1299         if (!mtx_owned(&Giant)) {
1300                 return;                 /* XXX */
1301         }
1302         mtx_assert(&Giant, MA_OWNED);
1303
1304         sc->sc_flags &= ~(UKBD_FLAG_COMPOSE | UKBD_FLAG_POLLING);
1305         sc->sc_state &= LOCK_MASK;      /* preserve locking key state */
1306         sc->sc_accents = 0;
1307         sc->sc_composed_char = 0;
1308 #ifdef UKBD_EMULATE_ATSCANCODE
1309         sc->sc_buffered_char[0] = 0;
1310         sc->sc_buffered_char[1] = 0;
1311 #endif
1312         bzero(&sc->sc_ndata, sizeof(sc->sc_ndata));
1313         bzero(&sc->sc_odata, sizeof(sc->sc_odata));
1314         bzero(&sc->sc_ntime, sizeof(sc->sc_ntime));
1315         bzero(&sc->sc_otime, sizeof(sc->sc_otime));
1316 }
1317
1318 /* save the internal state, not used */
1319 static int
1320 ukbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1321 {
1322         mtx_assert(&Giant, MA_OWNED);
1323         return (len == 0) ? 1 : -1;
1324 }
1325
1326 /* set the internal state, not used */
1327 static int
1328 ukbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1329 {
1330         mtx_assert(&Giant, MA_OWNED);
1331         return (EINVAL);
1332 }
1333
1334 static int
1335 ukbd_poll(keyboard_t *kbd, int on)
1336 {
1337         struct ukbd_softc *sc = kbd->kb_data;
1338
1339         if (!mtx_owned(&Giant)) {
1340                 return (0);             /* XXX */
1341         }
1342         mtx_assert(&Giant, MA_OWNED);
1343
1344         if (on) {
1345                 sc->sc_flags |= UKBD_FLAG_POLLING;
1346         } else {
1347                 sc->sc_flags &= ~UKBD_FLAG_POLLING;
1348         }
1349         return (0);
1350 }
1351
1352 /* local functions */
1353
1354 static void
1355 ukbd_set_leds(struct ukbd_softc *sc, uint8_t leds)
1356 {
1357         DPRINTF("leds=0x%02x\n", leds);
1358
1359         sc->sc_leds = leds;
1360         sc->sc_flags |= UKBD_FLAG_SET_LEDS;
1361
1362         /* start transfer, if not already started */
1363
1364         usb2_transfer_start(sc->sc_xfer[UKBD_CTRL_LED]);
1365 }
1366
1367 static int
1368 ukbd_set_typematic(keyboard_t *kbd, int code)
1369 {
1370         static const int delays[] = {250, 500, 750, 1000};
1371         static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
1372                 68, 76, 84, 92, 100, 110, 118, 126,
1373                 136, 152, 168, 184, 200, 220, 236, 252,
1374         272, 304, 336, 368, 400, 440, 472, 504};
1375
1376         if (code & ~0x7f) {
1377                 return (EINVAL);
1378         }
1379         kbd->kb_delay1 = delays[(code >> 5) & 3];
1380         kbd->kb_delay2 = rates[code & 0x1f];
1381         return (0);
1382 }
1383
1384 #ifdef UKBD_EMULATE_ATSCANCODE
1385 static int
1386 ukbd_key2scan(struct ukbd_softc *sc, int code, int shift, int up)
1387 {
1388         static const int scan[] = {
1389                 0x1c, 0x1d, 0x35,
1390                 0x37 | SCAN_PREFIX_SHIFT,       /* PrintScreen */
1391                 0x38, 0x47, 0x48, 0x49, 0x4b, 0x4d, 0x4f,
1392                 0x50, 0x51, 0x52, 0x53,
1393                 0x46,                   /* XXX Pause/Break */
1394                 0x5b, 0x5c, 0x5d,
1395                 /* SUN TYPE 6 USB KEYBOARD */
1396                 0x68, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
1397                 0x64, 0x65, 0x66, 0x67, 0x25, 0x1f, 0x1e,
1398                 0x20,
1399         };
1400
1401         if ((code >= 89) && (code < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
1402                 code = scan[code - 89] | SCAN_PREFIX_E0;
1403         }
1404         /* Pause/Break */
1405         if ((code == 104) && (!(shift & (MOD_CONTROL_L | MOD_CONTROL_R)))) {
1406                 code = (0x45 | SCAN_PREFIX_E1 | SCAN_PREFIX_CTL);
1407         }
1408         if (shift & (MOD_SHIFT_L | MOD_SHIFT_R)) {
1409                 code &= ~SCAN_PREFIX_SHIFT;
1410         }
1411         code |= (up ? SCAN_RELEASE : SCAN_PRESS);
1412
1413         if (code & SCAN_PREFIX) {
1414                 if (code & SCAN_PREFIX_CTL) {
1415                         /* Ctrl */
1416                         sc->sc_buffered_char[0] = (0x1d | (code & SCAN_RELEASE));
1417                         sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX);
1418                 } else if (code & SCAN_PREFIX_SHIFT) {
1419                         /* Shift */
1420                         sc->sc_buffered_char[0] = (0x2a | (code & SCAN_RELEASE));
1421                         sc->sc_buffered_char[1] = (code & ~SCAN_PREFIX_SHIFT);
1422                 } else {
1423                         sc->sc_buffered_char[0] = (code & ~SCAN_PREFIX);
1424                         sc->sc_buffered_char[1] = 0;
1425                 }
1426                 return ((code & SCAN_PREFIX_E0) ? 0xe0 : 0xe1);
1427         }
1428         return (code);
1429
1430 }
1431
1432 #endif                                  /* UKBD_EMULATE_ATSCANCODE */
1433
1434 keyboard_switch_t ukbdsw = {
1435         .probe = &ukbd__probe,
1436         .init = &ukbd_init,
1437         .term = &ukbd_term,
1438         .intr = &ukbd_intr,
1439         .test_if = &ukbd_test_if,
1440         .enable = &ukbd_enable,
1441         .disable = &ukbd_disable,
1442         .read = &ukbd_read,
1443         .check = &ukbd_check,
1444         .read_char = &ukbd_read_char,
1445         .check_char = &ukbd_check_char,
1446         .ioctl = &ukbd_ioctl,
1447         .lock = &ukbd_lock,
1448         .clear_state = &ukbd_clear_state,
1449         .get_state = &ukbd_get_state,
1450         .set_state = &ukbd_set_state,
1451         .get_fkeystr = &genkbd_get_fkeystr,
1452         .poll = &ukbd_poll,
1453         .diag = &genkbd_diag,
1454 };
1455
1456 KEYBOARD_DRIVER(ukbd, ukbdsw, ukbd_configure);
1457
1458 static int
1459 ukbd_driver_load(module_t mod, int what, void *arg)
1460 {
1461         switch (what) {
1462                 case MOD_LOAD:
1463                 kbd_add_driver(&ukbd_kbd_driver);
1464                 break;
1465         case MOD_UNLOAD:
1466                 kbd_delete_driver(&ukbd_kbd_driver);
1467                 break;
1468         }
1469         return (0);
1470 }
1471
1472 static devclass_t ukbd_devclass;
1473
1474 static device_method_t ukbd_methods[] = {
1475         DEVMETHOD(device_probe, ukbd_probe),
1476         DEVMETHOD(device_attach, ukbd_attach),
1477         DEVMETHOD(device_detach, ukbd_detach),
1478         DEVMETHOD(device_resume, ukbd_resume),
1479         {0, 0}
1480 };
1481
1482 static driver_t ukbd_driver = {
1483         .name = "ukbd",
1484         .methods = ukbd_methods,
1485         .size = sizeof(struct ukbd_softc),
1486 };
1487
1488 DRIVER_MODULE(ukbd, uhub, ukbd_driver, ukbd_devclass, ukbd_driver_load, 0);
1489 MODULE_DEPEND(ukbd, usb, 1, 1, 1);