Peios Learn
Documentation

Peios

Peios Peios Kernel Universal Directory Project

Developer Tools

Trail Acta Provium clackatoo CAF GCSL Pekit

Applications

Framewright

Standards and Conventions

Standards and Conventions Peios Specifications
Peios Kernel

GPIO

Single-page view · as markdown

GPIO

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

  • Character Device Userspace API
  • Obsolete Userspace APIs

GPIO Character Device Userspace API

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

This is latest version (v2) of the character device API, as defined in include/uapi/linux/gpio.h.

First added in 5.10.

Note

Do NOT abuse userspace APIs to control hardware that has proper kernel drivers. There may already be a driver for your use case, and an existing kernel driver is sure to provide a superior solution to bitbashing from userspace.

Read Documentation/driver-api/gpio/drivers-on-gpio.rst to avoid reinventing kernel wheels in userspace.

Similarly, for multi-function lines there may be other subsystems, such as Documentation/spi/index.rst, Documentation/i2c/index.rst, Documentation/driver-api/pwm.rst, Documentation/w1/index.rst etc, that provide suitable drivers and APIs for your hardware.

Basic examples using the character device API can be found in tools/gpio/*.

The API is based around two major objects, the Chip and the Line Request.

Chip #

The Chip represents a single GPIO chip and is exposed to userspace using device files of the form /dev/gpiochipX.

Each chip supports a number of GPIO lines, chip.lines<gpiochip_info>. Lines on the chip are identified by an offset in the range from 0 to chip.lines - 1, i.e. [0,chip.lines).

Lines are requested from the chip using gpio-v2-get-line-ioctl.rst and the resulting line request is used to access the GPIO chip's lines or monitor the lines for edge events.

Within this documentation, the file descriptor returned by calling open() on the GPIO device file is referred to as chip_fd.

Operations #

The following operations may be performed on the chip:

  • Get Line
  • Get Chip Info
  • Get Line Info
  • Watch Line Info
  • Unwatch Line Info
  • Read Line Info Changed Events

Line Request #

Line requests are created by gpio-v2-get-line-ioctl.rst and provide access to a set of requested lines. The line request is exposed to userspace via the anonymous file descriptor returned in request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.

Within this documentation, the line request file descriptor is referred to as req_fd.

Operations #

The following operations may be performed on the line request:

  • Get Line Values
  • Set Line Values
  • Read Line Edge Events
  • Reconfigure Lines

Types #

This section contains the structs and enums that are referenced by the API v2, as defined in include/uapi/linux/gpio.h.

struct gpiochip_info #

Information about a certain GPIO chip

Definition

struct gpiochip_info {
    char name[GPIO_MAX_NAME_SIZE];
    char label[GPIO_MAX_NAME_SIZE];
    __u32 lines;
};

Members

  • name: the Linux kernel name of this GPIO chip
  • label: a functional name for this GPIO chip, such as a product number, may be empty (i.e. label[0] == '\0')
  • lines: number of GPIO lines on this chip

enum gpio_v2_line_flag #

struct gpio_v2_line_attribute.flags values

Constants

  • GPIO_V2_LINE_FLAG_USED: line is not available for request
  • GPIO_V2_LINE_FLAG_ACTIVE_LOW: line active state is physical low
  • GPIO_V2_LINE_FLAG_INPUT: line is an input
  • GPIO_V2_LINE_FLAG_OUTPUT: line is an output
  • GPIO_V2_LINE_FLAG_EDGE_RISING: line detects rising (inactive to active) edges
  • GPIO_V2_LINE_FLAG_EDGE_FALLING: line detects falling (active to inactive) edges
  • GPIO_V2_LINE_FLAG_OPEN_DRAIN: line is an open drain output
  • GPIO_V2_LINE_FLAG_OPEN_SOURCE: line is an open source output
  • GPIO_V2_LINE_FLAG_BIAS_PULL_UP: line has pull-up bias enabled
  • GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN: line has pull-down bias enabled
  • GPIO_V2_LINE_FLAG_BIAS_DISABLED: line has bias disabled
  • GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME: line events contain REALTIME timestamps
  • GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE: line events contain timestamps from the hardware timestamping engine (HTE) subsystem

struct gpio_v2_line_values #

Values of GPIO lines

Definition

struct gpio_v2_line_values {
    __aligned_u64 bits;
    __aligned_u64 mask;
};

Members

  • bits: a bitmap containing the value of the lines, set to 1 for active and 0 for inactive
  • mask: a bitmap identifying the lines to get or set, with each bit number corresponding to the index into struct gpio_v2_line_request.offsets

enum gpio_v2_line_attr_id #

struct gpio_v2_line_attribute.id values identifying which field of the attribute union is in use.

Constants

  • GPIO_V2_LINE_ATTR_ID_FLAGS: flags field is in use
  • GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES: values field is in use
  • GPIO_V2_LINE_ATTR_ID_DEBOUNCE: debounce_period_us field is in use

struct gpio_v2_line_attribute #

a configurable attribute of a line

Definition

struct gpio_v2_line_attribute {
    __u32 id;
    __u32 padding;
    union {
        __aligned_u64 flags;
        __aligned_u64 values;
        __u32 debounce_period_us;
    };
};

Members

  • id: attribute identifier with value from enum gpio_v2_line_attr_id
  • padding: reserved for future use and must be zero filled
  • {unnamed_union}: anonymous
  • flags: if id is GPIO_V2_LINE_ATTR_ID_FLAGS, the flags for the GPIO line, with values from enum gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, added together. This overrides the default flags contained in the struct gpio_v2_line_config for the associated line.
  • values: if id is GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES, a bitmap containing the values to which the lines will be set, with each bit number corresponding to the index into struct gpio_v2_line_request.offsets
  • debounce_period_us: if id is GPIO_V2_LINE_ATTR_ID_DEBOUNCE, the desired debounce period, in microseconds

struct gpio_v2_line_config_attribute #

a configuration attribute associated with one or more of the requested lines.

Definition

struct gpio_v2_line_config_attribute {
    struct gpio_v2_line_attribute attr;
    __aligned_u64 mask;
};

Members

  • attr: the configurable attribute
  • mask: a bitmap identifying the lines to which the attribute applies, with each bit number corresponding to the index into struct gpio_v2_line_request.offsets

struct gpio_v2_line_config #

Configuration for GPIO lines

Definition

struct gpio_v2_line_config {
    __aligned_u64 flags;
    __u32 num_attrs;
    __u32 padding[5];
    struct gpio_v2_line_config_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
};

Members

  • flags: flags for the GPIO lines, with values from enum gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, added together. This is the default for all requested lines but may be overridden for particular lines using attrs.
  • num_attrs: the number of attributes in attrs
  • padding: reserved for future use and must be zero filled
  • attrs: the configuration attributes associated with the requested lines. Any attribute should only be associated with a particular line once. If an attribute is associated with a line multiple times then the first occurrence (i.e. lowest index) has precedence.

struct gpio_v2_line_request #

Information about a request for GPIO lines

Definition

struct gpio_v2_line_request {
    __u32 offsets[GPIO_V2_LINES_MAX];
    char consumer[GPIO_MAX_NAME_SIZE];
    struct gpio_v2_line_config config;
    __u32 num_lines;
    __u32 event_buffer_size;
    __u32 padding[5];
    __s32 fd;
};

Members

  • offsets: an array of desired lines, specified by offset index for the associated GPIO chip
  • consumer: a desired consumer label for the selected GPIO lines such as "my-bitbanged-relay"
  • config: requested configuration for the lines
  • num_lines: number of lines requested in this request, i.e. the number of valid fields in the GPIO_V2_LINES_MAX sized arrays, set to 1 to request a single line
  • event_buffer_size: a suggested minimum number of line events that the kernel should buffer. This is only relevant if edge detection is enabled in the configuration. Note that this is only a suggested value and the kernel may allocate a larger buffer or cap the size of the buffer. If this field is zero then the buffer size defaults to a minimum of num_lines * 16.
  • padding: reserved for future use and must be zero filled
  • fd: after a successful GPIO_V2_GET_LINE_IOCTL operation, contains a valid anonymous file descriptor representing the request

struct gpio_v2_line_info #

Information about a certain GPIO line

Definition

struct gpio_v2_line_info {
    char name[GPIO_MAX_NAME_SIZE];
    char consumer[GPIO_MAX_NAME_SIZE];
    __u32 offset;
    __u32 num_attrs;
    __aligned_u64 flags;
    struct gpio_v2_line_attribute attrs[GPIO_V2_LINE_NUM_ATTRS_MAX];
    __u32 padding[4];
};

Members

  • name: the name of this GPIO line, such as the output pin of the line on the chip, a rail or a pin header name on a board, as specified by the GPIO chip, may be empty (i.e. name[0] == '\0')
  • consumer: a functional name for the consumer of this GPIO line as set by whatever is using it, will be empty if there is no current user but may also be empty if the consumer doesn't set this up
  • offset: the local offset on this GPIO chip, fill this in when requesting the line information from the kernel
  • num_attrs: the number of attributes in attrs
  • flags: flags for this GPIO line, with values from enum gpio_v2_line_flag, such as GPIO_V2_LINE_FLAG_ACTIVE_LOW, GPIO_V2_LINE_FLAG_OUTPUT etc, added together
  • attrs: the configuration attributes associated with the line
  • padding: reserved for future use

enum gpio_v2_line_changed_type #

struct gpio_v2_line_changed.event_type values

Constants

  • GPIO_V2_LINE_CHANGED_REQUESTED: line has been requested
  • GPIO_V2_LINE_CHANGED_RELEASED: line has been released
  • GPIO_V2_LINE_CHANGED_CONFIG: line has been reconfigured

struct gpio_v2_line_info_changed #

Information about a change in status of a GPIO line

Definition

struct gpio_v2_line_info_changed {
    struct gpio_v2_line_info info;
    __aligned_u64 timestamp_ns;
    __u32 event_type;
    __u32 padding[5];
};

Members

  • info: updated line information
  • timestamp_ns: estimate of time of status change occurrence, in nanoseconds
  • event_type: the type of change with a value from enum gpio_v2_line_changed_type
  • padding: reserved for future use

enum gpio_v2_line_event_id #

struct gpio_v2_line_event.id values

Constants

  • GPIO_V2_LINE_EVENT_RISING_EDGE: event triggered by a rising edge
  • GPIO_V2_LINE_EVENT_FALLING_EDGE: event triggered by a falling edge

struct gpio_v2_line_event #

The actual event being pushed to userspace

Definition

struct gpio_v2_line_event {
    __aligned_u64 timestamp_ns;
    __u32 id;
    __u32 offset;
    __u32 seqno;
    __u32 line_seqno;
    __u32 padding[6];
};

Members

  • timestamp_ns: best estimate of time of event occurrence, in nanoseconds
  • id: event identifier with value from enum gpio_v2_line_event_id
  • offset: the offset of the line that triggered the event
  • seqno: the sequence number for this event in the sequence of events for all the lines in this line request
  • line_seqno: the sequence number for this event in the sequence of events on this particular line
  • padding: reserved for future use

Description

By default the timestamp_ns is read from CLOCK_MONOTONIC and is intended to allow the accurate measurement of the time between events. It does not provide the wall-clock time.

If the GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME flag is set then the timestamp_ns is read from CLOCK_REALTIME.

If the GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE flag is set then the timestamp_ns is provided by the hardware timestamping engine (HTE) subsystem.

  • GPIO Error Codes

GPIO Character Device Userspace API (v1)

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This API is obsoleted by chardev.rst (v2).

New developments should use the v2 API, and existing developments are encouraged to migrate as soon as possible, as this API will be removed in the future. The v2 API is a functional superset of the v1 API so any v1 call can be directly translated to a v2 equivalent.

This interface will continue to be maintained for the migration period, but new features will only be added to the new API.

First added in 4.8.

The API is based around three major objects, the Chip, the Line Handle, and the Line Event.

Where "line event" is used in this document it refers to the request that can monitor a line for edge events, not the edge events themselves.

Chip #

The Chip represents a single GPIO chip and is exposed to userspace using device files of the form /dev/gpiochipX.

Each chip supports a number of GPIO lines, chip.lines<gpiochip_info>. Lines on the chip are identified by an offset in the range from 0 to chip.lines - 1, i.e. [0,chip.lines).

Lines are requested from the chip using either gpio-get-linehandle-ioctl.rst and the resulting line handle is used to access the GPIO chip's lines, or gpio-get-lineevent-ioctl.rst and the resulting line event is used to monitor a GPIO line for edge events.

Within this documentation, the file descriptor returned by calling open() on the GPIO device file is referred to as chip_fd.

Operations #

The following operations may be performed on the chip:

  • Get Line Handle
  • Get Line Event
  • Get Chip Info
  • Get Line Info
  • Watch Line Info
  • Unwatch Line Info
  • Read Line Info Changed Events

Line Handle #

Line handles are created by gpio-get-linehandle-ioctl.rst and provide access to a set of requested lines. The line handle is exposed to userspace via the anonymous file descriptor returned in request.fd<gpiohandle_request> by gpio-get-linehandle-ioctl.rst.

Within this documentation, the line handle file descriptor is referred to as handle_fd.

Operations #

The following operations may be performed on the line handle:

  • Get Line Values
  • Set Line Values
  • Reconfigure Lines

Line Event #

Line events are created by gpio-get-lineevent-ioctl.rst and provide access to a requested line. The line event is exposed to userspace via the anonymous file descriptor returned in request.fd<gpioevent_request> by gpio-get-lineevent-ioctl.rst.

Within this documentation, the line event file descriptor is referred to as event_fd.

Operations #

The following operations may be performed on the line event:

  • Get Line Value
  • Read Line Edge Events

Types #

This section contains the structs that are referenced by the ABI v1.

The struct gpiochip_info<gpiochip_info> is common to ABI v1 and v2.

struct gpioline_info #

Information about a certain GPIO line

Definition

struct gpioline_info {
    __u32 line_offset;
    __u32 flags;
    char name[GPIO_MAX_NAME_SIZE];
    char consumer[GPIO_MAX_NAME_SIZE];
};

Members

  • line_offset: the local offset on this GPIO device, fill this in when requesting the line information from the kernel
  • flags: various flags for this line
  • name: the name of this GPIO line, such as the output pin of the line on the chip, a rail or a pin header name on a board, as specified by the gpio chip, may be empty (i.e. name[0] == '\0')
  • consumer: a functional name for the consumer of this GPIO line as set by whatever is using it, will be empty if there is no current user but may also be empty if the consumer doesn't set this up

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_info instead.

struct gpioline_info_changed #

Information about a change in status of a GPIO line

Definition

struct gpioline_info_changed {
    struct gpioline_info info;
    __u64 timestamp;
    __u32 event_type;
    __u32 padding[5];
};

Members

  • info: updated line information
  • timestamp: estimate of time of status change occurrence, in nanoseconds
  • event_type: one of GPIOLINE_CHANGED_REQUESTED, GPIOLINE_CHANGED_RELEASED and GPIOLINE_CHANGED_CONFIG
  • padding: reserved for future use

Description

The struct gpioline_info embedded here has 32-bit alignment on its own, but it works fine with 64-bit alignment too. With its 72 byte size, we can guarantee there are no implicit holes between it and subsequent members. The 20-byte padding at the end makes sure we don't add any implicit padding at the end of the structure on 64-bit architectures.

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_info_changed instead.

struct gpiohandle_request #

Information about a GPIO handle request

Definition

struct gpiohandle_request {
    __u32 lineoffsets[GPIOHANDLES_MAX];
    __u32 flags;
    __u8 default_values[GPIOHANDLES_MAX];
    char consumer_label[GPIO_MAX_NAME_SIZE];
    __u32 lines;
    int fd;
};

Members

  • lineoffsets: an array of desired lines, specified by offset index for the associated GPIO device
  • flags: desired flags for the desired GPIO lines, such as GPIOHANDLE_REQUEST_OUTPUT, GPIOHANDLE_REQUEST_ACTIVE_LOW etc, added together. Note that even if multiple lines are requested, the same flags must be applicable to all of them, if you want lines with individual flags set, request them one by one. It is possible to select a batch of input or output lines, but they must all have the same characteristics, i.e. all inputs or all outputs, all active low etc
  • default_values: if the GPIOHANDLE_REQUEST_OUTPUT is set for a requested line, this specifies the default output value, should be 0 (inactive) or 1 (active). Anything other than 0 or 1 will be interpreted as active.
  • consumer_label: a desired consumer label for the selected GPIO line(s) such as "my-bitbanged-relay"
  • lines: number of lines requested in this request, i.e. the number of valid fields in the above arrays, set to 1 to request a single line
  • fd: after a successful GPIO_GET_LINEHANDLE_IOCTL operation, contains a valid anonymous file descriptor representing the request

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_request instead.

struct gpiohandle_config #

Configuration for a GPIO handle request

Definition

struct gpiohandle_config {
    __u32 flags;
    __u8 default_values[GPIOHANDLES_MAX];
    __u32 padding[4];
};

Members

  • flags: updated flags for the requested GPIO lines, such as GPIOHANDLE_REQUEST_OUTPUT, GPIOHANDLE_REQUEST_ACTIVE_LOW etc, added together
  • default_values: if the GPIOHANDLE_REQUEST_OUTPUT is set in flags, this specifies the default output value, should be 0 (inactive) or 1 (active). Anything other than 0 or 1 will be interpreted as active.
  • padding: reserved for future use and should be zero filled

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_config instead.

struct gpiohandle_data #

Information of values on a GPIO handle

Definition

struct gpiohandle_data {
    __u8 values[GPIOHANDLES_MAX];
};

Members

  • values: when getting the state of lines this contains the current state of a line, when setting the state of lines these should contain the desired target state. States are 0 (inactive) or 1 (active). When setting, anything other than 0 or 1 will be interpreted as active.

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_values instead.

struct gpioevent_request #

Information about a GPIO event request

Definition

struct gpioevent_request {
    __u32 lineoffset;
    __u32 handleflags;
    __u32 eventflags;
    char consumer_label[GPIO_MAX_NAME_SIZE];
    int fd;
};

Members

  • lineoffset: the desired line to subscribe to events from, specified by offset index for the associated GPIO device
  • handleflags: desired handle flags for the desired GPIO line, such as GPIOHANDLE_REQUEST_ACTIVE_LOW or GPIOHANDLE_REQUEST_OPEN_DRAIN
  • eventflags: desired flags for the desired GPIO event line, such as GPIOEVENT_REQUEST_RISING_EDGE or GPIOEVENT_REQUEST_FALLING_EDGE
  • consumer_label: a desired consumer label for the selected GPIO line(s) such as "my-listener"
  • fd: after a successful GPIO_GET_LINEEVENT_IOCTL operation, contains a valid anonymous file descriptor representing the request

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_request instead.

struct gpioevent_data #

The actual event being pushed to userspace

Definition

struct gpioevent_data {
    __u64 timestamp;
    __u32 id;
};

Members

  • timestamp: best estimate of time of event occurrence, in nanoseconds
  • id: event identifier, one of GPIOEVENT_EVENT_RISING_EDGE or GPIOEVENT_EVENT_FALLING_EDGE

Note

This struct is part of ABI v1 and is deprecated. Use ABI v2 and struct gpio_v2_line_event instead.

  • GPIO Error Codes

GPIO Error Codes

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Common GPIO error codes

EAGAIN (aka EWOULDBLOCK)

The device was opened in non-blocking mode and a read can't be performed as there is no data available.

EBADF

The file descriptor is not valid.

EBUSY

The ioctl can't be handled because the device is busy. Typically returned when an ioctl attempts something that would require the usage of a resource that was already allocated. The ioctl must not be retried without performing another action to fix the problem first.

EFAULT

There was a failure while copying data from/to userspace, probably caused by an invalid pointer reference.

EINVAL

One or more of the ioctl parameters are invalid or out of the allowed range. This is a widely used error code.

ENODEV

Device not found or was removed.

ENOMEM

There's not enough memory to handle the desired operation.

EPERM

Permission denied. Typically returned in response to an attempt to perform an action incompatible with the current line configuration.

EIO

I/O error. Typically returned when there are problems communicating with a hardware device or requesting features that hardware does not support. This could indicate broken or flaky hardware. It's a 'Something is wrong, I give up!' type of error.

ENXIO

Typically returned when a feature requiring interrupt support was requested, but the line does not support interrupts.

Note

  1. This list is not exhaustive; ioctls may return other error codes. Since errors may have side effects such as a driver reset, applications should abort on unexpected errors, or otherwise assume that the device is in a bad state.

  2. Request-specific error codes are listed in the individual requests descriptions.

GPIO_GET_CHIPINFO_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_GET_CHIPINFO_IOCTL - Get the publicly available information for a chip.

Synopsis #

GPIO_GET_CHIPINFO_IOCTL

int ioctl(int chip_fd, GPIO_GET_CHIPINFO_IOCTL, struct gpiochip_info *info)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • info

    The chip_info<gpiochip_info> to be populated.

Description #

Gets the publicly available information for a particular GPIO chip.

Return Value #

On success 0 and info is populated with the chip info.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_GET_LINEEVENT_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-get-line-ioctl.rst.

Name #

GPIO_GET_LINEEVENT_IOCTL - Request a line with edge detection from the kernel.

Synopsis #

GPIO_GET_LINEEVENT_IOCTL

int ioctl(int chip_fd, GPIO_GET_LINEEVENT_IOCTL, struct gpioevent_request *request)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • request

    The event_request<gpioevent_request> specifying the line to request and its configuration.

Description #

Request a line with edge detection from the kernel.

On success, the requesting process is granted exclusive access to the line value and may receive events when edges are detected on the line, as described in gpio-lineevent-data-read.rst.

The state of a line is guaranteed to remain as requested until the returned file descriptor is closed. Once the file descriptor is closed, the state of the line becomes uncontrolled from the userspace perspective, and may revert to its default state.

Requesting a line already in use is an error (EBUSY).

Requesting edge detection on a line that does not support interrupts is an error (ENXIO).

As with the line handle, the bias configuration is best effort.

Closing the chip_fd has no effect on existing line events.

Configuration Rules #

The following configuration rules apply:

The line event is requested as an input, so no flags specific to output lines, GPIOHANDLE_REQUEST_OUTPUT, GPIOHANDLE_REQUEST_OPEN_DRAIN, or GPIOHANDLE_REQUEST_OPEN_SOURCE, may be set.

Only one bias flag, GPIOHANDLE_REQUEST_BIAS_xxx, may be set. If no bias flags are set then the bias configuration is not changed.

The edge flags, GPIOEVENT_REQUEST_RISING_EDGE and GPIOEVENT_REQUEST_FALLING_EDGE, may be combined to detect both rising and falling edges.

Requesting an invalid configuration is an error (EINVAL).

Return Value #

On success 0 and the request.fd<gpioevent_request> contains the file descriptor for the request.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_GET_LINEHANDLE_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-get-line-ioctl.rst.

Name #

GPIO_GET_LINEHANDLE_IOCTL - Request a line or lines from the kernel.

Synopsis #

GPIO_GET_LINEHANDLE_IOCTL

int ioctl(int chip_fd, GPIO_GET_LINEHANDLE_IOCTL, struct gpiohandle_request *request)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • request

    The handle_request<gpiohandle_request> specifying the lines to request and their configuration.

Description #

Request a line or lines from the kernel.

While multiple lines may be requested, the same configuration applies to all lines in the request.

On success, the requesting process is granted exclusive access to the line value and write access to the line configuration.

The state of a line, including the value of output lines, is guaranteed to remain as requested until the returned file descriptor is closed. Once the file descriptor is closed, the state of the line becomes uncontrolled from the userspace perspective, and may revert to its default state.

Requesting a line already in use is an error (EBUSY).

Closing the chip_fd has no effect on existing line handles.

Configuration Rules #

The following configuration rules apply:

The direction flags, GPIOHANDLE_REQUEST_INPUT and GPIOHANDLE_REQUEST_OUTPUT, cannot be combined. If neither are set then the only other flag that may be set is GPIOHANDLE_REQUEST_ACTIVE_LOW and the line is requested "as-is" to allow reading of the line value without altering the electrical configuration.

The drive flags, GPIOHANDLE_REQUEST_OPEN_xxx, require the GPIOHANDLE_REQUEST_OUTPUT to be set. Only one drive flag may be set. If none are set then the line is assumed push-pull.

Only one bias flag, GPIOHANDLE_REQUEST_BIAS_xxx, may be set, and it requires a direction flag to also be set. If no bias flags are set then the bias configuration is not changed.

Requesting an invalid configuration is an error (EINVAL).

Configuration Support #

Where the requested configuration is not directly supported by the underlying hardware and driver, the kernel applies one of these approaches:

  • reject the request

  • emulate the feature in software

  • treat the feature as best effort

The approach applied depends on whether the feature can reasonably be emulated in software, and the impact on the hardware and userspace if the feature is not supported. The approach applied for each feature is as follows:

Feature

Approach

Bias

best effort

Direction

reject

Drive

emulate

Bias is treated as best effort to allow userspace to apply the same configuration for platforms that support internal bias as those that require external bias. Worst case the line floats rather than being biased as expected.

Drive is emulated by switching the line to an input when the line should not be driven.

In all cases, the configuration reported by gpio-get-lineinfo-ioctl.rst is the requested configuration, not the resulting hardware configuration. Userspace cannot determine if a feature is supported in hardware, is emulated, or is best effort.

Return Value #

On success 0 and the request.fd<gpiohandle_request> contains the file descriptor for the request.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_GET_LINEINFO_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-get-lineinfo-ioctl.rst.

Name #

GPIO_GET_LINEINFO_IOCTL - Get the publicly available information for a line.

Synopsis #

GPIO_GET_LINEINFO_IOCTL

int ioctl(int chip_fd, GPIO_GET_LINEINFO_IOCTL, struct gpioline_info *info)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • info

    The line_info<gpioline_info> to be populated, with the offset field set to indicate the line to be collected.

Description #

Get the publicly available information for a line.

This information is available independent of whether the line is in use.

Note

The line info does not include the line value.

The line must be requested using gpio-get-linehandle-ioctl.rst or gpio-get-lineevent-ioctl.rst to access its value.

Return Value #

On success 0 and info is populated with the chip info.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_GET_LINEINFO_UNWATCH_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_GET_LINEINFO_UNWATCH_IOCTL - Disable watching a line for changes to its requested state and configuration information.

Synopsis #

GPIO_GET_LINEINFO_UNWATCH_IOCTL

int ioctl(int chip_fd, GPIO_GET_LINEINFO_UNWATCH_IOCTL, u32 *offset)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • offset

    The offset of the line to no longer watch.

Description #

Remove the line from the list of lines being watched on this chip_fd.

This is the reverse of gpio-v2-get-lineinfo-watch-ioctl.rst (v2) and gpio-get-lineinfo-watch-ioctl.rst (v1).

Unwatching a line that is not watched is an error (EBUSY).

First added in 5.7.

Return Value #

On success 0.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_GET_LINEINFO_WATCH_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-get-lineinfo-watch-ioctl.rst.

Name #

GPIO_GET_LINEINFO_WATCH_IOCTL - Enable watching a line for changes to its request state and configuration information.

Synopsis #

GPIO_GET_LINEINFO_WATCH_IOCTL

int ioctl(int chip_fd, GPIO_GET_LINEINFO_WATCH_IOCTL, struct gpioline_info *info)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • info

    The line_info<gpioline_info> struct to be populated, with the offset set to indicate the line to watch

Description #

Enable watching a line for changes to its request state and configuration information. Changes to line info include a line being requested, released or reconfigured.

Note

Watching line info is not generally required, and would typically only be used by a system monitoring component.

The line info does NOT include the line value.

The line must be requested using gpio-get-linehandle-ioctl.rst or gpio-get-lineevent-ioctl.rst to access its value, and the line event can monitor a line for events using gpio-lineevent-data-read.rst.

By default all lines are unwatched when the GPIO chip is opened.

Multiple lines may be watched simultaneously by adding a watch for each.

Once a watch is set, any changes to line info will generate events which can be read from the chip_fd as described in gpio-lineinfo-changed-read.rst.

Adding a watch to a line that is already watched is an error (EBUSY).

Watches are specific to the chip_fd and are independent of watches on the same GPIO chip opened with a separate call to open().

First added in 5.7.

Return Value #

On success 0 and info is populated with the current line info.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIOHANDLE_GET_LINE_VALUES_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-line-get-values-ioctl.rst.

Name #

GPIOHANDLE_GET_LINE_VALUES_IOCTL - Get the values of all requested lines.

Synopsis #

GPIOHANDLE_GET_LINE_VALUES_IOCTL

int ioctl(int handle_fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, struct gpiohandle_data *values)

Arguments #

  • handle_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpiohandle_request> by gpio-get-linehandle-ioctl.rst.

  • values

    The line_values<gpiohandle_data> to be populated.

Description #

Get the values of all requested lines.

The values returned are logical, indicating if the line is active or inactive. The GPIOHANDLE_REQUEST_ACTIVE_LOW flag controls the mapping between physical values (high/low) and logical values (active/inactive). If GPIOHANDLE_REQUEST_ACTIVE_LOW is not set then high is active and low is inactive. If GPIOHANDLE_REQUEST_ACTIVE_LOW is set then low is active and high is inactive.

The values of both input and output lines may be read.

For output lines, the value returned is driver and configuration dependent and may be either the output buffer (the last requested value set) or the input buffer (the actual level of the line), and depending on the hardware and configuration these may differ.

This ioctl can also be used to read the line value for line events, substituting the event_fd for the handle_fd. As there is only one line requested in that case, only the one value is returned in values.

Return Value #

On success 0 and values populated with the values read.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIOHANDLE_SET_CONFIG_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-line-set-config-ioctl.rst.

Name #

GPIOHANDLE_SET_CONFIG_IOCTL - Update the configuration of previously requested lines.

Synopsis #

GPIOHANDLE_SET_CONFIG_IOCTL

int ioctl(int handle_fd, GPIOHANDLE_SET_CONFIG_IOCTL, struct gpiohandle_config *config)

Arguments #

  • handle_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpiohandle_request> by gpio-get-linehandle-ioctl.rst.

  • config

    The new configuration<gpiohandle_config> to apply to the requested lines.

Description #

Update the configuration of previously requested lines, without releasing the line or introducing potential glitches.

The configuration applies to all requested lines.

The same Configuration Rules and Configuration Support that apply when requesting the lines also apply when updating the line configuration, with the additional restriction that a direction flag must be set. Requesting an invalid configuration, including without a direction flag set, is an error (EINVAL).

The motivating use case for this command is changing direction of bi-directional lines between input and output, but it may be used more generally to move lines seamlessly from one configuration state to another.

To only change the value of output lines, use gpio-handle-set-line-values-ioctl.rst.

First added in 5.5.

Return Value #

On success 0.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_HANDLE_SET_LINE_VALUES_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-line-set-values-ioctl.rst.

Name #

GPIO_HANDLE_SET_LINE_VALUES_IOCTL - Set the values of all requested output lines.

Synopsis #

GPIO_HANDLE_SET_LINE_VALUES_IOCTL

int ioctl(int handle_fd, GPIO_HANDLE_SET_LINE_VALUES_IOCTL, struct gpiohandle_data *values)

Arguments #

  • handle_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpiohandle_request> by gpio-get-linehandle-ioctl.rst.

  • values

    The line_values<gpiohandle_data> to set.

Description #

Set the values of all requested output lines.

The values set are logical, indicating if the line is to be active or inactive. The GPIOHANDLE_REQUEST_ACTIVE_LOW flag controls the mapping between logical values (active/inactive) and physical values (high/low). If GPIOHANDLE_REQUEST_ACTIVE_LOW is not set then active is high and inactive is low. If GPIOHANDLE_REQUEST_ACTIVE_LOW is set then active is low and inactive is high.

Only the values of output lines may be set. Attempting to set the value of input lines is an error (EPERM).

Return Value #

On success 0.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_LINEEVENT_DATA_READ

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-line-event-read.rst.

Name #

GPIO_LINEEVENT_DATA_READ - Read edge detection events from a line event.

Synopsis #

int read(int event_fd, void *buf, size_t count)

Arguments #

  • event_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpioevent_request> by gpio-get-lineevent-ioctl.rst.

  • buf

    The buffer to contain the events<gpioevent_data>.

  • count

    The number of bytes available in buf, which must be at least the size of a gpioevent_data.

Description #

Read edge detection events for a line from a line event.

Edge detection must be enabled for the input line using either GPIOEVENT_REQUEST_RISING_EDGE or GPIOEVENT_REQUEST_FALLING_EDGE, or both. Edge events are then generated whenever edge interrupts are detected on the input line.

Edges are defined in terms of changes to the logical line value, so an inactive to active transition is a rising edge. If GPIOHANDLE_REQUEST_ACTIVE_LOW is set then logical polarity is the opposite of physical polarity, and GPIOEVENT_REQUEST_RISING_EDGE then corresponds to a falling physical edge.

The kernel captures and timestamps edge events as close as possible to their occurrence and stores them in a buffer from where they can be read by userspace at its convenience using read().

The source of the clock for event.timestamp<gpioevent_data> is CLOCK_MONOTONIC, except for kernels earlier than Linux 5.7 when it was CLOCK_REALTIME. There is no indication in the gpioevent_data as to which clock source is used, it must be determined from either the kernel version or sanity checks on the timestamp itself.

Events read from the buffer are always in the same order that they were detected by the kernel.

The size of the kernel event buffer is fixed at 16 events.

The buffer may overflow if bursts of events occur quicker than they are read by userspace. If an overflow occurs then the most recent event is discarded. Overflow cannot be detected from userspace.

To minimize the number of calls required to copy events from the kernel to userspace, read() supports copying multiple events. The number of events copied is the lower of the number available in the kernel buffer and the number that will fit in the userspace buffer (buf).

The read() will block if no event is available and the event_fd has not been set O_NONBLOCK.

The presence of an event can be tested for by checking that the event_fd is readable using poll() or an equivalent.

Return Value #

On success the number of bytes read, which will be a multiple of the size of a gpio_lineevent_data event.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_LINEINFO_CHANGED_READ

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This ioctl is part of chardev_v1.rst and is obsoleted by gpio-v2-lineinfo-changed-read.rst.

Name #

GPIO_LINEINFO_CHANGED_READ - Read line info change events for watched lines from the chip.

Synopsis #

int read(int chip_fd, void *buf, size_t count)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • buf

    The buffer to contain the events<gpioline_info_changed>.

  • count

    The number of bytes available in buf, which must be at least the size of a gpioline_info_changed event.

Description #

Read line info change events for watched lines from the chip.

Note

Monitoring line info changes is not generally required, and would typically only be performed by a system monitoring component.

These events relate to changes in a line's request state or configuration, not its value. Use gpio-lineevent-data-read.rst to receive events when a line changes value.

A line must be watched using gpio-get-lineinfo-watch-ioctl.rst to generate info changed events. Subsequently, a request, release, or reconfiguration of the line will generate an info changed event.

The kernel timestamps events when they occur and stores them in a buffer from where they can be read by userspace at its convenience using read().

The size of the kernel event buffer is fixed at 32 events per chip_fd.

The buffer may overflow if bursts of events occur quicker than they are read by userspace. If an overflow occurs then the most recent event is discarded. Overflow cannot be detected from userspace.

Events read from the buffer are always in the same order that they were detected by the kernel, including when multiple lines are being monitored by the one chip_fd.

To minimize the number of calls required to copy events from the kernel to userspace, read() supports copying multiple events. The number of events copied is the lower of the number available in the kernel buffer and the number that will fit in the userspace buffer (buf).

A read() will block if no event is available and the chip_fd has not been set O_NONBLOCK.

The presence of an event can be tested for by checking that the chip_fd is readable using poll() or an equivalent.

First added in 5.7.

Return Value #

On success the number of bytes read, which will be a multiple of the size of a gpioline_info_changed event.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_GET_LINE_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_GET_LINE_IOCTL - Request a line or lines from the kernel.

Synopsis #

GPIO_V2_GET_LINE_IOCTL

int ioctl(int chip_fd, GPIO_V2_GET_LINE_IOCTL, struct gpio_v2_line_request *request)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • request

    The line_request<gpio_v2_line_request> specifying the lines to request and their configuration.

Description #

On success, the requesting process is granted exclusive access to the line value, write access to the line configuration, and may receive events when edges are detected on the line, all of which are described in more detail in Line Request.

A number of lines may be requested in the one line request, and request operations are performed on the requested lines by the kernel as atomically as possible. e.g. gpio-v2-line-get-values-ioctl.rst will read all the requested lines at once.

The state of a line, including the value of output lines, is guaranteed to remain as requested until the returned file descriptor is closed. Once the file descriptor is closed, the state of the line becomes uncontrolled from the userspace perspective, and may revert to its default state.

Requesting a line already in use is an error (EBUSY).

Closing the chip_fd has no effect on existing line requests.

Configuration Rules #

For any given requested line, the following configuration rules apply:

The direction flags, GPIO_V2_LINE_FLAG_INPUT and GPIO_V2_LINE_FLAG_OUTPUT, cannot be combined. If neither are set then the only other flag that may be set is GPIO_V2_LINE_FLAG_ACTIVE_LOW and the line is requested "as-is" to allow reading of the line value without altering the electrical configuration.

The drive flags, GPIO_V2_LINE_FLAG_OPEN_xxx, require the GPIO_V2_LINE_FLAG_OUTPUT to be set. Only one drive flag may be set. If none are set then the line is assumed push-pull.

Only one bias flag, GPIO_V2_LINE_FLAG_BIAS_xxx, may be set, and it requires a direction flag to also be set. If no bias flags are set then the bias configuration is not changed.

The edge flags, GPIO_V2_LINE_FLAG_EDGE_xxx, require GPIO_V2_LINE_FLAG_INPUT to be set and may be combined to detect both rising and falling edges. Requesting edge detection from a line that does not support it is an error (ENXIO).

Only one event clock flag, GPIO_V2_LINE_FLAG_EVENT_CLOCK_xxx, may be set. If none are set then the event clock defaults to CLOCK_MONOTONIC. The GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE flag requires supporting hardware and a kernel with CONFIG_HTE set. Requesting HTE from a device that doesn't support it is an error (EOPNOTSUPP).

The debounce_period_us<gpio_v2_line_attribute> attribute may only be applied to lines with GPIO_V2_LINE_FLAG_INPUT set. When set, debounce applies to both the values returned by gpio-v2-line-get-values-ioctl.rst and the edges returned by gpio-v2-line-event-read.rst. If not supported directly by hardware, debouncing is emulated in software by the kernel. Requesting debounce on a line that supports neither debounce in hardware nor interrupts, as required for software emulation, is an error (ENXIO).

Requesting an invalid configuration is an error (EINVAL).

Configuration Support #

Where the requested configuration is not directly supported by the underlying hardware and driver, the kernel applies one of these approaches:

  • reject the request

  • emulate the feature in software

  • treat the feature as best effort

The approach applied depends on whether the feature can reasonably be emulated in software, and the impact on the hardware and userspace if the feature is not supported. The approach applied for each feature is as follows:

Feature

Approach

Bias

best effort

Debounce

emulate

Direction

reject

Drive

emulate

Edge Detection

reject

Bias is treated as best effort to allow userspace to apply the same configuration for platforms that support internal bias as those that require external bias. Worst case the line floats rather than being biased as expected.

Debounce is emulated by applying a filter to hardware interrupts on the line. An edge event is generated after an edge is detected and the line remains stable for the debounce period. The event timestamp corresponds to the end of the debounce period.

Drive is emulated by switching the line to an input when the line should not be actively driven.

Edge detection requires interrupt support, and is rejected if that is not supported. Emulation by polling can still be performed from userspace.

In all cases, the configuration reported by gpio-v2-get-lineinfo-ioctl.rst is the requested configuration, not the resulting hardware configuration. Userspace cannot determine if a feature is supported in hardware, is emulated, or is best effort.

Return Value #

On success 0 and the request.fd<gpio_v2_line_request> contains the file descriptor for the request.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_GET_LINEINFO_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_GET_LINEINFO_IOCTL - Get the publicly available information for a line.

Synopsis #

GPIO_V2_GET_LINEINFO_IOCTL

int ioctl(int chip_fd, GPIO_V2_GET_LINEINFO_IOCTL, struct gpio_v2_line_info *info)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • info

    The line_info<gpio_v2_line_info> to be populated, with the offset field set to indicate the line to be collected.

Description #

Get the publicly available information for a line.

This information is available independent of whether the line is in use.

Note

The line info does not include the line value.

The line must be requested using gpio-v2-get-line-ioctl.rst to access its value.

Return Value #

On success 0 and info is populated with the chip info.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_GET_LINEINFO_WATCH_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_GET_LINEINFO_WATCH_IOCTL - Enable watching a line for changes to its request state and configuration information.

Synopsis #

GPIO_V2_GET_LINEINFO_WATCH_IOCTL

int ioctl(int chip_fd, GPIO_V2_GET_LINEINFO_WATCH_IOCTL, struct gpio_v2_line_info *info)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • info

    The line_info<gpio_v2_line_info> struct to be populated, with the offset set to indicate the line to watch

Description #

Enable watching a line for changes to its request state and configuration information. Changes to line info include a line being requested, released or reconfigured.

Note

Watching line info is not generally required, and would typically only be used by a system monitoring component.

The line info does NOT include the line value. The line must be requested using gpio-v2-get-line-ioctl.rst to access its value, and the line request can monitor a line for events using gpio-v2-line-event-read.rst.

By default all lines are unwatched when the GPIO chip is opened.

Multiple lines may be watched simultaneously by adding a watch for each.

Once a watch is set, any changes to line info will generate events which can be read from the chip_fd as described in gpio-v2-lineinfo-changed-read.rst.

Adding a watch to a line that is already watched is an error (EBUSY).

Watches are specific to the chip_fd and are independent of watches on the same GPIO chip opened with a separate call to open().

Return Value #

On success 0 and info is populated with the current line info.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_LINE_EVENT_READ

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_LINE_EVENT_READ - Read edge detection events for lines from a request.

Synopsis #

int read(int req_fd, void *buf, size_t count)

Arguments #

  • req_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.

  • buf

    The buffer to contain the events<gpio_v2_line_event>.

  • count

    The number of bytes available in buf, which must be at least the size of a gpio_v2_line_event.

Description #

Read edge detection events for lines from a request.

Edge detection must be enabled for the input line using either GPIO_V2_LINE_FLAG_EDGE_RISING or GPIO_V2_LINE_FLAG_EDGE_FALLING, or both. Edge events are then generated whenever edge interrupts are detected on the input line.

Edges are defined in terms of changes to the logical line value, so an inactive to active transition is a rising edge. If GPIO_V2_LINE_FLAG_ACTIVE_LOW is set then logical polarity is the opposite of physical polarity, and GPIO_V2_LINE_FLAG_EDGE_RISING then corresponds to a falling physical edge.

The kernel captures and timestamps edge events as close as possible to their occurrence and stores them in a buffer from where they can be read by userspace at its convenience using read().

Events read from the buffer are always in the same order that they were detected by the kernel, including when multiple lines are being monitored by the one request.

The size of the kernel event buffer is fixed at the time of line request creation, and can be influenced by the request.event_buffer_size<gpio_v2_line_request>. The default size is 16 times the number of lines requested.

The buffer may overflow if bursts of events occur quicker than they are read by userspace. If an overflow occurs then the oldest buffered event is discarded. Overflow can be detected from userspace by monitoring the event sequence numbers.

To minimize the number of calls required to copy events from the kernel to userspace, read() supports copying multiple events. The number of events copied is the lower of the number available in the kernel buffer and the number that will fit in the userspace buffer (buf).

Changing the edge detection flags using gpio-v2-line-set-config-ioctl.rst does not remove or modify the events already contained in the kernel event buffer.

The read() will block if no event is available and the req_fd has not been set O_NONBLOCK.

The presence of an event can be tested for by checking that the req_fd is readable using poll() or an equivalent.

Return Value #

On success the number of bytes read, which will be a multiple of the size of a gpio_v2_line_event event.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_LINE_GET_VALUES_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_LINE_GET_VALUES_IOCTL - Get the values of requested lines.

Synopsis #

GPIO_V2_LINE_GET_VALUES_IOCTL

int ioctl(int req_fd, GPIO_V2_LINE_GET_VALUES_IOCTL, struct gpio_v2_line_values *values)

Arguments #

  • req_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.

  • values

    The line_values<gpio_v2_line_values> to get with the mask set to indicate the subset of requested lines to get.

Description #

Get the values of requested lines.

The values returned are logical, indicating if the line is active or inactive. The GPIO_V2_LINE_FLAG_ACTIVE_LOW flag controls the mapping between physical values (high/low) and logical values (active/inactive). If GPIO_V2_LINE_FLAG_ACTIVE_LOW is not set then high is active and low is inactive. If GPIO_V2_LINE_FLAG_ACTIVE_LOW is set then low is active and high is inactive.

The values of both input and output lines may be read.

For output lines, the value returned is driver and configuration dependent and may be either the output buffer (the last requested value set) or the input buffer (the actual level of the line), and depending on the hardware and configuration these may differ.

Return Value #

On success 0 and the corresponding values.bits<gpio_v2_line_values> contain the value read.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_LINE_SET_CONFIG_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_LINE_SET_CONFIG_IOCTL - Update the configuration of previously requested lines.

Synopsis #

GPIO_V2_LINE_SET_CONFIG_IOCTL

int ioctl(int req_fd, GPIO_V2_LINE_SET_CONFIG_IOCTL, struct gpio_v2_line_config *config)

Arguments #

  • req_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.

  • config

    The new configuration<gpio_v2_line_config> to apply to the requested lines.

Description #

Update the configuration of previously requested lines, without releasing the line or introducing potential glitches.

The new configuration must specify a configuration for all requested lines.

The same Configuration Rules and Configuration Support that apply when requesting the lines also apply when updating the line configuration, with the additional restriction that a direction flag must be set to enable reconfiguration. If no direction flag is set in the configuration for a given line then the configuration for that line is left unchanged.

The motivating use case for this command is changing direction of bi-directional lines between input and output, but it may also be used to dynamically control edge detection, or more generally move lines seamlessly from one configuration state to another.

To only change the value of output lines, use gpio-v2-line-set-values-ioctl.rst.

Return Value #

On success 0.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_LINE_SET_VALUES_IOCTL

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_LINE_SET_VALUES_IOCTL - Set the values of requested output lines.

Synopsis #

GPIO_V2_LINE_SET_VALUES_IOCTL

int ioctl(int req_fd, GPIO_V2_LINE_SET_VALUES_IOCTL, struct gpio_v2_line_values *values)

Arguments #

  • req_fd

    The file descriptor of the GPIO character device, as returned in the request.fd<gpio_v2_line_request> by gpio-v2-get-line-ioctl.rst.

  • values

    The line_values<gpio_v2_line_values> to set with the mask set to indicate the subset of requested lines to set and bits set to indicate the new value.

Description #

Set the values of requested output lines.

The values set are logical, indicating if the line is to be active or inactive. The GPIO_V2_LINE_FLAG_ACTIVE_LOW flag controls the mapping between logical values (active/inactive) and physical values (high/low). If GPIO_V2_LINE_FLAG_ACTIVE_LOW is not set then active is high and inactive is low. If GPIO_V2_LINE_FLAG_ACTIVE_LOW is set then active is low and inactive is high.

Only the values of output lines may be set. Attempting to set the value of an input line is an error (EPERM).

Return Value #

On success 0.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

GPIO_V2_LINEINFO_CHANGED_READ

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Name #

GPIO_V2_LINEINFO_CHANGED_READ - Read line info changed events for watched lines from the chip.

Synopsis #

int read(int chip_fd, void *buf, size_t count)

Arguments #

  • chip_fd

    The file descriptor of the GPIO character device returned by open().

  • buf

    The buffer to contain the events<gpio_v2_line_info_changed>.

  • count

    The number of bytes available in buf, which must be at least the size of a gpio_v2_line_info_changed event.

Description #

Read line info changed events for watched lines from the chip.

Note

Monitoring line info changes is not generally required, and would typically only be performed by a system monitoring component.

These events relate to changes in a line's request state or configuration, not its value. Use gpio-v2-line-event-read.rst to receive events when a line changes value.

A line must be watched using gpio-v2-get-lineinfo-watch-ioctl.rst to generate info changed events. Subsequently, a request, release, or reconfiguration of the line will generate an info changed event.

The kernel timestamps events when they occur and stores them in a buffer from where they can be read by userspace at its convenience using read().

The size of the kernel event buffer is fixed at 32 events per chip_fd.

The buffer may overflow if bursts of events occur quicker than they are read by userspace. If an overflow occurs then the most recent event is discarded. Overflow cannot be detected from userspace.

Events read from the buffer are always in the same order that they were detected by the kernel, including when multiple lines are being monitored by the one chip_fd.

To minimize the number of calls required to copy events from the kernel to userspace, read() supports copying multiple events. The number of events copied is the lower of the number available in the kernel buffer and the number that will fit in the userspace buffer (buf).

A read() will block if no event is available and the chip_fd has not been set O_NONBLOCK.

The presence of an event can be tested for by checking that the chip_fd is readable using poll() or an equivalent.

Return Value #

On success the number of bytes read, which will be a multiple of the size of a gpio_v2_line_info_changed event.

On error -1 and the errno variable is set appropriately. Common error codes are described in error-codes.rst.

Obsolete GPIO Userspace APIs

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

  • Character Device Userspace API (v1)
  • Sysfs Interface

GPIO Sysfs Interface for Userspace

Peios Kernel / Userspace interfaces / System and device interfaces / GPIO

Warning

This API is obsoleted by the chardev.rst and the ABI documentation has been moved to Documentation/ABI/obsolete/sysfs-gpio.

New developments should use the chardev.rst, and existing developments are encouraged to migrate as soon as possible, as this API will be removed in the future.

This interface will continue to be maintained for the migration period, but new features will only be added to the new API.

The obsolete sysfs ABI #

Platforms which use the "gpiolib" implementors framework may choose to configure a sysfs user interface to GPIOs. This is different from the debugfs interface, since it provides control over GPIO direction and value instead of just showing a gpio state summary. Plus, it could be present on production systems without debugging support.

Given appropriate hardware documentation for the system, userspace could know for example that GPIO #23 controls the write protect line used to protect boot loader segments in flash memory. System upgrade procedures may need to temporarily remove that protection, first importing a GPIO, then changing its output state, then updating the code before re-enabling the write protection. In normal use, GPIO #23 would never be touched, and the kernel would have no need to know about it.

Again depending on appropriate hardware documentation, on some systems userspace GPIO can be used to determine system configuration data that standard kernels won't know about. And for some tasks, simple userspace GPIO drivers could be all that the system really needs.

Note

Do NOT abuse sysfs to control hardware that has proper kernel drivers. Please read Documentation/driver-api/gpio/drivers-on-gpio.rst to avoid reinventing kernel wheels in userspace.

I MEAN IT. REALLY.

Paths in Sysfs #

There are three kinds of entries in /sys/class/gpio:

  • Control interfaces used to get userspace control over GPIOs;

  • GPIOs themselves; and

  • GPIO controllers ("gpio_chip" instances).

That's in addition to standard files including the "device" symlink.

The control interfaces are write-only:

/sys/class/gpio/

  • "export" ...

    Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.

    Example: "echo 19 > export" will create a "gpio19" node for GPIO #19, if that's not requested by kernel code.

  • "unexport" ...

    Reverses the effect of exporting to userspace.

    Example: "echo 19 > unexport" will remove a "gpio19" node exported using the "export" file.

GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) and have the following read/write attributes:

/sys/class/gpio/gpioN/

  • "direction" ...

    reads as either "in" or "out". This value may normally be written. Writing as "out" defaults to initializing the value as low. To ensure glitch free operation, values "low" and "high" may be written to configure the GPIO as an output with that initial value.

    Note that this attribute will not exist if the kernel doesn't support changing the direction of a GPIO, or it was exported by kernel code that didn't explicitly allow userspace to reconfigure this GPIO's direction.

  • "value" ...

    reads as either 0 (inactive) or 1 (active). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as active.

    If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, use pread(2) to read the value at offset zero. Alternatively, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

  • "edge" ...

    reads as either "none", "rising", "falling", or "both". Write these strings to select the signal edge(s) that will make poll(2) on the "value" file return.

    This file exists only if the pin can be configured as an interrupt generating input pin.

  • "active_low" ...

    reads as either 0 (false) or 1 (true). Write any nonzero value to invert the value attribute both for reading and writing. Existing and subsequent poll(2) support configuration via the edge attribute for "rising" and "falling" edges will follow this setting.

GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the controller implementing GPIOs starting at #42) and have the following read-only attributes:

/sys/class/gpio/gpiochipN/

  • "base" ...

    same as N, the first GPIO managed by this chip

  • "label" ...

    provided for diagnostics (not always unique)

  • "ngpio" ...

    how many GPIOs this manages (N to N + ngpio - 1)

Board documentation should in most cases cover what GPIOs are used for what purposes. However, those numbers are not always stable; GPIOs on a daughtercard might be different depending on the base board being used, or other cards in the stack. In such cases, you may need to use the gpiochip nodes (possibly in conjunction with schematics) to determine the correct GPIO number to use for a given signal.

Exporting from Kernel code #

Kernel code can explicitly manage exports of GPIOs which have already been requested using gpio_request():

/* export the GPIO to userspace */
int gpiod_export(struct gpio_desc *desc, bool direction_may_change);

/* reverse gpiod_export() */
void gpiod_unexport(struct gpio_desc *desc);

/* create a sysfs link to an exported GPIO node */
int gpiod_export_link(struct device *dev, const char *name,
              struct gpio_desc *desc);

After a kernel driver requests a GPIO, it may only be made available in the sysfs interface by gpiod_export(). The driver can control whether the signal direction may change. This helps drivers prevent userspace code from accidentally clobbering important system state.

This explicit exporting can help with debugging (by making some kinds of experiments easier), or can provide an always-there interface that's suitable for documenting as part of a board support package.

After the GPIO has been exported, gpiod_export_link() allows creating symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can use this to provide the interface under their own device in sysfs with a descriptive name.

Peios Learn — documentation for Peios software.

Built with Trail.