1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/err.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mm.h>
18#include <linux/poll.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21
22#include <media/videobuf2-core.h>
23
24static int debug;
25module_param(debug, int, 0644);
26
27#define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2: " fmt, ## arg); \
31 } while (0)
32
33#define call_memop(q, plane, op, args...) \
34 (((q)->mem_ops->op) ? \
35 ((q)->mem_ops->op(args)) : 0)
36
37#define call_qop(q, op, args...) \
38 (((q)->ops->op) ? ((q)->ops->op(args)) : 0)
39
40#define V4L2_BUFFER_STATE_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
41 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR)
42
43
44
45
46static int __vb2_buf_mem_alloc(struct vb2_buffer *vb,
47 unsigned long *plane_sizes)
48{
49 struct vb2_queue *q = vb->vb2_queue;
50 void *mem_priv;
51 int plane;
52
53
54 for (plane = 0; plane < vb->num_planes; ++plane) {
55 mem_priv = call_memop(q, plane, alloc, q->alloc_ctx[plane],
56 plane_sizes[plane]);
57 if (IS_ERR_OR_NULL(mem_priv))
58 goto free;
59
60
61 vb->planes[plane].mem_priv = mem_priv;
62 vb->v4l2_planes[plane].length = plane_sizes[plane];
63 }
64
65 return 0;
66free:
67
68 for (; plane > 0; --plane)
69 call_memop(q, plane, put, vb->planes[plane - 1].mem_priv);
70
71 return -ENOMEM;
72}
73
74
75
76
77static void __vb2_buf_mem_free(struct vb2_buffer *vb)
78{
79 struct vb2_queue *q = vb->vb2_queue;
80 unsigned int plane;
81
82 for (plane = 0; plane < vb->num_planes; ++plane) {
83 call_memop(q, plane, put, vb->planes[plane].mem_priv);
84 vb->planes[plane].mem_priv = NULL;
85 dprintk(3, "Freed plane %d of buffer %d\n",
86 plane, vb->v4l2_buf.index);
87 }
88}
89
90
91
92
93
94static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
95{
96 struct vb2_queue *q = vb->vb2_queue;
97 unsigned int plane;
98
99 for (plane = 0; plane < vb->num_planes; ++plane) {
100 void *mem_priv = vb->planes[plane].mem_priv;
101
102 if (mem_priv) {
103 call_memop(q, plane, put_userptr, mem_priv);
104 vb->planes[plane].mem_priv = NULL;
105 }
106 }
107}
108
109
110
111
112
113static void __setup_offsets(struct vb2_queue *q)
114{
115 unsigned int buffer, plane;
116 struct vb2_buffer *vb;
117 unsigned long off = 0;
118
119 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
120 vb = q->bufs[buffer];
121 if (!vb)
122 continue;
123
124 for (plane = 0; plane < vb->num_planes; ++plane) {
125 vb->v4l2_planes[plane].m.mem_offset = off;
126
127 dprintk(3, "Buffer %d, plane %d offset 0x%08lx\n",
128 buffer, plane, off);
129
130 off += vb->v4l2_planes[plane].length;
131 off = PAGE_ALIGN(off);
132 }
133 }
134}
135
136
137
138
139
140
141
142
143static int __vb2_queue_alloc(struct vb2_queue *q, enum v4l2_memory memory,
144 unsigned int num_buffers, unsigned int num_planes,
145 unsigned long plane_sizes[])
146{
147 unsigned int buffer;
148 struct vb2_buffer *vb;
149 int ret;
150
151 for (buffer = 0; buffer < num_buffers; ++buffer) {
152
153 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
154 if (!vb) {
155 dprintk(1, "Memory alloc for buffer struct failed\n");
156 break;
157 }
158
159
160 if (V4L2_TYPE_IS_MULTIPLANAR(q->type))
161 vb->v4l2_buf.length = num_planes;
162
163 vb->state = VB2_BUF_STATE_DEQUEUED;
164 vb->vb2_queue = q;
165 vb->num_planes = num_planes;
166 vb->v4l2_buf.index = buffer;
167 vb->v4l2_buf.type = q->type;
168 vb->v4l2_buf.memory = memory;
169
170
171 if (memory == V4L2_MEMORY_MMAP) {
172 ret = __vb2_buf_mem_alloc(vb, plane_sizes);
173 if (ret) {
174 dprintk(1, "Failed allocating memory for "
175 "buffer %d\n", buffer);
176 kfree(vb);
177 break;
178 }
179
180
181
182
183
184 ret = call_qop(q, buf_init, vb);
185 if (ret) {
186 dprintk(1, "Buffer %d %p initialization"
187 " failed\n", buffer, vb);
188 __vb2_buf_mem_free(vb);
189 kfree(vb);
190 break;
191 }
192 }
193
194 q->bufs[buffer] = vb;
195 }
196
197 q->num_buffers = buffer;
198
199 __setup_offsets(q);
200
201 dprintk(1, "Allocated %d buffers, %d plane(s) each\n",
202 q->num_buffers, num_planes);
203
204 return buffer;
205}
206
207
208
209
210static void __vb2_free_mem(struct vb2_queue *q)
211{
212 unsigned int buffer;
213 struct vb2_buffer *vb;
214
215 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
216 vb = q->bufs[buffer];
217 if (!vb)
218 continue;
219
220
221 if (q->memory == V4L2_MEMORY_MMAP)
222 __vb2_buf_mem_free(vb);
223 else
224 __vb2_buf_userptr_put(vb);
225 }
226}
227
228
229
230
231
232
233static void __vb2_queue_free(struct vb2_queue *q)
234{
235 unsigned int buffer;
236
237
238 if (q->ops->buf_cleanup) {
239 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
240 if (NULL == q->bufs[buffer])
241 continue;
242 q->ops->buf_cleanup(q->bufs[buffer]);
243 }
244 }
245
246
247 __vb2_free_mem(q);
248
249
250 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
251 kfree(q->bufs[buffer]);
252 q->bufs[buffer] = NULL;
253 }
254
255 q->num_buffers = 0;
256 q->memory = 0;
257}
258
259
260
261
262
263static int __verify_planes_array(struct vb2_buffer *vb, struct v4l2_buffer *b)
264{
265
266 if (NULL == b->m.planes) {
267 dprintk(1, "Multi-planar buffer passed but "
268 "planes array not provided\n");
269 return -EINVAL;
270 }
271
272 if (b->length < vb->num_planes || b->length > VIDEO_MAX_PLANES) {
273 dprintk(1, "Incorrect planes array length, "
274 "expected %d, got %d\n", vb->num_planes, b->length);
275 return -EINVAL;
276 }
277
278 return 0;
279}
280
281
282
283
284
285static int __fill_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
286{
287 struct vb2_queue *q = vb->vb2_queue;
288 int ret = 0;
289
290
291 memcpy(b, &vb->v4l2_buf, offsetof(struct v4l2_buffer, m));
292 b->input = vb->v4l2_buf.input;
293 b->reserved = vb->v4l2_buf.reserved;
294
295 if (V4L2_TYPE_IS_MULTIPLANAR(q->type)) {
296 ret = __verify_planes_array(vb, b);
297 if (ret)
298 return ret;
299
300
301
302
303
304 memcpy(b->m.planes, vb->v4l2_planes,
305 b->length * sizeof(struct v4l2_plane));
306 } else {
307
308
309
310
311 b->length = vb->v4l2_planes[0].length;
312 b->bytesused = vb->v4l2_planes[0].bytesused;
313 if (q->memory == V4L2_MEMORY_MMAP)
314 b->m.offset = vb->v4l2_planes[0].m.mem_offset;
315 else if (q->memory == V4L2_MEMORY_USERPTR)
316 b->m.userptr = vb->v4l2_planes[0].m.userptr;
317 }
318
319
320
321
322 b->flags &= ~V4L2_BUFFER_STATE_FLAGS;
323
324 switch (vb->state) {
325 case VB2_BUF_STATE_QUEUED:
326 case VB2_BUF_STATE_ACTIVE:
327 b->flags |= V4L2_BUF_FLAG_QUEUED;
328 break;
329 case VB2_BUF_STATE_ERROR:
330 b->flags |= V4L2_BUF_FLAG_ERROR;
331
332 case VB2_BUF_STATE_DONE:
333 b->flags |= V4L2_BUF_FLAG_DONE;
334 break;
335 case VB2_BUF_STATE_DEQUEUED:
336
337 break;
338 }
339
340 if (vb->num_planes_mapped == vb->num_planes)
341 b->flags |= V4L2_BUF_FLAG_MAPPED;
342
343 return ret;
344}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
360{
361 struct vb2_buffer *vb;
362
363 if (b->type != q->type) {
364 dprintk(1, "querybuf: wrong buffer type\n");
365 return -EINVAL;
366 }
367
368 if (b->index >= q->num_buffers) {
369 dprintk(1, "querybuf: buffer index out of range\n");
370 return -EINVAL;
371 }
372 vb = q->bufs[b->index];
373
374 return __fill_v4l2_buffer(vb, b);
375}
376EXPORT_SYMBOL(vb2_querybuf);
377
378
379
380
381
382static int __verify_userptr_ops(struct vb2_queue *q)
383{
384 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
385 !q->mem_ops->put_userptr)
386 return -EINVAL;
387
388 return 0;
389}
390
391
392
393
394
395static int __verify_mmap_ops(struct vb2_queue *q)
396{
397 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
398 !q->mem_ops->put || !q->mem_ops->mmap)
399 return -EINVAL;
400
401 return 0;
402}
403
404
405
406
407
408static bool __buffers_in_use(struct vb2_queue *q)
409{
410 unsigned int buffer, plane;
411 struct vb2_buffer *vb;
412
413 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
414 vb = q->bufs[buffer];
415 for (plane = 0; plane < vb->num_planes; ++plane) {
416
417
418
419
420
421
422 if (call_memop(q, plane, num_users,
423 vb->planes[plane].mem_priv) > 1)
424 return true;
425 }
426 }
427
428 return false;
429}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
455{
456 unsigned int num_buffers, num_planes;
457 unsigned long plane_sizes[VIDEO_MAX_PLANES];
458 int ret = 0;
459
460 if (q->fileio) {
461 dprintk(1, "reqbufs: file io in progress\n");
462 return -EBUSY;
463 }
464
465 if (req->memory != V4L2_MEMORY_MMAP
466 && req->memory != V4L2_MEMORY_USERPTR) {
467 dprintk(1, "reqbufs: unsupported memory type\n");
468 return -EINVAL;
469 }
470
471 if (req->type != q->type) {
472 dprintk(1, "reqbufs: requested type is incorrect\n");
473 return -EINVAL;
474 }
475
476 if (q->streaming) {
477 dprintk(1, "reqbufs: streaming active\n");
478 return -EBUSY;
479 }
480
481
482
483
484
485 if (req->memory == V4L2_MEMORY_MMAP && __verify_mmap_ops(q)) {
486 dprintk(1, "reqbufs: MMAP for current setup unsupported\n");
487 return -EINVAL;
488 }
489
490 if (req->memory == V4L2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
491 dprintk(1, "reqbufs: USERPTR for current setup unsupported\n");
492 return -EINVAL;
493 }
494
495 if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) {
496
497
498
499
500 if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) {
501 dprintk(1, "reqbufs: memory in use, cannot free\n");
502 return -EBUSY;
503 }
504
505 __vb2_queue_free(q);
506
507
508
509
510
511 if (req->count == 0)
512 return 0;
513 }
514
515
516
517
518 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
519 memset(plane_sizes, 0, sizeof(plane_sizes));
520 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
521 q->memory = req->memory;
522
523
524
525
526
527 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
528 plane_sizes, q->alloc_ctx);
529 if (ret)
530 return ret;
531
532
533 ret = __vb2_queue_alloc(q, req->memory, num_buffers, num_planes,
534 plane_sizes);
535 if (ret == 0) {
536 dprintk(1, "Memory allocation failed\n");
537 return -ENOMEM;
538 }
539
540
541
542
543 if (ret < num_buffers) {
544 unsigned int orig_num_buffers;
545
546 orig_num_buffers = num_buffers = ret;
547 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
548 plane_sizes, q->alloc_ctx);
549 if (ret)
550 goto free_mem;
551
552 if (orig_num_buffers < num_buffers) {
553 ret = -ENOMEM;
554 goto free_mem;
555 }
556
557
558
559
560 ret = num_buffers;
561 }
562
563
564
565
566
567 req->count = ret;
568
569 return 0;
570
571free_mem:
572 __vb2_queue_free(q);
573 return ret;
574}
575EXPORT_SYMBOL_GPL(vb2_reqbufs);
576
577
578
579
580
581
582
583
584
585void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
586{
587 struct vb2_queue *q = vb->vb2_queue;
588
589 if (plane_no > vb->num_planes)
590 return NULL;
591
592 return call_memop(q, plane_no, vaddr, vb->planes[plane_no].mem_priv);
593
594}
595EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
596
597
598
599
600
601
602
603
604
605
606
607
608void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
609{
610 struct vb2_queue *q = vb->vb2_queue;
611
612 if (plane_no > vb->num_planes)
613 return NULL;
614
615 return call_memop(q, plane_no, cookie, vb->planes[plane_no].mem_priv);
616}
617EXPORT_SYMBOL_GPL(vb2_plane_cookie);
618
619
620
621
622
623
624
625
626
627
628
629
630
631void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
632{
633 struct vb2_queue *q = vb->vb2_queue;
634 unsigned long flags;
635
636 if (vb->state != VB2_BUF_STATE_ACTIVE)
637 return;
638
639 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
640 return;
641
642 dprintk(4, "Done processing on buffer %d, state: %d\n",
643 vb->v4l2_buf.index, vb->state);
644
645
646 spin_lock_irqsave(&q->done_lock, flags);
647 vb->state = state;
648 list_add_tail(&vb->done_entry, &q->done_list);
649 atomic_dec(&q->queued_count);
650 spin_unlock_irqrestore(&q->done_lock, flags);
651
652
653 wake_up(&q->done_wq);
654}
655EXPORT_SYMBOL_GPL(vb2_buffer_done);
656
657
658
659
660
661static int __fill_vb2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b,
662 struct v4l2_plane *v4l2_planes)
663{
664 unsigned int plane;
665 int ret;
666
667 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
668
669
670
671
672 ret = __verify_planes_array(vb, b);
673 if (ret)
674 return ret;
675
676
677 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
678
679
680
681
682 for (plane = 0; plane < vb->num_planes; ++plane) {
683 v4l2_planes[plane].bytesused =
684 b->m.planes[plane].bytesused;
685 v4l2_planes[plane].data_offset =
686 b->m.planes[plane].data_offset;
687 }
688 }
689
690 if (b->memory == V4L2_MEMORY_USERPTR) {
691 for (plane = 0; plane < vb->num_planes; ++plane) {
692 v4l2_planes[plane].m.userptr =
693 b->m.planes[plane].m.userptr;
694 v4l2_planes[plane].length =
695 b->m.planes[plane].length;
696 }
697 }
698 } else {
699
700
701
702
703
704
705 if (V4L2_TYPE_IS_OUTPUT(b->type))
706 v4l2_planes[0].bytesused = b->bytesused;
707
708 if (b->memory == V4L2_MEMORY_USERPTR) {
709 v4l2_planes[0].m.userptr = b->m.userptr;
710 v4l2_planes[0].length = b->length;
711 }
712 }
713
714 vb->v4l2_buf.field = b->field;
715 vb->v4l2_buf.timestamp = b->timestamp;
716 vb->v4l2_buf.input = b->input;
717 vb->v4l2_buf.flags = b->flags & ~V4L2_BUFFER_STATE_FLAGS;
718
719 return 0;
720}
721
722
723
724
725static int __qbuf_userptr(struct vb2_buffer *vb, struct v4l2_buffer *b)
726{
727 struct v4l2_plane planes[VIDEO_MAX_PLANES];
728 struct vb2_queue *q = vb->vb2_queue;
729 void *mem_priv;
730 unsigned int plane;
731 int ret;
732 int write = !V4L2_TYPE_IS_OUTPUT(q->type);
733
734
735 ret = __fill_vb2_buffer(vb, b, planes);
736 if (ret)
737 return ret;
738
739 for (plane = 0; plane < vb->num_planes; ++plane) {
740
741 if (vb->v4l2_planes[plane].m.userptr == planes[plane].m.userptr
742 && vb->v4l2_planes[plane].length == planes[plane].length)
743 continue;
744
745 dprintk(3, "qbuf: userspace address for plane %d changed, "
746 "reacquiring memory\n", plane);
747
748
749 if (vb->planes[plane].mem_priv)
750 call_memop(q, plane, put_userptr,
751 vb->planes[plane].mem_priv);
752
753 vb->planes[plane].mem_priv = NULL;
754
755
756 if (q->mem_ops->get_userptr) {
757 mem_priv = q->mem_ops->get_userptr(q->alloc_ctx[plane],
758 planes[plane].m.userptr,
759 planes[plane].length,
760 write);
761 if (IS_ERR(mem_priv)) {
762 dprintk(1, "qbuf: failed acquiring userspace "
763 "memory for plane %d\n", plane);
764 ret = PTR_ERR(mem_priv);
765 goto err;
766 }
767 vb->planes[plane].mem_priv = mem_priv;
768 }
769 }
770
771
772
773
774
775 ret = call_qop(q, buf_init, vb);
776 if (ret) {
777 dprintk(1, "qbuf: buffer initialization failed\n");
778 goto err;
779 }
780
781
782
783
784
785 for (plane = 0; plane < vb->num_planes; ++plane)
786 vb->v4l2_planes[plane] = planes[plane];
787
788 return 0;
789err:
790
791 for (; plane > 0; --plane) {
792 call_memop(q, plane, put_userptr,
793 vb->planes[plane - 1].mem_priv);
794 vb->planes[plane - 1].mem_priv = NULL;
795 }
796
797 return ret;
798}
799
800
801
802
803static int __qbuf_mmap(struct vb2_buffer *vb, struct v4l2_buffer *b)
804{
805 return __fill_vb2_buffer(vb, b, vb->v4l2_planes);
806}
807
808
809
810
811static void __enqueue_in_driver(struct vb2_buffer *vb)
812{
813 struct vb2_queue *q = vb->vb2_queue;
814
815 vb->state = VB2_BUF_STATE_ACTIVE;
816 atomic_inc(&q->queued_count);
817 q->ops->buf_queue(vb);
818}
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
838{
839 struct vb2_buffer *vb;
840 int ret = 0;
841
842 if (q->fileio) {
843 dprintk(1, "qbuf: file io in progress\n");
844 return -EBUSY;
845 }
846
847 if (b->type != q->type) {
848 dprintk(1, "qbuf: invalid buffer type\n");
849 return -EINVAL;
850 }
851
852 if (b->index >= q->num_buffers) {
853 dprintk(1, "qbuf: buffer index out of range\n");
854 return -EINVAL;
855 }
856
857 vb = q->bufs[b->index];
858 if (NULL == vb) {
859
860 dprintk(1, "qbuf: buffer is NULL\n");
861 return -EINVAL;
862 }
863
864 if (b->memory != q->memory) {
865 dprintk(1, "qbuf: invalid memory type\n");
866 return -EINVAL;
867 }
868
869 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
870 dprintk(1, "qbuf: buffer already in use\n");
871 return -EINVAL;
872 }
873
874 if (q->memory == V4L2_MEMORY_MMAP)
875 ret = __qbuf_mmap(vb, b);
876 else if (q->memory == V4L2_MEMORY_USERPTR)
877 ret = __qbuf_userptr(vb, b);
878 else {
879 WARN(1, "Invalid queue type\n");
880 return -EINVAL;
881 }
882
883 if (ret)
884 return ret;
885
886 ret = call_qop(q, buf_prepare, vb);
887 if (ret) {
888 dprintk(1, "qbuf: buffer preparation failed\n");
889 return ret;
890 }
891
892
893
894
895
896 list_add_tail(&vb->queued_entry, &q->queued_list);
897 vb->state = VB2_BUF_STATE_QUEUED;
898
899
900
901
902
903 if (q->streaming)
904 __enqueue_in_driver(vb);
905
906 dprintk(1, "qbuf of buffer %d succeeded\n", vb->v4l2_buf.index);
907 return 0;
908}
909EXPORT_SYMBOL_GPL(vb2_qbuf);
910
911
912
913
914
915
916
917static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
918{
919
920
921
922
923
924
925
926
927
928 for (;;) {
929 int ret;
930
931 if (!q->streaming) {
932 dprintk(1, "Streaming off, will not wait for buffers\n");
933 return -EINVAL;
934 }
935
936 if (!list_empty(&q->done_list)) {
937
938
939
940 break;
941 }
942
943 if (nonblocking) {
944 dprintk(1, "Nonblocking and no buffers to dequeue, "
945 "will not wait\n");
946 return -EAGAIN;
947 }
948
949
950
951
952
953
954 call_qop(q, wait_prepare, q);
955
956
957
958
959 dprintk(3, "Will sleep waiting for buffers\n");
960 ret = wait_event_interruptible(q->done_wq,
961 !list_empty(&q->done_list) || !q->streaming);
962
963
964
965
966
967 call_qop(q, wait_finish, q);
968 if (ret)
969 return ret;
970 }
971 return 0;
972}
973
974
975
976
977
978
979static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
980 int nonblocking)
981{
982 unsigned long flags;
983 int ret;
984
985
986
987
988 ret = __vb2_wait_for_done_vb(q, nonblocking);
989 if (ret)
990 return ret;
991
992
993
994
995
996 spin_lock_irqsave(&q->done_lock, flags);
997 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
998 list_del(&(*vb)->done_entry);
999 spin_unlock_irqrestore(&q->done_lock, flags);
1000
1001 return 0;
1002}
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013int vb2_wait_for_all_buffers(struct vb2_queue *q)
1014{
1015 if (!q->streaming) {
1016 dprintk(1, "Streaming off, will not wait for buffers\n");
1017 return -EINVAL;
1018 }
1019
1020 wait_event(q->done_wq, !atomic_read(&q->queued_count));
1021 return 0;
1022}
1023EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
1047{
1048 struct vb2_buffer *vb = NULL;
1049 int ret;
1050
1051 if (q->fileio) {
1052 dprintk(1, "dqbuf: file io in progress\n");
1053 return -EBUSY;
1054 }
1055
1056 if (b->type != q->type) {
1057 dprintk(1, "dqbuf: invalid buffer type\n");
1058 return -EINVAL;
1059 }
1060
1061 ret = __vb2_get_done_vb(q, &vb, nonblocking);
1062 if (ret < 0) {
1063 dprintk(1, "dqbuf: error getting next done buffer\n");
1064 return ret;
1065 }
1066
1067 ret = call_qop(q, buf_finish, vb);
1068 if (ret) {
1069 dprintk(1, "dqbuf: buffer finish failed\n");
1070 return ret;
1071 }
1072
1073 switch (vb->state) {
1074 case VB2_BUF_STATE_DONE:
1075 dprintk(3, "dqbuf: Returning done buffer\n");
1076 break;
1077 case VB2_BUF_STATE_ERROR:
1078 dprintk(3, "dqbuf: Returning done buffer with errors\n");
1079 break;
1080 default:
1081 dprintk(1, "dqbuf: Invalid buffer state\n");
1082 return -EINVAL;
1083 }
1084
1085
1086 __fill_v4l2_buffer(vb, b);
1087
1088 list_del(&vb->queued_entry);
1089
1090 dprintk(1, "dqbuf of buffer %d, with state %d\n",
1091 vb->v4l2_buf.index, vb->state);
1092
1093 vb->state = VB2_BUF_STATE_DEQUEUED;
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(vb2_dqbuf);
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1112{
1113 struct vb2_buffer *vb;
1114 int ret;
1115
1116 if (q->fileio) {
1117 dprintk(1, "streamon: file io in progress\n");
1118 return -EBUSY;
1119 }
1120
1121 if (type != q->type) {
1122 dprintk(1, "streamon: invalid stream type\n");
1123 return -EINVAL;
1124 }
1125
1126 if (q->streaming) {
1127 dprintk(1, "streamon: already streaming\n");
1128 return -EBUSY;
1129 }
1130
1131
1132
1133
1134
1135 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1136 if (list_empty(&q->queued_list)) {
1137 dprintk(1, "streamon: no output buffers queued\n");
1138 return -EINVAL;
1139 }
1140 }
1141
1142
1143
1144
1145 ret = call_qop(q, start_streaming, q);
1146 if (ret) {
1147 dprintk(1, "streamon: driver refused to start streaming\n");
1148 return ret;
1149 }
1150
1151 q->streaming = 1;
1152
1153
1154
1155
1156
1157 list_for_each_entry(vb, &q->queued_list, queued_entry)
1158 __enqueue_in_driver(vb);
1159
1160 dprintk(3, "Streamon successful\n");
1161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(vb2_streamon);
1164
1165
1166
1167
1168
1169
1170
1171static void __vb2_queue_cancel(struct vb2_queue *q)
1172{
1173 unsigned int i;
1174
1175
1176
1177
1178
1179 if (q->streaming)
1180 call_qop(q, stop_streaming, q);
1181 q->streaming = 0;
1182
1183
1184
1185
1186 INIT_LIST_HEAD(&q->queued_list);
1187
1188
1189
1190
1191 INIT_LIST_HEAD(&q->done_list);
1192 atomic_set(&q->queued_count, 0);
1193 wake_up_all(&q->done_wq);
1194
1195
1196
1197
1198 for (i = 0; i < q->num_buffers; ++i)
1199 q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
1200}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
1218{
1219 if (q->fileio) {
1220 dprintk(1, "streamoff: file io in progress\n");
1221 return -EBUSY;
1222 }
1223
1224 if (type != q->type) {
1225 dprintk(1, "streamoff: invalid stream type\n");
1226 return -EINVAL;
1227 }
1228
1229 if (!q->streaming) {
1230 dprintk(1, "streamoff: not streaming\n");
1231 return -EINVAL;
1232 }
1233
1234
1235
1236
1237
1238 __vb2_queue_cancel(q);
1239
1240 dprintk(3, "Streamoff successful\n");
1241 return 0;
1242}
1243EXPORT_SYMBOL_GPL(vb2_streamoff);
1244
1245
1246
1247
1248static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
1249 unsigned int *_buffer, unsigned int *_plane)
1250{
1251 struct vb2_buffer *vb;
1252 unsigned int buffer, plane;
1253
1254
1255
1256
1257
1258
1259 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
1260 vb = q->bufs[buffer];
1261
1262 for (plane = 0; plane < vb->num_planes; ++plane) {
1263 if (vb->v4l2_planes[plane].m.mem_offset == off) {
1264 *_buffer = buffer;
1265 *_plane = plane;
1266 return 0;
1267 }
1268 }
1269 }
1270
1271 return -EINVAL;
1272}
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
1294{
1295 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
1296 struct vb2_plane *vb_plane;
1297 struct vb2_buffer *vb;
1298 unsigned int buffer, plane;
1299 int ret;
1300
1301 if (q->memory != V4L2_MEMORY_MMAP) {
1302 dprintk(1, "Queue is not currently set up for mmap\n");
1303 return -EINVAL;
1304 }
1305
1306
1307
1308
1309 if (!(vma->vm_flags & VM_SHARED)) {
1310 dprintk(1, "Invalid vma flags, VM_SHARED needed\n");
1311 return -EINVAL;
1312 }
1313 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1314 if (!(vma->vm_flags & VM_WRITE)) {
1315 dprintk(1, "Invalid vma flags, VM_WRITE needed\n");
1316 return -EINVAL;
1317 }
1318 } else {
1319 if (!(vma->vm_flags & VM_READ)) {
1320 dprintk(1, "Invalid vma flags, VM_READ needed\n");
1321 return -EINVAL;
1322 }
1323 }
1324
1325
1326
1327
1328 ret = __find_plane_by_offset(q, off, &buffer, &plane);
1329 if (ret)
1330 return ret;
1331
1332 vb = q->bufs[buffer];
1333 vb_plane = &vb->planes[plane];
1334
1335 ret = q->mem_ops->mmap(vb_plane->mem_priv, vma);
1336 if (ret)
1337 return ret;
1338
1339 vb_plane->mapped = 1;
1340 vb->num_planes_mapped++;
1341
1342 dprintk(3, "Buffer %d, plane %d successfully mapped\n", buffer, plane);
1343 return 0;
1344}
1345EXPORT_SYMBOL_GPL(vb2_mmap);
1346
1347static int __vb2_init_fileio(struct vb2_queue *q, int read);
1348static int __vb2_cleanup_fileio(struct vb2_queue *q);
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
1367{
1368 unsigned long flags;
1369 unsigned int ret;
1370 struct vb2_buffer *vb = NULL;
1371
1372
1373
1374
1375 if (q->num_buffers == 0 && q->fileio == NULL) {
1376 if (!V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_READ)) {
1377 ret = __vb2_init_fileio(q, 1);
1378 if (ret)
1379 return POLLERR;
1380 }
1381 if (V4L2_TYPE_IS_OUTPUT(q->type) && (q->io_modes & VB2_WRITE)) {
1382 ret = __vb2_init_fileio(q, 0);
1383 if (ret)
1384 return POLLERR;
1385
1386
1387
1388 return POLLOUT | POLLWRNORM;
1389 }
1390 }
1391
1392
1393
1394
1395 if (list_empty(&q->queued_list))
1396 return POLLERR;
1397
1398 poll_wait(file, &q->done_wq, wait);
1399
1400
1401
1402
1403 spin_lock_irqsave(&q->done_lock, flags);
1404 if (!list_empty(&q->done_list))
1405 vb = list_first_entry(&q->done_list, struct vb2_buffer,
1406 done_entry);
1407 spin_unlock_irqrestore(&q->done_lock, flags);
1408
1409 if (vb && (vb->state == VB2_BUF_STATE_DONE
1410 || vb->state == VB2_BUF_STATE_ERROR)) {
1411 return (V4L2_TYPE_IS_OUTPUT(q->type)) ? POLLOUT | POLLWRNORM :
1412 POLLIN | POLLRDNORM;
1413 }
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(vb2_poll);
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429int vb2_queue_init(struct vb2_queue *q)
1430{
1431 BUG_ON(!q);
1432 BUG_ON(!q->ops);
1433 BUG_ON(!q->mem_ops);
1434 BUG_ON(!q->type);
1435 BUG_ON(!q->io_modes);
1436
1437 BUG_ON(!q->ops->queue_setup);
1438 BUG_ON(!q->ops->buf_queue);
1439
1440 INIT_LIST_HEAD(&q->queued_list);
1441 INIT_LIST_HEAD(&q->done_list);
1442 spin_lock_init(&q->done_lock);
1443 init_waitqueue_head(&q->done_wq);
1444
1445 if (q->buf_struct_size == 0)
1446 q->buf_struct_size = sizeof(struct vb2_buffer);
1447
1448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(vb2_queue_init);
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460void vb2_queue_release(struct vb2_queue *q)
1461{
1462 __vb2_cleanup_fileio(q);
1463 __vb2_queue_cancel(q);
1464 __vb2_queue_free(q);
1465}
1466EXPORT_SYMBOL_GPL(vb2_queue_release);
1467
1468
1469
1470
1471
1472
1473
1474
1475struct vb2_fileio_buf {
1476 void *vaddr;
1477 unsigned int size;
1478 unsigned int pos;
1479 unsigned int queued:1;
1480};
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490struct vb2_fileio_data {
1491 struct v4l2_requestbuffers req;
1492 struct v4l2_buffer b;
1493 struct vb2_fileio_buf bufs[VIDEO_MAX_FRAME];
1494 unsigned int index;
1495 unsigned int q_count;
1496 unsigned int dq_count;
1497 unsigned int flags;
1498};
1499
1500
1501
1502
1503
1504
1505static int __vb2_init_fileio(struct vb2_queue *q, int read)
1506{
1507 struct vb2_fileio_data *fileio;
1508 int i, ret;
1509 unsigned int count = 0;
1510
1511
1512
1513
1514 if ((read && !(q->io_modes & VB2_READ)) ||
1515 (!read && !(q->io_modes & VB2_WRITE)))
1516 BUG();
1517
1518
1519
1520
1521 if (!q->mem_ops->vaddr)
1522 return -EBUSY;
1523
1524
1525
1526
1527 if (q->streaming || q->num_buffers > 0)
1528 return -EBUSY;
1529
1530
1531
1532
1533 count = 1;
1534
1535 dprintk(3, "setting up file io: mode %s, count %d, flags %08x\n",
1536 (read) ? "read" : "write", count, q->io_flags);
1537
1538 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
1539 if (fileio == NULL)
1540 return -ENOMEM;
1541
1542 fileio->flags = q->io_flags;
1543
1544
1545
1546
1547
1548 fileio->req.count = count;
1549 fileio->req.memory = V4L2_MEMORY_MMAP;
1550 fileio->req.type = q->type;
1551 ret = vb2_reqbufs(q, &fileio->req);
1552 if (ret)
1553 goto err_kfree;
1554
1555
1556
1557
1558
1559 if (q->bufs[0]->num_planes != 1) {
1560 fileio->req.count = 0;
1561 ret = -EBUSY;
1562 goto err_reqbufs;
1563 }
1564
1565
1566
1567
1568 for (i = 0; i < q->num_buffers; i++) {
1569 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
1570 if (fileio->bufs[i].vaddr == NULL)
1571 goto err_reqbufs;
1572 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1573 }
1574
1575
1576
1577
1578 if (read) {
1579
1580
1581
1582 for (i = 0; i < q->num_buffers; i++) {
1583 struct v4l2_buffer *b = &fileio->b;
1584 memset(b, 0, sizeof(*b));
1585 b->type = q->type;
1586 b->memory = q->memory;
1587 b->index = i;
1588 ret = vb2_qbuf(q, b);
1589 if (ret)
1590 goto err_reqbufs;
1591 fileio->bufs[i].queued = 1;
1592 }
1593
1594
1595
1596
1597 ret = vb2_streamon(q, q->type);
1598 if (ret)
1599 goto err_reqbufs;
1600 }
1601
1602 q->fileio = fileio;
1603
1604 return ret;
1605
1606err_reqbufs:
1607 vb2_reqbufs(q, &fileio->req);
1608
1609err_kfree:
1610 kfree(fileio);
1611 return ret;
1612}
1613
1614
1615
1616
1617
1618static int __vb2_cleanup_fileio(struct vb2_queue *q)
1619{
1620 struct vb2_fileio_data *fileio = q->fileio;
1621
1622 if (fileio) {
1623
1624
1625
1626
1627 q->fileio = NULL;
1628
1629 vb2_streamoff(q, q->type);
1630 fileio->req.count = 0;
1631 vb2_reqbufs(q, &fileio->req);
1632 kfree(fileio);
1633 dprintk(3, "file io emulator closed\n");
1634 }
1635 return 0;
1636}
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1648 loff_t *ppos, int nonblock, int read)
1649{
1650 struct vb2_fileio_data *fileio;
1651 struct vb2_fileio_buf *buf;
1652 int ret, index;
1653
1654 dprintk(3, "file io: mode %s, offset %ld, count %zd, %sblocking\n",
1655 read ? "read" : "write", (long)*ppos, count,
1656 nonblock ? "non" : "");
1657
1658 if (!data)
1659 return -EINVAL;
1660
1661
1662
1663
1664 if (!q->fileio) {
1665 ret = __vb2_init_fileio(q, read);
1666 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1667 if (ret)
1668 return ret;
1669 }
1670 fileio = q->fileio;
1671
1672
1673
1674
1675
1676 q->fileio = NULL;
1677
1678 index = fileio->index;
1679 buf = &fileio->bufs[index];
1680
1681
1682
1683
1684 if (buf->queued) {
1685 struct vb2_buffer *vb;
1686
1687
1688
1689
1690 memset(&fileio->b, 0, sizeof(fileio->b));
1691 fileio->b.type = q->type;
1692 fileio->b.memory = q->memory;
1693 fileio->b.index = index;
1694 ret = vb2_dqbuf(q, &fileio->b, nonblock);
1695 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1696 if (ret)
1697 goto end;
1698 fileio->dq_count += 1;
1699
1700
1701
1702
1703 vb = q->bufs[index];
1704 buf->size = vb2_get_plane_payload(vb, 0);
1705 buf->queued = 0;
1706 }
1707
1708
1709
1710
1711 if (buf->pos + count > buf->size) {
1712 count = buf->size - buf->pos;
1713 dprintk(5, "reducing read count: %zd\n", count);
1714 }
1715
1716
1717
1718
1719 dprintk(3, "file io: copying %zd bytes - buffer %d, offset %u\n",
1720 count, index, buf->pos);
1721 if (read)
1722 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1723 else
1724 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1725 if (ret) {
1726 dprintk(3, "file io: error copying data\n");
1727 ret = -EFAULT;
1728 goto end;
1729 }
1730
1731
1732
1733
1734 buf->pos += count;
1735 *ppos += count;
1736
1737
1738
1739
1740 if (buf->pos == buf->size ||
1741 (!read && (fileio->flags & VB2_FILEIO_WRITE_IMMEDIATELY))) {
1742
1743
1744
1745 if (read && (fileio->flags & VB2_FILEIO_READ_ONCE) &&
1746 fileio->dq_count == 1) {
1747 dprintk(3, "file io: read limit reached\n");
1748
1749
1750
1751 q->fileio = fileio;
1752 return __vb2_cleanup_fileio(q);
1753 }
1754
1755
1756
1757
1758 memset(&fileio->b, 0, sizeof(fileio->b));
1759 fileio->b.type = q->type;
1760 fileio->b.memory = q->memory;
1761 fileio->b.index = index;
1762 fileio->b.bytesused = buf->pos;
1763 ret = vb2_qbuf(q, &fileio->b);
1764 dprintk(5, "file io: vb2_dbuf result: %d\n", ret);
1765 if (ret)
1766 goto end;
1767
1768
1769
1770
1771 buf->pos = 0;
1772 buf->queued = 1;
1773 buf->size = q->bufs[0]->v4l2_planes[0].length;
1774 fileio->q_count += 1;
1775
1776
1777
1778
1779 fileio->index = (index + 1) % q->num_buffers;
1780
1781
1782
1783
1784 if (!read && !q->streaming) {
1785 ret = vb2_streamon(q, q->type);
1786 if (ret)
1787 goto end;
1788 }
1789 }
1790
1791
1792
1793
1794 if (ret == 0)
1795 ret = count;
1796end:
1797
1798
1799
1800 q->fileio = fileio;
1801 return ret;
1802}
1803
1804size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1805 loff_t *ppos, int nonblocking)
1806{
1807 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1808}
1809EXPORT_SYMBOL_GPL(vb2_read);
1810
1811size_t vb2_write(struct vb2_queue *q, char __user *data, size_t count,
1812 loff_t *ppos, int nonblocking)
1813{
1814 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 0);
1815}
1816EXPORT_SYMBOL_GPL(vb2_write);
1817
1818MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1819MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1820MODULE_LICENSE("GPL");
1821