driver-hashbuster2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2013 Vladimir Strinski
  4. * Copyright 2013 HashBuster team
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include "deviceapi.h"
  15. #include "driver-bitfury.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 61
  23. BFG_REGISTER_DRIVER(hashbuster2_drv)
  24. static
  25. bool hashbuster2_io(libusb_device_handle * const h, unsigned char *buf, unsigned char *cmd)
  26. {
  27. const uint8_t cmdbyte = *((uint8_t *)cmd);
  28. int result;
  29. char x[0x81];
  30. bool rv = true;
  31. if (unlikely(opt_dev_protocol))
  32. {
  33. bin2hex(x, cmd, 0x40);
  34. applog(LOG_DEBUG, "%s(%p): SEND: %s", __func__, h, x);
  35. }
  36. do // Workaround for PIC USB buffer corruption. We should repeat last packet if receive FF
  37. {
  38. libusb_bulk_transfer(h, 0x01, cmd, 64, &result, 0);
  39. libusb_bulk_transfer(h, 0x81, buf, 64, &result, 0);
  40. } while(buf[0]==0xFF);
  41. if (unlikely(opt_dev_protocol))
  42. {
  43. bin2hex(x, buf, 0x40);
  44. applog(LOG_DEBUG, "%s(%p): RECV: %s", __func__, h, x);
  45. }
  46. return rv;
  47. }
  48. static
  49. bool hashbuster2_spi_config(libusb_device_handle * const h, const uint8_t mode, const uint8_t miso, const uint32_t freq)
  50. {
  51. uint8_t buf[0x40] = {'\x01', '\x01'};
  52. if (!hashbuster2_io(h, buf, buf))
  53. return false;
  54. return (buf[1] == '\x00');
  55. }
  56. static
  57. bool hashbuster2_spi_disable(libusb_device_handle * const h)
  58. {
  59. uint8_t buf[0x40] = {'\x01', '\x00'};
  60. if (!hashbuster2_io(h, buf, buf))
  61. return false;
  62. return (buf[1] == '\x00');
  63. }
  64. static
  65. bool hashbuster2_spi_reset(libusb_device_handle * const h, uint8_t chips)
  66. {
  67. uint8_t buf[0x40] = {'\x02', '\x00', chips};
  68. if (!hashbuster2_io(h, buf, buf))
  69. return false;
  70. return (buf[1] == '\x00');
  71. }
  72. static
  73. bool hashbuster2_spi_transfer(libusb_device_handle * const h, void * const buf, const void * const data, size_t datasz)
  74. {
  75. if (datasz > HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  76. return false;
  77. uint8_t cbuf[0x40] = {'\x03', '\x00', datasz};
  78. memcpy(&cbuf[3], data, datasz);
  79. if (!hashbuster2_io(h, cbuf, cbuf))
  80. return false;
  81. if (cbuf[2] != datasz)
  82. return false;
  83. memcpy(buf, &cbuf[3], datasz);
  84. return true;
  85. }
  86. static
  87. bool hashbuster2_spi_txrx(struct spi_port * const port)
  88. {
  89. libusb_device_handle * const h = port->userp;
  90. const uint8_t *wrbuf = spi_gettxbuf(port);
  91. uint8_t *rdbuf = spi_getrxbuf(port);
  92. size_t bufsz = spi_getbufsz(port);
  93. hashbuster2_spi_disable(h);
  94. hashbuster2_spi_reset(h, 0x10);
  95. hashbuster2_spi_config(h, port->mode, 0, port->speed);
  96. while (bufsz >= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  97. {
  98. if (!hashbuster2_spi_transfer(h, rdbuf, wrbuf, HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER))
  99. return false;
  100. rdbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  101. wrbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  102. bufsz -= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  103. }
  104. if (bufsz > 0)
  105. {
  106. if (!hashbuster2_spi_transfer(h, rdbuf, wrbuf, bufsz))
  107. return false;
  108. }
  109. return true;
  110. }
  111. static
  112. bool hashbuster2_lowl_match(const struct lowlevel_device_info * const info)
  113. {
  114. return lowlevel_match_id(info, &lowl_usb, 0xFA04, 0x000D);
  115. }
  116. static
  117. bool hashbuster2_lowl_probe(const struct lowlevel_device_info * const info)
  118. {
  119. struct cgpu_info *cgpu = NULL, *proc1 = NULL, *prev_cgpu = NULL;
  120. struct bitfury_device **devicelist, *bitfury;
  121. struct spi_port *port;
  122. int i, j;
  123. int proc_count = 0;
  124. struct bitfury_device dummy_bitfury;
  125. struct cgpu_info dummy_cgpu;
  126. const char * const product = info->product;
  127. const char * const serial = info->serial;
  128. char * const path = info->path;
  129. hid_device *h_lost;
  130. libusb_device_handle *h;
  131. uint8_t buf[0x40] = {'\xfe'};
  132. if (info->lowl != &lowl_usb)
  133. applogr(false, LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb_generic!",
  134. __func__, product, serial);
  135. if (info->vid != 0xFA04 || info->pid != 0x000D)
  136. applogr(false, LOG_WARNING, "%s: Wrong VID/PID", __func__);
  137. libusb_init(NULL);
  138. libusb_set_debug(NULL,3);
  139. libusb_device *dev = info->lowl_data;
  140. libusb_open(dev, &h);
  141. if (libusb_kernel_driver_active(h, 0))
  142. libusb_detach_kernel_driver(h, 0);
  143. libusb_set_configuration(h, 1);
  144. libusb_claim_interface(h, 0);
  145. unsigned char OUTPacket[64];
  146. unsigned char INPacket[64];
  147. signed int result;
  148. OUTPacket[0] = 0xFE;
  149. libusb_bulk_transfer(h, 0x01, OUTPacket, 64, &result, 0);
  150. libusb_bulk_transfer(h, 0x81, INPacket, 64, &result, 0);
  151. if (INPacket[1] == 0x18)
  152. {
  153. do
  154. {
  155. // Turn on miner PSU
  156. OUTPacket[0] = 0x10;
  157. OUTPacket[1] = 0x00;
  158. OUTPacket[2] = 0x01;
  159. libusb_bulk_transfer(h, 0x01, OUTPacket, 64, &result, 0);
  160. libusb_bulk_transfer(h, 0x81, INPacket, 64, &result, 0);
  161. } while (INPacket[0] == 0xFF);
  162. }
  163. int chip_n;
  164. port = malloc(sizeof(*port));
  165. port->cgpu = &dummy_cgpu;
  166. port->txrx = hashbuster2_spi_txrx;
  167. port->userp=h;
  168. port->repr = hashbuster2_drv.dname;
  169. port->logprio = LOG_DEBUG;
  170. port->speed = 100000;
  171. port->mode = 0;
  172. dummy_bitfury.slot = 0;
  173. chip_n = libbitfury_detectChips1(port);
  174. if (chip_n)
  175. {
  176. applog(LOG_WARNING, "BITFURY slot %d: %d chips detected", 0, chip_n);
  177. devicelist = malloc(sizeof(*devicelist) * chip_n);
  178. for (j = 0; j < chip_n; ++j)
  179. {
  180. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  181. *bitfury = (struct bitfury_device){
  182. .spi = port,
  183. .slot = 0,
  184. .fasync = j,
  185. };
  186. }
  187. cgpu = malloc(sizeof(*cgpu));
  188. *cgpu = (struct cgpu_info){
  189. .drv = &hashbuster2_drv,
  190. .procs = chip_n,
  191. .device_data = devicelist,
  192. .cutofftemp = 200,
  193. .threads = 1,
  194. .dev_manufacturer = maybe_strdup(info->manufacturer),
  195. .dev_product = maybe_strdup(product),
  196. .dev_serial = maybe_strdup(serial),
  197. .deven = DEV_ENABLED,
  198. };
  199. }
  200. return add_cgpu(cgpu);
  201. }
  202. static
  203. bool hashbuster2_init(struct thr_info * const thr)
  204. {
  205. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  206. struct spi_port *port;
  207. struct bitfury_device **devicelist;
  208. struct bitfury_device *bitfury;
  209. libusb_device_handle *h;
  210. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  211. {
  212. devicelist = proc->device_data;
  213. bitfury = devicelist[proc->proc_id];
  214. proc->device_data = bitfury;
  215. bitfury->spi->cgpu = proc;
  216. bitfury_init_chip(proc);
  217. bitfury->osc6_bits = 53;
  218. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  219. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  220. if (proc->proc_id == proc->procs - 1)
  221. free(devicelist);
  222. }
  223. timer_set_now(&thr->tv_poll);
  224. cgpu->status = LIFE_INIT2;
  225. return true;
  226. }
  227. static
  228. bool hashbuster2_get_stats(struct cgpu_info * const cgpu)
  229. {
  230. struct cgpu_info *proc;
  231. if (cgpu != cgpu->device)
  232. return true;
  233. struct bitfury_device * const bitfury = cgpu->device_data;
  234. struct spi_port * const spi = bitfury->spi;
  235. libusb_device_handle * const h = spi->userp;
  236. uint8_t buf[0x40] = {'\x04'};
  237. if (!hashbuster2_io(h, buf, buf))
  238. return false;
  239. if (buf[1])
  240. {
  241. for (proc = cgpu; proc; proc = proc->next_proc)
  242. proc->temp = buf[1];
  243. }
  244. return true;
  245. }
  246. struct device_drv hashbuster2_drv = {
  247. .dname = "hashbuster2",
  248. .name = "HBR",
  249. .lowl_match = hashbuster2_lowl_match,
  250. .lowl_probe = hashbuster2_lowl_probe,
  251. .thread_init = hashbuster2_init,
  252. .thread_disable = bitfury_disable,
  253. .thread_enable = bitfury_enable,
  254. .thread_shutdown = bitfury_shutdown,
  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 = hashbuster2_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. };