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/kernel.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/signalvar.h>
63 #include <sys/fcntl.h>
64 #include <sys/ioccom.h>
65 #include <sys/filio.h>
66 #include <sys/module.h>
68 #include <sys/ioccom.h>
71 #include <sys/selinfo.h>
74 #include <sys/sysctl.h>
77 #include <dev/usb/usb.h>
78 #include <dev/usb/usbhid.h>
81 #include <dev/usb/usbdi.h>
82 #include <dev/usb/usbdi_util.h>
83 #include <dev/usb/hid.h>
85 /* Replacement report descriptors for devices shipped with broken ones */
86 #include <dev/usb/ugraphire_rdesc.h>
87 #include <dev/usb/uxb360gp_rdesc.h>
89 /* For hid blacklist quirk */
90 #include <dev/usb/usb_quirks.h>
93 #define DPRINTF(x) if (uhiddebug) printf x
94 #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
96 SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
97 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
98 &uhiddebug, 0, "uhid debug level");
101 #define DPRINTFN(n,x)
105 device_t sc_dev; /* base device */
106 usbd_device_handle sc_udev;
107 usbd_interface_handle sc_iface; /* interface */
108 usbd_pipe_handle sc_intrpipe; /* interrupt pipe */
125 struct selinfo sc_rsel;
126 struct proc *sc_async; /* process that wants SIGIO */
127 u_char sc_state; /* driver state */
128 #define UHID_OPEN 0x01 /* device is open */
129 #define UHID_ASLP 0x02 /* waiting for device data */
130 #define UHID_NEEDCLEAR 0x04 /* needs clearing endpoint stall */
131 #define UHID_IMMED 0x08 /* return read data immediately */
139 #define UHIDUNIT(dev) (minor(dev))
140 #define UHID_CHUNK 128 /* chunk size for read */
141 #define UHID_BSIZE 1020 /* buffer size */
151 static struct cdevsw uhid_cdevsw = {
152 .d_version = D_VERSION,
153 .d_flags = D_NEEDGIANT,
155 .d_close = uhidclose,
157 .d_write = uhidwrite,
158 .d_ioctl = uhidioctl,
163 static void uhid_intr(usbd_xfer_handle, usbd_private_handle,
166 static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
167 static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
168 static int uhid_do_ioctl(struct uhid_softc *, u_long, caddr_t, int,
171 MODULE_DEPEND(uhid, usb, 1, 1, 1);
173 static device_probe_t uhid_match;
174 static device_attach_t uhid_attach;
175 static device_detach_t uhid_detach;
177 static device_method_t uhid_methods[] = {
178 /* Device interface */
179 DEVMETHOD(device_probe, uhid_match),
180 DEVMETHOD(device_attach, uhid_attach),
181 DEVMETHOD(device_detach, uhid_detach),
186 static driver_t uhid_driver = {
189 sizeof(struct uhid_softc)
192 static devclass_t uhid_devclass;
194 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
197 uhid_match(device_t self)
199 struct usb_attach_arg *uaa = device_get_ivars(self);
200 usb_interface_descriptor_t *id;
202 if (uaa->iface == NULL)
203 return (UMATCH_NONE);
204 id = usbd_get_interface_descriptor(uaa->iface);
206 return (UMATCH_NONE);
207 if (id->bInterfaceClass != UICLASS_HID) {
208 /* The Xbox 360 gamepad doesn't use the HID class. */
209 if (id->bInterfaceClass != UICLASS_VENDOR ||
210 id->bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER ||
211 id->bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)
212 return (UMATCH_NONE);
214 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_HID_IGNORE)
215 return (UMATCH_NONE);
218 return (uaa->matchlvl);
221 return (UMATCH_IFACECLASS_GENERIC);
225 uhid_attach(device_t self)
227 struct uhid_softc *sc = device_get_softc(self);
228 struct usb_attach_arg *uaa = device_get_ivars(self);
229 usbd_interface_handle iface = uaa->iface;
230 usb_interface_descriptor_t *id;
231 usb_endpoint_descriptor_t *ed;
238 sc->sc_udev = uaa->device;
239 sc->sc_iface = iface;
240 id = usbd_get_interface_descriptor(iface);
242 ed = usbd_interface2endpoint_descriptor(iface, 0);
244 printf("%s: could not read endpoint descriptor\n",
245 device_get_nameunit(sc->sc_dev));
250 DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
251 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
253 ed->bLength, ed->bDescriptorType,
254 ed->bEndpointAddress & UE_ADDR,
255 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
256 ed->bmAttributes & UE_XFERTYPE,
257 UGETW(ed->wMaxPacketSize), ed->bInterval));
259 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
260 (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
261 printf("%s: unexpected endpoint\n", device_get_nameunit(sc->sc_dev));
266 sc->sc_ep_addr = ed->bEndpointAddress;
269 if (uaa->vendor == USB_VENDOR_WACOM) {
270 /* The report descriptor for the Wacom Graphire is broken. */
271 if (uaa->product == USB_PRODUCT_WACOM_GRAPHIRE) {
272 size = sizeof uhid_graphire_report_descr;
273 descptr = uhid_graphire_report_descr;
274 } else if (uaa->product == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
275 static uByte reportbuf[] = {2, 2, 2};
278 * The Graphire3 needs 0x0202 to be written to
279 * feature report ID 2 before it'll start
280 * returning digitizer data.
282 usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
283 &reportbuf, sizeof reportbuf);
285 size = sizeof uhid_graphire3_4x5_report_descr;
286 descptr = uhid_graphire3_4x5_report_descr;
288 } else if (id->bInterfaceClass == UICLASS_VENDOR &&
289 id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER &&
290 id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD) {
291 static uByte reportbuf[] = {1, 3, 0};
293 /* The LEDs on the gamepad are blinking by default, turn off. */
294 usbd_set_report(uaa->iface, UHID_OUTPUT_REPORT, 0,
295 &reportbuf, sizeof reportbuf);
297 /* The Xbox 360 gamepad has no report descriptor. */
298 size = sizeof uhid_xb360gp_report_descr;
299 descptr = uhid_xb360gp_report_descr;
303 desc = malloc(size, M_USBDEV, M_NOWAIT);
307 err = USBD_NORMAL_COMPLETION;
308 memcpy(desc, descptr, size);
312 err = usbd_read_report_desc(uaa->iface, &desc, &size,M_USBDEV);
316 printf("%s: no report descriptor\n", device_get_nameunit(sc->sc_dev));
321 (void)usbd_set_idle(iface, 0, 0);
323 sc->sc_isize = hid_report_size(desc, size, hid_input, &sc->sc_iid);
324 sc->sc_osize = hid_report_size(desc, size, hid_output, &sc->sc_oid);
325 sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
327 sc->sc_repdesc = desc;
328 sc->sc_repdesc_size = size;
329 sc->dev = make_dev(&uhid_cdevsw, device_get_unit(self),
330 UID_ROOT, GID_OPERATOR,
331 0644, "uhid%d", device_get_unit(self));
336 uhid_detach(device_t self)
338 struct uhid_softc *sc = device_get_softc(self);
341 DPRINTF(("uhid_detach: sc=%p\n", sc));
343 if (sc->sc_intrpipe != NULL)
344 usbd_abort_pipe(sc->sc_intrpipe);
346 if (sc->sc_state & UHID_OPEN) {
348 if (--sc->sc_refcnt >= 0) {
351 /* Wait for processes to go away. */
352 usb_detach_wait(sc->sc_dev);
356 destroy_dev(sc->dev);
359 free(sc->sc_repdesc, M_USBDEV);
365 uhid_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
367 struct uhid_softc *sc = addr;
373 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
374 DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
375 DPRINTF(("uhid_intr: data ="));
376 for (i = 0; i < cc; i++)
377 DPRINTF((" %02x", sc->sc_ibuf[i]));
382 if (status == USBD_CANCELLED)
385 if (status != USBD_NORMAL_COMPLETION) {
386 DPRINTF(("uhid_intr: status=%d\n", status));
387 if (status == USBD_STALLED)
388 sc->sc_state |= UHID_NEEDCLEAR;
392 (void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
394 if (sc->sc_state & UHID_ASLP) {
395 sc->sc_state &= ~UHID_ASLP;
396 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
399 selwakeuppri(&sc->sc_rsel, PZERO);
400 if (sc->sc_async != NULL) {
401 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
402 PROC_LOCK(sc->sc_async);
403 psignal(sc->sc_async, SIGIO);
404 PROC_UNLOCK(sc->sc_async);
409 uhidopen(struct cdev *dev, int flag, int mode, struct thread *p)
411 struct uhid_softc *sc;
414 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
418 DPRINTF(("uhidopen: sc=%p\n", sc));
423 if (sc->sc_state & UHID_OPEN)
425 sc->sc_state |= UHID_OPEN;
427 clist_alloc_cblocks(&sc->sc_q, UHID_BSIZE, UHID_BSIZE);
428 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
429 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
431 /* Set up interrupt pipe. */
432 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
433 USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
434 sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
436 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
438 free(sc->sc_ibuf, M_USBDEV);
439 free(sc->sc_obuf, M_USBDEV);
440 sc->sc_ibuf = sc->sc_obuf = NULL;
442 sc->sc_state &= ~UHID_OPEN;
446 sc->sc_state &= ~UHID_IMMED;
454 uhidclose(struct cdev *dev, int flag, int mode, struct thread *p)
456 struct uhid_softc *sc;
458 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
460 DPRINTF(("uhidclose: sc=%p\n", sc));
462 /* Disable interrupts. */
463 usbd_abort_pipe(sc->sc_intrpipe);
464 usbd_close_pipe(sc->sc_intrpipe);
467 ndflush(&sc->sc_q, sc->sc_q.c_cc);
468 clist_free_cblocks(&sc->sc_q);
470 free(sc->sc_ibuf, M_USBDEV);
471 free(sc->sc_obuf, M_USBDEV);
472 sc->sc_ibuf = sc->sc_obuf = NULL;
474 sc->sc_state &= ~UHID_OPEN;
482 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
487 u_char buffer[UHID_CHUNK];
490 DPRINTFN(1, ("uhidread\n"));
491 if (sc->sc_state & UHID_IMMED) {
492 DPRINTFN(1, ("uhidread immed\n"));
494 err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
495 sc->sc_iid, buffer, sc->sc_isize);
498 return (uiomove(buffer, sc->sc_isize, uio));
502 while (sc->sc_q.c_cc == 0) {
503 if (flag & O_NONBLOCK) {
505 return (EWOULDBLOCK);
507 sc->sc_state |= UHID_ASLP;
508 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
509 error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
510 DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
514 sc->sc_state &= ~UHID_ASLP;
517 if (sc->sc_state & UHID_NEEDCLEAR) {
518 DPRINTFN(-1,("uhidread: clearing stall\n"));
519 sc->sc_state &= ~UHID_NEEDCLEAR;
520 usbd_clear_endpoint_stall(sc->sc_intrpipe);
525 /* Transfer as many chunks as possible. */
526 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
527 length = min(sc->sc_q.c_cc, uio->uio_resid);
528 if (length > sizeof(buffer))
529 length = sizeof(buffer);
531 /* Remove a small chunk from the input queue. */
532 (void) q_to_b(&sc->sc_q, buffer, length);
533 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
535 /* Copy the data to the user process. */
536 if ((error = uiomove(buffer, length, uio)) != 0)
544 uhidread(struct cdev *dev, struct uio *uio, int flag)
546 struct uhid_softc *sc;
549 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
551 error = uhid_do_read(sc, uio, flag);
552 if (--sc->sc_refcnt < 0)
553 usb_detach_wakeup(sc->sc_dev);
558 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
564 DPRINTFN(1, ("uhidwrite\n"));
571 if (uio->uio_resid != size)
573 error = uiomove(sc->sc_obuf, size, uio);
576 err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
577 sc->sc_obuf[0], sc->sc_obuf+1, size-1);
579 err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
580 0, sc->sc_obuf, size);
589 uhidwrite(struct cdev *dev, struct uio *uio, int flag)
591 struct uhid_softc *sc;
594 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
596 error = uhid_do_write(sc, uio, flag);
597 if (--sc->sc_refcnt < 0)
598 usb_detach_wakeup(sc->sc_dev);
603 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, caddr_t addr, int flag,
606 struct usb_ctl_report_desc *rd;
607 struct usb_ctl_report *re;
611 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
618 /* All handled in the upper FS layer. */
623 if (sc->sc_async != NULL)
625 sc->sc_async = p->td_proc;
626 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", sc->sc_async));
631 /* XXX this is not the most general solution. */
633 if (sc->sc_async == NULL)
635 if (*(int *)addr != sc->sc_async->p_pgid)
639 case USB_GET_REPORT_DESC:
640 rd = (struct usb_ctl_report_desc *)addr;
641 size = min(sc->sc_repdesc_size, sizeof rd->ucrd_data);
642 rd->ucrd_size = size;
643 memcpy(rd->ucrd_data, sc->sc_repdesc, size);
648 /* XXX should read into ibuf, but does it matter? */
649 err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
650 sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
654 sc->sc_state |= UHID_IMMED;
656 sc->sc_state &= ~UHID_IMMED;
660 re = (struct usb_ctl_report *)addr;
661 switch (re->ucr_report) {
662 case UHID_INPUT_REPORT:
666 case UHID_OUTPUT_REPORT:
670 case UHID_FEATURE_REPORT:
677 err = usbd_get_report(sc->sc_iface, re->ucr_report, id, re->ucr_data,
684 re = (struct usb_ctl_report *)addr;
685 switch (re->ucr_report) {
686 case UHID_INPUT_REPORT:
690 case UHID_OUTPUT_REPORT:
694 case UHID_FEATURE_REPORT:
701 err = usbd_set_report(sc->sc_iface, re->ucr_report, id, re->ucr_data,
707 case USB_GET_REPORT_ID:
708 *(int *)addr = 0; /* XXX: we only support reportid 0? */
718 uhidioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *p)
720 struct uhid_softc *sc;
723 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
725 error = uhid_do_ioctl(sc, cmd, addr, flag, p);
726 if (--sc->sc_refcnt < 0)
727 usb_detach_wakeup(sc->sc_dev);
732 uhidpoll(struct cdev *dev, int events, struct thread *p)
734 struct uhid_softc *sc;
738 sc = devclass_get_softc(uhid_devclass, UHIDUNIT(dev));
743 if (events & (POLLOUT | POLLWRNORM))
744 revents |= events & (POLLOUT | POLLWRNORM);
745 if (events & (POLLIN | POLLRDNORM)) {
746 if (sc->sc_q.c_cc > 0)
747 revents |= events & (POLLIN | POLLRDNORM);
749 selrecord(p, &sc->sc_rsel);