bitforce.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include <limits.h>
  10. #include <pthread.h>
  11. #include <stdio.h>
  12. #include <sys/time.h>
  13. #include <sys/types.h>
  14. #include <dirent.h>
  15. #ifndef WIN32
  16. #include <termios.h>
  17. #else
  18. #define NAME_MAX 255
  19. #endif
  20. #include <unistd.h>
  21. #include "elist.h"
  22. #include "miner.h"
  23. struct device_api bitforce_api;
  24. static bool bitforce_detect_one(const char *devpath)
  25. {
  26. char pdevbuf[0x100];
  27. int i = 0;
  28. if (total_devices == MAX_DEVICES)
  29. return false;
  30. FILE *fileDev = fopen(devpath, "r+b");
  31. if (unlikely(!fileDev))
  32. {
  33. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  34. return false;
  35. }
  36. setbuf(fileDev, NULL);
  37. fprintf(fileDev, "ZGX");
  38. if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
  39. {
  40. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  41. return 0;
  42. }
  43. fclose(fileDev);
  44. if (unlikely(!strstr(pdevbuf, "SHA256")))
  45. {
  46. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  47. return false;
  48. }
  49. // We have a real BitForce!
  50. struct cgpu_info *bitforce;
  51. bitforce = calloc(1, sizeof(*bitforce));
  52. devices[total_devices++] = bitforce;
  53. bitforce->api = &bitforce_api;
  54. bitforce->device_id = i++;
  55. bitforce->device_path = strdup(devpath);
  56. bitforce->enabled = true;
  57. bitforce->threads = 1;
  58. return true;
  59. }
  60. static void bitforce_detect_auto()
  61. {
  62. DIR *D;
  63. struct dirent *de;
  64. const char udevdir[] = "/dev/serial/by-id";
  65. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  66. char *devfile = devpath + sizeof(udevdir);
  67. D = opendir(udevdir);
  68. if (!D)
  69. return;
  70. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  71. devpath[sizeof(udevdir) - 1] = '/';
  72. while ( (de = readdir(D)) ) {
  73. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  74. continue;
  75. strcpy(devfile, de->d_name);
  76. bitforce_detect_one(devpath);
  77. }
  78. closedir(D);
  79. }
  80. static void bitforce_detect()
  81. {
  82. struct string_elist *iter, *tmp;
  83. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  84. if (bitforce_detect_one(iter->string))
  85. string_elist_del(iter);
  86. }
  87. bitforce_detect_auto();
  88. }
  89. static bool bitforce_thread_prepare(struct thr_info *thr)
  90. {
  91. struct cgpu_info *bitforce = thr->cgpu;
  92. struct timeval now;
  93. FILE *fileDev = fopen(bitforce->device_path, "r+b");
  94. if (unlikely(!fileDev))
  95. {
  96. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  97. return false;
  98. }
  99. #ifndef WIN32
  100. {
  101. int nDevFD = fileno(fileDev);
  102. struct termios pattr;
  103. tcgetattr(nDevFD, &pattr);
  104. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  105. pattr.c_oflag &= ~OPOST;
  106. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  107. pattr.c_cflag &= ~(CSIZE | PARENB);
  108. pattr.c_cflag |= CS8;
  109. tcsetattr(nDevFD, TCSANOW, &pattr);
  110. }
  111. #endif
  112. setbuf(fileDev, NULL);
  113. bitforce->device_file = fileDev;
  114. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  115. gettimeofday(&now, NULL);
  116. get_datestamp(bitforce->init, &now);
  117. return true;
  118. }
  119. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t max_nonce)
  120. {
  121. struct cgpu_info *bitforce = thr->cgpu;
  122. FILE *fileDev = bitforce->device_file;
  123. char pdevbuf[0x100];
  124. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  125. int i;
  126. char *pnoncebuf;
  127. uint32_t nonce;
  128. fprintf(fileDev, "ZDX");
  129. if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev)) {
  130. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  131. return 0;
  132. }
  133. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  134. {
  135. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  136. return 0;
  137. }
  138. memcpy(ob + 8, work->midstate, 32);
  139. memcpy(ob + 8 + 32, work->data + 64, 12);
  140. fwrite(ob, 60, 1, fileDev);
  141. applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
  142. if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
  143. {
  144. applog(LOG_ERR, "Error reading from BitForce (block data)");
  145. return 0;
  146. }
  147. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  148. {
  149. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  150. return 0;
  151. }
  152. usleep(4500000);
  153. i = 4500;
  154. while (1) {
  155. fprintf(fileDev, "ZFX");
  156. if (!fgets(pdevbuf, sizeof(pdevbuf), fileDev))
  157. {
  158. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  159. return 0;
  160. }
  161. if (pdevbuf[0] != 'B')
  162. break;
  163. usleep(10000);
  164. i += 10;
  165. }
  166. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  167. work->blk.nonce = 0xffffffff;
  168. if (pdevbuf[2] == '-')
  169. return 0xffffffff;
  170. else
  171. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  172. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  173. return 0;
  174. }
  175. pnoncebuf = &pdevbuf[12];
  176. while (1) {
  177. hex2bin((void*)&nonce, pnoncebuf, 4);
  178. #ifndef __BIG_ENDIAN__
  179. nonce = swab32(nonce);
  180. #endif
  181. submit_nonce(thr, work, nonce);
  182. if (pnoncebuf[8] != ',')
  183. break;
  184. pnoncebuf += 9;
  185. }
  186. return 0xffffffff;
  187. }
  188. struct device_api bitforce_api = {
  189. .name = "BFL",
  190. .api_detect = bitforce_detect,
  191. // .reinit_device = TODO
  192. .thread_prepare = bitforce_thread_prepare,
  193. .scanhash = bitforce_scanhash,
  194. };