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