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 * Copyright (c) 1998 The NetBSD Foundation, Inc.
12 * All rights reserved.
14 * This code is derived from software contributed to The NetBSD Foundation
15 * by Lennart Augustsson (lennart@augustsson.net) at
16 * Carlstedt Research & Technology.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the NetBSD
29 * Foundation, Inc. and its contributors.
30 * 4. Neither the name of The NetBSD Foundation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
48 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
71 #include <sys/fcntl.h>
74 #include <dev/usb/usb.h>
75 #include <dev/usb/usbdi.h>
76 #include <dev/usb/usbdi_util.h>
77 #include <dev/usb/usbhid.h>
78 #include <dev/usb/usb_ioctl.h>
80 #define USB_DEBUG_VAR uhid_debug
81 #include <dev/usb/usb_debug.h>
83 #include <dev/usb/input/usb_rdesc.h>
84 #include <dev/usb/quirk/usb_quirk.h>
87 static int uhid_debug = 0;
89 SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
90 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
91 &uhid_debug, 0, "Debug level");
94 #define UHID_BSIZE 1024 /* bytes, buffer size */
95 #define UHID_FRAME_NUM 50 /* bytes, frame number */
105 struct usb_fifo_sc sc_fifo;
108 struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
109 struct usb_device *sc_udev;
110 void *sc_repdesc_ptr;
116 uint16_t sc_repdesc_size;
119 uint8_t sc_iface_index;
124 #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */
125 #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are
129 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
130 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
131 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
135 static device_probe_t uhid_probe;
136 static device_attach_t uhid_attach;
137 static device_detach_t uhid_detach;
139 static usb_callback_t uhid_intr_callback;
140 static usb_callback_t uhid_write_callback;
141 static usb_callback_t uhid_read_callback;
143 static usb_fifo_cmd_t uhid_start_read;
144 static usb_fifo_cmd_t uhid_stop_read;
145 static usb_fifo_cmd_t uhid_start_write;
146 static usb_fifo_cmd_t uhid_stop_write;
147 static usb_fifo_open_t uhid_open;
148 static usb_fifo_close_t uhid_close;
149 static usb_fifo_ioctl_t uhid_ioctl;
151 static struct usb_fifo_methods uhid_fifo_methods = {
152 .f_open = &uhid_open,
153 .f_close = &uhid_close,
154 .f_ioctl = &uhid_ioctl,
155 .f_start_read = &uhid_start_read,
156 .f_stop_read = &uhid_stop_read,
157 .f_start_write = &uhid_start_write,
158 .f_stop_write = &uhid_stop_write,
159 .basename[0] = "uhid",
163 uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error)
165 struct uhid_softc *sc = usbd_xfer_softc(xfer);
166 struct usb_page_cache *pc;
169 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
171 switch (USB_GET_STATE(xfer)) {
172 case USB_ST_TRANSFERRED:
173 DPRINTF("transferred!\n");
175 pc = usbd_xfer_get_frame(xfer, 0);
176 if (actlen >= sc->sc_isize) {
177 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
181 DPRINTF("ignored short transfer, %d bytes\n", actlen);
186 if (usb_fifo_put_bytes_max(
187 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
188 usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
189 usbd_transfer_submit(xfer);
194 if (error != USB_ERR_CANCELLED) {
195 /* try to clear stall first */
196 usbd_xfer_set_stall(xfer);
204 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
205 uint8_t type, uint8_t id, uint16_t size)
207 req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
208 req->bRequest = UR_SET_REPORT;
209 USETW2(req->wValue, type, id);
210 req->wIndex[0] = iface_no;
212 USETW(req->wLength, size);
216 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
217 uint8_t type, uint8_t id, uint16_t size)
219 req->bmRequestType = UT_READ_CLASS_INTERFACE;
220 req->bRequest = UR_GET_REPORT;
221 USETW2(req->wValue, type, id);
222 req->wIndex[0] = iface_no;
224 USETW(req->wLength, size);
228 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
230 struct uhid_softc *sc = usbd_xfer_softc(xfer);
231 struct usb_device_request req;
232 struct usb_page_cache *pc;
233 uint32_t size = sc->sc_osize;
237 switch (USB_GET_STATE(xfer)) {
238 case USB_ST_TRANSFERRED:
240 /* try to extract the ID byte */
242 pc = usbd_xfer_get_frame(xfer, 0);
243 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
248 usbd_copy_out(pc, 0, &id, 1);
260 pc = usbd_xfer_get_frame(xfer, 1);
261 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
262 0, UHID_BSIZE, &actlen, 1)) {
263 if (actlen != size) {
267 (&req, sc->sc_iface_no,
268 UHID_OUTPUT_REPORT, id, size);
270 pc = usbd_xfer_get_frame(xfer, 0);
271 usbd_copy_in(pc, 0, &req, sizeof(req));
273 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
274 usbd_xfer_set_frame_len(xfer, 1, size);
275 usbd_xfer_set_frames(xfer, size ? 2 : 1);
276 usbd_transfer_submit(xfer);
283 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
289 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
291 struct uhid_softc *sc = usbd_xfer_softc(xfer);
292 struct usb_device_request req;
293 struct usb_page_cache *pc;
295 pc = usbd_xfer_get_frame(xfer, 0);
297 switch (USB_GET_STATE(xfer)) {
298 case USB_ST_TRANSFERRED:
299 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
305 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
308 (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
309 sc->sc_iid, sc->sc_isize);
311 usbd_copy_in(pc, 0, &req, sizeof(req));
313 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
314 usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
315 usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
316 usbd_transfer_submit(xfer);
322 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
327 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
329 [UHID_INTR_DT_RD] = {
330 .type = UE_INTERRUPT,
331 .endpoint = UE_ADDR_ANY,
332 .direction = UE_DIR_IN,
333 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
334 .bufsize = UHID_BSIZE,
335 .callback = &uhid_intr_callback,
338 [UHID_CTRL_DT_WR] = {
340 .endpoint = 0x00, /* Control pipe */
341 .direction = UE_DIR_ANY,
342 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
343 .callback = &uhid_write_callback,
344 .timeout = 1000, /* 1 second */
347 [UHID_CTRL_DT_RD] = {
349 .endpoint = 0x00, /* Control pipe */
350 .direction = UE_DIR_ANY,
351 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
352 .callback = &uhid_read_callback,
353 .timeout = 1000, /* 1 second */
358 uhid_start_read(struct usb_fifo *fifo)
360 struct uhid_softc *sc = usb_fifo_softc(fifo);
362 if (sc->sc_flags & UHID_FLAG_IMMED) {
363 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
365 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
370 uhid_stop_read(struct usb_fifo *fifo)
372 struct uhid_softc *sc = usb_fifo_softc(fifo);
374 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
375 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
379 uhid_start_write(struct usb_fifo *fifo)
381 struct uhid_softc *sc = usb_fifo_softc(fifo);
383 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
387 uhid_stop_write(struct usb_fifo *fifo)
389 struct uhid_softc *sc = usb_fifo_softc(fifo);
391 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
395 uhid_get_report(struct uhid_softc *sc, uint8_t type,
396 uint8_t id, void *kern_data, void *user_data,
400 uint8_t free_data = 0;
402 if (kern_data == NULL) {
403 kern_data = malloc(len, M_USBDEV, M_WAITOK);
404 if (kern_data == NULL) {
410 err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
411 len, sc->sc_iface_index, type, id);
418 err = copyout(kern_data, user_data, len);
425 free(kern_data, M_USBDEV);
431 uhid_set_report(struct uhid_softc *sc, uint8_t type,
432 uint8_t id, void *kern_data, void *user_data,
436 uint8_t free_data = 0;
438 if (kern_data == NULL) {
439 kern_data = malloc(len, M_USBDEV, M_WAITOK);
440 if (kern_data == NULL) {
445 err = copyin(user_data, kern_data, len);
450 err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
451 len, sc->sc_iface_index, type, id);
458 free(kern_data, M_USBDEV);
464 uhid_open(struct usb_fifo *fifo, int fflags)
466 struct uhid_softc *sc = usb_fifo_softc(fifo);
469 * The buffers are one byte larger than maximum so that one
470 * can detect too large read/writes and short transfers:
472 if (fflags & FREAD) {
474 sc->sc_flags &= ~UHID_FLAG_IMMED;
476 if (usb_fifo_alloc_buffer(fifo,
477 sc->sc_isize + 1, UHID_FRAME_NUM)) {
481 if (fflags & FWRITE) {
482 if (usb_fifo_alloc_buffer(fifo,
483 sc->sc_osize + 1, UHID_FRAME_NUM)) {
491 uhid_close(struct usb_fifo *fifo, int fflags)
493 if (fflags & (FREAD | FWRITE)) {
494 usb_fifo_free_buffer(fifo);
499 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
502 struct uhid_softc *sc = usb_fifo_softc(fifo);
503 struct usb_gen_descriptor *ugd;
509 case USB_GET_REPORT_DESC:
511 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
512 size = ugd->ugd_maxlen;
514 size = sc->sc_repdesc_size;
516 ugd->ugd_actlen = size;
517 if (ugd->ugd_data == NULL)
518 break; /* descriptor length only */
519 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
523 if (!(fflags & FREAD)) {
531 error = uhid_get_report(sc, UHID_INPUT_REPORT,
532 sc->sc_iid, NULL, NULL, sc->sc_isize);
536 mtx_lock(&sc->sc_mtx);
537 sc->sc_flags |= UHID_FLAG_IMMED;
538 mtx_unlock(&sc->sc_mtx);
540 mtx_lock(&sc->sc_mtx);
541 sc->sc_flags &= ~UHID_FLAG_IMMED;
542 mtx_unlock(&sc->sc_mtx);
547 if (!(fflags & FREAD)) {
552 switch (ugd->ugd_report_type) {
553 case UHID_INPUT_REPORT:
557 case UHID_OUTPUT_REPORT:
561 case UHID_FEATURE_REPORT:
568 error = uhid_get_report(sc, ugd->ugd_report_type, id,
569 NULL, ugd->ugd_data, size);
573 if (!(fflags & FWRITE)) {
578 switch (ugd->ugd_report_type) {
579 case UHID_INPUT_REPORT:
583 case UHID_OUTPUT_REPORT:
587 case UHID_FEATURE_REPORT:
594 error = uhid_set_report(sc, ugd->ugd_report_type, id,
595 NULL, ugd->ugd_data, size);
598 case USB_GET_REPORT_ID:
599 *(int *)addr = 0; /* XXX: we only support reportid 0? */
610 uhid_probe(device_t dev)
612 struct usb_attach_arg *uaa = device_get_ivars(dev);
616 if (uaa->usb_mode != USB_MODE_HOST) {
619 if (uaa->use_generic == 0) {
620 /* give Mouse and Keyboard drivers a try first */
623 if (uaa->info.bInterfaceClass != UICLASS_HID) {
625 /* the Xbox 360 gamepad doesn't use the HID class */
627 if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) ||
628 (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) ||
629 (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) {
633 if (usb_test_quirk(uaa, UQ_HID_IGNORE)) {
640 uhid_attach(device_t dev)
642 struct usb_attach_arg *uaa = device_get_ivars(dev);
643 struct uhid_softc *sc = device_get_softc(dev);
644 int unit = device_get_unit(dev);
647 DPRINTFN(10, "sc=%p\n", sc);
649 device_set_usb_desc(dev);
651 mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
653 sc->sc_udev = uaa->device;
655 sc->sc_iface_no = uaa->info.bIfaceNum;
656 sc->sc_iface_index = uaa->info.bIfaceIndex;
658 error = usbd_transfer_setup(uaa->device,
659 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
660 UHID_N_TRANSFER, sc, &sc->sc_mtx);
663 DPRINTF("error=%s\n", usbd_errstr(error));
666 if (uaa->info.idVendor == USB_VENDOR_WACOM) {
668 /* the report descriptor for the Wacom Graphire is broken */
670 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
672 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
673 sc->sc_repdesc_ptr = &uhid_graphire_report_descr;
674 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
676 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
678 static uint8_t reportbuf[] = {2, 2, 2};
681 * The Graphire3 needs 0x0202 to be written to
682 * feature report ID 2 before it'll start
683 * returning digitizer data.
685 error = usbd_req_set_report(uaa->device, NULL,
686 reportbuf, sizeof(reportbuf),
687 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
690 DPRINTF("set report failed, error=%s (ignored)\n",
693 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
694 sc->sc_repdesc_ptr = &uhid_graphire3_4x5_report_descr;
695 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
697 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
698 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
699 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
701 /* the Xbox 360 gamepad has no report descriptor */
702 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
703 sc->sc_repdesc_ptr = &uhid_xb360gp_report_descr;
704 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
706 if (sc->sc_repdesc_ptr == NULL) {
708 error = usbd_req_get_hid_desc(uaa->device, NULL,
709 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
710 M_USBDEV, uaa->info.bIfaceIndex);
713 device_printf(dev, "no report descriptor\n");
717 error = usbd_req_set_idle(uaa->device, NULL,
718 uaa->info.bIfaceIndex, 0, 0);
721 DPRINTF("set idle failed, error=%s (ignored)\n",
724 sc->sc_isize = hid_report_size
725 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
727 sc->sc_osize = hid_report_size
728 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
730 sc->sc_fsize = hid_report_size
731 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
733 if (sc->sc_isize > UHID_BSIZE) {
734 DPRINTF("input size is too large, "
735 "%d bytes (truncating)\n",
737 sc->sc_isize = UHID_BSIZE;
739 if (sc->sc_osize > UHID_BSIZE) {
740 DPRINTF("output size is too large, "
741 "%d bytes (truncating)\n",
743 sc->sc_osize = UHID_BSIZE;
745 if (sc->sc_fsize > UHID_BSIZE) {
746 DPRINTF("feature size is too large, "
747 "%d bytes (truncating)\n",
749 sc->sc_fsize = UHID_BSIZE;
752 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
753 &uhid_fifo_methods, &sc->sc_fifo,
754 unit, 0 - 1, uaa->info.bIfaceIndex,
755 UID_ROOT, GID_OPERATOR, 0644);
759 return (0); /* success */
767 uhid_detach(device_t dev)
769 struct uhid_softc *sc = device_get_softc(dev);
771 usb_fifo_detach(&sc->sc_fifo);
773 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
775 if (sc->sc_repdesc_ptr) {
776 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
777 free(sc->sc_repdesc_ptr, M_USBDEV);
780 mtx_destroy(&sc->sc_mtx);
785 static devclass_t uhid_devclass;
787 static device_method_t uhid_methods[] = {
788 DEVMETHOD(device_probe, uhid_probe),
789 DEVMETHOD(device_attach, uhid_attach),
790 DEVMETHOD(device_detach, uhid_detach),
794 static driver_t uhid_driver = {
796 .methods = uhid_methods,
797 .size = sizeof(struct uhid_softc),
800 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
801 MODULE_DEPEND(uhid, usb, 1, 1, 1);