[PATCH RFC v2 11/16] media: mc: Add .detect() to media_entity_operations
From: Mattijs Korpershoek
Date: Wed Nov 18 2026 - 07:44:48 EST
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@xxxxxxxxxx>
---
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