summaryrefslogtreecommitdiff
path: root/drivers/iio/industrialio-core.c
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2020-11-14 12:59:59 +0100
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2020-12-03 19:32:43 +0000
commiteda20ba1e25e6d8e7fa0c62d1bd685aa5de8e8ff (patch)
treee590dfb932cea6afbf3497b738ab4981daf1de2a /drivers/iio/industrialio-core.c
parent6d90c9a96e68967118149e04b7b170115866795b (diff)
iio: core: Consolidate iio_format_avail_{list,range}()
The iio_format_avail_list() and iio_format_avail_range() functions are almost identical. The only differences are that iio_format_avail_range() expects a fixed amount of items and adds brackets "[ ]" around the output. Refactor them into a common helper function. This improves the maintainability of the code as it makes it easier to modify the implementation of these functions. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Link: https://lore.kernel.org/r/20201114120000.6533-1-lars@metafoo.de Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Diffstat (limited to 'drivers/iio/industrialio-core.c')
-rw-r--r--drivers/iio/industrialio-core.c57
1 files changed, 14 insertions, 43 deletions
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 7cae46f9bc0d..9346f1c788b8 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -711,11 +711,13 @@ static ssize_t iio_read_channel_info(struct device *dev,
return iio_format_value(buf, ret, val_len, vals);
}
-static ssize_t iio_format_avail_list(char *buf, const int *vals,
- int type, int length)
+static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
+ const char *prefix, const char *suffix)
{
+ ssize_t len;
int i;
- ssize_t len = 0;
+
+ len = scnprintf(buf, PAGE_SIZE, prefix);
switch (type) {
case IIO_VAL_INT:
@@ -729,7 +731,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
" ");
else
len += scnprintf(buf + len, PAGE_SIZE - len,
- "\n");
+ "%s\n", suffix);
if (len >= PAGE_SIZE)
return -EFBIG;
}
@@ -745,7 +747,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
" ");
else
len += scnprintf(buf + len, PAGE_SIZE - len,
- "\n");
+ "%s\n", suffix);
if (len >= PAGE_SIZE)
return -EFBIG;
}
@@ -754,47 +756,16 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
return len;
}
-static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
+static ssize_t iio_format_avail_list(char *buf, const int *vals,
+ int type, int length)
{
- int i;
- ssize_t len;
- len = snprintf(buf, PAGE_SIZE, "[");
- switch (type) {
- case IIO_VAL_INT:
- for (i = 0; i < 3; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 1, &vals[i]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < 2)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "]\n");
- if (len >= PAGE_SIZE)
- return -EFBIG;
- }
- break;
- default:
- for (i = 0; i < 3; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 2, &vals[i * 2]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < 2)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "]\n");
- if (len >= PAGE_SIZE)
- return -EFBIG;
- }
- }
+ return iio_format_list(buf, vals, type, length, "", "");
+}
- return len;
+static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
+{
+ return iio_format_list(buf, vals, type, 3, "[", "]");
}
static ssize_t iio_read_channel_info_avail(struct device *dev,