1 /* $OpenBSD: uhub.c,v 1.59 2011/09/29 11:18:01 stsp 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 /* 256 * To have the best chance of success we do things in the exact same 257 * order as Windoze98. This should not be necessary, but some 258 * devices do not follow the USB specs to the letter. 259 * 260 * These are the events on the bus when a hub is attached: 261 * Get device and config descriptors (see attach code) 262 * Get hub descriptor (see above) 263 * For all ports 264 * turn on power 265 * wait for power to become stable 266 * (all below happens in explore code) 267 * For all ports 268 * clear C_PORT_CONNECTION 269 * For all ports 270 * get port status 271 * if device connected 272 * wait 100 ms 273 * turn on reset 274 * wait 275 * clear C_PORT_RESET 276 * get port status 277 * proceed with device attachment 278 */ 279 280 if (UHUB_IS_HIGH_SPEED(sc)) { 281 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 282 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT); 283 if (!tts) 284 goto bad; 285 } 286 /* Set up data structures */ 287 for (p = 0; p < nports; p++) { 288 struct usbd_port *up = &hub->ports[p]; 289 up->device = NULL; 290 up->parent = dev; 291 up->portno = p+1; 292 if (dev->self_powered) 293 /* Self powered hub, give ports maximum current. */ 294 up->power = USB_MAX_POWER; 295 else 296 up->power = USB_MIN_POWER; 297 up->restartcnt = 0; 298 up->reattach = 0; 299 if (UHUB_IS_HIGH_SPEED(sc)) { 300 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 301 up->tt->hub = hub; 302 } else { 303 up->tt = NULL; 304 } 305 } 306 307 /* XXX should check for none, individual, or ganged power? */ 308 309 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 310 + USB_EXTRA_POWER_UP_TIME; 311 for (port = 1; port <= nports; port++) { 312 /* Turn the power on. */ 313 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 314 if (err) 315 printf("%s: port %d power on failed, %s\n", 316 sc->sc_dev.dv_xname, port, 317 usbd_errstr(err)); 318 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 319 } 320 321 /* Wait for stable power. Root hubs delay in their event thread. */ 322 if (dev->powersrc->parent != NULL) 323 usbd_delay_ms(dev, pwrdly); 324 325 /* The usual exploration will finish the setup. */ 326 327 sc->sc_running = 1; 328 329 return; 330 331 bad: 332 if (sc->sc_statusbuf) 333 free(sc->sc_statusbuf, M_USBDEV); 334 if (hub) { 335 if (hub->ports) 336 free(hub->ports, M_USBDEV); 337 free(hub, M_USBDEV); 338 } 339 dev->hub = NULL; 340 } 341 342 usbd_status 343 uhub_explore(usbd_device_handle dev) 344 { 345 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 346 struct uhub_softc *sc = dev->hub->hubsoftc; 347 struct usbd_port *up; 348 usbd_status err; 349 int speed; 350 int port; 351 int change, status, reconnect; 352 353 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 354 355 if (usbd_is_dying(dev)) { 356 DPRINTF(("%s: dying\n", __func__)); 357 return (USBD_IOERROR); 358 } 359 360 if (!sc->sc_running) 361 return (USBD_NOT_STARTED); 362 363 /* Ignore hubs that are too deep. */ 364 if (dev->depth > USB_HUB_MAX_DEPTH) 365 return (USBD_TOO_DEEP); 366 367 for (port = 1; port <= hd->bNbrPorts; port++) { 368 up = &dev->hub->ports[port-1]; 369 err = usbd_get_port_status(dev, port, &up->status); 370 if (err) { 371 DPRINTF(("uhub_explore: get port status failed, " 372 "error=%s\n", usbd_errstr(err))); 373 continue; 374 } 375 status = UGETW(up->status.wPortStatus); 376 change = UGETW(up->status.wPortChange); 377 reconnect = up->reattach; 378 up->reattach = 0; 379 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", 380 sc->sc_dev.dv_xname, port, status, change)); 381 if (change & UPS_C_PORT_ENABLED) { 382 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 383 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 384 if (change & UPS_C_CONNECT_STATUS) { 385 /* Ignore the port error if the device 386 vanished. */ 387 } else if (status & UPS_PORT_ENABLED) { 388 printf("%s: illegal enable change, port %d\n", 389 sc->sc_dev.dv_xname, port); 390 } else { 391 /* Port error condition. */ 392 if (up->restartcnt) /* no message first time */ 393 printf("%s: port error, restarting " 394 "port %d\n", 395 sc->sc_dev.dv_xname, port); 396 397 if (up->restartcnt++ < USBD_RESTART_MAX) 398 goto disco; 399 else 400 printf("%s: port error, giving up " 401 "port %d\n", 402 sc->sc_dev.dv_xname, port); 403 } 404 } 405 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) { 406 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 407 "STATUS\n", port)); 408 /* No status change, just do recursive explore. */ 409 if (up->device != NULL && up->device->hub != NULL) 410 up->device->hub->explore(up->device); 411 #if 0 && defined(DIAGNOSTIC) 412 if (up->device == NULL && 413 (status & UPS_CURRENT_CONNECT_STATUS)) 414 printf("%s: connected, no device\n", 415 sc->sc_dev.dv_xname); 416 #endif 417 continue; 418 } 419 420 /* We have a connect status change, handle it. */ 421 422 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 423 dev->address, port)); 424 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 425 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ 426 /* 427 * If there is already a device on the port the change status 428 * must mean that is has disconnected. Looking at the 429 * current connect status is not enough to figure this out 430 * since a new unit may have been connected before we handle 431 * the disconnect. 432 */ 433 disco: 434 if (up->device != NULL) { 435 /* Disconnected */ 436 DPRINTF(("uhub_explore: device addr=%d disappeared " 437 "on port %d\n", up->device->address, port)); 438 usb_disconnect_port(up, &sc->sc_dev); 439 usbd_clear_port_feature(dev, port, 440 UHF_C_PORT_CONNECTION); 441 } 442 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 443 /* Nothing connected, just ignore it. */ 444 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 445 "_STATUS\n", port)); 446 continue; 447 } 448 449 /* Connected */ 450 451 if (!(status & UPS_PORT_POWER)) 452 printf("%s: strange, connected port %d has no power\n", 453 sc->sc_dev.dv_xname, port); 454 455 /* Wait for maximum device power up time. */ 456 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 457 458 /* Reset port, which implies enabling it. */ 459 if (usbd_reset_port(dev, port, &up->status)) { 460 printf("%s: port %d reset failed\n", 461 sc->sc_dev.dv_xname, port); 462 continue; 463 } 464 /* Get port status again, it might have changed during reset */ 465 err = usbd_get_port_status(dev, port, &up->status); 466 if (err) { 467 DPRINTF(("uhub_explore: get port status failed, " 468 "error=%s\n", usbd_errstr(err))); 469 continue; 470 } 471 status = UGETW(up->status.wPortStatus); 472 change = UGETW(up->status.wPortChange); 473 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 474 /* Nothing connected, just ignore it. */ 475 #ifdef UHUB_DEBUG 476 printf("%s: port %d, device disappeared after reset\n", 477 sc->sc_dev.dv_xname, port); 478 #endif 479 continue; 480 } 481 482 /* Figure out device speed */ 483 if (status & UPS_HIGH_SPEED) 484 speed = USB_SPEED_HIGH; 485 else if (status & UPS_LOW_SPEED) 486 speed = USB_SPEED_LOW; 487 else 488 speed = USB_SPEED_FULL; 489 /* Get device info and set its address. */ 490 err = usbd_new_device(&sc->sc_dev, dev->bus, 491 dev->depth + 1, speed, port, up); 492 /* XXX retry a few times? */ 493 if (err) { 494 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, " 495 "error=%s\n", usbd_errstr(err))); 496 /* Avoid addressing problems by disabling. */ 497 /* usbd_reset_port(dev, port, &up->status); */ 498 499 /* 500 * The unit refused to accept a new address, or had 501 * some other serious problem. Since we cannot leave 502 * at 0 we have to disable the port instead. 503 */ 504 printf("%s: device problem, disabling port %d\n", 505 sc->sc_dev.dv_xname, port); 506 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 507 } else { 508 /* The port set up succeeded, reset error count. */ 509 up->restartcnt = 0; 510 511 if (up->device->hub) 512 up->device->hub->explore(up->device); 513 } 514 } 515 return (USBD_NORMAL_COMPLETION); 516 } 517 518 int 519 uhub_activate(struct device *self, int act) 520 { 521 struct uhub_softc *sc = (struct uhub_softc *)self; 522 struct usbd_hub *hub = sc->sc_hub->hub; 523 usbd_device_handle dev; 524 int nports, port, i; 525 526 switch (act) { 527 case DVACT_DEACTIVATE: 528 if (hub == NULL) /* malfunctioning hub */ 529 break; 530 nports = hub->hubdesc.bNbrPorts; 531 for(port = 0; port < nports; port++) { 532 dev = hub->ports[port].device; 533 if (dev != NULL && dev->subdevs != NULL) { 534 for (i = 0; dev->subdevs[i] != NULL; i++) 535 config_deactivate(dev->subdevs[i]); 536 } 537 } 538 break; 539 } 540 return (0); 541 } 542 543 /* 544 * Called from process context when the hub is gone. 545 * Detach all devices on active ports. 546 */ 547 int 548 uhub_detach(struct device *self, int flags) 549 { 550 struct uhub_softc *sc = (struct uhub_softc *)self; 551 struct usbd_hub *hub = sc->sc_hub->hub; 552 struct usbd_port *rup; 553 int port, nports; 554 555 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 556 557 if (hub == NULL) /* Must be partially working */ 558 return (0); 559 560 usbd_abort_pipe(sc->sc_ipipe); 561 usbd_close_pipe(sc->sc_ipipe); 562 563 nports = hub->hubdesc.bNbrPorts; 564 for(port = 0; port < nports; port++) { 565 rup = &hub->ports[port]; 566 if (rup->device) 567 usb_disconnect_port(rup, self); 568 } 569 570 if (hub->ports[0].tt) 571 free(hub->ports[0].tt, M_USBDEV); 572 if (sc->sc_statusbuf) 573 free(sc->sc_statusbuf, M_USBDEV); 574 if (hub->ports) 575 free(hub->ports, M_USBDEV); 576 free(hub, M_USBDEV); 577 sc->sc_hub->hub = NULL; 578 579 return (0); 580 } 581 582 /* 583 * Hub interrupt. 584 * This an indication that some port has changed status. 585 * Notify the bus event handler thread that we need 586 * to be explored again. 587 */ 588 void 589 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 590 { 591 struct uhub_softc *sc = addr; 592 593 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 594 if (status == USBD_STALLED) 595 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 596 else if (status == USBD_NORMAL_COMPLETION) 597 usb_needs_explore(sc->sc_hub, 0); 598 else 599 DPRINTFN(8, ("uhub_intr: unknown status, %d\n", status)); 600 } 601