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