libztex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright 2012 nelisky
  3. * Copyright 2012-2013 Luke Dashjr
  4. * Copyright 2012-2013 Denis Ahrens~
  5. * Copyright 2012 Peter Stuge~
  6. *
  7. * This work is based upon the Java SDK provided by ztex which is
  8. * Copyright (C) 2009-2011 ZTEX GmbH.
  9. * http://www.ztex.de
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 3 of the License, or (at your option)
  14. * any later version. See COPYING for more details.
  15. *
  16. * The copyright status of some of this code is currently a bit confused. They
  17. * were initially released under a license (GPLv2 only) incompatible with the
  18. * rest of the program at the time (GPLv3 or newer), and I haven't had luck
  19. * getting in touch with some later contributors (denoted above with a tilde) to
  20. * clarify it. Since their modifications would have been a license violation,
  21. * I'm assuming it was just an innocent mistake on their part.
  22. */
  23. #define _GNU_SOURCE
  24. #include "config.h"
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include "compat.h"
  29. #include "dynclock.h"
  30. #include "miner.h"
  31. #include "fpgautils.h"
  32. #include "libztex.h"
  33. #include "util.h"
  34. //* Capability index for EEPROM support.
  35. #define CAPABILITY_EEPROM 0,0
  36. //* Capability index for FPGA configuration support.
  37. #define CAPABILITY_FPGA 0,1
  38. //* Capability index for FLASH memory support.
  39. #define CAPABILITY_FLASH 0,2
  40. //* Capability index for DEBUG helper support.
  41. #define CAPABILITY_DEBUG 0,3
  42. //* Capability index for AVR XMEGA support.
  43. #define CAPABILITY_XMEGA 0,4
  44. //* Capability index for AVR XMEGA support.
  45. #define CAPABILITY_HS_FPGA 0,5
  46. //* Capability index for AVR XMEGA support.
  47. #define CAPABILITY_MAC_EEPROM 0,6
  48. //* Capability index for multi FPGA support.
  49. #define CAPABILITY_MULTI_FPGA 0,7
  50. static int libztex_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index,
  51. unsigned char *data, int length)
  52. {
  53. int i, cnt;
  54. uint16_t langid;
  55. unsigned char buf[260];
  56. /* We open code string descriptor retrieval and ASCII decoding here
  57. * in order to work around that libusb_get_string_descriptor_ascii()
  58. * in the FreeBSD libusb implementation hits a bug in ZTEX firmware,
  59. * where the device returns more bytes than requested, causing babble,
  60. * which makes FreeBSD return an error to us.
  61. *
  62. * Avoid the mess by doing it manually the same way as libusb-1.0.
  63. */
  64. cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  65. LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
  66. 0x0000, buf, sizeof(buf), 1000);
  67. if (cnt < 0) {
  68. applog(LOG_ERR, "%s: Failed to read LANGIDs: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  69. return cnt;
  70. }
  71. langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
  72. cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  73. LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
  74. langid, buf, sizeof(buf), 1000);
  75. if (cnt < 0) {
  76. applog(LOG_ERR, "%s: Failed to read string descriptor: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  77. return cnt;
  78. }
  79. /* num chars = (all bytes except bLength and bDescriptorType) / 2 */
  80. for (i = 0; i <= (cnt - 2) / 2 && i < length-1; i++)
  81. data[i] = buf[2 + i*2];
  82. data[i] = 0;
  83. return LIBUSB_SUCCESS;
  84. }
  85. enum check_result
  86. {
  87. CHECK_ERROR,
  88. CHECK_IS_NOT_ZTEX,
  89. CHECK_OK,
  90. CHECK_RESCAN,
  91. };
  92. static bool libztex_firmwareReset(struct libusb_device_handle *hndl, bool enable)
  93. {
  94. uint8_t reset = enable ? 1 : 0;
  95. int cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, &reset, 1, 1000);
  96. if (cnt < 0)
  97. {
  98. applog(LOG_ERR, "Ztex reset %d failed: %s", enable, bfg_strerror(cnt, BST_LIBUSB));
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. static enum check_result libztex_checkDevice(struct libusb_device *dev)
  104. {
  105. FILE *fp = NULL;
  106. libusb_device_handle *hndl = NULL;
  107. struct libusb_device_descriptor desc;
  108. int ret = CHECK_ERROR, err, cnt;
  109. size_t got_bytes, length;
  110. unsigned char buf[64], *fw_buf = NULL;
  111. unsigned int i;
  112. err = libusb_get_device_descriptor(dev, &desc);
  113. if (unlikely(err != 0)) {
  114. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  115. return CHECK_ERROR;
  116. }
  117. if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) {
  118. applog(LOG_DEBUG, "Not a ZTEX device %04x:%04x", desc.idVendor, desc.idProduct);
  119. return CHECK_IS_NOT_ZTEX;
  120. }
  121. err = libusb_open(dev, &hndl);
  122. if (err != LIBUSB_SUCCESS) {
  123. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  124. goto done;
  125. }
  126. cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  127. if (unlikely(cnt < 0)) {
  128. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  129. goto done;
  130. }
  131. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  132. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  133. goto done;
  134. }
  135. if (buf[6] != 10)
  136. {
  137. ret = CHECK_IS_NOT_ZTEX;
  138. goto done;
  139. }
  140. // 15 = 1.15y 13 = 1.15d or 1.15x
  141. switch(buf[7])
  142. {
  143. case 13:
  144. applog(LOG_ERR, "Found ztex board 1.15d or 1.15x");
  145. break;
  146. case 15:
  147. applog(LOG_ERR, "Found ztex board 1.15y");
  148. break;
  149. default:
  150. applog(LOG_ERR, "Found unknown ztex board");
  151. ret = CHECK_IS_NOT_ZTEX;
  152. goto done;
  153. }
  154. // testing for dummy firmware
  155. if (buf[8] != 0) {
  156. ret = CHECK_OK;
  157. goto done;
  158. }
  159. applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware");
  160. char productString[32];
  161. cnt = libztex_get_string_descriptor_ascii(hndl, desc.iProduct, (unsigned char*)productString, sizeof(productString));
  162. if (unlikely(cnt < 0)) {
  163. applog(LOG_ERR, "Ztex check device: Failed to read device productString with err %d", cnt);
  164. return cnt;
  165. }
  166. applog(LOG_ERR, "productString: %s", productString);
  167. unsigned char productID2 = buf[7];
  168. char *firmware = NULL;
  169. if (strcmp("USB-FPGA Module 1.15d (default)", productString) == 0 && productID2 == 13)
  170. {
  171. firmware = "ztex_ufm1_15d4.bin";
  172. }
  173. else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13)
  174. {
  175. firmware = "ztex_ufm1_15d4.bin";
  176. }
  177. else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15)
  178. {
  179. firmware = "ztex_ufm1_15y1.bin";
  180. }
  181. if (firmware == NULL)
  182. {
  183. applog(LOG_ERR, "could not figure out which firmware to use");
  184. goto done;
  185. }
  186. applog(LOG_ERR, "Mining firmware filename: %s", firmware);
  187. fp = open_bitstream("ztex", firmware);
  188. if (!fp) {
  189. applog(LOG_ERR, "failed to open firmware file '%s'", firmware);
  190. goto done;
  191. }
  192. if (0 != fseek(fp, 0, SEEK_END)) {
  193. applog(LOG_ERR, "Ztex firmware fseek: %s", bfg_strerror(errno, BST_ERRNO));
  194. goto done;
  195. }
  196. length = ftell(fp);
  197. rewind(fp);
  198. fw_buf = malloc(length);
  199. if (!fw_buf) {
  200. applog(LOG_ERR, "%s: Can not allocate memory: %s", __func__, bfg_strerror(errno, BST_ERRNO));
  201. goto done;
  202. }
  203. got_bytes = fread(fw_buf, 1, length, fp);
  204. fclose(fp);
  205. fp = NULL;
  206. if (got_bytes < length) {
  207. applog(LOG_ERR, "%s: Incomplete firmware read: %"PRIu64"/%"PRIu64,
  208. __func__, (uint64_t)got_bytes, (uint64_t)length);
  209. goto done;
  210. }
  211. // in buf[] is still the identifier of the dummy firmware
  212. // use it to compare it with the new firmware
  213. char *rv = memmem(fw_buf, got_bytes, buf, 8);
  214. if (rv == NULL)
  215. {
  216. applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__);
  217. goto done;
  218. }
  219. // check for dummy firmware
  220. if (rv[8] == 0)
  221. {
  222. applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__);
  223. goto done;
  224. }
  225. if (libztex_firmwareReset(hndl, true))
  226. goto done;
  227. for (i = 0; i < length; i+= 256) {
  228. // firmware wants data in small chunks like 256 bytes
  229. int numbytes = (length - i) < 256 ? (length - i) : 256;
  230. int k = libusb_control_transfer(hndl, 0x40, 0xA0, i, 0, fw_buf + i, numbytes, 1000);
  231. if (k < numbytes)
  232. {
  233. applog(LOG_ERR, "Ztex device: Failed to write firmware at %d with: %s", i, bfg_strerror(k, BST_LIBUSB));
  234. goto done;
  235. }
  236. }
  237. if (libztex_firmwareReset(hndl, false))
  238. goto done;
  239. applog(LOG_ERR, "Ztex device: succesfully wrote firmware");
  240. ret = CHECK_RESCAN;
  241. done:
  242. free(fw_buf);
  243. if (fp)
  244. fclose(fp);
  245. if (hndl)
  246. libusb_close(hndl);
  247. return ret;
  248. }
  249. static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
  250. {
  251. if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
  252. (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
  253. applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
  254. return false;
  255. }
  256. return true;
  257. }
  258. static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
  259. {
  260. int i;
  261. for (i = 0; i < size - 4; i++) {
  262. if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
  263. return 1;
  264. if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
  265. return 0;
  266. }
  267. applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
  268. return 0;
  269. }
  270. static void libztex_swapBits(unsigned char *buf, int size)
  271. {
  272. unsigned char c;
  273. int i;
  274. for (i = 0; i < size; i++) {
  275. c = buf[i];
  276. buf[i] = ((c & 128) >> 7) |
  277. ((c & 64) >> 5) |
  278. ((c & 32) >> 3) |
  279. ((c & 16) >> 1) |
  280. ((c & 8) << 1) |
  281. ((c & 4) << 3) |
  282. ((c & 2) << 5) |
  283. ((c & 1) << 7);
  284. }
  285. }
  286. static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
  287. {
  288. unsigned char buf[9];
  289. int cnt;
  290. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  291. return -1;
  292. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
  293. if (unlikely(cnt < 0)) {
  294. applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
  295. return cnt;
  296. }
  297. state->fpgaConfigured = (buf[0] == 0);
  298. state->fpgaChecksum = buf[1] & 0xff;
  299. state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
  300. state->fpgaInitB = buf[6] & 0xff;
  301. state->fpgaFlashResult = buf[7];
  302. state->fpgaFlashBitSwap = (buf[8] != 0);
  303. return 0;
  304. }
  305. static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  306. {
  307. struct libztex_fpgastate state;
  308. const int transactionBytes = 65536;
  309. unsigned char buf[transactionBytes], settings[2];
  310. int tries, cnt, err;
  311. FILE *fp;
  312. if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
  313. return -1;
  314. libztex_getFpgaState(ztex, &state);
  315. if (!force && state.fpgaConfigured) {
  316. applog(LOG_INFO, "Bitstream already configured");
  317. return 0;
  318. }
  319. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
  320. if (unlikely(cnt < 0)) {
  321. applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt);
  322. return cnt;
  323. }
  324. err = libusb_claim_interface(ztex->hndl, settings[1]);
  325. if (err != LIBUSB_SUCCESS) {
  326. applog(LOG_ERR, "%s: failed to claim interface for hs transfer", ztex->repr);
  327. return -4;
  328. }
  329. for (tries = 3; tries > 0; tries--) {
  330. fp = open_bitstream("ztex", firmware);
  331. if (!fp) {
  332. applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
  333. libusb_release_interface(ztex->hndl, settings[1]);
  334. return -2;
  335. }
  336. libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
  337. // 0x34 - initHSFPGAConfiguration
  338. do
  339. {
  340. int length = fread(buf,1,transactionBytes,fp);
  341. if (bs != 0 && bs != 1)
  342. bs = libztex_detectBitstreamBitOrder(buf, length);
  343. if (bs == 1)
  344. libztex_swapBits(buf, length);
  345. err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000);
  346. if (cnt != length)
  347. applog(LOG_ERR, "%s: cnt != length", ztex->repr);
  348. if (err != 0)
  349. applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
  350. }
  351. while (!feof(fp));
  352. // While 1.15y can finish immediately, at least 1.15x needs some delay
  353. // (200ms might be enough, but 500ms is safer)
  354. if (ztex->productId[1] != 15)
  355. usleep(500);
  356. libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
  357. // 0x35 - finishHSFPGAConfiguration
  358. if (cnt >= 0)
  359. tries = 0;
  360. fclose(fp);
  361. libztex_getFpgaState(ztex, &state);
  362. if (!state.fpgaConfigured) {
  363. applog(LOG_ERR, "%"PRIpreprv": HS FPGA configuration failed: DONE pin does not go high", repr);
  364. libusb_release_interface(ztex->hndl, settings[1]);
  365. return -3;
  366. }
  367. }
  368. libusb_release_interface(ztex->hndl, settings[1]);
  369. nmsleep(200);
  370. applog(LOG_INFO, "%"PRIpreprv": HS FPGA configuration done", repr);
  371. return 0;
  372. }
  373. static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  374. {
  375. struct libztex_fpgastate state;
  376. const int transactionBytes = 2048;
  377. unsigned char buf[transactionBytes];
  378. int tries, cnt;
  379. FILE *fp;
  380. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  381. return -1;
  382. libztex_getFpgaState(ztex, &state);
  383. if (!force && state.fpgaConfigured) {
  384. applog(LOG_DEBUG, "Bitstream already configured");
  385. return 0;
  386. }
  387. for (tries = 10; tries > 0; tries--) {
  388. fp = open_bitstream("ztex", firmware);
  389. if (!fp) {
  390. applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
  391. return -2;
  392. }
  393. //* Reset fpga
  394. cnt = libztex_resetFpga(ztex);
  395. if (unlikely(cnt < 0)) {
  396. applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
  397. continue;
  398. }
  399. do
  400. {
  401. int length = fread(buf, 1, transactionBytes, fp);
  402. if (bs != 0 && bs != 1)
  403. bs = libztex_detectBitstreamBitOrder(buf, length);
  404. if (bs == 1)
  405. libztex_swapBits(buf, length);
  406. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, buf, length, 5000);
  407. if (cnt != length)
  408. {
  409. applog(LOG_ERR, "%s: Failed send ls fpga data", ztex->repr);
  410. break;
  411. }
  412. }
  413. while (!feof(fp));
  414. if (cnt > 0)
  415. tries = 0;
  416. fclose(fp);
  417. }
  418. libztex_getFpgaState(ztex, &state);
  419. if (!state.fpgaConfigured) {
  420. applog(LOG_ERR, "%"PRIpreprv": LS FPGA configuration failed: DONE pin does not go high", repr);
  421. return -3;
  422. }
  423. nmsleep(200);
  424. applog(LOG_INFO, "%"PRIpreprv": FPGA configuration done", repr);
  425. return 0;
  426. }
  427. int libztex_configureFpga(struct libztex_device *ztex, const char *repr)
  428. {
  429. char buf[256];
  430. int rv;
  431. strcpy(buf, ztex->bitFileName);
  432. strcat(buf, ".bit");
  433. rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
  434. if (rv != 0)
  435. rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
  436. return rv;
  437. }
  438. int libztex_numberOfFpgas(struct libztex_device *ztex)
  439. {
  440. int cnt;
  441. unsigned char buf[3];
  442. if (ztex->numberOfFpgas < 0) {
  443. if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
  444. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
  445. if (unlikely(cnt < 0)) {
  446. applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
  447. return cnt;
  448. }
  449. ztex->numberOfFpgas = buf[0] + 1;
  450. ztex->selectedFpga = -1;//buf[1];
  451. ztex->parallelConfigSupport = (buf[2] == 1);
  452. } else {
  453. ztex->numberOfFpgas = 1;
  454. ztex->selectedFpga = -1;//0;
  455. ztex->parallelConfigSupport = false;
  456. }
  457. }
  458. return ztex->numberOfFpgas;
  459. }
  460. int libztex_selectFpga(struct libztex_device *ztex, int16_t number)
  461. {
  462. int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root);
  463. if (number < 0 || number >= fpgacnt) {
  464. applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
  465. return 1;
  466. }
  467. if (ztex->root->selectedFpga != number && libztex_checkCapability(ztex->root, CAPABILITY_MULTI_FPGA)) {
  468. cnt = libusb_control_transfer(ztex->root->hndl, 0x40, 0x51, (uint16_t)number, 0, NULL, 0, 500);
  469. if (unlikely(cnt < 0)) {
  470. applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
  471. ztex->root->selectedFpga = -1;
  472. return cnt;
  473. }
  474. ztex->root->selectedFpga = number;
  475. }
  476. return 0;
  477. }
  478. int libztex_setFreq(struct libztex_device *ztex, uint16_t freq, const char *repr)
  479. {
  480. int cnt;
  481. uint16_t oldfreq = ztex->dclk.freqM;
  482. if (freq > ztex->dclk.freqMaxM)
  483. freq = ztex->dclk.freqMaxM;
  484. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
  485. if (unlikely(cnt < 0)) {
  486. applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
  487. return cnt;
  488. }
  489. ztex->dclk.freqM = freq;
  490. if (oldfreq > ztex->dclk.freqMaxM)
  491. applog(LOG_WARNING, "%"PRIpreprv": Frequency set to %u MHz (range: %u-%u)",
  492. repr,
  493. (unsigned)(ztex->freqM1 * (ztex->dclk.freqM + 1)),
  494. (unsigned)ztex->freqM1,
  495. (unsigned)(ztex->freqM1 * (ztex->dclk.freqMaxM + 1))
  496. );
  497. else
  498. dclk_msg_freqchange(repr,
  499. ztex->freqM1 * (oldfreq + 1),
  500. ztex->freqM1 * (ztex->dclk.freqM + 1),
  501. NULL);
  502. return 0;
  503. }
  504. int libztex_resetFpga(struct libztex_device *ztex)
  505. {
  506. return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
  507. }
  508. int libztex_suspend(struct libztex_device *ztex)
  509. {
  510. if (ztex->suspendSupported) {
  511. return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
  512. } else {
  513. return 0;
  514. }
  515. }
  516. int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
  517. {
  518. struct libztex_device *newdev = *ztex;
  519. int i, cnt, err;
  520. unsigned char buf[64];
  521. dclk_prepare(&newdev->dclk);
  522. err = libusb_open(dev, &newdev->hndl);
  523. if (err != LIBUSB_SUCCESS) {
  524. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  525. return CHECK_ERROR;
  526. }
  527. err = libusb_get_device_descriptor(dev, &newdev->descriptor);
  528. if (unlikely(err != 0)) {
  529. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  530. return CHECK_ERROR;
  531. }
  532. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
  533. if (unlikely(cnt < 0)) {
  534. applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
  535. return cnt;
  536. }
  537. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  538. if (unlikely(cnt < 0)) {
  539. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  540. return cnt;
  541. }
  542. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  543. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  544. return 2;
  545. }
  546. newdev->productId[0] = buf[6];
  547. newdev->productId[1] = buf[7];
  548. newdev->productId[2] = buf[8];
  549. newdev->productId[3] = buf[9];
  550. newdev->fwVersion = buf[10];
  551. newdev->interfaceVersion = buf[11];
  552. newdev->interfaceCapabilities[0] = buf[12];
  553. newdev->interfaceCapabilities[1] = buf[13];
  554. newdev->interfaceCapabilities[2] = buf[14];
  555. newdev->interfaceCapabilities[3] = buf[15];
  556. newdev->interfaceCapabilities[4] = buf[16];
  557. newdev->interfaceCapabilities[5] = buf[17];
  558. newdev->moduleReserved[0] = buf[18];
  559. newdev->moduleReserved[1] = buf[19];
  560. newdev->moduleReserved[2] = buf[20];
  561. newdev->moduleReserved[3] = buf[21];
  562. newdev->moduleReserved[4] = buf[22];
  563. newdev->moduleReserved[5] = buf[23];
  564. newdev->moduleReserved[6] = buf[24];
  565. newdev->moduleReserved[7] = buf[25];
  566. newdev->moduleReserved[8] = buf[26];
  567. newdev->moduleReserved[9] = buf[27];
  568. newdev->moduleReserved[10] = buf[28];
  569. newdev->moduleReserved[11] = buf[29];
  570. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
  571. if (unlikely(cnt < 0)) {
  572. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  573. return cnt;
  574. }
  575. if (unlikely(buf[0] != 5)) {
  576. if (unlikely(buf[0] != 2 && buf[0] != 4)) {
  577. applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
  578. return 3;
  579. }
  580. applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]);
  581. }
  582. i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
  583. while (cnt < 64 && buf[cnt] != 0)
  584. cnt++;
  585. if (cnt < i + 1) {
  586. applog(LOG_ERR, "Invalid bitstream file name .");
  587. return 4;
  588. }
  589. newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
  590. memcpy(newdev->bitFileName, &buf[i], cnt);
  591. newdev->bitFileName[cnt] = 0;
  592. newdev->numNonces = buf[1] + 1;
  593. newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
  594. newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
  595. newdev->dclk.freqMaxM = (buf[7] & 255);
  596. newdev->dclk.freqM = (buf[6] & 255);
  597. newdev->dclk.freqMDefault = newdev->dclk.freqM;
  598. newdev->suspendSupported = (buf[0] == 5);
  599. newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
  600. newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
  601. applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
  602. buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->dclk.freqMaxM, newdev->dclk.freqM, newdev->suspendSupported ? "T": "F",
  603. newdev->hashesPerClock, newdev->extraSolutions);
  604. if (buf[0] < 4) {
  605. if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
  606. newdev->hashesPerClock = 0.5;
  607. applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock);
  608. }
  609. newdev->usbbus = libusb_get_bus_number(dev);
  610. newdev->usbaddress = libusb_get_device_address(dev);
  611. sprintf(newdev->repr, "ZTEX %s-1", newdev->snString);
  612. return 0;
  613. }
  614. void libztex_destroy_device(struct libztex_device* ztex)
  615. {
  616. if (ztex->hndl != NULL) {
  617. libusb_close(ztex->hndl);
  618. ztex->hndl = NULL;
  619. }
  620. if (ztex->bitFileName != NULL) {
  621. free(ztex->bitFileName);
  622. ztex->bitFileName = NULL;
  623. }
  624. free(ztex);
  625. }
  626. int libztex_scanDevices(struct libztex_dev_list*** devs_p)
  627. {
  628. int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
  629. struct libztex_dev_list **devs = NULL;
  630. struct libztex_device *ztex = NULL;
  631. int found, max_found = 0, pos = 0, err, rescan, ret = 0;
  632. libusb_device **list = NULL;
  633. ssize_t cnt, i;
  634. do {
  635. cnt = libusb_get_device_list(NULL, &list);
  636. if (unlikely(cnt < 0)) {
  637. applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %"PRId64, (int64_t)cnt);
  638. goto done;
  639. }
  640. for (found = rescan = i = 0; i < cnt; i++) {
  641. err = libztex_checkDevice(list[i]);
  642. switch (err) {
  643. case CHECK_ERROR:
  644. applog(LOG_ERR, "Ztex: Can not check device: %s", bfg_strerror(err, BST_LIBUSB));
  645. continue;
  646. case CHECK_IS_NOT_ZTEX:
  647. continue;
  648. case CHECK_OK:
  649. // Got one!
  650. usbdevices[found++] = i;
  651. break;
  652. case CHECK_RESCAN:
  653. rescan = 1;
  654. found++;
  655. break;
  656. }
  657. }
  658. if (found < max_found)
  659. rescan = 1;
  660. else if (found > max_found)
  661. max_found = found;
  662. if (rescan)
  663. libusb_free_device_list(list, 1);
  664. } while (rescan);
  665. if (0 == found)
  666. goto done;
  667. devs = malloc(sizeof(struct libztex_dev_list *) * found);
  668. if (devs == NULL) {
  669. applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
  670. goto done;
  671. }
  672. for (i = 0; i < found; i++) {
  673. if (!ztex) {
  674. ztex = malloc(sizeof(*ztex));
  675. if (!ztex) {
  676. applog(LOG_ERR, "%s: Can not allocate memory for device struct: %s", __func__, bfg_strerror(errno, BST_ERRNO));
  677. goto done;
  678. }
  679. }
  680. ztex->bitFileName = NULL;
  681. ztex->numberOfFpgas = -1;
  682. err = libztex_prepare_device(list[usbdevices[i]], &ztex);
  683. if (unlikely(err != 0)) {
  684. applog(LOG_ERR, "prepare device: %d", err);
  685. libztex_destroy_device(ztex);
  686. ztex = NULL;
  687. continue;
  688. }
  689. devs[pos] = malloc(sizeof(struct libztex_dev_list));
  690. if (NULL == devs[pos]) {
  691. applog(LOG_ERR, "%s: Can not allocate memory for device: %s", __func__, bfg_strerror(errno, BST_ERRNO));
  692. libztex_destroy_device(ztex);
  693. ztex = NULL;
  694. continue;
  695. }
  696. devs[pos]->dev = ztex;
  697. ztex = NULL;
  698. devs[pos]->next = NULL;
  699. if (pos > 0)
  700. devs[pos - 1]->next = devs[pos];
  701. pos++;
  702. }
  703. ret = pos;
  704. done:
  705. if (ret > 0)
  706. *devs_p = devs;
  707. else if (devs)
  708. free(devs);
  709. if (list)
  710. libusb_free_device_list(list, 1);
  711. return ret;
  712. }
  713. int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
  714. {
  715. int cnt = 0, ret, len;
  716. if (ztex == NULL || ztex->hndl == NULL)
  717. return 0;
  718. ret = 44; len = 0;
  719. while (ret > 0) {
  720. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf + len, ret, 1000);
  721. if (cnt >= 0) {
  722. ret -= cnt;
  723. len += cnt;
  724. } else
  725. break;
  726. }
  727. if (unlikely(cnt < 0))
  728. applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
  729. return cnt;
  730. }
  731. int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
  732. {
  733. int bufsize = 12 + ztex->extraSolutions * 4;
  734. int cnt = 0, i, j, ret, len;
  735. unsigned char *rbuf;
  736. if (ztex->hndl == NULL)
  737. return 0;
  738. rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
  739. if (rbuf == NULL) {
  740. applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
  741. return 0;
  742. }
  743. ret = bufsize * ztex->numNonces; len = 0;
  744. while (ret > 0) {
  745. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf + len, ret, 1000);
  746. if (cnt >= 0) {
  747. ret -= cnt;
  748. len += cnt;
  749. } else
  750. break;
  751. }
  752. if (unlikely(cnt < 0)) {
  753. applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
  754. free(rbuf);
  755. return cnt;
  756. }
  757. for (i=0; i<ztex->numNonces; i++) {
  758. uint32_t *nonce_data = (void*)&rbuf[i * bufsize];
  759. nonces[i].goldenNonce[0] = nonce_data[0] - ztex->offsNonces;
  760. //applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
  761. nonces[i].nonce = le32toh(nonce_data[1]) - ztex->offsNonces;
  762. nonces[i].hash7 = le32toh(nonce_data[2]);
  763. for (j = 1; j <= ztex->extraSolutions; ++j)
  764. {
  765. nonces[i].goldenNonce[j] = le32toh(nonce_data[2 + j]) - ztex->offsNonces;
  766. //applog(LOG_DEBUG, "W %d:%d %0.8x", i, j, nonces[i].goldenNonce[j]);
  767. }
  768. }
  769. free(rbuf);
  770. return cnt;
  771. }
  772. void libztex_freeDevList(struct libztex_dev_list **devs)
  773. {
  774. bool done = false;
  775. ssize_t cnt = 0;
  776. while (!done) {
  777. if (devs[cnt]->next == NULL)
  778. done = true;
  779. free(devs[cnt++]);
  780. }
  781. free(devs);
  782. }