[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250723102515.64585-5-tarang.raval@siliconsignals.io>
Date: Wed, 23 Jul 2025 15:55:08 +0530
From: Tarang Raval <tarang.raval@...iconsignals.io>
To: sakari.ailus@...ux.intel.com,
laurent.pinchart@...asonboard.com,
hverkuil@...all.nl
Cc: Tarang Raval <tarang.raval@...iconsignals.io>,
Mauro Carvalho Chehab <mchehab@...nel.org>,
Ricardo Ribalda <ribalda@...omium.org>,
Hans de Goede <hansg@...nel.org>,
Yunke Cao <yunkec@...gle.com>,
James Cowgill <james.cowgill@...ize.com>,
Tomi Valkeinen <tomi.valkeinen@...asonboard.com>,
Lad Prabhakar <prabhakar.mahadev-lad.rj@...renesas.com>,
Tommaso Merciai <tomm.merciai@...il.com>,
linux-media@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH 4/4] media: v4l2-ctrls: Add devm_v4l2_ctrl_handler_init() helper
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@...iconsignals.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@...all.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
Powered by blists - more mailing lists