[PATCH 3/4] media: v4l2-subdev: Add devm_v4l2_subdev_init_finalize() helper

Tarang Raval posted 4 patches 2 months, 2 weeks ago
[PATCH 3/4] media: v4l2-subdev: Add devm_v4l2_subdev_init_finalize() helper
Posted by Tarang Raval 2 months, 2 weeks ago
Add a devm-managed version of v4l2_subdev_init_finalize() to simplify
sub-device finalization and cleanup using devres.

Signed-off-by: Tarang Raval <tarang.raval@siliconsignals.io>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 18 ++++++++++++++++++
 include/media/v4l2-subdev.h           | 17 +++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index c69d1aff701f..da7a479584bc 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -8,6 +8,7 @@
  *	    Sakari Ailus <sakari.ailus@iki.fi>
  */
 
+#include <linux/device/devres.h>
 #include <linux/export.h>
 #include <linux/ioctl.h>
 #include <linux/leds.h>
@@ -1710,6 +1711,23 @@ void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
 
+static void devm_v4l2_subdev_cleanup(void *data)
+{
+	v4l2_subdev_cleanup(data);
+}
+
+int devm_v4l2_subdev_init_finalize(struct device *dev, struct v4l2_subdev *sd)
+{
+	int err;
+
+	err = v4l2_subdev_init_finalize(sd);
+	if (err)
+		return err;
+
+	return devm_add_action_or_reset(dev, devm_v4l2_subdev_cleanup, sd);
+}
+EXPORT_SYMBOL_GPL(devm_v4l2_subdev_init_finalize);
+
 struct v4l2_mbus_framefmt *
 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
 			       unsigned int pad, u32 stream)
diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
index 57f2bcb4eb16..a5da32783846 100644
--- a/include/media/v4l2-subdev.h
+++ b/include/media/v4l2-subdev.h
@@ -1337,6 +1337,23 @@ void __v4l2_subdev_state_free(struct v4l2_subdev_state *state);
 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
 				struct lock_class_key *key);
 
+/**
+ * devm_v4l2_subdev_init_finalize - Managed finalization of V4L2 sub-device initialization
+ *
+ * @dev:        Device that manages the lifecycle of the V4L2 sub-device.
+ * @sd:         Pointer to the initialized V4L2 sub-device.
+ *
+ * This function finalizes the initialization of a V4L2 sub-device and registers
+ * a managed cleanup action to be performed automatically when the device is
+ * detached or the driver is unloaded.
+ *
+ * This is a managed version of v4l2_subdev_init_finalize(), and simplifies
+ * resource management using devres.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int devm_v4l2_subdev_init_finalize(struct device *dev, struct v4l2_subdev *sd);
+
 /**
  * v4l2_subdev_cleanup() - Releases the resources allocated by the subdevice
  * @sd: The subdevice
-- 
2.34.1