blob: 367c10636890a68de9f63d7b70bc351e5db9d6c4 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij97f69742016-06-02 11:38:33 +02002/*
Baruch Siach925baf32016-07-27 08:41:49 +03003 * gpio-event-mon - monitor GPIO line events from userspace
Linus Walleij97f69742016-06-02 11:38:33 +02004 *
5 * Copyright (C) 2016 Linus Walleij
6 *
Linus Walleij97f69742016-06-02 11:38:33 +02007 * Usage:
8 * gpio-event-mon -n <device-name> -o <offset>
9 */
10
11#include <unistd.h>
12#include <stdlib.h>
13#include <stdbool.h>
Jonathan Neuschäfer92e70b82017-12-14 21:26:08 +010014#include <stdint.h>
Linus Walleij97f69742016-06-02 11:38:33 +020015#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>
Joel Stanley16967842017-12-21 11:11:31 +103024#include <sys/types.h>
Linus Walleij97f69742016-06-02 11:38:33 +020025#include <linux/gpio.h>
Kent Gibson0acda972020-09-28 08:28:05 +080026#include "gpio-utils.h"
Linus Walleij97f69742016-06-02 11:38:33 +020027
28int monitor_device(const char *device_name,
Kent Gibson62757c32020-09-28 08:28:06 +080029 unsigned int *lines,
30 unsigned int num_lines,
Kent Gibson0acda972020-09-28 08:28:05 +080031 struct gpio_v2_line_config *config,
Linus Walleij97f69742016-06-02 11:38:33 +020032 unsigned int loops)
33{
Kent Gibson0acda972020-09-28 08:28:05 +080034 struct gpio_v2_line_values values;
Linus Walleij97f69742016-06-02 11:38:33 +020035 char *chrdev_name;
Kent Gibson0acda972020-09-28 08:28:05 +080036 int cfd, lfd;
Linus Walleij97f69742016-06-02 11:38:33 +020037 int ret;
38 int i = 0;
39
40 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
41 if (ret < 0)
42 return -ENOMEM;
43
Kent Gibson0acda972020-09-28 08:28:05 +080044 cfd = open(chrdev_name, 0);
45 if (cfd == -1) {
Linus Walleij97f69742016-06-02 11:38:33 +020046 ret = -errno;
47 fprintf(stderr, "Failed to open %s\n", chrdev_name);
Kent Gibsondf51f402020-07-08 12:16:00 +080048 goto exit_free_name;
Linus Walleij97f69742016-06-02 11:38:33 +020049 }
50
Kent Gibson62757c32020-09-28 08:28:06 +080051 ret = gpiotools_request_line(device_name, lines, num_lines, config,
Kent Gibson0acda972020-09-28 08:28:05 +080052 "gpio-event-mon");
53 if (ret < 0)
54 goto exit_device_close;
55 else
56 lfd = ret;
Linus Walleij97f69742016-06-02 11:38:33 +020057
58 /* Read initial states */
Kent Gibson62757c32020-09-28 08:28:06 +080059 values.mask = 0;
Kent Gibson0acda972020-09-28 08:28:05 +080060 values.bits = 0;
Kent Gibson62757c32020-09-28 08:28:06 +080061 for (i = 0; i < num_lines; i++)
62 gpiotools_set_bit(&values.mask, i);
Kent Gibson0acda972020-09-28 08:28:05 +080063 ret = gpiotools_get_values(lfd, &values);
64 if (ret < 0) {
65 fprintf(stderr,
66 "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
Linus Walleij97f69742016-06-02 11:38:33 +020067 ret);
Kent Gibson0acda972020-09-28 08:28:05 +080068 goto exit_line_close;
Linus Walleij97f69742016-06-02 11:38:33 +020069 }
70
Kent Gibson62757c32020-09-28 08:28:06 +080071 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 }
Linus Walleij97f69742016-06-02 11:38:33 +020088
Ivo Borisov Shopov230be652023-01-26 15:10:33 +020089 i = 0;
Linus Walleij97f69742016-06-02 11:38:33 +020090 while (1) {
Kent Gibson0acda972020-09-28 08:28:05 +080091 struct gpio_v2_line_event event;
Linus Walleij97f69742016-06-02 11:38:33 +020092
Kent Gibson0acda972020-09-28 08:28:05 +080093 ret = read(lfd, &event, sizeof(event));
Linus Walleij97f69742016-06-02 11:38:33 +020094 if (ret == -1) {
95 if (errno == -EAGAIN) {
96 fprintf(stderr, "nothing available\n");
97 continue;
98 } else {
99 ret = -errno;
100 fprintf(stderr, "Failed to read event (%d)\n",
101 ret);
102 break;
103 }
104 }
105
106 if (ret != sizeof(event)) {
107 fprintf(stderr, "Reading event failed\n");
108 ret = -EIO;
109 break;
110 }
Kent Gibson3fa4a032021-01-07 12:00:19 +0800111 fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ",
112 (uint64_t)event.timestamp_ns, event.offset, event.line_seqno,
Kent Gibson0acda972020-09-28 08:28:05 +0800113 event.seqno);
Linus Walleij97f69742016-06-02 11:38:33 +0200114 switch (event.id) {
Kent Gibson0acda972020-09-28 08:28:05 +0800115 case GPIO_V2_LINE_EVENT_RISING_EDGE:
Linus Walleij97f69742016-06-02 11:38:33 +0200116 fprintf(stdout, "rising edge");
117 break;
Kent Gibson0acda972020-09-28 08:28:05 +0800118 case GPIO_V2_LINE_EVENT_FALLING_EDGE:
Linus Walleij97f69742016-06-02 11:38:33 +0200119 fprintf(stdout, "falling edge");
120 break;
121 default:
122 fprintf(stdout, "unknown event");
123 }
124 fprintf(stdout, "\n");
125
126 i++;
127 if (i == loops)
128 break;
129 }
130
Kent Gibson0acda972020-09-28 08:28:05 +0800131exit_line_close:
132 if (close(lfd) == -1)
133 perror("Failed to close line file");
134exit_device_close:
135 if (close(cfd) == -1)
Linus Walleij97f69742016-06-02 11:38:33 +0200136 perror("Failed to close GPIO character device file");
Kent Gibsondf51f402020-07-08 12:16:00 +0800137exit_free_name:
Linus Walleij97f69742016-06-02 11:38:33 +0200138 free(chrdev_name);
139 return ret;
140}
141
142void print_usage(void)
143{
144 fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
145 "Listen to events on GPIO lines, 0->1 1->0\n"
146 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
Kent Gibson62757c32020-09-28 08:28:06 +0800147 " -o <n> Offset of line to monitor (may be repeated)\n"
Linus Walleij97f69742016-06-02 11:38:33 +0200148 " -d Set line as open drain\n"
149 " -s Set line as open source\n"
150 " -r Listen for rising edges\n"
151 " -f Listen for falling edges\n"
Kent Gibsoncf048e02020-09-28 08:28:07 +0800152 " -b <n> Debounce the line with period n microseconds\n"
Linus Walleij97f69742016-06-02 11:38:33 +0200153 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
154 " -? This helptext\n"
155 "\n"
156 "Example:\n"
Kent Gibsoncf048e02020-09-28 08:28:07 +0800157 "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
Linus Walleij97f69742016-06-02 11:38:33 +0200158 );
159}
160
Kent Gibson0acda972020-09-28 08:28:05 +0800161#define EDGE_FLAGS \
162 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
163 GPIO_V2_LINE_FLAG_EDGE_FALLING)
164
Linus Walleij97f69742016-06-02 11:38:33 +0200165int main(int argc, char **argv)
166{
167 const char *device_name = NULL;
Kent Gibson62757c32020-09-28 08:28:06 +0800168 unsigned int lines[GPIO_V2_LINES_MAX];
169 unsigned int num_lines = 0;
Linus Walleij97f69742016-06-02 11:38:33 +0200170 unsigned int loops = 0;
Kent Gibson0acda972020-09-28 08:28:05 +0800171 struct gpio_v2_line_config config;
Kent Gibsoncf048e02020-09-28 08:28:07 +0800172 int c, attr, i;
173 unsigned long debounce_period_us = 0;
Linus Walleij97f69742016-06-02 11:38:33 +0200174
Kent Gibson0acda972020-09-28 08:28:05 +0800175 memset(&config, 0, sizeof(config));
176 config.flags = GPIO_V2_LINE_FLAG_INPUT;
Kent Gibsoncf048e02020-09-28 08:28:07 +0800177 while ((c = getopt(argc, argv, "c:n:o:b:dsrf?")) != -1) {
Linus Walleij97f69742016-06-02 11:38:33 +0200178 switch (c) {
179 case 'c':
180 loops = strtoul(optarg, NULL, 10);
181 break;
182 case 'n':
183 device_name = optarg;
184 break;
185 case 'o':
Kent Gibson62757c32020-09-28 08:28:06 +0800186 if (num_lines >= GPIO_V2_LINES_MAX) {
187 print_usage();
188 return -1;
189 }
190 lines[num_lines] = strtoul(optarg, NULL, 10);
191 num_lines++;
Linus Walleij97f69742016-06-02 11:38:33 +0200192 break;
Kent Gibsoncf048e02020-09-28 08:28:07 +0800193 case 'b':
194 debounce_period_us = strtoul(optarg, NULL, 10);
195 break;
Linus Walleij97f69742016-06-02 11:38:33 +0200196 case 'd':
Kent Gibson0acda972020-09-28 08:28:05 +0800197 config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
Linus Walleij97f69742016-06-02 11:38:33 +0200198 break;
199 case 's':
Kent Gibson0acda972020-09-28 08:28:05 +0800200 config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
Linus Walleij97f69742016-06-02 11:38:33 +0200201 break;
202 case 'r':
Kent Gibson0acda972020-09-28 08:28:05 +0800203 config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
Linus Walleij97f69742016-06-02 11:38:33 +0200204 break;
205 case 'f':
Kent Gibson0acda972020-09-28 08:28:05 +0800206 config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
Linus Walleij97f69742016-06-02 11:38:33 +0200207 break;
208 case '?':
209 print_usage();
210 return -1;
211 }
212 }
213
Kent Gibsoncf048e02020-09-28 08:28:07 +0800214 if (debounce_period_us) {
215 attr = config.num_attrs;
216 config.num_attrs++;
217 for (i = 0; i < num_lines; i++)
218 gpiotools_set_bit(&config.attrs[attr].mask, i);
219 config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
220 config.attrs[attr].attr.debounce_period_us = debounce_period_us;
221 }
222
Kent Gibson62757c32020-09-28 08:28:06 +0800223 if (!device_name || num_lines == 0) {
Linus Walleij97f69742016-06-02 11:38:33 +0200224 print_usage();
225 return -1;
226 }
Kent Gibson0acda972020-09-28 08:28:05 +0800227 if (!(config.flags & EDGE_FLAGS)) {
Linus Walleij97f69742016-06-02 11:38:33 +0200228 printf("No flags specified, listening on both rising and "
229 "falling edges\n");
Kent Gibson0acda972020-09-28 08:28:05 +0800230 config.flags |= EDGE_FLAGS;
Linus Walleij97f69742016-06-02 11:38:33 +0200231 }
Kent Gibson62757c32020-09-28 08:28:06 +0800232 return monitor_device(device_name, lines, num_lines, &config, loops);
Linus Walleij97f69742016-06-02 11:38:33 +0200233}