driver-bigpic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright 2013 Andreas Auer
  3. * Copyright 2013 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. /*
  11. * Big Picture Mining USB miner with Bitfury ASIC
  12. */
  13. #include "config.h"
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include "miner.h"
  17. #include "fpgautils.h"
  18. #include "logging.h"
  19. #include "libbitfury.h"
  20. #include "lowlevel.h"
  21. #include "deviceapi.h"
  22. #include "sha2.h"
  23. #include "driver-bigpic.h"
  24. #include <stdio.h>
  25. BFG_REGISTER_DRIVER(bigpic_drv)
  26. static
  27. bool bigpic_lowl_match(const struct lowlevel_device_info * const info)
  28. {
  29. return lowlevel_match_lowlproduct(info, &lowl_vcom, "Bitfury", "BF1");
  30. }
  31. //------------------------------------------------------------------------------
  32. static bool bigpic_detect_custom(const char *devpath, struct device_drv *api, struct bigpic_info *info)
  33. {
  34. int fd = serial_open(devpath, info->baud, 1, true);
  35. if(fd < 0)
  36. {
  37. return false;
  38. }
  39. char buf[sizeof(struct bigpic_identity)+1];
  40. int len;
  41. if (1 != write(fd, "I", 1))
  42. {
  43. applog(LOG_ERR, "%s: Failed writing id request to %s",
  44. bigpic_drv.dname, devpath);
  45. serial_close(fd);
  46. return false;
  47. }
  48. len = serial_read(fd, buf, sizeof(buf));
  49. if(len != 14)
  50. {
  51. serial_close(fd);
  52. return false;
  53. }
  54. info->id.version = buf[1];
  55. memcpy(info->id.product, buf+2, 8);
  56. memcpy(&info->id.serial, buf+10, 4);
  57. info->id.serial = le32toh(info->id.serial);
  58. applog(LOG_DEBUG, "%s: %s: %d, %s %08x",
  59. bigpic_drv.dname,
  60. devpath,
  61. info->id.version, info->id.product, info->id.serial);
  62. char buf_state[sizeof(struct bigpic_state)+1];
  63. len = 0;
  64. if (1 != write(fd, "R", 1))
  65. {
  66. applog(LOG_ERR, "%s: Failed writing reset request to %s",
  67. bigpic_drv.dname, devpath);
  68. serial_close(fd);
  69. return false;
  70. }
  71. int limit = 50;
  72. while (len == 0 && --limit)
  73. {
  74. len = serial_read(fd, buf, sizeof(buf_state));
  75. cgsleep_ms(100);
  76. }
  77. serial_close(fd);
  78. if(len != 7)
  79. {
  80. applog(LOG_ERR, "%s: %s not responding to reset: %d",
  81. bigpic_drv.dname,
  82. devpath, len);
  83. return false;
  84. }
  85. if (serial_claim_v(devpath, api))
  86. return false;
  87. struct cgpu_info *bigpic;
  88. bigpic = calloc(1, sizeof(struct cgpu_info));
  89. bigpic->drv = api;
  90. bigpic->device_path = strdup(devpath);
  91. bigpic->device_fd = -1;
  92. bigpic->threads = 1;
  93. add_cgpu(bigpic);
  94. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bigpic->proc_repr, devpath);
  95. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  96. bigpic->proc_repr, info->baud);
  97. bigpic->device_data = info;
  98. return true;
  99. }
  100. //------------------------------------------------------------------------------
  101. static bool bigpic_detect_one(const char *devpath)
  102. {
  103. struct bigpic_info *info = calloc(1, sizeof(struct bigpic_info));
  104. if (unlikely(!info))
  105. quit(1, "Failed to malloc bigpicInfo");
  106. info->baud = BPM_BAUD;
  107. if (!bigpic_detect_custom(devpath, &bigpic_drv, info))
  108. {
  109. free(info);
  110. return false;
  111. }
  112. return true;
  113. }
  114. static
  115. bool bigpic_lowl_probe(const struct lowlevel_device_info * const info)
  116. {
  117. return vcom_lowl_probe_wrapper(info, bigpic_detect_one);
  118. }
  119. //------------------------------------------------------------------------------
  120. static bool bigpic_init(struct thr_info *thr)
  121. {
  122. struct cgpu_info *bigpic = thr->cgpu;
  123. struct bigpic_info *info = (struct bigpic_info *)bigpic->device_data;
  124. applog(LOG_DEBUG, "%"PRIpreprv": init", bigpic->proc_repr);
  125. int fd = serial_open(bigpic->device_path, info->baud, 1, true);
  126. if (unlikely(-1 == fd))
  127. {
  128. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s",
  129. bigpic->proc_repr, bigpic->device_path);
  130. return false;
  131. }
  132. bigpic->device_fd = fd;
  133. applog(LOG_INFO, "%"PRIpreprv": Opened %s", bigpic->proc_repr, bigpic->device_path);
  134. info->tx_buffer[0] = 'W';
  135. return true;
  136. }
  137. //------------------------------------------------------------------------------
  138. static bool duplicate(uint32_t *results, uint32_t size, uint32_t test_nonce)
  139. {
  140. for(uint32_t i=0; i<size; i++)
  141. {
  142. if(results[i] == test_nonce)
  143. {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. //------------------------------------------------------------------------------
  150. static void bigpic_process_results(struct thr_info *thr, struct work *work)
  151. {
  152. struct cgpu_info *board = thr->cgpu;
  153. struct bigpic_info *info = (struct bigpic_info *)board->device_data;
  154. uint32_t results[16*6];
  155. uint32_t num_results;
  156. int hwe = 0;
  157. uint32_t m7 = *((uint32_t *)&work->data[64]);
  158. uint32_t ntime = *((uint32_t *)&work->data[68]);
  159. uint32_t nbits = *((uint32_t *)&work->data[72]);
  160. num_results = 0;
  161. for(int i=0; i<info->rx_len; i+=7)
  162. {
  163. struct bigpic_state state;
  164. state.state = info->rx_buffer[i + 1];
  165. state.switched = info->rx_buffer[i + 2];
  166. memcpy(&state.nonce, info->rx_buffer + i + 3, 4);
  167. if(duplicate(results, num_results, state.nonce))
  168. continue;
  169. state.nonce = le32toh(state.nonce);
  170. uint32_t nonce = bitfury_decnonce(state.nonce);
  171. results[num_results++] = state.nonce;
  172. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c State: %c Switched: %d Nonce: %08lx",
  173. board->proc_repr,
  174. (unsigned long)info->rx_len, info->rx_buffer[i], state.state, state.switched, (unsigned long)nonce);
  175. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  176. submit_nonce(thr, work, nonce);
  177. else
  178. if (info->rx_buffer[i + 3] != '\xe0' || hwe++)
  179. inc_hw_errors(thr, work, nonce);
  180. }
  181. }
  182. static
  183. bool bigpic_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  184. {
  185. struct cgpu_info *board = thr->cgpu;
  186. struct bigpic_info *info = (struct bigpic_info *)board->device_data;
  187. memcpy(&info->tx_buffer[ 1], work->midstate, 32);
  188. memcpy(&info->tx_buffer[33], &work->data[64], 12);
  189. work->blk.nonce = 0xffffffff;
  190. return true;
  191. }
  192. static
  193. void bigpic_job_start(struct thr_info *thr)
  194. {
  195. struct cgpu_info *board = thr->cgpu;
  196. struct bigpic_info *info = (struct bigpic_info *)board->device_data;
  197. if (opt_dev_protocol && opt_debug)
  198. {
  199. char hex[91];
  200. bin2hex(hex, info->tx_buffer, 45);
  201. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  202. board->proc_repr, hex);
  203. }
  204. if (45 != write(board->device_fd, info->tx_buffer, 45))
  205. {
  206. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  207. dev_error(board, REASON_DEV_COMMS_ERROR);
  208. job_start_abort(thr, true);
  209. return;
  210. }
  211. while(1)
  212. {
  213. uint8_t buffer[7];
  214. int len;
  215. len = serial_read(board->device_fd, buffer, 7);
  216. if(len > 0)
  217. break;
  218. }
  219. applog(LOG_DEBUG, "%"PRIpreprv": Work Task sent", board->proc_repr);
  220. while(1)
  221. {
  222. info->rx_len = serial_read(board->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  223. if(info->rx_len > 0)
  224. break;
  225. }
  226. applog(LOG_DEBUG, "%"PRIpreprv": Work Task accepted", board->proc_repr);
  227. applog(LOG_DEBUG, "%"PRIpreprv": Nonces sent back: %d",
  228. board->proc_repr, info->rx_len / 7);
  229. mt_job_transition(thr);
  230. // TODO: Delay morework until right before it's needed
  231. timer_set_now(&thr->tv_morework);
  232. job_start_complete(thr);
  233. }
  234. static
  235. int64_t bigpic_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  236. {
  237. // FIXME: not sure how to handle stopping
  238. bigpic_process_results(thr, work);
  239. return 0xBD000000;
  240. }
  241. //------------------------------------------------------------------------------
  242. static void bigpic_shutdown(struct thr_info *thr)
  243. {
  244. struct cgpu_info *cgpu = thr->cgpu;
  245. serial_close(cgpu->device_fd);
  246. }
  247. //------------------------------------------------------------------------------
  248. static bool bigpic_identify(struct cgpu_info *cgpu)
  249. {
  250. char buf[] = "L";
  251. if (sizeof(buf) != write(cgpu->device_fd, buf, sizeof(buf)))
  252. return false;
  253. return true;
  254. }
  255. //------------------------------------------------------------------------------
  256. struct device_drv bigpic_drv = {
  257. .dname = "bigpic",
  258. .name = "BPM",
  259. .probe_priority = -110,
  260. .lowl_match = bigpic_lowl_match,
  261. .lowl_probe = bigpic_lowl_probe,
  262. .identify_device = bigpic_identify,
  263. .thread_init = bigpic_init,
  264. .minerloop = minerloop_async,
  265. .job_prepare = bigpic_job_prepare,
  266. .job_start = bigpic_job_start,
  267. .job_process_results = bigpic_job_process_results,
  268. .thread_shutdown = bigpic_shutdown,
  269. };