[PATCH v8 19/22] counter: Implement extension*_name sysfs attributes

From: William Breathitt Gray
Date: Fri Feb 12 2021 - 07:21:12 EST


The Generic Counter chrdev interface expects users to supply extension
IDs in order to select extensions for requests. In order for users to
know what extension ID belongs to which extension this information must
be exposed. The extension*_name attribute provides a way for users to
discover what extension ID belongs to which extension by reading the
respective extension name for an extension ID.

Cc: David Lechner <david@xxxxxxxxxxxxxx>
Cc: Gwendal Grignou <gwendal@xxxxxxxxxxxx>
Cc: Dan Carpenter <dan.carpenter@xxxxxxxxxx>
Signed-off-by: William Breathitt Gray <vilhelm.gray@xxxxxxxxx>
---
Documentation/ABI/testing/sysfs-bus-counter | 9 ++++
drivers/counter/counter-sysfs.c | 51 +++++++++++++++++----
2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-bus-counter b/Documentation/ABI/testing/sysfs-bus-counter
index 6353f0a2f8f8..847e96f19d19 100644
--- a/Documentation/ABI/testing/sysfs-bus-counter
+++ b/Documentation/ABI/testing/sysfs-bus-counter
@@ -100,6 +100,15 @@ Description:
Read-only attribute that indicates whether excessive noise is
present at the channel Y counter inputs.

+What: /sys/bus/counter/devices/counterX/countY/extensionZ_name
+What: /sys/bus/counter/devices/counterX/extensionZ_name
+What: /sys/bus/counter/devices/counterX/signalY/extensionZ_name
+KernelVersion: 5.13
+Contact: linux-iio@xxxxxxxxxxxxxxx
+Description:
+ Read-only attribute that indicates the component name of
+ Extension Z.
+
What: /sys/bus/counter/devices/counterX/countY/function
KernelVersion: 5.2
Contact: linux-iio@xxxxxxxxxxxxxxx
diff --git a/drivers/counter/counter-sysfs.c b/drivers/counter/counter-sysfs.c
index 52513a213cc5..0cb3dba950bc 100644
--- a/drivers/counter/counter-sysfs.c
+++ b/drivers/counter/counter-sysfs.c
@@ -494,6 +494,7 @@ static ssize_t counter_comp_name_show(struct device *dev,

static int counter_name_attr_create(struct device *const dev,
struct counter_attribute_group *const group,
+ const char *const attr_name,
const char *const name)
{
struct counter_attribute *counter_attr;
@@ -508,7 +509,7 @@ static int counter_name_attr_create(struct device *const dev,

/* Configure device attribute */
sysfs_attr_init(&counter_attr->dev_attr.attr);
- counter_attr->dev_attr.attr.name = "name";
+ counter_attr->dev_attr.attr.name = attr_name;
counter_attr->dev_attr.attr.mode = 0444;
counter_attr->dev_attr.show = counter_comp_name_show;

@@ -519,6 +520,18 @@ static int counter_name_attr_create(struct device *const dev,
return 0;
}

+static int counter_ext_name_attr_create(struct device *const dev,
+ struct counter_attribute_group *const group, const size_t i,
+ const char *const name)
+{
+ const char *attr_name;
+
+ attr_name = devm_kasprintf(dev, GFP_KERNEL, "extension%zu_name", i);
+ if (!attr_name)
+ return -ENOMEM;
+
+ return counter_name_attr_create(dev, group, attr_name, name);
+}

static struct counter_comp counter_signal_comp = {
.type = COUNTER_COMP_SIGNAL_LEVEL,
@@ -534,6 +547,7 @@ static int counter_signal_attrs_create(struct counter_device *const counter,
int err;
struct counter_comp comp;
size_t i;
+ struct counter_comp *ext;

/* Create main Signal attribute */
comp = counter_signal_comp;
@@ -543,14 +557,19 @@ static int counter_signal_attrs_create(struct counter_device *const counter,
return err;

/* Create Signal name attribute */
- err = counter_name_attr_create(dev, group, signal->name);
+ err = counter_name_attr_create(dev, group, "name", signal->name);
if (err < 0)
return err;

/* Create an attribute for each extension */
for (i = 0; i < signal->num_ext; i++) {
- err = counter_attr_create(dev, group, signal->ext + i, scope,
- signal);
+ ext = signal->ext + i;
+
+ err = counter_attr_create(dev, group, ext, scope, signal);
+ if (err < 0)
+ return err;
+
+ err = counter_ext_name_attr_create(dev, group, i, ext->name);
if (err < 0)
return err;
}
@@ -636,6 +655,7 @@ static int counter_count_attrs_create(struct counter_device *const counter,
int err;
struct counter_comp comp;
size_t i;
+ struct counter_comp *ext;

/* Create main Count attribute */
comp = counter_count_comp;
@@ -646,7 +666,7 @@ static int counter_count_attrs_create(struct counter_device *const counter,
return err;

/* Create Count name attribute */
- err = counter_name_attr_create(dev, group, count->name);
+ err = counter_name_attr_create(dev, group, "name", count->name);
if (err < 0)
return err;

@@ -660,8 +680,13 @@ static int counter_count_attrs_create(struct counter_device *const counter,

/* Create an attribute for each extension */
for (i = 0; i < count->num_ext; i++) {
- err = counter_attr_create(dev, group, count->ext + i, scope,
- count);
+ ext = count->ext + i;
+
+ err = counter_attr_create(dev, group, ext, scope, count);
+ if (err < 0)
+ return err;
+
+ err = counter_ext_name_attr_create(dev, group, i, ext->name);
if (err < 0)
return err;
}
@@ -725,6 +750,7 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,
struct device *const dev = &counter->dev;
int err;
size_t i;
+ struct counter_comp *ext;

/* Add Signals sysfs attributes */
err = counter_sysfs_signals_add(counter, group);
@@ -739,7 +765,7 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,
group += counter->num_counts;

/* Create name attribute */
- err = counter_name_attr_create(dev, group, counter->name);
+ err = counter_name_attr_create(dev, group, "name", counter->name);
if (err < 0)
return err;

@@ -757,8 +783,13 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,

/* Create an attribute for each extension */
for (i = 0; i < counter->num_ext; i++) {
- err = counter_attr_create(dev, group, counter->ext + i, scope,
- NULL);
+ ext = counter->ext + i;
+
+ err = counter_attr_create(dev, group, ext, scope, NULL);
+ if (err < 0)
+ return err;
+
+ err = counter_ext_name_attr_create(dev, group, i, ext->name);
if (err < 0)
return err;
}
--
2.30.0