From 732c919cf04c0aaf1b092238e8b84cdb7adf657a Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 18 Mar 2019 17:12:25 +0100 Subject: [PATCH] libqos: split I2CAdapter initialization and allocation Provide *_init functions that populate an I2CAdapter struct without allocating one, and make the existing *_create functions wrap them. Because in the new setup *_create might return a pointer inside the IMXI2C or OMAPI2C struct, create companion *_free functions to go back to the outer pointer. All this is temporary until allocation will be handled entirely by qgraph. Signed-off-by: Paolo Bonzini --- tests/libqos/i2c-imx.c | 41 ++++++++++++++++++++++++----------------- tests/libqos/i2c-omap.c | 34 ++++++++++++++++++++++------------ tests/libqos/i2c.h | 22 +++++++++++++++++++--- tests/pca9552-test.c | 2 +- tests/tmp105-test.c | 2 +- 5 files changed, 67 insertions(+), 34 deletions(-) diff --git a/tests/libqos/i2c-imx.c b/tests/libqos/i2c-imx.c index 0945f2ecdc..28289c521a 100644 --- a/tests/libqos/i2c-imx.c +++ b/tests/libqos/i2c-imx.c @@ -30,13 +30,6 @@ enum IMXI2CDirection { IMX_I2C_WRITE, }; -typedef struct IMXI2C { - I2CAdapter parent; - - uint64_t addr; -} IMXI2C; - - static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr, enum IMXI2CDirection direction) { @@ -47,7 +40,7 @@ static void imx_i2c_set_slave_addr(IMXI2C *s, uint8_t addr, static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr, const uint8_t *buf, uint16_t len) { - IMXI2C *s = (IMXI2C *)i2c; + IMXI2C *s = container_of(i2c, IMXI2C, parent); uint8_t data; uint8_t status; uint16_t size = 0; @@ -107,7 +100,7 @@ static void imx_i2c_send(I2CAdapter *i2c, uint8_t addr, static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr, uint8_t *buf, uint16_t len) { - IMXI2C *s = (IMXI2C *)i2c; + IMXI2C *s = container_of(i2c, IMXI2C, parent); uint8_t data; uint8_t status; uint16_t size = 0; @@ -193,16 +186,30 @@ static void imx_i2c_recv(I2CAdapter *i2c, uint8_t addr, g_assert((status & I2SR_IBB) == 0); } +void imx_i2c_init(IMXI2C *s, QTestState *qts, uint64_t addr) +{ + s->addr = addr; + + s->parent.send = imx_i2c_send; + s->parent.recv = imx_i2c_recv; + s->parent.qts = qts; +} + I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr) { IMXI2C *s = g_malloc0(sizeof(*s)); - I2CAdapter *i2c = (I2CAdapter *)s; - s->addr = addr; - - i2c->send = imx_i2c_send; - i2c->recv = imx_i2c_recv; - i2c->qts = qts; - - return i2c; + imx_i2c_init(s, qts, addr); + return &s->parent; +} + +void imx_i2c_free(I2CAdapter *i2c) +{ + IMXI2C *s; + + if (!i2c) { + return; + } + s = container_of(i2c, IMXI2C, parent); + g_free(s); } diff --git a/tests/libqos/i2c-omap.c b/tests/libqos/i2c-omap.c index bb65336832..c7cbb9ecdd 100644 --- a/tests/libqos/i2c-omap.c +++ b/tests/libqos/i2c-omap.c @@ -40,12 +40,6 @@ enum OMAPI2CCONBits { OMAP_I2C_CON_I2C_EN = 1 << 15, }; -typedef struct OMAPI2C { - I2CAdapter parent; - - uint64_t addr; -} OMAPI2C; - static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr) { @@ -59,7 +53,7 @@ static void omap_i2c_set_slave_addr(OMAPI2C *s, uint8_t addr) static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr, const uint8_t *buf, uint16_t len) { - OMAPI2C *s = (OMAPI2C *)i2c; + OMAPI2C *s = container_of(i2c, OMAPI2C, parent); uint16_t data; omap_i2c_set_slave_addr(s, addr); @@ -103,7 +97,7 @@ static void omap_i2c_send(I2CAdapter *i2c, uint8_t addr, static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr, uint8_t *buf, uint16_t len) { - OMAPI2C *s = (OMAPI2C *)i2c; + OMAPI2C *s = container_of(i2c, OMAPI2C, parent); uint16_t data, stat; uint16_t orig_len = len; @@ -161,9 +155,8 @@ static void omap_i2c_recv(I2CAdapter *i2c, uint8_t addr, g_assert((data & OMAP_I2C_CON_STP) == 0); } -I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr) +void omap_i2c_init(OMAPI2C *s, QTestState *qts, uint64_t addr) { - OMAPI2C *s = g_malloc0(sizeof(*s)); I2CAdapter *i2c = (I2CAdapter *)s; uint16_t data; @@ -176,6 +169,23 @@ I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr) /* verify the mmio address by looking for a known signature */ data = qtest_readw(qts, addr + OMAP_I2C_REV); g_assert_cmphex(data, ==, 0x34); - - return i2c; +} + +I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr) +{ + OMAPI2C *s = g_malloc0(sizeof(*s)); + + omap_i2c_init(s, qts, addr); + return &s->parent; +} + +void omap_i2c_free(I2CAdapter *i2c) +{ + OMAPI2C *s; + + if (!i2c) { + return; + } + s = container_of(i2c, OMAPI2C, parent); + g_free(s); } diff --git a/tests/libqos/i2c.h b/tests/libqos/i2c.h index a462114597..877d2ab0e8 100644 --- a/tests/libqos/i2c.h +++ b/tests/libqos/i2c.h @@ -39,10 +39,26 @@ void i2c_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg, void i2c_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg, uint16_t value); -/* libi2c-omap.c */ -I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr); +/* i2c-omap.c */ +typedef struct OMAPI2C { + I2CAdapter parent; -/* libi2c-imx.c */ + uint64_t addr; +} OMAPI2C; + +void omap_i2c_init(OMAPI2C *s, QTestState *qts, uint64_t addr); +I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr); +void omap_i2c_free(I2CAdapter *i2c); + +/* i2c-imx.c */ +typedef struct IMXI2C { + I2CAdapter parent; + + uint64_t addr; +} IMXI2C; + +void imx_i2c_init(IMXI2C *s, QTestState *qts, uint64_t addr); I2CAdapter *imx_i2c_create(QTestState *qts, uint64_t addr); +void imx_i2c_free(I2CAdapter *i2c); #endif diff --git a/tests/pca9552-test.c b/tests/pca9552-test.c index 89b4445e29..f9509324e2 100644 --- a/tests/pca9552-test.c +++ b/tests/pca9552-test.c @@ -96,7 +96,7 @@ int main(int argc, char **argv) if (s) { qtest_quit(s); } - g_free(i2c); + omap_i2c_free(i2c); return ret; } diff --git a/tests/tmp105-test.c b/tests/tmp105-test.c index c86d2571e2..25ea05f5fd 100644 --- a/tests/tmp105-test.c +++ b/tests/tmp105-test.c @@ -122,7 +122,7 @@ int main(int argc, char **argv) ret = g_test_run(); qtest_quit(s); - g_free(i2c); + omap_i2c_free(i2c); return ret; }