Devicetree
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Svyatoslav Ryhel <clamor95@gmail.com>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Esben Haabendal" <esben@geanix.com>,
	"Kuppuswamy Sathyanarayanan"
	<sathyanarayanan.kuppuswamy@linux.intel.com>,
	"Uwe Kleine-König (The Capable Hub)"
	<u.kleine-koenig@baylibre.com>, "Pan Chuang" <panchuang@vivo.com>,
	"Hemanth Selam" <hemanth.selam@gmail.com>,
	"Antoniu Miclaus" <antoniu.miclaus@analog.com>,
	"Nikita Travkin" <nikita@trvn.ru>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/4] iio: light: ltr501: Switch to read_avail() op from manual attribute registration
Date: Sat, 14 Nov 2026 22:55:49 +0000	[thread overview]
Message-ID: <20261114225549.003f9244@jic23-hlaptop> (raw)
In-Reply-To: <20261108094253.83899-2-clamor95@gmail.com>

On Sun,  8 Nov 2026 11:42:50 +0200
Svyatoslav Ryhel <clamor95@gmail.com> wrote:

> Switch to the read_avail() operation from manual attribute group
> registration. This eliminates the need for per-chip attribute_group
> structures and related ltr501_chip_info iio_info entries.
> 
> Adjust LTR501_INTENSITY_CHANNEL to account for the new
> info_mask_*_available layout, allowing the removal of _shared since all
> intensity channels share the same layout of available entries.
> 
> Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
Sashiko caught the bug I noticed as well..
https://sashiko.dev/#/patchset/20261108094253.83899-1-clamor95%40gmail.com
> ---
>  drivers/iio/light/ltr501.c | 192 +++++++++++++++----------------------
>  1 file changed, 76 insertions(+), 116 deletions(-)
> 
> diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
> index 302be2fc314c5..1ddd0dfd55ea2 100644
> --- a/drivers/iio/light/ltr501.c
> +++ b/drivers/iio/light/ltr501.c
> @@ -133,6 +133,22 @@ static const struct ltr501_gain ltr559_ps_gain_tbl[] = {
>  	{0, 15624},
>  };
>  
> +static const int int_time_avail[] = {
> +	0, 50000,
> +	0, 100000,
> +	0, 200000,
> +	0, 400000
> +};
> +
> +static const int samp_freq_avail[] = {
> +	20, 0,
> +	10, 0,
> +	5,  0,
> +	2,  0,
> +	1,  0,
> +	0,  500000
> +};

> -static ssize_t ltr501_show_proximity_scale_avail(struct device *dev,
> -						 struct device_attribute *attr,
> -						 char *buf)
> +static int ltr501_read_avail(struct iio_dev *indio_dev,
> +			     struct iio_chan_spec const *chan,
> +			     const int **vals, int *type, int *length, long mask)
>  {
> -	struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev));
> +	struct ltr501_data *data = iio_priv(indio_dev);
>  	const struct ltr501_chip_info *info = data->chip_info;
> -	ssize_t len = 0;
> -	int i;
> -
> -	for (i = 0; i < info->ps_gain_tbl_size; i++) {
> -		if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN)
> -			continue;
> -		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ",
> -				 info->ps_gain[i].scale,
> -				 info->ps_gain[i].uscale);
> -	}
>  
> -	buf[len - 1] = '\n';
> +	switch (mask) {
> +	case IIO_CHAN_INFO_SCALE:
> +		if (chan->type == IIO_INTENSITY) {
> +			int valid_als_gains[ARRAY_SIZE(ltr559_als_gain_tbl) * 2];
> +			int j = 0;
> +
> +			for (int i = 0; i < info->als_gain_tbl_size; i++) {
> +				if (info->als_gain[i].scale == LTR501_RESERVED_GAIN)
> +					continue;
> +
> +				valid_als_gains[j++] = info->als_gain[i].scale;
> +				valid_als_gains[j++] = info->als_gain[i].uscale;
> +			}
> +
> +			*length = j;
> +			*vals = (const int *)valid_als_gains;

As sashiko points out you just passed out a dangling pointer to the stack.
So this never works.  Even without that, there are some rather badly handled
issues with consumers of read_avail and lifetimes.  We had some fixes about
a year ago but they never made it upstream and it's not currently high
priority for me at least to revisit.  Upshot is that we are much better
returning a pointer to pseudo const data.  Can initialise before registering
the device but after that we should only use pointers that remain stable
and not change the data so as to avoid potential tearing.

Given the reserved entries are in the middle, you'll need to stash a copy
of the array with them scrubbed out. Do that in probe and put it somewhere
in the iio_priv() structure

> +		} else {
> +			*length = info->ps_gain_tbl_size * 2;
> +			*vals = (const int *)info->ps_gain;
> +		}
> +		*type = IIO_VAL_INT_PLUS_MICRO;
> +		return IIO_AVAIL_LIST;
>  
> -	return len;
> -}
> +	case IIO_CHAN_INFO_INT_TIME:
> +		*vals = int_time_avail;
> +		*type = IIO_VAL_INT_PLUS_MICRO;
> +		*length = ARRAY_SIZE(int_time_avail);
> +		return IIO_AVAIL_LIST;



  parent reply	other threads:[~2026-11-14 22:55 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-08  9:42 [PATCH v3 0/4] iio: light: ltr501: Add LTR-659PS-01 prox sensor support Svyatoslav Ryhel
2026-11-08  9:42 ` [PATCH v3 1/4] iio: light: ltr501: Switch to read_avail() op from manual attribute registration Svyatoslav Ryhel
2026-11-08  9:50   ` sashiko-bot
2026-11-09 20:03   ` Andy Shevchenko
2026-11-14 22:55   ` Jonathan Cameron [this message]
2026-11-08  9:42 ` [PATCH v3 2/4] iio: light: ltr501: Switch to modern match mechanism Svyatoslav Ryhel
2026-11-08  9:42 ` [PATCH v3 3/4] dt-bindings: iio: light: ltr501: Document LTR659 proximity sensor Svyatoslav Ryhel
2026-11-08  9:42 ` [PATCH v3 4/4] iio: light: ltr501: Add support for the LTR-659PS-01 " Svyatoslav Ryhel
2026-11-08 10:01   ` sashiko-bot
2026-11-08 21:48   ` kernel test robot
2026-11-09 20:04     ` Andy Shevchenko
2026-11-09 20:07       ` Svyatoslav Ryhel
2026-11-14 14:45         ` Svyatoslav Ryhel
2026-11-14 22:59           ` Jonathan Cameron
2026-11-14 23:02   ` Jonathan Cameron

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=20261114225549.003f9244@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=antoniu.miclaus@analog.com \
    --cc=clamor95@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=esben@geanix.com \
    --cc=hemanth.selam@gmail.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nikita@trvn.ru \
    --cc=nuno.sa@analog.com \
    --cc=panchuang@vivo.com \
    --cc=robh@kernel.org \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=u.kleine-koenig@baylibre.com \
    /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