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