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