1 /* $NetBSD: uhidev.c,v 1.97 2025/12/07 19:59:51 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2001, 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart (at) augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna23.net). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.97 2025/12/07 19:59:51 jmcneill Exp $"); 39 40 #ifdef _KERNEL_OPT 41 #include "opt_usb.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/types.h> 46 47 #include <sys/atomic.h> 48 #include <sys/conf.h> 49 #include <sys/device.h> 50 #include <sys/ioctl.h> 51 #include <sys/kernel.h> 52 #include <sys/kmem.h> 53 #include <sys/lwp.h> 54 #include <sys/rndsource.h> 55 #include <sys/signalvar.h> 56 #include <sys/systm.h> 57 #include <sys/xcall.h> 58 59 #include <dev/usb/usb.h> 60 #include <dev/usb/usbhid.h> 61 62 #include <dev/usb/usbdevs.h> 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 #include <dev/usb/usb_quirks.h> 66 67 #include <dev/usb/uhidev.h> 68 #include <dev/hid/hid.h> 69 #include <dev/hid/hidev.h> 70 71 /* Report descriptor for broken Wacom Graphire */ 72 #include <dev/usb/ugraphire_rdesc.h> 73 /* Report descriptor for game controllers in "XInput" mode */ 74 #include <dev/usb/xinput_rdesc.h> 75 /* Report descriptor for Xbox One controllers */ 76 #include <dev/usb/x1input_rdesc.h> 77 78 #include "locators.h" 79 80 struct uhidev_softc { 81 device_t sc_dev; /* base device */ 82 struct usbd_device *sc_udev; 83 struct usbd_interface *sc_iface; /* interface */ 84 int sc_iep_addr; 85 int sc_oep_addr; 86 u_int sc_isize; 87 88 int sc_repdesc_size; 89 void *sc_repdesc; 90 91 u_int sc_nrepid; 92 struct uhidev { 93 struct uhidev_softc *sc_parent; 94 device_t sc_dev; 95 void (*sc_intr)(void *, void *, u_int); 96 void *sc_cookie; 97 krndsource_t sc_rndsource; 98 int sc_in_rep_size; 99 uint8_t sc_report_id; 100 uint8_t sc_state; 101 #define UHIDEV_OPEN 0x01 /* device is open */ 102 #define UHIDEV_STOPPED 0x02 /* xfers are stopped */ 103 struct hidev_tag sc_hidev; 104 } *sc_subdevs; 105 106 kmutex_t sc_lock; 107 kcondvar_t sc_cv; 108 109 /* Read/written under sc_lock. */ 110 struct lwp *sc_writelock; 111 struct lwp *sc_configlock; 112 int sc_refcnt; 113 int sc_writereportid; 114 int sc_stopreportid; 115 116 /* 117 * - Read under sc_lock, provided sc_refcnt > 0. 118 * - Written under sc_configlock only when transitioning to and 119 * from sc_refcnt = 0. 120 */ 121 u_char *sc_ibuf; 122 struct usbd_pipe *sc_ipipe; /* input interrupt pipe */ 123 struct usbd_pipe *sc_opipe; /* output interrupt pipe */ 124 struct usbd_xfer *sc_oxfer; /* write request */ 125 usbd_callback sc_writecallback; /* async write request callback */ 126 void *sc_writecookie; 127 128 u_int sc_flags; 129 #define UHIDEV_F_XB1 0x0001 /* Xbox 1 controller */ 130 }; 131 132 #ifdef UHIDEV_DEBUG 133 #define DPRINTF(x) if (uhidevdebug) printf x 134 #define DPRINTFN(n,x) if (uhidevdebug>(n)) printf x 135 int uhidevdebug = 0; 136 #else 137 #define DPRINTF(x) 138 #define DPRINTFN(n,x) 139 #endif 140 141 static void uhidev_intr(struct usbd_xfer *, void *, usbd_status); 142 143 static int uhidev_maxrepid(void *, int); 144 static int uhidevprint(void *, const char *); 145 146 static int uhidev_match(device_t, cfdata_t, void *); 147 static void uhidev_attach(device_t, device_t, void *); 148 static void uhidev_childdet(device_t, device_t); 149 static int uhidev_detach(device_t, int); 150 151 static void uhidev_init_tag(struct uhidev *); 152 153 CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match, 154 uhidev_attach, uhidev_detach, NULL, NULL, uhidev_childdet); 155 156 static int 157 uhidev_match(device_t parent, cfdata_t match, void *aux) 158 { 159 struct usbif_attach_arg *uiaa = aux; 160 161 /* Game controllers in "XInput" mode */ 162 if (USBIF_IS_XINPUT(uiaa)) 163 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO; 164 /* Xbox One controllers */ 165 if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0) 166 return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO; 167 168 if (uiaa->uiaa_class != UICLASS_HID) 169 return UMATCH_NONE; 170 if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE) 171 return UMATCH_NONE; 172 return UMATCH_IFACECLASS_GENERIC; 173 } 174 175 static void 176 uhidev_attach(device_t parent, device_t self, void *aux) 177 { 178 struct uhidev_softc *sc = device_private(self); 179 struct usbif_attach_arg *uiaa = aux; 180 struct usbd_interface *iface = uiaa->uiaa_iface; 181 usb_interface_descriptor_t *id; 182 usb_endpoint_descriptor_t *ed; 183 struct uhidev_attach_arg uha; 184 device_t dev; 185 int maxinpktsize, size, nrepid, repid, repsz; 186 int *repsizes; 187 int i; 188 void *desc; 189 const void *descptr; 190 usbd_status err; 191 char *devinfop; 192 int locs[UHIDBUSCF_NLOCS]; 193 194 sc->sc_dev = self; 195 sc->sc_udev = uiaa->uiaa_device; 196 sc->sc_iface = iface; 197 198 aprint_naive("\n"); 199 aprint_normal("\n"); 200 201 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 202 cv_init(&sc->sc_cv, "uhidev"); 203 sc->sc_writelock = NULL; 204 sc->sc_configlock = NULL; 205 sc->sc_refcnt = 0; 206 sc->sc_writereportid = -1; 207 sc->sc_stopreportid = -1; 208 209 id = usbd_get_interface_descriptor(iface); 210 211 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0); 212 aprint_normal_dev(self, "%s, iclass %d/%d\n", 213 devinfop, id->bInterfaceClass, id->bInterfaceSubClass); 214 usbd_devinfo_free(devinfop); 215 216 if (!pmf_device_register(self, NULL, NULL)) 217 aprint_error_dev(self, "couldn't establish power handler\n"); 218 219 if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) { 220 if (uiaa->uiaa_product == USB_PRODUCT_WACOM_XD0912U) { 221 /* 222 * Wacom Intuos2 (XD-0912-U) requires longer idle time to 223 * initialize the device with 0x0202. 224 */ 225 DELAY(500000); 226 } 227 } 228 (void)usbd_set_idle(iface, 0, 0); 229 230 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0) 231 (void)usbd_set_protocol(iface, 1); 232 233 maxinpktsize = 0; 234 sc->sc_iep_addr = sc->sc_oep_addr = -1; 235 for (i = 0; i < id->bNumEndpoints; i++) { 236 ed = usbd_interface2endpoint_descriptor(iface, i); 237 if (ed == NULL) { 238 aprint_error_dev(self, 239 "could not read endpoint descriptor\n"); 240 return; 241 } 242 243 DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d " 244 "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d" 245 " bInterval=%d\n", 246 ed->bLength, ed->bDescriptorType, 247 ed->bEndpointAddress & UE_ADDR, 248 UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out", 249 ed->bmAttributes & UE_XFERTYPE, 250 UGETW(ed->wMaxPacketSize), ed->bInterval)); 251 252 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 253 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { 254 maxinpktsize = UGETW(ed->wMaxPacketSize); 255 sc->sc_iep_addr = ed->bEndpointAddress; 256 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 257 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { 258 sc->sc_oep_addr = ed->bEndpointAddress; 259 } else { 260 aprint_verbose_dev(self, "endpoint %d: ignored\n", i); 261 } 262 } 263 264 /* 265 * Check that we found an input interrupt endpoint. The output interrupt 266 * endpoint is optional 267 */ 268 if (sc->sc_iep_addr == -1) { 269 aprint_error_dev(self, "no input interrupt endpoint\n"); 270 return; 271 } 272 273 /* XXX need to extend this */ 274 descptr = NULL; 275 if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) { 276 static uByte reportbuf[3]; 277 278 /* The report descriptor for the Wacom Graphire is broken. */ 279 switch (uiaa->uiaa_product) { 280 case USB_PRODUCT_WACOM_GRAPHIRE3_4X5: 281 case USB_PRODUCT_WACOM_GRAPHIRE3_6X8: 282 case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */ 283 /* 284 * The Graphire3 needs 0x0202 to be written to 285 * feature report ID 2 before it'll start 286 * returning digitizer data. 287 */ 288 reportbuf[0] = 0x02; 289 reportbuf[1] = 0x02; 290 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2, 291 &reportbuf, 2); 292 293 size = sizeof(uhid_graphire3_4x5_report_descr); 294 descptr = uhid_graphire3_4x5_report_descr; 295 break; 296 case USB_PRODUCT_WACOM_GRAPHIRE: 297 case USB_PRODUCT_WACOM_GRAPHIRE2: 298 case USB_PRODUCT_WACOM_XD0912U: 299 case USB_PRODUCT_WACOM_CTH690K0: 300 reportbuf[0] = 0x02; 301 reportbuf[1] = 0x02; 302 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2, 303 &reportbuf, 2); 304 break; 305 default: 306 /* Keep descriptor */ 307 break; 308 } 309 } 310 if (USBIF_IS_XINPUT(uiaa)) { 311 size = sizeof(uhid_xinput_report_descr); 312 descptr = uhid_xinput_report_descr; 313 } 314 if (USBIF_IS_X1INPUT(uiaa)) { 315 sc->sc_flags |= UHIDEV_F_XB1; 316 size = sizeof(uhid_x1input_report_descr); 317 descptr = uhid_x1input_report_descr; 318 } 319 320 if (descptr) { 321 desc = kmem_alloc(size, KM_SLEEP); 322 err = USBD_NORMAL_COMPLETION; 323 memcpy(desc, descptr, size); 324 } else { 325 desc = NULL; 326 err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size); 327 } 328 if (err) { 329 aprint_error_dev(self, "no report descriptor\n"); 330 return; 331 } 332 333 if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN && 334 uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) { 335 static uByte reportbuf[] = { 1 }; 336 /* 337 * This device was sold by Konami with its ParaParaParadise 338 * game for PlayStation2. It needs to be "turned on" 339 * before it will send any reports. 340 */ 341 342 usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0, 343 &reportbuf, sizeof(reportbuf)); 344 } 345 346 if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH && 347 uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) { 348 uint8_t *data = desc; 349 /* 350 * This device has a odd USAGE_MINIMUM value that would 351 * cause the multimedia keys to have their usage number 352 * shifted up one usage. Adjust so the usages are sane. 353 */ 354 355 if (data[0x56] == 0x19 && data[0x57] == 0x01 && 356 data[0x58] == 0x2a && data[0x59] == 0x8c) 357 data[0x57] = 0x00; 358 } 359 360 /* 361 * Enable the Six Axis and DualShock 3 controllers. 362 * See http://ps3.jim.sh/sixaxis/usb/ 363 */ 364 if (uiaa->uiaa_vendor == USB_VENDOR_SONY && 365 uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) { 366 usb_device_request_t req; 367 char data[17]; 368 int actlen; 369 370 req.bmRequestType = UT_READ_CLASS_INTERFACE; 371 req.bRequest = 1; 372 USETW(req.wValue, 0x3f2); 373 USETW(req.wIndex, 0); 374 USETW(req.wLength, sizeof(data)); 375 376 usbd_do_request_flags(sc->sc_udev, &req, data, 377 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT); 378 } 379 380 sc->sc_repdesc = desc; 381 sc->sc_repdesc_size = size; 382 383 uha.uiaa = uiaa; 384 nrepid = uhidev_maxrepid(desc, size); 385 if (nrepid < 0) 386 return; 387 if (nrepid > 0) 388 aprint_normal_dev(self, "%d report ids\n", nrepid); 389 nrepid++; 390 repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP); 391 sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(sc->sc_subdevs[0]), 392 KM_SLEEP); 393 394 /* Just request max packet size for the interrupt pipe */ 395 sc->sc_isize = maxinpktsize; 396 sc->sc_nrepid = nrepid; 397 398 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 399 400 for (repid = 0; repid < nrepid; repid++) { 401 repsz = hid_report_size(desc, size, hid_input, repid); 402 DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz)); 403 repsizes[repid] = repsz; 404 } 405 406 DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize)); 407 408 for (repid = 0; repid < nrepid; repid++) { 409 struct uhidev *scd = &sc->sc_subdevs[repid]; 410 411 scd->sc_parent = sc; 412 scd->sc_report_id = repid; 413 scd->sc_in_rep_size = repsizes[repid]; 414 uhidev_init_tag(scd); 415 416 DPRINTF(("uhidev_match: try repid=%d\n", repid)); 417 if (hid_report_size(desc, size, hid_input, repid) == 0 && 418 hid_report_size(desc, size, hid_output, repid) == 0 && 419 hid_report_size(desc, size, hid_feature, repid) == 0) { 420 ; /* already NULL in sc->sc_subdevs[repid] */ 421 } else { 422 uha.parent = scd; 423 uha.reportid = repid; 424 uha.hidev = &scd->sc_hidev; 425 locs[UHIDBUSCF_REPORTID] = repid; 426 427 dev = config_found(self, &uha, uhidevprint, 428 CFARGS(.submatch = config_stdsubmatch, 429 .locators = locs)); 430 sc->sc_subdevs[repid].sc_dev = dev; 431 if (dev == NULL) 432 continue; 433 /* 434 * XXXSMP -- could be detached in the middle of 435 * sleeping for allocation in rnd_attach_source 436 */ 437 rnd_attach_source(&scd->sc_rndsource, 438 device_xname(dev), RND_TYPE_TTY, RND_FLAG_DEFAULT); 439 } 440 } 441 kmem_free(repsizes, nrepid * sizeof(*repsizes)); 442 443 return; 444 } 445 446 static int 447 uhidev_maxrepid(void *buf, int len) 448 { 449 struct hid_data *d; 450 struct hid_item h; 451 int maxid; 452 453 maxid = -1; 454 h.report_ID = 0; 455 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 456 if ((int)h.report_ID > maxid) 457 maxid = h.report_ID; 458 hid_end_parse(d); 459 return MIN(maxid, UHIDEV_MAXREPID); 460 } 461 462 static int 463 uhidevprint(void *aux, const char *pnp) 464 { 465 struct uhidev_attach_arg *uha = aux; 466 467 if (pnp) 468 aprint_normal("uhid at %s", pnp); 469 if (uha->reportid != 0) 470 aprint_normal(" reportid %d", uha->reportid); 471 return UNCONF; 472 } 473 474 static void 475 uhidev_childdet(device_t self, device_t child) 476 { 477 int i; 478 struct uhidev_softc *sc = device_private(self); 479 480 for (i = 0; i < sc->sc_nrepid; i++) { 481 if (sc->sc_subdevs[i].sc_dev == child) 482 break; 483 } 484 KASSERT(i < sc->sc_nrepid); 485 sc->sc_subdevs[i].sc_dev = NULL; 486 /* 487 * XXXSMP -- could be reattached in the middle of sleeping for 488 * lock on sources to delete this in rnd_attach_source 489 * 490 * (Actually this can't happen right now because there's no 491 * rescan method, but if there were, it could.) 492 */ 493 rnd_detach_source(&sc->sc_subdevs[i].sc_rndsource); 494 } 495 496 static int 497 uhidev_detach(device_t self, int flags) 498 { 499 struct uhidev_softc *sc = device_private(self); 500 int rv; 501 502 DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags)); 503 504 /* 505 * Try to detach all our children. If anything fails, bail. 506 * Failure can happen if this is from drvctl -d; of course, if 507 * this is a USB device being yanked, flags will have 508 * DETACH_FORCE and the children will not have the option of 509 * refusing detachment. If they do detach, the pipes can no 510 * longer be in use. 511 */ 512 rv = config_detach_children(self, flags); 513 if (rv) 514 return rv; 515 516 KASSERTMSG(sc->sc_refcnt == 0, 517 "%s: %d refs remain", device_xname(sc->sc_dev), sc->sc_refcnt); 518 KASSERT(sc->sc_opipe == NULL); 519 KASSERT(sc->sc_ipipe == NULL); 520 KASSERT(sc->sc_ibuf == NULL); 521 522 if (sc->sc_repdesc != NULL) { 523 kmem_free(sc->sc_repdesc, sc->sc_repdesc_size); 524 sc->sc_repdesc = NULL; 525 } 526 if (sc->sc_subdevs != NULL) { 527 int nrepid = sc->sc_nrepid; 528 kmem_free(sc->sc_subdevs, nrepid * sizeof(sc->sc_subdevs[0])); 529 sc->sc_subdevs = NULL; 530 } 531 532 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 533 534 pmf_device_deregister(self); 535 KASSERT(sc->sc_configlock == NULL); 536 KASSERT(sc->sc_writelock == NULL); 537 cv_destroy(&sc->sc_cv); 538 mutex_destroy(&sc->sc_lock); 539 540 return rv; 541 } 542 543 static void 544 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 545 { 546 struct uhidev_softc *sc = addr; 547 struct uhidev *scd; 548 u_char *p; 549 u_int rep; 550 uint32_t cc; 551 552 usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL); 553 554 #ifdef UHIDEV_DEBUG 555 if (uhidevdebug > 5) { 556 uint32_t i; 557 558 DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc)); 559 DPRINTF(("uhidev_intr: data =")); 560 for (i = 0; i < cc; i++) 561 DPRINTF((" %02x", sc->sc_ibuf[i])); 562 DPRINTF(("\n")); 563 } 564 #endif 565 566 if (status == USBD_CANCELLED) 567 return; 568 569 if (status != USBD_NORMAL_COMPLETION) { 570 DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev), 571 status)); 572 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 573 return; 574 } 575 576 p = sc->sc_ibuf; 577 if (sc->sc_nrepid != 1) 578 rep = *p++, cc--; 579 else 580 rep = 0; 581 if (rep >= sc->sc_nrepid) { 582 printf("uhidev_intr: bad repid %d\n", rep); 583 return; 584 } 585 scd = &sc->sc_subdevs[rep]; 586 DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=%#x\n", 587 rep, scd, scd->sc_state)); 588 if (!(atomic_load_acquire(&scd->sc_state) & UHIDEV_OPEN)) 589 return; 590 #ifdef UHIDEV_DEBUG 591 if (scd->sc_in_rep_size != cc) { 592 DPRINTF(("%s: expected %d bytes, got %d\n", 593 device_xname(sc->sc_dev), scd->sc_in_rep_size, cc)); 594 } 595 #endif 596 if (cc == 0) { 597 DPRINTF(("%s: 0-length input ignored\n", 598 device_xname(sc->sc_dev))); 599 return; 600 } 601 rnd_add_uint32(&scd->sc_rndsource, (uintptr_t)(sc->sc_ibuf)); 602 scd->sc_intr(scd->sc_cookie, p, cc); 603 } 604 605 void 606 uhidev_get_report_desc(struct uhidev *scd, void **desc, int *size) 607 { 608 struct uhidev_softc *sc = scd->sc_parent; 609 610 *desc = sc->sc_repdesc; 611 *size = sc->sc_repdesc_size; 612 } 613 614 static int 615 uhidev_config_enter(struct uhidev_softc *sc) 616 { 617 int error; 618 619 KASSERT(mutex_owned(&sc->sc_lock)); 620 621 for (;;) { 622 if (sc->sc_configlock == NULL) 623 break; 624 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock); 625 if (error) 626 return error; 627 } 628 629 sc->sc_configlock = curlwp; 630 return 0; 631 } 632 633 static void 634 uhidev_config_enter_nointr(struct uhidev_softc *sc) 635 { 636 637 KASSERT(mutex_owned(&sc->sc_lock)); 638 639 while (sc->sc_configlock) 640 cv_wait(&sc->sc_cv, &sc->sc_lock); 641 sc->sc_configlock = curlwp; 642 } 643 644 static void 645 uhidev_config_exit(struct uhidev_softc *sc) 646 { 647 648 KASSERT(mutex_owned(&sc->sc_lock)); 649 KASSERTMSG(sc->sc_configlock == curlwp, "%s: migrated from %p to %p", 650 device_xname(sc->sc_dev), curlwp, sc->sc_configlock); 651 652 sc->sc_configlock = NULL; 653 cv_broadcast(&sc->sc_cv); 654 } 655 656 /* 657 * uhidev_open_pipes(sc) 658 * 659 * Ensure the pipes of the softc are open. Caller must hold 660 * sc_lock, which may be released and reacquired. 661 */ 662 static int 663 uhidev_open_pipes(struct uhidev_softc *sc) 664 { 665 usbd_status err; 666 int error; 667 668 KASSERT(mutex_owned(&sc->sc_lock)); 669 670 /* 671 * If the pipes are already open, just increment the reference 672 * count. The reference count is limited by the number of 673 * report ids, so this can't overflow. 674 */ 675 if (sc->sc_refcnt) { 676 KASSERT(sc->sc_refcnt < UHIDEV_MAXREPID); 677 sc->sc_refcnt++; 678 return 0; 679 } 680 681 /* 682 * If there's no input data to prepare, don't bother with the 683 * pipes. We assume any device that does output also does 684 * input; if you have a device where this is wrong, then 685 * uhidev_write will fail gracefully (it checks sc->sc_opipe), 686 * and you can use that device to test the changes needed to 687 * open the output pipe here. 688 */ 689 if (sc->sc_isize == 0) 690 return 0; 691 692 /* 693 * Lock the configuration and release sc_lock -- we may sleep 694 * to allocate. If someone else got in first, we're done; 695 * otherwise open the pipes. 696 */ 697 error = uhidev_config_enter(sc); 698 if (error) 699 goto out; 700 if (sc->sc_refcnt) { 701 KASSERT(sc->sc_refcnt < UHIDEV_MAXREPID); 702 sc->sc_refcnt++; 703 error = 0; 704 goto out0; 705 } 706 mutex_exit(&sc->sc_lock); 707 708 /* Allocate an input buffer. */ 709 sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP); 710 711 /* Set up input interrupt pipe. */ 712 DPRINTF(("%s: isize=%d, ep=0x%02x\n", __func__, sc->sc_isize, 713 sc->sc_iep_addr)); 714 715 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr, 716 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf, 717 sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL); 718 if (err != USBD_NORMAL_COMPLETION) { 719 DPRINTF(("uhidopen: usbd_open_pipe_intr failed, " 720 "error=%d\n", err)); 721 error = EIO; 722 goto out1; 723 } 724 725 /* 726 * Set up output interrupt pipe if an output interrupt endpoint 727 * exists. 728 */ 729 if (sc->sc_oep_addr != -1) { 730 DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr)); 731 732 err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr, 733 0, &sc->sc_opipe); 734 735 if (err != USBD_NORMAL_COMPLETION) { 736 DPRINTF(("uhidev_open: usbd_open_pipe failed, " 737 "error=%d\n", err)); 738 error = EIO; 739 goto out2; 740 } 741 DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe)); 742 743 error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0, 744 &sc->sc_oxfer); 745 if (error) { 746 DPRINTF(("uhidev_open: couldn't allocate an xfer\n")); 747 goto out3; 748 } 749 750 if (sc->sc_flags & UHIDEV_F_XB1) { 751 uint8_t init_data[] = { 0x05, 0x20 }; 752 int init_data_len = sizeof(init_data); 753 err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, 754 USBD_NO_TIMEOUT, init_data, &init_data_len); 755 if (err != USBD_NORMAL_COMPLETION) { 756 DPRINTF(("uhidev_open: xb1 init failed, " 757 "error=%d\n", err)); 758 error = EIO; 759 goto out4; 760 } 761 } 762 } 763 764 /* Success! */ 765 mutex_enter(&sc->sc_lock); 766 KASSERTMSG(sc->sc_refcnt == 0, "%d refs spuriously acquired", 767 sc->sc_refcnt); 768 sc->sc_refcnt++; 769 goto out0; 770 771 out4: if (sc->sc_oxfer) { 772 usbd_abort_pipe(sc->sc_opipe); 773 usbd_destroy_xfer(sc->sc_oxfer); 774 sc->sc_oxfer = NULL; 775 } 776 out3: if (sc->sc_opipe) { 777 usbd_close_pipe(sc->sc_opipe); 778 sc->sc_opipe = NULL; 779 } 780 out2: if (sc->sc_ipipe) { 781 usbd_abort_pipe(sc->sc_ipipe); 782 usbd_close_pipe(sc->sc_ipipe); 783 sc->sc_ipipe = NULL; 784 } 785 out1: kmem_free(sc->sc_ibuf, sc->sc_isize); 786 sc->sc_ibuf = NULL; 787 mutex_enter(&sc->sc_lock); 788 out0: KASSERT(mutex_owned(&sc->sc_lock)); 789 uhidev_config_exit(sc); 790 out: KASSERT(mutex_owned(&sc->sc_lock)); 791 return error; 792 } 793 794 static void 795 uhidev_close_pipes(struct uhidev_softc *sc) 796 { 797 798 KASSERT(mutex_owned(&sc->sc_lock)); 799 KASSERTMSG(sc->sc_refcnt > 0, "%s: refcnt fouled: %d", 800 device_xname(sc->sc_dev), sc->sc_refcnt); 801 802 /* If this isn't the last reference, just decrement. */ 803 if (sc->sc_refcnt > 1) { 804 sc->sc_refcnt--; 805 return; 806 } 807 808 /* 809 * Lock the configuration and release sc_lock so we may sleep 810 * to free memory. We're not waiting for anyone to allocate or 811 * free anything. 812 */ 813 uhidev_config_enter_nointr(sc); 814 815 /* 816 * If someone else acquired a reference while we were waiting 817 * for the config lock, nothing more for us to do. 818 */ 819 if (sc->sc_refcnt > 1) { 820 sc->sc_refcnt--; 821 uhidev_config_exit(sc); 822 return; 823 } 824 825 /* 826 * We're the last reference and committed to closing the pipes. 827 * Decrement the reference count before we release the lock -- 828 * access to the pipes is allowed as long as the reference 829 * count is positive, so this forces all new opens to wait 830 * until the config lock is released. 831 */ 832 KASSERTMSG(sc->sc_refcnt == 1, "%s: refcnt fouled: %d", 833 device_xname(sc->sc_dev), sc->sc_refcnt); 834 sc->sc_refcnt--; 835 mutex_exit(&sc->sc_lock); 836 837 if (sc->sc_oxfer) { 838 usbd_abort_pipe(sc->sc_opipe); 839 usbd_destroy_xfer(sc->sc_oxfer); 840 sc->sc_oxfer = NULL; 841 } 842 if (sc->sc_opipe) { 843 usbd_close_pipe(sc->sc_opipe); 844 sc->sc_opipe = NULL; 845 } 846 if (sc->sc_ipipe) { 847 usbd_abort_pipe(sc->sc_ipipe); 848 usbd_close_pipe(sc->sc_ipipe); 849 sc->sc_ipipe = NULL; 850 } 851 kmem_free(sc->sc_ibuf, sc->sc_isize); 852 sc->sc_ibuf = NULL; 853 854 mutex_enter(&sc->sc_lock); 855 uhidev_config_exit(sc); 856 KASSERTMSG(sc->sc_refcnt == 0, "%s: refcnt fouled: %d", 857 device_xname(sc->sc_dev), sc->sc_refcnt); 858 } 859 860 int 861 uhidev_open(struct uhidev *scd, void (*intr)(void *, void *, u_int), 862 void *cookie) 863 { 864 struct uhidev_softc *sc = scd->sc_parent; 865 int error; 866 867 mutex_enter(&sc->sc_lock); 868 869 DPRINTF(("uhidev_open(%s, report %d = %s): state=%x refcnt=%d\n", 870 device_xname(sc->sc_dev), 871 scd->sc_report_id, 872 device_xname(scd->sc_dev), 873 scd->sc_state, 874 sc->sc_refcnt)); 875 876 /* Mark the report id open. This is an exclusive lock. */ 877 if (scd->sc_state & UHIDEV_OPEN) { 878 error = EBUSY; 879 goto out; 880 } 881 scd->sc_intr = intr; 882 scd->sc_cookie = cookie; 883 atomic_store_release(&scd->sc_state, scd->sc_state | UHIDEV_OPEN); 884 885 /* Open the pipes which are shared by all report ids. */ 886 error = uhidev_open_pipes(sc); 887 if (error) 888 goto out; 889 890 /* Success! */ 891 error = 0; 892 893 out: if (error) { 894 KASSERTMSG(scd->sc_state & UHIDEV_OPEN, 895 "%s: report id %d: closed while opening", 896 device_xname(sc->sc_dev), scd->sc_report_id); 897 atomic_store_relaxed(&scd->sc_state, 898 scd->sc_state & ~UHIDEV_OPEN); 899 } 900 mutex_exit(&sc->sc_lock); 901 return error; 902 } 903 904 /* 905 * uhidev_stop(scd) 906 * 907 * Make all current and future output reports or xfers by scd to 908 * the output pipe to fail. Caller must then ensure no more will 909 * be submitted and then call uhidev_close. 910 * 911 * Side effect: If uhidev_write was in progress for this scd, 912 * blocks all other uhidev_writes until uhidev_close on this scd. 913 * 914 * May sleep but only for a short duration to wait for USB 915 * transfer completion callbacks to run. 916 */ 917 void 918 uhidev_stop(struct uhidev *scd) 919 { 920 struct uhidev_softc *sc = scd->sc_parent; 921 922 mutex_enter(&sc->sc_lock); 923 924 /* Prevent further writes on this report from starting. */ 925 atomic_store_relaxed(&scd->sc_state, scd->sc_state | UHIDEV_STOPPED); 926 927 /* If there's no output pipe at all, nothing to do. */ 928 if (sc->sc_opipe == NULL) 929 goto out; 930 931 /* 932 * If there's no write on this report in progress, nothing to 933 * do -- any subsequent attempts will be prevented by 934 * UHIDEV_STOPPED. 935 */ 936 if (sc->sc_writereportid != scd->sc_report_id) 937 goto out; 938 939 /* 940 * Caller must wait for uhidev_open to succeed before calling 941 * uhidev_write, and must wait for all uhidev_writes to return 942 * before calling uhidev_close, so neither on can be in flight 943 * right now. 944 * 945 * Suspend the pipe, but hold up uhidev_write from any report 946 * until we confirm this one has finished. We will resume the 947 * pipe only after all uhidev_writes on this report have 948 * finished -- when the caller calls uhidev_close. 949 */ 950 KASSERTMSG(sc->sc_stopreportid == -1, "%d", sc->sc_stopreportid); 951 sc->sc_stopreportid = scd->sc_report_id; 952 mutex_exit(&sc->sc_lock); 953 954 usbd_suspend_pipe(sc->sc_opipe); 955 956 mutex_enter(&sc->sc_lock); 957 KASSERT(sc->sc_stopreportid == scd->sc_report_id); 958 sc->sc_stopreportid = scd->sc_report_id; 959 cv_broadcast(&sc->sc_cv); 960 out: mutex_exit(&sc->sc_lock); 961 } 962 963 /* 964 * uhidev_close(scd) 965 * 966 * Close a uhidev previously opened with uhidev_open. If writes 967 * had been stopped with uhidev_stop, allow writes at other report 968 * ids again. 969 */ 970 void 971 uhidev_close(struct uhidev *scd) 972 { 973 struct uhidev_softc *sc = scd->sc_parent; 974 975 mutex_enter(&sc->sc_lock); 976 977 DPRINTF(("uhidev_close(%s, report %d = %s): state=%x refcnt=%d\n", 978 device_xname(sc->sc_dev), 979 scd->sc_report_id, 980 device_xname(scd->sc_dev), 981 scd->sc_state, 982 sc->sc_refcnt)); 983 984 KASSERTMSG(scd->sc_state & UHIDEV_OPEN, 985 "%s: report id %d: unpaired close", 986 device_xname(sc->sc_dev), scd->sc_report_id); 987 988 /* 989 * If the caller had issued uhidev_stop to interrupt a write 990 * for this report, then resume the pipe now that no further 991 * uhidev_write on the same report is possible, and wake anyone 992 * trying to write on other reports. 993 */ 994 if (sc->sc_stopreportid == scd->sc_report_id) { 995 KASSERT(scd->sc_state & UHIDEV_STOPPED); 996 mutex_exit(&sc->sc_lock); 997 998 usbd_resume_pipe(sc->sc_opipe); 999 1000 mutex_enter(&sc->sc_lock); 1001 KASSERT(sc->sc_stopreportid == scd->sc_report_id); 1002 KASSERT(scd->sc_state & UHIDEV_STOPPED); 1003 sc->sc_stopreportid = -1; 1004 cv_broadcast(&sc->sc_cv); 1005 } 1006 1007 /* 1008 * Close our reference to the pipes, and mark our report as no 1009 * longer open. If it was stopped, clear that too -- drivers 1010 * are forbidden from issuing writes after uhidev_close anyway. 1011 */ 1012 KASSERT(scd->sc_state & UHIDEV_OPEN); 1013 uhidev_close_pipes(sc); 1014 KASSERT(scd->sc_state & UHIDEV_OPEN); 1015 atomic_store_relaxed(&scd->sc_state, 1016 scd->sc_state & ~(UHIDEV_OPEN | UHIDEV_STOPPED)); 1017 1018 /* 1019 * Make sure the next uhidev_intr (which runs in softint, like 1020 * XC_HIGHPRI) notices that UHIDEV_OPEN is cleared, and wait 1021 * for any current one to finish, in case the pipe is still 1022 * open for other report ids. 1023 * 1024 * We must drop the lock while doing this, because 1025 * uhidev_write_callback takes the lock in softint context and 1026 * it could deadlock with the xcall softint. 1027 * 1028 * It is safe to drop the lock now before zeroing sc_intr and 1029 * sc_cookie because the driver is obligated not to reopen 1030 * until after uhidev_close returns. 1031 */ 1032 mutex_exit(&sc->sc_lock); 1033 xc_barrier(XC_HIGHPRI); 1034 mutex_enter(&sc->sc_lock); 1035 KASSERT((scd->sc_state & UHIDEV_OPEN) == 0); 1036 scd->sc_intr = NULL; 1037 scd->sc_cookie = NULL; 1038 1039 mutex_exit(&sc->sc_lock); 1040 } 1041 1042 usbd_status 1043 uhidev_set_report(struct uhidev *scd, int type, void *data, int len) 1044 { 1045 char *buf; 1046 usbd_status retstat; 1047 1048 if (scd->sc_report_id == 0) 1049 return usbd_set_report(scd->sc_parent->sc_iface, type, 1050 scd->sc_report_id, data, len); 1051 1052 buf = kmem_alloc(len + 1, KM_SLEEP); 1053 buf[0] = scd->sc_report_id; 1054 memcpy(buf+1, data, len); 1055 1056 retstat = usbd_set_report(scd->sc_parent->sc_iface, type, 1057 scd->sc_report_id, buf, len + 1); 1058 1059 kmem_free(buf, len + 1); 1060 1061 return retstat; 1062 } 1063 1064 usbd_status 1065 uhidev_get_report(struct uhidev *scd, int type, void *data, int len) 1066 { 1067 return usbd_get_report(scd->sc_parent->sc_iface, type, 1068 scd->sc_report_id, data, len); 1069 } 1070 1071 usbd_status 1072 uhidev_write(struct uhidev *scd, void *data, int len) 1073 { 1074 struct uhidev_softc *sc = scd->sc_parent; 1075 usbd_status err; 1076 1077 DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len)); 1078 1079 if (sc->sc_opipe == NULL) 1080 return USBD_INVAL; 1081 1082 mutex_enter(&sc->sc_lock); 1083 KASSERT(sc->sc_refcnt); 1084 KASSERT(scd->sc_state & UHIDEV_OPEN); 1085 for (;;) { 1086 if (scd->sc_state & UHIDEV_STOPPED) { 1087 err = USBD_CANCELLED; 1088 goto out; 1089 } 1090 if (sc->sc_writelock == NULL && sc->sc_stopreportid == -1) 1091 break; 1092 if (cv_wait_sig(&sc->sc_cv, &sc->sc_lock)) { 1093 err = USBD_INTERRUPTED; 1094 goto out; 1095 } 1096 } 1097 sc->sc_writelock = curlwp; 1098 sc->sc_writereportid = scd->sc_report_id; 1099 mutex_exit(&sc->sc_lock); 1100 1101 #ifdef UHIDEV_DEBUG 1102 if (uhidevdebug > 50) { 1103 1104 uint32_t i; 1105 uint8_t *d = data; 1106 1107 DPRINTF(("uhidev_write: data =")); 1108 for (i = 0; i < len; i++) 1109 DPRINTF((" %02x", d[i])); 1110 DPRINTF(("\n")); 1111 } 1112 #endif 1113 err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, 1114 USBD_NO_TIMEOUT, data, &len); 1115 1116 mutex_enter(&sc->sc_lock); 1117 KASSERT(sc->sc_refcnt); 1118 KASSERT(scd->sc_state & UHIDEV_OPEN); 1119 KASSERTMSG(sc->sc_writelock == curlwp, "%s: migrated from %p to %p", 1120 device_xname(sc->sc_dev), curlwp, sc->sc_writelock); 1121 KASSERTMSG(sc->sc_writereportid == scd->sc_report_id, 1122 "%s: changed write report ids from %d to %d", 1123 device_xname(sc->sc_dev), scd->sc_report_id, sc->sc_writereportid); 1124 sc->sc_writereportid = -1; 1125 sc->sc_writelock = NULL; 1126 cv_broadcast(&sc->sc_cv); 1127 out: mutex_exit(&sc->sc_lock); 1128 return err; 1129 } 1130 1131 static void 1132 uhidev_write_callback(struct usbd_xfer *xfer, void *cookie, usbd_status err) 1133 { 1134 struct uhidev_softc *sc = cookie; 1135 usbd_callback writecallback; 1136 void *writecookie; 1137 1138 if (err) { 1139 if (err != USBD_CANCELLED) 1140 usbd_clear_endpoint_stall_async(sc->sc_opipe); 1141 } 1142 1143 mutex_enter(&sc->sc_lock); 1144 KASSERT(sc->sc_writelock == (void *)1); 1145 writecallback = sc->sc_writecallback; 1146 writecookie = sc->sc_writecookie; 1147 sc->sc_writereportid = -1; 1148 sc->sc_writelock = NULL; 1149 sc->sc_writecallback = NULL; 1150 sc->sc_writecookie = NULL; 1151 cv_broadcast(&sc->sc_cv); 1152 mutex_exit(&sc->sc_lock); 1153 1154 (*writecallback)(xfer, writecookie, err); 1155 } 1156 1157 usbd_status 1158 uhidev_write_async(struct uhidev *scd, void *data, int len, int flags, 1159 int timo, usbd_callback writecallback, void *writecookie) 1160 { 1161 struct uhidev_softc *sc = scd->sc_parent; 1162 usbd_status err; 1163 1164 DPRINTF(("%s: data=%p, len=%d\n", __func__, data, len)); 1165 1166 if (sc->sc_opipe == NULL) 1167 return USBD_INVAL; 1168 1169 mutex_enter(&sc->sc_lock); 1170 KASSERT(sc->sc_refcnt); 1171 KASSERT(scd->sc_state & UHIDEV_OPEN); 1172 if (scd->sc_state & UHIDEV_STOPPED) { 1173 err = USBD_CANCELLED; 1174 goto out; 1175 } 1176 if (sc->sc_writelock != NULL || sc->sc_stopreportid != -1) { 1177 err = USBD_IN_USE; 1178 goto out; 1179 } 1180 sc->sc_writelock = (void *)1; /* XXX no lwp to attribute async xfer */ 1181 sc->sc_writereportid = scd->sc_report_id; 1182 sc->sc_writecallback = writecallback; 1183 sc->sc_writecookie = writecookie; 1184 usbd_setup_xfer(sc->sc_oxfer, sc, data, len, flags, timo, 1185 uhidev_write_callback); 1186 err = usbd_transfer(sc->sc_oxfer); 1187 switch (err) { 1188 case USBD_IN_PROGRESS: 1189 break; 1190 case USBD_NORMAL_COMPLETION: 1191 panic("unexpected normal completion of async xfer under lock"); 1192 default: /* error */ 1193 sc->sc_writelock = NULL; 1194 sc->sc_writereportid = -1; 1195 sc->sc_writecallback = NULL; 1196 sc->sc_writecookie = NULL; 1197 cv_broadcast(&sc->sc_cv); 1198 } 1199 out: mutex_exit(&sc->sc_lock); 1200 return err; 1201 } 1202 1203 static void 1204 uhidev_hidev_get_report_desc(void *cookie, void **desc, int *size) 1205 { 1206 uhidev_get_report_desc(cookie, desc, size); 1207 } 1208 1209 static int 1210 uhidev_hidev_open(void *cookie, void (*intr)(void *, void *, u_int), void *arg) 1211 { 1212 return uhidev_open(cookie, intr, arg); 1213 } 1214 1215 static void 1216 uhidev_hidev_stop(void *cookie) 1217 { 1218 uhidev_stop(cookie); 1219 } 1220 1221 static void 1222 uhidev_hidev_close(void *cookie) 1223 { 1224 uhidev_close(cookie); 1225 } 1226 1227 static usbd_status 1228 uhidev_hidev_set_report(void *cookie, int type, void *data, int len) 1229 { 1230 return uhidev_set_report(cookie, type, data, len); 1231 } 1232 1233 static usbd_status 1234 uhidev_hidev_get_report(void *cookie, int type, void *data, int len) 1235 { 1236 return uhidev_get_report(cookie, type, data, len); 1237 } 1238 1239 static usbd_status 1240 uhidev_hidev_write(void *cookie, void *data, int len) 1241 { 1242 return uhidev_write(cookie, data, len); 1243 } 1244 1245 static void 1246 uhidev_init_tag(struct uhidev *scd) 1247 { 1248 struct hidev_tag *t = &scd->sc_hidev; 1249 1250 t->_cookie = scd; 1251 t->_get_report_desc = uhidev_hidev_get_report_desc; 1252 t->_open = uhidev_hidev_open; 1253 t->_stop = uhidev_hidev_stop; 1254 t->_close = uhidev_hidev_close; 1255 t->_set_report = uhidev_hidev_set_report; 1256 t->_get_report = uhidev_hidev_get_report; 1257 t->_write = uhidev_hidev_write; 1258 } 1259