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/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>
20 static int usb_port_block_power_off;
22 static const struct attribute_group *port_dev_group[];
24 static ssize_t early_stop_show(struct device *dev,
25 struct device_attribute *attr, char *buf)
27 struct usb_port *port_dev = to_usb_port(dev);
29 return sysfs_emit(buf, "%s\n", str_yes_no(port_dev->early_stop));
32 static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
33 const char *buf, size_t count)
35 struct usb_port *port_dev = to_usb_port(dev);
38 if (kstrtobool(buf, &value))
42 port_dev->early_stop = 1;
44 port_dev->early_stop = 0;
48 static DEVICE_ATTR_RW(early_stop);
50 static ssize_t disable_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
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;
61 struct kernfs_node *kn;
66 rc = usb_autopm_get_interface(intf);
71 * Prevent deadlock if another process is concurrently
72 * trying to unregister hdev.
74 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
79 usb_lock_device(hdev);
80 if (hub->disconnected) {
85 usb_hub_port_status(hub, port1, &portstatus, &unused);
86 disabled = !usb_port_is_power_on(hub, portstatus);
89 usb_unlock_device(hdev);
90 sysfs_unbreak_active_protection(kn);
92 usb_autopm_put_interface(intf);
99 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
102 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
103 const char *buf, size_t count)
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;
112 struct kernfs_node *kn;
116 rc = kstrtobool(buf, &disabled);
121 rc = usb_autopm_get_interface(intf);
126 * Prevent deadlock if another process is concurrently
127 * trying to unregister hdev.
129 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
134 usb_lock_device(hdev);
135 if (hub->disconnected) {
140 if (disabled && port_dev->child)
141 usb_disconnect(&port_dev->child);
143 rc = usb_hub_set_port_power(hdev, hub, port1, !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);
155 usb_unlock_device(hdev);
156 sysfs_unbreak_active_protection(kn);
158 usb_autopm_put_interface(intf);
164 static DEVICE_ATTR_RW(disable);
166 static ssize_t location_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 struct usb_port *port_dev = to_usb_port(dev);
171 return sysfs_emit(buf, "0x%08x\n", port_dev->location);
173 static DEVICE_ATTR_RO(location);
175 static ssize_t connect_type_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 struct usb_port *port_dev = to_usb_port(dev);
181 switch (port_dev->connect_type) {
182 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
185 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
186 result = "hardwired";
188 case USB_PORT_NOT_USED:
196 return sysfs_emit(buf, "%s\n", result);
198 static DEVICE_ATTR_RO(connect_type);
200 static ssize_t state_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct usb_port *port_dev = to_usb_port(dev);
204 enum usb_device_state state = READ_ONCE(port_dev->state);
206 return sysfs_emit(buf, "%s\n", usb_state_string(state));
208 static DEVICE_ATTR_RO(state);
210 static ssize_t over_current_count_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
213 struct usb_port *port_dev = to_usb_port(dev);
215 return sysfs_emit(buf, "%u\n", port_dev->over_current_count);
217 static DEVICE_ATTR_RO(over_current_count);
219 static ssize_t quirks_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct usb_port *port_dev = to_usb_port(dev);
224 return sysfs_emit(buf, "%08x\n", port_dev->quirks);
227 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
228 const char *buf, size_t count)
230 struct usb_port *port_dev = to_usb_port(dev);
233 if (kstrtou32(buf, 16, &value))
236 port_dev->quirks = value;
239 static DEVICE_ATTR_RW(quirks);
241 static ssize_t usb3_lpm_permit_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 struct usb_port *port_dev = to_usb_port(dev);
247 if (port_dev->usb3_lpm_u1_permit) {
248 if (port_dev->usb3_lpm_u2_permit)
253 if (port_dev->usb3_lpm_u2_permit)
259 return sysfs_emit(buf, "%s\n", p);
262 static ssize_t usb3_lpm_permit_store(struct device *dev,
263 struct device_attribute *attr,
264 const char *buf, size_t count)
266 struct usb_port *port_dev = to_usb_port(dev);
267 struct usb_device *udev = port_dev->child;
270 if (!strncmp(buf, "u1_u2", 5)) {
271 port_dev->usb3_lpm_u1_permit = 1;
272 port_dev->usb3_lpm_u2_permit = 1;
274 } else if (!strncmp(buf, "u1", 2)) {
275 port_dev->usb3_lpm_u1_permit = 1;
276 port_dev->usb3_lpm_u2_permit = 0;
278 } else if (!strncmp(buf, "u2", 2)) {
279 port_dev->usb3_lpm_u1_permit = 0;
280 port_dev->usb3_lpm_u2_permit = 1;
282 } else if (!strncmp(buf, "0", 1)) {
283 port_dev->usb3_lpm_u1_permit = 0;
284 port_dev->usb3_lpm_u2_permit = 0;
288 /* If device is connected to the port, disable or enable lpm
289 * to make new u1 u2 setting take effect immediately.
292 hcd = bus_to_hcd(udev->bus);
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);
305 static DEVICE_ATTR_RW(usb3_lpm_permit);
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,
318 static const struct attribute_group port_dev_attr_grp = {
319 .attrs = port_dev_attrs,
322 static const struct attribute_group *port_dev_group[] = {
327 static struct attribute *port_dev_usb3_attrs[] = {
328 &dev_attr_usb3_lpm_permit.attr,
332 static const struct attribute_group port_dev_usb3_attr_grp = {
333 .attrs = port_dev_usb3_attrs,
336 static const struct attribute_group *port_dev_usb3_group[] = {
338 &port_dev_usb3_attr_grp,
342 static void usb_port_device_release(struct device *dev)
344 struct usb_port *port_dev = to_usb_port(dev);
346 kfree(port_dev->req);
351 static int usb_port_runtime_resume(struct device *dev)
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;
365 set_bit(port1, hub->power_bits);
370 * Power on our usb3 peer before this usb2 port to prevent a usb3
371 * device from degrading to its usb2 connection
373 if (!port_dev->is_superspeed && peer)
374 pm_runtime_get_sync(&peer->dev);
376 retval = usb_autopm_get_interface(intf);
380 retval = usb_hub_set_port_power(hdev, hub, port1, true);
381 msleep(hub_power_on_good_delay(hub));
382 if (udev && !retval) {
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())
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);
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);
405 usb_autopm_put_interface(intf);
410 static int usb_port_runtime_suspend(struct device *dev)
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;
425 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
429 if (usb_port_block_power_off)
432 retval = usb_autopm_get_interface(intf);
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);
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.
447 if (!port_dev->is_superspeed && peer)
448 pm_runtime_put(&peer->dev);
454 static void usb_port_shutdown(struct device *dev)
456 struct usb_port *port_dev = to_usb_port(dev);
457 struct usb_device *udev = port_dev->child;
459 if (udev && !udev->port_is_suspended) {
460 usb_disable_usb2_hardware_lpm(udev);
461 usb_unlocked_disable_lpm(udev);
465 static const struct dev_pm_ops usb_port_pm_ops = {
467 .runtime_suspend = usb_port_runtime_suspend,
468 .runtime_resume = usb_port_runtime_resume,
472 const struct device_type usb_port_device_type = {
474 .release = usb_port_device_release,
475 .pm = &usb_port_pm_ops,
478 static struct device_driver usb_port_driver = {
480 .owner = THIS_MODULE,
481 .shutdown = usb_port_shutdown,
484 static int link_peers(struct usb_port *left, struct usb_port *right)
486 struct usb_port *ss_port, *hs_port;
489 if (left->peer == right && right->peer == left)
492 if (left->peer || right->peer) {
493 struct usb_port *lpeer = left->peer;
494 struct usb_port *rpeer = right->peer;
497 if (left->location && left->location == right->location)
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");
511 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
514 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
516 sysfs_remove_link(&left->dev.kobj, "peer");
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.
525 if (left->is_superspeed) {
527 WARN_ON(right->is_superspeed);
531 WARN_ON(!right->is_superspeed);
534 pm_runtime_get_sync(&hs_port->dev);
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.
545 * Also, drop the HiSpeed ref taken above.
547 pm_runtime_get_sync(&ss_port->dev);
548 pm_runtime_put(&hs_port->dev);
553 static void link_peers_report(struct usb_port *left, struct usb_port *right)
557 rc = link_peers(left, right);
559 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
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;
568 static void unlink_peers(struct usb_port *left, struct usb_port *right)
570 struct usb_port *ss_port, *hs_port;
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));
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.
581 if (left->is_superspeed) {
589 pm_runtime_get_sync(&hs_port->dev);
591 sysfs_remove_link(&left->dev.kobj, "peer");
593 sysfs_remove_link(&right->dev.kobj, "peer");
596 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
597 pm_runtime_put(&ss_port->dev);
599 /* Drop the ref taken above */
600 pm_runtime_put(&hs_port->dev);
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
608 static int match_location(struct usb_device *peer_hdev, void *p)
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);
616 if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
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)
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);
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.
642 static void find_and_link_peer(struct usb_hub *hub, int port1)
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;
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)
655 if (port_dev->location) {
656 /* we link the peer in match_location() if found */
657 usb_for_each_dev(port_dev, match_location);
659 } else if (!hdev->parent) {
660 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
661 struct usb_hcd *peer_hcd = hcd->shared_hcd;
666 peer_hdev = peer_hcd->self.root_hub;
668 struct usb_port *upstream;
669 struct usb_device *parent = hdev->parent;
670 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
675 upstream = parent_hub->ports[hdev->portnum - 1];
676 if (!upstream || !upstream->peer)
679 peer_hdev = upstream->peer->child;
682 peer_hub = usb_hub_to_struct_hub(peer_hdev);
683 if (!peer_hub || port1 > peer_hdev->maxchild)
687 * we found a valid default peer, last check is to make sure it
688 * does not have location data
690 peer = peer_hub->ports[port1 - 1];
691 if (peer && peer->location == 0)
692 link_peers_report(port_dev, peer);
695 static int connector_bind(struct device *dev, struct device *connector, void *data)
697 struct usb_port *port_dev = to_usb_port(dev);
700 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
704 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
706 sysfs_remove_link(&dev->kobj, "connector");
710 port_dev->connector = data;
713 * If there is already USB device connected to the port, letting the
714 * Type-C connector know about it immediately.
717 typec_attach(port_dev->connector, &port_dev->child->dev);
722 static void connector_unbind(struct device *dev, struct device *connector, void *data)
724 struct usb_port *port_dev = to_usb_port(dev);
726 sysfs_remove_link(&connector->kobj, dev_name(dev));
727 sysfs_remove_link(&dev->kobj, "connector");
728 port_dev->connector = NULL;
731 static const struct component_ops connector_ops = {
732 .bind = connector_bind,
733 .unbind = connector_unbind,
736 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
738 struct usb_port *port_dev;
739 struct usb_device *hdev = hub->hdev;
742 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
746 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
747 if (!port_dev->req) {
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;
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),
768 mutex_init(&port_dev->status_lock);
769 retval = device_register(&port_dev->dev);
771 put_device(&port_dev->dev);
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");
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);
789 retval = component_add(&port_dev->dev, &connector_ops);
791 dev_warn(&port_dev->dev, "failed to add component\n");
795 find_and_link_peer(hub, port1);
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).
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);
808 * Keep hidden the ability to enable port-poweroff if the hub
809 * does not support power switching.
811 if (!hub_is_port_power_switchable(hub))
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);
818 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
822 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
823 retval = dev_pm_qos_remove_request(port_dev->req);
825 kfree(port_dev->req);
826 port_dev->req = NULL;
831 sysfs_put(port_dev->state_kn);
833 device_unregister(&port_dev->dev);
838 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
840 struct usb_port *port_dev = hub->ports[port1 - 1];
841 struct usb_port *peer;
843 peer = port_dev->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);