tools/gpio/gpio-utils.c
Source file repositories/reference/linux-study-clean/tools/gpio/gpio-utils.c
File Facts
- System
- Linux kernel
- Corpus path
tools/gpio/gpio-utils.c- Extension
.c- Size
- 7520 bytes
- Lines
- 285
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
unistd.hstdlib.hstdio.herrno.hstring.hfcntl.hgetopt.hsys/ioctl.hlinux/gpio.hgpio-utils.h
Detected Declarations
function gpiotools_request_linefunction gpiotools_set_valuesfunction gpiotools_get_valuesfunction gpiotools_release_linefunction gpiotools_getfunction gpiotools_getsfunction gpiotools_setfunction gpiotools_sets
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/*
* GPIO tools - helpers library for the GPIO tools
*
* Copyright (C) 2015 Linus Walleij
* Copyright (C) 2016 Bamvor Jian Zhang
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
#include "gpio-utils.h"
#define CONSUMER "gpio-utils"
/**
* DOC: Operation of gpio
*
* Provide the api of gpiochip for chardev interface. There are two
* types of api. The first one provide as same function as each
* ioctl, including request and release for lines of gpio, read/write
* the value of gpio. If the user want to do lots of read and write of
* lines of gpio, user should use this type of api.
*
* The second one provide the easy to use api for user. Each of the
* following api will request gpio lines, do the operation and then
* release these lines.
*/
/**
* gpiotools_request_line() - request gpio lines in a gpiochip
* @device_name: The name of gpiochip without prefix "/dev/",
* such as "gpiochip0"
* @lines: An array desired lines, specified by offset
* index for the associated GPIO device.
* @num_lines: The number of lines to request.
* @config: The new config for requested gpio. Reference
* "linux/gpio.h" for config details.
* @consumer: The name of consumer, such as "sysfs",
* "powerkey". This is useful for other users to
* know who is using.
*
* Request gpio lines through the ioctl provided by chardev. User
* could call gpiotools_set_values() and gpiotools_get_values() to
* read and write respectively through the returned fd. Call
* gpiotools_release_line() to release these lines after that.
*
* Return: On success return the fd;
* On failure return the errno.
*/
int gpiotools_request_line(const char *device_name, unsigned int *lines,
unsigned int num_lines,
struct gpio_v2_line_config *config,
const char *consumer)
{
struct gpio_v2_line_request req;
char *chrdev_name;
int fd;
int i;
int ret;
ret = asprintf(&chrdev_name, "/dev/%s", device_name);
if (ret < 0)
return -ENOMEM;
fd = open(chrdev_name, 0);
if (fd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s, %s\n",
chrdev_name, strerror(errno));
goto exit_free_name;
}
memset(&req, 0, sizeof(req));
for (i = 0; i < num_lines; i++)
req.offsets[i] = lines[i];
req.config = *config;
strcpy(req.consumer, consumer);
req.num_lines = num_lines;
ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
if (ret == -1) {
ret = -errno;
Annotation
- Immediate include surface: `unistd.h`, `stdlib.h`, `stdio.h`, `errno.h`, `string.h`, `fcntl.h`, `getopt.h`, `sys/ioctl.h`.
- Detected declarations: `function gpiotools_request_line`, `function gpiotools_set_values`, `function gpiotools_get_values`, `function gpiotools_release_line`, `function gpiotools_get`, `function gpiotools_gets`, `function gpiotools_set`, `function gpiotools_sets`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.