driver-bitfury.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 uint32_t decnonce(uint32_t in)
  102. {
  103. uint32_t out;
  104. /* First part load */
  105. out = (in & 0xFF) << 24; in >>= 8;
  106. /* Byte reversal */
  107. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  108. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  109. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  110. out |= (in >> 2)&0x3FFFFF;
  111. /* Extraction */
  112. if (in & 1) out |= (1 << 23);
  113. if (in & 2) out |= (1 << 22);
  114. out -= 0x800004;
  115. return out;
  116. }
  117. static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  118. {
  119. uint32_t nonceoff;
  120. if (test_nonce(work, nonce)) {
  121. submit_nonce(thr, work, nonce);
  122. return true;
  123. }
  124. nonceoff = nonce - 0x400000;
  125. if (test_nonce(work, nonceoff)) {
  126. submit_nonce(thr, work, nonceoff);
  127. return true;
  128. }
  129. nonceoff = nonce - 0x800000;
  130. if (test_nonce(work, nonceoff)) {
  131. submit_nonce(thr, work, nonceoff);
  132. return true;
  133. }
  134. return false;
  135. }
  136. static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
  137. int64_t __maybe_unused max_nonce)
  138. {
  139. struct cgpu_info *bitfury = thr->cgpu;
  140. struct bitfury_info *info = bitfury->device_data;
  141. int amount, i;
  142. char buf[45];
  143. buf[0] = 'W';
  144. memcpy(buf + 1, work->midstate, 32);
  145. memcpy(buf + 33, work->data + 64, 12);
  146. /* New results may spill out from the latest work, making us drop out
  147. * too early so read whatever we get for the first half nonce and then
  148. * look for the results to prev work. */
  149. usb_read_timeout(bitfury, info->buf, 512, &info->tot, 600, C_BFO_GETRES);
  150. /* Now look for the bulk of the previous work results, they will come
  151. * in a batch following the first data. */
  152. usb_read_once_timeout(bitfury, info->buf + info->tot, 7, &amount, 1000, C_BFO_GETRES);
  153. info->tot += amount;
  154. while (amount) {
  155. usb_read_once_timeout(bitfury, info->buf + info->tot, 512, &amount, 10, C_BFO_GETRES);
  156. info->tot += amount;
  157. };
  158. /* Send work */
  159. usb_write(bitfury, buf, 45, &amount, C_BFO_REQWORK);
  160. /* Get response acknowledging work */
  161. usb_read(bitfury, buf, 7, &amount, C_BFO_GETWORK);
  162. /* Only happens on startup */
  163. if (unlikely(!info->prevwork2))
  164. goto cascade;
  165. /* Search for what work the nonce matches in order of likelihood. Last
  166. * entry is end of result marker. */
  167. for (i = 0; i < info->tot - 7; i += 7) {
  168. uint32_t nonce;
  169. /* Ignore state & switched data in results for now. */
  170. memcpy(&nonce, info->buf + i + 3, 4);
  171. nonce = decnonce(nonce);
  172. if (bitfury_checkresults(thr, info->prevwork1, nonce)) {
  173. info->nonces++;
  174. continue;
  175. }
  176. if (bitfury_checkresults(thr, info->prevwork2, nonce)) {
  177. info->nonces++;
  178. continue;
  179. }
  180. if (bitfury_checkresults(thr, work, nonce)) {
  181. info->nonces++;
  182. continue;
  183. }
  184. }
  185. free_work(info->prevwork2);
  186. cascade:
  187. info->prevwork2 = info->prevwork1;
  188. info->prevwork1 = copy_work(work);
  189. work->blk.nonce = 0xffffffff;
  190. if (info->nonces) {
  191. info->nonces--;
  192. return (int64_t)0xffffffff;
  193. }
  194. return 0;
  195. }
  196. static struct api_data *bitfury_api_stats(struct cgpu_info __maybe_unused *cgpu)
  197. {
  198. return NULL;
  199. }
  200. static void bitfury_init(struct cgpu_info __maybe_unused *bitfury)
  201. {
  202. }
  203. static void bitfury_shutdown(struct thr_info __maybe_unused *thr)
  204. {
  205. struct cgpu_info *bitfury = thr->cgpu;
  206. bitfury_close(bitfury);
  207. }
  208. /* Currently hardcoded to BF1 devices */
  209. struct device_drv bitfury_drv = {
  210. .drv_id = DRIVER_BITFURY,
  211. .dname = "bitfury",
  212. .name = "BFO",
  213. .drv_detect = bitfury_detect,
  214. .scanhash = bitfury_scanhash,
  215. .get_api_stats = bitfury_api_stats,
  216. .reinit_device = bitfury_init,
  217. .thread_shutdown = bitfury_shutdown,
  218. .identify_device = bitfury_identify
  219. };