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