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