SDL: hidapi/linux: retry hid_send_feature_report() if the ioctl() fails with EPIPE (e.g. the device stalled)

From b35d813ebb3c69c4e00472987c97ca3e964b672c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
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 <slouken@libsdl.org>
---
 src/hidapi/linux/hid.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/hidapi/linux/hid.c b/src/hidapi/linux/hid.c
index e3b200eddf5a..e742b13aa6f2 100644
--- a/src/hidapi/linux/hid.c
+++ b/src/hidapi/linux/hid.c
@@ -1176,14 +1176,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;
 }