bitforce.c 7.0 KB

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