driver-bigpic.c 7.9 KB

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