From: Mattijs Korpershoek <mkorpershoek@kernel.org>
To: Hans Verkuil <hverkuil+cisco@kernel.org>,
Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
Kieran Bingham <kieran.bingham@ideasonboard.com>,
Sakari Ailus <sakari.ailus@linux.intel.com>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Andy Walls <awalls@md.metrocast.net>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Mattijs Korpershoek <mkorpershoek@kernel.org>,
Nicolas Dufresne <nicolas.dufres@collabora.com>,
Hans de Goede <hansg@kernel.org>,
Michael Riesch <michael.riesch@collabora.com>,
Dave Stevenson <dave.stevenson@raspberrypi.com>,
Maxime Ripard <mripard@kernel.org>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH RFC v2 03/16] v4l: event: Separate event related information from struct v4l2_fh
Date: Wed, 18 Nov 2026 13:43:47 +0100 [thread overview]
Message-ID: <20261118-v4l2-sensor-detect-v2-3-1bb1a445b556@kernel.org> (raw)
In-Reply-To: <20261118-v4l2-sensor-detect-v2-0-1bb1a445b556@kernel.org>
From: Sakari Ailus <sakari.ailus@linux.intel.com>
Generalise event related file handle information to struct media_event_fh.
Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
drivers/media/pci/ivtv/ivtv-fileops.c | 2 +-
drivers/media/v4l2-core/v4l2-event.c | 72 ++++++++++++++++++-----------------
drivers/media/v4l2-core/v4l2-fh.c | 12 +++---
include/media/media-evbase.h | 40 +++++++++++++++++++
include/media/v4l2-fh.h | 20 +++-------
5 files changed, 90 insertions(+), 56 deletions(-)
diff --git a/drivers/media/pci/ivtv/ivtv-fileops.c b/drivers/media/pci/ivtv/ivtv-fileops.c
index e7fed9698758..5900dd97f0ed 100644
--- a/drivers/media/pci/ivtv/ivtv-fileops.c
+++ b/drivers/media/pci/ivtv/ivtv-fileops.c
@@ -742,7 +742,7 @@ __poll_t ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
/* If there are subscribed events, then only use the new event
API instead of the old video.h based API. */
- if (!list_empty(&id->fh.subscribed)) {
+ if (!list_empty(&id->fh.ev_fh.subscribed)) {
poll_wait(filp, &id->fh.wait, wait);
/* Turn off the old-style vsync events */
clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index 9dd2aaa95a67..a21a44c64264 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -24,16 +24,17 @@ static unsigned int sev_pos(const struct v4l2_subscribed_event *sev, unsigned in
return idx >= sev->elems ? idx - sev->elems : idx;
}
-static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
+static int __v4l2_event_dequeue(struct media_evbase_fh *fh,
+ struct v4l2_event *event)
{
struct v4l2_kevent *kev;
struct timespec64 ts;
unsigned long flags;
- spin_lock_irqsave(&fh->vdev->fh_lock, flags);
+ spin_lock_irqsave(fh->lock, flags);
if (list_empty(&fh->available)) {
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ spin_unlock_irqrestore(fh->lock, flags);
return -ENOENT;
}
@@ -51,7 +52,7 @@ static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
kev->sev->first = sev_pos(kev->sev, 1);
kev->sev->in_use--;
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ spin_unlock_irqrestore(fh->lock, flags);
return 0;
}
@@ -62,7 +63,7 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
int ret;
if (nonblocking)
- return __v4l2_event_dequeue(fh, event);
+ return __v4l2_event_dequeue(&fh->ev_fh, event);
/* Release the vdev lock while waiting */
if (fh->vdev->lock)
@@ -70,11 +71,11 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
do {
ret = wait_event_interruptible(fh->wait,
- fh->navailable != 0);
+ fh->ev_fh.navailable != 0);
if (ret < 0)
break;
- ret = __v4l2_event_dequeue(fh, event);
+ ret = __v4l2_event_dequeue(&fh->ev_fh, event);
} while (ret == -ENOENT);
if (fh->vdev->lock)
@@ -84,13 +85,13 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
}
EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
-/* Caller must hold fh->vdev->fh_lock! */
+/* Caller must hold fh->lock! */
static struct v4l2_subscribed_event *v4l2_event_subscribed(
- struct v4l2_fh *fh, u32 type, u32 id)
+ struct media_evbase_fh *fh, u32 type, u32 id)
{
struct v4l2_subscribed_event *sev;
- assert_spin_locked(&fh->vdev->fh_lock);
+ assert_spin_locked(fh->lock);
list_for_each_entry(sev, &fh->subscribed, list)
if (sev->type == type && sev->id == id)
@@ -99,7 +100,7 @@ static struct v4l2_subscribed_event *v4l2_event_subscribed(
return NULL;
}
-static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
+static void __v4l2_event_queue_fh(struct media_evbase_fh *fh,
const struct v4l2_event *ev, u64 ts)
{
struct v4l2_subscribed_event *sev;
@@ -147,7 +148,7 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
fh->navailable++;
- wake_up_all(&fh->wait);
+ wake_up_all(fh->wait);
}
void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
@@ -164,7 +165,7 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
spin_lock_irqsave(&vdev->fh_lock, flags);
list_for_each_entry(fh, &vdev->fh_list, list)
- __v4l2_event_queue_fh(fh, ev, ts);
+ __v4l2_event_queue_fh(&fh->ev_fh, ev, ts);
spin_unlock_irqrestore(&vdev->fh_lock, flags);
}
@@ -175,15 +176,15 @@ void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
unsigned long flags;
u64 ts = ktime_get_ns();
- spin_lock_irqsave(&fh->vdev->fh_lock, flags);
- __v4l2_event_queue_fh(fh, ev, ts);
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ spin_lock_irqsave(fh->ev_fh.lock, flags);
+ __v4l2_event_queue_fh(&fh->ev_fh, ev, ts);
+ spin_unlock_irqrestore(fh->ev_fh.lock, flags);
}
EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
int v4l2_event_pending(struct v4l2_fh *fh)
{
- return fh->navailable;
+ return fh->ev_fh.navailable;
}
EXPORT_SYMBOL_GPL(v4l2_event_pending);
@@ -209,13 +210,13 @@ static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
struct v4l2_fh *fh = sev->fh;
unsigned int i;
- lockdep_assert_held(&fh->subscribe_lock);
+ lockdep_assert_held(&fh->ev_fh.subscribe_lock);
assert_spin_locked(&fh->vdev->fh_lock);
/* Remove any pending events for this subscription */
for (i = 0; i < sev->in_use; i++) {
list_del(&sev->events[sev_pos(sev, i)].list);
- fh->navailable--;
+ fh->ev_fh.navailable--;
}
list_del(&sev->list);
}
@@ -247,13 +248,13 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
sev->fh = fh;
sev->ops = ops;
- mutex_lock(&fh->subscribe_lock);
+ mutex_lock(&fh->ev_fh.subscribe_lock);
- spin_lock_irqsave(&fh->vdev->fh_lock, flags);
- found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
+ spin_lock_irqsave(fh->ev_fh.lock, flags);
+ found_ev = v4l2_event_subscribed(&fh->ev_fh, sub->type, sub->id);
if (!found_ev)
- list_add(&sev->list, &fh->subscribed);
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ list_add(&sev->list, &fh->ev_fh.subscribed);
+ spin_unlock_irqrestore(fh->ev_fh.lock, flags);
if (found_ev) {
/* Already listening */
@@ -268,7 +269,7 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
}
}
- mutex_unlock(&fh->subscribe_lock);
+ mutex_unlock(&fh->ev_fh.subscribe_lock);
return ret;
}
@@ -283,14 +284,14 @@ void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
do {
sev = NULL;
- spin_lock_irqsave(&fh->vdev->fh_lock, flags);
- if (!list_empty(&fh->subscribed)) {
- sev = list_first_entry(&fh->subscribed,
+ spin_lock_irqsave(fh->ev_fh.lock, flags);
+ if (!list_empty(&fh->ev_fh.subscribed)) {
+ sev = list_first_entry(&fh->ev_fh.subscribed,
struct v4l2_subscribed_event, list);
sub.type = sev->type;
sub.id = sev->id;
}
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ spin_unlock_irqrestore(fh->ev_fh.lock, flags);
if (sev)
v4l2_event_unsubscribe(fh, &sub);
} while (sev);
@@ -308,20 +309,20 @@ int v4l2_event_unsubscribe(struct v4l2_fh *fh,
return 0;
}
- mutex_lock(&fh->subscribe_lock);
+ mutex_lock(&fh->ev_fh.subscribe_lock);
- spin_lock_irqsave(&fh->vdev->fh_lock, flags);
+ spin_lock_irqsave(fh->ev_fh.lock, flags);
- sev = v4l2_event_subscribed(fh, sub->type, sub->id);
+ sev = v4l2_event_subscribed(&fh->ev_fh, sub->type, sub->id);
if (sev != NULL)
__v4l2_event_unsubscribe(sev);
- spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
+ spin_unlock_irqrestore(fh->ev_fh.lock, flags);
if (sev && sev->ops && sev->ops->del)
sev->ops->del(sev);
- mutex_unlock(&fh->subscribe_lock);
+ mutex_unlock(&fh->ev_fh.subscribe_lock);
kvfree(sev);
@@ -329,7 +330,8 @@ int v4l2_event_unsubscribe(struct v4l2_fh *fh,
}
EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
-int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh,
+int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd,
+ struct v4l2_fh *fh,
struct v4l2_event_subscription *sub)
{
return v4l2_event_unsubscribe(fh, sub);
diff --git a/drivers/media/v4l2-core/v4l2-fh.c b/drivers/media/v4l2-core/v4l2-fh.c
index b184bed8aca9..c28653f03c32 100644
--- a/drivers/media/v4l2-core/v4l2-fh.c
+++ b/drivers/media/v4l2-core/v4l2-fh.c
@@ -34,10 +34,12 @@ void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)
set_bit(_IOC_NR(VIDIOC_S_PRIORITY), vdev->valid_ioctls);
fh->prio = V4L2_PRIORITY_UNSET;
init_waitqueue_head(&fh->wait);
- INIT_LIST_HEAD(&fh->available);
- INIT_LIST_HEAD(&fh->subscribed);
- fh->sequence = -1;
- mutex_init(&fh->subscribe_lock);
+ fh->ev_fh.lock = &vdev->fh_lock;
+ fh->ev_fh.wait = &fh->wait;
+ INIT_LIST_HEAD(&fh->ev_fh.available);
+ INIT_LIST_HEAD(&fh->ev_fh.subscribed);
+ fh->ev_fh.sequence = -1;
+ mutex_init(&fh->ev_fh.subscribe_lock);
}
EXPORT_SYMBOL_GPL(v4l2_fh_init);
@@ -86,7 +88,7 @@ void v4l2_fh_exit(struct v4l2_fh *fh)
return;
v4l_disable_media_source(fh->vdev);
v4l2_event_unsubscribe_all(fh);
- mutex_destroy(&fh->subscribe_lock);
+ mutex_destroy(&fh->ev_fh.subscribe_lock);
fh->vdev = NULL;
}
EXPORT_SYMBOL_GPL(v4l2_fh_exit);
diff --git a/include/media/media-evbase.h b/include/media/media-evbase.h
new file mode 100644
index 000000000000..011ca990fcc4
--- /dev/null
+++ b/include/media/media-evbase.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * media-evbase.h
+ *
+ * Copyright (C) 2018--2019 Intel Corporation
+ */
+
+#ifndef MEDIA_EVBASE_H
+#define MEDIA_EVBASE_H
+
+#include <linux/spinlock.h>
+#include <linux/wait.h>
+
+struct list_head;
+
+/*
+ * struct media_evbase_fh - File handles for media subsystem events
+ *
+ * @lock: serialise access to fields and lists (apart from wait)
+ * @subscribe_lock: serialise changes to the subscribed list; guarantee that
+ * the add and del event callbacks are orderly called
+ * @wait: waiting list for the events
+ * @subscribed: list of subscribed events
+ * @available: list of events waiting to be dequeued
+ * @navailable: number of available events at @available list
+ * @sequence: event sequence number
+ *
+ */
+struct media_evbase_fh {
+ spinlock_t *lock;
+ struct mutex subscribe_lock;
+ wait_queue_head_t *wait;
+
+ struct list_head subscribed;
+ struct list_head available;
+ unsigned int navailable;
+ u32 sequence;
+};
+
+#endif /* MEDIA_EVBASE_H */
diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
index aad4b3689d7e..cd5bb7972699 100644
--- a/include/media/v4l2-fh.h
+++ b/include/media/v4l2-fh.h
@@ -18,6 +18,8 @@
#include <linux/list.h>
#include <linux/videodev2.h>
+#include <media/media-evbase.h>
+
struct video_device;
struct v4l2_ctrl_handler;
@@ -28,32 +30,20 @@ struct v4l2_ctrl_handler;
* @vdev: pointer to &struct video_device
* @ctrl_handler: pointer to &struct v4l2_ctrl_handler
* @prio: priority of the file handler, as defined by &enum v4l2_priority
- *
- * @wait: event' s wait queue
- * @subscribe_lock: serialise changes to the subscribed list; guarantee that
- * the add and del event callbacks are orderly called
- * @subscribed: list of subscribed events
- * @available: list of events waiting to be dequeued
- * @navailable: number of available events at @available list
- * @sequence: event sequence number
+ * @wait: file handle's wait queue
*
* @m2m_ctx: pointer to &struct v4l2_m2m_ctx
+ * @ev_fh: file handle specific data for events
*/
struct v4l2_fh {
struct list_head list;
struct video_device *vdev;
struct v4l2_ctrl_handler *ctrl_handler;
enum v4l2_priority prio;
-
- /* Events */
wait_queue_head_t wait;
- struct mutex subscribe_lock;
- struct list_head subscribed;
- struct list_head available;
- unsigned int navailable;
- u32 sequence;
struct v4l2_m2m_ctx *m2m_ctx;
+ struct media_evbase_fh ev_fh;
};
/**
--
2.56.0
next prev parent reply other threads:[~2026-11-18 12:44 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 01/16] media: mc: Add struct media_device_fh pointer to media device IOCTL op Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 02/16] media: mc: Add per-file-handle data support Mattijs Korpershoek
2026-11-18 12:43 ` Mattijs Korpershoek [this message]
2026-11-18 12:43 ` [PATCH RFC v2 04/16] v4l: event: Rework data structures for separating events and V4L2 Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 05/16] v4l: event: Split the event framework into generic and V4L2 bits Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 06/16] media: mc: Maintain a list of open file handles in a media device Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 07/16] media: mc: Add MEDIA_IOC_DQEVENT Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 08/16] media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 09/16] Documentation: media: Add Media controller event documentation Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 10/16] media: media.h: Add entity connection status Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 11/16] media: mc: Add .detect() to media_entity_operations Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 12/16] media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 13/16] media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 14/16] media: mc: Add detect polling helpers for drivers Mattijs Korpershoek
2026-11-18 12:43 ` [PATCH RFC v2 15/16] media: imx219: Allow driver probe with missing sensor Mattijs Korpershoek
2026-11-18 12:44 ` [PATCH RFC v2 16/16] media: imx219: Implement .detect() sensor operation Mattijs Korpershoek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118-v4l2-sensor-detect-v2-3-1bb1a445b556@kernel.org \
--to=mkorpershoek@kernel.org \
--cc=awalls@md.metrocast.net \
--cc=dave.stevenson@raspberrypi.com \
--cc=gustavoars@kernel.org \
--cc=hansg@kernel.org \
--cc=hverkuil+cisco@kernel.org \
--cc=kees@kernel.org \
--cc=kieran.bingham@ideasonboard.com \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=michael.riesch@collabora.com \
--cc=mripard@kernel.org \
--cc=nicolas.dufres@collabora.com \
--cc=sakari.ailus@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®