libztex.c 20 KB

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