driver-hashbuster.c 8.0 KB

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