From patchwork Thu Dec 9 13:32:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 15101 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 EFC70BF415 for ; Thu, 9 Dec 2021 13:33:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B06B060890; Thu, 9 Dec 2021 14:32:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="cnQ0RPb8"; dkim-atps=neutral Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 235CD6086A for ; Thu, 9 Dec 2021 14:32:57 +0100 (CET) Received: by mail-wm1-x32f.google.com with SMTP id o19-20020a1c7513000000b0033a93202467so4088365wmc.2 for ; Thu, 09 Dec 2021 05:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nJnkmgQAsgdErldvbjoJq1Zg/UvxAKQrj2jHEwu8eT0=; b=cnQ0RPb8In0cnqTpOntQS1pQV1d8GjZiWD/654Siwn325l+MfKeqsQiGvsk3imu8IE CghP+yWJwrfUEGMuGf8Oor20VEYx02LEcUU8zOpMFY3cUEaXGpkdxJRQPAyegNDPs2b2 qKwYn2jELjFjDtq7qM0E8CxvaW7stuQoBpZzwSsulmnsMrTui0zmQBM+thL5Pu7v37bg NWA/YzK5M7ybLPQFbiswIGHrSsPBPk9qXWEd5ah+P1qrBiIj2bZWH8mg/4eRRFALR439 qaTktJ462K6Nu+vghk4yWiinAd4xQkyxRbbKA02O33vNXLujhsZVNt2kXb7R+tFziD0+ 8Qaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nJnkmgQAsgdErldvbjoJq1Zg/UvxAKQrj2jHEwu8eT0=; b=51wNWr+fjgF1099E6Ek83ojdQBUh11fciaAQzcdcAA6a6fpE2/Ck/CiqMaUalFciNv BgIGyEBknWwQUe7WDIdaNEcZnc6UvTRZiI+75FrwubdxRHWttdCb8upBl3QmhjhT0IJj WiZgApjtx4W1t4SxjUYIW6zNBO0toGleEAbB8z1VWNnYdvRf0LAKVVVv+Kr44svQNVaj JWUYF6RGfnfl6gIzsDH+Au+v0lkvhV3LCEBlUQdiPBnbW9xeuKDAEeguU0SadNxrHFS4 ThwCVzG6i9SKpnJYv6CfCrxF12IO7viunR+7UMEyIJbsEoDx+VXPfetg6BaJvWLIImf9 tt2Q== X-Gm-Message-State: AOAM531psP+oRx3nDfR1e3aTMTIqYKMOeQWYZjiO4E5g+REySYTu7djE UmDaNHzfVdAKogvasSV/Md2yEggOppLvJXgP X-Google-Smtp-Source: ABdhPJxW2pc3ItQvRGfUN1u8Mo5OL14lwNo8GMNnrQYooZxZRJR05UVCQ3vdnekQtTNb/97P82meYQ== X-Received: by 2002:a05:600c:3658:: with SMTP id y24mr7242621wmq.161.1639056776530; Thu, 09 Dec 2021 05:32:56 -0800 (PST) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:a3bd:8799:8317:6d3f]) by smtp.gmail.com with ESMTPSA id r15sm8449244wmh.13.2021.12.09.05.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Dec 2021 05:32:56 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Thu, 9 Dec 2021 13:32:51 +0000 Message-Id: <20211209133252.2234668-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211209133252.2234668-1-naush@raspberrypi.com> References: <20211209133252.2234668-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 1/2] pipeline: raspberrypi: Move sensor entity detection out of registerCamera() 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" Enumerate the sensor device entities in PipelineHandlerRPi::match() and loop over PipelineHandlerRPi::registerCamera() for each sensor found. This will allow the pipeline handler to register multiple cameras attached to a single Unicam instance with a Video Mux device. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham --- .../pipeline/raspberrypi/raspberrypi.cpp | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 86851ac467ad..2a2fb5273eb8 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -310,7 +310,7 @@ private: return static_cast(camera->_d()); } - int registerCamera(MediaDevice *unicam, MediaDevice *isp); + int registerCamera(MediaDevice *unicam, MediaDevice *isp, MediaEntity *sensorEntity); int queueAllBuffers(Camera *camera); int prepareBuffers(Camera *camera); void freeBuffers(Camera *camera); @@ -1029,16 +1029,28 @@ bool PipelineHandlerRPi::match(DeviceEnumerator *enumerator) return false; } - int ret = registerCamera(unicamDevice, ispDevice); - if (ret) { - LOG(RPI, Error) << "Failed to register camera: " << ret; - return false; + /* + * The loop below is used to register multiple cameras behind one or more + * video mux devices that are attached to a particular Unicam instance. + * Obviously these cameras cannot be used simultaneously. + */ + unsigned int numCameras = 0; + for (MediaEntity *entity : unicamDevice->entities()) { + if (entity->function() != MEDIA_ENT_F_CAM_SENSOR) + continue; + + int ret = registerCamera(unicamDevice, ispDevice, entity); + if (ret) + LOG(RPI, Error) << "Failed to register camera " + << entity->name() << ": " << ret; + else + numCameras++; } - return true; + return !!numCameras; } -int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp) +int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, MediaEntity *sensorEntity) { std::unique_ptr data = std::make_unique(this); @@ -1079,14 +1091,7 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp) data->isp_[Isp::Output1].dev()->bufferReady.connect(data.get(), &RPiCameraData::ispOutputDequeue); data->isp_[Isp::Stats].dev()->bufferReady.connect(data.get(), &RPiCameraData::ispOutputDequeue); - /* Identify the sensor. */ - for (MediaEntity *entity : unicam->entities()) { - if (entity->function() == MEDIA_ENT_F_CAM_SENSOR) { - data->sensor_ = std::make_unique(entity); - break; - } - } - + data->sensor_ = std::make_unique(sensorEntity); if (!data->sensor_) return -EINVAL; From patchwork Thu Dec 9 13:32:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 15102 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 C91DCC324B for ; Thu, 9 Dec 2021 13:33:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C9A9D6087E; Thu, 9 Dec 2021 14:33:00 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="Y0gXpZ+k"; dkim-atps=neutral Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DEAC460876 for ; Thu, 9 Dec 2021 14:32:57 +0100 (CET) Received: by mail-wm1-x32e.google.com with SMTP id i8-20020a7bc948000000b0030db7b70b6bso6571241wml.1 for ; Thu, 09 Dec 2021 05:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=w2OtKuHX6CYbNhGvo4cGz+74cdbg5QRxPILGyFh9P+g=; b=Y0gXpZ+kxnuI2DSCOxmsFWFL6c/eQrDeSxprWW6GFj8rljRGj2Cf2wc7hsBlqITEJ1 gtugQcpgIDX2lIYDpmnyfqwutZ+cSWvU2PIRBgqyghVBK2YDimFFDrbR7e+UGF/ckb6n 1fshAqV0/kxuE7rYNMFIJz+ce0NYFKr4mnbdsS2NL+VJK13eSlWY7k8x81mZbjvRKuFK jCvrTQxPOQlXOkeiknwJDqgOLkRapnqD9OpHIS4ruD4l9b/tRKCrC4fTyMBgdvuiLtRk Gsj2S4cXPfJk3R4b9bgv5B7jvqnAOuaxAyl5hpt718LY9h6z5GQTuOFAbw7xW3mG3STZ jCsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=w2OtKuHX6CYbNhGvo4cGz+74cdbg5QRxPILGyFh9P+g=; b=wfH9n3xT5fdOY/x4IIi+qsEw3ziRpxE/g66LzR2dU3dqnH4BLWD/SUI22OOwI+bxGk Dn8KwT68V8kfvDfTsq6IMTomNadVdZvHW9BDZZDCwz3QBZUE+zzkdNOWWcI3MmMNYsgA zdzMpDxfJ19e4H/+MXi559eVioNCMdwgluer2gjQhJ0ol1tmPrfg6ebn/57OW3s36RFM 2psMW6AocSDdcKa1RAXwf1lZ8jrs2SV/devcTFCPsnaZwnn6FMrzVgVcb7xsNtUYhhAT bXdbwJoWWKmdNe8hRMDJQxunNz7Yek7Hz6ZXrmbdutdOnpmZhwmcnnGFRgsE4D4GsqwN E7Hg== X-Gm-Message-State: AOAM531lhLr7bB8XQNVuywqTFvq5fefk4ZD+vN4hBEYZzEPTWjiE7xgH 1h6dcg9/D18FyTS4d/vltpvjTyn3F2nonET7 X-Google-Smtp-Source: ABdhPJy8cbLo7bcYn68RiJRZEj9sZ0lwVIQPC6EFCP3qftMJSo+4ZGXKDJs9iVIZ5eGAg+LtP+x2rw== X-Received: by 2002:a05:600c:4e07:: with SMTP id b7mr6966524wmq.16.1639056777255; Thu, 09 Dec 2021 05:32:57 -0800 (PST) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:a3bd:8799:8317:6d3f]) by smtp.gmail.com with ESMTPSA id r15sm8449244wmh.13.2021.12.09.05.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Dec 2021 05:32:56 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Thu, 9 Dec 2021 13:32:52 +0000 Message-Id: <20211209133252.2234668-3-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211209133252.2234668-1-naush@raspberrypi.com> References: <20211209133252.2234668-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 2/2] pipeline: raspberrypi: Add support for Video Mux and Bridge devices 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" This change will allow the pipeline handler to enumerate and control Video Mux or Bridge devices that may be attached between sensors and a particular Unicam instance. Cascaded mux or bridge devices are also handled. A new member function enumerateVideoDevices(), called from registerCamera(), is used to identify and open all mux and bridge subdevices present in the sensor -> Unicam link. Relevent links are enabled/disabled and pad formats correctly set in configure() before the camera is started. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham --- .../pipeline/raspberrypi/raspberrypi.cpp | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 2a2fb5273eb8..49dbefd8637c 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -220,6 +221,11 @@ public: std::vector streams_; /* Stores the ids of the buffers mapped in the IPA. */ std::unordered_set ipaBuffers_; + /* + * Stores a cascade of Video Mux or Bridge devices between the sensor and + * Unicam together with media link across the entities. + */ + std::vector, MediaLink *>> bridgeDevices_; /* DMAHEAP allocation helper. */ RPi::DmaHeap dmaHeap_; @@ -311,6 +317,7 @@ private: } int registerCamera(MediaDevice *unicam, MediaDevice *isp, MediaEntity *sensorEntity); + void enumerateVideoDevices(RPiCameraData *data, MediaLink *link); int queueAllBuffers(Camera *camera); int prepareBuffers(Camera *camera); void freeBuffers(Camera *camera); @@ -868,6 +875,33 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) */ data->properties_.set(properties::ScalerCropMaximum, data->sensorInfo_.analogCrop); + /* Setup the Video Mux/Bridge entities. */ + for (auto &[device, link] : data->bridgeDevices_) { + /* Start by disabling all the sink pad links on the devices in the cascade. */ + for (const MediaPad *p : device->entity()->pads()) { + if (!(p->flags() & MEDIA_PAD_FL_SINK)) + continue; + + for (MediaLink *l : p->links()) + l->setEnabled(false); + } + + /* Next, enable the entity -> entity links, and setup the pad format. */ + link->setEnabled(true); + const MediaPad *srcPad = link->sink(); + ret = device->setFormat(srcPad->index(), &sensorFormat); + if (ret) { + LOG(RPI, Error) << "Failed to set format on " << device->entity()->name() + << " pad " << srcPad->index() + << " with format " << format.toString() + << ": " << ret; + return ret; + } + + LOG(RPI, Info) << "Configured media link on device " << device->entity()->name() + << " on pad " << srcPad->index(); + } + return ret; } @@ -1098,6 +1132,13 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, Me if (data->sensor_->init()) return -EINVAL; + /* + * Enumerate all the Video Mux/Bridge devices across the sensor -> unicam + * link. There may be a cascade of devices in this link! + */ + MediaLink *link = sensorEntity->getPadByIndex(0)->links()[0]; + enumerateVideoDevices(data.get(), link); + data->sensorFormats_ = populateSensorFormats(data->sensor_); ipa::RPi::SensorConfig sensorConfig; @@ -1224,6 +1265,84 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, Me return 0; } +/* + * enumerateVideoDevices() iterates over the Media Controller topology, starting + * at the sensor and finishing at Unicam. For each sensor, RPiCameraData stores + * a unique list of any intermediate video mux or bridge devices connected in a + * cascade, together with the entity to entity link. + * + * Entity pad configuration and link enabling happens at the end of configure(). + * We first disables all pad links on each entity device in the chain, and then + * selectively enabling the specific links to link sensor to Unicam across all + * intermediate muxes and bridges. + * + * In the cascaded topology below, if Sensor1 is used, the Mux2 -> Mux1 link + * will be disabled, and Sensor1 -> Mux1 -> Unicam links enabled. Alternatively, + * if Sensor3 is used, the Sensor2 -> Mux2 and Sensor1 -> Mux1 links are disabled, + * and Sensor3 -> Mux2 -> Mux1 -> Unicam links are enabled. All other links will + * remain unchanged. + * + * +----------+ + * | Unicam | + * +-----^----+ + * | + * +---+---+ + * | Mux1 <-------+ + * +--^----+ | + * | | + * +-----+---+ +---+---+ + * | Sensor1 | | Mux2 |<--+ + * +---------+ +-^-----+ | + * | | + * +-------+-+ +---+-----+ + * | Sensor2 | | Sensor3 | + * +---------+ +---------+ + */ +void PipelineHandlerRPi::enumerateVideoDevices(RPiCameraData *data, MediaLink *link) +{ + const MediaPad *sinkPad = link->sink(); + const MediaEntity *entity = sinkPad->entity(); + bool unicamFound = false; + + /* We only deal with Video Mux and Bridge devices in cascade. */ + if (entity->function() != MEDIA_ENT_F_VID_MUX && + entity->function() != MEDIA_ENT_F_VID_IF_BRIDGE) + return; + + LOG(RPI, Info) << "Found video mux device " << entity->name() + << " linked to sink pad " << sinkPad->index(); + + data->bridgeDevices_.emplace_back(std::make_unique(entity), link); + data->bridgeDevices_.back().first->open(); + + for (const MediaPad *pad : entity->pads()) { + /* + * Iterate through all the sink pads down the cascade to find any + * other Video Mux and Bridge devices. + */ + if (!(pad->flags() & MEDIA_PAD_FL_SOURCE)) + continue; + + for (MediaLink *l : pad->links()) { + enumerateVideoDevices(data, l); + if (l->sink()->entity()->name() == "unicam-image") + unicamFound = true; + } + } + + /* This identifies the end of our entity enumeration recursion. */ + if (link->source()->entity()->function() == MEDIA_ENT_F_CAM_SENSOR) { + /* + * If Unicam is not at the end of this cascade, we cannot configure + * this topology automatically, so remove all entity references. + */ + if (!unicamFound) { + LOG(RPI, Warning) << "Cannot automatically configure this MC topology!"; + data->bridgeDevices_.clear(); + } + } +} + int PipelineHandlerRPi::queueAllBuffers(Camera *camera) { RPiCameraData *data = cameraData(camera);