mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2
@ 2026-11-18 12:43 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
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening, Laurent Pinchart

On media-centric camera pipelines, every entity is known at boot time
and described statically (for example via device-tree).

The V4L2 framework assumes that every entity of the camera
pipeline is always present. This is a reasonable assumption for
integrated cameras in laptops or phones. However, in automotive systems,
cameras (e.g. rear-view or surround-view) can be damaged or disconnected.
When the framework assumes all sensors are present, a single missing
camera can prevent the entire pipeline from operating.

This is a known problem and has been discussed at various conferences:
* https://www.linuxtv.org/downloads/presentations/media_summit_2026/Michael%20-%20The%20Butterfly%20Effect.pdf
* https://www.linuxtv.org/news.php?entry=2022-11-14-0.hverkuil
* https://lore.kernel.org/linux-media/20261021224119.GA383235@killaraus.ideasonboard.com/T/#u

For example, in the following pipeline:
"""
  imx219  9-0010 -> ds90ub953 7-0044 -> ds90ub960 pad0 -+
                                                        |-- pad4 -> cdns_csi2rx -> ticsi2rx
  imx219 10-0010 -> ds90ub953 7-0045 -> ds90ub960 pad1 -+
"""

If imx219 9-0010 fails to probe, media-ctl -p will show:
"""
                    ds90ub953 7-0044 -> ds90ub960 pad0 -+
                                                        |-- pad4 -> cdns_csi2rx -> ticsi2rx
  imx219 10-0010 -> ds90ub953 7-0045 -> ds90ub960 pad1 -+
"""

Now, if ds90ub953 7-0044 fails to probe(), media-ctl -p will only show the
following topology:
"""
                                                                    cdns_csi2rx -> ticsi2rx
"""

To solve this, we chose to go for a simple solution inspired by
DRM connectors.

We created an ioctl for media entitities to report their current
connection status.
This allows applications to discover an entity state when discovering
the graph (using media-ctl -p, for example)
The entity status is then cached. When applications start streaming,
the status is polled on a regular basis and reported as a media event
every time it changes.
This mandates that the driver can probe even when the device is
not connected.

This has the benefits of being fairly simple to implement, understand
and support in drivers.

This new behaviour is opt-in. It is enabled via a module parameter
for the Media Controler: mc.fault_tolerant.

Patch 1-9 Add event support at media device level
Patch 10-14 Add entity status and ioctls to interact with it
Patch 15-16 implement this pattern switch for the imx219 driver.

With this, we can hot-plug the sensor and get notified via the media
device of the status:

[root@fedora]# ./media-event-monitor
Subscribed to ENTITY_REGISTRATION and ENTITY_STATUS_CHANGE on /dev/media0
Waiting for events (Ctrl-C to quit)...
event: type=2 (ENTITY_STATUS_CHANGE) id=0 seq=0 ts=403045107120 pending=0
  entity_id=40 status=DISCONNECTED
event: type=2 (ENTITY_STATUS_CHANGE) id=0 seq=1 ts=415292362587 pending=0
  entity_id=40 status=CONNECTED

The code for this demo is available at:
https://gitlab.com/-/snippets/6077614

The media device event code is mostly Sakari's work which can be found at:
https://git.linuxtv.org/sailus/media_tree.git/log/?h=partial-reg

Current known limitations:
* Only leaf entities (typically sensor nodes) are covered. We should
  also address entities that are in the middle of the media graph

This has been tested on a SK-AM69 board on top of the following series:
* https://lore.kernel.org/all/20260824121451.3348583-1-sakari.ailus@linux.intel.com/
* https://lore.kernel.org/all/20261104-j721e-csi2rx-crash-sd-enable-streams-v1-1-b77ee38c8dcf@kernel.org/

Thanks,
Mattijs

---
Changes in v2:
- Notifications are now done at media device level (Laurent, Nicolas)
- Connection status is tracked at entity level (Laurent)
- imx219: Fail the probe if CHIP_ID does not match (Dave)
- Dropped patch 'media: imx219: Move LP-11 state switch to power_on()'
  as we did not get any feedback from cadence on this
- Replaced VIDIOC_SUBDEV_G_CONNECTION_STATUS with a media controller
  ioctl instead: MEDIA_IOC_G_ENTITY_STATUS
- Do status polling in generic framework (Dave)
- Increase polling interval to 10s (Dave)
- Polling/Fault tolerance is only enabled when mc.faul_tolerance=1
  module param is set (Dave, Hans V, Sakari)
- Reworked .detect() polling to be only done when streaming is on. This
  should address the privacy led blinking concerns (Dave, Hans D)
- Link to v1: https://lore.kernel.org/r/20261001-v4l2-sensor-detect-v1-0-a45993be17b8@kernel.org

---
Laurent Pinchart (1):
      media: mc: Add per-file-handle data support

Mattijs Korpershoek (7):
      media: media.h: Add entity connection status
      media: mc: Add .detect() to media_entity_operations
      media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl
      media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event
      media: mc: Add detect polling helpers for drivers
      media: imx219: Allow driver probe with missing sensor
      media: imx219: Implement .detect() sensor operation

Sakari Ailus (8):
      media: mc: Add struct media_device_fh pointer to media device IOCTL op
      v4l: event: Separate event related information from struct v4l2_fh
      v4l: event: Rework data structures for separating events and V4L2
      v4l: event: Split the event framework into generic and V4L2 bits
      media: mc: Maintain a list of open file handles in a media device
      media: mc: Add MEDIA_IOC_DQEVENT
      media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION
      Documentation: media: Add Media controller event documentation

 .../userspace-api/media/mediactl/media-funcs.rst   |   3 +
 .../media/mediactl/media-ioc-dqevent.rst           | 130 +++++++++
 .../media/mediactl/media-ioc-g-entity-status.rst   |  92 ++++++
 .../media/mediactl/media-ioc-subscribe-event.rst   | 112 +++++++
 drivers/media/Kconfig                              |   3 +
 drivers/media/i2c/imx219.c                         | 117 +++++---
 drivers/media/mc/Kconfig                           |   3 +
 drivers/media/mc/Makefile                          |   3 +-
 drivers/media/mc/mc-device.c                       | 236 ++++++++++++++-
 drivers/media/mc/mc-devnode.c                      |  19 +-
 drivers/media/mc/mc-entity.c                       |  59 ++++
 drivers/media/mc/mc-evbase.c                       | 260 +++++++++++++++++
 drivers/media/mc/mc-event.c                        | 244 ++++++++++++++++
 drivers/media/pci/ivtv/ivtv-fileops.c              |   2 +-
 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               | 324 +++++++++------------
 drivers/media/v4l2-core/v4l2-fh.c                  |  12 +-
 include/media/media-device.h                       |  24 ++
 include/media/media-devnode.h                      |  18 +-
 include/media/media-entity.h                       |  77 +++++
 include/media/media-evbase.h                       | 200 +++++++++++++
 include/media/media-event.h                        | 181 ++++++++++++
 include/media/media-fh.h                           |  37 +++
 include/media/v4l2-event.h                         | 106 ++++---
 include/media/v4l2-fh.h                            |  32 +-
 include/uapi/linux/media.h                         |  51 +++-
 28 files changed, 2079 insertions(+), 338 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
change-id: 20260923-v4l2-sensor-detect-32a0b2936cc1

Best regards,
--  
Mattijs Korpershoek <mkorpershoek@kernel.org>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 01/16] media: mc: Add struct media_device_fh pointer to media device IOCTL op
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
@ 2026-11-18 12:43 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 02/16] media: mc: Add per-file-handle data support Mattijs Korpershoek
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

In order to use the struct media_device_fh pointer in IOCTL callbacks, add
it to the callback, between the Media device and the IOCTL argument.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/mc/mc-device.c | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index f8ea7cfe10b2..32e5c9f02ed4 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -55,7 +55,8 @@ static int media_device_close(struct file *filp)
 	return 0;
 }
 
-static long media_device_get_info(struct media_device *dev, void *arg)
+static long media_device_get_info(struct media_device *dev,
+				  struct file *file, void *arg)
 {
 	struct media_device_info *info = arg;
 
@@ -95,7 +96,8 @@ static struct media_entity *find_entity(struct media_device *mdev, u32 id)
 	return NULL;
 }
 
-static long media_device_enum_entities(struct media_device *mdev, void *arg)
+static long media_device_enum_entities(struct media_device *mdev,
+				       struct file *file, void *arg)
 {
 	struct media_entity_desc *entd = arg;
 	struct media_entity *ent;
@@ -148,7 +150,8 @@ static void media_device_kpad_to_upad(const struct media_pad *kpad,
 	upad->flags = kpad->flags;
 }
 
-static long media_device_enum_links(struct media_device *mdev, void *arg)
+static long media_device_enum_links(struct media_device *mdev,
+				    struct file *file, void *arg)
 {
 	struct media_links_enum *links = arg;
 	struct media_entity *entity;
@@ -197,7 +200,8 @@ static long media_device_enum_links(struct media_device *mdev, void *arg)
 	return 0;
 }
 
-static long media_device_setup_link(struct media_device *mdev, void *arg)
+static long media_device_setup_link(struct media_device *mdev,
+				    struct file *file, void *arg)
 {
 	struct media_link_desc *linkd = arg;
 	struct media_link *link = NULL;
@@ -227,7 +231,8 @@ static long media_device_setup_link(struct media_device *mdev, void *arg)
 	return __media_entity_setup_link(link, linkd->flags);
 }
 
-static long media_device_get_topology(struct media_device *mdev, void *arg)
+static long media_device_get_topology(struct media_device *mdev,
+				      struct file *file, void *arg)
 {
 	struct media_v2_topology *topo = arg;
 	struct media_entity *entity;
@@ -370,7 +375,8 @@ static long media_device_get_topology(struct media_device *mdev, void *arg)
 	return ret;
 }
 
-static long media_device_request_alloc(struct media_device *mdev, void *arg)
+static long media_device_request_alloc(struct media_device *mdev,
+				       struct file *file, void *arg)
 {
 	int *alloc_fd = arg;
 
@@ -417,7 +423,8 @@ static long copy_arg_to_user(void __user *uarg, void *karg, unsigned int cmd)
 struct media_ioctl_info {
 	unsigned int cmd;
 	unsigned short flags;
-	long (*fn)(struct media_device *dev, void *arg);
+	long (*fn)(struct media_device *dev, struct file *file,
+		   void *arg);
 	long (*arg_from_user)(void *karg, void __user *uarg, unsigned int cmd);
 	long (*arg_to_user)(void __user *uarg, void *karg, unsigned int cmd);
 };
@@ -462,7 +469,7 @@ static long media_device_ioctl(struct file *filp, unsigned int cmd,
 	if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
 		mutex_lock(&dev->graph_mutex);
 
-	ret = info->fn(dev, karg);
+	ret = info->fn(dev, filp, karg);
 
 	if (info->flags & MEDIA_IOC_FL_GRAPH_MUTEX)
 		mutex_unlock(&dev->graph_mutex);
@@ -487,6 +494,7 @@ struct media_links_enum32 {
 };
 
 static long media_device_enum_links32(struct media_device *mdev,
+				      struct file *file,
 				      struct media_links_enum32 __user *ulinks)
 {
 	struct media_links_enum links;
@@ -503,7 +511,7 @@ static long media_device_enum_links32(struct media_device *mdev,
 	links.pads = compat_ptr(pads_ptr);
 	links.links = compat_ptr(links_ptr);
 
-	ret = media_device_enum_links(mdev, &links);
+	ret = media_device_enum_links(mdev, file, &links);
 	if (ret)
 		return ret;
 
@@ -525,8 +533,8 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
 	switch (cmd) {
 	case MEDIA_IOC_ENUM_LINKS32:
 		mutex_lock(&dev->graph_mutex);
-		ret = media_device_enum_links32(dev,
-				(struct media_links_enum32 __user *)arg);
+		ret = media_device_enum_links32(dev, filp,
+						(struct media_links_enum32 __user *)arg);
 		mutex_unlock(&dev->graph_mutex);
 		break;
 

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 02/16] media: mc: Add per-file-handle data support
  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 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 03/16] v4l: event: Separate event related information from struct v4l2_fh Mattijs Korpershoek
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening, Laurent Pinchart

From: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>

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@ideasonboard.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 03/16] v4l: event: Separate event related information from struct v4l2_fh
  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
  2026-11-18 12:43 ` [PATCH RFC v2 04/16] v4l: event: Rework data structures for separating events and V4L2 Mattijs Korpershoek
                   ` (12 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 04/16] v4l: event: Rework data structures for separating events and V4L2
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (2 preceding siblings ...)
  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
  2026-11-18 12:43 ` [PATCH RFC v2 05/16] v4l: event: Split the event framework into generic and V4L2 bits Mattijs Korpershoek
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 05/16] v4l: event: Split the event framework into generic and V4L2 bits
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (3 preceding siblings ...)
  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 ` 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
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

Split the functionality of the V4L2 event framework into generic and V4L2
specific parts. This enables using the same infrastructure for
implementing events for the Media controller API.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/Kconfig                |   2 +
 drivers/media/mc/Kconfig             |   3 +
 drivers/media/mc/Makefile            |   1 +
 drivers/media/mc/mc-evbase.c         | 260 ++++++++++++++++++++++++++++++++++
 drivers/media/v4l2-core/v4l2-event.c | 264 ++++++++++-------------------------
 include/media/media-evbase.h         |  82 +++++++++++
 include/media/v4l2-event.h           |   6 +-
 7 files changed, 425 insertions(+), 193 deletions(-)

diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index 6abc9302cd84..70e090757492 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -162,6 +162,7 @@ config VIDEO_DEV
 	default MEDIA_CAMERA_SUPPORT || MEDIA_ANALOG_TV_SUPPORT || MEDIA_RADIO_SUPPORT || MEDIA_SDR_SUPPORT || MEDIA_PLATFORM_SUPPORT || MEDIA_TEST_SUPPORT
 	depends on (I2C || I2C=n)
 	select RATIONAL
+	select MEDIA_EVENT
 	select VIDEOBUF2_V4L2 if VIDEOBUF2_CORE
 	help
 	  Enables the V4L2 API, used by cameras, analog TV, video grabbers,
@@ -170,6 +171,7 @@ config VIDEO_DEV
 config MEDIA_CONTROLLER
 	bool "Media Controller API"
 	default MEDIA_CAMERA_SUPPORT || MEDIA_ANALOG_TV_SUPPORT || MEDIA_DIGITAL_TV_SUPPORT || MEDIA_PLATFORM_SUPPORT
+	select MEDIA_EVENT
 	help
 	  Enable the media controller API used to query media devices internal
 	  topology and configure it dynamically.
diff --git a/drivers/media/mc/Kconfig b/drivers/media/mc/Kconfig
index c82b07d2ef36..9593d7c5b64d 100644
--- a/drivers/media/mc/Kconfig
+++ b/drivers/media/mc/Kconfig
@@ -11,3 +11,6 @@ config MEDIA_CONTROLLER_DVB
 	  Enable the media controller API support for DVB.
 
 	  This is currently experimental.
+
+config MEDIA_EVENT
+	tristate
diff --git a/drivers/media/mc/Makefile b/drivers/media/mc/Makefile
index 2b7af42ba59c..d501327436a5 100644
--- a/drivers/media/mc/Makefile
+++ b/drivers/media/mc/Makefile
@@ -8,3 +8,4 @@ ifneq ($(CONFIG_USB),)
 endif
 
 obj-$(CONFIG_MEDIA_SUPPORT) += mc.o
+obj-$(CONFIG_MEDIA_EVENT) += mc-evbase.o
diff --git a/drivers/media/mc/mc-evbase.c b/drivers/media/mc/mc-evbase.c
new file mode 100644
index 000000000000..7e20f7eac9f8
--- /dev/null
+++ b/drivers/media/mc/mc-evbase.c
@@ -0,0 +1,260 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Media event base
+ *
+ * Copyright (C) 2018--2019 Intel Corporation
+ * Copyright (C) 2009--2010 Nokia Corporation.
+ *
+ * Based on V4L2 events in drivers/media/v4l2-core/v4l2-events.c .
+ */
+
+#include <linux/errno.h>
+#include <linux/lockdep.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include <media/media-evbase.h>
+
+/* Caller must hold &fh->lock! */
+struct media_subscribed_evbase *
+media_evbase_is_subscribed(struct media_evbase_fh *fh, const void *evspec)
+{
+	struct media_subscribed_evbase *mse;
+
+	assert_spin_locked(fh->lock);
+
+	list_for_each_entry(mse, &fh->subscribed, list)
+		if (mse->ops->is_subscribed(mse, evspec))
+			return mse;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(media_evbase_is_subscribed);
+
+struct media_subscribed_evbase *
+media_evbase_is_queueable(struct media_evbase_fh *fh, const void *ev)
+{
+	struct media_subscribed_evbase *mse;
+
+	assert_spin_locked(fh->lock);
+
+	list_for_each_entry(mse, &fh->subscribed, list)
+		if (mse->ops->is_queueable(mse, ev))
+			return mse;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(media_evbase_is_queueable);
+
+/* Caller must hold &fh->lock and &fh->subscribe_lock! */
+static void __media_evbase_unsubscribe(struct media_subscribed_evbase *mse)
+{
+	struct media_evbase_fh *fh = mse->fh;
+	unsigned int i;
+
+	lockdep_assert_held(&fh->subscribe_lock);
+	assert_spin_locked(fh->lock);
+
+	/* Remove any pending events for this subscription */
+	for (i = 0; i < mse->in_use; i++) {
+		list_del(&media_evbase_ptr(mse, i)->list);
+		fh->navailable--;
+	}
+	list_del(&mse->list);
+}
+
+static void media_evbase_unsubscribe_one(struct media_subscribed_evbase *mse)
+{
+	struct media_evbase_fh *fh = mse->fh;
+	unsigned long flags;
+
+	lockdep_assert_held(&fh->subscribe_lock);
+
+	spin_lock_irqsave(fh->lock, flags);
+	__media_evbase_unsubscribe(mse);
+	spin_unlock_irqrestore(fh->lock, flags);
+
+	mse->ops->del(mse);
+}
+
+struct media_evbase *__media_evbase_dequeue(struct media_evbase_fh *fh)
+{
+	struct media_evbase *kev;
+
+	assert_spin_locked(fh->lock);
+
+	if (list_empty(&fh->available))
+		return NULL;
+
+	WARN_ON(fh->navailable == 0);
+
+	kev = list_first_entry(&fh->available, struct media_evbase, list);
+	list_del(&kev->list);
+	fh->navailable--;
+
+	kev->mse->first = media_evbase_pos(kev->mse, 1);
+	kev->mse->in_use--;
+
+	return kev;
+}
+EXPORT_SYMBOL_GPL(__media_evbase_dequeue);
+
+void __media_evbase_queue_fh(struct media_evbase_fh *fh, const void *ev, u64 ts)
+{
+	struct media_subscribed_evbase *mse;
+	struct media_evbase *kev;
+	bool copy_payload = true;
+
+	/* Are we subscribed? */
+	mse = media_evbase_is_queueable(fh, ev);
+	if (mse == NULL)
+		return;
+
+	/* Increase event sequence number on fh. */
+	fh->sequence++;
+
+	/* Do we have any free events? */
+	if (mse->in_use == mse->elems) {
+		/* no, remove the oldest one */
+		kev = media_evbase_ptr(mse, 0);
+		list_del(&kev->list);
+		mse->in_use--;
+		mse->first = media_evbase_pos(mse, 1);
+		fh->navailable--;
+		if (mse->elems == 1) {
+			if (mse->ops->replace(mse, kev, ev))
+				copy_payload = false;
+		} else {
+			mse->ops->merge(mse, kev, media_evbase_ptr(mse, 0));
+		}
+	}
+
+	/* Take one and fill it. */
+	kev = media_evbase_ptr(mse, mse->in_use);
+	mse->ops->queue(kev, ev, ts, fh->sequence, copy_payload);
+	mse->in_use++;
+	list_add_tail(&kev->list, &fh->available);
+
+	fh->navailable++;
+
+	wake_up_all(fh->wait);
+}
+EXPORT_SYMBOL_GPL(__media_evbase_queue_fh);
+
+void media_evbase_queue_fh(struct media_evbase_fh *fh, const void *ev, u64 ts)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(fh->lock, flags);
+	__media_evbase_queue_fh(fh, ev, ts);
+	spin_unlock_irqrestore(fh->lock, flags);
+}
+EXPORT_SYMBOL_GPL(media_evbase_queue_fh);
+
+int media_evbase_subscribe(struct media_evbase_fh *fh,
+			  struct media_subscribed_evbase *mse,
+			  unsigned int elems)
+{
+	struct media_subscribed_evbase *found_mse;
+	unsigned long flags;
+	int ret = 0;
+
+	/* Require all callbacks in order to avoid checks elsewhere */
+	if (WARN_ON(!mse->ops->add || !mse->ops->del || !mse->ops->replace ||
+		    !mse->ops->merge || !mse->ops->queue ||
+		    !mse->ops->is_subscribed || !mse->ops->is_queueable ||
+		    !mse->ops->release))
+		return -EINVAL;
+
+	if (elems < 1)
+		return -EINVAL;
+
+	mse->elems = elems;
+
+	mutex_lock(&fh->subscribe_lock);
+
+	spin_lock_irqsave(fh->lock, flags);
+	found_mse = media_evbase_is_subscribed(fh, mse->evspec);
+	if (!found_mse)
+		list_add(&mse->list, &fh->subscribed);
+	spin_unlock_irqrestore(fh->lock, flags);
+
+	if (found_mse) {
+		/* Already listening */
+		mse->ops->release(mse);
+	} else if (mse->ops && mse->ops->add) {
+		unsigned int i;
+
+		for (i = 0; i < elems; i++)
+			media_evbase_ptr(mse, i)->mse = mse;
+
+		ret = mse->ops->add(mse, elems);
+		if (ret) {
+			spin_lock_irqsave(fh->lock, flags);
+			__media_evbase_unsubscribe(mse);
+			spin_unlock_irqrestore(fh->lock, flags);
+			mse->ops->release(mse);
+		}
+	}
+
+	mutex_unlock(&fh->subscribe_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(media_evbase_subscribe);
+
+void media_evbase_unsubscribe_all(struct media_evbase_fh *fh)
+{
+	struct media_subscribed_evbase *mse;
+
+	mutex_lock(&fh->subscribe_lock);
+
+	do {
+		unsigned long flags;
+
+		mse = NULL;
+
+		spin_lock_irqsave(fh->lock, flags);
+
+		if (!list_empty(&fh->subscribed))
+			mse = list_first_entry(&fh->subscribed,
+					       struct media_subscribed_evbase,
+					       list);
+
+		spin_unlock_irqrestore(fh->lock, flags);
+
+		if (mse) {
+			media_evbase_unsubscribe_one(mse);
+			mse->ops->release(mse);
+		}
+	} while (mse);
+
+	mutex_unlock(&fh->subscribe_lock);
+}
+EXPORT_SYMBOL_GPL(media_evbase_unsubscribe_all);
+
+void media_evbase_unsubscribe(struct media_evbase_fh *fh, const void *evspec)
+{
+	struct media_subscribed_evbase *mse;
+	unsigned long flags;
+
+	mutex_lock(&fh->subscribe_lock);
+
+	spin_lock_irqsave(fh->lock, flags);
+	mse = media_evbase_is_subscribed(fh, evspec);
+	spin_unlock_irqrestore(fh->lock, flags);
+
+	if (mse)
+		media_evbase_unsubscribe_one(mse);
+
+	mutex_unlock(&fh->subscribe_lock);
+
+	if (mse)
+		mse->ops->release(mse);
+}
+EXPORT_SYMBOL_GPL(media_evbase_unsubscribe);
+
+MODULE_AUTHOR("Sakari Ailus <sakari.ailus@linux.intel.com>");
+MODULE_DESCRIPTION("Media controller event base framework");
+MODULE_LICENSE("GPL");
diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index c8371a4fa90b..3e784e44e6e7 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -4,6 +4,7 @@
  *
  * V4L2 events.
  *
+ * Copyright (C) 2019 Intel Corporation
  * Copyright (C) 2009--2010 Nokia Corporation.
  *
  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
@@ -18,20 +19,6 @@
 #include <linux/slab.h>
 #include <linux/export.h>
 
-static inline size_t media_evbase_pos(const struct media_subscribed_evbase *mse,
-				      unsigned int 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);
@@ -75,41 +62,83 @@ static void v4l2_evop_merge(struct media_subscribed_evbase *mse,
 				&to_v4l2_kevent(new)->event);
 }
 
+static void v4l2_evop_queue(struct media_evbase *kev, const void *__ev,
+			    u64 ts, u32 sequence, bool copy_payload)
+{
+	struct v4l2_event *old = &to_v4l2_kevent(kev)->event;
+	const struct v4l2_event *new = __ev;
+	struct timespec64 ts64;
+
+	old->type = new->type;
+	if (copy_payload)
+		old->u = new->u;
+	old->id = new->id;
+	ts64 = ns_to_timespec64(ts);
+	old->timestamp.tv_sec = ts64.tv_sec;
+	old->timestamp.tv_nsec = ts64.tv_nsec;
+	old->sequence = sequence;
+	to_v4l2_kevent(kev)->ts = ts;
+}
+
+static bool v4l2_evop_is_subscribed(struct media_subscribed_evbase *mse,
+				    const void *__evspec)
+{
+	const struct v4l2_event_spec *evspec = __evspec;
+	struct v4l2_subscribed_event *sev = to_v4l2_subscribed_event(mse);
+
+	return sev->evspec.type == evspec->type &&
+		sev->evspec.id == evspec->id;
+}
+
+static bool v4l2_evup_is_queueable(struct media_subscribed_evbase *mse,
+				   const void *__ev)
+{
+	const struct v4l2_event *ev = __ev;
+	struct v4l2_subscribed_event *sev =
+		container_of(mse, struct v4l2_subscribed_event, mse);
+
+	return sev->evspec.type == ev->type && sev->evspec.id == ev->id;
+}
+
+static void v4l2_evop_release(struct media_subscribed_evbase *mse)
+{
+	kvfree(to_v4l2_subscribed_event(mse));
+}
+
 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,
+	.queue          = v4l2_evop_queue,
+	.is_subscribed  = v4l2_evop_is_subscribed,
+	.is_queueable   = v4l2_evup_is_queueable,
+	.release        = v4l2_evop_release,
 };
 
 static int __v4l2_event_dequeue(struct media_evbase_fh *fh,
 				struct v4l2_event *event)
 {
+	struct media_evbase *mev;
 	struct v4l2_kevent *kev;
 	struct timespec64 ts;
 	unsigned long flags;
 
 	spin_lock_irqsave(fh->lock, flags);
 
-	if (list_empty(&fh->available)) {
+	mev = __media_evbase_dequeue(fh);
+	if (!mev) {
 		spin_unlock_irqrestore(fh->lock, flags);
 		return -ENOENT;
 	}
 
-	WARN_ON(fh->navailable == 0);
-
-	kev = to_v4l2_kevent(list_first_entry(&fh->available,
-					      struct media_evbase, list));
-	list_del(&kev->mev.list);
-	fh->navailable--;
+	kev = container_of(mev, struct v4l2_kevent, mev);
 
-	kev->event.pending = fh->navailable;
 	*event = kev->event;
 	ts = ns_to_timespec64(kev->ts);
 	event->timestamp.tv_sec = ts.tv_sec;
 	event->timestamp.tv_nsec = ts.tv_nsec;
-	kev->mev.mse->first = media_evbase_pos(kev->mev.mse, 1);
-	kev->mev.mse->in_use--;
+	event->pending = fh->navailable;
 
 	spin_unlock_irqrestore(fh->lock, flags);
 
@@ -144,76 +173,6 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
 }
 EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
 
-/* Caller must hold fh->lock! */
-static struct v4l2_subscribed_event *v4l2_event_subscribed(
-	struct media_evbase_fh *fh, u32 type, u32 id)
-{
-	struct media_subscribed_evbase *mse;
-
-	assert_spin_locked(fh->lock);
-
-	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;
-}
-
-static void __v4l2_event_queue_fh(struct media_evbase_fh *fh,
-				  const struct v4l2_event *ev, u64 ts)
-{
-	struct v4l2_subscribed_event *sev;
-	struct v4l2_kevent *kev;
-	bool copy_payload = true;
-
-	/* Are we subscribed? */
-	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
-	if (sev == NULL)
-		return;
-
-	/* Increase event sequence number on fh. */
-	fh->sequence++;
-
-	/* Do we have any free events? */
-	if (sev->mse.in_use == sev->mse.elems) {
-		/* no, remove the oldest one */
-		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->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 =
-				to_v4l2_kevent(media_evbase_ptr(&sev->mse, 0));
-			sev->ops->merge(&kev->event, &second_oldest->event);
-		}
-	}
-
-	/* Take one and fill it. */
-	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->mse.in_use++;
-	list_add_tail(&kev->mev.list, &fh->available);
-
-	fh->navailable++;
-
-	wake_up_all(fh->wait);
-}
-
 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
 {
 	struct v4l2_fh *fh;
@@ -228,26 +187,15 @@ 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_fh, ev, ts);
+		__media_evbase_queue_fh(&fh->ev_fh, ev, ts);
 
 	spin_unlock_irqrestore(&vdev->fh_lock, flags);
 }
 EXPORT_SYMBOL_GPL(v4l2_event_queue);
 
-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->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->ev_fh.navailable;
+	return media_evbase_pending(&fh->ev_fh);
 }
 EXPORT_SYMBOL_GPL(v4l2_event_pending);
 
@@ -268,37 +216,17 @@ void v4l2_event_wake_all(struct video_device *vdev)
 }
 EXPORT_SYMBOL_GPL(v4l2_event_wake_all);
 
-static void __v4l2_event_unsubscribe(struct v4l2_subscribed_event *sev)
-{
-	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->mse.in_use; i++) {
-		list_del(&media_evbase_ptr(&sev->mse, i)->list);
-		fh->ev_fh.navailable--;
-	}
-	list_del(&sev->mse.list);
-}
-
 int v4l2_event_subscribe(struct v4l2_fh *fh,
 			 const struct v4l2_event_subscription *sub, unsigned int elems,
 			 const struct v4l2_subscribed_event_ops *ops)
 {
-	struct v4l2_subscribed_event *sev, *found_ev;
-	unsigned long flags;
-	unsigned int i;
-	int ret = 0;
+	struct media_subscribed_evbase *mse;
+	struct v4l2_subscribed_event *sev;
 
 	if (sub->type == V4L2_EVENT_ALL)
 		return -EINVAL;
 
-	if (elems < 1)
-		elems = 1;
+	elems = max(elems, 1U);
 
 	sev = kvzalloc(sizeof(*sev) + array_size(sizeof(struct v4l2_kevent),
 						 elems), GFP_KERNEL);
@@ -306,93 +234,45 @@ int v4l2_event_subscribe(struct v4l2_fh *fh,
 		return -ENOMEM;
 	sev->mse.elems = elems;
 	sev->mse.ev_size = sizeof(struct v4l2_kevent);
-	for (i = 0; i < elems; i++)
+	for (unsigned int i = 0; i < elems; i++)
 		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);
-
-	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->mse.list, &fh->ev_fh.subscribed);
-	spin_unlock_irqrestore(fh->ev_fh.lock, flags);
-
-	if (found_ev) {
-		/* Already listening */
-		kvfree(sev);
-	} else if (sev->ops && sev->ops->add) {
-		ret = sev->ops->add(sev, elems);
-		if (ret) {
-			spin_lock_irqsave(&fh->vdev->fh_lock, flags);
-			__v4l2_event_unsubscribe(sev);
-			spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
-			kvfree(sev);
-		}
-	}
+	mse = &sev->mse;
 
-	mutex_unlock(&fh->ev_fh.subscribe_lock);
+	mse->ops = &v4l2_event_ops;
+	mse->fh = &fh->ev_fh;
+	mse->evspec = &sev->evspec;
 
-	return ret;
+	return media_evbase_subscribe(&fh->ev_fh, mse, elems);
 }
 EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
 
 void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
 {
-	struct v4l2_event_subscription sub;
-	struct v4l2_subscribed_event *sev;
-	unsigned long flags;
-
-	do {
-		sev = NULL;
-
-		spin_lock_irqsave(fh->ev_fh.lock, flags);
-		if (!list_empty(&fh->ev_fh.subscribed)) {
-			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)
-			v4l2_event_unsubscribe(fh, &sub);
-	} while (sev);
+	media_evbase_unsubscribe_all(&fh->ev_fh);
 }
 EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
 
 int v4l2_event_unsubscribe(struct v4l2_fh *fh,
 			   const struct v4l2_event_subscription *sub)
 {
-	struct v4l2_subscribed_event *sev;
-	unsigned long flags;
+	struct v4l2_event_spec evspec = {
+		.type = sub->type,
+		.id = sub->id,
+		.flags = sub->flags,
+	};
 
 	if (sub->type == V4L2_EVENT_ALL) {
 		v4l2_event_unsubscribe_all(fh);
 		return 0;
 	}
 
-	mutex_lock(&fh->ev_fh.subscribe_lock);
-
-	spin_lock_irqsave(fh->ev_fh.lock, flags);
-
-	sev = v4l2_event_subscribed(&fh->ev_fh, sub->type, sub->id);
-	if (sev != NULL)
-		__v4l2_event_unsubscribe(sev);
-
-	spin_unlock_irqrestore(fh->ev_fh.lock, flags);
-
-	if (sev && sev->ops && sev->ops->del)
-		sev->ops->del(sev);
-
-	mutex_unlock(&fh->ev_fh.subscribe_lock);
-
-	kvfree(sev);
+	media_evbase_unsubscribe(&fh->ev_fh, &evspec);
 
 	return 0;
 }
diff --git a/include/media/media-evbase.h b/include/media/media-evbase.h
index bfb36a946386..06993f9d2f6a 100644
--- a/include/media/media-evbase.h
+++ b/include/media/media-evbase.h
@@ -61,6 +61,10 @@ struct media_evbase_fh {
  *		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'.
+ * @queue:	Copy a user-provided event to the event queue.
+ * @is_subscribed: Tell whether a specified subscription already exists.
+ * @is_queueable: Tell whether a given event matches a subscription.
+ * @release:	Called to release a media event subscription.
  */
 struct media_subscribed_evbase_ops {
 	int  (*add)(struct media_subscribed_evbase *mse, unsigned int elems);
@@ -69,6 +73,13 @@ struct media_subscribed_evbase_ops {
 			struct media_evbase *old, const void *new);
 	void (*merge)(struct media_subscribed_evbase *mse,
 		      const struct media_evbase *old, struct media_evbase *new);
+	void (*queue)(struct media_evbase *kev, const void *__ev, u64 ts,
+		      u32 sequence, bool copy_payload);
+	bool (*is_subscribed)(struct media_subscribed_evbase *mse,
+			      const void *evspec);
+	bool (*is_queueable)(struct media_subscribed_evbase *mse,
+			     const void *ev);
+	void (*release)(struct media_subscribed_evbase *mse);
 };
 
 /**
@@ -115,4 +126,75 @@ struct media_subscribed_evbase {
 struct media_evbase_subscription {
 };
 
+/**
+ * media_evbase_pos - Tell the position of the event structure in the event array
+ *
+ * @mse: The media event subscription
+ * @idx: Position in the event array from the first used event (mse->first)
+ */
+static inline size_t media_evbase_pos(const struct media_subscribed_evbase *mse,
+				     unsigned int idx)
+{
+	idx += mse->first;
+	return idx >= mse->elems ? idx - mse->elems : idx;
+}
+
+/**
+ * media_evbase_ptr - Tell the pointer to the next event structure in the event
+ *		     array
+ *
+ * @mse: The media event subscription
+ * @idx: Position in the event array from the first used event (mse->first)
+ */
+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;
+}
+
+/* Caller must hold fh->lock! */
+struct media_subscribed_evbase *
+media_evbase_is_subscribed(struct media_evbase_fh *fh, const void *evspec);
+
+struct media_evbase *__media_evbase_dequeue(struct media_evbase_fh *fh);
+
+void media_evbase_unsubscribe(struct media_evbase_fh *fh, const void *evspec);
+
+void media_evbase_unsubscribe_all(struct media_evbase_fh *fh);
+
+/**
+ * __media_evbase_queue_fh - Queue events to the media event file descriptor
+ *
+ * @fh: pointer to &struct media_evbase_fh
+ * @ev: pointer to &struct v4l2_event
+ *
+ * Queue an event to the media event file descriptor. The caller is responsible
+ * for acquiring fh->lock before calling this function.
+ */
+void __media_evbase_queue_fh(struct media_evbase_fh *fh, const void *ev, u64 ts);
+
+/**
+ * __media_evbase_queue_fh - Queue events to the media event file descriptor
+ *
+ * @fh: pointer to &struct media_evbase_fh
+ * @ev: pointer to &struct v4l2_event
+ *
+ * Queue an event to the media event file descriptor. This function acquires and
+ * releases fh->lock.
+ */
+void media_evbase_queue_fh(struct media_evbase_fh *fh, const void *ev, u64 ts);
+
+static inline int media_evbase_pending(struct media_evbase_fh *fh)
+{
+	return fh->navailable;
+}
+
+int media_evbase_subscribe(struct media_evbase_fh *fh,
+			  struct media_subscribed_evbase *mse,
+			  unsigned int elems);
+
+struct media_subscribed_evbase *
+media_evbase_is_queueable(struct media_evbase_fh *fh, const void *ev);
+
 #endif /* MEDIA_EVBASE_H */
diff --git a/include/media/v4l2-event.h b/include/media/v4l2-event.h
index 14c95a60700f..7f3e1373ef8e 100644
--- a/include/media/v4l2-event.h
+++ b/include/media/v4l2-event.h
@@ -148,7 +148,11 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
  *    The driver's only responsibility is to fill in the type and the data
  *    fields. The other fields will be filled in by V4L2.
  */
-void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev);
+static inline void v4l2_event_queue_fh(struct v4l2_fh *fh,
+				       const struct v4l2_event *ev)
+{
+	media_evbase_queue_fh(&fh->ev_fh, ev, ktime_get_ns());
+}
 
 /**
  * v4l2_event_wake_all - Wake all filehandles.

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 06/16] media: mc: Maintain a list of open file handles in a media device
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (4 preceding siblings ...)
  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 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 07/16] media: mc: Add MEDIA_IOC_DQEVENT Mattijs Korpershoek
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

The list of file handles is needed to deliver media events in the future.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/mc/mc-device.c | 35 +++++++++++++++++++++++++----------
 include/media/media-device.h |  7 +++++++
 include/media/media-fh.h     | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 8d0c35a11944..b64322efd99e 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -22,6 +22,7 @@
 #include <media/media-device.h>
 #include <media/media-devnode.h>
 #include <media/media-entity.h>
+#include <media/media-fh.h>
 #include <media/media-request.h>
 
 #ifdef CONFIG_MEDIA_CONTROLLER
@@ -35,16 +36,6 @@
 #define MEDIA_ENT_SUBTYPE_MASK			0x0000ffff
 #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
  */
@@ -56,7 +47,10 @@ static inline void __user *media_get_uptr(__u64 arg)
 
 static int media_device_open(struct file *filp)
 {
+	struct media_devnode *devnode = filp->private_data;
+	struct media_device *mdev = devnode->media_dev;
 	struct media_device_fh *fh;
+	unsigned long flags;
 
 	fh = kzalloc_obj(*fh);
 	if (!fh)
@@ -64,12 +58,25 @@ static int media_device_open(struct file *filp)
 
 	filp->private_data = &fh->fh;
 
+	spin_lock_irqsave(&mdev->fh_list_lock, flags);
+	list_add(&fh->mdev_list, &mdev->fh_list);
+	spin_unlock_irqrestore(&mdev->fh_list_lock, flags);
+
 	return 0;
 }
 
 static int media_device_close(struct file *filp)
 {
+	struct media_devnode *devnode = media_devnode_data(filp);
+	struct media_device *mdev = devnode->media_dev;
 	struct media_device_fh *fh = media_device_fh(filp);
+	unsigned long flags;
+
+	if (mdev) {
+		spin_lock_irqsave(&mdev->fh_list_lock, flags);
+		list_del(&fh->mdev_list);
+		spin_unlock_irqrestore(&mdev->fh_list_lock, flags);
+	}
 
 	kfree(fh);
 
@@ -732,12 +739,14 @@ void media_device_init(struct media_device *mdev)
 	INIT_LIST_HEAD(&mdev->pads);
 	INIT_LIST_HEAD(&mdev->links);
 	INIT_LIST_HEAD(&mdev->entity_notify);
+	INIT_LIST_HEAD(&mdev->fh_list);
 
 	mutex_init(&mdev->req_queue_mutex);
 	mutex_init(&mdev->graph_mutex);
 	ida_init(&mdev->entity_internal_idx);
 
 	atomic_set(&mdev->request_id, 0);
+	spin_lock_init(&mdev->fh_list_lock);
 
 	if (!*mdev->bus_info)
 		media_set_bus_info(mdev->bus_info, sizeof(mdev->bus_info),
@@ -843,6 +852,7 @@ void media_device_unregister(struct media_device *mdev)
 	struct media_entity *next;
 	struct media_interface *intf, *tmp_intf;
 	struct media_entity_notify *notify, *nextp;
+	unsigned long flags;
 
 	if (mdev == NULL)
 		return;
@@ -883,6 +893,11 @@ void media_device_unregister(struct media_device *mdev)
 	dev_dbg(mdev->dev, "Media device unregistered\n");
 
 	debugfs_remove_recursive(mdev->media_dir);
+
+	spin_lock_irqsave(&mdev->fh_list_lock, flags);
+	list_del_init(&mdev->fh_list);
+	spin_unlock_irqrestore(&mdev->fh_list_lock, flags);
+
 	device_remove_file(&mdev->devnode->dev, &dev_attr_model);
 	media_devnode_unregister(mdev->devnode);
 	/* devnode free is handled in media_devnode_*() */
diff --git a/include/media/media-device.h b/include/media/media-device.h
index 749c327e3c58..2e737fec1afc 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -112,6 +112,10 @@ struct media_device_ops {
  * @media_dir:	DebugFS media directory
  * @request_id: Used to generate unique request IDs
  *
+ * @fh_list:	List of file handles in the media device
+ *		(struct media_device_fh.mdev_list).
+ * @fh_list_lock: Serialise access to fh_list list.
+ *
  * This structure represents an abstract high-level media device. It allows easy
  * access to entities and provides basic media device-level support. The
  * structure can be allocated directly or embedded in a larger structure.
@@ -189,6 +193,9 @@ struct media_device {
 	/* debugfs */
 	struct dentry *media_dir;
 	atomic_t request_id;
+
+	struct list_head fh_list;
+	spinlock_t fh_list_lock;
 };
 
 /* We don't need to include usb.h here */
diff --git a/include/media/media-fh.h b/include/media/media-fh.h
new file mode 100644
index 000000000000..14ae4daaaddd
--- /dev/null
+++ b/include/media/media-fh.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Media device file handle
+ *
+ * Copyright (C) 2019 Intel Corporation
+ */
+
+#ifndef MEDIA_FH_H
+#define MEDIA_FH_H
+
+#include <linux/list.h>
+#include <linux/file.h>
+
+#include <media/media-devnode.h>
+
+/**
+ * struct media_device_fh - File handle specific information on MC
+ *
+ * @fh: The media device file handle
+ * @mdev_list: This file handle in media device's list of file handles
+ */
+struct media_device_fh {
+	struct media_devnode_fh fh;
+	struct list_head mdev_list;
+};
+
+static inline struct media_device_fh *media_device_fh(struct file *filp)
+{
+	return container_of(filp->private_data, struct media_device_fh, fh);
+}
+
+#endif /* MEDIA_FH_H */

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 07/16] media: mc: Add MEDIA_IOC_DQEVENT
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (5 preceding siblings ...)
  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 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 08/16] media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION Mattijs Korpershoek
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

Events on a media device tell about completion of requests. Blocking and
non-blocking operation is supported.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/Kconfig        |   1 +
 drivers/media/mc/Makefile    |   2 +-
 drivers/media/mc/mc-device.c |  48 +++++++++
 drivers/media/mc/mc-event.c  | 244 +++++++++++++++++++++++++++++++++++++++++++
 include/media/media-device.h |   3 +
 include/media/media-event.h  | 181 ++++++++++++++++++++++++++++++++
 include/media/media-fh.h     |   5 +
 include/uapi/linux/media.h   |  24 +++++
 8 files changed, 507 insertions(+), 1 deletion(-)

diff --git a/drivers/media/Kconfig b/drivers/media/Kconfig
index 70e090757492..5ffcd5565bd1 100644
--- a/drivers/media/Kconfig
+++ b/drivers/media/Kconfig
@@ -12,6 +12,7 @@ source "drivers/media/cec/Kconfig"
 menuconfig MEDIA_SUPPORT
 	tristate "Multimedia support"
 	depends on HAS_IOMEM
+	select MEDIA_EVENT
 	help
 	  If you want to use media devices, including Webcams, Video grabber
 	  devices and/or TV devices, V4L2 codecs, etc, enable this option
diff --git a/drivers/media/mc/Makefile b/drivers/media/mc/Makefile
index d501327436a5..1e6540161e0c 100644
--- a/drivers/media/mc/Makefile
+++ b/drivers/media/mc/Makefile
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 mc-objs	:= mc-device.o mc-devnode.o mc-entity.o \
-	   mc-request.o
+	   mc-request.o mc-event.o
 
 ifneq ($(CONFIG_USB),)
 	mc-objs += mc-dev-allocator.o
diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index b64322efd99e..98d323c89b41 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -8,6 +8,7 @@
  *	     Sakari Ailus <sakari.ailus@iki.fi>
  */
 
+#include <linux/atomic.h>
 #include <linux/compat.h>
 #include <linux/export.h>
 #include <linux/idr.h>
@@ -22,6 +23,7 @@
 #include <media/media-device.h>
 #include <media/media-devnode.h>
 #include <media/media-entity.h>
+#include <media/media-event.h>
 #include <media/media-fh.h>
 #include <media/media-request.h>
 
@@ -57,6 +59,14 @@ static int media_device_open(struct file *filp)
 		return -ENOMEM;
 
 	filp->private_data = &fh->fh;
+	init_waitqueue_head(&fh->wait);
+
+	fh->ev_fh.lock = &mdev->fh_list_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);
 
 	spin_lock_irqsave(&mdev->fh_list_lock, flags);
 	list_add(&fh->mdev_list, &mdev->fh_list);
@@ -78,6 +88,9 @@ static int media_device_close(struct file *filp)
 		spin_unlock_irqrestore(&mdev->fh_list_lock, flags);
 	}
 
+	media_event_unsubscribe_all(fh);
+	mutex_destroy(&fh->ev_fh.subscribe_lock);
+
 	kfree(fh);
 
 	return 0;
@@ -414,6 +427,25 @@ static long media_device_request_alloc(struct media_device *mdev,
 	return media_request_alloc(mdev, alloc_fd);
 }
 
+static long media_device_event_dequeue(struct media_device *mdev,
+				       struct file *file, void *arg)
+{
+	return media_event_dequeue(media_device_fh(file), arg,
+				   file->f_flags & O_NONBLOCK);
+}
+
+static long media_device_event_subscribe(struct media_device *mdev,
+					 struct file *file, void *arg)
+{
+	return media_event_subscribe(media_device_fh(file), arg, 0, NULL);
+}
+
+static long media_device_event_unsubscribe(struct media_device *mdev,
+					   struct file *file, void *arg)
+{
+	return media_event_unsubscribe(media_device_fh(file), arg);
+}
+
 static long copy_arg_from_user(void *karg, void __user *uarg, unsigned int cmd)
 {
 	if ((_IOC_DIR(cmd) & _IOC_WRITE) &&
@@ -464,6 +496,9 @@ static const struct media_ioctl_info ioctl_info[] = {
 	MEDIA_IOC(SETUP_LINK, media_device_setup_link, MEDIA_IOC_FL_GRAPH_MUTEX),
 	MEDIA_IOC(G_TOPOLOGY, media_device_get_topology, MEDIA_IOC_FL_GRAPH_MUTEX),
 	MEDIA_IOC(REQUEST_ALLOC, media_device_request_alloc, 0),
+	MEDIA_IOC(DQEVENT, media_device_event_dequeue, 0),
+	MEDIA_IOC(SUBSCRIBE_EVENT, media_device_event_subscribe, 0),
+	MEDIA_IOC(UNSUBSCRIBE_EVENT, media_device_event_unsubscribe, 0),
 };
 
 static long media_device_ioctl(struct file *filp, unsigned int cmd,
@@ -574,9 +609,22 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd,
 }
 #endif /* CONFIG_COMPAT */
 
+static __poll_t media_device_poll(struct file *filp,
+				  struct poll_table_struct *wait)
+{
+	struct media_device_fh *fh = media_device_fh(filp);
+
+	poll_wait(filp, &fh->wait, wait);
+	if (media_event_pending(fh))
+		return EPOLLPRI;
+
+	return 0;
+}
+
 static const struct media_file_operations media_device_fops = {
 	.owner = THIS_MODULE,
 	.open = media_device_open,
+	.poll = media_device_poll,
 	.ioctl = media_device_ioctl,
 #ifdef CONFIG_COMPAT
 	.compat_ioctl = media_device_compat_ioctl,
diff --git a/drivers/media/mc/mc-event.c b/drivers/media/mc/mc-event.c
new file mode 100644
index 000000000000..2369e40e522f
--- /dev/null
+++ b/drivers/media/mc/mc-event.c
@@ -0,0 +1,244 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Media events
+ *
+ * Copyright (C) 2019, 2026 Intel Corporation
+ * Copyright (C) 2009--2010 Nokia Corporation.
+ *
+ * Based on V4L2 events in drivers/media/v4l2-core/v4l2-events.c .
+ */
+
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/export.h>
+
+#include <media/media-device.h>
+#include <media/media-event.h>
+#include <media/media-fh.h>
+
+static int media_evop_add(struct media_subscribed_evbase *mse, unsigned int elems)
+{
+	struct media_subscribed_event *sev = to_media_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->add)
+		return sev->ops->add(sev, elems);
+
+	return 0;
+}
+
+static void media_evop_del(struct media_subscribed_evbase *mse)
+{
+	struct media_subscribed_event *sev = to_media_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->del)
+		sev->ops->del(sev);
+}
+
+static bool media_evop_replace(struct media_subscribed_evbase *mse,
+			       struct media_evbase *old,
+			       const void *new)
+{
+	struct media_subscribed_event *sev = to_media_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->replace) {
+		sev->ops->replace(&to_media_kevent(old)->event, new);
+		return true;
+	}
+
+	return false;
+}
+
+static void media_evop_merge(struct media_subscribed_evbase *mse,
+			     const struct media_evbase *old,
+			     struct media_evbase *new)
+{
+	struct media_subscribed_event *sev = to_media_subscribed_event(mse);
+
+	if (sev->ops && sev->ops->merge)
+		sev->ops->merge(&to_media_kevent_const(old)->event,
+				&to_media_kevent(new)->event);
+}
+
+static void media_evop_queue(struct media_evbase *kev, const void *__ev,
+			     u64 ts, u32 sequence, bool copy_payload)
+{
+	struct media_event *old = &to_media_kevent(kev)->event;
+	const struct media_event *new = __ev;
+
+	old->type = new->type;
+	if (copy_payload)
+		old->u = new->u;
+	old->id = new->id;
+	old->timestamp = ts;
+	old->sequence = sequence;
+}
+
+static bool media_evop_is_subscribed(struct media_subscribed_evbase *mse,
+				     const void *__evspec)
+{
+	const struct media_event_spec *evspec = __evspec;
+	struct media_subscribed_event *sev =
+		container_of(mse, struct media_subscribed_event, mse);
+
+	return sev->evspec.type == evspec->type &&
+		sev->evspec.id == evspec->id;
+}
+
+static bool media_evop_is_queueable(struct media_subscribed_evbase *mse,
+				    const void *__ev)
+{
+	const struct media_event *ev = __ev;
+	struct media_subscribed_event *sev =
+		container_of(mse, struct media_subscribed_event, mse);
+
+	return sev->evspec.type == ev->type && sev->evspec.id == ev->id;
+}
+
+static void media_evop_release(struct media_subscribed_evbase *mse)
+{
+	kvfree(to_media_subscribed_event(mse));
+}
+
+static const struct media_subscribed_evbase_ops media_event_ops = {
+	.add            = media_evop_add,
+	.del            = media_evop_del,
+	.replace        = media_evop_replace,
+	.merge          = media_evop_merge,
+	.queue          = media_evop_queue,
+	.is_subscribed  = media_evop_is_subscribed,
+	.is_queueable   = media_evop_is_queueable,
+	.release        = media_evop_release,
+};
+
+static int __media_event_dequeue(struct media_evbase_fh *fh,
+				 struct media_event *event)
+{
+	struct media_evbase *mev;
+	struct media_kevent *kev;
+	unsigned long flags;
+
+	spin_lock_irqsave(fh->lock, flags);
+
+	mev = __media_evbase_dequeue(fh);
+	if (!mev) {
+		spin_unlock_irqrestore(fh->lock, flags);
+		return -ENOENT;
+	}
+
+	kev = container_of(mev, struct media_kevent, mev);
+
+	*event = kev->event;
+	event->pending = fh->navailable;
+
+	spin_unlock_irqrestore(fh->lock, flags);
+
+	return 0;
+}
+
+long media_event_dequeue(struct media_device_fh *fh, struct media_event *event,
+			 int nonblocking)
+{
+	int ret;
+
+	if (nonblocking)
+		return __media_event_dequeue(&fh->ev_fh, event);
+
+	do {
+		ret = wait_event_interruptible(fh->wait,
+					       fh->ev_fh.navailable != 0);
+		if (ret < 0)
+			break;
+
+		ret = __media_event_dequeue(&fh->ev_fh, event);
+	} while (ret == -ENOENT);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(media_event_dequeue);
+
+void media_event_queue(struct media_device *mdev, const struct media_event *ev)
+{
+	struct media_device_fh *fh;
+	unsigned long flags;
+	u64 ts;
+
+	if (mdev == NULL)
+		return;
+
+	ts = ktime_get_ns();
+
+	spin_lock_irqsave(&mdev->fh_list_lock, flags);
+
+	list_for_each_entry(fh, &mdev->fh_list, mdev_list)
+		__media_evbase_queue_fh(&fh->ev_fh, ev, ts);
+
+	spin_unlock_irqrestore(&mdev->fh_list_lock, flags);
+}
+EXPORT_SYMBOL_GPL(media_event_queue);
+
+int media_event_pending(struct media_device_fh *fh)
+{
+	return media_evbase_pending(&fh->ev_fh);
+}
+EXPORT_SYMBOL_GPL(media_event_pending);
+
+long media_event_subscribe(struct media_device_fh *fh,
+			   const struct media_event_subscription *sub,
+			   unsigned int elems,
+			   const struct media_subscribed_event_ops *ops)
+{
+	struct media_subscribed_evbase *mse;
+	struct media_subscribed_event *sev;
+
+	if (sub->type == MEDIA_EVENT_ALL)
+		return -EINVAL;
+
+	elems = max(elems, 1U);
+
+	sev = kvzalloc(sizeof(*sev) + array_size(sizeof(struct media_kevent),
+						 elems), GFP_KERNEL);
+	if (!sev)
+		return -ENOMEM;
+
+	sev->evspec.type = sub->type;
+	sev->evspec.id = sub->id;
+	sev->evspec.flags = sub->flags;
+	sev->ops = ops;
+
+	mse = &sev->mse;
+
+	mse->ev_size = sizeof(struct media_kevent);
+	mse->ops = &media_event_ops;
+	mse->fh = &fh->ev_fh;
+	mse->evspec = &sev->evspec;
+
+	return media_evbase_subscribe(&fh->ev_fh, mse, elems);
+}
+EXPORT_SYMBOL_GPL(media_event_subscribe);
+
+void media_event_unsubscribe_all(struct media_device_fh *fh)
+{
+	media_evbase_unsubscribe_all(&fh->ev_fh);
+}
+EXPORT_SYMBOL_GPL(media_event_unsubscribe_all);
+
+long media_event_unsubscribe(struct media_device_fh *fh,
+			    const struct media_event_subscription *sub)
+{
+	struct media_event_spec evspec = {
+		.type = sub->type,
+		.id = sub->id,
+		.flags = sub->flags,
+	};
+
+	if (sub->type == MEDIA_EVENT_ALL) {
+		media_event_unsubscribe_all(fh);
+		return 0;
+	}
+
+	media_evbase_unsubscribe(&fh->ev_fh, &evspec);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(media_event_unsubscribe);
diff --git a/include/media/media-device.h b/include/media/media-device.h
index 2e737fec1afc..3ef277309d59 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -19,10 +19,13 @@
 
 #include <media/media-devnode.h>
 #include <media/media-entity.h>
+#include <media/media-evbase.h>
 
 struct ida;
 struct media_device;
 
+#include <uapi/linux/media.h>
+
 /**
  * struct media_entity_notify - Media Entity Notify
  *
diff --git a/include/media/media-event.h b/include/media/media-event.h
new file mode 100644
index 000000000000..57265d117cf5
--- /dev/null
+++ b/include/media/media-event.h
@@ -0,0 +1,181 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * media-event.h
+ *
+ * Media controller events.
+ *
+ * Copyright (C) 2019, 2026 Intel Corporation
+ * Copyright (C) 2009--2010 Nokia Corporation.
+ *
+ * Based on V4L2 events.
+ */
+
+#ifndef MEDIA_EVENT_H
+#define MEDIA_EVENT_H
+
+#include <linux/types.h>
+#include <linux/media.h>
+
+#include <media/media-evbase.h>
+
+struct media_device;
+struct media_device_fh;
+struct media_subscribed_event;
+
+/**
+ * struct media_event_spec - Event subscription parameters
+ *
+ * @type:	Media event type, MEDIA_EVENT_*.
+ * @id:		Media event id.
+ * @flags:	Media event flags.
+ */
+struct media_event_spec {
+	u32 type;
+	u32 id;
+	u32 flags;
+};
+
+/**
+ * struct media_subscribed_event_ops - Subscribed event operations.
+ *
+ * @add:	Optional callback, called when a new listener is added
+ * @del:	Optional callback, called when a listener stops listening
+ * @replace:	Optional callback that can replace event 'old' with event 'new'.
+ * @merge:	Optional callback that can merge event 'old' into event 'new'.
+ */
+struct media_subscribed_event_ops {
+	int  (*add)(struct media_subscribed_event *sev, unsigned int elems);
+	void (*del)(struct media_subscribed_event *sev);
+	void (*replace)(struct media_event *old, const struct media_event *new);
+	void (*merge)(const struct media_event *old, struct media_event *new);
+};
+
+/**
+ * struct media_subscribed_event - Describes a single subscribed media event.
+ *
+ * @evspec:	Parameters of the subscribed event.
+ * @ops:	Operations for the subscribed event.
+ * @mse:	Common media event descriptor.
+ */
+struct media_subscribed_event {
+	struct media_event_spec evspec;
+	const struct media_subscribed_event_ops *ops;
+	struct media_subscribed_evbase mse;
+};
+
+/**
+ * struct media_kevent - Internal kernel event struct.
+ *
+ * @mev:	Generic media subsystem kernel event.
+ * @event:	The media event itself.
+ */
+struct media_kevent {
+	struct media_evbase mev;
+	struct media_event event;
+};
+
+/**
+ * to_media_kevent - Convert a generic media_evbase into a media_kevent
+ *
+ * @mev: The generic media event
+ */
+static inline struct media_kevent *to_media_kevent(struct media_evbase *mev)
+{
+	return container_of(mev, struct media_kevent, mev);
+}
+
+/**
+ * to_media_kevent_const - Convert a const generic media_evbase into a
+ *			   media_kevent
+ *
+ * @mev: The generic media event
+ *
+ * This is just like @to_media_kevent, but const.
+ */
+static inline const struct media_kevent *
+to_media_kevent_const(const struct media_evbase *mev)
+{
+	return container_of(mev, const struct media_kevent, mev);
+}
+
+/**
+ * to_media_subscribed_event - Convert a generic media_subscribed_evbase into
+ *			       a media_subscribed_event.
+ *
+ * @mse: The generic media subscribed event base
+ */
+static inline struct media_subscribed_event *
+to_media_subscribed_event(struct media_subscribed_evbase *mse)
+{
+	return container_of(mse, struct media_subscribed_event, mse);
+}
+
+/**
+ * media_event_dequeue - Dequeue events from a media device.
+ *
+ * @fh: pointer to struct media_device_fh
+ * @event: pointer to struct media_event
+ * @nonblocking: if zero, wait for an event to arrive
+ */
+long media_event_dequeue(struct media_device_fh *fh,
+			 struct media_event *event, int nonblocking);
+
+/**
+ * media_event_queue - Queue events to a media device.
+ *
+ * @mdev: pointer to &struct media_device
+ * @ev: pointer to &struct media_event
+ *
+ * The event will be queued for all &struct media_device_fh file handlers.
+ *
+ * .. note::
+ *    The driver's only responsibility is to fill in the type and the data
+ *    fields. The other fields will be filled in by the framework.
+ */
+void media_event_queue(struct media_device *mdev,
+		       const struct media_event *ev);
+
+/**
+ * media_event_pending - Check if an event is available
+ *
+ * @fh: pointer to &struct media_device_fh
+ *
+ * Returns the number of pending events.
+ */
+int media_event_pending(struct media_device_fh *fh);
+
+/**
+ * media_event_subscribe - Subscribes to a media event.
+ *
+ * @fh: pointer to &struct media_device_fh
+ * @sub: pointer to &struct media_event_subscription
+ * @elems: size of the events queue
+ * @ops: pointer to &media_subscribed_event_ops
+ *
+ * .. note::
+ *
+ *    if @elems is zero, the framework will fill in a default value,
+ *    which is currently 1 element.
+ */
+long media_event_subscribe(struct media_device_fh *fh,
+			   const struct media_event_subscription *sub,
+			   unsigned int elems,
+			   const struct media_subscribed_event_ops *ops);
+
+/**
+ * media_event_unsubscribe - Unsubscribe from a media event.
+ *
+ * @fh: pointer to &struct media_device_fh
+ * @sub: pointer to &struct media_event_subscription
+ */
+long media_event_unsubscribe(struct media_device_fh *fh,
+			     const struct media_event_subscription *sub);
+
+/**
+ * media_event_unsubscribe_all - Unsubscribe from all media events.
+ *
+ * @fh: pointer to &struct media_device_fh
+ */
+void media_event_unsubscribe_all(struct media_device_fh *fh);
+
+#endif /* MEDIA_EVENT_H */
diff --git a/include/media/media-fh.h b/include/media/media-fh.h
index 14ae4daaaddd..ca8bf6154096 100644
--- a/include/media/media-fh.h
+++ b/include/media/media-fh.h
@@ -12,16 +12,21 @@
 #include <linux/file.h>
 
 #include <media/media-devnode.h>
+#include <media/media-evbase.h>
 
 /**
  * struct media_device_fh - File handle specific information on MC
  *
  * @fh: The media device file handle
  * @mdev_list: This file handle in media device's list of file handles
+ * @ev_fh: File handle specific media event information
+ * @wait: File handle's wait queue
  */
 struct media_device_fh {
 	struct media_devnode_fh fh;
 	struct list_head mdev_list;
+	struct media_evbase_fh ev_fh;
+	struct wait_queue_head wait;
 };
 
 static inline struct media_device_fh *media_device_fh(struct file *filp)
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index 2da6ae4da003..24b6b3f75592 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -373,6 +373,27 @@ struct media_v2_topology {
 	__u64 ptr_links;
 } __attribute__ ((packed));
 
+#define MEDIA_EVENT_ALL		0
+
+struct media_event {
+	__u32 type;
+	union {
+		__u8 data[64];
+	} u;
+	__u32 reserved[8];
+	__u32 pending;
+	__u32 sequence;
+	__u64 timestamp;
+	__u32 id;
+} __attribute__((packed));
+
+struct media_event_subscription {
+	__u32 type;
+	__u32 id;
+	__u32 flags;
+	__u32 reserved[5];
+} __attribute__((packed));
+
 /* ioctls */
 
 #define MEDIA_IOC_DEVICE_INFO	_IOWR('|', 0x00, struct media_device_info)
@@ -381,6 +402,9 @@ struct media_v2_topology {
 #define MEDIA_IOC_SETUP_LINK	_IOWR('|', 0x03, struct media_link_desc)
 #define MEDIA_IOC_G_TOPOLOGY	_IOWR('|', 0x04, struct media_v2_topology)
 #define MEDIA_IOC_REQUEST_ALLOC	_IOR ('|', 0x05, int)
+#define MEDIA_IOC_SUBSCRIBE_EVENT _IOWR ('|', 0x06, struct media_event_subscription)
+#define MEDIA_IOC_UNSUBSCRIBE_EVENT _IOWR ('|', 0x07, struct media_event_subscription)
+#define MEDIA_IOC_DQEVENT	_IOWR('|', 0x08, struct media_event)
 
 /*
  * These ioctls are called on the request file descriptor as returned

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 08/16] media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (6 preceding siblings ...)
  2026-11-18 12:43 ` [PATCH RFC v2 07/16] media: mc: Add MEDIA_IOC_DQEVENT Mattijs Korpershoek
@ 2026-11-18 12:43 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 09/16] Documentation: media: Add Media controller event documentation Mattijs Korpershoek
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

Add a media event to tell a new media entity has been registered.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/mc/mc-device.c | 5 +++++
 include/uapi/linux/media.h   | 9 ++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 98d323c89b41..82f5676afbc0 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -746,6 +746,11 @@ int __must_check media_device_register_entity(struct media_device *mdev,
 	}
 	mutex_unlock(&mdev->graph_mutex);
 
+	media_event_queue(mdev, &(struct media_event){
+		.type = MEDIA_EVENT_ENTITY_REGISTRATION,
+		.u.entity_reg.entity_id = entity->graph_obj.id,
+	});
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(media_device_register_entity);
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index 24b6b3f75592..b5275acad87f 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -373,11 +373,18 @@ struct media_v2_topology {
 	__u64 ptr_links;
 } __attribute__ ((packed));
 
-#define MEDIA_EVENT_ALL		0
+#define MEDIA_EVENT_ALL			0
+#define MEDIA_EVENT_ENTITY_REGISTRATION	1
+
+struct media_event_entity_reg {
+	__u32 entity_id;
+	__u32 pending;
+} __attribute__ ((packed));
 
 struct media_event {
 	__u32 type;
 	union {
+		struct media_event_entity_reg entity_reg;
 		__u8 data[64];
 	} u;
 	__u32 reserved[8];

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 09/16] Documentation: media: Add Media controller event documentation
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (7 preceding siblings ...)
  2026-11-18 12:43 ` [PATCH RFC v2 08/16] media: mc: Add MEDIA_EVENT_ENTITY_REGISTRATION Mattijs Korpershoek
@ 2026-11-18 12:43 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 10/16] media: media.h: Add entity connection status Mattijs Korpershoek
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

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

Add documentation for the Media controller event IOCTLs as well as an
event, MEDIA_EVENT_ENTITY_REGISTRATION.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 .../userspace-api/media/mediactl/media-funcs.rst   |   2 +
 .../media/mediactl/media-ioc-dqevent.rst           | 125 +++++++++++++++++++++
 .../media/mediactl/media-ioc-subscribe-event.rst   | 112 ++++++++++++++++++
 3 files changed, 239 insertions(+)

diff --git a/Documentation/userspace-api/media/mediactl/media-funcs.rst b/Documentation/userspace-api/media/mediactl/media-funcs.rst
index e896296812c1..a9ce38b7144f 100644
--- a/Documentation/userspace-api/media/mediactl/media-funcs.rst
+++ b/Documentation/userspace-api/media/mediactl/media-funcs.rst
@@ -19,6 +19,8 @@ Function Reference
     media-ioc-enum-links
     media-ioc-setup-link
     media-ioc-request-alloc
+    media-ioc-subscribe-event
+    media-ioc-dqevent
     request-func-close
     request-func-ioctl
     request-func-poll
diff --git a/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst b/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst
new file mode 100644
index 000000000000..f7a2cd2675f5
--- /dev/null
+++ b/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst
@@ -0,0 +1,125 @@
+.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
+.. c:namespace:: MC
+
+.. _media_ioc_dqevent:
+
+***********************
+ioctl MEDIA_IOC_DQEVENT
+***********************
+
+Name
+====
+
+MEDIA_IOC_DQEVENT - Dequeue a Media controller event
+
+
+Synopsis
+========
+
+.. c:function:: int ioctl( int fd, MEDIA_IOC_DQEVENT, struct media_event *argp )
+    :name: MEDIA_IOC_DQEVENT
+
+
+Arguments
+=========
+
+``fd``
+    File descriptor returned by :ref:`open() <media-func-open>`.
+
+``argp``
+    Pointer to struct :c:type:`media_event`.
+
+
+Description
+===========
+
+The MEDIA_IOC_DQEVENT IOCTL may be used to dequeue Media controller events from
+the file handle. The :ref:`MEDIA_IOC_SUBSCRIBE_EVENT` IOCTL can be used to
+subscribe the events.
+
+.. tabularcolumns:: |p{4.6cm}|p{4.4cm}|p{8.7cm}|
+
+.. c:type:: media_event
+
+.. flat-table:: struct media_event
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2 2
+
+    * - __u32
+      - ``type``
+      -
+      - Type of the event, see :ref:`media-event-type`.
+    * - union
+      - ``u``
+      -
+      -
+    * -
+      - struct :c:type:`media_event_entity_reg`
+      -	``entity_reg``
+      - Event data for event ``MEDIA_EVENT_ENTITY_REGISTRATION``.
+    * -
+      - __u8
+      - ``data``\ [64]
+      - Event data. Defined by the event type. The union should be used to
+	define easily accessible type for events.
+    * - __u32
+      - ``reserved``\ [8]
+      -
+      - Reserved for future extensions. Drivers and applications must set
+	the array to zero.
+    * - __u32
+      - ``pending``
+      -
+      - The number of pending events excluding this one.
+    * - __u32
+      - ``sequence``
+      -
+      - Event sequence number. The sequence number is incremented for
+	every subscribed event that takes place. If sequence numbers are
+	not contiguous it means that events have been lost.
+    * - __u64
+      - ``timestamp``
+      -
+      -	Event timestamp. The timestamp has been taken from the
+	``CLOCK_MONOTONIC`` clock.
+    * - __u32
+      - ``id``
+      -
+      - The ID associated with the event source. If the event does not
+	have an associated ID (this depends on the event type), then this
+	is 0.
+
+.. tabularcolumns:: |p{6.6cm}|p{2.2cm}|p{8.7cm}|
+
+.. cssclass:: longtable
+
+.. _media-event-type:
+
+.. flat-table:: Media Event Types
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       3 1 4
+
+    * - ``MEDIA_EVENT_ALL``
+      - 0
+      - All events. MEDIA_EVENT_ALL is valid only for
+	MEDIA_IOC_UNSUBSCRIBE_EVENT for unsubscribing all events at once.
+    * - ``MEDIA_EVENT_ENTITY_REGISTRATION``
+      - 1
+      - Triggered when a media entity is registered to a Media controller
+        device. The pending field designates the number of entities that are
+	known to be still missing. This number may be less than the total
+	number of all missing entities. However if this number is zero, then
+	all entities have been registered to the media device.
+
+Return Value
+============
+
+On success 0 is returned, on error -1 and the ``errno`` variable is set
+appropriately. The generic error codes are described at the
+:ref:`Generic Error Codes <gen-errors>` chapter.
+
+ENOENT
+    The event queue is empty and the file descriptor is in non-blocking
+    mode.
diff --git a/Documentation/userspace-api/media/mediactl/media-ioc-subscribe-event.rst b/Documentation/userspace-api/media/mediactl/media-ioc-subscribe-event.rst
new file mode 100644
index 000000000000..61e537b6ba6d
--- /dev/null
+++ b/Documentation/userspace-api/media/mediactl/media-ioc-subscribe-event.rst
@@ -0,0 +1,112 @@
+.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
+.. c:namespace:: MC
+
+.. _media_ioc_subscribe_event:
+
+************************************************************
+ioctl MEDIA_IOC_SUBSCRIBE_EVENT, MEDIA_IOC_UNSUBSCRIBE_EVENT
+************************************************************
+
+Name
+====
+
+MEDIA_IOC_SUBSCRIBE_EVENT - MEDIA_IOC_UNSUBSCRIBE_EVENT - Subscribe or unsubscribe a Media controller event
+
+
+Synopsis
+========
+
+.. c:function:: int ioctl( int fd, MEDIA_IOC_SUBSCRIBE_EVENT, struct media_event_subscription *argp )
+    :name: MEDIA_IOC_SUBSCRIBE_EVENT
+
+.. c:function:: int ioctl( int fd, MEDIA_IOC_UNSUBSCRIBE_EVENT, struct media_event_subscription *argp )
+    :name: MEDIA_IOC_UNSUBSCRIBE_EVENT
+
+
+Arguments
+=========
+
+``fd``
+    File descriptor returned by :ref:`open() <media-func-open>`.
+
+``argp``
+    Pointer to struct :c:type:`media_event_subscription`.
+
+
+Description
+===========
+
+The MEDIA_IOC_SUBSCRIBE_EVENT IOCTL may be used to subscribe Media controller
+events to the file handle. The events are identified by their type and id, and
+the user may set the flags field to specify properties of the subscription. The
+subscriptions are specific to a file handle.
+
+The caller is responsible for setting the value of all fields in the argument
+struct by either initialising the fields of the entire struct to a known value
+or by setting the memory of the entire struct to zero and then filling the
+desired fields.
+
+The events may be dequeued using the :ref:`MEDIA_IOC_DQEVENT` IOCTL.
+
+The MEDIA_IOC_UNSUBSCRIBE_EVENT IOCTL is used to unsubscribe a subscribed
+event. A special value of ''MEDIA_EVENT_ALL'' may be used to unsubscribe all
+events on a file handle.
+
+.. tabularcolumns:: |p{4.6cm}|p{4.4cm}|p{8.7cm}|
+
+.. c:type:: media_event_subscription
+
+.. flat-table:: struct media_event_subscription
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - __u32
+      - ``type``
+      - Type of the event, see :ref:`media-event-type`.
+
+	.. note::
+
+	   ``MEDIA_EVENT_ALL`` can be used with
+	   :ref:`MEDIA_IOC_UNSUBSCRIBE_EVENT <MEDIA_SUBSCRIBE_EVENT>` for
+	   unsubscribing all events at once.
+    * - __u32
+      - ``id``
+      - ID of the event source. If there is no ID associated with the
+	event source, then set this to 0. Whether or not an event needs an
+	ID depends on the event type.
+    * - __u32
+      - ``flags``
+      - Event flags, see :ref:`media-event-flags`.
+    * - __u32
+      - ``reserved``\ [5]
+      - Reserved for future extensions. Drivers and applications must set
+	the array to zero.
+
+.. tabularcolumns:: |p{6.8cm}|p{2.2cm}|p{8.5cm}|
+
+.. _media-event-flags:
+
+.. flat-table:: Media event Flags
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       3 1 4
+
+    * - ``MEDIA_EVENT_SUB_FL_SEND_INITIAL``
+      - 0x0001
+      - When this event is subscribed an initial event will be sent
+	containing the current status. This only makes sense for events
+	that are triggered by a status change such as ``V4L2_EVENT_CTRL``.
+	Other events will ignore this flag.
+
+
+Return Value
+============
+
+On success 0 is returned, on error -1 and the ``errno`` variable is set
+appropriately. The generic error codes are described at the
+:ref:`Generic Error Codes <gen-errors>` chapter.
+
+EINVAL
+    The struct :c:type:`media_event_subscription` specifies an event that may not
+    generated by the given Media device.

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 10/16] media: media.h: Add entity connection status
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (8 preceding siblings ...)
  2026-11-18 12:43 ` [PATCH RFC v2 09/16] Documentation: media: Add Media controller event documentation Mattijs Korpershoek
@ 2026-11-18 12:43 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 11/16] media: mc: Add .detect() to media_entity_operations Mattijs Korpershoek
                   ` (5 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

Some elements in the media graph (such as camera sensors) can be
physically disconnected/reconnected. Drivers may still probe in that
case to have a complete media graph.

Add a new entity field, named status, which reflects the connection
status of that particular entity.

This field is retrieved by userspace via the MEDIA_IOC_G_TOPOLOGY and
can be updated from drivers via media_entity_set_status().

When a status is not initialized, it's set to zero (STATUS_CONNECTED).
This matches older media graphs were all entities are always connected.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/mc/mc-device.c |  1 +
 drivers/media/mc/mc-entity.c | 29 +++++++++++++++++++++++++++++
 include/media/media-entity.h | 20 ++++++++++++++++++++
 include/uapi/linux/media.h   |  6 +++++-
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 82f5676afbc0..71cdf079c8a8 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -307,6 +307,7 @@ static long media_device_get_topology(struct media_device *mdev,
 		kentity.id = entity->graph_obj.id;
 		kentity.function = entity->function;
 		kentity.flags = entity->flags;
+		kentity.status = entity->status;
 		strscpy(kentity.name, entity->name,
 			sizeof(kentity.name));
 
diff --git a/drivers/media/mc/mc-entity.c b/drivers/media/mc/mc-entity.c
index 7938f785e2e3..c152d3a61d51 100644
--- a/drivers/media/mc/mc-entity.c
+++ b/drivers/media/mc/mc-entity.c
@@ -1530,6 +1530,35 @@ struct media_pipeline *media_entity_pipeline(struct media_entity *entity)
 }
 EXPORT_SYMBOL_GPL(media_entity_pipeline);
 
+void __media_entity_set_status(struct media_entity *entity, u32 status)
+{
+	struct media_device *mdev = entity->graph_obj.mdev;
+
+	/* Do nothing if the entity is not registered. */
+	if (mdev == NULL)
+		return;
+
+	lockdep_assert_held(&mdev->graph_mutex);
+
+	if (entity->status != status)
+		entity->status = status;
+}
+EXPORT_SYMBOL_GPL(__media_entity_set_status);
+
+void media_entity_set_status(struct media_entity *entity, u32 status)
+{
+	struct media_device *mdev = entity->graph_obj.mdev;
+
+	/* Do nothing if the entity is not registered. */
+	if (mdev == NULL)
+		return;
+
+	mutex_lock(&mdev->graph_mutex);
+	__media_entity_set_status(entity, status);
+	mutex_unlock(&mdev->graph_mutex);
+}
+EXPORT_SYMBOL_GPL(media_entity_set_status);
+
 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
 {
 	return pad->pipe;
diff --git a/include/media/media-entity.h b/include/media/media-entity.h
index fa393e840669..1ec62793fc76 100644
--- a/include/media/media-entity.h
+++ b/include/media/media-entity.h
@@ -325,6 +325,10 @@ enum media_entity_type {
  * @flags:	Entity flags, as defined in
  *		:ref:`include/uapi/linux/media.h <media_header>`
  *		(seek for ``MEDIA_ENT_FL_*``)
+ * @status:	Entity connection status, as defined in
+ *		:ref:`include/uapi/linux/media.h <media_header>`
+ *		(seek for ``MEDIA_ENT_STATUS_*``).
+ *		Access with media_entity_set_status()
  * @num_pads:	Number of sink and source pads.
  * @num_links:	Total number of links, forward and back, enabled and disabled.
  * @num_backlinks: Number of backlinks
@@ -352,6 +356,7 @@ struct media_entity {
 	enum media_entity_type obj_type;
 	u32 function;
 	unsigned long flags;
+	u32 status;
 
 	u16 num_pads;
 	u16 num_links;
@@ -1022,6 +1027,21 @@ static inline bool media_entity_is_streaming(const struct media_entity *entity)
 	return false;
 }
 
+void __media_entity_set_status(struct media_entity *entity, u32 status);
+
+/**
+ * media_entity_set_status - Set the connection status of an entity
+ * @entity: The entity
+ * @status: The new status (%MEDIA_ENT_STATUS_CONNECTED or
+ *	                    %MEDIA_ENT_STATUS_DISCONNECTED)
+ *
+ * Update the entity's connection status.
+ *
+ * The entity must be registered with a media device. To set the initial
+ * status before registration, assign entity->status directly.
+ */
+void media_entity_set_status(struct media_entity *entity, u32 status);
+
 /**
  * media_entity_pipeline - Get the media pipeline an entity is part of
  * @entity: The entity
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index b5275acad87f..ab1dea809879 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -302,12 +302,16 @@ struct media_links_enum {
 #define MEDIA_V2_ENTITY_HAS_FLAGS(media_version) \
 	((media_version) >= ((4U << 16) | (19U << 8) | 0U))
 
+#define MEDIA_ENT_STATUS_CONNECTED		0
+#define MEDIA_ENT_STATUS_DISCONNECTED		1
+
 struct media_v2_entity {
 	__u32 id;
 	char name[64];
 	__u32 function;		/* Main function of the entity */
 	__u32 flags;
-	__u32 reserved[5];
+	__u32 status;
+	__u32 reserved[4];
 } __attribute__ ((packed));
 
 /* Should match the specific fields at media_intf_devnode */

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 11/16] media: mc: Add .detect() to media_entity_operations
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (9 preceding siblings ...)
  2026-11-18 12:43 ` [PATCH RFC v2 10/16] media: media.h: Add entity connection status Mattijs Korpershoek
@ 2026-11-18 12:43 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 12/16] media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl Mattijs Korpershoek
                   ` (4 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

When mc.fault_tolerance = true, media drivers can probe without the
underlying hardware being physically connected.
In that case, The media controller core needs to be able to retrieve
the driver state in order to update the topology.

Add a new, optional, media_entity_operation named .detect().

To support fault tolerance, drivers must be modified and do the
following when media_fault_tolerance_enabled() = true:

* Probe must succeed even when the underlying hardware is not present
* Probe must set initial entity connections status if disconnected
* The .detect() operation must be implemented
* .enable_streams() must query hardware for presence and must fail when
  it's not detected. Can be done using the media_entity_detect_or_fail()
  helper.

Note: this is based on the .detect() helpers from DRM connectors.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/mc/mc-device.c | 11 +++++++++++
 drivers/media/mc/mc-entity.c | 22 ++++++++++++++++++++++
 include/media/media-device.h | 10 ++++++++++
 include/media/media-entity.h | 24 ++++++++++++++++++++++++
 4 files changed, 67 insertions(+)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 71cdf079c8a8..9151a47887a7 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -14,6 +14,7 @@
 #include <linux/idr.h>
 #include <linux/ioctl.h>
 #include <linux/media.h>
+#include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/pci.h>
@@ -27,6 +28,16 @@
 #include <media/media-fh.h>
 #include <media/media-request.h>
 
+static bool fault_tolerance;
+module_param(fault_tolerance, bool, 0444);
+MODULE_PARM_DESC(fault_tolerance, "Enable fault-tolerant support");
+
+bool media_fault_tolerance_enabled(void)
+{
+	return fault_tolerance;
+}
+EXPORT_SYMBOL_GPL(media_fault_tolerance_enabled);
+
 #ifdef CONFIG_MEDIA_CONTROLLER
 
 /*
diff --git a/drivers/media/mc/mc-entity.c b/drivers/media/mc/mc-entity.c
index c152d3a61d51..2b5fc5d1b0a4 100644
--- a/drivers/media/mc/mc-entity.c
+++ b/drivers/media/mc/mc-entity.c
@@ -1559,6 +1559,28 @@ void media_entity_set_status(struct media_entity *entity, u32 status)
 }
 EXPORT_SYMBOL_GPL(media_entity_set_status);
 
+int media_entity_detect_or_fail(struct media_entity *entity)
+{
+	int ret;
+
+	if (!media_fault_tolerance_enabled())
+		return 0;
+
+	ret = media_entity_call(entity, detect);
+	if (ret == -ENOIOCTLCMD)
+		return 0;
+	if (ret < 0)
+		return ret;
+
+	media_entity_set_status(entity, ret);
+
+	if (ret == MEDIA_ENT_STATUS_DISCONNECTED)
+		return -ENOLINK;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(media_entity_detect_or_fail);
+
 struct media_pipeline *media_pad_pipeline(struct media_pad *pad)
 {
 	return pad->pipe;
diff --git a/include/media/media-device.h b/include/media/media-device.h
index 3ef277309d59..9a0f38914354 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -204,6 +204,16 @@ struct media_device {
 /* We don't need to include usb.h here */
 struct usb_device;
 
+/**
+ * media_fault_tolerance_enabled - Check if media fault tolerance is enabled
+ *
+ * Returns true when the mc.fault_tolerance module parameter is set.
+ * Drivers should call this to decide whether to enable fault-tolerant
+ * behavior (e.g. probing without the underlying hardware being
+ * physically connected).
+ */
+bool media_fault_tolerance_enabled(void);
+
 #ifdef CONFIG_MEDIA_CONTROLLER
 
 /* Supported link_notify @notification values. */
diff --git a/include/media/media-entity.h b/include/media/media-entity.h
index 1ec62793fc76..4d66f9823c9d 100644
--- a/include/media/media-entity.h
+++ b/include/media/media-entity.h
@@ -269,6 +269,12 @@ struct media_pad {
  *			media_entity_has_pad_interdep().
  *			Optional: If the operation isn't implemented all pads
  *			will be considered as interdependent.
+ * @detect:		Query whether the entity's hardware is physically
+ *			connected and reachable. Must not modify entity state.
+ *			Returns a %MEDIA_ENT_STATUS_* value on success, or
+ *			a negative error code on failure.
+ *			Optional: If the operation isn't implemented the
+ *			entity will be considered as connected
  *
  * .. note::
  *
@@ -284,6 +290,7 @@ struct media_entity_operations {
 	int (*link_validate)(struct media_link *link);
 	bool (*has_pad_interdep)(struct media_entity *entity, unsigned int pad0,
 				 unsigned int pad1);
+	int (*detect)(struct media_entity *entity);
 };
 
 /**
@@ -1042,6 +1049,23 @@ void __media_entity_set_status(struct media_entity *entity, u32 status);
  */
 void media_entity_set_status(struct media_entity *entity, u32 status);
 
+/**
+ * media_entity_detect_or_fail - Detect entity hardware and fail if disconnected
+ * @entity: The entity
+ *
+ * Call the entity's &media_entity_operations.detect() operation to probe
+ * the hardware. If the entity is disconnected, the entity status is updated
+ * and a %MEDIA_EVENT_ENTITY_STATUS_CHANGE event is queued.
+ *
+ * Intended to be called by drivers at the beginning of their
+ * &v4l2_subdev_pad_ops.enable_streams() callback
+ *
+ * Return: 0 if the entity is connected or does not implement .detect(),
+ * -ENOLINK if the entity is disconnected, or a negative error code on
+ * failure.
+ */
+int media_entity_detect_or_fail(struct media_entity *entity);
+
 /**
  * media_entity_pipeline - Get the media pipeline an entity is part of
  * @entity: The entity

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 12/16] media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (10 preceding siblings ...)
  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 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 13/16] media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event Mattijs Korpershoek
                   ` (3 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

Userspace can get an entity's connection status using 'media-ctl -p'.
This calls MEDIA_IOC_G_TOPOLOGY which will also provide the entity
status.
However, that status is from the cache. When fault tolerance is enabled,
the status can be only be updated in 2 ways:
1. During driver probe
2. When a stream is started

Add MEDIA_IOC_G_ENTITY_STATUS which allows userspace to get the hardware
state at any given time.
This can be useful for diagnostics, for example.

Calling MEDIA_IOC_G_ENTITY_STATUS will also update the topology's cached
value.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 .../userspace-api/media/mediactl/media-funcs.rst   |  1 +
 .../media/mediactl/media-ioc-g-entity-status.rst   | 92 ++++++++++++++++++++++
 drivers/media/mc/mc-device.c                       | 23 ++++++
 include/uapi/linux/media.h                         |  7 ++
 4 files changed, 123 insertions(+)

diff --git a/Documentation/userspace-api/media/mediactl/media-funcs.rst b/Documentation/userspace-api/media/mediactl/media-funcs.rst
index a9ce38b7144f..54150179227c 100644
--- a/Documentation/userspace-api/media/mediactl/media-funcs.rst
+++ b/Documentation/userspace-api/media/mediactl/media-funcs.rst
@@ -21,6 +21,7 @@ Function Reference
     media-ioc-request-alloc
     media-ioc-subscribe-event
     media-ioc-dqevent
+    media-ioc-g-entity-status
     request-func-close
     request-func-ioctl
     request-func-poll
diff --git a/Documentation/userspace-api/media/mediactl/media-ioc-g-entity-status.rst b/Documentation/userspace-api/media/mediactl/media-ioc-g-entity-status.rst
new file mode 100644
index 000000000000..ee108428c0bb
--- /dev/null
+++ b/Documentation/userspace-api/media/mediactl/media-ioc-g-entity-status.rst
@@ -0,0 +1,92 @@
+.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
+.. c:namespace:: MC
+
+.. _media_ioc_g_entity_status:
+
+*********************************
+ioctl MEDIA_IOC_G_ENTITY_STATUS
+*********************************
+
+Name
+====
+
+MEDIA_IOC_G_ENTITY_STATUS - Get the connection status of an entity
+
+Synopsis
+========
+
+.. c:macro:: MEDIA_IOC_G_ENTITY_STATUS
+
+``int ioctl(int fd, MEDIA_IOC_G_ENTITY_STATUS, struct media_entity_status *argp)``
+
+Arguments
+=========
+
+``fd``
+    File descriptor returned by :c:func:`open()`.
+
+``argp``
+    Pointer to struct :c:type:`media_entity_status`.
+
+Description
+===========
+
+To query the connection status of a given entity, applications set the
+``id`` field of a struct :c:type:`media_entity_status` and call the ioctl.
+The driver fills the rest of the structure or returns an
+EINVAL error code when the ``id`` is invalid.
+
+If the entity does not implement detection, the driver sets
+the ``status`` field to ``MEDIA_ENT_STATUS_CONNECTED``.
+
+If the entity supports detection, the driver reads the hardware state at call time
+and sets the ``status`` field accordingly.
+
+.. c:type:: media_entity_status
+
+.. tabularcolumns:: |p{1.5cm}|p{3.0cm}|p{12.0cm}|
+
+.. flat-table:: struct media_entity_status
+    :header-rows:  0
+    :stub-columns: 0
+    :widths: 1 2 8
+
+    *  -  __u32
+       -  ``id``
+       -  Entity ID, set by the application.
+
+    *  -  __u32
+       -  ``status``
+       -  Entity connection status, see :ref:`media-entity-status`.
+
+    *  -  __u32
+       -  ``reserved``\ [6]
+       -  Reserved for future extensions. Drivers and applications must set
+          the array to zero.
+
+.. tabularcolumns:: |p{5.5cm}|p{1.0cm}|p{10.0cm}|
+
+.. _media-entity-status:
+
+.. flat-table:: Entity Status
+    :header-rows:  0
+    :stub-columns: 0
+    :widths: 3 1 4
+
+    *  -  ``MEDIA_ENT_STATUS_CONNECTED``
+       -  0
+       -  The entity hardware is connected and reachable.
+
+    *  -  ``MEDIA_ENT_STATUS_DISCONNECTED``
+       -  1
+       -  The entity hardware is not connected or not reachable.
+
+Return Value
+============
+
+On success 0 is returned, on error -1 and the ``errno`` variable is set
+appropriately. The generic error codes are described at the
+:ref:`Generic Error Codes <gen-errors>` chapter.
+
+EINVAL
+    The ``id`` references a non-existing entity.
diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 9151a47887a7..6f5b2daa1177 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -501,6 +501,28 @@ struct media_ioctl_info {
 	long (*arg_to_user)(void __user *uarg, void *karg, unsigned int cmd);
 };
 
+static long media_device_get_entity_status(struct media_device *mdev,
+					   struct file *file, void *arg)
+{
+	struct media_entity_status *estatus = arg;
+	struct media_entity *entity;
+	int ret;
+
+	entity = find_entity(mdev, estatus->id);
+	if (!entity)
+		return -EINVAL;
+
+	ret = media_entity_call(entity, detect);
+	if (ret >= 0)
+		__media_entity_set_status(entity, ret);
+
+	memset(estatus->reserved, 0, sizeof(estatus->reserved));
+	estatus->id = media_entity_id(entity);
+	estatus->status = entity->status;
+
+	return 0;
+}
+
 static const struct media_ioctl_info ioctl_info[] = {
 	MEDIA_IOC(DEVICE_INFO, media_device_get_info, MEDIA_IOC_FL_GRAPH_MUTEX),
 	MEDIA_IOC(ENUM_ENTITIES, media_device_enum_entities, MEDIA_IOC_FL_GRAPH_MUTEX),
@@ -511,6 +533,7 @@ static const struct media_ioctl_info ioctl_info[] = {
 	MEDIA_IOC(DQEVENT, media_device_event_dequeue, 0),
 	MEDIA_IOC(SUBSCRIBE_EVENT, media_device_event_subscribe, 0),
 	MEDIA_IOC(UNSUBSCRIBE_EVENT, media_device_event_unsubscribe, 0),
+	MEDIA_IOC(G_ENTITY_STATUS, media_device_get_entity_status, MEDIA_IOC_FL_GRAPH_MUTEX),
 };
 
 static long media_device_ioctl(struct file *filp, unsigned int cmd,
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index ab1dea809879..9f9ced3fb0e0 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -377,6 +377,12 @@ struct media_v2_topology {
 	__u64 ptr_links;
 } __attribute__ ((packed));
 
+struct media_entity_status {
+	__u32 id;
+	__u32 status;
+	__u32 reserved[6];
+} __attribute__ ((packed));
+
 #define MEDIA_EVENT_ALL			0
 #define MEDIA_EVENT_ENTITY_REGISTRATION	1
 
@@ -416,6 +422,7 @@ struct media_event_subscription {
 #define MEDIA_IOC_SUBSCRIBE_EVENT _IOWR ('|', 0x06, struct media_event_subscription)
 #define MEDIA_IOC_UNSUBSCRIBE_EVENT _IOWR ('|', 0x07, struct media_event_subscription)
 #define MEDIA_IOC_DQEVENT	_IOWR('|', 0x08, struct media_event)
+#define MEDIA_IOC_G_ENTITY_STATUS _IOWR('|', 0x09, struct media_entity_status)
 
 /*
  * These ioctls are called on the request file descriptor as returned

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 13/16] media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (11 preceding siblings ...)
  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 ` Mattijs Korpershoek
  2026-11-18 12:43 ` [PATCH RFC v2 14/16] media: mc: Add detect polling helpers for drivers Mattijs Korpershoek
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

Add a media event that notifies userspace when an entity's connection
state changes. This allows userspace to react when a sensor gets
disconnected. For example, by aborting an ongoing stream.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 .../userspace-api/media/mediactl/media-ioc-dqevent.rst         |  5 +++++
 drivers/media/mc/mc-entity.c                                   | 10 +++++++++-
 include/media/media-entity.h                                   |  3 ++-
 include/uapi/linux/media.h                                     |  7 +++++++
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst b/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst
index f7a2cd2675f5..2ae72523afc7 100644
--- a/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst
+++ b/Documentation/userspace-api/media/mediactl/media-ioc-dqevent.rst
@@ -112,6 +112,11 @@ subscribe the events.
 	known to be still missing. This number may be less than the total
 	number of all missing entities. However if this number is zero, then
 	all entities have been registered to the media device.
+    * - ``MEDIA_EVENT_ENTITY_STATUS_CHANGE``
+      - 2
+      - Triggered when a media entity connection status changes. The entity
+        id is in ``entity_id`` and the status is encoded in ``status``.
+        See :ref:`MEDIA_IOC_G_ENTITY_STATUS` for details about entity status.
 
 Return Value
 ============
diff --git a/drivers/media/mc/mc-entity.c b/drivers/media/mc/mc-entity.c
index 2b5fc5d1b0a4..4c6a1c40d698 100644
--- a/drivers/media/mc/mc-entity.c
+++ b/drivers/media/mc/mc-entity.c
@@ -14,6 +14,7 @@
 #include <linux/slab.h>
 #include <media/media-entity.h>
 #include <media/media-device.h>
+#include <media/media-event.h>
 
 static inline const char *intf_type(struct media_interface *intf)
 {
@@ -1540,8 +1541,15 @@ void __media_entity_set_status(struct media_entity *entity, u32 status)
 
 	lockdep_assert_held(&mdev->graph_mutex);
 
-	if (entity->status != status)
+	if (entity->status != status) {
 		entity->status = status;
+
+		media_event_queue(mdev, &(struct media_event){
+			.type = MEDIA_EVENT_ENTITY_STATUS_CHANGE,
+			.u.entity_status.entity_id = entity->graph_obj.id,
+			.u.entity_status.status = status,
+		});
+	}
 }
 EXPORT_SYMBOL_GPL(__media_entity_set_status);
 
diff --git a/include/media/media-entity.h b/include/media/media-entity.h
index 4d66f9823c9d..b0e1fc627d17 100644
--- a/include/media/media-entity.h
+++ b/include/media/media-entity.h
@@ -1042,7 +1042,8 @@ void __media_entity_set_status(struct media_entity *entity, u32 status);
  * @status: The new status (%MEDIA_ENT_STATUS_CONNECTED or
  *	                    %MEDIA_ENT_STATUS_DISCONNECTED)
  *
- * Update the entity's connection status.
+ * Update the entity's connection status. If the status has changed, a
+ * %MEDIA_EVENT_ENTITY_STATUS_CHANGE event is queued to the media device.
  *
  * The entity must be registered with a media device. To set the initial
  * status before registration, assign entity->status directly.
diff --git a/include/uapi/linux/media.h b/include/uapi/linux/media.h
index 9f9ced3fb0e0..4ccffe7434c9 100644
--- a/include/uapi/linux/media.h
+++ b/include/uapi/linux/media.h
@@ -385,16 +385,23 @@ struct media_entity_status {
 
 #define MEDIA_EVENT_ALL			0
 #define MEDIA_EVENT_ENTITY_REGISTRATION	1
+#define MEDIA_EVENT_ENTITY_STATUS_CHANGE	2
 
 struct media_event_entity_reg {
 	__u32 entity_id;
 	__u32 pending;
 } __attribute__ ((packed));
 
+struct media_event_entity_status {
+	__u32 entity_id;
+	__u32 status;
+} __attribute__ ((packed));
+
 struct media_event {
 	__u32 type;
 	union {
 		struct media_event_entity_reg entity_reg;
+		struct media_event_entity_status entity_status;
 		__u8 data[64];
 	} u;
 	__u32 reserved[8];

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 14/16] media: mc: Add detect polling helpers for drivers
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (12 preceding siblings ...)
  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 ` 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
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

If a stream is ongoing and an entity gets disconnected from the media
graph, userspace will hang on DQBUF without any additional information.

When fault tolerance is enabled, the media controller can notify
userspace of such happening by periodically polling all entities that
support the .detect() operation.

If an entity's status change, userspace gets notified via the
MEDIA_EVENT_ENTITY_STATUS_CHANGE event.

Add media_detect_poll_enable/disable so that drivers can opt-in into
polling for .detect().

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/mc/mc-device.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
 include/media/media-device.h |  4 +++
 include/media/media-entity.h | 32 ++++++++++++++++++
 3 files changed, 116 insertions(+)

diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c
index 6f5b2daa1177..76b099aa23cd 100644
--- a/drivers/media/mc/mc-device.c
+++ b/drivers/media/mc/mc-device.c
@@ -20,6 +20,7 @@
 #include <linux/pci.h>
 #include <linux/usb.h>
 #include <linux/version.h>
+#include <linux/workqueue.h>
 
 #include <media/media-device.h>
 #include <media/media-devnode.h>
@@ -700,6 +701,9 @@ static void __media_device_unregister_entity(struct media_entity *entity)
 
 	ida_free(&mdev->entity_internal_idx, entity->internal_idx);
 
+	if (!list_empty(&entity->detect_list))
+		list_del_init(&entity->detect_list);
+
 	/* Remove all interface links pointing to this entity */
 	list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) {
 		list_for_each_entry_safe(link, tmp, &intf->links, list) {
@@ -738,6 +742,7 @@ int __must_check media_device_register_entity(struct media_device *mdev,
 	WARN_ON(entity->graph_obj.mdev != NULL);
 	entity->graph_obj.mdev = mdev;
 	INIT_LIST_HEAD(&entity->links);
+	INIT_LIST_HEAD(&entity->detect_list);
 	entity->num_links = 0;
 	entity->num_backlinks = 0;
 
@@ -820,6 +825,76 @@ static int media_device_requests(struct seq_file *file, void *priv)
 }
 #endif
 
+#define MEDIA_DETECT_POLL_INTERVAL_MS	10000
+
+static void media_detect_work_fn(struct work_struct *work)
+{
+	struct media_device *mdev = container_of(to_delayed_work(work),
+						 struct media_device,
+						 detect_work);
+	struct media_entity *entity;
+	int ret;
+
+	if (!mutex_trylock(&mdev->graph_mutex)) {
+		schedule_delayed_work(&mdev->detect_work,
+				      msecs_to_jiffies(MEDIA_DETECT_POLL_INTERVAL_MS));
+		return;
+	}
+
+	list_for_each_entry(entity, &mdev->detect_entities, detect_list) {
+		ret = media_entity_call(entity, detect);
+		if (ret >= 0)
+			__media_entity_set_status(entity, ret);
+	}
+
+	if (!list_empty(&mdev->detect_entities))
+		schedule_delayed_work(&mdev->detect_work,
+				      msecs_to_jiffies(MEDIA_DETECT_POLL_INTERVAL_MS));
+
+	mutex_unlock(&mdev->graph_mutex);
+}
+
+void media_detect_poll_enable(struct media_entity *entity)
+{
+	struct media_device *mdev = entity->graph_obj.mdev;
+
+	if (!media_fault_tolerance_enabled())
+		return;
+
+	mutex_lock(&mdev->graph_mutex);
+
+	if (!list_empty(&entity->detect_list))
+		goto out;
+
+	list_add_tail(&entity->detect_list, &mdev->detect_entities);
+	schedule_delayed_work(&mdev->detect_work,
+			      msecs_to_jiffies(MEDIA_DETECT_POLL_INTERVAL_MS));
+out:
+	mutex_unlock(&mdev->graph_mutex);
+}
+EXPORT_SYMBOL_GPL(media_detect_poll_enable);
+
+void media_detect_poll_disable(struct media_entity *entity)
+{
+	struct media_device *mdev = entity->graph_obj.mdev;
+
+	if (!media_fault_tolerance_enabled())
+		return;
+
+	mutex_lock(&mdev->graph_mutex);
+
+	if (list_empty(&entity->detect_list))
+		goto out;
+
+	list_del_init(&entity->detect_list);
+
+	if (list_empty(&mdev->detect_entities))
+		cancel_delayed_work(&mdev->detect_work);
+out:
+	mutex_unlock(&mdev->graph_mutex);
+}
+EXPORT_SYMBOL_GPL(media_detect_poll_disable);
+
 void media_device_init(struct media_device *mdev)
 {
 	INIT_LIST_HEAD(&mdev->entities);
@@ -828,6 +903,8 @@ void media_device_init(struct media_device *mdev)
 	INIT_LIST_HEAD(&mdev->links);
 	INIT_LIST_HEAD(&mdev->entity_notify);
 	INIT_LIST_HEAD(&mdev->fh_list);
+	INIT_LIST_HEAD(&mdev->detect_entities);
+	INIT_DELAYED_WORK(&mdev->detect_work, media_detect_work_fn);
 
 	mutex_init(&mdev->req_queue_mutex);
 	mutex_init(&mdev->graph_mutex);
@@ -849,6 +926,7 @@ EXPORT_SYMBOL_GPL(media_device_init);
 
 void media_device_cleanup(struct media_device *mdev)
 {
+	cancel_delayed_work_sync(&mdev->detect_work);
 	ida_destroy(&mdev->entity_internal_idx);
 	mdev->entity_internal_idx_max = 0;
 	media_graph_walk_cleanup(&mdev->pm_count_walk);
@@ -945,6 +1023,8 @@ void media_device_unregister(struct media_device *mdev)
 	if (mdev == NULL)
 		return;
 
+	cancel_delayed_work_sync(&mdev->detect_work);
+
 	mutex_lock(&mdev->graph_mutex);
 
 	/* Check if mdev was ever registered at all */
diff --git a/include/media/media-device.h b/include/media/media-device.h
index 9a0f38914354..d1db783db4ac 100644
--- a/include/media/media-device.h
+++ b/include/media/media-device.h
@@ -16,6 +16,7 @@
 #include <linux/mutex.h>
 #include <linux/pci.h>
 #include <linux/platform_device.h>
+#include <linux/workqueue.h>
 
 #include <media/media-devnode.h>
 #include <media/media-entity.h>
@@ -199,6 +200,9 @@ struct media_device {
 
 	struct list_head fh_list;
 	spinlock_t fh_list_lock;
+
+	struct list_head detect_entities;
+	struct delayed_work detect_work;
 };
 
 /* We don't need to include usb.h here */
diff --git a/include/media/media-entity.h b/include/media/media-entity.h
index b0e1fc627d17..dea3a742a21a 100644
--- a/include/media/media-entity.h
+++ b/include/media/media-entity.h
@@ -343,6 +343,9 @@ enum media_entity_type {
  *		re-used if entities are unregistered or registered again.
  * @pads:	Pads array with the size defined by @num_pads.
  * @links:	List of data links.
+ * @detect_list: Entry in &media_device.detect_entities for poll-based
+ *		detection. Managed by media_detect_poll_enable() /
+ *		media_detect_poll_disable().
  * @ops:	Entity operations.
  * @use_count:	Use count for the entity.
  * @info:	Union with devnode information.  Kept just for backward
@@ -372,6 +375,7 @@ struct media_entity {
 
 	struct media_pad *pads;
 	struct list_head links;
+	struct list_head detect_list;
 
 	const struct media_entity_operations *ops;
 
@@ -1067,6 +1071,34 @@ void media_entity_set_status(struct media_entity *entity, u32 status);
  */
 int media_entity_detect_or_fail(struct media_entity *entity);
 
+/**
+ * media_detect_poll_enable - Enable periodic .detect() polling for an entity
+ * @entity: The entity to poll
+ *
+ * Add @entity to the media device's detect poll list and schedule the
+ * detect worker if not already running. The worker periodically calls
+ * the entity's &media_entity_operations.detect() operation and updates
+ * the entity status on change.
+ *
+ * Intended to be called from the driver's enable_streams() callback,
+ * so that detection polling only runs while the entity is powered and
+ * streaming.
+ *
+ * The entity must implement the .detect() operation.
+ */
+void media_detect_poll_enable(struct media_entity *entity);
+
+/**
+ * media_detect_poll_disable - Disable periodic .detect() polling for an entity
+ * @entity: The entity to stop polling
+ *
+ * Remove @entity from the media device's detect poll list. If no more
+ * entities are being polled, the detect worker is cancelled.
+ *
+ * Intended to be called from the driver's disable_streams() callback.
+ */
+void media_detect_poll_disable(struct media_entity *entity);
+
 /**
  * media_entity_pipeline - Get the media pipeline an entity is part of
  * @entity: The entity

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 15/16] media: imx219: Allow driver probe with missing sensor
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (13 preceding siblings ...)
  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 ` Mattijs Korpershoek
  2026-11-18 12:44 ` [PATCH RFC v2 16/16] media: imx219: Implement .detect() sensor operation Mattijs Korpershoek
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:43 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

When media fault tolerance is enabled, probe() should complete even when
a sensor is disconnected.

Currently, the driver reads the CHIP_ID over i2c in the probe().
When we can't read CHIP_ID, the probe errors out, which result in the
v4l2-subdev not being created.

Extract imx219_read_chip_id() from imx219_identify_module() so that
probe can distinguish an unreachable sensor (I2C read failure) from a
wrong sensor (CHIP_ID mismatch).

When the sensor is unreachable, skip the LP-11 state initialization and
continue probe so the v4l2-subdev and media entity are created.

Note that a CHIP_ID mismatch remains a fatal error.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/i2c/imx219.c | 74 ++++++++++++++++++++++------------------------
 1 file changed, 36 insertions(+), 38 deletions(-)

diff --git a/drivers/media/i2c/imx219.c b/drivers/media/i2c/imx219.c
index 1580562093e8..02d065fb88d0 100644
--- a/drivers/media/i2c/imx219.c
+++ b/drivers/media/i2c/imx219.c
@@ -1099,25 +1099,9 @@ static int imx219_get_regulators(struct imx219 *imx219)
 				       imx219->supplies);
 }
 
-/* Verify chip ID */
-static int imx219_identify_module(struct imx219 *imx219)
+static int imx219_read_chip_id(struct imx219 *imx219, u64 *chip_id)
 {
-	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
-	int ret;
-	u64 val;
-
-	ret = cci_read(imx219->regmap, IMX219_REG_CHIP_ID, &val, NULL);
-	if (ret)
-		return dev_err_probe(&client->dev, ret,
-				     "failed to read chip id %x\n",
-				     IMX219_CHIP_ID);
-
-	if (val != IMX219_CHIP_ID)
-		return dev_err_probe(&client->dev, -EIO,
-				     "chip id mismatch: %x!=%llx\n",
-				     IMX219_CHIP_ID, val);
-
-	return 0;
+	return cci_read(imx219->regmap, IMX219_REG_CHIP_ID, chip_id, NULL);
 }
 
 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
@@ -1194,6 +1178,7 @@ static int imx219_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct imx219 *imx219;
 	int ret;
+	u64 val;
 
 	imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL);
 	if (!imx219)
@@ -1235,36 +1220,49 @@ static int imx219_probe(struct i2c_client *client)
 				     "failed to get reset gpio\n");
 
 	/*
-	 * The sensor must be powered for imx219_identify_module()
+	 * The sensor must be powered for imx219_read_chip_id()
 	 * to be able to read the CHIP_ID register
 	 */
 	ret = imx219_power_on(dev);
 	if (ret)
 		return ret;
 
-	ret = imx219_identify_module(imx219);
-	if (ret)
-		goto error_power_off;
-
-	/*
-	 * Sensor doesn't enter LP-11 state upon power up until and unless
-	 * streaming is started, so upon power up switch the modes to:
-	 * streaming -> standby
-	 */
-	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
-			IMX219_MODE_STREAMING, NULL);
-	if (ret < 0)
+	/* Verify chip ID */
+	ret = imx219_read_chip_id(imx219, &val);
+	if (ret) {
+		if (!media_fault_tolerance_enabled()) {
+			dev_err_probe(&client->dev, ret,
+				      "failed to read chip id %x\n",
+				      IMX219_CHIP_ID);
+			goto error_power_off;
+		}
+		dev_dbg(&client->dev, "failed to read chip id %x\n", IMX219_CHIP_ID);
+	} else if (val != IMX219_CHIP_ID) {
+		ret = dev_err_probe(&client->dev, -EIO,
+				    "chip id mismatch: %x!=%llx\n",
+				    IMX219_CHIP_ID, val);
 		goto error_power_off;
+	} else {
+		/*
+		 * Sensor doesn't enter LP-11 state upon power up until and unless
+		 * streaming is started, so upon power up switch the modes to:
+		 * streaming -> standby
+		 */
+		ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
+				IMX219_MODE_STREAMING, NULL);
+		if (ret < 0)
+			goto error_power_off;
 
-	usleep_range(100, 110);
+		usleep_range(100, 110);
 
-	/* put sensor back to standby mode */
-	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
-			IMX219_MODE_STANDBY, NULL);
-	if (ret < 0)
-		goto error_power_off;
+		/* put sensor back to standby mode */
+		ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
+				IMX219_MODE_STANDBY, NULL);
+		if (ret < 0)
+			goto error_power_off;
 
-	usleep_range(100, 110);
+		usleep_range(100, 110);
+	}
 
 	ret = imx219_init_controls(imx219);
 	if (ret)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH RFC v2 16/16] media: imx219: Implement .detect() sensor operation
  2026-11-18 12:43 [PATCH RFC v2 00/16] media: Fault-Tolerant V4L2 Mattijs Korpershoek
                   ` (14 preceding siblings ...)
  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 ` Mattijs Korpershoek
  15 siblings, 0 replies; 17+ messages in thread
From: Mattijs Korpershoek @ 2026-11-18 12:44 UTC (permalink / raw)
  To: Hans Verkuil, Laurent Pinchart, Kieran Bingham, Sakari Ailus,
	Mauro Carvalho Chehab, Andy Walls, Kees Cook,
	Gustavo A. R. Silva
  Cc: Mattijs Korpershoek, Nicolas Dufresne, Hans de Goede,
	Michael Riesch, Dave Stevenson, Maxime Ripard, linux-media,
	linux-kernel, linux-hardening

Now that the driver can probe() with a missing sensor, userspace should
be able to query the sensor connection state via the media device.

Implement the .detect() sensor operation so that userspace can do that
using the MEDIA_IOC_G_ENTITY_STATUS ioctl.

Also call media_detect_poll_enable() when streaming successfully started
and poll_disable() when disabling streaming to make sure that the media
device reports the status to userspace while streaming.

Signed-off-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
---
 drivers/media/i2c/imx219.c | 47 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)

diff --git a/drivers/media/i2c/imx219.c b/drivers/media/i2c/imx219.c
index 02d065fb88d0..5d4432a97eee 100644
--- a/drivers/media/i2c/imx219.c
+++ b/drivers/media/i2c/imx219.c
@@ -28,6 +28,7 @@
 #include <media/v4l2-device.h>
 #include <media/v4l2-fwnode.h>
 #include <media/v4l2-mediabus.h>
+#include <media/v4l2-subdev.h>
 
 /* Chip ID */
 #define IMX219_REG_CHIP_ID		CCI_REG16(0x0000)
@@ -742,6 +743,10 @@ static int imx219_enable_streams(struct v4l2_subdev *sd,
 	if (ret < 0)
 		return ret;
 
+	ret = media_entity_detect_or_fail(&sd->entity);
+	if (ret)
+		goto err_rpm_put;
+
 	/* Send all registers that are common to all modes */
 	ret = cci_multi_reg_write(imx219->regmap, imx219_common_regs,
 				  ARRAY_SIZE(imx219_common_regs), NULL);
@@ -780,6 +785,8 @@ static int imx219_enable_streams(struct v4l2_subdev *sd,
 	__v4l2_ctrl_grab(imx219->vflip, true);
 	__v4l2_ctrl_grab(imx219->hflip, true);
 
+	media_detect_poll_enable(&sd->entity);
+
 	return 0;
 
 err_rpm_put:
@@ -795,6 +802,8 @@ static int imx219_disable_streams(struct v4l2_subdev *sd,
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
 	int ret;
 
+	media_detect_poll_disable(&sd->entity);
+
 	/* set stream off register */
 	ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT,
 			IMX219_MODE_STANDBY, NULL);
@@ -1006,6 +1015,33 @@ static int imx219_init_state(struct v4l2_subdev *sd,
 	return imx219_set_pad_format(sd, NULL, state, &fmt);
 }
 
+static int imx219_read_chip_id(struct imx219 *imx219, u64 *chip_id)
+{
+	return cci_read(imx219->regmap, IMX219_REG_CHIP_ID, chip_id, NULL);
+}
+
+static int imx219_detect(struct media_entity *entity)
+{
+	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+	struct imx219 *imx219 = to_imx219(sd);
+	u64 chip_id;
+	int ret;
+
+	ret = pm_runtime_resume_and_get(&client->dev);
+	if (ret < 0)
+		return ret;
+
+	if (imx219_read_chip_id(imx219, &chip_id))
+		ret = MEDIA_ENT_STATUS_DISCONNECTED;
+	else
+		ret = MEDIA_ENT_STATUS_CONNECTED;
+
+	pm_runtime_put_autosuspend(&client->dev);
+
+	return ret;
+}
+
 static const struct v4l2_subdev_video_ops imx219_video_ops = {
 	.s_stream = v4l2_subdev_s_stream_helper,
 };
@@ -1029,6 +1065,10 @@ static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
 	.init_state = imx219_init_state,
 };
 
+static const struct media_entity_operations imx219_entity_ops = {
+	.detect = imx219_detect,
+};
+
 /* -----------------------------------------------------------------------------
  * Power management
  */
@@ -1099,11 +1139,6 @@ static int imx219_get_regulators(struct imx219 *imx219)
 				       imx219->supplies);
 }
 
-static int imx219_read_chip_id(struct imx219 *imx219, u64 *chip_id)
-{
-	return cci_read(imx219->regmap, IMX219_REG_CHIP_ID, chip_id, NULL);
-}
-
 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
 {
 	struct fwnode_handle *endpoint;
@@ -1237,6 +1272,7 @@ static int imx219_probe(struct i2c_client *client)
 			goto error_power_off;
 		}
 		dev_dbg(&client->dev, "failed to read chip id %x\n", IMX219_CHIP_ID);
+		imx219->sd.entity.status = MEDIA_ENT_STATUS_DISCONNECTED;
 	} else if (val != IMX219_CHIP_ID) {
 		ret = dev_err_probe(&client->dev, -EIO,
 				    "chip id mismatch: %x!=%llx\n",
@@ -1271,6 +1307,7 @@ static int imx219_probe(struct i2c_client *client)
 	/* Initialize subdev */
 	imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 	imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+	imx219->sd.entity.ops = &imx219_entity_ops;
 
 	/* Initialize source pad */
 	imx219->pad.flags = MEDIA_PAD_FL_SOURCE;

-- 
2.56.0


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-11-18 12:44 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®