From 6691e39b7762817b83c8460dbb2b4279c65255a9 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 24 Nov 2016 22:08:31 +0000 Subject: 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. --- halfkay.c | 18 +++++++++++------- 1 file 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) -- cgit v1.2.3-54-g00ecf