+#if BUILD_PRESTO_FTD2XX == 1
+ DWORD ftbytes;
+ if ((presto->status = FT_Read(presto->handle, buf, size, &ftbytes)) != FT_OK)
+ {
+ LOG_ERROR("FT_Read returned: %lu", presto->status);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+#elif BUILD_PRESTO_LIBFTDI == 1
+ uint32_t ftbytes = 0;
+
+ struct timeval timeout, now;
+ gettimeofday(&timeout, NULL);
+ timeval_add_time(&timeout, 1, 0); /* one second timeout */
+
+ while (ftbytes < size)
+ {
+ if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes)) < 0)
+ {
+ LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+ ftbytes += presto->retval;
+
+ gettimeofday(&now, NULL);
+ if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
+ break;
+ }
+#endif
+
+ if (ftbytes != size)
+ {
+ /* this is just a warning, there might have been timeout when detecting PRESTO, which is not fatal */
+ LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
+ (unsigned)ftbytes, (unsigned)size);
+ return ERROR_JTAG_DEVICE_ERROR;
+ }
+
+ return ERROR_OK;
+}
+
+#if BUILD_PRESTO_FTD2XX == 1
+static int presto_open_ftd2xx(char *req_serial)
+{
+ uint32_t i;