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