Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.70
      1 /*	$NetBSD: uhidev.c,v 1.70 2017/06/01 02:45:12 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
     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.70 2017/06/01 02:45:12 chs Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_usb.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/kmem.h>
     48 #include <sys/signalvar.h>
     49 #include <sys/device.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/conf.h>
     52 #include <sys/rndsource.h>
     53 
     54 #include <dev/usb/usb.h>
     55 #include <dev/usb/usbhid.h>
     56 
     57 #include <dev/usb/usbdevs.h>
     58 #include <dev/usb/usbdi.h>
     59 #include <dev/usb/usbdi_util.h>
     60 #include <dev/usb/hid.h>
     61 #include <dev/usb/usb_quirks.h>
     62 
     63 #include <dev/usb/uhidev.h>
     64 
     65 /* Report descriptor for broken Wacom Graphire */
     66 #include <dev/usb/ugraphire_rdesc.h>
     67 /* Report descriptor for game controllers in "XInput" mode */
     68 #include <dev/usb/xinput_rdesc.h>
     69 /* Report descriptor for Xbox One controllers */
     70 #include <dev/usb/x1input_rdesc.h>
     71 
     72 #include "locators.h"
     73 
     74 #ifdef UHIDEV_DEBUG
     75 #define DPRINTF(x)	if (uhidevdebug) printf x
     76 #define DPRINTFN(n,x)	if (uhidevdebug>(n)) printf x
     77 int	uhidevdebug = 0;
     78 #else
     79 #define DPRINTF(x)
     80 #define DPRINTFN(n,x)
     81 #endif
     82 
     83 Static void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
     84 
     85 Static int uhidev_maxrepid(void *, int);
     86 Static int uhidevprint(void *, const char *);
     87 
     88 int uhidev_match(device_t, cfdata_t, void *);
     89 void uhidev_attach(device_t, device_t, void *);
     90 void uhidev_childdet(device_t, device_t);
     91 int uhidev_detach(device_t, int);
     92 int uhidev_activate(device_t, enum devact);
     93 extern struct cfdriver uhidev_cd;
     94 CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
     95     uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
     96 
     97 int
     98 uhidev_match(device_t parent, cfdata_t match, void *aux)
     99 {
    100 	struct usbif_attach_arg *uiaa = aux;
    101 
    102 	/* Game controllers in "XInput" mode */
    103 	if (USBIF_IS_XINPUT(uiaa))
    104 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    105 	/* Xbox One controllers */
    106 	if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0)
    107 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    108 
    109 	if (uiaa->uiaa_class != UICLASS_HID)
    110 		return UMATCH_NONE;
    111 	if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE)
    112 		return UMATCH_NONE;
    113 	return UMATCH_IFACECLASS_GENERIC;
    114 }
    115 
    116 void
    117 uhidev_attach(device_t parent, device_t self, void *aux)
    118 {
    119 	struct uhidev_softc *sc = device_private(self);
    120 	struct usbif_attach_arg *uiaa = aux;
    121 	struct usbd_interface *iface = uiaa->uiaa_iface;
    122 	usb_interface_descriptor_t *id;
    123 	usb_endpoint_descriptor_t *ed;
    124 	struct uhidev_attach_arg uha;
    125 	device_t dev;
    126 	struct uhidev *csc;
    127 	int maxinpktsize, size, nrepid, repid, repsz;
    128 	int *repsizes;
    129 	int i;
    130 	void *desc;
    131 	const void *descptr;
    132 	usbd_status err;
    133 	char *devinfop;
    134 	int locs[UHIDBUSCF_NLOCS];
    135 
    136 	sc->sc_dev = self;
    137 	sc->sc_udev = uiaa->uiaa_device;
    138 	sc->sc_iface = iface;
    139 
    140 	aprint_naive("\n");
    141 	aprint_normal("\n");
    142 
    143 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    144 
    145 	id = usbd_get_interface_descriptor(iface);
    146 
    147 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    148 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    149 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    150 	usbd_devinfo_free(devinfop);
    151 
    152 	if (!pmf_device_register(self, NULL, NULL))
    153 		aprint_error_dev(self, "couldn't establish power handler\n");
    154 
    155 	(void)usbd_set_idle(iface, 0, 0);
    156 #if 0
    157 
    158 	if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0 &&
    159 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
    160 		(void)usbd_set_protocol(iface, 1);
    161 #endif
    162 
    163 	maxinpktsize = 0;
    164 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
    165 	for (i = 0; i < id->bNumEndpoints; i++) {
    166 		ed = usbd_interface2endpoint_descriptor(iface, i);
    167 		if (ed == NULL) {
    168 			aprint_error_dev(self,
    169 			    "could not read endpoint descriptor\n");
    170 			sc->sc_dying = 1;
    171 			return;
    172 		}
    173 
    174 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    175 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    176 		    " bInterval=%d\n",
    177 		    ed->bLength, ed->bDescriptorType,
    178 		    ed->bEndpointAddress & UE_ADDR,
    179 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    180 		    ed->bmAttributes & UE_XFERTYPE,
    181 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
    182 
    183 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    184 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    185 			maxinpktsize = UGETW(ed->wMaxPacketSize);
    186 			sc->sc_iep_addr = ed->bEndpointAddress;
    187 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    188 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    189 			sc->sc_oep_addr = ed->bEndpointAddress;
    190 		} else {
    191 			aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
    192 		}
    193 	}
    194 
    195 	/*
    196 	 * Check that we found an input interrupt endpoint. The output interrupt
    197 	 * endpoint is optional
    198 	 */
    199 	if (sc->sc_iep_addr == -1) {
    200 		aprint_error_dev(self, "no input interrupt endpoint\n");
    201 		sc->sc_dying = 1;
    202 		return;
    203 	}
    204 
    205 	/* XXX need to extend this */
    206 	descptr = NULL;
    207 	if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
    208 		static uByte reportbuf[] = {2, 2, 2};
    209 
    210 		/* The report descriptor for the Wacom Graphire is broken. */
    211 		switch (uiaa->uiaa_product) {
    212 		case USB_PRODUCT_WACOM_GRAPHIRE:
    213 		case USB_PRODUCT_WACOM_GRAPHIRE2:
    214 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
    215 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
    216 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
    217 			/*
    218 			 * The Graphire3 needs 0x0202 to be written to
    219 			 * feature report ID 2 before it'll start
    220 			 * returning digitizer data.
    221 			 */
    222 			usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
    223 			    &reportbuf, sizeof(reportbuf));
    224 
    225 			size = sizeof(uhid_graphire3_4x5_report_descr);
    226 			descptr = uhid_graphire3_4x5_report_descr;
    227 			break;
    228 		default:
    229 			/* Keep descriptor */
    230 			break;
    231 		}
    232 	}
    233 	if (USBIF_IS_XINPUT(uiaa)) {
    234 		size = sizeof(uhid_xinput_report_descr);
    235 		descptr = uhid_xinput_report_descr;
    236 	}
    237 	if (USBIF_IS_X1INPUT(uiaa)) {
    238 		sc->sc_flags |= UHIDEV_F_XB1;
    239 		size = sizeof(uhid_x1input_report_descr);
    240 		descptr = uhid_x1input_report_descr;
    241 	}
    242 
    243 	if (descptr) {
    244 		desc = kmem_alloc(size, KM_SLEEP);
    245 		err = USBD_NORMAL_COMPLETION;
    246 		memcpy(desc, descptr, size);
    247 	} else {
    248 		desc = NULL;
    249 		err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size);
    250 	}
    251 	if (err) {
    252 		aprint_error_dev(self, "no report descriptor\n");
    253 		sc->sc_dying = 1;
    254 		return;
    255 	}
    256 
    257 	if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN &&
    258 	    uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) {
    259 		static uByte reportbuf[] = { 1 };
    260 		/*
    261 		 *  This device was sold by Konami with its ParaParaParadise
    262 		 *  game for PlayStation2.  It needs to be "turned on"
    263 		 *  before it will send any reports.
    264 		 */
    265 
    266 		usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0,
    267 		    &reportbuf, sizeof(reportbuf));
    268 	}
    269 
    270 	if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH &&
    271 	    uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
    272 		uint8_t *data = desc;
    273 		/*
    274 		 * This device has a odd USAGE_MINIMUM value that would
    275 		 * cause the multimedia keys to have their usage number
    276 		 * shifted up one usage.  Adjust so the usages are sane.
    277 		 */
    278 
    279 		if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
    280 		    data[0x58] == 0x2a && data[0x59] == 0x8c)
    281 			data[0x57] = 0x00;
    282 	}
    283 
    284 	/*
    285 	 * Enable the Six Axis and DualShock 3 controllers.
    286 	 * See http://ps3.jim.sh/sixaxis/usb/
    287 	 */
    288 	if (uiaa->uiaa_vendor == USB_VENDOR_SONY &&
    289 	    uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) {
    290 		usb_device_request_t req;
    291 		char data[17];
    292 		int actlen;
    293 
    294 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
    295 		req.bRequest = 1;
    296 		USETW(req.wValue, 0x3f2);
    297 		USETW(req.wIndex, 0);
    298 		USETW(req.wLength, sizeof(data));
    299 
    300 		usbd_do_request_flags(sc->sc_udev, &req, data,
    301 			USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    302 	}
    303 
    304 	sc->sc_repdesc = desc;
    305 	sc->sc_repdesc_size = size;
    306 
    307 	uha.uiaa = uiaa;
    308 	nrepid = uhidev_maxrepid(desc, size);
    309 	if (nrepid < 0)
    310 		return;
    311 	if (nrepid > 0)
    312 		aprint_normal_dev(self, "%d report ids\n", nrepid);
    313 	nrepid++;
    314 	repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP);
    315 	sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(device_t),
    316 	    KM_SLEEP);
    317 
    318 	/* Just request max packet size for the interrupt pipe */
    319 	sc->sc_isize = maxinpktsize;
    320 	sc->sc_nrepid = nrepid;
    321 
    322 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    323 
    324 	for (repid = 0; repid < nrepid; repid++) {
    325 		repsz = hid_report_size(desc, size, hid_input, repid);
    326 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    327 		repsizes[repid] = repsz;
    328 	}
    329 
    330 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    331 
    332 	uha.parent = sc;
    333 	for (repid = 0; repid < nrepid; repid++) {
    334 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    335 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    336 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    337 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    338 			;	/* already NULL in sc->sc_subdevs[repid] */
    339 		} else {
    340 			uha.reportid = repid;
    341 			locs[UHIDBUSCF_REPORTID] = repid;
    342 
    343 			dev = config_found_sm_loc(self,
    344 				"uhidbus", locs, &uha,
    345 				uhidevprint, config_stdsubmatch);
    346 			sc->sc_subdevs[repid] = dev;
    347 			if (dev != NULL) {
    348 				csc = device_private(dev);
    349 				csc->sc_in_rep_size = repsizes[repid];
    350 #ifdef DIAGNOSTIC
    351 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    352 					 repid, dev));
    353 				if (csc->sc_intr == NULL) {
    354 					kmem_free(repsizes,
    355 					    nrepid * sizeof(*repsizes));
    356 					aprint_error_dev(self,
    357 					    "sc_intr == NULL\n");
    358 					return;
    359 				}
    360 #endif
    361 				rnd_attach_source(&csc->rnd_source,
    362 						  device_xname(dev),
    363 						  RND_TYPE_TTY,
    364 						  RND_FLAG_DEFAULT);
    365 			}
    366 		}
    367 	}
    368 	kmem_free(repsizes, nrepid * sizeof(*repsizes));
    369 
    370 	return;
    371 }
    372 
    373 int
    374 uhidev_maxrepid(void *buf, int len)
    375 {
    376 	struct hid_data *d;
    377 	struct hid_item h;
    378 	int maxid;
    379 
    380 	maxid = -1;
    381 	h.report_ID = 0;
    382 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    383 		if (h.report_ID > maxid)
    384 			maxid = h.report_ID;
    385 	hid_end_parse(d);
    386 	return maxid;
    387 }
    388 
    389 int
    390 uhidevprint(void *aux, const char *pnp)
    391 {
    392 	struct uhidev_attach_arg *uha = aux;
    393 
    394 	if (pnp)
    395 		aprint_normal("uhid at %s", pnp);
    396 	if (uha->reportid != 0)
    397 		aprint_normal(" reportid %d", uha->reportid);
    398 	return UNCONF;
    399 }
    400 
    401 int
    402 uhidev_activate(device_t self, enum devact act)
    403 {
    404 	struct uhidev_softc *sc = device_private(self);
    405 
    406 	switch (act) {
    407 	case DVACT_DEACTIVATE:
    408 		sc->sc_dying = 1;
    409 		return 0;
    410 	default:
    411 		return EOPNOTSUPP;
    412 	}
    413 }
    414 
    415 void
    416 uhidev_childdet(device_t self, device_t child)
    417 {
    418 	int i;
    419 	struct uhidev_softc *sc = device_private(self);
    420 
    421 	for (i = 0; i < sc->sc_nrepid; i++) {
    422 		if (sc->sc_subdevs[i] == child)
    423 			break;
    424 	}
    425 	KASSERT(i < sc->sc_nrepid);
    426 	sc->sc_subdevs[i] = NULL;
    427 }
    428 
    429 int
    430 uhidev_detach(device_t self, int flags)
    431 {
    432 	struct uhidev_softc *sc = device_private(self);
    433 	int i, rv;
    434 	struct uhidev *csc;
    435 
    436 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    437 
    438 	sc->sc_dying = 1;
    439 	if (sc->sc_ipipe != NULL)
    440 		usbd_abort_pipe(sc->sc_ipipe);
    441 
    442 	if (sc->sc_repdesc != NULL)
    443 		kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
    444 
    445 	rv = 0;
    446 	for (i = 0; i < sc->sc_nrepid; i++) {
    447 		if (sc->sc_subdevs[i] != NULL) {
    448 			csc = device_private(sc->sc_subdevs[i]);
    449 			rnd_detach_source(&csc->rnd_source);
    450 			rv |= config_detach(sc->sc_subdevs[i], flags);
    451 		}
    452 	}
    453 
    454 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    455 
    456 	pmf_device_deregister(self);
    457 	mutex_destroy(&sc->sc_lock);
    458 
    459 	return rv;
    460 }
    461 
    462 void
    463 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    464 {
    465 	struct uhidev_softc *sc = addr;
    466 	device_t cdev;
    467 	struct uhidev *scd;
    468 	u_char *p;
    469 	u_int rep;
    470 	uint32_t cc;
    471 
    472 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    473 
    474 #ifdef UHIDEV_DEBUG
    475 	if (uhidevdebug > 5) {
    476 		uint32_t i;
    477 
    478 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    479 		DPRINTF(("uhidev_intr: data ="));
    480 		for (i = 0; i < cc; i++)
    481 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    482 		DPRINTF(("\n"));
    483 	}
    484 #endif
    485 
    486 	if (status == USBD_CANCELLED)
    487 		return;
    488 
    489 	if (status != USBD_NORMAL_COMPLETION) {
    490 		DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
    491 			 status));
    492 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    493 		return;
    494 	}
    495 
    496 	p = sc->sc_ibuf;
    497 	if (sc->sc_nrepid != 1)
    498 		rep = *p++, cc--;
    499 	else
    500 		rep = 0;
    501 	if (rep >= sc->sc_nrepid) {
    502 		printf("uhidev_intr: bad repid %d\n", rep);
    503 		return;
    504 	}
    505 	cdev = sc->sc_subdevs[rep];
    506 	if (!cdev)
    507 		return;
    508 	scd = device_private(cdev);
    509 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    510 		    rep, scd, scd ? scd->sc_state : 0));
    511 	if (!(scd->sc_state & UHIDEV_OPEN))
    512 		return;
    513 #ifdef UHIDEV_DEBUG
    514 	if (scd->sc_in_rep_size != cc) {
    515 		DPRINTF(("%s: expected %d bytes, got %d\n",
    516 		       device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
    517 	}
    518 #endif
    519 	if (cc == 0) {
    520 		DPRINTF(("%s: 0-length input ignored\n",
    521 			device_xname(sc->sc_dev)));
    522 		return;
    523 	}
    524 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    525 	scd->sc_intr(scd, p, cc);
    526 }
    527 
    528 void
    529 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    530 {
    531 	*desc = sc->sc_repdesc;
    532 	*size = sc->sc_repdesc_size;
    533 }
    534 
    535 int
    536 uhidev_open(struct uhidev *scd)
    537 {
    538 	struct uhidev_softc *sc = scd->sc_parent;
    539 	usbd_status err;
    540 	int error;
    541 
    542 	DPRINTF(("uhidev_open: open pipe, state=%d\n", scd->sc_state));
    543 
    544 	mutex_enter(&sc->sc_lock);
    545 	if (scd->sc_state & UHIDEV_OPEN) {
    546 		mutex_exit(&sc->sc_lock);
    547 		return EBUSY;
    548 	}
    549 	scd->sc_state |= UHIDEV_OPEN;
    550 	if (sc->sc_refcnt++) {
    551 		mutex_exit(&sc->sc_lock);
    552 		return 0;
    553 	}
    554 	mutex_exit(&sc->sc_lock);
    555 
    556 	if (sc->sc_isize == 0)
    557 		return 0;
    558 
    559 	sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    560 
    561 	/* Set up input interrupt pipe. */
    562 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    563 		 sc->sc_iep_addr));
    564 
    565 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    566 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    567 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    568 	if (err != USBD_NORMAL_COMPLETION) {
    569 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    570 		    "error=%d\n", err));
    571 		error = EIO;
    572 		goto out1;
    573 	}
    574 
    575 	/*
    576 	 * Set up output interrupt pipe if an output interrupt endpoint
    577 	 * exists.
    578 	 */
    579 	if (sc->sc_oep_addr != -1) {
    580 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    581 
    582 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    583 		    0, &sc->sc_opipe);
    584 
    585 		if (err != USBD_NORMAL_COMPLETION) {
    586 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    587 			    "error=%d\n", err));
    588 			error = EIO;
    589 			goto out2;
    590 		}
    591 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    592 
    593 		error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0,
    594 		    &sc->sc_oxfer);
    595 		if (error) {
    596 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    597 			goto out3;
    598 		}
    599 
    600 		if (sc->sc_flags & UHIDEV_F_XB1) {
    601 			uint8_t init_data[] = { 0x05, 0x20 };
    602 			int init_data_len = sizeof(init_data);
    603 			err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    604 			    USBD_NO_TIMEOUT, init_data, &init_data_len);
    605 			if (err != USBD_NORMAL_COMPLETION) {
    606 				DPRINTF(("uhidev_open: xb1 init failed, "
    607 				    "error=%d\n", err));
    608 				error = EIO;
    609 				goto out4;
    610 			}
    611 		}
    612 	}
    613 
    614 	return 0;
    615 out4:
    616 	/* Free output xfer */
    617 	if (sc->sc_oxfer != NULL)
    618 		usbd_destroy_xfer(sc->sc_oxfer);
    619 out3:
    620 	/* Abort output pipe */
    621 	usbd_close_pipe(sc->sc_opipe);
    622 out2:
    623 	/* Abort input pipe */
    624 	usbd_close_pipe(sc->sc_ipipe);
    625 out1:
    626 	DPRINTF(("uhidev_open: failed in someway"));
    627 	kmem_free(sc->sc_ibuf, sc->sc_isize);
    628 	mutex_enter(&sc->sc_lock);
    629 	scd->sc_state &= ~UHIDEV_OPEN;
    630 	sc->sc_refcnt = 0;
    631 	sc->sc_ibuf = NULL;
    632 	sc->sc_ipipe = NULL;
    633 	sc->sc_opipe = NULL;
    634 	sc->sc_oxfer = NULL;
    635 	mutex_exit(&sc->sc_lock);
    636 	return error;
    637 }
    638 
    639 void
    640 uhidev_stop(struct uhidev *scd)
    641 {
    642 	struct uhidev_softc *sc = scd->sc_parent;
    643 
    644 	/* Disable interrupts. */
    645 	if (sc->sc_opipe != NULL) {
    646 		usbd_abort_pipe(sc->sc_opipe);
    647 		usbd_close_pipe(sc->sc_opipe);
    648 		sc->sc_opipe = NULL;
    649 	}
    650 
    651 	if (sc->sc_ipipe != NULL) {
    652 		usbd_abort_pipe(sc->sc_ipipe);
    653 		usbd_close_pipe(sc->sc_ipipe);
    654 		sc->sc_ipipe = NULL;
    655 	}
    656 
    657 	if (sc->sc_ibuf != NULL) {
    658 		kmem_free(sc->sc_ibuf, sc->sc_isize);
    659 		sc->sc_ibuf = NULL;
    660 	}
    661 }
    662 
    663 void
    664 uhidev_close(struct uhidev *scd)
    665 {
    666 	struct uhidev_softc *sc = scd->sc_parent;
    667 
    668 	mutex_enter(&sc->sc_lock);
    669 	if (!(scd->sc_state & UHIDEV_OPEN)) {
    670 		mutex_exit(&sc->sc_lock);
    671 		return;
    672 	}
    673 	scd->sc_state &= ~UHIDEV_OPEN;
    674 	if (--sc->sc_refcnt) {
    675 		mutex_exit(&sc->sc_lock);
    676 		return;
    677 	}
    678 	mutex_exit(&sc->sc_lock);
    679 
    680 	DPRINTF(("uhidev_close: close pipe\n"));
    681 
    682 	if (sc->sc_oxfer != NULL) {
    683 		usbd_destroy_xfer(sc->sc_oxfer);
    684 		sc->sc_oxfer = NULL;
    685 	}
    686 
    687 
    688 	/* Possibly redundant, but properly handled */
    689 	uhidev_stop(scd);
    690 }
    691 
    692 usbd_status
    693 uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    694 {
    695 	char *buf;
    696 	usbd_status retstat;
    697 
    698 	if (scd->sc_report_id == 0)
    699 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    700 				       scd->sc_report_id, data, len);
    701 
    702 	buf = kmem_alloc(len + 1, KM_SLEEP);
    703 	buf[0] = scd->sc_report_id;
    704 	memcpy(buf+1, data, len);
    705 
    706 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    707 				  scd->sc_report_id, buf, len + 1);
    708 
    709 	kmem_free(buf, len + 1);
    710 
    711 	return retstat;
    712 }
    713 
    714 usbd_status
    715 uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    716 {
    717 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    718 			       scd->sc_report_id, data, len);
    719 }
    720 
    721 usbd_status
    722 uhidev_write(struct uhidev_softc *sc, void *data, int len)
    723 {
    724 
    725 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    726 
    727 	if (sc->sc_opipe == NULL)
    728 		return USBD_INVAL;
    729 
    730 #ifdef UHIDEV_DEBUG
    731 	if (uhidevdebug > 50) {
    732 
    733 		uint32_t i;
    734 		uint8_t *d = data;
    735 
    736 		DPRINTF(("uhidev_write: data ="));
    737 		for (i = 0; i < len; i++)
    738 			DPRINTF((" %02x", d[i]));
    739 		DPRINTF(("\n"));
    740 	}
    741 #endif
    742 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, USBD_NO_TIMEOUT,
    743 	    data, &len);
    744 }
    745