aboutsummaryrefslogtreecommitdiffstats
path: root/halfkay.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2016-11-24 22:08:31 +0000
committerTomasz Kramkowski <tk@the-tk.com>2016-11-24 22:08:31 +0000
commit6691e39b7762817b83c8460dbb2b4279c65255a9 (patch)
tree41e47d0bdb6b290a776d38ec22e5ef6d3a998891 /halfkay.c
parent8827e7bd1b8fb835435e4466db77f61b0a066880 (diff)
downloadhktool-6691e39b7762817b83c8460dbb2b4279c65255a9.tar.gz
hktool-6691e39b7762817b83c8460dbb2b4279c65255a9.tar.xz
hktool-6691e39b7762817b83c8460dbb2b4279c65255a9.zip
halfkay.c: usbsendmd: fix fast transmit EPIPE
Fixed an issue where transmitting too quickly would cause a LIBUSB_ERROR_PIPE after the third transmission. For the future the delay might want to back off and the initial delay could be shortened.
Diffstat (limited to 'halfkay.c')
-rw-r--r--halfkay.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/halfkay.c b/halfkay.c
index 755a84e..cfb10ce 100644
--- a/halfkay.c
+++ b/halfkay.c
@@ -38,6 +38,10 @@ enum {
| LIBUSB_RECIPIENT_INTERFACE),
HK_USB_REQ = LIBUSB_REQUEST_SET_CONFIGURATION,
HK_USB_VAL = 0x200,
+ TIMEOUT = 2000,
+ TIMEOUT_FIRST = 3000,
+ TRANS_DELAY = 10000,
+ TRANS_ATTEMPTS = 10,
};
libusb_device_handle *hk_usb_hnd;
@@ -74,17 +78,17 @@ static void usbclose(void)
static void usbsendcmd(void *data, size_t size, bool firstxfer)
{
- unsigned int timeout = firstxfer ? 3000 : 2000;
- int rc;
+ unsigned int timeout = firstxfer ? TIMEOUT_FIRST : TIMEOUT;
+ int rc, attempt = 0;
- rc = libusb_control_transfer(hk_usb_hnd, HK_USB_REQTYP, HK_USB_REQ,
- HK_USB_VAL, 0, data, size, timeout);
+ do {
+ rc = libusb_control_transfer(hk_usb_hnd, HK_USB_REQTYP, HK_USB_REQ,
+ HK_USB_VAL, 0, data, size, timeout);
+ usleep(TRANS_DELAY);
+ } while (rc == LIBUSB_ERROR_PIPE && ++attempt < TRANS_ATTEMPTS);
if (rc < 0)
libusb_err("transfer", rc);
-
- /* TODO: Replace this with the correct way to handle a halt... ffs idiot */
- usleep(10000); /* HalfKay says no if you write too fast */
}
static void fmtcmd(void *_dest, const struct flashparams *fp, size_t addr)