libztex.c 13 KB

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