libztex.c 25 KB

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