2017-11-03 10:28:30 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-01-11 12:10:38 +00:00
|
|
|
/*
|
|
|
|
* usb port device code
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Intel Corp
|
|
|
|
*
|
|
|
|
* Author: Lan Tianyu <tianyu.lan@intel.com>
|
|
|
|
*/
|
|
|
|
|
2023-05-05 15:02:46 +00:00
|
|
|
#include <linux/kstrtox.h>
|
2013-01-19 14:30:19 +00:00
|
|
|
#include <linux/slab.h>
|
2025-03-19 18:49:46 +00:00
|
|
|
#include <linux/sysfs.h>
|
2013-01-22 20:26:29 +00:00
|
|
|
#include <linux/pm_qos.h>
|
2022-05-02 16:32:58 +00:00
|
|
|
#include <linux/component.h>
|
usb: core: Set connect_type of ports based on DT node
JIRA: https://issues.redhat.com/browse/RHEL-37673
commit 82e82130a78b75a9ce5225df24d5a0b1b3290eb0
Author: Stephen Boyd <swboyd@chromium.org>
Date: Thu, 22 Feb 2024 16:58:21 -0800
When a USB hub is described in DT, such as any device that matches the
onboard-hub driver, the connect_type is set to "unknown" or
USB_PORT_CONNECT_TYPE_UNKNOWN. This makes any device plugged into that
USB port report their 'removable' device attribute as "unknown".
ChromeOS userspace would like to know if the USB device is actually
removable or not so that security policies can be applied. Improve the
connect_type attribute for ports, and in turn the removable attribute
for USB devices, by looking for child devices with a reg property or an
OF graph when the device is described in DT.
If the graph exists, endpoints that are connected to a remote node must
be something like a usb-{a,b,c}-connector compatible node, or an
intermediate node like a redriver, and not a hardwired USB device on the
board. Set the connect_type to USB_PORT_CONNECT_TYPE_HOT_PLUG in this
case because the device is going to be plugged in. Set the connect_type
to USB_PORT_CONNECT_TYPE_HARD_WIRED if there's a child node for the port
like 'device@2' for port2. Set the connect_type to USB_PORT_NOT_USED if
there isn't an endpoint or child node corresponding to the port number.
To make sure things don't change, only set the port to not used if
there are child nodes. This way an onboard hub connect_type doesn't
change until ports are added or child nodes are added to describe
hardwired devices. It's assumed that all ports or no ports will be
described for a device.
Cc: Matthias Kaehlcke <mka@chromium.org>
Cc: linux-usb@vger.kernel.org
Cc: devicetree@vger.kernel.org
Cc: Pin-yen Lin <treapking@chromium.org>
Cc: maciek swiech <drmasquatch@google.com>
Signed-off-by: Stephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20240223005823.3074029-3-swboyd@chromium.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Desnes Nunes <desnesn@redhat.com>
2024-06-10 17:28:56 +00:00
|
|
|
#include <linux/usb/of.h>
|
2013-01-19 14:30:19 +00:00
|
|
|
|
2013-01-11 12:10:38 +00:00
|
|
|
#include "hub.h"
|
|
|
|
|
2014-06-17 23:16:27 +00:00
|
|
|
static int usb_port_block_power_off;
|
|
|
|
|
2013-01-19 17:53:32 +00:00
|
|
|
static const struct attribute_group *port_dev_group[];
|
|
|
|
|
2023-05-05 15:02:51 +00:00
|
|
|
static ssize_t early_stop_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
|
|
|
return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
bool value;
|
|
|
|
|
|
|
|
if (kstrtobool(buf, &value))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
port_dev->early_stop = 1;
|
|
|
|
else
|
|
|
|
port_dev->early_stop = 0;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(early_stop);
|
|
|
|
|
2022-11-02 01:33:58 +00:00
|
|
|
static ssize_t disable_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
|
|
|
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
2024-06-10 17:29:09 +00:00
|
|
|
struct usb_interface *intf = to_usb_interface(dev->parent);
|
2022-11-02 01:33:58 +00:00
|
|
|
int port1 = port_dev->portnum;
|
|
|
|
u16 portstatus, unused;
|
|
|
|
bool disabled;
|
|
|
|
int rc;
|
2024-06-10 17:29:03 +00:00
|
|
|
struct kernfs_node *kn;
|
2022-11-02 01:33:58 +00:00
|
|
|
|
2024-06-10 17:29:09 +00:00
|
|
|
if (!hub)
|
|
|
|
return -ENODEV;
|
2024-06-10 17:29:03 +00:00
|
|
|
hub_get(hub);
|
2022-11-02 01:33:58 +00:00
|
|
|
rc = usb_autopm_get_interface(intf);
|
|
|
|
if (rc < 0)
|
2024-06-10 17:29:03 +00:00
|
|
|
goto out_hub_get;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent deadlock if another process is concurrently
|
|
|
|
* trying to unregister hdev.
|
|
|
|
*/
|
|
|
|
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
|
|
|
|
if (!kn) {
|
|
|
|
rc = -ENODEV;
|
|
|
|
goto out_autopm;
|
|
|
|
}
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_lock_device(hdev);
|
|
|
|
if (hub->disconnected) {
|
|
|
|
rc = -ENODEV;
|
|
|
|
goto out_hdev_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_hub_port_status(hub, port1, &portstatus, &unused);
|
|
|
|
disabled = !usb_port_is_power_on(hub, portstatus);
|
|
|
|
|
2024-06-10 17:29:03 +00:00
|
|
|
out_hdev_lock:
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_unlock_device(hdev);
|
2024-06-10 17:29:03 +00:00
|
|
|
sysfs_unbreak_active_protection(kn);
|
|
|
|
out_autopm:
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_autopm_put_interface(intf);
|
2024-06-10 17:29:03 +00:00
|
|
|
out_hub_get:
|
|
|
|
hub_put(hub);
|
2022-11-02 01:33:58 +00:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return sysfs_emit(buf, "%s\n", disabled ? "1" : "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t disable_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
|
|
|
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
2024-06-10 17:29:09 +00:00
|
|
|
struct usb_interface *intf = to_usb_interface(dev->parent);
|
2022-11-02 01:33:58 +00:00
|
|
|
int port1 = port_dev->portnum;
|
|
|
|
bool disabled;
|
|
|
|
int rc;
|
2024-06-10 17:29:03 +00:00
|
|
|
struct kernfs_node *kn;
|
2022-11-02 01:33:58 +00:00
|
|
|
|
2024-06-10 17:29:09 +00:00
|
|
|
if (!hub)
|
|
|
|
return -ENODEV;
|
2023-05-05 15:02:46 +00:00
|
|
|
rc = kstrtobool(buf, &disabled);
|
2022-11-02 01:33:58 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2024-06-10 17:29:03 +00:00
|
|
|
hub_get(hub);
|
2022-11-02 01:33:58 +00:00
|
|
|
rc = usb_autopm_get_interface(intf);
|
|
|
|
if (rc < 0)
|
2024-06-10 17:29:03 +00:00
|
|
|
goto out_hub_get;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent deadlock if another process is concurrently
|
|
|
|
* trying to unregister hdev.
|
|
|
|
*/
|
|
|
|
kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
|
|
|
|
if (!kn) {
|
|
|
|
rc = -ENODEV;
|
|
|
|
goto out_autopm;
|
|
|
|
}
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_lock_device(hdev);
|
|
|
|
if (hub->disconnected) {
|
|
|
|
rc = -ENODEV;
|
|
|
|
goto out_hdev_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled && port_dev->child)
|
|
|
|
usb_disconnect(&port_dev->child);
|
|
|
|
|
|
|
|
rc = usb_hub_set_port_power(hdev, hub, port1, !disabled);
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
|
|
|
|
if (!port_dev->is_superspeed)
|
|
|
|
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
rc = count;
|
|
|
|
|
2024-06-10 17:29:03 +00:00
|
|
|
out_hdev_lock:
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_unlock_device(hdev);
|
2024-06-10 17:29:03 +00:00
|
|
|
sysfs_unbreak_active_protection(kn);
|
|
|
|
out_autopm:
|
2022-11-02 01:33:58 +00:00
|
|
|
usb_autopm_put_interface(intf);
|
2024-06-10 17:29:03 +00:00
|
|
|
out_hub_get:
|
|
|
|
hub_put(hub);
|
2022-11-02 01:33:58 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(disable);
|
|
|
|
|
2018-09-28 13:40:31 +00:00
|
|
|
static ssize_t location_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
2025-03-19 18:49:46 +00:00
|
|
|
return sysfs_emit(buf, "0x%08x\n", port_dev->location);
|
2018-09-28 13:40:31 +00:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(location);
|
|
|
|
|
2013-08-23 23:05:26 +00:00
|
|
|
static ssize_t connect_type_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2013-01-19 17:53:32 +00:00
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
switch (port_dev->connect_type) {
|
|
|
|
case USB_PORT_CONNECT_TYPE_HOT_PLUG:
|
|
|
|
result = "hotplug";
|
|
|
|
break;
|
|
|
|
case USB_PORT_CONNECT_TYPE_HARD_WIRED:
|
|
|
|
result = "hardwired";
|
|
|
|
break;
|
|
|
|
case USB_PORT_NOT_USED:
|
|
|
|
result = "not used";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2025-03-19 18:49:46 +00:00
|
|
|
return sysfs_emit(buf, "%s\n", result);
|
2013-01-19 17:53:32 +00:00
|
|
|
}
|
2013-08-23 23:05:26 +00:00
|
|
|
static DEVICE_ATTR_RO(connect_type);
|
2013-01-19 17:53:32 +00:00
|
|
|
|
2023-11-21 14:58:56 +00:00
|
|
|
static ssize_t state_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
enum usb_device_state state = READ_ONCE(port_dev->state);
|
|
|
|
|
|
|
|
return sysfs_emit(buf, "%s\n", usb_state_string(state));
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(state);
|
|
|
|
|
2018-03-20 10:17:13 +00:00
|
|
|
static ssize_t over_current_count_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
2025-03-19 18:49:46 +00:00
|
|
|
return sysfs_emit(buf, "%u\n", port_dev->over_current_count);
|
2018-03-20 10:17:13 +00:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(over_current_count);
|
|
|
|
|
2018-05-28 06:32:18 +00:00
|
|
|
static ssize_t quirks_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
2025-03-19 18:49:46 +00:00
|
|
|
return sysfs_emit(buf, "%08x\n", port_dev->quirks);
|
2018-05-28 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
if (kstrtou32(buf, 16, &value))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
port_dev->quirks = value;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(quirks);
|
|
|
|
|
2015-11-14 08:26:33 +00:00
|
|
|
static ssize_t usb3_lpm_permit_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (port_dev->usb3_lpm_u1_permit) {
|
|
|
|
if (port_dev->usb3_lpm_u2_permit)
|
|
|
|
p = "u1_u2";
|
|
|
|
else
|
|
|
|
p = "u1";
|
|
|
|
} else {
|
|
|
|
if (port_dev->usb3_lpm_u2_permit)
|
|
|
|
p = "u2";
|
|
|
|
else
|
|
|
|
p = "0";
|
|
|
|
}
|
|
|
|
|
2025-03-19 18:49:46 +00:00
|
|
|
return sysfs_emit(buf, "%s\n", p);
|
2015-11-14 08:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t usb3_lpm_permit_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
struct usb_device *udev = port_dev->child;
|
|
|
|
struct usb_hcd *hcd;
|
|
|
|
|
|
|
|
if (!strncmp(buf, "u1_u2", 5)) {
|
|
|
|
port_dev->usb3_lpm_u1_permit = 1;
|
|
|
|
port_dev->usb3_lpm_u2_permit = 1;
|
|
|
|
|
|
|
|
} else if (!strncmp(buf, "u1", 2)) {
|
|
|
|
port_dev->usb3_lpm_u1_permit = 1;
|
|
|
|
port_dev->usb3_lpm_u2_permit = 0;
|
|
|
|
|
|
|
|
} else if (!strncmp(buf, "u2", 2)) {
|
|
|
|
port_dev->usb3_lpm_u1_permit = 0;
|
|
|
|
port_dev->usb3_lpm_u2_permit = 1;
|
|
|
|
|
|
|
|
} else if (!strncmp(buf, "0", 1)) {
|
|
|
|
port_dev->usb3_lpm_u1_permit = 0;
|
|
|
|
port_dev->usb3_lpm_u2_permit = 0;
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* If device is connected to the port, disable or enable lpm
|
|
|
|
* to make new u1 u2 setting take effect immediately.
|
|
|
|
*/
|
|
|
|
if (udev) {
|
|
|
|
hcd = bus_to_hcd(udev->bus);
|
|
|
|
if (!hcd)
|
|
|
|
return -EINVAL;
|
|
|
|
usb_lock_device(udev);
|
|
|
|
mutex_lock(hcd->bandwidth_mutex);
|
|
|
|
if (!usb_disable_lpm(udev))
|
|
|
|
usb_enable_lpm(udev);
|
|
|
|
mutex_unlock(hcd->bandwidth_mutex);
|
|
|
|
usb_unlock_device(udev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(usb3_lpm_permit);
|
|
|
|
|
2013-01-19 17:53:32 +00:00
|
|
|
static struct attribute *port_dev_attrs[] = {
|
|
|
|
&dev_attr_connect_type.attr,
|
2023-11-21 14:58:56 +00:00
|
|
|
&dev_attr_state.attr,
|
2018-09-28 13:40:31 +00:00
|
|
|
&dev_attr_location.attr,
|
2018-05-28 06:32:18 +00:00
|
|
|
&dev_attr_quirks.attr,
|
2018-03-20 10:17:13 +00:00
|
|
|
&dev_attr_over_current_count.attr,
|
2022-11-02 01:33:58 +00:00
|
|
|
&dev_attr_disable.attr,
|
2023-05-05 15:02:51 +00:00
|
|
|
&dev_attr_early_stop.attr,
|
2013-01-19 17:53:32 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2020-11-25 16:24:58 +00:00
|
|
|
static const struct attribute_group port_dev_attr_grp = {
|
2013-01-19 17:53:32 +00:00
|
|
|
.attrs = port_dev_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group *port_dev_group[] = {
|
|
|
|
&port_dev_attr_grp,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2015-11-14 08:26:33 +00:00
|
|
|
static struct attribute *port_dev_usb3_attrs[] = {
|
|
|
|
&dev_attr_usb3_lpm_permit.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2020-11-25 16:24:58 +00:00
|
|
|
static const struct attribute_group port_dev_usb3_attr_grp = {
|
2015-11-14 08:26:33 +00:00
|
|
|
.attrs = port_dev_usb3_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group *port_dev_usb3_group[] = {
|
|
|
|
&port_dev_attr_grp,
|
|
|
|
&port_dev_usb3_attr_grp,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2013-01-11 12:10:38 +00:00
|
|
|
static void usb_port_device_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
2014-06-17 23:16:32 +00:00
|
|
|
kfree(port_dev->req);
|
2013-01-11 12:10:38 +00:00
|
|
|
kfree(port_dev);
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:47:05 +00:00
|
|
|
#ifdef CONFIG_PM
|
2013-01-22 20:26:29 +00:00
|
|
|
static int usb_port_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
|
|
|
struct usb_interface *intf = to_usb_interface(dev->parent);
|
2013-01-22 20:26:30 +00:00
|
|
|
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
usb: resume child device when port is powered on
Unconditionally wake up the child device when the power session is
recovered.
This addresses the following scenarios:
1/ The device may need a reset on power-session loss, without this
change port power-on recovery exposes khubd to scenarios that
usb_port_resume() is set to handle. Prior to port power control the
only time a power session would be lost is during dpm_suspend of the
hub. In that scenario usb_port_resume() is guaranteed to be called
prior to khubd running for that port. With this change we wakeup the
child device as soon as possible (prior to khubd running again for this
port).
Although khubd has facilities to wake a child device it will only do
so if the portstatus / portchange indicates a suspend state. In the
case of port power control we are not coming from a hub-port-suspend
state. This implementation simply uses pm_request_resume() to wake the
device and relies on the port_dev->status_lock to prevent any collisions
between khubd and usb_port_resume().
2/ This mechanism rate limits port power toggling. The minimum port
power on/off period is now gated by the child device suspend/resume
latency. Empirically this mitigates devices downgrading their connection
on perceived instability of the host connection. This ratelimiting is
really only relevant to port power control testing, but it is a nice
side effect of closing the above race. Namely, the race of khubd for
the given port running while a usb_port_resume() event is pending.
3/ Going forward we are finding that power-session recovery requires
warm-resets (http://marc.info/?t=138659232900003&r=1&w=2). This
mechanism allows for warm-resets to be requested at the same point in
the resume path for hub dpm_suspend power session losses, or port
rpm_suspend power session losses.
4/ If the device *was* disconnected the only time we'll know for sure is
after a failed resume, so it's necessary for usb_port_runtime_resume()
to expedite a usb_port_resume() to clean up the removed device. The
reasoning for this is "least surprise" for the user. Turning on a port
means that hotplug detection is again enabled for the port, it is
surprising that devices that were removed while the port was off are not
disconnected until they are attempted to be used. As a user "why would
I try to use a device I removed from the system?"
1, 2, and 4 are not a problem in the system dpm_resume() case because,
although the power-session is lost, khubd is frozen until after device
resume. For the rpm_resume() case pm_request_resume() is used to
request re-validation of the device, and if it happens to collide with a
khubd run we rely on the port_dev->status_lock to synchronize those
operations.
Besides testing, the primary scenario where this mechanism is expected
to be triggered is when the user changes the port power policy
(control/pm_qos_no_poweroff, or power/control). Each time power is
enabled want to revalidate the child device, where the revalidation is
handled by usb_port_resume().
Given that this arranges for port_dev->child to be de-referenced in
usb_port_runtime_resume() we need to make sure not to collide with
usb_disconnect() that frees the usb_device. To this end we hold the
port active with the "child_usage" reference across the disconnect
event. Subsequently, the need to access hub->child_usage_bits lead to
the creation of hub_disconnect_children() to remove any ambiguity of
which "hub" is being acted on in usb_disconnect() (prompted-by sharp
eyes from Alan).
Cc: Rafael J. Wysocki <rjw@rjwysocki.net>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-05-21 01:09:36 +00:00
|
|
|
struct usb_device *udev = port_dev->child;
|
2014-05-21 01:08:57 +00:00
|
|
|
struct usb_port *peer = port_dev->peer;
|
2013-01-22 20:26:30 +00:00
|
|
|
int port1 = port_dev->portnum;
|
2013-01-22 20:26:29 +00:00
|
|
|
int retval;
|
|
|
|
|
2013-01-22 20:26:30 +00:00
|
|
|
if (!hub)
|
|
|
|
return -EINVAL;
|
2014-05-21 01:08:07 +00:00
|
|
|
if (hub->in_reset) {
|
2014-05-21 01:08:52 +00:00
|
|
|
set_bit(port1, hub->power_bits);
|
2014-05-21 01:08:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-22 20:26:30 +00:00
|
|
|
|
2014-05-21 01:08:57 +00:00
|
|
|
/*
|
|
|
|
* Power on our usb3 peer before this usb2 port to prevent a usb3
|
|
|
|
* device from degrading to its usb2 connection
|
|
|
|
*/
|
|
|
|
if (!port_dev->is_superspeed && peer)
|
|
|
|
pm_runtime_get_sync(&peer->dev);
|
|
|
|
|
2020-02-26 17:50:36 +00:00
|
|
|
retval = usb_autopm_get_interface(intf);
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
|
2013-06-18 14:28:48 +00:00
|
|
|
retval = usb_hub_set_port_power(hdev, hub, port1, true);
|
2014-05-21 01:08:57 +00:00
|
|
|
msleep(hub_power_on_good_delay(hub));
|
usb: resume child device when port is powered on
Unconditionally wake up the child device when the power session is
recovered.
This addresses the following scenarios:
1/ The device may need a reset on power-session loss, without this
change port power-on recovery exposes khubd to scenarios that
usb_port_resume() is set to handle. Prior to port power control the
only time a power session would be lost is during dpm_suspend of the
hub. In that scenario usb_port_resume() is guaranteed to be called
prior to khubd running for that port. With this change we wakeup the
child device as soon as possible (prior to khubd running again for this
port).
Although khubd has facilities to wake a child device it will only do
so if the portstatus / portchange indicates a suspend state. In the
case of port power control we are not coming from a hub-port-suspend
state. This implementation simply uses pm_request_resume() to wake the
device and relies on the port_dev->status_lock to prevent any collisions
between khubd and usb_port_resume().
2/ This mechanism rate limits port power toggling. The minimum port
power on/off period is now gated by the child device suspend/resume
latency. Empirically this mitigates devices downgrading their connection
on perceived instability of the host connection. This ratelimiting is
really only relevant to port power control testing, but it is a nice
side effect of closing the above race. Namely, the race of khubd for
the given port running while a usb_port_resume() event is pending.
3/ Going forward we are finding that power-session recovery requires
warm-resets (http://marc.info/?t=138659232900003&r=1&w=2). This
mechanism allows for warm-resets to be requested at the same point in
the resume path for hub dpm_suspend power session losses, or port
rpm_suspend power session losses.
4/ If the device *was* disconnected the only time we'll know for sure is
after a failed resume, so it's necessary for usb_port_runtime_resume()
to expedite a usb_port_resume() to clean up the removed device. The
reasoning for this is "least surprise" for the user. Turning on a port
means that hotplug detection is again enabled for the port, it is
surprising that devices that were removed while the port was off are not
disconnected until they are attempted to be used. As a user "why would
I try to use a device I removed from the system?"
1, 2, and 4 are not a problem in the system dpm_resume() case because,
although the power-session is lost, khubd is frozen until after device
resume. For the rpm_resume() case pm_request_resume() is used to
request re-validation of the device, and if it happens to collide with a
khubd run we rely on the port_dev->status_lock to synchronize those
operations.
Besides testing, the primary scenario where this mechanism is expected
to be triggered is when the user changes the port power policy
(control/pm_qos_no_poweroff, or power/control). Each time power is
enabled want to revalidate the child device, where the revalidation is
handled by usb_port_resume().
Given that this arranges for port_dev->child to be de-referenced in
usb_port_runtime_resume() we need to make sure not to collide with
usb_disconnect() that frees the usb_device. To this end we hold the
port active with the "child_usage" reference across the disconnect
event. Subsequently, the need to access hub->child_usage_bits lead to
the creation of hub_disconnect_children() to remove any ambiguity of
which "hub" is being acted on in usb_disconnect() (prompted-by sharp
eyes from Alan).
Cc: Rafael J. Wysocki <rjw@rjwysocki.net>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-05-21 01:09:36 +00:00
|
|
|
if (udev && !retval) {
|
2013-01-22 20:26:30 +00:00
|
|
|
/*
|
2014-05-29 19:58:46 +00:00
|
|
|
* Our preference is to simply wait for the port to reconnect,
|
|
|
|
* as that is the lowest latency method to restart the port.
|
|
|
|
* However, there are cases where toggling port power results in
|
|
|
|
* the host port and the device port getting out of sync causing
|
|
|
|
* a link training live lock. Upon timeout, flag the port as
|
|
|
|
* needing warm reset recovery (to be performed later by
|
|
|
|
* usb_port_resume() as requested via usb_wakeup_notification())
|
2013-01-22 20:26:30 +00:00
|
|
|
*/
|
2014-05-29 19:58:46 +00:00
|
|
|
if (hub_port_debounce_be_connected(hub, port1) < 0) {
|
|
|
|
dev_dbg(&port_dev->dev, "reconnect timeout\n");
|
|
|
|
if (hub_is_superspeed(hdev))
|
|
|
|
set_bit(port1, hub->warm_reset_bits);
|
|
|
|
}
|
usb: resume child device when port is powered on
Unconditionally wake up the child device when the power session is
recovered.
This addresses the following scenarios:
1/ The device may need a reset on power-session loss, without this
change port power-on recovery exposes khubd to scenarios that
usb_port_resume() is set to handle. Prior to port power control the
only time a power session would be lost is during dpm_suspend of the
hub. In that scenario usb_port_resume() is guaranteed to be called
prior to khubd running for that port. With this change we wakeup the
child device as soon as possible (prior to khubd running again for this
port).
Although khubd has facilities to wake a child device it will only do
so if the portstatus / portchange indicates a suspend state. In the
case of port power control we are not coming from a hub-port-suspend
state. This implementation simply uses pm_request_resume() to wake the
device and relies on the port_dev->status_lock to prevent any collisions
between khubd and usb_port_resume().
2/ This mechanism rate limits port power toggling. The minimum port
power on/off period is now gated by the child device suspend/resume
latency. Empirically this mitigates devices downgrading their connection
on perceived instability of the host connection. This ratelimiting is
really only relevant to port power control testing, but it is a nice
side effect of closing the above race. Namely, the race of khubd for
the given port running while a usb_port_resume() event is pending.
3/ Going forward we are finding that power-session recovery requires
warm-resets (http://marc.info/?t=138659232900003&r=1&w=2). This
mechanism allows for warm-resets to be requested at the same point in
the resume path for hub dpm_suspend power session losses, or port
rpm_suspend power session losses.
4/ If the device *was* disconnected the only time we'll know for sure is
after a failed resume, so it's necessary for usb_port_runtime_resume()
to expedite a usb_port_resume() to clean up the removed device. The
reasoning for this is "least surprise" for the user. Turning on a port
means that hotplug detection is again enabled for the port, it is
surprising that devices that were removed while the port was off are not
disconnected until they are attempted to be used. As a user "why would
I try to use a device I removed from the system?"
1, 2, and 4 are not a problem in the system dpm_resume() case because,
although the power-session is lost, khubd is frozen until after device
resume. For the rpm_resume() case pm_request_resume() is used to
request re-validation of the device, and if it happens to collide with a
khubd run we rely on the port_dev->status_lock to synchronize those
operations.
Besides testing, the primary scenario where this mechanism is expected
to be triggered is when the user changes the port power policy
(control/pm_qos_no_poweroff, or power/control). Each time power is
enabled want to revalidate the child device, where the revalidation is
handled by usb_port_resume().
Given that this arranges for port_dev->child to be de-referenced in
usb_port_runtime_resume() we need to make sure not to collide with
usb_disconnect() that frees the usb_device. To this end we hold the
port active with the "child_usage" reference across the disconnect
event. Subsequently, the need to access hub->child_usage_bits lead to
the creation of hub_disconnect_children() to remove any ambiguity of
which "hub" is being acted on in usb_disconnect() (prompted-by sharp
eyes from Alan).
Cc: Rafael J. Wysocki <rjw@rjwysocki.net>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-05-21 01:09:36 +00:00
|
|
|
|
|
|
|
/* Force the child awake to revalidate after the power loss. */
|
|
|
|
if (!test_and_set_bit(port1, hub->child_usage_bits)) {
|
|
|
|
pm_runtime_get_noresume(&port_dev->dev);
|
|
|
|
pm_request_resume(&udev->dev);
|
|
|
|
}
|
2013-01-22 20:26:30 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 20:26:29 +00:00
|
|
|
usb_autopm_put_interface(intf);
|
2014-05-21 01:08:57 +00:00
|
|
|
|
2013-01-22 20:26:29 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_port_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
struct usb_device *hdev = to_usb_device(dev->parent->parent);
|
|
|
|
struct usb_interface *intf = to_usb_interface(dev->parent);
|
2013-01-22 20:26:30 +00:00
|
|
|
struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
|
2014-05-21 01:08:57 +00:00
|
|
|
struct usb_port *peer = port_dev->peer;
|
2013-01-22 20:26:30 +00:00
|
|
|
int port1 = port_dev->portnum;
|
2013-01-22 20:26:29 +00:00
|
|
|
int retval;
|
|
|
|
|
2013-01-22 20:26:30 +00:00
|
|
|
if (!hub)
|
|
|
|
return -EINVAL;
|
2014-05-21 01:08:07 +00:00
|
|
|
if (hub->in_reset)
|
|
|
|
return -EBUSY;
|
2013-01-22 20:26:30 +00:00
|
|
|
|
2013-01-22 20:26:29 +00:00
|
|
|
if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
|
|
|
|
== PM_QOS_FLAGS_ALL)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2014-06-17 23:16:27 +00:00
|
|
|
if (usb_port_block_power_off)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2020-02-26 17:50:36 +00:00
|
|
|
retval = usb_autopm_get_interface(intf);
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
|
2013-06-18 14:28:48 +00:00
|
|
|
retval = usb_hub_set_port_power(hdev, hub, port1, false);
|
2013-01-22 20:26:30 +00:00
|
|
|
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
|
2014-05-21 01:09:10 +00:00
|
|
|
if (!port_dev->is_superspeed)
|
|
|
|
usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
|
2013-01-22 20:26:29 +00:00
|
|
|
usb_autopm_put_interface(intf);
|
2014-05-21 01:08:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our peer usb3 port may now be able to suspend, so
|
|
|
|
* asynchronously queue a suspend request to observe that this
|
|
|
|
* usb2 port is now off.
|
|
|
|
*/
|
|
|
|
if (!port_dev->is_superspeed && peer)
|
|
|
|
pm_runtime_put(&peer->dev);
|
|
|
|
|
2013-01-22 20:26:29 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-05 14:24:12 +00:00
|
|
|
static void usb_port_shutdown(struct device *dev)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
2025-03-19 18:50:51 +00:00
|
|
|
struct usb_device *udev = port_dev->child;
|
2019-08-05 14:24:12 +00:00
|
|
|
|
2025-03-19 18:50:51 +00:00
|
|
|
if (udev && !udev->port_is_suspended) {
|
|
|
|
usb_disable_usb2_hardware_lpm(udev);
|
|
|
|
usb_unlocked_disable_lpm(udev);
|
usb: Disable USB3 LPM at shutdown
JIRA: https://issues.redhat.com/browse/RHEL-37673
commit d920a2ed8620be04a3301e1a9c2b7cc1de65f19d
Author: Kai-Heng Feng <kai.heng.feng@canonical.com>
Date: Tue, 5 Mar 2024 14:51:38 +0800
SanDisks USB3 storage may disapper after system reboot:
usb usb2-port3: link state change
xhci_hcd 0000:00:14.0: clear port3 link state change, portsc: 0x2c0
usb usb2-port3: do warm reset, port only
xhci_hcd 0000:00:14.0: xhci_hub_status_data: stopping usb2 port polling
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x2b0, return 0x2b0
usb usb2-port3: not warm reset yet, waiting 50ms
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x2f0, return 0x2f0
usb usb2-port3: not warm reset yet, waiting 200ms
...
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x6802c0, return 0x7002c0
usb usb2-port3: not warm reset yet, waiting 200ms
xhci_hcd 0000:00:14.0: clear port3 reset change, portsc: 0x4802c0
xhci_hcd 0000:00:14.0: clear port3 warm(BH) reset change, portsc: 0x4002c0
xhci_hcd 0000:00:14.0: clear port3 link state change, portsc: 0x2c0
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x2c0, return 0x2c0
usb usb2-port3: not enabled, trying warm reset again...
This is due to the USB device still cause port change event after xHCI is
shuted down:
xhci_hcd 0000:38:00.0: // Setting command ring address to 0xffffe001
xhci_hcd 0000:38:00.0: xhci_resume: starting usb3 port polling.
xhci_hcd 0000:38:00.0: xhci_hub_status_data: stopping usb4 port polling
xhci_hcd 0000:38:00.0: xhci_hub_status_data: stopping usb3 port polling
xhci_hcd 0000:38:00.0: hcd_pci_runtime_resume: 0
xhci_hcd 0000:38:00.0: xhci_shutdown: stopping usb3 port polling.
xhci_hcd 0000:38:00.0: // Halt the HC
xhci_hcd 0000:38:00.0: xhci_shutdown completed - status = 1
xhci_hcd 0000:00:14.0: xhci_shutdown: stopping usb1 port polling.
xhci_hcd 0000:00:14.0: // Halt the HC
xhci_hcd 0000:00:14.0: xhci_shutdown completed - status = 1
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x1203, return 0x203
xhci_hcd 0000:00:14.0: set port reset, actual port 2-3 status = 0x1311
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x201203, return 0x100203
xhci_hcd 0000:00:14.0: clear port3 reset change, portsc: 0x1203
xhci_hcd 0000:00:14.0: clear port3 warm(BH) reset change, portsc: 0x1203
xhci_hcd 0000:00:14.0: clear port3 link state change, portsc: 0x1203
xhci_hcd 0000:00:14.0: clear port3 connect change, portsc: 0x1203
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x1203, return 0x203
usb 2-3: device not accepting address 2, error -108
xhci_hcd 0000:00:14.0: xHCI dying or halted, can't queue_command
xhci_hcd 0000:00:14.0: Set port 2-3 link state, portsc: 0x1203, write 0x11261
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x1263, return 0x263
xhci_hcd 0000:00:14.0: set port reset, actual port 2-3 status = 0x1271
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x12b1, return 0x2b1
usb usb2-port3: not reset yet, waiting 60ms
ACPI: PM: Preparing to enter system sleep state S5
xhci_hcd 0000:00:14.0: Get port status 2-3 read: 0x12f1, return 0x2f1
usb usb2-port3: not reset yet, waiting 200ms
reboot: Restarting system
The port change event is caused by LPM transition, so disabling LPM at shutdown
to make sure the device is in U0 for warmboot.
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Cc: stable <stable@kernel.org>
Link: https://lore.kernel.org/r/20240305065140.66801-1-kai.heng.feng@canonical.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Desnes Nunes <desnesn@redhat.com>
2024-06-10 17:29:04 +00:00
|
|
|
}
|
2019-08-05 14:24:12 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 20:26:29 +00:00
|
|
|
static const struct dev_pm_ops usb_port_pm_ops = {
|
2014-11-29 22:47:05 +00:00
|
|
|
#ifdef CONFIG_PM
|
2013-01-22 20:26:29 +00:00
|
|
|
.runtime_suspend = usb_port_runtime_suspend,
|
|
|
|
.runtime_resume = usb_port_runtime_resume,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2024-06-10 17:28:39 +00:00
|
|
|
const struct device_type usb_port_device_type = {
|
2013-01-11 12:10:38 +00:00
|
|
|
.name = "usb_port",
|
|
|
|
.release = usb_port_device_release,
|
2013-01-22 20:26:29 +00:00
|
|
|
.pm = &usb_port_pm_ops,
|
2013-01-11 12:10:38 +00:00
|
|
|
};
|
|
|
|
|
2014-05-21 01:08:17 +00:00
|
|
|
static struct device_driver usb_port_driver = {
|
|
|
|
.name = "usb",
|
|
|
|
.owner = THIS_MODULE,
|
2019-08-05 14:24:12 +00:00
|
|
|
.shutdown = usb_port_shutdown,
|
2014-05-21 01:08:17 +00:00
|
|
|
};
|
|
|
|
|
2014-05-21 01:08:45 +00:00
|
|
|
static int link_peers(struct usb_port *left, struct usb_port *right)
|
2014-05-21 01:08:28 +00:00
|
|
|
{
|
2014-05-21 01:08:57 +00:00
|
|
|
struct usb_port *ss_port, *hs_port;
|
2014-05-21 01:08:45 +00:00
|
|
|
int rc;
|
|
|
|
|
2014-05-21 01:08:28 +00:00
|
|
|
if (left->peer == right && right->peer == left)
|
2014-05-21 01:08:45 +00:00
|
|
|
return 0;
|
2014-05-21 01:08:28 +00:00
|
|
|
|
|
|
|
if (left->peer || right->peer) {
|
|
|
|
struct usb_port *lpeer = left->peer;
|
|
|
|
struct usb_port *rpeer = right->peer;
|
2014-06-17 23:16:27 +00:00
|
|
|
char *method;
|
|
|
|
|
|
|
|
if (left->location && left->location == right->location)
|
|
|
|
method = "location";
|
|
|
|
else
|
|
|
|
method = "default";
|
|
|
|
|
2015-12-03 22:26:27 +00:00
|
|
|
pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
|
2014-06-17 23:16:27 +00:00
|
|
|
dev_name(&left->dev), dev_name(&right->dev), method,
|
|
|
|
dev_name(&left->dev),
|
|
|
|
lpeer ? dev_name(&lpeer->dev) : "none",
|
|
|
|
dev_name(&right->dev),
|
|
|
|
rpeer ? dev_name(&rpeer->dev) : "none");
|
2014-05-21 01:08:45 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
|
|
|
|
if (rc) {
|
|
|
|
sysfs_remove_link(&left->dev.kobj, "peer");
|
|
|
|
return rc;
|
2014-05-21 01:08:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:08:57 +00:00
|
|
|
/*
|
|
|
|
* We need to wake the HiSpeed port to make sure we don't race
|
|
|
|
* setting ->peer with usb_port_runtime_suspend(). Otherwise we
|
|
|
|
* may miss a suspend event for the SuperSpeed port.
|
|
|
|
*/
|
|
|
|
if (left->is_superspeed) {
|
|
|
|
ss_port = left;
|
|
|
|
WARN_ON(right->is_superspeed);
|
|
|
|
hs_port = right;
|
|
|
|
} else {
|
|
|
|
ss_port = right;
|
|
|
|
WARN_ON(!right->is_superspeed);
|
|
|
|
hs_port = left;
|
|
|
|
}
|
|
|
|
pm_runtime_get_sync(&hs_port->dev);
|
|
|
|
|
2014-05-21 01:08:28 +00:00
|
|
|
left->peer = right;
|
|
|
|
right->peer = left;
|
2014-05-21 01:08:45 +00:00
|
|
|
|
2014-05-21 01:08:57 +00:00
|
|
|
/*
|
|
|
|
* The SuperSpeed reference is dropped when the HiSpeed port in
|
|
|
|
* this relationship suspends, i.e. when it is safe to allow a
|
|
|
|
* SuperSpeed connection to drop since there is no risk of a
|
|
|
|
* device degrading to its powered-off HiSpeed connection.
|
|
|
|
*
|
|
|
|
* Also, drop the HiSpeed ref taken above.
|
|
|
|
*/
|
|
|
|
pm_runtime_get_sync(&ss_port->dev);
|
|
|
|
pm_runtime_put(&hs_port->dev);
|
|
|
|
|
2014-05-21 01:08:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void link_peers_report(struct usb_port *left, struct usb_port *right)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = link_peers(left, right);
|
|
|
|
if (rc == 0) {
|
|
|
|
dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
|
|
|
|
} else {
|
2015-12-03 22:26:27 +00:00
|
|
|
dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
|
2014-05-21 01:08:45 +00:00
|
|
|
dev_name(&right->dev), rc);
|
|
|
|
pr_warn_once("usb: port power management may be unreliable\n");
|
2014-06-17 23:16:27 +00:00
|
|
|
usb_port_block_power_off = 1;
|
2014-05-21 01:08:45 +00:00
|
|
|
}
|
2014-05-21 01:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unlink_peers(struct usb_port *left, struct usb_port *right)
|
|
|
|
{
|
2014-05-21 01:08:57 +00:00
|
|
|
struct usb_port *ss_port, *hs_port;
|
|
|
|
|
2014-05-21 01:08:28 +00:00
|
|
|
WARN(right->peer != left || left->peer != right,
|
|
|
|
"%s and %s are not peers?\n",
|
|
|
|
dev_name(&left->dev), dev_name(&right->dev));
|
|
|
|
|
2014-05-21 01:08:57 +00:00
|
|
|
/*
|
|
|
|
* We wake the HiSpeed port to make sure we don't race its
|
|
|
|
* usb_port_runtime_resume() event which takes a SuperSpeed ref
|
|
|
|
* when ->peer is !NULL.
|
|
|
|
*/
|
|
|
|
if (left->is_superspeed) {
|
|
|
|
ss_port = left;
|
|
|
|
hs_port = right;
|
|
|
|
} else {
|
|
|
|
ss_port = right;
|
|
|
|
hs_port = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
pm_runtime_get_sync(&hs_port->dev);
|
|
|
|
|
2014-05-21 01:08:45 +00:00
|
|
|
sysfs_remove_link(&left->dev.kobj, "peer");
|
2014-05-21 01:08:28 +00:00
|
|
|
right->peer = NULL;
|
2014-05-21 01:08:45 +00:00
|
|
|
sysfs_remove_link(&right->dev.kobj, "peer");
|
2014-05-21 01:08:28 +00:00
|
|
|
left->peer = NULL;
|
2014-05-21 01:08:57 +00:00
|
|
|
|
|
|
|
/* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
|
|
|
|
pm_runtime_put(&ss_port->dev);
|
|
|
|
|
|
|
|
/* Drop the ref taken above */
|
|
|
|
pm_runtime_put(&hs_port->dev);
|
2014-05-21 01:08:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:08:33 +00:00
|
|
|
/*
|
2014-05-21 01:08:40 +00:00
|
|
|
* For each usb hub device in the system check to see if it is in the
|
|
|
|
* peer domain of the given port_dev, and if it is check to see if it
|
|
|
|
* has a port that matches the given port by location
|
|
|
|
*/
|
|
|
|
static int match_location(struct usb_device *peer_hdev, void *p)
|
|
|
|
{
|
|
|
|
int port1;
|
|
|
|
struct usb_hcd *hcd, *peer_hcd;
|
|
|
|
struct usb_port *port_dev = p, *peer;
|
|
|
|
struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
|
|
|
|
struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
|
|
|
|
|
2024-05-17 15:00:47 +00:00
|
|
|
if (!peer_hub || port_dev->connect_type == USB_PORT_NOT_USED)
|
2014-05-21 01:08:40 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
hcd = bus_to_hcd(hdev->bus);
|
|
|
|
peer_hcd = bus_to_hcd(peer_hdev->bus);
|
|
|
|
/* peer_hcd is provisional until we verify it against the known peer */
|
|
|
|
if (peer_hcd != hcd->shared_hcd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
|
|
|
|
peer = peer_hub->ports[port1 - 1];
|
2024-05-17 15:00:47 +00:00
|
|
|
if (peer && peer->connect_type != USB_PORT_NOT_USED &&
|
|
|
|
peer->location == port_dev->location) {
|
2014-05-21 01:08:45 +00:00
|
|
|
link_peers_report(port_dev, peer);
|
2014-05-21 01:08:40 +00:00
|
|
|
return 1; /* done */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the peer port either via explicit platform firmware "location"
|
|
|
|
* data, the peer hcd for root hubs, or the upstream peer relationship
|
|
|
|
* for all other hubs.
|
2014-05-21 01:08:33 +00:00
|
|
|
*/
|
2014-05-21 01:08:28 +00:00
|
|
|
static void find_and_link_peer(struct usb_hub *hub, int port1)
|
|
|
|
{
|
|
|
|
struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
|
|
|
|
struct usb_device *hdev = hub->hdev;
|
2014-05-21 01:08:33 +00:00
|
|
|
struct usb_device *peer_hdev;
|
|
|
|
struct usb_hub *peer_hub;
|
2014-05-21 01:08:28 +00:00
|
|
|
|
2014-05-21 01:08:40 +00:00
|
|
|
/*
|
|
|
|
* If location data is available then we can only peer this port
|
|
|
|
* by a location match, not the default peer (lest we create a
|
|
|
|
* situation where we need to go back and undo a default peering
|
|
|
|
* when the port is later peered by location data)
|
|
|
|
*/
|
|
|
|
if (port_dev->location) {
|
|
|
|
/* we link the peer in match_location() if found */
|
|
|
|
usb_for_each_dev(port_dev, match_location);
|
|
|
|
return;
|
|
|
|
} else if (!hdev->parent) {
|
2014-05-21 01:08:28 +00:00
|
|
|
struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
|
|
|
|
struct usb_hcd *peer_hcd = hcd->shared_hcd;
|
|
|
|
|
|
|
|
if (!peer_hcd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
peer_hdev = peer_hcd->self.root_hub;
|
2014-05-21 01:08:33 +00:00
|
|
|
} else {
|
|
|
|
struct usb_port *upstream;
|
|
|
|
struct usb_device *parent = hdev->parent;
|
|
|
|
struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
|
|
|
|
|
|
|
|
if (!parent_hub)
|
2014-05-21 01:08:28 +00:00
|
|
|
return;
|
|
|
|
|
2014-05-21 01:08:33 +00:00
|
|
|
upstream = parent_hub->ports[hdev->portnum - 1];
|
|
|
|
if (!upstream || !upstream->peer)
|
|
|
|
return;
|
2014-05-21 01:08:28 +00:00
|
|
|
|
2014-05-21 01:08:33 +00:00
|
|
|
peer_hdev = upstream->peer->child;
|
2014-05-21 01:08:28 +00:00
|
|
|
}
|
2014-05-21 01:08:33 +00:00
|
|
|
|
|
|
|
peer_hub = usb_hub_to_struct_hub(peer_hdev);
|
|
|
|
if (!peer_hub || port1 > peer_hdev->maxchild)
|
|
|
|
return;
|
|
|
|
|
2014-05-21 01:08:40 +00:00
|
|
|
/*
|
|
|
|
* we found a valid default peer, last check is to make sure it
|
|
|
|
* does not have location data
|
|
|
|
*/
|
2014-05-21 01:08:33 +00:00
|
|
|
peer = peer_hub->ports[port1 - 1];
|
2014-05-21 01:08:40 +00:00
|
|
|
if (peer && peer->location == 0)
|
2014-05-21 01:08:45 +00:00
|
|
|
link_peers_report(port_dev, peer);
|
2014-05-21 01:08:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 16:32:58 +00:00
|
|
|
static int connector_bind(struct device *dev, struct device *connector, void *data)
|
|
|
|
{
|
2024-04-24 22:57:01 +00:00
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
2022-05-02 16:32:58 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev));
|
2024-04-24 22:57:01 +00:00
|
|
|
if (ret) {
|
2022-05-02 16:32:58 +00:00
|
|
|
sysfs_remove_link(&dev->kobj, "connector");
|
2024-04-24 22:57:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_dev->connector = data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is already USB device connected to the port, letting the
|
|
|
|
* Type-C connector know about it immediately.
|
|
|
|
*/
|
|
|
|
if (port_dev->child)
|
|
|
|
typec_attach(port_dev->connector, &port_dev->child->dev);
|
2022-05-02 16:32:58 +00:00
|
|
|
|
2024-04-24 22:57:01 +00:00
|
|
|
return 0;
|
2022-05-02 16:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void connector_unbind(struct device *dev, struct device *connector, void *data)
|
|
|
|
{
|
2024-04-24 22:57:01 +00:00
|
|
|
struct usb_port *port_dev = to_usb_port(dev);
|
|
|
|
|
2022-05-02 16:32:58 +00:00
|
|
|
sysfs_remove_link(&connector->kobj, dev_name(dev));
|
|
|
|
sysfs_remove_link(&dev->kobj, "connector");
|
2024-04-24 22:57:01 +00:00
|
|
|
port_dev->connector = NULL;
|
2022-05-02 16:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct component_ops connector_ops = {
|
|
|
|
.bind = connector_bind,
|
|
|
|
.unbind = connector_unbind,
|
|
|
|
};
|
|
|
|
|
2013-01-11 12:10:38 +00:00
|
|
|
int usb_hub_create_port_device(struct usb_hub *hub, int port1)
|
|
|
|
{
|
2014-05-21 01:08:28 +00:00
|
|
|
struct usb_port *port_dev;
|
2015-11-14 08:26:33 +00:00
|
|
|
struct usb_device *hdev = hub->hdev;
|
2013-01-11 12:10:38 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
|
2014-06-17 23:16:32 +00:00
|
|
|
if (!port_dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
|
|
|
|
if (!port_dev->req) {
|
|
|
|
kfree(port_dev);
|
|
|
|
return -ENOMEM;
|
2013-01-11 12:10:38 +00:00
|
|
|
}
|
|
|
|
|
usb: core: Set connect_type of ports based on DT node
JIRA: https://issues.redhat.com/browse/RHEL-37673
commit 82e82130a78b75a9ce5225df24d5a0b1b3290eb0
Author: Stephen Boyd <swboyd@chromium.org>
Date: Thu, 22 Feb 2024 16:58:21 -0800
When a USB hub is described in DT, such as any device that matches the
onboard-hub driver, the connect_type is set to "unknown" or
USB_PORT_CONNECT_TYPE_UNKNOWN. This makes any device plugged into that
USB port report their 'removable' device attribute as "unknown".
ChromeOS userspace would like to know if the USB device is actually
removable or not so that security policies can be applied. Improve the
connect_type attribute for ports, and in turn the removable attribute
for USB devices, by looking for child devices with a reg property or an
OF graph when the device is described in DT.
If the graph exists, endpoints that are connected to a remote node must
be something like a usb-{a,b,c}-connector compatible node, or an
intermediate node like a redriver, and not a hardwired USB device on the
board. Set the connect_type to USB_PORT_CONNECT_TYPE_HOT_PLUG in this
case because the device is going to be plugged in. Set the connect_type
to USB_PORT_CONNECT_TYPE_HARD_WIRED if there's a child node for the port
like 'device@2' for port2. Set the connect_type to USB_PORT_NOT_USED if
there isn't an endpoint or child node corresponding to the port number.
To make sure things don't change, only set the port to not used if
there are child nodes. This way an onboard hub connect_type doesn't
change until ports are added or child nodes are added to describe
hardwired devices. It's assumed that all ports or no ports will be
described for a device.
Cc: Matthias Kaehlcke <mka@chromium.org>
Cc: linux-usb@vger.kernel.org
Cc: devicetree@vger.kernel.org
Cc: Pin-yen Lin <treapking@chromium.org>
Cc: maciek swiech <drmasquatch@google.com>
Signed-off-by: Stephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20240223005823.3074029-3-swboyd@chromium.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Desnes Nunes <desnesn@redhat.com>
2024-06-10 17:28:56 +00:00
|
|
|
port_dev->connect_type = usb_of_get_connect_type(hdev, port1);
|
2013-01-11 12:10:38 +00:00
|
|
|
hub->ports[port1 - 1] = port_dev;
|
2013-01-22 20:26:29 +00:00
|
|
|
port_dev->portnum = port1;
|
2014-05-21 01:08:52 +00:00
|
|
|
set_bit(port1, hub->power_bits);
|
2013-01-11 12:10:38 +00:00
|
|
|
port_dev->dev.parent = hub->intfdev;
|
2015-11-14 08:26:33 +00:00
|
|
|
if (hub_is_superspeed(hdev)) {
|
2024-04-24 22:57:33 +00:00
|
|
|
port_dev->is_superspeed = 1;
|
2015-11-14 08:26:33 +00:00
|
|
|
port_dev->usb3_lpm_u1_permit = 1;
|
|
|
|
port_dev->usb3_lpm_u2_permit = 1;
|
|
|
|
port_dev->dev.groups = port_dev_usb3_group;
|
|
|
|
} else
|
|
|
|
port_dev->dev.groups = port_dev_group;
|
2013-01-11 12:10:38 +00:00
|
|
|
port_dev->dev.type = &usb_port_device_type;
|
2014-05-21 01:08:17 +00:00
|
|
|
port_dev->dev.driver = &usb_port_driver;
|
|
|
|
dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
|
|
|
|
port1);
|
2014-05-21 01:09:26 +00:00
|
|
|
mutex_init(&port_dev->status_lock);
|
2013-01-11 12:10:38 +00:00
|
|
|
retval = device_register(&port_dev->dev);
|
2014-06-17 23:16:32 +00:00
|
|
|
if (retval) {
|
|
|
|
put_device(&port_dev->dev);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2023-11-21 14:58:56 +00:00
|
|
|
port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state");
|
|
|
|
if (!port_dev->state_kn) {
|
|
|
|
dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n");
|
|
|
|
retval = -ENODEV;
|
|
|
|
goto err_unregister;
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:16:32 +00:00
|
|
|
/* Set default policy of port-poweroff disabled. */
|
|
|
|
retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
|
|
|
|
DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
|
|
|
|
if (retval < 0) {
|
2023-11-21 14:58:56 +00:00
|
|
|
goto err_put_kn;
|
2014-06-17 23:16:32 +00:00
|
|
|
}
|
2013-01-11 12:10:38 +00:00
|
|
|
|
2022-05-02 16:32:58 +00:00
|
|
|
retval = component_add(&port_dev->dev, &connector_ops);
|
2022-05-02 16:33:07 +00:00
|
|
|
if (retval) {
|
2022-05-02 16:32:58 +00:00
|
|
|
dev_warn(&port_dev->dev, "failed to add component\n");
|
2023-11-21 14:58:56 +00:00
|
|
|
goto err_put_kn;
|
2022-05-02 16:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
find_and_link_peer(hub, port1);
|
2022-05-02 16:32:58 +00:00
|
|
|
|
2014-06-17 23:16:32 +00:00
|
|
|
/*
|
|
|
|
* Enable runtime pm and hold a refernce that hub_configure()
|
|
|
|
* will drop once the PM_QOS_NO_POWER_OFF flag state has been set
|
|
|
|
* and the hub has been fully registered (hdev->maxchild set).
|
|
|
|
*/
|
2013-01-22 20:26:29 +00:00
|
|
|
pm_runtime_set_active(&port_dev->dev);
|
2014-06-17 23:16:32 +00:00
|
|
|
pm_runtime_get_noresume(&port_dev->dev);
|
|
|
|
pm_runtime_enable(&port_dev->dev);
|
|
|
|
device_enable_async_suspend(&port_dev->dev);
|
2013-01-22 20:26:31 +00:00
|
|
|
|
2014-05-21 01:08:12 +00:00
|
|
|
/*
|
2014-06-17 23:16:32 +00:00
|
|
|
* Keep hidden the ability to enable port-poweroff if the hub
|
|
|
|
* does not support power switching.
|
2013-01-22 20:26:31 +00:00
|
|
|
*/
|
2014-06-17 23:16:32 +00:00
|
|
|
if (!hub_is_port_power_switchable(hub))
|
|
|
|
return 0;
|
2013-01-22 20:26:31 +00:00
|
|
|
|
2014-06-17 23:16:32 +00:00
|
|
|
/* Attempt to let userspace take over the policy. */
|
|
|
|
retval = dev_pm_qos_expose_flags(&port_dev->dev,
|
|
|
|
PM_QOS_FLAG_NO_POWER_OFF);
|
|
|
|
if (retval < 0) {
|
|
|
|
dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-11 12:10:38 +00:00
|
|
|
|
2014-06-17 23:16:32 +00:00
|
|
|
/* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
|
|
|
|
retval = dev_pm_qos_remove_request(port_dev->req);
|
|
|
|
if (retval >= 0) {
|
|
|
|
kfree(port_dev->req);
|
|
|
|
port_dev->req = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-11-21 14:58:56 +00:00
|
|
|
|
|
|
|
err_put_kn:
|
|
|
|
sysfs_put(port_dev->state_kn);
|
|
|
|
err_unregister:
|
|
|
|
device_unregister(&port_dev->dev);
|
|
|
|
|
|
|
|
return retval;
|
2013-01-11 12:10:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:08:28 +00:00
|
|
|
void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
|
2013-01-11 12:10:38 +00:00
|
|
|
{
|
2014-05-21 01:08:28 +00:00
|
|
|
struct usb_port *port_dev = hub->ports[port1 - 1];
|
|
|
|
struct usb_port *peer;
|
2013-01-11 12:10:38 +00:00
|
|
|
|
2014-05-21 01:08:28 +00:00
|
|
|
peer = port_dev->peer;
|
|
|
|
if (peer)
|
|
|
|
unlink_peers(port_dev, peer);
|
2022-05-02 16:32:58 +00:00
|
|
|
component_del(&port_dev->dev, &connector_ops);
|
2023-11-21 14:58:56 +00:00
|
|
|
sysfs_put(port_dev->state_kn);
|
2014-05-21 01:08:28 +00:00
|
|
|
device_unregister(&port_dev->dev);
|
|
|
|
}
|