xref: /openbsd-src/sys/dev/usb/uhidev.c (revision f35c38e7e06be9f6d198734d5925b22fde54d006)
1 /*	$OpenBSD: uhidev.c,v 1.17 2006/08/18 02:54:11 jason Exp $	*/
2 /*	$NetBSD: uhidev.c,v 1.14 2003/03/11 16:44:00 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/conf.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/hid.h>
61 #include <dev/usb/usb_quirks.h>
62 
63 #include <dev/usb/uhidev.h>
64 
65 /* Report descriptor for broken Wacom Graphire */
66 #include <dev/usb/ugraphire_rdesc.h>
67 
68 #ifdef UHIDEV_DEBUG
69 #define DPRINTF(x)	do { if (uhidevdebug) logprintf x; } while (0)
70 #define DPRINTFN(n,x)	do { if (uhidevdebug>(n)) logprintf x; } while (0)
71 int	uhidevdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76 
77 Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
78 
79 Static int uhidev_maxrepid(void *buf, int len);
80 Static int uhidevprint(void *aux, const char *pnp);
81 #if defined(__NetBSD__)
82 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux);
83 #else
84 Static int uhidevsubmatch(struct device *parent, void *cf, void *aux);
85 #endif
86 
87 USB_DECLARE_DRIVER(uhidev);
88 
89 USB_MATCH(uhidev)
90 {
91 	USB_MATCH_START(uhidev, uaa);
92 	usb_interface_descriptor_t *id;
93 
94 	if (uaa->iface == NULL)
95 		return (UMATCH_NONE);
96 	id = usbd_get_interface_descriptor(uaa->iface);
97 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
98 		return (UMATCH_NONE);
99 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID)
100 		return (UMATCH_NONE);
101 	if (uaa->matchlvl)
102 		return (uaa->matchlvl);
103 
104 #ifdef __macppc__
105 	/*
106 	 * Some Apple laptops have USB phantom devices which match
107 	 * the ADB devices.  We want to ignore them to avoid
108 	 * confusing users, as the real hardware underneath is adb
109 	 * and has already attached.
110 	 */
111 	if (uaa->vendor == USB_VENDOR_APPLE &&
112 	    uaa->product == USB_PRODUCT_APPLE_ADB)
113 		return (UMATCH_NONE);
114 #endif
115 
116 	return (UMATCH_IFACECLASS_GENERIC);
117 }
118 
119 USB_ATTACH(uhidev)
120 {
121 	USB_ATTACH_START(uhidev, sc, uaa);
122 	usbd_interface_handle iface = uaa->iface;
123 	usb_interface_descriptor_t *id;
124 	usb_endpoint_descriptor_t *ed;
125 	struct uhidev_attach_arg uha;
126 	struct uhidev *dev;
127 	int size, nrepid, repid, repsz;
128 	int repsizes[256];
129 	int i;
130 	void *desc;
131 	const void *descptr;
132 	usbd_status err;
133 	char *devinfop;
134 
135 	sc->sc_udev = uaa->device;
136 	sc->sc_iface = iface;
137 	id = usbd_get_interface_descriptor(iface);
138 
139 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
140 	USB_ATTACH_SETUP;
141 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
142 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
143 	usbd_devinfo_free(devinfop);
144 
145 	(void)usbd_set_idle(iface, 0, 0);
146 #if 0
147 
148 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
149 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
150 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
151 		(void)usbd_set_protocol(iface, 1);
152 #endif
153 
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) {
158 			printf("%s: could not read endpoint descriptor\n",
159 			    USBDEVNAME(sc->sc_dev));
160 			sc->sc_dying = 1;
161 			USB_ATTACH_ERROR_RETURN;
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) {
175 			sc->sc_iep_addr = ed->bEndpointAddress;
176 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
177 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
178 			sc->sc_oep_addr = ed->bEndpointAddress;
179 		} else {
180 			printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
181 			sc->sc_dying = 1;
182 			USB_ATTACH_ERROR_RETURN;
183 		}
184 	}
185 
186 	/*
187 	 * Check that we found an input interrupt endpoint. The output interrupt
188 	 * endpoint is optional
189 	 */
190 	if (sc->sc_iep_addr == -1) {
191 		printf("%s: no input interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
192 		sc->sc_dying = 1;
193 		USB_ATTACH_ERROR_RETURN;
194 	}
195 
196 	/* XXX need to extend this */
197 	descptr = NULL;
198 	if (uaa->vendor == USB_VENDOR_WACOM) {
199 		static uByte reportbuf[] = {2, 2, 2};
200 
201 		/* The report descriptor for the Wacom Graphire is broken. */
202 		switch (uaa->product) {
203 		case USB_PRODUCT_WACOM_GRAPHIRE:
204 			size = sizeof uhid_graphire_report_descr;
205 			descptr = uhid_graphire_report_descr;
206 			break;
207 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
208 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5:
209 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
210 			    &reportbuf, sizeof reportbuf);
211 			size = sizeof uhid_graphire3_4x5_report_descr;
212 			descptr = uhid_graphire3_4x5_report_descr;
213 			break;
214 		default:
215 			/* Keep descriptor */
216 			break;
217 		}
218 	}
219 
220 	if (descptr) {
221 		desc = malloc(size, M_USBDEV, M_NOWAIT);
222 		if (desc == NULL)
223 			err = USBD_NOMEM;
224 		else {
225 			err = USBD_NORMAL_COMPLETION;
226 			memcpy(desc, descptr, size);
227 		}
228 	} else {
229 		desc = NULL;
230 		err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV);
231 	}
232 	if (err) {
233 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
234 		sc->sc_dying = 1;
235 		USB_ATTACH_ERROR_RETURN;
236 	}
237 
238 	sc->sc_repdesc = desc;
239 	sc->sc_repdesc_size = size;
240 
241 	uha.uaa = uaa;
242 	nrepid = uhidev_maxrepid(desc, size);
243 	if (nrepid < 0)
244 		USB_ATTACH_SUCCESS_RETURN;
245 	if (nrepid > 0)
246 		printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
247 	nrepid++;
248 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
249 				M_USBDEV, M_NOWAIT);
250 	bzero(sc->sc_subdevs, nrepid * sizeof(device_ptr_t));
251 	if (sc->sc_subdevs == NULL) {
252 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
253 		USB_ATTACH_ERROR_RETURN;
254 	}
255 	sc->sc_nrepid = nrepid;
256 	sc->sc_isize = 0;
257 
258 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
259 			   USBDEV(sc->sc_dev));
260 
261 	for (repid = 0; repid < nrepid; repid++) {
262 		repsz = hid_report_size(desc, size, hid_input, repid);
263 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
264 		repsizes[repid] = repsz;
265 		if (repsz > 0) {
266 			if (repsz > sc->sc_isize)
267 				sc->sc_isize = repsz;
268 		}
269 	}
270 	sc->sc_isize += nrepid != 1;	/* space for report ID */
271 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
272 
273 	uha.parent = sc;
274 	for (repid = 0; repid < nrepid; repid++) {
275 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
276 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
277 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
278 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
279 			;	/* already NULL in sc->sc_subdevs[repid] */
280 		} else {
281 			uha.reportid = repid;
282 			dev = (struct uhidev *)config_found_sm(self, &uha,
283 			                           uhidevprint, uhidevsubmatch);
284 			sc->sc_subdevs[repid] = dev;
285 			if (dev != NULL) {
286 				dev->sc_in_rep_size = repsizes[repid];
287 #ifdef DIAGNOSTIC
288 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
289 					 repid, dev));
290 				if (dev->sc_intr == NULL) {
291 					printf("%s: sc_intr == NULL\n",
292 					       USBDEVNAME(sc->sc_dev));
293 					USB_ATTACH_ERROR_RETURN;
294 				}
295 #endif
296 			}
297 		}
298 	}
299 
300 	USB_ATTACH_SUCCESS_RETURN;
301 }
302 
303 int
304 uhidev_maxrepid(void *buf, int len)
305 {
306 	struct hid_data *d;
307 	struct hid_item h;
308 	int maxid;
309 
310 	maxid = -1;
311 	h.report_ID = 0;
312 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
313 		if (h.report_ID > maxid)
314 			maxid = h.report_ID;
315 	hid_end_parse(d);
316 	return (maxid);
317 }
318 
319 int
320 uhidevprint(void *aux, const char *pnp)
321 {
322 	struct uhidev_attach_arg *uha = aux;
323 
324 	if (pnp)
325 		printf("uhid at %s", pnp);
326 	if (uha->reportid != 0)
327 		printf(" reportid %d", uha->reportid);
328 	return (UNCONF);
329 }
330 
331 #if defined(__NetBSD__)
332 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf, void *aux)
333 #else
334 Static int uhidevsubmatch(struct device *parent, void *match, void *aux)
335 #endif
336 {
337 	struct uhidev_attach_arg *uha = aux;
338 #if defined(__OpenBSD__)
339         struct cfdata *cf = match;
340 #endif
341 
342 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
343 	    cf->uhidevcf_reportid != uha->reportid)
344 		return (0);
345 	if (cf->uhidevcf_reportid == uha->reportid)
346 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
347 	else
348 		uha->matchlvl = 0;
349 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
350 }
351 
352 int
353 uhidev_activate(device_ptr_t self, enum devact act)
354 {
355 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
356 	int i, rv = 0;
357 
358 	switch (act) {
359 	case DVACT_ACTIVATE:
360 		break;
361 
362 	case DVACT_DEACTIVATE:
363 		for (i = 0; i < sc->sc_nrepid; i++)
364 			if (sc->sc_subdevs[i] != NULL)
365 				rv |= config_deactivate(
366 					&sc->sc_subdevs[i]->sc_dev);
367 		sc->sc_dying = 1;
368 		break;
369 	}
370 	return (rv);
371 }
372 
373 USB_DETACH(uhidev)
374 {
375 	USB_DETACH_START(uhidev, sc);
376 	int i, rv;
377 
378 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
379 
380 	sc->sc_dying = 1;
381 	if (sc->sc_ipipe != NULL)
382 		usbd_abort_pipe(sc->sc_ipipe);
383 
384 	if (sc->sc_repdesc != NULL)
385 		free(sc->sc_repdesc, M_USBDEV);
386 
387 	rv = 0;
388 	for (i = 0; i < sc->sc_nrepid; i++) {
389 		if (sc->sc_subdevs[i] != NULL) {
390 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
391 			sc->sc_subdevs[i] = NULL;
392 		}
393 	}
394 
395 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
396 			   USBDEV(sc->sc_dev));
397 
398 	return (rv);
399 }
400 
401 void
402 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
403 {
404 	struct uhidev_softc *sc = addr;
405 	struct uhidev *scd;
406 	u_char *p;
407 	u_int rep;
408 	u_int32_t cc;
409 
410 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
411 
412 #ifdef UHIDEV_DEBUG
413 	if (uhidevdebug > 5) {
414 		u_int32_t i;
415 
416 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
417 		DPRINTF(("uhidev_intr: data ="));
418 		for (i = 0; i < cc; i++)
419 			DPRINTF((" %02x", sc->sc_ibuf[i]));
420 		DPRINTF(("\n"));
421 	}
422 #endif
423 
424 	if (status == USBD_CANCELLED)
425 		return;
426 
427 	if (status != USBD_NORMAL_COMPLETION) {
428 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
429 			 status));
430 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
431 		return;
432 	}
433 
434 	p = sc->sc_ibuf;
435 	if (sc->sc_nrepid != 1)
436 		rep = *p++, cc--;
437 	else
438 		rep = 0;
439 	if (rep >= sc->sc_nrepid) {
440 		printf("uhidev_intr: bad repid %d\n", rep);
441 		return;
442 	}
443 	scd = sc->sc_subdevs[rep];
444 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
445 		    rep, scd, scd ? scd->sc_state : 0));
446 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
447 		return;
448 #ifdef DIAGNOSTIC
449 	if (scd->sc_in_rep_size != cc)
450 		printf("%s: bad input length %d != %d\n",USBDEVNAME(sc->sc_dev),
451 		       scd->sc_in_rep_size, cc);
452 #endif
453 	scd->sc_intr(scd, p, cc);
454 }
455 
456 void
457 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
458 {
459 	*desc = sc->sc_repdesc;
460 	*size = sc->sc_repdesc_size;
461 }
462 
463 int
464 uhidev_open(struct uhidev *scd)
465 {
466 	struct uhidev_softc *sc = scd->sc_parent;
467 	usbd_status err;
468 	int error;
469 
470 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
471 		 scd->sc_state, sc->sc_refcnt));
472 
473 	if (scd->sc_state & UHIDEV_OPEN)
474 		return (EBUSY);
475 	scd->sc_state |= UHIDEV_OPEN;
476 	if (sc->sc_refcnt++)
477 		return (0);
478 
479 	if (sc->sc_isize == 0)
480 		return (0);
481 
482 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
483 
484 	/* Set up input interrupt pipe. */
485 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
486 	    sc->sc_iep_addr));
487 
488 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
489 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
490 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
491 	if (err != USBD_NORMAL_COMPLETION) {
492 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
493 		    "error=%d\n", err));
494 		error = EIO;
495 		goto out1;
496 	}
497 
498 	DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe));
499 
500 	sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
501 	if (sc->sc_ixfer == NULL) {
502 		DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
503 		error = ENOMEM;
504 		goto out1; // xxxx
505 	}
506 
507 	/*
508 	 * Set up output interrupt pipe if an output interrupt endpoint
509 	 * exists.
510 	 */
511 	if (sc->sc_oep_addr != -1) {
512 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
513 
514 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
515 		    0, &sc->sc_opipe);
516 
517 		if (err != USBD_NORMAL_COMPLETION) {
518 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
519 			    "error=%d\n", err));
520 			error = EIO;
521 			goto out2;
522 		}
523 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
524 
525 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
526 		if (sc->sc_oxfer == NULL) {
527 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
528 			error = ENOMEM;
529 			goto out3;
530 		}
531 
532 		sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev);
533 		if (sc->sc_owxfer == NULL) {
534 			DPRINTF(("uhidev_open: couldn't allocate owxfer\n"));
535 			error = ENOMEM;
536 			goto out3;
537 		}
538 	}
539 
540 	return (0);
541 
542 out3:
543 	/* Abort output pipe */
544 	usbd_close_pipe(sc->sc_opipe);
545 out2:
546 	/* Abort input pipe */
547 	usbd_close_pipe(sc->sc_ipipe);
548 out1:
549 	DPRINTF(("uhidev_open: failed in someway"));
550 	free(sc->sc_ibuf, M_USBDEV);
551 	scd->sc_state &= ~UHIDEV_OPEN;
552 	sc->sc_refcnt = 0;
553 	sc->sc_ipipe = NULL;
554 	sc->sc_opipe = NULL;
555 	if (sc->sc_oxfer != NULL) {
556 		usbd_free_xfer(sc->sc_oxfer);
557 		sc->sc_oxfer = NULL;
558 	}
559 	if (sc->sc_owxfer != NULL) {
560 		usbd_free_xfer(sc->sc_owxfer);
561 		sc->sc_owxfer = NULL;
562 	}
563 	return (error);
564 }
565 
566 void
567 uhidev_close(struct uhidev *scd)
568 {
569 	struct uhidev_softc *sc = scd->sc_parent;
570 
571 	if (!(scd->sc_state & UHIDEV_OPEN))
572 		return;
573 	scd->sc_state &= ~UHIDEV_OPEN;
574 	if (--sc->sc_refcnt)
575 		return;
576 	DPRINTF(("uhidev_close: close pipe\n"));
577 
578 	if (sc->sc_oxfer != NULL)
579 		usbd_free_xfer(sc->sc_oxfer);
580 
581 	if (sc->sc_owxfer != NULL)
582 		usbd_free_xfer(sc->sc_owxfer);
583 
584 	/* Disable interrupts. */
585 	if (sc->sc_opipe != NULL) {
586 		usbd_abort_pipe(sc->sc_opipe);
587 		usbd_close_pipe(sc->sc_opipe);
588 		sc->sc_opipe = NULL;
589 	}
590 
591 	if (sc->sc_ipipe != NULL) {
592 		usbd_abort_pipe(sc->sc_ipipe);
593 		usbd_close_pipe(sc->sc_ipipe);
594 		sc->sc_ipipe = NULL;
595 	}
596 
597 	if (sc->sc_ibuf != NULL) {
598 		free(sc->sc_ibuf, M_USBDEV);
599 		sc->sc_ibuf = NULL;
600 	}
601 }
602 
603 usbd_status
604 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
605 {
606 	char *buf;
607 	usbd_status retstat;
608 
609 	if (scd->sc_report_id == 0)
610 		return usbd_set_report(scd->sc_parent->sc_iface, type,
611 				       scd->sc_report_id, data, len);
612 
613 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
614 	buf[0] = scd->sc_report_id;
615 	memcpy(buf+1, data, len);
616 
617 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
618 				  scd->sc_report_id, data, len + 1);
619 
620 	free(buf, M_TEMP);
621 
622 	return retstat;
623 }
624 
625 void
626 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
627 {
628 	/* XXX */
629 	char buf[100];
630 	if (scd->sc_report_id) {
631 		buf[0] = scd->sc_report_id;
632 		memcpy(buf+1, data, len);
633 		len++;
634 		data = buf;
635 	}
636 
637 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
638 			      scd->sc_report_id, data, len);
639 }
640 
641 usbd_status
642 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
643 {
644 	return usbd_get_report(scd->sc_parent->sc_iface, type,
645 			       scd->sc_report_id, data, len);
646 }
647 
648 usbd_status
649 uhidev_write(struct uhidev_softc *sc, void *data, int len)
650 {
651 
652 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
653 
654 	if (sc->sc_opipe == NULL)
655 		return USBD_INVAL;
656 
657 #ifdef UHIDEV_DEBUG
658 	if (uhidevdebug > 50) {
659 
660 		u_int32_t i;
661 		u_int8_t *d = data;
662 
663 		DPRINTF(("uhidev_write: data ="));
664 		for (i = 0; i < len; i++)
665 			DPRINTF((" %02x", d[i]));
666 		DPRINTF(("\n"));
667 	}
668 #endif
669 	return usbd_intr_transfer(sc->sc_owxfer, sc->sc_opipe, 0,
670 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
671 }
672