From nobody Mon Feb 9 20:35:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552931034958305.68316390932785; Mon, 18 Mar 2019 10:43:54 -0700 (PDT) Received: from localhost ([127.0.0.1]:45159 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h5wIg-0005fj-Ub for importer@patchew.org; Mon, 18 Mar 2019 13:43:50 -0400 Received: from eggs.gnu.org ([209.51.188.92]:55446) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h5w4u-0003qS-0j for qemu-devel@nongnu.org; Mon, 18 Mar 2019 13:29:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h5vrR-0003YO-Tb for qemu-devel@nongnu.org; Mon, 18 Mar 2019 13:15:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50278) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h5vrR-0003Y0-HY for qemu-devel@nongnu.org; Mon, 18 Mar 2019 13:15:41 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BDDDE81DE1 for ; Mon, 18 Mar 2019 17:15:40 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-67.ams2.redhat.com [10.36.112.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F5CB5D70E; Mon, 18 Mar 2019 17:15:37 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 18 Mar 2019 18:15:21 +0100 Message-Id: <20190318171521.8524-14-pbonzini@redhat.com> In-Reply-To: <20190318171521.8524-1-pbonzini@redhat.com> References: <20190318171521.8524-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Mon, 18 Mar 2019 17:15:40 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 13/13] libqos: i2c: move address into QI2CDevice X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo BOnzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" 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 --- tests/ds1338-test.c | 4 ++-- tests/libqos/i2c.c | 51 +++++++++++++++++++++++++------------------- tests/libqos/i2c.h | 27 ++++++++++++----------- tests/pca9552-test.c | 31 ++++++++++++--------------- tests/tmp105-test.c | 44 +++++++++++++++++++------------------- 5 files changed, 82 insertions(+), 75 deletions(-) diff --git a/tests/ds1338-test.c b/tests/ds1338-test.c index fd8fbad533..f6ade9a050 100644 --- a/tests/ds1338-test.c +++ b/tests/ds1338-test.c @@ -31,13 +31,12 @@ static inline uint8_t bcd2bin(uint8_t x) static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc) { QI2CDevice *i2cdev =3D (QI2CDevice *)obj; - I2CAdapter *i2c =3D i2cdev->bus; =20 uint8_t resp[7]; time_t now =3D time(NULL); struct tm *tm_ptr =3D gmtime(&now); =20 - i2c_read_block(i2c, DS1338_ADDR, 0, resp, sizeof(resp)); + i2c_read_block(i2cdev, 0, resp, sizeof(resp)); =20 /* check retrieved time againt local time */ g_assert_cmpuint(bcd2bin(resp[4]), =3D=3D , tm_ptr->tm_mday); @@ -50,6 +49,7 @@ static void ds1338_register_nodes(void) QOSGraphEdgeOptions opts =3D { .extra_device_opts =3D "address=3D0x68" }; + add_qi2c_address(&opts, &(QI2CAddress) { DS1338_ADDR }); =20 qos_node_create_driver("ds1338", i2c_device_create); qos_node_consumes("ds1338", "i2c-bus", &opts); 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" =20 -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); } =20 -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); } =20 -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, ®, 1); - i2c_recv(i2c, addr, buf, len); + i2c_send(i2cdev, ®, 1); + i2c_recv(i2cdev, buf, len); } =20 -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 =3D g_malloc(len + 1); cmd[0] =3D reg; memcpy(&cmd[1], buf, len); - i2c_send(i2c, addr, cmd, len + 1); + i2c_send(i2cdev, cmd, len + 1); g_free(cmd); } =20 -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]; } =20 -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]; } =20 -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); } =20 -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]; =20 data[0] =3D value >> 8; data[1] =3D value & 255; - i2c_write_block(i2c, addr, reg, data, sizeof(data)); + i2c_write_block(i2cdev, reg, data, sizeof(data)); } =20 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 =3D g_new0(QI2CDevice, 1); =20 i2cdev->bus =3D i2c_bus; + if (addr) { + i2cdev->addr =3D ((QI2CAddress *)addr)->addr; + } return &i2cdev->obj; } + +void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr) +{ + g_assert(addr); + + opts->arg =3D addr; + opts->size_arg =3D sizeof(QI2CAddress); +} diff --git a/tests/libqos/i2c.h b/tests/libqos/i2c.h index 2c8cc3eab5..945b65b34c 100644 --- a/tests/libqos/i2c.h +++ b/tests/libqos/i2c.h @@ -22,6 +22,11 @@ struct I2CAdapter { QTestState *qts; }; =20 +typedef struct QI2CAddress QI2CAddress; +struct QI2CAddress { + uint8_t addr; +}; + typedef struct QI2CDevice QI2CDevice; struct QI2CDevice { /* @@ -36,25 +41,23 @@ struct QI2CDevice { */ QOSGraphObject obj; I2CAdapter *bus; + uint8_t addr; }; =20 void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr); +void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr); =20 -void i2c_send(I2CAdapter *i2c, uint8_t addr, - const uint8_t *buf, uint16_t len); -void i2c_recv(I2CAdapter *i2c, uint8_t addr, - uint8_t *buf, uint16_t len); +void i2c_send(QI2CDevice *dev, const uint8_t *buf, uint16_t len); +void i2c_recv(QI2CDevice *dev, uint8_t *buf, uint16_t len); =20 -void i2c_read_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg, +void i2c_read_block(QI2CDevice *dev, uint8_t reg, uint8_t *buf, uint16_t len); -void i2c_write_block(I2CAdapter *i2c, uint8_t addr, uint8_t reg, +void i2c_write_block(QI2CDevice *dev, uint8_t reg, const uint8_t *buf, uint16_t len); -uint8_t i2c_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg); -uint16_t i2c_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg); -void i2c_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg, - uint8_t value); -void i2c_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg, - uint16_t value); +uint8_t i2c_get8(QI2CDevice *dev, uint8_t reg); +uint16_t i2c_get16(QI2CDevice *dev, uint8_t reg); +void i2c_set8(QI2CDevice *dev, uint8_t reg, uint8_t value); +void i2c_set16(QI2CDevice *dev, uint8_t reg, uint16_t value); =20 /* i2c-omap.c */ typedef struct OMAPI2C { diff --git a/tests/pca9552-test.c b/tests/pca9552-test.c index 0598d0b1eb..4b800d3c3e 100644 --- a/tests/pca9552-test.c +++ b/tests/pca9552-test.c @@ -19,65 +19,61 @@ =20 static void pca9552_init(QI2CDevice *i2cdev) { - I2CAdapter *i2c =3D i2cdev->bus; - /* Switch on LEDs 0 and 12 */ - i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54); - i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54); + i2c_set8(i2cdev, PCA9552_LS0, 0x54); + i2c_set8(i2cdev, PCA9552_LS3, 0x54); } =20 static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc) { QI2CDevice *i2cdev =3D (QI2CDevice *)obj; - I2CAdapter *i2c =3D i2cdev->bus; uint8_t resp; uint8_t reg =3D PCA9552_LS0 | PCA9552_AUTOINC; =20 pca9552_init(i2cdev); =20 - i2c_send(i2c, PCA9552_TEST_ADDR, ®, 1); + i2c_send(i2cdev, ®, 1); =20 /* PCA9552_LS0 */ - i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1); + i2c_recv(i2cdev, &resp, 1); g_assert_cmphex(resp, =3D=3D, 0x54); =20 /* PCA9552_LS1 */ - i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1); + i2c_recv(i2cdev, &resp, 1); g_assert_cmphex(resp, =3D=3D, 0x55); =20 /* PCA9552_LS2 */ - i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1); + i2c_recv(i2cdev, &resp, 1); g_assert_cmphex(resp, =3D=3D, 0x55); =20 /* PCA9552_LS3 */ - i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1); + i2c_recv(i2cdev, &resp, 1); g_assert_cmphex(resp, =3D=3D, 0x54); } =20 static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc) { QI2CDevice *i2cdev =3D (QI2CDevice *)obj; - I2CAdapter *i2c =3D i2cdev->bus; uint8_t value; =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); + value =3D i2c_get8(i2cdev, PCA9552_LS0); g_assert_cmphex(value, =3D=3D, 0x55); =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); + value =3D i2c_get8(i2cdev, PCA9552_INPUT0); g_assert_cmphex(value, =3D=3D, 0x0); =20 pca9552_init(i2cdev); =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); + value =3D i2c_get8(i2cdev, PCA9552_LS0); g_assert_cmphex(value, =3D=3D, 0x54); =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); + value =3D i2c_get8(i2cdev, PCA9552_INPUT0); g_assert_cmphex(value, =3D=3D, 0x01); =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3); + value =3D i2c_get8(i2cdev, PCA9552_LS3); g_assert_cmphex(value, =3D=3D, 0x54); =20 - value =3D i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1); + value =3D i2c_get8(i2cdev, PCA9552_INPUT1); g_assert_cmphex(value, =3D=3D, 0x10); } =20 @@ -86,6 +82,7 @@ static void pca9552_register_nodes(void) QOSGraphEdgeOptions opts =3D { .extra_device_opts =3D "address=3D0x60" }; + add_qi2c_address(&opts, &(QI2CAddress) { 0x60 }); =20 qos_node_create_driver("pca9552", i2c_device_create); qos_node_consumes("pca9552", "i2c-bus", &opts); diff --git a/tests/tmp105-test.c b/tests/tmp105-test.c index 4031876387..f599309a4a 100644 --- a/tests/tmp105-test.c +++ b/tests/tmp105-test.c @@ -46,19 +46,18 @@ static void send_and_receive(void *obj, void *data, QGu= estAllocator *alloc) { uint16_t value; QI2CDevice *i2cdev =3D (QI2CDevice *)obj; - I2CAdapter *i2c =3D i2cdev->bus; =20 value =3D qmp_tmp105_get_temperature(TMP105_TEST_ID); g_assert_cmpuint(value, =3D=3D, 0); =20 - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0); =20 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000); value =3D qmp_tmp105_get_temperature(TMP105_TEST_ID); g_assert_cmpuint(value, =3D=3D, 20000); =20 - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x1400); =20 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */ @@ -67,27 +66,27 @@ static void send_and_receive(void *obj, void *data, QGu= estAllocator *alloc) g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2); =20 /* Set config */ - i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60); - value =3D i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG); + i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60); + value =3D i2c_get8(i2cdev, TMP105_REG_CONFIG); g_assert_cmphex(value, =3D=3D, 0x60); =20 - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x14f0); =20 /* Set precision to 9, 10, 11 bits. */ - i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00); - g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), = =3D=3D, 0x00); - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x00); + g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), =3D=3D, 0x00); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x1480); =20 - i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20); - g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), = =3D=3D, 0x20); - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x20); + g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), =3D=3D, 0x20); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x14c0); =20 - i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40); - g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), = =3D=3D, 0x40); - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x40); + g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), =3D=3D, 0x40); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x14e0); =20 /* stored precision remains the same */ @@ -95,15 +94,15 @@ static void send_and_receive(void *obj, void *data, QGu= estAllocator *alloc) g_assert_cmpuint(value, >=3D, 20938 - TMP105_PRECISION/2); g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2); =20 - i2c_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60); - g_assert_cmphex(i2c_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG), = =3D=3D, 0x60); - value =3D i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE); + i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60); + g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), =3D=3D, 0x60); + value =3D i2c_get16(i2cdev, TMP105_REG_TEMPERATURE); g_assert_cmphex(value, =3D=3D, 0x14f0); =20 - i2c_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234); - g_assert_cmphex(i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW), = =3D=3D, 0x1234); - i2c_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231); - g_assert_cmphex(i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH), = =3D=3D, 0x4231); + i2c_set16(i2cdev, TMP105_REG_T_LOW, 0x1234); + g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_LOW), =3D=3D, 0x1234); + i2c_set16(i2cdev, TMP105_REG_T_HIGH, 0x4231); + g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_HIGH), =3D=3D, 0x4231); } =20 static void tmp105_register_nodes(void) @@ -111,6 +110,7 @@ static void tmp105_register_nodes(void) QOSGraphEdgeOptions opts =3D { .extra_device_opts =3D "id=3D" TMP105_TEST_ID ",address=3D0x49" }; + add_qi2c_address(&opts, &(QI2CAddress) { 0x49 }); =20 qos_node_create_driver("tmp105", i2c_device_create); qos_node_consumes("tmp105", "i2c-bus", &opts); --=20 2.20.1