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/kstrtox.h> 11 #include <linux/slab.h> 12 #include <linux/pm_qos.h> 13 #include <linux/component.h> 14 #include <linux/usb/of.h> 15 16 #include "hub.h" 17 18 static int usb_port_block_power_off; 19 20 static const struct attribute_group *port_dev_group[]; 21 22 static ssize_t early_stop_show(struct device *dev, 23 struct device_attribute *attr, char *buf) 24 { 25 struct usb_port *port_dev = to_usb_port(dev); 26 27 return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no"); 28 } 29 30 static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr, 31 const char *buf, size_t count) 32 { 33 struct usb_port *port_dev = to_usb_port(dev); 34 bool value; 35 36 if (kstrtobool(buf, &value)) 37 return -EINVAL; 38 39 if (value) 40 port_dev->early_stop = 1; 41 else 42 port_dev->early_stop = 0; 43 44 return count; 45 } 46 static DEVICE_ATTR_RW(early_stop); 47 48 static ssize_t disable_show(struct device *dev, 49 struct device_attribute *attr, char *buf) 50 { 51 struct usb_port *port_dev = to_usb_port(dev); 52 struct usb_device *hdev = to_usb_device(dev->parent->parent); 53 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 54 struct usb_interface *intf = to_usb_interface(hub->intfdev); 55 int port1 = port_dev->portnum; 56 u16 portstatus, unused; 57 bool disabled; 58 int rc; 59 struct kernfs_node *kn; 60 61 hub_get(hub); 62 rc = usb_autopm_get_interface(intf); 63 if (rc < 0) 64 goto out_hub_get; 65 66 /* 67 * Prevent deadlock if another process is concurrently 68 * trying to unregister hdev. 69 */ 70 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr); 71 if (!kn) { 72 rc = -ENODEV; 73 goto out_autopm; 74 } 75 usb_lock_device(hdev); 76 if (hub->disconnected) { 77 rc = -ENODEV; 78 goto out_hdev_lock; 79 } 80 81 usb_hub_port_status(hub, port1, &portstatus, &unused); 82 disabled = !usb_port_is_power_on(hub, portstatus); 83 84 out_hdev_lock: 85 usb_unlock_device(hdev); 86 sysfs_unbreak_active_protection(kn); 87 out_autopm: 88 usb_autopm_put_interface(intf); 89 out_hub_get: 90 hub_put(hub); 91 92 if (rc) 93 return rc; 94 95 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0"); 96 } 97 98 static ssize_t disable_store(struct device *dev, struct device_attribute *attr, 99 const char *buf, size_t count) 100 { 101 struct usb_port *port_dev = to_usb_port(dev); 102 struct usb_device *hdev = to_usb_device(dev->parent->parent); 103 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 104 struct usb_interface *intf = to_usb_interface(hub->intfdev); 105 int port1 = port_dev->portnum; 106 bool disabled; 107 int rc; 108 struct kernfs_node *kn; 109 110 rc = kstrtobool(buf, &disabled); 111 if (rc) 112 return rc; 113 114 hub_get(hub); 115 rc = usb_autopm_get_interface(intf); 116 if (rc < 0) 117 goto out_hub_get; 118 119 /* 120 * Prevent deadlock if another process is concurrently 121 * trying to unregister hdev. 122 */ 123 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr); 124 if (!kn) { 125 rc = -ENODEV; 126 goto out_autopm; 127 } 128 usb_lock_device(hdev); 129 if (hub->disconnected) { 130 rc = -ENODEV; 131 goto out_hdev_lock; 132 } 133 134 if (disabled && port_dev->child) 135 usb_disconnect(&port_dev->child); 136 137 rc = usb_hub_set_port_power(hdev, hub, port1, !disabled); 138 139 if (disabled) { 140 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 141 if (!port_dev->is_superspeed) 142 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 143 } 144 145 if (!rc) 146 rc = count; 147 148 out_hdev_lock: 149 usb_unlock_device(hdev); 150 sysfs_unbreak_active_protection(kn); 151 out_autopm: 152 usb_autopm_put_interface(intf); 153 out_hub_get: 154 hub_put(hub); 155 156 return rc; 157 } 158 static DEVICE_ATTR_RW(disable); 159 160 static ssize_t location_show(struct device *dev, 161 struct device_attribute *attr, char *buf) 162 { 163 struct usb_port *port_dev = to_usb_port(dev); 164 165 return sprintf(buf, "0x%08x\n", port_dev->location); 166 } 167 static DEVICE_ATTR_RO(location); 168 169 static ssize_t connect_type_show(struct device *dev, 170 struct device_attribute *attr, char *buf) 171 { 172 struct usb_port *port_dev = to_usb_port(dev); 173 char *result; 174 175 switch (port_dev->connect_type) { 176 case USB_PORT_CONNECT_TYPE_HOT_PLUG: 177 result = "hotplug"; 178 break; 179 case USB_PORT_CONNECT_TYPE_HARD_WIRED: 180 result = "hardwired"; 181 break; 182 case USB_PORT_NOT_USED: 183 result = "not used"; 184 break; 185 default: 186 result = "unknown"; 187 break; 188 } 189 190 return sprintf(buf, "%s\n", result); 191 } 192 static DEVICE_ATTR_RO(connect_type); 193 194 static ssize_t state_show(struct device *dev, 195 struct device_attribute *attr, char *buf) 196 { 197 struct usb_port *port_dev = to_usb_port(dev); 198 enum usb_device_state state = READ_ONCE(port_dev->state); 199 200 return sysfs_emit(buf, "%s\n", usb_state_string(state)); 201 } 202 static DEVICE_ATTR_RO(state); 203 204 static ssize_t over_current_count_show(struct device *dev, 205 struct device_attribute *attr, char *buf) 206 { 207 struct usb_port *port_dev = to_usb_port(dev); 208 209 return sprintf(buf, "%u\n", port_dev->over_current_count); 210 } 211 static DEVICE_ATTR_RO(over_current_count); 212 213 static ssize_t quirks_show(struct device *dev, 214 struct device_attribute *attr, char *buf) 215 { 216 struct usb_port *port_dev = to_usb_port(dev); 217 218 return sprintf(buf, "%08x\n", port_dev->quirks); 219 } 220 221 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr, 222 const char *buf, size_t count) 223 { 224 struct usb_port *port_dev = to_usb_port(dev); 225 u32 value; 226 227 if (kstrtou32(buf, 16, &value)) 228 return -EINVAL; 229 230 port_dev->quirks = value; 231 return count; 232 } 233 static DEVICE_ATTR_RW(quirks); 234 235 static ssize_t usb3_lpm_permit_show(struct device *dev, 236 struct device_attribute *attr, char *buf) 237 { 238 struct usb_port *port_dev = to_usb_port(dev); 239 const char *p; 240 241 if (port_dev->usb3_lpm_u1_permit) { 242 if (port_dev->usb3_lpm_u2_permit) 243 p = "u1_u2"; 244 else 245 p = "u1"; 246 } else { 247 if (port_dev->usb3_lpm_u2_permit) 248 p = "u2"; 249 else 250 p = "0"; 251 } 252 253 return sprintf(buf, "%s\n", p); 254 } 255 256 static ssize_t usb3_lpm_permit_store(struct device *dev, 257 struct device_attribute *attr, 258 const char *buf, size_t count) 259 { 260 struct usb_port *port_dev = to_usb_port(dev); 261 struct usb_device *udev = port_dev->child; 262 struct usb_hcd *hcd; 263 264 if (!strncmp(buf, "u1_u2", 5)) { 265 port_dev->usb3_lpm_u1_permit = 1; 266 port_dev->usb3_lpm_u2_permit = 1; 267 268 } else if (!strncmp(buf, "u1", 2)) { 269 port_dev->usb3_lpm_u1_permit = 1; 270 port_dev->usb3_lpm_u2_permit = 0; 271 272 } else if (!strncmp(buf, "u2", 2)) { 273 port_dev->usb3_lpm_u1_permit = 0; 274 port_dev->usb3_lpm_u2_permit = 1; 275 276 } else if (!strncmp(buf, "0", 1)) { 277 port_dev->usb3_lpm_u1_permit = 0; 278 port_dev->usb3_lpm_u2_permit = 0; 279 } else 280 return -EINVAL; 281 282 /* If device is connected to the port, disable or enable lpm 283 * to make new u1 u2 setting take effect immediately. 284 */ 285 if (udev) { 286 hcd = bus_to_hcd(udev->bus); 287 if (!hcd) 288 return -EINVAL; 289 usb_lock_device(udev); 290 mutex_lock(hcd->bandwidth_mutex); 291 if (!usb_disable_lpm(udev)) 292 usb_enable_lpm(udev); 293 mutex_unlock(hcd->bandwidth_mutex); 294 usb_unlock_device(udev); 295 } 296 297 return count; 298 } 299 static DEVICE_ATTR_RW(usb3_lpm_permit); 300 301 static struct attribute *port_dev_attrs[] = { 302 &dev_attr_connect_type.attr, 303 &dev_attr_state.attr, 304 &dev_attr_location.attr, 305 &dev_attr_quirks.attr, 306 &dev_attr_over_current_count.attr, 307 &dev_attr_disable.attr, 308 &dev_attr_early_stop.attr, 309 NULL, 310 }; 311 312 static const struct attribute_group port_dev_attr_grp = { 313 .attrs = port_dev_attrs, 314 }; 315 316 static const struct attribute_group *port_dev_group[] = { 317 &port_dev_attr_grp, 318 NULL, 319 }; 320 321 static struct attribute *port_dev_usb3_attrs[] = { 322 &dev_attr_usb3_lpm_permit.attr, 323 NULL, 324 }; 325 326 static const struct attribute_group port_dev_usb3_attr_grp = { 327 .attrs = port_dev_usb3_attrs, 328 }; 329 330 static const struct attribute_group *port_dev_usb3_group[] = { 331 &port_dev_attr_grp, 332 &port_dev_usb3_attr_grp, 333 NULL, 334 }; 335 336 static void usb_port_device_release(struct device *dev) 337 { 338 struct usb_port *port_dev = to_usb_port(dev); 339 340 kfree(port_dev->req); 341 kfree(port_dev); 342 } 343 344 #ifdef CONFIG_PM 345 static int usb_port_runtime_resume(struct device *dev) 346 { 347 struct usb_port *port_dev = to_usb_port(dev); 348 struct usb_device *hdev = to_usb_device(dev->parent->parent); 349 struct usb_interface *intf = to_usb_interface(dev->parent); 350 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 351 struct usb_device *udev = port_dev->child; 352 struct usb_port *peer = port_dev->peer; 353 int port1 = port_dev->portnum; 354 int retval; 355 356 if (!hub) 357 return -EINVAL; 358 if (hub->in_reset) { 359 set_bit(port1, hub->power_bits); 360 return 0; 361 } 362 363 /* 364 * Power on our usb3 peer before this usb2 port to prevent a usb3 365 * device from degrading to its usb2 connection 366 */ 367 if (!port_dev->is_superspeed && peer) 368 pm_runtime_get_sync(&peer->dev); 369 370 retval = usb_autopm_get_interface(intf); 371 if (retval < 0) 372 return retval; 373 374 retval = usb_hub_set_port_power(hdev, hub, port1, true); 375 msleep(hub_power_on_good_delay(hub)); 376 if (udev && !retval) { 377 /* 378 * Our preference is to simply wait for the port to reconnect, 379 * as that is the lowest latency method to restart the port. 380 * However, there are cases where toggling port power results in 381 * the host port and the device port getting out of sync causing 382 * a link training live lock. Upon timeout, flag the port as 383 * needing warm reset recovery (to be performed later by 384 * usb_port_resume() as requested via usb_wakeup_notification()) 385 */ 386 if (hub_port_debounce_be_connected(hub, port1) < 0) { 387 dev_dbg(&port_dev->dev, "reconnect timeout\n"); 388 if (hub_is_superspeed(hdev)) 389 set_bit(port1, hub->warm_reset_bits); 390 } 391 392 /* Force the child awake to revalidate after the power loss. */ 393 if (!test_and_set_bit(port1, hub->child_usage_bits)) { 394 pm_runtime_get_noresume(&port_dev->dev); 395 pm_request_resume(&udev->dev); 396 } 397 } 398 399 usb_autopm_put_interface(intf); 400 401 return retval; 402 } 403 404 static int usb_port_runtime_suspend(struct device *dev) 405 { 406 struct usb_port *port_dev = to_usb_port(dev); 407 struct usb_device *hdev = to_usb_device(dev->parent->parent); 408 struct usb_interface *intf = to_usb_interface(dev->parent); 409 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 410 struct usb_port *peer = port_dev->peer; 411 int port1 = port_dev->portnum; 412 int retval; 413 414 if (!hub) 415 return -EINVAL; 416 if (hub->in_reset) 417 return -EBUSY; 418 419 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) 420 == PM_QOS_FLAGS_ALL) 421 return -EAGAIN; 422 423 if (usb_port_block_power_off) 424 return -EBUSY; 425 426 retval = usb_autopm_get_interface(intf); 427 if (retval < 0) 428 return retval; 429 430 retval = usb_hub_set_port_power(hdev, hub, port1, false); 431 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 432 if (!port_dev->is_superspeed) 433 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 434 usb_autopm_put_interface(intf); 435 436 /* 437 * Our peer usb3 port may now be able to suspend, so 438 * asynchronously queue a suspend request to observe that this 439 * usb2 port is now off. 440 */ 441 if (!port_dev->is_superspeed && peer) 442 pm_runtime_put(&peer->dev); 443 444 return retval; 445 } 446 #endif 447 448 static void usb_port_shutdown(struct device *dev) 449 { 450 struct usb_port *port_dev = to_usb_port(dev); 451 452 if (port_dev->child) 453 usb_disable_usb2_hardware_lpm(port_dev->child); 454 } 455 456 static const struct dev_pm_ops usb_port_pm_ops = { 457 #ifdef CONFIG_PM 458 .runtime_suspend = usb_port_runtime_suspend, 459 .runtime_resume = usb_port_runtime_resume, 460 #endif 461 }; 462 463 const struct device_type usb_port_device_type = { 464 .name = "usb_port", 465 .release = usb_port_device_release, 466 .pm = &usb_port_pm_ops, 467 }; 468 469 static struct device_driver usb_port_driver = { 470 .name = "usb", 471 .owner = THIS_MODULE, 472 .shutdown = usb_port_shutdown, 473 }; 474 475 static int link_peers(struct usb_port *left, struct usb_port *right) 476 { 477 struct usb_port *ss_port, *hs_port; 478 int rc; 479 480 if (left->peer == right && right->peer == left) 481 return 0; 482 483 if (left->peer || right->peer) { 484 struct usb_port *lpeer = left->peer; 485 struct usb_port *rpeer = right->peer; 486 char *method; 487 488 if (left->location && left->location == right->location) 489 method = "location"; 490 else 491 method = "default"; 492 493 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n", 494 dev_name(&left->dev), dev_name(&right->dev), method, 495 dev_name(&left->dev), 496 lpeer ? dev_name(&lpeer->dev) : "none", 497 dev_name(&right->dev), 498 rpeer ? dev_name(&rpeer->dev) : "none"); 499 return -EBUSY; 500 } 501 502 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer"); 503 if (rc) 504 return rc; 505 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer"); 506 if (rc) { 507 sysfs_remove_link(&left->dev.kobj, "peer"); 508 return rc; 509 } 510 511 /* 512 * We need to wake the HiSpeed port to make sure we don't race 513 * setting ->peer with usb_port_runtime_suspend(). Otherwise we 514 * may miss a suspend event for the SuperSpeed port. 515 */ 516 if (left->is_superspeed) { 517 ss_port = left; 518 WARN_ON(right->is_superspeed); 519 hs_port = right; 520 } else { 521 ss_port = right; 522 WARN_ON(!right->is_superspeed); 523 hs_port = left; 524 } 525 pm_runtime_get_sync(&hs_port->dev); 526 527 left->peer = right; 528 right->peer = left; 529 530 /* 531 * The SuperSpeed reference is dropped when the HiSpeed port in 532 * this relationship suspends, i.e. when it is safe to allow a 533 * SuperSpeed connection to drop since there is no risk of a 534 * device degrading to its powered-off HiSpeed connection. 535 * 536 * Also, drop the HiSpeed ref taken above. 537 */ 538 pm_runtime_get_sync(&ss_port->dev); 539 pm_runtime_put(&hs_port->dev); 540 541 return 0; 542 } 543 544 static void link_peers_report(struct usb_port *left, struct usb_port *right) 545 { 546 int rc; 547 548 rc = link_peers(left, right); 549 if (rc == 0) { 550 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev)); 551 } else { 552 dev_dbg(&left->dev, "failed to peer to %s (%d)\n", 553 dev_name(&right->dev), rc); 554 pr_warn_once("usb: port power management may be unreliable\n"); 555 usb_port_block_power_off = 1; 556 } 557 } 558 559 static void unlink_peers(struct usb_port *left, struct usb_port *right) 560 { 561 struct usb_port *ss_port, *hs_port; 562 563 WARN(right->peer != left || left->peer != right, 564 "%s and %s are not peers?\n", 565 dev_name(&left->dev), dev_name(&right->dev)); 566 567 /* 568 * We wake the HiSpeed port to make sure we don't race its 569 * usb_port_runtime_resume() event which takes a SuperSpeed ref 570 * when ->peer is !NULL. 571 */ 572 if (left->is_superspeed) { 573 ss_port = left; 574 hs_port = right; 575 } else { 576 ss_port = right; 577 hs_port = left; 578 } 579 580 pm_runtime_get_sync(&hs_port->dev); 581 582 sysfs_remove_link(&left->dev.kobj, "peer"); 583 right->peer = NULL; 584 sysfs_remove_link(&right->dev.kobj, "peer"); 585 left->peer = NULL; 586 587 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */ 588 pm_runtime_put(&ss_port->dev); 589 590 /* Drop the ref taken above */ 591 pm_runtime_put(&hs_port->dev); 592 } 593 594 /* 595 * For each usb hub device in the system check to see if it is in the 596 * peer domain of the given port_dev, and if it is check to see if it 597 * has a port that matches the given port by location 598 */ 599 static int match_location(struct usb_device *peer_hdev, void *p) 600 { 601 int port1; 602 struct usb_hcd *hcd, *peer_hcd; 603 struct usb_port *port_dev = p, *peer; 604 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev); 605 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent); 606 607 if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED) 608 return 0; 609 610 hcd = bus_to_hcd(hdev->bus); 611 peer_hcd = bus_to_hcd(peer_hdev->bus); 612 /* peer_hcd is provisional until we verify it against the known peer */ 613 if (peer_hcd != hcd->shared_hcd) 614 return 0; 615 616 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) { 617 peer = peer_hub->ports[port1 - 1]; 618 if (peer && peer->connect_type != USB_PORT_NOT_USED && 619 peer->location == port_dev->location) { 620 link_peers_report(port_dev, peer); 621 return 1; /* done */ 622 } 623 } 624 625 return 0; 626 } 627 628 /* 629 * Find the peer port either via explicit platform firmware "location" 630 * data, the peer hcd for root hubs, or the upstream peer relationship 631 * for all other hubs. 632 */ 633 static void find_and_link_peer(struct usb_hub *hub, int port1) 634 { 635 struct usb_port *port_dev = hub->ports[port1 - 1], *peer; 636 struct usb_device *hdev = hub->hdev; 637 struct usb_device *peer_hdev; 638 struct usb_hub *peer_hub; 639 640 /* 641 * If location data is available then we can only peer this port 642 * by a location match, not the default peer (lest we create a 643 * situation where we need to go back and undo a default peering 644 * when the port is later peered by location data) 645 */ 646 if (port_dev->location) { 647 /* we link the peer in match_location() if found */ 648 usb_for_each_dev(port_dev, match_location); 649 return; 650 } else if (!hdev->parent) { 651 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 652 struct usb_hcd *peer_hcd = hcd->shared_hcd; 653 654 if (!peer_hcd) 655 return; 656 657 peer_hdev = peer_hcd->self.root_hub; 658 } else { 659 struct usb_port *upstream; 660 struct usb_device *parent = hdev->parent; 661 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent); 662 663 if (!parent_hub) 664 return; 665 666 upstream = parent_hub->ports[hdev->portnum - 1]; 667 if (!upstream || !upstream->peer) 668 return; 669 670 peer_hdev = upstream->peer->child; 671 } 672 673 peer_hub = usb_hub_to_struct_hub(peer_hdev); 674 if (!peer_hub || port1 > peer_hdev->maxchild) 675 return; 676 677 /* 678 * we found a valid default peer, last check is to make sure it 679 * does not have location data 680 */ 681 peer = peer_hub->ports[port1 - 1]; 682 if (peer && peer->location == 0) 683 link_peers_report(port_dev, peer); 684 } 685 686 static int connector_bind(struct device *dev, struct device *connector, void *data) 687 { 688 struct usb_port *port_dev = to_usb_port(dev); 689 int ret; 690 691 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector"); 692 if (ret) 693 return ret; 694 695 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev)); 696 if (ret) { 697 sysfs_remove_link(&dev->kobj, "connector"); 698 return ret; 699 } 700 701 port_dev->connector = data; 702 703 /* 704 * If there is already USB device connected to the port, letting the 705 * Type-C connector know about it immediately. 706 */ 707 if (port_dev->child) 708 typec_attach(port_dev->connector, &port_dev->child->dev); 709 710 return 0; 711 } 712 713 static void connector_unbind(struct device *dev, struct device *connector, void *data) 714 { 715 struct usb_port *port_dev = to_usb_port(dev); 716 717 sysfs_remove_link(&connector->kobj, dev_name(dev)); 718 sysfs_remove_link(&dev->kobj, "connector"); 719 port_dev->connector = NULL; 720 } 721 722 static const struct component_ops connector_ops = { 723 .bind = connector_bind, 724 .unbind = connector_unbind, 725 }; 726 727 int usb_hub_create_port_device(struct usb_hub *hub, int port1) 728 { 729 struct usb_port *port_dev; 730 struct usb_device *hdev = hub->hdev; 731 int retval; 732 733 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); 734 if (!port_dev) 735 return -ENOMEM; 736 737 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL); 738 if (!port_dev->req) { 739 kfree(port_dev); 740 return -ENOMEM; 741 } 742 743 port_dev->connect_type = usb_of_get_connect_type(hdev, port1); 744 hub->ports[port1 - 1] = port_dev; 745 port_dev->portnum = port1; 746 set_bit(port1, hub->power_bits); 747 port_dev->dev.parent = hub->intfdev; 748 if (hub_is_superspeed(hdev)) { 749 port_dev->is_superspeed = 1; 750 port_dev->usb3_lpm_u1_permit = 1; 751 port_dev->usb3_lpm_u2_permit = 1; 752 port_dev->dev.groups = port_dev_usb3_group; 753 } else 754 port_dev->dev.groups = port_dev_group; 755 port_dev->dev.type = &usb_port_device_type; 756 port_dev->dev.driver = &usb_port_driver; 757 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev), 758 port1); 759 mutex_init(&port_dev->status_lock); 760 retval = device_register(&port_dev->dev); 761 if (retval) { 762 put_device(&port_dev->dev); 763 return retval; 764 } 765 766 port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state"); 767 if (!port_dev->state_kn) { 768 dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n"); 769 retval = -ENODEV; 770 goto err_unregister; 771 } 772 773 /* Set default policy of port-poweroff disabled. */ 774 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req, 775 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF); 776 if (retval < 0) { 777 goto err_put_kn; 778 } 779 780 retval = component_add(&port_dev->dev, &connector_ops); 781 if (retval) { 782 dev_warn(&port_dev->dev, "failed to add component\n"); 783 goto err_put_kn; 784 } 785 786 find_and_link_peer(hub, port1); 787 788 /* 789 * Enable runtime pm and hold a refernce that hub_configure() 790 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set 791 * and the hub has been fully registered (hdev->maxchild set). 792 */ 793 pm_runtime_set_active(&port_dev->dev); 794 pm_runtime_get_noresume(&port_dev->dev); 795 pm_runtime_enable(&port_dev->dev); 796 device_enable_async_suspend(&port_dev->dev); 797 798 /* 799 * Keep hidden the ability to enable port-poweroff if the hub 800 * does not support power switching. 801 */ 802 if (!hub_is_port_power_switchable(hub)) 803 return 0; 804 805 /* Attempt to let userspace take over the policy. */ 806 retval = dev_pm_qos_expose_flags(&port_dev->dev, 807 PM_QOS_FLAG_NO_POWER_OFF); 808 if (retval < 0) { 809 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n"); 810 return 0; 811 } 812 813 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */ 814 retval = dev_pm_qos_remove_request(port_dev->req); 815 if (retval >= 0) { 816 kfree(port_dev->req); 817 port_dev->req = NULL; 818 } 819 return 0; 820 821 err_put_kn: 822 sysfs_put(port_dev->state_kn); 823 err_unregister: 824 device_unregister(&port_dev->dev); 825 826 return retval; 827 } 828 829 void usb_hub_remove_port_device(struct usb_hub *hub, int port1) 830 { 831 struct usb_port *port_dev = hub->ports[port1 - 1]; 832 struct usb_port *peer; 833 834 peer = port_dev->peer; 835 if (peer) 836 unlink_peers(port_dev, peer); 837 component_del(&port_dev->dev, &connector_ops); 838 sysfs_put(port_dev->state_kn); 839 device_unregister(&port_dev->dev); 840 } 841