@@ -75,7 +75,16 @@ 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);
+/**
+ * i2c_recv_send: receive from or send to an I2C bus.
+ *
+ * @bus: #I2CBus to be used
+ * @data: buffer with the data transferred
+ * @is_recv: indicates the transfer direction
+ *
+ * Returns: 0 on success, -1 on error
+ */
+int i2c_recv_send(I2CBus *bus, uint8_t *data, bool is_recv);
int i2c_send(I2CBus *bus, uint8_t data);
uint8_t i2c_recv(I2CBus *bus);
@@ -1040,8 +1040,8 @@ static void sm501_i2c_write(void *opaque, hwaddr addr, uint64_t value,
SM501_DPRINTF("sm501 i2c : transferring %d bytes to 0x%x\n",
s->i2c_byte_count + 1, s->i2c_addr >> 1);
for (i = 0; i <= s->i2c_byte_count; i++) {
- res = i2c_send_recv(s->i2c_bus, &s->i2c_data[i],
- !(s->i2c_addr & 1));
+ res = i2c_recv_send(s->i2c_bus, &s->i2c_data[i],
+ s->i2c_addr & 1);
if (res) {
SM501_DPRINTF("sm501 i2c : transfer failed"
" i=%d, res=%d\n", i, res);
@@ -175,26 +175,14 @@ void i2c_end_transfer(I2CBus *bus)
bus->broadcast = false;
}
-int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
+int i2c_recv_send(I2CBus *bus, uint8_t *data, bool is_recv)
{
I2CSlaveClass *sc;
I2CSlave *s;
I2CNode *node;
int ret = 0;
- if (send) {
- QLIST_FOREACH(node, &bus->current_devs, next) {
- s = node->elt;
- sc = I2C_SLAVE_GET_CLASS(s);
- if (sc->send) {
- trace_i2c_send(s->address, *data);
- ret = ret || sc->send(s, *data);
- } else {
- ret = -1;
- }
- }
- return ret ? -1 : 0;
- } else {
+ if (is_recv) {
ret = 0xff;
if (!QLIST_EMPTY(&bus->current_devs) && !bus->broadcast) {
sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
@@ -206,19 +194,31 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
}
*data = ret;
return 0;
+ } else {
+ QLIST_FOREACH(node, &bus->current_devs, next) {
+ s = node->elt;
+ sc = I2C_SLAVE_GET_CLASS(s);
+ if (sc->send) {
+ trace_i2c_send(s->address, *data);
+ ret = ret || sc->send(s, *data);
+ } else {
+ ret = -1;
+ }
+ }
+ return ret ? -1 : 0;
}
}
int i2c_send(I2CBus *bus, uint8_t data)
{
- return i2c_send_recv(bus, &data, true);
+ return i2c_recv_send(bus, &data, false);
}
uint8_t i2c_recv(I2CBus *bus)
{
uint8_t data = 0xff;
- i2c_send_recv(bus, &data, false);
+ i2c_recv_send(bus, &data, true);
return data;
}
@@ -239,7 +239,7 @@ static void ppc4xx_i2c_writeb(void *opaque, hwaddr addr, uint64_t value,
}
}
if (!(i2c->sts & IIC_STS_ERR) &&
- i2c_send_recv(i2c->bus, &i2c->mdata[i], !recv)) {
+ i2c_recv_send(i2c->bus, &i2c->mdata[i], recv)) {
i2c->sts |= IIC_STS_ERR;
i2c->extsts |= IIC_EXTSTS_XFRA;
break;
@@ -148,7 +148,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint32_t address,
ret = AUX_I2C_ACK;
while (len > 0) {
- if (i2c_send_recv(i2c_bus, data++, is_write) < 0) {
+ if (i2c_recv_send(i2c_bus, data++, is_write) < 0) {
ret = AUX_I2C_NACK;
break;
}
@@ -189,7 +189,7 @@ AUXReply aux_request(AUXBus *bus, AUXCommand cmd, uint32_t address,
bus->last_transaction = cmd;
bus->last_i2c_address = address;
while (len > 0) {
- if (i2c_send_recv(i2c_bus, data++, is_write) < 0) {
+ if (i2c_recv_send(i2c_bus, data++, is_write) < 0) {
i2c_end_transfer(i2c_bus);
break;
}