Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 2 | /* |
| 3 | * lsgpio - example on how to list the GPIO lines on a system |
| 4 | * |
| 5 | * Copyright (C) 2015 Linus Walleij |
| 6 | * |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 7 | * Usage: |
| 8 | * lsgpio <-n device-name> |
| 9 | */ |
| 10 | |
| 11 | #include <unistd.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdio.h> |
| 15 | #include <dirent.h> |
| 16 | #include <errno.h> |
| 17 | #include <string.h> |
| 18 | #include <poll.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <getopt.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <linux/gpio.h> |
| 23 | |
| 24 | #include "gpio-utils.h" |
| 25 | |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 26 | struct gpio_flag { |
| 27 | char *name; |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 28 | unsigned long long mask; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | struct gpio_flag flagnames[] = { |
| 32 | { |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 33 | .name = "used", |
| 34 | .mask = GPIO_V2_LINE_FLAG_USED, |
| 35 | }, |
| 36 | { |
| 37 | .name = "input", |
| 38 | .mask = GPIO_V2_LINE_FLAG_INPUT, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 39 | }, |
| 40 | { |
| 41 | .name = "output", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 42 | .mask = GPIO_V2_LINE_FLAG_OUTPUT, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 43 | }, |
| 44 | { |
| 45 | .name = "active-low", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 46 | .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | .name = "open-drain", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 50 | .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 51 | }, |
| 52 | { |
| 53 | .name = "open-source", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 54 | .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 55 | }, |
Kent Gibson | 3831c05 | 2020-04-30 08:09:16 +0800 | [diff] [blame] | 56 | { |
| 57 | .name = "pull-up", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 58 | .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP, |
Kent Gibson | 3831c05 | 2020-04-30 08:09:16 +0800 | [diff] [blame] | 59 | }, |
| 60 | { |
| 61 | .name = "pull-down", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 62 | .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN, |
Kent Gibson | 3831c05 | 2020-04-30 08:09:16 +0800 | [diff] [blame] | 63 | }, |
| 64 | { |
| 65 | .name = "bias-disabled", |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 66 | .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED, |
Kent Gibson | 3831c05 | 2020-04-30 08:09:16 +0800 | [diff] [blame] | 67 | }, |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 70 | static void print_attributes(struct gpio_v2_line_info *info) |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 71 | { |
| 72 | int i; |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 73 | const char *field_format = "%s"; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 74 | |
| 75 | for (i = 0; i < ARRAY_SIZE(flagnames); i++) { |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 76 | if (info->flags & flagnames[i].mask) { |
| 77 | fprintf(stdout, field_format, flagnames[i].name); |
| 78 | field_format = ", %s"; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 81 | |
| 82 | if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) && |
| 83 | (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)) |
| 84 | fprintf(stdout, field_format, "both-edges"); |
| 85 | else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) |
| 86 | fprintf(stdout, field_format, "rising-edge"); |
| 87 | else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING) |
| 88 | fprintf(stdout, field_format, "falling-edge"); |
| 89 | |
| 90 | for (i = 0; i < info->num_attrs; i++) { |
| 91 | if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE) |
| 92 | fprintf(stdout, ", debounce_period=%dusec", |
| 93 | info->attrs[0].debounce_period_us); |
| 94 | } |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 97 | int list_device(const char *device_name) |
| 98 | { |
| 99 | struct gpiochip_info cinfo; |
| 100 | char *chrdev_name; |
| 101 | int fd; |
| 102 | int ret; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 103 | int i; |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 104 | |
| 105 | ret = asprintf(&chrdev_name, "/dev/%s", device_name); |
| 106 | if (ret < 0) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | fd = open(chrdev_name, 0); |
| 110 | if (fd == -1) { |
| 111 | ret = -errno; |
| 112 | fprintf(stderr, "Failed to open %s\n", chrdev_name); |
Kent Gibson | ef3c61a0 | 2020-07-08 12:15:58 +0800 | [diff] [blame] | 113 | goto exit_free_name; |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Inspect this GPIO chip */ |
| 117 | ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo); |
| 118 | if (ret == -1) { |
| 119 | ret = -errno; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 120 | perror("Failed to issue CHIPINFO IOCTL\n"); |
| 121 | goto exit_close_error; |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 122 | } |
Linus Walleij | df4878e | 2016-02-12 14:48:23 +0100 | [diff] [blame] | 123 | fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n", |
| 124 | cinfo.name, cinfo.label, cinfo.lines); |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 125 | |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 126 | /* Loop over the lines and print info */ |
| 127 | for (i = 0; i < cinfo.lines; i++) { |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 128 | struct gpio_v2_line_info linfo; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 129 | |
| 130 | memset(&linfo, 0, sizeof(linfo)); |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 131 | linfo.offset = i; |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 132 | |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 133 | ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 134 | if (ret == -1) { |
| 135 | ret = -errno; |
| 136 | perror("Failed to issue LINEINFO IOCTL\n"); |
| 137 | goto exit_close_error; |
| 138 | } |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 139 | fprintf(stdout, "\tline %2d:", linfo.offset); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 140 | if (linfo.name[0]) |
Markus Pargmann | bb91d34 | 2016-02-23 08:54:46 +0100 | [diff] [blame] | 141 | fprintf(stdout, " \"%s\"", linfo.name); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 142 | else |
| 143 | fprintf(stdout, " unnamed"); |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 144 | if (linfo.consumer[0]) |
| 145 | fprintf(stdout, " \"%s\"", linfo.consumer); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 146 | else |
Linus Walleij | 214338e | 2016-02-25 21:01:48 +0100 | [diff] [blame] | 147 | fprintf(stdout, " unused"); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 148 | if (linfo.flags) { |
| 149 | fprintf(stdout, " ["); |
Kent Gibson | 3c333c4 | 2020-09-28 08:28:01 +0800 | [diff] [blame] | 150 | print_attributes(&linfo); |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 151 | fprintf(stdout, "]"); |
| 152 | } |
| 153 | fprintf(stdout, "\n"); |
| 154 | |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Linus Walleij | 521a2ad | 2016-02-12 22:25:22 +0100 | [diff] [blame] | 157 | exit_close_error: |
| 158 | if (close(fd) == -1) |
| 159 | perror("Failed to close GPIO character device file"); |
Kent Gibson | ef3c61a0 | 2020-07-08 12:15:58 +0800 | [diff] [blame] | 160 | exit_free_name: |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 161 | free(chrdev_name); |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 162 | return ret; |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void print_usage(void) |
| 166 | { |
| 167 | fprintf(stderr, "Usage: lsgpio [options]...\n" |
| 168 | "List GPIO chips, lines and states\n" |
| 169 | " -n <name> List GPIOs on a named device\n" |
| 170 | " -? This helptext\n" |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | int main(int argc, char **argv) |
| 175 | { |
Geert Uytterhoeven | 691998f | 2016-03-25 13:36:30 +0100 | [diff] [blame] | 176 | const char *device_name = NULL; |
Linus Walleij | 6d591c4 | 2015-10-21 15:45:54 +0200 | [diff] [blame] | 177 | int ret; |
| 178 | int c; |
| 179 | |
| 180 | while ((c = getopt(argc, argv, "n:")) != -1) { |
| 181 | switch (c) { |
| 182 | case 'n': |
| 183 | device_name = optarg; |
| 184 | break; |
| 185 | case '?': |
| 186 | print_usage(); |
| 187 | return -1; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (device_name) |
| 192 | ret = list_device(device_name); |
| 193 | else { |
| 194 | const struct dirent *ent; |
| 195 | DIR *dp; |
| 196 | |
| 197 | /* List all GPIO devices one at a time */ |
| 198 | dp = opendir("/dev"); |
| 199 | if (!dp) { |
| 200 | ret = -errno; |
| 201 | goto error_out; |
| 202 | } |
| 203 | |
| 204 | ret = -ENOENT; |
| 205 | while (ent = readdir(dp), ent) { |
| 206 | if (check_prefix(ent->d_name, "gpiochip")) { |
| 207 | ret = list_device(ent->d_name); |
| 208 | if (ret) |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ret = 0; |
| 214 | if (closedir(dp) == -1) { |
| 215 | perror("scanning devices: Failed to close directory"); |
| 216 | ret = -errno; |
| 217 | } |
| 218 | } |
| 219 | error_out: |
| 220 | return ret; |
| 221 | } |