Lines Matching refs:q

37 #define dprintk(q, level, fmt, arg...)					\  argument
40 pr_info("[%s] %s: " fmt, (q)->name, __func__, \
93 #define log_qop(q, op) \ argument
94 dprintk(q, 2, "call_qop(%s)%s\n", #op, \
95 (q)->ops->op ? "" : " (nop)")
97 #define call_qop(q, op, args...) \ argument
101 log_qop(q, op); \
102 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
104 (q)->cnt_ ## op++; \
108 #define call_void_qop(q, op, args...) \ argument
110 log_qop(q, op); \
111 if ((q)->ops->op) \
112 (q)->ops->op(args); \
113 (q)->cnt_ ## op++; \
157 #define call_qop(q, op, args...) \ argument
158 ((q)->ops->op ? (q)->ops->op(args) : 0)
160 #define call_void_qop(q, op, args...) \ argument
162 if ((q)->ops->op) \
163 (q)->ops->op(args); \
177 #define call_bufop(q, op, args...) \ argument
180 if (q && q->buf_ops && q->buf_ops->op) \
181 ret = q->buf_ops->op(args); \
185 #define call_void_bufop(q, op, args...) \ argument
187 if (q && q->buf_ops && q->buf_ops->op) \
188 q->buf_ops->op(args); \
191 static void __vb2_queue_cancel(struct vb2_queue *q);
216 struct vb2_queue *q = vb->vb2_queue; in __vb2_buf_mem_alloc() local
235 q->alloc_devs[plane] ? : q->dev, in __vb2_buf_mem_alloc()
357 struct vb2_queue *q = vb->vb2_queue; in __setup_offsets() local
362 struct vb2_buffer *prev = q->bufs[vb->index - 1]; in __setup_offsets()
371 dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n", in __setup_offsets()
379 static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) in init_buffer_cache_hints() argument
386 if (q->memory == VB2_MEMORY_DMABUF) { in init_buffer_cache_hints()
396 if (q->dma_dir == DMA_TO_DEVICE) in init_buffer_cache_hints()
407 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, in __vb2_queue_alloc() argument
417 VB2_MAX_FRAME - q->num_buffers); in __vb2_queue_alloc()
421 vb = kzalloc(q->buf_struct_size, GFP_KERNEL); in __vb2_queue_alloc()
423 dprintk(q, 1, "memory alloc for buffer struct failed\n"); in __vb2_queue_alloc()
428 vb->vb2_queue = q; in __vb2_queue_alloc()
430 vb->index = q->num_buffers + buffer; in __vb2_queue_alloc()
431 vb->type = q->type; in __vb2_queue_alloc()
433 init_buffer_cache_hints(q, vb); in __vb2_queue_alloc()
438 call_void_bufop(q, init_buffer, vb); in __vb2_queue_alloc()
440 q->bufs[vb->index] = vb; in __vb2_queue_alloc()
446 dprintk(q, 1, "failed allocating memory for buffer %d\n", in __vb2_queue_alloc()
448 q->bufs[vb->index] = NULL; in __vb2_queue_alloc()
460 dprintk(q, 1, "buffer %d %p initialization failed\n", in __vb2_queue_alloc()
463 q->bufs[vb->index] = NULL; in __vb2_queue_alloc()
470 dprintk(q, 3, "allocated %d buffers, %d plane(s) each\n", in __vb2_queue_alloc()
479 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) in __vb2_free_mem() argument
484 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_free_mem()
486 vb = q->bufs[buffer]; in __vb2_free_mem()
491 if (q->memory == VB2_MEMORY_MMAP) in __vb2_free_mem()
493 else if (q->memory == VB2_MEMORY_DMABUF) in __vb2_free_mem()
505 static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) in __vb2_queue_free() argument
517 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_queue_free()
519 if (q->bufs[buffer] == NULL) in __vb2_queue_free()
521 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) { in __vb2_queue_free()
522 dprintk(q, 1, "preparing buffers, cannot free\n"); in __vb2_queue_free()
528 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_queue_free()
530 struct vb2_buffer *vb = q->bufs[buffer]; in __vb2_queue_free()
537 __vb2_free_mem(q, buffers); in __vb2_queue_free()
545 if (q->num_buffers) { in __vb2_queue_free()
546 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || in __vb2_queue_free()
547 q->cnt_wait_prepare != q->cnt_wait_finish; in __vb2_queue_free()
550 pr_info("counters for queue %p:%s\n", q, in __vb2_queue_free()
553 q->cnt_queue_setup, q->cnt_start_streaming, in __vb2_queue_free()
554 q->cnt_stop_streaming); in __vb2_queue_free()
556 q->cnt_wait_prepare, q->cnt_wait_finish); in __vb2_queue_free()
558 q->cnt_queue_setup = 0; in __vb2_queue_free()
559 q->cnt_wait_prepare = 0; in __vb2_queue_free()
560 q->cnt_wait_finish = 0; in __vb2_queue_free()
561 q->cnt_start_streaming = 0; in __vb2_queue_free()
562 q->cnt_stop_streaming = 0; in __vb2_queue_free()
564 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __vb2_queue_free()
565 struct vb2_buffer *vb = q->bufs[buffer]; in __vb2_queue_free()
577 q, buffer, unbalanced ? " UNBALANCED!" : ""); in __vb2_queue_free()
603 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_queue_free()
605 kfree(q->bufs[buffer]); in __vb2_queue_free()
606 q->bufs[buffer] = NULL; in __vb2_queue_free()
609 q->num_buffers -= buffers; in __vb2_queue_free()
610 if (!q->num_buffers) { in __vb2_queue_free()
611 q->memory = VB2_MEMORY_UNKNOWN; in __vb2_queue_free()
612 INIT_LIST_HEAD(&q->queued_list); in __vb2_queue_free()
617 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) in vb2_buffer_in_use() argument
639 static bool __buffers_in_use(struct vb2_queue *q) in __buffers_in_use() argument
642 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __buffers_in_use()
643 if (vb2_buffer_in_use(q, q->bufs[buffer])) in __buffers_in_use()
649 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb) in vb2_core_querybuf() argument
651 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb); in vb2_core_querybuf()
659 static int __verify_userptr_ops(struct vb2_queue *q) in __verify_userptr_ops() argument
661 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || in __verify_userptr_ops()
662 !q->mem_ops->put_userptr) in __verify_userptr_ops()
672 static int __verify_mmap_ops(struct vb2_queue *q) in __verify_mmap_ops() argument
674 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || in __verify_mmap_ops()
675 !q->mem_ops->put || !q->mem_ops->mmap) in __verify_mmap_ops()
685 static int __verify_dmabuf_ops(struct vb2_queue *q) in __verify_dmabuf_ops() argument
687 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf || in __verify_dmabuf_ops()
688 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf || in __verify_dmabuf_ops()
689 !q->mem_ops->unmap_dmabuf) in __verify_dmabuf_ops()
695 int vb2_verify_memory_type(struct vb2_queue *q, in vb2_verify_memory_type() argument
700 dprintk(q, 1, "unsupported memory type\n"); in vb2_verify_memory_type()
704 if (type != q->type) { in vb2_verify_memory_type()
705 dprintk(q, 1, "requested type is incorrect\n"); in vb2_verify_memory_type()
713 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) { in vb2_verify_memory_type()
714 dprintk(q, 1, "MMAP for current setup unsupported\n"); in vb2_verify_memory_type()
718 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) { in vb2_verify_memory_type()
719 dprintk(q, 1, "USERPTR for current setup unsupported\n"); in vb2_verify_memory_type()
723 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) { in vb2_verify_memory_type()
724 dprintk(q, 1, "DMABUF for current setup unsupported\n"); in vb2_verify_memory_type()
733 if (vb2_fileio_is_active(q)) { in vb2_verify_memory_type()
734 dprintk(q, 1, "file io in progress\n"); in vb2_verify_memory_type()
741 static void set_queue_coherency(struct vb2_queue *q, bool non_coherent_mem) in set_queue_coherency() argument
743 q->non_coherent_mem = 0; in set_queue_coherency()
745 if (!vb2_queue_allows_cache_hints(q)) in set_queue_coherency()
747 q->non_coherent_mem = non_coherent_mem; in set_queue_coherency()
750 static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem) in verify_coherency_flags() argument
752 if (non_coherent_mem != q->non_coherent_mem) { in verify_coherency_flags()
753 dprintk(q, 1, "memory coherency model mismatch\n"); in verify_coherency_flags()
759 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, in vb2_core_reqbufs() argument
768 if (q->streaming) { in vb2_core_reqbufs()
769 dprintk(q, 1, "streaming active\n"); in vb2_core_reqbufs()
773 if (q->waiting_in_dqbuf && *count) { in vb2_core_reqbufs()
774 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in vb2_core_reqbufs()
778 if (*count == 0 || q->num_buffers != 0 || in vb2_core_reqbufs()
779 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) || in vb2_core_reqbufs()
780 !verify_coherency_flags(q, non_coherent_mem)) { in vb2_core_reqbufs()
785 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
786 if (debug && q->memory == VB2_MEMORY_MMAP && in vb2_core_reqbufs()
787 __buffers_in_use(q)) in vb2_core_reqbufs()
788 dprintk(q, 1, "memory in use, orphaning buffers\n"); in vb2_core_reqbufs()
795 __vb2_queue_cancel(q); in vb2_core_reqbufs()
796 ret = __vb2_queue_free(q, q->num_buffers); in vb2_core_reqbufs()
797 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
812 WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME); in vb2_core_reqbufs()
813 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed); in vb2_core_reqbufs()
815 memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); in vb2_core_reqbufs()
816 q->memory = memory; in vb2_core_reqbufs()
817 set_queue_coherency(q, non_coherent_mem); in vb2_core_reqbufs()
823 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, in vb2_core_reqbufs()
824 plane_sizes, q->alloc_devs); in vb2_core_reqbufs()
838 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); in vb2_core_reqbufs()
840 dprintk(q, 1, "memory allocation failed\n"); in vb2_core_reqbufs()
848 if (allocated_buffers < q->min_buffers_needed) in vb2_core_reqbufs()
864 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_reqbufs()
865 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_reqbufs()
876 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
877 q->num_buffers = allocated_buffers; in vb2_core_reqbufs()
884 __vb2_queue_free(q, allocated_buffers); in vb2_core_reqbufs()
885 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
888 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
895 q->waiting_for_buffers = !q->is_output; in vb2_core_reqbufs()
901 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, in vb2_core_create_bufs() argument
911 if (q->num_buffers == VB2_MAX_FRAME) { in vb2_core_create_bufs()
912 dprintk(q, 1, "maximum number of buffers already allocated\n"); in vb2_core_create_bufs()
916 if (!q->num_buffers) { in vb2_core_create_bufs()
917 if (q->waiting_in_dqbuf && *count) { in vb2_core_create_bufs()
918 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in vb2_core_create_bufs()
921 memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); in vb2_core_create_bufs()
922 q->memory = memory; in vb2_core_create_bufs()
923 q->waiting_for_buffers = !q->is_output; in vb2_core_create_bufs()
924 set_queue_coherency(q, non_coherent_mem); in vb2_core_create_bufs()
926 if (q->memory != memory) { in vb2_core_create_bufs()
927 dprintk(q, 1, "memory model mismatch\n"); in vb2_core_create_bufs()
930 if (!verify_coherency_flags(q, non_coherent_mem)) in vb2_core_create_bufs()
934 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); in vb2_core_create_bufs()
945 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_create_bufs()
946 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_create_bufs()
951 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers, in vb2_core_create_bufs()
954 dprintk(q, 1, "memory allocation failed\n"); in vb2_core_create_bufs()
968 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_create_bufs()
969 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_create_bufs()
980 mutex_lock(&q->mmap_lock); in vb2_core_create_bufs()
981 q->num_buffers += allocated_buffers; in vb2_core_create_bufs()
988 __vb2_queue_free(q, allocated_buffers); in vb2_core_create_bufs()
989 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
992 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
1025 struct vb2_queue *q = vb->vb2_queue; in vb2_buffer_done() local
1043 dprintk(q, 4, "done processing on buffer %d, state: %s\n", in vb2_buffer_done()
1049 spin_lock_irqsave(&q->done_lock, flags); in vb2_buffer_done()
1054 list_add_tail(&vb->done_entry, &q->done_list); in vb2_buffer_done()
1057 atomic_dec(&q->owned_by_drv_count); in vb2_buffer_done()
1064 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_buffer_done()
1066 trace_vb2_buf_done(q, vb); in vb2_buffer_done()
1073 wake_up(&q->done_wq); in vb2_buffer_done()
1079 void vb2_discard_done(struct vb2_queue *q) in vb2_discard_done() argument
1084 spin_lock_irqsave(&q->done_lock, flags); in vb2_discard_done()
1085 list_for_each_entry(vb, &q->done_list, done_entry) in vb2_discard_done()
1087 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_discard_done()
1109 struct vb2_queue *q = vb->vb2_queue; in __prepare_userptr() local
1129 dprintk(q, 3, "userspace address for plane %d changed, reacquiring memory\n", in __prepare_userptr()
1134 dprintk(q, 1, "provided buffer size %u is less than setup size %u for plane %d\n", in __prepare_userptr()
1161 q->alloc_devs[plane] ? : q->dev, in __prepare_userptr()
1165 dprintk(q, 1, "failed acquiring userspace memory for plane %d\n", in __prepare_userptr()
1192 dprintk(q, 1, "buffer initialization failed\n"); in __prepare_userptr()
1199 dprintk(q, 1, "buffer preparation failed\n"); in __prepare_userptr()
1225 struct vb2_queue *q = vb->vb2_queue; in __prepare_dmabuf() local
1242 dprintk(q, 1, "invalid dmabuf fd for plane %d\n", in __prepare_dmabuf()
1253 dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n", in __prepare_dmabuf()
1268 dprintk(q, 3, "buffer for plane %d changed\n", plane); in __prepare_dmabuf()
1286 q->alloc_devs[plane] ? : q->dev, in __prepare_dmabuf()
1290 dprintk(q, 1, "failed to attach dmabuf\n"); in __prepare_dmabuf()
1311 dprintk(q, 1, "failed to map dmabuf for plane %d\n", in __prepare_dmabuf()
1336 dprintk(q, 1, "buffer initialization failed\n"); in __prepare_dmabuf()
1343 dprintk(q, 1, "buffer preparation failed\n"); in __prepare_dmabuf()
1361 struct vb2_queue *q = vb->vb2_queue; in __enqueue_in_driver() local
1364 atomic_inc(&q->owned_by_drv_count); in __enqueue_in_driver()
1366 trace_vb2_buf_queue(q, vb); in __enqueue_in_driver()
1373 struct vb2_queue *q = vb->vb2_queue; in __buf_prepare() local
1377 if (q->error) { in __buf_prepare()
1378 dprintk(q, 1, "fatal error occurred on queue\n"); in __buf_prepare()
1386 if (q->is_output) { in __buf_prepare()
1389 dprintk(q, 1, "buffer validation failed\n"); in __buf_prepare()
1396 switch (q->memory) { in __buf_prepare()
1413 dprintk(q, 1, "buffer preparation failed: %d\n", ret); in __buf_prepare()
1452 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1524 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb) in vb2_core_prepare_buf() argument
1529 vb = q->bufs[index]; in vb2_core_prepare_buf()
1531 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_prepare_buf()
1536 dprintk(q, 1, "buffer already prepared\n"); in vb2_core_prepare_buf()
1545 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_prepare_buf()
1547 dprintk(q, 2, "prepare of buffer %d succeeded\n", vb->index); in vb2_core_prepare_buf()
1564 static int vb2_start_streaming(struct vb2_queue *q) in vb2_start_streaming() argument
1573 list_for_each_entry(vb, &q->queued_list, queued_entry) in vb2_start_streaming()
1577 q->start_streaming_called = 1; in vb2_start_streaming()
1578 ret = call_qop(q, start_streaming, q, in vb2_start_streaming()
1579 atomic_read(&q->owned_by_drv_count)); in vb2_start_streaming()
1583 q->start_streaming_called = 0; in vb2_start_streaming()
1585 dprintk(q, 1, "driver refused to start streaming\n"); in vb2_start_streaming()
1592 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { in vb2_start_streaming()
1599 for (i = 0; i < q->num_buffers; ++i) { in vb2_start_streaming()
1600 vb = q->bufs[i]; in vb2_start_streaming()
1605 WARN_ON(atomic_read(&q->owned_by_drv_count)); in vb2_start_streaming()
1612 WARN_ON(!list_empty(&q->done_list)); in vb2_start_streaming()
1616 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb, in vb2_core_qbuf() argument
1623 if (q->error) { in vb2_core_qbuf()
1624 dprintk(q, 1, "fatal error occurred on queue\n"); in vb2_core_qbuf()
1628 vb = q->bufs[index]; in vb2_core_qbuf()
1631 q->requires_requests) { in vb2_core_qbuf()
1632 dprintk(q, 1, "qbuf requires a request\n"); in vb2_core_qbuf()
1636 if ((req && q->uses_qbuf) || in vb2_core_qbuf()
1638 q->uses_requests)) { in vb2_core_qbuf()
1639 dprintk(q, 1, "queue in wrong mode (qbuf vs requests)\n"); in vb2_core_qbuf()
1646 q->uses_requests = 1; in vb2_core_qbuf()
1648 dprintk(q, 1, "buffer %d not in dequeued state\n", in vb2_core_qbuf()
1653 if (q->is_output && !vb->prepared) { in vb2_core_qbuf()
1656 dprintk(q, 1, "buffer validation failed\n"); in vb2_core_qbuf()
1668 q, true, &vb->req_obj); in vb2_core_qbuf()
1688 call_void_bufop(q, copy_timestamp, vb, pb); in vb2_core_qbuf()
1689 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_qbuf()
1692 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index); in vb2_core_qbuf()
1697 q->uses_qbuf = 1; in vb2_core_qbuf()
1709 dprintk(q, 1, "buffer still being prepared\n"); in vb2_core_qbuf()
1712 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_qbuf()
1722 list_add_tail(&vb->queued_entry, &q->queued_list); in vb2_core_qbuf()
1723 q->queued_count++; in vb2_core_qbuf()
1724 q->waiting_for_buffers = false; in vb2_core_qbuf()
1728 call_void_bufop(q, copy_timestamp, vb, pb); in vb2_core_qbuf()
1730 trace_vb2_qbuf(q, vb); in vb2_core_qbuf()
1736 if (q->start_streaming_called) in vb2_core_qbuf()
1741 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_qbuf()
1749 if (q->streaming && !q->start_streaming_called && in vb2_core_qbuf()
1750 q->queued_count >= q->min_buffers_needed) { in vb2_core_qbuf()
1751 ret = vb2_start_streaming(q); in vb2_core_qbuf()
1759 q->queued_count--; in vb2_core_qbuf()
1765 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index); in vb2_core_qbuf()
1776 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) in __vb2_wait_for_done_vb() argument
1790 if (q->waiting_in_dqbuf) { in __vb2_wait_for_done_vb()
1791 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in __vb2_wait_for_done_vb()
1795 if (!q->streaming) { in __vb2_wait_for_done_vb()
1796 dprintk(q, 1, "streaming off, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1800 if (q->error) { in __vb2_wait_for_done_vb()
1801 dprintk(q, 1, "Queue in error state, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1805 if (q->last_buffer_dequeued) { in __vb2_wait_for_done_vb()
1806 dprintk(q, 3, "last buffer dequeued already, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1810 if (!list_empty(&q->done_list)) { in __vb2_wait_for_done_vb()
1818 dprintk(q, 3, "nonblocking and no buffers to dequeue, will not wait\n"); in __vb2_wait_for_done_vb()
1822 q->waiting_in_dqbuf = 1; in __vb2_wait_for_done_vb()
1828 call_void_qop(q, wait_prepare, q); in __vb2_wait_for_done_vb()
1833 dprintk(q, 3, "will sleep waiting for buffers\n"); in __vb2_wait_for_done_vb()
1834 ret = wait_event_interruptible(q->done_wq, in __vb2_wait_for_done_vb()
1835 !list_empty(&q->done_list) || !q->streaming || in __vb2_wait_for_done_vb()
1836 q->error); in __vb2_wait_for_done_vb()
1842 call_void_qop(q, wait_finish, q); in __vb2_wait_for_done_vb()
1843 q->waiting_in_dqbuf = 0; in __vb2_wait_for_done_vb()
1845 dprintk(q, 1, "sleep was interrupted\n"); in __vb2_wait_for_done_vb()
1857 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, in __vb2_get_done_vb() argument
1866 ret = __vb2_wait_for_done_vb(q, nonblocking); in __vb2_get_done_vb()
1874 spin_lock_irqsave(&q->done_lock, flags); in __vb2_get_done_vb()
1875 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); in __vb2_get_done_vb()
1882 ret = call_bufop(q, verify_planes_array, *vb, pb); in __vb2_get_done_vb()
1885 spin_unlock_irqrestore(&q->done_lock, flags); in __vb2_get_done_vb()
1890 int vb2_wait_for_all_buffers(struct vb2_queue *q) in vb2_wait_for_all_buffers() argument
1892 if (!q->streaming) { in vb2_wait_for_all_buffers()
1893 dprintk(q, 1, "streaming off, will not wait for buffers\n"); in vb2_wait_for_all_buffers()
1897 if (q->start_streaming_called) in vb2_wait_for_all_buffers()
1898 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count)); in vb2_wait_for_all_buffers()
1908 struct vb2_queue *q = vb->vb2_queue; in __vb2_dqbuf() local
1916 call_void_bufop(q, init_buffer, vb); in __vb2_dqbuf()
1919 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb, in vb2_core_dqbuf() argument
1925 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking); in vb2_core_dqbuf()
1931 dprintk(q, 3, "returning done buffer\n"); in vb2_core_dqbuf()
1934 dprintk(q, 3, "returning done buffer with errors\n"); in vb2_core_dqbuf()
1937 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_dqbuf()
1950 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_dqbuf()
1954 q->queued_count--; in vb2_core_dqbuf()
1956 trace_vb2_dqbuf(q, vb); in vb2_core_dqbuf()
1969 dprintk(q, 2, "dqbuf of buffer %d, state: %s\n", in vb2_core_dqbuf()
1983 static void __vb2_queue_cancel(struct vb2_queue *q) in __vb2_queue_cancel() argument
1991 if (q->start_streaming_called) in __vb2_queue_cancel()
1992 call_void_qop(q, stop_streaming, q); in __vb2_queue_cancel()
2000 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { in __vb2_queue_cancel()
2001 for (i = 0; i < q->num_buffers; ++i) in __vb2_queue_cancel()
2002 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) { in __vb2_queue_cancel()
2004 q->bufs[i]); in __vb2_queue_cancel()
2005 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR); in __vb2_queue_cancel()
2008 WARN_ON(atomic_read(&q->owned_by_drv_count)); in __vb2_queue_cancel()
2011 q->streaming = 0; in __vb2_queue_cancel()
2012 q->start_streaming_called = 0; in __vb2_queue_cancel()
2013 q->queued_count = 0; in __vb2_queue_cancel()
2014 q->error = 0; in __vb2_queue_cancel()
2015 q->uses_requests = 0; in __vb2_queue_cancel()
2016 q->uses_qbuf = 0; in __vb2_queue_cancel()
2021 INIT_LIST_HEAD(&q->queued_list); in __vb2_queue_cancel()
2026 INIT_LIST_HEAD(&q->done_list); in __vb2_queue_cancel()
2027 atomic_set(&q->owned_by_drv_count, 0); in __vb2_queue_cancel()
2028 wake_up_all(&q->done_wq); in __vb2_queue_cancel()
2039 for (i = 0; i < q->num_buffers; ++i) { in __vb2_queue_cancel()
2040 struct vb2_buffer *vb = q->bufs[i]; in __vb2_queue_cancel()
2080 int vb2_core_streamon(struct vb2_queue *q, unsigned int type) in vb2_core_streamon() argument
2084 if (type != q->type) { in vb2_core_streamon()
2085 dprintk(q, 1, "invalid stream type\n"); in vb2_core_streamon()
2089 if (q->streaming) { in vb2_core_streamon()
2090 dprintk(q, 3, "already streaming\n"); in vb2_core_streamon()
2094 if (!q->num_buffers) { in vb2_core_streamon()
2095 dprintk(q, 1, "no buffers have been allocated\n"); in vb2_core_streamon()
2099 if (q->num_buffers < q->min_buffers_needed) { in vb2_core_streamon()
2100 dprintk(q, 1, "need at least %u allocated buffers\n", in vb2_core_streamon()
2101 q->min_buffers_needed); in vb2_core_streamon()
2109 if (q->queued_count >= q->min_buffers_needed) { in vb2_core_streamon()
2110 ret = v4l_vb2q_enable_media_source(q); in vb2_core_streamon()
2113 ret = vb2_start_streaming(q); in vb2_core_streamon()
2118 q->streaming = 1; in vb2_core_streamon()
2120 dprintk(q, 3, "successful\n"); in vb2_core_streamon()
2125 void vb2_queue_error(struct vb2_queue *q) in vb2_queue_error() argument
2127 q->error = 1; in vb2_queue_error()
2129 wake_up_all(&q->done_wq); in vb2_queue_error()
2133 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type) in vb2_core_streamoff() argument
2135 if (type != q->type) { in vb2_core_streamoff()
2136 dprintk(q, 1, "invalid stream type\n"); in vb2_core_streamoff()
2149 __vb2_queue_cancel(q); in vb2_core_streamoff()
2150 q->waiting_for_buffers = !q->is_output; in vb2_core_streamoff()
2151 q->last_buffer_dequeued = false; in vb2_core_streamoff()
2153 dprintk(q, 3, "successful\n"); in vb2_core_streamoff()
2161 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, in __find_plane_by_offset() argument
2172 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __find_plane_by_offset()
2173 vb = q->bufs[buffer]; in __find_plane_by_offset()
2187 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, in vb2_core_expbuf() argument
2195 if (q->memory != VB2_MEMORY_MMAP) { in vb2_core_expbuf()
2196 dprintk(q, 1, "queue is not currently set up for mmap\n"); in vb2_core_expbuf()
2200 if (!q->mem_ops->get_dmabuf) { in vb2_core_expbuf()
2201 dprintk(q, 1, "queue does not support DMA buffer exporting\n"); in vb2_core_expbuf()
2206 dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n"); in vb2_core_expbuf()
2210 if (type != q->type) { in vb2_core_expbuf()
2211 dprintk(q, 1, "invalid buffer type\n"); in vb2_core_expbuf()
2215 if (index >= q->num_buffers) { in vb2_core_expbuf()
2216 dprintk(q, 1, "buffer index out of range\n"); in vb2_core_expbuf()
2220 vb = q->bufs[index]; in vb2_core_expbuf()
2223 dprintk(q, 1, "buffer plane out of range\n"); in vb2_core_expbuf()
2227 if (vb2_fileio_is_active(q)) { in vb2_core_expbuf()
2228 dprintk(q, 1, "expbuf: file io in progress\n"); in vb2_core_expbuf()
2239 dprintk(q, 1, "failed to export buffer %d, plane %d\n", in vb2_core_expbuf()
2246 dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n", in vb2_core_expbuf()
2252 dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n", in vb2_core_expbuf()
2260 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) in vb2_mmap() argument
2268 if (q->memory != VB2_MEMORY_MMAP) { in vb2_mmap()
2269 dprintk(q, 1, "queue is not currently set up for mmap\n"); in vb2_mmap()
2277 dprintk(q, 1, "invalid vma flags, VM_SHARED needed\n"); in vb2_mmap()
2280 if (q->is_output) { in vb2_mmap()
2282 dprintk(q, 1, "invalid vma flags, VM_WRITE needed\n"); in vb2_mmap()
2287 dprintk(q, 1, "invalid vma flags, VM_READ needed\n"); in vb2_mmap()
2292 mutex_lock(&q->mmap_lock); in vb2_mmap()
2294 if (vb2_fileio_is_active(q)) { in vb2_mmap()
2295 dprintk(q, 1, "mmap: file io in progress\n"); in vb2_mmap()
2303 ret = __find_plane_by_offset(q, off, &buffer, &plane); in vb2_mmap()
2307 vb = q->bufs[buffer]; in vb2_mmap()
2316 dprintk(q, 1, in vb2_mmap()
2332 mutex_unlock(&q->mmap_lock); in vb2_mmap()
2336 dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane); in vb2_mmap()
2342 unsigned long vb2_get_unmapped_area(struct vb2_queue *q, in vb2_get_unmapped_area() argument
2354 if (q->memory != VB2_MEMORY_MMAP) { in vb2_get_unmapped_area()
2355 dprintk(q, 1, "queue is not currently set up for mmap\n"); in vb2_get_unmapped_area()
2362 ret = __find_plane_by_offset(q, off, &buffer, &plane); in vb2_get_unmapped_area()
2366 vb = q->bufs[buffer]; in vb2_get_unmapped_area()
2374 int vb2_core_queue_init(struct vb2_queue *q) in vb2_core_queue_init() argument
2379 if (WARN_ON(!q) || in vb2_core_queue_init()
2380 WARN_ON(!q->ops) || in vb2_core_queue_init()
2381 WARN_ON(!q->mem_ops) || in vb2_core_queue_init()
2382 WARN_ON(!q->type) || in vb2_core_queue_init()
2383 WARN_ON(!q->io_modes) || in vb2_core_queue_init()
2384 WARN_ON(!q->ops->queue_setup) || in vb2_core_queue_init()
2385 WARN_ON(!q->ops->buf_queue)) in vb2_core_queue_init()
2388 if (WARN_ON(q->requires_requests && !q->supports_requests)) in vb2_core_queue_init()
2399 if (WARN_ON(q->supports_requests && q->min_buffers_needed)) in vb2_core_queue_init()
2402 INIT_LIST_HEAD(&q->queued_list); in vb2_core_queue_init()
2403 INIT_LIST_HEAD(&q->done_list); in vb2_core_queue_init()
2404 spin_lock_init(&q->done_lock); in vb2_core_queue_init()
2405 mutex_init(&q->mmap_lock); in vb2_core_queue_init()
2406 init_waitqueue_head(&q->done_wq); in vb2_core_queue_init()
2408 q->memory = VB2_MEMORY_UNKNOWN; in vb2_core_queue_init()
2410 if (q->buf_struct_size == 0) in vb2_core_queue_init()
2411 q->buf_struct_size = sizeof(struct vb2_buffer); in vb2_core_queue_init()
2413 if (q->bidirectional) in vb2_core_queue_init()
2414 q->dma_dir = DMA_BIDIRECTIONAL; in vb2_core_queue_init()
2416 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; in vb2_core_queue_init()
2418 if (q->name[0] == '\0') in vb2_core_queue_init()
2419 snprintf(q->name, sizeof(q->name), "%s-%p", in vb2_core_queue_init()
2420 q->is_output ? "out" : "cap", q); in vb2_core_queue_init()
2426 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2427 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2428 void vb2_core_queue_release(struct vb2_queue *q) in vb2_core_queue_release() argument
2430 __vb2_cleanup_fileio(q); in vb2_core_queue_release()
2431 __vb2_queue_cancel(q); in vb2_core_queue_release()
2432 mutex_lock(&q->mmap_lock); in vb2_core_queue_release()
2433 __vb2_queue_free(q, q->num_buffers); in vb2_core_queue_release()
2434 mutex_unlock(&q->mmap_lock); in vb2_core_queue_release()
2438 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file, in vb2_core_poll() argument
2452 poll_wait(file, &q->done_wq, wait); in vb2_core_poll()
2454 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM))) in vb2_core_poll()
2456 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM))) in vb2_core_poll()
2462 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) { in vb2_core_poll()
2463 if (!q->is_output && (q->io_modes & VB2_READ) && in vb2_core_poll()
2465 if (__vb2_init_fileio(q, 1)) in vb2_core_poll()
2468 if (q->is_output && (q->io_modes & VB2_WRITE) && in vb2_core_poll()
2470 if (__vb2_init_fileio(q, 0)) in vb2_core_poll()
2483 if (!vb2_is_streaming(q) || q->error) in vb2_core_poll()
2492 if (q->quirk_poll_must_check_waiting_for_buffers && in vb2_core_poll()
2493 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM))) in vb2_core_poll()
2500 if (q->is_output && q->fileio && q->queued_count < q->num_buffers) in vb2_core_poll()
2503 if (list_empty(&q->done_list)) { in vb2_core_poll()
2508 if (q->last_buffer_dequeued) in vb2_core_poll()
2515 spin_lock_irqsave(&q->done_lock, flags); in vb2_core_poll()
2516 if (!list_empty(&q->done_list)) in vb2_core_poll()
2517 vb = list_first_entry(&q->done_list, struct vb2_buffer, in vb2_core_poll()
2519 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_core_poll()
2523 return (q->is_output) ? in vb2_core_poll()
2587 static int __vb2_init_fileio(struct vb2_queue *q, int read) in __vb2_init_fileio() argument
2596 if (WARN_ON((read && !(q->io_modes & VB2_READ)) || in __vb2_init_fileio()
2597 (!read && !(q->io_modes & VB2_WRITE)))) in __vb2_init_fileio()
2603 if (!q->mem_ops->vaddr) in __vb2_init_fileio()
2609 if (q->streaming || q->num_buffers > 0) in __vb2_init_fileio()
2617 dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", in __vb2_init_fileio()
2618 (read) ? "read" : "write", count, q->fileio_read_once, in __vb2_init_fileio()
2619 q->fileio_write_immediately); in __vb2_init_fileio()
2625 fileio->read_once = q->fileio_read_once; in __vb2_init_fileio()
2626 fileio->write_immediately = q->fileio_write_immediately; in __vb2_init_fileio()
2634 fileio->type = q->type; in __vb2_init_fileio()
2635 q->fileio = fileio; in __vb2_init_fileio()
2636 ret = vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_init_fileio()
2644 if (q->bufs[0]->num_planes != 1) { in __vb2_init_fileio()
2652 for (i = 0; i < q->num_buffers; i++) { in __vb2_init_fileio()
2653 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); in __vb2_init_fileio()
2658 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); in __vb2_init_fileio()
2668 for (i = 0; i < q->num_buffers; i++) { in __vb2_init_fileio()
2669 ret = vb2_core_qbuf(q, i, NULL, NULL); in __vb2_init_fileio()
2678 fileio->initial_index = q->num_buffers; in __vb2_init_fileio()
2679 fileio->cur_index = q->num_buffers; in __vb2_init_fileio()
2685 ret = vb2_core_streamon(q, q->type); in __vb2_init_fileio()
2693 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_init_fileio()
2696 q->fileio = NULL; in __vb2_init_fileio()
2705 static int __vb2_cleanup_fileio(struct vb2_queue *q) in __vb2_cleanup_fileio() argument
2707 struct vb2_fileio_data *fileio = q->fileio; in __vb2_cleanup_fileio()
2710 vb2_core_streamoff(q, q->type); in __vb2_cleanup_fileio()
2711 q->fileio = NULL; in __vb2_cleanup_fileio()
2713 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_cleanup_fileio()
2715 dprintk(q, 3, "file io emulator closed\n"); in __vb2_cleanup_fileio()
2729 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, in __vb2_perform_fileio() argument
2734 bool is_multiplanar = q->is_multiplanar; in __vb2_perform_fileio()
2740 bool copy_timestamp = !read && q->copy_timestamp; in __vb2_perform_fileio()
2744 dprintk(q, 3, "mode %s, offset %ld, count %zd, %sblocking\n", in __vb2_perform_fileio()
2751 if (q->waiting_in_dqbuf) { in __vb2_perform_fileio()
2752 dprintk(q, 3, "another dup()ped fd is %s\n", in __vb2_perform_fileio()
2760 if (!vb2_fileio_is_active(q)) { in __vb2_perform_fileio()
2761 ret = __vb2_init_fileio(q, read); in __vb2_perform_fileio()
2762 dprintk(q, 3, "vb2_init_fileio result: %d\n", ret); in __vb2_perform_fileio()
2766 fileio = q->fileio; in __vb2_perform_fileio()
2772 if (index >= q->num_buffers) { in __vb2_perform_fileio()
2778 ret = vb2_core_dqbuf(q, &index, NULL, nonblock); in __vb2_perform_fileio()
2779 dprintk(q, 5, "vb2_dqbuf result: %d\n", ret); in __vb2_perform_fileio()
2786 b = q->bufs[index]; in __vb2_perform_fileio()
2793 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0) in __vb2_perform_fileio()
2794 : vb2_plane_size(q->bufs[index], 0); in __vb2_perform_fileio()
2810 dprintk(q, 5, "reducing read count: %zd\n", count); in __vb2_perform_fileio()
2816 dprintk(q, 3, "copying %zd bytes - buffer %d, offset %u\n", in __vb2_perform_fileio()
2823 dprintk(q, 3, "error copying data\n"); in __vb2_perform_fileio()
2837 struct vb2_buffer *b = q->bufs[index]; in __vb2_perform_fileio()
2843 dprintk(q, 3, "read limit reached\n"); in __vb2_perform_fileio()
2844 return __vb2_cleanup_fileio(q); in __vb2_perform_fileio()
2854 ret = vb2_core_qbuf(q, index, NULL, NULL); in __vb2_perform_fileio()
2855 dprintk(q, 5, "vb2_dbuf result: %d\n", ret); in __vb2_perform_fileio()
2864 buf->size = vb2_plane_size(q->bufs[index], 0); in __vb2_perform_fileio()
2870 if (fileio->initial_index < q->num_buffers) in __vb2_perform_fileio()
2890 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, in vb2_read() argument
2893 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); in vb2_read()
2897 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count, in vb2_write() argument
2900 return __vb2_perform_fileio(q, (char __user *) data, count, in vb2_write()
2914 struct vb2_queue *q = data; in vb2_thread() local
2915 struct vb2_threadio_data *threadio = q->threadio; in vb2_thread()
2921 if (q->is_output) { in vb2_thread()
2922 prequeue = q->num_buffers; in vb2_thread()
2923 copy_timestamp = q->copy_timestamp; in vb2_thread()
2935 vb = q->bufs[index++]; in vb2_thread()
2938 call_void_qop(q, wait_finish, q); in vb2_thread()
2940 ret = vb2_core_dqbuf(q, &index, NULL, 0); in vb2_thread()
2941 call_void_qop(q, wait_prepare, q); in vb2_thread()
2942 dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret); in vb2_thread()
2944 vb = q->bufs[index]; in vb2_thread()
2953 call_void_qop(q, wait_finish, q); in vb2_thread()
2957 ret = vb2_core_qbuf(q, vb->index, NULL, NULL); in vb2_thread()
2958 call_void_qop(q, wait_prepare, q); in vb2_thread()
2976 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv, in vb2_thread_start() argument
2982 if (q->threadio) in vb2_thread_start()
2984 if (vb2_is_busy(q)) in vb2_thread_start()
2986 if (WARN_ON(q->fileio)) in vb2_thread_start()
2995 ret = __vb2_init_fileio(q, !q->is_output); in vb2_thread_start()
2996 dprintk(q, 3, "file io: vb2_init_fileio result: %d\n", ret); in vb2_thread_start()
2999 q->threadio = threadio; in vb2_thread_start()
3000 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name); in vb2_thread_start()
3009 __vb2_cleanup_fileio(q); in vb2_thread_start()
3016 int vb2_thread_stop(struct vb2_queue *q) in vb2_thread_stop() argument
3018 struct vb2_threadio_data *threadio = q->threadio; in vb2_thread_stop()
3025 vb2_queue_error(q); in vb2_thread_stop()
3027 __vb2_cleanup_fileio(q); in vb2_thread_stop()
3030 q->threadio = NULL; in vb2_thread_stop()