[PATCH RFC v2 12/16] media: mc: Add MEDIA_IOC_G_ENTITY_STATUS ioctl

From: Mattijs Korpershoek

Date: Wed Nov 18 2026 - 07:44:55 EST


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@xxxxxxxxxx>
---
.../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