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