1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2012 Intel Corp
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 disable_show(struct device *dev,
21 struct device_attribute *attr, char *buf)
23 struct usb_port *port_dev = to_usb_port(dev);
24 struct usb_device *hdev = to_usb_device(dev->parent->parent);
25 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
26 struct usb_interface *intf = to_usb_interface(hub->intfdev);
27 int port1 = port_dev->portnum;
28 u16 portstatus, unused;
32 rc = usb_autopm_get_interface(intf);
36 usb_lock_device(hdev);
37 if (hub->disconnected) {
42 usb_hub_port_status(hub, port1, &portstatus, &unused);
43 disabled = !usb_port_is_power_on(hub, portstatus);
46 usb_unlock_device(hdev);
47 usb_autopm_put_interface(intf);
52 return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
55 static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
56 const char *buf, size_t count)
58 struct usb_port *port_dev = to_usb_port(dev);
59 struct usb_device *hdev = to_usb_device(dev->parent->parent);
60 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
61 struct usb_interface *intf = to_usb_interface(hub->intfdev);
62 int port1 = port_dev->portnum;
66 rc = strtobool(buf, &disabled);
70 rc = usb_autopm_get_interface(intf);
74 usb_lock_device(hdev);
75 if (hub->disconnected) {
80 if (disabled && port_dev->child)
81 usb_disconnect(&port_dev->child);
83 rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
86 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
87 if (!port_dev->is_superspeed)
88 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
95 usb_unlock_device(hdev);
96 usb_autopm_put_interface(intf);
100 static DEVICE_ATTR_RW(disable);
102 static ssize_t location_show(struct device *dev,
103 struct device_attribute *attr, char *buf)
105 struct usb_port *port_dev = to_usb_port(dev);
107 return sprintf(buf, "0x%08x\n", port_dev->location);
109 static DEVICE_ATTR_RO(location);
111 static ssize_t connect_type_show(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct usb_port *port_dev = to_usb_port(dev);
117 switch (port_dev->connect_type) {
118 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
121 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
122 result = "hardwired";
124 case USB_PORT_NOT_USED:
132 return sprintf(buf, "%s\n", result);
134 static DEVICE_ATTR_RO(connect_type);
136 static ssize_t over_current_count_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
139 struct usb_port *port_dev = to_usb_port(dev);
141 return sprintf(buf, "%u\n", port_dev->over_current_count);
143 static DEVICE_ATTR_RO(over_current_count);
145 static ssize_t quirks_show(struct device *dev,
146 struct device_attribute *attr, char *buf)
148 struct usb_port *port_dev = to_usb_port(dev);
150 return sprintf(buf, "%08x\n", port_dev->quirks);
153 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
154 const char *buf, size_t count)
156 struct usb_port *port_dev = to_usb_port(dev);
159 if (kstrtou32(buf, 16, &value))
162 port_dev->quirks = value;
165 static DEVICE_ATTR_RW(quirks);
167 static ssize_t usb3_lpm_permit_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct usb_port *port_dev = to_usb_port(dev);
173 if (port_dev->usb3_lpm_u1_permit) {
174 if (port_dev->usb3_lpm_u2_permit)
179 if (port_dev->usb3_lpm_u2_permit)
185 return sprintf(buf, "%s\n", p);
188 static ssize_t usb3_lpm_permit_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct usb_port *port_dev = to_usb_port(dev);
193 struct usb_device *udev = port_dev->child;
196 if (!strncmp(buf, "u1_u2", 5)) {
197 port_dev->usb3_lpm_u1_permit = 1;
198 port_dev->usb3_lpm_u2_permit = 1;
200 } else if (!strncmp(buf, "u1", 2)) {
201 port_dev->usb3_lpm_u1_permit = 1;
202 port_dev->usb3_lpm_u2_permit = 0;
204 } else if (!strncmp(buf, "u2", 2)) {
205 port_dev->usb3_lpm_u1_permit = 0;
206 port_dev->usb3_lpm_u2_permit = 1;
208 } else if (!strncmp(buf, "0", 1)) {
209 port_dev->usb3_lpm_u1_permit = 0;
210 port_dev->usb3_lpm_u2_permit = 0;
214 /* If device is connected to the port, disable or enable lpm
215 * to make new u1 u2 setting take effect immediately.
218 hcd = bus_to_hcd(udev->bus);
221 usb_lock_device(udev);
222 mutex_lock(hcd->bandwidth_mutex);
223 if (!usb_disable_lpm(udev))
224 usb_enable_lpm(udev);
225 mutex_unlock(hcd->bandwidth_mutex);
226 usb_unlock_device(udev);
231 static DEVICE_ATTR_RW(usb3_lpm_permit);
233 static struct attribute *port_dev_attrs[] = {
234 &dev_attr_connect_type.attr,
235 &dev_attr_location.attr,
236 &dev_attr_quirks.attr,
237 &dev_attr_over_current_count.attr,
238 &dev_attr_disable.attr,
242 static const struct attribute_group port_dev_attr_grp = {
243 .attrs = port_dev_attrs,
246 static const struct attribute_group *port_dev_group[] = {
251 static struct attribute *port_dev_usb3_attrs[] = {
252 &dev_attr_usb3_lpm_permit.attr,
256 static const struct attribute_group port_dev_usb3_attr_grp = {
257 .attrs = port_dev_usb3_attrs,
260 static const struct attribute_group *port_dev_usb3_group[] = {
262 &port_dev_usb3_attr_grp,
266 static void usb_port_device_release(struct device *dev)
268 struct usb_port *port_dev = to_usb_port(dev);
270 kfree(port_dev->req);
275 static int usb_port_runtime_resume(struct device *dev)
277 struct usb_port *port_dev = to_usb_port(dev);
278 struct usb_device *hdev = to_usb_device(dev->parent->parent);
279 struct usb_interface *intf = to_usb_interface(dev->parent);
280 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
281 struct usb_device *udev = port_dev->child;
282 struct usb_port *peer = port_dev->peer;
283 int port1 = port_dev->portnum;
289 set_bit(port1, hub->power_bits);
294 * Power on our usb3 peer before this usb2 port to prevent a usb3
295 * device from degrading to its usb2 connection
297 if (!port_dev->is_superspeed && peer)
298 pm_runtime_get_sync(&peer->dev);
300 retval = usb_autopm_get_interface(intf);
304 retval = usb_hub_set_port_power(hdev, hub, port1, true);
305 msleep(hub_power_on_good_delay(hub));
306 if (udev && !retval) {
308 * Our preference is to simply wait for the port to reconnect,
309 * as that is the lowest latency method to restart the port.
310 * However, there are cases where toggling port power results in
311 * the host port and the device port getting out of sync causing
312 * a link training live lock. Upon timeout, flag the port as
313 * needing warm reset recovery (to be performed later by
314 * usb_port_resume() as requested via usb_wakeup_notification())
316 if (hub_port_debounce_be_connected(hub, port1) < 0) {
317 dev_dbg(&port_dev->dev, "reconnect timeout\n");
318 if (hub_is_superspeed(hdev))
319 set_bit(port1, hub->warm_reset_bits);
322 /* Force the child awake to revalidate after the power loss. */
323 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
324 pm_runtime_get_noresume(&port_dev->dev);
325 pm_request_resume(&udev->dev);
329 usb_autopm_put_interface(intf);
334 static int usb_port_runtime_suspend(struct device *dev)
336 struct usb_port *port_dev = to_usb_port(dev);
337 struct usb_device *hdev = to_usb_device(dev->parent->parent);
338 struct usb_interface *intf = to_usb_interface(dev->parent);
339 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
340 struct usb_port *peer = port_dev->peer;
341 int port1 = port_dev->portnum;
349 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
353 if (usb_port_block_power_off)
356 retval = usb_autopm_get_interface(intf);
360 retval = usb_hub_set_port_power(hdev, hub, port1, false);
361 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
362 if (!port_dev->is_superspeed)
363 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
364 usb_autopm_put_interface(intf);
367 * Our peer usb3 port may now be able to suspend, so
368 * asynchronously queue a suspend request to observe that this
369 * usb2 port is now off.
371 if (!port_dev->is_superspeed && peer)
372 pm_runtime_put(&peer->dev);
378 static void usb_port_shutdown(struct device *dev)
380 struct usb_port *port_dev = to_usb_port(dev);
383 usb_disable_usb2_hardware_lpm(port_dev->child);
386 static const struct dev_pm_ops usb_port_pm_ops = {
388 .runtime_suspend = usb_port_runtime_suspend,
389 .runtime_resume = usb_port_runtime_resume,
393 struct device_type usb_port_device_type = {
395 .release = usb_port_device_release,
396 .pm = &usb_port_pm_ops,
399 static struct device_driver usb_port_driver = {
401 .owner = THIS_MODULE,
402 .shutdown = usb_port_shutdown,
405 static int link_peers(struct usb_port *left, struct usb_port *right)
407 struct usb_port *ss_port, *hs_port;
410 if (left->peer == right && right->peer == left)
413 if (left->peer || right->peer) {
414 struct usb_port *lpeer = left->peer;
415 struct usb_port *rpeer = right->peer;
418 if (left->location && left->location == right->location)
423 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
424 dev_name(&left->dev), dev_name(&right->dev), method,
425 dev_name(&left->dev),
426 lpeer ? dev_name(&lpeer->dev) : "none",
427 dev_name(&right->dev),
428 rpeer ? dev_name(&rpeer->dev) : "none");
432 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
435 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
437 sysfs_remove_link(&left->dev.kobj, "peer");
442 * We need to wake the HiSpeed port to make sure we don't race
443 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
444 * may miss a suspend event for the SuperSpeed port.
446 if (left->is_superspeed) {
448 WARN_ON(right->is_superspeed);
452 WARN_ON(!right->is_superspeed);
455 pm_runtime_get_sync(&hs_port->dev);
461 * The SuperSpeed reference is dropped when the HiSpeed port in
462 * this relationship suspends, i.e. when it is safe to allow a
463 * SuperSpeed connection to drop since there is no risk of a
464 * device degrading to its powered-off HiSpeed connection.
466 * Also, drop the HiSpeed ref taken above.
468 pm_runtime_get_sync(&ss_port->dev);
469 pm_runtime_put(&hs_port->dev);
474 static void link_peers_report(struct usb_port *left, struct usb_port *right)
478 rc = link_peers(left, right);
480 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
482 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
483 dev_name(&right->dev), rc);
484 pr_warn_once("usb: port power management may be unreliable\n");
485 usb_port_block_power_off = 1;
489 static void unlink_peers(struct usb_port *left, struct usb_port *right)
491 struct usb_port *ss_port, *hs_port;
493 WARN(right->peer != left || left->peer != right,
494 "%s and %s are not peers?\n",
495 dev_name(&left->dev), dev_name(&right->dev));
498 * We wake the HiSpeed port to make sure we don't race its
499 * usb_port_runtime_resume() event which takes a SuperSpeed ref
500 * when ->peer is !NULL.
502 if (left->is_superspeed) {
510 pm_runtime_get_sync(&hs_port->dev);
512 sysfs_remove_link(&left->dev.kobj, "peer");
514 sysfs_remove_link(&right->dev.kobj, "peer");
517 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
518 pm_runtime_put(&ss_port->dev);
520 /* Drop the ref taken above */
521 pm_runtime_put(&hs_port->dev);
525 * For each usb hub device in the system check to see if it is in the
526 * peer domain of the given port_dev, and if it is check to see if it
527 * has a port that matches the given port by location
529 static int match_location(struct usb_device *peer_hdev, void *p)
532 struct usb_hcd *hcd, *peer_hcd;
533 struct usb_port *port_dev = p, *peer;
534 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
535 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
540 hcd = bus_to_hcd(hdev->bus);
541 peer_hcd = bus_to_hcd(peer_hdev->bus);
542 /* peer_hcd is provisional until we verify it against the known peer */
543 if (peer_hcd != hcd->shared_hcd)
546 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
547 peer = peer_hub->ports[port1 - 1];
548 if (peer && peer->location == port_dev->location) {
549 link_peers_report(port_dev, peer);
558 * Find the peer port either via explicit platform firmware "location"
559 * data, the peer hcd for root hubs, or the upstream peer relationship
560 * for all other hubs.
562 static void find_and_link_peer(struct usb_hub *hub, int port1)
564 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
565 struct usb_device *hdev = hub->hdev;
566 struct usb_device *peer_hdev;
567 struct usb_hub *peer_hub;
570 * If location data is available then we can only peer this port
571 * by a location match, not the default peer (lest we create a
572 * situation where we need to go back and undo a default peering
573 * when the port is later peered by location data)
575 if (port_dev->location) {
576 /* we link the peer in match_location() if found */
577 usb_for_each_dev(port_dev, match_location);
579 } else if (!hdev->parent) {
580 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
581 struct usb_hcd *peer_hcd = hcd->shared_hcd;
586 peer_hdev = peer_hcd->self.root_hub;
588 struct usb_port *upstream;
589 struct usb_device *parent = hdev->parent;
590 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
595 upstream = parent_hub->ports[hdev->portnum - 1];
596 if (!upstream || !upstream->peer)
599 peer_hdev = upstream->peer->child;
602 peer_hub = usb_hub_to_struct_hub(peer_hdev);
603 if (!peer_hub || port1 > peer_hdev->maxchild)
607 * we found a valid default peer, last check is to make sure it
608 * does not have location data
610 peer = peer_hub->ports[port1 - 1];
611 if (peer && peer->location == 0)
612 link_peers_report(port_dev, peer);
615 static int connector_bind(struct device *dev, struct device *connector, void *data)
619 ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
623 ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
625 sysfs_remove_link(&dev->kobj, "connector");
630 static void connector_unbind(struct device *dev, struct device *connector, void *data)
632 sysfs_remove_link(&connector->kobj, dev_name(dev));
633 sysfs_remove_link(&dev->kobj, "connector");
636 static const struct component_ops connector_ops = {
637 .bind = connector_bind,
638 .unbind = connector_unbind,
641 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
643 struct usb_port *port_dev;
644 struct usb_device *hdev = hub->hdev;
647 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
651 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
652 if (!port_dev->req) {
657 hub->ports[port1 - 1] = port_dev;
658 port_dev->portnum = port1;
659 set_bit(port1, hub->power_bits);
660 port_dev->dev.parent = hub->intfdev;
661 if (hub_is_superspeed(hdev)) {
662 port_dev->usb3_lpm_u1_permit = 1;
663 port_dev->usb3_lpm_u2_permit = 1;
664 port_dev->dev.groups = port_dev_usb3_group;
666 port_dev->dev.groups = port_dev_group;
667 port_dev->dev.type = &usb_port_device_type;
668 port_dev->dev.driver = &usb_port_driver;
669 if (hub_is_superspeed(hub->hdev))
670 port_dev->is_superspeed = 1;
671 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
673 mutex_init(&port_dev->status_lock);
674 retval = device_register(&port_dev->dev);
676 put_device(&port_dev->dev);
680 /* Set default policy of port-poweroff disabled. */
681 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
682 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
684 device_unregister(&port_dev->dev);
688 retval = component_add(&port_dev->dev, &connector_ops);
690 dev_warn(&port_dev->dev, "failed to add component\n");
691 device_unregister(&port_dev->dev);
695 find_and_link_peer(hub, port1);
698 * Enable runtime pm and hold a refernce that hub_configure()
699 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
700 * and the hub has been fully registered (hdev->maxchild set).
702 pm_runtime_set_active(&port_dev->dev);
703 pm_runtime_get_noresume(&port_dev->dev);
704 pm_runtime_enable(&port_dev->dev);
705 device_enable_async_suspend(&port_dev->dev);
708 * Keep hidden the ability to enable port-poweroff if the hub
709 * does not support power switching.
711 if (!hub_is_port_power_switchable(hub))
714 /* Attempt to let userspace take over the policy. */
715 retval = dev_pm_qos_expose_flags(&port_dev->dev,
716 PM_QOS_FLAG_NO_POWER_OFF);
718 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
722 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
723 retval = dev_pm_qos_remove_request(port_dev->req);
725 kfree(port_dev->req);
726 port_dev->req = NULL;
731 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
733 struct usb_port *port_dev = hub->ports[port1 - 1];
734 struct usb_port *peer;
736 peer = port_dev->peer;
738 unlink_peers(port_dev, peer);
739 component_del(&port_dev->dev, &connector_ops);
740 device_unregister(&port_dev->dev);