1 /* $OpenBSD: uhidev.c,v 1.102 2021/11/22 03:30:20 jsg Exp $ */ 2 /* $NetBSD: uhidev.c,v 1.14 2003/03/11 16:44:00 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * HID spec: https://www.usb.org/sites/default/files/hid1_11.pdf 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/signalvar.h> 43 #include <sys/device.h> 44 #include <sys/ioctl.h> 45 #include <sys/conf.h> 46 47 #include <machine/bus.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/usbdivar.h> 56 #include <dev/usb/usb_mem.h> 57 #include <dev/usb/usb_quirks.h> 58 59 #include <dev/usb/uhidev.h> 60 61 #ifndef SMALL_KERNEL 62 /* Replacement report descriptors for devices shipped with broken ones */ 63 #include <dev/usb/uhid_rdesc.h> 64 int uhidev_use_rdesc(struct uhidev_softc *, usb_interface_descriptor_t *, 65 int, int, void **, int *); 66 #define UISUBCLASS_XBOX360_CONTROLLER 0x5d 67 #define UIPROTO_XBOX360_GAMEPAD 0x01 68 #endif /* !SMALL_KERNEL */ 69 70 #define DEVNAME(sc) ((sc)->sc_dev.dv_xname) 71 72 #ifdef UHIDEV_DEBUG 73 #define DPRINTF(x) do { if (uhidevdebug) printf x; } while (0) 74 #define DPRINTFN(n,x) do { if (uhidevdebug>(n)) printf x; } while (0) 75 int uhidevdebug = 0; 76 #else 77 #define DPRINTF(x) 78 #define DPRINTFN(n,x) 79 #endif 80 81 struct uhidev_async_info { 82 void (*callback)(void *priv, int id, void *data, int len); 83 void *priv; 84 void *data; 85 int id; 86 }; 87 88 void uhidev_intr(struct usbd_xfer *, void *, usbd_status); 89 90 int uhidev_maxrepid(void *buf, int len); 91 int uhidevprint(void *aux, const char *pnp); 92 93 int uhidev_match(struct device *, void *, void *); 94 void uhidev_attach(struct device *, struct device *, void *); 95 int uhidev_detach(struct device *, int); 96 int uhidev_activate(struct device *, int); 97 98 void uhidev_get_report_async_cb(struct usbd_xfer *, void *, usbd_status); 99 void uhidev_set_report_async_cb(struct usbd_xfer *, void *, usbd_status); 100 101 struct cfdriver uhidev_cd = { 102 NULL, "uhidev", DV_DULL 103 }; 104 105 const struct cfattach uhidev_ca = { 106 sizeof(struct uhidev_softc), uhidev_match, uhidev_attach, 107 uhidev_detach, uhidev_activate, 108 }; 109 110 int 111 uhidev_match(struct device *parent, void *match, void *aux) 112 { 113 struct usb_attach_arg *uaa = aux; 114 usb_interface_descriptor_t *id; 115 116 if (uaa->iface == NULL) 117 return (UMATCH_NONE); 118 id = usbd_get_interface_descriptor(uaa->iface); 119 if (id == NULL) 120 return (UMATCH_NONE); 121 #ifndef SMALL_KERNEL 122 if (id->bInterfaceClass == UICLASS_VENDOR && 123 id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER && 124 id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD) 125 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO); 126 #endif /* !SMALL_KERNEL */ 127 if (id->bInterfaceClass != UICLASS_HID) 128 return (UMATCH_NONE); 129 if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID) 130 return (UMATCH_NONE); 131 132 return (UMATCH_IFACECLASS_GENERIC); 133 } 134 135 void 136 uhidev_attach(struct device *parent, struct device *self, void *aux) 137 { 138 struct uhidev_softc *sc = (struct uhidev_softc *)self; 139 struct usb_attach_arg *uaa = aux; 140 usb_interface_descriptor_t *id; 141 usb_endpoint_descriptor_t *ed; 142 struct uhidev_attach_arg uha; 143 int nrepid, repid, repsz; 144 int i; 145 void *desc = NULL; 146 int size = 0; 147 struct device *dev; 148 149 sc->sc_udev = uaa->device; 150 sc->sc_iface = uaa->iface; 151 sc->sc_ifaceno = uaa->ifaceno; 152 id = usbd_get_interface_descriptor(sc->sc_iface); 153 154 sc->sc_iep_addr = sc->sc_oep_addr = -1; 155 for (i = 0; i < id->bNumEndpoints; i++) { 156 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 157 if (ed == NULL) { 158 printf("%s: could not read endpoint descriptor\n", 159 DEVNAME(sc)); 160 return; 161 } 162 163 DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d " 164 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d" 165 " bInterval=%d\n", 166 ed->bLength, ed->bDescriptorType, 167 ed->bEndpointAddress & UE_ADDR, 168 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out", 169 UE_GET_XFERTYPE(ed->bmAttributes), 170 UGETW(ed->wMaxPacketSize), ed->bInterval)); 171 172 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 173 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 174 sc->sc_iep_addr = ed->bEndpointAddress; 175 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 176 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 177 sc->sc_oep_addr = ed->bEndpointAddress; 178 } else { 179 printf("%s: unexpected endpoint\n", DEVNAME(sc)); 180 return; 181 } 182 } 183 184 /* 185 * Check that we found an input interrupt endpoint. 186 * The output interrupt endpoint is optional 187 */ 188 if (sc->sc_iep_addr == -1) { 189 printf("%s: no input interrupt endpoint\n", DEVNAME(sc)); 190 return; 191 } 192 193 #ifndef SMALL_KERNEL 194 if (uhidev_use_rdesc(sc, id, uaa->vendor, uaa->product, &desc, &size)) 195 return; 196 #endif /* !SMALL_KERNEL */ 197 198 if (desc == NULL) { 199 struct usb_hid_descriptor *hid; 200 201 hid = usbd_get_hid_descriptor(sc->sc_udev, id); 202 if (hid == NULL) { 203 printf("%s: no HID descriptor\n", DEVNAME(sc)); 204 return; 205 } 206 size = UGETW(hid->descrs[0].wDescriptorLength); 207 desc = malloc(size, M_USBDEV, M_NOWAIT); 208 if (desc == NULL) { 209 printf("%s: no memory\n", DEVNAME(sc)); 210 return; 211 } 212 if (usbd_get_report_descriptor(sc->sc_udev, sc->sc_ifaceno, 213 desc, size)) { 214 printf("%s: no report descriptor\n", DEVNAME(sc)); 215 free(desc, M_USBDEV, size); 216 return; 217 } 218 } 219 220 sc->sc_repdesc = desc; 221 sc->sc_repdesc_size = size; 222 223 nrepid = uhidev_maxrepid(desc, size); 224 if (nrepid < 0) 225 return; 226 printf("%s: iclass %d/%d", DEVNAME(sc), id->bInterfaceClass, 227 id->bInterfaceSubClass); 228 if (nrepid > 0) 229 printf(", %d report id%s", nrepid, nrepid > 1 ? "s" : ""); 230 printf("\n"); 231 nrepid++; 232 sc->sc_subdevs = mallocarray(nrepid, sizeof(struct uhidev *), 233 M_USBDEV, M_NOWAIT | M_ZERO); 234 if (sc->sc_subdevs == NULL) { 235 printf("%s: no memory\n", DEVNAME(sc)); 236 return; 237 } 238 sc->sc_nrepid = nrepid; 239 sc->sc_isize = 0; 240 241 for (repid = 0; repid < nrepid; repid++) { 242 repsz = hid_report_size(desc, size, hid_input, repid); 243 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz)); 244 if (repsz > sc->sc_isize) 245 sc->sc_isize = repsz; 246 } 247 sc->sc_isize += (nrepid != 1); /* one byte for the report ID */ 248 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize)); 249 250 uha.uaa = uaa; 251 uha.parent = sc; 252 uha.reportid = __UHIDEV_CLAIM_MULTIPLE_REPORTID; 253 uha.nreports = nrepid; 254 uha.claimed = malloc(nrepid, M_TEMP, M_WAITOK|M_ZERO); 255 256 /* Look for a driver claiming multiple report IDs first. */ 257 dev = config_found_sm(self, &uha, NULL, NULL); 258 if (dev != NULL) { 259 for (repid = 0; repid < nrepid; repid++) { 260 /* 261 * Could already be assigned by uhidev_set_report_dev(). 262 */ 263 if (sc->sc_subdevs[repid] != NULL) 264 continue; 265 266 if (uha.claimed[repid]) 267 sc->sc_subdevs[repid] = (struct uhidev *)dev; 268 } 269 } 270 271 free(uha.claimed, M_TEMP, nrepid); 272 uha.claimed = NULL; 273 274 for (repid = 0; repid < nrepid; repid++) { 275 DPRINTF(("%s: try repid=%d\n", __func__, repid)); 276 if (hid_report_size(desc, size, hid_input, repid) == 0 && 277 hid_report_size(desc, size, hid_output, repid) == 0 && 278 hid_report_size(desc, size, hid_feature, repid) == 0) 279 continue; 280 281 /* Could already be assigned by uhidev_set_report_dev(). */ 282 if (sc->sc_subdevs[repid] != NULL) 283 continue; 284 285 uha.reportid = repid; 286 dev = config_found_sm(self, &uha, uhidevprint, NULL); 287 sc->sc_subdevs[repid] = (struct uhidev *)dev; 288 } 289 } 290 291 #ifndef SMALL_KERNEL 292 int 293 uhidev_use_rdesc(struct uhidev_softc *sc, usb_interface_descriptor_t *id, 294 int vendor, int product, void **descp, int *sizep) 295 { 296 static uByte reportbuf[] = {2, 2}; 297 const void *descptr = NULL; 298 void *desc; 299 int size; 300 301 if (vendor == USB_VENDOR_WACOM) { 302 /* The report descriptor for the Wacom Graphire is broken. */ 303 switch (product) { 304 case USB_PRODUCT_WACOM_GRAPHIRE: 305 size = sizeof(uhid_graphire_report_descr); 306 descptr = uhid_graphire_report_descr; 307 break; 308 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 309 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: 310 uhidev_set_report(sc, UHID_FEATURE_REPORT, 311 2, &reportbuf, sizeof(reportbuf)); 312 size = sizeof(uhid_graphire3_4x5_report_descr); 313 descptr = uhid_graphire3_4x5_report_descr; 314 break; 315 default: 316 break; 317 } 318 } else if ((id->bInterfaceClass == UICLASS_VENDOR && 319 id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER && 320 id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 321 /* The Xbox 360 gamepad has no report descriptor. */ 322 size = sizeof(uhid_xb360gp_report_descr); 323 descptr = uhid_xb360gp_report_descr; 324 } 325 326 if (descptr) { 327 desc = malloc(size, M_USBDEV, M_NOWAIT); 328 if (desc == NULL) 329 return (ENOMEM); 330 331 memcpy(desc, descptr, size); 332 333 *descp = desc; 334 *sizep = size; 335 } 336 337 return (0); 338 } 339 #endif /* !SMALL_KERNEL */ 340 341 int 342 uhidev_maxrepid(void *buf, int len) 343 { 344 struct hid_data *d; 345 struct hid_item h; 346 int maxid; 347 348 maxid = -1; 349 h.report_ID = 0; 350 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 351 if (h.report_ID > maxid) 352 maxid = h.report_ID; 353 hid_end_parse(d); 354 return (maxid); 355 } 356 357 int 358 uhidevprint(void *aux, const char *pnp) 359 { 360 struct uhidev_attach_arg *uha = aux; 361 362 if (pnp) 363 printf("uhid at %s", pnp); 364 if (uha->reportid != 0 && uha->reportid != __UHIDEV_CLAIM_MULTIPLE_REPORTID) 365 printf(" reportid %d", uha->reportid); 366 return (UNCONF); 367 } 368 369 int 370 uhidev_activate(struct device *self, int act) 371 { 372 struct uhidev_softc *sc = (struct uhidev_softc *)self; 373 int i, j, already, rv = 0, r; 374 375 switch (act) { 376 case DVACT_DEACTIVATE: 377 for (i = 0; i < sc->sc_nrepid; i++) { 378 if (sc->sc_subdevs[i] == NULL) 379 continue; 380 381 /* 382 * Only notify devices attached to multiple report ids 383 * once. 384 */ 385 for (already = 0, j = 0; j < i; j++) { 386 if (sc->sc_subdevs[i] == sc->sc_subdevs[j]) { 387 already = 1; 388 break; 389 } 390 } 391 392 if (!already) { 393 r = config_deactivate( 394 &sc->sc_subdevs[i]->sc_dev); 395 if (r && r != EOPNOTSUPP) 396 rv = r; 397 } 398 } 399 usbd_deactivate(sc->sc_udev); 400 break; 401 } 402 return (rv); 403 } 404 405 int 406 uhidev_detach(struct device *self, int flags) 407 { 408 struct uhidev_softc *sc = (struct uhidev_softc *)self; 409 int i, j, rv = 0; 410 411 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 412 413 if (sc->sc_opipe != NULL) { 414 usbd_close_pipe(sc->sc_opipe); 415 sc->sc_opipe = NULL; 416 } 417 418 if (sc->sc_ipipe != NULL) { 419 usbd_close_pipe(sc->sc_ipipe); 420 sc->sc_ipipe = NULL; 421 } 422 423 if (sc->sc_repdesc != NULL) 424 free(sc->sc_repdesc, M_USBDEV, sc->sc_repdesc_size); 425 426 for (i = 0; i < sc->sc_nrepid; i++) { 427 if (sc->sc_subdevs[i] == NULL) 428 continue; 429 430 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags); 431 432 /* 433 * Nullify without detaching any other instances of this device 434 * found on other report ids. 435 */ 436 for (j = i + 1; j < sc->sc_nrepid; j++) { 437 if (sc->sc_subdevs[i] == sc->sc_subdevs[j]) 438 sc->sc_subdevs[j] = NULL; 439 } 440 441 sc->sc_subdevs[i] = NULL; 442 } 443 free(sc->sc_subdevs, M_USBDEV, sc->sc_nrepid * sizeof(struct uhidev *)); 444 445 return (rv); 446 } 447 448 void 449 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 450 { 451 struct uhidev_softc *sc = addr; 452 struct uhidev *scd; 453 u_char *p; 454 u_int rep; 455 u_int32_t cc; 456 457 if (usbd_is_dying(sc->sc_udev)) 458 return; 459 460 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 461 462 #ifdef UHIDEV_DEBUG 463 if (uhidevdebug > 5) { 464 u_int32_t i; 465 466 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 467 DPRINTF(("uhidev_intr: data =")); 468 for (i = 0; i < cc; i++) 469 DPRINTF((" %02x", sc->sc_ibuf[i])); 470 DPRINTF(("\n")); 471 } 472 #endif 473 474 if (status == USBD_CANCELLED || status == USBD_IOERROR) 475 return; 476 477 if (status != USBD_NORMAL_COMPLETION) { 478 DPRINTF(("%s: interrupt status=%d\n", DEVNAME(sc), status)); 479 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 480 return; 481 } 482 483 p = sc->sc_ibuf; 484 if (sc->sc_nrepid != 1) 485 rep = *p++, cc--; 486 else 487 rep = 0; 488 if (rep >= sc->sc_nrepid) { 489 printf("uhidev_intr: bad repid %d\n", rep); 490 return; 491 } 492 scd = sc->sc_subdevs[rep]; 493 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n", 494 rep, scd, scd ? scd->sc_state : 0)); 495 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN)) 496 return; 497 498 scd->sc_intr(scd, p, cc); 499 } 500 501 void 502 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size) 503 { 504 *desc = sc->sc_repdesc; 505 *size = sc->sc_repdesc_size; 506 } 507 508 int 509 uhidev_open(struct uhidev *scd) 510 { 511 struct uhidev_softc *sc = scd->sc_parent; 512 usbd_status err; 513 int error; 514 515 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n", 516 scd->sc_state, sc->sc_refcnt)); 517 518 if (scd->sc_state & UHIDEV_OPEN) 519 return (EBUSY); 520 scd->sc_state |= UHIDEV_OPEN; 521 if (sc->sc_refcnt++) 522 return (0); 523 524 if (sc->sc_isize == 0) 525 return (0); 526 527 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 528 529 /* Set up input interrupt pipe. */ 530 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize, 531 sc->sc_iep_addr)); 532 533 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr, 534 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf, 535 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 536 if (err != USBD_NORMAL_COMPLETION) { 537 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 538 "error=%d\n", err)); 539 error = EIO; 540 goto out1; 541 } 542 543 DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe)); 544 545 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev); 546 if (sc->sc_ixfer == NULL) { 547 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 548 error = ENOMEM; 549 goto out1; // xxxx 550 } 551 552 /* 553 * Set up output interrupt pipe if an output interrupt endpoint 554 * exists. 555 */ 556 if (sc->sc_oep_addr != -1) { 557 DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr)); 558 559 err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr, 560 0, &sc->sc_opipe); 561 if (err != USBD_NORMAL_COMPLETION) { 562 DPRINTF(("uhidev_open: usbd_open_pipe failed, " 563 "error=%d\n", err)); 564 error = EIO; 565 goto out2; 566 } 567 568 DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe)); 569 570 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev); 571 if (sc->sc_oxfer == NULL) { 572 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 573 error = ENOMEM; 574 goto out3; 575 } 576 577 sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev); 578 if (sc->sc_owxfer == NULL) { 579 DPRINTF(("uhidev_open: couldn't allocate owxfer\n")); 580 error = ENOMEM; 581 goto out3; 582 } 583 } 584 585 return (0); 586 587 out3: 588 /* Abort output pipe */ 589 usbd_close_pipe(sc->sc_opipe); 590 out2: 591 /* Abort input pipe */ 592 usbd_close_pipe(sc->sc_ipipe); 593 out1: 594 DPRINTF(("uhidev_open: failed in someway")); 595 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 596 sc->sc_ibuf = NULL; 597 scd->sc_state &= ~UHIDEV_OPEN; 598 sc->sc_refcnt = 0; 599 sc->sc_ipipe = NULL; 600 sc->sc_opipe = NULL; 601 if (sc->sc_oxfer != NULL) { 602 usbd_free_xfer(sc->sc_oxfer); 603 sc->sc_oxfer = NULL; 604 } 605 if (sc->sc_owxfer != NULL) { 606 usbd_free_xfer(sc->sc_owxfer); 607 sc->sc_owxfer = NULL; 608 } 609 if (sc->sc_ixfer != NULL) { 610 usbd_free_xfer(sc->sc_ixfer); 611 sc->sc_ixfer = NULL; 612 } 613 return (error); 614 } 615 616 void 617 uhidev_close(struct uhidev *scd) 618 { 619 struct uhidev_softc *sc = scd->sc_parent; 620 621 if (!(scd->sc_state & UHIDEV_OPEN)) 622 return; 623 scd->sc_state &= ~UHIDEV_OPEN; 624 if (--sc->sc_refcnt) 625 return; 626 DPRINTF(("uhidev_close: close pipe\n")); 627 628 /* Disable interrupts. */ 629 if (sc->sc_opipe != NULL) { 630 usbd_close_pipe(sc->sc_opipe); 631 sc->sc_opipe = NULL; 632 } 633 634 if (sc->sc_ipipe != NULL) { 635 usbd_close_pipe(sc->sc_ipipe); 636 sc->sc_ipipe = NULL; 637 } 638 639 if (sc->sc_oxfer != NULL) { 640 usbd_free_xfer(sc->sc_oxfer); 641 sc->sc_oxfer = NULL; 642 } 643 644 if (sc->sc_owxfer != NULL) { 645 usbd_free_xfer(sc->sc_owxfer); 646 sc->sc_owxfer = NULL; 647 } 648 649 if (sc->sc_ixfer != NULL) { 650 usbd_free_xfer(sc->sc_ixfer); 651 sc->sc_ixfer = NULL; 652 } 653 654 if (sc->sc_ibuf != NULL) { 655 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 656 sc->sc_ibuf = NULL; 657 } 658 } 659 660 int 661 uhidev_report_type_conv(int hid_type_id) 662 { 663 switch (hid_type_id) { 664 case hid_input: 665 return UHID_INPUT_REPORT; 666 case hid_output: 667 return UHID_OUTPUT_REPORT; 668 case hid_feature: 669 return UHID_FEATURE_REPORT; 670 default: 671 return -1; 672 } 673 } 674 675 int 676 uhidev_set_report(struct uhidev_softc *sc, int type, int id, void *data, 677 int len) 678 { 679 usb_device_request_t req; 680 char *buf = data; 681 int actlen = len; 682 683 /* Prepend the reportID. */ 684 if (id > 0) { 685 len++; 686 buf = malloc(len, M_TEMP, M_WAITOK); 687 buf[0] = id; 688 memcpy(buf + 1, data, len - 1); 689 } 690 691 if (sc->sc_opipe != NULL) { 692 usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, buf, len, 693 USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL); 694 if (usbd_transfer(sc->sc_owxfer)) { 695 usbd_clear_endpoint_stall(sc->sc_opipe); 696 actlen = -1; 697 } 698 } else { 699 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 700 req.bRequest = UR_SET_REPORT; 701 USETW2(req.wValue, type, id); 702 USETW(req.wIndex, sc->sc_ifaceno); 703 USETW(req.wLength, len); 704 705 if (usbd_do_request(sc->sc_udev, &req, buf)) 706 actlen = -1; 707 } 708 709 if (id > 0) 710 free(buf, M_TEMP, len); 711 712 return (actlen); 713 } 714 715 void 716 uhidev_set_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err) 717 { 718 struct uhidev_softc *sc = priv; 719 720 if (err == USBD_STALLED) 721 usbd_clear_endpoint_stall_async(sc->sc_opipe); 722 usbd_free_xfer(xfer); 723 } 724 725 int 726 uhidev_set_report_async(struct uhidev_softc *sc, int type, int id, void *data, 727 int len) 728 { 729 struct usbd_xfer *xfer; 730 usb_device_request_t req; 731 int actlen = len; 732 char *buf; 733 734 xfer = usbd_alloc_xfer(sc->sc_udev); 735 if (xfer == NULL) 736 return (-1); 737 738 if (id > 0) 739 len++; 740 741 buf = usbd_alloc_buffer(xfer, len); 742 if (buf == NULL) { 743 usbd_free_xfer(xfer); 744 return (-1); 745 } 746 747 /* Prepend the reportID. */ 748 if (id > 0) { 749 buf[0] = id; 750 memcpy(buf + 1, data, len - 1); 751 } else { 752 memcpy(buf, data, len); 753 } 754 755 if (sc->sc_opipe != NULL) { 756 usbd_setup_xfer(xfer, sc->sc_opipe, sc, buf, len, 757 USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 758 uhidev_set_report_async_cb); 759 if (usbd_transfer(xfer)) { 760 usbd_clear_endpoint_stall_async(sc->sc_opipe); 761 actlen = -1; 762 } 763 } else { 764 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 765 req.bRequest = UR_SET_REPORT; 766 USETW2(req.wValue, type, id); 767 USETW(req.wIndex, sc->sc_ifaceno); 768 USETW(req.wLength, len); 769 if (usbd_request_async(xfer, &req, NULL, NULL)) 770 actlen = -1; 771 } 772 773 return (actlen); 774 } 775 776 int 777 uhidev_get_report(struct uhidev_softc *sc, int type, int id, void *data, 778 int len) 779 { 780 usb_device_request_t req; 781 char *buf = data; 782 usbd_status err; 783 int actlen; 784 785 if (id > 0) { 786 len++; 787 buf = malloc(len, M_TEMP, M_WAITOK|M_ZERO); 788 } 789 790 req.bmRequestType = UT_READ_CLASS_INTERFACE; 791 req.bRequest = UR_GET_REPORT; 792 USETW2(req.wValue, type, id); 793 USETW(req.wIndex, sc->sc_ifaceno); 794 USETW(req.wLength, len); 795 796 err = usbd_do_request_flags(sc->sc_udev, &req, buf, 0, &actlen, 797 USBD_DEFAULT_TIMEOUT); 798 if (err != USBD_NORMAL_COMPLETION && err != USBD_SHORT_XFER) 799 actlen = -1; 800 801 /* Skip the reportID. */ 802 if (id > 0) { 803 memcpy(data, buf + 1, len - 1); 804 free(buf, M_TEMP, len); 805 } 806 807 return (actlen); 808 } 809 810 void 811 uhidev_get_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err) 812 { 813 struct uhidev_async_info *info = priv; 814 char *buf; 815 int len = -1; 816 817 if (!usbd_is_dying(xfer->pipe->device)) { 818 if (err == USBD_NORMAL_COMPLETION || err == USBD_SHORT_XFER) { 819 len = xfer->actlen; 820 buf = KERNADDR(&xfer->dmabuf, 0); 821 if (info->id > 0) { 822 len--; 823 memcpy(info->data, buf + 1, len); 824 } else { 825 memcpy(info->data, buf, len); 826 } 827 } 828 info->callback(info->priv, info->id, info->data, len); 829 } 830 free(info, M_TEMP, sizeof(*info)); 831 usbd_free_xfer(xfer); 832 } 833 834 int 835 uhidev_get_report_async(struct uhidev_softc *sc, int type, int id, void *data, 836 int len, void *priv, void (*callback)(void *, int, void *, int)) 837 { 838 struct usbd_xfer *xfer; 839 usb_device_request_t req; 840 struct uhidev_async_info *info; 841 int actlen = len; 842 char *buf; 843 844 xfer = usbd_alloc_xfer(sc->sc_udev); 845 if (xfer == NULL) 846 return (-1); 847 848 if (id > 0) 849 len++; 850 851 buf = usbd_alloc_buffer(xfer, len); 852 if (buf == NULL) { 853 usbd_free_xfer(xfer); 854 return (-1); 855 } 856 857 info = malloc(sizeof(*info), M_TEMP, M_NOWAIT); 858 if (info == NULL) { 859 usbd_free_xfer(xfer); 860 return (-1); 861 } 862 863 info->callback = callback; 864 info->priv = priv; 865 info->data = data; 866 info->id = id; 867 868 req.bmRequestType = UT_READ_CLASS_INTERFACE; 869 req.bRequest = UR_GET_REPORT; 870 USETW2(req.wValue, type, id); 871 USETW(req.wIndex, sc->sc_ifaceno); 872 USETW(req.wLength, len); 873 874 if (usbd_request_async(xfer, &req, info, uhidev_get_report_async_cb)) { 875 free(info, M_TEMP, sizeof(*info)); 876 actlen = -1; 877 } 878 879 return (actlen); 880 } 881 882 usbd_status 883 uhidev_write(struct uhidev_softc *sc, void *data, int len) 884 { 885 usbd_status error; 886 887 DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len)); 888 889 if (sc->sc_opipe == NULL) 890 return USBD_INVAL; 891 892 #ifdef UHIDEV_DEBUG 893 if (uhidevdebug > 50) { 894 895 u_int32_t i; 896 u_int8_t *d = data; 897 898 DPRINTF(("uhidev_write: data =")); 899 for (i = 0; i < len; i++) 900 DPRINTF((" %02x", d[i])); 901 DPRINTF(("\n")); 902 } 903 #endif 904 usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, data, len, 905 USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL); 906 error = usbd_transfer(sc->sc_owxfer); 907 if (error) 908 usbd_clear_endpoint_stall(sc->sc_opipe); 909 910 return (error); 911 } 912 913 int 914 uhidev_ioctl(struct uhidev *sc, u_long cmd, caddr_t addr, int flag, 915 struct proc *p) 916 { 917 struct usb_ctl_report_desc *rd; 918 struct usb_ctl_report *re; 919 int size; 920 void *desc; 921 922 switch (cmd) { 923 case USB_GET_REPORT_DESC: 924 uhidev_get_report_desc(sc->sc_parent, &desc, &size); 925 rd = (struct usb_ctl_report_desc *)addr; 926 size = min(size, sizeof rd->ucrd_data); 927 rd->ucrd_size = size; 928 memcpy(rd->ucrd_data, desc, size); 929 break; 930 case USB_GET_REPORT: 931 re = (struct usb_ctl_report *)addr; 932 switch (re->ucr_report) { 933 case UHID_INPUT_REPORT: 934 size = sc->sc_isize; 935 break; 936 case UHID_OUTPUT_REPORT: 937 size = sc->sc_osize; 938 break; 939 case UHID_FEATURE_REPORT: 940 size = sc->sc_fsize; 941 break; 942 default: 943 return EINVAL; 944 } 945 if (uhidev_get_report(sc->sc_parent, re->ucr_report, 946 sc->sc_report_id, re->ucr_data, size) != size) 947 return EIO; 948 break; 949 case USB_SET_REPORT: 950 re = (struct usb_ctl_report *)addr; 951 switch (re->ucr_report) { 952 case UHID_INPUT_REPORT: 953 size = sc->sc_isize; 954 break; 955 case UHID_OUTPUT_REPORT: 956 size = sc->sc_osize; 957 break; 958 case UHID_FEATURE_REPORT: 959 size = sc->sc_fsize; 960 break; 961 default: 962 return EINVAL; 963 } 964 if (uhidev_set_report(sc->sc_parent, re->ucr_report, 965 sc->sc_report_id, re->ucr_data, size) != size) 966 return EIO; 967 break; 968 case USB_GET_REPORT_ID: 969 *(int *)addr = sc->sc_report_id; 970 break; 971 default: 972 return -1; 973 } 974 return 0; 975 } 976 977 int 978 uhidev_set_report_dev(struct uhidev_softc *sc, struct uhidev *dev, int repid) 979 { 980 if ((dev->sc_state & UHIDEV_OPEN) == 0) 981 return ENODEV; 982 if (repid >= sc->sc_nrepid) 983 return EINVAL; 984 985 sc->sc_subdevs[repid] = dev; 986 return 0; 987 } 988