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