driver-hashbusterusb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <string.h>
  15. #include "deviceapi.h"
  16. #include "driver-bitfury.h"
  17. #include "libbitfury.h"
  18. #include "logging.h"
  19. #include "lowlevel.h"
  20. #include "lowl-usb.h"
  21. #include "miner.h"
  22. #define HASHBUSTER_USB_PRODUCT "HashBuster"
  23. #define HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER 61
  24. BFG_REGISTER_DRIVER(hashbusterusb_drv)
  25. static
  26. bool hashbusterusb_io(struct lowl_usb_endpoint * const h, unsigned char *buf, unsigned char *cmd)
  27. {
  28. char x[0x81];
  29. bool rv = true;
  30. if (unlikely(opt_dev_protocol))
  31. {
  32. bin2hex(x, cmd, 0x40);
  33. applog(LOG_DEBUG, "%s(%p): SEND: %s", __func__, h, x);
  34. }
  35. do // Workaround for PIC USB buffer corruption. We should repeat last packet if receive FF
  36. {
  37. do
  38. {
  39. usb_write(h, cmd, 64);
  40. } while (usb_read(h, buf, 64) != 64);
  41. } while(buf[0]==0xFF);
  42. if (unlikely(opt_dev_protocol))
  43. {
  44. bin2hex(x, buf, 0x40);
  45. applog(LOG_DEBUG, "%s(%p): RECV: %s", __func__, h, x);
  46. }
  47. return rv;
  48. }
  49. static
  50. bool hashbusterusb_spi_config(struct lowl_usb_endpoint * const h, const uint8_t mode, const uint8_t miso, const uint32_t freq)
  51. {
  52. uint8_t buf[0x40] = {'\x01', '\x01'};
  53. if (!hashbusterusb_io(h, buf, buf))
  54. return false;
  55. return (buf[1] == '\x00');
  56. }
  57. static
  58. bool hashbusterusb_spi_disable(struct lowl_usb_endpoint * const h)
  59. {
  60. uint8_t buf[0x40] = {'\x01', '\x00'};
  61. if (!hashbusterusb_io(h, buf, buf))
  62. return false;
  63. return (buf[1] == '\x00');
  64. }
  65. static
  66. bool hashbusterusb_spi_reset(struct lowl_usb_endpoint * const h, uint8_t chips)
  67. {
  68. uint8_t buf[0x40] = {'\x02', '\x00', chips};
  69. if (!hashbusterusb_io(h, buf, buf))
  70. return false;
  71. return (buf[1] == '\x00');
  72. }
  73. static
  74. bool hashbusterusb_spi_transfer(struct lowl_usb_endpoint * const h, void * const buf, const void * const data, size_t datasz)
  75. {
  76. if (datasz > HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  77. return false;
  78. uint8_t cbuf[0x40] = {'\x03', '\x00', datasz};
  79. memcpy(&cbuf[3], data, datasz);
  80. if (!hashbusterusb_io(h, cbuf, cbuf))
  81. return false;
  82. if (cbuf[2] != datasz)
  83. return false;
  84. memcpy(buf, &cbuf[3], datasz);
  85. return true;
  86. }
  87. static
  88. bool hashbusterusb_spi_txrx(struct spi_port * const port)
  89. {
  90. struct lowl_usb_endpoint * const h = port->userp;
  91. const uint8_t *wrbuf = spi_gettxbuf(port);
  92. uint8_t *rdbuf = spi_getrxbuf(port);
  93. size_t bufsz = spi_getbufsz(port);
  94. hashbusterusb_spi_disable(h);
  95. hashbusterusb_spi_reset(h, 0x10);
  96. hashbusterusb_spi_config(h, port->mode, 0, port->speed);
  97. while (bufsz >= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  98. {
  99. if (!hashbusterusb_spi_transfer(h, rdbuf, wrbuf, HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER))
  100. return false;
  101. rdbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  102. wrbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  103. bufsz -= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  104. }
  105. if (bufsz > 0)
  106. {
  107. if (!hashbusterusb_spi_transfer(h, rdbuf, wrbuf, bufsz))
  108. return false;
  109. }
  110. return true;
  111. }
  112. static
  113. bool hashbusterusb_lowl_match(const struct lowlevel_device_info * const info)
  114. {
  115. return lowlevel_match_id(info, &lowl_usb, 0xFA04, 0x000D);
  116. }
  117. static
  118. bool hashbusterusb_lowl_probe(const struct lowlevel_device_info * const info)
  119. {
  120. struct cgpu_info *cgpu = NULL;
  121. struct bitfury_device **devicelist, *bitfury;
  122. struct spi_port *port;
  123. int j;
  124. struct cgpu_info dummy_cgpu;
  125. const char * const product = info->product;
  126. char *serial = info->serial;
  127. libusb_device_handle *h;
  128. if (info->lowl != &lowl_usb)
  129. applogr(false, LOG_DEBUG, "%s: Matched \"%s\" %s, but lowlevel driver is not usb_generic!",
  130. __func__, product, info->devid);
  131. if (info->vid != 0xFA04 || info->pid != 0x000D)
  132. applogr(false, LOG_DEBUG, "%s: Wrong VID/PID", __func__);
  133. libusb_device *dev = info->lowl_data;
  134. if ( (j = libusb_open(dev, &h)) )
  135. applogr(false, LOG_ERR, "%s: Failed to open %s: %s",
  136. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  137. if ( (j = libusb_set_configuration(h, 1)) )
  138. applogr(false, LOG_ERR, "%s: Failed to set configuration 1 on %s: %s",
  139. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  140. if ( (j = libusb_claim_interface(h, 0)) )
  141. applogr(false, LOG_ERR, "%s: Failed to claim interface 0 on %s: %s",
  142. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  143. struct lowl_usb_endpoint * const ep = usb_open_ep_pair(h, 0x81, 64, 0x01, 64);
  144. usb_ep_set_timeouts_ms(ep, 100, 0);
  145. unsigned char OUTPacket[64];
  146. unsigned char INPacket[64];
  147. OUTPacket[0] = 0xFE;
  148. hashbusterusb_io(ep, INPacket, OUTPacket);
  149. if (INPacket[1] == 0x18)
  150. {
  151. // Turn on miner PSU
  152. OUTPacket[0] = 0x10;
  153. OUTPacket[1] = 0x00;
  154. OUTPacket[2] = 0x01;
  155. hashbusterusb_io(ep, INPacket, OUTPacket);
  156. }
  157. OUTPacket[0] = '\x20';
  158. hashbusterusb_io(ep, INPacket, OUTPacket);
  159. if (!memcmp(INPacket, "\x20\0", 2))
  160. {
  161. // 64-bit BE serial number
  162. uint64_t sernum = 0;
  163. for (j = 0; j < 8; ++j)
  164. sernum |= (uint64_t)INPacket[j + 2] << (j * 8);
  165. serial = malloc((8 * 2) + 1);
  166. sprintf(serial, "%08"PRIX64, sernum);
  167. }
  168. else
  169. serial = maybe_strdup(info->serial);
  170. int chip_n;
  171. port = malloc(sizeof(*port));
  172. port->cgpu = &dummy_cgpu;
  173. port->txrx = hashbusterusb_spi_txrx;
  174. port->userp = ep;
  175. port->repr = hashbusterusb_drv.dname;
  176. port->logprio = LOG_DEBUG;
  177. port->speed = 100000;
  178. port->mode = 0;
  179. chip_n = libbitfury_detectChips1(port);
  180. if (unlikely(!chip_n))
  181. chip_n = libbitfury_detectChips1(port);
  182. if (unlikely(!chip_n))
  183. {
  184. applog(LOG_WARNING, "%s: No chips found on %s (serial \"%s\")",
  185. __func__, info->devid, serial);
  186. fail:
  187. usb_close_ep(ep);
  188. free(port);
  189. free(serial);
  190. libusb_release_interface(h, 0);
  191. libusb_close(h);
  192. return false;
  193. }
  194. if (bfg_claim_libusb(&hashbusterusb_drv, true, dev))
  195. goto fail;
  196. {
  197. devicelist = malloc(sizeof(*devicelist) * chip_n);
  198. for (j = 0; j < chip_n; ++j)
  199. {
  200. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  201. *bitfury = (struct bitfury_device){
  202. .spi = port,
  203. .slot = 0,
  204. .fasync = j,
  205. };
  206. }
  207. cgpu = malloc(sizeof(*cgpu));
  208. *cgpu = (struct cgpu_info){
  209. .drv = &hashbusterusb_drv,
  210. .procs = chip_n,
  211. .device_data = devicelist,
  212. .cutofftemp = 200,
  213. .threads = 1,
  214. .device_path = strdup(info->devid),
  215. .dev_manufacturer = maybe_strdup(info->manufacturer),
  216. .dev_product = maybe_strdup(product),
  217. .dev_serial = serial,
  218. .deven = DEV_ENABLED,
  219. };
  220. }
  221. return add_cgpu(cgpu);
  222. }
  223. static
  224. bool hashbusterusb_init(struct thr_info * const thr)
  225. {
  226. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  227. struct bitfury_device **devicelist;
  228. struct bitfury_device *bitfury;
  229. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  230. {
  231. devicelist = proc->device_data;
  232. bitfury = devicelist[proc->proc_id];
  233. proc->device_data = bitfury;
  234. bitfury->spi->cgpu = proc;
  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. if (proc->proc_id == proc->procs - 1)
  240. free(devicelist);
  241. }
  242. timer_set_now(&thr->tv_poll);
  243. cgpu->status = LIFE_INIT2;
  244. return true;
  245. }
  246. static
  247. bool hashbusterusb_get_stats(struct cgpu_info * const cgpu)
  248. {
  249. struct cgpu_info *proc;
  250. if (cgpu != cgpu->device)
  251. return true;
  252. struct bitfury_device * const bitfury = cgpu->device_data;
  253. struct spi_port * const spi = bitfury->spi;
  254. struct lowl_usb_endpoint * const h = spi->userp;
  255. uint8_t buf[0x40] = {'\x04'};
  256. if (!hashbusterusb_io(h, buf, buf))
  257. return false;
  258. if (buf[1])
  259. {
  260. for (proc = cgpu; proc; proc = proc->next_proc)
  261. proc->temp = buf[1];
  262. }
  263. return true;
  264. }
  265. static
  266. void hashbusterusb_shutdown(struct thr_info *thr)
  267. {
  268. struct cgpu_info *cgpu = thr->cgpu;
  269. struct bitfury_device * const bitfury = cgpu->device_data;
  270. struct spi_port * const spi = bitfury->spi;
  271. struct lowl_usb_endpoint * const h = spi->userp;
  272. // Shutdown PSU
  273. unsigned char OUTPacket[64];
  274. unsigned char INPacket[64];
  275. OUTPacket[0] = 0x10;
  276. OUTPacket[1] = 0x00;
  277. OUTPacket[2] = 0x00;
  278. hashbusterusb_io(h, INPacket, OUTPacket);
  279. }
  280. struct device_drv hashbusterusb_drv = {
  281. .dname = "hashbusterusb",
  282. .name = "HBR",
  283. .lowl_match = hashbusterusb_lowl_match,
  284. .lowl_probe = hashbusterusb_lowl_probe,
  285. .thread_init = hashbusterusb_init,
  286. .thread_disable = bitfury_disable,
  287. .thread_enable = bitfury_enable,
  288. .thread_shutdown = hashbusterusb_shutdown,
  289. .minerloop = minerloop_async,
  290. .job_prepare = bitfury_job_prepare,
  291. .job_start = bitfury_noop_job_start,
  292. .poll = bitfury_do_io,
  293. .job_process_results = bitfury_job_process_results,
  294. .get_stats = hashbusterusb_get_stats,
  295. .get_api_extra_device_detail = bitfury_api_device_detail,
  296. .get_api_extra_device_status = bitfury_api_device_status,
  297. .set_device = bitfury_set_device,
  298. #ifdef HAVE_CURSES
  299. .proc_wlogprint_status = bitfury_wlogprint_status,
  300. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  301. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  302. #endif
  303. };