thunderbolt: Add Thunderbolt/USB4 <-> USB3 match function
authorAlan Borzeszkowski <alan.borzeszkowski@linux.intel.com>
Mon, 14 Apr 2025 17:55:53 +0000 (19:55 +0200)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Thu, 17 Apr 2025 09:21:45 +0000 (12:21 +0300)
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.

Signed-off-by: Alan Borzeszkowski <alan.borzeszkowski@linux.intel.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/usb4_port.c
include/linux/thunderbolt.h

index 5150879888cac7f2bfa8693fe43b4116829b6f61..852a45fcd19d12551fbeaa2591d5e74fde93ddd8 100644 (file)
@@ -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);
 }
 
index 7d902d8c054b288eb7393a056dcaca893d47be57..75247486616bb7aed26442fc69a0d537b0cccd41 100644 (file)
 #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_ */