68 static void LIBUSB_CALL
69 ftdi_readstream_cb(
struct libusb_transfer *transfer)
75 if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
78 uint8_t *ptr = transfer->buffer;
79 int length = transfer->actual_length;
80 int numPackets = (length + packet_size - 1) / packet_size;
83 for (i = 0; i < numPackets; i++)
86 int packetLen = length;
88 if (packetLen > packet_size)
89 packetLen = packet_size;
91 payloadLen = packetLen - 2;
94 res = state->
callback(ptr + 2, payloadLen,
102 free(transfer->buffer);
103 libusb_free_transfer(transfer);
107 transfer->status = -1;
108 state->
result = libusb_submit_transfer(transfer);
113 fprintf(stderr,
"unknown status %d\n",transfer->status);
114 state->
result = LIBUSB_ERROR_IO;
125 TimevalDiff(
const struct timeval *a,
const struct timeval *b)
127 return (a->tv_sec - b->tv_sec) + 1e-6 * (a->tv_usec - b->tv_usec);
153 int packetsPerTransfer,
int numTransfers)
155 struct libusb_transfer **transfers;
164 fprintf(stderr,
"Device doesn't support synchronous FIFO mode\n");
171 fprintf(stderr,
"Can't reset mode\n");
178 fprintf(stderr,
"Can't flush FIFOs & buffers\n");
186 transfers = calloc(numTransfers,
sizeof *transfers);
189 err = LIBUSB_ERROR_NO_MEM;
193 for (xferIndex = 0; xferIndex < numTransfers; xferIndex++)
195 struct libusb_transfer *transfer;
197 transfer = libusb_alloc_transfer(0);
198 transfers[xferIndex] = transfer;
201 err = LIBUSB_ERROR_NO_MEM;
205 libusb_fill_bulk_transfer(transfer, ftdi->
usb_dev, ftdi->
out_ep,
206 malloc(bufferSize), bufferSize,
210 if (!transfer->buffer)
212 err = LIBUSB_ERROR_NO_MEM;
216 transfer->status = -1;
217 err = libusb_submit_transfer(transfer);
229 fprintf(stderr,
"Can't set synchronous fifo mode: %s\n",
243 const double progressInterval = 1.0;
247 int err = libusb_handle_events_timeout(ftdi->
usb_ctx, &timeout);
248 if (err == LIBUSB_ERROR_INTERRUPTED)
250 err = libusb_handle_events_timeout(ftdi->
usb_ctx, &timeout);
261 gettimeofday(&now, NULL);
262 if (TimevalDiff(&now, &progress->
current.
time) >= progressInterval)
295 fprintf(stderr,
"cleanup\n");