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