[{"id":35530,"web_url":"https://patchwork.libcamera.org/comment/35530/","msgid":"<d7aa7bb1-a5a8-48f5-9083-2a00bcd07573@ideasonboard.com>","date":"2025-08-20T13:36:31","subject":"Re: [PATCH 3/3] libcamera: device_enumerator_udev: Defer invalid\n\tmedia devices","submitter":{"id":216,"url":"https://patchwork.libcamera.org/api/people/216/","name":"Barnabás Pőcze","email":"barnabas.pocze@ideasonboard.com"},"content":"Hi\n\n2025. 08. 20. 15:23 keltezéssel, Daniel Scally írta:\n> A MediaDevice created by DeviceEnumeratorUdev may have an invalid\n> topology if the device nodes for some of its entities have not yet\n> been created. If that's the case, defer initialisation of the\n> MediaDevice. Re-try all deferred media devices whenever a new device\n> is added to see if they're ready to be initialised yet.\n> \n> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n> ---\n>   .../internal/device_enumerator_udev.h         |  2 +\n>   src/libcamera/device_enumerator.cpp           |  2 +-\n>   src/libcamera/device_enumerator_udev.cpp      | 37 +++++++++++++++++++\n>   3 files changed, 40 insertions(+), 1 deletion(-)\n> \n> diff --git a/include/libcamera/internal/device_enumerator_udev.h b/include/libcamera/internal/device_enumerator_udev.h\n> index a5e5e6efe..b76315c63 100644\n> --- a/include/libcamera/internal/device_enumerator_udev.h\n> +++ b/include/libcamera/internal/device_enumerator_udev.h\n> @@ -13,6 +13,7 @@\n>   #include <set>\n>   #include <string>\n>   #include <sys/types.h>\n> +#include <vector>\n>   \n>   #include \"libcamera/internal/device_enumerator.h\"\n>   \n> @@ -67,6 +68,7 @@ private:\n>   \tEventNotifier *notifier_;\n>   \n>   \tstd::set<dev_t> orphans_;\n> +\tstd::vector<std::unique_ptr<MediaDevice>> topologyPending_;\n>   \tstd::list<MediaDeviceDeps> pending_;\n>   \tstd::map<dev_t, MediaDeviceDeps *> devMap_;\n>   };\n> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\n> index ae17862f6..ab7bceec4 100644\n> --- a/src/libcamera/device_enumerator.cpp\n> +++ b/src/libcamera/device_enumerator.cpp\n> @@ -220,7 +220,7 @@ std::unique_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &d\n>   \tstd::unique_ptr<MediaDevice> media = std::make_unique<MediaDevice>(deviceNode);\n>   \n>   \tint ret = media->populate();\n> -\tif (ret < 0) {\n> +\tif (ret < 0 && ret != -EAGAIN) {\n>   \t\tLOG(DeviceEnumerator, Info)\n>   \t\t\t<< \"Unable to populate media device \" << deviceNode\n>   \t\t\t<< \" (\" << strerror(-ret) << \"), skipping\";\n> diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp\n> index 53b4fac80..631e583e7 100644\n> --- a/src/libcamera/device_enumerator_udev.cpp\n> +++ b/src/libcamera/device_enumerator_udev.cpp\n> @@ -82,6 +82,14 @@ int DeviceEnumeratorUdev::addUdevDevice(struct udev_device *dev)\n>   \t\tif (!media)\n>   \t\t\treturn -ENODEV;\n>   \n> +\t\tif (!media->isValid()) {\n> +\t\t\tLOG(DeviceEnumerator, Debug)\n> +\t\t\t\t<< \"Defer media device \" << media->deviceNode()\n> +\t\t\t\t<< \" due to an invalid topology\";\n> +\t\t\ttopologyPending_.emplace_back(std::move(media));\n> +\t\t\treturn 0;\n> +\t\t}\n> +\n>   \t\treturn initMediaDevice(std::move(media));\n>   \t}\n>   \n> @@ -353,6 +361,35 @@ void DeviceEnumeratorUdev::udevNotify()\n>   \t\t<< action << \" device \" << deviceNode;\n>   \n>   \tif (action == \"add\") {\n> +\t\t/*\n> +\t\t* The addition of a new device may signal that a previously\n> +\t\t* deferred media device has had its topology updated to the\n> +\t\t* extent that it's now valid - retry devices deferred due to\n> +\t\t* invalid topology to see if they're now ready.\n> +\t\t*/\n> +\n> +\t\tLOG(DeviceEnumerator, Debug)\n> +\t\t\t<< \"Re-evaluating \" << topologyPending_.size()\n> +\t\t\t<< \" deferred media devices\";\n> +\n> +\t\tfor (auto media = topologyPending_.begin();\n> +\t\t     media != topologyPending_.end(); ++media) {\n> +\t\t\tLOG(DeviceEnumerator, Debug)\n> +\t\t\t\t<< \"Evaluating media device \" << (*media)->deviceNode()\n> +\t\t\t\t<< \" again...\";\n> +\n> +\t\t\tint ret = (*media)->populate();\n> +\t\t\tif (ret == -EAGAIN) {\n> +\t\t\t\tLOG(DeviceEnumerator, Debug)\n> +\t\t\t\t\t<< \"Media device \" << (*media)->deviceNode()\n> +\t\t\t\t\t<< \" still has invalid topology\";\n> +\t\t\t\tcontinue;\n> +\t\t\t}\n> +\n> +\t\t\tinitMediaDevice(std::move(*media));\n> +\t\t\ttopologyPending_.erase(media);\n\nI'm not sure this is correct. Generally one is not allowed to modify containers\nduring iteration. If you omit this erase, after the loop, I believe something\nlike this could be done:\n\n   topologyPending_.erase(std::remove(topologyPending_.begin(), topologyPending_.end(), nullptr),\n                          topologyPending_.end());\n\n\nRegards,\nBarnabás Pőcze\n\n> +\t\t}\n> +\n>   \t\taddUdevDevice(dev);\n>   \t} else if (action == \"remove\") {\n>   \t\tconst char *subsystem = udev_device_get_subsystem(dev);","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 838ECBEFBE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 20 Aug 2025 13:36:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9AB8B6924B;\n\tWed, 20 Aug 2025 15:36:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D5D4E6142C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 20 Aug 2025 15:36:35 +0200 (CEST)","from [192.168.33.19] (185.221.141.188.nat.pool.zt.hu\n\t[185.221.141.188])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D9D866AF;\n\tWed, 20 Aug 2025 15:35:36 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ins1dG9s\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1755696937;\n\tbh=GYElPg4dQgal3AcFo2HRlniHth6MEjwquF99y7T2RnU=;\n\th=Date:Subject:To:References:From:In-Reply-To:From;\n\tb=ins1dG9sRx52GE6tmbpkmO/YcXvVYdYq1GSy+5UGzsIsO0u3NbJRMfkhAF/Ndjgp8\n\t4K1O0pKW+B6eFlQ+/+sN/rORJ4Fcy+k8c37dRIZ8mIHPU5ex2LWnNe5yUBWj+cqY36\n\t8T3VIq1f8bzM4jLqODIz9QzCNucm+koCk9hb4zCg=","Message-ID":"<d7aa7bb1-a5a8-48f5-9083-2a00bcd07573@ideasonboard.com>","Date":"Wed, 20 Aug 2025 15:36:31 +0200","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH 3/3] libcamera: device_enumerator_udev: Defer invalid\n\tmedia devices","To":"Daniel Scally <dan.scally@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20250820132316.1033443-1-dan.scally@ideasonboard.com>\n\t<20250820132316.1033443-4-dan.scally@ideasonboard.com>","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Content-Language":"en-US, hu-HU","In-Reply-To":"<20250820132316.1033443-4-dan.scally@ideasonboard.com>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]