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