Skip to contentSkip to conversation

iio: adis_buffer: fix buffer ordering for IIO core demultiplexing

RFC PATCH In review Shehryar Ahmad 3 participants 5 messages Sep 25 – Oct 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 iio: adis_buffer: fix buffer ordering for IIO core demultiplexing

Hi Jonathan,

After issue raised by Sashiko in adis16203 graduation series [1], looking at other drivers if they might have same issue. It appears that the root cause is in adis_buffer.c itself. The library constructs the SPI transaction in channel array order while IIO core demultiplexes in scan_index order.

adis16209 and adis16260 have the same mismatched ordering that adis16201/16203 had.

Rather than reordering channel arrays in affected driver, would it be sensible to make adis_update_scan_mode() iterate in scan_index order instead of channel array order?

Would this be the right approach, or is there something I'm missing?

[1]: https://lore.kernel.org/all/20260914015321.7a660ab5@jic23-hlaptop/

---
 drivers/iio/imu/adis_buffer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/imu/adis_buffer.c +4 -4
index 501da4eae..45200b753 100644
@@ -110,10 +110,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
 	}
 
-	chan = indio_dev->channels;
-	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
-		if (!test_bit(chan->scan_index, scan_mask))
-			continue;
+	for_each_set_bit(i, scan_mask, iio_get_masklength(indio_dev)) {
+		chan = iio_find_channel_from_si(indio_dev, i);
+		if (!chan)
+			return -EINVAL;
 		if (chan->scan_type.storagebits == 32)
 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
 		*tx++ = cpu_to_be16(chan->address << 8);
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 8 lines · On Fri, 25 Sep 2026 16:02:39 +0500 Shehryar Ahmad <[email protected]>…

Hi Jonathan,

After issue raised by Sashiko in adis16203 graduation series [1], looking at other drivers if they might have same issue. It appears that the root cause is in adis_buffer.c itself. The library constructs the SPI transaction in channel array order while IIO core demultiplexes in scan_index order.

Quoted 8 lines

adis16209 and adis16260 have the same mismatched ordering that adis16201/16203 had.

Rather than reordering channel arrays in affected driver, would it be sensible to make adis_update_scan_mode() iterate in scan_index order instead of channel array order?

Would this be the right approach, or is there something I'm missing?

It is definitely plausible. For this I'd need some input from someone who can actually test some of these parts.

Nuno, you are listed as library maintainer - so can you get someone to look at this please. +CC various other folk from maintainers or who have worked in this area in last few years.

Otherwise, just a comment to use a Link tag for cross references.

Thanks,

Jonathan

Quoted 3 lines

[1]: https://lore.kernel.org/all/20260914015321.7a660ab5@jic23-hlaptop/

Quoted 24 lines
---
 drivers/iio/imu/adis_buffer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/imu/adis_buffer.c +4 -4
index 501da4eae..45200b753 100644
@@ -110,10 +110,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
 	}
 
-	chan = indio_dev->channels;
-	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
-		if (!test_bit(chan->scan_index, scan_mask))
-			continue;
+	for_each_set_bit(i, scan_mask, iio_get_masklength(indio_dev)) {
+		chan = iio_find_channel_from_si(indio_dev, i);
+		if (!chan)
+			return -EINVAL;
 		if (chan->scan_type.storagebits == 32)
 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
 		*tx++ = cpu_to_be16(chan->address << 8);
#2 lore raw

Hi,

A gentle reminder, it's been more than a week since Jonathan's review. Responses from library maintainers would be appreciated :)

Thanks

maintainer
#3 lore raw
In reply to Jonathan Cameron
Quoted 20 lines · Jonathan Cameron
Quoted 8 lines · On Fri, 25 Sep 2026 16:02:39 +0500 Shehryar Ahmad <[email protected]>…

Hi Jonathan,

After issue raised by Sashiko in adis16203 graduation series [1], looking at other drivers if they might have same issue. It appears that the root cause is in adis_buffer.c itself. The library constructs the SPI transaction in channel array order while IIO core demultiplexes in scan_index order.

Quoted 8 lines

adis16209 and adis16260 have the same mismatched ordering that adis16201/16203 had.

Rather than reordering channel arrays in affected driver, would it be sensible to make adis_update_scan_mode() iterate in scan_index order instead of channel array order?

Would this be the right approach, or is there something I'm missing?

Quoted 9 lines

It is definitely plausible. For this I'd need some input from someone who can actually test some of these parts.

Nuno, you are listed as library maintainer - so can you get someone to look at this please. +CC various other folk from maintainers or who have worked in this area in last few years.

Otherwise, just a comment to use a Link tag for cross references.

Hmm missed this one! So the issue looks very much real (just by code inspection). Just never saw it since the more recent devices I worked with just do burst mode which is not affected by that path. Moreover I'm not sure any of the affected drivers is really using the IIO demux (though I don't think that matters - seems to me the buffer will never respect scan_index order so not sure how it was ever right).

+Cc Radu as he has been working with these devices and on the library.

Radu, can you give a quick test here? Worst case scenario and you only have devices with burst mode, you can just tweak the driver to disable the burst path :)

Thanks!
- Nuno Sá

Quoted 34 lines

Thanks,

Jonathan

Quoted 3 lines

[1]: https://lore.kernel.org/all/20260914015321.7a660ab5@jic23-hlaptop/

Quoted 24 lines
---
 drivers/iio/imu/adis_buffer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/imu/adis_buffer.c +4 -4
index 501da4eae..45200b753 100644
@@ -110,10 +110,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
 	}
 
-	chan = indio_dev->channels;
-	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
-		if (!test_bit(chan->scan_index, scan_mask))
-			continue;
+	for_each_set_bit(i, scan_mask, iio_get_masklength(indio_dev)) {
+		chan = iio_find_channel_from_si(indio_dev, i);
+		if (!chan)
+			return -EINVAL;
 		if (chan->scan_type.storagebits == 32)
 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
 		*tx++ = cpu_to_be16(chan->address << 8);
maintainer
#4 lore raw
In reply to Shehryar Ahmad
Quoted 29 lines · Shehryar Ahmad

Hi Jonathan,

After issue raised by Sashiko in adis16203 graduation series [1], looking at other drivers if they might have same issue. It appears that the root cause is in adis_buffer.c itself. The library constructs the SPI transaction in channel array order while IIO core demultiplexes in scan_index order.

adis16209 and adis16260 have the same mismatched ordering that adis16201/16203 had.

Rather than reordering channel arrays in affected driver, would it be sensible to make adis_update_scan_mode() iterate in scan_index order instead of channel array order?

Would this be the right approach, or is there something I'm missing?

[1]: https://lore.kernel.org/all/20260914015321.7a660ab5@jic23-hlaptop/

---
 drivers/iio/imu/adis_buffer.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/imu/adis_buffer.c +0 -0
index 501da4eae..45200b753 100644

drivers/iio/imu/adis_buffer.c int adis_update_scan_mode(struct iio_dev *indio_dev, · line 113

Quoted 8 lines
drivers/iio/imu/adis_buffer.c +1 -4
@@ -110,10 +110,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		spi_message_add_tail(&adis->xfer[j], &adis->msg);
 	}
 
-	chan = indio_dev->channels;
-	for (i = 0; i < indio_dev->num_channels; i++, chan++) {
-		if (!test_bit(chan->scan_index, scan_mask))
-			continue;
+	for_each_set_bit(i, scan_mask, iio_get_masklength(indio_dev)) {

I guess it can be iio_for_each_active_channel()

- Nuno Sá

Quoted 9 lines
drivers/iio/imu/adis_buffer.c +3 -0
+		chan = iio_find_channel_from_si(indio_dev, i);
+		if (!chan)
+			return -EINVAL;
 		if (chan->scan_type.storagebits == 32)
 			*tx++ = cpu_to_be16((chan->address + 2) << 8);
 		*tx++ = cpu_to_be16(chan->address << 8);
Signature · 1 line
2.55.0