Loading...
1/*
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.
11 */
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#include <linux/usb/video.h>
19
20#include <media/v4l2-dev.h>
21
22#include "uvc.h"
23#include "uvc_queue.h"
24#include "uvc_video.h"
25
26/* --------------------------------------------------------------------------
27 * Video codecs
28 */
29
30static int
31uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
32 u8 *data, int len)
33{
34 data[0] = 2;
35 data[1] = UVC_STREAM_EOH | video->fid;
36
37 if (buf->bytesused - video->queue.buf_used <= len - 2)
38 data[1] |= UVC_STREAM_EOF;
39
40 return 2;
41}
42
43static int
44uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
45 u8 *data, int len)
46{
47 struct uvc_video_queue *queue = &video->queue;
48 unsigned int nbytes;
49 void *mem;
50
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);
54
55 memcpy(data, mem, nbytes);
56 queue->buf_used += nbytes;
57
58 return nbytes;
59}
60
61static void
62uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
63 struct uvc_buffer *buf)
64{
65 void *mem = req->buf;
66 int len = video->req_size;
67 int ret;
68
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;
73 mem += ret;
74 len -= ret;
75 }
76
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);
80
81 video->payload_size += ret;
82 len -= ret;
83
84 req->length = video->req_size - len;
85 req->zero = video->payload_size == video->max_payload_size;
86
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;
92
93 video->payload_size = 0;
94 }
95
96 if (video->payload_size == video->max_payload_size ||
97 buf->bytesused == video->queue.buf_used)
98 video->payload_size = 0;
99}
100
101static void
102uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
103 struct uvc_buffer *buf)
104{
105 void *mem = req->buf;
106 int len = video->req_size;
107 int ret;
108
109 /* Add the header. */
110 ret = uvc_video_encode_header(video, buf, mem, len);
111 mem += ret;
112 len -= ret;
113
114 /* Process video data. */
115 ret = uvc_video_encode_data(video, buf, mem, len);
116 len -= ret;
117
118 req->length = video->req_size - len;
119
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;
125 }
126}
127
128/* --------------------------------------------------------------------------
129 * Request handling
130 */
131
132/*
133 * I somehow feel that synchronisation won't be easy to achieve here. We have
134 * three events that control USB requests submission:
135 *
136 * - USB request completion: the completion handler will resubmit the request
137 * if a video buffer is available.
138 *
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.
142 *
143 * - V4L2 buffer queueing: the driver will start streaming if video is not
144 * currently streaming.
145 *
146 * Race conditions between those 3 events might lead to deadlocks or other
147 * nasty side effects.
148 *
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.
152 *
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.
155 *
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.
161 */
162static void
163uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
164{
165 struct uvc_video *video = req->context;
166 struct uvc_video_queue *queue = &video->queue;
167 struct uvc_buffer *buf;
168 unsigned long flags;
169 int ret;
170
171 switch (req->status) {
172 case 0:
173 break;
174
175 case -ESHUTDOWN: /* disconnect from host. */
176 printk(KERN_DEBUG "VS request cancelled.\n");
177 uvcg_queue_cancel(queue, 1);
178 goto requeue;
179
180 default:
181 printk(KERN_INFO "VS request completed with status %d.\n",
182 req->status);
183 uvcg_queue_cancel(queue, 0);
184 goto requeue;
185 }
186
187 spin_lock_irqsave(&video->queue.irqlock, flags);
188 buf = uvcg_queue_head(&video->queue);
189 if (buf == NULL) {
190 spin_unlock_irqrestore(&video->queue.irqlock, flags);
191 goto requeue;
192 }
193
194 video->encode(req, video, buf);
195
196 if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
197 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
198 usb_ep_set_halt(ep);
199 spin_unlock_irqrestore(&video->queue.irqlock, flags);
200 uvcg_queue_cancel(queue, 0);
201 goto requeue;
202 }
203 spin_unlock_irqrestore(&video->queue.irqlock, flags);
204
205 return;
206
207requeue:
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);
211}
212
213static int
214uvc_video_free_requests(struct uvc_video *video)
215{
216 unsigned int i;
217
218 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
219 if (video->req[i]) {
220 usb_ep_free_request(video->ep, video->req[i]);
221 video->req[i] = NULL;
222 }
223
224 if (video->req_buffer[i]) {
225 kfree(video->req_buffer[i]);
226 video->req_buffer[i] = NULL;
227 }
228 }
229
230 INIT_LIST_HEAD(&video->req_free);
231 video->req_size = 0;
232 return 0;
233}
234
235static int
236uvc_video_alloc_requests(struct uvc_video *video)
237{
238 unsigned int req_size;
239 unsigned int i;
240 int ret = -ENOMEM;
241
242 BUG_ON(video->req_size);
243
244 req_size = video->ep->maxpacket
245 * max_t(unsigned int, video->ep->maxburst, 1)
246 * (video->ep->mult + 1);
247
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)
251 goto error;
252
253 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
254 if (video->req[i] == NULL)
255 goto error;
256
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;
261
262 list_add_tail(&video->req[i]->list, &video->req_free);
263 }
264
265 video->req_size = req_size;
266
267 return 0;
268
269error:
270 uvc_video_free_requests(video);
271 return ret;
272}
273
274/* --------------------------------------------------------------------------
275 * Video streaming
276 */
277
278/*
279 * uvcg_video_pump - Pump video data into the USB requests
280 *
281 * This function fills the available USB requests (listed in req_free) with
282 * video data from the queued buffers.
283 */
284int uvcg_video_pump(struct uvc_video *video)
285{
286 struct uvc_video_queue *queue = &video->queue;
287 struct usb_request *req;
288 struct uvc_buffer *buf;
289 unsigned long flags;
290 int ret;
291
292 /* FIXME TODO Race between uvcg_video_pump and requests completion
293 * handler ???
294 */
295
296 while (1) {
297 /* Retrieve the first available USB request, protected by the
298 * request lock.
299 */
300 spin_lock_irqsave(&video->req_lock, flags);
301 if (list_empty(&video->req_free)) {
302 spin_unlock_irqrestore(&video->req_lock, flags);
303 return 0;
304 }
305 req = list_first_entry(&video->req_free, struct usb_request,
306 list);
307 list_del(&req->list);
308 spin_unlock_irqrestore(&video->req_lock, flags);
309
310 /* Retrieve the first available video buffer and fill the
311 * request, protected by the video queue irqlock.
312 */
313 spin_lock_irqsave(&queue->irqlock, flags);
314 buf = uvcg_queue_head(queue);
315 if (buf == NULL) {
316 spin_unlock_irqrestore(&queue->irqlock, flags);
317 break;
318 }
319
320 video->encode(req, video, buf);
321
322 /* Queue the USB request */
323 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
324 if (ret < 0) {
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);
329 break;
330 }
331 spin_unlock_irqrestore(&queue->irqlock, flags);
332 }
333
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);
337 return 0;
338}
339
340/*
341 * Enable or disable the video stream.
342 */
343int uvcg_video_enable(struct uvc_video *video, int enable)
344{
345 unsigned int i;
346 int ret;
347
348 if (video->ep == NULL) {
349 printk(KERN_INFO "Video enable failed, device is "
350 "uninitialized.\n");
351 return -ENODEV;
352 }
353
354 if (!enable) {
355 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
356 if (video->req[i])
357 usb_ep_dequeue(video->ep, video->req[i]);
358
359 uvc_video_free_requests(video);
360 uvcg_queue_enable(&video->queue, 0);
361 return 0;
362 }
363
364 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
365 return ret;
366
367 if ((ret = uvc_video_alloc_requests(video)) < 0)
368 return ret;
369
370 if (video->max_payload_size) {
371 video->encode = uvc_video_encode_bulk;
372 video->payload_size = 0;
373 } else
374 video->encode = uvc_video_encode_isoc;
375
376 return uvcg_video_pump(video);
377}
378
379/*
380 * Initialize the UVC video stream.
381 */
382int uvcg_video_init(struct uvc_video *video)
383{
384 INIT_LIST_HEAD(&video->req_free);
385 spin_lock_init(&video->req_lock);
386
387 video->fcc = V4L2_PIX_FMT_YUYV;
388 video->bpp = 16;
389 video->width = 320;
390 video->height = 240;
391 video->imagesize = 320 * 240 * 2;
392
393 /* Initialize the video buffers queue. */
394 uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
395 &video->mutex);
396 return 0;
397}
398
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * uvc_video.c -- USB Video Class Gadget driver
4 *
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 */
8
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>
15#include <asm/unaligned.h>
16
17#include <media/v4l2-dev.h>
18
19#include "uvc.h"
20#include "uvc_queue.h"
21#include "uvc_video.h"
22
23/* --------------------------------------------------------------------------
24 * Video codecs
25 */
26
27static int
28uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
29 u8 *data, int len)
30{
31 struct uvc_device *uvc = container_of(video, struct uvc_device, video);
32 struct usb_composite_dev *cdev = uvc->func.config->cdev;
33 struct timespec64 ts = ns_to_timespec64(buf->buf.vb2_buf.timestamp);
34 int pos = 2;
35
36 data[1] = UVC_STREAM_EOH | video->fid;
37
38 if (video->queue.buf_used == 0 && ts.tv_sec) {
39 /* dwClockFrequency is 48 MHz */
40 u32 pts = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
41
42 data[1] |= UVC_STREAM_PTS;
43 put_unaligned_le32(pts, &data[pos]);
44 pos += 4;
45 }
46
47 if (cdev->gadget->ops->get_frame) {
48 u32 sof, stc;
49
50 sof = usb_gadget_frame_number(cdev->gadget);
51 ktime_get_ts64(&ts);
52 stc = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
53
54 data[1] |= UVC_STREAM_SCR;
55 put_unaligned_le32(stc, &data[pos]);
56 put_unaligned_le16(sof, &data[pos+4]);
57 pos += 6;
58 }
59
60 data[0] = pos;
61
62 if (buf->bytesused - video->queue.buf_used <= len - pos)
63 data[1] |= UVC_STREAM_EOF;
64
65 return pos;
66}
67
68static int
69uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
70 u8 *data, int len)
71{
72 struct uvc_video_queue *queue = &video->queue;
73 unsigned int nbytes;
74 void *mem;
75
76 /* Copy video data to the USB buffer. */
77 mem = buf->mem + queue->buf_used;
78 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
79
80 memcpy(data, mem, nbytes);
81 queue->buf_used += nbytes;
82
83 return nbytes;
84}
85
86static void
87uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
88 struct uvc_buffer *buf)
89{
90 void *mem = req->buf;
91 struct uvc_request *ureq = req->context;
92 int len = video->req_size;
93 int ret;
94
95 /* Add a header at the beginning of the payload. */
96 if (video->payload_size == 0) {
97 ret = uvc_video_encode_header(video, buf, mem, len);
98 video->payload_size += ret;
99 mem += ret;
100 len -= ret;
101 }
102
103 /* Process video data. */
104 len = min((int)(video->max_payload_size - video->payload_size), len);
105 ret = uvc_video_encode_data(video, buf, mem, len);
106
107 video->payload_size += ret;
108 len -= ret;
109
110 req->length = video->req_size - len;
111 req->zero = video->payload_size == video->max_payload_size;
112
113 if (buf->bytesused == video->queue.buf_used) {
114 video->queue.buf_used = 0;
115 buf->state = UVC_BUF_STATE_DONE;
116 list_del(&buf->queue);
117 video->fid ^= UVC_STREAM_FID;
118 ureq->last_buf = buf;
119
120 video->payload_size = 0;
121 }
122
123 if (video->payload_size == video->max_payload_size ||
124 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE ||
125 buf->bytesused == video->queue.buf_used)
126 video->payload_size = 0;
127}
128
129static void
130uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
131 struct uvc_buffer *buf)
132{
133 unsigned int pending = buf->bytesused - video->queue.buf_used;
134 struct uvc_request *ureq = req->context;
135 struct scatterlist *sg, *iter;
136 unsigned int len = video->req_size;
137 unsigned int sg_left, part = 0;
138 unsigned int i;
139 int header_len;
140
141 sg = ureq->sgt.sgl;
142 sg_init_table(sg, ureq->sgt.nents);
143
144 /* Init the header. */
145 header_len = uvc_video_encode_header(video, buf, ureq->header,
146 video->req_size);
147 sg_set_buf(sg, ureq->header, header_len);
148 len -= header_len;
149
150 if (pending <= len)
151 len = pending;
152
153 req->length = (len == pending) ?
154 len + header_len : video->req_size;
155
156 /* Init the pending sgs with payload */
157 sg = sg_next(sg);
158
159 for_each_sg(sg, iter, ureq->sgt.nents - 1, i) {
160 if (!len || !buf->sg || !buf->sg->length)
161 break;
162
163 sg_left = buf->sg->length - buf->offset;
164 part = min_t(unsigned int, len, sg_left);
165
166 sg_set_page(iter, sg_page(buf->sg), part, buf->offset);
167
168 if (part == sg_left) {
169 buf->offset = 0;
170 buf->sg = sg_next(buf->sg);
171 } else {
172 buf->offset += part;
173 }
174 len -= part;
175 }
176
177 /* Assign the video data with header. */
178 req->buf = NULL;
179 req->sg = ureq->sgt.sgl;
180 req->num_sgs = i + 1;
181
182 req->length -= len;
183 video->queue.buf_used += req->length - header_len;
184
185 if (buf->bytesused == video->queue.buf_used || !buf->sg ||
186 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
187 video->queue.buf_used = 0;
188 buf->state = UVC_BUF_STATE_DONE;
189 buf->offset = 0;
190 list_del(&buf->queue);
191 video->fid ^= UVC_STREAM_FID;
192 ureq->last_buf = buf;
193 }
194}
195
196static void
197uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
198 struct uvc_buffer *buf)
199{
200 void *mem = req->buf;
201 struct uvc_request *ureq = req->context;
202 int len = video->req_size;
203 int ret;
204
205 /* Add the header. */
206 ret = uvc_video_encode_header(video, buf, mem, len);
207 mem += ret;
208 len -= ret;
209
210 /* Process video data. */
211 ret = uvc_video_encode_data(video, buf, mem, len);
212 len -= ret;
213
214 req->length = video->req_size - len;
215
216 if (buf->bytesused == video->queue.buf_used ||
217 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
218 video->queue.buf_used = 0;
219 buf->state = UVC_BUF_STATE_DONE;
220 list_del(&buf->queue);
221 video->fid ^= UVC_STREAM_FID;
222 ureq->last_buf = buf;
223 }
224}
225
226/* --------------------------------------------------------------------------
227 * Request handling
228 */
229
230static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
231{
232 int ret;
233
234 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
235 if (ret < 0) {
236 uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
237 ret);
238
239 /* If the endpoint is disabled the descriptor may be NULL. */
240 if (video->ep->desc) {
241 /* Isochronous endpoints can't be halted. */
242 if (usb_endpoint_xfer_bulk(video->ep->desc))
243 usb_ep_set_halt(video->ep);
244 }
245 }
246
247 return ret;
248}
249
250static void
251uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
252{
253 struct uvc_request *ureq = req->context;
254 struct uvc_video *video = ureq->video;
255 struct uvc_video_queue *queue = &video->queue;
256 struct uvc_device *uvc = video->uvc;
257 unsigned long flags;
258
259 switch (req->status) {
260 case 0:
261 break;
262
263 case -EXDEV:
264 uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n");
265 queue->flags |= UVC_QUEUE_DROP_INCOMPLETE;
266 break;
267
268 case -ESHUTDOWN: /* disconnect from host. */
269 uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
270 uvcg_queue_cancel(queue, 1);
271 break;
272
273 default:
274 uvcg_warn(&video->uvc->func,
275 "VS request completed with status %d.\n",
276 req->status);
277 uvcg_queue_cancel(queue, 0);
278 }
279
280 if (ureq->last_buf) {
281 uvcg_complete_buffer(&video->queue, ureq->last_buf);
282 ureq->last_buf = NULL;
283 }
284
285 spin_lock_irqsave(&video->req_lock, flags);
286 list_add_tail(&req->list, &video->req_free);
287 spin_unlock_irqrestore(&video->req_lock, flags);
288
289 if (uvc->state == UVC_STATE_STREAMING)
290 queue_work(video->async_wq, &video->pump);
291}
292
293static int
294uvc_video_free_requests(struct uvc_video *video)
295{
296 unsigned int i;
297
298 if (video->ureq) {
299 for (i = 0; i < video->uvc_num_requests; ++i) {
300 sg_free_table(&video->ureq[i].sgt);
301
302 if (video->ureq[i].req) {
303 usb_ep_free_request(video->ep, video->ureq[i].req);
304 video->ureq[i].req = NULL;
305 }
306
307 if (video->ureq[i].req_buffer) {
308 kfree(video->ureq[i].req_buffer);
309 video->ureq[i].req_buffer = NULL;
310 }
311 }
312
313 kfree(video->ureq);
314 video->ureq = NULL;
315 }
316
317 INIT_LIST_HEAD(&video->req_free);
318 video->req_size = 0;
319 return 0;
320}
321
322static int
323uvc_video_alloc_requests(struct uvc_video *video)
324{
325 unsigned int req_size;
326 unsigned int i;
327 int ret = -ENOMEM;
328
329 BUG_ON(video->req_size);
330
331 req_size = video->ep->maxpacket
332 * max_t(unsigned int, video->ep->maxburst, 1)
333 * (video->ep->mult);
334
335 video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
336 if (video->ureq == NULL)
337 return -ENOMEM;
338
339 for (i = 0; i < video->uvc_num_requests; ++i) {
340 video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
341 if (video->ureq[i].req_buffer == NULL)
342 goto error;
343
344 video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
345 if (video->ureq[i].req == NULL)
346 goto error;
347
348 video->ureq[i].req->buf = video->ureq[i].req_buffer;
349 video->ureq[i].req->length = 0;
350 video->ureq[i].req->complete = uvc_video_complete;
351 video->ureq[i].req->context = &video->ureq[i];
352 video->ureq[i].video = video;
353 video->ureq[i].last_buf = NULL;
354
355 list_add_tail(&video->ureq[i].req->list, &video->req_free);
356 /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
357 sg_alloc_table(&video->ureq[i].sgt,
358 DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
359 PAGE_SIZE) + 2, GFP_KERNEL);
360 }
361
362 video->req_size = req_size;
363
364 return 0;
365
366error:
367 uvc_video_free_requests(video);
368 return ret;
369}
370
371/* --------------------------------------------------------------------------
372 * Video streaming
373 */
374
375/*
376 * uvcg_video_pump - Pump video data into the USB requests
377 *
378 * This function fills the available USB requests (listed in req_free) with
379 * video data from the queued buffers.
380 */
381static void uvcg_video_pump(struct work_struct *work)
382{
383 struct uvc_video *video = container_of(work, struct uvc_video, pump);
384 struct uvc_video_queue *queue = &video->queue;
385 struct usb_request *req = NULL;
386 struct uvc_buffer *buf;
387 unsigned long flags;
388 int ret;
389
390 while (video->ep->enabled) {
391 /*
392 * Retrieve the first available USB request, protected by the
393 * request lock.
394 */
395 spin_lock_irqsave(&video->req_lock, flags);
396 if (list_empty(&video->req_free)) {
397 spin_unlock_irqrestore(&video->req_lock, flags);
398 return;
399 }
400 req = list_first_entry(&video->req_free, struct usb_request,
401 list);
402 list_del(&req->list);
403 spin_unlock_irqrestore(&video->req_lock, flags);
404
405 /*
406 * Retrieve the first available video buffer and fill the
407 * request, protected by the video queue irqlock.
408 */
409 spin_lock_irqsave(&queue->irqlock, flags);
410 buf = uvcg_queue_head(queue);
411 if (buf == NULL) {
412 spin_unlock_irqrestore(&queue->irqlock, flags);
413 break;
414 }
415
416 video->encode(req, video, buf);
417
418 /*
419 * With usb3 we have more requests. This will decrease the
420 * interrupt load to a quarter but also catches the corner
421 * cases, which needs to be handled.
422 */
423 if (list_empty(&video->req_free) ||
424 buf->state == UVC_BUF_STATE_DONE ||
425 !(video->req_int_count %
426 DIV_ROUND_UP(video->uvc_num_requests, 4))) {
427 video->req_int_count = 0;
428 req->no_interrupt = 0;
429 } else {
430 req->no_interrupt = 1;
431 }
432
433 /* Queue the USB request */
434 ret = uvcg_video_ep_queue(video, req);
435 spin_unlock_irqrestore(&queue->irqlock, flags);
436
437 if (ret < 0) {
438 uvcg_queue_cancel(queue, 0);
439 break;
440 }
441
442 /* Endpoint now owns the request */
443 req = NULL;
444 if (buf->state != UVC_BUF_STATE_DONE)
445 video->req_int_count++;
446 }
447
448 if (!req)
449 return;
450
451 spin_lock_irqsave(&video->req_lock, flags);
452 list_add_tail(&req->list, &video->req_free);
453 spin_unlock_irqrestore(&video->req_lock, flags);
454 return;
455}
456
457/*
458 * Enable or disable the video stream.
459 */
460int uvcg_video_enable(struct uvc_video *video, int enable)
461{
462 unsigned int i;
463 int ret;
464
465 if (video->ep == NULL) {
466 uvcg_info(&video->uvc->func,
467 "Video enable failed, device is uninitialized.\n");
468 return -ENODEV;
469 }
470
471 if (!enable) {
472 cancel_work_sync(&video->pump);
473 uvcg_queue_cancel(&video->queue, 0);
474
475 for (i = 0; i < video->uvc_num_requests; ++i)
476 if (video->ureq && video->ureq[i].req)
477 usb_ep_dequeue(video->ep, video->ureq[i].req);
478
479 uvc_video_free_requests(video);
480 uvcg_queue_enable(&video->queue, 0);
481 return 0;
482 }
483
484 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
485 return ret;
486
487 if ((ret = uvc_video_alloc_requests(video)) < 0)
488 return ret;
489
490 if (video->max_payload_size) {
491 video->encode = uvc_video_encode_bulk;
492 video->payload_size = 0;
493 } else
494 video->encode = video->queue.use_sg ?
495 uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
496
497 video->req_int_count = 0;
498
499 queue_work(video->async_wq, &video->pump);
500
501 return ret;
502}
503
504/*
505 * Initialize the UVC video stream.
506 */
507int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
508{
509 INIT_LIST_HEAD(&video->req_free);
510 spin_lock_init(&video->req_lock);
511 INIT_WORK(&video->pump, uvcg_video_pump);
512
513 /* Allocate a work queue for asynchronous video pump handler. */
514 video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
515 if (!video->async_wq)
516 return -EINVAL;
517
518 video->uvc = uvc;
519 video->fcc = V4L2_PIX_FMT_YUYV;
520 video->bpp = 16;
521 video->width = 320;
522 video->height = 240;
523 video->imagesize = 320 * 240 * 2;
524
525 /* Initialize the video buffers queue. */
526 uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
527 V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);
528 return 0;
529}
530