aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>2023-10-23 13:02:42 +0200
committerUwe Kleine-König <uwe@kleine-koenig.org>2023-10-26 07:32:10 +0200
commit5dc4fe252ffa93cc4d4ed5d6288e5d3d53ce086b (patch)
tree0365d6a2ceb47689ebfdbd74d318021298e360e3
parent67f0b9f2a2aa9bb4d54bb2e7e42cec69f30d7951 (diff)
downloadlibpwm-5dc4fe252ffa93cc4d4ed5d6288e5d3d53ce086b.tar.gz
Fix sysfs backend for pwmchips with npwm > 1
-rw-r--r--sysfs.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/sysfs.c b/sysfs.c
index eca92ae..47b1690 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -6,6 +6,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@@ -53,6 +54,10 @@ static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
struct pwm_chip_sysfs *chip_sysfs = container_of(chip, struct pwm_chip_sysfs, chip);
struct pwm_sysfs *pwm_sysfs;
struct pwm *pwm;
+#if UINT_MAX <= 4294967295U
+ /* force a compiler error if buffer gets too small */
+ char pwmX[14];
+#endif
int fd;
if (chip_sysfs->pwms[offset])
@@ -65,7 +70,9 @@ static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
pwm->chip = chip;
- pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, "pwm0",
+ sprintf(pwmX, "pwm%u", offset);
+
+ pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
O_PATH | O_CLOEXEC);
if (pwm_sysfs->dirfd < 0 && errno == ENOENT) {
int ret;
@@ -89,7 +96,7 @@ static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
return NULL;
}
- pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, "pwm0",
+ pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
O_PATH | O_CLOEXEC);
}