driver-hashbuster.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. bool hashbuster_lowl_probe(const struct lowlevel_device_info * const info)
  132. {
  133. const char * const product = info->product;
  134. const char * const serial = info->serial;
  135. char * const path = info->path;
  136. hid_device *h;
  137. uint8_t buf[0x40] = {'\xfe'};
  138. if (info->lowl != &lowl_hid)
  139. applogr(false, LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not hid!",
  140. __func__, product, serial);
  141. if (info->vid != 0xFA04 || info->pid != 0x0011)
  142. applogr(false, LOG_WARNING, "%s: Wrong VID/PID", __func__);
  143. h = hid_open_path(path);
  144. if (!h)
  145. applogr(false, LOG_WARNING, "%s: Failed to open HID path %s",
  146. __func__, path);
  147. if ((!hashbuster_io(h, buf, buf)) || buf[1] != 0x07)
  148. applogr(false, LOG_DEBUG, "%s: Identify sequence didn't match on %s",
  149. __func__, path);
  150. struct spi_port spi = {
  151. .txrx = hashbuster_spi_txrx,
  152. .userp = h,
  153. .repr = hashbuster_drv.dname,
  154. .logprio = LOG_DEBUG,
  155. .speed = 100000,
  156. .mode = 0,
  157. };
  158. const int chip_n = libbitfury_detectChips1(&spi);
  159. hid_close(h);
  160. if (lowlevel_claim(&hashbuster_drv, true, info))
  161. return false;
  162. struct cgpu_info *cgpu;
  163. cgpu = malloc(sizeof(*cgpu));
  164. *cgpu = (struct cgpu_info){
  165. .drv = &hashbuster_drv,
  166. .device_data = lowlevel_ref(info),
  167. .threads = 1,
  168. .procs = chip_n,
  169. .device_path = strdup(info->path),
  170. .dev_manufacturer = maybe_strdup(info->manufacturer),
  171. .dev_product = maybe_strdup(product),
  172. .dev_serial = maybe_strdup(serial),
  173. .deven = DEV_ENABLED,
  174. };
  175. return add_cgpu(cgpu);
  176. }
  177. static
  178. bool hashbuster_init(struct thr_info * const thr)
  179. {
  180. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  181. struct bitfury_device *bitfury;
  182. struct spi_port *port;
  183. hid_device *h;
  184. h = hid_open_path(cgpu->device_path);
  185. lowlevel_devinfo_free(cgpu->device_data);
  186. if (!h)
  187. applogr(false, LOG_ERR, "%s: Failed to open hid device", cgpu->dev_repr);
  188. port = malloc(sizeof(*port));
  189. if (!port)
  190. applogr(false, LOG_ERR, "%s: Failed to allocate spi_port", cgpu->dev_repr);
  191. *port = (struct spi_port){
  192. .txrx = hashbuster_spi_txrx,
  193. .userp = h,
  194. .cgpu = cgpu,
  195. .repr = cgpu->dev_repr,
  196. .logprio = LOG_ERR,
  197. .speed = 100000,
  198. .mode = 0,
  199. };
  200. for (proc = cgpu; proc; proc = proc->next_proc)
  201. {
  202. bitfury = malloc(sizeof(*bitfury));
  203. if (!bitfury)
  204. {
  205. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate bitfury_device",
  206. cgpu->proc_repr);
  207. proc->status = LIFE_DEAD2;
  208. continue;
  209. }
  210. *bitfury = (struct bitfury_device){
  211. .spi = port,
  212. };
  213. proc->device_data = bitfury;
  214. bitfury_init_chip(proc);
  215. bitfury->osc6_bits = 53;
  216. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  217. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  218. }
  219. timer_set_now(&thr->tv_poll);
  220. cgpu->status = LIFE_INIT2;
  221. return true;
  222. }
  223. static
  224. bool hashbuster_get_stats(struct cgpu_info * const cgpu)
  225. {
  226. struct cgpu_info *proc;
  227. if (cgpu != cgpu->device)
  228. return true;
  229. struct bitfury_device * const bitfury = cgpu->device_data;
  230. struct spi_port * const spi = bitfury->spi;
  231. hid_device * const h = spi->userp;
  232. uint8_t buf[0x40] = {'\x04'};
  233. if (!hashbuster_io(h, buf, buf))
  234. return false;
  235. if (buf[1])
  236. {
  237. for (proc = cgpu; proc; proc = proc->next_proc)
  238. proc->temp = buf[1];
  239. }
  240. return true;
  241. }
  242. struct device_drv hashbuster_drv = {
  243. .dname = "hashbuster",
  244. .name = "HBR",
  245. .lowl_match = hashbuster_lowl_match,
  246. .lowl_probe = hashbuster_lowl_probe,
  247. .thread_init = hashbuster_init,
  248. .thread_disable = bitfury_disable,
  249. .thread_enable = bitfury_enable,
  250. .thread_shutdown = bitfury_shutdown,
  251. .minerloop = minerloop_async,
  252. .job_prepare = bitfury_job_prepare,
  253. .job_start = bitfury_noop_job_start,
  254. .poll = bitfury_do_io,
  255. .job_process_results = bitfury_job_process_results,
  256. .get_stats = hashbuster_get_stats,
  257. .get_api_extra_device_detail = bitfury_api_device_detail,
  258. .get_api_extra_device_status = bitfury_api_device_status,
  259. .set_device = bitfury_set_device,
  260. #ifdef HAVE_CURSES
  261. .proc_wlogprint_status = bitfury_wlogprint_status,
  262. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  263. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  264. #endif
  265. };