Commit fcadec31105cee7e381a8a41ecab3f48e605cd8e

Authored by Phil Elwell
Committed by Gordon Hollingworth
1 parent 2e03233e

Smallpacket (#26)

* Add support for larger files.

Based on original patch by ZoltanSzenczi:
https://github.com/raspberrypi/tools/pull/20

* Fix typo in max transfer size.

Original patch had a magic number of "LIBUSB_MAX_TRANSFER 507904"

Assume that 496K ia a typo for 4096K, although 8M also works for me.

* Put back out_ep and long timeout.

Master uses out_ep variable instead of hard coding 0x01.

Master also sets a much longer time out for the transfer. Transfering
4MB takes almost 5 seconds, so the original patch was dangerously close
to timing out here. Master uses 100 second timeout, which is plenty.

* Use 16KB instead of 4MB.

The usbfs buffer memory is apparently shared across all usbfs devs.
So we should try not to use a huge amount of it. Using a 16KB buffer
here has no observable impact on performance as the trasnfer will be
split up in to packets at least that small anyway.

This also removes the abort counter. That doesn't seem to do anything
except make the transfer fail if it has to be split into more than
20 parts. There is no "retry" code to break out of; the loop always
aborts after a single error.

* Reduce the timeout for 16KB blocks.

Since the block size is reduced we no longer need to set a huge
time out of 100 seconds. 5 seconds should be plenty to transfer
16KB. It should in fact take well under 1 second.
Showing 1 changed file with 13 additions and 4 deletions
... ... @@ -189,9 +189,12 @@ int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device)
189 189 return ret;
190 190 }
191 191  
  192 +#define LIBUSB_MAX_TRANSFER (16 * 1024)
  193 +
192 194 int ep_write(void *buf, int len, libusb_device_handle * usb_device)
193 195 {
194 196 int a_len = 0;
  197 + int sending, sent;
195 198 int ret =
196 199 libusb_control_transfer(usb_device, LIBUSB_REQUEST_TYPE_VENDOR, 0,
197 200 len & 0xffff, len >> 16, NULL, 0, 1000);
... ... @@ -202,12 +205,18 @@ int ep_write(void *buf, int len, libusb_device_handle * usb_device)
202 205 return ret;
203 206 }
204 207  
205   - if(len > 0)
  208 + while(len > 0)
206 209 {
207   - ret = libusb_bulk_transfer(usb_device, out_ep, buf, len, &a_len, 100000);
208   - if(verbose)
209   - printf("libusb_bulk_transfer returned %d\n", ret);
  210 + sending = len < LIBUSB_MAX_TRANSFER ? len : LIBUSB_MAX_TRANSFER;
  211 + ret = libusb_bulk_transfer(usb_device, out_ep, buf, sending, &sent, 5000);
  212 + if (ret)
  213 + break;
  214 + a_len += sent;
  215 + buf += sent;
  216 + len -= sent;
210 217 }
  218 + if(verbose)
  219 + printf("libusb_bulk_transfer sent %d bytes; returned %d\n", a_len, ret);
211 220  
212 221 return a_len;
213 222 }
... ...