[PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf
From: Rodrigo Alencar via B4 Relay
Date: Fri Nov 27 2026 - 04:48:29 EST
From: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
__iio_chan_prefix_emit() builds the channel prefix of an attribute name
through a struct seq_buf that is local to the function, so a caller that
wants to append to the prefix has to deal with the buffer offset itself.
Move the prefix logic into iio_chan_prefix_seq_buf(), which takes the
struct seq_buf directly and returns the length of the prefix, and keep
__iio_chan_prefix_emit() as a wrapper for its current users. This lets
the IIO core build a full attribute name in a single struct seq_buf.
Signed-off-by: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
---
drivers/iio/industrialio-core.c | 46 ++++++++++++++++++++++++++---------------
1 file changed, 29 insertions(+), 17 deletions(-)
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index dabd57a0994e..5267a9c5908c 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -206,28 +206,24 @@ static const char * const iio_chan_info_postfix[] = {
[IIO_CHAN_INFO_POWERFACTOR] = "powerfactor",
};
-VISIBLE_IF_KUNIT
-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)
+static ssize_t iio_chan_prefix_seq_buf(struct device *dev,
+ const struct iio_chan_spec *chan,
+ enum iio_shared_by shared_by,
+ struct seq_buf *s)
{
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);
+ seq_buf_printf(s, "%s", dir);
break;
case IIO_SHARED_BY_TYPE:
- seq_buf_printf(&s, "%s_%s", dir, type);
+ seq_buf_printf(s, "%s_%s", dir, type);
if (chan->differential)
- seq_buf_printf(&s, "-%s", type);
+ seq_buf_printf(s, "-%s", type);
break;
case IIO_SEPARATE:
/* Validate differential channel settings */
@@ -242,23 +238,39 @@ ssize_t __iio_chan_prefix_emit(struct device *dev,
}
}
- seq_buf_printf(&s, "%s_%s", dir, type);
+ seq_buf_printf(s, "%s_%s", dir, type);
if (chan->indexed)
- seq_buf_printf(&s, "%d", chan->channel);
+ seq_buf_printf(s, "%d", chan->channel);
if (chan->differential)
- seq_buf_printf(&s, "-%s%d", type, chan->channel2);
+ seq_buf_printf(s, "-%s%d", type, chan->channel2);
if (chan->modified)
- seq_buf_printf(&s, "_%s", iio_modifier_names[chan->channel2]);
+ seq_buf_printf(s, "_%s", iio_modifier_names[chan->channel2]);
if (chan->extend_name)
- seq_buf_printf(&s, "_%s", chan->extend_name);
+ seq_buf_printf(s, "_%s", chan->extend_name);
break;
}
- return seq_buf_has_overflowed(&s) ? -EOVERFLOW : s.len;
+ if (seq_buf_has_overflowed(s))
+ return -EOVERFLOW;
+
+ return seq_buf_used(s);
+}
+
+VISIBLE_IF_KUNIT
+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)
+{
+ struct seq_buf s;
+
+ seq_buf_init(&s, buf, len);
+
+ return iio_chan_prefix_seq_buf(dev, chan, shared_by, &s);
}
EXPORT_SYMBOL_IF_KUNIT(__iio_chan_prefix_emit);
--
2.43.0