libztex.c 24 KB

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