libztex.c 24 KB

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