Skip to contentSkip to conversation

iio: adc: pac1921: fix wrong channel used in trigger handler read

PATCH Applied Cong Nguyen 4 participants 5 messages Aug 2 – Aug 4

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

PATCH 1/1 iio: adc: pac1921: fix wrong channel used in trigger handler read

pac1921_trigger_handler() walks the enabled channels with iio_for_each_active_channel(), which yields the scan index (bit) of each active channel, while ch is a separate counter used to pack the samples contiguously into the scan buffer.

The register to read was looked up with the packing counter instead of the scan index:

	ret = pac1921_read_res(priv, idev->channels[ch].address, &val);

pac1921_channels[] is ordered by scan index, so channels[bit] is the channel that is actually enabled, whereas channels[ch] is merely the ch-th array entry. These coincide only when the enabled channels form a contiguous prefix (e.g. all channels enabled). With a sparse scan mask - for example when only the power channel (scan index 3) is enabled - the handler reads the wrong register (VBUS instead of VPOWER) and pushes it to userspace as the enabled channel's data.

Index the channel array by the scan index (bit) to read the correct register, keeping ch only for contiguous packing into the scan buffer.

Fixes 371f778b83cd ("iio: adc: add support for pac1921")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/pac1921.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/pac1921.c +1 -1
index bce7185953ec..0037509503ed 100644
@@ -1037,7 +1037,7 @@ static irqreturn_t pac1921_trigger_handler(int irq, void *p)
 	iio_for_each_active_channel(idev, bit) {
 		u16 val;
 
-		ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
+		ret = pac1921_read_res(priv, idev->channels[bit].address, &val);
 		if (ret)
 			goto done;
 
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 34 lines · Cong Nguyen

pac1921_trigger_handler() walks the enabled channels with iio_for_each_active_channel(), which yields the scan index (bit) of each active channel, while ch is a separate counter used to pack the samples contiguously into the scan buffer.

The register to read was looked up with the packing counter instead of the scan index:

	ret = pac1921_read_res(priv, idev->channels[ch].address, &val);

pac1921_channels[] is ordered by scan index, so channels[bit] is the channel that is actually enabled, whereas channels[ch] is merely the ch-th array entry. These coincide only when the enabled channels form a contiguous prefix (e.g. all channels enabled). With a sparse scan mask - for example when only the power channel (scan index 3) is enabled - the handler reads the wrong register (VBUS instead of VPOWER) and pushes it to userspace as the enabled channel's data.

Index the channel array by the scan index (bit) to read the correct register, keeping ch only for contiguous packing into the scan buffer.

Fixes 371f778b83cd ("iio: adc: add support for pac1921")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/pac1921.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/pac1921.c +0 -0
index bce7185953ec..0037509503ed 100644

drivers/iio/adc/pac1921.c static irqreturn_t pac1921_trigger_handler(int irq, void *p) · line 1043

Quoted 8 lines
drivers/iio/adc/pac1921.c +1 -1
@@ -1037,7 +1037,7 @@ static irqreturn_t pac1921_trigger_handler(int irq, void *p)
 	iio_for_each_active_channel(idev, bit) {
 		u16 val;
 
-		ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
+		ret = pac1921_read_res(priv, idev->channels[bit].address, &val);
 		if (ret)
 			goto done;
 

Looks correct.

maintainer
#2 lore raw
Quoted 46 lines · On Sun, 2 Aug 2026 10:48:26 -0500 David Lechner <[email protected]> wrote:
Quoted 34 lines · Cong Nguyen

pac1921_trigger_handler() walks the enabled channels with iio_for_each_active_channel(), which yields the scan index (bit) of each active channel, while ch is a separate counter used to pack the samples contiguously into the scan buffer.

The register to read was looked up with the packing counter instead of the scan index:

	ret = pac1921_read_res(priv, idev->channels[ch].address, &val);

pac1921_channels[] is ordered by scan index, so channels[bit] is the channel that is actually enabled, whereas channels[ch] is merely the ch-th array entry. These coincide only when the enabled channels form a contiguous prefix (e.g. all channels enabled). With a sparse scan mask - for example when only the power channel (scan index 3) is enabled - the handler reads the wrong register (VBUS instead of VPOWER) and pushes it to userspace as the enabled channel's data.

Index the channel array by the scan index (bit) to read the correct register, keeping ch only for contiguous packing into the scan buffer.

Fixes 371f778b83cd ("iio: adc: add support for pac1921")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/pac1921.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/pac1921.c +0 -0
index bce7185953ec..0037509503ed 100644

drivers/iio/adc/pac1921.c static irqreturn_t pac1921_trigger_handler(int irq, void *p) · line 1043

Quoted 8 lines
drivers/iio/adc/pac1921.c +1 -1
@@ -1037,7 +1037,7 @@ static irqreturn_t pac1921_trigger_handler(int irq, void *p)
 	iio_for_each_active_channel(idev, bit) {
 		u16 val;
 
-		ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
+		ret = pac1921_read_res(priv, idev->channels[bit].address, &val);
 		if (ret)
 			goto done;
 

Looks correct.

Quoted 1 line

+CC Ariana who is dealing with similar parts and might be able to sanity check this.

FWIW looks correct to me too.

Jonathan

Quoted 1 line
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Cong Nguyen
Quoted 45 lines · Cong Nguyen

pac1921_trigger_handler() walks the enabled channels with iio_for_each_active_channel(), which yields the scan index (bit) of each active channel, while ch is a separate counter used to pack the samples contiguously into the scan buffer.

The register to read was looked up with the packing counter instead of the scan index:

	ret = pac1921_read_res(priv, idev->channels[ch].address, &val);

pac1921_channels[] is ordered by scan index, so channels[bit] is the channel that is actually enabled, whereas channels[ch] is merely the ch-th array entry. These coincide only when the enabled channels form a contiguous prefix (e.g. all channels enabled). With a sparse scan mask - for example when only the power channel (scan index 3) is enabled - the handler reads the wrong register (VBUS instead of VPOWER) and pushes it to userspace as the enabled channel's data.

Index the channel array by the scan index (bit) to read the correct register, keeping ch only for contiguous packing into the scan buffer.

Fixes 371f778b83cd ("iio: adc: add support for pac1921")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/pac1921.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/pac1921.c +1 -1
index bce7185953ec..0037509503ed 100644
@@ -1037,7 +1037,7 @@ static irqreturn_t pac1921_trigger_handler(int irq, void *p)
 	iio_for_each_active_channel(idev, bit) {
 		u16 val;
 
-		ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
+		ret = pac1921_read_res(priv, idev->channels[bit].address, &val);
 		if (ret)
 			goto done;
 
Signature · 1 line
2.25.1

This looks correct to me. I guess I didn't test it properly with a subset of enabled channels when I wrote this.
Thanks for the fix!

maintainer Applied
#4 lore raw
Quoted 51 lines · On Tue, 04 Aug 2026 08:49:25 +0200 Matteo Martelli <[email protected]>…
Quoted 45 lines · Cong Nguyen

pac1921_trigger_handler() walks the enabled channels with iio_for_each_active_channel(), which yields the scan index (bit) of each active channel, while ch is a separate counter used to pack the samples contiguously into the scan buffer.

The register to read was looked up with the packing counter instead of the scan index:

	ret = pac1921_read_res(priv, idev->channels[ch].address, &val);

pac1921_channels[] is ordered by scan index, so channels[bit] is the channel that is actually enabled, whereas channels[ch] is merely the ch-th array entry. These coincide only when the enabled channels form a contiguous prefix (e.g. all channels enabled). With a sparse scan mask - for example when only the power channel (scan index 3) is enabled - the handler reads the wrong register (VBUS instead of VPOWER) and pushes it to userspace as the enabled channel's data.

Index the channel array by the scan index (bit) to read the correct register, keeping ch only for contiguous packing into the scan buffer.

Fixes 371f778b83cd ("iio: adc: add support for pac1921")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/pac1921.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/pac1921.c +1 -1
index bce7185953ec..0037509503ed 100644
@@ -1037,7 +1037,7 @@ static irqreturn_t pac1921_trigger_handler(int irq, void *p)
 	iio_for_each_active_channel(idev, bit) {
 		u16 val;
 
-		ret = pac1921_read_res(priv, idev->channels[ch].address, &val);
+		ret = pac1921_read_res(priv, idev->channels[bit].address, &val);
 		if (ret)
 			goto done;
 
Signature · 1 line
2.25.1

This looks correct to me. I guess I didn't test it properly with a subset of enabled channels when I wrote this.
Thanks for the fix!

Quoted 1 line

Applied to the fixes-togreg branch of iio.git

Thanks,

Jonathan