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
23static 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
42static 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: dev->udev, buf: dev->input_phys, size: sizeof(dev->input_phys));
55 strlcat(p: dev->input_phys, q: "/button", avail: sizeof(dev->input_phys));
56
57 input->name = dev->name;
58 input->phys = dev->input_phys;
59 usb_to_input_id(dev: dev->udev, id: &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 ret = input_register_device(input);
66 if (ret < 0)
67 goto error;
68
69 dev->input = input;
70 return 0;
71
72error:
73 input_free_device(dev: input);
74 return ret;
75}
76
77static void uvc_input_unregister(struct uvc_device *dev)
78{
79 if (dev->input)
80 input_unregister_device(dev->input);
81}
82
83static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,
84 int value)
85{
86 if (dev->input) {
87 input_report_key(dev: dev->input, code, value);
88 input_sync(dev: dev->input);
89 }
90}
91
92#else
93#define uvc_input_init(dev)
94#define uvc_input_unregister(dev)
95#define uvc_input_report_key(dev, code, value)
96#endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
97
98/* --------------------------------------------------------------------------
99 * Status interrupt endpoint
100 */
101static void uvc_event_streaming(struct uvc_device *dev,
102 struct uvc_status *status, int len)
103{
104 if (len <= offsetof(struct uvc_status, bEvent)) {
105 uvc_dbg(dev, STATUS,
106 "Invalid streaming status event received\n");
107 return;
108 }
109
110 if (status->bEvent == 0) {
111 if (len <= offsetof(struct uvc_status, streaming))
112 return;
113
114 uvc_dbg(dev, STATUS, "Button (intf %u) %s len %d\n",
115 status->bOriginator,
116 status->streaming.button ? "pressed" : "released", len);
117 uvc_input_report_key(dev, KEY_CAMERA, value: status->streaming.button);
118 } else {
119 uvc_dbg(dev, STATUS, "Stream %u error event %02x len %d\n",
120 status->bOriginator, status->bEvent, len);
121 }
122}
123
124#define UVC_CTRL_VALUE_CHANGE 0
125#define UVC_CTRL_INFO_CHANGE 1
126#define UVC_CTRL_FAILURE_CHANGE 2
127#define UVC_CTRL_MIN_CHANGE 3
128#define UVC_CTRL_MAX_CHANGE 4
129
130static struct uvc_control *uvc_event_entity_find_ctrl(struct uvc_entity *entity,
131 u8 selector)
132{
133 struct uvc_control *ctrl;
134 unsigned int i;
135
136 for (i = 0, ctrl = entity->controls; i < entity->ncontrols; i++, ctrl++)
137 if (ctrl->info.selector == selector)
138 return ctrl;
139
140 return NULL;
141}
142
143static struct uvc_control *uvc_event_find_ctrl(struct uvc_device *dev,
144 const struct uvc_status *status,
145 struct uvc_video_chain **chain)
146{
147 list_for_each_entry((*chain), &dev->chains, list) {
148 struct uvc_entity *entity;
149 struct uvc_control *ctrl;
150
151 list_for_each_entry(entity, &(*chain)->entities, chain) {
152 if (entity->id != status->bOriginator)
153 continue;
154
155 ctrl = uvc_event_entity_find_ctrl(entity,
156 selector: status->control.bSelector);
157 if (ctrl)
158 return ctrl;
159 }
160 }
161
162 return NULL;
163}
164
165static bool uvc_event_control(struct urb *urb,
166 const struct uvc_status *status, int len)
167{
168 static const char *attrs[] = { "value", "info", "failure", "min", "max" };
169 struct uvc_device *dev = urb->context;
170 struct uvc_video_chain *chain;
171 struct uvc_control *ctrl;
172
173 if (len < 6 || status->bEvent != 0 ||
174 status->control.bAttribute >= ARRAY_SIZE(attrs)) {
175 uvc_dbg(dev, STATUS, "Invalid control status event received\n");
176 return false;
177 }
178
179 uvc_dbg(dev, STATUS, "Control %u/%u %s change len %d\n",
180 status->bOriginator, status->control.bSelector,
181 attrs[status->control.bAttribute], len);
182
183 /* Find the control. */
184 ctrl = uvc_event_find_ctrl(dev, status, chain: &chain);
185 if (!ctrl)
186 return false;
187
188 switch (status->control.bAttribute) {
189 case UVC_CTRL_VALUE_CHANGE:
190 return uvc_ctrl_status_event_async(urb, chain, ctrl,
191 data: status->control.bValue);
192
193 case UVC_CTRL_INFO_CHANGE:
194 case UVC_CTRL_FAILURE_CHANGE:
195 case UVC_CTRL_MIN_CHANGE:
196 case UVC_CTRL_MAX_CHANGE:
197 break;
198 }
199
200 return false;
201}
202
203static void uvc_status_complete(struct urb *urb)
204{
205 struct uvc_device *dev = urb->context;
206 int len, ret;
207
208 switch (urb->status) {
209 case 0:
210 break;
211
212 case -ENOENT: /* usb_kill_urb() called. */
213 case -ECONNRESET: /* usb_unlink_urb() called. */
214 case -ESHUTDOWN: /* The endpoint is being disabled. */
215 case -EPROTO: /* Device is disconnected (reported by some host controllers). */
216 return;
217
218 default:
219 dev_warn(&dev->intf->dev,
220 "Non-zero status (%d) in status completion handler.\n",
221 urb->status);
222 return;
223 }
224
225 len = urb->actual_length;
226 if (len > 0) {
227 switch (dev->status->bStatusType & 0x0f) {
228 case UVC_STATUS_TYPE_CONTROL: {
229 if (uvc_event_control(urb, status: dev->status, len))
230 /* The URB will be resubmitted in work context. */
231 return;
232 break;
233 }
234
235 case UVC_STATUS_TYPE_STREAMING: {
236 uvc_event_streaming(dev, status: dev->status, len);
237 break;
238 }
239
240 default:
241 uvc_dbg(dev, STATUS, "Unknown status event type %u\n",
242 dev->status->bStatusType);
243 break;
244 }
245 }
246
247 /* Resubmit the URB. */
248 urb->interval = dev->int_ep->desc.bInterval;
249 ret = usb_submit_urb(urb, GFP_ATOMIC);
250 if (ret < 0)
251 dev_err(&dev->intf->dev,
252 "Failed to resubmit status URB (%d).\n", ret);
253}
254
255int uvc_status_init(struct uvc_device *dev)
256{
257 struct usb_host_endpoint *ep = dev->int_ep;
258 unsigned int pipe;
259 int interval;
260
261 mutex_init(&dev->status_lock);
262
263 if (ep == NULL)
264 return 0;
265
266 dev->status = kzalloc(sizeof(*dev->status), GFP_KERNEL);
267 if (!dev->status)
268 return -ENOMEM;
269
270 dev->int_urb = usb_alloc_urb(iso_packets: 0, GFP_KERNEL);
271 if (!dev->int_urb) {
272 kfree(objp: dev->status);
273 dev->status = NULL;
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(x: interval) - 1;
287
288 usb_fill_int_urb(urb: dev->int_urb, dev: dev->udev, pipe,
289 transfer_buffer: dev->status, buffer_length: sizeof(*dev->status), complete_fn: uvc_status_complete,
290 context: dev, interval);
291
292 uvc_input_init(dev);
293
294 return 0;
295}
296
297void uvc_status_unregister(struct uvc_device *dev)
298{
299 if (!dev->status)
300 return;
301
302 uvc_status_suspend(dev);
303 uvc_input_unregister(dev);
304}
305
306void uvc_status_cleanup(struct uvc_device *dev)
307{
308 usb_free_urb(urb: dev->int_urb);
309 kfree(objp: dev->status);
310}
311
312static int uvc_status_start(struct uvc_device *dev, gfp_t flags)
313{
314 lockdep_assert_held(&dev->status_lock);
315
316 if (!dev->int_urb)
317 return 0;
318
319 return usb_submit_urb(urb: dev->int_urb, mem_flags: flags);
320}
321
322static void uvc_status_stop(struct uvc_device *dev)
323{
324 struct uvc_ctrl_work *w = &dev->async_ctrl;
325
326 lockdep_assert_held(&dev->status_lock);
327
328 if (!dev->int_urb)
329 return;
330
331 /*
332 * Prevent the asynchronous control handler from requeing the URB. The
333 * barrier is needed so the flush_status change is visible to other
334 * CPUs running the asynchronous handler before usb_kill_urb() is
335 * called below.
336 */
337 smp_store_release(&dev->flush_status, true);
338
339 /*
340 * Cancel any pending asynchronous work. If any status event was queued,
341 * process it synchronously.
342 */
343 if (cancel_work_sync(work: &w->work))
344 uvc_ctrl_status_event(chain: w->chain, ctrl: w->ctrl, data: w->data);
345
346 /* Kill the urb. */
347 usb_kill_urb(urb: dev->int_urb);
348
349 /*
350 * The URB completion handler may have queued asynchronous work. This
351 * won't resubmit the URB as flush_status is set, but it needs to be
352 * cancelled before returning or it could then race with a future
353 * uvc_status_start() call.
354 */
355 if (cancel_work_sync(work: &w->work))
356 uvc_ctrl_status_event(chain: w->chain, ctrl: w->ctrl, data: w->data);
357
358 /*
359 * From this point, there are no events on the queue and the status URB
360 * is dead. No events will be queued until uvc_status_start() is called.
361 * The barrier is needed to make sure that flush_status is visible to
362 * uvc_ctrl_status_event_work() when uvc_status_start() will be called
363 * again.
364 */
365 smp_store_release(&dev->flush_status, false);
366}
367
368int uvc_status_resume(struct uvc_device *dev)
369{
370 guard(mutex)(T: &dev->status_lock);
371
372 if (dev->status_users)
373 return uvc_status_start(dev, GFP_NOIO);
374
375 return 0;
376}
377
378void uvc_status_suspend(struct uvc_device *dev)
379{
380 guard(mutex)(T: &dev->status_lock);
381
382 if (dev->status_users)
383 uvc_status_stop(dev);
384}
385
386int uvc_status_get(struct uvc_device *dev)
387{
388 int ret;
389
390 guard(mutex)(T: &dev->status_lock);
391
392 if (!dev->status_users) {
393 ret = uvc_status_start(dev, GFP_KERNEL);
394 if (ret)
395 return ret;
396 }
397
398 dev->status_users++;
399
400 return 0;
401}
402
403void uvc_status_put(struct uvc_device *dev)
404{
405 guard(mutex)(T: &dev->status_lock);
406
407 if (dev->status_users == 1)
408 uvc_status_stop(dev);
409 WARN_ON(!dev->status_users);
410 if (dev->status_users)
411 dev->status_users--;
412}
413

source code of linux/drivers/media/usb/uvc/uvc_status.c