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 * XXX TODO: Convert this driver to use si_drv[12] rather than the
53 * devclass_get_softc junk
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/clist.h>
59 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #include <sys/mutex.h>
63 #include <sys/signalvar.h>
64 #include <sys/fcntl.h>
65 #include <sys/ioccom.h>
66 #include <sys/filio.h>
67 #include <sys/module.h>
69 #include <sys/ioccom.h>
71 #include <sys/selinfo.h>
74 #include <sys/sysctl.h>
75 #include <sys/ttycom.h>
78 #include <dev/usb/usb.h>
79 #include <dev/usb/usbhid.h>
82 #include <dev/usb/usbdi.h>
83 #include <dev/usb/usbdi_util.h>
84 #include <dev/usb/hid.h>
86 /* Replacement report descriptors for devices shipped with broken ones */
87 #include <dev/usb/ugraphire_rdesc.h>
88 #include <dev/usb/uxb360gp_rdesc.h>
90 /* For hid blacklist quirk */
91 #include <dev/usb/usb_quirks.h>
94 #define DPRINTF(x) if (uhiddebug) printf x
95 #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
97 SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
98 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
99 &uhiddebug, 0, "uhid debug level");
102 #define DPRINTFN(n,x)
106 device_t sc_dev; /* base device */
107 usbd_device_handle sc_udev;
108 usbd_interface_handle sc_iface; /* interface */
109 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
126 struct selinfo sc_rsel;
127 struct proc *sc_async; /* process that wants SIGIO */
128 u_char sc_state; /* driver state */
129 #define UHID_OPEN 0x01 /* device is open */
130 #define UHID_ASLP 0x02 /* waiting for device data */
131 #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
132 #define UHID_IMMED 0x08 /* return read data immediately */
140 #define UHIDUNIT(dev) (dev2unit(dev))
141 #define UHID_CHUNK 128 /* chunk size for read */
142 #define UHID_BSIZE 1020 /* buffer size */
152 static struct cdevsw uhid_cdevsw = {
153 .d_version = D_VERSION,
154 .d_flags = D_NEEDGIANT,
156 .d_close = uhidclose,
158 .d_write = uhidwrite,
159 .d_ioctl = uhidioctl,
164 static void uhid_intr(usbd_xfer_handle, usbd_private_handle,
167 static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
168 static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
169 static int uhid_do_ioctl(struct uhid_softc *, u_long, caddr_t, int,
172 MODULE_DEPEND(uhid, usb, 1, 1, 1);
174 static device_probe_t uhid_match;
175 static device_attach_t uhid_attach;
176 static device_detach_t uhid_detach;
178 static device_method_t uhid_methods[] = {
179 /* Device interface */
180 DEVMETHOD(device_probe, uhid_match),
181 DEVMETHOD(device_attach, uhid_attach),
182 DEVMETHOD(device_detach, uhid_detach),
187 static driver_t uhid_driver = {
190 sizeof(struct uhid_softc)
193 static devclass_t uhid_devclass;
195 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
198 uhid_match(device_t self)
200 struct usb_attach_arg *uaa = device_get_ivars(self);
201 usb_interface_descriptor_t *id;
203 if (uaa->iface == NULL)
204 return (UMATCH_NONE);
205 id = usbd_get_interface_descriptor(uaa->iface);
207 return (UMATCH_NONE);
208 if (id->bInterfaceClass != UICLASS_HID) {
209 /* The Xbox 360 gamepad doesn't use the HID class. */
210 if (id->bInterfaceClass != UICLASS_VENDOR ||
211 id->bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER ||
212 id->bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)
213 return (UMATCH_NONE);
215 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_HID_IGNORE)
216 return (UMATCH_NONE);
219 return (uaa->matchlvl);
222 return (UMATCH_IFACECLASS_GENERIC);
226 uhid_attach(device_t self)
228 struct uhid_softc *sc = device_get_softc(self);
229 struct usb_attach_arg *uaa = device_get_ivars(self);
230 usbd_interface_handle iface = uaa->iface;
231 usb_interface_descriptor_t *id;
232 usb_endpoint_descriptor_t *ed;
239 sc->sc_udev = uaa->device;
240 sc->sc_iface = iface;
241 id = usbd_get_interface_descriptor(iface);
243 ed = usbd_interface2endpoint_descriptor(iface, 0);
245 printf("%s: could not read endpoint descriptor\n",
246 device_get_nameunit(sc->sc_dev));
251 DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
252 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
254 ed->bLength, ed->bDescriptorType,
255 ed->bEndpointAddress & UE_ADDR,
256 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
257 ed->bmAttributes & UE_XFERTYPE,
258 UGETW(ed->wMaxPacketSize), ed->bInterval));
260 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
261 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
262 printf("%s: unexpected endpoint\n", device_get_nameunit(sc->sc_dev));
267 sc->sc_ep_addr = ed->bEndpointAddress;
270 if (uaa->vendor == USB_VENDOR_WACOM) {
271 /* The report descriptor for the Wacom Graphire is broken. */
272 if (uaa->product == USB_PRODUCT_WACOM_GRAPHIRE) {
273 size = sizeof uhid_graphire_report_descr;
274 descptr = uhid_graphire_report_descr;
275 } else if (uaa->product == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
276 static uByte reportbuf[] = {2, 2, 2};
279 * The Graphire3 needs 0x0202 to be written to
280 * feature report ID 2 before it'll start
281 * returning digitizer data.
283 usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
284 &reportbuf, sizeof reportbuf);
286 size = sizeof uhid_graphire3_4x5_report_descr;
287 descptr = uhid_graphire3_4x5_report_descr;
289 } else if (id->bInterfaceClass == UICLASS_VENDOR &&
290 id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER &&
291 id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD) {
292 static uByte reportbuf[] = {1, 3, 0};
294 /* The LEDs on the gamepad are blinking by default, turn off. */
295 usbd_set_report(uaa->iface, UHID_OUTPUT_REPORT, 0,
296 &reportbuf, sizeof reportbuf);
298 /* The Xbox 360 gamepad has no report descriptor. */
299 size = sizeof uhid_xb360gp_report_descr;
300 descptr = uhid_xb360gp_report_descr;
304 desc = malloc(size, M_USBDEV, M_NOWAIT);
308 err = USBD_NORMAL_COMPLETION;
309 memcpy(desc, descptr, size);
313 err = usbd_read_report_desc(uaa->iface, &desc, &size,M_USBDEV);
317 printf("%s: no report descriptor\n", device_get_nameunit(sc->sc_dev));
322 (void)usbd_set_idle(iface, 0, 0);
324 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
325 sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
326 sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
328 sc->sc_repdesc = desc;
329 sc->sc_repdesc_size = size;
330 sc->dev = make_dev(&uhid_cdevsw, device_get_unit(self),
331 UID_ROOT, GID_OPERATOR,
332 0644, "uhid%d", device_get_unit(self));
337 uhid_detach(device_t self)
339 struct uhid_softc *sc = device_get_softc(self);
342 DPRINTF(("uhid_detach: sc=%p\n", sc));
344 if (sc->sc_intrpipe != NULL)
345 usbd_abort_pipe(sc->sc_intrpipe);
347 if (sc->sc_state & UHID_OPEN) {
349 if (--sc->sc_refcnt >= 0) {
352 /* Wait for processes to go away. */
353 usb_detach_wait(sc->sc_dev);
357 destroy_dev(sc->dev);
360 free(sc->sc_repdesc, M_USBDEV);
366 uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
368 struct uhid_softc *sc = addr;
374 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
375 DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
376 DPRINTF(("uhid_intr: data ="));
377 for (i = 0; i < cc; i++)
378 DPRINTF((" %02x", sc->sc_ibuf[i]));
383 if (status == USBD_CANCELLED)
386 if (status != USBD_NORMAL_COMPLETION) {
387 DPRINTF(("uhid_intr: status=%d\n", status));
388 if (status == USBD_STALLED)
389 sc->sc_state |= UHID_NEEDCLEAR;
393 (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
395 if (sc->sc_state & UHID_ASLP) {
396 sc->sc_state &= ~UHID_ASLP;
397 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
400 selwakeuppri(&sc->sc_rsel, PZERO);
401 if (sc->sc_async != NULL) {
402 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
403 PROC_LOCK(sc->sc_async);
404 psignal(sc->sc_async, SIGIO);
405 PROC_UNLOCK(sc->sc_async);
410 uhidopen(struct cdev *dev, int flag, int mode, struct thread *p)
412 struct uhid_softc *sc;
415 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
419 DPRINTF(("uhidopen: sc=%p\n", sc));
424 if (sc->sc_state & UHID_OPEN)
426 sc->sc_state |= UHID_OPEN;
428 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, UHID_BSIZE);
429 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
430 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
432 /* Set up interrupt pipe. */
433 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
434 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
435 sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
437 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
439 free(sc->sc_ibuf, M_USBDEV);
440 free(sc->sc_obuf, M_USBDEV);
441 sc->sc_ibuf = sc->sc_obuf = NULL;
443 sc->sc_state &= ~UHID_OPEN;
447 sc->sc_state &= ~UHID_IMMED;
455 uhidclose(struct cdev *dev, int flag, int mode, struct thread *p)
457 struct uhid_softc *sc;
459 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
461 DPRINTF(("uhidclose: sc=%p\n", sc));
463 /* Disable interrupts. */
464 usbd_abort_pipe(sc->sc_intrpipe);
465 usbd_close_pipe(sc->sc_intrpipe);
468 ndflush(&sc->sc_q, sc->sc_q.c_cc);
469 clist_free_cblocks(&sc->sc_q);
471 free(sc->sc_ibuf, M_USBDEV);
472 free(sc->sc_obuf, M_USBDEV);
473 sc->sc_ibuf = sc->sc_obuf = NULL;
475 sc->sc_state &= ~UHID_OPEN;
483 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
488 u_char buffer[UHID_CHUNK];
491 DPRINTFN(1, ("uhidread\n"));
492 if (sc->sc_state & UHID_IMMED) {
493 DPRINTFN(1, ("uhidread immed\n"));
495 err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
496 sc->sc_iid, buffer, sc->sc_isize);
499 return (uiomove(buffer, sc->sc_isize, uio));
503 while (sc->sc_q.c_cc == 0) {
504 if (flag & O_NONBLOCK) {
506 return (EWOULDBLOCK);
508 sc->sc_state |= UHID_ASLP;
509 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
510 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
511 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
515 sc->sc_state &= ~UHID_ASLP;
518 if (sc->sc_state & UHID_NEEDCLEAR) {
519 DPRINTFN(-1,("uhidread: clearing stall\n"));
520 sc->sc_state &= ~UHID_NEEDCLEAR;
521 usbd_clear_endpoint_stall(sc->sc_intrpipe);
526 /* Transfer as many chunks as possible. */
527 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
528 length = min(sc->sc_q.c_cc, uio->uio_resid);
529 if (length > sizeof(buffer))
530 length = sizeof(buffer);
532 /* Remove a small chunk from the input queue. */
533 (void) q_to_b(&sc->sc_q, buffer, length);
534 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
536 /* Copy the data to the user process. */
537 if ((error = uiomove(buffer, length, uio)) != 0)
545 uhidread(struct cdev *dev, struct uio *uio, int flag)
547 struct uhid_softc *sc;
550 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
552 error = uhid_do_read(sc, uio, flag);
553 if (--sc->sc_refcnt < 0)
554 usb_detach_wakeup(sc->sc_dev);
559 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
565 DPRINTFN(1, ("uhidwrite\n"));
572 if (uio->uio_resid != size)
574 error = uiomove(sc->sc_obuf, size, uio);
577 err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
578 sc->sc_obuf[0], sc->sc_obuf+1, size-1);
580 err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
581 0, sc->sc_obuf, size);
590 uhidwrite(struct cdev *dev, struct uio *uio, int flag)
592 struct uhid_softc *sc;
595 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
597 error = uhid_do_write(sc, uio, flag);
598 if (--sc->sc_refcnt < 0)
599 usb_detach_wakeup(sc->sc_dev);
604 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr, int flag,
607 struct usb_ctl_report_desc *rd;
608 struct usb_ctl_report *re;
612 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
619 /* All handled in the upper FS layer. */
624 if (sc->sc_async != NULL)
626 sc->sc_async = p->td_proc;
627 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", sc->sc_async));
632 /* XXX this is not the most general solution. */
634 if (sc->sc_async == NULL)
636 if (*(int *)addr != sc->sc_async->p_pgid)
640 case USB_GET_REPORT_DESC:
641 rd = (struct usb_ctl_report_desc *)addr;
642 size = min(sc->sc_repdesc_size, sizeof rd->ucrd_data);
643 rd->ucrd_size = size;
644 memcpy(rd->ucrd_data, sc->sc_repdesc, size);
649 /* XXX should read into ibuf, but does it matter? */
650 err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
651 sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
655 sc->sc_state |= UHID_IMMED;
657 sc->sc_state &= ~UHID_IMMED;
661 re = (struct usb_ctl_report *)addr;
662 switch (re->ucr_report) {
663 case UHID_INPUT_REPORT:
667 case UHID_OUTPUT_REPORT:
671 case UHID_FEATURE_REPORT:
678 err = usbd_get_report(sc->sc_iface, re->ucr_report, id, re->ucr_data,
685 re = (struct usb_ctl_report *)addr;
686 switch (re->ucr_report) {
687 case UHID_INPUT_REPORT:
691 case UHID_OUTPUT_REPORT:
695 case UHID_FEATURE_REPORT:
702 err = usbd_set_report(sc->sc_iface, re->ucr_report, id, re->ucr_data,
708 case USB_GET_REPORT_ID:
709 *(int *)addr = 0; /* XXX: we only support reportid 0? */
719 uhidioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *p)
721 struct uhid_softc *sc;
724 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
726 error = uhid_do_ioctl(sc, cmd, addr, flag, p);
727 if (--sc->sc_refcnt < 0)
728 usb_detach_wakeup(sc->sc_dev);
733 uhidpoll(struct cdev *dev, int events, struct thread *p)
735 struct uhid_softc *sc;
739 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
744 if (events & (POLLOUT | POLLWRNORM))
745 revents |= events & (POLLOUT | POLLWRNORM);
746 if (events & (POLLIN | POLLRDNORM)) {
747 if (sc->sc_q.c_cc > 0)
748 revents |= events & (POLLIN | POLLRDNORM);
750 selrecord(p, &sc->sc_rsel);