Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * lsgpio - example on how to list the GPIO lines on a system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015 Linus Walleij
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	lsgpio <-n device-name>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "gpio-utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct gpio_flag {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	unsigned long long mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct gpio_flag flagnames[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		.name = "used",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		.mask = GPIO_V2_LINE_FLAG_USED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		.name = "input",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		.mask = GPIO_V2_LINE_FLAG_INPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		.name = "output",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		.mask = GPIO_V2_LINE_FLAG_OUTPUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		.name = "active-low",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		.mask = GPIO_V2_LINE_FLAG_ACTIVE_LOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		.name = "open-drain",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		.mask = GPIO_V2_LINE_FLAG_OPEN_DRAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.name = "open-source",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.mask = GPIO_V2_LINE_FLAG_OPEN_SOURCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		.name = "pull-up",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		.mask = GPIO_V2_LINE_FLAG_BIAS_PULL_UP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		.name = "pull-down",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		.mask = GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		.name = "bias-disabled",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		.mask = GPIO_V2_LINE_FLAG_BIAS_DISABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void print_attributes(struct gpio_v2_line_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	const char *field_format = "%s";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	for (i = 0; i < ARRAY_SIZE(flagnames); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (info->flags & flagnames[i].mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			fprintf(stdout, field_format, flagnames[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			field_format = ", %s";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if ((info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	    (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		fprintf(stdout, field_format, "both-edges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_RISING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		fprintf(stdout, field_format, "rising-edge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	else if (info->flags & GPIO_V2_LINE_FLAG_EDGE_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		fprintf(stdout, field_format, "falling-edge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	for (i = 0; i < info->num_attrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (info->attrs[i].id == GPIO_V2_LINE_ATTR_ID_DEBOUNCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			fprintf(stdout, ", debounce_period=%dusec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				info->attrs[0].debounce_period_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) int list_device(const char *device_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct gpiochip_info cinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	char *chrdev_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ret = asprintf(&chrdev_name, "/dev/%s", device_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	fd = open(chrdev_name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		fprintf(stderr, "Failed to open %s\n", chrdev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		goto exit_free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	/* Inspect this GPIO chip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (ret == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		perror("Failed to issue CHIPINFO IOCTL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		goto exit_close_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	fprintf(stdout, "GPIO chip: %s, \"%s\", %u GPIO lines\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		cinfo.name, cinfo.label, cinfo.lines);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Loop over the lines and print info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	for (i = 0; i < cinfo.lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		struct gpio_v2_line_info linfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		memset(&linfo, 0, sizeof(linfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		linfo.offset = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (ret == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			perror("Failed to issue LINEINFO IOCTL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			goto exit_close_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		fprintf(stdout, "\tline %2d:", linfo.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (linfo.name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			fprintf(stdout, " \"%s\"", linfo.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			fprintf(stdout, " unnamed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (linfo.consumer[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			fprintf(stdout, " \"%s\"", linfo.consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			fprintf(stdout, " unused");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (linfo.flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			fprintf(stdout, " [");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			print_attributes(&linfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			fprintf(stdout, "]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		fprintf(stdout, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) exit_close_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (close(fd) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		perror("Failed to close GPIO character device file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) exit_free_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	free(chrdev_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void print_usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	fprintf(stderr, "Usage: lsgpio [options]...\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		"List GPIO chips, lines and states\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		"  -n <name>  List GPIOs on a named device\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		"  -?         This helptext\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	const char *device_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	while ((c = getopt(argc, argv, "n:")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			device_name = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			print_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (device_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		ret = list_device(device_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		const struct dirent *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		DIR *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		/* List all GPIO devices one at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		dp = opendir("/dev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		if (!dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		while (ent = readdir(dp), ent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			if (check_prefix(ent->d_name, "gpiochip")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 				ret = list_device(ent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		if (closedir(dp) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			perror("scanning devices: Failed to close directory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }