From 553b6cae78a38e3c10f4aba3f703b5950294f3eb Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 25 May 2023 17:11:18 -0700 Subject: [PATCH] hidapi/linux: retry hid_send_feature_report() if the ioctl() fails with EPIPE (e.g. the device stalled) Signed-off-by: Sam Lantinga --- linux/hid.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/linux/hid.c b/linux/hid.c index a499f04c2..1d1c7ea50 100644 --- a/linux/hid.c +++ b/linux/hid.c @@ -1110,14 +1110,23 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock) int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length) { + static const int MAX_RETRIES = 50; + int retry; int res; register_device_error(dev, NULL); - res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data); - if (res < 0) - register_device_error_format(dev, "ioctl (SFEATURE): %s", strerror(errno)); + for (retry = 0; retry < MAX_RETRIES; ++retry) { + res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data); + if (res < 0 && errno == EPIPE) { + /* Try again... */ + continue; + } + if (res < 0) + register_device_error_format(dev, "ioctl (SFEATURE): %s", strerror(errno)); + break; + } return res; }