libztex.c 25 KB

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