Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 2 | /* |
Baruch Siach | 925baf3 | 2016-07-27 08:41:49 +0300 | [diff] [blame] | 3 | * gpio-event-mon - monitor GPIO line events from userspace |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2016 Linus Walleij |
| 6 | * |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 7 | * 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äfer | 92e70b8 | 2017-12-14 21:26:08 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 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> |
Joel Stanley | 1696784 | 2017-12-21 11:11:31 +1030 | [diff] [blame] | 24 | #include <sys/types.h> |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 25 | #include <linux/gpio.h> |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 26 | #include "gpio-utils.h" |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 27 | |
| 28 | int monitor_device(const char *device_name, |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 29 | unsigned int *lines, |
| 30 | unsigned int num_lines, |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 31 | struct gpio_v2_line_config *config, |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 32 | unsigned int loops) |
| 33 | { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 34 | struct gpio_v2_line_values values; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 35 | char *chrdev_name; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 36 | int cfd, lfd; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 37 | 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 Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 44 | cfd = open(chrdev_name, 0); |
| 45 | if (cfd == -1) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 46 | ret = -errno; |
| 47 | fprintf(stderr, "Failed to open %s\n", chrdev_name); |
Kent Gibson | df51f40 | 2020-07-08 12:16:00 +0800 | [diff] [blame] | 48 | goto exit_free_name; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 51 | ret = gpiotools_request_line(device_name, lines, num_lines, config, |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 52 | "gpio-event-mon"); |
| 53 | if (ret < 0) |
| 54 | goto exit_device_close; |
| 55 | else |
| 56 | lfd = ret; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 57 | |
| 58 | /* Read initial states */ |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 59 | values.mask = 0; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 60 | values.bits = 0; |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 61 | for (i = 0; i < num_lines; i++) |
| 62 | gpiotools_set_bit(&values.mask, i); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 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", |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 67 | ret); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 68 | goto exit_line_close; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 71 | 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 Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 88 | |
| 89 | while (1) { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 90 | struct gpio_v2_line_event event; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 91 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 92 | ret = read(lfd, &event, sizeof(event)); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 93 | 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 | } |
Kent Gibson | 2fe7c2f | 2021-01-07 12:00:19 +0800 | [diff] [blame] | 110 | fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ", |
| 111 | (uint64_t)event.timestamp_ns, event.offset, event.line_seqno, |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 112 | event.seqno); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 113 | switch (event.id) { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 114 | case GPIO_V2_LINE_EVENT_RISING_EDGE: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 115 | fprintf(stdout, "rising edge"); |
| 116 | break; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 117 | case GPIO_V2_LINE_EVENT_FALLING_EDGE: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 118 | 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 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 130 | exit_line_close: |
| 131 | if (close(lfd) == -1) |
| 132 | perror("Failed to close line file"); |
| 133 | exit_device_close: |
| 134 | if (close(cfd) == -1) |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 135 | perror("Failed to close GPIO character device file"); |
Kent Gibson | df51f40 | 2020-07-08 12:16:00 +0800 | [diff] [blame] | 136 | exit_free_name: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 137 | free(chrdev_name); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | void 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" |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 146 | " -o <n> Offset of line to monitor (may be repeated)\n" |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 147 | " -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" |
Kent Gibson | e0822cf | 2020-10-15 07:11:58 +0800 | [diff] [blame] | 151 | " -w Report the wall-clock time for events\n" |
Kent Gibson | cf048e0 | 2020-09-28 08:28:07 +0800 | [diff] [blame] | 152 | " -b <n> Debounce the line with period n microseconds\n" |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 153 | " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n" |
| 154 | " -? This helptext\n" |
| 155 | "\n" |
| 156 | "Example:\n" |
Kent Gibson | cf048e0 | 2020-09-28 08:28:07 +0800 | [diff] [blame] | 157 | "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n" |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 158 | ); |
| 159 | } |
| 160 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 161 | #define EDGE_FLAGS \ |
| 162 | (GPIO_V2_LINE_FLAG_EDGE_RISING | \ |
| 163 | GPIO_V2_LINE_FLAG_EDGE_FALLING) |
| 164 | |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 165 | int main(int argc, char **argv) |
| 166 | { |
| 167 | const char *device_name = NULL; |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 168 | unsigned int lines[GPIO_V2_LINES_MAX]; |
| 169 | unsigned int num_lines = 0; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 170 | unsigned int loops = 0; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 171 | struct gpio_v2_line_config config; |
Kent Gibson | cf048e0 | 2020-09-28 08:28:07 +0800 | [diff] [blame] | 172 | int c, attr, i; |
| 173 | unsigned long debounce_period_us = 0; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 174 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 175 | memset(&config, 0, sizeof(config)); |
| 176 | config.flags = GPIO_V2_LINE_FLAG_INPUT; |
Kent Gibson | e0822cf | 2020-10-15 07:11:58 +0800 | [diff] [blame] | 177 | while ((c = getopt(argc, argv, "c:n:o:b:dsrfw?")) != -1) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 178 | 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 Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 186 | 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 Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 192 | break; |
Kent Gibson | cf048e0 | 2020-09-28 08:28:07 +0800 | [diff] [blame] | 193 | case 'b': |
| 194 | debounce_period_us = strtoul(optarg, NULL, 10); |
| 195 | break; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 196 | case 'd': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 197 | config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 198 | break; |
| 199 | case 's': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 200 | config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 201 | break; |
| 202 | case 'r': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 203 | config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 204 | break; |
| 205 | case 'f': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 206 | config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 207 | break; |
Kent Gibson | e0822cf | 2020-10-15 07:11:58 +0800 | [diff] [blame] | 208 | case 'w': |
| 209 | config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; |
| 210 | break; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 211 | case '?': |
| 212 | print_usage(); |
| 213 | return -1; |
| 214 | } |
| 215 | } |
| 216 | |
Kent Gibson | cf048e0 | 2020-09-28 08:28:07 +0800 | [diff] [blame] | 217 | if (debounce_period_us) { |
| 218 | attr = config.num_attrs; |
| 219 | config.num_attrs++; |
| 220 | for (i = 0; i < num_lines; i++) |
| 221 | gpiotools_set_bit(&config.attrs[attr].mask, i); |
| 222 | config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; |
| 223 | config.attrs[attr].attr.debounce_period_us = debounce_period_us; |
| 224 | } |
| 225 | |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 226 | if (!device_name || num_lines == 0) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 227 | print_usage(); |
| 228 | return -1; |
| 229 | } |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 230 | if (!(config.flags & EDGE_FLAGS)) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 231 | printf("No flags specified, listening on both rising and " |
| 232 | "falling edges\n"); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame] | 233 | config.flags |= EDGE_FLAGS; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 234 | } |
Kent Gibson | 62757c3 | 2020-09-28 08:28:06 +0800 | [diff] [blame] | 235 | return monitor_device(device_name, lines, num_lines, &config, loops); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 236 | } |