* [PATCH] pwm: Ensure callbacks exist before calling them
@ 2025-01-23 17:27 Uwe Kleine-König
2025-01-23 19:28 ` Trevor Gamblin
0 siblings, 1 reply; 2+ messages in thread
From: Uwe Kleine-König @ 2025-01-23 17:27 UTC (permalink / raw)
To: linux-pwm; +Cc: stable
If one of the waveform functions is called for a chip that only supports
.apply(), we want that an error code is returned and not a NULL pointer
exception.
Fixes: 6c5126c6406d ("pwm: Provide new consumer API functions for waveforms")
Cc: stable@vger.kernel.org
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
Hello,
assuming nobody spots a problem I will send this patch to Linus next week for
inclusion in -rc1.
Best regards
Uwe
drivers/pwm/core.c | 13 +++++++++++--
include/linux/pwm.h | 17 +++++++++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 9c733877e98e..1a36ee3cab91 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -242,6 +242,9 @@ int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *
BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+ if (!pwmchip_supports_waveform(chip))
+ return -EOPNOTSUPP;
+
if (!pwm_wf_valid(wf))
return -EINVAL;
@@ -294,6 +297,9 @@ int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf
BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+ if (!pwmchip_supports_waveform(chip) || !ops->read_waveform)
+ return -EOPNOTSUPP;
+
guard(pwmchip)(chip);
if (!chip->operational)
@@ -320,6 +326,9 @@ static int __pwm_set_waveform(struct pwm_device *pwm,
BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
+ if (!pwmchip_supports_waveform(chip))
+ return -EOPNOTSUPP;
+
if (!pwm_wf_valid(wf))
return -EINVAL;
@@ -592,7 +601,7 @@ static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
state->usage_power == pwm->state.usage_power)
return 0;
- if (ops->write_waveform) {
+ if (pwmchip_supports_waveform(chip)) {
struct pwm_waveform wf;
char wfhw[WFHWSIZE];
@@ -746,7 +755,7 @@ int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
if (!chip->operational)
return -ENODEV;
- if (ops->read_waveform) {
+ if (pwmchip_supports_waveform(chip) && ops->read_waveform) {
char wfhw[WFHWSIZE];
struct pwm_waveform wf;
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 78827f312407..b8d78009e779 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -347,6 +347,23 @@ struct pwm_chip {
struct pwm_device pwms[] __counted_by(npwm);
};
+/**
+ * pwmchip_supports_waveform() - checks if the given chip supports waveform callbacks
+ * @chip: The pwm_chip to test
+ *
+ * Returns true iff the pwm chip support the waveform functions like
+ * pwm_set_waveform_might_sleep() and pwm_round_waveform_might_sleep()
+ */
+static inline bool pwmchip_supports_waveform(struct pwm_chip *chip)
+{
+ /*
+ * only check for .write_waveform(). If that is available,
+ * .round_waveform_tohw() and .round_waveform_fromhw() asserted to be
+ * available, too, in pwmchip_add().
+ */
+ return chip->ops->write_waveform != NULL;
+}
+
static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
{
return chip->dev.parent;
base-commit: e8c59791ebb60790c74b2c3ab520f04a8a57219a
prerequisite-patch-id: f5f481d393ddd1fd20a685c86cd4e93dd40d26c7
--
2.47.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] pwm: Ensure callbacks exist before calling them
2025-01-23 17:27 [PATCH] pwm: Ensure callbacks exist before calling them Uwe Kleine-König
@ 2025-01-23 19:28 ` Trevor Gamblin
0 siblings, 0 replies; 2+ messages in thread
From: Trevor Gamblin @ 2025-01-23 19:28 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm; +Cc: stable
On 2025-01-23 12:27, Uwe Kleine-König wrote:
> If one of the waveform functions is called for a chip that only supports
> .apply(), we want that an error code is returned and not a NULL pointer
> exception.
>
> Fixes: 6c5126c6406d ("pwm: Provide new consumer API functions for waveforms")
> Cc: stable@vger.kernel.org
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
Tested-by: Trevor Gamblin <tgamblin@baylibre.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-01-23 19:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-23 17:27 [PATCH] pwm: Ensure callbacks exist before calling them Uwe Kleine-König
2025-01-23 19:28 ` Trevor Gamblin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox