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