1 /* $OpenBSD: usb.c,v 1.123 2019/01/09 12:10:37 mpi 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(task, PWAIT, "endtask", 0); 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(&usb_run_tasks, PWAIT, "usbtsk", 0); 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(&usb_run_abort_tasks, PWAIT, "usbatsk", 0); 456 continue; 457 } 458 /* 459 * Set the state run bit before clearing the onq bit. 460 * This avoids state == none between dequeue and 461 * execution, which could cause usb_wait_task() to do 462 * the wrong thing. 463 */ 464 task->state |= USB_TASK_STATE_RUN; 465 task->state &= ~USB_TASK_STATE_ONQ; 466 splx(s); 467 task->fun(task->arg); 468 s = splusb(); 469 task->state &= ~USB_TASK_STATE_RUN; 470 if (task->state == USB_TASK_STATE_NONE) 471 wakeup(task); 472 } 473 splx(s); 474 475 kthread_exit(0); 476 } 477 478 int 479 usbctlprint(void *aux, const char *pnp) 480 { 481 /* only "usb"es can attach to host controllers */ 482 if (pnp) 483 printf("usb at %s", pnp); 484 485 return (UNCONF); 486 } 487 488 int 489 usbopen(dev_t dev, int flag, int mode, struct proc *p) 490 { 491 int unit = minor(dev); 492 struct usb_softc *sc; 493 494 if (unit >= usb_cd.cd_ndevs) 495 return (ENXIO); 496 sc = usb_cd.cd_devs[unit]; 497 if (sc == NULL) 498 return (ENXIO); 499 500 if (sc->sc_bus->dying) 501 return (EIO); 502 503 return (0); 504 } 505 506 int 507 usbclose(dev_t dev, int flag, int mode, struct proc *p) 508 { 509 return (0); 510 } 511 512 void 513 usb_fill_udc_task(void *arg) 514 { 515 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)arg; 516 struct usb_softc *sc; 517 struct usbd_device *dev; 518 int addr = udc->udc_addr; 519 usb_config_descriptor_t *cdesc; 520 521 /* check that the bus and device are still present */ 522 if (udc->udc_bus >= usb_cd.cd_ndevs) 523 return; 524 sc = usb_cd.cd_devs[udc->udc_bus]; 525 if (sc == NULL) 526 return; 527 dev = sc->sc_bus->devices[udc->udc_addr]; 528 if (dev == NULL) 529 return; 530 531 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 532 udc->udc_config_index, 0); 533 if (cdesc == NULL) 534 return; 535 udc->udc_desc = *cdesc; 536 free(cdesc, M_TEMP, UGETW(cdesc->wTotalLength)); 537 } 538 539 void 540 usb_fill_udf_task(void *arg) 541 { 542 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)arg; 543 struct usb_softc *sc; 544 struct usbd_device *dev; 545 int addr = udf->udf_addr; 546 usb_config_descriptor_t *cdesc; 547 548 /* check that the bus and device are still present */ 549 if (udf->udf_bus >= usb_cd.cd_ndevs) 550 return; 551 sc = usb_cd.cd_devs[udf->udf_bus]; 552 if (sc == NULL) 553 return; 554 dev = sc->sc_bus->devices[udf->udf_addr]; 555 if (dev == NULL) 556 return; 557 558 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 559 udf->udf_config_index, &udf->udf_size); 560 udf->udf_data = (char *)cdesc; 561 } 562 563 int 564 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p) 565 { 566 struct usb_softc *sc; 567 int unit = minor(devt); 568 int error; 569 570 sc = usb_cd.cd_devs[unit]; 571 572 if (sc->sc_bus->dying) 573 return (EIO); 574 575 error = 0; 576 switch (cmd) { 577 #ifdef USB_DEBUG 578 case USB_SETDEBUG: 579 /* only root can access to these debug flags */ 580 if ((error = suser(curproc)) != 0) 581 return (error); 582 if (!(flag & FWRITE)) 583 return (EBADF); 584 usbdebug = ((*(unsigned int *)data) & 0x000000ff); 585 #if defined(UHCI_DEBUG) && NUHCI > 0 586 uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8; 587 #endif 588 #if defined(OHCI_DEBUG) && NOHCI > 0 589 ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16; 590 #endif 591 #if defined(EHCI_DEBUG) && NEHCI > 0 592 ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24; 593 #endif 594 break; 595 #endif /* USB_DEBUG */ 596 case USB_REQUEST: 597 { 598 struct usb_ctl_request *ur = (void *)data; 599 size_t len = UGETW(ur->ucr_request.wLength), mlen; 600 struct iovec iov; 601 struct uio uio; 602 void *ptr = NULL; 603 int addr = ur->ucr_addr; 604 usbd_status err; 605 606 if (!(flag & FWRITE)) 607 return (EBADF); 608 609 DPRINTF(("%s: USB_REQUEST addr=%d len=%zu\n", __func__, addr, len)); 610 /* Avoid requests that would damage the bus integrity. */ 611 if ((ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 612 ur->ucr_request.bRequest == UR_SET_ADDRESS) || 613 (ur->ucr_request.bmRequestType == UT_WRITE_DEVICE && 614 ur->ucr_request.bRequest == UR_SET_CONFIG) || 615 (ur->ucr_request.bmRequestType == UT_WRITE_INTERFACE && 616 ur->ucr_request.bRequest == UR_SET_INTERFACE)) 617 return (EINVAL); 618 619 if (len > 32767) 620 return (EINVAL); 621 if (addr < 0 || addr >= USB_MAX_DEVICES) 622 return (EINVAL); 623 if (sc->sc_bus->devices[addr] == NULL) 624 return (ENXIO); 625 if (len != 0) { 626 iov.iov_base = (caddr_t)ur->ucr_data; 627 iov.iov_len = len; 628 uio.uio_iov = &iov; 629 uio.uio_iovcnt = 1; 630 uio.uio_resid = len; 631 uio.uio_offset = 0; 632 uio.uio_segflg = UIO_USERSPACE; 633 uio.uio_rw = 634 ur->ucr_request.bmRequestType & UT_READ ? 635 UIO_READ : UIO_WRITE; 636 uio.uio_procp = p; 637 if ((ptr = malloc(len, M_TEMP, M_NOWAIT)) == NULL) { 638 error = ENOMEM; 639 goto ret; 640 } 641 if (uio.uio_rw == UIO_WRITE) { 642 error = uiomove(ptr, len, &uio); 643 if (error) 644 goto ret; 645 } 646 } 647 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 648 &ur->ucr_request, ptr, ur->ucr_flags, 649 &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT); 650 if (err) { 651 error = EIO; 652 goto ret; 653 } 654 /* Only if USBD_SHORT_XFER_OK is set. */ 655 mlen = len; 656 if (mlen > ur->ucr_actlen) 657 mlen = ur->ucr_actlen; 658 if (mlen != 0) { 659 if (uio.uio_rw == UIO_READ) { 660 error = uiomove(ptr, mlen, &uio); 661 if (error) 662 goto ret; 663 } 664 } 665 ret: 666 free(ptr, M_TEMP, len); 667 return (error); 668 } 669 670 case USB_DEVICEINFO: 671 { 672 struct usb_device_info *di = (void *)data; 673 int addr = di->udi_addr; 674 struct usbd_device *dev; 675 676 if (addr < 1 || addr >= USB_MAX_DEVICES) 677 return (EINVAL); 678 679 dev = sc->sc_bus->devices[addr]; 680 if (dev == NULL) 681 return (ENXIO); 682 683 usbd_fill_deviceinfo(dev, di); 684 break; 685 } 686 687 case USB_DEVICESTATS: 688 *(struct usb_device_stats *)data = sc->sc_bus->stats; 689 break; 690 691 case USB_DEVICE_GET_DDESC: 692 { 693 struct usb_device_ddesc *udd = (struct usb_device_ddesc *)data; 694 int addr = udd->udd_addr; 695 struct usbd_device *dev; 696 697 if (addr < 1 || addr >= USB_MAX_DEVICES) 698 return (EINVAL); 699 700 dev = sc->sc_bus->devices[addr]; 701 if (dev == NULL) 702 return (ENXIO); 703 704 udd->udd_bus = unit; 705 706 udd->udd_desc = *usbd_get_device_descriptor(dev); 707 break; 708 } 709 710 case USB_DEVICE_GET_CDESC: 711 { 712 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)data; 713 int addr = udc->udc_addr; 714 struct usb_task udc_task; 715 716 if (addr < 1 || addr >= USB_MAX_DEVICES) 717 return (EINVAL); 718 if (sc->sc_bus->devices[addr] == NULL) 719 return (ENXIO); 720 721 udc->udc_bus = unit; 722 723 udc->udc_desc.bLength = 0; 724 usb_init_task(&udc_task, usb_fill_udc_task, udc, 725 USB_TASK_TYPE_GENERIC); 726 usb_add_task(sc->sc_bus->root_hub, &udc_task); 727 usb_wait_task(sc->sc_bus->root_hub, &udc_task); 728 if (udc->udc_desc.bLength == 0) 729 return (EINVAL); 730 break; 731 } 732 733 case USB_DEVICE_GET_FDESC: 734 { 735 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)data; 736 int addr = udf->udf_addr; 737 struct usb_task udf_task; 738 struct usb_device_fdesc save_udf; 739 usb_config_descriptor_t *cdesc; 740 struct iovec iov; 741 struct uio uio; 742 size_t len; 743 744 if (addr < 1 || addr >= USB_MAX_DEVICES) 745 return (EINVAL); 746 if (sc->sc_bus->devices[addr] == NULL) 747 return (ENXIO); 748 749 udf->udf_bus = unit; 750 751 save_udf = *udf; 752 udf->udf_data = NULL; 753 usb_init_task(&udf_task, usb_fill_udf_task, udf, 754 USB_TASK_TYPE_GENERIC); 755 usb_add_task(sc->sc_bus->root_hub, &udf_task); 756 usb_wait_task(sc->sc_bus->root_hub, &udf_task); 757 len = udf->udf_size; 758 cdesc = (usb_config_descriptor_t *)udf->udf_data; 759 *udf = save_udf; 760 if (cdesc == NULL) 761 return (EINVAL); 762 if (len > udf->udf_size) 763 len = udf->udf_size; 764 iov.iov_base = (caddr_t)udf->udf_data; 765 iov.iov_len = len; 766 uio.uio_iov = &iov; 767 uio.uio_iovcnt = 1; 768 uio.uio_resid = len; 769 uio.uio_offset = 0; 770 uio.uio_segflg = UIO_USERSPACE; 771 uio.uio_rw = UIO_READ; 772 uio.uio_procp = p; 773 error = uiomove((void *)cdesc, len, &uio); 774 free(cdesc, M_TEMP, UGETW(cdesc->wTotalLength)); 775 return (error); 776 } 777 778 default: 779 return (EINVAL); 780 } 781 return (0); 782 } 783 784 /* 785 * Explore device tree from the root. We need mutual exclusion to this 786 * hub while traversing the device tree, but this is guaranteed since this 787 * function is only called from the task thread, with one exception: 788 * usb_attach() calls this function, but there shouldn't be anything else 789 * trying to explore this hub at that time. 790 */ 791 void 792 usb_explore(void *v) 793 { 794 struct usb_softc *sc = v; 795 struct timeval now, waited; 796 int pwrdly, waited_ms; 797 798 DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname)); 799 #ifdef USB_DEBUG 800 if (usb_noexplore) 801 return; 802 #endif 803 804 if (sc->sc_bus->dying) 805 return; 806 807 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 808 /* 809 * If this is a low/full speed hub and there is a high 810 * speed hub that hasn't explored yet, reshedule this 811 * task, allowing the high speed explore task to run. 812 */ 813 if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) { 814 usb_add_task(sc->sc_bus->root_hub, 815 &sc->sc_explore_task); 816 return; 817 } 818 819 /* 820 * Wait for power to stabilize. 821 */ 822 getmicrouptime(&now); 823 timersub(&now, &sc->sc_ptime, &waited); 824 waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000; 825 826 pwrdly = sc->sc_bus->root_hub->hub->powerdelay + 827 USB_EXTRA_POWER_UP_TIME; 828 if (pwrdly > waited_ms) 829 usb_delay_ms(sc->sc_bus, pwrdly - waited_ms); 830 } 831 832 if (sc->sc_bus->flags & USB_BUS_DISCONNECTING) { 833 /* Prevent new tasks from being scheduled. */ 834 sc->sc_bus->dying = 1; 835 836 /* Make all devices disconnect. */ 837 if (sc->sc_port.device != NULL) { 838 usbd_detach(sc->sc_port.device, (struct device *)sc); 839 sc->sc_port.device = NULL; 840 } 841 842 sc->sc_bus->flags &= ~USB_BUS_DISCONNECTING; 843 } else { 844 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 845 } 846 847 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 848 DPRINTF(("%s: %s: first explore done\n", __func__, 849 sc->sc_dev.dv_xname)); 850 if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending) 851 explore_pending--; 852 config_pending_decr(); 853 sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING); 854 } 855 } 856 857 void 858 usb_needs_explore(struct usbd_device *dev, int first_explore) 859 { 860 struct usb_softc *usbctl = (struct usb_softc *)dev->bus->usbctl; 861 862 DPRINTFN(3,("%s: %s\n", usbctl->sc_dev.dv_xname, __func__)); 863 864 if (!first_explore && (dev->bus->flags & USB_BUS_CONFIG_PENDING)) { 865 DPRINTF(("%s: %s: not exploring before first explore\n", 866 __func__, usbctl->sc_dev.dv_xname)); 867 return; 868 } 869 870 usb_add_task(dev, &usbctl->sc_explore_task); 871 } 872 873 void 874 usb_needs_reattach(struct usbd_device *dev) 875 { 876 DPRINTFN(2,("usb_needs_reattach\n")); 877 dev->powersrc->reattach = 1; 878 usb_needs_explore(dev, 0); 879 } 880 881 void 882 usb_schedsoftintr(struct usbd_bus *bus) 883 { 884 DPRINTFN(10,("%s: polling=%d\n", __func__, bus->use_polling)); 885 886 if (bus->use_polling) { 887 bus->methods->soft_intr(bus); 888 } else { 889 softintr_schedule(bus->soft); 890 } 891 } 892 893 int 894 usb_activate(struct device *self, int act) 895 { 896 struct usb_softc *sc = (struct usb_softc *)self; 897 int rv = 0; 898 899 switch (act) { 900 case DVACT_QUIESCE: 901 if (sc->sc_bus->root_hub != NULL) 902 usb_detach_roothub(sc); 903 break; 904 case DVACT_RESUME: 905 sc->sc_bus->dying = 0; 906 907 /* 908 * Make sure the root hub is present before interrupts 909 * get enabled. As long as the bus is in polling mode 910 * it is safe to call usbd_new_device() now since root 911 * hub transfers do not need to sleep. 912 */ 913 sc->sc_bus->use_polling++; 914 if (!usb_attach_roothub(sc)) 915 usb_needs_explore(sc->sc_bus->root_hub, 0); 916 sc->sc_bus->use_polling--; 917 break; 918 default: 919 rv = config_activate_children(self, act); 920 break; 921 } 922 return (rv); 923 } 924 925 int 926 usb_detach(struct device *self, int flags) 927 { 928 struct usb_softc *sc = (struct usb_softc *)self; 929 930 if (sc->sc_bus->root_hub != NULL) { 931 usb_detach_roothub(sc); 932 933 if (--usb_nbuses == 0) { 934 usb_run_tasks = usb_run_abort_tasks = 0; 935 wakeup(&usb_run_abort_tasks); 936 wakeup(&usb_run_tasks); 937 } 938 } 939 940 if (sc->sc_bus->soft != NULL) { 941 softintr_disestablish(sc->sc_bus->soft); 942 sc->sc_bus->soft = NULL; 943 } 944 945 #if NBPFILTER > 0 946 bpfsdetach(sc->sc_bus->bpfif); 947 #endif 948 return (0); 949 } 950 951 void 952 usb_tap(struct usbd_bus *bus, struct usbd_xfer *xfer, uint8_t dir) 953 { 954 #if NBPFILTER > 0 955 struct usb_softc *sc = (struct usb_softc *)bus->usbctl; 956 usb_endpoint_descriptor_t *ed = xfer->pipe->endpoint->edesc; 957 union { 958 struct usbpcap_ctl_hdr uch; 959 struct usbpcap_iso_hdr_full uih; 960 } h; 961 struct usbpcap_pkt_hdr *uph = &h.uch.uch_hdr; 962 uint32_t nframes, offset; 963 unsigned int bpfdir; 964 void *data = NULL; 965 size_t flen; 966 caddr_t bpf; 967 int i; 968 969 bpf = bus->bpf; 970 if (bpf == NULL) 971 return; 972 973 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 974 case UE_CONTROL: 975 /* Control transfer headers include an extra byte */ 976 uph->uph_hlen = htole16(sizeof(struct usbpcap_ctl_hdr)); 977 uph->uph_xfertype = USBPCAP_TRANSFER_CONTROL; 978 break; 979 case UE_ISOCHRONOUS: 980 offset = 0; 981 nframes = xfer->nframes; 982 #ifdef DIAGNOSTIC 983 if (nframes > _USBPCAP_MAX_ISOFRAMES) { 984 printf("%s: too many frames: %d > %d\n", __func__, 985 xfer->nframes, _USBPCAP_MAX_ISOFRAMES); 986 nframes = _USBPCAP_MAX_ISOFRAMES; 987 } 988 #endif 989 /* Isochronous transfer headers include space for one frame */ 990 flen = (nframes - 1) * sizeof(struct usbpcap_iso_pkt); 991 uph->uph_hlen = htole16(sizeof(struct usbpcap_iso_hdr) + flen); 992 uph->uph_xfertype = USBPCAP_TRANSFER_ISOCHRONOUS; 993 h.uih.uih_startframe = 0; /* not yet used */ 994 h.uih.uih_nframes = nframes; 995 h.uih.uih_errors = 0; /* we don't have per-frame error */ 996 for (i = 0; i < nframes; i++) { 997 h.uih.uih_frames[i].uip_offset = offset; 998 h.uih.uih_frames[i].uip_length = xfer->frlengths[i]; 999 /* See above, we don't have per-frame error */ 1000 h.uih.uih_frames[i].uip_status = 0; 1001 offset += xfer->frlengths[i]; 1002 } 1003 break; 1004 case UE_BULK: 1005 uph->uph_hlen = htole16(sizeof(*uph)); 1006 uph->uph_xfertype = USBPCAP_TRANSFER_BULK; 1007 break; 1008 case UE_INTERRUPT: 1009 uph->uph_hlen = htole16(sizeof(*uph)); 1010 uph->uph_xfertype = USBPCAP_TRANSFER_INTERRUPT; 1011 break; 1012 default: 1013 return; 1014 } 1015 1016 uph->uph_id = 0; /* not yet used */ 1017 uph->uph_status = htole32(xfer->status); 1018 uph->uph_function = 0; /* not yet used */ 1019 uph->uph_bus = htole32(sc->sc_dev.dv_unit); 1020 uph->uph_devaddr = htole16(xfer->device->address); 1021 uph->uph_epaddr = ed->bEndpointAddress; 1022 uph->uph_info = 0; 1023 1024 /* Outgoing control requests start with a STAGE dump. */ 1025 if ((xfer->rqflags & URQ_REQUEST) && (dir == USBTAP_DIR_OUT)) { 1026 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_SETUP; 1027 uph->uph_dlen = sizeof(usb_device_request_t); 1028 bpf_tap_hdr(bpf, uph, uph->uph_hlen, &xfer->request, 1029 uph->uph_dlen, BPF_DIRECTION_OUT); 1030 } 1031 1032 if (dir == USBTAP_DIR_OUT) { 1033 bpfdir = BPF_DIRECTION_OUT; 1034 if (!usbd_xfer_isread(xfer)) { 1035 data = KERNADDR(&xfer->dmabuf, 0); 1036 uph->uph_dlen = xfer->length; 1037 if (xfer->rqflags & URQ_REQUEST) 1038 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_DATA; 1039 } else { 1040 data = NULL; 1041 uph->uph_dlen = 0; 1042 if (xfer->rqflags & URQ_REQUEST) 1043 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1044 } 1045 } else { /* USBTAP_DIR_IN */ 1046 bpfdir = BPF_DIRECTION_IN; 1047 uph->uph_info = USBPCAP_INFO_DIRECTION_IN; 1048 if (usbd_xfer_isread(xfer)) { 1049 data = KERNADDR(&xfer->dmabuf, 0); 1050 uph->uph_dlen = xfer->actlen; 1051 if (xfer->rqflags & URQ_REQUEST) 1052 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_DATA; 1053 } else { 1054 data = NULL; 1055 uph->uph_dlen = 0; 1056 if (xfer->rqflags & URQ_REQUEST) 1057 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1058 } 1059 } 1060 1061 /* Dump bulk/intr/iso data, ctrl DATA or STATUS stage. */ 1062 bpf_tap_hdr(bpf, uph, uph->uph_hlen, data, uph->uph_dlen, bpfdir); 1063 1064 /* Incoming control requests with DATA need a STATUS stage. */ 1065 if ((xfer->rqflags & URQ_REQUEST) && (dir == USBTAP_DIR_IN) && 1066 (h.uch.uch_stage == USBPCAP_CONTROL_STAGE_DATA)) { 1067 h.uch.uch_stage = USBPCAP_CONTROL_STAGE_STATUS; 1068 uph->uph_dlen = 0; 1069 bpf_tap_hdr(bpf, uph, uph->uph_hlen, NULL, 0, BPF_DIRECTION_IN); 1070 } 1071 #endif 1072 } 1073