Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.35.10.2
      1 /*	$NetBSD: uhidev.c,v 1.35.10.2 2008/01/09 01:54:43 matt 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.35.10.2 2008/01/09 01:54:43 matt 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 	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_ptr_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_ptr_t self, enum devact act)
    355 {
    356 	struct uhidev_softc *sc = (struct uhidev_softc *)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 USB_DETACH(uhidev)
    379 {
    380 	USB_DETACH_START(uhidev, sc);
    381 	int i, rv;
    382 
    383 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    384 
    385 	sc->sc_dying = 1;
    386 	if (sc->sc_ipipe != NULL)
    387 		usbd_abort_pipe(sc->sc_ipipe);
    388 
    389 	if (sc->sc_repdesc != NULL)
    390 		free(sc->sc_repdesc, M_USBDEV);
    391 
    392 	rv = 0;
    393 	for (i = 0; i < sc->sc_nrepid; i++) {
    394 		if (sc->sc_subdevs[i] != NULL) {
    395 #if NRND > 0
    396 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    397 #endif
    398 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    399 			sc->sc_subdevs[i] = NULL;
    400 		}
    401 	}
    402 
    403 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    404 			   USBDEV(sc->sc_dev));
    405 
    406 	pmf_device_deregister(self);
    407 
    408 	return (rv);
    409 }
    410 
    411 void
    412 uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    413 {
    414 	struct uhidev_softc *sc = addr;
    415 	struct uhidev *scd;
    416 	u_char *p;
    417 	u_int rep;
    418 	u_int32_t cc;
    419 
    420 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    421 
    422 #ifdef UHIDEV_DEBUG
    423 	if (uhidevdebug > 5) {
    424 		u_int32_t i;
    425 
    426 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    427 		DPRINTF(("uhidev_intr: data ="));
    428 		for (i = 0; i < cc; i++)
    429 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    430 		DPRINTF(("\n"));
    431 	}
    432 #endif
    433 
    434 	if (status == USBD_CANCELLED)
    435 		return;
    436 
    437 	if (status != USBD_NORMAL_COMPLETION) {
    438 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    439 			 status));
    440 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    441 		return;
    442 	}
    443 
    444 	p = sc->sc_ibuf;
    445 	if (sc->sc_nrepid != 1)
    446 		rep = *p++, cc--;
    447 	else
    448 		rep = 0;
    449 	if (rep >= sc->sc_nrepid) {
    450 		printf("uhidev_intr: bad repid %d\n", rep);
    451 		return;
    452 	}
    453 	scd = sc->sc_subdevs[rep];
    454 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    455 		    rep, scd, scd ? scd->sc_state : 0));
    456 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    457 		return;
    458 	if (scd->sc_in_rep_size != cc) {
    459 		printf("%s: bad input length %d != %d\n",
    460 		       USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
    461 		return;
    462 	}
    463 #if NRND > 0
    464 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    465 #endif
    466 	scd->sc_intr(scd, p, cc);
    467 }
    468 
    469 void
    470 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    471 {
    472 	*desc = sc->sc_repdesc;
    473 	*size = sc->sc_repdesc_size;
    474 }
    475 
    476 int
    477 uhidev_open(struct uhidev *scd)
    478 {
    479 	struct uhidev_softc *sc = scd->sc_parent;
    480 	usbd_status err;
    481 	int error;
    482 
    483 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    484 		 scd->sc_state, sc->sc_refcnt));
    485 
    486 	if (scd->sc_state & UHIDEV_OPEN)
    487 		return (EBUSY);
    488 	scd->sc_state |= UHIDEV_OPEN;
    489 	if (sc->sc_refcnt++)
    490 		return (0);
    491 
    492 	if (sc->sc_isize == 0)
    493 		return (0);
    494 
    495 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    496 
    497 	/* Set up input interrupt pipe. */
    498 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    499 		 sc->sc_iep_addr));
    500 
    501 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    502 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    503 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    504 	if (err != USBD_NORMAL_COMPLETION) {
    505 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    506 		    "error=%d\n", err));
    507 		error = EIO;
    508 		goto out1;
    509 	}
    510 
    511 	/*
    512 	 * Set up output interrupt pipe if an output interrupt endpoint
    513 	 * exists.
    514 	 */
    515 	if (sc->sc_oep_addr != -1) {
    516 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    517 
    518 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    519 		    0, &sc->sc_opipe);
    520 
    521 		if (err != USBD_NORMAL_COMPLETION) {
    522 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    523 			    "error=%d\n", err));
    524 			error = EIO;
    525 			goto out2;
    526 		}
    527 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    528 
    529 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    530 		if (sc->sc_oxfer == NULL) {
    531 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    532 			error = ENOMEM;
    533 			goto out3;
    534 		}
    535 	}
    536 
    537 	return (0);
    538 out3:
    539 	/* Abort output pipe */
    540 	usbd_close_pipe(sc->sc_opipe);
    541 out2:
    542 	/* Abort input pipe */
    543 	usbd_close_pipe(sc->sc_ipipe);
    544 out1:
    545 	DPRINTF(("uhidev_open: failed in someway"));
    546 	free(sc->sc_ibuf, M_USBDEV);
    547 	scd->sc_state &= ~UHIDEV_OPEN;
    548 	sc->sc_refcnt = 0;
    549 	sc->sc_ipipe = NULL;
    550 	sc->sc_opipe = NULL;
    551 	sc->sc_oxfer = NULL;
    552 	return error;
    553 }
    554 
    555 void
    556 uhidev_close(struct uhidev *scd)
    557 {
    558 	struct uhidev_softc *sc = scd->sc_parent;
    559 
    560 	if (!(scd->sc_state & UHIDEV_OPEN))
    561 		return;
    562 	scd->sc_state &= ~UHIDEV_OPEN;
    563 	if (--sc->sc_refcnt)
    564 		return;
    565 	DPRINTF(("uhidev_close: close pipe\n"));
    566 
    567 	if (sc->sc_oxfer != NULL)
    568 		usbd_free_xfer(sc->sc_oxfer);
    569 
    570 	/* Disable interrupts. */
    571 	if (sc->sc_opipe != NULL) {
    572 		usbd_abort_pipe(sc->sc_opipe);
    573 		usbd_close_pipe(sc->sc_opipe);
    574 		sc->sc_opipe = NULL;
    575 	}
    576 
    577 	if (sc->sc_ipipe != NULL) {
    578 		usbd_abort_pipe(sc->sc_ipipe);
    579 		usbd_close_pipe(sc->sc_ipipe);
    580 		sc->sc_ipipe = NULL;
    581 	}
    582 
    583 	if (sc->sc_ibuf != NULL) {
    584 		free(sc->sc_ibuf, M_USBDEV);
    585 		sc->sc_ibuf = NULL;
    586 	}
    587 }
    588 
    589 usbd_status
    590 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    591 {
    592 	char *buf;
    593 	usbd_status retstat;
    594 
    595 	if (scd->sc_report_id == 0)
    596 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    597 				       scd->sc_report_id, data, len);
    598 
    599 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    600 	buf[0] = scd->sc_report_id;
    601 	memcpy(buf+1, data, len);
    602 
    603 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    604 				  scd->sc_report_id, buf, len + 1);
    605 
    606 	free(buf, M_TEMP);
    607 
    608 	return retstat;
    609 }
    610 
    611 void
    612 uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    613 {
    614 	/* XXX */
    615 	char buf[100];
    616 	if (scd->sc_report_id) {
    617 		buf[0] = scd->sc_report_id;
    618 		memcpy(buf+1, data, len);
    619 		len++;
    620 		data = buf;
    621 	}
    622 
    623 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    624 			      scd->sc_report_id, data, len);
    625 }
    626 
    627 usbd_status
    628 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    629 {
    630 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    631 			       scd->sc_report_id, data, len);
    632 }
    633 
    634 usbd_status
    635 uhidev_write(struct uhidev_softc *sc, void *data, int len)
    636 {
    637 
    638 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    639 
    640 	if (sc->sc_opipe == NULL)
    641 		return USBD_INVAL;
    642 
    643 #ifdef UHIDEV_DEBUG
    644 	if (uhidevdebug > 50) {
    645 
    646 		u_int32_t i;
    647 		u_int8_t *d = data;
    648 
    649 		DPRINTF(("uhidev_write: data ="));
    650 		for (i = 0; i < len; i++)
    651 			DPRINTF((" %02x", d[i]));
    652 		DPRINTF(("\n"));
    653 	}
    654 #endif
    655 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    656 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
    657 }
    658