aboutsummaryrefslogtreecommitdiff
path: root/tests/libqos/i2c.c
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2019-03-18 15:06:50 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2019-06-03 14:03:02 +0200
commit06599472ec40efef3de7cb2bc98d61e8d4b18a63 (patch)
tree284d2baa0ab63e29a7c0e70adca2d80602a7e16e /tests/libqos/i2c.c
parent8130dbcbcda15b3fe5ae1da76bf48868c4ce90fb (diff)
libqos: i2c: move address into QI2CDevice
This removes the hardcoded I2C address from the tests. The address is passed via QOSGraphEdgeOptions to i2c_device_create and stored in the QI2CDevice. The i2c_send and i2c_recv functions, along with their wrappers, therefore, can be changed to take a QI2CDevice rather than an adapter/address pair. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests/libqos/i2c.c')
-rw-r--r--tests/libqos/i2c.c51
1 files changed, 29 insertions, 22 deletions
diff --git a/tests/libqos/i2c.c b/tests/libqos/i2c.c
index 8117d13170..156114e745 100644
--- a/tests/libqos/i2c.c
+++ b/tests/libqos/i2c.c
@@ -10,63 +10,59 @@
#include "libqos/i2c.h"
#include "libqtest.h"
-void i2c_send(I2CAdapter *i2c, uint8_t addr,
- const uint8_t *buf, uint16_t len)
+void i2c_send(QI2CDevice *i2cdev, const uint8_t *buf, uint16_t len)
{
- i2c->send(i2c, addr, buf, len);
+ i2cdev->bus->send(i2cdev->bus, i2cdev->addr, buf, len);
}
-void i2c_recv(I2CAdapter *i2c, uint8_t addr,
- uint8_t *buf, uint16_t len)
+void i2c_recv(QI2CDevice *i2cdev, uint8_t *buf, uint16_t len)
{
- i2c->recv(i2c, addr, buf, len);
+ i2cdev->bus->recv(i2cdev->bus, i2cdev->addr, buf, len);
}
-void i2c_read_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
- uint8_t *buf, uint16_t len)
+void i2c_read_block(QI2CDevice *i2cdev, uint8_t reg,
+ uint8_t *buf, uint16_t len)
{
- i2c_send(i2c, addr, &reg, 1);
- i2c_recv(i2c, addr, buf, len);
+ i2c_send(i2cdev, &reg, 1);
+ i2c_recv(i2cdev, buf, len);
}
-void i2c_write_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
+void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,
const uint8_t *buf, uint16_t len)
{
uint8_t *cmd = g_malloc(len + 1);
cmd[0] = reg;
memcpy(&cmd[1], buf, len);
- i2c_send(i2c, addr, cmd, len + 1);
+ i2c_send(i2cdev, cmd, len + 1);
g_free(cmd);
}
-uint8_t i2c_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
+uint8_t i2c_get8(QI2CDevice *i2cdev, uint8_t reg)
{
uint8_t resp[1];
- i2c_read_block(i2c, addr, reg, resp, sizeof(resp));
+ i2c_read_block(i2cdev, reg, resp, sizeof(resp));
return resp[0];
}
-uint16_t i2c_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
+uint16_t i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
{
uint8_t resp[2];
- i2c_read_block(i2c, addr, reg, resp, sizeof(resp));
+ i2c_read_block(i2cdev, reg, resp, sizeof(resp));
return (resp[0] << 8) | resp[1];
}
-void i2c_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
- uint8_t value)
+void i2c_set8(QI2CDevice *i2cdev, uint8_t reg, uint8_t value)
{
- i2c_write_block(i2c, addr, reg, &value, 1);
+ i2c_write_block(i2cdev, reg, &value, 1);
}
-void i2c_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
- uint16_t value)
+void i2c_set16(QI2CDevice *i2cdev, uint8_t reg, uint16_t value)
{
uint8_t data[2];
data[0] = value >> 8;
data[1] = value & 255;
- i2c_write_block(i2c, addr, reg, data, sizeof(data));
+ i2c_write_block(i2cdev, reg, data, sizeof(data));
}
void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr)
@@ -74,5 +70,16 @@ void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr)
QI2CDevice *i2cdev = g_new0(QI2CDevice, 1);
i2cdev->bus = i2c_bus;
+ if (addr) {
+ i2cdev->addr = ((QI2CAddress *)addr)->addr;
+ }
return &i2cdev->obj;
}
+
+void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr)
+{
+ g_assert(addr);
+
+ opts->arg = addr;
+ opts->size_arg = sizeof(QI2CAddress);
+}