gpio-event-mon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * gpio-event-mon - monitor GPIO line events from userspace
  4. *
  5. * Copyright (C) 2016 Linus Walleij
  6. *
  7. * Usage:
  8. * gpio-event-mon -n <device-name> -o <offset>
  9. */
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <poll.h>
  19. #include <fcntl.h>
  20. #include <getopt.h>
  21. #include <inttypes.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <linux/gpio.h>
  25. int monitor_device(const char *device_name,
  26. unsigned int line,
  27. uint32_t handleflags,
  28. uint32_t eventflags,
  29. unsigned int loops)
  30. {
  31. struct gpioevent_request req;
  32. struct gpiohandle_data data;
  33. char *chrdev_name;
  34. int fd;
  35. int ret;
  36. int i = 0;
  37. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  38. if (ret < 0)
  39. return -ENOMEM;
  40. fd = open(chrdev_name, 0);
  41. if (fd == -1) {
  42. ret = -errno;
  43. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  44. goto exit_close_error;
  45. }
  46. req.lineoffset = line;
  47. req.handleflags = handleflags;
  48. req.eventflags = eventflags;
  49. strcpy(req.consumer_label, "gpio-event-mon");
  50. ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
  51. if (ret == -1) {
  52. ret = -errno;
  53. fprintf(stderr, "Failed to issue GET EVENT "
  54. "IOCTL (%d)\n",
  55. ret);
  56. goto exit_close_error;
  57. }
  58. /* Read initial states */
  59. ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  60. if (ret == -1) {
  61. ret = -errno;
  62. fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
  63. "VALUES IOCTL (%d)\n",
  64. ret);
  65. goto exit_close_error;
  66. }
  67. fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
  68. fprintf(stdout, "Initial line value: %d\n", data.values[0]);
  69. while (1) {
  70. struct gpioevent_data event;
  71. ret = read(req.fd, &event, sizeof(event));
  72. if (ret == -1) {
  73. if (errno == -EAGAIN) {
  74. fprintf(stderr, "nothing available\n");
  75. continue;
  76. } else {
  77. ret = -errno;
  78. fprintf(stderr, "Failed to read event (%d)\n",
  79. ret);
  80. break;
  81. }
  82. }
  83. if (ret != sizeof(event)) {
  84. fprintf(stderr, "Reading event failed\n");
  85. ret = -EIO;
  86. break;
  87. }
  88. fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp);
  89. switch (event.id) {
  90. case GPIOEVENT_EVENT_RISING_EDGE:
  91. fprintf(stdout, "rising edge");
  92. break;
  93. case GPIOEVENT_EVENT_FALLING_EDGE:
  94. fprintf(stdout, "falling edge");
  95. break;
  96. default:
  97. fprintf(stdout, "unknown event");
  98. }
  99. fprintf(stdout, "\n");
  100. i++;
  101. if (i == loops)
  102. break;
  103. }
  104. exit_close_error:
  105. if (close(fd) == -1)
  106. perror("Failed to close GPIO character device file");
  107. free(chrdev_name);
  108. return ret;
  109. }
  110. void print_usage(void)
  111. {
  112. fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
  113. "Listen to events on GPIO lines, 0->1 1->0\n"
  114. " -n <name> Listen on GPIOs on a named device (must be stated)\n"
  115. " -o <n> Offset to monitor\n"
  116. " -d Set line as open drain\n"
  117. " -s Set line as open source\n"
  118. " -r Listen for rising edges\n"
  119. " -f Listen for falling edges\n"
  120. " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
  121. " -? This helptext\n"
  122. "\n"
  123. "Example:\n"
  124. "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
  125. );
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. const char *device_name = NULL;
  130. unsigned int line = -1;
  131. unsigned int loops = 0;
  132. uint32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
  133. uint32_t eventflags = 0;
  134. int c;
  135. while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
  136. switch (c) {
  137. case 'c':
  138. loops = strtoul(optarg, NULL, 10);
  139. break;
  140. case 'n':
  141. device_name = optarg;
  142. break;
  143. case 'o':
  144. line = strtoul(optarg, NULL, 10);
  145. break;
  146. case 'd':
  147. handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
  148. break;
  149. case 's':
  150. handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
  151. break;
  152. case 'r':
  153. eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
  154. break;
  155. case 'f':
  156. eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
  157. break;
  158. case '?':
  159. print_usage();
  160. return -1;
  161. }
  162. }
  163. if (!device_name || line == -1) {
  164. print_usage();
  165. return -1;
  166. }
  167. if (!eventflags) {
  168. printf("No flags specified, listening on both rising and "
  169. "falling edges\n");
  170. eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
  171. }
  172. return monitor_device(device_name, line, handleflags,
  173. eventflags, loops);
  174. }