gpio-event-mon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "gpio-utils.h"
  26. int monitor_device(const char *device_name,
  27. unsigned int *lines,
  28. unsigned int num_lines,
  29. struct gpio_v2_line_config *config,
  30. unsigned int loops)
  31. {
  32. struct gpio_v2_line_values values;
  33. char *chrdev_name;
  34. int cfd, lfd;
  35. int ret;
  36. int i = 0;
  37. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  38. if (ret < 0)
  39. return -ENOMEM;
  40. cfd = open(chrdev_name, 0);
  41. if (cfd == -1) {
  42. ret = -errno;
  43. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  44. goto exit_free_name;
  45. }
  46. ret = gpiotools_request_line(device_name, lines, num_lines, config,
  47. "gpio-event-mon");
  48. if (ret < 0)
  49. goto exit_device_close;
  50. else
  51. lfd = ret;
  52. /* Read initial states */
  53. values.mask = 0;
  54. values.bits = 0;
  55. for (i = 0; i < num_lines; i++)
  56. gpiotools_set_bit(&values.mask, i);
  57. ret = gpiotools_get_values(lfd, &values);
  58. if (ret < 0) {
  59. fprintf(stderr,
  60. "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
  61. ret);
  62. goto exit_line_close;
  63. }
  64. if (num_lines == 1) {
  65. fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name);
  66. fprintf(stdout, "Initial line value: %d\n",
  67. gpiotools_test_bit(values.bits, 0));
  68. } else {
  69. fprintf(stdout, "Monitoring lines %d", lines[0]);
  70. for (i = 1; i < num_lines - 1; i++)
  71. fprintf(stdout, ", %d", lines[i]);
  72. fprintf(stdout, " and %d on %s\n", lines[i], device_name);
  73. fprintf(stdout, "Initial line values: %d",
  74. gpiotools_test_bit(values.bits, 0));
  75. for (i = 1; i < num_lines - 1; i++)
  76. fprintf(stdout, ", %d",
  77. gpiotools_test_bit(values.bits, i));
  78. fprintf(stdout, " and %d\n",
  79. gpiotools_test_bit(values.bits, i));
  80. }
  81. i = 0;
  82. while (1) {
  83. struct gpio_v2_line_event event;
  84. ret = read(lfd, &event, sizeof(event));
  85. if (ret == -1) {
  86. if (errno == -EAGAIN) {
  87. fprintf(stderr, "nothing available\n");
  88. continue;
  89. } else {
  90. ret = -errno;
  91. fprintf(stderr, "Failed to read event (%d)\n",
  92. ret);
  93. break;
  94. }
  95. }
  96. if (ret != sizeof(event)) {
  97. fprintf(stderr, "Reading event failed\n");
  98. ret = -EIO;
  99. break;
  100. }
  101. fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ",
  102. (uint64_t)event.timestamp_ns, event.offset, event.line_seqno,
  103. event.seqno);
  104. switch (event.id) {
  105. case GPIO_V2_LINE_EVENT_RISING_EDGE:
  106. fprintf(stdout, "rising edge");
  107. break;
  108. case GPIO_V2_LINE_EVENT_FALLING_EDGE:
  109. fprintf(stdout, "falling edge");
  110. break;
  111. default:
  112. fprintf(stdout, "unknown event");
  113. }
  114. fprintf(stdout, "\n");
  115. i++;
  116. if (i == loops)
  117. break;
  118. }
  119. exit_line_close:
  120. if (close(lfd) == -1)
  121. perror("Failed to close line file");
  122. exit_device_close:
  123. if (close(cfd) == -1)
  124. perror("Failed to close GPIO character device file");
  125. exit_free_name:
  126. free(chrdev_name);
  127. return ret;
  128. }
  129. void print_usage(void)
  130. {
  131. fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
  132. "Listen to events on GPIO lines, 0->1 1->0\n"
  133. " -n <name> Listen on GPIOs on a named device (must be stated)\n"
  134. " -o <n> Offset of line to monitor (may be repeated)\n"
  135. " -d Set line as open drain\n"
  136. " -s Set line as open source\n"
  137. " -r Listen for rising edges\n"
  138. " -f Listen for falling edges\n"
  139. " -w Report the wall-clock time for events\n"
  140. " -t Report the hardware timestamp for events\n"
  141. " -b <n> Debounce the line with period n microseconds\n"
  142. " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
  143. " -? This helptext\n"
  144. "\n"
  145. "Example:\n"
  146. "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
  147. );
  148. }
  149. #define EDGE_FLAGS \
  150. (GPIO_V2_LINE_FLAG_EDGE_RISING | \
  151. GPIO_V2_LINE_FLAG_EDGE_FALLING)
  152. int main(int argc, char **argv)
  153. {
  154. const char *device_name = NULL;
  155. unsigned int lines[GPIO_V2_LINES_MAX];
  156. unsigned int num_lines = 0;
  157. unsigned int loops = 0;
  158. struct gpio_v2_line_config config;
  159. int c, attr, i;
  160. unsigned long debounce_period_us = 0;
  161. memset(&config, 0, sizeof(config));
  162. config.flags = GPIO_V2_LINE_FLAG_INPUT;
  163. while ((c = getopt(argc, argv, "c:n:o:b:dsrfwt?")) != -1) {
  164. switch (c) {
  165. case 'c':
  166. loops = strtoul(optarg, NULL, 10);
  167. break;
  168. case 'n':
  169. device_name = optarg;
  170. break;
  171. case 'o':
  172. if (num_lines >= GPIO_V2_LINES_MAX) {
  173. print_usage();
  174. return -1;
  175. }
  176. lines[num_lines] = strtoul(optarg, NULL, 10);
  177. num_lines++;
  178. break;
  179. case 'b':
  180. debounce_period_us = strtoul(optarg, NULL, 10);
  181. break;
  182. case 'd':
  183. config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
  184. break;
  185. case 's':
  186. config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
  187. break;
  188. case 'r':
  189. config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
  190. break;
  191. case 'f':
  192. config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
  193. break;
  194. case 'w':
  195. config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
  196. break;
  197. case 't':
  198. config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
  199. break;
  200. case '?':
  201. print_usage();
  202. return -1;
  203. }
  204. }
  205. if (debounce_period_us) {
  206. attr = config.num_attrs;
  207. config.num_attrs++;
  208. for (i = 0; i < num_lines; i++)
  209. gpiotools_set_bit(&config.attrs[attr].mask, i);
  210. config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
  211. config.attrs[attr].attr.debounce_period_us = debounce_period_us;
  212. }
  213. if (!device_name || num_lines == 0) {
  214. print_usage();
  215. return -1;
  216. }
  217. if (!(config.flags & EDGE_FLAGS)) {
  218. printf("No flags specified, listening on both rising and "
  219. "falling edges\n");
  220. config.flags |= EDGE_FLAGS;
  221. }
  222. return monitor_device(device_name, lines, num_lines, &config, loops);
  223. }