libztex.c 25 KB

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