{"id":1333,"url":"https://patchwork.libcamera.org/api/patches/1333/?format=json","web_url":"https://patchwork.libcamera.org/patch/1333/","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":"<20190602130435.18780-5-jacopo@jmondi.org>","date":"2019-06-02T13:04:33","name":"[libcamera-devel,4/6] libcamera: v4l2_base: Add V4L2 control support","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"dc8c47ed3e10c7b803f9a692bf72409dcc8072f2","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":{"id":15,"url":"https://patchwork.libcamera.org/api/users/15/?format=json","username":"jmondi","first_name":"Jacopo","last_name":"Mondi","email":"jacopo@jmondi.org"},"mbox":"https://patchwork.libcamera.org/patch/1333/mbox/","series":[{"id":333,"url":"https://patchwork.libcamera.org/api/series/333/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=333","date":"2019-06-02T13:04:29","name":"libcamera: v4l2_controls: Add support for V4L2 controls","version":1,"mbox":"https://patchwork.libcamera.org/series/333/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1333/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1333/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay11.mail.gandi.net (relay11.mail.gandi.net\n\t[217.70.178.231])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3C6F760BFA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  2 Jun 2019 15:03:48 +0200 (CEST)","from uno.homenet.telecomitalia.it (unknown [79.35.79.123])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay11.mail.gandi.net (Postfix) with ESMTPSA id 4992F100005;\n\tSun,  2 Jun 2019 13:03:47 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun,  2 Jun 2019 15:04:33 +0200","Message-Id":"<20190602130435.18780-5-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190602130435.18780-1-jacopo@jmondi.org>","References":"<20190602130435.18780-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 4/6] libcamera: v4l2_base: Add V4L2\n\tcontrol support","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":"Sun, 02 Jun 2019 13:03:48 -0000"},"content":"Implement operations to set and get V4L2 controls in the V4L2Base class.\nThe operations allow to set and get a single or a list of controls.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/include/v4l2_base.h |  12 ++\n src/libcamera/v4l2_base.cpp       | 290 ++++++++++++++++++++++++++++++\n 2 files changed, 302 insertions(+)","diff":"diff --git a/src/libcamera/include/v4l2_base.h b/src/libcamera/include/v4l2_base.h\nindex 2fda81a960d2..7d4e238dadfa 100644\n--- a/src/libcamera/include/v4l2_base.h\n+++ b/src/libcamera/include/v4l2_base.h\n@@ -9,6 +9,8 @@\n \n #include <vector>\n \n+#include <v4l2_controls.h>\n+\n namespace libcamera {\n \n class V4L2Base\n@@ -22,8 +24,18 @@ public:\n \tvirtual void close() = 0;\n \tbool isOpen() const;\n \n+\tstd::vector<V4L2Control *> getControls(std::vector<unsigned int> &ctrl_ids);\n+\tV4L2Control *getControl(unsigned int ctrl_id);\n+\tint setControls(std::vector<V4L2Control *> &ctrls);\n+\tint setControl(V4L2Control *ctrl);\n+\n protected:\n \tint fd_;\n+\n+private:\n+\tint queryControl(unsigned int ctrl_id,\n+\t\t\t struct v4l2_query_ext_ctrl *qext_ctrl);\n+\n };\n \n }; /* namespace libcamera */\ndiff --git a/src/libcamera/v4l2_base.cpp b/src/libcamera/v4l2_base.cpp\nindex 7d05a3c82e4d..423a4d84e276 100644\n--- a/src/libcamera/v4l2_base.cpp\n+++ b/src/libcamera/v4l2_base.cpp\n@@ -5,7 +5,13 @@\n  * v4l2_base.cpp - Common base for V4L2 devices and subdevices\n  */\n \n+#include <linux/v4l2-subdev.h>\n+#include <linux/videodev2.h>\n+#include <sys/ioctl.h>\n+\n+#include \"log.h\"\n #include \"v4l2_base.h\"\n+#include \"v4l2_controls.h\"\n \n /**\n  * \\file v4l2_base.h\n@@ -14,6 +20,8 @@\n \n namespace libcamera {\n \n+LOG_DEFINE_CATEGORY(V4L2Base)\n+\n /**\n  * \\class V4L2Base\n  * \\brief Base class for V4L2Device and V4L2Subdevice\n@@ -31,6 +39,7 @@ namespace libcamera {\n  */\n \n /**\n+ * \\fn V4L2Base::open()\n  * \\brief Open a V4L2 device or subdevice\n  *\n  * Pure virtual method to be implemented by the derived classes.\n@@ -39,6 +48,7 @@ namespace libcamera {\n  */\n \n /**\n+ * \\fn V4L2Base::close()\n  * \\brief Close the device or subdevice\n  *\n  * Pure virtual method to be implemented by the derived classes.\n@@ -53,6 +63,286 @@ bool V4L2Base::isOpen() const\n \treturn fd_ != -1;\n }\n \n+int V4L2Base::queryControl(unsigned int id,\n+\t\t\t   struct v4l2_query_ext_ctrl *qext_ctrl)\n+{\n+\tqext_ctrl->id = id;\n+\n+\tint ret = ioctl(fd_, VIDIOC_QUERY_EXT_CTRL, qext_ctrl);\n+\tif (ret) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2Base, Error)\n+\t\t\t<< \"Unable to query extended control 0x\"\n+\t\t\t<< std::hex << qext_ctrl->id\n+\t\t\t<< \": \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tif (qext_ctrl->flags & V4L2_CTRL_FLAG_DISABLED) {\n+\t\tLOG(V4L2Base, Error)\n+\t\t\t<< \"Extended control 0x\" << std::hex\n+\t\t\t<< qext_ctrl->id << \" is disabled\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Get values of a list of control IDs\n+ * \\param ctrl_ids The list of control IDs to retrieve value of\n+ * \\return A list of V4L2Control on success or a empty list otherwise\n+ */\n+std::vector<V4L2Control *> V4L2Base::getControls(std::vector<unsigned int> &ctrl_ids)\n+{\n+\tunsigned int count = ctrl_ids.size();\n+\tif (!count)\n+\t\treturn std::vector<V4L2Control *> {};\n+\n+\tstruct v4l2_ext_control *v4l2_ctrls = static_cast<struct v4l2_ext_control *>\n+\t\t\t\t\t      (new struct v4l2_ext_control[count]);\n+\tif (!v4l2_ctrls)\n+\t\treturn std::vector<V4L2Control *> {};\n+\n+\t/*\n+\t * Query each control in the vector to get back its type and expected\n+\t * size in order to get its current value.\n+\t */\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2_ctrl = &v4l2_ctrls[i];\n+\t\tunsigned int ctrl_id = ctrl_ids[i];\n+\t\tstruct v4l2_query_ext_ctrl qext_ctrl = {};\n+\n+\t\tint ret = queryControl(ctrl_id, &qext_ctrl);\n+\t\tif (ret)\n+\t\t\treturn std::vector<V4L2Control *> {};\n+\n+\t\tv4l2_ctrl->id = ctrl_id;\n+\t\tv4l2_ctrl->size = qext_ctrl.elem_size * qext_ctrl.elems;\n+\n+\t\t/* Reserve memory for controls with payload. */\n+\t\tif (!(qext_ctrl.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD))\n+\t\t\tcontinue;\n+\n+\t\tswitch (qext_ctrl.type) {\n+\t\tcase V4L2_CTRL_TYPE_U8:\n+\t\t\tv4l2_ctrl->p_u8 = static_cast<uint8_t *>\n+\t\t\t\t\t  (new uint8_t[v4l2_ctrl->size]);\n+\t\t\tbreak;\n+\t\tcase V4L2_CTRL_TYPE_U16:\n+\t\t\tv4l2_ctrl->p_u16 = static_cast<uint16_t *>\n+\t\t\t\t\t  (new uint16_t[v4l2_ctrl->size]);\n+\t\t\tbreak;\n+\t\tcase V4L2_CTRL_TYPE_U32:\n+\t\t\tv4l2_ctrl->p_u32 = static_cast<uint32_t *>\n+\t\t\t\t\t  (new uint32_t[v4l2_ctrl->size]);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tstruct v4l2_ext_controls v4l2_ext_ctrls = {};\n+\tv4l2_ext_ctrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n+\tv4l2_ext_ctrls.count = count;\n+\tv4l2_ext_ctrls.controls = v4l2_ctrls;\n+\n+\tint ret = ioctl(fd_, VIDIOC_G_EXT_CTRLS, &v4l2_ext_ctrls);\n+\tif (ret) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2Base, Error)\n+\t\t\t<< \"Unable to get extended controls: \" << strerror(-ret);\n+\t\t\treturn std::vector<V4L2Control *> {};\n+\t}\n+\n+\t/*\n+\t * For each requested control, create a V4L2Control which contains\n+\t * the current value and store it in the vector returned to the caller.\n+\t */\n+\tstd::vector<V4L2Control *> controls = {};\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2_ctrl = &v4l2_ctrls[i];\n+\t\tstruct v4l2_query_ext_ctrl qext_ctrl = {};\n+\n+\t\tint ret = queryControl(v4l2_ctrl->id, &qext_ctrl);\n+\t\tif (ret)\n+\t\t\treturn std::vector<V4L2Control *> {};\n+\n+\t\tunsigned int size = qext_ctrl.elem_size * qext_ctrl.elem_size;\n+\t\tif (qext_ctrl.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {\n+\t\t\t/* Control types with payload */\n+\t\t\tswitch (qext_ctrl.type) {\n+\t\t\tcase V4L2_CTRL_TYPE_U8:\n+\t\t\t{\n+\t\t\t\tV4L2U8Control *c =\n+\t\t\t\t\tnew V4L2U8Control(v4l2_ctrl->id, size,\n+\t\t\t\t\t\t\t  v4l2_ctrl->p_u8);\n+\t\t\t\tcontrols.push_back(c);\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_U16:\n+\t\t\t{\n+\t\t\t\tV4L2U16Control *c =\n+\t\t\t\t\tnew V4L2U16Control(v4l2_ctrl->id, size,\n+\t\t\t\t\t\t\t   v4l2_ctrl->p_u16);\n+\t\t\t\tcontrols.push_back(c);\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_U32:\n+\t\t\t{\n+\t\t\t\tV4L2U32Control *c =\n+\t\t\t\t\tnew V4L2U32Control(v4l2_ctrl->id, size,\n+\t\t\t\t\t\t\t  v4l2_ctrl->p_u32);\n+\t\t\t\tcontrols.push_back(c);\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tLOG(V4L2Base, Error)\n+\t\t\t\t\t<< \"Compound control types not supported: \"\n+\t\t\t\t\t<< std::hex << qext_ctrl.type;\n+\t\t\t\treturn std::vector<V4L2Control *> {};\n+\t\t\t}\n+\t\t} else {\n+\t\t\t/* Control types without payload. */\n+\t\t\tswitch (qext_ctrl.type) {\n+\t\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\t{\n+\t\t\t\tV4L2Int64Control *c =\n+\t\t\t\t\tnew V4L2Int64Control(v4l2_ctrl->id,\n+\t\t\t\t\t\t\t     v4l2_ctrl->value64);\n+\t\t\t\tcontrols.push_back(c);\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_INTEGER:\n+\t\t\tcase V4L2_CTRL_TYPE_BOOLEAN:\n+\t\t\tcase V4L2_CTRL_TYPE_MENU:\n+\t\t\tcase V4L2_CTRL_TYPE_BUTTON:\n+\t\t\t{\n+\t\t\t\tV4L2IntControl *c =\n+\t\t\t\t\tnew V4L2IntControl(v4l2_ctrl->id,\n+\t\t\t\t\t\t\t   v4l2_ctrl->value);\n+\t\t\t\tcontrols.push_back(c);\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tLOG(V4L2Base, Error)\n+\t\t\t\t\t<< \"Invalid non-compound control type :\"\n+\t\t\t\t\t<< std::hex << qext_ctrl.type;\n+\t\t\t\treturn std::vector<V4L2Control *> {};\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\treturn controls;\n+}\n+\n+/**\n+ * \\brief Get the value of control with id \\a ctrl_id\n+ * \\param ctrl_id The control id\n+ * \\return The V4L2Control which represent the value of the control on success\n+ * nullptr otherwise\n+ */\n+V4L2Control *V4L2Base::getControl(unsigned int ctrl_id)\n+{\n+\tstd::vector<unsigned int> v = { ctrl_id, };\n+\tstd::vector<V4L2Control *> ctrls = getControls(v);\n+\n+\tif (ctrls.empty())\n+\t\treturn nullptr;\n+\n+\treturn ctrls[0];\n+}\n+\n+/**\n+ * \\brief Apply a list of controls to the device\n+ * \\param ctrls The list of controls to apply\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+int V4L2Base::setControls(std::vector<V4L2Control *> &ctrls)\n+{\n+\tunsigned int count = ctrls.size();\n+\tif (!count)\n+\t\treturn -EINVAL;\n+\n+\tstruct v4l2_ext_control *v4l2_ctrls = static_cast<struct v4l2_ext_control *>\n+\t\t\t\t\t      (new struct v4l2_ext_control[count]);\n+\tif (!v4l2_ctrls)\n+\t\treturn -ENOMEM;\n+\n+\t/*\n+\t * Query each control in the vector to get back its type and expected\n+\t * size and set the new desired value in each v4l2_ext_control member.\n+\t */\n+\tfor (unsigned int i = 0; i < count; ++i) {\n+\t\tstruct v4l2_ext_control *v4l2_ctrl = &v4l2_ctrls[i];\n+\t\tstruct v4l2_query_ext_ctrl qext_ctrl = {};\n+\t\tV4L2Control *ctrl = ctrls[i];\n+\n+\t\tint ret = queryControl(ctrl->id(), &qext_ctrl);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tv4l2_ctrl->id = ctrl->id();\n+\t\tv4l2_ctrl->size = qext_ctrl.elem_size * qext_ctrl.elems;\n+\n+\t\tif (qext_ctrl.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {\n+\t\t\t/** \\todo: Support set controls with payload. */\n+\t\t\tLOG(V4L2Base, Error)\n+\t\t\t\t<< \"Controls with payload not supported\";\n+\t\t\treturn -EINVAL;\n+\t\t} else {\n+\t\t\tswitch (qext_ctrl.type) {\n+\t\t\tcase V4L2_CTRL_TYPE_INTEGER64:\n+\t\t\t{\n+\t\t\t\tV4L2Int64Control *c =\n+\t\t\t\t\tstatic_cast<V4L2Int64Control *>(ctrl);\n+\t\t\t\tv4l2_ctrl->value64 = c->value();\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_INTEGER:\n+\t\t\tcase V4L2_CTRL_TYPE_BOOLEAN:\n+\t\t\tcase V4L2_CTRL_TYPE_MENU:\n+\t\t\tcase V4L2_CTRL_TYPE_BUTTON:\n+\t\t\t{\n+\t\t\t\tV4L2IntControl *c =\n+\t\t\t\t\tstatic_cast<V4L2IntControl *>(ctrl);\n+\t\t\t\tv4l2_ctrl->value = c->value();\n+\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tLOG(V4L2Base, Error)\n+\t\t\t\t\t<< \"Invalid non-compound control type :\"\n+\t\t\t\t\t<< qext_ctrl.type;\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tstruct v4l2_ext_controls v4l2_ext_ctrls = {};\n+\tv4l2_ext_ctrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n+\tv4l2_ext_ctrls.count = count;\n+\tv4l2_ext_ctrls.controls = v4l2_ctrls;\n+\n+\tint ret = ioctl(fd_, VIDIOC_S_EXT_CTRLS, &v4l2_ext_ctrls);\n+\tif (ret) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2Base, Error)\n+\t\t\t<< \"Unable to set extended controls: \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Apply the control \\a ctrl to the device\n+ * \\param ctrl The control to apply to the device\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int V4L2Base::setControl(V4L2Control *ctrl)\n+{\n+\tstd::vector<V4L2Control *> v = { ctrl, };\n+\treturn setControls(v);\n+}\n+\n /**\n  * \\var V4L2Base::fd_\n  * \\brief The V4L2 device or subdevice device node file descriptor\n","prefixes":["libcamera-devel","4/6"]}