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