1 /* $OpenBSD: usb.c,v 1.134 2024/12/22 22:36:23 kirill Exp $ */ 2 /* $NetBSD: usb.c,v 1.77 2003/01/01 00:10:26 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 2002 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 * USB specifications and other documentation can be found at 36 * https://www.usb.org/documents 37 */ 38 39 #include "ohci.h" 40 #include "uhci.h" 41 #include "ehci.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 #include <sys/device.h> 47 #include <sys/kthread.h> 48 #include <sys/conf.h> 49 #include <sys/fcntl.h> 50 #include <sys/time.h> 51 #include <sys/rwlock.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usbdi.h> 55 56 #include <machine/bus.h> 57 58 #include <dev/usb/usbdivar.h> 59 #include <dev/usb/usb_mem.h> 60 #include <dev/usb/usbpcap.h> 61 62 #ifdef USB_DEBUG 63 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0) 64 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0) 65 int usbdebug = 0; 66 #if defined(UHCI_DEBUG) && NUHCI > 0 67 extern int uhcidebug; 68 #endif 69 #if defined(OHCI_DEBUG) && NOHCI > 0 70 extern int ohcidebug; 71 #endif 72 #if defined(EHCI_DEBUG) && NEHCI > 0 73 extern int ehcidebug; 74 #endif 75 /* 76 * 0 - do usual exploration 77 * !0 - do no exploration 78 */ 79 int usb_noexplore = 0; 80 #else 81 #define DPRINTF(x) 82 #define DPRINTFN(n,x) 83 #endif 84 85 struct usb_softc { 86 struct device sc_dev; /* base device */ 87 struct usbd_bus *sc_bus; /* USB controller */ 88 struct usbd_port sc_port; /* dummy port for root hub */ 89 int sc_speed; 90 91 struct usb_task sc_explore_task; 92 93 struct timeval sc_ptime; 94 }; 95 96 struct rwlock usbpalock; 97 98 TAILQ_HEAD(, usb_task) usb_abort_tasks; 99 TAILQ_HEAD(, usb_task) usb_explore_tasks; 100 TAILQ_HEAD(, usb_task) usb_generic_tasks; 101 102 static int usb_nbuses = 0; 103 static int usb_run_tasks, usb_run_abort_tasks; 104 int explore_pending; 105 const char *usbrev_str[] = USBREV_STR; 106 107 void usb_explore(void *); 108 void usb_create_task_threads(void *); 109 void usb_task_thread(void *); 110 struct proc *usb_task_thread_proc = NULL; 111 void usb_abort_task_thread(void *); 112 struct proc *usb_abort_task_thread_proc = NULL; 113 114 void usb_fill_udc_task(void *); 115 void usb_fill_udf_task(void *); 116 117 int usb_match(struct device *, void *, void *); 118 void usb_attach(struct device *, struct device *, void *); 119 int usb_detach(struct device *, int); 120 int usb_activate(struct device *, int); 121 122 int usb_attach_roothub(struct usb_softc *); 123 void usb_detach_roothub(struct usb_softc *); 124 125 struct cfdriver usb_cd = { 126 NULL, "usb", DV_DULL 127 }; 128 129 const struct cfattach usb_ca = { 130 sizeof(struct usb_softc), usb_match, usb_attach, usb_detach, 131 usb_activate, 132 }; 133 134 int 135 usb_match(struct device *parent, void *match, void *aux) 136 { 137 return (1); 138 } 139 140 void 141 usb_attach(struct device *parent, struct device *self, void *aux) 142 { 143 struct usb_softc *sc = (struct usb_softc *)self; 144 int usbrev; 145 146 if (usb_nbuses == 0) { 147 rw_init(&usbpalock, "usbpalock"); 148 TAILQ_INIT(&usb_abort_tasks); 149 TAILQ_INIT(&usb_explore_tasks); 150 TAILQ_INIT(&usb_generic_tasks); 151 usb_run_tasks = usb_run_abort_tasks = 1; 152 kthread_create_deferred(usb_create_task_threads, NULL); 153 } 154 usb_nbuses++; 155 156 sc->sc_bus = aux; 157 sc->sc_bus->usbctl = self; 158 sc->sc_port.power = USB_MAX_POWER; 159 160 usbrev = sc->sc_bus->usbrev; 161 printf(": USB revision %s", usbrev_str[usbrev]); 162 switch (usbrev) { 163 case USBREV_1_0: 164 case USBREV_1_1: 165 sc->sc_speed = USB_SPEED_FULL; 166 break; 167 case USBREV_2_0: 168 sc->sc_speed = USB_SPEED_HIGH; 169 break; 170 case USBREV_3_0: 171 sc->sc_speed = USB_SPEED_SUPER; 172 break; 173 default: 174 printf(", not supported\n"); 175 sc->sc_bus->dying = 1; 176 return; 177 } 178 printf("\n"); 179 180 #if NBPFILTER > 0 181 sc->sc_bus->bpfif = bpfsattach(&sc->sc_bus->bpf, sc->sc_dev.dv_xname, 182 DLT_USBPCAP, sizeof(struct usbpcap_pkt_hdr)); 183 #endif 184 185 /* Make sure not to use tsleep() if we are cold booting. */ 186 if (cold) 187 sc->sc_bus->use_polling++; 188 189 /* Don't let hub interrupts cause explore until ready. */ 190 sc->sc_bus->flags |= USB_BUS_CONFIG_PENDING; 191 192 /* explore task */ 193 usb_init_task(&sc->sc_explore_task, usb_explore, sc, 194 USB_TASK_TYPE_EXPLORE); 195 196 sc->sc_bus->soft = softintr_establish(IPL_SOFTUSB, 197 sc->sc_bus->methods->soft_intr, sc->sc_bus); 198 if (sc->sc_bus->soft == NULL) { 199 printf("%s: can't register softintr\n", sc->sc_dev.dv_xname); 200 sc->sc_bus->dying = 1; 201 return; 202 } 203 204 if (!usb_attach_roothub(sc)) { 205 struct usbd_device *dev = sc->sc_bus->root_hub; 206 #if 1 207 /* 208 * Turning this code off will delay attachment of USB devices 209 * until the USB task thread is running, which means that 210 * the keyboard will not work until after cold boot. 211 */ 212 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1)) 213 dev->hub->explore(sc->sc_bus->root_hub); 214 #endif 215 } 216 217 if (cold) 218 sc->sc_bus->use_polling--; 219 220 if (!sc->sc_bus->dying) { 221 getmicrouptime(&sc->sc_ptime); 222 if (sc->sc_bus->usbrev == USBREV_2_0) 223 explore_pending++; 224 config_pending_incr(); 225 usb_needs_explore(sc->sc_bus->root_hub, 1); 226 } 227 } 228 229 int 230 usb_attach_roothub(struct usb_softc *sc) 231 { 232 struct usbd_device *dev; 233 234 if (usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, sc->sc_speed, 0, 235 &sc->sc_port)) { 236 printf("%s: root hub problem\n", sc->sc_dev.dv_xname); 237 sc->sc_bus->dying = 1; 238 return (1); 239 } 240 241 dev = sc->sc_port.device; 242 if (dev->hub == NULL) { 243 printf("%s: root device is not a hub\n", sc->sc_dev.dv_xname); 244 sc->sc_bus->dying = 1; 245 return (1); 246 } 247 sc->sc_bus->root_hub = dev; 248 249 return (0); 250 } 251 252 void 253 usb_detach_roothub(struct usb_softc *sc) 254 { 255 /* 256 * To avoid races with the usb task thread, mark the root hub 257 * as disconnecting and schedule an exploration task to detach 258 * it. 259 */ 260 sc->sc_bus->flags |= USB_BUS_DISCONNECTING; 261 /* 262 * Reset the dying flag in case it has been set by the interrupt 263 * handler when unplugging an HC card otherwise the task won't be 264 * scheduled. This is safe since a dead HC should not trigger 265 * new interrupt. 266 */ 267 sc->sc_bus->dying = 0; 268 usb_needs_explore(sc->sc_bus->root_hub, 0); 269 270 usb_wait_task(sc->sc_bus->root_hub, &sc->sc_explore_task); 271 272 sc->sc_bus->root_hub = NULL; 273 } 274 275 void 276 usb_create_task_threads(void *arg) 277 { 278 if (kthread_create(usb_abort_task_thread, NULL, 279 &usb_abort_task_thread_proc, "usbatsk")) 280 panic("unable to create usb abort task thread"); 281 282 if (kthread_create(usb_task_thread, NULL, 283 &usb_task_thread_proc, "usbtask")) 284 panic("unable to create usb task thread"); 285 } 286 287 /* 288 * Add a task to be performed by the task thread. This function can be 289 * called from any context and the task will be executed in a process 290 * context ASAP. 291 */ 292 void 293 usb_add_task(struct usbd_device *dev, struct usb_task *task) 294 { 295 int s; 296 297 /* 298 * If the thread detaching ``dev'' is sleeping, waiting 299 * for all submitted transfers to finish, we must be able 300 * to enqueue abort tasks. Otherwise timeouts can't give 301 * back submitted transfers to the stack. 302 */ 303 if (usbd_is_dying(dev) && (task->type != USB_TASK_TYPE_ABORT)) 304 return; 305 306 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 307 task->state, task->type)); 308 309 s = splusb(); 310 if (!(task->state & USB_TASK_STATE_ONQ)) { 311 switch (task->type) { 312 case USB_TASK_TYPE_ABORT: 313 TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next); 314 break; 315 case USB_TASK_TYPE_EXPLORE: 316 TAILQ_INSERT_TAIL(&usb_explore_tasks, task, next); 317 break; 318 case USB_TASK_TYPE_GENERIC: 319 TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next); 320 break; 321 } 322 task->state |= USB_TASK_STATE_ONQ; 323 task->dev = dev; 324 } 325 if (task->type == USB_TASK_TYPE_ABORT) 326 wakeup(&usb_run_abort_tasks); 327 else 328 wakeup(&usb_run_tasks); 329 splx(s); 330 } 331 332 void 333 usb_rem_task(struct usbd_device *dev, struct usb_task *task) 334 { 335 int s; 336 337 if (!(task->state & USB_TASK_STATE_ONQ)) 338 return; 339 340 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 341 task->state, task->type)); 342 343 s = splusb(); 344 345 switch (task->type) { 346 case USB_TASK_TYPE_ABORT: 347 TAILQ_REMOVE(&usb_abort_tasks, task, next); 348 break; 349 case USB_TASK_TYPE_EXPLORE: 350 TAILQ_REMOVE(&usb_explore_tasks, task, next); 351 break; 352 case USB_TASK_TYPE_GENERIC: 353 TAILQ_REMOVE(&usb_generic_tasks, task, next); 354 break; 355 } 356 task->state &= ~USB_TASK_STATE_ONQ; 357 if (task->state == USB_TASK_STATE_NONE) 358 wakeup(task); 359 360 splx(s); 361 } 362 363 void 364 usb_wait_task(struct usbd_device *dev, struct usb_task *task) 365 { 366 int s; 367 368 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 369 task->state, task->type)); 370 371 if (task->state == USB_TASK_STATE_NONE) 372 return; 373 374 s = splusb(); 375 while (task->state != USB_TASK_STATE_NONE) { 376 DPRINTF(("%s: waiting for task to complete\n", __func__)); 377 tsleep_nsec(task, PWAIT, "endtask", INFSLP); 378 } 379 splx(s); 380 } 381 382 void 383 usb_rem_wait_task(struct usbd_device *dev, struct usb_task *task) 384 { 385 usb_rem_task(dev, task); 386 usb_wait_task(dev, task); 387 } 388 389 void 390 usb_task_thread(void *arg) 391 { 392 struct usb_task *task; 393 int s; 394 395 DPRINTF(("usb_task_thread: start\n")); 396 397 s = splusb(); 398 while (usb_run_tasks) { 399 if ((task = TAILQ_FIRST(&usb_explore_tasks)) != NULL) 400 TAILQ_REMOVE(&usb_explore_tasks, task, next); 401 else if ((task = TAILQ_FIRST(&usb_generic_tasks)) != NULL) 402 TAILQ_REMOVE(&usb_generic_tasks, task, next); 403 else { 404 tsleep_nsec(&usb_run_tasks, PWAIT, "usbtsk", INFSLP); 405 continue; 406 } 407 /* 408 * Set the state run bit before clearing the onq bit. 409 * This avoids state == none between dequeue and 410 * execution, which could cause usb_wait_task() to do 411 * the wrong thing. 412 */ 413 task->state |= USB_TASK_STATE_RUN; 414 task->state &= ~USB_TASK_STATE_ONQ; 415 /* Don't actually execute the task if dying. */ 416 if (!usbd_is_dying(task->dev)) { 417 splx(s); 418 task->fun(task->arg); 419 s = splusb(); 420 } 421 task->state &= ~USB_TASK_STATE_RUN; 422 if (task->state == USB_TASK_STATE_NONE) 423 wakeup(task); 424 } 425 splx(s); 426 427 kthread_exit(0); 428 } 429 430 /* 431 * This thread is ONLY for the HCI drivers to be able to abort xfers. 432 * Synchronous xfers sleep the task thread, so the aborts need to happen 433 * in a different thread. 434 */ 435 void 436 usb_abort_task_thread(void *arg) 437 { 438 struct usb_task *task; 439 int s; 440 441 DPRINTF(("usb_xfer_abort_thread: start\n")); 442 443 s = splusb(); 444 while (usb_run_abort_tasks) { 445 if ((task = TAILQ_FIRST(&usb_abort_tasks)) != NULL) 446 TAILQ_REMOVE(&usb_abort_tasks, task, next); 447 else { 448 tsleep_nsec(&usb_run_abort_tasks, PWAIT, "usbatsk", 449 INFSLP); 450 continue; 451 } 452 /* 453 * Set the state run bit before clearing the onq bit. 454 * This avoids state == none between dequeue and 455 * execution, which could cause usb_wait_task() to do 456 * the wrong thing. 457 */ 458 task->state |= USB_TASK_STATE_RUN; 459 task->state &= ~USB_TASK_STATE_ONQ; 460 splx(s); 461 task->fun(task->arg); 462 s = splusb(); 463 task->state &= ~USB_TASK_STATE_RUN; 464 if (task->state == USB_TASK_STATE_NONE) 465 wakeup(task); 466 } 467 splx(s); 468 469 kthread_exit(0); 470 } 471 472 int 473 usbctlprint(void *aux, const char *pnp) 474 { 475 /* only "usb"es can attach to host controllers */ 476 if (pnp) 477 printf("usb at %s", pnp); 478 479 return (UNCONF); 480 } 481 482 int 483 usbopen(dev_t dev, int flag, int mode, struct proc *p) 484 { 485 int unit = minor(dev); 486 struct usb_softc *sc; 487 488 if (unit >= usb_cd.cd_ndevs) 489 return (ENXIO); 490 sc = usb_cd.cd_devs[unit]; 491 if (sc == NULL) 492 return (ENXIO); 493 494 if (sc->sc_bus->dying) 495 return (EIO); 496 497 return (0); 498 } 499 500 int 501 usbclose(dev_t dev, int flag, int mode, struct proc *p) 502 { 503 return (0); 504 } 505 506 void 507 usb_fill_udc_task(void *arg) 508 { 509 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)arg; 510 struct usb_softc *sc; 511 struct usbd_device *dev; 512 int addr = udc->udc_addr, cdesc_len; 513 usb_config_descriptor_t *cdesc; 514 515 /* check that the bus and device are still present */ 516 if (udc->udc_bus >= usb_cd.cd_ndevs) 517 return; 518 sc = usb_cd.cd_devs[udc->udc_bus]; 519 if (sc == NULL) 520 return; 521 dev = sc->sc_bus->devices[udc->udc_addr]; 522 if (dev == NULL) 523 return; 524 525 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 526 udc->udc_config_index, &cdesc_len); 527 if (cdesc == NULL) 528 return; 529 udc->udc_desc = *cdesc; 530 free(cdesc, M_TEMP, cdesc_len); 531 } 532 533 void 534 usb_fill_udf_task(void *arg) 535 { 536 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)arg; 537 struct usb_softc *sc; 538 struct usbd_device *dev; 539 int addr = udf->udf_addr; 540 usb_config_descriptor_t *cdesc; 541 542 /* check that the bus and device are still present */ 543 if (udf->udf_bus >= usb_cd.cd_ndevs) 544 return; 545 sc = usb_cd.cd_devs[udf->udf_bus]; 546 if (sc == NULL) 547 return; 548 dev = sc->sc_bus->devices[udf->udf_addr]; 549 if (dev == NULL) 550 return; 551 552 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 553 udf->udf_config_index, &udf->udf_size); 554 udf->udf_data = (char *)cdesc; 555 } 556 557 int 558 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p) 559 { 560 struct usb_softc *sc; 561 int unit = minor(devt); 562 int error; 563 564 sc = usb_cd.cd_devs[unit]; 565 566 if (sc->sc_bus->dying) 567 return (EIO); 568 569 error = 0; 570 switch (cmd) { 571 #ifdef USB_DEBUG 572 case USB_SETDEBUG: 573 /* only root can access to these debug flags */ 574 if ((error = suser(curproc)) != 0) 575 return (error); 576 if (!(flag & FWRITE)) 577 return (EBADF); 578 usbdebug = ((*(unsigned int *)data) & 0x000000ff); 579 #if defined(UHCI_DEBUG) && NUHCI > 0 580 uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8; 581 #endif 582 #if defined(OHCI_DEBUG) && NOHCI > 0 583 ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16; 584 #endif 585 #if defined(EHCI_DEBUG) && NEHCI > 0 586 ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24; 587 #endif 588 break; 589 #endif /* USB_DEBUG */ 590 case USB_REQUEST: 591 { 592 struct usb_ctl_request *ur = (void *)data; 593 size_t len = UGETW(ur->ucr_request.wLength), mlen; 594 struct iovec iov; 595 struct uio uio; 596 void *ptr = NULL; 597 int addr = ur->ucr_addr; 598 usbd_status err; 599 600 if (!(flag & FWRITE)) 601 return (EBADF); 602 603 DPRINTF(("%s: USB_REQUEST addr=%d len=%zu\n", __func__, addr, len)); 604 /* Avoid requests that would damage the bus integrity. */ 605 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 606 ur->ucr_request.bRequest == UR_SET_ADDRESS) || 607 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 608 ur->ucr_request.bRequest == UR_SET_CONFIG) || 609 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE && 610 ur->ucr_request.bRequest == UR_SET_INTERFACE)) 611 return (EINVAL); 612 613 if (len > 32767) 614 return (EINVAL); 615 if (addr < 0 || addr >= USB_MAX_DEVICES) 616 return (EINVAL); 617 if (sc->sc_bus->devices[addr] == NULL) 618 return (ENXIO); 619 if (len != 0) { 620 iov.iov_base = (caddr_t)ur->ucr_data; 621 iov.iov_len = len; 622 uio.uio_iov = &iov; 623 uio.uio_iovcnt = 1; 624 uio.uio_resid = len; 625 uio.uio_offset = 0; 626 uio.uio_segflg = UIO_USERSPACE; 627 uio.uio_rw = 628 ur->ucr_request.bmRequestType & UT_READ ? 629 UIO_READ : UIO_WRITE; 630 uio.uio_procp = p; 631 if ((ptr = malloc(len, M_TEMP, M_NOWAIT)) == NULL) { 632 error = ENOMEM; 633 goto ret; 634 } 635 if (uio.uio_rw == UIO_WRITE) { 636 error = uiomove(ptr, len, &uio); 637 if (error) 638 goto ret; 639 } 640 } 641 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 642 &ur->ucr_request, ptr, ur->ucr_flags, 643 &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT); 644 if (err) { 645 error = EIO; 646 goto ret; 647 } 648 /* Only if USBD_SHORT_XFER_OK is set. */ 649 mlen = len; 650 if (mlen > ur->ucr_actlen) 651 mlen = ur->ucr_actlen; 652 if (mlen != 0) { 653 if (uio.uio_rw == UIO_READ) { 654 error = uiomove(ptr, mlen, &uio); 655 if (error) 656 goto ret; 657 } 658 } 659 ret: 660 free(ptr, M_TEMP, len); 661 return (error); 662 } 663 664 case USB_DEVICEINFO: 665 { 666 struct usb_device_info *di = (void *)data; 667 int addr = di->udi_addr; 668 struct usbd_device *dev; 669 670 if (addr < 1 || addr >= USB_MAX_DEVICES) 671 return (EINVAL); 672 673 dev = sc->sc_bus->devices[addr]; 674 if (dev == NULL) 675 return (ENXIO); 676 677 usbd_fill_deviceinfo(dev, di); 678 break; 679 } 680 681 case USB_DEVICESTATS: 682 *(struct usb_device_stats *)data = sc->sc_bus->stats; 683 break; 684 685 case USB_DEVICE_GET_DDESC: 686 { 687 struct usb_device_ddesc *udd = (struct usb_device_ddesc *)data; 688 int addr = udd->udd_addr; 689 struct usbd_device *dev; 690 691 if (addr < 1 || addr >= USB_MAX_DEVICES) 692 return (EINVAL); 693 694 dev = sc->sc_bus->devices[addr]; 695 if (dev == NULL) 696 return (ENXIO); 697 698 udd->udd_bus = unit; 699 700 udd->udd_desc = *usbd_get_device_descriptor(dev); 701 break; 702 } 703 704 case USB_DEVICE_GET_CDESC: 705 { 706 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)data; 707 int addr = udc->udc_addr; 708 struct usb_task udc_task; 709 710 if (addr < 1 || addr >= USB_MAX_DEVICES) 711 return (EINVAL); 712 if (sc->sc_bus->devices[addr] == NULL) 713 return (ENXIO); 714 715 udc->udc_bus = unit; 716 717 udc->udc_desc.bLength = 0; 718 usb_init_task(&udc_task, usb_fill_udc_task, udc, 719 USB_TASK_TYPE_GENERIC); 720 usb_add_task(sc->sc_bus->root_hub, &udc_task); 721 usb_wait_task(sc->sc_bus->root_hub, &udc_task); 722 if (udc->udc_desc.bLength == 0) 723 return (EINVAL); 724 break; 725 } 726 727 case USB_DEVICE_GET_FDESC: 728 { 729 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)data; 730 int addr = udf->udf_addr; 731 struct usb_task udf_task; 732 struct usb_device_fdesc save_udf; 733 usb_config_descriptor_t *cdesc; 734 struct iovec iov; 735 struct uio uio; 736 size_t len, cdesc_len; 737 738 if (addr < 1 || addr >= USB_MAX_DEVICES) 739 return (EINVAL); 740 if (sc->sc_bus->devices[addr] == NULL) 741 return (ENXIO); 742 743 udf->udf_bus = unit; 744 745 save_udf = *udf; 746 udf->udf_data = NULL; 747 usb_init_task(&udf_task, usb_fill_udf_task, udf, 748 USB_TASK_TYPE_GENERIC); 749 usb_add_task(sc->sc_bus->root_hub, &udf_task); 750 usb_wait_task(sc->sc_bus->root_hub, &udf_task); 751 len = cdesc_len = udf->udf_size; 752 cdesc = (usb_config_descriptor_t *)udf->udf_data; 753 *udf = save_udf; 754 if (cdesc == NULL) 755 return (EINVAL); 756 if (len > udf->udf_size) 757 len = udf->udf_size; 758 iov.iov_base = (caddr_t)udf->udf_data; 759 iov.iov_len = len; 760 uio.uio_iov = &iov; 761 uio.uio_iovcnt = 1; 762 uio.uio_resid = len; 763 uio.uio_offset = 0; 764 uio.uio_segflg = UIO_USERSPACE; 765 uio.uio_rw = UIO_READ; 766 uio.uio_procp = p; 767 error = uiomove((void *)cdesc, len, &uio); 768 free(cdesc, M_TEMP, cdesc_len); 769 return (error); 770 } 771 772 default: 773 return (EINVAL); 774 } 775 return (0); 776 } 777 778 /* 779 * Explore device tree from the root. We need mutual exclusion to this 780 * hub while traversing the device tree, but this is guaranteed since this 781 * function is only called from the task thread, with one exception: 782 * usb_attach() calls this function, but there shouldn't be anything else 783 * trying to explore this hub at that time. 784 */ 785 void 786 usb_explore(void *v) 787 { 788 struct usb_softc *sc = v; 789 struct timeval now, waited; 790 int pwrdly, waited_ms; 791 792 DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname)); 793 #ifdef USB_DEBUG 794 if (usb_noexplore) 795 return; 796 #endif 797 798 if (sc->sc_bus->dying) 799 return; 800 801 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 802 /* 803 * If this is a low/full speed hub and there is a high 804 * speed hub that hasn't explored yet, reschedule this 805 * task, allowing the high speed explore task to run. 806 */ 807 if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) { 808 usb_add_task(sc->sc_bus->root_hub, 809 &sc->sc_explore_task); 810 return; 811 } 812 813 /* 814 * Wait for power to stabilize. 815 */ 816 getmicrouptime(&now); 817 timersub(&now, &sc->sc_ptime, &waited); 818 waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000; 819 820 pwrdly = sc->sc_bus->root_hub->hub->powerdelay + 821 USB_EXTRA_POWER_UP_TIME; 822 if (pwrdly > waited_ms) 823 usb_delay_ms(sc->sc_bus, pwrdly - waited_ms); 824 } 825 826 if (sc->sc_bus->flags & USB_BUS_DISCONNECTING) { 827 /* Prevent new tasks from being scheduled. */ 828 sc->sc_bus->dying = 1; 829 830 /* Make all devices disconnect. */ 831 if (sc->sc_port.device != NULL) { 832 usbd_detach(sc->sc_port.device, (struct device *)sc); 833 sc->sc_port.device = NULL; 834 } 835 836 sc->sc_bus->flags &= ~USB_BUS_DISCONNECTING; 837 } else { 838 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 839 } 840 841 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 842 DPRINTF(("%s: %s: first explore done\n", __func__, 843 sc->sc_dev.dv_xname)); 844 if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending) 845 explore_pending--; 846 config_pending_decr(); 847 sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING); 848 } 849 } 850 851 void 852 usb_needs_explore(struct usbd_device *dev, int first_explore) 853 { 854 struct usb_softc *usbctl = (struct usb_softc *)dev->bus->usbctl; 855 856 DPRINTFN(3,("%s: %s\n", usbctl->sc_dev.dv_xname, __func__)); 857 858 if (!first_explore && (dev->bus->flags & USB_BUS_CONFIG_PENDING)) { 859 DPRINTF(("%s: %s: not exploring before first explore\n", 860 __func__, usbctl->sc_dev.dv_xname)); 861 return; 862 } 863 864 usb_add_task(dev, &usbctl->sc_explore_task); 865 } 866 867 void 868 usb_needs_reattach(struct usbd_device *dev) 869 { 870 DPRINTFN(2,("usb_needs_reattach\n")); 871 dev->powersrc->reattach = 1; 872 usb_needs_explore(dev, 0); 873 } 874 875 void 876 usb_schedsoftintr(struct usbd_bus *bus) 877 { 878 DPRINTFN(10,("%s: polling=%d\n", __func__, bus->use_polling)); 879 880 /* In case usb(4) is disabled */ 881 if (bus->soft == NULL) 882 return; 883 884 if (bus->use_polling) { 885 bus->methods->soft_intr(bus); 886 } else { 887 softintr_schedule(bus->soft); 888 } 889 } 890 891 int 892 usb_activate(struct device *self, int act) 893 { 894 struct usb_softc *sc = (struct usb_softc *)self; 895 int rv = 0; 896 897 switch (act) { 898 case DVACT_QUIESCE: 899 if (sc->sc_bus->root_hub != NULL) 900 usb_detach_roothub(sc); 901 break; 902 case DVACT_RESUME: 903 sc->sc_bus->dying = 0; 904 break; 905 case DVACT_WAKEUP: 906 sc->sc_bus->use_polling++; 907 if (!usb_attach_roothub(sc)) 908 usb_needs_explore(sc->sc_bus->root_hub, 0); 909 sc->sc_bus->use_polling--; 910 break; 911 default: 912 rv = config_activate_children(self, act); 913 break; 914 } 915 return (rv); 916 } 917 918 int 919 usb_detach(struct device *self, int flags) 920 { 921 struct usb_softc *sc = (struct usb_softc *)self; 922 923 if (sc->sc_bus->root_hub != NULL) { 924 usb_detach_roothub(sc); 925 926 if (--usb_nbuses == 0) { 927 usb_run_tasks = usb_run_abort_tasks = 0; 928 wakeup(&usb_run_abort_tasks); 929 wakeup(&usb_run_tasks); 930 } 931 } 932 933 if (sc->sc_bus->soft != NULL) { 934 softintr_disestablish(sc->sc_bus->soft); 935 sc->sc_bus->soft = NULL; 936 } 937 938 #if NBPFILTER > 0 939 bpfsdetach(sc->sc_bus->bpfif); 940 #endif 941 return (0); 942 } 943 944 void 945 usb_tap(struct usbd_bus *bus, struct usbd_xfer *xfer, uint8_t dir) 946 { 947 #if NBPFILTER > 0 948 struct usb_softc *sc = (struct usb_softc *)bus->usbctl; 949 usb_endpoint_descriptor_t *ed = xfer->pipe->endpoint->edesc; 950 union { 951 struct usbpcap_ctl_hdr uch; 952 struct usbpcap_iso_hdr_full uih; 953 } h; 954 struct usbpcap_pkt_hdr *uph = &h.uch.uch_hdr; 955 uint32_t nframes, psize; 956 unsigned int bpfdir; 957 void *data = NULL; 958 size_t flen; 959 caddr_t bpf; 960 int i; 961 962 bpf = bus->bpf; 963 if (bpf == NULL) 964 return; 965 966 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 967 case UE_CONTROL: 968 /* Control transfer headers include an extra byte */ 969 uph->uph_hlen = htole16(sizeof(struct usbpcap_ctl_hdr)); 970 uph->uph_xfertype = USBPCAP_TRANSFER_CONTROL; 971 break; 972 case UE_ISOCHRONOUS: 973 nframes = xfer->nframes; 974 /* 975 * All our drivers use a fixed size (psize) for 976 * ISOCHRONOUS packets. Calculate it to determine the 977 * correct offset below. 978 */ 979 psize = xfer->length / nframes; 980 #ifdef DIAGNOSTIC 981 if (nframes > _USBPCAP_MAX_ISOFRAMES) { 982 printf("%s: too many frames: %d > %d\n", __func__, 983 xfer->nframes, _USBPCAP_MAX_ISOFRAMES); 984 nframes = _USBPCAP_MAX_ISOFRAMES; 985 } 986 #endif 987 /* Isochronous transfer headers include space for one frame */ 988 flen = (nframes - 1) * sizeof(struct usbpcap_iso_pkt); 989 uph->uph_hlen = htole16(sizeof(struct usbpcap_iso_hdr) + flen); 990 uph->uph_xfertype = USBPCAP_TRANSFER_ISOCHRONOUS; 991 h.uih.uih_startframe = 0; /* not yet used */ 992 h.uih.uih_nframes = nframes; 993 h.uih.uih_errors = 0; /* we don't have per-frame error */ 994 for (i = 0; i < nframes; i++) { 995 /* 996 * We can't use length, because IN frame may 997 * have shorter length of packet whan expected. 998 */ 999 h.uih.uih_frames[i].uip_offset = i * psize; 1000 h.uih.uih_frames[i].uip_length = xfer->frlengths[i]; 1001 /* See above, we don't have per-frame error */ 1002 h.uih.uih_frames[i].uip_status = 0; 1003 } 1004 break; 1005 case UE_BULK: 1006 uph->uph_hlen = htole16(sizeof(*uph)); 1007 uph->uph_xfertype = USBPCAP_TRANSFER_BULK; 1008 break; 1009 case UE_INTERRUPT: 1010 uph->uph_hlen = htole16(sizeof(*uph)); 1011 uph->uph_xfertype = USBPCAP_TRANSFER_INTERRUPT; 1012 break; 1013 default: 1014 return; 1015 } 1016 1017 uph->uph_id = 0; /* not yet used */ 1018 uph->uph_status = htole32(xfer->status); 1019 uph->uph_function = 0; /* not yet used */ 1020 uph->uph_bus = htole32(sc->sc_dev.dv_unit); 1021 uph->uph_devaddr = htole16(xfer->device->address); 1022 uph->uph_epaddr = ed->bEndpointAddress; 1023 uph->uph_info = 0; 1024 1025 /* Outgoing control requests start with a STAGE dump. */ 1026 if ((xfer->rqflags & URQ_REQUEST) && (dir == USBTAP_DIR_OUT)) { 1027 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_SETUP; 1028 uph->uph_dlen = sizeof(usb_device_request_t); 1029 bpf_tap_hdr(bpf, uph, uph->uph_hlen, &xfer->request, 1030 uph->uph_dlen, BPF_DIRECTION_OUT); 1031 } 1032 1033 if (dir == USBTAP_DIR_OUT) { 1034 bpfdir = BPF_DIRECTION_OUT; 1035 if (!usbd_xfer_isread(xfer)) { 1036 data = KERNADDR(&xfer->dmabuf, 0); 1037 uph->uph_dlen = xfer->length; 1038 if (xfer->rqflags & URQ_REQUEST) 1039 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_DATA; 1040 } else { 1041 data = NULL; 1042 uph->uph_dlen = 0; 1043 if (xfer->rqflags & URQ_REQUEST) 1044 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1045 } 1046 } else { /* USBTAP_DIR_IN */ 1047 bpfdir = BPF_DIRECTION_IN; 1048 uph->uph_info = USBPCAP_INFO_DIRECTION_IN; 1049 if (usbd_xfer_isread(xfer)) { 1050 data = KERNADDR(&xfer->dmabuf, 0); 1051 uph->uph_dlen = xfer->actlen; 1052 if (xfer->rqflags & URQ_REQUEST) 1053 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_DATA; 1054 } else { 1055 data = NULL; 1056 uph->uph_dlen = 0; 1057 if (xfer->rqflags & URQ_REQUEST) 1058 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1059 } 1060 } 1061 1062 /* ISOCHRONOUS IN from device may have gaps, use full buffer */ 1063 if (bpfdir == BPF_DIRECTION_IN && uph->uph_dlen > 0 && 1064 uph->uph_xfertype == USBPCAP_TRANSFER_ISOCHRONOUS) { 1065 uph->uph_dlen = xfer->length; 1066 } 1067 1068 /* Dump bulk/intr/iso data, ctrl DATA or STATUS stage. */ 1069 bpf_tap_hdr(bpf, uph, uph->uph_hlen, data, uph->uph_dlen, bpfdir); 1070 1071 /* Incoming control requests with DATA need a STATUS stage. */ 1072 if ((xfer->rqflags & URQ_REQUEST) && (dir == USBTAP_DIR_IN) && 1073 (h.uch.uch_stage == USBPCAP_CONTROL_STAGE_DATA)) { 1074 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1075 uph->uph_dlen = 0; 1076 bpf_tap_hdr(bpf, uph, uph->uph_hlen, NULL, 0, BPF_DIRECTION_IN); 1077 } 1078 #endif 1079 } 1080