libztex.c 20 KB

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