[PATCH] usb: typec: pd: Add symlink to linked device

From: Prashant Malani
Date: Mon Nov 21 2022 - 01:21:19 EST


There exists a symlink from a device to its USB Power Delivery object,
but not the other way around. Add a symlink from the USB PD object to
the device which it's associated with, and call it "device".

This is helpful to identify said device (a Type-C peripheral for
example) during uevents, since during USB PD object
creation/destruction, a uevent is generated for the PD object,
but not the device linked to it.

Cc: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
Cc: Benson Leung <bleung@xxxxxxxxxxxx>
Signed-off-by: Prashant Malani <pmalani@xxxxxxxxxxxx>
---
.../ABI/testing/sysfs-class-usb_power_delivery | 6 ++++++
drivers/usb/typec/pd.c | 12 ++++++++++--
2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-usb_power_delivery b/Documentation/ABI/testing/sysfs-class-usb_power_delivery
index ce2b1b563cb3..e7d19193875f 100644
--- a/Documentation/ABI/testing/sysfs-class-usb_power_delivery
+++ b/Documentation/ABI/testing/sysfs-class-usb_power_delivery
@@ -4,6 +4,12 @@ Contact: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
Description:
Directory for USB Power Delivery devices.

+What: /sys/class/usb_power_delivery/.../device
+Date: November 2022
+Contact: Prashant Malani <pmalani@xxxxxxxxxxxx>
+Description:
+ Symbolic link to the directory of the device to which the USB PD object is linked.
+
What: /sys/class/usb_power_delivery/.../revision
Date: May 2022
Contact: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
diff --git a/drivers/usb/typec/pd.c b/drivers/usb/typec/pd.c
index dc72005d68db..ed073d922655 100644
--- a/drivers/usb/typec/pd.c
+++ b/drivers/usb/typec/pd.c
@@ -656,7 +656,8 @@ EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
* @dev: The device.
*
* This function can be used to create a symlink named "usb_power_delivery" for
- * @dev that points to @pd.
+ * @dev that points to @pd. It also creates another symlink named "device" which
+ * points from @pd to @dev.
*/
int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
{
@@ -669,6 +670,12 @@ int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device
if (ret)
return ret;

+ ret = sysfs_create_link(&pd->dev.kobj, &dev->kobj, "device");
+ if (ret) {
+ sysfs_remove_link(&dev->kobj, "usb_power_delivery");
+ return ret;
+ }
+
get_device(&pd->dev);
get_device(dev);

@@ -681,13 +688,14 @@ EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
* @pd: The USB PD instance.
* @dev: The device.
*
- * Remove the symlink that was previously created with pd_link_device().
+ * Remove the symlinks that were previously created with pd_link_device().
*/
void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
{
if (IS_ERR_OR_NULL(pd) || !dev)
return;

+ sysfs_remove_link(&pd->dev.kobj, "device");
sysfs_remove_link(&dev->kobj, "usb_power_delivery");
put_device(&pd->dev);
put_device(dev);
--
2.38.1.584.g0f3c55d4c2-goog