Skip to contentSkip to conversation

iio:adc:ti-adc084s021: Endian casting tidy ups.

PATCH In review [email protected] 2 participants 4 messages Oct 13, 2019 – Nov 11, 2019 IIO

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/1 iio:adc:ti-adc084s021: Endian casting tidy ups.

From: Jonathan Cameron <[email protected]>

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

---
 drivers/iio/adc/ti-adc084s021.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/adc/ti-adc084s021.c +4 -3
index bdedf456ee05..42966f2eb3d8 100644
@@ -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);
 		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;
Signature · 1 line
2.23.0
#1 lore raw
Quoted 18 lines · On Sun, 2019-10-13 at 10:47 +0100, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

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

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]

Quoted 32 lines
---
 drivers/iio/adc/ti-adc084s021.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/adc/ti- renamed from drivers/iio/adc/ti-adc084s021.c +0 -0

adc084s021.c
index bdedf456ee05..42966f2eb3d8 100644

drivers/iio/adc/ti-adc084s021.c +1 -1
@@ -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;
 
drivers/iio/adc/ti-adc084s021.c +1 -0
 	/* 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) {
drivers/iio/adc/ti-adc084s021.c +0 -0
 	case IIO_CHAN_INFO_RAW:

drivers/iio/adc/ti-adc084s021.c static int adc084s021_read_raw(struct iio_dev · line 106

Quoted 6 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
@@ -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.

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

Quoted 10 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
 		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;

author maintainer
#2 lore raw
Quoted 20 lines · Alexandru
Quoted 18 lines · On Sun, 2019-10-13 at 10:47 +0100, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

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!

Quoted 49 lines

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]

Quoted 35 lines
---
 drivers/iio/adc/ti-adc084s021.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/adc/ti- renamed from drivers/iio/adc/ti-adc084s021.c +0 -0

adc084s021.c
index bdedf456ee05..42966f2eb3d8 100644

drivers/iio/adc/ti-adc084s021.c +1 -1
@@ -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;
 
drivers/iio/adc/ti-adc084s021.c +1 -0
 	/* 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) {
drivers/iio/adc/ti-adc084s021.c +0 -0
 	case IIO_CHAN_INFO_RAW:
@@ -101,13 +102,13 @@ static int adc084s021_read_raw(struct iio_dev
 *indio_dev,
 		}
 
Quoted 7 lines

drivers/iio/adc/ti-adc084s021.c static int adc084s021_read_raw(struct iio_dev *indio_dev, · line 105

Quoted 3 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
 		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.

Quoted 3 lines

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

Quoted 12 lines
Quoted 12 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
 		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;

#3 lore raw
Quoted 88 lines · Jonathan Cameron

[External]

Quoted 12 lines · Alexandru
Quoted 11 lines · On Sun, 2019-10-13 at 10:47 +0100, [email protected] wrote:

From: Jonathan Cameron <[email protected]>

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

Quoted 8 lines
Quoted 8 lines

__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!

Quoted 50 lines

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]

Quoted 36 lines
---
 drivers/iio/adc/ti-adc084s021.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/adc/ti- renamed from drivers/iio/adc/ti-adc084s021.c +0 -0

adc084s021.c
index bdedf456ee05..42966f2eb3d8 100644

drivers/iio/adc/ti-adc084s021.c +0 -0
@@ -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
*/

drivers/iio/adc/ti-adc084s021.c +2 -1
 	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) {
drivers/iio/adc/ti-adc084s021.c +0 -0
 	case IIO_CHAN_INFO_RAW:
@@ -101,13 +102,13 @@ static int adc084s021_read_raw(struct iio_dev
 *indio_dev,
 		}
 
Quoted 9 lines

drivers/iio/adc/ti-adc084s021.c static int adc084s021_read_raw(struct iio_dev *indio_dev, · line 105

Quoted 3 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
 		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.

Quoted 7 lines

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.

No idea what's best here.
I am mostly interested in getting the sparse checks up-n-running, so that we don't have another dance with Greg [as the ones with the ADIS patches].

Quoted 10 lines

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

Quoted 2 lines

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.

Let's leave things here as-is for now.
sparse checks [or any new checks we're adding] feel more important right now.

Thanks
Alex

Quoted 16 lines

Thanks,

Jonathan

Quoted 11 lines
Quoted 11 lines
drivers/iio/adc/ti-adc084s021.c +1 -1
 		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;