+ presto->total_out=0;
+ presto->total_in=0;
+
+ presto->jtag_tms=0;
+ presto->jtag_tck=0;
+ presto->jtag_rst=0;
+ presto->jtag_tdi_data=0;
+ presto->jtag_tdi_count=0;
+
+ presto->jtag_speed=0;
+
+#if BUILD_PRESTO_FTD2XX == 1
+ return presto_open_ftd2xx(req_serial);
+#elif BUILD_PRESTO_LIBFTDI == 1
+ return presto_open_libftdi(req_serial);
+#endif
+}
+
+static int presto_close(void)
+{
+
+ int result = ERROR_OK;
+
+#if BUILD_PRESTO_FTD2XX == 1
+ unsigned long ftbytes;
+
+ if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
+ return result;
+
+ presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
+ if (presto->status != FT_OK)
+ result = ERROR_JTAG_DEVICE_ERROR;
+
+ presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
+ if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
+ result = ERROR_JTAG_DEVICE_ERROR;
+
+ if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK)
+ result = ERROR_JTAG_DEVICE_ERROR;
+
+ if ((presto->status = FT_Close(presto->handle)) != FT_OK)
+ result = ERROR_JTAG_DEVICE_ERROR;
+ else
+ presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
+
+#elif BUILD_PRESTO_LIBFTDI == 1
+
+ if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) != sizeof(presto_init_seq))
+ result = ERROR_JTAG_DEVICE_ERROR;
+
+ if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0)
+ result = ERROR_JTAG_DEVICE_ERROR;
+
+ if ((presto->retval = ftdi_usb_close(&presto->ftdic)) < 0)
+ result = ERROR_JTAG_DEVICE_ERROR;
+ else
+ ftdi_deinit(&presto->ftdic);
+#endif
+
+ return result;
+}
+
+static int presto_flush(void)
+{
+ if (presto->buff_out_pos == 0)
+ return ERROR_OK;
+
+#if BUILD_PRESTO_FTD2XX == 1
+ if (presto->status != FT_OK)
+#elif BUILD_PRESTO_LIBFTDI == 1
+ if (presto->retval < 0)
+#endif