aboutsummaryrefslogtreecommitdiffstats
path: root/halfkay.c
diff options
context:
space:
mode:
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)