driver-dualminer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2014 Dualminer Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include "miner.h"
  19. #include "driver-icarus.h"
  20. #include "lowlevel.h"
  21. #include "lowl-vcom.h"
  22. #include "deviceapi.h"
  23. #include "logging.h"
  24. #include "util.h"
  25. #include "gc3355.h"
  26. #ifndef WIN32
  27. #include <sys/ioctl.h>
  28. #else
  29. #include <io.h>
  30. #endif
  31. // mining both Scrypt & SHA2 at the same time with two processes
  32. // SHA2 process must be run first, no arg requirements, first serial port will be used
  33. // Scrypt process must be launched after, --scrypt and --dual-mode args required
  34. bool opt_dual_mode = false;
  35. #define DUALMINER_IO_SPEED 115200
  36. #define DUALMINER_SCRYPT_SM_HASH_TIME 0.00001428571429
  37. #define DUALMINER_SCRYPT_DM_HASH_TIME 0.00003333333333
  38. #define DUALMINER_SHA2_DM_HASH_TIME 0.00000000300000
  39. #define DUALMINER_SCRYPT_READ_COUNT 48 // 4.8s to read
  40. #define DUALMINER_SHA2_READ_COUNT 16 // 1.6s to read
  41. #define DUALMINER_0_9V_SHA2_UNITS 60
  42. #define DUALMINER_1_2V_SHA2_UNITS 0
  43. #define DUALMINER_DM_DEFAULT_FREQUENCY 550
  44. #define DUALMINER_SM_DEFAULT_FREQUENCY 850
  45. static
  46. const char sha2_golden_ob[] =
  47. "55aa0f00a08701004a548fe471fa3a9a"
  48. "1371144556c3f64d2500b4826008fe4b"
  49. "bf7698c94eba7946ce22a72f4f672614"
  50. "1a0b3287";
  51. static
  52. const char sha2_golden_nonce[] = "a2870100";
  53. static
  54. const char scrypt_golden_ob[] =
  55. "55aa1f00000000000000000000000000"
  56. "000000000000000000000000aaaaaaaa"
  57. "711c0000603ebdb6e35b05223c54f815"
  58. "5ac33123006b4192e7aafafbeb9ef654"
  59. "4d2973d700000002069b9f9e3ce8a677"
  60. "8dea3d7a00926cd6eaa9585502c9b83a"
  61. "5601f198d7fbf09be9559d6335ebad36"
  62. "3e4f147a8d9934006963030b4e54c408"
  63. "c837ebc2eeac129852a55fee1b1d88f6"
  64. "000c050000000600";
  65. static
  66. const char scrypt_golden_nonce[] = "dd0c0500";
  67. BFG_REGISTER_DRIVER(dualminer_drv)
  68. static
  69. const struct bfg_set_device_definition dualminer_set_device_funcs[];
  70. // device helper functions
  71. static
  72. void dualminer_teardown_device(int fd)
  73. {
  74. // set data terminal ready (DTR) status
  75. set_serial_dtr(fd, BGV_HIGH);
  76. // set request to send (RTS) status
  77. set_serial_rts(fd, BGV_LOW);
  78. }
  79. static
  80. void dualminer_init_hashrate(struct cgpu_info * const cgpu)
  81. {
  82. int fd = cgpu->device_fd;
  83. struct ICARUS_INFO *info = cgpu->device_data;
  84. // get clear to send (CTS) status
  85. if ((gc3355_get_cts_status(fd) != 1) && // 0.9v - dip-switch set to B
  86. (opt_scrypt))
  87. // adjust hash-rate for voltage
  88. info->Hs = DUALMINER_SCRYPT_DM_HASH_TIME;
  89. }
  90. static
  91. bool dualminer_init(struct thr_info * const thr)
  92. {
  93. struct cgpu_info * const cgpu = thr->cgpu;
  94. if (opt_scrypt)
  95. cgpu->min_nonce_diff = 1./0x10000;
  96. return icarus_init(thr);
  97. }
  98. // runs when job starts and the device has been reset (or first run)
  99. static
  100. void dualminer_init_firstrun(struct cgpu_info *icarus)
  101. {
  102. int fd = icarus->device_fd;
  103. gc3355_init_usbstick(fd, opt_pll_freq, !opt_dual_mode, false);
  104. dualminer_init_hashrate(icarus);
  105. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, scrypt: %d, scrypt only: %d",
  106. icarus->proc_repr,
  107. opt_pll_freq,
  108. opt_scrypt,
  109. opt_scrypt && !opt_dual_mode);
  110. }
  111. // set defaults for options that the user didn't specify
  112. static
  113. void dualminer_set_defaults(int fd)
  114. {
  115. // set opt_sha2_units defaults depending on dip-switch
  116. if (opt_sha2_units == -1)
  117. {
  118. // get clear to send (CTS) status
  119. if (gc3355_get_cts_status(fd) == 1)
  120. opt_sha2_units = DUALMINER_1_2V_SHA2_UNITS; // dip-switch in L position
  121. else
  122. opt_sha2_units = DUALMINER_0_9V_SHA2_UNITS; // dip-switch in B position
  123. }
  124. // set opt_pll_freq defaults depending on dip-switch
  125. if (opt_pll_freq <= 0)
  126. {
  127. // get clear to send (CTS) status
  128. if (gc3355_get_cts_status(fd) == 1)
  129. opt_pll_freq = DUALMINER_SM_DEFAULT_FREQUENCY; // 1.2v - dip-switch in L position
  130. else
  131. opt_pll_freq = DUALMINER_DM_DEFAULT_FREQUENCY; // 0.9v - dip-switch in B position
  132. }
  133. }
  134. // ICARUS_INFO functions - icarus-common.h
  135. // runs after fd is opened but before the device detection code
  136. static
  137. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * __maybe_unused info)
  138. {
  139. dualminer_set_defaults(fd);
  140. gc3355_init_usbstick(fd, opt_pll_freq, !opt_dual_mode, true);
  141. return true;
  142. }
  143. // runs each time a job starts
  144. static
  145. bool dualminer_job_start(struct thr_info * const thr)
  146. {
  147. struct cgpu_info *icarus = thr->cgpu;
  148. struct icarus_state * const state = thr->cgpu_data;
  149. int fd = icarus->device_fd;
  150. if (state->firstrun)
  151. // runs when job starts and the device has been reset (or first run)
  152. dualminer_init_firstrun(icarus);
  153. if (opt_scrypt)
  154. {
  155. if (opt_dual_mode)
  156. gc3355_scrypt_reset(fd);
  157. else
  158. gc3355_scrypt_only_reset(fd);
  159. }
  160. return icarus_job_start(thr);
  161. }
  162. // device detection
  163. static
  164. bool dualminer_detect_one(const char *devpath)
  165. {
  166. struct device_drv *drv = &dualminer_drv;
  167. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  168. if (unlikely(!info))
  169. quit(1, "Failed to malloc ICARUS_INFO");
  170. *info = (struct ICARUS_INFO){
  171. .baud = DUALMINER_IO_SPEED,
  172. .timing_mode = MODE_DEFAULT,
  173. .do_icarus_timing = false,
  174. .nonce_littleendian = true,
  175. .work_division = 2,
  176. .fpga_count = 2,
  177. .detect_init_func = dualminer_detect_init,
  178. .job_start_func = dualminer_job_start
  179. };
  180. if (opt_scrypt)
  181. {
  182. info->golden_ob = (char*)scrypt_golden_ob;
  183. info->golden_nonce = (char*)scrypt_golden_nonce;
  184. info->Hs = DUALMINER_SCRYPT_SM_HASH_TIME;
  185. }
  186. else
  187. {
  188. info->golden_ob = (char*)sha2_golden_ob;
  189. info->golden_nonce = (char*)sha2_golden_nonce;
  190. info->Hs = DUALMINER_SHA2_DM_HASH_TIME;
  191. }
  192. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  193. if (!icarus_detect_custom(devpath, drv, info))
  194. {
  195. free(info);
  196. return false;
  197. }
  198. if (opt_scrypt)
  199. info->read_count = DUALMINER_SCRYPT_READ_COUNT; // 4.8s to read
  200. else
  201. info->read_count = DUALMINER_SHA2_READ_COUNT; // 1.6s to read
  202. return true;
  203. }
  204. // support for --set-device dualminer:dual_mode=1
  205. // most be set before probing the device
  206. static
  207. const char *dualminer_set_dual_mode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  208. {
  209. int val = atoi(setting);
  210. opt_dual_mode = val == 1;
  211. return NULL;
  212. }
  213. static
  214. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  215. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining with two BFGMiner processes"},
  216. {NULL},
  217. };
  218. // device_drv functions - miner.h
  219. static
  220. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  221. {
  222. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  223. }
  224. static
  225. void dualminer_thread_shutdown(struct thr_info *thr)
  226. {
  227. // dualminer teardown
  228. dualminer_teardown_device(thr->cgpu->device_fd);
  229. // icarus teardown
  230. do_icarus_close(thr);
  231. free(thr->cgpu_data);
  232. }
  233. static
  234. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  235. {
  236. struct cgpu_info * const icarus = thr->cgpu;
  237. struct icarus_state * const state = thr->cgpu_data;
  238. struct ICARUS_INFO * const info = icarus->device_data;
  239. memset(state->ob_bin, 0, info->ob_size);
  240. if (opt_scrypt)
  241. gc3355_scrypt_prepare_work(state->ob_bin, work);
  242. else
  243. gc3355_sha2_prepare_work(state->ob_bin, work, false);
  244. return true;
  245. }
  246. // support for --set-device dualminer:clock=freq
  247. static
  248. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  249. {
  250. if (strcasecmp(option, "clock") == 0)
  251. {
  252. int val = atoi(setting);
  253. opt_pll_freq = val;
  254. return NULL;
  255. }
  256. sprintf(replybuf, "Unknown option: %s", option);
  257. return replybuf;
  258. }
  259. // device_drv definition - miner.h
  260. static
  261. void dualminer_drv_init()
  262. {
  263. dualminer_drv = icarus_drv;
  264. dualminer_drv.dname = "dualminer";
  265. dualminer_drv.name = "DMU";
  266. dualminer_drv.supported_algos = POW_SCRYPT | POW_SHA256D;
  267. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  268. dualminer_drv.thread_init = dualminer_init;
  269. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  270. dualminer_drv.job_prepare = dualminer_job_prepare;
  271. dualminer_drv.set_device = dualminer_set_device;
  272. ++dualminer_drv.probe_priority;
  273. }
  274. struct device_drv dualminer_drv =
  275. {
  276. .drv_init = dualminer_drv_init,
  277. };