libztex.c 25 KB

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