Linux Media Controller development
 help / color / mirror / Atom feed
* [PATCH 0/2] media: usb: hdpvr: make use of quirk flags from I2C core
@ 2025-05-22  6:55 Wolfram Sang
  2025-05-22  6:55 ` [PATCH 1/2] media: usb: hdpvr: disable zero-length read messages Wolfram Sang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Wolfram Sang @ 2025-05-22  6:55 UTC (permalink / raw)
  To: linux-i2c
  Cc: Alan Stern, Nikita Zhandarovich, Wolfram Sang, Hans Verkuil,
	linux-media, Mauro Carvalho Chehab

We found a pattern that could create USB protocol violations for devices
which embed an I2C bus and use I2C message lengths directly. Patch 1
fixes the bug for this driver by using a quirk flag. Patch 2 is a
cleanup to remove a pattern in the driver which can also be handled with
a quirk flag.

Wolfram Sang (2):
  media: usb: hdpvr: disable zero-length read messages
  media: usb: hdpvr: use I2C core to handle only supported messages

 drivers/media/usb/hdpvr/hdpvr-i2c.c | 29 ++++++++---------------------
 1 file changed, 8 insertions(+), 21 deletions(-)

-- 
2.47.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] media: usb: hdpvr: disable zero-length read messages
  2025-05-22  6:55 [PATCH 0/2] media: usb: hdpvr: make use of quirk flags from I2C core Wolfram Sang
@ 2025-05-22  6:55 ` Wolfram Sang
  2025-05-22  6:55 ` [PATCH 2/2] media: usb: hdpvr: use I2C core to handle only supported messages Wolfram Sang
       [not found] ` <682ed8b3.050a0220.1b76f0.5818@mx.google.com>
  2 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2025-05-22  6:55 UTC (permalink / raw)
  To: linux-i2c
  Cc: Alan Stern, Nikita Zhandarovich, Wolfram Sang, Hans Verkuil,
	Mauro Carvalho Chehab, Janne Grunau, linux-media

This driver passes the length of an i2c_msg directly to
usb_control_msg(). If the message is now a read and of length 0, it
violates the USB protocol and a warning will be printed. Enable the
I2C_AQ_NO_ZERO_LEN_READ quirk for this adapter thus forbidding 0-length
read messages altogether.

Fixes: 9aba42efe85b ("V4L/DVB (11096): V4L2 Driver for the Hauppauge HD PVR usb capture device")
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 drivers/media/usb/hdpvr/hdpvr-i2c.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/media/usb/hdpvr/hdpvr-i2c.c b/drivers/media/usb/hdpvr/hdpvr-i2c.c
index 070559b01b01..54956a8ff15e 100644
--- a/drivers/media/usb/hdpvr/hdpvr-i2c.c
+++ b/drivers/media/usb/hdpvr/hdpvr-i2c.c
@@ -165,10 +165,16 @@ static const struct i2c_algorithm hdpvr_algo = {
 	.functionality = hdpvr_functionality,
 };
 
+/* prevent invalid 0-length usb_control_msg */
+static const struct i2c_adapter_quirks hdpvr_quirks = {
+	.flags = I2C_AQ_NO_ZERO_LEN_READ,
+};
+
 static const struct i2c_adapter hdpvr_i2c_adapter_template = {
 	.name   = "Hauppauge HD PVR I2C",
 	.owner  = THIS_MODULE,
 	.algo   = &hdpvr_algo,
+	.quirks = &hdpvr_quirks,
 };
 
 static int hdpvr_activate_ir(struct hdpvr_device *dev)
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] media: usb: hdpvr: use I2C core to handle only supported messages
  2025-05-22  6:55 [PATCH 0/2] media: usb: hdpvr: make use of quirk flags from I2C core Wolfram Sang
  2025-05-22  6:55 ` [PATCH 1/2] media: usb: hdpvr: disable zero-length read messages Wolfram Sang
@ 2025-05-22  6:55 ` Wolfram Sang
       [not found] ` <682ed8b3.050a0220.1b76f0.5818@mx.google.com>
  2 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2025-05-22  6:55 UTC (permalink / raw)
  To: linux-i2c
  Cc: Alan Stern, Nikita Zhandarovich, Wolfram Sang, Hans Verkuil,
	Mauro Carvalho Chehab, linux-media

The HW can only do write-then-read transactions. This is a common
limitation, so we can add an adapter quirk flag to let the I2C core
enforce the checks instead of open coding them.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 drivers/media/usb/hdpvr/hdpvr-i2c.c | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/drivers/media/usb/hdpvr/hdpvr-i2c.c b/drivers/media/usb/hdpvr/hdpvr-i2c.c
index 54956a8ff15e..3c5e6c33bee4 100644
--- a/drivers/media/usb/hdpvr/hdpvr-i2c.c
+++ b/drivers/media/usb/hdpvr/hdpvr-i2c.c
@@ -124,29 +124,10 @@ static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
 		else
 			retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
 						 msgs[0].len);
-	} else if (num == 2) {
-		if (msgs[0].addr != msgs[1].addr) {
-			v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer with conflicting target addresses\n");
-			retval = -EINVAL;
-			goto out;
-		}
-
-		if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
-			v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with r0=%d, r1=%d\n",
-				  msgs[0].flags & I2C_M_RD,
-				  msgs[1].flags & I2C_M_RD);
-			retval = -EINVAL;
-			goto out;
-		}
-
-		/*
-		 * Write followed by atomic read is the only complex xfer that
-		 * we actually support here.
-		 */
+	} else {
+		/* do write-then-read */
 		retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
 					msgs[1].buf, msgs[1].len);
-	} else {
-		v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
 	}
 
 out:
@@ -165,9 +146,9 @@ static const struct i2c_algorithm hdpvr_algo = {
 	.functionality = hdpvr_functionality,
 };
 
-/* prevent invalid 0-length usb_control_msg */
+/* prevent invalid 0-length usb_control_msg and support only write-then-read */
 static const struct i2c_adapter_quirks hdpvr_quirks = {
-	.flags = I2C_AQ_NO_ZERO_LEN_READ,
+	.flags = I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_COMB_WRITE_THEN_READ,
 };
 
 static const struct i2c_adapter hdpvr_i2c_adapter_template = {
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [0/2] media: usb: hdpvr: make use of quirk flags from I2C core
       [not found] ` <682ed8b3.050a0220.1b76f0.5818@mx.google.com>
@ 2025-05-22  8:05   ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2025-05-22  8:05 UTC (permalink / raw)
  To: linux-media

[-- Attachment #1: Type: text/plain, Size: 318 bytes --]

> # Test build:allmodconfig x86_64
> drivers/media/usb/hdpvr/hdpvr-i2c.c: In function ‘hdpvr_transfer’:
> drivers/media/usb/hdpvr/hdpvr-i2c.c:133:1: warning: label ‘out’ defined but not used [-Wunused-label]
>   133 | out:
>       | ^~~

Embarassing, I just overlooked this output in my build. Sorry!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-05-22  8:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-22  6:55 [PATCH 0/2] media: usb: hdpvr: make use of quirk flags from I2C core Wolfram Sang
2025-05-22  6:55 ` [PATCH 1/2] media: usb: hdpvr: disable zero-length read messages Wolfram Sang
2025-05-22  6:55 ` [PATCH 2/2] media: usb: hdpvr: use I2C core to handle only supported messages Wolfram Sang
     [not found] ` <682ed8b3.050a0220.1b76f0.5818@mx.google.com>
2025-05-22  8:05   ` [0/2] media: usb: hdpvr: make use of quirk flags from I2C core Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox