Skip to contentSkip to conversation

i2c: add "reset" sysfs entry for adapters.

PATCH In review Rodolfo Giometti 5 participants 13 messages Feb 6, 2009 – Feb 13, 2009 8 messages not in the archive

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

PATCH 1/2 i2c: add "reset" sysfs entry for adapters.

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset
---
 drivers/i2c/i2c-core.c |   21 +++++++++++++++++++++
 include/linux/i2c.h    |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +21 -0
index b1c9abe..b0c4053 100644
@@ -393,8 +393,29 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 	return sprintf(buf, "%s\n", adap->name);
 }
 
+static ssize_t
+write_adapter_reset(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct i2c_adapter *adap = to_i2c_adapter(dev);
+	unsigned long val;
+	int ret;
+
+	if (!adap->reset)
+		return -EINVAL;
+
+	ret = strict_strtoul(buf, 10, &val);
+	if (ret || val != 1)
+		return -EINVAL;
+
+	adap->reset(adap);
+
+	return count;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
 	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
+	__ATTR(reset, S_IWUSR, NULL, write_adapter_reset),
 	{ },
 };
 
include/linux/i2c.h +2 -0
index fcfbfea..0f84345 100644
@@ -369,6 +369,8 @@ struct i2c_adapter {
 	struct list_head clients;	/* DEPRECATED */
 	char name[48];
 	struct completion dev_released;
+
+	void (*reset)(struct i2c_adapter *);	/* user request adap reset */
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
Signature · 1 line
1.5.6.3

A message that is not in the archive (sent off-list or not yet received): 1233933798-17673-1-git-send-email-giometti-k2GhghHVRtY@public.gmane.org

#2 lore raw

PATCH 2/2 i2c pxa: add the force reset method.

---
 drivers/i2c/busses/i2c-pxa.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/i2c/busses/i2c-pxa.c +8 -0
index 6af6814..ccbc9ba 100644
@@ -963,6 +963,13 @@ static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num
 	return ret;
 }
 
+static void i2c_pxa_force_reset(struct i2c_adapter *adap)
+{
+	struct pxa_i2c *i2c = adap->algo_data;
+
+	i2c_pxa_reset(i2c);
+}
+
 static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
 {
 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
@@ -1065,6 +1072,7 @@ static int i2c_pxa_probe(struct platform_device *dev)
 
 	i2c->adap.algo_data = i2c;
 	i2c->adap.dev.parent = &dev->dev;
+	i2c->adap.reset	  = i2c_pxa_force_reset;
 
 	ret = i2c_add_numbered_adapter(&i2c->adap);
 	if (ret < 0) {
Signature · 1 line
1.5.6.3
#3 lore raw
In reply to a message not in the archive
Quoted 6 lines · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

There doesn't seem to be any locking to stop issuing a reset during an extant transaction... should the controller driver deal with cancelling any outstanding transactions?

Quoted 59 lines
---
 drivers/i2c/i2c-core.c |   21 +++++++++++++++++++++
 include/linux/i2c.h    |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +21 -0
index b1c9abe..b0c4053 100644
@@ -393,8 +393,29 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 	return sprintf(buf, "%s\n", adap->name);
 }
 
+static ssize_t
+write_adapter_reset(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct i2c_adapter *adap = to_i2c_adapter(dev);
+	unsigned long val;
+	int ret;
+
+	if (!adap->reset)
+		return -EINVAL;
+
+	ret = strict_strtoul(buf, 10, &val);
+	if (ret || val != 1)
+		return -EINVAL;
+
+	adap->reset(adap);
+
+	return count;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
 	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
+	__ATTR(reset, S_IWUSR, NULL, write_adapter_reset),
 	{ },
 };
 
include/linux/i2c.h +2 -0
index fcfbfea..0f84345 100644
@@ -369,6 +369,8 @@ struct i2c_adapter {
 	struct list_head clients;	/* DEPRECATED */
 	char name[48];
 	struct completion dev_released;
+
+	void (*reset)(struct i2c_adapter *);	/* user request adap reset */
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
Signature · 6 lines
1.5.6.3

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 3 lines
Ben ([email protected], http://www.fluff.org/)

  'a smiley only costs 4 bytes'

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 11 lines · Ben Dooks
Quoted 6 lines · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

There doesn't seem to be any locking to stop issuing a reset during an extant transaction... should the controller driver deal with cancelling any outstanding transactions?

I think this should be done inside each adapter driver since it's an adapter specific issue.

If you see next patch of this patchset you can see that the adapter i2c-pxa aborts any transfer currently under way during reset.

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: giometti-AVVDYK/kqiJWk0Htik3J/[email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti

A message that is not in the archive (sent off-list or not yet received): 20090209150255.GA7975-AVVDYK/kqiJWk0Htik3J/[email protected]

#7 lore raw
Quoted 13 lines · Rodolfo Giometti
Quoted 11 lines · Ben Dooks
Quoted 6 lines · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

There doesn't seem to be any locking to stop issuing a reset during an extant transaction... should the controller driver deal with cancelling any outstanding transactions?

Quoted 5 lines

I think this should be done inside each adapter driver since it's an adapter specific issue.

If you see next patch of this patchset you can see that the adapter i2c-pxa aborts any transfer currently under way during reset.

I disagree. An i2c bus driver should never get stuck in the middle of a transaction (that is, with i2c_adapter->bus_lock held.) If a transaction fails, the driver should timeout and return. Thus I think Ben is right and it would make a lot of sense to have function write_adapter_reset take i2c_adapter->bus_lock. This guarantees that all bus drivers will get locking right and won't allow resetting in the middle of a (non-stuck) transaction.

Signature · 1 line
Jean Delvare

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 20 lines · Jean Delvare
Quoted 13 lines · Rodolfo Giometti
Quoted 11 lines · Ben Dooks
Quoted 6 lines · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

There doesn't seem to be any locking to stop issuing a reset during an extant transaction... should the controller driver deal with cancelling any outstanding transactions?

Quoted 5 lines

I think this should be done inside each adapter driver since it's an adapter specific issue.

If you see next patch of this patchset you can see that the adapter i2c-pxa aborts any transfer currently under way during reset.

Quoted 7 lines

I disagree. An i2c bus driver should never get stuck in the middle of a transaction (that is, with i2c_adapter->bus_lock held.) If a transaction fails, the driver should timeout and return. Thus I think Ben is right and it would make a lot of sense to have function write_adapter_reset take i2c_adapter->bus_lock. This guarantees that all bus drivers will get locking right and won't allow resetting in the middle of a (non-stuck) transaction.

Ok, I'll modify the code as you suggested ASAP.

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: giometti-AVVDYK/kqiJWk0Htik3J/[email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
#10 lore raw
In reply to a message not in the archive
Quoted 1 line · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due

Typo: "due due" -> "due to".

Quoted 3 lines

electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

What is the intended purpose, debugging? I would certainly hope that drivers know by themselves when they need a reset. Requiring users to reset themselves doesn't sound terribly friendly :(

Quoted 20 lines
---
 drivers/i2c/i2c-core.c |   21 +++++++++++++++++++++
 include/linux/i2c.h    |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +6 -0
index b1c9abe..b0c4053 100644
@@ -393,8 +393,29 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 	return sprintf(buf, "%s\n", adap->name);
 }
 
+static ssize_t
+write_adapter_reset(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct i2c_adapter *adap = to_i2c_adapter(dev);
+	unsigned long val;

drivers/i2c/i2c-core.c show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) · line 411

Quoted 10 lines
drivers/i2c/i2c-core.c +10 -0
+	int ret;
+
+	if (!adap->reset)
+		return -EINVAL;
+
+	ret = strict_strtoul(buf, 10, &val);
+	if (ret || val != 1)
+		return -EINVAL;
+
+	adap->reset(adap);

I consider it good practice to always test for function pointers _right_ before dereferencing them. Makes it harder to accidentally break things later.

Quoted 23 lines
drivers/i2c/i2c-core.c +5 -0
+
+	return count;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
 	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
+	__ATTR(reset, S_IWUSR, NULL, write_adapter_reset),
 	{ },
 };
 
include/linux/i2c.h +2 -0
index fcfbfea..0f84345 100644
@@ -369,6 +369,8 @@ struct i2c_adapter {
 	struct list_head clients;	/* DEPRECATED */
 	char name[48];
 	struct completion dev_released;
+
+	void (*reset)(struct i2c_adapter *);	/* user request adap reset */
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
Signature · 1 line
Jean Delvare

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw
Quoted 4 lines · Jean Delvare
Quoted 1 line · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due

Typo: "due due" -> "due to".

Ok.

Quoted 7 lines
Quoted 3 lines

electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

What is the intended purpose, debugging? I would certainly hope that drivers know by themselves when they need a reset. Requiring users to reset themselves doesn't sound terribly friendly :(

Unluckely not all adapter does it by themselfes! At least not PXA one. I'm currently work on adding this new feature.

Quoted 26 lines
Quoted 26 lines
---
 drivers/i2c/i2c-core.c |   21 +++++++++++++++++++++
 include/linux/i2c.h    |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +12 -0
index b1c9abe..b0c4053 100644
@@ -393,8 +393,29 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 	return sprintf(buf, "%s\n", adap->name);
 }
 
+static ssize_t
+write_adapter_reset(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct i2c_adapter *adap = to_i2c_adapter(dev);
+	unsigned long val;
+	int ret;
+
+	if (!adap->reset)
+		return -EINVAL;
+
+	ret = strict_strtoul(buf, 10, &val);
Quoted 8 lines

drivers/i2c/i2c-core.c show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) · line 411

Quoted 4 lines
drivers/i2c/i2c-core.c +4 -0
+	if (ret || val != 1)
+		return -EINVAL;
+
+	adap->reset(adap);

I consider it good practice to always test for function pointers _right_ before dereferencing them. Makes it harder to accidentally break things later.

Ok.

Quoted 10 lines
Quoted 10 lines
drivers/i2c/i2c-core.c +5 -0
+
+	return count;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
 	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
+	__ATTR(reset, S_IWUSR, NULL, write_adapter_reset),
 	{ },
 };
 
Quoted 13 lines

include/linux/i2c.h struct i2c_adapter { · line 376

Quoted 13 lines
include/linux/i2c.h +2 -0
index fcfbfea..0f84345 100644
@@ -369,6 +369,8 @@ struct i2c_adapter {
 	struct list_head clients;	/* DEPRECATED */
 	char name[48];
 	struct completion dev_released;
+
+	void (*reset)(struct i2c_adapter *);	/* user request adap reset */
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 

I'll repropose a new patch ASAP.

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: giometti-AVVDYK/kqiJWk0Htik3J/[email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti

A message that is not in the archive (sent off-list or not yet received): 20090209204108.GH7975-AVVDYK/kqiJWk0Htik3J/[email protected]

#14 lore raw
Quoted 10 lines · Rodolfo Giometti
Quoted 4 lines · Jean Delvare
Quoted 1 line · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due

Typo: "due due" -> "due to".

Ok.

Quoted 2 lines
Quoted 2 lines

electrical problems, so the user may recover this stale state by using:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

What is the intended purpose, debugging? I would certainly hope that drivers know by themselves when they need a reset. Requiring users to reset themselves doesn't sound terribly friendly :(

Unluckely not all adapter does it by themselfes! At least not PXA one. I'm currently work on adding this new feature.

Well, if you are going to fix the i2c-pxa driver, and it's the only driver using the manual reset feature, then do we really need to add this feature?

Don't get me wrong, if this feature is still useful then I am fine adding it. But OTOH we still have to think twice when doing such core changes, because they increase the memory footprint for all systems.

I am curious what other developers think. Me, I never felt the need for a manual reset feature. Usually when I have managed to break an I2C controller, cycling the driver would fix it, or if not, it was so broken that I had to power-cycle the box anyway.

Are there other developers out there who consider the proposed manual reset feature a good thing to have? Please speak up.

Signature · 1 line
Jean Delvare

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw
Quoted 17 lines · Jean Delvare
Quoted 16 lines · Rodolfo Giometti
Quoted 4 lines · Jean Delvare
Quoted 1 line · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due

Typo: "due due" -> "due to".

Ok.

Quoted 7 lines
Quoted 3 lines

electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

What is the intended purpose, debugging? I would certainly hope that drivers know by themselves when they need a reset. Requiring users to reset themselves doesn't sound terribly friendly :(

Quoted 6 lines
Quoted 2 lines

Unluckely not all adapter does it by themselfes! At least not PXA one. I'm currently work on adding this new feature.

Well, if you are going to fix the i2c-pxa driver, and it's the only driver using the manual reset feature, then do we really need to add this feature?

I don't know about other controllers, I know about PXA one because I'm currently working on it. :)

Quoted 4 lines

Don't get me wrong, if this feature is still useful then I am fine adding it. But OTOH we still have to think twice when doing such core changes, because they increase the memory footprint for all systems.

I agree. Maybe I can add such entry with a conditional #ifdef DEBUG and setting it off by default?

Quoted 8 lines

I am curious what other developers think. Me, I never felt the need for a manual reset feature. Usually when I have managed to break an I2C controller, cycling the driver would fix it, or if not, it was so broken that I had to power-cycle the box anyway.

Are there other developers out there who consider the proposed manual reset feature a good thing to have? Please speak up.

However, regarding PXA controller I found a way to automagically reset it when it locks. I'll send a patchset ASAP.

Ciao,

Rodolfo

Signature · 4 lines
GNU/Linux Solutions                  e-mail: giometti-AVVDYK/kqiJWk0Htik3J/[email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti

A message that is not in the archive (sent off-list or not yet received): 20090211130028.GM8639-AVVDYK/kqiJWk0Htik3J/[email protected]

#18 lore raw

Sorry, I missed the original post.

Quoted 2 lines
Quoted 2 lines

Are there other developers out there who consider the proposed manual reset feature a good thing to have? Please speak up.

Hmm, never needed it so far. Can't really think of a reason for it besides debugging...

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#19 lore raw
In reply to a message not in the archive
Quoted 29 lines · Rodolfo Giometti
Quoted 17 lines · Jean Delvare
Quoted 16 lines · Rodolfo Giometti
Quoted 4 lines · Jean Delvare
Quoted 1 line · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due

Typo: "due due" -> "due to".

Ok.

Quoted 7 lines
Quoted 3 lines

electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset

What is the intended purpose, debugging? I would certainly hope that drivers know by themselves when they need a reset. Requiring users to reset themselves doesn't sound terribly friendly :(

Quoted 6 lines
Quoted 2 lines

Unluckely not all adapter does it by themselfes! At least not PXA one. I'm currently work on adding this new feature.

Well, if you are going to fix the i2c-pxa driver, and it's the only driver using the manual reset feature, then do we really need to add this feature?

I don't know about other controllers, I know about PXA one because I'm currently working on it. :)

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Don't get me wrong, if this feature is still useful then I am fine adding it. But OTOH we still have to think twice when doing such core changes, because they increase the memory footprint for all systems.

I agree. Maybe I can add such entry with a conditional #ifdef DEBUG and setting it off by default?

Yes, I would make it conditional upon CONFIG_I2C_DEBUG_BUS, and possibly make it a private attribute of i2c-pxa if no other developers are interested.

Quoted 10 lines
Quoted 7 lines

I am curious what other developers think. Me, I never felt the need for a manual reset feature. Usually when I have managed to break an I2C controller, cycling the driver would fix it, or if not, it was so broken that I had to power-cycle the box anyway.

Are there other developers out there who consider the proposed manual reset feature a good thing to have? Please speak up.

However, regarding PXA controller I found a way to automagically reset it when it locks. I'll send a patchset ASAP.

Well, then I'm not sure if we still care about this debugging patch...

Signature · 1 line
Jean Delvare
#20 lore raw
In reply to a message not in the archive
Quoted 19 lines · Rodolfo Giometti

It could happen that an i2c adapter may lock the bus due due electrical problems, so the user may recover this stale state by using:

	$ echo 1 > /sys/class/i2c-adapter/i2c-0/reset
---
 drivers/i2c/i2c-core.c |   21 +++++++++++++++++++++
 include/linux/i2c.h    |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)
drivers/i2c/i2c-core.c +0 -0
index b1c9abe..b0c4053 100644
@@ -393,8 +393,29 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
 	return sprintf(buf, "%s\n", adap->name);
 }
 

drivers/i2c/i2c-core.c show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) · line 405

Quoted 10 lines
drivers/i2c/i2c-core.c +10 -0
+static ssize_t
+write_adapter_reset(struct device *dev, struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct i2c_adapter *adap = to_i2c_adapter(dev);
+	unsigned long val;
+	int ret;
+
+	if (!adap->reset)
+		return -EINVAL;

On second thought I believe that -ENOTSUP (or -EOPNOTSUPP) would be a better error code for this case.

Quoted 29 lines
drivers/i2c/i2c-core.c +11 -0
+
+	ret = strict_strtoul(buf, 10, &val);
+	if (ret || val != 1)
+		return -EINVAL;
+
+	adap->reset(adap);
+
+	return count;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
 	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
+	__ATTR(reset, S_IWUSR, NULL, write_adapter_reset),
 	{ },
 };
 
include/linux/i2c.h +2 -0
index fcfbfea..0f84345 100644
@@ -369,6 +369,8 @@ struct i2c_adapter {
 	struct list_head clients;	/* DEPRECATED */
 	char name[48];
 	struct completion dev_released;
+
+	void (*reset)(struct i2c_adapter *);	/* user request adap reset */
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
Signature · 1 line
Jean Delvare