[libcamera-devel,v4,4/9] libcamera: camera_sensor: Discover VCMs through ancillary links
diff mbox series

Message ID 20220131223330.61563-5-djrscally@gmail.com
State Superseded
Headers show
Series
  • Enumerate CameraLens by following sensor's ancillary links
Related show

Commit Message

Daniel Scally Jan. 31, 2022, 10:33 p.m. UTC
Add a function to check for and initialise any VCMs linked to the
CameraSensor's entity by ancillary links. This should initialise
the lens_ member with the linked entity. Call the new function
during CameraSensor::init().

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Daniel Scally <djrscally@gmail.com>
---
Changes in v4:

	- Fixed typo (Laurent)

Changes in v3:

	- Fixed some style errors
	- Replaced the flash case by printing the entity function in the 
	default case.

 include/libcamera/internal/camera_sensor.h |  1 +
 src/libcamera/camera_sensor.cpp            | 39 ++++++++++++++++++++++
 2 files changed, 40 insertions(+)

Comments

Laurent Pinchart Feb. 3, 2022, 12:19 a.m. UTC | #1
Hi Dan,

Thank you for the patch.

On Mon, Jan 31, 2022 at 10:33:25PM +0000, Daniel Scally wrote:
> Add a function to check for and initialise any VCMs linked to the
> CameraSensor's entity by ancillary links. This should initialise
> the lens_ member with the linked entity. Call the new function
> during CameraSensor::init().
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Signed-off-by: Daniel Scally <djrscally@gmail.com>
> ---
> Changes in v4:
> 
> 	- Fixed typo (Laurent)
> 
> Changes in v3:
> 
> 	- Fixed some style errors
> 	- Replaced the flash case by printing the entity function in the 
> 	default case.
> 
>  include/libcamera/internal/camera_sensor.h |  1 +
>  src/libcamera/camera_sensor.cpp            | 39 ++++++++++++++++++++++
>  2 files changed, 40 insertions(+)
> 
> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h
> index 2fa11567..7fb4eded 100644
> --- a/include/libcamera/internal/camera_sensor.h
> +++ b/include/libcamera/internal/camera_sensor.h
> @@ -81,6 +81,7 @@ private:
>  	void initTestPatternModes();
>  	int initProperties();
>  	int applyTestPatternMode(controls::draft::TestPatternModeEnum mode);
> +	int discoverAncillaryDevices();
>  
>  	const MediaEntity *entity_;
>  	std::unique_ptr<V4L2Subdevice> subdev_;
> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
> index 64f7f12c..f00c0aca 100644
> --- a/src/libcamera/camera_sensor.cpp
> +++ b/src/libcamera/camera_sensor.cpp
> @@ -172,6 +172,10 @@ int CameraSensor::init()
>  	if (ret)
>  		return ret;
>  
> +	ret = discoverAncillaryDevices();
> +	if (ret)
> +		return ret;
> +
>  	return applyTestPatternMode(controls::draft::TestPatternModeEnum::TestPatternModeOff);
>  }
>  
> @@ -443,6 +447,41 @@ int CameraSensor::initProperties()
>  	return 0;
>  }
>  
> +/**
> + * \brief Check for and initialise any ancillary devices
> + *
> + * Sensors sometimes have ancillary devices such as a Lens or Flash that could
> + * be linked to their MediaEntity by the kernel. Search for and handle any
> + * such device.
> + *
> + * \todo Handle MEDIA_ENT_F_FLASH too.
> + */
> +int CameraSensor::discoverAncillaryDevices()
> +{
> +	int ret;
> +
> +	for (MediaEntity *ancillary : entity_->ancillaryEntities()) {
> +		switch (ancillary->function()) {
> +		case MEDIA_ENT_F_LENS:
> +			focusLens_ = std::make_unique<CameraLens>(ancillary);
> +			ret = focusLens_->init();
> +			if (ret) {
> +				LOG(CameraSensor, Error)
> +					<< "CameraLens initialisation failed";
> +				return ret;
> +			}
> +			break;
> +
> +		default:
> +			LOG(CameraSensor, Warning)
> +				<< "Unsupported ancillary entity function "
> +				<< ancillary->function();

A break would be good here too, even if not strictly required.

> +		}
> +	}
> +
> +	return 0;
> +}
> +
>  /**
>   * \fn CameraSensor::model()
>   * \brief Retrieve the sensor model name
Daniel Scally Feb. 3, 2022, 9:40 a.m. UTC | #2
Morning Laurent

On 03/02/2022 00:19, Laurent Pinchart wrote:
> Hi Dan,
>
> Thank you for the patch.
>
> On Mon, Jan 31, 2022 at 10:33:25PM +0000, Daniel Scally wrote:
>> Add a function to check for and initialise any VCMs linked to the
>> CameraSensor's entity by ancillary links. This should initialise
>> the lens_ member with the linked entity. Call the new function
>> during CameraSensor::init().
>>
>> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> Signed-off-by: Daniel Scally <djrscally@gmail.com>
>> ---
>> Changes in v4:
>>
>> 	- Fixed typo (Laurent)
>>
>> Changes in v3:
>>
>> 	- Fixed some style errors
>> 	- Replaced the flash case by printing the entity function in the 
>> 	default case.
>>
>>  include/libcamera/internal/camera_sensor.h |  1 +
>>  src/libcamera/camera_sensor.cpp            | 39 ++++++++++++++++++++++
>>  2 files changed, 40 insertions(+)
>>
>> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h
>> index 2fa11567..7fb4eded 100644
>> --- a/include/libcamera/internal/camera_sensor.h
>> +++ b/include/libcamera/internal/camera_sensor.h
>> @@ -81,6 +81,7 @@ private:
>>  	void initTestPatternModes();
>>  	int initProperties();
>>  	int applyTestPatternMode(controls::draft::TestPatternModeEnum mode);
>> +	int discoverAncillaryDevices();
>>  
>>  	const MediaEntity *entity_;
>>  	std::unique_ptr<V4L2Subdevice> subdev_;
>> diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
>> index 64f7f12c..f00c0aca 100644
>> --- a/src/libcamera/camera_sensor.cpp
>> +++ b/src/libcamera/camera_sensor.cpp
>> @@ -172,6 +172,10 @@ int CameraSensor::init()
>>  	if (ret)
>>  		return ret;
>>  
>> +	ret = discoverAncillaryDevices();
>> +	if (ret)
>> +		return ret;
>> +
>>  	return applyTestPatternMode(controls::draft::TestPatternModeEnum::TestPatternModeOff);
>>  }
>>  
>> @@ -443,6 +447,41 @@ int CameraSensor::initProperties()
>>  	return 0;
>>  }
>>  
>> +/**
>> + * \brief Check for and initialise any ancillary devices
>> + *
>> + * Sensors sometimes have ancillary devices such as a Lens or Flash that could
>> + * be linked to their MediaEntity by the kernel. Search for and handle any
>> + * such device.
>> + *
>> + * \todo Handle MEDIA_ENT_F_FLASH too.
>> + */
>> +int CameraSensor::discoverAncillaryDevices()
>> +{
>> +	int ret;
>> +
>> +	for (MediaEntity *ancillary : entity_->ancillaryEntities()) {
>> +		switch (ancillary->function()) {
>> +		case MEDIA_ENT_F_LENS:
>> +			focusLens_ = std::make_unique<CameraLens>(ancillary);
>> +			ret = focusLens_->init();
>> +			if (ret) {
>> +				LOG(CameraSensor, Error)
>> +					<< "CameraLens initialisation failed";
>> +				return ret;
>> +			}
>> +			break;
>> +
>> +		default:
>> +			LOG(CameraSensor, Warning)
>> +				<< "Unsupported ancillary entity function "
>> +				<< ancillary->function();
> A break would be good here too, even if not strictly required.


Okedokey - I'll add it in v5. Thanks for the comments on the other
patches also :)

>
>> +		}
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>>  /**
>>   * \fn CameraSensor::model()
>>   * \brief Retrieve the sensor model name

Patch
diff mbox series

diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h
index 2fa11567..7fb4eded 100644
--- a/include/libcamera/internal/camera_sensor.h
+++ b/include/libcamera/internal/camera_sensor.h
@@ -81,6 +81,7 @@  private:
 	void initTestPatternModes();
 	int initProperties();
 	int applyTestPatternMode(controls::draft::TestPatternModeEnum mode);
+	int discoverAncillaryDevices();
 
 	const MediaEntity *entity_;
 	std::unique_ptr<V4L2Subdevice> subdev_;
diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp
index 64f7f12c..f00c0aca 100644
--- a/src/libcamera/camera_sensor.cpp
+++ b/src/libcamera/camera_sensor.cpp
@@ -172,6 +172,10 @@  int CameraSensor::init()
 	if (ret)
 		return ret;
 
+	ret = discoverAncillaryDevices();
+	if (ret)
+		return ret;
+
 	return applyTestPatternMode(controls::draft::TestPatternModeEnum::TestPatternModeOff);
 }
 
@@ -443,6 +447,41 @@  int CameraSensor::initProperties()
 	return 0;
 }
 
+/**
+ * \brief Check for and initialise any ancillary devices
+ *
+ * Sensors sometimes have ancillary devices such as a Lens or Flash that could
+ * be linked to their MediaEntity by the kernel. Search for and handle any
+ * such device.
+ *
+ * \todo Handle MEDIA_ENT_F_FLASH too.
+ */
+int CameraSensor::discoverAncillaryDevices()
+{
+	int ret;
+
+	for (MediaEntity *ancillary : entity_->ancillaryEntities()) {
+		switch (ancillary->function()) {
+		case MEDIA_ENT_F_LENS:
+			focusLens_ = std::make_unique<CameraLens>(ancillary);
+			ret = focusLens_->init();
+			if (ret) {
+				LOG(CameraSensor, Error)
+					<< "CameraLens initialisation failed";
+				return ret;
+			}
+			break;
+
+		default:
+			LOG(CameraSensor, Warning)
+				<< "Unsupported ancillary entity function "
+				<< ancillary->function();
+		}
+	}
+
+	return 0;
+}
+
 /**
  * \fn CameraSensor::model()
  * \brief Retrieve the sensor model name