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