This source file includes following definitions.
- __vb2_buf_mem_alloc
- __vb2_buf_mem_free
- __vb2_buf_userptr_put
- __vb2_plane_dmabuf_put
- __vb2_buf_dmabuf_put
- __setup_offsets
- __vb2_queue_alloc
- __vb2_free_mem
- __vb2_queue_free
- vb2_buffer_in_use
- __buffers_in_use
- vb2_core_querybuf
- __verify_userptr_ops
- __verify_mmap_ops
- __verify_dmabuf_ops
- vb2_verify_memory_type
- vb2_core_reqbufs
- vb2_core_create_bufs
- vb2_plane_vaddr
- vb2_plane_cookie
- vb2_buffer_done
- vb2_discard_done
- __prepare_mmap
- __prepare_userptr
- __prepare_dmabuf
- __enqueue_in_driver
- __buf_prepare
- vb2_req_prepare
- vb2_req_unprepare
- vb2_req_queue
- vb2_req_unbind
- vb2_req_release
- vb2_request_object_is_buffer
- vb2_request_buffer_cnt
- vb2_core_prepare_buf
- vb2_start_streaming
- vb2_core_qbuf
- __vb2_wait_for_done_vb
- __vb2_get_done_vb
- vb2_wait_for_all_buffers
- __vb2_dqbuf
- vb2_core_dqbuf
- __vb2_queue_cancel
- vb2_core_streamon
- vb2_queue_error
- vb2_core_streamoff
- __find_plane_by_offset
- vb2_core_expbuf
- vb2_mmap
- vb2_get_unmapped_area
- vb2_core_queue_init
- vb2_core_queue_release
- vb2_core_poll
- __vb2_init_fileio
- __vb2_cleanup_fileio
- __vb2_perform_fileio
- vb2_read
- vb2_write
- vb2_thread
- vb2_thread_start
- vb2_thread_stop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/err.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mm.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/freezer.h>
27 #include <linux/kthread.h>
28
29 #include <media/videobuf2-core.h>
30 #include <media/v4l2-mc.h>
31
32 #include <trace/events/vb2.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("%s: " fmt, __func__, ## arg); \
41 } while (0)
42
43 #ifdef CONFIG_VIDEO_ADV_DEBUG
44
45
46
47
48
49
50
51
52
53 #define log_memop(vb, op) \
54 dprintk(2, "call_memop(%p, %d, %s)%s\n", \
55 (vb)->vb2_queue, (vb)->index, #op, \
56 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
57
58 #define call_memop(vb, op, args...) \
59 ({ \
60 struct vb2_queue *_q = (vb)->vb2_queue; \
61 int err; \
62 \
63 log_memop(vb, op); \
64 err = _q->mem_ops->op ? _q->mem_ops->op(args) : 0; \
65 if (!err) \
66 (vb)->cnt_mem_ ## op++; \
67 err; \
68 })
69
70 #define call_ptr_memop(vb, op, args...) \
71 ({ \
72 struct vb2_queue *_q = (vb)->vb2_queue; \
73 void *ptr; \
74 \
75 log_memop(vb, op); \
76 ptr = _q->mem_ops->op ? _q->mem_ops->op(args) : NULL; \
77 if (!IS_ERR_OR_NULL(ptr)) \
78 (vb)->cnt_mem_ ## op++; \
79 ptr; \
80 })
81
82 #define call_void_memop(vb, op, args...) \
83 ({ \
84 struct vb2_queue *_q = (vb)->vb2_queue; \
85 \
86 log_memop(vb, op); \
87 if (_q->mem_ops->op) \
88 _q->mem_ops->op(args); \
89 (vb)->cnt_mem_ ## op++; \
90 })
91
92 #define log_qop(q, op) \
93 dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
94 (q)->ops->op ? "" : " (nop)")
95
96 #define call_qop(q, op, args...) \
97 ({ \
98 int err; \
99 \
100 log_qop(q, op); \
101 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
102 if (!err) \
103 (q)->cnt_ ## op++; \
104 err; \
105 })
106
107 #define call_void_qop(q, op, args...) \
108 ({ \
109 log_qop(q, op); \
110 if ((q)->ops->op) \
111 (q)->ops->op(args); \
112 (q)->cnt_ ## op++; \
113 })
114
115 #define log_vb_qop(vb, op, args...) \
116 dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
117 (vb)->vb2_queue, (vb)->index, #op, \
118 (vb)->vb2_queue->ops->op ? "" : " (nop)")
119
120 #define call_vb_qop(vb, op, args...) \
121 ({ \
122 int err; \
123 \
124 log_vb_qop(vb, op); \
125 err = (vb)->vb2_queue->ops->op ? \
126 (vb)->vb2_queue->ops->op(args) : 0; \
127 if (!err) \
128 (vb)->cnt_ ## op++; \
129 err; \
130 })
131
132 #define call_void_vb_qop(vb, op, args...) \
133 ({ \
134 log_vb_qop(vb, op); \
135 if ((vb)->vb2_queue->ops->op) \
136 (vb)->vb2_queue->ops->op(args); \
137 (vb)->cnt_ ## op++; \
138 })
139
140 #else
141
142 #define call_memop(vb, op, args...) \
143 ((vb)->vb2_queue->mem_ops->op ? \
144 (vb)->vb2_queue->mem_ops->op(args) : 0)
145
146 #define call_ptr_memop(vb, op, args...) \
147 ((vb)->vb2_queue->mem_ops->op ? \
148 (vb)->vb2_queue->mem_ops->op(args) : NULL)
149
150 #define call_void_memop(vb, op, args...) \
151 do { \
152 if ((vb)->vb2_queue->mem_ops->op) \
153 (vb)->vb2_queue->mem_ops->op(args); \
154 } while (0)
155
156 #define call_qop(q, op, args...) \
157 ((q)->ops->op ? (q)->ops->op(args) : 0)
158
159 #define call_void_qop(q, op, args...) \
160 do { \
161 if ((q)->ops->op) \
162 (q)->ops->op(args); \
163 } while (0)
164
165 #define call_vb_qop(vb, op, args...) \
166 ((vb)->vb2_queue->ops->op ? (vb)->vb2_queue->ops->op(args) : 0)
167
168 #define call_void_vb_qop(vb, op, args...) \
169 do { \
170 if ((vb)->vb2_queue->ops->op) \
171 (vb)->vb2_queue->ops->op(args); \
172 } while (0)
173
174 #endif
175
176 #define call_bufop(q, op, args...) \
177 ({ \
178 int ret = 0; \
179 if (q && q->buf_ops && q->buf_ops->op) \
180 ret = q->buf_ops->op(args); \
181 ret; \
182 })
183
184 #define call_void_bufop(q, op, args...) \
185 ({ \
186 if (q && q->buf_ops && q->buf_ops->op) \
187 q->buf_ops->op(args); \
188 })
189
190 static void __vb2_queue_cancel(struct vb2_queue *q);
191 static void __enqueue_in_driver(struct vb2_buffer *vb);
192
193
194
195
196 static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
197 {
198 struct vb2_queue *q = vb->vb2_queue;
199 void *mem_priv;
200 int plane;
201 int ret = -ENOMEM;
202
203
204
205
206
207 for (plane = 0; plane < vb->num_planes; ++plane) {
208
209 unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
210
211
212 if (size < vb->planes[plane].length)
213 goto free;
214
215 mem_priv = call_ptr_memop(vb, alloc,
216 q->alloc_devs[plane] ? : q->dev,
217 q->dma_attrs, size, q->dma_dir, q->gfp_flags);
218 if (IS_ERR_OR_NULL(mem_priv)) {
219 if (mem_priv)
220 ret = PTR_ERR(mem_priv);
221 goto free;
222 }
223
224
225 vb->planes[plane].mem_priv = mem_priv;
226 }
227
228 return 0;
229 free:
230
231 for (; plane > 0; --plane) {
232 call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
233 vb->planes[plane - 1].mem_priv = NULL;
234 }
235
236 return ret;
237 }
238
239
240
241
242 static void __vb2_buf_mem_free(struct vb2_buffer *vb)
243 {
244 unsigned int plane;
245
246 for (plane = 0; plane < vb->num_planes; ++plane) {
247 call_void_memop(vb, put, vb->planes[plane].mem_priv);
248 vb->planes[plane].mem_priv = NULL;
249 dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
250 }
251 }
252
253
254
255
256
257 static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
258 {
259 unsigned int plane;
260
261 for (plane = 0; plane < vb->num_planes; ++plane) {
262 if (vb->planes[plane].mem_priv)
263 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
264 vb->planes[plane].mem_priv = NULL;
265 }
266 }
267
268
269
270
271
272 static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
273 {
274 if (!p->mem_priv)
275 return;
276
277 if (p->dbuf_mapped)
278 call_void_memop(vb, unmap_dmabuf, p->mem_priv);
279
280 call_void_memop(vb, detach_dmabuf, p->mem_priv);
281 dma_buf_put(p->dbuf);
282 p->mem_priv = NULL;
283 p->dbuf = NULL;
284 p->dbuf_mapped = 0;
285 }
286
287
288
289
290
291 static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
292 {
293 unsigned int plane;
294
295 for (plane = 0; plane < vb->num_planes; ++plane)
296 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
297 }
298
299
300
301
302
303 static void __setup_offsets(struct vb2_buffer *vb)
304 {
305 struct vb2_queue *q = vb->vb2_queue;
306 unsigned int plane;
307 unsigned long off = 0;
308
309 if (vb->index) {
310 struct vb2_buffer *prev = q->bufs[vb->index - 1];
311 struct vb2_plane *p = &prev->planes[prev->num_planes - 1];
312
313 off = PAGE_ALIGN(p->m.offset + p->length);
314 }
315
316 for (plane = 0; plane < vb->num_planes; ++plane) {
317 vb->planes[plane].m.offset = off;
318
319 dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
320 vb->index, plane, off);
321
322 off += vb->planes[plane].length;
323 off = PAGE_ALIGN(off);
324 }
325 }
326
327
328
329
330
331
332
333
334 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
335 unsigned int num_buffers, unsigned int num_planes,
336 const unsigned plane_sizes[VB2_MAX_PLANES])
337 {
338 unsigned int buffer, plane;
339 struct vb2_buffer *vb;
340 int ret;
341
342
343 num_buffers = min_t(unsigned int, num_buffers,
344 VB2_MAX_FRAME - q->num_buffers);
345
346 for (buffer = 0; buffer < num_buffers; ++buffer) {
347
348 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
349 if (!vb) {
350 dprintk(1, "memory alloc for buffer struct failed\n");
351 break;
352 }
353
354 vb->state = VB2_BUF_STATE_DEQUEUED;
355 vb->vb2_queue = q;
356 vb->num_planes = num_planes;
357 vb->index = q->num_buffers + buffer;
358 vb->type = q->type;
359 vb->memory = memory;
360 for (plane = 0; plane < num_planes; ++plane) {
361 vb->planes[plane].length = plane_sizes[plane];
362 vb->planes[plane].min_length = plane_sizes[plane];
363 }
364 call_void_bufop(q, init_buffer, vb);
365
366 q->bufs[vb->index] = vb;
367
368
369 if (memory == VB2_MEMORY_MMAP) {
370 ret = __vb2_buf_mem_alloc(vb);
371 if (ret) {
372 dprintk(1, "failed allocating memory for buffer %d\n",
373 buffer);
374 q->bufs[vb->index] = NULL;
375 kfree(vb);
376 break;
377 }
378 __setup_offsets(vb);
379
380
381
382
383
384 ret = call_vb_qop(vb, buf_init, vb);
385 if (ret) {
386 dprintk(1, "buffer %d %p initialization failed\n",
387 buffer, vb);
388 __vb2_buf_mem_free(vb);
389 q->bufs[vb->index] = NULL;
390 kfree(vb);
391 break;
392 }
393 }
394 }
395
396 dprintk(1, "allocated %d buffers, %d plane(s) each\n",
397 buffer, num_planes);
398
399 return buffer;
400 }
401
402
403
404
405 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
406 {
407 unsigned int buffer;
408 struct vb2_buffer *vb;
409
410 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
411 ++buffer) {
412 vb = q->bufs[buffer];
413 if (!vb)
414 continue;
415
416
417 if (q->memory == VB2_MEMORY_MMAP)
418 __vb2_buf_mem_free(vb);
419 else if (q->memory == VB2_MEMORY_DMABUF)
420 __vb2_buf_dmabuf_put(vb);
421 else
422 __vb2_buf_userptr_put(vb);
423 }
424 }
425
426
427
428
429
430
431 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
432 {
433 unsigned int buffer;
434
435
436
437
438
439
440
441
442
443 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
444 ++buffer) {
445 if (q->bufs[buffer] == NULL)
446 continue;
447 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
448 dprintk(1, "preparing buffers, cannot free\n");
449 return -EAGAIN;
450 }
451 }
452
453
454 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
455 ++buffer) {
456 struct vb2_buffer *vb = q->bufs[buffer];
457
458 if (vb && vb->planes[0].mem_priv)
459 call_void_vb_qop(vb, buf_cleanup, vb);
460 }
461
462
463 __vb2_free_mem(q, buffers);
464
465 #ifdef CONFIG_VIDEO_ADV_DEBUG
466
467
468
469
470
471 if (q->num_buffers) {
472 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
473 q->cnt_wait_prepare != q->cnt_wait_finish;
474
475 if (unbalanced || debug) {
476 pr_info("counters for queue %p:%s\n", q,
477 unbalanced ? " UNBALANCED!" : "");
478 pr_info(" setup: %u start_streaming: %u stop_streaming: %u\n",
479 q->cnt_queue_setup, q->cnt_start_streaming,
480 q->cnt_stop_streaming);
481 pr_info(" wait_prepare: %u wait_finish: %u\n",
482 q->cnt_wait_prepare, q->cnt_wait_finish);
483 }
484 q->cnt_queue_setup = 0;
485 q->cnt_wait_prepare = 0;
486 q->cnt_wait_finish = 0;
487 q->cnt_start_streaming = 0;
488 q->cnt_stop_streaming = 0;
489 }
490 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
491 struct vb2_buffer *vb = q->bufs[buffer];
492 bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
493 vb->cnt_mem_prepare != vb->cnt_mem_finish ||
494 vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
495 vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
496 vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
497 vb->cnt_buf_queue != vb->cnt_buf_done ||
498 vb->cnt_buf_prepare != vb->cnt_buf_finish ||
499 vb->cnt_buf_init != vb->cnt_buf_cleanup;
500
501 if (unbalanced || debug) {
502 pr_info(" counters for queue %p, buffer %d:%s\n",
503 q, buffer, unbalanced ? " UNBALANCED!" : "");
504 pr_info(" buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
505 vb->cnt_buf_init, vb->cnt_buf_cleanup,
506 vb->cnt_buf_prepare, vb->cnt_buf_finish);
507 pr_info(" buf_out_validate: %u buf_queue: %u buf_done: %u buf_request_complete: %u\n",
508 vb->cnt_buf_out_validate, vb->cnt_buf_queue,
509 vb->cnt_buf_done, vb->cnt_buf_request_complete);
510 pr_info(" alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
511 vb->cnt_mem_alloc, vb->cnt_mem_put,
512 vb->cnt_mem_prepare, vb->cnt_mem_finish,
513 vb->cnt_mem_mmap);
514 pr_info(" get_userptr: %u put_userptr: %u\n",
515 vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
516 pr_info(" attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
517 vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
518 vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
519 pr_info(" get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
520 vb->cnt_mem_get_dmabuf,
521 vb->cnt_mem_num_users,
522 vb->cnt_mem_vaddr,
523 vb->cnt_mem_cookie);
524 }
525 }
526 #endif
527
528
529 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
530 ++buffer) {
531 kfree(q->bufs[buffer]);
532 q->bufs[buffer] = NULL;
533 }
534
535 q->num_buffers -= buffers;
536 if (!q->num_buffers) {
537 q->memory = VB2_MEMORY_UNKNOWN;
538 INIT_LIST_HEAD(&q->queued_list);
539 }
540 return 0;
541 }
542
543 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
544 {
545 unsigned int plane;
546 for (plane = 0; plane < vb->num_planes; ++plane) {
547 void *mem_priv = vb->planes[plane].mem_priv;
548
549
550
551
552
553
554 if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
555 return true;
556 }
557 return false;
558 }
559 EXPORT_SYMBOL(vb2_buffer_in_use);
560
561
562
563
564
565 static bool __buffers_in_use(struct vb2_queue *q)
566 {
567 unsigned int buffer;
568 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
569 if (vb2_buffer_in_use(q, q->bufs[buffer]))
570 return true;
571 }
572 return false;
573 }
574
575 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
576 {
577 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb);
578 }
579 EXPORT_SYMBOL_GPL(vb2_core_querybuf);
580
581
582
583
584
585 static int __verify_userptr_ops(struct vb2_queue *q)
586 {
587 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
588 !q->mem_ops->put_userptr)
589 return -EINVAL;
590
591 return 0;
592 }
593
594
595
596
597
598 static int __verify_mmap_ops(struct vb2_queue *q)
599 {
600 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
601 !q->mem_ops->put || !q->mem_ops->mmap)
602 return -EINVAL;
603
604 return 0;
605 }
606
607
608
609
610
611 static int __verify_dmabuf_ops(struct vb2_queue *q)
612 {
613 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
614 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
615 !q->mem_ops->unmap_dmabuf)
616 return -EINVAL;
617
618 return 0;
619 }
620
621 int vb2_verify_memory_type(struct vb2_queue *q,
622 enum vb2_memory memory, unsigned int type)
623 {
624 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
625 memory != VB2_MEMORY_DMABUF) {
626 dprintk(1, "unsupported memory type\n");
627 return -EINVAL;
628 }
629
630 if (type != q->type) {
631 dprintk(1, "requested type is incorrect\n");
632 return -EINVAL;
633 }
634
635
636
637
638
639 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
640 dprintk(1, "MMAP for current setup unsupported\n");
641 return -EINVAL;
642 }
643
644 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
645 dprintk(1, "USERPTR for current setup unsupported\n");
646 return -EINVAL;
647 }
648
649 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
650 dprintk(1, "DMABUF for current setup unsupported\n");
651 return -EINVAL;
652 }
653
654
655
656
657
658
659 if (vb2_fileio_is_active(q)) {
660 dprintk(1, "file io in progress\n");
661 return -EBUSY;
662 }
663 return 0;
664 }
665 EXPORT_SYMBOL(vb2_verify_memory_type);
666
667 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
668 unsigned int *count)
669 {
670 unsigned int num_buffers, allocated_buffers, num_planes = 0;
671 unsigned plane_sizes[VB2_MAX_PLANES] = { };
672 unsigned int i;
673 int ret;
674
675 if (q->streaming) {
676 dprintk(1, "streaming active\n");
677 return -EBUSY;
678 }
679
680 if (q->waiting_in_dqbuf && *count) {
681 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
682 return -EBUSY;
683 }
684
685 if (*count == 0 || q->num_buffers != 0 ||
686 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
687
688
689
690
691 mutex_lock(&q->mmap_lock);
692 if (debug && q->memory == VB2_MEMORY_MMAP &&
693 __buffers_in_use(q))
694 dprintk(1, "memory in use, orphaning buffers\n");
695
696
697
698
699
700
701 __vb2_queue_cancel(q);
702 ret = __vb2_queue_free(q, q->num_buffers);
703 mutex_unlock(&q->mmap_lock);
704 if (ret)
705 return ret;
706
707
708
709
710
711 if (*count == 0)
712 return 0;
713 }
714
715
716
717
718 WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME);
719 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed);
720 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
721 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
722 q->memory = memory;
723
724
725
726
727
728 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
729 plane_sizes, q->alloc_devs);
730 if (ret)
731 return ret;
732
733
734 if (WARN_ON(!num_planes))
735 return -EINVAL;
736
737 for (i = 0; i < num_planes; i++)
738 if (WARN_ON(!plane_sizes[i]))
739 return -EINVAL;
740
741
742 allocated_buffers =
743 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
744 if (allocated_buffers == 0) {
745 dprintk(1, "memory allocation failed\n");
746 return -ENOMEM;
747 }
748
749
750
751
752
753 if (allocated_buffers < q->min_buffers_needed)
754 ret = -ENOMEM;
755
756
757
758
759 if (!ret && allocated_buffers < num_buffers) {
760 num_buffers = allocated_buffers;
761
762
763
764
765
766
767 num_planes = 0;
768
769 ret = call_qop(q, queue_setup, q, &num_buffers,
770 &num_planes, plane_sizes, q->alloc_devs);
771
772 if (!ret && allocated_buffers < num_buffers)
773 ret = -ENOMEM;
774
775
776
777
778
779 }
780
781 mutex_lock(&q->mmap_lock);
782 q->num_buffers = allocated_buffers;
783
784 if (ret < 0) {
785
786
787
788
789 __vb2_queue_free(q, allocated_buffers);
790 mutex_unlock(&q->mmap_lock);
791 return ret;
792 }
793 mutex_unlock(&q->mmap_lock);
794
795
796
797
798
799 *count = allocated_buffers;
800 q->waiting_for_buffers = !q->is_output;
801
802 return 0;
803 }
804 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
805
806 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
807 unsigned int *count, unsigned requested_planes,
808 const unsigned requested_sizes[])
809 {
810 unsigned int num_planes = 0, num_buffers, allocated_buffers;
811 unsigned plane_sizes[VB2_MAX_PLANES] = { };
812 int ret;
813
814 if (q->num_buffers == VB2_MAX_FRAME) {
815 dprintk(1, "maximum number of buffers already allocated\n");
816 return -ENOBUFS;
817 }
818
819 if (!q->num_buffers) {
820 if (q->waiting_in_dqbuf && *count) {
821 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
822 return -EBUSY;
823 }
824 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
825 q->memory = memory;
826 q->waiting_for_buffers = !q->is_output;
827 } else if (q->memory != memory) {
828 dprintk(1, "memory model mismatch\n");
829 return -EINVAL;
830 }
831
832 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
833
834 if (requested_planes && requested_sizes) {
835 num_planes = requested_planes;
836 memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
837 }
838
839
840
841
842
843 ret = call_qop(q, queue_setup, q, &num_buffers,
844 &num_planes, plane_sizes, q->alloc_devs);
845 if (ret)
846 return ret;
847
848
849 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
850 num_planes, plane_sizes);
851 if (allocated_buffers == 0) {
852 dprintk(1, "memory allocation failed\n");
853 return -ENOMEM;
854 }
855
856
857
858
859 if (allocated_buffers < num_buffers) {
860 num_buffers = allocated_buffers;
861
862
863
864
865
866 ret = call_qop(q, queue_setup, q, &num_buffers,
867 &num_planes, plane_sizes, q->alloc_devs);
868
869 if (!ret && allocated_buffers < num_buffers)
870 ret = -ENOMEM;
871
872
873
874
875
876 }
877
878 mutex_lock(&q->mmap_lock);
879 q->num_buffers += allocated_buffers;
880
881 if (ret < 0) {
882
883
884
885
886 __vb2_queue_free(q, allocated_buffers);
887 mutex_unlock(&q->mmap_lock);
888 return -ENOMEM;
889 }
890 mutex_unlock(&q->mmap_lock);
891
892
893
894
895
896 *count = allocated_buffers;
897
898 return 0;
899 }
900 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
901
902 void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
903 {
904 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
905 return NULL;
906
907 return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
908
909 }
910 EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
911
912 void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
913 {
914 if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
915 return NULL;
916
917 return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
918 }
919 EXPORT_SYMBOL_GPL(vb2_plane_cookie);
920
921 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
922 {
923 struct vb2_queue *q = vb->vb2_queue;
924 unsigned long flags;
925 unsigned int plane;
926
927 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
928 return;
929
930 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
931 state != VB2_BUF_STATE_ERROR &&
932 state != VB2_BUF_STATE_QUEUED))
933 state = VB2_BUF_STATE_ERROR;
934
935 #ifdef CONFIG_VIDEO_ADV_DEBUG
936
937
938
939
940 vb->cnt_buf_done++;
941 #endif
942 dprintk(4, "done processing on buffer %d, state: %d\n",
943 vb->index, state);
944
945 if (state != VB2_BUF_STATE_QUEUED) {
946
947 for (plane = 0; plane < vb->num_planes; ++plane)
948 call_void_memop(vb, finish, vb->planes[plane].mem_priv);
949 vb->synced = 0;
950 }
951
952 spin_lock_irqsave(&q->done_lock, flags);
953 if (state == VB2_BUF_STATE_QUEUED) {
954 vb->state = VB2_BUF_STATE_QUEUED;
955 } else {
956
957 list_add_tail(&vb->done_entry, &q->done_list);
958 vb->state = state;
959 }
960 atomic_dec(&q->owned_by_drv_count);
961
962 if (state != VB2_BUF_STATE_QUEUED && vb->req_obj.req) {
963 media_request_object_unbind(&vb->req_obj);
964 media_request_object_put(&vb->req_obj);
965 }
966
967 spin_unlock_irqrestore(&q->done_lock, flags);
968
969 trace_vb2_buf_done(q, vb);
970
971 switch (state) {
972 case VB2_BUF_STATE_QUEUED:
973 return;
974 default:
975
976 wake_up(&q->done_wq);
977 break;
978 }
979 }
980 EXPORT_SYMBOL_GPL(vb2_buffer_done);
981
982 void vb2_discard_done(struct vb2_queue *q)
983 {
984 struct vb2_buffer *vb;
985 unsigned long flags;
986
987 spin_lock_irqsave(&q->done_lock, flags);
988 list_for_each_entry(vb, &q->done_list, done_entry)
989 vb->state = VB2_BUF_STATE_ERROR;
990 spin_unlock_irqrestore(&q->done_lock, flags);
991 }
992 EXPORT_SYMBOL_GPL(vb2_discard_done);
993
994
995
996
997 static int __prepare_mmap(struct vb2_buffer *vb)
998 {
999 int ret = 0;
1000
1001 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1002 vb, vb->planes);
1003 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
1004 }
1005
1006
1007
1008
1009 static int __prepare_userptr(struct vb2_buffer *vb)
1010 {
1011 struct vb2_plane planes[VB2_MAX_PLANES];
1012 struct vb2_queue *q = vb->vb2_queue;
1013 void *mem_priv;
1014 unsigned int plane;
1015 int ret = 0;
1016 bool reacquired = vb->planes[0].mem_priv == NULL;
1017
1018 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1019
1020 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1021 vb, planes);
1022 if (ret)
1023 return ret;
1024
1025 for (plane = 0; plane < vb->num_planes; ++plane) {
1026
1027 if (vb->planes[plane].m.userptr &&
1028 vb->planes[plane].m.userptr == planes[plane].m.userptr
1029 && vb->planes[plane].length == planes[plane].length)
1030 continue;
1031
1032 dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
1033 plane);
1034
1035
1036 if (planes[plane].length < vb->planes[plane].min_length) {
1037 dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
1038 planes[plane].length,
1039 vb->planes[plane].min_length,
1040 plane);
1041 ret = -EINVAL;
1042 goto err;
1043 }
1044
1045
1046 if (vb->planes[plane].mem_priv) {
1047 if (!reacquired) {
1048 reacquired = true;
1049 vb->copied_timestamp = 0;
1050 call_void_vb_qop(vb, buf_cleanup, vb);
1051 }
1052 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
1053 }
1054
1055 vb->planes[plane].mem_priv = NULL;
1056 vb->planes[plane].bytesused = 0;
1057 vb->planes[plane].length = 0;
1058 vb->planes[plane].m.userptr = 0;
1059 vb->planes[plane].data_offset = 0;
1060
1061
1062 mem_priv = call_ptr_memop(vb, get_userptr,
1063 q->alloc_devs[plane] ? : q->dev,
1064 planes[plane].m.userptr,
1065 planes[plane].length, q->dma_dir);
1066 if (IS_ERR(mem_priv)) {
1067 dprintk(1, "failed acquiring userspace memory for plane %d\n",
1068 plane);
1069 ret = PTR_ERR(mem_priv);
1070 goto err;
1071 }
1072 vb->planes[plane].mem_priv = mem_priv;
1073 }
1074
1075
1076
1077
1078
1079 for (plane = 0; plane < vb->num_planes; ++plane) {
1080 vb->planes[plane].bytesused = planes[plane].bytesused;
1081 vb->planes[plane].length = planes[plane].length;
1082 vb->planes[plane].m.userptr = planes[plane].m.userptr;
1083 vb->planes[plane].data_offset = planes[plane].data_offset;
1084 }
1085
1086 if (reacquired) {
1087
1088
1089
1090
1091
1092 ret = call_vb_qop(vb, buf_init, vb);
1093 if (ret) {
1094 dprintk(1, "buffer initialization failed\n");
1095 goto err;
1096 }
1097 }
1098
1099 ret = call_vb_qop(vb, buf_prepare, vb);
1100 if (ret) {
1101 dprintk(1, "buffer preparation failed\n");
1102 call_void_vb_qop(vb, buf_cleanup, vb);
1103 goto err;
1104 }
1105
1106 return 0;
1107 err:
1108
1109 for (plane = 0; plane < vb->num_planes; ++plane) {
1110 if (vb->planes[plane].mem_priv)
1111 call_void_memop(vb, put_userptr,
1112 vb->planes[plane].mem_priv);
1113 vb->planes[plane].mem_priv = NULL;
1114 vb->planes[plane].m.userptr = 0;
1115 vb->planes[plane].length = 0;
1116 }
1117
1118 return ret;
1119 }
1120
1121
1122
1123
1124 static int __prepare_dmabuf(struct vb2_buffer *vb)
1125 {
1126 struct vb2_plane planes[VB2_MAX_PLANES];
1127 struct vb2_queue *q = vb->vb2_queue;
1128 void *mem_priv;
1129 unsigned int plane;
1130 int ret = 0;
1131 bool reacquired = vb->planes[0].mem_priv == NULL;
1132
1133 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
1134
1135 ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1136 vb, planes);
1137 if (ret)
1138 return ret;
1139
1140 for (plane = 0; plane < vb->num_planes; ++plane) {
1141 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
1142
1143 if (IS_ERR_OR_NULL(dbuf)) {
1144 dprintk(1, "invalid dmabuf fd for plane %d\n",
1145 plane);
1146 ret = -EINVAL;
1147 goto err;
1148 }
1149
1150
1151 if (planes[plane].length == 0)
1152 planes[plane].length = dbuf->size;
1153
1154 if (planes[plane].length < vb->planes[plane].min_length) {
1155 dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1156 planes[plane].length, plane,
1157 vb->planes[plane].min_length);
1158 dma_buf_put(dbuf);
1159 ret = -EINVAL;
1160 goto err;
1161 }
1162
1163
1164 if (dbuf == vb->planes[plane].dbuf &&
1165 vb->planes[plane].length == planes[plane].length) {
1166 dma_buf_put(dbuf);
1167 continue;
1168 }
1169
1170 dprintk(3, "buffer for plane %d changed\n", plane);
1171
1172 if (!reacquired) {
1173 reacquired = true;
1174 vb->copied_timestamp = 0;
1175 call_void_vb_qop(vb, buf_cleanup, vb);
1176 }
1177
1178
1179 __vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
1180 vb->planes[plane].bytesused = 0;
1181 vb->planes[plane].length = 0;
1182 vb->planes[plane].m.fd = 0;
1183 vb->planes[plane].data_offset = 0;
1184
1185
1186 mem_priv = call_ptr_memop(vb, attach_dmabuf,
1187 q->alloc_devs[plane] ? : q->dev,
1188 dbuf, planes[plane].length, q->dma_dir);
1189 if (IS_ERR(mem_priv)) {
1190 dprintk(1, "failed to attach dmabuf\n");
1191 ret = PTR_ERR(mem_priv);
1192 dma_buf_put(dbuf);
1193 goto err;
1194 }
1195
1196 vb->planes[plane].dbuf = dbuf;
1197 vb->planes[plane].mem_priv = mem_priv;
1198 }
1199
1200
1201
1202
1203
1204
1205 for (plane = 0; plane < vb->num_planes; ++plane) {
1206 if (vb->planes[plane].dbuf_mapped)
1207 continue;
1208
1209 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
1210 if (ret) {
1211 dprintk(1, "failed to map dmabuf for plane %d\n",
1212 plane);
1213 goto err;
1214 }
1215 vb->planes[plane].dbuf_mapped = 1;
1216 }
1217
1218
1219
1220
1221
1222 for (plane = 0; plane < vb->num_planes; ++plane) {
1223 vb->planes[plane].bytesused = planes[plane].bytesused;
1224 vb->planes[plane].length = planes[plane].length;
1225 vb->planes[plane].m.fd = planes[plane].m.fd;
1226 vb->planes[plane].data_offset = planes[plane].data_offset;
1227 }
1228
1229 if (reacquired) {
1230
1231
1232
1233
1234 ret = call_vb_qop(vb, buf_init, vb);
1235 if (ret) {
1236 dprintk(1, "buffer initialization failed\n");
1237 goto err;
1238 }
1239 }
1240
1241 ret = call_vb_qop(vb, buf_prepare, vb);
1242 if (ret) {
1243 dprintk(1, "buffer preparation failed\n");
1244 call_void_vb_qop(vb, buf_cleanup, vb);
1245 goto err;
1246 }
1247
1248 return 0;
1249 err:
1250
1251 __vb2_buf_dmabuf_put(vb);
1252
1253 return ret;
1254 }
1255
1256
1257
1258
1259 static void __enqueue_in_driver(struct vb2_buffer *vb)
1260 {
1261 struct vb2_queue *q = vb->vb2_queue;
1262
1263 vb->state = VB2_BUF_STATE_ACTIVE;
1264 atomic_inc(&q->owned_by_drv_count);
1265
1266 trace_vb2_buf_queue(q, vb);
1267
1268 call_void_vb_qop(vb, buf_queue, vb);
1269 }
1270
1271 static int __buf_prepare(struct vb2_buffer *vb)
1272 {
1273 struct vb2_queue *q = vb->vb2_queue;
1274 enum vb2_buffer_state orig_state = vb->state;
1275 unsigned int plane;
1276 int ret;
1277
1278 if (q->error) {
1279 dprintk(1, "fatal error occurred on queue\n");
1280 return -EIO;
1281 }
1282
1283 if (vb->prepared)
1284 return 0;
1285 WARN_ON(vb->synced);
1286
1287 if (q->is_output) {
1288 ret = call_vb_qop(vb, buf_out_validate, vb);
1289 if (ret) {
1290 dprintk(1, "buffer validation failed\n");
1291 return ret;
1292 }
1293 }
1294
1295 vb->state = VB2_BUF_STATE_PREPARING;
1296
1297 switch (q->memory) {
1298 case VB2_MEMORY_MMAP:
1299 ret = __prepare_mmap(vb);
1300 break;
1301 case VB2_MEMORY_USERPTR:
1302 ret = __prepare_userptr(vb);
1303 break;
1304 case VB2_MEMORY_DMABUF:
1305 ret = __prepare_dmabuf(vb);
1306 break;
1307 default:
1308 WARN(1, "Invalid queue type\n");
1309 ret = -EINVAL;
1310 break;
1311 }
1312
1313 if (ret) {
1314 dprintk(1, "buffer preparation failed: %d\n", ret);
1315 vb->state = orig_state;
1316 return ret;
1317 }
1318
1319
1320 for (plane = 0; plane < vb->num_planes; ++plane)
1321 call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
1322
1323 vb->synced = 1;
1324 vb->prepared = 1;
1325 vb->state = orig_state;
1326
1327 return 0;
1328 }
1329
1330 static int vb2_req_prepare(struct media_request_object *obj)
1331 {
1332 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1333 int ret;
1334
1335 if (WARN_ON(vb->state != VB2_BUF_STATE_IN_REQUEST))
1336 return -EINVAL;
1337
1338 mutex_lock(vb->vb2_queue->lock);
1339 ret = __buf_prepare(vb);
1340 mutex_unlock(vb->vb2_queue->lock);
1341 return ret;
1342 }
1343
1344 static void __vb2_dqbuf(struct vb2_buffer *vb);
1345
1346 static void vb2_req_unprepare(struct media_request_object *obj)
1347 {
1348 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1349
1350 mutex_lock(vb->vb2_queue->lock);
1351 __vb2_dqbuf(vb);
1352 vb->state = VB2_BUF_STATE_IN_REQUEST;
1353 mutex_unlock(vb->vb2_queue->lock);
1354 WARN_ON(!vb->req_obj.req);
1355 }
1356
1357 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1358 struct media_request *req);
1359
1360 static void vb2_req_queue(struct media_request_object *obj)
1361 {
1362 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1363
1364 mutex_lock(vb->vb2_queue->lock);
1365 vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
1366 mutex_unlock(vb->vb2_queue->lock);
1367 }
1368
1369 static void vb2_req_unbind(struct media_request_object *obj)
1370 {
1371 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1372
1373 if (vb->state == VB2_BUF_STATE_IN_REQUEST)
1374 call_void_bufop(vb->vb2_queue, init_buffer, vb);
1375 }
1376
1377 static void vb2_req_release(struct media_request_object *obj)
1378 {
1379 struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1380
1381 if (vb->state == VB2_BUF_STATE_IN_REQUEST) {
1382 vb->state = VB2_BUF_STATE_DEQUEUED;
1383 if (vb->request)
1384 media_request_put(vb->request);
1385 vb->request = NULL;
1386 }
1387 }
1388
1389 static const struct media_request_object_ops vb2_core_req_ops = {
1390 .prepare = vb2_req_prepare,
1391 .unprepare = vb2_req_unprepare,
1392 .queue = vb2_req_queue,
1393 .unbind = vb2_req_unbind,
1394 .release = vb2_req_release,
1395 };
1396
1397 bool vb2_request_object_is_buffer(struct media_request_object *obj)
1398 {
1399 return obj->ops == &vb2_core_req_ops;
1400 }
1401 EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer);
1402
1403 unsigned int vb2_request_buffer_cnt(struct media_request *req)
1404 {
1405 struct media_request_object *obj;
1406 unsigned long flags;
1407 unsigned int buffer_cnt = 0;
1408
1409 spin_lock_irqsave(&req->lock, flags);
1410 list_for_each_entry(obj, &req->objects, list)
1411 if (vb2_request_object_is_buffer(obj))
1412 buffer_cnt++;
1413 spin_unlock_irqrestore(&req->lock, flags);
1414
1415 return buffer_cnt;
1416 }
1417 EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
1418
1419 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
1420 {
1421 struct vb2_buffer *vb;
1422 int ret;
1423
1424 vb = q->bufs[index];
1425 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1426 dprintk(1, "invalid buffer state %d\n",
1427 vb->state);
1428 return -EINVAL;
1429 }
1430 if (vb->prepared) {
1431 dprintk(1, "buffer already prepared\n");
1432 return -EINVAL;
1433 }
1434
1435 ret = __buf_prepare(vb);
1436 if (ret)
1437 return ret;
1438
1439
1440 call_void_bufop(q, fill_user_buffer, vb, pb);
1441
1442 dprintk(2, "prepare of buffer %d succeeded\n", vb->index);
1443
1444 return 0;
1445 }
1446 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459 static int vb2_start_streaming(struct vb2_queue *q)
1460 {
1461 struct vb2_buffer *vb;
1462 int ret;
1463
1464
1465
1466
1467
1468 list_for_each_entry(vb, &q->queued_list, queued_entry)
1469 __enqueue_in_driver(vb);
1470
1471
1472 q->start_streaming_called = 1;
1473 ret = call_qop(q, start_streaming, q,
1474 atomic_read(&q->owned_by_drv_count));
1475 if (!ret)
1476 return 0;
1477
1478 q->start_streaming_called = 0;
1479
1480 dprintk(1, "driver refused to start streaming\n");
1481
1482
1483
1484
1485
1486
1487 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1488 unsigned i;
1489
1490
1491
1492
1493
1494 for (i = 0; i < q->num_buffers; ++i) {
1495 vb = q->bufs[i];
1496 if (vb->state == VB2_BUF_STATE_ACTIVE)
1497 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1498 }
1499
1500 WARN_ON(atomic_read(&q->owned_by_drv_count));
1501 }
1502
1503
1504
1505
1506
1507 WARN_ON(!list_empty(&q->done_list));
1508 return ret;
1509 }
1510
1511 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1512 struct media_request *req)
1513 {
1514 struct vb2_buffer *vb;
1515 int ret;
1516
1517 if (q->error) {
1518 dprintk(1, "fatal error occurred on queue\n");
1519 return -EIO;
1520 }
1521
1522 vb = q->bufs[index];
1523
1524 if (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1525 q->requires_requests) {
1526 dprintk(1, "qbuf requires a request\n");
1527 return -EBADR;
1528 }
1529
1530 if ((req && q->uses_qbuf) ||
1531 (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1532 q->uses_requests)) {
1533 dprintk(1, "queue in wrong mode (qbuf vs requests)\n");
1534 return -EBUSY;
1535 }
1536
1537 if (req) {
1538 int ret;
1539
1540 q->uses_requests = 1;
1541 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1542 dprintk(1, "buffer %d not in dequeued state\n",
1543 vb->index);
1544 return -EINVAL;
1545 }
1546
1547 if (q->is_output && !vb->prepared) {
1548 ret = call_vb_qop(vb, buf_out_validate, vb);
1549 if (ret) {
1550 dprintk(1, "buffer validation failed\n");
1551 return ret;
1552 }
1553 }
1554
1555 media_request_object_init(&vb->req_obj);
1556
1557
1558 ret = media_request_lock_for_update(req);
1559 if (ret)
1560 return ret;
1561 ret = media_request_object_bind(req, &vb2_core_req_ops,
1562 q, true, &vb->req_obj);
1563 media_request_unlock_for_update(req);
1564 if (ret)
1565 return ret;
1566
1567 vb->state = VB2_BUF_STATE_IN_REQUEST;
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577 media_request_get(req);
1578 vb->request = req;
1579
1580
1581 if (pb) {
1582 call_void_bufop(q, copy_timestamp, vb, pb);
1583 call_void_bufop(q, fill_user_buffer, vb, pb);
1584 }
1585
1586 dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1587 return 0;
1588 }
1589
1590 if (vb->state != VB2_BUF_STATE_IN_REQUEST)
1591 q->uses_qbuf = 1;
1592
1593 switch (vb->state) {
1594 case VB2_BUF_STATE_DEQUEUED:
1595 case VB2_BUF_STATE_IN_REQUEST:
1596 if (!vb->prepared) {
1597 ret = __buf_prepare(vb);
1598 if (ret)
1599 return ret;
1600 }
1601 break;
1602 case VB2_BUF_STATE_PREPARING:
1603 dprintk(1, "buffer still being prepared\n");
1604 return -EINVAL;
1605 default:
1606 dprintk(1, "invalid buffer state %d\n", vb->state);
1607 return -EINVAL;
1608 }
1609
1610
1611
1612
1613
1614 list_add_tail(&vb->queued_entry, &q->queued_list);
1615 q->queued_count++;
1616 q->waiting_for_buffers = false;
1617 vb->state = VB2_BUF_STATE_QUEUED;
1618
1619 if (pb)
1620 call_void_bufop(q, copy_timestamp, vb, pb);
1621
1622 trace_vb2_qbuf(q, vb);
1623
1624
1625
1626
1627
1628 if (q->start_streaming_called)
1629 __enqueue_in_driver(vb);
1630
1631
1632 if (pb)
1633 call_void_bufop(q, fill_user_buffer, vb, pb);
1634
1635
1636
1637
1638
1639
1640
1641 if (q->streaming && !q->start_streaming_called &&
1642 q->queued_count >= q->min_buffers_needed) {
1643 ret = vb2_start_streaming(q);
1644 if (ret)
1645 return ret;
1646 }
1647
1648 dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1649 return 0;
1650 }
1651 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
1652
1653
1654
1655
1656
1657
1658
1659 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
1660 {
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670 for (;;) {
1671 int ret;
1672
1673 if (q->waiting_in_dqbuf) {
1674 dprintk(1, "another dup()ped fd is waiting for a buffer\n");
1675 return -EBUSY;
1676 }
1677
1678 if (!q->streaming) {
1679 dprintk(1, "streaming off, will not wait for buffers\n");
1680 return -EINVAL;
1681 }
1682
1683 if (q->error) {
1684 dprintk(1, "Queue in error state, will not wait for buffers\n");
1685 return -EIO;
1686 }
1687
1688 if (q->last_buffer_dequeued) {
1689 dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1690 return -EPIPE;
1691 }
1692
1693 if (!list_empty(&q->done_list)) {
1694
1695
1696
1697 break;
1698 }
1699
1700 if (nonblocking) {
1701 dprintk(3, "nonblocking and no buffers to dequeue, will not wait\n");
1702 return -EAGAIN;
1703 }
1704
1705 q->waiting_in_dqbuf = 1;
1706
1707
1708
1709
1710
1711 call_void_qop(q, wait_prepare, q);
1712
1713
1714
1715
1716 dprintk(3, "will sleep waiting for buffers\n");
1717 ret = wait_event_interruptible(q->done_wq,
1718 !list_empty(&q->done_list) || !q->streaming ||
1719 q->error);
1720
1721
1722
1723
1724
1725 call_void_qop(q, wait_finish, q);
1726 q->waiting_in_dqbuf = 0;
1727 if (ret) {
1728 dprintk(1, "sleep was interrupted\n");
1729 return ret;
1730 }
1731 }
1732 return 0;
1733 }
1734
1735
1736
1737
1738
1739
1740 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
1741 void *pb, int nonblocking)
1742 {
1743 unsigned long flags;
1744 int ret = 0;
1745
1746
1747
1748
1749 ret = __vb2_wait_for_done_vb(q, nonblocking);
1750 if (ret)
1751 return ret;
1752
1753
1754
1755
1756
1757 spin_lock_irqsave(&q->done_lock, flags);
1758 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
1759
1760
1761
1762
1763
1764 if (pb)
1765 ret = call_bufop(q, verify_planes_array, *vb, pb);
1766 if (!ret)
1767 list_del(&(*vb)->done_entry);
1768 spin_unlock_irqrestore(&q->done_lock, flags);
1769
1770 return ret;
1771 }
1772
1773 int vb2_wait_for_all_buffers(struct vb2_queue *q)
1774 {
1775 if (!q->streaming) {
1776 dprintk(1, "streaming off, will not wait for buffers\n");
1777 return -EINVAL;
1778 }
1779
1780 if (q->start_streaming_called)
1781 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1782 return 0;
1783 }
1784 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
1785
1786
1787
1788
1789 static void __vb2_dqbuf(struct vb2_buffer *vb)
1790 {
1791 struct vb2_queue *q = vb->vb2_queue;
1792
1793
1794 if (vb->state == VB2_BUF_STATE_DEQUEUED)
1795 return;
1796
1797 vb->state = VB2_BUF_STATE_DEQUEUED;
1798
1799 call_void_bufop(q, init_buffer, vb);
1800 }
1801
1802 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
1803 bool nonblocking)
1804 {
1805 struct vb2_buffer *vb = NULL;
1806 int ret;
1807
1808 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking);
1809 if (ret < 0)
1810 return ret;
1811
1812 switch (vb->state) {
1813 case VB2_BUF_STATE_DONE:
1814 dprintk(3, "returning done buffer\n");
1815 break;
1816 case VB2_BUF_STATE_ERROR:
1817 dprintk(3, "returning done buffer with errors\n");
1818 break;
1819 default:
1820 dprintk(1, "invalid buffer state\n");
1821 return -EINVAL;
1822 }
1823
1824 call_void_vb_qop(vb, buf_finish, vb);
1825 vb->prepared = 0;
1826
1827 if (pindex)
1828 *pindex = vb->index;
1829
1830
1831 if (pb)
1832 call_void_bufop(q, fill_user_buffer, vb, pb);
1833
1834
1835 list_del(&vb->queued_entry);
1836 q->queued_count--;
1837
1838 trace_vb2_dqbuf(q, vb);
1839
1840
1841 __vb2_dqbuf(vb);
1842
1843 if (WARN_ON(vb->req_obj.req)) {
1844 media_request_object_unbind(&vb->req_obj);
1845 media_request_object_put(&vb->req_obj);
1846 }
1847 if (vb->request)
1848 media_request_put(vb->request);
1849 vb->request = NULL;
1850
1851 dprintk(2, "dqbuf of buffer %d, with state %d\n",
1852 vb->index, vb->state);
1853
1854 return 0;
1855
1856 }
1857 EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
1858
1859
1860
1861
1862
1863
1864
1865 static void __vb2_queue_cancel(struct vb2_queue *q)
1866 {
1867 unsigned int i;
1868
1869
1870
1871
1872
1873 if (q->start_streaming_called)
1874 call_void_qop(q, stop_streaming, q);
1875
1876
1877
1878
1879
1880
1881
1882 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1883 for (i = 0; i < q->num_buffers; ++i)
1884 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
1885 pr_warn("driver bug: stop_streaming operation is leaving buf %p in active state\n",
1886 q->bufs[i]);
1887 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
1888 }
1889
1890 WARN_ON(atomic_read(&q->owned_by_drv_count));
1891 }
1892
1893 q->streaming = 0;
1894 q->start_streaming_called = 0;
1895 q->queued_count = 0;
1896 q->error = 0;
1897 q->uses_requests = 0;
1898 q->uses_qbuf = 0;
1899
1900
1901
1902
1903 INIT_LIST_HEAD(&q->queued_list);
1904
1905
1906
1907
1908 INIT_LIST_HEAD(&q->done_list);
1909 atomic_set(&q->owned_by_drv_count, 0);
1910 wake_up_all(&q->done_wq);
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921 for (i = 0; i < q->num_buffers; ++i) {
1922 struct vb2_buffer *vb = q->bufs[i];
1923 struct media_request *req = vb->req_obj.req;
1924
1925
1926
1927
1928
1929
1930
1931 if (req) {
1932 enum media_request_state state;
1933 unsigned long flags;
1934
1935 spin_lock_irqsave(&req->lock, flags);
1936 state = req->state;
1937 spin_unlock_irqrestore(&req->lock, flags);
1938
1939 if (state == MEDIA_REQUEST_STATE_QUEUED)
1940 call_void_vb_qop(vb, buf_request_complete, vb);
1941 }
1942
1943 if (vb->synced) {
1944 unsigned int plane;
1945
1946 for (plane = 0; plane < vb->num_planes; ++plane)
1947 call_void_memop(vb, finish,
1948 vb->planes[plane].mem_priv);
1949 vb->synced = 0;
1950 }
1951
1952 if (vb->prepared) {
1953 call_void_vb_qop(vb, buf_finish, vb);
1954 vb->prepared = 0;
1955 }
1956 __vb2_dqbuf(vb);
1957
1958 if (vb->req_obj.req) {
1959 media_request_object_unbind(&vb->req_obj);
1960 media_request_object_put(&vb->req_obj);
1961 }
1962 if (vb->request)
1963 media_request_put(vb->request);
1964 vb->request = NULL;
1965 vb->copied_timestamp = 0;
1966 }
1967 }
1968
1969 int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
1970 {
1971 int ret;
1972
1973 if (type != q->type) {
1974 dprintk(1, "invalid stream type\n");
1975 return -EINVAL;
1976 }
1977
1978 if (q->streaming) {
1979 dprintk(3, "already streaming\n");
1980 return 0;
1981 }
1982
1983 if (!q->num_buffers) {
1984 dprintk(1, "no buffers have been allocated\n");
1985 return -EINVAL;
1986 }
1987
1988 if (q->num_buffers < q->min_buffers_needed) {
1989 dprintk(1, "need at least %u allocated buffers\n",
1990 q->min_buffers_needed);
1991 return -EINVAL;
1992 }
1993
1994
1995
1996
1997
1998 if (q->queued_count >= q->min_buffers_needed) {
1999 ret = v4l_vb2q_enable_media_source(q);
2000 if (ret)
2001 return ret;
2002 ret = vb2_start_streaming(q);
2003 if (ret)
2004 return ret;
2005 }
2006
2007 q->streaming = 1;
2008
2009 dprintk(3, "successful\n");
2010 return 0;
2011 }
2012 EXPORT_SYMBOL_GPL(vb2_core_streamon);
2013
2014 void vb2_queue_error(struct vb2_queue *q)
2015 {
2016 q->error = 1;
2017
2018 wake_up_all(&q->done_wq);
2019 }
2020 EXPORT_SYMBOL_GPL(vb2_queue_error);
2021
2022 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
2023 {
2024 if (type != q->type) {
2025 dprintk(1, "invalid stream type\n");
2026 return -EINVAL;
2027 }
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038 __vb2_queue_cancel(q);
2039 q->waiting_for_buffers = !q->is_output;
2040 q->last_buffer_dequeued = false;
2041
2042 dprintk(3, "successful\n");
2043 return 0;
2044 }
2045 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
2046
2047
2048
2049
2050 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
2051 unsigned int *_buffer, unsigned int *_plane)
2052 {
2053 struct vb2_buffer *vb;
2054 unsigned int buffer, plane;
2055
2056
2057
2058
2059
2060
2061 for (buffer = 0; buffer < q->num_buffers; ++buffer) {
2062 vb = q->bufs[buffer];
2063
2064 for (plane = 0; plane < vb->num_planes; ++plane) {
2065 if (vb->planes[plane].m.offset == off) {
2066 *_buffer = buffer;
2067 *_plane = plane;
2068 return 0;
2069 }
2070 }
2071 }
2072
2073 return -EINVAL;
2074 }
2075
2076 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
2077 unsigned int index, unsigned int plane, unsigned int flags)
2078 {
2079 struct vb2_buffer *vb = NULL;
2080 struct vb2_plane *vb_plane;
2081 int ret;
2082 struct dma_buf *dbuf;
2083
2084 if (q->memory != VB2_MEMORY_MMAP) {
2085 dprintk(1, "queue is not currently set up for mmap\n");
2086 return -EINVAL;
2087 }
2088
2089 if (!q->mem_ops->get_dmabuf) {
2090 dprintk(1, "queue does not support DMA buffer exporting\n");
2091 return -EINVAL;
2092 }
2093
2094 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
2095 dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2096 return -EINVAL;
2097 }
2098
2099 if (type != q->type) {
2100 dprintk(1, "invalid buffer type\n");
2101 return -EINVAL;
2102 }
2103
2104 if (index >= q->num_buffers) {
2105 dprintk(1, "buffer index out of range\n");
2106 return -EINVAL;
2107 }
2108
2109 vb = q->bufs[index];
2110
2111 if (plane >= vb->num_planes) {
2112 dprintk(1, "buffer plane out of range\n");
2113 return -EINVAL;
2114 }
2115
2116 if (vb2_fileio_is_active(q)) {
2117 dprintk(1, "expbuf: file io in progress\n");
2118 return -EBUSY;
2119 }
2120
2121 vb_plane = &vb->planes[plane];
2122
2123 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
2124 flags & O_ACCMODE);
2125 if (IS_ERR_OR_NULL(dbuf)) {
2126 dprintk(1, "failed to export buffer %d, plane %d\n",
2127 index, plane);
2128 return -EINVAL;
2129 }
2130
2131 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
2132 if (ret < 0) {
2133 dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2134 index, plane, ret);
2135 dma_buf_put(dbuf);
2136 return ret;
2137 }
2138
2139 dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2140 index, plane, ret);
2141 *fd = ret;
2142
2143 return 0;
2144 }
2145 EXPORT_SYMBOL_GPL(vb2_core_expbuf);
2146
2147 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
2148 {
2149 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
2150 struct vb2_buffer *vb;
2151 unsigned int buffer = 0, plane = 0;
2152 int ret;
2153 unsigned long length;
2154
2155 if (q->memory != VB2_MEMORY_MMAP) {
2156 dprintk(1, "queue is not currently set up for mmap\n");
2157 return -EINVAL;
2158 }
2159
2160
2161
2162
2163 if (!(vma->vm_flags & VM_SHARED)) {
2164 dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2165 return -EINVAL;
2166 }
2167 if (q->is_output) {
2168 if (!(vma->vm_flags & VM_WRITE)) {
2169 dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2170 return -EINVAL;
2171 }
2172 } else {
2173 if (!(vma->vm_flags & VM_READ)) {
2174 dprintk(1, "invalid vma flags, VM_READ needed\n");
2175 return -EINVAL;
2176 }
2177 }
2178
2179 mutex_lock(&q->mmap_lock);
2180
2181 if (vb2_fileio_is_active(q)) {
2182 dprintk(1, "mmap: file io in progress\n");
2183 ret = -EBUSY;
2184 goto unlock;
2185 }
2186
2187
2188
2189
2190 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2191 if (ret)
2192 goto unlock;
2193
2194 vb = q->bufs[buffer];
2195
2196
2197
2198
2199
2200
2201 length = PAGE_ALIGN(vb->planes[plane].length);
2202 if (length < (vma->vm_end - vma->vm_start)) {
2203 dprintk(1,
2204 "MMAP invalid, as it would overflow buffer length\n");
2205 ret = -EINVAL;
2206 goto unlock;
2207 }
2208
2209
2210
2211
2212
2213
2214 vma->vm_pgoff = 0;
2215
2216 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
2217
2218 unlock:
2219 mutex_unlock(&q->mmap_lock);
2220 if (ret)
2221 return ret;
2222
2223 dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2224 return 0;
2225 }
2226 EXPORT_SYMBOL_GPL(vb2_mmap);
2227
2228 #ifndef CONFIG_MMU
2229 unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
2230 unsigned long addr,
2231 unsigned long len,
2232 unsigned long pgoff,
2233 unsigned long flags)
2234 {
2235 unsigned long off = pgoff << PAGE_SHIFT;
2236 struct vb2_buffer *vb;
2237 unsigned int buffer, plane;
2238 void *vaddr;
2239 int ret;
2240
2241 if (q->memory != VB2_MEMORY_MMAP) {
2242 dprintk(1, "queue is not currently set up for mmap\n");
2243 return -EINVAL;
2244 }
2245
2246
2247
2248
2249 ret = __find_plane_by_offset(q, off, &buffer, &plane);
2250 if (ret)
2251 return ret;
2252
2253 vb = q->bufs[buffer];
2254
2255 vaddr = vb2_plane_vaddr(vb, plane);
2256 return vaddr ? (unsigned long)vaddr : -EINVAL;
2257 }
2258 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
2259 #endif
2260
2261 int vb2_core_queue_init(struct vb2_queue *q)
2262 {
2263
2264
2265
2266 if (WARN_ON(!q) ||
2267 WARN_ON(!q->ops) ||
2268 WARN_ON(!q->mem_ops) ||
2269 WARN_ON(!q->type) ||
2270 WARN_ON(!q->io_modes) ||
2271 WARN_ON(!q->ops->queue_setup) ||
2272 WARN_ON(!q->ops->buf_queue))
2273 return -EINVAL;
2274
2275 if (WARN_ON(q->requires_requests && !q->supports_requests))
2276 return -EINVAL;
2277
2278 INIT_LIST_HEAD(&q->queued_list);
2279 INIT_LIST_HEAD(&q->done_list);
2280 spin_lock_init(&q->done_lock);
2281 mutex_init(&q->mmap_lock);
2282 init_waitqueue_head(&q->done_wq);
2283
2284 q->memory = VB2_MEMORY_UNKNOWN;
2285
2286 if (q->buf_struct_size == 0)
2287 q->buf_struct_size = sizeof(struct vb2_buffer);
2288
2289 if (q->bidirectional)
2290 q->dma_dir = DMA_BIDIRECTIONAL;
2291 else
2292 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2293
2294 return 0;
2295 }
2296 EXPORT_SYMBOL_GPL(vb2_core_queue_init);
2297
2298 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2299 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2300 void vb2_core_queue_release(struct vb2_queue *q)
2301 {
2302 __vb2_cleanup_fileio(q);
2303 __vb2_queue_cancel(q);
2304 mutex_lock(&q->mmap_lock);
2305 __vb2_queue_free(q, q->num_buffers);
2306 mutex_unlock(&q->mmap_lock);
2307 }
2308 EXPORT_SYMBOL_GPL(vb2_core_queue_release);
2309
2310 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file,
2311 poll_table *wait)
2312 {
2313 __poll_t req_events = poll_requested_events(wait);
2314 struct vb2_buffer *vb = NULL;
2315 unsigned long flags;
2316
2317 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM)))
2318 return 0;
2319 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
2320 return 0;
2321
2322 poll_wait(file, &q->done_wq, wait);
2323
2324
2325
2326
2327 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
2328 if (!q->is_output && (q->io_modes & VB2_READ) &&
2329 (req_events & (EPOLLIN | EPOLLRDNORM))) {
2330 if (__vb2_init_fileio(q, 1))
2331 return EPOLLERR;
2332 }
2333 if (q->is_output && (q->io_modes & VB2_WRITE) &&
2334 (req_events & (EPOLLOUT | EPOLLWRNORM))) {
2335 if (__vb2_init_fileio(q, 0))
2336 return EPOLLERR;
2337
2338
2339
2340 return EPOLLOUT | EPOLLWRNORM;
2341 }
2342 }
2343
2344
2345
2346
2347
2348 if (!vb2_is_streaming(q) || q->error)
2349 return EPOLLERR;
2350
2351
2352
2353
2354
2355
2356
2357 if (q->quirk_poll_must_check_waiting_for_buffers &&
2358 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM)))
2359 return EPOLLERR;
2360
2361
2362
2363
2364
2365 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
2366 return EPOLLOUT | EPOLLWRNORM;
2367
2368 if (list_empty(&q->done_list)) {
2369
2370
2371
2372
2373 if (q->last_buffer_dequeued)
2374 return EPOLLIN | EPOLLRDNORM;
2375 }
2376
2377
2378
2379
2380 spin_lock_irqsave(&q->done_lock, flags);
2381 if (!list_empty(&q->done_list))
2382 vb = list_first_entry(&q->done_list, struct vb2_buffer,
2383 done_entry);
2384 spin_unlock_irqrestore(&q->done_lock, flags);
2385
2386 if (vb && (vb->state == VB2_BUF_STATE_DONE
2387 || vb->state == VB2_BUF_STATE_ERROR)) {
2388 return (q->is_output) ?
2389 EPOLLOUT | EPOLLWRNORM :
2390 EPOLLIN | EPOLLRDNORM;
2391 }
2392 return 0;
2393 }
2394 EXPORT_SYMBOL_GPL(vb2_core_poll);
2395
2396
2397
2398
2399
2400
2401
2402
2403 struct vb2_fileio_buf {
2404 void *vaddr;
2405 unsigned int size;
2406 unsigned int pos;
2407 unsigned int queued:1;
2408 };
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434 struct vb2_fileio_data {
2435 unsigned int count;
2436 unsigned int type;
2437 unsigned int memory;
2438 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
2439 unsigned int cur_index;
2440 unsigned int initial_index;
2441 unsigned int q_count;
2442 unsigned int dq_count;
2443 unsigned read_once:1;
2444 unsigned write_immediately:1;
2445 };
2446
2447
2448
2449
2450
2451
2452 static int __vb2_init_fileio(struct vb2_queue *q, int read)
2453 {
2454 struct vb2_fileio_data *fileio;
2455 int i, ret;
2456 unsigned int count = 0;
2457
2458
2459
2460
2461 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
2462 (!read && !(q->io_modes & VB2_WRITE))))
2463 return -EINVAL;
2464
2465
2466
2467
2468 if (!q->mem_ops->vaddr)
2469 return -EBUSY;
2470
2471
2472
2473
2474 if (q->streaming || q->num_buffers > 0)
2475 return -EBUSY;
2476
2477
2478
2479
2480 count = 1;
2481
2482 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2483 (read) ? "read" : "write", count, q->fileio_read_once,
2484 q->fileio_write_immediately);
2485
2486 fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
2487 if (fileio == NULL)
2488 return -ENOMEM;
2489
2490 fileio->read_once = q->fileio_read_once;
2491 fileio->write_immediately = q->fileio_write_immediately;
2492
2493
2494
2495
2496
2497 fileio->count = count;
2498 fileio->memory = VB2_MEMORY_MMAP;
2499 fileio->type = q->type;
2500 q->fileio = fileio;
2501 ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2502 if (ret)
2503 goto err_kfree;
2504
2505
2506
2507
2508
2509 if (q->bufs[0]->num_planes != 1) {
2510 ret = -EBUSY;
2511 goto err_reqbufs;
2512 }
2513
2514
2515
2516
2517 for (i = 0; i < q->num_buffers; i++) {
2518 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
2519 if (fileio->bufs[i].vaddr == NULL) {
2520 ret = -EINVAL;
2521 goto err_reqbufs;
2522 }
2523 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
2524 }
2525
2526
2527
2528
2529 if (read) {
2530
2531
2532
2533 for (i = 0; i < q->num_buffers; i++) {
2534 ret = vb2_core_qbuf(q, i, NULL, NULL);
2535 if (ret)
2536 goto err_reqbufs;
2537 fileio->bufs[i].queued = 1;
2538 }
2539
2540
2541
2542
2543 fileio->initial_index = q->num_buffers;
2544 fileio->cur_index = q->num_buffers;
2545 }
2546
2547
2548
2549
2550 ret = vb2_core_streamon(q, q->type);
2551 if (ret)
2552 goto err_reqbufs;
2553
2554 return ret;
2555
2556 err_reqbufs:
2557 fileio->count = 0;
2558 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2559
2560 err_kfree:
2561 q->fileio = NULL;
2562 kfree(fileio);
2563 return ret;
2564 }
2565
2566
2567
2568
2569
2570 static int __vb2_cleanup_fileio(struct vb2_queue *q)
2571 {
2572 struct vb2_fileio_data *fileio = q->fileio;
2573
2574 if (fileio) {
2575 vb2_core_streamoff(q, q->type);
2576 q->fileio = NULL;
2577 fileio->count = 0;
2578 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2579 kfree(fileio);
2580 dprintk(3, "file io emulator closed\n");
2581 }
2582 return 0;
2583 }
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
2595 loff_t *ppos, int nonblock, int read)
2596 {
2597 struct vb2_fileio_data *fileio;
2598 struct vb2_fileio_buf *buf;
2599 bool is_multiplanar = q->is_multiplanar;
2600
2601
2602
2603
2604
2605 bool copy_timestamp = !read && q->copy_timestamp;
2606 unsigned index;
2607 int ret;
2608
2609 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2610 read ? "read" : "write", (long)*ppos, count,
2611 nonblock ? "non" : "");
2612
2613 if (!data)
2614 return -EINVAL;
2615
2616 if (q->waiting_in_dqbuf) {
2617 dprintk(3, "another dup()ped fd is %s\n",
2618 read ? "reading" : "writing");
2619 return -EBUSY;
2620 }
2621
2622
2623
2624
2625 if (!vb2_fileio_is_active(q)) {
2626 ret = __vb2_init_fileio(q, read);
2627 dprintk(3, "vb2_init_fileio result: %d\n", ret);
2628 if (ret)
2629 return ret;
2630 }
2631 fileio = q->fileio;
2632
2633
2634
2635
2636 index = fileio->cur_index;
2637 if (index >= q->num_buffers) {
2638 struct vb2_buffer *b;
2639
2640
2641
2642
2643 ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2644 dprintk(5, "vb2_dqbuf result: %d\n", ret);
2645 if (ret)
2646 return ret;
2647 fileio->dq_count += 1;
2648
2649 fileio->cur_index = index;
2650 buf = &fileio->bufs[index];
2651 b = q->bufs[index];
2652
2653
2654
2655
2656 buf->pos = 0;
2657 buf->queued = 0;
2658 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
2659 : vb2_plane_size(q->bufs[index], 0);
2660
2661 if (is_multiplanar && read &&
2662 b->planes[0].data_offset < buf->size) {
2663 buf->pos = b->planes[0].data_offset;
2664 buf->size -= buf->pos;
2665 }
2666 } else {
2667 buf = &fileio->bufs[index];
2668 }
2669
2670
2671
2672
2673 if (buf->pos + count > buf->size) {
2674 count = buf->size - buf->pos;
2675 dprintk(5, "reducing read count: %zd\n", count);
2676 }
2677
2678
2679
2680
2681 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2682 count, index, buf->pos);
2683 if (read)
2684 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
2685 else
2686 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
2687 if (ret) {
2688 dprintk(3, "error copying data\n");
2689 return -EFAULT;
2690 }
2691
2692
2693
2694
2695 buf->pos += count;
2696 *ppos += count;
2697
2698
2699
2700
2701 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
2702 struct vb2_buffer *b = q->bufs[index];
2703
2704
2705
2706
2707 if (read && fileio->read_once && fileio->dq_count == 1) {
2708 dprintk(3, "read limit reached\n");
2709 return __vb2_cleanup_fileio(q);
2710 }
2711
2712
2713
2714
2715 b->planes[0].bytesused = buf->pos;
2716
2717 if (copy_timestamp)
2718 b->timestamp = ktime_get_ns();
2719 ret = vb2_core_qbuf(q, index, NULL, NULL);
2720 dprintk(5, "vb2_dbuf result: %d\n", ret);
2721 if (ret)
2722 return ret;
2723
2724
2725
2726
2727 buf->pos = 0;
2728 buf->queued = 1;
2729 buf->size = vb2_plane_size(q->bufs[index], 0);
2730 fileio->q_count += 1;
2731
2732
2733
2734
2735 if (fileio->initial_index < q->num_buffers)
2736 fileio->initial_index++;
2737
2738
2739
2740
2741
2742
2743
2744 fileio->cur_index = fileio->initial_index;
2745 }
2746
2747
2748
2749
2750 if (ret == 0)
2751 ret = count;
2752 return ret;
2753 }
2754
2755 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
2756 loff_t *ppos, int nonblocking)
2757 {
2758 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
2759 }
2760 EXPORT_SYMBOL_GPL(vb2_read);
2761
2762 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
2763 loff_t *ppos, int nonblocking)
2764 {
2765 return __vb2_perform_fileio(q, (char __user *) data, count,
2766 ppos, nonblocking, 0);
2767 }
2768 EXPORT_SYMBOL_GPL(vb2_write);
2769
2770 struct vb2_threadio_data {
2771 struct task_struct *thread;
2772 vb2_thread_fnc fnc;
2773 void *priv;
2774 bool stop;
2775 };
2776
2777 static int vb2_thread(void *data)
2778 {
2779 struct vb2_queue *q = data;
2780 struct vb2_threadio_data *threadio = q->threadio;
2781 bool copy_timestamp = false;
2782 unsigned prequeue = 0;
2783 unsigned index = 0;
2784 int ret = 0;
2785
2786 if (q->is_output) {
2787 prequeue = q->num_buffers;
2788 copy_timestamp = q->copy_timestamp;
2789 }
2790
2791 set_freezable();
2792
2793 for (;;) {
2794 struct vb2_buffer *vb;
2795
2796
2797
2798
2799 if (prequeue) {
2800 vb = q->bufs[index++];
2801 prequeue--;
2802 } else {
2803 call_void_qop(q, wait_finish, q);
2804 if (!threadio->stop)
2805 ret = vb2_core_dqbuf(q, &index, NULL, 0);
2806 call_void_qop(q, wait_prepare, q);
2807 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2808 if (!ret)
2809 vb = q->bufs[index];
2810 }
2811 if (ret || threadio->stop)
2812 break;
2813 try_to_freeze();
2814
2815 if (vb->state != VB2_BUF_STATE_ERROR)
2816 if (threadio->fnc(vb, threadio->priv))
2817 break;
2818 call_void_qop(q, wait_finish, q);
2819 if (copy_timestamp)
2820 vb->timestamp = ktime_get_ns();
2821 if (!threadio->stop)
2822 ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
2823 call_void_qop(q, wait_prepare, q);
2824 if (ret || threadio->stop)
2825 break;
2826 }
2827
2828
2829 while (!kthread_should_stop()) {
2830 set_current_state(TASK_INTERRUPTIBLE);
2831 schedule();
2832 }
2833 return 0;
2834 }
2835
2836
2837
2838
2839
2840
2841 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
2842 const char *thread_name)
2843 {
2844 struct vb2_threadio_data *threadio;
2845 int ret = 0;
2846
2847 if (q->threadio)
2848 return -EBUSY;
2849 if (vb2_is_busy(q))
2850 return -EBUSY;
2851 if (WARN_ON(q->fileio))
2852 return -EBUSY;
2853
2854 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
2855 if (threadio == NULL)
2856 return -ENOMEM;
2857 threadio->fnc = fnc;
2858 threadio->priv = priv;
2859
2860 ret = __vb2_init_fileio(q, !q->is_output);
2861 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2862 if (ret)
2863 goto nomem;
2864 q->threadio = threadio;
2865 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
2866 if (IS_ERR(threadio->thread)) {
2867 ret = PTR_ERR(threadio->thread);
2868 threadio->thread = NULL;
2869 goto nothread;
2870 }
2871 return 0;
2872
2873 nothread:
2874 __vb2_cleanup_fileio(q);
2875 nomem:
2876 kfree(threadio);
2877 return ret;
2878 }
2879 EXPORT_SYMBOL_GPL(vb2_thread_start);
2880
2881 int vb2_thread_stop(struct vb2_queue *q)
2882 {
2883 struct vb2_threadio_data *threadio = q->threadio;
2884 int err;
2885
2886 if (threadio == NULL)
2887 return 0;
2888 threadio->stop = true;
2889
2890 vb2_queue_error(q);
2891 err = kthread_stop(threadio->thread);
2892 __vb2_cleanup_fileio(q);
2893 threadio->thread = NULL;
2894 kfree(threadio);
2895 q->threadio = NULL;
2896 return err;
2897 }
2898 EXPORT_SYMBOL_GPL(vb2_thread_stop);
2899
2900 MODULE_DESCRIPTION("Media buffer core framework");
2901 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
2902 MODULE_LICENSE("GPL");