blob: 91728c18430d82f662b58546cbc9ad76055946a6 [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 Sung3c5be982015-10-06 06:37:49 -030034#include "videobuf2-internal.h"
35
36/* Flags that are set by the vb2 core */
37#define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
38 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
39 V4L2_BUF_FLAG_PREPARED | \
40 V4L2_BUF_FLAG_TIMESTAMP_MASK)
41/* Output buffer flags that should be passed on to the driver */
42#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
43 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
44
45/**
46 * __verify_planes_array() - verify that the planes array passed in struct
47 * v4l2_buffer from userspace can be safely used
48 */
49static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
50{
51 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
52 return 0;
53
54 /* Is memory for copying plane information present? */
Julia Lawalle88a3f82015-11-13 08:46:00 -020055 if (b->m.planes == NULL) {
Junghak Sung3c5be982015-10-06 06:37:49 -030056 dprintk(1, "multi-planar buffer passed but "
57 "planes array not provided\n");
58 return -EINVAL;
59 }
60
61 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
62 dprintk(1, "incorrect planes array length, "
63 "expected %d, got %d\n", vb->num_planes, b->length);
64 return -EINVAL;
65 }
66
67 return 0;
68}
69
70/**
71 * __verify_length() - Verify that the bytesused value for each plane fits in
72 * the plane length and that the data offset doesn't exceed the bytesused value.
73 */
74static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
75{
76 unsigned int length;
77 unsigned int bytesused;
78 unsigned int plane;
79
80 if (!V4L2_TYPE_IS_OUTPUT(b->type))
81 return 0;
82
83 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
84 for (plane = 0; plane < vb->num_planes; ++plane) {
85 length = (b->memory == VB2_MEMORY_USERPTR ||
86 b->memory == VB2_MEMORY_DMABUF)
87 ? b->m.planes[plane].length
88 : vb->planes[plane].length;
89 bytesused = b->m.planes[plane].bytesused
90 ? b->m.planes[plane].bytesused : length;
91
92 if (b->m.planes[plane].bytesused > length)
93 return -EINVAL;
94
95 if (b->m.planes[plane].data_offset > 0 &&
96 b->m.planes[plane].data_offset >= bytesused)
97 return -EINVAL;
98 }
99 } else {
100 length = (b->memory == VB2_MEMORY_USERPTR)
101 ? b->length : vb->planes[0].length;
102
103 if (b->bytesused > length)
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
Junghak Sung959c3ef2015-11-03 08:16:38 -0200110static int __copy_timestamp(struct vb2_buffer *vb, const void *pb)
Junghak Sung3c5be982015-10-06 06:37:49 -0300111{
112 const struct v4l2_buffer *b = pb;
113 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
114 struct vb2_queue *q = vb->vb2_queue;
115
116 if (q->is_output) {
117 /*
118 * For output buffers copy the timestamp if needed,
119 * and the timecode field and flag if needed.
120 */
Junghak Sung959c3ef2015-11-03 08:16:38 -0200121 if (q->copy_timestamp)
Junghak Sungd6dd6452015-11-03 08:16:37 -0200122 vb->timestamp = timeval_to_ns(&b->timestamp);
Junghak Sung3c5be982015-10-06 06:37:49 -0300123 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
124 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
125 vbuf->timecode = b->timecode;
126 }
127 return 0;
128};
129
130static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
131{
132 static bool check_once;
133
134 if (check_once)
135 return;
136
137 check_once = true;
138 WARN_ON(1);
139
140 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
141 if (vb->vb2_queue->allow_zero_bytesused)
142 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
143 else
144 pr_warn("use the actual size instead.\n");
145}
146
147static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
148 const char *opname)
149{
150 if (b->type != q->type) {
151 dprintk(1, "%s: invalid buffer type\n", opname);
152 return -EINVAL;
153 }
154
155 if (b->index >= q->num_buffers) {
156 dprintk(1, "%s: buffer index out of range\n", opname);
157 return -EINVAL;
158 }
159
160 if (q->bufs[b->index] == NULL) {
161 /* Should never happen */
162 dprintk(1, "%s: buffer is NULL\n", opname);
163 return -EINVAL;
164 }
165
166 if (b->memory != q->memory) {
167 dprintk(1, "%s: invalid memory type\n", opname);
168 return -EINVAL;
169 }
170
171 return __verify_planes_array(q->bufs[b->index], b);
172}
173
174/**
175 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
176 * returned to userspace
177 */
178static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
179{
180 struct v4l2_buffer *b = pb;
181 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
182 struct vb2_queue *q = vb->vb2_queue;
183 unsigned int plane;
184
185 /* Copy back data such as timestamp, flags, etc. */
186 b->index = vb->index;
187 b->type = vb->type;
188 b->memory = vb->memory;
189 b->bytesused = 0;
190
191 b->flags = vbuf->flags;
192 b->field = vbuf->field;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200193 b->timestamp = ns_to_timeval(vb->timestamp);
Junghak Sung3c5be982015-10-06 06:37:49 -0300194 b->timecode = vbuf->timecode;
195 b->sequence = vbuf->sequence;
196 b->reserved2 = 0;
197 b->reserved = 0;
198
199 if (q->is_multiplanar) {
200 /*
201 * Fill in plane-related data if userspace provided an array
202 * for it. The caller has already verified memory and size.
203 */
204 b->length = vb->num_planes;
205 for (plane = 0; plane < vb->num_planes; ++plane) {
206 struct v4l2_plane *pdst = &b->m.planes[plane];
207 struct vb2_plane *psrc = &vb->planes[plane];
208
209 pdst->bytesused = psrc->bytesused;
210 pdst->length = psrc->length;
211 if (q->memory == VB2_MEMORY_MMAP)
212 pdst->m.mem_offset = psrc->m.offset;
213 else if (q->memory == VB2_MEMORY_USERPTR)
214 pdst->m.userptr = psrc->m.userptr;
215 else if (q->memory == VB2_MEMORY_DMABUF)
216 pdst->m.fd = psrc->m.fd;
217 pdst->data_offset = psrc->data_offset;
218 memset(pdst->reserved, 0, sizeof(pdst->reserved));
219 }
220 } else {
221 /*
222 * We use length and offset in v4l2_planes array even for
223 * single-planar buffers, but userspace does not.
224 */
225 b->length = vb->planes[0].length;
226 b->bytesused = vb->planes[0].bytesused;
227 if (q->memory == VB2_MEMORY_MMAP)
228 b->m.offset = vb->planes[0].m.offset;
229 else if (q->memory == VB2_MEMORY_USERPTR)
230 b->m.userptr = vb->planes[0].m.userptr;
231 else if (q->memory == VB2_MEMORY_DMABUF)
232 b->m.fd = vb->planes[0].m.fd;
233 }
234
235 /*
236 * Clear any buffer state related flags.
237 */
238 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
239 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
Junghak Sung959c3ef2015-11-03 08:16:38 -0200240 if (!q->copy_timestamp) {
Junghak Sung3c5be982015-10-06 06:37:49 -0300241 /*
242 * For non-COPY timestamps, drop timestamp source bits
243 * and obtain the timestamp source from the queue.
244 */
245 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
246 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
247 }
248
249 switch (vb->state) {
250 case VB2_BUF_STATE_QUEUED:
251 case VB2_BUF_STATE_ACTIVE:
252 b->flags |= V4L2_BUF_FLAG_QUEUED;
253 break;
254 case VB2_BUF_STATE_ERROR:
255 b->flags |= V4L2_BUF_FLAG_ERROR;
256 /* fall through */
257 case VB2_BUF_STATE_DONE:
258 b->flags |= V4L2_BUF_FLAG_DONE;
259 break;
260 case VB2_BUF_STATE_PREPARED:
261 b->flags |= V4L2_BUF_FLAG_PREPARED;
262 break;
263 case VB2_BUF_STATE_PREPARING:
264 case VB2_BUF_STATE_DEQUEUED:
265 case VB2_BUF_STATE_REQUEUEING:
266 /* nothing */
267 break;
268 }
269
270 if (vb2_buffer_in_use(q, vb))
271 b->flags |= V4L2_BUF_FLAG_MAPPED;
272
Junghak Sungdcbc2162015-11-03 08:16:40 -0200273 if (!q->is_output &&
274 b->flags & V4L2_BUF_FLAG_DONE &&
275 b->flags & V4L2_BUF_FLAG_LAST)
276 q->last_buffer_dequeued = true;
277
Junghak Sung3c5be982015-10-06 06:37:49 -0300278 return 0;
279}
280
281/**
282 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
283 * v4l2_buffer by the userspace. It also verifies that struct
284 * v4l2_buffer has a valid number of planes.
285 */
286static int __fill_vb2_buffer(struct vb2_buffer *vb,
287 const void *pb, struct vb2_plane *planes)
288{
289 struct vb2_queue *q = vb->vb2_queue;
290 const struct v4l2_buffer *b = pb;
291 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
292 unsigned int plane;
293 int ret;
294
295 ret = __verify_length(vb, b);
296 if (ret < 0) {
297 dprintk(1, "plane parameters verification failed: %d\n", ret);
298 return ret;
299 }
300 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
301 /*
302 * If the format's field is ALTERNATE, then the buffer's field
303 * should be either TOP or BOTTOM, not ALTERNATE since that
304 * makes no sense. The driver has to know whether the
305 * buffer represents a top or a bottom field in order to
306 * program any DMA correctly. Using ALTERNATE is wrong, since
307 * that just says that it is either a top or a bottom field,
308 * but not which of the two it is.
309 */
310 dprintk(1, "the field is incorrectly set to ALTERNATE "
311 "for an output buffer\n");
312 return -EINVAL;
313 }
Junghak Sungd6dd6452015-11-03 08:16:37 -0200314 vb->timestamp = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300315 vbuf->sequence = 0;
316
317 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
318 if (b->memory == VB2_MEMORY_USERPTR) {
319 for (plane = 0; plane < vb->num_planes; ++plane) {
320 planes[plane].m.userptr =
321 b->m.planes[plane].m.userptr;
322 planes[plane].length =
323 b->m.planes[plane].length;
324 }
325 }
326 if (b->memory == VB2_MEMORY_DMABUF) {
327 for (plane = 0; plane < vb->num_planes; ++plane) {
328 planes[plane].m.fd =
329 b->m.planes[plane].m.fd;
330 planes[plane].length =
331 b->m.planes[plane].length;
332 }
333 }
334
335 /* Fill in driver-provided information for OUTPUT types */
336 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
337 /*
338 * Will have to go up to b->length when API starts
339 * accepting variable number of planes.
340 *
341 * If bytesused == 0 for the output buffer, then fall
342 * back to the full buffer size. In that case
343 * userspace clearly never bothered to set it and
344 * it's a safe assumption that they really meant to
345 * use the full plane sizes.
346 *
347 * Some drivers, e.g. old codec drivers, use bytesused == 0
348 * as a way to indicate that streaming is finished.
349 * In that case, the driver should use the
350 * allow_zero_bytesused flag to keep old userspace
351 * applications working.
352 */
353 for (plane = 0; plane < vb->num_planes; ++plane) {
354 struct vb2_plane *pdst = &planes[plane];
355 struct v4l2_plane *psrc = &b->m.planes[plane];
356
357 if (psrc->bytesused == 0)
358 vb2_warn_zero_bytesused(vb);
359
360 if (vb->vb2_queue->allow_zero_bytesused)
361 pdst->bytesused = psrc->bytesused;
362 else
363 pdst->bytesused = psrc->bytesused ?
364 psrc->bytesused : pdst->length;
365 pdst->data_offset = psrc->data_offset;
366 }
367 }
368 } else {
369 /*
370 * Single-planar buffers do not use planes array,
371 * so fill in relevant v4l2_buffer struct fields instead.
372 * In videobuf we use our internal V4l2_planes struct for
373 * single-planar buffers as well, for simplicity.
374 *
375 * If bytesused == 0 for the output buffer, then fall back
376 * to the full buffer size as that's a sensible default.
377 *
378 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
379 * a way to indicate that streaming is finished. In that case,
380 * the driver should use the allow_zero_bytesused flag to keep
381 * old userspace applications working.
382 */
383 if (b->memory == VB2_MEMORY_USERPTR) {
384 planes[0].m.userptr = b->m.userptr;
385 planes[0].length = b->length;
386 }
387
388 if (b->memory == VB2_MEMORY_DMABUF) {
389 planes[0].m.fd = b->m.fd;
390 planes[0].length = b->length;
391 }
392
393 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
394 if (b->bytesused == 0)
395 vb2_warn_zero_bytesused(vb);
396
397 if (vb->vb2_queue->allow_zero_bytesused)
398 planes[0].bytesused = b->bytesused;
399 else
400 planes[0].bytesused = b->bytesused ?
401 b->bytesused : planes[0].length;
402 } else
403 planes[0].bytesused = 0;
404
405 }
406
407 /* Zero flags that the vb2 core handles */
408 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
Junghak Sung959c3ef2015-11-03 08:16:38 -0200409 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
Junghak Sung3c5be982015-10-06 06:37:49 -0300410 /*
411 * Non-COPY timestamps and non-OUTPUT queues will get
412 * their timestamp and timestamp source flags from the
413 * queue.
414 */
415 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
416 }
417
418 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
419 /*
420 * For output buffers mask out the timecode flag:
421 * this will be handled later in vb2_internal_qbuf().
422 * The 'field' is valid metadata for this output buffer
423 * and so that needs to be copied here.
424 */
425 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
426 vbuf->field = b->field;
427 } else {
428 /* Zero any output buffer flags as this is a capture buffer */
429 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
430 }
431
432 return 0;
433}
434
435static const struct vb2_buf_ops v4l2_buf_ops = {
436 .fill_user_buffer = __fill_v4l2_buffer,
437 .fill_vb2_buffer = __fill_vb2_buffer,
Junghak Sung959c3ef2015-11-03 08:16:38 -0200438 .copy_timestamp = __copy_timestamp,
Junghak Sung3c5be982015-10-06 06:37:49 -0300439};
440
441/**
442 * vb2_querybuf() - query video buffer information
443 * @q: videobuf queue
444 * @b: buffer struct passed from userspace to vidioc_querybuf handler
445 * in driver
446 *
447 * Should be called from vidioc_querybuf ioctl handler in driver.
448 * This function will verify the passed v4l2_buffer structure and fill the
449 * relevant information for the userspace.
450 *
451 * The return values from this function are intended to be directly returned
452 * from vidioc_querybuf handler in driver.
453 */
454int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
455{
456 struct vb2_buffer *vb;
457 int ret;
458
459 if (b->type != q->type) {
460 dprintk(1, "wrong buffer type\n");
461 return -EINVAL;
462 }
463
464 if (b->index >= q->num_buffers) {
465 dprintk(1, "buffer index out of range\n");
466 return -EINVAL;
467 }
468 vb = q->bufs[b->index];
469 ret = __verify_planes_array(vb, b);
470
471 return ret ? ret : vb2_core_querybuf(q, b->index, b);
472}
473EXPORT_SYMBOL(vb2_querybuf);
474
475/**
476 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
477 * the memory and type values.
478 * @q: videobuf2 queue
479 * @req: struct passed from userspace to vidioc_reqbufs handler
480 * in driver
481 */
482int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
483{
484 int ret = vb2_verify_memory_type(q, req->memory, req->type);
485
486 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
487}
488EXPORT_SYMBOL_GPL(vb2_reqbufs);
489
490/**
491 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
492 * @q: videobuf2 queue
493 * @b: buffer structure passed from userspace to vidioc_prepare_buf
494 * handler in driver
495 *
496 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
497 * This function:
498 * 1) verifies the passed buffer,
499 * 2) calls buf_prepare callback in the driver (if provided), in which
500 * driver-specific buffer initialization can be performed,
501 *
502 * The return values from this function are intended to be directly returned
503 * from vidioc_prepare_buf handler in driver.
504 */
505int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
506{
507 int ret;
508
509 if (vb2_fileio_is_active(q)) {
510 dprintk(1, "file io in progress\n");
511 return -EBUSY;
512 }
513
514 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
515
516 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
517}
518EXPORT_SYMBOL_GPL(vb2_prepare_buf);
519
520/**
521 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
522 * the memory and type values.
523 * @q: videobuf2 queue
524 * @create: creation parameters, passed from userspace to vidioc_create_bufs
525 * handler in driver
526 */
527int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
528{
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200529 unsigned requested_planes = 1;
530 unsigned requested_sizes[VIDEO_MAX_PLANES];
531 struct v4l2_format *f = &create->format;
532 int ret = vb2_verify_memory_type(q, create->memory, f->type);
533 unsigned i;
Junghak Sung3c5be982015-10-06 06:37:49 -0300534
535 create->index = q->num_buffers;
536 if (create->count == 0)
537 return ret != -EBUSY ? ret : 0;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200538
539 switch (f->type) {
540 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
541 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
542 requested_planes = f->fmt.pix_mp.num_planes;
543 if (requested_planes == 0 ||
544 requested_planes > VIDEO_MAX_PLANES)
545 return -EINVAL;
546 for (i = 0; i < requested_planes; i++)
547 requested_sizes[i] =
548 f->fmt.pix_mp.plane_fmt[i].sizeimage;
549 break;
550 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
551 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
552 requested_sizes[0] = f->fmt.pix.sizeimage;
553 break;
554 case V4L2_BUF_TYPE_VBI_CAPTURE:
555 case V4L2_BUF_TYPE_VBI_OUTPUT:
556 requested_sizes[0] = f->fmt.vbi.samples_per_line *
557 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
558 break;
559 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
560 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
561 requested_sizes[0] = f->fmt.sliced.io_size;
562 break;
563 case V4L2_BUF_TYPE_SDR_CAPTURE:
564 case V4L2_BUF_TYPE_SDR_OUTPUT:
565 requested_sizes[0] = f->fmt.sdr.buffersize;
566 break;
567 default:
568 return -EINVAL;
569 }
570 for (i = 0; i < requested_planes; i++)
571 if (requested_sizes[i] == 0)
572 return -EINVAL;
Junghak Sung3c5be982015-10-06 06:37:49 -0300573 return ret ? ret : vb2_core_create_bufs(q, create->memory,
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200574 &create->count, requested_planes, requested_sizes);
Junghak Sung3c5be982015-10-06 06:37:49 -0300575}
576EXPORT_SYMBOL_GPL(vb2_create_bufs);
577
578static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
579{
580 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
581
582 return ret ? ret : vb2_core_qbuf(q, b->index, b);
583}
584
585/**
586 * vb2_qbuf() - Queue a buffer from userspace
587 * @q: videobuf2 queue
588 * @b: buffer structure passed from userspace to vidioc_qbuf handler
589 * in driver
590 *
591 * Should be called from vidioc_qbuf ioctl handler of a driver.
592 * This function:
593 * 1) verifies the passed buffer,
594 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
595 * which driver-specific buffer initialization can be performed,
596 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
597 * callback for processing.
598 *
599 * The return values from this function are intended to be directly returned
600 * from vidioc_qbuf handler in driver.
601 */
602int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
603{
604 if (vb2_fileio_is_active(q)) {
605 dprintk(1, "file io in progress\n");
606 return -EBUSY;
607 }
608
609 return vb2_internal_qbuf(q, b);
610}
611EXPORT_SYMBOL_GPL(vb2_qbuf);
612
613static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
614 bool nonblocking)
615{
616 int ret;
617
618 if (b->type != q->type) {
619 dprintk(1, "invalid buffer type\n");
620 return -EINVAL;
621 }
622
623 ret = vb2_core_dqbuf(q, b, nonblocking);
624
Junghak Sung3c5be982015-10-06 06:37:49 -0300625 return ret;
626}
627
628/**
629 * vb2_dqbuf() - Dequeue a buffer to the userspace
630 * @q: videobuf2 queue
631 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
632 * in driver
633 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
634 * buffers ready for dequeuing are present. Normally the driver
635 * would be passing (file->f_flags & O_NONBLOCK) here
636 *
637 * Should be called from vidioc_dqbuf ioctl handler of a driver.
638 * This function:
639 * 1) verifies the passed buffer,
640 * 2) calls buf_finish callback in the driver (if provided), in which
641 * driver can perform any additional operations that may be required before
642 * returning the buffer to userspace, such as cache sync,
643 * 3) the buffer struct members are filled with relevant information for
644 * the userspace.
645 *
646 * The return values from this function are intended to be directly returned
647 * from vidioc_dqbuf handler in driver.
648 */
649int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
650{
651 if (vb2_fileio_is_active(q)) {
652 dprintk(1, "file io in progress\n");
653 return -EBUSY;
654 }
655 return vb2_internal_dqbuf(q, b, nonblocking);
656}
657EXPORT_SYMBOL_GPL(vb2_dqbuf);
658
659/**
660 * vb2_streamon - start streaming
661 * @q: videobuf2 queue
662 * @type: type argument passed from userspace to vidioc_streamon handler
663 *
664 * Should be called from vidioc_streamon handler of a driver.
665 * This function:
666 * 1) verifies current state
667 * 2) passes any previously queued buffers to the driver and starts streaming
668 *
669 * The return values from this function are intended to be directly returned
670 * from vidioc_streamon handler in the driver.
671 */
672int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
673{
674 if (vb2_fileio_is_active(q)) {
675 dprintk(1, "file io in progress\n");
676 return -EBUSY;
677 }
678 return vb2_core_streamon(q, type);
679}
680EXPORT_SYMBOL_GPL(vb2_streamon);
681
682/**
683 * vb2_streamoff - stop streaming
684 * @q: videobuf2 queue
685 * @type: type argument passed from userspace to vidioc_streamoff handler
686 *
687 * Should be called from vidioc_streamoff handler of a driver.
688 * This function:
689 * 1) verifies current state,
690 * 2) stop streaming and dequeues any queued buffers, including those previously
691 * passed to the driver (after waiting for the driver to finish).
692 *
693 * This call can be used for pausing playback.
694 * The return values from this function are intended to be directly returned
695 * from vidioc_streamoff handler in the driver
696 */
697int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
698{
699 if (vb2_fileio_is_active(q)) {
700 dprintk(1, "file io in progress\n");
701 return -EBUSY;
702 }
703 return vb2_core_streamoff(q, type);
704}
705EXPORT_SYMBOL_GPL(vb2_streamoff);
706
707/**
708 * vb2_expbuf() - Export a buffer as a file descriptor
709 * @q: videobuf2 queue
710 * @eb: export buffer structure passed from userspace to vidioc_expbuf
711 * handler in driver
712 *
713 * The return values from this function are intended to be directly returned
714 * from vidioc_expbuf handler in driver.
715 */
716int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
717{
718 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
719 eb->plane, eb->flags);
720}
721EXPORT_SYMBOL_GPL(vb2_expbuf);
722
723/**
724 * vb2_queue_init() - initialize a videobuf2 queue
725 * @q: videobuf2 queue; this structure should be allocated in driver
726 *
727 * The vb2_queue structure should be allocated by the driver. The driver is
728 * responsible of clearing it's content and setting initial values for some
729 * required entries before calling this function.
730 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
731 * to the struct vb2_queue description in include/media/videobuf2-core.h
732 * for more information.
733 */
734int vb2_queue_init(struct vb2_queue *q)
735{
736 /*
737 * Sanity check
738 */
739 if (WARN_ON(!q) ||
740 WARN_ON(q->timestamp_flags &
741 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
742 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
743 return -EINVAL;
744
745 /* Warn that the driver should choose an appropriate timestamp type */
746 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
747 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
748
749 /* Warn that vb2_memory should match with v4l2_memory */
750 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
751 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
752 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
753 return -EINVAL;
754
755 if (q->buf_struct_size == 0)
756 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
757
758 q->buf_ops = &v4l2_buf_ops;
759 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
760 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
Junghak Sung959c3ef2015-11-03 08:16:38 -0200761 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
762 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
Junghak Sung3c5be982015-10-06 06:37:49 -0300763
764 return vb2_core_queue_init(q);
765}
766EXPORT_SYMBOL_GPL(vb2_queue_init);
767
768static int __vb2_init_fileio(struct vb2_queue *q, int read);
769static int __vb2_cleanup_fileio(struct vb2_queue *q);
770
771/**
772 * vb2_queue_release() - stop streaming, release the queue and free memory
773 * @q: videobuf2 queue
774 *
775 * This function stops streaming and performs necessary clean ups, including
776 * freeing video buffer memory. The driver is responsible for freeing
777 * the vb2_queue structure itself.
778 */
779void vb2_queue_release(struct vb2_queue *q)
780{
781 __vb2_cleanup_fileio(q);
782 vb2_core_queue_release(q);
783}
784EXPORT_SYMBOL_GPL(vb2_queue_release);
785
786/**
Junghak Sung49d8ab92015-11-03 08:16:39 -0200787 * vb2_core_poll() - implements poll userspace operation
788 * @q: videobuf2 queue
789 * @file: file argument passed to the poll file operation handler
790 * @wait: wait argument passed to the poll file operation handler
791 *
792 * This function implements poll file operation handler for a driver.
793 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
794 * be informed that the file descriptor of a video device is available for
795 * reading.
796 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
797 * will be reported as available for writing.
798 *
799 * The return values from this function are intended to be directly returned
800 * from poll handler in driver.
801 */
802unsigned int vb2_core_poll(struct vb2_queue *q, struct file *file,
803 poll_table *wait)
804{
805 unsigned long req_events = poll_requested_events(wait);
806 struct vb2_buffer *vb = NULL;
807 unsigned long flags;
808
809 if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
810 return 0;
811 if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
812 return 0;
813
814 /*
815 * Start file I/O emulator only if streaming API has not been used yet.
816 */
817 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
818 if (!q->is_output && (q->io_modes & VB2_READ) &&
819 (req_events & (POLLIN | POLLRDNORM))) {
820 if (__vb2_init_fileio(q, 1))
821 return POLLERR;
822 }
823 if (q->is_output && (q->io_modes & VB2_WRITE) &&
824 (req_events & (POLLOUT | POLLWRNORM))) {
825 if (__vb2_init_fileio(q, 0))
826 return POLLERR;
827 /*
828 * Write to OUTPUT queue can be done immediately.
829 */
830 return POLLOUT | POLLWRNORM;
831 }
832 }
833
834 /*
835 * There is nothing to wait for if the queue isn't streaming, or if the
836 * error flag is set.
837 */
838 if (!vb2_is_streaming(q) || q->error)
839 return POLLERR;
840
841 /*
842 * For output streams you can call write() as long as there are fewer
843 * buffers queued than there are buffers available.
844 */
845 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
846 return POLLOUT | POLLWRNORM;
847
848 if (list_empty(&q->done_list)) {
849 /*
850 * If the last buffer was dequeued from a capture queue,
851 * return immediately. DQBUF will return -EPIPE.
852 */
853 if (q->last_buffer_dequeued)
854 return POLLIN | POLLRDNORM;
855
856 poll_wait(file, &q->done_wq, wait);
857 }
858
859 /*
860 * Take first buffer available for dequeuing.
861 */
862 spin_lock_irqsave(&q->done_lock, flags);
863 if (!list_empty(&q->done_list))
864 vb = list_first_entry(&q->done_list, struct vb2_buffer,
865 done_entry);
866 spin_unlock_irqrestore(&q->done_lock, flags);
867
868 if (vb && (vb->state == VB2_BUF_STATE_DONE
869 || vb->state == VB2_BUF_STATE_ERROR)) {
870 return (q->is_output) ?
871 POLLOUT | POLLWRNORM :
872 POLLIN | POLLRDNORM;
873 }
874 return 0;
875}
876
877/**
Junghak Sung3c5be982015-10-06 06:37:49 -0300878 * vb2_poll() - implements poll userspace operation
879 * @q: videobuf2 queue
880 * @file: file argument passed to the poll file operation handler
881 * @wait: wait argument passed to the poll file operation handler
882 *
883 * This function implements poll file operation handler for a driver.
884 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
885 * be informed that the file descriptor of a video device is available for
886 * reading.
887 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
888 * will be reported as available for writing.
889 *
890 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
891 * pending events.
892 *
893 * The return values from this function are intended to be directly returned
894 * from poll handler in driver.
895 */
896unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
897{
898 struct video_device *vfd = video_devdata(file);
899 unsigned long req_events = poll_requested_events(wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300900 unsigned int res = 0;
Junghak Sung3c5be982015-10-06 06:37:49 -0300901
902 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
903 struct v4l2_fh *fh = file->private_data;
904
905 if (v4l2_event_pending(fh))
906 res = POLLPRI;
907 else if (req_events & POLLPRI)
908 poll_wait(file, &fh->wait, wait);
909 }
910
Junghak Sung3c5be982015-10-06 06:37:49 -0300911 /*
912 * For compatibility with vb1: if QBUF hasn't been called yet, then
913 * return POLLERR as well. This only affects capture queues, output
914 * queues will always initialize waiting_for_buffers to false.
915 */
Junghak Sung49d8ab92015-11-03 08:16:39 -0200916 if (q->waiting_for_buffers && (req_events & (POLLIN | POLLRDNORM)))
917 return POLLERR;
Junghak Sung3c5be982015-10-06 06:37:49 -0300918
Junghak Sung49d8ab92015-11-03 08:16:39 -0200919 return res | vb2_core_poll(q, file, wait);
Junghak Sung3c5be982015-10-06 06:37:49 -0300920}
921EXPORT_SYMBOL_GPL(vb2_poll);
922
923/**
924 * struct vb2_fileio_buf - buffer context used by file io emulator
925 *
926 * vb2 provides a compatibility layer and emulator of file io (read and
927 * write) calls on top of streaming API. This structure is used for
928 * tracking context related to the buffers.
929 */
930struct vb2_fileio_buf {
931 void *vaddr;
932 unsigned int size;
933 unsigned int pos;
934 unsigned int queued:1;
935};
936
937/**
938 * struct vb2_fileio_data - queue context used by file io emulator
939 *
940 * @cur_index: the index of the buffer currently being read from or
941 * written to. If equal to q->num_buffers then a new buffer
942 * must be dequeued.
943 * @initial_index: in the read() case all buffers are queued up immediately
944 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
945 * buffers. However, in the write() case no buffers are initially
946 * queued, instead whenever a buffer is full it is queued up by
947 * __vb2_perform_fileio(). Only once all available buffers have
948 * been queued up will __vb2_perform_fileio() start to dequeue
949 * buffers. This means that initially __vb2_perform_fileio()
950 * needs to know what buffer index to use when it is queuing up
951 * the buffers for the first time. That initial index is stored
952 * in this field. Once it is equal to q->num_buffers all
953 * available buffers have been queued and __vb2_perform_fileio()
954 * should start the normal dequeue/queue cycle.
955 *
956 * vb2 provides a compatibility layer and emulator of file io (read and
957 * write) calls on top of streaming API. For proper operation it required
958 * this structure to save the driver state between each call of the read
959 * or write function.
960 */
961struct vb2_fileio_data {
962 struct v4l2_requestbuffers req;
963 struct v4l2_plane p;
964 struct v4l2_buffer b;
965 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
966 unsigned int cur_index;
967 unsigned int initial_index;
968 unsigned int q_count;
969 unsigned int dq_count;
970 unsigned read_once:1;
971 unsigned write_immediately:1;
972};
973
974/**
975 * __vb2_init_fileio() - initialize file io emulator
976 * @q: videobuf2 queue
977 * @read: mode selector (1 means read, 0 means write)
978 */
979static int __vb2_init_fileio(struct vb2_queue *q, int read)
980{
981 struct vb2_fileio_data *fileio;
982 int i, ret;
983 unsigned int count = 0;
984
985 /*
986 * Sanity check
987 */
988 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
989 (!read && !(q->io_modes & VB2_WRITE))))
990 return -EINVAL;
991
992 /*
993 * Check if device supports mapping buffers to kernel virtual space.
994 */
995 if (!q->mem_ops->vaddr)
996 return -EBUSY;
997
998 /*
999 * Check if streaming api has not been already activated.
1000 */
1001 if (q->streaming || q->num_buffers > 0)
1002 return -EBUSY;
1003
1004 /*
1005 * Start with count 1, driver can increase it in queue_setup()
1006 */
1007 count = 1;
1008
1009 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
1010 (read) ? "read" : "write", count, q->fileio_read_once,
1011 q->fileio_write_immediately);
1012
1013 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1014 if (fileio == NULL)
1015 return -ENOMEM;
1016
1017 fileio->read_once = q->fileio_read_once;
1018 fileio->write_immediately = q->fileio_write_immediately;
1019
1020 /*
1021 * Request buffers and use MMAP type to force driver
1022 * to allocate buffers by itself.
1023 */
1024 fileio->req.count = count;
1025 fileio->req.memory = VB2_MEMORY_MMAP;
1026 fileio->req.type = q->type;
1027 q->fileio = fileio;
1028 ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1029 if (ret)
1030 goto err_kfree;
1031
1032 /*
1033 * Check if plane_count is correct
1034 * (multiplane buffers are not supported).
1035 */
1036 if (q->bufs[0]->num_planes != 1) {
1037 ret = -EBUSY;
1038 goto err_reqbufs;
1039 }
1040
1041 /*
1042 * Get kernel address of each buffer.
1043 */
1044 for (i = 0; i < q->num_buffers; i++) {
1045 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1046 if (fileio->bufs[i].vaddr == NULL) {
1047 ret = -EINVAL;
1048 goto err_reqbufs;
1049 }
1050 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1051 }
1052
1053 /*
1054 * Read mode requires pre queuing of all buffers.
1055 */
1056 if (read) {
1057 bool is_multiplanar = q->is_multiplanar;
1058
1059 /*
1060 * Queue all buffers.
1061 */
1062 for (i = 0; i < q->num_buffers; i++) {
1063 struct v4l2_buffer *b = &fileio->b;
1064
1065 memset(b, 0, sizeof(*b));
1066 b->type = q->type;
1067 if (is_multiplanar) {
1068 memset(&fileio->p, 0, sizeof(fileio->p));
1069 b->m.planes = &fileio->p;
1070 b->length = 1;
1071 }
1072 b->memory = q->memory;
1073 b->index = i;
1074 ret = vb2_internal_qbuf(q, b);
1075 if (ret)
1076 goto err_reqbufs;
1077 fileio->bufs[i].queued = 1;
1078 }
1079 /*
1080 * All buffers have been queued, so mark that by setting
1081 * initial_index to q->num_buffers
1082 */
1083 fileio->initial_index = q->num_buffers;
1084 fileio->cur_index = q->num_buffers;
1085 }
1086
1087 /*
1088 * Start streaming.
1089 */
1090 ret = vb2_core_streamon(q, q->type);
1091 if (ret)
1092 goto err_reqbufs;
1093
1094 return ret;
1095
1096err_reqbufs:
1097 fileio->req.count = 0;
1098 vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1099
1100err_kfree:
1101 q->fileio = NULL;
1102 kfree(fileio);
1103 return ret;
1104}
1105
1106/**
1107 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1108 * @q: videobuf2 queue
1109 */
1110static int __vb2_cleanup_fileio(struct vb2_queue *q)
1111{
1112 struct vb2_fileio_data *fileio = q->fileio;
1113
1114 if (fileio) {
1115 vb2_core_streamoff(q, q->type);
1116 q->fileio = NULL;
1117 fileio->req.count = 0;
1118 vb2_reqbufs(q, &fileio->req);
1119 kfree(fileio);
1120 dprintk(3, "file io emulator closed\n");
1121 }
1122 return 0;
1123}
1124
1125/**
1126 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1127 * @q: videobuf2 queue
1128 * @data: pointed to target userspace buffer
1129 * @count: number of bytes to read or write
1130 * @ppos: file handle position tracking pointer
1131 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1132 * @read: access mode selector (1 means read, 0 means write)
1133 */
1134static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1135 loff_t *ppos, int nonblock, int read)
1136{
1137 struct vb2_fileio_data *fileio;
1138 struct vb2_fileio_buf *buf;
1139 bool is_multiplanar = q->is_multiplanar;
1140 /*
1141 * When using write() to write data to an output video node the vb2 core
Junghak Sung959c3ef2015-11-03 08:16:38 -02001142 * should copy timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
Junghak Sung3c5be982015-10-06 06:37:49 -03001143 * else is able to provide this information with the write() operation.
1144 */
Junghak Sung959c3ef2015-11-03 08:16:38 -02001145 bool copy_timestamp = !read && q->copy_timestamp;
Junghak Sung3c5be982015-10-06 06:37:49 -03001146 int ret, index;
1147
1148 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
1149 read ? "read" : "write", (long)*ppos, count,
1150 nonblock ? "non" : "");
1151
1152 if (!data)
1153 return -EINVAL;
1154
1155 /*
1156 * Initialize emulator on first call.
1157 */
1158 if (!vb2_fileio_is_active(q)) {
1159 ret = __vb2_init_fileio(q, read);
1160 dprintk(3, "vb2_init_fileio result: %d\n", ret);
1161 if (ret)
1162 return ret;
1163 }
1164 fileio = q->fileio;
1165
1166 /*
1167 * Check if we need to dequeue the buffer.
1168 */
1169 index = fileio->cur_index;
1170 if (index >= q->num_buffers) {
1171 /*
1172 * Call vb2_dqbuf to get buffer back.
1173 */
1174 memset(&fileio->b, 0, sizeof(fileio->b));
1175 fileio->b.type = q->type;
1176 fileio->b.memory = q->memory;
1177 if (is_multiplanar) {
1178 memset(&fileio->p, 0, sizeof(fileio->p));
1179 fileio->b.m.planes = &fileio->p;
1180 fileio->b.length = 1;
1181 }
1182 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
1183 dprintk(5, "vb2_dqbuf result: %d\n", ret);
1184 if (ret)
1185 return ret;
1186 fileio->dq_count += 1;
1187
1188 fileio->cur_index = index = fileio->b.index;
1189 buf = &fileio->bufs[index];
1190
1191 /*
1192 * Get number of bytes filled by the driver
1193 */
1194 buf->pos = 0;
1195 buf->queued = 0;
1196 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
1197 : vb2_plane_size(q->bufs[index], 0);
1198 /* Compensate for data_offset on read in the multiplanar case. */
1199 if (is_multiplanar && read &&
1200 fileio->b.m.planes[0].data_offset < buf->size) {
1201 buf->pos = fileio->b.m.planes[0].data_offset;
1202 buf->size -= buf->pos;
1203 }
1204 } else {
1205 buf = &fileio->bufs[index];
1206 }
1207
1208 /*
1209 * Limit count on last few bytes of the buffer.
1210 */
1211 if (buf->pos + count > buf->size) {
1212 count = buf->size - buf->pos;
1213 dprintk(5, "reducing read count: %zd\n", count);
1214 }
1215
1216 /*
1217 * Transfer data to userspace.
1218 */
1219 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
1220 count, index, buf->pos);
1221 if (read)
1222 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1223 else
1224 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1225 if (ret) {
1226 dprintk(3, "error copying data\n");
1227 return -EFAULT;
1228 }
1229
1230 /*
1231 * Update counters.
1232 */
1233 buf->pos += count;
1234 *ppos += count;
1235
1236 /*
1237 * Queue next buffer if required.
1238 */
1239 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
1240 /*
1241 * Check if this is the last buffer to read.
1242 */
1243 if (read && fileio->read_once && fileio->dq_count == 1) {
1244 dprintk(3, "read limit reached\n");
1245 return __vb2_cleanup_fileio(q);
1246 }
1247
1248 /*
1249 * Call vb2_qbuf and give buffer to the driver.
1250 */
1251 memset(&fileio->b, 0, sizeof(fileio->b));
1252 fileio->b.type = q->type;
1253 fileio->b.memory = q->memory;
1254 fileio->b.index = index;
1255 fileio->b.bytesused = buf->pos;
1256 if (is_multiplanar) {
1257 memset(&fileio->p, 0, sizeof(fileio->p));
1258 fileio->p.bytesused = buf->pos;
1259 fileio->b.m.planes = &fileio->p;
1260 fileio->b.length = 1;
1261 }
Junghak Sung959c3ef2015-11-03 08:16:38 -02001262 if (copy_timestamp)
Junghak Sung3c5be982015-10-06 06:37:49 -03001263 v4l2_get_timestamp(&fileio->b.timestamp);
1264 ret = vb2_internal_qbuf(q, &fileio->b);
1265 dprintk(5, "vb2_dbuf result: %d\n", ret);
1266 if (ret)
1267 return ret;
1268
1269 /*
1270 * Buffer has been queued, update the status
1271 */
1272 buf->pos = 0;
1273 buf->queued = 1;
1274 buf->size = vb2_plane_size(q->bufs[index], 0);
1275 fileio->q_count += 1;
1276 /*
1277 * If we are queuing up buffers for the first time, then
1278 * increase initial_index by one.
1279 */
1280 if (fileio->initial_index < q->num_buffers)
1281 fileio->initial_index++;
1282 /*
1283 * The next buffer to use is either a buffer that's going to be
1284 * queued for the first time (initial_index < q->num_buffers)
1285 * or it is equal to q->num_buffers, meaning that the next
1286 * time we need to dequeue a buffer since we've now queued up
1287 * all the 'first time' buffers.
1288 */
1289 fileio->cur_index = fileio->initial_index;
1290 }
1291
1292 /*
1293 * Return proper number of bytes processed.
1294 */
1295 if (ret == 0)
1296 ret = count;
1297 return ret;
1298}
1299
1300size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1301 loff_t *ppos, int nonblocking)
1302{
1303 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1304}
1305EXPORT_SYMBOL_GPL(vb2_read);
1306
1307size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1308 loff_t *ppos, int nonblocking)
1309{
1310 return __vb2_perform_fileio(q, (char __user *) data, count,
1311 ppos, nonblocking, 0);
1312}
1313EXPORT_SYMBOL_GPL(vb2_write);
1314
1315struct vb2_threadio_data {
1316 struct task_struct *thread;
1317 vb2_thread_fnc fnc;
1318 void *priv;
1319 bool stop;
1320};
1321
1322static int vb2_thread(void *data)
1323{
1324 struct vb2_queue *q = data;
1325 struct vb2_threadio_data *threadio = q->threadio;
1326 struct vb2_fileio_data *fileio = q->fileio;
Junghak Sung959c3ef2015-11-03 08:16:38 -02001327 bool copy_timestamp = false;
Junghak Sung3c5be982015-10-06 06:37:49 -03001328 int prequeue = 0;
1329 int index = 0;
1330 int ret = 0;
1331
1332 if (q->is_output) {
1333 prequeue = q->num_buffers;
Junghak Sung959c3ef2015-11-03 08:16:38 -02001334 copy_timestamp = q->copy_timestamp;
Junghak Sung3c5be982015-10-06 06:37:49 -03001335 }
1336
1337 set_freezable();
1338
1339 for (;;) {
1340 struct vb2_buffer *vb;
1341
1342 /*
1343 * Call vb2_dqbuf to get buffer back.
1344 */
1345 memset(&fileio->b, 0, sizeof(fileio->b));
1346 fileio->b.type = q->type;
1347 fileio->b.memory = q->memory;
1348 if (prequeue) {
1349 fileio->b.index = index++;
1350 prequeue--;
1351 } else {
1352 call_void_qop(q, wait_finish, q);
1353 if (!threadio->stop)
1354 ret = vb2_internal_dqbuf(q, &fileio->b, 0);
1355 call_void_qop(q, wait_prepare, q);
1356 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1357 }
1358 if (ret || threadio->stop)
1359 break;
1360 try_to_freeze();
1361
1362 vb = q->bufs[fileio->b.index];
1363 if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
1364 if (threadio->fnc(vb, threadio->priv))
1365 break;
1366 call_void_qop(q, wait_finish, q);
Junghak Sung959c3ef2015-11-03 08:16:38 -02001367 if (copy_timestamp)
Junghak Sung3c5be982015-10-06 06:37:49 -03001368 v4l2_get_timestamp(&fileio->b.timestamp);
1369 if (!threadio->stop)
1370 ret = vb2_internal_qbuf(q, &fileio->b);
1371 call_void_qop(q, wait_prepare, q);
1372 if (ret || threadio->stop)
1373 break;
1374 }
1375
1376 /* Hmm, linux becomes *very* unhappy without this ... */
1377 while (!kthread_should_stop()) {
1378 set_current_state(TASK_INTERRUPTIBLE);
1379 schedule();
1380 }
1381 return 0;
1382}
1383
1384/*
1385 * This function should not be used for anything else but the videobuf2-dvb
1386 * support. If you think you have another good use-case for this, then please
1387 * contact the linux-media mailinglist first.
1388 */
1389int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1390 const char *thread_name)
1391{
1392 struct vb2_threadio_data *threadio;
1393 int ret = 0;
1394
1395 if (q->threadio)
1396 return -EBUSY;
1397 if (vb2_is_busy(q))
1398 return -EBUSY;
1399 if (WARN_ON(q->fileio))
1400 return -EBUSY;
1401
1402 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
1403 if (threadio == NULL)
1404 return -ENOMEM;
1405 threadio->fnc = fnc;
1406 threadio->priv = priv;
1407
1408 ret = __vb2_init_fileio(q, !q->is_output);
1409 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1410 if (ret)
1411 goto nomem;
1412 q->threadio = threadio;
1413 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
1414 if (IS_ERR(threadio->thread)) {
1415 ret = PTR_ERR(threadio->thread);
1416 threadio->thread = NULL;
1417 goto nothread;
1418 }
1419 return 0;
1420
1421nothread:
1422 __vb2_cleanup_fileio(q);
1423nomem:
1424 kfree(threadio);
1425 return ret;
1426}
1427EXPORT_SYMBOL_GPL(vb2_thread_start);
1428
1429int vb2_thread_stop(struct vb2_queue *q)
1430{
1431 struct vb2_threadio_data *threadio = q->threadio;
1432 int err;
1433
1434 if (threadio == NULL)
1435 return 0;
1436 threadio->stop = true;
1437 /* Wake up all pending sleeps in the thread */
1438 vb2_queue_error(q);
1439 err = kthread_stop(threadio->thread);
1440 __vb2_cleanup_fileio(q);
1441 threadio->thread = NULL;
1442 kfree(threadio);
1443 q->threadio = NULL;
1444 return err;
1445}
1446EXPORT_SYMBOL_GPL(vb2_thread_stop);
1447
1448/*
1449 * The following functions are not part of the vb2 core API, but are helper
1450 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
1451 * and struct vb2_ops.
1452 * They contain boilerplate code that most if not all drivers have to do
1453 * and so they simplify the driver code.
1454 */
1455
1456/* The queue is busy if there is a owner and you are not that owner. */
1457static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
1458{
1459 return vdev->queue->owner && vdev->queue->owner != file->private_data;
1460}
1461
1462/* vb2 ioctl helpers */
1463
1464int vb2_ioctl_reqbufs(struct file *file, void *priv,
1465 struct v4l2_requestbuffers *p)
1466{
1467 struct video_device *vdev = video_devdata(file);
1468 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
1469
1470 if (res)
1471 return res;
1472 if (vb2_queue_is_busy(vdev, file))
1473 return -EBUSY;
1474 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
1475 /* If count == 0, then the owner has released all buffers and he
1476 is no longer owner of the queue. Otherwise we have a new owner. */
1477 if (res == 0)
1478 vdev->queue->owner = p->count ? file->private_data : NULL;
1479 return res;
1480}
1481EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1482
1483int vb2_ioctl_create_bufs(struct file *file, void *priv,
1484 struct v4l2_create_buffers *p)
1485{
1486 struct video_device *vdev = video_devdata(file);
1487 int res = vb2_verify_memory_type(vdev->queue, p->memory,
1488 p->format.type);
1489
1490 p->index = vdev->queue->num_buffers;
1491 /*
1492 * If count == 0, then just check if memory and type are valid.
1493 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1494 */
1495 if (p->count == 0)
1496 return res != -EBUSY ? res : 0;
1497 if (res)
1498 return res;
1499 if (vb2_queue_is_busy(vdev, file))
1500 return -EBUSY;
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001501
1502 res = vb2_create_bufs(vdev->queue, p);
Junghak Sung3c5be982015-10-06 06:37:49 -03001503 if (res == 0)
1504 vdev->queue->owner = file->private_data;
1505 return res;
1506}
1507EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1508
1509int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1510 struct v4l2_buffer *p)
1511{
1512 struct video_device *vdev = video_devdata(file);
1513
1514 if (vb2_queue_is_busy(vdev, file))
1515 return -EBUSY;
1516 return vb2_prepare_buf(vdev->queue, p);
1517}
1518EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1519
1520int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1521{
1522 struct video_device *vdev = video_devdata(file);
1523
1524 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1525 return vb2_querybuf(vdev->queue, p);
1526}
1527EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1528
1529int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1530{
1531 struct video_device *vdev = video_devdata(file);
1532
1533 if (vb2_queue_is_busy(vdev, file))
1534 return -EBUSY;
1535 return vb2_qbuf(vdev->queue, p);
1536}
1537EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1538
1539int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1540{
1541 struct video_device *vdev = video_devdata(file);
1542
1543 if (vb2_queue_is_busy(vdev, file))
1544 return -EBUSY;
1545 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1546}
1547EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1548
1549int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1550{
1551 struct video_device *vdev = video_devdata(file);
1552
1553 if (vb2_queue_is_busy(vdev, file))
1554 return -EBUSY;
1555 return vb2_streamon(vdev->queue, i);
1556}
1557EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1558
1559int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1560{
1561 struct video_device *vdev = video_devdata(file);
1562
1563 if (vb2_queue_is_busy(vdev, file))
1564 return -EBUSY;
1565 return vb2_streamoff(vdev->queue, i);
1566}
1567EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1568
1569int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1570{
1571 struct video_device *vdev = video_devdata(file);
1572
1573 if (vb2_queue_is_busy(vdev, file))
1574 return -EBUSY;
1575 return vb2_expbuf(vdev->queue, p);
1576}
1577EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1578
1579/* v4l2_file_operations helpers */
1580
1581int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1582{
1583 struct video_device *vdev = video_devdata(file);
1584
1585 return vb2_mmap(vdev->queue, vma);
1586}
1587EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1588
1589int _vb2_fop_release(struct file *file, struct mutex *lock)
1590{
1591 struct video_device *vdev = video_devdata(file);
1592
1593 if (lock)
1594 mutex_lock(lock);
1595 if (file->private_data == vdev->queue->owner) {
1596 vb2_queue_release(vdev->queue);
1597 vdev->queue->owner = NULL;
1598 }
1599 if (lock)
1600 mutex_unlock(lock);
1601 return v4l2_fh_release(file);
1602}
1603EXPORT_SYMBOL_GPL(_vb2_fop_release);
1604
1605int vb2_fop_release(struct file *file)
1606{
1607 struct video_device *vdev = video_devdata(file);
1608 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1609
1610 return _vb2_fop_release(file, lock);
1611}
1612EXPORT_SYMBOL_GPL(vb2_fop_release);
1613
1614ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1615 size_t count, loff_t *ppos)
1616{
1617 struct video_device *vdev = video_devdata(file);
1618 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1619 int err = -EBUSY;
1620
1621 if (!(vdev->queue->io_modes & VB2_WRITE))
1622 return -EINVAL;
1623 if (lock && mutex_lock_interruptible(lock))
1624 return -ERESTARTSYS;
1625 if (vb2_queue_is_busy(vdev, file))
1626 goto exit;
1627 err = vb2_write(vdev->queue, buf, count, ppos,
1628 file->f_flags & O_NONBLOCK);
1629 if (vdev->queue->fileio)
1630 vdev->queue->owner = file->private_data;
1631exit:
1632 if (lock)
1633 mutex_unlock(lock);
1634 return err;
1635}
1636EXPORT_SYMBOL_GPL(vb2_fop_write);
1637
1638ssize_t vb2_fop_read(struct file *file, char __user *buf,
1639 size_t count, loff_t *ppos)
1640{
1641 struct video_device *vdev = video_devdata(file);
1642 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1643 int err = -EBUSY;
1644
1645 if (!(vdev->queue->io_modes & VB2_READ))
1646 return -EINVAL;
1647 if (lock && mutex_lock_interruptible(lock))
1648 return -ERESTARTSYS;
1649 if (vb2_queue_is_busy(vdev, file))
1650 goto exit;
1651 err = vb2_read(vdev->queue, buf, count, ppos,
1652 file->f_flags & O_NONBLOCK);
1653 if (vdev->queue->fileio)
1654 vdev->queue->owner = file->private_data;
1655exit:
1656 if (lock)
1657 mutex_unlock(lock);
1658 return err;
1659}
1660EXPORT_SYMBOL_GPL(vb2_fop_read);
1661
1662unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
1663{
1664 struct video_device *vdev = video_devdata(file);
1665 struct vb2_queue *q = vdev->queue;
1666 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1667 unsigned res;
1668 void *fileio;
1669
1670 /*
1671 * If this helper doesn't know how to lock, then you shouldn't be using
1672 * it but you should write your own.
1673 */
1674 WARN_ON(!lock);
1675
1676 if (lock && mutex_lock_interruptible(lock))
1677 return POLLERR;
1678
1679 fileio = q->fileio;
1680
1681 res = vb2_poll(vdev->queue, file, wait);
1682
1683 /* If fileio was started, then we have a new queue owner. */
1684 if (!fileio && q->fileio)
1685 q->owner = file->private_data;
1686 if (lock)
1687 mutex_unlock(lock);
1688 return res;
1689}
1690EXPORT_SYMBOL_GPL(vb2_fop_poll);
1691
1692#ifndef CONFIG_MMU
1693unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1694 unsigned long len, unsigned long pgoff, unsigned long flags)
1695{
1696 struct video_device *vdev = video_devdata(file);
1697
1698 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1699}
1700EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1701#endif
1702
1703/* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1704
1705void vb2_ops_wait_prepare(struct vb2_queue *vq)
1706{
1707 mutex_unlock(vq->lock);
1708}
1709EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1710
1711void vb2_ops_wait_finish(struct vb2_queue *vq)
1712{
1713 mutex_lock(vq->lock);
1714}
1715EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1716
Junghak Sungc1399902015-09-22 10:30:29 -03001717MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1718MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1719MODULE_LICENSE("GPL");