bitforce.c 6.9 KB

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