Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2206148pxb; Fri, 25 Mar 2022 13:02:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwH0Fn5iLRt4gaKLZh4hlAEvMM8lmOTjAaHsc0u/72ZgZrO9L8Y900YDPg/YNP2Rl8TxeiT X-Received: by 2002:a17:90b:1b12:b0:1c7:6f5d:d7e8 with SMTP id nu18-20020a17090b1b1200b001c76f5dd7e8mr22623057pjb.154.1648238545397; Fri, 25 Mar 2022 13:02:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648238545; cv=none; d=google.com; s=arc-20160816; b=eq2zkF1OzLL0hzTd6kjdnxnjQtV5xipvAFeI/tvKwxsdyRFmWkVGwrx0kB8rp9CbKJ oqN/ThJWCZejyNeQvfpUr3mtaLjBWndJqIjN80axz/Rthv8H08RGshWQYwg8tnjbh1Fc oTJffHSNF6vH4MVs05WYwW97PcIEpVQvc5fPHDCjroruoxRmiaiXfe5BHXOa1JhGQfe3 QgGkbFLmC/rocem/Tl4fgli/1Pa8PUJwgy681zm5ygQgkGExKzUWoOkDWl2qbVWa9UXp PjvBnNtYlA+JAFyS8PjzZYefvAx28C4dhjF1yB3fhRUi49NM7uiCmD2sd3AcSocAGmsU 1FDQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=7Y8Ke4EKJ+n0sWG8HfMTEQMTwBxHtm/aFE+FUw2Eq8I=; b=KiIEj2kqKHwT1q3N98npHDRo5fLQb3Ca1QjjD09Xgtnc7n6StpJQh6fGoos8m9wNDG oMOpcvWwol90VBDNWlrOxAYGxjkAaLlQXRzryS+peT9A+vVKbWBDnLOJ/KJPGWWMZ8SS 77XZJPFD9FUobKXlmgukkhnzcEjyAhNhzxvsz3/nePv7jpv4takq1NcH6mtFMCDgUYpS t83d+POo8n1Z6kf+vfowyAB+hv9N+EguHFVrBqo7cKVMO11CvWTDbzt+jp/hS3KIHA7X Dlyd99ZJzVuAeZPfFTOvCNFNqoeinq570i3yv6/a1bihNvnEQ7vqRLTDwU98GgWxnnCA GnXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QTkG7yzE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id x12-20020a170902ec8c00b00153b2d164c0si3463779plg.200.2022.03.25.13.02.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 13:02:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QTkG7yzE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2C00C1BA69E; Fri, 25 Mar 2022 11:51:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357958AbiCYOKw (ORCPT + 99 others); Fri, 25 Mar 2022 10:10:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357880AbiCYOKq (ORCPT ); Fri, 25 Mar 2022 10:10:46 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76930D8F69 for ; Fri, 25 Mar 2022 07:09:11 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id o10so15654118ejd.1 for ; Fri, 25 Mar 2022 07: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:mime-version :content-transfer-encoding; bh=7Y8Ke4EKJ+n0sWG8HfMTEQMTwBxHtm/aFE+FUw2Eq8I=; b=QTkG7yzE0VA3tbHnhVo79rmLtyvMtgtPYvmp6iOr44D+sx3bfReMFLwxq2nCuKe96j 8mkk6oHj/B/yiFiuZ4E1cAyeeKJdY9uPKzpddu+F1rUF9EIOq7re8ZayVHSH/XgVHsM3 0P1bykV/XRv6wrvpXrOEBr1jKLBJnKVzzy1jM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7Y8Ke4EKJ+n0sWG8HfMTEQMTwBxHtm/aFE+FUw2Eq8I=; b=t82QZXh0ZEbvsTbhla/ymViP8QXXvtqf9q3NcuZqxznWvSWJQQaJ1VHvKljXRqy3EN 8Ka0MD1C3KOMzfcrn2R4PDnKVtaoZr3Gfm6BwQsUEY+nV4A9QwHvyCDrKQDDQAU5KnhS eooizJgaWPWiZ5Ag6gQH1viJvebIEFL7rCZdWwtsueJXOGu7IkUAuY2X5Go0AOBbPQ9a CnhFFuJAiDXhNyCnbi54wrMK+VPfOaw3yMXJsXSqTA+X4HeiV8Lupo+UMglbE32iqFiy lxbIjdPF3AG5KhV+4c9FlWONRLDs2+y8i5OpTNLGxyaM3rraSEZzirua7smqQqmTpVEY anlA== X-Gm-Message-State: AOAM531eKWjjK9gtxOJL9TaO/NmLHsEx8aWcWQ4YOean2ra24zNeuwJe MiFuRgWRlm+kmqeZgCOHf+E7SA== X-Received: by 2002:a17:906:c307:b0:6df:c7d0:90e8 with SMTP id s7-20020a170906c30700b006dfc7d090e8mr11541350ejz.421.1648217349950; Fri, 25 Mar 2022 07:09:09 -0700 (PDT) Received: from alco.corp.google.com ([2620:0:1059:10:c825:3420:d3ca:fdf1]) by smtp.gmail.com with ESMTPSA id u19-20020a17090617d300b006cea86ca384sm2328667eje.40.2022.03.25.07.09.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 07:09:09 -0700 (PDT) From: Ricardo Ribalda To: Laurent Pinchart , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Hans Verkuil , senozhatsky@chromium.org, tfiga@chromium.org Cc: Ricardo Ribalda Subject: [PATCH v3] media: uvcvideo: Fix handling on Bitmask controls Date: Fri, 25 Mar 2022 15:09:06 +0100 Message-Id: <20220325140906.482970-1-ribalda@chromium.org> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Minimum and step values for V4L2_CTRL_TYPE_BITMASK controls should be 0. There is no need to query the camera firmware about this and maybe get invalid results. Also value should be masked to the max value advertised by the hardware. Finally, handle uvc 1.5 mask controls that use MAX instead of RES to describe the valid bits. Fixes v4l2-compliane: Control ioctls (Input 0): fail: v4l2-test-controls.cpp(97): minimum must be 0 for a bitmask control test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: FAIL Signed-off-by: Ricardo Ribalda --- v3: Changes requested by Laurent Support controls that use GET_RES for describing the valid bits. drivers/media/usb/uvc/uvc_ctrl.c | 53 ++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index b4f6edf968bc..d474fe07de84 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -1078,6 +1078,25 @@ static const char *uvc_map_get_name(const struct uvc_control_mapping *map) return "Unknown Control"; } +static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl, + struct uvc_control_mapping *mapping) +{ + /* + * Some controls, like CT_AE_MODE_CONTROL use GET_RES to + * represent the number of bits supported, those controls + * do not list GET_MAX as supported. + */ + if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) + return mapping->get(mapping, UVC_GET_MAX, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX)); + + if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) + return mapping->get(mapping, UVC_GET_RES, + uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); + + return ~0; +} + static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, struct uvc_control *ctrl, struct uvc_control_mapping *mapping, @@ -1152,6 +1171,12 @@ static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, v4l2_ctrl->step = 0; return 0; + case V4L2_CTRL_TYPE_BITMASK: + v4l2_ctrl->minimum = 0; + v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping); + v4l2_ctrl->step = 0; + return 0; + default: break; } @@ -1253,19 +1278,14 @@ int uvc_query_v4l2_menu(struct uvc_video_chain *chain, menu_info = &mapping->menu_info[query_menu->index]; - if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK && - (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) { - s32 bitmap; - + if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { if (!ctrl->cached) { ret = uvc_ctrl_populate_cache(chain, ctrl); if (ret < 0) goto done; } - bitmap = mapping->get(mapping, UVC_GET_RES, - uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); - if (!(bitmap & menu_info->value)) { + if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & menu_info->value)) { ret = -EINVAL; goto done; } @@ -1745,6 +1765,18 @@ int uvc_ctrl_set(struct uvc_fh *handle, value = xctrl->value; break; + case V4L2_CTRL_TYPE_BITMASK: + if (!ctrl->cached) { + ret = uvc_ctrl_populate_cache(chain, ctrl); + if (ret < 0) + return ret; + } + + xctrl->value = max(0, xctrl->value); + xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping); + value = xctrl->value; + break; + case V4L2_CTRL_TYPE_BOOLEAN: xctrl->value = clamp(xctrl->value, 0, 1); value = xctrl->value; @@ -1758,17 +1790,14 @@ int uvc_ctrl_set(struct uvc_fh *handle, /* Valid menu indices are reported by the GET_RES request for * UVC controls that support it. */ - if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK && - (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) { + if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) { if (!ctrl->cached) { ret = uvc_ctrl_populate_cache(chain, ctrl); if (ret < 0) return ret; } - step = mapping->get(mapping, UVC_GET_RES, - uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES)); - if (!(step & value)) + if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value)) return -EINVAL; } -- 2.35.1.1021.g381101b075-goog