{"id":18935,"url":"https://patchwork.libcamera.org/api/1.1/patches/18935/?format=json","web_url":"https://patchwork.libcamera.org/patch/18935/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230808125228.29043-9-jacopo.mondi@ideasonboard.com>","date":"2023-08-08T12:52:27","name":"[libcamera-devel,RFC,8/9] libcamera: pipeline_handler: Add function to acquire a USB device","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b2f4648b242062f79222347208d8596041a8f5db","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18935/mbox/","series":[{"id":4002,"url":"https://patchwork.libcamera.org/api/1.1/series/4002/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4002","date":"2023-08-08T12:52:19","name":"libcamera: Generalize device match and support USB devices","version":1,"mbox":"https://patchwork.libcamera.org/series/4002/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18935/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18935/checks/","tags":{},"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 55C65C32B2\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Aug 2023 12:52:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8722A62806;\n\tTue,  8 Aug 2023 14:52:58 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 28D71627EB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Aug 2023 14:52:51 +0200 (CEST)","from uno.localdomain (mob-5-90-60-22.net.vodafone.it [5.90.60.22])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4D062864C; \n\tTue,  8 Aug 2023 14:51:43 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1691499178;\n\tbh=mtyz3Oza0nU6tQb7l72g9z6aNbT0OdIHbijtg8CYxEU=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=sTu09M1uHJ36VhfUOltLAh3oVGvD+rWbnDyXC7APeW/Taw/r1jJMn/lKV6C6p+9vH\n\tfZm9A5wMOl7bgs/RSSRmoG/omY4Dm9WoGsZWWhlI3tMo4p8uaG7Tdr62xYi2hbn7st\n\tUHKHYfpkhXIef7qYiW5KWRXdYvtwZDxMh8SZEK4c5MO3Wyjtw1EOWZ19pi1wkqA9ze\n\tcOX5uxkzlNFuMnx7cRE94A6HCaMjw9aLlcDraME4Xf9020txRyRbPSOpHSalVsnKXB\n\tFpqdK0mf1WlT2xaE4xcGyRT1nodUisCR9K8BGkxPsf0+HExgRm64hqZMsP187Qkhbk\n\tSX5cn39K2xQaw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1691499103;\n\tbh=mtyz3Oza0nU6tQb7l72g9z6aNbT0OdIHbijtg8CYxEU=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=MPsnwXhNvYrfm5q3UI4RfY0JHbYMsR5/lfqWt9W3DAOoum0aL+HAmSiXfhiJZqODg\n\t1HcqcJUmbXvsIHRP+t2R5zFSCfb4eEd+nH3TmLXRuTSjIunrynEicWMi4bV18jX1TP\n\tzulMwnEWQer6EeEpgyCSK48/wMLRfVxrhzY5Lpao="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"MPsnwXhN\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org, Sophie Friedrich <dev@flowerpot.me>","Date":"Tue,  8 Aug 2023 14:52:27 +0200","Message-Id":"<20230808125228.29043-9-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.40.1","In-Reply-To":"<20230808125228.29043-1-jacopo.mondi@ideasonboard.com>","References":"<20230808125228.29043-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC 8/9] libcamera: pipeline_handler: Add\n\tfunction to acquire a USB device","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>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a function to the PipelineHandler base class to acquire a USB device\nby providing a USBDeviceMatch class instance.\n\nSupport multiple CameraDevice derived classes by generalizing whenever\npossible the device type in use.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n include/libcamera/internal/pipeline_handler.h |  9 ++-\n src/libcamera/pipeline_handler.cpp            | 69 +++++++++++++++----\n 2 files changed, 64 insertions(+), 14 deletions(-)","diff":"diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h\nindex 02b2f33727a3..b6b68aab1521 100644\n--- a/include/libcamera/internal/pipeline_handler.h\n+++ b/include/libcamera/internal/pipeline_handler.h\n@@ -26,6 +26,7 @@ namespace libcamera {\n \n class Camera;\n class CameraConfiguration;\n+class CameraDevice;\n class CameraManager;\n class DeviceEnumerator;\n class MediaDeviceMatch;\n@@ -33,6 +34,8 @@ class FrameBuffer;\n class MediaDevice;\n class PipelineHandler;\n class Request;\n+class USBDevice;\n+class USBDeviceMatch;\n \n class PipelineHandler : public std::enable_shared_from_this<PipelineHandler>,\n \t\t\tpublic Object\n@@ -44,6 +47,8 @@ public:\n \tvirtual bool match(DeviceEnumerator *enumerator) = 0;\n \tMediaDevice *acquireMediaDevice(DeviceEnumerator *enumerator,\n \t\t\t\t\tconst MediaDeviceMatch &dm);\n+\tUSBDevice *acquireUSBDevice(DeviceEnumerator *enumerator,\n+\t\t\t\t    const USBDeviceMatch &dm);\n \n \tbool acquire();\n \tvoid release(Camera *camera);\n@@ -82,7 +87,7 @@ protected:\n \tCameraManager *manager_;\n \n private:\n-\tvoid unlockMediaDevices();\n+\tvoid unlockCameraDevices();\n \n \tvoid mediaDeviceDisconnected(MediaDevice *media);\n \tvirtual void disconnect();\n@@ -90,7 +95,7 @@ private:\n \tvoid doQueueRequest(Request *request);\n \tvoid doQueueRequests();\n \n-\tstd::vector<std::shared_ptr<MediaDevice>> mediaDevices_;\n+\tstd::vector<std::shared_ptr<CameraDevice>> cameraDevices_;\n \tstd::vector<std::weak_ptr<Camera>> cameras_;\n \n \tstd::queue<Request *> waitingRequests_;\ndiff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\nindex ca177cad985c..0b3e0fd8fa8b 100644\n--- a/src/libcamera/pipeline_handler.cpp\n+++ b/src/libcamera/pipeline_handler.cpp\n@@ -27,6 +27,7 @@\n #include \"libcamera/internal/media_device.h\"\n #include \"libcamera/internal/request.h\"\n #include \"libcamera/internal/tracepoints.h\"\n+#include \"libcamera/internal/usb_device.h\"\n \n /**\n  * \\file pipeline_handler.h\n@@ -76,8 +77,8 @@ PipelineHandler::PipelineHandler(CameraManager *manager)\n \n PipelineHandler::~PipelineHandler()\n {\n-\tfor (std::shared_ptr<MediaDevice> media : mediaDevices_)\n-\t\tmedia->release();\n+\tfor (std::shared_ptr<CameraDevice> device : cameraDevices_)\n+\t\tdevice->release();\n }\n \n /**\n@@ -139,11 +140,42 @@ MediaDevice *PipelineHandler::acquireMediaDevice(DeviceEnumerator *enumerator,\n \tif (!media->acquire())\n \t\treturn nullptr;\n \n-\tmediaDevices_.push_back(media);\n+\tcameraDevices_.push_back(media);\n \n \treturn media.get();\n }\n \n+/**\n+ * \\brief Search and acquire a USBDevice matching a device pattern\n+ * \\param[in] enumerator Enumerator containing all media devices in the system\n+ * \\param[in] dm Device match pattern\n+ *\n+ * Search the device \\a enumerator for an available USB device matching the\n+ * device match pattern \\a dm. Matching devices that have previously been\n+ * acquired by MediaDevice::acquire() are not considered. If a match is found,\n+ * the USB device is acquired and returned. The caller shall not release the\n+ * device explicitly, it will be automatically released when the pipeline\n+ * handler is destroyed.\n+ *\n+ * \\context This function shall be called from the CameraManager thread.\n+ *\n+ * \\return A pointer to the matching USBDevice, or nullptr if no match is found\n+ */\n+USBDevice *PipelineHandler::acquireUSBDevice(DeviceEnumerator *enumerator,\n+\t\t\t\t\t     const USBDeviceMatch &dm)\n+{\n+\tstd::shared_ptr<USBDevice> usb = enumerator->search(dm);\n+\tif (!usb)\n+\t\treturn nullptr;\n+\n+\tif (!usb->acquire())\n+\t\treturn nullptr;\n+\n+\tcameraDevices_.push_back(usb);\n+\n+\treturn usb.get();\n+}\n+\n /**\n  * \\brief Acquire exclusive access to the pipeline handler for the process\n  *\n@@ -173,9 +205,9 @@ bool PipelineHandler::acquire()\n \t\treturn true;\n \t}\n \n-\tfor (std::shared_ptr<MediaDevice> &media : mediaDevices_) {\n-\t\tif (!media->lock()) {\n-\t\t\tunlockMediaDevices();\n+\tfor (std::shared_ptr<CameraDevice> &device : cameraDevices_) {\n+\t\tif (!device->lock()) {\n+\t\t\tunlockCameraDevices();\n \t\t\treturn false;\n \t\t}\n \t}\n@@ -206,7 +238,7 @@ void PipelineHandler::release(Camera *camera)\n \n \tASSERT(useCount_);\n \n-\tunlockMediaDevices();\n+\tunlockCameraDevices();\n \n \treleaseDevice(camera);\n \n@@ -224,10 +256,10 @@ void PipelineHandler::releaseDevice([[maybe_unused]] Camera *camera)\n {\n }\n \n-void PipelineHandler::unlockMediaDevices()\n+void PipelineHandler::unlockCameraDevices()\n {\n-\tfor (std::shared_ptr<MediaDevice> &media : mediaDevices_)\n-\t\tmedia->unlock();\n+\tfor (std::shared_ptr<CameraDevice> &device : cameraDevices_)\n+\t\tdevice->unlock();\n }\n \n /**\n@@ -606,7 +638,7 @@ void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera)\n {\n \tcameras_.push_back(camera);\n \n-\tif (mediaDevices_.empty())\n+\tif (cameraDevices_.empty())\n \t\tLOG(Pipeline, Fatal)\n \t\t\t<< \"Registering camera with no media devices!\";\n \n@@ -615,7 +647,20 @@ void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera)\n \t * to the camera.\n \t */\n \tstd::vector<int64_t> devnums;\n-\tfor (const std::shared_ptr<MediaDevice> &media : mediaDevices_) {\n+\tfor (const std::shared_ptr<CameraDevice> &device : cameraDevices_) {\n+\t\t/*\n+\t\t * Only MediaDevice has entities and devnums.\n+\t\t *\n+\t\t * FIXME: This code path \"abuses\" RTTI. In general, conditional\n+\t\t * execution based on type information is discouraged and is\n+\t\t * a symptom of a fragile design. However this could be\n+\t\t * considered a temporary workaround, as USB-based devices\n+\t\t * should report devnums as well in future.\n+\t\t */\n+\t\tMediaDevice *media = dynamic_cast<MediaDevice *>(device.get());\n+\t\tif (!media)\n+\t\t\tcontinue;\n+\n \t\tfor (const MediaEntity *entity : media->entities()) {\n \t\t\tif (entity->pads().size() == 1 &&\n \t\t\t    (entity->pads()[0]->flags() & MEDIA_PAD_FL_SINK) &&\n","prefixes":["libcamera-devel","RFC","8/9"]}