bitforce.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <strings.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #ifndef WIN32
  17. #include <termios.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #ifndef O_CLOEXEC
  21. #define O_CLOEXEC 0
  22. #endif
  23. #else
  24. #include <windows.h>
  25. #include <io.h>
  26. #endif
  27. #include <unistd.h>
  28. #include "elist.h"
  29. #include "miner.h"
  30. struct device_api bitforce_api;
  31. static int BFopen(const char *devpath)
  32. {
  33. #ifdef WIN32
  34. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  35. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  36. return -1;
  37. COMMTIMEOUTS cto = {30000, 0, 30000, 0, 30000};
  38. SetCommTimeouts(hSerial, &cto);
  39. return _open_osfhandle((LONG)hSerial, 0);
  40. #else
  41. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  42. if (likely(fdDev != -1))
  43. {
  44. struct termios pattr;
  45. tcgetattr(fdDev, &pattr);
  46. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  47. pattr.c_oflag &= ~OPOST;
  48. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  49. pattr.c_cflag &= ~(CSIZE | PARENB);
  50. pattr.c_cflag |= CS8;
  51. tcsetattr(fdDev, TCSANOW, &pattr);
  52. }
  53. tcflush(fdDev, TCOFLUSH);
  54. tcflush(fdDev, TCIFLUSH);
  55. return fdDev;
  56. #endif
  57. }
  58. static void BFgets(char *buf, size_t bufLen, int fd)
  59. {
  60. do
  61. --bufLen;
  62. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  63. ;
  64. buf[0] = '\0';
  65. }
  66. static void BFwrite(int fd, const void *buf, ssize_t bufLen)
  67. {
  68. ssize_t ret = write(fd, buf, bufLen);
  69. if (unlikely(ret != bufLen))
  70. quit(1, "BFwrite failed");
  71. }
  72. #define BFclose(fd) close(fd)
  73. static bool bitforce_detect_one(const char *devpath)
  74. {
  75. char pdevbuf[0x100];
  76. static int i = 0;
  77. if (total_devices == MAX_DEVICES)
  78. return false;
  79. int fdDev = BFopen(devpath);
  80. if (unlikely(fdDev == -1))
  81. {
  82. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  83. return false;
  84. }
  85. BFwrite(fdDev, "ZGX", 3);
  86. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  87. if (unlikely(!pdevbuf[0]))
  88. {
  89. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  90. return 0;
  91. }
  92. BFclose(fdDev);
  93. if (unlikely(!strstr(pdevbuf, "SHA256")))
  94. {
  95. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  96. return false;
  97. }
  98. // We have a real BitForce!
  99. struct cgpu_info *bitforce;
  100. bitforce = calloc(1, sizeof(*bitforce));
  101. devices[total_devices++] = bitforce;
  102. bitforce->api = &bitforce_api;
  103. bitforce->device_id = i++;
  104. bitforce->device_path = strdup(devpath);
  105. bitforce->enabled = true;
  106. bitforce->threads = 1;
  107. return true;
  108. }
  109. static void bitforce_detect_auto()
  110. {
  111. #ifndef WIN32
  112. DIR *D;
  113. struct dirent *de;
  114. const char udevdir[] = "/dev/serial/by-id";
  115. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  116. char *devfile = devpath + sizeof(udevdir);
  117. D = opendir(udevdir);
  118. if (!D)
  119. return;
  120. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  121. devpath[sizeof(udevdir) - 1] = '/';
  122. while ( (de = readdir(D)) ) {
  123. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  124. continue;
  125. strcpy(devfile, de->d_name);
  126. bitforce_detect_one(devpath);
  127. }
  128. closedir(D);
  129. #endif
  130. }
  131. static void bitforce_detect()
  132. {
  133. struct string_elist *iter, *tmp;
  134. bool found = false;
  135. bool autoscan = false;
  136. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  137. if (!strcmp(iter->string, "auto"))
  138. autoscan = true;
  139. else
  140. if (bitforce_detect_one(iter->string))
  141. {
  142. string_elist_del(iter);
  143. found = true;
  144. }
  145. }
  146. if (autoscan || !found)
  147. bitforce_detect_auto();
  148. }
  149. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  150. {
  151. float gt = bitforce->temp;
  152. if (gt > 0)
  153. tailsprintf(buf, "%5.1fC ", gt);
  154. else
  155. tailsprintf(buf, " ", gt);
  156. tailsprintf(buf, " | ");
  157. }
  158. static bool bitforce_thread_prepare(struct thr_info *thr)
  159. {
  160. struct cgpu_info *bitforce = thr->cgpu;
  161. struct timeval now;
  162. int fdDev = BFopen(bitforce->device_path);
  163. if (unlikely(-1 == fdDev))
  164. {
  165. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  166. return false;
  167. }
  168. bitforce->device_fd = fdDev;
  169. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  170. gettimeofday(&now, NULL);
  171. get_datestamp(bitforce->init, &now);
  172. return true;
  173. }
  174. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  175. {
  176. struct cgpu_info *bitforce = thr->cgpu;
  177. int fdDev = bitforce->device_fd;
  178. char pdevbuf[0x100];
  179. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  180. int i;
  181. char *pnoncebuf;
  182. uint32_t nonce;
  183. BFwrite(fdDev, "ZDX", 3);
  184. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  185. if (unlikely(!pdevbuf[0])) {
  186. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  187. return 0;
  188. }
  189. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  190. {
  191. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  192. return 0;
  193. }
  194. memcpy(ob + 8, work->midstate, 32);
  195. memcpy(ob + 8 + 32, work->data + 64, 12);
  196. BFwrite(fdDev, ob, 60);
  197. applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
  198. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  199. if (unlikely(!pdevbuf[0]))
  200. {
  201. applog(LOG_ERR, "Error reading from BitForce (block data)");
  202. return 0;
  203. }
  204. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  205. {
  206. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  207. return 0;
  208. }
  209. BFwrite(fdDev, "ZKX", 3);
  210. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  211. if (unlikely(!pdevbuf[0])) {
  212. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  213. return 0;
  214. }
  215. if (!strncasecmp(pdevbuf, "TEMP:", 5)) {
  216. float temp = strtof(pdevbuf + 5, NULL);
  217. if (temp > 0) {
  218. bitforce->temp = temp;
  219. if (temp > bitforce->cutofftemp) {
  220. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  221. bitforce->enabled = false;
  222. }
  223. }
  224. }
  225. usleep(4500000);
  226. i = 4500;
  227. while (1) {
  228. BFwrite(fdDev, "ZFX", 3);
  229. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  230. if (unlikely(!pdevbuf[0]))
  231. {
  232. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  233. return 0;
  234. }
  235. if (pdevbuf[0] != 'B')
  236. break;
  237. usleep(10000);
  238. i += 10;
  239. }
  240. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  241. work->blk.nonce = 0xffffffff;
  242. if (pdevbuf[2] == '-')
  243. return 0xffffffff;
  244. else
  245. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  246. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  247. return 0;
  248. }
  249. pnoncebuf = &pdevbuf[12];
  250. while (1) {
  251. hex2bin((void*)&nonce, pnoncebuf, 4);
  252. #ifndef __BIG_ENDIAN__
  253. nonce = swab32(nonce);
  254. #endif
  255. submit_nonce(thr, work, nonce);
  256. if (pnoncebuf[8] != ',')
  257. break;
  258. pnoncebuf += 9;
  259. }
  260. return 0xffffffff;
  261. }
  262. struct device_api bitforce_api = {
  263. .name = "BFL",
  264. .api_detect = bitforce_detect,
  265. .get_statline_before = get_bitforce_statline_before,
  266. .thread_prepare = bitforce_thread_prepare,
  267. .scanhash = bitforce_scanhash,
  268. };