* [PATCH v2 0/2] iio: adc: ad7124: Implement input validation
@ 2024-11-18 11:19 Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 1/2] iio: adc: ad7124: Don't create more channels than the driver can handle Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 2/2] iio: adc: ad7124: Refuse invalid input specifiers Uwe Kleine-König
0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2024-11-18 11:19 UTC (permalink / raw)
To: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron
Cc: linux-iio, linux-kernel
Hello,
(implicit) v1 of the series can be found at
https://lore.kernel.org/linux-iio/20241108181813.272593-4-u.kleine-koenig@baylibre.com.
This v2 bases on v6.12-rc1 + commit 64612ec9b909 ("iio: adc: ad7124:
Disable all channels at probe time") which is already in next.
Changes since v1:
- reword commit log of patch #1 to honestly tell that the maximal
number of logical channels is a limitation of the current driver
implementation (and not one imposed by hardware).
- Make the driver explicitly fail to load if > 16 channels are defined.
v1 only limited the maximal channel address which then later yielded
a probe failure on the first channel exeeding that maximum.
- in patch #2 fix "passing zero to 'dev_err_probe'" as reported by
smatch + Dan Carpenter.
- Fix my address in patch #2's S-o-b trailer.
Uwe Kleine-König (2):
iio: adc: ad7124: Don't create more channels than the driver can
handle
iio: adc: ad7124: Refuse invalid input specifiers
drivers/iio/adc/ad7124.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
prerequisite-patch-id: 617af17fc377a984762c61893b9f2a92ae62213a
--
2.45.2
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 1/2] iio: adc: ad7124: Don't create more channels than the driver can handle
2024-11-18 11:19 [PATCH v2 0/2] iio: adc: ad7124: Implement input validation Uwe Kleine-König
@ 2024-11-18 11:19 ` Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 2/2] iio: adc: ad7124: Refuse invalid input specifiers Uwe Kleine-König
1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2024-11-18 11:19 UTC (permalink / raw)
To: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron
Cc: linux-iio, linux-kernel
The ad7124-4 and ad7124-8 both support 16 channel registers and assigns
each channel defined in dt statically such a register. While the driver
could be a bit more clever about this, it currently isn't and specifying
more than 16 channels yields broken behaviour. So just refuse to bind in
this situation.
Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/iio/adc/ad7124.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 749304d38415..4de69bb8653a 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -821,6 +821,16 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev,
if (!st->num_channels)
return dev_err_probe(dev, -ENODEV, "no channel children\n");
+ /*
+ * The driver assigns each logical channel defined in the device tree
+ * statically one channel register. So only accept 16 such logical
+ * channels to not treat CONFIG_0 (i.e. the register following
+ * CHANNEL_15) as an additional channel register. The driver could be
+ * improved to lift this limitation.
+ */
+ if (st->num_channels > AD7124_MAX_CHANNELS)
+ return dev_err_probe(dev, -EINVAL, "Too many channels defined\n");
+
chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels,
sizeof(*chan), GFP_KERNEL);
if (!chan)
--
2.45.2
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] iio: adc: ad7124: Refuse invalid input specifiers
2024-11-18 11:19 [PATCH v2 0/2] iio: adc: ad7124: Implement input validation Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 1/2] iio: adc: ad7124: Don't create more channels than the driver can handle Uwe Kleine-König
@ 2024-11-18 11:19 ` Uwe Kleine-König
1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2024-11-18 11:19 UTC (permalink / raw)
To: Lars-Peter Clausen, Michael Hennerich, Jonathan Cameron
Cc: linux-iio, linux-kernel
The ad7124-4 has 8 analog inputs; the input select values 8 to 15 are
reserved and not to be used. These are fine for ad7124-8. For both
ad7124-4 and ad7124-8 values bigger than 15 are internal channels that
might appear as inputs in the channels specified in the device
description according to the description of commit f1794fd7bdf7 ("iio:
adc: ad7124: Remove input number limitation"), values bigger than 31
don't fit into the respective register bit field and the driver masked
them to smaller values.
Check for these invalid input specifiers and fail to probe if one is
found.
Fixes: f1794fd7bdf7 ("iio: adc: ad7124: Remove input number limitation")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/iio/adc/ad7124.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
index 4de69bb8653a..bfeec59e33ba 100644
--- a/drivers/iio/adc/ad7124.c
+++ b/drivers/iio/adc/ad7124.c
@@ -807,6 +807,19 @@ static int ad7124_check_chip_id(struct ad7124_state *st)
return 0;
}
+/*
+ * Input specifiers 8 - 15 are explicitly reserved for ad7124-4
+ * while they are fine for ad7124-8. Values above 31 don't fit
+ * into the register field and so are invalid for sure.
+ */
+static bool ad7124_valid_input_select(unsigned int ain, const struct ad7124_chip_info *info)
+{
+ if (ain >= info->num_inputs && ain < 16)
+ return false;
+
+ return ain <= FIELD_MAX(AD7124_CHANNEL_AINM_MSK);
+}
+
static int ad7124_parse_channel_config(struct iio_dev *indio_dev,
struct device *dev)
{
@@ -859,6 +872,11 @@ static int ad7124_parse_channel_config(struct iio_dev *indio_dev,
if (ret)
return ret;
+ if (!ad7124_valid_input_select(ain[0], st->chip_info) ||
+ !ad7124_valid_input_select(ain[1], st->chip_info))
+ return dev_err_probe(dev, -EINVAL,
+ "diff-channels property of %pfwP contains invalid data\n", child);
+
st->channels[channel].nr = channel;
st->channels[channel].ain = AD7124_CHANNEL_AINP(ain[0]) |
AD7124_CHANNEL_AINM(ain[1]);
--
2.45.2
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-11-18 11:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-18 11:19 [PATCH v2 0/2] iio: adc: ad7124: Implement input validation Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 1/2] iio: adc: ad7124: Don't create more channels than the driver can handle Uwe Kleine-König
2024-11-18 11:19 ` [PATCH v2 2/2] iio: adc: ad7124: Refuse invalid input specifiers Uwe Kleine-König
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®