bitforce.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <limits.h>
  11. #include <pthread.h>
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #ifndef WIN32
  18. #include <termios.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #ifndef O_CLOEXEC
  22. #define O_CLOEXEC 0
  23. #endif
  24. #else
  25. #include <windows.h>
  26. #include <io.h>
  27. #endif
  28. #include <unistd.h>
  29. #include "elist.h"
  30. #include "miner.h"
  31. struct device_api bitforce_api;
  32. static int BFopen(const char *devpath)
  33. {
  34. #ifdef WIN32
  35. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  36. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  37. return -1;
  38. COMMTIMEOUTS cto = {30000, 0, 30000, 0, 30000};
  39. SetCommTimeouts(hSerial, &cto);
  40. return _open_osfhandle((LONG)hSerial, 0);
  41. #else
  42. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  43. if (likely(fdDev != -1)) {
  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. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  82. return false;
  83. }
  84. BFwrite(fdDev, "ZGX", 3);
  85. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  86. if (unlikely(!pdevbuf[0])) {
  87. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  88. return 0;
  89. }
  90. BFclose(fdDev);
  91. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  92. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  93. return false;
  94. }
  95. // We have a real BitForce!
  96. struct cgpu_info *bitforce;
  97. bitforce = calloc(1, sizeof(*bitforce));
  98. devices[total_devices++] = bitforce;
  99. bitforce->api = &bitforce_api;
  100. bitforce->device_id = i++;
  101. bitforce->device_path = strdup(devpath);
  102. bitforce->deven = DEV_ENABLED;
  103. bitforce->threads = 1;
  104. return true;
  105. }
  106. static void bitforce_detect_auto()
  107. {
  108. #ifndef WIN32
  109. DIR *D;
  110. struct dirent *de;
  111. const char udevdir[] = "/dev/serial/by-id";
  112. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  113. char *devfile = devpath + sizeof(udevdir);
  114. D = opendir(udevdir);
  115. if (!D)
  116. return;
  117. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  118. devpath[sizeof(udevdir) - 1] = '/';
  119. while ( (de = readdir(D)) ) {
  120. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  121. continue;
  122. strcpy(devfile, de->d_name);
  123. bitforce_detect_one(devpath);
  124. }
  125. closedir(D);
  126. #endif
  127. }
  128. static void bitforce_detect()
  129. {
  130. struct string_elist *iter, *tmp;
  131. bool found = false;
  132. bool autoscan = false;
  133. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  134. if (!strcmp(iter->string, "auto"))
  135. autoscan = true;
  136. else if (bitforce_detect_one(iter->string)) {
  137. string_elist_del(iter);
  138. found = true;
  139. }
  140. }
  141. if (autoscan || !found)
  142. bitforce_detect_auto();
  143. }
  144. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  145. {
  146. float gt = bitforce->temp;
  147. if (gt > 0)
  148. tailsprintf(buf, "%5.1fC ", gt);
  149. else
  150. tailsprintf(buf, " ", gt);
  151. tailsprintf(buf, " | ");
  152. }
  153. static bool bitforce_thread_prepare(struct thr_info *thr)
  154. {
  155. struct cgpu_info *bitforce = thr->cgpu;
  156. struct timeval now;
  157. int fdDev = BFopen(bitforce->device_path);
  158. if (unlikely(-1 == fdDev)) {
  159. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  160. return false;
  161. }
  162. bitforce->device_fd = fdDev;
  163. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  164. gettimeofday(&now, NULL);
  165. get_datestamp(bitforce->init, &now);
  166. return true;
  167. }
  168. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  169. {
  170. struct cgpu_info *bitforce = thr->cgpu;
  171. int fdDev = bitforce->device_fd;
  172. char pdevbuf[0x100];
  173. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  174. int i;
  175. char *pnoncebuf;
  176. uint32_t nonce;
  177. BFwrite(fdDev, "ZDX", 3);
  178. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  179. if (unlikely(!pdevbuf[0])) {
  180. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  181. return 0;
  182. }
  183. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  184. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  185. return 0;
  186. }
  187. memcpy(ob + 8, work->midstate, 32);
  188. memcpy(ob + 8 + 32, work->data + 64, 12);
  189. BFwrite(fdDev, ob, 60);
  190. applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
  191. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  192. if (unlikely(!pdevbuf[0])) {
  193. applog(LOG_ERR, "Error reading from BitForce (block data)");
  194. return 0;
  195. }
  196. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  197. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  198. return 0;
  199. }
  200. BFwrite(fdDev, "ZKX", 3);
  201. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  202. if (unlikely(!pdevbuf[0])) {
  203. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  204. return 0;
  205. }
  206. if (!strncasecmp(pdevbuf, "TEMP:", 5)) {
  207. float temp = strtof(pdevbuf + 5, NULL);
  208. if (temp > 0) {
  209. bitforce->temp = temp;
  210. if (temp > bitforce->cutofftemp) {
  211. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  212. bitforce->deven = DEV_RECOVER;
  213. }
  214. }
  215. }
  216. usleep(4500000);
  217. i = 4500;
  218. while (1) {
  219. BFwrite(fdDev, "ZFX", 3);
  220. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  221. if (unlikely(!pdevbuf[0])) {
  222. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  223. return 0;
  224. }
  225. if (pdevbuf[0] != 'B')
  226. break;
  227. usleep(10000);
  228. i += 10;
  229. }
  230. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  231. work->blk.nonce = 0xffffffff;
  232. if (pdevbuf[2] == '-')
  233. return 0xffffffff;
  234. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  235. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  236. return 0;
  237. }
  238. pnoncebuf = &pdevbuf[12];
  239. while (1) {
  240. hex2bin((void*)&nonce, pnoncebuf, 4);
  241. #ifndef __BIG_ENDIAN__
  242. nonce = swab32(nonce);
  243. #endif
  244. submit_nonce(thr, work, nonce);
  245. if (pnoncebuf[8] != ',')
  246. break;
  247. pnoncebuf += 9;
  248. }
  249. return 0xffffffff;
  250. }
  251. struct device_api bitforce_api = {
  252. .name = "BFL",
  253. .api_detect = bitforce_detect,
  254. .get_statline_before = get_bitforce_statline_before,
  255. .thread_prepare = bitforce_thread_prepare,
  256. .scanhash = bitforce_scanhash,
  257. };