libztex.c 24 KB

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