Skip to contentSkip to conversation

ipu3-cio2 format fixes

PATCH In review Sakari Ailus 3 participants 11 messages Oct 8, 2020 – Oct 9, 2020

Subsystems (2)
lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

cover ipu3-cio2 format fixes

Hello all,

This set addresses most notable subdev format related issues, namely the sub-device sink format being unaccessible. The result of accessing it varied from oopses to crashes.

Sakari Ailus (3):
ipu3-cio2: Return actual subdev format
ipu3-cio2: Serialise access to pad format
ipu3-cio2: Only allow setting valid mbus codes

 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 42 ++++++++++++------------
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  1 +
 2 files changed, 22 insertions(+), 21 deletions(-)
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

PATCH 1/3 ipu3-cio2: Return actual subdev format

Return actual subdev format on ipu3-cio2 subdev pads. The earlier implementation was based on an infinite recursion that exhausted the stack.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +3 -21
index 4e598e937dfe..afa472026ba4 100644
@@ -1232,29 +1232,11 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 			       struct v4l2_subdev_format *fmt)
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
-	struct v4l2_subdev_format format;
-	int ret;
 
-	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
+	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
-		return 0;
-	}
-
-	if (fmt->pad == CIO2_PAD_SINK) {
-		format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
-		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
-				       &format);
-
-		if (ret)
-			return ret;
-		/* update colorspace etc */
-		q->subdev_fmt.colorspace = format.format.colorspace;
-		q->subdev_fmt.ycbcr_enc = format.format.ycbcr_enc;
-		q->subdev_fmt.quantization = format.format.quantization;
-		q->subdev_fmt.xfer_func = format.format.xfer_func;
-	}
-
-	fmt->format = q->subdev_fmt;
+	else
+		fmt->format = q->subdev_fmt;
 
 	return 0;
 }
Signature · 1 line
2.27.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] ipu3-cio2: Return actual subdev format

Hi Sakari,

Thank you for the patch.

Quoted 3 lines · Sakari Ailus

Return actual subdev format on ipu3-cio2 subdev pads. The earlier implementation was based on an infinite recursion that exhausted the stack.

A bad idea indeed :-)

Quoted 29 lines
Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +1 -11
index 4e598e937dfe..afa472026ba4 100644
@@ -1232,29 +1232,11 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 			       struct v4l2_subdev_format *fmt)
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
-	struct v4l2_subdev_format format;
-	int ret;
 
-	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
+	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
-		return 0;
-	}
-
-	if (fmt->pad == CIO2_PAD_SINK) {
-		format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
-		ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
-				       &format);
-

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_get_fmt(struct v4l2_subdev *sd, · line 1257

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +0 -10
-		if (ret)
-			return ret;
-		/* update colorspace etc */
-		q->subdev_fmt.colorspace = format.format.colorspace;
-		q->subdev_fmt.ycbcr_enc = format.format.ycbcr_enc;
-		q->subdev_fmt.quantization = format.format.quantization;
-		q->subdev_fmt.xfer_func = format.format.xfer_func;
-	}
-
-	fmt->format = q->subdev_fmt;

I'm pretty speechless. All I can say is

and let's forget this has ever existed :-)

Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -0
+	else
+		fmt->format = q->subdev_fmt;
 
 	return 0;
 }
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#3 lore raw

PATCH 2/3 ipu3-cio2: Serialise access to pad format

In reply to Sakari Ailus

Pad format can be accessed from user space. Serialise access to it.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 11 +++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  1 +
 2 files changed, 12 insertions(+)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +11 -0
index afa472026ba4..9c7b527a8800 100644
@@ -1233,11 +1233,15 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
 	else
 		fmt->format = q->subdev_fmt;
 
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1261,6 +1265,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->pad == CIO2_PAD_SOURCE)
 		return cio2_subdev_get_fmt(sd, cfg, fmt);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
@@ -1271,6 +1277,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 		fmt->format = q->subdev_fmt;
 	}
 
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1529,6 +1537,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 
 	/* Initialize miscellaneous variables */
 	mutex_init(&q->lock);
+	mutex_init(&q->subdev_lock);
 
 	/* Initialize formats to default values */
 	fmt = &q->subdev_fmt;
@@ -1646,6 +1655,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 fail_fbpt:
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 
 	return r;
 }
@@ -1658,6 +1668,7 @@ static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q)
 	media_entity_cleanup(&q->subdev.entity);
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 }
 
 static int cio2_queues_init(struct cio2_device *cio2)
drivers/media/pci/intel/ipu3/ipu3-cio2.h +1 -0
index 549b08f88f0c..df0247326a1d 100644
@@ -335,6 +335,7 @@ struct cio2_queue {
 
 	/* Subdev, /dev/v4l-subdevX */
 	struct v4l2_subdev subdev;
+	struct mutex subdev_lock;
 	struct media_pad subdev_pads[CIO2_PADS];
 	struct v4l2_mbus_framefmt subdev_fmt;
 	atomic_t frame_sequence;
Signature · 1 line
2.27.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] ipu3-cio2: Serialise access to pad format

Hi Sakari,

Thank you for the patch.

Quoted 39 lines · Sakari Ailus

Pad format can be accessed from user space. Serialise access to it.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 11 +++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  1 +
 2 files changed, 12 insertions(+)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +6 -0
index afa472026ba4..9c7b527a8800 100644
@@ -1233,11 +1233,15 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
 	else
 		fmt->format = q->subdev_fmt;
 
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1261,6 +1265,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->pad == CIO2_PAD_SOURCE)
 		return cio2_subdev_get_fmt(sd, cfg, fmt);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_set_fmt(struct v4l2_subdev *sd, · line 1278

Quoted 2 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +0 -0
@@ -1271,6 +1277,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 		fmt->format = q->subdev_fmt;
 	}

Not a candidate for this patch, but this should restrict the pixel format and size to supported values.

Quoted 35 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +5 -0
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1529,6 +1537,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 
 	/* Initialize miscellaneous variables */
 	mutex_init(&q->lock);
+	mutex_init(&q->subdev_lock);
 
 	/* Initialize formats to default values */
 	fmt = &q->subdev_fmt;
@@ -1646,6 +1655,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 fail_fbpt:
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 
 	return r;
 }
@@ -1658,6 +1668,7 @@ static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q)
 	media_entity_cleanup(&q->subdev.entity);
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 }
 
 static int cio2_queues_init(struct cio2_device *cio2)
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
index 549b08f88f0c..df0247326a1d 100644

drivers/media/pci/intel/ipu3/ipu3-cio2.h struct cio2_queue { · line 338

Quoted 4 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +1 -0
@@ -335,6 +335,7 @@ struct cio2_queue {
 
 	/* Subdev, /dev/v4l-subdevX */
 	struct v4l2_subdev subdev;
+	struct mutex subdev_lock;

Can you add a small comment to tell which field(s) this lock protects ?

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
 	struct media_pad subdev_pads[CIO2_PADS];
 	struct v4l2_mbus_framefmt subdev_fmt;
 	atomic_t frame_sequence;
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#5 lore raw

Re: [PATCH 2/3] ipu3-cio2: Serialise access to pad format

Hi Laurent,

Quoted 3 lines · Laurent Pinchart

Hi Sakari,

Thank you for the patch.

Thanks for the review!

Quoted 35 lines · Sakari Ailus
Quoted 33 lines

Pad format can be accessed from user space. Serialise access to it.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 11 +++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  1 +
 2 files changed, 12 insertions(+)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +4 -0
index afa472026ba4..9c7b527a8800 100644
@@ -1233,11 +1233,15 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
 	else
 		fmt->format = q->subdev_fmt;
 
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1261,6 +1265,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->pad == CIO2_PAD_SOURCE)
 		return cio2_subdev_get_fmt(sd, cfg, fmt);
 
Quoted 11 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_set_fmt(struct v4l2_subdev *sd, · line 1278

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -0
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
@@ -1271,6 +1277,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 		fmt->format = q->subdev_fmt;
 	}

Not a candidate for this patch, but this should restrict the pixel format and size to supported values.

Yes.

Quoted 37 lines
Quoted 37 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +5 -0
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1529,6 +1537,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 
 	/* Initialize miscellaneous variables */
 	mutex_init(&q->lock);
+	mutex_init(&q->subdev_lock);
 
 	/* Initialize formats to default values */
 	fmt = &q->subdev_fmt;
@@ -1646,6 +1655,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 fail_fbpt:
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 
 	return r;
 }
@@ -1658,6 +1668,7 @@ static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q)
 	media_entity_cleanup(&q->subdev.entity);
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 }
 
 static int cio2_queues_init(struct cio2_device *cio2)
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
index 549b08f88f0c..df0247326a1d 100644
@@ -335,6 +335,7 @@ struct cio2_queue {
 
Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.h struct cio2_queue { · line 338

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +1 -0
 	/* Subdev, /dev/v4l-subdevX */
 	struct v4l2_subdev subdev;
+	struct mutex subdev_lock;

Can you add a small comment to tell which field(s) this lock protects ?

How about:

	/* Serialise access to subdev_fmt field */

Currently it's just that, but I feel locking in this driver would generally benefit from refactoring. That can wait a little though as it's not an acute problem.

Quoted 11 lines
Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
 	struct media_pad subdev_pads[CIO2_PADS];
 	struct v4l2_mbus_framefmt subdev_fmt;
 	atomic_t frame_sequence;
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 1 line
Sakari Ailus
#6 lore raw

Re: [PATCH 2/3] ipu3-cio2: Serialise access to pad format

Hi Sakari,

Quoted 92 lines · Sakari Ailus
Quoted 34 lines · Laurent Pinchart
Quoted 33 lines · Sakari Ailus

Pad format can be accessed from user space. Serialise access to it.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 11 +++++++++++
 drivers/media/pci/intel/ipu3/ipu3-cio2.h |  1 +
 2 files changed, 12 insertions(+)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +4 -0
index afa472026ba4..9c7b527a8800 100644
@@ -1233,11 +1233,15 @@ static int cio2_subdev_get_fmt(struct v4l2_subdev *sd,
 {
 	struct cio2_queue *q = container_of(sd, struct cio2_queue, subdev);
 
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
 		fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
 	else
 		fmt->format = q->subdev_fmt;
 
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1261,6 +1265,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->pad == CIO2_PAD_SOURCE)
 		return cio2_subdev_get_fmt(sd, cfg, fmt);
 
Quoted 11 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_set_fmt(struct v4l2_subdev *sd, · line 1278

Quoted 8 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -0
+	mutex_lock(&q->subdev_lock);
+
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
@@ -1271,6 +1277,8 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 		fmt->format = q->subdev_fmt;
 	}

Not a candidate for this patch, but this should restrict the pixel format and size to supported values.

Yes.

Quoted 37 lines
Quoted 37 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +5 -0
+	mutex_unlock(&q->subdev_lock);
+
 	return 0;
 }
 
@@ -1529,6 +1537,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 
 	/* Initialize miscellaneous variables */
 	mutex_init(&q->lock);
+	mutex_init(&q->subdev_lock);
 
 	/* Initialize formats to default values */
 	fmt = &q->subdev_fmt;
@@ -1646,6 +1655,7 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 fail_fbpt:
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 
 	return r;
 }
@@ -1658,6 +1668,7 @@ static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q)
 	media_entity_cleanup(&q->subdev.entity);
 	cio2_fbpt_exit(q, &cio2->pci_dev->dev);
 	mutex_destroy(&q->lock);
+	mutex_destroy(&q->subdev_lock);
 }
 
 static int cio2_queues_init(struct cio2_device *cio2)
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
index 549b08f88f0c..df0247326a1d 100644
@@ -335,6 +335,7 @@ struct cio2_queue {
 
Quoted 5 lines

drivers/media/pci/intel/ipu3/ipu3-cio2.h struct cio2_queue { · line 338

Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +1 -0
 	/* Subdev, /dev/v4l-subdevX */
 	struct v4l2_subdev subdev;
+	struct mutex subdev_lock;

Can you add a small comment to tell which field(s) this lock protects ?

Quoted 3 lines

How about:

	/* Serialise access to subdev_fmt field */

That's perfect :-)

Quoted 3 lines

Currently it's just that, but I feel locking in this driver would generally benefit from refactoring. That can wait a little though as it's not an acute problem.

Sure.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.h +0 -0
 	struct media_pad subdev_pads[CIO2_PADS];
 	struct v4l2_mbus_framefmt subdev_fmt;
 	atomic_t frame_sequence;
Signature · 3 lines
Regards,

Laurent Pinchart
#7 lore raw

Re: [PATCH 2/3] ipu3-cio2: Serialise access to pad format

In reply to Sakari Ailus
Quoted 2 lines · Sakari Ailus

Pad format can be accessed from user space. Serialise access to it.

...

Quoted 3 lines
        /* Initialize miscellaneous variables */
        mutex_init(&q->lock);

+ mutex_init(&q->subdev_lock);

...

Quoted 2 lines
        mutex_destroy(&q->lock);

+ mutex_destroy(&q->subdev_lock);

A nit: perhaps reversed order?

Quoted 2 lines
        mutex_destroy(&q->lock);

+ mutex_destroy(&q->subdev_lock);

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#8 lore raw

PATCH 3/3 ipu3-cio2: Only allow setting valid mbus codes

In reply to Sakari Ailus

Check the mbus code provided by the user is one of those the driver supports. Ignore the code in set_fmt otherwise.

Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -1
index 9c7b527a8800..2ea6313e00b0 100644
@@ -1270,10 +1270,17 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
+		unsigned int i;
+
 		/* It's the sink, allow changing frame size */
 		q->subdev_fmt.width = fmt->format.width;
 		q->subdev_fmt.height = fmt->format.height;
-		q->subdev_fmt.code = fmt->format.code;
+		for (i = 0; i < ARRAY_SIZE(formats); i++) {
+			if (formats[i].mbus_code == fmt->format.code) {
+				q->subdev_fmt.code = fmt->format.code;
+				break;
+			}
+		}
 		fmt->format = q->subdev_fmt;
 	}
 
Signature · 1 line
2.27.0
#9 lore raw

Re: [PATCH 3/3] ipu3-cio2: Only allow setting valid mbus codes

Hi Sakari,

Thank you for the patch.

Quoted 2 lines · Sakari Ailus

Check the mbus code provided by the user is one of those the driver supports. Ignore the code in set_fmt otherwise.

You're reading my mind :-)

The code shouldn't be ignored though, when an unsupported code is set, it's best to use a fixed default code instead to make the driver behaviour as stateless as possible.

Quoted 17 lines
Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -0
index 9c7b527a8800..2ea6313e00b0 100644
@@ -1270,10 +1270,17 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
+		unsigned int i;
+
 		/* It's the sink, allow changing frame size */

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_set_fmt(struct v4l2_subdev *sd, · line 1284

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +6 -1
 		q->subdev_fmt.width = fmt->format.width;
 		q->subdev_fmt.height = fmt->format.height;
-		q->subdev_fmt.code = fmt->format.code;
+		for (i = 0; i < ARRAY_SIZE(formats); i++) {
+			if (formats[i].mbus_code == fmt->format.code) {
+				q->subdev_fmt.code = fmt->format.code;
+				break;
+			}
+		}
 		fmt->format = q->subdev_fmt;

This should equally apply to the TRY format, we should accept an unsupported code. I'd rework the code as follows:

	v4l2_mbus_framefmt *format;

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
		format = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
	else
		format = &q->subdev_fmt;

(this can be done outside of the mutex-protected section) and then operate on format unconditionally.

Note that we should also allow changing the field and colorspace information.

Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#10 lore raw

Re: [PATCH 3/3] ipu3-cio2: Only allow setting valid mbus codes

Hi Laurent,

Quoted 5 lines · Laurent Pinchart

Hi Sakari,

Thank you for the patch.

On Thu, Oct 08, 2020 at 11:47:47PM +0300, Sakari Ailus wrote:

Quoted 8 lines
Quoted 2 lines

Check the mbus code provided by the user is one of those the driver supports. Ignore the code in set_fmt otherwise.

You're reading my mind :-)

The code shouldn't be ignored though, when an unsupported code is set, it's best to use a fixed default code instead to make the driver behaviour as stateless as possible.

I can change it to that, yes.

Quoted 39 lines
Quoted 18 lines
Fixes c2a6a07afe4a ("media: intel-ipu3: cio2: add new MIPI-CSI2 driver")
---
 drivers/media/pci/intel/ipu3/ipu3-cio2.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +2 -0
index 9c7b527a8800..2ea6313e00b0 100644
@@ -1270,10 +1270,17 @@ static int cio2_subdev_set_fmt(struct v4l2_subdev *sd,
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
 	} else {
+		unsigned int i;
+
 		/* It's the sink, allow changing frame size */

drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_subdev_set_fmt(struct v4l2_subdev *sd, · line 1284

Quoted 10 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +6 -1
 		q->subdev_fmt.width = fmt->format.width;
 		q->subdev_fmt.height = fmt->format.height;
-		q->subdev_fmt.code = fmt->format.code;
+		for (i = 0; i < ARRAY_SIZE(formats); i++) {
+			if (formats[i].mbus_code == fmt->format.code) {
+				q->subdev_fmt.code = fmt->format.code;
+				break;
+			}
+		}
 		fmt->format = q->subdev_fmt;

This should equally apply to the TRY format, we should accept an unsupported code. I'd rework the code as follows:

	v4l2_mbus_framefmt *format;

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
		format = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
	else
		format = &q->subdev_fmt;
Quoted 2 lines

(this can be done outside of the mutex-protected section) and then operate on format unconditionally.

Agreed.

Quoted 2 lines

Note that we should also allow changing the field and colorspace information.

Indeed.

Signature · 1 line
Sakari Ailus