mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes
@ 2026-11-27  9:47 Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf Rodrigo Alencar via B4 Relay
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

struct iio_dev_attr does three unrelated jobs: it backs the static
attributes declared by drivers through IIO_DEVICE_ATTR*(), the channel
and event attributes generated by the IIO core, and the per-buffer
attributes. Every user carries every field (l, c, buffer), whether it
needs them or not, and the meaning of @address depends on the context:
an info mask bit, an ext_info index, a scan index or, for events, the
event_spec index and the event info packed together.

This series keeps struct iio_dev_attr as what drivers use (dev_attr and
address) and moves the core-only state into private types embedding it:

  struct iio_chan_attr   - core channel attributes (channel, list node)
  struct iio_event_attr  - extends it with the event spec
  struct iio_buffer_attr - extends it with the owning buffer

Attribute creation is split into iio_chan_attr_prepare(), which builds
the full name and runs the duplicate check, and iio_chan_attr_add(),
which initializes an attribute allocated by the caller. Each context
allocates its own type and sets its own fields, so the core needs no
size, offset or context pointer.

The intended benefits are:

- Static driver attributes no longer carry the core-only fields: 32
  bytes less per IIO_DEVICE_ATTR*() on 64-bit (72 -> 40, without
  lockdep), for roughly 220 users in tree.

- Core channel attributes fit a smaller slab class: struct iio_chan_attr
  is 64 bytes on 64-bit, where struct iio_dev_attr was 72 (kmalloc-64
  instead of kmalloc-96).

- Shared attributes (info_mask_shared_by_*, shared ext_info and shared
  event info) are deduplicated before anything is allocated. Today,
  every channel after the first one allocates the attribute and its
  name, finds the duplicate and frees both again.

- Event attributes point to their struct iio_event_spec instead of
  packing (info << 16) | spec_index into the address.

- Buffer implementations no longer depend on the attribute layout to
  find their buffer: iio_attr_to_buffer() replaces
  to_iio_dev_attr(attr)->buffer.

- Context-specific attributes become an extension point instead of yet
  another field in a shared structure. The IIO backend framework is the
  obvious next user: iio_backend_ext_info_{get,set}() recover the
  backend from indio_dev->dev.parent, which, as noted there, breaks as
  soon as a frontend has more than one backend, and
  iio_backend_extend_chan_spec() refuses to combine backend ext_info with
  the frontend's own. A struct iio_backend_attr carrying its struct
  iio_backend, created through the same prepare/add steps (and so
  deduplicated against the frontend attributes), could address both
  without touching struct iio_dev_attr or struct iio_info. That is not
  part of this series.

No ABI change is intended: attribute names, modes and callbacks are
unchanged.

This series was developed with the help of an AI coding assistant
(Claude Opus 5.5, through Claude Code). Over an interactive session, it
reviewed the IIO core, discussed design alternatives, wrote the code
under my direction and helped write the commit messages. I reviewed all
of it, and the design choices are mine.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
Changes in v2:
- Drop the RFC prefix.
- New patch 1: emit channel prefixes into a struct seq_buf.
- Remove each struct iio_dev_attr field in the patch where it stops being used.
- iio_buffer_wrap_attr(): use __free(kfree) and no_free_ptr().
- Link to v1: https://patch.msgid.link/20261120-iio-core-attr-review-v1-0-05b41f8133c2@analog.com

To: linux-kernel@vger.kernel.org
To: linux-iio@vger.kernel.org
Cc: Jonathan Cameron <jic23@kernel.org>
Cc: David Lechner <dlechner@baylibre.com>
Cc: Nuno Sá <nuno.sa@analog.com>
Cc: Andy Shevchenko <andy@kernel.org>

---
Rodrigo Alencar (5):
      iio: core: emit channel prefixes into a struct seq_buf
      iio: buffer: add iio_attr_to_buffer() helper
      iio: core: add struct iio_chan_attr and split attribute creation
      iio: event: add struct iio_event_attr
      iio: buffer: add struct iio_buffer_attr

 drivers/iio/buffer/industrialio-buffer-dmaengine.c |   2 +-
 drivers/iio/iio_core.h                             |  58 +++-
 drivers/iio/industrialio-buffer.c                  | 154 ++++++----
 drivers/iio/industrialio-core.c                    | 321 +++++++++++----------
 drivers/iio/industrialio-event.c                   |  99 +++++--
 include/linux/iio/buffer_impl.h                    |   3 +
 include/linux/iio/sysfs.h                          |   9 -
 7 files changed, 387 insertions(+), 259 deletions(-)
---
base-commit: 39508411b82d4dc29dd245bb471ec9ecc1111c9d
change-id: 20261120-iio-core-attr-review-1323b39846f9

Best regards,
--  
Rodrigo Alencar <rodrigo.alencar@analog.com>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf
  2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
@ 2026-11-27  9:47 ` Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 2/5] iio: buffer: add iio_attr_to_buffer() helper Rodrigo Alencar via B4 Relay
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

From: Rodrigo Alencar <rodrigo.alencar@analog.com>

__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@analog.com>
---
 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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/5] iio: buffer: add iio_attr_to_buffer() helper
  2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf Rodrigo Alencar via B4 Relay
@ 2026-11-27  9:47 ` Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 3/5] iio: core: add struct iio_chan_attr and split attribute creation Rodrigo Alencar via B4 Relay
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

From: Rodrigo Alencar <rodrigo.alencar@analog.com>

Buffer implementations providing their own attributes through
struct iio_buffer::attrs need to find the buffer an attribute belongs
to. The DMAengine buffer does that with to_iio_dev_attr(attr)->buffer,
which depends on the IIO buffer core wrapping those attributes into its
own copies and on the layout of struct iio_dev_attr.

Add iio_attr_to_buffer(), so buffer implementations no longer depend on
how the IIO core keeps track of the buffer, and use it in the DMAengine
buffer.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
 drivers/iio/buffer/industrialio-buffer-dmaengine.c |  2 +-
 drivers/iio/industrialio-buffer.c                  | 15 +++++++++++++++
 include/linux/iio/buffer_impl.h                    |  3 +++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c
index 7ecb9d875b8e..2e04cd5e3411 100644
--- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c
+++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c
@@ -226,7 +226,7 @@ static const struct iio_dma_buffer_ops iio_dmaengine_default_ops = {
 static ssize_t iio_dmaengine_buffer_get_length_align(struct device *dev,
 						     struct device_attribute *attr, char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = iio_attr_to_buffer(attr);
 	struct dmaengine_buffer *dmaengine_buffer =
 		iio_buffer_to_dmaengine_buffer(buffer);
 
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 4294101810fd..2d7759325eb3 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -69,6 +69,21 @@ static const char * const iio_endian_prefix[] = {
 	[IIO_LE] = "le",
 };
 
+/**
+ * iio_attr_to_buffer() - Get the IIO buffer a buffer attribute belongs to
+ * @attr: Device attribute of the buffer, as passed to its show/store callbacks
+ *
+ * Only valid for attributes registered through &struct iio_buffer.attrs or
+ * created by the IIO buffer core.
+ *
+ * Returns: Pointer to the IIO buffer.
+ */
+struct iio_buffer *iio_attr_to_buffer(struct device_attribute *attr)
+{
+	return to_iio_dev_attr(attr)->buffer;
+}
+EXPORT_SYMBOL_GPL(iio_attr_to_buffer);
+
 static bool iio_buffer_is_active(struct iio_buffer *buf)
 {
 	return !list_empty(&buf->buffer_list);
diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
index 9442c165561a..c259dfdf46a2 100644
--- a/include/linux/iio/buffer_impl.h
+++ b/include/linux/iio/buffer_impl.h
@@ -9,6 +9,7 @@
 #include <uapi/linux/iio/buffer.h>
 #include <linux/iio/buffer.h>
 
+struct device_attribute;
 struct dma_buf_attachment;
 struct dma_fence;
 struct iio_dev;
@@ -197,6 +198,8 @@ void iio_buffer_put(struct iio_buffer *buffer);
 
 void iio_buffer_signal_dmabuf_done(struct dma_fence *fence, int ret);
 
+struct iio_buffer *iio_attr_to_buffer(struct device_attribute *attr);
+
 #else /* CONFIG_IIO_BUFFER */
 
 static inline void iio_buffer_get(struct iio_buffer *buffer) {}

-- 
2.43.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 3/5] iio: core: add struct iio_chan_attr and split attribute creation
  2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 2/5] iio: buffer: add iio_attr_to_buffer() helper Rodrigo Alencar via B4 Relay
@ 2026-11-27  9:47 ` Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 4/5] iio: event: add struct iio_event_attr Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 5/5] iio: buffer: add struct iio_buffer_attr Rodrigo Alencar via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

From: Rodrigo Alencar <rodrigo.alencar@analog.com>

Channel, event and buffer attributes are all created through
__iio_add_chan_devattr(), which allocates a struct iio_dev_attr and its
name, and only then walks the attribute list looking for a duplicate.
For shared attributes (info_mask_shared_by_*, shared ext_info and shared
event info) every channel after the first one allocates the attribute
and its name just to find the duplicate and free both again. The
function also takes a buffer pointer that only buffer attributes use.

Introduce struct iio_chan_attr for the attributes created by the IIO
core. It embeds struct iio_dev_attr and adds the channel spec and the
list node, so that the core-only state no longer needs to live in the
structure drivers use for their own attributes. Other attribute types
can extend it further.

Replace __iio_add_chan_devattr() with two steps:

 - iio_chan_attr_prepare() builds the full attribute name on the stack
   and checks it against the attribute list.
 - iio_chan_attr_add() initializes an attribute allocated by the caller
   and adds it to the list.

The caller allocates its own attribute type in between, so the core
needs no size or context pointer, and duplicates are detected before
anything is allocated.

Also add iio_attr_show_t and iio_attr_store_t, so the show and store
callback prototypes are not spelled out in every helper.

Event and buffer attributes are converted to struct iio_chan_attr, but
otherwise keep their current behavior.

The list node and channel spec of struct iio_dev_attr are no longer used,
so remove them. Attributes declared by drivers through IIO_DEVICE_ATTR*()
and friends no longer carry them.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
 drivers/iio/iio_core.h            |  58 ++++++--
 drivers/iio/industrialio-buffer.c |  96 ++++++-------
 drivers/iio/industrialio-core.c   | 275 ++++++++++++++++++++------------------
 drivers/iio/industrialio-event.c  |  72 +++++++---
 include/linux/iio/sysfs.h         |   5 -
 5 files changed, 287 insertions(+), 219 deletions(-)

diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 59fa1bd9924b..6a391a604bf9 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -11,6 +11,9 @@
 #define _IIO_CORE_H_
 #include <linux/kernel.h>
 #include <linux/device.h>
+#include <linux/limits.h>
+#include <linux/list.h>
+#include <linux/iio/sysfs.h>
 
 struct iio_buffer;
 struct iio_chan_spec;
@@ -38,20 +41,47 @@ ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
 				  const struct iio_chan_spec *c,
 				  char *buf);
 
-int __iio_add_chan_devattr(const char *postfix,
-			   struct iio_chan_spec const *chan,
-			   ssize_t (*func)(struct device *dev,
-					   struct device_attribute *attr,
-					   char *buf),
-			   ssize_t (*writefunc)(struct device *dev,
-						struct device_attribute *attr,
-						const char *buf,
-						size_t len),
-			   u64 mask,
-			   enum iio_shared_by shared_by,
-			   struct device *dev,
-			   struct iio_buffer *buffer,
-			   struct list_head *attr_list);
+typedef typeof_member(struct device_attribute, show) iio_attr_show_t;
+typedef typeof_member(struct device_attribute, store) iio_attr_store_t;
+
+/**
+ * struct iio_chan_attr - IIO core managed channel attribute
+ * @iio_attr:	underlying IIO device attribute
+ * @c:		specification for the underlying channel (if any)
+ * @l:		list head for maintaining list of dynamically created attrs
+ *
+ * Other IIO core attribute types extend this one and must embed it as their
+ * first member, since attribute lists are freed through this type.
+ */
+struct iio_chan_attr {
+	struct iio_dev_attr iio_attr;
+	const struct iio_chan_spec *c;
+	struct list_head l;
+};
+
+#define to_iio_chan_attr(_dev_attr)					\
+	container_of(to_iio_dev_attr(_dev_attr), struct iio_chan_attr, iio_attr)
+
+/**
+ * struct iio_chan_attr_ctx - context for creating an IIO channel attribute
+ * @chan:	specification for the underlying channel
+ * @attr_list:	list the attribute is added to
+ * @name:	full attribute name
+ */
+struct iio_chan_attr_ctx {
+	const struct iio_chan_spec *chan;
+	struct list_head *attr_list;
+	char name[NAME_MAX + 1];
+};
+
+int iio_chan_attr_prepare(struct iio_chan_attr_ctx *ctx, struct device *dev,
+			  const struct iio_chan_spec *chan,
+			  enum iio_shared_by shared_by, const char *postfix,
+			  struct list_head *attr_list);
+int iio_chan_attr_add(const struct iio_chan_attr_ctx *ctx,
+		      struct iio_chan_attr *attr,
+		      iio_attr_show_t readfunc, iio_attr_store_t writefunc,
+		      u64 mask);
 void iio_free_chan_devattr_list(struct list_head *attr_list);
 
 int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 2d7759325eb3..52025d60052b 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -412,7 +412,7 @@ static ssize_t iio_show_scan_index(struct device *dev,
 				   struct device_attribute *attr,
 				   char *buf)
 {
-	return sysfs_emit(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
+	return sysfs_emit(buf, "%u\n", to_iio_chan_attr(attr)->c->scan_index);
 }
 
 static ssize_t iio_show_fixed_type(struct device *dev,
@@ -420,7 +420,7 @@ static ssize_t iio_show_fixed_type(struct device *dev,
 				   char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	const struct iio_scan_type *scan_type;
 	u8 type;
 
@@ -651,56 +651,61 @@ static ssize_t iio_scan_el_ts_store(struct device *dev,
 	return len;
 }
 
+static int iio_buffer_add_chan_devattr(struct iio_dev *indio_dev,
+				       struct iio_buffer *buffer,
+				       const char *postfix,
+				       const struct iio_chan_spec *chan,
+				       iio_attr_show_t readfunc,
+				       iio_attr_store_t writefunc,
+				       u64 mask)
+{
+	struct iio_chan_attr *buf_attr;
+	struct iio_chan_attr_ctx ctx;
+	int ret;
+
+	ret = iio_chan_attr_prepare(&ctx, &indio_dev->dev, chan, IIO_SEPARATE,
+				    postfix, &buffer->buffer_attr_list);
+	if (ret)
+		return ret;
+
+	buf_attr = kzalloc_obj(*buf_attr);
+	if (!buf_attr)
+		return -ENOMEM;
+
+	buf_attr->iio_attr.buffer = buffer;
+	ret = iio_chan_attr_add(&ctx, buf_attr, readfunc, writefunc, mask);
+	if (ret)
+		kfree(buf_attr);
+
+	return ret;
+}
+
 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
 					struct iio_buffer *buffer,
 					const struct iio_chan_spec *chan)
 {
 	int ret, attrcount = 0;
 
-	ret = __iio_add_chan_devattr("index",
-				     chan,
-				     &iio_show_scan_index,
-				     NULL,
-				     0,
-				     IIO_SEPARATE,
-				     &indio_dev->dev,
-				     buffer,
-				     &buffer->buffer_attr_list);
+	ret = iio_buffer_add_chan_devattr(indio_dev, buffer, "index", chan,
+					  &iio_show_scan_index, NULL, 0);
 	if (ret)
 		return ret;
 	attrcount++;
-	ret = __iio_add_chan_devattr("type",
-				     chan,
-				     &iio_show_fixed_type,
-				     NULL,
-				     0,
-				     IIO_SEPARATE,
-				     &indio_dev->dev,
-				     buffer,
-				     &buffer->buffer_attr_list);
+	ret = iio_buffer_add_chan_devattr(indio_dev, buffer, "type", chan,
+					  &iio_show_fixed_type, NULL, 0);
 	if (ret)
 		return ret;
 	attrcount++;
 	if (chan->type != IIO_TIMESTAMP)
-		ret = __iio_add_chan_devattr("en",
-					     chan,
-					     &iio_scan_el_show,
-					     &iio_scan_el_store,
-					     chan->scan_index,
-					     IIO_SEPARATE,
-					     &indio_dev->dev,
-					     buffer,
-					     &buffer->buffer_attr_list);
+		ret = iio_buffer_add_chan_devattr(indio_dev, buffer, "en", chan,
+						  &iio_scan_el_show,
+						  &iio_scan_el_store,
+						  chan->scan_index);
 	else
-		ret = __iio_add_chan_devattr("en",
-					     chan,
-					     &iio_scan_el_ts_show,
-					     &iio_scan_el_ts_store,
-					     chan->scan_index,
-					     IIO_SEPARATE,
-					     &indio_dev->dev,
-					     buffer,
-					     &buffer->buffer_attr_list);
+		ret = iio_buffer_add_chan_devattr(indio_dev, buffer, "en", chan,
+						  &iio_scan_el_ts_show,
+						  &iio_scan_el_ts_store,
+						  chan->scan_index);
 	if (ret)
 		return ret;
 	attrcount++;
@@ -1520,24 +1525,23 @@ static struct attribute *iio_buffer_attrs[] = {
 static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
 					      struct attribute *attr)
 {
+	struct iio_chan_attr *buf_attr __free(kfree) = kzalloc_obj(*buf_attr);
 	struct device_attribute *dattr = to_dev_attr(attr);
 	struct iio_dev_attr *iio_attr;
 
-	iio_attr = kzalloc_obj(*iio_attr);
-	if (!iio_attr)
+	if (!buf_attr)
 		return NULL;
 
+	iio_attr = &buf_attr->iio_attr;
 	iio_attr->buffer = buffer;
 	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
 	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
-	if (!iio_attr->dev_attr.attr.name) {
-		kfree(iio_attr);
+	if (!iio_attr->dev_attr.attr.name)
 		return NULL;
-	}
 
 	sysfs_attr_init(&iio_attr->dev_attr.attr);
 
-	list_add(&iio_attr->l, &buffer->buffer_attr_list);
+	list_add(&no_free_ptr(buf_attr)->l, &buffer->buffer_attr_list);
 
 	return &iio_attr->dev_attr.attr;
 }
@@ -2151,7 +2155,7 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	unsigned int masklength = iio_get_masklength(indio_dev);
-	struct iio_dev_attr *p;
+	struct iio_chan_attr *p;
 	const struct iio_dev_attr *id_attr;
 	struct attribute **attr;
 	int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
@@ -2258,7 +2262,7 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 
 	attrn = 0;
 	list_for_each_entry(p, &buffer->buffer_attr_list, l)
-		attr[attrn++] = &p->dev_attr.attr;
+		attr[attrn++] = &p->iio_attr.dev_attr.attr;
 
 	buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
 	if (!buffer->buffer_group.name) {
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 5267a9c5908c..0bef03cb9fd4 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -594,10 +594,10 @@ static ssize_t iio_read_channel_ext_info(struct device *dev,
 				     char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	const struct iio_chan_spec_ext_info *ext_info;
 
-	ext_info = &this_attr->c->ext_info[this_attr->address];
+	ext_info = &this_attr->c->ext_info[this_attr->iio_attr.address];
 
 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
 }
@@ -607,10 +607,10 @@ static ssize_t iio_write_channel_ext_info(struct device *dev,
 				     const char *buf, size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	const struct iio_chan_spec_ext_info *ext_info;
 
-	ext_info = &this_attr->c->ext_info[this_attr->address];
+	ext_info = &this_attr->c->ext_info[this_attr->iio_attr.address];
 
 	return ext_info->write(indio_dev, ext_info->private,
 			       this_attr->c, buf, len);
@@ -889,14 +889,14 @@ static ssize_t iio_read_channel_label(struct device *dev,
 				      char *buf)
 {
 	return do_iio_read_channel_label(dev_to_iio_dev(dev),
-					 to_iio_dev_attr(attr)->c, buf);
+					 to_iio_chan_attr(attr)->c, buf);
 }
 
 static ssize_t iio_read_channel_parent(struct device *dev,
 				       struct device_attribute *attr,
 				       char *buf)
 {
-	const struct iio_chan_spec *chan = to_iio_dev_attr(attr)->c->parent;
+	const struct iio_chan_spec *chan = to_iio_chan_attr(attr)->c->parent;
 	ssize_t len;
 
 	len = __iio_chan_prefix_emit(dev, chan, IIO_SEPARATE, buf, PAGE_SIZE - 1);
@@ -911,7 +911,7 @@ static ssize_t iio_read_channel_info(struct device *dev,
 				     char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int vals[INDIO_MAX_RAW_ELEMENTS];
 	int ret;
 	int val_len = 2;
@@ -920,10 +920,10 @@ static ssize_t iio_read_channel_info(struct device *dev,
 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
 							INDIO_MAX_RAW_ELEMENTS,
 							vals, &val_len,
-							this_attr->address);
+							this_attr->iio_attr.address);
 	else if (indio_dev->info->read_raw)
 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
-				    &vals[0], &vals[1], this_attr->address);
+				    &vals[0], &vals[1], this_attr->iio_attr.address);
 	else
 		return -EINVAL;
 
@@ -1002,7 +1002,7 @@ static ssize_t iio_read_channel_info_avail(struct device *dev,
 					   char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	const int *vals;
 	int ret;
 	int length;
@@ -1013,7 +1013,7 @@ static ssize_t iio_read_channel_info_avail(struct device *dev,
 
 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
 					  &vals, &type, &length,
-					  this_attr->address);
+					  this_attr->iio_attr.address);
 
 	if (ret < 0)
 		return ret;
@@ -1122,7 +1122,7 @@ static ssize_t iio_write_channel_info(struct device *dev,
 				      size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int ret, fract_mult = 100000;
 	int type, dec_scale = 0;
 	int integer, fract = 0;
@@ -1138,7 +1138,7 @@ static ssize_t iio_write_channel_info(struct device *dev,
 	if (indio_dev->info->write_raw_get_fmt) {
 		type = indio_dev->info->write_raw_get_fmt(indio_dev,
 							  this_attr->c,
-							  this_attr->address);
+							  this_attr->iio_attr.address);
 		switch (type) {
 		case IIO_VAL_INT:
 			fract_mult = 0;
@@ -1196,40 +1196,96 @@ static ssize_t iio_write_channel_info(struct device *dev,
 	}
 
 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
-					 integer, fract, this_attr->address);
+					 integer, fract, this_attr->iio_attr.address);
 	if (ret)
 		return ret;
 
 	return len;
 }
 
-static
-int __iio_device_attr_init(struct device *dev,
-			   struct device_attribute *dev_attr,
-			   const char *postfix,
-			   struct iio_chan_spec const *chan,
-			   ssize_t (*readfunc)(struct device *dev,
-					       struct device_attribute *attr,
-					       char *buf),
-			   ssize_t (*writefunc)(struct device *dev,
-						struct device_attribute *attr,
-						const char *buf,
-						size_t len),
-			   enum iio_shared_by shared_by)
+static int iio_chan_attr_name_emit(struct device *dev,
+				   const struct iio_chan_spec *chan,
+				   enum iio_shared_by shared_by,
+				   const char *postfix,
+				   char *buf, size_t len)
 {
-	char prefix[NAME_MAX + 1];
+	struct seq_buf s;
 	ssize_t ret;
 
+	seq_buf_init(&s, buf, len);
+	ret = iio_chan_prefix_seq_buf(dev, chan, shared_by, &s);
+	if (ret < 0)
+		return ret;
+
+	seq_buf_printf(&s, ret ? "_%s" : "%s", postfix);
+
+	return seq_buf_has_overflowed(&s) ? -EOVERFLOW : 0;
+}
+
+/**
+ * iio_chan_attr_prepare() - Prepare the creation of an IIO channel attribute
+ * @ctx: Context to be filled in
+ * @dev: Device used for error reporting
+ * @chan: Specification for the underlying channel
+ * @shared_by: Sharing scope of the attribute
+ * @postfix: Attribute name postfix, appended to the channel prefix
+ * @attr_list: List the attribute is going to be added to
+ *
+ * Builds the full attribute name and makes sure it is not on @attr_list yet,
+ * so that the caller only allocates the attribute if it is going to be used.
+ *
+ * Returns: 0 on success, -EBUSY if the attribute already exists, or another
+ * negative error code on failure.
+ */
+int iio_chan_attr_prepare(struct iio_chan_attr_ctx *ctx, struct device *dev,
+			  const struct iio_chan_spec *chan,
+			  enum iio_shared_by shared_by, const char *postfix,
+			  struct list_head *attr_list)
+{
+	struct iio_chan_attr *t;
+	int ret;
+
+	ret = iio_chan_attr_name_emit(dev, chan, shared_by, postfix,
+				      ctx->name, sizeof(ctx->name));
+	if (ret)
+		return ret;
+
+	list_for_each_entry(t, attr_list, l)
+		if (strcmp(t->iio_attr.dev_attr.attr.name, ctx->name) == 0) {
+			if (shared_by == IIO_SEPARATE)
+				dev_err(dev, "tried to double register : %s\n",
+					ctx->name);
+			return -EBUSY;
+		}
+
+	ctx->chan = chan;
+	ctx->attr_list = attr_list;
+
+	return 0;
+}
+
+/**
+ * iio_chan_attr_add() - Initialize an IIO channel attribute and add it
+ * @ctx: Context filled in by iio_chan_attr_prepare()
+ * @attr: Zeroed attribute allocated by the caller
+ * @readfunc: Show callback, if any
+ * @writefunc: Store callback, if any
+ * @mask: Value for the attribute address
+ *
+ * On success, the attribute is owned by the list it was added to and gets
+ * freed by iio_free_chan_devattr_list(). On failure, the caller still owns it.
+ *
+ * Returns: 0 on success, negative error code on failure.
+ */
+int iio_chan_attr_add(const struct iio_chan_attr_ctx *ctx,
+		      struct iio_chan_attr *attr,
+		      iio_attr_show_t readfunc, iio_attr_store_t writefunc,
+		      u64 mask)
+{
+	struct device_attribute *dev_attr = &attr->iio_attr.dev_attr;
+
 	sysfs_attr_init(&dev_attr->attr);
-
-	ret = __iio_chan_prefix_emit(dev, chan, shared_by, prefix, sizeof(prefix));
-	if (ret < 0)
-		return ret;
-
-	if (ret)
-		dev_attr->attr.name = kasprintf(GFP_KERNEL, "%s_%s", prefix, postfix);
-	else
-		dev_attr->attr.name = kstrdup(postfix, GFP_KERNEL);
+	dev_attr->attr.name = kstrdup(ctx->name, GFP_KERNEL);
 	if (!dev_attr->attr.name)
 		return -ENOMEM;
 
@@ -1243,81 +1299,53 @@ int __iio_device_attr_init(struct device *dev,
 		dev_attr->store = writefunc;
 	}
 
+	attr->c = ctx->chan;
+	attr->iio_attr.address = mask;
+	list_add(&attr->l, ctx->attr_list);
+
 	return 0;
 }
 
-static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
-{
-	kfree(dev_attr->attr.name);
-}
-
-int __iio_add_chan_devattr(const char *postfix,
-			   struct iio_chan_spec const *chan,
-			   ssize_t (*readfunc)(struct device *dev,
-					       struct device_attribute *attr,
-					       char *buf),
-			   ssize_t (*writefunc)(struct device *dev,
-						struct device_attribute *attr,
-						const char *buf,
-						size_t len),
-			   u64 mask,
-			   enum iio_shared_by shared_by,
-			   struct device *dev,
-			   struct iio_buffer *buffer,
-			   struct list_head *attr_list)
+static int iio_device_add_chan_devattr(struct iio_dev *indio_dev,
+				       const char *postfix,
+				       struct iio_chan_spec const *chan,
+				       iio_attr_show_t readfunc,
+				       iio_attr_store_t writefunc,
+				       u64 mask,
+				       enum iio_shared_by shared_by)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+	struct iio_chan_attr *iio_attr;
+	struct iio_chan_attr_ctx ctx;
 	int ret;
-	struct iio_dev_attr *iio_attr, *t;
+
+	ret = iio_chan_attr_prepare(&ctx, &indio_dev->dev, chan, shared_by,
+				    postfix, &iio_dev_opaque->channel_attr_list);
+	if (ret)
+		return ret;
 
 	iio_attr = kzalloc_obj(*iio_attr);
-	if (iio_attr == NULL)
+	if (!iio_attr)
 		return -ENOMEM;
-	ret = __iio_device_attr_init(dev, &iio_attr->dev_attr,
-				     postfix, chan,
-				     readfunc, writefunc, shared_by);
+
+	ret = iio_chan_attr_add(&ctx, iio_attr, readfunc, writefunc, mask);
 	if (ret)
-		goto error_iio_dev_attr_free;
-	iio_attr->c = chan;
-	iio_attr->address = mask;
-	iio_attr->buffer = buffer;
-	list_for_each_entry(t, attr_list, l)
-		if (strcmp(t->dev_attr.attr.name,
-			   iio_attr->dev_attr.attr.name) == 0) {
-			if (shared_by == IIO_SEPARATE)
-				dev_err(dev, "tried to double register : %s\n",
-					t->dev_attr.attr.name);
-			ret = -EBUSY;
-			goto error_device_attr_deinit;
-		}
-	list_add(&iio_attr->l, attr_list);
+		kfree(iio_attr);
 
-	return 0;
-
-error_device_attr_deinit:
-	__iio_device_attr_deinit(&iio_attr->dev_attr);
-error_iio_dev_attr_free:
-	kfree(iio_attr);
 	return ret;
 }
 
 static int iio_device_add_channel_label(struct iio_dev *indio_dev,
 					 struct iio_chan_spec const *chan)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int ret;
 
 	if (!indio_dev->info->read_label && !chan->extend_name)
 		return 0;
 
-	ret = __iio_add_chan_devattr("label",
-				     chan,
-				     &iio_read_channel_label,
-				     NULL,
-				     0,
-				     IIO_SEPARATE,
-				     &indio_dev->dev,
-				     NULL,
-				     &iio_dev_opaque->channel_attr_list);
+	ret = iio_device_add_chan_devattr(indio_dev, "label", chan,
+					  &iio_read_channel_label, NULL,
+					  0, IIO_SEPARATE);
 	if (ret < 0)
 		return ret;
 
@@ -1327,16 +1355,14 @@ static int iio_device_add_channel_label(struct iio_dev *indio_dev,
 static int iio_device_add_channel_parent_attr(struct iio_dev *indio_dev,
 					      struct iio_chan_spec const *chan)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int ret;
 
 	if (!chan->parent)
 		return 0;
 
-	ret = __iio_add_chan_devattr("parent", chan,
-				     &iio_read_channel_parent, NULL,
-				     0, IIO_SEPARATE, &indio_dev->dev, NULL,
-				     &iio_dev_opaque->channel_attr_list);
+	ret = iio_device_add_chan_devattr(indio_dev, "parent", chan,
+					  &iio_read_channel_parent, NULL,
+					  0, IIO_SEPARATE);
 	if (ret < 0)
 		return ret;
 
@@ -1348,21 +1374,17 @@ static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
 					 enum iio_shared_by shared_by,
 					 const unsigned long *infomask)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int i, ret, attrcount = 0;
 
 	for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
 			return -EINVAL;
-		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
-					     chan,
-					     &iio_read_channel_info,
-					     &iio_write_channel_info,
-					     i,
-					     shared_by,
-					     &indio_dev->dev,
-					     NULL,
-					     &iio_dev_opaque->channel_attr_list);
+		ret = iio_device_add_chan_devattr(indio_dev,
+						  iio_chan_info_postfix[i],
+						  chan,
+						  &iio_read_channel_info,
+						  &iio_write_channel_info,
+						  i, shared_by);
 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
 			continue;
 		if (ret < 0)
@@ -1378,7 +1400,6 @@ static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
 					       enum iio_shared_by shared_by,
 					       const unsigned long *infomask)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int i, ret, attrcount = 0;
 	char *avail_postfix;
 
@@ -1391,15 +1412,10 @@ static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
 		if (!avail_postfix)
 			return -ENOMEM;
 
-		ret = __iio_add_chan_devattr(avail_postfix,
-					     chan,
-					     &iio_read_channel_info_avail,
-					     NULL,
-					     i,
-					     shared_by,
-					     &indio_dev->dev,
-					     NULL,
-					     &iio_dev_opaque->channel_attr_list);
+		ret = iio_device_add_chan_devattr(indio_dev, avail_postfix,
+						  chan,
+						  &iio_read_channel_info_avail,
+						  NULL, i, shared_by);
 		kfree(avail_postfix);
 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
 			continue;
@@ -1414,7 +1430,6 @@ static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
 					struct iio_chan_spec const *chan)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int ret, attrcount = 0;
 	const struct iio_chan_spec_ext_info *ext_info;
 
@@ -1490,17 +1505,13 @@ static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
 		unsigned int i = 0;
 
 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
-			ret = __iio_add_chan_devattr(ext_info->name,
-					chan,
-					ext_info->read ?
-					    &iio_read_channel_ext_info : NULL,
-					ext_info->write ?
-					    &iio_write_channel_ext_info : NULL,
-					i,
-					ext_info->shared,
-					&indio_dev->dev,
-					NULL,
-					&iio_dev_opaque->channel_attr_list);
+			ret = iio_device_add_chan_devattr(indio_dev,
+							  ext_info->name, chan,
+							  ext_info->read ?
+							  &iio_read_channel_ext_info : NULL,
+							  ext_info->write ?
+							  &iio_write_channel_ext_info : NULL,
+							  i, ext_info->shared);
 			i++;
 			if (ret == -EBUSY && ext_info->shared)
 				continue;
@@ -1524,10 +1535,10 @@ static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  */
 void iio_free_chan_devattr_list(struct list_head *attr_list)
 {
-	struct iio_dev_attr *p, *n;
+	struct iio_chan_attr *p, *n;
 
 	list_for_each_entry_safe(p, n, attr_list, l) {
-		kfree_const(p->dev_attr.attr.name);
+		kfree_const(p->iio_attr.dev_attr.attr.name);
 		list_del(&p->l);
 		kfree(p);
 	}
@@ -1649,7 +1660,7 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
-	struct iio_dev_attr *p;
+	struct iio_chan_attr *p;
 	struct attribute **attr, *clk = NULL;
 
 	/* First count elements in any existing group */
@@ -1708,7 +1719,7 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
 	attrn = attrcount_orig;
 	/* Add all elements from the list. */
 	list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
-		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
+		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->iio_attr.dev_attr.attr;
 	if (indio_dev->name)
 		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
 	if (indio_dev->label)
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index e6730f52262a..e72b665cc79b 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -258,19 +258,19 @@ static const char * const iio_ev_info_text[] = {
 	[IIO_EV_INFO_SCALE] = "scale",
 };
 
-static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
+static enum iio_event_direction iio_ev_attr_dir(struct iio_chan_attr *attr)
 {
-	return attr->c->event_spec[attr->address & 0xffff].dir;
+	return attr->c->event_spec[attr->iio_attr.address & 0xffff].dir;
 }
 
-static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
+static enum iio_event_type iio_ev_attr_type(struct iio_chan_attr *attr)
 {
-	return attr->c->event_spec[attr->address & 0xffff].type;
+	return attr->c->event_spec[attr->iio_attr.address & 0xffff].type;
 }
 
-static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
+static enum iio_event_info iio_ev_attr_info(struct iio_chan_attr *attr)
 {
-	return (attr->address >> 16) & 0xffff;
+	return (attr->iio_attr.address >> 16) & 0xffff;
 }
 
 static ssize_t iio_ev_state_store(struct device *dev,
@@ -279,7 +279,7 @@ static ssize_t iio_ev_state_store(struct device *dev,
 				  size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int ret;
 	bool val;
 
@@ -302,7 +302,7 @@ static ssize_t iio_ev_state_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int val;
 
 	if (!indio_dev->info->read_event_config)
@@ -322,7 +322,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int val, val2, val_arr[2];
 	int ret;
 
@@ -346,7 +346,7 @@ static ssize_t iio_ev_value_store(struct device *dev,
 				  size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 	int val, val2;
 	int ret;
 
@@ -371,7 +371,7 @@ static ssize_t iio_ev_label_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
 
 	if (indio_dev->info->read_event_label)
 		return indio_dev->info->read_event_label(indio_dev,
@@ -381,12 +381,41 @@ static ssize_t iio_ev_label_show(struct device *dev,
 	return -EINVAL;
 }
 
+static int iio_device_add_event_attr(struct iio_dev *indio_dev,
+				     const char *postfix,
+				     const struct iio_chan_spec *chan,
+				     iio_attr_show_t show,
+				     iio_attr_store_t store,
+				     u64 mask,
+				     enum iio_shared_by shared_by)
+{
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+	struct iio_chan_attr *ev_attr;
+	struct iio_chan_attr_ctx ctx;
+	int ret;
+
+	ret = iio_chan_attr_prepare(&ctx, &indio_dev->dev, chan, shared_by,
+				    postfix,
+				    &iio_dev_opaque->event_interface->dev_attr_list);
+	if (ret)
+		return ret;
+
+	ev_attr = kzalloc_obj(*ev_attr);
+	if (!ev_attr)
+		return -ENOMEM;
+
+	ret = iio_chan_attr_add(&ctx, ev_attr, show, store, mask);
+	if (ret)
+		kfree(ev_attr);
+
+	return ret;
+}
+
 static int iio_device_add_event(struct iio_dev *indio_dev,
 	const struct iio_chan_spec *chan, unsigned int spec_index,
 	enum iio_event_type type, enum iio_event_direction dir,
 	enum iio_shared_by shared_by, const unsigned long *mask)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
 		char *buf);
 	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
@@ -419,10 +448,10 @@ static int iio_device_add_event(struct iio_dev *indio_dev,
 			store = iio_ev_value_store;
 		}
 
-		ret = __iio_add_chan_devattr(postfix, chan, show, store,
-			 (i << 16) | spec_index, shared_by, &indio_dev->dev,
-			 NULL,
-			&iio_dev_opaque->event_interface->dev_attr_list);
+		ret = iio_device_add_event_attr(indio_dev, postfix, chan,
+						show, store,
+						(i << 16) | spec_index,
+						shared_by);
 		kfree(postfix);
 
 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
@@ -443,7 +472,6 @@ static int iio_device_add_event_label(struct iio_dev *indio_dev,
 				      enum iio_event_type type,
 				      enum iio_event_direction dir)
 {
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	char *postfix;
 	int ret;
 
@@ -460,9 +488,9 @@ static int iio_device_add_event_label(struct iio_dev *indio_dev,
 	if (postfix == NULL)
 		return -ENOMEM;
 
-	ret = __iio_add_chan_devattr(postfix, chan, &iio_ev_label_show, NULL,
-				spec_index, IIO_SEPARATE, &indio_dev->dev, NULL,
-				&iio_dev_opaque->event_interface->dev_attr_list);
+	ret = iio_device_add_event_attr(indio_dev, postfix, chan,
+					&iio_ev_label_show, NULL, spec_index,
+					IIO_SEPARATE);
 
 	kfree(postfix);
 
@@ -575,7 +603,7 @@ int iio_device_register_eventset(struct iio_dev *indio_dev)
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	struct iio_event_interface *ev_int;
-	struct iio_dev_attr *p;
+	struct iio_chan_attr *p;
 	int ret = 0, attrcount_orig = 0, attrcount, attrn;
 	struct attribute **attr;
 
@@ -619,7 +647,7 @@ int iio_device_register_eventset(struct iio_dev *indio_dev)
 	attrn = attrcount_orig;
 	/* Add all elements from the list. */
 	list_for_each_entry(p, &ev_int->dev_attr_list, l)
-		ev_int->group.attrs[attrn++] = &p->dev_attr.attr;
+		ev_int->group.attrs[attrn++] = &p->iio_attr.dev_attr.attr;
 
 	ret = iio_device_register_sysfs_group(indio_dev, &ev_int->group);
 	if (ret)
diff --git a/include/linux/iio/sysfs.h b/include/linux/iio/sysfs.h
index de5bb125815c..26c88767afe6 100644
--- a/include/linux/iio/sysfs.h
+++ b/include/linux/iio/sysfs.h
@@ -10,21 +10,16 @@
 #define _INDUSTRIAL_IO_SYSFS_H_
 
 struct iio_buffer;
-struct iio_chan_spec;
 
 /**
  * struct iio_dev_attr - iio specific device attribute
  * @dev_attr:	underlying device attribute
  * @address:	associated register address
- * @l:		list head for maintaining list of dynamically created attrs
- * @c:		specification for the underlying channel
  * @buffer:	the IIO buffer to which this attribute belongs to (if any)
  */
 struct iio_dev_attr {
 	struct device_attribute dev_attr;
 	u64 address;
-	struct list_head l;
-	struct iio_chan_spec const *c;
 	struct iio_buffer *buffer;
 };
 

-- 
2.43.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 4/5] iio: event: add struct iio_event_attr
  2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-27  9:47 ` [PATCH v2 3/5] iio: core: add struct iio_chan_attr and split attribute creation Rodrigo Alencar via B4 Relay
@ 2026-11-27  9:47 ` Rodrigo Alencar via B4 Relay
  2026-11-27  9:47 ` [PATCH v2 5/5] iio: buffer: add struct iio_buffer_attr Rodrigo Alencar via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

From: Rodrigo Alencar <rodrigo.alencar@analog.com>

Event attributes encode both the index of their struct iio_event_spec
and the event info into the attribute address, as
(info << 16) | spec_index, and look the event spec up through the
channel on every access.

Add struct iio_event_attr, extending struct iio_chan_attr with a pointer
to the event spec. The address now only holds the event info, and the
event type and direction are read directly from the spec.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
 drivers/iio/industrialio-event.c | 63 +++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 23 deletions(-)

diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index e72b665cc79b..e6a404642417 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -258,19 +258,35 @@ static const char * const iio_ev_info_text[] = {
 	[IIO_EV_INFO_SCALE] = "scale",
 };
 
-static enum iio_event_direction iio_ev_attr_dir(struct iio_chan_attr *attr)
+/**
+ * struct iio_event_attr - IIO event attribute
+ * @chan_attr:	underlying IIO channel attribute, its address holds the
+ *		event info
+ * @spec:	event specification the attribute belongs to
+ */
+struct iio_event_attr {
+	struct iio_chan_attr chan_attr;
+	const struct iio_event_spec *spec;
+};
+
+static_assert(offsetof(struct iio_event_attr, chan_attr) == 0);
+
+#define to_iio_event_attr(_dev_attr)					\
+	container_of(to_iio_chan_attr(_dev_attr), struct iio_event_attr, chan_attr)
+
+static enum iio_event_direction iio_ev_attr_dir(struct iio_event_attr *attr)
 {
-	return attr->c->event_spec[attr->iio_attr.address & 0xffff].dir;
+	return attr->spec->dir;
 }
 
-static enum iio_event_type iio_ev_attr_type(struct iio_chan_attr *attr)
+static enum iio_event_type iio_ev_attr_type(struct iio_event_attr *attr)
 {
-	return attr->c->event_spec[attr->iio_attr.address & 0xffff].type;
+	return attr->spec->type;
 }
 
-static enum iio_event_info iio_ev_attr_info(struct iio_chan_attr *attr)
+static enum iio_event_info iio_ev_attr_info(struct iio_event_attr *attr)
 {
-	return (attr->iio_attr.address >> 16) & 0xffff;
+	return attr->chan_attr.iio_attr.address;
 }
 
 static ssize_t iio_ev_state_store(struct device *dev,
@@ -279,7 +295,7 @@ static ssize_t iio_ev_state_store(struct device *dev,
 				  size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
+	struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 	int ret;
 	bool val;
 
@@ -291,7 +307,7 @@ static ssize_t iio_ev_state_store(struct device *dev,
 		return -EINVAL;
 
 	ret = indio_dev->info->write_event_config(indio_dev,
-		this_attr->c, iio_ev_attr_type(this_attr),
+		this_attr->chan_attr.c, iio_ev_attr_type(this_attr),
 		iio_ev_attr_dir(this_attr), val);
 
 	return (ret < 0) ? ret : len;
@@ -302,14 +318,14 @@ static ssize_t iio_ev_state_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
+	struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 	int val;
 
 	if (!indio_dev->info->read_event_config)
 		return -EINVAL;
 
 	val = indio_dev->info->read_event_config(indio_dev,
-		this_attr->c, iio_ev_attr_type(this_attr),
+		this_attr->chan_attr.c, iio_ev_attr_type(this_attr),
 		iio_ev_attr_dir(this_attr));
 	if (val < 0)
 		return val;
@@ -322,7 +338,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
+	struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 	int val, val2, val_arr[2];
 	int ret;
 
@@ -330,7 +346,7 @@ static ssize_t iio_ev_value_show(struct device *dev,
 		return -EINVAL;
 
 	ret = indio_dev->info->read_event_value(indio_dev,
-		this_attr->c, iio_ev_attr_type(this_attr),
+		this_attr->chan_attr.c, iio_ev_attr_type(this_attr),
 		iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
 		&val, &val2);
 	if (ret < 0)
@@ -346,7 +362,7 @@ static ssize_t iio_ev_value_store(struct device *dev,
 				  size_t len)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
+	struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 	int val, val2;
 	int ret;
 
@@ -357,7 +373,7 @@ static ssize_t iio_ev_value_store(struct device *dev,
 	if (ret)
 		return ret;
 	ret = indio_dev->info->write_event_value(indio_dev,
-		this_attr->c, iio_ev_attr_type(this_attr),
+		this_attr->chan_attr.c, iio_ev_attr_type(this_attr),
 		iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
 		val, val2);
 	if (ret < 0)
@@ -371,11 +387,11 @@ static ssize_t iio_ev_label_show(struct device *dev,
 				 char *buf)
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_chan_attr *this_attr = to_iio_chan_attr(attr);
+	struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 
 	if (indio_dev->info->read_event_label)
 		return indio_dev->info->read_event_label(indio_dev,
-				 this_attr->c, iio_ev_attr_type(this_attr),
+				 this_attr->chan_attr.c, iio_ev_attr_type(this_attr),
 				 iio_ev_attr_dir(this_attr), buf);
 
 	return -EINVAL;
@@ -384,13 +400,14 @@ static ssize_t iio_ev_label_show(struct device *dev,
 static int iio_device_add_event_attr(struct iio_dev *indio_dev,
 				     const char *postfix,
 				     const struct iio_chan_spec *chan,
+				     unsigned int spec_index,
 				     iio_attr_show_t show,
 				     iio_attr_store_t store,
-				     u64 mask,
+				     enum iio_event_info info,
 				     enum iio_shared_by shared_by)
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-	struct iio_chan_attr *ev_attr;
+	struct iio_event_attr *ev_attr;
 	struct iio_chan_attr_ctx ctx;
 	int ret;
 
@@ -404,7 +421,8 @@ static int iio_device_add_event_attr(struct iio_dev *indio_dev,
 	if (!ev_attr)
 		return -ENOMEM;
 
-	ret = iio_chan_attr_add(&ctx, ev_attr, show, store, mask);
+	ev_attr->spec = &chan->event_spec[spec_index];
+	ret = iio_chan_attr_add(&ctx, &ev_attr->chan_attr, show, store, info);
 	if (ret)
 		kfree(ev_attr);
 
@@ -449,8 +467,7 @@ static int iio_device_add_event(struct iio_dev *indio_dev,
 		}
 
 		ret = iio_device_add_event_attr(indio_dev, postfix, chan,
-						show, store,
-						(i << 16) | spec_index,
+						spec_index, show, store, i,
 						shared_by);
 		kfree(postfix);
 
@@ -488,8 +505,8 @@ static int iio_device_add_event_label(struct iio_dev *indio_dev,
 	if (postfix == NULL)
 		return -ENOMEM;
 
-	ret = iio_device_add_event_attr(indio_dev, postfix, chan,
-					&iio_ev_label_show, NULL, spec_index,
+	ret = iio_device_add_event_attr(indio_dev, postfix, chan, spec_index,
+					&iio_ev_label_show, NULL, 0,
 					IIO_SEPARATE);
 
 	kfree(postfix);

-- 
2.43.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 5/5] iio: buffer: add struct iio_buffer_attr
  2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
                   ` (3 preceding siblings ...)
  2026-11-27  9:47 ` [PATCH v2 4/5] iio: event: add struct iio_event_attr Rodrigo Alencar via B4 Relay
@ 2026-11-27  9:47 ` Rodrigo Alencar via B4 Relay
  4 siblings, 0 replies; 6+ messages in thread
From: Rodrigo Alencar via B4 Relay @ 2026-11-27  9:47 UTC (permalink / raw)
  To: linux-kernel, linux-iio
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko

From: Rodrigo Alencar <rodrigo.alencar@analog.com>

Buffer attributes find their buffer through the buffer field of
struct iio_dev_attr, which is unused by every other attribute.

Add struct iio_buffer_attr, extending struct iio_chan_attr with the
buffer the attribute belongs to, and use it both for the scan element
attributes and for the buffer attributes wrapped by the buffer core.
iio_attr_to_buffer() now resolves the buffer through it.

The buffer pointer was the last core-only field of struct iio_dev_attr,
so remove it. On 64-bit, without lockdep, struct iio_dev_attr is now 40
bytes instead of 72, which every attribute declared through
IIO_DEVICE_ATTR*() and friends benefits from.

Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
 drivers/iio/industrialio-buffer.c | 57 +++++++++++++++++++++++++--------------
 include/linux/iio/sysfs.h         |  4 ---
 2 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 52025d60052b..16629344410e 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -69,6 +69,22 @@ static const char * const iio_endian_prefix[] = {
 	[IIO_LE] = "le",
 };
 
+/**
+ * struct iio_buffer_attr - IIO buffer attribute
+ * @chan_attr:	underlying IIO channel attribute (channel is NULL for buffer
+ *		wide attributes)
+ * @buffer:	the IIO buffer to which this attribute belongs to
+ */
+struct iio_buffer_attr {
+	struct iio_chan_attr chan_attr;
+	struct iio_buffer *buffer;
+};
+
+static_assert(offsetof(struct iio_buffer_attr, chan_attr) == 0);
+
+#define to_iio_buffer_attr(_dev_attr)					\
+	container_of(to_iio_chan_attr(_dev_attr), struct iio_buffer_attr, chan_attr)
+
 /**
  * iio_attr_to_buffer() - Get the IIO buffer a buffer attribute belongs to
  * @attr: Device attribute of the buffer, as passed to its show/store callbacks
@@ -80,7 +96,7 @@ static const char * const iio_endian_prefix[] = {
  */
 struct iio_buffer *iio_attr_to_buffer(struct device_attribute *attr)
 {
-	return to_iio_dev_attr(attr)->buffer;
+	return to_iio_buffer_attr(attr)->buffer;
 }
 EXPORT_SYMBOL_GPL(iio_attr_to_buffer);
 
@@ -459,7 +475,7 @@ static ssize_t iio_scan_el_show(struct device *dev,
 				char *buf)
 {
 	int ret;
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	/* Ensure ret is 0 or 1. */
 	ret = !!test_bit(to_iio_dev_attr(attr)->address,
@@ -591,7 +607,7 @@ static ssize_t iio_scan_el_store(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-	struct iio_buffer *buffer = this_attr->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	ret = kstrtobool(buf, &state);
 	if (ret < 0)
@@ -622,7 +638,7 @@ static ssize_t iio_scan_el_ts_show(struct device *dev,
 				   struct device_attribute *attr,
 				   char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	return sysfs_emit(buf, "%d\n", buffer->scan_timestamp);
 }
@@ -635,7 +651,7 @@ static ssize_t iio_scan_el_ts_store(struct device *dev,
 	int ret;
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 	bool state;
 
 	ret = kstrtobool(buf, &state);
@@ -659,7 +675,7 @@ static int iio_buffer_add_chan_devattr(struct iio_dev *indio_dev,
 				       iio_attr_store_t writefunc,
 				       u64 mask)
 {
-	struct iio_chan_attr *buf_attr;
+	struct iio_buffer_attr *buf_attr;
 	struct iio_chan_attr_ctx ctx;
 	int ret;
 
@@ -672,8 +688,9 @@ static int iio_buffer_add_chan_devattr(struct iio_dev *indio_dev,
 	if (!buf_attr)
 		return -ENOMEM;
 
-	buf_attr->iio_attr.buffer = buffer;
-	ret = iio_chan_attr_add(&ctx, buf_attr, readfunc, writefunc, mask);
+	buf_attr->buffer = buffer;
+	ret = iio_chan_attr_add(&ctx, &buf_attr->chan_attr, readfunc, writefunc,
+				mask);
 	if (ret)
 		kfree(buf_attr);
 
@@ -716,7 +733,7 @@ static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
 static ssize_t length_show(struct device *dev, struct device_attribute *attr,
 			   char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	return sysfs_emit(buf, "%d\n", buffer->length);
 }
@@ -726,7 +743,7 @@ static ssize_t length_store(struct device *dev, struct device_attribute *attr,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 	unsigned int val;
 	int ret;
 
@@ -752,7 +769,7 @@ static ssize_t length_store(struct device *dev, struct device_attribute *attr,
 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
 			   char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	return sysfs_emit(buf, "%d\n", iio_buffer_is_active(buffer));
 }
@@ -1412,7 +1429,7 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
 	bool requested_state;
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 	bool inlist;
 
 	ret = kstrtobool(buf, &requested_state);
@@ -1440,7 +1457,7 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
 static ssize_t watermark_show(struct device *dev, struct device_attribute *attr,
 			      char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	return sysfs_emit(buf, "%u\n", buffer->watermark);
 }
@@ -1451,7 +1468,7 @@ static ssize_t watermark_store(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 	unsigned int val;
 	int ret;
 
@@ -1477,7 +1494,7 @@ static ssize_t watermark_store(struct device *dev,
 static ssize_t data_available_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer));
 }
@@ -1486,7 +1503,7 @@ static ssize_t direction_show(struct device *dev,
 			      struct device_attribute *attr,
 			      char *buf)
 {
-	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
+	struct iio_buffer *buffer = to_iio_buffer_attr(attr)->buffer;
 
 	switch (buffer->direction) {
 	case IIO_BUFFER_DIRECTION_IN:
@@ -1525,15 +1542,15 @@ static struct attribute *iio_buffer_attrs[] = {
 static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
 					      struct attribute *attr)
 {
-	struct iio_chan_attr *buf_attr __free(kfree) = kzalloc_obj(*buf_attr);
+	struct iio_buffer_attr *buf_attr __free(kfree) = kzalloc_obj(*buf_attr);
 	struct device_attribute *dattr = to_dev_attr(attr);
 	struct iio_dev_attr *iio_attr;
 
 	if (!buf_attr)
 		return NULL;
 
-	iio_attr = &buf_attr->iio_attr;
-	iio_attr->buffer = buffer;
+	buf_attr->buffer = buffer;
+	iio_attr = &buf_attr->chan_attr.iio_attr;
 	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
 	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
 	if (!iio_attr->dev_attr.attr.name)
@@ -1541,7 +1558,7 @@ static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
 
 	sysfs_attr_init(&iio_attr->dev_attr.attr);
 
-	list_add(&no_free_ptr(buf_attr)->l, &buffer->buffer_attr_list);
+	list_add(&no_free_ptr(buf_attr)->chan_attr.l, &buffer->buffer_attr_list);
 
 	return &iio_attr->dev_attr.attr;
 }
diff --git a/include/linux/iio/sysfs.h b/include/linux/iio/sysfs.h
index 26c88767afe6..1e4122b8c13a 100644
--- a/include/linux/iio/sysfs.h
+++ b/include/linux/iio/sysfs.h
@@ -9,18 +9,14 @@
 #ifndef _INDUSTRIAL_IO_SYSFS_H_
 #define _INDUSTRIAL_IO_SYSFS_H_
 
-struct iio_buffer;
-
 /**
  * struct iio_dev_attr - iio specific device attribute
  * @dev_attr:	underlying device attribute
  * @address:	associated register address
- * @buffer:	the IIO buffer to which this attribute belongs to (if any)
  */
 struct iio_dev_attr {
 	struct device_attribute dev_attr;
 	u64 address;
-	struct iio_buffer *buffer;
 };
 
 #define to_iio_dev_attr(_dev_attr)				\

-- 
2.43.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-27  9:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  9:47 [PATCH v2 0/5] iio: split struct iio_dev_attr into typed core attributes Rodrigo Alencar via B4 Relay
2026-11-27  9:47 ` [PATCH v2 1/5] iio: core: emit channel prefixes into a struct seq_buf Rodrigo Alencar via B4 Relay
2026-11-27  9:47 ` [PATCH v2 2/5] iio: buffer: add iio_attr_to_buffer() helper Rodrigo Alencar via B4 Relay
2026-11-27  9:47 ` [PATCH v2 3/5] iio: core: add struct iio_chan_attr and split attribute creation Rodrigo Alencar via B4 Relay
2026-11-27  9:47 ` [PATCH v2 4/5] iio: event: add struct iio_event_attr Rodrigo Alencar via B4 Relay
2026-11-27  9:47 ` [PATCH v2 5/5] iio: buffer: add struct iio_buffer_attr Rodrigo Alencar via B4 Relay

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®