driver-bigpic.c 8.1 KB

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