From patchwork Tue Oct 25 14:34:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 619383 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 9A320C04A95 for ; Tue, 25 Oct 2022 14:35:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232322AbiJYOfT (ORCPT ); Tue, 25 Oct 2022 10:35:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232101AbiJYOfO (ORCPT ); Tue, 25 Oct 2022 10:35:14 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE90589CE9 for ; Tue, 25 Oct 2022 07:35:13 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id bj12so12906671ejb.13 for ; Tue, 25 Oct 2022 07:35:13 -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:message-id :reply-to; bh=EPUsq9ry6a/B8ysE7p/Gp0RizcV4HTBL3tocv0bMA9I=; b=fTdB2oj5zXnht0oEeX2xmQrEskfYbWhGUNlF3H6haxGBao5IRjdLyR3preVA3LQeVZ 29aYtsL68obHnIarkE0OWrORdyGhwVRhNoNqhC3YnSq3HWVaiWq9VPMoEzowIiRd0n4n LY+xBjK2zsEa8RhE2TcTNt5FSnP8AxhJMYqnQ= 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=EPUsq9ry6a/B8ysE7p/Gp0RizcV4HTBL3tocv0bMA9I=; b=lv0C20PZn08LHlAIhbfOjmtGQB9aBUnExPriR/QqXo72F/MSusD1yG/487ZeKO337+ gWNJnbTw91kESmTQq4etBDzH9fVxF/IbS9eCTnw7Xtw3izHehtFoIb4Y/UPFoYBjIdsT WDON/JnTSg0eB7osRZ5QluwhV8MUQL75sNhFtDy4tFJp6tp9VdlX87qZVM5A5KrKzEwg OsPbf4ANFNUC1+EQBNToE03SBZKmH13NM3Ts5xtR2t2A4tFuv1wBqES78HybnorBRMKX qlZkOxdrmniLHD3Eu/KrNxHMEFaU0IgF9343Xx2Pp9Ua8DNt431NQdeqOKR8EIMf+pE5 vZzQ== X-Gm-Message-State: ACrzQf32Fc8TLfXBeh6kkmFQiLVn58HCU0/SyOT1KGIfejbTXLysoL1p Nqt1im/7RuB7MUcVCuMBtQgayStYRMto3vMv X-Google-Smtp-Source: AMsMyM71hWCBlcJ9ysbiTMgKrX6H+JQgkTH7FjPE7GyhEjGcNmC0UZDMmqJQd4vHudJwmXXeBi4xPw== X-Received: by 2002:a17:907:7203:b0:7a5:b062:2338 with SMTP id dr3-20020a170907720300b007a5b0622338mr11741623ejc.8.1666708512271; Tue, 25 Oct 2022 07:35:12 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:11 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:22 +0200 Subject: [PATCH v2 1/8] media: uvcvideo: Only create input devs if hw supports it MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-1-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1336; i=ribalda@chromium.org; h=from:subject:message-id; bh=3NadNTBF4BvX9SA2sRowXe1SUqwds9fIYTSwvXBWhlY=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QPx9RjCOkK3+R3LAQ5GMY4FjigtbgykVx6lPrt swaMdAeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0DwAKCRDRN9E+zzrEiJyPD/ 9f5qy1Bc9FRGR0NTl0gc9YOOsfmXgD5GOrksxDkgvooTyJLJwQbGlAy4UbzlXv7hGoE13ZpwVt7knm zSeQzm+to8p0nlRIBvQEyj5LGn6DXxBseUS9XoIUl9Ii/gMt9dnraRVsvjitYSNd2PHgNWmQc3gFV0 5dRMO5JZJ6DOiCdqYzoH3PiiIpZzuAwa+wQIc5uRPAV8Z9RAknxVkqD/bbpmNE7447clD6iGFkpnzT G66Q0s7Si1wtvus0CE7aGgS/WMkwQo7roJT2jQ6FdWc/41eFyYNx8byRLTcP9nD8IuC/mWIZgBLazZ pI2KG7XiJhP/6douXCtcuYKtSxfsHw11K4DtOQyRjw/PsgSLolAovRMA3/0/GuASTMD81MVJPeqtxG O2Cpde/SXeiEYYk0ySF1awU8tivB7lUl/EzQ2lCbtvQr5h7/rEgEfKeDpVJgvKMsIO+WC8dd238WAF QiLYsPHvZgrIBoIV8Gh3BOLN6zRA/W+wNXdlryR4NMcOZpQmoz0xVCoxsgXimdw1imHKop5Up0DidC 1uGuDn+jb9GsrIDaDmykie6PeZn311GDOaY/hb0SGu/ozfm2eTuGPF3bMqDeKGlqmThaKwZpQ+oOT4 gtlSk08fAtJqgGxkvqDdee5WzXnF5pExtS7xlW+xSzrH3tZt0ChSY4lXw3DA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Examine the stream headers to figure out if the device has a button and can be used as an input. Reviewed-by: Laurent Pinchart Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 7518ffce22ed..cb90aff344bc 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -18,11 +18,34 @@ * Input device */ #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV + +static bool uvc_input_has_button(struct uvc_device *dev) +{ + struct uvc_streaming *stream; + + /* + * The device has button events if both bTriggerSupport and + * bTriggerUsage are one. Otherwise the camera button does not + * exist or is handled automatically by the camera without host + * driver or client application intervention. + */ + list_for_each_entry(stream, &dev->streams, list) { + if (stream->header.bTriggerSupport == 1 && + stream->header.bTriggerUsage == 1) + return true; + } + + return false; +} + static int uvc_input_init(struct uvc_device *dev) { struct input_dev *input; int ret; + if (!uvc_input_has_button(dev)) + return 0; + input = input_allocate_device(); if (input == NULL) return -ENOMEM; From patchwork Tue Oct 25 14:34:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 618554 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 BFC48C04A95 for ; Tue, 25 Oct 2022 14:35:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232265AbiJYOfU (ORCPT ); Tue, 25 Oct 2022 10:35:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232201AbiJYOfP (ORCPT ); Tue, 25 Oct 2022 10:35:15 -0400 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 E3B0A8D227 for ; Tue, 25 Oct 2022 07:35:14 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id q9so13176602ejd.0 for ; Tue, 25 Oct 2022 07:35:14 -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:message-id :reply-to; bh=fw978dyctQKYz24O/MzUIY5FG9WdD7MbzKHKlFXG3Qw=; b=bmLkeE92rI1LV5eRKtnqFtVtKlsxbsg9ecVDft0DT/GIwpvbLDl01gJxig5T0jNIyd +4diSJLScqdvKuCMrCzoJ6SVfJwYuoKr3pk2VSYImmhZg3sPEgPNTlRUslJw0hjj+iVZ Bn/Frf/KpoKM7o1+hOrk1+ZKYfI3YnRw5QZ6A= 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=fw978dyctQKYz24O/MzUIY5FG9WdD7MbzKHKlFXG3Qw=; b=dMiRxFP7Lbi4IWIEXT4sewO8wbF+b7bQsAzofDsyaFtDtJIZniz2PF7vsJw1ImwRFc QvuBf1tTYvQDlzhT64ZwGLQ/i5S0/PGcZd87awDXAmvOeAxvv06NgIpWKujj3bizyY43 1S41A2/Hpx2YjJ4nWh1iXjS4j0hPOB62TWXCqvzErV9ezmJZkQsxKMqRBTm6My3gXWQg iLp/GN8xg+/fGmJyQD2NspWYBQmzp5TSNPgGZURcEAtMUefC/GTUzzmA6gP8iPaMWocC i9q9oLqfNDtSXzHA5Sh+bTS/JV2bnLpx6wzi3d6zaDgmBLHozbY6Gty13iXqf9pMNgX2 6Lmw== X-Gm-Message-State: ACrzQf1OSFompQAya+/bzmgd3m8JO70QF6A2Zno7giB2Gy8oPjitjzuE xiHbJqt05v3c+1FXsUR37s6aPw== X-Google-Smtp-Source: AMsMyM77Li5Xvdjs5rOn2zK1EnbtQexTxS7e0kamFnWqQjkI4wBzvSv9N/PFgWWQnxoEHdC0ebsEHQ== X-Received: by 2002:a17:907:c05:b0:73d:6e0a:8d22 with SMTP id ga5-20020a1709070c0500b0073d6e0a8d22mr32994041ejc.646.1666708513448; Tue, 25 Oct 2022 07:35:13 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:12 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:23 +0200 Subject: [PATCH v2 2/8] media: uvcvideo: Refactor streamon/streamoff MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-2-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1977; i=ribalda@chromium.org; h=from:subject:message-id; bh=NB6aEyZdAyQsktG3au/4ny2fc8G0li6jsr2nryh8gpk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QR6p/N/GSdWGniClNtVwAPcJBAGTXLWJoRKpUt 0WA3K4yJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0EQAKCRDRN9E+zzrEiOeREA CB2VW7wYO9JmilaSY15eLOYmFRdBeDHrKtiMPi7vGit/vrZQEdlX1GkhsmvhtFQOYf5X1bOWzEDFHz 39J8jZqoLIVNkQF7KiyserEfeAtCFaqOYTvNCmGuCmp1tUUTqgdPkTRDlwUOM26FfSuYhaVWcGLdGW QSEZp993O0oDkpfsmCXenx6Vitop2NCT5NOKguJqTb9nCzbp5aSQcWkKvRZmlQvCjsq/gtrViy+161 /gY254FN95KzohXog9BodoFz7nmTGWnscVRdbDsBXA7+WwwkKwMQVec35X+JFHPOywWv9SYOv6ZnXE DUBu1WYi5mqLgpQ05jBw3JPliocxdZ23k4pK05ekZuHOeRlvwlrF5PIfXPmV+5AX/5V3Z+ZD5wthHu fsLk7e+9T7E6pDPpu2DemRO9LS4INVocaafzIeqPYTXors63sWqCr+4Bbkb2CE9qU1ASM+he5ObXfg rAyBA3gQpaU+Ry6/WafZRN6s3wI/xp1XBpW69C/4moCMe9hIUlo5bSFz++ISYR92oDoRscFSH5UKYi QF/Az9KgcfKi0SoR19lfI8dEuYTp+1SfyqYfcx5zZtQsU9DXcaJ+N7IKpwyv+25MIJpsTdk9pdGyJt gWsBDMvV9C7H/vtIemgsOBpTVkq8eI7JnG++g0M7TXp7869qJxxLniTlKzOw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add a new variable to handle the streaming state and handle the streamoff errors, that were not handled before. Suggested-by: Laurent Pinchart Signed-off-by: Ricardo Ribalda 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 Tue Oct 25 14:34:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 619382 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 743C0C38A2D for ; Tue, 25 Oct 2022 14:35:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232429AbiJYOfW (ORCPT ); Tue, 25 Oct 2022 10:35:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232302AbiJYOfS (ORCPT ); Tue, 25 Oct 2022 10:35:18 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE701923C9 for ; Tue, 25 Oct 2022 07:35:15 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id 13so13048105ejn.3 for ; Tue, 25 Oct 2022 07:35:15 -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:message-id :reply-to; bh=Y3aLAqx1UmJhgGtzBHcHb9QyFZFPiOEXRjvNmq6X8fw=; b=ObHKsaoM0udFhG5HUXUbGulvSvahWlGNY0ellhchoPQK1v4KiCVA6eVPiDLNZ2nPuK CG3b2IS4fmJB7S2ocHGzF0927jbnWMlc9qq66Ge78hdoFZUujctVT3FtpjUO0vWgg/sU yX5mNJ5oRM0b4FyZRoN3jwuFpUoehez/Tb0lA= 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=Y3aLAqx1UmJhgGtzBHcHb9QyFZFPiOEXRjvNmq6X8fw=; b=We76uFu/3oRpeu3U7lb0YalkKmQ3bRDZIGRzHS3AIu0zI8CWPsO1M7x2UPdwvWBo3L xLsbHJqWe27m4omGgaiHBBElKz2iRAYCd6UuXUP0TJpyPJXj+PZFgNR+j1G7bV+wR9B8 CtRfHu+zRr4my6fSYxKRSIJx0lKm+RY0deGkFNfTY8TR3HA1S7c+VVz5wf5s6BPjOyU7 dIqNvsK7FHEn9Dv3zrE7UM0Tw/4lB3IOE4awUpSmuUUJfLz/nVXZ/qfh4OyFMfMm+4J2 bClq8IOJRwDQj+PPK0GeUmuFQEV6GzoJizcXMeoZKjOqeYOnhh7UDa08umqaG0I0zCvf aCzQ== X-Gm-Message-State: ACrzQf3xncyDMLcTSinITuJKwfI/7/Q5B2w9MeVgLrbPk68ir1ROBybW 0nKoDGdpz+NiffijT4G0lTT21g== X-Google-Smtp-Source: AMsMyM4kSd36FJ4mphBdmq9PXd97TupX7MCfqePX2ImuA56/2zS+3gkA3f286ZILClPOSfJ1teUDow== X-Received: by 2002:a17:907:1c01:b0:78d:eb6e:3807 with SMTP id nc1-20020a1709071c0100b0078deb6e3807mr32969272ejc.481.1666708514425; Tue, 25 Oct 2022 07:35:14 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:14 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:24 +0200 Subject: [PATCH v2 3/8] media: uvcvideo: Do power management granularly MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-3-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=10240; i=ribalda@chromium.org; h=from:subject:message-id; bh=9JC+xKdhqfQ07Aja2WDiXvrtLcLtkT6rTXo67NjWgLI=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QShKIPLM8A/G8CTvA7j7cOb5vcI1AhvVorXxW2 E4Iq54WJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0EgAKCRDRN9E+zzrEiFitD/ 9FAFJTqD17eH9nS+vWxZBvFp2xBILkvdAgHZdZL58nx5mIH58WWX9BmSjJ+qfeKe8kWmcpIpJVytJ6 dywz46j+exs2yhL7Q+juCQVrSfjbNruuKxUZFxXOaIg6uPJYmr6+5AvwFerodDfH4Iia3nxGpK07Vn 0FWg1P2lLbPMwhPsQtOt2FlB/UDMtX4w4ddrOTabsNDI2uR1NHkJ5I/mLpR1UcQ24oKeRF7At9ONKy wpxxZInWakaf3fOkxIA7/gOJ/Cvmop8HBBcVAyQ2ZUQy1f0XAo/J79WpHaeJCyfSmjYXrr3mvavsfZ 1050FMtCW/uMjgwfc+9lOofYJhUy0rjnFXNZEl+N9GQZzuGoE7wb6zbh6RfshtuyM8oFsNqOiDYHaD XqGVKvLnvDfnAjznrc5UoOQwDiYwoeIfrRKD6KrVqpistTtzlIg0Dikw55Alhuv5JwMoC4cn0zink0 TMi3P92aWdJJrVrnNzvi56PbF7YzAqVMRFHIwZ/z/G9gUVsIlOo1xQqkTUHPSHFDwtc/yslgoJcU3g rOZbw0nUUebpY9q5PAZzpmd9HBJdUTUp7FqXgPkmMNSqo26PouO4QCHvY29GKH/Y45mVpOEb2AGP6e 4Ds5KkuX/LsB8JHti8mW3BZquETsAqJfLJsWRlIsCLeqZvDcCMwVQBJb9dUw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@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_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, From patchwork Tue Oct 25 14:34:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 618553 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 C5D73C04A95 for ; Tue, 25 Oct 2022 14:35:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229543AbiJYOfY (ORCPT ); Tue, 25 Oct 2022 10:35:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232376AbiJYOfT (ORCPT ); Tue, 25 Oct 2022 10:35:19 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B0B692CF0 for ; Tue, 25 Oct 2022 07:35:17 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id sc25so12944227ejc.12 for ; Tue, 25 Oct 2022 07:35:17 -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:message-id :reply-to; bh=KvGsjskLZRNKygMGJ72lSJUoo6AJ3rOQ6/7RzoglZg8=; b=I4lq41owIEKs6FfUYkPvTw8hJ5RUkfEADXJCABfFBjTkDUumGSSbh87hiz1PZenvuG o5KrsJCRigJshepcumssIl4Hgqt8TQ6wpenI89tYxdFsJn77X3HQiTGT3FdReqHmoPRO j0tn18aDbDI1Z45O1D6Lgxa2qOl250o9EocGY= 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=KvGsjskLZRNKygMGJ72lSJUoo6AJ3rOQ6/7RzoglZg8=; b=kQ1w33ootYcqzVWGVhWsmjIZ8rWKCiWuDyRr6TMuvgiMMJZmTPigD1JSmS1k9b+Ovg fKzr0nTij3yhFNqIDkHlwLbbG9fniZeK7yWp2GaKX3vFFE4QscQ6HFFDHLvBLDyGHIgx rX26u/MfkyJb4Uqsw3Xre3OHQjfd7se+OOWhlFhk6/FEYBB2TmcwjLsvQnA4uVIMkuNc pEq02YKbiJZ+zo+Py+IyZlE1FqA82Q6RzpMHcYBz7yBzjnNU6E4ymtL1fLA+mn8tiBko DI9zl/Jcr/nEFqdGVT+V5GZxkbVyd13pSZQAV8lhl9a2V0V3yHHfpWVN12+VadmOu8jG XSaw== X-Gm-Message-State: ACrzQf1BdwPhKNm7XSJw6deQhKsqwF2o8bqV4myNBxzKn0t5yuLdNWQo D+Wo3ToDAPyoZCuwoC/Q4RJWaQ== X-Google-Smtp-Source: AMsMyM79SNVJINBvLnNZgH+FrZFXsGIcYR0CtMZbeaLNq2btEAvEEluL3w7s1VQ2Yt9ldaiuHfVL1Q== X-Received: by 2002:a17:907:b01:b0:78d:ce3d:905d with SMTP id h1-20020a1709070b0100b0078dce3d905dmr32380992ejl.45.1666708515627; Tue, 25 Oct 2022 07:35:15 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:15 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:25 +0200 Subject: [PATCH v2 4/8] media: uvcvideo: Cancel async worker earlier MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-4-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=3850; i=ribalda@chromium.org; h=from:subject:message-id; bh=0l2KdHP21BwHh7O1kFOKoF3d4tIlj1BptCuhhZq8++g=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QVaLHI+mQrPtakacw9hVvyePqZ+MWgIM8uJidM E8/BakCJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0FQAKCRDRN9E+zzrEiECID/ 0RWgzXkrL4a09vGFcPSax2yoszoL7vBaUlZQXK7vmTI6PDTe6GnCloeadCiIS31fThH+juyEWznqvL nwwKjB8lopBC6EjDOkoBPumWZ10CBPQtgOjffooREj6kR6IlflTeJ62XafEdHvyXwfsQlu0UgdHO12 CF8Kb5RwXOpibO/wXCCTiTI9ZoFs4xPsY66Vp6SOUeMiFO3AjiCtwTgFD8NLgebhHdz+2kLsuHvHTx 1LcPxb96dXOHmLH6r/8JlzX8I16gQzej0uP5Kom+JJhUyluB56pWM/JAxrwM3Y1vDnNVP58p4d/Ttz jd7E2p/INm6kkFD8dFWozf71qTDwkq9P9Mmmi+k1x2U5B4fExdBtz7MCneVodXl2jJpIZOK3pjkOkC 2KQU2+KVQpd4vDx9VWQvsPSQIxgLKWmPfodRV9kbF8usACFYhfwFzUEV0j97LIjZWqVx/Aq95da7+O WVOknGfIBTJINPNgfMqx+pCaphuShOKdIK+CpyBINqnsCNbfl7Gc4ub1TUObOcvldp1cFMs2O1hj1l sHCmG0Ocv9tRAuDyEElUURLMgygz5wFfAzeIlOVamZuGVu7GINggogX4di/9fjKRN35ZgCmAdmjfhX 9ghrjilTPhpjnjgpStSxyUeEePvqZ2CsZFclqW9OGHsuKyTfF422+4+4PiMw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Guenter Roeck So far the asynchronous control worker was canceled only in uvc_ctrl_cleanup_device. This is much later than the call to uvc_disconnect. However, after the call to uvc_disconnect returns, there must be no more USB activity. This can result in all kinds of problems in the USB code. One observed example: URB ffff993e83d0bc00 submitted while active WARNING: CPU: 0 PID: 4046 at drivers/usb/core/urb.c:364 usb_submit_urb+0x4ba/0x55e Modules linked in: <...> CPU: 0 PID: 4046 Comm: kworker/0:35 Not tainted 4.19.139 #18 Hardware name: Google Phaser/Phaser, BIOS Google_Phaser.10952.0.0 08/09/2018 Workqueue: events uvc_ctrl_status_event_work [uvcvideo] RIP: 0010:usb_submit_urb+0x4ba/0x55e Code: <...> RSP: 0018:ffffb08d471ebde8 EFLAGS: 00010246 RAX: a6da85d923ea5d00 RBX: ffff993e71985928 RCX: 0000000000000000 RDX: ffff993f37a1de90 RSI: ffff993f37a153d0 RDI: ffff993f37a153d0 RBP: ffffb08d471ebe28 R08: 000000000000003b R09: 001424bf85822e96 R10: 0000001000000000 R11: ffffffff975a4398 R12: ffff993e83d0b000 R13: ffff993e83d0bc00 R14: 0000000000000000 R15: 00000000fffffff0 FS: 0000000000000000(0000) GS:ffff993f37a00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00000000ec9c0000 CR3: 000000025b160000 CR4: 0000000000340ef0 Call Trace: uvc_ctrl_status_event_work+0xd6/0x107 [uvcvideo] process_one_work+0x19b/0x4c5 worker_thread+0x10d/0x286 kthread+0x138/0x140 ? process_one_work+0x4c5/0x4c5 ? kthread_associate_blkcg+0xc1/0xc1 ret_from_fork+0x1f/0x40 Introduce new function uvc_ctrl_stop_device() to cancel the worker and call it from uvc_unregister_video() to solve the problem. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c index c95a2229f4fa..c7af3e08b2c6 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -2597,14 +2597,17 @@ static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev, } } -void uvc_ctrl_cleanup_device(struct uvc_device *dev) +void uvc_ctrl_stop_device(struct uvc_device *dev) { - struct uvc_entity *entity; - unsigned int i; - /* Can be uninitialized if we are aborting on probe error. */ if (dev->async_ctrl.work.func) cancel_work_sync(&dev->async_ctrl.work); +} + +void uvc_ctrl_cleanup_device(struct uvc_device *dev) +{ + struct uvc_entity *entity; + unsigned int i; /* Free controls and control mappings for all entities. */ list_for_each_entry(entity, &dev->entities, list) { diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 215fb483efb0..fb58f80f0b37 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1909,6 +1909,7 @@ static void uvc_unregister_video(struct uvc_device *dev) } uvc_status_unregister(dev); + uvc_ctrl_stop_device(dev); if (dev->vdev.dev) v4l2_device_unregister(&dev->vdev); diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 45310f55475f..6b07abac7034 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -739,6 +739,7 @@ int uvc_query_v4l2_menu(struct uvc_video_chain *chain, int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, const struct uvc_control_mapping *mapping); int uvc_ctrl_init_device(struct uvc_device *dev); +void uvc_ctrl_stop_device(struct uvc_device *dev); void uvc_ctrl_cleanup_device(struct uvc_device *dev); int uvc_ctrl_restore_values(struct uvc_device *dev); bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain, From patchwork Tue Oct 25 14:34:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 618552 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 AF96EC04A95 for ; Tue, 25 Oct 2022 14:35:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232417AbiJYOfe (ORCPT ); Tue, 25 Oct 2022 10:35:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232382AbiJYOfV (ORCPT ); Tue, 25 Oct 2022 10:35:21 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56AE58E457 for ; Tue, 25 Oct 2022 07:35:18 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id f27so2441008eje.1 for ; Tue, 25 Oct 2022 07:35:18 -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:message-id :reply-to; bh=C1brNmOeIqvLHZriUymMjmWBOUrEuqumqBXcSDvQDmk=; b=kk8ztiwJk/VrU1CnULvrrSl1rCZFGsUTjAEEuzfqylHAa5owbW3dVON4IAjb8Pdm31 yEreP1X4dIxQmO6rzP5130rr1eLwznp2BBG6IXeq9nkwqsn4QpX8ACnyFcm7MeIoBXFX DbQhtCVHwggkGWFOkZKvqCLgbVPUifol2GQL0= 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=C1brNmOeIqvLHZriUymMjmWBOUrEuqumqBXcSDvQDmk=; b=wUS0R8WD09AJOXOvKTsDVjJKyJt+CGxmHpS9sbUfPrCYEA01zly4ybaWY+6xoILK0C IHxxdjWCjQrobfM+RjdBT8jgqTs5vJLQ6HbQ5JPJW9Br6dJOeTbrxJWABnQ4z1D2bGQv Mou75DFjYbfHva7wWsA3DBBeQDPnR9k8dRW98lwODwfj3PGNCs+umc7JxfT81cD+AIYq sI0ZzDsJ4Aygf6BGfO+rgDDpuVroY7+/X/5ThCCKt3TilRMbwCoD3EY+zYXDbNUsiqpb bz5kxsN1PwA2FZBM0AMyGMkZLNiemTTo0zFxK1qy7VEXP9lroDuxUnjSw+Fzr8M6fS8A YnwQ== X-Gm-Message-State: ACrzQf2OIwNxFQOw9Sa6K02RcESnGl0Rj5Dbf+68aDuSDUuQMlsVWDmN xHRkhaZ4128KU1cQtcW2XiWxaA== X-Google-Smtp-Source: AMsMyM6PFzCguJkAXuZKdCeaGzZkf14xA4WDwbOhOMT6QyqJlPAfkksQyRLrhjzPmYvYrX/6zh8+mQ== X-Received: by 2002:a17:906:7945:b0:73b:e605:f31 with SMTP id l5-20020a170906794500b0073be6050f31mr32990283ejo.129.1666708516872; Tue, 25 Oct 2022 07:35:16 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:16 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:26 +0200 Subject: [PATCH v2 5/8] media: uvcvideo: Lock video streams and queues while unregistering MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-5-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=4775; i=ribalda@chromium.org; h=from:subject:message-id; bh=JccDHJlMGn0ERZrldEIMeE1yCBRex4FL0yGZe55zmQk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QX1OMDpV++F5pP93hJJb0oIw/vYJZddRC/Z+bx i1JmOr6JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0FwAKCRDRN9E+zzrEiBcVD/ 9tJaK168vSuc8YLN6Gdlp+81+REVjfDUgo3Y9xFQ1FWA39QM8iXTLnrpDs/QvjoWWSAVBu26P1hAZu gHtvLg5PbLy9C9V85/Zltg82NdtKHLPwMqZnun4DpizBarJe3AL3oi85mUaR0fmwoGau2WRpU26JYE yZFcLKLbeMfkoq4lmrJxuWry08d29JrMbPjCljOWZ0nAWgbz1Fi1xQ8i4+eIxAnmqQr6UXx9yvJuLI DpttCA9Sol9gHubHcqiMoqAlIz2M6ednqMzD3dbUITTViQ3W7LtoY+RQLwKEcShdmwUn7EFbK4hbn+ QQewXQiel09f+6llZC4XOkbvjgHQEpfH7DPIIq95He3QDK33tERAYvqib+wGvfEkPdelY9hLqEwcyJ 1kx7hgRK2NhnxfszSGvs4NgMyQT7ekk1bnwJ5axFvLpQsysyFGLK+/fDh8PEC4L4SuLxoOccC7I+rj oXVAC4zfEsjee/T/ljNX3YAjzZmoNxUfjL0+06my4aZGSXyE2r2H0Dn3DuOIpwcxBVIdzaCfv0WB/B fUrdm0/FbmjPYbthwLqljD/5DPnxNgrlV0FyDmT2F/XgYHD0gS875Kxd/AsvHqahSnvYnohLkv1Eh4 v8o4WsVZj3dD6O9Baa6RqBj6iHuOiBnVeSJXEYXDFNHRWzW/0yeafyyzkyUw== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Guenter Roeck The call to uvc_disconnect() is not protected by any mutex. This means it can and will be called while other accesses to the video device are in progress. This can cause all kinds of race conditions, including crashes such as the following. usb 1-4: USB disconnect, device number 3 BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP PTI CPU: 0 PID: 5633 Comm: V4L2CaptureThre Not tainted 4.19.113-08536-g5d29ca36db06 #1 Hardware name: GOOGLE Edgar, BIOS Google_Edgar.7287.167.156 03/25/2019 RIP: 0010:usb_ifnum_to_if+0x29/0x40 Code: <...> RSP: 0018:ffffa46f42a47a80 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 0000000000000000 RCX: ffff904a396c9000 RDX: ffff904a39641320 RSI: 0000000000000001 RDI: 0000000000000000 RBP: ffffa46f42a47a80 R08: 0000000000000002 R09: 0000000000000000 R10: 0000000000009975 R11: 0000000000000009 R12: 0000000000000000 R13: ffff904a396b3800 R14: ffff904a39e88000 R15: 0000000000000000 FS: 00007f396448e700(0000) GS:ffff904a3ba00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000000 CR3: 000000016cb46000 CR4: 00000000001006f0 Call Trace: usb_hcd_alloc_bandwidth+0x1ee/0x30f usb_set_interface+0x1a3/0x2b7 uvc_video_start_transfer+0x29b/0x4b8 [uvcvideo] uvc_video_start_streaming+0x91/0xdd [uvcvideo] uvc_start_streaming+0x28/0x5d [uvcvideo] vb2_start_streaming+0x61/0x143 [videobuf2_common] vb2_core_streamon+0xf7/0x10f [videobuf2_common] uvc_queue_streamon+0x2e/0x41 [uvcvideo] uvc_ioctl_streamon+0x42/0x5c [uvcvideo] __video_do_ioctl+0x33d/0x42a video_usercopy+0x34e/0x5ff ? video_ioctl2+0x16/0x16 v4l2_ioctl+0x46/0x53 do_vfs_ioctl+0x50a/0x76f ksys_ioctl+0x58/0x83 __x64_sys_ioctl+0x1a/0x1e do_syscall_64+0x54/0xde usb_set_interface() should not be called after the USB device has been unregistered. However, in the above case the disconnect happened after v4l2_ioctl() was called, but before the call to usb_ifnum_to_if(). Acquire various mutexes in uvc_unregister_video() to fix the majority (maybe all) of the observed race conditions. The uvc_device lock prevents races against suspend and resume calls and the poll function. The uvc_streaming lock prevents races against stream related functions; for the most part, those are ioctls. This lock also requires other functions using this lock to check if a video device is still registered after acquiring it. For example, it was observed that the video device was already unregistered by the time the stream lock was acquired in uvc_ioctl_streamon(). The uvc_queue lock prevents races against queue functions, Most of those are already protected by the uvc_streaming lock, but some are called directly. This is done as added protection; an actual race was not (yet) observed. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index fb58f80f0b37..14b66019208d 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1898,14 +1898,22 @@ static void uvc_unregister_video(struct uvc_device *dev) { struct uvc_streaming *stream; + mutex_lock(&dev->lock); + list_for_each_entry(stream, &dev->streams, list) { if (!video_is_registered(&stream->vdev)) continue; + mutex_lock(&stream->mutex); + mutex_lock(&stream->queue.mutex); + video_unregister_device(&stream->vdev); video_unregister_device(&stream->meta.vdev); uvc_debugfs_cleanup_stream(stream); + + mutex_unlock(&stream->queue.mutex); + mutex_unlock(&stream->mutex); } uvc_status_unregister(dev); @@ -1917,6 +1925,8 @@ static void uvc_unregister_video(struct uvc_device *dev) if (media_devnode_is_registered(dev->mdev.devnode)) media_device_unregister(&dev->mdev); #endif + + mutex_unlock(&dev->lock); } int uvc_register_video_device(struct uvc_device *dev, diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f5b0f1905962..c250b628fc4f 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -38,10 +38,18 @@ static int uvc_pm_get(struct uvc_streaming *stream) return ret; mutex_lock(&stream->dev->lock); + + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto done; + } + if (!stream->dev->users) ret = uvc_status_start(stream->dev, GFP_KERNEL); if (!ret) stream->dev->users++; + +done: mutex_unlock(&stream->dev->lock); if (ret) From patchwork Tue Oct 25 14:34:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 619381 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 B4D08C38A2D for ; Tue, 25 Oct 2022 14:35:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232467AbiJYOf0 (ORCPT ); Tue, 25 Oct 2022 10:35:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232330AbiJYOfV (ORCPT ); Tue, 25 Oct 2022 10:35:21 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 533A58D20F for ; Tue, 25 Oct 2022 07:35:18 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id 13so13048597ejn.3 for ; Tue, 25 Oct 2022 07:35:18 -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:message-id :reply-to; bh=CBQi3G3Ym31IbBZKsntX/6j7yLQdhJIuZJ9n/O1WbSA=; b=IjtjnaitqL4YWVUyckfhshIgXOqDSQJwVkL3vu2mQ4xLMHxL/dvldbBzC935krRQ11 ThoQfe/9ZmVJR9xGdVe49xD+ISZZz6kRFkaVl8VD77hG5C25AaD7EiilpJzTriHn38TK h8Dpip8/8XY32l896EkV72KqhvKco/+1rFN8g= 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=CBQi3G3Ym31IbBZKsntX/6j7yLQdhJIuZJ9n/O1WbSA=; b=Pu2r6Qd2PDINIh7rHNIgvt/HQlM9VfFoDkWAN0PpBNiA5HExENuTQBl16wKRdFC/Hj UawAeUw6PebEnGWfk5e6eP1yI0gzTdKmphs0Rx93x7xmNt1w07wlqhivZH8rjeIetm1O RkIFNPMR8golxoFWhogFxNmjBCJSAb7O7WRjeovov0nrU6ZHQFDML2r8nCyEGP/fWDbN FjRaZZvJnTKKN4KpzD6lT17ZUCgxsabDNExm7+RE01HN0zWUmYcT1m4klKUd/VlR/TlS htc9jmvsibHkw3Uy1m4M8ikFyoECmDMjResaW2CAXmw4yD9mlo6PjtaWyI5xoP3RE991 v44g== X-Gm-Message-State: ACrzQf03FWb+UqsU82mHHwPx3L+zfsiGSTzAxnOzFZ3ZCKhk8VmcWyu7 DtleDZbuETPPXV9AG5iV+yEehQ== X-Google-Smtp-Source: AMsMyM56iCvlaSjd6gTckL2lznGsHz+xLxt6FQFcShC9bAxTNItckDBO95rvBajAVRfKDgZqHqnUeA== X-Received: by 2002:a17:907:2672:b0:780:8bb5:25a3 with SMTP id ci18-20020a170907267200b007808bb525a3mr32505579ejc.281.1666708517878; Tue, 25 Oct 2022 07:35:17 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:17 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:27 +0200 Subject: [PATCH v2 6/8] media: uvcvideo: Release stream queue when unregistering video device MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-6-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=3485; i=ribalda@chromium.org; h=from:subject:message-id; bh=lnR79aPbgxGWW1+CW9B6Zk1T+xgYfHfisqEZO2U/Www=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QYDKMQuekATrtARrL80BycwWYzTmtrt+zsj9AD Uhw4JSiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0GAAKCRDRN9E+zzrEiI6QD/ 0YKUZQY1+1WWOIAxk70HDqqWeaHrf/XJIMpizWgNm50/uF6FBK/Fj8hYdn+TwVNqn9lL7mYKzRxPag wlWYgRgL/E2K9QuwwdkFTsC2OLArFbm3KVaH1mCIotDg9O/TLJh2f3o8rHEXFg8GWVhkkMHMoeaQOK +UnpMs8fVCfDv9XOmOqP8T+tsHV7H21K5D5+6dA6ExzDQqFcU2X7fWkBwGgCA98YIOxTrlN4Ud5XWK nCvGc2669t2c+J1u8ZON1MCRrPb3ihRJ3qWIFLCppFhcCV8eseRKkzAUIBIjUB1AtckDWG0mMPW4RH Xd7iLVpcy95Leh1PlxfCDwntWwk4BX44btHUDu+MBaIxQQtQhnesSrLD9IJfQDat8gYfj1mYUaCL3F u2tMmFZtLHfr0XwXa15Ir4oyyPwDDPGeXpIdAAa9PddzaHuio1DgSM/pLugytcvKIKwFcwvDYCHPvm 2lmLqG1fr3duw+kxwbnjsiqMm7B1+0OqgY8CD2NL2k2MonWEsOQFjBYJxPzhQa9IPb/ldhUqERMcwK YYO3w/vvBv3m5NaIBD/6MGPu7BXM/7qqu2ETJ5L2yWawxlxbQ7VdeQUwteDiVf7bByiS7xijIb+TKh jdweYJRY/fQklPWZZBwnEm5a367xU50ftQ6S/BYPFmav6XIg1p6aBMDxJx7Q== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Guenter Roeck The following traceback was observed when stress testing the uvcvideo driver. config->interface[0] is NULL WARNING: CPU: 0 PID: 2757 at drivers/usb/core/usb.c:285 usb_ifnum_to_if+0x81/0x85 ^^^ added log message and WARN() to prevent crash Modules linked in: <...> CPU: 0 PID: 2757 Comm: inotify_reader Not tainted 4.19.139 #20 Hardware name: Google Phaser/Phaser, BIOS Google_Phaser.10952.0.0 08/09/2018 RIP: 0010:usb_ifnum_to_if+0x81/0x85 Code: <...> RSP: 0018:ffff9ee20141fa58 EFLAGS: 00010246 RAX: 438a0e5a525f1800 RBX: 0000000000000000 RCX: 0000000000000000 RDX: ffff975477a1de90 RSI: ffff975477a153d0 RDI: ffff975477a153d0 RBP: ffff9ee20141fa70 R08: 000000000000002c R09: 002daec189138d78 R10: 0000001000000000 R11: ffffffffa7da42e6 R12: ffff975459594800 R13: 0000000000000001 R14: 0000000000000001 R15: ffff975465376400 FS: 00007ddebffd6700(0000) GS:ffff975477a00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000012c83000 CR3: 00000001bbaf8000 CR4: 0000000000340ef0 Call Trace: usb_set_interface+0x3b/0x2f3 uvc_video_stop_streaming+0x38/0x81 [uvcvideo] uvc_stop_streaming+0x21/0x49 [uvcvideo] __vb2_queue_cancel+0x33/0x249 [videobuf2_common] vb2_core_queue_release+0x1c/0x45 [videobuf2_common] uvc_queue_release+0x26/0x32 [uvcvideo] uvc_v4l2_release+0x41/0xdd [uvcvideo] v4l2_release+0x99/0xed __fput+0xf0/0x1ea task_work_run+0x7f/0xa7 do_exit+0x1d1/0x6eb do_group_exit+0x9d/0xac get_signal+0x135/0x482 do_signal+0x4a/0x63c exit_to_usermode_loop+0x86/0xa5 do_syscall_64+0x171/0x269 ? __do_page_fault+0x272/0x474 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7ddec156dc26 Code: Bad RIP value. RSP: 002b:00007ddebffd5c10 EFLAGS: 00000293 ORIG_RAX: 0000000000000017 RAX: fffffffffffffdfe RBX: 000000000000000a RCX: 00007ddec156dc26 RDX: 0000000000000000 RSI: 00007ddebffd5d28 RDI: 000000000000000a RBP: 00007ddebffd5c50 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000293 R12: 00007ddebffd5d28 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 When this was observed, a USB disconnect was in progress, uvc_disconnect() had returned, but usb_disable_device() was still running. Drivers are not supposed to call any USB functions after the driver disconnect function has been called. The uvcvideo driver does not follow that convention and tries to write to the disconnected USB interface anyway. This results in a variety of race conditions. To solve this specific problem, release the uvc queue from uvc_unregister_video() after the associated video devices have been unregistered. Since the function already holds the uvc queue mutex, bypass uvc_queue_release() and call vb2_queue_release() directly. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 14b66019208d..f15fbdbcd8bb 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1912,6 +1912,8 @@ static void uvc_unregister_video(struct uvc_device *dev) uvc_debugfs_cleanup_stream(stream); + vb2_queue_release(&stream->queue.queue); + mutex_unlock(&stream->queue.mutex); mutex_unlock(&stream->mutex); } From patchwork Tue Oct 25 14:34:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 619380 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 94872C04A95 for ; Tue, 25 Oct 2022 14:36:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbiJYOff (ORCPT ); Tue, 25 Oct 2022 10:35:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232442AbiJYOfX (ORCPT ); Tue, 25 Oct 2022 10:35:23 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2926497EE0 for ; Tue, 25 Oct 2022 07:35:20 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id y69so17527239ede.5 for ; Tue, 25 Oct 2022 07:35:20 -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:message-id :reply-to; bh=mMLlFka1I97WBpNYC6a4inYcni8WZqVzi63jLlkZWi4=; b=Qfx1Bj1JK4e65/6R6MgGPuZBNlmRrurek97IQhmwS7ceqdGvFOmGBXWflS5Vd3NqdV 6iHkNVHzo3C12kEjve4QbVskgamxt0uKBkixiQ3UM/8EOccBLC71OkDBWpxiRqYdOBH5 mx9SSGbDC2WSkZFqEc+VOYvHzPLJ0OrYbE19g= 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=mMLlFka1I97WBpNYC6a4inYcni8WZqVzi63jLlkZWi4=; b=Zs0QpJHFR2+QqV7JTr735egO7dnTUxkLF6Hl4HeBHGbSBZdA7buz6s8lapA1+iJF7/ Lo6rC5smZ7Ds5UyiUQj7MtUpwFpgQ5hSsV1TTT1ha3labdlP8F4t9pD0lmK92rtb71ZC xDnRMQvMhV4+D3hNqkUfAtCZE/M5KwIdLvrJ3Bbcb/iK5ksVArY5QxU/vKfoep5WqjH3 eBV7VfxBszyRGlTiG4Wsfezx9SUGjD7ShKffCIbIMLH6s7k3oRMMUGZOLfw72hyDfWPW ujCncLcdCH72sTDiMg7DeKzOwBEyZJxrYpXYtV1vdhF7TWR+ds3RhHqnrR6PUiP9dblt ShTg== X-Gm-Message-State: ACrzQf2B2BZ+5rHvqh0emkXDvaZ+HsNL7GRTyZqBKf31xIY4VKQaty/K 985UbA7Jj5hUQySGeMasZhh99/pVGhfbZbtx X-Google-Smtp-Source: AMsMyM6FQBVzRSrfYqYI4K+zQsyvap1uAg7GY4Wor8zwg61CEy1cwP5rbvR3Fl0VoLOrK9Bkb95w9Q== X-Received: by 2002:aa7:c504:0:b0:461:122b:882b with SMTP id o4-20020aa7c504000000b00461122b882bmr25086551edq.14.1666708519387; Tue, 25 Oct 2022 07:35:19 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:18 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:28 +0200 Subject: [PATCH v2 7/8] media: uvcvideo: Protect uvc queue file operations against disconnect MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-7-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=2373; i=ribalda@chromium.org; h=from:subject:message-id; bh=VNSZpEP9LE78JkEZDvuh4x3SXN68AOH2OfrHeEklewU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QaTdNr8W8xCBJiosDRFO8HU1dfVbh3902iej3P m8gzCRmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0GgAKCRDRN9E+zzrEiBnhD/ 9wmBIBsC055I344aXmhM2aHVv1TK7xU8KToLtryYZk8LQNH9y8Ig9+QVK+r4QJg4ospiMPZD1SOawX VUnsDnvOceokhNWnFuKYgUecVqEOlqR8hw7dAbgnyVD1bwj4a5sehLIb3zPikjifZQfMK2dhCNJaYM e5SpvDDj0ovqFzeAwm8Zu93D+SBeW/XQ+smypqV5/Yp2mAf19HpqWLc9BibgpyJvxsMrAEm39EkPg1 BSStVdabcgeVmokFb7QCjTcKgAPCXxICwDljQcdTR3cUt3Vw6N0j1iIenekVpOgh2f/F9N1T6TF6wR GVf+cLl4t6MnSAGEXL8xWpqx/zZZ+CfO3B0ofoLxFvdhiGa62Igbi2uRGnpzlZdoYmg9vINVdXu1EQ neRRkAB0kBEdWswz5RDsfNVsq+OoXGfqfmOFLTVIzZbf+oPDGQPJWmH30b/TEZeruDwMu6LY9/Izhp JTiNsIf7/m4wFbBFXgYs3dy5trr4OpkvQfdgXcbgn/L/c5Uc0GqH4Z1nHko560Uduxpr2z8xXUOWfo G4yT1y48Ct0+i4CXNaN5aBgMg//PdRCdCSnhrvZBly9WPcsGrZZCtRl7skmukpShQbdCewb1KkvYdt h3FS7QlmqZ0RuR8pFcjWvmgk1nIFnfgUm0p6wsRhwxCimvmyV5+2hXDo9ndg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Guenter Roeck uvc queue file operations have no mutex protection against USB disconnect. This is questionable at least for the poll operation, which has to wait for the uvc queue mutex. By the time that mutex has been acquired, is it possible that the video device has been unregistered. Protect all file operations by using the queue mutex to avoid possible race conditions. After acquiring the mutex, check if the video device is still registered, and bail out if not. Cc: Laurent Pinchart Cc: Alan Stern Cc: Hans Verkuil Signed-off-by: Guenter Roeck diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c index 16fa17bbd15e..aed45cbc814e 100644 --- a/drivers/media/usb/uvc/uvc_queue.c +++ b/drivers/media/usb/uvc/uvc_queue.c @@ -354,24 +354,52 @@ int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type) int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) { - return vb2_mmap(&queue->queue, vma); + struct uvc_streaming *stream = uvc_queue_to_stream(queue); + int ret; + + mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto unlock; + } + ret = vb2_mmap(&queue->queue, vma); +unlock: + mutex_unlock(&queue->mutex); + return ret; } #ifndef CONFIG_MMU unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, unsigned long pgoff) { - return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); + struct uvc_streaming *stream = uvc_queue_to_stream(queue); + unsigned long ret; + + mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = -ENODEV; + goto unlock; + } + ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); +unlock: + mutex_unlock(&queue->mutex); + return ret; } #endif __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, poll_table *wait) { + struct uvc_streaming *stream = uvc_queue_to_stream(queue); __poll_t ret; mutex_lock(&queue->mutex); + if (!video_is_registered(&stream->vdev)) { + ret = EPOLLERR; + goto unlock; + } ret = vb2_poll(&queue->queue, file, wait); +unlock: mutex_unlock(&queue->mutex); return ret; From patchwork Tue Oct 25 14:34:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 618551 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 6804EECDFA1 for ; Tue, 25 Oct 2022 14:36:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231349AbiJYOgF (ORCPT ); Tue, 25 Oct 2022 10:36:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232477AbiJYOfb (ORCPT ); Tue, 25 Oct 2022 10:35:31 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E16FC9F750 for ; Tue, 25 Oct 2022 07:35:21 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id r14so36542159edc.7 for ; Tue, 25 Oct 2022 07:35:21 -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:message-id :reply-to; bh=cDM4r82Vu25XAn8FTag9RA1fMYhJwpZR6aZfJo/nKnA=; b=nIVyfuuLI2cY//5mXH5HGb5Gjqd/AcguOXfo5HF8Q8BDI7cOi75JXbGnOG3v+vIuz3 mt1skZQQchxvpTC3zTEBetG8//LJ4N9Xl1oxUA3Tj98dynsanFBRDfN5LYrPR8TOVWal FXN3gLDVO7LtkrPcDknz18vwrl8Qhcxtgll4s= 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=cDM4r82Vu25XAn8FTag9RA1fMYhJwpZR6aZfJo/nKnA=; b=Zs0clJ7755gt5eKEbDyHpkx5iGdPSzow5NAiLlSdkPKFAoLxV3Pv4eASawSY+7xb9F IvPp/3/sGZuQbTGpyRGgoPxUizqxnMVxRtVwiniy9zictMSIOimB7xZnNQDDNL+01xMe 9txo7oKVgrIs2Mf5DXqDVQHJJM0TEEQPnmbGDs8i5fU/mIXyYAyr1Gh5Vp9haUpkeeUs oN45JjmiD87wWJf/kr6ls4++u8fRVxDTPjHuoDlfx5XoPg0zm0PW9dBQBoYr9ylQ/zyL PqZdF6nXxDq+Cb4yV2jPjI0xXO8m6knIVcqmWHLitmnorsm09TJvrUa4deoZMaxy3O9S OpFg== X-Gm-Message-State: ACrzQf1iBk9YMmK4t5QS04qtDrAXWkE1ptwr0xkMy57Z7mAcnsl8kbg8 ifAo8XmWcwMuUQMYi+hDAyp+ng== X-Google-Smtp-Source: AMsMyM6ZxduJLpfqNo1FL1eE1kp3pE8jxsD/Mw6MleY81obi30oIH1q1NflkTjkUCgp4ZZhPnjBT1g== X-Received: by 2002:a05:6402:496:b0:443:a5f5:d3b with SMTP id k22-20020a056402049600b00443a5f50d3bmr37136318edv.331.1666708520472; Tue, 25 Oct 2022 07:35:20 -0700 (PDT) Received: from alco.roam.corp.google.com ([2620:0:1059:10:e6ae:c7ac:c234:953c]) by smtp.gmail.com with ESMTPSA id e9-20020a170906314900b007acd04fcedcsm631021eje.46.2022.10.25.07.35.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:35:20 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 25 Oct 2022 16:34:29 +0200 Subject: [PATCH v2 8/8] media: uvcvideo: Only call status ep if hw supports it MIME-Version: 1.0 Message-Id: <20220920-resend-powersave-v2-8-5135d1bb1c38@chromium.org> References: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> In-Reply-To: <20220920-resend-powersave-v2-0-5135d1bb1c38@chromium.org> To: Mauro Carvalho Chehab Cc: Laurent Pinchart , Guenter Roeck , Max Staudt , linux-media@vger.kernel.org, Tomasz Figa , linux-kernel@vger.kernel.org, Alan Stern , Ricardo Ribalda , Hans Verkuil X-Mailer: b4 0.11.0-dev-d93f8 X-Developer-Signature: v=1; a=openpgp-sha256; l=4002; i=ribalda@chromium.org; h=from:subject:message-id; bh=IWi5UBoOQCglgZ8fk/i0G4nYeLqeSgpNrXbJjKKYXhk=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjV/QcG5LK6K0jUjnoGq8KBuuEpJ6sMYJlNi4Z99xs BCAWNeiJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY1f0HAAKCRDRN9E+zzrEiH3VEA CIqowtYcGGbBD5Wo2qirQ1FwAwCwchTq489hfSl5qGvsmBQohbYAFQDlUA7h2CRwwQnkU/vt3ZpFab 8M1Rvi+rfRZk9bSrcvodaXlamQIAQvShxFKOBniermTFJ4WJwyTDboodQyCtnhkiVczysx0Tb7RcrQ dNH9Uw/2ttvLSclYZO/msOuA8XYz+64OWIoMVP6wkeimPHw+QXUq+aFmRaWeIwEsEU394owG674Ct7 tvA72i0vlg3Ls3sA1vPavMa/RDwnR+Arx7wXHAhZEYoTHTrq7PBvhBXWaHhye3roTAQZBbokSTmKq6 v8Byx7IpUaBSoURpqsi342uJU+kdU7Or4foGrEXvrM7s5O+O0EHeXGGw8JO4xVZeA/edYDcg5vQ6/u jnE5jYg5sm1mom3gG0sKQnWVxWw770mR+kgAM9eL5nlRqg15VXz/rRLPau8giNSudLk6bmw1bGysgt +eLWLT7vIBwUWfU7I0HPZ8hF8Hxz9I0n26m8VK1xQlP1VxxqHtcvJ85dkIckJ1P++hBOKH9kDumDNe C7NU709b8c9tWMVgdHsaOtB+vgT5t65tpN7+bplq6flFkkt9Id9y6RhD6IbvVzgvo5Zs7c1UsGz8rU 03qsjRjeWNYJueL1GByAPAzYO+yRWATDEkzzZ+velzXiVLlbQCK4VKm1wxmQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Instead of calling uvc_status_* regardless if the hw supports it or not, make all the calls conditional. This simplifies the locking during suspend/resume. Signed-off-by: Ricardo Ribalda diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index f15fbdbcd8bb..d2841f0d9132 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1847,7 +1847,8 @@ static void uvc_delete(struct kref *kref) struct uvc_device *dev = container_of(kref, struct uvc_device, ref); struct list_head *p, *n; - uvc_status_cleanup(dev); + if (dev->int_ep) + uvc_status_cleanup(dev); uvc_ctrl_cleanup_device(dev); usb_put_intf(dev->intf); @@ -1918,7 +1919,8 @@ static void uvc_unregister_video(struct uvc_device *dev) mutex_unlock(&stream->mutex); } - uvc_status_unregister(dev); + if (dev->int_ep) + uvc_status_unregister(dev); uvc_ctrl_stop_device(dev); if (dev->vdev.dev) @@ -2222,7 +2224,9 @@ static int uvc_probe(struct usb_interface *intf, usb_set_intfdata(intf, dev); /* Initialize the interrupt URB. */ - if ((ret = uvc_status_init(dev)) < 0) { + if (dev->int_ep) + ret = uvc_status_init(dev); + if (ret < 0) { dev_info(&dev->udev->dev, "Unable to initialize the status endpoint (%d), status interrupt will not be supported.\n", ret); @@ -2274,6 +2278,8 @@ static int uvc_suspend(struct usb_interface *intf, pm_message_t message) /* Controls are cached on the fly so they don't need to be saved. */ if (intf->cur_altsetting->desc.bInterfaceSubClass == UVC_SC_VIDEOCONTROL) { + if (!dev->int_ep) + return 0; mutex_lock(&dev->lock); if (dev->users) uvc_status_stop(dev); @@ -2308,6 +2314,9 @@ static int __uvc_resume(struct usb_interface *intf, int reset) return ret; } + if (!dev->int_ep) + return ret; + mutex_lock(&dev->lock); if (dev->users) ret = uvc_status_start(dev, GFP_NOIO); diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index cb90aff344bc..627cf11066e7 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -277,7 +277,7 @@ int uvc_status_init(struct uvc_device *dev) unsigned int pipe; int interval; - if (ep == NULL) + if (WARN_ON(!ep)) return 0; uvc_input_init(dev); @@ -312,19 +312,23 @@ int uvc_status_init(struct uvc_device *dev) void uvc_status_unregister(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep)) + return; usb_kill_urb(dev->int_urb); uvc_input_unregister(dev); } void uvc_status_cleanup(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep)) + return; usb_free_urb(dev->int_urb); kfree(dev->status); } int uvc_status_start(struct uvc_device *dev, gfp_t flags) { - if (dev->int_urb == NULL) + if (WARN_ON(!dev->int_ep) || !dev->int_urb) return 0; return usb_submit_urb(dev->int_urb, flags); @@ -332,5 +336,8 @@ int uvc_status_start(struct uvc_device *dev, gfp_t flags) void uvc_status_stop(struct uvc_device *dev) { + if (WARN_ON(!dev->int_ep) || !dev->int_urb) + return; + usb_kill_urb(dev->int_urb); } diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index c250b628fc4f..2861ab122beb 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -44,7 +44,7 @@ static int uvc_pm_get(struct uvc_streaming *stream) goto done; } - if (!stream->dev->users) + if (!stream->dev->users && stream->dev->int_ep) ret = uvc_status_start(stream->dev, GFP_KERNEL); if (!ret) stream->dev->users++; @@ -66,7 +66,7 @@ static void uvc_pm_put(struct uvc_streaming *stream) return; } stream->dev->users--; - if (!stream->dev->users) + if (!stream->dev->users && stream->dev->int_ep) uvc_status_stop(stream->dev); mutex_unlock(&stream->dev->lock);