xref: /openbsd-src/sys/dev/usb/uhidev.c (revision 224959d1bf038b24dee22081d4f1d2fd47b1e2e7)
1 /*	$OpenBSD: uhidev.c,v 1.31 2007/11/12 04:18:09 deraadt 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) printf x; } while (0)
70 #define DPRINTFN(n,x)	do { if (uhidevdebug>(n)) printf x; } while (0)
71 int	uhidevdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76 
77 void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
78 
79 int uhidev_maxrepid(void *buf, int len);
80 int uhidevprint(void *aux, const char *pnp);
81 int uhidevsubmatch(struct device *parent, void *cf, void *aux);
82 
83 int uhidev_match(struct device *, void *, void *);
84 void uhidev_attach(struct device *, struct device *, void *);
85 int uhidev_detach(struct device *, int);
86 int uhidev_activate(struct device *, enum devact);
87 
88 struct cfdriver uhidev_cd = {
89 	NULL, "uhidev", DV_DULL
90 };
91 
92 const struct cfattach uhidev_ca = {
93 	sizeof(struct uhidev_softc),
94 	uhidev_match,
95 	uhidev_attach,
96 	uhidev_detach,
97 	uhidev_activate,
98 };
99 
100 int
101 uhidev_match(struct device *parent, void *match, void *aux)
102 {
103 	struct usb_attach_arg *uaa = aux;
104 	usb_interface_descriptor_t *id;
105 
106 	if (uaa->iface == NULL)
107 		return (UMATCH_NONE);
108 	id = usbd_get_interface_descriptor(uaa->iface);
109 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
110 		return (UMATCH_NONE);
111 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID)
112 		return (UMATCH_NONE);
113 	if (uaa->matchlvl)
114 		return (uaa->matchlvl);
115 
116 #ifdef __macppc__
117 	/*
118 	 * Some Apple laptops have USB phantom devices which match
119 	 * the ADB devices.  We want to ignore them to avoid
120 	 * confusing users, as the real hardware underneath is adb
121 	 * and has already attached.
122 	 */
123 	if (uaa->vendor == USB_VENDOR_APPLE &&
124 	    uaa->product == USB_PRODUCT_APPLE_ADB)
125 		return (UMATCH_NONE);
126 #endif
127 
128 	return (UMATCH_IFACECLASS_GENERIC);
129 }
130 
131 void
132 uhidev_attach(struct device *parent, struct device *self, void *aux)
133 {
134 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
135 	struct usb_attach_arg *uaa = aux;
136 	usbd_interface_handle iface = uaa->iface;
137 	usb_interface_descriptor_t *id;
138 	usb_endpoint_descriptor_t *ed;
139 	struct uhidev_attach_arg uha;
140 	struct uhidev *dev;
141 	int size, nrepid, repid, repsz;
142 	int repsizes[256];
143 	int i;
144 	void *desc;
145 	const void *descptr;
146 	usbd_status err;
147 
148 	sc->sc_udev = uaa->device;
149 	sc->sc_iface = iface;
150 	id = usbd_get_interface_descriptor(iface);
151 
152 	(void)usbd_set_idle(iface, 0, 0);
153 #if 0
154 
155 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
156 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
157 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
158 		(void)usbd_set_protocol(iface, 1);
159 #endif
160 
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 			printf("%s: could not read endpoint descriptor\n",
166 			    sc->sc_dev.dv_xname);
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 			sc->sc_iep_addr = ed->bEndpointAddress;
183 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
184 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
185 			sc->sc_oep_addr = ed->bEndpointAddress;
186 		} else {
187 			printf("%s: unexpected endpoint\n", sc->sc_dev.dv_xname);
188 			sc->sc_dying = 1;
189 			return;
190 		}
191 	}
192 
193 	/*
194 	 * Check that we found an input interrupt endpoint. The output interrupt
195 	 * endpoint is optional
196 	 */
197 	if (sc->sc_iep_addr == -1) {
198 		printf("%s: no input interrupt endpoint\n", sc->sc_dev.dv_xname);
199 		sc->sc_dying = 1;
200 		return;
201 	}
202 
203 	/* XXX need to extend this */
204 	descptr = NULL;
205 	if (uaa->vendor == USB_VENDOR_WACOM) {
206 		static uByte reportbuf[] = {2, 2, 2};
207 
208 		/* The report descriptor for the Wacom Graphire is broken. */
209 		switch (uaa->product) {
210 		case USB_PRODUCT_WACOM_GRAPHIRE:
211 			size = sizeof uhid_graphire_report_descr;
212 			descptr = uhid_graphire_report_descr;
213 			break;
214 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
215 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5:
216 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
217 			    &reportbuf, sizeof reportbuf);
218 			size = sizeof uhid_graphire3_4x5_report_descr;
219 			descptr = uhid_graphire3_4x5_report_descr;
220 			break;
221 		default:
222 			/* Keep descriptor */
223 			break;
224 		}
225 	}
226 
227 	if (descptr) {
228 		desc = malloc(size, M_USBDEV, M_NOWAIT);
229 		if (desc == NULL)
230 			err = USBD_NOMEM;
231 		else {
232 			err = USBD_NORMAL_COMPLETION;
233 			memcpy(desc, descptr, size);
234 		}
235 	} else {
236 		desc = NULL;
237 		err = usbd_read_report_desc(uaa->iface, &desc, &size, M_USBDEV);
238 	}
239 	if (err) {
240 		printf("%s: no report descriptor\n", sc->sc_dev.dv_xname);
241 		sc->sc_dying = 1;
242 		return;
243 	}
244 
245 	sc->sc_repdesc = desc;
246 	sc->sc_repdesc_size = size;
247 
248 	uha.uaa = uaa;
249 	nrepid = uhidev_maxrepid(desc, size);
250 	if (nrepid < 0)
251 		return;
252 	printf("%s: iclass %d/%d", sc->sc_dev.dv_xname,
253 	    id->bInterfaceClass, id->bInterfaceSubClass);
254 	if (nrepid > 0)
255 		printf(", %d report id%s", nrepid,
256 		    nrepid > 1 ? "s" : "");
257 	printf("\n");
258 	nrepid++;
259 	sc->sc_subdevs = malloc(nrepid * sizeof(struct device *),
260 	    M_USBDEV, M_NOWAIT | M_ZERO);
261 	if (sc->sc_subdevs == NULL) {
262 		printf("%s: no memory\n", sc->sc_dev.dv_xname);
263 		return;
264 	}
265 	sc->sc_nrepid = nrepid;
266 	sc->sc_isize = 0;
267 
268 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
269 			   &sc->sc_dev);
270 
271 	for (repid = 0; repid < nrepid; repid++) {
272 		repsz = hid_report_size(desc, size, hid_input, repid);
273 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
274 		repsizes[repid] = repsz;
275 		if (repsz > 0) {
276 			if (repsz > sc->sc_isize)
277 				sc->sc_isize = repsz;
278 		}
279 	}
280 	sc->sc_isize += nrepid != 1;	/* space for report ID */
281 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
282 
283 	uha.parent = sc;
284 	for (repid = 0; repid < nrepid; repid++) {
285 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
286 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
287 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
288 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
289 			;	/* already NULL in sc->sc_subdevs[repid] */
290 		} else {
291 			uha.reportid = repid;
292 			dev = (struct uhidev *)config_found_sm(self, &uha,
293 			                           uhidevprint, uhidevsubmatch);
294 			sc->sc_subdevs[repid] = dev;
295 			if (dev != NULL) {
296 				dev->sc_in_rep_size = repsizes[repid];
297 #ifdef DIAGNOSTIC
298 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
299 					 repid, dev));
300 				if (dev->sc_intr == NULL) {
301 					printf("%s: sc_intr == NULL\n",
302 					       sc->sc_dev.dv_xname);
303 					return;
304 				}
305 #endif
306 			}
307 		}
308 	}
309 }
310 
311 int
312 uhidev_maxrepid(void *buf, int len)
313 {
314 	struct hid_data *d;
315 	struct hid_item h;
316 	int maxid;
317 
318 	maxid = -1;
319 	h.report_ID = 0;
320 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
321 		if (h.report_ID > maxid)
322 			maxid = h.report_ID;
323 	hid_end_parse(d);
324 	return (maxid);
325 }
326 
327 int
328 uhidevprint(void *aux, const char *pnp)
329 {
330 	struct uhidev_attach_arg *uha = aux;
331 
332 	if (pnp)
333 		printf("uhid at %s", pnp);
334 	if (uha->reportid != 0)
335 		printf(" reportid %d", uha->reportid);
336 	return (UNCONF);
337 }
338 
339 int uhidevsubmatch(struct device *parent, void *match, void *aux)
340 {
341 	struct uhidev_attach_arg *uha = aux;
342         struct cfdata *cf = match;
343 
344 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
345 	    cf->uhidevcf_reportid != uha->reportid)
346 		return (0);
347 	if (cf->uhidevcf_reportid == uha->reportid)
348 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
349 	else
350 		uha->matchlvl = 0;
351 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
352 }
353 
354 int
355 uhidev_activate(struct device *self, enum devact act)
356 {
357 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
358 	int i, rv = 0;
359 
360 	switch (act) {
361 	case DVACT_ACTIVATE:
362 		break;
363 
364 	case DVACT_DEACTIVATE:
365 		for (i = 0; i < sc->sc_nrepid; i++)
366 			if (sc->sc_subdevs[i] != NULL)
367 				rv |= config_deactivate(
368 					&sc->sc_subdevs[i]->sc_dev);
369 		sc->sc_dying = 1;
370 		break;
371 	}
372 	return (rv);
373 }
374 
375 int
376 uhidev_detach(struct device *self, int flags)
377 {
378 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
379 	int i, rv;
380 
381 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
382 
383 	sc->sc_dying = 1;
384 	if (sc->sc_ipipe != NULL)
385 		usbd_abort_pipe(sc->sc_ipipe);
386 
387 	if (sc->sc_repdesc != NULL)
388 		free(sc->sc_repdesc, M_USBDEV);
389 
390 	rv = 0;
391 	for (i = 0; i < sc->sc_nrepid; i++) {
392 		if (sc->sc_subdevs[i] != NULL) {
393 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
394 			sc->sc_subdevs[i] = NULL;
395 		}
396 	}
397 
398 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
399 			   &sc->sc_dev);
400 
401 	return (rv);
402 }
403 
404 void
405 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
406 {
407 	struct uhidev_softc *sc = addr;
408 	struct uhidev *scd;
409 	u_char *p;
410 	u_int rep;
411 	u_int32_t cc;
412 
413 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
414 
415 #ifdef UHIDEV_DEBUG
416 	if (uhidevdebug > 5) {
417 		u_int32_t i;
418 
419 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
420 		DPRINTF(("uhidev_intr: data ="));
421 		for (i = 0; i < cc; i++)
422 			DPRINTF((" %02x", sc->sc_ibuf[i]));
423 		DPRINTF(("\n"));
424 	}
425 #endif
426 
427 	if (status == USBD_CANCELLED)
428 		return;
429 
430 	if (status != USBD_NORMAL_COMPLETION) {
431 		DPRINTF(("%s: interrupt status=%d\n", sc->sc_dev.dv_xname,
432 			 status));
433 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
434 		return;
435 	}
436 
437 	p = sc->sc_ibuf;
438 	if (sc->sc_nrepid != 1)
439 		rep = *p++, cc--;
440 	else
441 		rep = 0;
442 	if (rep >= sc->sc_nrepid) {
443 		printf("uhidev_intr: bad repid %d\n", rep);
444 		return;
445 	}
446 	scd = sc->sc_subdevs[rep];
447 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
448 		    rep, scd, scd ? scd->sc_state : 0));
449 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
450 		return;
451 #ifdef UHIDEV_DEBUG
452 	if (scd->sc_in_rep_size != cc)
453 		printf("%s: bad input length %d != %d\n",sc->sc_dev.dv_xname,
454 		       scd->sc_in_rep_size, cc);
455 #endif
456 	scd->sc_intr(scd, p, cc);
457 }
458 
459 void
460 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
461 {
462 	*desc = sc->sc_repdesc;
463 	*size = sc->sc_repdesc_size;
464 }
465 
466 int
467 uhidev_open(struct uhidev *scd)
468 {
469 	struct uhidev_softc *sc = scd->sc_parent;
470 	usbd_status err;
471 	int error;
472 
473 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
474 		 scd->sc_state, sc->sc_refcnt));
475 
476 	if (scd->sc_state & UHIDEV_OPEN)
477 		return (EBUSY);
478 	scd->sc_state |= UHIDEV_OPEN;
479 	if (sc->sc_refcnt++)
480 		return (0);
481 
482 	if (sc->sc_isize == 0)
483 		return (0);
484 
485 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
486 
487 	/* Set up input interrupt pipe. */
488 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
489 	    sc->sc_iep_addr));
490 
491 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
492 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
493 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
494 	if (err != USBD_NORMAL_COMPLETION) {
495 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
496 		    "error=%d\n", err));
497 		error = EIO;
498 		goto out1;
499 	}
500 
501 	DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe));
502 
503 	sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
504 	if (sc->sc_ixfer == NULL) {
505 		DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
506 		error = ENOMEM;
507 		goto out1; // xxxx
508 	}
509 
510 	/*
511 	 * Set up output interrupt pipe if an output interrupt endpoint
512 	 * exists.
513 	 */
514 	if (sc->sc_oep_addr != -1) {
515 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
516 
517 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
518 		    0, &sc->sc_opipe);
519 
520 		if (err != USBD_NORMAL_COMPLETION) {
521 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
522 			    "error=%d\n", err));
523 			error = EIO;
524 			goto out2;
525 		}
526 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
527 
528 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
529 		if (sc->sc_oxfer == NULL) {
530 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
531 			error = ENOMEM;
532 			goto out3;
533 		}
534 
535 		sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev);
536 		if (sc->sc_owxfer == NULL) {
537 			DPRINTF(("uhidev_open: couldn't allocate owxfer\n"));
538 			error = ENOMEM;
539 			goto out3;
540 		}
541 	}
542 
543 	return (0);
544 
545 out3:
546 	/* Abort output pipe */
547 	usbd_close_pipe(sc->sc_opipe);
548 out2:
549 	/* Abort input pipe */
550 	usbd_close_pipe(sc->sc_ipipe);
551 out1:
552 	DPRINTF(("uhidev_open: failed in someway"));
553 	free(sc->sc_ibuf, M_USBDEV);
554 	scd->sc_state &= ~UHIDEV_OPEN;
555 	sc->sc_refcnt = 0;
556 	sc->sc_ipipe = NULL;
557 	sc->sc_opipe = NULL;
558 	if (sc->sc_oxfer != NULL) {
559 		usbd_free_xfer(sc->sc_oxfer);
560 		sc->sc_oxfer = NULL;
561 	}
562 	if (sc->sc_owxfer != NULL) {
563 		usbd_free_xfer(sc->sc_owxfer);
564 		sc->sc_owxfer = NULL;
565 	}
566 	return (error);
567 }
568 
569 void
570 uhidev_close(struct uhidev *scd)
571 {
572 	struct uhidev_softc *sc = scd->sc_parent;
573 
574 	if (!(scd->sc_state & UHIDEV_OPEN))
575 		return;
576 	scd->sc_state &= ~UHIDEV_OPEN;
577 	if (--sc->sc_refcnt)
578 		return;
579 	DPRINTF(("uhidev_close: close pipe\n"));
580 
581 	if (sc->sc_oxfer != NULL)
582 		usbd_free_xfer(sc->sc_oxfer);
583 
584 	if (sc->sc_owxfer != NULL)
585 		usbd_free_xfer(sc->sc_owxfer);
586 
587 	/* Disable interrupts. */
588 	if (sc->sc_opipe != NULL) {
589 		usbd_abort_pipe(sc->sc_opipe);
590 		usbd_close_pipe(sc->sc_opipe);
591 		sc->sc_opipe = NULL;
592 	}
593 
594 	if (sc->sc_ipipe != NULL) {
595 		usbd_abort_pipe(sc->sc_ipipe);
596 		usbd_close_pipe(sc->sc_ipipe);
597 		sc->sc_ipipe = NULL;
598 	}
599 
600 	if (sc->sc_ibuf != NULL) {
601 		free(sc->sc_ibuf, M_USBDEV);
602 		sc->sc_ibuf = NULL;
603 	}
604 }
605 
606 usbd_status
607 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
608 {
609 	char *buf;
610 	usbd_status retstat;
611 
612 	if (scd->sc_report_id == 0)
613 		return usbd_set_report(scd->sc_parent->sc_iface, type,
614 				       scd->sc_report_id, data, len);
615 
616 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
617 	buf[0] = scd->sc_report_id;
618 	memcpy(buf+1, data, len);
619 
620 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
621 				  scd->sc_report_id, data, len + 1);
622 
623 	free(buf, M_TEMP);
624 
625 	return retstat;
626 }
627 
628 void
629 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
630 {
631 	/* XXX */
632 	char buf[100];
633 	if (scd->sc_report_id) {
634 		buf[0] = scd->sc_report_id;
635 		memcpy(buf+1, data, len);
636 		len++;
637 		data = buf;
638 	}
639 
640 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
641 			      scd->sc_report_id, data, len);
642 }
643 
644 usbd_status
645 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
646 {
647 	return usbd_get_report(scd->sc_parent->sc_iface, type,
648 			       scd->sc_report_id, data, len);
649 }
650 
651 usbd_status
652 uhidev_write(struct uhidev_softc *sc, void *data, int len)
653 {
654 
655 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
656 
657 	if (sc->sc_opipe == NULL)
658 		return USBD_INVAL;
659 
660 #ifdef UHIDEV_DEBUG
661 	if (uhidevdebug > 50) {
662 
663 		u_int32_t i;
664 		u_int8_t *d = data;
665 
666 		DPRINTF(("uhidev_write: data ="));
667 		for (i = 0; i < len; i++)
668 			DPRINTF((" %02x", d[i]));
669 		DPRINTF(("\n"));
670 	}
671 #endif
672 	return usbd_intr_transfer(sc->sc_owxfer, sc->sc_opipe, 0,
673 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
674 }
675