blob: 7e45da4d52e1e1291d21a4d524a28e3386380dad [file] [log] [blame]
Junghak Sungc1399902015-09-22 10:30:29 -03001/*
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
Junghak Sung3c5be982015-10-06 06:37:49 -030027#include <media/v4l2-dev.h>
28#include <media/v4l2-fh.h>
29#include <media/v4l2-event.h>
30#include <media/v4l2-common.h>
31
Junghak Sungc1399902015-09-22 10:30:29 -030032#include <media/videobuf2-v4l2.h>
33
Junghak Sungaf3bac12015-11-03 08:16:42 -020034static 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)
Junghak Sung3c5be982015-10-06 06:37:49 -030042
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? */
Julia Lawalle88a3f82015-11-13 08:46:00 -020062 if (b->m.planes == NULL) {
Junghak Sung3c5be982015-10-06 06:37:49 -030063 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
Sakari Ailus83934b72016-04-03 16:31:03 -030077static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
78{
79 return __verify_planes_array(vb, pb);
80}
81
Junghak Sung3c5be982015-10-06 06:37:49 -030082/**
83 * __verify_length() - Verify that the bytesused value for each plane fits in
84 * the plane length and that the data offset doesn't exceed the bytesused value.
85 */
86static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
87{
88 unsigned int length;
89 unsigned int bytesused;
90 unsigned int plane;
91
92 if (!V4L2_TYPE_IS_OUTPUT(b->type))
93 return 0;
94
95 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
96 for (plane = 0; plane < vb->num_planes; ++plane) {
97 length = (b->memory == VB2_MEMORY_USERPTR ||
98 b->memory == VB2_MEMORY_DMABUF)
99 ? b->m.planes[plane].length
100 : vb->planes[plane].length;
101 bytesused = b->m.planes[plane].bytesused
102 ? b->m.planes[plane].bytesused : length;
103
104 if (b->m.planes[plane].bytesused > length)
105 return -EINVAL;
106
107 if (b->m.planes[plane].data_offset > 0 &&
108 b->m.planes[plane].data_offset >= bytesused)
109 return -EINVAL;
110 }
111 } else {
112 length = (b->memory == VB2_MEMORY_USERPTR)
113 ? b->length : vb->planes[0].length;
114
115 if (b->bytesused > length)
116 return -EINVAL;
117 }
118
119 return 0;
120}
121
Hans Verkuil10cc3b12015-11-20 08:32:00 -0200122static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
Junghak Sung3c5be982015-10-06 06:37:49 -0300123{
124 const struct v4l2_buffer *b = pb;
125 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
126 struct vb2_queue *q = vb->vb2_queue;
127
128 if (q->is_output) {
129 /*
130 * For output buffers copy the timestamp if needed,
131 * and the timecode field and flag if needed.
132 */
Junghak Sung959c3ef2015-11-03 08:16:38 -0200133 if (q->copy_timestamp)
Junghak Sungd6dd6452015-11-03 08:16:37 -0200134 vb->timestamp = timeval_to_ns(&b->timestamp);
Junghak Sung3c5be982015-10-06 06:37:49 -0300135 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
136 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
137 vbuf->timecode = b->timecode;
138 }
Junghak Sung3c5be982015-10-06 06:37:49 -0300139};
140
141static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
142{
143 static bool check_once;
144
145 if (check_once)
146 return;
147
148 check_once = true;
Junghak Sung3c5be982015-10-06 06:37:49 -0300149
150 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
151 if (vb->vb2_queue->allow_zero_bytesused)
152 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
153 else
154 pr_warn("use the actual size instead.\n");
155}
156
157static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
158 const char *opname)
159{
160 if (b->type != q->type) {
161 dprintk(1, "%s: invalid buffer type\n", opname);
162 return -EINVAL;
163 }
164
165 if (b->index >= q->num_buffers) {
166 dprintk(1, "%s: buffer index out of range\n", opname);
167 return -EINVAL;
168 }
169
170 if (q->bufs[b->index] == NULL) {
171 /* Should never happen */
172 dprintk(1, "%s: buffer is NULL\n", opname);
173 return -EINVAL;
174 }
175
176 if (b->memory != q->memory) {
177 dprintk(1, "%s: invalid memory type\n", opname);
178 return -EINVAL;
179 }
180
181 return __verify_planes_array(q->bufs[b->index], b);
182}
183
184/**
185 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
186 * returned to userspace
187 */
Hans Verkuil10cc3b12015-11-20 08:32:00 -0200188static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
Junghak Sung3c5be982015-10-06 06:37:49 -0300189{
190 struct v4l2_buffer *b = pb;
191 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
192 struct vb2_queue *q = vb->vb2_queue;
193 unsigned int plane;
194
195 /* Copy back data such as timestamp, flags, etc. */
196 b->index = vb->index;
197 b->type = vb->type;
198 b->memory = vb->memory;
199 b->bytesused = 0;
200
201 b->flags = vbuf->flags;
202 b->field = vbuf->field;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200203 b->timestamp = ns_to_timeval(vb->timestamp);
Junghak Sung3c5be982015-10-06 06:37:49 -0300204 b->timecode = vbuf->timecode;
205 b->sequence = vbuf->sequence;
206 b->reserved2 = 0;
207 b->reserved = 0;
208
209 if (q->is_multiplanar) {
210 /*
211 * Fill in plane-related data if userspace provided an array
212 * for it. The caller has already verified memory and size.
213 */
214 b->length = vb->num_planes;
215 for (plane = 0; plane < vb->num_planes; ++plane) {
216 struct v4l2_plane *pdst = &b->m.planes[plane];
217 struct vb2_plane *psrc = &vb->planes[plane];
218
219 pdst->bytesused = psrc->bytesused;
220 pdst->length = psrc->length;
221 if (q->memory == VB2_MEMORY_MMAP)
222 pdst->m.mem_offset = psrc->m.offset;
223 else if (q->memory == VB2_MEMORY_USERPTR)
224 pdst->m.userptr = psrc->m.userptr;
225 else if (q->memory == VB2_MEMORY_DMABUF)
226 pdst->m.fd = psrc->m.fd;
227 pdst->data_offset = psrc->data_offset;
228 memset(pdst->reserved, 0, sizeof(pdst->reserved));
229 }
230 } else {
231 /*
232 * We use length and offset in v4l2_planes array even for
233 * single-planar buffers, but userspace does not.
234 */
235 b->length = vb->planes[0].length;
236 b->bytesused = vb->planes[0].bytesused;
237 if (q->memory == VB2_MEMORY_MMAP)
238 b->m.offset = vb->planes[0].m.offset;
239 else if (q->memory == VB2_MEMORY_USERPTR)
240 b->m.userptr = vb->planes[0].m.userptr;
241 else if (q->memory == VB2_MEMORY_DMABUF)
242 b->m.fd = vb->planes[0].m.fd;
243 }
244
245 /*
246 * Clear any buffer state related flags.
247 */
248 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
249 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
Junghak Sung959c3ef2015-11-03 08:16:38 -0200250 if (!q->copy_timestamp) {
Junghak Sung3c5be982015-10-06 06:37:49 -0300251 /*
252 * For non-COPY timestamps, drop timestamp source bits
253 * and obtain the timestamp source from the queue.
254 */
255 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
256 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
257 }
258
259 switch (vb->state) {
260 case VB2_BUF_STATE_QUEUED:
261 case VB2_BUF_STATE_ACTIVE:
262 b->flags |= V4L2_BUF_FLAG_QUEUED;
263 break;
264 case VB2_BUF_STATE_ERROR:
265 b->flags |= V4L2_BUF_FLAG_ERROR;
266 /* fall through */
267 case VB2_BUF_STATE_DONE:
268 b->flags |= V4L2_BUF_FLAG_DONE;
269 break;
270 case VB2_BUF_STATE_PREPARED:
271 b->flags |= V4L2_BUF_FLAG_PREPARED;
272 break;
273 case VB2_BUF_STATE_PREPARING:
274 case VB2_BUF_STATE_DEQUEUED:
275 case VB2_BUF_STATE_REQUEUEING:
276 /* nothing */
277 break;
278 }
279
280 if (vb2_buffer_in_use(q, vb))
281 b->flags |= V4L2_BUF_FLAG_MAPPED;
282
Junghak Sungdcbc2162015-11-03 08:16:40 -0200283 if (!q->is_output &&
284 b->flags & V4L2_BUF_FLAG_DONE &&
285 b->flags & V4L2_BUF_FLAG_LAST)
286 q->last_buffer_dequeued = true;
Junghak Sung3c5be982015-10-06 06:37:49 -0300287}
288
289/**
290 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
291 * v4l2_buffer by the userspace. It also verifies that struct
292 * v4l2_buffer has a valid number of planes.
293 */
294static int __fill_vb2_buffer(struct vb2_buffer *vb,
295 const void *pb, struct vb2_plane *planes)
296{
297 struct vb2_queue *q = vb->vb2_queue;
298 const struct v4l2_buffer *b = pb;
299 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
300 unsigned int plane;
301 int ret;
302
303 ret = __verify_length(vb, b);
304 if (ret < 0) {
305 dprintk(1, "plane parameters verification failed: %d\n", ret);
306 return ret;
307 }
308 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
309 /*
310 * If the format's field is ALTERNATE, then the buffer's field
311 * should be either TOP or BOTTOM, not ALTERNATE since that
312 * makes no sense. The driver has to know whether the
313 * buffer represents a top or a bottom field in order to
314 * program any DMA correctly. Using ALTERNATE is wrong, since
315 * that just says that it is either a top or a bottom field,
316 * but not which of the two it is.
317 */
318 dprintk(1, "the field is incorrectly set to ALTERNATE "
319 "for an output buffer\n");
320 return -EINVAL;
321 }
Junghak Sungd6dd6452015-11-03 08:16:37 -0200322 vb->timestamp = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300323 vbuf->sequence = 0;
324
325 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
326 if (b->memory == VB2_MEMORY_USERPTR) {
327 for (plane = 0; plane < vb->num_planes; ++plane) {
328 planes[plane].m.userptr =
329 b->m.planes[plane].m.userptr;
330 planes[plane].length =
331 b->m.planes[plane].length;
332 }
333 }
334 if (b->memory == VB2_MEMORY_DMABUF) {
335 for (plane = 0; plane < vb->num_planes; ++plane) {
336 planes[plane].m.fd =
337 b->m.planes[plane].m.fd;
338 planes[plane].length =
339 b->m.planes[plane].length;
340 }
341 }
342
343 /* Fill in driver-provided information for OUTPUT types */
344 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
345 /*
346 * Will have to go up to b->length when API starts
347 * accepting variable number of planes.
348 *
349 * If bytesused == 0 for the output buffer, then fall
350 * back to the full buffer size. In that case
351 * userspace clearly never bothered to set it and
352 * it's a safe assumption that they really meant to
353 * use the full plane sizes.
354 *
355 * Some drivers, e.g. old codec drivers, use bytesused == 0
356 * as a way to indicate that streaming is finished.
357 * In that case, the driver should use the
358 * allow_zero_bytesused flag to keep old userspace
359 * applications working.
360 */
361 for (plane = 0; plane < vb->num_planes; ++plane) {
362 struct vb2_plane *pdst = &planes[plane];
363 struct v4l2_plane *psrc = &b->m.planes[plane];
364
365 if (psrc->bytesused == 0)
366 vb2_warn_zero_bytesused(vb);
367
368 if (vb->vb2_queue->allow_zero_bytesused)
369 pdst->bytesused = psrc->bytesused;
370 else
371 pdst->bytesused = psrc->bytesused ?
372 psrc->bytesused : pdst->length;
373 pdst->data_offset = psrc->data_offset;
374 }
375 }
376 } else {
377 /*
378 * Single-planar buffers do not use planes array,
379 * so fill in relevant v4l2_buffer struct fields instead.
380 * In videobuf we use our internal V4l2_planes struct for
381 * single-planar buffers as well, for simplicity.
382 *
383 * If bytesused == 0 for the output buffer, then fall back
384 * to the full buffer size as that's a sensible default.
385 *
386 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
387 * a way to indicate that streaming is finished. In that case,
388 * the driver should use the allow_zero_bytesused flag to keep
389 * old userspace applications working.
390 */
391 if (b->memory == VB2_MEMORY_USERPTR) {
392 planes[0].m.userptr = b->m.userptr;
393 planes[0].length = b->length;
394 }
395
396 if (b->memory == VB2_MEMORY_DMABUF) {
397 planes[0].m.fd = b->m.fd;
398 planes[0].length = b->length;
399 }
400
401 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
402 if (b->bytesused == 0)
403 vb2_warn_zero_bytesused(vb);
404
405 if (vb->vb2_queue->allow_zero_bytesused)
406 planes[0].bytesused = b->bytesused;
407 else
408 planes[0].bytesused = b->bytesused ?
409 b->bytesused : planes[0].length;
410 } else
411 planes[0].bytesused = 0;
412
413 }
414
415 /* Zero flags that the vb2 core handles */
416 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Junghak Sung959c3ef2015-11-03 08:16:38 -0200417 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
Junghak Sung3c5be982015-10-06 06:37:49 -0300418 /*
419 * Non-COPY timestamps and non-OUTPUT queues will get
420 * their timestamp and timestamp source flags from the
421 * queue.
422 */
423 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
424 }
425
426 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
427 /*
428 * For output buffers mask out the timecode flag:
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300429 * this will be handled later in vb2_qbuf().
Junghak Sung3c5be982015-10-06 06:37:49 -0300430 * The 'field' is valid metadata for this output buffer
431 * and so that needs to be copied here.
432 */
433 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
434 vbuf->field = b->field;
435 } else {
436 /* Zero any output buffer flags as this is a capture buffer */
437 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
438 }
439
440 return 0;
441}
442
443static const struct vb2_buf_ops v4l2_buf_ops = {
Sakari Ailus83934b72016-04-03 16:31:03 -0300444 .verify_planes_array = __verify_planes_array_core,
Junghak Sung3c5be982015-10-06 06:37:49 -0300445 .fill_user_buffer = __fill_v4l2_buffer,
446 .fill_vb2_buffer = __fill_vb2_buffer,
Junghak Sung959c3ef2015-11-03 08:16:38 -0200447 .copy_timestamp = __copy_timestamp,
Junghak Sung3c5be982015-10-06 06:37:49 -0300448};
449
450/**
451 * vb2_querybuf() - query video buffer information
452 * @q: videobuf queue
453 * @b: buffer struct passed from userspace to vidioc_querybuf handler
454 * in driver
455 *
456 * Should be called from vidioc_querybuf ioctl handler in driver.
457 * This function will verify the passed v4l2_buffer structure and fill the
458 * relevant information for the userspace.
459 *
460 * The return values from this function are intended to be directly returned
461 * from vidioc_querybuf handler in driver.
462 */
463int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
464{
465 struct vb2_buffer *vb;
466 int ret;
467
468 if (b->type != q->type) {
469 dprintk(1, "wrong buffer type\n");
470 return -EINVAL;
471 }
472
473 if (b->index >= q->num_buffers) {
474 dprintk(1, "buffer index out of range\n");
475 return -EINVAL;
476 }
477 vb = q->bufs[b->index];
478 ret = __verify_planes_array(vb, b);
Hans Verkuil10cc3b12015-11-20 08:32:00 -0200479 if (!ret)
480 vb2_core_querybuf(q, b->index, b);
481 return ret;
Junghak Sung3c5be982015-10-06 06:37:49 -0300482}
483EXPORT_SYMBOL(vb2_querybuf);
484
Junghak Sung3c5be982015-10-06 06:37:49 -0300485int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
486{
487 int ret = vb2_verify_memory_type(q, req->memory, req->type);
488
489 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
490}
491EXPORT_SYMBOL_GPL(vb2_reqbufs);
492
Junghak Sung3c5be982015-10-06 06:37:49 -0300493int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
494{
495 int ret;
496
497 if (vb2_fileio_is_active(q)) {
498 dprintk(1, "file io in progress\n");
499 return -EBUSY;
500 }
501
502 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
503
504 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
505}
506EXPORT_SYMBOL_GPL(vb2_prepare_buf);
507
Junghak Sung3c5be982015-10-06 06:37:49 -0300508int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
509{
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200510 unsigned requested_planes = 1;
511 unsigned requested_sizes[VIDEO_MAX_PLANES];
512 struct v4l2_format *f = &create->format;
513 int ret = vb2_verify_memory_type(q, create->memory, f->type);
514 unsigned i;
Junghak Sung3c5be982015-10-06 06:37:49 -0300515
516 create->index = q->num_buffers;
517 if (create->count == 0)
518 return ret != -EBUSY ? ret : 0;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200519
520 switch (f->type) {
521 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
522 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
523 requested_planes = f->fmt.pix_mp.num_planes;
524 if (requested_planes == 0 ||
525 requested_planes > VIDEO_MAX_PLANES)
526 return -EINVAL;
527 for (i = 0; i < requested_planes; i++)
528 requested_sizes[i] =
529 f->fmt.pix_mp.plane_fmt[i].sizeimage;
530 break;
531 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
532 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
533 requested_sizes[0] = f->fmt.pix.sizeimage;
534 break;
535 case V4L2_BUF_TYPE_VBI_CAPTURE:
536 case V4L2_BUF_TYPE_VBI_OUTPUT:
537 requested_sizes[0] = f->fmt.vbi.samples_per_line *
538 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
539 break;
540 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
541 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
542 requested_sizes[0] = f->fmt.sliced.io_size;
543 break;
544 case V4L2_BUF_TYPE_SDR_CAPTURE:
545 case V4L2_BUF_TYPE_SDR_OUTPUT:
546 requested_sizes[0] = f->fmt.sdr.buffersize;
547 break;
548 default:
549 return -EINVAL;
550 }
551 for (i = 0; i < requested_planes; i++)
552 if (requested_sizes[i] == 0)
553 return -EINVAL;
Junghak Sung3c5be982015-10-06 06:37:49 -0300554 return ret ? ret : vb2_core_create_bufs(q, create->memory,
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200555 &create->count, requested_planes, requested_sizes);
Junghak Sung3c5be982015-10-06 06:37:49 -0300556}
557EXPORT_SYMBOL_GPL(vb2_create_bufs);
558
Junghak Sung3c5be982015-10-06 06:37:49 -0300559int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
560{
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300561 int ret;
562
Junghak Sung3c5be982015-10-06 06:37:49 -0300563 if (vb2_fileio_is_active(q)) {
564 dprintk(1, "file io in progress\n");
565 return -EBUSY;
566 }
567
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300568 ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
569 return ret ? ret : vb2_core_qbuf(q, b->index, b);
Junghak Sung3c5be982015-10-06 06:37:49 -0300570}
571EXPORT_SYMBOL_GPL(vb2_qbuf);
572
Junghak Sung3c5be982015-10-06 06:37:49 -0300573int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
574{
Ricardo Ribalda5256e052016-06-20 09:47:23 -0300575 int ret;
576
Junghak Sung3c5be982015-10-06 06:37:49 -0300577 if (vb2_fileio_is_active(q)) {
578 dprintk(1, "file io in progress\n");
579 return -EBUSY;
580 }
Ricardo Ribalda5256e052016-06-20 09:47:23 -0300581
582 if (b->type != q->type) {
583 dprintk(1, "invalid buffer type\n");
584 return -EINVAL;
585 }
586
587 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
588
589 /*
590 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
591 * cleared.
592 */
593 b->flags &= ~V4L2_BUF_FLAG_DONE;
594
595 return ret;
Junghak Sung3c5be982015-10-06 06:37:49 -0300596}
597EXPORT_SYMBOL_GPL(vb2_dqbuf);
598
Junghak Sung3c5be982015-10-06 06:37:49 -0300599int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
600{
601 if (vb2_fileio_is_active(q)) {
602 dprintk(1, "file io in progress\n");
603 return -EBUSY;
604 }
605 return vb2_core_streamon(q, type);
606}
607EXPORT_SYMBOL_GPL(vb2_streamon);
608
Junghak Sung3c5be982015-10-06 06:37:49 -0300609int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
610{
611 if (vb2_fileio_is_active(q)) {
612 dprintk(1, "file io in progress\n");
613 return -EBUSY;
614 }
615 return vb2_core_streamoff(q, type);
616}
617EXPORT_SYMBOL_GPL(vb2_streamoff);
618
Junghak Sung3c5be982015-10-06 06:37:49 -0300619int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
620{
621 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
622 eb->plane, eb->flags);
623}
624EXPORT_SYMBOL_GPL(vb2_expbuf);
625
Junghak Sung3c5be982015-10-06 06:37:49 -0300626int vb2_queue_init(struct vb2_queue *q)
627{
628 /*
629 * Sanity check
630 */
631 if (WARN_ON(!q) ||
632 WARN_ON(q->timestamp_flags &
633 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
634 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
635 return -EINVAL;
636
637 /* Warn that the driver should choose an appropriate timestamp type */
638 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
639 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
640
641 /* Warn that vb2_memory should match with v4l2_memory */
642 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
643 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
644 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
645 return -EINVAL;
646
647 if (q->buf_struct_size == 0)
648 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
649
650 q->buf_ops = &v4l2_buf_ops;
651 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
652 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
Junghak Sung959c3ef2015-11-03 08:16:38 -0200653 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
654 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
Ricardo Ribaldab9387682016-04-25 06:04:45 -0300655 /*
656 * For compatibility with vb1: if QBUF hasn't been called yet, then
657 * return POLLERR as well. This only affects capture queues, output
658 * queues will always initialize waiting_for_buffers to false.
659 */
660 q->quirk_poll_must_check_waiting_for_buffers = true;
Junghak Sung3c5be982015-10-06 06:37:49 -0300661
662 return vb2_core_queue_init(q);
663}
664EXPORT_SYMBOL_GPL(vb2_queue_init);
665
Junghak Sung3c5be982015-10-06 06:37:49 -0300666void vb2_queue_release(struct vb2_queue *q)
667{
Junghak Sung3c5be982015-10-06 06:37:49 -0300668 vb2_core_queue_release(q);
669}
670EXPORT_SYMBOL_GPL(vb2_queue_release);
671
Junghak Sung3c5be982015-10-06 06:37:49 -0300672unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
673{
674 struct video_device *vfd = video_devdata(file);
675 unsigned long req_events = poll_requested_events(wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300676 unsigned int res = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300677
678 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
679 struct v4l2_fh *fh = file->private_data;
680
681 if (v4l2_event_pending(fh))
682 res = POLLPRI;
683 else if (req_events & POLLPRI)
684 poll_wait(file, &fh->wait, wait);
685 }
686
Junghak Sung49d8ab92015-11-03 08:16:39 -0200687 return res | vb2_core_poll(q, file, wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300688}
689EXPORT_SYMBOL_GPL(vb2_poll);
690
Junghak Sung3c5be982015-10-06 06:37:49 -0300691/*
692 * The following functions are not part of the vb2 core API, but are helper
693 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
694 * and struct vb2_ops.
695 * They contain boilerplate code that most if not all drivers have to do
696 * and so they simplify the driver code.
697 */
698
699/* The queue is busy if there is a owner and you are not that owner. */
700static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
701{
702 return vdev->queue->owner && vdev->queue->owner != file->private_data;
703}
704
705/* vb2 ioctl helpers */
706
707int vb2_ioctl_reqbufs(struct file *file, void *priv,
708 struct v4l2_requestbuffers *p)
709{
710 struct video_device *vdev = video_devdata(file);
711 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
712
713 if (res)
714 return res;
715 if (vb2_queue_is_busy(vdev, file))
716 return -EBUSY;
717 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
718 /* If count == 0, then the owner has released all buffers and he
719 is no longer owner of the queue. Otherwise we have a new owner. */
720 if (res == 0)
721 vdev->queue->owner = p->count ? file->private_data : NULL;
722 return res;
723}
724EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
725
726int vb2_ioctl_create_bufs(struct file *file, void *priv,
727 struct v4l2_create_buffers *p)
728{
729 struct video_device *vdev = video_devdata(file);
730 int res = vb2_verify_memory_type(vdev->queue, p->memory,
731 p->format.type);
732
733 p->index = vdev->queue->num_buffers;
734 /*
735 * If count == 0, then just check if memory and type are valid.
736 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
737 */
738 if (p->count == 0)
739 return res != -EBUSY ? res : 0;
740 if (res)
741 return res;
742 if (vb2_queue_is_busy(vdev, file))
743 return -EBUSY;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200744
745 res = vb2_create_bufs(vdev->queue, p);
Junghak Sung3c5be982015-10-06 06:37:49 -0300746 if (res == 0)
747 vdev->queue->owner = file->private_data;
748 return res;
749}
750EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
751
752int vb2_ioctl_prepare_buf(struct file *file, void *priv,
753 struct v4l2_buffer *p)
754{
755 struct video_device *vdev = video_devdata(file);
756
757 if (vb2_queue_is_busy(vdev, file))
758 return -EBUSY;
759 return vb2_prepare_buf(vdev->queue, p);
760}
761EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
762
763int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
764{
765 struct video_device *vdev = video_devdata(file);
766
767 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
768 return vb2_querybuf(vdev->queue, p);
769}
770EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
771
772int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
773{
774 struct video_device *vdev = video_devdata(file);
775
776 if (vb2_queue_is_busy(vdev, file))
777 return -EBUSY;
778 return vb2_qbuf(vdev->queue, p);
779}
780EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
781
782int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
783{
784 struct video_device *vdev = video_devdata(file);
785
786 if (vb2_queue_is_busy(vdev, file))
787 return -EBUSY;
788 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
789}
790EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
791
792int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
793{
794 struct video_device *vdev = video_devdata(file);
795
796 if (vb2_queue_is_busy(vdev, file))
797 return -EBUSY;
798 return vb2_streamon(vdev->queue, i);
799}
800EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
801
802int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
803{
804 struct video_device *vdev = video_devdata(file);
805
806 if (vb2_queue_is_busy(vdev, file))
807 return -EBUSY;
808 return vb2_streamoff(vdev->queue, i);
809}
810EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
811
812int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
813{
814 struct video_device *vdev = video_devdata(file);
815
816 if (vb2_queue_is_busy(vdev, file))
817 return -EBUSY;
818 return vb2_expbuf(vdev->queue, p);
819}
820EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
821
822/* v4l2_file_operations helpers */
823
824int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
825{
826 struct video_device *vdev = video_devdata(file);
827
828 return vb2_mmap(vdev->queue, vma);
829}
830EXPORT_SYMBOL_GPL(vb2_fop_mmap);
831
832int _vb2_fop_release(struct file *file, struct mutex *lock)
833{
834 struct video_device *vdev = video_devdata(file);
835
836 if (lock)
837 mutex_lock(lock);
838 if (file->private_data == vdev->queue->owner) {
839 vb2_queue_release(vdev->queue);
840 vdev->queue->owner = NULL;
841 }
842 if (lock)
843 mutex_unlock(lock);
844 return v4l2_fh_release(file);
845}
846EXPORT_SYMBOL_GPL(_vb2_fop_release);
847
848int vb2_fop_release(struct file *file)
849{
850 struct video_device *vdev = video_devdata(file);
851 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
852
853 return _vb2_fop_release(file, lock);
854}
855EXPORT_SYMBOL_GPL(vb2_fop_release);
856
857ssize_t vb2_fop_write(struct file *file, const char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 struct video_device *vdev = video_devdata(file);
861 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
862 int err = -EBUSY;
863
864 if (!(vdev->queue->io_modes & VB2_WRITE))
865 return -EINVAL;
866 if (lock && mutex_lock_interruptible(lock))
867 return -ERESTARTSYS;
868 if (vb2_queue_is_busy(vdev, file))
869 goto exit;
870 err = vb2_write(vdev->queue, buf, count, ppos,
871 file->f_flags & O_NONBLOCK);
872 if (vdev->queue->fileio)
873 vdev->queue->owner = file->private_data;
874exit:
875 if (lock)
876 mutex_unlock(lock);
877 return err;
878}
879EXPORT_SYMBOL_GPL(vb2_fop_write);
880
881ssize_t vb2_fop_read(struct file *file, char __user *buf,
882 size_t count, loff_t *ppos)
883{
884 struct video_device *vdev = video_devdata(file);
885 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
886 int err = -EBUSY;
887
888 if (!(vdev->queue->io_modes & VB2_READ))
889 return -EINVAL;
890 if (lock && mutex_lock_interruptible(lock))
891 return -ERESTARTSYS;
892 if (vb2_queue_is_busy(vdev, file))
893 goto exit;
894 err = vb2_read(vdev->queue, buf, count, ppos,
895 file->f_flags & O_NONBLOCK);
896 if (vdev->queue->fileio)
897 vdev->queue->owner = file->private_data;
898exit:
899 if (lock)
900 mutex_unlock(lock);
901 return err;
902}
903EXPORT_SYMBOL_GPL(vb2_fop_read);
904
905unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
906{
907 struct video_device *vdev = video_devdata(file);
908 struct vb2_queue *q = vdev->queue;
909 struct mutex *lock = q->lock ? q->lock : vdev->lock;
910 unsigned res;
911 void *fileio;
912
913 /*
914 * If this helper doesn't know how to lock, then you shouldn't be using
915 * it but you should write your own.
916 */
917 WARN_ON(!lock);
918
919 if (lock && mutex_lock_interruptible(lock))
920 return POLLERR;
921
922 fileio = q->fileio;
923
924 res = vb2_poll(vdev->queue, file, wait);
925
926 /* If fileio was started, then we have a new queue owner. */
927 if (!fileio && q->fileio)
928 q->owner = file->private_data;
929 if (lock)
930 mutex_unlock(lock);
931 return res;
932}
933EXPORT_SYMBOL_GPL(vb2_fop_poll);
934
935#ifndef CONFIG_MMU
936unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
937 unsigned long len, unsigned long pgoff, unsigned long flags)
938{
939 struct video_device *vdev = video_devdata(file);
940
941 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
942}
943EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
944#endif
945
946/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
947
948void vb2_ops_wait_prepare(struct vb2_queue *vq)
949{
950 mutex_unlock(vq->lock);
951}
952EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
953
954void vb2_ops_wait_finish(struct vb2_queue *vq)
955{
956 mutex_lock(vq->lock);
957}
958EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
959
Junghak Sungc1399902015-09-22 10:30:29 -0300960MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
961MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
962MODULE_LICENSE("GPL");