From patchwork Fri Dec 2 17:19:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 29035 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp981911wrr; Fri, 2 Dec 2022 09:25:54 -0800 (PST) X-Google-Smtp-Source: AA0mqf48kGEjUGpWvieIgJlqoo7hTO+qDc3WifviOQ4FTLr6UGcMkDol4y2J3ZfD5Qr1QGgs2hRG X-Received: by 2002:a17:903:2351:b0:189:6574:a4b7 with SMTP id c17-20020a170903235100b001896574a4b7mr39432725plh.107.1670001953765; Fri, 02 Dec 2022 09:25:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670001953; cv=none; d=google.com; s=arc-20160816; b=jxvbdcFKx09C7NzgDFOY3JBv3Ee5dOiiax3MBrpIJn0614APmo2OY072UPnlKIs1gC PbM2gZj3/0kh8/Ud6HJAzJNJ/zBIn1adABxZ4RcIMLNxRMkG1aub1aodFC2DrrxWkD1U 78d+GJKmlCmOHxCUyBJQGcJv2QVULxCIa0c6LHnW4bas5/fW3Xi6OTbQewVbmHfjQ34o nOdZNxJDAE8FTXq6BOtcDuqrV+qjMVQvdOPr5x01Jve/uSrkUdWnfMPDmr/td3wAepYY c3i7O2Vkmd3K/CE0nh0NlUL+T5DX6lcGFlCakzUpc2k0cVvhbB+s2FWFuiHcxBQTr2Po PNfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=s4bSOSlIclNNMQjhpRHtDeBwxprB0cAQM7uBgtvrsE0=; b=IIOggse/VtOjUzMUCIaKV+pp6Y/ev0KD0GfISByZe4y9T0mzOxQ0VucpLs2HgvLEZ0 LSo8/OCqXfAOkA/vDUjz/i8R53OGL4ZA+TFQS8gh+9elS7+TqlUM7zEyeMcKyckgiU9Z DCiZA/jWHINz0LG9zCo4BC7mfWtPtD5i3RhoZxl1aOmNykfX9glS1ZeGNJ/LqAyrS9YM qq4qiSoBS1tvUBF2m0sc6N6bJP9gvjckz8WWDqk99GuZQbhnrXb3ZqO2RAqqAy9EKtQL oWRaz7YZxFWci+mTXOvaT9ZEPZMDwatvqywelov47Rqyqpc0EM3SlSNmjUzI/JENBi80 ce3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IBEHZHv0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l22-20020a635716000000b00477aa9a43d5si7920175pgb.616.2022.12.02.09.25.39; Fri, 02 Dec 2022 09:25:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IBEHZHv0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233637AbiLBRTw (ORCPT + 99 others); Fri, 2 Dec 2022 12:19:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233807AbiLBRTr (ORCPT ); Fri, 2 Dec 2022 12:19:47 -0500 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64DFD862D3 for ; Fri, 2 Dec 2022 09:19:46 -0800 (PST) Received: by mail-ej1-x630.google.com with SMTP id bj12so13022324ejb.13 for ; Fri, 02 Dec 2022 09:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=s4bSOSlIclNNMQjhpRHtDeBwxprB0cAQM7uBgtvrsE0=; b=IBEHZHv0WH5QpgzyMh/FDZY7giKGelj7WrVy6ToYlYHm52XFl/KS36wnfihqGQfxHa Z7SMeR0plGpOvu8dDta2o/Fa9mEzNb7ghpeqFFUWUPFv1KAGIKNzKCAiZZCOh6EyU8Yq GRsUSuyY8FV457PCDDkTfUC/BzSytXx0ig3lU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s4bSOSlIclNNMQjhpRHtDeBwxprB0cAQM7uBgtvrsE0=; b=VvLG73hwUxTy9ZCmHaNGqrYBVW9WXCLyzwOCnSSk9vWYINdrYKQHeSuHzTGUq3r2Vx fFzWwDP0WkyIOD712FzKPCfBebqIym8As+Bwg3e22VOgm9pdt3KaYoOh49odU9g88iI9 qu74PZrHy7HC1X0EvEcTy5mpkLefyI0dzffwP3pzdSUjxn7oU0zDUGTXKdq+zwue5//J en2lUFvPxcRmNvpDdCZce+RJkaBkTxmr+IvRtkcVqaCf19v6loajZM8XZWeWms+FUTDm 6n2N7MSAwN0S04QbsA5w5PXd74zrywVdR54QkkvL2W6JlKyNpMTpfiB9yMV7pVO4yD1U ZpIQ== X-Gm-Message-State: ANoB5pn9gwkTIV0h2hs4Xax4iOfygiXdhFMNykheYQpDUggs1SdTGxNL NZsfXcWCjIJ2s7OsKz5etqfaCg== X-Received: by 2002:a17:906:d295:b0:7c0:aff1:f1b6 with SMTP id ay21-20020a170906d29500b007c0aff1f1b6mr7039524ejb.418.1670001584870; Fri, 02 Dec 2022 09:19:44 -0800 (PST) Received: from alco.roam.corp.google.com (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id l4-20020aa7c304000000b00458dc7e8ecasm3117864edq.72.2022.12.02.09.19.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Dec 2022 09:19:44 -0800 (PST) From: Ricardo Ribalda Date: Fri, 02 Dec 2022 18:19:31 +0100 Subject: [PATCH v4 1/2] media: uvcvideo: Refactor streamon/streamoff MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v4-1-47484ae40761@chromium.org> References: <20220920-resend-powersave-v4-0-47484ae40761@chromium.org> In-Reply-To: <20220920-resend-powersave-v4-0-47484ae40761@chromium.org> To: Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, Yunke Cao , Max Staudt , Hans Verkuil , Laurent Pinchart , Tomasz Figa , Guenter Roeck , Sergey Senozhatsky , Alan Stern , Ricardo Ribalda , linux-kernel@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2216; i=ribalda@chromium.org; h=from:subject:message-id; bh=it0NuE/Vzlg0rdtNCao9FxFkt3XwIbqERC0L8SjLc2M=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjijOqhC3V07c53unnuqEQsWOyNuPKlRrDkDPQ5XOJ FC3WYDGJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY4ozqgAKCRDRN9E+zzrEiBRiD/ 9moZtau9pKvyLy0ry+B+VFxfqRBh0kJe9Na4fV3RTQyL78Ax7jW+6XDflbPTV11RuodZ4EW7IgI5Jb F4icdovhxKQNLijZKyQcmDWrfU8gGLM2Mxnlo7a6UcQcyaP8h0rRWHwDvwnKwDtu5FfoQcABZW3jfk mPgVzLiJgMMN4ERLyRFPdW0MlmQ+7aIZ2qDS3d6DNbgaYWrmoinUkxhlknyxxHvTNQlbmzUnKETvjR YmLGlkNocsGpdUMVW+JhRrs2Ue40temCwRoaptGvIOcXFqY0kre7x9bokmb1OCwe3E4WPY8gJXM8Aj tCbVd2m8YkAYikrEY6LEu/Q2Tjqy0Gog43ab/+EUR4clrs39VU01owZsGsBXsCrnWyA3Zh7S6dx8/R N+TbGUWUZoad0m/qo4sJmJMJtV2BhRXcrdeCq4EpYbBjV/DlkF0KdTs4dyOl4tALY5+1IhZwkBCapv f52mls3sjPsW7u6Ktf/JHNEjmrDLiAQlL5w/2yzaMgQUA4zT7eKNUU7FCSm3YUNUGW06zBYUcJ0+80 WABpVbOZSOEa0DANf5eogCJAtFkfU3bZYxRLCBOye2DUbc+nnkpBTC3BjkdpTr+vu2B9XFnjIzcCRP 2GT9MoPStpR9Tv5WX4Fsm29njlV7LTqg2GacaMqKv33ErCIUCPdbZjISqFZQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751123968558134329?= X-GMAIL-MSGID: =?utf-8?q?1751123968558134329?= Add a new variable to handle the streaming state and handle the streamoff errors, that were not handled before. Suggested-by: Laurent Pinchart Reviewed-by: Max Staudt Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_v4l2.c | 19 ++++++++++++++++--- drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f4d4c33b6dfb..1389a87b8ae1 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -840,13 +840,19 @@ static int uvc_ioctl_streamon(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_streaming *stream = handle->stream; - int ret; + int ret = -EBUSY; if (!uvc_has_privileges(handle)) return -EBUSY; mutex_lock(&stream->mutex); + + if (handle->is_streaming) + goto unlock; ret = uvc_queue_streamon(&stream->queue, type); + handle->is_streaming = !ret; + +unlock: mutex_unlock(&stream->mutex); return ret; @@ -857,15 +863,22 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_streaming *stream = handle->stream; + int ret = 0; if (!uvc_has_privileges(handle)) return -EBUSY; mutex_lock(&stream->mutex); - uvc_queue_streamoff(&stream->queue, type); + + if (!handle->is_streaming) + goto unlock; + ret = uvc_queue_streamoff(&stream->queue, type); + handle->is_streaming = !!ret; + +unlock: mutex_unlock(&stream->mutex); - return 0; + return ret; } static int uvc_ioctl_enum_input(struct file *file, void *fh, diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index df93db259312..45310f55475f 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -581,6 +581,7 @@ enum uvc_handle_state { struct uvc_fh { struct v4l2_fh vfh; + bool is_streaming; struct uvc_video_chain *chain; struct uvc_streaming *stream; enum uvc_handle_state state; From patchwork Fri Dec 2 17:19:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 29037 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp981919wrr; Fri, 2 Dec 2022 09:25:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf7C+Ae3smXD61lK3QTdQv+3AH1LE9ks2n3CYz7OOAAOClOihPFJHxQKo849ys7nz/9Gq6kB X-Received: by 2002:a62:7bd4:0:b0:576:8e7d:2c0a with SMTP id w203-20020a627bd4000000b005768e7d2c0amr454255pfc.54.1670001955099; Fri, 02 Dec 2022 09:25:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670001955; cv=none; d=google.com; s=arc-20160816; b=Mpx5pE2pOVa6Jv/Qb35irgC/Wt/R9nCA41zRx8C6KIaYP2GJgVfkh36S0V/VkfX04z lUGN7+VNHamXoIr8nHtBKMg9QMwd18FxYUP79174TElNl27krwCm88hnKW1nOMkZb5TG jTb/bjjXtY0UzJ1muqnFkLHQleomVyRim5QyVPgKLs2NPKCqCtxc3+Bs3aqnuyqrjnz8 7OJvOKAf+XgwCzssnUStbGwQj32dKZQYmi1WjCrpWOHtIzHfw5ZPFfFCoRq9AR/LgK0n lhx6Xbp/iXktSqRyMxvz+CxjSH7oqf05BDvVRA3ByZ3xhiUfdKGcEg5B0B8PhpekZiBW /6FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=DFhDF/K1xBNCd81nvtlYA1OZ1iPMNCjyBMR69X5yrP8=; b=KM/dmr25CGhCjfkmpaqAAJmRFYeu2eHfDDInKNTofgsVk6AvYNv8noV1+KFQmUYeTF Vpzo3H604TMuZY6E6ZyhGkeDovQ5HphlFfYSWq0KLE9hGwQ2tgW+d1HWGtKvbyRzfrbz s9ogf+OEsNFkvUkuD4jQXTJTfMiWbXwrj/Udm6i/wLHi2KrcnEeSRr6qpZHuvyzXzSVz lfE9bLs8Eu/qh0CmHQOY5Fnkr0AxnYdnkIRAFy1eWo5YFA54ZTam3F+BnKjYiE+OHlpK 08G03KJAKDgMl31NUu8Ax7e7yB33u7jAlrz81oHyKAx0uJuibpFWS0JMKmXfHwdRSplP GGqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OnqfxpYB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b3-20020a056a000cc300b0056bce3dfaacsi8164047pfv.25.2022.12.02.09.25.41; Fri, 02 Dec 2022 09:25:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OnqfxpYB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234069AbiLBRT4 (ORCPT + 99 others); Fri, 2 Dec 2022 12:19:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233757AbiLBRTs (ORCPT ); Fri, 2 Dec 2022 12:19:48 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18F23DE84 for ; Fri, 2 Dec 2022 09:19:47 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id vp12so13052073ejc.8 for ; Fri, 02 Dec 2022 09:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=DFhDF/K1xBNCd81nvtlYA1OZ1iPMNCjyBMR69X5yrP8=; b=OnqfxpYBgq8BfSAZy+0aiDmYmu4Ui/nnt1BwsyP4svRb1/JRQj2jsYjwDPit9TgF3x zBr3n0afLb0y5bu0zKq/jvP9jBSuuApOfW5uitOiyo4BynkDeM4gWg16s7qcAca3NuM5 7ZzTnDc0pU++9WJX6SkNfIleoQIrvN19GTWxs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DFhDF/K1xBNCd81nvtlYA1OZ1iPMNCjyBMR69X5yrP8=; b=mapTm71tY9xO7tlsuEXFjQ8h3EM0qjGceS2KUTQTv9qzIbLf8v/skCNtSiClIER5oE iTgM6qjCTDoWmsKnrUs0I/XmLOVkftaYW3XqqFZhFG2WVLCNLeVNtDPCwibja2jBGl0M x4jgWp/j6rN2l+YtbqNUfukz13kwleh4u5SClniY9Afr948UJYIFKrH07Zt3uW8G5Wdw hzAKEiCo6SohjFJj+MY0uWX6BMW7jR46rTfCuz5TO5hc4MIf4Et4rAkp4OMtGasGMUBe 2PyDwhliHG2m8JGOY6qndlkzxkfreCxWuQwVg72IS4a3q4d3LM8PORng/fe+5uYkC9WG ycPQ== X-Gm-Message-State: ANoB5pnh8zE0pEagEn71PCOktI75Mn7SFv8LfDE/Zi9levjpwQeGDFT+ NYCoVk4aXU3YGoyO0tRp1fHT7fvDkxFYxNuRkeg= X-Received: by 2002:a17:906:fd57:b0:7be:5c6f:e63c with SMTP id wi23-20020a170906fd5700b007be5c6fe63cmr24887069ejb.253.1670001585627; Fri, 02 Dec 2022 09:19:45 -0800 (PST) Received: from alco.roam.corp.google.com (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id l4-20020aa7c304000000b00458dc7e8ecasm3117864edq.72.2022.12.02.09.19.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Dec 2022 09:19:45 -0800 (PST) From: Ricardo Ribalda Date: Fri, 02 Dec 2022 18:19:32 +0100 Subject: [PATCH v4 2/2] media: uvcvideo: Do power management granularly MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v4-2-47484ae40761@chromium.org> References: <20220920-resend-powersave-v4-0-47484ae40761@chromium.org> In-Reply-To: <20220920-resend-powersave-v4-0-47484ae40761@chromium.org> To: Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, Yunke Cao , Max Staudt , Hans Verkuil , Laurent Pinchart , Tomasz Figa , Guenter Roeck , Sergey Senozhatsky , Alan Stern , Ricardo Ribalda , linux-kernel@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=10459; i=ribalda@chromium.org; h=from:subject:message-id; bh=SSBrN/bsc2uIn+IxDFf5q8l+vH/Kaz2gJNRm1Tk/syE=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjijOt9+69pqS/JyjZzJxwAfjl/TVNcKY1RM9s8Rt1 4ku4YtKJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY4ozrQAKCRDRN9E+zzrEiMIVD/ 9bJaHkb7L8HHLwtmaSp3YyoOpfDsiLWexi55+XzBDd8tLkMs39zKWfI3iTd/hob8AOGl7obuCsq+HL zar1D4pCWUONdTxo/UgbkMwL8LyQyuMw96jWcsG2slZ4CenSj5g8CTe9kTd3niOxznlRcH9rZW9XYY QQTYs2vclsTEyEqrC7vGqCWogXA0vdxk1qRlqaRr42VH4/Nwp/nkhDmoJVZWTBtBZ3WVRqrDs5zU/c VjbQgxdDqpAXNutD0cDHYcGeezpZThnrGz90L6VdVYa5zT0Q3IH7EtZWViUpROkUU9Qsa6bdL1ZJGq AiLM2MJb080zvoZXJi1OsspoznoZePjn2m2U2LEvARlGlwk+b0IlTKLCEsnRiUD3PR7asgNC5ceySj YBk4X1guM1X+uM4cI+jV9bFeoJ59Lb3RodbmREa/hLZ0dedaORhZE2LhE1v9YmSvTqjmkUlb69ZO6s B7RjuTH9FD2+5p/i410rYg+wHEuVV9WFSGCRElIz8Li5swyPdUmv92eHbnOU6XVRVkbJ6lfkApOOdv KPfghPjgOQvlBv97E9xiuUdpes5Q3sc4C1vNFPNfxaRB5AX7lptsmgCHRBc2QlfBhifQlSP9D8nZK3 larJbpfx9uLosT9VlM5+0oPIHupUqMREJEVFWca4EO8x9MpwhMeMdKWgTZPQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751123969937959281?= X-GMAIL-MSGID: =?utf-8?q?1751123969937959281?= Instead of suspending/resume the USB device at open()/close(), do it when the device is actually used. This way we can reduce the power consumption when a service is holding the video device and leaving it in an idle state. Reviewed-by: Tomasz Figa Reviewed-by: Max Staudt Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_v4l2.c | 174 ++++++++++++++++++++++++++++++++------- 1 file changed, 142 insertions(+), 32 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 1389a87b8ae1..f5b0f1905962 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -25,6 +25,46 @@ #include "uvcvideo.h" +/* ------------------------------------------------------------------------ + * UVC power management + */ + +static int uvc_pm_get(struct uvc_streaming *stream) +{ + int ret; + + ret = usb_autopm_get_interface(stream->dev->intf); + if (ret) + return ret; + + mutex_lock(&stream->dev->lock); + if (!stream->dev->users) + ret = uvc_status_start(stream->dev, GFP_KERNEL); + if (!ret) + stream->dev->users++; + mutex_unlock(&stream->dev->lock); + + if (ret) + usb_autopm_put_interface(stream->dev->intf); + + return ret; +} + +static void uvc_pm_put(struct uvc_streaming *stream) +{ + mutex_lock(&stream->dev->lock); + if (WARN_ON(!stream->dev->users)) { + mutex_unlock(&stream->dev->lock); + return; + } + stream->dev->users--; + if (!stream->dev->users) + uvc_status_stop(stream->dev); + mutex_unlock(&stream->dev->lock); + + usb_autopm_put_interface(stream->dev->intf); +} + /* ------------------------------------------------------------------------ * UVC ioctls */ @@ -249,6 +289,9 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream, * developers test their webcams with the Linux driver as well as with * the Windows driver). */ + ret = uvc_pm_get(stream); + if (ret) + return ret; mutex_lock(&stream->mutex); if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) probe->dwMaxVideoFrameSize = @@ -257,6 +300,7 @@ static int uvc_v4l2_try_format(struct uvc_streaming *stream, /* Probe the device. */ ret = uvc_probe_video(stream, probe); mutex_unlock(&stream->mutex); + uvc_pm_put(stream); if (ret < 0) return ret; @@ -468,7 +512,13 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream, } /* Probe the device with the new settings. */ + ret = uvc_pm_get(stream); + if (ret) { + mutex_unlock(&stream->mutex); + return ret; + } ret = uvc_probe_video(stream, &probe); + uvc_pm_put(stream); if (ret < 0) { mutex_unlock(&stream->mutex); return ret; @@ -559,36 +609,29 @@ static int uvc_v4l2_open(struct file *file) { struct uvc_streaming *stream; struct uvc_fh *handle; - int ret = 0; stream = video_drvdata(file); uvc_dbg(stream->dev, CALLS, "%s\n", __func__); - ret = usb_autopm_get_interface(stream->dev->intf); - if (ret < 0) - return ret; - /* Create the device handle. */ handle = kzalloc(sizeof(*handle), GFP_KERNEL); - if (handle == NULL) { - usb_autopm_put_interface(stream->dev->intf); + if (!handle) return -ENOMEM; - } - mutex_lock(&stream->dev->lock); - if (stream->dev->users == 0) { - ret = uvc_status_start(stream->dev, GFP_KERNEL); - if (ret < 0) { - mutex_unlock(&stream->dev->lock); - usb_autopm_put_interface(stream->dev->intf); + /* + * If the uvc evdev exists we cannot suspend when the device + * is idle. Otherwise we will miss button actions. + */ + if (stream->dev->input) { + int ret; + + ret = uvc_pm_get(stream); + if (ret) { kfree(handle); return ret; } } - stream->dev->users++; - mutex_unlock(&stream->dev->lock); - v4l2_fh_init(&handle->vfh, &stream->vdev); v4l2_fh_add(&handle->vfh); handle->chain = stream->chain; @@ -610,6 +653,12 @@ static int uvc_v4l2_release(struct file *file) if (uvc_has_privileges(handle)) uvc_queue_release(&stream->queue); + if (handle->is_streaming) + uvc_pm_put(stream); + + if (stream->dev->input) + uvc_pm_put(stream); + /* Release the file handle. */ uvc_dismiss_privileges(handle); v4l2_fh_del(&handle->vfh); @@ -617,12 +666,6 @@ static int uvc_v4l2_release(struct file *file) kfree(handle); file->private_data = NULL; - mutex_lock(&stream->dev->lock); - if (--stream->dev->users == 0) - uvc_status_stop(stream->dev); - mutex_unlock(&stream->dev->lock); - - usb_autopm_put_interface(stream->dev->intf); return 0; } @@ -849,9 +892,17 @@ static int uvc_ioctl_streamon(struct file *file, void *fh, if (handle->is_streaming) goto unlock; + + ret = uvc_pm_get(stream); + if (ret) + goto unlock; + ret = uvc_queue_streamon(&stream->queue, type); handle->is_streaming = !ret; + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); @@ -875,6 +926,9 @@ static int uvc_ioctl_streamoff(struct file *file, void *fh, ret = uvc_queue_streamoff(&stream->queue, type); handle->is_streaming = !!ret; + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); @@ -928,6 +982,7 @@ static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input) { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; u8 *buf; int ret; @@ -941,9 +996,16 @@ static int uvc_ioctl_g_input(struct file *file, void *fh, unsigned int *input) if (!buf) return -ENOMEM; + ret = uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, chain->selector->id, chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, buf, 1); + uvc_pm_put(stream); if (!ret) *input = *buf - 1; @@ -956,6 +1018,7 @@ static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input) { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; u8 *buf; int ret; @@ -977,10 +1040,17 @@ static int uvc_ioctl_s_input(struct file *file, void *fh, unsigned int input) if (!buf) return -ENOMEM; + ret = uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + *buf = input + 1; ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, chain->selector->id, chain->dev->intfnum, UVC_SU_INPUT_SELECT_CONTROL, buf, 1); + uvc_pm_put(stream); kfree(buf); return ret; @@ -991,8 +1061,15 @@ static int uvc_ioctl_queryctrl(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; + int ret; - return uvc_query_v4l2_ctrl(chain, qc); + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_query_v4l2_ctrl(chain, qc); + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, @@ -1000,10 +1077,15 @@ static int uvc_ioctl_query_ext_ctrl(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; struct v4l2_queryctrl qc = { qec->id }; int ret; + ret = uvc_pm_get(stream); + if (ret) + return ret; ret = uvc_query_v4l2_ctrl(chain, &qc); + uvc_pm_put(stream); if (ret) return ret; @@ -1049,6 +1131,7 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; struct v4l2_ext_control *ctrl = ctrls->controls; unsigned int i; int ret; @@ -1073,22 +1156,30 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, void *fh, return 0; } + ret = uvc_pm_get(stream); + if (ret) + return ret; ret = uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } for (i = 0; i < ctrls->count; ++ctrl, ++i) { ret = uvc_ctrl_get(chain, ctrl); if (ret < 0) { uvc_ctrl_rollback(handle); ctrls->error_idx = i; - return ret; + goto done; } } ctrls->error_idx = 0; - return uvc_ctrl_rollback(handle); + ret = uvc_ctrl_rollback(handle); +done: + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, @@ -1097,6 +1188,7 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, { struct v4l2_ext_control *ctrl = ctrls->controls; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; unsigned int i; int ret; @@ -1104,9 +1196,15 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, if (ret < 0) return ret; + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } for (i = 0; i < ctrls->count; ++ctrl, ++i) { ret = uvc_ctrl_set(handle, ctrl); @@ -1114,16 +1212,20 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *handle, uvc_ctrl_rollback(handle); ctrls->error_idx = ioctl == VIDIOC_S_EXT_CTRLS ? ctrls->count : i; - return ret; + goto done; } } ctrls->error_idx = 0; if (ioctl == VIDIOC_S_EXT_CTRLS) - return uvc_ctrl_commit(handle, ctrls); + ret = uvc_ctrl_commit(handle, ctrls); else - return uvc_ctrl_rollback(handle); + ret = uvc_ctrl_rollback(handle); + +done: + uvc_pm_put(stream); + return ret; } static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh, @@ -1147,8 +1249,16 @@ static int uvc_ioctl_querymenu(struct file *file, void *fh, { struct uvc_fh *handle = fh; struct uvc_video_chain *chain = handle->chain; + struct uvc_streaming *stream = handle->stream; + int ret; + + ret = uvc_pm_get(stream); + if (ret) + return ret; + ret = uvc_query_v4l2_menu(chain, qm); + uvc_pm_put(stream); - return uvc_query_v4l2_menu(chain, qm); + return ret; } static int uvc_ioctl_g_selection(struct file *file, void *fh,