Re: [PATCH v3 3/4] media: raspberrypi: Add support for RP1-CFE

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/09/2024 10:07, Sakari Ailus wrote:
Moi,

On Mon, Sep 02, 2024 at 01:05:42PM +0300, Tomi Valkeinen wrote:
Hi Sakari,

Thanks for the review!

You're welcome.

+#define cfe_dbg(fmt, arg...) dev_dbg(&cfe->pdev->dev, fmt, ##arg)

cfe should be an argument to cfe_dbg().

Why? This, and the ones below, is an internal macro to make it easier and
shorter to do prints. Adding the parameter gives no benefit that I can see.

Generally macros shouldn't expect certain variables not defined on the same
level the macros themselves. It gets harder to maintain this way.

I agree, but I'm fine taking shortcuts in private macros to make the code a bit shorter and more readable.

In any case, I don't feel strongly about this so I'll make the change, it's an easy find and replace.

+#define node_supports_image_output(node) \
+	(!!(node_desc[(node)->id].caps & V4L2_CAP_VIDEO_CAPTURE))

No need to cast to bool through !!. Same below.

I like my bools to be bools, not ints... But at the same time, I don't see
how that would cause issues in the uses we have in this driver. So I'll drop
these.

Alternatively, explicitly cast to bool. But I don't think it's needed.


+#define node_supports_meta_output(node) \
+	(!!(node_desc[(node)->id].caps & V4L2_CAP_META_CAPTURE))
+#define node_supports_image_input(node) \
+	(!!(node_desc[(node)->id].caps & V4L2_CAP_VIDEO_OUTPUT))
+#define node_supports_meta_input(node) \
+	(!!(node_desc[(node)->id].caps & V4L2_CAP_META_OUTPUT))
+#define node_supports_image(node) \
+	(node_supports_image_output(node) || node_supports_image_input(node))
+#define node_supports_meta(node) \
+	(node_supports_meta_output(node) || node_supports_meta_input(node))
+
+#define is_image_output_node(node) \
+	((node)->buffer_queue.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
+#define is_image_input_node(node) \
+	((node)->buffer_queue.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
+#define is_image_node(node) \
+	(is_image_output_node(node) || is_image_input_node(node))
+#define is_meta_output_node(node) \
+	((node)->buffer_queue.type == V4L2_BUF_TYPE_META_CAPTURE)
+#define is_meta_input_node(node) \
+	((node)->buffer_queue.type == V4L2_BUF_TYPE_META_OUTPUT)
+#define is_meta_node(node) \
+	(is_meta_output_node(node) || is_meta_input_node(node))
+
+/* To track state across all nodes. */
+#define NUM_STATES		5

This might be nicer if declared as last.

Sure.

+#define NODE_REGISTERED		BIT(0)
+#define NODE_ENABLED		BIT(1)
+#define NODE_STREAMING		BIT(2)
+#define FS_INT			BIT(3)
+#define FE_INT			BIT(4)

...

+static int cfe_start_channel(struct cfe_node *node)
+{
+	struct cfe_device *cfe = node->cfe;
+	struct v4l2_subdev_state *state;
+	struct v4l2_mbus_framefmt *source_fmt;
+	const struct cfe_fmt *fmt;
+	unsigned long flags;
+	bool start_fe;
+	int ret;
+
+	cfe_dbg("%s: [%s]\n", __func__, node_desc[node->id].name);

This looks like a development time leftover. There are quite a few such
prints that provide little information anyway. How about removing them all?

These are very valuable when testing, fixing or improving the driver. If I
were to remove them, I would just have to add them back whenever I'd be
doing something with the driver and things would not work perfectly.

The debug prints we have are all low frequency. There's a bunch printed when
starting the streaming and when stopping it, but the debug prints are not
used while streaming is on-going, and instead we have trace events for that.

I'm fine with debug prints when they do print useful information but you
have many of them just in the beginning of the function, printing only the
function name (and possibly the node name). I'd remove those, except in
cases where calling the function itself is useful information, such as on
returning the buffers.

I have removed a few debug prints, but I ended up adding a few too.

The useful information here in many cases is the function name and the node name. With multiple streams, and with the different possibilities in handling enable and disable (start streaming when the first video node is enabled? or only when all of them are enabled? what happens when one of the enabled nodes is disabled? is the csi side enabled along with the fe, and which csi channel?), it's very beneficial to see what goes on.

+static int cfe_link_node_pads(struct cfe_device *cfe)
+{
+	int ret;
+	int pad;
+
+	/* Source -> CSI2 */
+
+	pad = media_entity_get_fwnode_pad(&cfe->source_sd->entity,
+					  cfe->remote_ep_fwnode,
+					  MEDIA_PAD_FL_SOURCE);
+	if (pad < 0) {
+		cfe_err("Source %s has no connected source pad\n",
+			cfe->source_sd->name);
+		return pad;
+	}
+
+	cfe->source_pad = pad;
+
+	ret = media_create_pad_link(&cfe->source_sd->entity, pad,
+				    &cfe->csi2.sd.entity, CSI2_PAD_SINK,
+				    MEDIA_LNK_FL_IMMUTABLE |
+				    MEDIA_LNK_FL_ENABLED);
+	if (ret)
+		return ret;
+
+	for (unsigned int i = 0; i < CSI2_NUM_CHANNELS; i++) {
+		struct cfe_node *node = &cfe->node[i];
+
+		if (!check_state(cfe, NODE_REGISTERED, i))
+			continue;
+
+		/* CSI2 channel # -> /dev/video# */
+		ret = media_create_pad_link(&cfe->csi2.sd.entity,
+					    node_desc[i].link_pad,
+					    &node->video_dev.entity, 0, 0);
+		if (ret)
+			return ret;
+
+		if (node_supports_image(node)) {
+			/* CSI2 channel # -> FE Input */
+			ret = media_create_pad_link(&cfe->csi2.sd.entity,
+						    node_desc[i].link_pad,
+						    &cfe->fe.sd.entity,
+						    FE_STREAM_PAD, 0);
+			if (ret)
+				return ret;
+		}
+	}
+
+	for (unsigned int i = CSI2_NUM_CHANNELS; i < NUM_NODES; i++) {
+		struct cfe_node *node = &cfe->node[i];
+		struct media_entity *src, *dst;
+		unsigned int src_pad, dst_pad;
+
+		if (node_desc[i].pad_flags & MEDIA_PAD_FL_SINK) {
+			/* FE -> /dev/video# */
+			src = &cfe->fe.sd.entity;
+			src_pad = node_desc[i].link_pad;
+			dst = &node->video_dev.entity;
+			dst_pad = 0;
+		} else {
+			/* /dev/video# -> FE */
+			dst = &cfe->fe.sd.entity;
+			dst_pad = node_desc[i].link_pad;
+			src = &node->video_dev.entity;
+			src_pad = 0;
+		}
+
+		ret = media_create_pad_link(src, src_pad, dst, dst_pad, 0);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int cfe_probe_complete(struct cfe_device *cfe)
+{
+	int ret;
+
+	cfe->v4l2_dev.notify = cfe_notify;
+
+	for (unsigned int i = 0; i < NUM_NODES; i++) {
+		ret = cfe_register_node(cfe, i);
+		if (ret) {
+			cfe_err("Unable to register video node %u.\n", i);
+			goto unregister;
+		}
+	}
+
+	ret = cfe_link_node_pads(cfe);
+	if (ret) {
+		cfe_err("Unable to link node pads.\n");
+		goto unregister;
+	}
+
+	ret = v4l2_device_register_subdev_nodes(&cfe->v4l2_dev);
+	if (ret) {
+		cfe_err("Unable to register subdev nodes.\n");
+		goto unregister;
+	}
+
+	return 0;
+
+unregister:
+	cfe_unregister_nodes(cfe);
+	return ret;
+}
+
+static int cfe_async_bound(struct v4l2_async_notifier *notifier,
+			   struct v4l2_subdev *subdev,
+			   struct v4l2_async_connection *asd)
+{
+	struct cfe_device *cfe = to_cfe_device(notifier->v4l2_dev);
+
+	if (cfe->source_sd) {
+		cfe_err("Rejecting subdev %s (Already set!!)", subdev->name);
+		return 0;
+	}
+
+	cfe->source_sd = subdev;
+
+	cfe_dbg("Using source %s for capture\n", subdev->name);
+
+	return 0;
+}
+
+static int cfe_async_complete(struct v4l2_async_notifier *notifier)
+{
+	struct cfe_device *cfe = to_cfe_device(notifier->v4l2_dev);
+
+	return cfe_probe_complete(cfe);
+}
+
+static const struct v4l2_async_notifier_operations cfe_async_ops = {
+	.bound = cfe_async_bound,
+	.complete = cfe_async_complete,
+};
+
+static int cfe_register_async_nf(struct cfe_device *cfe)
+{
+	struct platform_device *pdev = cfe->pdev;
+	struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY };
+	int ret = -EINVAL;

Is the assignment necessary?

No, doesn't look like it.

+	struct fwnode_handle *local_ep_fwnode;
+	struct fwnode_handle *remote_ep_fwnode;
+	struct v4l2_async_connection *asd;
+
+	local_ep_fwnode = fwnode_graph_get_endpoint_by_id(pdev->dev.fwnode, 0, 0, 0);
+	if (!local_ep_fwnode) {
+		cfe_err("Failed to find local endpoint fwnode\n");
+		return -ENODEV;
+	}
+
+	remote_ep_fwnode = fwnode_graph_get_remote_endpoint(local_ep_fwnode);
+	if (!remote_ep_fwnode) {
+		cfe_err("Failed to find remote endpoint fwnode\n");
+		ret = -ENODEV;
+		goto err_put_local_fwnode;
+	}
+
+	/* Parse the local endpoint and validate its configuration. */
+	v4l2_fwnode_endpoint_parse(local_ep_fwnode, &ep);

You'll need to check the return value here.

I'll add that.

+
+	if (ep.bus_type != V4L2_MBUS_CSI2_DPHY) {

This check is redundant.

Indeed.

+		cfe_err("endpoint node type != CSI2\n");
+		ret = -EINVAL;
+		goto err_put_remote_fwnode;
+	}
+
+	for (unsigned int lane = 0; lane < ep.bus.mipi_csi2.num_data_lanes; lane++) {
+		if (ep.bus.mipi_csi2.data_lanes[lane] != lane + 1) {
+			cfe_err("subdevice %pfwf: data lanes reordering not supported\n",
+				remote_ep_fwnode);
+			ret = -EINVAL;
+			goto err_put_remote_fwnode;
+		}
+	}
+
+	cfe->csi2.dphy.max_lanes = ep.bus.mipi_csi2.num_data_lanes;
+	cfe->csi2.bus_flags = ep.bus.mipi_csi2.flags;
+
+	cfe->remote_ep_fwnode = remote_ep_fwnode;
+
+	cfe_dbg("source %pfwf: %u data lanes, flags=0x%08x\n",
+		remote_ep_fwnode, cfe->csi2.dphy.max_lanes, cfe->csi2.bus_flags);
+
+	/* Initialize and register the async notifier. */
+	v4l2_async_nf_init(&cfe->notifier, &cfe->v4l2_dev);
+	cfe->notifier.ops = &cfe_async_ops;
+
+	asd = v4l2_async_nf_add_fwnode(&cfe->notifier, remote_ep_fwnode,
+				       struct v4l2_async_connection);

Could you use v4l2_async_nf_add_fwnode_remote() and just not bother with
remote_ep_fwnode at all?

I need the remote_ep_fwnode in cfe_link_node_pads() when creating the links.

Is there some other way to get the remote pad so that I can call the
media_create_pad_link()?

Could you use v4l2_create_fwnode_links_to_pad() for that?

Yes, seems to work. However, I don't know why it works, as the docs say the sink has to implement .get_fwnode_pad(), which I don't have, yet everything seems to work fine... Ah, the code looks for the first sink pad if .get_fwnode_pad() is not implemented. So either the code or the doc is not right.

I still need to get the remote pad number, which I previously got from media_entity_get_fwnode_pad(), but I can now create the link first with v4l2_create_fwnode_links_to_pad() and then get the pad with media_pad_remote_pad_unique().

 Tomi





[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux