driver-bigpic.c 8.0 KB

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