Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.48
      1 /*	$NetBSD: uhidev.c,v 1.48 2010/11/03 22:34:24 dyoung 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.48 2010/11/03 22:34:24 dyoung Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/signalvar.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/conf.h>
     48 
     49 #include <dev/usb/usb.h>
     50 #include <dev/usb/usbhid.h>
     51 
     52 #include <dev/usb/usbdevs.h>
     53 #include <dev/usb/usbdi.h>
     54 #include <dev/usb/usbdi_util.h>
     55 #include <dev/usb/hid.h>
     56 #include <dev/usb/usb_quirks.h>
     57 
     58 #include <dev/usb/uhidev.h>
     59 
     60 /* Report descriptor for broken Wacom Graphire */
     61 #include <dev/usb/ugraphire_rdesc.h>
     62 
     63 #include "locators.h"
     64 
     65 #ifdef UHIDEV_DEBUG
     66 #define DPRINTF(x)	if (uhidevdebug) printf x
     67 #define DPRINTFN(n,x)	if (uhidevdebug>(n)) printf x
     68 int	uhidevdebug = 0;
     69 #else
     70 #define DPRINTF(x)
     71 #define DPRINTFN(n,x)
     72 #endif
     73 
     74 Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
     75 
     76 Static int uhidev_maxrepid(void *, int);
     77 Static int uhidevprint(void *, const char *);
     78 
     79 int uhidev_match(device_t, cfdata_t, void *);
     80 void uhidev_attach(device_t, device_t, void *);
     81 void uhidev_childdet(device_t, device_t);
     82 int uhidev_detach(device_t, int);
     83 int uhidev_activate(device_t, enum devact);
     84 extern struct cfdriver uhidev_cd;
     85 CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
     86     uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
     87 
     88 int
     89 uhidev_match(device_t parent, cfdata_t match, void *aux)
     90 {
     91 	struct usbif_attach_arg *uaa = aux;
     92 
     93 	if (uaa->class != UICLASS_HID)
     94 		return (UMATCH_NONE);
     95 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_HID_IGNORE)
     96 		return (UMATCH_NONE);
     97 	return (UMATCH_IFACECLASS_GENERIC);
     98 }
     99 
    100 void
    101 uhidev_attach(device_t parent, device_t self, void *aux)
    102 {
    103 	struct uhidev_softc *sc = device_private(self);
    104 	struct usbif_attach_arg *uaa = aux;
    105 	usbd_interface_handle iface = uaa->iface;
    106 	usb_interface_descriptor_t *id;
    107 	usb_endpoint_descriptor_t *ed;
    108 	struct uhidev_attach_arg uha;
    109 	device_t dev;
    110 	struct uhidev *csc;
    111 	int maxinpktsize, size, nrepid, repid, repsz;
    112 	int *repsizes;
    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_dev = self;
    121 	sc->sc_udev = uaa->device;
    122 	sc->sc_iface = iface;
    123 
    124 	aprint_naive("\n");
    125 	aprint_normal("\n");
    126 
    127 	id = usbd_get_interface_descriptor(iface);
    128 
    129 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    130 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    131 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    132 	usbd_devinfo_free(devinfop);
    133 
    134 	if (!pmf_device_register(self, NULL, NULL))
    135 		aprint_error_dev(self, "couldn't establish power handler\n");
    136 
    137 	(void)usbd_set_idle(iface, 0, 0);
    138 #if 0
    139 
    140 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
    141 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
    142 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
    143 		(void)usbd_set_protocol(iface, 1);
    144 #endif
    145 
    146 	maxinpktsize = 0;
    147 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
    148 	for (i = 0; i < id->bNumEndpoints; i++) {
    149 		ed = usbd_interface2endpoint_descriptor(iface, i);
    150 		if (ed == NULL) {
    151 			aprint_error_dev(self,
    152 			    "could not read endpoint descriptor\n");
    153 			sc->sc_dying = 1;
    154 			return;
    155 		}
    156 
    157 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    158 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    159 		    " bInterval=%d\n",
    160 		    ed->bLength, ed->bDescriptorType,
    161 		    ed->bEndpointAddress & UE_ADDR,
    162 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    163 		    ed->bmAttributes & UE_XFERTYPE,
    164 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
    165 
    166 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    167 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    168 			maxinpktsize = UGETW(ed->wMaxPacketSize);
    169 			sc->sc_iep_addr = ed->bEndpointAddress;
    170 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    171 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    172 			sc->sc_oep_addr = ed->bEndpointAddress;
    173 		} else {
    174 			aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
    175 		}
    176 	}
    177 
    178 	/*
    179 	 * Check that we found an input interrupt endpoint. The output interrupt
    180 	 * endpoint is optional
    181 	 */
    182 	if (sc->sc_iep_addr == -1) {
    183 		aprint_error_dev(self, "no input interrupt endpoint\n");
    184 		sc->sc_dying = 1;
    185 		return;
    186 	}
    187 
    188 	/* XXX need to extend this */
    189 	descptr = NULL;
    190 	if (uaa->vendor == USB_VENDOR_WACOM) {
    191 		static uByte reportbuf[] = {2, 2, 2};
    192 
    193 		/* The report descriptor for the Wacom Graphire is broken. */
    194 		switch (uaa->product) {
    195 		case USB_PRODUCT_WACOM_GRAPHIRE:
    196 			size = sizeof uhid_graphire_report_descr;
    197 			descptr = uhid_graphire_report_descr;
    198 			break;
    199 
    200 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
    201 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
    202 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
    203 			/*
    204 			 * The Graphire3 needs 0x0202 to be written to
    205 			 * feature report ID 2 before it'll start
    206 			 * returning digitizer data.
    207 			 */
    208 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
    209 			    &reportbuf, sizeof reportbuf);
    210 
    211 			size = sizeof uhid_graphire3_4x5_report_descr;
    212 			descptr = uhid_graphire3_4x5_report_descr;
    213 			break;
    214 		default:
    215 			/* Keep descriptor */
    216 			break;
    217 		}
    218 	}
    219 
    220 	if (descptr) {
    221 		desc = malloc(size, M_USBDEV, M_NOWAIT);
    222 		if (desc == NULL)
    223 			err = USBD_NOMEM;
    224 		else {
    225 			err = USBD_NORMAL_COMPLETION;
    226 			memcpy(desc, descptr, size);
    227 		}
    228 	} else {
    229 		desc = NULL;
    230 		err = usbd_read_report_desc(uaa->iface, &desc, &size,
    231 		    M_USBDEV);
    232 	}
    233 	if (err) {
    234 		aprint_error_dev(self, "no report descriptor\n");
    235 		sc->sc_dying = 1;
    236 		return;
    237 	}
    238 
    239 	if (uaa->vendor == USB_VENDOR_HOSIDEN &&
    240 	    uaa->product == USB_PRODUCT_HOSIDEN_PPP) {
    241 		static uByte reportbuf[] = { 1 };
    242 		/*
    243 		 *  This device was sold by Konami with its ParaParaParadise
    244 		 *  game for PlayStation2.  It needs to be "turned on"
    245 		 *  before it will send any reports.
    246 		 */
    247 
    248 		usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 0,
    249 		    &reportbuf, sizeof reportbuf);
    250 	}
    251 
    252 	if (uaa->vendor == USB_VENDOR_LOGITECH &&
    253 	    uaa->product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
    254 		uint8_t *data = desc;
    255 		/*
    256 		 * This device has a odd USAGE_MINIMUM value that would
    257 		 * cause the multimedia keys to have their usage number
    258 		 * shifted up one usage.  Adjust so the usages are sane.
    259 		 */
    260 
    261 		if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
    262 		    data[0x58] == 0x2a && data[0x59] == 0x8c)
    263 			data[0x57] = 0x00;
    264 	}
    265 
    266 	sc->sc_repdesc = desc;
    267 	sc->sc_repdesc_size = size;
    268 
    269 	uha.uaa = uaa;
    270 	nrepid = uhidev_maxrepid(desc, size);
    271 	if (nrepid < 0)
    272 		return;
    273 	if (nrepid > 0)
    274 		aprint_normal_dev(self, "%d report ids\n", nrepid);
    275 	nrepid++;
    276 	repsizes = malloc(nrepid * sizeof(*repsizes), M_TEMP, M_NOWAIT);
    277 	if (repsizes == NULL)
    278 		goto nomem;
    279 	sc->sc_subdevs = malloc(nrepid * sizeof(device_t),
    280 				M_USBDEV, M_NOWAIT | M_ZERO);
    281 	if (sc->sc_subdevs == NULL) {
    282 		free(repsizes, M_TEMP);
    283 nomem:
    284 		aprint_error_dev(self, "no memory\n");
    285 		return;
    286 	}
    287 
    288 	/* Just request max packet size for the interrupt pipe */
    289 	sc->sc_isize = maxinpktsize;
    290 	sc->sc_nrepid = nrepid;
    291 
    292 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    293 			   sc->sc_dev);
    294 
    295 	for (repid = 0; repid < nrepid; repid++) {
    296 		repsz = hid_report_size(desc, size, hid_input, repid);
    297 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    298 		repsizes[repid] = repsz;
    299 	}
    300 
    301 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    302 
    303 	uha.parent = sc;
    304 	for (repid = 0; repid < nrepid; repid++) {
    305 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    306 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    307 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    308 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    309 			;	/* already NULL in sc->sc_subdevs[repid] */
    310 		} else {
    311 			uha.reportid = repid;
    312 			locs[UHIDBUSCF_REPORTID] = repid;
    313 
    314 			dev = config_found_sm_loc(self,
    315 				"uhidbus", locs, &uha,
    316 				uhidevprint, config_stdsubmatch);
    317 			sc->sc_subdevs[repid] = dev;
    318 			if (dev != NULL) {
    319 				csc = device_private(dev);
    320 				csc->sc_in_rep_size = repsizes[repid];
    321 #ifdef DIAGNOSTIC
    322 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    323 					 repid, dev));
    324 				if (csc->sc_intr == NULL) {
    325 					free(repsizes, M_TEMP);
    326 					aprint_error_dev(self,
    327 					    "sc_intr == NULL\n");
    328 					return;
    329 				}
    330 #endif
    331 #if NRND > 0
    332 				rnd_attach_source(&csc->rnd_source,
    333 						  device_xname(dev),
    334 						  RND_TYPE_TTY, 0);
    335 #endif
    336 			}
    337 		}
    338 	}
    339 	free(repsizes, M_TEMP);
    340 
    341 	return;
    342 }
    343 
    344 int
    345 uhidev_maxrepid(void *buf, int len)
    346 {
    347 	struct hid_data *d;
    348 	struct hid_item h;
    349 	int maxid;
    350 
    351 	maxid = -1;
    352 	h.report_ID = 0;
    353 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    354 		if (h.report_ID > maxid)
    355 			maxid = h.report_ID;
    356 	hid_end_parse(d);
    357 	return (maxid);
    358 }
    359 
    360 int
    361 uhidevprint(void *aux, const char *pnp)
    362 {
    363 	struct uhidev_attach_arg *uha = aux;
    364 
    365 	if (pnp)
    366 		aprint_normal("uhid at %s", pnp);
    367 	if (uha->reportid != 0)
    368 		aprint_normal(" reportid %d", uha->reportid);
    369 	return (UNCONF);
    370 }
    371 
    372 int
    373 uhidev_activate(device_t self, enum devact act)
    374 {
    375 	struct uhidev_softc *sc = device_private(self);
    376 
    377 	switch (act) {
    378 	case DVACT_DEACTIVATE:
    379 		sc->sc_dying = 1;
    380 		return 0;
    381 	default:
    382 		return EOPNOTSUPP;
    383 	}
    384 }
    385 
    386 void
    387 uhidev_childdet(device_t self, device_t child)
    388 {
    389 	int i;
    390 	struct uhidev_softc *sc = device_private(self);
    391 
    392 	for (i = 0; i < sc->sc_nrepid; i++) {
    393 		if (sc->sc_subdevs[i] == child)
    394 			break;
    395 	}
    396 	KASSERT(i < sc->sc_nrepid);
    397 	sc->sc_subdevs[i] = NULL;
    398 }
    399 
    400 int
    401 uhidev_detach(device_t self, int flags)
    402 {
    403 	struct uhidev_softc *sc = device_private(self);
    404 	int i, rv;
    405 #if NRND > 0
    406 	struct uhidev *csc;
    407 #endif
    408 
    409 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    410 
    411 	sc->sc_dying = 1;
    412 	if (sc->sc_ipipe != NULL)
    413 		usbd_abort_pipe(sc->sc_ipipe);
    414 
    415 	if (sc->sc_repdesc != NULL)
    416 		free(sc->sc_repdesc, M_USBDEV);
    417 
    418 	rv = 0;
    419 	for (i = 0; i < sc->sc_nrepid; i++) {
    420 		if (sc->sc_subdevs[i] != NULL) {
    421 #if NRND > 0
    422 			csc = device_private(sc->sc_subdevs[i]);
    423 			rnd_detach_source(&csc->rnd_source);
    424 #endif
    425 			rv |= config_detach(sc->sc_subdevs[i], flags);
    426 		}
    427 	}
    428 
    429 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    430 			   sc->sc_dev);
    431 
    432 	pmf_device_deregister(self);
    433 
    434 	return (rv);
    435 }
    436 
    437 void
    438 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    439 {
    440 	struct uhidev_softc *sc = addr;
    441 	device_t cdev;
    442 	struct uhidev *scd;
    443 	u_char *p;
    444 	u_int rep;
    445 	u_int32_t cc;
    446 
    447 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    448 
    449 #ifdef UHIDEV_DEBUG
    450 	if (uhidevdebug > 5) {
    451 		u_int32_t i;
    452 
    453 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    454 		DPRINTF(("uhidev_intr: data ="));
    455 		for (i = 0; i < cc; i++)
    456 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    457 		DPRINTF(("\n"));
    458 	}
    459 #endif
    460 
    461 	if (status == USBD_CANCELLED)
    462 		return;
    463 
    464 	if (status != USBD_NORMAL_COMPLETION) {
    465 		DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
    466 			 status));
    467 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    468 		return;
    469 	}
    470 
    471 	p = sc->sc_ibuf;
    472 	if (sc->sc_nrepid != 1)
    473 		rep = *p++, cc--;
    474 	else
    475 		rep = 0;
    476 	if (rep >= sc->sc_nrepid) {
    477 		printf("uhidev_intr: bad repid %d\n", rep);
    478 		return;
    479 	}
    480 	cdev = sc->sc_subdevs[rep];
    481 	if (!cdev)
    482 		return;
    483 	scd = device_private(cdev);
    484 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    485 		    rep, scd, scd ? scd->sc_state : 0));
    486 	if (!(scd->sc_state & UHIDEV_OPEN))
    487 		return;
    488 #ifdef UHIDEV_DEBUG
    489 	if (scd->sc_in_rep_size != cc) {
    490 		DPRINTF(("%s: expected %d bytes, got %d\n",
    491 		       device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
    492 	}
    493 #endif
    494 	if (cc == 0) {
    495 		DPRINTF(("%s: 0-length input ignored\n",
    496 			device_xname(sc->sc_dev)));
    497 		return;
    498 	}
    499 #if NRND > 0
    500 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    501 #endif
    502 	scd->sc_intr(scd, p, cc);
    503 }
    504 
    505 void
    506 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    507 {
    508 	*desc = sc->sc_repdesc;
    509 	*size = sc->sc_repdesc_size;
    510 }
    511 
    512 int
    513 uhidev_open(struct uhidev *scd)
    514 {
    515 	struct uhidev_softc *sc = scd->sc_parent;
    516 	usbd_status err;
    517 	int error;
    518 
    519 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    520 		 scd->sc_state, sc->sc_refcnt));
    521 
    522 	if (scd->sc_state & UHIDEV_OPEN)
    523 		return (EBUSY);
    524 	scd->sc_state |= UHIDEV_OPEN;
    525 	if (sc->sc_refcnt++)
    526 		return (0);
    527 
    528 	if (sc->sc_isize == 0)
    529 		return (0);
    530 
    531 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    532 
    533 	/* Set up input interrupt pipe. */
    534 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    535 		 sc->sc_iep_addr));
    536 
    537 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    538 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    539 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    540 	if (err != USBD_NORMAL_COMPLETION) {
    541 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    542 		    "error=%d\n", err));
    543 		error = EIO;
    544 		goto out1;
    545 	}
    546 
    547 	/*
    548 	 * Set up output interrupt pipe if an output interrupt endpoint
    549 	 * exists.
    550 	 */
    551 	if (sc->sc_oep_addr != -1) {
    552 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    553 
    554 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    555 		    0, &sc->sc_opipe);
    556 
    557 		if (err != USBD_NORMAL_COMPLETION) {
    558 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    559 			    "error=%d\n", err));
    560 			error = EIO;
    561 			goto out2;
    562 		}
    563 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    564 
    565 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    566 		if (sc->sc_oxfer == NULL) {
    567 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    568 			error = ENOMEM;
    569 			goto out3;
    570 		}
    571 	}
    572 
    573 	return (0);
    574 out3:
    575 	/* Abort output pipe */
    576 	usbd_close_pipe(sc->sc_opipe);
    577 out2:
    578 	/* Abort input pipe */
    579 	usbd_close_pipe(sc->sc_ipipe);
    580 out1:
    581 	DPRINTF(("uhidev_open: failed in someway"));
    582 	free(sc->sc_ibuf, M_USBDEV);
    583 	scd->sc_state &= ~UHIDEV_OPEN;
    584 	sc->sc_refcnt = 0;
    585 	sc->sc_ipipe = NULL;
    586 	sc->sc_opipe = NULL;
    587 	sc->sc_oxfer = NULL;
    588 	return error;
    589 }
    590 
    591 void
    592 uhidev_close(struct uhidev *scd)
    593 {
    594 	struct uhidev_softc *sc = scd->sc_parent;
    595 
    596 	if (!(scd->sc_state & UHIDEV_OPEN))
    597 		return;
    598 	scd->sc_state &= ~UHIDEV_OPEN;
    599 	if (--sc->sc_refcnt)
    600 		return;
    601 	DPRINTF(("uhidev_close: close pipe\n"));
    602 
    603 	if (sc->sc_oxfer != NULL)
    604 		usbd_free_xfer(sc->sc_oxfer);
    605 
    606 	/* Disable interrupts. */
    607 	if (sc->sc_opipe != NULL) {
    608 		usbd_abort_pipe(sc->sc_opipe);
    609 		usbd_close_pipe(sc->sc_opipe);
    610 		sc->sc_opipe = NULL;
    611 	}
    612 
    613 	if (sc->sc_ipipe != NULL) {
    614 		usbd_abort_pipe(sc->sc_ipipe);
    615 		usbd_close_pipe(sc->sc_ipipe);
    616 		sc->sc_ipipe = NULL;
    617 	}
    618 
    619 	if (sc->sc_ibuf != NULL) {
    620 		free(sc->sc_ibuf, M_USBDEV);
    621 		sc->sc_ibuf = NULL;
    622 	}
    623 }
    624 
    625 usbd_status
    626 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    627 {
    628 	char *buf;
    629 	usbd_status retstat;
    630 
    631 	if (scd->sc_report_id == 0)
    632 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    633 				       scd->sc_report_id, data, len);
    634 
    635 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    636 	buf[0] = scd->sc_report_id;
    637 	memcpy(buf+1, data, len);
    638 
    639 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    640 				  scd->sc_report_id, buf, len + 1);
    641 
    642 	free(buf, M_TEMP);
    643 
    644 	return retstat;
    645 }
    646 
    647 void
    648 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    649 {
    650 	/* XXX */
    651 	char buf[100];
    652 	if (scd->sc_report_id) {
    653 		buf[0] = scd->sc_report_id;
    654 		memcpy(buf+1, data, len);
    655 		len++;
    656 		data = buf;
    657 	}
    658 
    659 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    660 			      scd->sc_report_id, data, len);
    661 }
    662 
    663 usbd_status
    664 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    665 {
    666 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    667 			       scd->sc_report_id, data, len);
    668 }
    669 
    670 usbd_status
    671 uhidev_write(struct uhidev_softc *sc, void *data, int len)
    672 {
    673 
    674 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    675 
    676 	if (sc->sc_opipe == NULL)
    677 		return USBD_INVAL;
    678 
    679 #ifdef UHIDEV_DEBUG
    680 	if (uhidevdebug > 50) {
    681 
    682 		u_int32_t i;
    683 		u_int8_t *d = data;
    684 
    685 		DPRINTF(("uhidev_write: data ="));
    686 		for (i = 0; i < len; i++)
    687 			DPRINTF((" %02x", d[i]));
    688 		DPRINTF(("\n"));
    689 	}
    690 #endif
    691 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    692 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
    693 }
    694