driver-dualminer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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": scrypt: %d, scrypt only: %d\n", icarus->proc_repr, opt_scrypt, opt_scrypt);
  106. applog(LOG_DEBUG, "%"PRIpreprv": dualminer: Init: pll=%d, sha2num=%d", icarus->proc_repr, opt_pll_freq, opt_sha2_units);
  107. }
  108. // set defaults for options that the user didn't specify
  109. static
  110. void dualminer_set_defaults(int fd)
  111. {
  112. // set opt_sha2_units defaults depending on dip-switch
  113. if (opt_sha2_units == -1)
  114. {
  115. // get clear to send (CTS) status
  116. if (gc3355_get_cts_status(fd) == 1)
  117. opt_sha2_units = DUALMINER_1_2V_SHA2_UNITS; // dip-switch in L position
  118. else
  119. opt_sha2_units = DUALMINER_0_9V_SHA2_UNITS; // dip-switch in B position
  120. }
  121. // set opt_pll_freq defaults depending on dip-switch
  122. if (opt_pll_freq <= 0)
  123. {
  124. // get clear to send (CTS) status
  125. if (gc3355_get_cts_status(fd) == 1)
  126. opt_pll_freq = DUALMINER_SM_DEFAULT_FREQUENCY; // 1.2v - dip-switch in L position
  127. else
  128. opt_pll_freq = DUALMINER_DM_DEFAULT_FREQUENCY; // 0.9v - dip-switch in B position
  129. }
  130. }
  131. // ICARUS_INFO functions - icarus-common.h
  132. // runs after fd is opened but before the device detection code
  133. static
  134. bool dualminer_detect_init(const char *devpath, int fd, struct ICARUS_INFO * __maybe_unused info)
  135. {
  136. dualminer_set_defaults(fd);
  137. gc3355_init_usbstick(fd, opt_pll_freq, !opt_dual_mode, true);
  138. return true;
  139. }
  140. // runs each time a job starts
  141. static
  142. bool dualminer_job_start(struct thr_info * const thr)
  143. {
  144. struct cgpu_info *icarus = thr->cgpu;
  145. struct icarus_state * const state = thr->cgpu_data;
  146. int fd = icarus->device_fd;
  147. if (state->firstrun)
  148. // runs when job starts and the device has been reset (or first run)
  149. dualminer_init_firstrun(icarus);
  150. if (opt_scrypt)
  151. {
  152. if (opt_dual_mode)
  153. gc3355_scrypt_reset(fd);
  154. else
  155. gc3355_scrypt_only_reset(fd);
  156. }
  157. return icarus_job_start(thr);
  158. }
  159. // device detection
  160. static
  161. bool dualminer_detect_one(const char *devpath)
  162. {
  163. struct device_drv *drv = &dualminer_drv;
  164. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  165. if (unlikely(!info))
  166. quit(1, "Failed to malloc ICARUS_INFO");
  167. *info = (struct ICARUS_INFO){
  168. .baud = DUALMINER_IO_SPEED,
  169. .timing_mode = MODE_DEFAULT,
  170. .do_icarus_timing = false,
  171. .nonce_littleendian = true,
  172. .work_division = 2,
  173. .fpga_count = 2,
  174. .detect_init_func = dualminer_detect_init,
  175. .job_start_func = dualminer_job_start
  176. };
  177. if (opt_scrypt)
  178. {
  179. info->golden_ob = (char*)scrypt_golden_ob;
  180. info->golden_nonce = (char*)scrypt_golden_nonce;
  181. info->Hs = DUALMINER_SCRYPT_SM_HASH_TIME;
  182. }
  183. else
  184. {
  185. info->golden_ob = (char*)sha2_golden_ob;
  186. info->golden_nonce = (char*)sha2_golden_nonce;
  187. info->Hs = DUALMINER_SHA2_DM_HASH_TIME;
  188. }
  189. drv_set_defaults(drv, dualminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  190. if (!icarus_detect_custom(devpath, drv, info))
  191. {
  192. free(info);
  193. return false;
  194. }
  195. if (opt_scrypt)
  196. info->read_count = DUALMINER_SCRYPT_READ_COUNT; // 4.8s to read
  197. else
  198. info->read_count = DUALMINER_SHA2_READ_COUNT; // 1.6s to read
  199. return true;
  200. }
  201. // support for --set-device dualminer:dual_mode=1
  202. // most be set before probing the device
  203. static
  204. 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)
  205. {
  206. int val = atoi(setting);
  207. opt_dual_mode = val == 1;
  208. return NULL;
  209. }
  210. static
  211. const struct bfg_set_device_definition dualminer_set_device_funcs[] = {
  212. {"dual_mode", dualminer_set_dual_mode, "set to 1 to enable dual algorithm mining with two BFGMiner processes"},
  213. {NULL},
  214. };
  215. // device_drv functions - miner.h
  216. static
  217. bool dualminer_lowl_probe(const struct lowlevel_device_info * const info)
  218. {
  219. return vcom_lowl_probe_wrapper(info, dualminer_detect_one);
  220. }
  221. static
  222. void dualminer_thread_shutdown(struct thr_info *thr)
  223. {
  224. // dualminer teardown
  225. dualminer_teardown_device(thr->cgpu->device_fd);
  226. // icarus teardown
  227. do_icarus_close(thr);
  228. free(thr->cgpu_data);
  229. }
  230. static
  231. bool dualminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  232. {
  233. struct cgpu_info * const icarus = thr->cgpu;
  234. struct icarus_state * const state = thr->cgpu_data;
  235. struct ICARUS_INFO * const info = icarus->device_data;
  236. memset(state->ob_bin, 0, info->ob_size);
  237. if (opt_scrypt)
  238. gc3355_scrypt_prepare_work(state->ob_bin, work);
  239. else
  240. gc3355_sha2_prepare_work(state->ob_bin, work, false);
  241. return true;
  242. }
  243. // support for --set-device dualminer:clock=freq
  244. static
  245. char *dualminer_set_device(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  246. {
  247. if (strcasecmp(option, "clock") == 0)
  248. {
  249. int val = atoi(setting);
  250. opt_pll_freq = val;
  251. return NULL;
  252. }
  253. sprintf(replybuf, "Unknown option: %s", option);
  254. return replybuf;
  255. }
  256. // device_drv definition - miner.h
  257. static
  258. void dualminer_drv_init()
  259. {
  260. dualminer_drv = icarus_drv;
  261. dualminer_drv.dname = "dualminer";
  262. dualminer_drv.name = "DMU";
  263. dualminer_drv.supported_algos = POW_SCRYPT | POW_SHA256D;
  264. dualminer_drv.lowl_probe = dualminer_lowl_probe;
  265. dualminer_drv.thread_init = dualminer_init;
  266. dualminer_drv.thread_shutdown = dualminer_thread_shutdown;
  267. dualminer_drv.job_prepare = dualminer_job_prepare;
  268. dualminer_drv.set_device = dualminer_set_device;
  269. ++dualminer_drv.probe_priority;
  270. }
  271. struct device_drv dualminer_drv =
  272. {
  273. .drv_init = dualminer_drv_init,
  274. };