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