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