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