libztex.c 25 KB

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