lsgpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * lsgpio - example on how to list the GPIO lines on a system
  3. *
  4. * Copyright (C) 2015 Linus Walleij
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Usage:
  11. * lsgpio <-n device-name>
  12. */
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <poll.h>
  21. #include <fcntl.h>
  22. #include <getopt.h>
  23. #include <sys/ioctl.h>
  24. #include <linux/gpio.h>
  25. #include "gpio-utils.h"
  26. struct gpio_flag {
  27. char *name;
  28. unsigned long mask;
  29. };
  30. struct gpio_flag flagnames[] = {
  31. {
  32. .name = "kernel",
  33. .mask = GPIOLINE_FLAG_KERNEL,
  34. },
  35. {
  36. .name = "output",
  37. .mask = GPIOLINE_FLAG_IS_OUT,
  38. },
  39. {
  40. .name = "active-low",
  41. .mask = GPIOLINE_FLAG_ACTIVE_LOW,
  42. },
  43. {
  44. .name = "open-drain",
  45. .mask = GPIOLINE_FLAG_OPEN_DRAIN,
  46. },
  47. {
  48. .name = "open-source",
  49. .mask = GPIOLINE_FLAG_OPEN_SOURCE,
  50. },
  51. };
  52. void print_flags(unsigned long flags)
  53. {
  54. int i;
  55. int printed = 0;
  56. for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
  57. if (flags & flagnames[i].mask) {
  58. if (printed)
  59. fprintf(stdout, " ");
  60. fprintf(stdout, "%s", flagnames[i].name);
  61. printed++;
  62. }
  63. }
  64. }
  65. int list_device(const char *device_name)
  66. {
  67. struct gpiochip_info cinfo;
  68. char *chrdev_name;
  69. int fd;
  70. int ret;
  71. int i;
  72. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  73. if (ret < 0)
  74. return -ENOMEM;
  75. fd = open(chrdev_name, 0);
  76. if (fd == -1) {
  77. ret = -errno;
  78. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  79. goto exit_close_error;
  80. }
  81. /* Inspect this GPIO chip */
  82. ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
  83. if (ret == -1) {
  84. ret = -errno;
  85. perror("Failed to issue CHIPINFO IOCTL\n");
  86. goto exit_close_error;
  87. }
  88. fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
  89. cinfo.name, cinfo.label, cinfo.lines);
  90. /* Loop over the lines and print info */
  91. for (i = 0; i < cinfo.lines; i++) {
  92. struct gpioline_info linfo;
  93. memset(&linfo, 0, sizeof(linfo));
  94. linfo.line_offset = i;
  95. ret = ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &linfo);
  96. if (ret == -1) {
  97. ret = -errno;
  98. perror("Failed to issue LINEINFO IOCTL\n");
  99. goto exit_close_error;
  100. }
  101. fprintf(stdout, "\tline %2d:", linfo.line_offset);
  102. if (linfo.name[0])
  103. fprintf(stdout, " \"%s\"", linfo.name);
  104. else
  105. fprintf(stdout, " unnamed");
  106. if (linfo.consumer[0])
  107. fprintf(stdout, " \"%s\"", linfo.consumer);
  108. else
  109. fprintf(stdout, " unused");
  110. if (linfo.flags) {
  111. fprintf(stdout, " [");
  112. print_flags(linfo.flags);
  113. fprintf(stdout, "]");
  114. }
  115. fprintf(stdout, "\n");
  116. }
  117. exit_close_error:
  118. if (close(fd) == -1)
  119. perror("Failed to close GPIO character device file");
  120. free(chrdev_name);
  121. return ret;
  122. }
  123. void print_usage(void)
  124. {
  125. fprintf(stderr, "Usage: lsgpio [options]...\n"
  126. "List GPIO chips, lines and states\n"
  127. " -n <name> List GPIOs on a named device\n"
  128. " -? This helptext\n"
  129. );
  130. }
  131. int main(int argc, char **argv)
  132. {
  133. const char *device_name = NULL;
  134. int ret;
  135. int c;
  136. while ((c = getopt(argc, argv, "n:")) != -1) {
  137. switch (c) {
  138. case 'n':
  139. device_name = optarg;
  140. break;
  141. case '?':
  142. print_usage();
  143. return -1;
  144. }
  145. }
  146. if (device_name)
  147. ret = list_device(device_name);
  148. else {
  149. const struct dirent *ent;
  150. DIR *dp;
  151. /* List all GPIO devices one at a time */
  152. dp = opendir("/dev");
  153. if (!dp) {
  154. ret = -errno;
  155. goto error_out;
  156. }
  157. ret = -ENOENT;
  158. while (ent = readdir(dp), ent) {
  159. if (check_prefix(ent->d_name, "gpiochip")) {
  160. ret = list_device(ent->d_name);
  161. if (ret)
  162. break;
  163. }
  164. }
  165. ret = 0;
  166. if (closedir(dp) == -1) {
  167. perror("scanning devices: Failed to close directory");
  168. ret = -errno;
  169. }
  170. }
  171. error_out:
  172. return ret;
  173. }