qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 23/30] i2c: Factor our send() and recv() common logic
Date: Tue, 14 Jun 2016 15:13:58 +0100	[thread overview]
Message-ID: <1465913645-19346-24-git-send-email-peter.maydell@linaro.org> (raw)
In-Reply-To: <1465913645-19346-1-git-send-email-peter.maydell@linaro.org>

From: Peter Crosthwaite <crosthwaitepeter@gmail.com>

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 <crosthwaite.peter@gmail.com>
Reviewed-by: Alistair Francis <alistair.francis@xilinx.com>
Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
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 <fred.konrad@greensocs.com>
Reviewed-by: Alistair Francis <alistair.francis@xilinx.com>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 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);
 
-- 
1.9.1

  parent reply	other threads:[~2016-06-14 14:14 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-14 14:13 [Qemu-devel] [PULL 00/30] target-arm queue Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 01/30] target-arm: kvm64: set guest PMUv3 feature bit if supported Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 02/30] hw/arm/virt: Add PMU node for virt machine Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 03/30] hw/arm/virt-acpi-build: Add PMU IRQ number in ACPI table Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 04/30] target-arm: Fix reset and migration of TTBCR(S) Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 05/30] hw/arm/virt: separate versioned type-init code Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 06/30] hw/arm/virt: introduce DEFINE_VIRT_MACHINE Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 07/30] hw/arm/virt: introduce DEFINE_VIRT_MACHINE_AS_LATEST Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 08/30] hw/arm/virt: create the 2.7 machine type Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 09/30] hw/i2c: QOM'ify bitbang_i2c.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 10/30] hw/i2c: QOM'ify exynos4210_i2c.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 11/30] hw/i2c: QOM'ify omap_i2c.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 12/30] hw/i2c: QOM'ify versatile_i2c.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 13/30] hw/gpio: QOM'ify omap_gpio.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 14/30] hw/gpio: QOM'ify pl061.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 15/30] hw/gpio: QOM'ify zaurus.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 16/30] hw/misc: QOM'ify arm_l2x0.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 17/30] hw/misc: QOM'ify exynos4210_pmu.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 18/30] hw/misc: QOM'ify mst_fpga.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 19/30] hw/dma: QOM'ify pxa2xx_dma.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 20/30] hw/sd: QOM'ify pl181.c Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 21/30] i2cbus: remove unused dev field Peter Maydell
2016-06-14 14:13 ` [Qemu-devel] [PULL 22/30] i2c: implement broadcast write Peter Maydell
2016-06-14 14:13 ` Peter Maydell [this message]
2016-06-14 14:13 ` [Qemu-devel] [PULL 24/30] introduce aux-bus Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 25/30] introduce dpcd module Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 26/30] hw/i2c-ddc.c: Implement DDC I2C slave Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 27/30] introduce xlnx-dpdma Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 28/30] introduce xlnx-dp Peter Maydell
2022-04-07 10:32   ` Peter Maydell
2022-04-07 11:28     ` Frederic Konrad
2022-04-07 12:26       ` Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 29/30] arm: xlnx-zynqmp: Add xlnx-dp and xlnx-dpdma Peter Maydell
2016-06-14 14:14 ` [Qemu-devel] [PULL 30/30] target-arm: Don't permit ARMv8-only Neon insns on ARMv7 Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1465913645-19346-24-git-send-email-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).