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