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