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>
18 #include <linux/usb/video.h>
20 #include <media/v4l2-dev.h>
23 #include "uvc_queue.h"
24 #include "uvc_video.h"
26 /* --------------------------------------------------------------------------
31 uvc_video_encode_header(struct uvc_video
*video
, struct uvc_buffer
*buf
,
35 data
[1] = UVC_STREAM_EOH
| video
->fid
;
37 if (buf
->bytesused
- video
->queue
.buf_used
<= len
- 2)
38 data
[1] |= UVC_STREAM_EOF
;
44 uvc_video_encode_data(struct uvc_video
*video
, struct uvc_buffer
*buf
,
47 struct uvc_video_queue
*queue
= &video
->queue
;
51 /* Copy video data to the USB buffer. */
52 mem
= buf
->mem
+ queue
->buf_used
;
53 nbytes
= min((unsigned int)len
, buf
->bytesused
- queue
->buf_used
);
55 memcpy(data
, mem
, nbytes
);
56 queue
->buf_used
+= nbytes
;
62 uvc_video_encode_bulk(struct usb_request
*req
, struct uvc_video
*video
,
63 struct uvc_buffer
*buf
)
66 int len
= video
->req_size
;
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
;
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
);
81 video
->payload_size
+= ret
;
84 req
->length
= video
->req_size
- len
;
85 req
->zero
= video
->payload_size
== video
->max_payload_size
;
87 if (buf
->bytesused
== video
->queue
.buf_used
) {
88 video
->queue
.buf_used
= 0;
89 buf
->state
= UVC_BUF_STATE_DONE
;
90 uvcg_queue_next_buffer(&video
->queue
, buf
);
91 video
->fid
^= UVC_STREAM_FID
;
93 video
->payload_size
= 0;
96 if (video
->payload_size
== video
->max_payload_size
||
97 buf
->bytesused
== video
->queue
.buf_used
)
98 video
->payload_size
= 0;
102 uvc_video_encode_isoc(struct usb_request
*req
, struct uvc_video
*video
,
103 struct uvc_buffer
*buf
)
105 void *mem
= req
->buf
;
106 int len
= video
->req_size
;
109 /* Add the header. */
110 ret
= uvc_video_encode_header(video
, buf
, mem
, len
);
114 /* Process video data. */
115 ret
= uvc_video_encode_data(video
, buf
, mem
, len
);
118 req
->length
= video
->req_size
- len
;
120 if (buf
->bytesused
== video
->queue
.buf_used
) {
121 video
->queue
.buf_used
= 0;
122 buf
->state
= UVC_BUF_STATE_DONE
;
123 uvcg_queue_next_buffer(&video
->queue
, buf
);
124 video
->fid
^= UVC_STREAM_FID
;
128 /* --------------------------------------------------------------------------
133 * I somehow feel that synchronisation won't be easy to achieve here. We have
134 * three events that control USB requests submission:
136 * - USB request completion: the completion handler will resubmit the request
137 * if a video buffer is available.
139 * - USB interface setting selection: in response to a SET_INTERFACE request,
140 * the handler will start streaming if a video buffer is available and if
141 * video is not currently streaming.
143 * - V4L2 buffer queueing: the driver will start streaming if video is not
144 * currently streaming.
146 * Race conditions between those 3 events might lead to deadlocks or other
147 * nasty side effects.
149 * The "video currently streaming" condition can't be detected by the irqqueue
150 * being empty, as a request can still be in flight. A separate "queue paused"
151 * flag is thus needed.
153 * The paused flag will be set when we try to retrieve the irqqueue head if the
154 * queue is empty, and cleared when we queue a buffer.
156 * The USB request completion handler will get the buffer at the irqqueue head
157 * under protection of the queue spinlock. If the queue is empty, the streaming
158 * paused flag will be set. Right after releasing the spinlock a userspace
159 * application can queue a buffer. The flag will then cleared, and the ioctl
160 * handler will restart the video stream.
163 uvc_video_complete(struct usb_ep
*ep
, struct usb_request
*req
)
165 struct uvc_video
*video
= req
->context
;
166 struct uvc_video_queue
*queue
= &video
->queue
;
167 struct uvc_buffer
*buf
;
171 switch (req
->status
) {
175 case -ESHUTDOWN
: /* disconnect from host. */
176 printk(KERN_DEBUG
"VS request cancelled.\n");
177 uvcg_queue_cancel(queue
, 1);
181 printk(KERN_INFO
"VS request completed with status %d.\n",
183 uvcg_queue_cancel(queue
, 0);
187 spin_lock_irqsave(&video
->queue
.irqlock
, flags
);
188 buf
= uvcg_queue_head(&video
->queue
);
190 spin_unlock_irqrestore(&video
->queue
.irqlock
, flags
);
194 video
->encode(req
, video
, buf
);
196 if ((ret
= usb_ep_queue(ep
, req
, GFP_ATOMIC
)) < 0) {
197 printk(KERN_INFO
"Failed to queue request (%d).\n", ret
);
199 spin_unlock_irqrestore(&video
->queue
.irqlock
, flags
);
200 uvcg_queue_cancel(queue
, 0);
203 spin_unlock_irqrestore(&video
->queue
.irqlock
, flags
);
208 spin_lock_irqsave(&video
->req_lock
, flags
);
209 list_add_tail(&req
->list
, &video
->req_free
);
210 spin_unlock_irqrestore(&video
->req_lock
, flags
);
214 uvc_video_free_requests(struct uvc_video
*video
)
218 for (i
= 0; i
< UVC_NUM_REQUESTS
; ++i
) {
220 usb_ep_free_request(video
->ep
, video
->req
[i
]);
221 video
->req
[i
] = NULL
;
224 if (video
->req_buffer
[i
]) {
225 kfree(video
->req_buffer
[i
]);
226 video
->req_buffer
[i
] = NULL
;
230 INIT_LIST_HEAD(&video
->req_free
);
236 uvc_video_alloc_requests(struct uvc_video
*video
)
238 unsigned int req_size
;
242 BUG_ON(video
->req_size
);
244 req_size
= video
->ep
->maxpacket
245 * max_t(unsigned int, video
->ep
->maxburst
, 1)
246 * (video
->ep
->mult
+ 1);
248 for (i
= 0; i
< UVC_NUM_REQUESTS
; ++i
) {
249 video
->req_buffer
[i
] = kmalloc(req_size
, GFP_KERNEL
);
250 if (video
->req_buffer
[i
] == NULL
)
253 video
->req
[i
] = usb_ep_alloc_request(video
->ep
, GFP_KERNEL
);
254 if (video
->req
[i
] == NULL
)
257 video
->req
[i
]->buf
= video
->req_buffer
[i
];
258 video
->req
[i
]->length
= 0;
259 video
->req
[i
]->complete
= uvc_video_complete
;
260 video
->req
[i
]->context
= video
;
262 list_add_tail(&video
->req
[i
]->list
, &video
->req_free
);
265 video
->req_size
= req_size
;
270 uvc_video_free_requests(video
);
274 /* --------------------------------------------------------------------------
279 * uvcg_video_pump - Pump video data into the USB requests
281 * This function fills the available USB requests (listed in req_free) with
282 * video data from the queued buffers.
284 int uvcg_video_pump(struct uvc_video
*video
)
286 struct uvc_video_queue
*queue
= &video
->queue
;
287 struct usb_request
*req
;
288 struct uvc_buffer
*buf
;
292 /* FIXME TODO Race between uvcg_video_pump and requests completion
297 /* Retrieve the first available USB request, protected by the
300 spin_lock_irqsave(&video
->req_lock
, flags
);
301 if (list_empty(&video
->req_free
)) {
302 spin_unlock_irqrestore(&video
->req_lock
, flags
);
305 req
= list_first_entry(&video
->req_free
, struct usb_request
,
307 list_del(&req
->list
);
308 spin_unlock_irqrestore(&video
->req_lock
, flags
);
310 /* Retrieve the first available video buffer and fill the
311 * request, protected by the video queue irqlock.
313 spin_lock_irqsave(&queue
->irqlock
, flags
);
314 buf
= uvcg_queue_head(queue
);
316 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
320 video
->encode(req
, video
, buf
);
322 /* Queue the USB request */
323 ret
= usb_ep_queue(video
->ep
, req
, GFP_ATOMIC
);
325 printk(KERN_INFO
"Failed to queue request (%d)\n", ret
);
326 usb_ep_set_halt(video
->ep
);
327 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
328 uvcg_queue_cancel(queue
, 0);
331 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
334 spin_lock_irqsave(&video
->req_lock
, flags
);
335 list_add_tail(&req
->list
, &video
->req_free
);
336 spin_unlock_irqrestore(&video
->req_lock
, flags
);
341 * Enable or disable the video stream.
343 int uvcg_video_enable(struct uvc_video
*video
, int enable
)
348 if (video
->ep
== NULL
) {
349 printk(KERN_INFO
"Video enable failed, device is "
355 for (i
= 0; i
< UVC_NUM_REQUESTS
; ++i
)
357 usb_ep_dequeue(video
->ep
, video
->req
[i
]);
359 uvc_video_free_requests(video
);
360 uvcg_queue_enable(&video
->queue
, 0);
364 if ((ret
= uvcg_queue_enable(&video
->queue
, 1)) < 0)
367 if ((ret
= uvc_video_alloc_requests(video
)) < 0)
370 if (video
->max_payload_size
) {
371 video
->encode
= uvc_video_encode_bulk
;
372 video
->payload_size
= 0;
374 video
->encode
= uvc_video_encode_isoc
;
376 return uvcg_video_pump(video
);
380 * Initialize the UVC video stream.
382 int uvcg_video_init(struct uvc_video
*video
)
384 INIT_LIST_HEAD(&video
->req_free
);
385 spin_lock_init(&video
->req_lock
);
387 video
->fcc
= V4L2_PIX_FMT_YUYV
;
391 video
->imagesize
= 320 * 240 * 2;
393 /* Initialize the video buffers queue. */
394 uvcg_queue_init(&video
->queue
, V4L2_BUF_TYPE_VIDEO_OUTPUT
,