GNU Linux-libre 4.19.211-gnu1
[releases.git] / drivers / usb / gadget / function / uvc_video.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *      uvc_video.c  --  USB Video Class Gadget driver
4  *
5  *      Copyright (C) 2009-2010
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/usb/gadget.h>
14 #include <linux/usb/video.h>
15
16 #include <media/v4l2-dev.h>
17
18 #include "uvc.h"
19 #include "uvc_queue.h"
20 #include "uvc_video.h"
21
22 /* --------------------------------------------------------------------------
23  * Video codecs
24  */
25
26 static int
27 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
28                 u8 *data, int len)
29 {
30         data[0] = 2;
31         data[1] = UVC_STREAM_EOH | video->fid;
32
33         if (buf->bytesused - video->queue.buf_used <= len - 2)
34                 data[1] |= UVC_STREAM_EOF;
35
36         return 2;
37 }
38
39 static int
40 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
41                 u8 *data, int len)
42 {
43         struct uvc_video_queue *queue = &video->queue;
44         unsigned int nbytes;
45         void *mem;
46
47         /* Copy video data to the USB buffer. */
48         mem = buf->mem + queue->buf_used;
49         nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
50
51         memcpy(data, mem, nbytes);
52         queue->buf_used += nbytes;
53
54         return nbytes;
55 }
56
57 static void
58 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
59                 struct uvc_buffer *buf)
60 {
61         void *mem = req->buf;
62         int len = video->req_size;
63         int ret;
64
65         /* Add a header at the beginning of the payload. */
66         if (video->payload_size == 0) {
67                 ret = uvc_video_encode_header(video, buf, mem, len);
68                 video->payload_size += ret;
69                 mem += ret;
70                 len -= ret;
71         }
72
73         /* Process video data. */
74         len = min((int)(video->max_payload_size - video->payload_size), len);
75         ret = uvc_video_encode_data(video, buf, mem, len);
76
77         video->payload_size += ret;
78         len -= ret;
79
80         req->length = video->req_size - len;
81         req->zero = video->payload_size == video->max_payload_size;
82
83         if (buf->bytesused == video->queue.buf_used) {
84                 video->queue.buf_used = 0;
85                 buf->state = UVC_BUF_STATE_DONE;
86                 uvcg_queue_next_buffer(&video->queue, buf);
87                 video->fid ^= UVC_STREAM_FID;
88
89                 video->payload_size = 0;
90         }
91
92         if (video->payload_size == video->max_payload_size ||
93             buf->bytesused == video->queue.buf_used)
94                 video->payload_size = 0;
95 }
96
97 static void
98 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
99                 struct uvc_buffer *buf)
100 {
101         void *mem = req->buf;
102         int len = video->req_size;
103         int ret;
104
105         /* Add the header. */
106         ret = uvc_video_encode_header(video, buf, mem, len);
107         mem += ret;
108         len -= ret;
109
110         /* Process video data. */
111         ret = uvc_video_encode_data(video, buf, mem, len);
112         len -= ret;
113
114         req->length = video->req_size - len;
115
116         if (buf->bytesused == video->queue.buf_used) {
117                 video->queue.buf_used = 0;
118                 buf->state = UVC_BUF_STATE_DONE;
119                 uvcg_queue_next_buffer(&video->queue, buf);
120                 video->fid ^= UVC_STREAM_FID;
121         }
122 }
123
124 /* --------------------------------------------------------------------------
125  * Request handling
126  */
127
128 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
129 {
130         int ret;
131
132         ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
133         if (ret < 0) {
134                 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
135                 /* Isochronous endpoints can't be halted. */
136                 if (usb_endpoint_xfer_bulk(video->ep->desc))
137                         usb_ep_set_halt(video->ep);
138         }
139
140         return ret;
141 }
142
143 /*
144  * I somehow feel that synchronisation won't be easy to achieve here. We have
145  * three events that control USB requests submission:
146  *
147  * - USB request completion: the completion handler will resubmit the request
148  *   if a video buffer is available.
149  *
150  * - USB interface setting selection: in response to a SET_INTERFACE request,
151  *   the handler will start streaming if a video buffer is available and if
152  *   video is not currently streaming.
153  *
154  * - V4L2 buffer queueing: the driver will start streaming if video is not
155  *   currently streaming.
156  *
157  * Race conditions between those 3 events might lead to deadlocks or other
158  * nasty side effects.
159  *
160  * The "video currently streaming" condition can't be detected by the irqqueue
161  * being empty, as a request can still be in flight. A separate "queue paused"
162  * flag is thus needed.
163  *
164  * The paused flag will be set when we try to retrieve the irqqueue head if the
165  * queue is empty, and cleared when we queue a buffer.
166  *
167  * The USB request completion handler will get the buffer at the irqqueue head
168  * under protection of the queue spinlock. If the queue is empty, the streaming
169  * paused flag will be set. Right after releasing the spinlock a userspace
170  * application can queue a buffer. The flag will then cleared, and the ioctl
171  * handler will restart the video stream.
172  */
173 static void
174 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
175 {
176         struct uvc_video *video = req->context;
177         struct uvc_video_queue *queue = &video->queue;
178         struct uvc_buffer *buf;
179         unsigned long flags;
180         int ret;
181
182         switch (req->status) {
183         case 0:
184                 break;
185
186         case -ESHUTDOWN:        /* disconnect from host. */
187                 printk(KERN_DEBUG "VS request cancelled.\n");
188                 uvcg_queue_cancel(queue, 1);
189                 goto requeue;
190
191         default:
192                 printk(KERN_INFO "VS request completed with status %d.\n",
193                         req->status);
194                 uvcg_queue_cancel(queue, 0);
195                 goto requeue;
196         }
197
198         spin_lock_irqsave(&video->queue.irqlock, flags);
199         buf = uvcg_queue_head(&video->queue);
200         if (buf == NULL) {
201                 spin_unlock_irqrestore(&video->queue.irqlock, flags);
202                 goto requeue;
203         }
204
205         video->encode(req, video, buf);
206
207         ret = uvcg_video_ep_queue(video, req);
208         spin_unlock_irqrestore(&video->queue.irqlock, flags);
209
210         if (ret < 0) {
211                 uvcg_queue_cancel(queue, 0);
212                 goto requeue;
213         }
214
215         return;
216
217 requeue:
218         spin_lock_irqsave(&video->req_lock, flags);
219         list_add_tail(&req->list, &video->req_free);
220         spin_unlock_irqrestore(&video->req_lock, flags);
221 }
222
223 static int
224 uvc_video_free_requests(struct uvc_video *video)
225 {
226         unsigned int i;
227
228         for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
229                 if (video->req[i]) {
230                         usb_ep_free_request(video->ep, video->req[i]);
231                         video->req[i] = NULL;
232                 }
233
234                 if (video->req_buffer[i]) {
235                         kfree(video->req_buffer[i]);
236                         video->req_buffer[i] = NULL;
237                 }
238         }
239
240         INIT_LIST_HEAD(&video->req_free);
241         video->req_size = 0;
242         return 0;
243 }
244
245 static int
246 uvc_video_alloc_requests(struct uvc_video *video)
247 {
248         unsigned int req_size;
249         unsigned int i;
250         int ret = -ENOMEM;
251
252         BUG_ON(video->req_size);
253
254         req_size = video->ep->maxpacket
255                  * max_t(unsigned int, video->ep->maxburst, 1)
256                  * (video->ep->mult);
257
258         for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
259                 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
260                 if (video->req_buffer[i] == NULL)
261                         goto error;
262
263                 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
264                 if (video->req[i] == NULL)
265                         goto error;
266
267                 video->req[i]->buf = video->req_buffer[i];
268                 video->req[i]->length = 0;
269                 video->req[i]->complete = uvc_video_complete;
270                 video->req[i]->context = video;
271
272                 list_add_tail(&video->req[i]->list, &video->req_free);
273         }
274
275         video->req_size = req_size;
276
277         return 0;
278
279 error:
280         uvc_video_free_requests(video);
281         return ret;
282 }
283
284 /* --------------------------------------------------------------------------
285  * Video streaming
286  */
287
288 /*
289  * uvcg_video_pump - Pump video data into the USB requests
290  *
291  * This function fills the available USB requests (listed in req_free) with
292  * video data from the queued buffers.
293  */
294 int uvcg_video_pump(struct uvc_video *video)
295 {
296         struct uvc_video_queue *queue = &video->queue;
297         struct usb_request *req;
298         struct uvc_buffer *buf;
299         unsigned long flags;
300         int ret;
301
302         /* FIXME TODO Race between uvcg_video_pump and requests completion
303          * handler ???
304          */
305
306         while (1) {
307                 /* Retrieve the first available USB request, protected by the
308                  * request lock.
309                  */
310                 spin_lock_irqsave(&video->req_lock, flags);
311                 if (list_empty(&video->req_free)) {
312                         spin_unlock_irqrestore(&video->req_lock, flags);
313                         return 0;
314                 }
315                 req = list_first_entry(&video->req_free, struct usb_request,
316                                         list);
317                 list_del(&req->list);
318                 spin_unlock_irqrestore(&video->req_lock, flags);
319
320                 /* Retrieve the first available video buffer and fill the
321                  * request, protected by the video queue irqlock.
322                  */
323                 spin_lock_irqsave(&queue->irqlock, flags);
324                 buf = uvcg_queue_head(queue);
325                 if (buf == NULL) {
326                         spin_unlock_irqrestore(&queue->irqlock, flags);
327                         break;
328                 }
329
330                 video->encode(req, video, buf);
331
332                 /* Queue the USB request */
333                 ret = uvcg_video_ep_queue(video, req);
334                 spin_unlock_irqrestore(&queue->irqlock, flags);
335
336                 if (ret < 0) {
337                         uvcg_queue_cancel(queue, 0);
338                         break;
339                 }
340         }
341
342         spin_lock_irqsave(&video->req_lock, flags);
343         list_add_tail(&req->list, &video->req_free);
344         spin_unlock_irqrestore(&video->req_lock, flags);
345         return 0;
346 }
347
348 /*
349  * Enable or disable the video stream.
350  */
351 int uvcg_video_enable(struct uvc_video *video, int enable)
352 {
353         unsigned int i;
354         int ret;
355
356         if (video->ep == NULL) {
357                 printk(KERN_INFO "Video enable failed, device is "
358                         "uninitialized.\n");
359                 return -ENODEV;
360         }
361
362         if (!enable) {
363                 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
364                         if (video->req[i])
365                                 usb_ep_dequeue(video->ep, video->req[i]);
366
367                 uvc_video_free_requests(video);
368                 uvcg_queue_enable(&video->queue, 0);
369                 return 0;
370         }
371
372         if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
373                 return ret;
374
375         if ((ret = uvc_video_alloc_requests(video)) < 0)
376                 return ret;
377
378         if (video->max_payload_size) {
379                 video->encode = uvc_video_encode_bulk;
380                 video->payload_size = 0;
381         } else
382                 video->encode = uvc_video_encode_isoc;
383
384         return uvcg_video_pump(video);
385 }
386
387 /*
388  * Initialize the UVC video stream.
389  */
390 int uvcg_video_init(struct uvc_video *video)
391 {
392         INIT_LIST_HEAD(&video->req_free);
393         spin_lock_init(&video->req_lock);
394
395         video->fcc = V4L2_PIX_FMT_YUYV;
396         video->bpp = 16;
397         video->width = 320;
398         video->height = 240;
399         video->imagesize = 320 * 240 * 2;
400
401         /* Initialize the video buffers queue. */
402         uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
403                         &video->mutex);
404         return 0;
405 }
406