1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * usb port device code 4 * 5 * Copyright (C) 2012 Intel Corp 6 * 7 * Author: Lan Tianyu <tianyu.lan@intel.com> 8 */ 9 10 #include <linux/slab.h> 11 #include <linux/pm_qos.h> 12 13 #include "hub.h" 14 15 static int usb_port_block_power_off; 16 17 static const struct attribute_group *port_dev_group[]; 18 19 static ssize_t connect_type_show(struct device *dev, 20 struct device_attribute *attr, char *buf) 21 { 22 struct usb_port *port_dev = to_usb_port(dev); 23 char *result; 24 25 switch (port_dev->connect_type) { 26 case USB_PORT_CONNECT_TYPE_HOT_PLUG: 27 result = "hotplug"; 28 break; 29 case USB_PORT_CONNECT_TYPE_HARD_WIRED: 30 result = "hardwired"; 31 break; 32 case USB_PORT_NOT_USED: 33 result = "not used"; 34 break; 35 default: 36 result = "unknown"; 37 break; 38 } 39 40 return sprintf(buf, "%s\n", result); 41 } 42 static DEVICE_ATTR_RO(connect_type); 43 44 static ssize_t over_current_count_show(struct device *dev, 45 struct device_attribute *attr, char *buf) 46 { 47 struct usb_port *port_dev = to_usb_port(dev); 48 49 return sprintf(buf, "%u\n", port_dev->over_current_count); 50 } 51 static DEVICE_ATTR_RO(over_current_count); 52 53 static ssize_t quirks_show(struct device *dev, 54 struct device_attribute *attr, char *buf) 55 { 56 struct usb_port *port_dev = to_usb_port(dev); 57 58 return sprintf(buf, "%08x\n", port_dev->quirks); 59 } 60 61 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr, 62 const char *buf, size_t count) 63 { 64 struct usb_port *port_dev = to_usb_port(dev); 65 u32 value; 66 67 if (kstrtou32(buf, 16, &value)) 68 return -EINVAL; 69 70 port_dev->quirks = value; 71 return count; 72 } 73 static DEVICE_ATTR_RW(quirks); 74 75 static ssize_t usb3_lpm_permit_show(struct device *dev, 76 struct device_attribute *attr, char *buf) 77 { 78 struct usb_port *port_dev = to_usb_port(dev); 79 const char *p; 80 81 if (port_dev->usb3_lpm_u1_permit) { 82 if (port_dev->usb3_lpm_u2_permit) 83 p = "u1_u2"; 84 else 85 p = "u1"; 86 } else { 87 if (port_dev->usb3_lpm_u2_permit) 88 p = "u2"; 89 else 90 p = "0"; 91 } 92 93 return sprintf(buf, "%s\n", p); 94 } 95 96 static ssize_t usb3_lpm_permit_store(struct device *dev, 97 struct device_attribute *attr, 98 const char *buf, size_t count) 99 { 100 struct usb_port *port_dev = to_usb_port(dev); 101 struct usb_device *udev = port_dev->child; 102 struct usb_hcd *hcd; 103 104 if (!strncmp(buf, "u1_u2", 5)) { 105 port_dev->usb3_lpm_u1_permit = 1; 106 port_dev->usb3_lpm_u2_permit = 1; 107 108 } else if (!strncmp(buf, "u1", 2)) { 109 port_dev->usb3_lpm_u1_permit = 1; 110 port_dev->usb3_lpm_u2_permit = 0; 111 112 } else if (!strncmp(buf, "u2", 2)) { 113 port_dev->usb3_lpm_u1_permit = 0; 114 port_dev->usb3_lpm_u2_permit = 1; 115 116 } else if (!strncmp(buf, "0", 1)) { 117 port_dev->usb3_lpm_u1_permit = 0; 118 port_dev->usb3_lpm_u2_permit = 0; 119 } else 120 return -EINVAL; 121 122 /* If device is connected to the port, disable or enable lpm 123 * to make new u1 u2 setting take effect immediately. 124 */ 125 if (udev) { 126 hcd = bus_to_hcd(udev->bus); 127 if (!hcd) 128 return -EINVAL; 129 usb_lock_device(udev); 130 mutex_lock(hcd->bandwidth_mutex); 131 if (!usb_disable_lpm(udev)) 132 usb_enable_lpm(udev); 133 mutex_unlock(hcd->bandwidth_mutex); 134 usb_unlock_device(udev); 135 } 136 137 return count; 138 } 139 static DEVICE_ATTR_RW(usb3_lpm_permit); 140 141 static struct attribute *port_dev_attrs[] = { 142 &dev_attr_connect_type.attr, 143 &dev_attr_quirks.attr, 144 &dev_attr_over_current_count.attr, 145 NULL, 146 }; 147 148 static struct attribute_group port_dev_attr_grp = { 149 .attrs = port_dev_attrs, 150 }; 151 152 static const struct attribute_group *port_dev_group[] = { 153 &port_dev_attr_grp, 154 NULL, 155 }; 156 157 static struct attribute *port_dev_usb3_attrs[] = { 158 &dev_attr_usb3_lpm_permit.attr, 159 NULL, 160 }; 161 162 static struct attribute_group port_dev_usb3_attr_grp = { 163 .attrs = port_dev_usb3_attrs, 164 }; 165 166 static const struct attribute_group *port_dev_usb3_group[] = { 167 &port_dev_attr_grp, 168 &port_dev_usb3_attr_grp, 169 NULL, 170 }; 171 172 static void usb_port_device_release(struct device *dev) 173 { 174 struct usb_port *port_dev = to_usb_port(dev); 175 176 kfree(port_dev->req); 177 kfree(port_dev); 178 } 179 180 #ifdef CONFIG_PM 181 static int usb_port_runtime_resume(struct device *dev) 182 { 183 struct usb_port *port_dev = to_usb_port(dev); 184 struct usb_device *hdev = to_usb_device(dev->parent->parent); 185 struct usb_interface *intf = to_usb_interface(dev->parent); 186 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 187 struct usb_device *udev = port_dev->child; 188 struct usb_port *peer = port_dev->peer; 189 int port1 = port_dev->portnum; 190 int retval; 191 192 if (!hub) 193 return -EINVAL; 194 if (hub->in_reset) { 195 set_bit(port1, hub->power_bits); 196 return 0; 197 } 198 199 /* 200 * Power on our usb3 peer before this usb2 port to prevent a usb3 201 * device from degrading to its usb2 connection 202 */ 203 if (!port_dev->is_superspeed && peer) 204 pm_runtime_get_sync(&peer->dev); 205 206 usb_autopm_get_interface(intf); 207 retval = usb_hub_set_port_power(hdev, hub, port1, true); 208 msleep(hub_power_on_good_delay(hub)); 209 if (udev && !retval) { 210 /* 211 * Our preference is to simply wait for the port to reconnect, 212 * as that is the lowest latency method to restart the port. 213 * However, there are cases where toggling port power results in 214 * the host port and the device port getting out of sync causing 215 * a link training live lock. Upon timeout, flag the port as 216 * needing warm reset recovery (to be performed later by 217 * usb_port_resume() as requested via usb_wakeup_notification()) 218 */ 219 if (hub_port_debounce_be_connected(hub, port1) < 0) { 220 dev_dbg(&port_dev->dev, "reconnect timeout\n"); 221 if (hub_is_superspeed(hdev)) 222 set_bit(port1, hub->warm_reset_bits); 223 } 224 225 /* Force the child awake to revalidate after the power loss. */ 226 if (!test_and_set_bit(port1, hub->child_usage_bits)) { 227 pm_runtime_get_noresume(&port_dev->dev); 228 pm_request_resume(&udev->dev); 229 } 230 } 231 232 usb_autopm_put_interface(intf); 233 234 return retval; 235 } 236 237 static int usb_port_runtime_suspend(struct device *dev) 238 { 239 struct usb_port *port_dev = to_usb_port(dev); 240 struct usb_device *hdev = to_usb_device(dev->parent->parent); 241 struct usb_interface *intf = to_usb_interface(dev->parent); 242 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 243 struct usb_port *peer = port_dev->peer; 244 int port1 = port_dev->portnum; 245 int retval; 246 247 if (!hub) 248 return -EINVAL; 249 if (hub->in_reset) 250 return -EBUSY; 251 252 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) 253 == PM_QOS_FLAGS_ALL) 254 return -EAGAIN; 255 256 if (usb_port_block_power_off) 257 return -EBUSY; 258 259 usb_autopm_get_interface(intf); 260 retval = usb_hub_set_port_power(hdev, hub, port1, false); 261 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 262 if (!port_dev->is_superspeed) 263 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 264 usb_autopm_put_interface(intf); 265 266 /* 267 * Our peer usb3 port may now be able to suspend, so 268 * asynchronously queue a suspend request to observe that this 269 * usb2 port is now off. 270 */ 271 if (!port_dev->is_superspeed && peer) 272 pm_runtime_put(&peer->dev); 273 274 return retval; 275 } 276 #endif 277 278 static const struct dev_pm_ops usb_port_pm_ops = { 279 #ifdef CONFIG_PM 280 .runtime_suspend = usb_port_runtime_suspend, 281 .runtime_resume = usb_port_runtime_resume, 282 #endif 283 }; 284 285 struct device_type usb_port_device_type = { 286 .name = "usb_port", 287 .release = usb_port_device_release, 288 .pm = &usb_port_pm_ops, 289 }; 290 291 static struct device_driver usb_port_driver = { 292 .name = "usb", 293 .owner = THIS_MODULE, 294 }; 295 296 static int link_peers(struct usb_port *left, struct usb_port *right) 297 { 298 struct usb_port *ss_port, *hs_port; 299 int rc; 300 301 if (left->peer == right && right->peer == left) 302 return 0; 303 304 if (left->peer || right->peer) { 305 struct usb_port *lpeer = left->peer; 306 struct usb_port *rpeer = right->peer; 307 char *method; 308 309 if (left->location && left->location == right->location) 310 method = "location"; 311 else 312 method = "default"; 313 314 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n", 315 dev_name(&left->dev), dev_name(&right->dev), method, 316 dev_name(&left->dev), 317 lpeer ? dev_name(&lpeer->dev) : "none", 318 dev_name(&right->dev), 319 rpeer ? dev_name(&rpeer->dev) : "none"); 320 return -EBUSY; 321 } 322 323 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer"); 324 if (rc) 325 return rc; 326 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer"); 327 if (rc) { 328 sysfs_remove_link(&left->dev.kobj, "peer"); 329 return rc; 330 } 331 332 /* 333 * We need to wake the HiSpeed port to make sure we don't race 334 * setting ->peer with usb_port_runtime_suspend(). Otherwise we 335 * may miss a suspend event for the SuperSpeed port. 336 */ 337 if (left->is_superspeed) { 338 ss_port = left; 339 WARN_ON(right->is_superspeed); 340 hs_port = right; 341 } else { 342 ss_port = right; 343 WARN_ON(!right->is_superspeed); 344 hs_port = left; 345 } 346 pm_runtime_get_sync(&hs_port->dev); 347 348 left->peer = right; 349 right->peer = left; 350 351 /* 352 * The SuperSpeed reference is dropped when the HiSpeed port in 353 * this relationship suspends, i.e. when it is safe to allow a 354 * SuperSpeed connection to drop since there is no risk of a 355 * device degrading to its powered-off HiSpeed connection. 356 * 357 * Also, drop the HiSpeed ref taken above. 358 */ 359 pm_runtime_get_sync(&ss_port->dev); 360 pm_runtime_put(&hs_port->dev); 361 362 return 0; 363 } 364 365 static void link_peers_report(struct usb_port *left, struct usb_port *right) 366 { 367 int rc; 368 369 rc = link_peers(left, right); 370 if (rc == 0) { 371 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev)); 372 } else { 373 dev_dbg(&left->dev, "failed to peer to %s (%d)\n", 374 dev_name(&right->dev), rc); 375 pr_warn_once("usb: port power management may be unreliable\n"); 376 usb_port_block_power_off = 1; 377 } 378 } 379 380 static void unlink_peers(struct usb_port *left, struct usb_port *right) 381 { 382 struct usb_port *ss_port, *hs_port; 383 384 WARN(right->peer != left || left->peer != right, 385 "%s and %s are not peers?\n", 386 dev_name(&left->dev), dev_name(&right->dev)); 387 388 /* 389 * We wake the HiSpeed port to make sure we don't race its 390 * usb_port_runtime_resume() event which takes a SuperSpeed ref 391 * when ->peer is !NULL. 392 */ 393 if (left->is_superspeed) { 394 ss_port = left; 395 hs_port = right; 396 } else { 397 ss_port = right; 398 hs_port = left; 399 } 400 401 pm_runtime_get_sync(&hs_port->dev); 402 403 sysfs_remove_link(&left->dev.kobj, "peer"); 404 right->peer = NULL; 405 sysfs_remove_link(&right->dev.kobj, "peer"); 406 left->peer = NULL; 407 408 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */ 409 pm_runtime_put(&ss_port->dev); 410 411 /* Drop the ref taken above */ 412 pm_runtime_put(&hs_port->dev); 413 } 414 415 /* 416 * For each usb hub device in the system check to see if it is in the 417 * peer domain of the given port_dev, and if it is check to see if it 418 * has a port that matches the given port by location 419 */ 420 static int match_location(struct usb_device *peer_hdev, void *p) 421 { 422 int port1; 423 struct usb_hcd *hcd, *peer_hcd; 424 struct usb_port *port_dev = p, *peer; 425 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev); 426 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent); 427 428 if (!peer_hub) 429 return 0; 430 431 hcd = bus_to_hcd(hdev->bus); 432 peer_hcd = bus_to_hcd(peer_hdev->bus); 433 /* peer_hcd is provisional until we verify it against the known peer */ 434 if (peer_hcd != hcd->shared_hcd) 435 return 0; 436 437 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) { 438 peer = peer_hub->ports[port1 - 1]; 439 if (peer && peer->location == port_dev->location) { 440 link_peers_report(port_dev, peer); 441 return 1; /* done */ 442 } 443 } 444 445 return 0; 446 } 447 448 /* 449 * Find the peer port either via explicit platform firmware "location" 450 * data, the peer hcd for root hubs, or the upstream peer relationship 451 * for all other hubs. 452 */ 453 static void find_and_link_peer(struct usb_hub *hub, int port1) 454 { 455 struct usb_port *port_dev = hub->ports[port1 - 1], *peer; 456 struct usb_device *hdev = hub->hdev; 457 struct usb_device *peer_hdev; 458 struct usb_hub *peer_hub; 459 460 /* 461 * If location data is available then we can only peer this port 462 * by a location match, not the default peer (lest we create a 463 * situation where we need to go back and undo a default peering 464 * when the port is later peered by location data) 465 */ 466 if (port_dev->location) { 467 /* we link the peer in match_location() if found */ 468 usb_for_each_dev(port_dev, match_location); 469 return; 470 } else if (!hdev->parent) { 471 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 472 struct usb_hcd *peer_hcd = hcd->shared_hcd; 473 474 if (!peer_hcd) 475 return; 476 477 peer_hdev = peer_hcd->self.root_hub; 478 } else { 479 struct usb_port *upstream; 480 struct usb_device *parent = hdev->parent; 481 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent); 482 483 if (!parent_hub) 484 return; 485 486 upstream = parent_hub->ports[hdev->portnum - 1]; 487 if (!upstream || !upstream->peer) 488 return; 489 490 peer_hdev = upstream->peer->child; 491 } 492 493 peer_hub = usb_hub_to_struct_hub(peer_hdev); 494 if (!peer_hub || port1 > peer_hdev->maxchild) 495 return; 496 497 /* 498 * we found a valid default peer, last check is to make sure it 499 * does not have location data 500 */ 501 peer = peer_hub->ports[port1 - 1]; 502 if (peer && peer->location == 0) 503 link_peers_report(port_dev, peer); 504 } 505 506 int usb_hub_create_port_device(struct usb_hub *hub, int port1) 507 { 508 struct usb_port *port_dev; 509 struct usb_device *hdev = hub->hdev; 510 int retval; 511 512 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 513 if (!port_dev) 514 return -ENOMEM; 515 516 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL); 517 if (!port_dev->req) { 518 kfree(port_dev); 519 return -ENOMEM; 520 } 521 522 hub->ports[port1 - 1] = port_dev; 523 port_dev->portnum = port1; 524 set_bit(port1, hub->power_bits); 525 port_dev->dev.parent = hub->intfdev; 526 if (hub_is_superspeed(hdev)) { 527 port_dev->usb3_lpm_u1_permit = 1; 528 port_dev->usb3_lpm_u2_permit = 1; 529 port_dev->dev.groups = port_dev_usb3_group; 530 } else 531 port_dev->dev.groups = port_dev_group; 532 port_dev->dev.type = &usb_port_device_type; 533 port_dev->dev.driver = &usb_port_driver; 534 if (hub_is_superspeed(hub->hdev)) 535 port_dev->is_superspeed = 1; 536 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev), 537 port1); 538 mutex_init(&port_dev->status_lock); 539 retval = device_register(&port_dev->dev); 540 if (retval) { 541 put_device(&port_dev->dev); 542 return retval; 543 } 544 545 /* Set default policy of port-poweroff disabled. */ 546 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req, 547 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF); 548 if (retval < 0) { 549 device_unregister(&port_dev->dev); 550 return retval; 551 } 552 553 find_and_link_peer(hub, port1); 554 555 /* 556 * Enable runtime pm and hold a refernce that hub_configure() 557 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set 558 * and the hub has been fully registered (hdev->maxchild set). 559 */ 560 pm_runtime_set_active(&port_dev->dev); 561 pm_runtime_get_noresume(&port_dev->dev); 562 pm_runtime_enable(&port_dev->dev); 563 device_enable_async_suspend(&port_dev->dev); 564 565 /* 566 * Keep hidden the ability to enable port-poweroff if the hub 567 * does not support power switching. 568 */ 569 if (!hub_is_port_power_switchable(hub)) 570 return 0; 571 572 /* Attempt to let userspace take over the policy. */ 573 retval = dev_pm_qos_expose_flags(&port_dev->dev, 574 PM_QOS_FLAG_NO_POWER_OFF); 575 if (retval < 0) { 576 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n"); 577 return 0; 578 } 579 580 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */ 581 retval = dev_pm_qos_remove_request(port_dev->req); 582 if (retval >= 0) { 583 kfree(port_dev->req); 584 port_dev->req = NULL; 585 } 586 return 0; 587 } 588 589 void usb_hub_remove_port_device(struct usb_hub *hub, int port1) 590 { 591 struct usb_port *port_dev = hub->ports[port1 - 1]; 592 struct usb_port *peer; 593 594 peer = port_dev->peer; 595 if (peer) 596 unlink_peers(port_dev, peer); 597 device_unregister(&port_dev->dev); 598 } 599