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>
15 static int usb_port_block_power_off;
17 static const struct attribute_group *port_dev_group[];
19 static ssize_t location_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct usb_port *port_dev = to_usb_port(dev);
24 return sprintf(buf, "0x%08x\n", port_dev->location);
26 static DEVICE_ATTR_RO(location);
28 static ssize_t connect_type_show(struct device *dev,
29 struct device_attribute *attr, char *buf)
31 struct usb_port *port_dev = to_usb_port(dev);
34 switch (port_dev->connect_type) {
35 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
38 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
41 case USB_PORT_NOT_USED:
49 return sprintf(buf, "%s\n", result);
51 static DEVICE_ATTR_RO(connect_type);
53 static ssize_t over_current_count_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct usb_port *port_dev = to_usb_port(dev);
58 return sprintf(buf, "%u\n", port_dev->over_current_count);
60 static DEVICE_ATTR_RO(over_current_count);
62 static ssize_t quirks_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
65 struct usb_port *port_dev = to_usb_port(dev);
67 return sprintf(buf, "%08x\n", port_dev->quirks);
70 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
71 const char *buf, size_t count)
73 struct usb_port *port_dev = to_usb_port(dev);
76 if (kstrtou32(buf, 16, &value))
79 port_dev->quirks = value;
82 static DEVICE_ATTR_RW(quirks);
84 static ssize_t usb3_lpm_permit_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 struct usb_port *port_dev = to_usb_port(dev);
90 if (port_dev->usb3_lpm_u1_permit) {
91 if (port_dev->usb3_lpm_u2_permit)
96 if (port_dev->usb3_lpm_u2_permit)
102 return sprintf(buf, "%s\n", p);
105 static ssize_t usb3_lpm_permit_store(struct device *dev,
106 struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct usb_port *port_dev = to_usb_port(dev);
110 struct usb_device *udev = port_dev->child;
113 if (!strncmp(buf, "u1_u2", 5)) {
114 port_dev->usb3_lpm_u1_permit = 1;
115 port_dev->usb3_lpm_u2_permit = 1;
117 } else if (!strncmp(buf, "u1", 2)) {
118 port_dev->usb3_lpm_u1_permit = 1;
119 port_dev->usb3_lpm_u2_permit = 0;
121 } else if (!strncmp(buf, "u2", 2)) {
122 port_dev->usb3_lpm_u1_permit = 0;
123 port_dev->usb3_lpm_u2_permit = 1;
125 } else if (!strncmp(buf, "0", 1)) {
126 port_dev->usb3_lpm_u1_permit = 0;
127 port_dev->usb3_lpm_u2_permit = 0;
131 /* If device is connected to the port, disable or enable lpm
132 * to make new u1 u2 setting take effect immediately.
135 hcd = bus_to_hcd(udev->bus);
138 usb_lock_device(udev);
139 mutex_lock(hcd->bandwidth_mutex);
140 if (!usb_disable_lpm(udev))
141 usb_enable_lpm(udev);
142 mutex_unlock(hcd->bandwidth_mutex);
143 usb_unlock_device(udev);
148 static DEVICE_ATTR_RW(usb3_lpm_permit);
150 static struct attribute *port_dev_attrs[] = {
151 &dev_attr_connect_type.attr,
152 &dev_attr_location.attr,
153 &dev_attr_quirks.attr,
154 &dev_attr_over_current_count.attr,
158 static struct attribute_group port_dev_attr_grp = {
159 .attrs = port_dev_attrs,
162 static const struct attribute_group *port_dev_group[] = {
167 static struct attribute *port_dev_usb3_attrs[] = {
168 &dev_attr_usb3_lpm_permit.attr,
172 static struct attribute_group port_dev_usb3_attr_grp = {
173 .attrs = port_dev_usb3_attrs,
176 static const struct attribute_group *port_dev_usb3_group[] = {
178 &port_dev_usb3_attr_grp,
182 static void usb_port_device_release(struct device *dev)
184 struct usb_port *port_dev = to_usb_port(dev);
186 kfree(port_dev->req);
191 static int usb_port_runtime_resume(struct device *dev)
193 struct usb_port *port_dev = to_usb_port(dev);
194 struct usb_device *hdev = to_usb_device(dev->parent->parent);
195 struct usb_interface *intf = to_usb_interface(dev->parent);
196 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
197 struct usb_device *udev = port_dev->child;
198 struct usb_port *peer = port_dev->peer;
199 int port1 = port_dev->portnum;
205 set_bit(port1, hub->power_bits);
210 * Power on our usb3 peer before this usb2 port to prevent a usb3
211 * device from degrading to its usb2 connection
213 if (!port_dev->is_superspeed && peer)
214 pm_runtime_get_sync(&peer->dev);
216 retval = usb_autopm_get_interface(intf);
220 retval = usb_hub_set_port_power(hdev, hub, port1, true);
221 msleep(hub_power_on_good_delay(hub));
222 if (udev && !retval) {
224 * Our preference is to simply wait for the port to reconnect,
225 * as that is the lowest latency method to restart the port.
226 * However, there are cases where toggling port power results in
227 * the host port and the device port getting out of sync causing
228 * a link training live lock. Upon timeout, flag the port as
229 * needing warm reset recovery (to be performed later by
230 * usb_port_resume() as requested via usb_wakeup_notification())
232 if (hub_port_debounce_be_connected(hub, port1) < 0) {
233 dev_dbg(&port_dev->dev, "reconnect timeout\n");
234 if (hub_is_superspeed(hdev))
235 set_bit(port1, hub->warm_reset_bits);
238 /* Force the child awake to revalidate after the power loss. */
239 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
240 pm_runtime_get_noresume(&port_dev->dev);
241 pm_request_resume(&udev->dev);
245 usb_autopm_put_interface(intf);
250 static int usb_port_runtime_suspend(struct device *dev)
252 struct usb_port *port_dev = to_usb_port(dev);
253 struct usb_device *hdev = to_usb_device(dev->parent->parent);
254 struct usb_interface *intf = to_usb_interface(dev->parent);
255 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
256 struct usb_port *peer = port_dev->peer;
257 int port1 = port_dev->portnum;
265 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
269 if (usb_port_block_power_off)
272 retval = usb_autopm_get_interface(intf);
276 retval = usb_hub_set_port_power(hdev, hub, port1, false);
277 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
278 if (!port_dev->is_superspeed)
279 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
280 usb_autopm_put_interface(intf);
283 * Our peer usb3 port may now be able to suspend, so
284 * asynchronously queue a suspend request to observe that this
285 * usb2 port is now off.
287 if (!port_dev->is_superspeed && peer)
288 pm_runtime_put(&peer->dev);
294 static void usb_port_shutdown(struct device *dev)
296 struct usb_port *port_dev = to_usb_port(dev);
298 if (port_dev->child) {
299 usb_disable_usb2_hardware_lpm(port_dev->child);
300 usb_unlocked_disable_lpm(port_dev->child);
304 static const struct dev_pm_ops usb_port_pm_ops = {
306 .runtime_suspend = usb_port_runtime_suspend,
307 .runtime_resume = usb_port_runtime_resume,
311 struct device_type usb_port_device_type = {
313 .release = usb_port_device_release,
314 .pm = &usb_port_pm_ops,
317 static struct device_driver usb_port_driver = {
319 .owner = THIS_MODULE,
320 .shutdown = usb_port_shutdown,
323 static int link_peers(struct usb_port *left, struct usb_port *right)
325 struct usb_port *ss_port, *hs_port;
328 if (left->peer == right && right->peer == left)
331 if (left->peer || right->peer) {
332 struct usb_port *lpeer = left->peer;
333 struct usb_port *rpeer = right->peer;
336 if (left->location && left->location == right->location)
341 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
342 dev_name(&left->dev), dev_name(&right->dev), method,
343 dev_name(&left->dev),
344 lpeer ? dev_name(&lpeer->dev) : "none",
345 dev_name(&right->dev),
346 rpeer ? dev_name(&rpeer->dev) : "none");
350 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
353 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
355 sysfs_remove_link(&left->dev.kobj, "peer");
360 * We need to wake the HiSpeed port to make sure we don't race
361 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
362 * may miss a suspend event for the SuperSpeed port.
364 if (left->is_superspeed) {
366 WARN_ON(right->is_superspeed);
370 WARN_ON(!right->is_superspeed);
373 pm_runtime_get_sync(&hs_port->dev);
379 * The SuperSpeed reference is dropped when the HiSpeed port in
380 * this relationship suspends, i.e. when it is safe to allow a
381 * SuperSpeed connection to drop since there is no risk of a
382 * device degrading to its powered-off HiSpeed connection.
384 * Also, drop the HiSpeed ref taken above.
386 pm_runtime_get_sync(&ss_port->dev);
387 pm_runtime_put(&hs_port->dev);
392 static void link_peers_report(struct usb_port *left, struct usb_port *right)
396 rc = link_peers(left, right);
398 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
400 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
401 dev_name(&right->dev), rc);
402 pr_warn_once("usb: port power management may be unreliable\n");
403 usb_port_block_power_off = 1;
407 static void unlink_peers(struct usb_port *left, struct usb_port *right)
409 struct usb_port *ss_port, *hs_port;
411 WARN(right->peer != left || left->peer != right,
412 "%s and %s are not peers?\n",
413 dev_name(&left->dev), dev_name(&right->dev));
416 * We wake the HiSpeed port to make sure we don't race its
417 * usb_port_runtime_resume() event which takes a SuperSpeed ref
418 * when ->peer is !NULL.
420 if (left->is_superspeed) {
428 pm_runtime_get_sync(&hs_port->dev);
430 sysfs_remove_link(&left->dev.kobj, "peer");
432 sysfs_remove_link(&right->dev.kobj, "peer");
435 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
436 pm_runtime_put(&ss_port->dev);
438 /* Drop the ref taken above */
439 pm_runtime_put(&hs_port->dev);
443 * For each usb hub device in the system check to see if it is in the
444 * peer domain of the given port_dev, and if it is check to see if it
445 * has a port that matches the given port by location
447 static int match_location(struct usb_device *peer_hdev, void *p)
450 struct usb_hcd *hcd, *peer_hcd;
451 struct usb_port *port_dev = p, *peer;
452 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
453 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
455 if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
458 hcd = bus_to_hcd(hdev->bus);
459 peer_hcd = bus_to_hcd(peer_hdev->bus);
460 /* peer_hcd is provisional until we verify it against the known peer */
461 if (peer_hcd != hcd->shared_hcd)
464 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
465 peer = peer_hub->ports[port1 - 1];
466 if (peer && peer->connect_type != USB_PORT_NOT_USED &&
467 peer->location == port_dev->location) {
468 link_peers_report(port_dev, peer);
477 * Find the peer port either via explicit platform firmware "location"
478 * data, the peer hcd for root hubs, or the upstream peer relationship
479 * for all other hubs.
481 static void find_and_link_peer(struct usb_hub *hub, int port1)
483 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
484 struct usb_device *hdev = hub->hdev;
485 struct usb_device *peer_hdev;
486 struct usb_hub *peer_hub;
489 * If location data is available then we can only peer this port
490 * by a location match, not the default peer (lest we create a
491 * situation where we need to go back and undo a default peering
492 * when the port is later peered by location data)
494 if (port_dev->location) {
495 /* we link the peer in match_location() if found */
496 usb_for_each_dev(port_dev, match_location);
498 } else if (!hdev->parent) {
499 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
500 struct usb_hcd *peer_hcd = hcd->shared_hcd;
505 peer_hdev = peer_hcd->self.root_hub;
507 struct usb_port *upstream;
508 struct usb_device *parent = hdev->parent;
509 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
514 upstream = parent_hub->ports[hdev->portnum - 1];
515 if (!upstream || !upstream->peer)
518 peer_hdev = upstream->peer->child;
521 peer_hub = usb_hub_to_struct_hub(peer_hdev);
522 if (!peer_hub || port1 > peer_hdev->maxchild)
526 * we found a valid default peer, last check is to make sure it
527 * does not have location data
529 peer = peer_hub->ports[port1 - 1];
530 if (peer && peer->location == 0)
531 link_peers_report(port_dev, peer);
534 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
536 struct usb_port *port_dev;
537 struct usb_device *hdev = hub->hdev;
540 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
544 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
545 if (!port_dev->req) {
550 hub->ports[port1 - 1] = port_dev;
551 port_dev->portnum = port1;
552 set_bit(port1, hub->power_bits);
553 port_dev->dev.parent = hub->intfdev;
554 if (hub_is_superspeed(hdev)) {
555 port_dev->usb3_lpm_u1_permit = 1;
556 port_dev->usb3_lpm_u2_permit = 1;
557 port_dev->dev.groups = port_dev_usb3_group;
559 port_dev->dev.groups = port_dev_group;
560 port_dev->dev.type = &usb_port_device_type;
561 port_dev->dev.driver = &usb_port_driver;
562 if (hub_is_superspeed(hub->hdev))
563 port_dev->is_superspeed = 1;
564 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
566 mutex_init(&port_dev->status_lock);
567 retval = device_register(&port_dev->dev);
569 put_device(&port_dev->dev);
573 /* Set default policy of port-poweroff disabled. */
574 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
575 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
577 device_unregister(&port_dev->dev);
581 find_and_link_peer(hub, port1);
584 * Enable runtime pm and hold a refernce that hub_configure()
585 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
586 * and the hub has been fully registered (hdev->maxchild set).
588 pm_runtime_set_active(&port_dev->dev);
589 pm_runtime_get_noresume(&port_dev->dev);
590 pm_runtime_enable(&port_dev->dev);
591 device_enable_async_suspend(&port_dev->dev);
594 * Keep hidden the ability to enable port-poweroff if the hub
595 * does not support power switching.
597 if (!hub_is_port_power_switchable(hub))
600 /* Attempt to let userspace take over the policy. */
601 retval = dev_pm_qos_expose_flags(&port_dev->dev,
602 PM_QOS_FLAG_NO_POWER_OFF);
604 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
608 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
609 retval = dev_pm_qos_remove_request(port_dev->req);
611 kfree(port_dev->req);
612 port_dev->req = NULL;
617 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
619 struct usb_port *port_dev = hub->ports[port1 - 1];
620 struct usb_port *peer;
622 peer = port_dev->peer;
624 unlink_peers(port_dev, peer);
625 device_unregister(&port_dev->dev);