From nobody Wed Sep 3 18:08:34 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cH9b65Pqrz66tB4; Wed, 03 Sep 2025 18:08:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cH9b64hyjz3LZQ; Wed, 03 Sep 2025 18:08:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756922914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=enRvuANU9YsX4TQcIPougAEAJf8VXHI5M/x4JcDB1AE=; b=IdubCKVsmNFNtfbhb4GvFB1wSrXUheypudSNZcy6FwZkhbYk1MjMhmPswDPx5t6t3BqXTA HBawveohrAhGCMkFL+kdczWhpjfFTJuHBc3+a1QW3JYPQ7st6GRIdWTTQoVnEIBH4mBXXS Z8ZdUDG1CPFqDF4dQmlAGfkQZW4k9jbD5orrduslld+u0V6PiHMtJtuRZL7Ku9Vg83F7Df QgKXN1lD4CTS+boxyzadeA5VPEks4FvxT9VuRWdUtso375TX9hHMuiRJIZE9JN9B8M6XrI f5CUebMHGkgx+Ll+YaXX/S46sqH5r8mawkk4VObIZDI6POaM/0b8pHBfLoxYRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756922914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=enRvuANU9YsX4TQcIPougAEAJf8VXHI5M/x4JcDB1AE=; b=pzBg4OmU428EfShlXljHFTdjUG40s7rasSsa2Yl4ufF0vz5j/uPyyCps00Y3mR+BW4aThN GUs2FsdbymADy2gHmNWNzqof957Ag764u7b7Vf2ftT+eDrKNyX15h2iVX6l8kwGShRVhSs 19z1mXCYehPco0c1ZU+ChyRlPdunW36szfV6yU/he6gjr7uLwR1kHjhYvcRREacZPDjpfD v4q9twCkTPxjLmepmYZtsteDKcgsuGkBEZkVrxUtceIwhAjgvuxSQssulfKkGtS3kBaMCU VzQ8FCHzAT/5waH7Hgo1Ol0Ign4O1bwwZv15sgmKniKnVdTgiHY8p+AjXp960w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1756922914; a=rsa-sha256; cv=none; b=GLMFW3sWJMWhKwSvgSKyXnLPLL1Ie4tzerpPD2cDgbTSh3whb/+3/kRl5CTZp6PZVlPQhl lFXD+kqnf3fZwA9KiDNLMtg5RocHOYQoot5vgxWa9Qk1BQ894K7XMhc3XhVWnM4Zirc4Sg QUtMKH+ZULy2jaNFkMrtcKxKyyanMQ/qDt45PeRa3oy55qdsRommw0Ps/1PKuA14A5dUr6 IoCeq/pwI1pkAopsdBegXjXSEx6FJ+jWltKUnATHkEvOZeeaAa4wk5Lxywf1yxUZ2Snc2G FpWXUzoQyctKoFnFYcr53IN0R6P15Wspr1lOUhWOmPz7KGYb1sJOfUbuk4SyVg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cH9b63stWzCJJ; Wed, 03 Sep 2025 18:08:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 583I8Y9u006235; Wed, 3 Sep 2025 18:08:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 583I8YlJ006232; Wed, 3 Sep 2025 18:08:34 GMT (envelope-from git) Date: Wed, 3 Sep 2025 18:08:34 GMT Message-Id: <202509031808.583I8YlJ006232@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ahmad Khalifa Subject: git: ccc6cf3b67ed - main - libgpio: Add and document gpio interrupt utility functions List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: vexeduxr X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ccc6cf3b67edb9d50f6edb74c9b0e2c23f8b24d3 Auto-Submitted: auto-generated The branch main has been updated by vexeduxr: URL: https://cgit.FreeBSD.org/src/commit/?id=ccc6cf3b67edb9d50f6edb74c9b0e2c23f8b24d3 commit ccc6cf3b67edb9d50f6edb74c9b0e2c23f8b24d3 Author: Evgenii Ivanov AuthorDate: 2025-09-03 12:13:59 +0000 Commit: Ahmad Khalifa CommitDate: 2025-09-03 18:01:05 +0000 libgpio: Add and document gpio interrupt utility functions Add utility functions for configuring gpio interrupts and document file operations. Reviewed by: vexeduxr, ziaee Differential Revision: https://reviews.freebsd.org/D52102 --- lib/libgpio/Makefile | 4 ++- lib/libgpio/gpio.3 | 68 +++++++++++++++++++++++++++++++++++++++++++++++++-- lib/libgpio/gpio.c | 20 +++++++++++++++ lib/libgpio/libgpio.h | 13 ++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/lib/libgpio/Makefile b/lib/libgpio/Makefile index 42f822e97ba0..271595339792 100644 --- a/lib/libgpio/Makefile +++ b/lib/libgpio/Makefile @@ -27,6 +27,8 @@ MLINKS= gpio.3 gpio_open.3 \ gpio.3 gpio_pin_pulldown.3 \ gpio.3 gpio_pin_invin.3 \ gpio.3 gpio_pin_invout.3 \ - gpio.3 gpio_pin_pulsate.3 + gpio.3 gpio_pin_pulsate.3 \ + gpio.3 gpio_configure_events.3 \ + gpio.3 gpio_fileno.3 .include diff --git a/lib/libgpio/gpio.3 b/lib/libgpio/gpio.3 index ed2860776c3c..cb413b838bd0 100644 --- a/lib/libgpio/gpio.3 +++ b/lib/libgpio/gpio.3 @@ -23,7 +23,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd July 1, 2015 +.Dd September 3, 2025 .Dt GPIO 3 .Os .Sh NAME @@ -79,6 +79,10 @@ .Fn gpio_pin_invout "gpio_handle_t handle" "gpio_pin_t pin" .Ft int .Fn gpio_pin_pulsate "gpio_handle_t handle" "gpio_pin_t pin" +.Ft int +.Fn gpio_configure_events "gpio_handle_t handle" "uint32_t report_type" "uint32_t fifo_size" +.Ft int +.Fn gpio_fileno "gpio_handle_t handle" .Sh DESCRIPTION The .Nm libgpio @@ -125,7 +129,7 @@ The pin number should also be passed in through the variable. All other structure members will be ignored by this function. The list of flags can be found in -.Pa /usr/include/sys/gpio.h . +.In sys/gpio.h . .Pp The get or set the state of a GPIO pin, the functions .Fn gpio_pin_get @@ -156,6 +160,66 @@ and .Fn gpio_pin_pulsate are wrappers around .Fn gpio_pin_set_flags . +.Pp +The function +.Fn gpio_configure_events +configures the interrupt report type and FIFO size for buffered +gpio interrupts. +The report type is specified by one of the following values: +.Bl -tag -width indent +.It Dv GPIO_EVENT_REPORT_DETAIL +Events are reported using +.Ft struct gpio_event_detail . +.It Dv GPIO_EVENT_REPORT_SUMMARY +Events are reported using +.Ft struct gpio_event_summary . +.El +.Pp +By default, the report type is +.Dv GPIO_EVENT_REPORT_DETAIL , +with a default FIFO size of 2 * number of pins belonging to the +.Ft gpio_device_t +instance. +The FIFO argument is only meaningful when +.Fa report_type +is +.Dv GPIO_EVENT_REPORT_DETAIL . +The structures associated with each report type are defined in +.In sys/gpio.h . +This setting is tracked on a per device instance basis. +The FIFO size cannot be reduced below the default value, +nor can it be decreased after it has been increased. +If any pin on the device has already been configured for interrupts, +.Fn gpio_configure_events +fails and returns -1. +On success 0 is returned. +.Pp +The function +.Fn gpio_fileno +returns the file descriptor associated with the +.Ft gpio_handle_t +instance. +.Pp +File operations have the following semantics: +.Bl -tag -width "read (2)" +.It Xr read 2 +Read one or more gpio interrupts that have occured +since the last successful +.Xr read 2 . +The results are placed into the output buffer +of the type previously established via +.Fn gpio_configure_events . +If there are no pending interrupts, +.Xr read 2 +blocks until an interrupt occurs, unless +.Dv O_NONBLOCK +is set. +.It Xr poll 2 +When receiving notification via +.Xr poll 2 +or similar interfaces, the file descriptor becomes readable when +one or more gpio interrupts are pending. +.El .Sh EXAMPLES The following example shows how to configure pin 16 as output and then drive it high: diff --git a/lib/libgpio/gpio.c b/lib/libgpio/gpio.c index e37ac1cdf8e8..c789bb34cacd 100644 --- a/lib/libgpio/gpio.c +++ b/lib/libgpio/gpio.c @@ -274,3 +274,23 @@ gpio_pin_pulsate(gpio_handle_t handle, gpio_pin_t pin) { return (gpio_pin_set_flag(handle, pin, GPIO_PIN_PULSATE)); } + +int +gpio_configure_events(gpio_handle_t handle, uint32_t report_type, + uint32_t fifo_size) +{ + struct gpio_event_config gpevent_config; + + gpevent_config.gp_report_type = report_type; + gpevent_config.gp_fifo_size = fifo_size; + if (ioctl(handle, GPIOCONFIGEVENTS, &gpevent_config) < 0) + return (-1); + + return (0); +} + +int +gpio_fileno(gpio_handle_t handle) +{ + return (handle); +} diff --git a/lib/libgpio/libgpio.h b/lib/libgpio/libgpio.h index 35651ecd8cca..abffc7b1b6ab 100644 --- a/lib/libgpio/libgpio.h +++ b/lib/libgpio/libgpio.h @@ -102,6 +102,19 @@ int gpio_pin_pulldown(gpio_handle_t, gpio_pin_t); int gpio_pin_invin(gpio_handle_t, gpio_pin_t); int gpio_pin_invout(gpio_handle_t, gpio_pin_t); int gpio_pin_pulsate(gpio_handle_t, gpio_pin_t); +/* + * GPIO event reporting configuration + * + * Set the event reporting type, the default being GPIO_EVENT_REPORT_DETAIL, + * and fifo size of 2 * number of pins belonging to the gpioc device instance. + * FIFO size can only be changed when report_type is GPIO_EVENT_REPORT_DETAIL. + */ +int gpio_configure_events(gpio_handle_t, uint32_t, uint32_t); +/* + * Retrieve the file descriptor associated with gpio_handle_t, which can + * be used for gpio interrupts. + */ +int gpio_fileno(gpio_handle_t); __END_DECLS