gpio: add simple get/set helpers for GPIO lines
PATCH v3 1/1 gpio: add simple 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 simple support for setting and getting the value for a single gpio line. It does so by obtaining a line handle to get/set the value, and then release the handle immediately again.
Both the l_gpio_chip and l_gpio_line structures reference an internal gpio structure that holds the file descriptor to the open /dev/gpiochipX character device. This file descriptor is used by l_gpio_chip to lookup line info, and by l_gpio_line to request a virtual file descriptor used to get and set line values.
Functionality that could be implemented, but is postponed until the need arises includes:
* setting/getting multiple gpio lines with a single function
* waiting for events
* holding on to handles
Some of the above probably require adding structures to represent multiple gpio lines and events, while handles should be private to the class.
---
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 | 15 +++ ell/gpio.c | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ell/gpio.h | 59 +++++++++ 5 files changed, 439 insertions(+), 2 deletions(-) create mode 100644 ell/gpio.c create mode 100644 ell/gpio.h
Makefile.am +4 -2
@@ -51,7 +51,8 @@ pkginclude_HEADERS = ell/ell.h \ ell/dhcp.h \ ell/cert.h \ ell/ecc.h \ - ell/ecdh.h + ell/ecdh.h \ + ell/gpio.h lib_LTLIBRARIES = ell/libell.la
@@ -119,7 +120,8 @@ ell_libell_la_SOURCES = $(linux_headers) \ ell/ecc.h \ ell/ecc-external.c \ ell/ecc.c \ - ell/ecdh.c + ell/ecdh.c \ + ell/gpio.c ell_libell_la_LDFLAGS = -no-undefined \ -Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/ell.h +1 -0
@@ -59,3 +59,4 @@#include <ell/cert.h> #include <ell/ecc.h> #include <ell/ecdh.h> +#include <ell/gpio.h>
ell/ell.sym +15 -0
@@ -463,6 +463,21 @@ global: /* ecdh */ l_ecdh_generate_key_pair; l_ecdh_generate_shared_secret; + /* gpio */ + l_gpio_chip_new; + l_gpio_chip_free; + l_gpio_chip_get_name; + l_gpio_chip_get_label; + l_gpio_chip_get_num_lines; + l_gpio_chip_get_line; + l_gpio_chip_find_line; + l_gpio_line_free; + l_gpio_line_get_label; + l_gpio_line_get_num; + l_gpio_line_get_chip; + l_gpio_line_set_value; + l_gpio_line_get_value; + l_gpio_find_line; local: *; };
ell/gpio.c new file +360 -0
360 lines not on this page · raw message
ell/gpio.h new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.20.1
Hi,
Quoted 29 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 simple support for setting and getting the value for a single gpio line. It does so by obtaining a line handle to get/set the value, and then release the handle immediately again.
Both the
l_gpio_chipandl_gpio_linestructures reference an internal gpio structure that holds the file descriptor to the open/dev/gpiochipXcharacter device. This file descriptor is used byl_gpio_chipto lookup line info, and byl_gpio_lineto request a virtual file descriptor used to get and set line values.Functionality that could be implemented, but is postponed until the need arises includes:
Quoted 7 lines
* setting/getting multiple gpio lines with a single function
* waiting for events
* holding on to handlesSome of the above probably require adding structures to represent multiple gpio lines and events, while handles should be private to the class.
I have attached a small find/get/set gpio util, that I use for testing my patch. It uses most of the API.
I am not really sure how to implement unit tests for the gpio interface, though.
// Martin
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ell/gpio.h>
static int print_usage(void)
{
fprintf(stderr,
"usage: %s find <name>\n"
" %s get (<chip> <line>|<name>)\n"
" %s set (<chip> <line>|<name>) <value>\n"
" %s chip <chip>\n"
" %s line <chip> <line>\n",
program_invocation_short_name, program_invocation_short_name,
program_invocation_short_name, program_invocation_short_name,
program_invocation_short_name);
return EXIT_FAILURE;
}static int find(int argc, char **argv)
{
struct l_gpio_chip *chip;
struct l_gpio_line *line;
if (argc != 2)
return print_usage();
line = l_gpio_find_line(argv[1]);
if (!line)
return EXIT_FAILURE;
chip = l_gpio_line_get_chip(line);
if (!chip) {
l_gpio_line_free(line);
return EXIT_FAILURE;
}
printf("chip: %s\n", l_gpio_chip_get_name(chip));
printf("line: %u\n", l_gpio_line_get_num(line));
l_gpio_line_free(line);
l_gpio_chip_free(chip);
return EXIT_SUCCESS;
}static int get(int argc, char **argv)
{
struct l_gpio_chip *chip;
struct l_gpio_line *line;
bool value;
if (argc == 2) {
line = l_gpio_find_line(argv[1]);
if (!line)
return EXIT_FAILURE;
chip = l_gpio_line_get_chip(line);
if (!chip) {
l_gpio_line_free(line);
return EXIT_FAILURE;
}
} else if (argc == 3) {
char *chip_name;
uint32_t line_num;
chip_name = argv[1];
line_num = strtoul(argv[2], NULL, 0);
chip = l_gpio_chip_new(chip_name);
if (!chip)
return EXIT_FAILURE;
line = l_gpio_chip_get_line(chip, line_num);
if (!line) {
l_gpio_chip_free(chip);
return EXIT_FAILURE;
}
} else {
return print_usage();
}
printf("chip: %s\n", l_gpio_chip_get_name(chip));
printf("line: %u\n", l_gpio_line_get_num(line));
if (!l_gpio_line_get_value(line, &value)) {
l_gpio_line_free(line);
l_gpio_chip_free(chip);
return EXIT_FAILURE;
}
printf("val: %hhu\n", value);
l_gpio_line_free(line);
l_gpio_chip_free(chip);
return EXIT_SUCCESS;
}static int set(int argc, char **argv)
{
struct l_gpio_chip *chip;
struct l_gpio_line *line;
uint8_t value = 1;
if (argc == 3) {
line = l_gpio_find_line(argv[1]);
if (!line)
return EXIT_FAILURE;
chip = l_gpio_line_get_chip(line);
if (!chip) {
l_gpio_line_free(line);
return EXIT_FAILURE;
}
value = strtol(argv[2], NULL, 0);
} else if (argc == 4) {
char *chip_name;
uint32_t line_num;
chip_name = argv[1];
line_num = strtoul(argv[2], NULL, 0);
value = strtol(argv[3], NULL, 0);
chip = l_gpio_chip_new(chip_name);
if (!chip)
return EXIT_FAILURE;
line = l_gpio_chip_get_line(chip, line_num);
if (!line) {
l_gpio_chip_free(chip);
return EXIT_FAILURE;
}
} else {
return print_usage();
}
printf("chip: %s\n", l_gpio_chip_get_name(chip));
printf("line: %u\n", l_gpio_line_get_num(line));
if (!l_gpio_line_set_value(line, value)) {
l_gpio_chip_free(chip);
l_gpio_line_free(line);
return EXIT_FAILURE;
}
printf("val: %hhu\n", value);
l_gpio_chip_free(chip);
l_gpio_line_free(line);
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_FAILURE;
}static int line(int argc, char **argv)
{
struct l_gpio_chip *chip;
struct l_gpio_line *line;
uint32_t line_num;
if (argc != 3)
return print_usage();
chip = l_gpio_chip_new(argv[1]);
if (!chip)
return EXIT_FAILURE;
line_num = strtoul(argv[2], NULL, 0);
line = l_gpio_chip_get_line(chip, line_num);
if (!line) {
l_gpio_chip_free(chip);
return EXIT_FAILURE;
}
printf("chip: %s\n", l_gpio_chip_get_name(chip));
printf("line: %u\n", l_gpio_line_get_num(line));
printf("label: %s\n", l_gpio_line_get_label(line));
l_gpio_chip_free(chip);
l_gpio_line_free(line);
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();
}