mirror of https://github.com/GOSTSec/sgminer
Con Kolivas
13 years ago
14 changed files with 946 additions and 238 deletions
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
All the bitstream files included in this directory that follow the name pattern fpgaminer_*.ncd are: |
||||
|
||||
---- |
||||
|
||||
Copyright (c) 2011-2012 fpgaminer@bitcoin-mining.com |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
||||
---- |
||||
|
||||
You can find the original sources at the Open Source FPGA Bitcoin Miner project GitHub repository: |
||||
https://github.com/progranism/Open-Source-FPGA-Bitcoin-Miner/tree/master/projects/X6000_ztex_comm4/hdl |
Binary file not shown.
@ -0,0 +1,514 @@
@@ -0,0 +1,514 @@
|
||||
/*
|
||||
* Copyright 2012 Luke Dashjr |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the Free |
||||
* Software Foundation; either version 3 of the License, or (at your option) |
||||
* any later version. See COPYING for more details. |
||||
*/ |
||||
|
||||
#include "config.h" |
||||
|
||||
#include <stdarg.h> |
||||
#include <stdio.h> |
||||
#include <unistd.h> |
||||
|
||||
#include "fpgautils.h" |
||||
#include "logging.h" |
||||
#include "miner.h" |
||||
|
||||
#define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd" |
||||
#define BISTREAM_USER_ID "\2\4$B" |
||||
|
||||
struct device_api modminer_api; |
||||
|
||||
static inline bool |
||||
_bailout(int fd, struct cgpu_info*modminer, int prio, const char *fmt, ...) |
||||
{ |
||||
if (fd != -1) |
||||
serial_close(fd); |
||||
if (modminer) { |
||||
modminer->device_fd = -1; |
||||
mutex_unlock(&modminer->device_mutex); |
||||
} |
||||
|
||||
va_list ap; |
||||
va_start(ap, fmt); |
||||
vapplog(prio, fmt, ap); |
||||
va_end(ap); |
||||
return false; |
||||
} |
||||
#define bailout(...) return _bailout(fd, NULL, __VA_ARGS__); |
||||
|
||||
static bool |
||||
modminer_detect_one(const char *devpath) |
||||
{ |
||||
int fd = serial_open(devpath, 0, 10, true); |
||||
if (unlikely(fd == -1)) |
||||
bailout(LOG_DEBUG, "ModMiner detect: failed to open %s", devpath); |
||||
|
||||
char buf[0x100]; |
||||
size_t len; |
||||
|
||||
// Sending 45 noops, just in case the device was left in "start job" reading
|
||||
write(fd, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 45) ?:0; |
||||
while (serial_read(fd, buf, sizeof(buf)) > 0) |
||||
; |
||||
|
||||
if (1 != write(fd, "\x01", 1)) // Get version
|
||||
bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get version)", devpath); |
||||
len = serial_read(fd, buf, sizeof(buf)-1); |
||||
if (len < 1) |
||||
bailout(LOG_DEBUG, "ModMiner detect: no response to version request from %s", devpath); |
||||
buf[len] = '\0'; |
||||
char*devname = strdup(buf); |
||||
applog(LOG_DEBUG, "ModMiner identified as: %s", devname); |
||||
|
||||
if (1 != write(fd, "\x02", 1)) // Get FPGA count
|
||||
bailout(LOG_DEBUG, "ModMiner detect: write failed on %s (get FPGA count)", devpath); |
||||
len = read(fd, buf, 1); |
||||
if (len < 1) |
||||
bailout(LOG_ERR, "ModMiner detect: timeout waiting for FPGA count from %s", devpath); |
||||
if (!buf[0]) |
||||
bailout(LOG_ERR, "ModMiner detect: zero FPGAs reported on %s", devpath); |
||||
applog(LOG_DEBUG, "ModMiner %s has %u FPGAs", devname, buf[0]); |
||||
|
||||
serial_close(fd); |
||||
|
||||
struct cgpu_info *modminer; |
||||
modminer = calloc(1, sizeof(*modminer)); |
||||
modminer->api = &modminer_api; |
||||
mutex_init(&modminer->device_mutex); |
||||
modminer->device_path = strdup(devpath); |
||||
modminer->device_fd = -1; |
||||
modminer->deven = DEV_ENABLED; |
||||
modminer->threads = buf[0]; |
||||
modminer->name = devname; |
||||
modminer->cutofftemp = 85; |
||||
|
||||
return add_cgpu(modminer); |
||||
} |
||||
|
||||
#undef bailout |
||||
|
||||
static char |
||||
modminer_detect_auto() |
||||
{ |
||||
return |
||||
serial_autodetect_udev (modminer_detect_one, "BTCFPGA*ModMiner") ?: |
||||
serial_autodetect_devserial(modminer_detect_one, "BTCFPGA_ModMiner") ?: |
||||
0; |
||||
} |
||||
|
||||
static void |
||||
modminer_detect() |
||||
{ |
||||
serial_detect_auto("modminer", modminer_detect_one, modminer_detect_auto); |
||||
} |
||||
|
||||
static void |
||||
get_modminer_statline_before(char *buf, struct cgpu_info *modminer) |
||||
{ |
||||
float gt = modminer->temp; |
||||
if (gt > 0) |
||||
tailsprintf(buf, "%5.1fC ", gt); |
||||
else |
||||
tailsprintf(buf, " ", gt); |
||||
tailsprintf(buf, " | "); |
||||
} |
||||
|
||||
#define bailout(...) return _bailout(-1, modminer, __VA_ARGS__); |
||||
#define bailout2(...) return _bailout(fd, modminer, __VA_ARGS__); |
||||
|
||||
#define check_magic(L) do { \ |
||||
if (1 != fread(buf, 1, 1, f)) \ |
||||
bailout(LOG_ERR, "Error reading ModMiner firmware ('%c')", L); \ |
||||
if (buf[0] != L) \ |
||||
bailout(LOG_ERR, "ModMiner firmware has wrong magic ('%c')", L); \ |
||||
} while(0) |
||||
|
||||
#define read_str(eng) do { \ |
||||
if (1 != fread(buf, 2, 1, f)) \ |
||||
bailout(LOG_ERR, "Error reading ModMiner firmware (" eng " len)"); \ |
||||
len = (ubuf[0] << 8) | ubuf[1]; \ |
||||
if (len >= sizeof(buf)) \ |
||||
bailout(LOG_ERR, "ModMiner firmware " eng " too long"); \ |
||||
if (1 != fread(buf, len, 1, f)) \ |
||||
bailout(LOG_ERR, "Error reading ModMiner firmware (" eng ")"); \ |
||||
buf[len] = '\0'; \ |
||||
} while(0) |
||||
|
||||
#define status_read(eng) do { \ |
||||
FD_SET(fd, &fds); \ |
||||
select(fd+1, &fds, NULL, NULL, NULL); \ |
||||
if (1 != read(fd, buf, 1)) \ |
||||
bailout2(LOG_ERR, "Error programming ModMiner %s (" eng ")", modminer->device_path); \ |
||||
if (buf[0] != 1) \ |
||||
bailout2(LOG_ERR, "Wrong " eng " programming ModMiner %s", modminer->device_path); \ |
||||
} while(0) |
||||
|
||||
static bool |
||||
modminer_fpga_upload_bitstream(struct cgpu_info*modminer) |
||||
{ |
||||
fd_set fds; |
||||
char buf[0x100]; |
||||
unsigned char *ubuf = (unsigned char*)buf; |
||||
unsigned long len; |
||||
char *p; |
||||
const char *fwfile = BITSTREAM_FILENAME; |
||||
char fpgaid = 4; // "all FPGAs"
|
||||
|
||||
FILE *f = open_bitstream("modminer", fwfile); |
||||
if (!f) |
||||
bailout(LOG_ERR, "Error opening ModMiner firmware file %s", fwfile); |
||||
if (1 != fread(buf, 2, 1, f)) |
||||
bailout(LOG_ERR, "Error reading ModMiner firmware (magic)"); |
||||
if (buf[0] || buf[1] != 9) |
||||
bailout(LOG_ERR, "ModMiner firmware has wrong magic (9)"); |
||||
if (-1 == fseek(f, 11, SEEK_CUR)) |
||||
bailout(LOG_ERR, "ModMiner firmware seek failed"); |
||||
check_magic('a'); |
||||
read_str("design name"); |
||||
applog(LOG_DEBUG, "ModMiner firmware file %s info:", fwfile); |
||||
applog(LOG_DEBUG, " Design name: %s", buf); |
||||
p = strrchr(buf, ';') ?: buf; |
||||
p = strrchr(buf, '=') ?: p; |
||||
if (p[0] == '=') |
||||
++p; |
||||
long fwusercode = strtol(p, &p, 16); |
||||
if (p[0] != '\0') |
||||
bailout(LOG_ERR, "Bad usercode in ModMiner firmware file"); |
||||
if (fwusercode == 0xffffffff) |
||||
bailout(LOG_ERR, "ModMiner firmware doesn't support user code"); |
||||
applog(LOG_DEBUG, " Version: %u, build %u", (fwusercode >> 8) & 0xff, fwusercode & 0xff); |
||||
check_magic('b'); |
||||
read_str("part number"); |
||||
applog(LOG_DEBUG, " Part number: %s", buf); |
||||
check_magic('c'); |
||||
read_str("build date"); |
||||
applog(LOG_DEBUG, " Build date: %s", buf); |
||||
check_magic('d'); |
||||
read_str("build time"); |
||||
applog(LOG_DEBUG, " Build time: %s", buf); |
||||
check_magic('e'); |
||||
if (1 != fread(buf, 4, 1, f)) |
||||
bailout(LOG_ERR, "Error reading ModMiner firmware (data len)"); |
||||
len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3]; |
||||
applog(LOG_DEBUG, " Bitstream size: %lu", len); |
||||
|
||||
int fd = modminer->device_fd; |
||||
|
||||
applog(LOG_WARNING, "Programming %s... DO NOT EXIT CGMINER UNTIL COMPLETE", modminer->device_path, fpgaid); |
||||
buf[0] = '\x05'; // Program Bitstream
|
||||
buf[1] = fpgaid; |
||||
buf[2] = (len >> 0) & 0xff; |
||||
buf[3] = (len >> 8) & 0xff; |
||||
buf[4] = (len >> 16) & 0xff; |
||||
buf[5] = (len >> 24) & 0xff; |
||||
if (6 != write(fd, buf, 6)) |
||||
bailout2(LOG_ERR, "Error programming ModMiner %s (cmd)", modminer->device_path); |
||||
status_read("cmd reply"); |
||||
size_t buflen; |
||||
while (len) { |
||||
buflen = len < 32 ? len : 32; |
||||
if (fread(buf, buflen, 1, f) != 1) |
||||
bailout2(LOG_ERR, "File underrun programming ModMiner %s (%d bytes left)", modminer->device_path, len); |
||||
if (write(fd, buf, buflen) != buflen) |
||||
bailout2(LOG_ERR, "Error programming ModMiner %s (data)"); |
||||
status_read("status"); |
||||
len -= buflen; |
||||
} |
||||
status_read("final status"); |
||||
applog(LOG_WARNING, "Done programming %s", modminer->device_path); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
static bool |
||||
modminer_device_prepare(struct cgpu_info *modminer) |
||||
{ |
||||
int fd = serial_open(modminer->device_path, 0, /*FIXME=-1*/3000, true); |
||||
if (unlikely(-1 == fd)) |
||||
bailout(LOG_ERR, "Failed to open ModMiner on %s", modminer->device_path); |
||||
|
||||
modminer->device_fd = fd; |
||||
applog(LOG_INFO, "Opened ModMiner on %s", modminer->device_path); |
||||
|
||||
struct timeval now; |
||||
gettimeofday(&now, NULL); |
||||
get_datestamp(modminer->init, &now); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
#undef bailout |
||||
|
||||
struct modminer_fpga_state { |
||||
bool work_running; |
||||
struct work running_work; |
||||
struct timeval tv_workstart; |
||||
uint32_t hashes; |
||||
|
||||
char next_work_cmd[46]; |
||||
|
||||
unsigned char clock; |
||||
int no_nonce_counter; |
||||
int good_share_counter; |
||||
time_t last_cutoff_reduced; |
||||
}; |
||||
|
||||
static bool |
||||
modminer_fpga_prepare(struct thr_info *thr) |
||||
{ |
||||
struct cgpu_info *modminer = thr->cgpu; |
||||
|
||||
// Don't need to lock the mutex here, since prepare runs from the main thread before the miner threads start
|
||||
if (modminer->device_fd == -1 && !modminer_device_prepare(modminer)) |
||||
return false; |
||||
|
||||
struct modminer_fpga_state *state; |
||||
state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state)); |
||||
state->next_work_cmd[0] = '\x08'; // Send Job
|
||||
state->next_work_cmd[1] = thr->device_thread; // FPGA id
|
||||
|
||||
return true; |
||||
} |
||||
|
||||
static bool |
||||
modminer_reduce_clock(struct thr_info*thr, bool needlock) |
||||
{ |
||||
struct cgpu_info*modminer = thr->cgpu; |
||||
struct modminer_fpga_state *state = thr->cgpu_data; |
||||
char fpgaid = thr->device_thread; |
||||
int fd = modminer->device_fd; |
||||
unsigned char cmd[6], buf[1]; |
||||
|
||||
if (state->clock <= 100) |
||||
return false; |
||||
|
||||
cmd[0] = '\x06'; // set clock speed
|
||||
cmd[1] = fpgaid; |
||||
cmd[2] = state->clock -= 2; |
||||
cmd[3] = cmd[4] = cmd[5] = '\0'; |
||||
|
||||
if (needlock) |
||||
mutex_lock(&modminer->device_mutex); |
||||
if (6 != write(fd, cmd, 6)) |
||||
bailout2(LOG_ERR, "Error writing to ModMiner (set clock speed)"); |
||||
if (serial_read(fd, &buf, 1) != 1) |
||||
bailout2(LOG_ERR, "Error reading from ModMiner (set clock speed)"); |
||||
if (needlock) |
||||
mutex_unlock(&modminer->device_mutex); |
||||
|
||||
applog(LOG_WARNING, "ModMiner: Setting clock speed of %s %d (FPGA #%u) to %u", modminer->api->name, modminer->device_id, fpgaid, state->clock); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
static bool |
||||
modminer_fpga_init(struct thr_info *thr) |
||||
{ |
||||
struct cgpu_info *modminer = thr->cgpu; |
||||
struct modminer_fpga_state *state = thr->cgpu_data; |
||||
int fd; |
||||
char fpgaid = thr->device_thread; |
||||
|
||||
unsigned char cmd[2], buf[4]; |
||||
|
||||
mutex_lock(&modminer->device_mutex); |
||||
fd = modminer->device_fd; |
||||
if (fd == -1) { |
||||
// Died in another thread...
|
||||
mutex_unlock(&modminer->device_mutex); |
||||
return false; |
||||
} |
||||
|
||||
cmd[0] = '\x04'; // Read USER code (bitstream id)
|
||||
cmd[1] = fpgaid; |
||||
if (write(fd, cmd, 2) != 2) |
||||
bailout2(LOG_ERR, "Error writing to ModMiner (read USER code)"); |
||||
if (serial_read(fd, buf, 4) != 4) |
||||
bailout2(LOG_ERR, "Error reading from ModMiner (read USER code)"); |
||||
|
||||
if (memcmp(buf, BISTREAM_USER_ID, 4)) { |
||||
applog(LOG_ERR, "FPGA #%d not programmed", fpgaid); |
||||
if (!modminer_fpga_upload_bitstream(modminer)) |
||||
return false; |
||||
} |
||||
else |
||||
applog(LOG_DEBUG, "FPGA #%d is already programmed :)", fpgaid); |
||||
|
||||
state->clock = 212; // Will be reduced to 210 by modminer_reduce_clock
|
||||
modminer_reduce_clock(thr, false); |
||||
|
||||
mutex_unlock(&modminer->device_mutex); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
static bool |
||||
modminer_prepare_next_work(struct modminer_fpga_state*state, struct work*work) |
||||
{ |
||||
char *midstate = state->next_work_cmd + 2; |
||||
char *taildata = midstate + 32; |
||||
if (!(memcmp(midstate, work->midstate, 32) || memcmp(taildata, work->data + 64, 12))) |
||||
return false; |
||||
memcpy(midstate, work->midstate, 32); |
||||
memcpy(taildata, work->data + 64, 12); |
||||
return true; |
||||
} |
||||
|
||||
static bool |
||||
modminer_start_work(struct thr_info*thr) |
||||
{ |
||||
fd_set fds; |
||||
struct cgpu_info*modminer = thr->cgpu; |
||||
struct modminer_fpga_state *state = thr->cgpu_data; |
||||
int fd = modminer->device_fd; |
||||
|
||||
char buf[1]; |
||||
|
||||
mutex_lock(&modminer->device_mutex); |
||||
if (46 != write(fd, state->next_work_cmd, 46)) |
||||
bailout2(LOG_ERR, "Error writing to ModMiner (start work)"); |
||||
gettimeofday(&state->tv_workstart, NULL); |
||||
state->hashes = 0; |
||||
status_read("start work"); |
||||
mutex_unlock(&modminer->device_mutex); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
#define work_restart(thr) work_restart[thr->id].restart |
||||
|
||||
static uint64_t |
||||
modminer_process_results(struct thr_info*thr) |
||||
{ |
||||
struct cgpu_info*modminer = thr->cgpu; |
||||
struct modminer_fpga_state *state = thr->cgpu_data; |
||||
char fpgaid = thr->device_thread; |
||||
int fd = modminer->device_fd; |
||||
struct work *work = &state->running_work; |
||||
|
||||
char cmd[2], temperature; |
||||
uint32_t nonce; |
||||
long iter; |
||||
bool bad; |
||||
cmd[0] = '\x0a'; |
||||
cmd[1] = fpgaid; |
||||
|
||||
mutex_lock(&modminer->device_mutex); |
||||
if (2 == write(fd, cmd, 2) && read(fd, &temperature, 1) == 1) |
||||
{ |
||||
modminer->temp = (float)temperature; |
||||
if (temperature > modminer->cutofftemp - 2) { |
||||
if (temperature > modminer->cutofftemp) { |
||||
applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", modminer->api->name, modminer->device_id); |
||||
modminer->deven = DEV_RECOVER; |
||||
|
||||
modminer->device_last_not_well = time(NULL); |
||||
modminer->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF; |
||||
++modminer->dev_thermal_cutoff_count; |
||||
} else { |
||||
time_t now = time(NULL); |
||||
if (state->last_cutoff_reduced != now) { |
||||
state->last_cutoff_reduced = now; |
||||
modminer_reduce_clock(thr, false); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
cmd[0] = '\x09'; |
||||
iter = 200; |
||||
while (1) { |
||||
if (write(fd, cmd, 2) != 2) |
||||
bailout2(LOG_ERR, "Error reading from ModMiner (get nonce)"); |
||||
serial_read(fd, &nonce, 4); |
||||
mutex_unlock(&modminer->device_mutex); |
||||
if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) { |
||||
state->no_nonce_counter = 0; |
||||
bad = !test_nonce(work, nonce); |
||||
if (!bad) |
||||
submit_nonce(thr, work, nonce); |
||||
else { |
||||
++hw_errors; |
||||
if (++modminer->hw_errors * 100 > 1000 + state->good_share_counter) |
||||
// Only reduce clocks if hardware errors are more than ~1% of results
|
||||
modminer_reduce_clock(thr, true); |
||||
} |
||||
} |
||||
else |
||||
if (++state->no_nonce_counter > 18000) { |
||||
state->no_nonce_counter = 0; |
||||
modminer_reduce_clock(thr, true); |
||||
} |
||||
if (work_restart(thr)) |
||||
break; |
||||
usleep(10000); |
||||
if (work_restart(thr) || !--iter) |
||||
break; |
||||
mutex_lock(&modminer->device_mutex); |
||||
} |
||||
|
||||
struct timeval tv_workend, elapsed; |
||||
gettimeofday(&tv_workend, NULL); |
||||
timeval_subtract(&elapsed, &tv_workend, &state->tv_workstart); |
||||
|
||||
uint64_t hashes = (uint64_t)state->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec); |
||||
if (hashes > 0xffffffff) |
||||
hashes = 0xffffffff; |
||||
else |
||||
if (hashes <= state->hashes) |
||||
hashes = 1; |
||||
else |
||||
hashes -= state->hashes; |
||||
state->hashes += hashes; |
||||
return hashes; |
||||
} |
||||
|
||||
static uint64_t |
||||
modminer_scanhash(struct thr_info*thr, struct work*work, uint64_t __maybe_unused max_nonce) |
||||
{ |
||||
struct modminer_fpga_state *state = thr->cgpu_data; |
||||
uint64_t hashes = 1; |
||||
bool startwork; |
||||
|
||||
startwork = modminer_prepare_next_work(state, work); |
||||
if (state->work_running) { |
||||
hashes = modminer_process_results(thr); |
||||
if (work_restart(thr)) { |
||||
state->work_running = false; |
||||
return 1; |
||||
} |
||||
} |
||||
else |
||||
state->work_running = true; |
||||
|
||||
if (startwork) { |
||||
if (!modminer_start_work(thr)) |
||||
return 0; |
||||
memcpy(&state->running_work, work, sizeof(state->running_work)); |
||||
} |
||||
|
||||
// This is intentionally early
|
||||
work->blk.nonce += hashes; |
||||
return hashes; |
||||
} |
||||
|
||||
static void |
||||
modminer_fpga_shutdown(struct thr_info *thr) |
||||
{ |
||||
free(thr->cgpu_data); |
||||
} |
||||
|
||||
struct device_api modminer_api = { |
||||
.dname = "modminer", |
||||
.name = "PGA", |
||||
.api_detect = modminer_detect, |
||||
.get_statline_before = get_modminer_statline_before, |
||||
.thread_prepare = modminer_fpga_prepare, |
||||
.thread_init = modminer_fpga_init, |
||||
.scanhash = modminer_scanhash, |
||||
.thread_shutdown = modminer_fpga_shutdown, |
||||
}; |
@ -0,0 +1,273 @@
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* Copyright 2012 Luke Dashjr |
||||
* Copyright 2012 Andrew Smith |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the Free |
||||
* Software Foundation; either version 3 of the License, or (at your option) |
||||
* any later version. See COPYING for more details. |
||||
*/ |
||||
|
||||
#include "config.h" |
||||
|
||||
#include <sys/types.h> |
||||
#include <dirent.h> |
||||
#include <string.h> |
||||
|
||||
#ifndef WIN32 |
||||
#include <termios.h> |
||||
#include <sys/stat.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#ifndef O_CLOEXEC |
||||
#define O_CLOEXEC 0 |
||||
#endif |
||||
#else |
||||
#include <windows.h> |
||||
#include <io.h> |
||||
#endif |
||||
|
||||
#ifdef HAVE_LIBUDEV |
||||
#include <libudev.h> |
||||
#endif |
||||
|
||||
#include "elist.h" |
||||
#include "fpgautils.h" |
||||
#include "logging.h" |
||||
#include "miner.h" |
||||
|
||||
char |
||||
serial_autodetect_udev(detectone_func_t detectone, const char*prodname) |
||||
{ |
||||
#ifdef HAVE_LIBUDEV |
||||
if (total_devices == MAX_DEVICES) |
||||
return 0; |
||||
|
||||
struct udev *udev = udev_new(); |
||||
struct udev_enumerate *enumerate = udev_enumerate_new(udev); |
||||
struct udev_list_entry *list_entry; |
||||
char found = 0; |
||||
|
||||
udev_enumerate_add_match_subsystem(enumerate, "tty"); |
||||
udev_enumerate_add_match_property(enumerate, "ID_MODEL", prodname); |
||||
udev_enumerate_scan_devices(enumerate); |
||||
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) { |
||||
struct udev_device *device = udev_device_new_from_syspath( |
||||
udev_enumerate_get_udev(enumerate), |
||||
udev_list_entry_get_name(list_entry) |
||||
); |
||||
if (!device) |
||||
continue; |
||||
|
||||
const char *devpath = udev_device_get_devnode(device); |
||||
if (devpath && detectone(devpath)) |
||||
++found; |
||||
|
||||
udev_device_unref(device); |
||||
|
||||
if (total_devices == MAX_DEVICES) |
||||
break; |
||||
} |
||||
udev_enumerate_unref(enumerate); |
||||
udev_unref(udev); |
||||
|
||||
return found; |
||||
#else |
||||
return 0; |
||||
#endif |
||||
} |
||||
|
||||
char |
||||
serial_autodetect_devserial(detectone_func_t detectone, const char*prodname) |
||||
{ |
||||
#ifndef WIN32 |
||||
if (total_devices == MAX_DEVICES) |
||||
return 0; |
||||
|
||||
DIR *D; |
||||
struct dirent *de; |
||||
const char udevdir[] = "/dev/serial/by-id"; |
||||
char devpath[sizeof(udevdir) + 1 + NAME_MAX]; |
||||
char *devfile = devpath + sizeof(udevdir); |
||||
char found = 0; |
||||
|
||||
D = opendir(udevdir); |
||||
if (!D) |
||||
return 0; |
||||
memcpy(devpath, udevdir, sizeof(udevdir) - 1); |
||||
devpath[sizeof(udevdir) - 1] = '/'; |
||||
while ( (de = readdir(D)) ) { |
||||
if (!strstr(de->d_name, prodname)) |
||||
continue; |
||||
strcpy(devfile, de->d_name); |
||||
if (detectone(devpath)) { |
||||
++found; |
||||
if (total_devices == MAX_DEVICES) |
||||
break; |
||||
} |
||||
} |
||||
closedir(D); |
||||
|
||||
return found; |
||||
#else |
||||
return 0; |
||||
#endif |
||||
} |
||||
|
||||
char |
||||
_serial_detect(const char*dnamec, size_t dnamel, detectone_func_t detectone, autoscan_func_t autoscan, bool force_autoscan) |
||||
{ |
||||
if (total_devices == MAX_DEVICES) |
||||
return 0; |
||||
|
||||
struct string_elist *iter, *tmp; |
||||
const char*s; |
||||
bool inhibitauto = false; |
||||
bool forceauto = false; |
||||
char found = 0; |
||||
|
||||
list_for_each_entry_safe(iter, tmp, &scan_devices, list) { |
||||
s = iter->string; |
||||
if (!strncmp(dnamec, iter->string, dnamel)) |
||||
s += dnamel; |
||||
if (!strcmp(s, "auto")) |
||||
forceauto = true; |
||||
else |
||||
if (!strcmp(s, "noauto")) |
||||
inhibitauto = true; |
||||
else |
||||
if (detectone(s)) { |
||||
string_elist_del(iter); |
||||
inhibitauto = true; |
||||
++found; |
||||
if (total_devices == MAX_DEVICES) |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if ((forceauto || !inhibitauto) && autoscan && total_devices < MAX_DEVICES) |
||||
found += autoscan(); |
||||
|
||||
return found; |
||||
} |
||||
|
||||
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); |
||||
if (unlikely(hSerial == INVALID_HANDLE_VALUE)) |
||||
return -1; |
||||
|
||||
// thanks to af_newbie for pointers about this
|
||||
COMMCONFIG comCfg = {0}; |
||||
comCfg.dwSize = sizeof(COMMCONFIG); |
||||
comCfg.wVersion = 1; |
||||
comCfg.dcb.DCBlength = sizeof(DCB); |
||||
comCfg.dcb.BaudRate = baud; |
||||
comCfg.dcb.fBinary = 1; |
||||
comCfg.dcb.fDtrControl = DTR_CONTROL_ENABLE; |
||||
comCfg.dcb.fRtsControl = RTS_CONTROL_ENABLE; |
||||
comCfg.dcb.ByteSize = 8; |
||||
|
||||
SetCommConfig(hSerial, &comCfg, sizeof(comCfg)); |
||||
|
||||
const DWORD ctoms = (timeout == -1) ? 30000 : (timeout * 100); |
||||
COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms}; |
||||
SetCommTimeouts(hSerial, &cto); |
||||
|
||||
if (purge) { |
||||
PurgeComm(hSerial, PURGE_RXABORT); |
||||
PurgeComm(hSerial, PURGE_TXABORT); |
||||
PurgeComm(hSerial, PURGE_RXCLEAR); |
||||
PurgeComm(hSerial, PURGE_TXCLEAR); |
||||
} |
||||
|
||||
return _open_osfhandle((LONG)hSerial, 0); |
||||
#else |
||||
int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY); |
||||
|
||||
if (unlikely(fdDev == -1)) |
||||
return -1; |
||||
|
||||
struct termios pattr; |
||||
tcgetattr(fdDev, &pattr); |
||||
pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
||||
pattr.c_oflag &= ~OPOST; |
||||
pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); |
||||
pattr.c_cflag &= ~(CSIZE | PARENB); |
||||
pattr.c_cflag |= CS8; |
||||
|
||||
switch (baud) { |
||||
case 0: break; |
||||
case 115200: pattr.c_cflag = B115200; break; |
||||
default: |
||||
applog(LOG_WARNING, "Unrecognized baud rate: %lu", baud); |
||||
} |
||||
pattr.c_cflag |= CREAD | CLOCAL; |
||||
|
||||
if (timeout >= 0) { |
||||
pattr.c_cc[VTIME] = (cc_t)timeout; |
||||
pattr.c_cc[VMIN] = 0; |
||||
} |
||||
|
||||
tcsetattr(fdDev, TCSANOW, &pattr); |
||||
if (purge) |
||||
tcflush(fdDev, TCIOFLUSH); |
||||
return fdDev; |
||||
#endif |
||||
} |
||||
|
||||
ssize_t |
||||
_serial_read(int fd, char *buf, size_t bufsiz, char *eol) |
||||
{ |
||||
ssize_t len, tlen = 0; |
||||
while (bufsiz) { |
||||
len = read(fd, buf, eol ? 1 : bufsiz); |
||||
if (len < 1) |
||||
break; |
||||
tlen += len; |
||||
if (eol && *eol == buf[0]) |
||||
break; |
||||
buf += len; |
||||
bufsiz -= len; |
||||
} |
||||
return tlen; |
||||
} |
||||
|
||||
static FILE* |
||||
_open_bitstream(const char*path, const char*subdir, const char*filename) |
||||
{ |
||||
char fullpath[PATH_MAX]; |
||||
strcpy(fullpath, path); |
||||
strcat(fullpath, "/"); |
||||
if (subdir) { |
||||
strcat(fullpath, subdir); |
||||
strcat(fullpath, "/"); |
||||
} |
||||
strcat(fullpath, filename); |
||||
return fopen(fullpath, "rb"); |
||||
} |
||||
#define _open_bitstream(path, subdir) do { \ |
||||
f = _open_bitstream(path, subdir, filename); \ |
||||
if (f) \ |
||||
return f; \ |
||||
} while(0) |
||||
|
||||
#define _open_bitstream3(path) do { \ |
||||
_open_bitstream(path, dname); \ |
||||
_open_bitstream(path, "bitstreams"); \ |
||||
_open_bitstream(path, NULL); \ |
||||
} while(0) |
||||
|
||||
FILE* |
||||
open_bitstream(const char*dname, const char*filename) |
||||
{ |
||||
FILE *f; |
||||
|
||||
_open_bitstream3(opt_kernel_path); |
||||
_open_bitstream3(cgminer_path); |
||||
_open_bitstream3("."); |
||||
|
||||
return NULL; |
||||
} |
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012 Luke Dashjr |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify it |
||||
* under the terms of the GNU General Public License as published by the Free |
||||
* Software Foundation; either version 3 of the License, or (at your option) |
||||
* any later version. See COPYING for more details. |
||||
*/ |
||||
|
||||
#ifndef FPGAUTILS_H |
||||
#define FPGAUTILS_H |
||||
|
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
|
||||
typedef bool(*detectone_func_t)(const char*); |
||||
typedef char(*autoscan_func_t)(); |
||||
|
||||
extern char _serial_detect(const char*dnamec, size_t dnamel, detectone_func_t, autoscan_func_t, bool force_autoscan); |
||||
#define serial_detect_fauto(dname, detectone, autoscan) \ |
||||
_serial_detect(dname ":", sizeof(dname)+1, detectone, autoscan, true) |
||||
#define serial_detect_auto(dname, detectone, autoscan) \ |
||||
_serial_detect(dname ":", sizeof(dname)+1, detectone, autoscan, false) |
||||
#define serial_detect(dname, detectone) \ |
||||
_serial_detect(dname ":", sizeof(dname)+1, detectone, NULL, false) |
||||
extern char serial_autodetect_devserial(detectone_func_t, const char*prodname); |
||||
extern char 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); |
||||
#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) |
||||
#define serial_close(fd) close(fd) |
||||
|
||||
extern FILE*open_bitstream(const char*dname, const char*filename); |
||||
|
||||
#endif |
Loading…
Reference in new issue