Skip to contentSkip to conversation

Rework frame descriptors

PATCH Superseded Sakari Ailus 4 participants 35 messages May 13 – May 18

Subsystems (16)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 1/17 media: v4l2-common: Add mipi_csi2_dt_for_mbus()

In reply to Sakari Ailus

Add mipi_csi2_dt_for_mbus() for obtaining the MIPI CSI-2 data type (DT) for a given Media bus pixel code.

---
 drivers/media/v4l2-core/v4l2-common.c | 47 +++++++++++++++++++++++++++
 include/media/mipi-csi2.h             |  2 ++
 2 files changed, 49 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +47 -0
index bceafc4e92c8..3f40de563bbd 100644
@@ -46,6 +46,7 @@
 #include <linux/uaccess.h>
 #include <asm/io.h>
 #include <asm/div64.h>
+#include <media/mipi-csi2.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
@@ -808,3 +809,49 @@ struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id,
 	return clk_hw->clk;
 }
 EXPORT_SYMBOL_GPL(__devm_v4l2_sensor_clk_get);
+
+int mipi_csi2_dt_for_mbus(u32 code)
+{
+	switch (code) {
+	case MEDIA_BUS_FMT_BGR888_1X24:
+		return MIPI_CSI2_DT_RGB888;
+	case MEDIA_BUS_FMT_Y8_1X8:
+	case MEDIA_BUS_FMT_SBGGR8_1X8:
+	case MEDIA_BUS_FMT_SGBRG8_1X8:
+	case MEDIA_BUS_FMT_SGRBG8_1X8:
+	case MEDIA_BUS_FMT_SRGGB8_1X8:
+		return MIPI_CSI2_DT_RAW8;
+	case MEDIA_BUS_FMT_Y10_1X10:
+	case MEDIA_BUS_FMT_SBGGR10_1X10:
+	case MEDIA_BUS_FMT_SGBRG10_1X10:
+	case MEDIA_BUS_FMT_SGRBG10_1X10:
+	case MEDIA_BUS_FMT_SRGGB10_1X10:
+		return MIPI_CSI2_DT_RAW10;
+	case MEDIA_BUS_FMT_Y12_1X12:
+	case MEDIA_BUS_FMT_SBGGR12_1X12:
+	case MEDIA_BUS_FMT_SGBRG12_1X12:
+	case MEDIA_BUS_FMT_SGRBG12_1X12:
+	case MEDIA_BUS_FMT_SRGGB12_1X12:
+		return MIPI_CSI2_DT_RAW12;
+	case MEDIA_BUS_FMT_Y14_1X14:
+	case MEDIA_BUS_FMT_SBGGR14_1X14:
+	case MEDIA_BUS_FMT_SGBRG14_1X14:
+	case MEDIA_BUS_FMT_SGRBG14_1X14:
+	case MEDIA_BUS_FMT_SRGGB14_1X14:
+		return MIPI_CSI2_DT_RAW14;
+	case MEDIA_BUS_FMT_Y16_1X16:
+	case MEDIA_BUS_FMT_SBGGR16_1X16:
+	case MEDIA_BUS_FMT_SGBRG16_1X16:
+	case MEDIA_BUS_FMT_SGRBG16_1X16:
+	case MEDIA_BUS_FMT_SRGGB16_1X16:
+		return MIPI_CSI2_DT_RAW16;
+	case MEDIA_BUS_FMT_SBGGR20_1X20:
+	case MEDIA_BUS_FMT_SGBRG20_1X20:
+	case MEDIA_BUS_FMT_SGRBG20_1X20:
+	case MEDIA_BUS_FMT_SRGGB20_1X20:
+		return MIPI_CSI2_DT_RAW20;
+	default:
+		return -EINVAL;
+	}
+}
+EXPORT_SYMBOL_GPL(mipi_csi2_dt_for_mbus);
include/media/mipi-csi2.h +2 -0
index 40fc0264250d..0bea79ab66c6 100644
@@ -44,4 +44,6 @@
 #define MIPI_CSI2_DT_RAW20		0x2f
 #define MIPI_CSI2_DT_USER_DEFINED(n)	(0x30 + (n))	/* 0..7 */
 
+int mipi_csi2_dt_for_mbus(u32 code);
+
 #endif /* _MEDIA_MIPI_CSI2_H */
Signature · 1 line
2.47.3

Sakari Ailus

author maintainer
#0 lore raw

cover Rework frame descriptors

Hi folks,

This smallish set makes frame descriptors dynamically allocated and implements a single-entry frame descriptor based on the device's format, using a new helper called v4l2_subdev_get_frame_desc(). All drivers that do not obtain their frame descriptor from upstream are converted. The helper also obtains a frame descriptor for the desired type (parallel or CSI-2) and checks there's at least one entry there. These checks are removed from drivers that currently perform them. (Some drivers also check there's exactly a single frame descriptor entry but I think in most cases this check could be loosened. That could be done after this set.)

On callee side these patches introduce no changes as the number of pre-allocated memory for 8 frame descriptors remains as-is. The get_frame_desc() pad op can return more than 8 frame descriptors by setting the num_entries to the desired number and returning -ENOSPC.

If people prefer using cleanup.h / __free() to release the dynamically allocated array (I think I'd almost require that), I'll merge the now-separate __v4l2_subdev_get_frame_desc() into v4l2_subdev_get_frame_desc().

More formats can be added to df-to-mbus conversion as needed. These are meant to be initial formats that are enough for typical raw sensors (and one RGB format, too).

Sakari Ailus (17):
media: v4l2-common: Add mipi_csi2_dt_for_mbus()
media: v4l2-subdev: Align frame descriptor error codes with routing
media: v4l2-subdev: Prepare for changes in getting frame descriptors
media: v4l2-subdev: Allocate frame descriptors based on the need
media: v4l2-subdev: Provide a cleanup-friendly get_frame_desc
media: v4l2-subdev: Change the maximum number of routes
media: v4l2-subdev: Return dynamically allocated pass-through routes
media: v4l2-subdev: Always return at least one frame descriptor
media: bcm2835-unicam: Use v4l2_subdev_get_frame_desc()
media: nxp: imx8-isi: Use v4l2_subdev_get_frame_desc()
media: raspberrypi: cfe: Use v4l2_subdev_get_frame_desc()
media: rzg2l-cru: Use v4l2_subdev_get_frame_desc()
media: rkisp1: Use v4l2_subdev_get_frame_desc()
media: exynos4-is: Use v4l2_subdev_get_frame_desc()
media: ti: cal: Use v4l2_subdev_get_frame_desc()
media: ipu6: Use v4l2_subdev_get_frame_desc()
staging: media: ipu7: Use v4l2_subdev_get_frame_desc()

 drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c |  23 ++-
 .../media/platform/broadcom/bcm2835-unicam.c  |  20 +--
 .../platform/nxp/imx8-isi/imx8-isi-crossbar.c |  15 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c  |  29 ++--
 .../platform/renesas/rzg2l-cru/rzg2l-video.c  |  21 +--
 .../platform/rockchip/rkisp1/rkisp1-isp.c     |  12 +-
 .../samsung/exynos4-is/fimc-capture.c         |  16 +-
 drivers/media/platform/ti/cal/cal-camerarx.c  |  25 ++-
 drivers/media/v4l2-core/v4l2-common.c         |  47 ++++++
 drivers/media/v4l2-core/v4l2-subdev.c         | 156 +++++++++++++++---
 .../staging/media/ipu7/ipu7-isys-csi-phy.c    |  16 +-
 drivers/staging/media/ipu7/ipu7-isys-csi2.c   |  22 ++-
 include/media/mipi-csi2.h                     |   2 +
 include/media/v4l2-subdev.h                   |  76 ++++++++-
 14 files changed, 344 insertions(+), 136 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 2/17 media: v4l2-subdev: Align frame descriptor error codes with routing

__v4l2_subdev_get_frame_desc_passthrough() returns -ENOSPC when there are too many routes. There's a subtle difference when compared to -E2BIG, but -E2BIG can be used in this case as well. In practice this was unlikely to having been ever returned from the kernel as things currently stand.

-ENOSPC can be then repurposed for signalling of running out of entries in the statically allocated array.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-subdev.c +1 -1
index e9f81b9be9e2..d93ed50255ed 100644
@@ -2639,7 +2639,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 
 			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
-				return -ENOSPC;
+				return -E2BIG;
 			}
 
 			fd->entry[fd->num_entries] = *source_entry;
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 3/17 media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc() to both facilitate implementing drivers that need frame descriptors as well as prepare for having a larger number of frame descriptors. If the remote sub-device does not support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with a single entry) opportunistically, thus avoiding the need to add frame descriptor support to sensor drivers the device for which only generates a single stream, or managing the situation on the caller side.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 59 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 +++++++++
 2 files changed, 79 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +59 -0
index d93ed50255ed..b8acce8f9c33 100644
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2671,6 +2672,64 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	struct v4l2_subdev_format subdev_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = pad,
+	};
+	int ret;
+
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+		if (ret)
+			return ret;
+
+		if (desc->type != type) {
+			dev_dbg(sd->dev,
+				"wrong type of frame descriptor for pad %d (got %u, expected %u)\n",
+				pad, desc->type, type);
+			return -EINVAL;
+		}
+
+		return ret;
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
+	v4l2_subdev_unlock_state(state);
+	if (ret)
+		return ret;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = subdev_fmt.format.code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
+		int dt;
+
+		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
+		if (dt < 0)
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +20 -0
index d256b7ec8f84..c9e74566c85a 100644
@@ -1778,6 +1778,26 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 					   unsigned int pad,
 					   struct v4l2_mbus_frame_desc *fd);
 
+/**
+ * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
+ * @sd: The sub-device
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then a frame descriptor containing a single entry is created using the
+ * information from the sub-device format for types
+ * V4L2_MBUS_FRAME_DESC_TYPE_CSI2 and V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
Signature · 1 line
2.47.3
author maintainer
#13 lore raw

PATCH 4/17 media: v4l2-subdev: Allocate frame descriptors based on the need

In reply to Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +39 -10
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)
 		return ret;
 
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;
 
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +35 -9

77 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#23 lore raw

PATCH 9/17 media: bcm2835-unicam: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../media/platform/broadcom/bcm2835-unicam.c  | 20 +++++++++----------
 1 file changed, 9 insertions(+), 11 deletions(-)
drivers/media/platform/broadcom/bcm2835-unicam.c +9 -11
index 8d28ba0b59a3..2e78b16bf18c 100644
@@ -888,7 +888,8 @@ static int unicam_get_image_vc_dt(struct unicam_device *unicam,
 				  struct v4l2_subdev_state *state,
 				  u8 *vc, u8 *dt)
 {
-	struct v4l2_mbus_frame_desc fd;
+	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	u32 stream;
 	int ret;
 
@@ -898,17 +899,14 @@ static int unicam_get_image_vc_dt(struct unicam_device *unicam,
 	if (ret)
 		return ret;
 
-	ret = v4l2_subdev_call(unicam->sensor.subdev, pad, get_frame_desc,
-			       unicam->sensor.pad->index, &fd);
-	if (ret)
-		return ret;
-
-	/* Only CSI-2 supports DTs. */
-	if (fd.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
-		return -EINVAL;
+	fd = v4l2_subdev_get_frame_desc(unicam->sensor.subdev,
+					unicam->sensor.pad->index,
+					V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+	if (IS_ERR(fd))
+		return PTR_ERR(fd);
 
-	for (unsigned int i = 0; i < fd.num_entries; ++i) {
-		const struct v4l2_mbus_frame_desc_entry *fde = &fd.entry[i];
+	for (unsigned int i = 0; i < fd->num_entries; ++i) {
+		const struct v4l2_mbus_frame_desc_entry *fde = &fd->entry[i];
 
 		if (fde->stream == stream) {
 			*vc = fde->bus.csi2.vc;
Signature · 1 line
2.47.3
author maintainer
#24 lore raw

PATCH 10/17 media: nxp: imx8-isi: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../platform/nxp/imx8-isi/imx8-isi-crossbar.c     | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +9 -6
index 605a45124103..5bb536465c57 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2022 Laurent Pinchart <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
@@ -32,7 +33,8 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	struct mxc_isi_dev *isi = xbar->isi;
 	const struct mxc_gasket_ops *gasket_ops = isi->pdata->gasket_ops;
 	const struct v4l2_mbus_framefmt *fmt;
-	struct v4l2_mbus_frame_desc fd;
+	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	int ret;
 
 	if (!gasket_ops)
@@ -44,15 +46,16 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	 * to match the configuration of the CSIS.
 	 */
 
-	ret = v4l2_subdev_call(remote_sd, pad, get_frame_desc, remote_pad, &fd);
+	fd = v4l2_subdev_get_fmame_desc(remote_sd, remote_pad,
+					V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL);
 	if (ret) {
 		dev_err(isi->dev,
 			"failed to get frame descriptor from '%s':%u: %d\n",
-			remote_sd->name, remote_pad, ret);
-		return ret;
+			remote_sd->name, remote_pad, PTR_ERR(ret));
+		return PTR_ERR(ret);
 	}
 
-	if (fd.num_entries != 1) {
+	if (fd->num_entries != 1) {
 		dev_err(isi->dev, "invalid frame descriptor for '%s':%u\n",
 			remote_sd->name, remote_pad);
 		return -EINVAL;
@@ -62,7 +65,7 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	if (!fmt)
 		return -EINVAL;
 
-	gasket_ops->enable(isi, &fd, fmt, port);
+	gasket_ops->enable(isi, fd, fmt, port);
 	return 0;
 }
 
Signature · 1 line
2.47.3
author maintainer
#20 lore raw

PATCH 6/17 media: v4l2-subdev: Change the maximum number of routes

In reply to Sakari Ailus

As the frame descriptors are now allocated dynamically, allow as many routes that there can be dynamically allocated frame descriptors.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +1 -8
index a49aaad595af..43d8c0f8701f 100644
@@ -1100,14 +1100,7 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 				num_active_routes++;
 		}
 
-		/*
-		 * Drivers that implement routing need to report a frame
-		 * descriptor accordingly, with up to one entry per route. Until
-		 * the frame descriptors entries get allocated dynamically,
-		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
-		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
 			return -E2BIG;
 
 		/*
Signature · 1 line
2.47.3
author maintainer
#22 lore raw

PATCH 8/17 media: v4l2-subdev: Always return at least one frame descriptor

In reply to Sakari Ailus

Make v4l2_subdev_get_frame_desc() return at least one frame descriptor entry or an error. Empty frame descriptors aren't useful for callers so callers can now omit this check.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 5 +++++
 include/media/v4l2-subdev.h           | 2 ++
 2 files changed, 7 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +5 -0
index 9d7f616ab7ea..b92ae87fa5d0 100644
@@ -2709,6 +2709,11 @@ static int __v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad
 			return -EINVAL;
 		}
 
+		if (!desc->num_entries) {
+			dev_dbg(sd->dev, "no frame descriptor entries\n");
+			return -EINVAL;
+		}
+
 		return ret;
 	}
 
include/media/v4l2-subdev.h +2 -0
index 6e40420c2fcc..8b10dcab5590 100644
@@ -1816,6 +1816,8 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  * struct v4l2_mbus_frame_desc *desc __free(v4l2_subdev_free_frame_desc) =
  *	v4l2_subdev_get_frame_desc(sd, pad, desc);
  *
+ * The returned frame descriptor will contain at least one entry.
+ *
  * Return: The frame descriptor on success or a negative error code on failure.
  */
 struct v4l2_mbus_frame_desc *
Signature · 1 line
2.47.3
author maintainer
#21 lore raw

PATCH 7/17 media: v4l2-subdev: Return dynamically allocated pass-through routes

In reply to Sakari Ailus

Count the number of pass-through routes and then return the full table once enough memory is available for it.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +7 -5
index 43d8c0f8701f..9d7f616ab7ea 100644
@@ -2650,20 +2650,22 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
 
-			fd->entry[fd->num_entries] = *source_entry;
-
-			fd->entry[fd->num_entries].stream = route->source_stream;
+			if (fd->num_entries < fd->len_entries) {
+				fd->entry[fd->num_entries] = *source_entry;
+				fd->entry[fd->num_entries].stream =
+					route->source_stream;
+			}
 
 			fd->num_entries++;
 		}
 	}
 
-	return 0;
+	return fd->num_entries < fd->len_entries ? 0 : -ENOSPC;
 }
 EXPORT_SYMBOL_GPL(__v4l2_subdev_get_frame_desc_passthrough);
 
Signature · 1 line
2.47.3
author maintainer
#19 lore raw

PATCH 5/17 media: v4l2-subdev: Provide a cleanup-friendly get_frame_desc

In reply to Sakari Ailus

Provide a cleanup-friendly get_frame_desc() API function that returns a frame descriptor that can be automatically released using __free().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 36 ++++++++++++++++++++++++---
 include/media/v4l2-subdev.h           | 22 +++++++++++-----
 2 files changed, 49 insertions(+), 9 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +33 -3
index 012ff6fb2988..a49aaad595af 100644
@@ -2691,8 +2691,8 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
-int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
-			       struct v4l2_mbus_frame_desc *desc)
+static int __v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+					struct v4l2_mbus_frame_desc *desc)
 {
 	struct v4l2_subdev_format subdev_fmt = {
 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
@@ -2747,9 +2747,33 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 
 	return 0;
 }
+
+struct v4l2_mbus_frame_desc *
+v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			   enum v4l2_mbus_frame_desc_type type)
+{
+	struct v4l2_mbus_frame_desc *desc;
+	int ret;
+
+	desc = kzalloc_obj(*desc, GFP_KERNEL);
+	if (!desc)
+		return ERR_PTR(-ENOMEM);
+
+	desc->type = type;
+	desc->entry = desc->entry_mem;
+	desc->len_entries = ARRAY_SIZE(desc->entry_mem);
+
+	ret = __v4l2_subdev_get_frame_desc(sd, pad, desc);
+	if (ret) {
+		kfree(desc);
+		return ERR_PTR(ret);
+	}
+
+	return desc;
+}
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
-void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+static void __v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
 {
 	if (desc->entry != desc->entry_mem)
 		kfree(desc->entry);
@@ -2757,6 +2781,12 @@ void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
 	desc->entry = NULL;
 	desc->len_entries = desc->num_entries = 0;
 }
+
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{
+	__v4l2_subdev_free_frame_desc(desc);
+	kfree(desc);
+}
 EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
 
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
include/media/v4l2-subdev.h +16 -6
index bd97510c7024..6e40420c2fcc 100644
@@ -8,6 +8,7 @@
 #ifndef _V4L2_SUBDEV_H
 #define _V4L2_SUBDEV_H
 
+#include <linux/cleanup.h>
 #include <linux/types.h>
 #include <linux/v4l2-subdev.h>
 #include <media/media-entity.h>
@@ -1794,7 +1795,7 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
  * @sd: The sub-device
  * @pad: The number of the pad in @sd from which to obtain the frame descriptor
- * @desc: A pointer to a frame descriptor, with its type field set
+ * @type: The type of the frame descriptor
  *
  * Obtain a frame descriptor from a sub-device. If the sub-device supports the
  * get_frame_desc pad operation, its result is returned, just like calling it
@@ -1810,20 +1811,29 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  * each frame descriptor obtained by calling this function using
  * v4l2_subdev_free_frame_desc().
  *
- * Return: %0 on success or negative error code on failure.
+ * Use __free() to release the frame descriptor automatically:
+ *
+ * struct v4l2_mbus_frame_desc *desc __free(v4l2_subdev_free_frame_desc) =
+ *	v4l2_subdev_get_frame_desc(sd, pad, desc);
+ *
+ * Return: The frame descriptor on success or a negative error code on failure.
  */
-int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
-			       struct v4l2_mbus_frame_desc *desc);
+struct v4l2_mbus_frame_desc *
+v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			   enum v4l2_mbus_frame_desc_type type);
 
 /**
  * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
  * @desc: A pointer to a frame descriptor
  *
- * Release the frame descriptor entries in a frame descriptor. The number of
- * entries in the descriptor are set to 0 again.
+ * Release the frame descriptor entries and the frame descriptor itself.
  */
 void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
 
+DEFINE_FREE(v4l2_subdev_free_frame_desc, struct v4l2_mbus_frame_desc *, \
+	    if (!IS_ERR_OR_NULL(_T))					\
+		    v4l2_subdev_free_frame_desc(_T))
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
Signature · 1 line
2.47.3
author maintainer
#27 lore raw

PATCH 11/17 media: raspberrypi: cfe: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../media/platform/raspberrypi/rp1-cfe/cfe.c  | 29 +++++++++----------
 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +13 -16
index 8375ed3e97b9..111597c5b031 100644
@@ -6,6 +6,7 @@
  * Copyright (c) 2023-2024 Ideas on Board Oy
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
@@ -803,7 +804,8 @@ static int cfe_get_vc_dt_fallback(struct cfe_device *cfe, u8 *vc, u8 *dt)
 static int cfe_get_vc_dt(struct cfe_device *cfe, unsigned int channel, u8 *vc,
 			 u8 *dt)
 {
-	struct v4l2_mbus_frame_desc remote_desc;
+	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	struct v4l2_subdev_state *state;
 	u32 sink_stream;
 	unsigned int i;
@@ -816,34 +818,29 @@ static int cfe_get_vc_dt(struct cfe_device *cfe, unsigned int channel, u8 *vc,
 	if (ret)
 		return ret;
 
-	ret = v4l2_subdev_call(cfe->source_sd, pad, get_frame_desc,
-			       cfe->source_pad, &remote_desc);
-	if (ret == -ENOIOCTLCMD) {
+	fd = v4l2_subdev_get_frame_desc(cfe->source_sd, cfe->source_pad,
+					V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+	if (PTR_ERR(fd) == -ENOIOCTLCMD) {
 		cfe_dbg(cfe, "source does not support get_frame_desc, use fallback\n");
 		return cfe_get_vc_dt_fallback(cfe, vc, dt);
-	} else if (ret) {
+	} else if (IS_ERR(fd)) {
 		cfe_err(cfe, "Failed to get frame descriptor\n");
-		return ret;
-	}
-
-	if (remote_desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		cfe_err(cfe, "Frame descriptor does not describe CSI-2 link");
-		return -EINVAL;
+		return PTR_ERR(fd);
 	}
 
-	for (i = 0; i < remote_desc.num_entries; i++) {
-		if (remote_desc.entry[i].stream == sink_stream)
+	for (i = 0; i < fd->num_entries; i++) {
+		if (fd->entry[i].stream == sink_stream)
 			break;
 	}
 
-	if (i == remote_desc.num_entries) {
+	if (i == fd->num_entries) {
 		cfe_err(cfe, "Stream %u not found in remote frame desc\n",
 			sink_stream);
 		return -EINVAL;
 	}
 
-	*vc = remote_desc.entry[i].bus.csi2.vc;
-	*dt = remote_desc.entry[i].bus.csi2.dt;
+	*vc = fd->entry[i].bus.csi2.vc;
+	*dt = fd->entry[i].bus.csi2.dt;
 
 	return 0;
 }
Signature · 1 line
2.47.3
author maintainer
#28 lore raw

PATCH 12/17 media: rzg2l-cru: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../platform/renesas/rzg2l-cru/rzg2l-video.c  | 21 ++++---------------
 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/media/platform/renesas/rzg2l-cru/rzg2l-video.c +4 -17
index 162e2ace6931..96c899bb542a 100644
@@ -11,6 +11,7 @@
  * Copyright (C) 2008 Magnus Damm
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/pm_runtime.h>
@@ -411,24 +412,10 @@ static int rzg2l_cru_get_virtual_channel(struct rzg2l_cru_dev *cru)
 	int ret;
 
 	remote_pad = media_pad_remote_pad_unique(&cru->ip.pads[RZG2L_CRU_IP_SINK]);
-	ret = v4l2_subdev_call(cru->ip.remote, pad, get_frame_desc, remote_pad->index, &fd);
-	if (ret < 0 && ret != -ENOIOCTLCMD) {
-		dev_err(cru->dev, "get_frame_desc failed on IP remote subdev\n");
+	ret = v4l2_subdev_get_frame_desc(cru->ip.remote, remote_pad->index,
+					 V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+	if (ret < 0)
 		return ret;
-	}
-	/* If remote subdev does not implement .get_frame_desc default to VC0. */
-	if (ret == -ENOIOCTLCMD)
-		return 0;
-
-	if (fd.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		dev_err(cru->dev, "get_frame_desc returned invalid bus type %d\n", fd.type);
-		return -EINVAL;
-	}
-
-	if (!fd.num_entries) {
-		dev_err(cru->dev, "get_frame_desc returned zero entries\n");
-		return -EINVAL;
-	}
 
 	return fd.entry[0].bus.csi2.vc;
 }
Signature · 1 line
2.47.3
author maintainer
#29 lore raw

PATCH 13/17 media: rkisp1: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/media/platform/rockchip/rkisp1/rkisp1-isp.c +7 -5
index 2311672cedb1..7e94ff2037a6 100644
@@ -8,6 +8,7 @@
  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
  */
 
+#include <linux/cleanup.h>
 #include <linux/iopoll.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
@@ -87,7 +88,8 @@ static int rkisp1_gasket_enable(struct rkisp1_device *rkisp1,
 				struct media_pad *source)
 {
 	struct v4l2_subdev *source_sd;
-	struct v4l2_mbus_frame_desc fd;
+	struct v4l2_mbus_frame_desc *fd = __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	unsigned int dt;
 	u32 mask;
 	u32 val;
@@ -101,8 +103,8 @@ static int rkisp1_gasket_enable(struct rkisp1_device *rkisp1,
 	 */
 
 	source_sd = media_entity_to_v4l2_subdev(source->entity);
-	ret = v4l2_subdev_call(source_sd, pad, get_frame_desc,
-			       source->index, &fd);
+	fd = v4l2_subdev_get_frame_desc(source_sd, source->index,
+					V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
 	if (ret) {
 		dev_err(rkisp1->dev,
 			"failed to get frame descriptor from '%s':%u: %d\n",
@@ -110,13 +112,13 @@ static int rkisp1_gasket_enable(struct rkisp1_device *rkisp1,
 		return ret;
 	}
 
-	if (fd.num_entries != 1) {
+	if (fd->num_entries != 1) {
 		dev_err(rkisp1->dev, "invalid frame descriptor for '%s':%u\n",
 			source_sd->name, 0);
 		return -EINVAL;
 	}
 
-	dt = fd.entry[0].bus.csi2.dt;
+	dt = fd->entry[0].bus.csi2.dt;
 
 	if (rkisp1->gasket_id == 0) {
 		mask = ISP_DEWARP_CONTROL_MIPI_CSI1_HS_POLARITY
Signature · 1 line
2.47.3
author maintainer
#30 lore raw

PATCH 14/17 media: exynos4-is: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../platform/samsung/exynos4-is/fimc-capture.c   | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/media/platform/samsung/exynos4-is/fimc-capture.c +11 -5
index d85811f4b8c5..7f463871ea0c 100644
@@ -6,6 +6,7 @@
  * Sylwester Nawrocki <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
@@ -853,18 +854,23 @@ static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
 				      struct v4l2_plane_pix_format *plane_fmt,
 				      unsigned int num_planes, bool try)
 {
-	struct v4l2_mbus_frame_desc fd = { };
+	struct v4l2_mbus_frame_desc *alloc_fd
+		__free(v4l2_subdev_free_frame_desc) = NULL, fd = { };
 	int i, ret;
 	int pad;
 
 	for (i = 0; i < num_planes; i++)
-		fd.entry[i].length = plane_fmt[i].sizeimage;
+		set_fd.entry[i].length = plane_fmt[i].sizeimage;
 
 	pad = sensor->entity.num_pads - 1;
-	if (try)
+	if (try) {
 		ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
-	else
-		ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
+	} else {
+		alloc_fd = v4l2_subdev_get_frame_desc(sensor, pad,
+						      V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+		ret = PTR_ERR(alloc_fd);
+		fd = *alloc_fd;
+	}
 
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.47.3
author maintainer
#31 lore raw

PATCH 15/17 media: ti: cal: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 drivers/media/platform/ti/cal/cal-camerarx.c | 25 +++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)
drivers/media/platform/ti/cal/cal-camerarx.c +11 -14
index 00a71dac0ff4..8a81fecc00af 100644
@@ -9,6 +9,7 @@
  *	Laurent Pinchart <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/mfd/syscon.h>
@@ -872,7 +873,8 @@ static int cal_camerarx_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 				       struct v4l2_mbus_frame_desc *fd)
 {
 	struct cal_camerarx *phy = to_cal_camerarx(sd);
-	struct v4l2_mbus_frame_desc remote_desc;
+	struct v4l2_mbus_frame_desc *remote_desc
+		__free(v4l2_subdev_free_frame_desc) = NULL;
 	const struct media_pad *remote_pad;
 	struct v4l2_subdev_state *state;
 	u32 sink_stream;
@@ -893,24 +895,19 @@ static int cal_camerarx_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		goto out_unlock;
 	}
 
-	ret = v4l2_subdev_call(phy->source, pad, get_frame_desc,
-			       remote_pad->index, &remote_desc);
-	if (ret)
-		goto out_unlock;
-
-	if (remote_desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		cal_err(phy->cal,
-			"Frame descriptor does not describe CSI-2 link");
-		ret = -EINVAL;
+	remote_desc = v4l2_subdev_get_frame_desc(phy->source, remote_pad->index,
+						 V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+	if (IS_ERR(remote_desc)) {
+		ret = PTR_ERR(remote_desc);
 		goto out_unlock;
 	}
 
-	for (i = 0; i < remote_desc.num_entries; i++) {
-		if (remote_desc.entry[i].stream == sink_stream)
+	for (i = 0; i < remote_desc->num_entries; i++) {
+		if (remote_desc->entry[i].stream == sink_stream)
 			break;
 	}
 
-	if (i == remote_desc.num_entries) {
+	if (i == remote_desc->num_entries) {
 		cal_err(phy->cal, "Stream %u not found in remote frame desc\n",
 			sink_stream);
 		ret = -EINVAL;
@@ -919,7 +916,7 @@ static int cal_camerarx_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 
 	fd->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
 	fd->num_entries = 1;
-	fd->entry[0] = remote_desc.entry[i];
+	fd->entry[0] = remote_desc->entry[i];
 
 out_unlock:
 	v4l2_subdev_unlock_state(state);
Signature · 1 line
2.47.3
author maintainer
#32 lore raw

PATCH 16/17 media: ipu6: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c | 23 ++++++++-----------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c +10 -13
index 7e539a0c6c92..b47f65d7b668 100644
@@ -6,6 +6,7 @@
 #include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -600,11 +601,11 @@ int ipu6_isys_csi2_get_remote_desc(u32 source_stream,
 {
 	struct v4l2_mbus_frame_desc_entry *desc_entry = NULL;
 	struct device *dev = &csi2->isys->adev->auxdev.dev;
-	struct v4l2_mbus_frame_desc desc;
+	struct v4l2_mbus_frame_desc *desc __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	struct v4l2_subdev *source;
 	struct media_pad *pad;
 	unsigned int i;
-	int ret;
 
 	source = media_entity_to_v4l2_subdev(source_entity);
 	if (!source)
@@ -614,18 +615,14 @@ int ipu6_isys_csi2_get_remote_desc(u32 source_stream,
 	if (!pad)
 		return -EPIPE;
 
-	ret = v4l2_subdev_call(source, pad, get_frame_desc, pad->index, &desc);
-	if (ret)
-		return ret;
-
-	if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		dev_err(dev, "Unsupported frame descriptor type\n");
-		return -EINVAL;
-	}
+	desc = v4l2_subdev_get_frame_desc(source, pad->index,
+					  V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
+	if (IS_ERR(desc))
+		return PTR_ERR(desc);
 
-	for (i = 0; i < desc.num_entries; i++) {
-		if (source_stream == desc.entry[i].stream) {
-			desc_entry = &desc.entry[i];
+	for (i = 0; i < desc->num_entries; i++) {
+		if (source_stream == desc->entry[i].stream) {
+			desc_entry = &desc->entry[i];
 			break;
 		}
 	}
Signature · 1 line
2.47.3
author maintainer
#33 lore raw

PATCH 17/17 staging: media: ipu7: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../staging/media/ipu7/ipu7-isys-csi-phy.c    | 16 ++++++--------
 drivers/staging/media/ipu7/ipu7-isys-csi2.c   | 22 +++++++++----------
 2 files changed, 17 insertions(+), 21 deletions(-)
drivers/staging/media/ipu7/ipu7-isys-csi-phy.c +7 -9
index 3f15af3b4c79..75ac77e1b782 100644
@@ -5,6 +5,7 @@
 
 #include <linux/bitmap.h>
 #include <linux/bug.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/iopoll.h>
@@ -300,7 +301,8 @@ static int ipu7_isys_csi_ctrl_dids_config(struct ipu7_isys_csi2 *csi2, u32 id)
 {
 	struct v4l2_mbus_frame_desc_entry *desc_entry = NULL;
 	struct device *dev = &csi2->isys->adev->auxdev.dev;
-	struct v4l2_mbus_frame_desc desc;
+	struct v4l2_mbus_frame_desc *desc __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	struct v4l2_subdev *ext_sd;
 	struct media_pad *pad;
 	unsigned int i;
@@ -318,17 +320,13 @@ static int ipu7_isys_csi_ctrl_dids_config(struct ipu7_isys_csi2 *csi2, u32 id)
 		 pad->entity->name))
 		return -ENODEV;
 
-	ret = v4l2_subdev_call(ext_sd, pad, get_frame_desc, pad->index, &desc);
+	desc = v4l2_subdev_get_frame_desc(ext_sd, pad->index,
+					  V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
 	if (ret)
 		return ret;
 
-	if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		dev_warn(dev, "Unsupported frame descriptor type\n");
-		return -EINVAL;
-	}
-
-	for (i = 0; i < desc.num_entries; i++) {
-		desc_entry = &desc.entry[i];
+	for (i = 0; i < desc->num_entries; i++) {
+		desc_entry = &desc->entry[i];
 		if (desc_entry->bus.csi2.vc < IPU7_NR_OF_CSI2_VC) {
 			ret = __dids_config(csi2, id, desc_entry->bus.csi2.vc,
 					    desc_entry->bus.csi2.dt);
drivers/staging/media/ipu7/ipu7-isys-csi2.c +10 -12
index f34eabfe8a98..5a4a53ae1104 100644
@@ -6,6 +6,7 @@
 #include <linux/atomic.h>
 #include <linux/bits.h>
 #include <linux/bug.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/io.h>
@@ -491,7 +492,8 @@ int ipu7_isys_csi2_get_remote_desc(u32 source_stream,
 {
 	struct v4l2_mbus_frame_desc_entry *desc_entry = NULL;
 	struct device *dev = &csi2->isys->adev->auxdev.dev;
-	struct v4l2_mbus_frame_desc desc;
+	struct v4l2_mbus_frame_desc *desc __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	struct v4l2_subdev *source;
 	struct media_pad *pad;
 	unsigned int i;
@@ -505,18 +507,14 @@ int ipu7_isys_csi2_get_remote_desc(u32 source_stream,
 	if (!pad)
 		return -EPIPE;
 
-	ret = v4l2_subdev_call(source, pad, get_frame_desc, pad->index, &desc);
+	desc = v4l2_subdev_get_frame_desc(source, pad->index,
+					  V4L2_MBUS_FRAME_DESC_TYPE_CSI2);
 	if (ret)
 		return ret;
 
-	if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
-		dev_err(dev, "Unsupported frame descriptor type\n");
-		return -EINVAL;
-	}
-
-	for (i = 0; i < desc.num_entries; i++) {
-		if (source_stream == desc.entry[i].stream) {
-			desc_entry = &desc.entry[i];
+	for (i = 0; i < desc->num_entries; i++) {
+		if (source_stream == desc->entry[i].stream) {
+			desc_entry = &desc->entry[i];
 			break;
 		}
 	}
@@ -534,8 +532,8 @@ int ipu7_isys_csi2_get_remote_desc(u32 source_stream,
 
 	*entry = *desc_entry;
 
-	for (i = 0; i < desc.num_entries; i++) {
-		if (desc_entry->bus.csi2.vc == desc.entry[i].bus.csi2.vc)
+	for (i = 0; i < desc->num_entries; i++) {
+		if (desc_entry->bus.csi2.vc == desc->entry[i].bus.csi2.vc)
 			(*nr_queues)++;
 	}
 
Signature · 1 line
2.47.3
#34 lore raw
In reply to Sakari Ailus
Quoted 9 lines · Sakari Ailus

This smallish set makes frame descriptors dynamically allocated and implements a single-entry frame descriptor based on the device's format, using a new helper called v4l2_subdev_get_frame_desc(). All drivers that do not obtain their frame descriptor from upstream are converted. The helper also obtains a frame descriptor for the desired type (parallel or CSI-2) and checks there's at least one entry there. These checks are removed from drivers that currently perform them. (Some drivers also check there's exactly a single frame descriptor entry but I think in most cases this check could be loosened. That could be done after this set.)

Please ignore the driver patches for now. There are some issue remaining which I'll fix for v2.

Signature · 1 line
Sakari Ailus
maintainer
#2 lore raw

Re: [PATCH 01/17] media: v4l2-common: Add mipi_csi2_dt_for_mbus()

In reply to Sakari Ailus
Quoted 2 lines · Sakari Ailus

Add mipi_csi2_dt_for_mbus() for obtaining the MIPI CSI-2 data type (DT) for a given Media bus pixel code.

Thanks, I worked similar version before.
https://lore.kernel.org/linux-media/aP+isGnWmJ4tLXcs@lizhi-Precision-Tower-5810/

strange, I have not find updated one, I remember I posted. we may need more information, such as bpp.

Frank

Quoted 83 lines
---
 drivers/media/v4l2-core/v4l2-common.c | 47 +++++++++++++++++++++++++++
 include/media/mipi-csi2.h             |  2 ++
 2 files changed, 49 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +47 -0
index bceafc4e92c8..3f40de563bbd 100644
@@ -46,6 +46,7 @@
 #include <linux/uaccess.h>
 #include <asm/io.h>
 #include <asm/div64.h>
+#include <media/mipi-csi2.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
@@ -808,3 +809,49 @@ struct clk *__devm_v4l2_sensor_clk_get(struct device *dev, const char *id,
 	return clk_hw->clk;
 }
 EXPORT_SYMBOL_GPL(__devm_v4l2_sensor_clk_get);
+
+int mipi_csi2_dt_for_mbus(u32 code)
+{
+	switch (code) {
+	case MEDIA_BUS_FMT_BGR888_1X24:
+		return MIPI_CSI2_DT_RGB888;
+	case MEDIA_BUS_FMT_Y8_1X8:
+	case MEDIA_BUS_FMT_SBGGR8_1X8:
+	case MEDIA_BUS_FMT_SGBRG8_1X8:
+	case MEDIA_BUS_FMT_SGRBG8_1X8:
+	case MEDIA_BUS_FMT_SRGGB8_1X8:
+		return MIPI_CSI2_DT_RAW8;
+	case MEDIA_BUS_FMT_Y10_1X10:
+	case MEDIA_BUS_FMT_SBGGR10_1X10:
+	case MEDIA_BUS_FMT_SGBRG10_1X10:
+	case MEDIA_BUS_FMT_SGRBG10_1X10:
+	case MEDIA_BUS_FMT_SRGGB10_1X10:
+		return MIPI_CSI2_DT_RAW10;
+	case MEDIA_BUS_FMT_Y12_1X12:
+	case MEDIA_BUS_FMT_SBGGR12_1X12:
+	case MEDIA_BUS_FMT_SGBRG12_1X12:
+	case MEDIA_BUS_FMT_SGRBG12_1X12:
+	case MEDIA_BUS_FMT_SRGGB12_1X12:
+		return MIPI_CSI2_DT_RAW12;
+	case MEDIA_BUS_FMT_Y14_1X14:
+	case MEDIA_BUS_FMT_SBGGR14_1X14:
+	case MEDIA_BUS_FMT_SGBRG14_1X14:
+	case MEDIA_BUS_FMT_SGRBG14_1X14:
+	case MEDIA_BUS_FMT_SRGGB14_1X14:
+		return MIPI_CSI2_DT_RAW14;
+	case MEDIA_BUS_FMT_Y16_1X16:
+	case MEDIA_BUS_FMT_SBGGR16_1X16:
+	case MEDIA_BUS_FMT_SGBRG16_1X16:
+	case MEDIA_BUS_FMT_SGRBG16_1X16:
+	case MEDIA_BUS_FMT_SRGGB16_1X16:
+		return MIPI_CSI2_DT_RAW16;
+	case MEDIA_BUS_FMT_SBGGR20_1X20:
+	case MEDIA_BUS_FMT_SGBRG20_1X20:
+	case MEDIA_BUS_FMT_SGRBG20_1X20:
+	case MEDIA_BUS_FMT_SRGGB20_1X20:
+		return MIPI_CSI2_DT_RAW20;
+	default:
+		return -EINVAL;
+	}
+}
+EXPORT_SYMBOL_GPL(mipi_csi2_dt_for_mbus);
include/media/mipi-csi2.h +2 -1
index 40fc0264250d..0bea79ab66c6 100644
@@ -44,4 +44,6 @@
 #define MIPI_CSI2_DT_RAW20		0x2f
 #define MIPI_CSI2_DT_USER_DEFINED(n)	(0x30 + (n))	/* 0..7 */
 
+int mipi_csi2_dt_for_mbus(u32 code);
+
 #endif /* _MEDIA_MIPI_CSI2_H */
--

2.47.3

author maintainer
#3 lore raw

Re: [PATCH 01/17] media: v4l2-common: Add mipi_csi2_dt_for_mbus()

Hi Frank,

Quoted 9 lines · Frank Li
Quoted 2 lines · Sakari Ailus

Add mipi_csi2_dt_for_mbus() for obtaining the MIPI CSI-2 data type (DT) for a given Media bus pixel code.

Thanks, I worked similar version before.
https://lore.kernel.org/linux-media/aP+isGnWmJ4tLXcs@lizhi-Precision-Tower-5810/

strange, I have not find updated one, I remember I posted. we may need more information, such as bpp.

Ah, I remember this, too! I'd prefer your patch in fact, but it needs some cleanup first. I'd be interested in seeing the lost updated version of it. :-)

Signature · 3 lines
Kind regards,

Sakari Ailus
maintainer
#4 lore raw

Re: [PATCH 01/17] media: v4l2-common: Add mipi_csi2_dt_for_mbus()

Quoted 10 lines · Sakari Ailus

Hi Frank,

Quoted 7 lines · Frank Li
Quoted 2 lines · Sakari Ailus

Add mipi_csi2_dt_for_mbus() for obtaining the MIPI CSI-2 data type (DT) for a given Media bus pixel code.

Thanks, I worked similar version before.
https://lore.kernel.org/linux-media/aP+isGnWmJ4tLXcs@lizhi-Precision-Tower-5810/

Quoted 6 lines
Quoted 2 lines

strange, I have not find updated one, I remember I posted. we may need more information, such as bpp.

Ah, I remember this, too! I'd prefer your patch in fact, but it needs some cleanup first. I'd be interested in seeing the lost updated version of it. :-)

I just post single patch for your reference
https://lore.kernel.org/linux-media/[email protected]/T/#u

you can combine it into this thread, I need more time to work original imx6 stage threads, which is big changes.

Best regards
Frank

Quoted 5 lines

--
Kind regards,

Sakari Ailus

maintainer Reviewed-by on v1 2/17
#6 lore raw

Re: [PATCH 02/17] media: v4l2-subdev: Align frame descriptor error codes with routing

In reply to Sakari Ailus
Quoted 10 lines · Sakari Ailus

__v4l2_subdev_get_frame_desc_passthrough() returns -ENOSPC when there are too many routes. There's a subtle difference when compared to -E2BIG, but -E2BIG can be used in this case as well. In practice this was unlikely to having been ever returned from the kernel as things currently stand.

-ENOSPC can be then repurposed for signalling of running out of entries in the statically allocated array.

---

Quoted 19 lines
 drivers/media/v4l2-core/v4l2-subdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-subdev.c +1 -2
index e9f81b9be9e2..d93ed50255ed 100644
@@ -2639,7 +2639,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 
 			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
-				return -ENOSPC;
+				return -E2BIG;
 			}
 
 			fd->entry[fd->num_entries] = *source_entry;
--

2.47.3

maintainer Reviewed-by on v1 3/17
#8 lore raw

Re: [PATCH 03/17] media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Sakari Ailus
Quoted 11 lines · Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc() to both facilitate implementing drivers that need frame descriptors as well as prepare for having a larger number of frame descriptors. If the remote sub-device does not support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with a single entry) opportunistically, thus avoiding the need to add frame descriptor support to sensor drivers the device for which only generates a single stream, or managing the situation on the caller side.

---

Quoted 115 lines
 drivers/media/v4l2-core/v4l2-subdev.c | 59 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 +++++++++
 2 files changed, 79 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +59 -0
index d93ed50255ed..b8acce8f9c33 100644
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2671,6 +2672,64 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	struct v4l2_subdev_format subdev_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = pad,
+	};
+	int ret;
+
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+		if (ret)
+			return ret;
+
+		if (desc->type != type) {
+			dev_dbg(sd->dev,
+				"wrong type of frame descriptor for pad %d (got %u, expected %u)\n",
+				pad, desc->type, type);
+			return -EINVAL;
+		}
+
+		return ret;
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
+	v4l2_subdev_unlock_state(state);
+	if (ret)
+		return ret;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = subdev_fmt.format.code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
+		int dt;
+
+		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
+		if (dt < 0)
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +20 -1
index d256b7ec8f84..c9e74566c85a 100644
@@ -1778,6 +1778,26 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 					   unsigned int pad,
 					   struct v4l2_mbus_frame_desc *fd);
 
+/**
+ * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
+ * @sd: The sub-device
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then a frame descriptor containing a single entry is created using the
+ * information from the sub-device format for types
+ * V4L2_MBUS_FRAME_DESC_TYPE_CSI2 and V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

maintainer
#9 lore raw

Re: [PATCH 03/17] media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Sakari Ailus
Quoted 1 line · Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc()

Sorry, where v4l2_subdev_alloc_frame_desc() ? do you means v4l2_subdev_get_frame_desc() ?

Frank

Quoted 125 lines

to both facilitate implementing drivers that need frame descriptors as well as prepare for having a larger number of frame descriptors. If the remote sub-device does not support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with a single entry) opportunistically, thus avoiding the need to add frame descriptor support to sensor drivers the device for which only generates a single stream, or managing the situation on the caller side.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 59 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 +++++++++
 2 files changed, 79 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +59 -0
index d93ed50255ed..b8acce8f9c33 100644
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2671,6 +2672,64 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	struct v4l2_subdev_format subdev_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = pad,
+	};
+	int ret;
+
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+		if (ret)
+			return ret;
+
+		if (desc->type != type) {
+			dev_dbg(sd->dev,
+				"wrong type of frame descriptor for pad %d (got %u, expected %u)\n",
+				pad, desc->type, type);
+			return -EINVAL;
+		}
+
+		return ret;
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
+	v4l2_subdev_unlock_state(state);
+	if (ret)
+		return ret;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = subdev_fmt.format.code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
+		int dt;
+
+		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
+		if (dt < 0)
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +20 -1
index d256b7ec8f84..c9e74566c85a 100644
@@ -1778,6 +1778,26 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 					   unsigned int pad,
 					   struct v4l2_mbus_frame_desc *fd);
 
+/**
+ * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
+ * @sd: The sub-device
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then a frame descriptor containing a single entry is created using the
+ * information from the sub-device format for types
+ * V4L2_MBUS_FRAME_DESC_TYPE_CSI2 and V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

maintainer
#25 lore raw

Re: [PATCH 10/17] media: nxp: imx8-isi: Use v4l2_subdev_get_frame_desc()

In reply to Sakari Ailus
Quoted 31 lines · Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../platform/nxp/imx8-isi/imx8-isi-crossbar.c     | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +3 -1
index 605a45124103..5bb536465c57 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2022 Laurent Pinchart <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
@@ -32,7 +33,8 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	struct mxc_isi_dev *isi = xbar->isi;
 	const struct mxc_gasket_ops *gasket_ops = isi->pdata->gasket_ops;
 	const struct v4l2_mbus_framefmt *fmt;
-	struct v4l2_mbus_frame_desc fd;
+	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	int ret;
 
 	if (!gasket_ops)

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar, · line 50

Quoted 6 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
@@ -44,15 +46,16 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	 * to match the configuration of the CSIS.
 	 */
 
-	ret = v4l2_subdev_call(remote_sd, pad, get_frame_desc, remote_pad, &fd);
+	fd = v4l2_subdev_get_fmame_desc(remote_sd, remote_pad,
+					V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL);

cleanup.h perfer declear fd here

	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
		v4l2_subdev_get_fmame_desc()

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar, · line 51

Quoted 1 line
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +0 -0
 	if (ret) {
	ret is not set

	if (IS_ERR(fd))

Frank

Quoted 25 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +4 -5
 		dev_err(isi->dev,
 			"failed to get frame descriptor from '%s':%u: %d\n",
-			remote_sd->name, remote_pad, ret);
-		return ret;
+			remote_sd->name, remote_pad, PTR_ERR(ret));
+		return PTR_ERR(ret);
 	}
 
-	if (fd.num_entries != 1) {
+	if (fd->num_entries != 1) {
 		dev_err(isi->dev, "invalid frame descriptor for '%s':%u\n",
 			remote_sd->name, remote_pad);
 		return -EINVAL;
@@ -62,7 +65,7 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	if (!fmt)
 		return -EINVAL;
 
-	gasket_ops->enable(isi, &fd, fmt, port);
+	gasket_ops->enable(isi, fd, fmt, port);
 	return 0;
 }
 
--

2.47.3

maintainer
#11 lore raw

Re: [PATCH 03/17] media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Sakari Ailus

Hi Sakari

Quoted 3 lines · Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc() to both facilitate implementing drivers that need frame descriptors as well as prepare for having a larger number of frame descriptors.

As noticed by Frank, this line doesn't seem to match the patch content

Quoted 26 lines

If the remote sub-device does not support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with a single entry) opportunistically, thus avoiding the need to add frame descriptor support to sensor drivers the device for which only generates a single stream, or managing the situation on the caller side.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 59 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 +++++++++
 2 files changed, 79 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +1 -0
index d93ed50255ed..b8acce8f9c33 100644
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2671,6 +2672,64 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2682

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +8 -0
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	struct v4l2_subdev_format subdev_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = pad,
+	};
+	int ret;

is it worth being extra paranoid and add an

        if (WARN_ON(!desc))
                return -EINVAL;

here ?

And make immediately sure that desc->type is populated by checking for

        if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED)
                return -EINVAL;

or is passing UNDEFINED in supported ?

Quoted 6 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+		if (ret)

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2698

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +10 -0
+			return ret;
+
+		if (desc->type != type) {
+			dev_dbg(sd->dev,
+				"wrong type of frame descriptor for pad %d (got %u, expected %u)\n",
+				pad, desc->type, type);
+			return -EINVAL;
+		}
+
+		return ret;

You can return 0;

Quoted 18 lines
drivers/media/v4l2-core/v4l2-subdev.c +18 -0
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
+	v4l2_subdev_unlock_state(state);
+	if (ret)
+		return ret;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = subdev_fmt.format.code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2726

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +10 -0
+		int dt;
+
+		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
+		if (dt < 0)
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;

Can't you populate desc->entry[0] directly and avoid a copy ?

Quoted 14 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +0 -0
index d256b7ec8f84..c9e74566c85a 100644

include/media/v4l2-subdev.h int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 1783

Quoted 6 lines
include/media/v4l2-subdev.h +3 -0
@@ -1778,6 +1778,26 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 					   unsigned int pad,
 					   struct v4l2_mbus_frame_desc *fd);
 
+/**
+ * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
+ * @sd: The sub-device

The -remote- sub-device ?

Quoted 24 lines
include/media/v4l2-subdev.h +17 -1
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then a frame descriptor containing a single entry is created using the
+ * information from the sub-device format for types
+ * V4L2_MBUS_FRAME_DESC_TYPE_CSI2 and V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

maintainer
#14 lore raw

Re: [PATCH 04/17] media: v4l2-subdev: Allocate frame descriptors based on the need

In reply to Sakari Ailus

Hi Sakari

Quoted 48 lines · Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -4
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 372

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +7 -0
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {

nit: you can invert the condition and save one indentation level

                if (fd->num_entries <= V4L2_FRAME_DESC_ENTRY_PREALLOC ||
                    fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX)
                    return -E2BIG;
Quoted 14 lines
drivers/media/v4l2-core/v4l2-subdev.c +13 -0
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 393

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +4 -3
 		return ret;
 
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;

If we end up calling d->ops->pad->get_frame_desc() don't we overwrite the returned type ?

Quoted 25 lines
drivers/media/v4l2-core/v4l2-subdev.c +3 -3
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 2753

Quoted 5 lines
drivers/media/v4l2-core/v4l2-subdev.c +2 -0
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{

As this might be used as a __cleanup() should we check if desc is valid ?

Quoted 71 lines
drivers/media/v4l2-core/v4l2-subdev.c +8 -0
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +21 -9
index c9e74566c85a..bd97510c7024 100644
@@ -365,11 +365,13 @@ struct v4l2_mbus_frame_desc_entry {
 	} bus;
 };
 
- /*
-  * If this number is too small, it should be dropped altogether and the
-  * API switched to a dynamic number of frame descriptor entries.
-  */
-#define V4L2_FRAME_DESC_ENTRY_MAX	8
+/* Size of the statically allocated frame descriptor array. */
+#define V4L2_FRAME_DESC_ENTRY_PREALLOC	8
+/*
+ * Maximum number of dynamically allocated frame descriptors. Note that
+ * V4L2_SUBDEV_MAX_STREAM_ID is related to this limit as well.
+ */
+#define V4L2_FRAME_DESC_ENTRY_MAX	64
 
 /**
  * enum v4l2_mbus_frame_desc_type - media bus frame description type
@@ -392,13 +394,17 @@ enum v4l2_mbus_frame_desc_type {
 /**
  * struct v4l2_mbus_frame_desc - media bus data frame description
  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
- * @entry: frame descriptors array
- * @num_entries: number of entries in @entry array
+ * @entry_mem: memory for the frame descriptors (@entry)
+ * @entry: pointer to the frame descriptors
+ * @num_entries: number of entries in @entry
+ * @len_entries: number of entries allocated for @entry
  */
 struct v4l2_mbus_frame_desc {
 	enum v4l2_mbus_frame_desc_type type;
-	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
+	struct v4l2_mbus_frame_desc_entry entry_mem[V4L2_FRAME_DESC_ENTRY_PREALLOC];
+	struct v4l2_mbus_frame_desc_entry *entry;
 	unsigned short num_entries;
+	unsigned short len_entries;
 };
 
 /**
@@ -780,7 +786,13 @@ struct v4l2_subdev_state {
  * @link_validate: used by the media controller code to check if the links
  *		   that belongs to a pipeline can be used for stream.
  *
- * @get_frame_desc: get the current low level media bus frame parameters.
+ * @get_frame_desc: get the current low level media bus frame parameters. The
+ *		    callback is required to update the num_entries field to the
+ *		    total number of entries in the frame descriptor. The
+ *		    callback shall fill the first entries array up to
+ *		    len_entries, which signifies the number of entries
+ *		    allocated. If num_entries exceeds len_entries, the callback
+ *		    shall return -ENOSPC.
  *
  * @set_frame_desc: set the low level media bus frame parameters, @fd array
  *                  may be adjusted by the subdev driver to device capabilities.

include/media/v4l2-subdev.h int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 1809

Quoted 5 lines
include/media/v4l2-subdev.h +2 -0
@@ -1793,11 +1805,25 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  *
  * The caller is required to set @desc->type to the expected bus type.
  *
+ * The entries in the frame descriptor are allocated based on the need. The
+ * caller is required to release the memory of the frame descriptor entries for

Is it unconditionally true ? If fd->entry is not initialized aren't we deflecting to the pre-allocated entries in call_get_frame_desc() ?

	if (!fd->entry) {
		fd->entry = fd->entry_mem;
		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
	}

v4l2_subdev_free_frame_desc() shouldn't be called in this case, right ?

Quoted 24 lines
include/media/v4l2-subdev.h +12 -1
+ * each frame descriptor obtained by calling this function using
+ * v4l2_subdev_free_frame_desc().
+ *
  * Return: %0 on success or negative error code on failure.
  */
 int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *desc);
 
+/**
+ * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
+ * @desc: A pointer to a frame descriptor
+ *
+ * Release the frame descriptor entries in a frame descriptor. The number of
+ * entries in the descriptor are set to 0 again.
+ */
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

maintainer
#15 lore raw

Re: [PATCH 04/17] media: v4l2-subdev: Allocate frame descriptors based on the need

Hi Sakari

Quoted 124 lines · Jacopo Mondi

Hi Sakari

Quoted 48 lines · Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -4
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 372

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +7 -0
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {

nit: you can invert the condition and save one indentation level

                if (fd->num_entries <= V4L2_FRAME_DESC_ENTRY_PREALLOC ||
                    fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX)
                    return -E2BIG;
Quoted 14 lines
drivers/media/v4l2-core/v4l2-subdev.c +13 -0
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 393

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +4 -3
 		return ret;
 
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;

If we end up calling d->ops->pad->get_frame_desc() don't we overwrite the returned type ?

Quoted 28 lines
drivers/media/v4l2-core/v4l2-subdev.c +3 -3
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
Quoted 8 lines

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 2753

Quoted 2 lines
drivers/media/v4l2-core/v4l2-subdev.c +2 -0
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{

As this might be used as a __cleanup() should we check if desc is valid ?

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 2755

Quoted 2 lines
drivers/media/v4l2-core/v4l2-subdev.c +2 -0
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);

Sorry, in my below comment ....

Quoted 78 lines
Quoted 69 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +21 -9
index c9e74566c85a..bd97510c7024 100644
@@ -365,11 +365,13 @@ struct v4l2_mbus_frame_desc_entry {
 	} bus;
 };
 
- /*
-  * If this number is too small, it should be dropped altogether and the
-  * API switched to a dynamic number of frame descriptor entries.
-  */
-#define V4L2_FRAME_DESC_ENTRY_MAX	8
+/* Size of the statically allocated frame descriptor array. */
+#define V4L2_FRAME_DESC_ENTRY_PREALLOC	8
+/*
+ * Maximum number of dynamically allocated frame descriptors. Note that
+ * V4L2_SUBDEV_MAX_STREAM_ID is related to this limit as well.
+ */
+#define V4L2_FRAME_DESC_ENTRY_MAX	64
 
 /**
  * enum v4l2_mbus_frame_desc_type - media bus frame description type
@@ -392,13 +394,17 @@ enum v4l2_mbus_frame_desc_type {
 /**
  * struct v4l2_mbus_frame_desc - media bus data frame description
  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
- * @entry: frame descriptors array
- * @num_entries: number of entries in @entry array
+ * @entry_mem: memory for the frame descriptors (@entry)
+ * @entry: pointer to the frame descriptors
+ * @num_entries: number of entries in @entry
+ * @len_entries: number of entries allocated for @entry
  */
 struct v4l2_mbus_frame_desc {
 	enum v4l2_mbus_frame_desc_type type;
-	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
+	struct v4l2_mbus_frame_desc_entry entry_mem[V4L2_FRAME_DESC_ENTRY_PREALLOC];
+	struct v4l2_mbus_frame_desc_entry *entry;
 	unsigned short num_entries;
+	unsigned short len_entries;
 };
 
 /**
@@ -780,7 +786,13 @@ struct v4l2_subdev_state {
  * @link_validate: used by the media controller code to check if the links
  *		   that belongs to a pipeline can be used for stream.
  *
- * @get_frame_desc: get the current low level media bus frame parameters.
+ * @get_frame_desc: get the current low level media bus frame parameters. The
+ *		    callback is required to update the num_entries field to the
+ *		    total number of entries in the frame descriptor. The
+ *		    callback shall fill the first entries array up to
+ *		    len_entries, which signifies the number of entries
+ *		    allocated. If num_entries exceeds len_entries, the callback
+ *		    shall return -ENOSPC.
  *
  * @set_frame_desc: set the low level media bus frame parameters, @fd array
  *                  may be adjusted by the subdev driver to device capabilities.

include/media/v4l2-subdev.h int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 1809

Quoted 5 lines
include/media/v4l2-subdev.h +2 -0
@@ -1793,11 +1805,25 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  *
  * The caller is required to set @desc->type to the expected bus type.
  *
+ * The entries in the frame descriptor are allocated based on the need. The
+ * caller is required to release the memory of the frame descriptor entries for

Is it unconditionally true ? If fd->entry is not initialized aren't we deflecting to the pre-allocated entries in call_get_frame_desc() ?

Quoted 6 lines
	if (!fd->entry) {
		fd->entry = fd->entry_mem;
		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
	}

v4l2_subdev_free_frame_desc() shouldn't be called in this case, right ?

... here I missed this, so it's safe to call
v4l2_subdev_free_frame_desc() unconditionally

Quoted 26 lines
Quoted 26 lines
include/media/v4l2-subdev.h +12 -1
+ * each frame descriptor obtained by calling this function using
+ * v4l2_subdev_free_frame_desc().
+ *
  * Return: %0 on success or negative error code on failure.
  */
 int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *desc);
 
+/**
+ * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
+ * @desc: A pointer to a frame descriptor
+ *
+ * Release the frame descriptor entries in a frame descriptor. The number of
+ * entries in the descriptor are set to 0 again.
+ */
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

maintainer
#17 lore raw

Re: [PATCH 04/17] media: v4l2-subdev: Allocate frame descriptors based on the need

In reply to Sakari Ailus

Hi Sakari,

one more comment

Quoted 154 lines · Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +39 -10
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)
 		return ret;
 
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;
 
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +9 -7
index c9e74566c85a..bd97510c7024 100644
@@ -365,11 +365,13 @@ struct v4l2_mbus_frame_desc_entry {
 	} bus;
 };
 
- /*
-  * If this number is too small, it should be dropped altogether and the
-  * API switched to a dynamic number of frame descriptor entries.
-  */
-#define V4L2_FRAME_DESC_ENTRY_MAX	8
+/* Size of the statically allocated frame descriptor array. */
+#define V4L2_FRAME_DESC_ENTRY_PREALLOC	8
+/*
+ * Maximum number of dynamically allocated frame descriptors. Note that
+ * V4L2_SUBDEV_MAX_STREAM_ID is related to this limit as well.
+ */
+#define V4L2_FRAME_DESC_ENTRY_MAX	64
 
 /**
  * enum v4l2_mbus_frame_desc_type - media bus frame description type
@@ -392,13 +394,17 @@ enum v4l2_mbus_frame_desc_type {
 /**
  * struct v4l2_mbus_frame_desc - media bus data frame description
  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
- * @entry: frame descriptors array
- * @num_entries: number of entries in @entry array
+ * @entry_mem: memory for the frame descriptors (@entry)
+ * @entry: pointer to the frame descriptors

include/media/v4l2-subdev.h enum v4l2_mbus_frame_desc_type { · line 407

Quoted 10 lines
include/media/v4l2-subdev.h +5 -1
+ * @num_entries: number of entries in @entry
+ * @len_entries: number of entries allocated for @entry
  */
 struct v4l2_mbus_frame_desc {
 	enum v4l2_mbus_frame_desc_type type;
-	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
+	struct v4l2_mbus_frame_desc_entry entry_mem[V4L2_FRAME_DESC_ENTRY_PREALLOC];
+	struct v4l2_mbus_frame_desc_entry *entry;
 	unsigned short num_entries;
+	unsigned short len_entries;

What are the implications of this change on this part of v4l2_subdev_get_frame_desc() ?

	struct v4l2_subdev_state *state =
		v4l2_subdev_lock_and_get_active_state(sd);
	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
	v4l2_subdev_unlock_state(state);
	if (ret)
		return ret;

	struct v4l2_mbus_frame_desc_entry entry = {
		.pixelcode = subdev_fmt.format.code,
	};

	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
		int dt;

		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
		if (dt < 0)
			return dt;

		entry.bus.csi2.dt = dt;
	}

	desc->entry[0] = entry;

        ^
        |---- here ?

Is there any risk this now access invalid memory ?
Should we do the same thing that you're doing call_get_frame_desc() ?

	fd->entry = fd->entry_mem;

before assigning its first member ?

Quoted 47 lines
include/media/v4l2-subdev.h +21 -2
 };
 
 /**
@@ -780,7 +786,13 @@ struct v4l2_subdev_state {
  * @link_validate: used by the media controller code to check if the links
  *		   that belongs to a pipeline can be used for stream.
  *
- * @get_frame_desc: get the current low level media bus frame parameters.
+ * @get_frame_desc: get the current low level media bus frame parameters. The
+ *		    callback is required to update the num_entries field to the
+ *		    total number of entries in the frame descriptor. The
+ *		    callback shall fill the first entries array up to
+ *		    len_entries, which signifies the number of entries
+ *		    allocated. If num_entries exceeds len_entries, the callback
+ *		    shall return -ENOSPC.
  *
  * @set_frame_desc: set the low level media bus frame parameters, @fd array
  *                  may be adjusted by the subdev driver to device capabilities.
@@ -1793,11 +1805,25 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  *
  * The caller is required to set @desc->type to the expected bus type.
  *
+ * The entries in the frame descriptor are allocated based on the need. The
+ * caller is required to release the memory of the frame descriptor entries for
+ * each frame descriptor obtained by calling this function using
+ * v4l2_subdev_free_frame_desc().
+ *
  * Return: %0 on success or negative error code on failure.
  */
 int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *desc);
 
+/**
+ * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
+ * @desc: A pointer to a frame descriptor
+ *
+ * Release the frame descriptor entries in a frame descriptor. The number of
+ * entries in the descriptor are set to 0 again.
+ */
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
--

2.47.3

author maintainer
#26 lore raw

Re: [PATCH 10/17] media: nxp: imx8-isi: Use v4l2_subdev_get_frame_desc()

In reply to Frank Li

Hi Frank,

Quoted 35 lines · Frank Li
Quoted 34 lines · Sakari Ailus

Call v4l2_subdev_get_frame_desc() to obtain the frame descriptor. This is preferred over calling the get_frame_desc() pad operation directly.

---
 .../platform/nxp/imx8-isi/imx8-isi-crossbar.c     | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +3 -1
index 605a45124103..5bb536465c57 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2022 Laurent Pinchart <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
@@ -32,7 +33,8 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	struct mxc_isi_dev *isi = xbar->isi;
 	const struct mxc_gasket_ops *gasket_ops = isi->pdata->gasket_ops;
 	const struct v4l2_mbus_framefmt *fmt;
-	struct v4l2_mbus_frame_desc fd;
+	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
+		NULL;
 	int ret;
 
 	if (!gasket_ops)
@@ -44,15 +46,16 @@ static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar,
 	 * to match the configuration of the CSIS.
 	 */
 
Quoted 8 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c static int mxc_isi_crossbar_gasket_enable(struct mxc_isi_crossbar *xbar, · line 50

Quoted 3 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
-	ret = v4l2_subdev_call(remote_sd, pad, get_frame_desc, remote_pad, &fd);
+	fd = v4l2_subdev_get_fmame_desc(remote_sd, remote_pad,
+					V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL);

cleanup.h perfer declear fd here

	struct v4l2_mbus_frame_desc *fd __free(v4l2_subdev_free_frame_desc) =
		v4l2_subdev_get_fmame_desc()

That would indeed make sense. I'll take this into account for v2.

Signature · 3 lines
Regards,

Sakari Ailus
author maintainer
#12 lore raw

Re: [PATCH 03/17] media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Jacopo Mondi

Hi Jacopo,

Quoted 8 lines · Jacopo Mondi

Hi Sakari

Quoted 3 lines · Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc() to both facilitate implementing drivers that need frame descriptors as well as prepare for having a larger number of frame descriptors.

As noticed by Frank, this line doesn't seem to match the patch content

Indeed. I'll fix this for v2.

Quoted 40 lines
Quoted 27 lines

If the remote sub-device does not support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with a single entry) opportunistically, thus avoiding the need to add frame descriptor support to sensor drivers the device for which only generates a single stream, or managing the situation on the caller side.

---
 drivers/media/v4l2-core/v4l2-subdev.c | 59 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 +++++++++
 2 files changed, 79 insertions(+)
drivers/media/v4l2-core/v4l2-subdev.c +1 -0
index d93ed50255ed..b8acce8f9c33 100644
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2671,6 +2672,64 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 }

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2682

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +8 -0
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc_passthrough);
 
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	struct v4l2_subdev_format subdev_fmt = {
+		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = pad,
+	};
+	int ret;

is it worth being extra paranoid and add an

Quoted 2 lines
        if (WARN_ON(!desc))
                return -EINVAL;

In v2 the frame descriptor will be returned to the caller.

Quoted 9 lines

here ?

And make immediately sure that desc->type is populated by checking for

        if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED)
                return -EINVAL;

or is passing UNDEFINED in supported ?

In fact for v4l2_subdev_get_frame_desc() we should require either of the supported types. I'll move the descriptor type check here.

Quoted 11 lines
Quoted 11 lines
drivers/media/v4l2-core/v4l2-subdev.c +10 -0
+
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+		if (ret)
+			return ret;
+
+		if (desc->type != type) {
+			dev_dbg(sd->dev,
Quoted 8 lines

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2698

Quoted 6 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+				"wrong type of frame descriptor for pad %d (got %u, expected %u)\n",
+				pad, desc->type, type);
+			return -EINVAL;
+		}
+
+		return ret;

You can return 0;

Yes.

Quoted 23 lines
Quoted 23 lines
drivers/media/v4l2-core/v4l2-subdev.c +22 -0
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
+	v4l2_subdev_unlock_state(state);
+	if (ret)
+		return ret;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = subdev_fmt.format.code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
+		int dt;
+
+		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
+		if (dt < 0)
Quoted 8 lines

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 2726

Quoted 6 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;

Can't you populate desc->entry[0] directly and avoid a copy ?

That'd work, yes. I'll change this for v2.

Quoted 18 lines
Quoted 18 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +0 -0
index d256b7ec8f84..c9e74566c85a 100644
@@ -1778,6 +1778,26 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 					   unsigned int pad,
 					   struct v4l2_mbus_frame_desc *fd);
 
Quoted 4 lines

include/media/v4l2-subdev.h int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 1783

Quoted 3 lines
include/media/v4l2-subdev.h +3 -0
+/**
+ * v4l2_subdev_get_frame_desc() - Get a frame descriptor for a pad
+ * @sd: The sub-device

The -remote- sub-device ?

The API is meant for all sub-devices, in most cases it's remote but not always. Getting a frame descriptor from the local sub-device likely has no use cases but I wouldn't document that here.

Quoted 21 lines
Quoted 21 lines
include/media/v4l2-subdev.h +17 -0
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then a frame descriptor containing a single entry is created using the
+ * information from the sub-device format for types
+ * V4L2_MBUS_FRAME_DESC_TYPE_CSI2 and V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#10 lore raw

Re: [PATCH 03/17] media: v4l2-subdev: Prepare for changes in getting frame descriptors

In reply to Frank Li

Hi Frank,

Thanks for the review.

Quoted 5 lines · Frank Li
Quoted 1 line · Sakari Ailus

Introduce v4l2_subdev_alloc_frame_desc() and v4l2_subdev_free_frame_desc()

Sorry, where v4l2_subdev_alloc_frame_desc() ? do you means v4l2_subdev_get_frame_desc() ?

Yes, I renamed the function and forgot to update it here. I'll fix this for v2.

Signature · 1 line
Sakari Ailus
author maintainer
#16 lore raw

Re: [PATCH 04/17] media: v4l2-subdev: Allocate frame descriptors based on the need

In reply to Jacopo Mondi

Hi Jacopo,

Thanks for the review.

Quoted 66 lines · Jacopo Mondi

Hi Sakari

Quoted 63 lines · Jacopo Mondi

Hi Sakari

Quoted 48 lines · Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -4
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 372

Quoted 10 lines
drivers/media/v4l2-core/v4l2-subdev.c +7 -0
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {

nit: you can invert the condition and save one indentation level

I agree.

Quoted 21 lines
Quoted 21 lines
                if (fd->num_entries <= V4L2_FRAME_DESC_ENTRY_PREALLOC ||
                    fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX)
                    return -E2BIG;
Quoted 16 lines
drivers/media/v4l2-core/v4l2-subdev.c +13 -0
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)
 		return ret;
 
Quoted 11 lines
Quoted 11 lines

drivers/media/v4l2-core/v4l2-subdev.c static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 393

Quoted 8 lines
drivers/media/v4l2-core/v4l2-subdev.c +4 -3
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;

If we end up calling d->ops->pad->get_frame_desc() don't we overwrite the returned type ?

Good point. I'll instead check the type remains what was expected.

This in fact requires some compatibility code until all the callers have been converted.

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
drivers/media/v4l2-core/v4l2-subdev.c +3 -3
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
Quoted 5 lines
Quoted 5 lines

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 2753

Quoted 2 lines
drivers/media/v4l2-core/v4l2-subdev.c +2 -0
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{

As this might be used as a __cleanup() should we check if desc is valid ?

There's a check in the caller -- see include/linux/cleanup.h around line 192.

Quoted 90 lines
Quoted 3 lines

drivers/media/v4l2-core/v4l2-subdev.c int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, · line 2755

Quoted 3 lines
drivers/media/v4l2-core/v4l2-subdev.c +2 -0
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);

Sorry, in my below comment ....

Quoted 83 lines
Quoted 69 lines
drivers/media/v4l2-core/v4l2-subdev.c +6 -0
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +21 -9
index c9e74566c85a..bd97510c7024 100644
@@ -365,11 +365,13 @@ struct v4l2_mbus_frame_desc_entry {
 	} bus;
 };
 
- /*
-  * If this number is too small, it should be dropped altogether and the
-  * API switched to a dynamic number of frame descriptor entries.
-  */
-#define V4L2_FRAME_DESC_ENTRY_MAX	8
+/* Size of the statically allocated frame descriptor array. */
+#define V4L2_FRAME_DESC_ENTRY_PREALLOC	8
+/*
+ * Maximum number of dynamically allocated frame descriptors. Note that
+ * V4L2_SUBDEV_MAX_STREAM_ID is related to this limit as well.
+ */
+#define V4L2_FRAME_DESC_ENTRY_MAX	64
 
 /**
  * enum v4l2_mbus_frame_desc_type - media bus frame description type
@@ -392,13 +394,17 @@ enum v4l2_mbus_frame_desc_type {
 /**
  * struct v4l2_mbus_frame_desc - media bus data frame description
  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
- * @entry: frame descriptors array
- * @num_entries: number of entries in @entry array
+ * @entry_mem: memory for the frame descriptors (@entry)
+ * @entry: pointer to the frame descriptors
+ * @num_entries: number of entries in @entry
+ * @len_entries: number of entries allocated for @entry
  */
 struct v4l2_mbus_frame_desc {
 	enum v4l2_mbus_frame_desc_type type;
-	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
+	struct v4l2_mbus_frame_desc_entry entry_mem[V4L2_FRAME_DESC_ENTRY_PREALLOC];
+	struct v4l2_mbus_frame_desc_entry *entry;
 	unsigned short num_entries;
+	unsigned short len_entries;
 };
 
 /**
@@ -780,7 +786,13 @@ struct v4l2_subdev_state {
  * @link_validate: used by the media controller code to check if the links
  *		   that belongs to a pipeline can be used for stream.
  *
- * @get_frame_desc: get the current low level media bus frame parameters.
+ * @get_frame_desc: get the current low level media bus frame parameters. The
+ *		    callback is required to update the num_entries field to the
+ *		    total number of entries in the frame descriptor. The
+ *		    callback shall fill the first entries array up to
+ *		    len_entries, which signifies the number of entries
+ *		    allocated. If num_entries exceeds len_entries, the callback
+ *		    shall return -ENOSPC.
  *
  * @set_frame_desc: set the low level media bus frame parameters, @fd array
  *                  may be adjusted by the subdev driver to device capabilities.

include/media/v4l2-subdev.h int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd, · line 1809

Quoted 5 lines
include/media/v4l2-subdev.h +2 -0
@@ -1793,11 +1805,25 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  *
  * The caller is required to set @desc->type to the expected bus type.
  *
+ * The entries in the frame descriptor are allocated based on the need. The
+ * caller is required to release the memory of the frame descriptor entries for

Is it unconditionally true ? If fd->entry is not initialized aren't we deflecting to the pre-allocated entries in call_get_frame_desc() ?

	if (!fd->entry) {
		fd->entry = fd->entry_mem;
		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
	}
Quoted 4 lines
Quoted 1 line

v4l2_subdev_free_frame_desc() shouldn't be called in this case, right ?

... here I missed this, so it's safe to call
v4l2_subdev_free_frame_desc() unconditionally

Correct.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
include/media/v4l2-subdev.h +12 -0
+ * each frame descriptor obtained by calling this function using
+ * v4l2_subdev_free_frame_desc().
+ *
  * Return: %0 on success or negative error code on failure.
  */
 int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *desc);
 
+/**
+ * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
+ * @desc: A pointer to a frame descriptor
+ *
+ * Release the frame descriptor entries in a frame descriptor. The number of
+ * entries in the descriptor are set to 0 again.
+ */
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#18 lore raw

Re: [PATCH 04/17] media: v4l2-subdev: Allocate frame descriptors based on the need

In reply to Jacopo Mondi

Hi Jacopo,

Quoted 199 lines · Jacopo Mondi

Hi Sakari,

one more comment

Quoted 154 lines · Sakari Ailus

Frame descriptors entries require a small amount of memory per entry (20 bytes), but if the number of entries in a frame descriptor is large, an unreasonably large amount of memory would need to be allocated in the stack. Therefore the number of entries has been limited to 8.

Support larger frame descriptors by allocating as much memory as required. The get_frame_desc() op can now set the num_entries to a number larger than V4L2_FRAME_BUS_ENTRY_MAX and return -ENOSPC. The caller, v4l2_subdev_get_frame_desc(), will then allocate memory for that amount of memory and call the get_frame_desc() op again.

The caller is also responsible for releasing the allocated memory by calling v4l2_subdev_free_frame_desc().

---
 drivers/media/v4l2-core/v4l2-subdev.c | 49 +++++++++++++++++++++------
 include/media/v4l2-subdev.h           | 44 +++++++++++++++++++-----
 2 files changed, 74 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +39 -10
index b8acce8f9c33..012ff6fb2988 100644
@@ -63,10 +63,6 @@ static bool v4l2_subdev_enable_streams_api;
 /*
  * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
  */
 #define V4L2_SUBDEV_MAX_STREAM_ID 63
 
@@ -354,6 +350,7 @@ static int call_set_frame_interval(struct v4l2_subdev *sd,
 static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *fd)
 {
+	unsigned int type;
 	unsigned int i;
 	int ret;
 
@@ -362,16 +359,38 @@ static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 		return -EOPNOTSUPP;
 #endif
 
+	type = fd->type;
 	memset(fd, 0, sizeof(*fd));
+	if (!fd->entry) {
+		fd->entry = fd->entry_mem;
+		fd->len_entries = ARRAY_SIZE(fd->entry_mem);
+	}
 
 	ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+	if (ret == -ENOSPC) {
+		if (fd->num_entries > V4L2_FRAME_DESC_ENTRY_PREALLOC &&
+		    fd->num_entries < V4L2_FRAME_DESC_ENTRY_MAX) {
+			fd->entry = kzalloc_objs(*fd->entry, fd->num_entries,
+						 GFP_KERNEL);
+			if (!fd->entry)
+				return -ENOMEM;
+
+			fd->len_entries = fd->num_entries;
+			fd->num_entries = 0;
+
+			ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
+		} else {
+			return -E2BIG;
+		}
+	}
 	if (ret)
 		return ret;
 
 	dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
-		fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
-		"unknown");
+		type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
+		type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" : "unknown");
+
+	fd->type = type;
 
 	for (i = 0; i < fd->num_entries; i++) {
 		struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
@@ -1086,9 +1105,9 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
 		 * descriptor accordingly, with up to one entry per route. Until
 		 * the frame descriptors entries get allocated dynamically,
 		 * limit the number of active routes to
-		 * V4L2_FRAME_DESC_ENTRY_MAX.
+		 * V4L2_FRAME_DESC_ENTRY_PREALLOC.
 		 */
-		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_MAX)
+		if (num_active_routes > V4L2_FRAME_DESC_ENTRY_PREALLOC)
 			return -E2BIG;
 
 		/*
@@ -2638,7 +2657,7 @@ int __v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
 				return -EPIPE;
 			}
 
-			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_MAX) {
+			if (fd->num_entries >= V4L2_FRAME_DESC_ENTRY_PREALLOC) {
 				dev_dbg(dev, "Frame desc entry limit reached\n");
 				return -E2BIG;
 			}
@@ -2730,6 +2749,16 @@ int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
 
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc)
+{
+	if (desc->entry != desc->entry_mem)
+		kfree(desc->entry);
+
+	desc->entry = NULL;
+	desc->len_entries = desc->num_entries = 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_free_frame_desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
include/media/v4l2-subdev.h +9 -7
index c9e74566c85a..bd97510c7024 100644
@@ -365,11 +365,13 @@ struct v4l2_mbus_frame_desc_entry {
 	} bus;
 };
 
- /*
-  * If this number is too small, it should be dropped altogether and the
-  * API switched to a dynamic number of frame descriptor entries.
-  */
-#define V4L2_FRAME_DESC_ENTRY_MAX	8
+/* Size of the statically allocated frame descriptor array. */
+#define V4L2_FRAME_DESC_ENTRY_PREALLOC	8
+/*
+ * Maximum number of dynamically allocated frame descriptors. Note that
+ * V4L2_SUBDEV_MAX_STREAM_ID is related to this limit as well.
+ */
+#define V4L2_FRAME_DESC_ENTRY_MAX	64
 
 /**
  * enum v4l2_mbus_frame_desc_type - media bus frame description type
@@ -392,13 +394,17 @@ enum v4l2_mbus_frame_desc_type {
 /**
  * struct v4l2_mbus_frame_desc - media bus data frame description
  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
- * @entry: frame descriptors array
- * @num_entries: number of entries in @entry array
+ * @entry_mem: memory for the frame descriptors (@entry)
+ * @entry: pointer to the frame descriptors

include/media/v4l2-subdev.h enum v4l2_mbus_frame_desc_type { · line 407

Quoted 10 lines
include/media/v4l2-subdev.h +5 -1
+ * @num_entries: number of entries in @entry
+ * @len_entries: number of entries allocated for @entry
  */
 struct v4l2_mbus_frame_desc {
 	enum v4l2_mbus_frame_desc_type type;
-	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
+	struct v4l2_mbus_frame_desc_entry entry_mem[V4L2_FRAME_DESC_ENTRY_PREALLOC];
+	struct v4l2_mbus_frame_desc_entry *entry;
 	unsigned short num_entries;
+	unsigned short len_entries;

What are the implications of this change on this part of v4l2_subdev_get_frame_desc() ?

	struct v4l2_subdev_state *state =
		v4l2_subdev_lock_and_get_active_state(sd);
	ret = v4l2_subdev_call(sd, pad, get_fmt, state, &subdev_fmt);
	v4l2_subdev_unlock_state(state);
	if (ret)
		return ret;

	struct v4l2_mbus_frame_desc_entry entry = {
		.pixelcode = subdev_fmt.format.code,
	};

	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
		int dt;

		dt = mipi_csi2_dt_for_mbus(subdev_fmt.format.code);
		if (dt < 0)
			return dt;

		entry.bus.csi2.dt = dt;
	}

	desc->entry[0] = entry;

        ^
        |---- here ?
Quoted 6 lines

Is there any risk this now access invalid memory ?
Should we do the same thing that you're doing call_get_frame_desc() ?

	fd->entry = fd->entry_mem;

before assigning its first member ?

This is in fact done by the caller in the next patch. I think I'll squash it here.

Quoted 45 lines
Quoted 45 lines
include/media/v4l2-subdev.h +21 -1
 };
 
 /**
@@ -780,7 +786,13 @@ struct v4l2_subdev_state {
  * @link_validate: used by the media controller code to check if the links
  *		   that belongs to a pipeline can be used for stream.
  *
- * @get_frame_desc: get the current low level media bus frame parameters.
+ * @get_frame_desc: get the current low level media bus frame parameters. The
+ *		    callback is required to update the num_entries field to the
+ *		    total number of entries in the frame descriptor. The
+ *		    callback shall fill the first entries array up to
+ *		    len_entries, which signifies the number of entries
+ *		    allocated. If num_entries exceeds len_entries, the callback
+ *		    shall return -ENOSPC.
  *
  * @set_frame_desc: set the low level media bus frame parameters, @fd array
  *                  may be adjusted by the subdev driver to device capabilities.
@@ -1793,11 +1805,25 @@ int v4l2_subdev_get_frame_desc_passthrough(struct v4l2_subdev *sd,
  *
  * The caller is required to set @desc->type to the expected bus type.
  *
+ * The entries in the frame descriptor are allocated based on the need. The
+ * caller is required to release the memory of the frame descriptor entries for
+ * each frame descriptor obtained by calling this function using
+ * v4l2_subdev_free_frame_desc().
+ *
  * Return: %0 on success or negative error code on failure.
  */
 int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
 			       struct v4l2_mbus_frame_desc *desc);
 
+/**
+ * v4l2_subdev_free_frame_desc() - Release the memory of a frame descriptor
+ * @desc: A pointer to a frame descriptor
+ *
+ * Release the frame descriptor entries in a frame descriptor. The number of
+ * entries in the descriptor are set to 0 again.
+ */
+void v4l2_subdev_free_frame_desc(struct v4l2_mbus_frame_desc *desc);
+
 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
 
 #endif /* CONFIG_MEDIA_CONTROLLER */
Signature · 3 lines
Kind regards,

Sakari Ailus