Linux-IIO Archive on lore.kernel.org
 help / color / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: "Ardelean, Alexandru" <alexandru.Ardelean@analog.com>
Cc: "linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
	"Jonathan.Cameron@huawei.com" <Jonathan.Cameron@huawei.com>,
	"martenli@axis.com" <martenli@axis.com>
Subject: Re: [PATCH] iio:adc:ti-adc084s021: Endian casting tidy ups.
Date: Sat, 9 Nov 2019 12:11:49 +0000
Message-ID: <20191109121149.4ffec6bb@archlinux> (raw)
In-Reply-To: <a07e927d00802115b17a5b01a3b0db245e776d7e.camel@analog.com>

On Mon, 4 Nov 2019 15:08:12 +0000
"Ardelean, Alexandru" <alexandru.Ardelean@analog.com> wrote:

> On Sun, 2019-10-13 at 10:47 +0100, jic23@kernel.org wrote:
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > 
> > Highlighted by sparse:
> > CHECK   drivers/iio/adc/ti-adc084s021.c
> > drivers/iio/adc/ti-adc084s021.c:79:26: warning: incorrect type in
> > assignment (different base types)
> > drivers/iio/adc/ti-adc084s021.c:79:26:    expected unsigned short
> > [unsigned] [short] [usertype] <noident>
> > drivers/iio/adc/ti-adc084s021.c:79:26:    got restricted __be16 <noident>
> > drivers/iio/adc/ti-adc084s021.c:110:24: warning: cast to restricted
> > __be16
> > drivers/iio/adc/ti-adc084s021.c:110:24: warning: cast to restricted
> > __be16
> > drivers/iio/adc/ti-adc084s021.c:110:24: warning: cast to restricted
> > __be16
> > drivers/iio/adc/ti-adc084s021.c:110:24: warning: cast to restricted
> > __be16
> >   
> 
Thanks for looking at this. I'd missed entirely that a void *
was hiding some more nastiness!

> This one looks a bit tricky.
> And looks like it could use a bit more cleanup than this.
> Otherwise sparse may come along and complain about more stuff.
> 
> One thing that would be good, would be to change:
> 
> int adc084s021_adc_conversion(struct adc084s021 *adc, void *data)
> 
> to
> 
> int adc084s021_adc_conversion(struct adc084s021 *adc, __be16 *data, int
> buf_size)   [1]
> 
> 
> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > Cc: Mårten Lindahl <martenli@axis.com>
> > ---
> >  drivers/iio/adc/ti-adc084s021.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/iio/adc/ti-adc084s021.c b/drivers/iio/adc/ti-
> > adc084s021.c
> > index bdedf456ee05..42966f2eb3d8 100644
> > --- a/drivers/iio/adc/ti-adc084s021.c
> > +++ b/drivers/iio/adc/ti-adc084s021.c
> > @@ -68,7 +68,7 @@ static int adc084s021_adc_conversion(struct adc084s021
> > *adc, void *data)
> >  {
> >  	int n_words = (adc->spi_trans.len >> 1) - 1; /* Discard first word
> > */
> >  	int ret, i = 0;
> > -	u16 *p = data;
> > +	__be16 *p = data;
> >  
> >  	/* Do the transfer */
> >  	ret = spi_sync(adc->spi, &adc->message);
> > @@ -87,6 +87,7 @@ static int adc084s021_read_raw(struct iio_dev
> > *indio_dev,
> >  {
> >  	struct adc084s021 *adc = iio_priv(indio_dev);
> >  	int ret;
> > +	__be16 value;
> >  
> >  	switch (mask) {
> >  	case IIO_CHAN_INFO_RAW:
> > @@ -101,13 +102,13 @@ static int adc084s021_read_raw(struct iio_dev
> > *indio_dev,
> >  		}
> >  
> >  		adc->tx_buf[0] = channel->channel << 3;
> > -		ret = adc084s021_adc_conversion(adc, val);
> > +		ret = adc084s021_adc_conversion(adc, &value);  
> 
> Following [1], this could be called with  "adc084s021_adc_conversion(adc,
> &value, 1)" to make sure it's not doing any stack corruption. I can't tell
> if this is doing any or not; the code is a bit fuzzy to me.

I'm fairly sure current code is safe as IIO_CHAN_INFO_RAW reads occur
with protection against buffered mode and when not in buffered mode the
magic length is 4 which is divided by 2 and has 1 subtracted giving a
safe value of 1.  This dance is ensure there is only one place where
the length is recorded and avoid recomputing it in *_buffer_trigger_handler.
We could stash it in the private data though for easier to read code.

Agreed that the type change to __be16 * definitely makes sense though.
Will respin with that.

> 
> The neat part is that memcpy() could be used to then access the data on
> rx_buf.

We could do that, but as it's not part of the fix really I'd rather leave
that for another day.  Not sure anything stops us doing this whilst doing
this tidy up even without passing in the size.

Thanks,

Jonathan

> 
> 
> >  		iio_device_release_direct_mode(indio_dev);
> >  		regulator_disable(adc->reg);
> >  		if (ret < 0)
> >  			return ret;
> >  
> > -		*val = be16_to_cpu(*val);
> > +		*val = be16_to_cpu(value);
> >  		*val = (*val >> channel->scan_type.shift) & 0xff;
> >  
> >  		return IIO_VAL_INT;  


  reply index

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-13  9:47 jic23
2019-11-04 15:08 ` Ardelean, Alexandru
2019-11-09 12:11   ` Jonathan Cameron [this message]
2019-11-11  9:22     ` Ardelean, Alexandru

Reply instructions:

You may reply publically 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=20191109121149.4ffec6bb@archlinux \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alexandru.Ardelean@analog.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=martenli@axis.com \
    /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

Linux-IIO Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-iio/0 linux-iio/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-iio linux-iio/ https://lore.kernel.org/linux-iio \
		linux-iio@vger.kernel.org
	public-inbox-index linux-iio

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-iio


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git