libztex.c 25 KB

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