{"id":14647,"url":"https://patchwork.libcamera.org/api/patches/14647/?format=json","web_url":"https://patchwork.libcamera.org/patch/14647/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20211119110955.3137585-2-naush@raspberrypi.com>","date":"2021-11-19T11:09:54","name":"[libcamera-devel,2/3] pipeline: raspberrypi: Allow registration of multiple cameras","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"cdb343f54485e99db1eb143a0db3707f4d014cca","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14647/mbox/","series":[{"id":2734,"url":"https://patchwork.libcamera.org/api/series/2734/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2734","date":"2021-11-19T11:09:53","name":"[libcamera-devel,1/3] pipeline: raspberrypi: Split out device enumeration and camera registration","version":1,"mbox":"https://patchwork.libcamera.org/series/2734/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14647/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14647/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 85D1CBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 19 Nov 2021 11:10:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D849A6038C;\n\tFri, 19 Nov 2021 12:10:14 +0100 (CET)","from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com\n\t[IPv6:2a00:1450:4864:20::32d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D197E60233\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Nov 2021 12:10:12 +0100 (CET)","by mail-wm1-x32d.google.com with SMTP id 137so4600346wma.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Nov 2021 03:10:12 -0800 (PST)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:bad1:6339:b276:e376])\n\tby smtp.gmail.com with ESMTPSA id\n\to5sm2596486wrx.83.2021.11.19.03.10.10\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 19 Nov 2021 03:10:11 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"Z4a8NX8V\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=lg6hLkZF5C4o5pvhsXfDsmOyV/0MVmZT0N0mZ4xVufk=;\n\tb=Z4a8NX8VZthVpYNo+myqDiXH9sijSiTZOb2+nRCFQEe5ugYwVbpXYPgAAQZrZlXmC7\n\tT6HliBUz29UKEUw855V37q8VuC9HbOYroWUahcrG64A0u7LCrbPhu3e4nLI3LO9yAhW/\n\twAEAHYhZm5WefUJv56bsn0yQGFSs9WEJUBynqamPMeg481V9+ni13MmlMrRQeU0KIVdh\n\t7AxFgvjt/XRZMEEmw5XvzniGtmlzD11mGlUloCml/kM6ika+jvkEQycsk9uZFlxXRvR0\n\tVVtUxCarp2Q0xPyk9BH+2zqPA+u6bYbOEdd8w92JeQ2ZZQduFIQn52rDa1Q3wOen7/Ld\n\t0Y5w==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=lg6hLkZF5C4o5pvhsXfDsmOyV/0MVmZT0N0mZ4xVufk=;\n\tb=qoufVv0LokM817HftAjIsUuZdKhGZkvV59MacC66ECkn/usvZIARxSg6LM29Tneen3\n\trgRubDjaR//umtoBCz//NcVdUgLxUxZ9k5RGchIZ//AbdOCd9OwIgKN+lLhEsvHxsP7r\n\t64MidzMfLJegDHK8zu6IjvxVx8BToDt8SvkruHYev6NWHS5SZWwXB4SA7M6QCj3r6mho\n\tZNTQQE2EuD4Tyr3XLSfY99L92KQGsZ3rcG/6aruePJClbnHvwzMAe9NR0NkCru80/851\n\tbwl5E5UrefcsA1/W/DqnG6Gbgk5QLGUlYdGSYdGmACY7qc4CWV7p+R0Dali6yzKoiwqA\n\ttA2A==","X-Gm-Message-State":"AOAM5315mcL36ZXQoe2V8tapkJrg2NCpqNcqr9qSs8dwdOqIu3Y2t3kC\n\tGmT3UwaDaNbj5687CW+uKuB/HW8sBaB4Z9ew","X-Google-Smtp-Source":"ABdhPJzDowZtO5edeIYqLrjYTjFXT4iB6VU58vugf4CYoGUHCEEavIXhF22Sww1fr0x/JO/J4RNOgg==","X-Received":"by 2002:a05:600c:35c8:: with SMTP id\n\tr8mr5891314wmq.8.1637320212061; \n\tFri, 19 Nov 2021 03:10:12 -0800 (PST)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 19 Nov 2021 11:09:54 +0000","Message-Id":"<20211119110955.3137585-2-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20211119110955.3137585-1-naush@raspberrypi.com>","References":"<20211119110955.3137585-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 2/3] pipeline: raspberrypi: Allow\n\tregistration of multiple cameras","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>"},"content":"Expand the pipeline handler camera registration to correctly handle multiple\ncameras attached to the platform. For example, Raspberry Pi Compute Module\nplatforms have two camera connectors, and this change would allow the user to\nselect either of the two cameras to run.\n\nThere are associated kernel driver changes for both Unicam and the ISP needed\nto correctly advertise multiple media devices and nodes for multi-camera usage:\n\nhttps://github.com/raspberrypi/linux/pull/4140\nhttps://github.com/raspberrypi/linux/pull/4709\n\nHowever, this change is backward compatible with kernel builds that do not have\nthese changes for standard single camera usage.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 112 ++++++++++++------\n 1 file changed, 77 insertions(+), 35 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 9aa7e9eef5e7..8ed2ebcaafe7 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -311,7 +311,8 @@ private:\n \t\treturn static_cast<RPiCameraData *>(camera->_d());\n \t}\n \n-\tbool registerCameras();\n+\tint registerCameras(MediaDevice *unicam, MediaDevice *isp,\n+\t\t\t    const std::string &unicamIdStr, const std::string &ispIdStr);\n \tint queueAllBuffers(Camera *camera);\n \tint prepareBuffers(Camera *camera);\n \tvoid freeBuffers(Camera *camera);\n@@ -993,49 +994,87 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n \n bool PipelineHandlerRPi::match(DeviceEnumerator *enumerator)\n {\n-\tDeviceMatch unicam(\"unicam\");\n-\tDeviceMatch isp(\"bcm2835-isp\");\n+\tunsigned int numCameras = 0;\n \n-\tunicam.add(\"unicam-image\");\n+\t/*\n+\t * String of indexes to append to the entity names when searching for\n+\t * the Unican media devices. The first string is empty (un-indexed) to\n+\t * to maintain backward compatability with old versions of the Unicam\n+\t * kernel driver that did not advertise instance indexes.\n+\t */\n+\tfor (const std::string &unicamIdStr : { \"\", \"0\", \"1\" }) {\n+\t\tMediaDevice *unicamDevice;\n \n-\tisp.add(\"bcm2835-isp0-output0\"); /* Input */\n-\tisp.add(\"bcm2835-isp0-capture1\"); /* Output 0 */\n-\tisp.add(\"bcm2835-isp0-capture2\"); /* Output 1 */\n-\tisp.add(\"bcm2835-isp0-capture3\"); /* Stats */\n+\t\tDeviceMatch unicam(\"unicam\");\n+\t\tunicam.add(\"unicam\" + unicamIdStr + \"-image\");\n+\t\tunicamDevice = acquireMediaDevice(enumerator, unicam);\n \n-\tunicam_ = acquireMediaDevice(enumerator, unicam);\n-\tif (!unicam_)\n-\t\treturn false;\n+\t\tif (!unicamDevice)\n+\t\t\tcontinue;\n \n-\tisp_ = acquireMediaDevice(enumerator, isp);\n-\tif (!isp_)\n-\t\treturn false;\n+\t\tfor (const std::string &ispIdStr : { \"0\", \"1\" }) {\n+\t\t\tMediaDevice *ispDevice;\n+\t\t\tint ret;\n+\n+\t\t\tDeviceMatch isp(\"bcm2835-isp\");\n+\t\t\tisp.add(\"bcm2835-isp\" + ispIdStr + \"-output0\"); /* Input */\n+\t\t\tisp.add(\"bcm2835-isp\" + ispIdStr + \"-capture1\"); /* Output 0 */\n+\t\t\tisp.add(\"bcm2835-isp\" + ispIdStr + \"-capture2\"); /* Output 0 */\n+\t\t\tisp.add(\"bcm2835-isp\" + ispIdStr + \"-capture3\"); /* Stats */\n+\t\t\tispDevice = acquireMediaDevice(enumerator, isp);\n+\n+\t\t\tif (!ispDevice)\n+\t\t\t\tcontinue;\n+\n+\t\t\tret = registerCameras(unicamDevice, ispDevice, unicamIdStr, ispIdStr);\n+\t\t\tif (ret) {\n+\t\t\t\tLOG(RPI, Error) << \"Failed to register camera: \" << ret;\n+\t\t\t\tispDevice->release();\n+\t\t\t\tunicamDevice->release();\n+\t\t\t} else\n+\t\t\t\tnumCameras++;\n \n-\treturn registerCameras();\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\treturn !!numCameras;\n }\n \n-bool PipelineHandlerRPi::registerCameras()\n+int PipelineHandlerRPi::registerCameras(MediaDevice *unicam, MediaDevice *isp,\n+\t\t\t\t\tconst std::string &unicamIdStr,\n+\t\t\t\t\tconst std::string &ispIdStr)\n {\n \tstd::unique_ptr<RPiCameraData> data = std::make_unique<RPiCameraData>(this);\n+\n \tif (!data->dmaHeap_.isValid())\n-\t\treturn false;\n+\t\treturn -ENOMEM;\n+\n+\tMediaEntity *unicamImage = unicam->getEntityByName(\"unicam\" + unicamIdStr + \"-image\");\n+\tMediaEntity *ispOutput0 = isp->getEntityByName(\"bcm2835-isp\" + ispIdStr + \"-output0\");\n+\tMediaEntity *ispCapture1 = isp->getEntityByName(\"bcm2835-isp\" + ispIdStr + \"-capture1\");\n+\tMediaEntity *ispCapture2 = isp->getEntityByName(\"bcm2835-isp\" + ispIdStr + \"-capture2\");\n+\tMediaEntity *ispCapture3 = isp->getEntityByName(\"bcm2835-isp\" + ispIdStr + \"-capture3\");\n+\n+\tif (!unicamImage || !ispOutput0 || !ispCapture1 || !ispCapture2 || !ispCapture3)\n+\t\treturn -ENOENT;\n \n \t/* Locate and open the unicam video streams. */\n-\tdata->unicam_[Unicam::Image] = RPi::Stream(\"Unicam Image\", unicam_->getEntityByName(\"unicam-image\"));\n+\tdata->unicam_[Unicam::Image] = RPi::Stream(\"Unicam Image\", unicamImage);\n \n \t/* An embedded data node will not be present if the sensor does not support it. */\n-\tMediaEntity *embeddedEntity = unicam_->getEntityByName(\"unicam-embedded\");\n-\tif (embeddedEntity) {\n-\t\tdata->unicam_[Unicam::Embedded] = RPi::Stream(\"Unicam Embedded\", embeddedEntity);\n+\tMediaEntity *unicamEmbedded = unicam->getEntityByName(\"unicam\" + unicamIdStr + \"-embedded\");\n+\tif (unicamEmbedded) {\n+\t\tdata->unicam_[Unicam::Embedded] = RPi::Stream(\"Unicam Embedded\", unicamEmbedded);\n \t\tdata->unicam_[Unicam::Embedded].dev()->bufferReady.connect(data.get(),\n \t\t\t\t\t\t\t\t\t   &RPiCameraData::unicamBufferDequeue);\n \t}\n \n \t/* Tag the ISP input stream as an import stream. */\n-\tdata->isp_[Isp::Input] = RPi::Stream(\"ISP Input\", isp_->getEntityByName(\"bcm2835-isp0-output0\"), true);\n-\tdata->isp_[Isp::Output0] = RPi::Stream(\"ISP Output0\", isp_->getEntityByName(\"bcm2835-isp0-capture1\"));\n-\tdata->isp_[Isp::Output1] = RPi::Stream(\"ISP Output1\", isp_->getEntityByName(\"bcm2835-isp0-capture2\"));\n-\tdata->isp_[Isp::Stats] = RPi::Stream(\"ISP Stats\", isp_->getEntityByName(\"bcm2835-isp0-capture3\"));\n+\tdata->isp_[Isp::Input] = RPi::Stream(\"ISP Input\", ispOutput0, true);\n+\tdata->isp_[Isp::Output0] = RPi::Stream(\"ISP Output0\", ispCapture1);\n+\tdata->isp_[Isp::Output1] = RPi::Stream(\"ISP Output1\", ispCapture2);\n+\tdata->isp_[Isp::Stats] = RPi::Stream(\"ISP Stats\", ispCapture3);\n \n \t/* Wire up all the buffer connections. */\n \tdata->unicam_[Unicam::Image].dev()->frameStart.connect(data.get(), &RPiCameraData::frameStarted);\n@@ -1046,7 +1085,7 @@ bool PipelineHandlerRPi::registerCameras()\n \tdata->isp_[Isp::Stats].dev()->bufferReady.connect(data.get(), &RPiCameraData::ispOutputDequeue);\n \n \t/* Identify the sensor. */\n-\tfor (MediaEntity *entity : unicam_->entities()) {\n+\tfor (MediaEntity *entity : unicam->entities()) {\n \t\tif (entity->function() == MEDIA_ENT_F_CAM_SENSOR) {\n \t\t\tdata->sensor_ = std::make_unique<CameraSensor>(entity);\n \t\t\tbreak;\n@@ -1054,23 +1093,23 @@ bool PipelineHandlerRPi::registerCameras()\n \t}\n \n \tif (!data->sensor_)\n-\t\treturn false;\n+\t\treturn -EINVAL;\n \n \tif (data->sensor_->init())\n-\t\treturn false;\n+\t\treturn -EINVAL;\n \n \tdata->sensorFormats_ = populateSensorFormats(data->sensor_);\n \n \tipa::RPi::SensorConfig sensorConfig;\n \tif (data->loadIPA(&sensorConfig)) {\n \t\tLOG(RPI, Error) << \"Failed to load a suitable IPA library\";\n-\t\treturn false;\n+\t\treturn -EINVAL;\n \t}\n \n-\tif (sensorConfig.sensorMetadata ^ !!embeddedEntity) {\n+\tif (sensorConfig.sensorMetadata ^ !!unicamEmbedded) {\n \t\tLOG(RPI, Warning) << \"Mismatch between Unicam and CamHelper for embedded data usage!\";\n \t\tsensorConfig.sensorMetadata = false;\n-\t\tif (embeddedEntity)\n+\t\tif (unicamEmbedded)\n \t\t\tdata->unicam_[Unicam::Embedded].dev()->bufferReady.disconnect();\n \t}\n \n@@ -1091,12 +1130,12 @@ bool PipelineHandlerRPi::registerCameras()\n \n \tfor (auto stream : data->streams_) {\n \t\tif (stream->dev()->open())\n-\t\t\treturn false;\n+\t\t\tcontinue;\n \t}\n \n \tif (!data->unicam_[Unicam::Image].dev()->caps().hasMediaController()) {\n \t\tLOG(RPI, Error) << \"Unicam driver does not use the MediaController, please update your kernel!\";\n-\t\treturn false;\n+\t\treturn -EINVAL;\n \t}\n \n \t/*\n@@ -1158,7 +1197,7 @@ bool PipelineHandlerRPi::registerCameras()\n \n \tif (!bayerFormat.isValid()) {\n \t\tLOG(RPI, Error) << \"No Bayer format found\";\n-\t\treturn false;\n+\t\treturn -EINVAL;\n \t}\n \tdata->nativeBayerOrder_ = bayerFormat.order;\n \n@@ -1178,7 +1217,10 @@ bool PipelineHandlerRPi::registerCameras()\n \t\tCamera::create(std::move(data), id, streams);\n \tregisterCamera(std::move(camera));\n \n-\treturn true;\n+\tLOG(RPI, Info) << \"Registered camera \" << id\n+\t\t       << \" to instances \\\"unicam\" << unicamIdStr << \"\\\"\"\n+\t\t       << \" and \\\"isp\" << ispIdStr << \"\\\"\";\n+\treturn 0;\n }\n \n int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n","prefixes":["libcamera-devel","2/3"]}