mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 04/16] v4l: event: Rework data structures for separating events and V4L2
Date: Wed, 18 Nov 2026 13:43:48 +0100	[thread overview]
Message-ID: <20261118-v4l2-sensor-detect-v2-4-1bb1a445b556@kernel.org> (raw)
In-Reply-To: <20261118-v4l2-sensor-detect-v2-0-1bb1a445b556@kernel.org>

From: Sakari Ailus <sakari.ailus@linux.intel.com>

The V4L2 event framework will be functionality-wise useful for
implementing events for the media controller. Separate the V4L2 dependent
and V4L2 independent bits from data structures now.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/usb/uvc/uvc_ctrl.c          |  36 ++++----
 drivers/media/v4l2-core/v4l2-ctrls-api.c  |  16 ++--
 drivers/media/v4l2-core/v4l2-ctrls-core.c |  20 +++--
 drivers/media/v4l2-core/v4l2-event.c      | 140 ++++++++++++++++++++++--------
 include/media/media-evbase.h              |  80 ++++++++++++++++-
 include/media/v4l2-event.h                | 100 +++++++++++++--------
 include/media/v4l2-fh.h                   |  12 +++
 7 files changed, 303 insertions(+), 101 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index d2f722bc3459..f8931c6bc918 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -2054,7 +2054,7 @@ static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
 	struct uvc_control_mapping *mapping, s32 value, u32 changes)
 {
 	struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
-	struct v4l2_subscribed_event *sev;
+	struct media_subscribed_evbase *mse;
 	struct v4l2_event ev;
 
 	if (list_empty(&mapping->ev_subs))
@@ -2062,11 +2062,15 @@ static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
 
 	uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
 
-	list_for_each_entry(sev, &mapping->ev_subs, node) {
-		if (sev->fh != originator ||
-		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
+	list_for_each_entry(mse, &mapping->ev_subs, node) {
+		struct v4l2_subscribed_event *sev =
+			to_v4l2_subscribed_event(mse);
+
+		if (media_evbase_fh_to_v4l2(mse->fh) != originator ||
+		    (sev->evspec.flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
 		    (changes & V4L2_EVENT_CTRL_CH_FLAGS))
-			v4l2_event_queue_fh(sev->fh, &ev);
+			v4l2_event_queue_fh(media_evbase_fh_to_v4l2(mse->fh),
+					    &ev);
 	}
 }
 
@@ -2295,7 +2299,8 @@ static void uvc_ctrl_send_events(struct uvc_fh *handle,
 
 static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
 {
-	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
+	struct uvc_fh *handle = container_of(
+		media_evbase_fh_to_v4l2(sev->mse.fh), struct uvc_fh, vfh);
 	struct uvc_control_mapping *mapping;
 	struct uvc_control *ctrl;
 	int ret;
@@ -2304,18 +2309,18 @@ static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
 	if (ret < 0)
 		return -ERESTARTSYS;
 
-	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
+	if (__uvc_query_v4l2_class(handle->chain, sev->evspec.id, 0) >= 0) {
 		ret = 0;
 		goto done;
 	}
 
-	ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
+	ctrl = uvc_find_control(handle->chain, sev->evspec.id, &mapping);
 	if (ctrl == NULL) {
 		ret = -EINVAL;
 		goto done;
 	}
 
-	if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
+	if (sev->evspec.flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
 		struct v4l2_event ev;
 		u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
 		s32 val = 0;
@@ -2337,11 +2342,11 @@ static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
 		 * Mark the queue as active, allowing this initial event to be
 		 * accepted.
 		 */
-		sev->elems = elems;
-		v4l2_event_queue_fh(sev->fh, &ev);
+		sev->mse.elems = elems;
+		v4l2_event_queue_fh(media_evbase_fh_to_v4l2(sev->mse.fh), &ev);
 	}
 
-	list_add_tail(&sev->node, &mapping->ev_subs);
+	list_add_tail(&sev->mse.node, &mapping->ev_subs);
 
 done:
 	mutex_unlock(&handle->chain->ctrl_mutex);
@@ -2350,12 +2355,13 @@ static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
 
 static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
 {
-	struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
+	struct uvc_fh *handle = container_of(
+		media_evbase_fh_to_v4l2(sev->mse.fh), struct uvc_fh, vfh);
 
 	mutex_lock(&handle->chain->ctrl_mutex);
-	if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
+	if (__uvc_query_v4l2_class(handle->chain, sev->evspec.id, 0) >= 0)
 		goto done;
-	list_del(&sev->node);
+	list_del(&sev->mse.node);
 done:
 	mutex_unlock(&handle->chain->ctrl_mutex);
 }
diff --git a/drivers/media/v4l2-core/v4l2-ctrls-api.c b/drivers/media/v4l2-core/v4l2-ctrls-api.c
index 93d8d4012d0f..0da97e7011eb 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-api.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-api.c
@@ -1277,29 +1277,33 @@ EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
 static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev,
 			       unsigned int elems)
 {
-	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
+	struct v4l2_fh *fh = media_evbase_fh_to_v4l2(sev->mse.fh);
+	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler,
+						sev->evspec.id);
 
 	if (!ctrl)
 		return -EINVAL;
 
 	v4l2_ctrl_lock(ctrl);
-	list_add_tail(&sev->node, &ctrl->ev_subs);
+	list_add_tail(&sev->mse.node, &ctrl->ev_subs);
 	if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
-	    (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL))
-		send_initial_event(sev->fh, ctrl);
+	    (sev->evspec.flags & V4L2_EVENT_SUB_FL_SEND_INITIAL))
+		send_initial_event(media_evbase_fh_to_v4l2(sev->mse.fh), ctrl);
 	v4l2_ctrl_unlock(ctrl);
 	return 0;
 }
 
 static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
 {
-	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
+	struct v4l2_fh *fh = media_evbase_fh_to_v4l2(sev->mse.fh);
+	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler,
+						sev->evspec.id);
 
 	if (!ctrl)
 		return;
 
 	v4l2_ctrl_lock(ctrl);
-	list_del(&sev->node);
+	list_del(&sev->mse.node);
 	v4l2_ctrl_unlock(ctrl);
 }
 
diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 6e751a043a21..433ebe2acf4a 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -56,16 +56,20 @@ void send_initial_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl)
 void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
 {
 	struct v4l2_event ev;
-	struct v4l2_subscribed_event *sev;
+	struct media_subscribed_evbase *mse;
 
 	if (list_empty(&ctrl->ev_subs))
 		return;
 	fill_event(&ev, ctrl, changes);
 
-	list_for_each_entry(sev, &ctrl->ev_subs, node)
-		if (sev->fh != fh ||
-		    (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
-			v4l2_event_queue_fh(sev->fh, &ev);
+	list_for_each_entry(mse, &ctrl->ev_subs, node) {
+		struct v4l2_subscribed_event *sev =
+			to_v4l2_subscribed_event(mse);
+
+		if (mse->fh != &fh->ev_fh ||
+		    (sev->evspec.flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
+			v4l2_event_queue_fh(media_evbase_fh_to_v4l2(mse->fh), &ev);
+	}
 }
 
 bool v4l2_ctrl_type_op_equal(const struct v4l2_ctrl *ctrl,
@@ -1821,7 +1825,7 @@ int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 {
 	struct v4l2_ctrl_ref *ref, *next_ref;
 	struct v4l2_ctrl *ctrl, *next_ctrl;
-	struct v4l2_subscribed_event *sev, *next_sev;
+	struct media_subscribed_evbase *mse, *next_mse;
 
 	if (!hdl)
 		return 0;
@@ -1842,8 +1846,8 @@ int v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
 	/* Free all controls owned by the handler */
 	list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
 		list_del(&ctrl->node);
-		list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
-			list_del(&sev->node);
+		list_for_each_entry_safe(mse, next_mse, &ctrl->ev_subs, node)
+			list_del(&mse->node);
 		kvfree(ctrl->p_array);
 		kvfree(ctrl);
 	}
diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index a21a44c64264..c8371a4fa90b 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -18,12 +18,70 @@
 #include <linux/slab.h>
 #include <linux/export.h>
 
-static unsigned int sev_pos(const struct v4l2_subscribed_event *sev, unsigned int idx)
+static inline size_t media_evbase_pos(const struct media_subscribed_evbase *mse,
+				      unsigned int idx)
 {
-	idx += sev->first;
-	return idx >= sev->elems ? idx - sev->elems : idx;
+	idx += mse->first;
+	return idx >= mse->elems ? idx - mse->elems : idx;
 }
 
+static inline struct media_evbase *
+media_evbase_ptr(const struct media_subscribed_evbase *mse, unsigned int idx)
+{
+	return (void *)(mse + 1) +
+		media_evbase_pos(mse, idx) * mse->ev_size;
+}
+
+static int v4l2_evop_add(struct media_subscribed_evbase *mse, unsigned int elems)
+{
+	struct v4l2_subscribed_event *sev = to_v4l2_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->add)
+		return sev->ops->add(sev, elems);
+
+	return 0;
+}
+
+static void v4l2_evop_del(struct media_subscribed_evbase *mse)
+{
+	struct v4l2_subscribed_event *sev = to_v4l2_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->del)
+		sev->ops->del(sev);
+}
+
+static bool v4l2_evop_replace(struct media_subscribed_evbase *mse,
+			      struct media_evbase *old,
+			      const void *new)
+{
+	struct v4l2_subscribed_event *sev = to_v4l2_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->replace) {
+		sev->ops->replace(&to_v4l2_kevent(old)->event, new);
+		return true;
+	}
+
+	return false;
+}
+
+static void v4l2_evop_merge(struct media_subscribed_evbase *mse,
+			    const struct media_evbase *old,
+			    struct media_evbase *new)
+{
+	struct v4l2_subscribed_event *sev = to_v4l2_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->merge)
+		sev->ops->merge(&to_v4l2_kevent_const(old)->event,
+				&to_v4l2_kevent(new)->event);
+}
+
+static const struct media_subscribed_evbase_ops v4l2_event_ops = {
+	.add            = v4l2_evop_add,
+	.del            = v4l2_evop_del,
+	.replace        = v4l2_evop_replace,
+	.merge          = v4l2_evop_merge,
+};
+
 static int __v4l2_event_dequeue(struct media_evbase_fh *fh,
 				struct v4l2_event *event)
 {
@@ -40,8 +98,9 @@ static int __v4l2_event_dequeue(struct media_evbase_fh *fh,
 
 	WARN_ON(fh->navailable == 0);
 
-	kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
-	list_del(&kev->list);
+	kev = to_v4l2_kevent(list_first_entry(&fh->available,
+					      struct media_evbase, list));
+	list_del(&kev->mev.list);
 	fh->navailable--;
 
 	kev->event.pending = fh->navailable;
@@ -49,8 +108,8 @@ static int __v4l2_event_dequeue(struct media_evbase_fh *fh,
 	ts = ns_to_timespec64(kev->ts);
 	event->timestamp.tv_sec = ts.tv_sec;
 	event->timestamp.tv_nsec = ts.tv_nsec;
-	kev->sev->first = sev_pos(kev->sev, 1);
-	kev->sev->in_use--;
+	kev->mev.mse->first = media_evbase_pos(kev->mev.mse, 1);
+	kev->mev.mse->in_use--;
 
 	spin_unlock_irqrestore(fh->lock, flags);
 
@@ -89,13 +148,17 @@ EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
 static struct v4l2_subscribed_event *v4l2_event_subscribed(
 	struct media_evbase_fh *fh, u32 type, u32 id)
 {
-	struct v4l2_subscribed_event *sev;
+	struct media_subscribed_evbase *mse;
 
 	assert_spin_locked(fh->lock);
 
-	list_for_each_entry(sev, &fh->subscribed, list)
-		if (sev->type == type && sev->id == id)
+	list_for_each_entry(mse, &fh->subscribed, list) {
+		struct v4l2_subscribed_event *sev =
+			to_v4l2_subscribed_event(mse);
+
+		if (sev->evspec.type == type && sev->evspec.id == id)
 			return sev;
+	}
 
 	return NULL;
 }
@@ -116,35 +179,35 @@ static void __v4l2_event_queue_fh(struct media_evbase_fh *fh,
 	fh->sequence++;
 
 	/* Do we have any free events? */
-	if (sev->in_use == sev->elems) {
+	if (sev->mse.in_use == sev->mse.elems) {
 		/* no, remove the oldest one */
-		kev = sev->events + sev_pos(sev, 0);
-		list_del(&kev->list);
-		sev->in_use--;
-		sev->first = sev_pos(sev, 1);
+		kev = to_v4l2_kevent(media_evbase_ptr(&sev->mse, 0));
+		list_del(&kev->mev.list);
+		sev->mse.in_use--;
+		sev->mse.first = media_evbase_pos(&sev->mse, 1);
 		fh->navailable--;
-		if (sev->elems == 1) {
+		if (sev->mse.elems == 1) {
 			if (sev->ops && sev->ops->replace) {
 				sev->ops->replace(&kev->event, ev);
 				copy_payload = false;
 			}
 		} else if (sev->ops && sev->ops->merge) {
 			struct v4l2_kevent *second_oldest =
-				sev->events + sev_pos(sev, 0);
+				to_v4l2_kevent(media_evbase_ptr(&sev->mse, 0));
 			sev->ops->merge(&kev->event, &second_oldest->event);
 		}
 	}
 
 	/* Take one and fill it. */
-	kev = sev->events + sev_pos(sev, sev->in_use);
+	kev = to_v4l2_kevent(media_evbase_ptr(&sev->mse, sev->mse.in_use));
 	kev->event.type = ev->type;
 	if (copy_payload)
 		kev->event.u = ev->u;
 	kev->event.id = ev->id;
 	kev->ts = ts;
 	kev->event.sequence = fh->sequence;
-	sev->in_use++;
-	list_add_tail(&kev->list, &fh->available);
+	sev->mse.in_use++;
+	list_add_tail(&kev->mev.list, &fh->available);
 
 	fh->navailable++;
 
@@ -207,18 +270,19 @@ EXPORT_SYMBOL_GPL(v4l2_event_wake_all);
 
 static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
 {
-	struct v4l2_fh *fh = sev->fh;
+	struct media_evbase_fh *ev_fh = sev->mse.fh;
+	struct v4l2_fh *fh = container_of(ev_fh, struct v4l2_fh, ev_fh);
 	unsigned int i;
 
 	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);
+	for (i = 0; i < sev->mse.in_use; i++) {
+		list_del(&media_evbase_ptr(&sev->mse, i)->list);
 		fh->ev_fh.navailable--;
 	}
-	list_del(&sev->list);
+	list_del(&sev->mse.list);
 }
 
 int v4l2_event_subscribe(struct v4l2_fh *fh,
@@ -236,16 +300,18 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
 	if (elems < 1)
 		elems = 1;
 
-	sev = kvzalloc_flex(*sev, events, elems);
+	sev = kvzalloc(sizeof(*sev) + array_size(sizeof(struct v4l2_kevent),
+						 elems), GFP_KERNEL);
 	if (!sev)
 		return -ENOMEM;
-	sev->elems = elems;
+	sev->mse.elems = elems;
+	sev->mse.ev_size = sizeof(struct v4l2_kevent);
 	for (i = 0; i < elems; i++)
-		sev->events[i].sev = sev;
-	sev->type = sub->type;
-	sev->id = sub->id;
-	sev->flags = sub->flags;
-	sev->fh = fh;
+		media_evbase_ptr(&sev->mse, i)->mse = &sev->mse;
+	sev->evspec.type = sub->type;
+	sev->evspec.id = sub->id;
+	sev->evspec.flags = sub->flags;
+	sev->mse.fh = &fh->ev_fh;
 	sev->ops = ops;
 
 	mutex_lock(&fh->ev_fh.subscribe_lock);
@@ -253,7 +319,7 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
 	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->ev_fh.subscribed);
+		list_add(&sev->mse.list, &fh->ev_fh.subscribed);
 	spin_unlock_irqrestore(fh->ev_fh.lock, flags);
 
 	if (found_ev) {
@@ -286,10 +352,12 @@ void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
 
 		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;
+			sev = to_v4l2_subscribed_event(
+				list_first_entry(&fh->ev_fh.subscribed,
+						 struct media_subscribed_evbase,
+						 list));
+			sub.type = sev->evspec.type;
+			sub.id = sev->evspec.id;
 		}
 		spin_unlock_irqrestore(fh->ev_fh.lock, flags);
 		if (sev)
diff --git a/include/media/media-evbase.h b/include/media/media-evbase.h
index 011ca990fcc4..bfb36a946386 100644
--- a/include/media/media-evbase.h
+++ b/include/media/media-evbase.h
@@ -8,10 +8,25 @@
 #ifndef MEDIA_EVBASE_H
 #define MEDIA_EVBASE_H
 
+#include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/spinlock.h>
 #include <linux/wait.h>
 
-struct list_head;
+struct media_evbase;
+struct media_subscribed_evbase;
+
+/**
+ * struct media_evbase - Internal kernel event struct.
+ * @list:	List node for the media_evbase_fh->available list.
+ * @mse:	Pointer to parent media_subscribed_evbase.
+ *
+ * The sub-system specific event payload must trail this event struct.
+ */
+struct media_evbase {
+	struct list_head	list;
+	struct media_subscribed_evbase *mse;
+};
 
 /*
  * struct media_evbase_fh - File handles for media subsystem events
@@ -37,4 +52,67 @@ struct media_evbase_fh {
 	u32			sequence;
 };
 
+/*
+ * struct media_subscribed_evbase_ops - Subscribed event operations.
+ *
+ * @add:	Called when a new listener is added.
+ * @del:	Called when a listener stops listening.
+ * @replace:	Callback that can replace event 'old' with event 'new'.
+ *		Returns true if the payload of 'new' was consumed, false if
+ *		the caller still needs to copy it.
+ * @merge:	Callback that can merge event 'old' into event 'new'.
+ */
+struct media_subscribed_evbase_ops {
+	int  (*add)(struct media_subscribed_evbase *mse, unsigned int elems);
+	void (*del)(struct media_subscribed_evbase *mse);
+	bool (*replace)(struct media_subscribed_evbase *mse,
+			struct media_evbase *old, const void *new);
+	void (*merge)(struct media_subscribed_evbase *mse,
+		      const struct media_evbase *old, struct media_evbase *new);
+};
+
+/**
+ * struct media_subscribed_evbase - Internal struct representing a subscribed
+ *		event.
+ *
+ * @list:	List node for the v4l2_fh->subscribed list.
+ * @fh:		Filehandle that subscribed to this event.
+ * @node:	List node that hooks into the object's event list
+ *		(if there is one).
+ * @ops:	sub-system specific operations
+ * @elems:	The number of elements in the events array.
+ * @first:	The index of the events containing the oldest available event.
+ * @in_use:	The number of queued events.
+ * @ev_size:	Size of the payload of a single event in the kernel
+ * @evspec:	The event subscription specifier. Sub-system specific.
+ *		Unique for a given subscription.
+ *
+ * The array of sub-system specific in-kernel event array is immediately
+ * followed by struct. The struct shall contain struct media_evbase as its first
+ * member, followed by sub-system specific fields. The size of a single entry in
+ * the array is specified by the ev_size field.
+ *
+ * The sub-system specific struct MUST contain this struct as its last field.
+ */
+struct media_subscribed_evbase {
+	struct list_head	list;
+	struct media_evbase_fh	*fh;
+	struct list_head	node;
+	const struct media_subscribed_evbase_ops *ops;
+	unsigned int		elems;
+	unsigned int		first;
+	unsigned int		in_use;
+	size_t			ev_size;
+	const void		*evspec;
+};
+
+/**
+ * struct media_evbase_subscription - Subscription of an event for a file handle
+ *
+ * A subscription of a given media event. Currently empty. The data is in the
+ * sub-system specific struct which this is a member of.
+ */
+struct media_evbase_subscription {
+};
+
 #endif /* MEDIA_EVBASE_H */
diff --git a/include/media/v4l2-event.h b/include/media/v4l2-event.h
index 3a0e2588361c..14c95a60700f 100644
--- a/include/media/v4l2-event.h
+++ b/include/media/v4l2-event.h
@@ -16,23 +16,24 @@
 #include <linux/videodev2.h>
 #include <linux/wait.h>
 
-struct v4l2_fh;
+#include <media/v4l2-fh.h>
+#include <media/media-evbase.h>
+
 struct v4l2_subdev;
 struct v4l2_subscribed_event;
 struct video_device;
 
 /**
- * struct v4l2_kevent - Internal kernel event struct.
- * @list:	List node for the v4l2_fh->available list.
- * @sev:	Pointer to parent v4l2_subscribed_event.
- * @event:	The event itself.
- * @ts:		The timestamp of the event.
+ * struct v4l2_event_spec - Event subscription parameters
+ *
+ * @type:	V4L2 event type, V4L2_EVENT_*.
+ * @id:		V4L2 event id.
+ * @flags:	V4L2 event flags, V4L2_EVENT_SUB_FL_*.
  */
-struct v4l2_kevent {
-	struct list_head	list;
-	struct v4l2_subscribed_event *sev;
-	struct v4l2_event	event;
-	u64			ts;
+struct v4l2_event_spec {
+	u32 type;
+	u32 id;
+	u32 flags;
 };
 
 /**
@@ -51,34 +52,63 @@ struct v4l2_subscribed_event_ops {
 };
 
 /**
- * struct v4l2_subscribed_event - Internal struct representing a subscribed
- *		event.
+ * struct v4l2_subscribed_event - Describes a single subscribed V4L2 event.
  *
- * @list:	List node for the v4l2_fh->subscribed list.
- * @type:	Event type.
- * @id:	Associated object ID (e.g. control ID). 0 if there isn't any.
- * @flags:	Copy of v4l2_event_subscription->flags.
- * @fh:	Filehandle that subscribed to this event.
- * @node:	List node that hooks into the object's event list
- *		(if there is one).
- * @ops:	v4l2_subscribed_event_ops
- * @elems:	The number of elements in the events array.
- * @first:	The index of the events containing the oldest available event.
- * @in_use:	The number of queued events.
- * @events:	An array of @elems events.
+ * @evspec:	Parameters of the subscribed event.
+ * @ops:	Operations for the subscribed event.
+ * @mse:	Common media event descriptor.
  */
 struct v4l2_subscribed_event {
-	struct list_head	list;
-	u32			type;
-	u32			id;
-	u32			flags;
-	struct v4l2_fh		*fh;
-	struct list_head	node;
+	struct v4l2_event_spec evspec;
 	const struct v4l2_subscribed_event_ops *ops;
-	unsigned int		elems;
-	unsigned int		first;
-	unsigned int		in_use;
-	struct v4l2_kevent	events[] __counted_by(elems);
+	struct media_subscribed_evbase mse;
+};
+
+/**
+ * struct v4l2_kevent - Internal kernel event struct.
+ * @mev:	generic media subsystem kernel event
+ * @event:	The V4L2 event itself.
+ * @ts:		The timestamp of the event.
+ */
+struct v4l2_kevent {
+	struct media_evbase mev;
+	struct v4l2_event event;
+	u64 ts;
+};
+
+/**
+ * to_v4l2_kevent - Convert a generic media kevent into a v4l2_kevent
+ *
+ * @mev: The generic media event
+ */
+static inline struct v4l2_kevent *to_v4l2_kevent(struct media_evbase *mev)
+{
+	return container_of(mev, struct v4l2_kevent, mev);
+}
+
+/**
+ * to_v4l2_kevent - Convert a const generic media_evbase into a v4l2_kevent
+ *
+ * @mev: The generic media event
+ *
+ * This is just like @to_v4l2_event, but const.
+ */
+static inline const struct v4l2_kevent *
+to_v4l2_kevent_const(const struct media_evbase *mev)
+{
+	return container_of(mev, const struct v4l2_kevent, mev);
+}
+
+/**
+ * to_v4l2_subscribed_event - Convert a generic media_subscribed_evbase into
+ *			      a v4l2_subscribed_event.
+ *
+ * @mse: The generic media event
+ */
+static inline struct v4l2_subscribed_event *
+to_v4l2_subscribed_event(struct media_subscribed_evbase *mse)
+{
+	return container_of(mse, struct v4l2_subscribed_event, mse);
 };
 
 /**
diff --git a/include/media/v4l2-fh.h b/include/media/v4l2-fh.h
index cd5bb7972699..c72083643c13 100644
--- a/include/media/v4l2-fh.h
+++ b/include/media/v4l2-fh.h
@@ -168,4 +168,16 @@ static inline int v4l2_fh_is_singular_file(struct file *filp)
 	return v4l2_fh_is_singular(filp->private_data);
 }
 
+/**
+ * media_evbase_fh_to_v4l2 - Return the struct v4l2_fh pointer from the
+ *			    media event fh
+ *
+ * @fh: pointer to &struct media_evbase_fh
+ */
+static inline struct v4l2_fh *
+media_evbase_fh_to_v4l2(struct media_evbase_fh *fh)
+{
+	return container_of(fh, struct v4l2_fh, ev_fh);
+}
+
 #endif /* V4L2_EVENT_H */

-- 
2.56.0


  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 ` [PATCH RFC v2 03/16] v4l: event: Separate event related information from struct v4l2_fh Mattijs Korpershoek
2026-11-18 12:43 ` Mattijs Korpershoek [this message]
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-4-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®