Skip to contentSkip to conversation

: pressure: bmp280: Improve pushing of data to buffer

PATCH Superseded Vasileios Amoiridis 2 participants 8 messages Sep 29, 2024 – Sep 30, 2024 IIO

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover : pressure: bmp280: Improve pushing of data to buffer

Changes in v2:
- Add 1st commit with a small fix of some data types. Shouldn't pose a problem.
- In commit no.2 add logic with "offset" to make it more clear what we are adding in the buffer. Also move the memcpy() operations after the if() statements so the memcpy happens only if the data are correct to avoid overhead for no reason.

P.S. After this patchseries is applied, I will rebase this [1] and resend it.

[1]: https://lore.kernel.org/linux-iio/[email protected]/

---
v1: https://lore.kernel.org/linux-iio/[email protected]/

Vasileios Amoiridis (2):
iio: pressure: bmp280: Fix type for raw values
iio: pressure: bmp280: Use char instead of s32 for data buffer

 drivers/iio/pressure/bmp280-core.c | 75 +++++++++++++++++++-----------
 drivers/iio/pressure/bmp280.h      |  5 +-
 2 files changed, 51 insertions(+), 29 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 iio: pressure: bmp280: Fix type for raw values

The adc values coming directly from the sensor in the BM{E,P}{2,3}xx sensors are unsigned values so treat them as such.

Fixes 80cd23f43ddc ("iio: pressure: bmp280: Add triggered buffer support")
---
 drivers/iio/pressure/bmp280-core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/iio/pressure/bmp280-core.c +6 -3
index 6c2606f34ec4..472a6696303b 100644
@@ -1023,7 +1023,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;
+	u32 adc_temp, adc_press;
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1137,7 +1138,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, adc_humidity, t_fine;
+	u32 adc_temp, adc_press, adc_humidity;
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1616,7 +1618,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;
+	u32 adc_temp, adc_press;
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH v2 1/2] iio: pressure: bmp280: Fix type for raw values

Quoted 4 lines · On Sun, 29 Sep 2024 13:25:10 +0200 Vasileios Amoiridis…

The adc values coming directly from the sensor in the BM{E,P}{2,3}xx sensors are unsigned values so treat them as such.

Fixes 80cd23f43ddc ("iio: pressure: bmp280: Add triggered buffer support")

Why is this a fix rather than a cleanup? Looks to me like all the values are going to be small enough that they fit either way.
So good to tidy up for consistency etc, but why a fixes tag?

I

Quoted 10 lines
---
 drivers/iio/pressure/bmp280-core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/iio/pressure/bmp280-core.c +0 -0
index 6c2606f34ec4..472a6696303b 100644

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp280_trigger_handler(int irq, void *p) · line 1026

Quoted 5 lines
drivers/iio/pressure/bmp280-core.c +1 -1
@@ -1023,7 +1023,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;
+	u32 adc_temp, adc_press;

These are filled as part of a get_unaligned_be24() so the value will never be big enough that signed / unsigned should make any difference.

drivers/iio/pressure/bmp280-core.c static irqreturn_t bme280_trigger_handler(int irq, void *p) · line 1141

Quoted 10 lines
drivers/iio/pressure/bmp280-core.c +2 -1
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1137,7 +1138,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, adc_humidity, t_fine;
+	u32 adc_temp, adc_press, adc_humidity;

Same with these.

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp380_trigger_handler(int irq, void *p) · line 1619

Quoted 9 lines
drivers/iio/pressure/bmp280-core.c +1 -1
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1616,7 +1618,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;

These are similar but le24.

Quoted 5 lines
drivers/iio/pressure/bmp280-core.c +2 -0
+	u32 adc_temp, adc_press;
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
#3 lore raw

Re: [PATCH v2 1/2] iio: pressure: bmp280: Fix type for raw values

Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Sun, 29 Sep 2024 13:25:10 +0200 Vasileios Amoiridis…

The adc values coming directly from the sensor in the BM{E,P}{2,3}xx sensors are unsigned values so treat them as such.

Quoted 7 lines
Quoted 1 line
Fixes 80cd23f43ddc ("iio: pressure: bmp280: Add triggered buffer support")

Why is this a fix rather than a cleanup? Looks to me like all the values are going to be small enough that they fit either way.
So good to tidy up for consistency etc, but why a fixes tag?

I

Hi Jonathan,

I used the fixes tag because I though it was appropriate since it was using a wrong variable type even though it was not posing any functional thread (I mentioned it in the cover-letter as well).

Since I am doing a new version I can drop the tag, no problem!!!

Cheers,
Vasilis

Quoted 46 lines
Quoted 10 lines
---
 drivers/iio/pressure/bmp280-core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/iio/pressure/bmp280-core.c +0 -0
index 6c2606f34ec4..472a6696303b 100644

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp280_trigger_handler(int irq, void *p) · line 1026

Quoted 5 lines
drivers/iio/pressure/bmp280-core.c +1 -1
@@ -1023,7 +1023,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;
+	u32 adc_temp, adc_press;

These are filled as part of a get_unaligned_be24() so the value will never be big enough that signed / unsigned should make any difference.

drivers/iio/pressure/bmp280-core.c static irqreturn_t bme280_trigger_handler(int irq, void *p) · line 1141

Quoted 10 lines
drivers/iio/pressure/bmp280-core.c +2 -1
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1137,7 +1138,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, adc_humidity, t_fine;
+	u32 adc_temp, adc_press, adc_humidity;

Same with these.

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp380_trigger_handler(int irq, void *p) · line 1619

Quoted 9 lines
drivers/iio/pressure/bmp280-core.c +1 -1
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
@@ -1616,7 +1618,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	s32 adc_temp, adc_press, t_fine;

These are similar but le24.

Quoted 5 lines
drivers/iio/pressure/bmp280-core.c +2 -0
+	u32 adc_temp, adc_press;
+	s32 t_fine;
 	int ret;
 
 	guard(mutex)(&data->lock);
#4 lore raw

PATCH v2 2/2 iio: pressure: bmp280: Use char instead of s32 for data buffer

In reply to Vasileios Amoiridis

As it was reported and discussed here [1], storing the sensor data in an endian aware s32 buffer is not optimal. Advertising the timestamp as an addition of 2 s32 variables which is also implied is again not the best practice. For that reason, change the s32 sensor_data buffer to a char buffer with an extra value for the timestamp (as it is common practice).

[1]: https://lore.kernel.org/linux-iio/[email protected]/

---
 drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------
 drivers/iio/pressure/bmp280.h      |  5 +-
 2 files changed, 51 insertions(+), 32 deletions(-)
drivers/iio/pressure/bmp280-core.c +47 -31
index 472a6696303b..2c62490a40c6 100644
@@ -1023,9 +1023,9 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	u32 adc_temp, adc_press;
-	s32 t_fine;
-	int ret;
+	u32 adc_temp, adc_press, comp_press;
+	s32 t_fine, comp_temp;
+	int ret, offset;
 
 	guard(mutex)(&data->lock);
 
@@ -1044,7 +1044,7 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 		goto out;
 	}
 
-	data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
+	comp_temp = bmp280_compensate_temp(data, adc_temp);
 
 	/* Pressure calculations */
 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1054,10 +1054,13 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
 	}
 
 	t_fine = bmp280_calc_t_fine(data, adc_temp);
+	comp_press = bmp280_compensate_press(data, adc_press, t_fine);
 
-	data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
@@ -1138,9 +1141,9 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	u32 adc_temp, adc_press, adc_humidity;
-	s32 t_fine;
-	int ret;
+	u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity;
+	s32 t_fine, comp_temp;
+	int ret, offset;
 
 	guard(mutex)(&data->lock);
 
@@ -1159,7 +1162,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 		goto out;
 	}
 
-	data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
+	comp_temp = bmp280_compensate_temp(data, adc_temp);
 
 	/* Pressure calculations */
 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1169,8 +1172,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 	}
 
 	t_fine = bmp280_calc_t_fine(data, adc_temp);
-
-	data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
+	comp_press = bmp280_compensate_press(data, adc_press, t_fine);
 
 	/* Humidity calculations */
 	adc_humidity = get_unaligned_be16(&data->buf[6]);
@@ -1179,9 +1181,16 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
 		dev_err(data->dev, "reading humidity skipped\n");
 		goto out;
 	}
-	data->sensor_data[2] = bme280_compensate_humidity(data, adc_humidity, t_fine);
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
+
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_humidity, sizeof(s32));
+
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
@@ -1618,9 +1627,9 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	u32 adc_temp, adc_press;
-	s32 t_fine;
-	int ret;
+	u32 adc_temp, adc_press, comp_press;
+	s32 t_fine, comp_temp;
+	int ret, offset;
 
 	guard(mutex)(&data->lock);
 
@@ -1639,7 +1648,7 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 		goto out;
 	}
 
-	data->sensor_data[1] = bmp380_compensate_temp(data, adc_temp);
+	comp_temp = bmp380_compensate_temp(data, adc_temp);
 
 	/* Pressure calculations */
 	adc_press = get_unaligned_le24(&data->buf[0]);
@@ -1649,10 +1658,13 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
 	}
 
 	t_fine = bmp380_calc_t_fine(data, adc_temp);
+	comp_press = bmp380_compensate_press(data, adc_press, t_fine);
 
-	data->sensor_data[0] = bmp380_compensate_press(data, adc_press, t_fine);
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
@@ -2199,7 +2211,7 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	int ret;
+	int ret, offset;
 
 	guard(mutex)(&data->lock);
 
@@ -2211,13 +2223,16 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
 		goto out;
 	}
 
+	/* Pressure calculations */
+	memcpy(&data->buffer.buf[offset], &data->buf[3], 3);
+
+	offset += sizeof(s32);
+
 	/* Temperature calculations */
-	memcpy(&data->sensor_data[1], &data->buf[0], 3);
+	memcpy(&data->buffer.buf[offset], &data->buf[0], 3);
 
-	/* Pressure calculations */
-	memcpy(&data->sensor_data[0], &data->buf[3], 3);
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
@@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	int ret, chan_value;
+	int ret, comp_temp, comp_press, offset;
 
 	guard(mutex)(&data->lock);
 
-	ret = bmp180_read_temp(data, &chan_value);
+	ret = bmp180_read_temp(data, &comp_temp);
 	if (ret)
 		goto out;
 
-	data->sensor_data[1] = chan_value;
 
-	ret = bmp180_read_press(data, &chan_value);
+	ret = bmp180_read_press(data, &comp_press);
 	if (ret)
 		goto out;
 
-	data->sensor_data[0] = chan_value;
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
drivers/iio/pressure/bmp280.h +4 -1
index a9f220c1f77a..b0c26f55c6af 100644
@@ -419,7 +419,10 @@ struct bmp280_data {
 	 * Data to push to userspace triggered buffer. Up to 3 channels and
 	 * s64 timestamp, aligned.
 	 */
-	s32 sensor_data[6] __aligned(8);
+	struct {
+		u8 buf[12];
+		aligned_s64 ts;
+	} buffer;
 
 	/*
 	 * DMA (thus cache coherency maintenance) may require the
Signature · 1 line
2.25.1
maintainer
#5 lore raw

Re: [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer

Quoted 9 lines · On Sun, 29 Sep 2024 13:25:11 +0200 Vasileios Amoiridis…

As it was reported and discussed here [1], storing the sensor data in an endian aware s32 buffer is not optimal. Advertising the timestamp as an addition of 2 s32 variables which is also implied is again not the best practice. For that reason, change the s32 sensor_data buffer to a char buffer with an extra value for the timestamp (as it is common practice).

[1]: https://lore.kernel.org/linux-iio/[email protected]/

Hi Vasileois.

I missed a purely semantic issue in previous versions :(

A few other places where you can achieve the same effect with less code and clear casting to correct types.

Jonathan

Quoted 26 lines
---
 drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------
 drivers/iio/pressure/bmp280.h      |  5 +-
 2 files changed, 51 insertions(+), 32 deletions(-)
drivers/iio/pressure/bmp280-core.c +2 -3
index 472a6696303b..2c62490a40c6 100644
@@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	int ret, chan_value;
+	int ret, comp_temp, comp_press, offset;
 
 	guard(mutex)(&data->lock);
 
-	ret = bmp180_read_temp(data, &chan_value);
+	ret = bmp180_read_temp(data, &comp_temp);
 	if (ret)
 		goto out;
 
-	data->sensor_data[1] = chan_value;

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp180_trigger_handler(int irq, void *p) · line 2556

Quoted 10 lines
drivers/iio/pressure/bmp280-core.c +4 -2
 
-	ret = bmp180_read_press(data, &chan_value);
+	ret = bmp180_read_press(data, &comp_press);
 	if (ret)
 		goto out;
 
-	data->sensor_data[0] = chan_value;
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));

I suppose there is a consistency argument for using memcpy even for the s32 cases but you 'could' if you like do

	s32 *chans = (s32 *)data->buffer.buf;
at top
and 
	chans[0] = comp_press;
	chans[1] = comp_temp;

here, which is functionally equivalent, particularly as we are forcing the buffer alignment to be larger than this s32.

Similar for the other simple native endian s32 cases.

The memcpy is needed for the le24 one.

Quoted 11 lines
drivers/iio/pressure/bmp280-core.c +1 -1
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
drivers/iio/pressure/bmp280.h +0 -0
index a9f220c1f77a..b0c26f55c6af 100644

drivers/iio/pressure/bmp280.h struct bmp280_data { · line 424

Quoted 7 lines
drivers/iio/pressure/bmp280.h +3 -1
@@ -419,7 +419,10 @@ struct bmp280_data {
 	 * Data to push to userspace triggered buffer. Up to 3 channels and
 	 * s64 timestamp, aligned.
 	 */
-	s32 sensor_data[6] __aligned(8);
+	struct {
+		u8 buf[12];
+		aligned_s64 ts;

I'd missed that this depends on the number of channels. It makes no functional difference because the core code will happily write over the end of buf, but from a representation point of view this might be

		u8 buf[8];
		aligned_s64 ts;
or
		u8 buf[12];
		aligned_s64 ts;

So given we can't actually fix on one or the other normal convention is to just use something that forces a large enough aligned u8 buffer like

		u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)]
			__aligned(sizeof(s64));

Sorry for leading you astray on this!

Jonathan

Quoted 4 lines
drivers/iio/pressure/bmp280.h +1 -0
+	} buffer;
 
 	/*
 	 * DMA (thus cache coherency maintenance) may require the
#6 lore raw

Re: [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer

Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Sun, 29 Sep 2024 13:25:11 +0200 Vasileios Amoiridis…

As it was reported and discussed here [1], storing the sensor data in an endian aware s32 buffer is not optimal. Advertising the timestamp as an addition of 2 s32 variables which is also implied is again not the best practice. For that reason, change the s32 sensor_data buffer to a char buffer with an extra value for the timestamp (as it is common practice).

[1]: https://lore.kernel.org/linux-iio/[email protected]/

Quoted 10 lines

Hi Vasileois.

I missed a purely semantic issue in previous versions :(

A few other places where you can achieve the same effect with less code and clear casting to correct types.

Jonathan

Hi Jonathan,

Quoted 43 lines
Quoted 26 lines
---
 drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------
 drivers/iio/pressure/bmp280.h      |  5 +-
 2 files changed, 51 insertions(+), 32 deletions(-)
drivers/iio/pressure/bmp280-core.c +2 -3
index 472a6696303b..2c62490a40c6 100644
@@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct bmp280_data *data = iio_priv(indio_dev);
-	int ret, chan_value;
+	int ret, comp_temp, comp_press, offset;
 
 	guard(mutex)(&data->lock);
 
-	ret = bmp180_read_temp(data, &chan_value);
+	ret = bmp180_read_temp(data, &comp_temp);
 	if (ret)
 		goto out;
 
-	data->sensor_data[1] = chan_value;

drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp180_trigger_handler(int irq, void *p) · line 2556

Quoted 10 lines
drivers/iio/pressure/bmp280-core.c +4 -2
 
-	ret = bmp180_read_press(data, &chan_value);
+	ret = bmp180_read_press(data, &comp_press);
 	if (ret)
 		goto out;
 
-	data->sensor_data[0] = chan_value;
+	memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+	offset += sizeof(s32);
+	memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));

I suppose there is a consistency argument for using memcpy even for the s32 cases but you 'could' if you like do

	s32 *chans = (s32 *)data->buffer.buf;
at top
and 
	chans[0] = comp_press;
	chans[1] = comp_temp;
Quoted 8 lines

here, which is functionally equivalent, particularly as we are forcing the buffer alignment to be larger than this s32.

Similar for the other simple native endian s32 cases.

The memcpy is needed for the le24 one.

I see what you mean, indeed I think your proposal will beautify it a lot!

Quoted 29 lines
Quoted 11 lines
drivers/iio/pressure/bmp280-core.c +1 -1
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
 					   iio_get_time_ns(indio_dev));
 
 out:
drivers/iio/pressure/bmp280.h +0 -0
index a9f220c1f77a..b0c26f55c6af 100644

drivers/iio/pressure/bmp280.h struct bmp280_data { · line 424

Quoted 7 lines
drivers/iio/pressure/bmp280.h +3 -1
@@ -419,7 +419,10 @@ struct bmp280_data {
 	 * Data to push to userspace triggered buffer. Up to 3 channels and
 	 * s64 timestamp, aligned.
 	 */
-	s32 sensor_data[6] __aligned(8);
+	struct {
+		u8 buf[12];
+		aligned_s64 ts;

I'd missed that this depends on the number of channels. It makes no functional difference because the core code will happily write over the end of buf, but from a representation point of view this might be

		u8 buf[8];
		aligned_s64 ts;
or
		u8 buf[12];
		aligned_s64 ts;
Quoted 10 lines

So given we can't actually fix on one or the other normal convention is to just use something that forces a large enough aligned u8 buffer like

		u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)]
			__aligned(sizeof(s64));

Sorry for leading you astray on this!

Jonathan

I see your point. I can fix it in next version!

This is a neat issue here that requires indeed extra attention since this type of buffers is used basically by the majority of the drivers. Some type of runtime check in those registers would have been very very helpful, but I can't see of an easy way of doing it in the core code..

Thanks for the review :)

Cheers,
Vasilis

Quoted 5 lines
Quoted 4 lines
drivers/iio/pressure/bmp280.h +1 -0
+	} buffer;
 
 	/*
 	 * DMA (thus cache coherency maintenance) may require the
maintainer
#7 lore raw

Re: [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer

Quoted 27 lines
Quoted 16 lines

drivers/iio/pressure/bmp280.h struct bmp280_data { · line 424

Quoted 5 lines
drivers/iio/pressure/bmp280.h +3 -1
-	s32 sensor_data[6] __aligned(8);
+	struct {
+		u8 buf[12];
+		aligned_s64 ts;  

I'd missed that this depends on the number of channels. It makes no functional difference because the core code will happily write over the end of buf, but from a representation point of view this might be

		u8 buf[8];
		aligned_s64 ts;
or
		u8 buf[12];
		aligned_s64 ts;
Quoted 10 lines

So given we can't actually fix on one or the other normal convention is to just use something that forces a large enough aligned u8 buffer like

		u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)]
			__aligned(sizeof(s64));

Sorry for leading you astray on this!

Jonathan

Quoted 7 lines

I see your point. I can fix it in next version!

This is a neat issue here that requires indeed extra attention since this type of buffers is used basically by the majority of the drivers. Some type of runtime check in those registers would have been very very helpful, but I can't see of an easy way of doing it in the core code..

Adding size description has been on my todo list for a while so as to allow the kernel to check the buffer is big enough and get rid of subtle overrun bugs due to that oddity of the buffer needing to include the timestamp space even though it's not obvious it will be written to.
That would also allow us to check alignment. What we can't do is finer grained checking of these structures but arguably we don't want to as this is an elegance not correctness issue!

Quoted 11 lines

Thanks for the review :)

Cheers,
Vasilis

Quoted 5 lines
Quoted 4 lines
drivers/iio/pressure/bmp280.h +1 -0
+	} buffer;
 
 	/*
 	 * DMA (thus cache coherency maintenance) may require the