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 
25 /* --------------------------------------------------------------------------
26  * Video codecs
27  */
28 
29 static int
30 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
31 		u8 *data, int len)
32 {
33 	data[0] = 2;
34 	data[1] = UVC_STREAM_EOH | video->fid;
35 
36 	if (buf->bytesused - video->queue.buf_used <= len - 2)
37 		data[1] |= UVC_STREAM_EOF;
38 
39 	return 2;
40 }
41 
42 static int
43 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
44 		u8 *data, int len)
45 {
46 	struct uvc_video_queue *queue = &video->queue;
47 	unsigned int nbytes;
48 	void *mem;
49 
50 	/* Copy video data to the USB buffer. */
51 	mem = buf->mem + queue->buf_used;
52 	nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
53 
54 	memcpy(data, mem, nbytes);
55 	queue->buf_used += nbytes;
56 
57 	return nbytes;
58 }
59 
60 static void
61 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
62 		struct uvc_buffer *buf)
63 {
64 	void *mem = req->buf;
65 	int len = video->req_size;
66 	int ret;
67 
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;
74 	}
75 
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);
79 
80 	video->payload_size += ret;
81 	len -= ret;
82 
83 	req->length = video->req_size - len;
84 	req->zero = video->payload_size == video->max_payload_size;
85 
86 	if (buf->bytesused == video->queue.buf_used) {
87 		video->queue.buf_used = 0;
88 		buf->state = UVC_BUF_STATE_DONE;
89 		uvcg_queue_next_buffer(&video->queue, buf);
90 		video->fid ^= UVC_STREAM_FID;
91 
92 		video->payload_size = 0;
93 	}
94 
95 	if (video->payload_size == video->max_payload_size ||
96 	    buf->bytesused == video->queue.buf_used)
97 		video->payload_size = 0;
98 }
99 
100 static void
101 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
102 		struct uvc_buffer *buf)
103 {
104 	void *mem = req->buf;
105 	int len = video->req_size;
106 	int ret;
107 
108 	/* Add the header. */
109 	ret = uvc_video_encode_header(video, buf, mem, len);
110 	mem += ret;
111 	len -= ret;
112 
113 	/* Process video data. */
114 	ret = uvc_video_encode_data(video, buf, mem, len);
115 	len -= ret;
116 
117 	req->length = video->req_size - len;
118 
119 	if (buf->bytesused == video->queue.buf_used) {
120 		video->queue.buf_used = 0;
121 		buf->state = UVC_BUF_STATE_DONE;
122 		uvcg_queue_next_buffer(&video->queue, buf);
123 		video->fid ^= UVC_STREAM_FID;
124 	}
125 }
126 
127 /* --------------------------------------------------------------------------
128  * Request handling
129  */
130 
131 /*
132  * I somehow feel that synchronisation won't be easy to achieve here. We have
133  * three events that control USB requests submission:
134  *
135  * - USB request completion: the completion handler will resubmit the request
136  *   if a video buffer is available.
137  *
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.
141  *
142  * - V4L2 buffer queueing: the driver will start streaming if video is not
143  *   currently streaming.
144  *
145  * Race conditions between those 3 events might lead to deadlocks or other
146  * nasty side effects.
147  *
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.
151  *
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.
154  *
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.
160  */
161 static void
162 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
163 {
164 	struct uvc_video *video = req->context;
165 	struct uvc_video_queue *queue = &video->queue;
166 	struct uvc_buffer *buf;
167 	unsigned long flags;
168 	int ret;
169 
170 	switch (req->status) {
171 	case 0:
172 		break;
173 
174 	case -ESHUTDOWN:	/* disconnect from host. */
175 		printk(KERN_DEBUG "VS request cancelled.\n");
176 		uvcg_queue_cancel(queue, 1);
177 		goto requeue;
178 
179 	default:
180 		printk(KERN_INFO "VS request completed with status %d.\n",
181 			req->status);
182 		uvcg_queue_cancel(queue, 0);
183 		goto requeue;
184 	}
185 
186 	spin_lock_irqsave(&video->queue.irqlock, flags);
187 	buf = uvcg_queue_head(&video->queue);
188 	if (buf == NULL) {
189 		spin_unlock_irqrestore(&video->queue.irqlock, flags);
190 		goto requeue;
191 	}
192 
193 	video->encode(req, video, buf);
194 
195 	if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
196 		printk(KERN_INFO "Failed to queue request (%d).\n", ret);
197 		usb_ep_set_halt(ep);
198 		spin_unlock_irqrestore(&video->queue.irqlock, flags);
199 		uvcg_queue_cancel(queue, 0);
200 		goto requeue;
201 	}
202 	spin_unlock_irqrestore(&video->queue.irqlock, flags);
203 
204 	return;
205 
206 requeue:
207 	spin_lock_irqsave(&video->req_lock, flags);
208 	list_add_tail(&req->list, &video->req_free);
209 	spin_unlock_irqrestore(&video->req_lock, flags);
210 }
211 
212 static int
213 uvc_video_free_requests(struct uvc_video *video)
214 {
215 	unsigned int i;
216 
217 	for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
218 		if (video->req[i]) {
219 			usb_ep_free_request(video->ep, video->req[i]);
220 			video->req[i] = NULL;
221 		}
222 
223 		if (video->req_buffer[i]) {
224 			kfree(video->req_buffer[i]);
225 			video->req_buffer[i] = NULL;
226 		}
227 	}
228 
229 	INIT_LIST_HEAD(&video->req_free);
230 	video->req_size = 0;
231 	return 0;
232 }
233 
234 static int
235 uvc_video_alloc_requests(struct uvc_video *video)
236 {
237 	unsigned int req_size;
238 	unsigned int i;
239 	int ret = -ENOMEM;
240 
241 	BUG_ON(video->req_size);
242 
243 	req_size = video->ep->maxpacket
244 		 * max_t(unsigned int, video->ep->maxburst, 1)
245 		 * (video->ep->mult + 1);
246 
247 	for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
248 		video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
249 		if (video->req_buffer[i] == NULL)
250 			goto error;
251 
252 		video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
253 		if (video->req[i] == NULL)
254 			goto error;
255 
256 		video->req[i]->buf = video->req_buffer[i];
257 		video->req[i]->length = 0;
258 		video->req[i]->complete = uvc_video_complete;
259 		video->req[i]->context = video;
260 
261 		list_add_tail(&video->req[i]->list, &video->req_free);
262 	}
263 
264 	video->req_size = req_size;
265 
266 	return 0;
267 
268 error:
269 	uvc_video_free_requests(video);
270 	return ret;
271 }
272 
273 /* --------------------------------------------------------------------------
274  * Video streaming
275  */
276 
277 /*
278  * uvcg_video_pump - Pump video data into the USB requests
279  *
280  * This function fills the available USB requests (listed in req_free) with
281  * video data from the queued buffers.
282  */
283 int uvcg_video_pump(struct uvc_video *video)
284 {
285 	struct uvc_video_queue *queue = &video->queue;
286 	struct usb_request *req;
287 	struct uvc_buffer *buf;
288 	unsigned long flags;
289 	int ret;
290 
291 	/* FIXME TODO Race between uvcg_video_pump and requests completion
292 	 * handler ???
293 	 */
294 
295 	while (1) {
296 		/* Retrieve the first available USB request, protected by the
297 		 * request lock.
298 		 */
299 		spin_lock_irqsave(&video->req_lock, flags);
300 		if (list_empty(&video->req_free)) {
301 			spin_unlock_irqrestore(&video->req_lock, flags);
302 			return 0;
303 		}
304 		req = list_first_entry(&video->req_free, struct usb_request,
305 					list);
306 		list_del(&req->list);
307 		spin_unlock_irqrestore(&video->req_lock, flags);
308 
309 		/* Retrieve the first available video buffer and fill the
310 		 * request, protected by the video queue irqlock.
311 		 */
312 		spin_lock_irqsave(&queue->irqlock, flags);
313 		buf = uvcg_queue_head(queue);
314 		if (buf == NULL) {
315 			spin_unlock_irqrestore(&queue->irqlock, flags);
316 			break;
317 		}
318 
319 		video->encode(req, video, buf);
320 
321 		/* Queue the USB request */
322 		ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
323 		if (ret < 0) {
324 			printk(KERN_INFO "Failed to queue request (%d)\n", ret);
325 			usb_ep_set_halt(video->ep);
326 			spin_unlock_irqrestore(&queue->irqlock, flags);
327 			uvcg_queue_cancel(queue, 0);
328 			break;
329 		}
330 		spin_unlock_irqrestore(&queue->irqlock, flags);
331 	}
332 
333 	spin_lock_irqsave(&video->req_lock, flags);
334 	list_add_tail(&req->list, &video->req_free);
335 	spin_unlock_irqrestore(&video->req_lock, flags);
336 	return 0;
337 }
338 
339 /*
340  * Enable or disable the video stream.
341  */
342 int uvcg_video_enable(struct uvc_video *video, int enable)
343 {
344 	unsigned int i;
345 	int ret;
346 
347 	if (video->ep == NULL) {
348 		printk(KERN_INFO "Video enable failed, device is "
349 			"uninitialized.\n");
350 		return -ENODEV;
351 	}
352 
353 	if (!enable) {
354 		for (i = 0; i < UVC_NUM_REQUESTS; ++i)
355 			if (video->req[i])
356 				usb_ep_dequeue(video->ep, video->req[i]);
357 
358 		uvc_video_free_requests(video);
359 		uvcg_queue_enable(&video->queue, 0);
360 		return 0;
361 	}
362 
363 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
364 		return ret;
365 
366 	if ((ret = uvc_video_alloc_requests(video)) < 0)
367 		return ret;
368 
369 	if (video->max_payload_size) {
370 		video->encode = uvc_video_encode_bulk;
371 		video->payload_size = 0;
372 	} else
373 		video->encode = uvc_video_encode_isoc;
374 
375 	return uvcg_video_pump(video);
376 }
377 
378 /*
379  * Initialize the UVC video stream.
380  */
381 int uvcg_video_init(struct uvc_video *video)
382 {
383 	INIT_LIST_HEAD(&video->req_free);
384 	spin_lock_init(&video->req_lock);
385 
386 	video->fcc = V4L2_PIX_FMT_YUYV;
387 	video->bpp = 16;
388 	video->width = 320;
389 	video->height = 240;
390 	video->imagesize = 320 * 240 * 2;
391 
392 	/* Initialize the video buffers queue. */
393 	uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
394 	return 0;
395 }
396 
397