mbox series

[v3,0/3] Use void pointers instead of char in I2C transfer APIs

Message ID 20191112203132.163306-1-dmitry.torokhov@gmail.com (mailing list archive)
Headers show
Series Use void pointers instead of char in I2C transfer APIs | expand

Message

Dmitry Torokhov Nov. 12, 2019, 8:31 p.m. UTC
While we indeed often deal with a stream of bytes when executing a
transfer, at the higher layers we usually work with more structured
data, and there is not really a reason to require casts to u8 * form the
callers. These series change I2C APIs to accept [const] void pointers,
and also adjust SMBUS implementation to use get/put_unaligned_16() and
memcpy() for moving data around.

Changes in v3:
- addressed Luca's comments
- added Jonathan's Acked-by
- split put_unaligned_le16 into a separate patch
- more call sites converted to get/put_unaligned_le16
- new patch using memcpy() for moving data around

Changes in v2:
- adjusted max1363 to the new i2c_master_send/recv signatures

Dmitry Torokhov (3):
  i2c: use void pointers for supplying data for reads and writes
  i2c: smbus: use get/put_unaligned_le16 when working with word data
  i2c: smbus: switch from loops to memcpy

 drivers/i2c/i2c-core-base.c  |  2 +-
 drivers/i2c/i2c-core-smbus.c | 40 +++++++++++++++---------------------
 drivers/iio/adc/max1363.c    | 14 +++++++------
 include/linux/i2c.h          | 28 +++++++++++++------------
 4 files changed, 41 insertions(+), 43 deletions(-)

Comments

Dmitry Torokhov Nov. 18, 2019, 8:09 a.m. UTC | #1
On Mon, Nov 18, 2019 at 08:47:57AM +0100, Uwe Kleine-König wrote:
> Hello Dmitry,
> 
> On Tue, Nov 12, 2019 at 12:31:32PM -0800, Dmitry Torokhov wrote:
> > When copying memory from one buffer to another, instead of open-coding
> > loops with byte-by-byte copies let's use memcpy() which might be a bit
> > faster and makes intent more clear.
> > 
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > 
> > ---
> > 
> > Changes in v3:
> > - new patch using memcpy() for moving data around
> > 
> >  drivers/i2c/i2c-core-smbus.c | 15 +++++----------
> >  1 file changed, 5 insertions(+), 10 deletions(-)
> > 
> > diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
> > index 7b4e2270eeda1..bbafdd3b1b114 100644
> > --- a/drivers/i2c/i2c-core-smbus.c
> > +++ b/drivers/i2c/i2c-core-smbus.c
> > @@ -397,8 +397,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
> >  			}
> >  
> >  			i2c_smbus_try_get_dmabuf(&msg[0], command);
> > -			for (i = 1; i < msg[0].len; i++)
> > -				msg[0].buf[i] = data->block[i - 1];
> > +			memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
> 
> Can it happen that msg[0].len is zero?

No, it can not, because of the "msg[0].len = data->block[0] + 2;" line
above.

Thanks.
Uwe Kleine-König Nov. 18, 2019, 8:47 a.m. UTC | #2
On Mon, Nov 18, 2019 at 12:09:39AM -0800, Dmitry Torokhov wrote:
> On Mon, Nov 18, 2019 at 08:47:57AM +0100, Uwe Kleine-König wrote:
> > Hello Dmitry,
> > 
> > On Tue, Nov 12, 2019 at 12:31:32PM -0800, Dmitry Torokhov wrote:
> > > When copying memory from one buffer to another, instead of open-coding
> > > loops with byte-by-byte copies let's use memcpy() which might be a bit
> > > faster and makes intent more clear.
> > > 
> > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > > 
> > > ---
> > > 
> > > Changes in v3:
> > > - new patch using memcpy() for moving data around
> > > 
> > >  drivers/i2c/i2c-core-smbus.c | 15 +++++----------
> > >  1 file changed, 5 insertions(+), 10 deletions(-)
> > > 
> > > diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c
> > > index 7b4e2270eeda1..bbafdd3b1b114 100644
> > > --- a/drivers/i2c/i2c-core-smbus.c
> > > +++ b/drivers/i2c/i2c-core-smbus.c
> > > @@ -397,8 +397,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
> > >  			}
> > >  
> > >  			i2c_smbus_try_get_dmabuf(&msg[0], command);
> > > -			for (i = 1; i < msg[0].len; i++)
> > > -				msg[0].buf[i] = data->block[i - 1];
> > > +			memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
> > 
> > Can it happen that msg[0].len is zero?
> 
> No, it can not, because of the "msg[0].len = data->block[0] + 2;" line
> above.

OK, and as passing data with data->block[0] = 254 also makes the code do
strange things already without your patch. I now also checked the other
conversions for similar problems and didn't find any. So:

Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Best regards
Uwe
Wolfram Sang Jan. 11, 2021, 9:01 p.m. UTC | #3
On Tue, Nov 12, 2019 at 12:31:29PM -0800, Dmitry Torokhov wrote:
> While we indeed often deal with a stream of bytes when executing a
> transfer, at the higher layers we usually work with more structured
> data, and there is not really a reason to require casts to u8 * form the
> callers. These series change I2C APIs to accept [const] void pointers,
> and also adjust SMBUS implementation to use get/put_unaligned_16() and
> memcpy() for moving data around.

I just started to work on something SMBus, so I rediscovered this series
and will comment now.