iio: pressure: bmp280: Use char instead of s32 for data buffer
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 | 43 +++++++++++++++++------------- drivers/iio/pressure/bmp280.h | 5 +++- 2 files changed, 28 insertions(+), 20 deletions(-)
drivers/iio/pressure/bmp280-core.c +24 -19
@@ -1035,7 +1035,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));
@@ -1045,10 +1046,10 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) } t_fine =bmp280_calc_t_fine (data, adc_temp); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp280_compensate_press (data, adc_press, t_fine); - -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:
@@ -1148,7 +1149,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));
@@ -1158,8 +1160,8 @@ 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); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 );/* Humidity calculations */ adc_humidity =get_unaligned_be16 (&data->buf[6 ]);
@@ -1168,9 +1170,11 @@ 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, + ret =bme280_compensate_humidity (data, adc_humidity, t_fine); +memcpy (&data->buffer.buf[8 ], &ret,4 ); + +iio_push_to_buffers_with_timestamp (indio_dev, &data->buffer,iio_get_time_ns (indio_dev)); out:
@@ -1628,7 +1632,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp380_compensate_temp (data, adc_temp); + ret =bmp380_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =get_unaligned_le24 (&data->buf[0 ]);
@@ -1638,10 +1643,10 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) } t_fine =bmp380_calc_t_fine (data, adc_temp); + ret =bmp380_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp380_compensate_press (data, adc_press, t_fine); - -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:
@@ -2203,12 +2208,12 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p) }/* Temperature calculations */ -memcpy (&data->sensor_data[1 ], &data->buf[0 ],3 ); +memcpy (&data->buffer.buf[4 ], &data->buf[0 ],3 );/* Pressure calculations */ -memcpy (&data->sensor_data[0 ], &data->buf[3 ],3 ); +memcpy (&data->buffer.buf[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:
@@ -2522,15 +2527,15 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)if (ret) goto out; - data->sensor_data[1 ] = chan_value; +memcpy (&data->buffer.buf[4 ], &chan_value,4 ); ret =bmp180_read_press (data, &chan_value);if (ret) goto out; - data->sensor_data[0 ] = chan_value; +memcpy (&data->buffer.buf[0 ], &chan_value,4 ); -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
@@ -411,7 +411,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 ]; +s64 ts__aligned(8) ; + } buffer;/* * DMA (thus cache coherency maintenance) may require the
Signature · 1 line
2.25.1
Quoted 6 lines · 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_databuffer to a char buffer with an extra value for the timestamp (as it is common practice).
Quoted 8 lines
[1]: https://lore.kernel.org/linux-iio/[email protected]/
Signed-off-by Vasileios Amoiridis <[email protected]>--- drivers/iio/pressure/bmp280-core.c | 43 +++++++++++++++++------------- drivers/iio/pressure/bmp280.h | 5 +++- 2 files changed, 28 insertions(+), 20 deletions(-)
Hi Jonathan,
I noticed that this patch didn't receive any comments nor applied. Was this intentional because we wait for something from mainline to be merged back?
Cheers,
Vasilis
Jonathan Cameron
maintainerQuoted 9 lines · On Fri, 23 Aug 2024 19:20:17 +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_databuffer 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]/
Signed-off-by Vasileios Amoiridis <[email protected]>
Hi.
Thanks for poking. Looks like I'd accidentally marked this one handled without actually doing so :(
Quoted 10 lines
--- drivers/iio/pressure/bmp280-core.c | 43 +++++++++++++++++------------- drivers/iio/pressure/bmp280.h | 5 +++- 2 files changed, 28 insertions(+), 20 deletions(-)drivers/iio/pressure/bmp280-core.c +0
-0
drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp280_trigger_handler(int irq, void *p) · line 1039
Quoted 6 lines
drivers/iio/pressure/bmp280-core.c +2
-1@@ -1035,7 +1035,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );
The 4's in here are magic indexes. Probably use sizeof(s32) in both cases. Maybe for the first one make the fact it's after an s32 explicit with &data->buffer.buf[1 * sizeof(s32)]
Of carry an offset through the code that you update when you fill in part of the buffer. That is probably cleaner.
int offset = 0; stuff offset += sizeof(s32); mempcy(&data->bufer.buf[offset], &ret, 4);
if offset is used for a later entry then add the new element.
Quoted 39 lines
drivers/iio/pressure/bmp280-core.c +7
-6/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));@@ -1045,10 +1046,10 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) } t_fine =bmp280_calc_t_fine (data, adc_temp); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp280_compensate_press (data, adc_press, t_fine); - -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:@@ -1148,7 +1149,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));@@ -1158,8 +1160,8 @@ 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); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 );/* Humidity calculations */ adc_humidity =get_unaligned_be16 (&data->buf[6 ]);
drivers/iio/pressure/bmp280-core.c static irqreturn_t bme280_trigger_handler(int irq, void *p) · line 1175
Quoted 8 lines
drivers/iio/pressure/bmp280-core.c +2
-2@@ -1168,9 +1170,11 @@ 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, + ret =bme280_compensate_humidity (data, adc_humidity, t_fine); +memcpy (&data->buffer.buf[8 ], &ret,4 );
as above, but here it is I guess after 2 other elements, so
2 * sizeof(s32)
Quoted 69 lines
drivers/iio/pressure/bmp280-core.c +13
-10+ +iio_push_to_buffers_with_timestamp (indio_dev, &data->buffer,iio_get_time_ns (indio_dev)); out:@@ -1628,7 +1632,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp380_compensate_temp (data, adc_temp); + ret =bmp380_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =get_unaligned_le24 (&data->buf[0 ]);@@ -1638,10 +1643,10 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) } t_fine =bmp380_calc_t_fine (data, adc_temp); + ret =bmp380_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp380_compensate_press (data, adc_press, t_fine); - -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:@@ -2203,12 +2208,12 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p) }/* Temperature calculations */ -memcpy (&data->sensor_data[1 ], &data->buf[0 ],3 ); +memcpy (&data->buffer.buf[4 ], &data->buf[0 ],3 );/* Pressure calculations */ -memcpy (&data->sensor_data[0 ], &data->buf[3 ],3 ); +memcpy (&data->buffer.buf[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:@@ -2522,15 +2527,15 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)if (ret) goto out; - data->sensor_data[1 ] = chan_value; +memcpy (&data->buffer.buf[4 ], &chan_value,4 ); ret =bmp180_read_press (data, &chan_value);if (ret) goto out; - data->sensor_data[0 ] = chan_value; +memcpy (&data->buffer.buf[0 ], &chan_value,4 ); -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
drivers/iio/pressure/bmp280.h struct bmp280_data { · line 416
Quoted 7 lines
drivers/iio/pressure/bmp280.h +3
-1@@ -411,7 +411,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 ]; +s64 ts__aligned(8) ;
We have a new aligned_s64 that is cleaner for these cases. It's only in my togreg branch currently though so you'll need to base on that to use it.
Jonathan
Quoted 4 lines
drivers/iio/pressure/bmp280.h +1
-0+ } buffer;/* * DMA (thus cache coherency maintenance) may require the
Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Fri, 23 Aug 2024 19:20:17 +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_databuffer to a char buffer with an extra value for the timestamp (as it is common practice).
Quoted 7 lines
Quoted 3 lines
[1]: https://lore.kernel.org/linux-iio/[email protected]/
Signed-off-by Vasileios Amoiridis <[email protected]>Hi.
Thanks for poking. Looks like I'd accidentally marked this one handled without actually doing so :(
Hi Jonathan,
No problem at all!!!
Quoted 24 lines
Quoted 10 lines
--- drivers/iio/pressure/bmp280-core.c | 43 +++++++++++++++++------------- drivers/iio/pressure/bmp280.h | 5 +++- 2 files changed, 28 insertions(+), 20 deletions(-)drivers/iio/pressure/bmp280-core.c +0
-0drivers/iio/pressure/bmp280-core.c static irqreturn_t bmp280_trigger_handler(int irq, void *p) · line 1039
Quoted 6 lines
drivers/iio/pressure/bmp280-core.c +2
-1@@ -1035,7 +1035,8 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );The 4's in here are magic indexes. Probably use sizeof(s32) in both cases. Maybe for the first one make the fact it's after an s32 explicit with &data->buffer.buf[1 * sizeof(s32)]
Of carry an offset through the code that you update when you fill in part of the buffer. That is probably cleaner.
Quoted 8 lines
int offset = 0; stuff offset += sizeof(s32); mempcy(&data->bufer.buf[offset], &ret, 4);if offset is used for a later entry then add the new element.
Looks nice. I could also move both memcpy() after the if's so in case there is an error I save myself from memcpy() once for no reason.
Quoted 132 lines
Quoted 39 lines
drivers/iio/pressure/bmp280-core.c +7
-6/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));@@ -1045,10 +1046,10 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p) } t_fine =bmp280_calc_t_fine (data, adc_temp); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp280_compensate_press (data, adc_press, t_fine); - -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:@@ -1148,7 +1149,8 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp280_compensate_temp (data, adc_temp); + ret =bmp280_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =FIELD_GET (BMP280_MEAS_TRIM_MASK ,get_unaligned_be24 (&data->buf[0 ]));@@ -1158,8 +1160,8 @@ 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); + ret =bmp280_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 );/* Humidity calculations */ adc_humidity =get_unaligned_be16 (&data->buf[6 ]);drivers/iio/pressure/bmp280-core.c static irqreturn_t bme280_trigger_handler(int irq, void *p) · line 1175
Quoted 8 lines
drivers/iio/pressure/bmp280-core.c +2
-2@@ -1168,9 +1170,11 @@ 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, + ret =bme280_compensate_humidity (data, adc_humidity, t_fine); +memcpy (&data->buffer.buf[8 ], &ret,4 );as above, but here it is I guess after 2 other elements, so
2 * sizeof(s32)
Quoted 69 lines
drivers/iio/pressure/bmp280-core.c +13
-10+ +iio_push_to_buffers_with_timestamp (indio_dev, &data->buffer,iio_get_time_ns (indio_dev)); out:@@ -1628,7 +1632,8 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) goto out; } - data->sensor_data[1 ] =bmp380_compensate_temp (data, adc_temp); + ret =bmp380_compensate_temp (data, adc_temp); +memcpy (&data->buffer.buf[4 ], &ret,4 );/* Pressure calculations */ adc_press =get_unaligned_le24 (&data->buf[0 ]);@@ -1638,10 +1643,10 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p) } t_fine =bmp380_calc_t_fine (data, adc_temp); + ret =bmp380_compensate_press (data, adc_press, t_fine); +memcpy (&data->buffer.buf[0 ], &ret,4 ); - data->sensor_data[0 ] =bmp380_compensate_press (data, adc_press, t_fine); - -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:@@ -2203,12 +2208,12 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p) }/* Temperature calculations */ -memcpy (&data->sensor_data[1 ], &data->buf[0 ],3 ); +memcpy (&data->buffer.buf[4 ], &data->buf[0 ],3 );/* Pressure calculations */ -memcpy (&data->sensor_data[0 ], &data->buf[3 ],3 ); +memcpy (&data->buffer.buf[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:@@ -2522,15 +2527,15 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)if (ret) goto out; - data->sensor_data[1 ] = chan_value; +memcpy (&data->buffer.buf[4 ], &chan_value,4 ); ret =bmp180_read_press (data, &chan_value);if (ret) goto out; - data->sensor_data[0 ] = chan_value; +memcpy (&data->buffer.buf[0 ], &chan_value,4 ); -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
-0drivers/iio/pressure/bmp280.h struct bmp280_data { · line 416
Quoted 7 lines
drivers/iio/pressure/bmp280.h +3
-1@@ -411,7 +411,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 ]; +s64 ts__aligned(8) ;We have a new
aligned_s64that is cleaner for these cases. It's only in my togreg branch currently though so you'll need to base on that to use it.
Quoted 3 lines
Jonathan
Looks nice, I will try it.
Cheers,
Vasilis
Quoted 5 lines
Quoted 4 lines
drivers/iio/pressure/bmp280.h +1
-0+ } buffer;/* * DMA (thus cache coherency maintenance) may require the