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