xref: /netbsd-src/sys/dev/usb/uhidev.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: uhidev.c,v 1.42 2008/05/26 19:01:51 drochner 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.42 2008/05/26 19:01:51 drochner 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 	id = usbd_get_interface_descriptor(iface);
121 
122 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
123 	USB_ATTACH_SETUP;
124 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
125 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
126 	usbd_devinfo_free(devinfop);
127 
128 	if (!pmf_device_register(self, NULL, NULL))
129 		aprint_error_dev(self, "couldn't establish power handler\n");
130 
131 	(void)usbd_set_idle(iface, 0, 0);
132 #if 0
133 
134 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
135 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
136 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
137 		(void)usbd_set_protocol(iface, 1);
138 #endif
139 
140 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
141 	for (i = 0; i < id->bNumEndpoints; i++) {
142 		ed = usbd_interface2endpoint_descriptor(iface, i);
143 		if (ed == NULL) {
144 			aprint_error_dev(self,
145 			    "could not read endpoint descriptor\n");
146 			sc->sc_dying = 1;
147 			USB_ATTACH_ERROR_RETURN;
148 		}
149 
150 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
151 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
152 		    " bInterval=%d\n",
153 		    ed->bLength, ed->bDescriptorType,
154 		    ed->bEndpointAddress & UE_ADDR,
155 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
156 		    ed->bmAttributes & UE_XFERTYPE,
157 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
158 
159 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
160 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
161 			sc->sc_iep_addr = ed->bEndpointAddress;
162 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
163 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
164 			sc->sc_oep_addr = ed->bEndpointAddress;
165 		} else {
166 			aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
167 		}
168 	}
169 
170 	/*
171 	 * Check that we found an input interrupt endpoint. The output interrupt
172 	 * endpoint is optional
173 	 */
174 	if (sc->sc_iep_addr == -1) {
175 		aprint_error_dev(self, "no input interrupt endpoint\n");
176 		sc->sc_dying = 1;
177 		USB_ATTACH_ERROR_RETURN;
178 	}
179 
180 	/* XXX need to extend this */
181 	descptr = NULL;
182 	if (uaa->vendor == USB_VENDOR_WACOM) {
183 		static uByte reportbuf[] = {2, 2, 2};
184 
185 		/* The report descriptor for the Wacom Graphire is broken. */
186 		switch (uaa->product) {
187 		case USB_PRODUCT_WACOM_GRAPHIRE:
188 			size = sizeof uhid_graphire_report_descr;
189 			descptr = uhid_graphire_report_descr;
190 			break;
191 
192 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
193 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
194 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
195 			/*
196 			 * The Graphire3 needs 0x0202 to be written to
197 			 * feature report ID 2 before it'll start
198 			 * returning digitizer data.
199 			 */
200 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
201 			    &reportbuf, sizeof reportbuf);
202 
203 			size = sizeof uhid_graphire3_4x5_report_descr;
204 			descptr = uhid_graphire3_4x5_report_descr;
205 			break;
206 		default:
207 			/* Keep descriptor */
208 			break;
209 		}
210 	}
211 
212 	if (descptr) {
213 		desc = malloc(size, M_USBDEV, M_NOWAIT);
214 		if (desc == NULL)
215 			err = USBD_NOMEM;
216 		else {
217 			err = USBD_NORMAL_COMPLETION;
218 			memcpy(desc, descptr, size);
219 		}
220 	} else {
221 		desc = NULL;
222 		err = usbd_read_report_desc(uaa->iface, &desc, &size,
223 		    M_USBDEV);
224 	}
225 	if (err) {
226 		aprint_error_dev(self, "no report descriptor\n");
227 		sc->sc_dying = 1;
228 		USB_ATTACH_ERROR_RETURN;
229 	}
230 
231 	if (uaa->vendor == USB_VENDOR_HOSIDEN &&
232 	    uaa->product == USB_PRODUCT_HOSIDEN_PPP) {
233 		static uByte reportbuf[] = { 1 };
234 		/*
235 		 *  This device was sold by Konami with its ParaParaParadise
236 		 *  game for PlayStation2.  It needs to be "turned on"
237 		 *  before it will send any reports.
238 		 */
239 
240 		usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 0,
241 		    &reportbuf, sizeof reportbuf);
242 	}
243 
244 	sc->sc_repdesc = desc;
245 	sc->sc_repdesc_size = size;
246 
247 	uha.uaa = uaa;
248 	nrepid = uhidev_maxrepid(desc, size);
249 	if (nrepid < 0)
250 		USB_ATTACH_SUCCESS_RETURN;
251 	if (nrepid > 0)
252 		aprint_normal_dev(self, "%d report ids\n", nrepid);
253 	nrepid++;
254 	repsizes = malloc(nrepid * sizeof(*repsizes), M_TEMP, M_NOWAIT);
255 	if (repsizes == NULL)
256 		goto nomem;
257 	sc->sc_subdevs = malloc(nrepid * sizeof(device_t),
258 				M_USBDEV, M_NOWAIT | M_ZERO);
259 	if (sc->sc_subdevs == NULL) {
260 		free(repsizes, M_TEMP);
261 nomem:
262 		aprint_error_dev(self, "no memory\n");
263 		USB_ATTACH_ERROR_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 			   USBDEV(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 			locs[UHIDBUSCF_REPORTID] = repid;
293 
294 			dev = config_found_sm_loc(self,
295 				"uhidbus", locs, &uha,
296 				uhidevprint, config_stdsubmatch);
297 			sc->sc_subdevs[repid] = dev;
298 			if (dev != NULL) {
299 				csc = device_private(dev);
300 				csc->sc_in_rep_size = repsizes[repid];
301 #ifdef DIAGNOSTIC
302 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
303 					 repid, dev));
304 				if (csc->sc_intr == NULL) {
305 					free(repsizes, M_TEMP);
306 					aprint_error_dev(self,
307 					    "sc_intr == NULL\n");
308 					USB_ATTACH_ERROR_RETURN;
309 				}
310 #endif
311 #if NRND > 0
312 				rnd_attach_source(&csc->rnd_source,
313 						  USBDEVNAME(dev),
314 						  RND_TYPE_TTY, 0);
315 #endif
316 			}
317 		}
318 	}
319 	free(repsizes, M_TEMP);
320 
321 	USB_ATTACH_SUCCESS_RETURN;
322 }
323 
324 int
325 uhidev_maxrepid(void *buf, int len)
326 {
327 	struct hid_data *d;
328 	struct hid_item h;
329 	int maxid;
330 
331 	maxid = -1;
332 	h.report_ID = 0;
333 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
334 		if (h.report_ID > maxid)
335 			maxid = h.report_ID;
336 	hid_end_parse(d);
337 	return (maxid);
338 }
339 
340 int
341 uhidevprint(void *aux, const char *pnp)
342 {
343 	struct uhidev_attach_arg *uha = aux;
344 
345 	if (pnp)
346 		aprint_normal("uhid at %s", pnp);
347 	if (uha->reportid != 0)
348 		aprint_normal(" reportid %d", uha->reportid);
349 	return (UNCONF);
350 }
351 
352 int
353 uhidev_activate(device_t self, enum devact act)
354 {
355 	struct uhidev_softc *sc = device_private(self);
356 	int i, rv;
357 
358 	switch (act) {
359 	case DVACT_ACTIVATE:
360 		return (EOPNOTSUPP);
361 
362 	case DVACT_DEACTIVATE:
363 		rv = 0;
364 		for (i = 0; i < sc->sc_nrepid; i++)
365 			if (sc->sc_subdevs[i] != NULL)
366 				rv |= config_deactivate(
367 					sc->sc_subdevs[i]);
368 		sc->sc_dying = 1;
369 		break;
370 	default:
371 		rv = 0;
372 		break;
373 	}
374 	return (rv);
375 }
376 
377 void
378 uhidev_childdet(device_t self, device_t child)
379 {
380 	int i;
381 	struct uhidev_softc *sc = device_private(self);
382 
383 	for (i = 0; i < sc->sc_nrepid; i++) {
384 		if (sc->sc_subdevs[i] == child)
385 			break;
386 	}
387 	KASSERT(i < sc->sc_nrepid);
388 	sc->sc_subdevs[i] = NULL;
389 }
390 
391 USB_DETACH(uhidev)
392 {
393 	USB_DETACH_START(uhidev, sc);
394 	int i, rv;
395 #if NRND > 0
396 	struct uhidev *csc;
397 #endif
398 
399 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
400 
401 	sc->sc_dying = 1;
402 	if (sc->sc_ipipe != NULL)
403 		usbd_abort_pipe(sc->sc_ipipe);
404 
405 	if (sc->sc_repdesc != NULL)
406 		free(sc->sc_repdesc, M_USBDEV);
407 
408 	rv = 0;
409 	for (i = 0; i < sc->sc_nrepid; i++) {
410 		if (sc->sc_subdevs[i] != NULL) {
411 #if NRND > 0
412 			csc = device_private(sc->sc_subdevs[i]);
413 			rnd_detach_source(&csc->rnd_source);
414 #endif
415 			rv |= config_detach(sc->sc_subdevs[i], flags);
416 		}
417 	}
418 
419 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
420 			   USBDEV(sc->sc_dev));
421 
422 	pmf_device_deregister(self);
423 
424 	return (rv);
425 }
426 
427 void
428 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
429 {
430 	struct uhidev_softc *sc = addr;
431 	device_t cdev;
432 	struct uhidev *scd;
433 	u_char *p;
434 	u_int rep;
435 	u_int32_t cc;
436 
437 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
438 
439 #ifdef UHIDEV_DEBUG
440 	if (uhidevdebug > 5) {
441 		u_int32_t i;
442 
443 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
444 		DPRINTF(("uhidev_intr: data ="));
445 		for (i = 0; i < cc; i++)
446 			DPRINTF((" %02x", sc->sc_ibuf[i]));
447 		DPRINTF(("\n"));
448 	}
449 #endif
450 
451 	if (status == USBD_CANCELLED)
452 		return;
453 
454 	if (status != USBD_NORMAL_COMPLETION) {
455 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
456 			 status));
457 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
458 		return;
459 	}
460 
461 	p = sc->sc_ibuf;
462 	if (sc->sc_nrepid != 1)
463 		rep = *p++, cc--;
464 	else
465 		rep = 0;
466 	if (rep >= sc->sc_nrepid) {
467 		printf("uhidev_intr: bad repid %d\n", rep);
468 		return;
469 	}
470 	cdev = sc->sc_subdevs[rep];
471 	if (!cdev)
472 		return;
473 	scd = device_private(cdev);
474 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
475 		    rep, scd, scd ? scd->sc_state : 0));
476 	if (!(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