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>
14 #include <linux/usb/of.h>
18 static int usb_port_block_power_off;
20 static const struct attribute_group *port_dev_group[];
22 static ssize_t early_stop_show(struct device *dev,
23 struct device_attribute *attr, char *buf)
25 struct usb_port *port_dev = to_usb_port(dev);
27 return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no");
30 static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
31 const char *buf, size_t count)
33 struct usb_port *port_dev = to_usb_port(dev);
36 if (kstrtobool(buf, &value))
40 port_dev->early_stop = 1;
42 port_dev->early_stop = 0;
46 static DEVICE_ATTR_RW(early_stop);
48 static ssize_t disable_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
51 struct usb_port *port_dev = to_usb_port(dev);
52 struct usb_device *hdev = to_usb_device(dev->parent->parent);
53 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
54 struct usb_interface *intf = to_usb_interface(hub->intfdev);
55 int port1 = port_dev->portnum;
56 u16 portstatus, unused;
59 struct kernfs_node *kn;
62 rc = usb_autopm_get_interface(intf);
67 * Prevent deadlock if another process is concurrently
68 * trying to unregister hdev.
70 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
75 usb_lock_device(hdev);
76 if (hub->disconnected) {
81 usb_hub_port_status(hub, port1, &portstatus, &unused);
82 disabled = !usb_port_is_power_on(hub, portstatus);
85 usb_unlock_device(hdev);
86 sysfs_unbreak_active_protection(kn);
88 usb_autopm_put_interface(intf);
95 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
98 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
99 const char *buf, size_t count)
101 struct usb_port *port_dev = to_usb_port(dev);
102 struct usb_device *hdev = to_usb_device(dev->parent->parent);
103 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
104 struct usb_interface *intf = to_usb_interface(hub->intfdev);
105 int port1 = port_dev->portnum;
108 struct kernfs_node *kn;
110 rc = kstrtobool(buf, &disabled);
115 rc = usb_autopm_get_interface(intf);
120 * Prevent deadlock if another process is concurrently
121 * trying to unregister hdev.
123 kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
128 usb_lock_device(hdev);
129 if (hub->disconnected) {
134 if (disabled && port_dev->child)
135 usb_disconnect(&port_dev->child);
137 rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
140 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
141 if (!port_dev->is_superspeed)
142 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
149 usb_unlock_device(hdev);
150 sysfs_unbreak_active_protection(kn);
152 usb_autopm_put_interface(intf);
158 static DEVICE_ATTR_RW(disable);
160 static ssize_t location_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct usb_port *port_dev = to_usb_port(dev);
165 return sprintf(buf, "0x%08x\n", port_dev->location);
167 static DEVICE_ATTR_RO(location);
169 static ssize_t connect_type_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct usb_port *port_dev = to_usb_port(dev);
175 switch (port_dev->connect_type) {
176 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
179 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
180 result = "hardwired";
182 case USB_PORT_NOT_USED:
190 return sprintf(buf, "%s\n", result);
192 static DEVICE_ATTR_RO(connect_type);
194 static ssize_t state_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct usb_port *port_dev = to_usb_port(dev);
198 enum usb_device_state state = READ_ONCE(port_dev->state);
200 return sysfs_emit(buf, "%s\n", usb_state_string(state));
202 static DEVICE_ATTR_RO(state);
204 static ssize_t over_current_count_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
207 struct usb_port *port_dev = to_usb_port(dev);
209 return sprintf(buf, "%u\n", port_dev->over_current_count);
211 static DEVICE_ATTR_RO(over_current_count);
213 static ssize_t quirks_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct usb_port *port_dev = to_usb_port(dev);
218 return sprintf(buf, "%08x\n", port_dev->quirks);
221 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t count)
224 struct usb_port *port_dev = to_usb_port(dev);
227 if (kstrtou32(buf, 16, &value))
230 port_dev->quirks = value;
233 static DEVICE_ATTR_RW(quirks);
235 static ssize_t usb3_lpm_permit_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct usb_port *port_dev = to_usb_port(dev);
241 if (port_dev->usb3_lpm_u1_permit) {
242 if (port_dev->usb3_lpm_u2_permit)
247 if (port_dev->usb3_lpm_u2_permit)
253 return sprintf(buf, "%s\n", p);
256 static ssize_t usb3_lpm_permit_store(struct device *dev,
257 struct device_attribute *attr,
258 const char *buf, size_t count)
260 struct usb_port *port_dev = to_usb_port(dev);
261 struct usb_device *udev = port_dev->child;
264 if (!strncmp(buf, "u1_u2", 5)) {
265 port_dev->usb3_lpm_u1_permit = 1;
266 port_dev->usb3_lpm_u2_permit = 1;
268 } else if (!strncmp(buf, "u1", 2)) {
269 port_dev->usb3_lpm_u1_permit = 1;
270 port_dev->usb3_lpm_u2_permit = 0;
272 } else if (!strncmp(buf, "u2", 2)) {
273 port_dev->usb3_lpm_u1_permit = 0;
274 port_dev->usb3_lpm_u2_permit = 1;
276 } else if (!strncmp(buf, "0", 1)) {
277 port_dev->usb3_lpm_u1_permit = 0;
278 port_dev->usb3_lpm_u2_permit = 0;
282 /* If device is connected to the port, disable or enable lpm
283 * to make new u1 u2 setting take effect immediately.
286 hcd = bus_to_hcd(udev->bus);
289 usb_lock_device(udev);
290 mutex_lock(hcd->bandwidth_mutex);
291 if (!usb_disable_lpm(udev))
292 usb_enable_lpm(udev);
293 mutex_unlock(hcd->bandwidth_mutex);
294 usb_unlock_device(udev);
299 static DEVICE_ATTR_RW(usb3_lpm_permit);
301 static struct attribute *port_dev_attrs[] = {
302 &dev_attr_connect_type.attr,
303 &dev_attr_state.attr,
304 &dev_attr_location.attr,
305 &dev_attr_quirks.attr,
306 &dev_attr_over_current_count.attr,
307 &dev_attr_disable.attr,
308 &dev_attr_early_stop.attr,
312 static const struct attribute_group port_dev_attr_grp = {
313 .attrs = port_dev_attrs,
316 static const struct attribute_group *port_dev_group[] = {
321 static struct attribute *port_dev_usb3_attrs[] = {
322 &dev_attr_usb3_lpm_permit.attr,
326 static const struct attribute_group port_dev_usb3_attr_grp = {
327 .attrs = port_dev_usb3_attrs,
330 static const struct attribute_group *port_dev_usb3_group[] = {
332 &port_dev_usb3_attr_grp,
336 static void usb_port_device_release(struct device *dev)
338 struct usb_port *port_dev = to_usb_port(dev);
340 kfree(port_dev->req);
345 static int usb_port_runtime_resume(struct device *dev)
347 struct usb_port *port_dev = to_usb_port(dev);
348 struct usb_device *hdev = to_usb_device(dev->parent->parent);
349 struct usb_interface *intf = to_usb_interface(dev->parent);
350 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
351 struct usb_device *udev = port_dev->child;
352 struct usb_port *peer = port_dev->peer;
353 int port1 = port_dev->portnum;
359 set_bit(port1, hub->power_bits);
364 * Power on our usb3 peer before this usb2 port to prevent a usb3
365 * device from degrading to its usb2 connection
367 if (!port_dev->is_superspeed && peer)
368 pm_runtime_get_sync(&peer->dev);
370 retval = usb_autopm_get_interface(intf);
374 retval = usb_hub_set_port_power(hdev, hub, port1, true);
375 msleep(hub_power_on_good_delay(hub));
376 if (udev && !retval) {
378 * Our preference is to simply wait for the port to reconnect,
379 * as that is the lowest latency method to restart the port.
380 * However, there are cases where toggling port power results in
381 * the host port and the device port getting out of sync causing
382 * a link training live lock. Upon timeout, flag the port as
383 * needing warm reset recovery (to be performed later by
384 * usb_port_resume() as requested via usb_wakeup_notification())
386 if (hub_port_debounce_be_connected(hub, port1) < 0) {
387 dev_dbg(&port_dev->dev, "reconnect timeout\n");
388 if (hub_is_superspeed(hdev))
389 set_bit(port1, hub->warm_reset_bits);
392 /* Force the child awake to revalidate after the power loss. */
393 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
394 pm_runtime_get_noresume(&port_dev->dev);
395 pm_request_resume(&udev->dev);
399 usb_autopm_put_interface(intf);
404 static int usb_port_runtime_suspend(struct device *dev)
406 struct usb_port *port_dev = to_usb_port(dev);
407 struct usb_device *hdev = to_usb_device(dev->parent->parent);
408 struct usb_interface *intf = to_usb_interface(dev->parent);
409 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
410 struct usb_port *peer = port_dev->peer;
411 int port1 = port_dev->portnum;
419 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
423 if (usb_port_block_power_off)
426 retval = usb_autopm_get_interface(intf);
430 retval = usb_hub_set_port_power(hdev, hub, port1, false);
431 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
432 if (!port_dev->is_superspeed)
433 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
434 usb_autopm_put_interface(intf);
437 * Our peer usb3 port may now be able to suspend, so
438 * asynchronously queue a suspend request to observe that this
439 * usb2 port is now off.
441 if (!port_dev->is_superspeed && peer)
442 pm_runtime_put(&peer->dev);
448 static void usb_port_shutdown(struct device *dev)
450 struct usb_port *port_dev = to_usb_port(dev);
452 if (port_dev->child) {
453 usb_disable_usb2_hardware_lpm(port_dev->child);
454 usb_unlocked_disable_lpm(port_dev->child);
458 static const struct dev_pm_ops usb_port_pm_ops = {
460 .runtime_suspend = usb_port_runtime_suspend,
461 .runtime_resume = usb_port_runtime_resume,
465 const struct device_type usb_port_device_type = {
467 .release = usb_port_device_release,
468 .pm = &usb_port_pm_ops,
471 static struct device_driver usb_port_driver = {
473 .owner = THIS_MODULE,
474 .shutdown = usb_port_shutdown,
477 static int link_peers(struct usb_port *left, struct usb_port *right)
479 struct usb_port *ss_port, *hs_port;
482 if (left->peer == right && right->peer == left)
485 if (left->peer || right->peer) {
486 struct usb_port *lpeer = left->peer;
487 struct usb_port *rpeer = right->peer;
490 if (left->location && left->location == right->location)
495 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
496 dev_name(&left->dev), dev_name(&right->dev), method,
497 dev_name(&left->dev),
498 lpeer ? dev_name(&lpeer->dev) : "none",
499 dev_name(&right->dev),
500 rpeer ? dev_name(&rpeer->dev) : "none");
504 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
507 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
509 sysfs_remove_link(&left->dev.kobj, "peer");
514 * We need to wake the HiSpeed port to make sure we don't race
515 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
516 * may miss a suspend event for the SuperSpeed port.
518 if (left->is_superspeed) {
520 WARN_ON(right->is_superspeed);
524 WARN_ON(!right->is_superspeed);
527 pm_runtime_get_sync(&hs_port->dev);
533 * The SuperSpeed reference is dropped when the HiSpeed port in
534 * this relationship suspends, i.e. when it is safe to allow a
535 * SuperSpeed connection to drop since there is no risk of a
536 * device degrading to its powered-off HiSpeed connection.
538 * Also, drop the HiSpeed ref taken above.
540 pm_runtime_get_sync(&ss_port->dev);
541 pm_runtime_put(&hs_port->dev);
546 static void link_peers_report(struct usb_port *left, struct usb_port *right)
550 rc = link_peers(left, right);
552 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
554 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
555 dev_name(&right->dev), rc);
556 pr_warn_once("usb: port power management may be unreliable\n");
557 usb_port_block_power_off = 1;
561 static void unlink_peers(struct usb_port *left, struct usb_port *right)
563 struct usb_port *ss_port, *hs_port;
565 WARN(right->peer != left || left->peer != right,
566 "%s and %s are not peers?\n",
567 dev_name(&left->dev), dev_name(&right->dev));
570 * We wake the HiSpeed port to make sure we don't race its
571 * usb_port_runtime_resume() event which takes a SuperSpeed ref
572 * when ->peer is !NULL.
574 if (left->is_superspeed) {
582 pm_runtime_get_sync(&hs_port->dev);
584 sysfs_remove_link(&left->dev.kobj, "peer");
586 sysfs_remove_link(&right->dev.kobj, "peer");
589 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
590 pm_runtime_put(&ss_port->dev);
592 /* Drop the ref taken above */
593 pm_runtime_put(&hs_port->dev);
597 * For each usb hub device in the system check to see if it is in the
598 * peer domain of the given port_dev, and if it is check to see if it
599 * has a port that matches the given port by location
601 static int match_location(struct usb_device *peer_hdev, void *p)
604 struct usb_hcd *hcd, *peer_hcd;
605 struct usb_port *port_dev = p, *peer;
606 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
607 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
609 if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
612 hcd = bus_to_hcd(hdev->bus);
613 peer_hcd = bus_to_hcd(peer_hdev->bus);
614 /* peer_hcd is provisional until we verify it against the known peer */
615 if (peer_hcd != hcd->shared_hcd)
618 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
619 peer = peer_hub->ports[port1 - 1];
620 if (peer && peer->connect_type != USB_PORT_NOT_USED &&
621 peer->location == port_dev->location) {
622 link_peers_report(port_dev, peer);
631 * Find the peer port either via explicit platform firmware "location"
632 * data, the peer hcd for root hubs, or the upstream peer relationship
633 * for all other hubs.
635 static void find_and_link_peer(struct usb_hub *hub, int port1)
637 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
638 struct usb_device *hdev = hub->hdev;
639 struct usb_device *peer_hdev;
640 struct usb_hub *peer_hub;
643 * If location data is available then we can only peer this port
644 * by a location match, not the default peer (lest we create a
645 * situation where we need to go back and undo a default peering
646 * when the port is later peered by location data)
648 if (port_dev->location) {
649 /* we link the peer in match_location() if found */
650 usb_for_each_dev(port_dev, match_location);
652 } else if (!hdev->parent) {
653 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
654 struct usb_hcd *peer_hcd = hcd->shared_hcd;
659 peer_hdev = peer_hcd->self.root_hub;
661 struct usb_port *upstream;
662 struct usb_device *parent = hdev->parent;
663 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
668 upstream = parent_hub->ports[hdev->portnum - 1];
669 if (!upstream || !upstream->peer)
672 peer_hdev = upstream->peer->child;
675 peer_hub = usb_hub_to_struct_hub(peer_hdev);
676 if (!peer_hub || port1 > peer_hdev->maxchild)
680 * we found a valid default peer, last check is to make sure it
681 * does not have location data
683 peer = peer_hub->ports[port1 - 1];
684 if (peer && peer->location == 0)
685 link_peers_report(port_dev, peer);
688 static int connector_bind(struct device *dev, struct device *connector, void *data)
690 struct usb_port *port_dev = to_usb_port(dev);
693 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
697 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
699 sysfs_remove_link(&dev->kobj, "connector");
703 port_dev->connector = data;
706 * If there is already USB device connected to the port, letting the
707 * Type-C connector know about it immediately.
710 typec_attach(port_dev->connector, &port_dev->child->dev);
715 static void connector_unbind(struct device *dev, struct device *connector, void *data)
717 struct usb_port *port_dev = to_usb_port(dev);
719 sysfs_remove_link(&connector->kobj, dev_name(dev));
720 sysfs_remove_link(&dev->kobj, "connector");
721 port_dev->connector = NULL;
724 static const struct component_ops connector_ops = {
725 .bind = connector_bind,
726 .unbind = connector_unbind,
729 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
731 struct usb_port *port_dev;
732 struct usb_device *hdev = hub->hdev;
735 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
739 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
740 if (!port_dev->req) {
745 port_dev->connect_type = usb_of_get_connect_type(hdev, port1);
746 hub->ports[port1 - 1] = port_dev;
747 port_dev->portnum = port1;
748 set_bit(port1, hub->power_bits);
749 port_dev->dev.parent = hub->intfdev;
750 if (hub_is_superspeed(hdev)) {
751 port_dev->is_superspeed = 1;
752 port_dev->usb3_lpm_u1_permit = 1;
753 port_dev->usb3_lpm_u2_permit = 1;
754 port_dev->dev.groups = port_dev_usb3_group;
756 port_dev->dev.groups = port_dev_group;
757 port_dev->dev.type = &usb_port_device_type;
758 port_dev->dev.driver = &usb_port_driver;
759 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
761 mutex_init(&port_dev->status_lock);
762 retval = device_register(&port_dev->dev);
764 put_device(&port_dev->dev);
768 port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
769 if (!port_dev->state_kn) {
770 dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
775 /* Set default policy of port-poweroff disabled. */
776 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
777 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
782 retval = component_add(&port_dev->dev, &connector_ops);
784 dev_warn(&port_dev->dev, "failed to add component\n");
788 find_and_link_peer(hub, port1);
791 * Enable runtime pm and hold a refernce that hub_configure()
792 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
793 * and the hub has been fully registered (hdev->maxchild set).
795 pm_runtime_set_active(&port_dev->dev);
796 pm_runtime_get_noresume(&port_dev->dev);
797 pm_runtime_enable(&port_dev->dev);
798 device_enable_async_suspend(&port_dev->dev);
801 * Keep hidden the ability to enable port-poweroff if the hub
802 * does not support power switching.
804 if (!hub_is_port_power_switchable(hub))
807 /* Attempt to let userspace take over the policy. */
808 retval = dev_pm_qos_expose_flags(&port_dev->dev,
809 PM_QOS_FLAG_NO_POWER_OFF);
811 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
815 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
816 retval = dev_pm_qos_remove_request(port_dev->req);
818 kfree(port_dev->req);
819 port_dev->req = NULL;
824 sysfs_put(port_dev->state_kn);
826 device_unregister(&port_dev->dev);
831 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
833 struct usb_port *port_dev = hub->ports[port1 - 1];
834 struct usb_port *peer;
836 peer = port_dev->peer;
838 unlink_peers(port_dev, peer);
839 component_del(&port_dev->dev, &connector_ops);
840 sysfs_put(port_dev->state_kn);
841 device_unregister(&port_dev->dev);