[libcamera-devel,RFC,v1,1/5] libcamera: v4l2_device: VIDIOC_QUERYCTRL call to fetch device attributes
diff mbox series

Message ID 20220414074342.7455-2-hpa@redhat.com
State Superseded
Headers show
Series
  • Enabling AF algorithm to get the VCM attributes from the device driver
Related show

Commit Message

Kate Hsuan April 14, 2022, 7:43 a.m. UTC
VIDIOC_QUERYCTRL can be used to get the device attributes. It can be used
to get the attributes for a given control list and store them in it.
Fianlly, the maximum capacity of the device can be found in the control
list.

Signed-off-by: Kate Hsuan<hpa@redhat.com>
---
 include/libcamera/internal/v4l2_device.h |  2 ++
 src/libcamera/v4l2_device.cpp            | 37 ++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h
index a52a5f2c..d93a72a0 100644
--- a/include/libcamera/internal/v4l2_device.h
+++ b/include/libcamera/internal/v4l2_device.h
@@ -37,6 +37,8 @@  public:
 	ControlList getControls(const std::vector<uint32_t> &ids);
 	int setControls(ControlList *ctrls);
 
+	int getAttributes(ControlList *ctrls);
+
 	const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
 
 	const std::string &deviceNode() const { return deviceNode_; }
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index 3fc8438f..eda429be 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -364,6 +364,43 @@  int V4L2Device::setControls(ControlList *ctrls)
 	return ret;
 }
 
+/**
+ * \brief Get capacity for a given control list
+ *
+ * This function will get device maximum capacity through VIDIOC_QUERYCTRL and
+ * store the maximum value to the \a ctrls. If a non-support control is found,
+ * it will return -EINVAL.
+ *
+ * \param ctrls The list of controls  to get the capacity
+ * \return 0 or non-zero on success or a empty control list
+ * \retval -EINVAL One of the control is not supported or not accessible
+ */
+int V4L2Device::getAttributes(ControlList *ctrls)
+{
+	int ret = 0;
+	if (ctrls->empty())
+		return 0;
+
+	struct v4l2_queryctrl query_ctrl;
+	for (auto [ctrl, i] = std::pair(ctrls->begin(), 0u); i < ctrls->size(); ctrl++, i++) {
+		const unsigned int id = ctrl->first;
+		const auto iter = controls_.find(id);
+		if (iter == controls_.end()) {
+			LOG(V4L2, Error)
+				<< "Control " << utils::hex(id) << " not found";
+			return -EINVAL;
+		}
+
+		memset(&query_ctrl, 0, sizeof(struct v4l2_queryctrl));
+		query_ctrl.id = id;
+		query_ctrl.type = V4L2_CTRL_TYPE_INTEGER;
+		ret = ioctl(VIDIOC_QUERYCTRL, &query_ctrl);
+		ctrls->set(id, query_ctrl.maximum);
+	}
+
+	return ret;
+}
+
 /**
  * \brief Retrieve the v4l2_query_ext_ctrl information for the given control
  * \param[in] id The V4L2 control id