driver-hashbusterusb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. struct hashbusterusb_state {
  26. uint16_t voltage;
  27. struct timeval identify_started;
  28. bool identify_requested;
  29. };
  30. static
  31. bool hashbusterusb_io(struct lowl_usb_endpoint * const h, unsigned char *buf, unsigned char *cmd)
  32. {
  33. char x[0x81];
  34. bool rv = true;
  35. if (unlikely(opt_dev_protocol))
  36. {
  37. bin2hex(x, cmd, 0x40);
  38. applog(LOG_DEBUG, "%s(%p): SEND: %s", __func__, h, x);
  39. }
  40. do // Workaround for PIC USB buffer corruption. We should repeat last packet if receive FF
  41. {
  42. do
  43. {
  44. usb_write(h, cmd, 64);
  45. } while (usb_read(h, buf, 64) != 64);
  46. } while(buf[0]==0xFF);
  47. if (unlikely(opt_dev_protocol))
  48. {
  49. bin2hex(x, buf, 0x40);
  50. applog(LOG_DEBUG, "%s(%p): RECV: %s", __func__, h, x);
  51. }
  52. return rv;
  53. }
  54. static
  55. bool hashbusterusb_spi_config(struct lowl_usb_endpoint * const h, const uint8_t mode, const uint8_t miso, const uint32_t freq)
  56. {
  57. uint8_t buf[0x40] = {'\x01', '\x01'};
  58. if (!hashbusterusb_io(h, buf, buf))
  59. return false;
  60. return (buf[1] == '\x00');
  61. }
  62. static
  63. bool hashbusterusb_spi_disable(struct lowl_usb_endpoint * const h)
  64. {
  65. uint8_t buf[0x40] = {'\x01', '\x00'};
  66. if (!hashbusterusb_io(h, buf, buf))
  67. return false;
  68. return (buf[1] == '\x00');
  69. }
  70. static
  71. bool hashbusterusb_spi_reset(struct lowl_usb_endpoint * const h, uint8_t chips)
  72. {
  73. uint8_t buf[0x40] = {'\x02', '\x00', chips};
  74. if (!hashbusterusb_io(h, buf, buf))
  75. return false;
  76. return (buf[1] == '\x00');
  77. }
  78. static
  79. bool hashbusterusb_spi_transfer(struct lowl_usb_endpoint * const h, void * const buf, const void * const data, size_t datasz)
  80. {
  81. if (datasz > HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  82. return false;
  83. uint8_t cbuf[0x40] = {'\x03', '\x00', datasz};
  84. memcpy(&cbuf[3], data, datasz);
  85. if (!hashbusterusb_io(h, cbuf, cbuf))
  86. return false;
  87. if (cbuf[2] != datasz)
  88. return false;
  89. memcpy(buf, &cbuf[3], datasz);
  90. return true;
  91. }
  92. static
  93. bool hashbusterusb_spi_txrx(struct spi_port * const port)
  94. {
  95. struct lowl_usb_endpoint * const h = port->userp;
  96. const uint8_t *wrbuf = spi_gettxbuf(port);
  97. uint8_t *rdbuf = spi_getrxbuf(port);
  98. size_t bufsz = spi_getbufsz(port);
  99. hashbusterusb_spi_disable(h);
  100. hashbusterusb_spi_reset(h, 0x10);
  101. hashbusterusb_spi_config(h, port->mode, 0, port->speed);
  102. while (bufsz >= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
  103. {
  104. if (!hashbusterusb_spi_transfer(h, rdbuf, wrbuf, HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER))
  105. return false;
  106. rdbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  107. wrbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  108. bufsz -= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
  109. }
  110. if (bufsz > 0)
  111. {
  112. if (!hashbusterusb_spi_transfer(h, rdbuf, wrbuf, bufsz))
  113. return false;
  114. }
  115. return true;
  116. }
  117. static
  118. bool hashbusterusb_lowl_match(const struct lowlevel_device_info * const info)
  119. {
  120. return lowlevel_match_id(info, &lowl_usb, 0xFA04, 0x000D);
  121. }
  122. static
  123. bool hashbusterusb_lowl_probe(const struct lowlevel_device_info * const info)
  124. {
  125. struct cgpu_info *cgpu = NULL;
  126. struct bitfury_device **devicelist, *bitfury;
  127. struct spi_port *port;
  128. int j;
  129. struct cgpu_info dummy_cgpu;
  130. const char * const product = info->product;
  131. char *serial = info->serial;
  132. libusb_device_handle *h;
  133. if (info->lowl != &lowl_usb)
  134. applogr(false, LOG_DEBUG, "%s: Matched \"%s\" %s, but lowlevel driver is not usb_generic!",
  135. __func__, product, info->devid);
  136. if (info->vid != 0xFA04 || info->pid != 0x000D)
  137. applogr(false, LOG_DEBUG, "%s: Wrong VID/PID", __func__);
  138. libusb_device *dev = info->lowl_data;
  139. if ( (j = libusb_open(dev, &h)) )
  140. applogr(false, LOG_ERR, "%s: Failed to open %s: %s",
  141. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  142. if ( (j = libusb_set_configuration(h, 1)) )
  143. {
  144. libusb_close(h);
  145. applogr(false, LOG_ERR, "%s: Failed to set configuration 1 on %s: %s",
  146. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  147. }
  148. if ( (j = libusb_claim_interface(h, 0)) )
  149. {
  150. libusb_close(h);
  151. applogr(false, LOG_ERR, "%s: Failed to claim interface 0 on %s: %s",
  152. __func__, info->devid, bfg_strerror(j, BST_LIBUSB));
  153. }
  154. struct lowl_usb_endpoint * const ep = usb_open_ep_pair(h, 0x81, 64, 0x01, 64);
  155. usb_ep_set_timeouts_ms(ep, 100, 0);
  156. unsigned char OUTPacket[64];
  157. unsigned char INPacket[64];
  158. OUTPacket[0] = 0xFE;
  159. hashbusterusb_io(ep, INPacket, OUTPacket);
  160. if (INPacket[1] == 0x18)
  161. {
  162. // Turn on miner PSU
  163. OUTPacket[0] = 0x10;
  164. OUTPacket[1] = 0x00;
  165. OUTPacket[2] = 0x01;
  166. hashbusterusb_io(ep, INPacket, OUTPacket);
  167. }
  168. OUTPacket[0] = '\x20';
  169. hashbusterusb_io(ep, INPacket, OUTPacket);
  170. if (!memcmp(INPacket, "\x20\0", 2))
  171. {
  172. // 64-bit BE serial number
  173. uint64_t sernum = 0;
  174. for (j = 0; j < 8; ++j)
  175. sernum |= (uint64_t)INPacket[j + 2] << (j * 8);
  176. serial = malloc((8 * 2) + 1);
  177. sprintf(serial, "%08"PRIX64, sernum);
  178. }
  179. else
  180. serial = maybe_strdup(info->serial);
  181. int chip_n;
  182. port = malloc(sizeof(*port));
  183. port->cgpu = &dummy_cgpu;
  184. port->txrx = hashbusterusb_spi_txrx;
  185. port->userp = ep;
  186. port->repr = hashbusterusb_drv.dname;
  187. port->logprio = LOG_DEBUG;
  188. port->speed = 100000;
  189. port->mode = 0;
  190. chip_n = libbitfury_detectChips1(port);
  191. if (unlikely(!chip_n))
  192. chip_n = libbitfury_detectChips1(port);
  193. if (unlikely(!chip_n))
  194. {
  195. applog(LOG_WARNING, "%s: No chips found on %s (serial \"%s\")",
  196. __func__, info->devid, serial);
  197. fail:
  198. usb_close_ep(ep);
  199. free(port);
  200. free(serial);
  201. libusb_release_interface(h, 0);
  202. libusb_close(h);
  203. return false;
  204. }
  205. if (bfg_claim_libusb(&hashbusterusb_drv, true, dev))
  206. goto fail;
  207. {
  208. devicelist = malloc(sizeof(*devicelist) * chip_n);
  209. for (j = 0; j < chip_n; ++j)
  210. {
  211. devicelist[j] = bitfury = malloc(sizeof(*bitfury));
  212. *bitfury = (struct bitfury_device){
  213. .spi = port,
  214. .slot = 0,
  215. .fasync = j,
  216. };
  217. }
  218. cgpu = malloc(sizeof(*cgpu));
  219. *cgpu = (struct cgpu_info){
  220. .drv = &hashbusterusb_drv,
  221. .procs = chip_n,
  222. .device_data = devicelist,
  223. .cutofftemp = 200,
  224. .threads = 1,
  225. .device_path = strdup(info->devid),
  226. .dev_manufacturer = maybe_strdup(info->manufacturer),
  227. .dev_product = maybe_strdup(product),
  228. .dev_serial = serial,
  229. .deven = DEV_ENABLED,
  230. };
  231. }
  232. return add_cgpu(cgpu);
  233. }
  234. static
  235. bool hashbusterusb_init(struct thr_info * const thr)
  236. {
  237. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  238. struct bitfury_device **devicelist;
  239. struct bitfury_device *bitfury;
  240. struct hashbusterusb_state * const state = malloc(sizeof(*state));
  241. *state = (struct hashbusterusb_state){
  242. .voltage = 0,
  243. };
  244. cgpu_setup_control_requests(cgpu);
  245. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  246. {
  247. devicelist = proc->device_data;
  248. bitfury = devicelist[proc->proc_id];
  249. proc->device_data = bitfury;
  250. proc->thr[0]->cgpu_data = state;
  251. bitfury->spi->cgpu = proc;
  252. bitfury_init_chip(proc);
  253. bitfury->osc6_bits = 53;
  254. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  255. bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
  256. if (proc->proc_id == proc->procs - 1)
  257. free(devicelist);
  258. }
  259. timer_set_now(&thr->tv_poll);
  260. cgpu->status = LIFE_INIT2;
  261. return true;
  262. }
  263. static void hashbusterusb_set_colour(struct cgpu_info *, uint8_t, uint8_t, uint8_t);
  264. static
  265. void hashbusterusb_poll(struct thr_info * const master_thr)
  266. {
  267. struct hashbusterusb_state * const state = master_thr->cgpu_data;
  268. struct cgpu_info * const cgpu = master_thr->cgpu;
  269. if (state->identify_requested)
  270. {
  271. if (!timer_isset(&state->identify_started))
  272. hashbusterusb_set_colour(cgpu, 0xff, 0, 0xff);
  273. timer_set_delay_from_now(&state->identify_started, 5000000);
  274. state->identify_requested = false;
  275. }
  276. bitfury_do_io(master_thr);
  277. if (timer_passed(&state->identify_started, NULL))
  278. {
  279. hashbusterusb_set_colour(cgpu, 0, 0x7e, 0);
  280. timer_unset(&state->identify_started);
  281. }
  282. }
  283. static
  284. bool hashbusterusb_get_stats(struct cgpu_info * const cgpu)
  285. {
  286. bool rv = false;
  287. struct cgpu_info *proc;
  288. if (cgpu != cgpu->device)
  289. return true;
  290. struct bitfury_device * const bitfury = cgpu->device_data;
  291. struct spi_port * const spi = bitfury->spi;
  292. struct lowl_usb_endpoint * const h = spi->userp;
  293. uint8_t buf[0x40] = {'\x04'};
  294. if (hashbusterusb_io(h, buf, buf))
  295. {
  296. if (buf[1])
  297. {
  298. rv = true;
  299. for (proc = cgpu; proc; proc = proc->next_proc)
  300. proc->temp = buf[1];
  301. }
  302. }
  303. buf[0] = '\x15';
  304. if (hashbusterusb_io(h, buf, buf))
  305. {
  306. if (!memcmp(buf, "\x15\0", 2))
  307. {
  308. rv = true;
  309. const uint16_t voltage = (buf[3] << 8) | buf[2];
  310. for (proc = cgpu; proc; proc = proc->next_proc)
  311. {
  312. struct hashbusterusb_state * const state = proc->thr[0]->cgpu_data;
  313. state->voltage = voltage;
  314. }
  315. }
  316. }
  317. return rv;
  318. }
  319. static
  320. void hashbusterusb_shutdown(struct thr_info *thr)
  321. {
  322. struct cgpu_info *cgpu = thr->cgpu;
  323. struct bitfury_device * const bitfury = cgpu->device_data;
  324. struct spi_port * const spi = bitfury->spi;
  325. struct lowl_usb_endpoint * const h = spi->userp;
  326. // Shutdown PSU
  327. unsigned char OUTPacket[64];
  328. unsigned char INPacket[64];
  329. OUTPacket[0] = 0x10;
  330. OUTPacket[1] = 0x00;
  331. OUTPacket[2] = 0x00;
  332. hashbusterusb_io(h, INPacket, OUTPacket);
  333. }
  334. static
  335. void hashbusterusb_set_colour(struct cgpu_info * const cgpu, const uint8_t red, const uint8_t green, const uint8_t blue)
  336. {
  337. struct bitfury_device * const bitfury = cgpu->device_data;
  338. struct spi_port * const spi = bitfury->spi;
  339. struct lowl_usb_endpoint * const h = spi->userp;
  340. uint8_t buf[0x40] = {'\x30', 0, red, green, blue};
  341. hashbusterusb_io(h, buf, buf);
  342. applog(LOG_DEBUG, "%s: Set LED colour to r=0x%x g=0x%x b=0x%x",
  343. cgpu->dev_repr, (unsigned)red, (unsigned)green, (unsigned)blue);
  344. }
  345. static
  346. bool hashbusterusb_identify(struct cgpu_info * const proc)
  347. {
  348. struct hashbusterusb_state * const state = proc->thr[0]->cgpu_data;
  349. state->identify_requested = true;
  350. return true;
  351. }
  352. static
  353. bool hashbusterusb_set_voltage(struct cgpu_info * const proc, const uint16_t nv)
  354. {
  355. struct bitfury_device * const bitfury = proc->device_data;
  356. struct spi_port * const spi = bitfury->spi;
  357. struct lowl_usb_endpoint * const h = spi->userp;
  358. unsigned char buf[0x40] = {0x11, 0, (nv & 0xff), (nv >> 8)};
  359. hashbusterusb_io(h, buf, buf);
  360. return !memcmp(buf, "\x11\0", 2);
  361. }
  362. static
  363. bool hashbusterusb_vrm_unlock(struct cgpu_info * const proc, const char * const code)
  364. {
  365. struct bitfury_device * const bitfury = proc->device_data;
  366. struct spi_port * const spi = bitfury->spi;
  367. struct lowl_usb_endpoint * const h = spi->userp;
  368. unsigned char buf[0x40] = {0x12};
  369. size_t size;
  370. size = strlen(code) >> 1;
  371. if (size > 63)
  372. size = 63;
  373. hex2bin(&buf[1], code, size);
  374. hashbusterusb_io(h, buf, buf);
  375. return !memcmp(buf, "\x12\0", 2);
  376. }
  377. static
  378. void hashbusterusb_vrm_lock(struct cgpu_info * const proc)
  379. {
  380. struct bitfury_device * const bitfury = proc->device_data;
  381. struct spi_port * const spi = bitfury->spi;
  382. struct lowl_usb_endpoint * const h = spi->userp;
  383. unsigned char buf[0x40] = {0x14};
  384. hashbusterusb_io(h, buf, buf);
  385. }
  386. static
  387. struct api_data *hashbusterusb_api_extra_device_stats(struct cgpu_info * const cgpu)
  388. {
  389. struct hashbusterusb_state * const state = cgpu->thr[0]->cgpu_data;
  390. struct api_data *root = bitfury_api_device_status(cgpu);
  391. float volts = state->voltage;
  392. volts /= 1000.;
  393. root = api_add_volts(root, "Voltage", &volts, true);
  394. return root;
  395. }
  396. static
  397. char *hashbusterusb_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  398. {
  399. if (!strcasecmp(option, "help"))
  400. {
  401. bitfury_set_device(proc, option, setting, replybuf);
  402. tailsprintf(replybuf, 1024, "\nvrmlock: Lock the VRM voltage to safe range\nvrmunlock: Allow setting potentially unsafe voltages (requires unlock code)\nvoltage: Set voltage");
  403. return replybuf;
  404. }
  405. if (!strcasecmp(option, "vrmlock"))
  406. {
  407. cgpu_request_control(proc->device);
  408. hashbusterusb_vrm_lock(proc);
  409. cgpu_release_control(proc->device);
  410. return NULL;
  411. }
  412. if (!strcasecmp(option, "vrmunlock"))
  413. {
  414. cgpu_request_control(proc->device);
  415. const bool rv = hashbusterusb_vrm_unlock(proc, setting);
  416. cgpu_release_control(proc->device);
  417. if (!rv)
  418. return "Unlock error";
  419. return NULL;
  420. }
  421. if (!strcasecmp(option, "voltage"))
  422. {
  423. const int val = atof(setting) * 1000;
  424. if (val < 600 || val > 1100)
  425. return "Invalid PSU voltage value";
  426. cgpu_request_control(proc->device);
  427. const bool rv = hashbusterusb_set_voltage(proc, val);
  428. cgpu_release_control(proc->device);
  429. if (!rv)
  430. return "Voltage change error";
  431. return NULL;
  432. }
  433. return bitfury_set_device(proc, option, setting, replybuf);
  434. }
  435. #ifdef HAVE_CURSES
  436. void hashbusterusb_tui_wlogprint_choices(struct cgpu_info * const proc)
  437. {
  438. wlogprint("[V]oltage ");
  439. wlogprint("[O]scillator bits ");
  440. //wlogprint("[F]an speed "); // To be implemented
  441. wlogprint("[U]nlock VRM ");
  442. wlogprint("[L]ock VRM ");
  443. }
  444. const char *hashbusterusb_tui_handle_choice(struct cgpu_info * const proc, const int input)
  445. {
  446. switch (input)
  447. {
  448. case 'v': case 'V':
  449. {
  450. const int val = curses_int("Set PSU voltage (range 600mV-1100mV. VRM unlock is required for over 870mV)");
  451. if (val < 600 || val > 1100)
  452. return "Invalid PSU voltage value\n";
  453. cgpu_request_control(proc->device);
  454. const bool rv = hashbusterusb_set_voltage(proc, val);
  455. cgpu_release_control(proc->device);
  456. if (!rv)
  457. return "Voltage change error\n";
  458. return "Voltage change successful\n";
  459. }
  460. case 'u': case 'U':
  461. {
  462. char *input = curses_input("VRM unlock code");
  463. if (!input)
  464. input = calloc(1, 1);
  465. cgpu_request_control(proc->device);
  466. const bool rv = hashbusterusb_vrm_unlock(proc, input);
  467. cgpu_release_control(proc->device);
  468. free(input);
  469. if (!rv)
  470. return "Unlock error\n";
  471. return "Unlocking PSU\n";
  472. }
  473. case 'o': case 'O':
  474. return bitfury_tui_handle_choice(proc, input);
  475. case 'l': case 'L':
  476. {
  477. cgpu_request_control(proc->device);
  478. hashbusterusb_vrm_lock(proc);
  479. cgpu_release_control(proc->device);
  480. return "VRM lock\n";
  481. }
  482. }
  483. return NULL;
  484. }
  485. void hashbusterusb_wlogprint_status(struct cgpu_info * const proc)
  486. {
  487. struct hashbusterusb_state * const state = proc->thr[0]->cgpu_data;
  488. bitfury_wlogprint_status(proc);
  489. wlogprint("PSU voltage: %umV\n", (unsigned)state->voltage);
  490. }
  491. #endif
  492. struct device_drv hashbusterusb_drv = {
  493. .dname = "hashbusterusb",
  494. .name = "HBR",
  495. .lowl_match = hashbusterusb_lowl_match,
  496. .lowl_probe = hashbusterusb_lowl_probe,
  497. .thread_init = hashbusterusb_init,
  498. .thread_disable = bitfury_disable,
  499. .thread_enable = bitfury_enable,
  500. .thread_shutdown = hashbusterusb_shutdown,
  501. .minerloop = minerloop_async,
  502. .job_prepare = bitfury_job_prepare,
  503. .job_start = bitfury_noop_job_start,
  504. .poll = hashbusterusb_poll,
  505. .job_process_results = bitfury_job_process_results,
  506. .get_stats = hashbusterusb_get_stats,
  507. .get_api_extra_device_detail = bitfury_api_device_detail,
  508. .get_api_extra_device_status = hashbusterusb_api_extra_device_stats,
  509. .set_device = hashbusterusb_set_device,
  510. .identify_device = hashbusterusb_identify,
  511. #ifdef HAVE_CURSES
  512. .proc_wlogprint_status = hashbusterusb_wlogprint_status,
  513. .proc_tui_wlogprint_choices = hashbusterusb_tui_wlogprint_choices,
  514. .proc_tui_handle_choice = hashbusterusb_tui_handle_choice,
  515. #endif
  516. };