blob: 84ae1039b0a877a2b293e1d0132ed9d5e2a20761 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * gpio-event-mon - monitor GPIO line events from userspace
4 *
5 * Copyright (C) 2016 Linus Walleij
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * Usage:
8 * gpio-event-mon -n <device-name> -o <offset>
9 */
10
11#include <unistd.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <dirent.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <inttypes.h>
23#include <sys/ioctl.h>
24#include <sys/types.h>
25#include <linux/gpio.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020026#include "gpio-utils.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
28int monitor_device(const char *device_name,
Olivier Deprez157378f2022-04-04 15:47:50 +020029 unsigned int *lines,
30 unsigned int num_lines,
31 struct gpio_v2_line_config *config,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 unsigned int loops)
33{
Olivier Deprez157378f2022-04-04 15:47:50 +020034 struct gpio_v2_line_values values;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 char *chrdev_name;
Olivier Deprez157378f2022-04-04 15:47:50 +020036 int cfd, lfd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 int ret;
38 int i = 0;
39
40 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
41 if (ret < 0)
42 return -ENOMEM;
43
Olivier Deprez157378f2022-04-04 15:47:50 +020044 cfd = open(chrdev_name, 0);
45 if (cfd == -1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 ret = -errno;
47 fprintf(stderr, "Failed to open %s\n", chrdev_name);
Olivier Deprez157378f2022-04-04 15:47:50 +020048 goto exit_free_name;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 }
50
Olivier Deprez157378f2022-04-04 15:47:50 +020051 ret = gpiotools_request_line(device_name, lines, num_lines, config,
52 "gpio-event-mon");
53 if (ret < 0)
54 goto exit_device_close;
55 else
56 lfd = ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057
58 /* Read initial states */
Olivier Deprez157378f2022-04-04 15:47:50 +020059 values.mask = 0;
60 values.bits = 0;
61 for (i = 0; i < num_lines; i++)
62 gpiotools_set_bit(&values.mask, i);
63 ret = gpiotools_get_values(lfd, &values);
64 if (ret < 0) {
65 fprintf(stderr,
66 "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 ret);
Olivier Deprez157378f2022-04-04 15:47:50 +020068 goto exit_line_close;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 }
70
Olivier Deprez157378f2022-04-04 15:47:50 +020071 if (num_lines == 1) {
72 fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name);
73 fprintf(stdout, "Initial line value: %d\n",
74 gpiotools_test_bit(values.bits, 0));
75 } else {
76 fprintf(stdout, "Monitoring lines %d", lines[0]);
77 for (i = 1; i < num_lines - 1; i++)
78 fprintf(stdout, ", %d", lines[i]);
79 fprintf(stdout, " and %d on %s\n", lines[i], device_name);
80 fprintf(stdout, "Initial line values: %d",
81 gpiotools_test_bit(values.bits, 0));
82 for (i = 1; i < num_lines - 1; i++)
83 fprintf(stdout, ", %d",
84 gpiotools_test_bit(values.bits, i));
85 fprintf(stdout, " and %d\n",
86 gpiotools_test_bit(values.bits, i));
87 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088
89 while (1) {
Olivier Deprez157378f2022-04-04 15:47:50 +020090 struct gpio_v2_line_event event;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091
Olivier Deprez157378f2022-04-04 15:47:50 +020092 ret = read(lfd, &event, sizeof(event));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093 if (ret == -1) {
94 if (errno == -EAGAIN) {
95 fprintf(stderr, "nothing available\n");
96 continue;
97 } else {
98 ret = -errno;
99 fprintf(stderr, "Failed to read event (%d)\n",
100 ret);
101 break;
102 }
103 }
104
105 if (ret != sizeof(event)) {
106 fprintf(stderr, "Reading event failed\n");
107 ret = -EIO;
108 break;
109 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ",
111 (uint64_t)event.timestamp_ns, event.offset, event.line_seqno,
112 event.seqno);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113 switch (event.id) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200114 case GPIO_V2_LINE_EVENT_RISING_EDGE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 fprintf(stdout, "rising edge");
116 break;
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 case GPIO_V2_LINE_EVENT_FALLING_EDGE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 fprintf(stdout, "falling edge");
119 break;
120 default:
121 fprintf(stdout, "unknown event");
122 }
123 fprintf(stdout, "\n");
124
125 i++;
126 if (i == loops)
127 break;
128 }
129
Olivier Deprez157378f2022-04-04 15:47:50 +0200130exit_line_close:
131 if (close(lfd) == -1)
132 perror("Failed to close line file");
133exit_device_close:
134 if (close(cfd) == -1)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135 perror("Failed to close GPIO character device file");
Olivier Deprez157378f2022-04-04 15:47:50 +0200136exit_free_name:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137 free(chrdev_name);
138 return ret;
139}
140
141void print_usage(void)
142{
143 fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
144 "Listen to events on GPIO lines, 0->1 1->0\n"
145 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 " -o <n> Offset of line to monitor (may be repeated)\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 " -d Set line as open drain\n"
148 " -s Set line as open source\n"
149 " -r Listen for rising edges\n"
150 " -f Listen for falling edges\n"
Olivier Deprez157378f2022-04-04 15:47:50 +0200151 " -b <n> Debounce the line with period n microseconds\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000152 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
153 " -? This helptext\n"
154 "\n"
155 "Example:\n"
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157 );
158}
159
Olivier Deprez157378f2022-04-04 15:47:50 +0200160#define EDGE_FLAGS \
161 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
162 GPIO_V2_LINE_FLAG_EDGE_FALLING)
163
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164int main(int argc, char **argv)
165{
166 const char *device_name = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200167 unsigned int lines[GPIO_V2_LINES_MAX];
168 unsigned int num_lines = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 unsigned int loops = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200170 struct gpio_v2_line_config config;
171 int c, attr, i;
172 unsigned long debounce_period_us = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173
Olivier Deprez157378f2022-04-04 15:47:50 +0200174 memset(&config, 0, sizeof(config));
175 config.flags = GPIO_V2_LINE_FLAG_INPUT;
176 while ((c = getopt(argc, argv, "c:n:o:b:dsrf?")) != -1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177 switch (c) {
178 case 'c':
179 loops = strtoul(optarg, NULL, 10);
180 break;
181 case 'n':
182 device_name = optarg;
183 break;
184 case 'o':
Olivier Deprez157378f2022-04-04 15:47:50 +0200185 if (num_lines >= GPIO_V2_LINES_MAX) {
186 print_usage();
187 return -1;
188 }
189 lines[num_lines] = strtoul(optarg, NULL, 10);
190 num_lines++;
191 break;
192 case 'b':
193 debounce_period_us = strtoul(optarg, NULL, 10);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194 break;
195 case 'd':
Olivier Deprez157378f2022-04-04 15:47:50 +0200196 config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 break;
198 case 's':
Olivier Deprez157378f2022-04-04 15:47:50 +0200199 config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 break;
201 case 'r':
Olivier Deprez157378f2022-04-04 15:47:50 +0200202 config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 break;
204 case 'f':
Olivier Deprez157378f2022-04-04 15:47:50 +0200205 config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 break;
207 case '?':
208 print_usage();
209 return -1;
210 }
211 }
212
Olivier Deprez157378f2022-04-04 15:47:50 +0200213 if (debounce_period_us) {
214 attr = config.num_attrs;
215 config.num_attrs++;
216 for (i = 0; i < num_lines; i++)
217 gpiotools_set_bit(&config.attrs[attr].mask, i);
218 config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
219 config.attrs[attr].attr.debounce_period_us = debounce_period_us;
220 }
221
222 if (!device_name || num_lines == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 print_usage();
224 return -1;
225 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200226 if (!(config.flags & EDGE_FLAGS)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 printf("No flags specified, listening on both rising and "
228 "falling edges\n");
Olivier Deprez157378f2022-04-04 15:47:50 +0200229 config.flags |= EDGE_FLAGS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200231 return monitor_device(device_name, lines, num_lines, &config, loops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232}