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