[media] media: videobuf2: Move vb2_fileio_data and vb2_thread to core part
[deliverable/linux.git] / drivers / media / v4l2-core / videobuf2-v4l2.c
CommitLineData
c139990e
JS
1/*
2 * videobuf2-v4l2.c - V4L2 driver helper framework
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Pawel Osciak <pawel@osciak.com>
7 * Marek Szyprowski <m.szyprowski@samsung.com>
8 *
9 * The vb2_thread implementation was based on code from videobuf-dvb.c:
10 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
11 *
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.
15 */
16
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>
26
3c5be988
JS
27#include <media/v4l2-dev.h>
28#include <media/v4l2-fh.h>
29#include <media/v4l2-event.h>
30#include <media/v4l2-common.h>
31
c139990e
JS
32#include <media/videobuf2-v4l2.h>
33
af3bac1a
JS
34static int debug;
35module_param(debug, int, 0644);
36
37#define dprintk(level, fmt, arg...) \
38 do { \
39 if (debug >= level) \
40 pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
41 } while (0)
3c5be988
JS
42
43/* Flags that are set by the vb2 core */
44#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
45 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
46 V4L2_BUF_FLAG_PREPARED | \
47 V4L2_BUF_FLAG_TIMESTAMP_MASK)
48/* Output buffer flags that should be passed on to the driver */
49#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
50 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
51
52/**
53 * __verify_planes_array() - verify that the planes array passed in struct
54 * v4l2_buffer from userspace can be safely used
55 */
56static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
57{
58 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
59 return 0;
60
61 /* Is memory for copying plane information present? */
e88a3f81 62 if (b->m.planes == NULL) {
3c5be988
JS
63 dprintk(1, "multi-planar buffer passed but "
64 "planes array not provided\n");
65 return -EINVAL;
66 }
67
68 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
69 dprintk(1, "incorrect planes array length, "
70 "expected %d, got %d\n", vb->num_planes, b->length);
71 return -EINVAL;
72 }
73
74 return 0;
75}
76
77/**
78 * __verify_length() - Verify that the bytesused value for each plane fits in
79 * the plane length and that the data offset doesn't exceed the bytesused value.
80 */
81static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
82{
83 unsigned int length;
84 unsigned int bytesused;
85 unsigned int plane;
86
87 if (!V4L2_TYPE_IS_OUTPUT(b->type))
88 return 0;
89
90 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
91 for (plane = 0; plane < vb->num_planes; ++plane) {
92 length = (b->memory == VB2_MEMORY_USERPTR ||
93 b->memory == VB2_MEMORY_DMABUF)
94 ? b->m.planes[plane].length
95 : vb->planes[plane].length;
96 bytesused = b->m.planes[plane].bytesused
97 ? b->m.planes[plane].bytesused : length;
98
99 if (b->m.planes[plane].bytesused > length)
100 return -EINVAL;
101
102 if (b->m.planes[plane].data_offset > 0 &&
103 b->m.planes[plane].data_offset >= bytesused)
104 return -EINVAL;
105 }
106 } else {
107 length = (b->memory == VB2_MEMORY_USERPTR)
108 ? b->length : vb->planes[0].length;
109
110 if (b->bytesused > length)
111 return -EINVAL;
112 }
113
114 return 0;
115}
116
959c3ef3 117static int __copy_timestamp(struct vb2_buffer *vb, const void *pb)
3c5be988
JS
118{
119 const struct v4l2_buffer *b = pb;
120 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
121 struct vb2_queue *q = vb->vb2_queue;
122
123 if (q->is_output) {
124 /*
125 * For output buffers copy the timestamp if needed,
126 * and the timecode field and flag if needed.
127 */
959c3ef3 128 if (q->copy_timestamp)
d6dd645e 129 vb->timestamp = timeval_to_ns(&b->timestamp);
3c5be988
JS
130 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
131 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
132 vbuf->timecode = b->timecode;
133 }
134 return 0;
135};
136
137static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
138{
139 static bool check_once;
140
141 if (check_once)
142 return;
143
144 check_once = true;
145 WARN_ON(1);
146
147 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
148 if (vb->vb2_queue->allow_zero_bytesused)
149 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
150 else
151 pr_warn("use the actual size instead.\n");
152}
153
154static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
155 const char *opname)
156{
157 if (b->type != q->type) {
158 dprintk(1, "%s: invalid buffer type\n", opname);
159 return -EINVAL;
160 }
161
162 if (b->index >= q->num_buffers) {
163 dprintk(1, "%s: buffer index out of range\n", opname);
164 return -EINVAL;
165 }
166
167 if (q->bufs[b->index] == NULL) {
168 /* Should never happen */
169 dprintk(1, "%s: buffer is NULL\n", opname);
170 return -EINVAL;
171 }
172
173 if (b->memory != q->memory) {
174 dprintk(1, "%s: invalid memory type\n", opname);
175 return -EINVAL;
176 }
177
178 return __verify_planes_array(q->bufs[b->index], b);
179}
180
181/**
182 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
183 * returned to userspace
184 */
185static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
186{
187 struct v4l2_buffer *b = pb;
188 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
189 struct vb2_queue *q = vb->vb2_queue;
190 unsigned int plane;
191
192 /* Copy back data such as timestamp, flags, etc. */
193 b->index = vb->index;
194 b->type = vb->type;
195 b->memory = vb->memory;
196 b->bytesused = 0;
197
198 b->flags = vbuf->flags;
199 b->field = vbuf->field;
d6dd645e 200 b->timestamp = ns_to_timeval(vb->timestamp);
3c5be988
JS
201 b->timecode = vbuf->timecode;
202 b->sequence = vbuf->sequence;
203 b->reserved2 = 0;
204 b->reserved = 0;
205
206 if (q->is_multiplanar) {
207 /*
208 * Fill in plane-related data if userspace provided an array
209 * for it. The caller has already verified memory and size.
210 */
211 b->length = vb->num_planes;
212 for (plane = 0; plane < vb->num_planes; ++plane) {
213 struct v4l2_plane *pdst = &b->m.planes[plane];
214 struct vb2_plane *psrc = &vb->planes[plane];
215
216 pdst->bytesused = psrc->bytesused;
217 pdst->length = psrc->length;
218 if (q->memory == VB2_MEMORY_MMAP)
219 pdst->m.mem_offset = psrc->m.offset;
220 else if (q->memory == VB2_MEMORY_USERPTR)
221 pdst->m.userptr = psrc->m.userptr;
222 else if (q->memory == VB2_MEMORY_DMABUF)
223 pdst->m.fd = psrc->m.fd;
224 pdst->data_offset = psrc->data_offset;
225 memset(pdst->reserved, 0, sizeof(pdst->reserved));
226 }
227 } else {
228 /*
229 * We use length and offset in v4l2_planes array even for
230 * single-planar buffers, but userspace does not.
231 */
232 b->length = vb->planes[0].length;
233 b->bytesused = vb->planes[0].bytesused;
234 if (q->memory == VB2_MEMORY_MMAP)
235 b->m.offset = vb->planes[0].m.offset;
236 else if (q->memory == VB2_MEMORY_USERPTR)
237 b->m.userptr = vb->planes[0].m.userptr;
238 else if (q->memory == VB2_MEMORY_DMABUF)
239 b->m.fd = vb->planes[0].m.fd;
240 }
241
242 /*
243 * Clear any buffer state related flags.
244 */
245 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
246 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
959c3ef3 247 if (!q->copy_timestamp) {
3c5be988
JS
248 /*
249 * For non-COPY timestamps, drop timestamp source bits
250 * and obtain the timestamp source from the queue.
251 */
252 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
253 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
254 }
255
256 switch (vb->state) {
257 case VB2_BUF_STATE_QUEUED:
258 case VB2_BUF_STATE_ACTIVE:
259 b->flags |= V4L2_BUF_FLAG_QUEUED;
260 break;
261 case VB2_BUF_STATE_ERROR:
262 b->flags |= V4L2_BUF_FLAG_ERROR;
263 /* fall through */
264 case VB2_BUF_STATE_DONE:
265 b->flags |= V4L2_BUF_FLAG_DONE;
266 break;
267 case VB2_BUF_STATE_PREPARED:
268 b->flags |= V4L2_BUF_FLAG_PREPARED;
269 break;
270 case VB2_BUF_STATE_PREPARING:
271 case VB2_BUF_STATE_DEQUEUED:
272 case VB2_BUF_STATE_REQUEUEING:
273 /* nothing */
274 break;
275 }
276
277 if (vb2_buffer_in_use(q, vb))
278 b->flags |= V4L2_BUF_FLAG_MAPPED;
279
dcbc216d
JS
280 if (!q->is_output &&
281 b->flags & V4L2_BUF_FLAG_DONE &&
282 b->flags & V4L2_BUF_FLAG_LAST)
283 q->last_buffer_dequeued = true;
284
3c5be988
JS
285 return 0;
286}
287
288/**
289 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
290 * v4l2_buffer by the userspace. It also verifies that struct
291 * v4l2_buffer has a valid number of planes.
292 */
293static int __fill_vb2_buffer(struct vb2_buffer *vb,
294 const void *pb, struct vb2_plane *planes)
295{
296 struct vb2_queue *q = vb->vb2_queue;
297 const struct v4l2_buffer *b = pb;
298 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
299 unsigned int plane;
300 int ret;
301
302 ret = __verify_length(vb, b);
303 if (ret < 0) {
304 dprintk(1, "plane parameters verification failed: %d\n", ret);
305 return ret;
306 }
307 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
308 /*
309 * If the format's field is ALTERNATE, then the buffer's field
310 * should be either TOP or BOTTOM, not ALTERNATE since that
311 * makes no sense. The driver has to know whether the
312 * buffer represents a top or a bottom field in order to
313 * program any DMA correctly. Using ALTERNATE is wrong, since
314 * that just says that it is either a top or a bottom field,
315 * but not which of the two it is.
316 */
317 dprintk(1, "the field is incorrectly set to ALTERNATE "
318 "for an output buffer\n");
319 return -EINVAL;
320 }
d6dd645e 321 vb->timestamp = 0;
3c5be988
JS
322 vbuf->sequence = 0;
323
324 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
325 if (b->memory == VB2_MEMORY_USERPTR) {
326 for (plane = 0; plane < vb->num_planes; ++plane) {
327 planes[plane].m.userptr =
328 b->m.planes[plane].m.userptr;
329 planes[plane].length =
330 b->m.planes[plane].length;
331 }
332 }
333 if (b->memory == VB2_MEMORY_DMABUF) {
334 for (plane = 0; plane < vb->num_planes; ++plane) {
335 planes[plane].m.fd =
336 b->m.planes[plane].m.fd;
337 planes[plane].length =
338 b->m.planes[plane].length;
339 }
340 }
341
342 /* Fill in driver-provided information for OUTPUT types */
343 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
344 /*
345 * Will have to go up to b->length when API starts
346 * accepting variable number of planes.
347 *
348 * If bytesused == 0 for the output buffer, then fall
349 * back to the full buffer size. In that case
350 * userspace clearly never bothered to set it and
351 * it's a safe assumption that they really meant to
352 * use the full plane sizes.
353 *
354 * Some drivers, e.g. old codec drivers, use bytesused == 0
355 * as a way to indicate that streaming is finished.
356 * In that case, the driver should use the
357 * allow_zero_bytesused flag to keep old userspace
358 * applications working.
359 */
360 for (plane = 0; plane < vb->num_planes; ++plane) {
361 struct vb2_plane *pdst = &planes[plane];
362 struct v4l2_plane *psrc = &b->m.planes[plane];
363
364 if (psrc->bytesused == 0)
365 vb2_warn_zero_bytesused(vb);
366
367 if (vb->vb2_queue->allow_zero_bytesused)
368 pdst->bytesused = psrc->bytesused;
369 else
370 pdst->bytesused = psrc->bytesused ?
371 psrc->bytesused : pdst->length;
372 pdst->data_offset = psrc->data_offset;
373 }
374 }
375 } else {
376 /*
377 * Single-planar buffers do not use planes array,
378 * so fill in relevant v4l2_buffer struct fields instead.
379 * In videobuf we use our internal V4l2_planes struct for
380 * single-planar buffers as well, for simplicity.
381 *
382 * If bytesused == 0 for the output buffer, then fall back
383 * to the full buffer size as that's a sensible default.
384 *
385 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
386 * a way to indicate that streaming is finished. In that case,
387 * the driver should use the allow_zero_bytesused flag to keep
388 * old userspace applications working.
389 */
390 if (b->memory == VB2_MEMORY_USERPTR) {
391 planes[0].m.userptr = b->m.userptr;
392 planes[0].length = b->length;
393 }
394
395 if (b->memory == VB2_MEMORY_DMABUF) {
396 planes[0].m.fd = b->m.fd;
397 planes[0].length = b->length;
398 }
399
400 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
401 if (b->bytesused == 0)
402 vb2_warn_zero_bytesused(vb);
403
404 if (vb->vb2_queue->allow_zero_bytesused)
405 planes[0].bytesused = b->bytesused;
406 else
407 planes[0].bytesused = b->bytesused ?
408 b->bytesused : planes[0].length;
409 } else
410 planes[0].bytesused = 0;
411
412 }
413
414 /* Zero flags that the vb2 core handles */
415 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
959c3ef3 416 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
3c5be988
JS
417 /*
418 * Non-COPY timestamps and non-OUTPUT queues will get
419 * their timestamp and timestamp source flags from the
420 * queue.
421 */
422 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
423 }
424
425 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
426 /*
427 * For output buffers mask out the timecode flag:
428 * this will be handled later in vb2_internal_qbuf().
429 * The 'field' is valid metadata for this output buffer
430 * and so that needs to be copied here.
431 */
432 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
433 vbuf->field = b->field;
434 } else {
435 /* Zero any output buffer flags as this is a capture buffer */
436 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
437 }
438
439 return 0;
440}
441
442static const struct vb2_buf_ops v4l2_buf_ops = {
443 .fill_user_buffer = __fill_v4l2_buffer,
444 .fill_vb2_buffer = __fill_vb2_buffer,
959c3ef3 445 .copy_timestamp = __copy_timestamp,
3c5be988
JS
446};
447
448/**
449 * vb2_querybuf() - query video buffer information
450 * @q: videobuf queue
451 * @b: buffer struct passed from userspace to vidioc_querybuf handler
452 * in driver
453 *
454 * Should be called from vidioc_querybuf ioctl handler in driver.
455 * This function will verify the passed v4l2_buffer structure and fill the
456 * relevant information for the userspace.
457 *
458 * The return values from this function are intended to be directly returned
459 * from vidioc_querybuf handler in driver.
460 */
461int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
462{
463 struct vb2_buffer *vb;
464 int ret;
465
466 if (b->type != q->type) {
467 dprintk(1, "wrong buffer type\n");
468 return -EINVAL;
469 }
470
471 if (b->index >= q->num_buffers) {
472 dprintk(1, "buffer index out of range\n");
473 return -EINVAL;
474 }
475 vb = q->bufs[b->index];
476 ret = __verify_planes_array(vb, b);
477
478 return ret ? ret : vb2_core_querybuf(q, b->index, b);
479}
480EXPORT_SYMBOL(vb2_querybuf);
481
482/**
483 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
484 * the memory and type values.
485 * @q: videobuf2 queue
486 * @req: struct passed from userspace to vidioc_reqbufs handler
487 * in driver
488 */
489int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
490{
491 int ret = vb2_verify_memory_type(q, req->memory, req->type);
492
493 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
494}
495EXPORT_SYMBOL_GPL(vb2_reqbufs);
496
497/**
498 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
499 * @q: videobuf2 queue
500 * @b: buffer structure passed from userspace to vidioc_prepare_buf
501 * handler in driver
502 *
503 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
504 * This function:
505 * 1) verifies the passed buffer,
506 * 2) calls buf_prepare callback in the driver (if provided), in which
507 * driver-specific buffer initialization can be performed,
508 *
509 * The return values from this function are intended to be directly returned
510 * from vidioc_prepare_buf handler in driver.
511 */
512int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
513{
514 int ret;
515
516 if (vb2_fileio_is_active(q)) {
517 dprintk(1, "file io in progress\n");
518 return -EBUSY;
519 }
520
521 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
522
523 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
524}
525EXPORT_SYMBOL_GPL(vb2_prepare_buf);
526
527/**
528 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
529 * the memory and type values.
530 * @q: videobuf2 queue
531 * @create: creation parameters, passed from userspace to vidioc_create_bufs
532 * handler in driver
533 */
534int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
535{
df9ecb0c
HV
536 unsigned requested_planes = 1;
537 unsigned requested_sizes[VIDEO_MAX_PLANES];
538 struct v4l2_format *f = &create->format;
539 int ret = vb2_verify_memory_type(q, create->memory, f->type);
540 unsigned i;
3c5be988
JS
541
542 create->index = q->num_buffers;
543 if (create->count == 0)
544 return ret != -EBUSY ? ret : 0;
df9ecb0c
HV
545
546 switch (f->type) {
547 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
548 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
549 requested_planes = f->fmt.pix_mp.num_planes;
550 if (requested_planes == 0 ||
551 requested_planes > VIDEO_MAX_PLANES)
552 return -EINVAL;
553 for (i = 0; i < requested_planes; i++)
554 requested_sizes[i] =
555 f->fmt.pix_mp.plane_fmt[i].sizeimage;
556 break;
557 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
558 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
559 requested_sizes[0] = f->fmt.pix.sizeimage;
560 break;
561 case V4L2_BUF_TYPE_VBI_CAPTURE:
562 case V4L2_BUF_TYPE_VBI_OUTPUT:
563 requested_sizes[0] = f->fmt.vbi.samples_per_line *
564 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
565 break;
566 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
567 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
568 requested_sizes[0] = f->fmt.sliced.io_size;
569 break;
570 case V4L2_BUF_TYPE_SDR_CAPTURE:
571 case V4L2_BUF_TYPE_SDR_OUTPUT:
572 requested_sizes[0] = f->fmt.sdr.buffersize;
573 break;
574 default:
575 return -EINVAL;
576 }
577 for (i = 0; i < requested_planes; i++)
578 if (requested_sizes[i] == 0)
579 return -EINVAL;
3c5be988 580 return ret ? ret : vb2_core_create_bufs(q, create->memory,
df9ecb0c 581 &create->count, requested_planes, requested_sizes);
3c5be988
JS
582}
583EXPORT_SYMBOL_GPL(vb2_create_bufs);
584
585static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
586{
587 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
588
589 return ret ? ret : vb2_core_qbuf(q, b->index, b);
590}
591
592/**
593 * vb2_qbuf() - Queue a buffer from userspace
594 * @q: videobuf2 queue
595 * @b: buffer structure passed from userspace to vidioc_qbuf handler
596 * in driver
597 *
598 * Should be called from vidioc_qbuf ioctl handler of a driver.
599 * This function:
600 * 1) verifies the passed buffer,
601 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
602 * which driver-specific buffer initialization can be performed,
603 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
604 * callback for processing.
605 *
606 * The return values from this function are intended to be directly returned
607 * from vidioc_qbuf handler in driver.
608 */
609int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
610{
611 if (vb2_fileio_is_active(q)) {
612 dprintk(1, "file io in progress\n");
613 return -EBUSY;
614 }
615
616 return vb2_internal_qbuf(q, b);
617}
618EXPORT_SYMBOL_GPL(vb2_qbuf);
619
620static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
621 bool nonblocking)
622{
623 int ret;
624
625 if (b->type != q->type) {
626 dprintk(1, "invalid buffer type\n");
627 return -EINVAL;
628 }
629
630 ret = vb2_core_dqbuf(q, b, nonblocking);
631
3c5be988
JS
632 return ret;
633}
634
635/**
636 * vb2_dqbuf() - Dequeue a buffer to the userspace
637 * @q: videobuf2 queue
638 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
639 * in driver
640 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
641 * buffers ready for dequeuing are present. Normally the driver
642 * would be passing (file->f_flags & O_NONBLOCK) here
643 *
644 * Should be called from vidioc_dqbuf ioctl handler of a driver.
645 * This function:
646 * 1) verifies the passed buffer,
647 * 2) calls buf_finish callback in the driver (if provided), in which
648 * driver can perform any additional operations that may be required before
649 * returning the buffer to userspace, such as cache sync,
650 * 3) the buffer struct members are filled with relevant information for
651 * the userspace.
652 *
653 * The return values from this function are intended to be directly returned
654 * from vidioc_dqbuf handler in driver.
655 */
656int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
657{
658 if (vb2_fileio_is_active(q)) {
659 dprintk(1, "file io in progress\n");
660 return -EBUSY;
661 }
662 return vb2_internal_dqbuf(q, b, nonblocking);
663}
664EXPORT_SYMBOL_GPL(vb2_dqbuf);
665
666/**
667 * vb2_streamon - start streaming
668 * @q: videobuf2 queue
669 * @type: type argument passed from userspace to vidioc_streamon handler
670 *
671 * Should be called from vidioc_streamon handler of a driver.
672 * This function:
673 * 1) verifies current state
674 * 2) passes any previously queued buffers to the driver and starts streaming
675 *
676 * The return values from this function are intended to be directly returned
677 * from vidioc_streamon handler in the driver.
678 */
679int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
680{
681 if (vb2_fileio_is_active(q)) {
682 dprintk(1, "file io in progress\n");
683 return -EBUSY;
684 }
685 return vb2_core_streamon(q, type);
686}
687EXPORT_SYMBOL_GPL(vb2_streamon);
688
689/**
690 * vb2_streamoff - stop streaming
691 * @q: videobuf2 queue
692 * @type: type argument passed from userspace to vidioc_streamoff handler
693 *
694 * Should be called from vidioc_streamoff handler of a driver.
695 * This function:
696 * 1) verifies current state,
697 * 2) stop streaming and dequeues any queued buffers, including those previously
698 * passed to the driver (after waiting for the driver to finish).
699 *
700 * This call can be used for pausing playback.
701 * The return values from this function are intended to be directly returned
702 * from vidioc_streamoff handler in the driver
703 */
704int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
705{
706 if (vb2_fileio_is_active(q)) {
707 dprintk(1, "file io in progress\n");
708 return -EBUSY;
709 }
710 return vb2_core_streamoff(q, type);
711}
712EXPORT_SYMBOL_GPL(vb2_streamoff);
713
714/**
715 * vb2_expbuf() - Export a buffer as a file descriptor
716 * @q: videobuf2 queue
717 * @eb: export buffer structure passed from userspace to vidioc_expbuf
718 * handler in driver
719 *
720 * The return values from this function are intended to be directly returned
721 * from vidioc_expbuf handler in driver.
722 */
723int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
724{
725 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
726 eb->plane, eb->flags);
727}
728EXPORT_SYMBOL_GPL(vb2_expbuf);
729
730/**
731 * vb2_queue_init() - initialize a videobuf2 queue
732 * @q: videobuf2 queue; this structure should be allocated in driver
733 *
734 * The vb2_queue structure should be allocated by the driver. The driver is
735 * responsible of clearing it's content and setting initial values for some
736 * required entries before calling this function.
737 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
738 * to the struct vb2_queue description in include/media/videobuf2-core.h
739 * for more information.
740 */
741int vb2_queue_init(struct vb2_queue *q)
742{
743 /*
744 * Sanity check
745 */
746 if (WARN_ON(!q) ||
747 WARN_ON(q->timestamp_flags &
748 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
749 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
750 return -EINVAL;
751
752 /* Warn that the driver should choose an appropriate timestamp type */
753 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
754 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
755
756 /* Warn that vb2_memory should match with v4l2_memory */
757 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
758 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
759 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
760 return -EINVAL;
761
762 if (q->buf_struct_size == 0)
763 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
764
765 q->buf_ops = &v4l2_buf_ops;
766 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
767 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
959c3ef3
JS
768 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
769 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
3c5be988
JS
770
771 return vb2_core_queue_init(q);
772}
773EXPORT_SYMBOL_GPL(vb2_queue_init);
774
3c5be988
JS
775/**
776 * vb2_queue_release() - stop streaming, release the queue and free memory
777 * @q: videobuf2 queue
778 *
779 * This function stops streaming and performs necessary clean ups, including
780 * freeing video buffer memory. The driver is responsible for freeing
781 * the vb2_queue structure itself.
782 */
783void vb2_queue_release(struct vb2_queue *q)
784{
3c5be988
JS
785 vb2_core_queue_release(q);
786}
787EXPORT_SYMBOL_GPL(vb2_queue_release);
788
49d8ab9f
JS
789/**
790 * vb2_poll() - implements poll userspace operation
791 * @q: videobuf2 queue
792 * @file: file argument passed to the poll file operation handler
793 * @wait: wait argument passed to the poll file operation handler
794 *
795 * This function implements poll file operation handler for a driver.
796 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
797 * be informed that the file descriptor of a video device is available for
798 * reading.
799 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
800 * will be reported as available for writing.
801 *
802 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
803 * pending events.
804 *
805 * The return values from this function are intended to be directly returned
806 * from poll handler in driver.
807 */
808unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
809{
810 struct video_device *vfd = video_devdata(file);
811 unsigned long req_events = poll_requested_events(wait);
812 unsigned int res = 0;
813
814 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
815 struct v4l2_fh *fh = file->private_data;
816
817 if (v4l2_event_pending(fh))
818 res = POLLPRI;
819 else if (req_events & POLLPRI)
820 poll_wait(file, &fh->wait, wait);
3c5be988 821 }
49d8ab9f
JS
822
823 /*
824 * For compatibility with vb1: if QBUF hasn't been called yet, then
825 * return POLLERR as well. This only affects capture queues, output
826 * queues will always initialize waiting_for_buffers to false.
827 */
828 if (q->waiting_for_buffers && (req_events & (POLLIN | POLLRDNORM)))
829 return POLLERR;
830
831 return res | vb2_core_poll(q, file, wait);
3c5be988
JS
832}
833EXPORT_SYMBOL_GPL(vb2_poll);
834
3c5be988
JS
835/*
836 * The following functions are not part of the vb2 core API, but are helper
837 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
838 * and struct vb2_ops.
839 * They contain boilerplate code that most if not all drivers have to do
840 * and so they simplify the driver code.
841 */
842
843/* The queue is busy if there is a owner and you are not that owner. */
844static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
845{
846 return vdev->queue->owner && vdev->queue->owner != file->private_data;
847}
848
849/* vb2 ioctl helpers */
850
851int vb2_ioctl_reqbufs(struct file *file, void *priv,
852 struct v4l2_requestbuffers *p)
853{
854 struct video_device *vdev = video_devdata(file);
855 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
856
857 if (res)
858 return res;
859 if (vb2_queue_is_busy(vdev, file))
860 return -EBUSY;
861 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
862 /* If count == 0, then the owner has released all buffers and he
863 is no longer owner of the queue. Otherwise we have a new owner. */
864 if (res == 0)
865 vdev->queue->owner = p->count ? file->private_data : NULL;
866 return res;
867}
868EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
869
870int vb2_ioctl_create_bufs(struct file *file, void *priv,
871 struct v4l2_create_buffers *p)
872{
873 struct video_device *vdev = video_devdata(file);
874 int res = vb2_verify_memory_type(vdev->queue, p->memory,
875 p->format.type);
876
877 p->index = vdev->queue->num_buffers;
878 /*
879 * If count == 0, then just check if memory and type are valid.
880 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
881 */
882 if (p->count == 0)
883 return res != -EBUSY ? res : 0;
884 if (res)
885 return res;
886 if (vb2_queue_is_busy(vdev, file))
887 return -EBUSY;
df9ecb0c
HV
888
889 res = vb2_create_bufs(vdev->queue, p);
3c5be988
JS
890 if (res == 0)
891 vdev->queue->owner = file->private_data;
892 return res;
893}
894EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
895
896int vb2_ioctl_prepare_buf(struct file *file, void *priv,
897 struct v4l2_buffer *p)
898{
899 struct video_device *vdev = video_devdata(file);
900
901 if (vb2_queue_is_busy(vdev, file))
902 return -EBUSY;
903 return vb2_prepare_buf(vdev->queue, p);
904}
905EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
906
907int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
908{
909 struct video_device *vdev = video_devdata(file);
910
911 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
912 return vb2_querybuf(vdev->queue, p);
913}
914EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
915
916int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
917{
918 struct video_device *vdev = video_devdata(file);
919
920 if (vb2_queue_is_busy(vdev, file))
921 return -EBUSY;
922 return vb2_qbuf(vdev->queue, p);
923}
924EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
925
926int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
927{
928 struct video_device *vdev = video_devdata(file);
929
930 if (vb2_queue_is_busy(vdev, file))
931 return -EBUSY;
932 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
933}
934EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
935
936int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
937{
938 struct video_device *vdev = video_devdata(file);
939
940 if (vb2_queue_is_busy(vdev, file))
941 return -EBUSY;
942 return vb2_streamon(vdev->queue, i);
943}
944EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
945
946int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
947{
948 struct video_device *vdev = video_devdata(file);
949
950 if (vb2_queue_is_busy(vdev, file))
951 return -EBUSY;
952 return vb2_streamoff(vdev->queue, i);
953}
954EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
955
956int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
957{
958 struct video_device *vdev = video_devdata(file);
959
960 if (vb2_queue_is_busy(vdev, file))
961 return -EBUSY;
962 return vb2_expbuf(vdev->queue, p);
963}
964EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
965
966/* v4l2_file_operations helpers */
967
968int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
969{
970 struct video_device *vdev = video_devdata(file);
971
972 return vb2_mmap(vdev->queue, vma);
973}
974EXPORT_SYMBOL_GPL(vb2_fop_mmap);
975
976int _vb2_fop_release(struct file *file, struct mutex *lock)
977{
978 struct video_device *vdev = video_devdata(file);
979
980 if (lock)
981 mutex_lock(lock);
982 if (file->private_data == vdev->queue->owner) {
983 vb2_queue_release(vdev->queue);
984 vdev->queue->owner = NULL;
985 }
986 if (lock)
987 mutex_unlock(lock);
988 return v4l2_fh_release(file);
989}
990EXPORT_SYMBOL_GPL(_vb2_fop_release);
991
992int vb2_fop_release(struct file *file)
993{
994 struct video_device *vdev = video_devdata(file);
995 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
996
997 return _vb2_fop_release(file, lock);
998}
999EXPORT_SYMBOL_GPL(vb2_fop_release);
1000
1001ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1002 size_t count, loff_t *ppos)
1003{
1004 struct video_device *vdev = video_devdata(file);
1005 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1006 int err = -EBUSY;
1007
1008 if (!(vdev->queue->io_modes & VB2_WRITE))
1009 return -EINVAL;
1010 if (lock && mutex_lock_interruptible(lock))
1011 return -ERESTARTSYS;
1012 if (vb2_queue_is_busy(vdev, file))
1013 goto exit;
1014 err = vb2_write(vdev->queue, buf, count, ppos,
1015 file->f_flags & O_NONBLOCK);
1016 if (vdev->queue->fileio)
1017 vdev->queue->owner = file->private_data;
1018exit:
1019 if (lock)
1020 mutex_unlock(lock);
1021 return err;
1022}
1023EXPORT_SYMBOL_GPL(vb2_fop_write);
1024
1025ssize_t vb2_fop_read(struct file *file, char __user *buf,
1026 size_t count, loff_t *ppos)
1027{
1028 struct video_device *vdev = video_devdata(file);
1029 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1030 int err = -EBUSY;
1031
1032 if (!(vdev->queue->io_modes & VB2_READ))
1033 return -EINVAL;
1034 if (lock && mutex_lock_interruptible(lock))
1035 return -ERESTARTSYS;
1036 if (vb2_queue_is_busy(vdev, file))
1037 goto exit;
1038 err = vb2_read(vdev->queue, buf, count, ppos,
1039 file->f_flags & O_NONBLOCK);
1040 if (vdev->queue->fileio)
1041 vdev->queue->owner = file->private_data;
1042exit:
1043 if (lock)
1044 mutex_unlock(lock);
1045 return err;
1046}
1047EXPORT_SYMBOL_GPL(vb2_fop_read);
1048
1049unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
1050{
1051 struct video_device *vdev = video_devdata(file);
1052 struct vb2_queue *q = vdev->queue;
1053 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1054 unsigned res;
1055 void *fileio;
1056
1057 /*
1058 * If this helper doesn't know how to lock, then you shouldn't be using
1059 * it but you should write your own.
1060 */
1061 WARN_ON(!lock);
1062
1063 if (lock && mutex_lock_interruptible(lock))
1064 return POLLERR;
1065
1066 fileio = q->fileio;
1067
1068 res = vb2_poll(vdev->queue, file, wait);
1069
1070 /* If fileio was started, then we have a new queue owner. */
1071 if (!fileio && q->fileio)
1072 q->owner = file->private_data;
1073 if (lock)
1074 mutex_unlock(lock);
1075 return res;
1076}
1077EXPORT_SYMBOL_GPL(vb2_fop_poll);
1078
1079#ifndef CONFIG_MMU
1080unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1081 unsigned long len, unsigned long pgoff, unsigned long flags)
1082{
1083 struct video_device *vdev = video_devdata(file);
1084
1085 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1086}
1087EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1088#endif
1089
1090/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1091
1092void vb2_ops_wait_prepare(struct vb2_queue *vq)
1093{
1094 mutex_unlock(vq->lock);
1095}
1096EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1097
1098void vb2_ops_wait_finish(struct vb2_queue *vq)
1099{
1100 mutex_lock(vq->lock);
1101}
1102EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1103
c139990e
JS
1104MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1105MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1106MODULE_LICENSE("GPL");
This page took 0.088602 seconds and 5 git commands to generate.