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