libztex.c 12 KB

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