1/* $NetBSD$ */ 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$"); 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 60int uhubdebug = 0; 61#else 62#define DPRINTF(x) 63#define DPRINTFN(n,x) 64#endif 65 66struct 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 88Static usbd_status uhub_explore(usbd_device_handle hub); 89Static 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 98int uhub_match(device_t, cfdata_t, void *); 99void uhub_attach(device_t, device_t, void *); 100int uhub_rescan(device_t, const char *, const int *); 101void uhub_childdet(device_t, device_t); 102int uhub_detach(device_t, int); 103extern struct cfdriver uhub_cd; 104CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match, 105 uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet, 106 DVF_DETACH_SHUTDOWN); 107CFATTACH_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 */ 115int uhub_ubermatch = 0; 116 117int 118uhub_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 138void 139uhub_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 369usbd_status 370uhub_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 (!(change & UPS_C_CONNECT_STATUS)) 468 continue; 469 470 /* We have a connect status change, handle it. */ 471 472 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 473 dev->address, port)); 474 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 475 /* 476 * If there is already a device on the port the change status 477 * must mean that is has disconnected. Looking at the 478 * current connect status is not enough to figure this out 479 * since a new unit may have been connected before we handle 480 * the disconnect. 481 */ 482 disco: 483 if (up->device != NULL) { 484 /* Disconnected */ 485 DPRINTF(("uhub_explore: device addr=%d disappeared " 486 "on port %d\n", up->device->address, port)); 487 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE); 488 usbd_clear_port_feature(dev, port, 489 UHF_C_PORT_CONNECTION); 490 } 491 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 492 /* Nothing connected, just ignore it. */ 493 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 494 "_STATUS\n", port)); 495 continue; 496 } 497 498 /* Connected */ 499 500 if (!(status & UPS_PORT_POWER)) 501 aprint_normal_dev(sc->sc_dev, 502 "strange, connected port %d has no power\n", port); 503 504 /* Wait for maximum device power up time. */ 505 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 506 507 /* Reset port, which implies enabling it. */ 508 if (usbd_reset_port(dev, port, &up->status)) { 509 aprint_error_dev(sc->sc_dev, 510 "port %d reset failed\n", port); 511 continue; 512 } 513 /* Get port status again, it might have changed during reset */ 514 err = usbd_get_port_status(dev, port, &up->status); 515 if (err) { 516 DPRINTF(("uhub_explore: get port status failed, " 517 "error=%s\n", usbd_errstr(err))); 518 continue; 519 } 520 status = UGETW(up->status.wPortStatus); 521 change = UGETW(up->status.wPortChange); 522 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 523 /* Nothing connected, just ignore it. */ 524#ifdef DIAGNOSTIC 525 aprint_debug_dev(sc->sc_dev, 526 "port %d, device disappeared after reset\n", port); 527#endif 528 continue; 529 } 530 if (!(status & UPS_PORT_ENABLED)) { 531 /* Not allowed send/receive packet. */ 532#ifdef DIAGNOSTIC 533 printf("%s: port %d, device not enable\n", 534 device_xname(sc->sc_dev), port); 535#endif 536 continue; 537 } 538 539 /* Figure out device speed */ 540 if (status & UPS_HIGH_SPEED) 541 speed = USB_SPEED_HIGH; 542 else if (status & UPS_LOW_SPEED) 543 speed = USB_SPEED_LOW; 544 else 545 speed = USB_SPEED_FULL; 546 /* Get device info and set its address. */ 547 err = usbd_new_device(sc->sc_dev, dev->bus, 548 dev->depth + 1, speed, port, up); 549 /* XXX retry a few times? */ 550 if (err) { 551 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, " 552 "error=%s\n", usbd_errstr(err))); 553 /* Avoid addressing problems by disabling. */ 554 /* usbd_reset_port(dev, port, &up->status); */ 555 556 /* 557 * The unit refused to accept a new address, or had 558 * some other serious problem. Since we cannot leave 559 * at 0 we have to disable the port instead. 560 */ 561 aprint_error_dev(sc->sc_dev, 562 "device problem, disabling port %d\n", port); 563 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 564 } else { 565 /* The port set up succeeded, reset error count. */ 566 up->restartcnt = 0; 567 568 if (up->device->hub) 569 up->device->hub->explore(up->device); 570 } 571 } 572 /* enable status change notifications again */ 573 if (!sc->sc_isehciroothub) 574 memset(sc->sc_status, 0, sc->sc_statuslen); 575 sc->sc_explorepending = 0; 576 return (USBD_NORMAL_COMPLETION); 577} 578 579/* 580 * Called from process context when the hub is gone. 581 * Detach all devices on active ports. 582 */ 583int 584uhub_detach(device_t self, int flags) 585{ 586 struct uhub_softc *sc = device_private(self); 587 struct usbd_hub *hub = sc->sc_hub->hub; 588 struct usbd_port *rup; 589 int nports, port, rc; 590 591 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 592 593 if (hub == NULL) /* Must be partially working */ 594 return (0); 595 596 /* XXXSMP usb */ 597 KERNEL_LOCK(1, curlwp); 598 599 nports = hub->hubdesc.bNbrPorts; 600 for(port = 0; port < nports; port++) { 601 rup = &hub->ports[port]; 602 if (rup->device == NULL) 603 continue; 604 if ((rc = usb_disconnect_port(rup, self, flags)) != 0) { 605 /* XXXSMP usb */ 606 KERNEL_UNLOCK_ONE(curlwp); 607 608 return rc; 609 } 610 } 611 612 pmf_device_deregister(self); 613 usbd_abort_pipe(sc->sc_ipipe); 614 usbd_close_pipe(sc->sc_ipipe); 615 616 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev); 617 618#if 0 619 if (hub->ports[0].tt) 620 free(hub->ports[0].tt, M_USBDEV); 621#endif 622 free(hub, M_USBDEV); 623 sc->sc_hub->hub = NULL; 624 if (sc->sc_status) 625 free(sc->sc_status, M_USBDEV); 626 if (sc->sc_statusbuf) 627 free(sc->sc_statusbuf, M_USBDEV); 628 629 /* XXXSMP usb */ 630 KERNEL_UNLOCK_ONE(curlwp); 631 632 return (0); 633} 634 635int 636uhub_rescan(device_t self, const char *ifattr, const int *locators) 637{ 638 struct uhub_softc *sc = device_private(self); 639 struct usbd_hub *hub = sc->sc_hub->hub; 640 usbd_device_handle dev; 641 int port, err; 642 643 for (port = 0; port < hub->hubdesc.bNbrPorts; port++) { 644 dev = hub->ports[port].device; 645 if (dev == NULL) 646 continue; 647 err = usbd_reattach_device(sc->sc_dev, dev, port, locators); 648 } 649 return 0; 650} 651 652/* Called when a device has been detached from it */ 653void 654uhub_childdet(device_t self, device_t child) 655{ 656 struct uhub_softc *sc = device_private(self); 657 usbd_device_handle devhub = sc->sc_hub; 658 usbd_device_handle dev; 659 int nports; 660 int port; 661 int i; 662 663 if (!devhub->hub) 664 /* should never happen; children are only created after init */ 665 panic("hub not fully initialised, but child deleted?"); 666 667 nports = devhub->hub->hubdesc.bNbrPorts; 668 for (port = 0; port < nports; port++) { 669 dev = devhub->hub->ports[port].device; 670 if (!dev) 671 continue; 672 for (i = 0; i < dev->subdevlen; i++) { 673 if (dev->subdevs[i] == child) { 674 dev->subdevs[i] = NULL; 675 dev->nifaces_claimed--; 676 } 677 } 678 if (dev->nifaces_claimed == 0) { 679 free(dev->subdevs, M_USB); 680 dev->subdevs = NULL; 681 dev->subdevlen = 0; 682 } 683 } 684} 685 686 687/* 688 * Hub interrupt. 689 * This an indication that some port has changed status. 690 * Notify the bus event handler thread that we need 691 * to be explored again. 692 */ 693void 694uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 695{ 696 struct uhub_softc *sc = addr; 697 698 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 699 700 if (status == USBD_STALLED) 701 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 702 else if (status == USBD_NORMAL_COMPLETION && 703 !sc->sc_explorepending) { 704 /* 705 * Make sure the status is not overwritten in between. 706 * XXX we should suspend the pipe instead 707 */ 708 memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen); 709 sc->sc_explorepending = 1; 710 usb_needs_explore(sc->sc_hub); 711 } 712 /* 713 * XXX workaround for broken implementation of the interrupt 714 * pipe in EHCI root hub emulation which doesn't resend 715 * status change notifications until handled: force a rescan 716 * of the ports we touched in the last run 717 */ 718 if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending && 719 sc->sc_isehciroothub) 720 usb_needs_explore(sc->sc_hub); 721} 722