aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/userspace-api/gpio/gpio-v2-line-event-read.rst
blob: 6513c23fb7cadaff4223583e7b09253be3e553d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
.. SPDX-License-Identifier: GPL-2.0

.. _GPIO_V2_LINE_EVENT_READ:

***********************
GPIO_V2_LINE_EVENT_READ
***********************

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
    :c:type:`request.fd<gpio_v2_line_request>` by gpio-v2-get-line-ioctl.rst.

``buf``
    The buffer to contain the :c:type:`events<gpio_v2_line_event>`.

``count``
    The number of bytes available in ``buf``, which must be at
    least the size of a :c:type:`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.

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
:c:type:`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
:c:type:`gpio_v2_line_event` event.

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