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, |
| 29 | unsigned int line, |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 30 | struct gpio_v2_line_config *config, |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 31 | unsigned int loops) |
| 32 | { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 33 | struct gpio_v2_line_values values; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 34 | char *chrdev_name; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 35 | int cfd, lfd; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 36 | int ret; |
| 37 | int i = 0; |
| 38 | |
| 39 | ret = asprintf(&chrdev_name, "/dev/%s", device_name); |
| 40 | if (ret < 0) |
| 41 | return -ENOMEM; |
| 42 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 43 | cfd = open(chrdev_name, 0); |
| 44 | if (cfd == -1) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 45 | ret = -errno; |
| 46 | fprintf(stderr, "Failed to open %s\n", chrdev_name); |
Kent Gibson | df51f40 | 2020-07-08 12:16:00 +0800 | [diff] [blame] | 47 | goto exit_free_name; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 50 | ret = gpiotools_request_line(device_name, &line, 1, config, |
| 51 | "gpio-event-mon"); |
| 52 | if (ret < 0) |
| 53 | goto exit_device_close; |
| 54 | else |
| 55 | lfd = ret; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 56 | |
| 57 | /* Read initial states */ |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 58 | values.mask = 1; |
| 59 | values.bits = 0; |
| 60 | ret = gpiotools_get_values(lfd, &values); |
| 61 | if (ret < 0) { |
| 62 | fprintf(stderr, |
| 63 | "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n", |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 64 | ret); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 65 | goto exit_line_close; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | fprintf(stdout, "Monitoring line %d on %s\n", line, device_name); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 69 | fprintf(stdout, "Initial line value: %d\n", |
| 70 | gpiotools_test_bit(values.bits, 0)); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 71 | |
| 72 | while (1) { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 73 | struct gpio_v2_line_event event; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 74 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 75 | ret = read(lfd, &event, sizeof(event)); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 76 | if (ret == -1) { |
| 77 | if (errno == -EAGAIN) { |
| 78 | fprintf(stderr, "nothing available\n"); |
| 79 | continue; |
| 80 | } else { |
| 81 | ret = -errno; |
| 82 | fprintf(stderr, "Failed to read event (%d)\n", |
| 83 | ret); |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (ret != sizeof(event)) { |
| 89 | fprintf(stderr, "Reading event failed\n"); |
| 90 | ret = -EIO; |
| 91 | break; |
| 92 | } |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 93 | fprintf(stdout, "GPIO EVENT at %llu on line %d (%d|%d) ", |
| 94 | event.timestamp_ns, event.offset, event.line_seqno, |
| 95 | event.seqno); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 96 | switch (event.id) { |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 97 | case GPIO_V2_LINE_EVENT_RISING_EDGE: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 98 | fprintf(stdout, "rising edge"); |
| 99 | break; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 100 | case GPIO_V2_LINE_EVENT_FALLING_EDGE: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 101 | fprintf(stdout, "falling edge"); |
| 102 | break; |
| 103 | default: |
| 104 | fprintf(stdout, "unknown event"); |
| 105 | } |
| 106 | fprintf(stdout, "\n"); |
| 107 | |
| 108 | i++; |
| 109 | if (i == loops) |
| 110 | break; |
| 111 | } |
| 112 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 113 | exit_line_close: |
| 114 | if (close(lfd) == -1) |
| 115 | perror("Failed to close line file"); |
| 116 | exit_device_close: |
| 117 | if (close(cfd) == -1) |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 118 | perror("Failed to close GPIO character device file"); |
Kent Gibson | df51f40 | 2020-07-08 12:16:00 +0800 | [diff] [blame] | 119 | exit_free_name: |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 120 | free(chrdev_name); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | void print_usage(void) |
| 125 | { |
| 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" |
| 136 | "\n" |
| 137 | "Example:\n" |
| 138 | "gpio-event-mon -n gpiochip0 -o 4 -r -f\n" |
| 139 | ); |
| 140 | } |
| 141 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 142 | #define EDGE_FLAGS \ |
| 143 | (GPIO_V2_LINE_FLAG_EDGE_RISING | \ |
| 144 | GPIO_V2_LINE_FLAG_EDGE_FALLING) |
| 145 | |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 146 | int main(int argc, char **argv) |
| 147 | { |
| 148 | const char *device_name = NULL; |
| 149 | unsigned int line = -1; |
| 150 | unsigned int loops = 0; |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 151 | struct gpio_v2_line_config config; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 152 | int c; |
| 153 | |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 154 | memset(&config, 0, sizeof(config)); |
| 155 | config.flags = GPIO_V2_LINE_FLAG_INPUT; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 156 | while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) { |
| 157 | switch (c) { |
| 158 | case 'c': |
| 159 | loops = strtoul(optarg, NULL, 10); |
| 160 | break; |
| 161 | case 'n': |
| 162 | device_name = optarg; |
| 163 | break; |
| 164 | case 'o': |
| 165 | line = strtoul(optarg, NULL, 10); |
| 166 | break; |
| 167 | case 'd': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 168 | config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 169 | break; |
| 170 | case 's': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 171 | config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 172 | break; |
| 173 | case 'r': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 174 | config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 175 | break; |
| 176 | case 'f': |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 177 | config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 178 | break; |
| 179 | case '?': |
| 180 | print_usage(); |
| 181 | return -1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (!device_name || line == -1) { |
| 186 | print_usage(); |
| 187 | return -1; |
| 188 | } |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 189 | if (!(config.flags & EDGE_FLAGS)) { |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 190 | printf("No flags specified, listening on both rising and " |
| 191 | "falling edges\n"); |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 192 | config.flags |= EDGE_FLAGS; |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 193 | } |
Kent Gibson | 0acda97 | 2020-09-28 08:28:05 +0800 | [diff] [blame^] | 194 | return monitor_device(device_name, line, &config, loops); |
Linus Walleij | 97f6974 | 2016-06-02 11:38:33 +0200 | [diff] [blame] | 195 | } |