- LOG_DEBUG("FTDI devices available: %lu", numdevs);
- for (i = 0; i < numdevs; i++)
- {
- if ((presto->status = FT_Open(i, &(presto->handle))) != FT_OK)
- {
- /* this is not fatal, the device may be legitimately open by other process, hence debug message only */
- LOG_DEBUG("FT_Open failed: %i", (int)presto->status);
- continue;
- }
- LOG_DEBUG("FTDI device %i open", (int)i);
-
- if ((presto->status = FT_GetDeviceInfo(presto->handle, &device, &vidpid,
- presto->serial, devname, NULL)) == FT_OK)
- {
- if (vidpid == PRESTO_VID_PID
- && (req_serial == NULL || !strcmp(presto->serial, req_serial)))
- break;
- }
- else
- LOG_DEBUG("FT_GetDeviceInfo failed: %lu", presto->status);
-
- LOG_DEBUG("FTDI device %i does not match, closing", (int)i);
- FT_Close(presto->handle);
- presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
- }
-
- if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
- return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */
-
- if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
-
- if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- presto_data = 0xD0;
- if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
- probably a bug in library threading */
- usleep(100000);
- if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- if (ftbytes != 1)
- {
- LOG_DEBUG("PRESTO reset");
-
- if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
- if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
- if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- presto_data = 0;
- for (i = 0; i < 4 * 62; i++)
- if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- usleep(100000);
-
- if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- presto_data = 0xD0;
- if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
- probably a bug in library threading */
- usleep(100000);
- if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
- return ERROR_JTAG_DEVICE_ERROR;
-
- if (ftbytes != 1)
- {
- LOG_DEBUG("PRESTO not responding");
- return ERROR_JTAG_DEVICE_ERROR;
- }
- }
-
- if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK)
- return 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))
- return ERROR_JTAG_DEVICE_ERROR;
-