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