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