From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 15/24] libqos: split I2CAdapter initialization and allocation
Date: Mon, 3 Jun 2019 19:10:34 +0200 [thread overview]
Message-ID: <1559581843-3968-16-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1559581843-3968-1-git-send-email-pbonzini@redhat.com>
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 <pbonzini@redhat.com>
---
tests/libqos/i2c-imx.c | 37 ++++++++++++++++++++++---------------
tests/libqos/i2c-omap.c | 32 +++++++++++++++++++++-----------
tests/libqos/i2c.h | 20 ++++++++++++++++++--
tests/pca9552-test.c | 2 +-
tests/tmp105-test.c | 2 +-
5 files changed, 63 insertions(+), 30 deletions(-)
diff --git a/tests/libqos/i2c-imx.c b/tests/libqos/i2c-imx.c
index 0945f2e..28289c5 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;
+ imx_i2c_init(s, qts, addr);
+ return &s->parent;
+}
- i2c->send = imx_i2c_send;
- i2c->recv = imx_i2c_recv;
- i2c->qts = qts;
+void imx_i2c_free(I2CAdapter *i2c)
+{
+ IMXI2C *s;
- return i2c;
+ 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 bb65336..c7cbb9e 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);
+}
+
+I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr)
+{
+ OMAPI2C *s = g_malloc0(sizeof(*s));
+
+ omap_i2c_init(s, qts, addr);
+ return &s->parent;
+}
- return i2c;
+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 a462114..877d2ab 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 */
+/* i2c-omap.c */
+typedef struct OMAPI2C {
+ I2CAdapter parent;
+
+ 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;
-/* libi2c-imx.c */
+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 89b4445..f950932 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 c86d257..25ea05f 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;
}
--
1.8.3.1
next prev parent reply other threads:[~2019-06-03 17:28 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-03 17:10 [Qemu-devel] [PULL 00/24] Misc patches for 2019-06-03 Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 01/24] test-thread-pool: be more reliable Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 02/24] vl: make -accel help to list enabled accelerators only Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 03/24] checkpatch: allow SPDX-License-Identifier Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 04/24] memory: Remove memory_region_get_dirty() Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 05/24] i386: Enable IA32_MISC_ENABLE MWAIT bit when exposing mwait/monitor Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 06/24] edu: mmio: allow 64-bit access Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 07/24] edu: mmio: allow 64-bit access in read dispatch Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 08/24] edu: uses uint64_t in dma operation Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 09/24] qgraph: allow extra_device_opts on contains nodes Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 10/24] qgraph: fix qos_node_contains with options Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 11/24] libqos: move common i2c code to libqos Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 12/24] libqos: fix omap-i2c receiving more than 4 bytes Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 13/24] pca9552-test: do not rely on state across tests Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 14/24] imx25-pdk: create ds1338 for qtest inside the test Paolo Bonzini
2019-06-03 17:10 ` Paolo Bonzini [this message]
2019-06-03 17:10 ` [Qemu-devel] [PULL 16/24] libqos: convert I2C to qgraph Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 17/24] libqos: add ARM n800 machine object Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 18/24] libqos: add ARM imx25-pdk " Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 19/24] tests: convert OMAP i2c tests to qgraph Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 20/24] tests: convert ds1338-test to qtest Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 21/24] libqos: i2c: move address into QI2CDevice Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 22/24] ci: store Patchew configuration in the tree Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 23/24] configure: remove tpm_passthrough & tpm_emulator Paolo Bonzini
2019-06-03 17:10 ` [Qemu-devel] [PULL 24/24] q35: Revert to kernel irqchip Paolo Bonzini
2019-06-05 9:14 ` Greg Kurz
2019-06-03 18:18 ` [Qemu-devel] [PULL 00/24] Misc patches for 2019-06-03 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=1559581843-3968-16-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--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).