1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
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 connect_type_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct usb_port *port_dev = to_usb_port(dev);
25 switch (port_dev->connect_type) {
26 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
29 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
32 case USB_PORT_NOT_USED:
40 return sprintf(buf, "%s\n", result);
42 static DEVICE_ATTR_RO(connect_type);
44 static ssize_t over_current_count_show(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct usb_port *port_dev = to_usb_port(dev);
49 return sprintf(buf, "%u\n", port_dev->over_current_count);
51 static DEVICE_ATTR_RO(over_current_count);
53 static ssize_t usb3_lpm_permit_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct usb_port *port_dev = to_usb_port(dev);
59 if (port_dev->usb3_lpm_u1_permit) {
60 if (port_dev->usb3_lpm_u2_permit)
65 if (port_dev->usb3_lpm_u2_permit)
71 return sprintf(buf, "%s\n", p);
74 static ssize_t usb3_lpm_permit_store(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t count)
78 struct usb_port *port_dev = to_usb_port(dev);
79 struct usb_device *udev = port_dev->child;
82 if (!strncmp(buf, "u1_u2", 5)) {
83 port_dev->usb3_lpm_u1_permit = 1;
84 port_dev->usb3_lpm_u2_permit = 1;
86 } else if (!strncmp(buf, "u1", 2)) {
87 port_dev->usb3_lpm_u1_permit = 1;
88 port_dev->usb3_lpm_u2_permit = 0;
90 } else if (!strncmp(buf, "u2", 2)) {
91 port_dev->usb3_lpm_u1_permit = 0;
92 port_dev->usb3_lpm_u2_permit = 1;
94 } else if (!strncmp(buf, "0", 1)) {
95 port_dev->usb3_lpm_u1_permit = 0;
96 port_dev->usb3_lpm_u2_permit = 0;
100 /* If device is connected to the port, disable or enable lpm
101 * to make new u1 u2 setting take effect immediately.
104 hcd = bus_to_hcd(udev->bus);
107 usb_lock_device(udev);
108 mutex_lock(hcd->bandwidth_mutex);
109 if (!usb_disable_lpm(udev))
110 usb_enable_lpm(udev);
111 mutex_unlock(hcd->bandwidth_mutex);
112 usb_unlock_device(udev);
117 static DEVICE_ATTR_RW(usb3_lpm_permit);
119 static struct attribute *port_dev_attrs[] = {
120 &dev_attr_connect_type.attr,
121 &dev_attr_over_current_count.attr,
125 static struct attribute_group port_dev_attr_grp = {
126 .attrs = port_dev_attrs,
129 static const struct attribute_group *port_dev_group[] = {
134 static struct attribute *port_dev_usb3_attrs[] = {
135 &dev_attr_usb3_lpm_permit.attr,
139 static struct attribute_group port_dev_usb3_attr_grp = {
140 .attrs = port_dev_usb3_attrs,
143 static const struct attribute_group *port_dev_usb3_group[] = {
145 &port_dev_usb3_attr_grp,
149 static void usb_port_device_release(struct device *dev)
151 struct usb_port *port_dev = to_usb_port(dev);
153 kfree(port_dev->req);
158 static int usb_port_runtime_resume(struct device *dev)
160 struct usb_port *port_dev = to_usb_port(dev);
161 struct usb_device *hdev = to_usb_device(dev->parent->parent);
162 struct usb_interface *intf = to_usb_interface(dev->parent);
163 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
164 struct usb_device *udev = port_dev->child;
165 struct usb_port *peer = port_dev->peer;
166 int port1 = port_dev->portnum;
172 set_bit(port1, hub->power_bits);
177 * Power on our usb3 peer before this usb2 port to prevent a usb3
178 * device from degrading to its usb2 connection
180 if (!port_dev->is_superspeed && peer)
181 pm_runtime_get_sync(&peer->dev);
183 usb_autopm_get_interface(intf);
184 retval = usb_hub_set_port_power(hdev, hub, port1, true);
185 msleep(hub_power_on_good_delay(hub));
186 if (udev && !retval) {
188 * Our preference is to simply wait for the port to reconnect,
189 * as that is the lowest latency method to restart the port.
190 * However, there are cases where toggling port power results in
191 * the host port and the device port getting out of sync causing
192 * a link training live lock. Upon timeout, flag the port as
193 * needing warm reset recovery (to be performed later by
194 * usb_port_resume() as requested via usb_wakeup_notification())
196 if (hub_port_debounce_be_connected(hub, port1) < 0) {
197 dev_dbg(&port_dev->dev, "reconnect timeout\n");
198 if (hub_is_superspeed(hdev))
199 set_bit(port1, hub->warm_reset_bits);
202 /* Force the child awake to revalidate after the power loss. */
203 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
204 pm_runtime_get_noresume(&port_dev->dev);
205 pm_request_resume(&udev->dev);
209 usb_autopm_put_interface(intf);
214 static int usb_port_runtime_suspend(struct device *dev)
216 struct usb_port *port_dev = to_usb_port(dev);
217 struct usb_device *hdev = to_usb_device(dev->parent->parent);
218 struct usb_interface *intf = to_usb_interface(dev->parent);
219 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
220 struct usb_port *peer = port_dev->peer;
221 int port1 = port_dev->portnum;
229 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
233 if (usb_port_block_power_off)
236 usb_autopm_get_interface(intf);
237 retval = usb_hub_set_port_power(hdev, hub, port1, false);
238 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
239 if (!port_dev->is_superspeed)
240 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
241 usb_autopm_put_interface(intf);
244 * Our peer usb3 port may now be able to suspend, so
245 * asynchronously queue a suspend request to observe that this
246 * usb2 port is now off.
248 if (!port_dev->is_superspeed && peer)
249 pm_runtime_put(&peer->dev);
255 static const struct dev_pm_ops usb_port_pm_ops = {
257 .runtime_suspend = usb_port_runtime_suspend,
258 .runtime_resume = usb_port_runtime_resume,
262 struct device_type usb_port_device_type = {
264 .release = usb_port_device_release,
265 .pm = &usb_port_pm_ops,
268 static struct device_driver usb_port_driver = {
270 .owner = THIS_MODULE,
273 static int link_peers(struct usb_port *left, struct usb_port *right)
275 struct usb_port *ss_port, *hs_port;
278 if (left->peer == right && right->peer == left)
281 if (left->peer || right->peer) {
282 struct usb_port *lpeer = left->peer;
283 struct usb_port *rpeer = right->peer;
286 if (left->location && left->location == right->location)
291 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
292 dev_name(&left->dev), dev_name(&right->dev), method,
293 dev_name(&left->dev),
294 lpeer ? dev_name(&lpeer->dev) : "none",
295 dev_name(&right->dev),
296 rpeer ? dev_name(&rpeer->dev) : "none");
300 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
303 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
305 sysfs_remove_link(&left->dev.kobj, "peer");
310 * We need to wake the HiSpeed port to make sure we don't race
311 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
312 * may miss a suspend event for the SuperSpeed port.
314 if (left->is_superspeed) {
316 WARN_ON(right->is_superspeed);
320 WARN_ON(!right->is_superspeed);
323 pm_runtime_get_sync(&hs_port->dev);
329 * The SuperSpeed reference is dropped when the HiSpeed port in
330 * this relationship suspends, i.e. when it is safe to allow a
331 * SuperSpeed connection to drop since there is no risk of a
332 * device degrading to its powered-off HiSpeed connection.
334 * Also, drop the HiSpeed ref taken above.
336 pm_runtime_get_sync(&ss_port->dev);
337 pm_runtime_put(&hs_port->dev);
342 static void link_peers_report(struct usb_port *left, struct usb_port *right)
346 rc = link_peers(left, right);
348 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
350 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
351 dev_name(&right->dev), rc);
352 pr_warn_once("usb: port power management may be unreliable\n");
353 usb_port_block_power_off = 1;
357 static void unlink_peers(struct usb_port *left, struct usb_port *right)
359 struct usb_port *ss_port, *hs_port;
361 WARN(right->peer != left || left->peer != right,
362 "%s and %s are not peers?\n",
363 dev_name(&left->dev), dev_name(&right->dev));
366 * We wake the HiSpeed port to make sure we don't race its
367 * usb_port_runtime_resume() event which takes a SuperSpeed ref
368 * when ->peer is !NULL.
370 if (left->is_superspeed) {
378 pm_runtime_get_sync(&hs_port->dev);
380 sysfs_remove_link(&left->dev.kobj, "peer");
382 sysfs_remove_link(&right->dev.kobj, "peer");
385 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
386 pm_runtime_put(&ss_port->dev);
388 /* Drop the ref taken above */
389 pm_runtime_put(&hs_port->dev);
393 * For each usb hub device in the system check to see if it is in the
394 * peer domain of the given port_dev, and if it is check to see if it
395 * has a port that matches the given port by location
397 static int match_location(struct usb_device *peer_hdev, void *p)
400 struct usb_hcd *hcd, *peer_hcd;
401 struct usb_port *port_dev = p, *peer;
402 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
403 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
408 hcd = bus_to_hcd(hdev->bus);
409 peer_hcd = bus_to_hcd(peer_hdev->bus);
410 /* peer_hcd is provisional until we verify it against the known peer */
411 if (peer_hcd != hcd->shared_hcd)
414 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
415 peer = peer_hub->ports[port1 - 1];
416 if (peer && peer->location == port_dev->location) {
417 link_peers_report(port_dev, peer);
426 * Find the peer port either via explicit platform firmware "location"
427 * data, the peer hcd for root hubs, or the upstream peer relationship
428 * for all other hubs.
430 static void find_and_link_peer(struct usb_hub *hub, int port1)
432 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
433 struct usb_device *hdev = hub->hdev;
434 struct usb_device *peer_hdev;
435 struct usb_hub *peer_hub;
438 * If location data is available then we can only peer this port
439 * by a location match, not the default peer (lest we create a
440 * situation where we need to go back and undo a default peering
441 * when the port is later peered by location data)
443 if (port_dev->location) {
444 /* we link the peer in match_location() if found */
445 usb_for_each_dev(port_dev, match_location);
447 } else if (!hdev->parent) {
448 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
449 struct usb_hcd *peer_hcd = hcd->shared_hcd;
454 peer_hdev = peer_hcd->self.root_hub;
456 struct usb_port *upstream;
457 struct usb_device *parent = hdev->parent;
458 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
463 upstream = parent_hub->ports[hdev->portnum - 1];
464 if (!upstream || !upstream->peer)
467 peer_hdev = upstream->peer->child;
470 peer_hub = usb_hub_to_struct_hub(peer_hdev);
471 if (!peer_hub || port1 > peer_hdev->maxchild)
475 * we found a valid default peer, last check is to make sure it
476 * does not have location data
478 peer = peer_hub->ports[port1 - 1];
479 if (peer && peer->location == 0)
480 link_peers_report(port_dev, peer);
483 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
485 struct usb_port *port_dev;
486 struct usb_device *hdev = hub->hdev;
489 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
493 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
494 if (!port_dev->req) {
499 hub->ports[port1 - 1] = port_dev;
500 port_dev->portnum = port1;
501 set_bit(port1, hub->power_bits);
502 port_dev->dev.parent = hub->intfdev;
503 if (hub_is_superspeed(hdev)) {
504 port_dev->usb3_lpm_u1_permit = 1;
505 port_dev->usb3_lpm_u2_permit = 1;
506 port_dev->dev.groups = port_dev_usb3_group;
508 port_dev->dev.groups = port_dev_group;
509 port_dev->dev.type = &usb_port_device_type;
510 port_dev->dev.driver = &usb_port_driver;
511 if (hub_is_superspeed(hub->hdev))
512 port_dev->is_superspeed = 1;
513 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
515 mutex_init(&port_dev->status_lock);
516 retval = device_register(&port_dev->dev);
518 put_device(&port_dev->dev);
522 /* Set default policy of port-poweroff disabled. */
523 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
524 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
526 device_unregister(&port_dev->dev);
530 find_and_link_peer(hub, port1);
533 * Enable runtime pm and hold a refernce that hub_configure()
534 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
535 * and the hub has been fully registered (hdev->maxchild set).
537 pm_runtime_set_active(&port_dev->dev);
538 pm_runtime_get_noresume(&port_dev->dev);
539 pm_runtime_enable(&port_dev->dev);
540 device_enable_async_suspend(&port_dev->dev);
543 * Keep hidden the ability to enable port-poweroff if the hub
544 * does not support power switching.
546 if (!hub_is_port_power_switchable(hub))
549 /* Attempt to let userspace take over the policy. */
550 retval = dev_pm_qos_expose_flags(&port_dev->dev,
551 PM_QOS_FLAG_NO_POWER_OFF);
553 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
557 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
558 retval = dev_pm_qos_remove_request(port_dev->req);
560 kfree(port_dev->req);
561 port_dev->req = NULL;
566 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
568 struct usb_port *port_dev = hub->ports[port1 - 1];
569 struct usb_port *peer;
571 peer = port_dev->peer;
573 unlink_peers(port_dev, peer);
574 device_unregister(&port_dev->dev);