{"id":1506,"url":"https://patchwork.libcamera.org/api/patches/1506/?format=json","web_url":"https://patchwork.libcamera.org/patch/1506/","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":"<20190624142859.19313-4-jacopo@jmondi.org>","date":"2019-06-24T14:28:56","name":"[libcamera-devel,v6,3/6] libcamera: v4l2_device: Implement get and set controls","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"63f98f2bb156198f285c053c2b5b803a27ce926c","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/1506/mbox/","series":[{"id":371,"url":"https://patchwork.libcamera.org/api/series/371/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=371","date":"2019-06-24T14:28:53","name":"Add support for V4L2 controls","version":6,"mbox":"https://patchwork.libcamera.org/series/371/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1506/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1506/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net\n\t[217.70.183.200])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 51B98615BC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Jun 2019 16:27:52 +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 relay7-d.mail.gandi.net (Postfix) with ESMTPSA id DDE722000E;\n\tMon, 24 Jun 2019 14:27:51 +0000 (UTC)"],"X-Originating-IP":"2.224.242.101","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 24 Jun 2019 16:28:56 +0200","Message-Id":"<20190624142859.19313-4-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190624142859.19313-1-jacopo@jmondi.org>","References":"<20190624142859.19313-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v6 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":"Mon, 24 Jun 2019 14:27:52 -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 |   9 ++\n src/libcamera/v4l2_device.cpp       | 186 ++++++++++++++++++++++++++++\n 2 files changed, 195 insertions(+)","diff":"diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 556960467e10..39a47f55acb8 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -10,11 +10,14 @@\n #include <map>\n #include <string>\n \n+#include <linux/videodev2.h>\n+\n #include \"log.h\"\n \n namespace libcamera {\n \n class V4L2ControlInfo;\n+class V4L2ControlList;\n \n class V4L2Device : protected Loggable\n {\n@@ -23,6 +26,8 @@ public:\n \tbool isOpen() const { return fd_ != -1; }\n \n \tconst V4L2ControlInfo *getControlInfo(unsigned int id) const;\n+\tint getControls(V4L2ControlList *ctrls);\n+\tint setControls(V4L2ControlList *ctrls);\n \n \tconst std::string &deviceNode() const { return deviceNode_; }\n \n@@ -38,6 +43,10 @@ protected:\n \n private:\n \tvoid listControls();\n+\tvoid updateControls(V4L2ControlList *ctrls,\n+\t\t\t    const V4L2ControlInfo **controlInfo,\n+\t\t\t    struct v4l2_ext_control *v4l2Ctrls,\n+\t\t\t    unsigned int count);\n \n \tstd::map<unsigned int, V4L2ControlInfo> controls_;\n \tstd::string deviceNode_;\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 51764b441f92..6319d1cfaaa1 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -125,6 +125,156 @@ 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+ * This method reads the value of all controls contained in \\a ctrls, and stores\n+ * their values in the corresponding \\a ctrls entry.\n+ *\n+ * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n+ * has the V4L2_CTRL_FLAG_DISABLED flag set), is a compound control, or if any\n+ * other error occurs during validation of the requested controls, no control is\n+ * read and this method returns -EINVAL.\n+ *\n+ * If an error occurs while reading the controls, the index of the first control\n+ * that couldn't be read is returned. The value of all controls below that index\n+ * are updated in \\a ctrls, while the value of all the other controls are not\n+ * changed.\n+ */\n+int V4L2Device::getControls(V4L2ControlList *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+\tfor (V4L2Control &ctrl : *ctrls) {\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 == 0 || 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\tcount = errorIdx - 1;\n+\t\tret = errorIdx;\n+\t}\n+\n+\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * \\brief Write controls to the device\n+ * \\param[in] ctrls The list of controls to write\n+ *\n+ * This method writes the value of all controls contained in \\a ctrls, and\n+ * stores the values actually applied to the device in the corresponding\n+ * \\a ctrls entry.\n+ *\n+ * If any control in \\a ctrls is not supported by the device, is disabled (i.e.\n+ * has the V4L2_CTRL_FLAG_DISABLED flag set), is read-only, is a\n+ * compound control, or if any other error occurs during validation of\n+ * the requested controls, no control is written and this method returns\n+ * -EINVAL.\n+ *\n+ * If an error occurs while writing the controls, the index of the first\n+ * control that couldn't be written is returned. All controls below that index\n+ * are written and their values are updated in \\a ctrls, while all other\n+ * controls are not written and their values are not changed.\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(V4L2ControlList *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+\tfor (V4L2Control &ctrl : *ctrls) {\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 = 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 == 0 || 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\tcount = errorIdx - 1;\n+\t\tret = errorIdx;\n+\t}\n+\n+\tupdateControls(ctrls, controlInfo, v4l2Ctrls, count);\n+\n+\treturn ret;\n+}\n+\n /**\n  * \\brief Perform an IOCTL system call on the device node\n  * \\param[in] request The IOCTL request code\n@@ -194,4 +344,40 @@ void V4L2Device::listControls()\n \t}\n }\n \n+/*\n+ * \\brief Update the value of \\a count V4L2 controls in \\a ctrls using values\n+ * in \\a v4l2Ctrls\n+ * \\param[inout] ctrls List of V4L2 controls to update\n+ * \\param[in] controlInfo List of V4L2 control information\n+ * \\param[in] v4l2Ctrls List of V4L2 extended controls as returned by the driver\n+ * \\param[in] count The number of controls to update\n+ */\n+void V4L2Device::updateControls(V4L2ControlList *ctrls,\n+\t\t\t\tconst V4L2ControlInfo **controlInfo,\n+\t\t\t\tstruct v4l2_ext_control *v4l2Ctrls,\n+\t\t\t\tunsigned int count)\n+{\n+\tunsigned int i = 0;\n+\tfor (V4L2Control &ctrl : *ctrls) {\n+\t\tstruct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n+\t\tconst V4L2ControlInfo *info = controlInfo[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(v4l2Ctrl->value);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (++i == count)\n+\t\t\tbreak;\n+\t}\n+}\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","v6","3/6"]}