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