1 /* $OpenBSD: uhub.c,v 1.56 2010/12/06 04:41:40 jakemsr 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 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/device.h> 44 #include <sys/proc.h> 45 46 #include <machine/bus.h> 47 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usbdi.h> 50 #include <dev/usb/usbdi_util.h> 51 #include <dev/usb/usbdivar.h> 52 53 #define UHUB_INTR_INTERVAL 255 /* ms */ 54 55 #ifdef UHUB_DEBUG 56 #define DPRINTF(x) do { if (uhubdebug) printf x; } while (0) 57 #define DPRINTFN(n,x) do { if (uhubdebug>(n)) printf x; } while (0) 58 int uhubdebug = 0; 59 #else 60 #define DPRINTF(x) 61 #define DPRINTFN(n,x) 62 #endif 63 64 struct uhub_softc { 65 struct device sc_dev; /* base device */ 66 usbd_device_handle sc_hub; /* USB device */ 67 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 68 u_int8_t *sc_statusbuf; /* per port status buffer */ 69 size_t sc_statuslen; /* status bufferlen */ 70 u_char sc_running; 71 }; 72 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol) 73 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 74 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 75 76 usbd_status uhub_explore(usbd_device_handle hub); 77 void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status); 78 79 /* 80 * We need two attachment points: 81 * hub to usb and hub to hub 82 * Every other driver only connects to hubs 83 */ 84 85 int uhub_match(struct device *, void *, void *); 86 void uhub_attach(struct device *, struct device *, void *); 87 int uhub_detach(struct device *, int); 88 int uhub_activate(struct device *, int); 89 90 struct cfdriver uhub_cd = { 91 NULL, "uhub", DV_DULL 92 }; 93 94 const struct cfattach uhub_ca = { 95 sizeof(struct uhub_softc), 96 uhub_match, 97 uhub_attach, 98 uhub_detach, 99 uhub_activate, 100 }; 101 102 struct cfattach uhub_uhub_ca = { 103 sizeof(struct uhub_softc), uhub_match, uhub_attach, 104 uhub_detach, uhub_activate 105 }; 106 107 int 108 uhub_match(struct device *parent, void *match, void *aux) 109 { 110 struct usb_attach_arg *uaa = aux; 111 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 112 113 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 114 /* 115 * The subclass for hubs seems to be 0 for some and 1 for others, 116 * so we just ignore the subclass. 117 */ 118 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 119 return (UMATCH_DEVCLASS_DEVSUBCLASS); 120 return (UMATCH_NONE); 121 } 122 123 void 124 uhub_attach(struct device *parent, struct device *self, void *aux) 125 { 126 struct uhub_softc *sc = (struct uhub_softc *)self; 127 struct usb_attach_arg *uaa = aux; 128 usbd_device_handle dev = uaa->device; 129 usbd_status err; 130 struct usbd_hub *hub = NULL; 131 usb_device_request_t req; 132 usb_hub_descriptor_t hubdesc; 133 int p, port, nports, nremov, pwrdly; 134 usbd_interface_handle iface; 135 usb_endpoint_descriptor_t *ed; 136 struct usbd_tt *tts = NULL; 137 138 DPRINTFN(1,("uhub_attach\n")); 139 sc->sc_hub = dev; 140 141 err = usbd_set_config_index(dev, 0, 1); 142 if (err) { 143 DPRINTF(("%s: configuration failed, error=%s\n", 144 sc->sc_dev.dv_xname, usbd_errstr(err))); 145 return; 146 } 147 148 if (dev->depth > USB_HUB_MAX_DEPTH) { 149 printf("%s: hub depth (%d) exceeded, hub ignored\n", 150 sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH); 151 return; 152 } 153 154 /* Get hub descriptor. */ 155 req.bmRequestType = UT_READ_CLASS_DEVICE; 156 req.bRequest = UR_GET_DESCRIPTOR; 157 USETW2(req.wValue, UDESC_HUB, 0); 158 USETW(req.wIndex, 0); 159 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 160 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 161 err = usbd_do_request(dev, &req, &hubdesc); 162 nports = hubdesc.bNbrPorts; 163 if (!err && nports > 7) { 164 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 165 err = usbd_do_request(dev, &req, &hubdesc); 166 } 167 if (err) { 168 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 169 sc->sc_dev.dv_xname, usbd_errstr(err))); 170 return; 171 } 172 173 for (nremov = 0, port = 1; port <= nports; port++) 174 if (!UHD_NOT_REMOV(&hubdesc, port)) 175 nremov++; 176 177 #ifdef UHUB_DEBUG 178 printf("%s: %d port%s with %d removable, %s powered", 179 sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "", 180 nremov, dev->self_powered ? "self" : "bus"); 181 182 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) { 183 printf(", %s transaction translator%s", 184 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple", 185 UHUB_IS_SINGLE_TT(sc) ? "" : "s"); 186 } 187 printf("\n"); 188 #endif 189 190 if (nports == 0) { 191 printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname); 192 goto bad; 193 } 194 195 hub = malloc(sizeof(*hub), M_USBDEV, M_NOWAIT); 196 if (hub == NULL) 197 return; 198 hub->ports = malloc(sizeof(struct usbd_port) * nports, 199 M_USBDEV, M_NOWAIT); 200 if (hub->ports == NULL) { 201 free(hub, M_USBDEV); 202 return; 203 } 204 dev->hub = hub; 205 dev->hub->hubsoftc = sc; 206 hub->explore = uhub_explore; 207 hub->hubdesc = hubdesc; 208 209 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 210 "parent->selfpowered=%d\n", 211 dev->self_powered, dev->powersrc->parent, 212 dev->powersrc->parent ? 213 dev->powersrc->parent->self_powered : 0)); 214 215 if (!dev->self_powered && dev->powersrc->parent != NULL && 216 !dev->powersrc->parent->self_powered) { 217 printf("%s: bus powered hub connected to bus powered hub, " 218 "ignored\n", sc->sc_dev.dv_xname); 219 goto bad; 220 } 221 222 /* Set up interrupt pipe. */ 223 err = usbd_device2interface_handle(dev, 0, &iface); 224 if (err) { 225 printf("%s: no interface handle\n", sc->sc_dev.dv_xname); 226 goto bad; 227 } 228 ed = usbd_interface2endpoint_descriptor(iface, 0); 229 if (ed == NULL) { 230 printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname); 231 goto bad; 232 } 233 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 234 printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname); 235 goto bad; 236 } 237 238 sc->sc_statuslen = (nports + 1 + 7) / 8; 239 sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT); 240 if (!sc->sc_statusbuf) 241 goto bad; 242 243 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 244 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf, 245 sc->sc_statuslen, uhub_intr, UHUB_INTR_INTERVAL); 246 if (err) { 247 printf("%s: cannot open interrupt pipe\n", 248 sc->sc_dev.dv_xname); 249 goto bad; 250 } 251 252 /* Wait with power off for a while. */ 253 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 254 255 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, &sc->sc_dev); 256 257 /* 258 * To have the best chance of success we do things in the exact same 259 * order as Windoze98. This should not be necessary, but some 260 * devices do not follow the USB specs to the letter. 261 * 262 * These are the events on the bus when a hub is attached: 263 * Get device and config descriptors (see attach code) 264 * Get hub descriptor (see above) 265 * For all ports 266 * turn on power 267 * wait for power to become stable 268 * (all below happens in explore code) 269 * For all ports 270 * clear C_PORT_CONNECTION 271 * For all ports 272 * get port status 273 * if device connected 274 * wait 100 ms 275 * turn on reset 276 * wait 277 * clear C_PORT_RESET 278 * get port status 279 * proceed with device attachment 280 */ 281 282 if (UHUB_IS_HIGH_SPEED(sc)) { 283 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 284 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT); 285 if (!tts) 286 goto bad; 287 } 288 /* Set up data structures */ 289 for (p = 0; p < nports; p++) { 290 struct usbd_port *up = &hub->ports[p]; 291 up->device = NULL; 292 up->parent = dev; 293 up->portno = p+1; 294 if (dev->self_powered) 295 /* Self powered hub, give ports maximum current. */ 296 up->power = USB_MAX_POWER; 297 else 298 up->power = USB_MIN_POWER; 299 up->restartcnt = 0; 300 up->reattach = 0; 301 if (UHUB_IS_HIGH_SPEED(sc)) { 302 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 303 up->tt->hub = hub; 304 } else { 305 up->tt = NULL; 306 } 307 } 308 309 /* XXX should check for none, individual, or ganged power? */ 310 311 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 312 + USB_EXTRA_POWER_UP_TIME; 313 for (port = 1; port <= nports; port++) { 314 /* Turn the power on. */ 315 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 316 if (err) 317 printf("%s: port %d power on failed, %s\n", 318 sc->sc_dev.dv_xname, port, 319 usbd_errstr(err)); 320 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 321 } 322 323 /* Wait for stable power. Root hubs delay in their event thread. */ 324 if (dev->powersrc->parent != NULL) 325 usbd_delay_ms(dev, pwrdly); 326 327 /* The usual exploration will finish the setup. */ 328 329 sc->sc_running = 1; 330 331 return; 332 333 bad: 334 if (sc->sc_statusbuf) 335 free(sc->sc_statusbuf, M_USBDEV); 336 if (hub->ports) 337 free(hub->ports, M_USBDEV); 338 if (hub) 339 free(hub, M_USBDEV); 340 dev->hub = NULL; 341 } 342 343 usbd_status 344 uhub_explore(usbd_device_handle dev) 345 { 346 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 347 struct uhub_softc *sc = dev->hub->hubsoftc; 348 struct usbd_port *up; 349 usbd_status err; 350 int speed; 351 int port; 352 int change, status, reconnect; 353 354 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 355 356 if (usbd_is_dying(dev)) { 357 DPRINTF(("%s: dying\n", __func__)); 358 return (USBD_IOERROR); 359 } 360 361 if (!sc->sc_running) 362 return (USBD_NOT_STARTED); 363 364 /* Ignore hubs that are too deep. */ 365 if (dev->depth > USB_HUB_MAX_DEPTH) 366 return (USBD_TOO_DEEP); 367 368 for (port = 1; port <= hd->bNbrPorts; port++) { 369 up = &dev->hub->ports[port-1]; 370 err = usbd_get_port_status(dev, port, &up->status); 371 if (err) { 372 DPRINTF(("uhub_explore: get port status failed, " 373 "error=%s\n", usbd_errstr(err))); 374 continue; 375 } 376 status = UGETW(up->status.wPortStatus); 377 change = UGETW(up->status.wPortChange); 378 reconnect = up->reattach; 379 up->reattach = 0; 380 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", 381 sc->sc_dev.dv_xname, port, status, change)); 382 if (change & UPS_C_PORT_ENABLED) { 383 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 384 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 385 if (change & UPS_C_CONNECT_STATUS) { 386 /* Ignore the port error if the device 387 vanished. */ 388 } else if (status & UPS_PORT_ENABLED) { 389 printf("%s: illegal enable change, port %d\n", 390 sc->sc_dev.dv_xname, port); 391 } else { 392 /* Port error condition. */ 393 if (up->restartcnt) /* no message first time */ 394 printf("%s: port error, restarting " 395 "port %d\n", 396 sc->sc_dev.dv_xname, port); 397 398 if (up->restartcnt++ < USBD_RESTART_MAX) 399 goto disco; 400 else 401 printf("%s: port error, giving up " 402 "port %d\n", 403 sc->sc_dev.dv_xname, port); 404 } 405 } 406 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) { 407 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 408 "STATUS\n", port)); 409 /* No status change, just do recursive explore. */ 410 if (up->device != NULL && up->device->hub != NULL) 411 up->device->hub->explore(up->device); 412 #if 0 && defined(DIAGNOSTIC) 413 if (up->device == NULL && 414 (status & UPS_CURRENT_CONNECT_STATUS)) 415 printf("%s: connected, no device\n", 416 sc->sc_dev.dv_xname); 417 #endif 418 continue; 419 } 420 421 /* We have a connect status change, handle it. */ 422 423 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 424 dev->address, port)); 425 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 426 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ 427 /* 428 * If there is already a device on the port the change status 429 * must mean that is has disconnected. Looking at the 430 * current connect status is not enough to figure this out 431 * since a new unit may have been connected before we handle 432 * the disconnect. 433 */ 434 disco: 435 if (up->device != NULL) { 436 /* Disconnected */ 437 DPRINTF(("uhub_explore: device addr=%d disappeared " 438 "on port %d\n", up->device->address, port)); 439 usb_disconnect_port(up, &sc->sc_dev); 440 usbd_clear_port_feature(dev, port, 441 UHF_C_PORT_CONNECTION); 442 } 443 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 444 /* Nothing connected, just ignore it. */ 445 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 446 "_STATUS\n", port)); 447 continue; 448 } 449 450 /* Connected */ 451 452 if (!(status & UPS_PORT_POWER)) 453 printf("%s: strange, connected port %d has no power\n", 454 sc->sc_dev.dv_xname, port); 455 456 /* Wait for maximum device power up time. */ 457 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 458 459 /* Reset port, which implies enabling it. */ 460 if (usbd_reset_port(dev, port, &up->status)) { 461 printf("%s: port %d reset failed\n", 462 sc->sc_dev.dv_xname, port); 463 continue; 464 } 465 /* Get port status again, it might have changed during reset */ 466 err = usbd_get_port_status(dev, port, &up->status); 467 if (err) { 468 DPRINTF(("uhub_explore: get port status failed, " 469 "error=%s\n", usbd_errstr(err))); 470 continue; 471 } 472 status = UGETW(up->status.wPortStatus); 473 change = UGETW(up->status.wPortChange); 474 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 475 /* Nothing connected, just ignore it. */ 476 #ifdef UHUB_DEBUG 477 printf("%s: port %d, device disappeared after reset\n", 478 sc->sc_dev.dv_xname, port); 479 #endif 480 continue; 481 } 482 483 /* Figure out device speed */ 484 if (status & UPS_HIGH_SPEED) 485 speed = USB_SPEED_HIGH; 486 else if (status & UPS_LOW_SPEED) 487 speed = USB_SPEED_LOW; 488 else 489 speed = USB_SPEED_FULL; 490 /* Get device info and set its address. */ 491 err = usbd_new_device(&sc->sc_dev, dev->bus, 492 dev->depth + 1, speed, port, up); 493 /* XXX retry a few times? */ 494 if (err) { 495 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, " 496 "error=%s\n", usbd_errstr(err))); 497 /* Avoid addressing problems by disabling. */ 498 /* usbd_reset_port(dev, port, &up->status); */ 499 500 /* 501 * The unit refused to accept a new address, or had 502 * some other serious problem. Since we cannot leave 503 * at 0 we have to disable the port instead. 504 */ 505 printf("%s: device problem, disabling port %d\n", 506 sc->sc_dev.dv_xname, port); 507 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 508 } else { 509 /* The port set up succeeded, reset error count. */ 510 up->restartcnt = 0; 511 512 if (up->device->hub) 513 up->device->hub->explore(up->device); 514 } 515 } 516 return (USBD_NORMAL_COMPLETION); 517 } 518 519 int 520 uhub_activate(struct device *self, int act) 521 { 522 struct uhub_softc *sc = (struct uhub_softc *)self; 523 struct usbd_hub *hub = sc->sc_hub->hub; 524 usbd_device_handle dev; 525 int nports, port, i; 526 527 switch (act) { 528 case DVACT_ACTIVATE: 529 break; 530 531 case DVACT_DEACTIVATE: 532 if (hub == NULL) /* malfunctioning hub */ 533 break; 534 nports = hub->hubdesc.bNbrPorts; 535 for(port = 0; port < nports; port++) { 536 dev = hub->ports[port].device; 537 if (dev != NULL && dev->subdevs != NULL) { 538 for (i = 0; dev->subdevs[i] != NULL; i++) 539 config_deactivate(dev->subdevs[i]); 540 } 541 } 542 break; 543 } 544 return (0); 545 } 546 547 /* 548 * Called from process context when the hub is gone. 549 * Detach all devices on active ports. 550 */ 551 int 552 uhub_detach(struct device *self, int flags) 553 { 554 struct uhub_softc *sc = (struct uhub_softc *)self; 555 struct usbd_hub *hub = sc->sc_hub->hub; 556 struct usbd_port *rup; 557 int port, nports; 558 559 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 560 561 if (hub == NULL) /* Must be partially working */ 562 return (0); 563 564 usbd_abort_pipe(sc->sc_ipipe); 565 usbd_close_pipe(sc->sc_ipipe); 566 567 nports = hub->hubdesc.bNbrPorts; 568 for(port = 0; port < nports; port++) { 569 rup = &hub->ports[port]; 570 if (rup->device) 571 usb_disconnect_port(rup, self); 572 } 573 574 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 575 &sc->sc_dev); 576 577 if (hub->ports[0].tt) 578 free(hub->ports[0].tt, M_USBDEV); 579 if (sc->sc_statusbuf) 580 free(sc->sc_statusbuf, M_USBDEV); 581 if (hub->ports) 582 free(hub->ports, M_USBDEV); 583 free(hub, M_USBDEV); 584 sc->sc_hub->hub = NULL; 585 586 return (0); 587 } 588 589 /* 590 * Hub interrupt. 591 * This an indication that some port has changed status. 592 * Notify the bus event handler thread that we need 593 * to be explored again. 594 */ 595 void 596 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 597 { 598 struct uhub_softc *sc = addr; 599 600 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 601 if (status == USBD_STALLED) 602 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 603 else if (status == USBD_NORMAL_COMPLETION) 604 usb_needs_explore(sc->sc_hub, 0); 605 else 606 DPRINTFN(8, ("uhub_intr: unknown status, %d\n", status)); 607 } 608