xref: /openbmc/linux/drivers/media/usb/uvc/uvc_status.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab  *      uvc_status.c  --  USB Video Class driver - Status endpoint
40c0d06caSMauro Carvalho Chehab  *
50c0d06caSMauro Carvalho Chehab  *      Copyright (C) 2005-2009
60c0d06caSMauro Carvalho Chehab  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
70c0d06caSMauro Carvalho Chehab  */
80c0d06caSMauro Carvalho Chehab 
9*619d9b71SRicardo Ribalda #include <asm/barrier.h>
100c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
110c0d06caSMauro Carvalho Chehab #include <linux/input.h>
120c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
130c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
140c0d06caSMauro Carvalho Chehab #include <linux/usb/input.h>
150c0d06caSMauro Carvalho Chehab 
160c0d06caSMauro Carvalho Chehab #include "uvcvideo.h"
170c0d06caSMauro Carvalho Chehab 
180c0d06caSMauro Carvalho Chehab /* --------------------------------------------------------------------------
190c0d06caSMauro Carvalho Chehab  * Input device
200c0d06caSMauro Carvalho Chehab  */
210c0d06caSMauro Carvalho Chehab #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
223bc22dc6SRicardo Ribalda 
uvc_input_has_button(struct uvc_device * dev)233bc22dc6SRicardo Ribalda static bool uvc_input_has_button(struct uvc_device *dev)
243bc22dc6SRicardo Ribalda {
253bc22dc6SRicardo Ribalda 	struct uvc_streaming *stream;
263bc22dc6SRicardo Ribalda 
273bc22dc6SRicardo Ribalda 	/*
283bc22dc6SRicardo Ribalda 	 * The device has button events if both bTriggerSupport and
293bc22dc6SRicardo Ribalda 	 * bTriggerUsage are one. Otherwise the camera button does not
303bc22dc6SRicardo Ribalda 	 * exist or is handled automatically by the camera without host
313bc22dc6SRicardo Ribalda 	 * driver or client application intervention.
323bc22dc6SRicardo Ribalda 	 */
333bc22dc6SRicardo Ribalda 	list_for_each_entry(stream, &dev->streams, list) {
343bc22dc6SRicardo Ribalda 		if (stream->header.bTriggerSupport == 1 &&
353bc22dc6SRicardo Ribalda 		    stream->header.bTriggerUsage == 1)
363bc22dc6SRicardo Ribalda 			return true;
373bc22dc6SRicardo Ribalda 	}
383bc22dc6SRicardo Ribalda 
393bc22dc6SRicardo Ribalda 	return false;
403bc22dc6SRicardo Ribalda }
413bc22dc6SRicardo Ribalda 
uvc_input_init(struct uvc_device * dev)420c0d06caSMauro Carvalho Chehab static int uvc_input_init(struct uvc_device *dev)
430c0d06caSMauro Carvalho Chehab {
440c0d06caSMauro Carvalho Chehab 	struct input_dev *input;
450c0d06caSMauro Carvalho Chehab 	int ret;
460c0d06caSMauro Carvalho Chehab 
473bc22dc6SRicardo Ribalda 	if (!uvc_input_has_button(dev))
483bc22dc6SRicardo Ribalda 		return 0;
493bc22dc6SRicardo Ribalda 
500c0d06caSMauro Carvalho Chehab 	input = input_allocate_device();
510c0d06caSMauro Carvalho Chehab 	if (input == NULL)
520c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
530c0d06caSMauro Carvalho Chehab 
540c0d06caSMauro Carvalho Chehab 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
550c0d06caSMauro Carvalho Chehab 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
560c0d06caSMauro Carvalho Chehab 
570c0d06caSMauro Carvalho Chehab 	input->name = dev->name;
580c0d06caSMauro Carvalho Chehab 	input->phys = dev->input_phys;
590c0d06caSMauro Carvalho Chehab 	usb_to_input_id(dev->udev, &input->id);
600c0d06caSMauro Carvalho Chehab 	input->dev.parent = &dev->intf->dev;
610c0d06caSMauro Carvalho Chehab 
620c0d06caSMauro Carvalho Chehab 	__set_bit(EV_KEY, input->evbit);
630c0d06caSMauro Carvalho Chehab 	__set_bit(KEY_CAMERA, input->keybit);
640c0d06caSMauro Carvalho Chehab 
650c0d06caSMauro Carvalho Chehab 	if ((ret = input_register_device(input)) < 0)
660c0d06caSMauro Carvalho Chehab 		goto error;
670c0d06caSMauro Carvalho Chehab 
680c0d06caSMauro Carvalho Chehab 	dev->input = input;
690c0d06caSMauro Carvalho Chehab 	return 0;
700c0d06caSMauro Carvalho Chehab 
710c0d06caSMauro Carvalho Chehab error:
720c0d06caSMauro Carvalho Chehab 	input_free_device(input);
730c0d06caSMauro Carvalho Chehab 	return ret;
740c0d06caSMauro Carvalho Chehab }
750c0d06caSMauro Carvalho Chehab 
uvc_input_unregister(struct uvc_device * dev)7610e1fdb9SDaniel Axtens static void uvc_input_unregister(struct uvc_device *dev)
770c0d06caSMauro Carvalho Chehab {
780c0d06caSMauro Carvalho Chehab 	if (dev->input)
790c0d06caSMauro Carvalho Chehab 		input_unregister_device(dev->input);
800c0d06caSMauro Carvalho Chehab }
810c0d06caSMauro Carvalho Chehab 
uvc_input_report_key(struct uvc_device * dev,unsigned int code,int value)820c0d06caSMauro Carvalho Chehab static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
830c0d06caSMauro Carvalho Chehab 	int value)
840c0d06caSMauro Carvalho Chehab {
850c0d06caSMauro Carvalho Chehab 	if (dev->input) {
860c0d06caSMauro Carvalho Chehab 		input_report_key(dev->input, code, value);
870c0d06caSMauro Carvalho Chehab 		input_sync(dev->input);
880c0d06caSMauro Carvalho Chehab 	}
890c0d06caSMauro Carvalho Chehab }
900c0d06caSMauro Carvalho Chehab 
910c0d06caSMauro Carvalho Chehab #else
920c0d06caSMauro Carvalho Chehab #define uvc_input_init(dev)
9310e1fdb9SDaniel Axtens #define uvc_input_unregister(dev)
940c0d06caSMauro Carvalho Chehab #define uvc_input_report_key(dev, code, value)
950c0d06caSMauro Carvalho Chehab #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
960c0d06caSMauro Carvalho Chehab 
970c0d06caSMauro Carvalho Chehab /* --------------------------------------------------------------------------
980c0d06caSMauro Carvalho Chehab  * Status interrupt endpoint
990c0d06caSMauro Carvalho Chehab  */
uvc_event_streaming(struct uvc_device * dev,struct uvc_status * status,int len)100e5225c82SGuennadi Liakhovetski static void uvc_event_streaming(struct uvc_device *dev,
101adfd3910SRicardo Ribalda 				struct uvc_status *status, int len)
1020c0d06caSMauro Carvalho Chehab {
103adfd3910SRicardo Ribalda 	if (len <= offsetof(struct uvc_status, bEvent)) {
1049e56380aSJoe Perches 		uvc_dbg(dev, STATUS,
1059e56380aSJoe Perches 			"Invalid streaming status event received\n");
1060c0d06caSMauro Carvalho Chehab 		return;
1070c0d06caSMauro Carvalho Chehab 	}
1080c0d06caSMauro Carvalho Chehab 
109e5225c82SGuennadi Liakhovetski 	if (status->bEvent == 0) {
110adfd3910SRicardo Ribalda 		if (len <= offsetof(struct uvc_status, streaming))
1110c0d06caSMauro Carvalho Chehab 			return;
112adfd3910SRicardo Ribalda 
1139e56380aSJoe Perches 		uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
114e5225c82SGuennadi Liakhovetski 			status->bOriginator,
115adfd3910SRicardo Ribalda 			status->streaming.button ? "pressed" : "released", len);
116adfd3910SRicardo Ribalda 		uvc_input_report_key(dev, KEY_CAMERA, status->streaming.button);
1170c0d06caSMauro Carvalho Chehab 	} else {
1189e56380aSJoe Perches 		uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
119e5225c82SGuennadi Liakhovetski 			status->bOriginator, status->bEvent, len);
1200c0d06caSMauro Carvalho Chehab 	}
1210c0d06caSMauro Carvalho Chehab }
1220c0d06caSMauro Carvalho Chehab 
123e5225c82SGuennadi Liakhovetski #define UVC_CTRL_VALUE_CHANGE	0
124e5225c82SGuennadi Liakhovetski #define UVC_CTRL_INFO_CHANGE	1
125e5225c82SGuennadi Liakhovetski #define UVC_CTRL_FAILURE_CHANGE	2
126e5225c82SGuennadi Liakhovetski #define UVC_CTRL_MIN_CHANGE	3
127e5225c82SGuennadi Liakhovetski #define UVC_CTRL_MAX_CHANGE	4
128e5225c82SGuennadi Liakhovetski 
uvc_event_entity_find_ctrl(struct uvc_entity * entity,u8 selector)129e5225c82SGuennadi Liakhovetski static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
130e5225c82SGuennadi Liakhovetski 						      u8 selector)
1310c0d06caSMauro Carvalho Chehab {
132e5225c82SGuennadi Liakhovetski 	struct uvc_control *ctrl;
133e5225c82SGuennadi Liakhovetski 	unsigned int i;
1340c0d06caSMauro Carvalho Chehab 
135e5225c82SGuennadi Liakhovetski 	for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
136e5225c82SGuennadi Liakhovetski 		if (ctrl->info.selector == selector)
137e5225c82SGuennadi Liakhovetski 			return ctrl;
138e5225c82SGuennadi Liakhovetski 
139e5225c82SGuennadi Liakhovetski 	return NULL;
140e5225c82SGuennadi Liakhovetski }
141e5225c82SGuennadi Liakhovetski 
uvc_event_find_ctrl(struct uvc_device * dev,const struct uvc_status * status,struct uvc_video_chain ** chain)142e5225c82SGuennadi Liakhovetski static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
143adfd3910SRicardo Ribalda 					const struct uvc_status *status,
144e5225c82SGuennadi Liakhovetski 					struct uvc_video_chain **chain)
145e5225c82SGuennadi Liakhovetski {
146e5225c82SGuennadi Liakhovetski 	list_for_each_entry((*chain), &dev->chains, list) {
147e5225c82SGuennadi Liakhovetski 		struct uvc_entity *entity;
148e5225c82SGuennadi Liakhovetski 		struct uvc_control *ctrl;
149e5225c82SGuennadi Liakhovetski 
150e5225c82SGuennadi Liakhovetski 		list_for_each_entry(entity, &(*chain)->entities, chain) {
151e5225c82SGuennadi Liakhovetski 			if (entity->id != status->bOriginator)
152e5225c82SGuennadi Liakhovetski 				continue;
153e5225c82SGuennadi Liakhovetski 
154e5225c82SGuennadi Liakhovetski 			ctrl = uvc_event_entity_find_ctrl(entity,
155adfd3910SRicardo Ribalda 						     status->control.bSelector);
156e5225c82SGuennadi Liakhovetski 			if (ctrl)
157e5225c82SGuennadi Liakhovetski 				return ctrl;
158e5225c82SGuennadi Liakhovetski 		}
159e5225c82SGuennadi Liakhovetski 	}
160e5225c82SGuennadi Liakhovetski 
161e5225c82SGuennadi Liakhovetski 	return NULL;
162e5225c82SGuennadi Liakhovetski }
163e5225c82SGuennadi Liakhovetski 
uvc_event_control(struct urb * urb,const struct uvc_status * status,int len)164e5225c82SGuennadi Liakhovetski static bool uvc_event_control(struct urb *urb,
165adfd3910SRicardo Ribalda 			      const struct uvc_status *status, int len)
166e5225c82SGuennadi Liakhovetski {
167e5225c82SGuennadi Liakhovetski 	static const char *attrs[] = { "value", "info", "failure", "min", "max" };
168e5225c82SGuennadi Liakhovetski 	struct uvc_device *dev = urb->context;
169e5225c82SGuennadi Liakhovetski 	struct uvc_video_chain *chain;
170e5225c82SGuennadi Liakhovetski 	struct uvc_control *ctrl;
171e5225c82SGuennadi Liakhovetski 
172e5225c82SGuennadi Liakhovetski 	if (len < 6 || status->bEvent != 0 ||
173adfd3910SRicardo Ribalda 	    status->control.bAttribute >= ARRAY_SIZE(attrs)) {
1749e56380aSJoe Perches 		uvc_dbg(dev, STATUS, "Invalid control status event received\n");
175e5225c82SGuennadi Liakhovetski 		return false;
1760c0d06caSMauro Carvalho Chehab 	}
1770c0d06caSMauro Carvalho Chehab 
1789e56380aSJoe Perches 	uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
179adfd3910SRicardo Ribalda 		status->bOriginator, status->control.bSelector,
180adfd3910SRicardo Ribalda 		attrs[status->control.bAttribute], len);
181e5225c82SGuennadi Liakhovetski 
182e5225c82SGuennadi Liakhovetski 	/* Find the control. */
183e5225c82SGuennadi Liakhovetski 	ctrl = uvc_event_find_ctrl(dev, status, &chain);
184e5225c82SGuennadi Liakhovetski 	if (!ctrl)
185e5225c82SGuennadi Liakhovetski 		return false;
186e5225c82SGuennadi Liakhovetski 
187adfd3910SRicardo Ribalda 	switch (status->control.bAttribute) {
188e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_VALUE_CHANGE:
189d9c8763eSRicardo Ribalda 		return uvc_ctrl_status_event_async(urb, chain, ctrl,
190adfd3910SRicardo Ribalda 						   status->control.bValue);
191e5225c82SGuennadi Liakhovetski 
192e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_INFO_CHANGE:
193e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_FAILURE_CHANGE:
194e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_MIN_CHANGE:
195e5225c82SGuennadi Liakhovetski 	case UVC_CTRL_MAX_CHANGE:
196e5225c82SGuennadi Liakhovetski 		break;
197e5225c82SGuennadi Liakhovetski 	}
198e5225c82SGuennadi Liakhovetski 
199e5225c82SGuennadi Liakhovetski 	return false;
2000c0d06caSMauro Carvalho Chehab }
2010c0d06caSMauro Carvalho Chehab 
uvc_status_complete(struct urb * urb)2020c0d06caSMauro Carvalho Chehab static void uvc_status_complete(struct urb *urb)
2030c0d06caSMauro Carvalho Chehab {
2040c0d06caSMauro Carvalho Chehab 	struct uvc_device *dev = urb->context;
2050c0d06caSMauro Carvalho Chehab 	int len, ret;
2060c0d06caSMauro Carvalho Chehab 
2070c0d06caSMauro Carvalho Chehab 	switch (urb->status) {
2080c0d06caSMauro Carvalho Chehab 	case 0:
2090c0d06caSMauro Carvalho Chehab 		break;
2100c0d06caSMauro Carvalho Chehab 
2110c0d06caSMauro Carvalho Chehab 	case -ENOENT:		/* usb_kill_urb() called. */
2120c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:	/* usb_unlink_urb() called. */
2130c0d06caSMauro Carvalho Chehab 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
214699b9a86SLaurent Pinchart 	case -EPROTO:		/* Device is disconnected (reported by some host controllers). */
2150c0d06caSMauro Carvalho Chehab 		return;
2160c0d06caSMauro Carvalho Chehab 
2170c0d06caSMauro Carvalho Chehab 	default:
21869df0954SRicardo Ribalda 		dev_warn(&dev->udev->dev,
21969df0954SRicardo Ribalda 			 "Non-zero status (%d) in status completion handler.\n",
22069df0954SRicardo Ribalda 			 urb->status);
2210c0d06caSMauro Carvalho Chehab 		return;
2220c0d06caSMauro Carvalho Chehab 	}
2230c0d06caSMauro Carvalho Chehab 
2240c0d06caSMauro Carvalho Chehab 	len = urb->actual_length;
2250c0d06caSMauro Carvalho Chehab 	if (len > 0) {
226adfd3910SRicardo Ribalda 		switch (dev->status->bStatusType & 0x0f) {
227e5225c82SGuennadi Liakhovetski 		case UVC_STATUS_TYPE_CONTROL: {
228adfd3910SRicardo Ribalda 			if (uvc_event_control(urb, dev->status, len))
229e5225c82SGuennadi Liakhovetski 				/* The URB will be resubmitted in work context. */
230e5225c82SGuennadi Liakhovetski 				return;
2310c0d06caSMauro Carvalho Chehab 			break;
232e5225c82SGuennadi Liakhovetski 		}
233e5225c82SGuennadi Liakhovetski 
234e5225c82SGuennadi Liakhovetski 		case UVC_STATUS_TYPE_STREAMING: {
235adfd3910SRicardo Ribalda 			uvc_event_streaming(dev, dev->status, len);
236e5225c82SGuennadi Liakhovetski 			break;
237e5225c82SGuennadi Liakhovetski 		}
2380c0d06caSMauro Carvalho Chehab 
2390c0d06caSMauro Carvalho Chehab 		default:
2409e56380aSJoe Perches 			uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
241adfd3910SRicardo Ribalda 				dev->status->bStatusType);
2420c0d06caSMauro Carvalho Chehab 			break;
2430c0d06caSMauro Carvalho Chehab 		}
2440c0d06caSMauro Carvalho Chehab 	}
2450c0d06caSMauro Carvalho Chehab 
2460c0d06caSMauro Carvalho Chehab 	/* Resubmit the URB. */
2470c0d06caSMauro Carvalho Chehab 	urb->interval = dev->int_ep->desc.bInterval;
24869df0954SRicardo Ribalda 	ret = usb_submit_urb(urb, GFP_ATOMIC);
24969df0954SRicardo Ribalda 	if (ret < 0)
25069df0954SRicardo Ribalda 		dev_err(&dev->udev->dev,
25169df0954SRicardo Ribalda 			"Failed to resubmit status URB (%d).\n", ret);
2520c0d06caSMauro Carvalho Chehab }
2530c0d06caSMauro Carvalho Chehab 
uvc_status_init(struct uvc_device * dev)2540c0d06caSMauro Carvalho Chehab int uvc_status_init(struct uvc_device *dev)
2550c0d06caSMauro Carvalho Chehab {
2560c0d06caSMauro Carvalho Chehab 	struct usb_host_endpoint *ep = dev->int_ep;
2570c0d06caSMauro Carvalho Chehab 	unsigned int pipe;
2580c0d06caSMauro Carvalho Chehab 	int interval;
2590c0d06caSMauro Carvalho Chehab 
2600c0d06caSMauro Carvalho Chehab 	if (ep == NULL)
2610c0d06caSMauro Carvalho Chehab 		return 0;
2620c0d06caSMauro Carvalho Chehab 
2630c0d06caSMauro Carvalho Chehab 	uvc_input_init(dev);
2640c0d06caSMauro Carvalho Chehab 
265adfd3910SRicardo Ribalda 	dev->status = kzalloc(sizeof(*dev->status), GFP_KERNEL);
266adfd3910SRicardo Ribalda 	if (!dev->status)
2670c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
2680c0d06caSMauro Carvalho Chehab 
2690c0d06caSMauro Carvalho Chehab 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
270adfd3910SRicardo Ribalda 	if (!dev->int_urb) {
2710c0d06caSMauro Carvalho Chehab 		kfree(dev->status);
2720c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
2730c0d06caSMauro Carvalho Chehab 	}
2740c0d06caSMauro Carvalho Chehab 
2750c0d06caSMauro Carvalho Chehab 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
2760c0d06caSMauro Carvalho Chehab 
277699b9a86SLaurent Pinchart 	/*
278699b9a86SLaurent Pinchart 	 * For high-speed interrupt endpoints, the bInterval value is used as
2790c0d06caSMauro Carvalho Chehab 	 * an exponent of two. Some developers forgot about it.
2800c0d06caSMauro Carvalho Chehab 	 */
2810c0d06caSMauro Carvalho Chehab 	interval = ep->desc.bInterval;
2820c0d06caSMauro Carvalho Chehab 	if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
2830c0d06caSMauro Carvalho Chehab 	    (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
2840c0d06caSMauro Carvalho Chehab 		interval = fls(interval) - 1;
2850c0d06caSMauro Carvalho Chehab 
2860c0d06caSMauro Carvalho Chehab 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
287adfd3910SRicardo Ribalda 		dev->status, sizeof(*dev->status), uvc_status_complete,
2880c0d06caSMauro Carvalho Chehab 		dev, interval);
2890c0d06caSMauro Carvalho Chehab 
2900c0d06caSMauro Carvalho Chehab 	return 0;
2910c0d06caSMauro Carvalho Chehab }
2920c0d06caSMauro Carvalho Chehab 
uvc_status_unregister(struct uvc_device * dev)29310e1fdb9SDaniel Axtens void uvc_status_unregister(struct uvc_device *dev)
2940c0d06caSMauro Carvalho Chehab {
2950c0d06caSMauro Carvalho Chehab 	usb_kill_urb(dev->int_urb);
29610e1fdb9SDaniel Axtens 	uvc_input_unregister(dev);
29710e1fdb9SDaniel Axtens }
29810e1fdb9SDaniel Axtens 
uvc_status_cleanup(struct uvc_device * dev)29910e1fdb9SDaniel Axtens void uvc_status_cleanup(struct uvc_device *dev)
30010e1fdb9SDaniel Axtens {
3010c0d06caSMauro Carvalho Chehab 	usb_free_urb(dev->int_urb);
3020c0d06caSMauro Carvalho Chehab 	kfree(dev->status);
3030c0d06caSMauro Carvalho Chehab }
3040c0d06caSMauro Carvalho Chehab 
uvc_status_start(struct uvc_device * dev,gfp_t flags)30517706f56SLaurent Pinchart int uvc_status_start(struct uvc_device *dev, gfp_t flags)
3060c0d06caSMauro Carvalho Chehab {
3070c0d06caSMauro Carvalho Chehab 	if (dev->int_urb == NULL)
3080c0d06caSMauro Carvalho Chehab 		return 0;
3090c0d06caSMauro Carvalho Chehab 
31017706f56SLaurent Pinchart 	return usb_submit_urb(dev->int_urb, flags);
3110c0d06caSMauro Carvalho Chehab }
3120c0d06caSMauro Carvalho Chehab 
uvc_status_stop(struct uvc_device * dev)3130c0d06caSMauro Carvalho Chehab void uvc_status_stop(struct uvc_device *dev)
3140c0d06caSMauro Carvalho Chehab {
315*619d9b71SRicardo Ribalda 	struct uvc_ctrl_work *w = &dev->async_ctrl;
316*619d9b71SRicardo Ribalda 
317*619d9b71SRicardo Ribalda 	/*
318*619d9b71SRicardo Ribalda 	 * Prevent the asynchronous control handler from requeing the URB. The
319*619d9b71SRicardo Ribalda 	 * barrier is needed so the flush_status change is visible to other
320*619d9b71SRicardo Ribalda 	 * CPUs running the asynchronous handler before usb_kill_urb() is
321*619d9b71SRicardo Ribalda 	 * called below.
322*619d9b71SRicardo Ribalda 	 */
323*619d9b71SRicardo Ribalda 	smp_store_release(&dev->flush_status, true);
324*619d9b71SRicardo Ribalda 
325*619d9b71SRicardo Ribalda 	/*
326*619d9b71SRicardo Ribalda 	 * Cancel any pending asynchronous work. If any status event was queued,
327*619d9b71SRicardo Ribalda 	 * process it synchronously.
328*619d9b71SRicardo Ribalda 	 */
329*619d9b71SRicardo Ribalda 	if (cancel_work_sync(&w->work))
330*619d9b71SRicardo Ribalda 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
331*619d9b71SRicardo Ribalda 
332*619d9b71SRicardo Ribalda 	/* Kill the urb. */
3330c0d06caSMauro Carvalho Chehab 	usb_kill_urb(dev->int_urb);
334*619d9b71SRicardo Ribalda 
335*619d9b71SRicardo Ribalda 	/*
336*619d9b71SRicardo Ribalda 	 * The URB completion handler may have queued asynchronous work. This
337*619d9b71SRicardo Ribalda 	 * won't resubmit the URB as flush_status is set, but it needs to be
338*619d9b71SRicardo Ribalda 	 * cancelled before returning or it could then race with a future
339*619d9b71SRicardo Ribalda 	 * uvc_status_start() call.
340*619d9b71SRicardo Ribalda 	 */
341*619d9b71SRicardo Ribalda 	if (cancel_work_sync(&w->work))
342*619d9b71SRicardo Ribalda 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
343*619d9b71SRicardo Ribalda 
344*619d9b71SRicardo Ribalda 	/*
345*619d9b71SRicardo Ribalda 	 * From this point, there are no events on the queue and the status URB
346*619d9b71SRicardo Ribalda 	 * is dead. No events will be queued until uvc_status_start() is called.
347*619d9b71SRicardo Ribalda 	 * The barrier is needed to make sure that flush_status is visible to
348*619d9b71SRicardo Ribalda 	 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
349*619d9b71SRicardo Ribalda 	 * again.
350*619d9b71SRicardo Ribalda 	 */
351*619d9b71SRicardo Ribalda 	smp_store_release(&dev->flush_status, false);
3520c0d06caSMauro Carvalho Chehab }
353