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