From ad7aa2b382896abaf70ed5c0b6f92b314c1753d3 Mon Sep 17 00:00:00 2001 From: Kano Date: Sun, 16 Sep 2012 11:42:51 +1000 Subject: [PATCH] FPGA - allow long or short device names in detect code + style police --- README | 1 + driver-bitforce.c | 4 ++-- driver-icarus.c | 4 ++-- driver-modminer.c | 4 ++-- fpgautils.c | 53 +++++++++++++++++++++-------------------------- fpgautils.h | 26 +++++++++++------------ 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/README b/README index fd29dc8c..36de306f 100644 --- a/README +++ b/README @@ -239,6 +239,7 @@ The official supplied binaries are compiled with support for all FPGAs. To force the code to only attempt detection with a specific driver, prepend the argument with the driver name followed by a colon. For example, "icarus:/dev/ttyUSB0" or "bitforce:\\.\COM5" +or using the short name: "ica:/dev/ttyUSB0" or "bfl:\\.\COM5" For other FPGA details see the FPGA-README diff --git a/driver-bitforce.c b/driver-bitforce.c index c1107fea..ded9e923 100644 --- a/driver-bitforce.c +++ b/driver-bitforce.c @@ -45,8 +45,8 @@ enum { #endif /* WIN32 */ #include "compat.h" -#include "fpgautils.h" #include "miner.h" +#include "fpgautils.h" #define BITFORCE_SLEEP_MS 500 #define BITFORCE_TIMEOUT_S 7 @@ -230,7 +230,7 @@ static int bitforce_detect_auto(void) static void bitforce_detect(void) { - serial_detect_auto(bitforce_api.dname, bitforce_detect_one, bitforce_detect_auto); + serial_detect_auto(&bitforce_api, bitforce_detect_one, bitforce_detect_auto); } static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce) diff --git a/driver-icarus.c b/driver-icarus.c index 33b875d7..4214c31b 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -49,8 +49,8 @@ #endif #include "elist.h" -#include "fpgautils.h" #include "miner.h" +#include "fpgautils.h" // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h #define ICARUS_IO_SPEED 115200 @@ -598,7 +598,7 @@ static bool icarus_detect_one(const char *devpath) static void icarus_detect() { - serial_detect(icarus_api.dname, icarus_detect_one); + serial_detect(&icarus_api, icarus_detect_one); } static bool icarus_prepare(struct thr_info *thr) diff --git a/driver-modminer.c b/driver-modminer.c index 750f9ec5..d7359834 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -13,9 +13,9 @@ #include #include -#include "fpgautils.h" #include "logging.h" #include "miner.h" +#include "fpgautils.h" #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd" #define BISTREAM_USER_ID "\2\4$B" @@ -103,7 +103,7 @@ modminer_detect_auto() static void modminer_detect() { - serial_detect_auto(modminer_api.dname, modminer_detect_one, modminer_detect_auto); + serial_detect_auto(&modminer_api, modminer_detect_one, modminer_detect_auto); } #define bailout(...) return _bailout(-1, modminer, __VA_ARGS__); diff --git a/fpgautils.c b/fpgautils.c index 287f277e..de9d93e7 100644 --- a/fpgautils.c +++ b/fpgautils.c @@ -33,13 +33,12 @@ #endif #include "elist.h" -#include "fpgautils.h" #include "logging.h" #include "miner.h" +#include "fpgautils.h" #ifdef HAVE_LIBUDEV -int -serial_autodetect_udev(detectone_func_t detectone, const char*prodname) +int serial_autodetect_udev(detectone_func_t detectone, const char*prodname) { struct udev *udev = udev_new(); struct udev_enumerate *enumerate = udev_enumerate_new(udev); @@ -69,15 +68,13 @@ serial_autodetect_udev(detectone_func_t detectone, const char*prodname) return found; } #else -int -serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname) +int serial_autodetect_udev(__maybe_unused detectone_func_t detectone, __maybe_unused const char*prodname) { return 0; } #endif -int -serial_autodetect_devserial(detectone_func_t detectone, const char*prodname) +int serial_autodetect_devserial(detectone_func_t detectone, const char*prodname) { #ifndef WIN32 DIR *D; @@ -107,30 +104,32 @@ serial_autodetect_devserial(detectone_func_t detectone, const char*prodname) #endif } -int -_serial_detect(const char*dname, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto) +int _serial_detect(struct device_api *api, detectone_func_t detectone, autoscan_func_t autoscan, bool forceauto) { struct string_elist *iter, *tmp; - const char*s, *p; + const char *dev, *colon; bool inhibitauto = false; char found = 0; - size_t dnamel = strlen(dname); + size_t namel = strlen(api->name); + size_t dnamel = strlen(api->dname); list_for_each_entry_safe(iter, tmp, &scan_devices, list) { - s = iter->string; - if ((p = strchr(s, ':')) && p[1] != '\0') { - size_t plen = p - s; - if (plen != dnamel || strncasecmp(s, dname, plen)) + dev = iter->string; + if ((colon = strchr(dev, ':')) && colon[1] != '\0') { + size_t idlen = colon - dev; + + // allow either name:device or dname:device + if ((idlen != namel || strncasecmp(dev, api->name, idlen)) + && (idlen != dnamel || strncasecmp(dev, api->dname, idlen))) continue; - s = p + 1; + + dev = colon + 1; } - if (!strcmp(s, "auto")) + if (!strcmp(dev, "auto")) forceauto = true; - else - if (!strcmp(s, "noauto")) + else if (!strcmp(dev, "noauto")) inhibitauto = true; - else - if (detectone(s)) { + else if (detectone(dev)) { string_elist_del(iter); inhibitauto = true; ++found; @@ -311,8 +310,7 @@ void termios_debug(const char *devpath, struct termios *my_termios, const char * #endif #endif -int -serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge) +int serial_open(const char *devpath, unsigned long baud, signed short timeout, bool purge) { #ifdef WIN32 HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); @@ -429,8 +427,7 @@ serial_open(const char*devpath, unsigned long baud, signed short timeout, bool p #endif } -ssize_t -_serial_read(int fd, char *buf, size_t bufsiz, char *eol) +ssize_t _serial_read(int fd, char *buf, size_t bufsiz, char *eol) { ssize_t len, tlen = 0; while (bufsiz) { @@ -446,8 +443,7 @@ _serial_read(int fd, char *buf, size_t bufsiz, char *eol) return tlen; } -static FILE* -_open_bitstream(const char*path, const char*subdir, const char*filename) +static FILE *_open_bitstream(const char *path, const char *subdir, const char *filename) { char fullpath[PATH_MAX]; strcpy(fullpath, path); @@ -471,8 +467,7 @@ _open_bitstream(const char*path, const char*subdir, const char*filename) _open_bitstream(path, NULL); \ } while(0) -FILE* -open_bitstream(const char*dname, const char*filename) +FILE *open_bitstream(const char *dname, const char *filename) { FILE *f; diff --git a/fpgautils.h b/fpgautils.h index 5b743bc5..5c8b6bfe 100644 --- a/fpgautils.h +++ b/fpgautils.h @@ -16,24 +16,24 @@ typedef bool(*detectone_func_t)(const char*); typedef int(*autoscan_func_t)(); -extern int _serial_detect(const char*dname, detectone_func_t, autoscan_func_t, bool force_autoscan); -#define serial_detect_fauto(dname, detectone, autoscan) \ - _serial_detect(dname, detectone, autoscan, true) -#define serial_detect_auto(dname, detectone, autoscan) \ - _serial_detect(dname, detectone, autoscan, false) -#define serial_detect(dname, detectone) \ - _serial_detect(dname, detectone, NULL, false) -extern int serial_autodetect_devserial(detectone_func_t, const char*prodname); -extern int serial_autodetect_udev (detectone_func_t, const char*prodname); +extern int _serial_detect(struct device_api *api, detectone_func_t, autoscan_func_t, bool force_autoscan); +#define serial_detect_fauto(api, detectone, autoscan) \ + _serial_detect(api, detectone, autoscan, true) +#define serial_detect_auto(api, detectone, autoscan) \ + _serial_detect(api, detectone, autoscan, false) +#define serial_detect(api, detectone) \ + _serial_detect(api, detectone, NULL, false) +extern int serial_autodetect_devserial(detectone_func_t, const char *prodname); +extern int serial_autodetect_udev(detectone_func_t, const char *prodname); -extern int serial_open(const char*devpath, unsigned long baud, signed short timeout, bool purge); -extern ssize_t _serial_read(int fd, char *buf, size_t buflen, char*eol); +extern int serial_open(const char *devpath, unsigned long baud, signed short timeout, bool purge); +extern ssize_t _serial_read(int fd, char *buf, size_t buflen, char *eol); #define serial_read(fd, buf, count) \ _serial_read(fd, (char*)(buf), count, NULL) #define serial_read_line(fd, buf, bufsiz, eol) \ - _serial_read(fd, buf, count, &eol) + _serial_read(fd, buf, bufsiz, &eol) #define serial_close(fd) close(fd) -extern FILE*open_bitstream(const char*dname, const char*filename); +extern FILE *open_bitstream(const char *dname, const char *filename); #endif