From patchwork Mon Oct 24 15:42:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Corey Minyard X-Patchwork-Id: 78985 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp2650911qge; Mon, 24 Oct 2016 08:52:44 -0700 (PDT) X-Received: by 10.28.212.17 with SMTP id l17mr21915625wmg.64.1477324364389; Mon, 24 Oct 2016 08:52:44 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id t203si12759144wmg.9.2016.10.24.08.52.44 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 24 Oct 2016 08:52:44 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org Received: from localhost ([::1]:47619 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1byhYJ-0000Ia-7L for patch@linaro.org; Mon, 24 Oct 2016 11:52:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47966) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1byhOj-0001kg-BJ for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:42:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1byhOi-00018H-Er for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:42:49 -0400 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]:34160) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1byhOi-00017L-5v for qemu-devel@nongnu.org; Mon, 24 Oct 2016 11:42:48 -0400 Received: by mail-pf0-x244.google.com with SMTP id 128so16918878pfz.1 for ; Mon, 24 Oct 2016 08:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id; bh=Rm2cwXkWW2kdReiB+LQagnfB89AUBB77Ri0OJef3bqs=; b=ih69BYsk5plCWfhLgyNouvGmkcdbUilUz7S45ClVAgTwCj+BK7tAVzD0G1lVIh6sL9 bUrtLu772k6L5KUDNLOMhIg7F04XnejhDq/foiOd6bABGXMll5heGQ8Pdj31PWTFpR9I Bi2/ZOYFThySl/lXaQe3Q8lKH+apE+1ixv7C71pew2CYNscTsBh3S4O9RpWLRtAEhFdm bzFH92pZ3LhxO9ku0NQ4w2Xu4wMsQEaeWS/fNUcEFkd+osFBXe00ZdIJ9cyOkMVQYbcr T93Z6f4EoDJ9HDKYTP4HMYumwAgb1Z978YVwZn/Vht09go4JY7VkQECaGHYK0q58RQt9 8IjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=Rm2cwXkWW2kdReiB+LQagnfB89AUBB77Ri0OJef3bqs=; b=O3Hie4+oHapiV97U9/+5CAh2A4KrFoUNMANJomI4UjnE7g9NB758cGbZE2ja5RGnjO WFZY7Y3Y1NxtTIUIlFbXof1yt09dQyP+3NsZm0X39Auesm4ILHRE/Yx/yL+bcLTtyAVd O/0MONNRbBtj5B7wHKqKHEZdi+oMT91zw6her6UA2Ov49laED8UE0Mhr629BV5yDD5Gz h5TMI8zH+okEJTa+uleTEXPF5CQ+yBUpKEYXOg+94Faa6cEVD4iWv8KPA/q5l0dJ0nRn nS2iBU3t97BWTvZgSQ+PSYpQ4GOxce5zPBFOtegX1nZyIP/GeHGLpu3yaYB2j1z9YI1r 5xYg== X-Gm-Message-State: ABUngvdajcrMAZiEwz0gjmyiG5ZgcGmARFaAb5iaHQ/qkqmXVvrKP8+6weRL2/cCLh2Wog== X-Received: by 10.98.106.196 with SMTP id f187mr29695923pfc.91.1477323766339; Mon, 24 Oct 2016 08:42:46 -0700 (PDT) Received: from serve.minyard.net (serve.minyard.net. [2001:470:b8f6:1b::1]) by smtp.gmail.com with ESMTPSA id g9sm26410899paw.40.2016.10.24.08.42.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Oct 2016 08:42:45 -0700 (PDT) Received: from t430.minyard.net (unknown [IPv6:2001:470:b8f6:1b:785f:c781:63d2:a774]) by serve.minyard.net (Postfix) with ESMTPA id ACE5FCD1; Mon, 24 Oct 2016 10:42:43 -0500 (CDT) Received: by t430.minyard.net (Postfix, from userid 1000) id 3B20C300070; Mon, 24 Oct 2016 10:42:43 -0500 (CDT) From: minyard@acm.org To: Peter Maydell Date: Mon, 24 Oct 2016 10:42:33 -0500 Message-Id: <1477323753-20768-1-git-send-email-minyard@acm.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::244 Subject: [Qemu-devel] [PATCH v2] i2c: Add asserts for second smbus i2c_start_transfer() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Corey Minyard , KONRAD Frederic , QEMU Developers , Alistair Francis Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 | 7 ++++++- hw/i2c/smbus.c | 12 +++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) Changes since v1: * Fixed the comments, I should have run it through checkpatch. * Fixed the asserts to avoid side effects. -- 2.7.4 diff --git a/hw/i2c/core.c b/hw/i2c/core.c index bd8f167..abd4c4c 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -88,7 +88,12 @@ 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..5b4dd3e 100644 --- a/hw/i2c/smbus.c +++ b/hw/i2c/smbus.c @@ -248,7 +248,9 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command) return -1; } i2c_send(bus, command); - i2c_start_transfer(bus, addr, 1); + if (i2c_start_transfer(bus, addr, 1)) { + assert(0); + } data = i2c_recv(bus); i2c_nack(bus); i2c_end_transfer(bus); @@ -273,7 +275,9 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command) return -1; } i2c_send(bus, command); - i2c_start_transfer(bus, addr, 1); + if (i2c_start_transfer(bus, addr, 1)) { + assert(0); + } data = i2c_recv(bus); data |= i2c_recv(bus) << 8; i2c_nack(bus); @@ -302,7 +306,9 @@ 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); + if (i2c_start_transfer(bus, addr, 1)) { + assert(0); + } len = i2c_recv(bus); if (len > 32) { len = 0;