Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: videobuf2: Use vb2_get_num_buffers() helper

Stop using queue num_buffers field directly, instead use
vb2_get_num_buffers().
This prepares for the future 'delete buffers' feature where there are
holes in the buffer indices.

Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>

authored by

Benjamin Gaignard and committed by
Mauro Carvalho Chehab
da8fc26b 741d0f6b

+52 -46
+50 -44
drivers/media/common/videobuf2/videobuf2-core.c
··· 449 449 unsigned int num_buffers, unsigned int num_planes, 450 450 const unsigned plane_sizes[VB2_MAX_PLANES]) 451 451 { 452 + unsigned int q_num_buffers = vb2_get_num_buffers(q); 452 453 unsigned int buffer, plane; 453 454 struct vb2_buffer *vb; 454 455 int ret; 455 456 456 457 /* Ensure that q->num_buffers+num_buffers is below VB2_MAX_FRAME */ 457 458 num_buffers = min_t(unsigned int, num_buffers, 458 - VB2_MAX_FRAME - q->num_buffers); 459 + VB2_MAX_FRAME - q_num_buffers); 459 460 460 461 for (buffer = 0; buffer < num_buffers; ++buffer) { 461 462 /* Allocate vb2 buffer structures */ ··· 476 475 vb->planes[plane].min_length = plane_sizes[plane]; 477 476 } 478 477 479 - vb2_queue_add_buffer(q, vb, q->num_buffers + buffer); 478 + vb2_queue_add_buffer(q, vb, q_num_buffers + buffer); 480 479 call_void_bufop(q, init_buffer, vb); 481 480 482 481 /* Allocate video buffer memory for the MMAP type */ ··· 520 519 { 521 520 unsigned int buffer; 522 521 struct vb2_buffer *vb; 522 + unsigned int q_num_buffers = vb2_get_num_buffers(q); 523 523 524 - for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; 524 + for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; 525 525 ++buffer) { 526 526 vb = vb2_get_buffer(q, buffer); 527 527 if (!vb) ··· 546 544 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) 547 545 { 548 546 unsigned int buffer; 547 + unsigned int q_num_buffers = vb2_get_num_buffers(q); 549 548 550 549 lockdep_assert_held(&q->mmap_lock); 551 550 552 551 /* Call driver-provided cleanup function for each buffer, if provided */ 553 - for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; 552 + for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; 554 553 ++buffer) { 555 554 struct vb2_buffer *vb = vb2_get_buffer(q, buffer); 556 555 ··· 567 564 * Check that all the calls were balanced during the life-time of this 568 565 * queue. If not then dump the counters to the kernel log. 569 566 */ 570 - if (q->num_buffers) { 567 + if (q_num_buffers) { 571 568 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || 572 569 q->cnt_prepare_streaming != q->cnt_unprepare_streaming || 573 570 q->cnt_wait_prepare != q->cnt_wait_finish; ··· 593 590 q->cnt_stop_streaming = 0; 594 591 q->cnt_unprepare_streaming = 0; 595 592 } 596 - for (buffer = 0; buffer < q->num_buffers; ++buffer) { 593 + for (buffer = 0; buffer < vb2_get_num_buffers(q); buffer++) { 597 594 struct vb2_buffer *vb = vb2_get_buffer(q, buffer); 598 595 bool unbalanced; 599 596 ··· 645 642 #endif 646 643 647 644 /* Free vb2 buffers */ 648 - for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; 645 + for (buffer = q_num_buffers - buffers; buffer < q_num_buffers; 649 646 ++buffer) { 650 647 struct vb2_buffer *vb = vb2_get_buffer(q, buffer); 651 648 ··· 657 654 } 658 655 659 656 q->num_buffers -= buffers; 660 - if (!q->num_buffers) { 657 + if (!vb2_get_num_buffers(q)) { 661 658 q->memory = VB2_MEMORY_UNKNOWN; 662 659 INIT_LIST_HEAD(&q->queued_list); 663 660 } ··· 688 685 static bool __buffers_in_use(struct vb2_queue *q) 689 686 { 690 687 unsigned int buffer; 691 - for (buffer = 0; buffer < q->num_buffers; ++buffer) { 688 + for (buffer = 0; buffer < vb2_get_num_buffers(q); ++buffer) { 692 689 struct vb2_buffer *vb = vb2_get_buffer(q, buffer); 693 690 694 691 if (!vb) ··· 814 811 unsigned int flags, unsigned int *count) 815 812 { 816 813 unsigned int num_buffers, allocated_buffers, num_planes = 0; 814 + unsigned int q_num_bufs = vb2_get_num_buffers(q); 817 815 unsigned plane_sizes[VB2_MAX_PLANES] = { }; 818 816 bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; 819 817 unsigned int i; ··· 830 826 return -EBUSY; 831 827 } 832 828 833 - if (*count == 0 || q->num_buffers != 0 || 829 + if (*count == 0 || q_num_bufs != 0 || 834 830 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) || 835 831 !verify_coherency_flags(q, non_coherent_mem)) { 836 832 /* ··· 848 844 * queued without ever calling STREAMON. 849 845 */ 850 846 __vb2_queue_cancel(q); 851 - __vb2_queue_free(q, q->num_buffers); 847 + __vb2_queue_free(q, q_num_bufs); 852 848 mutex_unlock(&q->mmap_lock); 853 849 854 850 /* ··· 943 939 if (ret < 0) { 944 940 /* 945 941 * Note: __vb2_queue_free() will subtract 'allocated_buffers' 946 - * from q->num_buffers and it will reset q->memory to 942 + * from already queued buffers and it will reset q->memory to 947 943 * VB2_MEMORY_UNKNOWN. 948 944 */ 949 945 __vb2_queue_free(q, allocated_buffers); ··· 977 973 unsigned int num_planes = 0, num_buffers, allocated_buffers; 978 974 unsigned plane_sizes[VB2_MAX_PLANES] = { }; 979 975 bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; 980 - bool no_previous_buffers = !q->num_buffers; 981 - int ret; 976 + unsigned int q_num_bufs = vb2_get_num_buffers(q); 977 + bool no_previous_buffers = !q_num_bufs; 978 + int ret = 0; 982 979 983 - if (q->num_buffers == VB2_MAX_FRAME) { 980 + if (q_num_bufs == VB2_MAX_FRAME) { 984 981 dprintk(q, 1, "maximum number of buffers already allocated\n"); 985 982 return -ENOBUFS; 986 983 } ··· 1010 1005 return -EINVAL; 1011 1006 } 1012 1007 1013 - num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); 1008 + num_buffers = min(*count, VB2_MAX_FRAME - q_num_bufs); 1014 1009 1015 1010 if (requested_planes && requested_sizes) { 1016 1011 num_planes = requested_planes; ··· 1042 1037 num_buffers = allocated_buffers; 1043 1038 1044 1039 /* 1045 - * q->num_buffers contains the total number of buffers, that the 1040 + * num_buffers contains the total number of buffers, that the 1046 1041 * queue driver has set up 1047 1042 */ 1048 1043 ret = call_qop(q, queue_setup, q, &num_buffers, ··· 1063 1058 if (ret < 0) { 1064 1059 /* 1065 1060 * Note: __vb2_queue_free() will subtract 'allocated_buffers' 1066 - * from q->num_buffers and it will reset q->memory to 1061 + * from already queued buffers and it will reset q->memory to 1067 1062 * VB2_MEMORY_UNKNOWN. 1068 1063 */ 1069 1064 __vb2_queue_free(q, allocated_buffers); ··· 1680 1675 * Forcefully reclaim buffers if the driver did not 1681 1676 * correctly return them to vb2. 1682 1677 */ 1683 - for (i = 0; i < q->num_buffers; ++i) { 1678 + for (i = 0; i < vb2_get_num_buffers(q); ++i) { 1684 1679 vb = vb2_get_buffer(q, i); 1685 1680 1686 1681 if (!vb) ··· 2086 2081 * to vb2 in stop_streaming(). 2087 2082 */ 2088 2083 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { 2089 - for (i = 0; i < q->num_buffers; ++i) { 2084 + for (i = 0; i < vb2_get_num_buffers(q); i++) { 2090 2085 struct vb2_buffer *vb = vb2_get_buffer(q, i); 2091 2086 2092 2087 if (!vb) ··· 2130 2125 * call to __fill_user_buffer() after buf_finish(). That order can't 2131 2126 * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). 2132 2127 */ 2133 - for (i = 0; i < q->num_buffers; ++i) { 2128 + for (i = 0; i < vb2_get_num_buffers(q); i++) { 2134 2129 struct vb2_buffer *vb; 2135 2130 struct media_request *req; 2136 2131 ··· 2178 2173 2179 2174 int vb2_core_streamon(struct vb2_queue *q, unsigned int type) 2180 2175 { 2176 + unsigned int q_num_bufs = vb2_get_num_buffers(q); 2181 2177 int ret; 2182 2178 2183 2179 if (type != q->type) { ··· 2191 2185 return 0; 2192 2186 } 2193 2187 2194 - if (!q->num_buffers) { 2188 + if (!q_num_bufs) { 2195 2189 dprintk(q, 1, "no buffers have been allocated\n"); 2196 2190 return -EINVAL; 2197 2191 } 2198 2192 2199 - if (q->num_buffers < q->min_buffers_needed) { 2193 + if (q_num_bufs < q->min_buffers_needed) { 2200 2194 dprintk(q, 1, "need at least %u allocated buffers\n", 2201 2195 q->min_buffers_needed); 2202 2196 return -EINVAL; ··· 2524 2518 __vb2_cleanup_fileio(q); 2525 2519 __vb2_queue_cancel(q); 2526 2520 mutex_lock(&q->mmap_lock); 2527 - __vb2_queue_free(q, q->num_buffers); 2521 + __vb2_queue_free(q, vb2_get_num_buffers(q)); 2528 2522 mutex_unlock(&q->mmap_lock); 2529 2523 } 2530 2524 EXPORT_SYMBOL_GPL(vb2_core_queue_release); ··· 2553 2547 /* 2554 2548 * Start file I/O emulator only if streaming API has not been used yet. 2555 2549 */ 2556 - if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) { 2550 + if (vb2_get_num_buffers(q) == 0 && !vb2_fileio_is_active(q)) { 2557 2551 if (!q->is_output && (q->io_modes & VB2_READ) && 2558 2552 (req_events & (EPOLLIN | EPOLLRDNORM))) { 2559 2553 if (__vb2_init_fileio(q, 1)) ··· 2591 2585 * For output streams you can call write() as long as there are fewer 2592 2586 * buffers queued than there are buffers available. 2593 2587 */ 2594 - if (q->is_output && q->fileio && q->queued_count < q->num_buffers) 2588 + if (q->is_output && q->fileio && q->queued_count < vb2_get_num_buffers(q)) 2595 2589 return EPOLLOUT | EPOLLWRNORM; 2596 2590 2597 2591 if (list_empty(&q->done_list)) { ··· 2640 2634 * struct vb2_fileio_data - queue context used by file io emulator 2641 2635 * 2642 2636 * @cur_index: the index of the buffer currently being read from or 2643 - * written to. If equal to q->num_buffers then a new buffer 2644 - * must be dequeued. 2637 + * written to. If equal to number of buffers in the vb2_queue 2638 + * then a new buffer must be dequeued. 2645 2639 * @initial_index: in the read() case all buffers are queued up immediately 2646 2640 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles 2647 2641 * buffers. However, in the write() case no buffers are initially ··· 2651 2645 * buffers. This means that initially __vb2_perform_fileio() 2652 2646 * needs to know what buffer index to use when it is queuing up 2653 2647 * the buffers for the first time. That initial index is stored 2654 - * in this field. Once it is equal to q->num_buffers all 2655 - * available buffers have been queued and __vb2_perform_fileio() 2656 - * should start the normal dequeue/queue cycle. 2648 + * in this field. Once it is equal to number of buffers in the 2649 + * vb2_queue all available buffers have been queued and 2650 + * __vb2_perform_fileio() should start the normal dequeue/queue cycle. 2657 2651 * 2658 2652 * vb2 provides a compatibility layer and emulator of file io (read and 2659 2653 * write) calls on top of streaming API. For proper operation it required ··· 2701 2695 /* 2702 2696 * Check if streaming api has not been already activated. 2703 2697 */ 2704 - if (q->streaming || q->num_buffers > 0) 2698 + if (q->streaming || vb2_get_num_buffers(q) > 0) 2705 2699 return -EBUSY; 2706 2700 2707 2701 /* ··· 2751 2745 /* 2752 2746 * Get kernel address of each buffer. 2753 2747 */ 2754 - for (i = 0; i < q->num_buffers; i++) { 2748 + for (i = 0; i < vb2_get_num_buffers(q); i++) { 2755 2749 /* vb can never be NULL when using fileio. */ 2756 2750 vb = vb2_get_buffer(q, i); 2757 2751 ··· 2770 2764 /* 2771 2765 * Queue all buffers. 2772 2766 */ 2773 - for (i = 0; i < q->num_buffers; i++) { 2767 + for (i = 0; i < vb2_get_num_buffers(q); i++) { 2774 2768 struct vb2_buffer *vb2 = vb2_get_buffer(q, i); 2775 2769 2776 2770 if (!vb2) ··· 2783 2777 } 2784 2778 /* 2785 2779 * All buffers have been queued, so mark that by setting 2786 - * initial_index to q->num_buffers 2780 + * initial_index to the number of buffers in the vb2_queue 2787 2781 */ 2788 - fileio->initial_index = q->num_buffers; 2789 - fileio->cur_index = q->num_buffers; 2782 + fileio->initial_index = vb2_get_num_buffers(q); 2783 + fileio->cur_index = fileio->initial_index; 2790 2784 } 2791 2785 2792 2786 /* ··· 2879 2873 * Check if we need to dequeue the buffer. 2880 2874 */ 2881 2875 index = fileio->cur_index; 2882 - if (index >= q->num_buffers) { 2876 + if (index >= vb2_get_num_buffers(q)) { 2883 2877 struct vb2_buffer *b; 2884 2878 2885 2879 /* ··· 2980 2974 * If we are queuing up buffers for the first time, then 2981 2975 * increase initial_index by one. 2982 2976 */ 2983 - if (fileio->initial_index < q->num_buffers) 2977 + if (fileio->initial_index < vb2_get_num_buffers(q)) 2984 2978 fileio->initial_index++; 2985 2979 /* 2986 2980 * The next buffer to use is either a buffer that's going to be 2987 - * queued for the first time (initial_index < q->num_buffers) 2988 - * or it is equal to q->num_buffers, meaning that the next 2989 - * time we need to dequeue a buffer since we've now queued up 2990 - * all the 'first time' buffers. 2981 + * queued for the first time (initial_index < number of buffers in the vb2_queue) 2982 + * or it is equal to the number of buffers in the vb2_queue, 2983 + * meaning that the next time we need to dequeue a buffer since 2984 + * we've now queued up all the 'first time' buffers. 2991 2985 */ 2992 2986 fileio->cur_index = fileio->initial_index; 2993 2987 } ··· 3032 3026 int ret = 0; 3033 3027 3034 3028 if (q->is_output) { 3035 - prequeue = q->num_buffers; 3029 + prequeue = vb2_get_num_buffers(q); 3036 3030 copy_timestamp = q->copy_timestamp; 3037 3031 } 3038 3032
+2 -2
drivers/media/common/videobuf2/videobuf2-v4l2.c
··· 621 621 * This loop doesn't scale if there is a really large number of buffers. 622 622 * Maybe something more efficient will be needed in this case. 623 623 */ 624 - for (i = 0; i < q->num_buffers; i++) { 624 + for (i = 0; i < vb2_get_num_buffers(q); i++) { 625 625 vb2 = vb2_get_buffer(q, i); 626 626 627 627 if (!vb2) ··· 755 755 756 756 fill_buf_caps(q, &create->capabilities); 757 757 validate_memory_flags(q, create->memory, &create->flags); 758 - create->index = q->num_buffers; 758 + create->index = vb2_get_num_buffers(q); 759 759 if (create->count == 0) 760 760 return ret != -EBUSY ? ret : 0; 761 761