gpio-line-name.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GPIO character device helper for reading line names.
  4. *
  5. * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
  6. */
  7. #include <fcntl.h>
  8. #include <linux/gpio.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/types.h>
  14. static void print_usage(void)
  15. {
  16. printf("usage:\n");
  17. printf(" gpio-line-name <chip path> <line offset>\n");
  18. }
  19. int main(int argc, char **argv)
  20. {
  21. struct gpio_v2_line_info info;
  22. int fd, ret;
  23. char *endp;
  24. if (argc != 3) {
  25. print_usage();
  26. return EXIT_FAILURE;
  27. }
  28. fd = open(argv[1], O_RDWR);
  29. if (fd < 0) {
  30. perror("unable to open the GPIO chip");
  31. return EXIT_FAILURE;
  32. }
  33. memset(&info, 0, sizeof(info));
  34. info.offset = strtoul(argv[2], &endp, 10);
  35. if (*endp != '\0') {
  36. print_usage();
  37. return EXIT_FAILURE;
  38. }
  39. ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &info);
  40. if (ret) {
  41. perror("line info ioctl failed");
  42. return EXIT_FAILURE;
  43. }
  44. printf("%s\n", info.name);
  45. return EXIT_SUCCESS;
  46. }