Skip to contentSkip to conversation

iio: accel: bmc150: DMA-safe buffers and use iio_push_to_buffers_with_ts()

PATCH In review Yash Suthar 3 participants 13 messages Aug 25 – Sep 1 IIO

lore
Share & tools
lore
#0 lore raw

v3 cover iio: accel: bmc150: DMA-safe buffers and use iio_push_to_buffers_with_ts()

The trigger path used a plain s16 buffer while the FIFO path already had a properly typed scan member (__le16 + aligned timestamp). Those two buffers did the same job, and s16 does not represent little-endian data. Drop the duplicate, keep scan at the end of struct bmc150_accel_data with IIO_DMA_MINALIGN, and use it for both the trigger and FIFO paths.

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Move them into struct bmc150_accel_data after the aligned scan buffer. Because all buffers are serialized by data->mutex, placing them after scan ensures DMA safety without needing separate alignment.

Finally, replace the deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() so the push sites pass an explicit buffer size.

This supersedes the earlier standalone patches:

Link to v1: https://lore.kernel.org/all/[email protected]/
Link to v2: https://lore.kernel.org/linux-iio/[email protected]/

Changes in v3:
- patch 1: hold mutex for both the read and push to guarantee DMA safety
- patch 1: drop unused AXIS_MAX and use sizeof(data->scan.channels)
- patch 2: remove redundant __aligned(IIO_DMA_MINALIGN) from fifo_buff and regval, as the mutex serializes access
- patch 2: fix comma typo in commit message

Changes in v2:
- patch 1: restore early error check flow
- patch 2: keep fifo_buff declaration in one line

Yash Suthar (3):
iio: accel: bmc150: use aligned scan buffer for both trigger and fifo
iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads
iio: accel: bmc150: use iio_push_to_buffers_with_ts()

 drivers/iio/accel/bmc150-accel-core.c | 28 +++++++++++++--------------
 drivers/iio/accel/bmc150-accel.h      | 19 +++++++++---------
 2 files changed, 23 insertions(+), 24 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

In trigger handle, replaced AXIS_MAX with sizeof(data->scan.channels). Additionally, ensure mutex is held across both read and push to get DMA safety for the scan buffer.

Fixes bd7fe5b71918 ("iio: accel: BMC150 accel support")
---
 drivers/iio/accel/bmc150-accel-core.c |  8 ++++----
 drivers/iio/accel/bmc150-accel.h      | 14 +++++---------
 2 files changed, 9 insertions(+), 13 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +4 -4
index dc8a6285cf3d..1472e0d1ef65 100644
@@ -131,7 +131,6 @@ enum bmc150_accel_axis {
 	AXIS_X,
 	AXIS_Y,
 	AXIS_Z,
-	AXIS_MAX,
 };
 
 enum bmc150_power_modes {
@@ -1191,14 +1190,15 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 
 	mutex_lock(&data->mutex);
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
-			       data->buffer, AXIS_MAX * 2);
-	mutex_unlock(&data->mutex);
+			       data->scan.channels,
+			       sizeof(data->scan.channels));
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
 					   pf->timestamp);
 err_read:
+	mutex_unlock(&data->mutex);
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;
drivers/iio/accel/bmc150-accel.h +5 -9
index e8f26198359f..9deff256aed5 100644
@@ -64,15 +64,6 @@ struct bmc150_accel_data {
 	struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
 	struct mutex mutex;
 	u8 fifo_mode, watermark;
-	s16 buffer[8];
-	/*
-	 * Ensure there is sufficient space and correct alignment for
-	 * the timestamp if enabled
-	 */
-	struct {
-		__le16 channels[3];
-		aligned_s64 ts;
-	} scan;
 	u8 bw_bits;
 	u32 slope_dur;
 	u32 slope_thres;
@@ -85,6 +76,11 @@ struct bmc150_accel_data {
 	void (*resume_callback)(struct device *dev);
 	struct delayed_work resume_work;
 	struct iio_mount_matrix orientation;
+	/* Ensure correct alignment of timestamp and DMA safety */
+	struct {
+		__le16 channels[3];
+		aligned_s64 ts;
+	} scan __aligned(IIO_DMA_MINALIGN);
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
Signature · 1 line
2.43.0
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] iio: accel: bmc150: use aligned scan buffer for both trigger and fifo

Quoted 7 lines · Yash Suthar

Drop buffer as duplicate and s16 is not correct we needed __le16 as little endian, keep scan at the end of the bmc150_accel_data struct with IIO_DMA_MINALIGN, and use it for both paths.

In trigger handle, replaced AXIS_MAX with sizeof(data->scan.channels). Additionally, ensure mutex is held across both read and push to get DMA safety for the scan buffer.

...

drivers/iio/accel/bmc150-accel-core.c static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)

Quoted 7 lines
drivers/iio/accel/bmc150-accel-core.c +2 -2
 	mutex_lock(&data->mutex);
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L,
-			       data->buffer, AXIS_MAX * 2);
-	mutex_unlock(&data->mutex);
 
+			       data->scan.channels,
+			       sizeof(data->scan.channels));

I would put this on a single line (it is 81 characters only).

drivers/iio/accel/bmc150-accel-core.c static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) · line 1199

Quoted 6 lines
drivers/iio/accel/bmc150-accel-core.c +1 -1
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
+	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
 					   pf->timestamp);

I would put this on a single line (it is 82 characters only).

Quoted 3 lines

err_read:

drivers/iio/accel/bmc150-accel-core.c +1 -0
+	mutex_unlock(&data->mutex);
 	iio_trigger_notify_done(indio_dev->trig);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v3 2/3 iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

In reply to Yash Suthar

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Moved them into struct bmc150_accel_data after scan, each buffer are protected by mutex, placing them after ensure DMA safety.

---
 drivers/iio/accel/bmc150-accel-core.c | 14 ++++++--------
 drivers/iio/accel/bmc150-accel.h      |  5 +++++
 2 files changed, 11 insertions(+), 8 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +6 -8
index 1472e0d1ef65..4c98f840e626 100644
@@ -125,7 +125,6 @@
 #define BMC150_ACCEL_REG_FIFO_CONFIG0		0x30
 #define BMC150_ACCEL_REG_FIFO_CONFIG1		0x3E
 #define BMC150_ACCEL_REG_FIFO_DATA		0x3F
-#define BMC150_ACCEL_FIFO_LENGTH		32
 
 enum bmc150_accel_axis {
 	AXIS_X,
@@ -621,7 +620,6 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret;
 	int axis = chan->scan_index;
-	__le16 raw_val;
 
 	mutex_lock(&data->mutex);
 	ret = bmc150_accel_set_power_state(data, true);
@@ -631,14 +629,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	}
 
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
-			       &raw_val, sizeof(raw_val));
+			       &data->regval, sizeof(data->regval));
 	if (ret < 0) {
 		dev_err(dev, "Error reading axis %d\n", axis);
 		bmc150_accel_set_power_state(data, false);
 		mutex_unlock(&data->mutex);
 		return ret;
 	}
-	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
+	*val = sign_extend32(le16_to_cpu(data->regval) >> chan->scan_type.shift,
 			     chan->scan_type.realbits - 1);
 	ret = bmc150_accel_set_power_state(data, false);
 	mutex_unlock(&data->mutex);
@@ -917,7 +915,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
  * frame data is discarded.
  */
 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data,
-				      char *buffer, int samples)
+				      void *buffer, int samples)
 {
 	struct device *dev = regmap_get_device(data->regmap);
 	int sample_length = 3 * 2;
@@ -940,7 +938,6 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret, i;
 	u8 count;
-	u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
 	int64_t tstamp;
 	uint64_t sample_period;
 	unsigned int val;
@@ -992,7 +989,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 	count = min_t(u8, count, BMC150_ACCEL_FIFO_LENGTH);
 
-	ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count);
+	ret = bmc150_accel_fifo_transfer(data, data->fifo_buff, count);
 	if (ret)
 		return ret;
 
@@ -1007,7 +1004,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));
 
 		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
drivers/iio/accel/bmc150-accel.h +5 -0
index 9deff256aed5..40f9d25a3d4e 100644
@@ -56,6 +56,8 @@ enum bmc150_accel_trigger_id {
 	BMC150_ACCEL_TRIGGERS,
 };
 
+#define BMC150_ACCEL_FIFO_LENGTH		32
+
 struct bmc150_accel_data {
 	struct regmap *regmap;
 	int irq;
@@ -81,6 +83,9 @@ struct bmc150_accel_data {
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan __aligned(IIO_DMA_MINALIGN);
+	/* DMA-safe buffers for bulk/raw reads */
+	__le16 fifo_buff[BMC150_ACCEL_FIFO_LENGTH * 3];
+	__le16 regval;
 };
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v3 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

Quoted 4 lines · Yash Suthar

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Moved them into struct bmc150_accel_data after scan, each buffer are protected by mutex, placing them after ensure DMA safety.

...

drivers/iio/accel/bmc150-accel-core.c static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, · line 1009

Quoted 6 lines
drivers/iio/accel/bmc150-accel-core.c +2 -1
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));

While this works, I find a bit confusing usage of [j++] and [0] together.

IIUC the below is an equivalent

		iio_for_each_active_channel(indio_dev, bit) {
			__le16 *val = &data->scan.channels[j++];

			memcpy(val, &data->fifo_buff[i * 3 + bit], sizeof(val));
		}

and if go further we may use asm/byteorder.h macros instead of plain memcpy().

...

Maybe this is for a separate change, I dunno. I leave it to others to decide.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v3 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

Quoted 17 lines · On Thu, 27 Aug 2026 10:05:19 +0300 Andy Shevchenko…
Quoted 4 lines · Yash Suthar

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Moved them into struct bmc150_accel_data after scan, each buffer are protected by mutex, placing them after ensure DMA safety.

...

drivers/iio/accel/bmc150-accel-core.c static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, · line 1009

Quoted 6 lines
drivers/iio/accel/bmc150-accel-core.c +2 -1
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));  

While this works, I find a bit confusing usage of [j++] and [0] together.

Quoted 7 lines

IIUC the below is an equivalent

		iio_for_each_active_channel(indio_dev, bit) {
			__le16 *val = &data->scan.channels[j++];

			memcpy(val, &data->fifo_buff[i * 3 + bit], sizeof(val));
		}

This bit I fully agree with. Much more readable.

Quoted 3 lines

and if go further we may use asm/byteorder.h macros instead of plain memcpy().

Here I'm lost. We are just moving little endian values around.

For that matter if it is a single value, why can't see just assign it?

		iio_for_each_active_channel(indio_dev, bit)
			data->scan.channels[j++] = data->fifo_buf[i * 3 + bit];
Quoted 4 lines

...

Maybe this is for a separate change, I dunno. I leave it to others to decide.

#6 lore raw

Re: [PATCH v3 2/3] iio: accel: bmc150: use DMA-safe buffers for regmap bulk reads

Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Thu, 27 Aug 2026 10:05:19 +0300 Andy Shevchenko…

On Tue, Aug 25, 2026 at 02:21:55PM +0530, Yash Suthar wrote:

...

Quoted 19 lines
Quoted 9 lines

drivers/iio/accel/bmc150-accel-core.c static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, · line 1009

Quoted 6 lines
drivers/iio/accel/bmc150-accel-core.c +2 -1
 		j = 0;
 		iio_for_each_active_channel(indio_dev, bit)
-			memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit],
+			memcpy(&data->scan.channels[j++],
+			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));  

While this works, I find a bit confusing usage of [j++] and [0] together.

Quoted 7 lines

IIUC the below is an equivalent

		iio_for_each_active_channel(indio_dev, bit) {
			__le16 *val = &data->scan.channels[j++];

			memcpy(val, &data->fifo_buff[i * 3 + bit], sizeof(val));
		}

This bit I fully agree with. Much more readable.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

and if go further we may use asm/byteorder.h macros instead of plain memcpy().

Here I'm lost. We are just moving little endian values around.

For that matter if it is a single value, why can't see just assign it?

		iio_for_each_active_channel(indio_dev, bit)
			data->scan.channels[j++] = data->fifo_buf[i * 3 + bit];

It depends on the alignment. I haven't checked the assignee and the source for that. That's why I mentioned byteorder.h (and implying also unaligned.h) for the cases like put_unaligned_le16(le16_to_cpup(val)). If both sides are aligned, then yours suggestion will work.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v3 3/3 iio: accel: bmc150: use iio_push_to_buffers_with_ts()

In reply to Yash Suthar

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

---
 drivers/iio/accel/bmc150-accel-core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/accel/bmc150-accel-core.c +4 -4
index 4c98f840e626..e487e1b9239b 100644
@@ -1008,8 +1008,8 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
 			       &data->fifo_buff[i * 3 + bit],
 			       sizeof(data->scan.channels[0]));
 
-		iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
-						   tstamp);
+		iio_push_to_buffers_with_ts(indio_dev, &data->scan,
+					    sizeof(data->scan), tstamp);
 
 		tstamp += sample_period;
 	}
@@ -1193,8 +1193,8 @@ static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
 	if (ret < 0)
 		goto err_read;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
-					   pf->timestamp);
+	iio_push_to_buffers_with_ts(indio_dev, &data->scan,
+				    sizeof(data->scan), pf->timestamp);
 err_read:
 	mutex_unlock(&data->mutex);
 	iio_trigger_notify_done(indio_dev->trig);
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v3 3/3] iio: accel: bmc150: use iio_push_to_buffers_with_ts()

Quoted 2 lines · Yash Suthar

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

With this you might want to use IIO_DECLARE_DMA_BUFFER_WITH_TS() in the definition of the scan buffer.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#9 lore raw

Re: [PATCH v3 3/3] iio: accel: bmc150: use iio_push_to_buffers_with_ts()

On Thu, 27 Aug 2026 10:08:07 +0300
Andy Shevchenko <[email protected]> wrote:

Hi Andy,

Quoted 7 lines · Yash Suthar
Quoted 2 lines

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

With this you might want to use IIO_DECLARE_DMA_BUFFER_WITH_TS() in the definition of the scan buffer.

Why? The structure used is:

	struct {

__le16 channels[3];
aligned_s64 ts;
} scan __aligned(IIO_DMA_MINALIGN);

So given we never let things get enabled with just timestamp, the timestamp and channels always end up in the places the structure describes.

Where that is the case, I much prefer this to the magic buffer with enough elements of the channel type on the end.

Jonathan

#10 lore raw

Re: [PATCH v3 3/3] iio: accel: bmc150: use iio_push_to_buffers_with_ts()

Quoted 7 lines · Jonathan Cameron
Quoted 5 lines · On Thu, 27 Aug 2026 10:08:07 +0300 Andy Shevchenko…
Quoted 2 lines · Yash Suthar

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

With this you might want to use IIO_DECLARE_DMA_BUFFER_WITH_TS() in the

(For the record, I put it as "might".)

Quoted 10 lines
Quoted 2 lines

definition of the scan buffer.

Why? The structure used is:

	struct {
(quoted diff) +0 -0
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan __aligned(IIO_DMA_MINALIGN);

So given we never let things get enabled with just timestamp, the timestamp and channels always end up in the places the structure describes.

OK! I personally consider the macro a bit more robust in the cases when we _might_ have new HW to support where it will be the case.

Quoted 2 lines

Where that is the case, I much prefer this to the magic buffer with enough elements of the channel type on the end.

No objections.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#11 lore raw

Re: [PATCH v3 3/3] iio: accel: bmc150: use iio_push_to_buffers_with_ts()

Quoted 19 lines · On Mon, 31 Aug 2026 10:00:47 +0300 Andy Shevchenko…
Quoted 7 lines · Jonathan Cameron
Quoted 5 lines · On Thu, 27 Aug 2026 10:08:07 +0300 Andy Shevchenko…
Quoted 2 lines · Yash Suthar

Replace deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() to allow source size runtime checks.

With this you might want to use IIO_DECLARE_DMA_BUFFER_WITH_TS() in the

(For the record, I put it as "might".)

Quoted 8 lines
Quoted 2 lines

definition of the scan buffer.

Why? The structure used is:

	struct {
(quoted diff) +0 -0
 		__le16 channels[3];
 		aligned_s64 ts;
 	} scan __aligned(IIO_DMA_MINALIGN);
Quoted 5 lines
Quoted 2 lines

So given we never let things get enabled with just timestamp, the timestamp and channels always end up in the places the structure describes.

OK! I personally consider the macro a bit more robust in the cases when we _might_ have new HW to support where it will be the case.

Ah, I kind of get that but doesn't worry me too much.

They will need to modify the structure and then we'll spot it. It isn't a source of bugs if it does go wrong, just inelegant as the structure kind of implies the timestamp is in a different place from where ends up.

Maybe this is one for a maintainers profile that hopefully the AI bots will then use to monitor this for us ;)

Jonathan

Quoted 6 lines
Quoted 3 lines

Where that is the case, I much prefer this to the magic buffer with enough elements of the channel type on the end.

No objections.

#12 lore raw
In reply to Yash Suthar
Quoted 11 lines · Yash Suthar

The trigger path used a plain s16 buffer while the FIFO path already had a properly typed scan member (__le16 + aligned timestamp). Those two buffers did the same job, and s16 does not represent little-endian data. Drop the duplicate, keep scan at the end of struct bmc150_accel_data with IIO_DMA_MINALIGN, and use it for both the trigger and FIFO paths.

The FIFO and read_raw buffers are passed to regmap bulk/raw reads, which is not DMA-safe. Move them into struct bmc150_accel_data after the aligned scan buffer. Because all buffers are serialized by data->mutex, placing them after scan ensures DMA safety without needing separate alignment.

Quoted 7 lines

Finally, replace the deprecated iio_push_to_buffers_with_timestamp() with iio_push_to_buffers_with_ts() so the push sites pass an explicit buffer size.

This supersedes the earlier standalone patches:

In this case it's better not to use Link: tag. b4 can't distinguish them from the important ones.

Quoted 2 lines

Link to v1: https://lore.kernel.org/all/[email protected]/
Link to v2: https://lore.kernel.org/linux-iio/[email protected]/

Signature · 2 lines
With Best Regards,
Andy Shevchenko