Skip to contentSkip to conversation

fix buggy mxl5007t register read

PATCH In review Antti Palosaari 2 participants 10 messages Apr 9, 2013 – Apr 10, 2013

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

cover fix buggy mxl5007t register read

Current MxL5007t driver implements repeated start condition (badly) whilst device uses stop before read operation.

I added "use_broken_read_reg_intentionally" config option to avoid regressions as I don't have all devices to test / fix.

Antti Palosaari (5):
mxl5007t: fix buggy register read
af9015: fix I2C adapter read (without REPEATED STOP)
af9015: do not use buggy mxl5007t read reg
af9035: implement I2C adapter read operation
af9035: do not use buggy mxl5007t read reg

 drivers/media/tuners/mxl5007t.c             | 56 ++++++++++++++++++++++++++++-
 drivers/media/tuners/mxl5007t.h             |  7 ++++
 drivers/media/usb/au0828/au0828-dvb.c       |  1 +
 drivers/media/usb/dvb-usb-v2/af9015.c       |  2 +-
 drivers/media/usb/dvb-usb-v2/af9035.c       | 22 ++++++++++--
 drivers/media/usb/dvb-usb/dib0700_devices.c |  1 +
 6 files changed, 85 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/5 mxl5007t: fix buggy register read

Chip uses WRITE + STOP + READ + STOP sequence for I2C register read. Driver was using REPEATED START condition which makes it failing if I2C adapter was implemented correctly.

Add use_broken_read_reg_intentionally option to keep old buggy implantation as there is buggy I2C adapter implementation relying that bug...

---
 drivers/media/tuners/mxl5007t.c             | 56 ++++++++++++++++++++++++++++-
 drivers/media/tuners/mxl5007t.h             |  7 ++++
 drivers/media/usb/au0828/au0828-dvb.c       |  1 +
 drivers/media/usb/dvb-usb-v2/af9015.c       |  1 +
 drivers/media/usb/dvb-usb-v2/af9035.c       |  2 ++
 drivers/media/usb/dvb-usb/dib0700_devices.c |  1 +
 6 files changed, 67 insertions(+), 1 deletion(-)
drivers/media/tuners/mxl5007t.c +55 -1
index 69e453e..36605ea 100644
@@ -156,6 +156,7 @@ struct mxl5007t_state {
 	struct tuner_i2c_props i2c_props;
 
 	struct mutex lock;
+	struct mutex i2c_lock;
 
 	struct mxl5007t_config *config;
 
@@ -490,7 +491,8 @@ static int mxl5007t_write_regs(struct mxl5007t_state *state,
 	return ret;
 }
 
-static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val)
+/* XXX: bad implementation for avoiding regressions */
+static int mxl5007t_read_reg_bad(struct mxl5007t_state *state, u8 reg, u8 *val)
 {
 	u8 buf[2] = { 0xfb, reg };
 	struct i2c_msg msg[] = {
@@ -509,6 +511,57 @@ static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val)
 	return 0;
 }
 
+/* chip uses I2C write + read with STOP condition */
+static int mxl5007t_read_reg_good(struct mxl5007t_state *state, u8 reg, u8 *val)
+{
+	int ret;
+	u8 buf[2] = { 0xfb, reg };
+	struct i2c_msg msg1[] = {
+		{
+			.addr = state->i2c_props.addr,
+			.flags = 0,
+			.buf = buf,
+			.len = 2,
+		},
+	};
+	struct i2c_msg msg2[] = {
+		{
+			.addr = state->i2c_props.addr,
+			.flags = I2C_M_RD,
+			.buf = val,
+			.len = 1,
+		},
+	};
+
+	mutex_lock(&state->i2c_lock);
+
+	ret = i2c_transfer(state->i2c_props.adap, msg1, 1);
+	if (ret != 1) {
+		mxl_err("failed!");
+		ret = -EREMOTEIO;
+		goto fail;
+	}
+
+	ret = i2c_transfer(state->i2c_props.adap, msg2, 1);
+	if (ret != 1) {
+		mxl_err("failed!");
+		ret = -EREMOTEIO;
+		goto fail;
+	}
+fail:
+	mutex_unlock(&state->i2c_lock);
+
+	return ret;
+}
+
+static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val)
+{
+	if (state->config->use_broken_read_reg_intentionally)
+		return mxl5007t_read_reg_bad(state, reg, val);
+	else
+		return mxl5007t_read_reg_good(state, reg, val);
+}
+
 static int mxl5007t_soft_reset(struct mxl5007t_state *state)
 {
 	u8 d = 0xff;
@@ -883,6 +936,7 @@ struct dvb_frontend *mxl5007t_attach(struct dvb_frontend *fe,
 		state->config = cfg;
 
 		mutex_init(&state->lock);
+		mutex_init(&state->i2c_lock);
 
 		if (fe->ops.i2c_gate_ctrl)
 			fe->ops.i2c_gate_ctrl(fe, 1);
drivers/media/tuners/mxl5007t.h +7 -0
index 37b0942..728779b 100644
@@ -75,6 +75,13 @@ struct mxl5007t_config {
 	unsigned int invert_if:1;
 	unsigned int loop_thru_enable:1;
 	unsigned int clk_out_enable:1;
+	/*
+	 * XXX: This should not be used. Defined for avoiding regressions.
+	 * Remove use of that option after device is tested to be working with
+	 * correct implementation.
+	 * MxL5007t does not use I2C REPEATED START condition for register read.
+	 */
+	unsigned int use_broken_read_reg_intentionally:1;
 };
 
 #if IS_ENABLED(CONFIG_MEDIA_TUNER_MXL5007T)
drivers/media/usb/au0828/au0828-dvb.c +1 -0
index 9a6f156..7d32a0c 100644
@@ -95,6 +95,7 @@ static struct xc5000_config hauppauge_xc5000c_config = {
 static struct mxl5007t_config mxl5007t_hvr950q_config = {
 	.xtal_freq_hz = MxL_XTAL_24_MHZ,
 	.if_freq_hz = MxL_IF_6_MHZ,
+	.use_broken_read_reg_intentionally = 1,
 };
 
 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
drivers/media/usb/dvb-usb-v2/af9015.c +1 -0
index d556042..b943304 100644
@@ -931,6 +931,7 @@ static struct tda18218_config af9015_tda18218_config = {
 static struct mxl5007t_config af9015_mxl5007t_config = {
 	.xtal_freq_hz = MxL_XTAL_24_MHZ,
 	.if_freq_hz = MxL_IF_4_57_MHZ,
+	.use_broken_read_reg_intentionally = 1,
 };
 
 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
drivers/media/usb/dvb-usb-v2/af9035.c +2 -0
index b638fc1..b7e7135 100644
@@ -947,6 +947,7 @@ static struct mxl5007t_config af9035_mxl5007t_config[] = {
 		.loop_thru_enable = 0,
 		.clk_out_enable = 0,
 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
+		.use_broken_read_reg_intentionally = 1,
 	}, {
 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
 		.if_freq_hz = MxL_IF_4_57_MHZ,
@@ -954,6 +955,7 @@ static struct mxl5007t_config af9035_mxl5007t_config[] = {
 		.loop_thru_enable = 1,
 		.clk_out_enable = 1,
 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
+		.use_broken_read_reg_intentionally = 1,
 	}
 };
 
drivers/media/usb/dvb-usb/dib0700_devices.c +1 -0
index 1179842..c58c6ea 100644
@@ -3433,6 +3433,7 @@ static struct mxl5007t_config hcw_mxl5007t_config = {
 	.xtal_freq_hz = MxL_XTAL_25_MHZ,
 	.if_freq_hz = MxL_IF_6_MHZ,
 	.invert_if = 1,
+	.use_broken_read_reg_intentionally = 1,
 };
 
 /* TIGER-ATSC map:
Signature · 1 line
1.7.11.7
#6 lore raw

PATCH 2/5 af9015: fix I2C adapter read (without REPEATED STOP)

In reply to Antti Palosaari
---
 drivers/media/usb/dvb-usb-v2/af9015.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/dvb-usb-v2/af9015.c +1 -1
index b943304..a523a25 100644
@@ -282,7 +282,7 @@ Due to that the only way to select correct tuner is use demodulator I2C-gate.
 			req.i2c_addr = msg[i].addr;
 			req.addr = addr;
 			req.mbox = mbox;
-			req.addr_len = addr_len;
+			req.addr_len = 0;
 			req.data_len = msg[i].len;
 			req.data = &msg[i].buf[0];
 			ret = af9015_ctrl_msg(d, &req);
Signature · 1 line
1.7.11.7
#7 lore raw

PATCH 3/5 af9015: do not use buggy mxl5007t read reg

In reply to Antti Palosaari

That backward compatibility hack option is no longer needed.

---
 drivers/media/usb/dvb-usb-v2/af9015.c | 1 -
 1 file changed, 1 deletion(-)
drivers/media/usb/dvb-usb-v2/af9015.c +0 -1
index a523a25..4d2587a 100644
@@ -931,7 +931,6 @@ static struct tda18218_config af9015_tda18218_config = {
 static struct mxl5007t_config af9015_mxl5007t_config = {
 	.xtal_freq_hz = MxL_XTAL_24_MHZ,
 	.if_freq_hz = MxL_IF_4_57_MHZ,
-	.use_broken_read_reg_intentionally = 1,
 };
 
 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
Signature · 1 line
1.7.11.7
#8 lore raw

PATCH 4/5 af9035: implement I2C adapter read operation

In reply to Antti Palosaari
---
 drivers/media/usb/dvb-usb-v2/af9035.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/media/usb/dvb-usb-v2/af9035.c +20 -2
index b7e7135..6c039eb 100644
@@ -268,11 +268,29 @@ static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
 			memcpy(&buf[5], msg[0].buf, msg[0].len);
 			ret = af9035_ctrl_msg(d, &req);
 		}
+	} else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
+		if (msg[0].len > 40) {
+			/* TODO: correct limits > 40 */
+			ret = -EOPNOTSUPP;
+		} else {
+			/* I2C */
+			u8 buf[5];
+			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
+					buf, msg[0].len, msg[0].buf };
+			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
+			buf[0] = msg[0].len;
+			buf[1] = msg[0].addr << 1;
+			buf[2] = 0x00; /* reg addr len */
+			buf[3] = 0x00; /* reg addr MSB */
+			buf[4] = 0x00; /* reg addr LSB */
+			ret = af9035_ctrl_msg(d, &req);
+		}
 	} else {
 		/*
-		 * We support only two kind of I2C transactions:
-		 * 1) 1 x read + 1 x write
+		 * We support only three kind of I2C transactions:
+		 * 1) 1 x read + 1 x write (repeated start)
 		 * 2) 1 x write
+		 * 3) 1 x read
 		 */
 		ret = -EOPNOTSUPP;
 	}
Signature · 1 line
1.7.11.7
#9 lore raw

PATCH 5/5 af9035: do not use buggy mxl5007t read reg

In reply to Antti Palosaari

That backward compatibility hack option is no longer needed.

---
 drivers/media/usb/dvb-usb-v2/af9035.c | 2 --
 1 file changed, 2 deletions(-)
drivers/media/usb/dvb-usb-v2/af9035.c +0 -2
index 6c039eb..cfcf79b 100644
@@ -965,7 +965,6 @@ static struct mxl5007t_config af9035_mxl5007t_config[] = {
 		.loop_thru_enable = 0,
 		.clk_out_enable = 0,
 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
-		.use_broken_read_reg_intentionally = 1,
 	}, {
 		.xtal_freq_hz = MxL_XTAL_24_MHZ,
 		.if_freq_hz = MxL_IF_4_57_MHZ,
@@ -973,7 +972,6 @@ static struct mxl5007t_config af9035_mxl5007t_config[] = {
 		.loop_thru_enable = 1,
 		.clk_out_enable = 1,
 		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
-		.use_broken_read_reg_intentionally = 1,
 	}
 };
 
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH 1/5] mxl5007t: fix buggy register read

In reply to Antti Palosaari
Quoted 9 lines · Antti Palosaari

Chip uses WRITE + STOP + READ + STOP sequence for I2C register read. Driver was using REPEATED START condition which makes it failing if I2C adapter was implemented correctly.

Add use_broken_read_reg_intentionally option to keep old buggy implantation as there is buggy I2C adapter implementation relying that bug...

Hi Antti,

The existing code actually looks fine. This is actually how most devices do register reads.

Further, it *should* be done in a single call to i2c_transfer() or else you won't hold the lock and you will create a race condition.

This sounds more like it's a bug in the i2c master rather than the 5007 driver.

Do you have i2c bus traces that clearly show that this was the cause of the issue? If we need to define something as "broken" behavior, at first glance it looks like the way *you're* proposing is the broken behavior - presumably to work around a bug in the i2c master not properly supporting repeated start.

Also, any reason you didn't put Mike into the cc: for this (since he owns the driver)?

Devin

Signature · 2 lines
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com
#3 lore raw

Re: [PATCH 1/5] mxl5007t: fix buggy register read

Quoted 9 lines · Devin Heitmueller
Quoted 8 lines · Antti Palosaari

Chip uses WRITE + STOP + READ + STOP sequence for I2C register read. Driver was using REPEATED START condition which makes it failing if I2C adapter was implemented correctly.

Add use_broken_read_reg_intentionally option to keep old buggy implantation as there is buggy I2C adapter implementation relying that bug...

Quoted 6 lines
Quoted 1 line

Hi Antti,

The existing code actually looks fine. This is actually how most devices do register reads.

Yes, most devices do that, but not all!
MxL5007t has a special register for setting register to read. Look the code and you could see it easily. It was over year ago I fixed it...

Quoted 2 lines

Further, it *should* be done in a single call to i2c_transfer() or else you won't hold the lock and you will create a race condition.

No. That's why I added new lock. Single i2c_transfer() means all messages are done using repeated START condition.

Quoted 1 line

This sounds more like it's a bug in the i2c master rather than the 5007 driver.

No.

Quoted 5 lines

Do you have i2c bus traces that clearly show that this was the cause of the issue? If we need to define something as "broken" behavior, at first glance it looks like the way *you're* proposing is the broken behavior - presumably to work around a bug in the i2c master not properly supporting repeated start.

Yes and no. I made own Cypress FX2 firmware and saw initially that issue then. Also, as you could see looking the following patches I ensured / confirmed issue using two different I2C adapters (AF9015 and AF9035). So I have totally 3 working adapters to prove it (which are broken without that patch)!

Quoted 2 lines

Also, any reason you didn't put Mike into the cc: for this (since he owns the driver)?

you added :)

Quoted 1 line

Devin

regards
Antti

Signature · 1 line
http://palosaari.fi/
#4 lore raw

Re: [PATCH 1/5] mxl5007t: fix buggy register read

Quoted 3 lines · Antti Palosaari

Yes, most devices do that, but not all!
MxL5007t has a special register for setting register to read. Look the code and you could see it easily. It was over year ago I fixed it...

That sounds kind of insane, but I haven't looked at the datasheets and if Mike Ack'd it, then so be it.

Quoted 6 lines
Quoted 2 lines

Further, it *should* be done in a single call to i2c_transfer() or else you won't hold the lock and you will create a race condition.

No. That's why I added new lock. Single i2c_transfer() means all messages are done using repeated START condition.

No need to add a new lock to your bridge driver. You can just use __i2c_transfer() and i2c_lock_adapter(state->i2c). That's what they're doing in the drx-k driver for just such cases where they need the lock to span multiple calls to i2c_transfer().

Quoted 15 lines
Quoted 3 lines

This sounds more like it's a bug in the i2c master rather than the 5007 driver.

No.

Quoted 5 lines

Do you have i2c bus traces that clearly show that this was the cause of the issue? If we need to define something as "broken" behavior, at first glance it looks like the way *you're* proposing is the broken behavior - presumably to work around a bug in the i2c master not properly supporting repeated start.

Quoted 5 lines

Yes and no. I made own Cypress FX2 firmware and saw initially that issue then. Also, as you could see looking the following patches I ensured / confirmed issue using two different I2C adapters (AF9015 and AF9035). So I have totally 3 working adapters to prove it (which are broken without that patch)!

The whole think sounds screwed up, and without a real i2c trace of the bus we'll never know. That said, I'm not really in a position to dispute it given I don't have any devices with the chip in question.

I would suggest renaming the configuration value to something less controversial, such as "use_repeated_start_for_reads" and avoid using terms like broken where it's not clear that's actually the case.

Devin

Signature · 2 lines
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com
#5 lore raw

Re: [PATCH 1/5] mxl5007t: fix buggy register read

Quoted 12 lines · Devin Heitmueller
Quoted 3 lines · Antti Palosaari

Yes, most devices do that, but not all!
MxL5007t has a special register for setting register to read. Look the code and you could see it easily. It was over year ago I fixed it...

That sounds kind of insane, but I haven't looked at the datasheets and if Mike Ack'd it, then so be it.

Quoted 4 lines
Quoted 2 lines

Further, it *should* be done in a single call to i2c_transfer() or else you won't hold the lock and you will create a race condition.

Quoted 7 lines
Quoted 2 lines

No. That's why I added new lock. Single i2c_transfer() means all messages are done using repeated START condition.

No need to add a new lock to your bridge driver. You can just use __i2c_transfer() and i2c_lock_adapter(state->i2c). That's what they're doing in the drx-k driver for just such cases where they need the lock to span multiple calls to i2c_transfer().

Thanks for the tip. It could be cleaner way to do it like that and likely I will change it after quick check.

I don't need to lock whole adapter, only unsure there is none changing "register to read" value (stored to special register) before I read it. So I have to ensure I could do normal writes without a unneeded waiting - even those happens just between that two phase read.

Quoted 20 lines
Quoted 14 lines
Quoted 2 lines

This sounds more like it's a bug in the i2c master rather than the 5007 driver.

No.

Quoted 5 lines

Do you have i2c bus traces that clearly show that this was the cause of the issue? If we need to define something as "broken" behavior, at first glance it looks like the way *you're* proposing is the broken behavior - presumably to work around a bug in the i2c master not properly supporting repeated start.

Quoted 5 lines

Yes and no. I made own Cypress FX2 firmware and saw initially that issue then. Also, as you could see looking the following patches I ensured / confirmed issue using two different I2C adapters (AF9015 and AF9035). So I have totally 3 working adapters to prove it (which are broken without that patch)!

Quoted 7 lines

The whole think sounds screwed up, and without a real i2c trace of the bus we'll never know. That said, I'm not really in a position to dispute it given I don't have any devices with the chip in question.

I would suggest renaming the configuration value to something less controversial, such as "use_repeated_start_for_reads" and avoid using terms like broken where it's not clear that's actually the case.

It is 100% clearly broken :-] And MxL5007t will not reply correct value. It returns 0x3f for chip ID. I didn't tested it, but quite good theory is that there is some other than chip id reg to set special register when that happens.

I have tested it with 3 devices and there is two devices to left which are using that new parameter.

regards
Antti

Signature · 1 line
http://palosaari.fi/