# GPIO

---

# GPIO

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Reference for GPIO.

<a id="rst-gpio"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [Character Device Userspace API](</kernel/userspace/userspace-api/gpio/chardev.md>)
- [Obsolete Userspace APIs](</kernel/userspace/userspace-api/gpio/obsolete.md>)

---

# 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.

<a id="rst-gpio-character-device-userspace-api"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

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](/kernel/hardware/driver-api/gpio/drivers-on-gpio.md) to avoid reinventing kernel wheels in userspace.
>
> Similarly, for multi-function lines there may be other subsystems, such as [Documentation/spi/index.rst](/kernel/hardware/spi/index.md), [Documentation/i2c/index.rst](/kernel/hardware/i2c/index.md), [Documentation/driver-api/pwm.rst](/kernel/hardware/driver-api/services/pwm.md), [Documentation/w1/index.rst](/kernel/hardware/w1/index.md) 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](<#rst-gpio-v2-chip>) and the [Line Request](<#rst-gpio-v2-line-request>).

<a id="rst-chip"></a>
<a id="rst-gpio-v2-chip"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md) 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`.

<a id="rst-operations"></a>

### Operations

The following operations may be performed on the chip:

- [Get Line](</kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md>)
- [Get Chip Info](</kernel/userspace/userspace-api/gpio/gpio-get-chipinfo-ioctl.md>)
- [Get Line Info](</kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-ioctl.md>)
- [Watch Line Info](</kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-watch-ioctl.md>)
- [Unwatch Line Info](</kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-unwatch-ioctl.md>)
- [Read Line Info Changed Events](</kernel/userspace/userspace-api/gpio/gpio-v2-lineinfo-changed-read.md>)

<a id="rst-line-request"></a>
<a id="rst-gpio-v2-line-request"></a>

## Line Request

Line requests are created by [gpio-v2-get-line-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md) 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

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

<a id="rst-operations-1"></a>

### Operations

The following operations may be performed on the line request:

- [Get Line Values](</kernel/userspace/userspace-api/gpio/gpio-v2-line-get-values-ioctl.md>)
- [Set Line Values](</kernel/userspace/userspace-api/gpio/gpio-v2-line-set-values-ioctl.md>)
- [Read Line Edge Events](</kernel/userspace/userspace-api/gpio/gpio-v2-line-event-read.md>)
- [Reconfigure Lines](</kernel/userspace/userspace-api/gpio/gpio-v2-line-set-config-ioctl.md>)

<a id="rst-types"></a>

## Types

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


#### <a id="c-struct-gpiochip_info"></a>`struct gpiochip_info`

Information about a certain GPIO chip

**Definition**

```c
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

#### <a id="c-enum-gpio_v2_line_flag"></a>`enum gpio_v2_line_flag`

[`struct gpio_v2_line_attribute`](#c-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

#### <a id="c-struct-gpio_v2_line_values"></a>`struct gpio_v2_line_values`

Values of GPIO lines

**Definition**

```c
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`](#c-struct-gpio_v2_line_request).offsets

#### <a id="c-enum-gpio_v2_line_attr_id"></a>`enum gpio_v2_line_attr_id`

[`struct gpio_v2_line_attribute`](#c-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

#### <a id="c-struct-gpio_v2_line_attribute"></a>`struct gpio_v2_line_attribute`

a configurable attribute of a line

**Definition**

```c
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`](#c-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`](#c-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`](#c-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`](#c-struct-gpio_v2_line_request).offsets
- `debounce_period_us`: if id is `GPIO_V2_LINE_ATTR_ID_DEBOUNCE`, the
  desired debounce period, in microseconds

#### <a id="c-struct-gpio_v2_line_config_attribute"></a>`struct gpio_v2_line_config_attribute`

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

**Definition**

```c
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`](#c-struct-gpio_v2_line_request).offsets

#### <a id="c-struct-gpio_v2_line_config"></a>`struct gpio_v2_line_config`

Configuration for GPIO lines

**Definition**

```c
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`](#c-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.

#### <a id="c-struct-gpio_v2_line_request"></a>`struct gpio_v2_line_request`

Information about a request for GPIO lines

**Definition**

```c
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

#### <a id="c-struct-gpio_v2_line_info"></a>`struct gpio_v2_line_info`

Information about a certain GPIO line

**Definition**

```c
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`](#c-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

#### <a id="c-enum-gpio_v2_line_changed_type"></a>`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

#### <a id="c-struct-gpio_v2_line_info_changed"></a>`struct gpio_v2_line_info_changed`

Information about a change in status of a GPIO line

**Definition**

```c
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`](#c-enum-gpio_v2_line_changed_type)
- `padding`: reserved for future use

#### <a id="c-enum-gpio_v2_line_event_id"></a>`enum gpio_v2_line_event_id`

[`struct gpio_v2_line_event`](#c-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

#### <a id="c-struct-gpio_v2_line_event"></a>`struct gpio_v2_line_event`

The actual event being pushed to userspace

**Definition**

```c
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`](#c-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](</kernel/userspace/userspace-api/gpio/error-codes.md>)

---

# GPIO Character Device Userspace API (v1)

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> First added in 4.8.

<a id="rst-gpio-character-device-userspace-api-v1"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This API is obsoleted by [chardev.rst](/kernel/userspace/userspace-api/gpio/chardev.md) (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](<#rst-gpio-v1-chip>), the [Line Handle](<#rst-gpio-v1-line-handle>), and the [Line Event](<#rst-gpio-v1-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.

<a id="rst-chip"></a>
<a id="rst-gpio-v1-chip"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md) and the resulting line handle is used to access the GPIO chip's lines, or [gpio-get-lineevent-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md) 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`.

<a id="rst-operations"></a>

### Operations

The following operations may be performed on the chip:

- [Get Line Handle](</kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md>)
- [Get Line Event](</kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md>)
- [Get Chip Info](</kernel/userspace/userspace-api/gpio/gpio-get-chipinfo-ioctl.md>)
- [Get Line Info](</kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-ioctl.md>)
- [Watch Line Info](</kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-watch-ioctl.md>)
- [Unwatch Line Info](</kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-unwatch-ioctl.md>)
- [Read Line Info Changed Events](</kernel/userspace/userspace-api/gpio/gpio-lineinfo-changed-read.md>)

<a id="rst-line-handle"></a>
<a id="rst-gpio-v1-line-handle"></a>

## Line Handle

Line handles are created by [gpio-get-linehandle-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md) 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md).

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

<a id="rst-operations-1"></a>

### Operations

The following operations may be performed on the line handle:

- [Get Line Values](</kernel/userspace/userspace-api/gpio/gpio-handle-get-line-values-ioctl.md>)
- [Set Line Values](</kernel/userspace/userspace-api/gpio/gpio-handle-set-line-values-ioctl.md>)
- [Reconfigure Lines](</kernel/userspace/userspace-api/gpio/gpio-handle-set-config-ioctl.md>)

<a id="rst-line-event"></a>
<a id="rst-gpio-v1-line-event"></a>

## Line Event

Line events are created by [gpio-get-lineevent-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md) 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](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md).

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

<a id="rst-operations-2"></a>

### Operations

The following operations may be performed on the line event:

- [Get Line Value](</kernel/userspace/userspace-api/gpio/gpio-handle-get-line-values-ioctl.md>)
- [Read Line Edge Events](</kernel/userspace/userspace-api/gpio/gpio-lineevent-data-read.md>)

<a id="rst-types"></a>

## 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.


#### <a id="c-struct-gpioline_info"></a>`struct gpioline_info`

Information about a certain GPIO line

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_info) instead.

#### <a id="c-struct-gpioline_info_changed"></a>`struct gpioline_info_changed`

Information about a change in status of a GPIO line

**Definition**

```c
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`](#c-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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_info_changed) instead.

#### <a id="c-struct-gpiohandle_request"></a>`struct gpiohandle_request`

Information about a GPIO handle request

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_request) instead.

#### <a id="c-struct-gpiohandle_config"></a>`struct gpiohandle_config`

Configuration for a GPIO handle request

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_config) instead.

#### <a id="c-struct-gpiohandle_data"></a>`struct gpiohandle_data`

Information of values on a GPIO handle

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_values) instead.

#### <a id="c-struct-gpioevent_request"></a>`struct gpioevent_request`

Information about a GPIO event request

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_request) instead.

#### <a id="c-struct-gpioevent_data"></a>`struct gpioevent_data`

The actual event being pushed to userspace

**Definition**

```c
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`](/kernel/userspace/userspace-api/gpio/chardev.md#c-struct-gpio_v2_line_event) instead.


- [GPIO Error Codes](</kernel/userspace/userspace-api/gpio/error-codes.md>)

---

# GPIO Error Codes

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Reference for GPIO Error Codes.

<a id="rst-gpio-error-codes"></a>
<a id="rst-gpio-errors"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-gpio-errors-1"></a>



<!-- tabularcolumns: |p{2.5cm}|p{15.0cm}| -->


<table>
<caption>Common GPIO error codes</caption>
<tbody>
<tr>
<td>


`EAGAIN` (aka `EWOULDBLOCK`)

</td>
<td>


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

</td>
</tr>
<tr>
<td>


`EBADF`

</td>
<td>


The file descriptor is not valid.

</td>
</tr>
<tr>
<td>


`EBUSY`

</td>
<td>


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.

</td>
</tr>
<tr>
<td>


`EFAULT`

</td>
<td>


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

</td>
</tr>
<tr>
<td>


`EINVAL`

</td>
<td>


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

</td>
</tr>
<tr>
<td>


`ENODEV`

</td>
<td>


Device not found or was removed.

</td>
</tr>
<tr>
<td>


`ENOMEM`

</td>
<td>


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

</td>
</tr>
<tr>
<td>


`EPERM`

</td>
<td>


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

</td>
</tr>
<tr>
<td>


`EIO`

</td>
<td>


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.

</td>
</tr>
<tr>
<td>


`ENXIO`

</td>
<td>


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

</td>
</tr>
</tbody>
</table>

> [!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_

> Duplicate implicit target name: "gpio_get_chipinfo_ioctl".

<a id="rst-gpio-get-chipinfo-ioctl-1"></a>
<a id="rst-gpio-get-chipinfo-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_GET_CHIPINFO_IOCTL - Get the publicly available information for a chip.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_CHIPINFO_IOCTL"></a>

```c
GPIO_GET_CHIPINFO_IOCTL
```

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

<a id="rst-arguments"></a>

## Arguments

- **`chip_fd`**

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

- **`info`**

  The `chip_info<gpiochip_info>` to be populated.

<a id="rst-description"></a>

## Description

Gets the publicly available information for a particular GPIO chip.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_GET_LINEEVENT_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_get_lineevent_ioctl".

<a id="rst-gpio-get-lineevent-ioctl-1"></a>
<a id="rst-gpio-get-lineevent-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-get-line-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_LINEEVENT_IOCTL"></a>

```c
GPIO_GET_LINEEVENT_IOCTL
```

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-lineevent-data-read.md).

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](</kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md#rst-gpio-get-linehandle-config-support>), the bias configuration is best effort.

Closing the `chip_fd` has no effect on existing line events.

<a id="rst-configuration-rules"></a>

### 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**).

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_GET_LINEHANDLE_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_get_linehandle_ioctl".

<a id="rst-gpio-get-linehandle-ioctl-1"></a>
<a id="rst-gpio-get-linehandle-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-get-line-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

<a id="rst-name"></a>

## Name

GPIO_GET_LINEHANDLE_IOCTL - Request a line or lines from the kernel.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_LINEHANDLE_IOCTL"></a>

```c
GPIO_GET_LINEHANDLE_IOCTL
```

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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.

<a id="rst-configuration-rules"></a>
<a id="rst-gpio-get-linehandle-config-rules"></a>

### 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**).

<a id="rst-configuration-support"></a>
<a id="rst-gpio-get-linehandle-config-support"></a>

### 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:


<table>
<thead>
<tr>
<th>


Feature

</th>
<th>


Approach

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


Bias

</td>
<td>


best effort

</td>
</tr>
<tr>
<td>


Direction

</td>
<td>


reject

</td>
</tr>
<tr>
<td>


Drive

</td>
<td>


emulate

</td>
</tr>
</tbody>
</table>

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](/kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-ioctl.md) 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_GET_LINEINFO_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_get_lineinfo_ioctl".

<a id="rst-gpio-get-lineinfo-ioctl-1"></a>
<a id="rst-gpio-get-lineinfo-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-get-lineinfo-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-ioctl.md).

<a id="rst-name"></a>

## Name

GPIO_GET_LINEINFO_IOCTL - Get the publicly available information for a line.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_LINEINFO_IOCTL"></a>

```c
GPIO_GET_LINEINFO_IOCTL
```

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md) or [gpio-get-lineevent-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md) to access its value.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_GET_LINEINFO_UNWATCH_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_get_lineinfo_unwatch_ioctl".

<a id="rst-gpio-get-lineinfo-unwatch-ioctl-1"></a>
<a id="rst-gpio-get-lineinfo-unwatch-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_LINEINFO_UNWATCH_IOCTL"></a>

```c
GPIO_GET_LINEINFO_UNWATCH_IOCTL
```

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

<a id="rst-arguments"></a>

## Arguments

- **`chip_fd`**

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

- **`offset`**

  The offset of the line to no longer watch.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-watch-ioctl.md) (v2) and [gpio-get-lineinfo-watch-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-watch-ioctl.md) (v1).

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

First added in 5.7.

<a id="rst-return-value"></a>

## Return Value

On success 0.

On error -1 and the `errno` variable is set appropriately. Common error codes are described in [error-codes.rst](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_GET_LINEINFO_WATCH_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_get_lineinfo_watch_ioctl".

<a id="rst-gpio-get-lineinfo-watch-ioctl-1"></a>
<a id="rst-gpio-get-lineinfo-watch-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-get-lineinfo-watch-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-watch-ioctl.md).

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_GET_LINEINFO_WATCH_IOCTL"></a>

```c
GPIO_GET_LINEINFO_WATCH_IOCTL
```

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

<a id="rst-arguments"></a>

## 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

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md) or [gpio-get-lineevent-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md) to access its value, and the line event can monitor a line for events using [gpio-lineevent-data-read.rst](/kernel/userspace/userspace-api/gpio/gpio-lineevent-data-read.md).

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](/kernel/userspace/userspace-api/gpio/gpio-lineinfo-changed-read.md).

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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIOHANDLE_GET_LINE_VALUES_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpiohandle_get_line_values_ioctl".

<a id="rst-gpiohandle-get-line-values-ioctl-1"></a>
<a id="rst-gpiohandle-get-line-values-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-line-get-values-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-get-values-ioctl.md).

<a id="rst-name"></a>

## Name

GPIOHANDLE_GET_LINE_VALUES_IOCTL - Get the values of all requested lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIOHANDLE_GET_LINE_VALUES_IOCTL"></a>

```c
GPIOHANDLE_GET_LINE_VALUES_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md).

- **`values`**

  The `line_values<gpiohandle_data>` to be populated.

<a id="rst-description"></a>

## 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`.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIOHANDLE_SET_CONFIG_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpiohandle_set_config_ioctl".

<a id="rst-gpiohandle-set-config-ioctl-1"></a>
<a id="rst-gpiohandle-set-config-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-line-set-config-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-set-config-ioctl.md).

<a id="rst-name"></a>

## Name

GPIOHANDLE_SET_CONFIG_IOCTL - Update the configuration of previously requested lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIOHANDLE_SET_CONFIG_IOCTL"></a>

```c
GPIOHANDLE_SET_CONFIG_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md).

- **`config`**

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

<a id="rst-description"></a>

## 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](</kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md#rst-gpio-get-linehandle-config-rules>) and [Configuration Support](</kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md#rst-gpio-get-linehandle-config-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](/kernel/userspace/userspace-api/gpio/gpio-handle-set-line-values-ioctl.md).

First added in 5.5.

<a id="rst-return-value"></a>

## Return Value

On success 0.

On error -1 and the `errno` variable is set appropriately. Common error codes are described in [error-codes.rst](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_HANDLE_SET_LINE_VALUES_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_handle_set_line_values_ioctl".

<a id="rst-gpio-handle-set-line-values-ioctl-1"></a>
<a id="rst-gpio-handle-set-line-values-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-line-set-values-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-set-values-ioctl.md).

<a id="rst-name"></a>

## Name

GPIO_HANDLE_SET_LINE_VALUES_IOCTL - Set the values of all requested output lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_HANDLE_SET_LINE_VALUES_IOCTL"></a>

```c
GPIO_HANDLE_SET_LINE_VALUES_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-linehandle-ioctl.md).

- **`values`**

  The `line_values<gpiohandle_data>` to set.

<a id="rst-description"></a>

## 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**).

<a id="rst-return-value"></a>

## Return Value

On success 0.

On error -1 and the `errno` variable is set appropriately. Common error codes are described in [error-codes.rst](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_LINEEVENT_DATA_READ

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_lineevent_data_read".

<a id="rst-gpio-lineevent-data-read-1"></a>
<a id="rst-gpio-lineevent-data-read"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-line-event-read.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-event-read.md).

<a id="rst-name"></a>

## Name

GPIO_LINEEVENT_DATA_READ - Read edge detection events from a line event.

<a id="rst-synopsis"></a>

## Synopsis

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-get-lineevent-ioctl.md).

- **`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`.

<a id="rst-description"></a>

## 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_LINEINFO_CHANGED_READ

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_lineinfo_changed_read".

<a id="rst-gpio-lineinfo-changed-read-1"></a>
<a id="rst-gpio-lineinfo-changed-read"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

> [!WARNING]
>
> This ioctl is part of [chardev_v1.rst](/kernel/userspace/userspace-api/gpio/chardev-v1.md) and is obsoleted by [gpio-v2-lineinfo-changed-read.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-lineinfo-changed-read.md).

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-lineevent-data-read.md) to receive events when a line changes value.

A line must be watched using [gpio-get-lineinfo-watch-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-get-lineinfo-watch-ioctl.md) 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_GET_LINE_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_get_line_ioctl".

<a id="rst-gpio-v2-get-line-ioctl-1"></a>
<a id="rst-gpio-v2-get-line-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_V2_GET_LINE_IOCTL - Request a line or lines from the kernel.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_GET_LINE_IOCTL"></a>

```c
GPIO_V2_GET_LINE_IOCTL
```

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](</kernel/userspace/userspace-api/gpio/chardev.md#rst-gpio-v2-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](/kernel/userspace/userspace-api/gpio/gpio-v2-line-get-values-ioctl.md) 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.

<a id="rst-configuration-rules"></a>
<a id="rst-gpio-v2-get-line-config-rules"></a>

### 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](/kernel/userspace/userspace-api/gpio/gpio-v2-line-get-values-ioctl.md) and the edges returned by [gpio-v2-line-event-read.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-event-read.md).  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**).

<a id="rst-configuration-support"></a>
<a id="rst-gpio-v2-get-line-config-support"></a>

### 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:


<table>
<thead>
<tr>
<th>


Feature

</th>
<th>


Approach

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


Bias

</td>
<td>


best effort

</td>
</tr>
<tr>
<td>


Debounce

</td>
<td>


emulate

</td>
</tr>
<tr>
<td>


Direction

</td>
<td>


reject

</td>
</tr>
<tr>
<td>


Drive

</td>
<td>


emulate

</td>
</tr>
<tr>
<td>


Edge Detection

</td>
<td>


reject

</td>
</tr>
</tbody>
</table>

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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-ioctl.md) 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_GET_LINEINFO_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_get_lineinfo_ioctl".

<a id="rst-gpio-v2-get-lineinfo-ioctl-1"></a>
<a id="rst-gpio-v2-get-lineinfo-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_V2_GET_LINEINFO_IOCTL - Get the publicly available information for a line.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_GET_LINEINFO_IOCTL"></a>

```c
GPIO_V2_GET_LINEINFO_IOCTL
```

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md) to access its value.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_GET_LINEINFO_WATCH_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_get_lineinfo_watch_ioctl".

<a id="rst-gpio-v2-get-lineinfo-watch-ioctl-1"></a>
<a id="rst-gpio-v2-get-lineinfo-watch-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_GET_LINEINFO_WATCH_IOCTL"></a>

```c
GPIO_V2_GET_LINEINFO_WATCH_IOCTL
```

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

<a id="rst-arguments"></a>

## 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

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md) to access its value, and the line request can monitor a line for events using [gpio-v2-line-event-read.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-line-event-read.md).

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](/kernel/userspace/userspace-api/gpio/gpio-v2-lineinfo-changed-read.md).

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()`.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_LINE_EVENT_READ

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_line_event_read".

<a id="rst-gpio-v2-line-event-read-1"></a>
<a id="rst-gpio-v2-line-event-read"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

- **`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`.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-line-set-config-ioctl.md) 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_LINE_GET_VALUES_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_line_get_values_ioctl".

<a id="rst-gpio-v2-line-get-values-ioctl-1"></a>
<a id="rst-gpio-v2-line-get-values-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_V2_LINE_GET_VALUES_IOCTL - Get the values of requested lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_LINE_GET_VALUES_IOCTL"></a>

```c
GPIO_V2_LINE_GET_VALUES_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

- **`values`**

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

<a id="rst-description"></a>

## 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_LINE_SET_CONFIG_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_line_set_config_ioctl".

<a id="rst-gpio-v2-line-set-config-ioctl-1"></a>
<a id="rst-gpio-v2-line-set-config-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_V2_LINE_SET_CONFIG_IOCTL - Update the configuration of previously requested lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_LINE_SET_CONFIG_IOCTL"></a>

```c
GPIO_V2_LINE_SET_CONFIG_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

- **`config`**

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

<a id="rst-description"></a>

## 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](</kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md#rst-gpio-v2-get-line-config-rules>) and [Configuration Support](</kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md#rst-gpio-v2-get-line-config-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](/kernel/userspace/userspace-api/gpio/gpio-v2-line-set-values-ioctl.md).

<a id="rst-return-value"></a>

## Return Value

On success 0.

On error -1 and the `errno` variable is set appropriately. Common error codes are described in [error-codes.rst](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_LINE_SET_VALUES_IOCTL

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_line_set_values_ioctl".

<a id="rst-gpio-v2-line-set-values-ioctl-1"></a>
<a id="rst-gpio-v2-line-set-values-ioctl"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

GPIO_V2_LINE_SET_VALUES_IOCTL - Set the values of requested output lines.

<a id="rst-synopsis"></a>

## Synopsis





<a id="c-GPIO_V2_LINE_SET_VALUES_IOCTL"></a>

```c
GPIO_V2_LINE_SET_VALUES_IOCTL
```

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

<a id="rst-arguments"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-get-line-ioctl.md).

- **`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.

<a id="rst-description"></a>

## 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**).

<a id="rst-return-value"></a>

## Return Value

On success 0.

On error -1 and the `errno` variable is set appropriately. Common error codes are described in [error-codes.rst](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# GPIO_V2_LINEINFO_CHANGED_READ

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Duplicate implicit target name: "gpio_v2_lineinfo_changed_read".

<a id="rst-gpio-v2-lineinfo-changed-read-1"></a>
<a id="rst-gpio-v2-lineinfo-changed-read"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-name"></a>

## Name

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

<a id="rst-synopsis"></a>

## Synopsis

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

<a id="rst-arguments"></a>

## 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.

<a id="rst-description"></a>

## 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](/kernel/userspace/userspace-api/gpio/gpio-v2-line-event-read.md) to receive events when a line changes value.

A line must be watched using [gpio-v2-get-lineinfo-watch-ioctl.rst](/kernel/userspace/userspace-api/gpio/gpio-v2-get-lineinfo-watch-ioctl.md) 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.

<a id="rst-return-value"></a>

## 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](/kernel/userspace/userspace-api/gpio/error-codes.md).

---

# Obsolete GPIO Userspace APIs

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> Reference for Obsolete GPIO Userspace APIs.

<a id="rst-obsolete-gpio-userspace-apis"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [Character Device Userspace API (v1)](</kernel/userspace/userspace-api/gpio/chardev-v1.md>)
- [Sysfs Interface](</kernel/userspace/userspace-api/gpio/sysfs.md>)

---

# GPIO Sysfs Interface for Userspace

_Peios Kernel / Userspace interfaces / System and device interfaces / GPIO_

> 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 id="rst-gpio-sysfs-interface-for-userspace"></a>

> [!WARNING]
>
> This API is obsoleted by the [chardev.rst](/kernel/userspace/userspace-api/gpio/chardev.md) and the ABI documentation has been moved to Documentation/ABI/obsolete/sysfs-gpio.
>
> New developments should use the [chardev.rst](/kernel/userspace/userspace-api/gpio/chardev.md), 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.

<a id="rst-the-obsolete-sysfs-abi"></a>

## 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](/kernel/hardware/driver-api/gpio/drivers-on-gpio.md) to avoid reinventing kernel wheels in userspace.
>
> I MEAN IT. REALLY.

<a id="rst-paths-in-sysfs"></a>

## 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 &gt; 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 &gt; 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.

<a id="rst-exporting-from-kernel-code"></a>

## Exporting from Kernel code

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



```text
/* 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.
