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