libztex.c 24 KB

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