blob: 52a0be45410c97e8f02c0ccc301855fbc57864b2 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij6d591c42015-10-21 15:45:54 +02002/*
3 * lsgpio - example on how to list the GPIO lines on a system
4 *
5 * Copyright (C) 2015 Linus Walleij
6 *
Linus Walleij6d591c42015-10-21 15:45:54 +02007 * 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 Walleij521a2ad2016-02-12 22:25:22 +010026struct gpio_flag {
27 char *name;
Kent Gibson3c333c42020-09-28 08:28:01 +080028 unsigned long long mask;
Linus Walleij521a2ad2016-02-12 22:25:22 +010029};
30
31struct gpio_flag flagnames[] = {
32 {
Kent Gibson3c333c42020-09-28 08:28:01 +080033 .name = "used",
34 .mask = GPIO_V2_LINE_FLAG_USED,
35 },
36 {
37 .name = "input",
38 .mask = GPIO_V2_LINE_FLAG_INPUT,
Linus Walleij521a2ad2016-02-12 22:25:22 +010039 },
40 {
41 .name = "output",
Kent Gibson3c333c42020-09-28 08:28:01 +080042 .mask = GPIO_V2_LINE_FLAG_OUTPUT,
Linus Walleij521a2ad2016-02-12 22:25:22 +010043 },
44 {
45 .name = "active-low",
Kent Gibson3c333c42020-09-28 08:28:01 +080046 .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
Linus Walleij521a2ad2016-02-12 22:25:22 +010047 },
48 {
49 .name = "open-drain",
Kent Gibson3c333c42020-09-28 08:28:01 +080050 .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
Linus Walleij521a2ad2016-02-12 22:25:22 +010051 },
52 {
53 .name = "open-source",
Kent Gibson3c333c42020-09-28 08:28:01 +080054 .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
Linus Walleij521a2ad2016-02-12 22:25:22 +010055 },
Kent Gibson3831c052020-04-30 08:09:16 +080056 {
57 .name = "pull-up",
Kent Gibson3c333c42020-09-28 08:28:01 +080058 .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
Kent Gibson3831c052020-04-30 08:09:16 +080059 },
60 {
61 .name = "pull-down",
Kent Gibson3c333c42020-09-28 08:28:01 +080062 .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
Kent Gibson3831c052020-04-30 08:09:16 +080063 },
64 {
65 .name = "bias-disabled",
Kent Gibson3c333c42020-09-28 08:28:01 +080066 .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
Kent Gibson3831c052020-04-30 08:09:16 +080067 },
Kent Gibsonda777be2020-10-15 07:11:57 +080068 {
69 .name = "clock-realtime",
70 .mask = GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME,
71 },
Linus Walleij521a2ad2016-02-12 22:25:22 +010072};
73
Kent Gibson3c333c42020-09-28 08:28:01 +080074static void print_attributes(struct gpio_v2_line_info *info)
Linus Walleij521a2ad2016-02-12 22:25:22 +010075{
76 int i;
Kent Gibson3c333c42020-09-28 08:28:01 +080077 const char *field_format = "%s";
Linus Walleij521a2ad2016-02-12 22:25:22 +010078
79 for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
Kent Gibson3c333c42020-09-28 08:28:01 +080080 if (info->flags & flagnames[i].mask) {
81 fprintf(stdout, field_format, flagnames[i].name);
82 field_format = ", %s";
Linus Walleij521a2ad2016-02-12 22:25:22 +010083 }
84 }
Kent Gibson3c333c42020-09-28 08:28:01 +080085
86 if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
87 (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
88 fprintf(stdout, field_format, "both-edges");
89 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
90 fprintf(stdout, field_format, "rising-edge");
91 else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
92 fprintf(stdout, field_format, "falling-edge");
93
94 for (i = 0; i < info->num_attrs; i++) {
95 if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
96 fprintf(stdout, ", debounce_period=%dusec",
Milo Spadacini19d09d32023-05-08 15:18:48 +020097 info->attrs[i].debounce_period_us);
Kent Gibson3c333c42020-09-28 08:28:01 +080098 }
Linus Walleij521a2ad2016-02-12 22:25:22 +010099}
100
Linus Walleij6d591c42015-10-21 15:45:54 +0200101int list_device(const char *device_name)
102{
103 struct gpiochip_info cinfo;
104 char *chrdev_name;
105 int fd;
106 int ret;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100107 int i;
Linus Walleij6d591c42015-10-21 15:45:54 +0200108
109 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
110 if (ret < 0)
111 return -ENOMEM;
112
113 fd = open(chrdev_name, 0);
114 if (fd == -1) {
115 ret = -errno;
116 fprintf(stderr, "Failed to open %s\n", chrdev_name);
Kent Gibsonef3c61a02020-07-08 12:15:58 +0800117 goto exit_free_name;
Linus Walleij6d591c42015-10-21 15:45:54 +0200118 }
119
120 /* Inspect this GPIO chip */
121 ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
122 if (ret == -1) {
123 ret = -errno;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100124 perror("Failed to issue CHIPINFO IOCTL\n");
125 goto exit_close_error;
Linus Walleij6d591c42015-10-21 15:45:54 +0200126 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100127 fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
128 cinfo.name, cinfo.label, cinfo.lines);
Linus Walleij6d591c42015-10-21 15:45:54 +0200129
Linus Walleij521a2ad2016-02-12 22:25:22 +0100130 /* Loop over the lines and print info */
131 for (i = 0; i < cinfo.lines; i++) {
Kent Gibson3c333c42020-09-28 08:28:01 +0800132 struct gpio_v2_line_info linfo;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100133
134 memset(&linfo, 0, sizeof(linfo));
Kent Gibson3c333c42020-09-28 08:28:01 +0800135 linfo.offset = i;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100136
Kent Gibson3c333c42020-09-28 08:28:01 +0800137 ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100138 if (ret == -1) {
139 ret = -errno;
140 perror("Failed to issue LINEINFO IOCTL\n");
141 goto exit_close_error;
142 }
Kent Gibson3c333c42020-09-28 08:28:01 +0800143 fprintf(stdout, "\tline %2d:", linfo.offset);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100144 if (linfo.name[0])
Markus Pargmannbb91d342016-02-23 08:54:46 +0100145 fprintf(stdout, " \"%s\"", linfo.name);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100146 else
147 fprintf(stdout, " unnamed");
Linus Walleij214338e2016-02-25 21:01:48 +0100148 if (linfo.consumer[0])
149 fprintf(stdout, " \"%s\"", linfo.consumer);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100150 else
Linus Walleij214338e2016-02-25 21:01:48 +0100151 fprintf(stdout, " unused");
Linus Walleij521a2ad2016-02-12 22:25:22 +0100152 if (linfo.flags) {
153 fprintf(stdout, " [");
Kent Gibson3c333c42020-09-28 08:28:01 +0800154 print_attributes(&linfo);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100155 fprintf(stdout, "]");
156 }
157 fprintf(stdout, "\n");
158
Linus Walleij6d591c42015-10-21 15:45:54 +0200159 }
160
Linus Walleij521a2ad2016-02-12 22:25:22 +0100161exit_close_error:
162 if (close(fd) == -1)
163 perror("Failed to close GPIO character device file");
Kent Gibsonef3c61a02020-07-08 12:15:58 +0800164exit_free_name:
Linus Walleij6d591c42015-10-21 15:45:54 +0200165 free(chrdev_name);
Linus Walleij6d591c42015-10-21 15:45:54 +0200166 return ret;
Linus Walleij6d591c42015-10-21 15:45:54 +0200167}
168
169void print_usage(void)
170{
171 fprintf(stderr, "Usage: lsgpio [options]...\n"
172 "List GPIO chips, lines and states\n"
173 " -n <name> List GPIOs on a named device\n"
174 " -? This helptext\n"
175 );
176}
177
178int main(int argc, char **argv)
179{
Geert Uytterhoeven691998f2016-03-25 13:36:30 +0100180 const char *device_name = NULL;
Linus Walleij6d591c42015-10-21 15:45:54 +0200181 int ret;
182 int c;
183
184 while ((c = getopt(argc, argv, "n:")) != -1) {
185 switch (c) {
186 case 'n':
187 device_name = optarg;
188 break;
189 case '?':
190 print_usage();
191 return -1;
192 }
193 }
194
195 if (device_name)
196 ret = list_device(device_name);
197 else {
198 const struct dirent *ent;
199 DIR *dp;
200
201 /* List all GPIO devices one at a time */
202 dp = opendir("/dev");
203 if (!dp) {
204 ret = -errno;
205 goto error_out;
206 }
207
208 ret = -ENOENT;
209 while (ent = readdir(dp), ent) {
210 if (check_prefix(ent->d_name, "gpiochip")) {
211 ret = list_device(ent->d_name);
212 if (ret)
213 break;
214 }
215 }
216
217 ret = 0;
218 if (closedir(dp) == -1) {
219 perror("scanning devices: Failed to close directory");
220 ret = -errno;
221 }
222 }
223error_out:
224 return ret;
225}