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