iio: dac: mcp47feb02: reject out-of-range raw values
Subsystems (2)
PATCH 1/1 iio: dac: mcp47feb02: reject out-of-range raw values
mcp47feb02_write_raw() writes the user supplied raw value to the DAC register without checking it against the chip resolution. The parts are 8, 10 or 12 bit, and the bits above the resolution are not implemented, so writing 300 to out_voltage0_raw on an 8-bit part sets the output to code 44 instead of failing, and -1 is written as 0xffff.
The out-of-range value is also cached in dac_data, so it is written back on resume and stored to EEPROM by store_eeprom.
Return -EINVAL for values outside [0, 2^resolution - 1].
Found by code inspection. Build tested with W=1 (=m and =y) only; no hardware was available for runtime testing.
--- drivers/iio/dac/mcp47feb02-core.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/dac/mcp47feb02-core.c +3 -0
@@ -696,6 +696,9 @@ static int mcp47feb02_write_raw(struct iio_dev *indio_dev, struct iio_chan_specswitch (mask) {case IIO_CHAN_INFO_RAW : +if (val <0 || val >=BIT (data->chip_features->resolution)) +return -EINVAL ; + ret =regmap_write (data->regmap,REG_ADDR (ch->address), val);if (ret)return ret;
Signature · 1 line
2.55.0
Quoted 6 lines · On Sat, 24 Oct 2026 22:44:23 +0100 David Carlier <[email protected]> wrote:
mcp47feb02_write_raw()writes the user supplied raw value to the DAC register without checking it against the chip resolution. The parts are 8, 10 or 12 bit, and the bits above the resolution are not implemented, so writing 300 toout_voltage0_rawon an 8-bit part sets the output to code 44 instead of failing, and -1 is written as0xffff.
Quoted 7 lines
The out-of-range value is also cached in
dac_data, so it is written back on resume and stored to EEPROM bystore_eeprom.Return
-EINVALfor values outside [0, 2^resolution - 1].Found by code inspection. Build tested with W=1 (=m and =y) only; no hardware was available for runtime testing.
This should go under the ---.
Quoted 3 lines
Fixes bf394cc80369 ("iio: dac: adding support for Microchip MCP47FEB02")Assisted-by LLM
This tag has been deprecated.
Otherwise LGTM.
Signature · 2 lines
Kind regards, Joshua Crofts
Quoted 13 lines · David Carlier
mcp47feb02_write_raw()writes the user supplied raw value to the DAC register without checking it against the chip resolution. The parts are 8, 10 or 12 bit, and the bits above the resolution are not implemented, so writing 300 toout_voltage0_rawon an 8-bit part sets the output to code 44 instead of failing, and -1 is written as0xffff.The out-of-range value is also cached in
dac_data, so it is written back on resume and stored to EEPROM bystore_eeprom.Return
-EINVALfor values outside [0, 2^resolution - 1].Found by code inspection. Build tested with W=1 (=m and =y) only; no hardware was available for runtime testing.
...
drivers/iio/dac/mcp47feb02-core.c static int mcp47feb02_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec · line 699
Quoted 2 lines
drivers/iio/dac/mcp47feb02-core.c +1
-0case IIO_CHAN_INFO_RAW : +if (val <0 || val >=BIT (data->chip_features->resolution))
If resolution is 32, this will suck. (Yes, this is a bit theoretical case.)
drivers/iio/dac/mcp47feb02-core.c static int mcp47feb02_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec · line 700
Quoted 1 line
drivers/iio/dac/mcp47feb02-core.c +1
-0+return -EINVAL ;
Not sure about this error code. Do other IIO drivers use same in the similar situation?
Signature · 2 lines
With Best Regards, Andy Shevchenko
A message that is not in the archive (sent off-list or not yet received): CA+XhMqx5jusLqo8G2XCY9gy_P=3-jAwTacgzrR4XFyjpagi8dA@mail.gmail.com
Quoted 3 lines · David CARLIER
Quoted 1 line · Andy Shevchenko
On Sat, Oct 24, 2026 at 10:44:23PM +0100, David Carlier wrote:
...
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
case
IIO_CHAN_INFO_RAW:
+ if (val < 0 || val >=BIT(data->chip_features->resolution))If resolution is 32, this will suck. (Yes, this is a bit theoretical case.)
Quoted 8 lines
Hmm true.
Quoted 4 lines
Quoted 1 line
+ return
-EINVAL;Not sure about this error code. Do other IIO drivers use same in the similar situation?
Yes other drivers for every out-of-range check
IIO_CHAN_INFO_RAW.
No doubts. But I asked a different question. Can you answer it?
Signature · 2 lines
With Best Regards, Andy Shevchenko
A message that is not in the archive (sent off-list or not yet received): CA+XhMqzZZyN80nP2ER3jyTsM9UC2xMRLiqdCsAtp+K=hDbqtQg@mail.gmail.com
Quoted 6 lines · David CARLIER
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · David CARLIER
Quoted 1 line · Andy Shevchenko
On Sat, Oct 24, 2026 at 10:44:23PM +0100, David Carlier wrote:
...
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
+ return
-EINVAL;Not sure about this error code. Do other IIO drivers use same in the similar situation?
Yes other drivers for every out-of-range check
IIO_CHAN_INFO_RAW.No doubts. But I asked a different question. Can you answer it?
I meant in this case the other drivers
-EINVAL.
OK, thanks for confirming!
Signature · 2 lines
With Best Regards, Andy Shevchenko