From patchwork Tue Jun 11 08:12:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 803374 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4050173345 for ; Tue, 11 Jun 2024 08:12:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093534; cv=none; b=BDeFms2N15PCGev+IkDdv7jDLAngH00G/k046xBaN/fg5b+VS0+q217zkvlKdXxdMU9YPiaV9Oo4F9k9zBxN/6WjaR0yziYj7JdOEFlX+HxE/MttSnODd9rQME23ymaog7ImFScWJYTbrSLKcsTFNVjjcy/9mwSv7hy6mg2+qco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718093534; c=relaxed/simple; bh=64alHD74cnDTIfHQOlIz+XywS99iYHgMLqGlSmRdKn0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TaW3fjR+a1v0K+45Et9HCoU5YT/43f/GKg6KDZ43smPI62FQ3hlKu7KASzEM5dtCu92VVghr8osPjrfrvbOUeBIMczj39fPj3p5ul+iCu7olK56Ga3ELiXMbY48nMlgPad8SLCkX7c1ZmO4Qeds5CXheMvewldnuaWgo+54Rmu4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=EYrlbsNF; arc=none smtp.client-ip=209.85.222.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="EYrlbsNF" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-795482e114cso46419785a.1 for ; Tue, 11 Jun 2024 01:12:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718093532; x=1718698332; darn=vger.kernel.org; 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=EOMaXZDQx8tFTSRH/17UvPzHD6txAvg9gMmc9mkFLc0=; b=EYrlbsNFlqU7UlOXwXFRLpxZs1ELmSEIgSwC/VOo31hy6JHrCCFdUJ81uUN77Weqjd neBxiic6vit+o3v6OUwsdGrxGDx1JmuvKmUZb63E2EQLJriGXPXiPYqWTfxIPx38596p qvivmwk5PGc7yZK+XvOED5pxKiabflCrhnM6Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718093532; x=1718698332; 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=EOMaXZDQx8tFTSRH/17UvPzHD6txAvg9gMmc9mkFLc0=; b=A/8tnnp0SnjRI5nnJRWp/X7Wx6q7Un3VdvX3SUBlaOV0RmPDyPvnU7fnV5mQT0YuxZ jKDMuE/OWeRyl20/iWRCmvWE0/L5mr10sDIZdAY3zeV1GOKR5TWPV2FvSPHw7LRGQvo/ 3bouNVDqxMo7+98xFRKI/jPr5s0aCNa/3REK7KrvdMsXwK4O3VOipcx8hfT2rCBMj7CZ Vo5yt0dd5gTszV6SQxLhmJZGosDU2Cwvf0bfsjCIh2CInZa0h9NtwG9Pc6twyAmn30hR w4XqaIJoVmsqoSJdzjzIG4cwogo9sFnGwQbSHYXsNkQqOOHQsdyEsaqAZCYmym0vBMMQ BUbw== X-Forwarded-Encrypted: i=1; AJvYcCUBKZleJo3o/3Pc5N60XmHP2dknqHnfQX911CSSr6vPSLnvkqZ1JAKU/9bcQobAfefh7w3ttKdyIeuL3VHdmt7VhGTPWzDQ47M03kI= X-Gm-Message-State: AOJu0YxYwMZCoXieYLzmWrHMn0sZEj2lIl1pKOLgJRwto9Zx61tbQEKz H+tFpcayb5Ve2Wpq2SY2Z9uznaE0fSfNEMlhc9LkLhtThAgkZmGZJRgf9HiyAA== X-Google-Smtp-Source: AGHT+IGJ+4NUXCKFlk1lZknsxWrx0lQOFKK6eC9/TccyOHyB219dw2R65Gykyu0l1FHZsE4vR0WQQw== X-Received: by 2002:a05:6214:4486:b0:6a3:294b:5622 with SMTP id 6a1803df08f44-6b059b712camr137350906d6.17.1718093531754; Tue, 11 Jun 2024 01:12:11 -0700 (PDT) Received: from denia.c.googlers.com (123.178.145.34.bc.googleusercontent.com. [34.145.178.123]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b09039c2b9sm1548886d6.28.2024.06.11.01.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 01:12:11 -0700 (PDT) From: Ricardo Ribalda Date: Tue, 11 Jun 2024 08:12:07 +0000 Subject: [PATCH v5 2/4] media: uvcvideo: Refactor the status irq API Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240611-guenter-mini-v5-2-047b6fe5d08b@chromium.org> References: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> In-Reply-To: <20240611-guenter-mini-v5-0-047b6fe5d08b@chromium.org> To: Mauro Carvalho Chehab Cc: Guenter Roeck , Tomasz Figa , Laurent Pinchart , Alan Stern , Hans Verkuil , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sean Paul , Ricardo Ribalda , Sakari Ailus X-Mailer: b4 0.13.0 There are two different use-cases of uvc_status(): - adding/removing a user when the camera is open/closed - stopping/starting when the camera is suspended/resumed Make the API reflect these two use-cases and move all the refcounting and locking logic to the uvc_status.c file. Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 13 ++------- drivers/media/usb/uvc/uvc_status.c | 55 ++++++++++++++++++++++++++++++++++++-- drivers/media/usb/uvc/uvc_v4l2.c | 22 +++++---------- drivers/media/usb/uvc/uvcvideo.h | 10 ++++--- 4 files changed, 67 insertions(+), 33 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index f1df6384e738..078b8fb05b54 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2135,7 +2135,6 @@ static int uvc_probe(struct usb_interface *intf, INIT_LIST_HEAD(&dev->streams); kref_init(&dev->ref); atomic_set(&dev->nmappings, 0); - mutex_init(&dev->lock); dev->udev = usb_get_dev(udev); dev->intf = usb_get_intf(intf); @@ -2301,10 +2300,7 @@ 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) { - mutex_lock(&dev->lock); - if (dev->users) - uvc_status_stop(dev); - mutex_unlock(&dev->lock); + uvc_status_suspend(dev); return 0; } @@ -2335,12 +2331,7 @@ static int __uvc_resume(struct usb_interface *intf, int reset) return ret; } - mutex_lock(&dev->lock); - if (dev->users) - ret = uvc_status_start(dev, GFP_NOIO); - mutex_unlock(&dev->lock); - - return ret; + return uvc_status_resume(dev); } list_for_each_entry(stream, &dev->streams, list) { diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index a78a88c710e2..375a95dd3011 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -257,6 +257,8 @@ int uvc_status_init(struct uvc_device *dev) unsigned int pipe; int interval; + mutex_init(&dev->status_lock); + if (ep == NULL) return 0; @@ -302,18 +304,22 @@ void uvc_status_cleanup(struct uvc_device *dev) kfree(dev->status); } -int uvc_status_start(struct uvc_device *dev, gfp_t flags) +static int __uvc_status_start(struct uvc_device *dev, gfp_t flags) { + lockdep_assert_held(&dev->status_lock); + if (dev->int_urb == NULL) return 0; return usb_submit_urb(dev->int_urb, flags); } -void uvc_status_stop(struct uvc_device *dev) +static void __uvc_status_stop(struct uvc_device *dev) { struct uvc_ctrl_work *w = &dev->async_ctrl; + lockdep_assert_held(&dev->status_lock); + /* * Prevent the asynchronous control handler from requeing the URB. The * barrier is needed so the flush_status change is visible to other @@ -350,3 +356,48 @@ void uvc_status_stop(struct uvc_device *dev) */ smp_store_release(&dev->flush_status, false); } + +int uvc_status_resume(struct uvc_device *dev) +{ + int ret = 0; + + mutex_lock(&dev->status_lock); + if (dev->status_users) + ret = __uvc_status_start(dev, GFP_NOIO); + mutex_unlock(&dev->status_lock); + + return ret; +} + +void uvc_status_suspend(struct uvc_device *dev) +{ + mutex_lock(&dev->status_lock); + if (dev->status_users) + __uvc_status_stop(dev); + mutex_unlock(&dev->status_lock); +} + +int uvc_status_get(struct uvc_device *dev) +{ + int ret = 0; + + mutex_lock(&dev->status_lock); + if (!dev->status_users) + ret = __uvc_status_start(dev, GFP_KERNEL); + if (!ret) + dev->status_users++; + mutex_unlock(&dev->status_lock); + + return ret; +} + +void uvc_status_put(struct uvc_device *dev) +{ + mutex_lock(&dev->status_lock); + if (dev->status_users == 1) + __uvc_status_stop(dev); + WARN_ON(!dev->status_users); + if (dev->status_users) + dev->status_users--; + mutex_unlock(&dev->status_lock); +} diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index f4988f03640a..97c5407f6603 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -628,20 +628,13 @@ static int uvc_v4l2_open(struct file *file) 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); - kfree(handle); - return ret; - } + ret = uvc_status_get(stream->dev); + if (ret) { + usb_autopm_put_interface(stream->dev->intf); + 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; @@ -670,10 +663,7 @@ 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); + uvc_status_put(stream->dev); usb_autopm_put_interface(stream->dev->intf); return 0; diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 6fb0a78b1b00..00b600eb058c 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -555,8 +555,6 @@ struct uvc_device { const struct uvc_device_info *info; - struct mutex lock; /* Protects users */ - unsigned int users; atomic_t nmappings; /* Video control interface */ @@ -578,6 +576,8 @@ struct uvc_device { struct usb_host_endpoint *int_ep; struct urb *int_urb; struct uvc_status *status; + struct mutex status_lock; /* Protects status_users */ + unsigned int status_users; bool flush_status; struct input_dev *input; @@ -744,8 +744,10 @@ int uvc_register_video_device(struct uvc_device *dev, int uvc_status_init(struct uvc_device *dev); void uvc_status_unregister(struct uvc_device *dev); void uvc_status_cleanup(struct uvc_device *dev); -int uvc_status_start(struct uvc_device *dev, gfp_t flags); -void uvc_status_stop(struct uvc_device *dev); +int uvc_status_resume(struct uvc_device *dev); +void uvc_status_suspend(struct uvc_device *dev); +int uvc_status_get(struct uvc_device *dev); +void uvc_status_put(struct uvc_device *dev); /* Controls */ extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited;