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