1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
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;
58 struct kernfs_node *kn;
61 rc = usb_autopm_get_interface(intf);
66 * Prevent deadlock if another process is concurrently
67 * trying to unregister hdev.
69 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
74 usb_lock_device(hdev);
75 if (hub->disconnected) {
80 usb_hub_port_status(hub, port1, &portstatus, &unused);
81 disabled = !usb_port_is_power_on(hub, portstatus);
84 usb_unlock_device(hdev);
85 sysfs_unbreak_active_protection(kn);
87 usb_autopm_put_interface(intf);
94 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
97 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
98 const char *buf, size_t count)
100 struct usb_port *port_dev = to_usb_port(dev);
101 struct usb_device *hdev = to_usb_device(dev->parent->parent);
102 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
103 struct usb_interface *intf = to_usb_interface(hub->intfdev);
104 int port1 = port_dev->portnum;
107 struct kernfs_node *kn;
109 rc = kstrtobool(buf, &disabled);
114 rc = usb_autopm_get_interface(intf);
119 * Prevent deadlock if another process is concurrently
120 * trying to unregister hdev.
122 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
127 usb_lock_device(hdev);
128 if (hub->disconnected) {
133 if (disabled && port_dev->child)
134 usb_disconnect(&port_dev->child);
136 rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
139 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
140 if (!port_dev->is_superspeed)
141 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
148 usb_unlock_device(hdev);
149 sysfs_unbreak_active_protection(kn);
151 usb_autopm_put_interface(intf);
157 static DEVICE_ATTR_RW(disable);
159 static ssize_t location_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct usb_port *port_dev = to_usb_port(dev);
164 return sprintf(buf, "0x%08x\n", port_dev->location);
166 static DEVICE_ATTR_RO(location);
168 static ssize_t connect_type_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct usb_port *port_dev = to_usb_port(dev);
174 switch (port_dev->connect_type) {
175 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
178 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
179 result = "hardwired";
181 case USB_PORT_NOT_USED:
189 return sprintf(buf, "%s\n", result);
191 static DEVICE_ATTR_RO(connect_type);
193 static ssize_t state_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct usb_port *port_dev = to_usb_port(dev);
197 enum usb_device_state state = READ_ONCE(port_dev->state);
199 return sysfs_emit(buf, "%s\n", usb_state_string(state));
201 static DEVICE_ATTR_RO(state);
203 static ssize_t over_current_count_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct usb_port *port_dev = to_usb_port(dev);
208 return sprintf(buf, "%u\n", port_dev->over_current_count);
210 static DEVICE_ATTR_RO(over_current_count);
212 static ssize_t quirks_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
215 struct usb_port *port_dev = to_usb_port(dev);
217 return sprintf(buf, "%08x\n", port_dev->quirks);
220 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
221 const char *buf, size_t count)
223 struct usb_port *port_dev = to_usb_port(dev);
226 if (kstrtou32(buf, 16, &value))
229 port_dev->quirks = value;
232 static DEVICE_ATTR_RW(quirks);
234 static ssize_t usb3_lpm_permit_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct usb_port *port_dev = to_usb_port(dev);
240 if (port_dev->usb3_lpm_u1_permit) {
241 if (port_dev->usb3_lpm_u2_permit)
246 if (port_dev->usb3_lpm_u2_permit)
252 return sprintf(buf, "%s\n", p);
255 static ssize_t usb3_lpm_permit_store(struct device *dev,
256 struct device_attribute *attr,
257 const char *buf, size_t count)
259 struct usb_port *port_dev = to_usb_port(dev);
260 struct usb_device *udev = port_dev->child;
263 if (!strncmp(buf, "u1_u2", 5)) {
264 port_dev->usb3_lpm_u1_permit = 1;
265 port_dev->usb3_lpm_u2_permit = 1;
267 } else if (!strncmp(buf, "u1", 2)) {
268 port_dev->usb3_lpm_u1_permit = 1;
269 port_dev->usb3_lpm_u2_permit = 0;
271 } else if (!strncmp(buf, "u2", 2)) {
272 port_dev->usb3_lpm_u1_permit = 0;
273 port_dev->usb3_lpm_u2_permit = 1;
275 } else if (!strncmp(buf, "0", 1)) {
276 port_dev->usb3_lpm_u1_permit = 0;
277 port_dev->usb3_lpm_u2_permit = 0;
281 /* If device is connected to the port, disable or enable lpm
282 * to make new u1 u2 setting take effect immediately.
285 hcd = bus_to_hcd(udev->bus);
288 usb_lock_device(udev);
289 mutex_lock(hcd->bandwidth_mutex);
290 if (!usb_disable_lpm(udev))
291 usb_enable_lpm(udev);
292 mutex_unlock(hcd->bandwidth_mutex);
293 usb_unlock_device(udev);
298 static DEVICE_ATTR_RW(usb3_lpm_permit);
300 static struct attribute *port_dev_attrs[] = {
301 &dev_attr_connect_type.attr,
302 &dev_attr_state.attr,
303 &dev_attr_location.attr,
304 &dev_attr_quirks.attr,
305 &dev_attr_over_current_count.attr,
306 &dev_attr_disable.attr,
307 &dev_attr_early_stop.attr,
311 static const struct attribute_group port_dev_attr_grp = {
312 .attrs = port_dev_attrs,
315 static const struct attribute_group *port_dev_group[] = {
320 static struct attribute *port_dev_usb3_attrs[] = {
321 &dev_attr_usb3_lpm_permit.attr,
325 static const struct attribute_group port_dev_usb3_attr_grp = {
326 .attrs = port_dev_usb3_attrs,
329 static const struct attribute_group *port_dev_usb3_group[] = {
331 &port_dev_usb3_attr_grp,
335 static void usb_port_device_release(struct device *dev)
337 struct usb_port *port_dev = to_usb_port(dev);
339 kfree(port_dev->req);
344 static int usb_port_runtime_resume(struct device *dev)
346 struct usb_port *port_dev = to_usb_port(dev);
347 struct usb_device *hdev = to_usb_device(dev->parent->parent);
348 struct usb_interface *intf = to_usb_interface(dev->parent);
349 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
350 struct usb_device *udev = port_dev->child;
351 struct usb_port *peer = port_dev->peer;
352 int port1 = port_dev->portnum;
358 set_bit(port1, hub->power_bits);
363 * Power on our usb3 peer before this usb2 port to prevent a usb3
364 * device from degrading to its usb2 connection
366 if (!port_dev->is_superspeed && peer)
367 pm_runtime_get_sync(&peer->dev);
369 retval = usb_autopm_get_interface(intf);
373 retval = usb_hub_set_port_power(hdev, hub, port1, true);
374 msleep(hub_power_on_good_delay(hub));
375 if (udev && !retval) {
377 * Our preference is to simply wait for the port to reconnect,
378 * as that is the lowest latency method to restart the port.
379 * However, there are cases where toggling port power results in
380 * the host port and the device port getting out of sync causing
381 * a link training live lock. Upon timeout, flag the port as
382 * needing warm reset recovery (to be performed later by
383 * usb_port_resume() as requested via usb_wakeup_notification())
385 if (hub_port_debounce_be_connected(hub, port1) < 0) {
386 dev_dbg(&port_dev->dev, "reconnect timeout\n");
387 if (hub_is_superspeed(hdev))
388 set_bit(port1, hub->warm_reset_bits);
391 /* Force the child awake to revalidate after the power loss. */
392 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
393 pm_runtime_get_noresume(&port_dev->dev);
394 pm_request_resume(&udev->dev);
398 usb_autopm_put_interface(intf);
403 static int usb_port_runtime_suspend(struct device *dev)
405 struct usb_port *port_dev = to_usb_port(dev);
406 struct usb_device *hdev = to_usb_device(dev->parent->parent);
407 struct usb_interface *intf = to_usb_interface(dev->parent);
408 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
409 struct usb_port *peer = port_dev->peer;
410 int port1 = port_dev->portnum;
418 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
422 if (usb_port_block_power_off)
425 retval = usb_autopm_get_interface(intf);
429 retval = usb_hub_set_port_power(hdev, hub, port1, false);
430 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
431 if (!port_dev->is_superspeed)
432 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
433 usb_autopm_put_interface(intf);
436 * Our peer usb3 port may now be able to suspend, so
437 * asynchronously queue a suspend request to observe that this
438 * usb2 port is now off.
440 if (!port_dev->is_superspeed && peer)
441 pm_runtime_put(&peer->dev);
447 static void usb_port_shutdown(struct device *dev)
449 struct usb_port *port_dev = to_usb_port(dev);
452 usb_disable_usb2_hardware_lpm(port_dev->child);
455 static const struct dev_pm_ops usb_port_pm_ops = {
457 .runtime_suspend = usb_port_runtime_suspend,
458 .runtime_resume = usb_port_runtime_resume,
462 struct device_type usb_port_device_type = {
464 .release = usb_port_device_release,
465 .pm = &usb_port_pm_ops,
468 static struct device_driver usb_port_driver = {
470 .owner = THIS_MODULE,
471 .shutdown = usb_port_shutdown,
474 static int link_peers(struct usb_port *left, struct usb_port *right)
476 struct usb_port *ss_port, *hs_port;
479 if (left->peer == right && right->peer == left)
482 if (left->peer || right->peer) {
483 struct usb_port *lpeer = left->peer;
484 struct usb_port *rpeer = right->peer;
487 if (left->location && left->location == right->location)
492 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
493 dev_name(&left->dev), dev_name(&right->dev), method,
494 dev_name(&left->dev),
495 lpeer ? dev_name(&lpeer->dev) : "none",
496 dev_name(&right->dev),
497 rpeer ? dev_name(&rpeer->dev) : "none");
501 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
504 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
506 sysfs_remove_link(&left->dev.kobj, "peer");
511 * We need to wake the HiSpeed port to make sure we don't race
512 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
513 * may miss a suspend event for the SuperSpeed port.
515 if (left->is_superspeed) {
517 WARN_ON(right->is_superspeed);
521 WARN_ON(!right->is_superspeed);
524 pm_runtime_get_sync(&hs_port->dev);
530 * The SuperSpeed reference is dropped when the HiSpeed port in
531 * this relationship suspends, i.e. when it is safe to allow a
532 * SuperSpeed connection to drop since there is no risk of a
533 * device degrading to its powered-off HiSpeed connection.
535 * Also, drop the HiSpeed ref taken above.
537 pm_runtime_get_sync(&ss_port->dev);
538 pm_runtime_put(&hs_port->dev);
543 static void link_peers_report(struct usb_port *left, struct usb_port *right)
547 rc = link_peers(left, right);
549 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
551 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
552 dev_name(&right->dev), rc);
553 pr_warn_once("usb: port power management may be unreliable\n");
554 usb_port_block_power_off = 1;
558 static void unlink_peers(struct usb_port *left, struct usb_port *right)
560 struct usb_port *ss_port, *hs_port;
562 WARN(right->peer != left || left->peer != right,
563 "%s and %s are not peers?\n",
564 dev_name(&left->dev), dev_name(&right->dev));
567 * We wake the HiSpeed port to make sure we don't race its
568 * usb_port_runtime_resume() event which takes a SuperSpeed ref
569 * when ->peer is !NULL.
571 if (left->is_superspeed) {
579 pm_runtime_get_sync(&hs_port->dev);
581 sysfs_remove_link(&left->dev.kobj, "peer");
583 sysfs_remove_link(&right->dev.kobj, "peer");
586 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
587 pm_runtime_put(&ss_port->dev);
589 /* Drop the ref taken above */
590 pm_runtime_put(&hs_port->dev);
594 * For each usb hub device in the system check to see if it is in the
595 * peer domain of the given port_dev, and if it is check to see if it
596 * has a port that matches the given port by location
598 static int match_location(struct usb_device *peer_hdev, void *p)
601 struct usb_hcd *hcd, *peer_hcd;
602 struct usb_port *port_dev = p, *peer;
603 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
604 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
606 if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
609 hcd = bus_to_hcd(hdev->bus);
610 peer_hcd = bus_to_hcd(peer_hdev->bus);
611 /* peer_hcd is provisional until we verify it against the known peer */
612 if (peer_hcd != hcd->shared_hcd)
615 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
616 peer = peer_hub->ports[port1 - 1];
617 if (peer && peer->connect_type != USB_PORT_NOT_USED &&
618 peer->location == port_dev->location) {
619 link_peers_report(port_dev, peer);
628 * Find the peer port either via explicit platform firmware "location"
629 * data, the peer hcd for root hubs, or the upstream peer relationship
630 * for all other hubs.
632 static void find_and_link_peer(struct usb_hub *hub, int port1)
634 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
635 struct usb_device *hdev = hub->hdev;
636 struct usb_device *peer_hdev;
637 struct usb_hub *peer_hub;
640 * If location data is available then we can only peer this port
641 * by a location match, not the default peer (lest we create a
642 * situation where we need to go back and undo a default peering
643 * when the port is later peered by location data)
645 if (port_dev->location) {
646 /* we link the peer in match_location() if found */
647 usb_for_each_dev(port_dev, match_location);
649 } else if (!hdev->parent) {
650 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
651 struct usb_hcd *peer_hcd = hcd->shared_hcd;
656 peer_hdev = peer_hcd->self.root_hub;
658 struct usb_port *upstream;
659 struct usb_device *parent = hdev->parent;
660 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
665 upstream = parent_hub->ports[hdev->portnum - 1];
666 if (!upstream || !upstream->peer)
669 peer_hdev = upstream->peer->child;
672 peer_hub = usb_hub_to_struct_hub(peer_hdev);
673 if (!peer_hub || port1 > peer_hdev->maxchild)
677 * we found a valid default peer, last check is to make sure it
678 * does not have location data
680 peer = peer_hub->ports[port1 - 1];
681 if (peer && peer->location == 0)
682 link_peers_report(port_dev, peer);
685 static int connector_bind(struct device *dev, struct device *connector, void *data)
687 struct usb_port *port_dev = to_usb_port(dev);
690 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
694 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
696 sysfs_remove_link(&dev->kobj, "connector");
700 port_dev->connector = data;
703 * If there is already USB device connected to the port, letting the
704 * Type-C connector know about it immediately.
707 typec_attach(port_dev->connector, &port_dev->child->dev);
712 static void connector_unbind(struct device *dev, struct device *connector, void *data)
714 struct usb_port *port_dev = to_usb_port(dev);
716 sysfs_remove_link(&connector->kobj, dev_name(dev));
717 sysfs_remove_link(&dev->kobj, "connector");
718 port_dev->connector = NULL;
721 static const struct component_ops connector_ops = {
722 .bind = connector_bind,
723 .unbind = connector_unbind,
726 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
728 struct usb_port *port_dev;
729 struct usb_device *hdev = hub->hdev;
732 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
736 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
737 if (!port_dev->req) {
742 hub->ports[port1 - 1] = port_dev;
743 port_dev->portnum = port1;
744 set_bit(port1, hub->power_bits);
745 port_dev->dev.parent = hub->intfdev;
746 if (hub_is_superspeed(hdev)) {
747 port_dev->is_superspeed = 1;
748 port_dev->usb3_lpm_u1_permit = 1;
749 port_dev->usb3_lpm_u2_permit = 1;
750 port_dev->dev.groups = port_dev_usb3_group;
752 port_dev->dev.groups = port_dev_group;
753 port_dev->dev.type = &usb_port_device_type;
754 port_dev->dev.driver = &usb_port_driver;
755 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
757 mutex_init(&port_dev->status_lock);
758 retval = device_register(&port_dev->dev);
760 put_device(&port_dev->dev);
764 port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
765 if (!port_dev->state_kn) {
766 dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
771 /* Set default policy of port-poweroff disabled. */
772 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
773 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
778 retval = component_add(&port_dev->dev, &connector_ops);
780 dev_warn(&port_dev->dev, "failed to add component\n");
784 find_and_link_peer(hub, port1);
787 * Enable runtime pm and hold a refernce that hub_configure()
788 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
789 * and the hub has been fully registered (hdev->maxchild set).
791 pm_runtime_set_active(&port_dev->dev);
792 pm_runtime_get_noresume(&port_dev->dev);
793 pm_runtime_enable(&port_dev->dev);
794 device_enable_async_suspend(&port_dev->dev);
797 * Keep hidden the ability to enable port-poweroff if the hub
798 * does not support power switching.
800 if (!hub_is_port_power_switchable(hub))
803 /* Attempt to let userspace take over the policy. */
804 retval = dev_pm_qos_expose_flags(&port_dev->dev,
805 PM_QOS_FLAG_NO_POWER_OFF);
807 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
811 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
812 retval = dev_pm_qos_remove_request(port_dev->req);
814 kfree(port_dev->req);
815 port_dev->req = NULL;
820 sysfs_put(port_dev->state_kn);
822 device_unregister(&port_dev->dev);
827 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
829 struct usb_port *port_dev = hub->ports[port1 - 1];
830 struct usb_port *peer;
832 peer = port_dev->peer;
834 unlink_peers(port_dev, peer);
835 component_del(&port_dev->dev, &connector_ops);
836 sysfs_put(port_dev->state_kn);
837 device_unregister(&port_dev->dev);