Skip to contentSkip to conversation

iio: maxim_thermocouple: Align 16 bit big endian value of raw reads

PATCH In review sayli karnik 2 participants 2 messages Sep 28, 2016 – Oct 1, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 iio: maxim_thermocouple: Align 16 bit big endian value of raw reads

Driver was reporting invalid raw read values for MAX6675 on big endian architectures. MAX6675 buffered mode is not affected, nor is the MAX31855.

The driver was losing a 2 byte read value when it used a 32 bit integer buffer to store a 16 bit big endian value. Use big endian types to properly align buffers on big endian architectures.

Fixes following sparse endianness warnings:
warning: cast to restricted __be16
warning: cast to restricted __be32
Fixes checkpatch issue:
CHECK: No space is necessary after a cast

Fixes 1f25ca11d84a ("iio: temperature: add support for Maxim

thermocouple chips")
---
Changes in v4:
Reverted back to v2 with changed names for buffers

Changes in v3:
Updated the commit message
Updated the changelog to include explanation to the bug fix Fixed the checkpatch issue: CHECK: No space is necessary after a cast Added the fixes tag

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -7
index 39dd202..066161a 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
 	unsigned int storage_bytes = data->chip->read_size;
 	unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-	unsigned int buf;
+	__be16 buf16;
+	__be32 buf32;
 	int ret;
 
-	ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-	if (ret)
-		return ret;
-
 	switch (storage_bytes) {
 	case 2:
-		*val = be16_to_cpu(buf);
+		ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
+		*val = be16_to_cpu(buf16);
 		break;
 	case 4:
-		*val = be32_to_cpu(buf);
+		ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
+		*val = be32_to_cpu(buf32);
 		break;
 	}
 
+	if (ret)
+		return ret;
+
 	/* check to be sure this is a valid reading */
 	if (*val & data->chip->status_bit)
 		return -EINVAL;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 14 lines · sayli karnik

Driver was reporting invalid raw read values for MAX6675 on big endian architectures. MAX6675 buffered mode is not affected, nor is the MAX31855.

The driver was losing a 2 byte read value when it used a 32 bit integer buffer to store a 16 bit big endian value. Use big endian types to properly align buffers on big endian architectures.

Fixes following sparse endianness warnings:
warning: cast to restricted __be16
warning: cast to restricted __be32
Fixes checkpatch issue:
CHECK: No space is necessary after a cast

Quoted 3 lines
Fixes 1f25ca11d84a ("iio: temperature: add support for Maxim

thermocouple chips")

Applied to the fixes-togreg-post-rc1 branch of iio.git where I gather patches like this one which are too late to make the merge window (which is probably opening tomorrow) and rely on patches that will merge during that window.

Things always get complex for a few weeks around this point in the cycle!

Thanks. Nice patch and good description,

Jonathan

Quoted 50 lines

---
Changes in v4:
Reverted back to v2 with changed names for buffers

Changes in v3:
Updated the commit message
Updated the changelog to include explanation to the bug fix Fixed the checkpatch issue: CHECK: No space is necessary after a cast Added the fixes tag

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -7
index 39dd202..066161a 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
 	unsigned int storage_bytes = data->chip->read_size;
 	unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-	unsigned int buf;
+	__be16 buf16;
+	__be32 buf32;
 	int ret;
 
-	ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-	if (ret)
-		return ret;
-
 	switch (storage_bytes) {
 	case 2:
-		*val = be16_to_cpu(buf);
+		ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
+		*val = be16_to_cpu(buf16);
 		break;
 	case 4:
-		*val = be32_to_cpu(buf);
+		ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
+		*val = be32_to_cpu(buf32);
 		break;
 	}
 
+	if (ret)
+		return ret;
+
 	/* check to be sure this is a valid reading */
 	if (*val & data->chip->status_bit)
 		return -EINVAL;