driver-bitfury.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. struct device_drv bitfury_drv;
  13. static void bitfury_open(struct cgpu_info *bitfury)
  14. {
  15. /* Magic open sequence */
  16. usb_transfer(bitfury, 0x21, 0x22, 0x0003, 0, C_BFO_OPEN);
  17. }
  18. static void bitfury_close(struct cgpu_info *bitfury)
  19. {
  20. /* Magic close sequence */
  21. usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BFO_CLOSE);
  22. }
  23. static void bitfury_empty_buffer(struct cgpu_info *bitfury)
  24. {
  25. char buf[512];
  26. int amount;
  27. do {
  28. usb_read(bitfury, buf, 512, &amount, C_PING);
  29. } while (amount);
  30. }
  31. static void bitfury_identify(struct cgpu_info *bitfury)
  32. {
  33. int amount;
  34. usb_write(bitfury, "L", 1, &amount, C_PING);
  35. }
  36. static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  37. {
  38. struct cgpu_info *bitfury;
  39. struct bitfury_info *info;
  40. char buf[512];
  41. int amount;
  42. bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
  43. if (!usb_init(bitfury, dev, found)) {
  44. bitfury = usb_free_cgpu(bitfury);
  45. goto out;
  46. }
  47. applog(LOG_INFO, "%s%d: Found at %s", bitfury->drv->name,
  48. bitfury->device_id, bitfury->device_path);
  49. info = calloc(sizeof(struct bitfury_info), 1);
  50. if (!info)
  51. quit(1, "Failed to calloc info in bitfury_detect_one");
  52. bitfury->device_data = info;
  53. bitfury_empty_buffer(bitfury);
  54. usb_buffer_enable(bitfury);
  55. bitfury_open(bitfury);
  56. /* Send getinfo request */
  57. usb_write(bitfury, "I", 1, &amount, C_BFO_REQINFO);
  58. usb_read(bitfury, buf, 14, &amount, C_BFO_GETINFO);
  59. if (amount != 14) {
  60. applog(LOG_WARNING, "%s%d: Getinfo received %d bytes",
  61. bitfury->drv->name, bitfury->device_id, amount);
  62. goto out_close;
  63. }
  64. info->version = buf[1];
  65. memcpy(&info->product, buf + 2, 8);
  66. memcpy(&info->serial, buf + 10, 4);
  67. applog(LOG_INFO, "%s%d: Getinfo returned version %d, product %s serial %08x", bitfury->drv->name,
  68. bitfury->device_id, info->version, info->product, info->serial);
  69. bitfury_empty_buffer(bitfury);
  70. /* Send reset request */
  71. usb_write(bitfury, "R", 1, &amount, C_BFO_REQRESET);
  72. usb_read_timeout(bitfury, buf, 7, &amount, 1000, C_BFO_GETRESET);
  73. if (amount != 7) {
  74. applog(LOG_WARNING, "%s%d: Getreset received %d bytes",
  75. bitfury->drv->name, bitfury->device_id, amount);
  76. goto out_close;
  77. }
  78. applog(LOG_INFO, "%s%d: Getreset returned %s", bitfury->drv->name,
  79. bitfury->device_id, buf);
  80. bitfury_empty_buffer(bitfury);
  81. bitfury_identify(bitfury);
  82. bitfury_empty_buffer(bitfury);
  83. //return true;
  84. out_close:
  85. bitfury_close(bitfury);
  86. out:
  87. return false;
  88. }
  89. static void bitfury_detect(void)
  90. {
  91. usb_detect(&bitfury_drv, bitfury_detect_one);
  92. }
  93. static bool bitfury_prepare(struct thr_info __maybe_unused *thr)
  94. {
  95. return false;
  96. }
  97. static bool bitfury_fill(struct cgpu_info __maybe_unused *bitfury)
  98. {
  99. return true;
  100. }
  101. static int64_t bitfury_scanhash(struct thr_info __maybe_unused *thr)
  102. {
  103. return 0;
  104. }
  105. static void bitfury_flush_work(struct cgpu_info __maybe_unused *bitfury)
  106. {
  107. }
  108. static struct api_data *bitfury_api_stats(struct cgpu_info __maybe_unused *cgpu)
  109. {
  110. return NULL;
  111. }
  112. static void get_bitfury_statline_before(char __maybe_unused *buf, size_t __maybe_unused bufsiz,
  113. struct cgpu_info __maybe_unused *bitfury)
  114. {
  115. }
  116. static void bitfury_init(struct cgpu_info __maybe_unused *bitfury)
  117. {
  118. }
  119. static void bitfury_shutdown(struct thr_info __maybe_unused *thr)
  120. {
  121. struct cgpu_info *bitfury = thr->cgpu;
  122. bitfury_close(bitfury);
  123. }
  124. /* Currently hardcoded to BF1 devices */
  125. struct device_drv bitfury_drv = {
  126. .drv_id = DRIVER_BITFURY,
  127. .dname = "bitfury",
  128. .name = "BFO",
  129. .drv_detect = bitfury_detect,
  130. .thread_prepare = bitfury_prepare,
  131. .hash_work = hash_queued_work,
  132. .queue_full = bitfury_fill,
  133. .scanwork = bitfury_scanhash,
  134. .flush_work = bitfury_flush_work,
  135. .get_api_stats = bitfury_api_stats,
  136. .get_statline_before = get_bitfury_statline_before,
  137. .reinit_device = bitfury_init,
  138. .thread_shutdown = bitfury_shutdown,
  139. .identify_device = bitfury_identify
  140. };