Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GPIO character device helper for reading line names.
0004  *
0005  * Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
0006  */
0007 
0008 #include <fcntl.h>
0009 #include <linux/gpio.h>
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <string.h>
0013 #include <sys/ioctl.h>
0014 #include <sys/types.h>
0015 
0016 static void print_usage(void)
0017 {
0018     printf("usage:\n");
0019     printf("  gpio-line-name <chip path> <line offset>\n");
0020 }
0021 
0022 int main(int argc, char **argv)
0023 {
0024     struct gpio_v2_line_info info;
0025     int fd, ret;
0026     char *endp;
0027 
0028     if (argc != 3) {
0029         print_usage();
0030         return EXIT_FAILURE;
0031     }
0032 
0033     fd = open(argv[1], O_RDWR);
0034     if (fd < 0) {
0035         perror("unable to open the GPIO chip");
0036         return EXIT_FAILURE;
0037     }
0038 
0039     memset(&info, 0, sizeof(info));
0040     info.offset = strtoul(argv[2], &endp, 10);
0041     if (*endp != '\0') {
0042         print_usage();
0043         return EXIT_FAILURE;
0044     }
0045 
0046     ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &info);
0047     if (ret) {
0048         perror("line info ioctl failed");
0049         return EXIT_FAILURE;
0050     }
0051 
0052     printf("%s\n", info.name);
0053 
0054     return EXIT_SUCCESS;
0055 }