+
+ return result;
+}
+
+/* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
+static int usb_bulk_with_retries(
+ int (*f)(usb_dev_handle *, int, char *, int, int),
+ usb_dev_handle *dev, int ep,
+ char *bytes, int size, int timeout)
+{
+ int tries = 3, count = 0;
+
+ while (tries && (count < size))
+ {
+ int result = f(dev, ep, bytes + count, size - count, timeout);
+ if (result > 0)
+ count += result;
+ else if ((-ETIMEDOUT != result) || !--tries)
+ return result;
+ }
+ return count;
+}
+
+static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
+ char *buff, int size, int timeout)
+{
+ /* usb_bulk_write() takes const char *buff */
+ return usb_bulk_write(dev, ep, buff, size, timeout);
+}
+
+static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
+ char *bytes, int size, int timeout)
+{
+ return usb_bulk_with_retries(&wrap_usb_bulk_write,
+ dev, ep, bytes, size, timeout);
+}
+
+static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
+ char *bytes, int size, int timeout)
+{
+ return usb_bulk_with_retries(&usb_bulk_read,
+ dev, ep, bytes, size, timeout);