driver-bigpic.c 7.5 KB

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