driver-dualminer.c 8.2 KB

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