[libcamera-devel] [PATCH v7 3/6] libcamera: v4l2_device: Implement get and set controls

Laurent Pinchart laurent.pinchart at ideasonboard.com
Tue Jun 25 13:49:05 CEST 2019


Hi Jacopo,

Thank you for the patch.

On Tue, Jun 25, 2019 at 08:48:27AM +0200, Jacopo Mondi wrote:
> Implement getControls() and setControls() operations in V4L2Device class.
> 
> Both operations take a V4L2Controls instance and read or write the V4L2
> controls on the V4L2 device.
> 
> Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>

Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

> ---
>  src/libcamera/include/v4l2_device.h |   9 ++
>  src/libcamera/v4l2_device.cpp       | 190 ++++++++++++++++++++++++++++
>  2 files changed, 199 insertions(+)
> 
> diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h
> index 556960467e10..24a0134a2cba 100644
> --- a/src/libcamera/include/v4l2_device.h
> +++ b/src/libcamera/include/v4l2_device.h
> @@ -10,11 +10,14 @@
>  #include <map>
>  #include <string>
>  
> +#include <linux/videodev2.h>
> +
>  #include "log.h"
>  
>  namespace libcamera {
>  
>  class V4L2ControlInfo;
> +class V4L2ControlList;
>  
>  class V4L2Device : protected Loggable
>  {
> @@ -23,6 +26,8 @@ public:
>  	bool isOpen() const { return fd_ != -1; }
>  
>  	const V4L2ControlInfo *getControlInfo(unsigned int id) const;
> +	int getControls(V4L2ControlList *ctrls);
> +	int setControls(V4L2ControlList *ctrls);
>  
>  	const std::string &deviceNode() const { return deviceNode_; }
>  
> @@ -38,6 +43,10 @@ protected:
>  
>  private:
>  	void listControls();
> +	void updateControls(V4L2ControlList *ctrls,
> +			    const V4L2ControlInfo **controlInfo,
> +			    const struct v4l2_ext_control *v4l2Ctrls,
> +			    unsigned int count);
>  
>  	std::map<unsigned int, V4L2ControlInfo> controls_;
>  	std::string deviceNode_;
> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> index 51764b441f92..84758a811c27 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -125,6 +125,163 @@ const V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id) const
>  	return &it->second;
>  }
>  
> +/**
> + * \brief Read controls from the device
> + * \param[inout] ctrls The list of controls to read
> + *
> + * This method reads the value of all controls contained in \a ctrls, and stores
> + * their values in the corresponding \a ctrls entry.
> + *
> + * If any control in \a ctrls is not supported by the device, is disabled (i.e.
> + * has the V4L2_CTRL_FLAG_DISABLED flag set), is a compound control, or if any
> + * other error occurs during validation of the requested controls, no control is
> + * read and this method returns -EINVAL.
> + *
> + * If an error occurs while reading the controls, the index of the first control
> + * that couldn't be read is returned. The value of all controls below that index
> + * are updated in \a ctrls, while the value of all the other controls are not
> + * changed.
> + *
> + * \return 0 on success or an error code otherwise
> + * \retval -EINVAL One of the control is not supported or not accessible
> + * \retval i The index of the control that failed
> + */
> +int V4L2Device::getControls(V4L2ControlList *ctrls)
> +{
> +	unsigned int count = ctrls->size();
> +	if (count == 0)
> +		return 0;
> +
> +	const V4L2ControlInfo *controlInfo[count] = {};
> +	struct v4l2_ext_control v4l2Ctrls[count] = {};
> +	for (unsigned int i = 0; i < count; ++i) {
> +		const V4L2Control *ctrl = ctrls->getByIndex(i);
> +		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> +		if (!info) {
> +			LOG(V4L2, Error)
> +				<< "Control '" << ctrl->id() << "' not found";
> +			return -EINVAL;
> +		}
> +
> +		controlInfo[i] = info;
> +		v4l2Ctrls[i].id = info->id();
> +	}
> +
> +	struct v4l2_ext_controls v4l2ExtCtrls = {};
> +	v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;
> +	v4l2ExtCtrls.controls = v4l2Ctrls;
> +	v4l2ExtCtrls.count = count;
> +
> +	int ret = ioctl(VIDIOC_G_EXT_CTRLS, &v4l2ExtCtrls);
> +	if (ret) {
> +		unsigned int errorIdx = v4l2ExtCtrls.error_idx;
> +
> +		/* Generic validation error. */
> +		if (errorIdx == 0 || errorIdx >= count) {
> +			LOG(V4L2, Error) << "Unable to read controls: "
> +					 << strerror(ret);
> +			return -EINVAL;
> +		}
> +
> +		/* A specific control failed. */
> +		LOG(V4L2, Error) << "Unable to read control " << errorIdx
> +				 << ": " << strerror(ret);
> +		count = errorIdx - 1;
> +		ret = errorIdx;
> +	}
> +
> +	updateControls(ctrls, controlInfo, v4l2Ctrls, count);
> +
> +	return ret;
> +}
> +
> +/**
> + * \brief Write controls to the device
> + * \param[in] ctrls The list of controls to write
> + *
> + * This method writes the value of all controls contained in \a ctrls, and
> + * stores the values actually applied to the device in the corresponding
> + * \a ctrls entry.
> + *
> + * If any control in \a ctrls is not supported by the device, is disabled (i.e.
> + * has the V4L2_CTRL_FLAG_DISABLED flag set), is read-only, is a
> + * compound control, or if any other error occurs during validation of
> + * the requested controls, no control is written and this method returns
> + * -EINVAL.
> + *
> + * If an error occurs while writing the controls, the index of the first
> + * control that couldn't be written is returned. All controls below that index
> + * are written and their values are updated in \a ctrls, while all other
> + * controls are not written and their values are not changed.
> + *
> + * \return 0 on success or an error code otherwise
> + * \retval -EINVAL One of the control is not supported or not accessible
> + * \retval i The index of the control that failed
> + */
> +int V4L2Device::setControls(V4L2ControlList *ctrls)
> +{
> +	unsigned int count = ctrls->size();
> +	if (count == 0)
> +		return 0;
> +
> +	const V4L2ControlInfo *controlInfo[count] = {};
> +	struct v4l2_ext_control v4l2Ctrls[count] = {};
> +
> +	for (unsigned int i = 0; i < count; ++i) {
> +		const V4L2Control *ctrl = ctrls->getByIndex(i);
> +		const V4L2ControlInfo *info = getControlInfo(ctrl->id());
> +		if (!info) {
> +			LOG(V4L2, Error)
> +				<< "Control '" << ctrl->id() << "' not found";
> +			return -EINVAL;
> +		}
> +
> +		controlInfo[i] = info;
> +		v4l2Ctrls[i].id = info->id();
> +
> +		/* Set the v4l2_ext_control value for the write operation. */
> +		switch (info->type()) {
> +		case V4L2_CTRL_TYPE_INTEGER64:
> +			v4l2Ctrls[i].value64 = ctrl->value();
> +			break;
> +		default:
> +			/*
> +			 * \todo To be changed when support for string and
> +			 * compound controls will be added.
> +			 */
> +			v4l2Ctrls[i].value = ctrl->value();
> +			break;
> +		}
> +	}
> +
> +	struct v4l2_ext_controls v4l2ExtCtrls = {};
> +	v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;
> +	v4l2ExtCtrls.controls = v4l2Ctrls;
> +	v4l2ExtCtrls.count = count;
> +
> +	int ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);
> +	if (ret) {
> +		unsigned int errorIdx = v4l2ExtCtrls.error_idx;
> +
> +		/* Generic validation error. */
> +		if (errorIdx == 0 || errorIdx >= count) {
> +			LOG(V4L2, Error) << "Unable to read controls: "
> +					 << strerror(ret);
> +			return -EINVAL;
> +		}
> +
> +		/* A specific control failed. */
> +		LOG(V4L2, Error) << "Unable to read control " << errorIdx
> +				 << ": " << strerror(ret);
> +		count = errorIdx - 1;
> +		ret = errorIdx;
> +	}
> +
> +	updateControls(ctrls, controlInfo, v4l2Ctrls, count);
> +
> +	return ret;
> +}
> +
>  /**
>   * \brief Perform an IOCTL system call on the device node
>   * \param[in] request The IOCTL request code
> @@ -194,4 +351,37 @@ void V4L2Device::listControls()
>  	}
>  }
>  
> +/*
> + * \brief Update the value of the first \a count V4L2 controls in \a ctrls using
> + * values in \a v4l2Ctrls
> + * \param[inout] ctrls List of V4L2 controls to update
> + * \param[in] controlInfo List of V4L2 control information
> + * \param[in] v4l2Ctrls List of V4L2 extended controls as returned by the driver
> + * \param[in] count The number of controls to update
> + */
> +void V4L2Device::updateControls(V4L2ControlList *ctrls,
> +				const V4L2ControlInfo **controlInfo,
> +				const struct v4l2_ext_control *v4l2Ctrls,
> +				unsigned int count)
> +{
> +	for (unsigned int i = 0; i < count; ++i) {
> +		const struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];
> +		const V4L2ControlInfo *info = controlInfo[i];
> +		V4L2Control *ctrl = ctrls->getByIndex(i);
> +
> +		switch (info->type()) {
> +		case V4L2_CTRL_TYPE_INTEGER64:
> +			ctrl->setValue(v4l2Ctrl->value64);
> +			break;
> +		default:
> +			/*
> +			 * \todo To be changed when support for string and
> +			 * compound controls will be added.
> +			 */
> +			ctrl->setValue(v4l2Ctrl->value);
> +			break;
> +		}
> +	}
> +}
> +
>  } /* namespace libcamera */

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list