1 /* $OpenBSD: uhub.c,v 1.9 2000/03/30 16:19:33 aaron Exp $ */ 2 /* $NetBSD: uhub.c,v 1.41 2000/03/27 12:33:56 augustss 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 (augustss@carlstedt.se) 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/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #if defined(__NetBSD__) || defined(__OpenBSD__) 51 #include <sys/device.h> 52 #include <sys/proc.h> 53 #elif defined(__FreeBSD__) 54 #include <sys/module.h> 55 #include <sys/bus.h> 56 #include "bus_if.h" 57 #endif 58 59 #include <machine/bus.h> 60 61 #include <dev/usb/usb.h> 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usbdi_util.h> 64 #include <dev/usb/usbdivar.h> 65 66 #ifdef UHUB_DEBUG 67 #define DPRINTF(x) if (uhubdebug) logprintf x 68 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x 69 int uhubdebug; 70 #else 71 #define DPRINTF(x) 72 #define DPRINTFN(n,x) 73 #endif 74 75 struct uhub_softc { 76 USBBASEDEVICE sc_dev; /* base device */ 77 usbd_device_handle sc_hub; /* USB device */ 78 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 79 u_int8_t sc_status[1]; /* XXX more ports */ 80 u_char sc_running; 81 }; 82 83 Static usbd_status uhub_init_port __P((struct usbd_port *)); 84 Static usbd_status uhub_explore __P((usbd_device_handle hub)); 85 Static void uhub_intr __P((usbd_xfer_handle, usbd_private_handle,usbd_status)); 86 87 #if defined(__FreeBSD__) 88 Static bus_child_detached_t uhub_child_detached; 89 #endif 90 91 92 /* 93 * We need two attachment points: 94 * hub to usb and hub to hub 95 * Every other driver only connects to hubs 96 */ 97 98 #if defined(__NetBSD__) || defined(__OpenBSD__) 99 USB_DECLARE_DRIVER(uhub); 100 101 /* Create the driver instance for the hub connected to hub case */ 102 struct cfattach uhub_uhub_ca = { 103 sizeof(struct uhub_softc), uhub_match, uhub_attach, 104 uhub_detach, uhub_activate 105 }; 106 #elif defined(__FreeBSD__) 107 USB_DECLARE_DRIVER_INIT(uhub, 108 DEVMETHOD(bus_child_detached, uhub_child_detached)); 109 110 /* Create the driver instance for the hub connected to usb case. */ 111 devclass_t uhubroot_devclass; 112 113 Static device_method_t uhubroot_methods[] = { 114 DEVMETHOD(device_probe, uhub_match), 115 DEVMETHOD(device_attach, uhub_attach), 116 117 /* detach is not allowed for a root hub */ 118 {0,0} 119 }; 120 121 Static driver_t uhubroot_driver = { 122 "uhub", 123 uhubroot_methods, 124 sizeof(struct uhub_softc) 125 }; 126 #endif 127 128 USB_MATCH(uhub) 129 { 130 USB_MATCH_START(uhub, uaa); 131 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 132 133 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 134 /* 135 * The subclass for hubs seems to be 0 for some and 1 for others, 136 * so we just ignore the subclass. 137 */ 138 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 139 return (UMATCH_DEVCLASS_DEVSUBCLASS); 140 return (UMATCH_NONE); 141 } 142 143 USB_ATTACH(uhub) 144 { 145 USB_ATTACH_START(uhub, sc, uaa); 146 usbd_device_handle dev = uaa->device; 147 char devinfo[1024]; 148 usbd_status err; 149 struct usbd_hub *hub; 150 usb_device_request_t req; 151 usb_hub_descriptor_t hubdesc; 152 int p, port, nports, nremov; 153 usbd_interface_handle iface; 154 usb_endpoint_descriptor_t *ed; 155 156 DPRINTFN(1,("uhub_attach\n")); 157 sc->sc_hub = dev; 158 usbd_devinfo(dev, 1, devinfo); 159 USB_ATTACH_SETUP; 160 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 161 162 err = usbd_set_config_index(dev, 0, 1); 163 if (err) { 164 DPRINTF(("%s: configuration failed, error=%s\n", 165 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 166 USB_ATTACH_ERROR_RETURN; 167 } 168 169 if (dev->depth > USB_HUB_MAX_DEPTH) { 170 printf("%s: hub depth (%d) exceeded, hub ignored\n", 171 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 172 USB_ATTACH_ERROR_RETURN; 173 } 174 175 /* Get hub descriptor. */ 176 req.bmRequestType = UT_READ_CLASS_DEVICE; 177 req.bRequest = UR_GET_DESCRIPTOR; 178 USETW(req.wValue, 0); 179 USETW(req.wIndex, 0); 180 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 181 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 182 err = usbd_do_request(dev, &req, &hubdesc); 183 nports = hubdesc.bNbrPorts; 184 if (!err && nports > 7) { 185 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 186 err = usbd_do_request(dev, &req, &hubdesc); 187 } 188 if (err) { 189 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 190 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 191 USB_ATTACH_ERROR_RETURN; 192 } 193 194 for (nremov = 0, port = 1; port <= nports; port++) 195 if (!UHD_NOT_REMOV(&hubdesc, port)) 196 nremov++; 197 printf("%s: %d port%s with %d removable, %s powered\n", 198 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 199 nremov, dev->self_powered ? "self" : "bus"); 200 201 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 202 M_USBDEV, M_NOWAIT); 203 if (hub == NULL) 204 USB_ATTACH_ERROR_RETURN; 205 dev->hub = hub; 206 dev->hub->hubsoftc = sc; 207 hub->explore = uhub_explore; 208 hub->hubdesc = hubdesc; 209 210 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 211 "parent->selfpowered=%d\n", 212 dev->self_powered, dev->powersrc->parent, 213 dev->powersrc->parent ? 214 dev->powersrc->parent->self_powered : 0)); 215 216 if (!dev->self_powered && dev->powersrc->parent != NULL && 217 !dev->powersrc->parent->self_powered) { 218 printf("%s: bus powered hub connected to bus powered hub, " 219 "ignored\n", USBDEVNAME(sc->sc_dev)); 220 goto bad; 221 } 222 223 /* Set up interrupt pipe. */ 224 err = usbd_device2interface_handle(dev, 0, &iface); 225 if (err) { 226 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev)); 227 goto bad; 228 } 229 ed = usbd_interface2endpoint_descriptor(iface, 0); 230 if (ed == NULL) { 231 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 232 goto bad; 233 } 234 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 235 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 236 goto bad; 237 } 238 239 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 240 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 241 sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL); 242 if (err) { 243 printf("%s: cannot open interrupt pipe\n", 244 USBDEVNAME(sc->sc_dev)); 245 goto bad; 246 } 247 248 /* Wait with power off for a while. */ 249 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 250 251 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev)); 252 253 for (p = 0; p < nports; p++) { 254 struct usbd_port *up = &hub->ports[p]; 255 up->device = 0; 256 up->parent = dev; 257 up->portno = p+1; 258 err = uhub_init_port(up); 259 if (err) 260 printf("%s: init of port %d failed\n", 261 USBDEVNAME(sc->sc_dev), up->portno); 262 } 263 sc->sc_running = 1; 264 265 USB_ATTACH_SUCCESS_RETURN; 266 267 bad: 268 free(hub, M_USBDEV); 269 dev->hub = 0; 270 USB_ATTACH_ERROR_RETURN; 271 } 272 273 usbd_status 274 uhub_init_port(up) 275 struct usbd_port *up; 276 { 277 int port = up->portno; 278 usbd_device_handle dev = up->parent; 279 usbd_status err; 280 u_int16_t pstatus; 281 282 err = usbd_get_port_status(dev, port, &up->status); 283 if (err) 284 return (err); 285 pstatus = UGETW(up->status.wPortStatus); 286 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x " 287 "change=0x%04x\n", 288 port, pstatus, UGETW(up->status.wPortChange))); 289 if ((pstatus & UPS_PORT_POWER) == 0) { 290 /* Port lacks power, turn it on */ 291 292 /* First let the device go through a good power cycle, */ 293 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME); 294 295 /* then turn the power on. */ 296 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 297 if (err) 298 return (err); 299 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x " 300 "change=0x%04x\n", 301 port, UGETW(up->status.wPortStatus), 302 UGETW(up->status.wPortChange))); 303 /* Wait for stable power. */ 304 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood * 305 UHD_PWRON_FACTOR); 306 /* Get the port status again. */ 307 err = usbd_get_port_status(dev, port, &up->status); 308 if (err) 309 return (err); 310 DPRINTF(("usb_init_port: after power on status=0x%04x " 311 "change=0x%04x\n", 312 UGETW(up->status.wPortStatus), 313 UGETW(up->status.wPortChange))); 314 315 #if 0 316 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT); 317 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT); 318 usbd_get_port_status(dev, port, &up->status); 319 #endif 320 321 pstatus = UGETW(up->status.wPortStatus); 322 if ((pstatus & UPS_PORT_POWER) == 0) 323 printf("%s: port %d did not power up\n", 324 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port); 325 326 } 327 if (dev->self_powered) 328 /* Self powered hub, give ports maximum current. */ 329 up->power = USB_MAX_POWER; 330 else 331 up->power = USB_MIN_POWER; 332 return (USBD_NORMAL_COMPLETION); 333 } 334 335 usbd_status 336 uhub_explore(dev) 337 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 port; 344 int change, status; 345 346 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 347 348 if (!sc->sc_running) 349 return (USBD_NOT_STARTED); 350 351 /* Ignore hubs that are too deep. */ 352 if (dev->depth > USB_HUB_MAX_DEPTH) 353 return (USBD_TOO_DEEP); 354 355 for(port = 1; port <= hd->bNbrPorts; port++) { 356 up = &dev->hub->ports[port-1]; 357 err = usbd_get_port_status(dev, port, &up->status); 358 if (err) { 359 DPRINTF(("uhub_explore: get port status failed, " 360 "error=%s\n", usbd_errstr(err))); 361 continue; 362 } 363 status = UGETW(up->status.wPortStatus); 364 change = UGETW(up->status.wPortChange); 365 DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n", 366 port, status, change)); 367 if (change & UPS_C_PORT_ENABLED) { 368 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 369 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 370 if (status & UPS_PORT_ENABLED) { 371 printf("%s: illegal enable change, port %d\n", 372 USBDEVNAME(sc->sc_dev), port); 373 } else { 374 /* Port error condition. */ 375 if (up->restartcnt++ < USBD_RESTART_MAX) { 376 printf("%s: port error, restarting " 377 "port %d\n", 378 USBDEVNAME(sc->sc_dev), port); 379 goto disco; 380 } else { 381 printf("%s: port error, giving up " 382 "port %d\n", 383 USBDEVNAME(sc->sc_dev), port); 384 } 385 } 386 } 387 if (!(change & UPS_C_CONNECT_STATUS)) { 388 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 389 "STATUS\n", port)); 390 /* No status change, just do recursive explore. */ 391 if (up->device && up->device->hub) 392 up->device->hub->explore(up->device); 393 continue; 394 } 395 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 396 dev->address, port)); 397 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 398 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 399 /* 400 * If there is already a device on the port the change status 401 * must mean that is has disconnected. Looking at the 402 * current connect status is not enough to figure this out 403 * since a new unit may have been connected before we handle 404 * the disconnect. 405 */ 406 disco: 407 if (up->device != NULL) { 408 /* Disconnected */ 409 DPRINTF(("uhub_explore: device addr=%d disappeared " 410 "on port %d\n", up->device->address, port)); 411 usb_disconnect_port(up, USBDEV(sc->sc_dev)); 412 usbd_clear_port_feature(dev, port, 413 UHF_C_PORT_CONNECTION); 414 } 415 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 416 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 417 "_STATUS\n", port)); 418 continue; 419 } 420 421 /* Connected */ 422 up->restartcnt = 0; 423 424 /* Wait for maximum device power up time. */ 425 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 426 427 /* Reset port, which implies enabling it. */ 428 if (usbd_reset_port(dev, port, &up->status)) { 429 DPRINTF(("uhub_explore: port=%d reset failed\n", 430 port)); 431 continue; 432 } 433 434 /* Get device info and set its address. */ 435 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 436 dev->depth + 1, status & UPS_LOW_SPEED, 437 port, up); 438 /* XXX retry a few times? */ 439 if (err) { 440 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 441 "error=%s\n", usbd_errstr(err))); 442 /* Avoid addressing problems by disabling. */ 443 /* usbd_reset_port(dev, port, &up->status); */ 444 445 /* 446 * The unit refused to accept a new address, or had 447 * some other serious problem. Since we cannot leave 448 * at 0 we have to disable the port instead. 449 */ 450 printf("%s: device problem, disabling port %d\n", 451 USBDEVNAME(sc->sc_dev), port); 452 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 453 /* Make sure we don't try to restart it infinitely. */ 454 up->restartcnt = USBD_RESTART_MAX; 455 } else { 456 if (up->device->hub) 457 up->device->hub->explore(up->device); 458 } 459 } 460 return (USBD_NORMAL_COMPLETION); 461 } 462 463 #if defined(__NetBSD__) || defined(__OpenBSD__) 464 int 465 uhub_activate(self, act) 466 device_ptr_t self; 467 enum devact act; 468 { 469 struct uhub_softc *sc = (struct uhub_softc *)self; 470 struct usbd_hub *hub = sc->sc_hub->hub; 471 usbd_device_handle dev; 472 int nports, port, i; 473 474 switch (act) { 475 case DVACT_ACTIVATE: 476 return (EOPNOTSUPP); 477 break; 478 479 case DVACT_DEACTIVATE: 480 if (hub == NULL) /* malfunctioning hub */ 481 break; 482 nports = hub->hubdesc.bNbrPorts; 483 for(port = 0; port < nports; port++) { 484 dev = hub->ports[port].device; 485 if (dev != NULL) { 486 for (i = 0; dev->subdevs[i]; i++) 487 config_deactivate(dev->subdevs[i]); 488 } 489 } 490 break; 491 } 492 return (0); 493 } 494 #endif 495 496 /* 497 * Called from process context when the hub is gone. 498 * Detach all devices on active ports. 499 */ 500 USB_DETACH(uhub) 501 { 502 USB_DETACH_START(uhub, sc); 503 struct usbd_hub *hub = sc->sc_hub->hub; 504 struct usbd_port *rup; 505 int port, nports; 506 507 #if defined(__NetBSD__) || defined(__OpenBSD__) 508 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 509 #elif defined(__FreeBSD__) 510 DPRINTF(("uhub_detach: sc=%port\n", sc)); 511 #endif 512 513 if (hub == NULL) /* Must be partially working */ 514 return (0); 515 516 usbd_abort_pipe(sc->sc_ipipe); 517 usbd_close_pipe(sc->sc_ipipe); 518 519 nports = hub->hubdesc.bNbrPorts; 520 for(port = 0; port < nports; port++) { 521 rup = &hub->ports[port]; 522 if (rup->device) 523 usb_disconnect_port(rup, self); 524 } 525 526 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 527 USBDEV(sc->sc_dev)); 528 529 free(hub, M_USBDEV); 530 sc->sc_hub->hub = NULL; 531 532 return (0); 533 } 534 535 #if defined(__FreeBSD__) 536 /* Called when a device has been detached from it */ 537 Static void 538 uhub_child_detached(self, child) 539 device_t self; 540 device_t child; 541 { 542 struct uhub_softc *sc = device_get_softc(self); 543 usbd_device_handle devhub = sc->sc_hub; 544 usbd_device_handle dev; 545 int nports; 546 int port; 547 int i; 548 549 if (!devhub->hub) 550 /* should never happen; children are only created after init */ 551 panic("hub not fully initialised, but child deleted?"); 552 553 nports = devhub->hub->hubdesc.bNbrPorts; 554 for (port = 0; port < nports; port++) { 555 dev = devhub->hub->ports[port].device; 556 if (dev && dev->subdevs) { 557 for (i = 0; dev->subdevs[i]; i++) { 558 if (dev->subdevs[i] == child) { 559 dev->subdevs[i] = NULL; 560 return; 561 } 562 } 563 } 564 } 565 } 566 #endif 567 568 569 /* 570 * Hub interrupt. 571 * This an indication that some port has changed status. 572 * Notify the bus event handler thread that we need 573 * to be explored again. 574 */ 575 void 576 uhub_intr(xfer, addr, status) 577 usbd_xfer_handle xfer; 578 usbd_private_handle addr; 579 usbd_status status; 580 { 581 struct uhub_softc *sc = addr; 582 583 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 584 if (status != USBD_NORMAL_COMPLETION) 585 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 586 587 usb_needs_explore(sc->sc_hub->bus); 588 } 589 590 #if defined(__FreeBSD__) 591 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 592 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 593 #endif 594