~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/gpio/gpio-watch.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
 13 #include <inttypes.h>
 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 {
 25         struct gpio_v2_line_info_changed chg;
 26         struct gpio_v2_line_info req;
 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 
 44                 req.offset = strtoul(argv[j], &end, 0);
 45                 if (*end != '\0')
 46                         goto err_usage;
 47 
 48                 ret = ioctl(fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, &req);
 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) {
 75                         case GPIO_V2_LINE_CHANGED_REQUESTED:
 76                                 event = "requested";
 77                                 break;
 78                         case GPIO_V2_LINE_CHANGED_RELEASED:
 79                                 event = "released";
 80                                 break;
 81                         case GPIO_V2_LINE_CHANGED_CONFIG:
 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 
 90                         printf("line %u: %s at %" PRIu64 "\n",
 91                                chg.info.offset, event, (uint64_t)chg.timestamp_ns);
 92                 }
 93         }
 94 
 95         return 0;
 96 
 97 err_usage:
 98         printf("%s: <gpiochip> <line0> <line1> ...\n", argv[0]);
 99         return EXIT_FAILURE;
100 }
101 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php