libztex.c 25 KB

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