Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GPIO character device helper for reading chip information.
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-chip-info <chip path> [name|label|num-lines]\n");
0020 }
0021 
0022 int main(int argc, char **argv)
0023 {
0024     struct gpiochip_info info;
0025     int fd, ret;
0026 
0027     if (argc != 3) {
0028         print_usage();
0029         return EXIT_FAILURE;
0030     }
0031 
0032     fd = open(argv[1], O_RDWR);
0033     if (fd < 0) {
0034         perror("unable to open the GPIO chip");
0035         return EXIT_FAILURE;
0036     }
0037 
0038     memset(&info, 0, sizeof(info));
0039     ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info);
0040     if (ret) {
0041         perror("chip info ioctl failed");
0042         return EXIT_FAILURE;
0043     }
0044 
0045     if (strcmp(argv[2], "name") == 0) {
0046         printf("%s\n", info.name);
0047     } else if (strcmp(argv[2], "label") == 0) {
0048         printf("%s\n", info.label);
0049     } else if (strcmp(argv[2], "num-lines") == 0) {
0050         printf("%u\n", info.lines);
0051     } else {
0052         fprintf(stderr, "unknown command: %s\n", argv[2]);
0053         return EXIT_FAILURE;
0054     }
0055 
0056     return EXIT_SUCCESS;
0057 }