driver-hashbuster.c 7.7 KB

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