From patchwork Sat May 1 08:19:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12234697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F781C43470 for ; Sat, 1 May 2021 08:20:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C3C461414 for ; Sat, 1 May 2021 08:20:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231681AbhEAIVB (ORCPT ); Sat, 1 May 2021 04:21:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbhEAIVA (ORCPT ); Sat, 1 May 2021 04:21:00 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBAE4C06138B for ; Sat, 1 May 2021 01:20:10 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id lp8so316317pjb.1 for ; Sat, 01 May 2021 01:20:10 -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=ylnHk+JNuqq5ZmDFacYDEfwOgLykql+ZShEQSvQwkpU=; b=YXhoS2pU2hvfW019BAiAszVK4eSDsIqFZ+yEKWjufqpG5zfOG7SIkphyor3r0BuXlP wy2iqwRKgdHi55Ku+vCNmgiMUBhFebPk8X0KYaWiS/Ty7R9i+yHaRjzaB5FTrF8c23O0 WoLVWPAoTEnMMf8evhZBQGUKngWTYDIAp17lM= 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=ylnHk+JNuqq5ZmDFacYDEfwOgLykql+ZShEQSvQwkpU=; b=sF/56Exnk9RTty5YDipHvsdgzOQtjn4l/Kw3nMgMOuNwGJxeIfCBh8YZas/V1I0cr4 LJ5+S+qPZefLuI7vgXlgWZ7COB2WuAEk3pQvohLjhoYlV9/5gZzd6IUdLhRQB3Qzw19f o+npItO66ilrURnLuH6s0E3CZEVoRn8nc2mWHT2c2NSqVRII4b4YLwFHxeerKj25oKbM ln1M5pqy3gOarfeb8+DH5h0S1bHK4y4p7wHRYylzKj+kxJaUQ0FstrljDYt8b07Egstb hwuIQ2P0bryFQiPUEMQEFf8q03dRAJgIXuf5M+YvGzNAPYQIl+xMKJdraat9foLgwE4Y 2ZnA== X-Gm-Message-State: AOAM5331GN3nPbpHOex0JLukbOGaF6L32hLJQVTwn2tD9A0wtm54Ar++ /izYH31/39913nux4oRREuvMTg== X-Google-Smtp-Source: ABdhPJx2mp2dZu1A0XSG18f8CEsROVW/j3BS/eOmMLtj0jWiktr1sOZCdImZ0JpqCeUCapoTrxSiUQ== X-Received: by 2002:a17:90a:a589:: with SMTP id b9mr9487803pjq.80.1619857210492; Sat, 01 May 2021 01:20:10 -0700 (PDT) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:8be7:e987:d00a:1442]) by smtp.gmail.com with ESMTPSA id b7sm3763003pjq.36.2021.05.01.01.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 May 2021 01:20:10 -0700 (PDT) From: Sergey Senozhatsky To: Laurent Pinchart , Ricardo Ribalda Cc: Tomasz Figa , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv5 1/5] media: v4l UAPI: add ROI selection targets Date: Sat, 1 May 2021 17:19:57 +0900 Message-Id: <20210501082001.100533-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog In-Reply-To: <20210501082001.100533-1-senozhatsky@chromium.org> References: <20210501082001.100533-1-senozhatsky@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org UVC 1.5 requires Region Of Interest control to implement GET_CUR, GET_DEF, GET_MIN and GET_MAX requests. This patch adds new V4L2 selection API targets that will implement those ROI requests. Signed-off-by: Sergey Senozhatsky --- include/uapi/linux/v4l2-common.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/uapi/linux/v4l2-common.h b/include/uapi/linux/v4l2-common.h index 7d21c1634b4d..3651ebb8cb23 100644 --- a/include/uapi/linux/v4l2-common.h +++ b/include/uapi/linux/v4l2-common.h @@ -78,6 +78,14 @@ #define V4L2_SEL_TGT_COMPOSE_BOUNDS 0x0102 /* Current composing area plus all padding pixels */ #define V4L2_SEL_TGT_COMPOSE_PADDED 0x0103 +/* Current Region of Interest area */ +#define V4L2_SEL_TGT_ROI 0x0200 +/* Default Region of Interest area */ +#define V4L2_SEL_TGT_ROI_DEFAULT 0x0201 +/* Region of Interest minimum values */ +#define V4L2_SEL_TGT_ROI_BOUNDS_MIN 0x0202 +/* Region of Interest maximum values */ +#define V4L2_SEL_TGT_ROI_BOUNDS_MAX 0x0203 /* Selection flags */ #define V4L2_SEL_FLAG_GE (1 << 0) From patchwork Sat May 1 08:19:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12234701 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8AE5AC433ED for ; Sat, 1 May 2021 08:20:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 661A361409 for ; Sat, 1 May 2021 08:20:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231791AbhEAIVE (ORCPT ); Sat, 1 May 2021 04:21:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231713AbhEAIVE (ORCPT ); Sat, 1 May 2021 04:21:04 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41AD1C06138B for ; Sat, 1 May 2021 01:20:14 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id t22so266721pgu.0 for ; Sat, 01 May 2021 01:20:14 -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=oQxgXbJ1dnrWy/sF8jzljIADGkpBbS6PQOX1pY2bYYo=; b=VURGzM4OPnfrdmIm//RVnKQL79RLkBI0T8BgdQBowgY0j5tVNn/+Hq5Py34gS/Smvg kGBDeRlUZYd1QDPXLTW6+a5FC5mPKJB1vbd9n6z4BxO3hUntkv8fm6LjZIRNd2n0i3do hMAt+TwerUTkos/kLxHqQM5tyybQb9w98VR/s= 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=oQxgXbJ1dnrWy/sF8jzljIADGkpBbS6PQOX1pY2bYYo=; b=mZSMoCgR3E4pCIZJK2U9jl3SU1sOQNYKAhMt9F70BwExzl4c86jzWXZ6zQSCExmztX Lya8oAWJU3ERkkhHmflvVFFJGC0zV8ik16upA1hvuoDtLESN7h3B3ZtBIRLb1jHEeosi X3AJ/mqiA8yKshZm1PsuhUDJ/gjCUkuQJ7A2mP5dgL/x33X30Kr0YD0fD+DNvIrTALCj ikE3FH03FDn4nRznoJKCZFee2NB/EyUqbQfhp0Y9eQjDiTa74ihMYdJGWgsqmUCLMkl3 Q3vEjWmDzvNJoLwzb6YKiCPH79gM9WWqKPoyT5baI7winYNa6kq0/j2LcWO/O/b3rETN kJZg== X-Gm-Message-State: AOAM532ND+2H4WohG5/6NT+4+CN+yaMK4UXLjiz9zJUdZxWRfu2nHKMH B8tAJ6f+YiWEc8ps4Ubq+Elr2A== X-Google-Smtp-Source: ABdhPJwSRfoW6Pm2vHysB79ZLPzTk5PqgOCbrbYBiLdUsztTASGUah0iW4Ly6W7zWsthXpAG7lSCDw== X-Received: by 2002:a63:fc10:: with SMTP id j16mr8592123pgi.152.1619857213836; Sat, 01 May 2021 01:20:13 -0700 (PDT) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:8be7:e987:d00a:1442]) by smtp.gmail.com with ESMTPSA id b7sm3763003pjq.36.2021.05.01.01.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 May 2021 01:20:13 -0700 (PDT) From: Sergey Senozhatsky To: Laurent Pinchart , Ricardo Ribalda Cc: Tomasz Figa , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv5 2/5] media: v4l UAPI: document ROI selection targets Date: Sat, 1 May 2021 17:19:58 +0900 Message-Id: <20210501082001.100533-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog In-Reply-To: <20210501082001.100533-1-senozhatsky@chromium.org> References: <20210501082001.100533-1-senozhatsky@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Document V4L2 selection targets that will be used to ROI implementation. Signed-off-by: Sergey Senozhatsky --- .../media/v4l/selection-api-configuration.rst | 22 +++++++++++++++ .../media/v4l/selection-api-examples.rst | 27 +++++++++++++++++++ .../media/v4l/v4l2-selection-targets.rst | 24 +++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/Documentation/userspace-api/media/v4l/selection-api-configuration.rst b/Documentation/userspace-api/media/v4l/selection-api-configuration.rst index fee49bf1a1c0..b5fdd765e2db 100644 --- a/Documentation/userspace-api/media/v4l/selection-api-configuration.rst +++ b/Documentation/userspace-api/media/v4l/selection-api-configuration.rst @@ -135,3 +135,25 @@ and the height of rectangles obtained using ``V4L2_SEL_TGT_CROP`` and ``V4L2_SEL_TGT_COMPOSE`` targets. If these are not equal then the scaling is applied. The application can compute the scaling ratios using these values. + +Configuration of Region of Interest (ROI) +========================================= + +The range of auto-controls values and of coordinates of the top left +corner, width and height of areas that can be ROI is given by the +``V4L2_SEL_TGT_ROI_BOUNDS_MIN`` and ``V4L2_SEL_TGT_ROI_BOUNDS_MAX`` +targets. It is recommended for the driver developers to put the top/left +corner at position ``(0,0)``. + +The top left corner, width and height of the Region of Interest area +and auto-controls currently being employed by the device are given by +the ``V4L2_SEL_TGT_ROI`` target. It uses the same coordinate system +as ``V4L2_SEL_TGT_ROI_BOUNDS_MIN`` and ``V4L2_SEL_TGT_ROI_BOUNDS_MAX``. + +In order to change active ROI top left, width and height coordinates +and ROI auto-controls use ``V4L2_SEL_TGT_ROI`` target. + +Each capture device has a default ROI rectangle and auto-controls +value given by the ``V4L2_SEL_TGT_ROI_DEFAULT`` target. Drivers shall +set the ROI rectangle to the default when the driver is first loaded, +but not later. diff --git a/Documentation/userspace-api/media/v4l/selection-api-examples.rst b/Documentation/userspace-api/media/v4l/selection-api-examples.rst index 5f8e8a1f59d7..39aba98d55f1 100644 --- a/Documentation/userspace-api/media/v4l/selection-api-examples.rst +++ b/Documentation/userspace-api/media/v4l/selection-api-examples.rst @@ -82,3 +82,30 @@ Example: Querying for scaling factors /* computing scaling factors */ hscale = (double)compose.r.width / crop.r.width; vscale = (double)compose.r.height / crop.r.height; + +Setting Region Of Interest area to half of the default value + +Example: Simple ROI +=========================== + +.. code-block:: c + + struct v4l2_selection roi = { + .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .target = V4L2_SEL_TGT_ROI_DEFAULT, + }; + struct v4l2_rect r; + + ret = ioctl(fd, VIDIOC_G_SELECTION, &roi); + if (ret) + exit(-1); + /* setting smaller ROI rectangle */ + r.width = roi.r.width / 2; + r.height = roi.r.height / 2; + r.left = roi.r.width / 4; + r.top = roi.r.height / 4; + roi.r = r; + roi.target = V4L2_SEL_TGT_ROI; + ret = ioctl(fd, VIDIOC_S_SELECTION, &roi); + if (ret) + exit(-1); diff --git a/Documentation/userspace-api/media/v4l/v4l2-selection-targets.rst b/Documentation/userspace-api/media/v4l/v4l2-selection-targets.rst index b46bae984f35..d1dc9c50eb05 100644 --- a/Documentation/userspace-api/media/v4l/v4l2-selection-targets.rst +++ b/Documentation/userspace-api/media/v4l/v4l2-selection-targets.rst @@ -75,6 +75,30 @@ of the two interfaces they are used. modified by hardware. - Yes - No + * - ``V4L2_SEL_TGT_ROI`` + - 0x0200 + - Current Region of Interest rectangle and auto-controls value. + - Yes + - No + * - ``V4L2_SEL_TGT_ROI_DEFAULT`` + - 0x0201 + - Suggested Region of Interest rectangle and auto-controls value. + - Yes + - No + * - ``V4L2_SEL_TGT_ROI_BOUNDS_MIN`` + - 0x0202 + - Minimum bounds of the Region of Interest rectangle and minimum + auto-controls value. All valid ROI rectangles and auto-controls + should be within minimum-maximum range. + - Yes + - No + * - ``V4L2_SEL_TGT_ROI_BOUNDS_MAX`` + - 0x0203 + - Maximum bounds of the Region of Interest rectangle and maximum + auto-controls value. All valid ROI rectangles and auto-controls + should be within minimum-maximum range. + - Yes + - No .. raw:: latex From patchwork Sat May 1 08:19:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12234703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9918C433ED for ; Sat, 1 May 2021 08:20:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A79DB61407 for ; Sat, 1 May 2021 08:20:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231863AbhEAIVM (ORCPT ); Sat, 1 May 2021 04:21:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231839AbhEAIVJ (ORCPT ); Sat, 1 May 2021 04:21:09 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7AE4C06174A for ; Sat, 1 May 2021 01:20:17 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id k3-20020a17090ad083b0290155b934a295so2964817pju.2 for ; Sat, 01 May 2021 01:20:17 -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=e+UREwMvCU0p3OyZGXcUE16i/Oh63EmiI8PH18y1vb4=; b=ZK8+D/as8DjRIpotVFjLYMEBQQUy+dGI7Hy0EQJY7ZqBoeb+TK3Mdp5t8sQs+CvsrO YkrOcf5HswROCirjmDM8PMIRRVVJfLM2NuCpJ2ZZ7YpJbLm/PyqnU0fjar0Wro8jN9Rb Gb+P5SBAXCCvRjMpsXb1CkdPEHi6TYHeoApNc= 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=e+UREwMvCU0p3OyZGXcUE16i/Oh63EmiI8PH18y1vb4=; b=bherDDb2vr9dOXR5JocuALnYpGHBEq8MQgiOZNiMXjoi9awgzg+XitfRUY5oLWAbik oTFZ/VfI5slt63ngFM3BR1/RxpGv9qTIpQ4X3A0BH0LJZ/cgfVNWUKV5c2F+j5fhrOQ4 ZkWQhtzniJIJXKd+lEOINnhWoY785u6Onn31FR13jg6AgqmnHaurQPd6uPbcp09p5bfb NxYCywI0vbdWtuaeIIo/j+Z8FljuGGYWKS1Hsxv8cu7aRJ8gMDv7REEzj/aJ4CnZdJyV E1kDhGTWYjJLQyBt6elH8muOWm+S78pDON9Q7BPEW+kGJEhyZ4SFg/koTRUvRGoBr3Jz QuEw== X-Gm-Message-State: AOAM530sNgeebWNGR1DfhYSClCrLROqG4uqDsIsviQlHuI/DD9Vj3pa5 ru+9DIX7U2TAOSjIFvsY7PC/sg== X-Google-Smtp-Source: ABdhPJyy4DBtvRhqdgmbFu2rM+S4smwJszQFFUF0M0OjAAThphehEU+5PDPSPQSC8/9oVO/C2f4eIg== X-Received: by 2002:a17:90a:1657:: with SMTP id x23mr341811pje.120.1619857217306; Sat, 01 May 2021 01:20:17 -0700 (PDT) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:8be7:e987:d00a:1442]) by smtp.gmail.com with ESMTPSA id b7sm3763003pjq.36.2021.05.01.01.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 May 2021 01:20:16 -0700 (PDT) From: Sergey Senozhatsky To: Laurent Pinchart , Ricardo Ribalda Cc: Tomasz Figa , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv5 3/5] media: uvcvideo: add ROI auto controls Date: Sat, 1 May 2021 17:19:59 +0900 Message-Id: <20210501082001.100533-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog In-Reply-To: <20210501082001.100533-1-senozhatsky@chromium.org> References: <20210501082001.100533-1-senozhatsky@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This patch adds support for Region of Interest bmAutoControls. ROI control is a compound data type: Control Selector CT_REGION_OF_INTEREST_CONTROL Mandatory Requests SET_CUR, GET_CUR, GET_MIN, GET_MAX, GET_DEF wLength 10 Offset Field Size 0 wROI_Top 2 2 wROI_Left 2 4 wROI_Bottom 2 6 wROI_Right 2 8 bmAutoControls 2 (Bitmap) uvc_control_mapping, however, can handle only s32 data type at the moment: ->get() returns s32 value, ->set() accepts s32 value; while v4l2_ctrl maximum/minimum/default_value can hold only s64 values. Hence ROI control handling is split into two patches: a) bmAutoControls is handled via uvc_control_mapping as V4L2_CTRL_TYPE_BITMASK b) ROI rectangle (SET_CUR, GET_CUR, GET_DEF, etc.) handling is implemented separately, by the means of selection API. Signed-off-by: Sergey Senozhatsky --- drivers/media/usb/uvc/uvc_ctrl.c | 19 +++++++++++++++++++ drivers/media/v4l2-core/v4l2-ctrls.c | 1 + include/uapi/linux/usb/video.h | 1 + include/uapi/linux/v4l2-controls.h | 10 ++++++++++ 4 files changed, 31 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index b3dde98499f4..5502fe540519 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -355,6 +355,15 @@ static const struct uvc_control_info uvc_ctrls[] = { .flags = UVC_CTRL_FLAG_GET_CUR | UVC_CTRL_FLAG_AUTO_UPDATE, }, + { + .entity = UVC_GUID_UVC_CAMERA, + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, + .index = 21, + .size = 10, + .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR + | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX + | UVC_CTRL_FLAG_GET_DEF + }, }; static const struct uvc_menu_info power_line_frequency_controls[] = { @@ -753,6 +762,16 @@ static const struct uvc_control_mapping uvc_ctrl_mappings[] = { .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN, .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN, }, + { + .id = V4L2_CID_REGION_OF_INTEREST_AUTO, + .name = "Region of Interest (auto)", + .entity = UVC_GUID_UVC_CAMERA, + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, + .size = 16, + .offset = 64, + .v4l2_type = V4L2_CTRL_TYPE_BITMASK, + .data_type = UVC_CTRL_DATA_TYPE_BITMASK, + }, }; /* ------------------------------------------------------------------------ diff --git a/drivers/media/v4l2-core/v4l2-ctrls.c b/drivers/media/v4l2-core/v4l2-ctrls.c index 0d7fe1bd975a..8c5816f3f7d4 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls.c +++ b/drivers/media/v4l2-core/v4l2-ctrls.c @@ -832,6 +832,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Min Number of Output Buffers"; case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component"; case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr"; + case V4L2_CID_REGION_OF_INTEREST_AUTO: return "Region Of Interest Auto Controls"; /* Codec controls */ /* The MPEG controls are applicable to all codec controls diff --git a/include/uapi/linux/usb/video.h b/include/uapi/linux/usb/video.h index bfdae12cdacf..9076a444758a 100644 --- a/include/uapi/linux/usb/video.h +++ b/include/uapi/linux/usb/video.h @@ -104,6 +104,7 @@ #define UVC_CT_ROLL_ABSOLUTE_CONTROL 0x0f #define UVC_CT_ROLL_RELATIVE_CONTROL 0x10 #define UVC_CT_PRIVACY_CONTROL 0x11 +#define UVC_CT_REGION_OF_INTEREST_CONTROL 0x14 /* A.9.5. Processing Unit Control Selectors */ #define UVC_PU_CONTROL_UNDEFINED 0x00 diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h index d43bec5f1afd..97f11b7a3a7c 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -993,6 +993,16 @@ enum v4l2_auto_focus_range { #define V4L2_CID_CAMERA_SENSOR_ROTATION (V4L2_CID_CAMERA_CLASS_BASE+35) +#define V4L2_CID_REGION_OF_INTEREST_AUTO (V4L2_CID_CAMERA_CLASS_BASE+36) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_EXPOSURE (1 << 0) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_IRIS (1 << 1) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_WHITE_BALANCE (1 << 2) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_FOCUS (1 << 3) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_FACE_DETECT (1 << 4) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK (1 << 5) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION (1 << 6) +#define V4L2_CID_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY (1 << 7) + /* FM Modulator class control IDs */ #define V4L2_CID_FM_TX_CLASS_BASE (V4L2_CTRL_CLASS_FM_TX | 0x900) From patchwork Sat May 1 08:20:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12234705 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 130D9C43460 for ; Sat, 1 May 2021 08:20:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E3892610E9 for ; Sat, 1 May 2021 08:20:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229517AbhEAIVP (ORCPT ); Sat, 1 May 2021 04:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231849AbhEAIVK (ORCPT ); Sat, 1 May 2021 04:21:10 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE632C06138D for ; Sat, 1 May 2021 01:20:20 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id p17so198928plf.12 for ; Sat, 01 May 2021 01:20:20 -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=kUuJOx/AeUiCPZH/3dKnOjX2Mw03lZQ+/w7qSbaYDnE=; b=jgjF5nZS67LyXAmTIvUg1x2FotO+EmeSEhgPqJelFWtgBadFr+OOszqztQb5AKG1TJ RR7sF8BfbF3y5iUJ4ZNW2pSwpV8YnA0z0vHMm/jTIAUGW0it4sTjsersGeKgRvqGxWZG 9ClMtXgePLXsSouR0HTeRfSUXHUrkqAAlsSK8= 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=kUuJOx/AeUiCPZH/3dKnOjX2Mw03lZQ+/w7qSbaYDnE=; b=ClH+RFiwjsUZIyr0aiyTCjw9Z+S+t/Lj/pPN2EnsObnPC0C/oFNVd2S9EZwagsOn/+ cyvrUzi6jLjx02xDAPFxDLYiN2cZal+z9hkMtUc5qIxbFU7uHK+WFeemCRluxpVQfp8b 3VkHU9/nrlZ7GAZM5QaiPREbrEYi1kRHFlfPhm03YxidcyKK0uPxEdTtTpXzkvJmjdrw nxPFjmOo9mvHoDcOOarGR5SEtAuV7lcxRdLIdZ8gCEiaCcepJOplkJStd97KwIMZdtDH UIHGAXwDHCdxXOjKqtN1yeLXpTb8jevPefzJ6FzICSYfOFDTz5AO1YR37wgrPC1N5xYO PlxA== X-Gm-Message-State: AOAM5312qzpaiaLBcCXaXS251WGCILDfcppI/2zoH0tydcmjeMsZkzxl G5weWii/HgHKpmjvoFZSE32zXHkxGtSPfA== X-Google-Smtp-Source: ABdhPJzGz4RS0xDBo4lWip3mqE/woDu99ydFoDvoPCsQrG5fIaxbZASrdp1oPJ9RxsFOyppuMiZaFw== X-Received: by 2002:a17:90b:3591:: with SMTP id mm17mr9346961pjb.184.1619857220478; Sat, 01 May 2021 01:20:20 -0700 (PDT) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:8be7:e987:d00a:1442]) by smtp.gmail.com with ESMTPSA id b7sm3763003pjq.36.2021.05.01.01.20.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 May 2021 01:20:20 -0700 (PDT) From: Sergey Senozhatsky To: Laurent Pinchart , Ricardo Ribalda Cc: Tomasz Figa , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv5 4/5] media: v4l UAPI: document ROI auto_controls Date: Sat, 1 May 2021 17:20:00 +0900 Message-Id: <20210501082001.100533-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog In-Reply-To: <20210501082001.100533-1-senozhatsky@chromium.org> References: <20210501082001.100533-1-senozhatsky@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org UVC 1.5 defines the following Region of Interest auto_controls |D0: Auto Exposure |D1: Auto Iris |D2: Auto White Balance |D3: Auto Focus |D4: Auto Face Detect |D5: Auto Detect and Track |D6: Image Stabilization |D7: Higher Quality |D8 – D15: Reserved, set to zero Signed-off-by: Sergey Senozhatsky --- .../media/v4l/ext-ctrls-camera.rst | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Documentation/userspace-api/media/v4l/ext-ctrls-camera.rst b/Documentation/userspace-api/media/v4l/ext-ctrls-camera.rst index 4c5061aa9cd4..f59cb4d06697 100644 --- a/Documentation/userspace-api/media/v4l/ext-ctrls-camera.rst +++ b/Documentation/userspace-api/media/v4l/ext-ctrls-camera.rst @@ -217,7 +217,30 @@ enum v4l2_auto_focus_range - * - ``V4L2_AUTO_FOCUS_RANGE_INFINITY`` - The lens is set to focus on an object at infinite distance. +``V4L2_CID_REGION_OF_INTEREST_AUTO (bitmask)`` + This determines which, if any, on board features should track to the + Region of Interest. +.. flat-table:: + :header-rows: 0 + :stub-columns: 0 + + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_EXPOSURE`` + - Auto Exposure. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_IRIS`` + - Auto Iris. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_WHITE_BALANCE`` + - Auto White Balance. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_FOCUS`` + - Auto Focus. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_FACE_DETECT`` + - Auto Face Detect. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK`` + - Auto Detect and Track. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION`` + - Image Stabilization. + * - ``V4L2_CID_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY`` + - Higher Quality. ``V4L2_CID_ZOOM_ABSOLUTE (integer)`` Specify the objective lens focal length as an absolute value. The From patchwork Sat May 1 08:20:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 12234707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CDBBC433ED for ; Sat, 1 May 2021 08:20:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EEC7F610E9 for ; Sat, 1 May 2021 08:20:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231927AbhEAIVX (ORCPT ); Sat, 1 May 2021 04:21:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231871AbhEAIVN (ORCPT ); Sat, 1 May 2021 04:21:13 -0400 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D69FC061342 for ; Sat, 1 May 2021 01:20:24 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id y1so200526plg.11 for ; Sat, 01 May 2021 01:20:24 -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=CHYuxRMx2G9OxlyPukHJZmXS6/yVwJCnqXBPckFpWCE=; b=PdbQEM9YM85rtzmOupYskTDyBpLhtZYnL6O0x9TVMbo+ZsuPGODn5naX8HbNoNuiTy dT1gSX/E0dTAR0jSej+P17bjIMNZ1zBsCZLmJ/C0q4WhWWkMD2/fu05Bl5RTtdssuUge Y8GB5NPqhVmTCFTPQMvnrbgJpm+PSYC2SXL8g= 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=CHYuxRMx2G9OxlyPukHJZmXS6/yVwJCnqXBPckFpWCE=; b=rWQZKqThDQDhIDprvxzWKfBA4rEkIO31QXy070VUiJM+X3m+bsepee+ceQQ1Tc2W3g dn6czmDdk3xvS7lGy9axCFLxzOjegPkQPAq+Sv3wDvFAM+B3I0C9Yc7lbs6X2FKR0Dz/ buAPYZWkE4awagPslQ5ummMEMinIlQ1vckB0BsLD97RDL0wjCStrmdboB0jT6+JIHgrM LrLk2S9bm//wF6jiVDpJm8H5+WPUBOBXWTFJDPEx2sy0iNGQv9wEITTHyMwAXcfoZO6y 3ghSnt37oSrLOqaukSpyiN+zakAvtdv+643f34VYOi2KIPrtQ1bbpfkc8kdkHcM88UKN LhmA== X-Gm-Message-State: AOAM533F7wV1F/R6DJnToAGeuZnpWy2uO35jAdra1+Y1rnlLSd8Eg6QQ Fp1FeIhqdE9NgNHA0E0bcZNoGg== X-Google-Smtp-Source: ABdhPJyJDe+88t94rta+S8dgXEEsG4BXtzagK2NUaIBnYm9XrgkHavMREOqg72oCEwJPvZl7jtmF7A== X-Received: by 2002:a17:90b:4a4e:: with SMTP id lb14mr18610347pjb.115.1619857223705; Sat, 01 May 2021 01:20:23 -0700 (PDT) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:8be7:e987:d00a:1442]) by smtp.gmail.com with ESMTPSA id b7sm3763003pjq.36.2021.05.01.01.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 May 2021 01:20:23 -0700 (PDT) From: Sergey Senozhatsky To: Laurent Pinchart , Ricardo Ribalda Cc: Tomasz Figa , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv5 5/5] media: uvcvideo: add UVC 1.5 ROI control Date: Sat, 1 May 2021 17:20:01 +0900 Message-Id: <20210501082001.100533-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog In-Reply-To: <20210501082001.100533-1-senozhatsky@chromium.org> References: <20210501082001.100533-1-senozhatsky@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This patch implements UVC 1.5 Region of Interest (ROI) control. Note that, UVC 1.5 defines CT_DIGITAL_WINDOW_CONTROL controls and mentions that ROI rectangle coordinates "must be within the current Digital Window as specified by the CT_WINDOW control." (4.2.2.1.20 Digital Region of Interest (ROI) Control). It's is not entirely clear if we need to implement WINDOW_CONTROL. ROI is naturally limited by GET_MIN and GET_MAX rectangles. Another thing to note is that ROI support is implemented as V4L2 selection target: selection rectangle represents ROI rectangle. Signed-off-by: Sergey Senozhatsky --- drivers/media/usb/uvc/uvc_v4l2.c | 189 ++++++++++++++++++++++++++++++- 1 file changed, 186 insertions(+), 3 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 252136cc885c..fc2ccc144cdd 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -1139,14 +1139,74 @@ static int uvc_ioctl_querymenu(struct file *file, void *fh, return uvc_query_v4l2_menu(chain, qm); } -static int uvc_ioctl_g_selection(struct file *file, void *fh, - struct v4l2_selection *sel) +/* UVC 1.5 ROI rectangle is half the size of v4l2_rect */ +struct uvc_roi_rect { + __u16 top; + __u16 left; + __u16 bottom; + __u16 right; + __u16 auto_controls; +} __packed; + +static int uvc_ioctl_g_roi_target(struct file *file, void *fh, + struct v4l2_selection *sel) { struct uvc_fh *handle = fh; struct uvc_streaming *stream = handle->stream; + struct uvc_video_chain *chain = handle->chain; + struct uvc_roi_rect *roi; + u8 query; + int ret; - if (sel->type != stream->type) + switch (sel->target) { + case V4L2_SEL_TGT_ROI: + query = UVC_GET_CUR; + break; + case V4L2_SEL_TGT_ROI_DEFAULT: + query = UVC_GET_DEF; + break; + case V4L2_SEL_TGT_ROI_BOUNDS_MIN: + query = UVC_GET_MAX; + break; + case V4L2_SEL_TGT_ROI_BOUNDS_MAX: + query = UVC_GET_MAX; + break; + default: return -EINVAL; + } + + /* hcd requires transfer buffer to be DMA capable */ + roi = kzalloc(sizeof(struct uvc_roi_rect), GFP_KERNEL); + if (!roi) + return -ENOMEM; + + /* + * Synchronize with uvc_ioctl_query_ext_ctrl() that can set + * ROI auto_controls concurrently. + */ + mutex_lock(&chain->ctrl_mutex); + + ret = uvc_query_ctrl(stream->dev, query, 1, stream->dev->intfnum, + UVC_CT_REGION_OF_INTEREST_CONTROL, roi, + sizeof(struct uvc_roi_rect)); + if (!ret) { + /* ROI left, top, right, bottom are global coordinates. */ + sel->r.left = roi->left; + sel->r.top = roi->top; + sel->r.width = roi->right - roi->left + 1; + sel->r.height = roi->bottom - roi->top + 1; + } + + mutex_unlock(&chain->ctrl_mutex); + kfree(roi); + return ret; +} + +static int uvc_ioctl_g_sel_target(struct file *file, void *fh, + struct v4l2_selection *sel) +{ + struct uvc_fh *handle = fh; + struct uvc_streaming *stream = handle->stream; switch (sel->target) { case V4L2_SEL_TGT_CROP_DEFAULT: @@ -1173,6 +1233,128 @@ static int uvc_ioctl_g_selection(struct file *file, void *fh, return 0; } +static int uvc_ioctl_g_selection(struct file *file, void *fh, + struct v4l2_selection *sel) +{ + struct uvc_fh *handle = fh; + struct uvc_streaming *stream = handle->stream; + + if (sel->type != stream->type) + return -EINVAL; + + switch (sel->target) { + case V4L2_SEL_TGT_CROP_DEFAULT: + case V4L2_SEL_TGT_CROP_BOUNDS: + case V4L2_SEL_TGT_COMPOSE_DEFAULT: + case V4L2_SEL_TGT_COMPOSE_BOUNDS: + return uvc_ioctl_g_sel_target(file, fh, sel); + case V4L2_SEL_TGT_ROI: + case V4L2_SEL_TGT_ROI_DEFAULT: + case V4L2_SEL_TGT_ROI_BOUNDS_MIN: + case V4L2_SEL_TGT_ROI_BOUNDS_MAX: + return uvc_ioctl_g_roi_target(file, fh, sel); + } + + return -EINVAL; +} + +static void validate_roi_bounds(struct uvc_streaming *stream, + struct v4l2_selection *sel) +{ + lockdep_assert_held(&stream->mutex); + + if (sel->r.left > USHRT_MAX) + sel->r.left = 0; + + if (sel->r.top > USHRT_MAX) + sel->r.top = 0; + + if (sel->r.width + sel->r.left > USHRT_MAX || !sel->r.width) { + sel->r.left = 0; + sel->r.width = stream->cur_frame->wWidth; + } + + if (sel->r.height + sel->r.top > USHRT_MAX || !sel->r.height) { + sel->r.top = 0; + sel->r.height = stream->cur_frame->wHeight; + } +} + +static int uvc_ioctl_s_roi(struct file *file, void *fh, + struct v4l2_selection *sel) +{ + struct uvc_fh *handle = fh; + struct uvc_streaming *stream = handle->stream; + struct uvc_video_chain *chain = handle->chain; + struct uvc_roi_rect *roi; + int ret; + + /* hcd requires transfer buffer to be DMA capable */ + roi = kzalloc(sizeof(struct uvc_roi_rect), GFP_KERNEL); + if (!roi) + return -ENOMEM; + + /* + * Synchronize with uvc_ioctl_query_ext_ctrl() that can set + * ROI auto_controls concurrently. + */ + mutex_lock(&chain->ctrl_mutex); + + /* + * Get current ROI configuration. We are especially interested in + * ->auto_controls, because we will use GET_CUR ->auto_controls + * value for SET_CUR. Some firmwares require sizeof(uvc_roi_rect) + * to be 5 * sizeof(__u16) so we need to set correct rectangle + * dimensions and correct auto_controls value. + */ + ret = uvc_query_ctrl(stream->dev, UVC_GET_CUR, 1, stream->dev->intfnum, + UVC_CT_REGION_OF_INTEREST_CONTROL, roi, + sizeof(struct uvc_roi_rect)); + if (ret) + goto out; + + mutex_lock(&stream->mutex); + + validate_roi_bounds(stream, sel); + + /* + * ROI left, top, right, bottom are global coordinates. + * Note that we use ->auto_controls value which we read earlier. + */ + roi->left = sel->r.left; + roi->top = sel->r.top; + roi->right = sel->r.width + sel->r.left - 1; + roi->bottom = sel->r.height + sel->r.top - 1; + + ret = uvc_query_ctrl(stream->dev, UVC_SET_CUR, 1, stream->dev->intfnum, + UVC_CT_REGION_OF_INTEREST_CONTROL, roi, + sizeof(struct uvc_roi_rect)); + + mutex_unlock(&stream->mutex); + +out: + mutex_unlock(&chain->ctrl_mutex); + kfree(roi); + return ret; +} + +static int uvc_ioctl_s_selection(struct file *file, void *fh, + struct v4l2_selection *sel) +{ + struct uvc_fh *handle = fh; + struct uvc_streaming *stream = handle->stream; + + if (sel->type != stream->type) + return -EINVAL; + + switch (sel->target) { + case V4L2_SEL_TGT_ROI: + return uvc_ioctl_s_roi(file, fh, sel); + } + + return -EINVAL; +} + static int uvc_ioctl_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm) { @@ -1533,6 +1715,7 @@ const struct v4l2_ioctl_ops uvc_ioctl_ops = { .vidioc_try_ext_ctrls = uvc_ioctl_try_ext_ctrls, .vidioc_querymenu = uvc_ioctl_querymenu, .vidioc_g_selection = uvc_ioctl_g_selection, + .vidioc_s_selection = uvc_ioctl_s_selection, .vidioc_g_parm = uvc_ioctl_g_parm, .vidioc_s_parm = uvc_ioctl_s_parm, .vidioc_enum_framesizes = uvc_ioctl_enum_framesizes,