driver-hashbuster.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. applogr(false, LOG_DEBUG, "%s: Identify sequence didn't match on %s",
  160. __func__, path);
  161. const int chip_n = hashbuster_chip_count(h);
  162. hid_close(h);
  163. if (bfg_claim_hid(&hashbuster_drv, true, info->path))
  164. return false;
  165. struct cgpu_info *cgpu;
  166. cgpu = malloc(sizeof(*cgpu));
  167. *cgpu = (struct cgpu_info){
  168. .drv = &hashbuster_drv,
  169. .device_data = info,
  170. .threads = 1,
  171. .procs = chip_n,
  172. .device_path = strdup(info->path),
  173. .dev_manufacturer = maybe_strdup(info->manufacturer),
  174. .dev_product = maybe_strdup(product),
  175. .dev_serial = maybe_strdup(serial),
  176. .deven = DEV_ENABLED,
  177. };
  178. return add_cgpu(cgpu);
  179. }
  180. static bool hashbuster_detect_one(const char *serial)
  181. {
  182. return lowlevel_detect_serial(hashbuster_foundlowl, serial);
  183. }
  184. static int hashbuster_detect_auto()
  185. {
  186. return lowlevel_detect(hashbuster_foundlowl, HASHBUSTER_USB_PRODUCT);
  187. }
  188. static void hashbuster_detect()
  189. {
  190. serial_detect_auto(&hashbuster_drv, hashbuster_detect_one, hashbuster_detect_auto);
  191. }
  192. static
  193. bool hashbuster_init(struct thr_info * const thr)
  194. {
  195. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  196. struct bitfury_device *bitfury;
  197. struct spi_port *port;
  198. hid_device *h;
  199. h = hid_open_path(cgpu->device_path);
  200. lowlevel_devinfo_free(cgpu->device_data);
  201. if (!h)
  202. applogr(false, LOG_ERR, "%s: Failed to open hid device", cgpu->dev_repr);
  203. port = malloc(sizeof(*port));
  204. if (!port)
  205. applogr(false, LOG_ERR, "%s: Failed to allocate spi_port", cgpu->dev_repr);
  206. /* Be careful, read spidevc.h comments for warnings */
  207. memset(port, 0, sizeof(*port));
  208. port->txrx = hashbuster_spi_txrx;
  209. port->userp = h;
  210. port->cgpu = cgpu;
  211. port->repr = cgpu->dev_repr;
  212. port->logprio = LOG_ERR;
  213. port->speed = 100000;
  214. port->mode = 0;
  215. for (proc = cgpu; proc; proc = proc->next_proc)
  216. {
  217. bitfury = malloc(sizeof(*bitfury));
  218. if (!bitfury)
  219. {
  220. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate bitfury_device",
  221. cgpu->proc_repr);
  222. proc->status = LIFE_DEAD2;
  223. continue;
  224. }
  225. *bitfury = (struct bitfury_device){
  226. .spi = port,
  227. };
  228. proc->device_data = bitfury;
  229. bitfury_init_chip(proc);
  230. bitfury->osc6_bits = 53;
  231. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  232. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  233. }
  234. timer_set_now(&thr->tv_poll);
  235. cgpu->status = LIFE_INIT2;
  236. return true;
  237. }
  238. static
  239. bool hashbuster_get_stats(struct cgpu_info * const cgpu)
  240. {
  241. struct cgpu_info *proc;
  242. if (cgpu != cgpu->device)
  243. return true;
  244. struct bitfury_device * const bitfury = cgpu->device_data;
  245. struct spi_port * const spi = bitfury->spi;
  246. hid_device * const h = spi->userp;
  247. uint8_t buf[0x40] = {'\x04'};
  248. if (!hashbuster_io(h, buf, buf))
  249. return false;
  250. if (buf[1])
  251. {
  252. for (proc = cgpu; proc; proc = proc->next_proc)
  253. proc->temp = buf[1];
  254. }
  255. return true;
  256. }
  257. struct device_drv hashbuster_drv = {
  258. .dname = "hashbuster",
  259. .name = "HBR",
  260. .drv_detect = hashbuster_detect,
  261. .thread_init = hashbuster_init,
  262. .thread_disable = bitfury_disable,
  263. .thread_enable = bitfury_enable,
  264. .thread_shutdown = bitfury_shutdown,
  265. .minerloop = minerloop_async,
  266. .job_prepare = bitfury_job_prepare,
  267. .job_start = bitfury_noop_job_start,
  268. .poll = bitfury_do_io,
  269. .job_process_results = bitfury_job_process_results,
  270. .get_stats = hashbuster_get_stats,
  271. .get_api_extra_device_detail = bitfury_api_device_detail,
  272. .get_api_extra_device_status = bitfury_api_device_status,
  273. .set_device = bitfury_set_device,
  274. #ifdef HAVE_CURSES
  275. .proc_wlogprint_status = bitfury_wlogprint_status,
  276. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  277. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  278. #endif
  279. };