Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * gpio-watch - monitor unrequested lines for property changes using the |
| 4 | * character device |
| 5 | * |
| 6 | * Copyright (C) 2019 BayLibre SAS |
| 7 | * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com> |
| 8 | */ |
| 9 | |
| 10 | #include <ctype.h> |
| 11 | #include <errno.h> |
| 12 | #include <fcntl.h> |
Kent Gibson | 1fc7c1e | 2021-01-07 12:00:20 +0800 | [diff] [blame] | 13 | #include <inttypes.h> |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 14 | #include <linux/gpio.h> |
| 15 | #include <poll.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | int main(int argc, char **argv) |
| 24 | { |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 25 | struct gpio_v2_line_info_changed chg; |
| 26 | struct gpio_v2_line_info req; |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 27 | struct pollfd pfd; |
| 28 | int fd, i, j, ret; |
| 29 | char *event, *end; |
| 30 | ssize_t rd; |
| 31 | |
| 32 | if (argc < 3) |
| 33 | goto err_usage; |
| 34 | |
| 35 | fd = open(argv[1], O_RDWR | O_CLOEXEC); |
| 36 | if (fd < 0) { |
| 37 | perror("unable to open gpiochip"); |
| 38 | return EXIT_FAILURE; |
| 39 | } |
| 40 | |
| 41 | for (i = 0, j = 2; i < argc - 2; i++, j++) { |
| 42 | memset(&req, 0, sizeof(req)); |
| 43 | |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 44 | req.offset = strtoul(argv[j], &end, 0); |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 45 | if (*end != '\0') |
| 46 | goto err_usage; |
| 47 | |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 48 | ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req); |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 49 | if (ret) { |
| 50 | perror("unable to set up line watch"); |
| 51 | return EXIT_FAILURE; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pfd.fd = fd; |
| 56 | pfd.events = POLLIN | POLLPRI; |
| 57 | |
| 58 | for (;;) { |
| 59 | ret = poll(&pfd, 1, 5000); |
| 60 | if (ret < 0) { |
| 61 | perror("error polling the linechanged fd"); |
| 62 | return EXIT_FAILURE; |
| 63 | } else if (ret > 0) { |
| 64 | memset(&chg, 0, sizeof(chg)); |
| 65 | rd = read(pfd.fd, &chg, sizeof(chg)); |
| 66 | if (rd < 0 || rd != sizeof(chg)) { |
| 67 | if (rd != sizeof(chg)) |
| 68 | errno = EIO; |
| 69 | |
| 70 | perror("error reading line change event"); |
| 71 | return EXIT_FAILURE; |
| 72 | } |
| 73 | |
| 74 | switch (chg.event_type) { |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 75 | case GPIO_V2_LINE_CHANGED_REQUESTED: |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 76 | event = "requested"; |
| 77 | break; |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 78 | case GPIO_V2_LINE_CHANGED_RELEASED: |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 79 | event = "released"; |
| 80 | break; |
Kent Gibson | e86a863 | 2020-09-28 08:28:02 +0800 | [diff] [blame] | 81 | case GPIO_V2_LINE_CHANGED_CONFIG: |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 82 | event = "config changed"; |
| 83 | break; |
| 84 | default: |
| 85 | fprintf(stderr, |
| 86 | "invalid event type received from the kernel\n"); |
| 87 | return EXIT_FAILURE; |
| 88 | } |
| 89 | |
Kent Gibson | 1fc7c1e | 2021-01-07 12:00:20 +0800 | [diff] [blame] | 90 | printf("line %u: %s at %" PRIu64 "\n", |
| 91 | chg.info.offset, event, (uint64_t)chg.timestamp_ns); |
Bartosz Golaszewski | 33f0c47 | 2019-11-27 10:17:54 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | |
| 97 | err_usage: |
| 98 | printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]); |
| 99 | return EXIT_FAILURE; |
| 100 | } |