libztex.c 23 KB

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