libztex.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**
  2. * libztex.c - Ztex 1.15x fpga board support library
  3. *
  4. * Copyright (c) 2012 nelisky.btc@gmail.com
  5. *
  6. * This work is based upon the Java SDK provided by ztex which is
  7. * Copyright (C) 2009-2011 ZTEX GmbH.
  8. * http://www.ztex.de
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see http://www.gnu.org/licenses/.
  21. **/
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include "miner.h"
  25. #include "libztex.h"
  26. #define BUFSIZE 256
  27. //* Capability index for EEPROM support.
  28. #define CAPABILITY_EEPROM 0,0
  29. //* Capability index for FPGA configuration support.
  30. #define CAPABILITY_FPGA 0,1
  31. //* Capability index for FLASH memory support.
  32. #define CAPABILITY_FLASH 0,2
  33. //* Capability index for DEBUG helper support.
  34. #define CAPABILITY_DEBUG 0,3
  35. //* Capability index for AVR XMEGA support.
  36. #define CAPABILITY_XMEGA 0,4
  37. //* Capability index for AVR XMEGA support.
  38. #define CAPABILITY_HS_FPGA 0,5
  39. //* Capability index for AVR XMEGA support.
  40. #define CAPABILITY_MAC_EEPROM 0,6
  41. //* Capability index for multi FPGA support.
  42. #define CAPABILITY_MULTI_FPGA 0,7
  43. static bool libztex_checkDevice(struct libusb_device *dev)
  44. {
  45. struct libusb_device_descriptor desc;
  46. int err;
  47. err = libusb_get_device_descriptor(dev, &desc);
  48. if (unlikely(err != 0)) {
  49. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  50. return false;
  51. }
  52. if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
  53. applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
  54. return false;
  55. }
  56. return true;
  57. }
  58. static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
  59. {
  60. if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
  61. (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
  62. applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
  63. return false;
  64. }
  65. return true;
  66. }
  67. static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
  68. {
  69. int i;
  70. for (i = 0; i < size - 4; i++) {
  71. if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
  72. return 1;
  73. if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
  74. return 0;
  75. }
  76. applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
  77. return 0;
  78. }
  79. static void libztex_swapBits(unsigned char *buf, int size)
  80. {
  81. unsigned char c;
  82. int i;
  83. for (i = 0; i < size; i++) {
  84. c = buf[i];
  85. buf[i] = ((c & 128) >> 7) |
  86. ((c & 64) >> 5) |
  87. ((c & 32) >> 3) |
  88. ((c & 16) >> 1) |
  89. ((c & 8) << 1) |
  90. ((c & 4) << 3) |
  91. ((c & 2) << 5) |
  92. ((c & 1) << 7);
  93. }
  94. }
  95. static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
  96. {
  97. unsigned char buf[9];
  98. int cnt;
  99. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  100. return -1;
  101. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
  102. if (unlikely(cnt < 0)) {
  103. applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
  104. return cnt;
  105. }
  106. state->fpgaConfigured = (buf[0] == 0);
  107. state->fpgaChecksum = buf[1] & 0xff;
  108. state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
  109. state->fpgaInitB = buf[6] & 0xff;
  110. state->fpgaFlashResult = buf[7];
  111. state->fpgaFlashBitSwap = (buf[8] != 0);
  112. return 0;
  113. }
  114. static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
  115. {
  116. struct libztex_fpgastate state;
  117. const int transactionBytes = 65536;
  118. unsigned char buf[transactionBytes], settings[2];
  119. int tries, cnt, buf_p, i;
  120. ssize_t pos = 0;
  121. FILE *fp;
  122. if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
  123. return -1;
  124. libztex_getFpgaState(ztex, &state);
  125. if (!force && state.fpgaConfigured) {
  126. applog(LOG_INFO, "Bitstream already configured");
  127. return 1;
  128. }
  129. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
  130. if (unlikely(cnt < 0)) {
  131. applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt);
  132. return cnt;
  133. }
  134. libusb_claim_interface(ztex->hndl, settings[1]);
  135. for (tries = 3; tries > 0; tries--) {
  136. fp = fopen(firmware, "rb");
  137. if (!fp) {
  138. applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
  139. return -2;
  140. }
  141. while (pos < transactionBytes && !feof(fp)) {
  142. buf[pos++] = getc(fp);
  143. }
  144. if (feof(fp))
  145. pos--;
  146. if (bs != 0 && bs != 1)
  147. bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
  148. if (bs == 1)
  149. libztex_swapBits(buf, pos);
  150. libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
  151. // 0x34 - initHSFPGAConfiguration
  152. buf_p = pos;
  153. while (1) {
  154. i = 0;
  155. while (i < buf_p) {
  156. if (libusb_bulk_transfer(ztex->hndl,
  157. settings[0],
  158. &buf[i],
  159. buf_p - i,
  160. &cnt, 1000) != 0) {
  161. applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
  162. break;
  163. }
  164. usleep(500);
  165. i += cnt;
  166. }
  167. if (i < buf_p || buf_p < transactionBytes)
  168. break;
  169. buf_p = 0;
  170. while (buf_p < transactionBytes && !feof(fp)) {
  171. buf[buf_p++] = getc(fp);
  172. }
  173. if (feof(fp))
  174. buf_p--;
  175. pos += buf_p;
  176. if (buf_p == 0)
  177. break;
  178. if (bs == 1)
  179. libztex_swapBits(buf, buf_p);
  180. }
  181. libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
  182. // 0x35 - finishHSFPGAConfiguration
  183. if (cnt >= 0)
  184. tries = 0;
  185. fclose(fp);
  186. libztex_getFpgaState(ztex, &state);
  187. if (!state.fpgaConfigured) {
  188. applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr);
  189. return -3;
  190. }
  191. }
  192. libusb_release_interface(ztex->hndl, settings[1]);
  193. usleep(200000);
  194. applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr);
  195. return 0;
  196. }
  197. static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
  198. {
  199. struct libztex_fpgastate state;
  200. const int transactionBytes = 2048;
  201. unsigned char buf[transactionBytes], cs;
  202. int tries, cnt, buf_p, i;
  203. ssize_t pos = 0;
  204. FILE *fp;
  205. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  206. return -1;
  207. libztex_getFpgaState(ztex, &state);
  208. if (!force && state.fpgaConfigured) {
  209. applog(LOG_DEBUG, "Bitstream already configured");
  210. return 1;
  211. }
  212. for (tries = 10; tries > 0; tries--) {
  213. fp = fopen(firmware, "rb");
  214. if (!fp) {
  215. applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
  216. return -2;
  217. }
  218. cs = 0;
  219. while (pos < transactionBytes && !feof(fp)) {
  220. buf[pos] = getc(fp);
  221. cs += buf[pos++];
  222. }
  223. if (feof(fp))
  224. pos--;
  225. if (bs != 0 && bs != 1)
  226. bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
  227. //* Reset fpga
  228. cnt = libztex_resetFpga(ztex);
  229. if (unlikely(cnt < 0)) {
  230. applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
  231. continue;
  232. }
  233. if (bs == 1)
  234. libztex_swapBits(buf, pos);
  235. buf_p = pos;
  236. while (1) {
  237. i = 0;
  238. while (i < buf_p) {
  239. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
  240. if (unlikely(cnt < 0)) {
  241. applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
  242. break;
  243. }
  244. i += cnt;
  245. }
  246. if (i < buf_p || buf_p < transactionBytes)
  247. break;
  248. buf_p = 0;
  249. while (buf_p < transactionBytes && !feof(fp)) {
  250. buf[buf_p] = getc(fp);
  251. cs += buf[buf_p++];
  252. }
  253. if (feof(fp))
  254. buf_p--;
  255. pos += buf_p;
  256. if (buf_p == 0)
  257. break;
  258. if (bs == 1)
  259. libztex_swapBits(buf, buf_p);
  260. }
  261. if (cnt >= 0)
  262. tries = 0;
  263. fclose(fp);
  264. }
  265. libztex_getFpgaState(ztex, &state);
  266. if (!state.fpgaConfigured) {
  267. applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
  268. return 3;
  269. }
  270. usleep(200000);
  271. applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
  272. return 0;
  273. }
  274. int libztex_configureFpga(struct libztex_device *ztex)
  275. {
  276. char buf[256] = "bitstreams/";
  277. int rv;
  278. memset(&buf[11], 0, 245);
  279. strcpy(&buf[11], ztex->bitFileName);
  280. strcpy(&buf[strlen(buf)], ".bit");
  281. rv = libztex_configureFpgaHS(ztex, buf, true, 2);
  282. if (rv != 0)
  283. rv = libztex_configureFpgaLS(ztex, buf, true, 2);
  284. return rv;
  285. }
  286. int libztex_numberOfFpgas(struct libztex_device *ztex) {
  287. int cnt;
  288. unsigned char buf[3];
  289. if (ztex->numberOfFpgas < 0) {
  290. if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
  291. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
  292. if (unlikely(cnt < 0)) {
  293. applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
  294. return cnt;
  295. }
  296. ztex->numberOfFpgas = buf[0] + 1;
  297. ztex->selectedFpga = buf[1];
  298. ztex->parallelConfigSupport = (buf[2] == 1);
  299. } else {
  300. ztex->numberOfFpgas = 1;
  301. ztex->selectedFpga = 0;
  302. ztex->parallelConfigSupport = false;
  303. }
  304. }
  305. return ztex->numberOfFpgas;
  306. }
  307. int libztex_selectFpga(struct libztex_device *ztex, int number) {
  308. int cnt, fpgacnt = libztex_numberOfFpgas(ztex);
  309. if (number < 0 || number >= fpgacnt) {
  310. applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
  311. return 1;
  312. }
  313. if (ztex->selectedFpga != number && libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
  314. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x51, number, 0, NULL, 0, 500);
  315. if (unlikely(cnt < 0)) {
  316. applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
  317. return cnt;
  318. }
  319. ztex->selectedFpga = number;
  320. }
  321. return 0;
  322. }
  323. int libztex_setFreq(struct libztex_device *ztex, uint16_t freq) {
  324. int cnt;
  325. uint16_t oldfreq = ztex->freqM;
  326. if (freq > ztex->freqMaxM)
  327. freq = ztex->freqMaxM;
  328. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
  329. if (unlikely(cnt < 0)) {
  330. applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
  331. return cnt;
  332. }
  333. ztex->freqM = freq;
  334. applog(LOG_WARNING, "%s: Frequency change from %0.2f to %0.2f Mhz",
  335. ztex->repr, ztex->freqM1 * (oldfreq + 1), ztex->freqM1 * (ztex->freqM + 1));
  336. return 0;
  337. }
  338. int libztex_resetFpga(struct libztex_device *ztex)
  339. {
  340. return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
  341. }
  342. int libztex_suspend(struct libztex_device *ztex) {
  343. if (ztex->suspendSupported) {
  344. return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
  345. } else {
  346. return 0;
  347. }
  348. }
  349. int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex) {
  350. struct libztex_device *newdev;
  351. int i, cnt, err;
  352. unsigned char buf[64];
  353. newdev = malloc(sizeof(struct libztex_device));
  354. newdev->bitFileName = NULL;
  355. newdev->numberOfFpgas = -1;
  356. newdev->valid = false;
  357. newdev->hndl = NULL;
  358. *ztex = newdev;
  359. err = libusb_get_device_descriptor(dev, &newdev->descriptor);
  360. if (unlikely(err != 0)) {
  361. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  362. return err;
  363. }
  364. // Check vendorId and productId
  365. if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
  366. newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
  367. applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
  368. return 1;
  369. }
  370. err = libusb_open(dev, &newdev->hndl);
  371. if (unlikely(err != 0)) {
  372. applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
  373. return err;
  374. }
  375. cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
  376. LIBZTEX_SNSTRING_LEN + 1);
  377. if (unlikely(cnt < 0)) {
  378. applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
  379. return cnt;
  380. }
  381. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  382. if (unlikely(cnt < 0)) {
  383. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  384. return cnt;
  385. }
  386. if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
  387. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  388. return 2;
  389. }
  390. newdev->productId[0] = buf[6];
  391. newdev->productId[1] = buf[7];
  392. newdev->productId[2] = buf[8];
  393. newdev->productId[3] = buf[9];
  394. newdev->fwVersion = buf[10];
  395. newdev->interfaceVersion = buf[11];
  396. newdev->interfaceCapabilities[0] = buf[12];
  397. newdev->interfaceCapabilities[1] = buf[13];
  398. newdev->interfaceCapabilities[2] = buf[14];
  399. newdev->interfaceCapabilities[3] = buf[15];
  400. newdev->interfaceCapabilities[4] = buf[16];
  401. newdev->interfaceCapabilities[5] = buf[17];
  402. newdev->moduleReserved[0] = buf[18];
  403. newdev->moduleReserved[1] = buf[19];
  404. newdev->moduleReserved[2] = buf[20];
  405. newdev->moduleReserved[3] = buf[21];
  406. newdev->moduleReserved[4] = buf[22];
  407. newdev->moduleReserved[5] = buf[23];
  408. newdev->moduleReserved[6] = buf[24];
  409. newdev->moduleReserved[7] = buf[25];
  410. newdev->moduleReserved[8] = buf[26];
  411. newdev->moduleReserved[9] = buf[27];
  412. newdev->moduleReserved[10] = buf[28];
  413. newdev->moduleReserved[11] = buf[29];
  414. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
  415. if (unlikely(cnt < 0)) {
  416. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  417. return cnt;
  418. }
  419. if (unlikely(buf[0] != 5)) {
  420. if (unlikely(buf[0] != 2 && buf[0] != 4)) {
  421. applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
  422. return 3;
  423. }
  424. applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]);
  425. }
  426. i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
  427. while (cnt < 64 && buf[cnt] != 0)
  428. cnt++;
  429. if (cnt < i + 1) {
  430. applog(LOG_ERR, "Invalid bitstream file name .");
  431. return 4;
  432. }
  433. newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
  434. memcpy(newdev->bitFileName, &buf[i], cnt);
  435. newdev->bitFileName[cnt] = 0;
  436. newdev->numNonces = buf[1] + 1;
  437. newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
  438. newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
  439. newdev->freqMaxM = (buf[7] & 255);
  440. newdev->freqM = (buf[6] & 255);
  441. newdev->freqMDefault = newdev->freqM;
  442. newdev->suspendSupported = (buf[0] == 5);
  443. newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
  444. newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
  445. applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
  446. buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->freqMaxM, newdev->freqM, newdev->suspendSupported ? "T": "F",
  447. newdev->hashesPerClock, newdev->extraSolutions);
  448. if (buf[0] < 4) {
  449. if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
  450. newdev->hashesPerClock = 0.5;
  451. applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock);
  452. }
  453. for (cnt=0; cnt < 255; cnt++) {
  454. newdev->errorCount[cnt] = 0;
  455. newdev->errorWeight[cnt] = 0;
  456. newdev->errorRate[cnt] = 0;
  457. newdev->maxErrorRate[cnt] = 0;
  458. }
  459. newdev->usbbus = libusb_get_bus_number(dev);
  460. newdev->usbaddress = libusb_get_device_address(dev);
  461. sprintf(newdev->repr, "ZTEX %.3d:%.3d-%s", newdev->usbbus, newdev->usbaddress, newdev->snString);
  462. newdev->valid = true;
  463. return 0;
  464. }
  465. void libztex_destroy_device(struct libztex_device* ztex)
  466. {
  467. if (ztex->hndl != NULL) {
  468. libusb_close(ztex->hndl);
  469. ztex->hndl = NULL;
  470. }
  471. if (ztex->bitFileName != NULL) {
  472. free(ztex->bitFileName);
  473. ztex->bitFileName = NULL;
  474. }
  475. free(ztex);
  476. }
  477. int libztex_scanDevices(struct libztex_dev_list*** devs_p)
  478. {
  479. int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
  480. struct libztex_dev_list **devs;
  481. struct libztex_device *ztex;
  482. int found = 0, pos = 0, err;
  483. libusb_device **list;
  484. ssize_t cnt, i = 0;
  485. cnt = libusb_get_device_list(NULL, &list);
  486. if (unlikely(cnt < 0)) {
  487. applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
  488. return 0;
  489. }
  490. for (i = 0; i < cnt; i++) {
  491. if (libztex_checkDevice(list[i])) {
  492. // Got one!
  493. usbdevices[found] = i;
  494. found++;
  495. }
  496. }
  497. devs = malloc(sizeof(struct libztex_dev_list *) * found);
  498. if (devs == NULL) {
  499. applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
  500. return 0;
  501. }
  502. for (i = 0; i < found; i++) {
  503. err = libztex_prepare_device(list[usbdevices[i]], &ztex);
  504. if (unlikely(err != 0))
  505. applog(LOG_ERR, "prepare device: %d", err);
  506. // check if valid
  507. if (!ztex->valid) {
  508. libztex_destroy_device(ztex);
  509. continue;
  510. }
  511. devs[pos] = malloc(sizeof(struct libztex_dev_list));
  512. devs[pos]->dev = ztex;
  513. devs[pos]->next = NULL;
  514. if (pos > 0)
  515. devs[pos - 1]->next = devs[pos];
  516. pos++;
  517. }
  518. libusb_free_device_list(list, 1);
  519. *devs_p = devs;
  520. return pos;
  521. }
  522. int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
  523. {
  524. int cnt;
  525. if (ztex == NULL || ztex->hndl == NULL)
  526. return 0;
  527. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
  528. if (unlikely(cnt < 0))
  529. applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
  530. return cnt;
  531. }
  532. int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[]) {
  533. int bufsize = 12 + ztex->extraSolutions * 4;
  534. unsigned char *rbuf;
  535. int cnt, i, j;
  536. if (ztex->hndl == NULL)
  537. return 0;
  538. rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
  539. if (rbuf == NULL) {
  540. applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
  541. return 0;
  542. }
  543. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, bufsize * ztex->numNonces, 1000);
  544. if (unlikely(cnt < 0)) {
  545. applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
  546. free(rbuf);
  547. return cnt;
  548. }
  549. for (i=0; i<ztex->numNonces; i++) {
  550. memcpy((char*)&nonces[i].goldenNonce[0], &rbuf[i*bufsize], 4);
  551. nonces[i].goldenNonce[0] -= ztex->offsNonces;
  552. //applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
  553. memcpy((char*)&nonces[i].nonce, &rbuf[(i*bufsize)+4], 4);
  554. nonces[i].nonce -= ztex->offsNonces;
  555. memcpy((char*)&nonces[i].hash7, &rbuf[(i*bufsize)+8], 4);
  556. for (j=0; j<ztex->extraSolutions; j++) {
  557. memcpy((char*)&nonces[i].goldenNonce[j+1], &rbuf[(i*bufsize)+12+(j*4)], 4);
  558. nonces[i].goldenNonce[j+1] -= ztex->offsNonces;
  559. //applog(LOG_DEBUG, "W %d:%d %0.8x", i, j+1, nonces[i].goldenNonce[j+1]);
  560. }
  561. }
  562. free(rbuf);
  563. return cnt;
  564. }
  565. void libztex_freeDevList(struct libztex_dev_list **devs)
  566. {
  567. bool done = false;
  568. ssize_t cnt = 0;
  569. while (!done) {
  570. if (devs[cnt]->next == NULL)
  571. done = true;
  572. free(devs[cnt++]);
  573. }
  574. free(devs);
  575. }