From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48489) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSvfs-0002kv-3R for qemu-devel@nongnu.org; Mon, 08 Feb 2016 18:56:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aSvfq-0004b7-SL for qemu-devel@nongnu.org; Mon, 08 Feb 2016 18:56:55 -0500 Received: from mail-oi0-x241.google.com ([2607:f8b0:4003:c06::241]:33642) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aSvfq-0004b3-Ln for qemu-devel@nongnu.org; Mon, 08 Feb 2016 18:56:54 -0500 Received: by mail-oi0-x241.google.com with SMTP id j125so245604oih.0 for ; Mon, 08 Feb 2016 15:56:54 -0800 (PST) MIME-Version: 1.0 Sender: alistair23@gmail.com In-Reply-To: <1454341418-24227-4-git-send-email-fred.konrad@greensocs.com> References: <1454341418-24227-1-git-send-email-fred.konrad@greensocs.com> <1454341418-24227-4-git-send-email-fred.konrad@greensocs.com> From: Alistair Francis Date: Mon, 8 Feb 2016 15:56:24 -0800 Message-ID: Content-Type: text/plain; charset=UTF-8 Subject: Re: [Qemu-devel] [PATCH V7 3/9] i2c: Factor our send() and recv() common logic List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?UTF-8?B?S09OUkFEIEZyw6lkw6lyaWM=?= Cc: Edgar Iglesias , Peter Maydell , hyunk@xilinx.com, Peter Crosthwaite , Mark Burton , "qemu-devel@nongnu.org Developers" , Alistair Francis , Peter Crosthwaite , Guillaume Delbergue On Mon, Feb 1, 2016 at 7:43 AM, wrote: > From: Peter Crosthwaite > > Most of the control flow logic between send and recv (error checking > etc) is the same. Factor this out into a common send_recv() API. > This is then usable by clients, where the control logic for send > and receive differs only by a boolean. E.g. > > if (send) > i2c_send(...): > else > i2c_recv(...); > > becomes: > > i2c_send_recv(... , send); > > Signed-off-by: Peter Crosthwaite > Changes from FK: > * Rebased on master. > * Rebased on my i2c broadcast patch. > Signed-off-by: KONRAD Frederic Reviewed-by: Alistair Francis Thanks, Alistair > --- > hw/i2c/core.c | 48 ++++++++++++++++++++++++++++++++---------------- > include/hw/i2c/i2c.h | 1 + > 2 files changed, 33 insertions(+), 16 deletions(-) > > diff --git a/hw/i2c/core.c b/hw/i2c/core.c > index a3921d9..a49138f 100644 > --- a/hw/i2c/core.c > +++ b/hw/i2c/core.c > @@ -148,34 +148,50 @@ void i2c_end_transfer(I2CBus *bus) > bus->broadcast = false; > } > > -int i2c_send(I2CBus *bus, uint8_t data) > +int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send) > { > I2CSlaveClass *sc; > I2CNode *node; > int ret = -1; > > - QLIST_FOREACH(node, &bus->current_devs, next) { > - sc = I2C_SLAVE_GET_CLASS(node->elt); > - if (sc->send) { > - ret |= sc->send(node->elt, data); > + if (send) { > + QLIST_FOREACH(node, &bus->current_devs, next) { > + sc = I2C_SLAVE_GET_CLASS(node->elt); > + if (sc->send) { > + ret |= sc->send(node->elt, *data); > + } > + } > + return ret; > + } else { > + if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) { > + return -1; > } > + > + sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt); > + if (sc->recv) { > + ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt); > + if (ret < 0) { > + return ret; > + } else { > + *data = ret; > + return 0; > + } > + } > + return -1; > } > - return ret; > } > > -int i2c_recv(I2CBus *bus) > +int i2c_send(I2CBus *bus, uint8_t data) > { > - I2CSlaveClass *sc; > + return i2c_send_recv(bus, &data, true); > +} > > - if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) { > - return -1; > - } > +int i2c_recv(I2CBus *bus) > +{ > + uint8_t data; > + int ret = i2c_send_recv(bus, &data, false); > > - sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt); > - if (sc->recv) { > - return sc->recv(QLIST_FIRST(&bus->current_devs)->elt); > - } > - return -1; > + return ret < 0 ? ret : data; > } > > void i2c_nack(I2CBus *bus) > diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h > index 4986ebc..c4085aa 100644 > --- a/include/hw/i2c/i2c.h > +++ b/include/hw/i2c/i2c.h > @@ -56,6 +56,7 @@ 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); > int i2c_send(I2CBus *bus, uint8_t data); > int i2c_recv(I2CBus *bus); > > -- > 1.9.0 > >