1 /* $NetBSD: uhub.c,v 1.107 2009/09/04 18:14:41 dyoung Exp $ */ 2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 2004 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 spec: http://www.usb.org/developers/docs/usbspec.zip 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.107 2009/09/04 18:14:41 dyoung Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/device.h> 46 #include <sys/proc.h> 47 48 #include <sys/bus.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbdi.h> 52 #include <dev/usb/usbdi_util.h> 53 #include <dev/usb/usbdivar.h> 54 55 #ifdef UHUB_DEBUG 56 #define DPRINTF(x) if (uhubdebug) logprintf x 57 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x 58 int uhubdebug = 0; 59 #else 60 #define DPRINTF(x) 61 #define DPRINTFN(n,x) 62 #endif 63 64 struct uhub_softc { 65 USBBASEDEVICE sc_dev; /* base device */ 66 usbd_device_handle sc_hub; /* USB device */ 67 int sc_proto; /* device protocol */ 68 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 69 70 /* XXX second buffer needed because we can't suspend pipes yet */ 71 u_int8_t *sc_statusbuf; 72 u_int8_t *sc_status; 73 size_t sc_statuslen; 74 int sc_explorepending; 75 int sc_isehciroothub; /* see comment in uhub_intr() */ 76 77 u_char sc_running; 78 }; 79 80 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB) 81 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT) 82 83 #define PORTSTAT_ISSET(sc, port) \ 84 ((sc)->sc_status[(port) / 8] & (1 << ((port) % 8))) 85 86 Static usbd_status uhub_explore(usbd_device_handle hub); 87 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status); 88 89 90 /* 91 * We need two attachment points: 92 * hub to usb and hub to hub 93 * Every other driver only connects to hubs 94 */ 95 96 int uhub_match(device_t, cfdata_t, void *); 97 void uhub_attach(device_t, device_t, void *); 98 int uhub_rescan(device_t, const char *, const int *); 99 void uhub_childdet(device_t, device_t); 100 int uhub_detach(device_t, int); 101 int uhub_activate(device_t, enum devact); 102 extern struct cfdriver uhub_cd; 103 CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match, 104 uhub_attach, uhub_detach, uhub_activate, uhub_rescan, uhub_childdet, 105 DVF_DETACH_SHUTDOWN); 106 CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match, 107 uhub_attach, uhub_detach, uhub_activate, uhub_rescan, uhub_childdet); 108 109 int 110 uhub_match(device_t parent, cfdata_t match, void *aux) 111 { 112 struct usb_attach_arg *uaa = aux; 113 114 DPRINTFN(5,("uhub_match, uaa=%p\n", uaa)); 115 /* 116 * The subclass for hubs seems to be 0 for some and 1 for others, 117 * so we just ignore the subclass. 118 */ 119 if (uaa->class == UDCLASS_HUB) 120 return (UMATCH_DEVCLASS_DEVSUBCLASS); 121 return (UMATCH_NONE); 122 } 123 124 void 125 uhub_attach(device_t parent, device_t self, void *aux) 126 { 127 struct uhub_softc *sc = device_private(self); 128 struct usb_attach_arg *uaa = aux; 129 usbd_device_handle dev = uaa->device; 130 char *devinfop; 131 usbd_status err; 132 struct usbd_hub *hub = NULL; 133 usb_device_request_t req; 134 usb_hub_descriptor_t hubdesc; 135 int p, port, nports, nremov, pwrdly; 136 usbd_interface_handle iface; 137 usb_endpoint_descriptor_t *ed; 138 #if 0 /* notyet */ 139 struct usbd_tt *tts = NULL; 140 #endif 141 142 DPRINTFN(1,("uhub_attach\n")); 143 sc->sc_dev = self; 144 sc->sc_hub = dev; 145 sc->sc_proto = uaa->proto; 146 147 devinfop = usbd_devinfo_alloc(dev, 1); 148 aprint_naive("\n"); 149 aprint_normal(": %s\n", devinfop); 150 usbd_devinfo_free(devinfop); 151 152 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) { 153 aprint_normal_dev(self, "%s transaction translator%s\n", 154 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple", 155 UHUB_IS_SINGLE_TT(sc) ? "" : "s"); 156 } 157 158 err = usbd_set_config_index(dev, 0, 1); 159 if (err) { 160 DPRINTF(("%s: configuration failed, error=%s\n", 161 device_xname(sc->sc_dev), usbd_errstr(err))); 162 return; 163 } 164 165 if (dev->depth > USB_HUB_MAX_DEPTH) { 166 aprint_error_dev(self, 167 "hub depth (%d) exceeded, hub ignored\n", 168 USB_HUB_MAX_DEPTH); 169 return; 170 } 171 172 /* Get hub descriptor. */ 173 req.bmRequestType = UT_READ_CLASS_DEVICE; 174 req.bRequest = UR_GET_DESCRIPTOR; 175 USETW2(req.wValue, UDESC_HUB, 0); 176 USETW(req.wIndex, 0); 177 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 178 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 179 err = usbd_do_request(dev, &req, &hubdesc); 180 nports = hubdesc.bNbrPorts; 181 if (!err && nports > 7) { 182 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 183 err = usbd_do_request(dev, &req, &hubdesc); 184 } 185 if (err) { 186 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 187 device_xname(sc->sc_dev), usbd_errstr(err))); 188 return; 189 } 190 191 for (nremov = 0, port = 1; port <= nports; port++) 192 if (!UHD_NOT_REMOV(&hubdesc, port)) 193 nremov++; 194 aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n", 195 nports, nports != 1 ? "s" : "", nremov, 196 dev->self_powered ? "self" : "bus"); 197 198 if (nports == 0) { 199 aprint_debug_dev(self, "no ports, hub ignored\n"); 200 goto bad; 201 } 202 203 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 204 M_USBDEV, M_NOWAIT); 205 if (hub == NULL) 206 return; 207 dev->hub = hub; 208 dev->hub->hubsoftc = sc; 209 hub->explore = uhub_explore; 210 hub->hubdesc = hubdesc; 211 212 /* Set up interrupt pipe. */ 213 err = usbd_device2interface_handle(dev, 0, &iface); 214 if (err) { 215 aprint_error_dev(self, "no interface handle\n"); 216 goto bad; 217 } 218 219 if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) { 220 err = usbd_set_interface(iface, 1); 221 if (err) 222 aprint_error_dev(self, "can't enable multiple TTs\n"); 223 } 224 225 ed = usbd_interface2endpoint_descriptor(iface, 0); 226 if (ed == NULL) { 227 aprint_error_dev(self, "no endpoint descriptor\n"); 228 goto bad; 229 } 230 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 231 aprint_error_dev(self, "bad interrupt endpoint\n"); 232 goto bad; 233 } 234 235 sc->sc_statuslen = (nports + 1 + 7) / 8; 236 sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT); 237 if (!sc->sc_statusbuf) 238 goto bad; 239 sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT); 240 if (!sc->sc_status) 241 goto bad; 242 if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci")) 243 sc->sc_isehciroothub = 1; 244 245 /* force initial scan */ 246 memset(sc->sc_status, 0xff, sc->sc_statuslen); 247 sc->sc_explorepending = 1; 248 249 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 250 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf, 251 sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL); 252 if (err) { 253 aprint_error_dev(self, "cannot open interrupt pipe\n"); 254 goto bad; 255 } 256 257 /* Wait with power off for a while. */ 258 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 259 260 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev); 261 262 /* 263 * To have the best chance of success we do things in the exact same 264 * order as Windoze98. This should not be necessary, but some 265 * devices do not follow the USB specs to the letter. 266 * 267 * These are the events on the bus when a hub is attached: 268 * Get device and config descriptors (see attach code) 269 * Get hub descriptor (see above) 270 * For all ports 271 * turn on power 272 * wait for power to become stable 273 * (all below happens in explore code) 274 * For all ports 275 * clear C_PORT_CONNECTION 276 * For all ports 277 * get port status 278 * if device connected 279 * wait 100 ms 280 * turn on reset 281 * wait 282 * clear C_PORT_RESET 283 * get port status 284 * proceed with device attachment 285 */ 286 287 #if 0 288 if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) { 289 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 290 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT); 291 if (!tts) 292 goto bad; 293 } 294 #endif 295 /* Set up data structures */ 296 for (p = 0; p < nports; p++) { 297 struct usbd_port *up = &hub->ports[p]; 298 up->device = NULL; 299 up->parent = dev; 300 up->portno = p+1; 301 if (dev->self_powered) 302 /* Self powered hub, give ports maximum current. */ 303 up->power = USB_MAX_POWER; 304 else 305 up->power = USB_MIN_POWER; 306 up->restartcnt = 0; 307 up->reattach = 0; 308 #if 0 309 if (UHUB_IS_HIGH_SPEED(sc)) { 310 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 311 up->tt->hub = hub; 312 } else { 313 up->tt = NULL; 314 } 315 #endif 316 } 317 318 /* XXX should check for none, individual, or ganged power? */ 319 320 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 321 + USB_EXTRA_POWER_UP_TIME; 322 for (port = 1; port <= nports; port++) { 323 /* Turn the power on. */ 324 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 325 if (err) 326 aprint_error_dev(self, "port %d power on failed, %s\n", 327 port, usbd_errstr(err)); 328 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 329 } 330 331 /* Wait for stable power if we are not a root hub */ 332 if (dev->powersrc->parent != NULL) 333 usbd_delay_ms(dev, pwrdly); 334 335 /* The usual exploration will finish the setup. */ 336 337 sc->sc_running = 1; 338 339 if (!pmf_device_register(self, NULL, NULL)) 340 aprint_error_dev(self, "couldn't establish power handler\n"); 341 342 return; 343 344 bad: 345 if (sc->sc_status) 346 free(sc->sc_status, M_USBDEV); 347 if (sc->sc_statusbuf) 348 free(sc->sc_statusbuf, M_USBDEV); 349 if (hub) 350 free(hub, M_USBDEV); 351 dev->hub = NULL; 352 return; 353 } 354 355 usbd_status 356 uhub_explore(usbd_device_handle dev) 357 { 358 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 359 struct uhub_softc *sc = dev->hub->hubsoftc; 360 struct usbd_port *up; 361 usbd_status err; 362 int speed; 363 int port; 364 int change, status, reconnect; 365 366 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 367 368 if (!sc->sc_running) 369 return (USBD_NOT_STARTED); 370 371 /* Ignore hubs that are too deep. */ 372 if (dev->depth > USB_HUB_MAX_DEPTH) 373 return (USBD_TOO_DEEP); 374 375 if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */ 376 usb_hub_status_t hs; 377 378 err = usbd_get_hub_status(dev, &hs); 379 if (err) { 380 DPRINTF(("%s: get hub status failed, err=%d\n", 381 device_xname(sc->sc_dev), err)); 382 } else { 383 /* just acknowledge */ 384 status = UGETW(hs.wHubStatus); 385 change = UGETW(hs.wHubChange); 386 if (change & UHS_LOCAL_POWER) 387 usbd_clear_hub_feature(dev, 388 UHF_C_HUB_LOCAL_POWER); 389 if (change & UHS_OVER_CURRENT) 390 usbd_clear_hub_feature(dev, 391 UHF_C_HUB_OVER_CURRENT); 392 } 393 } 394 395 for (port = 1; port <= hd->bNbrPorts; port++) { 396 up = &dev->hub->ports[port-1]; 397 398 /* reattach is needed after firmware upload */ 399 reconnect = up->reattach; 400 up->reattach = 0; 401 402 status = change = 0; 403 404 /* don't check if no change summary notification */ 405 if (PORTSTAT_ISSET(sc, port) || reconnect) { 406 err = usbd_get_port_status(dev, port, &up->status); 407 if (err) { 408 DPRINTF(("uhub_explore: get port stat failed, " 409 "error=%s\n", usbd_errstr(err))); 410 continue; 411 } 412 status = UGETW(up->status.wPortStatus); 413 change = UGETW(up->status.wPortChange); 414 #if 0 415 printf("%s port %d: s/c=%x/%x\n", 416 device_xname(sc->sc_dev), port, status, change); 417 #endif 418 } 419 if (!change && !reconnect) { 420 /* No status change, just do recursive explore. */ 421 if (up->device != NULL && up->device->hub != NULL) 422 up->device->hub->explore(up->device); 423 continue; 424 } 425 426 if (change & UPS_C_PORT_ENABLED) { 427 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 428 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 429 if (change & UPS_C_CONNECT_STATUS) { 430 /* Ignore the port error if the device 431 vanished. */ 432 } else if (status & UPS_PORT_ENABLED) { 433 aprint_error_dev(sc->sc_dev, 434 "illegal enable change, port %d\n", port); 435 } else { 436 /* Port error condition. */ 437 if (up->restartcnt) /* no message first time */ 438 aprint_error_dev(sc->sc_dev, 439 "port error, restarting port %d\n", 440 port); 441 442 if (up->restartcnt++ < USBD_RESTART_MAX) 443 goto disco; 444 else 445 aprint_error_dev(sc->sc_dev, 446 "port error, giving up port %d\n", 447 port); 448 } 449 } 450 451 /* XXX handle overcurrent and resume events! */ 452 453 if (!(change & UPS_C_CONNECT_STATUS)) 454 continue; 455 456 /* We have a connect status change, handle it. */ 457 458 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 459 dev->address, port)); 460 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 461 /* 462 * If there is already a device on the port the change status 463 * must mean that is has disconnected. Looking at the 464 * current connect status is not enough to figure this out 465 * since a new unit may have been connected before we handle 466 * the disconnect. 467 */ 468 disco: 469 if (up->device != NULL) { 470 /* Disconnected */ 471 DPRINTF(("uhub_explore: device addr=%d disappeared " 472 "on port %d\n", up->device->address, port)); 473 usb_disconnect_port(up, sc->sc_dev); 474 usbd_clear_port_feature(dev, port, 475 UHF_C_PORT_CONNECTION); 476 } 477 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 478 /* Nothing connected, just ignore it. */ 479 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 480 "_STATUS\n", port)); 481 continue; 482 } 483 484 /* Connected */ 485 486 if (!(status & UPS_PORT_POWER)) 487 aprint_normal_dev(sc->sc_dev, 488 "strange, connected port %d has no power\n", port); 489 490 /* Wait for maximum device power up time. */ 491 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 492 493 /* Reset port, which implies enabling it. */ 494 if (usbd_reset_port(dev, port, &up->status)) { 495 aprint_error_dev(sc->sc_dev, 496 "port %d reset failed\n", port); 497 continue; 498 } 499 /* Get port status again, it might have changed during reset */ 500 err = usbd_get_port_status(dev, port, &up->status); 501 if (err) { 502 DPRINTF(("uhub_explore: get port status failed, " 503 "error=%s\n", usbd_errstr(err))); 504 continue; 505 } 506 status = UGETW(up->status.wPortStatus); 507 change = UGETW(up->status.wPortChange); 508 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 509 /* Nothing connected, just ignore it. */ 510 #ifdef DIAGNOSTIC 511 aprint_debug_dev(sc->sc_dev, 512 "port %d, device disappeared after reset\n", port); 513 #endif 514 continue; 515 } 516 517 /* Figure out device speed */ 518 if (status & UPS_HIGH_SPEED) 519 speed = USB_SPEED_HIGH; 520 else if (status & UPS_LOW_SPEED) 521 speed = USB_SPEED_LOW; 522 else 523 speed = USB_SPEED_FULL; 524 /* Get device info and set its address. */ 525 err = usbd_new_device(sc->sc_dev, dev->bus, 526 dev->depth + 1, speed, port, up); 527 /* XXX retry a few times? */ 528 if (err) { 529 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 530 "error=%s\n", usbd_errstr(err))); 531 /* Avoid addressing problems by disabling. */ 532 /* usbd_reset_port(dev, port, &up->status); */ 533 534 /* 535 * The unit refused to accept a new address, or had 536 * some other serious problem. Since we cannot leave 537 * at 0 we have to disable the port instead. 538 */ 539 aprint_error_dev(sc->sc_dev, 540 "device problem, disabling port %d\n", port); 541 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 542 } else { 543 /* The port set up succeeded, reset error count. */ 544 up->restartcnt = 0; 545 546 if (up->device->hub) 547 up->device->hub->explore(up->device); 548 } 549 } 550 /* enable status change notifications again */ 551 if (!sc->sc_isehciroothub) 552 memset(sc->sc_status, 0, sc->sc_statuslen); 553 sc->sc_explorepending = 0; 554 return (USBD_NORMAL_COMPLETION); 555 } 556 557 int 558 uhub_activate(device_t self, enum devact act) 559 { 560 struct uhub_softc *sc = device_private(self); 561 struct usbd_hub *hub = sc->sc_hub->hub; 562 usbd_device_handle dev; 563 int nports, port, i; 564 565 switch (act) { 566 case DVACT_ACTIVATE: 567 return (EOPNOTSUPP); 568 569 case DVACT_DEACTIVATE: 570 if (hub == NULL) /* malfunctioning hub */ 571 break; 572 nports = hub->hubdesc.bNbrPorts; 573 for(port = 0; port < nports; port++) { 574 dev = hub->ports[port].device; 575 if (!dev) 576 continue; 577 for (i = 0; i < dev->subdevlen; i++) 578 if (dev->subdevs[i]) 579 config_deactivate(dev->subdevs[i]); 580 } 581 break; 582 } 583 return (0); 584 } 585 586 /* 587 * Called from process context when the hub is gone. 588 * Detach all devices on active ports. 589 */ 590 int 591 uhub_detach(device_t self, int flags) 592 { 593 struct uhub_softc *sc = device_private(self); 594 struct usbd_hub *hub = sc->sc_hub->hub; 595 struct usbd_port *rup; 596 int port, nports; 597 598 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 599 600 if (hub == NULL) /* Must be partially working */ 601 return (0); 602 603 pmf_device_deregister(self); 604 usbd_abort_pipe(sc->sc_ipipe); 605 usbd_close_pipe(sc->sc_ipipe); 606 607 nports = hub->hubdesc.bNbrPorts; 608 for(port = 0; port < nports; port++) { 609 rup = &hub->ports[port]; 610 if (rup->device != NULL) 611 usb_disconnect_port(rup, self); 612 } 613 614 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev); 615 616 #if 0 617 if (hub->ports[0].tt) 618 free(hub->ports[0].tt, M_USBDEV); 619 #endif 620 free(hub, M_USBDEV); 621 sc->sc_hub->hub = NULL; 622 if (sc->sc_status) 623 free(sc->sc_status, M_USBDEV); 624 if (sc->sc_statusbuf) 625 free(sc->sc_statusbuf, M_USBDEV); 626 627 return (0); 628 } 629 630 int 631 uhub_rescan(device_t self, const char *ifattr, const int *locators) 632 { 633 struct uhub_softc *sc = device_private(self); 634 struct usbd_hub *hub = sc->sc_hub->hub; 635 usbd_device_handle dev; 636 int port, err; 637 638 for (port = 0; port < hub->hubdesc.bNbrPorts; port++) { 639 dev = hub->ports[port].device; 640 if (dev == NULL) 641 continue; 642 err = usbd_reattach_device(sc->sc_dev, dev, port, locators); 643 } 644 return 0; 645 } 646 647 /* Called when a device has been detached from it */ 648 void 649 uhub_childdet(device_t self, device_t child) 650 { 651 struct uhub_softc *sc = device_private(self); 652 usbd_device_handle devhub = sc->sc_hub; 653 usbd_device_handle dev; 654 int nports; 655 int port; 656 int i; 657 658 if (!devhub->hub) 659 /* should never happen; children are only created after init */ 660 panic("hub not fully initialised, but child deleted?"); 661 662 nports = devhub->hub->hubdesc.bNbrPorts; 663 for (port = 0; port < nports; port++) { 664 dev = devhub->hub->ports[port].device; 665 if (!dev) 666 continue; 667 for (i = 0; i < dev->subdevlen; i++) { 668 if (dev->subdevs[i] == child) { 669 dev->subdevs[i] = NULL; 670 dev->nifaces_claimed--; 671 } 672 } 673 } 674 } 675 676 677 /* 678 * Hub interrupt. 679 * This an indication that some port has changed status. 680 * Notify the bus event handler thread that we need 681 * to be explored again. 682 */ 683 void 684 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 685 { 686 struct uhub_softc *sc = addr; 687 688 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 689 690 if (status == USBD_STALLED) 691 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 692 else if (status == USBD_NORMAL_COMPLETION && 693 !sc->sc_explorepending) { 694 /* 695 * Make sure the status is not overwritten in between. 696 * XXX we should suspend the pipe instead 697 */ 698 memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen); 699 sc->sc_explorepending = 1; 700 usb_needs_explore(sc->sc_hub); 701 } 702 /* 703 * XXX workaround for broken implementation of the interrupt 704 * pipe in EHCI root hub emulation which doesn't resend 705 * status change notifications until handled: force a rescan 706 * of the ports we touched in the last run 707 */ 708 if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending && 709 sc->sc_isehciroothub) 710 usb_needs_explore(sc->sc_hub); 711 } 712