1 /* $OpenBSD: usb.c,v 1.100 2014/07/12 18:48:52 tedu 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_quirks.h> 67 68 #ifdef USB_DEBUG 69 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0) 70 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0) 71 int usbdebug = 0; 72 #if defined(UHCI_DEBUG) && NUHCI > 0 73 extern int uhcidebug; 74 #endif 75 #if defined(OHCI_DEBUG) && NOHCI > 0 76 extern int ohcidebug; 77 #endif 78 #if defined(EHCI_DEBUG) && NEHCI > 0 79 extern int ehcidebug; 80 #endif 81 /* 82 * 0 - do usual exploration 83 * !0 - do no exploration 84 */ 85 int usb_noexplore = 0; 86 #else 87 #define DPRINTF(x) 88 #define DPRINTFN(n,x) 89 #endif 90 91 struct usb_softc { 92 struct device sc_dev; /* base device */ 93 struct usbd_bus *sc_bus; /* USB controller */ 94 struct usbd_port sc_port; /* dummy port for root hub */ 95 int sc_speed; 96 97 struct usb_task sc_explore_task; 98 99 struct timeval sc_ptime; 100 }; 101 102 struct rwlock usbpalock; 103 104 TAILQ_HEAD(, usb_task) usb_abort_tasks; 105 TAILQ_HEAD(, usb_task) usb_explore_tasks; 106 TAILQ_HEAD(, usb_task) usb_generic_tasks; 107 108 static int usb_nbuses = 0; 109 static int usb_run_tasks, usb_run_abort_tasks; 110 int explore_pending; 111 const char *usbrev_str[] = USBREV_STR; 112 113 void usb_explore(void *); 114 void usb_create_task_threads(void *); 115 void usb_task_thread(void *); 116 struct proc *usb_task_thread_proc = NULL; 117 void usb_abort_task_thread(void *); 118 struct proc *usb_abort_task_thread_proc = NULL; 119 120 void usb_fill_di_task(void *); 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 /* Make sure not to use tsleep() if we are cold booting. */ 188 if (cold) 189 sc->sc_bus->use_polling++; 190 191 /* Don't let hub interrupts cause explore until ready. */ 192 sc->sc_bus->flags |= USB_BUS_CONFIG_PENDING; 193 194 /* explore task */ 195 usb_init_task(&sc->sc_explore_task, usb_explore, sc, 196 USB_TASK_TYPE_EXPLORE); 197 198 /* XXX we should have our own level */ 199 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET, 200 sc->sc_bus->methods->soft_intr, sc->sc_bus); 201 if (sc->sc_bus->soft == NULL) { 202 printf("%s: can't register softintr\n", sc->sc_dev.dv_xname); 203 sc->sc_bus->dying = 1; 204 return; 205 } 206 207 208 209 if (!usb_attach_roothub(sc)) { 210 struct usbd_device *dev = sc->sc_bus->root_hub; 211 #if 1 212 /* 213 * Turning this code off will delay attachment of USB devices 214 * until the USB task thread is running, which means that 215 * the keyboard will not work until after cold boot. 216 */ 217 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1)) 218 dev->hub->explore(sc->sc_bus->root_hub); 219 #endif 220 } 221 222 if (cold) 223 sc->sc_bus->use_polling--; 224 225 if (!sc->sc_bus->dying) { 226 getmicrouptime(&sc->sc_ptime); 227 if (sc->sc_bus->usbrev == USBREV_2_0) 228 explore_pending++; 229 config_pending_incr(); 230 usb_needs_explore(sc->sc_bus->root_hub, 1); 231 } 232 } 233 234 int 235 usb_attach_roothub(struct usb_softc *sc) 236 { 237 struct usbd_device *dev; 238 239 if (usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, sc->sc_speed, 0, 240 &sc->sc_port)) { 241 printf("%s: root hub problem\n", sc->sc_dev.dv_xname); 242 sc->sc_bus->dying = 1; 243 return (1); 244 } 245 246 dev = sc->sc_port.device; 247 if (dev->hub == NULL) { 248 printf("%s: root device is not a hub\n", sc->sc_dev.dv_xname); 249 sc->sc_bus->dying = 1; 250 return (1); 251 } 252 sc->sc_bus->root_hub = dev; 253 254 return (0); 255 } 256 257 void 258 usb_detach_roothub(struct usb_softc *sc) 259 { 260 /* 261 * To avoid races with the usb task thread, mark the root hub 262 * as disconnecting and schedule an exploration task to detach 263 * it. 264 */ 265 sc->sc_bus->flags |= USB_BUS_DISCONNECTING; 266 usb_needs_explore(sc->sc_bus->root_hub, 0); 267 268 usb_wait_task(sc->sc_bus->root_hub, &sc->sc_explore_task); 269 270 sc->sc_bus->root_hub = NULL; 271 } 272 273 void 274 usb_create_task_threads(void *arg) 275 { 276 if (kthread_create(usb_abort_task_thread, NULL, 277 &usb_abort_task_thread_proc, "usbatsk")) 278 panic("unable to create usb abort task thread"); 279 280 if (kthread_create(usb_task_thread, NULL, 281 &usb_task_thread_proc, "usbtask")) 282 panic("unable to create usb task thread"); 283 } 284 285 /* 286 * Add a task to be performed by the task thread. This function can be 287 * called from any context and the task will be executed in a process 288 * context ASAP. 289 */ 290 void 291 usb_add_task(struct usbd_device *dev, struct usb_task *task) 292 { 293 int s; 294 295 /* Don't add task if the device's root hub is dying. */ 296 if (usbd_is_dying(dev)) 297 return; 298 299 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 300 task->state, task->type)); 301 302 s = splusb(); 303 if (!(task->state & USB_TASK_STATE_ONQ)) { 304 switch (task->type) { 305 case USB_TASK_TYPE_ABORT: 306 TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next); 307 break; 308 case USB_TASK_TYPE_EXPLORE: 309 TAILQ_INSERT_TAIL(&usb_explore_tasks, task, next); 310 break; 311 case USB_TASK_TYPE_GENERIC: 312 TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next); 313 break; 314 } 315 task->state |= USB_TASK_STATE_ONQ; 316 task->dev = dev; 317 } 318 if (task->type == USB_TASK_TYPE_ABORT) 319 wakeup(&usb_run_abort_tasks); 320 else 321 wakeup(&usb_run_tasks); 322 splx(s); 323 } 324 325 void 326 usb_rem_task(struct usbd_device *dev, struct usb_task *task) 327 { 328 int s; 329 330 if (!(task->state & USB_TASK_STATE_ONQ)) 331 return; 332 333 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 334 task->state, task->type)); 335 336 s = splusb(); 337 338 switch (task->type) { 339 case USB_TASK_TYPE_ABORT: 340 TAILQ_REMOVE(&usb_abort_tasks, task, next); 341 break; 342 case USB_TASK_TYPE_EXPLORE: 343 TAILQ_REMOVE(&usb_explore_tasks, task, next); 344 break; 345 case USB_TASK_TYPE_GENERIC: 346 TAILQ_REMOVE(&usb_generic_tasks, task, next); 347 break; 348 } 349 task->state &= ~USB_TASK_STATE_ONQ; 350 if (task->state == USB_TASK_STATE_NONE) 351 wakeup(task); 352 353 splx(s); 354 } 355 356 void 357 usb_wait_task(struct usbd_device *dev, struct usb_task *task) 358 { 359 int s; 360 361 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 362 task->state, task->type)); 363 364 if (task->state == USB_TASK_STATE_NONE) 365 return; 366 367 s = splusb(); 368 while (task->state != USB_TASK_STATE_NONE) { 369 DPRINTF(("%s: waiting for task to complete\n", __func__)); 370 tsleep(task, PWAIT, "endtask", 0); 371 } 372 splx(s); 373 } 374 375 void 376 usb_rem_wait_task(struct usbd_device *dev, struct usb_task *task) 377 { 378 usb_rem_task(dev, task); 379 usb_wait_task(dev, task); 380 } 381 382 void 383 usb_task_thread(void *arg) 384 { 385 struct usb_task *task; 386 int s; 387 388 DPRINTF(("usb_task_thread: start\n")); 389 390 s = splusb(); 391 while (usb_run_tasks) { 392 if ((task = TAILQ_FIRST(&usb_explore_tasks)) != NULL) 393 TAILQ_REMOVE(&usb_explore_tasks, task, next); 394 else if ((task = TAILQ_FIRST(&usb_generic_tasks)) != NULL) 395 TAILQ_REMOVE(&usb_generic_tasks, task, next); 396 else { 397 tsleep(&usb_run_tasks, PWAIT, "usbtsk", 0); 398 continue; 399 } 400 /* 401 * Set the state run bit before clearing the onq bit. 402 * This avoids state == none between dequeue and 403 * execution, which could cause usb_wait_task() to do 404 * the wrong thing. 405 */ 406 task->state |= USB_TASK_STATE_RUN; 407 task->state &= ~USB_TASK_STATE_ONQ; 408 /* Don't actually execute the task if dying. */ 409 if (!usbd_is_dying(task->dev)) { 410 splx(s); 411 task->fun(task->arg); 412 s = splusb(); 413 } 414 task->state &= ~USB_TASK_STATE_RUN; 415 if (task->state == USB_TASK_STATE_NONE) 416 wakeup(task); 417 } 418 splx(s); 419 420 kthread_exit(0); 421 } 422 423 /* 424 * This thread is ONLY for the HCI drivers to be able to abort xfers. 425 * Synchronous xfers sleep the task thread, so the aborts need to happen 426 * in a different thread. 427 */ 428 void 429 usb_abort_task_thread(void *arg) 430 { 431 struct usb_task *task; 432 int s; 433 434 DPRINTF(("usb_xfer_abort_thread: start\n")); 435 436 s = splusb(); 437 while (usb_run_abort_tasks) { 438 if ((task = TAILQ_FIRST(&usb_abort_tasks)) != NULL) 439 TAILQ_REMOVE(&usb_abort_tasks, task, next); 440 else { 441 tsleep(&usb_run_abort_tasks, PWAIT, "usbatsk", 0); 442 continue; 443 } 444 /* 445 * Set the state run bit before clearing the onq bit. 446 * This avoids state == none between dequeue and 447 * execution, which could cause usb_wait_task() to do 448 * the wrong thing. 449 */ 450 task->state |= USB_TASK_STATE_RUN; 451 task->state &= ~USB_TASK_STATE_ONQ; 452 /* Don't actually execute the task if dying. */ 453 if (!usbd_is_dying(task->dev)) { 454 splx(s); 455 task->fun(task->arg); 456 s = splusb(); 457 } 458 task->state &= ~USB_TASK_STATE_RUN; 459 if (task->state == USB_TASK_STATE_NONE) 460 wakeup(task); 461 } 462 splx(s); 463 464 kthread_exit(0); 465 } 466 467 int 468 usbctlprint(void *aux, const char *pnp) 469 { 470 /* only "usb"es can attach to host controllers */ 471 if (pnp) 472 printf("usb at %s", pnp); 473 474 return (UNCONF); 475 } 476 477 int 478 usbopen(dev_t dev, int flag, int mode, struct proc *p) 479 { 480 int unit = minor(dev); 481 struct usb_softc *sc; 482 483 if (unit >= usb_cd.cd_ndevs) 484 return (ENXIO); 485 sc = usb_cd.cd_devs[unit]; 486 if (sc == NULL) 487 return (ENXIO); 488 489 if (sc->sc_bus->dying) 490 return (EIO); 491 492 return (0); 493 } 494 495 int 496 usbclose(dev_t dev, int flag, int mode, struct proc *p) 497 { 498 return (0); 499 } 500 501 void 502 usb_fill_di_task(void *arg) 503 { 504 struct usb_device_info *di = (struct usb_device_info *)arg; 505 struct usb_softc *sc; 506 struct usbd_device *dev; 507 508 /* check that the bus and device are still present */ 509 if (di->udi_bus >= usb_cd.cd_ndevs) 510 return; 511 sc = usb_cd.cd_devs[di->udi_bus]; 512 if (sc == NULL) 513 return; 514 dev = sc->sc_bus->devices[di->udi_addr]; 515 if (dev == NULL) 516 return; 517 518 usbd_fill_deviceinfo(dev, di, 1); 519 } 520 521 void 522 usb_fill_udc_task(void *arg) 523 { 524 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)arg; 525 struct usb_softc *sc; 526 struct usbd_device *dev; 527 int addr = udc->udc_addr; 528 usb_config_descriptor_t *cdesc; 529 530 /* check that the bus and device are still present */ 531 if (udc->udc_bus >= usb_cd.cd_ndevs) 532 return; 533 sc = usb_cd.cd_devs[udc->udc_bus]; 534 if (sc == NULL) 535 return; 536 dev = sc->sc_bus->devices[udc->udc_addr]; 537 if (dev == NULL) 538 return; 539 540 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 541 udc->udc_config_index, 0); 542 if (cdesc == NULL) 543 return; 544 udc->udc_desc = *cdesc; 545 free(cdesc, M_TEMP, 0); 546 } 547 548 void 549 usb_fill_udf_task(void *arg) 550 { 551 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)arg; 552 struct usb_softc *sc; 553 struct usbd_device *dev; 554 int addr = udf->udf_addr; 555 usb_config_descriptor_t *cdesc; 556 557 /* check that the bus and device are still present */ 558 if (udf->udf_bus >= usb_cd.cd_ndevs) 559 return; 560 sc = usb_cd.cd_devs[udf->udf_bus]; 561 if (sc == NULL) 562 return; 563 dev = sc->sc_bus->devices[udf->udf_addr]; 564 if (dev == NULL) 565 return; 566 567 cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr], 568 udf->udf_config_index, &udf->udf_size); 569 udf->udf_data = (char *)cdesc; 570 } 571 572 int 573 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p) 574 { 575 struct usb_softc *sc; 576 int unit = minor(devt); 577 int error; 578 579 sc = usb_cd.cd_devs[unit]; 580 581 if (sc->sc_bus->dying) 582 return (EIO); 583 584 error = 0; 585 switch (cmd) { 586 #ifdef USB_DEBUG 587 case USB_SETDEBUG: 588 /* only root can access to these debug flags */ 589 if ((error = suser(curproc, 0)) != 0) 590 return (error); 591 if (!(flag & FWRITE)) 592 return (EBADF); 593 usbdebug = ((*(unsigned int *)data) & 0x000000ff); 594 #if defined(UHCI_DEBUG) && NUHCI > 0 595 uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8; 596 #endif 597 #if defined(OHCI_DEBUG) && NOHCI > 0 598 ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16; 599 #endif 600 #if defined(EHCI_DEBUG) && NEHCI > 0 601 ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24; 602 #endif 603 break; 604 #endif /* USB_DEBUG */ 605 case USB_REQUEST: 606 { 607 struct usb_ctl_request *ur = (void *)data; 608 int len = UGETW(ur->ucr_request.wLength); 609 struct iovec iov; 610 struct uio uio; 611 void *ptr = 0; 612 int addr = ur->ucr_addr; 613 usbd_status err; 614 int error = 0; 615 616 if (!(flag & FWRITE)) 617 return (EBADF); 618 619 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len)); 620 if (len < 0 || len > 32768) 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 ptr = malloc(len, M_TEMP, M_WAITOK); 639 if (uio.uio_rw == UIO_WRITE) { 640 error = uiomove(ptr, len, &uio); 641 if (error) 642 goto ret; 643 } 644 } 645 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 646 &ur->ucr_request, ptr, ur->ucr_flags, 647 &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT); 648 if (err) { 649 error = EIO; 650 goto ret; 651 } 652 /* Only if USBD_SHORT_XFER_OK is set. */ 653 if (len > ur->ucr_actlen) 654 len = ur->ucr_actlen; 655 if (len != 0) { 656 if (uio.uio_rw == UIO_READ) { 657 error = uiomove(ptr, len, &uio); 658 if (error) 659 goto ret; 660 } 661 } 662 ret: 663 if (ptr) 664 free(ptr, M_TEMP, 0); 665 return (error); 666 } 667 668 case USB_DEVICEINFO: 669 { 670 struct usb_device_info *di = (void *)data; 671 int addr = di->udi_addr; 672 struct usb_task di_task; 673 struct usbd_device *dev; 674 675 if (addr < 1 || addr >= USB_MAX_DEVICES) 676 return (EINVAL); 677 678 dev = sc->sc_bus->devices[addr]; 679 if (dev == NULL) 680 return (ENXIO); 681 682 di->udi_bus = unit; 683 684 /* All devices get a driver, thanks to ugen(4). If the 685 * task ends without adding a driver name, there was an error. 686 */ 687 di->udi_devnames[0][0] = '\0'; 688 689 usb_init_task(&di_task, usb_fill_di_task, di, 690 USB_TASK_TYPE_GENERIC); 691 usb_add_task(sc->sc_bus->root_hub, &di_task); 692 usb_wait_task(sc->sc_bus->root_hub, &di_task); 693 694 if (di->udi_devnames[0][0] == '\0') 695 return (ENXIO); 696 697 break; 698 } 699 700 case USB_DEVICESTATS: 701 *(struct usb_device_stats *)data = sc->sc_bus->stats; 702 break; 703 704 case USB_DEVICE_GET_DDESC: 705 { 706 struct usb_device_ddesc *udd = (struct usb_device_ddesc *)data; 707 int addr = udd->udd_addr; 708 struct usbd_device *dev; 709 710 if (addr < 1 || addr >= USB_MAX_DEVICES) 711 return (EINVAL); 712 713 dev = sc->sc_bus->devices[addr]; 714 if (dev == NULL) 715 return (ENXIO); 716 717 udd->udd_bus = unit; 718 719 udd->udd_desc = *usbd_get_device_descriptor(dev); 720 break; 721 } 722 723 case USB_DEVICE_GET_CDESC: 724 { 725 struct usb_device_cdesc *udc = (struct usb_device_cdesc *)data; 726 int addr = udc->udc_addr; 727 struct usb_task udc_task; 728 729 if (addr < 1 || addr >= USB_MAX_DEVICES) 730 return (EINVAL); 731 if (sc->sc_bus->devices[addr] == NULL) 732 return (ENXIO); 733 734 udc->udc_bus = unit; 735 736 udc->udc_desc.bLength = 0; 737 usb_init_task(&udc_task, usb_fill_udc_task, udc, 738 USB_TASK_TYPE_GENERIC); 739 usb_add_task(sc->sc_bus->root_hub, &udc_task); 740 usb_wait_task(sc->sc_bus->root_hub, &udc_task); 741 if (udc->udc_desc.bLength == 0) 742 return (EINVAL); 743 break; 744 } 745 746 case USB_DEVICE_GET_FDESC: 747 { 748 struct usb_device_fdesc *udf = (struct usb_device_fdesc *)data; 749 int addr = udf->udf_addr; 750 struct usb_task udf_task; 751 struct usb_device_fdesc save_udf; 752 usb_config_descriptor_t *cdesc; 753 struct iovec iov; 754 struct uio uio; 755 int len, error; 756 757 if (addr < 1 || addr >= USB_MAX_DEVICES) 758 return (EINVAL); 759 if (sc->sc_bus->devices[addr] == NULL) 760 return (ENXIO); 761 762 udf->udf_bus = unit; 763 764 save_udf = *udf; 765 usb_init_task(&udf_task, usb_fill_udf_task, udf, 766 USB_TASK_TYPE_GENERIC); 767 usb_add_task(sc->sc_bus->root_hub, &udf_task); 768 usb_wait_task(sc->sc_bus->root_hub, &udf_task); 769 len = udf->udf_size; 770 cdesc = (usb_config_descriptor_t *)udf->udf_data; 771 *udf = save_udf; 772 if (cdesc == NULL) 773 return (EINVAL); 774 if (len > udf->udf_size) 775 len = udf->udf_size; 776 iov.iov_base = (caddr_t)udf->udf_data; 777 iov.iov_len = len; 778 uio.uio_iov = &iov; 779 uio.uio_iovcnt = 1; 780 uio.uio_resid = len; 781 uio.uio_offset = 0; 782 uio.uio_segflg = UIO_USERSPACE; 783 uio.uio_rw = UIO_READ; 784 uio.uio_procp = p; 785 error = uiomove((void *)cdesc, len, &uio); 786 free(cdesc, M_TEMP, 0); 787 return (error); 788 } 789 790 default: 791 return (EINVAL); 792 } 793 return (0); 794 } 795 796 /* 797 * Explore device tree from the root. We need mutual exclusion to this 798 * hub while traversing the device tree, but this is guaranteed since this 799 * function is only called from the task thread, with one exception: 800 * usb_attach() calls this function, but there shouldn't be anything else 801 * trying to explore this hub at that time. 802 */ 803 void 804 usb_explore(void *v) 805 { 806 struct usb_softc *sc = v; 807 struct timeval now, waited; 808 int pwrdly, waited_ms; 809 810 DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname)); 811 #ifdef USB_DEBUG 812 if (usb_noexplore) 813 return; 814 #endif 815 816 if (sc->sc_bus->dying) 817 return; 818 819 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 820 /* 821 * If this is a low/full speed hub and there is a high 822 * speed hub that hasn't explored yet, reshedule this 823 * task, allowing the high speed explore task to run. 824 */ 825 if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) { 826 usb_add_task(sc->sc_bus->root_hub, 827 &sc->sc_explore_task); 828 return; 829 } 830 831 /* 832 * Wait for power to stabilize. 833 */ 834 getmicrouptime(&now); 835 timersub(&now, &sc->sc_ptime, &waited); 836 waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000; 837 838 pwrdly = sc->sc_bus->root_hub->hub->hubdesc.bPwrOn2PwrGood * 839 UHD_PWRON_FACTOR + USB_EXTRA_POWER_UP_TIME; 840 if (pwrdly > waited_ms) 841 usb_delay_ms(sc->sc_bus, pwrdly - waited_ms); 842 } 843 844 if (sc->sc_bus->flags & USB_BUS_DISCONNECTING) { 845 /* Prevent new tasks from being scheduled. */ 846 sc->sc_bus->dying = 1; 847 848 /* Make all devices disconnect. */ 849 if (sc->sc_port.device != NULL) { 850 usbd_detach(sc->sc_port.device, (struct device *)sc); 851 sc->sc_port.device = NULL; 852 } 853 854 sc->sc_bus->flags &= ~USB_BUS_DISCONNECTING; 855 } else { 856 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 857 } 858 859 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 860 DPRINTF(("%s: %s: first explore done\n", __func__, 861 sc->sc_dev.dv_xname)); 862 if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending) 863 explore_pending--; 864 config_pending_decr(); 865 sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING); 866 } 867 } 868 869 void 870 usb_needs_explore(struct usbd_device *dev, int first_explore) 871 { 872 struct usb_softc *usbctl = (struct usb_softc *)dev->bus->usbctl; 873 874 DPRINTFN(3,("%s: %s\n", usbctl->sc_dev.dv_xname, __func__)); 875 876 if (!first_explore && (dev->bus->flags & USB_BUS_CONFIG_PENDING)) { 877 DPRINTF(("%s: %s: not exploring before first explore\n", 878 __func__, usbctl->sc_dev.dv_xname)); 879 return; 880 } 881 882 usb_add_task(dev, &usbctl->sc_explore_task); 883 } 884 885 void 886 usb_needs_reattach(struct usbd_device *dev) 887 { 888 DPRINTFN(2,("usb_needs_reattach\n")); 889 dev->powersrc->reattach = 1; 890 usb_needs_explore(dev, 0); 891 } 892 893 void 894 usb_schedsoftintr(struct usbd_bus *bus) 895 { 896 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling)); 897 898 if (bus->use_polling) { 899 bus->methods->soft_intr(bus); 900 } else { 901 softintr_schedule(bus->soft); 902 } 903 } 904 905 int 906 usb_activate(struct device *self, int act) 907 { 908 struct usb_softc *sc = (struct usb_softc *)self; 909 int rv = 0; 910 911 switch (act) { 912 case DVACT_QUIESCE: 913 if (sc->sc_bus->root_hub != NULL) 914 usb_detach_roothub(sc); 915 break; 916 case DVACT_RESUME: 917 sc->sc_bus->dying = 0; 918 919 /* 920 * Make sure the root hub is present before interrupts 921 * get enabled. As long as the bus is in polling mode 922 * it is safe to call usbd_new_device() now since root 923 * hub transfers do not need to sleep. 924 */ 925 sc->sc_bus->use_polling++; 926 if (!usb_attach_roothub(sc)) 927 usb_needs_explore(sc->sc_bus->root_hub, 0); 928 sc->sc_bus->use_polling--; 929 break; 930 default: 931 rv = config_activate_children(self, act); 932 break; 933 } 934 return (rv); 935 } 936 937 int 938 usb_detach(struct device *self, int flags) 939 { 940 struct usb_softc *sc = (struct usb_softc *)self; 941 942 if (sc->sc_bus->root_hub != NULL) { 943 usb_detach_roothub(sc); 944 945 if (--usb_nbuses == 0) { 946 usb_run_tasks = usb_run_abort_tasks = 0; 947 wakeup(&usb_run_abort_tasks); 948 wakeup(&usb_run_tasks); 949 } 950 } 951 952 if (sc->sc_bus->soft != NULL) { 953 softintr_disestablish(sc->sc_bus->soft); 954 sc->sc_bus->soft = NULL; 955 } 956 957 return (0); 958 } 959