[{"id":5131,"web_url":"https://patchwork.libcamera.org/comment/5131/","msgid":"<20200608141005.GE5896@pendragon.ideasonboard.com>","date":"2020-06-08T14:10:05","subject":"Re: [libcamera-devel] [PATCH v3] libcamera: CameraManager,\n\tPipelineHandler: Automatically map devnums to Camera","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Mon, Jun 08, 2020 at 04:32:53PM +0900, Paul Elder wrote:\n> The V4L2 compatibility layer uses devnum to match video device nodes to\n> libcamera Cameras. Some pipeline handlers don't report a devnum for\n> their camera, which prevents the V4L2 compatibility layer from matching\n> video device nodes to these cameras. To fix this, we first allow the\n> camera manager to map multiple devnums to a camera. Next, we walk the\n> media device and entity list and tell the camera manager to map every\n> one of these devnums that is a video capture node to the camera.\n> \n> Since we decided that all video capture nodes that belong to a camera\n> can be opened via the V4L2 compatibility layer to map to that camera, it\n> would cause confusion for users if some pipeline handlers decided that\n> only specific device nodes would map to the camera. To prevent this\n> confusion, remove the ability for pipeline handlers to declare their own\n> devnum-to-camera mapping. The only pipeline handler that declares the\n> devnum mapping is the UVC pipeline handler, so remove the devnum there.\n> \n> We considered walking the media entity list and taking the devnum from\n> just the one with the default flag set, but we found that some drivers\n> (eg. vimc) don't set this flag for any entity. Instead, we take all the\n> video capture nodes (entities with the sink pad flag set).\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> \n> ---\n> Changes in v3:\n> - add match condition entity->function() == MEDIA_ENT_F_IO_V4L\n> \n> Changes in v2:\n> - pipeline handlers can no longer declare their own devnum\n>   mapping.\n>   - remove the uvc pipeline handler devnum mapping\n> - cosmetic changes\n> ---\n>  include/libcamera/camera_manager.h            |  3 +-\n>  include/libcamera/internal/pipeline_handler.h |  2 +-\n>  src/libcamera/camera_manager.cpp              | 17 +++++------\n>  src/libcamera/pipeline/uvcvideo/uvcvideo.cpp  |  5 +---\n>  src/libcamera/pipeline_handler.cpp            | 28 +++++++++++++------\n>  5 files changed, 32 insertions(+), 23 deletions(-)\n> \n> diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h\n> index 079f848a..95dc6360 100644\n> --- a/include/libcamera/camera_manager.h\n> +++ b/include/libcamera/camera_manager.h\n> @@ -34,7 +34,8 @@ public:\n>  \tstd::shared_ptr<Camera> get(const std::string &name);\n>  \tstd::shared_ptr<Camera> get(dev_t devnum);\n>  \n> -\tvoid addCamera(std::shared_ptr<Camera> camera, dev_t devnum);\n> +\tvoid addCamera(std::shared_ptr<Camera> camera,\n> +\t\t       const std::vector<dev_t> &devnums);\n>  \tvoid removeCamera(Camera *camera);\n>  \n>  \tstatic const std::string &version() { return version_; }\n> diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h\n> index 56968d14..22e629a8 100644\n> --- a/include/libcamera/internal/pipeline_handler.h\n> +++ b/include/libcamera/internal/pipeline_handler.h\n> @@ -91,7 +91,7 @@ public:\n>  \n>  protected:\n>  \tvoid registerCamera(std::shared_ptr<Camera> camera,\n> -\t\t\t    std::unique_ptr<CameraData> data, dev_t devnum = 0);\n> +\t\t\t    std::unique_ptr<CameraData> data);\n>  \tvoid hotplugMediaDevice(MediaDevice *media);\n>  \n>  \tvirtual int queueRequestDevice(Camera *camera, Request *request) = 0;\n> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp\n> index da806fa7..3d055f78 100644\n> --- a/src/libcamera/camera_manager.cpp\n> +++ b/src/libcamera/camera_manager.cpp\n> @@ -36,7 +36,8 @@ public:\n>  \tPrivate(CameraManager *cm);\n>  \n>  \tint start();\n> -\tvoid addCamera(std::shared_ptr<Camera> &camera, dev_t devnum);\n> +\tvoid addCamera(std::shared_ptr<Camera> &camera,\n> +\t\t       const std::vector<dev_t> &devnums);\n>  \tvoid removeCamera(Camera *camera);\n>  \n>  \t/*\n> @@ -168,7 +169,7 @@ void CameraManager::Private::cleanup()\n>  }\n>  \n>  void CameraManager::Private::addCamera(std::shared_ptr<Camera> &camera,\n> -\t\t\t\t       dev_t devnum)\n> +\t\t\t\t       const std::vector<dev_t> &devnums)\n>  {\n>  \tMutexLocker locker(mutex_);\n>  \n> @@ -183,10 +184,9 @@ void CameraManager::Private::addCamera(std::shared_ptr<Camera> &camera,\n>  \n>  \tcameras_.push_back(std::move(camera));\n>  \n> -\tif (devnum) {\n> -\t\tunsigned int index = cameras_.size() - 1;\n> +\tunsigned int index = cameras_.size() - 1;\n> +\tfor (dev_t devnum : devnums)\n>  \t\tcamerasByDevnum_[devnum] = cameras_[index];\n> -\t}\n>  }\n>  \n>  void CameraManager::Private::removeCamera(Camera *camera)\n> @@ -374,7 +374,7 @@ std::shared_ptr<Camera> CameraManager::get(dev_t devnum)\n>  /**\n>   * \\brief Add a camera to the camera manager\n>   * \\param[in] camera The camera to be added\n> - * \\param[in] devnum The device number to associate with \\a camera\n> + * \\param[in] devnums The device numbers to associate with \\a camera\n>   *\n>   * This function is called by pipeline handlers to register the cameras they\n>   * handle with the camera manager. Registered cameras are immediately made\n> @@ -385,11 +385,12 @@ std::shared_ptr<Camera> CameraManager::get(dev_t devnum)\n\nThere's one other mention of devnum in the documentation,\ns/devnum/devnums/ should be enough.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>   *\n>   * \\context This function shall be called from the CameraManager thread.\n>   */\n> -void CameraManager::addCamera(std::shared_ptr<Camera> camera, dev_t devnum)\n> +void CameraManager::addCamera(std::shared_ptr<Camera> camera,\n> +\t\t\t      const std::vector<dev_t> &devnums)\n>  {\n>  \tASSERT(Thread::current() == p_.get());\n>  \n> -\tp_->addCamera(camera, devnum);\n> +\tp_->addCamera(camera, devnums);\n>  }\n>  \n>  /**\n> diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> index a0749094..80a0e77b 100644\n> --- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> +++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n> @@ -8,7 +8,6 @@\n>  #include <algorithm>\n>  #include <iomanip>\n>  #include <math.h>\n> -#include <sys/sysmacros.h>\n>  #include <tuple>\n>  \n>  #include <libcamera/camera.h>\n> @@ -396,12 +395,10 @@ bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator)\n>  \tif (data->init(*entity))\n>  \t\treturn false;\n>  \n> -\tdev_t devnum = makedev((*entity)->deviceMajor(), (*entity)->deviceMinor());\n> -\n>  \t/* Create and register the camera. */\n>  \tstd::set<Stream *> streams{ &data->stream_ };\n>  \tstd::shared_ptr<Camera> camera = Camera::create(this, media->model(), streams);\n> -\tregisterCamera(std::move(camera), std::move(data), devnum);\n> +\tregisterCamera(std::move(camera), std::move(data));\n>  \n>  \t/* Enable hot-unplug notifications. */\n>  \thotplugMediaDevice(media);\n> diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp\n> index 53aeebdc..14dc7194 100644\n> --- a/src/libcamera/pipeline_handler.cpp\n> +++ b/src/libcamera/pipeline_handler.cpp\n> @@ -481,28 +481,38 @@ void PipelineHandler::completeRequest(Camera *camera, Request *request)\n>   * \\brief Register a camera to the camera manager and pipeline handler\n>   * \\param[in] camera The camera to be added\n>   * \\param[in] data Pipeline-specific data for the camera\n> - * \\param[in] devnum Device number of the camera (optional)\n>   *\n>   * This method is called by pipeline handlers to register the cameras they\n>   * handle with the camera manager. It associates the pipeline-specific \\a data\n>   * with the camera, for later retrieval with cameraData(). Ownership of \\a data\n>   * is transferred to the PipelineHandler.\n>   *\n> - * \\a devnum is the device number (as returned by makedev) that the \\a camera\n> - * is to be associated with. This is for the V4L2 compatibility layer to map\n> - * device nodes to Camera instances based on the device number\n> - * registered by this method in \\a devnum.\n> - *\n>   * \\context This function shall be called from the CameraManager thread.\n>   */\n>  void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera,\n> -\t\t\t\t     std::unique_ptr<CameraData> data,\n> -\t\t\t\t     dev_t devnum)\n> +\t\t\t\t     std::unique_ptr<CameraData> data)\n>  {\n>  \tdata->camera_ = camera.get();\n>  \tcameraData_[camera.get()] = std::move(data);\n>  \tcameras_.push_back(camera);\n> -\tmanager_->addCamera(std::move(camera), devnum);\n> +\n> +\t/*\n> +\t * Walk the entity list and map the devnums of all capture video nodes\n> +\t * to the camera.\n> +\t */\n> +\tstd::vector<dev_t> devnums;\n> +\tfor (const std::shared_ptr<MediaDevice> media : mediaDevices_) {\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> +\t\t\t    entity->function() == MEDIA_ENT_F_IO_V4L) {\n> +\t\t\t\tdevnums.push_back(makedev(entity->deviceMajor(),\n> +\t\t\t\t\t\t\t  entity->deviceMinor()));\n> +\t\t\t}\n> +\t\t}\n> +\t}\n> +\n> +\tmanager_->addCamera(std::move(camera), devnums);\n>  }\n>  \n>  /**","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6BD0061027\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  8 Jun 2020 16:10:25 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D137124F;\n\tMon,  8 Jun 2020 16:10:24 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"TBXSNSfL\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591625425;\n\tbh=8ge+Ts0VNDsjONNfwojGllPIF/64hVDHzbCnGjzUEgU=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=TBXSNSfLGAqx3+K6OfSq2clJ9Z3ujUAi9KiKs7Gfgrg6I+4pbtQ6VQTaLFvkIM3Gx\n\ttuofb/VmSASLRAApbw5XHVKxJiTi3RrT1kv3Iy3Lv/XxX7opvUgfaMPffsLdRbtHOk\n\tzV+51lAosagGa6p5JwwAYJkRQaq0mSJvDE0k7Wd8=","Date":"Mon, 8 Jun 2020 17:10:05 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200608141005.GE5896@pendragon.ideasonboard.com>","References":"<20200608073253.5435-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20200608073253.5435-1-paul.elder@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v3] libcamera: CameraManager,\n\tPipelineHandler: Automatically map devnums to Camera","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>","X-List-Received-Date":"Mon, 08 Jun 2020 14:10:25 -0000"}}]