Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp866613pxf; Thu, 18 Mar 2021 13:31:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrjVLQ6SWz3VO107VbjajQzddyjxDqh4995DHL6O9+reje4LnADrmJwmkdSWUmlj6bS2U+ X-Received: by 2002:a17:907:6005:: with SMTP id fs5mr449013ejc.184.1616099482278; Thu, 18 Mar 2021 13:31:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616099482; cv=none; d=google.com; s=arc-20160816; b=fnrYuFKqxvdWPgbBB8WBR9T1A62rXfqnsouqPAkqT7n6Ues1Vwk9jmjiJQM3/5jiH2 Jf+c4beKxuf31NZLO6CQvGmuz1oZkqEcC4Fgs57TIWFnXe8g+ctdMxCu3uSTOSBA8t80 mHFjekxTnbnLxeL9m/y43q/IhIC0qITuvLxWr3Cd+O/De5w2EV8jeijD6f4cNHcm5vEv d7xAIijU1aPc6m0++hjPWgHUaOqj+VtvgBk9He+aHVJVQdYYpYNNZQKqbHXvWylN09RI w20H/YCwDROJzOBwh/K8TxEdbATAYJH2/53e3GjdbPNg5XPOyw2HCwxyUks6ZAyZ3rXY Cumg== 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=FoTNLAOXFMQlhoehVNvrR3D+MbUba/aJhWEJ/3u+/wQ=; b=Gwou1FFqvwxkFAovyspPsNW8iaDjy1BAvxCQHFleZWcbgTW+Y9mtkwPPr2Az0VMY2i Wi6vlD94DwTAlhbbl8N3L/iP2rYSmbtIAHq3R4zmFpZAiPKV9eHLstsly/6FWwZ5QP9Z 64urjJJ1eU2VQ8tvU9IfgDfGYCk10V5HOYiaxHt7Kvuk9MPTRV9nV6Mrjv5Ppwzc9OF5 90KLCNLE2vxH2B4wxFm4YMrQTqN0TiiiX2HkQPRuzvP6dHWPpzZ4WgYq2ACnSwcS2Nl4 4e/Va0uO7aj4klCihdMaBhKVmpUQSXZVhSYgZdAvHpyytUz2xFlZ3AeRD9SmmYiDaPGr a+HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Gy6lr22S; 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 g26si2406519edr.573.2021.03.18.13.30.59; Thu, 18 Mar 2021 13:31:22 -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=Gy6lr22S; 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 S233145AbhCRU3l (ORCPT + 99 others); Thu, 18 Mar 2021 16:29:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231797AbhCRU3c (ORCPT ); Thu, 18 Mar 2021 16:29:32 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13C6EC06175F for ; Thu, 18 Mar 2021 13:29:32 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id j3so8257898edp.11 for ; Thu, 18 Mar 2021 13:29:32 -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=FoTNLAOXFMQlhoehVNvrR3D+MbUba/aJhWEJ/3u+/wQ=; b=Gy6lr22SSC6SHuKO3ZQL9boQrV+nP0bJNV43u7oKTPA/l/I7KFgSMPWesy+r3kc6rH oQ/3ld5dd0taAQYxooaVdn81vhKNpwIJrDgleWUYgQzK1syv2po4prno0Jh62+fALIgr 3bQTIfY9JISvyyJb2h0UMOMSt+G9sl/N4RCpE= 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=FoTNLAOXFMQlhoehVNvrR3D+MbUba/aJhWEJ/3u+/wQ=; b=OD0p3GgZMVSJj4hCx1ImUIIHq4nlDCf9WJO3QVo4GKGIbYNd2jVFcPywKeMKIqVY5e 4SglylIG8hFDj5+vD1ruxQTaBXU3EBy7o2mt7Rv6dz8xeTa5l1ukpVk9tiXXsSfbDsrE 6nFPl4NEBSV8x2so39bzp+p8FOS2MRgcZpoi6XL90SLDfQ4O4vcuQzWJZXBeYNBeg3jt nTbnKKIgV4a4MAjk8xASr7FqVj8APteBpR0xtAMlgMMfky+3mNBatC3yntn3s5QER8pl /7bVHD3HoJaSfpCjW2dPS/ANLbJaZi3jGY8f7bQ+9KqKcUWQ2QpcSToLPYLlFk2FjeIk u0Tw== X-Gm-Message-State: AOAM5328wLqGz8TiiSeEiqrcglIT43by+fb6A9SqfAX2aaQaL3FdNfs6 NHtUDreChLhZ/nnN6uB5fKw/wQ== X-Received: by 2002:a05:6402:12cf:: with SMTP id k15mr5718330edx.192.1616099370709; Thu, 18 Mar 2021 13:29:30 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id a22sm2533767ejr.89.2021.03.18.13.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Mar 2021 13:29:30 -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 v7 01/17] media: v4l2-ioctl: Fix check_ext_ctrls Date: Thu, 18 Mar 2021 21:29:12 +0100 Message-Id: <20210318202928.166955-2-ribalda@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210318202928.166955-1-ribalda@chromium.org> References: <20210318202928.166955-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 | 59 ++++++++++++++++++---------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index 31d1342e61e8..ccd21b4d9c72 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,40 @@ 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_CROP) + 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 +2246,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 +2280,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 +2302,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 +2323,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 +2344,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