Annotation of src/sys/dev/usb/uhidev.c, Revision 1.61.4.1
1.61.4.1! skrll 1: /* $NetBSD: uhidev.c,v 1.61 2014/08/10 16:44:36 tls Exp $ */
1.1 augustss 2:
3: /*
1.56 mrg 4: * Copyright (c) 2001, 2012 The NetBSD Foundation, Inc.
1.1 augustss 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Lennart Augustsson (lennart@augustsson.net) at
1.56 mrg 9: * Carlstedt Research & Technology and Matthew R. Green (mrg@eterna.com.au).
1.1 augustss 10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30: * POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: /*
1.14 augustss 34: * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
1.1 augustss 35: */
1.15 lukem 36:
37: #include <sys/cdefs.h>
1.61.4.1! skrll 38: __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.61 2014/08/10 16:44:36 tls Exp $");
1.1 augustss 39:
40: #include <sys/param.h>
41: #include <sys/systm.h>
42: #include <sys/kernel.h>
43: #include <sys/malloc.h>
44: #include <sys/signalvar.h>
45: #include <sys/device.h>
46: #include <sys/ioctl.h>
47: #include <sys/conf.h>
48:
49: #include <dev/usb/usb.h>
50: #include <dev/usb/usbhid.h>
51:
52: #include <dev/usb/usbdevs.h>
53: #include <dev/usb/usbdi.h>
54: #include <dev/usb/usbdi_util.h>
55: #include <dev/usb/hid.h>
56: #include <dev/usb/usb_quirks.h>
57:
58: #include <dev/usb/uhidev.h>
59:
60: /* Report descriptor for broken Wacom Graphire */
61: #include <dev/usb/ugraphire_rdesc.h>
1.51 jmcneill 62: /* Report descriptor for game controllers in "XInput" mode */
63: #include <dev/usb/xinput_rdesc.h>
1.1 augustss 64:
1.22 drochner 65: #include "locators.h"
66:
1.1 augustss 67: #ifdef UHIDEV_DEBUG
1.48 dyoung 68: #define DPRINTF(x) if (uhidevdebug) printf x
69: #define DPRINTFN(n,x) if (uhidevdebug>(n)) printf x
1.1 augustss 70: int uhidevdebug = 0;
71: #else
72: #define DPRINTF(x)
73: #define DPRINTFN(n,x)
74: #endif
75:
76: Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
77:
1.29 drochner 78: Static int uhidev_maxrepid(void *, int);
79: Static int uhidevprint(void *, const char *);
1.1 augustss 80:
1.41 cube 81: int uhidev_match(device_t, cfdata_t, void *);
1.39 dyoung 82: void uhidev_attach(device_t, device_t, void *);
83: void uhidev_childdet(device_t, device_t);
84: int uhidev_detach(device_t, int);
85: int uhidev_activate(device_t, enum devact);
86: extern struct cfdriver uhidev_cd;
1.41 cube 87: CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
1.39 dyoung 88: uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
1.1 augustss 89:
1.58 skrll 90: int
1.48 dyoung 91: uhidev_match(device_t parent, cfdata_t match, void *aux)
1.1 augustss 92: {
1.48 dyoung 93: struct usbif_attach_arg *uaa = aux;
1.6 augustss 94:
1.51 jmcneill 95: /* Game controllers in "XInput" mode */
96: if (USBIF_IS_XINPUT(uaa))
97: return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
1.34 drochner 98: if (uaa->class != UICLASS_HID)
1.1 augustss 99: return (UMATCH_NONE);
1.36 veego 100: if (usbd_get_quirks(uaa->device)->uq_flags & UQ_HID_IGNORE)
101: return (UMATCH_NONE);
1.1 augustss 102: return (UMATCH_IFACECLASS_GENERIC);
103: }
104:
1.58 skrll 105: void
1.48 dyoung 106: uhidev_attach(device_t parent, device_t self, void *aux)
1.1 augustss 107: {
1.48 dyoung 108: struct uhidev_softc *sc = device_private(self);
109: struct usbif_attach_arg *uaa = aux;
1.1 augustss 110: usbd_interface_handle iface = uaa->iface;
111: usb_interface_descriptor_t *id;
112: usb_endpoint_descriptor_t *ed;
113: struct uhidev_attach_arg uha;
1.42 drochner 114: device_t dev;
115: struct uhidev *csc;
1.44 rafal 116: int maxinpktsize, size, nrepid, repid, repsz;
1.32 christos 117: int *repsizes;
1.25 skrll 118: int i;
1.19 jdolecek 119: void *desc;
1.20 augustss 120: const void *descptr;
1.1 augustss 121: usbd_status err;
1.26 augustss 122: char *devinfop;
1.28 drochner 123: int locs[UHIDBUSCF_NLOCS];
1.6 augustss 124:
1.41 cube 125: sc->sc_dev = self;
1.1 augustss 126: sc->sc_udev = uaa->device;
127: sc->sc_iface = iface;
1.43 plunky 128:
129: aprint_naive("\n");
130: aprint_normal("\n");
131:
1.56 mrg 132: mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_USB);
133:
1.1 augustss 134: id = usbd_get_interface_descriptor(iface);
1.26 augustss 135:
136: devinfop = usbd_devinfo_alloc(uaa->device, 0);
1.41 cube 137: aprint_normal_dev(self, "%s, iclass %d/%d\n",
1.26 augustss 138: devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
139: usbd_devinfo_free(devinfop);
1.1 augustss 140:
1.38 jmcneill 141: if (!pmf_device_register(self, NULL, NULL))
142: aprint_error_dev(self, "couldn't establish power handler\n");
143:
1.1 augustss 144: (void)usbd_set_idle(iface, 0, 0);
145: #if 0
146:
147: qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
148: if ((qflags & UQ_NO_SET_PROTO) == 0 &&
149: id->bInterfaceSubClass != UISUBCLASS_BOOT)
150: (void)usbd_set_protocol(iface, 1);
151: #endif
152:
1.44 rafal 153: maxinpktsize = 0;
1.25 skrll 154: sc->sc_iep_addr = sc->sc_oep_addr = -1;
155: for (i = 0; i < id->bNumEndpoints; i++) {
156: ed = usbd_interface2endpoint_descriptor(iface, i);
157: if (ed == NULL) {
1.41 cube 158: aprint_error_dev(self,
159: "could not read endpoint descriptor\n");
1.25 skrll 160: sc->sc_dying = 1;
1.48 dyoung 161: return;
1.25 skrll 162: }
163:
164: DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
165: "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
166: " bInterval=%d\n",
167: ed->bLength, ed->bDescriptorType,
168: ed->bEndpointAddress & UE_ADDR,
169: UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
170: ed->bmAttributes & UE_XFERTYPE,
171: UGETW(ed->wMaxPacketSize), ed->bInterval));
172:
173: if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
174: (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
1.44 rafal 175: maxinpktsize = UGETW(ed->wMaxPacketSize);
1.25 skrll 176: sc->sc_iep_addr = ed->bEndpointAddress;
177: } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
178: (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
179: sc->sc_oep_addr = ed->bEndpointAddress;
180: } else {
1.41 cube 181: aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
1.25 skrll 182: }
1.1 augustss 183: }
184:
1.25 skrll 185: /*
186: * Check that we found an input interrupt endpoint. The output interrupt
187: * endpoint is optional
188: */
189: if (sc->sc_iep_addr == -1) {
1.41 cube 190: aprint_error_dev(self, "no input interrupt endpoint\n");
1.1 augustss 191: sc->sc_dying = 1;
1.48 dyoung 192: return;
1.1 augustss 193: }
194:
195: /* XXX need to extend this */
1.20 augustss 196: descptr = NULL;
197: if (uaa->vendor == USB_VENDOR_WACOM) {
198: static uByte reportbuf[] = {2, 2, 2};
1.17 augustss 199:
1.1 augustss 200: /* The report descriptor for the Wacom Graphire is broken. */
1.17 augustss 201: switch (uaa->product) {
202: case USB_PRODUCT_WACOM_GRAPHIRE:
1.49 tsutsui 203: case USB_PRODUCT_WACOM_GRAPHIRE2:
1.33 ghen 204: case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
205: case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
206: case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
1.17 augustss 207: /*
208: * The Graphire3 needs 0x0202 to be written to
209: * feature report ID 2 before it'll start
210: * returning digitizer data.
211: */
212: usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
1.20 augustss 213: &reportbuf, sizeof reportbuf);
1.17 augustss 214:
215: size = sizeof uhid_graphire3_4x5_report_descr;
216: descptr = uhid_graphire3_4x5_report_descr;
217: break;
1.20 augustss 218: default:
219: /* Keep descriptor */
220: break;
1.17 augustss 221: }
222: }
1.51 jmcneill 223: if (USBIF_IS_XINPUT(uaa)) {
224: size = sizeof uhid_xinput_report_descr;
225: descptr = uhid_xinput_report_descr;
226: }
1.17 augustss 227:
228: if (descptr) {
1.1 augustss 229: desc = malloc(size, M_USBDEV, M_NOWAIT);
230: if (desc == NULL)
231: err = USBD_NOMEM;
232: else {
233: err = USBD_NORMAL_COMPLETION;
1.17 augustss 234: memcpy(desc, descptr, size);
1.1 augustss 235: }
236: } else {
237: desc = NULL;
1.17 augustss 238: err = usbd_read_report_desc(uaa->iface, &desc, &size,
239: M_USBDEV);
1.1 augustss 240: }
241: if (err) {
1.41 cube 242: aprint_error_dev(self, "no report descriptor\n");
1.1 augustss 243: sc->sc_dying = 1;
1.48 dyoung 244: return;
1.1 augustss 245: }
1.6 augustss 246:
1.58 skrll 247: if (uaa->vendor == USB_VENDOR_HOSIDEN &&
1.27 augustss 248: uaa->product == USB_PRODUCT_HOSIDEN_PPP) {
249: static uByte reportbuf[] = { 1 };
250: /*
1.58 skrll 251: * This device was sold by Konami with its ParaParaParadise
1.27 augustss 252: * game for PlayStation2. It needs to be "turned on"
253: * before it will send any reports.
254: */
255:
256: usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 0,
257: &reportbuf, sizeof reportbuf);
258: }
259:
1.47 jakllsch 260: if (uaa->vendor == USB_VENDOR_LOGITECH &&
261: uaa->product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
262: uint8_t *data = desc;
263: /*
264: * This device has a odd USAGE_MINIMUM value that would
265: * cause the multimedia keys to have their usage number
266: * shifted up one usage. Adjust so the usages are sane.
267: */
268:
269: if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
270: data[0x58] == 0x2a && data[0x59] == 0x8c)
271: data[0x57] = 0x00;
272: }
273:
1.52 aymeric 274: /*
275: * Enable the Six Axis and DualShock 3 controllers.
276: * See http://ps3.jim.sh/sixaxis/usb/
277: */
278: if (uaa->vendor == USB_VENDOR_SONY &&
279: uaa->product == USB_PRODUCT_SONY_PS3CONTROLLER) {
280: usb_device_request_t req;
281: char data[17];
282: int actlen;
283:
284: req.bmRequestType = UT_READ_CLASS_INTERFACE;
285: req.bRequest = 1;
286: USETW(req.wValue, 0x3f2);
287: USETW(req.wIndex, 0);
288: USETW(req.wLength, sizeof data);
289:
290: usbd_do_request_flags(sc->sc_udev, &req, data,
291: USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
292: }
293:
1.1 augustss 294: sc->sc_repdesc = desc;
295: sc->sc_repdesc_size = size;
296:
297: uha.uaa = uaa;
298: nrepid = uhidev_maxrepid(desc, size);
299: if (nrepid < 0)
1.48 dyoung 300: return;
1.1 augustss 301: if (nrepid > 0)
1.41 cube 302: aprint_normal_dev(self, "%d report ids\n", nrepid);
1.1 augustss 303: nrepid++;
1.32 christos 304: repsizes = malloc(nrepid * sizeof(*repsizes), M_TEMP, M_NOWAIT);
305: if (repsizes == NULL)
306: goto nomem;
1.39 dyoung 307: sc->sc_subdevs = malloc(nrepid * sizeof(device_t),
1.5 augustss 308: M_USBDEV, M_NOWAIT | M_ZERO);
1.1 augustss 309: if (sc->sc_subdevs == NULL) {
1.32 christos 310: free(repsizes, M_TEMP);
311: nomem:
1.41 cube 312: aprint_error_dev(self, "no memory\n");
1.48 dyoung 313: return;
1.1 augustss 314: }
1.44 rafal 315:
316: /* Just request max packet size for the interrupt pipe */
317: sc->sc_isize = maxinpktsize;
1.1 augustss 318: sc->sc_nrepid = nrepid;
319:
320: usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
1.48 dyoung 321: sc->sc_dev);
1.1 augustss 322:
323: for (repid = 0; repid < nrepid; repid++) {
324: repsz = hid_report_size(desc, size, hid_input, repid);
325: DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
1.2 augustss 326: repsizes[repid] = repsz;
1.1 augustss 327: }
1.44 rafal 328:
1.1 augustss 329: DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
330:
331: uha.parent = sc;
332: for (repid = 0; repid < nrepid; repid++) {
333: DPRINTF(("uhidev_match: try repid=%d\n", repid));
334: if (hid_report_size(desc, size, hid_input, repid) == 0 &&
335: hid_report_size(desc, size, hid_output, repid) == 0 &&
336: hid_report_size(desc, size, hid_feature, repid) == 0) {
1.5 augustss 337: ; /* already NULL in sc->sc_subdevs[repid] */
1.1 augustss 338: } else {
339: uha.reportid = repid;
1.28 drochner 340: locs[UHIDBUSCF_REPORTID] = repid;
1.22 drochner 341:
1.42 drochner 342: dev = config_found_sm_loc(self,
1.28 drochner 343: "uhidbus", locs, &uha,
1.42 drochner 344: uhidevprint, config_stdsubmatch);
1.1 augustss 345: sc->sc_subdevs[repid] = dev;
1.4 augustss 346: if (dev != NULL) {
1.42 drochner 347: csc = device_private(dev);
348: csc->sc_in_rep_size = repsizes[repid];
1.1 augustss 349: #ifdef DIAGNOSTIC
350: DPRINTF(("uhidev_match: repid=%d dev=%p\n",
351: repid, dev));
1.42 drochner 352: if (csc->sc_intr == NULL) {
1.32 christos 353: free(repsizes, M_TEMP);
1.41 cube 354: aprint_error_dev(self,
355: "sc_intr == NULL\n");
1.48 dyoung 356: return;
1.1 augustss 357: }
1.4 augustss 358: #endif
1.42 drochner 359: rnd_attach_source(&csc->rnd_source,
1.48 dyoung 360: device_xname(dev),
1.61 tls 361: RND_TYPE_TTY,
362: RND_FLAG_DEFAULT);
1.1 augustss 363: }
364: }
365: }
1.32 christos 366: free(repsizes, M_TEMP);
1.1 augustss 367:
1.48 dyoung 368: return;
1.1 augustss 369: }
370:
371: int
372: uhidev_maxrepid(void *buf, int len)
373: {
374: struct hid_data *d;
375: struct hid_item h;
376: int maxid;
377:
378: maxid = -1;
379: h.report_ID = 0;
380: for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
381: if (h.report_ID > maxid)
382: maxid = h.report_ID;
383: hid_end_parse(d);
384: return (maxid);
385: }
386:
387: int
388: uhidevprint(void *aux, const char *pnp)
389: {
390: struct uhidev_attach_arg *uha = aux;
391:
392: if (pnp)
1.12 thorpej 393: aprint_normal("uhid at %s", pnp);
1.1 augustss 394: if (uha->reportid != 0)
1.12 thorpej 395: aprint_normal(" reportid %d", uha->reportid);
1.1 augustss 396: return (UNCONF);
397: }
398:
399: int
1.39 dyoung 400: uhidev_activate(device_t self, enum devact act)
1.1 augustss 401: {
1.39 dyoung 402: struct uhidev_softc *sc = device_private(self);
1.1 augustss 403:
404: switch (act) {
405: case DVACT_DEACTIVATE:
406: sc->sc_dying = 1;
1.45 dyoung 407: return 0;
1.16 christos 408: default:
1.45 dyoung 409: return EOPNOTSUPP;
1.1 augustss 410: }
411: }
412:
1.39 dyoung 413: void
414: uhidev_childdet(device_t self, device_t child)
415: {
416: int i;
417: struct uhidev_softc *sc = device_private(self);
418:
419: for (i = 0; i < sc->sc_nrepid; i++) {
1.42 drochner 420: if (sc->sc_subdevs[i] == child)
1.39 dyoung 421: break;
422: }
423: KASSERT(i < sc->sc_nrepid);
424: sc->sc_subdevs[i] = NULL;
425: }
426:
1.58 skrll 427: int
1.48 dyoung 428: uhidev_detach(device_t self, int flags)
1.1 augustss 429: {
1.48 dyoung 430: struct uhidev_softc *sc = device_private(self);
1.1 augustss 431: int i, rv;
1.42 drochner 432: struct uhidev *csc;
1.1 augustss 433:
434: DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
435:
436: sc->sc_dying = 1;
1.25 skrll 437: if (sc->sc_ipipe != NULL)
438: usbd_abort_pipe(sc->sc_ipipe);
1.1 augustss 439:
440: if (sc->sc_repdesc != NULL)
441: free(sc->sc_repdesc, M_USBDEV);
442:
443: rv = 0;
444: for (i = 0; i < sc->sc_nrepid; i++) {
445: if (sc->sc_subdevs[i] != NULL) {
1.42 drochner 446: csc = device_private(sc->sc_subdevs[i]);
447: rnd_detach_source(&csc->rnd_source);
448: rv |= config_detach(sc->sc_subdevs[i], flags);
1.1 augustss 449: }
450: }
451:
452: usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1.48 dyoung 453: sc->sc_dev);
1.1 augustss 454:
1.38 jmcneill 455: pmf_device_deregister(self);
1.56 mrg 456: mutex_destroy(&sc->sc_lock);
1.38 jmcneill 457:
1.1 augustss 458: return (rv);
459: }
460:
461: void
462: uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
463: {
464: struct uhidev_softc *sc = addr;
1.42 drochner 465: device_t cdev;
1.1 augustss 466: struct uhidev *scd;
467: u_char *p;
468: u_int rep;
1.61.4.1! skrll 469: uint32_t cc;
1.1 augustss 470:
471: usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
472:
473: #ifdef UHIDEV_DEBUG
474: if (uhidevdebug > 5) {
1.61.4.1! skrll 475: uint32_t i;
1.6 augustss 476:
1.1 augustss 477: DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
478: DPRINTF(("uhidev_intr: data ="));
479: for (i = 0; i < cc; i++)
480: DPRINTF((" %02x", sc->sc_ibuf[i]));
481: DPRINTF(("\n"));
482: }
483: #endif
484:
485: if (status == USBD_CANCELLED)
486: return;
487:
488: if (status != USBD_NORMAL_COMPLETION) {
1.48 dyoung 489: DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
1.3 augustss 490: status));
1.25 skrll 491: usbd_clear_endpoint_stall_async(sc->sc_ipipe);
1.1 augustss 492: return;
493: }
494:
495: p = sc->sc_ibuf;
496: if (sc->sc_nrepid != 1)
497: rep = *p++, cc--;
498: else
499: rep = 0;
500: if (rep >= sc->sc_nrepid) {
501: printf("uhidev_intr: bad repid %d\n", rep);
502: return;
503: }
1.42 drochner 504: cdev = sc->sc_subdevs[rep];
505: if (!cdev)
506: return;
507: scd = device_private(cdev);
1.1 augustss 508: DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
509: rep, scd, scd ? scd->sc_state : 0));
1.42 drochner 510: if (!(scd->sc_state & UHIDEV_OPEN))
1.1 augustss 511: return;
1.46 jakllsch 512: #ifdef UHIDEV_DEBUG
513: if (scd->sc_in_rep_size != cc) {
514: DPRINTF(("%s: expected %d bytes, got %d\n",
1.48 dyoung 515: device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
1.46 jakllsch 516: }
517: #endif
518: if (cc == 0) {
519: DPRINTF(("%s: 0-length input ignored\n",
1.48 dyoung 520: device_xname(sc->sc_dev)));
1.23 augustss 521: return;
522: }
1.10 fair 523: rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
1.1 augustss 524: scd->sc_intr(scd, p, cc);
525: }
526:
527: void
528: uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
529: {
530: *desc = sc->sc_repdesc;
531: *size = sc->sc_repdesc_size;
532: }
533:
534: int
535: uhidev_open(struct uhidev *scd)
536: {
537: struct uhidev_softc *sc = scd->sc_parent;
538: usbd_status err;
1.25 skrll 539: int error;
1.1 augustss 540:
1.56 mrg 541: DPRINTF(("uhidev_open: open pipe, state=%d\n", scd->sc_state));
1.1 augustss 542:
1.56 mrg 543: mutex_enter(&sc->sc_lock);
544: if (scd->sc_state & UHIDEV_OPEN) {
545: mutex_exit(&sc->sc_lock);
1.1 augustss 546: return (EBUSY);
1.56 mrg 547: }
1.1 augustss 548: scd->sc_state |= UHIDEV_OPEN;
1.60 skrll 549: if (sc->sc_refcnt++) {
550: mutex_exit(&sc->sc_lock);
551: return (0);
552: }
1.56 mrg 553: mutex_exit(&sc->sc_lock);
1.1 augustss 554:
555: if (sc->sc_isize == 0)
556: return (0);
557:
558: sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
559:
1.25 skrll 560: /* Set up input interrupt pipe. */
1.1 augustss 561: DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
1.25 skrll 562: sc->sc_iep_addr));
1.58 skrll 563:
1.25 skrll 564: err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
565: USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
1.1 augustss 566: sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
1.25 skrll 567: if (err != USBD_NORMAL_COMPLETION) {
1.1 augustss 568: DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
1.25 skrll 569: "error=%d\n", err));
570: error = EIO;
571: goto out1;
572: }
573:
574: /*
575: * Set up output interrupt pipe if an output interrupt endpoint
576: * exists.
577: */
578: if (sc->sc_oep_addr != -1) {
579: DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
580:
581: err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
582: 0, &sc->sc_opipe);
583:
584: if (err != USBD_NORMAL_COMPLETION) {
585: DPRINTF(("uhidev_open: usbd_open_pipe failed, "
586: "error=%d\n", err));
587: error = EIO;
588: goto out2;
589: }
590: DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
591:
592: sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
593: if (sc->sc_oxfer == NULL) {
594: DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
595: error = ENOMEM;
596: goto out3;
597: }
1.1 augustss 598: }
1.58 skrll 599:
1.1 augustss 600: return (0);
1.25 skrll 601: out3:
602: /* Abort output pipe */
603: usbd_close_pipe(sc->sc_opipe);
604: out2:
605: /* Abort input pipe */
606: usbd_close_pipe(sc->sc_ipipe);
607: out1:
608: DPRINTF(("uhidev_open: failed in someway"));
609: free(sc->sc_ibuf, M_USBDEV);
1.56 mrg 610: mutex_enter(&sc->sc_lock);
1.25 skrll 611: scd->sc_state &= ~UHIDEV_OPEN;
1.60 skrll 612: sc->sc_refcnt = 0;
1.59 christos 613: sc->sc_ibuf = NULL;
1.25 skrll 614: sc->sc_ipipe = NULL;
615: sc->sc_opipe = NULL;
616: sc->sc_oxfer = NULL;
1.56 mrg 617: mutex_exit(&sc->sc_lock);
1.25 skrll 618: return error;
1.1 augustss 619: }
620:
621: void
622: uhidev_close(struct uhidev *scd)
623: {
624: struct uhidev_softc *sc = scd->sc_parent;
625:
1.56 mrg 626: mutex_enter(&sc->sc_lock);
627: if (!(scd->sc_state & UHIDEV_OPEN)) {
628: mutex_exit(&sc->sc_lock);
1.1 augustss 629: return;
1.56 mrg 630: }
1.1 augustss 631: scd->sc_state &= ~UHIDEV_OPEN;
1.60 skrll 632: if (--sc->sc_refcnt) {
633: mutex_exit(&sc->sc_lock);
634: return;
635: }
1.56 mrg 636: mutex_exit(&sc->sc_lock);
637:
1.1 augustss 638: DPRINTF(("uhidev_close: close pipe\n"));
639:
1.59 christos 640: if (sc->sc_oxfer != NULL) {
1.25 skrll 641: usbd_free_xfer(sc->sc_oxfer);
1.59 christos 642: sc->sc_oxfer = NULL;
643: }
1.58 skrll 644:
1.1 augustss 645: /* Disable interrupts. */
1.25 skrll 646: if (sc->sc_opipe != NULL) {
647: usbd_abort_pipe(sc->sc_opipe);
648: usbd_close_pipe(sc->sc_opipe);
649: sc->sc_opipe = NULL;
650: }
651:
652: if (sc->sc_ipipe != NULL) {
653: usbd_abort_pipe(sc->sc_ipipe);
654: usbd_close_pipe(sc->sc_ipipe);
655: sc->sc_ipipe = NULL;
1.1 augustss 656: }
657:
658: if (sc->sc_ibuf != NULL) {
659: free(sc->sc_ibuf, M_USBDEV);
660: sc->sc_ibuf = NULL;
661: }
662: }
663:
664: usbd_status
665: uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
666: {
1.13 dsainty 667: char *buf;
668: usbd_status retstat;
669:
670: if (scd->sc_report_id == 0)
671: return usbd_set_report(scd->sc_parent->sc_iface, type,
672: scd->sc_report_id, data, len);
673:
674: buf = malloc(len + 1, M_TEMP, M_WAITOK);
675: buf[0] = scd->sc_report_id;
676: memcpy(buf+1, data, len);
677:
678: retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
1.18 dsainty 679: scd->sc_report_id, buf, len + 1);
1.13 dsainty 680:
681: free(buf, M_TEMP);
1.1 augustss 682:
1.13 dsainty 683: return retstat;
1.1 augustss 684: }
685:
686: usbd_status
687: uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
688: {
1.6 augustss 689: return usbd_get_report(scd->sc_parent->sc_iface, type,
1.1 augustss 690: scd->sc_report_id, data, len);
691: }
1.25 skrll 692:
693: usbd_status
694: uhidev_write(struct uhidev_softc *sc, void *data, int len)
695: {
696:
697: DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
698:
699: if (sc->sc_opipe == NULL)
700: return USBD_INVAL;
701:
702: #ifdef UHIDEV_DEBUG
703: if (uhidevdebug > 50) {
704:
1.61.4.1! skrll 705: uint32_t i;
! 706: uint8_t *d = data;
1.25 skrll 707:
708: DPRINTF(("uhidev_write: data ="));
709: for (i = 0; i < len; i++)
710: DPRINTF((" %02x", d[i]));
711: DPRINTF(("\n"));
712: }
713: #endif
714: return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
715: USBD_NO_TIMEOUT, data, &len, "uhidevwi");
716: }
CVSweb <webmaster@jp.NetBSD.org>