1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
10 #include <linux/slab.h>
11 #include <linux/pm_qos.h>
12 #include <linux/component.h>
16 static int usb_port_block_power_off;
18 static const struct attribute_group *port_dev_group[];
20 static ssize_t location_show(struct device *dev,
21 struct device_attribute *attr, char *buf)
23 struct usb_port *port_dev = to_usb_port(dev);
25 return sprintf(buf, "0x%08x\n", port_dev->location);
27 static DEVICE_ATTR_RO(location);
29 static ssize_t connect_type_show(struct device *dev,
30 struct device_attribute *attr, char *buf)
32 struct usb_port *port_dev = to_usb_port(dev);
35 switch (port_dev->connect_type) {
36 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
39 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
42 case USB_PORT_NOT_USED:
50 return sprintf(buf, "%s\n", result);
52 static DEVICE_ATTR_RO(connect_type);
54 static ssize_t over_current_count_show(struct device *dev,
55 struct device_attribute *attr, char *buf)
57 struct usb_port *port_dev = to_usb_port(dev);
59 return sprintf(buf, "%u\n", port_dev->over_current_count);
61 static DEVICE_ATTR_RO(over_current_count);
63 static ssize_t quirks_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
66 struct usb_port *port_dev = to_usb_port(dev);
68 return sprintf(buf, "%08x\n", port_dev->quirks);
71 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
72 const char *buf, size_t count)
74 struct usb_port *port_dev = to_usb_port(dev);
77 if (kstrtou32(buf, 16, &value))
80 port_dev->quirks = value;
83 static DEVICE_ATTR_RW(quirks);
85 static ssize_t usb3_lpm_permit_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
88 struct usb_port *port_dev = to_usb_port(dev);
91 if (port_dev->usb3_lpm_u1_permit) {
92 if (port_dev->usb3_lpm_u2_permit)
97 if (port_dev->usb3_lpm_u2_permit)
103 return sprintf(buf, "%s\n", p);
106 static ssize_t usb3_lpm_permit_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf, size_t count)
110 struct usb_port *port_dev = to_usb_port(dev);
111 struct usb_device *udev = port_dev->child;
114 if (!strncmp(buf, "u1_u2", 5)) {
115 port_dev->usb3_lpm_u1_permit = 1;
116 port_dev->usb3_lpm_u2_permit = 1;
118 } else if (!strncmp(buf, "u1", 2)) {
119 port_dev->usb3_lpm_u1_permit = 1;
120 port_dev->usb3_lpm_u2_permit = 0;
122 } else if (!strncmp(buf, "u2", 2)) {
123 port_dev->usb3_lpm_u1_permit = 0;
124 port_dev->usb3_lpm_u2_permit = 1;
126 } else if (!strncmp(buf, "0", 1)) {
127 port_dev->usb3_lpm_u1_permit = 0;
128 port_dev->usb3_lpm_u2_permit = 0;
132 /* If device is connected to the port, disable or enable lpm
133 * to make new u1 u2 setting take effect immediately.
136 hcd = bus_to_hcd(udev->bus);
139 usb_lock_device(udev);
140 mutex_lock(hcd->bandwidth_mutex);
141 if (!usb_disable_lpm(udev))
142 usb_enable_lpm(udev);
143 mutex_unlock(hcd->bandwidth_mutex);
144 usb_unlock_device(udev);
149 static DEVICE_ATTR_RW(usb3_lpm_permit);
151 static struct attribute *port_dev_attrs[] = {
152 &dev_attr_connect_type.attr,
153 &dev_attr_location.attr,
154 &dev_attr_quirks.attr,
155 &dev_attr_over_current_count.attr,
159 static const struct attribute_group port_dev_attr_grp = {
160 .attrs = port_dev_attrs,
163 static const struct attribute_group *port_dev_group[] = {
168 static struct attribute *port_dev_usb3_attrs[] = {
169 &dev_attr_usb3_lpm_permit.attr,
173 static const struct attribute_group port_dev_usb3_attr_grp = {
174 .attrs = port_dev_usb3_attrs,
177 static const struct attribute_group *port_dev_usb3_group[] = {
179 &port_dev_usb3_attr_grp,
183 static void usb_port_device_release(struct device *dev)
185 struct usb_port *port_dev = to_usb_port(dev);
187 kfree(port_dev->req);
192 static int usb_port_runtime_resume(struct device *dev)
194 struct usb_port *port_dev = to_usb_port(dev);
195 struct usb_device *hdev = to_usb_device(dev->parent->parent);
196 struct usb_interface *intf = to_usb_interface(dev->parent);
197 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
198 struct usb_device *udev = port_dev->child;
199 struct usb_port *peer = port_dev->peer;
200 int port1 = port_dev->portnum;
206 set_bit(port1, hub->power_bits);
211 * Power on our usb3 peer before this usb2 port to prevent a usb3
212 * device from degrading to its usb2 connection
214 if (!port_dev->is_superspeed && peer)
215 pm_runtime_get_sync(&peer->dev);
217 retval = usb_autopm_get_interface(intf);
221 retval = usb_hub_set_port_power(hdev, hub, port1, true);
222 msleep(hub_power_on_good_delay(hub));
223 if (udev && !retval) {
225 * Our preference is to simply wait for the port to reconnect,
226 * as that is the lowest latency method to restart the port.
227 * However, there are cases where toggling port power results in
228 * the host port and the device port getting out of sync causing
229 * a link training live lock. Upon timeout, flag the port as
230 * needing warm reset recovery (to be performed later by
231 * usb_port_resume() as requested via usb_wakeup_notification())
233 if (hub_port_debounce_be_connected(hub, port1) < 0) {
234 dev_dbg(&port_dev->dev, "reconnect timeout\n");
235 if (hub_is_superspeed(hdev))
236 set_bit(port1, hub->warm_reset_bits);
239 /* Force the child awake to revalidate after the power loss. */
240 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
241 pm_runtime_get_noresume(&port_dev->dev);
242 pm_request_resume(&udev->dev);
246 usb_autopm_put_interface(intf);
251 static int usb_port_runtime_suspend(struct device *dev)
253 struct usb_port *port_dev = to_usb_port(dev);
254 struct usb_device *hdev = to_usb_device(dev->parent->parent);
255 struct usb_interface *intf = to_usb_interface(dev->parent);
256 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
257 struct usb_port *peer = port_dev->peer;
258 int port1 = port_dev->portnum;
266 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
270 if (usb_port_block_power_off)
273 retval = usb_autopm_get_interface(intf);
277 retval = usb_hub_set_port_power(hdev, hub, port1, false);
278 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
279 if (!port_dev->is_superspeed)
280 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
281 usb_autopm_put_interface(intf);
284 * Our peer usb3 port may now be able to suspend, so
285 * asynchronously queue a suspend request to observe that this
286 * usb2 port is now off.
288 if (!port_dev->is_superspeed && peer)
289 pm_runtime_put(&peer->dev);
295 static void usb_port_shutdown(struct device *dev)
297 struct usb_port *port_dev = to_usb_port(dev);
300 usb_disable_usb2_hardware_lpm(port_dev->child);
303 static const struct dev_pm_ops usb_port_pm_ops = {
305 .runtime_suspend = usb_port_runtime_suspend,
306 .runtime_resume = usb_port_runtime_resume,
310 struct device_type usb_port_device_type = {
312 .release = usb_port_device_release,
313 .pm = &usb_port_pm_ops,
316 static struct device_driver usb_port_driver = {
318 .owner = THIS_MODULE,
319 .shutdown = usb_port_shutdown,
322 static int link_peers(struct usb_port *left, struct usb_port *right)
324 struct usb_port *ss_port, *hs_port;
327 if (left->peer == right && right->peer == left)
330 if (left->peer || right->peer) {
331 struct usb_port *lpeer = left->peer;
332 struct usb_port *rpeer = right->peer;
335 if (left->location && left->location == right->location)
340 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
341 dev_name(&left->dev), dev_name(&right->dev), method,
342 dev_name(&left->dev),
343 lpeer ? dev_name(&lpeer->dev) : "none",
344 dev_name(&right->dev),
345 rpeer ? dev_name(&rpeer->dev) : "none");
349 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
352 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
354 sysfs_remove_link(&left->dev.kobj, "peer");
359 * We need to wake the HiSpeed port to make sure we don't race
360 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
361 * may miss a suspend event for the SuperSpeed port.
363 if (left->is_superspeed) {
365 WARN_ON(right->is_superspeed);
369 WARN_ON(!right->is_superspeed);
372 pm_runtime_get_sync(&hs_port->dev);
378 * The SuperSpeed reference is dropped when the HiSpeed port in
379 * this relationship suspends, i.e. when it is safe to allow a
380 * SuperSpeed connection to drop since there is no risk of a
381 * device degrading to its powered-off HiSpeed connection.
383 * Also, drop the HiSpeed ref taken above.
385 pm_runtime_get_sync(&ss_port->dev);
386 pm_runtime_put(&hs_port->dev);
391 static void link_peers_report(struct usb_port *left, struct usb_port *right)
395 rc = link_peers(left, right);
397 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
399 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
400 dev_name(&right->dev), rc);
401 pr_warn_once("usb: port power management may be unreliable\n");
402 usb_port_block_power_off = 1;
406 static void unlink_peers(struct usb_port *left, struct usb_port *right)
408 struct usb_port *ss_port, *hs_port;
410 WARN(right->peer != left || left->peer != right,
411 "%s and %s are not peers?\n",
412 dev_name(&left->dev), dev_name(&right->dev));
415 * We wake the HiSpeed port to make sure we don't race its
416 * usb_port_runtime_resume() event which takes a SuperSpeed ref
417 * when ->peer is !NULL.
419 if (left->is_superspeed) {
427 pm_runtime_get_sync(&hs_port->dev);
429 sysfs_remove_link(&left->dev.kobj, "peer");
431 sysfs_remove_link(&right->dev.kobj, "peer");
434 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
435 pm_runtime_put(&ss_port->dev);
437 /* Drop the ref taken above */
438 pm_runtime_put(&hs_port->dev);
442 * For each usb hub device in the system check to see if it is in the
443 * peer domain of the given port_dev, and if it is check to see if it
444 * has a port that matches the given port by location
446 static int match_location(struct usb_device *peer_hdev, void *p)
449 struct usb_hcd *hcd, *peer_hcd;
450 struct usb_port *port_dev = p, *peer;
451 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
452 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
457 hcd = bus_to_hcd(hdev->bus);
458 peer_hcd = bus_to_hcd(peer_hdev->bus);
459 /* peer_hcd is provisional until we verify it against the known peer */
460 if (peer_hcd != hcd->shared_hcd)
463 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
464 peer = peer_hub->ports[port1 - 1];
465 if (peer && peer->location == port_dev->location) {
466 link_peers_report(port_dev, peer);
475 * Find the peer port either via explicit platform firmware "location"
476 * data, the peer hcd for root hubs, or the upstream peer relationship
477 * for all other hubs.
479 static void find_and_link_peer(struct usb_hub *hub, int port1)
481 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
482 struct usb_device *hdev = hub->hdev;
483 struct usb_device *peer_hdev;
484 struct usb_hub *peer_hub;
487 * If location data is available then we can only peer this port
488 * by a location match, not the default peer (lest we create a
489 * situation where we need to go back and undo a default peering
490 * when the port is later peered by location data)
492 if (port_dev->location) {
493 /* we link the peer in match_location() if found */
494 usb_for_each_dev(port_dev, match_location);
496 } else if (!hdev->parent) {
497 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
498 struct usb_hcd *peer_hcd = hcd->shared_hcd;
503 peer_hdev = peer_hcd->self.root_hub;
505 struct usb_port *upstream;
506 struct usb_device *parent = hdev->parent;
507 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
512 upstream = parent_hub->ports[hdev->portnum - 1];
513 if (!upstream || !upstream->peer)
516 peer_hdev = upstream->peer->child;
519 peer_hub = usb_hub_to_struct_hub(peer_hdev);
520 if (!peer_hub || port1 > peer_hdev->maxchild)
524 * we found a valid default peer, last check is to make sure it
525 * does not have location data
527 peer = peer_hub->ports[port1 - 1];
528 if (peer && peer->location == 0)
529 link_peers_report(port_dev, peer);
532 static int connector_bind(struct device *dev, struct device *connector, void *data)
536 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
540 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
542 sysfs_remove_link(&dev->kobj, "connector");
547 static void connector_unbind(struct device *dev, struct device *connector, void *data)
549 sysfs_remove_link(&connector->kobj, dev_name(dev));
550 sysfs_remove_link(&dev->kobj, "connector");
553 static const struct component_ops connector_ops = {
554 .bind = connector_bind,
555 .unbind = connector_unbind,
558 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
560 struct usb_port *port_dev;
561 struct usb_device *hdev = hub->hdev;
564 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
568 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
569 if (!port_dev->req) {
574 hub->ports[port1 - 1] = port_dev;
575 port_dev->portnum = port1;
576 set_bit(port1, hub->power_bits);
577 port_dev->dev.parent = hub->intfdev;
578 if (hub_is_superspeed(hdev)) {
579 port_dev->usb3_lpm_u1_permit = 1;
580 port_dev->usb3_lpm_u2_permit = 1;
581 port_dev->dev.groups = port_dev_usb3_group;
583 port_dev->dev.groups = port_dev_group;
584 port_dev->dev.type = &usb_port_device_type;
585 port_dev->dev.driver = &usb_port_driver;
586 if (hub_is_superspeed(hub->hdev))
587 port_dev->is_superspeed = 1;
588 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
590 mutex_init(&port_dev->status_lock);
591 retval = device_register(&port_dev->dev);
593 put_device(&port_dev->dev);
597 /* Set default policy of port-poweroff disabled. */
598 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
599 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
601 device_unregister(&port_dev->dev);
605 find_and_link_peer(hub, port1);
607 retval = component_add(&port_dev->dev, &connector_ops);
609 dev_warn(&port_dev->dev, "failed to add component\n");
612 * Enable runtime pm and hold a refernce that hub_configure()
613 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
614 * and the hub has been fully registered (hdev->maxchild set).
616 pm_runtime_set_active(&port_dev->dev);
617 pm_runtime_get_noresume(&port_dev->dev);
618 pm_runtime_enable(&port_dev->dev);
619 device_enable_async_suspend(&port_dev->dev);
622 * Keep hidden the ability to enable port-poweroff if the hub
623 * does not support power switching.
625 if (!hub_is_port_power_switchable(hub))
628 /* Attempt to let userspace take over the policy. */
629 retval = dev_pm_qos_expose_flags(&port_dev->dev,
630 PM_QOS_FLAG_NO_POWER_OFF);
632 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
636 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
637 retval = dev_pm_qos_remove_request(port_dev->req);
639 kfree(port_dev->req);
640 port_dev->req = NULL;
645 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
647 struct usb_port *port_dev = hub->ports[port1 - 1];
648 struct usb_port *peer;
650 peer = port_dev->peer;
652 unlink_peers(port_dev, peer);
653 component_del(&port_dev->dev, &connector_ops);
654 device_unregister(&port_dev->dev);