From patchwork Wed Apr 26 08:29:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677650 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5DFCC7618E for ; Wed, 26 Apr 2023 08:30:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239789AbjDZIaZ (ORCPT ); Wed, 26 Apr 2023 04:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjDZIaW (ORCPT ); Wed, 26 Apr 2023 04:30:22 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 679383A94 for ; Wed, 26 Apr 2023 01:30:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b9229dd8c62so7589235276.0 for ; Wed, 26 Apr 2023 01:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497820; x=1685089820; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aIZN8WkdQsWMJh4E/TvPA7aIY8MRdA1afAPZxigEuD8=; b=k/TMmhCFSWzkJWQTI/bvweC0nmcBw6uKa1LA/fAHMSnTKYek1bsRIPTdQz1vALQYI7 mfU1GVEreawwslF6aKtqQl5TCm4LJtxD3kMIMr/TGreC81JcZcWkGFCQkAjQcs47ACRz V8q4EfP1NkDgoesH9Kzd6SdGEYxQIClBPKQLdlBk/xjJnh5CoiExWyv4SKPF8uJebU5c FV89DQbDRdIbDgVIbXti5d5SFuC5qwQYKE500j8S9CNt+KtoEK2vUuZAV1Rrq9v3XRwI 4fJzo5FNKqZISOZPrm0WjQB7INvVAGVAu+kvH5kt4UebeeXY9YN0QW7YBaZtiJBNTqve TMhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497820; x=1685089820; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aIZN8WkdQsWMJh4E/TvPA7aIY8MRdA1afAPZxigEuD8=; b=hbtQvi7Sg/3m61KgZZwhT3Ojw7+hgAfMEh463Wjo6N6PwOLxJ3mbRD9+4U9uOxMDlZ f0ve3fDY5LHWDzJlCFbKW+Q/izon+aKnjzzD0J92DKwF9XITPFo74G9yENJMZPpMOWg7 bUADzFtujqniYF1yp97EyWE+AY30YPQ5Fz9vkOD0pN5tmw9CfNbeyYMIBUPZkOpAn9Fk TdDQ7dT/g3JdbUK80VY3sfZo7BbELEzC8Jorm+QoumueH/e5+/1IjncQFv49iwB4dHnJ bXlikPpvdvub1/wjAqYNLvNKOGnDV9lL+Ugn6CVhm4zBhFy8B3D8URniMVPa9yg4SqTn mqJw== X-Gm-Message-State: AAQBX9euUvKU7N0jg6xWdFP+9IfWbFUXKSmRQ2MpnpbnEcVmd77QeLyA PxIN6yQcjGNEKEkE7j+lwSQhERckstw= X-Google-Smtp-Source: AKy350YxIL/oOtLEG5rB824j73vHDrF80dcn5WHOIHYnxWbWNeMTkbjp0G4PvujHWqmPZUjiglqeVfzUydM= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:e0d6:0:b0:b6a:5594:5936 with SMTP id x205-20020a25e0d6000000b00b6a55945936mr11241864ybg.5.1682497820684; Wed, 26 Apr 2023 01:30:20 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:13 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-2-yunkec@google.com> Subject: [PATCH v11 01/11] media: v4l2_ctrl: Add V4L2_CTRL_TYPE_RECT From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add p_rect to struct v4l2_ext_control with basic support in v4l2-ctrls. Reviewed-by: Laurent Pinchart Reviewed-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky Reviewed-by: Daniel Scally Signed-off-by: Yunke Cao Reviewed-by: Hans Verkuil --- Changelog since v10: - Added reviewed-by from Sergey and Daniel. Changelog since v9: - No Change. Changelog since v8: - No change. Changelog since v7: - Document V4L2_CTRL_TYPE_RECT in vidioc-queryctrl.rst. - Rebased to media-stage master. - Do not assign each field in std_equal .../media/v4l/vidioc-g-ext-ctrls.rst | 4 ++++ .../userspace-api/media/v4l/vidioc-queryctrl.rst | 7 +++++++ .../media/videodev2.h.rst.exceptions | 1 + drivers/media/v4l2-core/v4l2-ctrls-core.c | 15 +++++++++++++++ include/media/v4l2-ctrls.h | 2 ++ include/uapi/linux/videodev2.h | 2 ++ 6 files changed, 31 insertions(+) diff --git a/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst b/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst index 892cfeb8b988..927ef397f1ce 100644 --- a/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst +++ b/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst @@ -189,6 +189,10 @@ still cause this situation. - ``p_area`` - A pointer to a struct :c:type:`v4l2_area`. Valid if this control is of type ``V4L2_CTRL_TYPE_AREA``. + * - struct :c:type:`v4l2_rect` * + - ``p_rect`` + - A pointer to a struct :c:type:`v4l2_rect`. Valid if this control is + of type ``V4L2_CTRL_TYPE_RECT``. * - struct :c:type:`v4l2_ctrl_h264_sps` * - ``p_h264_sps`` - A pointer to a struct :c:type:`v4l2_ctrl_h264_sps`. Valid if this control is diff --git a/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst b/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst index a20dfa2a933b..58982cd382e3 100644 --- a/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst +++ b/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst @@ -441,6 +441,13 @@ See also the examples in :ref:`control`. - n/a - A struct :c:type:`v4l2_area`, containing the width and the height of a rectangular area. Units depend on the use case. + * - ``V4L2_CTRL_TYPE_RECT`` + - n/a + - n/a + - n/a + - A struct :c:type:`v4l2_rect`, containing a rectangle described by + the position of its top-left corner, the width and the height. Units + depend on the use case. * - ``V4L2_CTRL_TYPE_H264_SPS`` - n/a - n/a diff --git a/Documentation/userspace-api/media/videodev2.h.rst.exceptions b/Documentation/userspace-api/media/videodev2.h.rst.exceptions index 2a589d34b80e..828cca8e2daa 100644 --- a/Documentation/userspace-api/media/videodev2.h.rst.exceptions +++ b/Documentation/userspace-api/media/videodev2.h.rst.exceptions @@ -150,6 +150,7 @@ replace symbol V4L2_CTRL_TYPE_HEVC_SPS :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_HEVC_PPS :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_AREA :c:type:`v4l2_ctrl_type` +replace symbol V4L2_CTRL_TYPE_RECT :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_FWHT_PARAMS :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_VP8_FRAME :c:type:`v4l2_ctrl_type` replace symbol V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR :c:type:`v4l2_ctrl_type` diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c index 29169170880a..e7f232842376 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-core.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c @@ -350,6 +350,11 @@ void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl) case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS: pr_cont("HEVC_DECODE_PARAMS"); break; + case V4L2_CTRL_TYPE_RECT: + pr_cont("%ux%u@%dx%d", + ptr.p_rect->width, ptr.p_rect->height, + ptr.p_rect->left, ptr.p_rect->top); + break; default: pr_cont("unknown type %d", ctrl->type); break; @@ -569,6 +574,7 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx, struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering; struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params; struct v4l2_area *area; + struct v4l2_rect *rect; void *p = ptr.p + idx * ctrl->elem_size; unsigned int i; @@ -918,6 +924,12 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx, return -EINVAL; break; + case V4L2_CTRL_TYPE_RECT: + rect = p; + if (!rect->width || !rect->height) + return -EINVAL; + break; + default: return -EINVAL; } @@ -1605,6 +1617,9 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, case V4L2_CTRL_TYPE_AREA: elem_size = sizeof(struct v4l2_area); break; + case V4L2_CTRL_TYPE_RECT: + elem_size = sizeof(struct v4l2_rect); + break; default: if (type < V4L2_CTRL_COMPOUND_TYPES) elem_size = sizeof(s32); diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h index e59d9a234631..1846caf9dd53 100644 --- a/include/media/v4l2-ctrls.h +++ b/include/media/v4l2-ctrls.h @@ -52,6 +52,7 @@ struct video_device; * @p_hdr10_cll: Pointer to an HDR10 Content Light Level structure. * @p_hdr10_mastering: Pointer to an HDR10 Mastering Display structure. * @p_area: Pointer to an area. + * @p_rect: Pointer to a rectangle. * @p: Pointer to a compound value. * @p_const: Pointer to a constant compound value. */ @@ -81,6 +82,7 @@ union v4l2_ctrl_ptr { struct v4l2_ctrl_hdr10_cll_info *p_hdr10_cll; struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering; struct v4l2_area *p_area; + struct v4l2_rect *p_rect; void *p; const void *p_const; }; diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h index 17a9b975177a..ce2bd9ac4c47 100644 --- a/include/uapi/linux/videodev2.h +++ b/include/uapi/linux/videodev2.h @@ -1797,6 +1797,7 @@ struct v4l2_ext_control { __u32 __user *p_s32; __u32 __user *p_s64; struct v4l2_area __user *p_area; + struct v4l2_rect __user *p_rect; struct v4l2_ctrl_h264_sps __user *p_h264_sps; struct v4l2_ctrl_h264_pps *p_h264_pps; struct v4l2_ctrl_h264_scaling_matrix __user *p_h264_scaling_matrix; @@ -1861,6 +1862,7 @@ enum v4l2_ctrl_type { V4L2_CTRL_TYPE_U16 = 0x0101, V4L2_CTRL_TYPE_U32 = 0x0102, V4L2_CTRL_TYPE_AREA = 0x0106, + V4L2_CTRL_TYPE_RECT = 0x0107, V4L2_CTRL_TYPE_HDR10_CLL_INFO = 0x0110, V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY = 0x0111, From patchwork Wed Apr 26 08:29:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677450 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C140C7618E for ; Wed, 26 Apr 2023 08:30:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239817AbjDZIa3 (ORCPT ); Wed, 26 Apr 2023 04:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjDZIa1 (ORCPT ); Wed, 26 Apr 2023 04:30:27 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B5DF3A94 for ; Wed, 26 Apr 2023 01:30:26 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b9968fb4a8cso6854611276.0 for ; Wed, 26 Apr 2023 01:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497825; x=1685089825; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3VvY5qMZLce/da5NSM2FURd5mXhraxzBD6mRNX76xh0=; b=enpIaLYGUVGM198WkXSzhzOiWXugnuRPVI5tVWhUg6MOrfKm2pCz3rIbgHEOhEtdK6 aIgjMBKUwaewkaoNYecLSDEs8dUtMhn1m5iDyfBZFp+p8QvFF9AQc0QxLzx46a8VDB9I qAU+BV+5Ike2wIoCPWvHEhHfmyBCey4L0pZLibQJqO7JqOe8QtmXsNlM8U/3y6Pb2JSR 7IX3xa24oRXayNIWo1etc4a8JDePFiYjt5nrC8+qf1dXdG2YOfztqor8onEBqfLme9AK FiMrAEGONkDJkCoMITFPo7j1ai0Ch5W92mk9tEC6UbJmpyxofYpiWw4XoKJMsVBvbnDi 3dlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497825; x=1685089825; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3VvY5qMZLce/da5NSM2FURd5mXhraxzBD6mRNX76xh0=; b=LABXCeZRiKcznxnutXLu4wtQzhgLKYPNWmu2Jxd0n8eNeqDUTLKWBlTlQCJzJFzwaS Ju3B7qN/w+GIRIihx2UsKfSqe5DxAqTDmbB5K6BUVu4PKl6VovJYSMJZCMq6blrDeJC0 w2fqNp4ZkHwN94/M7OTMOfIHpSTow+8kv8n+sjdaS/K/NVHBhT2peGQ+l5ToMh3krkb0 xUNjpaFdS45xMQo4DnfLHS2rqWd3bx6c16BVE8VoXLEHEA9AZ4sCE+bb8lCk2XHqtGlO Uxhtdk246nbANrhQmCH1cWJvbr/rsiV+e8bRMX5c2PTogBICO17AmY1P1Nd4m2ftWwPJ Yqyg== X-Gm-Message-State: AAQBX9fIY20PSFBKpoQ8hydWHYeUBHq/OddOSaUCdIdztd5StB8OSUJY SRzKjDwNV1nEPUTvkPBhg5Hsro5LmHQ= X-Google-Smtp-Source: AKy350a9bJfq6YugdrRNYS1bx4C+G62/9SnypRjBlqge/XzOTDxeLbcyZym1dux738BkY2A6HgNF6plp0L4= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:d144:0:b0:b95:bdfb:df09 with SMTP id i65-20020a25d144000000b00b95bdfbdf09mr7727252ybg.7.1682497825496; Wed, 26 Apr 2023 01:30:25 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:14 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-3-yunkec@google.com> Subject: [PATCH v11 02/11] media: uvcvideo: add uvc_ctrl_get_boundary for getting default value From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Introduce uvc_ctrl_get_boundary(), making it easier to extend to support compound controls and V4L2_CTRL_WHICH_MIN/MAX_VAL in the following patches. For now, it simply returns EINVAL for compound controls and calls __query_v4l2_ctrl() for non-compound controls. Reviewed-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky Signed-off-by: Yunke Cao --- Changelog since v10: - Fix __uvc_ctrl_get_boundary_std() typo causing build error. - Added Reviewed-by from Sergey. Changelog since v9: - Make __uvc_ctrl_get_boundary_std() static. Changelog since v8: - No Change. Changelog since v7: - Rename uvc_ctrl_get_fixed() to uvc_ctrl_get_boundary(). - Move refactor (introduce __uvc_ctrl_get_boundary_std()) in this patch instead of in the patch where we implement compound control. - Fix locking. uvc_ctrl_get_boundary() now acquires ctrl_mutex. __uvc_ctrl_get_boundary_std() calls __uvc_query_v4l2_ctrl() while holding the mutex. - Define a uvc_ctrl_mapping_is_compound() for readability. drivers/media/usb/uvc/uvc_ctrl.c | 48 ++++++++++++++++++++++++++++++++ drivers/media/usb/uvc/uvc_v4l2.c | 6 +--- drivers/media/usb/uvc/uvcvideo.h | 2 ++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 5e9d3da862dd..4a4ceb6289f5 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -884,6 +884,12 @@ static void uvc_set_le_value(struct uvc_control_mapping *mapping, } } +static bool +uvc_ctrl_mapping_is_compound(const struct uvc_control_mapping *mapping) +{ + return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES; +} + /* ------------------------------------------------------------------------ * Terminal and unit management */ @@ -1877,6 +1883,48 @@ int uvc_ctrl_get(struct uvc_video_chain *chain, return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); } +static int __uvc_ctrl_get_boundary_std(struct uvc_video_chain *chain, + struct uvc_control *ctrl, + struct uvc_control_mapping *mapping, + struct v4l2_ext_control *xctrl) +{ + struct v4l2_queryctrl qc = { .id = xctrl->id }; + + int ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &qc); + + if (ret < 0) + return ret; + + xctrl->value = qc.default_value; + return 0; +} + +int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, + struct v4l2_ext_control *xctrl) +{ + struct uvc_control *ctrl; + struct uvc_control_mapping *mapping; + int ret; + + if (mutex_lock_interruptible(&chain->ctrl_mutex)) + return -ERESTARTSYS; + + ctrl = uvc_find_control(chain, xctrl->id, &mapping); + if (!ctrl) { + ret = -EINVAL; + goto done; + } + + if (uvc_ctrl_mapping_is_compound(mapping)) + ret = -EINVAL; + else + ret = __uvc_ctrl_get_boundary_std(chain, ctrl, mapping, xctrl); + +done: + mutex_unlock(&chain->ctrl_mutex); + return ret; +} + int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl) { diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 35453f81c1d9..0ee3932ec33a 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -1095,15 +1095,11 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) { for (i = 0; i < ctrls->count; ++ctrl, ++i) { - struct v4l2_queryctrl qc = { .id = ctrl->id }; - - ret = uvc_query_v4l2_ctrl(chain, &qc); + ret = uvc_ctrl_get_boundary(chain, ctrl); if (ret < 0) { ctrls->error_idx = i; return ret; } - - ctrl->value = qc.default_value; } return 0; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 9a596c8d894a..87222dd8920e 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -781,6 +781,8 @@ static inline int uvc_ctrl_rollback(struct uvc_fh *handle) } int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl); +int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, + struct v4l2_ext_control *xctrl); int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, const struct v4l2_ext_controls *ctrls, From patchwork Wed Apr 26 08:29:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677649 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 023A3C7618E for ; Wed, 26 Apr 2023 08:30:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240083AbjDZIae (ORCPT ); Wed, 26 Apr 2023 04:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240033AbjDZIac (ORCPT ); Wed, 26 Apr 2023 04:30:32 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E6833C39 for ; Wed, 26 Apr 2023 01:30:31 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b92309d84c1so33625891276.1 for ; Wed, 26 Apr 2023 01:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497830; x=1685089830; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1hIp5mYZdj0KUTMM3jiGLfDWgzgFP0+qMCQ75klt6RA=; b=gco/yaljPrlZFch8My0p513IbVr+1SrxD6QAMqqr7jxoEslynQBM0hAYTI7TXAZAuY cUNxJGD2YVJjUwy0FjjddbUdHePjpm4vbl3MuqkSRCgs+0b9S6tslJPddJ65GArj/qxE wMd2loQ0go8oUl3tFpxIu+9FnIBdOLG0dwFYbaWMNqDXntYBWfFcwwwpkceLGOoADqpg ssHnCAeYyF16zioJVDpkpxkaOeKExd6lp2//2hmDpn/uo2AuR/7uLarzOhu0oyLKFFhm 2xdbzTl00JJ8+mdfufwir7XVSqCreZmFVgqTUJdMa+675D7Oaxpo+Ab5v9baW0Y0MqXA kHiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497830; x=1685089830; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1hIp5mYZdj0KUTMM3jiGLfDWgzgFP0+qMCQ75klt6RA=; b=UictSVNfS3okelg4QL/lyCSt5VcvADyXpspp/3WV9HTwXB3owMa/1xT8aHhMwWCKFf X5jiD5caO9D2YfMHrTc92FYfIqwrWSN+4M5uXZ1aDVf0T1RPHLjua3pmfwohJZq40Tqy D0K5/I5g3Oo+aG9tedmp5TlK+MRgC+bP69/7plxc3RwVP5IHvkL2QsF6a67TToGvjQLZ IvtuOh38prYiSfUn5paZSJl2ahb9rv++tu1MTlA4v39C7kGpQYVctWcsmi3wv/ZeK2pP PgBxD/k+/GDM/luhDywRysbMdBwY2i0VuTX2HuYk6VNPm6u9EsOSDXyjKZhGSQlQ3mha WTMQ== X-Gm-Message-State: AC+VfDx0xiNAhirhs3d7ed/ic11TF4I13MSe3VLcP99uxe2kXKD0hz2J MN9W1M0NebtzQoG9VcKNdsBXIpCbCSw= X-Google-Smtp-Source: ACHHUZ6BHAR7fp2+EukKY6YESnegGOYPoSN7zFJXI7b9kchlFzpLw5vuFnhpX/AAeRi9AuEnmo/pAip8sME= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a81:7e0f:0:b0:556:c670:941d with SMTP id o15-20020a817e0f000000b00556c670941dmr1070272ywn.4.1682497830616; Wed, 26 Apr 2023 01:30:30 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:15 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-4-yunkec@google.com> Subject: [PATCH v11 03/11] media: uvcvideo: introduce __uvc_ctrl_get_std() From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Refactor uvc_ctrl to make adding compound control easier. Currently __uvc_ctrl_get() only work for non-compound controls. Move the logic into __uvc_ctrl_std(), return error for compound controls. Make __uvc_ctrl_get() call __uvc_ctrl_std() inside. Also modify some of the callers of __uvc_ctrl_get() to call __uvc_ctrl_get_std() instead. Signed-off-by: Yunke Cao --- Changelog since v10: - Better commit message. Changelog since v9: - No change. Changelog since v8: - No change. Changelog since v7: - Newly added patch. Split the refactoring of uvc_ctrl_get from v7 3/7. drivers/media/usb/uvc/uvc_ctrl.c | 42 +++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 4a4ceb6289f5..c4a9ea461eb0 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1081,15 +1081,15 @@ static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain, return ret; } -static int __uvc_ctrl_get(struct uvc_video_chain *chain, - struct uvc_control *ctrl, - struct uvc_control_mapping *mapping, - s32 *value) +static int __uvc_ctrl_get_std(struct uvc_video_chain *chain, + struct uvc_control *ctrl, + struct uvc_control_mapping *mapping, + s32 *value) { int ret; - if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) - return -EACCES; + if (uvc_ctrl_mapping_is_compound(mapping)) + return -EINVAL; ret = __uvc_ctrl_load_cur(chain, ctrl); if (ret < 0) @@ -1199,7 +1199,7 @@ int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) return 0; - ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); + ret = __uvc_ctrl_get_std(chain, master_ctrl, master_map, &val); if (ret >= 0 && val != mapping->master_manual) return -EACCES; @@ -1264,8 +1264,13 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, __uvc_find_control(ctrl->entity, mapping->master_id, &master_map, &master_ctrl, 0); if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) { - s32 val; - int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); + s32 val = 0; + int ret; + + if (uvc_ctrl_mapping_is_compound(master_map)) + return -EINVAL; + + ret = __uvc_ctrl_get_std(chain, master_ctrl, master_map, &val); if (ret < 0) return ret; @@ -1529,7 +1534,8 @@ static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain, if (ctrl == NULL) return; - if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0) + if (uvc_ctrl_mapping_is_compound(mapping) || + __uvc_ctrl_get_std(chain, ctrl, mapping, &val) == 0) changes |= V4L2_EVENT_CTRL_CH_VALUE; uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes); @@ -1700,7 +1706,8 @@ static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems) u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; s32 val = 0; - if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0) + if (uvc_ctrl_mapping_is_compound(mapping) || + __uvc_ctrl_get_std(handle->chain, ctrl, mapping, &val) == 0) changes |= V4L2_EVENT_CTRL_CH_VALUE; uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val, @@ -1880,7 +1887,10 @@ int uvc_ctrl_get(struct uvc_video_chain *chain, if (ctrl == NULL) return -EINVAL; - return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value); + if (uvc_ctrl_mapping_is_compound(mapping)) + return -EINVAL; + else + return __uvc_ctrl_get_std(chain, ctrl, mapping, &xctrl->value); } static int __uvc_ctrl_get_boundary_std(struct uvc_video_chain *chain, @@ -2039,8 +2049,12 @@ int uvc_ctrl_set(struct uvc_fh *handle, ctrl->info.size); } - mapping->set(mapping, value, - uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); + if (!uvc_ctrl_mapping_is_compound(mapping)) + mapping->set(mapping, value, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); + else + return -EINVAL; + if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) ctrl->handle = handle; From patchwork Wed Apr 26 08:29:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677449 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48A89C7618E for ; Wed, 26 Apr 2023 08:30:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240033AbjDZIaj (ORCPT ); Wed, 26 Apr 2023 04:30:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjDZIai (ORCPT ); Wed, 26 Apr 2023 04:30:38 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 200D93A94 for ; Wed, 26 Apr 2023 01:30:36 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-555d93630e7so94813497b3.3 for ; Wed, 26 Apr 2023 01:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497835; x=1685089835; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=AygahGuwOIGMbsXZ0I/WgdrYVLFHg144IGS7QH6QxNw=; b=RgpcMW5a7zJZZB3nuHeFfNsDfaWKTFKBpVB5y6nxlxYbfZSBLQVosd2EcGSpwc3NGd 8ZpVDBVGVFf11osPrc/D47S0evwE5OqUcRX15gPl43nOkcfTWNL5avlk9zRXzGLojAPJ jA62j0qww6PUZ9hMr/gnULW+2QPAO3WB1r7xM3FhxhttRwQEp2QO/pG7qb7KZGEROxKU 9EtVjsaVmE8+drE5bqLI7sTw+DbT8oizZkUI+dl6Zlwavr4k2cx7k6ZceVWfDMW7twuZ nr+4Bnc36sIW2bxaYefpJgFwdJkkr2yUsc/jUZc8cBJuxz5TLzua/MaWLhvmWbwyl7PL QaSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497835; x=1685089835; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AygahGuwOIGMbsXZ0I/WgdrYVLFHg144IGS7QH6QxNw=; b=URWsQXF3pt+EBKQjyrcyIPPjj6Mj909Bh5P/3w28gerYIYm818IcsBu3dKK52JMFQ+ KDcZ0ZnYtNF7p9+dC9E0BnXkibf/kbW395thLU3oVZYOhzvxTEZQlaCM4kg01QXmNPXD ZnaWzasDzaEBUEgj8dv9kP0/51Bg/P/TkXhwnrvmOGUv48xLi3NS7H6Rk6EhDFAwkcSk rvIU9Pvag+kw3ptdq/ovIbxnmv58mzQoQF399j5tMuhoYk79A/Dii6LS4EkoCitsIIKw jCmXK1MzIwDb5ixFcuXns0l4CK7aFyBnOmQAZg6cABKsR0wfopJWqa8DdvndZcwZPqOu cNmg== X-Gm-Message-State: AC+VfDw6HRW1Kj9CVursngF13/a5kfg0O4Pdgg7RT+7oF987r7POJsvg 6OPFkHqkyvC+2A0mW2zTtk+sUhZV4As= X-Google-Smtp-Source: ACHHUZ7UPZJW2Og3hYDJS8cvV4GK0kvMrcpjeAKZHnpRsQ+Qc3HQny57fILwCZN5wgLD+vpKcOdWgdlgYUI= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a81:bd48:0:b0:556:e1e0:e542 with SMTP id n8-20020a81bd48000000b00556e1e0e542mr358695ywk.8.1682497835444; Wed, 26 Apr 2023 01:30:35 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:16 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-5-yunkec@google.com> Subject: [PATCH v11 04/11] media: uvcvideo: Split uvc_control_mapping.size to v4l2 and data size From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Rename the existing size to data_size to represent uvc control data size, add a separate field for v4l2 control size. v4l2 control size will be used the compound controls. Also modify the uvc driver documents to clarify the size in uvc_xu_control_mapping corresponds to the uvc control data size. Reviewed-by: Daniel Scally Signed-off-by: Yunke Cao --- Changelog since v10: - Added Reviewed-by from Daniel Scally. Changelog since v9: - No change. Changelog since v8: - No change. Changelog since v7: - Newly added patch. .../userspace-api/media/drivers/uvcvideo.rst | 2 +- drivers/media/usb/uvc/uvc_ctrl.c | 80 +++++++++---------- drivers/media/usb/uvc/uvc_v4l2.c | 2 +- drivers/media/usb/uvc/uvcvideo.h | 6 +- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/Documentation/userspace-api/media/drivers/uvcvideo.rst b/Documentation/userspace-api/media/drivers/uvcvideo.rst index a290f9fadae9..aab4304e6bb5 100644 --- a/Documentation/userspace-api/media/drivers/uvcvideo.rst +++ b/Documentation/userspace-api/media/drivers/uvcvideo.rst @@ -157,7 +157,7 @@ Argument: struct uvc_xu_control_mapping __u8 name[32] V4L2 control name __u8 entity[16] UVC extension unit GUID __u8 selector UVC control selector - __u8 size V4L2 control size (in bits) + __u8 size UVC control data size (in bits) __u8 offset V4L2 control offset (in bits) enum v4l2_ctrl_type v4l2_type V4L2 control type diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index c4a9ea461eb0..4984438d85ca 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -464,7 +464,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_BRIGHTNESS, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_BRIGHTNESS_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -473,7 +473,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_CONTRAST, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_CONTRAST_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -482,7 +482,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_HUE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_HUE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -493,7 +493,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_SATURATION, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_SATURATION_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -502,7 +502,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_SHARPNESS, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_SHARPNESS_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -511,7 +511,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_GAMMA, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_GAMMA_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -520,7 +520,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_BACKLIGHT_COMPENSATION, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -529,7 +529,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_GAIN, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_GAIN_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -538,7 +538,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_HUE_AUTO, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_HUE_AUTO_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -548,7 +548,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_EXPOSURE_AUTO, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_AE_MODE_CONTROL, - .size = 4, + .data_size = 4, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_BITMASK, @@ -561,7 +561,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_AE_PRIORITY_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -570,7 +570,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_EXPOSURE_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL, - .size = 32, + .data_size = 32, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -581,7 +581,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_AUTO_WHITE_BALANCE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -591,7 +591,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -602,7 +602,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_AUTO_WHITE_BALANCE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -613,7 +613,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_BLUE_BALANCE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -624,7 +624,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_RED_BALANCE, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL, - .size = 16, + .data_size = 16, .offset = 16, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -635,7 +635,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_FOCUS_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -646,7 +646,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_FOCUS_AUTO, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_FOCUS_AUTO_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -656,7 +656,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_IRIS_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -665,7 +665,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_IRIS_RELATIVE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_IRIS_RELATIVE_CONTROL, - .size = 8, + .data_size = 8, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -674,7 +674,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_ZOOM_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED, @@ -683,7 +683,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_ZOOM_CONTINUOUS, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_ZOOM_RELATIVE_CONTROL, - .size = 0, + .data_size = 0, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -694,7 +694,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_PAN_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, - .size = 32, + .data_size = 32, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -703,7 +703,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_TILT_ABSOLUTE, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL, - .size = 32, + .data_size = 32, .offset = 32, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -712,7 +712,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_PAN_SPEED, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, - .size = 16, + .data_size = 16, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -723,7 +723,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_TILT_SPEED, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_PANTILT_RELATIVE_CONTROL, - .size = 16, + .data_size = 16, .offset = 16, .v4l2_type = V4L2_CTRL_TYPE_INTEGER, .data_type = UVC_CTRL_DATA_TYPE_SIGNED, @@ -734,7 +734,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_PRIVACY, .entity = UVC_GUID_UVC_CAMERA, .selector = UVC_CT_PRIVACY_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -743,7 +743,7 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .id = V4L2_CID_PRIVACY, .entity = UVC_GUID_EXT_GPIO_CONTROLLER, .selector = UVC_CT_PRIVACY_CONTROL, - .size = 1, + .data_size = 1, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, @@ -754,7 +754,7 @@ const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { .id = V4L2_CID_POWER_LINE_FREQUENCY, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, - .size = 2, + .data_size = 2, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, @@ -766,7 +766,7 @@ const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = { .id = V4L2_CID_POWER_LINE_FREQUENCY, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, - .size = 2, + .data_size = 2, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, @@ -783,7 +783,7 @@ static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = { .id = V4L2_CID_POWER_LINE_FREQUENCY, .entity = UVC_GUID_UVC_PROCESSING, .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, - .size = 2, + .data_size = 2, .offset = 0, .v4l2_type = V4L2_CTRL_TYPE_MENU, .data_type = UVC_CTRL_DATA_TYPE_ENUM, @@ -816,7 +816,7 @@ static inline void uvc_clear_bit(u8 *data, int bit) } /* - * Extract the bit string specified by mapping->offset and mapping->size + * Extract the bit string specified by mapping->offset and mapping->data_size * from the little-endian data stored at 'data' and return the result as * a signed 32bit integer. Sign extension will be performed if the mapping * references a signed data type. @@ -824,7 +824,7 @@ static inline void uvc_clear_bit(u8 *data, int bit) static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, u8 query, const u8 *data) { - int bits = mapping->size; + int bits = mapping->data_size; int offset = mapping->offset; s32 value = 0; u8 mask; @@ -847,19 +847,19 @@ static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, /* Sign-extend the value if needed. */ if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) - value |= -(value & (1 << (mapping->size - 1))); + value |= -(value & (1 << (mapping->data_size - 1))); return value; } /* - * Set the bit string specified by mapping->offset and mapping->size + * Set the bit string specified by mapping->offset and mapping->data_size * in the little-endian data stored at 'data' to the value 'value'. */ static void uvc_set_le_value(struct uvc_control_mapping *mapping, s32 value, u8 *data) { - int bits = mapping->size; + int bits = mapping->data_size; int offset = mapping->offset; u8 mask; @@ -2036,7 +2036,7 @@ int uvc_ctrl_set(struct uvc_fh *handle, * needs to be loaded from the device to perform the read-modify-write * operation. */ - if ((ctrl->info.size * 8) != mapping->size) { + if ((ctrl->info.size * 8) != mapping->data_size) { ret = __uvc_ctrl_load_cur(chain, ctrl); if (ret < 0) return ret; @@ -2543,8 +2543,8 @@ int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, } /* Validate the user-provided bit-size and offset */ - if (mapping->size > 32 || - mapping->offset + mapping->size > ctrl->info.size * 8) { + if (mapping->data_size > 32 || + mapping->offset + mapping->data_size > ctrl->info.size * 8) { ret = -EINVAL; goto done; } diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 0ee3932ec33a..77704bd21e5c 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -122,7 +122,7 @@ static int uvc_ioctl_xu_ctrl_map(struct uvc_video_chain *chain, } memcpy(map->entity, xmap->entity, sizeof(map->entity)); map->selector = xmap->selector; - map->size = xmap->size; + map->data_size = xmap->size; map->offset = xmap->offset; map->v4l2_type = xmap->v4l2_type; map->data_type = xmap->data_type; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 87222dd8920e..1c6c47ab94f0 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -110,7 +110,11 @@ struct uvc_control_mapping { u8 entity[16]; u8 selector; - u8 size; + /* Size of the v4l2 control. Required for compound controls. */ + u8 v4l2_size; + /* UVC data size. Required for all controls. */ + u8 data_size; + u8 offset; enum v4l2_ctrl_type v4l2_type; u32 data_type; From patchwork Wed Apr 26 08:29:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677648 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC36DC7618E for ; Wed, 26 Apr 2023 08:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240114AbjDZIao (ORCPT ); Wed, 26 Apr 2023 04:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjDZIam (ORCPT ); Wed, 26 Apr 2023 04:30:42 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC4B33C2D for ; Wed, 26 Apr 2023 01:30:40 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b99f3aee8e0so9748949276.0 for ; Wed, 26 Apr 2023 01:30:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497840; x=1685089840; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nF4Yffb6o0bjGCl6ueVjEDUbI81vEW/Q29gvaIMznlY=; b=cF3n8N0FgFCpJCm+rsQCbvEyAXZlJssOJnRcFnfE850nEF8bV5k9Y9U3sb/0Te6CrK eb9schRbn4u8dXWU+2d3dDG7A7p8hONhrFUt9Xy2ktqYLbgaxGpZM2pgKrQ6toaeveET CgDnnDQC2ECz32nZlpbO73Dxy2SSx0OUuBEx1Wh5q26FwEZFxqUOy5ZYe20HuqNwRXJR SKfRuN2g6KiwIIraNRyw7YFtXciw3ZS0091x7c9bZyEVWSNgBylQn7m5f6urYCTxaOcC JFc94Vfu/65v+cX+UBHyGMoKkuaC4K8N2jjnRz3Y6siXasmUnr202sdocjApiz5J5zoW CfYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497840; x=1685089840; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nF4Yffb6o0bjGCl6ueVjEDUbI81vEW/Q29gvaIMznlY=; b=J2Kj7ldGAf1LVvkgCIWvBuZLd5uGWs5QpidGUVH+/kSRYCOFKZ3Qywe0Qe7PkRxhIi XbNW+5+tjyUXKCkLVwbVJvkVZEQ2P7fLTdub81rUuofKzkTm96WbJcfe3hihhSS59j/M +gy4X1DoPKIG6h2lPkDLmf7UfqpWxbz/i+hPqb4SGsp8DUd4OviVsYNwpRYBehAGQFvB AEJ34reL9DKqZC2J1f0jpFEjlCgvBEcEaijx2bfYrJxAcc8VdraTmaluG2jLDbT44jTE 4IPtNeRba+ejiA8T9aRMPTRs/+A5G35L5qlpNECWZNM+9K/sQ7kuhiTs/04uN97rKrcQ SARA== X-Gm-Message-State: AC+VfDwndMOkv+HuuMTSsfVfNGDYvglXfwYIVBk6GDUDgg5Wvlxnpjgh YXHgq4afIIz5GhC2OpJk0RarC81C2Kg= X-Google-Smtp-Source: ACHHUZ6O1xAqLJap9LlKxm+XUSGFQIAqngqLxqp9oOu/ZfKd8BU4e2euh+36VLnk4BgFV2IZovVBEROhx7o= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a05:690c:986:b0:54c:15ad:11e4 with SMTP id ce6-20020a05690c098600b0054c15ad11e4mr1101885ywb.0.1682497840209; Wed, 26 Apr 2023 01:30:40 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:17 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-6-yunkec@google.com> Subject: [PATCH v11 05/11] media: uvcvideo: Add support for compound controls From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Supports getting/setting current value. Supports getting default value. Handles V4L2_CTRL_FLAG_NEXT_COMPOUND. Reviewed-by: Ricardo Ribalda Signed-off-by: Yunke Cao --- Changelog since v10: - Rewrite some logic in __uvc_find_control(). - Remove a duplicate check in __uvc_ctrl_get_compound_cur(). - Thanks, Daniel! Changelog since v9: - Make __uvc_ctrl_set_compound() static. Changelog since v8: - No change. Changelog since v7: - Fixed comments styles, indentation and a few other style issues. - Renamed uvc_g/set_array() to uvc_g/set_compound(). - Moved size check to __uvc_ctrl_add_mapping(). - After setting a new value, copy it back to user. - In __uvc_ctrl_set_compound(), check size before allocating. drivers/media/usb/uvc/uvc_ctrl.c | 180 +++++++++++++++++++++++++++---- drivers/media/usb/uvc/uvcvideo.h | 4 + 2 files changed, 165 insertions(+), 19 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 4984438d85ca..8f0401230649 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -884,6 +884,28 @@ static void uvc_set_le_value(struct uvc_control_mapping *mapping, } } +/* + * Extract the byte array specified by mapping->offset and mapping->data_size + * stored at 'data' to the output array 'data_out'. + */ +static int uvc_get_compound(struct uvc_control_mapping *mapping, const u8 *data, + u8 *data_out) +{ + memcpy(data_out, data + mapping->offset / 8, mapping->data_size / 8); + return 0; +} + +/* + * Copy the byte array 'data_in' to the destination specified by mapping->offset + * and mapping->data_size stored at 'data'. + */ +static int uvc_set_compound(struct uvc_control_mapping *mapping, + const u8 *data_in, u8 *data) +{ + memcpy(data + mapping->offset / 8, data_in, mapping->data_size / 8); + return 0; +} + static bool uvc_ctrl_mapping_is_compound(const struct uvc_control_mapping *mapping) { @@ -906,7 +928,7 @@ static int uvc_entity_match_guid(const struct uvc_entity *entity, static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id, struct uvc_control_mapping **mapping, struct uvc_control **control, - int next) + int next, int next_compound) { struct uvc_control *ctrl; struct uvc_control_mapping *map; @@ -921,14 +943,16 @@ static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id, continue; list_for_each_entry(map, &ctrl->info.mappings, list) { - if ((map->id == v4l2_id) && !next) { + if (map->id == v4l2_id && !next && !next_compound) { *control = ctrl; *mapping = map; return; } if ((*mapping == NULL || (*mapping)->id > map->id) && - (map->id > v4l2_id) && next) { + (map->id > v4l2_id) && + (uvc_ctrl_mapping_is_compound(map) ? + next_compound : next)) { *control = ctrl; *mapping = map; } @@ -942,6 +966,7 @@ static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, struct uvc_control *ctrl = NULL; struct uvc_entity *entity; int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL; + int next_compound = v4l2_id & V4L2_CTRL_FLAG_NEXT_COMPOUND; *mapping = NULL; @@ -950,12 +975,13 @@ static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain, /* Find the control. */ list_for_each_entry(entity, &chain->entities, chain) { - __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next); - if (ctrl && !next) + __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next, + next_compound); + if (ctrl && !next && !next_compound) return ctrl; } - if (ctrl == NULL && !next) + if (!ctrl && !next && !next_compound) uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n", v4l2_id); @@ -1101,12 +1127,59 @@ static int __uvc_ctrl_get_std(struct uvc_video_chain *chain, return 0; } +static int __uvc_ctrl_get_compound(struct uvc_control_mapping *mapping, + struct uvc_control *ctrl, + int id, + struct v4l2_ext_control *xctrl) +{ + u8 size; + u8 *data; + int ret; + + size = mapping->v4l2_size / 8; + if (xctrl->size < size) { + xctrl->size = size; + return -ENOSPC; + } + + data = kmalloc(size, GFP_KERNEL); + if (!data) + return -ENOMEM; + + ret = mapping->get_compound(mapping, uvc_ctrl_data(ctrl, id), data); + if (ret < 0) + goto out; + + ret = copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0; + +out: + kfree(data); + return ret; +} + +static int __uvc_ctrl_get_compound_cur(struct uvc_video_chain *chain, + struct uvc_control *ctrl, + struct uvc_control_mapping *mapping, + struct v4l2_ext_control *xctrl) +{ + int ret; + + ret = __uvc_ctrl_load_cur(chain, ctrl); + if (ret < 0) + return ret; + + return __uvc_ctrl_get_compound(mapping, ctrl, UVC_CTRL_DATA_CURRENT, + xctrl); +} + static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, u32 found_id) { - bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL; unsigned int i; + bool find_next = req_id & + (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND); + req_id &= V4L2_CTRL_ID_MASK; for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { @@ -1194,7 +1267,7 @@ int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, } __uvc_find_control(ctrl->entity, mapping->master_id, &master_map, - &master_ctrl, 0); + &master_ctrl, 0, 0); if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) return 0; @@ -1262,7 +1335,7 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, if (mapping->master_id) __uvc_find_control(ctrl->entity, mapping->master_id, - &master_map, &master_ctrl, 0); + &master_map, &master_ctrl, 0, 0); if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) { s32 val = 0; int ret; @@ -1278,6 +1351,15 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; } + if (v4l2_ctrl->type >= V4L2_CTRL_COMPOUND_TYPES) { + v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD; + v4l2_ctrl->default_value = 0; + v4l2_ctrl->minimum = 0; + v4l2_ctrl->maximum = 0; + v4l2_ctrl->step = 0; + return 0; + } + if (!ctrl->cached) { int ret = uvc_ctrl_populate_cache(chain, ctrl); if (ret < 0) @@ -1530,7 +1612,7 @@ static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain, u32 changes = V4L2_EVENT_CTRL_CH_FLAGS; s32 val = 0; - __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0); + __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0, 0); if (ctrl == NULL) return; @@ -1840,7 +1922,7 @@ static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity, for (i = 0; i < ctrls->count; i++) { __uvc_find_control(entity, ctrls->controls[i].id, &mapping, - &ctrl_found, 0); + &ctrl_found, 0, 0); if (uvc_control == ctrl_found) return i; } @@ -1888,7 +1970,7 @@ int uvc_ctrl_get(struct uvc_video_chain *chain, return -EINVAL; if (uvc_ctrl_mapping_is_compound(mapping)) - return -EINVAL; + return __uvc_ctrl_get_compound_cur(chain, ctrl, mapping, xctrl); else return __uvc_ctrl_get_std(chain, ctrl, mapping, &xctrl->value); } @@ -1909,6 +1991,22 @@ static int __uvc_ctrl_get_boundary_std(struct uvc_video_chain *chain, return 0; } +static int __uvc_ctrl_get_boundary_compound(struct uvc_video_chain *chain, + struct uvc_control *ctrl, + struct uvc_control_mapping *mapping, + struct v4l2_ext_control *xctrl) +{ + int ret; + + if (!ctrl->cached) { + ret = uvc_ctrl_populate_cache(chain, ctrl); + if (ret < 0) + return ret; + } + + return __uvc_ctrl_get_compound(mapping, ctrl, UVC_CTRL_DATA_DEF, xctrl); +} + int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl) { @@ -1926,7 +2024,8 @@ int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, } if (uvc_ctrl_mapping_is_compound(mapping)) - ret = -EINVAL; + ret = __uvc_ctrl_get_boundary_compound(chain, ctrl, mapping, + xctrl); else ret = __uvc_ctrl_get_boundary_std(chain, ctrl, mapping, xctrl); @@ -1935,6 +2034,34 @@ int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, return ret; } +static int __uvc_ctrl_set_compound(struct uvc_control_mapping *mapping, + struct v4l2_ext_control *xctrl, + struct uvc_control *ctrl) +{ + u8 *data; + int ret; + + if (xctrl->size != mapping->v4l2_size / 8) + return -EINVAL; + + data = kmalloc(xctrl->size, GFP_KERNEL); + if (!data) + return -ENOMEM; + + ret = copy_from_user(data, xctrl->ptr, xctrl->size); + if (ret < 0) + goto out; + + ret = mapping->set_compound(mapping, data, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); + + __uvc_ctrl_get_compound(mapping, ctrl, UVC_CTRL_DATA_CURRENT, xctrl); + +out: + kfree(data); + return ret; +} + int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl) { @@ -2049,12 +2176,14 @@ int uvc_ctrl_set(struct uvc_fh *handle, ctrl->info.size); } - if (!uvc_ctrl_mapping_is_compound(mapping)) + if (!uvc_ctrl_mapping_is_compound(mapping)) { mapping->set(mapping, value, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); - else - return -EINVAL; - + } else { + ret = __uvc_ctrl_set_compound(mapping, xctrl, ctrl); + if (ret < 0) + return ret; + } if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS) ctrl->handle = handle; @@ -2465,10 +2594,23 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain, goto err_nomem; } - if (map->get == NULL) + if (uvc_ctrl_mapping_is_compound(map)) { + if (map->data_size != map->v4l2_size) + return -EINVAL; + + /* Only supports byte-aligned data. */ + if (WARN_ON(map->offset % 8 || map->data_size % 8)) + return -EINVAL; + } + + if (!map->get && !uvc_ctrl_mapping_is_compound(map)) map->get = uvc_get_le_value; - if (map->set == NULL) + if (!map->set && !uvc_ctrl_mapping_is_compound(map)) map->set = uvc_set_le_value; + if (!map->get_compound && uvc_ctrl_mapping_is_compound(map)) + map->get_compound = uvc_get_compound; + if (!map->set_compound && uvc_ctrl_mapping_is_compound(map)) + map->set_compound = uvc_set_compound; for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) { if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) == diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 1c6c47ab94f0..8d36a3f8c52e 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -129,8 +129,12 @@ struct uvc_control_mapping { s32 (*get)(struct uvc_control_mapping *mapping, u8 query, const u8 *data); + int (*get_compound)(struct uvc_control_mapping *mapping, const u8 *data, + u8 *data_out); void (*set)(struct uvc_control_mapping *mapping, s32 value, u8 *data); + int (*set_compound)(struct uvc_control_mapping *mapping, const u8 *data_in, + u8 *data); }; struct uvc_control { From patchwork Wed Apr 26 08:29:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677647 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB567C7618E for ; Wed, 26 Apr 2023 08:30:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240143AbjDZIaw (ORCPT ); Wed, 26 Apr 2023 04:30:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240118AbjDZIav (ORCPT ); Wed, 26 Apr 2023 04:30:51 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 266A83AB0 for ; Wed, 26 Apr 2023 01:30:46 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b8ec97a706fso11666125276.3 for ; Wed, 26 Apr 2023 01:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497845; x=1685089845; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hTJab1vS51qb6IR7ogXiCFtB0cB/FHUvyralFeYUvlU=; b=KtXLIc5AiLVmPDBkbG85PVDnwz5Sqd0cTZtYDs63vSu78802EwXwG3B9KFRBMw3fG9 Tm/ht4k0RUlHnD3RYXOfxbP2/p8GeI/GiIxo4vratrkA58S374WfMbzEy+hxfwliP42J o/8Xg5nX97zhUvmjHymXPxxMh6ChngYmDdBMlcl0OE+r7H2s0bNLDWwvNcz2k0XsTOFV mL/M3q/nAlVEQTABeuSfvXwKVyKzNnKQ9sm9GrLhSKhwRylrS8aCMxj/teFGLTsxMhDt uQnmDKX823lJz5AVbtc7Ec6AYUbL/xbcHCoJVek+wQaxVkTbRWdQTrmysDek4ZburqnO 2Yvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497845; x=1685089845; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hTJab1vS51qb6IR7ogXiCFtB0cB/FHUvyralFeYUvlU=; b=bl8ytuoI6zdnj3TUw5k3hTO4UeQChHeQ8j3LGV7UCcFvI888vVnjMsp6Y8z/OlmQQu uTZCIFnlLtdUejQDr+eJqlLLnzgCwHnM0V3x4Y6FPF3xx8e6d8GpeCzZvGdxx3+t2T+e 5jrSDqxQ1HXQLjt3foHO/jSMiZJygjEcWULqHEaSMKKCVxAsSx6oBXjp3No1yVKHtfET 5CrwY1Ay35xb3DuhklDUqCM+K2D8t66/tRontyjR5jwILdy0s3EAevWnQ2d+HM9uit4i Cl0rVcdD7BmWYm4nirWiE+hNV3Jk0pQaTVOzK5dju3t/4Ocf95cjBx3+GPeohlmQZMli Gc1w== X-Gm-Message-State: AAQBX9fixD7l7dCBa9Tk4VlLva0DsFhh0spqvpWtYZG7/PXBhDuPwZvl +bvLW1F8ulGOcdOpGpv+apBaI0ydqpo= X-Google-Smtp-Source: AKy350YaaDoePQtgoKCL/YMGhdMzFx4PRCyfzEwfnXtinrNvq0U3Xx+WC4q0H9k6FwCoRP1RVUnNqGtxm1o= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:7681:0:b0:b98:6352:be20 with SMTP id r123-20020a257681000000b00b986352be20mr7881119ybc.0.1682497845253; Wed, 26 Apr 2023 01:30:45 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:18 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-7-yunkec@google.com> Subject: [PATCH v11 06/11] v4l2-ctrls: add support for V4L2_CTRL_WHICH_MIN/MAX_VAL From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Hans Verkuil Add the capability of retrieving the min and max values of a compound control. Signed-off-by: Hans Verkuil Signed-off-by: Yunke Cao --- Changelog since v10: - No change. Changelog since v9: - No change. Changelog since v8: - Return ENODATA when min/max is not implemented. Document this behavior. - Created a shared helper function __v4l2_ctrl_type_op_init that takes "which" as a parameter. Call it in def, min and max operations. Changelog since v7: - Document that the definition of the min/max are provided by compound controls are defined in control documentation. - Return error, instead of zeroed memory for v4l2_ctrl_ptr_create(NULL). git am from https://lore.kernel.org/all/20191119113457.57833-3-hverkuil-cisco@xs4all.nl/ - Fixed some merge conflits. - Fixed the build error in drivers/media/platform/qcom/venus. .../media/v4l/vidioc-g-ext-ctrls.rst | 11 +- .../media/videodev2.h.rst.exceptions | 2 + drivers/media/i2c/imx214.c | 5 +- .../media/platform/qcom/venus/venc_ctrls.c | 9 +- drivers/media/v4l2-core/v4l2-ctrls-api.c | 57 +++++-- drivers/media/v4l2-core/v4l2-ctrls-core.c | 156 +++++++++++++++--- drivers/media/v4l2-core/v4l2-ioctl.c | 4 +- include/media/v4l2-ctrls.h | 34 +++- include/uapi/linux/videodev2.h | 2 + 9 files changed, 236 insertions(+), 44 deletions(-) diff --git a/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst b/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst index 927ef397f1ce..1cc21ee229aa 100644 --- a/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst +++ b/Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst @@ -304,14 +304,21 @@ still cause this situation. - Which value of the control to get/set/try. * - :cspan:`2` ``V4L2_CTRL_WHICH_CUR_VAL`` will return the current value of the control, ``V4L2_CTRL_WHICH_DEF_VAL`` will return the default + value of the control, ``V4L2_CTRL_WHICH_MIN_VAL`` will return the minimum + value of the control, ``V4L2_CTRL_WHICH_MAX_VAL`` will return the maximum value of the control and ``V4L2_CTRL_WHICH_REQUEST_VAL`` indicates that these controls have to be retrieved from a request or tried/set for a request. In the latter case the ``request_fd`` field contains the file descriptor of the request that should be used. If the device does not support requests, then ``EACCES`` will be returned. - When using ``V4L2_CTRL_WHICH_DEF_VAL`` be aware that you can only - get the default value of the control, you cannot set or try it. + When using ``V4L2_CTRL_WHICH_DEF_VAL``, ``V4L2_CTRL_WHICH_MIN_VAL`` + or ``V4L2_CTRL_WHICH_MAX_VAL`` be aware that you can only get the + default/minimum/maximum value of the control, you cannot set or try it. + The definition of minimum/maximum values for compound types are provided by + the control documentation. If the control documentation does not + document the meaning of minimum/maximum value, then it is not supported. + Querying its minmimum/maximum value will result in -ENODATA. For backwards compatibility you can also use a control class here (see :ref:`ctrl-class`). In that case all controls have to diff --git a/Documentation/userspace-api/media/videodev2.h.rst.exceptions b/Documentation/userspace-api/media/videodev2.h.rst.exceptions index 828cca8e2daa..6295b0fa5716 100644 --- a/Documentation/userspace-api/media/videodev2.h.rst.exceptions +++ b/Documentation/userspace-api/media/videodev2.h.rst.exceptions @@ -563,6 +563,8 @@ ignore define V4L2_CTRL_DRIVER_PRIV ignore define V4L2_CTRL_MAX_DIMS ignore define V4L2_CTRL_WHICH_CUR_VAL ignore define V4L2_CTRL_WHICH_DEF_VAL +ignore define V4L2_CTRL_WHICH_MIN_VAL +ignore define V4L2_CTRL_WHICH_MAX_VAL ignore define V4L2_CTRL_WHICH_REQUEST_VAL ignore define V4L2_OUT_CAP_CUSTOM_TIMINGS ignore define V4L2_CID_MAX_CTRLS diff --git a/drivers/media/i2c/imx214.c b/drivers/media/i2c/imx214.c index 710c9fb515fd..bd11bd8105aa 100644 --- a/drivers/media/i2c/imx214.c +++ b/drivers/media/i2c/imx214.c @@ -1037,7 +1037,10 @@ static int imx214_probe(struct i2c_client *client) imx214->unit_size = v4l2_ctrl_new_std_compound(&imx214->ctrls, NULL, V4L2_CID_UNIT_CELL_SIZE, - v4l2_ctrl_ptr_create((void *)&unit_size)); + v4l2_ctrl_ptr_create((void *)&unit_size), + v4l2_ctrl_ptr_create(NULL), + v4l2_ctrl_ptr_create(NULL)); + ret = imx214->ctrls.error; if (ret) { dev_err(&client->dev, "%s control init failed (%d)\n", diff --git a/drivers/media/platform/qcom/venus/venc_ctrls.c b/drivers/media/platform/qcom/venus/venc_ctrls.c index 7468e43800a9..28eca8f9d148 100644 --- a/drivers/media/platform/qcom/venus/venc_ctrls.c +++ b/drivers/media/platform/qcom/venus/venc_ctrls.c @@ -607,11 +607,16 @@ int venc_ctrl_init(struct venus_inst *inst) v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops, V4L2_CID_COLORIMETRY_HDR10_CLL_INFO, - v4l2_ctrl_ptr_create(&p_hdr10_cll)); + v4l2_ctrl_ptr_create(&p_hdr10_cll), + v4l2_ctrl_ptr_create(NULL), + v4l2_ctrl_ptr_create(NULL)); v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops, V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY, - v4l2_ctrl_ptr_create((void *)&p_hdr10_mastering)); + v4l2_ctrl_ptr_create((void *)&p_hdr10_mastering), + v4l2_ctrl_ptr_create(NULL), + v4l2_ctrl_ptr_create(NULL)); + v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE, diff --git a/drivers/media/v4l2-core/v4l2-ctrls-api.c b/drivers/media/v4l2-core/v4l2-ctrls-api.c index 002ea6588edf..f8888568c5df 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-api.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-api.c @@ -94,6 +94,34 @@ static int def_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) return ptr_to_user(c, ctrl, ctrl->p_new); } +/* Helper function: copy the minimum control value back to the caller */ +static int min_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) +{ + int idx, ret; + + for (idx = 0; idx < ctrl->elems; idx++) { + ret = ctrl->type_ops->minimum(ctrl, idx, ctrl->p_new); + if (ret) + return ret; + } + + return ptr_to_user(c, ctrl, ctrl->p_new); +} + +/* Helper function: copy the maximum control value back to the caller */ +static int max_to_user(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) +{ + int idx, ret; + + for (idx = 0; idx < ctrl->elems; idx++) { + ret = ctrl->type_ops->maximum(ctrl, idx, ctrl->p_new); + if (ret) + return ret; + } + + return ptr_to_user(c, ctrl, ctrl->p_new); +} + /* Helper function: copy the caller-provider value as the new control value */ static int user_to_new(struct v4l2_ext_control *c, struct v4l2_ctrl *ctrl) { @@ -229,8 +257,8 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl, cs->error_idx = i; if (cs->which && - cs->which != V4L2_CTRL_WHICH_DEF_VAL && - cs->which != V4L2_CTRL_WHICH_REQUEST_VAL && + (cs->which < V4L2_CTRL_WHICH_DEF_VAL || + cs->which > V4L2_CTRL_WHICH_MAX_VAL) && V4L2_CTRL_ID2WHICH(id) != cs->which) { dprintk(vdev, "invalid which 0x%x or control id 0x%x\n", @@ -368,8 +396,8 @@ static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl, */ static int class_check(struct v4l2_ctrl_handler *hdl, u32 which) { - if (which == 0 || which == V4L2_CTRL_WHICH_DEF_VAL || - which == V4L2_CTRL_WHICH_REQUEST_VAL) + if (which == 0 || (which >= V4L2_CTRL_WHICH_DEF_VAL && + which <= V4L2_CTRL_WHICH_MAX_VAL)) return 0; return find_ref_lock(hdl, which | 1) ? 0 : -EINVAL; } @@ -389,10 +417,12 @@ int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl, struct v4l2_ctrl_helper *helpers = helper; int ret; int i, j; - bool is_default, is_request; + bool is_default, is_request, is_min, is_max; is_default = (cs->which == V4L2_CTRL_WHICH_DEF_VAL); is_request = (cs->which == V4L2_CTRL_WHICH_REQUEST_VAL); + is_min = (cs->which == V4L2_CTRL_WHICH_MIN_VAL); + is_max = (cs->which == V4L2_CTRL_WHICH_MAX_VAL); cs->error_idx = cs->count; cs->which = V4L2_CTRL_ID2WHICH(cs->which); @@ -432,13 +462,14 @@ int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl, /* * g_volatile_ctrl will update the new control values. - * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL and + * This makes no sense for V4L2_CTRL_WHICH_DEF_VAL, + * V4L2_CTRL_WHICH_MIN_VAL, V4L2_CTRL_WHICH_MAX_VAL and * V4L2_CTRL_WHICH_REQUEST_VAL. In the case of requests * it is v4l2_ctrl_request_complete() that copies the * volatile controls at the time of request completion * to the request, so you don't want to do that again. */ - if (!is_default && !is_request && + if (!is_default && !is_request && !is_min && !is_max && ((master->flags & V4L2_CTRL_FLAG_VOLATILE) || (master->has_volatiles && !is_cur_manual(master)))) { for (j = 0; j < master->ncontrols; j++) @@ -467,6 +498,10 @@ int v4l2_g_ext_ctrls_common(struct v4l2_ctrl_handler *hdl, ret = -ENOMEM; else if (is_request && ref->p_req_valid) ret = req_to_user(cs->controls + idx, ref); + else if (is_min) + ret = min_to_user(cs->controls + idx, ref->ctrl); + else if (is_max) + ret = max_to_user(cs->controls + idx, ref->ctrl); else if (is_volatile) ret = new_to_user(cs->controls + idx, ref->ctrl); else @@ -564,9 +599,11 @@ int try_set_ext_ctrls_common(struct v4l2_fh *fh, cs->error_idx = cs->count; - /* Default value cannot be changed */ - if (cs->which == V4L2_CTRL_WHICH_DEF_VAL) { - dprintk(vdev, "%s: cannot change default value\n", + /* Default/minimum/maximum values cannot be changed */ + if (cs->which == V4L2_CTRL_WHICH_DEF_VAL || + cs->which == V4L2_CTRL_WHICH_MIN_VAL || + cs->which == V4L2_CTRL_WHICH_MAX_VAL) { + dprintk(vdev, "%s: cannot change default/min/max value\n", video_device_node_name(vdev)); return -EINVAL; } diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c index e7f232842376..4f11cd6855e5 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-core.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c @@ -177,29 +177,72 @@ static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx, } } -void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, +static int std_min_compound(const struct v4l2_ctrl *ctrl, u32 idx, union v4l2_ctrl_ptr ptr) +{ + void *p = ptr.p + idx * ctrl->elem_size; + + if (!ctrl->p_min.p_const) + return -ENODATA; + + memcpy(p, ctrl->p_min.p_const, ctrl->elem_size); + return 0; +} + +static int std_max_compound(const struct v4l2_ctrl *ctrl, u32 idx, + union v4l2_ctrl_ptr ptr) +{ + void *p = ptr.p + idx * ctrl->elem_size; + + if (!ctrl->p_max.p_const) + return -ENODATA; + + memcpy(p, ctrl->p_max.p_const, ctrl->elem_size); + return 0; +} + +static int __v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, + u32 which, union v4l2_ctrl_ptr ptr) { unsigned int i; u32 tot_elems = ctrl->elems; u32 elems = tot_elems - from_idx; + s64 value; + int ret; if (from_idx >= tot_elems) - return; + return -EINVAL; + + switch (which) { + case V4L2_CTRL_WHICH_DEF_VAL: + value = ctrl->default_value; + break; + case V4L2_CTRL_WHICH_MAX_VAL: + value = ctrl->maximum; + break; + case V4L2_CTRL_WHICH_MIN_VAL: + value = ctrl->minimum; + break; + default: + return -EINVAL; + } switch (ctrl->type) { case V4L2_CTRL_TYPE_STRING: + if (which == V4L2_CTRL_WHICH_DEF_VAL) + value = ctrl->minimum; + for (i = from_idx; i < tot_elems; i++) { unsigned int offset = i * ctrl->elem_size; - memset(ptr.p_char + offset, ' ', ctrl->minimum); - ptr.p_char[offset + ctrl->minimum] = '\0'; + memset(ptr.p_char + offset, ' ', value); + ptr.p_char[offset + value] = '\0'; } break; case V4L2_CTRL_TYPE_INTEGER64: - if (ctrl->default_value) { + if (value) { for (i = from_idx; i < tot_elems; i++) - ptr.p_s64[i] = ctrl->default_value; + ptr.p_s64[i] = value; } else { memset(ptr.p_s64 + from_idx, 0, elems * sizeof(s64)); } @@ -209,9 +252,9 @@ void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, case V4L2_CTRL_TYPE_MENU: case V4L2_CTRL_TYPE_BITMASK: case V4L2_CTRL_TYPE_BOOLEAN: - if (ctrl->default_value) { + if (value) { for (i = from_idx; i < tot_elems; i++) - ptr.p_s32[i] = ctrl->default_value; + ptr.p_s32[i] = value; } else { memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32)); } @@ -221,32 +264,69 @@ void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, memset(ptr.p_s32 + from_idx, 0, elems * sizeof(s32)); break; case V4L2_CTRL_TYPE_U8: - memset(ptr.p_u8 + from_idx, ctrl->default_value, elems); + memset(ptr.p_u8 + from_idx, value, elems); break; case V4L2_CTRL_TYPE_U16: - if (ctrl->default_value) { + if (value) { for (i = from_idx; i < tot_elems; i++) - ptr.p_u16[i] = ctrl->default_value; + ptr.p_u16[i] = value; } else { memset(ptr.p_u16 + from_idx, 0, elems * sizeof(u16)); } break; case V4L2_CTRL_TYPE_U32: - if (ctrl->default_value) { + if (value) { for (i = from_idx; i < tot_elems; i++) - ptr.p_u32[i] = ctrl->default_value; + ptr.p_u32[i] = value; } else { memset(ptr.p_u32 + from_idx, 0, elems * sizeof(u32)); } break; default: - for (i = from_idx; i < tot_elems; i++) - std_init_compound(ctrl, i, ptr); + for (i = from_idx; i < tot_elems; i++) { + switch (which) { + case V4L2_CTRL_WHICH_DEF_VAL: + std_init_compound(ctrl, i, ptr); + break; + case V4L2_CTRL_WHICH_MAX_VAL: + ret = std_max_compound(ctrl, i, ptr); + if (ret) + return ret; + break; + case V4L2_CTRL_WHICH_MIN_VAL: + ret = std_min_compound(ctrl, i, ptr); + if (ret) + return ret; + break; + } + } break; } + + return 0; +} + +void v4l2_ctrl_type_op_init(const struct v4l2_ctrl *ctrl, u32 from_idx, + union v4l2_ctrl_ptr ptr) +{ + __v4l2_ctrl_type_op_init(ctrl, from_idx, V4L2_CTRL_WHICH_DEF_VAL, ptr); } EXPORT_SYMBOL(v4l2_ctrl_type_op_init); +static int v4l2_ctrl_type_op_minimum(const struct v4l2_ctrl *ctrl, u32 from_idx, + union v4l2_ctrl_ptr ptr) +{ + return __v4l2_ctrl_type_op_init(ctrl, from_idx, + V4L2_CTRL_WHICH_MIN_VAL, ptr); +} + +static int v4l2_ctrl_type_op_maximum(const struct v4l2_ctrl *ctrl, u32 from_idx, + union v4l2_ctrl_ptr ptr) +{ + return __v4l2_ctrl_type_op_init(ctrl, from_idx, + V4L2_CTRL_WHICH_MAX_VAL, ptr); +} + void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl) { union v4l2_ctrl_ptr ptr = ctrl->p_cur; @@ -1043,6 +1123,8 @@ EXPORT_SYMBOL(v4l2_ctrl_type_op_validate); static const struct v4l2_ctrl_type_ops std_type_ops = { .equal = v4l2_ctrl_type_op_equal, .init = v4l2_ctrl_type_op_init, + .minimum = v4l2_ctrl_type_op_minimum, + .maximum = v4l2_ctrl_type_op_maximum, .log = v4l2_ctrl_type_op_log, .validate = v4l2_ctrl_type_op_validate, }; @@ -1514,7 +1596,10 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, s64 min, s64 max, u64 step, s64 def, const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size, u32 flags, const char * const *qmenu, - const s64 *qmenu_int, const union v4l2_ctrl_ptr p_def, + const s64 *qmenu_int, + const union v4l2_ctrl_ptr p_def, + const union v4l2_ctrl_ptr p_min, + const union v4l2_ctrl_ptr p_max, void *priv) { struct v4l2_ctrl *ctrl; @@ -1634,6 +1719,14 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, handler_set_err(hdl, -ERANGE); return NULL; } + + if ((!p_def.p_const && p_min.p_const) || + (p_min.p_const && !p_max.p_const) || + (!p_min.p_const && p_max.p_const)) { + handler_set_err(hdl, -EINVAL); + return NULL; + } + err = check_range(type, min, max, step, def); if (err) { handler_set_err(hdl, err); @@ -1675,6 +1768,8 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, if (type >= V4L2_CTRL_COMPOUND_TYPES && p_def.p_const) sz_extra += elem_size; + if (type >= V4L2_CTRL_COMPOUND_TYPES && p_min.p_const) + sz_extra += elem_size * 2; ctrl = kvzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL); if (ctrl == NULL) { @@ -1740,6 +1835,14 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl, memcpy(ctrl->p_def.p, p_def.p_const, elem_size); } + if (type >= V4L2_CTRL_COMPOUND_TYPES && + p_min.p_const && p_max.p_const) { + ctrl->p_min.p = ctrl->p_def.p + elem_size; + memcpy(ctrl->p_min.p, p_min.p_const, elem_size); + ctrl->p_max.p = ctrl->p_min.p + elem_size; + memcpy(ctrl->p_max.p, p_max.p_const, elem_size); + } + ctrl->type_ops->init(ctrl, 0, ctrl->p_cur); cur_to_new(ctrl); @@ -1790,7 +1893,8 @@ struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl, type, min, max, is_menu ? cfg->menu_skip_mask : step, def, cfg->dims, cfg->elem_size, - flags, qmenu, qmenu_int, cfg->p_def, priv); + flags, qmenu, qmenu_int, cfg->p_def, cfg->p_min, + cfg->p_max, priv); if (ctrl) ctrl->is_private = cfg->is_private; return ctrl; @@ -1815,7 +1919,8 @@ struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl, } return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, min, max, step, def, NULL, 0, - flags, NULL, NULL, ptr_null, NULL); + flags, NULL, NULL, ptr_null, ptr_null, + ptr_null, NULL); } EXPORT_SYMBOL(v4l2_ctrl_new_std); @@ -1848,7 +1953,8 @@ struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl, } return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 0, max, mask, def, NULL, 0, - flags, qmenu, qmenu_int, ptr_null, NULL); + flags, qmenu, qmenu_int, ptr_null, ptr_null, + ptr_null, NULL); } EXPORT_SYMBOL(v4l2_ctrl_new_std_menu); @@ -1880,7 +1986,8 @@ struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl, } return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 0, max, mask, def, NULL, 0, - flags, qmenu, NULL, ptr_null, NULL); + flags, qmenu, NULL, ptr_null, ptr_null, + ptr_null, NULL); } EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items); @@ -1888,7 +1995,9 @@ EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items); /* Helper function for standard compound controls */ struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, const struct v4l2_ctrl_ops *ops, u32 id, - const union v4l2_ctrl_ptr p_def) + const union v4l2_ctrl_ptr p_def, + const union v4l2_ctrl_ptr p_min, + const union v4l2_ctrl_ptr p_max) { const char *name; enum v4l2_ctrl_type type; @@ -1902,7 +2011,7 @@ struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, } return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, min, max, step, def, NULL, 0, - flags, NULL, NULL, p_def, NULL); + flags, NULL, NULL, p_def, p_min, p_max, NULL); } EXPORT_SYMBOL(v4l2_ctrl_new_std_compound); @@ -1926,7 +2035,8 @@ struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl, } return v4l2_ctrl_new(hdl, ops, NULL, id, name, type, 0, max, 0, def, NULL, 0, - flags, NULL, qmenu_int, ptr_null, NULL); + flags, NULL, qmenu_int, ptr_null, ptr_null, + ptr_null, NULL); } EXPORT_SYMBOL(v4l2_ctrl_new_int_menu); diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index 87f163a89c80..1463d55fee0a 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -888,7 +888,9 @@ static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl) return false; break; case V4L2_CTRL_WHICH_DEF_VAL: - /* Default value cannot be changed */ + case V4L2_CTRL_WHICH_MIN_VAL: + case V4L2_CTRL_WHICH_MAX_VAL: + /* Default, minimum or maximum value cannot be changed */ if (ioctl == VIDIOC_S_EXT_CTRLS || ioctl == VIDIOC_TRY_EXT_CTRLS) { c->error_idx = c->count; diff --git a/include/media/v4l2-ctrls.h b/include/media/v4l2-ctrls.h index 1846caf9dd53..6feeb18411e4 100644 --- a/include/media/v4l2-ctrls.h +++ b/include/media/v4l2-ctrls.h @@ -125,6 +125,8 @@ struct v4l2_ctrl_ops { * * @equal: return true if all ctrl->elems array elements are equal. * @init: initialize the value for array elements from from_idx to ctrl->elems. + * @minimum: set the value to the minimum value of the control. + * @maximum: set the value to the maximum value of the control. * @log: log the value. * @validate: validate the value for ctrl->new_elems array elements. * Return 0 on success and a negative value otherwise. @@ -134,6 +136,10 @@ struct v4l2_ctrl_type_ops { union v4l2_ctrl_ptr ptr1, union v4l2_ctrl_ptr ptr2); void (*init)(const struct v4l2_ctrl *ctrl, u32 from_idx, union v4l2_ctrl_ptr ptr); + int (*minimum)(const struct v4l2_ctrl *ctrl, u32 idx, + union v4l2_ctrl_ptr ptr); + int (*maximum)(const struct v4l2_ctrl *ctrl, u32 idx, + union v4l2_ctrl_ptr ptr); void (*log)(const struct v4l2_ctrl *ctrl); int (*validate)(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr ptr); }; @@ -239,6 +245,12 @@ typedef void (*v4l2_ctrl_notify_fnc)(struct v4l2_ctrl *ctrl, void *priv); * @p_def: The control's default value represented via a union which * provides a standard way of accessing control types * through a pointer (for compound controls only). + * @p_min: The control's minimum value represented via a union which + * provides a standard way of accessing control types + * through a pointer (for compound controls only). + * @p_max: The control's maximum value represented via a union which + * provides a standard way of accessing control types + * through a pointer (for compound controls only). * @p_cur: The control's current value represented via a union which * provides a standard way of accessing control types * through a pointer. @@ -298,6 +310,8 @@ struct v4l2_ctrl { } cur; union v4l2_ctrl_ptr p_def; + union v4l2_ctrl_ptr p_min; + union v4l2_ctrl_ptr p_max; union v4l2_ctrl_ptr p_new; union v4l2_ctrl_ptr p_cur; }; @@ -417,6 +431,8 @@ struct v4l2_ctrl_handler { * @step: The control's step value for non-menu controls. * @def: The control's default value. * @p_def: The control's default value for compound controls. + * @p_min: The control's minimum value for compound controls. + * @p_max: The control's maximum value for compound controls. * @dims: The size of each dimension. * @elem_size: The size in bytes of the control. * @flags: The control's flags. @@ -446,6 +462,8 @@ struct v4l2_ctrl_config { u64 step; s64 def; union v4l2_ctrl_ptr p_def; + union v4l2_ctrl_ptr p_min; + union v4l2_ctrl_ptr p_max; u32 dims[V4L2_CTRL_MAX_DIMS]; u32 elem_size; u32 flags; @@ -715,17 +733,23 @@ struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl, * @ops: The control ops. * @id: The control ID. * @p_def: The control's default value. + * @p_min: The control's minimum value. + * @p_max: The control's maximum value. * - * Sames as v4l2_ctrl_new_std(), but with support to compound controls, thanks - * to the @p_def field. Use v4l2_ctrl_ptr_create() to create @p_def from a - * pointer. Use v4l2_ctrl_ptr_create(NULL) if the default value of the - * compound control should be all zeroes. + * Same as v4l2_ctrl_new_std(), but with support to compound controls, thanks + * to the @p_def/min/max fields. Use v4l2_ctrl_ptr_create() to create + * @p_def/min/max from a pointer. Use v4l2_ctrl_ptr_create(NULL) if the + * default value of the compound control should be all zeroes. Use + * v4l2_ctrl_ptr_create(NULL) if the min/max value of the compound control + * is not defined, -ENODATA will be returned in this case. * */ struct v4l2_ctrl *v4l2_ctrl_new_std_compound(struct v4l2_ctrl_handler *hdl, const struct v4l2_ctrl_ops *ops, u32 id, - const union v4l2_ctrl_ptr p_def); + const union v4l2_ctrl_ptr p_def, + const union v4l2_ctrl_ptr p_min, + const union v4l2_ctrl_ptr p_max); /** * v4l2_ctrl_new_int_menu() - Create a new standard V4L2 integer menu control. diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h index ce2bd9ac4c47..8870303df713 100644 --- a/include/uapi/linux/videodev2.h +++ b/include/uapi/linux/videodev2.h @@ -1844,6 +1844,8 @@ struct v4l2_ext_controls { #define V4L2_CTRL_WHICH_CUR_VAL 0 #define V4L2_CTRL_WHICH_DEF_VAL 0x0f000000 #define V4L2_CTRL_WHICH_REQUEST_VAL 0x0f010000 +#define V4L2_CTRL_WHICH_MIN_VAL 0x0f020000 +#define V4L2_CTRL_WHICH_MAX_VAL 0x0f030000 enum v4l2_ctrl_type { V4L2_CTRL_TYPE_INTEGER = 1, From patchwork Wed Apr 26 08:29:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677448 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF287C77B60 for ; Wed, 26 Apr 2023 08:30:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240118AbjDZIax (ORCPT ); Wed, 26 Apr 2023 04:30:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjDZIav (ORCPT ); Wed, 26 Apr 2023 04:30:51 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 232CA3AAF for ; Wed, 26 Apr 2023 01:30:51 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b99f5daf7c5so3367881276.1 for ; Wed, 26 Apr 2023 01:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497850; x=1685089850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lebKifv5lt6ag6XdjNWl3IyrPv7ib44asZSlE3CJHVM=; b=rejQ50owTpS6LrLGWbrnbFD3lm7s9qF9YK82F07kRwLBWWfsmtUZ1gy/J6eV87eB// ezGBCu4na7UOpH/FTDWSL9ghKFmRbpYfFo56f6n1ouhpWRAK+jgV0hUBcI6HefkeyXUP gnj+94CIlXObzns1RRtmTfXOrCBznQEHHk0xybiGzEz1s/a8bYaGnU0wyhDPbQAevGB7 b8dk76AFQyHgvlNre0TMLdcaUEZTElJ0pATF7Kg+2HKMfJ/V4OF/2WP0N9J2ej0PLGCV x9U4xSfirauTQNO1cj6th+dhq/VJ6k+xOdHl6a2ZnMZp8YPVlvHmh9Y1hSQKJshUW2lL TdOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497850; x=1685089850; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lebKifv5lt6ag6XdjNWl3IyrPv7ib44asZSlE3CJHVM=; b=JhQab3VtTGc8EScv/eKZC2eUQud0NiUcKxP2ruc2KQ59WOrRdY3O9nytkzZMFNePFe gtPwwOzF9UIeArbTXzBWoJFTs3cbEqVIsclWFnoF7nbf3rEx58pufMuU85plS1o3pna8 X8dyf1TBP/d5lYzC16nID1yC9+k8Ip6AZXEX+s7F2xGjqFkLaS2q54stCMoaPdkIUJFG erGEBbz0MLqp4xHAiGYLbv+PeDGUgSVokk2PrA9jq1UaynboefDCxlWek1EmQDW8BtCW 6zOXlRGwwN+M5VHOoFaErqIjxjeyOkO9gviaZSb5cy3m01d3Ljg26qfDnISDnrce+t/T FHqQ== X-Gm-Message-State: AC+VfDxAk+XJ82n3uBK2Y2Cr4tvReAcUlFswBTBviW/W+43mYvAHpynR 4IvM2BF/fDpcCGRmznwamxCl0hUwvQg= X-Google-Smtp-Source: ACHHUZ7FH+b1J5qlY8+3uCEQycjqZLnE3fMCa0FvIiZNcOpy6YQ4O4n7//4VipGYFbLVRkVN5rU4OVf4cec= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:d790:0:b0:b99:f202:db79 with SMTP id o138-20020a25d790000000b00b99f202db79mr3231106ybg.12.1682497850402; Wed, 26 Apr 2023 01:30:50 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:19 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-8-yunkec@google.com> Subject: [PATCH v11 07/11] media: v4l2: document the usage of min/max for V4L2_CTRL_TYPE_RECT From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Document the default implementation of min/max for rectangle controls. Reviewed-by: Daniel Scally Signed-off-by: Yunke Cao --- Changelog since v10: - Added Reviewed-by from Daniel. Changelog since v9: - No change. Changelog since v8: - New patch. Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst b/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst index 58982cd382e3..efa07428ba7a 100644 --- a/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst +++ b/Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst @@ -447,7 +447,11 @@ See also the examples in :ref:`control`. - n/a - A struct :c:type:`v4l2_rect`, containing a rectangle described by the position of its top-left corner, the width and the height. Units - depend on the use case. + depend on the use case. Use ``V4L2_CTRL_WHICH_MIN_VAL`` and + ``V4L2_CTRL_WHICH_MAX_VAL`` to query the range of rectangle sizes. The + top-left corner of the minimum and maximum rectangles should be the + same. For example, a control can have a minimum rectangle of 1x1@0x0 and + a maximum of 640x480@0x0. * - ``V4L2_CTRL_TYPE_H264_SPS`` - n/a - n/a From patchwork Wed Apr 26 08:29:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677447 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF912C7618E for ; Wed, 26 Apr 2023 08:31:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239998AbjDZIbB (ORCPT ); Wed, 26 Apr 2023 04:31:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240149AbjDZIa7 (ORCPT ); Wed, 26 Apr 2023 04:30:59 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54ACC3A94 for ; Wed, 26 Apr 2023 01:30:56 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b8f6bef3d4aso12970624276.0 for ; Wed, 26 Apr 2023 01:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497855; x=1685089855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OnZcxqHSuUyKZf7x8wUyjkC3dplKZZ6UnGtX7jcH56Y=; b=e1MUkYmlCCxr1zCApJDYBgTJzZWHGUSRlXQolg0lOUWfpcrZti7PrigONUuQGBNTFM R82mK9iSVWRodBoqHqHeYM0otbQhctR9jKq2N457Ri/rahcETXfctyestmOP520iww0R 1VWsPuOgvj8CVbiXN1VS8nL6zrJ+/YqLyOIc1Hp2yZkJ5XQ+4z/ssItwmwod43bHHW8m HuM8ckCTOQPoZrI5yJv28/h2MZb1A50uHUOWUM46XLLkCVSto0jYOcWFCzh7ejZkeNv/ Lugy/Q+iU8yabV8uKiEb+jWeFU4xaVGuItPvI4Ti26WMdU/5dTivEuboPq1kftrY4o8N mVbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497855; x=1685089855; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OnZcxqHSuUyKZf7x8wUyjkC3dplKZZ6UnGtX7jcH56Y=; b=YA6zfzBpag91mnYlXjtTu8icFUV34ndL3F906hs5DVyREFKbLSrVbyQPQxtw0HQn31 YrNa5qk8qgGwVgdEPhwcsgkRbxkw0cQSJPD6jXcXPA5Spm65+PpVOatUiarzbc/dGEWh fKWv+gKDC51OdYpuX4xNNbXG7pgVE98MU8wsjrg9c5OSdBdPxzSVV4ZfqVshBj3C4dMG PKWKZNId6eBNweYSBFT4yP/fxLM+LXExdhW0WORSHaDzB8Hx8T4Jvg4ZjRCEErSlBLCJ eHEI4ZMiPoGtqS6aqy21pSypOULEbq3EkDtZKCVnUiKkB9JWkwfxcWz7lZh7tbo+eyL0 /MIg== X-Gm-Message-State: AAQBX9cUlVfGAxn+V0kqfvG9tQtJ+z+9ehrd4sDJnan0jXIUs/SJsq63 K1o8CE/nQqeVGoA6NB8us5RgQngsIGA= X-Google-Smtp-Source: AKy350ZKSfwhFx9faNOqgdLUxtjPK4H0lK95GnLpSlugCfEZJEzGhNgkm1b9XyWgT5aPUzflOhPGwwDtnVU= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:778d:0:b0:b99:4132:b5bc with SMTP id s135-20020a25778d000000b00b994132b5bcmr9996967ybc.8.1682497855579; Wed, 26 Apr 2023 01:30:55 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:20 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-9-yunkec@google.com> Subject: [PATCH v11 08/11] media: uvcvideo: support V4L2_CTRL_WHICH_MIN/MAX_VAL From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add support for V4L2_CTRL_WHICH_MIN/MAX_VAL in uvc driver. It is useful for the V4L2_CID_UVC_REGION_OF_INTEREST_RECT control. Reviewed-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky Signed-off-by: Yunke Cao --- Changelog since v10: - Added Reviewed-by from Sergey. Changelog since v9: - Revert a change in v7 that causes v4l2-compliance failure: - In uvc_ioctl_g_ext_ctrls(), when v4l2_which is not V4L2_CTRL_WHICH_*_VAL, - treat it the same as cur instead of returning EINVAL. This is the existing - behavior. - The change in v7 of returning EINVAL fails the check in - v4l2-compliance/v4l2-test-controls.cpp#L834. Changelog since v8: - No change. Changelog since v7: - Address some comments. drivers/media/usb/uvc/uvc_ctrl.c | 44 ++++++++++++++++++++++++++++---- drivers/media/usb/uvc/uvc_v4l2.c | 7 +++-- drivers/media/usb/uvc/uvcvideo.h | 3 ++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 8f0401230649..109369ec2c42 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1978,6 +1978,7 @@ int uvc_ctrl_get(struct uvc_video_chain *chain, static int __uvc_ctrl_get_boundary_std(struct uvc_video_chain *chain, struct uvc_control *ctrl, struct uvc_control_mapping *mapping, + u32 v4l2_which, struct v4l2_ext_control *xctrl) { struct v4l2_queryctrl qc = { .id = xctrl->id }; @@ -1987,28 +1988,59 @@ static int __uvc_ctrl_get_boundary_std(struct uvc_video_chain *chain, if (ret < 0) return ret; - xctrl->value = qc.default_value; + switch (v4l2_which) { + case V4L2_CTRL_WHICH_DEF_VAL: + xctrl->value = qc.default_value; + break; + case V4L2_CTRL_WHICH_MIN_VAL: + xctrl->value = qc.minimum; + break; + case V4L2_CTRL_WHICH_MAX_VAL: + xctrl->value = qc.maximum; + break; + } + return 0; } static int __uvc_ctrl_get_boundary_compound(struct uvc_video_chain *chain, struct uvc_control *ctrl, struct uvc_control_mapping *mapping, + u32 v4l2_which, struct v4l2_ext_control *xctrl) { + u32 flag, id; int ret; + switch (v4l2_which) { + case V4L2_CTRL_WHICH_DEF_VAL: + flag = UVC_CTRL_FLAG_GET_DEF; + id = UVC_CTRL_DATA_DEF; + break; + case V4L2_CTRL_WHICH_MIN_VAL: + flag = UVC_CTRL_FLAG_GET_MIN; + id = UVC_CTRL_DATA_MIN; + break; + case V4L2_CTRL_WHICH_MAX_VAL: + flag = UVC_CTRL_FLAG_GET_MAX; + id = UVC_CTRL_DATA_MAX; + break; + } + + if (!(ctrl->info.flags & flag) && flag != UVC_CTRL_FLAG_GET_DEF) + return -EACCES; + if (!ctrl->cached) { ret = uvc_ctrl_populate_cache(chain, ctrl); if (ret < 0) return ret; } - return __uvc_ctrl_get_compound(mapping, ctrl, UVC_CTRL_DATA_DEF, xctrl); + return __uvc_ctrl_get_compound(mapping, ctrl, id, xctrl); } int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, - struct v4l2_ext_control *xctrl) + struct v4l2_ext_control *xctrl, u32 v4l2_which) { struct uvc_control *ctrl; struct uvc_control_mapping *mapping; @@ -2025,9 +2057,11 @@ int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, if (uvc_ctrl_mapping_is_compound(mapping)) ret = __uvc_ctrl_get_boundary_compound(chain, ctrl, mapping, - xctrl); + v4l2_which, xctrl); else - ret = __uvc_ctrl_get_boundary_std(chain, ctrl, mapping, xctrl); + ret = __uvc_ctrl_get_boundary_std(chain, ctrl, mapping, + v4l2_which, xctrl); + done: mutex_unlock(&chain->ctrl_mutex); diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 77704bd21e5c..2aeb92e8621f 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -1093,9 +1093,12 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, if (ret < 0) return ret; - if (ctrls->which == V4L2_CTRL_WHICH_DEF_VAL) { + switch (ctrls->which) { + case V4L2_CTRL_WHICH_DEF_VAL: + case V4L2_CTRL_WHICH_MIN_VAL: + case V4L2_CTRL_WHICH_MAX_VAL: for (i = 0; i < ctrls->count; ++ctrl, ++i) { - ret = uvc_ctrl_get_boundary(chain, ctrl); + ret = uvc_ctrl_get_boundary(chain, ctrl, ctrls->which); if (ret < 0) { ctrls->error_idx = i; return ret; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 8d36a3f8c52e..69aba8873f65 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -790,7 +790,8 @@ static inline int uvc_ctrl_rollback(struct uvc_fh *handle) int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl); int uvc_ctrl_get_boundary(struct uvc_video_chain *chain, - struct v4l2_ext_control *xctrl); + struct v4l2_ext_control *xctrl, + u32 v4l2_which); int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, const struct v4l2_ext_controls *ctrls, From patchwork Wed Apr 26 08:29:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677646 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FAB8C7618E for ; Wed, 26 Apr 2023 08:31:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240163AbjDZIbD (ORCPT ); Wed, 26 Apr 2023 04:31:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240148AbjDZIbC (ORCPT ); Wed, 26 Apr 2023 04:31:02 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E32953C1B for ; Wed, 26 Apr 2023 01:31:00 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f88a34bd6so94856847b3.2 for ; Wed, 26 Apr 2023 01:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497860; x=1685089860; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=LBReyqDryJ4djpuiJE0ysFpxJegam6a3HP/mTfR8JUk=; b=IhFiCYgDOjuPzcqbPspJAqhdJEL9zQRG/U/4lyoUZE+iYJw1Hsy1c6ov22wj9Rhz8u GxSMO54t1WvVOvSb1RPfC672R+xbkdKMuylFWjoDprr2UpkrKjORJxg4guDEUd24ZLxa FuD5Lk2gRI0dtI2mj34P0U5ogF6d8grw6nBpFEi/SIjybhuiwNvDqjxx6501qyNz9mzp IFsnhHlB3/yL+Kf1+GWkGHcqBwBL3fJBRKG77i45TAi6Ml51VyZfKBMdPStfl34sKl+G 2qb+kkcjENAxT2y8vR0Hd6fgUI3uOqkZvAhBZI7GcrNYHBHYiEfio/RR39HmXSDsFeHU rrhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497860; x=1685089860; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=LBReyqDryJ4djpuiJE0ysFpxJegam6a3HP/mTfR8JUk=; b=ZsT9OxzpcbC4uABzavsqYnCipImXjMmW2Q2Ou/d6SVFipbsuhzmHXAmfNBHRUCmxio CBw68VcpKnNjyyIweR1bTb99oxNaWFI7hgbJLs821I1/TS7t3qVSUAS5KMu86jY3zS5R xBbjOt2jaeRMLkLFXNBhVnUgmT6QY4b5w9eaYOotFZy23tEQFurevWxKEzvRtvZ2h2v/ TFb2+jmPdtNMULkIcIlosi2xeygotV/rBigZV5WLXaKcLdIDGOEo30iSs57f/ONKH5E+ jbuBVXlGLL0lSr5bdzMf/BL58jBJpt0KLDGixWSR5w2mvLDUoIOtdWSxhPSxUW3YO6EX wUJQ== X-Gm-Message-State: AAQBX9cPFn8cGJuG2gICtFkA/X2las7hiPqzBnIiDQ1kV3r0jeRLU8am clnQYfGTlX+rlFsITn4PcI7zLJhk4vo= X-Google-Smtp-Source: AKy350Z8QH7tmTGUoOoQzh9r8x7SHWL6q11sgcTNE0mIkKJUY1bd7fWC3IaodO5SYHN6+2cD6CEAahH89Qo= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a81:eb0d:0:b0:556:2da0:fc6 with SMTP id n13-20020a81eb0d000000b005562da00fc6mr7380491ywm.7.1682497860247; Wed, 26 Apr 2023 01:31:00 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:21 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-10-yunkec@google.com> Subject: [PATCH v11 09/11] media: uvcvideo: implement UVC v1.5 ROI From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Implement support for ROI as described in UVC 1.5: 4.2.2.1.20 Digital Region of Interest (ROI) Control ROI control is implemented using V4L2 control API as two UVC-specific controls: V4L2_CID_UVC_REGION_OF_INTEREST_RECT and V4L2_CID_UVC_REGION_OF_INTEREST_AUTO. Reviewed-by: Ricardo Ribalda Signed-off-by: Yunke Cao --- Changelog since v10: - Moved after the patches adding support for MIN/MAX. - Clamp the set current value based on MIN/MAX. - Thanks, Daniel! Changelog since v9: - No change. Changelog since v8: - No change. Changelog since v7: - Fix a few style issues. - Only allow 4-byte aligned data. - Add control names. - Move initialization to 7/10. drivers/media/usb/uvc/uvc_ctrl.c | 137 +++++++++++++++++++++++++++-- drivers/media/usb/uvc/uvc_v4l2.c | 5 +- drivers/media/usb/uvc/uvcvideo.h | 12 ++- include/uapi/linux/usb/video.h | 1 + include/uapi/linux/uvcvideo.h | 13 +++ include/uapi/linux/v4l2-controls.h | 9 ++ 6 files changed, 167 insertions(+), 10 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 109369ec2c42..df19978746d3 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -358,6 +358,24 @@ static const struct uvc_control_info uvc_ctrls[] = { .flags = UVC_CTRL_FLAG_GET_CUR | UVC_CTRL_FLAG_AUTO_UPDATE, }, + /* + * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated + * by sensors. + * "This RoI should be the same as specified in most recent SET_CUR + * except in the case where the ‘Auto Detect and Track’ and/or + * ‘Image Stabilization’ bit have been set." + * 4.2.2.1.20 Digital Region of Interest (ROI) Control + */ + { + .entity = UVC_GUID_UVC_CAMERA, + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, + .index = 21, + .size = 10, + .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR + | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX + | UVC_CTRL_FLAG_GET_DEF + | UVC_CTRL_FLAG_AUTO_UPDATE, + }, }; static const u32 uvc_control_classes[] = { @@ -459,6 +477,70 @@ static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping, data[first+1] = min_t(int, abs(value), 0xff); } +static int uvc_to_v4l2_rect(struct v4l2_rect *v4l2_rect, + const struct uvc_rect *uvc_rect) +{ + if (uvc_rect->bottom < uvc_rect->top || + uvc_rect->right < uvc_rect->left) + return -EINVAL; + + v4l2_rect->top = uvc_rect->top; + v4l2_rect->left = uvc_rect->left; + v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1; + v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1; + return 0; +} + +static int v4l2_to_uvc_rect(struct uvc_rect *uvc_rect, + const struct v4l2_rect *min_rect, + const struct v4l2_rect *max_rect, + struct v4l2_rect *v4l2_rect) +{ + v4l2_rect->left = clamp_t(s32, v4l2_rect->left, 0, max_rect->width); + v4l2_rect->top = clamp_t(s32, v4l2_rect->top, 0, max_rect->height); + v4l2_rect->height = clamp_t(s32, v4l2_rect->height, + min_rect->height, max_rect->height); + v4l2_rect->width = clamp_t(s32, v4l2_rect->width, + min_rect->width, max_rect->width); + + uvc_rect->top = v4l2_rect->top; + uvc_rect->left = v4l2_rect->left; + uvc_rect->bottom = v4l2_rect->height + v4l2_rect->top - 1; + uvc_rect->right = v4l2_rect->width + v4l2_rect->left - 1; + return 0; +} + +static int uvc_get_compound_rect(struct uvc_control_mapping *mapping, + const u8 *data, u8 *data_out) +{ + struct uvc_rect *uvc_rect; + + uvc_rect = (struct uvc_rect *)(data + mapping->offset / 8); + return uvc_to_v4l2_rect((struct v4l2_rect *)data_out, uvc_rect); +} + +static int uvc_set_compound_rect(struct uvc_control_mapping *mapping, + const u8 *data_in, const u8 *data_min, + const u8 *data_max, u8 *data) +{ + struct uvc_rect *uvc_rect; + struct v4l2_rect min_rect, max_rect; + int ret; + + uvc_rect = (struct uvc_rect *)(data + mapping->offset / 8); + + ret = uvc_get_compound_rect(mapping, data_min, (u8 *)&min_rect); + if (ret) + return ret; + + ret = uvc_get_compound_rect(mapping, data_max, (u8 *)&max_rect); + if (ret) + return ret; + + return v4l2_to_uvc_rect(uvc_rect, &min_rect, &max_rect, + (struct v4l2_rect *)data_in); +} + static const struct uvc_control_mapping uvc_ctrl_mappings[] = { { .id = V4L2_CID_BRIGHTNESS, @@ -748,6 +830,29 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, }, + { + .id = V4L2_CID_UVC_REGION_OF_INTEREST_RECT, + .entity = UVC_GUID_UVC_CAMERA, + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, + .v4l2_size = sizeof(struct v4l2_rect) * 8, + .data_size = sizeof(struct uvc_rect) * 8, + .offset = 0, + .v4l2_type = V4L2_CTRL_TYPE_RECT, + .data_type = UVC_CTRL_DATA_TYPE_RECT, + .get_compound = uvc_get_compound_rect, + .set_compound = uvc_set_compound_rect, + .name = "Region Of Interest Rectangle", + }, + { + .id = V4L2_CID_UVC_REGION_OF_INTEREST_AUTO, + .entity = UVC_GUID_UVC_CAMERA, + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, + .data_size = 16, + .offset = 64, + .v4l2_type = V4L2_CTRL_TYPE_BITMASK, + .data_type = UVC_CTRL_DATA_TYPE_BITMASK, + .name = "Region Of Interest Auto Controls", + }, }; const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = { @@ -900,12 +1005,12 @@ static int uvc_get_compound(struct uvc_control_mapping *mapping, const u8 *data, * and mapping->data_size stored at 'data'. */ static int uvc_set_compound(struct uvc_control_mapping *mapping, - const u8 *data_in, u8 *data) + const u8 *data_in, const u8 *data_min, + const u8 *data_max, u8 *data) { memcpy(data + mapping->offset / 8, data_in, mapping->data_size / 8); return 0; } - static bool uvc_ctrl_mapping_is_compound(const struct uvc_control_mapping *mapping) { @@ -2087,6 +2192,8 @@ static int __uvc_ctrl_set_compound(struct uvc_control_mapping *mapping, goto out; ret = mapping->set_compound(mapping, data, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN), + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX), uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); __uvc_ctrl_get_compound(mapping, ctrl, UVC_CTRL_DATA_CURRENT, xctrl); @@ -2214,6 +2321,13 @@ int uvc_ctrl_set(struct uvc_fh *handle, mapping->set(mapping, value, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT)); } else { + /* Populates min/max value cache for clamping. */ + if (!ctrl->cached) { + ret = uvc_ctrl_populate_cache(chain, ctrl); + if (ret < 0) + return ret; + } + ret = __uvc_ctrl_set_compound(mapping, xctrl, ctrl); if (ret < 0) return ret; @@ -2629,12 +2743,21 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain, } if (uvc_ctrl_mapping_is_compound(map)) { - if (map->data_size != map->v4l2_size) - return -EINVAL; + switch (map->v4l2_type) { + case V4L2_CTRL_TYPE_RECT: + /* Only supports 4 bytes-aligned data. */ + if (WARN_ON(map->offset % 32)) + return -EINVAL; + break; + default: + if (WARN_ON(map->data_size != map->v4l2_size)) + return -EINVAL; + + /* Only supports byte-aligned data. */ + if (WARN_ON(map->offset % 8 || map->data_size % 8)) + return -EINVAL; + } - /* Only supports byte-aligned data. */ - if (WARN_ON(map->offset % 8 || map->data_size % 8)) - return -EINVAL; } if (!map->get && !uvc_ctrl_mapping_is_compound(map)) diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 2aeb92e8621f..6e49a9e5d0d1 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -1052,7 +1052,10 @@ static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, qec->step = qc.step; qec->default_value = qc.default_value; qec->flags = qc.flags; - qec->elem_size = 4; + if (qc.type == V4L2_CTRL_TYPE_RECT) + qec->elem_size = sizeof(struct v4l2_rect); + else + qec->elem_size = 4; qec->elems = 1; qec->nr_of_dims = 0; memset(qec->dims, 0, sizeof(qec->dims)); diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 69aba8873f65..5f1d0ba4b761 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -133,8 +133,9 @@ struct uvc_control_mapping { u8 *data_out); void (*set)(struct uvc_control_mapping *mapping, s32 value, u8 *data); - int (*set_compound)(struct uvc_control_mapping *mapping, const u8 *data_in, - u8 *data); + int (*set_compound)(struct uvc_control_mapping *mapping, + const u8 *data_in, const u8 *data_min, + const u8 *data_max, u8 *data); }; struct uvc_control { @@ -289,6 +290,13 @@ struct uvc_streaming_header { u8 bTriggerUsage; }; +struct uvc_rect { + u16 top; + u16 left; + u16 bottom; + u16 right; +} __packed; + enum uvc_buffer_state { UVC_BUF_STATE_IDLE = 0, UVC_BUF_STATE_QUEUED = 1, diff --git a/include/uapi/linux/usb/video.h b/include/uapi/linux/usb/video.h index 2ff0e8a3a683..2afb4420e6c4 100644 --- a/include/uapi/linux/usb/video.h +++ b/include/uapi/linux/usb/video.h @@ -104,6 +104,7 @@ #define UVC_CT_ROLL_ABSOLUTE_CONTROL 0x0f #define UVC_CT_ROLL_RELATIVE_CONTROL 0x10 #define UVC_CT_PRIVACY_CONTROL 0x11 +#define UVC_CT_REGION_OF_INTEREST_CONTROL 0x14 /* A.9.5. Processing Unit Control Selectors */ #define UVC_PU_CONTROL_UNDEFINED 0x00 diff --git a/include/uapi/linux/uvcvideo.h b/include/uapi/linux/uvcvideo.h index f86185456dc5..9d1e6085feba 100644 --- a/include/uapi/linux/uvcvideo.h +++ b/include/uapi/linux/uvcvideo.h @@ -16,6 +16,7 @@ #define UVC_CTRL_DATA_TYPE_BOOLEAN 3 #define UVC_CTRL_DATA_TYPE_ENUM 4 #define UVC_CTRL_DATA_TYPE_BITMASK 5 +#define UVC_CTRL_DATA_TYPE_RECT 6 /* Control flags */ #define UVC_CTRL_FLAG_SET_CUR (1 << 0) @@ -38,6 +39,18 @@ #define UVC_MENU_NAME_LEN 32 +/* V4L2 driver-specific controls */ +#define V4L2_CID_UVC_REGION_OF_INTEREST_RECT (V4L2_CID_CAMERA_UVC_BASE + 1) +#define V4L2_CID_UVC_REGION_OF_INTEREST_AUTO (V4L2_CID_CAMERA_UVC_BASE + 2) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_EXPOSURE (1 << 0) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_IRIS (1 << 1) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_WHITE_BALANCE (1 << 2) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_FOCUS (1 << 3) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_FACE_DETECT (1 << 4) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK (1 << 5) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION (1 << 6) +#define V4L2_UVC_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY (1 << 7) + struct uvc_menu_info { __u32 value; __u8 name[UVC_MENU_NAME_LEN]; diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h index 5e80daa4ffe0..195c57489d6b 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -993,6 +993,15 @@ enum v4l2_auto_focus_range { #define V4L2_CID_HDR_SENSOR_MODE (V4L2_CID_CAMERA_CLASS_BASE+36) +/* CAMERA-class private control IDs */ + +/* + * The base for the uvc driver controls. + * See linux/uvcvideo.h for the list of controls. + * We reserve 64 controls for this driver. + */ +#define V4L2_CID_CAMERA_UVC_BASE (V4L2_CID_CAMERA_CLASS_BASE + 0x1000) + /* FM Modulator class control IDs */ #define V4L2_CID_FM_TX_CLASS_BASE (V4L2_CTRL_CLASS_FM_TX | 0x900) From patchwork Wed Apr 26 08:29:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677446 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C93D4C7618E for ; Wed, 26 Apr 2023 08:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240165AbjDZIbK (ORCPT ); Wed, 26 Apr 2023 04:31:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240133AbjDZIbI (ORCPT ); Wed, 26 Apr 2023 04:31:08 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C245140DC for ; Wed, 26 Apr 2023 01:31:05 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b98ef5b557fso12042083276.2 for ; Wed, 26 Apr 2023 01:31:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497865; x=1685089865; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/6eAcxcsZVn2Wn4m+e+g35tdx6ZI1ZmzqFc+ThNRwhc=; b=Ge/p+hzBiZvB2d5vj9mC0huONChMdYR4cvHJ3TtAnWPRy7kSa+4NIiHXtyeMYR5HSX +c917G2+h1p8ku8o5YKrIvb8cPCkI0SH8gg+gnbgAZt1uC0BPhWEc1HW8C0tCQ5OZTXb d4OcjSLarIyXG0Xn8Gq45ktFT/I+MqyzGeoT+gRk/4vBvqgWZ5wOmfU12fWpAeDArNwX rkKKpQchEb0e3mV3JJxboMdRHo08mSjaNrDpBXLaxtVSzKtl6ztwAT7JaCoDOMwuTtyi tV7Gzt/6HjYR8gsi9yt78XARKlMsRyjwA849NxL8rMNJ9YJ9V+wS4QiKfCwn6cwf7Y7H vS2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497865; x=1685089865; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/6eAcxcsZVn2Wn4m+e+g35tdx6ZI1ZmzqFc+ThNRwhc=; b=IO2qm35PkgyqO9dr5+ejrIB63zTz6xc2BFJDHt/IHwFiEYydjHAhsr4at3hNrOr/7E GLMySS+NXq3LgcP4P+ArrBgsKCTwf7/y2h5ARsSdyFJda6JtsHC/1Z0ObieceVvJMu3x JxSO/48eoT/I3I1g5pjG5Ka4zQ2l7rxjGb6gCamJXQF2TOsiiNB+aC1Ti2BFHOff1uSt r2Bc8XNBhYmVPOXp3Kvqxg3Sx/G1DX5tMY37X2tmMcWPXZkDCQM9YrgvaoJhH5CVpKgP 2pRenUBKOl+3x+ES0j4slln5jV4EU7Z87aRN+MFXaica8DFv/+cKOeJimGnLymk6+xmd 4u+g== X-Gm-Message-State: AAQBX9cPTyVbTeLcOSBdFDvbSYTHZ5Eyz4ZyMIv/Uuon10NxiNAVb5St LboRjIwbMws1J9WApZ0aI5NNawOZp90= X-Google-Smtp-Source: AKy350ZMURJlfsMnf6D6tQlt3FaI8ktIyMp0/3N+P0dQAUOO7jf3trBZL3EJzsn8zTFp8eeim32FnCa3o6w= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:d88f:0:b0:b1d:5061:98e3 with SMTP id p137-20020a25d88f000000b00b1d506198e3mr11301723ybg.6.1682497865101; Wed, 26 Apr 2023 01:31:05 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:22 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-11-yunkec@google.com> Subject: [PATCH v11 10/11] media: uvcvideo: initilaize ROI control to default value From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add an init function to uvc_control_info. Use the function to initialize ROI control to default value. Also moves utility functions to the top of uvc_ctrl.c, above the uvc_ctrls definition. uvc_ctrl_init_roi() calls uvc_ctrl_data() and need to be declared before uvc_ctrls. Signed-off-by: Yunke Cao --- Changelog since v10: - No change. Changelog since v9: - No change. Changelog since v8: - No change. Changelog since v7: - Newly added patch. Split initializing from the previous patch. - Add an init operation to uvc_control_info and use it for ROI initialization. drivers/media/usb/uvc/uvc_ctrl.c | 273 ++++++++++++++++++------------- drivers/media/usb/uvc/uvcvideo.h | 3 + 2 files changed, 160 insertions(+), 116 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index df19978746d3..9a46e0bdec32 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -32,6 +32,158 @@ #define UVC_CTRL_DATA_DEF 5 #define UVC_CTRL_DATA_LAST 6 +/* ------------------------------------------------------------------------ + * Utility functions + */ + +static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) +{ + return ctrl->uvc_data + id * ctrl->info.size; +} + +static inline int uvc_test_bit(const u8 *data, int bit) +{ + return (data[bit >> 3] >> (bit & 7)) & 1; +} + +static inline void uvc_clear_bit(u8 *data, int bit) +{ + data[bit >> 3] &= ~(1 << (bit & 7)); +} + +/* + * Extract the bit string specified by mapping->offset and mapping->data_size + * from the little-endian data stored at 'data' and return the result as + * a signed 32bit integer. Sign extension will be performed if the mapping + * references a signed data type. + */ +static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, + u8 query, const u8 *data) +{ + int bits = mapping->data_size; + int offset = mapping->offset; + s32 value = 0; + u8 mask; + + data += offset / 8; + offset &= 7; + mask = ((1LL << bits) - 1) << offset; + + while (1) { + u8 byte = *data & mask; + + value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); + bits -= 8 - (offset > 0 ? offset : 0); + if (bits <= 0) + break; + + offset -= 8; + mask = (1 << bits) - 1; + data++; + } + + /* Sign-extend the value if needed. */ + if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) + value |= -(value & (1 << (mapping->data_size - 1))); + + return value; +} + +/* + * Set the bit string specified by mapping->offset and mapping->data_size + * in the little-endian data stored at 'data' to the value 'value'. + */ +static void uvc_set_le_value(struct uvc_control_mapping *mapping, + s32 value, u8 *data) +{ + int bits = mapping->data_size; + int offset = mapping->offset; + u8 mask; + + /* + * According to the v4l2 spec, writing any value to a button control + * should result in the action belonging to the button control being + * triggered. UVC devices however want to see a 1 written -> override + * value. + */ + if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON) + value = -1; + + data += offset / 8; + offset &= 7; + + for (; bits > 0; data++) { + mask = ((1LL << bits) - 1) << offset; + *data = (*data & ~mask) | ((value << offset) & mask); + value >>= offset ? offset : 8; + bits -= 8 - offset; + offset = 0; + } +} + +/* + * Extract the byte array specified by mapping->offset and mapping->data_size + * stored at 'data' to the output array 'data_out'. + */ +static int uvc_get_compound(struct uvc_control_mapping *mapping, const u8 *data, + u8 *data_out) +{ + memcpy(data_out, data + mapping->offset / 8, mapping->data_size / 8); + return 0; +} + +/* + * Copy the byte array 'data_in' to the destination specified by mapping->offset + * and mapping->data_size stored at 'data'. + */ +static int uvc_set_compound(struct uvc_control_mapping *mapping, + const u8 *data_in, const u8 *data_min, + const u8 *data_max, u8 *data) +{ + memcpy(data + mapping->offset / 8, data_in, mapping->data_size / 8); + return 0; +} + +static bool +uvc_ctrl_mapping_is_compound(const struct uvc_control_mapping *mapping) +{ + return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES; +} + +static int uvc_ctrl_init_roi(struct uvc_device *dev, struct uvc_control *ctrl) +{ + int ret; + + ret = uvc_query_ctrl(dev, UVC_GET_DEF, ctrl->entity->id, dev->intfnum, + UVC_CT_REGION_OF_INTEREST_CONTROL, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), + ctrl->info.size); + if (ret) + goto out; + + /* + * Most firmwares have wrong GET_CUR configuration. E.g. it's + * below GET_MIN, or have rectangle coordinates mixed up. This + * causes problems sometimes, because we are unable to set + * auto-controls value without first setting ROI rectangle to + * valid configuration. + * + * We expect that default configuration is always correct and + * is within the GET_MIN / GET_MAX boundaries. + * + * Set current ROI configuration to GET_DEF, so that we will + * always have properly configured ROI. + */ + ret = uvc_query_ctrl(dev, UVC_SET_CUR, 1, dev->intfnum, + UVC_CT_REGION_OF_INTEREST_CONTROL, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF), + ctrl->info.size); +out: + if (ret) + dev_err(&dev->udev->dev, "Failed to fixup ROI (%d).\n", ret); + return ret; +} + /* ------------------------------------------------------------------------ * Controls */ @@ -375,6 +527,7 @@ static const struct uvc_control_info uvc_ctrls[] = { | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_AUTO_UPDATE, + .init = uvc_ctrl_init_roi, }, }; @@ -901,122 +1054,6 @@ static const struct uvc_control_mapping *uvc_ctrl_mappings_uvc15[] = { NULL, /* Sentinel */ }; -/* ------------------------------------------------------------------------ - * Utility functions - */ - -static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id) -{ - return ctrl->uvc_data + id * ctrl->info.size; -} - -static inline int uvc_test_bit(const u8 *data, int bit) -{ - return (data[bit >> 3] >> (bit & 7)) & 1; -} - -static inline void uvc_clear_bit(u8 *data, int bit) -{ - data[bit >> 3] &= ~(1 << (bit & 7)); -} - -/* - * Extract the bit string specified by mapping->offset and mapping->data_size - * from the little-endian data stored at 'data' and return the result as - * a signed 32bit integer. Sign extension will be performed if the mapping - * references a signed data type. - */ -static s32 uvc_get_le_value(struct uvc_control_mapping *mapping, - u8 query, const u8 *data) -{ - int bits = mapping->data_size; - int offset = mapping->offset; - s32 value = 0; - u8 mask; - - data += offset / 8; - offset &= 7; - mask = ((1LL << bits) - 1) << offset; - - while (1) { - u8 byte = *data & mask; - value |= offset > 0 ? (byte >> offset) : (byte << (-offset)); - bits -= 8 - (offset > 0 ? offset : 0); - if (bits <= 0) - break; - - offset -= 8; - mask = (1 << bits) - 1; - data++; - } - - /* Sign-extend the value if needed. */ - if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED) - value |= -(value & (1 << (mapping->data_size - 1))); - - return value; -} - -/* - * Set the bit string specified by mapping->offset and mapping->data_size - * in the little-endian data stored at 'data' to the value 'value'. - */ -static void uvc_set_le_value(struct uvc_control_mapping *mapping, - s32 value, u8 *data) -{ - int bits = mapping->data_size; - int offset = mapping->offset; - u8 mask; - - /* - * According to the v4l2 spec, writing any value to a button control - * should result in the action belonging to the button control being - * triggered. UVC devices however want to see a 1 written -> override - * value. - */ - if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON) - value = -1; - - data += offset / 8; - offset &= 7; - - for (; bits > 0; data++) { - mask = ((1LL << bits) - 1) << offset; - *data = (*data & ~mask) | ((value << offset) & mask); - value >>= offset ? offset : 8; - bits -= 8 - offset; - offset = 0; - } -} - -/* - * Extract the byte array specified by mapping->offset and mapping->data_size - * stored at 'data' to the output array 'data_out'. - */ -static int uvc_get_compound(struct uvc_control_mapping *mapping, const u8 *data, - u8 *data_out) -{ - memcpy(data_out, data + mapping->offset / 8, mapping->data_size / 8); - return 0; -} - -/* - * Copy the byte array 'data_in' to the destination specified by mapping->offset - * and mapping->data_size stored at 'data'. - */ -static int uvc_set_compound(struct uvc_control_mapping *mapping, - const u8 *data_in, const u8 *data_min, - const u8 *data_max, u8 *data) -{ - memcpy(data + mapping->offset / 8, data_in, mapping->data_size / 8); - return 0; -} -static bool -uvc_ctrl_mapping_is_compound(const struct uvc_control_mapping *mapping) -{ - return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES; -} - /* ------------------------------------------------------------------------ * Terminal and unit management */ @@ -2972,6 +3009,10 @@ static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain, * GET_INFO on standard controls. */ uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info); + + if (info->init) + info->init(chain->dev, ctrl); + break; } } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 5f1d0ba4b761..9a47d736ddf3 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -85,6 +85,7 @@ struct gpio_desc; struct sg_table; struct uvc_device; +struct uvc_control; /* * TODO: Put the most frequently accessed fields at the beginning of @@ -99,6 +100,8 @@ struct uvc_control_info { u16 size; u32 flags; + + int (*init)(struct uvc_device *dev, struct uvc_control *ctrl); }; struct uvc_control_mapping { From patchwork Wed Apr 26 08:29:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 677645 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D00B8C77B78 for ; Wed, 26 Apr 2023 08:31:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240166AbjDZIbN (ORCPT ); Wed, 26 Apr 2023 04:31:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240148AbjDZIbM (ORCPT ); Wed, 26 Apr 2023 04:31:12 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDD7E3A94 for ; Wed, 26 Apr 2023 01:31:10 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b8f4c437ce5so11616639276.3 for ; Wed, 26 Apr 2023 01:31:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682497870; x=1685089870; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4Gk5aoEiTe+90UlRiYhsTgf76MirYWxianGlradrCRw=; b=kUpy7tQ9YVpxasdmGqKUh/WjsmUjcjBs41owde87yKwQttDr9YGJtSASqvbHjSkfDl hTVF/5DI+7YugiWYTEbv80PBZ2wimr762ElnZQZTU+/UKn+IWz1+gv9HV+CxSN7LrpFz 0lW4t6IQP/kF2HXSqts6u+bthI5Qr0PKKLuv6ofOoXhtK5rr4c8vHbPdPCo+CKG22vWx GmaHntZswOfvBymwx0gl24KNRLv1wj6nR0B0T8dMZWllstEA9waAGWRR5fDOltOSX0Pq +/mMQKRsBZVLjT6WEYxKn+h1sp/h3lVLVTJ/WZEFort57ObHb906mgOBeID6nWRGKHaj Ty8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682497870; x=1685089870; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4Gk5aoEiTe+90UlRiYhsTgf76MirYWxianGlradrCRw=; b=Oz9BieKpJa8u5ty+JE4jP44GX0V+hDAn1UQOhcDAuUSjJSDhDI7Shhhh3Y+Q/5XaT+ Wlc28QTYEVRTgtbPz0FdsPzujqLNGR1auIqqg7gyHKh/IP+9bAVXYTbxAgji3z5MLohJ BxfTCdj3zJilubidzULLGXh4Eaoz2GNAQChheJq9tBXP9tiMR0xK6l+AKZdvXPmgKheT 9cR67vxhYxE08An6BzMpDUZq0NbRyLRYDaScafUWCPV9w+LnRKOZ2HLfU3KzaH3YbllG Zy6LyZ2PFAtVgNwJ5++OzIkPmBUzIdbYgO6qG6BqMYB7BGAYxu+n/N4clsFgaEmKpdPP ePWg== X-Gm-Message-State: AAQBX9fMp2P2bZ3cbTlKsKX1tjahg5HborDAQTvxkQhz24D7MJb19R9W +ypnvdBemgQrOEnhQn2SArjragzweR4= X-Google-Smtp-Source: AKy350Z4yPQ3xyFJ4DMu1NXB31piDweDDam8TlEOW3XXUkbAqLLGW9cwdy+kQeEhfeWg+sqn5R8evGOveCA= X-Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:1f4e:598b:ed:99bd]) (user=yunkec job=sendgmr) by 2002:a25:cc0c:0:b0:b8b:f584:6b73 with SMTP id l12-20020a25cc0c000000b00b8bf5846b73mr7433418ybf.10.1682497870091; Wed, 26 Apr 2023 01:31:10 -0700 (PDT) Date: Wed, 26 Apr 2023 17:29:23 +0900 In-Reply-To: <20230426082923.132909-1-yunkec@google.com> Mime-Version: 1.0 References: <20230426082923.132909-1-yunkec@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426082923.132909-12-yunkec@google.com> Subject: [PATCH v11 11/11] media: uvcvideo: document UVC v1.5 ROI From: Yunke Cao To: Hans Verkuil , Laurent Pinchart , Daniel Scally Cc: Tomasz Figa , Sergey Senozhatsky , Ricardo Ribalda , linux-media@vger.kernel.org, Yunke Cao Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Added documentation of V4L2_CID_UVC_REGION_OF_INTEREST_RECT and V4L2_CID_UVC_REGION_OF_INTEREST_AUTO. Reviewed-by: Ricardo Ribalda Reviewed-by: Sergey Senozhatsky Signed-off-by: Yunke Cao --- Changelog since v10: - Added Reviewed-by from Sergey. Changelog since v9: - No change. Changelog since v8: - No change. Changelog since v7: - Fix documentation for automatic exposure based on comment in v7. .../userspace-api/media/drivers/uvcvideo.rst | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/Documentation/userspace-api/media/drivers/uvcvideo.rst b/Documentation/userspace-api/media/drivers/uvcvideo.rst index aab4304e6bb5..3dc062221f8b 100644 --- a/Documentation/userspace-api/media/drivers/uvcvideo.rst +++ b/Documentation/userspace-api/media/drivers/uvcvideo.rst @@ -181,6 +181,7 @@ Argument: struct uvc_xu_control_mapping UVC_CTRL_DATA_TYPE_BOOLEAN Boolean UVC_CTRL_DATA_TYPE_ENUM Enumeration UVC_CTRL_DATA_TYPE_BITMASK Bitmask + UVC_CTRL_DATA_TYPE_RECT Rectangular area UVCIOC_CTRL_QUERY - Query a UVC XU control @@ -255,3 +256,64 @@ Argument: struct uvc_xu_control_query __u8 query Request code to send to the device __u16 size Control data size (in bytes) __u8 *data Control value + + +Driver-specific V4L2 controls +----------------------------- + +The uvcvideo driver implements the following UVC-specific controls: + +``V4L2_CID_UVC_REGION_OF_INTEREST_RECT (struct)`` + This control determines the region of interest (ROI). ROI is a + rectangular area represented by a struct :c:type:`v4l2_rect`. The + rectangle is in global sensor coordinates and pixel units. It is + independent of the field of view, not impacted by any cropping or + scaling. + + Use ``V4L2_CTRL_WHICH_MIN_VAL`` and ``V4L2_CTRL_WHICH_MAX_VAL`` to query + the range of rectangle sizes. The left/top coordinates of a minimum or + maximum rectangle are always 0. For example, a device can have a minimum + ROI rectangle of 1x1@0x0 and a maximum of 640x480@0x0. + + Setting a ROI allows the camera to optimize the capture for the region. + The value of ``V4L2_CID_REGION_OF_INTEREST_AUTO`` control determines + the detailed behavior. + + +``V4L2_CID_UVC_REGION_OF_INTEREST_AUTO (bitmask)`` + This determines which, if any, on board features should track to the + Region of Interest specified by the current value of + ``V4L2_CID_UVD__REGION_OF_INTEREST_RECT``. + + Max value is a mask indicating all supported Auto Controls. + +.. flat-table:: + :header-rows: 0 + :stub-columns: 0 + + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_EXPOSURE`` + - Setting this to true causes automatic exposure to track the region of + interest instead of the whole image. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_IRIS`` + - Setting this to true causes automatic iris to track the region of + interest instead of the whole image. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_WHITE_BALANCE`` + - Setting this to true causes automatic white balance to track the region + of interest instead of the whole image. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_FOCUS`` + - Setting this to true causes automatic focus adjustment to track the + region of interest instead of the whole image. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_FACE_DETECT`` + - Setting this to true causes automatic face detection to track the + region of interest instead of the whole image. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK`` + - Setting this to true enables automatic face detection and tracking. The + current value of ``V4L2_CID_REGION_OF_INTEREST_RECT`` may be updated by + the driver. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION`` + - Setting this to true enables automatic image stabilization. The + current value of ``V4L2_CID_REGION_OF_INTEREST_RECT`` may be updated by + the driver. + * - ``V4L2_UVC_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY`` + - Setting this to true enables automatically capture the specified region + with higher quality if possible.