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