blob: d6bab12b0b47d983b28fe5dfa1755e5f555c2d16 [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
2 * uvc_video.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020011 */
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/errno.h>
16#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +030018#include <linux/usb/video.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020019
20#include <media/v4l2-dev.h>
21
22#include "uvc.h"
23#include "uvc_queue.h"
Lad, Prabhakar70685712015-02-05 13:02:18 +000024#include "uvc_video.h"
Laurent Pinchartcdda4792010-05-02 20:57:41 +020025
26/* --------------------------------------------------------------------------
27 * Video codecs
28 */
29
30static int
31uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
32 u8 *data, int len)
33{
34 data[0] = 2;
35 data[1] = UVC_STREAM_EOH | video->fid;
36
Bhupesh Sharmad6925222013-03-28 15:11:52 +053037 if (buf->bytesused - video->queue.buf_used <= len - 2)
Laurent Pinchartcdda4792010-05-02 20:57:41 +020038 data[1] |= UVC_STREAM_EOF;
39
40 return 2;
41}
42
43static int
44uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
45 u8 *data, int len)
46{
47 struct uvc_video_queue *queue = &video->queue;
48 unsigned int nbytes;
49 void *mem;
50
51 /* Copy video data to the USB buffer. */
Bhupesh Sharmad6925222013-03-28 15:11:52 +053052 mem = buf->mem + queue->buf_used;
53 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020054
55 memcpy(data, mem, nbytes);
56 queue->buf_used += nbytes;
57
58 return nbytes;
59}
60
61static void
62uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
63 struct uvc_buffer *buf)
64{
65 void *mem = req->buf;
66 int len = video->req_size;
67 int ret;
68
69 /* Add a header at the beginning of the payload. */
70 if (video->payload_size == 0) {
71 ret = uvc_video_encode_header(video, buf, mem, len);
72 video->payload_size += ret;
73 mem += ret;
74 len -= ret;
75 }
76
77 /* Process video data. */
78 len = min((int)(video->max_payload_size - video->payload_size), len);
79 ret = uvc_video_encode_data(video, buf, mem, len);
80
81 video->payload_size += ret;
82 len -= ret;
83
84 req->length = video->req_size - len;
85 req->zero = video->payload_size == video->max_payload_size;
86
Bhupesh Sharmad6925222013-03-28 15:11:52 +053087 if (buf->bytesused == video->queue.buf_used) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +020088 video->queue.buf_used = 0;
89 buf->state = UVC_BUF_STATE_DONE;
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +030090 uvcg_queue_next_buffer(&video->queue, buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +020091 video->fid ^= UVC_STREAM_FID;
92
93 video->payload_size = 0;
94 }
95
96 if (video->payload_size == video->max_payload_size ||
Bhupesh Sharmad6925222013-03-28 15:11:52 +053097 buf->bytesused == video->queue.buf_used)
Laurent Pinchartcdda4792010-05-02 20:57:41 +020098 video->payload_size = 0;
99}
100
101static void
102uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
103 struct uvc_buffer *buf)
104{
105 void *mem = req->buf;
106 int len = video->req_size;
107 int ret;
108
109 /* Add the header. */
110 ret = uvc_video_encode_header(video, buf, mem, len);
111 mem += ret;
112 len -= ret;
113
114 /* Process video data. */
115 ret = uvc_video_encode_data(video, buf, mem, len);
116 len -= ret;
117
118 req->length = video->req_size - len;
119
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530120 if (buf->bytesused == video->queue.buf_used) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200121 video->queue.buf_used = 0;
122 buf->state = UVC_BUF_STATE_DONE;
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300123 uvcg_queue_next_buffer(&video->queue, buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200124 video->fid ^= UVC_STREAM_FID;
125 }
126}
127
128/* --------------------------------------------------------------------------
129 * Request handling
130 */
131
Laurent Pinchart28ff0fa2018-08-10 15:42:03 +0300132static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
133{
134 int ret;
135
136 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
137 if (ret < 0) {
138 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
Laurent Pinchart1f3745a2018-08-10 15:44:57 +0300139 /* Isochronous endpoints can't be halted. */
140 if (usb_endpoint_xfer_bulk(video->ep->desc))
141 usb_ep_set_halt(video->ep);
Laurent Pinchart28ff0fa2018-08-10 15:42:03 +0300142 }
143
144 return ret;
145}
146
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200147/*
148 * I somehow feel that synchronisation won't be easy to achieve here. We have
149 * three events that control USB requests submission:
150 *
151 * - USB request completion: the completion handler will resubmit the request
152 * if a video buffer is available.
153 *
154 * - USB interface setting selection: in response to a SET_INTERFACE request,
155 * the handler will start streaming if a video buffer is available and if
156 * video is not currently streaming.
157 *
158 * - V4L2 buffer queueing: the driver will start streaming if video is not
159 * currently streaming.
160 *
161 * Race conditions between those 3 events might lead to deadlocks or other
162 * nasty side effects.
163 *
164 * The "video currently streaming" condition can't be detected by the irqqueue
165 * being empty, as a request can still be in flight. A separate "queue paused"
166 * flag is thus needed.
167 *
168 * The paused flag will be set when we try to retrieve the irqqueue head if the
169 * queue is empty, and cleared when we queue a buffer.
170 *
171 * The USB request completion handler will get the buffer at the irqqueue head
172 * under protection of the queue spinlock. If the queue is empty, the streaming
173 * paused flag will be set. Right after releasing the spinlock a userspace
174 * application can queue a buffer. The flag will then cleared, and the ioctl
175 * handler will restart the video stream.
176 */
177static void
178uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
179{
180 struct uvc_video *video = req->context;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530181 struct uvc_video_queue *queue = &video->queue;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200182 struct uvc_buffer *buf;
183 unsigned long flags;
184 int ret;
185
186 switch (req->status) {
187 case 0:
188 break;
189
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530190 case -ESHUTDOWN: /* disconnect from host. */
Michael Grzeschik6bc17372014-08-21 16:54:43 +0200191 printk(KERN_DEBUG "VS request cancelled.\n");
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300192 uvcg_queue_cancel(queue, 1);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200193 goto requeue;
194
195 default:
196 printk(KERN_INFO "VS request completed with status %d.\n",
197 req->status);
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300198 uvcg_queue_cancel(queue, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200199 goto requeue;
200 }
201
202 spin_lock_irqsave(&video->queue.irqlock, flags);
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300203 buf = uvcg_queue_head(&video->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200204 if (buf == NULL) {
205 spin_unlock_irqrestore(&video->queue.irqlock, flags);
206 goto requeue;
207 }
208
209 video->encode(req, video, buf);
210
Laurent Pinchart28ff0fa2018-08-10 15:42:03 +0300211 ret = uvcg_video_ep_queue(video, req);
212 spin_unlock_irqrestore(&video->queue.irqlock, flags);
213
214 if (ret < 0) {
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300215 uvcg_queue_cancel(queue, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200216 goto requeue;
217 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200218
219 return;
220
221requeue:
222 spin_lock_irqsave(&video->req_lock, flags);
223 list_add_tail(&req->list, &video->req_free);
224 spin_unlock_irqrestore(&video->req_lock, flags);
225}
226
227static int
228uvc_video_free_requests(struct uvc_video *video)
229{
230 unsigned int i;
231
232 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
233 if (video->req[i]) {
234 usb_ep_free_request(video->ep, video->req[i]);
235 video->req[i] = NULL;
236 }
237
238 if (video->req_buffer[i]) {
239 kfree(video->req_buffer[i]);
240 video->req_buffer[i] = NULL;
241 }
242 }
243
244 INIT_LIST_HEAD(&video->req_free);
245 video->req_size = 0;
246 return 0;
247}
248
249static int
250uvc_video_alloc_requests(struct uvc_video *video)
251{
Bhupesh Sharma326b0e62013-03-01 20:46:31 +0100252 unsigned int req_size;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200253 unsigned int i;
254 int ret = -ENOMEM;
255
256 BUG_ON(video->req_size);
257
Bhupesh Sharma326b0e62013-03-01 20:46:31 +0100258 req_size = video->ep->maxpacket
259 * max_t(unsigned int, video->ep->maxburst, 1)
Felipe Balbi3999c532016-09-28 12:33:31 +0300260 * (video->ep->mult);
Bhupesh Sharma326b0e62013-03-01 20:46:31 +0100261
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200262 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
Bhupesh Sharma326b0e62013-03-01 20:46:31 +0100263 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200264 if (video->req_buffer[i] == NULL)
265 goto error;
266
267 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
268 if (video->req[i] == NULL)
269 goto error;
270
271 video->req[i]->buf = video->req_buffer[i];
272 video->req[i]->length = 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200273 video->req[i]->complete = uvc_video_complete;
274 video->req[i]->context = video;
275
276 list_add_tail(&video->req[i]->list, &video->req_free);
277 }
278
Bhupesh Sharma326b0e62013-03-01 20:46:31 +0100279 video->req_size = req_size;
280
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200281 return 0;
282
283error:
284 uvc_video_free_requests(video);
285 return ret;
286}
287
288/* --------------------------------------------------------------------------
289 * Video streaming
290 */
291
292/*
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300293 * uvcg_video_pump - Pump video data into the USB requests
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200294 *
295 * This function fills the available USB requests (listed in req_free) with
296 * video data from the queued buffers.
297 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300298int uvcg_video_pump(struct uvc_video *video)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200299{
Michael Grzeschikbd52b812014-08-08 17:38:57 +0200300 struct uvc_video_queue *queue = &video->queue;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200301 struct usb_request *req;
302 struct uvc_buffer *buf;
303 unsigned long flags;
304 int ret;
305
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300306 /* FIXME TODO Race between uvcg_video_pump and requests completion
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200307 * handler ???
308 */
309
310 while (1) {
311 /* Retrieve the first available USB request, protected by the
312 * request lock.
313 */
314 spin_lock_irqsave(&video->req_lock, flags);
315 if (list_empty(&video->req_free)) {
316 spin_unlock_irqrestore(&video->req_lock, flags);
317 return 0;
318 }
319 req = list_first_entry(&video->req_free, struct usb_request,
320 list);
321 list_del(&req->list);
322 spin_unlock_irqrestore(&video->req_lock, flags);
323
324 /* Retrieve the first available video buffer and fill the
325 * request, protected by the video queue irqlock.
326 */
Laurent Pinchart6dd5b022014-09-16 17:26:48 +0300327 spin_lock_irqsave(&queue->irqlock, flags);
328 buf = uvcg_queue_head(queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200329 if (buf == NULL) {
Laurent Pinchart6dd5b022014-09-16 17:26:48 +0300330 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200331 break;
332 }
333
334 video->encode(req, video, buf);
335
336 /* Queue the USB request */
Laurent Pinchart28ff0fa2018-08-10 15:42:03 +0300337 ret = uvcg_video_ep_queue(video, req);
338 spin_unlock_irqrestore(&queue->irqlock, flags);
339
Cyril Roelandt6854bcd2013-03-01 20:46:32 +0100340 if (ret < 0) {
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300341 uvcg_queue_cancel(queue, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200342 break;
343 }
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200344 }
345
346 spin_lock_irqsave(&video->req_lock, flags);
347 list_add_tail(&req->list, &video->req_free);
348 spin_unlock_irqrestore(&video->req_lock, flags);
349 return 0;
350}
351
352/*
353 * Enable or disable the video stream.
354 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300355int uvcg_video_enable(struct uvc_video *video, int enable)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200356{
357 unsigned int i;
358 int ret;
359
360 if (video->ep == NULL) {
361 printk(KERN_INFO "Video enable failed, device is "
362 "uninitialized.\n");
363 return -ENODEV;
364 }
365
366 if (!enable) {
367 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
Felipe Balbid7577b32014-09-29 09:19:59 -0500368 if (video->req[i])
369 usb_ep_dequeue(video->ep, video->req[i]);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200370
371 uvc_video_free_requests(video);
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300372 uvcg_queue_enable(&video->queue, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200373 return 0;
374 }
375
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300376 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200377 return ret;
378
379 if ((ret = uvc_video_alloc_requests(video)) < 0)
380 return ret;
381
382 if (video->max_payload_size) {
383 video->encode = uvc_video_encode_bulk;
384 video->payload_size = 0;
385 } else
386 video->encode = uvc_video_encode_isoc;
387
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300388 return uvcg_video_pump(video);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200389}
390
391/*
392 * Initialize the UVC video stream.
393 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300394int uvcg_video_init(struct uvc_video *video)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200395{
396 INIT_LIST_HEAD(&video->req_free);
397 spin_lock_init(&video->req_lock);
398
399 video->fcc = V4L2_PIX_FMT_YUYV;
400 video->bpp = 16;
401 video->width = 320;
402 video->height = 240;
403 video->imagesize = 320 * 240 * 2;
404
405 /* Initialize the video buffers queue. */
Hans Verkuild8e96c42015-02-17 05:44:06 -0300406 uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
407 &video->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200408 return 0;
409}
410