[PATCH 4/4] media: v4l2-ctrls: Add devm_v4l2_ctrl_handler_init() helper

Tarang Raval posted 4 patches 2 months, 2 weeks ago
[PATCH 4/4] media: v4l2-ctrls: Add devm_v4l2_ctrl_handler_init() helper
Posted by Tarang Raval 2 months, 2 weeks ago
Add a devm-managed version of v4l2_ctrl_handler_init() to simplify control
handler initialization and cleanup using devres.

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

diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 98b960775e87..2c8c46bc8d30 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -5,6 +5,7 @@
  * Copyright (C) 2010-2021  Hans Verkuil <hverkuil-cisco@xs4all.nl>
  */
 
+#include <linux/device/devres.h>
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
@@ -1671,6 +1672,25 @@ int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 }
 EXPORT_SYMBOL(v4l2_ctrl_handler_free);
 
+static void devm_v4l2_ctrl_handler_free(void *data)
+{
+	v4l2_ctrl_handler_free(data);
+}
+
+int devm_v4l2_ctrl_handler_init(struct device *dev,
+				struct v4l2_ctrl_handler *hdl,
+				unsigned int nr_of_controls_hint)
+{
+	int err;
+
+	err = v4l2_ctrl_handler_init(hdl, nr_of_controls_hint);
+	if (err)
+		return err;
+
+	return devm_add_action_or_reset(dev, devm_v4l2_ctrl_handler_free, hdl);
+}
+EXPORT_SYMBOL(devm_v4l2_ctrl_handler_init);
+
 /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
    be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
    with applications that do not use the NEXT_CTRL flag.
diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h
index c32c46286441..dfb956a5ad9a 100644
--- a/include/media/v4l2-ctrls.h
+++ b/include/media/v4l2-ctrls.h
@@ -573,6 +573,25 @@ int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
 	v4l2_ctrl_handler_init_class(hdl, nr_of_controls_hint, NULL, NULL)
 #endif
 
+/**
+ * devm_v4l2_ctrl_handler_init - Managed initialization of V4L2 control handler
+ *
+ * @dev:                  Device that manages the lifecycle of the control handler.
+ * @hdl:                  Pointer to the V4L2 control handler to initialize.
+ * @nr_of_controls_hint: Estimated number of controls to be added.
+ *
+ * This function initializes a V4L2 control handler 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_ctrl_handler_init(), and simplifies resource
+ * management using devres.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int devm_v4l2_ctrl_handler_init(struct device *dev,
+				struct v4l2_ctrl_handler *hdl,
+				unsigned int nr_of_controls_hint);
 /**
  * v4l2_ctrl_handler_free() - Free all controls owned by the handler and free
  * the control list.
-- 
2.34.1