From 31b01c8ac516275bd5990a2931cf9689bb3e3b83 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 5 Aug 2012 16:13:53 +1000 Subject: [PATCH] Author: Luke Dashjr Date: Thu Jul 12 16:49:26 2012 +0000 Use FTD2XX.DLL on Windows to autodetect BitFORCE SHA256 devices --- driver-bitforce.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/driver-bitforce.c b/driver-bitforce.c index 1cb958b1..c9926deb 100644 --- a/driver-bitforce.c +++ b/driver-bitforce.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,28 @@ #include "config.h" +#ifdef WIN32 + +#include + +#define dlsym (void*)GetProcAddress +#define dlclose FreeLibrary + +typedef unsigned long FT_STATUS; +typedef PVOID FT_HANDLE; +__stdcall FT_STATUS (*FT_ListDevices)(PVOID pArg1, PVOID pArg2, DWORD Flags); +__stdcall FT_STATUS (*FT_Open)(int idx, FT_HANDLE*); +__stdcall FT_STATUS (*FT_GetComPortNumber)(FT_HANDLE, LPLONG lplComPortNumber); +__stdcall FT_STATUS (*FT_Close)(FT_HANDLE); +const uint32_t FT_OPEN_BY_DESCRIPTION = 2; +const uint32_t FT_LIST_ALL = 0x20000000; +const uint32_t FT_LIST_NUMBER_ONLY = 0x80000000; +enum { + FT_OK, +}; + +#endif /* WIN32 */ + #include "compat.h" #include "fpgautils.h" #include "miner.h" @@ -114,10 +137,89 @@ static bool bitforce_detect_one(const char *devpath) return add_cgpu(bitforce); } +#define LOAD_SYM(sym) do { \ + if (!(sym = dlsym(dll, #sym))) { \ + applog(LOG_DEBUG, "Failed to load " #sym ", not using FTDI bitforce autodetect"); \ + goto nogood; \ + } \ +} while(0) + +static char bitforce_autodetect_ftdi() +{ +#ifdef WIN32 + FT_STATUS ftStatus; + DWORD numDevs; + HMODULE dll = LoadLibrary("FTD2XX.DLL"); + if (!dll) + { + applog(LOG_DEBUG, "FTD2XX.DLL failed to load, not using FTDI bitforce autodetect"); + return 0; + } + LOAD_SYM(FT_ListDevices); + LOAD_SYM(FT_Open); + LOAD_SYM(FT_GetComPortNumber); + LOAD_SYM(FT_Close); + + ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY); + if (ftStatus != FT_OK) + { + applog(LOG_DEBUG, "FTDI device count failed, not using FTDI bitforce autodetect"); +nogood: + dlclose(dll); + return 0; + } + applog(LOG_DEBUG, "FTDI reports %u devices", (unsigned)numDevs); + + char buf[65 * numDevs]; + char*bufptrs[numDevs + 1]; + int i; + for (i = 0; i < numDevs; ++i) + bufptrs[i] = &buf[i * 65]; + bufptrs[numDevs] = NULL; + ftStatus = FT_ListDevices(bufptrs, &numDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); + if (ftStatus != FT_OK) + { + applog(LOG_DEBUG, "FTDI device list failed, not using FTDI bitforce autodetect"); + goto nogood; + } + + char devpath[] = "\\\\.\\COMnnnnn"; + char *devpathnum = &devpath[7]; + char found = 0; + for (i = numDevs; i > 0; ) + { + --i; + bufptrs[i][64] = '\0'; + + if (!(strstr(bufptrs[i], "BitFORCE") && strstr(bufptrs[i], "SHA256"))) + continue; + + FT_HANDLE ftHandle; + if (FT_OK != FT_Open(i, &ftHandle)) + continue; + LONG lComPortNumber; + ftStatus = FT_GetComPortNumber(ftHandle, &lComPortNumber); + FT_Close(ftHandle); + if (FT_OK != ftStatus || lComPortNumber < 0) + continue; + + sprintf(devpathnum, "%d", (int)lComPortNumber); + + if (bitforce_detect_one(devpath)) + ++found; + } + dlclose(dll); + return found; +#else /* NOT WIN32 */ + return 0; +#endif +} + static char bitforce_detect_auto() { return (serial_autodetect_udev (bitforce_detect_one, "BitFORCE*SHA256") ?: serial_autodetect_devserial(bitforce_detect_one, "BitFORCE_SHA256") ?: + bitforce_autodetect_ftdi() ?: 0); }