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