linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] hwmon: iio: Add labels
@ 2024-06-20 21:13 Sean Anderson
  2024-06-20 21:13 ` [PATCH 1/2] iio: Add iio_read_channel_label to inkern API Sean Anderson
  2024-06-20 21:13 ` [PATCH 2/2] hwmon: iio: Add labels from IIO channels Sean Anderson
  0 siblings, 2 replies; 8+ messages in thread
From: Sean Anderson @ 2024-06-20 21:13 UTC (permalink / raw)
  To: Jonathan Cameron, Jean Delvare, Guenter Roeck, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel, Sean Anderson

Add support for using IIO channel labels for HWMON labels.


Sean Anderson (2):
  iio: Add iio_read_channel_label to inkern API
  hwmon: iio: Add labels from IIO channels

 drivers/hwmon/iio_hwmon.c       | 33 ++++++++++++++++++++++++++++++---
 drivers/iio/iio_core.h          |  4 ++++
 drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
 drivers/iio/inkern.c            |  6 ++++++
 include/linux/iio/consumer.h    | 10 ++++++++++
 5 files changed, 64 insertions(+), 12 deletions(-)

-- 
2.35.1.1320.gc452695387.dirty


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

* [PATCH 1/2] iio: Add iio_read_channel_label to inkern API
  2024-06-20 21:13 [PATCH 0/2] hwmon: iio: Add labels Sean Anderson
@ 2024-06-20 21:13 ` Sean Anderson
  2024-06-23 12:03   ` Jonathan Cameron
  2024-06-20 21:13 ` [PATCH 2/2] hwmon: iio: Add labels from IIO channels Sean Anderson
  1 sibling, 1 reply; 8+ messages in thread
From: Sean Anderson @ 2024-06-20 21:13 UTC (permalink / raw)
  To: Jonathan Cameron, Jean Delvare, Guenter Roeck, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel, Sean Anderson

It can be convenient for other in-kernel drivers to reuse IIO channel
labels. Export the iio_read_channel_label function to allow this. The
signature is different depending on where we are calling it from, so
the meat is moved to do_iio_read_channel_label.

Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
---

 drivers/iio/iio_core.h          |  4 ++++
 drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
 drivers/iio/inkern.c            |  6 ++++++
 include/linux/iio/consumer.h    | 10 ++++++++++
 4 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 1a38b1915e7a..b7d5f4f0fada 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -34,6 +34,10 @@ void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
 				       struct iio_ioctl_handler *h);
 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h);
 
+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,
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 2f185b386949..0f6cda7ffe45 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -727,22 +727,27 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
 }
 EXPORT_SYMBOL_GPL(iio_format_value);
 
-static ssize_t iio_read_channel_label(struct device *dev,
-				      struct device_attribute *attr,
-				      char *buf)
+ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
+				  const struct iio_chan_spec *c,
+				  char *buf)
 {
-	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-
 	if (indio_dev->info->read_label)
-		return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
+		return indio_dev->info->read_label(indio_dev, c, buf);
 
-	if (this_attr->c->extend_name)
-		return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
+	if (c->extend_name)
+		return sysfs_emit(buf, "%s\n", c->extend_name);
 
 	return -EINVAL;
 }
 
+static ssize_t iio_read_channel_label(struct device *dev,
+				      struct device_attribute *attr,
+				      char *buf)
+{
+	return do_iio_read_channel_label(dev_to_iio_dev(dev),
+					 to_iio_dev_attr(attr)->c, buf);
+}
+
 static ssize_t iio_read_channel_info(struct device *dev,
 				     struct device_attribute *attr,
 				     char *buf)
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 39cf26d69d17..9f484c94bc6e 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -1010,3 +1010,9 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 			       chan->channel, buf, len);
 }
 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
+
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
+{
+	return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
+}
+EXPORT_SYMBOL_GPL(iio_read_channel_label);
diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
index e9910b41d48e..333d1d8ccb37 100644
--- a/include/linux/iio/consumer.h
+++ b/include/linux/iio/consumer.h
@@ -441,4 +441,14 @@ ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
 				   const char *buf, size_t len);
 
+/**
+ * iio_read_channel_label() - read label for a given channel
+ * @chan:		The channel being queried.
+ * @buf:		Where to store the attribute value. Assumed to hold
+ *			at least PAGE_SIZE bytes.
+ *
+ * Returns the number of bytes written to buf, or an error code.
+ */
+ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf);
+
 #endif
-- 
2.35.1.1320.gc452695387.dirty


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

* [PATCH 2/2] hwmon: iio: Add labels from IIO channels
  2024-06-20 21:13 [PATCH 0/2] hwmon: iio: Add labels Sean Anderson
  2024-06-20 21:13 ` [PATCH 1/2] iio: Add iio_read_channel_label to inkern API Sean Anderson
@ 2024-06-20 21:13 ` Sean Anderson
  2024-06-21 15:08   ` Guenter Roeck
  1 sibling, 1 reply; 8+ messages in thread
From: Sean Anderson @ 2024-06-20 21:13 UTC (permalink / raw)
  To: Jonathan Cameron, Jean Delvare, Guenter Roeck, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel, Sean Anderson

Add labels from IIO channels to our channels. This allows userspace to
display more meaningful names instead of "in0" or "temp5".

Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
---

 drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c
index 4c8a80847891..588b64c18e63 100644
--- a/drivers/hwmon/iio_hwmon.c
+++ b/drivers/hwmon/iio_hwmon.c
@@ -33,6 +33,17 @@ struct iio_hwmon_state {
 	struct attribute **attrs;
 };
 
+static ssize_t iio_hwmon_read_label(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
+	struct iio_hwmon_state *state = dev_get_drvdata(dev);
+	struct iio_channel *chan = &state->channels[sattr->index];
+
+	return iio_read_channel_label(chan, buf);
+}
+
 /*
  * Assumes that IIO and hwmon operate in the same base units.
  * This is supposed to be true, but needs verification for
@@ -90,12 +101,12 @@ static int iio_hwmon_probe(struct platform_device *pdev)
 
 	st->channels = channels;
 
-	/* count how many attributes we have */
+	/* count how many channels we have */
 	while (st->channels[st->num_channels].indio_dev)
 		st->num_channels++;
 
 	st->attrs = devm_kcalloc(dev,
-				 st->num_channels + 1, sizeof(*st->attrs),
+				 2 * st->num_channels + 1, sizeof(*st->attrs),
 				 GFP_KERNEL);
 	if (st->attrs == NULL)
 		return -ENOMEM;
@@ -147,7 +158,23 @@ static int iio_hwmon_probe(struct platform_device *pdev)
 		a->dev_attr.show = iio_hwmon_read_val;
 		a->dev_attr.attr.mode = 0444;
 		a->index = i;
-		st->attrs[i] = &a->dev_attr.attr;
+		st->attrs[2 * i] = &a->dev_attr.attr;
+
+		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
+		if (a == NULL)
+			return -ENOMEM;
+
+		sysfs_attr_init(&a->dev_attr.attr);
+		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						       "%s%d_label",
+						       prefix, n);
+		if (!a->dev_attr.attr.name)
+			return -ENOMEM;
+
+		a->dev_attr.show = iio_hwmon_read_label;
+		a->dev_attr.attr.mode = 0444;
+		a->index = i;
+		st->attrs[2 * i + 1] = &a->dev_attr.attr;
 	}
 
 	st->attr_group.attrs = st->attrs;
-- 
2.35.1.1320.gc452695387.dirty


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

* Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels
  2024-06-20 21:13 ` [PATCH 2/2] hwmon: iio: Add labels from IIO channels Sean Anderson
@ 2024-06-21 15:08   ` Guenter Roeck
  2024-06-21 15:22     ` Sean Anderson
  0 siblings, 1 reply; 8+ messages in thread
From: Guenter Roeck @ 2024-06-21 15:08 UTC (permalink / raw)
  To: Sean Anderson, Jonathan Cameron, Jean Delvare, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel

On 6/20/24 14:13, Sean Anderson wrote:
> Add labels from IIO channels to our channels. This allows userspace to
> display more meaningful names instead of "in0" or "temp5".
> 
> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
> ---
> 
>   drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
>   1 file changed, 30 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c
> index 4c8a80847891..588b64c18e63 100644
> --- a/drivers/hwmon/iio_hwmon.c
> +++ b/drivers/hwmon/iio_hwmon.c
> @@ -33,6 +33,17 @@ struct iio_hwmon_state {
>   	struct attribute **attrs;
>   };
>   
> +static ssize_t iio_hwmon_read_label(struct device *dev,
> +				  struct device_attribute *attr,
> +				  char *buf)
> +{
> +	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
> +	struct iio_hwmon_state *state = dev_get_drvdata(dev);
> +	struct iio_channel *chan = &state->channels[sattr->index];
> +
> +	return iio_read_channel_label(chan, buf);

This can return -EINVAL if there is no label. Since the label attribute
is created unconditionally, every affected system would end up with
lots of error messages when running the "sensors" command.
This is not acceptable.

Guenter

> +}
> +
>   /*
>    * Assumes that IIO and hwmon operate in the same base units.
>    * This is supposed to be true, but needs verification for
> @@ -90,12 +101,12 @@ static int iio_hwmon_probe(struct platform_device *pdev)
>   
>   	st->channels = channels;
>   
> -	/* count how many attributes we have */
> +	/* count how many channels we have */
>   	while (st->channels[st->num_channels].indio_dev)
>   		st->num_channels++;
>   
>   	st->attrs = devm_kcalloc(dev,
> -				 st->num_channels + 1, sizeof(*st->attrs),
> +				 2 * st->num_channels + 1, sizeof(*st->attrs),
>   				 GFP_KERNEL);
>   	if (st->attrs == NULL)
>   		return -ENOMEM;
> @@ -147,7 +158,23 @@ static int iio_hwmon_probe(struct platform_device *pdev)
>   		a->dev_attr.show = iio_hwmon_read_val;
>   		a->dev_attr.attr.mode = 0444;
>   		a->index = i;
> -		st->attrs[i] = &a->dev_attr.attr;
> +		st->attrs[2 * i] = &a->dev_attr.attr;
> +
> +		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
> +		if (a == NULL)
> +			return -ENOMEM;
> +
> +		sysfs_attr_init(&a->dev_attr.attr);
> +		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
> +						       "%s%d_label",
> +						       prefix, n);
> +		if (!a->dev_attr.attr.name)
> +			return -ENOMEM;
> +
> +		a->dev_attr.show = iio_hwmon_read_label;
> +		a->dev_attr.attr.mode = 0444;
> +		a->index = i;
> +		st->attrs[2 * i + 1] = &a->dev_attr.attr;
>   	}
>   
>   	st->attr_group.attrs = st->attrs;


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

* Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels
  2024-06-21 15:08   ` Guenter Roeck
@ 2024-06-21 15:22     ` Sean Anderson
  2024-06-21 15:31       ` Sean Anderson
  0 siblings, 1 reply; 8+ messages in thread
From: Sean Anderson @ 2024-06-21 15:22 UTC (permalink / raw)
  To: Guenter Roeck, Jonathan Cameron, Jean Delvare, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel

On 6/21/24 11:08, Guenter Roeck wrote:
> On 6/20/24 14:13, Sean Anderson wrote:
>> Add labels from IIO channels to our channels. This allows userspace to
>> display more meaningful names instead of "in0" or "temp5".
>>
>> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
>> ---
>>
>>   drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
>>   1 file changed, 30 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c
>> index 4c8a80847891..588b64c18e63 100644
>> --- a/drivers/hwmon/iio_hwmon.c
>> +++ b/drivers/hwmon/iio_hwmon.c
>> @@ -33,6 +33,17 @@ struct iio_hwmon_state {
>>       struct attribute **attrs;
>>   };
>>   +static ssize_t iio_hwmon_read_label(struct device *dev,
>> +                  struct device_attribute *attr,
>> +                  char *buf)
>> +{
>> +    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
>> +    struct iio_hwmon_state *state = dev_get_drvdata(dev);
>> +    struct iio_channel *chan = &state->channels[sattr->index];
>> +
>> +    return iio_read_channel_label(chan, buf);
> 
> This can return -EINVAL if there is no label. Since the label attribute
> is created unconditionally, every affected system would end up with
> lots of error messages when running the "sensors" command.
> This is not acceptable.

The sensors command gracefully handles this. There are no errors, and the label is unused.

--Sean


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

* Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels
  2024-06-21 15:22     ` Sean Anderson
@ 2024-06-21 15:31       ` Sean Anderson
  2024-06-21 16:45         ` Guenter Roeck
  0 siblings, 1 reply; 8+ messages in thread
From: Sean Anderson @ 2024-06-21 15:31 UTC (permalink / raw)
  To: Guenter Roeck, Jonathan Cameron, Jean Delvare, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel

On 6/21/24 11:22, Sean Anderson wrote:
> On 6/21/24 11:08, Guenter Roeck wrote:
>> On 6/20/24 14:13, Sean Anderson wrote:
>>> Add labels from IIO channels to our channels. This allows userspace to
>>> display more meaningful names instead of "in0" or "temp5".
>>>
>>> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
>>> ---
>>>
>>>   drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
>>>   1 file changed, 30 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c
>>> index 4c8a80847891..588b64c18e63 100644
>>> --- a/drivers/hwmon/iio_hwmon.c
>>> +++ b/drivers/hwmon/iio_hwmon.c
>>> @@ -33,6 +33,17 @@ struct iio_hwmon_state {
>>>       struct attribute **attrs;
>>>   };
>>>   +static ssize_t iio_hwmon_read_label(struct device *dev,
>>> +                  struct device_attribute *attr,
>>> +                  char *buf)
>>> +{
>>> +    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
>>> +    struct iio_hwmon_state *state = dev_get_drvdata(dev);
>>> +    struct iio_channel *chan = &state->channels[sattr->index];
>>> +
>>> +    return iio_read_channel_label(chan, buf);
>> 
>> This can return -EINVAL if there is no label. Since the label attribute
>> is created unconditionally, every affected system would end up with
>> lots of error messages when running the "sensors" command.
>> This is not acceptable.
> 
> The sensors command gracefully handles this. There are no errors, and the label is unused.

For example, without IIO labels I get:

$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
in1:         825.00 mV 
in2:         826.00 mV 
in3:           1.81 V  
in4:           1.18 V  
in5:           1.80 V  
in6:           1.80 V  
in7:           3.27 V  
in8:           1.81 V  
in9:         825.00 mV 
in10:          1.81 V  
in11:          1.80 V  
temp1:        +79.8 C  
temp2:        +80.9 C  

and with labels I get

$ sensors hwmon_ams_ps-isa-0000
hwmon_ams_ps-isa-0000
Adapter: ISA adapter
VCC_PSINTLP: 824.00 mV 
VCC_PSINTFP: 822.00 mV 
VCC_PSAUX:     1.81 V  
VCC_PSDDR:     1.18 V  
VCC_PSIO3:     1.80 V  
VCC_PSIO0:     1.80 V  
VCC_PSIO1:     3.27 V  
VCC_PSIO2:     1.80 V  
PS_MGTRAVCC: 822.00 mV 
PS_MGTRAVTT:   1.81 V  
VCC_PSADC:     1.80 V  
Temp_LPD:     +79.5 C  
Temp_FPD:     +80.2 C  

I also was concerned about the same thing, but lm-sensors handles things
gracefully, allowing us to simplify the implementation.

--Sean

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

* Re: [PATCH 2/2] hwmon: iio: Add labels from IIO channels
  2024-06-21 15:31       ` Sean Anderson
@ 2024-06-21 16:45         ` Guenter Roeck
  0 siblings, 0 replies; 8+ messages in thread
From: Guenter Roeck @ 2024-06-21 16:45 UTC (permalink / raw)
  To: Sean Anderson, Jonathan Cameron, Jean Delvare, linux-iio,
	linux-hwmon
  Cc: Lars-Peter Clausen, linux-kernel

On 6/21/24 08:31, Sean Anderson wrote:
> On 6/21/24 11:22, Sean Anderson wrote:
>> On 6/21/24 11:08, Guenter Roeck wrote:
>>> On 6/20/24 14:13, Sean Anderson wrote:
>>>> Add labels from IIO channels to our channels. This allows userspace to
>>>> display more meaningful names instead of "in0" or "temp5".
>>>>
>>>> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>
>>>> ---
>>>>
>>>>    drivers/hwmon/iio_hwmon.c | 33 ++++++++++++++++++++++++++++++---
>>>>    1 file changed, 30 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/drivers/hwmon/iio_hwmon.c b/drivers/hwmon/iio_hwmon.c
>>>> index 4c8a80847891..588b64c18e63 100644
>>>> --- a/drivers/hwmon/iio_hwmon.c
>>>> +++ b/drivers/hwmon/iio_hwmon.c
>>>> @@ -33,6 +33,17 @@ struct iio_hwmon_state {
>>>>        struct attribute **attrs;
>>>>    };
>>>>    +static ssize_t iio_hwmon_read_label(struct device *dev,
>>>> +                  struct device_attribute *attr,
>>>> +                  char *buf)
>>>> +{
>>>> +    struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
>>>> +    struct iio_hwmon_state *state = dev_get_drvdata(dev);
>>>> +    struct iio_channel *chan = &state->channels[sattr->index];
>>>> +
>>>> +    return iio_read_channel_label(chan, buf);
>>>
>>> This can return -EINVAL if there is no label. Since the label attribute
>>> is created unconditionally, every affected system would end up with
>>> lots of error messages when running the "sensors" command.
>>> This is not acceptable.
>>
>> The sensors command gracefully handles this. There are no errors, and the label is unused.
> 
> For example, without IIO labels I get:
> 
> $ sensors hwmon_ams_ps-isa-0000
> hwmon_ams_ps-isa-0000
> Adapter: ISA adapter
> in1:         825.00 mV
> in2:         826.00 mV
> in3:           1.81 V
> in4:           1.18 V
> in5:           1.80 V
> in6:           1.80 V
> in7:           3.27 V
> in8:           1.81 V
> in9:         825.00 mV
> in10:          1.81 V
> in11:          1.80 V
> temp1:        +79.8 C
> temp2:        +80.9 C
> 
> and with labels I get
> 
> $ sensors hwmon_ams_ps-isa-0000
> hwmon_ams_ps-isa-0000
> Adapter: ISA adapter
> VCC_PSINTLP: 824.00 mV
> VCC_PSINTFP: 822.00 mV
> VCC_PSAUX:     1.81 V
> VCC_PSDDR:     1.18 V
> VCC_PSIO3:     1.80 V
> VCC_PSIO0:     1.80 V
> VCC_PSIO1:     3.27 V
> VCC_PSIO2:     1.80 V
> PS_MGTRAVCC: 822.00 mV
> PS_MGTRAVTT:   1.81 V
> VCC_PSADC:     1.80 V
> Temp_LPD:     +79.5 C
> Temp_FPD:     +80.2 C
> 

You are correct, the sensors code ignores access errors on label attributes.
Still, I am not even going there. If there is no label, don't create
the attribute, period. The ABI says

     Should only be created if the driver has hints about what
     this voltage channel is being used for, and user-space
     doesn't. In all other cases, the label is provided by
     user-space.

It should be obvious that this doesn't mean that you can create random
attributes and then just return an error if it isn't actually supported.

Guenter


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

* Re: [PATCH 1/2] iio: Add iio_read_channel_label to inkern API
  2024-06-20 21:13 ` [PATCH 1/2] iio: Add iio_read_channel_label to inkern API Sean Anderson
@ 2024-06-23 12:03   ` Jonathan Cameron
  0 siblings, 0 replies; 8+ messages in thread
From: Jonathan Cameron @ 2024-06-23 12:03 UTC (permalink / raw)
  To: Sean Anderson
  Cc: Jean Delvare, Guenter Roeck, linux-iio, linux-hwmon,
	Lars-Peter Clausen, linux-kernel

On Thu, 20 Jun 2024 17:13:08 -0400
Sean Anderson <sean.anderson@linux.dev> wrote:

> It can be convenient for other in-kernel drivers to reuse IIO channel
> labels. Export the iio_read_channel_label function to allow this. The
> signature is different depending on where we are calling it from, so
> the meat is moved to do_iio_read_channel_label.
> 
> Signed-off-by: Sean Anderson <sean.anderson@linux.dev>

Seems like a useful addition to me. I'm not 100% what path this will
take so if it goes via HWMON.

Acked-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

It may be messy and need an immutable branch however. I haven't
checked for churn in this code yet.


> ---
> 
>  drivers/iio/iio_core.h          |  4 ++++
>  drivers/iio/industrialio-core.c | 23 ++++++++++++++---------
>  drivers/iio/inkern.c            |  6 ++++++
>  include/linux/iio/consumer.h    | 10 ++++++++++
>  4 files changed, 34 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
> index 1a38b1915e7a..b7d5f4f0fada 100644
> --- a/drivers/iio/iio_core.h
> +++ b/drivers/iio/iio_core.h
> @@ -34,6 +34,10 @@ void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
>  				       struct iio_ioctl_handler *h);
>  void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h);
>  
> +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,
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 2f185b386949..0f6cda7ffe45 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -727,22 +727,27 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
>  }
>  EXPORT_SYMBOL_GPL(iio_format_value);
>  
> -static ssize_t iio_read_channel_label(struct device *dev,
> -				      struct device_attribute *attr,
> -				      char *buf)
> +ssize_t do_iio_read_channel_label(struct iio_dev *indio_dev,
> +				  const struct iio_chan_spec *c,
> +				  char *buf)
>  {
> -	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> -	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
> -
>  	if (indio_dev->info->read_label)
> -		return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
> +		return indio_dev->info->read_label(indio_dev, c, buf);
>  
> -	if (this_attr->c->extend_name)
> -		return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
> +	if (c->extend_name)
> +		return sysfs_emit(buf, "%s\n", c->extend_name);
>  
>  	return -EINVAL;
>  }
>  
> +static ssize_t iio_read_channel_label(struct device *dev,
> +				      struct device_attribute *attr,
> +				      char *buf)
> +{
> +	return do_iio_read_channel_label(dev_to_iio_dev(dev),
> +					 to_iio_dev_attr(attr)->c, buf);
> +}
> +
>  static ssize_t iio_read_channel_info(struct device *dev,
>  				     struct device_attribute *attr,
>  				     char *buf)
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 39cf26d69d17..9f484c94bc6e 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -1010,3 +1010,9 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
>  			       chan->channel, buf, len);
>  }
>  EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
> +
> +ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
> +{
> +	return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
> +}
> +EXPORT_SYMBOL_GPL(iio_read_channel_label);
> diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
> index e9910b41d48e..333d1d8ccb37 100644
> --- a/include/linux/iio/consumer.h
> +++ b/include/linux/iio/consumer.h
> @@ -441,4 +441,14 @@ ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
>  ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
>  				   const char *buf, size_t len);
>  
> +/**
> + * iio_read_channel_label() - read label for a given channel
> + * @chan:		The channel being queried.
> + * @buf:		Where to store the attribute value. Assumed to hold
> + *			at least PAGE_SIZE bytes.
> + *
> + * Returns the number of bytes written to buf, or an error code.
> + */
> +ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf);
> +
>  #endif


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

end of thread, other threads:[~2024-06-23 12:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-20 21:13 [PATCH 0/2] hwmon: iio: Add labels Sean Anderson
2024-06-20 21:13 ` [PATCH 1/2] iio: Add iio_read_channel_label to inkern API Sean Anderson
2024-06-23 12:03   ` Jonathan Cameron
2024-06-20 21:13 ` [PATCH 2/2] hwmon: iio: Add labels from IIO channels Sean Anderson
2024-06-21 15:08   ` Guenter Roeck
2024-06-21 15:22     ` Sean Anderson
2024-06-21 15:31       ` Sean Anderson
2024-06-21 16:45         ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).