1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio-event-mon - monitor GPIO line events from userspace
5 * Copyright (C) 2016 Linus Walleij
8 * gpio-event-mon -n <device-name> -o <offset>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <linux/gpio.h>
27 int monitor_device(const char *device_name
,
33 struct gpioevent_request req
;
34 struct gpiohandle_data data
;
40 ret
= asprintf(&chrdev_name
, "/dev/%s", device_name
);
44 fd
= open(chrdev_name
, 0);
47 fprintf(stderr
, "Failed to open %s\n", chrdev_name
);
48 goto exit_close_error
;
51 req
.lineoffset
= line
;
52 req
.handleflags
= handleflags
;
53 req
.eventflags
= eventflags
;
54 strcpy(req
.consumer_label
, "gpio-event-mon");
56 ret
= ioctl(fd
, GPIO_GET_LINEEVENT_IOCTL
, &req
);
59 fprintf(stderr
, "Failed to issue GET EVENT "
62 goto exit_close_error
;
65 /* Read initial states */
66 ret
= ioctl(req
.fd
, GPIOHANDLE_GET_LINE_VALUES_IOCTL
, &data
);
69 fprintf(stderr
, "Failed to issue GPIOHANDLE GET LINE "
70 "VALUES IOCTL (%d)\n",
72 goto exit_close_error
;
75 fprintf(stdout
, "Monitoring line %d on %s\n", line
, device_name
);
76 fprintf(stdout
, "Initial line value: %d\n", data
.values
[0]);
79 struct gpioevent_data event
;
81 ret
= read(req
.fd
, &event
, sizeof(event
));
83 if (errno
== -EAGAIN
) {
84 fprintf(stderr
, "nothing available\n");
88 fprintf(stderr
, "Failed to read event (%d)\n",
94 if (ret
!= sizeof(event
)) {
95 fprintf(stderr
, "Reading event failed\n");
99 fprintf(stdout
, "GPIO EVENT %llu: ", event
.timestamp
);
101 case GPIOEVENT_EVENT_RISING_EDGE
:
102 fprintf(stdout
, "rising edge");
104 case GPIOEVENT_EVENT_FALLING_EDGE
:
105 fprintf(stdout
, "falling edge");
108 fprintf(stdout
, "unknown event");
110 fprintf(stdout
, "\n");
119 perror("Failed to close GPIO character device file");
124 void print_usage(void)
126 fprintf(stderr
, "Usage: gpio-event-mon [options]...\n"
127 "Listen to events on GPIO lines, 0->1 1->0\n"
128 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
129 " -o <n> Offset to monitor\n"
130 " -d Set line as open drain\n"
131 " -s Set line as open source\n"
132 " -r Listen for rising edges\n"
133 " -f Listen for falling edges\n"
134 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
135 " -? This helptext\n"
138 "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
142 int main(int argc
, char **argv
)
144 const char *device_name
= NULL
;
145 unsigned int line
= -1;
146 unsigned int loops
= 0;
147 uint32_t handleflags
= GPIOHANDLE_REQUEST_INPUT
;
148 uint32_t eventflags
= 0;
151 while ((c
= getopt(argc
, argv
, "c:n:o:dsrf?")) != -1) {
154 loops
= strtoul(optarg
, NULL
, 10);
157 device_name
= optarg
;
160 line
= strtoul(optarg
, NULL
, 10);
163 handleflags
|= GPIOHANDLE_REQUEST_OPEN_DRAIN
;
166 handleflags
|= GPIOHANDLE_REQUEST_OPEN_SOURCE
;
169 eventflags
|= GPIOEVENT_REQUEST_RISING_EDGE
;
172 eventflags
|= GPIOEVENT_REQUEST_FALLING_EDGE
;
180 if (!device_name
|| line
== -1) {
185 printf("No flags specified, listening on both rising and "
187 eventflags
= GPIOEVENT_REQUEST_BOTH_EDGES
;
189 return monitor_device(device_name
, line
, handleflags
,