driver-bitfury.c 7.2 KB

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