1 /* $OpenBSD: uhub.c,v 1.21 2003/07/08 13:19:09 nate 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) 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 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 void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status); 87 88 #if defined(__FreeBSD__) 89 Static bus_child_detached_t uhub_child_detached; 90 #endif 91 92 93 /* 94 * We need two attachment points: 95 * hub to usb and hub to hub 96 * Every other driver only connects to hubs 97 */ 98 99 #if defined(__NetBSD__) || defined(__OpenBSD__) 100 USB_DECLARE_DRIVER(uhub); 101 102 #if defined(__NetBSD__) 103 /* Create the driver instance for the hub connected to hub case */ 104 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc), 105 uhub_match, uhub_attach, uhub_detach, uhub_activate); 106 #else 107 struct cfattach uhub_uhub_ca = { 108 sizeof(struct uhub_softc), uhub_match, uhub_attach, 109 uhub_detach, uhub_activate 110 }; 111 #endif 112 #elif defined(__FreeBSD__) 113 USB_DECLARE_DRIVER_INIT(uhub, 114 DEVMETHOD(bus_child_detached, uhub_child_detached)); 115 116 /* Create the driver instance for the hub connected to usb case. */ 117 devclass_t uhubroot_devclass; 118 119 Static device_method_t uhubroot_methods[] = { 120 DEVMETHOD(device_probe, uhub_match), 121 DEVMETHOD(device_attach, uhub_attach), 122 123 /* detach is not allowed for a root hub */ 124 {0,0} 125 }; 126 127 Static driver_t uhubroot_driver = { 128 "uhub", 129 uhubroot_methods, 130 sizeof(struct uhub_softc) 131 }; 132 #endif 133 134 USB_MATCH(uhub) 135 { 136 USB_MATCH_START(uhub, uaa); 137 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 138 139 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 140 /* 141 * The subclass for hubs seems to be 0 for some and 1 for others, 142 * so we just ignore the subclass. 143 */ 144 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 145 return (UMATCH_DEVCLASS_DEVSUBCLASS); 146 return (UMATCH_NONE); 147 } 148 149 USB_ATTACH(uhub) 150 { 151 USB_ATTACH_START(uhub, sc, uaa); 152 usbd_device_handle dev = uaa->device; 153 char devinfo[1024]; 154 usbd_status err; 155 struct usbd_hub *hub; 156 usb_device_request_t req; 157 usb_hub_descriptor_t hubdesc; 158 int p, port, nports, nremov, pwrdly; 159 usbd_interface_handle iface; 160 usb_endpoint_descriptor_t *ed; 161 162 DPRINTFN(1,("uhub_attach\n")); 163 sc->sc_hub = dev; 164 usbd_devinfo(dev, 1, devinfo, sizeof devinfo); 165 USB_ATTACH_SETUP; 166 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 167 168 err = usbd_set_config_index(dev, 0, 1); 169 if (err) { 170 DPRINTF(("%s: configuration failed, error=%s\n", 171 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 172 USB_ATTACH_ERROR_RETURN; 173 } 174 175 if (dev->depth > USB_HUB_MAX_DEPTH) { 176 printf("%s: hub depth (%d) exceeded, hub ignored\n", 177 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 178 USB_ATTACH_ERROR_RETURN; 179 } 180 181 /* Get hub descriptor. */ 182 req.bmRequestType = UT_READ_CLASS_DEVICE; 183 req.bRequest = UR_GET_DESCRIPTOR; 184 USETW(req.wValue, 0); 185 USETW(req.wIndex, 0); 186 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 187 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 188 err = usbd_do_request(dev, &req, &hubdesc); 189 nports = hubdesc.bNbrPorts; 190 if (!err && nports > 7) { 191 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 192 err = usbd_do_request(dev, &req, &hubdesc); 193 } 194 if (err) { 195 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 196 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 197 USB_ATTACH_ERROR_RETURN; 198 } 199 200 for (nremov = 0, port = 1; port <= nports; port++) 201 if (!UHD_NOT_REMOV(&hubdesc, port)) 202 nremov++; 203 printf("%s: %d port%s with %d removable, %s powered\n", 204 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 205 nremov, dev->self_powered ? "self" : "bus"); 206 207 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 208 M_USBDEV, M_NOWAIT); 209 if (hub == NULL) 210 USB_ATTACH_ERROR_RETURN; 211 dev->hub = hub; 212 dev->hub->hubsoftc = sc; 213 hub->explore = uhub_explore; 214 hub->hubdesc = hubdesc; 215 216 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 217 "parent->selfpowered=%d\n", 218 dev->self_powered, dev->powersrc->parent, 219 dev->powersrc->parent ? 220 dev->powersrc->parent->self_powered : 0)); 221 222 if (!dev->self_powered && dev->powersrc->parent != NULL && 223 !dev->powersrc->parent->self_powered) { 224 printf("%s: bus powered hub connected to bus powered hub, " 225 "ignored\n", USBDEVNAME(sc->sc_dev)); 226 goto bad; 227 } 228 229 /* Set up interrupt pipe. */ 230 err = usbd_device2interface_handle(dev, 0, &iface); 231 if (err) { 232 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev)); 233 goto bad; 234 } 235 ed = usbd_interface2endpoint_descriptor(iface, 0); 236 if (ed == NULL) { 237 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 238 goto bad; 239 } 240 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 241 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 242 goto bad; 243 } 244 245 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 246 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 247 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL); 248 if (err) { 249 printf("%s: cannot open interrupt pipe\n", 250 USBDEVNAME(sc->sc_dev)); 251 goto bad; 252 } 253 254 /* Wait with power off for a while. */ 255 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 256 257 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev)); 258 259 /* 260 * To have the best chance of success we do things in the exact same 261 * order as Windoze98. This should not be necessary, but some 262 * devices do not follow the USB specs to the letter. 263 * 264 * These are the events on the bus when a hub is attached: 265 * Get device and config descriptors (see attach code) 266 * Get hub descriptor (see above) 267 * For all ports 268 * turn on power 269 * wait for power to become stable 270 * (all below happens in explore code) 271 * For all ports 272 * clear C_PORT_CONNECTION 273 * For all ports 274 * get port status 275 * if device connected 276 * wait 100 ms 277 * turn on reset 278 * wait 279 * clear C_PORT_RESET 280 * get port status 281 * proceed with device attachment 282 */ 283 284 /* Set up data structures */ 285 for (p = 0; p < nports; p++) { 286 struct usbd_port *up = &hub->ports[p]; 287 up->device = 0; 288 up->parent = dev; 289 up->portno = p+1; 290 if (dev->self_powered) 291 /* Self powered hub, give ports maximum current. */ 292 up->power = USB_MAX_POWER; 293 else 294 up->power = USB_MIN_POWER; 295 } 296 297 /* XXX should check for none, individual, or ganged power? */ 298 299 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 300 + USB_EXTRA_POWER_UP_TIME; 301 for (port = 1; port <= nports; port++) { 302 /* Turn the power on. */ 303 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 304 if (err) 305 printf("%s: port %d power on failed, %s\n", 306 USBDEVNAME(sc->sc_dev), port, 307 usbd_errstr(err)); 308 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 309 /* Wait for stable power. */ 310 usbd_delay_ms(dev, pwrdly); 311 } 312 313 /* The usual exploration will finish the setup. */ 314 315 sc->sc_running = 1; 316 317 USB_ATTACH_SUCCESS_RETURN; 318 319 bad: 320 free(hub, M_USBDEV); 321 dev->hub = 0; 322 USB_ATTACH_ERROR_RETURN; 323 } 324 325 usbd_status 326 uhub_explore(usbd_device_handle dev) 327 { 328 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 329 struct uhub_softc *sc = dev->hub->hubsoftc; 330 struct usbd_port *up; 331 usbd_status err; 332 int speed; 333 int port; 334 int change, status; 335 336 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 337 338 if (!sc->sc_running) 339 return (USBD_NOT_STARTED); 340 341 /* Ignore hubs that are too deep. */ 342 if (dev->depth > USB_HUB_MAX_DEPTH) 343 return (USBD_TOO_DEEP); 344 345 for(port = 1; port <= hd->bNbrPorts; port++) { 346 up = &dev->hub->ports[port-1]; 347 err = usbd_get_port_status(dev, port, &up->status); 348 if (err) { 349 DPRINTF(("uhub_explore: get port status failed, " 350 "error=%s\n", usbd_errstr(err))); 351 continue; 352 } 353 status = UGETW(up->status.wPortStatus); 354 change = UGETW(up->status.wPortChange); 355 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", 356 USBDEVNAME(sc->sc_dev), port, status, change)); 357 if (change & UPS_C_PORT_ENABLED) { 358 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 359 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 360 if (status & UPS_PORT_ENABLED) { 361 printf("%s: illegal enable change, port %d\n", 362 USBDEVNAME(sc->sc_dev), port); 363 } else { 364 /* Port error condition. */ 365 if (up->restartcnt) /* no message first time */ 366 printf("%s: port error, restarting " 367 "port %d\n", 368 USBDEVNAME(sc->sc_dev), port); 369 370 if (up->restartcnt++ < USBD_RESTART_MAX) 371 goto disco; 372 else 373 printf("%s: port error, giving up " 374 "port %d\n", 375 USBDEVNAME(sc->sc_dev), port); 376 } 377 } 378 if (!(change & UPS_C_CONNECT_STATUS)) { 379 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 380 "STATUS\n", port)); 381 /* No status change, just do recursive explore. */ 382 if (up->device != NULL && up->device->hub != NULL) 383 up->device->hub->explore(up->device); 384 #if 0 && defined(DIAGNOSTIC) 385 if (up->device == NULL && 386 (status & UPS_CURRENT_CONNECT_STATUS)) 387 printf("%s: connected, no device\n", 388 USBDEVNAME(sc->sc_dev)); 389 #endif 390 continue; 391 } 392 393 /* We have a connect status change, handle it. */ 394 395 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 396 dev->address, port)); 397 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 398 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ 399 /* 400 * If there is already a device on the port the change status 401 * must mean that is has disconnected. Looking at the 402 * current connect status is not enough to figure this out 403 * since a new unit may have been connected before we handle 404 * the disconnect. 405 */ 406 disco: 407 if (up->device != NULL) { 408 /* Disconnected */ 409 DPRINTF(("uhub_explore: device addr=%d disappeared " 410 "on port %d\n", up->device->address, port)); 411 usb_disconnect_port(up, USBDEV(sc->sc_dev)); 412 usbd_clear_port_feature(dev, port, 413 UHF_C_PORT_CONNECTION); 414 } 415 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 416 /* Nothing connected, just ignore it. */ 417 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 418 "_STATUS\n", port)); 419 continue; 420 } 421 422 /* Connected */ 423 424 if (!(status & UPS_PORT_POWER)) 425 printf("%s: strange, connected port %d has no power\n", 426 USBDEVNAME(sc->sc_dev), port); 427 428 /* Wait for maximum device power up time. */ 429 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 430 431 /* Reset port, which implies enabling it. */ 432 if (usbd_reset_port(dev, port, &up->status)) { 433 printf("%s: port %d reset failed\n", 434 USBDEVNAME(sc->sc_dev), port); 435 continue; 436 } 437 /* Get port status again, it might have changed during reset */ 438 err = usbd_get_port_status(dev, port, &up->status); 439 if (err) { 440 DPRINTF(("uhub_explore: get port status failed, " 441 "error=%s\n", usbd_errstr(err))); 442 continue; 443 } 444 status = UGETW(up->status.wPortStatus); 445 change = UGETW(up->status.wPortChange); 446 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 447 /* Nothing connected, just ignore it. */ 448 #ifdef DIAGNOSTIC 449 printf("%s: port %d, device disappeared after reset\n", 450 USBDEVNAME(sc->sc_dev), port); 451 #endif 452 continue; 453 } 454 455 /* Figure out device speed */ 456 if (status & UPS_HIGH_SPEED) 457 speed = USB_SPEED_HIGH; 458 else if (status & UPS_LOW_SPEED) 459 speed = USB_SPEED_LOW; 460 else 461 speed = USB_SPEED_FULL; 462 /* Get device info and set its address. */ 463 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 464 dev->depth + 1, speed, port, up); 465 /* XXX retry a few times? */ 466 if (err) { 467 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 468 "error=%s\n", usbd_errstr(err))); 469 /* Avoid addressing problems by disabling. */ 470 /* usbd_reset_port(dev, port, &up->status); */ 471 472 /* 473 * The unit refused to accept a new address, or had 474 * some other serious problem. Since we cannot leave 475 * at 0 we have to disable the port instead. 476 */ 477 printf("%s: device problem, disabling port %d\n", 478 USBDEVNAME(sc->sc_dev), port); 479 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 480 } else { 481 /* The port set up succeeded, reset error count. */ 482 up->restartcnt = 0; 483 484 if (up->device->hub) 485 up->device->hub->explore(up->device); 486 } 487 } 488 return (USBD_NORMAL_COMPLETION); 489 } 490 491 #if defined(__NetBSD__) || defined(__OpenBSD__) 492 int 493 uhub_activate(device_ptr_t self, enum devact act) 494 { 495 struct uhub_softc *sc = (struct uhub_softc *)self; 496 struct usbd_hub *hub = sc->sc_hub->hub; 497 usbd_device_handle dev; 498 int nports, port, i; 499 500 switch (act) { 501 case DVACT_ACTIVATE: 502 return (EOPNOTSUPP); 503 504 case DVACT_DEACTIVATE: 505 if (hub == NULL) /* malfunctioning hub */ 506 break; 507 nports = hub->hubdesc.bNbrPorts; 508 for(port = 0; port < nports; port++) { 509 dev = hub->ports[port].device; 510 if (dev != NULL && dev->subdevs != NULL) { 511 for (i = 0; dev->subdevs[i] != NULL; i++) 512 config_deactivate(dev->subdevs[i]); 513 } 514 } 515 break; 516 } 517 return (0); 518 } 519 #endif 520 521 /* 522 * Called from process context when the hub is gone. 523 * Detach all devices on active ports. 524 */ 525 USB_DETACH(uhub) 526 { 527 USB_DETACH_START(uhub, sc); 528 struct usbd_hub *hub = sc->sc_hub->hub; 529 struct usbd_port *rup; 530 int port, nports; 531 532 #if defined(__NetBSD__) || defined(__OpenBSD__) 533 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 534 #elif defined(__FreeBSD__) 535 DPRINTF(("uhub_detach: sc=%port\n", sc)); 536 #endif 537 538 if (hub == NULL) /* Must be partially working */ 539 return (0); 540 541 usbd_abort_pipe(sc->sc_ipipe); 542 usbd_close_pipe(sc->sc_ipipe); 543 544 nports = hub->hubdesc.bNbrPorts; 545 for(port = 0; port < nports; port++) { 546 rup = &hub->ports[port]; 547 if (rup->device) 548 usb_disconnect_port(rup, self); 549 } 550 551 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 552 USBDEV(sc->sc_dev)); 553 554 free(hub, M_USBDEV); 555 sc->sc_hub->hub = NULL; 556 557 return (0); 558 } 559 560 #if defined(__FreeBSD__) 561 /* Called when a device has been detached from it */ 562 Static void 563 uhub_child_detached(device_t self, device_t child) 564 { 565 struct uhub_softc *sc = device_get_softc(self); 566 usbd_device_handle devhub = sc->sc_hub; 567 usbd_device_handle dev; 568 int nports; 569 int port; 570 int i; 571 572 if (!devhub->hub) 573 /* should never happen; children are only created after init */ 574 panic("hub not fully initialised, but child deleted?"); 575 576 nports = devhub->hub->hubdesc.bNbrPorts; 577 for (port = 0; port < nports; port++) { 578 dev = devhub->hub->ports[port].device; 579 if (dev && dev->subdevs) { 580 for (i = 0; dev->subdevs[i]; i++) { 581 if (dev->subdevs[i] == child) { 582 dev->subdevs[i] = NULL; 583 return; 584 } 585 } 586 } 587 } 588 } 589 #endif 590 591 592 /* 593 * Hub interrupt. 594 * This an indication that some port has changed status. 595 * Notify the bus event handler thread that we need 596 * to be explored again. 597 */ 598 void 599 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 600 { 601 struct uhub_softc *sc = addr; 602 603 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 604 if (status == USBD_STALLED) 605 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 606 else if (status == USBD_NORMAL_COMPLETION) 607 usb_needs_explore(sc->sc_hub); 608 } 609 610 #if defined(__FreeBSD__) 611 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 612 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 613 #endif 614