1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
10 #include <linux/kstrtox.h>
11 #include <linux/slab.h>
12 #include <linux/pm_qos.h>
13 #include <linux/component.h>
17 static int usb_port_block_power_off;
19 static const struct attribute_group *port_dev_group[];
21 static ssize_t early_stop_show(struct device *dev,
22 struct device_attribute *attr, char *buf)
24 struct usb_port *port_dev = to_usb_port(dev);
26 return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no");
29 static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
30 const char *buf, size_t count)
32 struct usb_port *port_dev = to_usb_port(dev);
35 if (kstrtobool(buf, &value))
39 port_dev->early_stop = 1;
41 port_dev->early_stop = 0;
45 static DEVICE_ATTR_RW(early_stop);
47 static ssize_t disable_show(struct device *dev,
48 struct device_attribute *attr, char *buf)
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(hub->intfdev);
54 int port1 = port_dev->portnum;
55 u16 portstatus, unused;
59 rc = usb_autopm_get_interface(intf);
63 usb_lock_device(hdev);
64 if (hub->disconnected) {
69 usb_hub_port_status(hub, port1, &portstatus, &unused);
70 disabled = !usb_port_is_power_on(hub, portstatus);
73 usb_unlock_device(hdev);
74 usb_autopm_put_interface(intf);
79 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
82 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
83 const char *buf, size_t count)
85 struct usb_port *port_dev = to_usb_port(dev);
86 struct usb_device *hdev = to_usb_device(dev->parent->parent);
87 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
88 struct usb_interface *intf = to_usb_interface(hub->intfdev);
89 int port1 = port_dev->portnum;
93 rc = kstrtobool(buf, &disabled);
97 rc = usb_autopm_get_interface(intf);
101 usb_lock_device(hdev);
102 if (hub->disconnected) {
107 if (disabled && port_dev->child)
108 usb_disconnect(&port_dev->child);
110 rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
113 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
114 if (!port_dev->is_superspeed)
115 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
122 usb_unlock_device(hdev);
123 usb_autopm_put_interface(intf);
127 static DEVICE_ATTR_RW(disable);
129 static ssize_t location_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
132 struct usb_port *port_dev = to_usb_port(dev);
134 return sprintf(buf, "0x%08x\n", port_dev->location);
136 static DEVICE_ATTR_RO(location);
138 static ssize_t connect_type_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
141 struct usb_port *port_dev = to_usb_port(dev);
144 switch (port_dev->connect_type) {
145 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
148 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
149 result = "hardwired";
151 case USB_PORT_NOT_USED:
159 return sprintf(buf, "%s\n", result);
161 static DEVICE_ATTR_RO(connect_type);
163 static ssize_t over_current_count_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct usb_port *port_dev = to_usb_port(dev);
168 return sprintf(buf, "%u\n", port_dev->over_current_count);
170 static DEVICE_ATTR_RO(over_current_count);
172 static ssize_t quirks_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct usb_port *port_dev = to_usb_port(dev);
177 return sprintf(buf, "%08x\n", port_dev->quirks);
180 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
183 struct usb_port *port_dev = to_usb_port(dev);
186 if (kstrtou32(buf, 16, &value))
189 port_dev->quirks = value;
192 static DEVICE_ATTR_RW(quirks);
194 static ssize_t usb3_lpm_permit_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct usb_port *port_dev = to_usb_port(dev);
200 if (port_dev->usb3_lpm_u1_permit) {
201 if (port_dev->usb3_lpm_u2_permit)
206 if (port_dev->usb3_lpm_u2_permit)
212 return sprintf(buf, "%s\n", p);
215 static ssize_t usb3_lpm_permit_store(struct device *dev,
216 struct device_attribute *attr,
217 const char *buf, size_t count)
219 struct usb_port *port_dev = to_usb_port(dev);
220 struct usb_device *udev = port_dev->child;
223 if (!strncmp(buf, "u1_u2", 5)) {
224 port_dev->usb3_lpm_u1_permit = 1;
225 port_dev->usb3_lpm_u2_permit = 1;
227 } else if (!strncmp(buf, "u1", 2)) {
228 port_dev->usb3_lpm_u1_permit = 1;
229 port_dev->usb3_lpm_u2_permit = 0;
231 } else if (!strncmp(buf, "u2", 2)) {
232 port_dev->usb3_lpm_u1_permit = 0;
233 port_dev->usb3_lpm_u2_permit = 1;
235 } else if (!strncmp(buf, "0", 1)) {
236 port_dev->usb3_lpm_u1_permit = 0;
237 port_dev->usb3_lpm_u2_permit = 0;
241 /* If device is connected to the port, disable or enable lpm
242 * to make new u1 u2 setting take effect immediately.
245 hcd = bus_to_hcd(udev->bus);
248 usb_lock_device(udev);
249 mutex_lock(hcd->bandwidth_mutex);
250 if (!usb_disable_lpm(udev))
251 usb_enable_lpm(udev);
252 mutex_unlock(hcd->bandwidth_mutex);
253 usb_unlock_device(udev);
258 static DEVICE_ATTR_RW(usb3_lpm_permit);
260 static struct attribute *port_dev_attrs[] = {
261 &dev_attr_connect_type.attr,
262 &dev_attr_location.attr,
263 &dev_attr_quirks.attr,
264 &dev_attr_over_current_count.attr,
265 &dev_attr_disable.attr,
266 &dev_attr_early_stop.attr,
270 static const struct attribute_group port_dev_attr_grp = {
271 .attrs = port_dev_attrs,
274 static const struct attribute_group *port_dev_group[] = {
279 static struct attribute *port_dev_usb3_attrs[] = {
280 &dev_attr_usb3_lpm_permit.attr,
284 static const struct attribute_group port_dev_usb3_attr_grp = {
285 .attrs = port_dev_usb3_attrs,
288 static const struct attribute_group *port_dev_usb3_group[] = {
290 &port_dev_usb3_attr_grp,
294 static void usb_port_device_release(struct device *dev)
296 struct usb_port *port_dev = to_usb_port(dev);
298 kfree(port_dev->req);
303 static int usb_port_runtime_resume(struct device *dev)
305 struct usb_port *port_dev = to_usb_port(dev);
306 struct usb_device *hdev = to_usb_device(dev->parent->parent);
307 struct usb_interface *intf = to_usb_interface(dev->parent);
308 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
309 struct usb_device *udev = port_dev->child;
310 struct usb_port *peer = port_dev->peer;
311 int port1 = port_dev->portnum;
317 set_bit(port1, hub->power_bits);
322 * Power on our usb3 peer before this usb2 port to prevent a usb3
323 * device from degrading to its usb2 connection
325 if (!port_dev->is_superspeed && peer)
326 pm_runtime_get_sync(&peer->dev);
328 retval = usb_autopm_get_interface(intf);
332 retval = usb_hub_set_port_power(hdev, hub, port1, true);
333 msleep(hub_power_on_good_delay(hub));
334 if (udev && !retval) {
336 * Our preference is to simply wait for the port to reconnect,
337 * as that is the lowest latency method to restart the port.
338 * However, there are cases where toggling port power results in
339 * the host port and the device port getting out of sync causing
340 * a link training live lock. Upon timeout, flag the port as
341 * needing warm reset recovery (to be performed later by
342 * usb_port_resume() as requested via usb_wakeup_notification())
344 if (hub_port_debounce_be_connected(hub, port1) < 0) {
345 dev_dbg(&port_dev->dev, "reconnect timeout\n");
346 if (hub_is_superspeed(hdev))
347 set_bit(port1, hub->warm_reset_bits);
350 /* Force the child awake to revalidate after the power loss. */
351 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
352 pm_runtime_get_noresume(&port_dev->dev);
353 pm_request_resume(&udev->dev);
357 usb_autopm_put_interface(intf);
362 static int usb_port_runtime_suspend(struct device *dev)
364 struct usb_port *port_dev = to_usb_port(dev);
365 struct usb_device *hdev = to_usb_device(dev->parent->parent);
366 struct usb_interface *intf = to_usb_interface(dev->parent);
367 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
368 struct usb_port *peer = port_dev->peer;
369 int port1 = port_dev->portnum;
377 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
381 if (usb_port_block_power_off)
384 retval = usb_autopm_get_interface(intf);
388 retval = usb_hub_set_port_power(hdev, hub, port1, false);
389 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
390 if (!port_dev->is_superspeed)
391 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
392 usb_autopm_put_interface(intf);
395 * Our peer usb3 port may now be able to suspend, so
396 * asynchronously queue a suspend request to observe that this
397 * usb2 port is now off.
399 if (!port_dev->is_superspeed && peer)
400 pm_runtime_put(&peer->dev);
406 static void usb_port_shutdown(struct device *dev)
408 struct usb_port *port_dev = to_usb_port(dev);
411 usb_disable_usb2_hardware_lpm(port_dev->child);
414 static const struct dev_pm_ops usb_port_pm_ops = {
416 .runtime_suspend = usb_port_runtime_suspend,
417 .runtime_resume = usb_port_runtime_resume,
421 struct device_type usb_port_device_type = {
423 .release = usb_port_device_release,
424 .pm = &usb_port_pm_ops,
427 static struct device_driver usb_port_driver = {
429 .owner = THIS_MODULE,
430 .shutdown = usb_port_shutdown,
433 static int link_peers(struct usb_port *left, struct usb_port *right)
435 struct usb_port *ss_port, *hs_port;
438 if (left->peer == right && right->peer == left)
441 if (left->peer || right->peer) {
442 struct usb_port *lpeer = left->peer;
443 struct usb_port *rpeer = right->peer;
446 if (left->location && left->location == right->location)
451 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
452 dev_name(&left->dev), dev_name(&right->dev), method,
453 dev_name(&left->dev),
454 lpeer ? dev_name(&lpeer->dev) : "none",
455 dev_name(&right->dev),
456 rpeer ? dev_name(&rpeer->dev) : "none");
460 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
463 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
465 sysfs_remove_link(&left->dev.kobj, "peer");
470 * We need to wake the HiSpeed port to make sure we don't race
471 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
472 * may miss a suspend event for the SuperSpeed port.
474 if (left->is_superspeed) {
476 WARN_ON(right->is_superspeed);
480 WARN_ON(!right->is_superspeed);
483 pm_runtime_get_sync(&hs_port->dev);
489 * The SuperSpeed reference is dropped when the HiSpeed port in
490 * this relationship suspends, i.e. when it is safe to allow a
491 * SuperSpeed connection to drop since there is no risk of a
492 * device degrading to its powered-off HiSpeed connection.
494 * Also, drop the HiSpeed ref taken above.
496 pm_runtime_get_sync(&ss_port->dev);
497 pm_runtime_put(&hs_port->dev);
502 static void link_peers_report(struct usb_port *left, struct usb_port *right)
506 rc = link_peers(left, right);
508 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
510 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
511 dev_name(&right->dev), rc);
512 pr_warn_once("usb: port power management may be unreliable\n");
513 usb_port_block_power_off = 1;
517 static void unlink_peers(struct usb_port *left, struct usb_port *right)
519 struct usb_port *ss_port, *hs_port;
521 WARN(right->peer != left || left->peer != right,
522 "%s and %s are not peers?\n",
523 dev_name(&left->dev), dev_name(&right->dev));
526 * We wake the HiSpeed port to make sure we don't race its
527 * usb_port_runtime_resume() event which takes a SuperSpeed ref
528 * when ->peer is !NULL.
530 if (left->is_superspeed) {
538 pm_runtime_get_sync(&hs_port->dev);
540 sysfs_remove_link(&left->dev.kobj, "peer");
542 sysfs_remove_link(&right->dev.kobj, "peer");
545 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
546 pm_runtime_put(&ss_port->dev);
548 /* Drop the ref taken above */
549 pm_runtime_put(&hs_port->dev);
553 * For each usb hub device in the system check to see if it is in the
554 * peer domain of the given port_dev, and if it is check to see if it
555 * has a port that matches the given port by location
557 static int match_location(struct usb_device *peer_hdev, void *p)
560 struct usb_hcd *hcd, *peer_hcd;
561 struct usb_port *port_dev = p, *peer;
562 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
563 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
568 hcd = bus_to_hcd(hdev->bus);
569 peer_hcd = bus_to_hcd(peer_hdev->bus);
570 /* peer_hcd is provisional until we verify it against the known peer */
571 if (peer_hcd != hcd->shared_hcd)
574 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
575 peer = peer_hub->ports[port1 - 1];
576 if (peer && peer->location == port_dev->location) {
577 link_peers_report(port_dev, peer);
586 * Find the peer port either via explicit platform firmware "location"
587 * data, the peer hcd for root hubs, or the upstream peer relationship
588 * for all other hubs.
590 static void find_and_link_peer(struct usb_hub *hub, int port1)
592 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
593 struct usb_device *hdev = hub->hdev;
594 struct usb_device *peer_hdev;
595 struct usb_hub *peer_hub;
598 * If location data is available then we can only peer this port
599 * by a location match, not the default peer (lest we create a
600 * situation where we need to go back and undo a default peering
601 * when the port is later peered by location data)
603 if (port_dev->location) {
604 /* we link the peer in match_location() if found */
605 usb_for_each_dev(port_dev, match_location);
607 } else if (!hdev->parent) {
608 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
609 struct usb_hcd *peer_hcd = hcd->shared_hcd;
614 peer_hdev = peer_hcd->self.root_hub;
616 struct usb_port *upstream;
617 struct usb_device *parent = hdev->parent;
618 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
623 upstream = parent_hub->ports[hdev->portnum - 1];
624 if (!upstream || !upstream->peer)
627 peer_hdev = upstream->peer->child;
630 peer_hub = usb_hub_to_struct_hub(peer_hdev);
631 if (!peer_hub || port1 > peer_hdev->maxchild)
635 * we found a valid default peer, last check is to make sure it
636 * does not have location data
638 peer = peer_hub->ports[port1 - 1];
639 if (peer && peer->location == 0)
640 link_peers_report(port_dev, peer);
643 static int connector_bind(struct device *dev, struct device *connector, void *data)
647 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
651 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
653 sysfs_remove_link(&dev->kobj, "connector");
658 static void connector_unbind(struct device *dev, struct device *connector, void *data)
660 sysfs_remove_link(&connector->kobj, dev_name(dev));
661 sysfs_remove_link(&dev->kobj, "connector");
664 static const struct component_ops connector_ops = {
665 .bind = connector_bind,
666 .unbind = connector_unbind,
669 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
671 struct usb_port *port_dev;
672 struct usb_device *hdev = hub->hdev;
675 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
679 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
680 if (!port_dev->req) {
685 hub->ports[port1 - 1] = port_dev;
686 port_dev->portnum = port1;
687 set_bit(port1, hub->power_bits);
688 port_dev->dev.parent = hub->intfdev;
689 if (hub_is_superspeed(hdev)) {
690 port_dev->usb3_lpm_u1_permit = 1;
691 port_dev->usb3_lpm_u2_permit = 1;
692 port_dev->dev.groups = port_dev_usb3_group;
694 port_dev->dev.groups = port_dev_group;
695 port_dev->dev.type = &usb_port_device_type;
696 port_dev->dev.driver = &usb_port_driver;
697 if (hub_is_superspeed(hub->hdev))
698 port_dev->is_superspeed = 1;
699 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
701 mutex_init(&port_dev->status_lock);
702 retval = device_register(&port_dev->dev);
704 put_device(&port_dev->dev);
708 /* Set default policy of port-poweroff disabled. */
709 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
710 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
712 device_unregister(&port_dev->dev);
716 retval = component_add(&port_dev->dev, &connector_ops);
718 dev_warn(&port_dev->dev, "failed to add component\n");
719 device_unregister(&port_dev->dev);
723 find_and_link_peer(hub, port1);
726 * Enable runtime pm and hold a refernce that hub_configure()
727 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
728 * and the hub has been fully registered (hdev->maxchild set).
730 pm_runtime_set_active(&port_dev->dev);
731 pm_runtime_get_noresume(&port_dev->dev);
732 pm_runtime_enable(&port_dev->dev);
733 device_enable_async_suspend(&port_dev->dev);
736 * Keep hidden the ability to enable port-poweroff if the hub
737 * does not support power switching.
739 if (!hub_is_port_power_switchable(hub))
742 /* Attempt to let userspace take over the policy. */
743 retval = dev_pm_qos_expose_flags(&port_dev->dev,
744 PM_QOS_FLAG_NO_POWER_OFF);
746 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
750 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
751 retval = dev_pm_qos_remove_request(port_dev->req);
753 kfree(port_dev->req);
754 port_dev->req = NULL;
759 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
761 struct usb_port *port_dev = hub->ports[port1 - 1];
762 struct usb_port *peer;
764 peer = port_dev->peer;
766 unlink_peers(port_dev, peer);
767 component_del(&port_dev->dev, &connector_ops);
768 device_unregister(&port_dev->dev);