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