{"id":1486,"url":"https://patchwork.libcamera.org/api/patches/1486/?format=json","web_url":"https://patchwork.libcamera.org/patch/1486/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190620153144.5394-4-jacopo@jmondi.org>","date":"2019-06-20T15:31:41","name":"[libcamera-devel,v5,3/6] libcamera: v4l2_device: Implement get and set controls","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"8de8ad4a61529951936ab3d1044baa9d11612f4c","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/1486/mbox/","series":[{"id":368,"url":"https://patchwork.libcamera.org/api/series/368/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=368","date":"2019-06-20T15:31:38","name":"Add support for V4L2 controls","version":5,"mbox":"https://patchwork.libcamera.org/series/368/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1486/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1486/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net\n\t[217.70.183.193])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 38D1160BA1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 20 Jun 2019 17:30:42 +0200 (CEST)","from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay1-d.mail.gandi.net (Postfix) with ESMTPSA id C4140240006;\n\tThu, 20 Jun 2019 15:30:41 +0000 (UTC)"],"X-Originating-IP":"2.224.242.101","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 20 Jun 2019 17:31:41 +0200","Message-Id":"<20190620153144.5394-4-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190620153144.5394-1-jacopo@jmondi.org>","References":"<20190620153144.5394-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 3/6] libcamera: v4l2_device: Implement\n\tget and set controls","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Thu, 20 Jun 2019 15:30:42 -0000"},"content":"Implement getControls() and setControls() operations in V4L2Device class.\n\nBoth operations take a V4L2Controls instance and read or write the V4L2\ncontrols on the V4L2 device.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/include/v4l2_device.h |   3 +\n src/libcamera/v4l2_device.cpp       | 193 ++++++++++++++++++++++++++++\n 2 files changed, 196 insertions(+)","diff":"diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 91a72fcecbcc..3af27c9f7af5 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -15,6 +15,7 @@\n namespace libcamera {\n \n class V4L2ControlInfo;\n+class V4L2Controls;\n \n class V4L2Device : protected Loggable\n {\n@@ -23,6 +24,8 @@ public:\n \tbool isOpen() const { return fd_ != -1; }\n \n \tconst V4L2ControlInfo *getControlInfo(unsigned int id) const;\n+\tint getControls(V4L2Controls *ctrls);\n+\tint setControls(V4L2Controls *ctrls);\n \n \tconst std::string &deviceNode() const { return deviceNode_; }\n \ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex b113ff77e3cf..efe30ef856ae 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -125,6 +125,199 @@ const V4L2ControlInfo *V4L2Device::getControlInfo(unsigned int id) const\n \treturn &it->second;\n }\n \n+/**\n+ * \\brief Read controls from the device\n+ * \\param[inout] ctrls The list of controls to read\n+ *\n+ * Read the value of each control contained in \\a ctrls, and store the\n+ * value in the corresponding \\a ctrls entry.\n+ *\n+ * If an integer number less than the requested number of controls is returned,\n+ * only controls up to that index have been successfully read.\n+ *\n+ * Each V4L2Control instance in \\a ctrls should be supported by the device and\n+ * accessible (ie the V4L2_CTRL_FLAG_DISABLED flag should not be set), otherwise\n+ * a negative error code (-EINVAL) is returned\n+ *\n+ * \\return 0 on success or an error code otherwise\n+ * \\retval -EINVAL One of the control is not supported or not accessible\n+ * \\retval i The index of the control that failed\n+ */\n+int V4L2Device::getControls(V4L2Controls *ctrls)\n+{\n+\tunsigned int count = ctrls->size();\n+\tif (count == 0)\n+\t\treturn 0;\n+\n+\tconst V4L2ControlInfo *controlInfo[count] = {};\n+\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n+\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl->id());\n+\t\tif (!info) {\n+\t\t\tLOG(V4L2, Error)\n+\t\t\t\t<< \"Control '\" << ctrl->id() << \"' not found\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tcontrolInfo[i] = info;\n+\t\tv4l2Ctrls[i].id = info->id();\n+\t}\n+\n+\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n+\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n+\tv4l2ExtCtrls.controls = v4l2Ctrls;\n+\tv4l2ExtCtrls.count = count;\n+\n+\tint ret = ioctl(VIDIOC_G_EXT_CTRLS, &v4l2ExtCtrls);\n+\tif (ret) {\n+\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n+\n+\t\t/* Generic validation error. */\n+\t\tif (errorIdx == count) {\n+\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n+\t\t\t\t\t << strerror(ret);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\t/* A specific control failed. */\n+\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n+\t\t\t\t << \": \" << strerror(ret);\n+\t\treturn errorIdx;\n+\t}\n+\n+\t/*\n+\t * For each control read from the device, set the value in the\n+\t * V4L2Control provided by the caller.\n+\t */\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tconst V4L2ControlInfo *info = controlInfo[i];\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\tswitch (info->type()) {\n+\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\tctrl->setValue(v4l2Ctrl->value64);\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\t/*\n+\t\t\t * \\todo To be changed when support for string and\n+\t\t\t * compound controls will be added.\n+\t\t\t */\n+\t\t\tctrl->setValue(static_cast<long int>(v4l2Ctrl->value));\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Write controls to the device\n+ * \\param[in] ctrls The list of controls to write\n+ *\n+ * Write the value of each V4L2Control contained in \\a ctrls. Each\n+ * control should be initialized by the caller with a value, otherwise\n+ * the result of the operation is undefined.\n+ *\n+ * The value of each control is updated to reflect what has been\n+ * actually applied on the device.\n+ *\n+ * If an integer number less than the requested number of controls is returned,\n+ * only controls up to that index have been successfully applied.\n+ *\n+ * Each V4L2Control instance in \\a ctrls should be supported by the device and\n+ * accessible (ie the V4L2_CTRL_FLAG_DISABLED flag should not be set), otherwise\n+ * a negative error code (-EINVAL) is returned\n+ *\n+ * \\return 0 on success or an error code otherwise\n+ * \\retval -EINVAL One of the control is not supported or not accessible\n+ * \\retval i The index of the control that failed\n+ */\n+int V4L2Device::setControls(V4L2Controls *ctrls)\n+{\n+\tunsigned int count = ctrls->size();\n+\tif (count == 0)\n+\t\treturn 0;\n+\n+\tconst V4L2ControlInfo *controlInfo[count] = {};\n+\tstruct v4l2_ext_control v4l2Ctrls[count] = {};\n+\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\tconst V4L2ControlInfo *info = getControlInfo(ctrl->id());\n+\t\tif (!info) {\n+\t\t\tLOG(V4L2, Error)\n+\t\t\t\t<< \"Control '\" << ctrl->id() << \"' not found\";\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tcontrolInfo[i] = info;\n+\t\tv4l2Ctrls[i].id = info->id();\n+\n+\t\t/* Set the v4l2_ext_control value for the write operation. */\n+\t\tswitch (info->type()) {\n+\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\tv4l2Ctrls[i].value64 = ctrl->value();\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\t/*\n+\t\t\t * \\todo To be changed when support for string and\n+\t\t\t * compound controls will be added.\n+\t\t\t */\n+\t\t\tv4l2Ctrls[i].value = static_cast<int32_t>(ctrl->value());\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n+\tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n+\tv4l2ExtCtrls.controls = v4l2Ctrls;\n+\tv4l2ExtCtrls.count = count;\n+\n+\tint ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n+\tif (ret) {\n+\t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n+\n+\t\t/* Generic validation error. */\n+\t\tif (errorIdx == count) {\n+\t\t\tLOG(V4L2, Error) << \"Unable to read controls: \"\n+\t\t\t\t\t << strerror(ret);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\t/* A specific control failed. */\n+\t\tLOG(V4L2, Error) << \"Unable to read control \" << errorIdx\n+\t\t\t\t << \": \" << strerror(ret);\n+\t\treturn errorIdx;\n+\t}\n+\n+\t/* Update the control value to what have actually been applied. */\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tconst V4L2ControlInfo *info = controlInfo[i];\n+\t\tV4L2Control *ctrl = (*ctrls)[i];\n+\n+\t\tswitch (info->type()) {\n+\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\tctrl->setValue(v4l2Ctrl->value64);\n+\t\t\tbreak;\n+\t\tdefault:\n+\t\t\t/*\n+\t\t\t * \\todo To be changed when support for string and\n+\t\t\t * compound controls will be added.\n+\t\t\t */\n+\t\t\tctrl->setValue(static_cast<long int>(v4l2Ctrl->value));\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Perform an IOCTL system call on the device node\n  * \\param[in] request The IOCTL request code\n","prefixes":["libcamera-devel","v5","3/6"]}