qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Frederic Konrad <fred.konrad@greensocs.com>
To: Peter Crosthwaite <crosthwaitepeter@gmail.com>, qemu-devel@nongnu.org
Cc: Peter Crosthwaite <crosthwaite.peter@gmail.com>
Subject: Re: [Qemu-devel] [RFC v1 1/1] i2c: Factor our send() and recv() common logic
Date: Mon, 02 Nov 2015 10:09:54 +0100	[thread overview]
Message-ID: <56372862.9030004@greensocs.com> (raw)
In-Reply-To: <edf3bf0fc5ed2378652b278cb9fa0350adcc78b8.1445226296.git.crosthwaite.peter@gmail.com>

On 19/10/2015 06:09, Peter Crosthwaite wrote:
> 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 <crosthwaite.peter@gmail.com>
> ---
>
>  hw/i2c/core.c        | 34 +++++++++++++++++++---------------
>  include/hw/i2c/i2c.h |  1 +
>  2 files changed, 20 insertions(+), 15 deletions(-)
>
> diff --git a/hw/i2c/core.c b/hw/i2c/core.c
> index 5a64026..d4a8cbb 100644
> --- a/hw/i2c/core.c
> +++ b/hw/i2c/core.c
> @@ -129,7 +129,7 @@ void i2c_end_transfer(I2CBus *bus)
>      bus->current_dev = NULL;
>  }
>  
> -int i2c_send(I2CBus *bus, uint8_t data)
> +int i2c_send_recv(I2CBus  *bus, uint8_t *data, bool send)
Double space here between I2CBus and *bus.

Otherwise it looks ok for me.

Fred
>  {
>      I2CSlave *dev = bus->current_dev;
>      I2CSlaveClass *sc;
> @@ -139,28 +139,32 @@ int i2c_send(I2CBus *bus, uint8_t data)
>      }
>  
>      sc = I2C_SLAVE_GET_CLASS(dev);
> -    if (sc->send) {
> -        return sc->send(dev, data);
> +    if (send && sc->send) {
> +        return sc->send(dev, *data);
> +    } else if (!send && sc->recv) {
> +        int ret = sc->recv(dev);
> +        if (ret < 0) {
> +            return ret;
> +        } else {
> +            *data = ret;
> +            return 0;
> +        }
>      }
>  
>      return -1;
>  }
>  
> -int i2c_recv(I2CBus *bus)
> +int i2c_send(I2CBus *bus, uint8_t data)
>  {
> -    I2CSlave *dev = bus->current_dev;
> -    I2CSlaveClass *sc;
> -
> -    if (!dev) {
> -        return -1;
> -    }
> +    return i2c_send_recv(bus, &data, true);
> +}
>  
> -    sc = I2C_SLAVE_GET_CLASS(dev);
> -    if (sc->recv) {
> -        return sc->recv(dev);
> -    }
> +int i2c_recv(I2CBus *bus)
> +{
> +    uint8_t data;
> +    int ret = i2c_send_recv(bus, &data, false);
>  
> -    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);
>  

  reply	other threads:[~2015-11-02  9:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-19  4:09 [Qemu-devel] [RFC v1 1/1] i2c: Factor our send() and recv() common logic Peter Crosthwaite
2015-11-02  9:09 ` Frederic Konrad [this message]
2016-01-28 15:04   ` Frederic Konrad

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=56372862.9030004@greensocs.com \
    --to=fred.konrad@greensocs.com \
    --cc=crosthwaite.peter@gmail.com \
    --cc=crosthwaitepeter@gmail.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).