x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / gadget / function / uvc_video.c
blobd3567b90343a4ab57cd185360b30761307304e3a
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * uvc_video.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
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>
16 #include <media/v4l2-dev.h>
18 #include "uvc.h"
19 #include "uvc_queue.h"
20 #include "uvc_video.h"
22 /* --------------------------------------------------------------------------
23 * Video codecs
26 static int
27 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
28 u8 *data, int len)
30 data[0] = 2;
31 data[1] = UVC_STREAM_EOH | video->fid;
33 if (buf->bytesused - video->queue.buf_used <= len - 2)
34 data[1] |= UVC_STREAM_EOF;
36 return 2;
39 static int
40 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
41 u8 *data, int len)
43 struct uvc_video_queue *queue = &video->queue;
44 unsigned int nbytes;
45 void *mem;
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);
51 memcpy(data, mem, nbytes);
52 queue->buf_used += nbytes;
54 return nbytes;
57 static void
58 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
59 struct uvc_buffer *buf)
61 void *mem = req->buf;
62 int len = video->req_size;
63 int ret;
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;
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);
77 video->payload_size += ret;
78 len -= ret;
80 req->length = video->req_size - len;
81 req->zero = video->payload_size == video->max_payload_size;
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;
89 video->payload_size = 0;
92 if (video->payload_size == video->max_payload_size ||
93 buf->bytesused == video->queue.buf_used)
94 video->payload_size = 0;
97 static void
98 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
99 struct uvc_buffer *buf)
101 void *mem = req->buf;
102 int len = video->req_size;
103 int ret;
105 /* Add the header. */
106 ret = uvc_video_encode_header(video, buf, mem, len);
107 mem += ret;
108 len -= ret;
110 /* Process video data. */
111 ret = uvc_video_encode_data(video, buf, mem, len);
112 len -= ret;
114 req->length = video->req_size - len;
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;
124 /* --------------------------------------------------------------------------
125 * Request handling
129 * I somehow feel that synchronisation won't be easy to achieve here. We have
130 * three events that control USB requests submission:
132 * - USB request completion: the completion handler will resubmit the request
133 * if a video buffer is available.
135 * - USB interface setting selection: in response to a SET_INTERFACE request,
136 * the handler will start streaming if a video buffer is available and if
137 * video is not currently streaming.
139 * - V4L2 buffer queueing: the driver will start streaming if video is not
140 * currently streaming.
142 * Race conditions between those 3 events might lead to deadlocks or other
143 * nasty side effects.
145 * The "video currently streaming" condition can't be detected by the irqqueue
146 * being empty, as a request can still be in flight. A separate "queue paused"
147 * flag is thus needed.
149 * The paused flag will be set when we try to retrieve the irqqueue head if the
150 * queue is empty, and cleared when we queue a buffer.
152 * The USB request completion handler will get the buffer at the irqqueue head
153 * under protection of the queue spinlock. If the queue is empty, the streaming
154 * paused flag will be set. Right after releasing the spinlock a userspace
155 * application can queue a buffer. The flag will then cleared, and the ioctl
156 * handler will restart the video stream.
158 static void
159 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
161 struct uvc_video *video = req->context;
162 struct uvc_video_queue *queue = &video->queue;
163 struct uvc_buffer *buf;
164 unsigned long flags;
165 int ret;
167 switch (req->status) {
168 case 0:
169 break;
171 case -ESHUTDOWN: /* disconnect from host. */
172 printk(KERN_DEBUG "VS request cancelled.\n");
173 uvcg_queue_cancel(queue, 1);
174 goto requeue;
176 default:
177 printk(KERN_INFO "VS request completed with status %d.\n",
178 req->status);
179 uvcg_queue_cancel(queue, 0);
180 goto requeue;
183 spin_lock_irqsave(&video->queue.irqlock, flags);
184 buf = uvcg_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 uvcg_queue_cancel(queue, 0);
197 goto requeue;
199 spin_unlock_irqrestore(&video->queue.irqlock, flags);
201 return;
203 requeue:
204 spin_lock_irqsave(&video->req_lock, flags);
205 list_add_tail(&req->list, &video->req_free);
206 spin_unlock_irqrestore(&video->req_lock, flags);
209 static int
210 uvc_video_free_requests(struct uvc_video *video)
212 unsigned int i;
214 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
215 if (video->req[i]) {
216 usb_ep_free_request(video->ep, video->req[i]);
217 video->req[i] = NULL;
220 if (video->req_buffer[i]) {
221 kfree(video->req_buffer[i]);
222 video->req_buffer[i] = NULL;
226 INIT_LIST_HEAD(&video->req_free);
227 video->req_size = 0;
228 return 0;
231 static int
232 uvc_video_alloc_requests(struct uvc_video *video)
234 unsigned int req_size;
235 unsigned int i;
236 int ret = -ENOMEM;
238 BUG_ON(video->req_size);
240 req_size = video->ep->maxpacket
241 * max_t(unsigned int, video->ep->maxburst, 1)
242 * (video->ep->mult);
244 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
245 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
246 if (video->req_buffer[i] == NULL)
247 goto error;
249 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
250 if (video->req[i] == NULL)
251 goto error;
253 video->req[i]->buf = video->req_buffer[i];
254 video->req[i]->length = 0;
255 video->req[i]->complete = uvc_video_complete;
256 video->req[i]->context = video;
258 list_add_tail(&video->req[i]->list, &video->req_free);
261 video->req_size = req_size;
263 return 0;
265 error:
266 uvc_video_free_requests(video);
267 return ret;
270 /* --------------------------------------------------------------------------
271 * Video streaming
275 * uvcg_video_pump - Pump video data into the USB requests
277 * This function fills the available USB requests (listed in req_free) with
278 * video data from the queued buffers.
280 int uvcg_video_pump(struct uvc_video *video)
282 struct uvc_video_queue *queue = &video->queue;
283 struct usb_request *req;
284 struct uvc_buffer *buf;
285 unsigned long flags;
286 int ret;
288 /* FIXME TODO Race between uvcg_video_pump and requests completion
289 * handler ???
292 while (1) {
293 /* Retrieve the first available USB request, protected by the
294 * request lock.
296 spin_lock_irqsave(&video->req_lock, flags);
297 if (list_empty(&video->req_free)) {
298 spin_unlock_irqrestore(&video->req_lock, flags);
299 return 0;
301 req = list_first_entry(&video->req_free, struct usb_request,
302 list);
303 list_del(&req->list);
304 spin_unlock_irqrestore(&video->req_lock, flags);
306 /* Retrieve the first available video buffer and fill the
307 * request, protected by the video queue irqlock.
309 spin_lock_irqsave(&queue->irqlock, flags);
310 buf = uvcg_queue_head(queue);
311 if (buf == NULL) {
312 spin_unlock_irqrestore(&queue->irqlock, flags);
313 break;
316 video->encode(req, video, buf);
318 /* Queue the USB request */
319 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
320 if (ret < 0) {
321 printk(KERN_INFO "Failed to queue request (%d)\n", ret);
322 usb_ep_set_halt(video->ep);
323 spin_unlock_irqrestore(&queue->irqlock, flags);
324 uvcg_queue_cancel(queue, 0);
325 break;
327 spin_unlock_irqrestore(&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 int uvcg_video_enable(struct uvc_video *video, int enable)
341 unsigned int i;
342 int ret;
344 if (video->ep == NULL) {
345 printk(KERN_INFO "Video enable failed, device is "
346 "uninitialized.\n");
347 return -ENODEV;
350 if (!enable) {
351 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
352 if (video->req[i])
353 usb_ep_dequeue(video->ep, video->req[i]);
355 uvc_video_free_requests(video);
356 uvcg_queue_enable(&video->queue, 0);
357 return 0;
360 if ((ret = uvcg_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 uvcg_video_pump(video);
376 * Initialize the UVC video stream.
378 int uvcg_video_init(struct uvc_video *video)
380 INIT_LIST_HEAD(&video->req_free);
381 spin_lock_init(&video->req_lock);
383 video->fcc = V4L2_PIX_FMT_YUYV;
384 video->bpp = 16;
385 video->width = 320;
386 video->height = 240;
387 video->imagesize = 320 * 240 * 2;
389 /* Initialize the video buffers queue. */
390 uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
391 &video->mutex);
392 return 0;