driver-bitfury.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2013 Con Kolivas
  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. #include "config.h"
  10. #include "miner.h"
  11. #include "driver-bitfury.h"
  12. #include "sha2.h"
  13. /* Wait longer 1/3 longer than it would take for a full nonce range */
  14. #define BF1WAIT 1600
  15. struct device_drv bitfury_drv;
  16. static void bitfury_open(struct cgpu_info *bitfury)
  17. {
  18. /* Magic open sequence */
  19. usb_transfer(bitfury, 0x21, 0x22, 0x0003, 0, C_BFO_OPEN);
  20. }
  21. static void bitfury_close(struct cgpu_info *bitfury)
  22. {
  23. /* Magic close sequence */
  24. usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BFO_CLOSE);
  25. }
  26. static void bitfury_empty_buffer(struct cgpu_info *bitfury)
  27. {
  28. char buf[512];
  29. int amount;
  30. do {
  31. usb_read(bitfury, buf, 512, &amount, C_PING);
  32. } while (amount);
  33. }
  34. static void bitfury_identify(struct cgpu_info *bitfury)
  35. {
  36. int amount;
  37. usb_write(bitfury, "L", 1, &amount, C_PING);
  38. }
  39. static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  40. {
  41. struct cgpu_info *bitfury;
  42. struct bitfury_info *info;
  43. char buf[512];
  44. int amount;
  45. bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
  46. if (!usb_init(bitfury, dev, found)) {
  47. bitfury = usb_free_cgpu(bitfury);
  48. goto out;
  49. }
  50. applog(LOG_INFO, "%s%d: Found at %s", bitfury->drv->name,
  51. bitfury->device_id, bitfury->device_path);
  52. info = calloc(sizeof(struct bitfury_info), 1);
  53. if (!info)
  54. quit(1, "Failed to calloc info in bitfury_detect_one");
  55. bitfury->device_data = info;
  56. bitfury_empty_buffer(bitfury);
  57. usb_buffer_enable(bitfury);
  58. bitfury_open(bitfury);
  59. /* Send getinfo request */
  60. usb_write(bitfury, "I", 1, &amount, C_BFO_REQINFO);
  61. usb_read(bitfury, buf, 14, &amount, C_BFO_GETINFO);
  62. if (amount != 14) {
  63. applog(LOG_WARNING, "%s%d: Getinfo received %d bytes",
  64. bitfury->drv->name, bitfury->device_id, amount);
  65. goto out_close;
  66. }
  67. info->version = buf[1];
  68. memcpy(&info->product, buf + 2, 8);
  69. memcpy(&info->serial, buf + 10, 4);
  70. applog(LOG_INFO, "%s%d: Getinfo returned version %d, product %s serial %08x", bitfury->drv->name,
  71. bitfury->device_id, info->version, info->product, info->serial);
  72. bitfury_empty_buffer(bitfury);
  73. /* Send reset request */
  74. usb_write(bitfury, "R", 1, &amount, C_BFO_REQRESET);
  75. usb_read_timeout(bitfury, buf, 7, &amount, BF1WAIT, C_BFO_GETRESET);
  76. if (amount != 7) {
  77. applog(LOG_WARNING, "%s%d: Getreset received %d bytes",
  78. bitfury->drv->name, bitfury->device_id, amount);
  79. goto out_close;
  80. }
  81. applog(LOG_INFO, "%s%d: Getreset returned %s", bitfury->drv->name,
  82. bitfury->device_id, buf);
  83. bitfury_empty_buffer(bitfury);
  84. bitfury_identify(bitfury);
  85. bitfury_empty_buffer(bitfury);
  86. if (!add_cgpu(bitfury))
  87. goto out_close;
  88. update_usb_stats(bitfury);
  89. applog(LOG_INFO, "%s%d: Found at %s",
  90. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  91. return true;
  92. out_close:
  93. bitfury_close(bitfury);
  94. out:
  95. return false;
  96. }
  97. static void bitfury_detect(void)
  98. {
  99. usb_detect(&bitfury_drv, bitfury_detect_one);
  100. }
  101. static bool bitfury_prepare(struct thr_info __maybe_unused *thr)
  102. {
  103. return true;
  104. }
  105. static bool bitfury_fill(struct cgpu_info __maybe_unused *bitfury)
  106. {
  107. return true;
  108. }
  109. static bool rehash(unsigned char *midstate, unsigned m7, unsigned ntime, unsigned nbits, unsigned nnonce)
  110. {
  111. uint8_t in[16];
  112. uint32_t *in32 = (uint32_t *)in;
  113. uint32_t *mid32 = (uint32_t *)midstate;
  114. uint32_t out32[8];
  115. uint8_t *out = (uint8_t *) out32;
  116. sha256_ctx ctx;
  117. memset( &ctx, 0, sizeof(sha256_ctx));
  118. memcpy(ctx.h, mid32, 8*4);
  119. ctx.tot_len = 64;
  120. nnonce = bswap_32(nnonce);
  121. in32[0] = bswap_32(m7);
  122. in32[1] = bswap_32(ntime);
  123. in32[2] = bswap_32(nbits);
  124. in32[3] = nnonce;
  125. sha256_update(&ctx, in, 16);
  126. sha256_final(&ctx, out);
  127. sha256(out, 32, out);
  128. if (out32[7] == 0)
  129. return true;
  130. return false;
  131. }
  132. static uint32_t decnonce(uint32_t in)
  133. {
  134. uint32_t out;
  135. /* First part load */
  136. out = (in & 0xFF) << 24; in >>= 8;
  137. /* Byte reversal */
  138. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  139. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  140. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  141. out |= (in >> 2)&0x3FFFFF;
  142. /* Extraction */
  143. if (in & 1) out |= (1 << 23);
  144. if (in & 2) out |= (1 << 22);
  145. out -= 0x800004;
  146. return out;
  147. }
  148. static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  149. {
  150. uint32_t nonceoff;
  151. if (test_nonce(work, nonce)) {
  152. submit_nonce(thr, work, nonce);
  153. return true;
  154. }
  155. nonceoff = nonce - 0x400000;
  156. if (test_nonce(work, nonceoff)) {
  157. submit_nonce(thr, work, nonceoff);
  158. return true;
  159. }
  160. nonceoff = nonce - 0x800000;
  161. if (test_nonce(work, nonceoff)) {
  162. submit_nonce(thr, work, nonceoff);
  163. return true;
  164. }
  165. nonceoff = nonce + 0x2800000;
  166. if (test_nonce(work, nonceoff)) {
  167. submit_nonce(thr, work, nonceoff);
  168. return true;
  169. }
  170. nonceoff = nonce + 0x2C800000;
  171. if (test_nonce(work, nonceoff)) {
  172. submit_nonce(thr, work, nonceoff);
  173. return true;
  174. }
  175. nonceoff = nonce + 0x400000;
  176. if (test_nonce(work, nonceoff)) {
  177. submit_nonce(thr, work, nonceoff);
  178. return true;
  179. }
  180. return false;
  181. }
  182. static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
  183. int64_t __maybe_unused max_nonce)
  184. {
  185. struct cgpu_info *bitfury = thr->cgpu;
  186. struct bitfury_info *info = bitfury->device_data;
  187. char sendbuf[45], buf[512];
  188. int64_t hashes = 0;
  189. int amount, i, tot;
  190. sendbuf[0] = 'W';
  191. memcpy(sendbuf + 1, work->midstate, 32);
  192. memcpy(sendbuf + 33, work->data + 64, 12);
  193. usb_write(bitfury, sendbuf, 45, &amount, C_BFO_REQWORK);
  194. usb_read(bitfury, buf, 7, &amount, C_BFO_GETWORK);
  195. if (unlikely(!info->prevwork)) {
  196. info->prevwork = copy_work(work);
  197. return 0;
  198. }
  199. usb_read_once_timeout(bitfury, buf, 7, &amount, BF1WAIT, C_BFO_GETRES);
  200. tot = amount;
  201. while (amount) {
  202. usb_read_once_timeout(bitfury, buf + tot, 512, &amount, 10, C_BFO_GETRES);
  203. tot += amount;
  204. }
  205. for (i = 0; i < tot; i += 7) {
  206. uint32_t nonce;
  207. /* Ignore state & switched data in results for now. */
  208. memcpy(&nonce, buf + i + 3, 4);
  209. nonce = decnonce(nonce);
  210. if (bitfury_checkresults(thr, work, nonce)) {
  211. hashes += 0xffffffff;
  212. continue;
  213. }
  214. if (bitfury_checkresults(thr, info->prevwork, nonce))
  215. hashes += 0xffffffff;
  216. }
  217. free_work(info->prevwork);
  218. info->prevwork = copy_work(work);
  219. work->blk.nonce = 0xffffffff;
  220. return hashes;
  221. }
  222. static void bitfury_flush_work(struct cgpu_info __maybe_unused *bitfury)
  223. {
  224. }
  225. static struct api_data *bitfury_api_stats(struct cgpu_info __maybe_unused *cgpu)
  226. {
  227. return NULL;
  228. }
  229. static void get_bitfury_statline_before(char __maybe_unused *buf, size_t __maybe_unused bufsiz,
  230. struct cgpu_info __maybe_unused *bitfury)
  231. {
  232. }
  233. static void bitfury_init(struct cgpu_info __maybe_unused *bitfury)
  234. {
  235. }
  236. static void bitfury_shutdown(struct thr_info __maybe_unused *thr)
  237. {
  238. struct cgpu_info *bitfury = thr->cgpu;
  239. bitfury_close(bitfury);
  240. }
  241. /* Currently hardcoded to BF1 devices */
  242. struct device_drv bitfury_drv = {
  243. .drv_id = DRIVER_BITFURY,
  244. .dname = "bitfury",
  245. .name = "BFO",
  246. .drv_detect = bitfury_detect,
  247. .scanhash = bitfury_scanhash,
  248. .flush_work = bitfury_flush_work,
  249. .get_api_stats = bitfury_api_stats,
  250. .thread_prepare = bitfury_prepare,
  251. .get_statline_before = get_bitfury_statline_before,
  252. .reinit_device = bitfury_init,
  253. .thread_shutdown = bitfury_shutdown,
  254. .identify_device = bitfury_identify
  255. };