[PATCH v2 12/18] tools: gpio: port lsgpio to v2 uAPI

From: Kent Gibson
Date: Sat Jul 25 2020 - 00:22:23 EST


Port the lsgpio tool to the latest GPIO uAPI.

Signed-off-by: Kent Gibson <warthog618@xxxxxxxxx>
---
tools/gpio/lsgpio.c | 102 ++++++++++++++++++++------------------------
1 file changed, 47 insertions(+), 55 deletions(-)

diff --git a/tools/gpio/lsgpio.c b/tools/gpio/lsgpio.c
index b08d7a5e779b..0a993d535707 100644
--- a/tools/gpio/lsgpio.c
+++ b/tools/gpio/lsgpio.c
@@ -23,58 +23,50 @@

#include "gpio-utils.h"

-struct gpio_flag {
- char *name;
- unsigned long mask;
-};
-
-struct gpio_flag flagnames[] = {
- {
- .name = "kernel",
- .mask = GPIOLINE_FLAG_KERNEL,
- },
- {
- .name = "output",
- .mask = GPIOLINE_FLAG_IS_OUT,
- },
- {
- .name = "active-low",
- .mask = GPIOLINE_FLAG_ACTIVE_LOW,
- },
- {
- .name = "open-drain",
- .mask = GPIOLINE_FLAG_OPEN_DRAIN,
- },
- {
- .name = "open-source",
- .mask = GPIOLINE_FLAG_OPEN_SOURCE,
- },
- {
- .name = "pull-up",
- .mask = GPIOLINE_FLAG_BIAS_PULL_UP,
- },
- {
- .name = "pull-down",
- .mask = GPIOLINE_FLAG_BIAS_PULL_DOWN,
- },
- {
- .name = "bias-disabled",
- .mask = GPIOLINE_FLAG_BIAS_DISABLE,
- },
-};
-
-void print_flags(unsigned long flags)
+static void print_attributes(struct gpioline_info_v2 *info)
{
int i;
- int printed = 0;
-
- for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
- if (flags & flagnames[i].mask) {
- if (printed)
- fprintf(stdout, " ");
- fprintf(stdout, "%s", flagnames[i].name);
- printed++;
- }
+ const char *field_format = "%s";
+
+ if (info->flags & GPIOLINE_FLAG_V2_USED) {
+ fprintf(stdout, field_format, "used");
+ field_format = ", %s";
+ }
+
+ if (info->flags & GPIOLINE_FLAG_V2_OUTPUT)
+ fprintf(stdout, field_format, "output");
+ else
+ fprintf(stdout, field_format, "input");
+
+ field_format = ", %s";
+
+ if (info->flags & GPIOLINE_FLAG_V2_ACTIVE_LOW)
+ fprintf(stdout, field_format, "active-low");
+
+ if (info->flags & GPIOLINE_FLAG_V2_OPEN_DRAIN)
+ fprintf(stdout, field_format, "open-drain");
+ if (info->flags & GPIOLINE_FLAG_V2_OPEN_SOURCE)
+ fprintf(stdout, field_format, "open-source");
+
+ if (info->flags & GPIOLINE_FLAG_V2_BIAS_DISABLED)
+ fprintf(stdout, field_format, "bias-disabled");
+ if (info->flags == GPIOLINE_FLAG_V2_BIAS_PULL_UP)
+ fprintf(stdout, field_format, "pull-up");
+ if (info->flags == GPIOLINE_FLAG_V2_BIAS_PULL_DOWN)
+ fprintf(stdout, field_format, "pull-down");
+
+ if ((info->flags & GPIOLINE_FLAG_V2_EDGE_RISING) &&
+ (info->flags & GPIOLINE_FLAG_V2_EDGE_FALLING))
+ fprintf(stdout, field_format, "both-edges");
+ else if (info->flags & GPIOLINE_FLAG_V2_EDGE_RISING)
+ fprintf(stdout, field_format, "rising-edge");
+ else if (info->flags & GPIOLINE_FLAG_V2_EDGE_FALLING)
+ fprintf(stdout, field_format, "falling-edge");
+
+ for (i = 0; i < info->num_attrs; i++) {
+ if (info->attrs[i].id == GPIOLINE_ATTR_ID_DEBOUNCE)
+ fprintf(stdout, ", debounce_period=%dusec",
+ info->attrs[0].debounce_period);
}
}

@@ -109,18 +101,18 @@ int list_device(const char *device_name)

/* Loop over the lines and print info */
for (i = 0; i < cinfo.lines; i++) {
- struct gpioline_info linfo;
+ struct gpioline_info_v2 linfo;

memset(&linfo, 0, sizeof(linfo));
- linfo.line_offset = i;
+ linfo.offset = i;

- ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo);
+ ret = ioctl(fd, GPIO_GET_LINEINFO_V2_IOCTL, &linfo);
if (ret == -1) {
ret = -errno;
perror("Failed to issue LINEINFO IOCTL\n");
goto exit_close_error;
}
- fprintf(stdout, "\tline %2d:", linfo.line_offset);
+ fprintf(stdout, "\tline %2d:", linfo.offset);
if (linfo.name[0])
fprintf(stdout, " \"%s\"", linfo.name);
else
@@ -131,7 +123,7 @@ int list_device(const char *device_name)
fprintf(stdout, " unused");
if (linfo.flags) {
fprintf(stdout, " [");
- print_flags(linfo.flags);
+ print_attributes(&linfo);
fprintf(stdout, "]");
}
fprintf(stdout, "\n");
--
2.27.0