lsgpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lsgpio - example on how to list the GPIO lines on a system
  4. *
  5. * Copyright (C) 2015 Linus Walleij
  6. *
  7. * Usage:
  8. * lsgpio <-n device-name>
  9. */
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <poll.h>
  18. #include <fcntl.h>
  19. #include <getopt.h>
  20. #include <sys/ioctl.h>
  21. #include <linux/gpio.h>
  22. #include "gpio-utils.h"
  23. struct gpio_flag {
  24. char *name;
  25. unsigned long long mask;
  26. };
  27. struct gpio_flag flagnames[] = {
  28. {
  29. .name = "used",
  30. .mask = GPIO_V2_LINE_FLAG_USED,
  31. },
  32. {
  33. .name = "input",
  34. .mask = GPIO_V2_LINE_FLAG_INPUT,
  35. },
  36. {
  37. .name = "output",
  38. .mask = GPIO_V2_LINE_FLAG_OUTPUT,
  39. },
  40. {
  41. .name = "active-low",
  42. .mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
  43. },
  44. {
  45. .name = "open-drain",
  46. .mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
  47. },
  48. {
  49. .name = "open-source",
  50. .mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
  51. },
  52. {
  53. .name = "pull-up",
  54. .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
  55. },
  56. {
  57. .name = "pull-down",
  58. .mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
  59. },
  60. {
  61. .name = "bias-disabled",
  62. .mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
  63. },
  64. {
  65. .name = "clock-realtime",
  66. .mask = GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME,
  67. },
  68. };
  69. static void print_attributes(struct gpio_v2_line_info *info)
  70. {
  71. int i;
  72. const char *field_format = "%s";
  73. for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
  74. if (info->flags & flagnames[i].mask) {
  75. fprintf(stdout, field_format, flagnames[i].name);
  76. field_format = ", %s";
  77. }
  78. }
  79. if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
  80. (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
  81. fprintf(stdout, field_format, "both-edges");
  82. else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
  83. fprintf(stdout, field_format, "rising-edge");
  84. else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
  85. fprintf(stdout, field_format, "falling-edge");
  86. for (i = 0; i < info->num_attrs; i++) {
  87. if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
  88. fprintf(stdout, ", debounce_period=%dusec",
  89. info->attrs[i].debounce_period_us);
  90. }
  91. }
  92. int list_device(const char *device_name)
  93. {
  94. struct gpiochip_info cinfo;
  95. char *chrdev_name;
  96. int fd;
  97. int ret;
  98. int i;
  99. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  100. if (ret < 0)
  101. return -ENOMEM;
  102. fd = open(chrdev_name, 0);
  103. if (fd == -1) {
  104. ret = -errno;
  105. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  106. goto exit_free_name;
  107. }
  108. /* Inspect this GPIO chip */
  109. ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
  110. if (ret == -1) {
  111. ret = -errno;
  112. perror("Failed to issue CHIPINFO IOCTL\n");
  113. goto exit_close_error;
  114. }
  115. fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
  116. cinfo.name, cinfo.label, cinfo.lines);
  117. /* Loop over the lines and print info */
  118. for (i = 0; i < cinfo.lines; i++) {
  119. struct gpio_v2_line_info linfo;
  120. memset(&linfo, 0, sizeof(linfo));
  121. linfo.offset = i;
  122. ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
  123. if (ret == -1) {
  124. ret = -errno;
  125. perror("Failed to issue LINEINFO IOCTL\n");
  126. goto exit_close_error;
  127. }
  128. fprintf(stdout, "\tline %2d:", linfo.offset);
  129. if (linfo.name[0])
  130. fprintf(stdout, " \"%s\"", linfo.name);
  131. else
  132. fprintf(stdout, " unnamed");
  133. if (linfo.consumer[0])
  134. fprintf(stdout, " \"%s\"", linfo.consumer);
  135. else
  136. fprintf(stdout, " unused");
  137. if (linfo.flags) {
  138. fprintf(stdout, " [");
  139. print_attributes(&linfo);
  140. fprintf(stdout, "]");
  141. }
  142. fprintf(stdout, "\n");
  143. }
  144. exit_close_error:
  145. if (close(fd) == -1)
  146. perror("Failed to close GPIO character device file");
  147. exit_free_name:
  148. free(chrdev_name);
  149. return ret;
  150. }
  151. void print_usage(void)
  152. {
  153. fprintf(stderr, "Usage: lsgpio [options]...\n"
  154. "List GPIO chips, lines and states\n"
  155. " -n <name> List GPIOs on a named device\n"
  156. " -? This helptext\n"
  157. );
  158. }
  159. int main(int argc, char **argv)
  160. {
  161. const char *device_name = NULL;
  162. int ret;
  163. int c;
  164. while ((c = getopt(argc, argv, "n:")) != -1) {
  165. switch (c) {
  166. case 'n':
  167. device_name = optarg;
  168. break;
  169. case '?':
  170. print_usage();
  171. return -1;
  172. }
  173. }
  174. if (device_name)
  175. ret = list_device(device_name);
  176. else {
  177. const struct dirent *ent;
  178. DIR *dp;
  179. /* List all GPIO devices one at a time */
  180. dp = opendir("/dev");
  181. if (!dp) {
  182. ret = -errno;
  183. goto error_out;
  184. }
  185. ret = -ENOENT;
  186. while (ent = readdir(dp), ent) {
  187. if (check_prefix(ent->d_name, "gpiochip")) {
  188. ret = list_device(ent->d_name);
  189. if (ret)
  190. break;
  191. }
  192. }
  193. ret = 0;
  194. if (closedir(dp) == -1) {
  195. perror("scanning devices: Failed to close directory");
  196. ret = -errno;
  197. }
  198. }
  199. error_out:
  200. return ret;
  201. }