libztex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * libztex.c - Ztex 1.15x fpga board support library
  3. *
  4. * Copyright (c) 2012 nelisky.btc@gmail.com
  5. *
  6. * This work is based upon the Java SDK provided by ztex which is
  7. * Copyright (C) 2009-2011 ZTEX GmbH.
  8. * http://www.ztex.de
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see http://www.gnu.org/licenses/.
  21. **/
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include "miner.h"
  25. #include "libztex.h"
  26. #define BUFSIZE 256
  27. //* Capability index for EEPROM support.
  28. #define CAPABILITY_EEPROM 0,0
  29. //* Capability index for FPGA configuration support.
  30. #define CAPABILITY_FPGA 0,1
  31. //* Capability index for FLASH memory support.
  32. #define CAPABILITY_FLASH 0,2
  33. //* Capability index for DEBUG helper support.
  34. #define CAPABILITY_DEBUG 0,3
  35. //* Capability index for AVR XMEGA support.
  36. #define CAPABILITY_XMEGA 0,4
  37. //* Capability index for AVR XMEGA support.
  38. #define CAPABILITY_HS_FPGA 0,5
  39. //* Capability index for AVR XMEGA support.
  40. #define CAPABILITY_MAC_EEPROM 0,6
  41. static bool libztex_checkDevice(struct libusb_device *dev)
  42. {
  43. struct libusb_device_descriptor desc;
  44. int err;
  45. err = libusb_get_device_descriptor(dev, &desc);
  46. if (unlikely(err != 0)) {
  47. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  48. return false;
  49. }
  50. if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
  51. applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
  52. return false;
  53. }
  54. return true;
  55. }
  56. static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
  57. {
  58. if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
  59. (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
  60. applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
  61. return false;
  62. }
  63. return true;
  64. }
  65. static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
  66. {
  67. int i;
  68. for (i = 0; i < size - 4; i++) {
  69. if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
  70. return 1;
  71. if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
  72. return 0;
  73. }
  74. applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
  75. return 0;
  76. }
  77. static void libztex_swapBits(unsigned char *buf, int size)
  78. {
  79. unsigned char c;
  80. int i;
  81. for (i = 0; i < size; i++) {
  82. c = buf[i];
  83. buf[i] = ((c & 128) >> 7) |
  84. ((c & 64) >> 5) |
  85. ((c & 32) >> 3) |
  86. ((c & 16) >> 1) |
  87. ((c & 8) << 1) |
  88. ((c & 4) << 3) |
  89. ((c & 2) << 5) |
  90. ((c & 1) << 7);
  91. }
  92. }
  93. static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
  94. {
  95. unsigned char buf[9];
  96. int cnt;
  97. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  98. return -1;
  99. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
  100. if (unlikely(cnt < 0)) {
  101. applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
  102. return cnt;
  103. }
  104. state->fpgaConfigured = (buf[0] == 0);
  105. state->fpgaChecksum = buf[1] & 0xff;
  106. state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
  107. state->fpgaInitB = buf[6] & 0xff;
  108. state->fpgaFlashResult = buf[7];
  109. state->fpgaFlashBitSwap = (buf[8] != 0);
  110. return 0;
  111. }
  112. static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
  113. {
  114. struct libztex_fpgastate state;
  115. const int transactionBytes = 2048;
  116. unsigned char buf[transactionBytes], cs;
  117. int tries, cnt, buf_p, i;
  118. ssize_t pos = 0;
  119. FILE *fp;
  120. if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
  121. return -1;
  122. libztex_getFpgaState(ztex, &state);
  123. if (!force && state.fpgaConfigured) {
  124. applog(LOG_DEBUG, "Bitstream already configured");
  125. return 1;
  126. }
  127. for (tries = 10; tries > 0; tries--) {
  128. fp = fopen(firmware, "rb");
  129. if (!fp) {
  130. applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
  131. return -2;
  132. }
  133. cs = 0;
  134. while (pos < transactionBytes && !feof(fp)) {
  135. buf[pos] = getc(fp);
  136. cs += buf[pos++];
  137. }
  138. if (feof(fp))
  139. pos--;
  140. if (bs != 0 && bs != 1)
  141. bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
  142. //* Reset fpga
  143. cnt = libztex_resetFpga(ztex);
  144. if (unlikely(cnt < 0)) {
  145. applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
  146. continue;
  147. }
  148. if (bs == 1)
  149. libztex_swapBits(buf, pos);
  150. buf_p = pos;
  151. while (1) {
  152. i = 0;
  153. while (i < buf_p) {
  154. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
  155. if (unlikely(cnt < 0)) {
  156. applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
  157. break;
  158. }
  159. i += cnt;
  160. }
  161. if (i < buf_p || buf_p < transactionBytes)
  162. break;
  163. buf_p = 0;
  164. while (buf_p < transactionBytes && !feof(fp)) {
  165. buf[buf_p] = getc(fp);
  166. cs += buf[buf_p++];
  167. }
  168. if (feof(fp))
  169. buf_p--;
  170. pos += buf_p;
  171. if (buf_p == 0)
  172. break;
  173. if (bs == 1)
  174. libztex_swapBits(buf, buf_p);
  175. }
  176. if (cnt >= 0)
  177. tries = 0;
  178. fclose(fp);
  179. }
  180. libztex_getFpgaState(ztex, &state);
  181. if (!state.fpgaConfigured) {
  182. applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
  183. return 3;
  184. }
  185. usleep(200000);
  186. applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
  187. return 0;
  188. }
  189. int libztex_configureFpga(struct libztex_device *ztex)
  190. {
  191. char buf[256] = "bitstreams/";
  192. memset(&buf[11], 0, 245);
  193. strcpy(&buf[11], ztex->bitFileName);
  194. strcpy(&buf[strlen(buf)], ".bit");
  195. return libztex_configureFpgaLS(ztex, buf, true, 2);
  196. }
  197. int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
  198. {
  199. int cnt;
  200. if (freq > ztex->freqMaxM)
  201. freq = ztex->freqMaxM;
  202. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
  203. if (unlikely(cnt < 0)) {
  204. applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
  205. return cnt;
  206. }
  207. ztex->freqM = freq;
  208. applog(LOG_WARNING, "%s: Frequency change to %0.2f Mhz", ztex->repr, ztex->freqM1 * (ztex->freqM + 1));
  209. return 0;
  210. }
  211. int libztex_resetFpga(struct libztex_device *ztex)
  212. {
  213. return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
  214. }
  215. int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
  216. {
  217. struct libztex_device *newdev;
  218. unsigned char buf[64];
  219. int cnt, err;
  220. newdev = malloc(sizeof(struct libztex_device));
  221. newdev->bitFileName = NULL;
  222. newdev->valid = false;
  223. newdev->hndl = NULL;
  224. *ztex = newdev;
  225. err = libusb_get_device_descriptor(dev, &newdev->descriptor);
  226. if (unlikely(err != 0)) {
  227. applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
  228. return err;
  229. }
  230. // Check vendorId and productId
  231. if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
  232. newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
  233. applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
  234. return 1;
  235. }
  236. err = libusb_open(dev, &newdev->hndl);
  237. if (unlikely(err != 0)) {
  238. applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
  239. return err;
  240. }
  241. cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
  242. LIBZTEX_SNSTRING_LEN + 1);
  243. if (unlikely(cnt < 0)) {
  244. applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
  245. return cnt;
  246. }
  247. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
  248. if (unlikely(cnt < 0)) {
  249. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  250. return cnt;
  251. }
  252. if ( buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X' ) {
  253. applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
  254. return 2;
  255. }
  256. newdev->productId[0] = buf[6];
  257. newdev->productId[1] = buf[7];
  258. newdev->productId[2] = buf[8];
  259. newdev->productId[3] = buf[9];
  260. newdev->fwVersion = buf[10];
  261. newdev->interfaceVersion = buf[11];
  262. newdev->interfaceCapabilities[0] = buf[12];
  263. newdev->interfaceCapabilities[1] = buf[13];
  264. newdev->interfaceCapabilities[2] = buf[14];
  265. newdev->interfaceCapabilities[3] = buf[15];
  266. newdev->interfaceCapabilities[4] = buf[16];
  267. newdev->interfaceCapabilities[5] = buf[17];
  268. newdev->moduleReserved[0] = buf[18];
  269. newdev->moduleReserved[1] = buf[19];
  270. newdev->moduleReserved[2] = buf[20];
  271. newdev->moduleReserved[3] = buf[21];
  272. newdev->moduleReserved[4] = buf[22];
  273. newdev->moduleReserved[5] = buf[23];
  274. newdev->moduleReserved[6] = buf[24];
  275. newdev->moduleReserved[7] = buf[25];
  276. newdev->moduleReserved[8] = buf[26];
  277. newdev->moduleReserved[9] = buf[27];
  278. newdev->moduleReserved[10] = buf[28];
  279. newdev->moduleReserved[11] = buf[29];
  280. cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
  281. if (unlikely(cnt < 0)) {
  282. applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
  283. return cnt;
  284. }
  285. if (unlikely(buf[0] != 4)) {
  286. if (unlikely(buf[0] != 2)) {
  287. applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
  288. return 3;
  289. }
  290. applog(LOG_WARNING, "Firmware out of date");
  291. }
  292. newdev->numNonces = buf[1] + 1;
  293. newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
  294. newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
  295. newdev->freqMaxM = (buf[7] & 255);
  296. newdev->freqM = (buf[6] & 255);
  297. newdev->freqMDefault = newdev->freqM;
  298. for (cnt=0; cnt < 255; cnt++) {
  299. newdev->errorCount[cnt] = 0;
  300. newdev->errorWeight[cnt] = 0;
  301. newdev->errorRate[cnt] = 0;
  302. newdev->maxErrorRate[cnt] = 0;
  303. }
  304. cnt = strlen((char *)&buf[buf[0] == 4? 10: 8]);
  305. newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
  306. memcpy(newdev->bitFileName, &buf[buf[0] == 4? 10: 8], cnt + 1);
  307. newdev->usbbus = libusb_get_bus_number(dev);
  308. newdev->usbaddress = libusb_get_device_address(dev);
  309. sprintf(newdev->repr, "ZTEX %.3d:%.3d-%s", newdev->usbbus, newdev->usbaddress, newdev->snString);
  310. newdev->valid = true;
  311. return 0;
  312. }
  313. void libztex_destroy_device(struct libztex_device* ztex)
  314. {
  315. if (ztex->hndl != NULL) {
  316. libusb_close(ztex->hndl);
  317. ztex->hndl = NULL;
  318. }
  319. if (ztex->bitFileName != NULL) {
  320. free(ztex->bitFileName);
  321. ztex->bitFileName = NULL;
  322. }
  323. free(ztex);
  324. }
  325. int libztex_scanDevices(struct libztex_dev_list*** devs_p)
  326. {
  327. int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
  328. struct libztex_dev_list **devs;
  329. struct libztex_device *ztex;
  330. int found = 0, pos = 0, err;
  331. libusb_device **list;
  332. ssize_t cnt, i = 0;
  333. cnt = libusb_get_device_list(NULL, &list);
  334. if (unlikely(cnt < 0)) {
  335. applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
  336. return 0;
  337. }
  338. for (i = 0; i < cnt; i++) {
  339. if (libztex_checkDevice(list[i])) {
  340. // Got one!
  341. usbdevices[found] = i;
  342. found++;
  343. }
  344. }
  345. devs = malloc(sizeof(struct libztex_dev_list *) * found);
  346. if (devs == NULL) {
  347. applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
  348. return 0;
  349. }
  350. for (i = 0; i < found; i++) {
  351. err = libztex_prepare_device(list[usbdevices[i]], &ztex);
  352. if (unlikely(err != 0))
  353. applog(LOG_ERR, "prepare device: %d", err);
  354. // check if valid
  355. if (!ztex->valid) {
  356. libztex_destroy_device(ztex);
  357. continue;
  358. }
  359. devs[pos] = malloc(sizeof(struct libztex_dev_list));
  360. devs[pos]->dev = ztex;
  361. devs[pos]->next = NULL;
  362. if (pos > 0)
  363. devs[pos - 1]->next = devs[pos];
  364. pos++;
  365. }
  366. libusb_free_device_list(list, 1);
  367. *devs_p = devs;
  368. return pos;
  369. }
  370. int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
  371. {
  372. int cnt;
  373. if (ztex == NULL || ztex->hndl == NULL)
  374. return 0;
  375. cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
  376. if (unlikely(cnt < 0))
  377. applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
  378. return cnt;
  379. }
  380. int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
  381. {
  382. // length of buf must be 8 * (numNonces + 1)
  383. unsigned char rbuf[12 * 8];
  384. int cnt, i;
  385. if (ztex->hndl == NULL)
  386. return 0;
  387. cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12 * ztex->numNonces, 1000);
  388. if (unlikely(cnt < 0)) {
  389. applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
  390. return cnt;
  391. }
  392. for (i = 0; i < ztex->numNonces; i++) {
  393. memcpy((char*)&nonces[i].goldenNonce, &rbuf[i * 12], 4);
  394. nonces[i].goldenNonce -= ztex->offsNonces;
  395. memcpy((char*)&nonces[i].nonce, &rbuf[(i * 12) + 4], 4);
  396. nonces[i].nonce -= ztex->offsNonces;
  397. memcpy((char*)&nonces[i].hash7, &rbuf[(i * 12) + 8], 4);
  398. }
  399. return cnt;
  400. }
  401. void libztex_freeDevList(struct libztex_dev_list **devs)
  402. {
  403. bool done = false;
  404. ssize_t cnt = 0;
  405. while (!done) {
  406. if (devs[cnt]->next == NULL)
  407. done = true;
  408. free(devs[cnt++]);
  409. }
  410. free(devs);
  411. }