driver-hashbuster.c 7.5 KB

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