s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / media / v4l2-core / videobuf2-v4l2.c
blob27b4b9e7c0c2f63c65c0fdaa1f515fc949a4dee2
1 /*
2 * videobuf2-v4l2.c - V4L2 driver helper framework
4 * Copyright (C) 2010 Samsung Electronics
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
10 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation.
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mm.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-common.h>
32 #include <media/videobuf2-v4l2.h>
34 #include "videobuf2-internal.h"
36 /* Flags that are set by the vb2 core */
37 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
38 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
39 V4L2_BUF_FLAG_PREPARED | \
40 V4L2_BUF_FLAG_TIMESTAMP_MASK)
41 /* Output buffer flags that should be passed on to the driver */
42 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
43 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
45 /**
46 * __verify_planes_array() - verify that the planes array passed in struct
47 * v4l2_buffer from userspace can be safely used
49 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
51 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
52 return 0;
54 /* Is memory for copying plane information present? */
55 if (NULL == b->m.planes) {
56 dprintk(1, "multi-planar buffer passed but "
57 "planes array not provided\n");
58 return -EINVAL;
61 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
62 dprintk(1, "incorrect planes array length, "
63 "expected %d, got %d\n", vb->num_planes, b->length);
64 return -EINVAL;
67 return 0;
70 /**
71 * __verify_length() - Verify that the bytesused value for each plane fits in
72 * the plane length and that the data offset doesn't exceed the bytesused value.
74 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
76 unsigned int length;
77 unsigned int bytesused;
78 unsigned int plane;
80 if (!V4L2_TYPE_IS_OUTPUT(b->type))
81 return 0;
83 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
84 for (plane = 0; plane < vb->num_planes; ++plane) {
85 length = (b->memory == VB2_MEMORY_USERPTR ||
86 b->memory == VB2_MEMORY_DMABUF)
87 ? b->m.planes[plane].length
88 : vb->planes[plane].length;
89 bytesused = b->m.planes[plane].bytesused
90 ? b->m.planes[plane].bytesused : length;
92 if (b->m.planes[plane].bytesused > length)
93 return -EINVAL;
95 if (b->m.planes[plane].data_offset > 0 &&
96 b->m.planes[plane].data_offset >= bytesused)
97 return -EINVAL;
99 } else {
100 length = (b->memory == VB2_MEMORY_USERPTR)
101 ? b->length : vb->planes[0].length;
103 if (b->bytesused > length)
104 return -EINVAL;
107 return 0;
110 static int __set_timestamp(struct vb2_buffer *vb, const void *pb)
112 const struct v4l2_buffer *b = pb;
113 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
114 struct vb2_queue *q = vb->vb2_queue;
116 if (q->is_output) {
118 * For output buffers copy the timestamp if needed,
119 * and the timecode field and flag if needed.
121 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
122 V4L2_BUF_FLAG_TIMESTAMP_COPY)
123 vbuf->timestamp = b->timestamp;
124 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
125 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
126 vbuf->timecode = b->timecode;
128 return 0;
131 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
133 static bool check_once;
135 if (check_once)
136 return;
138 check_once = true;
139 WARN_ON(1);
141 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
142 if (vb->vb2_queue->allow_zero_bytesused)
143 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
144 else
145 pr_warn("use the actual size instead.\n");
148 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
149 const char *opname)
151 if (b->type != q->type) {
152 dprintk(1, "%s: invalid buffer type\n", opname);
153 return -EINVAL;
156 if (b->index >= q->num_buffers) {
157 dprintk(1, "%s: buffer index out of range\n", opname);
158 return -EINVAL;
161 if (q->bufs[b->index] == NULL) {
162 /* Should never happen */
163 dprintk(1, "%s: buffer is NULL\n", opname);
164 return -EINVAL;
167 if (b->memory != q->memory) {
168 dprintk(1, "%s: invalid memory type\n", opname);
169 return -EINVAL;
172 return __verify_planes_array(q->bufs[b->index], b);
176 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
177 * returned to userspace
179 static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
181 struct v4l2_buffer *b = pb;
182 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
183 struct vb2_queue *q = vb->vb2_queue;
184 unsigned int plane;
186 /* Copy back data such as timestamp, flags, etc. */
187 b->index = vb->index;
188 b->type = vb->type;
189 b->memory = vb->memory;
190 b->bytesused = 0;
192 b->flags = vbuf->flags;
193 b->field = vbuf->field;
194 b->timestamp = vbuf->timestamp;
195 b->timecode = vbuf->timecode;
196 b->sequence = vbuf->sequence;
197 b->reserved2 = 0;
198 b->reserved = 0;
200 if (q->is_multiplanar) {
202 * Fill in plane-related data if userspace provided an array
203 * for it. The caller has already verified memory and size.
205 b->length = vb->num_planes;
206 for (plane = 0; plane < vb->num_planes; ++plane) {
207 struct v4l2_plane *pdst = &b->m.planes[plane];
208 struct vb2_plane *psrc = &vb->planes[plane];
210 pdst->bytesused = psrc->bytesused;
211 pdst->length = psrc->length;
212 if (q->memory == VB2_MEMORY_MMAP)
213 pdst->m.mem_offset = psrc->m.offset;
214 else if (q->memory == VB2_MEMORY_USERPTR)
215 pdst->m.userptr = psrc->m.userptr;
216 else if (q->memory == VB2_MEMORY_DMABUF)
217 pdst->m.fd = psrc->m.fd;
218 pdst->data_offset = psrc->data_offset;
219 memset(pdst->reserved, 0, sizeof(pdst->reserved));
221 } else {
223 * We use length and offset in v4l2_planes array even for
224 * single-planar buffers, but userspace does not.
226 b->length = vb->planes[0].length;
227 b->bytesused = vb->planes[0].bytesused;
228 if (q->memory == VB2_MEMORY_MMAP)
229 b->m.offset = vb->planes[0].m.offset;
230 else if (q->memory == VB2_MEMORY_USERPTR)
231 b->m.userptr = vb->planes[0].m.userptr;
232 else if (q->memory == VB2_MEMORY_DMABUF)
233 b->m.fd = vb->planes[0].m.fd;
237 * Clear any buffer state related flags.
239 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
240 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
241 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
242 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
244 * For non-COPY timestamps, drop timestamp source bits
245 * and obtain the timestamp source from the queue.
247 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
248 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
251 switch (vb->state) {
252 case VB2_BUF_STATE_QUEUED:
253 case VB2_BUF_STATE_ACTIVE:
254 b->flags |= V4L2_BUF_FLAG_QUEUED;
255 break;
256 case VB2_BUF_STATE_ERROR:
257 b->flags |= V4L2_BUF_FLAG_ERROR;
258 /* fall through */
259 case VB2_BUF_STATE_DONE:
260 b->flags |= V4L2_BUF_FLAG_DONE;
261 break;
262 case VB2_BUF_STATE_PREPARED:
263 b->flags |= V4L2_BUF_FLAG_PREPARED;
264 break;
265 case VB2_BUF_STATE_PREPARING:
266 case VB2_BUF_STATE_DEQUEUED:
267 case VB2_BUF_STATE_REQUEUEING:
268 /* nothing */
269 break;
272 if (vb2_buffer_in_use(q, vb))
273 b->flags |= V4L2_BUF_FLAG_MAPPED;
275 return 0;
279 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
280 * v4l2_buffer by the userspace. It also verifies that struct
281 * v4l2_buffer has a valid number of planes.
283 static int __fill_vb2_buffer(struct vb2_buffer *vb,
284 const void *pb, struct vb2_plane *planes)
286 struct vb2_queue *q = vb->vb2_queue;
287 const struct v4l2_buffer *b = pb;
288 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
289 unsigned int plane;
290 int ret;
292 ret = __verify_length(vb, b);
293 if (ret < 0) {
294 dprintk(1, "plane parameters verification failed: %d\n", ret);
295 return ret;
297 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
299 * If the format's field is ALTERNATE, then the buffer's field
300 * should be either TOP or BOTTOM, not ALTERNATE since that
301 * makes no sense. The driver has to know whether the
302 * buffer represents a top or a bottom field in order to
303 * program any DMA correctly. Using ALTERNATE is wrong, since
304 * that just says that it is either a top or a bottom field,
305 * but not which of the two it is.
307 dprintk(1, "the field is incorrectly set to ALTERNATE "
308 "for an output buffer\n");
309 return -EINVAL;
311 vbuf->timestamp.tv_sec = 0;
312 vbuf->timestamp.tv_usec = 0;
313 vbuf->sequence = 0;
315 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
316 if (b->memory == VB2_MEMORY_USERPTR) {
317 for (plane = 0; plane < vb->num_planes; ++plane) {
318 planes[plane].m.userptr =
319 b->m.planes[plane].m.userptr;
320 planes[plane].length =
321 b->m.planes[plane].length;
324 if (b->memory == VB2_MEMORY_DMABUF) {
325 for (plane = 0; plane < vb->num_planes; ++plane) {
326 planes[plane].m.fd =
327 b->m.planes[plane].m.fd;
328 planes[plane].length =
329 b->m.planes[plane].length;
333 /* Fill in driver-provided information for OUTPUT types */
334 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
336 * Will have to go up to b->length when API starts
337 * accepting variable number of planes.
339 * If bytesused == 0 for the output buffer, then fall
340 * back to the full buffer size. In that case
341 * userspace clearly never bothered to set it and
342 * it's a safe assumption that they really meant to
343 * use the full plane sizes.
345 * Some drivers, e.g. old codec drivers, use bytesused == 0
346 * as a way to indicate that streaming is finished.
347 * In that case, the driver should use the
348 * allow_zero_bytesused flag to keep old userspace
349 * applications working.
351 for (plane = 0; plane < vb->num_planes; ++plane) {
352 struct vb2_plane *pdst = &planes[plane];
353 struct v4l2_plane *psrc = &b->m.planes[plane];
355 if (psrc->bytesused == 0)
356 vb2_warn_zero_bytesused(vb);
358 if (vb->vb2_queue->allow_zero_bytesused)
359 pdst->bytesused = psrc->bytesused;
360 else
361 pdst->bytesused = psrc->bytesused ?
362 psrc->bytesused : pdst->length;
363 pdst->data_offset = psrc->data_offset;
366 } else {
368 * Single-planar buffers do not use planes array,
369 * so fill in relevant v4l2_buffer struct fields instead.
370 * In videobuf we use our internal V4l2_planes struct for
371 * single-planar buffers as well, for simplicity.
373 * If bytesused == 0 for the output buffer, then fall back
374 * to the full buffer size as that's a sensible default.
376 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
377 * a way to indicate that streaming is finished. In that case,
378 * the driver should use the allow_zero_bytesused flag to keep
379 * old userspace applications working.
381 if (b->memory == VB2_MEMORY_USERPTR) {
382 planes[0].m.userptr = b->m.userptr;
383 planes[0].length = b->length;
386 if (b->memory == VB2_MEMORY_DMABUF) {
387 planes[0].m.fd = b->m.fd;
388 planes[0].length = b->length;
391 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
392 if (b->bytesused == 0)
393 vb2_warn_zero_bytesused(vb);
395 if (vb->vb2_queue->allow_zero_bytesused)
396 planes[0].bytesused = b->bytesused;
397 else
398 planes[0].bytesused = b->bytesused ?
399 b->bytesused : planes[0].length;
400 } else
401 planes[0].bytesused = 0;
405 /* Zero flags that the vb2 core handles */
406 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
407 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
408 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
410 * Non-COPY timestamps and non-OUTPUT queues will get
411 * their timestamp and timestamp source flags from the
412 * queue.
414 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
417 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
419 * For output buffers mask out the timecode flag:
420 * this will be handled later in vb2_internal_qbuf().
421 * The 'field' is valid metadata for this output buffer
422 * and so that needs to be copied here.
424 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
425 vbuf->field = b->field;
426 } else {
427 /* Zero any output buffer flags as this is a capture buffer */
428 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
431 return 0;
434 static const struct vb2_buf_ops v4l2_buf_ops = {
435 .fill_user_buffer = __fill_v4l2_buffer,
436 .fill_vb2_buffer = __fill_vb2_buffer,
437 .set_timestamp = __set_timestamp,
441 * vb2_querybuf() - query video buffer information
442 * @q: videobuf queue
443 * @b: buffer struct passed from userspace to vidioc_querybuf handler
444 * in driver
446 * Should be called from vidioc_querybuf ioctl handler in driver.
447 * This function will verify the passed v4l2_buffer structure and fill the
448 * relevant information for the userspace.
450 * The return values from this function are intended to be directly returned
451 * from vidioc_querybuf handler in driver.
453 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
455 struct vb2_buffer *vb;
456 int ret;
458 if (b->type != q->type) {
459 dprintk(1, "wrong buffer type\n");
460 return -EINVAL;
463 if (b->index >= q->num_buffers) {
464 dprintk(1, "buffer index out of range\n");
465 return -EINVAL;
467 vb = q->bufs[b->index];
468 ret = __verify_planes_array(vb, b);
470 return ret ? ret : vb2_core_querybuf(q, b->index, b);
472 EXPORT_SYMBOL(vb2_querybuf);
475 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
476 * the memory and type values.
477 * @q: videobuf2 queue
478 * @req: struct passed from userspace to vidioc_reqbufs handler
479 * in driver
481 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
483 int ret = vb2_verify_memory_type(q, req->memory, req->type);
485 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
487 EXPORT_SYMBOL_GPL(vb2_reqbufs);
490 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
491 * @q: videobuf2 queue
492 * @b: buffer structure passed from userspace to vidioc_prepare_buf
493 * handler in driver
495 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
496 * This function:
497 * 1) verifies the passed buffer,
498 * 2) calls buf_prepare callback in the driver (if provided), in which
499 * driver-specific buffer initialization can be performed,
501 * The return values from this function are intended to be directly returned
502 * from vidioc_prepare_buf handler in driver.
504 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
506 int ret;
508 if (vb2_fileio_is_active(q)) {
509 dprintk(1, "file io in progress\n");
510 return -EBUSY;
513 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
515 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
517 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
520 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
521 * the memory and type values.
522 * @q: videobuf2 queue
523 * @create: creation parameters, passed from userspace to vidioc_create_bufs
524 * handler in driver
526 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
528 int ret = vb2_verify_memory_type(q, create->memory,
529 create->format.type);
531 create->index = q->num_buffers;
532 if (create->count == 0)
533 return ret != -EBUSY ? ret : 0;
534 return ret ? ret : vb2_core_create_bufs(q, create->memory,
535 &create->count, &create->format);
537 EXPORT_SYMBOL_GPL(vb2_create_bufs);
539 static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
541 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
543 return ret ? ret : vb2_core_qbuf(q, b->index, b);
547 * vb2_qbuf() - Queue a buffer from userspace
548 * @q: videobuf2 queue
549 * @b: buffer structure passed from userspace to vidioc_qbuf handler
550 * in driver
552 * Should be called from vidioc_qbuf ioctl handler of a driver.
553 * This function:
554 * 1) verifies the passed buffer,
555 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
556 * which driver-specific buffer initialization can be performed,
557 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
558 * callback for processing.
560 * The return values from this function are intended to be directly returned
561 * from vidioc_qbuf handler in driver.
563 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
565 if (vb2_fileio_is_active(q)) {
566 dprintk(1, "file io in progress\n");
567 return -EBUSY;
570 return vb2_internal_qbuf(q, b);
572 EXPORT_SYMBOL_GPL(vb2_qbuf);
574 static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
575 bool nonblocking)
577 int ret;
579 if (b->type != q->type) {
580 dprintk(1, "invalid buffer type\n");
581 return -EINVAL;
584 ret = vb2_core_dqbuf(q, b, nonblocking);
586 if (!ret && !q->is_output &&
587 b->flags & V4L2_BUF_FLAG_LAST)
588 q->last_buffer_dequeued = true;
590 return ret;
594 * vb2_dqbuf() - Dequeue a buffer to the userspace
595 * @q: videobuf2 queue
596 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
597 * in driver
598 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
599 * buffers ready for dequeuing are present. Normally the driver
600 * would be passing (file->f_flags & O_NONBLOCK) here
602 * Should be called from vidioc_dqbuf ioctl handler of a driver.
603 * This function:
604 * 1) verifies the passed buffer,
605 * 2) calls buf_finish callback in the driver (if provided), in which
606 * driver can perform any additional operations that may be required before
607 * returning the buffer to userspace, such as cache sync,
608 * 3) the buffer struct members are filled with relevant information for
609 * the userspace.
611 * The return values from this function are intended to be directly returned
612 * from vidioc_dqbuf handler in driver.
614 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
616 if (vb2_fileio_is_active(q)) {
617 dprintk(1, "file io in progress\n");
618 return -EBUSY;
620 return vb2_internal_dqbuf(q, b, nonblocking);
622 EXPORT_SYMBOL_GPL(vb2_dqbuf);
625 * vb2_streamon - start streaming
626 * @q: videobuf2 queue
627 * @type: type argument passed from userspace to vidioc_streamon handler
629 * Should be called from vidioc_streamon handler of a driver.
630 * This function:
631 * 1) verifies current state
632 * 2) passes any previously queued buffers to the driver and starts streaming
634 * The return values from this function are intended to be directly returned
635 * from vidioc_streamon handler in the driver.
637 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
639 if (vb2_fileio_is_active(q)) {
640 dprintk(1, "file io in progress\n");
641 return -EBUSY;
643 return vb2_core_streamon(q, type);
645 EXPORT_SYMBOL_GPL(vb2_streamon);
648 * vb2_streamoff - stop streaming
649 * @q: videobuf2 queue
650 * @type: type argument passed from userspace to vidioc_streamoff handler
652 * Should be called from vidioc_streamoff handler of a driver.
653 * This function:
654 * 1) verifies current state,
655 * 2) stop streaming and dequeues any queued buffers, including those previously
656 * passed to the driver (after waiting for the driver to finish).
658 * This call can be used for pausing playback.
659 * The return values from this function are intended to be directly returned
660 * from vidioc_streamoff handler in the driver
662 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
664 if (vb2_fileio_is_active(q)) {
665 dprintk(1, "file io in progress\n");
666 return -EBUSY;
668 return vb2_core_streamoff(q, type);
670 EXPORT_SYMBOL_GPL(vb2_streamoff);
673 * vb2_expbuf() - Export a buffer as a file descriptor
674 * @q: videobuf2 queue
675 * @eb: export buffer structure passed from userspace to vidioc_expbuf
676 * handler in driver
678 * The return values from this function are intended to be directly returned
679 * from vidioc_expbuf handler in driver.
681 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
683 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
684 eb->plane, eb->flags);
686 EXPORT_SYMBOL_GPL(vb2_expbuf);
689 * vb2_queue_init() - initialize a videobuf2 queue
690 * @q: videobuf2 queue; this structure should be allocated in driver
692 * The vb2_queue structure should be allocated by the driver. The driver is
693 * responsible of clearing it's content and setting initial values for some
694 * required entries before calling this function.
695 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
696 * to the struct vb2_queue description in include/media/videobuf2-core.h
697 * for more information.
699 int vb2_queue_init(struct vb2_queue *q)
702 * Sanity check
704 if (WARN_ON(!q) ||
705 WARN_ON(q->timestamp_flags &
706 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
707 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
708 return -EINVAL;
710 /* Warn that the driver should choose an appropriate timestamp type */
711 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
712 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
714 /* Warn that vb2_memory should match with v4l2_memory */
715 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
716 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
717 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
718 return -EINVAL;
720 if (q->buf_struct_size == 0)
721 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
723 q->buf_ops = &v4l2_buf_ops;
724 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
725 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
727 return vb2_core_queue_init(q);
729 EXPORT_SYMBOL_GPL(vb2_queue_init);
731 static int __vb2_init_fileio(struct vb2_queue *q, int read);
732 static int __vb2_cleanup_fileio(struct vb2_queue *q);
735 * vb2_queue_release() - stop streaming, release the queue and free memory
736 * @q: videobuf2 queue
738 * This function stops streaming and performs necessary clean ups, including
739 * freeing video buffer memory. The driver is responsible for freeing
740 * the vb2_queue structure itself.
742 void vb2_queue_release(struct vb2_queue *q)
744 __vb2_cleanup_fileio(q);
745 vb2_core_queue_release(q);
747 EXPORT_SYMBOL_GPL(vb2_queue_release);
750 * vb2_poll() - implements poll userspace operation
751 * @q: videobuf2 queue
752 * @file: file argument passed to the poll file operation handler
753 * @wait: wait argument passed to the poll file operation handler
755 * This function implements poll file operation handler for a driver.
756 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
757 * be informed that the file descriptor of a video device is available for
758 * reading.
759 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
760 * will be reported as available for writing.
762 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
763 * pending events.
765 * The return values from this function are intended to be directly returned
766 * from poll handler in driver.
768 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
770 struct video_device *vfd = video_devdata(file);
771 unsigned long req_events = poll_requested_events(wait);
772 struct vb2_buffer *vb = NULL;
773 unsigned int res = 0;
774 unsigned long flags;
776 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
777 struct v4l2_fh *fh = file->private_data;
779 if (v4l2_event_pending(fh))
780 res = POLLPRI;
781 else if (req_events & POLLPRI)
782 poll_wait(file, &fh->wait, wait);
785 if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
786 return res;
787 if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
788 return res;
791 * Start file I/O emulator only if streaming API has not been used yet.
793 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
794 if (!q->is_output && (q->io_modes & VB2_READ) &&
795 (req_events & (POLLIN | POLLRDNORM))) {
796 if (__vb2_init_fileio(q, 1))
797 return res | POLLERR;
799 if (q->is_output && (q->io_modes & VB2_WRITE) &&
800 (req_events & (POLLOUT | POLLWRNORM))) {
801 if (__vb2_init_fileio(q, 0))
802 return res | POLLERR;
804 * Write to OUTPUT queue can be done immediately.
806 return res | POLLOUT | POLLWRNORM;
811 * There is nothing to wait for if the queue isn't streaming, or if the
812 * error flag is set.
814 if (!vb2_is_streaming(q) || q->error)
815 return res | POLLERR;
817 * For compatibility with vb1: if QBUF hasn't been called yet, then
818 * return POLLERR as well. This only affects capture queues, output
819 * queues will always initialize waiting_for_buffers to false.
821 if (q->waiting_for_buffers)
822 return res | POLLERR;
825 * For output streams you can write as long as there are fewer buffers
826 * queued than there are buffers available.
828 if (q->is_output && q->queued_count < q->num_buffers)
829 return res | POLLOUT | POLLWRNORM;
831 if (list_empty(&q->done_list)) {
833 * If the last buffer was dequeued from a capture queue,
834 * return immediately. DQBUF will return -EPIPE.
836 if (q->last_buffer_dequeued)
837 return res | POLLIN | POLLRDNORM;
839 poll_wait(file, &q->done_wq, wait);
843 * Take first buffer available for dequeuing.
845 spin_lock_irqsave(&q->done_lock, flags);
846 if (!list_empty(&q->done_list))
847 vb = list_first_entry(&q->done_list, struct vb2_buffer,
848 done_entry);
849 spin_unlock_irqrestore(&q->done_lock, flags);
851 if (vb && (vb->state == VB2_BUF_STATE_DONE
852 || vb->state == VB2_BUF_STATE_ERROR)) {
853 return (q->is_output) ?
854 res | POLLOUT | POLLWRNORM :
855 res | POLLIN | POLLRDNORM;
857 return res;
859 EXPORT_SYMBOL_GPL(vb2_poll);
862 * struct vb2_fileio_buf - buffer context used by file io emulator
864 * vb2 provides a compatibility layer and emulator of file io (read and
865 * write) calls on top of streaming API. This structure is used for
866 * tracking context related to the buffers.
868 struct vb2_fileio_buf {
869 void *vaddr;
870 unsigned int size;
871 unsigned int pos;
872 unsigned int queued:1;
876 * struct vb2_fileio_data - queue context used by file io emulator
878 * @cur_index: the index of the buffer currently being read from or
879 * written to. If equal to q->num_buffers then a new buffer
880 * must be dequeued.
881 * @initial_index: in the read() case all buffers are queued up immediately
882 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
883 * buffers. However, in the write() case no buffers are initially
884 * queued, instead whenever a buffer is full it is queued up by
885 * __vb2_perform_fileio(). Only once all available buffers have
886 * been queued up will __vb2_perform_fileio() start to dequeue
887 * buffers. This means that initially __vb2_perform_fileio()
888 * needs to know what buffer index to use when it is queuing up
889 * the buffers for the first time. That initial index is stored
890 * in this field. Once it is equal to q->num_buffers all
891 * available buffers have been queued and __vb2_perform_fileio()
892 * should start the normal dequeue/queue cycle.
894 * vb2 provides a compatibility layer and emulator of file io (read and
895 * write) calls on top of streaming API. For proper operation it required
896 * this structure to save the driver state between each call of the read
897 * or write function.
899 struct vb2_fileio_data {
900 struct v4l2_requestbuffers req;
901 struct v4l2_plane p;
902 struct v4l2_buffer b;
903 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
904 unsigned int cur_index;
905 unsigned int initial_index;
906 unsigned int q_count;
907 unsigned int dq_count;
908 unsigned read_once:1;
909 unsigned write_immediately:1;
913 * __vb2_init_fileio() - initialize file io emulator
914 * @q: videobuf2 queue
915 * @read: mode selector (1 means read, 0 means write)
917 static int __vb2_init_fileio(struct vb2_queue *q, int read)
919 struct vb2_fileio_data *fileio;
920 int i, ret;
921 unsigned int count = 0;
924 * Sanity check
926 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
927 (!read && !(q->io_modes & VB2_WRITE))))
928 return -EINVAL;
931 * Check if device supports mapping buffers to kernel virtual space.
933 if (!q->mem_ops->vaddr)
934 return -EBUSY;
937 * Check if streaming api has not been already activated.
939 if (q->streaming || q->num_buffers > 0)
940 return -EBUSY;
943 * Start with count 1, driver can increase it in queue_setup()
945 count = 1;
947 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
948 (read) ? "read" : "write", count, q->fileio_read_once,
949 q->fileio_write_immediately);
951 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
952 if (fileio == NULL)
953 return -ENOMEM;
955 fileio->read_once = q->fileio_read_once;
956 fileio->write_immediately = q->fileio_write_immediately;
959 * Request buffers and use MMAP type to force driver
960 * to allocate buffers by itself.
962 fileio->req.count = count;
963 fileio->req.memory = VB2_MEMORY_MMAP;
964 fileio->req.type = q->type;
965 q->fileio = fileio;
966 ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
967 if (ret)
968 goto err_kfree;
971 * Check if plane_count is correct
972 * (multiplane buffers are not supported).
974 if (q->bufs[0]->num_planes != 1) {
975 ret = -EBUSY;
976 goto err_reqbufs;
980 * Get kernel address of each buffer.
982 for (i = 0; i < q->num_buffers; i++) {
983 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
984 if (fileio->bufs[i].vaddr == NULL) {
985 ret = -EINVAL;
986 goto err_reqbufs;
988 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
992 * Read mode requires pre queuing of all buffers.
994 if (read) {
995 bool is_multiplanar = q->is_multiplanar;
998 * Queue all buffers.
1000 for (i = 0; i < q->num_buffers; i++) {
1001 struct v4l2_buffer *b = &fileio->b;
1003 memset(b, 0, sizeof(*b));
1004 b->type = q->type;
1005 if (is_multiplanar) {
1006 memset(&fileio->p, 0, sizeof(fileio->p));
1007 b->m.planes = &fileio->p;
1008 b->length = 1;
1010 b->memory = q->memory;
1011 b->index = i;
1012 ret = vb2_internal_qbuf(q, b);
1013 if (ret)
1014 goto err_reqbufs;
1015 fileio->bufs[i].queued = 1;
1018 * All buffers have been queued, so mark that by setting
1019 * initial_index to q->num_buffers
1021 fileio->initial_index = q->num_buffers;
1022 fileio->cur_index = q->num_buffers;
1026 * Start streaming.
1028 ret = vb2_core_streamon(q, q->type);
1029 if (ret)
1030 goto err_reqbufs;
1032 return ret;
1034 err_reqbufs:
1035 fileio->req.count = 0;
1036 vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1038 err_kfree:
1039 q->fileio = NULL;
1040 kfree(fileio);
1041 return ret;
1045 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1046 * @q: videobuf2 queue
1048 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1050 struct vb2_fileio_data *fileio = q->fileio;
1052 if (fileio) {
1053 vb2_core_streamoff(q, q->type);
1054 q->fileio = NULL;
1055 fileio->req.count = 0;
1056 vb2_reqbufs(q, &fileio->req);
1057 kfree(fileio);
1058 dprintk(3, "file io emulator closed\n");
1060 return 0;
1064 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1065 * @q: videobuf2 queue
1066 * @data: pointed to target userspace buffer
1067 * @count: number of bytes to read or write
1068 * @ppos: file handle position tracking pointer
1069 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1070 * @read: access mode selector (1 means read, 0 means write)
1072 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1073 loff_t *ppos, int nonblock, int read)
1075 struct vb2_fileio_data *fileio;
1076 struct vb2_fileio_buf *buf;
1077 bool is_multiplanar = q->is_multiplanar;
1079 * When using write() to write data to an output video node the vb2 core
1080 * should set timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
1081 * else is able to provide this information with the write() operation.
1083 bool set_timestamp = !read &&
1084 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1085 V4L2_BUF_FLAG_TIMESTAMP_COPY;
1086 int ret, index;
1088 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
1089 read ? "read" : "write", (long)*ppos, count,
1090 nonblock ? "non" : "");
1092 if (!data)
1093 return -EINVAL;
1096 * Initialize emulator on first call.
1098 if (!vb2_fileio_is_active(q)) {
1099 ret = __vb2_init_fileio(q, read);
1100 dprintk(3, "vb2_init_fileio result: %d\n", ret);
1101 if (ret)
1102 return ret;
1104 fileio = q->fileio;
1107 * Check if we need to dequeue the buffer.
1109 index = fileio->cur_index;
1110 if (index >= q->num_buffers) {
1112 * Call vb2_dqbuf to get buffer back.
1114 memset(&fileio->b, 0, sizeof(fileio->b));
1115 fileio->b.type = q->type;
1116 fileio->b.memory = q->memory;
1117 if (is_multiplanar) {
1118 memset(&fileio->p, 0, sizeof(fileio->p));
1119 fileio->b.m.planes = &fileio->p;
1120 fileio->b.length = 1;
1122 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
1123 dprintk(5, "vb2_dqbuf result: %d\n", ret);
1124 if (ret)
1125 return ret;
1126 fileio->dq_count += 1;
1128 fileio->cur_index = index = fileio->b.index;
1129 buf = &fileio->bufs[index];
1132 * Get number of bytes filled by the driver
1134 buf->pos = 0;
1135 buf->queued = 0;
1136 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
1137 : vb2_plane_size(q->bufs[index], 0);
1138 /* Compensate for data_offset on read in the multiplanar case. */
1139 if (is_multiplanar && read &&
1140 fileio->b.m.planes[0].data_offset < buf->size) {
1141 buf->pos = fileio->b.m.planes[0].data_offset;
1142 buf->size -= buf->pos;
1144 } else {
1145 buf = &fileio->bufs[index];
1149 * Limit count on last few bytes of the buffer.
1151 if (buf->pos + count > buf->size) {
1152 count = buf->size - buf->pos;
1153 dprintk(5, "reducing read count: %zd\n", count);
1157 * Transfer data to userspace.
1159 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
1160 count, index, buf->pos);
1161 if (read)
1162 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1163 else
1164 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1165 if (ret) {
1166 dprintk(3, "error copying data\n");
1167 return -EFAULT;
1171 * Update counters.
1173 buf->pos += count;
1174 *ppos += count;
1177 * Queue next buffer if required.
1179 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
1181 * Check if this is the last buffer to read.
1183 if (read && fileio->read_once && fileio->dq_count == 1) {
1184 dprintk(3, "read limit reached\n");
1185 return __vb2_cleanup_fileio(q);
1189 * Call vb2_qbuf and give buffer to the driver.
1191 memset(&fileio->b, 0, sizeof(fileio->b));
1192 fileio->b.type = q->type;
1193 fileio->b.memory = q->memory;
1194 fileio->b.index = index;
1195 fileio->b.bytesused = buf->pos;
1196 if (is_multiplanar) {
1197 memset(&fileio->p, 0, sizeof(fileio->p));
1198 fileio->p.bytesused = buf->pos;
1199 fileio->b.m.planes = &fileio->p;
1200 fileio->b.length = 1;
1202 if (set_timestamp)
1203 v4l2_get_timestamp(&fileio->b.timestamp);
1204 ret = vb2_internal_qbuf(q, &fileio->b);
1205 dprintk(5, "vb2_dbuf result: %d\n", ret);
1206 if (ret)
1207 return ret;
1210 * Buffer has been queued, update the status
1212 buf->pos = 0;
1213 buf->queued = 1;
1214 buf->size = vb2_plane_size(q->bufs[index], 0);
1215 fileio->q_count += 1;
1217 * If we are queuing up buffers for the first time, then
1218 * increase initial_index by one.
1220 if (fileio->initial_index < q->num_buffers)
1221 fileio->initial_index++;
1223 * The next buffer to use is either a buffer that's going to be
1224 * queued for the first time (initial_index < q->num_buffers)
1225 * or it is equal to q->num_buffers, meaning that the next
1226 * time we need to dequeue a buffer since we've now queued up
1227 * all the 'first time' buffers.
1229 fileio->cur_index = fileio->initial_index;
1233 * Return proper number of bytes processed.
1235 if (ret == 0)
1236 ret = count;
1237 return ret;
1240 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1241 loff_t *ppos, int nonblocking)
1243 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1245 EXPORT_SYMBOL_GPL(vb2_read);
1247 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1248 loff_t *ppos, int nonblocking)
1250 return __vb2_perform_fileio(q, (char __user *) data, count,
1251 ppos, nonblocking, 0);
1253 EXPORT_SYMBOL_GPL(vb2_write);
1255 struct vb2_threadio_data {
1256 struct task_struct *thread;
1257 vb2_thread_fnc fnc;
1258 void *priv;
1259 bool stop;
1262 static int vb2_thread(void *data)
1264 struct vb2_queue *q = data;
1265 struct vb2_threadio_data *threadio = q->threadio;
1266 struct vb2_fileio_data *fileio = q->fileio;
1267 bool set_timestamp = false;
1268 int prequeue = 0;
1269 int index = 0;
1270 int ret = 0;
1272 if (q->is_output) {
1273 prequeue = q->num_buffers;
1274 set_timestamp =
1275 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1276 V4L2_BUF_FLAG_TIMESTAMP_COPY;
1279 set_freezable();
1281 for (;;) {
1282 struct vb2_buffer *vb;
1285 * Call vb2_dqbuf to get buffer back.
1287 memset(&fileio->b, 0, sizeof(fileio->b));
1288 fileio->b.type = q->type;
1289 fileio->b.memory = q->memory;
1290 if (prequeue) {
1291 fileio->b.index = index++;
1292 prequeue--;
1293 } else {
1294 call_void_qop(q, wait_finish, q);
1295 if (!threadio->stop)
1296 ret = vb2_internal_dqbuf(q, &fileio->b, 0);
1297 call_void_qop(q, wait_prepare, q);
1298 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1300 if (ret || threadio->stop)
1301 break;
1302 try_to_freeze();
1304 vb = q->bufs[fileio->b.index];
1305 if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
1306 if (threadio->fnc(vb, threadio->priv))
1307 break;
1308 call_void_qop(q, wait_finish, q);
1309 if (set_timestamp)
1310 v4l2_get_timestamp(&fileio->b.timestamp);
1311 if (!threadio->stop)
1312 ret = vb2_internal_qbuf(q, &fileio->b);
1313 call_void_qop(q, wait_prepare, q);
1314 if (ret || threadio->stop)
1315 break;
1318 /* Hmm, linux becomes *very* unhappy without this ... */
1319 while (!kthread_should_stop()) {
1320 set_current_state(TASK_INTERRUPTIBLE);
1321 schedule();
1323 return 0;
1327 * This function should not be used for anything else but the videobuf2-dvb
1328 * support. If you think you have another good use-case for this, then please
1329 * contact the linux-media mailinglist first.
1331 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1332 const char *thread_name)
1334 struct vb2_threadio_data *threadio;
1335 int ret = 0;
1337 if (q->threadio)
1338 return -EBUSY;
1339 if (vb2_is_busy(q))
1340 return -EBUSY;
1341 if (WARN_ON(q->fileio))
1342 return -EBUSY;
1344 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
1345 if (threadio == NULL)
1346 return -ENOMEM;
1347 threadio->fnc = fnc;
1348 threadio->priv = priv;
1350 ret = __vb2_init_fileio(q, !q->is_output);
1351 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1352 if (ret)
1353 goto nomem;
1354 q->threadio = threadio;
1355 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
1356 if (IS_ERR(threadio->thread)) {
1357 ret = PTR_ERR(threadio->thread);
1358 threadio->thread = NULL;
1359 goto nothread;
1361 return 0;
1363 nothread:
1364 __vb2_cleanup_fileio(q);
1365 nomem:
1366 kfree(threadio);
1367 return ret;
1369 EXPORT_SYMBOL_GPL(vb2_thread_start);
1371 int vb2_thread_stop(struct vb2_queue *q)
1373 struct vb2_threadio_data *threadio = q->threadio;
1374 int err;
1376 if (threadio == NULL)
1377 return 0;
1378 threadio->stop = true;
1379 /* Wake up all pending sleeps in the thread */
1380 vb2_queue_error(q);
1381 err = kthread_stop(threadio->thread);
1382 __vb2_cleanup_fileio(q);
1383 threadio->thread = NULL;
1384 kfree(threadio);
1385 q->threadio = NULL;
1386 return err;
1388 EXPORT_SYMBOL_GPL(vb2_thread_stop);
1391 * The following functions are not part of the vb2 core API, but are helper
1392 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
1393 * and struct vb2_ops.
1394 * They contain boilerplate code that most if not all drivers have to do
1395 * and so they simplify the driver code.
1398 /* The queue is busy if there is a owner and you are not that owner. */
1399 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
1401 return vdev->queue->owner && vdev->queue->owner != file->private_data;
1404 /* vb2 ioctl helpers */
1406 int vb2_ioctl_reqbufs(struct file *file, void *priv,
1407 struct v4l2_requestbuffers *p)
1409 struct video_device *vdev = video_devdata(file);
1410 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
1412 if (res)
1413 return res;
1414 if (vb2_queue_is_busy(vdev, file))
1415 return -EBUSY;
1416 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
1417 /* If count == 0, then the owner has released all buffers and he
1418 is no longer owner of the queue. Otherwise we have a new owner. */
1419 if (res == 0)
1420 vdev->queue->owner = p->count ? file->private_data : NULL;
1421 return res;
1423 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1425 int vb2_ioctl_create_bufs(struct file *file, void *priv,
1426 struct v4l2_create_buffers *p)
1428 struct video_device *vdev = video_devdata(file);
1429 int res = vb2_verify_memory_type(vdev->queue, p->memory,
1430 p->format.type);
1432 p->index = vdev->queue->num_buffers;
1434 * If count == 0, then just check if memory and type are valid.
1435 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1437 if (p->count == 0)
1438 return res != -EBUSY ? res : 0;
1439 if (res)
1440 return res;
1441 if (vb2_queue_is_busy(vdev, file))
1442 return -EBUSY;
1443 res = vb2_core_create_bufs(vdev->queue, p->memory, &p->count,
1444 &p->format);
1445 if (res == 0)
1446 vdev->queue->owner = file->private_data;
1447 return res;
1449 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1451 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1452 struct v4l2_buffer *p)
1454 struct video_device *vdev = video_devdata(file);
1456 if (vb2_queue_is_busy(vdev, file))
1457 return -EBUSY;
1458 return vb2_prepare_buf(vdev->queue, p);
1460 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1462 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1464 struct video_device *vdev = video_devdata(file);
1466 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1467 return vb2_querybuf(vdev->queue, p);
1469 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1471 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1473 struct video_device *vdev = video_devdata(file);
1475 if (vb2_queue_is_busy(vdev, file))
1476 return -EBUSY;
1477 return vb2_qbuf(vdev->queue, p);
1479 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1481 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1483 struct video_device *vdev = video_devdata(file);
1485 if (vb2_queue_is_busy(vdev, file))
1486 return -EBUSY;
1487 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1489 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1491 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1493 struct video_device *vdev = video_devdata(file);
1495 if (vb2_queue_is_busy(vdev, file))
1496 return -EBUSY;
1497 return vb2_streamon(vdev->queue, i);
1499 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1501 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1503 struct video_device *vdev = video_devdata(file);
1505 if (vb2_queue_is_busy(vdev, file))
1506 return -EBUSY;
1507 return vb2_streamoff(vdev->queue, i);
1509 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1511 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1513 struct video_device *vdev = video_devdata(file);
1515 if (vb2_queue_is_busy(vdev, file))
1516 return -EBUSY;
1517 return vb2_expbuf(vdev->queue, p);
1519 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1521 /* v4l2_file_operations helpers */
1523 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1525 struct video_device *vdev = video_devdata(file);
1527 return vb2_mmap(vdev->queue, vma);
1529 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1531 int _vb2_fop_release(struct file *file, struct mutex *lock)
1533 struct video_device *vdev = video_devdata(file);
1535 if (lock)
1536 mutex_lock(lock);
1537 if (file->private_data == vdev->queue->owner) {
1538 vb2_queue_release(vdev->queue);
1539 vdev->queue->owner = NULL;
1541 if (lock)
1542 mutex_unlock(lock);
1543 return v4l2_fh_release(file);
1545 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1547 int vb2_fop_release(struct file *file)
1549 struct video_device *vdev = video_devdata(file);
1550 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1552 return _vb2_fop_release(file, lock);
1554 EXPORT_SYMBOL_GPL(vb2_fop_release);
1556 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1557 size_t count, loff_t *ppos)
1559 struct video_device *vdev = video_devdata(file);
1560 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1561 int err = -EBUSY;
1563 if (!(vdev->queue->io_modes & VB2_WRITE))
1564 return -EINVAL;
1565 if (lock && mutex_lock_interruptible(lock))
1566 return -ERESTARTSYS;
1567 if (vb2_queue_is_busy(vdev, file))
1568 goto exit;
1569 err = vb2_write(vdev->queue, buf, count, ppos,
1570 file->f_flags & O_NONBLOCK);
1571 if (vdev->queue->fileio)
1572 vdev->queue->owner = file->private_data;
1573 exit:
1574 if (lock)
1575 mutex_unlock(lock);
1576 return err;
1578 EXPORT_SYMBOL_GPL(vb2_fop_write);
1580 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1581 size_t count, loff_t *ppos)
1583 struct video_device *vdev = video_devdata(file);
1584 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1585 int err = -EBUSY;
1587 if (!(vdev->queue->io_modes & VB2_READ))
1588 return -EINVAL;
1589 if (lock && mutex_lock_interruptible(lock))
1590 return -ERESTARTSYS;
1591 if (vb2_queue_is_busy(vdev, file))
1592 goto exit;
1593 err = vb2_read(vdev->queue, buf, count, ppos,
1594 file->f_flags & O_NONBLOCK);
1595 if (vdev->queue->fileio)
1596 vdev->queue->owner = file->private_data;
1597 exit:
1598 if (lock)
1599 mutex_unlock(lock);
1600 return err;
1602 EXPORT_SYMBOL_GPL(vb2_fop_read);
1604 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
1606 struct video_device *vdev = video_devdata(file);
1607 struct vb2_queue *q = vdev->queue;
1608 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1609 unsigned res;
1610 void *fileio;
1613 * If this helper doesn't know how to lock, then you shouldn't be using
1614 * it but you should write your own.
1616 WARN_ON(!lock);
1618 if (lock && mutex_lock_interruptible(lock))
1619 return POLLERR;
1621 fileio = q->fileio;
1623 res = vb2_poll(vdev->queue, file, wait);
1625 /* If fileio was started, then we have a new queue owner. */
1626 if (!fileio && q->fileio)
1627 q->owner = file->private_data;
1628 if (lock)
1629 mutex_unlock(lock);
1630 return res;
1632 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1634 #ifndef CONFIG_MMU
1635 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1636 unsigned long len, unsigned long pgoff, unsigned long flags)
1638 struct video_device *vdev = video_devdata(file);
1640 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1642 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1643 #endif
1645 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1647 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1649 mutex_unlock(vq->lock);
1651 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1653 void vb2_ops_wait_finish(struct vb2_queue *vq)
1655 mutex_lock(vq->lock);
1657 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1659 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1660 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1661 MODULE_LICENSE("GPL");