libztex.c 24 KB

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