libztex.c 26 KB

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