Btrfs: stop the readahead threads on failed mount
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / uvc_video.c
blobb08f35438d7012deb1b21088cc2a64fe45f2d531
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.
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
20 #include <media/v4l2-dev.h>
22 #include "uvc.h"
23 #include "uvc_queue.h"
25 /* --------------------------------------------------------------------------
26 * Video codecs
29 static int
30 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
31 u8 *data, int len)
33 data[0] = 2;
34 data[1] = UVC_STREAM_EOH | video->fid;
36 if (buf->buf.bytesused - video->queue.buf_used <= len - 2)
37 data[1] |= UVC_STREAM_EOF;
39 return 2;
42 static int
43 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
44 u8 *data, int len)
46 struct uvc_video_queue *queue = &video->queue;
47 unsigned int nbytes;
48 void *mem;
50 /* Copy video data to the USB buffer. */
51 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
52 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
54 memcpy(data, mem, nbytes);
55 queue->buf_used += nbytes;
57 return nbytes;
60 static void
61 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
62 struct uvc_buffer *buf)
64 void *mem = req->buf;
65 int len = video->req_size;
66 int ret;
68 /* Add a header at the beginning of the payload. */
69 if (video->payload_size == 0) {
70 ret = uvc_video_encode_header(video, buf, mem, len);
71 video->payload_size += ret;
72 mem += ret;
73 len -= ret;
76 /* Process video data. */
77 len = min((int)(video->max_payload_size - video->payload_size), len);
78 ret = uvc_video_encode_data(video, buf, mem, len);
80 video->payload_size += ret;
81 len -= ret;
83 req->length = video->req_size - len;
84 req->zero = video->payload_size == video->max_payload_size;
86 if (buf->buf.bytesused == video->queue.buf_used) {
87 video->queue.buf_used = 0;
88 buf->state = UVC_BUF_STATE_DONE;
89 uvc_queue_next_buffer(&video->queue, buf);
90 video->fid ^= UVC_STREAM_FID;
92 video->payload_size = 0;
95 if (video->payload_size == video->max_payload_size ||
96 buf->buf.bytesused == video->queue.buf_used)
97 video->payload_size = 0;
100 static void
101 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
102 struct uvc_buffer *buf)
104 void *mem = req->buf;
105 int len = video->req_size;
106 int ret;
108 /* Add the header. */
109 ret = uvc_video_encode_header(video, buf, mem, len);
110 mem += ret;
111 len -= ret;
113 /* Process video data. */
114 ret = uvc_video_encode_data(video, buf, mem, len);
115 len -= ret;
117 req->length = video->req_size - len;
119 if (buf->buf.bytesused == video->queue.buf_used) {
120 video->queue.buf_used = 0;
121 buf->state = UVC_BUF_STATE_DONE;
122 uvc_queue_next_buffer(&video->queue, buf);
123 video->fid ^= UVC_STREAM_FID;
127 /* --------------------------------------------------------------------------
128 * Request handling
132 * I somehow feel that synchronisation won't be easy to achieve here. We have
133 * three events that control USB requests submission:
135 * - USB request completion: the completion handler will resubmit the request
136 * if a video buffer is available.
138 * - USB interface setting selection: in response to a SET_INTERFACE request,
139 * the handler will start streaming if a video buffer is available and if
140 * video is not currently streaming.
142 * - V4L2 buffer queueing: the driver will start streaming if video is not
143 * currently streaming.
145 * Race conditions between those 3 events might lead to deadlocks or other
146 * nasty side effects.
148 * The "video currently streaming" condition can't be detected by the irqqueue
149 * being empty, as a request can still be in flight. A separate "queue paused"
150 * flag is thus needed.
152 * The paused flag will be set when we try to retrieve the irqqueue head if the
153 * queue is empty, and cleared when we queue a buffer.
155 * The USB request completion handler will get the buffer at the irqqueue head
156 * under protection of the queue spinlock. If the queue is empty, the streaming
157 * paused flag will be set. Right after releasing the spinlock a userspace
158 * application can queue a buffer. The flag will then cleared, and the ioctl
159 * handler will restart the video stream.
161 static void
162 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
164 struct uvc_video *video = req->context;
165 struct uvc_buffer *buf;
166 unsigned long flags;
167 int ret;
169 switch (req->status) {
170 case 0:
171 break;
173 case -ESHUTDOWN:
174 printk(KERN_INFO "VS request cancelled.\n");
175 goto requeue;
177 default:
178 printk(KERN_INFO "VS request completed with status %d.\n",
179 req->status);
180 goto requeue;
183 spin_lock_irqsave(&video->queue.irqlock, flags);
184 buf = uvc_queue_head(&video->queue);
185 if (buf == NULL) {
186 spin_unlock_irqrestore(&video->queue.irqlock, flags);
187 goto requeue;
190 video->encode(req, video, buf);
192 if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
193 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
194 usb_ep_set_halt(ep);
195 spin_unlock_irqrestore(&video->queue.irqlock, flags);
196 goto requeue;
198 spin_unlock_irqrestore(&video->queue.irqlock, flags);
200 return;
202 requeue:
203 spin_lock_irqsave(&video->req_lock, flags);
204 list_add_tail(&req->list, &video->req_free);
205 spin_unlock_irqrestore(&video->req_lock, flags);
208 static int
209 uvc_video_free_requests(struct uvc_video *video)
211 unsigned int i;
213 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
214 if (video->req[i]) {
215 usb_ep_free_request(video->ep, video->req[i]);
216 video->req[i] = NULL;
219 if (video->req_buffer[i]) {
220 kfree(video->req_buffer[i]);
221 video->req_buffer[i] = NULL;
225 INIT_LIST_HEAD(&video->req_free);
226 video->req_size = 0;
227 return 0;
230 static int
231 uvc_video_alloc_requests(struct uvc_video *video)
233 unsigned int i;
234 int ret = -ENOMEM;
236 BUG_ON(video->req_size);
238 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
239 video->req_buffer[i] = kmalloc(video->ep->maxpacket, GFP_KERNEL);
240 if (video->req_buffer[i] == NULL)
241 goto error;
243 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
244 if (video->req[i] == NULL)
245 goto error;
247 video->req[i]->buf = video->req_buffer[i];
248 video->req[i]->length = 0;
249 video->req[i]->dma = DMA_ADDR_INVALID;
250 video->req[i]->complete = uvc_video_complete;
251 video->req[i]->context = video;
253 list_add_tail(&video->req[i]->list, &video->req_free);
256 video->req_size = video->ep->maxpacket;
257 return 0;
259 error:
260 uvc_video_free_requests(video);
261 return ret;
264 /* --------------------------------------------------------------------------
265 * Video streaming
269 * uvc_video_pump - Pump video data into the USB requests
271 * This function fills the available USB requests (listed in req_free) with
272 * video data from the queued buffers.
274 static int
275 uvc_video_pump(struct uvc_video *video)
277 struct usb_request *req;
278 struct uvc_buffer *buf;
279 unsigned long flags;
280 int ret;
282 /* FIXME TODO Race between uvc_video_pump and requests completion
283 * handler ???
286 while (1) {
287 /* Retrieve the first available USB request, protected by the
288 * request lock.
290 spin_lock_irqsave(&video->req_lock, flags);
291 if (list_empty(&video->req_free)) {
292 spin_unlock_irqrestore(&video->req_lock, flags);
293 return 0;
295 req = list_first_entry(&video->req_free, struct usb_request,
296 list);
297 list_del(&req->list);
298 spin_unlock_irqrestore(&video->req_lock, flags);
300 /* Retrieve the first available video buffer and fill the
301 * request, protected by the video queue irqlock.
303 spin_lock_irqsave(&video->queue.irqlock, flags);
304 buf = uvc_queue_head(&video->queue);
305 if (buf == NULL) {
306 spin_unlock_irqrestore(&video->queue.irqlock, flags);
307 break;
310 video->encode(req, video, buf);
312 /* Queue the USB request */
313 if ((ret = usb_ep_queue(video->ep, req, GFP_KERNEL)) < 0) {
314 printk(KERN_INFO "Failed to queue request (%d)\n", ret);
315 usb_ep_set_halt(video->ep);
316 spin_unlock_irqrestore(&video->queue.irqlock, flags);
317 break;
319 spin_unlock_irqrestore(&video->queue.irqlock, flags);
322 spin_lock_irqsave(&video->req_lock, flags);
323 list_add_tail(&req->list, &video->req_free);
324 spin_unlock_irqrestore(&video->req_lock, flags);
325 return 0;
329 * Enable or disable the video stream.
331 static int
332 uvc_video_enable(struct uvc_video *video, int enable)
334 unsigned int i;
335 int ret;
337 if (video->ep == NULL) {
338 printk(KERN_INFO "Video enable failed, device is "
339 "uninitialized.\n");
340 return -ENODEV;
343 if (!enable) {
344 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
345 usb_ep_dequeue(video->ep, video->req[i]);
347 uvc_video_free_requests(video);
348 uvc_queue_enable(&video->queue, 0);
349 return 0;
352 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
353 return ret;
355 if ((ret = uvc_video_alloc_requests(video)) < 0)
356 return ret;
358 if (video->max_payload_size) {
359 video->encode = uvc_video_encode_bulk;
360 video->payload_size = 0;
361 } else
362 video->encode = uvc_video_encode_isoc;
364 return uvc_video_pump(video);
368 * Initialize the UVC video stream.
370 static int
371 uvc_video_init(struct uvc_video *video)
373 INIT_LIST_HEAD(&video->req_free);
374 spin_lock_init(&video->req_lock);
376 video->fcc = V4L2_PIX_FMT_YUYV;
377 video->bpp = 16;
378 video->width = 320;
379 video->height = 240;
380 video->imagesize = 320 * 240 * 2;
382 /* Initialize the video buffers queue. */
383 uvc_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
384 return 0;