1 /* $OpenBSD: uhub.c,v 1.54 2010/09/23 04:58:02 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 (!sc->sc_running) 357 return (USBD_NOT_STARTED); 358 359 /* Ignore hubs that are too deep. */ 360 if (dev->depth > USB_HUB_MAX_DEPTH) 361 return (USBD_TOO_DEEP); 362 363 for (port = 1; port <= hd->bNbrPorts; port++) { 364 up = &dev->hub->ports[port-1]; 365 err = usbd_get_port_status(dev, port, &up->status); 366 if (err) { 367 DPRINTF(("uhub_explore: get port status failed, " 368 "error=%s\n", usbd_errstr(err))); 369 continue; 370 } 371 status = UGETW(up->status.wPortStatus); 372 change = UGETW(up->status.wPortChange); 373 reconnect = up->reattach; 374 up->reattach = 0; 375 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", 376 sc->sc_dev.dv_xname, port, status, change)); 377 if (change & UPS_C_PORT_ENABLED) { 378 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 379 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 380 if (change & UPS_C_CONNECT_STATUS) { 381 /* Ignore the port error if the device 382 vanished. */ 383 } else if (status & UPS_PORT_ENABLED) { 384 printf("%s: illegal enable change, port %d\n", 385 sc->sc_dev.dv_xname, port); 386 } else { 387 /* Port error condition. */ 388 if (up->restartcnt) /* no message first time */ 389 printf("%s: port error, restarting " 390 "port %d\n", 391 sc->sc_dev.dv_xname, port); 392 393 if (up->restartcnt++ < USBD_RESTART_MAX) 394 goto disco; 395 else 396 printf("%s: port error, giving up " 397 "port %d\n", 398 sc->sc_dev.dv_xname, port); 399 } 400 } 401 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) { 402 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 403 "STATUS\n", port)); 404 /* No status change, just do recursive explore. */ 405 if (up->device != NULL && up->device->hub != NULL) 406 up->device->hub->explore(up->device); 407 #if 0 && defined(DIAGNOSTIC) 408 if (up->device == NULL && 409 (status & UPS_CURRENT_CONNECT_STATUS)) 410 printf("%s: connected, no device\n", 411 sc->sc_dev.dv_xname); 412 #endif 413 continue; 414 } 415 416 /* We have a connect status change, handle it. */ 417 418 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 419 dev->address, port)); 420 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 421 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ 422 /* 423 * If there is already a device on the port the change status 424 * must mean that is has disconnected. Looking at the 425 * current connect status is not enough to figure this out 426 * since a new unit may have been connected before we handle 427 * the disconnect. 428 */ 429 disco: 430 if (up->device != NULL) { 431 /* Disconnected */ 432 DPRINTF(("uhub_explore: device addr=%d disappeared " 433 "on port %d\n", up->device->address, port)); 434 usb_disconnect_port(up, &sc->sc_dev); 435 usbd_clear_port_feature(dev, port, 436 UHF_C_PORT_CONNECTION); 437 } 438 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 439 /* Nothing connected, just ignore it. */ 440 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 441 "_STATUS\n", port)); 442 continue; 443 } 444 445 /* Connected */ 446 447 if (!(status & UPS_PORT_POWER)) 448 printf("%s: strange, connected port %d has no power\n", 449 sc->sc_dev.dv_xname, port); 450 451 /* Wait for maximum device power up time. */ 452 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 453 454 /* Reset port, which implies enabling it. */ 455 if (usbd_reset_port(dev, port, &up->status)) { 456 printf("%s: port %d reset failed\n", 457 sc->sc_dev.dv_xname, port); 458 continue; 459 } 460 /* Get port status again, it might have changed during reset */ 461 err = usbd_get_port_status(dev, port, &up->status); 462 if (err) { 463 DPRINTF(("uhub_explore: get port status failed, " 464 "error=%s\n", usbd_errstr(err))); 465 continue; 466 } 467 status = UGETW(up->status.wPortStatus); 468 change = UGETW(up->status.wPortChange); 469 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 470 /* Nothing connected, just ignore it. */ 471 #ifdef UHUB_DEBUG 472 printf("%s: port %d, device disappeared after reset\n", 473 sc->sc_dev.dv_xname, port); 474 #endif 475 continue; 476 } 477 478 /* Figure out device speed */ 479 if (status & UPS_HIGH_SPEED) 480 speed = USB_SPEED_HIGH; 481 else if (status & UPS_LOW_SPEED) 482 speed = USB_SPEED_LOW; 483 else 484 speed = USB_SPEED_FULL; 485 /* Get device info and set its address. */ 486 err = usbd_new_device(&sc->sc_dev, dev->bus, 487 dev->depth + 1, speed, port, up); 488 /* XXX retry a few times? */ 489 if (err) { 490 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, " 491 "error=%s\n", usbd_errstr(err))); 492 /* Avoid addressing problems by disabling. */ 493 /* usbd_reset_port(dev, port, &up->status); */ 494 495 /* 496 * The unit refused to accept a new address, or had 497 * some other serious problem. Since we cannot leave 498 * at 0 we have to disable the port instead. 499 */ 500 printf("%s: device problem, disabling port %d\n", 501 sc->sc_dev.dv_xname, port); 502 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 503 } else { 504 /* The port set up succeeded, reset error count. */ 505 up->restartcnt = 0; 506 507 if (up->device->hub) 508 up->device->hub->explore(up->device); 509 } 510 } 511 return (USBD_NORMAL_COMPLETION); 512 } 513 514 int 515 uhub_activate(struct device *self, int act) 516 { 517 struct uhub_softc *sc = (struct uhub_softc *)self; 518 struct usbd_hub *hub = sc->sc_hub->hub; 519 usbd_device_handle dev; 520 int nports, port, i; 521 522 switch (act) { 523 case DVACT_ACTIVATE: 524 break; 525 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 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 570 &sc->sc_dev); 571 572 if (hub->ports[0].tt) 573 free(hub->ports[0].tt, M_USBDEV); 574 if (sc->sc_statusbuf) 575 free(sc->sc_statusbuf, M_USBDEV); 576 if (hub->ports) 577 free(hub->ports, M_USBDEV); 578 free(hub, M_USBDEV); 579 sc->sc_hub->hub = NULL; 580 581 return (0); 582 } 583 584 /* 585 * Hub interrupt. 586 * This an indication that some port has changed status. 587 * Notify the bus event handler thread that we need 588 * to be explored again. 589 */ 590 void 591 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 592 { 593 struct uhub_softc *sc = addr; 594 595 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 596 if (status == USBD_STALLED) 597 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 598 else if (status == USBD_NORMAL_COMPLETION) 599 usb_needs_explore(sc->sc_hub, 0); 600 else 601 DPRINTFN(8, ("uhub_intr: unknown status, %d\n", status)); 602 } 603