From patchwork Thu Nov 19 16:39:47 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29045 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 A84ECC3361 for ; Thu, 19 Nov 2026 16:40:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 692946A92F; Thu, 19 Nov 2026 17:40:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="wkN2qerK"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D757F6A928 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A645113D3; Thu, 19 Nov 2026 17:37:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106245; bh=CLpBa9ETYoiNILn2p7xAtPG6Vwz2eJoYXSQUTl5zlAc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=wkN2qerK+uC1fvfqBLynoa6nfD0mdhMnh7UQZJGHcSAEP06jd3rz48oCKFtfaEUx5 kF1qwzq/nQJhKGqwxCjeQYoT3tPZo6m82z7KoSfHMKzd0UJuol8YssfQElTqMjQSi4 ZB3gMUxnC2Ww9avdJKIWWydxJGpaLyqDpwjPll+Q= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:47 +0100 Subject: [PATCH 5/6] libcamera: device_enumerator: Use devnum to identify entities MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-5-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10060; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=CLpBa9ETYoiNILn2p7xAtPG6Vwz2eJoYXSQUTl5zlAc=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYCrTXTE03GVIrIr4/4kQ41xej9dIRTexhR hjU1tn+s7uJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PIbWEACVL2lSCjGmjE+haR0qRvAIvkJ7fOGcXvdgTm93MEK3zhR+fuvmECWEvsmHNuOhiyQ6d7t dvJajXn3h0AE8c58c6mUYSY3Yr1MIkL6uebo52Zpkrxra1GJY2A0BhszEqWnK/tlR8KKAXSUVVg 38N6+bwKibKPY3h94muFvu2QByo7j9N1MkzZ6JW/+/DNsvyVhjDlv9Ui7kSymFc8qTPvnOEqvTF BosOPuOaz5YZsCKvd3l65LTIngT1+UapR/0J4c/VNKUYM7TEQWMDB5jq3jw7MJ5f9oudlz8Lgxl QgS0z/H8E3oQO9S8Y9uwleazF9LcZBew8faBX/Ieo/2N6uGM57Hppui6vG4QE34pu5+nSz2F+mg zGUcLBjn+fH+szq+jQiDTPLLyOAJO00dlhPhFiZWeicVfQ9lnjoOlGy7VcF6CoSa1sfZBZdNzef nabC6tdLTAsTrVwbR3uhRhda6t3rI/DU1DPbPVqViQj44yL6j7hPAKBD+FlzrhPkeTJpHAqs28F XJCG2E7L0hyRaVzfesC4VwZzWJRs+mXxEd2L0G4USiAeUArKf/H/bYtK0BwsdcNWVV/pR/eVifX aeXrlIg0NZfBjfzrq88YzHR6ONWUWKA8RLy880hAPPWlXKiv6yQeIddrN+XmzeoyUpOOugj9x3q mjhqdeW1rpyowNA== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B 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 Sender: "libcamera-devel" The DeviceEnumeratorUdev and DeviceEnumeratorSysfs populates the media graph in two steps. Initially, at DeviceEnumerator::createDevice() time, the media device is created and its media topology enumerated in MediaDevice::populate(). Then the device node path is injected in the corresponding MediaEntity. To progress with the de-coupling of the DeviceEnumerator hierarchy from the MediaObject class, remove all usages of MediaEntity from the DeviceEnumerator and replace it with entities ids and the devnum-based interface of the MediaDevice class. The MediaDevice collects a list of media entities ids for each devnum it identifies during topology enumeration. The device enumerator uses those devnums to calculate the device node paths and passes them to the MediaDevice which stores them in a map of entity ids to devnode paths. When the MediaObjects are created later on the devnode paths are fetched from the map and injected in the MediaEntity. This change requires to delay the creation of MediaObjects after the enumerator has populated the devnode paths so that a valid path is available for each MediaEntity. Split the MediaObject creation to a dedicated function MediaDevice::createTopology() and call it in DeviceEnumerator just before adding the new MediaDevice. This nicely fit with the design goal of delaying MediaObject creation to a later point in time in the next patches, precisely when the MediaDevice is acquired. Signed-off-by: Jacopo Mondi --- .../libcamera/internal/device_enumerator_udev.h | 3 +- include/libcamera/internal/media_device.h | 8 ++++ src/libcamera/device_enumerator.cpp | 2 + src/libcamera/device_enumerator_sysfs.cpp | 15 ++++--- src/libcamera/device_enumerator_udev.cpp | 29 ++++++-------- src/libcamera/media_device.cpp | 46 +++++++++++++++++++--- 6 files changed, 71 insertions(+), 32 deletions(-) diff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h index 1ce988ab84d1..a7b2047d7d7a 100644 --- a/include/libcamera/internal/device_enumerator_udev.h +++ b/include/libcamera/internal/device_enumerator_udev.h @@ -27,7 +27,6 @@ namespace libcamera { class EventNotifier; class MediaDevice; -class MediaEntity; class DeviceEnumeratorUdev final : public DeviceEnumerator { @@ -39,7 +38,7 @@ public: int enumerate(); private: - using DependencyMap = std::map>; + using DependencyMap = std::map>; struct MediaDeviceDeps { MediaDeviceDeps(std::unique_ptr media, diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h index 6fd84de033b6..0e064e3fbcde 100644 --- a/include/libcamera/internal/media_device.h +++ b/include/libcamera/internal/media_device.h @@ -42,6 +42,7 @@ public: void unlock(); int populate(); + int createTopology(); bool isValid() const { return valid_; } bool match(const DeviceMatch &device) const; @@ -63,6 +64,12 @@ public: return devnumsMap_.at(devnum); } + void setEntityDeviceNode(unsigned int id, const std::string &devnode) + { + ASSERT(!devnodesMap_.contains(id)); + devnodesMap_[id] = devnode; + } + const std::vector &entities() const { return entities_; } MediaEntity *getEntityByName(const std::string &name) const; MediaEntity *getEntityByName(const std::regex &name) const; @@ -157,6 +164,7 @@ private: } mediaV2Topology_; std::map> devnumsMap_; + std::map devnodesMap_; UniqueFD fd_; bool valid_; diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp index 2b1056425f61..ded3b41b5f26 100644 --- a/src/libcamera/device_enumerator.cpp +++ b/src/libcamera/device_enumerator.cpp @@ -248,6 +248,8 @@ void DeviceEnumerator::addDevice(std::unique_ptr media) LOG(DeviceEnumerator, Debug) << "Added device " << media->deviceNode() << ": " << media->driver(); + media->createTopology(); + devices_.push_back(std::move(media)); /* \todo To batch multiple additions, emit with a small delay here. */ diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp index 7866885c0f73..f5248dd72315 100644 --- a/src/libcamera/device_enumerator_sysfs.cpp +++ b/src/libcamera/device_enumerator_sysfs.cpp @@ -96,18 +96,17 @@ int DeviceEnumeratorSysfs::enumerate() int DeviceEnumeratorSysfs::populateMediaDevice(MediaDevice *media) { /* Associate entities to device node paths. */ - for (MediaEntity *entity : media->entities()) { - if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0) - continue; + for (dev_t devnum : media->entitiesDevnums()) { + unsigned int major = major(devnum); + unsigned int minor = minor(devnum); - std::string deviceNode = lookupDeviceNode(entity->deviceMajor(), - entity->deviceMinor()); + std::string deviceNode = lookupDeviceNode(major, minor); if (deviceNode.empty()) return -EINVAL; - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; + const std::list &entitiesIds = media->entitiesIdsByDevNum(devnum); + for (unsigned int id : entitiesIds) + media->setEntityDeviceNode(id, deviceNode); } return 0; diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp index 8b9376ca6ac4..dbd0668e5b3b 100644 --- a/src/libcamera/device_enumerator_udev.cpp +++ b/src/libcamera/device_enumerator_udev.cpp @@ -221,19 +221,20 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap std::set children; /* Associate entities to device node paths. */ - for (MediaEntity *entity : media->entities()) { - if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0) - continue; + for (dev_t devnum : media->entitiesDevnums()) { - dev_t devnum = makedev(entity->deviceMajor(), - entity->deviceMinor()); + const std::list &entitiesIds = + media->entitiesIdsByDevNum(devnum); /* - * If the devnum isn't in the orphans list, add it to the unmet - * dependencies. + * If the devnum isn't in the orphans list, add all the entities + * ids to the list of dependencies. */ if (orphans_.find(devnum) == orphans_.end()) { - (*deps)[devnum].push_back(entity); + auto &it = (*deps)[devnum]; + for (unsigned int id : entitiesIds) + it.push_back(id); + continue; } @@ -246,9 +247,8 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap if (deviceNode.empty()) return -EINVAL; - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; + for (unsigned int id : entitiesIds) + media->setEntityDeviceNode(id, deviceNode); children.insert(devnum); } @@ -326,11 +326,8 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum) return -EINVAL; MediaDeviceDeps *deps = it->second; - for (MediaEntity *entity : deps->deps_[devnum]) { - int ret = entity->setDeviceNode(deviceNode); - if (ret) - return ret; - } + for (unsigned int id : deps->deps_[devnum]) + deps->media_->setEntityDeviceNode(id, deviceNode); /* * Remove the devnum from the unmet dependencies for this media device. diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index 6dd4bdcc49c7..0d5c3a18b471 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -260,12 +260,7 @@ int MediaDevice::populate() it->second.push_back(ent.id); } - /* Populate entities, pads and links. */ - if (populateEntities(mediaV2Topology_.topology_) && - populatePads(mediaV2Topology_.topology_) && - populateLinks(mediaV2Topology_.topology_)) - valid_ = true; - + valid_ = true; ret = 0; done: close(); @@ -278,6 +273,27 @@ done: return ret; } +/** + * \brief Create MediaObjects from a populated topology + * + * Populate the MediaObjects list and create MediaEntities, MediaPads and + * MediaLinks by parsing the topology populated at MediaDevice::populate() + * time. + * + * Requires the topology to be valid. + * + * \todo This function will be move to a new MediaContext type which will + * be created at MediaDevice::acquire() time. + */ +int MediaDevice::createTopology() +{ + ASSERT(valid_); + + return populateEntities(mediaV2Topology_.topology_) && + populatePads(mediaV2Topology_.topology_) && + populateLinks(mediaV2Topology_.topology_); +} + /** * \fn MediaDevice::isValid() * \brief Query whether the media graph has been populated and is valid @@ -409,6 +425,16 @@ bool MediaDevice::match(const DeviceMatch &device) const * \return A list of media entities ids */ +/** + * \fn MediaDevice::setEntityDeviceNode(unsigned in id, const std::string &devnode) + * \brief Associate the device node path \a devnode to the entity \a id + * \param[in] id The media entity id + * \param[in] devnode The device node path + * + * Associate to the media entity identified by \a id the path of its device + * node \a devnode. + */ + /** * \fn MediaDevice::entities() * \brief Retrieve the list of entities in the media graph @@ -757,6 +783,14 @@ bool MediaDevice::populateEntities(const struct media_v2_topology &topology) findInterface(topology, ent->id); MediaEntity *entity = new MediaEntity(this, ent, iface); + /* Inject the devnode path for entities with a devnum. */ + auto it = devnodesMap_.find(entity->id()); + if (it != devnodesMap_.end()) { + int ret = entity->setDeviceNode(it->second); + if (ret) + return false; + } + if (!addObject(entity)) { delete entity; return false;