From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bygu5-0007P4-FC for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:11:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bygu0-00037B-TC for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:11:09 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:36354) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bygu0-00036c-Kb for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:11:04 -0400 Received: by mail-pf0-x244.google.com with SMTP id r16so16818454pfg.3 for ; Mon, 24 Oct 2016 08:11:04 -0700 (PDT) Sender: Corey Minyard From: minyard@acm.org Date: Mon, 24 Oct 2016 10:10:54 -0500 Message-Id: <1477321854-8080-1-git-send-email-minyard@acm.org> Subject: [Qemu-devel] [PATCH] i2c: Add asserts for second smbus i2c_start_transfer() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: QEMU Developers , Corey Minyard , Alistair Francis , KONRAD Frederic From: Corey Minyard 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 --- 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. diff --git a/hw/i2c/core.c b/hw/i2c/core.c index bd8f167..b9dea79 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -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) { diff --git a/hw/i2c/smbus.c b/hw/i2c/smbus.c index 3979b3d..bf46d22 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -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; -- 2.7.4