libztex.c 24 KB

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