Merge git://www.linux-watchdog.org/linux-watchdog
[linux/fpc-iii.git] / drivers / usb / gadget / uvc_video.c
blob71e896d4c5ae481d189e582609d8cb456a8187c3
1 /*
2 * uvc_video.c -- USB Video Class Gadget driver
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
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>
19 #include <media/v4l2-dev.h>
21 #include "uvc.h"
22 #include "uvc_queue.h"
24 /* --------------------------------------------------------------------------
25 * Video codecs
28 static int
29 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
30 u8 *data, int len)
32 data[0] = 2;
33 data[1] = UVC_STREAM_EOH | video->fid;
35 if (buf->bytesused - video->queue.buf_used <= len - 2)
36 data[1] |= UVC_STREAM_EOF;
38 return 2;
41 static int
42 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
43 u8 *data, int len)
45 struct uvc_video_queue *queue = &video->queue;
46 unsigned int nbytes;
47 void *mem;
49 /* Copy video data to the USB buffer. */
50 mem = buf->mem + queue->buf_used;
51 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
53 memcpy(data, mem, nbytes);
54 queue->buf_used += nbytes;
56 return nbytes;
59 static void
60 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
61 struct uvc_buffer *buf)
63 void *mem = req->buf;
64 int len = video->req_size;
65 int ret;
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;
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);
79 video->payload_size += ret;
80 len -= ret;
82 req->length = video->req_size - len;
83 req->zero = video->payload_size == video->max_payload_size;
85 if (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;
91 video->payload_size = 0;
94 if (video->payload_size == video->max_payload_size ||
95 buf->bytesused == video->queue.buf_used)
96 video->payload_size = 0;
99 static void
100 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
101 struct uvc_buffer *buf)
103 void *mem = req->buf;
104 int len = video->req_size;
105 int ret;
107 /* Add the header. */
108 ret = uvc_video_encode_header(video, buf, mem, len);
109 mem += ret;
110 len -= ret;
112 /* Process video data. */
113 ret = uvc_video_encode_data(video, buf, mem, len);
114 len -= ret;
116 req->length = video->req_size - len;
118 if (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;
126 /* --------------------------------------------------------------------------
127 * Request handling
131 * I somehow feel that synchronisation won't be easy to achieve here. We have
132 * three events that control USB requests submission:
134 * - USB request completion: the completion handler will resubmit the request
135 * if a video buffer is available.
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.
141 * - V4L2 buffer queueing: the driver will start streaming if video is not
142 * currently streaming.
144 * Race conditions between those 3 events might lead to deadlocks or other
145 * nasty side effects.
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.
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.
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.
160 static void
161 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
163 struct uvc_video *video = req->context;
164 struct uvc_video_queue *queue = &video->queue;
165 struct uvc_buffer *buf;
166 unsigned long flags;
167 int ret;
169 switch (req->status) {
170 case 0:
171 break;
173 case -ESHUTDOWN: /* disconnect from host. */
174 printk(KERN_INFO "VS request cancelled.\n");
175 uvc_queue_cancel(queue, 1);
176 goto requeue;
178 default:
179 printk(KERN_INFO "VS request completed with status %d.\n",
180 req->status);
181 uvc_queue_cancel(queue, 0);
182 goto requeue;
185 spin_lock_irqsave(&video->queue.irqlock, flags);
186 buf = uvc_queue_head(&video->queue);
187 if (buf == NULL) {
188 spin_unlock_irqrestore(&video->queue.irqlock, flags);
189 goto requeue;
192 video->encode(req, video, buf);
194 if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
195 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
196 usb_ep_set_halt(ep);
197 spin_unlock_irqrestore(&video->queue.irqlock, flags);
198 goto requeue;
200 spin_unlock_irqrestore(&video->queue.irqlock, flags);
202 return;
204 requeue:
205 spin_lock_irqsave(&video->req_lock, flags);
206 list_add_tail(&req->list, &video->req_free);
207 spin_unlock_irqrestore(&video->req_lock, flags);
210 static int
211 uvc_video_free_requests(struct uvc_video *video)
213 unsigned int i;
215 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
216 if (video->req[i]) {
217 usb_ep_free_request(video->ep, video->req[i]);
218 video->req[i] = NULL;
221 if (video->req_buffer[i]) {
222 kfree(video->req_buffer[i]);
223 video->req_buffer[i] = NULL;
227 INIT_LIST_HEAD(&video->req_free);
228 video->req_size = 0;
229 return 0;
232 static int
233 uvc_video_alloc_requests(struct uvc_video *video)
235 unsigned int req_size;
236 unsigned int i;
237 int ret = -ENOMEM;
239 BUG_ON(video->req_size);
241 req_size = video->ep->maxpacket
242 * max_t(unsigned int, video->ep->maxburst, 1)
243 * (video->ep->mult + 1);
245 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
246 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
247 if (video->req_buffer[i] == NULL)
248 goto error;
250 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
251 if (video->req[i] == NULL)
252 goto error;
254 video->req[i]->buf = video->req_buffer[i];
255 video->req[i]->length = 0;
256 video->req[i]->complete = uvc_video_complete;
257 video->req[i]->context = video;
259 list_add_tail(&video->req[i]->list, &video->req_free);
262 video->req_size = req_size;
264 return 0;
266 error:
267 uvc_video_free_requests(video);
268 return ret;
271 /* --------------------------------------------------------------------------
272 * Video streaming
276 * uvc_video_pump - Pump video data into the USB requests
278 * This function fills the available USB requests (listed in req_free) with
279 * video data from the queued buffers.
281 static int
282 uvc_video_pump(struct uvc_video *video)
284 struct usb_request *req;
285 struct uvc_buffer *buf;
286 unsigned long flags;
287 int ret;
289 /* FIXME TODO Race between uvc_video_pump and requests completion
290 * handler ???
293 while (1) {
294 /* Retrieve the first available USB request, protected by the
295 * request lock.
297 spin_lock_irqsave(&video->req_lock, flags);
298 if (list_empty(&video->req_free)) {
299 spin_unlock_irqrestore(&video->req_lock, flags);
300 return 0;
302 req = list_first_entry(&video->req_free, struct usb_request,
303 list);
304 list_del(&req->list);
305 spin_unlock_irqrestore(&video->req_lock, flags);
307 /* Retrieve the first available video buffer and fill the
308 * request, protected by the video queue irqlock.
310 spin_lock_irqsave(&video->queue.irqlock, flags);
311 buf = uvc_queue_head(&video->queue);
312 if (buf == NULL) {
313 spin_unlock_irqrestore(&video->queue.irqlock, flags);
314 break;
317 video->encode(req, video, buf);
319 /* Queue the USB request */
320 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
321 if (ret < 0) {
322 printk(KERN_INFO "Failed to queue request (%d)\n", ret);
323 usb_ep_set_halt(video->ep);
324 spin_unlock_irqrestore(&video->queue.irqlock, flags);
325 break;
327 spin_unlock_irqrestore(&video->queue.irqlock, flags);
330 spin_lock_irqsave(&video->req_lock, flags);
331 list_add_tail(&req->list, &video->req_free);
332 spin_unlock_irqrestore(&video->req_lock, flags);
333 return 0;
337 * Enable or disable the video stream.
339 static int
340 uvc_video_enable(struct uvc_video *video, int enable)
342 unsigned int i;
343 int ret;
345 if (video->ep == NULL) {
346 printk(KERN_INFO "Video enable failed, device is "
347 "uninitialized.\n");
348 return -ENODEV;
351 if (!enable) {
352 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
353 usb_ep_dequeue(video->ep, video->req[i]);
355 uvc_video_free_requests(video);
356 uvc_queue_enable(&video->queue, 0);
357 return 0;
360 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
361 return ret;
363 if ((ret = uvc_video_alloc_requests(video)) < 0)
364 return ret;
366 if (video->max_payload_size) {
367 video->encode = uvc_video_encode_bulk;
368 video->payload_size = 0;
369 } else
370 video->encode = uvc_video_encode_isoc;
372 return uvc_video_pump(video);
376 * Initialize the UVC video stream.
378 static int
379 uvc_video_init(struct uvc_video *video)
381 INIT_LIST_HEAD(&video->req_free);
382 spin_lock_init(&video->req_lock);
384 video->fcc = V4L2_PIX_FMT_YUYV;
385 video->bpp = 16;
386 video->width = 320;
387 video->height = 240;
388 video->imagesize = 320 * 240 * 2;
390 /* Initialize the video buffers queue. */
391 uvc_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
392 return 0;