1 /* $NetBSD: uhidev.c,v 1.67 2016/04/30 18:40:26 jakllsch 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@augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg@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.67 2016/04/30 18:40:26 jakllsch 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 if (desc == NULL) 246 err = USBD_NOMEM; 247 else { 248 err = USBD_NORMAL_COMPLETION; 249 memcpy(desc, descptr, size); 250 } 251 } else { 252 desc = NULL; 253 err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size); 254 } 255 if (err) { 256 aprint_error_dev(self, "no report descriptor\n"); 257 sc->sc_dying = 1; 258 return; 259 } 260 261 if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN && 262 uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) { 263 static uByte reportbuf[] = { 1 }; 264 /* 265 * This device was sold by Konami with its ParaParaParadise 266 * game for PlayStation2. It needs to be "turned on" 267 * before it will send any reports. 268 */ 269 270 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0, 271 &reportbuf, sizeof(reportbuf)); 272 } 273 274 if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH && 275 uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) { 276 uint8_t *data = desc; 277 /* 278 * This device has a odd USAGE_MINIMUM value that would 279 * cause the multimedia keys to have their usage number 280 * shifted up one usage. Adjust so the usages are sane. 281 */ 282 283 if (data[0x56] == 0x19 && data[0x57] == 0x01 && 284 data[0x58] == 0x2a && data[0x59] == 0x8c) 285 data[0x57] = 0x00; 286 } 287 288 /* 289 * Enable the Six Axis and DualShock 3 controllers. 290 * See http://ps3.jim.sh/sixaxis/usb/ 291 */ 292 if (uiaa->uiaa_vendor == USB_VENDOR_SONY && 293 uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) { 294 usb_device_request_t req; 295 char data[17]; 296 int actlen; 297 298 req.bmRequestType = UT_READ_CLASS_INTERFACE; 299 req.bRequest = 1; 300 USETW(req.wValue, 0x3f2); 301 USETW(req.wIndex, 0); 302 USETW(req.wLength, sizeof(data)); 303 304 usbd_do_request_flags(sc->sc_udev, &req, data, 305 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 306 } 307 308 sc->sc_repdesc = desc; 309 sc->sc_repdesc_size = size; 310 311 uha.uiaa = uiaa; 312 nrepid = uhidev_maxrepid(desc, size); 313 if (nrepid < 0) 314 return; 315 if (nrepid > 0) 316 aprint_normal_dev(self, "%d report ids\n", nrepid); 317 nrepid++; 318 repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP); 319 if (repsizes == NULL) 320 goto nomem; 321 sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(device_t), 322 KM_SLEEP); 323 if (sc->sc_subdevs == NULL) { 324 kmem_free(repsizes, nrepid * sizeof(*repsizes)); 325 nomem: 326 aprint_error_dev(self, "no memory\n"); 327 return; 328 } 329 330 /* Just request max packet size for the interrupt pipe */ 331 sc->sc_isize = maxinpktsize; 332 sc->sc_nrepid = nrepid; 333 334 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 335 sc->sc_dev); 336 337 for (repid = 0; repid < nrepid; repid++) { 338 repsz = hid_report_size(desc, size, hid_input, repid); 339 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz)); 340 repsizes[repid] = repsz; 341 } 342 343 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize)); 344 345 uha.parent = sc; 346 for (repid = 0; repid < nrepid; repid++) { 347 DPRINTF(("uhidev_match: try repid=%d\n", repid)); 348 if (hid_report_size(desc, size, hid_input, repid) == 0 && 349 hid_report_size(desc, size, hid_output, repid) == 0 && 350 hid_report_size(desc, size, hid_feature, repid) == 0) { 351 ; /* already NULL in sc->sc_subdevs[repid] */ 352 } else { 353 uha.reportid = repid; 354 locs[UHIDBUSCF_REPORTID] = repid; 355 356 dev = config_found_sm_loc(self, 357 "uhidbus", locs, &uha, 358 uhidevprint, config_stdsubmatch); 359 sc->sc_subdevs[repid] = dev; 360 if (dev != NULL) { 361 csc = device_private(dev); 362 csc->sc_in_rep_size = repsizes[repid]; 363 #ifdef DIAGNOSTIC 364 DPRINTF(("uhidev_match: repid=%d dev=%p\n", 365 repid, dev)); 366 if (csc->sc_intr == NULL) { 367 kmem_free(repsizes, 368 nrepid * sizeof(*repsizes)); 369 aprint_error_dev(self, 370 "sc_intr == NULL\n"); 371 return; 372 } 373 #endif 374 rnd_attach_source(&csc->rnd_source, 375 device_xname(dev), 376 RND_TYPE_TTY, 377 RND_FLAG_DEFAULT); 378 } 379 } 380 } 381 kmem_free(repsizes, nrepid * sizeof(*repsizes)); 382 383 return; 384 } 385 386 int 387 uhidev_maxrepid(void *buf, int len) 388 { 389 struct hid_data *d; 390 struct hid_item h; 391 int maxid; 392 393 maxid = -1; 394 h.report_ID = 0; 395 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 396 if (h.report_ID > maxid) 397 maxid = h.report_ID; 398 hid_end_parse(d); 399 return maxid; 400 } 401 402 int 403 uhidevprint(void *aux, const char *pnp) 404 { 405 struct uhidev_attach_arg *uha = aux; 406 407 if (pnp) 408 aprint_normal("uhid at %s", pnp); 409 if (uha->reportid != 0) 410 aprint_normal(" reportid %d", uha->reportid); 411 return UNCONF; 412 } 413 414 int 415 uhidev_activate(device_t self, enum devact act) 416 { 417 struct uhidev_softc *sc = device_private(self); 418 419 switch (act) { 420 case DVACT_DEACTIVATE: 421 sc->sc_dying = 1; 422 return 0; 423 default: 424 return EOPNOTSUPP; 425 } 426 } 427 428 void 429 uhidev_childdet(device_t self, device_t child) 430 { 431 int i; 432 struct uhidev_softc *sc = device_private(self); 433 434 for (i = 0; i < sc->sc_nrepid; i++) { 435 if (sc->sc_subdevs[i] == child) 436 break; 437 } 438 KASSERT(i < sc->sc_nrepid); 439 sc->sc_subdevs[i] = NULL; 440 } 441 442 int 443 uhidev_detach(device_t self, int flags) 444 { 445 struct uhidev_softc *sc = device_private(self); 446 int i, rv; 447 struct uhidev *csc; 448 449 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 450 451 sc->sc_dying = 1; 452 if (sc->sc_ipipe != NULL) 453 usbd_abort_pipe(sc->sc_ipipe); 454 455 if (sc->sc_repdesc != NULL) 456 kmem_free(sc->sc_repdesc, sc->sc_repdesc_size); 457 458 rv = 0; 459 for (i = 0; i < sc->sc_nrepid; i++) { 460 if (sc->sc_subdevs[i] != NULL) { 461 csc = device_private(sc->sc_subdevs[i]); 462 rnd_detach_source(&csc->rnd_source); 463 rv |= config_detach(sc->sc_subdevs[i], flags); 464 } 465 } 466 467 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 468 sc->sc_dev); 469 470 pmf_device_deregister(self); 471 mutex_destroy(&sc->sc_lock); 472 473 return rv; 474 } 475 476 void 477 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 478 { 479 struct uhidev_softc *sc = addr; 480 device_t cdev; 481 struct uhidev *scd; 482 u_char *p; 483 u_int rep; 484 uint32_t cc; 485 486 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 487 488 #ifdef UHIDEV_DEBUG 489 if (uhidevdebug > 5) { 490 uint32_t i; 491 492 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 493 DPRINTF(("uhidev_intr: data =")); 494 for (i = 0; i < cc; i++) 495 DPRINTF((" %02x", sc->sc_ibuf[i])); 496 DPRINTF(("\n")); 497 } 498 #endif 499 500 if (status == USBD_CANCELLED) 501 return; 502 503 if (status != USBD_NORMAL_COMPLETION) { 504 DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev), 505 status)); 506 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 507 return; 508 } 509 510 p = sc->sc_ibuf; 511 if (sc->sc_nrepid != 1) 512 rep = *p++, cc--; 513 else 514 rep = 0; 515 if (rep >= sc->sc_nrepid) { 516 printf("uhidev_intr: bad repid %d\n", rep); 517 return; 518 } 519 cdev = sc->sc_subdevs[rep]; 520 if (!cdev) 521 return; 522 scd = device_private(cdev); 523 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n", 524 rep, scd, scd ? scd->sc_state : 0)); 525 if (!(scd->sc_state & UHIDEV_OPEN)) 526 return; 527 #ifdef UHIDEV_DEBUG 528 if (scd->sc_in_rep_size != cc) { 529 DPRINTF(("%s: expected %d bytes, got %d\n", 530 device_xname(sc->sc_dev), scd->sc_in_rep_size, cc)); 531 } 532 #endif 533 if (cc == 0) { 534 DPRINTF(("%s: 0-length input ignored\n", 535 device_xname(sc->sc_dev))); 536 return; 537 } 538 rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf)); 539 scd->sc_intr(scd, p, cc); 540 } 541 542 void 543 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size) 544 { 545 *desc = sc->sc_repdesc; 546 *size = sc->sc_repdesc_size; 547 } 548 549 int 550 uhidev_open(struct uhidev *scd) 551 { 552 struct uhidev_softc *sc = scd->sc_parent; 553 usbd_status err; 554 int error; 555 556 DPRINTF(("uhidev_open: open pipe, state=%d\n", scd->sc_state)); 557 558 mutex_enter(&sc->sc_lock); 559 if (scd->sc_state & UHIDEV_OPEN) { 560 mutex_exit(&sc->sc_lock); 561 return EBUSY; 562 } 563 scd->sc_state |= UHIDEV_OPEN; 564 if (sc->sc_refcnt++) { 565 mutex_exit(&sc->sc_lock); 566 return 0; 567 } 568 mutex_exit(&sc->sc_lock); 569 570 if (sc->sc_isize == 0) 571 return 0; 572 573 sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP); 574 575 /* Set up input interrupt pipe. */ 576 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize, 577 sc->sc_iep_addr)); 578 579 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr, 580 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf, 581 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 582 if (err != USBD_NORMAL_COMPLETION) { 583 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 584 "error=%d\n", err)); 585 error = EIO; 586 goto out1; 587 } 588 589 /* 590 * Set up output interrupt pipe if an output interrupt endpoint 591 * exists. 592 */ 593 if (sc->sc_oep_addr != -1) { 594 DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr)); 595 596 err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr, 597 0, &sc->sc_opipe); 598 599 if (err != USBD_NORMAL_COMPLETION) { 600 DPRINTF(("uhidev_open: usbd_open_pipe failed, " 601 "error=%d\n", err)); 602 error = EIO; 603 goto out2; 604 } 605 DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe)); 606 607 error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0, 608 &sc->sc_oxfer); 609 if (error) { 610 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 611 goto out3; 612 } 613 614 if (sc->sc_flags & UHIDEV_F_XB1) { 615 uint8_t init_data[] = { 0x05, 0x20 }; 616 int init_data_len = sizeof(init_data); 617 err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, 618 USBD_NO_TIMEOUT, init_data, &init_data_len); 619 if (err != USBD_NORMAL_COMPLETION) { 620 DPRINTF(("uhidev_open: xb1 init failed, " 621 "error=%d\n", err)); 622 error = EIO; 623 goto out4; 624 } 625 } 626 } 627 628 return 0; 629 out4: 630 /* Free output xfer */ 631 if (sc->sc_oxfer != NULL) 632 usbd_destroy_xfer(sc->sc_oxfer); 633 out3: 634 /* Abort output pipe */ 635 usbd_close_pipe(sc->sc_opipe); 636 out2: 637 /* Abort input pipe */ 638 usbd_close_pipe(sc->sc_ipipe); 639 out1: 640 DPRINTF(("uhidev_open: failed in someway")); 641 kmem_free(sc->sc_ibuf, sc->sc_isize); 642 mutex_enter(&sc->sc_lock); 643 scd->sc_state &= ~UHIDEV_OPEN; 644 sc->sc_refcnt = 0; 645 sc->sc_ibuf = NULL; 646 sc->sc_ipipe = NULL; 647 sc->sc_opipe = NULL; 648 sc->sc_oxfer = NULL; 649 mutex_exit(&sc->sc_lock); 650 return error; 651 } 652 653 void 654 uhidev_stop(struct uhidev *scd) 655 { 656 struct uhidev_softc *sc = scd->sc_parent; 657 658 /* Disable interrupts. */ 659 if (sc->sc_opipe != NULL) { 660 usbd_abort_pipe(sc->sc_opipe); 661 usbd_close_pipe(sc->sc_opipe); 662 sc->sc_opipe = NULL; 663 } 664 665 if (sc->sc_ipipe != NULL) { 666 usbd_abort_pipe(sc->sc_ipipe); 667 usbd_close_pipe(sc->sc_ipipe); 668 sc->sc_ipipe = NULL; 669 } 670 671 if (sc->sc_ibuf != NULL) { 672 kmem_free(sc->sc_ibuf, sc->sc_isize); 673 sc->sc_ibuf = NULL; 674 } 675 } 676 677 void 678 uhidev_close(struct uhidev *scd) 679 { 680 struct uhidev_softc *sc = scd->sc_parent; 681 682 mutex_enter(&sc->sc_lock); 683 if (!(scd->sc_state & UHIDEV_OPEN)) { 684 mutex_exit(&sc->sc_lock); 685 return; 686 } 687 scd->sc_state &= ~UHIDEV_OPEN; 688 if (--sc->sc_refcnt) { 689 mutex_exit(&sc->sc_lock); 690 return; 691 } 692 mutex_exit(&sc->sc_lock); 693 694 DPRINTF(("uhidev_close: close pipe\n")); 695 696 if (sc->sc_oxfer != NULL) { 697 usbd_destroy_xfer(sc->sc_oxfer); 698 sc->sc_oxfer = NULL; 699 } 700 701 702 /* Possibly redundant, but properly handled */ 703 uhidev_stop(scd); 704 } 705 706 usbd_status 707 uhidev_set_report(struct uhidev *scd, int type, void *data, int len) 708 { 709 char *buf; 710 usbd_status retstat; 711 712 if (scd->sc_report_id == 0) 713 return usbd_set_report(scd->sc_parent->sc_iface, type, 714 scd->sc_report_id, data, len); 715 716 buf = kmem_alloc(len + 1, KM_SLEEP); 717 buf[0] = scd->sc_report_id; 718 memcpy(buf+1, data, len); 719 720 retstat = usbd_set_report(scd->sc_parent->sc_iface, type, 721 scd->sc_report_id, buf, len + 1); 722 723 kmem_free(buf, len + 1); 724 725 return retstat; 726 } 727 728 usbd_status 729 uhidev_get_report(struct uhidev *scd, int type, void *data, int len) 730 { 731 return usbd_get_report(scd->sc_parent->sc_iface, type, 732 scd->sc_report_id, data, len); 733 } 734 735 usbd_status 736 uhidev_write(struct uhidev_softc *sc, void *data, int len) 737 { 738 739 DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len)); 740 741 if (sc->sc_opipe == NULL) 742 return USBD_INVAL; 743 744 #ifdef UHIDEV_DEBUG 745 if (uhidevdebug > 50) { 746 747 uint32_t i; 748 uint8_t *d = data; 749 750 DPRINTF(("uhidev_write: data =")); 751 for (i = 0; i < len; i++) 752 DPRINTF((" %02x", d[i])); 753 DPRINTF(("\n")); 754 } 755 #endif 756 return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, USBD_NO_TIMEOUT, 757 data, &len); 758 } 759