Path: blob/master/tools/testing/selftests/gpio/gpio-chip-info.c
26285 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* GPIO character device helper for reading chip information.3*4* Copyright (C) 2021 Bartosz Golaszewski <[email protected]>5*/67#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>1415static void print_usage(void)16{17printf("usage:\n");18printf(" gpio-chip-info <chip path> [name|label|num-lines]\n");19}2021int main(int argc, char **argv)22{23struct gpiochip_info info;24int fd, ret;2526if (argc != 3) {27print_usage();28return EXIT_FAILURE;29}3031fd = open(argv[1], O_RDWR);32if (fd < 0) {33perror("unable to open the GPIO chip");34return EXIT_FAILURE;35}3637memset(&info, 0, sizeof(info));38ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info);39if (ret) {40perror("chip info ioctl failed");41return EXIT_FAILURE;42}4344if (strcmp(argv[2], "name") == 0) {45printf("%s\n", info.name);46} else if (strcmp(argv[2], "label") == 0) {47printf("%s\n", info.label);48} else if (strcmp(argv[2], "num-lines") == 0) {49printf("%u\n", info.lines);50} else {51fprintf(stderr, "unknown command: %s\n", argv[2]);52return EXIT_FAILURE;53}5455return EXIT_SUCCESS;56}575859