From 7c86f7f24f254da6d139ead21123fb7b2e4565d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Tue, 2 Jul 2024 06:55:35 -0400 Subject: [PATCH] [libsimpleio] Update for RPi5 support --- 3rdparty/libsimpleio/libsimpleio/libadc.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libdac.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libevent.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libgpio.c | 48 ++++++++++++++----- 3rdparty/libsimpleio/libsimpleio/libhidraw.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libi2c.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libipv4.c | 2 +- 3rdparty/libsimpleio/libsimpleio/liblinux.c | 45 +++++++++++++++-- 3rdparty/libsimpleio/libsimpleio/liblinux.h | 6 ++- 3rdparty/libsimpleio/libsimpleio/libpwm.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libserial.c | 2 +- 3rdparty/libsimpleio/libsimpleio/libspi.c | 2 +- .../libsimpleio/libsimpleio/libwatchdog.c | 3 +- 13 files changed, 93 insertions(+), 27 deletions(-) diff --git a/3rdparty/libsimpleio/libsimpleio/libadc.c b/3rdparty/libsimpleio/libsimpleio/libadc.c index 4c4eaf3a18..5383e58df4 100644 --- a/3rdparty/libsimpleio/libsimpleio/libadc.c +++ b/3rdparty/libsimpleio/libsimpleio/libadc.c @@ -30,7 +30,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libadc.h" #define NAME_FILE "/sys/bus/iio/devices/iio:device%d/name" diff --git a/3rdparty/libsimpleio/libsimpleio/libdac.c b/3rdparty/libsimpleio/libsimpleio/libdac.c index f664dcc7df..112d7cad22 100644 --- a/3rdparty/libsimpleio/libsimpleio/libdac.c +++ b/3rdparty/libsimpleio/libsimpleio/libdac.c @@ -30,7 +30,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libadc.h" #define NAME_FILE "/sys/bus/iio/devices/iio:device%d/name" diff --git a/3rdparty/libsimpleio/libsimpleio/libevent.c b/3rdparty/libsimpleio/libsimpleio/libevent.c index 020f667681..33844cdacc 100644 --- a/3rdparty/libsimpleio/libsimpleio/libevent.c +++ b/3rdparty/libsimpleio/libsimpleio/libevent.c @@ -26,7 +26,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libevent.h" void EVENT_open(int32_t *epfd, int32_t *error) diff --git a/3rdparty/libsimpleio/libsimpleio/libgpio.c b/3rdparty/libsimpleio/libsimpleio/libgpio.c index f379d43645..8713a21f70 100644 --- a/3rdparty/libsimpleio/libsimpleio/libgpio.c +++ b/3rdparty/libsimpleio/libsimpleio/libgpio.c @@ -1,6 +1,6 @@ /* GPIO services for Linux */ -// Copyright (C)2016-2023, Philip Munts dba Munts Technologies. +// Copyright (C)2016-2024, Philip Munts dba Munts Technologies. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: @@ -32,8 +32,11 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libgpio.h" +#include "liblinux.h" + +#define RASPBERRYPI_GPIOCHIP "/dev/gpiochip-rpi" void GPIO_chip_info(int32_t chip, char *name, int32_t namesize, char *label, int32_t labelsize, int32_t *lines, int32_t *error) @@ -86,10 +89,17 @@ void GPIO_chip_info(int32_t chip, char *name, int32_t namesize, // Open the GPIO controller device - char nodename[32]; - snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + int chipfd; - int chipfd = open(nodename, O_RDWR); + if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) && + (strstr(LINUX_model_name(), "Raspberry Pi") != NULL)) + chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR); + else + { + char nodename[32]; + snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + chipfd = open(nodename, O_RDWR); + } if (chipfd < 0) { @@ -180,10 +190,17 @@ void GPIO_line_info(int32_t chip, int32_t line, int32_t *flags, char *name, // Open the GPIO controller device - char nodename[32]; - snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + int chipfd; - int chipfd = open(nodename, O_RDWR); + if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) && + (strstr(LINUX_model_name(), "Raspberry Pi") != NULL)) + chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR); + else + { + char nodename[32]; + snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + chipfd = open(nodename, O_RDWR); + } if (chipfd < 0) { @@ -322,12 +339,19 @@ void GPIO_line_open(int32_t chip, int32_t line, int32_t flags, int32_t events, return; } - // Open GPIO controller device + // Open the GPIO controller device - char nodename[32]; - snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + int chipfd; - int chipfd = open(nodename, O_RDWR); + if (!access(RASPBERRYPI_GPIOCHIP, F_OK) && (chip == 0) && + (strstr(LINUX_model_name(), "Raspberry Pi") != NULL)) + chipfd = open(RASPBERRYPI_GPIOCHIP, O_RDWR); + else + { + char nodename[32]; + snprintf(nodename, sizeof(nodename), "/dev/gpiochip%d", chip); + chipfd = open(nodename, O_RDWR); + } if (chipfd < 0) { diff --git a/3rdparty/libsimpleio/libsimpleio/libhidraw.c b/3rdparty/libsimpleio/libsimpleio/libhidraw.c index dd7434114f..c33ab9d777 100644 --- a/3rdparty/libsimpleio/libsimpleio/libhidraw.c +++ b/3rdparty/libsimpleio/libsimpleio/libhidraw.c @@ -32,7 +32,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libhidraw.h" // Compatibility shims diff --git a/3rdparty/libsimpleio/libsimpleio/libi2c.c b/3rdparty/libsimpleio/libsimpleio/libi2c.c index a0c0660c16..c407d05f63 100644 --- a/3rdparty/libsimpleio/libsimpleio/libi2c.c +++ b/3rdparty/libsimpleio/libsimpleio/libi2c.c @@ -33,7 +33,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libi2c.h" // Perform an I2C transaction diff --git a/3rdparty/libsimpleio/libsimpleio/libipv4.c b/3rdparty/libsimpleio/libsimpleio/libipv4.c index faf90cec07..5060f644d8 100644 --- a/3rdparty/libsimpleio/libsimpleio/libipv4.c +++ b/3rdparty/libsimpleio/libsimpleio/libipv4.c @@ -31,7 +31,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libipv4.h" // Resolve host name to IPV4 address diff --git a/3rdparty/libsimpleio/libsimpleio/liblinux.c b/3rdparty/libsimpleio/libsimpleio/liblinux.c index 47a8cdcfc9..75d1e33e7f 100644 --- a/3rdparty/libsimpleio/libsimpleio/liblinux.c +++ b/3rdparty/libsimpleio/libsimpleio/liblinux.c @@ -1,7 +1,7 @@ /* Linux syscall wrappers. These are primarily for the benefit of other */ /* programming languages, such as Ada, C#, Free Pascal, Go, etc. */ -// Copyright (C)2016-2023, Philip Munts dba Munts Technologies. +// Copyright (C)2016-2024, Philip Munts dba Munts Technologies. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: @@ -31,7 +31,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "liblinux.h" // Detach process from the controlling terminal and run it in the background @@ -749,9 +749,46 @@ void *LINUX_indexpp(void **p, int32_t i) return p[i]; } -// Function aliases +// Return the device tree model name from /proc/device-tree/model + +#define MODELPATH "/proc/device-tree/model" + +const char * const LINUX_model_name(void) +{ + int fd; + ssize_t len; + static char failure[4096]; + static char success[4096]; + + memset(failure, 0, sizeof(failure)); + memset(success, 0, sizeof(success)); + + if (access(MODELPATH, R_OK)) + return failure; + + fd = open(MODELPATH, O_RDONLY); + + if (fd < 0) + { + ERRORMSG("open() failed", errno, __LINE__ - 4); + return failure; + } + + len = read(fd, success, sizeof(success) - 1); + + if (len < 0) + { + ERRORMSG("read() failed", errno, __LINE__ - 4); + close(fd); + return failure; + } + + close(fd); -#define ALIAS(orig) __attribute__((weak, alias(orig))) + return success; +} + +// Function aliases void ADC_close(int32_t fd, int32_t *error) ALIAS("LINUX_close"); void DAC_close(int32_t fd, int32_t *error) ALIAS("LINUX_close"); diff --git a/3rdparty/libsimpleio/libsimpleio/liblinux.h b/3rdparty/libsimpleio/libsimpleio/liblinux.h index 2d30df3551..05af60ab9a 100644 --- a/3rdparty/libsimpleio/libsimpleio/liblinux.h +++ b/3rdparty/libsimpleio/libsimpleio/liblinux.h @@ -1,7 +1,7 @@ /* Linux syscall wrappers. These are primarily for the benefit of other */ /* programming languages, such as Ada, C#, Free Pascal, Go, etc. */ -// Copyright (C)2016-2023, Philip Munts dba Munts Technologies. +// Copyright (C)2016-2024, Philip Munts dba Munts Technologies. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: @@ -127,6 +127,10 @@ extern void LINUX_pclose(void *stream, int32_t *error); extern void *LINUX_indexpp(void **p, int32_t i); +// Return the device tree model name from /proc/device-tree/model + +extern const char * const LINUX_model_name(void); + _END_STD_C #endif diff --git a/3rdparty/libsimpleio/libsimpleio/libpwm.c b/3rdparty/libsimpleio/libsimpleio/libpwm.c index ab4bd704d8..0c2c218d5b 100644 --- a/3rdparty/libsimpleio/libsimpleio/libpwm.c +++ b/3rdparty/libsimpleio/libsimpleio/libpwm.c @@ -29,7 +29,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libpwm.h" // Device nodes diff --git a/3rdparty/libsimpleio/libsimpleio/libserial.c b/3rdparty/libsimpleio/libsimpleio/libserial.c index 36f00f8642..d7abe51043 100644 --- a/3rdparty/libsimpleio/libsimpleio/libserial.c +++ b/3rdparty/libsimpleio/libsimpleio/libserial.c @@ -28,7 +28,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libserial.h" void SERIAL_open(const char *name, int32_t baudrate, int32_t parity, diff --git a/3rdparty/libsimpleio/libsimpleio/libspi.c b/3rdparty/libsimpleio/libsimpleio/libspi.c index 9672b824a6..1be426d387 100644 --- a/3rdparty/libsimpleio/libsimpleio/libspi.c +++ b/3rdparty/libsimpleio/libsimpleio/libspi.c @@ -29,7 +29,7 @@ #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libgpio.h" #include "libspi.h" diff --git a/3rdparty/libsimpleio/libsimpleio/libwatchdog.c b/3rdparty/libsimpleio/libsimpleio/libwatchdog.c index add99a1957..ada2014413 100644 --- a/3rdparty/libsimpleio/libsimpleio/libwatchdog.c +++ b/3rdparty/libsimpleio/libsimpleio/libwatchdog.c @@ -22,10 +22,11 @@ #include #include +#include #include #include -#include "errmsg.inc" +#include "macros.inc" #include "libwatchdog.h" void WATCHDOG_get_timeout(int32_t fd, int32_t *timeout, int32_t *error)