From patchwork Sat Jun 6 08:07:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 3960 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 520E9600F7 for ; Sat, 6 Jun 2020 10:07:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VBoiKfAn"; dkim-atps=neutral Received: from emerald.amanokami.net (fs76eef344.knge213.ap.nuro.jp [118.238.243.68]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 46F9527C; Sat, 6 Jun 2020 10:07:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1591430845; bh=+tZvKFCwU9bVV3iE3ct0WybHyk8MjCudcq86kSJesfI=; h=From:To:Cc:Subject:Date:From; b=VBoiKfAncHEmcrkyHVWslHPgOyIYLwwqRx5ohNEoQbCfKCZPDqGLmq0gNZpH+T+2b YE2ydRpsQoBCChdXGM4Bd+z5bjxKFyDB3DW2pTgDexwoGFl2WM86s4ScPRxaSO1FiB 6WCjmeKLxrUcInbX4EN9rBGIMga/F2400gKYjscY= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Sat, 6 Jun 2020 17:07:06 +0900 Message-Id: <20200606080706.14290-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2] libcamera: CameraManager, PipelineHandler: Automatically map devnums to Camera 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: , X-List-Received-Date: Sat, 06 Jun 2020 08:07:26 -0000 The V4L2 compatibility layer uses devnum to match video device nodes to libcamera Cameras. Some pipeline handlers don't report a devnum for their camera, which prevents the V4L2 compatibility layer from matching video device nodes to these cameras. To fix this, we first allow the camera manager to map multiple devnums to a camera. Next, we walk the media device and entity list and tell the camera manager to map every one of these devnums that is a video capture node to the camera. Since we decided that all video capture nodes that belong to a camera can be opened via the V4L2 compatibility layer to map to that camera, it would cause confusion for users if some pipeline handlers decided that only specific device nodes would map to the camera. To prevent this confusion, remove the ability for pipeline handlers to declare their own devnum-to-camera mapping. The only pipeline handler that declares the devnum mapping is the UVC pipeline handler, so remove the devnum there. We considered walking the media entity list and taking the devnum from just the one with the default flag set, but we found that some drivers (eg. vimc) don't set this flag for any entity. Instead, we take all the video capture nodes (entities with the sink pad flag set). Signed-off-by: Paul Elder Reviewed-by: Niklas Söderlund --- Changes in v2: - pipeline handlers can no longer declare their own devnum mapping. - remove the uvc pipeline handler devnum mapping - cosmetic changes --- include/libcamera/camera_manager.h | 3 ++- include/libcamera/internal/pipeline_handler.h | 2 +- src/libcamera/camera_manager.cpp | 17 ++++++------- src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 4 +--- src/libcamera/pipeline_handler.cpp | 24 ++++++++++++------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h index 079f848a..95dc6360 100644 --- a/include/libcamera/camera_manager.h +++ b/include/libcamera/camera_manager.h @@ -34,7 +34,8 @@ public: std::shared_ptr get(const std::string &name); std::shared_ptr get(dev_t devnum); - void addCamera(std::shared_ptr camera, dev_t devnum); + void addCamera(std::shared_ptr camera, + const std::vector &devnums); void removeCamera(Camera *camera); static const std::string &version() { return version_; } diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h index 56968d14..22e629a8 100644 --- a/include/libcamera/internal/pipeline_handler.h +++ b/include/libcamera/internal/pipeline_handler.h @@ -91,7 +91,7 @@ public: protected: void registerCamera(std::shared_ptr camera, - std::unique_ptr data, dev_t devnum = 0); + std::unique_ptr data); void hotplugMediaDevice(MediaDevice *media); virtual int queueRequestDevice(Camera *camera, Request *request) = 0; diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index da806fa7..3d055f78 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -36,7 +36,8 @@ public: Private(CameraManager *cm); int start(); - void addCamera(std::shared_ptr &camera, dev_t devnum); + void addCamera(std::shared_ptr &camera, + const std::vector &devnums); void removeCamera(Camera *camera); /* @@ -168,7 +169,7 @@ void CameraManager::Private::cleanup() } void CameraManager::Private::addCamera(std::shared_ptr &camera, - dev_t devnum) + const std::vector &devnums) { MutexLocker locker(mutex_); @@ -183,10 +184,9 @@ void CameraManager::Private::addCamera(std::shared_ptr &camera, cameras_.push_back(std::move(camera)); - if (devnum) { - unsigned int index = cameras_.size() - 1; + unsigned int index = cameras_.size() - 1; + for (dev_t devnum : devnums) camerasByDevnum_[devnum] = cameras_[index]; - } } void CameraManager::Private::removeCamera(Camera *camera) @@ -374,7 +374,7 @@ std::shared_ptr CameraManager::get(dev_t devnum) /** * \brief Add a camera to the camera manager * \param[in] camera The camera to be added - * \param[in] devnum The device number to associate with \a camera + * \param[in] devnums The device numbers to associate with \a camera * * This function is called by pipeline handlers to register the cameras they * handle with the camera manager. Registered cameras are immediately made @@ -385,11 +385,12 @@ std::shared_ptr CameraManager::get(dev_t devnum) * * \context This function shall be called from the CameraManager thread. */ -void CameraManager::addCamera(std::shared_ptr camera, dev_t devnum) +void CameraManager::addCamera(std::shared_ptr camera, + const std::vector &devnums) { ASSERT(Thread::current() == p_.get()); - p_->addCamera(camera, devnum); + p_->addCamera(camera, devnums); } /** diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp index a0749094..396bbe9b 100644 --- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp +++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp @@ -396,12 +396,10 @@ bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator) if (data->init(*entity)) return false; - dev_t devnum = makedev((*entity)->deviceMajor(), (*entity)->deviceMinor()); - /* Create and register the camera. */ std::set streams{ &data->stream_ }; std::shared_ptr camera = Camera::create(this, media->model(), streams); - registerCamera(std::move(camera), std::move(data), devnum); + registerCamera(std::move(camera), std::move(data)); /* Enable hot-unplug notifications. */ hotplugMediaDevice(media); diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 53aeebdc..125e1779 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -481,28 +481,34 @@ void PipelineHandler::completeRequest(Camera *camera, Request *request) * \brief Register a camera to the camera manager and pipeline handler * \param[in] camera The camera to be added * \param[in] data Pipeline-specific data for the camera - * \param[in] devnum Device number of the camera (optional) * * This method is called by pipeline handlers to register the cameras they * handle with the camera manager. It associates the pipeline-specific \a data * with the camera, for later retrieval with cameraData(). Ownership of \a data * is transferred to the PipelineHandler. * - * \a devnum is the device number (as returned by makedev) that the \a camera - * is to be associated with. This is for the V4L2 compatibility layer to map - * device nodes to Camera instances based on the device number - * registered by this method in \a devnum. - * * \context This function shall be called from the CameraManager thread. */ void PipelineHandler::registerCamera(std::shared_ptr camera, - std::unique_ptr data, - dev_t devnum) + std::unique_ptr data) { data->camera_ = camera.get(); cameraData_[camera.get()] = std::move(data); cameras_.push_back(camera); - manager_->addCamera(std::move(camera), devnum); + + /* + * Walk the entity list and map the devnums of all capture video nodes + * to the camera. + */ + std::vector devnums; + for (const std::shared_ptr media : mediaDevices_) + for (const MediaEntity *entity : media->entities()) + if (entity->pads().size() == 1 && + (entity->pads()[0]->flags() & MEDIA_PAD_FL_SINK)) + devnums.push_back(makedev(entity->deviceMajor(), + entity->deviceMinor())); + + manager_->addCamera(std::move(camera), devnums); } /**