Re: [PATCH v8 06/17] iio: core: create local __iio_chan_prefix_emit() for reuse

Andy Shevchenko <[email protected]>
Newsgroups org.kernel.vger.linux-hardening,org.kernel.vger.linux-devicetree,org.kernel.vger.linux-doc,org.kernel.vger.linux-iio,org.kernel.vger.linux-kernel
Organization Intel Finland Oy - BIC 0357606-4 - c/o Alberga Business Park, 6 krs, Bertel Jungin Aukio 5, 02600 Espoo
Message-ID <[email protected]>
On Tue, Jul 14, 2026 at 12:02:46PM +0100, Rodrigo Alencar via B4 Relay wrote:

> Move logic to create a channel prefix for naming attribute files into a
> separate __iio_chan_prefix_emit() function for reuse. The dev pointer is
> passed to __iio_device_attr_init() so that dev_err() replaces WARN().
> The new helper is going to be used to expose channel relationships as
> sysfs attributes.

...

> +static ssize_t __iio_chan_prefix_emit(struct device *dev,
> +				      const struct iio_chan_spec *chan,
> +				      enum iio_shared_by shared_by,
> +				      char *buf, size_t len)
> +{
> +	const char *type = iio_chan_type_name_spec[chan->type];
> +	const char *dir = iio_direction[chan->output];
> +	struct seq_buf s;
> +
> +	seq_buf_init(&s, buf, len);
> +
> +	switch (shared_by) {
> +	case IIO_SHARED_BY_ALL:
> +		break;
> +	case IIO_SHARED_BY_DIR:
> +		seq_buf_printf(&s, "%s", dir);
> +		break;
> +	case IIO_SHARED_BY_TYPE:
> +		seq_buf_printf(&s, "%s_%s", dir, type);
> +		if (chan->differential)
> +			seq_buf_printf(&s, "-%s", type);
> +		break;
> +	case IIO_SEPARATE:

> +		seq_buf_printf(&s, "%s_%s", dir, type);
> +		if (chan->indexed) {
> +			seq_buf_printf(&s, "%d", chan->channel);
> +			if (chan->differential)
> +				seq_buf_printf(&s, "-%s%d", type, chan->channel2);
> +		} else if (chan->differential) {
> +			dev_err(dev, "Differential channels must be indexed\n");
> +			return -EINVAL;
> +		}
> +
> +		if (chan->modified) {
> +			if (chan->differential) {
> +				dev_err(dev, "Differential channels can not have modifier\n");
> +				return -EINVAL;
> +			}
> +			seq_buf_printf(&s, "_%s", iio_modifier_names[chan->channel2]);
> +		}
> +
> +		if (chan->extend_name)
> +			seq_buf_printf(&s, "_%s", chan->extend_name);
> +		break;

Can we actually do the validation first without dirtying the output?

		/* Validate differential channel settings */
		if (chan->differential) {
			if (!chan->indexed) {
				dev_err(dev, "Differential channels must be indexed\n");
				return -EINVAL;
			}
			if (chan->modified) {
				dev_err(dev, "Differential channels can not have modifier\n");
				return -EINVAL;
			}
		}

		seq_buf_printf(&s, "%s_%s", dir, type);

		if (chan->indexed) {
			seq_buf_printf(&s, "%d", chan->channel);

		if (chan->differential)
			seq_buf_printf(&s, "-%s%d", type, chan->channel2);

		if (chan->modified)
			seq_buf_printf(&s, "_%s", iio_modifier_names[chan->channel2]);

		if (chan->extend_name)
			seq_buf_printf(&s, "_%s", chan->extend_name);
		break;


> +	}
> +
> +	return seq_buf_has_overflowed(&s) ? -EOVERFLOW : s.len;
> +}

-- 
With Best Regards,
Andy Shevchenko
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.