All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v8] iio: Handle enumerated properties with gaps
@ 2021-01-07 11:20 Alexandru Ardelean
  2021-01-09 19:23 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandru Ardelean @ 2021-01-07 11:20 UTC (permalink / raw)
  To: linux-iio, linux-kernel; +Cc: jic23, lars, andy.shevchenko, Alexandru Ardelean

From: Lars-Peter Clausen <lars@metafoo.de>

Some enums might have gaps or reserved values in the middle of their value
range. E.g. consider a 2-bit enum where the values 0, 1 and 3 have a
meaning, but 2 is a reserved value and can not be used.

Add support for such enums to the IIO enum helper functions. A reserved
values is marked by setting its entry in the items array to NULL rather
than the normal descriptive string value.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---

Changelog v7 -> v8:
* https://lore.kernel.org/linux-iio/20210107084434.35283-1-alexandru.ardelean@analog.com/
* dropped patch 'lib/string.c: add __sysfs_match_string_with_gaps() helper'
* merged __sysfs_match_string_with_gaps into  drivers/iio/industrial-core.c 
  as iio_sysfs_match_string_with_gaps()

 drivers/iio/industrialio-core.c | 39 ++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index e9ee9363fed0..db20e2ab437d 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -169,6 +169,36 @@ static const char * const iio_chan_info_postfix[] = {
 	[IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
 };
 
+/**
+ * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
+ * @array: array of strings
+ * @n: number of strings in the array
+ * @str: string to match with
+ *
+ * Returns index of @str in the @array or -EINVAL, similar to match_string().
+ * Uses sysfs_streq instead of strcmp for matching.
+ *
+ * This routine will look for a string in an array of strings.
+ * The search will continue until the element is found or the n-th element
+ * is reached, regardless of any NULL elements in the array.
+ */
+static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
+					    const char *str)
+{
+	const char *item;
+	int index;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			continue;
+		if (sysfs_streq(item, str))
+			return index;
+	}
+
+	return -EINVAL;
+}
+
 #if defined(CONFIG_DEBUG_FS)
 /*
  * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
@@ -470,8 +500,11 @@ ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
 	if (!e->num_items)
 		return 0;
 
-	for (i = 0; i < e->num_items; ++i)
+	for (i = 0; i < e->num_items; ++i) {
+		if (!e->items[i])
+			continue;
 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
+	}
 
 	/* replace last space with a newline */
 	buf[len - 1] = '\n';
@@ -492,7 +525,7 @@ ssize_t iio_enum_read(struct iio_dev *indio_dev,
 	i = e->get(indio_dev, chan);
 	if (i < 0)
 		return i;
-	else if (i >= e->num_items)
+	else if (i >= e->num_items || !e->items[i])
 		return -EINVAL;
 
 	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
@@ -509,7 +542,7 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev,
 	if (!e->set)
 		return -EINVAL;
 
-	ret = __sysfs_match_string(e->items, e->num_items, buf);
+	ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
 	if (ret < 0)
 		return ret;
 
-- 
2.17.1


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

* Re: [PATCH v8] iio: Handle enumerated properties with gaps
  2021-01-07 11:20 [PATCH v8] iio: Handle enumerated properties with gaps Alexandru Ardelean
@ 2021-01-09 19:23 ` Jonathan Cameron
  2021-01-09 20:53   ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2021-01-09 19:23 UTC (permalink / raw)
  To: Alexandru Ardelean; +Cc: linux-iio, linux-kernel, lars, andy.shevchenko

On Thu, 7 Jan 2021 13:20:49 +0200
Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:

> From: Lars-Peter Clausen <lars@metafoo.de>
> 
> Some enums might have gaps or reserved values in the middle of their value
> range. E.g. consider a 2-bit enum where the values 0, 1 and 3 have a
> meaning, but 2 is a reserved value and can not be used.
> 
> Add support for such enums to the IIO enum helper functions. A reserved
> values is marked by setting its entry in the items array to NULL rather
> than the normal descriptive string value.
> 
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>

Applied to the togreg branch of iio.git and pushed out as testing for all
the normal autobuilder related reasons.

Note I can still rebase so if anyone wants to add tags or comment it's
not yet too late!

Jonathan

> ---
> 
> Changelog v7 -> v8:
> * https://lore.kernel.org/linux-iio/20210107084434.35283-1-alexandru.ardelean@analog.com/
> * dropped patch 'lib/string.c: add __sysfs_match_string_with_gaps() helper'
> * merged __sysfs_match_string_with_gaps into  drivers/iio/industrial-core.c 
>   as iio_sysfs_match_string_with_gaps()
> 
>  drivers/iio/industrialio-core.c | 39 ++++++++++++++++++++++++++++++---
>  1 file changed, 36 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index e9ee9363fed0..db20e2ab437d 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -169,6 +169,36 @@ static const char * const iio_chan_info_postfix[] = {
>  	[IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
>  };
>  
> +/**
> + * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
> + * @array: array of strings
> + * @n: number of strings in the array
> + * @str: string to match with
> + *
> + * Returns index of @str in the @array or -EINVAL, similar to match_string().
> + * Uses sysfs_streq instead of strcmp for matching.
> + *
> + * This routine will look for a string in an array of strings.
> + * The search will continue until the element is found or the n-th element
> + * is reached, regardless of any NULL elements in the array.
> + */
> +static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
> +					    const char *str)
> +{
> +	const char *item;
> +	int index;
> +
> +	for (index = 0; index < n; index++) {
> +		item = array[index];
> +		if (!item)
> +			continue;
> +		if (sysfs_streq(item, str))
> +			return index;
> +	}
> +
> +	return -EINVAL;
> +}
> +
>  #if defined(CONFIG_DEBUG_FS)
>  /*
>   * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
> @@ -470,8 +500,11 @@ ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
>  	if (!e->num_items)
>  		return 0;
>  
> -	for (i = 0; i < e->num_items; ++i)
> +	for (i = 0; i < e->num_items; ++i) {
> +		if (!e->items[i])
> +			continue;
>  		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
> +	}
>  
>  	/* replace last space with a newline */
>  	buf[len - 1] = '\n';
> @@ -492,7 +525,7 @@ ssize_t iio_enum_read(struct iio_dev *indio_dev,
>  	i = e->get(indio_dev, chan);
>  	if (i < 0)
>  		return i;
> -	else if (i >= e->num_items)
> +	else if (i >= e->num_items || !e->items[i])
>  		return -EINVAL;
>  
>  	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
> @@ -509,7 +542,7 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev,
>  	if (!e->set)
>  		return -EINVAL;
>  
> -	ret = __sysfs_match_string(e->items, e->num_items, buf);
> +	ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
>  	if (ret < 0)
>  		return ret;
>  


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

* Re: [PATCH v8] iio: Handle enumerated properties with gaps
  2021-01-09 19:23 ` Jonathan Cameron
@ 2021-01-09 20:53   ` Andy Shevchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2021-01-09 20:53 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Alexandru Ardelean, linux-iio, Linux Kernel Mailing List,
	Lars-Peter Clausen

On Sat, Jan 9, 2021 at 9:23 PM Jonathan Cameron <jic23@kernel.org> wrote:
>
> On Thu, 7 Jan 2021 13:20:49 +0200
> Alexandru Ardelean <alexandru.ardelean@analog.com> wrote:
>
> > From: Lars-Peter Clausen <lars@metafoo.de>
> >
> > Some enums might have gaps or reserved values in the middle of their value
> > range. E.g. consider a 2-bit enum where the values 0, 1 and 3 have a
> > meaning, but 2 is a reserved value and can not be used.
> >
> > Add support for such enums to the IIO enum helper functions. A reserved
> > values is marked by setting its entry in the items array to NULL rather
> > than the normal descriptive string value.
> >
> > Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> > Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
>
> Applied to the togreg branch of iio.git and pushed out as testing for all
> the normal autobuilder related reasons.
>
> Note I can still rebase so if anyone wants to add tags or comment it's
> not yet too late!

Free to add
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

-- 
With Best Regards,
Andy Shevchenko

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

end of thread, other threads:[~2021-01-09 20:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-07 11:20 [PATCH v8] iio: Handle enumerated properties with gaps Alexandru Ardelean
2021-01-09 19:23 ` Jonathan Cameron
2021-01-09 20:53   ` Andy Shevchenko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.