linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matti Vaittinen <mazziesaccount@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>,
	Chen-Yu Tsai <wens@csie.org>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	Samuel Holland <samuel@sholland.org>,
	Hugo Villeneuve <hvilleneuve@dimonoff.com>,
	Nuno Sa <nuno.sa@analog.com>,
	David Lechner <dlechner@baylibre.com>,
	Javier Carrasco <javier.carrasco.cruz@gmail.com>,
	Guillaume Stols <gstols@baylibre.com>,
	Dumitru Ceclan <mitrutzceclan@gmail.com>,
	Trevor Gamblin <tgamblin@baylibre.com>,
	Matteo Martelli <matteomartelli3@gmail.com>,
	Alisa-Dariana Roman <alisadariana@gmail.com>,
	Ramona Alexandra Nechita <ramona.nechita@analog.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org,
	linux-renesas-soc@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-sunxi@lists.linux.dev
Subject: Re: [PATCH v7 03/10] iio: adc: add helpers for parsing ADC nodes
Date: Thu, 13 Mar 2025 15:17:27 +0200	[thread overview]
Message-ID: <bca95d63-fb6e-4d6c-8ab6-df67f0e697e6@gmail.com> (raw)
In-Reply-To: <Z9LQFqSweiV-zT3b@smile.fi.intel.com>

On 13/03/2025 14:31, Andy Shevchenko wrote:
> On Thu, Mar 13, 2025 at 09:18:18AM +0200, Matti Vaittinen wrote:
>> There are ADC ICs which may have some of the AIN pins usable for other
>> functions. These ICs may have some of the AIN pins wired so that they
>> should not be used for ADC.
>>
>> (Preferred?) way for marking pins which can be used as ADC inputs is to
>> add corresponding channels@N nodes in the device tree as described in
>> the ADC binding yaml.
>>
>> Add couple of helper functions which can be used to retrieve the channel
>> information from the device node.
> 
> ...
> 
>> +int devm_iio_adc_device_alloc_chaninfo_se(struct device *dev,
>> +					  const struct iio_chan_spec *template,
>> +					  int max_chan_id,
>> +					  struct iio_chan_spec **cs)
>> +{
>> +	struct iio_chan_spec *chan_array, *chan;
>> +	int num_chan = 0, ret;
> 
> Unneeded assignment.

Hmm. I have a deja-vu. Thanks for the reminder.

> 
>> +	num_chan = iio_adc_device_num_channels(dev);
>> +	if (num_chan < 1)
>> +		return num_chan;
> 
> This is really interesting code. So, if the above returns negative error code,
> we return it, if it returns 0, we return success (but 0 channels)?

Yes. I don't think it's that interesting though. Checking the devicetree 
succeeded while no channels were found. I think returning 0 is very much 
aligned with this.

> Shouldn't we do *cs = NULL; at the case of 0 channels if it's a success?

I suppose you're right.

But, as you pointed out in review of the 05/10:
 > Usually in other similar APIs we return -ENOENT. And user won't need
 > to have an additional check in case of 0 being considered as an error
 > case too.

I don't know whether to agree with you here. For majority of the ADC 
drivers, having no channels in devicetree is indeed just another error, 
which I think is not in any ways special.

However, for 33,3333% of the users added in this patch, the "no channels 
found" is not really an error condition ;) The BD79124 could have all 
channels used for GPO - although this would probably be very very 
unusual. (Why buying an ADC chip if you need just a GPO?). Still, this 
wouldn't be an error. (And I need to handle this better in BD79124 probe 
- so thanks).

> (Under success I assume that returned values are okay to go with, and cs in
> your case will be left uninitialised or contain something we don't control.

I see your point although I wouldn't be concerned with cs not being NULL 
for as long as number of channels is zero.

Anyway, I think it makes sense to simplify ~67% of callers by returning 
-ENODEV if there is no channels. The remaining ~33% can then check for 
the -ENODEV and handle it separately from other returned errors. So, thanks.

>> +	chan_array = devm_kcalloc(dev, num_chan, sizeof(*chan_array),
>> +				  GFP_KERNEL);
>> +	if (!chan_array)
>> +		return -ENOMEM;
>> +
>> +	chan = &chan_array[0];
>> +
>> +	device_for_each_named_child_node_scoped(dev, child, "channel") {
>> +		u32 ch;
>> +
>> +		ret = fwnode_property_read_u32(child, "reg", &ch);
>> +		if (ret)
>> +			return ret;
> 
>> +		if (max_chan_id != -1 && ch > max_chan_id)
>> +			return -ERANGE;
> 
> Hmm... What if max_chan_id is equal to an error code?
> Or in other words, why -1 is special and not all negative numbers?

-1 was just picked to represent a 'don't care' value. Old habit. In the 
old days I handled lots of code where -1 was defined as 'invalid' for 
APIs with unsigned ints as well. It works nicely on systems where it 
turns out to be maximum positive value - leaving most of the number 
space for valid values.

I suppose saying any negative means "don't care" works well here. And, 
dropping all negatives here will also make the check below just work 
with unsigned comparison.

> Also note, you used unsigned type and compare it to int which,
> in case of being negative will give promotion.

Right. I didn't thin negative IDs would make sense and trusted users to 
pass only positive ones. Treating all negatives as "don't care" is 
indeed better than trusting this.

Thanks.

> The ch will not be
> big enough in most cases (unless it's great than (INT_MAX + 1).
> 
> TL;DR: you have a potential integer overflow here.
> 
>> +		*chan = *template;
>> +		chan->channel = ch;
>> +		chan++;
>> +	}
>> +
>> +	*cs = chan_array;
>> +
>> +	return num_chan;
>> +}
> 


  reply	other threads:[~2025-03-13 13:17 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13  7:16 [PATCH v7 00/10] Support ROHM BD79124 ADC Matti Vaittinen
2025-03-13  7:17 ` [PATCH v7 01/10] dt-bindings: ROHM BD79124 ADC/GPO Matti Vaittinen
2025-03-13  7:18 ` [PATCH v7 02/10] property: Add functions to iterate named child Matti Vaittinen
2025-03-13 12:15   ` Andy Shevchenko
2025-03-16 21:45   ` Marcelo Schmitt
2025-03-13  7:18 ` [PATCH v7 03/10] iio: adc: add helpers for parsing ADC nodes Matti Vaittinen
2025-03-13 12:31   ` Andy Shevchenko
2025-03-13 13:17     ` Matti Vaittinen [this message]
2025-03-13 13:29       ` Andy Shevchenko
2025-03-16  9:38   ` Jonathan Cameron
2025-03-17  8:22     ` Matti Vaittinen
2025-03-13  7:18 ` [PATCH v7 04/10] iio: adc: rzg2l_adc: Use adc-helpers Matti Vaittinen
2025-03-13  7:18 ` [PATCH v7 05/10] iio: adc: sun20i-gpadc: " Matti Vaittinen
2025-03-13 12:34   ` Andy Shevchenko
2025-03-16  9:41     ` Jonathan Cameron
2025-03-17  7:11       ` Matti Vaittinen
2025-03-17  7:51         ` Andy Shevchenko
2025-03-17  8:42           ` Matti Vaittinen
2025-03-17  9:27             ` Andy Shevchenko
2025-03-17 10:45               ` Jonathan Cameron
2025-03-13  7:19 ` [PATCH v7 06/10] iio: adc: Support ROHM BD79124 ADC Matti Vaittinen
2025-03-13 13:19   ` Andy Shevchenko
2025-03-14  7:31     ` Matti Vaittinen
2025-03-14  8:52       ` Matti Vaittinen
2025-03-14 14:33       ` Andy Shevchenko
2025-03-16  9:52         ` Jonathan Cameron
2025-03-16 10:01           ` Jonathan Cameron
2025-03-17  6:52           ` Matti Vaittinen
2025-03-17 10:52             ` Jonathan Cameron
2025-03-14  9:22     ` Matti Vaittinen
2025-03-14 14:37       ` Andy Shevchenko
2025-03-17  7:07         ` Matti Vaittinen
2025-03-17  7:57           ` Andy Shevchenko
2025-03-17  8:33             ` Matti Vaittinen
2025-03-16 11:02   ` Jonathan Cameron
2025-03-17  7:34     ` Matti Vaittinen
2025-03-17 11:24     ` Matti Vaittinen
2025-03-30 16:04       ` Jonathan Cameron
2025-03-31  7:37         ` Andy Shevchenko
2025-03-31  7:51           ` Matti Vaittinen
2025-03-13  7:19 ` [PATCH v7 07/10] MAINTAINERS: Add IIO ADC helpers Matti Vaittinen
2025-03-13  7:19 ` [PATCH v7 08/10] MAINTAINERS: Add ROHM BD79124 ADC/GPO Matti Vaittinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bca95d63-fb6e-4d6c-8ab6-df67f0e697e6@gmail.com \
    --to=mazziesaccount@gmail.com \
    --cc=alisadariana@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gstols@baylibre.com \
    --cc=hvilleneuve@dimonoff.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=matteomartelli3@gmail.com \
    --cc=matti.vaittinen@fi.rohmeurope.com \
    --cc=mitrutzceclan@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=prabhakar.mahadev-lad.rj@bp.renesas.com \
    --cc=ramona.nechita@analog.com \
    --cc=samuel@sholland.org \
    --cc=tgamblin@baylibre.com \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).