blob: 3529849d2218802c75055a53bfe54cc2c2d431ca [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) {
Mauro Carvalho Chehab87204272016-10-18 17:44:22 -020063 dprintk(1, "multi-planar buffer passed but planes array not provided\n");
Junghak Sung3c5be982015-10-06 06:37:49 -030064 return -EINVAL;
65 }
66
67 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
Mauro Carvalho Chehab87204272016-10-18 17:44:22 -020068 dprintk(1, "incorrect planes array length, expected %d, got %d\n",
69 vb->num_planes, b->length);
Junghak Sung3c5be982015-10-06 06:37:49 -030070 return -EINVAL;
71 }
72
73 return 0;
74}
75
Sakari Ailus83934b72016-04-03 16:31:03 -030076static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
77{
78 return __verify_planes_array(vb, pb);
79}
80
Junghak Sung3c5be982015-10-06 06:37:49 -030081/**
82 * __verify_length() - Verify that the bytesused value for each plane fits in
83 * the plane length and that the data offset doesn't exceed the bytesused value.
84 */
85static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
86{
87 unsigned int length;
88 unsigned int bytesused;
89 unsigned int plane;
90
91 if (!V4L2_TYPE_IS_OUTPUT(b->type))
92 return 0;
93
94 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
95 for (plane = 0; plane < vb->num_planes; ++plane) {
96 length = (b->memory == VB2_MEMORY_USERPTR ||
97 b->memory == VB2_MEMORY_DMABUF)
98 ? b->m.planes[plane].length
99 : vb->planes[plane].length;
100 bytesused = b->m.planes[plane].bytesused
101 ? b->m.planes[plane].bytesused : length;
102
103 if (b->m.planes[plane].bytesused > length)
104 return -EINVAL;
105
106 if (b->m.planes[plane].data_offset > 0 &&
107 b->m.planes[plane].data_offset >= bytesused)
108 return -EINVAL;
109 }
110 } else {
111 length = (b->memory == VB2_MEMORY_USERPTR)
112 ? b->length : vb->planes[0].length;
113
114 if (b->bytesused > length)
115 return -EINVAL;
116 }
117
118 return 0;
119}
120
Hans Verkuil10cc3b12015-11-20 08:32:00 -0200121static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
Junghak Sung3c5be982015-10-06 06:37:49 -0300122{
123 const struct v4l2_buffer *b = pb;
124 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
125 struct vb2_queue *q = vb->vb2_queue;
126
127 if (q->is_output) {
128 /*
129 * For output buffers copy the timestamp if needed,
130 * and the timecode field and flag if needed.
131 */
Junghak Sung959c3ef2015-11-03 08:16:38 -0200132 if (q->copy_timestamp)
Junghak Sungd6dd6452015-11-03 08:16:37 -0200133 vb->timestamp = timeval_to_ns(&b->timestamp);
Junghak Sung3c5be982015-10-06 06:37:49 -0300134 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
135 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
136 vbuf->timecode = b->timecode;
137 }
Junghak Sung3c5be982015-10-06 06:37:49 -0300138};
139
140static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
141{
142 static bool check_once;
143
144 if (check_once)
145 return;
146
147 check_once = true;
148 WARN_ON(1);
149
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 */
Mauro Carvalho Chehab87204272016-10-18 17:44:22 -0200318 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
Junghak Sung3c5be982015-10-06 06:37:49 -0300319 return -EINVAL;
320 }
Junghak Sungd6dd6452015-11-03 08:16:37 -0200321 vb->timestamp = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300322 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;
Junghak Sung959c3ef2015-11-03 08:16:38 -0200416 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
Junghak Sung3c5be982015-10-06 06:37:49 -0300417 /*
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:
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300428 * this will be handled later in vb2_qbuf().
Junghak Sung3c5be982015-10-06 06:37:49 -0300429 * 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 = {
Sakari Ailus83934b72016-04-03 16:31:03 -0300443 .verify_planes_array = __verify_planes_array_core,
Junghak Sung3c5be982015-10-06 06:37:49 -0300444 .fill_user_buffer = __fill_v4l2_buffer,
445 .fill_vb2_buffer = __fill_vb2_buffer,
Junghak Sung959c3ef2015-11-03 08:16:38 -0200446 .copy_timestamp = __copy_timestamp,
Junghak Sung3c5be982015-10-06 06:37:49 -0300447};
448
449/**
450 * vb2_querybuf() - query video buffer information
451 * @q: videobuf queue
452 * @b: buffer struct passed from userspace to vidioc_querybuf handler
453 * in driver
454 *
455 * Should be called from vidioc_querybuf ioctl handler in driver.
456 * This function will verify the passed v4l2_buffer structure and fill the
457 * relevant information for the userspace.
458 *
459 * The return values from this function are intended to be directly returned
460 * from vidioc_querybuf handler in driver.
461 */
462int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
463{
464 struct vb2_buffer *vb;
465 int ret;
466
467 if (b->type != q->type) {
468 dprintk(1, "wrong buffer type\n");
469 return -EINVAL;
470 }
471
472 if (b->index >= q->num_buffers) {
473 dprintk(1, "buffer index out of range\n");
474 return -EINVAL;
475 }
476 vb = q->bufs[b->index];
477 ret = __verify_planes_array(vb, b);
Hans Verkuil10cc3b12015-11-20 08:32:00 -0200478 if (!ret)
479 vb2_core_querybuf(q, b->index, b);
480 return ret;
Junghak Sung3c5be982015-10-06 06:37:49 -0300481}
482EXPORT_SYMBOL(vb2_querybuf);
483
Junghak Sung3c5be982015-10-06 06:37:49 -0300484int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
485{
486 int ret = vb2_verify_memory_type(q, req->memory, req->type);
487
488 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
489}
490EXPORT_SYMBOL_GPL(vb2_reqbufs);
491
Junghak Sung3c5be982015-10-06 06:37:49 -0300492int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
493{
494 int ret;
495
496 if (vb2_fileio_is_active(q)) {
497 dprintk(1, "file io in progress\n");
498 return -EBUSY;
499 }
500
501 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
502
503 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
504}
505EXPORT_SYMBOL_GPL(vb2_prepare_buf);
506
Junghak Sung3c5be982015-10-06 06:37:49 -0300507int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
508{
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200509 unsigned requested_planes = 1;
510 unsigned requested_sizes[VIDEO_MAX_PLANES];
511 struct v4l2_format *f = &create->format;
512 int ret = vb2_verify_memory_type(q, create->memory, f->type);
513 unsigned i;
Junghak Sung3c5be982015-10-06 06:37:49 -0300514
515 create->index = q->num_buffers;
516 if (create->count == 0)
517 return ret != -EBUSY ? ret : 0;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200518
519 switch (f->type) {
520 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
521 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
522 requested_planes = f->fmt.pix_mp.num_planes;
523 if (requested_planes == 0 ||
524 requested_planes > VIDEO_MAX_PLANES)
525 return -EINVAL;
526 for (i = 0; i < requested_planes; i++)
527 requested_sizes[i] =
528 f->fmt.pix_mp.plane_fmt[i].sizeimage;
529 break;
530 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
531 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
532 requested_sizes[0] = f->fmt.pix.sizeimage;
533 break;
534 case V4L2_BUF_TYPE_VBI_CAPTURE:
535 case V4L2_BUF_TYPE_VBI_OUTPUT:
536 requested_sizes[0] = f->fmt.vbi.samples_per_line *
537 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
538 break;
539 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
540 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
541 requested_sizes[0] = f->fmt.sliced.io_size;
542 break;
543 case V4L2_BUF_TYPE_SDR_CAPTURE:
544 case V4L2_BUF_TYPE_SDR_OUTPUT:
545 requested_sizes[0] = f->fmt.sdr.buffersize;
546 break;
547 default:
548 return -EINVAL;
549 }
550 for (i = 0; i < requested_planes; i++)
551 if (requested_sizes[i] == 0)
552 return -EINVAL;
Junghak Sung3c5be982015-10-06 06:37:49 -0300553 return ret ? ret : vb2_core_create_bufs(q, create->memory,
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200554 &create->count, requested_planes, requested_sizes);
Junghak Sung3c5be982015-10-06 06:37:49 -0300555}
556EXPORT_SYMBOL_GPL(vb2_create_bufs);
557
Junghak Sung3c5be982015-10-06 06:37:49 -0300558int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
559{
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300560 int ret;
561
Junghak Sung3c5be982015-10-06 06:37:49 -0300562 if (vb2_fileio_is_active(q)) {
563 dprintk(1, "file io in progress\n");
564 return -EBUSY;
565 }
566
Ricardo Ribaldadfadf022016-06-20 09:47:24 -0300567 ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
568 return ret ? ret : vb2_core_qbuf(q, b->index, b);
Junghak Sung3c5be982015-10-06 06:37:49 -0300569}
570EXPORT_SYMBOL_GPL(vb2_qbuf);
571
Junghak Sung3c5be982015-10-06 06:37:49 -0300572int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
573{
Ricardo Ribalda5256e052016-06-20 09:47:23 -0300574 int ret;
575
Junghak Sung3c5be982015-10-06 06:37:49 -0300576 if (vb2_fileio_is_active(q)) {
577 dprintk(1, "file io in progress\n");
578 return -EBUSY;
579 }
Ricardo Ribalda5256e052016-06-20 09:47:23 -0300580
581 if (b->type != q->type) {
582 dprintk(1, "invalid buffer type\n");
583 return -EINVAL;
584 }
585
586 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
587
588 /*
589 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
590 * cleared.
591 */
592 b->flags &= ~V4L2_BUF_FLAG_DONE;
593
594 return ret;
Junghak Sung3c5be982015-10-06 06:37:49 -0300595}
596EXPORT_SYMBOL_GPL(vb2_dqbuf);
597
Junghak Sung3c5be982015-10-06 06:37:49 -0300598int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
599{
600 if (vb2_fileio_is_active(q)) {
601 dprintk(1, "file io in progress\n");
602 return -EBUSY;
603 }
604 return vb2_core_streamon(q, type);
605}
606EXPORT_SYMBOL_GPL(vb2_streamon);
607
Junghak Sung3c5be982015-10-06 06:37:49 -0300608int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
609{
610 if (vb2_fileio_is_active(q)) {
611 dprintk(1, "file io in progress\n");
612 return -EBUSY;
613 }
614 return vb2_core_streamoff(q, type);
615}
616EXPORT_SYMBOL_GPL(vb2_streamoff);
617
Junghak Sung3c5be982015-10-06 06:37:49 -0300618int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
619{
620 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
621 eb->plane, eb->flags);
622}
623EXPORT_SYMBOL_GPL(vb2_expbuf);
624
Junghak Sung3c5be982015-10-06 06:37:49 -0300625int vb2_queue_init(struct vb2_queue *q)
626{
627 /*
628 * Sanity check
629 */
630 if (WARN_ON(!q) ||
631 WARN_ON(q->timestamp_flags &
632 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
633 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
634 return -EINVAL;
635
636 /* Warn that the driver should choose an appropriate timestamp type */
637 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
638 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
639
640 /* Warn that vb2_memory should match with v4l2_memory */
641 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
642 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
643 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
644 return -EINVAL;
645
646 if (q->buf_struct_size == 0)
647 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
648
649 q->buf_ops = &v4l2_buf_ops;
650 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
651 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
Junghak Sung959c3ef2015-11-03 08:16:38 -0200652 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
653 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
Ricardo Ribaldab9387682016-04-25 06:04:45 -0300654 /*
655 * For compatibility with vb1: if QBUF hasn't been called yet, then
656 * return POLLERR as well. This only affects capture queues, output
657 * queues will always initialize waiting_for_buffers to false.
658 */
659 q->quirk_poll_must_check_waiting_for_buffers = true;
Junghak Sung3c5be982015-10-06 06:37:49 -0300660
661 return vb2_core_queue_init(q);
662}
663EXPORT_SYMBOL_GPL(vb2_queue_init);
664
Junghak Sung3c5be982015-10-06 06:37:49 -0300665void vb2_queue_release(struct vb2_queue *q)
666{
Junghak Sung3c5be982015-10-06 06:37:49 -0300667 vb2_core_queue_release(q);
668}
669EXPORT_SYMBOL_GPL(vb2_queue_release);
670
Junghak Sung3c5be982015-10-06 06:37:49 -0300671unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
672{
673 struct video_device *vfd = video_devdata(file);
674 unsigned long req_events = poll_requested_events(wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300675 unsigned int res = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300676
677 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
678 struct v4l2_fh *fh = file->private_data;
679
680 if (v4l2_event_pending(fh))
681 res = POLLPRI;
682 else if (req_events & POLLPRI)
683 poll_wait(file, &fh->wait, wait);
684 }
685
Junghak Sung49d8ab92015-11-03 08:16:39 -0200686 return res | vb2_core_poll(q, file, wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300687}
688EXPORT_SYMBOL_GPL(vb2_poll);
689
Junghak Sung3c5be982015-10-06 06:37:49 -0300690/*
691 * The following functions are not part of the vb2 core API, but are helper
692 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
693 * and struct vb2_ops.
694 * They contain boilerplate code that most if not all drivers have to do
695 * and so they simplify the driver code.
696 */
697
698/* The queue is busy if there is a owner and you are not that owner. */
699static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
700{
701 return vdev->queue->owner && vdev->queue->owner != file->private_data;
702}
703
704/* vb2 ioctl helpers */
705
706int vb2_ioctl_reqbufs(struct file *file, void *priv,
707 struct v4l2_requestbuffers *p)
708{
709 struct video_device *vdev = video_devdata(file);
710 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
711
712 if (res)
713 return res;
714 if (vb2_queue_is_busy(vdev, file))
715 return -EBUSY;
716 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
717 /* If count == 0, then the owner has released all buffers and he
718 is no longer owner of the queue. Otherwise we have a new owner. */
719 if (res == 0)
720 vdev->queue->owner = p->count ? file->private_data : NULL;
721 return res;
722}
723EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
724
725int vb2_ioctl_create_bufs(struct file *file, void *priv,
726 struct v4l2_create_buffers *p)
727{
728 struct video_device *vdev = video_devdata(file);
729 int res = vb2_verify_memory_type(vdev->queue, p->memory,
730 p->format.type);
731
732 p->index = vdev->queue->num_buffers;
733 /*
734 * If count == 0, then just check if memory and type are valid.
735 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
736 */
737 if (p->count == 0)
738 return res != -EBUSY ? res : 0;
739 if (res)
740 return res;
741 if (vb2_queue_is_busy(vdev, file))
742 return -EBUSY;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200743
744 res = vb2_create_bufs(vdev->queue, p);
Junghak Sung3c5be982015-10-06 06:37:49 -0300745 if (res == 0)
746 vdev->queue->owner = file->private_data;
747 return res;
748}
749EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
750
751int vb2_ioctl_prepare_buf(struct file *file, void *priv,
752 struct v4l2_buffer *p)
753{
754 struct video_device *vdev = video_devdata(file);
755
756 if (vb2_queue_is_busy(vdev, file))
757 return -EBUSY;
758 return vb2_prepare_buf(vdev->queue, p);
759}
760EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
761
762int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
763{
764 struct video_device *vdev = video_devdata(file);
765
766 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
767 return vb2_querybuf(vdev->queue, p);
768}
769EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
770
771int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
772{
773 struct video_device *vdev = video_devdata(file);
774
775 if (vb2_queue_is_busy(vdev, file))
776 return -EBUSY;
777 return vb2_qbuf(vdev->queue, p);
778}
779EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
780
781int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
782{
783 struct video_device *vdev = video_devdata(file);
784
785 if (vb2_queue_is_busy(vdev, file))
786 return -EBUSY;
787 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
788}
789EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
790
791int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
792{
793 struct video_device *vdev = video_devdata(file);
794
795 if (vb2_queue_is_busy(vdev, file))
796 return -EBUSY;
797 return vb2_streamon(vdev->queue, i);
798}
799EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
800
801int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
802{
803 struct video_device *vdev = video_devdata(file);
804
805 if (vb2_queue_is_busy(vdev, file))
806 return -EBUSY;
807 return vb2_streamoff(vdev->queue, i);
808}
809EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
810
811int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
812{
813 struct video_device *vdev = video_devdata(file);
814
815 if (vb2_queue_is_busy(vdev, file))
816 return -EBUSY;
817 return vb2_expbuf(vdev->queue, p);
818}
819EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
820
821/* v4l2_file_operations helpers */
822
823int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
824{
825 struct video_device *vdev = video_devdata(file);
826
827 return vb2_mmap(vdev->queue, vma);
828}
829EXPORT_SYMBOL_GPL(vb2_fop_mmap);
830
831int _vb2_fop_release(struct file *file, struct mutex *lock)
832{
833 struct video_device *vdev = video_devdata(file);
834
835 if (lock)
836 mutex_lock(lock);
837 if (file->private_data == vdev->queue->owner) {
838 vb2_queue_release(vdev->queue);
839 vdev->queue->owner = NULL;
840 }
841 if (lock)
842 mutex_unlock(lock);
843 return v4l2_fh_release(file);
844}
845EXPORT_SYMBOL_GPL(_vb2_fop_release);
846
847int vb2_fop_release(struct file *file)
848{
849 struct video_device *vdev = video_devdata(file);
850 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
851
852 return _vb2_fop_release(file, lock);
853}
854EXPORT_SYMBOL_GPL(vb2_fop_release);
855
856ssize_t vb2_fop_write(struct file *file, const char __user *buf,
857 size_t count, loff_t *ppos)
858{
859 struct video_device *vdev = video_devdata(file);
860 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
861 int err = -EBUSY;
862
863 if (!(vdev->queue->io_modes & VB2_WRITE))
864 return -EINVAL;
865 if (lock && mutex_lock_interruptible(lock))
866 return -ERESTARTSYS;
867 if (vb2_queue_is_busy(vdev, file))
868 goto exit;
869 err = vb2_write(vdev->queue, buf, count, ppos,
870 file->f_flags & O_NONBLOCK);
871 if (vdev->queue->fileio)
872 vdev->queue->owner = file->private_data;
873exit:
874 if (lock)
875 mutex_unlock(lock);
876 return err;
877}
878EXPORT_SYMBOL_GPL(vb2_fop_write);
879
880ssize_t vb2_fop_read(struct file *file, char __user *buf,
881 size_t count, loff_t *ppos)
882{
883 struct video_device *vdev = video_devdata(file);
884 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
885 int err = -EBUSY;
886
887 if (!(vdev->queue->io_modes & VB2_READ))
888 return -EINVAL;
889 if (lock && mutex_lock_interruptible(lock))
890 return -ERESTARTSYS;
891 if (vb2_queue_is_busy(vdev, file))
892 goto exit;
893 err = vb2_read(vdev->queue, buf, count, ppos,
894 file->f_flags & O_NONBLOCK);
895 if (vdev->queue->fileio)
896 vdev->queue->owner = file->private_data;
897exit:
898 if (lock)
899 mutex_unlock(lock);
900 return err;
901}
902EXPORT_SYMBOL_GPL(vb2_fop_read);
903
904unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
905{
906 struct video_device *vdev = video_devdata(file);
907 struct vb2_queue *q = vdev->queue;
908 struct mutex *lock = q->lock ? q->lock : vdev->lock;
909 unsigned res;
910 void *fileio;
911
912 /*
913 * If this helper doesn't know how to lock, then you shouldn't be using
914 * it but you should write your own.
915 */
916 WARN_ON(!lock);
917
918 if (lock && mutex_lock_interruptible(lock))
919 return POLLERR;
920
921 fileio = q->fileio;
922
923 res = vb2_poll(vdev->queue, file, wait);
924
925 /* If fileio was started, then we have a new queue owner. */
926 if (!fileio && q->fileio)
927 q->owner = file->private_data;
928 if (lock)
929 mutex_unlock(lock);
930 return res;
931}
932EXPORT_SYMBOL_GPL(vb2_fop_poll);
933
934#ifndef CONFIG_MMU
935unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
936 unsigned long len, unsigned long pgoff, unsigned long flags)
937{
938 struct video_device *vdev = video_devdata(file);
939
940 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
941}
942EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
943#endif
944
945/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
946
947void vb2_ops_wait_prepare(struct vb2_queue *vq)
948{
949 mutex_unlock(vq->lock);
950}
951EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
952
953void vb2_ops_wait_finish(struct vb2_queue *vq)
954{
955 mutex_lock(vq->lock);
956}
957EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
958
Junghak Sungc1399902015-09-22 10:30:29 -0300959MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
960MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
961MODULE_LICENSE("GPL");