From nobody Thu Apr 2 23:14:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0956ECAAD8 for ; Tue, 20 Sep 2022 14:12:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231479AbiITOM3 (ORCPT ); Tue, 20 Sep 2022 10:12:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231649AbiITOLw (ORCPT ); Tue, 20 Sep 2022 10:11:52 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB16B61B01 for ; Tue, 20 Sep 2022 07:10:19 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id e17so4029101edc.5 for ; Tue, 20 Sep 2022 07:10:19 -0700 (PDT) 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; bh=ucPdrtJkPwf0BboNbtvdco9R/hcnAGguJpObhkf38C8=; b=HO5sr1cWLwa3y35YNEdTI16wyVUtMIACshxUIA4KWnbNs3UCm98hSB4OHTprMEwC/a T8LpJmaajKMQiwbza8YSKHfEnyK9uF7Y6dWfRWhnE+BDtPJAzNdUGCBQr4KkiG+XQf+Y P1IQdSd10CLIj2Iw85QCEoe+CFAkCWf5GjREY= 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; bh=ucPdrtJkPwf0BboNbtvdco9R/hcnAGguJpObhkf38C8=; b=sB1cBlmceXZx73bX/Uu248/WetpqWehJbeYXYdpfB+4kSHgaWjdmT1atAh8zZvXdou 1pIT4m++p2ZBnZaX7c2TuCK2l0inEdz0cAmUbwNkexPeSP1DCjoG0SmC52yQ8BYn9zpq lM2bWq4uxaTmkiNMD0112JPRC1pAKP4WdSWaG49s5DErnslQS/i9HXWMVHn3m9baRBf0 v40phvHuwGvkp9ySgjENZZHo1jP2WlGJ3tKoPiPd/+VV/gl+R58gj9/2oBh1NdzQhuwv B7NQ1ViMo8SyIirxZ33V3rdck0JLQSRI1phvfFaLNvAJJ59Krf1c1PFoTzPkyr0Q0P6S vVQQ== X-Gm-Message-State: ACrzQf3EFrkwJ+Ahoj+1r5BZPRHVHoWTo8vvsRxoV+Ljp+OsKUtk+Jrm rVvC7eA3Asx817REXiAjSQQZCuFrOJK/k5qsXBA= X-Google-Smtp-Source: AMsMyM4lxDO7g4HuMho4z6sf2P/lecptqkY7Q12fxuVeWwVVMeeySCymELdzq/VUaWzsQwXg81GsMw== X-Received: by 2002:a05:6402:26d2:b0:451:5a8c:346b with SMTP id x18-20020a05640226d200b004515a8c346bmr19975208edd.424.1663683005196; Tue, 20 Sep 2022 07:10:05 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:935d:52b0:7461:88e1]) by smtp.gmail.com with ESMTPSA id w11-20020a056402128b00b0044e796598bdsm115488edv.11.2022.09.20.07.10.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Sep 2022 07:10:04 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 20 Sep 2022 16:09:52 +0200 Subject: [PATCH v1 3/3] media: uvcvideo: Do power management granularly MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20220920-resend-powersave-v1-3-123aa2ba3836@chromium.org> References: <20220920-resend-powersave-v1-0-123aa2ba3836@chromium.org> In-Reply-To: <20220920-resend-powersave-v1-0-123aa2ba3836@chromium.org> To: Mauro Carvalho Chehab Cc: linux-kernel@vger.kernel.org, Tomasz Figa , Laurent Pinchart , Ricardo Ribalda , linux-media@vger.kernel.org X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=10244; i=ribalda@chromium.org; h=from:subject:message-id; bh=mQK7QNORiDfMtgiTEcpPSa/3OoMx5fx8L+y3jCnHq5c=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjKcm4Y0ax3mHrs1uEOk3ppGkwa6bxcvCk1VY/vljo 6dcksceJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCYynJuAAKCRDRN9E+zzrEiD93D/ 9CkA+JO7fksIwxy/Ssk6nrHJm1sVDO7SuMdyFr1TYCxeCYqIcCgdfTqtYbB0vNllSJcXh1GUMbR1c4 U51czGB1995pULG+dE7hnr5o1VDsBOCpSlKjeS0hpOAaQywgQH9qpJ3Dpr5sWbXh7Z+CqFWt9JL6B8 cKkIBnu0ZHJpgSptU0Nge/VsJDSnYZARZglarzCdrh/MnxKiwSuDYo02TwNQU5k36N8tsOgQ0CrDj3 rLR1oqSn2osS+d5kg2MGZVc8/XvjjE11ESov7E1MdVo/TI+6p+YjvEBDzEpL1Ro1vW+nZa+1IqvUXt LMVDDv8OcN7JFgCTbPMACETlNQIVbjZX4g6YGvVg0FUJdlBK0/F/DvFxyunzOay8HYIp0iIk/TOGdh FntTnv3e+lRrRukkIw+9GgxDoHwy7t4PIijOa48GMLaTJdfSsSu2chu0twMhWabPb1BoyPXngWltYn 6tyPxp8DurOTeVlaCqGG6tC3q6T76rdQrqTC0L1ZMdsrhO4uWy3k8KRZwYPTyjVDDyOAznAH/kH5e3 kS6hCHMLaOYgzFXUdXQlhGz6z/1WtJufAZgEACJ5XgvNfllbVr/fT+EJNXIDzZuhdLAghEreGonYNu KxtCNyIvnSPWCIzXccL7oSeF+nJPrEm7P3q+5QH2MVVu9g811uoXBaKlGLAA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v= 4l2.c index 8d5002543e2c..b9642afabd9b 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -25,6 +25,46 @@ =20 #include "uvcvideo.h" =20 +/* ------------------------------------------------------------------------ + * UVC power management + */ + +static int uvc_pm_get(struct uvc_streaming *stream) +{ + int ret =3D 0; + + ret =3D usb_autopm_get_interface(stream->dev->intf); + if (ret) + return ret; + + mutex_lock(&stream->dev->lock); + if (!stream->dev->users) + ret =3D 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 *st= ream, * developers test their webcams with the Linux driver as well as with * the Windows driver). */ + ret =3D uvc_pm_get(stream); + if (ret) + return ret; mutex_lock(&stream->mutex); if (stream->dev->quirks & UVC_QUIRK_PROBE_EXTRAFIELDS) probe->dwMaxVideoFrameSize =3D @@ -257,6 +300,7 @@ static int uvc_v4l2_try_format(struct uvc_streaming *st= ream, /* Probe the device. */ ret =3D uvc_probe_video(stream, probe); mutex_unlock(&stream->mutex); + uvc_pm_put(stream); if (ret < 0) return ret; =20 @@ -468,7 +512,13 @@ static int uvc_v4l2_set_streamparm(struct uvc_streamin= g *stream, } =20 /* Probe the device with the new settings. */ + ret =3D uvc_pm_get(stream); + if (ret) { + mutex_unlock(&stream->mutex); + return ret; + } ret =3D 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 =3D 0; =20 stream =3D video_drvdata(file); uvc_dbg(stream->dev, CALLS, "%s\n", __func__); =20 - ret =3D usb_autopm_get_interface(stream->dev->intf); - if (ret < 0) - return ret; - /* Create the device handle. */ handle =3D kzalloc(sizeof(*handle), GFP_KERNEL); - if (handle =3D=3D NULL) { - usb_autopm_put_interface(stream->dev->intf); + if (!handle) return -ENOMEM; - } =20 - mutex_lock(&stream->dev->lock); - if (stream->dev->users =3D=3D 0) { - ret =3D 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 =3D uvc_pm_get(stream); + if (ret) { kfree(handle); return ret; } } =20 - stream->dev->users++; - mutex_unlock(&stream->dev->lock); - v4l2_fh_init(&handle->vfh, &stream->vdev); v4l2_fh_add(&handle->vfh); handle->chain =3D stream->chain; @@ -610,6 +653,12 @@ static int uvc_v4l2_release(struct file *file) if (uvc_has_privileges(handle)) uvc_queue_release(&stream->queue); =20 + 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 =3D NULL; =20 - mutex_lock(&stream->dev->lock); - if (--stream->dev->users =3D=3D 0) - uvc_status_stop(stream->dev); - mutex_unlock(&stream->dev->lock); - - usb_autopm_put_interface(stream->dev->intf); return 0; } =20 @@ -849,9 +892,17 @@ static int uvc_ioctl_streamon(struct file *file, void = *fh, =20 if (handle->is_streaming) goto unlock; + + ret =3D uvc_pm_get(stream); + if (ret) + goto unlock; + ret =3D uvc_queue_streamon(&stream->queue, type); handle->is_streaming =3D !ret; =20 + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); =20 @@ -875,6 +926,9 @@ static int uvc_ioctl_streamoff(struct file *file, void = *fh, ret =3D uvc_queue_streamoff(&stream->queue, type); handle->is_streaming =3D !!ret; =20 + if (!handle->is_streaming) + uvc_pm_put(stream); + unlock: mutex_unlock(&stream->mutex); =20 @@ -928,6 +982,7 @@ static int uvc_ioctl_g_input(struct file *file, void *f= h, unsigned int *input) { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; u8 *buf; int ret; =20 @@ -941,9 +996,16 @@ static int uvc_ioctl_g_input(struct file *file, void *= fh, unsigned int *input) if (!buf) return -ENOMEM; =20 + ret =3D uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + ret =3D 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 =3D *buf - 1; =20 @@ -956,6 +1018,7 @@ static int uvc_ioctl_s_input(struct file *file, void *= fh, unsigned int input) { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; u8 *buf; int ret; =20 @@ -977,10 +1040,17 @@ static int uvc_ioctl_s_input(struct file *file, void= *fh, unsigned int input) if (!buf) return -ENOMEM; =20 + ret =3D uvc_pm_get(stream); + if (ret) { + kfree(buf); + return ret; + } + *buf =3D input + 1; ret =3D 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); =20 return ret; @@ -991,8 +1061,15 @@ static int uvc_ioctl_queryctrl(struct file *file, voi= d *fh, { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; + int ret; =20 - return uvc_query_v4l2_ctrl(chain, qc); + ret =3D uvc_pm_get(stream); + if (ret) + return ret; + ret =3D uvc_query_v4l2_ctrl(chain, qc); + uvc_pm_put(stream); + return ret; } =20 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 *fi= le, void *fh, { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; struct v4l2_queryctrl qc =3D { qec->id }; int ret; =20 + ret =3D uvc_pm_get(stream); + if (ret) + return ret; ret =3D uvc_query_v4l2_ctrl(chain, &qc); + uvc_pm_put(stream); if (ret) return ret; =20 @@ -1049,6 +1131,7 @@ static int uvc_ioctl_g_ext_ctrls(struct file *file, v= oid *fh, { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; struct v4l2_ext_control *ctrl =3D 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; } =20 + ret =3D uvc_pm_get(stream); + if (ret) + return ret; ret =3D uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } =20 for (i =3D 0; i < ctrls->count; ++ctrl, ++i) { ret =3D uvc_ctrl_get(chain, ctrl); if (ret < 0) { uvc_ctrl_rollback(handle); ctrls->error_idx =3D i; - return ret; + goto done; } } =20 ctrls->error_idx =3D 0; =20 - return uvc_ctrl_rollback(handle); + ret =3D uvc_ctrl_rollback(handle); +done: + uvc_pm_put(stream); + return ret; } =20 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 *h= andle, { struct v4l2_ext_control *ctrl =3D ctrls->controls; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; unsigned int i; int ret; =20 @@ -1104,9 +1196,15 @@ static int uvc_ioctl_s_try_ext_ctrls(struct uvc_fh *= handle, if (ret < 0) return ret; =20 + ret =3D uvc_pm_get(stream); + if (ret) + return ret; + ret =3D uvc_ctrl_begin(chain); - if (ret < 0) + if (ret < 0) { + uvc_pm_put(stream); return ret; + } =20 for (i =3D 0; i < ctrls->count; ++ctrl, ++i) { ret =3D 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 =3D ioctl =3D=3D VIDIOC_S_EXT_CTRLS ? ctrls->count : i; - return ret; + goto done; } } =20 ctrls->error_idx =3D 0; =20 if (ioctl =3D=3D VIDIOC_S_EXT_CTRLS) - return uvc_ctrl_commit(handle, ctrls); + ret =3D uvc_ctrl_commit(handle, ctrls); else - return uvc_ctrl_rollback(handle); + ret =3D uvc_ctrl_rollback(handle); + +done: + uvc_pm_put(stream); + return ret; } =20 static int uvc_ioctl_s_ext_ctrls(struct file *file, void *fh, @@ -1147,8 +1249,16 @@ static int uvc_ioctl_querymenu(struct file *file, vo= id *fh, { struct uvc_fh *handle =3D fh; struct uvc_video_chain *chain =3D handle->chain; + struct uvc_streaming *stream =3D handle->stream; + int ret; + + ret =3D uvc_pm_get(stream); + if (ret) + return ret; + ret =3D uvc_query_v4l2_menu(chain, qm); + uvc_pm_put(stream); =20 - return uvc_query_v4l2_menu(chain, qm); + return ret; } =20 static int uvc_ioctl_g_selection(struct file *file, void *fh, --=20 b4 0.11.0-dev-d93f8