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