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
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_mfunc.h>
54 #include <dev/usb/usb_error.h>
55 #include <dev/usb/usbhid.h>
56 #include <dev/usb/usb_ioctl.h>
58 #define USB_DEBUG_VAR uhid_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_util.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_busdma.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_transfer.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_mbuf.h>
69 #include <dev/usb/usb_dev.h>
70 #include <dev/usb/usb_hid.h>
72 #include <dev/usb/input/usb_rdesc.h>
74 #include <dev/usb/quirk/usb_quirk.h>
77 static int uhid_debug = 0;
79 SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
80 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
81 &uhid_debug, 0, "Debug level");
84 #define UHID_BSIZE 1024 /* bytes, buffer size */
85 #define UHID_FRAME_NUM 50 /* bytes, frame number */
95 struct usb_fifo_sc sc_fifo;
98 struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
99 struct usb_device *sc_udev;
100 void *sc_repdesc_ptr;
106 uint16_t sc_repdesc_size;
109 uint8_t sc_iface_index;
114 #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */
115 #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are
119 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
120 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
121 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
125 static device_probe_t uhid_probe;
126 static device_attach_t uhid_attach;
127 static device_detach_t uhid_detach;
129 static usb_callback_t uhid_intr_callback;
130 static usb_callback_t uhid_write_callback;
131 static usb_callback_t uhid_read_callback;
133 static usb_fifo_cmd_t uhid_start_read;
134 static usb_fifo_cmd_t uhid_stop_read;
135 static usb_fifo_cmd_t uhid_start_write;
136 static usb_fifo_cmd_t uhid_stop_write;
137 static usb_fifo_open_t uhid_open;
138 static usb_fifo_close_t uhid_close;
139 static usb_fifo_ioctl_t uhid_ioctl;
141 static struct usb_fifo_methods uhid_fifo_methods = {
142 .f_open = &uhid_open,
143 .f_close = &uhid_close,
144 .f_ioctl = &uhid_ioctl,
145 .f_start_read = &uhid_start_read,
146 .f_stop_read = &uhid_stop_read,
147 .f_start_write = &uhid_start_write,
148 .f_stop_write = &uhid_stop_write,
149 .basename[0] = "uhid",
153 uhid_intr_callback(struct usb_xfer *xfer)
155 struct uhid_softc *sc = xfer->priv_sc;
157 switch (USB_GET_STATE(xfer)) {
158 case USB_ST_TRANSFERRED:
159 DPRINTF("transferred!\n");
161 if (xfer->actlen >= sc->sc_isize) {
163 sc->sc_fifo.fp[USB_FIFO_RX],
168 DPRINTF("ignored short transfer, "
169 "%d bytes\n", xfer->actlen);
174 if (usb2_fifo_put_bytes_max(
175 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
176 xfer->frlengths[0] = sc->sc_isize;
177 usb2_start_hardware(xfer);
182 if (xfer->error != USB_ERR_CANCELLED) {
183 /* try to clear stall first */
184 xfer->flags.stall_pipe = 1;
192 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
193 uint8_t type, uint8_t id, uint16_t size)
195 req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
196 req->bRequest = UR_SET_REPORT;
197 USETW2(req->wValue, type, id);
198 req->wIndex[0] = iface_no;
200 USETW(req->wLength, size);
204 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
205 uint8_t type, uint8_t id, uint16_t size)
207 req->bmRequestType = UT_READ_CLASS_INTERFACE;
208 req->bRequest = UR_GET_REPORT;
209 USETW2(req->wValue, type, id);
210 req->wIndex[0] = iface_no;
212 USETW(req->wLength, size);
216 uhid_write_callback(struct usb_xfer *xfer)
218 struct uhid_softc *sc = xfer->priv_sc;
219 struct usb_device_request req;
220 uint32_t size = sc->sc_osize;
224 switch (USB_GET_STATE(xfer)) {
225 case USB_ST_TRANSFERRED:
227 /* try to extract the ID byte */
230 if (usb2_fifo_get_data(
231 sc->sc_fifo.fp[USB_FIFO_TX],
237 usb2_copy_out(xfer->frbuffers, 0, &id, 1);
249 if (usb2_fifo_get_data(
250 sc->sc_fifo.fp[USB_FIFO_TX],
252 0, UHID_BSIZE, &actlen, 1)) {
253 if (actlen != size) {
257 (&req, sc->sc_iface_no,
258 UHID_OUTPUT_REPORT, id, size);
260 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
262 xfer->frlengths[0] = sizeof(req);
263 xfer->frlengths[1] = size;
264 xfer->nframes = xfer->frlengths[1] ? 2 : 1;
265 usb2_start_hardware(xfer);
272 usb2_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
278 uhid_read_callback(struct usb_xfer *xfer)
280 struct uhid_softc *sc = xfer->priv_sc;
281 struct usb_device_request req;
283 switch (USB_GET_STATE(xfer)) {
284 case USB_ST_TRANSFERRED:
285 usb2_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], xfer->frbuffers,
286 sizeof(req), sc->sc_isize, 1);
291 if (usb2_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
294 (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
295 sc->sc_iid, sc->sc_isize);
297 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
299 xfer->frlengths[0] = sizeof(req);
300 xfer->frlengths[1] = sc->sc_isize;
301 xfer->nframes = xfer->frlengths[1] ? 2 : 1;
302 usb2_start_hardware(xfer);
308 usb2_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
313 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
315 [UHID_INTR_DT_RD] = {
316 .type = UE_INTERRUPT,
317 .endpoint = UE_ADDR_ANY,
318 .direction = UE_DIR_IN,
319 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
320 .bufsize = UHID_BSIZE,
321 .callback = &uhid_intr_callback,
324 [UHID_CTRL_DT_WR] = {
326 .endpoint = 0x00, /* Control pipe */
327 .direction = UE_DIR_ANY,
328 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
329 .callback = &uhid_write_callback,
330 .timeout = 1000, /* 1 second */
333 [UHID_CTRL_DT_RD] = {
335 .endpoint = 0x00, /* Control pipe */
336 .direction = UE_DIR_ANY,
337 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
338 .callback = &uhid_read_callback,
339 .timeout = 1000, /* 1 second */
344 uhid_start_read(struct usb_fifo *fifo)
346 struct uhid_softc *sc = fifo->priv_sc0;
348 if (sc->sc_flags & UHID_FLAG_IMMED) {
349 usb2_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
351 usb2_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
356 uhid_stop_read(struct usb_fifo *fifo)
358 struct uhid_softc *sc = fifo->priv_sc0;
360 usb2_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
361 usb2_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
365 uhid_start_write(struct usb_fifo *fifo)
367 struct uhid_softc *sc = fifo->priv_sc0;
369 usb2_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
373 uhid_stop_write(struct usb_fifo *fifo)
375 struct uhid_softc *sc = fifo->priv_sc0;
377 usb2_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
381 uhid_get_report(struct uhid_softc *sc, uint8_t type,
382 uint8_t id, void *kern_data, void *user_data,
386 uint8_t free_data = 0;
388 if (kern_data == NULL) {
389 kern_data = malloc(len, M_USBDEV, M_WAITOK);
390 if (kern_data == NULL) {
396 err = usb2_req_get_report(sc->sc_udev, NULL, kern_data,
397 len, sc->sc_iface_index, type, id);
404 err = copyout(kern_data, user_data, len);
411 free(kern_data, M_USBDEV);
417 uhid_set_report(struct uhid_softc *sc, uint8_t type,
418 uint8_t id, void *kern_data, void *user_data,
422 uint8_t free_data = 0;
424 if (kern_data == NULL) {
425 kern_data = malloc(len, M_USBDEV, M_WAITOK);
426 if (kern_data == NULL) {
431 err = copyin(user_data, kern_data, len);
436 err = usb2_req_set_report(sc->sc_udev, NULL, kern_data,
437 len, sc->sc_iface_index, type, id);
444 free(kern_data, M_USBDEV);
450 uhid_open(struct usb_fifo *fifo, int fflags)
452 struct uhid_softc *sc = fifo->priv_sc0;
455 * The buffers are one byte larger than maximum so that one
456 * can detect too large read/writes and short transfers:
458 if (fflags & FREAD) {
460 sc->sc_flags &= ~UHID_FLAG_IMMED;
462 if (usb2_fifo_alloc_buffer(fifo,
463 sc->sc_isize + 1, UHID_FRAME_NUM)) {
467 if (fflags & FWRITE) {
468 if (usb2_fifo_alloc_buffer(fifo,
469 sc->sc_osize + 1, UHID_FRAME_NUM)) {
477 uhid_close(struct usb_fifo *fifo, int fflags)
479 if (fflags & (FREAD | FWRITE)) {
480 usb2_fifo_free_buffer(fifo);
485 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
488 struct uhid_softc *sc = fifo->priv_sc0;
489 struct usb_gen_descriptor *ugd;
495 case USB_GET_REPORT_DESC:
497 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
498 size = ugd->ugd_maxlen;
500 size = sc->sc_repdesc_size;
502 ugd->ugd_actlen = size;
503 if (ugd->ugd_data == NULL)
504 break; /* descriptor length only */
505 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
509 if (!(fflags & FREAD)) {
517 error = uhid_get_report(sc, UHID_INPUT_REPORT,
518 sc->sc_iid, NULL, NULL, sc->sc_isize);
522 mtx_lock(&sc->sc_mtx);
523 sc->sc_flags |= UHID_FLAG_IMMED;
524 mtx_unlock(&sc->sc_mtx);
526 mtx_lock(&sc->sc_mtx);
527 sc->sc_flags &= ~UHID_FLAG_IMMED;
528 mtx_unlock(&sc->sc_mtx);
533 if (!(fflags & FREAD)) {
538 switch (ugd->ugd_report_type) {
539 case UHID_INPUT_REPORT:
543 case UHID_OUTPUT_REPORT:
547 case UHID_FEATURE_REPORT:
554 error = uhid_get_report(sc, ugd->ugd_report_type, id,
555 NULL, ugd->ugd_data, size);
559 if (!(fflags & FWRITE)) {
564 switch (ugd->ugd_report_type) {
565 case UHID_INPUT_REPORT:
569 case UHID_OUTPUT_REPORT:
573 case UHID_FEATURE_REPORT:
580 error = uhid_set_report(sc, ugd->ugd_report_type, id,
581 NULL, ugd->ugd_data, size);
584 case USB_GET_REPORT_ID:
585 *(int *)addr = 0; /* XXX: we only support reportid 0? */
596 uhid_probe(device_t dev)
598 struct usb_attach_arg *uaa = device_get_ivars(dev);
602 if (uaa->usb_mode != USB_MODE_HOST) {
605 if (uaa->use_generic == 0) {
606 /* give Mouse and Keyboard drivers a try first */
609 if (uaa->info.bInterfaceClass != UICLASS_HID) {
611 /* the Xbox 360 gamepad doesn't use the HID class */
613 if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) ||
614 (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) ||
615 (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) {
619 if (usb2_test_quirk(uaa, UQ_HID_IGNORE)) {
626 uhid_attach(device_t dev)
628 struct usb_attach_arg *uaa = device_get_ivars(dev);
629 struct uhid_softc *sc = device_get_softc(dev);
630 int unit = device_get_unit(dev);
633 DPRINTFN(10, "sc=%p\n", sc);
635 device_set_usb2_desc(dev);
637 mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
639 sc->sc_udev = uaa->device;
641 sc->sc_iface_no = uaa->info.bIfaceNum;
642 sc->sc_iface_index = uaa->info.bIfaceIndex;
644 error = usb2_transfer_setup(uaa->device,
645 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
646 UHID_N_TRANSFER, sc, &sc->sc_mtx);
649 DPRINTF("error=%s\n", usb2_errstr(error));
652 if (uaa->info.idVendor == USB_VENDOR_WACOM) {
654 /* the report descriptor for the Wacom Graphire is broken */
656 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
658 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
659 sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire_report_descr, 0);
660 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
662 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
664 static uint8_t reportbuf[] = {2, 2, 2};
667 * The Graphire3 needs 0x0202 to be written to
668 * feature report ID 2 before it'll start
669 * returning digitizer data.
671 error = usb2_req_set_report(uaa->device, NULL,
672 reportbuf, sizeof(reportbuf),
673 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
676 DPRINTF("set report failed, error=%s (ignored)\n",
679 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
680 sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire3_4x5_report_descr, 0);
681 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
683 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
684 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
685 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
687 /* the Xbox 360 gamepad has no report descriptor */
688 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
689 sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_xb360gp_report_descr, 0);
690 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
692 if (sc->sc_repdesc_ptr == NULL) {
694 error = usb2_req_get_hid_desc(uaa->device, NULL,
695 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
696 M_USBDEV, uaa->info.bIfaceIndex);
699 device_printf(dev, "no report descriptor\n");
703 error = usb2_req_set_idle(uaa->device, NULL,
704 uaa->info.bIfaceIndex, 0, 0);
707 DPRINTF("set idle failed, error=%s (ignored)\n",
710 sc->sc_isize = hid_report_size
711 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
713 sc->sc_osize = hid_report_size
714 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
716 sc->sc_fsize = hid_report_size
717 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
719 if (sc->sc_isize > UHID_BSIZE) {
720 DPRINTF("input size is too large, "
721 "%d bytes (truncating)\n",
723 sc->sc_isize = UHID_BSIZE;
725 if (sc->sc_osize > UHID_BSIZE) {
726 DPRINTF("output size is too large, "
727 "%d bytes (truncating)\n",
729 sc->sc_osize = UHID_BSIZE;
731 if (sc->sc_fsize > UHID_BSIZE) {
732 DPRINTF("feature size is too large, "
733 "%d bytes (truncating)\n",
735 sc->sc_fsize = UHID_BSIZE;
738 error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
739 &uhid_fifo_methods, &sc->sc_fifo,
740 unit, 0 - 1, uaa->info.bIfaceIndex,
741 UID_ROOT, GID_OPERATOR, 0644);
745 return (0); /* success */
753 uhid_detach(device_t dev)
755 struct uhid_softc *sc = device_get_softc(dev);
757 usb2_fifo_detach(&sc->sc_fifo);
759 usb2_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
761 if (sc->sc_repdesc_ptr) {
762 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
763 free(sc->sc_repdesc_ptr, M_USBDEV);
766 mtx_destroy(&sc->sc_mtx);
771 static devclass_t uhid_devclass;
773 static device_method_t uhid_methods[] = {
774 DEVMETHOD(device_probe, uhid_probe),
775 DEVMETHOD(device_attach, uhid_attach),
776 DEVMETHOD(device_detach, uhid_detach),
780 static driver_t uhid_driver = {
782 .methods = uhid_methods,
783 .size = sizeof(struct uhid_softc),
786 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
787 MODULE_DEPEND(uhid, usb, 1, 1, 1);