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