Skip to contentSkip to conversation

gpio: add get/set helpers for GPIO lines

PATCH Applied Martin Hundebøll 2 participants 4 messages Jun 17, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 gpio: add get/set helpers for GPIO lines

The Linux kernel GPIO api operates with chips, lines, handles, and events.

The chip and line structures represent info about gpio chips, and gpio lines, respectively. They are used to e.g. lookup a line with a certain name, and/or line flags.

The handle structure is used to "obtain" a handle to one or more gpio lines on a chip. Until the file descriptor in this handle is closed, the gpio lines cannot be used by others. The same file descriptor is used when setting or getting the line values (one can also set the initial value when obtaining handles for output lines).

The event structure is used to get a file descriptor that can be used with select/poll to wait for changes in line levels.

This commit adds support for setting and getting values for one or more gpio lines. So far three structures are implemented:

l_gpio_chip
l_gpio_writer
l_gpio_reader

The chip structure is used to look up line info, and to find line offsets for a certain label. The chip and offset is then used to instantiate writers and readers.

The writer and reader are created with one of more offsets on a certain chip. For the writer case, the offsets are accompanied with the default output values to set when requesting the gpio lines.

Functionality that could be implemented, but is postponed until the need arises includes:

* waiting for events
* one-shot wrappers that don't hold on to gpio lines

---

Changes since v4:
* use unlikely() when verifying parameters
* verify *values parameter in _get() and _set()
* no helpers for single line uses
* check for file type and prefix when scanning devices
* update copyright years

Changes since v3:
* let lookup-by-label return multiple gpio chips
* implemented lookup of line offsets
* use separate reader/writer objects
* make the multi-line case the common use

Changes since v2:
* added internal reference counted structure to hold primary fd
* added l_gpio_line structure to represent a single line
* added function to lookup gpio line by label

Changes since v1:
* added gpiochip info and corresponding getters
* changed "line" to "line_num" a few places

Changes since RFC:
* added gpio.h to ell.h
* changed copyright to Geanix
* open gpiochip in l_gpio_chip_new()
* open gpiochip read-only
* added input checks
* clear ioctl structs with memset instead of = {0}
* reorder error-paths

 Makefile.am |   6 +-
 ell/ell.h   |   1 +
 ell/ell.sym |  18 +++
 ell/gpio.c  | 401 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/gpio.h  |  72 ++++++++++
 5 files changed, 496 insertions(+), 2 deletions(-)
 create mode 100644 ell/gpio.c
 create mode 100644 ell/gpio.h
Makefile.am +4 -2
index 91299d4..072867f 100644
@@ -52,7 +52,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/cert.h \
 			ell/ecc.h \
 			ell/ecdh.h \
-			ell/time.h
+			ell/time.h \
+			ell/gpio.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -123,7 +124,8 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/ecc-external.c \
 			ell/ecc.c \
 			ell/ecdh.c \
-			ell/time.c
+			ell/time.c \
+			ell/gpio.c
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ell.h +1 -0
index 691bdb2..c5cb1f2 100644
@@ -60,3 +60,4 @@
 #include <ell/ecc.h>
 #include <ell/ecdh.h>
 #include <ell/time.h>
+#include <ell/gpio.h>
ell/ell.sym +18 -0
index e720322..ffa11d6 100644
@@ -503,6 +503,24 @@ global:
 	l_ecdh_generate_shared_secret;
 	/* time */
 	l_time_now;
+	/* gpio */
+	l_gpio_chips_with_line_label;
+	l_gpio_chip_new;
+	l_gpio_chip_free;
+	l_gpio_chip_find_line_offset;
+	l_gpio_chip_get_label;
+	l_gpio_chip_get_name;
+	l_gpio_chip_get_num_lines;
+	l_gpio_chip_get_line_label;
+	l_gpio_chip_get_line_consumer;
+	l_gpio_writer_new;
+	l_gpio_writer_free;
+	l_gpio_writer_set;
+	l_gpio_writer_get_label;
+	l_gpio_reader_new;
+	l_gpio_reader_free;
+	l_gpio_reader_get;
+	l_gpio_reader_get_label;
 local:
 	*;
 };
ell/gpio.c new file +401 -0

401 lines not on this page · raw message

ell/gpio.h new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.22.0
#1 lore raw

Hi Martin,

Quoted 30 lines · Martin Hundebøll

The Linux kernel GPIO api operates with chips, lines, handles, and events.

The chip and line structures represent info about gpio chips, and gpio lines, respectively. They are used to e.g. lookup a line with a certain name, and/or line flags.

The handle structure is used to "obtain" a handle to one or more gpio lines on a chip. Until the file descriptor in this handle is closed, the gpio lines cannot be used by others. The same file descriptor is used when setting or getting the line values (one can also set the initial value when obtaining handles for output lines).

The event structure is used to get a file descriptor that can be used with select/poll to wait for changes in line levels.

This commit adds support for setting and getting values for one or more gpio lines. So far three structures are implemented:

l_gpio_chip
l_gpio_writer
l_gpio_reader

The chip structure is used to look up line info, and to find line offsets for a certain label. The chip and offset is then used to instantiate writers and readers.

The writer and reader are created with one of more offsets on a certain chip. For the writer case, the offsets are accompanied with the default output values to set when requesting the gpio lines.

Quoted 6 lines

Functionality that could be implemented, but is postponed until the need arises includes:

* waiting for events
* one-shot wrappers that don't hold on to gpio lines

Applied, thanks.

Regards,
-Denis

#2 lore raw

Hi Denis,

Quoted 5 lines · Denis Kenzior

Applied, thanks.

Regards,
-Denis

Cool.

I can submit the attached test program as an example?

// Martin

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ell/ell.h>

static int print_usage(void)

{
	fprintf(stderr,
		"usage: %1$s find <name>\n"
		"       %1$s get (<chip> <line>|<name>)\n"
		"       %1$s set (<chip> <line>|<name>) <value>\n"
		"       %1$s chip <chip>\n"
		"       %1$s line <chip> <line>\n",
		program_invocation_short_name);

	return EXIT_FAILURE;
}

static int find(int argc, char **argv)

{
	struct l_gpio_chip *chip;
	char **chip_names = NULL;
	uint32_t offset;
	int i;

	if (argc != 2)
		return print_usage();

	chip_names = l_gpio_chips_with_line_label(argv[1]);
	if (!chip_names)
		return EXIT_FAILURE;

	for (i = 0; chip_names[i]; i++) {
		chip = l_gpio_chip_new(chip_names[0]);

		if (!chip)
			return EXIT_FAILURE;

		if (!l_gpio_chip_find_line_offset(chip, argv[1], &offset)) {
			l_gpio_chip_free(chip);
			return EXIT_FAILURE;
		}

		printf("chip: %s\n", l_gpio_chip_get_name(chip));
		printf("line: %u\n", offset);

		l_gpio_chip_free(chip);
	}
	l_strfreev(chip_names);

	return EXIT_SUCCESS;
}

static int get(int argc, char **argv)

{
	struct l_gpio_chip *chip;
	struct l_gpio_reader *reader;
	char **chip_names = NULL;
	uint32_t offset;
	uint32_t value;

	if (argc == 2) {
		chip_names = l_gpio_chips_with_line_label(argv[1]);
		if (l_strv_length(chip_names) != 1) {
			l_strfreev(chip_names);
			return EXIT_FAILURE;
		}

		chip = l_gpio_chip_new(chip_names[0]);
		l_strfreev(chip_names);

		if (!chip)
			return EXIT_FAILURE;

		if (!l_gpio_chip_find_line_offset(chip, argv[1], &offset)) {
			l_gpio_chip_free(chip);
			return EXIT_FAILURE;
		}
	} else if (argc == 3) {
		char *chip_name;

		chip_name = argv[1];
		offset = strtoul(argv[2], NULL, 0);

		chip = l_gpio_chip_new(chip_name);
		if (!chip)
			return EXIT_FAILURE;
	} else {
		return print_usage();
	}

	printf("chip: %s\n", l_gpio_chip_get_name(chip));
	printf("line: %u\n", offset);

	reader = l_gpio_reader_new(chip, "gpio-test", 1, &offset);
	if (!reader) {
		l_gpio_chip_free(chip);
		return EXIT_FAILURE;
	}

	if (!l_gpio_reader_get(reader, 1, &value)) {
		l_gpio_reader_free(reader);
		l_gpio_chip_free(chip);
		return EXIT_FAILURE;
	}

	printf("val:  %u\n", value);

	l_gpio_reader_free(reader);
	l_gpio_chip_free(chip);

	return EXIT_SUCCESS;
}

static int set(int argc, char **argv)

{
	struct l_gpio_chip *chip;
	struct l_gpio_writer *writer;
	char **chip_names;
	uint32_t offset;
	uint32_t value = 1;

	if (argc == 3) {
		chip_names = l_gpio_chips_with_line_label(argv[1]);
		if (l_strv_length(chip_names) != 1) {
			l_strfreev(chip_names);
			return EXIT_FAILURE;
		}

		chip = l_gpio_chip_new(chip_names[0]);
		l_strfreev(chip_names);

		if (!chip)
			return EXIT_FAILURE;

		if (!l_gpio_chip_find_line_offset(chip, argv[1], &offset)) {
			l_gpio_chip_free(chip);
			return EXIT_FAILURE;
		}

		value = strtol(argv[2], NULL, 0);
	} else if (argc == 4) {
		char *chip_name;

		chip_name = argv[1];
		offset = strtoul(argv[2], NULL, 0);

		chip = l_gpio_chip_new(chip_name);
		if (!chip)
			return EXIT_FAILURE;

		value = strtol(argv[3], NULL, 0);
	} else {
		return print_usage();
	}

	printf("chip: %s\n", l_gpio_chip_get_name(chip));
	printf("line: %u\n", offset);

	writer = l_gpio_writer_new(chip, "gpio-test", 1, &offset, &value);
	if (!writer) {
		l_gpio_chip_free(chip);
		return EXIT_FAILURE;
	}

	if (!l_gpio_writer_set(writer, 1, &value)) {
		l_gpio_writer_free(writer);
		l_gpio_chip_free(chip);
		return EXIT_FAILURE;
	}

	printf("val:  %u\n", value);

	l_gpio_writer_free(writer);
	l_gpio_chip_free(chip);

	return EXIT_SUCCESS;
}

static int chip(int argc, char **argv)

{
	struct l_gpio_chip *chip;

	if (argc != 2)
		return print_usage();

	chip = l_gpio_chip_new(argv[1]);
	if (!chip)
		return EXIT_FAILURE;

	printf("chip:  %s\n", l_gpio_chip_get_name(chip));
	printf("label: %s\n", l_gpio_chip_get_label(chip));
	printf("lines: %u\n", l_gpio_chip_get_num_lines(chip));

	l_gpio_chip_free(chip);

	return EXIT_SUCCESS;
}

static int line(int argc, char **argv)

{
	struct l_gpio_chip *chip;
	uint32_t offset;
	char *label;
	char *consumer;

	if (argc != 3)
		return print_usage();

	chip = l_gpio_chip_new(argv[1]);
	if (!chip)
		return EXIT_FAILURE;

	offset = strtoul(argv[2], NULL, 0);
	label = l_gpio_chip_get_line_label(chip, offset);
	consumer = l_gpio_chip_get_line_consumer(chip, offset);

	printf("chip:     %s\n", l_gpio_chip_get_name(chip));
	printf("line:     %u\n", offset);
	printf("label:    %s\n", label);
	printf("consumer: %s\n", consumer);

	l_free(label);
	l_gpio_chip_free(chip);

	return EXIT_SUCCESS;
}

int main(int argc, char **argv)

{
	const char *cmd;

	if (argc < 2)
		return print_usage();

	cmd = argv[1];
	argc--;
	argv++;

	if (strcmp(cmd, "get") == 0)
		return get(argc, argv);
	else if (strcmp(cmd, "set") == 0)
		return set(argc, argv);
	else if (strcmp(cmd, "find") == 0)
		return find(argc, argv);
	else if (strcmp(cmd, "chip") == 0)
		return chip(argc, argv);
	else if (strcmp(cmd, "line") == 0)
		return line(argc, argv);
	else
		return print_usage();
}
#3 lore raw

Hi Martin,

Quoted 6 lines · Martin Hundebøll

Hi Denis,

Quoted 3 lines · Denis Kenzior

Applied, thanks.

Quoted 7 lines
Quoted 2 lines

Regards,
-Denis

Cool.

I can submit the attached test program as an example?

Sure, go for it. Either in examples/ or tools/, depending on what you feel is more appropriate.

Regards,
-Denis