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>
13 #include <linux/gpio.h>
19 #include <sys/ioctl.h>
22 int main(int argc, char **argv)
24 struct gpioline_info_changed chg;
25 struct gpioline_info req;
34 fd = open(argv[1], O_RDWR | O_CLOEXEC);
36 perror("unable to open gpiochip");
40 for (i = 0, j = 2; i < argc - 2; i++, j++) {
41 memset(&req, 0, sizeof(req));
43 req.line_offset = strtoul(argv[j], &end, 0);
47 ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req);
49 perror("unable to set up line watch");
55 pfd.events = POLLIN | POLLPRI;
58 ret = poll(&pfd, 1, 5000);
60 perror("error polling the linechanged fd");
63 memset(&chg, 0, sizeof(chg));
64 rd = read(pfd.fd, &chg, sizeof(chg));
65 if (rd < 0 || rd != sizeof(chg)) {
66 if (rd != sizeof(chg))
69 perror("error reading line change event");
73 switch (chg.event_type) {
74 case GPIOLINE_CHANGED_REQUESTED:
77 case GPIOLINE_CHANGED_RELEASED:
80 case GPIOLINE_CHANGED_CONFIG:
81 event = "config changed";
85 "invalid event type received from the kernel\n");
89 printf("line %u: %s at %llu\n",
90 chg.info.line_offset, event, chg.timestamp);
97 printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]);