[PATCH 1/4] usb: typec: add typec_port_from_device()

Giandomenico Rossi rossi at amelchem.com
Sat Nov 14 08:19:50 PST 2026


Add an exported helper to retrieve the struct typec_port associated
with a struct device.

This allows callers holding a Type-C port device to obtain the
corresponding typec_port handle, whose definition is private to
drivers/usb/typec/class.c.

Reviewed-by: Nicola Fontana <ntd at entidi.it>
Signed-off-by: Giandomenico Rossi <rossi at amelchem.com>
---
 drivers/usb/typec/class.c | 21 +++++++++++++++++++++
 include/linux/usb/typec.h |  2 ++
 2 files changed, 23 insertions(+)

diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index e03eaccbb9..e13699719b 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -83,6 +83,27 @@ int typec_set_role(struct typec_port *port, enum usb_role role)
 }
 EXPORT_SYMBOL_GPL(typec_set_role);
 
+
+/**
+ * typec_port_from_device() - get the typec_port embedding a struct device
+ * @dev: device backing a registered Type-C port
+ *
+ * Resolves the struct typec_port that contains @dev. Intended for callers
+ * that hold a struct device (e.g. from get_device_by_name()) and need the
+ * opaque typec_port handle, which they cannot obtain themselves since the
+ * type is private to this file.
+ *
+ * The caller must ensure @dev actually backs a Type-C port; passing an
+ * unrelated device yields an invalid pointer.
+ *
+ * Return: the struct typec_port containing @dev.
+ */
+struct typec_port *typec_port_from_device(struct device *dev)
+{
+	return container_of(dev, struct typec_port, dev);
+}
+EXPORT_SYMBOL(typec_port_from_device);
+
 /**
  * typec_get_drvdata - Return private driver data pointer
  * @port: USB Type-C port
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 315dee95e4..2de110d592 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -51,4 +51,6 @@ int typec_set_role(struct typec_port *port, enum usb_role role);
 
 void *typec_get_drvdata(struct typec_port *port);
 
+struct typec_port *typec_port_from_device(struct device *dev);
+
 #endif /* __LINUX_USB_TYPEC_H */
-- 
2.34.1




More information about the barebox mailing list