Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1576756pxf; Fri, 19 Mar 2021 10:11:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxukWWymW1UBUy7wWl54/80uVoFQsjHaqCFZFeib9XP9F8TtvwoT/Qh1T36SsEFydNwwheE X-Received: by 2002:a17:906:8408:: with SMTP id n8mr5376438ejx.152.1616173862119; Fri, 19 Mar 2021 10:11:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616173862; cv=none; d=google.com; s=arc-20160816; b=XrqsBgzODRufNFsvHAOj3wdrGh/doj/kzD1YgWRDZ69wQXa63ttXXDEdrwDy0viWea vlkqUGNZc6ZsUWdtLC+D8+2zoNOQVpLVc4UKx1SSVA0fIYA7DU7xxGSFQvwz8vuxFa4K h4wDw1+94tyz0E3UeCgxxvoGLtP5h7Mhq/xeCcmJ1oK5tXQoB9+YBt3dVcg97/xQFpOJ kDl3MMtoBG3oO3qy8LgA5RQh4e4gCpnz02i/BOQzzJytimJE1H21V7IKgrFXtbFstyja ICadpExTOamx+DEZfbnAWVFrS5oQsapPRxRPwonQtFx3TR8TkJAoF5ZS4tG14vPk5sdE 4Ugw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4XvJz1yvJrZuTXeAiRraqR6H4CHgURI4MtE7jiLzPOk=; b=ML1WS62NHhbZ43tAZndY9HeIvDC/PBDpBX18niSOuSG3gkkdlu+SYGmcMqPL88jiB7 PmvecIXx2zLgcc9j+MIkviA3LjWxRLUntxlbx67zdFrmWbw3eWOO9t0KA95MNoQITa1Z fsWnyk8ErNMi46i1W29dWt7SW2N8jOH/6B0FujLn2VTCV4vFz+PJrHUhhb6x7f89b7Pd 4bh3apglZW1RLUFqnOuuvk9URHffL2ND12JaDikQWZxpSH+HJ1x2iM/GwPAFuHv5bW6v a0cEKfiVieImr1HOTlYPSTMkOlg8ot+xAyNWPhLLFbRBcuBmYrDQDNnUi7J0Zf3ZmrhE vrHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XarRGwcW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gg25si4798044ejb.693.2021.03.19.10.10.39; Fri, 19 Mar 2021 10:11:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=XarRGwcW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230308AbhCSRJo (ORCPT + 99 others); Fri, 19 Mar 2021 13:09:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230107AbhCSRJL (ORCPT ); Fri, 19 Mar 2021 13:09:11 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B099C06174A for ; Fri, 19 Mar 2021 10:09:11 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id l4so10868882ejc.10 for ; Fri, 19 Mar 2021 10:09:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4XvJz1yvJrZuTXeAiRraqR6H4CHgURI4MtE7jiLzPOk=; b=XarRGwcWIQ16asNYRIN+OBBFSQbr+UF0t8f4oOs3gkf1ZxCF/cMoLUZiWU53/d6geM zColXt4y3uDTYVs8Xp16YsMaHPCAfSPY7GProo7++RE5bOiSLUYZtJHFk9FpJnwtBdDG OAfixKilTnh3Ikj32k75jjlOjIxtPSCiTh8kY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4XvJz1yvJrZuTXeAiRraqR6H4CHgURI4MtE7jiLzPOk=; b=cbHDYDMb27Ckx9tPCoGH+eQIP05vyk48L49XDE6wCRFfPZAcCpz5Mr7eJ9FBS3i5ZQ Py6UFi0WqwHnAwqb6HSDhAFSnK4MFFCbVz9kCiTPsuhLRR96rzcqvLFpKGz+Gt3FPz7m qRRlDodIcSrBXkWrYmR5zujfpwAjA45iEbUXsfEgqn+Yq85Jo33fPWvI5oXAbFs0LIZX RvdnnUzcsjbXBmUbJrBorYgbTHXVhTfwcqWo3lawFw1XZWXsAMCf1M6ZSAxVDiIn361e GcFwJYXc7qLC4C5Ebj5KRngrMV/mSSpivyH1tKoU6+KBKAkRTLaIkkwrSwYXuSlAvccb fHPQ== X-Gm-Message-State: AOAM530u0gVJ+51aWnwEzRWb+j4MPGXjzOXjg3I0OlDp9zVKsvIMxrFc ooGgnRooKPnFODCQAtAlOGsRsQ== X-Received: by 2002:a17:906:7d82:: with SMTP id v2mr5540650ejo.524.1616173749918; Fri, 19 Mar 2021 10:09:09 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id be27sm4506050edb.47.2021.03.19.10.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Mar 2021 10:09:09 -0700 (PDT) From: Ricardo Ribalda To: Laurent Pinchart , Mauro Carvalho Chehab , Hans Verkuil , Sergey Senozhatsky , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, tfiga@chromium.org Cc: Ricardo Ribalda , stable@vger.kernel.org Subject: [PATCH v8 01/19] media: v4l2-ioctl: Fix check_ext_ctrls Date: Fri, 19 Mar 2021 18:08:48 +0100 Message-Id: <20210319170906.278238-2-ribalda@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210319170906.278238-1-ribalda@chromium.org> References: <20210319170906.278238-1-ribalda@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Drivers that do not use the ctrl-framework use this function instead. Fix the following issues: - Do not check for multiple classes when getting the DEF_VAL. - Return -EINVAL for request_api calls - Default value cannot be changed, return EINVAL as soon as possible. - Return the right error_idx [If an error is found when validating the list of controls passed with VIDIOC_G_EXT_CTRLS, then error_idx shall be set to ctrls->count to indicate to userspace that no actual hardware was touched. It would have been much nicer of course if error_idx could point to the control index that failed the validation, but sadly that's not how the API was designed.] Fixes v4l2-compliance: Control ioctls (Input 0): warn: v4l2-test-controls.cpp(834): error_idx should be equal to count warn: v4l2-test-controls.cpp(855): error_idx should be equal to count fail: v4l2-test-controls.cpp(813): doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL Buffer ioctls (Input 0): fail: v4l2-test-buffers.cpp(1994): ret != EINVAL && ret != EBADR && ret != ENOTTY test Requests: FAIL Cc: stable@vger.kernel.org Fixes: 6fa6f831f095 ("media: v4l2-ctrls: add core request support") Suggested-by: Hans Verkuil Reviewed-by: Hans Verkuil Signed-off-by: Ricardo Ribalda --- drivers/media/v4l2-core/v4l2-ioctl.c | 60 ++++++++++++++++++---------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index 31d1342e61e8..7b5ebdd329e8 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -908,7 +908,7 @@ static void v4l_print_default(const void *arg, bool write_only) pr_cont("driver-specific ioctl\n"); } -static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv) +static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl) { __u32 i; @@ -917,23 +917,41 @@ static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv) for (i = 0; i < c->count; i++) c->controls[i].reserved2[0] = 0; - /* V4L2_CID_PRIVATE_BASE cannot be used as control class - when using extended controls. - Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL - is it allowed for backwards compatibility. - */ - if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE) - return 0; - if (!c->which) - return 1; + switch (c->which) { + case V4L2_CID_PRIVATE_BASE: + /* + * V4L2_CID_PRIVATE_BASE cannot be used as control class + * when using extended controls. + * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL + * is it allowed for backwards compatibility. + */ + if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL) + return false; + break; + case V4L2_CTRL_WHICH_DEF_VAL: + /* Default value cannot be changed */ + if (ioctl == VIDIOC_S_EXT_CTRLS || + ioctl == VIDIOC_TRY_EXT_CTRLS) { + c->error_idx = c->count; + return false; + } + return true; + case V4L2_CTRL_WHICH_CUR_VAL: + return true; + case V4L2_CTRL_WHICH_REQUEST_VAL: + c->error_idx = c->count; + return false; + } + /* Check that all controls are from the same control class. */ for (i = 0; i < c->count; i++) { if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) { - c->error_idx = i; - return 0; + c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i : + c->count; + return false; } } - return 1; + return true; } static int check_fmt(struct file *file, enum v4l2_buf_type type) @@ -2229,7 +2247,7 @@ static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops, ctrls.controls = &ctrl; ctrl.id = p->id; ctrl.value = p->value; - if (check_ext_ctrls(&ctrls, 1)) { + if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) { int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls); if (ret == 0) @@ -2263,7 +2281,7 @@ static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops, ctrls.controls = &ctrl; ctrl.id = p->id; ctrl.value = p->value; - if (check_ext_ctrls(&ctrls, 1)) + if (check_ext_ctrls(&ctrls, VIDIOC_S_CTRL)) return ops->vidioc_s_ext_ctrls(file, fh, &ctrls); return -EINVAL; } @@ -2285,8 +2303,8 @@ static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops, vfd, vfd->v4l2_dev->mdev, p); if (ops->vidioc_g_ext_ctrls == NULL) return -ENOTTY; - return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) : - -EINVAL; + return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ? + ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL; } static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops, @@ -2306,8 +2324,8 @@ static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops, vfd, vfd->v4l2_dev->mdev, p); if (ops->vidioc_s_ext_ctrls == NULL) return -ENOTTY; - return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) : - -EINVAL; + return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ? + ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL; } static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops, @@ -2327,8 +2345,8 @@ static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops, vfd, vfd->v4l2_dev->mdev, p); if (ops->vidioc_try_ext_ctrls == NULL) return -ENOTTY; - return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) : - -EINVAL; + return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ? + ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL; } /* -- 2.31.0.rc2.261.g7f71774620-goog