xref: /linux/drivers/media/usb/uvc/uvc_status.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      uvc_status.c  --  USB Video Class driver - Status endpoint
4  *
5  *      Copyright (C) 2005-2009
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <asm/barrier.h>
10 #include <linux/kernel.h>
11 #include <linux/input.h>
12 #include <linux/slab.h>
13 #include <linux/usb.h>
14 #include <linux/usb/input.h>
15 
16 #include "uvcvideo.h"
17 
18 /* --------------------------------------------------------------------------
19  * Input device
20  */
21 #ifdef CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV
22 
23 static bool uvc_input_has_button(struct uvc_device *dev)
24 {
25 	struct uvc_streaming *stream;
26 
27 	/*
28 	 * The device has button events if both bTriggerSupport and
29 	 * bTriggerUsage are one. Otherwise the camera button does not
30 	 * exist or is handled automatically by the camera without host
31 	 * driver or client application intervention.
32 	 */
33 	list_for_each_entry(stream, &dev->streams, list) {
34 		if (stream->header.bTriggerSupport == 1 &&
35 		    stream->header.bTriggerUsage == 1)
36 			return true;
37 	}
38 
39 	return false;
40 }
41 
42 static int uvc_input_init(struct uvc_device *dev)
43 {
44 	struct input_dev *input;
45 	int ret;
46 
47 	if (!uvc_input_has_button(dev))
48 		return 0;
49 
50 	input = input_allocate_device();
51 	if (input == NULL)
52 		return -ENOMEM;
53 
54 	usb_make_path(dev->udev, dev->input_phys, sizeof(dev->input_phys));
55 	strlcat(dev->input_phys, "/button", sizeof(dev->input_phys));
56 
57 	input->name = dev->name;
58 	input->phys = dev->input_phys;
59 	usb_to_input_id(dev->udev, &input->id);
60 	input->dev.parent = &dev->intf->dev;
61 
62 	__set_bit(EV_KEY, input->evbit);
63 	__set_bit(KEY_CAMERA, input->keybit);
64 
65 	if ((ret = input_register_device(input)) < 0)
66 		goto error;
67 
68 	dev->input = input;
69 	return 0;
70 
71 error:
72 	input_free_device(input);
73 	return ret;
74 }
75 
76 static void uvc_input_unregister(struct uvc_device *dev)
77 {
78 	if (dev->input)
79 		input_unregister_device(dev->input);
80 }
81 
82 static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
83 	int value)
84 {
85 	if (dev->input) {
86 		input_report_key(dev->input, code, value);
87 		input_sync(dev->input);
88 	}
89 }
90 
91 #else
92 #define uvc_input_init(dev)
93 #define uvc_input_unregister(dev)
94 #define uvc_input_report_key(dev, code, value)
95 #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
96 
97 /* --------------------------------------------------------------------------
98  * Status interrupt endpoint
99  */
100 static void uvc_event_streaming(struct uvc_device *dev,
101 				struct uvc_status *status, int len)
102 {
103 	if (len <= offsetof(struct uvc_status, bEvent)) {
104 		uvc_dbg(dev, STATUS,
105 			"Invalid streaming status event received\n");
106 		return;
107 	}
108 
109 	if (status->bEvent == 0) {
110 		if (len <= offsetof(struct uvc_status, streaming))
111 			return;
112 
113 		uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
114 			status->bOriginator,
115 			status->streaming.button ? "pressed" : "released", len);
116 		uvc_input_report_key(dev, KEY_CAMERA, status->streaming.button);
117 	} else {
118 		uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
119 			status->bOriginator, status->bEvent, len);
120 	}
121 }
122 
123 #define UVC_CTRL_VALUE_CHANGE	0
124 #define UVC_CTRL_INFO_CHANGE	1
125 #define UVC_CTRL_FAILURE_CHANGE	2
126 #define UVC_CTRL_MIN_CHANGE	3
127 #define UVC_CTRL_MAX_CHANGE	4
128 
129 static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
130 						      u8 selector)
131 {
132 	struct uvc_control *ctrl;
133 	unsigned int i;
134 
135 	for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
136 		if (ctrl->info.selector == selector)
137 			return ctrl;
138 
139 	return NULL;
140 }
141 
142 static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
143 					const struct uvc_status *status,
144 					struct uvc_video_chain **chain)
145 {
146 	list_for_each_entry((*chain), &dev->chains, list) {
147 		struct uvc_entity *entity;
148 		struct uvc_control *ctrl;
149 
150 		list_for_each_entry(entity, &(*chain)->entities, chain) {
151 			if (entity->id != status->bOriginator)
152 				continue;
153 
154 			ctrl = uvc_event_entity_find_ctrl(entity,
155 						     status->control.bSelector);
156 			if (ctrl)
157 				return ctrl;
158 		}
159 	}
160 
161 	return NULL;
162 }
163 
164 static bool uvc_event_control(struct urb *urb,
165 			      const struct uvc_status *status, int len)
166 {
167 	static const char *attrs[] = { "value", "info", "failure", "min", "max" };
168 	struct uvc_device *dev = urb->context;
169 	struct uvc_video_chain *chain;
170 	struct uvc_control *ctrl;
171 
172 	if (len < 6 || status->bEvent != 0 ||
173 	    status->control.bAttribute >= ARRAY_SIZE(attrs)) {
174 		uvc_dbg(dev, STATUS, "Invalid control status event received\n");
175 		return false;
176 	}
177 
178 	uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
179 		status->bOriginator, status->control.bSelector,
180 		attrs[status->control.bAttribute], len);
181 
182 	/* Find the control. */
183 	ctrl = uvc_event_find_ctrl(dev, status, &chain);
184 	if (!ctrl)
185 		return false;
186 
187 	switch (status->control.bAttribute) {
188 	case UVC_CTRL_VALUE_CHANGE:
189 		return uvc_ctrl_status_event_async(urb, chain, ctrl,
190 						   status->control.bValue);
191 
192 	case UVC_CTRL_INFO_CHANGE:
193 	case UVC_CTRL_FAILURE_CHANGE:
194 	case UVC_CTRL_MIN_CHANGE:
195 	case UVC_CTRL_MAX_CHANGE:
196 		break;
197 	}
198 
199 	return false;
200 }
201 
202 static void uvc_status_complete(struct urb *urb)
203 {
204 	struct uvc_device *dev = urb->context;
205 	int len, ret;
206 
207 	switch (urb->status) {
208 	case 0:
209 		break;
210 
211 	case -ENOENT:		/* usb_kill_urb() called. */
212 	case -ECONNRESET:	/* usb_unlink_urb() called. */
213 	case -ESHUTDOWN:	/* The endpoint is being disabled. */
214 	case -EPROTO:		/* Device is disconnected (reported by some host controllers). */
215 		return;
216 
217 	default:
218 		dev_warn(&dev->udev->dev,
219 			 "Non-zero status (%d) in status completion handler.\n",
220 			 urb->status);
221 		return;
222 	}
223 
224 	len = urb->actual_length;
225 	if (len > 0) {
226 		switch (dev->status->bStatusType & 0x0f) {
227 		case UVC_STATUS_TYPE_CONTROL: {
228 			if (uvc_event_control(urb, dev->status, len))
229 				/* The URB will be resubmitted in work context. */
230 				return;
231 			break;
232 		}
233 
234 		case UVC_STATUS_TYPE_STREAMING: {
235 			uvc_event_streaming(dev, dev->status, len);
236 			break;
237 		}
238 
239 		default:
240 			uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
241 				dev->status->bStatusType);
242 			break;
243 		}
244 	}
245 
246 	/* Resubmit the URB. */
247 	urb->interval = dev->int_ep->desc.bInterval;
248 	ret = usb_submit_urb(urb, GFP_ATOMIC);
249 	if (ret < 0)
250 		dev_err(&dev->udev->dev,
251 			"Failed to resubmit status URB (%d).\n", ret);
252 }
253 
254 int uvc_status_init(struct uvc_device *dev)
255 {
256 	struct usb_host_endpoint *ep = dev->int_ep;
257 	unsigned int pipe;
258 	int interval;
259 
260 	mutex_init(&dev->status_lock);
261 
262 	if (ep == NULL)
263 		return 0;
264 
265 	uvc_input_init(dev);
266 
267 	dev->status = kzalloc(sizeof(*dev->status), GFP_KERNEL);
268 	if (!dev->status)
269 		return -ENOMEM;
270 
271 	dev->int_urb = usb_alloc_urb(0, GFP_KERNEL);
272 	if (!dev->int_urb) {
273 		kfree(dev->status);
274 		return -ENOMEM;
275 	}
276 
277 	pipe = usb_rcvintpipe(dev->udev, ep->desc.bEndpointAddress);
278 
279 	/*
280 	 * For high-speed interrupt endpoints, the bInterval value is used as
281 	 * an exponent of two. Some developers forgot about it.
282 	 */
283 	interval = ep->desc.bInterval;
284 	if (interval > 16 && dev->udev->speed == USB_SPEED_HIGH &&
285 	    (dev->quirks & UVC_QUIRK_STATUS_INTERVAL))
286 		interval = fls(interval) - 1;
287 
288 	usb_fill_int_urb(dev->int_urb, dev->udev, pipe,
289 		dev->status, sizeof(*dev->status), uvc_status_complete,
290 		dev, interval);
291 
292 	return 0;
293 }
294 
295 void uvc_status_unregister(struct uvc_device *dev)
296 {
297 	uvc_status_suspend(dev);
298 	uvc_input_unregister(dev);
299 }
300 
301 void uvc_status_cleanup(struct uvc_device *dev)
302 {
303 	usb_free_urb(dev->int_urb);
304 	kfree(dev->status);
305 }
306 
307 static int uvc_status_start(struct uvc_device *dev, gfp_t flags)
308 {
309 	lockdep_assert_held(&dev->status_lock);
310 
311 	if (!dev->int_urb)
312 		return 0;
313 
314 	return usb_submit_urb(dev->int_urb, flags);
315 }
316 
317 static void uvc_status_stop(struct uvc_device *dev)
318 {
319 	struct uvc_ctrl_work *w = &dev->async_ctrl;
320 
321 	lockdep_assert_held(&dev->status_lock);
322 
323 	if (!dev->int_urb)
324 		return;
325 
326 	/*
327 	 * Prevent the asynchronous control handler from requeing the URB. The
328 	 * barrier is needed so the flush_status change is visible to other
329 	 * CPUs running the asynchronous handler before usb_kill_urb() is
330 	 * called below.
331 	 */
332 	smp_store_release(&dev->flush_status, true);
333 
334 	/*
335 	 * Cancel any pending asynchronous work. If any status event was queued,
336 	 * process it synchronously.
337 	 */
338 	if (cancel_work_sync(&w->work))
339 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
340 
341 	/* Kill the urb. */
342 	usb_kill_urb(dev->int_urb);
343 
344 	/*
345 	 * The URB completion handler may have queued asynchronous work. This
346 	 * won't resubmit the URB as flush_status is set, but it needs to be
347 	 * cancelled before returning or it could then race with a future
348 	 * uvc_status_start() call.
349 	 */
350 	if (cancel_work_sync(&w->work))
351 		uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
352 
353 	/*
354 	 * From this point, there are no events on the queue and the status URB
355 	 * is dead. No events will be queued until uvc_status_start() is called.
356 	 * The barrier is needed to make sure that flush_status is visible to
357 	 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
358 	 * again.
359 	 */
360 	smp_store_release(&dev->flush_status, false);
361 }
362 
363 int uvc_status_resume(struct uvc_device *dev)
364 {
365 	guard(mutex)(&dev->status_lock);
366 
367 	if (dev->status_users)
368 		return uvc_status_start(dev, GFP_NOIO);
369 
370 	return 0;
371 }
372 
373 void uvc_status_suspend(struct uvc_device *dev)
374 {
375 	guard(mutex)(&dev->status_lock);
376 
377 	if (dev->status_users)
378 		uvc_status_stop(dev);
379 }
380 
381 int uvc_status_get(struct uvc_device *dev)
382 {
383 	int ret;
384 
385 	guard(mutex)(&dev->status_lock);
386 
387 	if (!dev->status_users) {
388 		ret = uvc_status_start(dev, GFP_KERNEL);
389 		if (ret)
390 			return ret;
391 	}
392 
393 	dev->status_users++;
394 
395 	return 0;
396 }
397 
398 void uvc_status_put(struct uvc_device *dev)
399 {
400 	guard(mutex)(&dev->status_lock);
401 
402 	if (dev->status_users == 1)
403 		uvc_status_stop(dev);
404 	WARN_ON(!dev->status_users);
405 	if (dev->status_users)
406 		dev->status_users--;
407 }
408