GNU Linux-libre 4.14.332-gnu1
[releases.git] / tools / gpio / gpio-event-mon.c
1 /*
2  * gpio-event-mon - monitor GPIO line events from userspace
3  *
4  * Copyright (C) 2016 Linus Walleij
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * Usage:
11  *      gpio-event-mon -n <device-name> -o <offset>
12  */
13
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17 #include <stdio.h>
18 #include <dirent.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <poll.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <linux/gpio.h>
28
29 int monitor_device(const char *device_name,
30                    unsigned int line,
31                    u_int32_t handleflags,
32                    u_int32_t eventflags,
33                    unsigned int loops)
34 {
35         struct gpioevent_request req;
36         struct gpiohandle_data data;
37         char *chrdev_name;
38         int fd;
39         int ret;
40         int i = 0;
41
42         ret = asprintf(&chrdev_name, "/dev/%s", device_name);
43         if (ret < 0)
44                 return -ENOMEM;
45
46         fd = open(chrdev_name, 0);
47         if (fd == -1) {
48                 ret = -errno;
49                 fprintf(stderr, "Failed to open %s\n", chrdev_name);
50                 goto exit_close_error;
51         }
52
53         req.lineoffset = line;
54         req.handleflags = handleflags;
55         req.eventflags = eventflags;
56         strcpy(req.consumer_label, "gpio-event-mon");
57
58         ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
59         if (ret == -1) {
60                 ret = -errno;
61                 fprintf(stderr, "Failed to issue GET EVENT "
62                         "IOCTL (%d)\n",
63                         ret);
64                 goto exit_close_error;
65         }
66
67         /* Read initial states */
68         ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
69         if (ret == -1) {
70                 ret = -errno;
71                 fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
72                         "VALUES IOCTL (%d)\n",
73                         ret);
74                 goto exit_close_error;
75         }
76
77         fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
78         fprintf(stdout, "Initial line value: %d\n", data.values[0]);
79
80         while (1) {
81                 struct gpioevent_data event;
82
83                 ret = read(req.fd, &event, sizeof(event));
84                 if (ret == -1) {
85                         if (errno == -EAGAIN) {
86                                 fprintf(stderr, "nothing available\n");
87                                 continue;
88                         } else {
89                                 ret = -errno;
90                                 fprintf(stderr, "Failed to read event (%d)\n",
91                                         ret);
92                                 break;
93                         }
94                 }
95
96                 if (ret != sizeof(event)) {
97                         fprintf(stderr, "Reading event failed\n");
98                         ret = -EIO;
99                         break;
100                 }
101                 fprintf(stdout, "GPIO EVENT %" PRIu64 ": ", event.timestamp);
102                 switch (event.id) {
103                 case GPIOEVENT_EVENT_RISING_EDGE:
104                         fprintf(stdout, "rising edge");
105                         break;
106                 case GPIOEVENT_EVENT_FALLING_EDGE:
107                         fprintf(stdout, "falling edge");
108                         break;
109                 default:
110                         fprintf(stdout, "unknown event");
111                 }
112                 fprintf(stdout, "\n");
113
114                 i++;
115                 if (i == loops)
116                         break;
117         }
118
119 exit_close_error:
120         if (close(fd) == -1)
121                 perror("Failed to close GPIO character device file");
122         free(chrdev_name);
123         return ret;
124 }
125
126 void print_usage(void)
127 {
128         fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
129                 "Listen to events on GPIO lines, 0->1 1->0\n"
130                 "  -n <name>  Listen on GPIOs on a named device (must be stated)\n"
131                 "  -o <n>     Offset to monitor\n"
132                 "  -d         Set line as open drain\n"
133                 "  -s         Set line as open source\n"
134                 "  -r         Listen for rising edges\n"
135                 "  -f         Listen for falling edges\n"
136                 " [-c <n>]    Do <n> loops (optional, infinite loop if not stated)\n"
137                 "  -?         This helptext\n"
138                 "\n"
139                 "Example:\n"
140                 "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
141         );
142 }
143
144 int main(int argc, char **argv)
145 {
146         const char *device_name = NULL;
147         unsigned int line = -1;
148         unsigned int loops = 0;
149         u_int32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
150         u_int32_t eventflags = 0;
151         int c;
152
153         while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
154                 switch (c) {
155                 case 'c':
156                         loops = strtoul(optarg, NULL, 10);
157                         break;
158                 case 'n':
159                         device_name = optarg;
160                         break;
161                 case 'o':
162                         line = strtoul(optarg, NULL, 10);
163                         break;
164                 case 'd':
165                         handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
166                         break;
167                 case 's':
168                         handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
169                         break;
170                 case 'r':
171                         eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
172                         break;
173                 case 'f':
174                         eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
175                         break;
176                 case '?':
177                         print_usage();
178                         return -1;
179                 }
180         }
181
182         if (!device_name || line == -1) {
183                 print_usage();
184                 return -1;
185         }
186         if (!eventflags) {
187                 printf("No flags specified, listening on both rising and "
188                        "falling edges\n");
189                 eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
190         }
191         return monitor_device(device_name, line, handleflags,
192                               eventflags, loops);
193 }