Skip to contentSkip to conversation

Introduce Thunderbolt/USB4 <-> USB Type-C port mapping

PATCH In review Alan Borzeszkowski 4 participants 10 messages Apr 14, 2025 – Apr 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Introduce Thunderbolt/USB4 <-> USB Type-C port mapping

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

Alan Borzeszkowski (3):
thunderbolt: Expose usb4_port_index() to other modules
thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function
usb: typec: Connect Type-C port with associated USB4 port

 drivers/thunderbolt/tb.h        |  1 +
 drivers/thunderbolt/usb4.c      | 14 +++++++--
 drivers/thunderbolt/usb4_port.c | 56 ++++++++++++++++++++++++++++-----
 drivers/usb/typec/port-mapper.c | 23 ++++++++++++--
 include/linux/thunderbolt.h     | 18 +++++++++++
 5 files changed, 99 insertions(+), 13 deletions(-)

base-commit: 306a14d03f47fa152c7e47be544d8d582d387a20

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 thunderbolt: Expose usb4_port_index() to other modules

Make usb4_port_index() available to other files in the driver, rename and add function documentation.

---
 drivers/thunderbolt/tb.h   |  1 +
 drivers/thunderbolt/usb4.c | 14 +++++++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/thunderbolt/tb.h +1 -0
index da2d7c1f165b..82d39eb68cd0 100644
@@ -1481,6 +1481,7 @@ static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
 struct usb4_port *usb4_port_device_add(struct tb_port *port);
 void usb4_port_device_remove(struct usb4_port *usb4);
 int usb4_port_device_resume(struct usb4_port *usb4);
+int usb4_port_index(const struct tb_switch *sw, const struct tb_port *port);
 
 static inline bool usb4_port_device_is_offline(const struct usb4_port *usb4)
 {
drivers/thunderbolt/usb4.c +11 -3
index 1b740d7fc7da..901ff57ad596 100644
@@ -935,7 +935,15 @@ int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
 	return status ? -EIO : 0;
 }
 
-static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
+/**
+ * usb4_port_index() - Finds matching USB4 port index
+ * @sw: USB4 router
+ * @port: USB4 protocol or lane adapter
+ *
+ * Finds matching USB4 port index (starting from %0) that given @port goes
+ * through.
+ */
+int usb4_port_index(const struct tb_switch *sw, const struct tb_port *port)
 {
 	struct tb_port *p;
 	int usb4_idx = 0;
@@ -969,7 +977,7 @@ static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
 					  const struct tb_port *port)
 {
-	int usb4_idx = usb4_port_idx(sw, port);
+	int usb4_idx = usb4_port_index(sw, port);
 	struct tb_port *p;
 	int pcie_idx = 0;
 
@@ -1000,7 +1008,7 @@ struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
 					  const struct tb_port *port)
 {
-	int usb4_idx = usb4_port_idx(sw, port);
+	int usb4_idx = usb4_port_index(sw, port);
 	struct tb_port *p;
 	int usb_idx = 0;
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function

In reply to Alan Borzeszkowski

This function checks whether given USB4 port device matches with USB3.x port device, using ACPI _DSD property.

It is designed to be used by component framework to match USB4 ports with Type-C ports they are connected to.

Also, added USB4 config stub in case mapping function is not reachable.

---
 drivers/thunderbolt/usb4_port.c | 56 ++++++++++++++++++++++++++++-----
 include/linux/thunderbolt.h     | 18 +++++++++++
 2 files changed, 66 insertions(+), 8 deletions(-)
drivers/thunderbolt/usb4_port.c +48 -8
index 5150879888ca..852a45fcd19d 100644
@@ -105,6 +105,49 @@ static void usb4_port_online(struct usb4_port *usb4)
 	tb_acpi_power_off_retimers(port);
 }
 
+/**
+ * usb4_usb3_port_match() - Matches USB4 port device with USB 3.x port device
+ * @usb4_port_dev: USB4 port device
+ * @usb3_port_fwnode: USB 3.x port firmware node
+ *
+ * Checks if USB 3.x port @usb3_port_fwnode is tunneled through USB4 port @usb4_port_dev.
+ * Returns true if match is found, false otherwise.
+ *
+ * Function is designed to be used with component framework (component_match_add).
+ */
+bool usb4_usb3_port_match(struct device *usb4_port_dev,
+			  const struct fwnode_handle *usb3_port_fwnode)
+{
+	struct fwnode_handle *nhi_fwnode __free(fwnode_handle) = NULL;
+	struct usb4_port *usb4;
+	struct tb_switch *sw;
+	struct tb_nhi *nhi;
+	u8 usb4_port_num;
+	struct tb *tb;
+
+	usb4 = tb_to_usb4_port_device(usb4_port_dev);
+	if (!usb4)
+		return false;
+
+	sw = usb4->port->sw;
+	tb = sw->tb;
+	nhi = tb->nhi;
+
+	nhi_fwnode = fwnode_find_reference(usb3_port_fwnode, "usb4-host-interface", 0);
+	if (IS_ERR(nhi_fwnode))
+		return false;
+
+	/* Check if USB3 fwnode references same NHI where USB4 port resides */
+	if (!device_match_fwnode(&nhi->pdev->dev, nhi_fwnode))
+		return false;
+
+	if (fwnode_property_read_u8(usb3_port_fwnode, "usb4-port-number", &usb4_port_num))
+		return false;
+
+	return usb4_port_index(sw, usb4->port) == usb4_port_num;
+}
+EXPORT_SYMBOL_GPL(usb4_usb3_port_match);
+
 static ssize_t offline_show(struct device *dev,
 	struct device_attribute *attr, char *buf)
 {
@@ -276,12 +319,10 @@ struct usb4_port *usb4_port_device_add(struct tb_port *port)
 		return ERR_PTR(ret);
 	}
 
-	if (dev_fwnode(&usb4->dev)) {
-		ret = component_add(&usb4->dev, &connector_ops);
-		if (ret) {
-			dev_err(&usb4->dev, "failed to add component\n");
-			device_unregister(&usb4->dev);
-		}
+	ret = component_add(&usb4->dev, &connector_ops);
+	if (ret) {
+		dev_err(&usb4->dev, "failed to add component\n");
+		device_unregister(&usb4->dev);
 	}
 
 	if (!tb_is_upstream_port(port))
@@ -306,8 +347,7 @@ struct usb4_port *usb4_port_device_add(struct tb_port *port)
  */
 void usb4_port_device_remove(struct usb4_port *usb4)
 {
-	if (dev_fwnode(&usb4->dev))
-		component_del(&usb4->dev, &connector_ops);
+	component_del(&usb4->dev, &connector_ops);
 	device_unregister(&usb4->dev);
 }
 
include/linux/thunderbolt.h +18 -0
index 7d902d8c054b..75247486616b 100644
@@ -11,6 +11,13 @@
 #ifndef THUNDERBOLT_H_
 #define THUNDERBOLT_H_
 
+#include <linux/types.h>
+
+struct fwnode_handle;
+struct device;
+
+#if IS_REACHABLE(CONFIG_USB4)
+
 #include <linux/device.h>
 #include <linux/idr.h>
 #include <linux/list.h>
@@ -674,4 +681,15 @@ static inline struct device *tb_ring_dma_device(struct tb_ring *ring)
 	return &ring->nhi->pdev->dev;
 }
 
+bool usb4_usb3_port_match(struct device *usb4_port_dev,
+			  const struct fwnode_handle *usb3_port_fwnode);
+
+#else /* CONFIG_USB4 */
+static inline bool usb4_usb3_port_match(struct device *usb4_port_dev,
+					const struct fwnode_handle *usb3_port_fwnode)
+{
+	return false;
+}
+#endif /* CONFIG_USB4 */
+
 #endif /* THUNDERBOLT_H_ */
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 usb: typec: Connect Type-C port with associated USB4 port

In reply to Alan Borzeszkowski

If USB3.x device references USB4 host interface, USB4 port can be connected with appropriate Type-C port. By using component framework, and in turn by creating symlinks, userspace can benefit from having Thunderbolt/USB4 connection to Type-C ports.

Note: This change introduces dependency on Thunderbolt driver as it's required to properly map USB4 port to Type-C port.

---
 drivers/usb/typec/port-mapper.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/usb/typec/port-mapper.c +21 -2
index d42da5720a25..cdbb7c11d714 100644
@@ -8,6 +8,7 @@
 
 #include <linux/acpi.h>
 #include <linux/component.h>
+#include <linux/thunderbolt.h>
 #include <linux/usb.h>
 
 #include "class.h"
@@ -36,6 +37,11 @@ struct each_port_arg {
 	struct component_match *match;
 };
 
+static int usb4_port_compare(struct device *dev, void *fwnode)
+{
+	return usb4_usb3_port_match(dev, fwnode);
+}
+
 static int typec_port_compare(struct device *dev, void *fwnode)
 {
 	return device_match_fwnode(dev, fwnode);
@@ -51,9 +57,22 @@ static int typec_port_match(struct device *dev, void *data)
 	if (con_adev == adev)
 		return 0;
 
-	if (con_adev->pld_crc == adev->pld_crc)
+	if (con_adev->pld_crc == adev->pld_crc)	{
+		struct fwnode_handle *adev_fwnode = acpi_fwnode_handle(adev);
+
 		component_match_add(&arg->port->dev, &arg->match, typec_port_compare,
-				    acpi_fwnode_handle(adev));
+				    adev_fwnode);
+
+		/*
+		 * If dev is USB 3.x port, it may have reference to the
+		 * USB4 host interface in which case we can also link the
+		 * Type-C port with the USB4 port.
+		 */
+		if (fwnode_property_present(adev_fwnode, "usb4-host-interface"))
+			component_match_add(&arg->port->dev, &arg->match,
+					    usb4_port_compare, adev_fwnode);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to Alan Borzeszkowski
Quoted 7 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

"mapping" in what way? sysfs links? If so, care to add Documentation/ABI/ updates?

thanks,

greg k-h

maintainer
#5 lore raw

Hi Greg,

Quoted 11 lines · Greg KH
Quoted 7 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

"mapping" in what way? sysfs links? If so, care to add Documentation/ABI/ updates?

It is already there:
https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-bus-thunderbolt-devices-usb4-portx-connector

The link is already created on Cromebooks like Alan explained, because there each USB4/TBT port has a device node in the ACPI tables with the appropriate _PLD (Physical Location of Device), but on systems targeted primarily for Windows, that is not the case. There ports do not have device nodes, so we can't make the link based on _PLD.

This series makes that same link by taking advantage of the "usb4-host-interface" _DSD device property [1] that we always have on those Windows platforms. That same device property that we also use in drivers/usb/code/usb-acpi.c, and probable some other places too.

[1] https://www.usb.org/sites/default/files/D1T2-2%20-%20USB4%20on%20Windows.pdf

thanks,

Signature · 1 line
heikki
maintainer
#6 lore raw
Quoted 24 lines · Heikki Krogerus

Hi Greg,

Quoted 11 lines · Greg KH
Quoted 7 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

"mapping" in what way? sysfs links? If so, care to add Documentation/ABI/ updates?

It is already there:
https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-bus-thunderbolt-devices-usb4-portx-connector

The link is already created on Cromebooks like Alan explained, because there each USB4/TBT port has a device node in the ACPI tables with the appropriate _PLD (Physical Location of Device), but on systems targeted primarily for Windows, that is not the case. There ports do not have device nodes, so we can't make the link based on _PLD.

Quoted 6 lines

This series makes that same link by taking advantage of the "usb4-host-interface" _DSD device property [1] that we always have on those Windows platforms. That same device property that we also use in drivers/usb/code/usb-acpi.c, and probable some other places too.

[1] https://www.usb.org/sites/default/files/D1T2-2%20-%20USB4%20on%20Windows.pdf

Thanks for the information, sorry I had missed that it was already there.

greg k-h

maintainer
#7 lore raw
In reply to Alan Borzeszkowski
Quoted 21 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

Alan Borzeszkowski (3):
thunderbolt: Expose usb4_port_index() to other modules
thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function
usb: typec: Connect Type-C port with associated USB4 port

 drivers/thunderbolt/tb.h        |  1 +
 drivers/thunderbolt/usb4.c      | 14 +++++++--
 drivers/thunderbolt/usb4_port.c | 56 ++++++++++++++++++++++++++++-----
 drivers/usb/typec/port-mapper.c | 23 ++++++++++++--
 include/linux/thunderbolt.h     | 18 +++++++++++
 5 files changed, 99 insertions(+), 13 deletions(-)
Quoted 5 lines

base-commit: 306a14d03f47fa152c7e47be544d8d582d387a20

Signature · 1 line
2.43.0

I'm guessing that these will come through the thunderbolt tree to me?

#8 lore raw
Quoted 22 lines · Greg KH
Quoted 21 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

Alan Borzeszkowski (3):
thunderbolt: Expose usb4_port_index() to other modules
thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function
usb: typec: Connect Type-C port with associated USB4 port

 drivers/thunderbolt/tb.h        |  1 +
 drivers/thunderbolt/usb4.c      | 14 +++++++--
 drivers/thunderbolt/usb4_port.c | 56 ++++++++++++++++++++++++++++-----
 drivers/usb/typec/port-mapper.c | 23 ++++++++++++--
 include/linux/thunderbolt.h     | 18 +++++++++++
 5 files changed, 99 insertions(+), 13 deletions(-)
Quoted 7 lines
Quoted 5 lines

base-commit: 306a14d03f47fa152c7e47be544d8d582d387a20

Signature · 1 line
2.43.0

I'm guessing that these will come through the thunderbolt tree to me?

Yes, I will pick them up and send to you with the rest of the v6.16 stuff.

#9 lore raw
In reply to Alan Borzeszkowski
Quoted 8 lines · Alan Borzeszkowski

Hello everyone,

This patch series introduces the creation of symlinks between Thunderbolt/USB4 ports and their corresponding USB Type-C ports. The primary goal is to provide users with clear visibility into which USB4 port is connected via a specific Type-C port. This provides the same functionality that is already present in Chromebooks.

Quoted 4 lines

Alan Borzeszkowski (3):
thunderbolt: Expose usb4_port_index() to other modules
thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function
usb: typec: Connect Type-C port with associated USB4 port

All applied to thunderbolt.git/next, thanks!