libztex.c 24 KB

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