libztex.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. libusb_device_handle *hndl = NULL;
  106. struct libusb_device_descriptor desc;
  107. int ret = CHECK_ERROR, err, cnt;
  108. unsigned char buf[64];
  109. unsigned int i;
  110. bytes_t bsdata = BYTES_INIT;
  111. err = libusb_get_device_descriptor(dev, &desc);
  112. if (unlikely(err != 0)) {
  113. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  114. return CHECK_ERROR;
  115. }
  116. if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) {
  117. applog(LOG_DEBUG, "Not a ZTEX device %04x:%04x", desc.idVendor, desc.idProduct);
  118. return CHECK_IS_NOT_ZTEX;
  119. }
  120. err = libusb_open(dev, &hndl);
  121. if (err != LIBUSB_SUCCESS) {
  122. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  123. goto done;
  124. }
  125. if (libusb_claim_interface(hndl, 0) == LIBUSB_ERROR_BUSY)
  126. {
  127. applog(LOG_DEBUG, "Ztex check device: Interface already busy, skipping");
  128. goto done;
  129. }
  130. cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  131. if (unlikely(cnt < 0)) {
  132. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  133. goto done;
  134. }
  135. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  136. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  137. goto done;
  138. }
  139. if (buf[6] != 10)
  140. {
  141. ret = CHECK_IS_NOT_ZTEX;
  142. goto done;
  143. }
  144. // 15 = 1.15y 13 = 1.15d or 1.15x
  145. switch(buf[7])
  146. {
  147. case 13:
  148. applog(LOG_ERR, "Found ztex board 1.15d or 1.15x");
  149. break;
  150. case 15:
  151. applog(LOG_ERR, "Found ztex board 1.15y");
  152. break;
  153. default:
  154. applog(LOG_ERR, "Found unknown ztex board");
  155. ret = CHECK_IS_NOT_ZTEX;
  156. goto done;
  157. }
  158. // testing for dummy firmware
  159. if (buf[8] != 0) {
  160. ret = CHECK_OK;
  161. goto done;
  162. }
  163. applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware");
  164. char productString[32];
  165. cnt = libztex_get_string_descriptor_ascii(hndl, desc.iProduct, (unsigned char*)productString, sizeof(productString));
  166. if (unlikely(cnt < 0)) {
  167. applog(LOG_ERR, "Ztex check device: Failed to read device productString with err %d", cnt);
  168. return cnt;
  169. }
  170. applog(LOG_ERR, "productString: %s", productString);
  171. unsigned char productID2 = buf[7];
  172. char *firmware = NULL;
  173. if (strcmp("USB-FPGA Module 1.15d (default)", productString) == 0 && productID2 == 13)
  174. {
  175. firmware = "ztex_ufm1_15d4";
  176. }
  177. else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13)
  178. {
  179. firmware = "ztex_ufm1_15d4";
  180. }
  181. else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15)
  182. {
  183. firmware = "ztex_ufm1_15y1";
  184. }
  185. if (firmware == NULL)
  186. {
  187. applog(LOG_ERR, "could not figure out which firmware to use");
  188. goto done;
  189. }
  190. applog(LOG_ERR, "Mining firmware filename: %s", firmware);
  191. bytes_init(&bsdata);
  192. if (!load_bitstream_bytes(&bsdata, "ztex", "ZTX *", firmware))
  193. goto done;
  194. // in buf[] is still the identifier of the dummy firmware
  195. // use it to compare it with the new firmware
  196. char *rv = memmem(bytes_buf(&bsdata), bytes_len(&bsdata), buf, 8);
  197. if (rv == NULL)
  198. {
  199. applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__);
  200. goto done;
  201. }
  202. // check for dummy firmware
  203. if (rv[8] == 0)
  204. {
  205. applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__);
  206. goto done;
  207. }
  208. if (libztex_firmwareReset(hndl, true))
  209. goto done;
  210. for (i = 0; i < bytes_len(&bsdata); i+= 256) {
  211. // firmware wants data in small chunks like 256 bytes
  212. int numbytes = (bytes_len(&bsdata) - i) < 256 ? (bytes_len(&bsdata) - i) : 256;
  213. int k = libusb_control_transfer(hndl, 0x40, 0xA0, i, 0, bytes_buf(&bsdata) + i, numbytes, 1000);
  214. if (k < numbytes)
  215. {
  216. applog(LOG_ERR, "Ztex device: Failed to write firmware at %d with: %s", i, bfg_strerror(k, BST_LIBUSB));
  217. goto done;
  218. }
  219. }
  220. if (libztex_firmwareReset(hndl, false))
  221. goto done;
  222. applog(LOG_ERR, "Ztex device: succesfully wrote firmware");
  223. ret = CHECK_RESCAN;
  224. done:
  225. bytes_free(&bsdata);
  226. if (hndl)
  227. {
  228. libusb_release_interface(hndl, 0);
  229. libusb_close(hndl);
  230. }
  231. return ret;
  232. }
  233. static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
  234. {
  235. if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
  236. (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
  237. applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
  238. return false;
  239. }
  240. return true;
  241. }
  242. static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
  243. {
  244. int i;
  245. for (i = 0; i < size - 4; i++) {
  246. if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
  247. return 1;
  248. if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
  249. return 0;
  250. }
  251. applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
  252. return 0;
  253. }
  254. static void libztex_swapBits(unsigned char *buf, int size)
  255. {
  256. unsigned char c;
  257. int i;
  258. for (i = 0; i < size; i++) {
  259. c = buf[i];
  260. buf[i] = ((c & 128) >> 7) |
  261. ((c & 64) >> 5) |
  262. ((c & 32) >> 3) |
  263. ((c & 16) >> 1) |
  264. ((c & 8) << 1) |
  265. ((c & 4) << 3) |
  266. ((c & 2) << 5) |
  267. ((c & 1) << 7);
  268. }
  269. }
  270. static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
  271. {
  272. unsigned char buf[9];
  273. int cnt;
  274. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  275. return -1;
  276. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
  277. if (unlikely(cnt < 0)) {
  278. applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
  279. return cnt;
  280. }
  281. state->fpgaConfigured = (buf[0] == 0);
  282. state->fpgaChecksum = buf[1] & 0xff;
  283. state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
  284. state->fpgaInitB = buf[6] & 0xff;
  285. state->fpgaFlashResult = buf[7];
  286. state->fpgaFlashBitSwap = (buf[8] != 0);
  287. return 0;
  288. }
  289. static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  290. {
  291. struct libztex_fpgastate state;
  292. const int transactionBytes = 65536;
  293. unsigned char buf[transactionBytes], settings[2];
  294. int tries, cnt, err;
  295. FILE *fp;
  296. if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
  297. return -1;
  298. libztex_getFpgaState(ztex, &state);
  299. if (!force && state.fpgaConfigured) {
  300. applog(LOG_INFO, "Bitstream already configured");
  301. return 0;
  302. }
  303. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
  304. if (unlikely(cnt < 0)) {
  305. applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt);
  306. return cnt;
  307. }
  308. err = libusb_claim_interface(ztex->hndl, settings[1]);
  309. if (err != LIBUSB_SUCCESS) {
  310. applog(LOG_ERR, "%s: failed to claim interface for hs transfer", ztex->repr);
  311. return -4;
  312. }
  313. for (tries = 3; tries > 0; tries--) {
  314. fp = open_bitstream("ztex", firmware);
  315. if (!fp) {
  316. applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
  317. libusb_release_interface(ztex->hndl, settings[1]);
  318. return -2;
  319. }
  320. libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
  321. // 0x34 - initHSFPGAConfiguration
  322. do
  323. {
  324. int length = fread(buf,1,transactionBytes,fp);
  325. if (bs != 0 && bs != 1)
  326. bs = libztex_detectBitstreamBitOrder(buf, length);
  327. if (bs == 1)
  328. libztex_swapBits(buf, length);
  329. err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000);
  330. if (cnt != length)
  331. applog(LOG_ERR, "%s: cnt != length", ztex->repr);
  332. if (err != 0)
  333. applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
  334. }
  335. while (!feof(fp));
  336. // While 1.15y can finish immediately, at least 1.15x needs some delay
  337. // (200ms might be enough, but 500ms is safer)
  338. if (ztex->productId[1] != 15)
  339. usleep(500);
  340. libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
  341. // 0x35 - finishHSFPGAConfiguration
  342. if (cnt >= 0)
  343. tries = 0;
  344. fclose(fp);
  345. libztex_getFpgaState(ztex, &state);
  346. if (!state.fpgaConfigured) {
  347. applog(LOG_ERR, "%"PRIpreprv": HS FPGA configuration failed: DONE pin does not go high", repr);
  348. libusb_release_interface(ztex->hndl, settings[1]);
  349. return -3;
  350. }
  351. }
  352. libusb_release_interface(ztex->hndl, settings[1]);
  353. cgsleep_ms(200);
  354. applog(LOG_INFO, "%"PRIpreprv": HS FPGA configuration done", repr);
  355. return 0;
  356. }
  357. static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  358. {
  359. struct libztex_fpgastate state;
  360. const int transactionBytes = 2048;
  361. unsigned char buf[transactionBytes];
  362. int tries, cnt;
  363. FILE *fp;
  364. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  365. return -1;
  366. libztex_getFpgaState(ztex, &state);
  367. if (!force && state.fpgaConfigured) {
  368. applog(LOG_DEBUG, "Bitstream already configured");
  369. return 0;
  370. }
  371. for (tries = 10; tries > 0; tries--) {
  372. fp = open_bitstream("ztex", firmware);
  373. if (!fp) {
  374. _bitstream_not_found(repr, firmware);
  375. return -2;
  376. }
  377. //* Reset fpga
  378. cnt = libztex_resetFpga(ztex);
  379. if (unlikely(cnt < 0)) {
  380. applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
  381. continue;
  382. }
  383. do
  384. {
  385. int length = fread(buf, 1, transactionBytes, fp);
  386. if (bs != 0 && bs != 1)
  387. bs = libztex_detectBitstreamBitOrder(buf, length);
  388. if (bs == 1)
  389. libztex_swapBits(buf, length);
  390. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, buf, length, 5000);
  391. if (cnt != length)
  392. {
  393. applog(LOG_ERR, "%s: Failed send ls fpga data", ztex->repr);
  394. break;
  395. }
  396. }
  397. while (!feof(fp));
  398. if (cnt > 0)
  399. tries = 0;
  400. fclose(fp);
  401. }
  402. libztex_getFpgaState(ztex, &state);
  403. if (!state.fpgaConfigured) {
  404. applog(LOG_ERR, "%"PRIpreprv": LS FPGA configuration failed: DONE pin does not go high", repr);
  405. return -3;
  406. }
  407. cgsleep_ms(200);
  408. applog(LOG_INFO, "%"PRIpreprv": FPGA configuration done", repr);
  409. return 0;
  410. }
  411. int libztex_configureFpga(struct libztex_device *ztex, const char *repr)
  412. {
  413. char buf[256];
  414. int rv;
  415. strcpy(buf, ztex->bitFileName);
  416. strcat(buf, ".bit");
  417. rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
  418. if (rv != 0)
  419. rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
  420. if (!rv)
  421. if (libusb_claim_interface(ztex->hndl, 0) == LIBUSB_ERROR_BUSY)
  422. rv = -5;
  423. return rv;
  424. }
  425. int libztex_numberOfFpgas(struct libztex_device *ztex)
  426. {
  427. int cnt;
  428. unsigned char buf[3];
  429. if (ztex->numberOfFpgas < 0) {
  430. if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
  431. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
  432. if (unlikely(cnt < 0)) {
  433. applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
  434. return cnt;
  435. }
  436. ztex->numberOfFpgas = buf[0] + 1;
  437. ztex->selectedFpga = -1;//buf[1];
  438. ztex->parallelConfigSupport = (buf[2] == 1);
  439. } else {
  440. ztex->numberOfFpgas = 1;
  441. ztex->selectedFpga = -1;//0;
  442. ztex->parallelConfigSupport = false;
  443. }
  444. }
  445. return ztex->numberOfFpgas;
  446. }
  447. int libztex_selectFpga(struct libztex_device *ztex, int16_t number)
  448. {
  449. int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root);
  450. if (number < 0 || number >= fpgacnt) {
  451. applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
  452. return 1;
  453. }
  454. if (ztex->root->selectedFpga != number && libztex_checkCapability(ztex->root, CAPABILITY_MULTI_FPGA)) {
  455. cnt = libusb_control_transfer(ztex->root->hndl, 0x40, 0x51, (uint16_t)number, 0, NULL, 0, 500);
  456. if (unlikely(cnt < 0)) {
  457. applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
  458. ztex->root->selectedFpga = -1;
  459. return cnt;
  460. }
  461. ztex->root->selectedFpga = number;
  462. }
  463. return 0;
  464. }
  465. int libztex_setFreq(struct libztex_device *ztex, uint16_t freq, const char *repr)
  466. {
  467. int cnt;
  468. uint16_t oldfreq = ztex->dclk.freqM;
  469. if (freq > ztex->dclk.freqMaxM)
  470. freq = ztex->dclk.freqMaxM;
  471. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
  472. if (unlikely(cnt < 0)) {
  473. applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
  474. return cnt;
  475. }
  476. ztex->dclk.freqM = freq;
  477. if (oldfreq > ztex->dclk.freqMaxM)
  478. applog(LOG_WARNING, "%"PRIpreprv": Frequency set to %u MHz (range: %u-%u)",
  479. repr,
  480. (unsigned)(ztex->freqM1 * (ztex->dclk.freqM + 1)),
  481. (unsigned)ztex->freqM1,
  482. (unsigned)(ztex->freqM1 * (ztex->dclk.freqMaxM + 1))
  483. );
  484. else
  485. dclk_msg_freqchange(repr,
  486. ztex->freqM1 * (oldfreq + 1),
  487. ztex->freqM1 * (ztex->dclk.freqM + 1),
  488. NULL);
  489. return 0;
  490. }
  491. int libztex_resetFpga(struct libztex_device *ztex)
  492. {
  493. return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
  494. }
  495. int libztex_suspend(struct libztex_device *ztex)
  496. {
  497. if (ztex->suspendSupported) {
  498. return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
  499. } else {
  500. return 0;
  501. }
  502. }
  503. int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
  504. {
  505. struct libztex_device *newdev = *ztex;
  506. int i, cnt, err;
  507. unsigned char buf[64];
  508. dclk_prepare(&newdev->dclk);
  509. newdev->dclk.freqMinM = 0;
  510. err = libusb_open(dev, &newdev->hndl);
  511. if (err != LIBUSB_SUCCESS) {
  512. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  513. return CHECK_ERROR;
  514. }
  515. err = libusb_get_device_descriptor(dev, &newdev->descriptor);
  516. if (unlikely(err != 0)) {
  517. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  518. return CHECK_ERROR;
  519. }
  520. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
  521. if (unlikely(cnt < 0)) {
  522. applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
  523. return cnt;
  524. }
  525. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iProduct, buf, sizeof(buf));
  526. if (unlikely(cnt < 0))
  527. applog(LOG_WARNING, "Ztex check device: Failed to read device product with err %d", cnt);
  528. else
  529. newdev->dev_product = buf[0] ? strdup((void*)buf) : NULL;
  530. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iManufacturer, buf, sizeof(buf));
  531. if (unlikely(cnt < 0))
  532. applog(LOG_WARNING, "Ztex check device: Failed to read device manufacturer with err %d", cnt);
  533. else
  534. newdev->dev_manufacturer = buf[0] ? strdup((void*)buf) : NULL;
  535. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  536. if (unlikely(cnt < 0)) {
  537. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  538. return cnt;
  539. }
  540. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  541. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  542. return 2;
  543. }
  544. newdev->productId[0] = buf[6];
  545. newdev->productId[1] = buf[7];
  546. newdev->productId[2] = buf[8];
  547. newdev->productId[3] = buf[9];
  548. newdev->fwVersion = buf[10];
  549. newdev->interfaceVersion = buf[11];
  550. newdev->interfaceCapabilities[0] = buf[12];
  551. newdev->interfaceCapabilities[1] = buf[13];
  552. newdev->interfaceCapabilities[2] = buf[14];
  553. newdev->interfaceCapabilities[3] = buf[15];
  554. newdev->interfaceCapabilities[4] = buf[16];
  555. newdev->interfaceCapabilities[5] = buf[17];
  556. newdev->moduleReserved[0] = buf[18];
  557. newdev->moduleReserved[1] = buf[19];
  558. newdev->moduleReserved[2] = buf[20];
  559. newdev->moduleReserved[3] = buf[21];
  560. newdev->moduleReserved[4] = buf[22];
  561. newdev->moduleReserved[5] = buf[23];
  562. newdev->moduleReserved[6] = buf[24];
  563. newdev->moduleReserved[7] = buf[25];
  564. newdev->moduleReserved[8] = buf[26];
  565. newdev->moduleReserved[9] = buf[27];
  566. newdev->moduleReserved[10] = buf[28];
  567. newdev->moduleReserved[11] = buf[29];
  568. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
  569. if (unlikely(cnt < 0)) {
  570. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  571. return cnt;
  572. }
  573. if (unlikely(buf[0] != 5)) {
  574. if (unlikely(buf[0] != 2 && buf[0] != 4)) {
  575. applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
  576. return 3;
  577. }
  578. applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]);
  579. }
  580. i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
  581. while (cnt < 64 && buf[cnt] != 0)
  582. cnt++;
  583. if (cnt < i + 1) {
  584. applog(LOG_ERR, "Invalid bitstream file name .");
  585. return 4;
  586. }
  587. newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
  588. memcpy(newdev->bitFileName, &buf[i], cnt);
  589. newdev->bitFileName[cnt] = 0;
  590. newdev->numNonces = buf[1] + 1;
  591. newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
  592. newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
  593. newdev->dclk.freqMaxM = (buf[7] & 255);
  594. newdev->dclk.freqM = (buf[6] & 255);
  595. newdev->dclk.freqMDefault = newdev->dclk.freqM;
  596. newdev->suspendSupported = (buf[0] == 5);
  597. newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
  598. newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
  599. applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
  600. buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->dclk.freqMaxM, newdev->dclk.freqM, newdev->suspendSupported ? "T": "F",
  601. newdev->hashesPerClock, newdev->extraSolutions);
  602. if (buf[0] < 4) {
  603. if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
  604. newdev->hashesPerClock = 0.5;
  605. applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock);
  606. }
  607. newdev->usbbus = libusb_get_bus_number(dev);
  608. newdev->usbaddress = libusb_get_device_address(dev);
  609. sprintf(newdev->repr, "ZTEX %s-1", newdev->snString);
  610. return 0;
  611. }
  612. void libztex_destroy_device(struct libztex_device* ztex)
  613. {
  614. if (ztex->hndl != NULL) {
  615. libusb_release_interface(ztex->hndl, 0);
  616. libusb_close(ztex->hndl);
  617. ztex->hndl = NULL;
  618. }
  619. if (ztex->bitFileName != NULL) {
  620. free(ztex->bitFileName);
  621. ztex->bitFileName = NULL;
  622. }
  623. free(ztex);
  624. }
  625. int libztex_scanDevices(struct libztex_dev_list*** devs_p)
  626. {
  627. int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
  628. struct libztex_dev_list **devs = NULL;
  629. struct libztex_device *ztex = NULL;
  630. int found, max_found = 0, pos = 0, err, rescan, ret = 0;
  631. libusb_device **list = NULL;
  632. ssize_t cnt, i;
  633. int skipped = 0;
  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. if (bfg_claim_libusb(NULL, false, list[i]))
  642. {
  643. ++skipped;
  644. continue;
  645. }
  646. err = libztex_checkDevice(list[i]);
  647. switch (err) {
  648. case CHECK_ERROR:
  649. applog(LOG_ERR, "Ztex: Can not check device %ld", (long)i);
  650. continue;
  651. case CHECK_IS_NOT_ZTEX:
  652. continue;
  653. case CHECK_OK:
  654. // Got one!
  655. usbdevices[found++] = i;
  656. break;
  657. case CHECK_RESCAN:
  658. rescan = 1;
  659. found++;
  660. break;
  661. }
  662. }
  663. if (found < max_found)
  664. rescan = 1;
  665. else if (found > max_found)
  666. max_found = found;
  667. if (rescan)
  668. libusb_free_device_list(list, 1);
  669. } while (rescan);
  670. if (0 == found)
  671. goto done;
  672. devs = malloc(sizeof(struct libztex_dev_list *) * found);
  673. if (devs == NULL) {
  674. applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
  675. goto done;
  676. }
  677. for (i = 0; i < found; i++) {
  678. if (!ztex) {
  679. ztex = malloc(sizeof(*ztex));
  680. if (!ztex) {
  681. applog(LOG_ERR, "%s: Can not allocate memory for device struct: %s", __func__, bfg_strerror(errno, BST_ERRNO));
  682. goto done;
  683. }
  684. }
  685. ztex->bitFileName = NULL;
  686. ztex->numberOfFpgas = -1;
  687. err = libztex_prepare_device(list[usbdevices[i]], &ztex);
  688. if (unlikely(err != 0)) {
  689. applog(LOG_ERR, "prepare device: %d", err);
  690. libztex_destroy_device(ztex);
  691. ztex = NULL;
  692. continue;
  693. }
  694. devs[pos] = malloc(sizeof(struct libztex_dev_list));
  695. if (NULL == devs[pos]) {
  696. applog(LOG_ERR, "%s: Can not allocate memory for device: %s", __func__, bfg_strerror(errno, BST_ERRNO));
  697. libztex_destroy_device(ztex);
  698. ztex = NULL;
  699. continue;
  700. }
  701. devs[pos]->dev = ztex;
  702. ztex = NULL;
  703. devs[pos]->next = NULL;
  704. if (pos > 0)
  705. devs[pos - 1]->next = devs[pos];
  706. pos++;
  707. }
  708. ret = pos;
  709. done:
  710. if (ret > 0)
  711. *devs_p = devs;
  712. else if (devs)
  713. free(devs);
  714. if (list)
  715. libusb_free_device_list(list, 1);
  716. if (skipped)
  717. applog(LOG_DEBUG, "%s: Skipping probe of %d claimed devices", __func__, skipped);
  718. return ret;
  719. }
  720. int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
  721. {
  722. int cnt = 0, ret, len;
  723. if (ztex == NULL || ztex->hndl == NULL)
  724. return 0;
  725. ret = 44; len = 0;
  726. while (ret > 0) {
  727. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf + len, ret, 1000);
  728. if (cnt >= 0) {
  729. ret -= cnt;
  730. len += cnt;
  731. } else
  732. break;
  733. }
  734. if (unlikely(cnt < 0))
  735. applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
  736. return cnt;
  737. }
  738. int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
  739. {
  740. int bufsize = 12 + ztex->extraSolutions * 4;
  741. int cnt = 0, i, j, ret, len;
  742. unsigned char *rbuf;
  743. if (ztex->hndl == NULL)
  744. return 0;
  745. rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
  746. if (rbuf == NULL) {
  747. applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
  748. return 0;
  749. }
  750. ret = bufsize * ztex->numNonces; len = 0;
  751. while (ret > 0) {
  752. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf + len, ret, 1000);
  753. if (cnt >= 0) {
  754. ret -= cnt;
  755. len += cnt;
  756. } else
  757. break;
  758. }
  759. if (unlikely(cnt < 0)) {
  760. applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
  761. free(rbuf);
  762. return cnt;
  763. }
  764. for (i=0; i<ztex->numNonces; i++) {
  765. uint32_t *nonce_data = (void*)&rbuf[i * bufsize];
  766. nonces[i].goldenNonce[0] = nonce_data[0] - ztex->offsNonces;
  767. //applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
  768. nonces[i].nonce = le32toh(nonce_data[1]) - ztex->offsNonces;
  769. nonces[i].hash7 = le32toh(nonce_data[2]);
  770. for (j = 1; j <= ztex->extraSolutions; ++j)
  771. {
  772. nonces[i].goldenNonce[j] = le32toh(nonce_data[2 + j]) - ztex->offsNonces;
  773. //applog(LOG_DEBUG, "W %d:%d %0.8x", i, j, nonces[i].goldenNonce[j]);
  774. }
  775. }
  776. free(rbuf);
  777. return cnt;
  778. }
  779. void libztex_freeDevList(struct libztex_dev_list **devs)
  780. {
  781. bool done = false;
  782. ssize_t cnt = 0;
  783. while (!done) {
  784. if (devs[cnt]->next == NULL)
  785. done = true;
  786. free(devs[cnt++]);
  787. }
  788. free(devs);
  789. }