Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.31
      1 /*	$NetBSD: uhidev.c,v 1.31 2005/11/23 08:54:48 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.31 2005/11/23 08:54:48 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 *, 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[256];
    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 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
    252 				M_USBDEV, M_NOWAIT | M_ZERO);
    253 	if (sc->sc_subdevs == NULL) {
    254 		printf("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    255 		USB_ATTACH_ERROR_RETURN;
    256 	}
    257 	sc->sc_nrepid = nrepid;
    258 	sc->sc_isize = 0;
    259 
    260 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    261 			   USBDEV(sc->sc_dev));
    262 
    263 	for (repid = 0; repid < nrepid; repid++) {
    264 		repsz = hid_report_size(desc, size, hid_input, repid);
    265 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    266 		repsizes[repid] = repsz;
    267 		if (repsz > 0) {
    268 			if (repsz > sc->sc_isize)
    269 				sc->sc_isize = repsz;
    270 		}
    271 	}
    272 	sc->sc_isize += nrepid != 1;	/* space for report ID */
    273 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    274 
    275 	uha.parent = sc;
    276 	for (repid = 0; repid < nrepid; repid++) {
    277 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    278 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    279 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    280 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    281 			;	/* already NULL in sc->sc_subdevs[repid] */
    282 		} else {
    283 			uha.reportid = repid;
    284 			locs[UHIDBUSCF_REPORTID] = repid;
    285 
    286 			dev = (struct uhidev *)config_found_sm_loc(self,
    287 				"uhidbus", locs, &uha,
    288 				uhidevprint, uhidevsubmatch);
    289 			sc->sc_subdevs[repid] = dev;
    290 			if (dev != NULL) {
    291 				dev->sc_in_rep_size = repsizes[repid];
    292 #ifdef DIAGNOSTIC
    293 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    294 					 repid, dev));
    295 				if (dev->sc_intr == NULL) {
    296 					printf("%s: sc_intr == NULL\n",
    297 					       USBDEVNAME(sc->sc_dev));
    298 					USB_ATTACH_ERROR_RETURN;
    299 				}
    300 #endif
    301 #if NRND > 0
    302 				rnd_attach_source(&dev->rnd_source,
    303 						  USBDEVNAME(dev->sc_dev),
    304 						  RND_TYPE_TTY, 0);
    305 #endif
    306 			}
    307 		}
    308 	}
    309 
    310 	USB_ATTACH_SUCCESS_RETURN;
    311 }
    312 
    313 int
    314 uhidev_maxrepid(void *buf, int len)
    315 {
    316 	struct hid_data *d;
    317 	struct hid_item h;
    318 	int maxid;
    319 
    320 	maxid = -1;
    321 	h.report_ID = 0;
    322 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    323 		if (h.report_ID > maxid)
    324 			maxid = h.report_ID;
    325 	hid_end_parse(d);
    326 	return (maxid);
    327 }
    328 
    329 int
    330 uhidevprint(void *aux, const char *pnp)
    331 {
    332 	struct uhidev_attach_arg *uha = aux;
    333 
    334 	if (pnp)
    335 		aprint_normal("uhid at %s", pnp);
    336 	if (uha->reportid != 0)
    337 		aprint_normal(" reportid %d", uha->reportid);
    338 	return (UNCONF);
    339 }
    340 
    341 int
    342 uhidevsubmatch(struct device *parent, struct cfdata *cf,
    343 	       const int *locs, void *aux)
    344 {
    345 	if (cf->cf_loc[UHIDBUSCF_REPORTID] != UHIDBUSCF_REPORTID_DEFAULT &&
    346 	    cf->cf_loc[UHIDBUSCF_REPORTID] != locs[UHIDBUSCF_REPORTID])
    347 		return (0);
    348 
    349 	return (config_match(parent, cf, aux));
    350 }
    351 
    352 int
    353 uhidev_activate(device_ptr_t self, enum devact act)
    354 {
    355 	struct uhidev_softc *sc = (struct uhidev_softc *)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]->sc_dev);
    368 		sc->sc_dying = 1;
    369 		break;
    370 	default:
    371 		rv = 0;
    372 		break;
    373 	}
    374 	return (rv);
    375 }
    376 
    377 USB_DETACH(uhidev)
    378 {
    379 	USB_DETACH_START(uhidev, sc);
    380 	int i, rv;
    381 
    382 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    383 
    384 	sc->sc_dying = 1;
    385 	if (sc->sc_ipipe != NULL)
    386 		usbd_abort_pipe(sc->sc_ipipe);
    387 
    388 	if (sc->sc_repdesc != NULL)
    389 		free(sc->sc_repdesc, M_USBDEV);
    390 
    391 	rv = 0;
    392 	for (i = 0; i < sc->sc_nrepid; i++) {
    393 		if (sc->sc_subdevs[i] != NULL) {
    394 #if NRND > 0
    395 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    396 #endif
    397 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    398 			sc->sc_subdevs[i] = NULL;
    399 		}
    400 	}
    401 
    402 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    403 			   USBDEV(sc->sc_dev));
    404 
    405 	return (rv);
    406 }
    407 
    408 void
    409 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    410 {
    411 	struct uhidev_softc *sc = addr;
    412 	struct uhidev *scd;
    413 	u_char *p;
    414 	u_int rep;
    415 	u_int32_t cc;
    416 
    417 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    418 
    419 #ifdef UHIDEV_DEBUG
    420 	if (uhidevdebug > 5) {
    421 		u_int32_t i;
    422 
    423 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    424 		DPRINTF(("uhidev_intr: data ="));
    425 		for (i = 0; i < cc; i++)
    426 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    427 		DPRINTF(("\n"));
    428 	}
    429 #endif
    430 
    431 	if (status == USBD_CANCELLED)
    432 		return;
    433 
    434 	if (status != USBD_NORMAL_COMPLETION) {
    435 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    436 			 status));
    437 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    438 		return;
    439 	}
    440 
    441 	p = sc->sc_ibuf;
    442 	if (sc->sc_nrepid != 1)
    443 		rep = *p++, cc--;
    444 	else
    445 		rep = 0;
    446 	if (rep >= sc->sc_nrepid) {
    447 		printf("uhidev_intr: bad repid %d\n", rep);
    448 		return;
    449 	}
    450 	scd = sc->sc_subdevs[rep];
    451 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    452 		    rep, scd, scd ? scd->sc_state : 0));
    453 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    454 		return;
    455 	if (scd->sc_in_rep_size != cc) {
    456 		printf("%s: bad input length %d != %d\n",
    457 		       USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
    458 		return;
    459 	}
    460 #if NRND > 0
    461 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    462 #endif
    463 	scd->sc_intr(scd, p, cc);
    464 }
    465 
    466 void
    467 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    468 {
    469 	*desc = sc->sc_repdesc;
    470 	*size = sc->sc_repdesc_size;
    471 }
    472 
    473 int
    474 uhidev_open(struct uhidev *scd)
    475 {
    476 	struct uhidev_softc *sc = scd->sc_parent;
    477 	usbd_status err;
    478 	int error;
    479 
    480 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    481 		 scd->sc_state, sc->sc_refcnt));
    482 
    483 	if (scd->sc_state & UHIDEV_OPEN)
    484 		return (EBUSY);
    485 	scd->sc_state |= UHIDEV_OPEN;
    486 	if (sc->sc_refcnt++)
    487 		return (0);
    488 
    489 	if (sc->sc_isize == 0)
    490 		return (0);
    491 
    492 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    493 
    494 	/* Set up input interrupt pipe. */
    495 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    496 		 sc->sc_iep_addr));
    497 
    498 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    499 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    500 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    501 	if (err != USBD_NORMAL_COMPLETION) {
    502 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    503 		    "error=%d\n", err));
    504 		error = EIO;
    505 		goto out1;
    506 	}
    507 
    508 	/*
    509 	 * Set up output interrupt pipe if an output interrupt endpoint
    510 	 * exists.
    511 	 */
    512 	if (sc->sc_oep_addr != -1) {
    513 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    514 
    515 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    516 		    0, &sc->sc_opipe);
    517 
    518 		if (err != USBD_NORMAL_COMPLETION) {
    519 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    520 			    "error=%d\n", err));
    521 			error = EIO;
    522 			goto out2;
    523 		}
    524 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    525 
    526 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    527 		if (sc->sc_oxfer == NULL) {
    528 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    529 			error = ENOMEM;
    530 			goto out3;
    531 		}
    532 	}
    533 
    534 	return (0);
    535 out3:
    536 	/* Abort output pipe */
    537 	usbd_close_pipe(sc->sc_opipe);
    538 out2:
    539 	/* Abort input pipe */
    540 	usbd_close_pipe(sc->sc_ipipe);
    541 out1:
    542 	DPRINTF(("uhidev_open: failed in someway"));
    543 	free(sc->sc_ibuf, M_USBDEV);
    544 	scd->sc_state &= ~UHIDEV_OPEN;
    545 	sc->sc_refcnt = 0;
    546 	sc->sc_ipipe = NULL;
    547 	sc->sc_opipe = NULL;
    548 	sc->sc_oxfer = NULL;
    549 	return error;
    550 }
    551 
    552 void
    553 uhidev_close(struct uhidev *scd)
    554 {
    555 	struct uhidev_softc *sc = scd->sc_parent;
    556 
    557 	if (!(scd->sc_state & UHIDEV_OPEN))
    558 		return;
    559 	scd->sc_state &= ~UHIDEV_OPEN;
    560 	if (--sc->sc_refcnt)
    561 		return;
    562 	DPRINTF(("uhidev_close: close pipe\n"));
    563 
    564 	if (sc->sc_oxfer != NULL)
    565 		usbd_free_xfer(sc->sc_oxfer);
    566 
    567 	/* Disable interrupts. */
    568 	if (sc->sc_opipe != NULL) {
    569 		usbd_abort_pipe(sc->sc_opipe);
    570 		usbd_close_pipe(sc->sc_opipe);
    571 		sc->sc_opipe = NULL;
    572 	}
    573 
    574 	if (sc->sc_ipipe != NULL) {
    575 		usbd_abort_pipe(sc->sc_ipipe);
    576 		usbd_close_pipe(sc->sc_ipipe);
    577 		sc->sc_ipipe = NULL;
    578 	}
    579 
    580 	if (sc->sc_ibuf != NULL) {
    581 		free(sc->sc_ibuf, M_USBDEV);
    582 		sc->sc_ibuf = NULL;
    583 	}
    584 }
    585 
    586 usbd_status
    587 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    588 {
    589 	char *buf;
    590 	usbd_status retstat;
    591 
    592 	if (scd->sc_report_id == 0)
    593 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    594 				       scd->sc_report_id, data, len);
    595 
    596 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    597 	buf[0] = scd->sc_report_id;
    598 	memcpy(buf+1, data, len);
    599 
    600 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    601 				  scd->sc_report_id, buf, len + 1);
    602 
    603 	free(buf, M_TEMP);
    604 
    605 	return retstat;
    606 }
    607 
    608 void
    609 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    610 {
    611 	/* XXX */
    612 	char buf[100];
    613 	if (scd->sc_report_id) {
    614 		buf[0] = scd->sc_report_id;
    615 		memcpy(buf+1, data, len);
    616 		len++;
    617 		data = buf;
    618 	}
    619 
    620 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    621 			      scd->sc_report_id, data, len);
    622 }
    623 
    624 usbd_status
    625 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    626 {
    627 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    628 			       scd->sc_report_id, data, len);
    629 }
    630 
    631 usbd_status
    632 uhidev_write(struct uhidev_softc *sc, void *data, int len)
    633 {
    634 
    635 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    636 
    637 	if (sc->sc_opipe == NULL)
    638 		return USBD_INVAL;
    639 
    640 #ifdef UHIDEV_DEBUG
    641 	if (uhidevdebug > 50) {
    642 
    643 		u_int32_t i;
    644 		u_int8_t *d = data;
    645 
    646 		DPRINTF(("uhidev_write: data ="));
    647 		for (i = 0; i < len; i++)
    648 			DPRINTF((" %02x", d[i]));
    649 		DPRINTF(("\n"));
    650 	}
    651 #endif
    652 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    653 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
    654 }
    655