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