929227b09c5207d47ddb671d17029bd30c851234
[dragonfly.git] / sys / bus / u4b / input / uhid.c
1 /*      $NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $  */
2
3 /* Also already merged from NetBSD:
4  *      $NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $
5  */
6
7 #include <sys/cdefs.h>
8 __FBSDID("$FreeBSD$");
9
10 /*-
11  * Copyright (c) 1998 The NetBSD Foundation, Inc.
12  * All rights reserved.
13  *
14  * This code is derived from software contributed to The NetBSD Foundation
15  * by Lennart Augustsson (lennart@augustsson.net) at
16  * Carlstedt Research & Technology.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 /*
41  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
42  */
43
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <sys/module.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sx.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
61 #include <sys/priv.h>
62 #include <sys/conf.h>
63 #include <sys/fcntl.h>
64
65 #include "usbdevs.h"
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
70 #include <dev/usb/usb_ioctl.h>
71
72 #define USB_DEBUG_VAR uhid_debug
73 #include <dev/usb/usb_debug.h>
74
75 #include <dev/usb/input/usb_rdesc.h>
76 #include <dev/usb/quirk/usb_quirk.h>
77
78 #ifdef USB_DEBUG
79 static int uhid_debug = 0;
80
81 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
82 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
83     &uhid_debug, 0, "Debug level");
84 #endif
85
86 #define UHID_BSIZE      1024            /* bytes, buffer size */
87 #define UHID_FRAME_NUM    50            /* bytes, frame number */
88
89 enum {
90         UHID_INTR_DT_RD,
91         UHID_CTRL_DT_WR,
92         UHID_CTRL_DT_RD,
93         UHID_N_TRANSFER,
94 };
95
96 struct uhid_softc {
97         struct usb_fifo_sc sc_fifo;
98         struct mtx sc_mtx;
99
100         struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
101         struct usb_device *sc_udev;
102         void   *sc_repdesc_ptr;
103
104         uint32_t sc_isize;
105         uint32_t sc_osize;
106         uint32_t sc_fsize;
107
108         uint16_t sc_repdesc_size;
109
110         uint8_t sc_iface_no;
111         uint8_t sc_iface_index;
112         uint8_t sc_iid;
113         uint8_t sc_oid;
114         uint8_t sc_fid;
115         uint8_t sc_flags;
116 #define UHID_FLAG_IMMED        0x01     /* set if read should be immediate */
117 #define UHID_FLAG_STATIC_DESC  0x04     /* set if report descriptors are
118                                          * static */
119 };
120
121 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
122 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
123 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
124
125 /* prototypes */
126
127 static device_probe_t uhid_probe;
128 static device_attach_t uhid_attach;
129 static device_detach_t uhid_detach;
130
131 static usb_callback_t uhid_intr_callback;
132 static usb_callback_t uhid_write_callback;
133 static usb_callback_t uhid_read_callback;
134
135 static usb_fifo_cmd_t uhid_start_read;
136 static usb_fifo_cmd_t uhid_stop_read;
137 static usb_fifo_cmd_t uhid_start_write;
138 static usb_fifo_cmd_t uhid_stop_write;
139 static usb_fifo_open_t uhid_open;
140 static usb_fifo_close_t uhid_close;
141 static usb_fifo_ioctl_t uhid_ioctl;
142
143 static struct usb_fifo_methods uhid_fifo_methods = {
144         .f_open = &uhid_open,
145         .f_close = &uhid_close,
146         .f_ioctl = &uhid_ioctl,
147         .f_start_read = &uhid_start_read,
148         .f_stop_read = &uhid_stop_read,
149         .f_start_write = &uhid_start_write,
150         .f_stop_write = &uhid_stop_write,
151         .basename[0] = "uhid",
152 };
153
154 static void
155 uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error)
156 {
157         struct uhid_softc *sc = usbd_xfer_softc(xfer);
158         struct usb_page_cache *pc;
159         int actlen;
160
161         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
162
163         switch (USB_GET_STATE(xfer)) {
164         case USB_ST_TRANSFERRED:
165                 DPRINTF("transferred!\n");
166
167                 pc = usbd_xfer_get_frame(xfer, 0);
168
169                 /* 
170                  * If the ID byte is non zero we allow descriptors
171                  * having multiple sizes:
172                  */
173                 if ((actlen >= sc->sc_isize) ||
174                     ((actlen > 0) && (sc->sc_iid != 0))) {
175                         /* limit report length to the maximum */
176                         if (actlen > sc->sc_isize)
177                                 actlen = sc->sc_isize;
178                         usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
179                             0, actlen, 1);
180                 } else {
181                         /* ignore it */
182                         DPRINTF("ignored transfer, %d bytes\n", actlen);
183                 }
184
185         case USB_ST_SETUP:
186 re_submit:
187                 if (usb_fifo_put_bytes_max(
188                     sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
189                         usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
190                         usbd_transfer_submit(xfer);
191                 }
192                 return;
193
194         default:                        /* Error */
195                 if (error != USB_ERR_CANCELLED) {
196                         /* try to clear stall first */
197                         usbd_xfer_set_stall(xfer);
198                         goto re_submit;
199                 }
200                 return;
201         }
202 }
203
204 static void
205 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
206     uint8_t type, uint8_t id, uint16_t size)
207 {
208         req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
209         req->bRequest = UR_SET_REPORT;
210         USETW2(req->wValue, type, id);
211         req->wIndex[0] = iface_no;
212         req->wIndex[1] = 0;
213         USETW(req->wLength, size);
214 }
215
216 static void
217 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
218     uint8_t type, uint8_t id, uint16_t size)
219 {
220         req->bmRequestType = UT_READ_CLASS_INTERFACE;
221         req->bRequest = UR_GET_REPORT;
222         USETW2(req->wValue, type, id);
223         req->wIndex[0] = iface_no;
224         req->wIndex[1] = 0;
225         USETW(req->wLength, size);
226 }
227
228 static void
229 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
230 {
231         struct uhid_softc *sc = usbd_xfer_softc(xfer);
232         struct usb_device_request req;
233         struct usb_page_cache *pc;
234         uint32_t size = sc->sc_osize;
235         uint32_t actlen;
236         uint8_t id;
237
238         switch (USB_GET_STATE(xfer)) {
239         case USB_ST_TRANSFERRED:
240         case USB_ST_SETUP:
241                 /* try to extract the ID byte */
242                 if (sc->sc_oid) {
243                         pc = usbd_xfer_get_frame(xfer, 0);
244                         if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
245                             0, 1, &actlen, 0)) {
246                                 if (actlen != 1) {
247                                         goto tr_error;
248                                 }
249                                 usbd_copy_out(pc, 0, &id, 1);
250
251                         } else {
252                                 return;
253                         }
254                         if (size) {
255                                 size--;
256                         }
257                 } else {
258                         id = 0;
259                 }
260
261                 pc = usbd_xfer_get_frame(xfer, 1);
262                 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
263                     0, UHID_BSIZE, &actlen, 1)) {
264                         if (actlen != size) {
265                                 goto tr_error;
266                         }
267                         uhid_fill_set_report
268                             (&req, sc->sc_iface_no,
269                             UHID_OUTPUT_REPORT, id, size);
270
271                         pc = usbd_xfer_get_frame(xfer, 0);
272                         usbd_copy_in(pc, 0, &req, sizeof(req));
273
274                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
275                         usbd_xfer_set_frame_len(xfer, 1, size);
276                         usbd_xfer_set_frames(xfer, size ? 2 : 1);
277                         usbd_transfer_submit(xfer);
278                 }
279                 return;
280
281         default:
282 tr_error:
283                 /* bomb out */
284                 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
285                 return;
286         }
287 }
288
289 static void
290 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
291 {
292         struct uhid_softc *sc = usbd_xfer_softc(xfer);
293         struct usb_device_request req;
294         struct usb_page_cache *pc;
295
296         pc = usbd_xfer_get_frame(xfer, 0);
297
298         switch (USB_GET_STATE(xfer)) {
299         case USB_ST_TRANSFERRED:
300                 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
301                     sc->sc_isize, 1);
302                 return;
303
304         case USB_ST_SETUP:
305
306                 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
307
308                         uhid_fill_get_report
309                             (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
310                             sc->sc_iid, sc->sc_isize);
311
312                         usbd_copy_in(pc, 0, &req, sizeof(req));
313
314                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
315                         usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
316                         usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
317                         usbd_transfer_submit(xfer);
318                 }
319                 return;
320
321         default:                        /* Error */
322                 /* bomb out */
323                 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
324                 return;
325         }
326 }
327
328 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
329
330         [UHID_INTR_DT_RD] = {
331                 .type = UE_INTERRUPT,
332                 .endpoint = UE_ADDR_ANY,
333                 .direction = UE_DIR_IN,
334                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
335                 .bufsize = UHID_BSIZE,
336                 .callback = &uhid_intr_callback,
337         },
338
339         [UHID_CTRL_DT_WR] = {
340                 .type = UE_CONTROL,
341                 .endpoint = 0x00,       /* Control pipe */
342                 .direction = UE_DIR_ANY,
343                 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
344                 .callback = &uhid_write_callback,
345                 .timeout = 1000,        /* 1 second */
346         },
347
348         [UHID_CTRL_DT_RD] = {
349                 .type = UE_CONTROL,
350                 .endpoint = 0x00,       /* Control pipe */
351                 .direction = UE_DIR_ANY,
352                 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
353                 .callback = &uhid_read_callback,
354                 .timeout = 1000,        /* 1 second */
355         },
356 };
357
358 static void
359 uhid_start_read(struct usb_fifo *fifo)
360 {
361         struct uhid_softc *sc = usb_fifo_softc(fifo);
362
363         if (sc->sc_flags & UHID_FLAG_IMMED) {
364                 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
365         } else {
366                 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
367         }
368 }
369
370 static void
371 uhid_stop_read(struct usb_fifo *fifo)
372 {
373         struct uhid_softc *sc = usb_fifo_softc(fifo);
374
375         usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
376         usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
377 }
378
379 static void
380 uhid_start_write(struct usb_fifo *fifo)
381 {
382         struct uhid_softc *sc = usb_fifo_softc(fifo);
383
384         usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
385 }
386
387 static void
388 uhid_stop_write(struct usb_fifo *fifo)
389 {
390         struct uhid_softc *sc = usb_fifo_softc(fifo);
391
392         usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
393 }
394
395 static int
396 uhid_get_report(struct uhid_softc *sc, uint8_t type,
397     uint8_t id, void *kern_data, void *user_data,
398     uint16_t len)
399 {
400         int err;
401         uint8_t free_data = 0;
402
403         if (kern_data == NULL) {
404                 kern_data = malloc(len, M_USBDEV, M_WAITOK);
405                 if (kern_data == NULL) {
406                         err = ENOMEM;
407                         goto done;
408                 }
409                 free_data = 1;
410         }
411         err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
412             len, sc->sc_iface_index, type, id);
413         if (err) {
414                 err = ENXIO;
415                 goto done;
416         }
417         if (user_data) {
418                 /* dummy buffer */
419                 err = copyout(kern_data, user_data, len);
420                 if (err) {
421                         goto done;
422                 }
423         }
424 done:
425         if (free_data) {
426                 free(kern_data, M_USBDEV);
427         }
428         return (err);
429 }
430
431 static int
432 uhid_set_report(struct uhid_softc *sc, uint8_t type,
433     uint8_t id, void *kern_data, void *user_data,
434     uint16_t len)
435 {
436         int err;
437         uint8_t free_data = 0;
438
439         if (kern_data == NULL) {
440                 kern_data = malloc(len, M_USBDEV, M_WAITOK);
441                 if (kern_data == NULL) {
442                         err = ENOMEM;
443                         goto done;
444                 }
445                 free_data = 1;
446                 err = copyin(user_data, kern_data, len);
447                 if (err) {
448                         goto done;
449                 }
450         }
451         err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
452             len, sc->sc_iface_index, type, id);
453         if (err) {
454                 err = ENXIO;
455                 goto done;
456         }
457 done:
458         if (free_data) {
459                 free(kern_data, M_USBDEV);
460         }
461         return (err);
462 }
463
464 static int
465 uhid_open(struct usb_fifo *fifo, int fflags)
466 {
467         struct uhid_softc *sc = usb_fifo_softc(fifo);
468
469         /*
470          * The buffers are one byte larger than maximum so that one
471          * can detect too large read/writes and short transfers:
472          */
473         if (fflags & FREAD) {
474                 /* reset flags */
475                 sc->sc_flags &= ~UHID_FLAG_IMMED;
476
477                 if (usb_fifo_alloc_buffer(fifo,
478                     sc->sc_isize + 1, UHID_FRAME_NUM)) {
479                         return (ENOMEM);
480                 }
481         }
482         if (fflags & FWRITE) {
483                 if (usb_fifo_alloc_buffer(fifo,
484                     sc->sc_osize + 1, UHID_FRAME_NUM)) {
485                         return (ENOMEM);
486                 }
487         }
488         return (0);
489 }
490
491 static void
492 uhid_close(struct usb_fifo *fifo, int fflags)
493 {
494         if (fflags & (FREAD | FWRITE)) {
495                 usb_fifo_free_buffer(fifo);
496         }
497 }
498
499 static int
500 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
501     int fflags)
502 {
503         struct uhid_softc *sc = usb_fifo_softc(fifo);
504         struct usb_gen_descriptor *ugd;
505         uint32_t size;
506         int error = 0;
507         uint8_t id;
508
509         switch (cmd) {
510         case USB_GET_REPORT_DESC:
511                 ugd = addr;
512                 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
513                         size = ugd->ugd_maxlen;
514                 } else {
515                         size = sc->sc_repdesc_size;
516                 }
517                 ugd->ugd_actlen = size;
518                 if (ugd->ugd_data == NULL)
519                         break;          /* descriptor length only */
520                 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
521                 break;
522
523         case USB_SET_IMMED:
524                 if (!(fflags & FREAD)) {
525                         error = EPERM;
526                         break;
527                 }
528                 if (*(int *)addr) {
529
530                         /* do a test read */
531
532                         error = uhid_get_report(sc, UHID_INPUT_REPORT,
533                             sc->sc_iid, NULL, NULL, sc->sc_isize);
534                         if (error) {
535                                 break;
536                         }
537                         mtx_lock(&sc->sc_mtx);
538                         sc->sc_flags |= UHID_FLAG_IMMED;
539                         mtx_unlock(&sc->sc_mtx);
540                 } else {
541                         mtx_lock(&sc->sc_mtx);
542                         sc->sc_flags &= ~UHID_FLAG_IMMED;
543                         mtx_unlock(&sc->sc_mtx);
544                 }
545                 break;
546
547         case USB_GET_REPORT:
548                 if (!(fflags & FREAD)) {
549                         error = EPERM;
550                         break;
551                 }
552                 ugd = addr;
553                 switch (ugd->ugd_report_type) {
554                 case UHID_INPUT_REPORT:
555                         size = sc->sc_isize;
556                         id = sc->sc_iid;
557                         break;
558                 case UHID_OUTPUT_REPORT:
559                         size = sc->sc_osize;
560                         id = sc->sc_oid;
561                         break;
562                 case UHID_FEATURE_REPORT:
563                         size = sc->sc_fsize;
564                         id = sc->sc_fid;
565                         break;
566                 default:
567                         return (EINVAL);
568                 }
569                 if (id != 0)
570                         copyin(ugd->ugd_data, &id, 1);
571                 error = uhid_get_report(sc, ugd->ugd_report_type, id,
572                     NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
573                 break;
574
575         case USB_SET_REPORT:
576                 if (!(fflags & FWRITE)) {
577                         error = EPERM;
578                         break;
579                 }
580                 ugd = addr;
581                 switch (ugd->ugd_report_type) {
582                 case UHID_INPUT_REPORT:
583                         size = sc->sc_isize;
584                         id = sc->sc_iid;
585                         break;
586                 case UHID_OUTPUT_REPORT:
587                         size = sc->sc_osize;
588                         id = sc->sc_oid;
589                         break;
590                 case UHID_FEATURE_REPORT:
591                         size = sc->sc_fsize;
592                         id = sc->sc_fid;
593                         break;
594                 default:
595                         return (EINVAL);
596                 }
597                 if (id != 0)
598                         copyin(ugd->ugd_data, &id, 1);
599                 error = uhid_set_report(sc, ugd->ugd_report_type, id,
600                     NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
601                 break;
602
603         case USB_GET_REPORT_ID:
604                 *(int *)addr = 0;       /* XXX: we only support reportid 0? */
605                 break;
606
607         default:
608                 error = EINVAL;
609                 break;
610         }
611         return (error);
612 }
613
614 static const STRUCT_USB_HOST_ID uhid_devs[] = {
615         /* generic HID class */
616         {USB_IFACE_CLASS(UICLASS_HID),},
617         /* the Xbox 360 gamepad doesn't use the HID class */
618         {USB_IFACE_CLASS(UICLASS_VENDOR),
619          USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
620          USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
621 };
622
623 static int
624 uhid_probe(device_t dev)
625 {
626         struct usb_attach_arg *uaa = device_get_ivars(dev);
627         int error;
628
629         DPRINTFN(11, "\n");
630
631         if (uaa->usb_mode != USB_MODE_HOST)
632                 return (ENXIO);
633
634         error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
635         if (error)
636                 return (error);
637
638         if (usb_test_quirk(uaa, UQ_HID_IGNORE))
639                 return (ENXIO);
640
641         /*
642          * Don't attach to mouse and keyboard devices, hence then no
643          * "nomatch" event is generated and then ums and ukbd won't
644          * attach properly when loaded.
645          */
646         if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
647             (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
648             ((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) ||
649              (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))) {
650                 return (ENXIO);
651         }
652
653         return (BUS_PROBE_GENERIC);
654 }
655
656 static int
657 uhid_attach(device_t dev)
658 {
659         struct usb_attach_arg *uaa = device_get_ivars(dev);
660         struct uhid_softc *sc = device_get_softc(dev);
661         int unit = device_get_unit(dev);
662         int error = 0;
663
664         DPRINTFN(10, "sc=%p\n", sc);
665
666         device_set_usb_desc(dev);
667
668         mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
669
670         sc->sc_udev = uaa->device;
671
672         sc->sc_iface_no = uaa->info.bIfaceNum;
673         sc->sc_iface_index = uaa->info.bIfaceIndex;
674
675         error = usbd_transfer_setup(uaa->device,
676             &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
677             UHID_N_TRANSFER, sc, &sc->sc_mtx);
678
679         if (error) {
680                 DPRINTF("error=%s\n", usbd_errstr(error));
681                 goto detach;
682         }
683         if (uaa->info.idVendor == USB_VENDOR_WACOM) {
684
685                 /* the report descriptor for the Wacom Graphire is broken */
686
687                 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
688
689                         sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
690                         sc->sc_repdesc_ptr = (void *)&uhid_graphire_report_descr;
691                         sc->sc_flags |= UHID_FLAG_STATIC_DESC;
692
693                 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
694
695                         static uint8_t reportbuf[] = {2, 2, 2};
696
697                         /*
698                          * The Graphire3 needs 0x0202 to be written to
699                          * feature report ID 2 before it'll start
700                          * returning digitizer data.
701                          */
702                         error = usbd_req_set_report(uaa->device, NULL,
703                             reportbuf, sizeof(reportbuf),
704                             uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
705
706                         if (error) {
707                                 DPRINTF("set report failed, error=%s (ignored)\n",
708                                     usbd_errstr(error));
709                         }
710                         sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
711                         sc->sc_repdesc_ptr = (void *)&uhid_graphire3_4x5_report_descr;
712                         sc->sc_flags |= UHID_FLAG_STATIC_DESC;
713                 }
714         } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
715                     (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
716             (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
717
718                 /* the Xbox 360 gamepad has no report descriptor */
719                 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
720                 sc->sc_repdesc_ptr = (void *)&uhid_xb360gp_report_descr;
721                 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
722         }
723         if (sc->sc_repdesc_ptr == NULL) {
724
725                 error = usbd_req_get_hid_desc(uaa->device, NULL,
726                     &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
727                     M_USBDEV, uaa->info.bIfaceIndex);
728
729                 if (error) {
730                         device_printf(dev, "no report descriptor\n");
731                         goto detach;
732                 }
733         }
734         error = usbd_req_set_idle(uaa->device, NULL,
735             uaa->info.bIfaceIndex, 0, 0);
736
737         if (error) {
738                 DPRINTF("set idle failed, error=%s (ignored)\n",
739                     usbd_errstr(error));
740         }
741         sc->sc_isize = hid_report_size
742             (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
743
744         sc->sc_osize = hid_report_size
745             (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
746
747         sc->sc_fsize = hid_report_size
748             (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
749
750         if (sc->sc_isize > UHID_BSIZE) {
751                 DPRINTF("input size is too large, "
752                     "%d bytes (truncating)\n",
753                     sc->sc_isize);
754                 sc->sc_isize = UHID_BSIZE;
755         }
756         if (sc->sc_osize > UHID_BSIZE) {
757                 DPRINTF("output size is too large, "
758                     "%d bytes (truncating)\n",
759                     sc->sc_osize);
760                 sc->sc_osize = UHID_BSIZE;
761         }
762         if (sc->sc_fsize > UHID_BSIZE) {
763                 DPRINTF("feature size is too large, "
764                     "%d bytes (truncating)\n",
765                     sc->sc_fsize);
766                 sc->sc_fsize = UHID_BSIZE;
767         }
768
769         error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
770             &uhid_fifo_methods, &sc->sc_fifo,
771             unit, 0 - 1, uaa->info.bIfaceIndex,
772             UID_ROOT, GID_OPERATOR, 0644);
773         if (error) {
774                 goto detach;
775         }
776         return (0);                     /* success */
777
778 detach:
779         uhid_detach(dev);
780         return (ENOMEM);
781 }
782
783 static int
784 uhid_detach(device_t dev)
785 {
786         struct uhid_softc *sc = device_get_softc(dev);
787
788         usb_fifo_detach(&sc->sc_fifo);
789
790         usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
791
792         if (sc->sc_repdesc_ptr) {
793                 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
794                         free(sc->sc_repdesc_ptr, M_USBDEV);
795                 }
796         }
797         mtx_destroy(&sc->sc_mtx);
798
799         return (0);
800 }
801
802 static devclass_t uhid_devclass;
803
804 static device_method_t uhid_methods[] = {
805         DEVMETHOD(device_probe, uhid_probe),
806         DEVMETHOD(device_attach, uhid_attach),
807         DEVMETHOD(device_detach, uhid_detach),
808         {0, 0}
809 };
810
811 static driver_t uhid_driver = {
812         .name = "uhid",
813         .methods = uhid_methods,
814         .size = sizeof(struct uhid_softc),
815 };
816
817 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
818 MODULE_DEPEND(uhid, usb, 1, 1, 1);
819 MODULE_VERSION(uhid, 1);