uhidev.c revision 1.61.2.2.4.1 1 /* $NetBSD: uhidev.c,v 1.61.2.2.4.1 2016/09/06 20:33:09 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.2.2.4.1 2016/09/06 20:33:09 skrll Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_usb.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/kmem.h>
48 #include <sys/signalvar.h>
49 #include <sys/device.h>
50 #include <sys/ioctl.h>
51 #include <sys/conf.h>
52
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbhid.h>
55
56 #include <dev/usb/usbdevs.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/hid.h>
60 #include <dev/usb/usb_quirks.h>
61
62 #include <dev/usb/uhidev.h>
63
64 /* Report descriptor for broken Wacom Graphire */
65 #include <dev/usb/ugraphire_rdesc.h>
66 /* Report descriptor for game controllers in "XInput" mode */
67 #include <dev/usb/xinput_rdesc.h>
68 /* Report descriptor for Xbox One controllers */
69 #include <dev/usb/x1input_rdesc.h>
70
71 #include "locators.h"
72
73 #ifdef UHIDEV_DEBUG
74 #define DPRINTF(x) if (uhidevdebug) printf x
75 #define DPRINTFN(n,x) if (uhidevdebug>(n)) printf x
76 int uhidevdebug = 0;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n,x)
80 #endif
81
82 Static void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
83
84 Static int uhidev_maxrepid(void *, int);
85 Static int uhidevprint(void *, const char *);
86
87 int uhidev_match(device_t, cfdata_t, void *);
88 void uhidev_attach(device_t, device_t, void *);
89 void uhidev_childdet(device_t, device_t);
90 int uhidev_detach(device_t, int);
91 int uhidev_activate(device_t, enum devact);
92 extern struct cfdriver uhidev_cd;
93 CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
94 uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
95
96 int
97 uhidev_match(device_t parent, cfdata_t match, void *aux)
98 {
99 struct usbif_attach_arg *uiaa = aux;
100
101 /* Game controllers in "XInput" mode */
102 if (USBIF_IS_XINPUT(uiaa))
103 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
104 /* Xbox One controllers */
105 if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0)
106 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
107
108 if (uiaa->uiaa_class != UICLASS_HID)
109 return UMATCH_NONE;
110 if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE)
111 return UMATCH_NONE;
112 return UMATCH_IFACECLASS_GENERIC;
113 }
114
115 void
116 uhidev_attach(device_t parent, device_t self, void *aux)
117 {
118 struct uhidev_softc *sc = device_private(self);
119 struct usbif_attach_arg *uiaa = aux;
120 struct usbd_interface *iface = uiaa->uiaa_iface;
121 usb_interface_descriptor_t *id;
122 usb_endpoint_descriptor_t *ed;
123 struct uhidev_attach_arg uha;
124 device_t dev;
125 struct uhidev *csc;
126 int maxinpktsize, size, nrepid, repid, repsz;
127 int *repsizes;
128 int i;
129 void *desc;
130 const void *descptr;
131 usbd_status err;
132 char *devinfop;
133 int locs[UHIDBUSCF_NLOCS];
134
135 sc->sc_dev = self;
136 sc->sc_udev = uiaa->uiaa_device;
137 sc->sc_iface = iface;
138
139 aprint_naive("\n");
140 aprint_normal("\n");
141
142 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
143
144 id = usbd_get_interface_descriptor(iface);
145
146 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
147 aprint_normal_dev(self, "%s, iclass %d/%d\n",
148 devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
149 usbd_devinfo_free(devinfop);
150
151 if (!pmf_device_register(self, NULL, NULL))
152 aprint_error_dev(self, "couldn't establish power handler\n");
153
154 (void)usbd_set_idle(iface, 0, 0);
155 #if 0
156
157 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0 &&
158 id->bInterfaceSubClass != UISUBCLASS_BOOT)
159 (void)usbd_set_protocol(iface, 1);
160 #endif
161
162 maxinpktsize = 0;
163 sc->sc_iep_addr = sc->sc_oep_addr = -1;
164 for (i = 0; i < id->bNumEndpoints; i++) {
165 ed = usbd_interface2endpoint_descriptor(iface, i);
166 if (ed == NULL) {
167 aprint_error_dev(self,
168 "could not read endpoint descriptor\n");
169 sc->sc_dying = 1;
170 return;
171 }
172
173 DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
174 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
175 " bInterval=%d\n",
176 ed->bLength, ed->bDescriptorType,
177 ed->bEndpointAddress & UE_ADDR,
178 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
179 ed->bmAttributes & UE_XFERTYPE,
180 UGETW(ed->wMaxPacketSize), ed->bInterval));
181
182 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
183 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
184 maxinpktsize = UGETW(ed->wMaxPacketSize);
185 sc->sc_iep_addr = ed->bEndpointAddress;
186 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
187 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
188 sc->sc_oep_addr = ed->bEndpointAddress;
189 } else {
190 aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
191 }
192 }
193
194 /*
195 * Check that we found an input interrupt endpoint. The output interrupt
196 * endpoint is optional
197 */
198 if (sc->sc_iep_addr == -1) {
199 aprint_error_dev(self, "no input interrupt endpoint\n");
200 sc->sc_dying = 1;
201 return;
202 }
203
204 /* XXX need to extend this */
205 descptr = NULL;
206 if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
207 static uByte reportbuf[] = {2, 2, 2};
208
209 /* The report descriptor for the Wacom Graphire is broken. */
210 switch (uiaa->uiaa_product) {
211 case USB_PRODUCT_WACOM_GRAPHIRE:
212 case USB_PRODUCT_WACOM_GRAPHIRE2:
213 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
214 case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
215 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
216 /*
217 * The Graphire3 needs 0x0202 to be written to
218 * feature report ID 2 before it'll start
219 * returning digitizer data.
220 */
221 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
222 &reportbuf, sizeof(reportbuf));
223
224 size = sizeof(uhid_graphire3_4x5_report_descr);
225 descptr = uhid_graphire3_4x5_report_descr;
226 break;
227 default:
228 /* Keep descriptor */
229 break;
230 }
231 }
232 if (USBIF_IS_XINPUT(uiaa)) {
233 size = sizeof(uhid_xinput_report_descr);
234 descptr = uhid_xinput_report_descr;
235 }
236 if (USBIF_IS_X1INPUT(uiaa)) {
237 sc->sc_flags |= UHIDEV_F_XB1;
238 size = sizeof(uhid_x1input_report_descr);
239 descptr = uhid_x1input_report_descr;
240 }
241
242 if (descptr) {
243 desc = kmem_alloc(size, KM_SLEEP);
244 if (desc == NULL)
245 err = USBD_NOMEM;
246 else {
247 err = USBD_NORMAL_COMPLETION;
248 memcpy(desc, descptr, size);
249 }
250 } else {
251 desc = NULL;
252 err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size);
253 }
254 if (err) {
255 aprint_error_dev(self, "no report descriptor\n");
256 sc->sc_dying = 1;
257 return;
258 }
259
260 if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN &&
261 uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) {
262 static uByte reportbuf[] = { 1 };
263 /*
264 * This device was sold by Konami with its ParaParaParadise
265 * game for PlayStation2. It needs to be "turned on"
266 * before it will send any reports.
267 */
268
269 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0,
270 &reportbuf, sizeof(reportbuf));
271 }
272
273 if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH &&
274 uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
275 uint8_t *data = desc;
276 /*
277 * This device has a odd USAGE_MINIMUM value that would
278 * cause the multimedia keys to have their usage number
279 * shifted up one usage. Adjust so the usages are sane.
280 */
281
282 if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
283 data[0x58] == 0x2a && data[0x59] == 0x8c)
284 data[0x57] = 0x00;
285 }
286
287 /*
288 * Enable the Six Axis and DualShock 3 controllers.
289 * See http://ps3.jim.sh/sixaxis/usb/
290 */
291 if (uiaa->uiaa_vendor == USB_VENDOR_SONY &&
292 uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) {
293 usb_device_request_t req;
294 char data[17];
295 int actlen;
296
297 req.bmRequestType = UT_READ_CLASS_INTERFACE;
298 req.bRequest = 1;
299 USETW(req.wValue, 0x3f2);
300 USETW(req.wIndex, 0);
301 USETW(req.wLength, sizeof(data));
302
303 usbd_do_request_flags(sc->sc_udev, &req, data,
304 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
305 }
306
307 sc->sc_repdesc = desc;
308 sc->sc_repdesc_size = size;
309
310 uha.uiaa = uiaa;
311 nrepid = uhidev_maxrepid(desc, size);
312 if (nrepid < 0)
313 return;
314 if (nrepid > 0)
315 aprint_normal_dev(self, "%d report ids\n", nrepid);
316 nrepid++;
317 repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP);
318 if (repsizes == NULL)
319 goto nomem;
320 sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(device_t),
321 KM_SLEEP);
322 if (sc->sc_subdevs == NULL) {
323 kmem_free(repsizes, nrepid * sizeof(*repsizes));
324 nomem:
325 aprint_error_dev(self, "no memory\n");
326 return;
327 }
328
329 /* Just request max packet size for the interrupt pipe */
330 sc->sc_isize = maxinpktsize;
331 sc->sc_nrepid = nrepid;
332
333 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
334
335 for (repid = 0; repid < nrepid; repid++) {
336 repsz = hid_report_size(desc, size, hid_input, repid);
337 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
338 repsizes[repid] = repsz;
339 }
340
341 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
342
343 uha.parent = sc;
344 for (repid = 0; repid < nrepid; repid++) {
345 DPRINTF(("uhidev_match: try repid=%d\n", repid));
346 if (hid_report_size(desc, size, hid_input, repid) == 0 &&
347 hid_report_size(desc, size, hid_output, repid) == 0 &&
348 hid_report_size(desc, size, hid_feature, repid) == 0) {
349 ; /* already NULL in sc->sc_subdevs[repid] */
350 } else {
351 uha.reportid = repid;
352 locs[UHIDBUSCF_REPORTID] = repid;
353
354 dev = config_found_sm_loc(self,
355 "uhidbus", locs, &uha,
356 uhidevprint, config_stdsubmatch);
357 sc->sc_subdevs[repid] = dev;
358 if (dev != NULL) {
359 csc = device_private(dev);
360 csc->sc_in_rep_size = repsizes[repid];
361 #ifdef DIAGNOSTIC
362 DPRINTF(("uhidev_match: repid=%d dev=%p\n",
363 repid, dev));
364 if (csc->sc_intr == NULL) {
365 kmem_free(repsizes,
366 nrepid * sizeof(*repsizes));
367 aprint_error_dev(self,
368 "sc_intr == NULL\n");
369 return;
370 }
371 #endif
372 rnd_attach_source(&csc->rnd_source,
373 device_xname(dev),
374 RND_TYPE_TTY,
375 RND_FLAG_DEFAULT);
376 }
377 }
378 }
379 kmem_free(repsizes, nrepid * sizeof(*repsizes));
380
381 return;
382 }
383
384 int
385 uhidev_maxrepid(void *buf, int len)
386 {
387 struct hid_data *d;
388 struct hid_item h;
389 int maxid;
390
391 maxid = -1;
392 h.report_ID = 0;
393 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
394 if (h.report_ID > maxid)
395 maxid = h.report_ID;
396 hid_end_parse(d);
397 return maxid;
398 }
399
400 int
401 uhidevprint(void *aux, const char *pnp)
402 {
403 struct uhidev_attach_arg *uha = aux;
404
405 if (pnp)
406 aprint_normal("uhid at %s", pnp);
407 if (uha->reportid != 0)
408 aprint_normal(" reportid %d", uha->reportid);
409 return UNCONF;
410 }
411
412 int
413 uhidev_activate(device_t self, enum devact act)
414 {
415 struct uhidev_softc *sc = device_private(self);
416
417 switch (act) {
418 case DVACT_DEACTIVATE:
419 sc->sc_dying = 1;
420 return 0;
421 default:
422 return EOPNOTSUPP;
423 }
424 }
425
426 void
427 uhidev_childdet(device_t self, device_t child)
428 {
429 int i;
430 struct uhidev_softc *sc = device_private(self);
431
432 for (i = 0; i < sc->sc_nrepid; i++) {
433 if (sc->sc_subdevs[i] == child)
434 break;
435 }
436 KASSERT(i < sc->sc_nrepid);
437 sc->sc_subdevs[i] = NULL;
438 }
439
440 int
441 uhidev_detach(device_t self, int flags)
442 {
443 struct uhidev_softc *sc = device_private(self);
444 int i, rv;
445 struct uhidev *csc;
446
447 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
448
449 sc->sc_dying = 1;
450 if (sc->sc_ipipe != NULL)
451 usbd_abort_pipe(sc->sc_ipipe);
452
453 if (sc->sc_repdesc != NULL)
454 kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
455
456 rv = 0;
457 for (i = 0; i < sc->sc_nrepid; i++) {
458 if (sc->sc_subdevs[i] != NULL) {
459 csc = device_private(sc->sc_subdevs[i]);
460 rnd_detach_source(&csc->rnd_source);
461 rv |= config_detach(sc->sc_subdevs[i], flags);
462 }
463 }
464
465 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 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