[media] cx23885: video instead of vbi register used
[deliverable/linux.git] / drivers / media / usb / uvc / uvc_queue.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
11fc5baf
LP
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
c0efd232
LP
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
6998b6fb 14#include <linux/atomic.h>
c0efd232 15#include <linux/kernel.h>
27ac792c 16#include <linux/mm.h>
c0efd232
LP
17#include <linux/list.h>
18#include <linux/module.h>
19#include <linux/usb.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
2d700715 23#include <media/videobuf2-v4l2.h>
6998b6fb 24#include <media/videobuf2-vmalloc.h>
c0efd232
LP
25
26#include "uvcvideo.h"
27
28/* ------------------------------------------------------------------------
29 * Video buffers queue management.
30 *
31 * Video queues is initialized by uvc_queue_init(). The function performs
32 * basic initialization of the uvc_video_queue struct and never fails.
33 *
6998b6fb
LP
34 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
35 * the videobuf2 queue operations by serializing calls to videobuf2 and a
36 * spinlock to protect the IRQ queue that holds the buffers to be processed by
37 * the driver.
c0efd232
LP
38 */
39
bc75d5a0
LP
40static inline struct uvc_streaming *
41uvc_queue_to_stream(struct uvc_video_queue *queue)
42{
43 return container_of(queue, struct uvc_streaming, queue);
44}
45
ef33d901
LP
46/*
47 * Return all queued buffers to videobuf2 in the requested state.
48 *
49 * This function must be called with the queue spinlock held.
50 */
51static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
52 enum uvc_buffer_state state)
53{
54 enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
55 ? VB2_BUF_STATE_ERROR
56 : VB2_BUF_STATE_QUEUED;
57
58 while (!list_empty(&queue->irqqueue)) {
59 struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
60 struct uvc_buffer,
61 queue);
62 list_del(&buf->queue);
63 buf->state = state;
2d700715 64 vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
ef33d901
LP
65 }
66}
67
6998b6fb
LP
68/* -----------------------------------------------------------------------------
69 * videobuf2 queue operations
8e815e17 70 */
6998b6fb 71
33119e80 72static int uvc_queue_setup(struct vb2_queue *vq, const void *parg,
6998b6fb
LP
73 unsigned int *nbuffers, unsigned int *nplanes,
74 unsigned int sizes[], void *alloc_ctxs[])
8e815e17 75{
33119e80 76 const struct v4l2_format *fmt = parg;
6998b6fb 77 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
bc75d5a0 78 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
8e815e17 79
bddb9d0e
LP
80 /* Make sure the image size is large enough. */
81 if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize)
82 return -EINVAL;
83
6998b6fb
LP
84 *nplanes = 1;
85
bddb9d0e
LP
86 sizes[0] = fmt ? fmt->fmt.pix.sizeimage
87 : stream->ctrl.dwMaxVideoFrameSize;
8e815e17
LP
88
89 return 0;
90}
91
6998b6fb 92static int uvc_buffer_prepare(struct vb2_buffer *vb)
8e815e17 93{
2d700715 94 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6998b6fb 95 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
2d700715 96 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
8e815e17 97
2d700715 98 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
6998b6fb
LP
99 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
100 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
101 return -EINVAL;
102 }
c0efd232 103
6998b6fb
LP
104 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
105 return -ENODEV;
c0efd232 106
6998b6fb
LP
107 buf->state = UVC_BUF_STATE_QUEUED;
108 buf->error = 0;
109 buf->mem = vb2_plane_vaddr(vb, 0);
110 buf->length = vb2_plane_size(vb, 0);
2d700715 111 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
6998b6fb
LP
112 buf->bytesused = 0;
113 else
114 buf->bytesused = vb2_get_plane_payload(vb, 0);
c0efd232 115
6998b6fb
LP
116 return 0;
117}
c0efd232 118
6998b6fb
LP
119static void uvc_buffer_queue(struct vb2_buffer *vb)
120{
2d700715 121 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6998b6fb 122 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
2d700715 123 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
6998b6fb 124 unsigned long flags;
c0efd232 125
6998b6fb
LP
126 spin_lock_irqsave(&queue->irqlock, flags);
127 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
128 list_add_tail(&buf->queue, &queue->irqqueue);
129 } else {
130 /* If the device is disconnected return the buffer to userspace
131 * directly. The next QBUF call will fail with -ENODEV.
132 */
133 buf->state = UVC_BUF_STATE_ERROR;
2d700715 134 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
c0efd232
LP
135 }
136
6998b6fb
LP
137 spin_unlock_irqrestore(&queue->irqlock, flags);
138}
c0efd232 139
06470642 140static void uvc_buffer_finish(struct vb2_buffer *vb)
66847ef0 141{
2d700715 142 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
66847ef0 143 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
bc75d5a0 144 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
2d700715 145 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
66847ef0 146
9c0863b1 147 if (vb->state == VB2_BUF_STATE_DONE)
2d700715 148 uvc_video_clock_update(stream, vbuf, buf);
66847ef0
LP
149}
150
a11a03e5
LP
151static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
152{
153 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
154 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
ef33d901
LP
155 unsigned long flags;
156 int ret;
a11a03e5
LP
157
158 queue->buf_used = 0;
159
ef33d901
LP
160 ret = uvc_video_enable(stream, 1);
161 if (ret == 0)
162 return 0;
163
164 spin_lock_irqsave(&queue->irqlock, flags);
165 uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
166 spin_unlock_irqrestore(&queue->irqlock, flags);
167
168 return ret;
a11a03e5
LP
169}
170
171static void uvc_stop_streaming(struct vb2_queue *vq)
172{
173 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
174 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
175 unsigned long flags;
176
177 uvc_video_enable(stream, 0);
178
179 spin_lock_irqsave(&queue->irqlock, flags);
ef33d901 180 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
a11a03e5
LP
181 spin_unlock_irqrestore(&queue->irqlock, flags);
182}
183
6998b6fb
LP
184static struct vb2_ops uvc_queue_qops = {
185 .queue_setup = uvc_queue_setup,
186 .buf_prepare = uvc_buffer_prepare,
187 .buf_queue = uvc_buffer_queue,
66847ef0 188 .buf_finish = uvc_buffer_finish,
5fb3f555
LP
189 .wait_prepare = vb2_ops_wait_prepare,
190 .wait_finish = vb2_ops_wait_finish,
a11a03e5
LP
191 .start_streaming = uvc_start_streaming,
192 .stop_streaming = uvc_stop_streaming,
6998b6fb 193};
c0efd232 194
5712661d 195int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
6998b6fb
LP
196 int drop_corrupted)
197{
5712661d
EG
198 int ret;
199
6998b6fb 200 queue->queue.type = type;
ccc4c539 201 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
6998b6fb
LP
202 queue->queue.drv_priv = queue;
203 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
204 queue->queue.ops = &uvc_queue_qops;
205 queue->queue.mem_ops = &vb2_vmalloc_memops;
c767492a
SA
206 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
207 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
5fb3f555 208 queue->queue.lock = &queue->mutex;
5712661d
EG
209 ret = vb2_queue_init(&queue->queue);
210 if (ret)
211 return ret;
c0efd232 212
6998b6fb
LP
213 mutex_init(&queue->mutex);
214 spin_lock_init(&queue->irqlock);
215 INIT_LIST_HEAD(&queue->irqqueue);
216 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
5712661d
EG
217
218 return 0;
c0efd232
LP
219}
220
3f02de27
LP
221void uvc_queue_release(struct uvc_video_queue *queue)
222{
223 mutex_lock(&queue->mutex);
224 vb2_queue_release(&queue->queue);
225 mutex_unlock(&queue->mutex);
226}
227
6998b6fb
LP
228/* -----------------------------------------------------------------------------
229 * V4L2 queue operations
23ff6043 230 */
6998b6fb 231
1b7f9c98
LP
232int uvc_request_buffers(struct uvc_video_queue *queue,
233 struct v4l2_requestbuffers *rb)
23ff6043 234{
6998b6fb 235 int ret;
23ff6043
LP
236
237 mutex_lock(&queue->mutex);
6998b6fb 238 ret = vb2_reqbufs(&queue->queue, rb);
23ff6043
LP
239 mutex_unlock(&queue->mutex);
240
6998b6fb 241 return ret ? ret : rb->count;
23ff6043
LP
242}
243
6998b6fb 244int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
c0efd232 245{
6998b6fb 246 int ret;
c0efd232
LP
247
248 mutex_lock(&queue->mutex);
6998b6fb 249 ret = vb2_querybuf(&queue->queue, buf);
2c2d264b 250 mutex_unlock(&queue->mutex);
6998b6fb 251
2c2d264b 252 return ret;
c0efd232
LP
253}
254
6e9179e2
PZ
255int uvc_create_buffers(struct uvc_video_queue *queue,
256 struct v4l2_create_buffers *cb)
257{
258 int ret;
259
260 mutex_lock(&queue->mutex);
261 ret = vb2_create_bufs(&queue->queue, cb);
262 mutex_unlock(&queue->mutex);
263
264 return ret;
265}
266
6998b6fb 267int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
c0efd232 268{
6998b6fb 269 int ret;
c0efd232
LP
270
271 mutex_lock(&queue->mutex);
6998b6fb 272 ret = vb2_qbuf(&queue->queue, buf);
c0efd232 273 mutex_unlock(&queue->mutex);
c0efd232 274
6998b6fb 275 return ret;
c0efd232
LP
276}
277
7195f61b
LP
278int uvc_export_buffer(struct uvc_video_queue *queue,
279 struct v4l2_exportbuffer *exp)
280{
281 int ret;
282
283 mutex_lock(&queue->mutex);
284 ret = vb2_expbuf(&queue->queue, exp);
285 mutex_unlock(&queue->mutex);
286
287 return ret;
288}
289
6998b6fb
LP
290int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
291 int nonblocking)
c0efd232 292{
6998b6fb 293 int ret;
c0efd232
LP
294
295 mutex_lock(&queue->mutex);
6998b6fb 296 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
c0efd232 297 mutex_unlock(&queue->mutex);
c0efd232 298
6998b6fb 299 return ret;
4aa27597
LP
300}
301
0da4ab98
LP
302int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
303{
304 int ret;
305
306 mutex_lock(&queue->mutex);
307 ret = vb2_streamon(&queue->queue, type);
308 mutex_unlock(&queue->mutex);
309
310 return ret;
311}
312
313int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
314{
315 int ret;
316
317 mutex_lock(&queue->mutex);
318 ret = vb2_streamoff(&queue->queue, type);
319 mutex_unlock(&queue->mutex);
320
321 return ret;
322}
323
4aa27597
LP
324int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
325{
9e68c539 326 return vb2_mmap(&queue->queue, vma);
6998b6fb 327}
4aa27597 328
4742c82e
LP
329#ifndef CONFIG_MMU
330unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
331 unsigned long pgoff)
332{
9e68c539 333 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
4742c82e
LP
334}
335#endif
336
6998b6fb
LP
337unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
338 poll_table *wait)
339{
340 unsigned int ret;
4aa27597 341
6998b6fb
LP
342 mutex_lock(&queue->mutex);
343 ret = vb2_poll(&queue->queue, file, wait);
4aa27597 344 mutex_unlock(&queue->mutex);
6998b6fb 345
4aa27597
LP
346 return ret;
347}
348
6998b6fb 349/* -----------------------------------------------------------------------------
c0efd232 350 *
c0efd232 351 */
6998b6fb
LP
352
353/*
354 * Check if buffers have been allocated.
355 */
356int uvc_queue_allocated(struct uvc_video_queue *queue)
c0efd232 357{
6998b6fb 358 int allocated;
c0efd232
LP
359
360 mutex_lock(&queue->mutex);
6998b6fb 361 allocated = vb2_is_busy(&queue->queue);
c0efd232 362 mutex_unlock(&queue->mutex);
6998b6fb
LP
363
364 return allocated;
c0efd232
LP
365}
366
c0efd232
LP
367/*
368 * Cancel the video buffers queue.
369 *
370 * Cancelling the queue marks all buffers on the irq queue as erroneous,
2c2d264b 371 * wakes them up and removes them from the queue.
c0efd232
LP
372 *
373 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
374 * fail with -ENODEV.
375 *
376 * This function acquires the irq spinlock and can be called from interrupt
377 * context.
378 */
379void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
380{
c0efd232
LP
381 unsigned long flags;
382
383 spin_lock_irqsave(&queue->irqlock, flags);
ef33d901 384 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
c0efd232 385 /* This must be protected by the irqlock spinlock to avoid race
6998b6fb 386 * conditions between uvc_buffer_queue and the disconnection event that
c0efd232 387 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
6998b6fb 388 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
c0efd232
LP
389 * state outside the queue code.
390 */
391 if (disconnect)
392 queue->flags |= UVC_QUEUE_DISCONNECTED;
393 spin_unlock_irqrestore(&queue->irqlock, flags);
394}
395
396struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
397 struct uvc_buffer *buf)
398{
399 struct uvc_buffer *nextbuf;
400 unsigned long flags;
401
9bde9f26
LP
402 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
403 buf->error = 0;
c0efd232 404 buf->state = UVC_BUF_STATE_QUEUED;
8a3f0ede 405 buf->bytesused = 0;
2d700715 406 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
c0efd232
LP
407 return buf;
408 }
409
410 spin_lock_irqsave(&queue->irqlock, flags);
411 list_del(&buf->queue);
412 if (!list_empty(&queue->irqqueue))
413 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
414 queue);
415 else
416 nextbuf = NULL;
417 spin_unlock_irqrestore(&queue->irqlock, flags);
418
6998b6fb 419 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
2d700715
JS
420 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
421 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
6998b6fb 422
c0efd232
LP
423 return nextbuf;
424}
This page took 0.783044 seconds and 5 git commands to generate.