From patchwork Mon Feb 1 15:43:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?S09OUkFEIEZyw6lkw6lyaWM=?= X-Patchwork-Id: 8180851 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9F98DBEEE5 for ; Mon, 1 Feb 2016 15:44:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DF034201EC for ; Mon, 1 Feb 2016 15:44:23 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id 0D1872011E for ; Mon, 1 Feb 2016 15:44:23 +0000 (UTC) Received: from localhost ([::1]:52939 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aQGeM-0007iS-1T for patchwork-qemu-devel@patchwork.kernel.org; Mon, 01 Feb 2016 10:44:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54029) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aQGe9-0007i0-Rv for qemu-devel@nongnu.org; Mon, 01 Feb 2016 10:44:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aQGe7-0004x6-Nn for qemu-devel@nongnu.org; Mon, 01 Feb 2016 10:44:09 -0500 Received: from greensocs.com ([193.104.36.180]:35892) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aQGe7-0004ws-EH for qemu-devel@nongnu.org; Mon, 01 Feb 2016 10:44:07 -0500 Received: from localhost (localhost [127.0.0.1]) by greensocs.com (Postfix) with ESMTP id D17E81D9B8D; Mon, 1 Feb 2016 16:44:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1454341446; bh=d9EYLt/Gb2NujEBac2EyigOoMaaY6p8BNvtT6Rr2Pts=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pmhKRIIr2uI4nSPeLgDT34zu85FvPwz5yuj7Xk9sXf27OhD4kSfq31P1wOQAaolse Rp6euvfj3W83FuSHOBwICwO7dsi1bPnpJBMpgaQXaIinqG/++Bue9IDByy5iAubA1x ZvAL9K1dKpaHETgMDsTD6K54ErB6jL9jX2Vyvz0Q= X-Virus-Scanned: amavisd-new at greensocs.com Authentication-Results: gs-01.greensocs.com (amavisd-new); dkim=pass (1024-bit key) header.d=greensocs.com Received: from greensocs.com ([127.0.0.1]) by localhost (gs-01.greensocs.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KVHXURElvG49; Mon, 1 Feb 2016 16:44:05 +0100 (CET) Received: from localhost.localdomain (AToulouse-659-1-6-64.w109-215.abo.wanadoo.fr [109.215.41.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: fred.konrad@greensocs.com) by greensocs.com (Postfix) with ESMTPSA id 9DA601D9BD8; Mon, 1 Feb 2016 16:44:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1454341445; bh=d9EYLt/Gb2NujEBac2EyigOoMaaY6p8BNvtT6Rr2Pts=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=7q9Dr7cDnI93+U7WIN3NqK0iwE+Vq2S0NzZJmjYFMlU8ydmPtAH6zItG5dkMBtWcC VUamgiLgoDILf1iPU0UKazaPQ6hy4U2m1UVbYC5wlZ/Ka2ZjlQ+HtSWpF676HWoUAi uQePJ5LnAmSIAIZlNOCpsFyzbf4J/P5ohMTgFJiQ= From: fred.konrad@greensocs.com To: qemu-devel@nongnu.org Date: Mon, 1 Feb 2016 16:43:32 +0100 Message-Id: <1454341418-24227-4-git-send-email-fred.konrad@greensocs.com> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1454341418-24227-1-git-send-email-fred.konrad@greensocs.com> References: <1454341418-24227-1-git-send-email-fred.konrad@greensocs.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 193.104.36.180 Cc: edgar.iglesias@xilinx.com, peter.maydell@linaro.org, hyunk@xilinx.com, Peter Crosthwaite , mark.burton@greensocs.com, alistair.francis@xilinx.com, crosthwaitepeter@gmail.com, guillaume.delbergue@greensocs.com, fred.konrad@greensocs.com Subject: [Qemu-devel] [PATCH V7 3/9] i2c: Factor our send() and recv() common logic X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 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-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org 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 Changes from FK: * Rebased on master. * Rebased on my i2c broadcast patch. Signed-off-by: KONRAD Frederic Reviewed-by: Alistair Francis --- hw/i2c/core.c | 48 ++++++++++++++++++++++++++++++++---------------- include/hw/i2c/i2c.h | 1 + 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/hw/i2c/core.c b/hw/i2c/core.c index a3921d9..a49138f 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -148,34 +148,50 @@ 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 = -1; - QLIST_FOREACH(node, &bus->current_devs, next) { - sc = I2C_SLAVE_GET_CLASS(node->elt); - if (sc->send) { - ret |= sc->send(node->elt, data); + if (send) { + QLIST_FOREACH(node, &bus->current_devs, next) { + sc = I2C_SLAVE_GET_CLASS(node->elt); + if (sc->send) { + ret |= sc->send(node->elt, *data); + } + } + return ret; + } 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; } -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);