diff options
author | Marius Cristea <marius.cristea@microchip.com> | 2024-04-25 14:42:32 +0300 |
---|---|---|
committer | Jonathan Cameron <Jonathan.Cameron@huawei.com> | 2024-04-28 14:31:49 +0100 |
commit | 40297eee0f393dea1eb84bde42bdc7313df61e72 (patch) | |
tree | c5ccf91a38a7e3381ee7dc429b58641fc0bd0b7b | |
parent | d7230b9952463731a7e64d788d008711d694e27f (diff) | |
download | iio-40297eee0f393dea1eb84bde42bdc7313df61e72.tar.gz |
iio: adc: PAC1934: fix accessing out of bounds array index
Notice: this object is not reachable from any branch.
Fix accessing out of bounds array index for average
current and voltage measurements. The device itself has
only 4 channels, but in sysfs there are "fake"
channels for the average voltages and currents too.
Fixes: 0fb528c8255b: "iio: adc: adding support for PAC193x"
Reported-by: Conor Dooley <conor.dooley@microchip.com>
Signed-off-by: Marius Cristea <marius.cristea@microchip.com>
Closes: https://lore.kernel.org/linux-iio/20240405-embellish-bonnet-ab5f10560d93@wendy/
Tested-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20240425114232.81390-1-marius.cristea@microchip.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Notice: this object is not reachable from any branch.
-rw-r--r-- | drivers/iio/adc/pac1934.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/drivers/iio/adc/pac1934.c b/drivers/iio/adc/pac1934.c index e0c2742da5236f..8a0c357422121b 100644 --- a/drivers/iio/adc/pac1934.c +++ b/drivers/iio/adc/pac1934.c @@ -787,6 +787,15 @@ static int pac1934_read_raw(struct iio_dev *indio_dev, s64 curr_energy; int ret, channel = chan->channel - 1; + /* + * For AVG the index should be between 5 to 8. + * To calculate PAC1934_CH_VOLTAGE_AVERAGE, + * respectively PAC1934_CH_CURRENT real index, we need + * to remove the added offset (PAC1934_MAX_NUM_CHANNELS). + */ + if (channel >= PAC1934_MAX_NUM_CHANNELS) + channel = channel - PAC1934_MAX_NUM_CHANNELS; + ret = pac1934_retrieve_data(info, PAC1934_MIN_UPDATE_WAIT_TIME_US); if (ret < 0) return ret; |