1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio-watch - monitor unrequested lines for property changes using the
6 * Copyright (C) 2019 BayLibre SAS
7 * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
14 #include <linux/gpio.h>
20 #include <sys/ioctl.h>
23 int main(int argc, char **argv)
25 struct gpio_v2_line_info_changed chg;
26 struct gpio_v2_line_info req;
35 fd = open(argv[1], O_RDWR | O_CLOEXEC);
37 perror("unable to open gpiochip");
41 for (i = 0, j = 2; i < argc - 2; i++, j++) {
42 memset(&req, 0, sizeof(req));
44 req.offset = strtoul(argv[j], &end, 0);
48 ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req);
50 perror("unable to set up line watch");
56 pfd.events = POLLIN | POLLPRI;
59 ret = poll(&pfd, 1, 5000);
61 perror("error polling the linechanged fd");
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))
70 perror("error reading line change event");
74 switch (chg.event_type) {
75 case GPIO_V2_LINE_CHANGED_REQUESTED:
78 case GPIO_V2_LINE_CHANGED_RELEASED:
81 case GPIO_V2_LINE_CHANGED_CONFIG:
82 event = "config changed";
86 "invalid event type received from the kernel\n");
90 printf("line %u: %s at %" PRIu64 "\n",
91 chg.info.offset, event, (uint64_t)chg.timestamp_ns);
98 printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]);