[{"id":2020,"web_url":"https://patchwork.libcamera.org/comment/2020/","msgid":"<20190625114905.GB5002@pendragon.ideasonboard.com>","date":"2019-06-25T11:49:05","subject":"Re: [libcamera-devel] [PATCH v7 3/6] libcamera: v4l2_device:\n\tImplement get and set controls","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Tue, Jun 25, 2019 at 08:48:27AM +0200, Jacopo Mondi wrote:\n> Implement getControls() and setControls() operations in V4L2Device class.\n> \n> Both operations take a V4L2Controls instance and read or write the V4L2\n> controls on the V4L2 device.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/libcamera/include/v4l2_device.h |   9 ++\n>  src/libcamera/v4l2_device.cpp       | 190 ++++++++++++++++++++++++++++\n>  2 files changed, 199 insertions(+)\n> \n> diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\n> index 556960467e10..24a0134a2cba 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    const struct v4l2_ext_control *v4l2Ctrls,\n> +\t\t\t    unsigned int count);\n>  \n>  \tstd::map<unsigned int, V4L2ControlInfo> controls_;\n>  \tstd::string deviceNode_;\n> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> index 51764b441f92..84758a811c27 100644\n> --- a/src/libcamera/v4l2_device.cpp\n> +++ b/src/libcamera/v4l2_device.cpp\n> @@ -125,6 +125,163 @@ 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> + * \\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(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 (unsigned int i = 0; i < count; ++i) {\n> +\t\tconst V4L2Control *ctrl = ctrls->getByIndex(i);\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> +\n> +\tfor (unsigned int i = 0; i < count; ++i) {\n> +\t\tconst V4L2Control *ctrl = ctrls->getByIndex(i);\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 +351,37 @@ void V4L2Device::listControls()\n>  \t}\n>  }\n>  \n> +/*\n> + * \\brief Update the value of the first \\a count V4L2 controls in \\a ctrls using\n> + * values 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\tconst struct v4l2_ext_control *v4l2Ctrls,\n> +\t\t\t\tunsigned int count)\n> +{\n> +\tfor (unsigned int i = 0; i < count; ++i) {\n> +\t\tconst struct v4l2_ext_control *v4l2Ctrl = &v4l2Ctrls[i];\n> +\t\tconst V4L2ControlInfo *info = controlInfo[i];\n> +\t\tV4L2Control *ctrl = ctrls->getByIndex(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> +\t}\n> +}\n> +\n>  } /* namespace libcamera */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0603460C29\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 25 Jun 2019 13:49:25 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7D75D510;\n\tTue, 25 Jun 2019 13:49:24 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1561463364;\n\tbh=7jbi2blIqGlvSVBNegfHeGyXXnVcG9BedYhuR5PgkwI=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=p0Vp9yaqBkYXqJe82IcklHyMpekk/xp1XreJCXdafEPdBsB9/CNUc+QJbB6KXT9Za\n\tvFni9iM7cSS2ULmNnOGswvJQ20637fgkXdzVKYSQaYMliKJjjZG8A3d/bj/Y7UkFjw\n\t8QbUsWCDuaZVz2EFaDO0pvfnkGpn2H8+MhM0VfrY=","Date":"Tue, 25 Jun 2019 14:49:05 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190625114905.GB5002@pendragon.ideasonboard.com>","References":"<20190625064830.951-1-jacopo@jmondi.org>\n\t<20190625064830.951-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190625064830.951-4-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v7 3/6] libcamera: v4l2_device:\n\tImplement get 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":"Tue, 25 Jun 2019 11:49:25 -0000"}}]