[{"id":35837,"web_url":"https://patchwork.libcamera.org/comment/35837/","msgid":"<175802075894.103890.17224447851472181739@isaac-ThinkPad-T16-Gen-2>","date":"2025-09-16T11:05:58","subject":"Re: [PATCH v2 2/5] libcamera: Add support for camera flash devices","submitter":{"id":215,"url":"https://patchwork.libcamera.org/api/people/215/","name":"Isaac Scott","email":"isaac.scott@ideasonboard.com"},"content":"Hi Matthias,\n\nThank you for the patch!\n\nQuoting Matthias Fend (2025-09-12 08:13:21)\n> Add a class to model camera flash devices. Currently, only v4l2 flash\n> devices are supported. The v4l2 flash devices are implemented similar to\n> the camera lenses.\n> \n> Signed-off-by: Matthias Fend <matthias.fend@emfend.at>\n> ---\n>  include/libcamera/internal/camera_flash.h     |  72 ++++++++\n>  include/libcamera/internal/camera_sensor.h    |   2 +\n>  src/libcamera/camera_flash.cpp                | 249 ++++++++++++++++++++++++++\n>  src/libcamera/meson.build                     |   1 +\n>  src/libcamera/sensor/camera_sensor_legacy.cpp |  13 ++\n>  src/libcamera/sensor/camera_sensor_raw.cpp    |  13 ++\n>  6 files changed, 350 insertions(+)\n> \n> diff --git a/include/libcamera/internal/camera_flash.h b/include/libcamera/internal/camera_flash.h\n> new file mode 100644\n> index 0000000000000000000000000000000000000000..70234ca96d98dd88a084e2c6804a66e455d87ef8\n> --- /dev/null\n> +++ b/include/libcamera/internal/camera_flash.h\n> @@ -0,0 +1,72 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2025, matthias.fend@emfend.at\n> + *\n> + * Camera flash support\n> + */\n> +#pragma once\n> +\n> +#include <memory>\n> +#include <stdint.h>\n> +#include <string>\n> +\n> +#include <libcamera/base/class.h>\n> +#include <libcamera/base/log.h>\n> +\n> +#include <libcamera/controls.h>\n> +\n> +namespace libcamera {\n> +\n> +class MediaEntity;\n> +class V4L2Subdevice;\n> +\n> +class CameraFlash : protected Loggable\n> +{\n> +public:\n> +       enum class Mode {\n> +               None,\n> +               Flash,\n> +               Torch,\n> +       };\n> +\n> +       enum class StrobeSource {\n> +               Software,\n> +               External,\n> +       };\n> +\n> +       explicit CameraFlash(const MediaEntity *entity);\n> +       ~CameraFlash();\n> +       int init();\n> +       std::optional<Mode> getMode() const;\n> +       int setMode(Mode mode);\n> +       const ControlInfo &getFlashIntensityInfo() const;\n> +       std::optional<int32_t> getFlashIntensity() const;\n> +       int setFlashIntensity(int32_t intensity);\n> +       const ControlInfo &getFlashTimeoutInfo() const;\n> +       std::optional<int32_t> getFlashTimeout() const;\n> +       int setFlashTimeout(int32_t timeout_us);\n> +       std::optional<StrobeSource> getStrobeSource() const;\n> +       int setStrobeSource(StrobeSource source);\n> +       int startStrobe();\n> +       int stopStrobe();\n> +       const ControlInfo &getTorchIntensityInfo() const;\n> +       std::optional<int32_t> getTorchIntensity() const;\n> +       int setTorchIntensity(int32_t intensity);\n> +\n> +protected:\n> +       std::string logPrefix() const override;\n> +\n> +private:\n> +       LIBCAMERA_DISABLE_COPY_AND_MOVE(CameraFlash)\n> +\n> +       std::optional<int32_t> getSubdevControl(uint32_t id) const;\n> +       int setSubdevControl(uint32_t id, int32_t value);\n> +       int validateDriver();\n> +\n> +       const MediaEntity *entity_;\n> +       std::unique_ptr<V4L2Subdevice> subdev_;\n> +       std::string model_;\n> +       const ControlInfoMap *controlInfoMap_ = nullptr;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\n> index f6ef4df170d43500bc652762e9a575010a6c1cbd..faeecf244c8d42a43eca52ae04813e0c2f80e516 100644\n> --- a/include/libcamera/internal/camera_sensor.h\n> +++ b/include/libcamera/internal/camera_sensor.h\n> @@ -28,6 +28,7 @@\n>  \n>  namespace libcamera {\n>  \n> +class CameraFlash;\n>  class CameraLens;\n>  class MediaEntity;\n>  class SensorConfiguration;\n> @@ -48,6 +49,7 @@ public:\n>         virtual V4L2Subdevice *device() = 0;\n>  \n>         virtual CameraLens *focusLens() = 0;\n> +       virtual CameraFlash *flash() = 0;\n>  \n>         virtual const std::vector<unsigned int> &mbusCodes() const = 0;\n>         virtual std::vector<Size> sizes(unsigned int mbusCode) const = 0;\n> diff --git a/src/libcamera/camera_flash.cpp b/src/libcamera/camera_flash.cpp\n> new file mode 100644\n> index 0000000000000000000000000000000000000000..d3b9fbf78f063ae0cb67635c4cda667508479534\n> --- /dev/null\n> +++ b/src/libcamera/camera_flash.cpp\n> @@ -0,0 +1,249 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2025, matthias.fend@emfend.at\n> + *\n> + * Camera flash support\n> + */\n> +\n> +#include \"libcamera/internal/camera_flash.h\"\n> +\n> +#include <libcamera/base/utils.h>\n> +\n> +#include \"libcamera/internal/v4l2_subdevice.h\"\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(CameraFlash)\n> +\n> +CameraFlash::CameraFlash(const MediaEntity *entity)\n> +       : entity_(entity)\n> +{\n> +}\n> +\n> +CameraFlash::~CameraFlash() = default;\n> +\n> +int CameraFlash::init()\n> +{\n> +       if (entity_->function() != MEDIA_ENT_F_FLASH) {\n> +               LOG(CameraFlash, Error)\n> +                       << \"Invalid flash function \"\n> +                       << utils::hex(entity_->function());\n> +               return -EINVAL;\n> +       }\n> +\n> +       subdev_ = std::make_unique<V4L2Subdevice>(entity_);\n> +       int ret = subdev_->open();\n> +       if (ret < 0)\n> +               return ret;\n> +\n> +       controlInfoMap_ = &subdev_->controls();\n> +\n> +       ret = validateDriver();\n> +       if (ret)\n> +               return ret;\n> +\n> +       model_ = subdev_->model();\n> +\n> +       return 0;\n> +}\n> +\n> +std::optional<CameraFlash::Mode> CameraFlash::getMode() const\n> +{\n> +       Mode mode;\n> +\n> +       auto v4l2Mode = getSubdevControl(V4L2_CID_FLASH_LED_MODE);\n> +       if (!v4l2Mode)\n> +               return std::nullopt;\n> +\n> +       switch (*v4l2Mode) {\n> +       case V4L2_FLASH_LED_MODE_FLASH:\n> +               mode = Mode::Flash;\n> +               break;\n> +       case V4L2_FLASH_LED_MODE_TORCH:\n> +               mode = Mode::Torch;\n> +               break;\n> +       case V4L2_FLASH_LED_MODE_NONE:\n> +       default:\n> +               mode = Mode::None;\n> +               break;\n> +       }\n> +\n> +       return mode;\n> +}\n> +\n> +int CameraFlash::setMode(Mode mode)\n> +{\n> +       int32_t v4l2Mode;\n> +\n> +       switch (mode) {\n> +       case Mode::Flash:\n> +               v4l2Mode = V4L2_FLASH_LED_MODE_FLASH;\n> +               break;\n> +       case Mode::Torch:\n> +               v4l2Mode = V4L2_FLASH_LED_MODE_TORCH;\n> +               break;\n> +       case Mode::None:\n> +               v4l2Mode = V4L2_FLASH_LED_MODE_NONE;\n> +               break;\n> +       default:\n> +               return -EINVAL;\n> +       }\n> +\n> +       return setSubdevControl(V4L2_CID_FLASH_LED_MODE, v4l2Mode);\n> +}\n> +\n> +const ControlInfo &CameraFlash::getFlashIntensityInfo() const\n> +{\n> +       return controlInfoMap_->find(V4L2_CID_FLASH_INTENSITY)->second;\n> +}\n> +\n> +std::optional<int32_t> CameraFlash::getFlashIntensity() const\n> +{\n> +       return getSubdevControl(V4L2_CID_FLASH_INTENSITY);\n> +}\n> +\n> +int CameraFlash::setFlashIntensity(int32_t intensity)\n> +{\n> +       return setSubdevControl(V4L2_CID_FLASH_INTENSITY, intensity);\n> +}\n> +\n> +const ControlInfo &CameraFlash::getFlashTimeoutInfo() const\n> +{\n> +       return controlInfoMap_->find(V4L2_CID_FLASH_TIMEOUT)->second;\n> +}\n> +\n> +std::optional<int32_t> CameraFlash::getFlashTimeout() const\n> +{\n> +       return getSubdevControl(V4L2_CID_FLASH_TIMEOUT);\n> +}\n> +\n> +int CameraFlash::setFlashTimeout(int32_t timeout)\n> +{\n> +       return setSubdevControl(V4L2_CID_FLASH_TIMEOUT, timeout);\n> +}\n> +\n> +std::optional<CameraFlash::StrobeSource> CameraFlash::getStrobeSource() const\n> +{\n> +       StrobeSource source;\n> +\n> +       auto v4l2Source = getSubdevControl(V4L2_CID_FLASH_STROBE_SOURCE);\n> +       if (!v4l2Source)\n> +               return std::nullopt;\n> +\n> +       switch (*v4l2Source) {\n> +       case V4L2_FLASH_STROBE_SOURCE_EXTERNAL:\n> +               source = StrobeSource::External;\n> +               break;\n> +       case V4L2_FLASH_STROBE_SOURCE_SOFTWARE:\n> +       default:\n> +               source = StrobeSource::Software;\n> +               break;\n> +       }\n> +\n> +       return source;\n> +}\n> +\n> +int CameraFlash::setStrobeSource(StrobeSource source)\n> +{\n> +       int32_t v4l2Source;\n> +\n> +       switch (source) {\n> +       case StrobeSource::External:\n> +               v4l2Source = V4L2_FLASH_STROBE_SOURCE_EXTERNAL;\n> +               break;\n> +       case StrobeSource::Software:\n> +               v4l2Source = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;\n> +               break;\n> +       default:\n> +               return -EINVAL;\n> +       }\n> +\n> +       return setSubdevControl(V4L2_CID_FLASH_STROBE_SOURCE, v4l2Source);\n> +}\n> +\n> +int CameraFlash::startStrobe()\n> +{\n> +       return setSubdevControl(V4L2_CID_FLASH_STROBE, 1);\n> +}\n> +\n> +int CameraFlash::stopStrobe()\n> +{\n> +       return setSubdevControl(V4L2_CID_FLASH_STROBE_STOP, 1);\n> +}\n> +\n> +const ControlInfo &CameraFlash::getTorchIntensityInfo() const\n> +{\n> +       return controlInfoMap_->find(V4L2_CID_FLASH_TORCH_INTENSITY)->second;\n> +}\n> +\n> +std::optional<int32_t> CameraFlash::getTorchIntensity() const\n> +{\n> +       return getSubdevControl(V4L2_CID_FLASH_TORCH_INTENSITY);\n> +}\n> +\n> +int CameraFlash::setTorchIntensity(int32_t intensity)\n> +{\n> +       return setSubdevControl(V4L2_CID_FLASH_TORCH_INTENSITY, intensity);\n> +}\n> +\n> +std::string CameraFlash::logPrefix() const\n> +{\n> +       return \"'\" + entity_->name() + \"'\";\n> +}\n> +\n> +std::optional<int32_t> CameraFlash::getSubdevControl(uint32_t id) const\n> +{\n> +       ControlList controlList = subdev_->getControls(std::array{ id });\n> +\n> +       if (controlList.contains(id))\n> +               return std::nullopt;\n> +\n> +       return controlList.get(id).get<int32_t>();\n> +}\n> +\n> +int CameraFlash::setSubdevControl(uint32_t id, int32_t value)\n> +{\n> +       ControlList flashCtrls(*controlInfoMap_);\n> +\n> +       flashCtrls.set(id, value);\n> +\n> +       if (subdev_->setControls(&flashCtrls))\n> +               return -EINVAL;\n> +\n> +       return 0;\n> +}\n> +\n> +int CameraFlash::validateDriver()\n> +{\n> +       int ret = 0;\n> +       static constexpr uint32_t mandatoryControls[] = {\n> +               V4L2_CID_FLASH_LED_MODE,\n> +               V4L2_CID_FLASH_STROBE_SOURCE,\n> +               V4L2_CID_FLASH_STROBE,\n> +               V4L2_CID_FLASH_TIMEOUT,\n> +               V4L2_CID_FLASH_INTENSITY,\n> +               V4L2_CID_FLASH_TORCH_INTENSITY,\n> +       };\n\nThis all looks good to me. The thoughts I have are relating to whether\nSTROBE controls should be mandatory in general; If a torch device only\nserves as a torch, I don't think it should have to implement strobe\ncontrols. When I've used v4l2_flash for torches in the past I have\nimplemented the strobe functionality as a function that just returns 0\nso I can register the v4l2_flash in the first place. But that's a\ndiscussion for the maintainers of v4l2_flash in the kernel.\n\nReviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\n\n> +\n> +       for (uint32_t ctrl : mandatoryControls) {\n> +               if (!controlInfoMap_->count(ctrl)) {\n> +                       LOG(CameraFlash, Error)\n> +                               << \"Mandatory V4L2 control \" << utils::hex(ctrl)\n> +                               << \" not available\";\n> +                       ret = -EINVAL;\n> +               }\n> +       }\n> +\n> +       if (ret) {\n> +               LOG(CameraFlash, Error)\n> +                       << \"The flash kernel driver needs to be fixed\";\n> +               LOG(CameraFlash, Error)\n> +                       << \"See Documentation/flash_driver_requirements.rst in\"\n> +                       << \" the libcamera sources for more information\";\n> +               return ret;\n> +       }\n> +\n> +       return ret;\n> +}\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index b3ca27f217da4ba3a896ef7cbfb5502fa82a4907..0f125661a51e2431c1febc353cef30a1219f9ce7 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -20,6 +20,7 @@ libcamera_internal_sources = files([\n>      'bayer_format.cpp',\n>      'byte_stream_buffer.cpp',\n>      'camera_controls.cpp',\n> +    'camera_flash.cpp',\n>      'camera_lens.cpp',\n>      'clock_recovery.cpp',\n>      'control_serializer.cpp',\n> diff --git a/src/libcamera/sensor/camera_sensor_legacy.cpp b/src/libcamera/sensor/camera_sensor_legacy.cpp\n> index f9e685a9acc499fc91d51ed1d66780a0ad2d2a8f..632b66ea0aa15fcd654e7f0efb50c24cb9b973bf 100644\n> --- a/src/libcamera/sensor/camera_sensor_legacy.cpp\n> +++ b/src/libcamera/sensor/camera_sensor_legacy.cpp\n> @@ -31,6 +31,7 @@\n>  #include <libcamera/ipa/core_ipa_interface.h>\n>  \n>  #include \"libcamera/internal/bayer_format.h\"\n> +#include \"libcamera/internal/camera_flash.h\"\n>  #include \"libcamera/internal/camera_lens.h\"\n>  #include \"libcamera/internal/camera_sensor.h\"\n>  #include \"libcamera/internal/camera_sensor_properties.h\"\n> @@ -68,6 +69,7 @@ public:\n>         V4L2Subdevice *device() override { return subdev_.get(); }\n>  \n>         CameraLens *focusLens() override { return focusLens_.get(); }\n> +       CameraFlash *flash() override { return flash_.get(); }\n>  \n>         const std::vector<unsigned int> &mbusCodes() const override { return mbusCodes_; }\n>         std::vector<Size> sizes(unsigned int mbusCode) const override;\n> @@ -139,6 +141,7 @@ private:\n>         ControlList properties_;\n>  \n>         std::unique_ptr<CameraLens> focusLens_;\n> +       std::unique_ptr<CameraFlash> flash_;\n>  };\n>  \n>  /**\n> @@ -665,6 +668,16 @@ int CameraSensorLegacy::discoverAncillaryDevices()\n>                         }\n>                         break;\n>  \n> +               case MEDIA_ENT_F_FLASH:\n> +                       flash_ = std::make_unique<CameraFlash>(ancillary);\n> +                       ret = flash_->init();\n> +                       if (ret) {\n> +                               LOG(CameraSensor, Error)\n> +                                       << \"Flash initialisation failed, flash disabled\";\n> +                               flash_.reset();\n> +                       }\n> +                       break;\n> +\n>                 default:\n>                         LOG(CameraSensor, Warning)\n>                                 << \"Unsupported ancillary entity function \"\n> diff --git a/src/libcamera/sensor/camera_sensor_raw.cpp b/src/libcamera/sensor/camera_sensor_raw.cpp\n> index 8ea4423698cd8c1eaae43eb5ba8b5d524b94d515..9d533d814b9df453aa4009a87818c1558bcbd665 100644\n> --- a/src/libcamera/sensor/camera_sensor_raw.cpp\n> +++ b/src/libcamera/sensor/camera_sensor_raw.cpp\n> @@ -32,6 +32,7 @@\n>  #include <libcamera/ipa/core_ipa_interface.h>\n>  \n>  #include \"libcamera/internal/bayer_format.h\"\n> +#include \"libcamera/internal/camera_flash.h\"\n>  #include \"libcamera/internal/camera_lens.h\"\n>  #include \"libcamera/internal/camera_sensor.h\"\n>  #include \"libcamera/internal/camera_sensor_properties.h\"\n> @@ -69,6 +70,7 @@ public:\n>         V4L2Subdevice *device() override { return subdev_.get(); }\n>  \n>         CameraLens *focusLens() override { return focusLens_.get(); }\n> +       CameraFlash *flash() override { return flash_.get(); }\n>  \n>         const std::vector<unsigned int> &mbusCodes() const override { return mbusCodes_; }\n>         std::vector<Size> sizes(unsigned int mbusCode) const override;\n> @@ -150,6 +152,7 @@ private:\n>         ControlList properties_;\n>  \n>         std::unique_ptr<CameraLens> focusLens_;\n> +       std::unique_ptr<CameraFlash> flash_;\n>  };\n>  \n>  /**\n> @@ -513,6 +516,16 @@ std::optional<int> CameraSensorRaw::init()\n>                         }\n>                         break;\n>  \n> +               case MEDIA_ENT_F_FLASH:\n> +                       flash_ = std::make_unique<CameraFlash>(ancillary);\n> +                       ret = flash_->init();\n> +                       if (ret) {\n> +                               LOG(CameraSensor, Error)\n> +                                       << \"Flash initialisation failed, flash disabled\";\n> +                               flash_.reset();\n> +                       }\n> +                       break;\n> +\n>                 default:\n>                         LOG(CameraSensor, Warning)\n>                                 << \"Unsupported ancillary entity function \"\n> \n> -- \n> 2.34.1\n> \n\nBest wishes,\n\nIsaac","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 7EF63C328C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 16 Sep 2025 11:06:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 43AE66936F;\n\tTue, 16 Sep 2025 13:06:03 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A236B613A1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 16 Sep 2025 13:06:01 +0200 (CEST)","from thinkpad.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 63C8FDF3;\n\tTue, 16 Sep 2025 13:04:43 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"QvKY/omi\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1758020683;\n\tbh=/ROWlg4yr8zu9cVHFaeTci/f+PUHq5/7RAxJmRFolB4=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=QvKY/omiUFkoV9ZB9+vZv53uypyc+2FooENd+AtJETvwGy4S4Lb/Q/CpFKmxKveb4\n\tRpKQgaVA84pOzko0fW0OAQvi9ITBLllycJiA124T+eBwEgDMEIQMQythZZIdgi+isu\n\tsaz5Jp9Lyu9r+SNA89HXwJIgL3z16uaGccNNZoIY=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250912-flash_reco-v2-2-d5bb80a2e619@emfend.at>","References":"<20250912-flash_reco-v2-0-d5bb80a2e619@emfend.at>\n\t<20250912-flash_reco-v2-2-d5bb80a2e619@emfend.at>","Subject":"Re: [PATCH v2 2/5] libcamera: Add support for camera flash devices","From":"Isaac Scott <isaac.scott@ideasonboard.com>","Cc":"Matthias Fend <matthias.fend@emfend.at>","To":"Matthias Fend <matthias.fend@emfend.at>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Tue, 16 Sep 2025 12:05:58 +0100","Message-ID":"<175802075894.103890.17224447851472181739@isaac-ThinkPad-T16-Gen-2>","User-Agent":"alot/0.10","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>"}}]