[v2,4/4] libcamera: V4L2Subdevice: Allow for a regex to match entity name
diff mbox series

Message ID 20250717124853.2317191-5-dan.scally@ideasonboard.com
State New
Headers show
Series
  • Use regular expressions for entity name matching
Related show

Commit Message

Dan Scally July 17, 2025, 12:48 p.m. UTC
Some kernel drivers give their entities names that will differ from
implementation to implementation; for example the drivers for the
Camera Receiver Unit and CSI-2 receiver in the RZ/V2H SoC give their
entities names that include their memory address, in the format
"csi-16000400.csi2". Passing that entity name to
V4L2Subdevice::fromEntityName() is too inflexible given it would only
then work if that specific CSI-2 receiver were the one being used.

Add an overload for V4L2Subdevice::fromEntityName() to instead allow
users to pass a std::basic_regex, and use std::regex_search() instead
of a direct string comparison to find a matching entity. Ths allows
us to form regular expressions like "csi-[0-9a-f]{8}.csi2" to find
the entities.

Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
Changes in v2:

	- New patch

 include/libcamera/internal/v4l2_subdevice.h |  3 +++
 src/libcamera/v4l2_subdevice.cpp            | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

Comments

Stefan Klug Aug. 18, 2025, 10:44 a.m. UTC | #1
Hi Dan,

Thank you for the patch.

Quoting Daniel Scally (2025-07-17 14:48:53)
> Some kernel drivers give their entities names that will differ from
> implementation to implementation; for example the drivers for the
> Camera Receiver Unit and CSI-2 receiver in the RZ/V2H SoC give their
> entities names that include their memory address, in the format
> "csi-16000400.csi2". Passing that entity name to
> V4L2Subdevice::fromEntityName() is too inflexible given it would only
> then work if that specific CSI-2 receiver were the one being used.
> 
> Add an overload for V4L2Subdevice::fromEntityName() to instead allow
> users to pass a std::basic_regex, and use std::regex_search() instead
> of a direct string comparison to find a matching entity. Ths allows
> us to form regular expressions like "csi-[0-9a-f]{8}.csi2" to find
> the entities.
> 
> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
> ---
> Changes in v2:
> 
>         - New patch
> 
>  include/libcamera/internal/v4l2_subdevice.h |  3 +++
>  src/libcamera/v4l2_subdevice.cpp            | 19 +++++++++++++++++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h
> index fa2a4a21..192141be 100644
> --- a/include/libcamera/internal/v4l2_subdevice.h
> +++ b/include/libcamera/internal/v4l2_subdevice.h
> @@ -18,6 +18,7 @@
>  
>  #include <libcamera/base/class.h>
>  #include <libcamera/base/log.h>
> +#include <libcamera/base/regex.h>
>  
>  #include <libcamera/color_space.h>
>  #include <libcamera/geometry.h>
> @@ -163,6 +164,8 @@ public:
>  
>         static std::unique_ptr<V4L2Subdevice>
>         fromEntityName(const MediaDevice *media, const std::string &entity);
> +       static std::unique_ptr<V4L2Subdevice>
> +       fromEntityName(const MediaDevice *media, const std::regex &entity);

In this context the function name seems a tiny bit off to me.
fromEntityNameMatching() or fromEntityMatching() come to mind but are
also not super smooth. So I'll leave that to you :-)

Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>

Cheers,
Stefan

>  
>  protected:
>         std::string logPrefix() const override;
> diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp
> index fd3b18c2..72fcf0d5 100644
> --- a/src/libcamera/v4l2_subdevice.cpp
> +++ b/src/libcamera/v4l2_subdevice.cpp
> @@ -1760,6 +1760,25 @@ V4L2Subdevice::fromEntityName(const MediaDevice *media,
>         return std::make_unique<V4L2Subdevice>(mediaEntity);
>  }
>  
> +/**
> + * \brief Create a new video subdevice instance from an entity in media device
> + * \a media
> + * \param[in] media The media device where the entity is registered
> + * \param[in] entity A regex that will match the media entity's name
> + *
> + * \return A newly created V4L2Subdevice on success, nullptr otherwise
> + */
> +std::unique_ptr<V4L2Subdevice>
> +V4L2Subdevice::fromEntityName(const MediaDevice *media,
> +                             const std::regex &entity)
> +{
> +       MediaEntity *mediaEntity = media->getEntityByName(entity);
> +       if (!mediaEntity)
> +               return nullptr;
> +
> +       return std::make_unique<V4L2Subdevice>(mediaEntity);
> +}
> +
>  std::string V4L2Subdevice::logPrefix() const
>  {
>         return "'" + entity_->name() + "'";
> -- 
> 2.34.1
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h
index fa2a4a21..192141be 100644
--- a/include/libcamera/internal/v4l2_subdevice.h
+++ b/include/libcamera/internal/v4l2_subdevice.h
@@ -18,6 +18,7 @@ 
 
 #include <libcamera/base/class.h>
 #include <libcamera/base/log.h>
+#include <libcamera/base/regex.h>
 
 #include <libcamera/color_space.h>
 #include <libcamera/geometry.h>
@@ -163,6 +164,8 @@  public:
 
 	static std::unique_ptr<V4L2Subdevice>
 	fromEntityName(const MediaDevice *media, const std::string &entity);
+	static std::unique_ptr<V4L2Subdevice>
+	fromEntityName(const MediaDevice *media, const std::regex &entity);
 
 protected:
 	std::string logPrefix() const override;
diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp
index fd3b18c2..72fcf0d5 100644
--- a/src/libcamera/v4l2_subdevice.cpp
+++ b/src/libcamera/v4l2_subdevice.cpp
@@ -1760,6 +1760,25 @@  V4L2Subdevice::fromEntityName(const MediaDevice *media,
 	return std::make_unique<V4L2Subdevice>(mediaEntity);
 }
 
+/**
+ * \brief Create a new video subdevice instance from an entity in media device
+ * \a media
+ * \param[in] media The media device where the entity is registered
+ * \param[in] entity A regex that will match the media entity's name
+ *
+ * \return A newly created V4L2Subdevice on success, nullptr otherwise
+ */
+std::unique_ptr<V4L2Subdevice>
+V4L2Subdevice::fromEntityName(const MediaDevice *media,
+			      const std::regex &entity)
+{
+	MediaEntity *mediaEntity = media->getEntityByName(entity);
+	if (!mediaEntity)
+		return nullptr;
+
+	return std::make_unique<V4L2Subdevice>(mediaEntity);
+}
+
 std::string V4L2Subdevice::logPrefix() const
 {
 	return "'" + entity_->name() + "'";