libztex.c 20 KB

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