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