Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *	uvc_queue.c  --  USB Video Class driver - Buffers management
  4 *
  5 *	Copyright (C) 2005-2010
  6 *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7 */
  8
  9#include <linux/atomic.h>
 10#include <linux/kernel.h>
 11#include <linux/mm.h>
 12#include <linux/list.h>
 13#include <linux/module.h>
 14#include <linux/usb.h>
 15#include <linux/videodev2.h>
 16#include <linux/vmalloc.h>
 17#include <linux/wait.h>
 18
 19#include <media/v4l2-common.h>
 20#include <media/videobuf2-dma-sg.h>
 21#include <media/videobuf2-vmalloc.h>
 22
 23#include "uvc.h"
 
 24
 25/* ------------------------------------------------------------------------
 26 * Video buffers queue management.
 27 *
 28 * Video queues is initialized by uvcg_queue_init(). The function performs
 29 * basic initialization of the uvc_video_queue struct and never fails.
 30 *
 31 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
 32 * the videobuf2 queue operations by serializing calls to videobuf2 and a
 33 * spinlock to protect the IRQ queue that holds the buffers to be processed by
 34 * the driver.
 35 */
 36
 37/* -----------------------------------------------------------------------------
 38 * videobuf2 queue operations
 39 */
 40
 41static int uvc_queue_setup(struct vb2_queue *vq,
 42			   unsigned int *nbuffers, unsigned int *nplanes,
 43			   unsigned int sizes[], struct device *alloc_devs[])
 44{
 45	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
 46	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
 47	unsigned int req_size;
 48	unsigned int nreq;
 49
 50	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
 51		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
 
 
 52
 53	*nplanes = 1;
 54
 55	sizes[0] = video->imagesize;
 56
 57	req_size = video->ep->maxpacket
 58		 * max_t(unsigned int, video->ep->maxburst, 1)
 59		 * (video->ep->mult);
 60
 61	/* We divide by two, to increase the chance to run
 62	 * into fewer requests for smaller framesizes.
 63	 */
 64	nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size);
 65	nreq = clamp(nreq, 4U, 64U);
 66	video->uvc_num_requests = nreq;
 67
 68	return 0;
 69}
 70
 71static int uvc_buffer_prepare(struct vb2_buffer *vb)
 72{
 73	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
 
 74	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 75	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
 76
 77	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
 78	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
 79		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
 80		return -EINVAL;
 81	}
 82
 83	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
 84		return -ENODEV;
 85
 86	buf->state = UVC_BUF_STATE_QUEUED;
 87	if (queue->use_sg) {
 88		buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
 89		buf->sg = buf->sgt->sgl;
 90	} else {
 91		buf->mem = vb2_plane_vaddr(vb, 0);
 92	}
 93	buf->length = vb2_plane_size(vb, 0);
 94	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
 95		buf->bytesused = 0;
 96	else
 97		buf->bytesused = vb2_get_plane_payload(vb, 0);
 
 
 
 
 
 98
 99	return 0;
100}
101
102static void uvc_buffer_queue(struct vb2_buffer *vb)
103{
104	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
105	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
106	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
107	unsigned long flags;
108
109	spin_lock_irqsave(&queue->irqlock, flags);
110
111	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
112		list_add_tail(&buf->queue, &queue->irqqueue);
113	} else {
114		/*
115		 * If the device is disconnected return the buffer to userspace
116		 * directly. The next QBUF call will fail with -ENODEV.
117		 */
118		buf->state = UVC_BUF_STATE_ERROR;
119		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
120	}
121
122	spin_unlock_irqrestore(&queue->irqlock, flags);
123}
124
125static const struct vb2_ops uvc_queue_qops = {
126	.queue_setup = uvc_queue_setup,
127	.buf_prepare = uvc_buffer_prepare,
128	.buf_queue = uvc_buffer_queue,
129	.wait_prepare = vb2_ops_wait_prepare,
130	.wait_finish = vb2_ops_wait_finish,
131};
132
133int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
134		    struct mutex *lock)
135{
136	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
137	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
138	int ret;
139
140	queue->queue.type = type;
141	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
142	queue->queue.drv_priv = queue;
143	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
144	queue->queue.ops = &uvc_queue_qops;
145	queue->queue.lock = lock;
146	if (cdev->gadget->sg_supported) {
147		queue->queue.mem_ops = &vb2_dma_sg_memops;
148		queue->use_sg = 1;
149	} else {
150		queue->queue.mem_ops = &vb2_vmalloc_memops;
151	}
152
153	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
154				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
155	queue->queue.dev = dev;
156
157	ret = vb2_queue_init(&queue->queue);
158	if (ret)
159		return ret;
160
161	spin_lock_init(&queue->irqlock);
162	INIT_LIST_HEAD(&queue->irqqueue);
163	queue->flags = 0;
164
165	return 0;
166}
167
168/*
169 * Free the video buffers.
170 */
171void uvcg_free_buffers(struct uvc_video_queue *queue)
172{
173	vb2_queue_release(&queue->queue);
174}
175
176/*
177 * Allocate the video buffers.
178 */
179int uvcg_alloc_buffers(struct uvc_video_queue *queue,
180			      struct v4l2_requestbuffers *rb)
181{
182	int ret;
183
184	ret = vb2_reqbufs(&queue->queue, rb);
185
186	return ret ? ret : rb->count;
187}
188
189int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
190{
191	return vb2_querybuf(&queue->queue, buf);
192}
193
194int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
195{
196	return vb2_qbuf(&queue->queue, NULL, buf);
197}
198
199/*
200 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
201 * available.
202 */
203int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
204			int nonblocking)
205{
206	return vb2_dqbuf(&queue->queue, buf, nonblocking);
207}
208
209/*
210 * Poll the video queue.
211 *
212 * This function implements video queue polling and is intended to be used by
213 * the device poll handler.
214 */
215__poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
216			     poll_table *wait)
217{
218	return vb2_poll(&queue->queue, file, wait);
219}
220
221int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
222{
223	return vb2_mmap(&queue->queue, vma);
224}
225
226#ifndef CONFIG_MMU
227/*
228 * Get unmapped area.
229 *
230 * NO-MMU arch need this function to make mmap() work correctly.
231 */
232unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
233					   unsigned long pgoff)
234{
235	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
236}
237#endif
238
239/*
240 * Cancel the video buffers queue.
241 *
242 * Cancelling the queue marks all buffers on the irq queue as erroneous,
243 * wakes them up and removes them from the queue.
244 *
245 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
246 * fail with -ENODEV.
247 *
248 * This function acquires the irq spinlock and can be called from interrupt
249 * context.
250 */
251void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
252{
253	struct uvc_buffer *buf;
254	unsigned long flags;
255
256	spin_lock_irqsave(&queue->irqlock, flags);
257	while (!list_empty(&queue->irqqueue)) {
258		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
259				       queue);
260		list_del(&buf->queue);
261		buf->state = UVC_BUF_STATE_ERROR;
262		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
263	}
264	queue->buf_used = 0;
265
266	/*
267	 * This must be protected by the irqlock spinlock to avoid race
268	 * conditions between uvc_queue_buffer and the disconnection event that
269	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
270	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
271	 * state outside the queue code.
272	 */
273	if (disconnect)
274		queue->flags |= UVC_QUEUE_DISCONNECTED;
275	spin_unlock_irqrestore(&queue->irqlock, flags);
276}
277
278/*
279 * Enable or disable the video buffers queue.
280 *
281 * The queue must be enabled before starting video acquisition and must be
282 * disabled after stopping it. This ensures that the video buffers queue
283 * state can be properly initialized before buffers are accessed from the
284 * interrupt handler.
285 *
286 * Enabling the video queue initializes parameters (such as sequence number,
287 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
288 *
289 * Disabling the video queue cancels the queue and removes all buffers from
290 * the main queue.
291 *
292 * This function can't be called from interrupt context. Use
293 * uvcg_queue_cancel() instead.
294 */
295int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
296{
297	unsigned long flags;
298	int ret = 0;
299
300	if (enable) {
301		ret = vb2_streamon(&queue->queue, queue->queue.type);
302		if (ret < 0)
303			return ret;
304
305		queue->sequence = 0;
306		queue->buf_used = 0;
307		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
308	} else {
309		ret = vb2_streamoff(&queue->queue, queue->queue.type);
310		if (ret < 0)
311			return ret;
312
313		spin_lock_irqsave(&queue->irqlock, flags);
314		INIT_LIST_HEAD(&queue->irqqueue);
315
316		/*
317		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
318		 * applications will be able to queue buffers for the next
319		 * streaming run. However, clearing it here doesn't guarantee
320		 * that the device will be reconnected in the meantime.
321		 */
322		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
323		spin_unlock_irqrestore(&queue->irqlock, flags);
324	}
325
326	return ret;
327}
328
329/* called with &queue_irqlock held.. */
330void uvcg_complete_buffer(struct uvc_video_queue *queue,
331					  struct uvc_buffer *buf)
332{
333	if (queue->flags & UVC_QUEUE_DROP_INCOMPLETE) {
334		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
335		buf->state = UVC_BUF_STATE_ERROR;
336		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
337		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
338		return;
339	}
340
341	buf->buf.field = V4L2_FIELD_NONE;
342	buf->buf.sequence = queue->sequence++;
343	buf->buf.vb2_buf.timestamp = ktime_get_ns();
344
345	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
346	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
347}
348
349struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
350{
351	struct uvc_buffer *buf = NULL;
352
353	if (!list_empty(&queue->irqqueue))
354		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
355				       queue);
356
357	return buf;
358}
359
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *	uvc_queue.c  --  USB Video Class driver - Buffers management
  4 *
  5 *	Copyright (C) 2005-2010
  6 *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7 */
  8
  9#include <linux/atomic.h>
 10#include <linux/kernel.h>
 11#include <linux/mm.h>
 12#include <linux/list.h>
 13#include <linux/module.h>
 14#include <linux/usb.h>
 15#include <linux/videodev2.h>
 16#include <linux/vmalloc.h>
 17#include <linux/wait.h>
 18
 19#include <media/v4l2-common.h>
 20#include <media/videobuf2-dma-sg.h>
 21#include <media/videobuf2-vmalloc.h>
 22
 23#include "uvc.h"
 24#include "uvc_video.h"
 25
 26/* ------------------------------------------------------------------------
 27 * Video buffers queue management.
 28 *
 29 * Video queues is initialized by uvcg_queue_init(). The function performs
 30 * basic initialization of the uvc_video_queue struct and never fails.
 31 *
 32 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
 33 * the videobuf2 queue operations by serializing calls to videobuf2 and a
 34 * spinlock to protect the IRQ queue that holds the buffers to be processed by
 35 * the driver.
 36 */
 37
 38/* -----------------------------------------------------------------------------
 39 * videobuf2 queue operations
 40 */
 41
 42static int uvc_queue_setup(struct vb2_queue *vq,
 43			   unsigned int *nbuffers, unsigned int *nplanes,
 44			   unsigned int sizes[], struct device *alloc_devs[])
 45{
 46	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
 47	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
 
 
 48
 49	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
 50		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
 51	if (*nbuffers < UVCG_STREAMING_MIN_BUFFERS)
 52		*nbuffers = UVCG_STREAMING_MIN_BUFFERS;
 53
 54	*nplanes = 1;
 55
 56	sizes[0] = video->imagesize;
 57
 
 
 
 
 
 
 
 
 
 
 
 58	return 0;
 59}
 60
 61static int uvc_buffer_prepare(struct vb2_buffer *vb)
 62{
 63	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
 64	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
 65	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 66	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
 67
 68	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
 69	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
 70		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
 71		return -EINVAL;
 72	}
 73
 74	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
 75		return -ENODEV;
 76
 77	buf->state = UVC_BUF_STATE_QUEUED;
 78	if (queue->use_sg) {
 79		buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
 80		buf->sg = buf->sgt->sgl;
 81	} else {
 82		buf->mem = vb2_plane_vaddr(vb, 0);
 83	}
 84	buf->length = vb2_plane_size(vb, 0);
 85	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 86		buf->bytesused = 0;
 87	} else {
 88		buf->bytesused = vb2_get_plane_payload(vb, 0);
 89		buf->req_payload_size =
 90			  DIV_ROUND_UP(buf->bytesused +
 91				       (video->reqs_per_frame * UVCG_REQUEST_HEADER_LEN),
 92				       video->reqs_per_frame);
 93	}
 94
 95	return 0;
 96}
 97
 98static void uvc_buffer_queue(struct vb2_buffer *vb)
 99{
100	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
101	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
102	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
103	unsigned long flags;
104
105	spin_lock_irqsave(&queue->irqlock, flags);
106
107	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
108		list_add_tail(&buf->queue, &queue->irqqueue);
109	} else {
110		/*
111		 * If the device is disconnected return the buffer to userspace
112		 * directly. The next QBUF call will fail with -ENODEV.
113		 */
114		buf->state = UVC_BUF_STATE_ERROR;
115		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
116	}
117
118	spin_unlock_irqrestore(&queue->irqlock, flags);
119}
120
121static const struct vb2_ops uvc_queue_qops = {
122	.queue_setup = uvc_queue_setup,
123	.buf_prepare = uvc_buffer_prepare,
124	.buf_queue = uvc_buffer_queue,
125	.wait_prepare = vb2_ops_wait_prepare,
126	.wait_finish = vb2_ops_wait_finish,
127};
128
129int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
130		    struct mutex *lock)
131{
132	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
133	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
134	int ret;
135
136	queue->queue.type = type;
137	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
138	queue->queue.drv_priv = queue;
139	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
140	queue->queue.ops = &uvc_queue_qops;
141	queue->queue.lock = lock;
142	if (cdev->gadget->sg_supported) {
143		queue->queue.mem_ops = &vb2_dma_sg_memops;
144		queue->use_sg = 1;
145	} else {
146		queue->queue.mem_ops = &vb2_vmalloc_memops;
147	}
148
149	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
150				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
151	queue->queue.dev = dev;
152
153	ret = vb2_queue_init(&queue->queue);
154	if (ret)
155		return ret;
156
157	spin_lock_init(&queue->irqlock);
158	INIT_LIST_HEAD(&queue->irqqueue);
159	queue->flags = 0;
160
161	return 0;
162}
163
164/*
165 * Free the video buffers.
166 */
167void uvcg_free_buffers(struct uvc_video_queue *queue)
168{
169	vb2_queue_release(&queue->queue);
170}
171
172/*
173 * Allocate the video buffers.
174 */
175int uvcg_alloc_buffers(struct uvc_video_queue *queue,
176			      struct v4l2_requestbuffers *rb)
177{
178	int ret;
179
180	ret = vb2_reqbufs(&queue->queue, rb);
181
182	return ret ? ret : rb->count;
183}
184
185int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
186{
187	return vb2_querybuf(&queue->queue, buf);
188}
189
190int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
191{
192	return vb2_qbuf(&queue->queue, NULL, buf);
193}
194
195/*
196 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
197 * available.
198 */
199int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
200			int nonblocking)
201{
202	return vb2_dqbuf(&queue->queue, buf, nonblocking);
203}
204
205/*
206 * Poll the video queue.
207 *
208 * This function implements video queue polling and is intended to be used by
209 * the device poll handler.
210 */
211__poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
212			     poll_table *wait)
213{
214	return vb2_poll(&queue->queue, file, wait);
215}
216
217int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
218{
219	return vb2_mmap(&queue->queue, vma);
220}
221
222#ifndef CONFIG_MMU
223/*
224 * Get unmapped area.
225 *
226 * NO-MMU arch need this function to make mmap() work correctly.
227 */
228unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
229					   unsigned long pgoff)
230{
231	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
232}
233#endif
234
235/*
236 * Cancel the video buffers queue.
237 *
238 * Cancelling the queue marks all buffers on the irq queue as erroneous,
239 * wakes them up and removes them from the queue.
240 *
241 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
242 * fail with -ENODEV.
243 *
244 * This function acquires the irq spinlock and can be called from interrupt
245 * context.
246 */
247void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
248{
249	struct uvc_buffer *buf;
250	unsigned long flags;
251
252	spin_lock_irqsave(&queue->irqlock, flags);
253	while (!list_empty(&queue->irqqueue)) {
254		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
255				       queue);
256		list_del(&buf->queue);
257		buf->state = UVC_BUF_STATE_ERROR;
258		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
259	}
260	queue->buf_used = 0;
261
262	/*
263	 * This must be protected by the irqlock spinlock to avoid race
264	 * conditions between uvc_queue_buffer and the disconnection event that
265	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
266	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
267	 * state outside the queue code.
268	 */
269	if (disconnect)
270		queue->flags |= UVC_QUEUE_DISCONNECTED;
271	spin_unlock_irqrestore(&queue->irqlock, flags);
272}
273
274/*
275 * Enable or disable the video buffers queue.
276 *
277 * The queue must be enabled before starting video acquisition and must be
278 * disabled after stopping it. This ensures that the video buffers queue
279 * state can be properly initialized before buffers are accessed from the
280 * interrupt handler.
281 *
282 * Enabling the video queue initializes parameters (such as sequence number,
283 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
284 *
285 * Disabling the video queue cancels the queue and removes all buffers from
286 * the main queue.
287 *
288 * This function can't be called from interrupt context. Use
289 * uvcg_queue_cancel() instead.
290 */
291int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
292{
293	unsigned long flags;
294	int ret = 0;
295
296	if (enable) {
297		ret = vb2_streamon(&queue->queue, queue->queue.type);
298		if (ret < 0)
299			return ret;
300
301		queue->sequence = 0;
302		queue->buf_used = 0;
303		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
304	} else {
305		ret = vb2_streamoff(&queue->queue, queue->queue.type);
306		if (ret < 0)
307			return ret;
308
309		spin_lock_irqsave(&queue->irqlock, flags);
310		INIT_LIST_HEAD(&queue->irqqueue);
311
312		/*
313		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
314		 * applications will be able to queue buffers for the next
315		 * streaming run. However, clearing it here doesn't guarantee
316		 * that the device will be reconnected in the meantime.
317		 */
318		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
319		spin_unlock_irqrestore(&queue->irqlock, flags);
320	}
321
322	return ret;
323}
324
325/* called with &queue_irqlock held.. */
326void uvcg_complete_buffer(struct uvc_video_queue *queue,
327					  struct uvc_buffer *buf)
328{
329	if (queue->flags & UVC_QUEUE_DROP_INCOMPLETE) {
330		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
331		buf->state = UVC_BUF_STATE_ERROR;
332		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
333		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
334		return;
335	}
336
337	buf->buf.field = V4L2_FIELD_NONE;
338	buf->buf.sequence = queue->sequence++;
339	buf->buf.vb2_buf.timestamp = ktime_get_ns();
340
341	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
342	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
343}
344
345struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
346{
347	struct uvc_buffer *buf = NULL;
348
349	if (!list_empty(&queue->irqqueue))
350		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
351				       queue);
352
353	return buf;
354}
355