[PATCH RFC v2 13/16] media: mc: Add MEDIA_EVENT_ENTITY_STATUS_CHANGE event
From: Mattijs Korpershoek
Date: Wed Nov 18 2026 - 07:45:42 EST
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@xxxxxxxxxx>
---
.../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