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