1 /* $OpenBSD: uhidev.c,v 1.106 2021/12/03 06:34:38 anton 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 = 0; 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 int nclaimed = 0; 260 261 for (repid = 0; repid < nrepid; repid++) { 262 if (!uha.claimed[repid]) 263 continue; 264 265 nclaimed++; 266 /* 267 * Could already be assigned by uhidev_set_report_dev(). 268 */ 269 if (sc->sc_subdevs[repid] == NULL) 270 sc->sc_subdevs[repid] = (struct uhidev *)dev; 271 } 272 KASSERTMSG(nclaimed > 0, "%s did not claim any report ids", 273 dev->dv_xname); 274 } 275 276 free(uha.claimed, M_TEMP, nrepid); 277 uha.claimed = NULL; 278 279 for (repid = 0; repid < nrepid; repid++) { 280 DPRINTF(("%s: try repid=%d\n", __func__, repid)); 281 if (hid_report_size(desc, size, hid_input, repid) == 0 && 282 hid_report_size(desc, size, hid_output, repid) == 0 && 283 hid_report_size(desc, size, hid_feature, repid) == 0) 284 continue; 285 286 /* Could already be assigned by uhidev_set_report_dev(). */ 287 if (sc->sc_subdevs[repid] != NULL) 288 continue; 289 290 uha.reportid = repid; 291 dev = config_found_sm(self, &uha, uhidevprint, NULL); 292 sc->sc_subdevs[repid] = (struct uhidev *)dev; 293 } 294 } 295 296 #ifndef SMALL_KERNEL 297 int 298 uhidev_use_rdesc(struct uhidev_softc *sc, usb_interface_descriptor_t *id, 299 int vendor, int product, void **descp, int *sizep) 300 { 301 static uByte reportbuf[] = {2, 2}; 302 const void *descptr = NULL; 303 void *desc; 304 int size; 305 306 if (vendor == USB_VENDOR_WACOM) { 307 /* The report descriptor for the Wacom Graphire is broken. */ 308 switch (product) { 309 case USB_PRODUCT_WACOM_GRAPHIRE: 310 size = sizeof(uhid_graphire_report_descr); 311 descptr = uhid_graphire_report_descr; 312 break; 313 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 314 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: 315 uhidev_set_report(sc, UHID_FEATURE_REPORT, 316 2, &reportbuf, sizeof(reportbuf)); 317 size = sizeof(uhid_graphire3_4x5_report_descr); 318 descptr = uhid_graphire3_4x5_report_descr; 319 break; 320 default: 321 break; 322 } 323 } else if ((id->bInterfaceClass == UICLASS_VENDOR && 324 id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER && 325 id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 326 /* The Xbox 360 gamepad has no report descriptor. */ 327 size = sizeof(uhid_xb360gp_report_descr); 328 descptr = uhid_xb360gp_report_descr; 329 } 330 331 if (descptr) { 332 desc = malloc(size, M_USBDEV, M_NOWAIT); 333 if (desc == NULL) 334 return (ENOMEM); 335 336 memcpy(desc, descptr, size); 337 338 *descp = desc; 339 *sizep = size; 340 } 341 342 return (0); 343 } 344 #endif /* !SMALL_KERNEL */ 345 346 int 347 uhidev_maxrepid(void *buf, int len) 348 { 349 struct hid_data *d; 350 struct hid_item h; 351 int maxid; 352 353 maxid = -1; 354 h.report_ID = 0; 355 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 356 if (h.report_ID > maxid) 357 maxid = h.report_ID; 358 hid_end_parse(d); 359 return (maxid); 360 } 361 362 int 363 uhidevprint(void *aux, const char *pnp) 364 { 365 struct uhidev_attach_arg *uha = aux; 366 367 if (pnp) 368 printf("uhid at %s", pnp); 369 if (uha->reportid != 0) 370 printf(" reportid %d", uha->reportid); 371 return (UNCONF); 372 } 373 374 int 375 uhidev_activate(struct device *self, int act) 376 { 377 struct uhidev_softc *sc = (struct uhidev_softc *)self; 378 int i, j, already, rv = 0, r; 379 380 switch (act) { 381 case DVACT_DEACTIVATE: 382 for (i = 0; i < sc->sc_nrepid; i++) { 383 if (sc->sc_subdevs[i] == NULL) 384 continue; 385 386 /* 387 * Only notify devices attached to multiple report ids 388 * once. 389 */ 390 for (already = 0, j = 0; j < i; j++) { 391 if (sc->sc_subdevs[i] == sc->sc_subdevs[j]) { 392 already = 1; 393 break; 394 } 395 } 396 397 if (!already) { 398 r = config_deactivate( 399 &sc->sc_subdevs[i]->sc_dev); 400 if (r && r != EOPNOTSUPP) 401 rv = r; 402 } 403 } 404 usbd_deactivate(sc->sc_udev); 405 break; 406 } 407 return (rv); 408 } 409 410 int 411 uhidev_detach(struct device *self, int flags) 412 { 413 struct uhidev_softc *sc = (struct uhidev_softc *)self; 414 int i, j, rv = 0; 415 416 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 417 418 if (sc->sc_opipe != NULL) { 419 usbd_close_pipe(sc->sc_opipe); 420 sc->sc_opipe = NULL; 421 } 422 423 if (sc->sc_ipipe != NULL) { 424 usbd_close_pipe(sc->sc_ipipe); 425 sc->sc_ipipe = NULL; 426 } 427 428 if (sc->sc_repdesc != NULL) 429 free(sc->sc_repdesc, M_USBDEV, sc->sc_repdesc_size); 430 431 for (i = 0; i < sc->sc_nrepid; i++) { 432 if (sc->sc_subdevs[i] == NULL) 433 continue; 434 435 rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags); 436 437 /* 438 * Nullify without detaching any other instances of this device 439 * found on other report ids. 440 */ 441 for (j = i + 1; j < sc->sc_nrepid; j++) { 442 if (sc->sc_subdevs[i] == sc->sc_subdevs[j]) 443 sc->sc_subdevs[j] = NULL; 444 } 445 446 sc->sc_subdevs[i] = NULL; 447 } 448 free(sc->sc_subdevs, M_USBDEV, sc->sc_nrepid * sizeof(struct uhidev *)); 449 450 return (rv); 451 } 452 453 void 454 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 455 { 456 struct uhidev_softc *sc = addr; 457 struct uhidev *scd; 458 u_char *p; 459 u_int rep; 460 u_int32_t cc; 461 462 if (usbd_is_dying(sc->sc_udev)) 463 return; 464 465 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 466 467 #ifdef UHIDEV_DEBUG 468 if (uhidevdebug > 5) { 469 u_int32_t i; 470 471 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 472 DPRINTF(("uhidev_intr: data =")); 473 for (i = 0; i < cc; i++) 474 DPRINTF((" %02x", sc->sc_ibuf[i])); 475 DPRINTF(("\n")); 476 } 477 #endif 478 479 if (status == USBD_CANCELLED || status == USBD_IOERROR) 480 return; 481 482 if (status != USBD_NORMAL_COMPLETION) { 483 DPRINTF(("%s: interrupt status=%d\n", DEVNAME(sc), status)); 484 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 485 return; 486 } 487 488 p = sc->sc_ibuf; 489 if (sc->sc_nrepid != 1) 490 rep = *p++, cc--; 491 else 492 rep = 0; 493 if (rep >= sc->sc_nrepid) { 494 printf("uhidev_intr: bad repid %d\n", rep); 495 return; 496 } 497 scd = sc->sc_subdevs[rep]; 498 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n", 499 rep, scd, scd ? scd->sc_state : 0)); 500 if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN)) 501 return; 502 503 scd->sc_intr(scd, p, cc); 504 } 505 506 void 507 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size) 508 { 509 *desc = sc->sc_repdesc; 510 *size = sc->sc_repdesc_size; 511 } 512 513 int 514 uhidev_open(struct uhidev *scd) 515 { 516 struct uhidev_softc *sc = scd->sc_parent; 517 usbd_status err; 518 int error; 519 520 DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n", 521 scd->sc_state, sc->sc_refcnt)); 522 523 if (scd->sc_state & UHIDEV_OPEN) 524 return (EBUSY); 525 scd->sc_state |= UHIDEV_OPEN; 526 if (sc->sc_refcnt++) 527 return (0); 528 529 if (sc->sc_isize == 0) 530 return (0); 531 532 sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK); 533 534 /* Set up input interrupt pipe. */ 535 DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize, 536 sc->sc_iep_addr)); 537 538 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr, 539 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf, 540 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 541 if (err != USBD_NORMAL_COMPLETION) { 542 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 543 "error=%d\n", err)); 544 error = EIO; 545 goto out1; 546 } 547 548 DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe)); 549 550 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev); 551 if (sc->sc_ixfer == NULL) { 552 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 553 error = ENOMEM; 554 goto out1; // xxxx 555 } 556 557 /* 558 * Set up output interrupt pipe if an output interrupt endpoint 559 * exists. 560 */ 561 if (sc->sc_oep_addr != -1) { 562 DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr)); 563 564 err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr, 565 0, &sc->sc_opipe); 566 if (err != USBD_NORMAL_COMPLETION) { 567 DPRINTF(("uhidev_open: usbd_open_pipe failed, " 568 "error=%d\n", err)); 569 error = EIO; 570 goto out2; 571 } 572 573 DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe)); 574 575 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev); 576 if (sc->sc_oxfer == NULL) { 577 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 578 error = ENOMEM; 579 goto out3; 580 } 581 582 sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev); 583 if (sc->sc_owxfer == NULL) { 584 DPRINTF(("uhidev_open: couldn't allocate owxfer\n")); 585 error = ENOMEM; 586 goto out3; 587 } 588 } 589 590 return (0); 591 592 out3: 593 /* Abort output pipe */ 594 usbd_close_pipe(sc->sc_opipe); 595 out2: 596 /* Abort input pipe */ 597 usbd_close_pipe(sc->sc_ipipe); 598 out1: 599 DPRINTF(("uhidev_open: failed in someway")); 600 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 601 sc->sc_ibuf = NULL; 602 scd->sc_state &= ~UHIDEV_OPEN; 603 sc->sc_refcnt = 0; 604 sc->sc_ipipe = NULL; 605 sc->sc_opipe = NULL; 606 if (sc->sc_oxfer != NULL) { 607 usbd_free_xfer(sc->sc_oxfer); 608 sc->sc_oxfer = NULL; 609 } 610 if (sc->sc_owxfer != NULL) { 611 usbd_free_xfer(sc->sc_owxfer); 612 sc->sc_owxfer = NULL; 613 } 614 if (sc->sc_ixfer != NULL) { 615 usbd_free_xfer(sc->sc_ixfer); 616 sc->sc_ixfer = NULL; 617 } 618 return (error); 619 } 620 621 void 622 uhidev_close(struct uhidev *scd) 623 { 624 struct uhidev_softc *sc = scd->sc_parent; 625 626 if (!(scd->sc_state & UHIDEV_OPEN)) 627 return; 628 scd->sc_state &= ~UHIDEV_OPEN; 629 if (--sc->sc_refcnt) 630 return; 631 DPRINTF(("uhidev_close: close pipe\n")); 632 633 /* Disable interrupts. */ 634 if (sc->sc_opipe != NULL) { 635 usbd_close_pipe(sc->sc_opipe); 636 sc->sc_opipe = NULL; 637 } 638 639 if (sc->sc_ipipe != NULL) { 640 usbd_close_pipe(sc->sc_ipipe); 641 sc->sc_ipipe = NULL; 642 } 643 644 if (sc->sc_oxfer != NULL) { 645 usbd_free_xfer(sc->sc_oxfer); 646 sc->sc_oxfer = NULL; 647 } 648 649 if (sc->sc_owxfer != NULL) { 650 usbd_free_xfer(sc->sc_owxfer); 651 sc->sc_owxfer = NULL; 652 } 653 654 if (sc->sc_ixfer != NULL) { 655 usbd_free_xfer(sc->sc_ixfer); 656 sc->sc_ixfer = NULL; 657 } 658 659 if (sc->sc_ibuf != NULL) { 660 free(sc->sc_ibuf, M_USBDEV, sc->sc_isize); 661 sc->sc_ibuf = NULL; 662 } 663 } 664 665 int 666 uhidev_report_type_conv(int hid_type_id) 667 { 668 switch (hid_type_id) { 669 case hid_input: 670 return UHID_INPUT_REPORT; 671 case hid_output: 672 return UHID_OUTPUT_REPORT; 673 case hid_feature: 674 return UHID_FEATURE_REPORT; 675 default: 676 return -1; 677 } 678 } 679 680 int 681 uhidev_set_report(struct uhidev_softc *sc, int type, int id, void *data, 682 int len) 683 { 684 usb_device_request_t req; 685 char *buf = data; 686 int actlen = len; 687 688 /* Prepend the reportID. */ 689 if (id > 0) { 690 len++; 691 buf = malloc(len, M_TEMP, M_WAITOK); 692 buf[0] = id; 693 memcpy(buf + 1, data, len - 1); 694 } 695 696 if (sc->sc_opipe != NULL) { 697 usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, buf, len, 698 USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL); 699 if (usbd_transfer(sc->sc_owxfer)) { 700 usbd_clear_endpoint_stall(sc->sc_opipe); 701 actlen = -1; 702 } 703 } else { 704 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 705 req.bRequest = UR_SET_REPORT; 706 USETW2(req.wValue, type, id); 707 USETW(req.wIndex, sc->sc_ifaceno); 708 USETW(req.wLength, len); 709 710 if (usbd_do_request(sc->sc_udev, &req, buf)) 711 actlen = -1; 712 } 713 714 if (id > 0) 715 free(buf, M_TEMP, len); 716 717 return (actlen); 718 } 719 720 void 721 uhidev_set_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err) 722 { 723 struct uhidev_softc *sc = priv; 724 725 if (err == USBD_STALLED) 726 usbd_clear_endpoint_stall_async(sc->sc_opipe); 727 usbd_free_xfer(xfer); 728 } 729 730 int 731 uhidev_set_report_async(struct uhidev_softc *sc, int type, int id, void *data, 732 int len) 733 { 734 struct usbd_xfer *xfer; 735 usb_device_request_t req; 736 int actlen = len; 737 char *buf; 738 739 xfer = usbd_alloc_xfer(sc->sc_udev); 740 if (xfer == NULL) 741 return (-1); 742 743 if (id > 0) 744 len++; 745 746 buf = usbd_alloc_buffer(xfer, len); 747 if (buf == NULL) { 748 usbd_free_xfer(xfer); 749 return (-1); 750 } 751 752 /* Prepend the reportID. */ 753 if (id > 0) { 754 buf[0] = id; 755 memcpy(buf + 1, data, len - 1); 756 } else { 757 memcpy(buf, data, len); 758 } 759 760 if (sc->sc_opipe != NULL) { 761 usbd_setup_xfer(xfer, sc->sc_opipe, sc, buf, len, 762 USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 763 uhidev_set_report_async_cb); 764 if (usbd_transfer(xfer)) { 765 usbd_clear_endpoint_stall_async(sc->sc_opipe); 766 actlen = -1; 767 } 768 } else { 769 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 770 req.bRequest = UR_SET_REPORT; 771 USETW2(req.wValue, type, id); 772 USETW(req.wIndex, sc->sc_ifaceno); 773 USETW(req.wLength, len); 774 if (usbd_request_async(xfer, &req, NULL, NULL)) 775 actlen = -1; 776 } 777 778 return (actlen); 779 } 780 781 int 782 uhidev_get_report(struct uhidev_softc *sc, int type, int id, void *data, 783 int len) 784 { 785 usb_device_request_t req; 786 char *buf = data; 787 usbd_status err; 788 int actlen; 789 790 if (id > 0) { 791 len++; 792 buf = malloc(len, M_TEMP, M_WAITOK|M_ZERO); 793 } 794 795 req.bmRequestType = UT_READ_CLASS_INTERFACE; 796 req.bRequest = UR_GET_REPORT; 797 USETW2(req.wValue, type, id); 798 USETW(req.wIndex, sc->sc_ifaceno); 799 USETW(req.wLength, len); 800 801 err = usbd_do_request_flags(sc->sc_udev, &req, buf, 0, &actlen, 802 USBD_DEFAULT_TIMEOUT); 803 if (err != USBD_NORMAL_COMPLETION && err != USBD_SHORT_XFER) 804 actlen = -1; 805 806 /* Skip the reportID. */ 807 if (id > 0) { 808 memcpy(data, buf + 1, len - 1); 809 free(buf, M_TEMP, len); 810 } 811 812 return (actlen); 813 } 814 815 void 816 uhidev_get_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err) 817 { 818 struct uhidev_async_info *info = priv; 819 char *buf; 820 int len = -1; 821 822 if (!usbd_is_dying(xfer->pipe->device)) { 823 if (err == USBD_NORMAL_COMPLETION || err == USBD_SHORT_XFER) { 824 len = xfer->actlen; 825 buf = KERNADDR(&xfer->dmabuf, 0); 826 if (info->id > 0) { 827 len--; 828 memcpy(info->data, buf + 1, len); 829 } else { 830 memcpy(info->data, buf, len); 831 } 832 } 833 info->callback(info->priv, info->id, info->data, len); 834 } 835 free(info, M_TEMP, sizeof(*info)); 836 usbd_free_xfer(xfer); 837 } 838 839 int 840 uhidev_get_report_async(struct uhidev_softc *sc, int type, int id, void *data, 841 int len, void *priv, void (*callback)(void *, int, void *, int)) 842 { 843 struct usbd_xfer *xfer; 844 usb_device_request_t req; 845 struct uhidev_async_info *info; 846 int actlen = len; 847 char *buf; 848 849 xfer = usbd_alloc_xfer(sc->sc_udev); 850 if (xfer == NULL) 851 return (-1); 852 853 if (id > 0) 854 len++; 855 856 buf = usbd_alloc_buffer(xfer, len); 857 if (buf == NULL) { 858 usbd_free_xfer(xfer); 859 return (-1); 860 } 861 862 info = malloc(sizeof(*info), M_TEMP, M_NOWAIT); 863 if (info == NULL) { 864 usbd_free_xfer(xfer); 865 return (-1); 866 } 867 868 info->callback = callback; 869 info->priv = priv; 870 info->data = data; 871 info->id = id; 872 873 req.bmRequestType = UT_READ_CLASS_INTERFACE; 874 req.bRequest = UR_GET_REPORT; 875 USETW2(req.wValue, type, id); 876 USETW(req.wIndex, sc->sc_ifaceno); 877 USETW(req.wLength, len); 878 879 if (usbd_request_async(xfer, &req, info, uhidev_get_report_async_cb)) { 880 free(info, M_TEMP, sizeof(*info)); 881 actlen = -1; 882 } 883 884 return (actlen); 885 } 886 887 usbd_status 888 uhidev_write(struct uhidev_softc *sc, void *data, int len) 889 { 890 usbd_status error; 891 892 DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len)); 893 894 if (sc->sc_opipe == NULL) 895 return USBD_INVAL; 896 897 #ifdef UHIDEV_DEBUG 898 if (uhidevdebug > 50) { 899 900 u_int32_t i; 901 u_int8_t *d = data; 902 903 DPRINTF(("uhidev_write: data =")); 904 for (i = 0; i < len; i++) 905 DPRINTF((" %02x", d[i])); 906 DPRINTF(("\n")); 907 } 908 #endif 909 usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, data, len, 910 USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL); 911 error = usbd_transfer(sc->sc_owxfer); 912 if (error) 913 usbd_clear_endpoint_stall(sc->sc_opipe); 914 915 return (error); 916 } 917 918 int 919 uhidev_ioctl(struct uhidev *sc, u_long cmd, caddr_t addr, int flag, 920 struct proc *p) 921 { 922 struct usb_ctl_report_desc *rd; 923 struct usb_ctl_report *re; 924 int size; 925 void *desc; 926 927 switch (cmd) { 928 case USB_GET_REPORT_DESC: 929 uhidev_get_report_desc(sc->sc_parent, &desc, &size); 930 rd = (struct usb_ctl_report_desc *)addr; 931 size = min(size, sizeof rd->ucrd_data); 932 rd->ucrd_size = size; 933 memcpy(rd->ucrd_data, desc, size); 934 break; 935 case USB_GET_REPORT: 936 re = (struct usb_ctl_report *)addr; 937 switch (re->ucr_report) { 938 case UHID_INPUT_REPORT: 939 size = sc->sc_isize; 940 break; 941 case UHID_OUTPUT_REPORT: 942 size = sc->sc_osize; 943 break; 944 case UHID_FEATURE_REPORT: 945 size = sc->sc_fsize; 946 break; 947 default: 948 return EINVAL; 949 } 950 if (uhidev_get_report(sc->sc_parent, re->ucr_report, 951 sc->sc_report_id, re->ucr_data, size) != size) 952 return EIO; 953 break; 954 case USB_SET_REPORT: 955 re = (struct usb_ctl_report *)addr; 956 switch (re->ucr_report) { 957 case UHID_INPUT_REPORT: 958 size = sc->sc_isize; 959 break; 960 case UHID_OUTPUT_REPORT: 961 size = sc->sc_osize; 962 break; 963 case UHID_FEATURE_REPORT: 964 size = sc->sc_fsize; 965 break; 966 default: 967 return EINVAL; 968 } 969 if (uhidev_set_report(sc->sc_parent, re->ucr_report, 970 sc->sc_report_id, re->ucr_data, size) != size) 971 return EIO; 972 break; 973 case USB_GET_REPORT_ID: 974 *(int *)addr = sc->sc_report_id; 975 break; 976 default: 977 return -1; 978 } 979 return 0; 980 } 981 982 int 983 uhidev_set_report_dev(struct uhidev_softc *sc, struct uhidev *dev, int repid) 984 { 985 if ((dev->sc_state & UHIDEV_OPEN) == 0) 986 return ENODEV; 987 if (repid >= sc->sc_nrepid) 988 return EINVAL; 989 990 sc->sc_subdevs[repid] = dev; 991 return 0; 992 } 993