i2c: recovery: make sure pulses are not misinterpreted
cover i2c: recovery: make sure pulses are not misinterpreted
As the 'incomplete_write_byte' fault injector has shown, we need to take care that pulses used for recovery will not be misinterpreted as a regular cycle. This series implements that. This approach makes sure that STOP signals put on the bus do not create additional pulses. We still do 9 pulses only, but generate a STOP signal in case SDA was released again (and we cannot read SDA status).
The first patch implements it in a minimal way, so it is suitable for stable. The second patch adds some sanity checking and documentation on top which is not suitable for stable.
Looking forward to comments.
Kind regards,
Wolfram
Wolfram Sang (2):
i2c: recovery: send STOP if get_sda is not available
i2c: recovery: require either get_sda or set_sda
drivers/i2c/i2c-core-base.c | 16 +++++++++++++++- include/linux/i2c.h | 12 ++++++------ 2 files changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/2 i2c: recovery: send STOP if get_sda is not available
If we can't read SDA to bail out early and toggle too many pulses, slaves may misunderstand these pulses. In the worst case, as a write operation. To avoid that and if we can write SDA, try to send STOP to avoid the misinterpretation.
--- drivers/i2c/i2c-core-base.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/i2c/i2c-core-base.c +10 -1
@@ -198,7 +198,16 @@ int i2c_generic_scl_recovery(struct i2c_adapter *adap) val = !val; bri->set_scl (adap, val); -ndelay (RECOVERY_NDELAY ); + +/* +* If we can't read SDA, we don't know when to bail out early. +* So, always create STOP then to ensure the additional pulses +* will do no harm. +*/ +ndelay (RECOVERY_NDELAY /2 ); +if (!bri->get_sda && bri->set_sda) + bri->set_sda (adap, val); +ndelay (RECOVERY_NDELAY /2 ); }/* check if recovery actually succeeded */
Signature · 1 line
2.11.0
PATCH 2/2 i2c: recovery: require either get_sda or set_sda
In reply to Wolfram SangFor bus recovery, we either need to bail out early if we can read SDA or we need to send STOP after every pulse. Otherwise recovery might be misinterpreted as an unwanted write. So, require one of those SDA handling functions to avoid this problem.
--- drivers/i2c/i2c-core-base.c | 9 +++++++-- include/linux/i2c.h | 12 ++++++------ 2 files changed, 13 insertions(+), 8 deletions(-)
drivers/i2c/i2c-core-base.c +7 -2
@@ -202,10 +202,11 @@ int i2c_generic_scl_recovery(struct i2c_adapter *adap)/* * If we can't read SDA, we don't know when to bail out early. * So, always create STOP then to ensure the additional pulses -* will do no harm. +* will do no harm. Check the 'incomplete_write_byte' fault +* injector for details. */ ndelay (RECOVERY_NDELAY /2 ); -if (!bri->get_sda && bri->set_sda) +if (!bri->get_sda) bri->set_sda (adap, val);ndelay (RECOVERY_NDELAY /2 ); }
@@ -274,6 +275,10 @@ static void i2c_init_recovery(struct i2c_adapter *adap) err_str ="no {get|set}_scl() found" ; goto err; } +if (!bri->set_sda && !bri->get_sda) { + err_str ="either get_sda() or set_sda() needed" ; + goto err; + } }return ;
include/linux/i2c.h +6 -6
@@ -576,12 +576,12 @@ struct i2c_timings {* recovery. Populated internally for generic GPIO recovery. * @set_scl: This sets/clears the SCL line. Mandatory for generic SCL recovery. * Populated internally for generic GPIO recovery. -* @get_sda: This gets current value of SDA line. Optional for generic SCL -* recovery. Populated internally, if sda_gpio is a valid GPIO, for generic -* GPIO recovery. -* @set_sda: This sets/clears the SDA line. Optional for generic SCL recovery. -* Populated internally, if sda_gpio is a valid GPIO, for generic GPIO -* recovery. +* @get_sda: This gets current value of SDA line. This or set_sda() is mandatory +* for generic SCL recovery. Populated internally, if sda_gpio is a valid +* GPIO, for generic GPIO recovery. +* @set_sda: This sets/clears the SDA line. This or get_sda() is mandatory for +* generic SCL recovery. Populated internally, if sda_gpio is a valid GPIO, +* for generic GPIO recovery. * @prepare_recovery: This will be called before starting recovery. Platform may * configure padmux here for SDA/SCL line or something else they want. * @unprepare_recovery: This will be called after completing recovery. Platform
Signature · 1 line
2.11.0