driver-bitfury.c 3.9 KB

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