Linux Audio

Check our new training course

Loading...
v6.8
  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
230/*
231 * Callers must take care to hold req_lock when this function may be called
232 * from multiple threads. For example, when frames are streaming to the host.
233 */
234static void
235uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
236{
237	sg_free_table(&ureq->sgt);
238	if (ureq->req && ep) {
239		usb_ep_free_request(ep, ureq->req);
240		ureq->req = NULL;
241	}
242
243	kfree(ureq->req_buffer);
244	ureq->req_buffer = NULL;
245
246	if (!list_empty(&ureq->list))
247		list_del_init(&ureq->list);
248
249	kfree(ureq);
250}
251
252static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
253{
254	int ret;
255
256	ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
257	if (ret < 0) {
258		uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
259			 ret);
260
261		/* If the endpoint is disabled the descriptor may be NULL. */
262		if (video->ep->desc) {
263			/* Isochronous endpoints can't be halted. */
264			if (usb_endpoint_xfer_bulk(video->ep->desc))
265				usb_ep_set_halt(video->ep);
266		}
267	}
268
269	return ret;
270}
271
272/* This function must be called with video->req_lock held. */
273static int uvcg_video_usb_req_queue(struct uvc_video *video,
274	struct usb_request *req, bool queue_to_ep)
275{
276	bool is_bulk = video->max_payload_size;
277	struct list_head *list = NULL;
278
279	if (!video->is_enabled)
280		return -ENODEV;
281
282	if (queue_to_ep) {
283		struct uvc_request *ureq = req->context;
284		/*
285		 * With USB3 handling more requests at a higher speed, we can't
286		 * afford to generate an interrupt for every request. Decide to
287		 * interrupt:
288		 *
289		 * - When no more requests are available in the free queue, as
290		 *   this may be our last chance to refill the endpoint's
291		 *   request queue.
292		 *
293		 * - When this is request is the last request for the video
294		 *   buffer, as we want to start sending the next video buffer
295		 *   ASAP in case it doesn't get started already in the next
296		 *   iteration of this loop.
297		 *
298		 * - Four times over the length of the requests queue (as
299		 *   indicated by video->uvc_num_requests), as a trade-off
300		 *   between latency and interrupt load.
301		 */
302		if (list_empty(&video->req_free) || ureq->last_buf ||
303			!(video->req_int_count %
304			DIV_ROUND_UP(video->uvc_num_requests, 4))) {
305			video->req_int_count = 0;
306			req->no_interrupt = 0;
307		} else {
308			req->no_interrupt = 1;
309		}
310		video->req_int_count++;
311		return uvcg_video_ep_queue(video, req);
312	}
313	/*
314	 * If we're not queuing to the ep, for isoc we're queuing
315	 * to the req_ready list, otherwise req_free.
316	 */
317	list = is_bulk ? &video->req_free : &video->req_ready;
318	list_add_tail(&req->list, list);
319	return 0;
320}
321
322/*
323 * Must only be called from uvcg_video_enable - since after that we only want to
324 * queue requests to the endpoint from the uvc_video_complete complete handler.
325 * This function is needed in order to 'kick start' the flow of requests from
326 * gadget driver to the usb controller.
327 */
328static void uvc_video_ep_queue_initial_requests(struct uvc_video *video)
329{
330	struct usb_request *req = NULL;
331	unsigned long flags = 0;
332	unsigned int count = 0;
333	int ret = 0;
334
335	/*
336	 * We only queue half of the free list since we still want to have
337	 * some free usb_requests in the free list for the video_pump async_wq
338	 * thread to encode uvc buffers into. Otherwise we could get into a
339	 * situation where the free list does not have any usb requests to
340	 * encode into - we always end up queueing 0 length requests to the
341	 * end point.
342	 */
343	unsigned int half_list_size = video->uvc_num_requests / 2;
344
345	spin_lock_irqsave(&video->req_lock, flags);
346	/*
347	 * Take these requests off the free list and queue them all to the
348	 * endpoint. Since we queue 0 length requests with the req_lock held,
349	 * there isn't any 'data' race involved here with the complete handler.
350	 */
351	while (count < half_list_size) {
352		req = list_first_entry(&video->req_free, struct usb_request,
353					list);
354		list_del(&req->list);
355		req->length = 0;
356		ret = uvcg_video_ep_queue(video, req);
357		if (ret < 0) {
358			uvcg_queue_cancel(&video->queue, 0);
359			break;
360		}
361		count++;
362	}
363	spin_unlock_irqrestore(&video->req_lock, flags);
364}
365
366static void
367uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
368{
369	struct uvc_request *ureq = req->context;
370	struct uvc_video *video = ureq->video;
371	struct uvc_video_queue *queue = &video->queue;
372	struct uvc_buffer *last_buf;
 
373	unsigned long flags;
374	bool is_bulk = video->max_payload_size;
375	int ret = 0;
376
377	spin_lock_irqsave(&video->req_lock, flags);
378	if (!video->is_enabled) {
379		/*
380		 * When is_enabled is false, uvcg_video_disable() ensures
381		 * that in-flight uvc_buffers are returned, so we can
382		 * safely call free_request without worrying about
383		 * last_buf.
384		 */
385		uvc_video_free_request(ureq, ep);
386		spin_unlock_irqrestore(&video->req_lock, flags);
387		return;
388	}
389
390	last_buf = ureq->last_buf;
391	ureq->last_buf = NULL;
392	spin_unlock_irqrestore(&video->req_lock, flags);
393
394	switch (req->status) {
395	case 0:
396		break;
397
398	case -EXDEV:
399		uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n");
400		queue->flags |= UVC_QUEUE_DROP_INCOMPLETE;
 
401		break;
402
403	case -ESHUTDOWN:	/* disconnect from host. */
404		uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
405		uvcg_queue_cancel(queue, 1);
406		break;
407
408	default:
409		uvcg_warn(&video->uvc->func,
410			  "VS request completed with status %d.\n",
411			  req->status);
412		uvcg_queue_cancel(queue, 0);
413	}
414
415	if (last_buf) {
416		spin_lock_irqsave(&queue->irqlock, flags);
417		uvcg_complete_buffer(queue, last_buf);
418		spin_unlock_irqrestore(&queue->irqlock, flags);
419	}
420
421	spin_lock_irqsave(&video->req_lock, flags);
422	/*
423	 * Video stream might have been disabled while we were
424	 * processing the current usb_request. So make sure
425	 * we're still streaming before queueing the usb_request
426	 * back to req_free
427	 */
428	if (video->is_enabled) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429		/*
430		 * Here we check whether any request is available in the ready
431		 * list. If it is, queue it to the ep and add the current
432		 * usb_request to the req_free list - for video_pump to fill in.
433		 * Otherwise, just use the current usb_request to queue a 0
434		 * length request to the ep. Since we always add to the req_free
435		 * list if we dequeue from the ready list, there will never
436		 * be a situation where the req_free list is completely out of
437		 * requests and cannot recover.
 
438		 */
439		struct usb_request *to_queue = req;
440
441		to_queue->length = 0;
442		if (!list_empty(&video->req_ready)) {
443			to_queue = list_first_entry(&video->req_ready,
444				struct usb_request, list);
445			list_del(&to_queue->list);
446			list_add_tail(&req->list, &video->req_free);
447			/*
448			 * Queue work to the wq as well since it is possible that a
449			 * buffer may not have been completely encoded with the set of
450			 * in-flight usb requests for whih the complete callbacks are
451			 * firing.
452			 * In that case, if we do not queue work to the worker thread,
453			 * the buffer will never be marked as complete - and therefore
454			 * not be returned to userpsace. As a result,
455			 * dequeue -> queue -> dequeue flow of uvc buffers will not
456			 * happen.
457			 */
458			queue_work(video->async_wq, &video->pump);
459		}
460		/*
461		 * Queue to the endpoint. The actual queueing to ep will
462		 * only happen on one thread - the async_wq for bulk endpoints
463		 * and this thread for isoc endpoints.
464		 */
465		ret = uvcg_video_usb_req_queue(video, to_queue, !is_bulk);
466		if (ret < 0) {
467			/*
468			 * Endpoint error, but the stream is still enabled.
469			 * Put request back in req_free for it to be cleaned
470			 * up later.
471			 */
472			list_add_tail(&to_queue->list, &video->req_free);
473		}
474	} else {
475		uvc_video_free_request(ureq, ep);
476		ret = 0;
477	}
 
478	spin_unlock_irqrestore(&video->req_lock, flags);
479	if (ret < 0)
480		uvcg_queue_cancel(queue, 0);
481}
482
483static int
484uvc_video_free_requests(struct uvc_video *video)
485{
486	struct uvc_request *ureq, *temp;
487
488	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
489		uvc_video_free_request(ureq, video->ep);
490
491	INIT_LIST_HEAD(&video->ureqs);
492	INIT_LIST_HEAD(&video->req_free);
493	INIT_LIST_HEAD(&video->req_ready);
494	video->req_size = 0;
495	return 0;
496}
497
498static int
499uvc_video_alloc_requests(struct uvc_video *video)
500{
501	struct uvc_request *ureq;
502	unsigned int req_size;
503	unsigned int i;
504	int ret = -ENOMEM;
505
506	BUG_ON(video->req_size);
507
508	req_size = video->ep->maxpacket
509		 * max_t(unsigned int, video->ep->maxburst, 1)
510		 * (video->ep->mult);
511
512	for (i = 0; i < video->uvc_num_requests; i++) {
513		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
514		if (ureq == NULL)
515			goto error;
516
517		INIT_LIST_HEAD(&ureq->list);
518
519		list_add_tail(&ureq->list, &video->ureqs);
520
521		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
522		if (ureq->req_buffer == NULL)
523			goto error;
524
525		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
526		if (ureq->req == NULL)
527			goto error;
528
529		ureq->req->buf = ureq->req_buffer;
530		ureq->req->length = 0;
531		ureq->req->complete = uvc_video_complete;
532		ureq->req->context = ureq;
533		ureq->video = video;
534		ureq->last_buf = NULL;
535
536		list_add_tail(&ureq->req->list, &video->req_free);
537		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
538		sg_alloc_table(&ureq->sgt,
539			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
540					    PAGE_SIZE) + 2, GFP_KERNEL);
541	}
542
543	video->req_size = req_size;
544
545	return 0;
546
547error:
548	uvc_video_free_requests(video);
549	return ret;
550}
551
552/* --------------------------------------------------------------------------
553 * Video streaming
554 */
555
556/*
557 * uvcg_video_pump - Pump video data into the USB requests
558 *
559 * This function fills the available USB requests (listed in req_free) with
560 * video data from the queued buffers.
561 */
562static void uvcg_video_pump(struct work_struct *work)
563{
564	struct uvc_video *video = container_of(work, struct uvc_video, pump);
565	struct uvc_video_queue *queue = &video->queue;
566	/* video->max_payload_size is only set when using bulk transfer */
567	bool is_bulk = video->max_payload_size;
568	struct usb_request *req = NULL;
569	struct uvc_buffer *buf;
570	unsigned long flags;
571	int ret = 0;
572
573	while (true) {
574		if (!video->ep->enabled)
575			return;
576
577		/*
578		 * Check is_enabled and retrieve the first available USB
579		 * request, protected by the request lock.
580		 */
581		spin_lock_irqsave(&video->req_lock, flags);
582		if (!video->is_enabled || list_empty(&video->req_free)) {
583			spin_unlock_irqrestore(&video->req_lock, flags);
584			return;
585		}
586		req = list_first_entry(&video->req_free, struct usb_request,
587					list);
588		list_del(&req->list);
589		spin_unlock_irqrestore(&video->req_lock, flags);
590
591		/*
592		 * Retrieve the first available video buffer and fill the
593		 * request, protected by the video queue irqlock.
594		 */
595		spin_lock_irqsave(&queue->irqlock, flags);
596		buf = uvcg_queue_head(queue);
597
598		if (buf != NULL) {
599			video->encode(req, video, buf);
600		} else {
601			/*
602			 * Either the queue has been disconnected or no video buffer
603			 * available for bulk transfer. Either way, stop processing
604			 * further.
605			 */
606			spin_unlock_irqrestore(&queue->irqlock, flags);
607			break;
608		}
609
 
 
610		spin_unlock_irqrestore(&queue->irqlock, flags);
611
612		spin_lock_irqsave(&video->req_lock, flags);
613		/* For bulk end points we queue from the worker thread
614		 * since we would preferably not want to wait on requests
615		 * to be ready, in the uvcg_video_complete() handler.
616		 * For isoc endpoints we add the request to the ready list
617		 * and only queue it to the endpoint from the complete handler.
618		 */
619		ret = uvcg_video_usb_req_queue(video, req, is_bulk);
620		spin_unlock_irqrestore(&video->req_lock, flags);
621
622		if (ret < 0) {
623			uvcg_queue_cancel(queue, 0);
624			break;
625		}
626
627		/* The request is owned by  the endpoint / ready list. */
628		req = NULL;
629	}
630
631	if (!req)
632		return;
633
634	spin_lock_irqsave(&video->req_lock, flags);
635	if (video->is_enabled)
636		list_add_tail(&req->list, &video->req_free);
637	else
638		uvc_video_free_request(req->context, video->ep);
639	spin_unlock_irqrestore(&video->req_lock, flags);
640}
641
642/*
643 * Disable the video stream
644 */
645int
646uvcg_video_disable(struct uvc_video *video)
647{
648	unsigned long flags;
649	struct list_head inflight_bufs;
650	struct usb_request *req, *temp;
651	struct uvc_buffer *buf, *btemp;
652	struct uvc_request *ureq, *utemp;
653
654	if (video->ep == NULL) {
655		uvcg_info(&video->uvc->func,
656			  "Video disable failed, device is uninitialized.\n");
657		return -ENODEV;
658	}
659
660	INIT_LIST_HEAD(&inflight_bufs);
661	spin_lock_irqsave(&video->req_lock, flags);
662	video->is_enabled = false;
663
664	/*
665	 * Remove any in-flight buffers from the uvc_requests
666	 * because we want to return them before cancelling the
667	 * queue. This ensures that we aren't stuck waiting for
668	 * all complete callbacks to come through before disabling
669	 * vb2 queue.
670	 */
671	list_for_each_entry(ureq, &video->ureqs, list) {
672		if (ureq->last_buf) {
673			list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
674			ureq->last_buf = NULL;
675		}
676	}
677	spin_unlock_irqrestore(&video->req_lock, flags);
678
679	cancel_work_sync(&video->pump);
680	uvcg_queue_cancel(&video->queue, 0);
681
682	spin_lock_irqsave(&video->req_lock, flags);
683	/*
684	 * Remove all uvc_requests from ureqs with list_del_init
685	 * This lets uvc_video_free_request correctly identify
686	 * if the uvc_request is attached to a list or not when freeing
687	 * memory.
688	 */
689	list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
690		list_del_init(&ureq->list);
691
692	list_for_each_entry_safe(req, temp, &video->req_free, list) {
693		list_del(&req->list);
694		uvc_video_free_request(req->context, video->ep);
695	}
696
697	list_for_each_entry_safe(req, temp, &video->req_ready, list) {
698		list_del(&req->list);
699		uvc_video_free_request(req->context, video->ep);
700	}
701
702	INIT_LIST_HEAD(&video->ureqs);
703	INIT_LIST_HEAD(&video->req_free);
704	INIT_LIST_HEAD(&video->req_ready);
705	video->req_size = 0;
706	spin_unlock_irqrestore(&video->req_lock, flags);
707
708	/*
709	 * Return all the video buffers before disabling the queue.
710	 */
711	spin_lock_irqsave(&video->queue.irqlock, flags);
712	list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
713		list_del(&buf->queue);
714		uvcg_complete_buffer(&video->queue, buf);
715	}
716	spin_unlock_irqrestore(&video->queue.irqlock, flags);
717
718	uvcg_queue_enable(&video->queue, 0);
719	return 0;
720}
721
722/*
723 * Enable the video stream.
724 */
725int uvcg_video_enable(struct uvc_video *video)
726{
727	int ret;
728
729	if (video->ep == NULL) {
730		uvcg_info(&video->uvc->func,
731			  "Video enable failed, device is uninitialized.\n");
732		return -ENODEV;
733	}
734
735	/*
736	 * Safe to access request related fields without req_lock because
737	 * this is the only thread currently active, and no other
738	 * request handling thread will become active until this function
739	 * returns.
740	 */
741	video->is_enabled = true;
742
743	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
744		return ret;
745
746	if ((ret = uvc_video_alloc_requests(video)) < 0)
747		return ret;
748
749	if (video->max_payload_size) {
750		video->encode = uvc_video_encode_bulk;
751		video->payload_size = 0;
752	} else
753		video->encode = video->queue.use_sg ?
754			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
755
756	video->req_int_count = 0;
757
758	uvc_video_ep_queue_initial_requests(video);
759
760	return ret;
761}
762
763/*
764 * Initialize the UVC video stream.
765 */
766int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
767{
768	video->is_enabled = false;
769	INIT_LIST_HEAD(&video->ureqs);
770	INIT_LIST_HEAD(&video->req_free);
771	INIT_LIST_HEAD(&video->req_ready);
772	spin_lock_init(&video->req_lock);
773	INIT_WORK(&video->pump, uvcg_video_pump);
774
775	/* Allocate a work queue for asynchronous video pump handler. */
776	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
777	if (!video->async_wq)
778		return -EINVAL;
779
780	video->uvc = uvc;
781	video->fcc = V4L2_PIX_FMT_YUYV;
782	video->bpp = 16;
783	video->width = 320;
784	video->height = 240;
785	video->imagesize = 320 * 240 * 2;
786
787	/* Initialize the video buffers queue. */
788	uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
789			V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);
790	return 0;
791}
v6.9.4
  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.flags & UVC_QUEUE_DROP_INCOMPLETE)
 39		data[1] |= UVC_STREAM_ERR;
 40
 41	if (video->queue.buf_used == 0 && ts.tv_sec) {
 42		/* dwClockFrequency is 48 MHz */
 43		u32 pts = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
 44
 45		data[1] |= UVC_STREAM_PTS;
 46		put_unaligned_le32(pts, &data[pos]);
 47		pos += 4;
 48	}
 49
 50	if (cdev->gadget->ops->get_frame) {
 51		u32 sof, stc;
 52
 53		sof = usb_gadget_frame_number(cdev->gadget);
 54		ktime_get_ts64(&ts);
 55		stc = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
 56
 57		data[1] |= UVC_STREAM_SCR;
 58		put_unaligned_le32(stc, &data[pos]);
 59		put_unaligned_le16(sof, &data[pos+4]);
 60		pos += 6;
 61	}
 62
 63	data[0] = pos;
 64
 65	if (buf->bytesused - video->queue.buf_used <= len - pos)
 66		data[1] |= UVC_STREAM_EOF;
 67
 68	return pos;
 69}
 70
 71static int
 72uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
 73		u8 *data, int len)
 74{
 75	struct uvc_video_queue *queue = &video->queue;
 76	unsigned int nbytes;
 77	void *mem;
 78
 79	/* Copy video data to the USB buffer. */
 80	mem = buf->mem + queue->buf_used;
 81	nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
 82
 83	memcpy(data, mem, nbytes);
 84	queue->buf_used += nbytes;
 85
 86	return nbytes;
 87}
 88
 89static void
 90uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
 91		struct uvc_buffer *buf)
 92{
 93	void *mem = req->buf;
 94	struct uvc_request *ureq = req->context;
 95	int len = video->req_size;
 96	int ret;
 97
 98	/* Add a header at the beginning of the payload. */
 99	if (video->payload_size == 0) {
100		ret = uvc_video_encode_header(video, buf, mem, len);
101		video->payload_size += ret;
102		mem += ret;
103		len -= ret;
104	}
105
106	/* Process video data. */
107	len = min((int)(video->max_payload_size - video->payload_size), len);
108	ret = uvc_video_encode_data(video, buf, mem, len);
109
110	video->payload_size += ret;
111	len -= ret;
112
113	req->length = video->req_size - len;
114	req->zero = video->payload_size == video->max_payload_size;
115
116	if (buf->bytesused == video->queue.buf_used) {
117		video->queue.buf_used = 0;
118		buf->state = UVC_BUF_STATE_DONE;
119		list_del(&buf->queue);
120		video->fid ^= UVC_STREAM_FID;
121		ureq->last_buf = buf;
122
123		video->payload_size = 0;
124	}
125
126	if (video->payload_size == video->max_payload_size ||
127	    video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE ||
128	    buf->bytesused == video->queue.buf_used)
129		video->payload_size = 0;
130}
131
132static void
133uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
134		struct uvc_buffer *buf)
135{
136	unsigned int pending = buf->bytesused - video->queue.buf_used;
137	struct uvc_request *ureq = req->context;
138	struct scatterlist *sg, *iter;
139	unsigned int len = video->req_size;
140	unsigned int sg_left, part = 0;
141	unsigned int i;
142	int header_len;
143
144	sg = ureq->sgt.sgl;
145	sg_init_table(sg, ureq->sgt.nents);
146
147	/* Init the header. */
148	header_len = uvc_video_encode_header(video, buf, ureq->header,
149				      video->req_size);
150	sg_set_buf(sg, ureq->header, header_len);
151	len -= header_len;
152
153	if (pending <= len)
154		len = pending;
155
156	req->length = (len == pending) ?
157		len + header_len : video->req_size;
158
159	/* Init the pending sgs with payload */
160	sg = sg_next(sg);
161
162	for_each_sg(sg, iter, ureq->sgt.nents - 1, i) {
163		if (!len || !buf->sg || !buf->sg->length)
164			break;
165
166		sg_left = buf->sg->length - buf->offset;
167		part = min_t(unsigned int, len, sg_left);
168
169		sg_set_page(iter, sg_page(buf->sg), part, buf->offset);
170
171		if (part == sg_left) {
172			buf->offset = 0;
173			buf->sg = sg_next(buf->sg);
174		} else {
175			buf->offset += part;
176		}
177		len -= part;
178	}
179
180	/* Assign the video data with header. */
181	req->buf = NULL;
182	req->sg	= ureq->sgt.sgl;
183	req->num_sgs = i + 1;
184
185	req->length -= len;
186	video->queue.buf_used += req->length - header_len;
187
188	if (buf->bytesused == video->queue.buf_used || !buf->sg ||
189			video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
190		video->queue.buf_used = 0;
191		buf->state = UVC_BUF_STATE_DONE;
192		buf->offset = 0;
193		list_del(&buf->queue);
194		video->fid ^= UVC_STREAM_FID;
195		ureq->last_buf = buf;
196	}
197}
198
199static void
200uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
201		struct uvc_buffer *buf)
202{
203	void *mem = req->buf;
204	struct uvc_request *ureq = req->context;
205	int len = video->req_size;
206	int ret;
207
208	/* Add the header. */
209	ret = uvc_video_encode_header(video, buf, mem, len);
210	mem += ret;
211	len -= ret;
212
213	/* Process video data. */
214	ret = uvc_video_encode_data(video, buf, mem, len);
215	len -= ret;
216
217	req->length = video->req_size - len;
218
219	if (buf->bytesused == video->queue.buf_used ||
220			video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
221		video->queue.buf_used = 0;
222		buf->state = UVC_BUF_STATE_DONE;
223		list_del(&buf->queue);
224		video->fid ^= UVC_STREAM_FID;
225		ureq->last_buf = buf;
226	}
227}
228
229/* --------------------------------------------------------------------------
230 * Request handling
231 */
232
233/*
234 * Callers must take care to hold req_lock when this function may be called
235 * from multiple threads. For example, when frames are streaming to the host.
236 */
237static void
238uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep)
239{
240	sg_free_table(&ureq->sgt);
241	if (ureq->req && ep) {
242		usb_ep_free_request(ep, ureq->req);
243		ureq->req = NULL;
244	}
245
246	kfree(ureq->req_buffer);
247	ureq->req_buffer = NULL;
248
249	if (!list_empty(&ureq->list))
250		list_del_init(&ureq->list);
251
252	kfree(ureq);
253}
254
255static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
256{
257	int ret;
258
259	ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
260	if (ret < 0) {
261		uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
262			 ret);
263
264		/* If the endpoint is disabled the descriptor may be NULL. */
265		if (video->ep->desc) {
266			/* Isochronous endpoints can't be halted. */
267			if (usb_endpoint_xfer_bulk(video->ep->desc))
268				usb_ep_set_halt(video->ep);
269		}
270	}
271
272	return ret;
273}
274
275/* This function must be called with video->req_lock held. */
276static int uvcg_video_usb_req_queue(struct uvc_video *video,
277	struct usb_request *req, bool queue_to_ep)
278{
279	bool is_bulk = video->max_payload_size;
280	struct list_head *list = NULL;
281
282	if (!video->is_enabled)
283		return -ENODEV;
284
285	if (queue_to_ep) {
286		struct uvc_request *ureq = req->context;
287		/*
288		 * With USB3 handling more requests at a higher speed, we can't
289		 * afford to generate an interrupt for every request. Decide to
290		 * interrupt:
291		 *
292		 * - When no more requests are available in the free queue, as
293		 *   this may be our last chance to refill the endpoint's
294		 *   request queue.
295		 *
296		 * - When this is request is the last request for the video
297		 *   buffer, as we want to start sending the next video buffer
298		 *   ASAP in case it doesn't get started already in the next
299		 *   iteration of this loop.
300		 *
301		 * - Four times over the length of the requests queue (as
302		 *   indicated by video->uvc_num_requests), as a trade-off
303		 *   between latency and interrupt load.
304		 */
305		if (list_empty(&video->req_free) || ureq->last_buf ||
306			!(video->req_int_count %
307			DIV_ROUND_UP(video->uvc_num_requests, 4))) {
308			video->req_int_count = 0;
309			req->no_interrupt = 0;
310		} else {
311			req->no_interrupt = 1;
312		}
313		video->req_int_count++;
314		return uvcg_video_ep_queue(video, req);
315	}
316	/*
317	 * If we're not queuing to the ep, for isoc we're queuing
318	 * to the req_ready list, otherwise req_free.
319	 */
320	list = is_bulk ? &video->req_free : &video->req_ready;
321	list_add_tail(&req->list, list);
322	return 0;
323}
324
325/*
326 * Must only be called from uvcg_video_enable - since after that we only want to
327 * queue requests to the endpoint from the uvc_video_complete complete handler.
328 * This function is needed in order to 'kick start' the flow of requests from
329 * gadget driver to the usb controller.
330 */
331static void uvc_video_ep_queue_initial_requests(struct uvc_video *video)
332{
333	struct usb_request *req = NULL;
334	unsigned long flags = 0;
335	unsigned int count = 0;
336	int ret = 0;
337
338	/*
339	 * We only queue half of the free list since we still want to have
340	 * some free usb_requests in the free list for the video_pump async_wq
341	 * thread to encode uvc buffers into. Otherwise we could get into a
342	 * situation where the free list does not have any usb requests to
343	 * encode into - we always end up queueing 0 length requests to the
344	 * end point.
345	 */
346	unsigned int half_list_size = video->uvc_num_requests / 2;
347
348	spin_lock_irqsave(&video->req_lock, flags);
349	/*
350	 * Take these requests off the free list and queue them all to the
351	 * endpoint. Since we queue 0 length requests with the req_lock held,
352	 * there isn't any 'data' race involved here with the complete handler.
353	 */
354	while (count < half_list_size) {
355		req = list_first_entry(&video->req_free, struct usb_request,
356					list);
357		list_del(&req->list);
358		req->length = 0;
359		ret = uvcg_video_ep_queue(video, req);
360		if (ret < 0) {
361			uvcg_queue_cancel(&video->queue, 0);
362			break;
363		}
364		count++;
365	}
366	spin_unlock_irqrestore(&video->req_lock, flags);
367}
368
369static void
370uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
371{
372	struct uvc_request *ureq = req->context;
373	struct uvc_video *video = ureq->video;
374	struct uvc_video_queue *queue = &video->queue;
375	struct uvc_buffer *last_buf;
376	struct usb_request *to_queue = req;
377	unsigned long flags;
378	bool is_bulk = video->max_payload_size;
379	int ret = 0;
380
381	spin_lock_irqsave(&video->req_lock, flags);
382	if (!video->is_enabled) {
383		/*
384		 * When is_enabled is false, uvcg_video_disable() ensures
385		 * that in-flight uvc_buffers are returned, so we can
386		 * safely call free_request without worrying about
387		 * last_buf.
388		 */
389		uvc_video_free_request(ureq, ep);
390		spin_unlock_irqrestore(&video->req_lock, flags);
391		return;
392	}
393
394	last_buf = ureq->last_buf;
395	ureq->last_buf = NULL;
396	spin_unlock_irqrestore(&video->req_lock, flags);
397
398	switch (req->status) {
399	case 0:
400		break;
401
402	case -EXDEV:
403		uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n");
404		if (req->length != 0)
405			queue->flags |= UVC_QUEUE_DROP_INCOMPLETE;
406		break;
407
408	case -ESHUTDOWN:	/* disconnect from host. */
409		uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
410		uvcg_queue_cancel(queue, 1);
411		break;
412
413	default:
414		uvcg_warn(&video->uvc->func,
415			  "VS request completed with status %d.\n",
416			  req->status);
417		uvcg_queue_cancel(queue, 0);
418	}
419
420	if (last_buf) {
421		spin_lock_irqsave(&queue->irqlock, flags);
422		uvcg_complete_buffer(queue, last_buf);
423		spin_unlock_irqrestore(&queue->irqlock, flags);
424	}
425
426	spin_lock_irqsave(&video->req_lock, flags);
427	/*
428	 * Video stream might have been disabled while we were
429	 * processing the current usb_request. So make sure
430	 * we're still streaming before queueing the usb_request
431	 * back to req_free
432	 */
433	if (!video->is_enabled) {
434		uvc_video_free_request(ureq, ep);
435		spin_unlock_irqrestore(&video->req_lock, flags);
436		uvcg_queue_cancel(queue, 0);
437
438		return;
439	}
440
441	/*
442	 * Here we check whether any request is available in the ready
443	 * list. If it is, queue it to the ep and add the current
444	 * usb_request to the req_free list - for video_pump to fill in.
445	 * Otherwise, just use the current usb_request to queue a 0
446	 * length request to the ep. Since we always add to the req_free
447	 * list if we dequeue from the ready list, there will never
448	 * be a situation where the req_free list is completely out of
449	 * requests and cannot recover.
450	 */
451	to_queue->length = 0;
452	if (!list_empty(&video->req_ready)) {
453		to_queue = list_first_entry(&video->req_ready,
454			struct usb_request, list);
455		list_del(&to_queue->list);
456		list_add_tail(&req->list, &video->req_free);
457		/*
458		 * Queue work to the wq as well since it is possible that a
459		 * buffer may not have been completely encoded with the set of
460		 * in-flight usb requests for whih the complete callbacks are
461		 * firing.
462		 * In that case, if we do not queue work to the worker thread,
463		 * the buffer will never be marked as complete - and therefore
464		 * not be returned to userpsace. As a result,
465		 * dequeue -> queue -> dequeue flow of uvc buffers will not
466		 * happen.
467		 */
468		queue_work(video->async_wq, &video->pump);
469	}
470	/*
471	 * Queue to the endpoint. The actual queueing to ep will
472	 * only happen on one thread - the async_wq for bulk endpoints
473	 * and this thread for isoc endpoints.
474	 */
475	ret = uvcg_video_usb_req_queue(video, to_queue, !is_bulk);
476	if (ret < 0) {
 
 
 
 
 
 
 
 
 
 
 
 
477		/*
478		 * Endpoint error, but the stream is still enabled.
479		 * Put request back in req_free for it to be cleaned
480		 * up later.
481		 */
482		list_add_tail(&to_queue->list, &video->req_free);
 
 
 
 
 
 
 
 
 
 
 
483	}
484
485	spin_unlock_irqrestore(&video->req_lock, flags);
 
 
486}
487
488static int
489uvc_video_free_requests(struct uvc_video *video)
490{
491	struct uvc_request *ureq, *temp;
492
493	list_for_each_entry_safe(ureq, temp, &video->ureqs, list)
494		uvc_video_free_request(ureq, video->ep);
495
496	INIT_LIST_HEAD(&video->ureqs);
497	INIT_LIST_HEAD(&video->req_free);
498	INIT_LIST_HEAD(&video->req_ready);
499	video->req_size = 0;
500	return 0;
501}
502
503static int
504uvc_video_alloc_requests(struct uvc_video *video)
505{
506	struct uvc_request *ureq;
507	unsigned int req_size;
508	unsigned int i;
509	int ret = -ENOMEM;
510
511	BUG_ON(video->req_size);
512
513	req_size = video->ep->maxpacket
514		 * max_t(unsigned int, video->ep->maxburst, 1)
515		 * (video->ep->mult);
516
517	for (i = 0; i < video->uvc_num_requests; i++) {
518		ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL);
519		if (ureq == NULL)
520			goto error;
521
522		INIT_LIST_HEAD(&ureq->list);
523
524		list_add_tail(&ureq->list, &video->ureqs);
525
526		ureq->req_buffer = kmalloc(req_size, GFP_KERNEL);
527		if (ureq->req_buffer == NULL)
528			goto error;
529
530		ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
531		if (ureq->req == NULL)
532			goto error;
533
534		ureq->req->buf = ureq->req_buffer;
535		ureq->req->length = 0;
536		ureq->req->complete = uvc_video_complete;
537		ureq->req->context = ureq;
538		ureq->video = video;
539		ureq->last_buf = NULL;
540
541		list_add_tail(&ureq->req->list, &video->req_free);
542		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
543		sg_alloc_table(&ureq->sgt,
544			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
545					    PAGE_SIZE) + 2, GFP_KERNEL);
546	}
547
548	video->req_size = req_size;
549
550	return 0;
551
552error:
553	uvc_video_free_requests(video);
554	return ret;
555}
556
557/* --------------------------------------------------------------------------
558 * Video streaming
559 */
560
561/*
562 * uvcg_video_pump - Pump video data into the USB requests
563 *
564 * This function fills the available USB requests (listed in req_free) with
565 * video data from the queued buffers.
566 */
567static void uvcg_video_pump(struct work_struct *work)
568{
569	struct uvc_video *video = container_of(work, struct uvc_video, pump);
570	struct uvc_video_queue *queue = &video->queue;
571	/* video->max_payload_size is only set when using bulk transfer */
572	bool is_bulk = video->max_payload_size;
573	struct usb_request *req = NULL;
574	struct uvc_buffer *buf;
575	unsigned long flags;
576	int ret = 0;
577
578	while (true) {
579		if (!video->ep->enabled)
580			return;
581
582		/*
583		 * Check is_enabled and retrieve the first available USB
584		 * request, protected by the request lock.
585		 */
586		spin_lock_irqsave(&video->req_lock, flags);
587		if (!video->is_enabled || list_empty(&video->req_free)) {
588			spin_unlock_irqrestore(&video->req_lock, flags);
589			return;
590		}
591		req = list_first_entry(&video->req_free, struct usb_request,
592					list);
593		list_del(&req->list);
594		spin_unlock_irqrestore(&video->req_lock, flags);
595
596		/*
597		 * Retrieve the first available video buffer and fill the
598		 * request, protected by the video queue irqlock.
599		 */
600		spin_lock_irqsave(&queue->irqlock, flags);
601		buf = uvcg_queue_head(queue);
602		if (!buf) {
 
 
 
603			/*
604			 * Either the queue has been disconnected or no video buffer
605			 * available for bulk transfer. Either way, stop processing
606			 * further.
607			 */
608			spin_unlock_irqrestore(&queue->irqlock, flags);
609			break;
610		}
611
612		video->encode(req, video, buf);
613
614		spin_unlock_irqrestore(&queue->irqlock, flags);
615
616		spin_lock_irqsave(&video->req_lock, flags);
617		/* For bulk end points we queue from the worker thread
618		 * since we would preferably not want to wait on requests
619		 * to be ready, in the uvcg_video_complete() handler.
620		 * For isoc endpoints we add the request to the ready list
621		 * and only queue it to the endpoint from the complete handler.
622		 */
623		ret = uvcg_video_usb_req_queue(video, req, is_bulk);
624		spin_unlock_irqrestore(&video->req_lock, flags);
625
626		if (ret < 0) {
627			uvcg_queue_cancel(queue, 0);
628			break;
629		}
 
 
 
630	}
 
 
 
 
631	spin_lock_irqsave(&video->req_lock, flags);
632	if (video->is_enabled)
633		list_add_tail(&req->list, &video->req_free);
634	else
635		uvc_video_free_request(req->context, video->ep);
636	spin_unlock_irqrestore(&video->req_lock, flags);
637}
638
639/*
640 * Disable the video stream
641 */
642int
643uvcg_video_disable(struct uvc_video *video)
644{
645	unsigned long flags;
646	struct list_head inflight_bufs;
647	struct usb_request *req, *temp;
648	struct uvc_buffer *buf, *btemp;
649	struct uvc_request *ureq, *utemp;
650
651	if (video->ep == NULL) {
652		uvcg_info(&video->uvc->func,
653			  "Video disable failed, device is uninitialized.\n");
654		return -ENODEV;
655	}
656
657	INIT_LIST_HEAD(&inflight_bufs);
658	spin_lock_irqsave(&video->req_lock, flags);
659	video->is_enabled = false;
660
661	/*
662	 * Remove any in-flight buffers from the uvc_requests
663	 * because we want to return them before cancelling the
664	 * queue. This ensures that we aren't stuck waiting for
665	 * all complete callbacks to come through before disabling
666	 * vb2 queue.
667	 */
668	list_for_each_entry(ureq, &video->ureqs, list) {
669		if (ureq->last_buf) {
670			list_add_tail(&ureq->last_buf->queue, &inflight_bufs);
671			ureq->last_buf = NULL;
672		}
673	}
674	spin_unlock_irqrestore(&video->req_lock, flags);
675
676	cancel_work_sync(&video->pump);
677	uvcg_queue_cancel(&video->queue, 0);
678
679	spin_lock_irqsave(&video->req_lock, flags);
680	/*
681	 * Remove all uvc_requests from ureqs with list_del_init
682	 * This lets uvc_video_free_request correctly identify
683	 * if the uvc_request is attached to a list or not when freeing
684	 * memory.
685	 */
686	list_for_each_entry_safe(ureq, utemp, &video->ureqs, list)
687		list_del_init(&ureq->list);
688
689	list_for_each_entry_safe(req, temp, &video->req_free, list) {
690		list_del(&req->list);
691		uvc_video_free_request(req->context, video->ep);
692	}
693
694	list_for_each_entry_safe(req, temp, &video->req_ready, list) {
695		list_del(&req->list);
696		uvc_video_free_request(req->context, video->ep);
697	}
698
699	INIT_LIST_HEAD(&video->ureqs);
700	INIT_LIST_HEAD(&video->req_free);
701	INIT_LIST_HEAD(&video->req_ready);
702	video->req_size = 0;
703	spin_unlock_irqrestore(&video->req_lock, flags);
704
705	/*
706	 * Return all the video buffers before disabling the queue.
707	 */
708	spin_lock_irqsave(&video->queue.irqlock, flags);
709	list_for_each_entry_safe(buf, btemp, &inflight_bufs, queue) {
710		list_del(&buf->queue);
711		uvcg_complete_buffer(&video->queue, buf);
712	}
713	spin_unlock_irqrestore(&video->queue.irqlock, flags);
714
715	uvcg_queue_enable(&video->queue, 0);
716	return 0;
717}
718
719/*
720 * Enable the video stream.
721 */
722int uvcg_video_enable(struct uvc_video *video)
723{
724	int ret;
725
726	if (video->ep == NULL) {
727		uvcg_info(&video->uvc->func,
728			  "Video enable failed, device is uninitialized.\n");
729		return -ENODEV;
730	}
731
732	/*
733	 * Safe to access request related fields without req_lock because
734	 * this is the only thread currently active, and no other
735	 * request handling thread will become active until this function
736	 * returns.
737	 */
738	video->is_enabled = true;
739
740	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
741		return ret;
742
743	if ((ret = uvc_video_alloc_requests(video)) < 0)
744		return ret;
745
746	if (video->max_payload_size) {
747		video->encode = uvc_video_encode_bulk;
748		video->payload_size = 0;
749	} else
750		video->encode = video->queue.use_sg ?
751			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
752
753	video->req_int_count = 0;
754
755	uvc_video_ep_queue_initial_requests(video);
756
757	return ret;
758}
759
760/*
761 * Initialize the UVC video stream.
762 */
763int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
764{
765	video->is_enabled = false;
766	INIT_LIST_HEAD(&video->ureqs);
767	INIT_LIST_HEAD(&video->req_free);
768	INIT_LIST_HEAD(&video->req_ready);
769	spin_lock_init(&video->req_lock);
770	INIT_WORK(&video->pump, uvcg_video_pump);
771
772	/* Allocate a work queue for asynchronous video pump handler. */
773	video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
774	if (!video->async_wq)
775		return -EINVAL;
776
777	video->uvc = uvc;
778	video->fcc = V4L2_PIX_FMT_YUYV;
779	video->bpp = 16;
780	video->width = 320;
781	video->height = 240;
782	video->imagesize = 320 * 240 * 2;
783
784	/* Initialize the video buffers queue. */
785	uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
786			V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);
787	return 0;
788}