aboutsummaryrefslogtreecommitdiff
path: root/hw/i2c/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw/i2c/core.c')
-rw-r--r--hw/i2c/core.c52
1 files changed, 34 insertions, 18 deletions
diff --git a/hw/i2c/core.c b/hw/i2c/core.c
index 1fa8268c30..abb3efb1db 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)