Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.25
      1 /*	$NetBSD: uhidev.c,v 1.25 2005/05/08 06:19:10 skrll 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.25 2005/05/08 06:19:10 skrll 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 *buf, int len);
     84 Static int uhidevprint(void *aux, const char *pnp);
     85 Static int uhidevsubmatch(struct device *parent, struct cfdata *cf,
     86 			  const locdesc_t *, void *aux);
     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 	if (uaa->matchlvl)
    101 		return (uaa->matchlvl);
    102 	return (UMATCH_IFACECLASS_GENERIC);
    103 }
    104 
    105 USB_ATTACH(uhidev)
    106 {
    107 	USB_ATTACH_START(uhidev, sc, uaa);
    108 	usbd_interface_handle iface = uaa->iface;
    109 	usb_interface_descriptor_t *id;
    110 	usb_endpoint_descriptor_t *ed;
    111 	struct uhidev_attach_arg uha;
    112 	struct uhidev *dev;
    113 	int size, nrepid, repid, repsz;
    114 	int repsizes[256];
    115 	int i;
    116 	void *desc;
    117 	const void *descptr;
    118 	usbd_status err;
    119 	char devinfo[1024];
    120 	int help[2];
    121 	locdesc_t *ldesc = (void *)help; /* XXX */
    122 
    123 	sc->sc_udev = uaa->device;
    124 	sc->sc_iface = iface;
    125 	id = usbd_get_interface_descriptor(iface);
    126 	usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
    127 	USB_ATTACH_SETUP;
    128 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    129 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    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 			printf("%s: could not read endpoint descriptor\n",
    145 			    USBDEVNAME(sc->sc_dev));
    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 			printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    167 			sc->sc_dying = 1;
    168 			USB_ATTACH_ERROR_RETURN;
    169 		}
    170 	}
    171 
    172 	/*
    173 	 * Check that we found an input interrupt endpoint. The output interrupt
    174 	 * endpoint is optional
    175 	 */
    176 	if (sc->sc_iep_addr == -1) {
    177 		printf("%s: no input interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    178 		sc->sc_dying = 1;
    179 		USB_ATTACH_ERROR_RETURN;
    180 	}
    181 
    182 	/* XXX need to extend this */
    183 	descptr = NULL;
    184 	if (uaa->vendor == USB_VENDOR_WACOM) {
    185 		static uByte reportbuf[] = {2, 2, 2};
    186 
    187 		/* The report descriptor for the Wacom Graphire is broken. */
    188 		switch (uaa->product) {
    189 		case USB_PRODUCT_WACOM_GRAPHIRE:
    190 			size = sizeof uhid_graphire_report_descr;
    191 			descptr = uhid_graphire_report_descr;
    192 			break;
    193 
    194 		case USB_PRODUCT_WACOM_GRAPHIRE3_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 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    227 		sc->sc_dying = 1;
    228 		USB_ATTACH_ERROR_RETURN;
    229 	}
    230 
    231 	sc->sc_repdesc = desc;
    232 	sc->sc_repdesc_size = size;
    233 
    234 	uha.uaa = uaa;
    235 	nrepid = uhidev_maxrepid(desc, size);
    236 	if (nrepid < 0)
    237 		USB_ATTACH_SUCCESS_RETURN;
    238 	if (nrepid > 0)
    239 		printf("%s: %d report ids\n", USBDEVNAME(sc->sc_dev), nrepid);
    240 	nrepid++;
    241 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
    242 				M_USBDEV, M_NOWAIT | M_ZERO);
    243 	if (sc->sc_subdevs == NULL) {
    244 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    245 		USB_ATTACH_ERROR_RETURN;
    246 	}
    247 	sc->sc_nrepid = nrepid;
    248 	sc->sc_isize = 0;
    249 
    250 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    251 			   USBDEV(sc->sc_dev));
    252 
    253 	for (repid = 0; repid < nrepid; repid++) {
    254 		repsz = hid_report_size(desc, size, hid_input, repid);
    255 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    256 		repsizes[repid] = repsz;
    257 		if (repsz > 0) {
    258 			if (repsz > sc->sc_isize)
    259 				sc->sc_isize = repsz;
    260 		}
    261 	}
    262 	sc->sc_isize += nrepid != 1;	/* space for report ID */
    263 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    264 
    265 	uha.parent = sc;
    266 	for (repid = 0; repid < nrepid; repid++) {
    267 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    268 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    269 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    270 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    271 			;	/* already NULL in sc->sc_subdevs[repid] */
    272 		} else {
    273 			uha.reportid = repid;
    274 			ldesc->len = 1;
    275 			ldesc->locs[UHIDBUSCF_REPORTID] = repid;
    276 
    277 			dev = (struct uhidev *)config_found_sm_loc(self,
    278 				"uhidbus", ldesc, &uha,
    279 				uhidevprint, uhidevsubmatch);
    280 			sc->sc_subdevs[repid] = dev;
    281 			if (dev != NULL) {
    282 				dev->sc_in_rep_size = repsizes[repid];
    283 #ifdef DIAGNOSTIC
    284 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    285 					 repid, dev));
    286 				if (dev->sc_intr == NULL) {
    287 					printf("%s: sc_intr == NULL\n",
    288 					       USBDEVNAME(sc->sc_dev));
    289 					USB_ATTACH_ERROR_RETURN;
    290 				}
    291 #endif
    292 #if NRND > 0
    293 				rnd_attach_source(&dev->rnd_source,
    294 						  USBDEVNAME(dev->sc_dev),
    295 						  RND_TYPE_TTY, 0);
    296 #endif
    297 			}
    298 		}
    299 	}
    300 
    301 	USB_ATTACH_SUCCESS_RETURN;
    302 }
    303 
    304 int
    305 uhidev_maxrepid(void *buf, int len)
    306 {
    307 	struct hid_data *d;
    308 	struct hid_item h;
    309 	int maxid;
    310 
    311 	maxid = -1;
    312 	h.report_ID = 0;
    313 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    314 		if (h.report_ID > maxid)
    315 			maxid = h.report_ID;
    316 	hid_end_parse(d);
    317 	return (maxid);
    318 }
    319 
    320 int
    321 uhidevprint(void *aux, const char *pnp)
    322 {
    323 	struct uhidev_attach_arg *uha = aux;
    324 
    325 	if (pnp)
    326 		aprint_normal("uhid at %s", pnp);
    327 	if (uha->reportid != 0)
    328 		aprint_normal(" reportid %d", uha->reportid);
    329 	return (UNCONF);
    330 }
    331 
    332 int
    333 uhidevsubmatch(struct device *parent, struct cfdata *cf,
    334 	       const locdesc_t *ldesc, void *aux)
    335 {
    336 	struct uhidev_attach_arg *uha = aux;
    337 
    338 	if (cf->cf_loc[UHIDBUSCF_REPORTID] != UHIDBUSCF_REPORTID_DEFAULT &&
    339 	    cf->cf_loc[UHIDBUSCF_REPORTID] != ldesc->locs[UHIDBUSCF_REPORTID])
    340 		return (0);
    341 
    342 	if (cf->cf_loc[UHIDBUSCF_REPORTID] == ldesc->locs[UHIDBUSCF_REPORTID])
    343 		uha->matchlvl = UMATCH_VENDOR_PRODUCT;
    344 	else
    345 		uha->matchlvl = 0;
    346 	return (config_match(parent, cf, aux));
    347 }
    348 
    349 int
    350 uhidev_activate(device_ptr_t self, enum devact act)
    351 {
    352 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
    353 	int i, rv;
    354 
    355 	switch (act) {
    356 	case DVACT_ACTIVATE:
    357 		return (EOPNOTSUPP);
    358 
    359 	case DVACT_DEACTIVATE:
    360 		rv = 0;
    361 		for (i = 0; i < sc->sc_nrepid; i++)
    362 			if (sc->sc_subdevs[i] != NULL)
    363 				rv |= config_deactivate(
    364 					&sc->sc_subdevs[i]->sc_dev);
    365 		sc->sc_dying = 1;
    366 		break;
    367 	default:
    368 		rv = 0;
    369 		break;
    370 	}
    371 	return (rv);
    372 }
    373 
    374 USB_DETACH(uhidev)
    375 {
    376 	USB_DETACH_START(uhidev, sc);
    377 	int i, rv;
    378 
    379 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    380 
    381 	sc->sc_dying = 1;
    382 	if (sc->sc_ipipe != NULL)
    383 		usbd_abort_pipe(sc->sc_ipipe);
    384 
    385 	if (sc->sc_repdesc != NULL)
    386 		free(sc->sc_repdesc, M_USBDEV);
    387 
    388 	rv = 0;
    389 	for (i = 0; i < sc->sc_nrepid; i++) {
    390 		if (sc->sc_subdevs[i] != NULL) {
    391 #if NRND > 0
    392 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    393 #endif
    394 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    395 			sc->sc_subdevs[i] = NULL;
    396 		}
    397 	}
    398 
    399 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    400 			   USBDEV(sc->sc_dev));
    401 
    402 	return (rv);
    403 }
    404 
    405 void
    406 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    407 {
    408 	struct uhidev_softc *sc = addr;
    409 	struct uhidev *scd;
    410 	u_char *p;
    411 	u_int rep;
    412 	u_int32_t cc;
    413 
    414 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    415 
    416 #ifdef UHIDEV_DEBUG
    417 	if (uhidevdebug > 5) {
    418 		u_int32_t i;
    419 
    420 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    421 		DPRINTF(("uhidev_intr: data ="));
    422 		for (i = 0; i < cc; i++)
    423 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    424 		DPRINTF(("\n"));
    425 	}
    426 #endif
    427 
    428 	if (status == USBD_CANCELLED)
    429 		return;
    430 
    431 	if (status != USBD_NORMAL_COMPLETION) {
    432 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    433 			 status));
    434 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    435 		return;
    436 	}
    437 
    438 	p = sc->sc_ibuf;
    439 	if (sc->sc_nrepid != 1)
    440 		rep = *p++, cc--;
    441 	else
    442 		rep = 0;
    443 	if (rep >= sc->sc_nrepid) {
    444 		printf("uhidev_intr: bad repid %d\n", rep);
    445 		return;
    446 	}
    447 	scd = sc->sc_subdevs[rep];
    448 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    449 		    rep, scd, scd ? scd->sc_state : 0));
    450 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    451 		return;
    452 	if (scd->sc_in_rep_size != cc) {
    453 		printf("%s: bad input length %d != %d\n",
    454 		       USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
    455 		return;
    456 	}
    457 #if NRND > 0
    458 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    459 #endif
    460 	scd->sc_intr(scd, p, cc);
    461 }
    462 
    463 void
    464 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    465 {
    466 	*desc = sc->sc_repdesc;
    467 	*size = sc->sc_repdesc_size;
    468 }
    469 
    470 int
    471 uhidev_open(struct uhidev *scd)
    472 {
    473 	struct uhidev_softc *sc = scd->sc_parent;
    474 	usbd_status err;
    475 	int error;
    476 
    477 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    478 		 scd->sc_state, sc->sc_refcnt));
    479 
    480 	if (scd->sc_state & UHIDEV_OPEN)
    481 		return (EBUSY);
    482 	scd->sc_state |= UHIDEV_OPEN;
    483 	if (sc->sc_refcnt++)
    484 		return (0);
    485 
    486 	if (sc->sc_isize == 0)
    487 		return (0);
    488 
    489 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    490 
    491 	/* Set up input interrupt pipe. */
    492 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    493 		 sc->sc_iep_addr));
    494 
    495 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    496 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    497 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    498 	if (err != USBD_NORMAL_COMPLETION) {
    499 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    500 		    "error=%d\n", err));
    501 		error = EIO;
    502 		goto out1;
    503 	}
    504 
    505 	/*
    506 	 * Set up output interrupt pipe if an output interrupt endpoint
    507 	 * exists.
    508 	 */
    509 	if (sc->sc_oep_addr != -1) {
    510 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    511 
    512 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    513 		    0, &sc->sc_opipe);
    514 
    515 		if (err != USBD_NORMAL_COMPLETION) {
    516 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    517 			    "error=%d\n", err));
    518 			error = EIO;
    519 			goto out2;
    520 		}
    521 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    522 
    523 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    524 		if (sc->sc_oxfer == NULL) {
    525 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    526 			error = ENOMEM;
    527 			goto out3;
    528 		}
    529 	}
    530 
    531 	return (0);
    532 out3:
    533 	/* Abort output pipe */
    534 	usbd_close_pipe(sc->sc_opipe);
    535 out2:
    536 	/* Abort input pipe */
    537 	usbd_close_pipe(sc->sc_ipipe);
    538 out1:
    539 	DPRINTF(("uhidev_open: failed in someway"));
    540 	free(sc->sc_ibuf, M_USBDEV);
    541 	scd->sc_state &= ~UHIDEV_OPEN;
    542 	sc->sc_refcnt = 0;
    543 	sc->sc_ipipe = NULL;
    544 	sc->sc_opipe = NULL;
    545 	sc->sc_oxfer = NULL;
    546 	return error;
    547 }
    548 
    549 void
    550 uhidev_close(struct uhidev *scd)
    551 {
    552 	struct uhidev_softc *sc = scd->sc_parent;
    553 
    554 	if (!(scd->sc_state & UHIDEV_OPEN))
    555 		return;
    556 	scd->sc_state &= ~UHIDEV_OPEN;
    557 	if (--sc->sc_refcnt)
    558 		return;
    559 	DPRINTF(("uhidev_close: close pipe\n"));
    560 
    561 	if (sc->sc_oxfer != NULL)
    562 		usbd_free_xfer(sc->sc_oxfer);
    563 
    564 	/* Disable interrupts. */
    565 	if (sc->sc_opipe != NULL) {
    566 		usbd_abort_pipe(sc->sc_opipe);
    567 		usbd_close_pipe(sc->sc_opipe);
    568 		sc->sc_opipe = NULL;
    569 	}
    570 
    571 	if (sc->sc_ipipe != NULL) {
    572 		usbd_abort_pipe(sc->sc_ipipe);
    573 		usbd_close_pipe(sc->sc_ipipe);
    574 		sc->sc_ipipe = NULL;
    575 	}
    576 
    577 	if (sc->sc_ibuf != NULL) {
    578 		free(sc->sc_ibuf, M_USBDEV);
    579 		sc->sc_ibuf = NULL;
    580 	}
    581 }
    582 
    583 usbd_status
    584 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    585 {
    586 	char *buf;
    587 	usbd_status retstat;
    588 
    589 	if (scd->sc_report_id == 0)
    590 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    591 				       scd->sc_report_id, data, len);
    592 
    593 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    594 	buf[0] = scd->sc_report_id;
    595 	memcpy(buf+1, data, len);
    596 
    597 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    598 				  scd->sc_report_id, buf, len + 1);
    599 
    600 	free(buf, M_TEMP);
    601 
    602 	return retstat;
    603 }
    604 
    605 void
    606 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    607 {
    608 	/* XXX */
    609 	char buf[100];
    610 	if (scd->sc_report_id) {
    611 		buf[0] = scd->sc_report_id;
    612 		memcpy(buf+1, data, len);
    613 		len++;
    614 		data = buf;
    615 	}
    616 
    617 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    618 			      scd->sc_report_id, data, len);
    619 }
    620 
    621 usbd_status
    622 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    623 {
    624 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    625 			       scd->sc_report_id, data, len);
    626 }
    627 
    628 usbd_status
    629 uhidev_write(struct uhidev_softc *sc, void *data, int len)
    630 {
    631 
    632 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    633 
    634 	if (sc->sc_opipe == NULL)
    635 		return USBD_INVAL;
    636 
    637 #ifdef UHIDEV_DEBUG
    638 	if (uhidevdebug > 50) {
    639 
    640 		u_int32_t i;
    641 		u_int8_t *d = data;
    642 
    643 		DPRINTF(("uhidev_write: data ="));
    644 		for (i = 0; i < len; i++)
    645 			DPRINTF((" %02x", d[i]));
    646 		DPRINTF(("\n"));
    647 	}
    648 #endif
    649 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    650 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
    651 }
    652