Skip to contentSkip to conversation

media: add min/rec_num_buffers + queue_info op

RFC PATCH New Hans Verkuil 1 participant 6 messages Nov 12, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover media: add min/rec_num_buffers + queue_info op

This series supersedes these older RFC patches:

https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
https://patchwork.linuxtv.org/project/linux-media/list/?series=13889
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

The first two patches add support for reporting min_num_buffers and rec_num_buffers.

The third patch renames the vb2_queue field min_reqbufs_allocation to rec_num_buffers, which makes much more sense as a name, and syncs up nicely with the new v4l2_createbuffers field.

The next patch adds support for a queue_info op to replace the existing queue_setup op, and the last patch implements that for vivid.

As you can see, the new op is a lot easier to use, and it also prevents drivers from messing with the buffer count. Instead they can just set rec_num_buffers according to what they need.

If these changes are accepted (esp. the first two patches), then the next step will be to go through all drivers that fiddle with the count value in queue_setup. Ideally I'd like to replace queue_setup by queue_info as well, but that's an optional step and depends one whether the queue_info proposal is accepted.

Regards,

Hans

Hans Verkuil (5):
media: vb2: add support for min/rec_num_buffers
Documentation: media: document min/rec_num_buffers
media: vb2: rename min_reqbufs_allocation to rec_num_buffers
media: vb2: introduce queue_info to replace queue_setup
media: vivid: convert queue_setup to queue_info

 .../media/v4l/vidioc-create-bufs.rst          | 34 +++++--
 .../media/v4l/vidioc-reqbufs.rst              |  6 ++
 .../media/common/videobuf2/videobuf2-core.c   | 96 +++++++++++++------
 .../media/common/videobuf2/videobuf2-v4l2.c   | 19 +++-
 .../media/test-drivers/vicodec/vicodec-core.c |  4 +-
 .../media/test-drivers/vimc/vimc-capture.c    |  2 +-
 drivers/media/test-drivers/vivid/vivid-core.c |  4 +-
 .../media/test-drivers/vivid/vivid-meta-cap.c | 17 +---
 .../media/test-drivers/vivid/vivid-meta-out.c | 27 ++----
 .../media/test-drivers/vivid/vivid-sdr-cap.c  | 16 ++--
 .../test-drivers/vivid/vivid-touch-cap.c      | 18 +---
 .../media/test-drivers/vivid/vivid-vbi-cap.c  | 13 +--
 .../media/test-drivers/vivid/vivid-vbi-out.c  | 13 +--
 .../media/test-drivers/vivid/vivid-vid-cap.c  | 39 +++-----
 .../media/test-drivers/vivid/vivid-vid-out.c  | 37 ++-----
 include/media/videobuf2-core.h                | 22 ++++-
 include/uapi/linux/videodev2.h                | 20 +++-
 17 files changed, 208 insertions(+), 179 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

RFC PATCH v2 1/5 media: vb2: add support for min/rec_num_buffers

Report the minimum and recommended number of buffers in struct v4l2_create_buffers. This is useful for those applications that want to have more precise control over how many buffers they want to allocate.

---
 .../media/common/videobuf2/videobuf2-v4l2.c   | 19 +++++++++++++-----
 include/uapi/linux/videodev2.h                | 20 +++++++++++++++----
 2 files changed, 30 insertions(+), 9 deletions(-)
drivers/media/common/videobuf2/videobuf2-v4l2.c +14 -5
index 9201d854dbcc..15394bb4168c 100644
@@ -672,7 +672,8 @@ int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
 EXPORT_SYMBOL(vb2_querybuf);
 
 static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory,
-				   u32 *flags, u32 *caps, u32 *max_num_bufs)
+				   u32 *flags, u32 *caps, u32 *max_num_bufs,
+				   u16 *min_num_bufs, u16 *rec_num_bufs)
 {
 	if (!q->allow_cache_hints || memory != V4L2_MEMORY_MMAP) {
 		/*
@@ -702,6 +703,11 @@ static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory,
 		*max_num_bufs = q->max_num_buffers;
 		*caps |= V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS;
 	}
+	if (min_num_bufs && rec_num_bufs) {
+		*min_num_bufs = q->min_queued_buffers + 1;
+		*rec_num_bufs = q->min_reqbufs_allocation;
+		*caps |= V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS;
+	}
 }
 
 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
@@ -710,7 +716,7 @@ int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
 	u32 flags = req->flags;
 
 	vb2_set_flags_and_caps(q, req->memory, &flags,
-			       &req->capabilities, NULL);
+			       &req->capabilities, NULL, NULL, NULL);
 	req->flags = flags;
 	return ret ? ret : vb2_core_reqbufs(q, req->memory,
 					    req->flags, &req->count);
@@ -753,7 +759,9 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
 
 	create->index = vb2_get_num_buffers(q);
 	vb2_set_flags_and_caps(q, create->memory, &create->flags,
-			       &create->capabilities, &create->max_num_buffers);
+			       &create->capabilities, &create->max_num_buffers,
+			       &create->min_num_buffers,
+			       &create->rec_num_buffers);
 	if (create->count == 0)
 		return ret != -EBUSY ? ret : 0;
 
@@ -1027,7 +1035,7 @@ int vb2_ioctl_reqbufs(struct file *file, void *priv,
 	u32 flags = p->flags;
 
 	vb2_set_flags_and_caps(vdev->queue, p->memory, &flags,
-			       &p->capabilities, NULL);
+			       &p->capabilities, NULL, NULL, NULL);
 	p->flags = flags;
 	if (res)
 		return res;
@@ -1050,7 +1058,8 @@ int vb2_ioctl_create_bufs(struct file *file, void *priv,
 
 	p->index = vb2_get_num_buffers(vdev->queue);
 	vb2_set_flags_and_caps(vdev->queue, p->memory, &p->flags,
-			       &p->capabilities, &p->max_num_buffers);
+			       &p->capabilities, &p->max_num_buffers,
+			       &p->min_num_buffers, &p->rec_num_buffers);
 	/*
 	 * If count == 0, then just check if memory and type are valid.
 	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
include/uapi/linux/videodev2.h +16 -4
index e7c4dce39007..68cca8dda937 100644
@@ -1079,6 +1079,7 @@ struct v4l2_requestbuffers {
 #define V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS		(1 << 6)
 #define V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS		(1 << 7)
 #define V4L2_BUF_CAP_SUPPORTS_REMOVE_BUFS		(1 << 8)
+#define V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS	(1 << 9)
 
 /**
  * struct v4l2_plane - plane info for multi-planar buffers
@@ -2660,9 +2661,18 @@ struct v4l2_dbg_chip_info {
  * @flags:	additional buffer management attributes (ignored unless the
  *		queue has V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS capability
  *		and configured for MMAP streaming I/O).
- * @max_num_buffers: if V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS capability flag is set
- *		this field indicate the maximum possible number of buffers
- *		for this queue.
+ * @max_num_buffers: if the V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS capability
+ *		flag is set, then this field indicates the maximum possible
+ *		number of buffers for this queue.
+ * @min_num_buffers: if the V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS capability
+ *		flag is set, then this field indicates the minimum possible
+ *		number of buffers for this queue. Using this value might cause
+ *		dropped frames, but it will be able to stream video. Useful for
+ *		capturing only a single frame.
+ * @rec_num_buffers: if the V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS capability
+ *		flag is set, then this field indicates the recommended number
+ *		of buffers for this queue. This is also used when calling
+ *		VIDIOC_REQBUFS to ensure count is at least this value.
  * @reserved:	future extensions
  */
 struct v4l2_create_buffers {
@@ -2673,7 +2683,9 @@ struct v4l2_create_buffers {
 	__u32			capabilities;
 	__u32			flags;
 	__u32			max_num_buffers;
-	__u32			reserved[5];
+	__u16			min_num_buffers;
+	__u16			rec_num_buffers;
+	__u32			reserved[4];
 };
 
 /**
Signature · 1 line
2.45.2
#2 lore raw

RFC PATCH v2 2/5 Documentation: media: document min/rec_num_buffers

In reply to Hans Verkuil

Document the new min_num_buffers and rec_num_buffers fields and the corresponding V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS capability flag.

---
 .../media/v4l/vidioc-create-bufs.rst          | 34 +++++++++++++++----
 .../media/v4l/vidioc-reqbufs.rst              |  6 ++++
 2 files changed, 33 insertions(+), 7 deletions(-)
Documentation/userspace-api/media/v4l/vidioc-create-bufs.rst +27 -7
index 49232c9006c2..a400d464cc9f 100644
@@ -68,7 +68,12 @@ When the ioctl is called with a pointer to this structure the driver
 will attempt to allocate up to the requested number of buffers and store
 the actual number allocated and the starting index in the ``count`` and
 the ``index`` fields respectively. On return ``count`` can be smaller
-than the number requested.
+than the number requested, but never larger. If no buffers could be
+allocated, then -1 is returned and errno is set to the ``ENOMEM`` error code.
+
+If ``count`` was set to 0, then no buffers are allocated, and instead
+it just sets the various informative fields, e.g. ``capabilities``,
+``max_num_buffers``, etc.
 
 .. c:type:: v4l2_create_buffers
 
@@ -88,9 +93,9 @@ than the number requested.
 	:ref:`VIDIOC_CREATE_BUFS` will set ``index`` to the current number of
 	created buffers, and it will check the validity of ``memory`` and
 	``format.type``. If those are invalid -1 is returned and errno is
-	set to ``EINVAL`` error code, otherwise :ref:`VIDIOC_CREATE_BUFS` returns
-	0. It will never set errno to ``EBUSY`` error code in this particular
-	case.
+	set to the ``EINVAL`` error code, otherwise :ref:`VIDIOC_CREATE_BUFS`
+	returns	0. It will never set errno to the ``EBUSY`` error code in this
+	particular case.
     * - __u32
       - ``memory``
       - Applications set this field to ``V4L2_MEMORY_MMAP``,
@@ -118,11 +123,26 @@ than the number requested.
 	See :ref:`memory-flags`.
     * - __u32
       - ``max_num_buffers``
-      - If the V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS capability flag is set
-        this field indicates the maximum possible number of buffers
+      - If the ``V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS`` capability flag is
+        set this field indicates the maximum possible number of buffers
         for this queue.
+    * - __u16
+      - ``min_num_buffers``
+      - If the ``V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS`` capability flag is
+        set this field indicates the minimum number of buffers for this queue.
+	Allocating this number of buffers will allow video streaming, but frame
+	drops might happen.
+    * - __u16
+      - ``rec_num_buffers``
+      - If the ``V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS`` capability flag is
+        set this field indicates the recommended number of buffers for this
+	queue.
+	The :ref:`VIDIOC_REQBUFS` will use this value if the requested number of
+	buffers	is less than this value. This value ensures reliable video
+	streaming without frame drops, provided the application can process
+	a dequeued buffer within a single frame period.
     * - __u32
-      - ``reserved``\ [5]
+      - ``reserved``\ [4]
       - A place holder for future extensions. Drivers and applications
 	must set the array to zero.
 
Documentation/userspace-api/media/v4l/vidioc-reqbufs.rst +6 -0
index daf9a6621b50..4c5bb303b5a2 100644
@@ -124,6 +124,7 @@ aborting or finishing any DMA in progress, an implicit
 .. _V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS:
 .. _V4L2-BUF-CAP-SUPPORTS-MAX-NUM-BUFFERS:
 .. _V4L2-BUF-CAP-SUPPORTS-REMOVE-BUFS:
+.. _V4L2-BUF-CAP-SUPPORTS-MIN-REC-NUM-BUFFERS:
 
 .. flat-table:: V4L2 Buffer Capabilities Flags
     :header-rows:  0
@@ -167,6 +168,11 @@ aborting or finishing any DMA in progress, an implicit
     * - ``V4L2_BUF_CAP_SUPPORTS_REMOVE_BUFS``
       - 0x00000100
       - If set, then ``VIDIOC_REMOVE_BUFS`` is supported.
+    * - ``V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS``
+      - 0x00000200
+      - If set, then the ``min_num_buffers`` and ``rec_num_buffers`` fields in
+        ``struct v4l2_create_buffers`` are valid. If not set, then these fields
+	are 0.
 
 .. _memory-flags:
 .. _V4L2-MEMORY-FLAG-NON-COHERENT:
Signature · 1 line
2.45.2
#3 lore raw

RFC PATCH v2 3/5 media: vb2: rename min_reqbufs_allocation to rec_num_buffers

In reply to Hans Verkuil

The name 'rec_num_buffers' matches what struct v4l2_create_buffers uses, and 'recommended number of buffers' is a much more descriptive name.

---
 drivers/media/common/videobuf2/videobuf2-core.c   | 14 +++++++-------
 drivers/media/common/videobuf2/videobuf2-v4l2.c   |  2 +-
 drivers/media/test-drivers/vicodec/vicodec-core.c |  4 ++--
 drivers/media/test-drivers/vimc/vimc-capture.c    |  2 +-
 drivers/media/test-drivers/vivid/vivid-core.c     |  4 ++--
 include/media/videobuf2-core.h                    | 12 +++++++-----
 6 files changed, 20 insertions(+), 18 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +7 -7
index c0cc441b5164..4b55280dc8b8 100644
@@ -923,7 +923,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 	/*
 	 * Make sure the requested values and current defaults are sane.
 	 */
-	num_buffers = max_t(unsigned int, *count, q->min_reqbufs_allocation);
+	num_buffers = max_t(unsigned int, *count, q->rec_num_buffers);
 	num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers);
 	memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
 	/*
@@ -974,7 +974,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 	 * There is no point in continuing if we can't allocate the minimum
 	 * number of buffers needed by this vb2_queue.
 	 */
-	if (allocated_buffers < q->min_reqbufs_allocation)
+	if (allocated_buffers < q->rec_num_buffers)
 		ret = -ENOMEM;
 
 	/*
@@ -2647,10 +2647,10 @@ int vb2_core_queue_init(struct vb2_queue *q)
 	 * 'min_queued_buffers + 1' to keep at least one buffer available
 	 * for userspace.
 	 */
-	if (q->min_reqbufs_allocation < q->min_queued_buffers + 1)
-		q->min_reqbufs_allocation = q->min_queued_buffers + 1;
+	if (q->rec_num_buffers < q->min_queued_buffers + 1)
+		q->rec_num_buffers = q->min_queued_buffers + 1;
 
-	if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers))
+	if (WARN_ON(q->rec_num_buffers > q->max_num_buffers))
 		return -EINVAL;
 
 	/* Either both or none are set */
@@ -2874,7 +2874,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
 		return -EBUSY;
 
 	dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
-		(read) ? "read" : "write", q->min_reqbufs_allocation, q->fileio_read_once,
+		(read) ? "read" : "write", q->rec_num_buffers, q->fileio_read_once,
 		q->fileio_write_immediately);
 
 	fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
@@ -2888,7 +2888,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
 	 * Request buffers and use MMAP type to force driver
 	 * to allocate buffers by itself.
 	 */
-	fileio->count = q->min_reqbufs_allocation;
+	fileio->count = q->rec_num_buffers;
 	fileio->memory = VB2_MEMORY_MMAP;
 	fileio->type = q->type;
 	q->fileio = fileio;
drivers/media/common/videobuf2/videobuf2-v4l2.c +1 -1
index 15394bb4168c..f4fdc5d77d9c 100644
@@ -705,7 +705,7 @@ static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory,
 	}
 	if (min_num_bufs && rec_num_bufs) {
 		*min_num_bufs = q->min_queued_buffers + 1;
-		*rec_num_bufs = q->min_reqbufs_allocation;
+		*rec_num_bufs = q->rec_num_buffers;
 		*caps |= V4L2_BUF_CAP_SUPPORTS_MIN_REC_NUM_BUFFERS;
 	}
 }
drivers/media/test-drivers/vicodec/vicodec-core.c +2 -2
index c45f5cf12ded..7e0b76606c8b 100644
@@ -1709,7 +1709,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 	if (ctx->is_enc) {
 		src_vq->lock = &ctx->dev->stateful_enc.mutex;
-		src_vq->min_reqbufs_allocation = VICODEC_REC_BUFS;
+		src_vq->rec_num_buffers = VICODEC_REC_BUFS;
 	} else if (ctx->is_stateless) {
 		src_vq->lock = &ctx->dev->stateless_dec.mutex;
 	} else {
@@ -1733,7 +1733,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 	dst_vq->lock = src_vq->lock;
 	if (!ctx->is_stateless && !ctx->is_enc)
-		dst_vq->min_reqbufs_allocation = VICODEC_REC_BUFS;
+		dst_vq->rec_num_buffers = VICODEC_REC_BUFS;
 
 	return vb2_queue_init(dst_vq);
 }
drivers/media/test-drivers/vimc/vimc-capture.c +1 -1
index 10df039278e7..1a1ca1d67fe6 100644
@@ -427,7 +427,7 @@ static struct vimc_ent_device *vimc_capture_add(struct vimc_device *vimc,
 	q->mem_ops = vimc_allocator == VIMC_ALLOCATOR_DMA_CONTIG
 		   ? &vb2_dma_contig_memops : &vb2_vmalloc_memops;
 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
-	q->min_reqbufs_allocation = 2;
+	q->rec_num_buffers = 2;
 	q->lock = &vcapture->lock;
 	q->dev = v4l2_dev->dev;
 
drivers/media/test-drivers/vivid/vivid-core.c +2 -2
index 7477ac8cb955..46ac3fd6e5dd 100644
@@ -887,7 +887,7 @@ static const struct media_device_ops vivid_media_ops = {
 static int vivid_create_queue(struct vivid_dev *dev,
 			      struct vb2_queue *q,
 			      u32 buf_type,
-			      unsigned int min_reqbufs_allocation,
+			      unsigned int rec_num_buffers,
 			      const struct vb2_ops *ops)
 {
 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->multiplanar)
@@ -924,7 +924,7 @@ static int vivid_create_queue(struct vivid_dev *dev,
 	q->mem_ops = allocators[dev->inst] == 1 ? &vb2_dma_contig_memops :
 						  &vb2_vmalloc_memops;
 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
-	q->min_reqbufs_allocation = min_reqbufs_allocation;
+	q->rec_num_buffers = rec_num_buffers;
 	q->lock = &dev->mutex;
 	q->dev = dev->v4l2_dev.dev;
 	q->supports_requests = supports_requests[dev->inst];
include/media/videobuf2-core.h +7 -5
index 9b02aeba4108..3282b55179a5 100644
@@ -555,14 +555,16 @@ struct vb2_buf_ops {
  *		VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1
  *		buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not
  *		modify the requested buffer count.
- * @min_reqbufs_allocation: the minimum number of buffers to be allocated when
- *		calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not*
- *		modify the requested buffer count and does not use this field.
+ * @rec_num_buffers: the recommended number of buffers to allocate for this
+ *		queue. This value is used when calling VIDIOC_REQBUFS: it will
+ *		allocate at least this many buffersa
+ *		Note that VIDIOC_CREATE_BUFS will *not* modify the requested
+ *		buffer count and it does not use this field.
  *		Drivers can set this if there has to be a certain number of
  *		buffers available for the hardware to work effectively.
  *		This allows calling VIDIOC_REQBUFS with a buffer count of 1 and
  *		it will be automatically adjusted to a workable	buffer count.
- *		If set, then @min_reqbufs_allocation must be larger than
+ *		If set, then @rec_num_buffers must be larger than
  *		@min_queued_buffers + 1.
  *		If this field is > 3, then it is highly recommended that the
  *		driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT
@@ -638,7 +640,7 @@ struct vb2_queue {
 	u32				timestamp_flags;
 	gfp_t				gfp_flags;
 	u32				min_queued_buffers;
-	u32				min_reqbufs_allocation;
+	u32				rec_num_buffers;
 
 	struct device			*alloc_devs[VB2_MAX_PLANES];
 
Signature · 1 line
2.45.2
#4 lore raw

RFC PATCH v2 4/5 media: vb2: introduce queue_info to replace queue_setup

In reply to Hans Verkuil

The new queue_info op just returns the number of planes and plane sizes for the currently configured format.

---
 .../media/common/videobuf2/videobuf2-core.c   | 82 ++++++++++++++-----
 include/media/videobuf2-core.h                | 10 +++
 2 files changed, 70 insertions(+), 22 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +60 -22
index 4b55280dc8b8..bd241fe9474b 100644
@@ -611,9 +611,9 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int start, unsigned i
 		if (unbalanced) {
 			pr_info("unbalanced counters for queue %p:\n", q);
 			if (q->cnt_start_streaming != q->cnt_stop_streaming)
-				pr_info("     setup: %u start_streaming: %u stop_streaming: %u\n",
-					q->cnt_queue_setup, q->cnt_start_streaming,
-					q->cnt_stop_streaming);
+				pr_info("     setup: %u info: %u start_streaming: %u stop_streaming: %u\n",
+					q->cnt_queue_setup, q->cnt_queue_info,
+					q->cnt_start_streaming, q->cnt_stop_streaming);
 			if (q->cnt_prepare_streaming != q->cnt_unprepare_streaming)
 				pr_info("     prepare_streaming: %u unprepare_streaming: %u\n",
 					q->cnt_prepare_streaming, q->cnt_unprepare_streaming);
@@ -622,6 +622,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsigned int start, unsigned i
 					q->cnt_wait_prepare, q->cnt_wait_finish);
 		}
 		q->cnt_queue_setup = 0;
+		q->cnt_queue_info = 0;
 		q->cnt_wait_prepare = 0;
 		q->cnt_wait_finish = 0;
 		q->cnt_prepare_streaming = 0;
@@ -914,7 +915,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 		q->is_busy = 0;
 		/*
 		 * In case of REQBUFS(0) return immediately without calling
-		 * driver's queue_setup() callback and allocating resources.
+		 * driver's queue_setup() or queue_info() callback and
+		 * allocating resources.
 		 */
 		if (*count == 0)
 			return 0;
@@ -928,7 +930,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 	memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
 	/*
 	 * Set this now to ensure that drivers see the correct q->memory value
-	 * in the queue_setup op.
+	 * in the queue_setup/info op.
 	 */
 	mutex_lock(&q->mmap_lock);
 	ret = vb2_core_allocated_buffers_storage(q);
@@ -938,12 +940,18 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 		return ret;
 	set_queue_coherency(q, non_coherent_mem);
 
-	/*
-	 * Ask the driver how many buffers and planes per buffer it requires.
-	 * Driver also sets the size and allocator context for each plane.
-	 */
-	ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
-		       plane_sizes, q->alloc_devs);
+	if (q->ops->queue_info) {
+		ret = call_qop(q, queue_info, q, &num_planes,
+			       plane_sizes, q->alloc_devs);
+	} else {
+		/*
+		 * Ask the driver how many buffers and planes per buffer it
+		 * requires. The driver also sets the size and (optionally)
+		 * the allocator context for each plane.
+		 */
+		ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
+			       plane_sizes, q->alloc_devs);
+	}
 	if (ret)
 		goto error;
 
@@ -980,7 +988,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 	/*
 	 * Check if driver can handle the allocated number of buffers.
 	 */
-	if (!ret && allocated_buffers < num_buffers) {
+	if (!ret && allocated_buffers < num_buffers && q->ops->queue_setup) {
 		num_buffers = allocated_buffers;
 		/*
 		 * num_planes is set by the previous queue_setup(), but since it
@@ -1082,17 +1090,44 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
 
 	num_buffers = min(*count, q->max_num_buffers - q_num_bufs);
 
-	if (requested_planes && requested_sizes) {
+	if (q->ops->queue_info) {
+		ret = call_qop(q, queue_info, q, &num_planes,
+			       plane_sizes, q->alloc_devs);
+
+		if (ret)
+			goto error;
+
+		/* Check that driver has set sane values */
+		if (WARN_ON(!num_planes)) {
+			ret = -EINVAL;
+			goto error;
+		}
+		if (num_planes != requested_planes) {
+			ret = -EINVAL;
+			goto error;
+		}
+
+		for (unsigned int i = 0; i < num_planes; i++) {
+			if (WARN_ON(!plane_sizes[i])) {
+				ret = -EINVAL;
+				goto error;
+			}
+			if (plane_sizes[i] > requested_sizes[i]) {
+				ret = -EINVAL;
+				goto error;
+			}
+			plane_sizes[i] = requested_sizes[i];
+		}
+	} else {
 		num_planes = requested_planes;
 		memcpy(plane_sizes, requested_sizes, sizeof(plane_sizes));
+		/*
+		 * Ask the driver, whether the requested number of buffers, planes per
+		 * buffer and their sizes are acceptable
+		 */
+		ret = call_qop(q, queue_setup, q, &num_buffers,
+			       &num_planes, plane_sizes, q->alloc_devs);
 	}
-
-	/*
-	 * Ask the driver, whether the requested number of buffers, planes per
-	 * buffer and their sizes are acceptable
-	 */
-	ret = call_qop(q, queue_setup, q, &num_buffers,
-		       &num_planes, plane_sizes, q->alloc_devs);
 	if (ret)
 		goto error;
 
@@ -1108,7 +1143,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
 	/*
 	 * Check if driver can handle the so far allocated number of buffers.
 	 */
-	if (allocated_buffers < num_buffers) {
+	if (allocated_buffers < num_buffers && q->ops->queue_setup) {
 		num_buffers = allocated_buffers;
 
 		/*
@@ -2619,10 +2654,13 @@ int vb2_core_queue_init(struct vb2_queue *q)
 	    WARN_ON(!q->mem_ops)	  ||
 	    WARN_ON(!q->type)		  ||
 	    WARN_ON(!q->io_modes)	  ||
-	    WARN_ON(!q->ops->queue_setup) ||
 	    WARN_ON(!q->ops->buf_queue))
 		return -EINVAL;
 
+	if (WARN_ON(!q->ops->queue_setup && !q->ops->queue_info) ||
+	    WARN_ON(q->ops->queue_setup && q->ops->queue_info))
+		return -EINVAL;
+
 	if (WARN_ON(q->max_num_buffers < VB2_MAX_FRAME) ||
 	    WARN_ON(q->min_queued_buffers > q->max_num_buffers))
 		return -EINVAL;
include/media/videobuf2-core.h +10 -0
index 3282b55179a5..89b7600e4cf6 100644
@@ -351,6 +351,13 @@ struct vb2_buffer {
  *			\*num_buffers are being allocated additionally to
  *			the buffers already allocated. If either \*num_planes
  *			or the requested sizes are invalid callback must return %-EINVAL.
+ * @queue_info:		called from VIDIOC_REQBUFS() and VIDIOC_CREATE_BUFS()
+ *			handlers before memory allocation.
+ *			The driver must return the required number of planes
+ *			per buffer in \*num_planes, and the size of each plane
+ *			must be set in the sizes\[\] array. Optionally set the
+ *			per-plane allocator specific device in the
+ *			alloc_devs\[\] array.
  * @wait_prepare:	release any locks taken while calling vb2 functions;
  *			it is called before an ioctl needs to wait for a new
  *			buffer to arrive; required to avoid a deadlock in
@@ -435,6 +442,8 @@ struct vb2_ops {
 	int (*queue_setup)(struct vb2_queue *q,
 			   unsigned int *num_buffers, unsigned int *num_planes,
 			   unsigned int sizes[], struct device *alloc_devs[]);
+	int (*queue_info)(struct vb2_queue *q, unsigned int *num_planes,
+			  unsigned int sizes[], struct device *alloc_devs[]);
 
 	void (*wait_prepare)(struct vb2_queue *q);
 	void (*wait_finish)(struct vb2_queue *q);
@@ -682,6 +691,7 @@ struct vb2_queue {
 	 * called. Used to check for unbalanced ops.
 	 */
 	u32				cnt_queue_setup;
+	u32				cnt_queue_info;
 	u32				cnt_wait_prepare;
 	u32				cnt_wait_finish;
 	u32				cnt_prepare_streaming;
Signature · 1 line
2.45.2
#5 lore raw

RFC PATCH v2 5/5 media: vivid: convert queue_setup to queue_info

In reply to Hans Verkuil

Use the new queue_info callback instead of queue_setup.

---
 .../media/test-drivers/vivid/vivid-meta-cap.c | 17 +++-----
 .../media/test-drivers/vivid/vivid-meta-out.c | 27 +++++--------
 .../media/test-drivers/vivid/vivid-sdr-cap.c  | 16 +++-----
 .../test-drivers/vivid/vivid-touch-cap.c      | 18 +++------
 .../media/test-drivers/vivid/vivid-vbi-cap.c  | 13 +++----
 .../media/test-drivers/vivid/vivid-vbi-out.c  | 13 +++----
 .../media/test-drivers/vivid/vivid-vid-cap.c  | 39 ++++++-------------
 .../media/test-drivers/vivid/vivid-vid-out.c  | 37 +++++-------------
 8 files changed, 56 insertions(+), 124 deletions(-)
drivers/media/test-drivers/vivid/vivid-meta-cap.c +5 -12
index c7aaecc0b5a2..90d3f6d74a21 100644
@@ -13,24 +13,17 @@
 #include "vivid-kthread-cap.h"
 #include "vivid-meta-cap.h"
 
-static int meta_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
-				unsigned int *nplanes, unsigned int sizes[],
-				struct device *alloc_devs[])
+static int meta_cap_queue_info(struct vb2_queue *vq,
+			       unsigned int *nplanes, unsigned int sizes[],
+			       struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
-	unsigned int size =  sizeof(struct vivid_uvc_meta_buf);
 
 	if (!vivid_is_webcam(dev))
 		return -EINVAL;
 
-	if (*nplanes) {
-		if (sizes[0] < size)
-			return -EINVAL;
-	} else {
-		sizes[0] = size;
-	}
-
 	*nplanes = 1;
+	sizes[0] = sizeof(struct vivid_uvc_meta_buf);
 	return 0;
 }
 
@@ -116,7 +109,7 @@ static void meta_cap_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_meta_cap_qops = {
-	.queue_setup		= meta_cap_queue_setup,
+	.queue_info		= meta_cap_queue_info,
 	.buf_prepare		= meta_cap_buf_prepare,
 	.buf_queue		= meta_cap_buf_queue,
 	.start_streaming	= meta_cap_start_streaming,
drivers/media/test-drivers/vivid/vivid-meta-out.c +10 -17
index 55e5e5dec2f2..432b0f6317bc 100644
@@ -13,24 +13,17 @@
 #include "vivid-kthread-out.h"
 #include "vivid-meta-out.h"
 
-static int meta_out_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
-				unsigned int *nplanes, unsigned int sizes[],
-				struct device *alloc_devs[])
+static int meta_out_queue_info(struct vb2_queue *vq,
+			       unsigned int *nplanes, unsigned int sizes[],
+			       struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
-	unsigned int size =  sizeof(struct vivid_meta_out_buf);
 
 	if (!vivid_is_webcam(dev))
 		return -EINVAL;
 
-	if (*nplanes) {
-		if (sizes[0] < size)
-			return -EINVAL;
-	} else {
-		sizes[0] = size;
-	}
-
 	*nplanes = 1;
+	sizes[0] = sizeof(struct vivid_meta_out_buf);
 	return 0;
 }
 
@@ -116,12 +109,12 @@ static void meta_out_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_meta_out_qops = {
-	.queue_setup            = meta_out_queue_setup,
-	.buf_prepare            = meta_out_buf_prepare,
-	.buf_queue              = meta_out_buf_queue,
-	.start_streaming        = meta_out_start_streaming,
-	.stop_streaming         = meta_out_stop_streaming,
-	.buf_request_complete   = meta_out_buf_request_complete,
+	.queue_info		= meta_out_queue_info,
+	.buf_prepare		= meta_out_buf_prepare,
+	.buf_queue		= meta_out_buf_queue,
+	.start_streaming	= meta_out_start_streaming,
+	.stop_streaming		= meta_out_stop_streaming,
+	.buf_request_complete	= meta_out_buf_request_complete,
 };
 
 int vidioc_enum_fmt_meta_out(struct file *file, void  *priv,
drivers/media/test-drivers/vivid/vivid-sdr-cap.c +6 -10
index 74a91d28c8be..ee94dcfb62f2 100644
@@ -214,18 +214,14 @@ static int vivid_thread_sdr_cap(void *data)
 	return 0;
 }
 
-static int sdr_cap_queue_setup(struct vb2_queue *vq,
-		       unsigned *nbuffers, unsigned *nplanes,
-		       unsigned sizes[], struct device *alloc_devs[])
+static int sdr_cap_queue_info(struct vb2_queue *vq,
+			      unsigned int *nplanes, unsigned int sizes[],
+			      struct device *alloc_devs[])
 {
-	/* 2 = max 16-bit sample returned */
-	u32 size = SDR_CAP_SAMPLES_PER_BUF * 2;
-
-	if (*nplanes)
-		return sizes[0] < size ? -EINVAL : 0;
 
 	*nplanes = 1;
-	sizes[0] = size;
+	/* 2 = max 16-bit sample returned */
+	sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
 	return 0;
 }
 
@@ -331,7 +327,7 @@ static void sdr_cap_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_sdr_cap_qops = {
-	.queue_setup		= sdr_cap_queue_setup,
+	.queue_info		= sdr_cap_queue_info,
 	.buf_prepare		= sdr_cap_buf_prepare,
 	.buf_queue		= sdr_cap_buf_queue,
 	.start_streaming	= sdr_cap_start_streaming,
drivers/media/test-drivers/vivid/vivid-touch-cap.c +5 -13
index 36a781fa17bc..49824674d0e9 100644
@@ -8,22 +8,14 @@
 #include "vivid-vid-common.h"
 #include "vivid-touch-cap.h"
 
-static int touch_cap_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
-				 unsigned int *nplanes, unsigned int sizes[],
-				 struct device *alloc_devs[])
+static int touch_cap_queue_info(struct vb2_queue *vq,
+				unsigned int *nplanes, unsigned int sizes[],
+				struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
-	struct v4l2_pix_format *f = &dev->tch_format;
-	unsigned int size = f->sizeimage;
-
-	if (*nplanes) {
-		if (*nplanes != 1)
-			return -EINVAL;
-		return sizes[0] < size ? -EINVAL : 0;
-	}
 
 	*nplanes = 1;
-	sizes[0] = size;
+	sizes[0] = dev->tch_format.sizeimage;
 	return 0;
 }
 
@@ -104,7 +96,7 @@ static void touch_cap_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_touch_cap_qops = {
-	.queue_setup		= touch_cap_queue_setup,
+	.queue_info		= touch_cap_queue_info,
 	.buf_prepare		= touch_cap_buf_prepare,
 	.buf_queue		= touch_cap_buf_queue,
 	.start_streaming	= touch_cap_start_streaming,
drivers/media/test-drivers/vivid/vivid-vbi-cap.c +5 -8
index a09f62c66c33..8d9696d5800a 100644
@@ -120,9 +120,9 @@ void vivid_sliced_vbi_cap_process(struct vivid_dev *dev,
 	}
 }
 
-static int vbi_cap_queue_setup(struct vb2_queue *vq,
-		       unsigned *nbuffers, unsigned *nplanes,
-		       unsigned sizes[], struct device *alloc_devs[])
+static int vbi_cap_queue_info(struct vb2_queue *vq,
+			      unsigned int *nplanes, unsigned int sizes[],
+			      struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
 	bool is_60hz = dev->std_cap[dev->input] & V4L2_STD_525_60;
@@ -133,11 +133,8 @@ static int vbi_cap_queue_setup(struct vb2_queue *vq,
 	if (!vivid_is_sdtv_cap(dev))
 		return -EINVAL;
 
-	if (*nplanes)
-		return sizes[0] < size ? -EINVAL : 0;
-	sizes[0] = size;
-
 	*nplanes = 1;
+	sizes[0] = size;
 	return 0;
 }
 
@@ -224,7 +221,7 @@ static void vbi_cap_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_vbi_cap_qops = {
-	.queue_setup		= vbi_cap_queue_setup,
+	.queue_info		= vbi_cap_queue_info,
 	.buf_prepare		= vbi_cap_buf_prepare,
 	.buf_queue		= vbi_cap_buf_queue,
 	.start_streaming	= vbi_cap_start_streaming,
drivers/media/test-drivers/vivid/vivid-vbi-out.c +5 -8
index b7a09d2f394e..99a5843e210a 100644
@@ -15,9 +15,9 @@
 #include "vivid-vbi-out.h"
 #include "vivid-vbi-cap.h"
 
-static int vbi_out_queue_setup(struct vb2_queue *vq,
-		       unsigned *nbuffers, unsigned *nplanes,
-		       unsigned sizes[], struct device *alloc_devs[])
+static int vbi_out_queue_info(struct vb2_queue *vq,
+			      unsigned int *nplanes, unsigned int sizes[],
+			      struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
 	bool is_60hz = dev->std_out & V4L2_STD_525_60;
@@ -28,11 +28,8 @@ static int vbi_out_queue_setup(struct vb2_queue *vq,
 	if (!vivid_is_svid_out(dev))
 		return -EINVAL;
 
-	if (*nplanes)
-		return sizes[0] < size ? -EINVAL : 0;
-	sizes[0] = size;
-
 	*nplanes = 1;
+	sizes[0] = size;
 	return 0;
 }
 
@@ -122,7 +119,7 @@ static void vbi_out_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_vbi_out_qops = {
-	.queue_setup		= vbi_out_queue_setup,
+	.queue_info		= vbi_out_queue_info,
 	.buf_prepare		= vbi_out_buf_prepare,
 	.buf_queue		= vbi_out_buf_queue,
 	.start_streaming	= vbi_out_start_streaming,
drivers/media/test-drivers/vivid/vivid-vid-cap.c +11 -28
index 27b1918b31c7..d03162887118 100644
@@ -72,12 +72,12 @@ static inline unsigned int webcam_ival_count(const struct vivid_dev *dev,
 	return ARRAY_SIZE(webcam_intervals);
 }
 
-static int vid_cap_queue_setup(struct vb2_queue *vq,
-		       unsigned *nbuffers, unsigned *nplanes,
-		       unsigned sizes[], struct device *alloc_devs[])
+static int vid_cap_queue_info(struct vb2_queue *vq,
+			      unsigned int *nplanes, unsigned int sizes[],
+			      struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
-	unsigned buffers = tpg_g_buffers(&dev->tpg);
+	unsigned int planes = tpg_g_buffers(&dev->tpg);
 	unsigned h = dev->fmt_cap_rect.height;
 	unsigned p;
 
@@ -98,30 +98,13 @@ static int vid_cap_queue_setup(struct vb2_queue *vq,
 		dev->queue_setup_error = false;
 		return -EINVAL;
 	}
-	if (*nplanes) {
-		/*
-		 * Check if the number of requested planes match
-		 * the number of buffers in the current format. You can't mix that.
-		 */
-		if (*nplanes != buffers)
-			return -EINVAL;
-		for (p = 0; p < buffers; p++) {
-			if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h /
-					dev->fmt_cap->vdownsampling[p] +
-					dev->fmt_cap->data_offset[p])
-				return -EINVAL;
-		}
-	} else {
-		for (p = 0; p < buffers; p++)
-			sizes[p] = (tpg_g_line_width(&dev->tpg, p) * h) /
-					dev->fmt_cap->vdownsampling[p] +
-					dev->fmt_cap->data_offset[p];
-	}
+	*nplanes = planes;
+	for (p = 0; p < planes; p++)
+		sizes[p] = (tpg_g_line_width(&dev->tpg, p) * h) /
+			dev->fmt_cap->vdownsampling[p] +
+			dev->fmt_cap->data_offset[p];
 
-	*nplanes = buffers;
-
-	dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
-	for (p = 0; p < buffers; p++)
+	for (p = 0; p < planes; p++)
 		dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
 
 	return 0;
@@ -250,7 +233,7 @@ static void vid_cap_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_vid_cap_qops = {
-	.queue_setup		= vid_cap_queue_setup,
+	.queue_info		= vid_cap_queue_info,
 	.buf_prepare		= vid_cap_buf_prepare,
 	.buf_finish		= vid_cap_buf_finish,
 	.buf_queue		= vid_cap_buf_queue,
drivers/media/test-drivers/vivid/vivid-vid-out.c +9 -28
index 5ec84db934d6..b183ee1b9048 100644
@@ -20,9 +20,9 @@
 #include "vivid-kthread-out.h"
 #include "vivid-vid-out.h"
 
-static int vid_out_queue_setup(struct vb2_queue *vq,
-		       unsigned *nbuffers, unsigned *nplanes,
-		       unsigned sizes[], struct device *alloc_devs[])
+static int vid_out_queue_info(struct vb2_queue *vq,
+			      unsigned int *nplanes, unsigned int sizes[],
+			      struct device *alloc_devs[])
 {
 	struct vivid_dev *dev = vb2_get_drv_priv(vq);
 	const struct vivid_fmt *vfmt = dev->fmt_out;
@@ -53,31 +53,12 @@ static int vid_out_queue_setup(struct vb2_queue *vq,
 		return -EINVAL;
 	}
 
-	if (*nplanes) {
-		/*
-		 * Check if the number of requested planes match
-		 * the number of planes in the current format. You can't mix that.
-		 */
-		if (*nplanes != planes)
-			return -EINVAL;
-		if (sizes[0] < size)
-			return -EINVAL;
-		for (p = 1; p < planes; p++) {
-			if (sizes[p] < dev->bytesperline_out[p] * h /
-					vfmt->vdownsampling[p] +
-					vfmt->data_offset[p])
-				return -EINVAL;
-		}
-	} else {
-		for (p = 0; p < planes; p++)
-			sizes[p] = p ? dev->bytesperline_out[p] * h /
-					vfmt->vdownsampling[p] +
-					vfmt->data_offset[p] : size;
-	}
-
 	*nplanes = planes;
+	for (p = 0; p < planes; p++)
+		sizes[p] = p ? dev->bytesperline_out[p] * h /
+			vfmt->vdownsampling[p] +
+			vfmt->data_offset[p] : size;
 
-	dprintk(dev, 1, "%s: count=%u\n", __func__, *nbuffers);
 	for (p = 0; p < planes; p++)
 		dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
 	return 0;
@@ -194,8 +175,8 @@ static void vid_out_buf_request_complete(struct vb2_buffer *vb)
 }
 
 const struct vb2_ops vivid_vid_out_qops = {
-	.queue_setup		= vid_out_queue_setup,
-	.buf_out_validate		= vid_out_buf_out_validate,
+	.queue_info		= vid_out_queue_info,
+	.buf_out_validate	= vid_out_buf_out_validate,
 	.buf_prepare		= vid_out_buf_prepare,
 	.buf_queue		= vid_out_buf_queue,
 	.start_streaming	= vid_out_start_streaming,
Signature · 1 line
2.45.2