From patchwork Mon Nov 30 23:08:26 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Menzel X-Patchwork-Id: 29115 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EC655C3361 for ; Tue, 1 Dec 2026 01:39:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6072B6AA7E; Tue, 1 Dec 2026 02:39:30 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=molgen.mpg.de header.i=@molgen.mpg.de header.b="ZDeB+LbY"; dkim-atps=neutral Received: from mx3.molgen.mpg.de (mx3.molgen.mpg.de [141.14.17.11]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E90156AA42 for ; Tue, 1 Dec 2026 00:09:04 +0100 (CET) Received: from localhost (hotelcalifornia.molgen.mpg.de [141.14.16.151]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx.molgen.mpg.de (Postfix) with ESMTPS id 939D14C442F934; Tue, 01 Dec 2026 00:08:53 +0100 (CET) From: Paul Menzel To: libcamera-devel@lists.libcamera.org Cc: Paul Menzel , Laurent Pinchart , Kieran Bingham , =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Milan Zamazal , Hans de Goede Subject: [PATCH 1/2] libcamera: device_enumerator_udev: Re-enumerate media devices that gain a device node Date: Tue, 1 Dec 2026 00:08:26 +0100 Message-ID: <20261130230839.95850-2-pmenzel@molgen.mpg.de> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=molgen.mpg.de; s=20260906; t=1796080133; h=from:from:subject:date:message-id:mime-version:content-type:content-transfer-encoding; bh=e9EmRH22EjdIleRpAnUKU9sQTCDE87md1GVie1wSjkY=; b=ZDeB+LbYtPyEgq9qjCucDt1Atpi3iU448burguNDSv1YM5UbN8J13ftSHjzjOq0aFdDpWYhIbOZf zKPo6bgYVl7VCFzQz+lbwhmMRZlPDU0fFRd30e1ij58SIGia6a9XVbgYaWPeVeNU+SVtdcc4DKARwS 5NiG9zsYWTZd7lgt8QZBWxOcg2vd2yBJfiBeFPK0NFpCrEoDyjmyrWMbBsa77+2SpZ9xzcBx7gZOfc YHU2TOMIVtlNI6fi/qh/Urk51Y90ir/yzoUKoqVVhqUXhGO4QgaCfO+SFtC5UE+ALyGjMqCcdEWS/2 blF/W5jl03FijiuK+LH71rNp5Je5yVvA== X-Mailman-Approved-At: Tue, 01 Dec 2026 02:39:29 +0100 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org 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 --- 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(-) 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 createDevice(const std::string &deviceNode); void addDevice(std::unique_ptr media); void removeDevice(const std::string &deviceNode); + std::vector> idleDevices() const; private: std::vector> 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 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 +#include #include #include @@ -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> DeviceEnumerator::idleDevices() const +{ + std::vector> idle; + + std::copy_if(devices_.begin(), devices_.end(), std::back_inserter(idle), + [](const std::shared_ptr &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 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 &media : idleDevices()) { + std::unique_ptr fresh = createDevice(media->deviceNode()); + if (!fresh) + continue; + + const std::vector &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; } From patchwork Mon Nov 30 23:08:28 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Menzel X-Patchwork-Id: 29116 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B9E44C3304 for ; Tue, 1 Dec 2026 01:39:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A1A886AA86; Tue, 1 Dec 2026 02:39:31 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=molgen.mpg.de header.i=@molgen.mpg.de header.b="la38N5Xz"; dkim-atps=neutral Received: from mx3.molgen.mpg.de (mx3.molgen.mpg.de [141.14.17.11]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C20BB6AA77 for ; Tue, 1 Dec 2026 00:09:37 +0100 (CET) Received: from localhost (hotelcalifornia.molgen.mpg.de [141.14.16.151]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx.molgen.mpg.de (Postfix) with ESMTPS id 1C8514C442F948; Tue, 01 Dec 2026 00:09:28 +0100 (CET) From: Paul Menzel To: libcamera-devel@lists.libcamera.org Cc: Paul Menzel , Laurent Pinchart , Kieran Bingham , =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Milan Zamazal , Hans de Goede Subject: [PATCH 2/2] pipeline: simple: Enable hot-unplug of the media devices Date: Tue, 1 Dec 2026 00:08:28 +0100 Message-ID: <20261130230839.95850-4-pmenzel@molgen.mpg.de> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261130230839.95850-2-pmenzel@molgen.mpg.de> References: <20261130230839.95850-2-pmenzel@molgen.mpg.de> MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=molgen.mpg.de; s=20260906; t=1796080168; h=from:from:subject:date:message-id:mime-version:content-type:content-transfer-encoding; bh=TxZ/iWnj6M2HKFJ/N0OIMHCjccyPNUGHBiwPKZJWTAs=; b=la38N5XzCbQ6mxjmY4aT1aL2Dp87eF6xyGe9Gb+4z+MLJgA/YTOQH5IqkpKQy72wgSGX8JEZveW6 h+EoGueUFxEir52IrB83LBHn+cDAoLDo8CoBVso59rODDc3fKOfZfJYy529AS1vxfWhp9vRHhfWRb7 1ARrKd76IfAljwel8S4kuZhXczU0HlV5jHk0EIbOz9/seXnFpIt4dT53FlNUKnSIHitHgv5kVzR7cu HgSWVDNKl5ScNyRzuW+D471UkrDfIVwPiAS2EVIWRmgns0xwCSGlJdO/mQuHaUbd5eTY8HeYwfHhU/ GF7Bh7sSbIthJBRNlyLwK2DNS6ygmgwg== X-Mailman-Approved-At: Tue, 01 Dec 2026 02:39:29 +0100 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org The simple pipeline handler does not call hotplugMediaDevice(), so its cameras stay registered when their media device goes away, for instance when the driver is unbound. If the media device comes back, the new instance yields a camera with the same ID. Before commit 50858dc3 ("libcamera: camera_manager: Non-fatal duplicate id detection") this aborted the process, as seen in WirePlumber on a Dell XPS 9315 (Intel IPU6) when the media device and its V4L2 nodes were removed and added again with synthetic udev events: FATAL Camera camera_manager.cpp:213 Trying to register a camera with a duplicated ID '\_SB_.PC00.LNK1' Now the new camera is dropped instead, and the stale one stays. With a real driver unbind on the same laptop (intel_ipu6_isys) and libcamera 0.7.2, the enumerator removes /dev/media0, DEBUG DeviceEnumerator device_enumerator.cpp:331 Media device for node /dev/media0 removed. but PipeWire keeps listing the camera, and the stale camera keeps its V4L2 device nodes open. After binding the driver again, only four of the six V4L2 subdevices came back (CSI2 0-3, numbered 0, 1, 3 and 6), the IVSC CSI and sensor subdevices were missing, and no camera was added. When WirePlumber closed the stale handles at shutdown, the kernel crashed in v4l2_release() (a separate kernel bug, the IPU6 driver frees its video devices on unbind while they are open [1]). Enable hot-unplug notifications for the media device, and for the converter if one is used, once a camera has been registered, like the UVC pipeline handler does. With this change, on the same laptop and in the same test (2 of 2 runs), the camera is removed when intel_ipu6_isys is unbound, and after binding it again all six subdevices are back and the camera is added again in the same WirePlumber process: DEBUG DeviceEnumerator device_enumerator.cpp:333 Media device for node /dev/media0 removed. INFO Camera camera_manager.cpp:223 Adding camera '\_SB_.PC00.LNK1' for pipeline handler simple [1]: https://lore.kernel.org/all/abcc474a-bd58-45da-8551-d7925ca18d52@molgen.mpg.de/ Signed-off-by: Paul Menzel --- 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, where the touched files are identical), and compile-tested on master with the unit tests. The test only checked that the camera is listed again, not that it streams. src/libcamera/pipeline/simple/simple.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp index 2f7f4c89..e658f85a 100644 --- a/src/libcamera/pipeline/simple/simple.cpp +++ b/src/libcamera/pipeline/simple/simple.cpp @@ -2082,7 +2082,20 @@ bool SimplePipelineHandler::matchDevice(std::shared_ptr media, registered = true; } - return registered; + if (!registered) + return false; + + /* + * Enable hot-unplug notifications. Platform media devices go away + * when their driver is unbound, and the cameras have to be + * unregistered then, or they would clash with the cameras created + * when the driver is bound again. + */ + hotplugMediaDevice(media); + if (converter_) + hotplugMediaDevice(converter_); + + return true; } bool SimplePipelineHandler::match(DeviceEnumerator *enumerator)