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