libztex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 <stdbool.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include "binloader.h"
  31. #include "compat.h"
  32. #include "deviceapi.h"
  33. #include "dynclock.h"
  34. #include "miner.h"
  35. #include "libztex.h"
  36. #include "util.h"
  37. //* Capability index for EEPROM support.
  38. #define CAPABILITY_EEPROM 0,0
  39. //* Capability index for FPGA configuration support.
  40. #define CAPABILITY_FPGA 0,1
  41. //* Capability index for FLASH memory support.
  42. #define CAPABILITY_FLASH 0,2
  43. //* Capability index for DEBUG helper support.
  44. #define CAPABILITY_DEBUG 0,3
  45. //* Capability index for AVR XMEGA support.
  46. #define CAPABILITY_XMEGA 0,4
  47. //* Capability index for AVR XMEGA support.
  48. #define CAPABILITY_HS_FPGA 0,5
  49. //* Capability index for AVR XMEGA support.
  50. #define CAPABILITY_MAC_EEPROM 0,6
  51. //* Capability index for multi FPGA support.
  52. #define CAPABILITY_MULTI_FPGA 0,7
  53. static int libztex_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index,
  54. unsigned char *data, int length)
  55. {
  56. int i, cnt;
  57. uint16_t langid;
  58. unsigned char buf[260];
  59. /* We open code string descriptor retrieval and ASCII decoding here
  60. * in order to work around that libusb_get_string_descriptor_ascii()
  61. * in the FreeBSD libusb implementation hits a bug in ZTEX firmware,
  62. * where the device returns more bytes than requested, causing babble,
  63. * which makes FreeBSD return an error to us.
  64. *
  65. * Avoid the mess by doing it manually the same way as libusb-1.0.
  66. */
  67. cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  68. LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
  69. 0x0000, buf, sizeof(buf), 1000);
  70. if (cnt < 0) {
  71. applog(LOG_ERR, "%s: Failed to read LANGIDs: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  72. return cnt;
  73. }
  74. langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
  75. cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
  76. LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
  77. langid, buf, sizeof(buf), 1000);
  78. if (cnt < 0) {
  79. applog(LOG_ERR, "%s: Failed to read string descriptor: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  80. return cnt;
  81. }
  82. /* num chars = (all bytes except bLength and bDescriptorType) / 2 */
  83. for (i = 0; i <= (cnt - 2) / 2 && i < length-1; i++)
  84. data[i] = buf[2 + i*2];
  85. data[i] = 0;
  86. return LIBUSB_SUCCESS;
  87. }
  88. static bool libztex_firmwareReset(struct libusb_device_handle *hndl, bool enable)
  89. {
  90. uint8_t reset = enable ? 1 : 0;
  91. int cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, &reset, 1, 1000);
  92. if (cnt < 0)
  93. {
  94. applog(LOG_ERR, "Ztex reset %d failed: %s", enable, bfg_strerror(cnt, BST_LIBUSB));
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. enum ztex_check_result libztex_checkDevice(struct libusb_device *dev)
  100. {
  101. libusb_device_handle *hndl = NULL;
  102. struct libusb_device_descriptor desc;
  103. int ret = CHECK_ERROR, err, cnt;
  104. unsigned char buf[64];
  105. unsigned int i;
  106. bytes_t bsdata = BYTES_INIT;
  107. err = libusb_get_device_descriptor(dev, &desc);
  108. if (unlikely(err != 0)) {
  109. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  110. return CHECK_ERROR;
  111. }
  112. if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) {
  113. applog(LOG_DEBUG, "Not a ZTEX device %04x:%04x", desc.idVendor, desc.idProduct);
  114. return CHECK_IS_NOT_ZTEX;
  115. }
  116. err = libusb_open(dev, &hndl);
  117. if (err != LIBUSB_SUCCESS) {
  118. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  119. goto done;
  120. }
  121. if (libusb_claim_interface(hndl, 0) == LIBUSB_ERROR_BUSY)
  122. {
  123. applog(LOG_DEBUG, "Ztex check device: Interface already busy, skipping");
  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";
  172. }
  173. else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13)
  174. {
  175. firmware = "ztex_ufm1_15d4";
  176. }
  177. else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15)
  178. {
  179. firmware = "ztex_ufm1_15y1";
  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. bytes_init(&bsdata);
  188. if (!load_bitstream_bytes(&bsdata, "ztex", "ZTX *", firmware))
  189. goto done;
  190. // in buf[] is still the identifier of the dummy firmware
  191. // use it to compare it with the new firmware
  192. char *rv = memmem(bytes_buf(&bsdata), bytes_len(&bsdata), buf, 8);
  193. if (rv == NULL)
  194. {
  195. applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__);
  196. goto done;
  197. }
  198. // check for dummy firmware
  199. if (rv[8] == 0)
  200. {
  201. applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__);
  202. goto done;
  203. }
  204. if (libztex_firmwareReset(hndl, true))
  205. goto done;
  206. for (i = 0; i < bytes_len(&bsdata); i+= 256) {
  207. // firmware wants data in small chunks like 256 bytes
  208. int numbytes = (bytes_len(&bsdata) - i) < 256 ? (bytes_len(&bsdata) - i) : 256;
  209. int k = libusb_control_transfer(hndl, 0x40, 0xA0, i, 0, bytes_buf(&bsdata) + i, numbytes, 1000);
  210. if (k < numbytes)
  211. {
  212. applog(LOG_ERR, "Ztex device: Failed to write firmware at %d with: %s", i, bfg_strerror(k, BST_LIBUSB));
  213. goto done;
  214. }
  215. }
  216. if (libztex_firmwareReset(hndl, false))
  217. goto done;
  218. applog(LOG_ERR, "Ztex device: succesfully wrote firmware");
  219. ret = CHECK_RESCAN;
  220. done:
  221. bytes_free(&bsdata);
  222. if (hndl)
  223. {
  224. libusb_release_interface(hndl, 0);
  225. libusb_close(hndl);
  226. }
  227. return ret;
  228. }
  229. static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
  230. {
  231. if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
  232. (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
  233. applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
  234. return false;
  235. }
  236. return true;
  237. }
  238. static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
  239. {
  240. int i;
  241. for (i = 0; i < size - 4; i++) {
  242. if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
  243. return 1;
  244. if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
  245. return 0;
  246. }
  247. applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
  248. return 0;
  249. }
  250. static void libztex_swapBits(unsigned char *buf, int size)
  251. {
  252. unsigned char c;
  253. int i;
  254. for (i = 0; i < size; i++) {
  255. c = buf[i];
  256. buf[i] = ((c & 128) >> 7) |
  257. ((c & 64) >> 5) |
  258. ((c & 32) >> 3) |
  259. ((c & 16) >> 1) |
  260. ((c & 8) << 1) |
  261. ((c & 4) << 3) |
  262. ((c & 2) << 5) |
  263. ((c & 1) << 7);
  264. }
  265. }
  266. static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
  267. {
  268. unsigned char buf[9];
  269. int cnt;
  270. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  271. return -1;
  272. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
  273. if (unlikely(cnt < 0)) {
  274. applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
  275. return cnt;
  276. }
  277. state->fpgaConfigured = (buf[0] == 0);
  278. state->fpgaChecksum = buf[1] & 0xff;
  279. state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
  280. state->fpgaInitB = buf[6] & 0xff;
  281. state->fpgaFlashResult = buf[7];
  282. state->fpgaFlashBitSwap = (buf[8] != 0);
  283. return 0;
  284. }
  285. static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  286. {
  287. struct libztex_fpgastate state;
  288. const int transactionBytes = 65536;
  289. unsigned char buf[transactionBytes], settings[2];
  290. int tries, cnt, err;
  291. FILE *fp;
  292. if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
  293. return -1;
  294. libztex_getFpgaState(ztex, &state);
  295. if (!force && state.fpgaConfigured) {
  296. applog(LOG_INFO, "Bitstream already configured");
  297. return 0;
  298. }
  299. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
  300. if (unlikely(cnt < 0)) {
  301. applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt);
  302. return cnt;
  303. }
  304. err = libusb_claim_interface(ztex->hndl, settings[1]);
  305. if (err != LIBUSB_SUCCESS) {
  306. applog(LOG_ERR, "%s: failed to claim interface for hs transfer", ztex->repr);
  307. return -4;
  308. }
  309. for (tries = 3; tries > 0; tries--) {
  310. fp = open_bitstream("ztex", firmware);
  311. if (!fp) {
  312. applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
  313. libusb_release_interface(ztex->hndl, settings[1]);
  314. return -2;
  315. }
  316. libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
  317. // 0x34 - initHSFPGAConfiguration
  318. do
  319. {
  320. int length = fread(buf,1,transactionBytes,fp);
  321. if (bs != 0 && bs != 1)
  322. bs = libztex_detectBitstreamBitOrder(buf, length);
  323. if (bs == 1)
  324. libztex_swapBits(buf, length);
  325. err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000);
  326. if (cnt != length)
  327. applog(LOG_ERR, "%s: cnt != length", ztex->repr);
  328. if (err != 0)
  329. applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
  330. }
  331. while (!feof(fp));
  332. // While 1.15y can finish immediately, at least 1.15x needs some delay
  333. // (200ms might be enough, but 500ms is safer)
  334. if (ztex->productId[1] != 15)
  335. usleep(500);
  336. libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
  337. // 0x35 - finishHSFPGAConfiguration
  338. if (cnt >= 0)
  339. tries = 0;
  340. fclose(fp);
  341. libztex_getFpgaState(ztex, &state);
  342. if (!state.fpgaConfigured) {
  343. applog(LOG_ERR, "%"PRIpreprv": HS FPGA configuration failed: DONE pin does not go high", repr);
  344. libusb_release_interface(ztex->hndl, settings[1]);
  345. return -3;
  346. }
  347. }
  348. libusb_release_interface(ztex->hndl, settings[1]);
  349. cgsleep_ms(200);
  350. applog(LOG_INFO, "%"PRIpreprv": HS FPGA configuration done", repr);
  351. return 0;
  352. }
  353. static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
  354. {
  355. struct libztex_fpgastate state;
  356. const int transactionBytes = 2048;
  357. unsigned char buf[transactionBytes];
  358. int tries, cnt;
  359. FILE *fp;
  360. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  361. return -1;
  362. libztex_getFpgaState(ztex, &state);
  363. if (!force && state.fpgaConfigured) {
  364. applog(LOG_DEBUG, "Bitstream already configured");
  365. return 0;
  366. }
  367. for (tries = 10; tries > 0; tries--) {
  368. fp = open_bitstream("ztex", firmware);
  369. if (!fp) {
  370. _bitstream_not_found(repr, firmware);
  371. return -2;
  372. }
  373. //* Reset fpga
  374. cnt = libztex_resetFpga(ztex);
  375. if (unlikely(cnt < 0)) {
  376. applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
  377. continue;
  378. }
  379. do
  380. {
  381. int length = fread(buf, 1, transactionBytes, fp);
  382. if (bs != 0 && bs != 1)
  383. bs = libztex_detectBitstreamBitOrder(buf, length);
  384. if (bs == 1)
  385. libztex_swapBits(buf, length);
  386. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, buf, length, 5000);
  387. if (cnt != length)
  388. {
  389. applog(LOG_ERR, "%s: Failed send ls fpga data", ztex->repr);
  390. break;
  391. }
  392. }
  393. while (!feof(fp));
  394. if (cnt > 0)
  395. tries = 0;
  396. fclose(fp);
  397. }
  398. libztex_getFpgaState(ztex, &state);
  399. if (!state.fpgaConfigured) {
  400. applog(LOG_ERR, "%"PRIpreprv": LS FPGA configuration failed: DONE pin does not go high", repr);
  401. return -3;
  402. }
  403. cgsleep_ms(200);
  404. applog(LOG_INFO, "%"PRIpreprv": FPGA configuration done", repr);
  405. return 0;
  406. }
  407. int libztex_configureFpga(struct libztex_device *ztex, const char *repr)
  408. {
  409. char buf[256];
  410. int rv;
  411. strcpy(buf, ztex->bitFileName);
  412. strcat(buf, ".bit");
  413. rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
  414. if (rv != 0)
  415. rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
  416. if (!rv)
  417. if (libusb_claim_interface(ztex->hndl, 0) == LIBUSB_ERROR_BUSY)
  418. rv = -5;
  419. return rv;
  420. }
  421. int libztex_numberOfFpgas(struct libztex_device *ztex)
  422. {
  423. int cnt;
  424. unsigned char buf[3];
  425. if (ztex->numberOfFpgas < 0) {
  426. if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
  427. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
  428. if (unlikely(cnt < 0)) {
  429. applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
  430. return cnt;
  431. }
  432. ztex->numberOfFpgas = buf[0] + 1;
  433. ztex->selectedFpga = -1;//buf[1];
  434. ztex->parallelConfigSupport = (buf[2] == 1);
  435. } else {
  436. ztex->numberOfFpgas = 1;
  437. ztex->selectedFpga = -1;//0;
  438. ztex->parallelConfigSupport = false;
  439. }
  440. }
  441. return ztex->numberOfFpgas;
  442. }
  443. int libztex_selectFpga(struct libztex_device *ztex, int16_t number)
  444. {
  445. int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root);
  446. if (number < 0 || number >= fpgacnt) {
  447. applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
  448. return 1;
  449. }
  450. if (ztex->root->selectedFpga != number && libztex_checkCapability(ztex->root, CAPABILITY_MULTI_FPGA)) {
  451. cnt = libusb_control_transfer(ztex->root->hndl, 0x40, 0x51, (uint16_t)number, 0, NULL, 0, 500);
  452. if (unlikely(cnt < 0)) {
  453. applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
  454. ztex->root->selectedFpga = -1;
  455. return cnt;
  456. }
  457. ztex->root->selectedFpga = number;
  458. }
  459. return 0;
  460. }
  461. int libztex_setFreq(struct libztex_device *ztex, uint16_t freq, const char *repr)
  462. {
  463. int cnt;
  464. uint16_t oldfreq = ztex->dclk.freqM;
  465. if (freq > ztex->dclk.freqMaxM)
  466. freq = ztex->dclk.freqMaxM;
  467. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
  468. if (unlikely(cnt < 0)) {
  469. applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
  470. return cnt;
  471. }
  472. ztex->dclk.freqM = freq;
  473. if (oldfreq > ztex->dclk.freqMaxM)
  474. applog(LOG_WARNING, "%"PRIpreprv": Frequency set to %u MHz (range: %u-%u)",
  475. repr,
  476. (unsigned)(ztex->freqM1 * (ztex->dclk.freqM + 1)),
  477. (unsigned)ztex->freqM1,
  478. (unsigned)(ztex->freqM1 * (ztex->dclk.freqMaxM + 1))
  479. );
  480. else
  481. dclk_msg_freqchange(repr,
  482. ztex->freqM1 * (oldfreq + 1),
  483. ztex->freqM1 * (ztex->dclk.freqM + 1),
  484. NULL);
  485. return 0;
  486. }
  487. int libztex_resetFpga(struct libztex_device *ztex)
  488. {
  489. return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
  490. }
  491. int libztex_suspend(struct libztex_device *ztex)
  492. {
  493. if (ztex->suspendSupported) {
  494. return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
  495. } else {
  496. return 0;
  497. }
  498. }
  499. struct libztex_device *libztex_prepare_device2(struct libusb_device * const dev)
  500. {
  501. struct libztex_device *newdev;
  502. int i, cnt, err;
  503. unsigned char buf[64];
  504. newdev = malloc(sizeof(*newdev));
  505. if (!newdev)
  506. applogr(NULL, LOG_ERR, "%s: Failed to malloc libztex_device", __func__);
  507. newdev->bitFileName = NULL;
  508. newdev->numberOfFpgas = -1;
  509. dclk_prepare(&newdev->dclk);
  510. newdev->dclk.freqMinM = 0;
  511. err = libusb_open(dev, &newdev->hndl);
  512. if (err != LIBUSB_SUCCESS) {
  513. applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  514. return NULL;
  515. }
  516. err = libusb_get_device_descriptor(dev, &newdev->descriptor);
  517. if (unlikely(err != 0)) {
  518. applogr(NULL, LOG_ERR, "%s: Failed to open read descriptor: %s", __func__, bfg_strerror(err, BST_LIBUSB));
  519. }
  520. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
  521. if (unlikely(cnt < 0)) {
  522. applogr(NULL, LOG_ERR, "%s: Failed to read device snString: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  523. }
  524. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iProduct, buf, sizeof(buf));
  525. if (unlikely(cnt < 0))
  526. applog(LOG_WARNING, "%s: Failed to read device product: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  527. else
  528. newdev->dev_product = buf[0] ? strdup((void*)buf) : NULL;
  529. cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iManufacturer, buf, sizeof(buf));
  530. if (unlikely(cnt < 0))
  531. applog(LOG_WARNING, "%s: Failed to read device manufacturer: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  532. else
  533. newdev->dev_manufacturer = buf[0] ? strdup((void*)buf) : NULL;
  534. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  535. if (unlikely(cnt < 0)) {
  536. applogr(NULL, LOG_ERR, "%s: Failed to read ztex descriptor: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  537. }
  538. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  539. applogr(NULL, LOG_ERR, "%s: Unexpected data reading ztex descriptor", __func__);
  540. }
  541. newdev->productId[0] = buf[6];
  542. newdev->productId[1] = buf[7];
  543. newdev->productId[2] = buf[8];
  544. newdev->productId[3] = buf[9];
  545. newdev->fwVersion = buf[10];
  546. newdev->interfaceVersion = buf[11];
  547. newdev->interfaceCapabilities[0] = buf[12];
  548. newdev->interfaceCapabilities[1] = buf[13];
  549. newdev->interfaceCapabilities[2] = buf[14];
  550. newdev->interfaceCapabilities[3] = buf[15];
  551. newdev->interfaceCapabilities[4] = buf[16];
  552. newdev->interfaceCapabilities[5] = buf[17];
  553. newdev->moduleReserved[0] = buf[18];
  554. newdev->moduleReserved[1] = buf[19];
  555. newdev->moduleReserved[2] = buf[20];
  556. newdev->moduleReserved[3] = buf[21];
  557. newdev->moduleReserved[4] = buf[22];
  558. newdev->moduleReserved[5] = buf[23];
  559. newdev->moduleReserved[6] = buf[24];
  560. newdev->moduleReserved[7] = buf[25];
  561. newdev->moduleReserved[8] = buf[26];
  562. newdev->moduleReserved[9] = buf[27];
  563. newdev->moduleReserved[10] = buf[28];
  564. newdev->moduleReserved[11] = buf[29];
  565. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
  566. if (unlikely(cnt < 0)) {
  567. applogr(NULL, LOG_ERR, "%s: Failed to read ztex descriptor: %s", __func__, bfg_strerror(cnt, BST_LIBUSB));
  568. }
  569. if (unlikely(buf[0] != 5)) {
  570. if (unlikely(buf[0] != 2 && buf[0] != 4)) {
  571. applogr(NULL, LOG_ERR, "%s: Invalid BTCMiner descriptor version (%d). Firmware must be updated.", __func__, buf[0]);
  572. return NULL;
  573. }
  574. applog(LOG_WARNING, "%s: Firmware out of date (%d).", __func__, buf[0]);
  575. }
  576. i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
  577. while (cnt < 64 && buf[cnt] != 0)
  578. cnt++;
  579. if (cnt < i + 1) {
  580. applogr(NULL, LOG_ERR, "%s: Invalid bitstream file name.", __func__);
  581. }
  582. newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
  583. memcpy(newdev->bitFileName, &buf[i], cnt);
  584. newdev->bitFileName[cnt] = 0;
  585. newdev->numNonces = buf[1] + 1;
  586. newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
  587. newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
  588. newdev->dclk.freqMaxM = (buf[7] & 255);
  589. newdev->dclk.freqM = (buf[6] & 255);
  590. newdev->dclk.freqMDefault = newdev->dclk.freqM;
  591. newdev->suspendSupported = (buf[0] == 5);
  592. newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
  593. newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
  594. applog(LOG_DEBUG, "%s: PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
  595. __func__,
  596. buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->dclk.freqMaxM, newdev->dclk.freqM, newdev->suspendSupported ? "T": "F",
  597. newdev->hashesPerClock, newdev->extraSolutions);
  598. if (buf[0] < 4) {
  599. if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
  600. newdev->hashesPerClock = 0.5;
  601. applog(LOG_WARNING, "%s: HASHES_PER_CLOCK not defined, assuming %0.2f", __func__, newdev->hashesPerClock);
  602. }
  603. newdev->usbbus = libusb_get_bus_number(dev);
  604. newdev->usbaddress = libusb_get_device_address(dev);
  605. sprintf(newdev->repr, "ZTEX %s-1", newdev->snString);
  606. return newdev;
  607. }
  608. void libztex_destroy_device(struct libztex_device* ztex)
  609. {
  610. if (ztex->hndl != NULL) {
  611. libusb_release_interface(ztex->hndl, 0);
  612. libusb_close(ztex->hndl);
  613. ztex->hndl = NULL;
  614. }
  615. if (ztex->bitFileName != NULL) {
  616. free(ztex->bitFileName);
  617. ztex->bitFileName = NULL;
  618. }
  619. free(ztex);
  620. }
  621. int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
  622. {
  623. int cnt = 0, ret, len;
  624. if (ztex == NULL || ztex->hndl == NULL)
  625. return 0;
  626. ret = 44; len = 0;
  627. while (ret > 0) {
  628. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf + len, ret, 1000);
  629. if (cnt >= 0) {
  630. ret -= cnt;
  631. len += cnt;
  632. } else
  633. break;
  634. }
  635. if (unlikely(cnt < 0))
  636. applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
  637. return cnt;
  638. }
  639. int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
  640. {
  641. int bufsize = 12 + ztex->extraSolutions * 4;
  642. int cnt = 0, i, j, ret, len;
  643. unsigned char *rbuf;
  644. if (ztex->hndl == NULL)
  645. return 0;
  646. rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
  647. if (rbuf == NULL) {
  648. applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
  649. return 0;
  650. }
  651. ret = bufsize * ztex->numNonces; len = 0;
  652. while (ret > 0) {
  653. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf + len, ret, 1000);
  654. if (cnt >= 0) {
  655. ret -= cnt;
  656. len += cnt;
  657. } else
  658. break;
  659. }
  660. if (unlikely(cnt < 0)) {
  661. applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
  662. free(rbuf);
  663. return cnt;
  664. }
  665. for (i=0; i<ztex->numNonces; i++) {
  666. uint32_t *nonce_data = (void*)&rbuf[i * bufsize];
  667. nonces[i].goldenNonce[0] = nonce_data[0] - ztex->offsNonces;
  668. //applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
  669. nonces[i].nonce = le32toh(nonce_data[1]) - ztex->offsNonces;
  670. nonces[i].hash7 = le32toh(nonce_data[2]);
  671. for (j = 1; j <= ztex->extraSolutions; ++j)
  672. {
  673. nonces[i].goldenNonce[j] = le32toh(nonce_data[2 + j]) - ztex->offsNonces;
  674. //applog(LOG_DEBUG, "W %d:%d %0.8x", i, j, nonces[i].goldenNonce[j]);
  675. }
  676. }
  677. free(rbuf);
  678. return cnt;
  679. }
  680. void libztex_freeDevList(struct libztex_dev_list **devs)
  681. {
  682. bool done = false;
  683. ssize_t cnt = 0;
  684. while (!done) {
  685. if (devs[cnt]->next == NULL)
  686. done = true;
  687. free(devs[cnt++]);
  688. }
  689. free(devs);
  690. }