libztex.c 24 KB

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