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 $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
38 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
41 #include <sys/stdint.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <sys/fcntl.h>
59 #include <bus/u4b/usbdevs.h>
60 #include <bus/u4b/usb.h>
61 #include <bus/u4b/usbdi.h>
62 #include <bus/u4b/usbdi_util.h>
63 #include <bus/u4b/usbhid.h>
64 #include <bus/u4b/usb_ioctl.h>
66 #define USB_DEBUG_VAR uhid_debug
67 #include <bus/u4b/usb_debug.h>
69 #include <bus/u4b/input/usb_rdesc.h>
70 #include <bus/u4b/quirk/usb_quirk.h>
73 static int uhid_debug = 0;
75 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
76 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
77 &uhid_debug, 0, "Debug level");
80 #define UHID_BSIZE 1024 /* bytes, buffer size */
81 #define UHID_FRAME_NUM 50 /* bytes, frame number */
91 struct usb_fifo_sc sc_fifo;
94 struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
95 struct usb_device *sc_udev;
102 uint16_t sc_repdesc_size;
105 uint8_t sc_iface_index;
110 #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */
111 #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are
115 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
116 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
117 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
121 static device_probe_t uhid_probe;
122 static device_attach_t uhid_attach;
123 static device_detach_t uhid_detach;
125 static usb_callback_t uhid_intr_callback;
126 static usb_callback_t uhid_write_callback;
127 static usb_callback_t uhid_read_callback;
129 static usb_fifo_cmd_t uhid_start_read;
130 static usb_fifo_cmd_t uhid_stop_read;
131 static usb_fifo_cmd_t uhid_start_write;
132 static usb_fifo_cmd_t uhid_stop_write;
133 static usb_fifo_open_t uhid_open;
134 static usb_fifo_close_t uhid_close;
135 static usb_fifo_ioctl_t uhid_ioctl;
137 static struct usb_fifo_methods uhid_fifo_methods = {
138 .f_open = &uhid_open,
139 .f_close = &uhid_close,
140 .f_ioctl = &uhid_ioctl,
141 .f_start_read = &uhid_start_read,
142 .f_stop_read = &uhid_stop_read,
143 .f_start_write = &uhid_start_write,
144 .f_stop_write = &uhid_stop_write,
145 .basename[0] = "uhid",
149 uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error)
151 struct uhid_softc *sc = usbd_xfer_softc(xfer);
152 struct usb_page_cache *pc;
155 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
157 switch (USB_GET_STATE(xfer)) {
158 case USB_ST_TRANSFERRED:
159 DPRINTF("transferred!\n");
161 pc = usbd_xfer_get_frame(xfer, 0);
164 * If the ID byte is non zero we allow descriptors
165 * having multiple sizes:
167 if ((actlen >= sc->sc_isize) ||
168 ((actlen > 0) && (sc->sc_iid != 0))) {
169 /* limit report length to the maximum */
170 if (actlen > sc->sc_isize)
171 actlen = sc->sc_isize;
172 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
176 DPRINTF("ignored transfer, %d bytes\n", actlen);
181 if (usb_fifo_put_bytes_max(
182 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
183 usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
184 usbd_transfer_submit(xfer);
189 if (error != USB_ERR_CANCELLED) {
190 /* try to clear stall first */
191 usbd_xfer_set_stall(xfer);
199 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
200 uint8_t type, uint8_t id, uint16_t size)
202 req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
203 req->bRequest = UR_SET_REPORT;
204 USETW2(req->wValue, type, id);
205 req->wIndex[0] = iface_no;
207 USETW(req->wLength, size);
211 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
212 uint8_t type, uint8_t id, uint16_t size)
214 req->bmRequestType = UT_READ_CLASS_INTERFACE;
215 req->bRequest = UR_GET_REPORT;
216 USETW2(req->wValue, type, id);
217 req->wIndex[0] = iface_no;
219 USETW(req->wLength, size);
223 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
225 struct uhid_softc *sc = usbd_xfer_softc(xfer);
226 struct usb_device_request req;
227 struct usb_page_cache *pc;
228 uint32_t size = sc->sc_osize;
232 switch (USB_GET_STATE(xfer)) {
233 case USB_ST_TRANSFERRED:
235 /* try to extract the ID byte */
237 pc = usbd_xfer_get_frame(xfer, 0);
238 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
243 usbd_copy_out(pc, 0, &id, 1);
255 pc = usbd_xfer_get_frame(xfer, 1);
256 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
257 0, UHID_BSIZE, &actlen, 1)) {
258 if (actlen != size) {
262 (&req, sc->sc_iface_no,
263 UHID_OUTPUT_REPORT, id, size);
265 pc = usbd_xfer_get_frame(xfer, 0);
266 usbd_copy_in(pc, 0, &req, sizeof(req));
268 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
269 usbd_xfer_set_frame_len(xfer, 1, size);
270 usbd_xfer_set_frames(xfer, size ? 2 : 1);
271 usbd_transfer_submit(xfer);
278 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
284 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
286 struct uhid_softc *sc = usbd_xfer_softc(xfer);
287 struct usb_device_request req;
288 struct usb_page_cache *pc;
290 pc = usbd_xfer_get_frame(xfer, 0);
292 switch (USB_GET_STATE(xfer)) {
293 case USB_ST_TRANSFERRED:
294 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
300 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
303 (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
304 sc->sc_iid, sc->sc_isize);
306 usbd_copy_in(pc, 0, &req, sizeof(req));
308 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
309 usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
310 usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
311 usbd_transfer_submit(xfer);
317 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
322 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
324 [UHID_INTR_DT_RD] = {
325 .type = UE_INTERRUPT,
326 .endpoint = UE_ADDR_ANY,
327 .direction = UE_DIR_IN,
328 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
329 .bufsize = UHID_BSIZE,
330 .callback = &uhid_intr_callback,
333 [UHID_CTRL_DT_WR] = {
335 .endpoint = 0x00, /* Control pipe */
336 .direction = UE_DIR_ANY,
337 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
338 .callback = &uhid_write_callback,
339 .timeout = 1000, /* 1 second */
342 [UHID_CTRL_DT_RD] = {
344 .endpoint = 0x00, /* Control pipe */
345 .direction = UE_DIR_ANY,
346 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
347 .callback = &uhid_read_callback,
348 .timeout = 1000, /* 1 second */
353 uhid_start_read(struct usb_fifo *fifo)
355 struct uhid_softc *sc = usb_fifo_softc(fifo);
357 if (sc->sc_flags & UHID_FLAG_IMMED) {
358 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
360 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
365 uhid_stop_read(struct usb_fifo *fifo)
367 struct uhid_softc *sc = usb_fifo_softc(fifo);
369 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
370 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
374 uhid_start_write(struct usb_fifo *fifo)
376 struct uhid_softc *sc = usb_fifo_softc(fifo);
378 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
382 uhid_stop_write(struct usb_fifo *fifo)
384 struct uhid_softc *sc = usb_fifo_softc(fifo);
386 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
390 uhid_get_report(struct uhid_softc *sc, uint8_t type,
391 uint8_t id, void *kern_data, void *user_data,
395 uint8_t free_data = 0;
397 if (kern_data == NULL) {
398 kern_data = kmalloc(len, M_USBDEV, M_WAITOK);
401 err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
402 len, sc->sc_iface_index, type, id);
409 err = copyout(kern_data, user_data, len);
416 kfree(kern_data, M_USBDEV);
422 uhid_set_report(struct uhid_softc *sc, uint8_t type,
423 uint8_t id, void *kern_data, void *user_data,
427 uint8_t free_data = 0;
429 if (kern_data == NULL) {
430 kern_data = kmalloc(len, M_USBDEV, M_WAITOK);
432 err = copyin(user_data, kern_data, len);
437 err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
438 len, sc->sc_iface_index, type, id);
445 kfree(kern_data, M_USBDEV);
451 uhid_open(struct usb_fifo *fifo, int fflags)
453 struct uhid_softc *sc = usb_fifo_softc(fifo);
456 * The buffers are one byte larger than maximum so that one
457 * can detect too large read/writes and short transfers:
459 if (fflags & FREAD) {
461 sc->sc_flags &= ~UHID_FLAG_IMMED;
463 if (usb_fifo_alloc_buffer(fifo,
464 sc->sc_isize + 1, UHID_FRAME_NUM)) {
468 if (fflags & FWRITE) {
469 if (usb_fifo_alloc_buffer(fifo,
470 sc->sc_osize + 1, UHID_FRAME_NUM)) {
478 uhid_close(struct usb_fifo *fifo, int fflags)
480 if (fflags & (FREAD | FWRITE)) {
481 usb_fifo_free_buffer(fifo);
486 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
489 struct uhid_softc *sc = usb_fifo_softc(fifo);
490 struct usb_gen_descriptor *ugd;
496 case USB_GET_REPORT_DESC:
498 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
499 size = ugd->ugd_maxlen;
501 size = sc->sc_repdesc_size;
503 ugd->ugd_actlen = size;
504 if (ugd->ugd_data == NULL)
505 break; /* descriptor length only */
506 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
510 if (!(fflags & FREAD)) {
518 error = uhid_get_report(sc, UHID_INPUT_REPORT,
519 sc->sc_iid, NULL, NULL, sc->sc_isize);
523 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
524 sc->sc_flags |= UHID_FLAG_IMMED;
525 lockmgr(&sc->sc_lock, LK_RELEASE);
527 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
528 sc->sc_flags &= ~UHID_FLAG_IMMED;
529 lockmgr(&sc->sc_lock, LK_RELEASE);
534 if (!(fflags & FREAD)) {
539 switch (ugd->ugd_report_type) {
540 case UHID_INPUT_REPORT:
544 case UHID_OUTPUT_REPORT:
548 case UHID_FEATURE_REPORT:
556 copyin(ugd->ugd_data, &id, 1);
557 error = uhid_get_report(sc, ugd->ugd_report_type, id,
558 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
562 if (!(fflags & FWRITE)) {
567 switch (ugd->ugd_report_type) {
568 case UHID_INPUT_REPORT:
572 case UHID_OUTPUT_REPORT:
576 case UHID_FEATURE_REPORT:
584 copyin(ugd->ugd_data, &id, 1);
585 error = uhid_set_report(sc, ugd->ugd_report_type, id,
586 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
589 case USB_GET_REPORT_ID:
590 *(int *)addr = 0; /* XXX: we only support reportid 0? */
600 static const STRUCT_USB_HOST_ID uhid_devs[] = {
601 /* generic HID class */
602 {USB_IFACE_CLASS(UICLASS_HID),},
603 /* the Xbox 360 gamepad doesn't use the HID class */
604 {USB_IFACE_CLASS(UICLASS_VENDOR),
605 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
606 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
610 uhid_probe(device_t dev)
612 struct usb_attach_arg *uaa = device_get_ivars(dev);
617 if (uaa->usb_mode != USB_MODE_HOST)
620 error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
624 if (usb_test_quirk(uaa, UQ_HID_IGNORE))
628 * Don't attach to mouse and keyboard devices, hence then no
629 * "nomatch" event is generated and then ums and ukbd won't
630 * attach properly when loaded.
632 if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
633 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
634 ((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) ||
635 (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))) {
639 return (BUS_PROBE_GENERIC);
643 uhid_attach(device_t dev)
645 struct usb_attach_arg *uaa = device_get_ivars(dev);
646 struct uhid_softc *sc = device_get_softc(dev);
647 int unit = device_get_unit(dev);
650 DPRINTFN(10, "sc=%p\n", sc);
652 device_set_usb_desc(dev);
654 lockinit(&sc->sc_lock, "uhid lock", 0, LK_CANRECURSE);
656 sc->sc_udev = uaa->device;
658 sc->sc_iface_no = uaa->info.bIfaceNum;
659 sc->sc_iface_index = uaa->info.bIfaceIndex;
661 error = usbd_transfer_setup(uaa->device,
662 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
663 UHID_N_TRANSFER, sc, &sc->sc_lock);
666 DPRINTF("error=%s\n", usbd_errstr(error));
669 if (uaa->info.idVendor == USB_VENDOR_WACOM) {
671 /* the report descriptor for the Wacom Graphire is broken */
673 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
675 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
676 sc->sc_repdesc_ptr = (void *)&uhid_graphire_report_descr;
677 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
679 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
681 static uint8_t reportbuf[] = {2, 2, 2};
684 * The Graphire3 needs 0x0202 to be written to
685 * feature report ID 2 before it'll start
686 * returning digitizer data.
688 error = usbd_req_set_report(uaa->device, NULL,
689 reportbuf, sizeof(reportbuf),
690 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
693 DPRINTF("set report failed, error=%s (ignored)\n",
696 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
697 sc->sc_repdesc_ptr = (void *)&uhid_graphire3_4x5_report_descr;
698 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
700 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
701 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
702 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
704 /* the Xbox 360 gamepad has no report descriptor */
705 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
706 sc->sc_repdesc_ptr = (void *)&uhid_xb360gp_report_descr;
707 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
709 if (sc->sc_repdesc_ptr == NULL) {
711 error = usbd_req_get_hid_desc(uaa->device, NULL,
712 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
713 M_USBDEV, uaa->info.bIfaceIndex);
716 device_printf(dev, "no report descriptor\n");
720 error = usbd_req_set_idle(uaa->device, NULL,
721 uaa->info.bIfaceIndex, 0, 0);
724 DPRINTF("set idle failed, error=%s (ignored)\n",
727 sc->sc_isize = hid_report_size
728 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
730 sc->sc_osize = hid_report_size
731 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
733 sc->sc_fsize = hid_report_size
734 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
736 if (sc->sc_isize > UHID_BSIZE) {
737 DPRINTF("input size is too large, "
738 "%d bytes (truncating)\n",
740 sc->sc_isize = UHID_BSIZE;
742 if (sc->sc_osize > UHID_BSIZE) {
743 DPRINTF("output size is too large, "
744 "%d bytes (truncating)\n",
746 sc->sc_osize = UHID_BSIZE;
748 if (sc->sc_fsize > UHID_BSIZE) {
749 DPRINTF("feature size is too large, "
750 "%d bytes (truncating)\n",
752 sc->sc_fsize = UHID_BSIZE;
755 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
756 &uhid_fifo_methods, &sc->sc_fifo,
757 unit, 0 - 1, uaa->info.bIfaceIndex,
758 UID_ROOT, GID_OPERATOR, 0644);
762 return (0); /* success */
770 uhid_detach(device_t dev)
772 struct uhid_softc *sc = device_get_softc(dev);
774 usb_fifo_detach(&sc->sc_fifo);
776 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
778 if (sc->sc_repdesc_ptr) {
779 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
780 kfree(sc->sc_repdesc_ptr, M_USBDEV);
783 lockuninit(&sc->sc_lock);
788 static devclass_t uhid_devclass;
790 static device_method_t uhid_methods[] = {
791 DEVMETHOD(device_probe, uhid_probe),
792 DEVMETHOD(device_attach, uhid_attach),
793 DEVMETHOD(device_detach, uhid_detach),
797 static driver_t uhid_driver = {
799 .methods = uhid_methods,
800 .size = sizeof(struct uhid_softc),
803 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, NULL);
804 MODULE_DEPEND(uhid, usb, 1, 1, 1);
805 MODULE_VERSION(uhid, 1);