libztex.c 25 KB

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