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