[1/2] libcamera: device_enumerator_udev: Re-enumerate media devices that gain a device node
diff mbox series

Message ID 20261130230839.95850-2-pmenzel@molgen.mpg.de
State New
Headers show
Series
  • [1/2] libcamera: device_enumerator_udev: Re-enumerate media devices that gain a device node
Related show

Commit Message

Paul Menzel Nov. 30, 2026, 11:08 p.m. UTC
The udev device enumerator matches the entities of a media device with
their device nodes only once, when the media device is added. A V4L2
device node that appears later is put on the orphans list and never
looked at again.

This breaks cameras whose sensor joins the media graph after the media
device has been registered, which is the normal order on Intel IPU3 and
IPU6 laptops: ipu-bridge creates the sensor’s fwnode graph in the ISP
probe function, so the sensor driver can only probe afterwards and its
subdevice is bound through the V4L2 async notifier. If a libcamera
client is already running, the simple pipeline handler fails to match,

    INFO SimplePipeline simple.cpp:1911 No sensor found for /dev/media0

and the camera does not show up until the client is restarted. On a
Dell XPS 9315 the IVSC firmware download delays the IPU6 probe by about
ten seconds, past the start of WirePlumber, so PipeWire offers no camera
after most boots.

When a V4L2 device node that no pending media device waits for appears
after the initial enumeration, populate the media devices that no
pipeline handler has acquired again. If the graph of one of them now
contains an entity backed by the new device node, return the device
nodes of its entities to the orphans list, remove it, and add the
freshly populated instance, so that the pipeline handlers get another
chance to match it. Media devices in use are left alone.

Tested on the Dell XPS 9315 with Debian’s libcamera 0.7.2 plus this
change (the touched files are identical in 0.7.2 and master): unbind
the ov01a10 driver, restart WirePlumber, bind the driver again. Without
the change no camera appears (2 of 2 runs); with it (2 of 2):

    DEBUG DeviceEnumerator device_enumerator_udev.cpp:179 Media device /dev/media0 gained device 81:37, re-enumerating it
    INFO Camera camera_manager.cpp:223 Adding camera '\_SB_.PC00.LNK1' for pipeline handler simple

Signed-off-by: Paul Menzel <pmenzel@molgen.mpg.de>
---

Notes:
    This patch was written with the help of an LLM (Claude Code, claude-opus-5.5). It was built and tested on the Dell XPS 9315 as described above (on top of libcamera 0.7.2), and compile-tested on master with the unit tests.

 .../libcamera/internal/device_enumerator.h    |   1 +
 .../internal/device_enumerator_udev.h         |   2 +
 src/libcamera/device_enumerator.cpp           |  23 ++++
 src/libcamera/device_enumerator_udev.cpp      | 125 ++++++++++++++----
 4 files changed, 125 insertions(+), 26 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
index 5e1aec99..77948af7 100644
--- a/include/libcamera/internal/device_enumerator.h
+++ b/include/libcamera/internal/device_enumerator.h
@@ -52,6 +52,7 @@  protected:
 	std::unique_ptr<MediaDevice> createDevice(const std::string &deviceNode);
 	void addDevice(std::unique_ptr<MediaDevice> media);
 	void removeDevice(const std::string &deviceNode);
+	std::vector<std::shared_ptr<MediaDevice>> idleDevices() const;
 
 private:
 	std::vector<std::shared_ptr<MediaDevice>> devices_;
diff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h
index 1ce988ab..d59f3872 100644
--- a/include/libcamera/internal/device_enumerator_udev.h
+++ b/include/libcamera/internal/device_enumerator_udev.h
@@ -60,6 +60,8 @@  private:
 	LIBCAMERA_DISABLE_COPY_AND_MOVE(DeviceEnumeratorUdev)
 
 	int addUdevDevice(struct udev_device *dev);
+	int addMediaDevice(std::unique_ptr<MediaDevice> media);
+	void rescanIdleMediaDevices(dev_t devnum);
 	void removeUdevDevice(struct udev_device *dev);
 	int populateMediaDevice(MediaDevice *media, DependencyMap *deps);
 	std::string lookupDeviceNode(dev_t devnum);
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index 0ce65e89..002aca20 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -7,6 +7,8 @@ 
 
 #include "libcamera/internal/device_enumerator.h"
 
+#include <algorithm>
+#include <iterator>
 #include <string.h>
 
 #include <libcamera/base/log.h>
@@ -334,6 +336,27 @@  void DeviceEnumerator::removeDevice(const std::string &deviceNode)
 	media->disconnected.emit();
 }
 
+/**
+ * \brief Retrieve the media devices not acquired by any pipeline handler
+ *
+ * This function is meant for device enumerators that need to re-examine media
+ * devices they have added, for instance when the media graph of a device that
+ * no pipeline handler could use has grown since it was populated.
+ *
+ * \return The media devices that are not busy
+ */
+std::vector<std::shared_ptr<MediaDevice>> DeviceEnumerator::idleDevices() const
+{
+	std::vector<std::shared_ptr<MediaDevice>> idle;
+
+	std::copy_if(devices_.begin(), devices_.end(), std::back_inserter(idle),
+		     [](const std::shared_ptr<MediaDevice> &media) {
+			     return !media->busy();
+		     });
+
+	return idle;
+}
+
 /**
  * \brief Search available media devices for a pattern match
  * \param[in] dm Search pattern
diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp
index 8b9376ca..d898269b 100644
--- a/src/libcamera/device_enumerator_udev.cpp
+++ b/src/libcamera/device_enumerator_udev.cpp
@@ -95,32 +95,7 @@  int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)
 		if (!media)
 			return -ENODEV;
 
-		DependencyMap deps;
-		int ret = populateMediaDevice(media.get(), &deps);
-		if (ret < 0) {
-			LOG(DeviceEnumerator, Warning)
-				<< "Failed to populate media device "
-				<< media->deviceNode()
-				<< " (" << media->driver() << "), skipping";
-			return ret;
-		}
-
-		if (!deps.empty()) {
-			LOG(DeviceEnumerator, Debug)
-				<< "Defer media device " << media->deviceNode()
-				<< " due to " << deps.size()
-				<< " missing dependencies";
-
-			pending_.emplace_back(std::move(media), std::move(deps));
-			MediaDeviceDeps *mediaDeps = &pending_.back();
-			for (const auto &dep : mediaDeps->deps_)
-				devMap_[dep.first] = mediaDeps;
-
-			return 0;
-		}
-
-		addDevice(std::move(media));
-		return 0;
+		return addMediaDevice(std::move(media));
 	}
 
 	if (!strcmp(subsystem, "video4linux")) {
@@ -131,6 +106,95 @@  int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)
 	return -ENODEV;
 }
 
+/**
+ * \brief Associate a media device with its device nodes and add it
+ * \param[in] media The media device, created with createDevice()
+ *
+ * Add the media device to the enumerator right away if the device nodes of all
+ * its entities have been reported by udev, or defer it until they have.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int DeviceEnumeratorUdev::addMediaDevice(std::unique_ptr<MediaDevice> media)
+{
+	DependencyMap deps;
+	int ret = populateMediaDevice(media.get(), &deps);
+	if (ret < 0) {
+		LOG(DeviceEnumerator, Warning)
+			<< "Failed to populate media device "
+			<< media->deviceNode()
+			<< " (" << media->driver() << "), skipping";
+		return ret;
+	}
+
+	if (!deps.empty()) {
+		LOG(DeviceEnumerator, Debug)
+			<< "Defer media device " << media->deviceNode()
+			<< " due to " << deps.size()
+			<< " missing dependencies";
+
+		pending_.emplace_back(std::move(media), std::move(deps));
+		MediaDeviceDeps *mediaDeps = &pending_.back();
+		for (const auto &dep : mediaDeps->deps_)
+			devMap_[dep.first] = mediaDeps;
+
+		return 0;
+	}
+
+	addDevice(std::move(media));
+	return 0;
+}
+
+/**
+ * \brief Re-enumerate the idle media device a new device node belongs to
+ * \param[in] devnum The device number of the new V4L2 device node
+ *
+ * Entities can join a media graph after the media device has been registered.
+ * This is the common case for camera sensors bound through a V4L2 async
+ * notifier: the ISP registers its media device in its probe function, and the
+ * sensor subdevice is added once the sensor driver has probed, possibly after
+ * the media device has been enumerated and no pipeline handler could match it.
+ *
+ * Look for an idle media device whose graph now contains an entity backed by
+ * \a devnum. If one is found, return the device nodes of its entities to the
+ * orphans list, drop it, and add a freshly populated instance, which gives the
+ * pipeline handlers another chance to match it.
+ */
+void DeviceEnumeratorUdev::rescanIdleMediaDevices(dev_t devnum)
+{
+	for (const std::shared_ptr<MediaDevice> &media : idleDevices()) {
+		std::unique_ptr<MediaDevice> fresh = createDevice(media->deviceNode());
+		if (!fresh)
+			continue;
+
+		const std::vector<MediaEntity *> &entities = fresh->entities();
+		bool member = std::any_of(entities.begin(), entities.end(),
+					  [devnum](const MediaEntity *entity) {
+						  return makedev(entity->deviceMajor(),
+								 entity->deviceMinor()) == devnum;
+					  });
+		if (!member)
+			continue;
+
+		LOG(DeviceEnumerator, Debug)
+			<< "Media device " << media->deviceNode()
+			<< " gained device " << major(devnum) << ":"
+			<< minor(devnum) << ", re-enumerating it";
+
+		for (const MediaEntity *entity : media->entities()) {
+			if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0)
+				continue;
+
+			orphans_.insert(makedev(entity->deviceMajor(),
+						entity->deviceMinor()));
+		}
+
+		removeDevice(media->deviceNode());
+		addMediaDevice(std::move(fresh));
+		return;
+	}
+}
+
 void DeviceEnumeratorUdev::removeUdevDevice(struct udev_device *dev)
 {
 	const char *subsystem = udev_device_get_subsystem(dev);
@@ -313,6 +377,15 @@  int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum)
 	auto it = devMap_.find(devnum);
 	if (it == devMap_.end()) {
 		orphans_.insert(devnum);
+
+		/*
+		 * Once the initial enumeration is done, a device node that no
+		 * pending media device waits for may have joined the graph of
+		 * a media device added earlier.
+		 */
+		if (notifier_)
+			rescanIdleMediaDevices(devnum);
+
 		return 0;
 	}