1 /* $OpenBSD: usb.c,v 1.77 2011/07/03 15:47:17 matthew 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/kthread.h> 50 #include <sys/proc.h> 51 #include <sys/conf.h> 52 #include <sys/fcntl.h> 53 #include <sys/poll.h> 54 #include <sys/selinfo.h> 55 #include <sys/vnode.h> 56 #include <sys/signalvar.h> 57 #include <sys/time.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 usbd_bus_handle sc_bus; /* USB controller */ 94 struct usbd_port sc_port; /* dummy port for root hub */ 95 96 struct usb_task sc_explore_task; 97 98 struct timeval sc_ptime; 99 }; 100 101 TAILQ_HEAD(, usb_task) usb_abort_tasks; 102 TAILQ_HEAD(, usb_task) usb_explore_tasks; 103 TAILQ_HEAD(, usb_task) usb_generic_tasks; 104 105 int usb_run_tasks, usb_run_abort_tasks; 106 int explore_pending; 107 108 void usb_explore(void *); 109 void usb_create_task_threads(void *); 110 void usb_task_thread(void *); 111 struct proc *usb_task_thread_proc = NULL; 112 void usb_abort_task_thread(void *); 113 struct proc *usb_abort_task_thread_proc = NULL; 114 115 const char *usbrev_str[] = USBREV_STR; 116 117 int usb_match(struct device *, void *, void *); 118 void usb_attach(struct device *, struct device *, void *); 119 int usb_detach(struct device *, int); 120 int usb_activate(struct device *, int); 121 122 struct cfdriver usb_cd = { 123 NULL, "usb", DV_DULL 124 }; 125 126 const struct cfattach usb_ca = { 127 sizeof(struct usb_softc), 128 usb_match, 129 usb_attach, 130 usb_detach, 131 usb_activate, 132 }; 133 134 int 135 usb_match(struct device *parent, void *match, void *aux) 136 { 137 DPRINTF(("usbd_match\n")); 138 return (UMATCH_GENERIC); 139 } 140 141 void 142 usb_attach(struct device *parent, struct device *self, void *aux) 143 { 144 struct usb_softc *sc = (struct usb_softc *)self; 145 usbd_device_handle dev; 146 usbd_status err; 147 int usbrev; 148 int speed; 149 150 DPRINTF(("usbd_attach\n")); 151 152 usbd_init(); 153 sc->sc_bus = aux; 154 sc->sc_bus->usbctl = sc; 155 sc->sc_port.power = USB_MAX_POWER; 156 157 usbrev = sc->sc_bus->usbrev; 158 printf(": USB revision %s", usbrev_str[usbrev]); 159 switch (usbrev) { 160 case USBREV_1_0: 161 case USBREV_1_1: 162 speed = USB_SPEED_FULL; 163 break; 164 case USBREV_2_0: 165 speed = USB_SPEED_HIGH; 166 break; 167 default: 168 printf(", not supported\n"); 169 sc->sc_bus->dying = 1; 170 return; 171 } 172 printf("\n"); 173 174 /* Make sure not to use tsleep() if we are cold booting. */ 175 if (cold) 176 sc->sc_bus->use_polling++; 177 178 /* Don't let hub interrupts cause explore until ready. */ 179 sc->sc_bus->flags |= USB_BUS_CONFIG_PENDING; 180 181 /* explore task */ 182 usb_init_task(&sc->sc_explore_task, usb_explore, sc, 183 USB_TASK_TYPE_EXPLORE); 184 185 /* XXX we should have our own level */ 186 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET, 187 sc->sc_bus->methods->soft_intr, sc->sc_bus); 188 if (sc->sc_bus->soft == NULL) { 189 printf("%s: can't register softintr\n", sc->sc_dev.dv_xname); 190 sc->sc_bus->dying = 1; 191 return; 192 } 193 194 err = usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, speed, 0, 195 &sc->sc_port); 196 if (!err) { 197 dev = sc->sc_port.device; 198 if (dev->hub == NULL) { 199 sc->sc_bus->dying = 1; 200 printf("%s: root device is not a hub\n", 201 sc->sc_dev.dv_xname); 202 return; 203 } 204 sc->sc_bus->root_hub = dev; 205 #if 1 206 /* 207 * Turning this code off will delay attachment of USB devices 208 * until the USB task thread is running, which means that 209 * the keyboard will not work until after cold boot. 210 */ 211 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1)) 212 dev->hub->explore(sc->sc_bus->root_hub); 213 #endif 214 } else { 215 printf("%s: root hub problem, error=%d\n", 216 sc->sc_dev.dv_xname, err); 217 sc->sc_bus->dying = 1; 218 } 219 if (cold) 220 sc->sc_bus->use_polling--; 221 222 if (!sc->sc_bus->dying) { 223 getmicrouptime(&sc->sc_ptime); 224 if (sc->sc_bus->usbrev == USBREV_2_0) 225 explore_pending++; 226 config_pending_incr(); 227 usb_needs_explore(sc->sc_bus->root_hub, 1); 228 } 229 } 230 231 /* 232 * Called by usbd_init when first usb is attached. 233 */ 234 void 235 usb_begin_tasks(void) 236 { 237 TAILQ_INIT(&usb_abort_tasks); 238 TAILQ_INIT(&usb_explore_tasks); 239 TAILQ_INIT(&usb_generic_tasks); 240 usb_run_tasks = usb_run_abort_tasks = 1; 241 kthread_create_deferred(usb_create_task_threads, NULL); 242 } 243 244 /* 245 * Called by usbd_finish when last usb is detached. 246 */ 247 void 248 usb_end_tasks(void) 249 { 250 usb_run_tasks = usb_run_abort_tasks = 0; 251 wakeup(&usb_run_abort_tasks); 252 wakeup(&usb_run_tasks); 253 } 254 255 void 256 usb_create_task_threads(void *arg) 257 { 258 if (kthread_create(usb_abort_task_thread, NULL, 259 &usb_abort_task_thread_proc, "usbatsk")) 260 panic("unable to create usb abort task thread"); 261 262 if (kthread_create(usb_task_thread, NULL, 263 &usb_task_thread_proc, "usbtask")) 264 panic("unable to create usb task thread"); 265 } 266 267 /* 268 * Add a task to be performed by the task thread. This function can be 269 * called from any context and the task will be executed in a process 270 * context ASAP. 271 */ 272 void 273 usb_add_task(usbd_device_handle dev, struct usb_task *task) 274 { 275 int s; 276 277 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 278 task->state, task->type)); 279 280 /* Don't add task if the device's root hub is dying. */ 281 if (usbd_is_dying(dev)) 282 return; 283 284 s = splusb(); 285 if (!(task->state & USB_TASK_STATE_ONQ)) { 286 switch (task->type) { 287 case USB_TASK_TYPE_ABORT: 288 TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next); 289 break; 290 case USB_TASK_TYPE_EXPLORE: 291 TAILQ_INSERT_TAIL(&usb_explore_tasks, task, next); 292 break; 293 case USB_TASK_TYPE_GENERIC: 294 TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next); 295 break; 296 } 297 task->state |= USB_TASK_STATE_ONQ; 298 task->dev = dev; 299 } 300 if (task->type == USB_TASK_TYPE_ABORT) 301 wakeup(&usb_run_abort_tasks); 302 else 303 wakeup(&usb_run_tasks); 304 splx(s); 305 } 306 307 void 308 usb_rem_task(usbd_device_handle dev, struct usb_task *task) 309 { 310 int s; 311 312 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 313 task->state, task->type)); 314 315 if (!(task->state & USB_TASK_STATE_ONQ)) 316 return; 317 318 s = splusb(); 319 320 switch (task->type) { 321 case USB_TASK_TYPE_ABORT: 322 TAILQ_REMOVE(&usb_abort_tasks, task, next); 323 break; 324 case USB_TASK_TYPE_EXPLORE: 325 TAILQ_REMOVE(&usb_explore_tasks, task, next); 326 break; 327 case USB_TASK_TYPE_GENERIC: 328 TAILQ_REMOVE(&usb_generic_tasks, task, next); 329 break; 330 } 331 task->state &= ~USB_TASK_STATE_ONQ; 332 if (task->state == USB_TASK_STATE_NONE) 333 wakeup(task); 334 335 splx(s); 336 } 337 338 void 339 usb_wait_task(usbd_device_handle dev, struct usb_task *task) 340 { 341 int s; 342 343 DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task, 344 task->state, task->type)); 345 346 if (task->state == USB_TASK_STATE_NONE) 347 return; 348 349 s = splusb(); 350 while (task->state != USB_TASK_STATE_NONE) { 351 DPRINTF(("%s: waiting for task to complete\n", __func__)); 352 tsleep(task, PWAIT, "endtask", 0); 353 } 354 splx(s); 355 } 356 357 void 358 usb_rem_wait_task(usbd_device_handle dev, struct usb_task *task) 359 { 360 usb_rem_task(dev, task); 361 usb_wait_task(dev, task); 362 } 363 364 void 365 usb_task_thread(void *arg) 366 { 367 struct usb_task *task; 368 int s; 369 370 DPRINTF(("usb_task_thread: start\n")); 371 372 s = splusb(); 373 while (usb_run_tasks) { 374 if ((task = TAILQ_FIRST(&usb_explore_tasks)) != NULL) 375 TAILQ_REMOVE(&usb_explore_tasks, task, next); 376 else if ((task = TAILQ_FIRST(&usb_generic_tasks)) != NULL) 377 TAILQ_REMOVE(&usb_generic_tasks, task, next); 378 else { 379 tsleep(&usb_run_tasks, PWAIT, "usbtsk", 0); 380 continue; 381 } 382 /* 383 * Set the state run bit before clearing the onq bit. 384 * This avoids state == none between dequeue and 385 * execution, which could cause usb_wait_task() to do 386 * the wrong thing. 387 */ 388 task->state |= USB_TASK_STATE_RUN; 389 task->state &= ~USB_TASK_STATE_ONQ; 390 /* Don't actually execute the task if dying. */ 391 if (!usbd_is_dying(task->dev)) { 392 splx(s); 393 task->fun(task->arg); 394 s = splusb(); 395 } 396 task->state &= ~USB_TASK_STATE_RUN; 397 if (task->state == USB_TASK_STATE_NONE) 398 wakeup(task); 399 } 400 splx(s); 401 402 kthread_exit(0); 403 } 404 405 /* 406 * This thread is ONLY for the HCI drivers to be able to abort xfers. 407 * Synchronous xfers sleep the task thread, so the aborts need to happen 408 * in a different thread. 409 */ 410 void 411 usb_abort_task_thread(void *arg) 412 { 413 struct usb_task *task; 414 int s; 415 416 DPRINTF(("usb_xfer_abort_thread: start\n")); 417 418 s = splusb(); 419 while (usb_run_abort_tasks) { 420 if ((task = TAILQ_FIRST(&usb_abort_tasks)) != NULL) 421 TAILQ_REMOVE(&usb_abort_tasks, task, next); 422 else { 423 tsleep(&usb_run_abort_tasks, PWAIT, "usbatsk", 0); 424 continue; 425 } 426 /* 427 * Set the state run bit before clearing the onq bit. 428 * This avoids state == none between dequeue and 429 * execution, which could cause usb_wait_task() to do 430 * the wrong thing. 431 */ 432 task->state |= USB_TASK_STATE_RUN; 433 task->state &= ~USB_TASK_STATE_ONQ; 434 /* Don't actually execute the task if dying. */ 435 if (!usbd_is_dying(task->dev)) { 436 splx(s); 437 task->fun(task->arg); 438 s = splusb(); 439 } 440 task->state &= ~USB_TASK_STATE_RUN; 441 if (task->state == USB_TASK_STATE_NONE) 442 wakeup(task); 443 } 444 splx(s); 445 446 kthread_exit(0); 447 } 448 449 int 450 usbctlprint(void *aux, const char *pnp) 451 { 452 /* only "usb"es can attach to host controllers */ 453 if (pnp) 454 printf("usb at %s", pnp); 455 456 return (UNCONF); 457 } 458 459 int 460 usbopen(dev_t dev, int flag, int mode, struct proc *p) 461 { 462 int unit = minor(dev); 463 struct usb_softc *sc; 464 465 if (unit >= usb_cd.cd_ndevs) 466 return (ENXIO); 467 sc = usb_cd.cd_devs[unit]; 468 if (sc == NULL) 469 return (ENXIO); 470 471 if (sc->sc_bus->dying) 472 return (EIO); 473 474 return (0); 475 } 476 477 int 478 usbclose(dev_t dev, int flag, int mode, struct proc *p) 479 { 480 return (0); 481 } 482 483 void 484 usbd_fill_di_task(void *arg) 485 { 486 struct usb_device_info *di = (struct usb_device_info *)arg; 487 struct usb_softc *sc; 488 usbd_device_handle dev; 489 490 /* check that the bus and device are still present */ 491 if (di->udi_bus >= usb_cd.cd_ndevs) 492 return; 493 sc = usb_cd.cd_devs[di->udi_bus]; 494 if (sc == NULL) 495 return; 496 dev = sc->sc_bus->devices[di->udi_addr]; 497 if (dev == NULL) 498 return; 499 500 usbd_fill_deviceinfo(dev, di, 1); 501 } 502 503 int 504 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p) 505 { 506 struct usb_softc *sc; 507 int unit = minor(devt); 508 int error; 509 510 sc = usb_cd.cd_devs[unit]; 511 512 if (sc->sc_bus->dying) 513 return (EIO); 514 515 error = 0; 516 switch (cmd) { 517 #ifdef USB_DEBUG 518 case USB_SETDEBUG: 519 /* only root can access to these debug flags */ 520 if ((error = suser(curproc, 0)) != 0) 521 return (error); 522 if (!(flag & FWRITE)) 523 return (EBADF); 524 usbdebug = ((*(unsigned int *)data) & 0x000000ff); 525 #if defined(UHCI_DEBUG) && NUHCI > 0 526 uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8; 527 #endif 528 #if defined(OHCI_DEBUG) && NOHCI > 0 529 ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16; 530 #endif 531 #if defined(EHCI_DEBUG) && NEHCI > 0 532 ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24; 533 #endif 534 break; 535 #endif /* USB_DEBUG */ 536 case USB_REQUEST: 537 { 538 struct usb_ctl_request *ur = (void *)data; 539 int len = UGETW(ur->ucr_request.wLength); 540 struct iovec iov; 541 struct uio uio; 542 void *ptr = 0; 543 int addr = ur->ucr_addr; 544 usbd_status err; 545 int error = 0; 546 547 if (!(flag & FWRITE)) 548 return (EBADF); 549 550 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len)); 551 if (len < 0 || len > 32768) 552 return (EINVAL); 553 if (addr < 0 || addr >= USB_MAX_DEVICES || 554 sc->sc_bus->devices[addr] == 0) 555 return (EINVAL); 556 if (len != 0) { 557 iov.iov_base = (caddr_t)ur->ucr_data; 558 iov.iov_len = len; 559 uio.uio_iov = &iov; 560 uio.uio_iovcnt = 1; 561 uio.uio_resid = len; 562 uio.uio_offset = 0; 563 uio.uio_segflg = UIO_USERSPACE; 564 uio.uio_rw = 565 ur->ucr_request.bmRequestType & UT_READ ? 566 UIO_READ : UIO_WRITE; 567 uio.uio_procp = p; 568 ptr = malloc(len, M_TEMP, M_WAITOK); 569 if (uio.uio_rw == UIO_WRITE) { 570 error = uiomove(ptr, len, &uio); 571 if (error) 572 goto ret; 573 } 574 } 575 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 576 &ur->ucr_request, ptr, ur->ucr_flags, 577 &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT); 578 if (err) { 579 error = EIO; 580 goto ret; 581 } 582 if (len != 0) { 583 if (uio.uio_rw == UIO_READ) { 584 error = uiomove(ptr, len, &uio); 585 if (error) 586 goto ret; 587 } 588 } 589 ret: 590 if (ptr) 591 free(ptr, M_TEMP); 592 return (error); 593 } 594 595 case USB_DEVICEINFO: 596 { 597 struct usb_device_info *di = (void *)data; 598 int addr = di->udi_addr; 599 struct usb_task di_task; 600 usbd_device_handle dev; 601 602 if (addr < 1 || addr >= USB_MAX_DEVICES) 603 return (EINVAL); 604 605 dev = sc->sc_bus->devices[addr]; 606 if (dev == NULL) 607 return (ENXIO); 608 609 di->udi_bus = unit; 610 611 /* All devices get a driver, thanks to ugen(4). If the 612 * task ends without adding a driver name, there was an error. 613 */ 614 di->udi_devnames[0][0] = '\0'; 615 616 usb_init_task(&di_task, usbd_fill_di_task, di, 617 USB_TASK_TYPE_GENERIC); 618 usb_add_task(sc->sc_bus->root_hub, &di_task); 619 usb_wait_task(sc->sc_bus->root_hub, &di_task); 620 621 if (di->udi_devnames[0][0] == '\0') 622 return (ENXIO); 623 624 break; 625 } 626 627 case USB_DEVICEINFO_48: 628 { 629 struct usb_device_info_48 *di_48 = (void *)data; 630 struct usb_device_info di_tmp; 631 int addr = di_48->udi_addr; 632 usbd_device_handle dev; 633 634 if (addr < 1 || addr >= USB_MAX_DEVICES) 635 return (EINVAL); 636 dev = sc->sc_bus->devices[addr]; 637 if (dev == NULL) 638 return (ENXIO); 639 640 bzero(&di_tmp, sizeof(struct usb_device_info)); 641 bcopy(di_48, &di_tmp, sizeof(struct usb_device_info_48)); 642 usbd_fill_deviceinfo(dev, &di_tmp, 1); 643 bcopy(&di_tmp, di_48, sizeof(struct usb_device_info_48)); 644 break; 645 } 646 647 case USB_DEVICESTATS: 648 *(struct usb_device_stats *)data = sc->sc_bus->stats; 649 break; 650 651 default: 652 return (EINVAL); 653 } 654 return (0); 655 } 656 657 /* 658 * Explore device tree from the root. We need mutual exclusion to this 659 * hub while traversing the device tree, but this is guaranteed since this 660 * function is only called from the task thread, with one exception: 661 * usb_attach() calls this function, but there shouldn't be anything else 662 * trying to explore this hub at that time. 663 */ 664 void 665 usb_explore(void *v) 666 { 667 struct usb_softc *sc = v; 668 struct timeval now, waited; 669 int pwrdly, waited_ms; 670 671 DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname)); 672 #ifdef USB_DEBUG 673 if (usb_noexplore) 674 return; 675 #endif 676 677 if (sc->sc_bus->dying) 678 return; 679 680 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 681 /* 682 * If this is a low/full speed hub and there is a high 683 * speed hub that hasn't explored yet, reshedule this 684 * task, allowing the high speed explore task to run. 685 */ 686 if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) { 687 usb_add_task(sc->sc_bus->root_hub, 688 &sc->sc_explore_task); 689 return; 690 } 691 692 /* 693 * Wait for power to stabilize. 694 */ 695 getmicrouptime(&now); 696 timersub(&now, &sc->sc_ptime, &waited); 697 waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000; 698 699 pwrdly = sc->sc_bus->root_hub->hub->hubdesc.bPwrOn2PwrGood * 700 UHD_PWRON_FACTOR + USB_EXTRA_POWER_UP_TIME; 701 if (pwrdly > waited_ms) 702 usb_delay_ms(sc->sc_bus, pwrdly - waited_ms); 703 } 704 705 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 706 707 if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) { 708 DPRINTF(("%s: %s: first explore done\n", __func__, 709 sc->sc_dev.dv_xname)); 710 if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending) 711 explore_pending--; 712 config_pending_decr(); 713 sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING); 714 } 715 } 716 717 void 718 usb_needs_explore(usbd_device_handle dev, int first_explore) 719 { 720 DPRINTFN(3,("%s: %s\n", dev->bus->usbctl->sc_dev.dv_xname, __func__)); 721 722 if (!first_explore && 723 (dev->bus->flags & USB_BUS_CONFIG_PENDING)) { 724 DPRINTF(("%s: %s: not exploring before first explore\n", 725 __func__, dev->bus->usbctl->sc_dev.dv_xname)); 726 return; 727 } 728 729 usb_add_task(dev, &dev->bus->usbctl->sc_explore_task); 730 } 731 732 void 733 usb_needs_reattach(usbd_device_handle dev) 734 { 735 DPRINTFN(2,("usb_needs_reattach\n")); 736 dev->powersrc->reattach = 1; 737 usb_needs_explore(dev, 0); 738 } 739 740 void 741 usb_schedsoftintr(usbd_bus_handle bus) 742 { 743 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling)); 744 745 if (bus->use_polling) { 746 bus->methods->soft_intr(bus); 747 } else { 748 softintr_schedule(bus->soft); 749 } 750 } 751 752 int 753 usb_activate(struct device *self, int act) 754 { 755 struct usb_softc *sc = (struct usb_softc *)self; 756 usbd_device_handle dev = sc->sc_port.device; 757 int i, rv = 0, r; 758 759 switch (act) { 760 case DVACT_DEACTIVATE: 761 sc->sc_bus->dying = 1; 762 if (dev != NULL && dev->cdesc != NULL && 763 dev->subdevs != NULL) { 764 for (i = 0; dev->subdevs[i]; i++) { 765 r = config_deactivate(dev->subdevs[i]); 766 if (r) 767 rv = r; 768 } 769 } 770 break; 771 } 772 return (rv); 773 } 774 775 int 776 usb_detach(struct device *self, int flags) 777 { 778 struct usb_softc *sc = (struct usb_softc *)self; 779 780 DPRINTF(("usb_detach: start\n")); 781 782 sc->sc_bus->dying = 1; 783 784 if (sc->sc_bus->root_hub != NULL) { 785 /* Make all devices disconnect. */ 786 if (sc->sc_port.device != NULL) 787 usb_disconnect_port(&sc->sc_port, self); 788 789 usb_rem_wait_task(sc->sc_bus->root_hub, &sc->sc_explore_task); 790 791 usbd_finish(); 792 } 793 794 if (sc->sc_bus->soft != NULL) { 795 softintr_disestablish(sc->sc_bus->soft); 796 sc->sc_bus->soft = NULL; 797 } 798 799 return (0); 800 } 801