driver-hashbuster.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2013 Vladimir Strinski
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include "deviceapi.h"
  14. #include "driver-bitfury.h"
  15. #include "fpgautils.h"
  16. #include "libbitfury.h"
  17. #include "logging.h"
  18. #include "lowlevel.h"
  19. #include "lowl-hid.h"
  20. #include "miner.h"
  21. #define HASHBUSTER_USB_PRODUCT "HashBuster"
  22. #define HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER 62
  23. BFG_REGISTER_DRIVER(hashbuster_drv)
  24. static
  25. bool hashbuster_io(hid_device * const h, void * const buf, const void * const cmd)
  26. {
  27. const uint8_t cmdbyte = *((uint8_t *)cmd);
  28. char x[0x81];
  29. if (unlikely(opt_dev_protocol))
  30. {
  31. bin2hex(x, cmd, 0x40);
  32. applog(LOG_DEBUG, "%s(%p): SEND: %s", __func__, h, x);
  33. }
  34. const bool rv = likely(
  35. 0x40 == hid_write(h, cmd, 0x40) &&
  36. 0x40 == hid_read (h, buf, 0x40) &&
  37. ((uint8_t *)buf)[0] == cmdbyte
  38. );
  39. if (unlikely(opt_dev_protocol))
  40. {
  41. bin2hex(x, buf, 0x40);
  42. applog(LOG_DEBUG, "%s(%p): RECV: %s", __func__, h, x);
  43. }
  44. return rv;
  45. }
  46. static
  47. bool hashbuster_spi_config(hid_device * const h, const uint8_t mode, const uint8_t miso, const uint32_t freq)
  48. {
  49. uint8_t buf[0x40] = {'\x01', '\x01', mode, miso};
  50. switch (freq)
  51. {
  52. case 100000:
  53. buf[4] = '\0';
  54. break;
  55. case 750000:
  56. buf[4] = '\x01';
  57. break;
  58. case 3000000:
  59. buf[4] = '\x02';
  60. break;
  61. case 12000000:
  62. buf[4] = '\x03';
  63. break;
  64. default:
  65. return false;
  66. }
  67. if (!hashbuster_io(h, buf, buf))
  68. return false;
  69. return (buf[1] == '\x0f');
  70. }
  71. static
  72. bool hashbuster_spi_disable(hid_device * const h)
  73. {
  74. uint8_t buf[0x40] = {'\x01'};
  75. if (!hashbuster_io(h, buf, buf))
  76. return false;
  77. return (buf[1] == '\x0f');
  78. }
  79. static
  80. bool hashbuster_spi_reset(hid_device * const h, uint8_t chips)
  81. {
  82. uint8_t buf[0x40] = {'\x02', chips};
  83. if (!hashbuster_io(h, buf, buf))
  84. return false;
  85. return (buf[1] == '\xff');
  86. }
  87. static
  88. bool hashbuster_spi_transfer(hid_device * const h, void * const buf, const void * const data, size_t datasz)
  89. {
  90. if (datasz > HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  91. return false;
  92. uint8_t cbuf[0x40] = {'\x03', datasz};
  93. memcpy(&cbuf[2], data, datasz);
  94. if (!hashbuster_io(h, cbuf, cbuf))
  95. return false;
  96. if (cbuf[1] != datasz)
  97. return false;
  98. memcpy(buf, &cbuf[2], datasz);
  99. return true;
  100. }
  101. static
  102. bool hashbuster_spi_txrx(struct spi_port * const port)
  103. {
  104. hid_device * const h = port->userp;
  105. const uint8_t *wrbuf = spi_gettxbuf(port);
  106. uint8_t *rdbuf = spi_getrxbuf(port);
  107. size_t bufsz = spi_getbufsz(port);
  108. hashbuster_spi_disable(h);
  109. hashbuster_spi_reset(h, 0x10);
  110. hashbuster_spi_config(h, port->mode, 0, port->speed);
  111. while (bufsz >= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  112. {
  113. if (!hashbuster_spi_transfer(h, rdbuf, wrbuf, HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER))
  114. return false;
  115. rdbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  116. wrbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  117. bufsz -= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  118. }
  119. if (bufsz > 0)
  120. {
  121. if (!hashbuster_spi_transfer(h, rdbuf, wrbuf, bufsz))
  122. return false;
  123. }
  124. return true;
  125. }
  126. static
  127. bool hashbuster_lowl_match(const struct lowlevel_device_info * const info)
  128. {
  129. return lowlevel_match_lowlproduct(info, &lowl_hid, HASHBUSTER_USB_PRODUCT);
  130. }
  131. static
  132. bool hashbuster_lowl_probe(const struct lowlevel_device_info * const info)
  133. {
  134. const char * const product = info->product;
  135. const char * const serial = info->serial;
  136. char * const path = info->path;
  137. hid_device *h;
  138. uint8_t buf[0x40] = {'\xfe'};
  139. if (info->lowl != &lowl_hid)
  140. applogr(false, LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not hid!",
  141. __func__, product, serial);
  142. if (info->vid != 0xFA04 || info->pid != 0x0011)
  143. applogr(false, LOG_WARNING, "%s: Wrong VID/PID", __func__);
  144. h = hid_open_path(path);
  145. if (!h)
  146. applogr(false, LOG_WARNING, "%s: Failed to open HID path %s",
  147. __func__, path);
  148. if ((!hashbuster_io(h, buf, buf)) || buf[1] != 0x07)
  149. applogr(false, LOG_DEBUG, "%s: Identify sequence didn't match on %s",
  150. __func__, path);
  151. struct spi_port spi = {
  152. .txrx = hashbuster_spi_txrx,
  153. .userp = h,
  154. .repr = hashbuster_drv.dname,
  155. .logprio = LOG_DEBUG,
  156. .speed = 100000,
  157. .mode = 0,
  158. };
  159. const int chip_n = libbitfury_detectChips1(&spi);
  160. hid_close(h);
  161. if (lowlevel_claim(&hashbuster_drv, true, info))
  162. return false;
  163. struct cgpu_info *cgpu;
  164. cgpu = malloc(sizeof(*cgpu));
  165. *cgpu = (struct cgpu_info){
  166. .drv = &hashbuster_drv,
  167. .device_data = lowlevel_ref(info),
  168. .threads = 1,
  169. .procs = chip_n,
  170. .device_path = strdup(info->path),
  171. .dev_manufacturer = maybe_strdup(info->manufacturer),
  172. .dev_product = maybe_strdup(product),
  173. .dev_serial = maybe_strdup(serial),
  174. .deven = DEV_ENABLED,
  175. };
  176. return add_cgpu(cgpu);
  177. }
  178. static
  179. bool hashbuster_init(struct thr_info * const thr)
  180. {
  181. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  182. struct bitfury_device *bitfury;
  183. struct spi_port *port;
  184. hid_device *h;
  185. h = hid_open_path(cgpu->device_path);
  186. lowlevel_devinfo_free(cgpu->device_data);
  187. if (!h)
  188. applogr(false, LOG_ERR, "%s: Failed to open hid device", cgpu->dev_repr);
  189. port = malloc(sizeof(*port));
  190. if (!port)
  191. applogr(false, LOG_ERR, "%s: Failed to allocate spi_port", cgpu->dev_repr);
  192. *port = (struct spi_port){
  193. .txrx = hashbuster_spi_txrx,
  194. .userp = h,
  195. .cgpu = cgpu,
  196. .repr = cgpu->dev_repr,
  197. .logprio = LOG_ERR,
  198. .speed = 100000,
  199. .mode = 0,
  200. };
  201. for (proc = cgpu; proc; proc = proc->next_proc)
  202. {
  203. bitfury = malloc(sizeof(*bitfury));
  204. if (!bitfury)
  205. {
  206. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate bitfury_device",
  207. cgpu->proc_repr);
  208. proc->status = LIFE_DEAD2;
  209. continue;
  210. }
  211. *bitfury = (struct bitfury_device){
  212. .spi = port,
  213. };
  214. proc->device_data = bitfury;
  215. bitfury_init_chip(proc);
  216. bitfury->osc6_bits = 53;
  217. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  218. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  219. }
  220. timer_set_now(&thr->tv_poll);
  221. cgpu->status = LIFE_INIT2;
  222. return true;
  223. }
  224. static
  225. bool hashbuster_get_stats(struct cgpu_info * const cgpu)
  226. {
  227. struct cgpu_info *proc;
  228. if (cgpu != cgpu->device)
  229. return true;
  230. struct bitfury_device * const bitfury = cgpu->device_data;
  231. struct spi_port * const spi = bitfury->spi;
  232. hid_device * const h = spi->userp;
  233. uint8_t buf[0x40] = {'\x04'};
  234. if (!hashbuster_io(h, buf, buf))
  235. return false;
  236. if (buf[1])
  237. {
  238. for (proc = cgpu; proc; proc = proc->next_proc)
  239. proc->temp = buf[1];
  240. }
  241. return true;
  242. }
  243. struct device_drv hashbuster_drv = {
  244. .dname = "hashbuster",
  245. .name = "HBR",
  246. .lowl_match = hashbuster_lowl_match,
  247. .lowl_probe = hashbuster_lowl_probe,
  248. .thread_init = hashbuster_init,
  249. .thread_disable = bitfury_disable,
  250. .thread_enable = bitfury_enable,
  251. .thread_shutdown = bitfury_shutdown,
  252. .minerloop = minerloop_async,
  253. .job_prepare = bitfury_job_prepare,
  254. .job_start = bitfury_noop_job_start,
  255. .poll = bitfury_do_io,
  256. .job_process_results = bitfury_job_process_results,
  257. .get_stats = hashbuster_get_stats,
  258. .get_api_extra_device_detail = bitfury_api_device_detail,
  259. .get_api_extra_device_status = bitfury_api_device_status,
  260. .set_device = bitfury_set_device,
  261. #ifdef HAVE_CURSES
  262. .proc_wlogprint_status = bitfury_wlogprint_status,
  263. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  264. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  265. #endif
  266. };