1 /* $NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $ */
3 /* Also already merged from NetBSD:
4 * $NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $
11 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
13 * Copyright (c) 1998 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Lennart Augustsson (lennart@augustsson.net) at
18 * Carlstedt Research & Technology.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
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.
43 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
46 #include <sys/stdint.h>
47 #include <sys/stddef.h>
48 #include <sys/param.h>
49 #include <sys/queue.h>
50 #include <sys/types.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
65 #include <sys/fcntl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbhid.h>
72 #include <dev/usb/usb_ioctl.h>
74 #define USB_DEBUG_VAR uhid_debug
75 #include <dev/usb/usb_debug.h>
77 #include <dev/usb/input/usb_rdesc.h>
78 #include <dev/usb/quirk/usb_quirk.h>
81 static int uhid_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
84 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RWTUN,
85 &uhid_debug, 0, "Debug level");
88 #define UHID_BSIZE 1024 /* bytes, buffer size */
89 #define UHID_FRAME_NUM 50 /* bytes, frame number */
100 struct usb_fifo_sc sc_fifo;
103 struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
104 struct usb_device *sc_udev;
105 void *sc_repdesc_ptr;
111 uint16_t sc_repdesc_size;
114 uint8_t sc_iface_index;
119 #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */
120 #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are
124 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
125 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
126 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
130 static device_probe_t uhid_probe;
131 static device_attach_t uhid_attach;
132 static device_detach_t uhid_detach;
134 static usb_callback_t uhid_intr_write_callback;
135 static usb_callback_t uhid_intr_read_callback;
136 static usb_callback_t uhid_write_callback;
137 static usb_callback_t uhid_read_callback;
139 static usb_fifo_cmd_t uhid_start_read;
140 static usb_fifo_cmd_t uhid_stop_read;
141 static usb_fifo_cmd_t uhid_start_write;
142 static usb_fifo_cmd_t uhid_stop_write;
143 static usb_fifo_open_t uhid_open;
144 static usb_fifo_close_t uhid_close;
145 static usb_fifo_ioctl_t uhid_ioctl;
147 static struct usb_fifo_methods uhid_fifo_methods = {
148 .f_open = &uhid_open,
149 .f_close = &uhid_close,
150 .f_ioctl = &uhid_ioctl,
151 .f_start_read = &uhid_start_read,
152 .f_stop_read = &uhid_stop_read,
153 .f_start_write = &uhid_start_write,
154 .f_stop_write = &uhid_stop_write,
155 .basename[0] = "uhid",
159 uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
161 struct uhid_softc *sc = usbd_xfer_softc(xfer);
162 struct usb_page_cache *pc;
165 switch (USB_GET_STATE(xfer)) {
166 case USB_ST_TRANSFERRED:
169 pc = usbd_xfer_get_frame(xfer, 0);
170 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
171 0, usbd_xfer_max_len(xfer), &actlen, 0)) {
172 usbd_xfer_set_frame_len(xfer, 0, actlen);
173 usbd_transfer_submit(xfer);
178 if (error != USB_ERR_CANCELLED) {
179 /* try to clear stall first */
180 usbd_xfer_set_stall(xfer);
188 uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
190 struct uhid_softc *sc = usbd_xfer_softc(xfer);
191 struct usb_page_cache *pc;
194 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
196 switch (USB_GET_STATE(xfer)) {
197 case USB_ST_TRANSFERRED:
198 DPRINTF("transferred!\n");
200 pc = usbd_xfer_get_frame(xfer, 0);
203 * If the ID byte is non zero we allow descriptors
204 * having multiple sizes:
206 if ((actlen >= (int)sc->sc_isize) ||
207 ((actlen > 0) && (sc->sc_iid != 0))) {
208 /* limit report length to the maximum */
209 if (actlen > (int)sc->sc_isize)
210 actlen = sc->sc_isize;
211 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
215 DPRINTF("ignored transfer, %d bytes\n", actlen);
220 if (usb_fifo_put_bytes_max(
221 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
222 usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
223 usbd_transfer_submit(xfer);
228 if (error != USB_ERR_CANCELLED) {
229 /* try to clear stall first */
230 usbd_xfer_set_stall(xfer);
238 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
239 uint8_t type, uint8_t id, uint16_t size)
241 req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
242 req->bRequest = UR_SET_REPORT;
243 USETW2(req->wValue, type, id);
244 req->wIndex[0] = iface_no;
246 USETW(req->wLength, size);
250 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
251 uint8_t type, uint8_t id, uint16_t size)
253 req->bmRequestType = UT_READ_CLASS_INTERFACE;
254 req->bRequest = UR_GET_REPORT;
255 USETW2(req->wValue, type, id);
256 req->wIndex[0] = iface_no;
258 USETW(req->wLength, size);
262 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
264 struct uhid_softc *sc = usbd_xfer_softc(xfer);
265 struct usb_device_request req;
266 struct usb_page_cache *pc;
267 uint32_t size = sc->sc_osize;
271 switch (USB_GET_STATE(xfer)) {
272 case USB_ST_TRANSFERRED:
274 /* try to extract the ID byte */
276 pc = usbd_xfer_get_frame(xfer, 0);
277 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
282 usbd_copy_out(pc, 0, &id, 1);
294 pc = usbd_xfer_get_frame(xfer, 1);
295 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
296 0, UHID_BSIZE, &actlen, 1)) {
297 if (actlen != size) {
301 (&req, sc->sc_iface_no,
302 UHID_OUTPUT_REPORT, id, size);
304 pc = usbd_xfer_get_frame(xfer, 0);
305 usbd_copy_in(pc, 0, &req, sizeof(req));
307 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
308 usbd_xfer_set_frame_len(xfer, 1, size);
309 usbd_xfer_set_frames(xfer, size ? 2 : 1);
310 usbd_transfer_submit(xfer);
317 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
323 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
325 struct uhid_softc *sc = usbd_xfer_softc(xfer);
326 struct usb_device_request req;
327 struct usb_page_cache *pc;
329 pc = usbd_xfer_get_frame(xfer, 0);
331 switch (USB_GET_STATE(xfer)) {
332 case USB_ST_TRANSFERRED:
333 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
339 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
342 (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
343 sc->sc_iid, sc->sc_isize);
345 usbd_copy_in(pc, 0, &req, sizeof(req));
347 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
348 usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
349 usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
350 usbd_transfer_submit(xfer);
356 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
361 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
363 [UHID_INTR_DT_WR] = {
364 .type = UE_INTERRUPT,
365 .endpoint = UE_ADDR_ANY,
366 .direction = UE_DIR_OUT,
367 .flags = {.pipe_bof = 1,.no_pipe_ok = 1, },
368 .bufsize = UHID_BSIZE,
369 .callback = &uhid_intr_write_callback,
372 [UHID_INTR_DT_RD] = {
373 .type = UE_INTERRUPT,
374 .endpoint = UE_ADDR_ANY,
375 .direction = UE_DIR_IN,
376 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
377 .bufsize = UHID_BSIZE,
378 .callback = &uhid_intr_read_callback,
381 [UHID_CTRL_DT_WR] = {
383 .endpoint = 0x00, /* Control pipe */
384 .direction = UE_DIR_ANY,
385 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
386 .callback = &uhid_write_callback,
387 .timeout = 1000, /* 1 second */
390 [UHID_CTRL_DT_RD] = {
392 .endpoint = 0x00, /* Control pipe */
393 .direction = UE_DIR_ANY,
394 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
395 .callback = &uhid_read_callback,
396 .timeout = 1000, /* 1 second */
401 uhid_start_read(struct usb_fifo *fifo)
403 struct uhid_softc *sc = usb_fifo_softc(fifo);
405 if (sc->sc_flags & UHID_FLAG_IMMED) {
406 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
408 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
413 uhid_stop_read(struct usb_fifo *fifo)
415 struct uhid_softc *sc = usb_fifo_softc(fifo);
417 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
418 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
422 uhid_start_write(struct usb_fifo *fifo)
424 struct uhid_softc *sc = usb_fifo_softc(fifo);
426 if ((sc->sc_flags & UHID_FLAG_IMMED) ||
427 sc->sc_xfer[UHID_INTR_DT_WR] == NULL) {
428 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
430 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]);
435 uhid_stop_write(struct usb_fifo *fifo)
437 struct uhid_softc *sc = usb_fifo_softc(fifo);
439 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
440 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]);
444 uhid_get_report(struct uhid_softc *sc, uint8_t type,
445 uint8_t id, void *kern_data, void *user_data,
449 uint8_t free_data = 0;
451 if (kern_data == NULL) {
452 kern_data = malloc(len, M_USBDEV, M_WAITOK);
453 if (kern_data == NULL) {
459 err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
460 len, sc->sc_iface_index, type, id);
467 err = copyout(kern_data, user_data, len);
474 free(kern_data, M_USBDEV);
480 uhid_set_report(struct uhid_softc *sc, uint8_t type,
481 uint8_t id, void *kern_data, void *user_data,
485 uint8_t free_data = 0;
487 if (kern_data == NULL) {
488 kern_data = malloc(len, M_USBDEV, M_WAITOK);
489 if (kern_data == NULL) {
494 err = copyin(user_data, kern_data, len);
499 err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
500 len, sc->sc_iface_index, type, id);
507 free(kern_data, M_USBDEV);
513 uhid_open(struct usb_fifo *fifo, int fflags)
515 struct uhid_softc *sc = usb_fifo_softc(fifo);
518 * The buffers are one byte larger than maximum so that one
519 * can detect too large read/writes and short transfers:
521 if (fflags & FREAD) {
523 mtx_lock(&sc->sc_mtx);
524 sc->sc_flags &= ~UHID_FLAG_IMMED;
525 mtx_unlock(&sc->sc_mtx);
527 if (usb_fifo_alloc_buffer(fifo,
528 sc->sc_isize + 1, UHID_FRAME_NUM)) {
532 if (fflags & FWRITE) {
533 if (usb_fifo_alloc_buffer(fifo,
534 sc->sc_osize + 1, UHID_FRAME_NUM)) {
542 uhid_close(struct usb_fifo *fifo, int fflags)
544 if (fflags & (FREAD | FWRITE)) {
545 usb_fifo_free_buffer(fifo);
550 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
553 struct uhid_softc *sc = usb_fifo_softc(fifo);
554 struct usb_gen_descriptor *ugd;
560 case USB_GET_REPORT_DESC:
562 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
563 size = ugd->ugd_maxlen;
565 size = sc->sc_repdesc_size;
567 ugd->ugd_actlen = size;
568 if (ugd->ugd_data == NULL)
569 break; /* descriptor length only */
570 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
574 if (!(fflags & FREAD)) {
582 error = uhid_get_report(sc, UHID_INPUT_REPORT,
583 sc->sc_iid, NULL, NULL, sc->sc_isize);
587 mtx_lock(&sc->sc_mtx);
588 sc->sc_flags |= UHID_FLAG_IMMED;
589 mtx_unlock(&sc->sc_mtx);
591 mtx_lock(&sc->sc_mtx);
592 sc->sc_flags &= ~UHID_FLAG_IMMED;
593 mtx_unlock(&sc->sc_mtx);
598 if (!(fflags & FREAD)) {
603 switch (ugd->ugd_report_type) {
604 case UHID_INPUT_REPORT:
608 case UHID_OUTPUT_REPORT:
612 case UHID_FEATURE_REPORT:
620 copyin(ugd->ugd_data, &id, 1);
621 error = uhid_get_report(sc, ugd->ugd_report_type, id,
622 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
626 if (!(fflags & FWRITE)) {
631 switch (ugd->ugd_report_type) {
632 case UHID_INPUT_REPORT:
636 case UHID_OUTPUT_REPORT:
640 case UHID_FEATURE_REPORT:
648 copyin(ugd->ugd_data, &id, 1);
649 error = uhid_set_report(sc, ugd->ugd_report_type, id,
650 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
653 case USB_GET_REPORT_ID:
654 *(int *)addr = 0; /* XXX: we only support reportid 0? */
664 static const STRUCT_USB_HOST_ID uhid_devs[] = {
665 /* generic HID class */
666 {USB_IFACE_CLASS(UICLASS_HID),},
667 /* the Xbox 360 gamepad doesn't use the HID class */
668 {USB_IFACE_CLASS(UICLASS_VENDOR),
669 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
670 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
674 uhid_probe(device_t dev)
676 struct usb_attach_arg *uaa = device_get_ivars(dev);
683 if (uaa->usb_mode != USB_MODE_HOST)
686 error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
690 if (usb_test_quirk(uaa, UQ_HID_IGNORE))
694 * Don't attach to mouse and keyboard devices, hence then no
695 * "nomatch" event is generated and then ums and ukbd won't
696 * attach properly when loaded.
698 if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
699 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
700 (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) &&
701 !usb_test_quirk(uaa, UQ_KBD_IGNORE)) ||
702 ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) &&
703 !usb_test_quirk(uaa, UQ_UMS_IGNORE))))
706 /* Check for mandatory multitouch usages to give wmt(4) a chance */
707 if (!usb_test_quirk(uaa, UQ_WMT_IGNORE)) {
708 error = usbd_req_get_hid_desc(uaa->device, NULL,
709 &buf, &len, M_USBDEV, uaa->info.bIfaceIndex);
710 /* Let HID decscriptor-less devices to be handled at attach */
712 if (hid_locate(buf, len,
713 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX),
714 hid_feature, 0, NULL, NULL, NULL) &&
716 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
717 hid_input, 0, NULL, NULL, NULL)) {
725 return (BUS_PROBE_GENERIC);
729 uhid_attach(device_t dev)
731 struct usb_attach_arg *uaa = device_get_ivars(dev);
732 struct uhid_softc *sc = device_get_softc(dev);
733 int unit = device_get_unit(dev);
736 DPRINTFN(10, "sc=%p\n", sc);
738 device_set_usb_desc(dev);
740 mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
742 sc->sc_udev = uaa->device;
744 sc->sc_iface_no = uaa->info.bIfaceNum;
745 sc->sc_iface_index = uaa->info.bIfaceIndex;
747 error = usbd_transfer_setup(uaa->device,
748 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
749 UHID_N_TRANSFER, sc, &sc->sc_mtx);
752 DPRINTF("error=%s\n", usbd_errstr(error));
755 if (uaa->info.idVendor == USB_VENDOR_WACOM) {
757 /* the report descriptor for the Wacom Graphire is broken */
759 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
761 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
762 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr);
763 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
765 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
767 static uint8_t reportbuf[] = {2, 2, 2};
770 * The Graphire3 needs 0x0202 to be written to
771 * feature report ID 2 before it'll start
772 * returning digitizer data.
774 error = usbd_req_set_report(uaa->device, NULL,
775 reportbuf, sizeof(reportbuf),
776 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
779 DPRINTF("set report failed, error=%s (ignored)\n",
782 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
783 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr);
784 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
786 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
787 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
788 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
789 static const uint8_t reportbuf[3] = {1, 3, 0};
791 * Turn off the four LEDs on the gamepad which
792 * are blinking by default:
794 error = usbd_req_set_report(uaa->device, NULL,
795 __DECONST(void *, reportbuf), sizeof(reportbuf),
796 uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0);
798 DPRINTF("set output report failed, error=%s (ignored)\n",
801 /* the Xbox 360 gamepad has no report descriptor */
802 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
803 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr);
804 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
806 if (sc->sc_repdesc_ptr == NULL) {
808 error = usbd_req_get_hid_desc(uaa->device, NULL,
809 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
810 M_USBDEV, uaa->info.bIfaceIndex);
813 device_printf(dev, "no report descriptor\n");
817 error = usbd_req_set_idle(uaa->device, NULL,
818 uaa->info.bIfaceIndex, 0, 0);
821 DPRINTF("set idle failed, error=%s (ignored)\n",
824 sc->sc_isize = hid_report_size
825 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
827 sc->sc_osize = hid_report_size
828 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
830 sc->sc_fsize = hid_report_size
831 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
833 if (sc->sc_isize > UHID_BSIZE) {
834 DPRINTF("input size is too large, "
835 "%d bytes (truncating)\n",
837 sc->sc_isize = UHID_BSIZE;
839 if (sc->sc_osize > UHID_BSIZE) {
840 DPRINTF("output size is too large, "
841 "%d bytes (truncating)\n",
843 sc->sc_osize = UHID_BSIZE;
845 if (sc->sc_fsize > UHID_BSIZE) {
846 DPRINTF("feature size is too large, "
847 "%d bytes (truncating)\n",
849 sc->sc_fsize = UHID_BSIZE;
852 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
853 &uhid_fifo_methods, &sc->sc_fifo,
854 unit, -1, uaa->info.bIfaceIndex,
855 UID_ROOT, GID_OPERATOR, 0644);
859 return (0); /* success */
867 uhid_detach(device_t dev)
869 struct uhid_softc *sc = device_get_softc(dev);
871 usb_fifo_detach(&sc->sc_fifo);
873 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
875 if (sc->sc_repdesc_ptr) {
876 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
877 free(sc->sc_repdesc_ptr, M_USBDEV);
880 mtx_destroy(&sc->sc_mtx);
885 static devclass_t uhid_devclass;
887 static device_method_t uhid_methods[] = {
888 DEVMETHOD(device_probe, uhid_probe),
889 DEVMETHOD(device_attach, uhid_attach),
890 DEVMETHOD(device_detach, uhid_detach),
895 static driver_t uhid_driver = {
897 .methods = uhid_methods,
898 .size = sizeof(struct uhid_softc),
901 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
902 MODULE_DEPEND(uhid, usb, 1, 1, 1);
903 MODULE_VERSION(uhid, 1);
904 USB_PNP_HOST_INFO(uhid_devs);