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