[PATCH RFC v2 02/16] media: mc: Add per-file-handle data support
From: Mattijs Korpershoek
Date: Wed Nov 18 2026 - 07:44:24 EST
From: Laurent Pinchart <laurent.pinchart+renesas@xxxxxxxxxxxxxxxx>
The media devnode core associates devnodes with files by storing the
devnode pointer in the file structure private_data field. In order to
allow tracking of per-file-handle data introduce a new media devnode
file handle structure that stores the devnode pointer, and store a
pointer to that structure in the file private_data field.
Users of the media devnode code (the only existing user being
media_device) are responsible for managing their own subclass of the
media_devnode_fh structure.
Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@xxxxxxxxxxxxxxxx>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@xxxxxxxxxx>
---
drivers/media/mc/mc-device.c | 21 +++++++++++++++++++++
drivers/media/mc/mc-devnode.c | 19 ++++++++++---------
include/media/media-devnode.h | 18 +++++++++++++++++-
3 files changed, 48 insertions(+), 10 deletions(-)
diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 32e5c9f02ed4..8d0c35a11944 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -36,6 +36,15 @@
#define MEDIA_ENT_T_DEVNODE_UNKNOWN (MEDIA_ENT_F_OLD_BASE | \
MEDIA_ENT_SUBTYPE_MASK)
+struct media_device_fh {
+ struct media_devnode_fh fh;
+};
+
+static inline struct media_device_fh *media_device_fh(struct file *filp)
+{
+ return container_of(filp->private_data, struct media_device_fh, fh);
+}
+
/* -----------------------------------------------------------------------------
* Userspace API
*/
@@ -47,11 +56,23 @@ static inline void __user *media_get_uptr(__u64 arg)
static int media_device_open(struct file *filp)
{
+ struct media_device_fh *fh;
+
+ fh = kzalloc_obj(*fh);
+ if (!fh)
+ return -ENOMEM;
+
+ filp->private_data = &fh->fh;
+
return 0;
}
static int media_device_close(struct file *filp)
{
+ struct media_device_fh *fh = media_device_fh(filp);
+
+ kfree(fh);
+
return 0;
}
diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c
index 51260a7d6921..bc06cbf0445a 100644
--- a/drivers/media/mc/mc-devnode.c
+++ b/drivers/media/mc/mc-devnode.c
@@ -140,6 +140,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd,
static int media_open(struct inode *inode, struct file *filp)
{
struct media_devnode *devnode;
+ struct media_devnode_fh *fh;
int ret;
/* Check if the media device is available. This needs to be done with
@@ -162,15 +163,16 @@ static int media_open(struct inode *inode, struct file *filp)
filp->private_data = devnode;
- if (devnode->fops->open) {
- ret = devnode->fops->open(filp);
- if (ret) {
- put_device(&devnode->dev);
- filp->private_data = NULL;
- return ret;
- }
+ ret = devnode->fops->open(filp);
+ if (ret) {
+ put_device(&devnode->dev);
+ filp->private_data = NULL;
+ return ret;
}
+ fh = filp->private_data;
+ fh->devnode = devnode;
+
return 0;
}
@@ -179,8 +181,7 @@ static int media_release(struct inode *inode, struct file *filp)
{
struct media_devnode *devnode = media_devnode_data(filp);
- if (devnode->fops->release)
- devnode->fops->release(filp);
+ devnode->fops->release(filp);
filp->private_data = NULL;
diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h
index dbcabeffcb57..0912368ea049 100644
--- a/include/media/media-devnode.h
+++ b/include/media/media-devnode.h
@@ -59,6 +59,20 @@ struct media_file_operations {
int (*release) (struct file *);
};
+/**
+ * struct media_devnode_fh - Media device node file handle
+ * @devnode: pointer to the media device node
+ *
+ * This structure serves as a base for per-file-handle data storage. Media
+ * device node users embed media_devnode_fh in their custom file handle data
+ * structures and store the media_devnode_fh in the file private_data in order
+ * to let the media device node core locate the media_devnode corresponding to a
+ * file handle.
+ */
+struct media_devnode_fh {
+ struct media_devnode *devnode;
+};
+
/**
* struct media_devnode - Media device node
* @media_dev: pointer to struct &media_device
@@ -150,7 +164,9 @@ void media_devnode_unregister(struct media_devnode *devnode);
*/
static inline struct media_devnode *media_devnode_data(struct file *filp)
{
- return filp->private_data;
+ struct media_devnode_fh *fh = filp->private_data;
+
+ return fh->devnode;
}
/**
--
2.56.0