i2c: Add asserts for second smbus i2c_start_transfer()
PATCH 1/1 i2c: Add asserts for second smbus i2c_start_transfer()
From: Corey Minyard <[email protected]>
Some SMBus operations restart the transfer to convert from write to read mode without an intervening i2c_end_transfer(). The second call cannot fail, so the return code is unchecked, but this causes Coverity to complain. So add some asserts and documentation about this.
--- hw/i2c/core.c | 5 ++++- hw/i2c/smbus.c | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-)
I tested this with the IPMI SMbus driver for a while.
hw/i2c/core.c +4 -1
@@ -88,7 +88,10 @@ int i2c_bus_busy(I2CBus *bus)return !QLIST_EMPTY (&bus->current_devs); } -/* Returns non-zero if the address is not valid. */ +/* Returns non-zero if the address is not valid. If this is called +again without an intervening i2c_end_transfer(), like in the SMBus +case where the operation is switched from write to read, this +function will not rescan the bus and thus cannot fail. */ /* TODO: Make this handle multiple masters. */ int i2c_start_transfer (I2CBus *bus,uint8_t address,int recv) {
hw/i2c/smbus.c +3 -3
@@ -248,7 +248,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)return -1 ; }i2c_send (bus, command); -i2c_start_transfer (bus, addr,1 ); +assert (!i2c_start_transfer (bus, addr,1 )); data =i2c_recv (bus);i2c_nack (bus);i2c_end_transfer (bus);
@@ -273,7 +273,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)return -1 ; }i2c_send (bus, command); -i2c_start_transfer (bus, addr,1 ); +assert (!i2c_start_transfer (bus, addr,1 )); data =i2c_recv (bus); data |=i2c_recv (bus) <<8 ;i2c_nack (bus);
@@ -302,7 +302,7 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)return -1 ; }i2c_send (bus, command); -i2c_start_transfer (bus, addr,1 ); +assert (!i2c_start_transfer (bus, addr,1 )); len =i2c_recv (bus);if (len >32 ) { len =0 ;
Signature · 1 line
2.7.4
Quoted 21 lines · On 24 October 2016 at 16:10, <[email protected]> wrote:
From: Corey Minyard <[email protected]>
Some SMBus operations restart the transfer to convert from write to read mode without an intervening
i2c_end_transfer(). The second call cannot fail, so the return code is unchecked, but this causes Coverity to complain. So add some asserts and documentation about this.Signed-off-by Corey Minyard <[email protected]>--- hw/i2c/core.c | 5 ++++- hw/i2c/smbus.c | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-)I tested this with the IPMI SMbus driver for a while.
hw/i2c/core.c +0
-0
hw/i2c/core.c int i2c_bus_busy(I2CBus *bus) · line 94
Quoted 8 lines
hw/i2c/core.c +4
-1@@ -88,7 +88,10 @@ int i2c_bus_busy(I2CBus *bus)return !QLIST_EMPTY (&bus->current_devs); } -/* Returns non-zero if the address is not valid. */ +/* Returns non-zero if the address is not valid. If this is called +again without an intervening i2c_end_transfer(), like in the SMBus +case where the operation is switched from write to read, this +function will not rescan the bus and thus cannot fail. */
A nit, but comment syntax convention in this file seems to be to have this kind of long-comment:
/*
* text text text
* text text text
*/
hw/i2c/smbus.c int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command) · line 251
Quoted 13 lines
hw/i2c/core.c +0
-0/* TODO: Make this handle multiple masters. */ int i2c_start_transfer (I2CBus *bus,uint8_t address,int recv) {hw/i2c/smbus.c +1
-1@@ -248,7 +248,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)return -1 ; }i2c_send (bus, command); -i2c_start_transfer (bus, addr,1 ); +assert (!i2c_start_transfer (bus, addr,1 ));
Better not to put expressions that have side-effects inside assert() -- if the assert() is compiled out then the expression inside it will never be evaluated...
thanks
-- PMM