1 /* $NetBSD: uhub.c,v 1.138 2018/02/01 09:50:48 msaitoh Exp $ */ 2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */ 3 /* $OpenBSD: uhub.c,v 1.86 2015/06/29 18:27:40 mpi Exp $ */ 4 5 /* 6 * Copyright (c) 1998, 2004 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 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.138 2018/02/01 09:50:48 msaitoh Exp $"); 41 42 #ifdef _KERNEL_OPT 43 #include "opt_usb.h" 44 #endif 45 46 #include <sys/param.h> 47 48 #include <sys/bus.h> 49 #include <sys/device.h> 50 #include <sys/kernel.h> 51 #include <sys/kmem.h> 52 #include <sys/proc.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 56 57 #include <dev/usb/usb.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 #include <dev/usb/usbdivar.h> 61 #include <dev/usb/usbhist.h> 62 63 #ifdef USB_DEBUG 64 #ifndef UHUB_DEBUG 65 #define uhubdebug 0 66 #else 67 static int uhubdebug = 0; 68 69 SYSCTL_SETUP(sysctl_hw_uhub_setup, "sysctl hw.uhub setup") 70 { 71 int err; 72 const struct sysctlnode *rnode; 73 const struct sysctlnode *cnode; 74 75 err = sysctl_createv(clog, 0, NULL, &rnode, 76 CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhub", 77 SYSCTL_DESCR("uhub global controls"), 78 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 79 80 if (err) 81 goto fail; 82 83 /* control debugging printfs */ 84 err = sysctl_createv(clog, 0, &rnode, &cnode, 85 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 86 "debug", SYSCTL_DESCR("Enable debugging output"), 87 NULL, 0, &uhubdebug, sizeof(uhubdebug), CTL_CREATE, CTL_EOL); 88 if (err) 89 goto fail; 90 91 return; 92 fail: 93 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 94 } 95 96 #endif /* UHUB_DEBUG */ 97 #endif /* USB_DEBUG */ 98 99 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D) 100 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D) 101 #define UHUBHIST_FUNC() USBHIST_FUNC() 102 #define UHUBHIST_CALLED(name) USBHIST_CALLED(uhubdebug) 103 104 struct uhub_softc { 105 device_t sc_dev; /* base device */ 106 struct usbd_device *sc_hub; /* USB device */ 107 int sc_proto; /* device protocol */ 108 struct usbd_pipe *sc_ipipe; /* interrupt pipe */ 109 110 kmutex_t sc_lock; 111 112 uint8_t *sc_statusbuf; 113 uint8_t *sc_statuspend; 114 uint8_t *sc_status; 115 size_t sc_statuslen; 116 int sc_explorepending; 117 118 u_char sc_running; 119 }; 120 121 #define UHUB_IS_HIGH_SPEED(sc) \ 122 ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT) 123 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT) 124 125 #define PORTSTAT_ISSET(sc, port) \ 126 ((sc)->sc_status[(port) / 8] & (1 << ((port) % 8))) 127 128 Static usbd_status uhub_explore(struct usbd_device *); 129 Static void uhub_intr(struct usbd_xfer *, void *, usbd_status); 130 131 132 /* 133 * We need two attachment points: 134 * hub to usb and hub to hub 135 * Every other driver only connects to hubs 136 */ 137 138 int uhub_match(device_t, cfdata_t, void *); 139 void uhub_attach(device_t, device_t, void *); 140 int uhub_rescan(device_t, const char *, const int *); 141 void uhub_childdet(device_t, device_t); 142 int uhub_detach(device_t, int); 143 extern struct cfdriver uhub_cd; 144 CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match, 145 uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet, 146 DVF_DETACH_SHUTDOWN); 147 CFATTACH_DECL3_NEW(uroothub, sizeof(struct uhub_softc), uhub_match, 148 uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet, 149 DVF_DETACH_SHUTDOWN); 150 151 /* 152 * Setting this to 1 makes sure than an uhub attaches even at higher 153 * priority than ugen when ugen_override is set to 1. This allows to 154 * probe the whole USB bus and attach functions with ugen. 155 */ 156 int uhub_ubermatch = 0; 157 158 static usbd_status 159 usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed) 160 { 161 usb_device_request_t req; 162 usbd_status err; 163 int nports; 164 165 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 166 167 /* don't issue UDESC_HUB to SS hub, or it would stall */ 168 if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) { 169 usb_hub_ss_descriptor_t hssd; 170 int rmvlen; 171 172 memset(&hssd, 0, sizeof(hssd)); 173 req.bmRequestType = UT_READ_CLASS_DEVICE; 174 req.bRequest = UR_GET_DESCRIPTOR; 175 USETW2(req.wValue, UDESC_SS_HUB, 0); 176 USETW(req.wIndex, 0); 177 USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE); 178 DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0); 179 err = usbd_do_request(dev, &req, &hssd); 180 nports = hssd.bNbrPorts; 181 if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) { 182 DPRINTF("num of ports %jd exceeds maxports %jd", 183 nports, UHD_SS_NPORTS_MAX, 0, 0); 184 nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX; 185 } 186 rmvlen = (nports + 7) / 8; 187 hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE + 188 (rmvlen > 1 ? rmvlen : 1) - 1; 189 memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen); 190 hd->bDescriptorType = hssd.bDescriptorType; 191 hd->bNbrPorts = hssd.bNbrPorts; 192 hd->wHubCharacteristics[0] = hssd.wHubCharacteristics[0]; 193 hd->wHubCharacteristics[1] = hssd.wHubCharacteristics[1]; 194 hd->bPwrOn2PwrGood = hssd.bPwrOn2PwrGood; 195 hd->bHubContrCurrent = hssd.bHubContrCurrent; 196 } else { 197 req.bmRequestType = UT_READ_CLASS_DEVICE; 198 req.bRequest = UR_GET_DESCRIPTOR; 199 USETW2(req.wValue, UDESC_HUB, 0); 200 USETW(req.wIndex, 0); 201 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 202 DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0); 203 err = usbd_do_request(dev, &req, hd); 204 nports = hd->bNbrPorts; 205 if (!err && nports > 7) { 206 USETW(req.wLength, 207 USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 208 err = usbd_do_request(dev, &req, hd); 209 } 210 } 211 212 return err; 213 } 214 215 static usbd_status 216 usbd_set_hub_depth(struct usbd_device *dev, int depth) 217 { 218 usb_device_request_t req; 219 220 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 221 req.bRequest = UR_SET_HUB_DEPTH; 222 USETW(req.wValue, depth); 223 USETW(req.wIndex, 0); 224 USETW(req.wLength, 0); 225 return usbd_do_request(dev, &req, 0); 226 } 227 228 int 229 uhub_match(device_t parent, cfdata_t match, void *aux) 230 { 231 struct usb_attach_arg *uaa = aux; 232 int matchvalue; 233 234 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 235 236 if (uhub_ubermatch) 237 matchvalue = UMATCH_HIGHEST+1; 238 else 239 matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS; 240 241 DPRINTFN(5, "uaa=%#jx", (uintptr_t)uaa, 0, 0, 0); 242 /* 243 * The subclass for hubs seems to be 0 for some and 1 for others, 244 * so we just ignore the subclass. 245 */ 246 if (uaa->uaa_class == UDCLASS_HUB) 247 return matchvalue; 248 return UMATCH_NONE; 249 } 250 251 void 252 uhub_attach(device_t parent, device_t self, void *aux) 253 { 254 struct uhub_softc *sc = device_private(self); 255 struct usb_attach_arg *uaa = aux; 256 struct usbd_device *dev = uaa->uaa_device; 257 char *devinfop; 258 usbd_status err; 259 struct usbd_hub *hub = NULL; 260 usb_hub_descriptor_t hubdesc; 261 int p, port, nports, nremov, pwrdly; 262 struct usbd_interface *iface; 263 usb_endpoint_descriptor_t *ed; 264 struct usbd_tt *tts = NULL; 265 266 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 267 268 sc->sc_dev = self; 269 sc->sc_hub = dev; 270 sc->sc_proto = uaa->uaa_proto; 271 272 devinfop = usbd_devinfo_alloc(dev, 1); 273 aprint_naive("\n"); 274 aprint_normal(": %s\n", devinfop); 275 usbd_devinfo_free(devinfop); 276 277 if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) { 278 aprint_normal_dev(self, "%s transaction translator%s\n", 279 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple", 280 UHUB_IS_SINGLE_TT(sc) ? "" : "s"); 281 } 282 283 err = usbd_set_config_index(dev, 0, 1); 284 if (err) { 285 DPRINTF("configuration failed, sc %#jx error %jd", 286 (uintptr_t)sc, err, 0, 0); 287 return; 288 } 289 290 if (dev->ud_depth > USB_HUB_MAX_DEPTH) { 291 aprint_error_dev(self, 292 "hub depth (%d) exceeded, hub ignored\n", 293 USB_HUB_MAX_DEPTH); 294 return; 295 } 296 297 /* Get hub descriptor. */ 298 memset(&hubdesc, 0, sizeof(hubdesc)); 299 err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed); 300 nports = hubdesc.bNbrPorts; 301 if (err) { 302 DPRINTF("getting hub descriptor failed, uhub%jd error %jd", 303 device_unit(self), err, 0, 0); 304 return; 305 } 306 307 for (nremov = 0, port = 1; port <= nports; port++) 308 if (!UHD_NOT_REMOV(&hubdesc, port)) 309 nremov++; 310 aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n", 311 nports, nports != 1 ? "s" : "", nremov, 312 dev->ud_selfpowered ? "self" : "bus"); 313 314 if (nports == 0) { 315 aprint_debug_dev(self, "no ports, hub ignored\n"); 316 goto bad; 317 } 318 319 hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 320 KM_SLEEP); 321 dev->ud_hub = hub; 322 dev->ud_hub->uh_hubsoftc = sc; 323 hub->uh_explore = uhub_explore; 324 hub->uh_hubdesc = hubdesc; 325 326 if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) { 327 aprint_debug_dev(self, "setting hub depth %u\n", 328 dev->ud_depth - 1); 329 err = usbd_set_hub_depth(dev, dev->ud_depth - 1); 330 if (err) { 331 aprint_error_dev(self, "can't set depth\n"); 332 goto bad; 333 } 334 } 335 336 /* Set up interrupt pipe. */ 337 err = usbd_device2interface_handle(dev, 0, &iface); 338 if (err) { 339 aprint_error_dev(self, "no interface handle\n"); 340 goto bad; 341 } 342 343 if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) { 344 err = usbd_set_interface(iface, 1); 345 if (err) 346 aprint_error_dev(self, "can't enable multiple TTs\n"); 347 } 348 349 ed = usbd_interface2endpoint_descriptor(iface, 0); 350 if (ed == NULL) { 351 aprint_error_dev(self, "no endpoint descriptor\n"); 352 goto bad; 353 } 354 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 355 aprint_error_dev(self, "bad interrupt endpoint\n"); 356 goto bad; 357 } 358 359 sc->sc_statuslen = (nports + 1 + 7) / 8; 360 sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP); 361 sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP); 362 sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP); 363 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 364 memset(sc->sc_statuspend, 0, sc->sc_statuslen); 365 366 /* force initial scan */ 367 memset(sc->sc_status, 0xff, sc->sc_statuslen); 368 sc->sc_explorepending = 1; 369 370 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 371 USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc, 372 sc->sc_statusbuf, sc->sc_statuslen, 373 uhub_intr, USBD_DEFAULT_INTERVAL); 374 if (err) { 375 aprint_error_dev(self, "cannot open interrupt pipe\n"); 376 goto bad; 377 } 378 379 /* Wait with power off for a while. */ 380 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 381 382 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev); 383 384 /* 385 * To have the best chance of success we do things in the exact same 386 * order as Windows 98. This should not be necessary, but some 387 * devices do not follow the USB specs to the letter. 388 * 389 * These are the events on the bus when a hub is attached: 390 * Get device and config descriptors (see attach code) 391 * Get hub descriptor (see above) 392 * For all ports 393 * turn on power 394 * wait for power to become stable 395 * (all below happens in explore code) 396 * For all ports 397 * clear C_PORT_CONNECTION 398 * For all ports 399 * get port status 400 * if device connected 401 * wait 100 ms 402 * turn on reset 403 * wait 404 * clear C_PORT_RESET 405 * get port status 406 * proceed with device attachment 407 */ 408 409 if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) { 410 tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 411 sizeof(struct usbd_tt), KM_SLEEP); 412 } 413 /* Set up data structures */ 414 for (p = 0; p < nports; p++) { 415 struct usbd_port *up = &hub->uh_ports[p]; 416 up->up_dev = NULL; 417 up->up_parent = dev; 418 up->up_portno = p + 1; 419 if (dev->ud_selfpowered) 420 /* Self powered hub, give ports maximum current. */ 421 up->up_power = USB_MAX_POWER; 422 else 423 up->up_power = USB_MIN_POWER; 424 up->up_restartcnt = 0; 425 up->up_reattach = 0; 426 if (UHUB_IS_HIGH_SPEED(sc)) { 427 up->up_tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 428 up->up_tt->utt_hub = hub; 429 } else { 430 up->up_tt = NULL; 431 } 432 } 433 434 /* XXX should check for none, individual, or ganged power? */ 435 436 pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 437 + USB_EXTRA_POWER_UP_TIME; 438 for (port = 1; port <= nports; port++) { 439 /* Turn the power on. */ 440 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 441 if (err) 442 aprint_error_dev(self, "port %d power on failed, %s\n", 443 port, usbd_errstr(err)); 444 DPRINTF("uhub%jd turn on port %jd power", device_unit(self), 445 port, 0, 0); 446 } 447 448 /* Wait for stable power if we are not a root hub */ 449 if (dev->ud_powersrc->up_parent != NULL) 450 usbd_delay_ms(dev, pwrdly); 451 452 /* The usual exploration will finish the setup. */ 453 454 sc->sc_running = 1; 455 456 if (!pmf_device_register(self, NULL, NULL)) 457 aprint_error_dev(self, "couldn't establish power handler\n"); 458 459 return; 460 461 bad: 462 if (sc->sc_status) 463 kmem_free(sc->sc_status, sc->sc_statuslen); 464 if (sc->sc_statuspend) 465 kmem_free(sc->sc_statuspend, sc->sc_statuslen); 466 if (sc->sc_statusbuf) 467 kmem_free(sc->sc_statusbuf, sc->sc_statuslen); 468 if (hub) 469 kmem_free(hub, 470 sizeof(*hub) + (nports-1) * sizeof(struct usbd_port)); 471 dev->ud_hub = NULL; 472 return; 473 } 474 475 usbd_status 476 uhub_explore(struct usbd_device *dev) 477 { 478 usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc; 479 struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc; 480 struct usbd_port *up; 481 usbd_status err; 482 int speed; 483 int port; 484 int change, status, reconnect; 485 486 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 487 488 DPRINTFN(10, "uhub%jd dev=%#jx addr=%jd speed=%ju", 489 device_unit(sc->sc_dev), (uintptr_t)dev, dev->ud_addr, 490 dev->ud_speed); 491 492 if (!sc->sc_running) 493 return USBD_NOT_STARTED; 494 495 /* Ignore hubs that are too deep. */ 496 if (dev->ud_depth > USB_HUB_MAX_DEPTH) 497 return USBD_TOO_DEEP; 498 499 if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */ 500 usb_hub_status_t hs; 501 502 err = usbd_get_hub_status(dev, &hs); 503 if (err) { 504 DPRINTF("uhub%jd get hub status failed, err %jd", 505 device_unit(sc->sc_dev), err, 0, 0); 506 } else { 507 /* just acknowledge */ 508 status = UGETW(hs.wHubStatus); 509 change = UGETW(hs.wHubChange); 510 DPRINTF("uhub%jd s/c=%jx/%jx", device_unit(sc->sc_dev), 511 status, change, 0); 512 513 if (change & UHS_LOCAL_POWER) 514 usbd_clear_hub_feature(dev, 515 UHF_C_HUB_LOCAL_POWER); 516 if (change & UHS_OVER_CURRENT) 517 usbd_clear_hub_feature(dev, 518 UHF_C_HUB_OVER_CURRENT); 519 } 520 } 521 522 for (port = 1; port <= hd->bNbrPorts; port++) { 523 up = &dev->ud_hub->uh_ports[port - 1]; 524 525 /* reattach is needed after firmware upload */ 526 reconnect = up->up_reattach; 527 up->up_reattach = 0; 528 529 status = change = 0; 530 531 /* don't check if no change summary notification */ 532 if (PORTSTAT_ISSET(sc, port) || reconnect) { 533 err = usbd_get_port_status(dev, port, &up->up_status); 534 if (err) { 535 DPRINTF("uhub%jd get port stat failed, err %jd", 536 device_unit(sc->sc_dev), err, 0, 0); 537 continue; 538 } 539 status = UGETW(up->up_status.wPortStatus); 540 change = UGETW(up->up_status.wPortChange); 541 542 DPRINTF("uhub%jd port %jd: s/c=%jx/%jx", 543 device_unit(sc->sc_dev), port, status, change); 544 } 545 if (!change && !reconnect) { 546 /* No status change, just do recursive explore. */ 547 if (up->up_dev != NULL && up->up_dev->ud_hub != NULL) 548 up->up_dev->ud_hub->uh_explore(up->up_dev); 549 continue; 550 } 551 552 if (change & UPS_C_PORT_ENABLED) { 553 DPRINTF("uhub%jd port %jd C_PORT_ENABLED", 554 device_unit(sc->sc_dev), port, 0, 0); 555 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 556 if (change & UPS_C_CONNECT_STATUS) { 557 /* Ignore the port error if the device 558 vanished. */ 559 } else if (status & UPS_PORT_ENABLED) { 560 aprint_error_dev(sc->sc_dev, 561 "illegal enable change, port %d\n", port); 562 } else { 563 /* Port error condition. */ 564 if (up->up_restartcnt) /* no message first time */ 565 aprint_error_dev(sc->sc_dev, 566 "port error, restarting port %d\n", 567 port); 568 569 if (up->up_restartcnt++ < USBD_RESTART_MAX) 570 goto disco; 571 else 572 aprint_error_dev(sc->sc_dev, 573 "port error, giving up port %d\n", 574 port); 575 } 576 } 577 if (change & UPS_C_PORT_RESET) { 578 /* 579 * some xHCs set PortResetChange instead of CSC 580 * when port is reset. 581 */ 582 if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) { 583 change |= UPS_C_CONNECT_STATUS; 584 } 585 usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET); 586 } 587 if (change & UPS_C_BH_PORT_RESET) { 588 /* 589 * some xHCs set WarmResetChange instead of CSC 590 * when port is reset. 591 */ 592 if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) { 593 change |= UPS_C_CONNECT_STATUS; 594 } 595 usbd_clear_port_feature(dev, port, 596 UHF_C_BH_PORT_RESET); 597 } 598 if (change & UPS_C_PORT_LINK_STATE) 599 usbd_clear_port_feature(dev, port, 600 UHF_C_PORT_LINK_STATE); 601 if (change & UPS_C_PORT_CONFIG_ERROR) 602 usbd_clear_port_feature(dev, port, 603 UHF_C_PORT_CONFIG_ERROR); 604 605 /* XXX handle overcurrent and resume events! */ 606 607 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) { 608 /* No status change, just do recursive explore. */ 609 if (up->up_dev != NULL && up->up_dev->ud_hub != NULL) 610 up->up_dev->ud_hub->uh_explore(up->up_dev); 611 continue; 612 } 613 614 /* We have a connect status change, handle it. */ 615 616 DPRINTF("uhub%jd status change port %jd", 617 device_unit(sc->sc_dev), port, 0, 0); 618 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 619 /* 620 * If there is already a device on the port the change status 621 * must mean that is has disconnected. Looking at the 622 * current connect status is not enough to figure this out 623 * since a new unit may have been connected before we handle 624 * the disconnect. 625 */ 626 disco: 627 if (up->up_dev != NULL) { 628 /* Disconnected */ 629 DPRINTF("uhub%jd device addr=%jd disappeared on " 630 "port %jd", 631 device_unit(sc->sc_dev), up->up_dev->ud_addr, port, 632 0); 633 634 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE); 635 usbd_clear_port_feature(dev, port, 636 UHF_C_PORT_CONNECTION); 637 } 638 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 639 /* Nothing connected, just ignore it. */ 640 DPRINTFN(3, "uhub%jd port %jd !CURRENT_CONNECT_STATUS", 641 device_unit(sc->sc_dev), port, 0, 0); 642 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE); 643 usbd_clear_port_feature(dev, port, 644 UHF_C_PORT_CONNECTION); 645 continue; 646 } 647 648 /* Connected */ 649 DPRINTF("unit %jd dev->speed=%ju dev->depth=%ju", 650 device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0); 651 652 /* Wait for maximum device power up time. */ 653 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 654 655 /* Reset port, which implies enabling it. */ 656 if (usbd_reset_port(dev, port, &up->up_status)) { 657 aprint_error_dev(sc->sc_dev, 658 "port %d reset failed\n", port); 659 continue; 660 } 661 #if 0 662 /* Get port status again, it might have changed during reset */ 663 err = usbd_get_port_status(dev, port, &up->up_status); 664 if (err) { 665 DPRINTF("uhub%jd port %jd get port status failed, " 666 "err %jd", device_unit(sc->sc_dev), port, err, 0); 667 continue; 668 } 669 #endif 670 /* 671 * Use the port status from the reset to check for the device 672 * disappearing, the port enable status, and the port speed 673 */ 674 status = UGETW(up->up_status.wPortStatus); 675 change = UGETW(up->up_status.wPortChange); 676 DPRINTF("uhub%jd port %jd after reset: s/c=%jx/%jx", 677 device_unit(sc->sc_dev), port, status, change); 678 679 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 680 /* Nothing connected, just ignore it. */ 681 #ifdef DIAGNOSTIC 682 aprint_debug_dev(sc->sc_dev, 683 "port %d, device disappeared after reset\n", port); 684 #endif 685 continue; 686 } 687 if (!(status & UPS_PORT_ENABLED)) { 688 /* Not allowed send/receive packet. */ 689 #ifdef DIAGNOSTIC 690 printf("%s: port %d, device not enabled\n", 691 device_xname(sc->sc_dev), port); 692 #endif 693 continue; 694 } 695 /* port reset may cause Warm Reset Change, drop it. */ 696 if (change & UPS_C_BH_PORT_RESET) 697 usbd_clear_port_feature(dev, port, 698 UHF_C_BH_PORT_RESET); 699 700 /* 701 * Figure out device speed from power bit of port status. 702 * USB 2.0 ch 11.24.2.7.1 703 * USB 3.1 ch 10.16.2.6.1 704 */ 705 int sts = status; 706 if ((sts & UPS_PORT_POWER) == 0) 707 sts &= ~UPS_PORT_POWER_SS; 708 709 if (sts & UPS_HIGH_SPEED) 710 speed = USB_SPEED_HIGH; 711 else if (sts & UPS_LOW_SPEED) 712 speed = USB_SPEED_LOW; 713 else { 714 /* 715 * If there is no power bit set, it is certainly 716 * a Super Speed device, so use the speed of its 717 * parent hub. 718 */ 719 if (sts & UPS_PORT_POWER) 720 speed = USB_SPEED_FULL; 721 else 722 speed = dev->ud_speed; 723 } 724 725 /* 726 * Reduce the speed, otherwise we won't setup the proper 727 * transfer methods. 728 */ 729 if (speed > dev->ud_speed) 730 speed = dev->ud_speed; 731 732 DPRINTF("uhub%jd speed %ju", device_unit(sc->sc_dev), speed, 0, 733 0); 734 735 /* 736 * To check whether port has power, 737 * check UPS_PORT_POWER_SS bit if port speed is SS, and 738 * check UPS_PORT_POWER bit if port speed is HS/FS/LS. 739 */ 740 if (USB_IS_SS(speed)) { 741 /* SS hub port */ 742 if (!(status & UPS_PORT_POWER_SS)) 743 aprint_normal_dev(sc->sc_dev, 744 "strange, connected port %d has no power\n", 745 port); 746 } else { 747 /* HS/FS/LS hub port */ 748 if (!(status & UPS_PORT_POWER)) 749 aprint_normal_dev(sc->sc_dev, 750 "strange, connected port %d has no power\n", 751 port); 752 } 753 754 /* Get device info and set its address. */ 755 err = usbd_new_device(sc->sc_dev, dev->ud_bus, 756 dev->ud_depth + 1, speed, port, up); 757 /* XXX retry a few times? */ 758 if (err) { 759 DPRINTF("usbd_new_device failed, error %jd", err, 0, 0, 760 0); 761 /* Avoid addressing problems by disabling. */ 762 /* usbd_reset_port(dev, port, &up->status); */ 763 764 /* 765 * The unit refused to accept a new address, or had 766 * some other serious problem. Since we cannot leave 767 * at 0 we have to disable the port instead. 768 */ 769 aprint_error_dev(sc->sc_dev, 770 "device problem, disabling port %d\n", port); 771 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 772 } else { 773 /* The port set up succeeded, reset error count. */ 774 up->up_restartcnt = 0; 775 776 if (up->up_dev->ud_hub) 777 up->up_dev->ud_hub->uh_explore(up->up_dev); 778 } 779 } 780 mutex_enter(&sc->sc_lock); 781 sc->sc_explorepending = 0; 782 for (int i = 0; i < sc->sc_statuslen; i++) { 783 if (sc->sc_statuspend[i] != 0) { 784 memcpy(sc->sc_status, sc->sc_statuspend, 785 sc->sc_statuslen); 786 memset(sc->sc_statuspend, 0, sc->sc_statuslen); 787 usb_needs_explore(sc->sc_hub); 788 break; 789 } 790 } 791 mutex_exit(&sc->sc_lock); 792 793 return USBD_NORMAL_COMPLETION; 794 } 795 796 /* 797 * Called from process context when the hub is gone. 798 * Detach all devices on active ports. 799 */ 800 int 801 uhub_detach(device_t self, int flags) 802 { 803 struct uhub_softc *sc = device_private(self); 804 struct usbd_hub *hub = sc->sc_hub->ud_hub; 805 struct usbd_port *rup; 806 int nports, port, rc; 807 808 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 809 810 DPRINTF("uhub%jd flags=%jd", device_unit(self), flags, 0, 0); 811 812 if (hub == NULL) /* Must be partially working */ 813 return 0; 814 815 /* XXXSMP usb */ 816 KERNEL_LOCK(1, curlwp); 817 818 nports = hub->uh_hubdesc.bNbrPorts; 819 for (port = 0; port < nports; port++) { 820 rup = &hub->uh_ports[port]; 821 if (rup->up_dev == NULL) 822 continue; 823 if ((rc = usb_disconnect_port(rup, self, flags)) != 0) { 824 /* XXXSMP usb */ 825 KERNEL_UNLOCK_ONE(curlwp); 826 827 return rc; 828 } 829 } 830 831 pmf_device_deregister(self); 832 usbd_abort_pipe(sc->sc_ipipe); 833 usbd_close_pipe(sc->sc_ipipe); 834 835 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev); 836 837 if (hub->uh_ports[0].up_tt) 838 kmem_free(hub->uh_ports[0].up_tt, 839 (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 840 sizeof(struct usbd_tt)); 841 kmem_free(hub, 842 sizeof(*hub) + (nports-1) * sizeof(struct usbd_port)); 843 sc->sc_hub->ud_hub = NULL; 844 if (sc->sc_status) 845 kmem_free(sc->sc_status, sc->sc_statuslen); 846 if (sc->sc_statuspend) 847 kmem_free(sc->sc_statuspend, sc->sc_statuslen); 848 if (sc->sc_statusbuf) 849 kmem_free(sc->sc_statusbuf, sc->sc_statuslen); 850 851 mutex_destroy(&sc->sc_lock); 852 853 /* XXXSMP usb */ 854 KERNEL_UNLOCK_ONE(curlwp); 855 856 return 0; 857 } 858 859 int 860 uhub_rescan(device_t self, const char *ifattr, const int *locators) 861 { 862 struct uhub_softc *sc = device_private(self); 863 struct usbd_hub *hub = sc->sc_hub->ud_hub; 864 struct usbd_device *dev; 865 int port; 866 867 for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) { 868 dev = hub->uh_ports[port].up_dev; 869 if (dev == NULL) 870 continue; 871 usbd_reattach_device(sc->sc_dev, dev, port, locators); 872 } 873 return 0; 874 } 875 876 /* Called when a device has been detached from it */ 877 void 878 uhub_childdet(device_t self, device_t child) 879 { 880 struct uhub_softc *sc = device_private(self); 881 struct usbd_device *devhub = sc->sc_hub; 882 struct usbd_device *dev; 883 int nports; 884 int port; 885 int i; 886 887 if (!devhub->ud_hub) 888 /* should never happen; children are only created after init */ 889 panic("hub not fully initialised, but child deleted?"); 890 891 nports = devhub->ud_hub->uh_hubdesc.bNbrPorts; 892 for (port = 0; port < nports; port++) { 893 dev = devhub->ud_hub->uh_ports[port].up_dev; 894 if (!dev || dev->ud_subdevlen == 0) 895 continue; 896 for (i = 0; i < dev->ud_subdevlen; i++) { 897 if (dev->ud_subdevs[i] == child) { 898 dev->ud_subdevs[i] = NULL; 899 dev->ud_nifaces_claimed--; 900 } 901 } 902 if (dev->ud_nifaces_claimed == 0) { 903 kmem_free(dev->ud_subdevs, 904 dev->ud_subdevlen * sizeof(device_t)); 905 dev->ud_subdevs = NULL; 906 dev->ud_subdevlen = 0; 907 } 908 } 909 } 910 911 912 /* 913 * Hub interrupt. 914 * This an indication that some port has changed status. 915 * Notify the bus event handler thread that we need 916 * to be explored again. 917 */ 918 void 919 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 920 { 921 struct uhub_softc *sc = addr; 922 923 UHUBHIST_FUNC(); UHUBHIST_CALLED(); 924 925 DPRINTFN(5, "uhub%jd", device_unit(sc->sc_dev), 0, 0, 0); 926 927 if (status == USBD_STALLED) 928 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 929 else if (status == USBD_NORMAL_COMPLETION) { 930 931 mutex_enter(&sc->sc_lock); 932 933 DPRINTFN(5, "uhub%jd: explore pending %jd", 934 device_unit(sc->sc_dev), sc->sc_explorepending, 0, 0); 935 936 /* merge port bitmap into pending interrupts list */ 937 for (size_t i = 0; i < sc->sc_statuslen; i++) { 938 sc->sc_statuspend[i] |= sc->sc_statusbuf[i]; 939 940 DPRINTFN(5, "uhub%jd: pending/new ports " 941 "[%jd] %#jx/%#jx", device_unit(sc->sc_dev), 942 i, sc->sc_statuspend[i], sc->sc_statusbuf[i]); 943 } 944 945 if (!sc->sc_explorepending) { 946 sc->sc_explorepending = 1; 947 948 memcpy(sc->sc_status, sc->sc_statuspend, 949 sc->sc_statuslen); 950 memset(sc->sc_statuspend, 0, sc->sc_statuslen); 951 952 for (size_t i = 0; i < sc->sc_statuslen; i++) { 953 DPRINTFN(5, "uhub%jd: exploring ports " 954 "[%jd] %#jx", device_unit(sc->sc_dev), 955 i, sc->sc_status[i], 0); 956 } 957 958 usb_needs_explore(sc->sc_hub); 959 } 960 mutex_exit(&sc->sc_lock); 961 } 962 } 963