Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp868608pxf; Thu, 18 Mar 2021 13:34:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFXwHGBPtVaALUZxFkjWwrCp5ZMsxxx2vw8OTSA8YzePPAwpGgpPX2awMmAULADy1Qq6VI X-Received: by 2002:a05:6402:438f:: with SMTP id o15mr5933365edc.123.1616099683452; Thu, 18 Mar 2021 13:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616099683; cv=none; d=google.com; s=arc-20160816; b=vfHSlNoILg03CF8O26aEOosHLJ8hRCgDjNArEQUwqwcuKq6ubRqs2csNlPuu+jOJyg BICJMtayK4tl4lK8EYcfhmGVtc7Aj/wuh3ArpkyWAeJSPEdbVGMu3K1RW5fn8wlLIJ4g TaamjIfox1qr1hWxtzb3z7jlth66wwCep4MksTMdabZHzeny/vnBy2WTfuizUxzvBQLs ZsyhD2LY2BgeY5yND9rvNwlxDa6tUzzbiYlaiepYWH4eLvCJdEBqLtcoEdr8FHCoq4Ts pNuor3OymvD5np0Y1F+kicgtGk4F1qW4afb/Jp6H98qvLh76dm9KwqKbJcxEmYQKj7OJ 2AXg== 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=oDvgQ7zr4LP4hONbUbR/B+hiXt3aduz+tBCu4CBH/00=; b=soBdZrzy99ltrO6t8t6TVcHVF4UEMW/moICgnktsXMwM+oIKNJZTJ0OF5ZXC9ycUIR VvXIclsj99DN4fQXiJYlodfFBklTSiBeYgo97Fi+YblFL+rMxN6ncFwFXVBFw/3V2Anv KF48UglXqH8IgjS63sx6DKLUZVREZYxSQj1Rcu+xs620CAJUbKOCqzQX1smOQk6U81yh LSApVQgt1V7SJwYyDTQ1e8cqgN05VpxXoD+HI107xfmmZ21L41uLKw6fBysDx4LH4ZWP mdqzyW6/B8SsYhhJFcVy993OXno+GVcs1OebORfGvXibgXiYbtDZ4fBbDB6LAUEi6Soy B17Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZbhkKybQ; 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 mp34si2388924ejc.140.2021.03.18.13.34.21; Thu, 18 Mar 2021 13:34:43 -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=ZbhkKybQ; 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 S233288AbhCRUa0 (ORCPT + 99 others); Thu, 18 Mar 2021 16:30:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233131AbhCRU3k (ORCPT ); Thu, 18 Mar 2021 16:29:40 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A8EAC06174A for ; Thu, 18 Mar 2021 13:29:39 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id u5so6016531ejn.8 for ; Thu, 18 Mar 2021 13:29:39 -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=oDvgQ7zr4LP4hONbUbR/B+hiXt3aduz+tBCu4CBH/00=; b=ZbhkKybQgWF5nut7L35CxYcq2MTshVwI0OZxfs0XQ9GISQkZ/QRtP52w6MdHgysJ5k fEAIAlGFmQih8DVMQwg58mOc7bmq5Wjq/T7fmQB0cvwLiRPMiIrDr0ZtJ9wMix+6v+94 IKb8zf1pvGgC7By0aXjmiO+ka8xseIlRHft7Y= 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=oDvgQ7zr4LP4hONbUbR/B+hiXt3aduz+tBCu4CBH/00=; b=W69fMQta6/9mUb4smT8kOpbC0ZO6WYwVVeLJAlxBGDUv6ujnW3gf3F7/0FTP7yc5X8 B7nbEFBNPIr7lKFPwH7/xiYDsviFS10GzNJ9D34tOryTpUoJ4KuHGvUxfM2kG7JmQakt CAWnGPnEZ1Xn1wIe803yFmvBsAQZO5F88xmiViGYiuOWzpYM9fSEqI+WKMVy9avyghuI JKatNDC4U4k7tcon+1R0moflWlZ9JnpRZKohQ4v0lspuExaLjxzXamR0wmhv/vwNMQHU 6aw3SLkEWEgfRkX2N+BDiBhymfaBBl8ti8Z0oPvS/3ejDxYMlewyBvyttwhSlwn67/kR L65A== X-Gm-Message-State: AOAM530LK0jf8aona3UslUdhzKdAwIk5ceQJCI2YG6wMvyMsGQenyK0g KszwQxgxpJUzXsex8ViuQZfLFw== X-Received: by 2002:a17:907:2054:: with SMTP id pg20mr399017ejb.213.1616099378160; Thu, 18 Mar 2021 13:29:38 -0700 (PDT) Received: from alco.lan ([80.71.134.83]) by smtp.gmail.com with ESMTPSA id a22sm2533767ejr.89.2021.03.18.13.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Mar 2021 13:29:37 -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 Subject: [PATCH v7 13/17] media: uvcvideo: Return -EACCES to inactive controls Date: Thu, 18 Mar 2021 21:29:24 +0100 Message-Id: <20210318202928.166955-14-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 If a control is inactive return -EACCES to let the userspace know that the value will not be applied automatically when the control is active again. Reviewed-by: Hans Verkuil Suggested-by: Hans Verkuil Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_ctrl.c | 68 ++++++++++++++++++++++---------- drivers/media/usb/uvc/uvc_v4l2.c | 11 +++++- drivers/media/usb/uvc/uvcvideo.h | 2 +- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index 24fd5afc4e4f..1ec8333811bc 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1046,8 +1046,33 @@ static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id, return 0; } +static bool uvc_ctrl_is_inactive(struct uvc_video_chain *chain, + struct uvc_control *ctrl, + struct uvc_control_mapping *mapping) +{ + struct uvc_control_mapping *master_map = NULL; + struct uvc_control *master_ctrl = NULL; + s32 val; + int ret; + + if (!mapping->master_id) + return false; + + __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)) + return false; + + ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val); + if (ret < 0 || val == mapping->master_manual) + return false; + + return true; +} + int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, - bool read) + unsigned long ioctl) { struct uvc_control_mapping *mapping; struct uvc_control *ctrl; @@ -1059,11 +1084,26 @@ int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, if (!ctrl) return -EINVAL; - if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read) - return -EACCES; - - if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read) - return -EACCES; + switch (ioctl) { + case VIDIOC_G_CTRL: + case VIDIOC_G_EXT_CTRLS: + if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) + return -EACCES; + break; + case VIDIOC_S_EXT_CTRLS: + case VIDIOC_S_CTRL: + if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) + return -EACCES; + if (uvc_ctrl_is_inactive(chain, ctrl, mapping)) + return -EACCES; + break; + case VIDIOC_TRY_EXT_CTRLS: + if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) + return -EACCES; + break; + default: + return -EINVAL; + } return 0; } @@ -1087,8 +1127,6 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, struct uvc_control_mapping *mapping, struct v4l2_queryctrl *v4l2_ctrl) { - struct uvc_control_mapping *master_map = NULL; - struct uvc_control *master_ctrl = NULL; const struct uvc_menu_info *menu; unsigned int i; @@ -1104,18 +1142,8 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR)) v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; - if (mapping->master_id) - __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); - if (ret < 0) - return ret; - - if (val != mapping->master_manual) - v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; - } + if (uvc_ctrl_is_inactive(chain, ctrl, mapping)) + v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE; if (!ctrl->cached) { int ret = uvc_ctrl_populate_cache(chain, ctrl); diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index fbb99f3c2fb4..ddebdeb5a81b 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -999,6 +999,10 @@ static int uvc_ioctl_g_ctrl(struct file *file, void *fh, struct v4l2_ext_control xctrl; int ret; + ret = uvc_ctrl_is_accessible(chain, ctrl->id, VIDIOC_G_CTRL); + if (ret) + return ret; + memset(&xctrl, 0, sizeof(xctrl)); xctrl.id = ctrl->id; @@ -1023,6 +1027,10 @@ static int uvc_ioctl_s_ctrl(struct file *file, void *fh, struct v4l2_ext_control xctrl; int ret; + ret = uvc_ctrl_is_accessible(chain, ctrl->id, VIDIOC_S_CTRL); + if (ret) + return ret; + memset(&xctrl, 0, sizeof(xctrl)); xctrl.id = ctrl->id; xctrl.value = ctrl->value; @@ -1054,8 +1062,7 @@ static int uvc_ctrl_check_access(struct uvc_video_chain *chain, int ret = 0; for (i = 0; i < ctrls->count; ++ctrl, ++i) { - ret = uvc_ctrl_is_accessible(chain, ctrl->id, - ioctl == VIDIOC_G_EXT_CTRLS); + ret = uvc_ctrl_is_accessible(chain, ctrl->id, ioctl); if (ret) break; } diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 9471c342a310..a93aeedb5499 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -903,7 +903,7 @@ 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_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id, - bool read); + unsigned long ioctl); int uvc_xu_ctrl_query(struct uvc_video_chain *chain, struct uvc_xu_control_query *xqry); -- 2.31.0.rc2.261.g7f71774620-goog