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