[PATCH] HID: cp2112: fix setter callbacks return value

Sébastien Szymanski posted 1 patch 4 weeks ago
drivers/hid/hid-cp2112.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
[PATCH] HID: cp2112: fix setter callbacks return value
Posted by Sébastien Szymanski 4 weeks ago
Since commit 6485543488a6 ("HID: cp2112: use new line value setter
callbacks"), setting a GPIO value always fails with error -EBADE.

That's because the returned value by the setter callbacks is the
returned value by the hid_hw_raw_request() function which is the number of
bytes sent on success or a negative value on error. The function
gpiochip_set() returns -EBADE if the setter callbacks return a value >
0.

Fix this by making the setter callbacks return 0 on success or a negative
value on error.

While at it, use the returned value by cp2112_gpio_set_unlocked() in the
direction_output callback.

Fixes: 6485543488a6 ("HID: cp2112: use new line value setter callbacks")
Signed-off-by: Sébastien Szymanski <sebastien.szymanski@armadeus.com>
---
 drivers/hid/hid-cp2112.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
index 482f62a78c4155a50cceea3c8894ee5959e9aaed..5a95ea3bec9805453712b7b9e3d3fefec2351822 100644
--- a/drivers/hid/hid-cp2112.c
+++ b/drivers/hid/hid-cp2112.c
@@ -229,10 +229,12 @@ static int cp2112_gpio_set_unlocked(struct cp2112_device *dev,
 	ret = hid_hw_raw_request(hdev, CP2112_GPIO_SET, buf,
 				 CP2112_GPIO_SET_LENGTH, HID_FEATURE_REPORT,
 				 HID_REQ_SET_REPORT);
-	if (ret < 0)
+	if (ret != CP2112_GPIO_SET_LENGTH) {
 		hid_err(hdev, "error setting GPIO values: %d\n", ret);
+		return ret < 0 ? ret : -EIO;
+	}
 
-	return ret;
+	return 0;
 }
 
 static int cp2112_gpio_set(struct gpio_chip *chip, unsigned int offset,
@@ -309,9 +311,7 @@ static int cp2112_gpio_direction_output(struct gpio_chip *chip,
 	 * Set gpio value when output direction is already set,
 	 * as specified in AN495, Rev. 0.2, cpt. 4.4
 	 */
-	cp2112_gpio_set_unlocked(dev, offset, value);
-
-	return 0;
+	return cp2112_gpio_set_unlocked(dev, offset, value);
 }
 
 static int cp2112_hid_get(struct hid_device *hdev, unsigned char report_number,

---
base-commit: b320789d6883cc00ac78ce83bccbfe7ed58afcf0
change-id: 20250902-hid-cp2112-fix-set-value-1898aa5e041f

Best regards,
-- 
Sébastien Szymanski <sebastien.szymanski@armadeus.com>

Re: [PATCH] HID: cp2112: fix setter callbacks return value
Posted by Jiri Kosina 2 weeks, 6 days ago
On Thu, 4 Sep 2025, Sébastien Szymanski wrote:

> Since commit 6485543488a6 ("HID: cp2112: use new line value setter
> callbacks"), setting a GPIO value always fails with error -EBADE.
> 
> That's because the returned value by the setter callbacks is the
> returned value by the hid_hw_raw_request() function which is the number of
> bytes sent on success or a negative value on error. The function
> gpiochip_set() returns -EBADE if the setter callbacks return a value >
> 0.
> 
> Fix this by making the setter callbacks return 0 on success or a negative
> value on error.
> 
> While at it, use the returned value by cp2112_gpio_set_unlocked() in the
> direction_output callback.
> 
> Fixes: 6485543488a6 ("HID: cp2112: use new line value setter callbacks")
> Signed-off-by: Sébastien Szymanski <sebastien.szymanski@armadeus.com>

Applied, thank you!

-- 
Jiri Kosina
SUSE Labs
Re: [PATCH] HID: cp2112: fix setter callbacks return value
Posted by Bartosz Golaszewski 4 weeks ago
On Thu, 4 Sep 2025 18:42:07 +0200, "Sébastien Szymanski"
<sebastien.szymanski@armadeus.com> said:
> Since commit 6485543488a6 ("HID: cp2112: use new line value setter
> callbacks"), setting a GPIO value always fails with error -EBADE.
>
> That's because the returned value by the setter callbacks is the
> returned value by the hid_hw_raw_request() function which is the number of
> bytes sent on success or a negative value on error. The function
> gpiochip_set() returns -EBADE if the setter callbacks return a value >
> 0.
>
> Fix this by making the setter callbacks return 0 on success or a negative
> value on error.
>
> While at it, use the returned value by cp2112_gpio_set_unlocked() in the
> direction_output callback.
>
> Fixes: 6485543488a6 ("HID: cp2112: use new line value setter callbacks")
> Signed-off-by: Sébastien Szymanski <sebastien.szymanski@armadeus.com>
> ---

Eek! With the number of conversions it was bound to happen to some drivers
since I can't really functionally test most of them. :(

Thanks for spotting it and fixing it!

Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>