From: fred.konrad@greensocs.com
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, crosthwaitepeter@gmail.com,
alistair.francis@xilinx.com, edgar.iglesias@xilinx.com,
hyunk@xilinx.com, guillaume.delbergue@greensocs.com,
mark.burton@greensocs.com, fred.konrad@greensocs.com,
Peter Crosthwaite <crosthwaite.peter@gmail.com>
Subject: [Qemu-devel] [PATCH V10 3/9] i2c: Factor our send() and recv() common logic
Date: Mon, 13 Jun 2016 17:50:08 +0200 [thread overview]
Message-ID: <1465833014-21982-4-git-send-email-fred.konrad@greensocs.com> (raw)
In-Reply-To: <1465833014-21982-1-git-send-email-fred.konrad@greensocs.com>
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>
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>
---
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.8.3.1
next prev parent reply other threads:[~2016-06-13 15:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-13 15:50 [Qemu-devel] [PATCH V10 0/9] Xilinx DisplayPort fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 1/9] i2cbus: remove unused dev field fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 2/9] i2c: implement broadcast write fred.konrad
2016-06-13 15:50 ` fred.konrad [this message]
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 4/9] introduce aux-bus fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 5/9] introduce dpcd module fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 6/9] hw/i2c-ddc.c: Implement DDC I2C slave fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 7/9] introduce xlnx-dpdma fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 8/9] introduce xlnx-dp fred.konrad
2016-06-13 15:50 ` [Qemu-devel] [PATCH V10 9/9] arm: xlnx-zynqmp: Add xlnx-dp and xlnx-dpdma fred.konrad
2016-06-14 13:59 ` [Qemu-devel] [PATCH V10 0/9] Xilinx DisplayPort Peter Maydell
2016-06-14 14:09 ` KONRAD Frederic
2016-06-14 14:58 ` Peter Maydell
2016-06-14 15:16 ` KONRAD Frederic
2016-06-14 15:20 ` 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=1465833014-21982-4-git-send-email-fred.konrad@greensocs.com \
--to=fred.konrad@greensocs.com \
--cc=alistair.francis@xilinx.com \
--cc=crosthwaite.peter@gmail.com \
--cc=crosthwaitepeter@gmail.com \
--cc=edgar.iglesias@xilinx.com \
--cc=guillaume.delbergue@greensocs.com \
--cc=hyunk@xilinx.com \
--cc=mark.burton@greensocs.com \
--cc=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).