Show a patch.

GET /api/1.1/patches/1385/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1385,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/1385/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/1385/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20190610164052.30741-5-jacopo@jmondi.org>",
    "date": "2019-06-10T16:40:50",
    "name": "[libcamera-devel,v2,4/6] libcamera: v4l2_base: Add V4L2 control support",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "cd7587fdb20c5302fc332951ea20f67cf1d7ef82",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/1385/mbox/",
    "series": [
        {
            "id": 344,
            "url": "https://patchwork.libcamera.org/api/1.1/series/344/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=344",
            "date": "2019-06-10T16:40:46",
            "name": "Add support for V4L2 Controls",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/344/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/1385/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/1385/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<jacopo@jmondi.org>",
        "Received": [
            "from relay10.mail.gandi.net (relay10.mail.gandi.net\n\t[217.70.178.230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1F158635E7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 10 Jun 2019 18:39:48 +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 relay10.mail.gandi.net (Postfix) with ESMTPSA id A204A240008;\n\tMon, 10 Jun 2019 16:39:47 +0000 (UTC)"
        ],
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Mon, 10 Jun 2019 18:40:50 +0200",
        "Message-Id": "<20190610164052.30741-5-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.21.0",
        "In-Reply-To": "<20190610164052.30741-1-jacopo@jmondi.org>",
        "References": "<20190610164052.30741-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 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": "Mon, 10 Jun 2019 16:39:49 -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 |  11 ++\n src/libcamera/v4l2_base.cpp       | 238 ++++++++++++++++++++++++++++++\n 2 files changed, 249 insertions(+)",
    "diff": "diff --git a/src/libcamera/include/v4l2_base.h b/src/libcamera/include/v4l2_base.h\nindex 2fda81a960d2..17ea734c8f49 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,17 @@ public:\n \tvirtual void close() = 0;\n \tbool isOpen() const;\n \n+\tint getControls(std::vector<unsigned int> &ctrl_ids,\n+\t\t\tV4L2Controls *ctrls);\n+\tint setControls(V4L2Controls &ctrls);\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..cbd7a551130f 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,234 @@ 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+int V4L2Base::getControls(std::vector<unsigned int> &ctrl_ids,\n+\t\t\t  V4L2Controls *controls)\n+{\n+\tunsigned int count = ctrl_ids.size();\n+\tif (!count)\n+\t\treturn -EINVAL;\n+\n+\tcontrols->clear();\n+\n+\tstruct v4l2_ext_control *v4l2_ctrls = new struct v4l2_ext_control[count];\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 ret;\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\treturn ret;\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+\tLOG(Error) << __func__;\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 ret;\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\tcontrols->set(v4l2_ctrl->id, size,\n+\t\t\t\t\t      v4l2_ctrl->p_u8);\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_U16:\n+\t\t\t\tcontrols->set(v4l2_ctrl->id, size,\n+\t\t\t\t\t      v4l2_ctrl->p_u16);\n+\t\t\t\tbreak;\n+\t\t\tcase V4L2_CTRL_TYPE_U32:\n+\t\t\t\tcontrols->set(v4l2_ctrl->id, size,\n+\t\t\t\t\t      v4l2_ctrl->p_u32);\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 -EINVAL;\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\tcontrols->set(v4l2_ctrl->id, static_cast<int64_t>\n+\t\t\t\t\t     (v4l2_ctrl->value64));\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\tcontrols->set(v4l2_ctrl->id, v4l2_ctrl->value);\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 -EINVAL;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\treturn 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(V4L2Controls &ctrls)\n+{\n+\tunsigned int count = ctrls.size();\n+\tif (!count)\n+\t\treturn -EINVAL;\n+\n+\tstruct v4l2_ext_control *v4l2_ctrls = new struct v4l2_ext_control[count];\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 controls: \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\var V4L2Base::fd_\n  * \\brief The V4L2 device or subdevice device node file descriptor\n",
    "prefixes": [
        "libcamera-devel",
        "v2",
        "4/6"
    ]
}