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