diff mbox series

[1/3] thunderbolt: Add details to router uevent

Message ID 20210309134818.63118-2-mika.westerberg@linux.intel.com (mailing list archive)
State Superseded
Headers show
Series thunderbolt: Expose details about tunneling | expand

Commit Message

Mika Westerberg March 9, 2021, 1:48 p.m. UTC
Expose two environment variables for routers as part of the initial
uevent:

  USB4_VERSION=1.0
  USB4_TYPE=host|device|hub

Userspace can use this information to expose more details about each
connected device. Only USB4 devices have USB4_VERSION but all devices
have USB4_TYPE.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/thunderbolt/switch.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Greg Kroah-Hartman March 9, 2021, 2:10 p.m. UTC | #1
On Tue, Mar 09, 2021 at 04:48:16PM +0300, Mika Westerberg wrote:
> Expose two environment variables for routers as part of the initial
> uevent:
> 
>   USB4_VERSION=1.0
>   USB4_TYPE=host|device|hub
> 
> Userspace can use this information to expose more details about each
> connected device. Only USB4 devices have USB4_VERSION but all devices
> have USB4_TYPE.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Limonciello, Mario March 10, 2021, 5:28 p.m. UTC | #2
> -----Original Message-----
> From: Mika Westerberg <mika.westerberg@linux.intel.com>
> Sent: Tuesday, March 9, 2021 7:48
> To: linux-usb@vger.kernel.org
> Cc: Michael Jamet; Yehezkel Bernat; Andreas Noever; Lukas Wunner; Limonciello,
> Mario; Christian Kellner; Benson Leung; Prashant Malani; Diego Rivas; Greg
> Kroah-Hartman; Mika Westerberg
> Subject: [PATCH 1/3] thunderbolt: Add details to router uevent
> 
> 
> [EXTERNAL EMAIL]
> 
> Expose two environment variables for routers as part of the initial
> uevent:
> 
>   USB4_VERSION=1.0
>   USB4_TYPE=host|device|hub

Presumably this will then show up in the uevent like this for a host controller:
DEVTYPE=thunderbolt_device
USB4_VERSION=1.0
USB4_TYPE=host

Since it's specifically for USB4, how about if you instead have new devtypes?
TBT3:
DEVTYPE=thunderbolt_device

USB4:
DEVTYPE=usb4_host|usb4_device|usb4_hub

That would at least make it clearer to userspace to make a delineation if it's
legacy device or not.   I don't know if that's actually valuable information however.

> 
> Userspace can use this information to expose more details about each
> connected device. Only USB4 devices have USB4_VERSION but all devices
> have USB4_TYPE.
> 
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> ---
>  drivers/thunderbolt/switch.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
> 
> diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
> index 72b43c7c0651..a82032c081e8 100644
> --- a/drivers/thunderbolt/switch.c
> +++ b/drivers/thunderbolt/switch.c
> @@ -1827,6 +1827,39 @@ static void tb_switch_release(struct device *dev)
>  	kfree(sw);
>  }
> 
> +static int tb_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
> +{
> +	struct tb_switch *sw = tb_to_switch(dev);
> +	const char *type;
> +
> +	if (sw->config.thunderbolt_version == USB4_VERSION_1_0) {
> +		if (add_uevent_var(env, "USB4_VERSION=1.0"))
> +			return -ENOMEM;
> +	}
> +
> +	if (!tb_route(sw)) {
> +		type = "host";
> +	} else {
> +		const struct tb_port *port;
> +		bool hub = false;
> +
> +		/* Device is hub if it has any downstream ports */
> +		tb_switch_for_each_port(sw, port) {
> +			if (!port->disabled && !tb_is_upstream_port(port) &&
> +			     tb_port_is_null(port)) {
> +				hub = true;
> +				break;
> +			}
> +		}
> +
> +		type = hub ? "hub" : "device";
> +	}
> +
> +	if (add_uevent_var(env, "USB4_TYPE=%s", type))
> +		return -ENOMEM;
> +	return 0;
> +}
> +
>  /*
>   * Currently only need to provide the callbacks. Everything else is handled
>   * in the connection manager.
> @@ -1860,6 +1893,7 @@ static const struct dev_pm_ops tb_switch_pm_ops = {
>  struct device_type tb_switch_type = {
>  	.name = "thunderbolt_device",
>  	.release = tb_switch_release,
> +	.uevent = tb_switch_uevent,
>  	.pm = &tb_switch_pm_ops,
>  };
> 
> --
> 2.30.1
Mika Westerberg March 11, 2021, 7:59 a.m. UTC | #3
Hi Mario,

On Wed, Mar 10, 2021 at 05:28:19PM +0000, Limonciello, Mario wrote:
> 
> 
> > -----Original Message-----
> > From: Mika Westerberg <mika.westerberg@linux.intel.com>
> > Sent: Tuesday, March 9, 2021 7:48
> > To: linux-usb@vger.kernel.org
> > Cc: Michael Jamet; Yehezkel Bernat; Andreas Noever; Lukas Wunner; Limonciello,
> > Mario; Christian Kellner; Benson Leung; Prashant Malani; Diego Rivas; Greg
> > Kroah-Hartman; Mika Westerberg
> > Subject: [PATCH 1/3] thunderbolt: Add details to router uevent
> > 
> > 
> > [EXTERNAL EMAIL]
> > 
> > Expose two environment variables for routers as part of the initial
> > uevent:
> > 
> >   USB4_VERSION=1.0
> >   USB4_TYPE=host|device|hub
> 
> Presumably this will then show up in the uevent like this for a host controller:
> DEVTYPE=thunderbolt_device
> USB4_VERSION=1.0
> USB4_TYPE=host
> 
> Since it's specifically for USB4, how about if you instead have new devtypes?
> TBT3:
> DEVTYPE=thunderbolt_device
> 
> USB4:
> DEVTYPE=usb4_host|usb4_device|usb4_hub
> 
> That would at least make it clearer to userspace to make a delineation if it's
> legacy device or not.   I don't know if that's actually valuable information however.

Unfortunately we can't do that. DEVTYPE is generated by the driver core
based on the struct device_type we register for routers (switches). Also
bolt, and I think fwupd too, already use DEVTYPE to distinguish routers
from other devices (like XDomains etc).
diff mbox series

Patch

diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 72b43c7c0651..a82032c081e8 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -1827,6 +1827,39 @@  static void tb_switch_release(struct device *dev)
 	kfree(sw);
 }
 
+static int tb_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
+{
+	struct tb_switch *sw = tb_to_switch(dev);
+	const char *type;
+
+	if (sw->config.thunderbolt_version == USB4_VERSION_1_0) {
+		if (add_uevent_var(env, "USB4_VERSION=1.0"))
+			return -ENOMEM;
+	}
+
+	if (!tb_route(sw)) {
+		type = "host";
+	} else {
+		const struct tb_port *port;
+		bool hub = false;
+
+		/* Device is hub if it has any downstream ports */
+		tb_switch_for_each_port(sw, port) {
+			if (!port->disabled && !tb_is_upstream_port(port) &&
+			     tb_port_is_null(port)) {
+				hub = true;
+				break;
+			}
+		}
+
+		type = hub ? "hub" : "device";
+	}
+
+	if (add_uevent_var(env, "USB4_TYPE=%s", type))
+		return -ENOMEM;
+	return 0;
+}
+
 /*
  * Currently only need to provide the callbacks. Everything else is handled
  * in the connection manager.
@@ -1860,6 +1893,7 @@  static const struct dev_pm_ops tb_switch_pm_ops = {
 struct device_type tb_switch_type = {
 	.name = "thunderbolt_device",
 	.release = tb_switch_release,
+	.uevent = tb_switch_uevent,
 	.pm = &tb_switch_pm_ops,
 };