From patchwork Tue Jun 14 14:13:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 9175975 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C72D76075D for ; Tue, 14 Jun 2016 14:44:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B69712012F for ; Tue, 14 Jun 2016 14:44:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A788A282F9; Tue, 14 Jun 2016 14:44:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 44F0F2012F for ; Tue, 14 Jun 2016 14:44:32 +0000 (UTC) Received: from localhost ([::1]:35768 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCpZv-00086p-CZ for patchwork-qemu-devel@patchwork.kernel.org; Tue, 14 Jun 2016 10:44:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42764) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCp6i-0003TF-3y for qemu-devel@nongnu.org; Tue, 14 Jun 2016 10:14:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bCp6f-00088O-RE for qemu-devel@nongnu.org; Tue, 14 Jun 2016 10:14:18 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:57715) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bCp6f-00087d-JR for qemu-devel@nongnu.org; Tue, 14 Jun 2016 10:14:17 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1bCp6f-0006Hf-6W for qemu-devel@nongnu.org; Tue, 14 Jun 2016 15:14:17 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 14 Jun 2016 15:13:58 +0100 Message-Id: <1465913645-19346-24-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1465913645-19346-1-git-send-email-peter.maydell@linaro.org> References: <1465913645-19346-1-git-send-email-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PULL 23/30] i2c: Factor our send() and recv() common logic 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: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Crosthwaite Most of the control flow logic between send and recv (error checking etc) is the same. Factor this out into a common send_recv() API. This is then usable by clients, where the control logic for send and receive differs only by a boolean. E.g. if (send) i2c_send(...): else i2c_recv(...); becomes: i2c_send_recv(... , send); Signed-off-by: Peter Crosthwaite Reviewed-by: Alistair Francis Signed-off-by: KONRAD Frederic Message-id: 1465833014-21982-4-git-send-email-fred.konrad@greensocs.com Changes from FK: * Rebased on master. * Rebased on my i2c broadcast patch. Signed-off-by: KONRAD Frederic Reviewed-by: Alistair Francis Signed-off-by: Peter Maydell --- hw/i2c/core.c | 52 ++++++++++++++++++++++++++++++++++------------------ include/hw/i2c/i2c.h | 1 + 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/hw/i2c/core.c b/hw/i2c/core.c index 1fa8268..abb3efb 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -149,36 +149,52 @@ void i2c_end_transfer(I2CBus *bus) bus->broadcast = false; } -int i2c_send(I2CBus *bus, uint8_t data) +int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send) { I2CSlaveClass *sc; I2CNode *node; int ret = 0; - QLIST_FOREACH(node, &bus->current_devs, next) { - sc = I2C_SLAVE_GET_CLASS(node->elt); - if (sc->send) { - ret = ret || sc->send(node->elt, data); - } else { - ret = -1; + if (send) { + QLIST_FOREACH(node, &bus->current_devs, next) { + sc = I2C_SLAVE_GET_CLASS(node->elt); + if (sc->send) { + ret = ret || sc->send(node->elt, *data); + } else { + ret = -1; + } + } + return ret ? -1 : 0; + } else { + if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) { + return -1; } + + sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt); + if (sc->recv) { + ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt); + if (ret < 0) { + return ret; + } else { + *data = ret; + return 0; + } + } + return -1; } - return ret ? -1 : 0; } -int i2c_recv(I2CBus *bus) +int i2c_send(I2CBus *bus, uint8_t data) { - I2CSlaveClass *sc; + return i2c_send_recv(bus, &data, true); +} - if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) { - return -1; - } +int i2c_recv(I2CBus *bus) +{ + uint8_t data; + int ret = i2c_send_recv(bus, &data, false); - sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt); - if (sc->recv) { - return sc->recv(QLIST_FIRST(&bus->current_devs)->elt); - } - return -1; + return ret < 0 ? ret : data; } void i2c_nack(I2CBus *bus) diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h index 4986ebc..c4085aa 100644 --- a/include/hw/i2c/i2c.h +++ b/include/hw/i2c/i2c.h @@ -56,6 +56,7 @@ int i2c_bus_busy(I2CBus *bus); int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv); void i2c_end_transfer(I2CBus *bus); void i2c_nack(I2CBus *bus); +int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send); int i2c_send(I2CBus *bus, uint8_t data); int i2c_recv(I2CBus *bus);