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