treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / gadget / function / uvc_video.c
blob5c042f3807081ac410c727984f0ef97bf330fda7
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
128 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
130 int ret;
132 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
133 if (ret < 0) {
134 uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
135 ret);
137 /* Isochronous endpoints can't be halted. */
138 if (usb_endpoint_xfer_bulk(video->ep->desc))
139 usb_ep_set_halt(video->ep);
142 return ret;
146 * I somehow feel that synchronisation won't be easy to achieve here. We have
147 * three events that control USB requests submission:
149 * - USB request completion: the completion handler will resubmit the request
150 * if a video buffer is available.
152 * - USB interface setting selection: in response to a SET_INTERFACE request,
153 * the handler will start streaming if a video buffer is available and if
154 * video is not currently streaming.
156 * - V4L2 buffer queueing: the driver will start streaming if video is not
157 * currently streaming.
159 * Race conditions between those 3 events might lead to deadlocks or other
160 * nasty side effects.
162 * The "video currently streaming" condition can't be detected by the irqqueue
163 * being empty, as a request can still be in flight. A separate "queue paused"
164 * flag is thus needed.
166 * The paused flag will be set when we try to retrieve the irqqueue head if the
167 * queue is empty, and cleared when we queue a buffer.
169 * The USB request completion handler will get the buffer at the irqqueue head
170 * under protection of the queue spinlock. If the queue is empty, the streaming
171 * paused flag will be set. Right after releasing the spinlock a userspace
172 * application can queue a buffer. The flag will then cleared, and the ioctl
173 * handler will restart the video stream.
175 static void
176 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
178 struct uvc_video *video = req->context;
179 struct uvc_video_queue *queue = &video->queue;
180 struct uvc_buffer *buf;
181 unsigned long flags;
182 int ret;
184 switch (req->status) {
185 case 0:
186 break;
188 case -ESHUTDOWN: /* disconnect from host. */
189 uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
190 uvcg_queue_cancel(queue, 1);
191 goto requeue;
193 default:
194 uvcg_info(&video->uvc->func,
195 "VS request completed with status %d.\n",
196 req->status);
197 uvcg_queue_cancel(queue, 0);
198 goto requeue;
201 spin_lock_irqsave(&video->queue.irqlock, flags);
202 buf = uvcg_queue_head(&video->queue);
203 if (buf == NULL) {
204 spin_unlock_irqrestore(&video->queue.irqlock, flags);
205 goto requeue;
208 video->encode(req, video, buf);
210 ret = uvcg_video_ep_queue(video, req);
211 spin_unlock_irqrestore(&video->queue.irqlock, flags);
213 if (ret < 0) {
214 uvcg_queue_cancel(queue, 0);
215 goto requeue;
218 return;
220 requeue:
221 spin_lock_irqsave(&video->req_lock, flags);
222 list_add_tail(&req->list, &video->req_free);
223 spin_unlock_irqrestore(&video->req_lock, flags);
226 static int
227 uvc_video_free_requests(struct uvc_video *video)
229 unsigned int i;
231 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
232 if (video->req[i]) {
233 usb_ep_free_request(video->ep, video->req[i]);
234 video->req[i] = NULL;
237 if (video->req_buffer[i]) {
238 kfree(video->req_buffer[i]);
239 video->req_buffer[i] = NULL;
243 INIT_LIST_HEAD(&video->req_free);
244 video->req_size = 0;
245 return 0;
248 static int
249 uvc_video_alloc_requests(struct uvc_video *video)
251 unsigned int req_size;
252 unsigned int i;
253 int ret = -ENOMEM;
255 BUG_ON(video->req_size);
257 req_size = video->ep->maxpacket
258 * max_t(unsigned int, video->ep->maxburst, 1)
259 * (video->ep->mult);
261 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
262 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
263 if (video->req_buffer[i] == NULL)
264 goto error;
266 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
267 if (video->req[i] == NULL)
268 goto error;
270 video->req[i]->buf = video->req_buffer[i];
271 video->req[i]->length = 0;
272 video->req[i]->complete = uvc_video_complete;
273 video->req[i]->context = video;
275 list_add_tail(&video->req[i]->list, &video->req_free);
278 video->req_size = req_size;
280 return 0;
282 error:
283 uvc_video_free_requests(video);
284 return ret;
287 /* --------------------------------------------------------------------------
288 * Video streaming
292 * uvcg_video_pump - Pump video data into the USB requests
294 * This function fills the available USB requests (listed in req_free) with
295 * video data from the queued buffers.
297 int uvcg_video_pump(struct uvc_video *video)
299 struct uvc_video_queue *queue = &video->queue;
300 struct usb_request *req;
301 struct uvc_buffer *buf;
302 unsigned long flags;
303 int ret;
305 /* FIXME TODO Race between uvcg_video_pump and requests completion
306 * handler ???
309 while (1) {
310 /* Retrieve the first available USB request, protected by the
311 * request lock.
313 spin_lock_irqsave(&video->req_lock, flags);
314 if (list_empty(&video->req_free)) {
315 spin_unlock_irqrestore(&video->req_lock, flags);
316 return 0;
318 req = list_first_entry(&video->req_free, struct usb_request,
319 list);
320 list_del(&req->list);
321 spin_unlock_irqrestore(&video->req_lock, flags);
323 /* Retrieve the first available video buffer and fill the
324 * request, protected by the video queue irqlock.
326 spin_lock_irqsave(&queue->irqlock, flags);
327 buf = uvcg_queue_head(queue);
328 if (buf == NULL) {
329 spin_unlock_irqrestore(&queue->irqlock, flags);
330 break;
333 video->encode(req, video, buf);
335 /* Queue the USB request */
336 ret = uvcg_video_ep_queue(video, req);
337 spin_unlock_irqrestore(&queue->irqlock, flags);
339 if (ret < 0) {
340 uvcg_queue_cancel(queue, 0);
341 break;
345 spin_lock_irqsave(&video->req_lock, flags);
346 list_add_tail(&req->list, &video->req_free);
347 spin_unlock_irqrestore(&video->req_lock, flags);
348 return 0;
352 * Enable or disable the video stream.
354 int uvcg_video_enable(struct uvc_video *video, int enable)
356 unsigned int i;
357 int ret;
359 if (video->ep == NULL) {
360 uvcg_info(&video->uvc->func,
361 "Video enable failed, device is uninitialized.\n");
362 return -ENODEV;
365 if (!enable) {
366 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
367 if (video->req[i])
368 usb_ep_dequeue(video->ep, video->req[i]);
370 uvc_video_free_requests(video);
371 uvcg_queue_enable(&video->queue, 0);
372 return 0;
375 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
376 return ret;
378 if ((ret = uvc_video_alloc_requests(video)) < 0)
379 return ret;
381 if (video->max_payload_size) {
382 video->encode = uvc_video_encode_bulk;
383 video->payload_size = 0;
384 } else
385 video->encode = uvc_video_encode_isoc;
387 return uvcg_video_pump(video);
391 * Initialize the UVC video stream.
393 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
395 INIT_LIST_HEAD(&video->req_free);
396 spin_lock_init(&video->req_lock);
398 video->uvc = uvc;
399 video->fcc = V4L2_PIX_FMT_YUYV;
400 video->bpp = 16;
401 video->width = 320;
402 video->height = 240;
403 video->imagesize = 320 * 240 * 2;
405 /* Initialize the video buffers queue. */
406 uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
407 &video->mutex);
408 return 0;