Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1186660pxf; Fri, 26 Mar 2021 03:00:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7YG57bIm1xvpYSPsK7Ay8jp/o05GJUOi1a67ENMZZGpPyC9gASYXLe9jSTkcJClYrDkZR X-Received: by 2002:a17:906:4747:: with SMTP id j7mr14144430ejs.221.1616752816200; Fri, 26 Mar 2021 03:00:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616752816; cv=none; d=google.com; s=arc-20160816; b=0kT1I8CCwxYU6C+jLv4BeNtg6ESU6gn1jdNY2b9OOT9c+daSNTGf7IyEqTkE1fkv9L Y7K0++mt/xM3klzL832orzCcCpT5N9xBqQvFFWsD9Wfl0nsFoaPal4+bivQLaFzGnEKT avQAzRFXPVSRdiQeb5q4HOi8CH5vJGN3X5we6bu2hrJ0sjslGTfMDFGveBumUTZdxa0O 4ZlWpPimKXbNeU9VwfoW0gRSckIdQ1XMo+v8lvbvPj7ssHS1ZaP2QmTNq9h8aTUDb9fy 5tp+7wCFY7j1g1GjaooLOTtzMZ/xoDk9fZ2xF2yOFlIalGjBQduc9GeyLxxwc1aanOrD Wygg== 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=l9DqWBZVACDhYWKU2Yn/vht3Qo/OTKVi3IGrxQaKbjA=; b=EZdHoUorjogtEM2sLuiDL/e8FMGrAWWRDbyiBGNIWGlLXuVwAgDmB7hT4qa+Nx+Hkl 3UOA41yorA1axbVnxuqxOIwVYYZBd1dOpF4cQCe5zAGUh3ROfJVEP7yNkd8atJ2VDTvB T5q/RbF6f1zsUXc8fqtjuplxRaHhUHnGQR/6olsEM1Ej6GcqwhvNquB29aJxk59o9kDv tFFMXRjERe1vR/zyIMnBwjD8G/BmSgNIenvbHHilT+qPiu6OxmiVTF+hk2cf6LEQPMD+ gX5WjaomaEj1QjlaDOgbQPTg9K0JBe8dyOp9UMg+kB7ReCyLpO8m6Erzje8+GTDDsboK F0Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="R/aKZgXT"; 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 r27si6141449edw.299.2021.03.26.02.59.53; Fri, 26 Mar 2021 03:00:16 -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="R/aKZgXT"; 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 S230100AbhCZJ65 (ORCPT + 99 others); Fri, 26 Mar 2021 05:58:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229898AbhCZJ6q (ORCPT ); Fri, 26 Mar 2021 05:58:46 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24C34C0613B2 for ; Fri, 26 Mar 2021 02:58:46 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id l4so7465877ejc.10 for ; Fri, 26 Mar 2021 02:58:46 -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=l9DqWBZVACDhYWKU2Yn/vht3Qo/OTKVi3IGrxQaKbjA=; b=R/aKZgXTHS/4xIEZdHeaHUgrSlfv2Dd9ROd6AzvFZEIHu0lXhaNx4QQnTd6BbMCQ9e 4KPC8iUUbLmXVJwh3563sZ4MqMKWdUxrcWAZpOQUNb7gK/0hnXoOOi05fGxEm/PBg9Cu QnPNZFh5aYiUY5q3glMpcnplZLVw5HNpyXwKw= 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=l9DqWBZVACDhYWKU2Yn/vht3Qo/OTKVi3IGrxQaKbjA=; b=AXew8NLuXdC3ulgTebEUrr7wRgsgn+JXH5/dB8gp3wx1TUvsrWdFSDCwTssV7JAHSN HvcI2jHBGBVBHbX7GKcAUxRNlZ7iSMy7McLqzFAeXbaGvWmrJUZov0dRtPzf6mXzVqpz +5PSjFRAA0sI7XS3mDLlU3ylupLXz5FgJ4sJ7dV46EPHkAWZ+iaPV0hoWwiYJQWbRZqc PFrSa7AvhbyzME3y1oWP4+22k855i1u6sjKGQ+auMKNOZOmMzQ5lw2z1nBejaQSIv+vi vsTrK8Tq1cVADtlXmUWYtg6F3jtr6XBZlBEfwOyMH8bj5FVBjA2HlSV6FCoRiaAhqTwx PcNg== X-Gm-Message-State: AOAM530Viz67Sinrl8yXbBH9dXZBT5Vf59I4sY5ojnSxt67Kj0VCuvKN eNicC/x9YvHOlMiaULUkaaqizw== X-Received: by 2002:a17:906:86c6:: with SMTP id j6mr13747183ejy.197.1616752724800; Fri, 26 Mar 2021 02:58:44 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id h13sm4036658edz.71.2021.03.26.02.58.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Mar 2021 02:58:44 -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 v9 01/22] media: v4l2-ioctl: Fix check_ext_ctrls Date: Fri, 26 Mar 2021 10:58:19 +0100 Message-Id: <20210326095840.364424-2-ribalda@chromium.org> X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog In-Reply-To: <20210326095840.364424-1-ribalda@chromium.org> References: <20210326095840.364424-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.291.g576ba9dcdaf-goog