Browse Source

Merge branch 'usbdev'

Conflicts:
	cgminer.c
nfactor-troky
Con Kolivas 12 years ago
parent
commit
181ada4e5a
  1. 17
      01-cgminer.rules
  2. 58
      ASIC-README
  3. 29
      FPGA-README
  4. 2
      Makefile.am
  5. 61
      README
  6. 11
      adl.c
  7. 127
      cgminer.c
  8. 39
      configure.ac
  9. 1090
      driver-avalon.c
  10. 39
      driver-avalon.h
  11. 74
      driver-bflsc.c
  12. 16
      driver-bitforce.c
  13. 651
      driver-icarus.c
  14. 2
      driver-opencl.c
  15. 2
      driver-ztex.c
  16. 15
      miner.h
  17. 86
      usbtest.py
  18. 1311
      usbutils.c
  19. 152
      usbutils.h
  20. 23
      util.c
  21. 1
      util.h

17
01-cgminer.rules

@ -1,3 +1,20 @@
# Butterfly Labs FPGA and ASIC devices # Butterfly Labs FPGA and ASIC devices
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev" ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# ModMinerQuad
ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev" ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# Lancelot and Avalon
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# Icarus
ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# AsicminerUSB
ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# Cairnsmore1
ATTRS{idVendor}=="067b", ATTRS{idProduct}=="0230", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
# Ztex
ATTRS{idVendor}=="221a", ATTRS{idProduct}=="0100", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"

58
ASIC-README

@ -44,17 +44,12 @@ If your distribution does not have the plugdev group you can create it with:
sudo groupadd plugdev sudo groupadd plugdev
In order for the BFL devices to instantly be owned by the plugdev group and In order for the BFL devices to instantly be owned by the plugdev group and
accessible by anyone from the plugdev group you can either copy the file accessible by anyone from the plugdev group you can copy the file
"01-cgminer.rules" from the cgminer archive into the /etc/udev/rules.d "01-cgminer.rules" from the cgminer archive into the /etc/udev/rules.d
directory with the following command: directory with the following command:
sudo cp 01-cgminer.rules /etc/udev/rules.d/ sudo cp 01-cgminer.rules /etc/udev/rules.d/
Or you can manually create a file/add to a rules.d file with following rules
(most users won't want to do this manually):
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
After this you can either manually restart udev and re-login, or more easily After this you can either manually restart udev and re-login, or more easily
just reboot. just reboot.
@ -64,11 +59,52 @@ AVALON DEVICES
Currently all known Avalon devices come with their own operating system and Currently all known Avalon devices come with their own operating system and
a preinstalled version of cgminer as part of the flash firmware, based on the a preinstalled version of cgminer as part of the flash firmware, based on the
most current cgminer version so no configuration should be necessary. It is most current cgminer version so no configuration should be necessary. It is
possible to plug a USB cable from a PC into the Avalon device and use the possible to plug a USB cable from a PC into the Avalon device and mine using
--avalon-options copying the command as used by the internal router used by the cgminer as per any other device. It will autodetect and hotplug using default
Avalon. However since the Avalon code still currently uses the old serial usb options. You can customise the avalon behaviour by using the --avalon-options
interface and is being rewritten to use direct USB, it is prudent to not be command.
dependent on this command long term, assuming it will go away.
eg:
--avalon-options 115200:24:10:45:282
The values are baud : miners : asic count : timeout : frequency.
Baud:
The device is pretty much hard coded to emulate 115200 baud so you shouldn't
change this.
Miners:
Most Avalons are 3 module devices, which come to 24 miners. 4 module devices
would use 32 here.
Asic count:
Virtually all have 10, so don't change this.
Timeout:
This is how long the device will work on a work item before accepting new work
to replace it. It should be changed according to the frequency (last setting).
It is possible to set this a little lower if you are trying to tune for short
block mining (eg p2pool) but much lower and the device will start creating
duplicate shares.
Sample settings for different frequencies (last 2 values):
43:300
45:282 (default)
47:270
50:256
Frequency:
This is the clock speed of the devices. Only specific values work, 256, 270,
282 (default) and 300.
If you use the full curses based interface with Avalons you will get this
information:
AVA 0: 22/ 46C 60%/2400R
The values are:
ambient temp / highest device temp set fan % / lowest detected fan RPM.
Use the API for more detailed information than this.
--- ---

29
FPGA-README

@ -2,16 +2,14 @@
This README contains extended details about FPGA mining with cgminer This README contains extended details about FPGA mining with cgminer
For ModMinerQuad (MMQ) and BitForce (BFL) For ModMinerQuad (MMQ) BitForce (BFL) and Icarus (ICA, BLT, LLT, AMU, CMR)
----------------------------------------- --------------------------------------------------------------------------
When mining on windows, the driver being used will determine if mining will work. When mining on windows, the driver being used will determine if mining will work.
If the driver doesn't allow mining, you will get a "USB init," error message If the driver doesn't allow mining, you will get a "USB init," error message
i.e. one of: i.e. one of:
open device failed, err %d, you need to install a Windows USB driver for the device open device failed, err %d, you need to install a Windows USB driver for the device
or
kernel detach failed :(
or or
claim interface %d failed, err %d claim interface %d failed, err %d
@ -32,19 +30,16 @@ with cgminer (not the libusbx version)
When mining on linux, but not using 'sudo' and not logged into 'root' you When mining on linux, but not using 'sudo' and not logged into 'root' you
may get a USB priviledge error (-3), so you may also need to do the following: may get a USB priviledge error (-3), so you may also need to do the following:
Create /etc/udev/rules.d/01-cgminer.rules sudo cp 01-cgminer.rules /etc/udev/rules.d/
With:
ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
ATTRS{idVendor}=="1fc9", ATTRS{idProduct}=="0003", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"
And also: And also:
sudo usermod -G plugdev -a `whoami` sudo usermod -G plugdev -a `whoami`
Then reboot ...
If your linux distro doesn't have the 'plugdev' group, you can create it like: If your linux distro doesn't have the 'plugdev' group, you can create it like:
sudo groupadd plugdev sudo groupadd plugdev
Then reboot ...
- -
There is a hidden option in cgminer to dump out a lot of information There is a hidden option in cgminer to dump out a lot of information
@ -53,8 +48,7 @@ problems:
--usb-dump 0 --usb-dump 0
It will only help if you have a working MMQ or BFL device attached to the It will only help if you have a working FPGA device listed above
computer
ModMinerQuad (MMQ) ModMinerQuad (MMQ)
@ -190,8 +184,8 @@ the MH/s value reported with the changed firmware - and the MH/s reported
will be less than the firmware speed since you lose work on every block change. will be less than the firmware speed since you lose work on every block change.
Icarus (ICA) Icarus (ICA, BLT, LLT, AMU, CMR)
------------ --------------------------------
There are two hidden options in cgminer when Icarus support is compiled in: There are two hidden options in cgminer when Icarus support is compiled in:
@ -245,8 +239,8 @@ scan hash time, for the first 5 nonce's or one minute (whichever is longer)
In 'default' or 'value' mode the 'constants' are calculated once at the start, based on the default In 'default' or 'value' mode the 'constants' are calculated once at the start, based on the default
value or the value specified value or the value specified
The optional additional =N specifies to set the default abort at N 1/10ths of a second, not the The optional additional =N specifies to set the default abort at N * 100ms, not the calculated
calculated value, which is 112 for 2.6316ns value, which is ~112 for 2.6316ns
To determine the hash time value for a non Icarus Rev3 device or an Icarus Rev3 with a different To determine the hash time value for a non Icarus Rev3 device or an Icarus Rev3 with a different
bitstream to the default one, use 'long' mode and give it at least a few hundred shares, or use bitstream to the default one, use 'long' mode and give it at least a few hundred shares, or use
@ -258,6 +252,9 @@ Icarus Rev3 requires and also is less than ~840MH/s and greater than 2MH/s
If an FPGA device does hash faster than ~840MH/s it should work correctly if you supply the If an FPGA device does hash faster than ~840MH/s it should work correctly if you supply the
correct hash time nanoseconds value correct hash time nanoseconds value
The Icarus code will automatically detect Icarus, Lancelot, AsicminerUSB and Cairnsmore1
FPGA devices and set default settings to match those devices if you don't specify them
The timing code itself will affect the Icarus performance since it increases the delay after The timing code itself will affect the Icarus performance since it increases the delay after
work is completed or aborted until it starts again work is completed or aborted until it starts again
The increase is, however, extremely small and the actual increase is reported with the The increase is, however, extremely small and the actual increase is reported with the

2
Makefile.am

@ -25,7 +25,7 @@ bin_SCRIPTS = $(top_srcdir)/*.cl
cgminer_LDFLAGS = $(PTHREAD_FLAGS) cgminer_LDFLAGS = $(PTHREAD_FLAGS)
cgminer_LDADD = $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \ cgminer_LDADD = $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
@OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \ @OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
@UDEV_LIBS@ @LIBUSB_LIBS@ @MM_LIBS@ \ @LIBUSB_LIBS@ @MM_LIBS@ \
@MATH_LIBS@ lib/libgnu.a ccan/libccan.a @MATH_LIBS@ lib/libgnu.a ccan/libccan.a
if HAVE_WINDOWS if HAVE_WINDOWS

61
README

@ -111,7 +111,7 @@ CGMiner specific configuration options:
--disable-adl Override detection and disable building with adl --disable-adl Override detection and disable building with adl
--enable-bflsc Compile support for BFL ASICs (default disabled) --enable-bflsc Compile support for BFL ASICs (default disabled)
--enable-bitforce Compile support for BitForce FPGAs(default disabled) --enable-bitforce Compile support for BitForce FPGAs(default disabled)
--enable-icarus Compile support for Icarus Board(default disabled) --enable-icarus Compile support for Icarus bitstream FPGAs(default disabled)
--enable-modminer Compile support for ModMiner FPGAs(default disabled) --enable-modminer Compile support for ModMiner FPGAs(default disabled)
--enable-ztex Compile support for Ztex Board(default disabled) --enable-ztex Compile support for Ztex Board(default disabled)
--enable-avalon Compile support for Avalon (default disabled) --enable-avalon Compile support for Avalon (default disabled)
@ -235,13 +235,14 @@ ASIC and FPGA mining boards (BFL ASIC, BitForce, Icarus, ModMiner, Ztex)
only options: only options:
Cgminer will automatically find all of your BFL ASIC, BitForce FPGAs, Cgminer will automatically find all of your BFL ASIC, BitForce FPGAs,
ModMiner FPGAs or Ztex FPGAs ModMiner FPGAs, Icarus bitstream FPGAs or Ztex FPGAs
The --usb option can restrict how many BFL ASIC, BitForce FPGAs or
ModMiner FPGAs it finds: The --usb option can restrict how many BFL ASIC, BitForce FPGAs,
ModMiner FPGAs or Icarus bitstream FPGAs it finds:
--usb 1:2,1:3,1:4,1:* --usb 1:2,1:3,1:4,1:*
or or
--usb BAS:1,BFL:1,MMQ:0 --usb BAS:1,BFL:1,MMQ:0,ICA:0
or or
--usb :10 --usb :10
@ -260,15 +261,16 @@ This is useful if you unplug a device then plug it back in the same port,
it usually reappears with the same bus_number but a different device_address it usually reappears with the same bus_number but a different device_address
You can see the list of all USB devices on linux with 'sudo lsusb' You can see the list of all USB devices on linux with 'sudo lsusb'
Cgminer will list the recognised USB devices with the '--usb-dump 0' option Cgminer will list the recognised USB devices with the '-n' option or the
'--usb-dump 0' option
The '--usb-dump N' option with a value of N greater than 0 will dump a lot The '--usb-dump N' option with a value of N greater than 0 will dump a lot
of details about each recognised USB device of details about each recognised USB device
If you wish to see all USB devices, include the --usb-list-all option If you wish to see all USB devices, include the --usb-list-all option
The second version The second version
--usb BAS:1,BFL:1,MMQ:0 --usb BAS:1,BFL:1,MMQ:0,ICA:0
allows you to specify how many devices to choose based on each device allows you to specify how many devices to choose based on each device
driver cgminer has - there are currently 3 USB drivers: BAS, BFL & MMQ driver cgminer has - there are currently 4 USB drivers: BAS, BFL, MMQ & ICA
N.B. you can only specify which device driver to limit, not the type of N.B. you can only specify which device driver to limit, not the type of
each device, e.g. with BAS:n you can limit how many BFL ASIC devices will each device, e.g. with BAS:n you can limit how many BFL ASIC devices will
be checked, but you cannot limit the number of each type of BFL ASIC be checked, but you cannot limit the number of each type of BFL ASIC
@ -283,28 +285,8 @@ not scan for any more
If one of the 10 devices stops working, hotplug - if enabled, as is default If one of the 10 devices stops working, hotplug - if enabled, as is default
- will scan normally again until it has 10 devices - will scan normally again until it has 10 devices
--usb :0 will disable all USB I/O other than to initialise libusb
--scan-serial|-S <arg> Serial port to probe for Icarus mining device
This option is only for Icarus bitstream FPGAs
By default, cgminer will scan for autodetected Icarus unless at least one
-S is specified for that driver. If you specify -S and still want cgminer
to scan, you must also use "-S auto". If you want to prevent cgminer from
scanning without specifying a device, you can use "-S noauto". Note that
presently, autodetection only works on Linux, and might only detect one
device depending on the version of udev being used.
On linux <arg> is usually of the format /dev/ttyUSBn
On windows <arg> is usually of the format \\.\COMn
(where n = the correct device number for the Icarus device)
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 using the short name: "ica:/dev/ttyUSB0"
This option not longer matters since Icarus is the only serial-USB
device that uses it
For other FPGA details see the FPGA-README For other FPGA details see the FPGA-README
@ -657,27 +639,6 @@ A: Cgminer currently supports 2 ASICs: Avalon and BitForce SC devices. They
are Application Specify Integrated Circuit devices and provide the highest are Application Specify Integrated Circuit devices and provide the highest
performance per unit power due to being dedicated to only one purpose. performance per unit power due to being dedicated to only one purpose.
Q: How do I get my Icarus/Lancelot/Cairnsmore device to auto-recognise?
A: On linux, if the /dev/ttyUSB* devices don't automatically appear, the only
thing that needs to be done is to load the driver for them:
Icarus: sudo modprobe pl2303 vendor=0x067b product=0x230
Lancelot: sudo modprobe ftdi_sio vendor=0x0403 product=0x6001
Cairnsmore: sudo modprobe ftdi_sio product=0x8350 vendor=0x0403
On windows you must install the pl2303 or ftdi driver required for the device
pl2303: http://prolificusa.com/pl-2303hx-drivers/
ftdi: http://www.ftdichip.com/Drivers/VCP.htm
Q: On linux I can see the /dev/ttyUSB* devices for my Icarus FPGAs, but
cgminer can't mine on them
A: Make sure you have the required priviledges to access the /dev/ttyUSB* devices:
sudo ls -las /dev/ttyUSB*
will give output like:
0 crw-rw---- 1 root dialout 188, 0 2012-09-11 13:49 /dev/ttyUSB0
This means your account must have the group 'dialout' or root priviledges
To permanently give your account the 'dialout' group:
sudo usermod -G dialout -a `whoami`
Then logout and back in again
Q: Can I mine scrypt with FPGAs or ASICs? Q: Can I mine scrypt with FPGAs or ASICs?
A: No. A: No.

11
adl.c

@ -493,9 +493,16 @@ void init_adl(int nDevs)
if (!gpus[gpu].cutofftemp) if (!gpus[gpu].cutofftemp)
gpus[gpu].cutofftemp = opt_cutofftemp; gpus[gpu].cutofftemp = opt_cutofftemp;
if (opt_autofan) { if (opt_autofan) {
ga->autofan = true;
/* Set a safe starting default if we're automanaging fan speeds */ /* Set a safe starting default if we're automanaging fan speeds */
set_fanspeed(gpu, 50); int nominal = 50;
ga->autofan = true;
/* Clamp fanspeed values to range provided */
if (nominal > gpus[gpu].gpu_fan)
nominal = gpus[gpu].gpu_fan;
if (nominal < gpus[gpu].min_fan)
nominal = gpus[gpu].min_fan;
set_fanspeed(gpu, nominal);
} }
if (opt_autoengine) { if (opt_autoengine) {
ga->autoengine = true; ga->autoengine = true;

127
cgminer.c

@ -28,6 +28,10 @@
#include <assert.h> #include <assert.h>
#include <signal.h> #include <signal.h>
#ifdef USE_USBUTILS
#include <semaphore.h>
#endif
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -60,9 +64,6 @@
#if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_MODMINER) #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_MODMINER)
# define USE_FPGA # define USE_FPGA
#if defined(USE_ICARUS) || defined(USE_AVALON)
# define USE_FPGA_SERIAL
#endif
#elif defined(USE_ZTEX) #elif defined(USE_ZTEX)
# define USE_FPGA # define USE_FPGA
#endif #endif
@ -147,6 +148,7 @@ char *opt_avalon_options = NULL;
char *opt_usb_select = NULL; char *opt_usb_select = NULL;
int opt_usbdump = -1; int opt_usbdump = -1;
bool opt_usb_list_all; bool opt_usb_list_all;
sem_t usb_resource_sem;
#endif #endif
char *opt_kernel_path; char *opt_kernel_path;
@ -169,6 +171,7 @@ static int input_thr_id;
int gpur_thr_id; int gpur_thr_id;
static int api_thr_id; static int api_thr_id;
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
static int usbres_thr_id;
static int hotplug_thr_id; static int hotplug_thr_id;
#endif #endif
static int total_control_threads; static int total_control_threads;
@ -180,6 +183,7 @@ int hotplug_time = 5;
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
pthread_mutex_t cgusb_lock; pthread_mutex_t cgusb_lock;
pthread_mutex_t cgusbres_lock;
#endif #endif
pthread_mutex_t hash_lock; pthread_mutex_t hash_lock;
@ -1089,7 +1093,7 @@ static struct opt_table opt_config_table[] = {
#ifdef USE_FPGA_SERIAL #ifdef USE_FPGA_SERIAL
OPT_WITH_ARG("--scan-serial|-S", OPT_WITH_ARG("--scan-serial|-S",
add_serial, NULL, NULL, add_serial, NULL, NULL,
"Serial port to probe for Icarus FPGA Mining device"), "Serial port to probe for Serial FPGA Mining device"),
#endif #endif
OPT_WITH_ARG("--scan-time|-s", OPT_WITH_ARG("--scan-time|-s",
set_int_0_to_9999, opt_show_intval, &opt_scantime, set_int_0_to_9999, opt_show_intval, &opt_scantime,
@ -2221,6 +2225,7 @@ void logwin_update(void)
if (curses_active_locked()) { if (curses_active_locked()) {
touchwin(logwin); touchwin(logwin);
wrefresh(logwin); wrefresh(logwin);
unlock_curses();
} }
} }
#endif #endif
@ -2809,6 +2814,22 @@ static void __kill_work(void)
thr = &control_thr[watchdog_thr_id]; thr = &control_thr[watchdog_thr_id];
thr_info_cancel(thr); thr_info_cancel(thr);
applog(LOG_DEBUG, "Shutting down mining threads");
for (i = 0; i < mining_threads; i++) {
struct cgpu_info *cgpu;
thr = get_thread(i);
if (!thr)
continue;
cgpu = thr->cgpu;
if (!cgpu)
continue;
cgpu->shutdown = true;
}
sleep(1);
applog(LOG_DEBUG, "Killing off mining threads"); applog(LOG_DEBUG, "Killing off mining threads");
/* Kill the mining threads*/ /* Kill the mining threads*/
for (i = 0; i < mining_threads; i++) { for (i = 0; i < mining_threads; i++) {
@ -2818,8 +2839,13 @@ static void __kill_work(void)
if (thr && PTH(thr) != 0L) if (thr && PTH(thr) != 0L)
pth = &thr->pth; pth = &thr->pth;
thr_info_cancel(thr); thr_info_cancel(thr);
if (pth) #ifndef WIN32
if (pth && *pth)
pthread_join(*pth, NULL);
#else
if (pth && pth->p)
pthread_join(*pth, NULL); pthread_join(*pth, NULL);
#endif
} }
applog(LOG_DEBUG, "Killing off stage thread"); applog(LOG_DEBUG, "Killing off stage thread");
@ -2837,6 +2863,10 @@ static void __kill_work(void)
if (!opt_scrypt) { if (!opt_scrypt) {
applog(LOG_DEBUG, "Releasing all USB devices"); applog(LOG_DEBUG, "Releasing all USB devices");
usb_cleanup(); usb_cleanup();
applog(LOG_DEBUG, "Killing off usbres thread");
thr = &control_thr[usbres_thr_id];
thr_info_cancel(thr);
} }
#endif #endif
@ -5505,7 +5535,7 @@ static struct work *get_work(struct thr_info *thr, const int thr_id)
return work; return work;
} }
void submit_work_async(struct work *work_in, struct timeval *tv_work_found) static void submit_work_async(struct work *work_in, struct timeval *tv_work_found)
{ {
struct work *work = copy_work(work_in); struct work *work = copy_work(work_in);
struct pool *pool = work->pool; struct pool *pool = work->pool;
@ -5559,13 +5589,17 @@ void inc_hw_errors(struct thr_info *thr)
thr->cgpu->drv->hw_error(thr); thr->cgpu->drv->hw_error(thr);
} }
void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce) /* Returns true if nonce for work was a valid share */
bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
{ {
uint32_t *work_nonce = (uint32_t *)(work->data + 64 + 12); uint32_t *work_nonce = (uint32_t *)(work->data + 64 + 12);
struct timeval tv_work_found; struct timeval tv_work_found;
unsigned char hash2[32]; unsigned char hash2[32];
uint32_t *hash2_32 = (uint32_t *)hash2; uint32_t *hash2_32 = (uint32_t *)hash2;
uint32_t diff1targ; uint32_t diff1targ;
bool ret = true;
thread_reportout(thr);
cgtime(&tv_work_found); cgtime(&tv_work_found);
*work_nonce = htole32(nonce); *work_nonce = htole32(nonce);
@ -5586,7 +5620,8 @@ void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
thr->cgpu->drv->name, thr->cgpu->device_id); thr->cgpu->drv->name, thr->cgpu->device_id);
inc_hw_errors(thr); inc_hw_errors(thr);
return; ret = false;
goto out;
} }
mutex_lock(&stats_lock); mutex_lock(&stats_lock);
@ -5595,10 +5630,14 @@ void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
if (!fulltest(hash2, work->target)) { if (!fulltest(hash2, work->target)) {
applog(LOG_INFO, "Share below target"); applog(LOG_INFO, "Share below target");
return; goto out;
} }
submit_work_async(work, &tv_work_found); submit_work_async(work, &tv_work_found);
out:
thread_reportin(thr);
return ret;
} }
static inline bool abandon_work(struct work *work, struct timeval *wdiff, uint64_t hashes) static inline bool abandon_work(struct work *work, struct timeval *wdiff, uint64_t hashes)
@ -5649,7 +5688,7 @@ static void hash_sole_work(struct thr_info *mythr)
sdiff.tv_sec = sdiff.tv_usec = 0; sdiff.tv_sec = sdiff.tv_usec = 0;
cgtime(&tv_lastupdate); cgtime(&tv_lastupdate);
while (42) { while (likely(!cgpu->shutdown)) {
struct work *work = get_work(mythr, thr_id); struct work *work = get_work(mythr, thr_id);
int64_t hashes; int64_t hashes;
@ -5788,6 +5827,7 @@ static void hash_sole_work(struct thr_info *mythr)
} while (!abandon_work(work, &wdiff, cgpu->max_hashes)); } while (!abandon_work(work, &wdiff, cgpu->max_hashes));
free_work(work); free_work(work);
} }
cgpu->deven = DEV_DISABLED;
} }
/* Create a hashtable of work items for devices with a queue. The device /* Create a hashtable of work items for devices with a queue. The device
@ -5925,7 +5965,7 @@ void hash_queued_work(struct thr_info *mythr)
const int thr_id = mythr->id; const int thr_id = mythr->id;
int64_t hashes_done = 0; int64_t hashes_done = 0;
while (42) { while (likely(!cgpu->shutdown)) {
struct timeval diff; struct timeval diff;
int64_t hashes; int64_t hashes;
@ -5960,6 +6000,7 @@ void hash_queued_work(struct thr_info *mythr)
drv->flush_work(cgpu); drv->flush_work(cgpu);
} }
} }
cgpu->deven = DEV_DISABLED;
} }
void *miner_thread(void *userdata) void *miner_thread(void *userdata)
@ -7111,6 +7152,10 @@ static void *hotplug_thread(void __maybe_unused *userdata)
new_devices = 0; new_devices = 0;
new_threads = 0; new_threads = 0;
#ifdef USE_ICARUS
icarus_drv.drv_detect();
#endif
#ifdef USE_BFLSC #ifdef USE_BFLSC
bflsc_drv.drv_detect(); bflsc_drv.drv_detect();
#endif #endif
@ -7123,6 +7168,10 @@ static void *hotplug_thread(void __maybe_unused *userdata)
modminer_drv.drv_detect(); modminer_drv.drv_detect();
#endif #endif
#ifdef USE_AVALON
avalon_drv.drv_detect();
#endif
if (new_devices) if (new_devices)
hotplug_process(); hotplug_process();
@ -7175,6 +7224,7 @@ int main(int argc, char *argv[])
} }
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
mutex_init(&cgusb_lock); mutex_init(&cgusb_lock);
mutex_init(&cgusbres_lock);
#endif #endif
#endif #endif
@ -7301,8 +7351,27 @@ int main(int argc, char *argv[])
/* Use a shorter scantime for scrypt */ /* Use a shorter scantime for scrypt */
if (opt_scantime < 0) if (opt_scantime < 0)
opt_scantime = opt_scrypt ? 30 : 60; opt_scantime = opt_scrypt ? 30 : 60;
total_control_threads = 9;
control_thr = calloc(total_control_threads, sizeof(*thr));
if (!control_thr)
quit(1, "Failed to calloc control_thr");
gwsched_thr_id = 0;
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
usb_initialise(); usb_initialise();
// before device detection
if (!opt_scrypt) {
if (sem_init(&usb_resource_sem, 0, 0))
quit(1, "Failed to sem_init usb_resource_sem");
usbres_thr_id = 1;
thr = &control_thr[usbres_thr_id];
if (thr_info_create(thr, NULL, usb_resource_thread, thr))
quit(1, "usb resource thread create failed");
pthread_detach(thr->pth);
}
#endif #endif
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL
@ -7316,11 +7385,6 @@ int main(int argc, char *argv[])
icarus_drv.drv_detect(); icarus_drv.drv_detect();
#endif #endif
#ifdef USE_AVALON
if (!opt_scrypt)
avalon_drv.drv_detect();
#endif
#ifdef USE_BFLSC #ifdef USE_BFLSC
if (!opt_scrypt) if (!opt_scrypt)
bflsc_drv.drv_detect(); bflsc_drv.drv_detect();
@ -7341,6 +7405,13 @@ int main(int argc, char *argv[])
ztex_drv.drv_detect(); ztex_drv.drv_detect();
#endif #endif
/* Detect avalon last since it will try to claim the device regardless
* as detection is unreliable. */
#ifdef USE_AVALON
if (!opt_scrypt)
avalon_drv.drv_detect();
#endif
if (devices_enabled == -1) { if (devices_enabled == -1) {
applog(LOG_ERR, "Devices detected:"); applog(LOG_ERR, "Devices detected:");
for (i = 0; i < total_devices; ++i) { for (i = 0; i < total_devices; ++i) {
@ -7442,13 +7513,7 @@ int main(int argc, char *argv[])
quit(1, "Failed to calloc mining_thr[%d]", i); quit(1, "Failed to calloc mining_thr[%d]", i);
} }
total_control_threads = 8; stage_thr_id = 2;
control_thr = calloc(total_control_threads, sizeof(*thr));
if (!control_thr)
quit(1, "Failed to calloc control_thr");
gwsched_thr_id = 0;
stage_thr_id = 1;
thr = &control_thr[stage_thr_id]; thr = &control_thr[stage_thr_id];
thr->q = tq_new(); thr->q = tq_new();
if (!thr->q) if (!thr->q)
@ -7569,14 +7634,14 @@ begin_bench:
cgtime(&total_tv_start); cgtime(&total_tv_start);
cgtime(&total_tv_end); cgtime(&total_tv_end);
watchpool_thr_id = 2; watchpool_thr_id = 3;
thr = &control_thr[watchpool_thr_id]; thr = &control_thr[watchpool_thr_id];
/* start watchpool thread */ /* start watchpool thread */
if (thr_info_create(thr, NULL, watchpool_thread, NULL)) if (thr_info_create(thr, NULL, watchpool_thread, NULL))
quit(1, "watchpool thread create failed"); quit(1, "watchpool thread create failed");
pthread_detach(thr->pth); pthread_detach(thr->pth);
watchdog_thr_id = 3; watchdog_thr_id = 4;
thr = &control_thr[watchdog_thr_id]; thr = &control_thr[watchdog_thr_id];
/* start watchdog thread */ /* start watchdog thread */
if (thr_info_create(thr, NULL, watchdog_thread, NULL)) if (thr_info_create(thr, NULL, watchdog_thread, NULL))
@ -7585,7 +7650,7 @@ begin_bench:
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL
/* Create reinit gpu thread */ /* Create reinit gpu thread */
gpur_thr_id = 4; gpur_thr_id = 5;
thr = &control_thr[gpur_thr_id]; thr = &control_thr[gpur_thr_id];
thr->q = tq_new(); thr->q = tq_new();
if (!thr->q) if (!thr->q)
@ -7595,14 +7660,14 @@ begin_bench:
#endif #endif
/* Create API socket thread */ /* Create API socket thread */
api_thr_id = 5; api_thr_id = 6;
thr = &control_thr[api_thr_id]; thr = &control_thr[api_thr_id];
if (thr_info_create(thr, NULL, api_thread, thr)) if (thr_info_create(thr, NULL, api_thread, thr))
quit(1, "API thread create failed"); quit(1, "API thread create failed");
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
if (!opt_scrypt) { if (!opt_scrypt) {
hotplug_thr_id = 6; hotplug_thr_id = 7;
thr = &control_thr[hotplug_thr_id]; thr = &control_thr[hotplug_thr_id];
if (thr_info_create(thr, NULL, hotplug_thread, thr)) if (thr_info_create(thr, NULL, hotplug_thread, thr))
quit(1, "hotplug thread create failed"); quit(1, "hotplug thread create failed");
@ -7614,7 +7679,7 @@ begin_bench:
/* Create curses input thread for keyboard input. Create this last so /* Create curses input thread for keyboard input. Create this last so
* that we know all threads are created since this can call kill_work * that we know all threads are created since this can call kill_work
* to try and shut down all previous threads. */ * to try and shut down all previous threads. */
input_thr_id = 7; input_thr_id = 8;
thr = &control_thr[input_thr_id]; thr = &control_thr[input_thr_id];
if (thr_info_create(thr, NULL, input_thread, thr)) if (thr_info_create(thr, NULL, input_thread, thr))
quit(1, "input thread create failed"); quit(1, "input thread create failed");
@ -7622,8 +7687,8 @@ begin_bench:
#endif #endif
/* Just to be sure */ /* Just to be sure */
if (total_control_threads != 8) if (total_control_threads != 9)
quit(1, "incorrect total_control_threads (%d) should be 8", total_control_threads); quit(1, "incorrect total_control_threads (%d) should be 9", total_control_threads);
/* Once everything is set up, main() becomes the getwork scheduler */ /* Once everything is set up, main() becomes the getwork scheduler */
while (42) { while (42) {

39
configure.ac

@ -1,8 +1,8 @@
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--## ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--## ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
m4_define([v_maj], [3]) m4_define([v_maj], [3])
m4_define([v_min], [1]) m4_define([v_min], [2])
m4_define([v_mic], [1]) m4_define([v_mic], [0])
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--## ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
m4_define([v_ver], [v_maj.v_min.v_mic]) m4_define([v_ver], [v_maj.v_min.v_mic])
m4_define([lt_rev], m4_eval(v_maj + v_min)) m4_define([lt_rev], m4_eval(v_maj + v_min))
@ -303,14 +303,14 @@ else
]) ])
fi fi
AM_CONDITIONAL([NEED_FPGAUTILS], [test x$avalon$icarus$bitforce$modminer$ztex != xnonononono]) AM_CONDITIONAL([NEED_FPGAUTILS], [test x$modminer$ztex != xnono])
AM_CONDITIONAL([NEED_USBUTILS_C], [test x$bitforce$modminer$bflsc != xnonono]) AM_CONDITIONAL([NEED_USBUTILS_C], [test x$avalon$bitforce$modminer$bflsc$icarus != xnonononono])
AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes]) AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes])
AM_CONDITIONAL([WANT_JANSSON], [test x$request_jansson = xtrue]) AM_CONDITIONAL([WANT_JANSSON], [test x$request_jansson = xtrue])
AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue]) AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue]) AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue])
if test "x$bitforce$modminer$bflsc" != xnonono; then if test "x$avalon$bitforce$modminer$bflsc$icarus" != xnonononono; then
AC_DEFINE([USE_USBUTILS], [1], [Defined to 1 if usbutils support required]) AC_DEFINE([USE_USBUTILS], [1], [Defined to 1 if usbutils support required])
fi fi
@ -321,26 +321,6 @@ else
JANSSON_LIBS=-ljansson JANSSON_LIBS=-ljansson
fi fi
if test "x$icarus" != xno; then
AC_ARG_WITH([libudev], [AC_HELP_STRING([--without-libudev], [Autodetect FPGAs using libudev (default enabled)])],
[libudev=$withval],
[libudev=auto]
)
if test "x$libudev" != "xno"; then
AC_CHECK_HEADER([libudev.h],[
libudev=yes
UDEV_LIBS=-ludev
AC_DEFINE([HAVE_LIBUDEV], [1], [Defined to 1 if libudev is wanted])
], [
if test "x$libudev" = "xyes"; then
AC_MSG_ERROR([libudev not found])
fi
libudev=no
])
fi
fi
AM_CONDITIONAL([HAVE_LIBUDEV], [test x$libudev != xno])
PKG_PROG_PKG_CONFIG() PKG_PROG_PKG_CONFIG()
if test "x$have_cgminer_sdk" = "xtrue"; then if test "x$have_cgminer_sdk" = "xtrue"; then
@ -353,7 +333,7 @@ if test "x$have_cgminer_sdk" = "xtrue"; then
PKG_CONFIG_PATH="$CGMINER_SDK/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" PKG_CONFIG_PATH="$CGMINER_SDK/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
fi fi
if test "x$ztex$modminer$bitforce$bflsc" != xnononono; then if test "x$avalon$ztex$modminer$bitforce$bflsc$icarus" != xnononononono; then
case $target in case $target in
*-*-freebsd*) *-*-freebsd*)
LIBUSB_LIBS="-lusb" LIBUSB_LIBS="-lusb"
@ -441,7 +421,6 @@ AC_SUBST(PDCURSES_LIBS)
AC_SUBST(WS2_LIBS) AC_SUBST(WS2_LIBS)
AC_SUBST(MM_LIBS) AC_SUBST(MM_LIBS)
AC_SUBST(MATH_LIBS) AC_SUBST(MATH_LIBS)
AC_SUBST(UDEV_LIBS)
AC_SUBST(ADL_CPPFLAGS) AC_SUBST(ADL_CPPFLAGS)
AC_CONFIG_FILES([ AC_CONFIG_FILES([
@ -538,16 +517,12 @@ else
echo " Ztex.FPGAs...........: Disabled" echo " Ztex.FPGAs...........: Disabled"
fi fi
if test "x$icarus" != xno; then
echo " libudev.detection....: $libudev"
fi
echo echo
echo "Compilation............: make (or gmake)" echo "Compilation............: make (or gmake)"
echo " CPPFLAGS.............: $CPPFLAGS" echo " CPPFLAGS.............: $CPPFLAGS"
echo " CFLAGS...............: $CFLAGS $LIBUSB_CFLAGS" echo " CFLAGS...............: $CFLAGS $LIBUSB_CFLAGS"
echo " LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS" echo " LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS"
echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS $LIBUSB_LIBS" echo " LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $LIBUSB_LIBS"
echo echo
echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')" echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
echo " prefix...............: $prefix" echo " prefix...............: $prefix"

1090
driver-avalon.c

File diff suppressed because it is too large Load Diff

39
driver-avalon.h

@ -12,6 +12,8 @@
#ifdef USE_AVALON #ifdef USE_AVALON
#include <semaphore.h>
#define AVALON_RESET_FAULT_DECISECONDS 1 #define AVALON_RESET_FAULT_DECISECONDS 1
#define AVALON_MINER_THREADS 1 #define AVALON_MINER_THREADS 1
@ -23,11 +25,16 @@
#define AVALON_DEFAULT_FAN_MAX_PWM 0xA0 /* 100% */ #define AVALON_DEFAULT_FAN_MAX_PWM 0xA0 /* 100% */
#define AVALON_DEFAULT_FAN_MIN_PWM 0x20 /* 20% */ #define AVALON_DEFAULT_FAN_MIN_PWM 0x20 /* 20% */
#define AVALON_DEFAULT_TIMEOUT 0x32 #define AVALON_DEFAULT_TIMEOUT 0x2D
#define AVALON_DEFAULT_FREQUENCY 256 #define AVALON_DEFAULT_FREQUENCY 282
#define AVALON_DEFAULT_MINER_NUM 0x20 #define AVALON_DEFAULT_MINER_NUM 0x20
#define AVALON_DEFAULT_ASIC_NUM 0xA #define AVALON_DEFAULT_ASIC_NUM 0xA
#define AVALON_FTDI_READSIZE 510
#define AVALON_READBUF_SIZE 8192
#define AVALON_RESET_TIMEOUT 100
#define AVALON_READ_TIMEOUT 10
struct avalon_task { struct avalon_task {
uint8_t reset :1; uint8_t reset :1;
uint8_t flush_fifo :1; uint8_t flush_fifo :1;
@ -95,30 +102,32 @@ struct avalon_info {
int matching_work[AVALON_DEFAULT_MINER_NUM]; int matching_work[AVALON_DEFAULT_MINER_NUM];
int frequency; int frequency;
struct thr_info *thr;
pthread_t read_thr;
pthread_t write_thr;
pthread_mutex_t lock;
pthread_mutex_t qlock;
pthread_cond_t qcond;
sem_t read_sem;
sem_t write_sem;
int nonces;
bool idle;
bool reset;
}; };
#define AVALON_WRITE_SIZE (sizeof(struct avalon_task)) #define AVALON_WRITE_SIZE (sizeof(struct avalon_task))
#define AVALON_READ_SIZE (sizeof(struct avalon_result)) #define AVALON_READ_SIZE (sizeof(struct avalon_result))
#define AVALON_ARRAY_SIZE 4 #define AVALON_ARRAY_SIZE 3
#define AVA_GETS_ERROR -1 #define AVA_GETS_ERROR -1
#define AVA_GETS_OK 0 #define AVA_GETS_OK 0
#define AVA_GETS_RESTART 1
#define AVA_GETS_TIMEOUT 2
#define AVA_SEND_ERROR -1 #define AVA_SEND_ERROR -1
#define AVA_SEND_OK 0 #define AVA_SEND_OK 0
#define AVA_SEND_BUFFER_EMPTY 1
#define AVA_SEND_BUFFER_FULL 2
#define AVA_BUFFER_FULL 0
#define AVA_BUFFER_EMPTY 1
#define avalon_open2(devpath, baud, purge) serial_open(devpath, baud, AVALON_RESET_FAULT_DECISECONDS, purge)
#define avalon_open(devpath, baud) avalon_open2(devpath, baud, true)
#define avalon_close(fd) close(fd)
#define avalon_buffer_full(fd) get_serial_cts(fd) #define avalon_buffer_full(avalon) !usb_ftdi_cts(avalon)
#define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud)) #define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud))
#define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1] #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]

74
driver-bflsc.c

@ -99,11 +99,6 @@ struct bflsc_dev {
bool flushed; // are any flushed? bool flushed; // are any flushed?
}; };
// TODO: I stole cgpu_info.device_file
// ... need to update miner.h to instead have a generic void *device_info = NULL;
// ... and these structure definitions need to be in miner.h if API needs to see them
// ... but then again maybe not - maybe another devinfo that the driver provides
// However, clean up all that for all devices in miner.h ... miner.h is a mess at the moment
struct bflsc_info { struct bflsc_info {
pthread_rwlock_t stat_lock; pthread_rwlock_t stat_lock;
struct thr_info results_thr; struct thr_info results_thr;
@ -328,7 +323,7 @@ static void xlinkstr(char *xlink, int dev, struct bflsc_info *sc_info)
static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err) static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int amount, int err)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char xlink[17]; char xlink[17];
xlinkstr(xlink, dev, sc_info); xlinkstr(xlink, dev, sc_info);
@ -487,7 +482,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
{ {
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
*err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd); *err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN) if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false; return false;
else else
@ -496,7 +491,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz) static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
{ {
*err = usb_ftdi_read_nl(bflsc, buf, bufsiz-1, amount, cmd); *err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN) if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false; return false;
else { else {
@ -529,7 +524,7 @@ static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
} }
} }
/* return True = attempted usb_ftdi_read_ok() /* return True = attempted usb_read_ok()
* set ignore to true means no applog/ignore errors */ * set ignore to true means no applog/ignore errors */
static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore) static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
{ {
@ -550,7 +545,7 @@ static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int de
// of course all other I/O must also be failing ... // of course all other I/O must also be failing ...
} else { } else {
readok = true; readok = true;
*err = usb_ftdi_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS); *err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (*err < 0 || *amount < 1) { if (*err < 0 || *amount < 1) {
@ -568,8 +563,6 @@ static void __bflsc_initialise(struct cgpu_info *bflsc)
{ {
int err; int err;
// TODO: this is a standard BFL FPGA Initialisation
// it probably will need changing ...
// TODO: does x-link bypass the other device FTDI? (I think it does) // TODO: does x-link bypass the other device FTDI? (I think it does)
// So no initialisation required except for the master device? // So no initialisation required except for the master device?
@ -588,7 +581,7 @@ static void __bflsc_initialise(struct cgpu_info *bflsc)
// Set data control // Set data control
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA, bflsc->usbdev->found->interface, C_SETDATA); FTDI_VALUE_DATA_BAS, bflsc->usbdev->found->interface, C_SETDATA);
applog(LOG_DEBUG, "%s%i: setdata got err %d", applog(LOG_DEBUG, "%s%i: setdata got err %d",
bflsc->drv->name, bflsc->device_id, err); bflsc->drv->name, bflsc->device_id, err);
@ -597,8 +590,8 @@ static void __bflsc_initialise(struct cgpu_info *bflsc)
return; return;
// Set the baud // Set the baud
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD, err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BAS,
(FTDI_INDEX_BAUD & 0xff00) | bflsc->usbdev->found->interface, (FTDI_INDEX_BAUD_BAS & 0xff00) | bflsc->usbdev->found->interface,
C_SETBAUD); C_SETBAUD);
applog(LOG_DEBUG, "%s%i: setbaud got err %d", applog(LOG_DEBUG, "%s%i: setbaud got err %d",
@ -648,7 +641,7 @@ static void __bflsc_initialise(struct cgpu_info *bflsc)
static void bflsc_initialise(struct cgpu_info *bflsc) static void bflsc_initialise(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
int err, amount; int err, amount;
int dev; int dev;
@ -665,7 +658,7 @@ static void bflsc_initialise(struct cgpu_info *bflsc)
static bool getinfo(struct cgpu_info *bflsc, int dev) static bool getinfo(struct cgpu_info *bflsc, int dev)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct bflsc_dev sc_dev; struct bflsc_dev sc_dev;
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
int err, amount; int err, amount;
@ -695,7 +688,7 @@ static bool getinfo(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS); err = usb_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
if (err < 0) { if (err < 0) {
applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)", applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
@ -805,7 +798,7 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices
if (unlikely(!sc_info)) if (unlikely(!sc_info))
quit(1, "Failed to calloc sc_info in bflsc_detect_one"); quit(1, "Failed to calloc sc_info in bflsc_detect_one");
// TODO: fix ... everywhere ... // TODO: fix ... everywhere ...
bflsc->device_file = (FILE *)sc_info; bflsc->device_data = (FILE *)sc_info;
if (!usb_init(bflsc, dev, found)) if (!usb_init(bflsc, dev, found))
goto shin; goto shin;
@ -830,7 +823,7 @@ reinit:
goto unshin; goto unshin;
} }
err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY); err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
init_count++; init_count++;
cgtime(&init_now); cgtime(&init_now);
@ -897,17 +890,20 @@ reinit:
sc_info->scan_sleep_time = BAM_SCAN_TIME; sc_info->scan_sleep_time = BAM_SCAN_TIME;
sc_info->results_sleep_time = BAM_RES_TIME; sc_info->results_sleep_time = BAM_RES_TIME;
sc_info->default_ms_work = BAM_WORK_TIME; sc_info->default_ms_work = BAM_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAM;
} else { } else {
if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2 if (sc_info->sc_devs[0].engines < 34) { // 16 * 2 + 2
newname = BFLSC_JALAPENO; newname = BFLSC_JALAPENO;
sc_info->scan_sleep_time = BAJ_SCAN_TIME; sc_info->scan_sleep_time = BAJ_SCAN_TIME;
sc_info->results_sleep_time = BAJ_RES_TIME; sc_info->results_sleep_time = BAJ_RES_TIME;
sc_info->default_ms_work = BAJ_WORK_TIME; sc_info->default_ms_work = BAJ_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAJ;
} else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2 } else if (sc_info->sc_devs[0].engines < 130) { // 16 * 8 + 2
newname = BFLSC_LITTLESINGLE; newname = BFLSC_LITTLESINGLE;
sc_info->scan_sleep_time = BAL_SCAN_TIME; sc_info->scan_sleep_time = BAL_SCAN_TIME;
sc_info->results_sleep_time = BAL_RES_TIME; sc_info->results_sleep_time = BAL_RES_TIME;
sc_info->default_ms_work = BAL_WORK_TIME; sc_info->default_ms_work = BAL_WORK_TIME;
bflsc->usbdev->ident = IDENT_BAL;
} }
} }
@ -941,7 +937,7 @@ unshin:
shin: shin:
free(bflsc->device_path); free(bflsc->device_path);
free(bflsc->device_file); free(bflsc->device_data);
if (bflsc->name != blank) if (bflsc->name != blank)
free(bflsc->name); free(bflsc->name);
@ -961,7 +957,7 @@ static void bflsc_detect(void)
static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc) static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
float temp = 0; float temp = 0;
float vcc1 = 0; float vcc1 = 0;
int i; int i;
@ -982,7 +978,7 @@ static void get_bflsc_statline_before(char *buf, struct cgpu_info *bflsc)
static void flush_one_dev(struct cgpu_info *bflsc, int dev) static void flush_one_dev(struct cgpu_info *bflsc, int dev)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct work *work, *tmp; struct work *work, *tmp;
bool did = false; bool did = false;
@ -1011,7 +1007,7 @@ static void flush_one_dev(struct cgpu_info *bflsc, int dev)
static void bflsc_flush_work(struct cgpu_info *bflsc) static void bflsc_flush_work(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int dev; int dev;
for (dev = 0; dev < sc_info->sc_count; dev++) for (dev = 0; dev < sc_info->sc_count; dev++)
@ -1020,7 +1016,7 @@ static void bflsc_flush_work(struct cgpu_info *bflsc)
static void bflsc_flash_led(struct cgpu_info *bflsc, int dev) static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int err, amount; int err, amount;
// Device is gone // Device is gone
@ -1051,7 +1047,7 @@ static void bflsc_flash_led(struct cgpu_info *bflsc, int dev)
static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev) static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct bflsc_dev *sc_dev; struct bflsc_dev *sc_dev;
char temp_buf[BFLSC_BUFSIZ+1]; char temp_buf[BFLSC_BUFSIZ+1];
char volt_buf[BFLSC_BUFSIZ+1]; char volt_buf[BFLSC_BUFSIZ+1];
@ -1095,7 +1091,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE); err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (err < 0) { if (err < 0) {
@ -1117,7 +1113,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE); err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (err < 0) { if (err < 0) {
@ -1256,7 +1252,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces) static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *data, int count, char **fields, int *nonces)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES]; char midstate[MIDSTATE_BYTES], blockdata[MERKLE_BYTES];
struct work *work; struct work *work;
uint32_t nonce; uint32_t nonce;
@ -1340,7 +1336,7 @@ static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char *
static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces) static int process_results(struct cgpu_info *bflsc, int dev, char *buf, int *nonces)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char **items, *firstname, **fields, *lf; char **items, *firstname, **fields, *lf;
int que, i, lines, count; int que, i, lines, count;
char xlink[17]; char xlink[17];
@ -1423,7 +1419,7 @@ arigatou:
static void *bflsc_get_results(void *userdata) static void *bflsc_get_results(void *userdata)
{ {
struct cgpu_info *bflsc = (struct cgpu_info *)userdata; struct cgpu_info *bflsc = (struct cgpu_info *)userdata;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct timeval elapsed, now; struct timeval elapsed, now;
float oldest, f; float oldest, f;
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
@ -1488,7 +1484,7 @@ utsura:
static bool bflsc_thread_prepare(struct thr_info *thr) static bool bflsc_thread_prepare(struct thr_info *thr)
{ {
struct cgpu_info *bflsc = thr->cgpu; struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct timeval now; struct timeval now;
if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) { if (thr_info_create(&(sc_info->results_thr), NULL, bflsc_get_results, (void *)bflsc)) {
@ -1506,7 +1502,7 @@ static bool bflsc_thread_prepare(struct thr_info *thr)
static void bflsc_shutdown(struct thr_info *thr) static void bflsc_shutdown(struct thr_info *thr)
{ {
struct cgpu_info *bflsc = thr->cgpu; struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
bflsc_flush_work(bflsc); bflsc_flush_work(bflsc);
sc_info->shutdown = true; sc_info->shutdown = true;
@ -1524,7 +1520,7 @@ static void bflsc_thread_enable(struct thr_info *thr)
static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work) static bool bflsc_send_work(struct cgpu_info *bflsc, int dev, struct work *work)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct FullNonceRangeJob data; struct FullNonceRangeJob data;
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
int err, amount; int err, amount;
@ -1598,7 +1594,7 @@ re_send:
static bool bflsc_queue_full(struct cgpu_info *bflsc) static bool bflsc_queue_full(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct work *work = NULL; struct work *work = NULL;
int i, dev, tried, que; int i, dev, tried, que;
bool ret = false; bool ret = false;
@ -1666,7 +1662,7 @@ static bool bflsc_queue_full(struct cgpu_info *bflsc)
static int64_t bflsc_scanwork(struct thr_info *thr) static int64_t bflsc_scanwork(struct thr_info *thr)
{ {
struct cgpu_info *bflsc = thr->cgpu; struct cgpu_info *bflsc = thr->cgpu;
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int64_t ret, unsent; int64_t ret, unsent;
bool flushed, cleanup; bool flushed, cleanup;
struct work *work, *tmp; struct work *work, *tmp;
@ -1773,7 +1769,7 @@ static int64_t bflsc_scanwork(struct thr_info *thr)
static bool bflsc_get_stats(struct cgpu_info *bflsc) static bool bflsc_get_stats(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
bool allok = true; bool allok = true;
int i; int i;
@ -1798,7 +1794,7 @@ static bool bflsc_get_stats(struct cgpu_info *bflsc)
static void bflsc_identify(struct cgpu_info *bflsc) static void bflsc_identify(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
// TODO: handle x-link // TODO: handle x-link
sc_info->flash_led = true; sc_info->flash_led = true;
@ -1822,7 +1818,7 @@ static bool bflsc_thread_init(struct thr_info *thr)
static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc) static struct api_data *bflsc_api_stats(struct cgpu_info *bflsc)
{ {
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_file); struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct api_data *root = NULL; struct api_data *root = NULL;
//if no x-link ... etc //if no x-link ... etc

16
driver-bitforce.c

@ -101,7 +101,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
// Set data control // Set data control
err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA, bitforce->usbdev->found->interface, C_SETDATA); FTDI_VALUE_DATA_BFL, bitforce->usbdev->found->interface, C_SETDATA);
if (opt_debug) if (opt_debug)
applog(LOG_DEBUG, "%s%i: setdata got err %d", applog(LOG_DEBUG, "%s%i: setdata got err %d",
bitforce->drv->name, bitforce->device_id, err); bitforce->drv->name, bitforce->device_id, err);
@ -110,8 +110,8 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
goto failed; goto failed;
// Set the baud // Set the baud
err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD, err = usb_transfer(bitforce, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BFL,
(FTDI_INDEX_BAUD & 0xff00) | bitforce->usbdev->found->interface, (FTDI_INDEX_BAUD_BFL & 0xff00) | bitforce->usbdev->found->interface,
C_SETBAUD); C_SETBAUD);
if (opt_debug) if (opt_debug)
applog(LOG_DEBUG, "%s%i: setbaud got err %d", applog(LOG_DEBUG, "%s%i: setbaud got err %d",
@ -200,7 +200,7 @@ reinit:
goto unshin; goto unshin;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
init_count++; init_count++;
cgtime(&init_now); cgtime(&init_now);
if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) { if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
@ -395,7 +395,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
if (err < 0) { if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)", applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
@ -470,7 +470,7 @@ re_send:
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)", applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err); bitforce->drv->name, bitforce->device_id, amount, err);
@ -524,7 +524,7 @@ re_send:
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)", applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err); bitforce->drv->name, bitforce->device_id, amount, err);
@ -573,7 +573,7 @@ static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
mutex_lock(&bitforce->device_mutex); mutex_lock(&bitforce->device_mutex);
usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS); usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS); usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
cgtime(&now); cgtime(&now);

651
driver-icarus.c

@ -1,7 +1,7 @@
/* /*
* Copyright 2012 Luke Dashjr * Copyright 2012-2013 Andrew Smith
* Copyright 2012 Xiangfu <xiangfu@openmobilefree.com> * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
* Copyright 2012 Andrew Smith * Copyright 2013 Con Kolivas <kernel@kolivas.org>
* *
* This program is free software; you can redistribute it and/or modify it * 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 * under the terms of the GNU General Public License as published by the Free
@ -29,30 +29,25 @@
* nonce range is completely calculated. * nonce range is completely calculated.
*/ */
#include "config.h"
#include "miner.h"
#include <float.h>
#include <limits.h> #include <limits.h>
#include <pthread.h> #include <pthread.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <strings.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h> #include <unistd.h>
#ifndef WIN32
#include <termios.h> #include "config.h"
#include <sys/stat.h>
#include <fcntl.h> #ifdef WIN32
#ifndef O_CLOEXEC #include <windows.h>
#define O_CLOEXEC 0
#endif
#else
#include <windows.h>
#include <io.h>
#endif #endif
#include "elist.h" #include "compat.h"
#include "fpgautils.h" #include "miner.h"
#include "usbutils.h"
// The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
#define ICARUS_IO_SPEED 115200 #define ICARUS_IO_SPEED 115200
@ -67,41 +62,53 @@
#define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1] #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
ASSERT1(sizeof(uint32_t) == 4); ASSERT1(sizeof(uint32_t) == 4);
#define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud)) // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
// or even use that number? 1/2
// #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
// maybe 1ms?
#define ICARUS_READ_TIME(baud) (0.001)
// Fraction of a second, USB timeout is measured in // USB ms timeout to wait
// i.e. 10 means 1/10 of a second #define ICARUS_WAIT_TIMEOUT 100
#define TIME_FACTOR 10
// It's 10 per second, thus value = 10/TIME_FACTOR =
#define ICARUS_READ_FAULT_DECISECONDS 1
// In timing mode: Default starting value until an estimate can be obtained // In timing mode: Default starting value until an estimate can be obtained
// 5 seconds allows for up to a ~840MH/s device // 5000 ms allows for up to a ~840MH/s device
#define ICARUS_READ_COUNT_TIMING (5 * TIME_FACTOR) #define ICARUS_READ_COUNT_TIMING 5000
#define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
#define SECTOMS(s) ((int)((s) * 1000))
// How many ms below the expected completion time to abort work
// extra in case the last read is delayed
#define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
// For a standard Icarus REV3 (to 5 places) // For a standard Icarus REV3 (to 5 places)
// Since this rounds up a the last digit - it is a slight overestimate // Since this rounds up a the last digit - it is a slight overestimate
// Thus the hash rate will be a VERY slight underestimate // Thus the hash rate will be a VERY slight underestimate
// (by a lot less than the displayed accuracy) // (by a lot less than the displayed accuracy)
// Minor inaccuracy of these numbers doesn't affect the work done,
// only the displayed MH/s
#define ICARUS_REV3_HASH_TIME 0.0000000026316 #define ICARUS_REV3_HASH_TIME 0.0000000026316
#define LANCELOT_HASH_TIME 0.0000000025000
#define ASICMINERUSB_HASH_TIME 0.0000000029761
// TODO: What is it?
#define CAIRNSMORE1_HASH_TIME 0.0000000026316
#define NANOSEC 1000000000.0 #define NANOSEC 1000000000.0
// Icarus Rev3 doesn't send a completion message when it finishes // Icarus Rev3 doesn't send a completion message when it finishes
// the full nonce range, so to avoid being idle we must abort the // the full nonce range, so to avoid being idle we must abort the
// work (by starting a new work) shortly before it finishes // work (by starting a new work item) shortly before it finishes
// //
// Thus we need to estimate 2 things: // Thus we need to estimate 2 things:
// 1) How many hashes were done if the work was aborted // 1) How many hashes were done if the work was aborted
// 2) How high can the timeout be before the Icarus is idle, // 2) How high can the timeout be before the Icarus is idle,
// to minimise the number of work started // to minimise the number of work items started
// We set 2) to 'the calculated estimate' - 1 // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
// to ensure the estimate ends before idle // to ensure the estimate ends before idle
// //
// The simple calculation used is: // The simple calculation used is:
// Tn = Total time in seconds to calculate n hashes // Tn = Total time in seconds to calculate n hashes
// Hs = seconds per hash // Hs = seconds per hash
// Xn = number of hashes // Xn = number of hashes
// W = code overhead per work // W = code/usb overhead per work
// //
// Rough but reasonable estimate: // Rough but reasonable estimate:
// Tn = Hs * Xn + W (of the form y = mx + b) // Tn = Hs * Xn + W (of the form y = mx + b)
@ -123,7 +130,7 @@ ASSERT1(sizeof(uint32_t) == 4);
#define HISTORY_SEC 60 #define HISTORY_SEC 60
// Minimum how many points a single ICARUS_HISTORY should have // Minimum how many points a single ICARUS_HISTORY should have
#define MIN_DATA_COUNT 5 #define MIN_DATA_COUNT 5
// The value above used is doubled each history until it exceeds: // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
#define MAX_MIN_DATA_COUNT 100 #define MAX_MIN_DATA_COUNT 100
static struct timeval history_sec = { HISTORY_SEC, 0 }; static struct timeval history_sec = { HISTORY_SEC, 0 };
@ -163,7 +170,8 @@ struct ICARUS_INFO {
// seconds per Hash // seconds per Hash
double Hs; double Hs;
int read_count; // ms til we abort
int read_time;
enum timing_mode timing_mode; enum timing_mode timing_mode;
bool do_icarus_timing; bool do_icarus_timing;
@ -188,14 +196,12 @@ struct ICARUS_INFO {
#define END_CONDITION 0x0000ffff #define END_CONDITION 0x0000ffff
// One for each possible device
static struct ICARUS_INFO **icarus_info;
// Looking for options in --icarus-timing and --icarus-options: // Looking for options in --icarus-timing and --icarus-options:
// //
// Code increments this each time we start to look at a device // Code increments this each time we start to look at a device
// However, this means that if other devices are checked by // However, this means that if other devices are checked by
// the Icarus code (e.g. BFL) they will count in the option offset // the Icarus code (e.g. Avalon only as at 20130517)
// they will count in the option offset
// //
// This, however, is deterministic so that's OK // This, however, is deterministic so that's OK
// //
@ -203,13 +209,173 @@ static struct ICARUS_INFO **icarus_info;
// that would be random since an Icarus may fail and thus we'd // that would be random since an Icarus may fail and thus we'd
// not be able to predict the option order // not be able to predict the option order
// //
// This also assumes that serial_detect() checks them sequentially // Devices are checked in the order libusb finds them which is ?
// and in the order specified on the command line
// //
static int option_offset = -1; static int option_offset = -1;
struct device_drv icarus_drv; struct device_drv icarus_drv;
/*
#define ICA_BUFSIZ (0x200)
static void transfer_read(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, enum usb_cmds cmd)
{
int err;
err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd);
applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
icarus->drv->name, icarus->cgminer_id,
usb_cmdname(cmd), err);
}
*/
static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
{
int err;
err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd);
applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
icarus->drv->name, icarus->cgminer_id,
usb_cmdname(cmd), err);
}
#define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \
_transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd)
static void icarus_initialise(struct cgpu_info *icarus, int baud)
{
uint16_t wValue, wIndex;
if (icarus->usbinfo.nodev)
return;
switch (icarus->usbdev->ident) {
case IDENT_BLT:
case IDENT_LLT:
case IDENT_CMR1:
case IDENT_CMR2:
// Latency
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY, FTDI_VALUE_LATENCY,
icarus->usbdev->found->interface, C_LATENCY);
if (icarus->usbinfo.nodev)
return;
// Reset
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
icarus->usbdev->found->interface, C_RESET);
if (icarus->usbinfo.nodev)
return;
// Set data control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
icarus->usbdev->found->interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
// default to BLT/LLT 115200
wValue = FTDI_VALUE_BAUD_BLT;
wIndex = FTDI_INDEX_BAUD_BLT;
if (icarus->usbdev->ident == IDENT_CMR1 ||
icarus->usbdev->ident == IDENT_CMR2) {
switch (baud) {
case 115200:
wValue = FTDI_VALUE_BAUD_CMR_115;
wIndex = FTDI_INDEX_BAUD_CMR_115;
break;
case 57600:
wValue = FTDI_VALUE_BAUD_CMR_57;
wIndex = FTDI_INDEX_BAUD_CMR_57;
break;
default:
quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud);
break;
}
}
// Set the baud
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
(wIndex & 0xff00) | icarus->usbdev->found->interface,
C_SETBAUD);
if (icarus->usbinfo.nodev)
return;
// Set Modem Control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
icarus->usbdev->found->interface, C_SETMODEM);
if (icarus->usbinfo.nodev)
return;
// Set Flow Control
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
icarus->usbdev->found->interface, C_SETFLOW);
if (icarus->usbinfo.nodev)
return;
// Clear any sent data
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
icarus->usbdev->found->interface, C_PURGETX);
if (icarus->usbinfo.nodev)
return;
// Clear any received data
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
icarus->usbdev->found->interface, C_PURGERX);
break;
case IDENT_ICA:
// Set Data Control
transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
icarus->usbdev->found->interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
// Set Line Control
uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
_transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
icarus->usbdev->found->interface,
&ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
if (icarus->usbinfo.nodev)
return;
// Vendor
transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
icarus->usbdev->found->interface, C_VENDOR);
break;
case IDENT_AMU:
// Set data control
transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
icarus->usbdev->found->interface, C_SETDATA);
if (icarus->usbinfo.nodev)
return;
// Set the baud
uint32_t data = CP210X_DATA_BAUD;
_transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
icarus->usbdev->found->interface,
&data, sizeof(data), C_SETBAUD);
break;
default:
quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
icarus->drv->name, icarus->cgminer_id,
icarus->usbdev->ident);
}
}
static void rev(unsigned char *s, size_t l) static void rev(unsigned char *s, size_t l)
{ {
size_t i, j; size_t i, j;
@ -222,81 +388,66 @@ static void rev(unsigned char *s, size_t l)
} }
} }
#define icarus_open2(devpath, baud, purge) serial_open(devpath, baud, ICARUS_READ_FAULT_DECISECONDS, purge) #define ICA_NONCE_ERROR -1
#define icarus_open(devpath, baud) icarus_open2(devpath, baud, false) #define ICA_NONCE_OK 0
#define ICA_NONCE_RESTART 1
#define ICA_GETS_ERROR -1 #define ICA_NONCE_TIMEOUT 2
#define ICA_GETS_OK 0
#define ICA_GETS_RESTART 1
#define ICA_GETS_TIMEOUT 2
static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count) static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start, struct timeval *tv_finish, struct thr_info *thr, int read_time)
{ {
ssize_t ret = 0; struct timeval read_start, read_finish;
int err, amt;
int rc = 0; int rc = 0;
int read_amount = ICARUS_READ_SIZE; int read_amount = ICARUS_READ_SIZE;
bool first = true; bool first = true;
// Read reply 1 byte at a time to get earliest tv_finish cgtime(tv_start);
while (true) { while (true) {
ret = read(fd, buf, 1); if (icarus->usbinfo.nodev)
if (ret < 0) return ICA_NONCE_ERROR;
return ICA_GETS_ERROR;
cgtime(&read_start);
err = usb_read_timeout(icarus, (char *)buf, read_amount, &amt, ICARUS_WAIT_TIMEOUT, C_GETRESULTS);
cgtime(&read_finish);
if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
applog(LOG_ERR, "%s%i: Comms error (rerr=%d amt=%d)",
icarus->drv->name, icarus->device_id, err, amt);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
return ICA_NONCE_ERROR;
}
if (first) if (first)
cgtime(tv_finish); copy_time(tv_finish, &read_finish);
if (ret >= read_amount) // TODO: test if there is more data? to read a 2nd nonce?
return ICA_GETS_OK; if (amt >= read_amount)
return ICA_NONCE_OK;
if (ret > 0) {
buf += ret; rc += SECTOMS(tdiff(&read_finish, &read_start));
read_amount -= ret; if (rc >= read_time) {
first = false; if (amt > 0)
continue; applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
else
applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
return ICA_NONCE_TIMEOUT;
} }
rc++; if (thr && thr->work_restart) {
if (rc >= read_count) {
if (opt_debug) { if (opt_debug) {
applog(LOG_DEBUG, applog(LOG_DEBUG,
"Icarus Read: No data in %.2f seconds", "Icarus Read: Work restart at %d ms", rc);
(float)rc/(float)TIME_FACTOR);
} }
return ICA_GETS_TIMEOUT; return ICA_NONCE_RESTART;
} }
if (thr && thr->work_restart) { if (amt > 0) {
if (opt_debug) { buf += amt;
applog(LOG_DEBUG, read_amount -= amt;
"Icarus Read: Work restart at %.2f seconds", first = false;
(float)(rc)/(float)TIME_FACTOR);
}
return ICA_GETS_RESTART;
} }
} }
} }
static int icarus_write(int fd, const void *buf, size_t bufLen)
{
size_t ret;
ret = write(fd, buf, bufLen);
if (unlikely(ret != bufLen))
return 1;
return 0;
}
#define icarus_close(fd) close(fd)
static void do_icarus_close(struct thr_info *thr)
{
struct cgpu_info *icarus = thr->cgpu;
icarus_close(icarus->device_fd);
icarus->device_fd = -1;
}
static const char *timing_mode_str(enum timing_mode timing_mode) static const char *timing_mode_str(enum timing_mode timing_mode)
{ {
switch(timing_mode) { switch(timing_mode) {
@ -315,7 +466,7 @@ static const char *timing_mode_str(enum timing_mode timing_mode)
static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus) static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
{ {
struct ICARUS_INFO *info = icarus_info[icarus->device_id]; struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
double Hs; double Hs;
char buf[BUFSIZ+1]; char buf[BUFSIZ+1];
char *ptr, *comma, *eq; char *ptr, *comma, *eq;
@ -345,18 +496,37 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
buf[max] = '\0'; buf[max] = '\0';
} }
info->Hs = 0; switch (icarus->usbdev->ident) {
info->read_count = 0; case IDENT_ICA:
info->Hs = ICARUS_REV3_HASH_TIME;
break;
case IDENT_BLT:
case IDENT_LLT:
info->Hs = LANCELOT_HASH_TIME;
break;
case IDENT_AMU:
info->Hs = ASICMINERUSB_HASH_TIME;
break;
// TODO: ?
case IDENT_CMR1:
case IDENT_CMR2:
info->Hs = CAIRNSMORE1_HASH_TIME;
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
icarus->drv->name, icarus->usbdev->ident);
}
info->read_time = 0;
// TODO: allow short=N and long=N
if (strcasecmp(buf, MODE_SHORT_STR) == 0) { if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
info->Hs = ICARUS_REV3_HASH_TIME; info->read_time = ICARUS_READ_COUNT_TIMING;
info->read_count = ICARUS_READ_COUNT_TIMING;
info->timing_mode = MODE_SHORT; info->timing_mode = MODE_SHORT;
info->do_icarus_timing = true; info->do_icarus_timing = true;
} else if (strcasecmp(buf, MODE_LONG_STR) == 0) { } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
info->Hs = ICARUS_REV3_HASH_TIME; info->read_time = ICARUS_READ_COUNT_TIMING;
info->read_count = ICARUS_READ_COUNT_TIMING;
info->timing_mode = MODE_LONG; info->timing_mode = MODE_LONG;
info->do_icarus_timing = true; info->do_icarus_timing = true;
@ -365,27 +535,29 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
info->fullnonce = info->Hs * (((double)0xffffffff) + 1); info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
if ((eq = strchr(buf, '=')) != NULL) if ((eq = strchr(buf, '=')) != NULL)
info->read_count = atoi(eq+1); info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
if (info->read_count < 1) if (info->read_time < ICARUS_READ_COUNT_MIN)
info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1; info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
if (unlikely(info->read_count < 1)) if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
info->read_count = 1; info->read_time = ICARUS_READ_COUNT_MIN;
info->timing_mode = MODE_VALUE; info->timing_mode = MODE_VALUE;
info->do_icarus_timing = false; info->do_icarus_timing = false;
} else { } else {
// Anything else in buf just uses DEFAULT mode // Anything else in buf just uses DEFAULT mode
info->Hs = ICARUS_REV3_HASH_TIME;
info->fullnonce = info->Hs * (((double)0xffffffff) + 1); info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
if ((eq = strchr(buf, '=')) != NULL) if ((eq = strchr(buf, '=')) != NULL)
info->read_count = atoi(eq+1); info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
if (info->read_count < 1) if (info->read_time < ICARUS_READ_COUNT_MIN)
info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1; info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
info->read_time = ICARUS_READ_COUNT_MIN;
info->timing_mode = MODE_DEFAULT; info->timing_mode = MODE_DEFAULT;
info->do_icarus_timing = false; info->do_icarus_timing = false;
@ -393,8 +565,10 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
info->min_data_count = MIN_DATA_COUNT; info->min_data_count = MIN_DATA_COUNT;
applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e", applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms Hs=%e",
icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs); icarus->drv->name, icarus->cgminer_id,
timing_mode_str(info->timing_mode),
info->read_time, info->Hs);
} }
static uint32_t mask(int work_division) static uint32_t mask(int work_division)
@ -424,7 +598,7 @@ static uint32_t mask(int work_division)
return nonce_mask; return nonce_mask;
} }
static void get_options(int this_option_offset, int *baud, int *work_division, int *fpga_count) static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
{ {
char err_buf[BUFSIZ+1]; char err_buf[BUFSIZ+1];
char buf[BUFSIZ+1]; char buf[BUFSIZ+1];
@ -455,9 +629,30 @@ static void get_options(int this_option_offset, int *baud, int *work_division, i
buf[max] = '\0'; buf[max] = '\0';
} }
*baud = ICARUS_IO_SPEED; switch (icarus->usbdev->ident) {
*work_division = 2; case IDENT_ICA:
*fpga_count = 2; case IDENT_BLT:
case IDENT_LLT:
*baud = ICARUS_IO_SPEED;
*work_division = 2;
*fpga_count = 2;
break;
case IDENT_AMU:
*baud = ICARUS_IO_SPEED;
*work_division = 1;
*fpga_count = 1;
break;
// TODO: ?
case IDENT_CMR1:
case IDENT_CMR2:
*baud = ICARUS_IO_SPEED;
*work_division = 2;
*fpga_count = 2;
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
icarus->drv->name, icarus->usbdev->ident);
}
if (*buf) { if (*buf) {
colon = strchr(buf, ':'); colon = strchr(buf, ':');
@ -508,13 +703,12 @@ static void get_options(int this_option_offset, int *baud, int *work_division, i
} }
} }
static bool icarus_detect_one(const char *devpath) static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{ {
int this_option_offset = ++option_offset; int this_option_offset = ++option_offset;
char devpath[20];
struct ICARUS_INFO *info; struct ICARUS_INFO *info;
struct timeval tv_start, tv_finish; struct timeval tv_start, tv_finish;
int fd;
// Block 171874 nonce = (0xa2870100) = 0x000187a2 // Block 171874 nonce = (0xa2870100) = 0x000187a2
// N.B. golden_ob MUST take less time to calculate // N.B. golden_ob MUST take less time to calculate
@ -528,68 +722,87 @@ static bool icarus_detect_one(const char *devpath)
const char golden_nonce[] = "000187a2"; const char golden_nonce[] = "000187a2";
const uint32_t golden_nonce_val = 0x000187a2; const uint32_t golden_nonce_val = 0x000187a2;
unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE]; unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
char *nonce_hex; char *nonce_hex;
int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
struct cgpu_info *icarus;
int ret, err, amount, tries;
bool ok;
int baud, work_division, fpga_count; icarus = calloc(1, sizeof(struct cgpu_info));
if (unlikely(!icarus))
quit(1, "Failed to calloc icarus in icarus_detect_one");
icarus->drv = &icarus_drv;
icarus->deven = DEV_ENABLED;
icarus->threads = 1;
get_options(this_option_offset, &baud, &work_division, &fpga_count); if (!usb_init(icarus, dev, found))
goto shin;
applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath); get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
fd = icarus_open2(devpath, baud, true); sprintf(devpath, "%d:%d",
if (unlikely(fd == -1)) { (int)(icarus->usbinfo.bus_number),
applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath); (int)(icarus->usbinfo.device_address));
return false;
} icarus->device_path = strdup(devpath);
hex2bin(ob_bin, golden_ob, sizeof(ob_bin)); hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
icarus_write(fd, ob_bin, sizeof(ob_bin));
cgtime(&tv_start);
memset(nonce_bin, 0, sizeof(nonce_bin)); tries = 2;
icarus_gets(nonce_bin, fd, &tv_finish, NULL, 1); ok = false;
while (!ok && tries-- > 0) {
icarus_initialise(icarus, baud);
icarus_close(fd); err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDTESTWORK);
if (err != LIBUSB_SUCCESS || amount != sizeof(ob_bin))
continue;
nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin)); memset(nonce_bin, 0, sizeof(nonce_bin));
if (strncmp(nonce_hex, golden_nonce, 8)) { ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
applog(LOG_ERR, if (ret != ICA_NONCE_OK)
"Icarus Detect: " continue;
"Test failed at %s: get %s, should: %s",
devpath, nonce_hex, golden_nonce); nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
if (strncmp(nonce_hex, golden_nonce, 8) == 0)
ok = true;
else {
if (tries < 0) {
applog(LOG_ERR,
"Icarus Detect: "
"Test failed at %s: get %s, should: %s",
devpath, nonce_hex, golden_nonce);
}
}
free(nonce_hex); free(nonce_hex);
return false;
} }
if (!ok)
goto unshin;
applog(LOG_DEBUG, applog(LOG_DEBUG,
"Icarus Detect: " "Icarus Detect: "
"Test succeeded at %s: got %s", "Test succeeded at %s: got %s",
devpath, nonce_hex); devpath, golden_nonce);
free(nonce_hex);
/* We have a real Icarus! */ /* We have a real Icarus! */
struct cgpu_info *icarus; if (!add_cgpu(icarus))
icarus = calloc(1, sizeof(struct cgpu_info)); goto unshin;
icarus->drv = &icarus_drv;
icarus->device_path = strdup(devpath); update_usb_stats(icarus);
icarus->device_fd = -1;
icarus->threads = 1;
add_cgpu(icarus);
icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
applog(LOG_INFO, "Found Icarus at %s, mark as %d", applog(LOG_INFO, "%s%d: Found at %s",
devpath, icarus->device_id); icarus->drv->name, icarus->device_id, devpath);
applog(LOG_DEBUG, "Icarus: Init: %d baud=%d work_division=%d fpga_count=%d", applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
icarus->device_id, baud, work_division, fpga_count); icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
// Since we are adding a new device on the end it needs to always be allocated info = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO)); if (unlikely(!info))
if (unlikely(!(icarus_info[icarus->device_id])))
quit(1, "Failed to malloc ICARUS_INFO"); quit(1, "Failed to malloc ICARUS_INFO");
info = icarus_info[icarus->device_id]; icarus->device_data = (void *)info;
// Initialise everything to zero for a new device // Initialise everything to zero for a new device
memset(info, 0, sizeof(struct ICARUS_INFO)); memset(info, 0, sizeof(struct ICARUS_INFO));
@ -605,11 +818,23 @@ static bool icarus_detect_one(const char *devpath)
set_timing_mode(this_option_offset, icarus); set_timing_mode(this_option_offset, icarus);
return true; return true;
unshin:
usb_uninit(icarus);
free(icarus->device_path);
shin:
free(icarus);
return false;
} }
static void icarus_detect() static void icarus_detect()
{ {
serial_detect(&icarus_drv, icarus_detect_one); usb_detect(&icarus_drv, icarus_detect_one);
} }
static bool icarus_prepare(struct thr_info *thr) static bool icarus_prepare(struct thr_info *thr)
@ -618,18 +843,6 @@ static bool icarus_prepare(struct thr_info *thr)
struct timeval now; struct timeval now;
icarus->device_fd = -1;
int fd = icarus_open(icarus->device_path, icarus_info[icarus->device_id]->baud);
if (unlikely(-1 == fd)) {
applog(LOG_ERR, "Failed to open Icarus on %s",
icarus->device_path);
return false;
}
icarus->device_fd = fd;
applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
cgtime(&now); cgtime(&now);
get_datestamp(icarus->init, &now); get_datestamp(icarus->init, &now);
@ -639,12 +852,9 @@ static bool icarus_prepare(struct thr_info *thr)
static int64_t icarus_scanhash(struct thr_info *thr, struct work *work, static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
__maybe_unused int64_t max_nonce) __maybe_unused int64_t max_nonce)
{ {
struct cgpu_info *icarus; struct cgpu_info *icarus = thr->cgpu;
int fd; struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
int ret; int ret, err, amount;
struct ICARUS_INFO *info;
unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE]; unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
char *ob_hex; char *ob_hex;
uint32_t nonce; uint32_t nonce;
@ -658,65 +868,49 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
struct ICARUS_HISTORY *history0, *history; struct ICARUS_HISTORY *history0, *history;
int count; int count;
double Hs, W, fullnonce; double Hs, W, fullnonce;
int read_count; int read_time;
int64_t estimate_hashes; int64_t estimate_hashes;
uint32_t values; uint32_t values;
int64_t hash_count_range; int64_t hash_count_range;
elapsed.tv_sec = elapsed.tv_usec = 0; // Device is gone
if (icarus->usbinfo.nodev)
icarus = thr->cgpu; return -1;
if (icarus->device_fd == -1)
if (!icarus_prepare(thr)) {
applog(LOG_ERR, "%s%i: Comms error", icarus->drv->name, icarus->device_id);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
// fail the device if the reopen attempt fails elapsed.tv_sec = elapsed.tv_usec = 0;
return -1;
}
fd = icarus->device_fd;
memset(ob_bin, 0, sizeof(ob_bin)); memset(ob_bin, 0, sizeof(ob_bin));
memcpy(ob_bin, work->midstate, 32); memcpy(ob_bin, work->midstate, 32);
memcpy(ob_bin + 52, work->data + 64, 12); memcpy(ob_bin + 52, work->data + 64, 12);
rev(ob_bin, 32); rev(ob_bin, 32);
rev(ob_bin + 52, 12); rev(ob_bin + 52, 12);
#ifndef WIN32
tcflush(fd, TCOFLUSH); err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDWORK);
#endif if (err < 0 || amount != sizeof(ob_bin)) {
ret = icarus_write(fd, ob_bin, sizeof(ob_bin)); applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
if (ret) { icarus->drv->name, icarus->device_id, err, amount);
do_icarus_close(thr);
applog(LOG_ERR, "%s%i: Comms error", icarus->drv->name, icarus->device_id);
dev_error(icarus, REASON_DEV_COMMS_ERROR); dev_error(icarus, REASON_DEV_COMMS_ERROR);
return 0; /* This should never happen */ icarus_initialise(icarus, info->baud);
return 0;
} }
cgtime(&tv_start);
if (opt_debug) { if (opt_debug) {
ob_hex = bin2hex(ob_bin, sizeof(ob_bin)); ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
applog(LOG_DEBUG, "Icarus %d sent: %s", applog(LOG_DEBUG, "%s%d: sent %s",
icarus->device_id, ob_hex); icarus->drv->name, icarus->device_id, ob_hex);
free(ob_hex); free(ob_hex);
} }
/* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */ /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
memset(nonce_bin, 0, sizeof(nonce_bin)); memset(nonce_bin, 0, sizeof(nonce_bin));
info = icarus_info[icarus->device_id]; ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
ret = icarus_gets(nonce_bin, fd, &tv_finish, thr, info->read_count); if (ret == ICA_NONCE_ERROR)
if (ret == ICA_GETS_ERROR) {
do_icarus_close(thr);
applog(LOG_ERR, "%s%i: Comms error", icarus->drv->name, icarus->device_id);
dev_error(icarus, REASON_DEV_COMMS_ERROR);
return 0; return 0;
}
work->blk.nonce = 0xffffffff; work->blk.nonce = 0xffffffff;
// aborted before becoming idle, get new work // aborted before becoming idle, get new work
if (ret == ICA_GETS_TIMEOUT || ret == ICA_GETS_RESTART) { if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
timersub(&tv_finish, &tv_start, &elapsed); timersub(&tv_finish, &tv_start, &elapsed);
// ONLY up to just when it aborted // ONLY up to just when it aborted
@ -730,8 +924,9 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
estimate_hashes = 0xffffffff; estimate_hashes = 0xffffffff;
if (opt_debug) { if (opt_debug) {
applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08lX hashes (%ld.%06lds)", applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
icarus->device_id, (long unsigned int)estimate_hashes, icarus->drv->name, icarus->device_id,
(long unsigned int)estimate_hashes,
elapsed.tv_sec, elapsed.tv_usec); elapsed.tv_sec, elapsed.tv_usec);
} }
@ -739,19 +934,11 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
} }
memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin)); memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
nonce = htobe32(nonce);
#if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
nonce = swab32(nonce);
#endif
curr_hw_errors = icarus->hw_errors; curr_hw_errors = icarus->hw_errors;
submit_nonce(thr, work, nonce); submit_nonce(thr, work, nonce);
was_hw_error = (curr_hw_errors > icarus->hw_errors); was_hw_error = (curr_hw_errors > icarus->hw_errors);
// Force a USB close/reopen on any hw error
if (was_hw_error)
do_icarus_close(thr);
hash_count = (nonce & info->nonce_mask); hash_count = (nonce & info->nonce_mask);
hash_count++; hash_count++;
hash_count *= info->fpga_count; hash_count *= info->fpga_count;
@ -760,8 +947,9 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
timersub(&tv_finish, &tv_start, &elapsed); timersub(&tv_finish, &tv_start, &elapsed);
if (opt_debug) { if (opt_debug) {
applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)", applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
icarus->device_id, nonce, (long unsigned int)hash_count, icarus->drv->name, icarus->device_id,
nonce, (long unsigned int)hash_count,
elapsed.tv_sec, elapsed.tv_usec); elapsed.tv_sec, elapsed.tv_usec);
} }
@ -804,7 +992,7 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
memset(history0, 0, sizeof(struct ICARUS_HISTORY)); memset(history0, 0, sizeof(struct ICARUS_HISTORY));
// We just completed a history data set // We just completed a history data set
// So now recalc read_count based on the whole history thus we will // So now recalc read_time based on the whole history thus we will
// initially get more accurate until it completes INFO_HISTORY // initially get more accurate until it completes INFO_HISTORY
// total data sets // total data sets
count = 0; count = 0;
@ -837,10 +1025,10 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
memset(history0, 0, sizeof(struct ICARUS_HISTORY)); memset(history0, 0, sizeof(struct ICARUS_HISTORY));
fullnonce = W + Hs * (((double)0xffffffff) + 1); fullnonce = W + Hs * (((double)0xffffffff) + 1);
read_count = (int)(fullnonce * TIME_FACTOR) - 1; read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
info->Hs = Hs; info->Hs = Hs;
info->read_count = read_count; info->read_time = read_time;
info->fullnonce = fullnonce; info->fullnonce = fullnonce;
info->count = count; info->count = count;
@ -853,9 +1041,8 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
else if (info->timing_mode == MODE_SHORT) else if (info->timing_mode == MODE_SHORT)
info->do_icarus_timing = false; info->do_icarus_timing = false;
// applog(LOG_WARNING, "Icarus %d Re-estimate: read_count=%d fullnonce=%fs history count=%d Hs=%e W=%e values=%d hash range=0x%08lx min data count=%u", icarus->device_id, read_count, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count); applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms fullnonce=%.3fs",
applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs", icarus->drv->name, icarus->device_id, Hs, W, read_time, fullnonce);
icarus->device_id, Hs, W, read_count, fullnonce);
} }
info->history_count++; info->history_count++;
cgtime(&tv_history_finish); cgtime(&tv_history_finish);
@ -870,13 +1057,13 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
static struct api_data *icarus_api_stats(struct cgpu_info *cgpu) static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
{ {
struct api_data *root = NULL; struct api_data *root = NULL;
struct ICARUS_INFO *info = icarus_info[cgpu->device_id]; struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
// Warning, access to these is not locked - but we don't really // Warning, access to these is not locked - but we don't really
// care since hashing performance is way more important than // care since hashing performance is way more important than
// locking access to displaying API debug 'stats' // locking access to displaying API debug 'stats'
// If locking becomes an issue for any of them, use copy_data=true also // If locking becomes an issue for any of them, use copy_data=true also
root = api_add_int(root, "read_count", &(info->read_count), false); root = api_add_int(root, "read_time", &(info->read_time), false);
root = api_add_double(root, "fullnonce", &(info->fullnonce), false); root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
root = api_add_int(root, "count", &(info->count), false); root = api_add_int(root, "count", &(info->count), false);
root = api_add_hs(root, "Hs", &(info->Hs), false); root = api_add_hs(root, "Hs", &(info->Hs), false);
@ -896,9 +1083,9 @@ static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
return root; return root;
} }
static void icarus_shutdown(struct thr_info *thr) static void icarus_shutdown(__maybe_unused struct thr_info *thr)
{ {
do_icarus_close(thr); // TODO: ?
} }
struct device_drv icarus_drv = { struct device_drv icarus_drv = {

2
driver-opencl.c

@ -1570,9 +1570,9 @@ static void opencl_thread_shutdown(struct thr_info *thr)
const int thr_id = thr->id; const int thr_id = thr->id;
_clState *clState = clStates[thr_id]; _clState *clState = clStates[thr_id];
clReleaseCommandQueue(clState->commandQueue);
clReleaseKernel(clState->kernel); clReleaseKernel(clState->kernel);
clReleaseProgram(clState->program); clReleaseProgram(clState->program);
clReleaseCommandQueue(clState->commandQueue);
clReleaseContext(clState->context); clReleaseContext(clState->context);
} }

2
driver-ztex.c

@ -7,7 +7,7 @@
* Copyright (C) 2009-2011 ZTEX GmbH. * Copyright (C) 2009-2011 ZTEX GmbH.
* http://www.ztex.de * http://www.ztex.de
* *
* This work is based upon the icarus.c worker which is * This work is based upon the icarus.c worker which was
* Copyright 2012 Luke Dashjr * Copyright 2012 Luke Dashjr
* Copyright 2012 Xiangfu <xiangfu@openmobilefree.com> * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
* *

15
miner.h

@ -19,6 +19,10 @@
# include <netdb.h> # include <netdb.h>
#endif #endif
#ifdef USE_USBUTILS
#include <semaphore.h>
#endif
#ifdef HAVE_OPENCL #ifdef HAVE_OPENCL
#ifdef __APPLE_CC__ #ifdef __APPLE_CC__
#include <OpenCL/opencl.h> #include <OpenCL/opencl.h>
@ -422,16 +426,13 @@ struct cgpu_info {
int device_id; int device_id;
char *name; char *name;
char *device_path; char *device_path;
FILE *device_file; void *device_data;
union { union {
#ifdef USE_ZTEX #ifdef USE_ZTEX
struct libztex_device *device_ztex; struct libztex_device *device_ztex;
#endif #endif
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
struct cg_usb_device *usbdev; struct cg_usb_device *usbdev;
#endif
#if defined(USE_ICARUS) || defined(USE_AVALON)
int device_fd;
#endif #endif
}; };
#ifdef USE_AVALON #ifdef USE_AVALON
@ -545,6 +546,8 @@ struct cgpu_info {
pthread_rwlock_t qlock; pthread_rwlock_t qlock;
struct work *queued_work; struct work *queued_work;
unsigned int queued_count; unsigned int queued_count;
bool shutdown;
}; };
extern bool add_cgpu(struct cgpu_info*); extern bool add_cgpu(struct cgpu_info*);
@ -847,6 +850,7 @@ extern char *opt_avalon_options;
extern char *opt_usb_select; extern char *opt_usb_select;
extern int opt_usbdump; extern int opt_usbdump;
extern bool opt_usb_list_all; extern bool opt_usb_list_all;
extern sem_t usb_resource_sem;
#endif #endif
#ifdef USE_BITFORCE #ifdef USE_BITFORCE
extern bool opt_bfl_noncerange; extern bool opt_bfl_noncerange;
@ -881,6 +885,7 @@ extern int opt_expiry;
#ifdef USE_USBUTILS #ifdef USE_USBUTILS
extern pthread_mutex_t cgusb_lock; extern pthread_mutex_t cgusb_lock;
extern pthread_mutex_t cgusbres_lock;
#endif #endif
extern cglock_t control_lock; extern cglock_t control_lock;
@ -1249,7 +1254,7 @@ struct modminer_fpga_state {
extern void get_datestamp(char *, struct timeval *); extern void get_datestamp(char *, struct timeval *);
extern void inc_hw_errors(struct thr_info *thr); extern void inc_hw_errors(struct thr_info *thr);
extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce); extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
extern struct work *get_queued(struct cgpu_info *cgpu); extern struct work *get_queued(struct cgpu_info *cgpu);
extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen); extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen); extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);

86
usbtest.py

@ -0,0 +1,86 @@
#!/usr/bin/env python2.7
#
# Original version supplied to me (Kano/kanoi) by xiangfu
#
# Modified to allow supplying the data to send
#
# Linux usAge: ./ubstest.py /dev/ttyUSB0 0xhexcodes|string|icarus
# OR python ubstest.py /dev/ttyUSB0 0xhexcodes|string|icarus
#
# Windows usAge: ./ubstest.py COM1 0xhexcodes|string|icarus
#
# sends the data sepcified to the USB device and waits
# for a reply then displays it
#
# the data can be:
# 0xhexcodes: e.g. 0x68656c6c6f20776f726c640a
# would send "hello world\n"
#
# string: e.g. sendsometext
#
# icarus: sends 2 known block payloads for an icarus device
# and shows the expected and actual answers if it's
# a working V3 icarus
import sys
import serial
import binascii
if len(sys.argv) < 2:
sys.stderr.write("usAge: " + sys.argv[0] + " device strings...\n")
sys.stderr.write(" where device is either like /dev/ttyUSB0 or COM1\n")
sys.stderr.write(" and strings are either '0xXXXX' or 'text'\n")
sys.stderr.write(" if the first string is 'icarus' the rest are ignored\n")
sys.stderr.write(" and 2 valid icarus test payloads are sent with results displayed\n")
sys.stderr.write("\nAfter any command is sent it waits up to 30 seconds for a reply\n");
sys.exit("Aborting")
# Open with a 10 second timeout - just to be sure
ser = serial.Serial(sys.argv[1], 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 10, False, False, 5, False, None)
if sys.argv[2] == "icarus":
# This show how Icarus use the block and midstate data
# This will produce nonce 063c5e01
block = "0000000120c8222d0497a7ab44a1a2c7bf39de941c9970b1dc7cdc400000079700000000e88aabe1f353238c668d8a4df9318e614c10c474f8cdf8bc5f6397b946c33d7c4e7242c31a098ea500000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
midstate = "33c5bf5751ec7f7e056443b5aee3800331432c83f404d9de38b94ecbf907b92d"
rdata2 = block.decode('hex')[95:63:-1]
rmid = midstate.decode('hex')[::-1]
payload = rmid + rdata2
print("Push payload to icarus: " + binascii.hexlify(payload))
ser.write(payload)
b=ser.read(4)
print("Result:(should be: 063c5e01): " + binascii.hexlify(b))
# Just another test
payload2 = "ce92099c5a80bb81c52990d5c0924c625fd25a535640607d5a4bdf8174e2c8d500000000000000000000000080000000000000000b290c1a42313b4f21b5bcb8"
print("Push payload to icarus: " + payload2)
ser.write(payload2.decode('hex'))
b=ser.read(4)
print("Result:(should be: 8e0b31c5): " + binascii.hexlify(b))
else:
data = ""
for arg in sys.argv[2::]:
if arg[0:2:] == '0x':
data += arg[2::].decode('hex')
else:
data += arg
print("Sending: 0x" + binascii.hexlify(data))
ser.write(data)
# If you're expecting more than one linefeed terminated reply,
# you'll only see the first one
# AND with no linefeed, this will wait the 10 seconds before returning
print("Waiting up to 10 seconds ...")
b=ser.readline()
print("Result: hex 0x" + binascii.hexlify(b))
# This could mess up the display - do it last
print("Result: asc '" + b + "'")
ser.close()

1311
usbutils.c

File diff suppressed because it is too large Load Diff

152
usbutils.h

@ -12,29 +12,88 @@
#include <libusb.h> #include <libusb.h>
// for 0x0403/0x6014 FT232H (and possibly others?) #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
#define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
// For 0x0403:0x6014/0x6001 FT232H (and possibly others?) - BFL, BAS, BLT, LLT, AVA
#define FTDI_TYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT) #define FTDI_TYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
#define FTDI_TYPE_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
#define FTDI_REQUEST_RESET ((uint8_t)0) #define FTDI_REQUEST_RESET ((uint8_t)0)
#define FTDI_REQUEST_MODEM ((uint8_t)1) #define FTDI_REQUEST_MODEM ((uint8_t)1)
#define FTDI_REQUEST_FLOW ((uint8_t)2) #define FTDI_REQUEST_FLOW ((uint8_t)2)
#define FTDI_REQUEST_BAUD ((uint8_t)3) #define FTDI_REQUEST_BAUD ((uint8_t)3)
#define FTDI_REQUEST_DATA ((uint8_t)4) #define FTDI_REQUEST_DATA ((uint8_t)4)
#define FTDI_REQUEST_LATENCY ((uint8_t)9)
#define FTDI_VALUE_RESET 0 #define FTDI_VALUE_RESET 0
#define FTDI_VALUE_PURGE_RX 1 #define FTDI_VALUE_PURGE_RX 1
#define FTDI_VALUE_PURGE_TX 2 #define FTDI_VALUE_PURGE_TX 2
#define FTDI_VALUE_LATENCY 1
// Baud
#define FTDI_VALUE_BAUD_BFL 0xc068
#define FTDI_INDEX_BAUD_BFL 0x0200
#define FTDI_VALUE_BAUD_BAS FTDI_VALUE_BAUD_BFL
#define FTDI_INDEX_BAUD_BAS FTDI_INDEX_BAUD_BFL
// LLT = BLT (same code)
#define FTDI_VALUE_BAUD_BLT 0x001a
#define FTDI_INDEX_BAUD_BLT 0x0000
// Avalon
#define FTDI_VALUE_BAUD_AVA 0x001A
#define FTDI_INDEX_BAUD_AVA 0x0000
#define FTDI_VALUE_DATA_AVA 8
// CMR = 115200 & 57600
#define FTDI_VALUE_BAUD_CMR_115 0xc068
#define FTDI_INDEX_BAUD_CMR_115 0x0200
#define FTDI_VALUE_BAUD_CMR_57 0x80d0
#define FTDI_INDEX_BAUD_CMR_57 0x0200
// baud with a 0 divisor is 120,000,000/10 // Data control
//#define FTDI_VALUE_BAUD (0) #define FTDI_VALUE_DATA_BFL 0
//#define FTDI_INDEX_BAUD (0) #define FTDI_VALUE_DATA_BAS FTDI_VALUE_DATA_BFL
#define FTDI_VALUE_BAUD 0xc068 // LLT = BLT (same code)
#define FTDI_INDEX_BAUD 0x0200 #define FTDI_VALUE_DATA_BLT 8
#define FTDI_VALUE_DATA 0
#define FTDI_VALUE_FLOW 0 #define FTDI_VALUE_FLOW 0
#define FTDI_VALUE_MODEM 0x0303 #define FTDI_VALUE_MODEM 0x0303
// For 0x10c4:0xea60 USB cp210x chip - AMU
#define CP210X_TYPE_OUT 0x41
#define CP210X_REQUEST_DATA 0x07
#define CP210X_REQUEST_BAUD 0x1e
#define CP210X_VALUE_DATA 0x0303
#define CP210X_DATA_BAUD 0x0001c200
// For 0x067b:0x2303 Prolific PL2303 - ICA
#define PL2303_CTRL_DTR 0x01
#define PL2303_CTRL_RTS 0x02
#define PL2303_CTRL_OUT 0x21
#define PL2303_VENDOR_OUT 0x40
#define PL2303_REQUEST_CTRL 0x22
#define PL2303_REQUEST_LINE 0x20
#define PL2303_REQUEST_VENDOR 0x01
#define PL2303_REPLY_CTRL 0x21
#define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
#define PL2303_VALUE_LINE 0
#define PL2303_VALUE_LINE0 0x0001c200
#define PL2303_VALUE_LINE1 0x080000
#define PL2303_VALUE_LINE_SIZE 7
#define PL2303_VALUE_VENDOR 0
// Use the device defined timeout // Use the device defined timeout
#define DEVTIMEOUT 0 #define DEVTIMEOUT 0
@ -50,9 +109,28 @@ struct usb_endpoints {
bool found; bool found;
}; };
enum sub_ident {
IDENT_UNK = 0,
IDENT_BAJ,
IDENT_BAL,
IDENT_BAS,
IDENT_BAM,
IDENT_BFL,
IDENT_MMQ,
IDENT_AVA,
IDENT_ICA,
IDENT_AMU,
IDENT_BLT,
IDENT_LLT,
IDENT_CMR1,
IDENT_CMR2,
IDENT_ZTX
};
struct usb_find_devices { struct usb_find_devices {
int drv; int drv;
const char *name; const char *name;
enum sub_ident ident;
uint16_t idVendor; uint16_t idVendor;
uint16_t idProduct; uint16_t idProduct;
char *iManufacturer; char *iManufacturer;
@ -65,11 +143,18 @@ struct usb_find_devices {
struct usb_endpoints *eps; struct usb_endpoints *eps;
}; };
enum usb_types {
USB_TYPE_STD = 0,
USB_TYPE_FTDI
};
struct cg_usb_device { struct cg_usb_device {
struct usb_find_devices *found; struct usb_find_devices *found;
libusb_device_handle *handle; libusb_device_handle *handle;
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
struct libusb_device_descriptor *descriptor; struct libusb_device_descriptor *descriptor;
enum usb_types usb_type;
enum sub_ident ident;
uint16_t usbver; uint16_t usbver;
int speed; int speed;
char *prod_string; char *prod_string;
@ -86,6 +171,8 @@ struct cg_usb_info {
bool nodev; bool nodev;
int nodev_count; int nodev_count;
struct timeval last_nodev; struct timeval last_nodev;
uint32_t ioerr_count;
uint32_t continuous_ioerr_count;
}; };
enum usb_cmds { enum usb_cmds {
@ -136,6 +223,16 @@ enum usb_cmds {
C_QUEFLUSH, C_QUEFLUSH,
C_QUEFLUSHREPLY, C_QUEFLUSHREPLY,
C_REQUESTVOLTS, C_REQUESTVOLTS,
C_SENDTESTWORK,
C_LATENCY,
C_SETLINE,
C_VENDOR,
C_AVALON_TASK,
C_AVALON_READ,
C_GET_AVALON_READY,
C_AVALON_RESET,
C_GET_AVALON_RESET,
C_FTDI_STATUS,
C_MAX C_MAX
}; };
@ -150,29 +247,39 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *)); void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
struct api_data *api_usb_stats(int *count); struct api_data *api_usb_stats(int *count);
void update_usb_stats(struct cgpu_info *cgpu); void update_usb_stats(struct cgpu_info *cgpu);
int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce); int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce);
int usb_ftdi_cts(struct cgpu_info *cgpu);
int usb_ftdi_ctw(struct cgpu_info *cgpu);
int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds); int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd); int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd);
int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, enum usb_cmds cmd);
void usb_cleanup(); void usb_cleanup();
void usb_initialise(); void usb_initialise();
void *usb_resource_thread(void *userdata);
#define usb_read(cgpu, buf, bufsiz, read, cmd) \ #define usb_read(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_once(cgpu, buf, bufsiz, read, cmd) \ #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
#define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true)
#define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \ #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
#define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
#define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \ #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false) _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \ #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \ #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false) _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_write(cgpu, buf, bufsiz, wrote, cmd) \ #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
_usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd) _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
@ -186,16 +293,13 @@ void usb_initialise();
#define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \ #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
_usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
#define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \ #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false) _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
#define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
#define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \ #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false) _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
#define usb_transfer(cgpu, typ, req, val, idx, cmd) \ #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
_usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd) _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
#endif #endif

23
util.c

@ -844,6 +844,29 @@ void nmsleep(unsigned int msecs)
#endif #endif
} }
/* Same for usecs */
void nusleep(unsigned int usecs)
{
struct timespec twait, tleft;
int ret;
ldiv_t d;
#ifdef WIN32
timeBeginPeriod(1);
#endif
d = ldiv(usecs, 1000000);
tleft.tv_sec = d.quot;
tleft.tv_nsec = d.rem * 1000;
do {
twait.tv_sec = tleft.tv_sec;
twait.tv_nsec = tleft.tv_nsec;
ret = nanosleep(&twait, &tleft);
} while (ret == -1 && errno == EINTR);
#ifdef WIN32
timeEndPeriod(1);
#endif
}
/* This is a cgminer gettimeofday wrapper. Since we always call gettimeofday /* This is a cgminer gettimeofday wrapper. Since we always call gettimeofday
* with tz set to NULL, and windows' default resolution is only 15ms, this * with tz set to NULL, and windows' default resolution is only 15ms, this
* gives us higher resolution times on windows. */ * gives us higher resolution times on windows. */

1
util.h

@ -58,6 +58,7 @@ int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (
void thr_info_freeze(struct thr_info *thr); void thr_info_freeze(struct thr_info *thr);
void thr_info_cancel(struct thr_info *thr); void thr_info_cancel(struct thr_info *thr);
void nmsleep(unsigned int msecs); void nmsleep(unsigned int msecs);
void nusleep(unsigned int usecs);
void cgtime(struct timeval *tv); void cgtime(struct timeval *tv);
void subtime(struct timeval *a, struct timeval *b); void subtime(struct timeval *a, struct timeval *b);
void addtime(struct timeval *a, struct timeval *b); void addtime(struct timeval *a, struct timeval *b);

Loading…
Cancel
Save