Commit 44f70338 authored by Ricardo Ribalda's avatar Ricardo Ribalda Committed by Hans Verkuil
Browse files

media: uvcvideo: Refactor the status irq API



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.

No functional change is expected from this patch.

Reviewed-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: default avatarRicardo Ribalda <ribalda@chromium.org>
Reviewed-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Link: https://lore.kernel.org/r/20240926-guenter-mini-v7-1-690441953d4a@chromium.org


Signed-off-by: default avatarLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
parent 3dd075fe
Loading
Loading
Loading
Loading
+2 −11
Original line number Diff line number Diff line
@@ -2132,7 +2132,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);
@@ -2304,10 +2303,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;
	}

@@ -2338,12 +2334,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) {
+54 −2
Original line number Diff line number Diff line
@@ -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,49 @@ void uvc_status_stop(struct uvc_device *dev)
	 */
	smp_store_release(&dev->flush_status, false);
}

int uvc_status_resume(struct uvc_device *dev)
{
	guard(mutex)(&dev->status_lock);

	if (dev->status_users)
		return uvc_status_start(dev, GFP_NOIO);

	return 0;
}

void uvc_status_suspend(struct uvc_device *dev)
{
	guard(mutex)(&dev->status_lock);

	if (dev->status_users)
		uvc_status_stop(dev);
}

int uvc_status_get(struct uvc_device *dev)
{
	int ret;

	guard(mutex)(&dev->status_lock);

	if (!dev->status_users) {
		ret = uvc_status_start(dev, GFP_KERNEL);
		if (ret)
			return ret;
	}

	dev->status_users++;

	return 0;
}

void uvc_status_put(struct uvc_device *dev)
{
	guard(mutex)(&dev->status_lock);

	if (dev->status_users == 1)
		uvc_status_stop(dev);
	WARN_ON(!dev->status_users);
	if (dev->status_users)
		dev->status_users--;
}
+6 −16
Original line number Diff line number Diff line
@@ -628,19 +628,12 @@ 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);
	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);
@@ -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;
+6 −4
Original line number Diff line number Diff line
@@ -563,8 +563,6 @@ struct uvc_device {

	const struct uvc_device_info *info;

	struct mutex lock;		/* Protects users */
	unsigned int users;
	atomic_t nmappings;

	/* Video control interface */
@@ -586,6 +584,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;
@@ -752,8 +752,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 v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;