bitforce.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifdef WIN32
  32. static int BFopen(const char *devpath)
  33. {
  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. }
  41. #else
  42. static int BFopen(const char *devpath)
  43. {
  44. return open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  45. }
  46. #endif
  47. static void BFgets(char *buf, size_t bufLen, int fd)
  48. {
  49. do
  50. --bufLen;
  51. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  52. ;
  53. buf[0] = '\0';
  54. }
  55. static void BFwrite(int fd, const void *buf, ssize_t bufLen)
  56. {
  57. ssize_t ret = write(fd, buf, bufLen);
  58. if (unlikely(ret != bufLen))
  59. quit(1, "BFwrite failed");
  60. }
  61. #define BFclose(fd) close(fd)
  62. static bool bitforce_detect_one(const char *devpath)
  63. {
  64. char pdevbuf[0x100];
  65. static int i = 0;
  66. if (total_devices == MAX_DEVICES)
  67. return false;
  68. int fdDev = BFopen(devpath);
  69. if (unlikely(fdDev == -1))
  70. {
  71. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  72. return false;
  73. }
  74. BFwrite(fdDev, "ZGX", 3);
  75. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  76. if (unlikely(!pdevbuf[0]))
  77. {
  78. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  79. return 0;
  80. }
  81. BFclose(fdDev);
  82. if (unlikely(!strstr(pdevbuf, "SHA256")))
  83. {
  84. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  85. return false;
  86. }
  87. // We have a real BitForce!
  88. struct cgpu_info *bitforce;
  89. bitforce = calloc(1, sizeof(*bitforce));
  90. devices[total_devices++] = bitforce;
  91. bitforce->api = &bitforce_api;
  92. bitforce->device_id = i++;
  93. bitforce->device_path = strdup(devpath);
  94. bitforce->enabled = true;
  95. bitforce->threads = 1;
  96. return true;
  97. }
  98. static void bitforce_detect_auto()
  99. {
  100. #ifndef WIN32
  101. DIR *D;
  102. struct dirent *de;
  103. const char udevdir[] = "/dev/serial/by-id";
  104. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  105. char *devfile = devpath + sizeof(udevdir);
  106. D = opendir(udevdir);
  107. if (!D)
  108. return;
  109. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  110. devpath[sizeof(udevdir) - 1] = '/';
  111. while ( (de = readdir(D)) ) {
  112. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  113. continue;
  114. strcpy(devfile, de->d_name);
  115. bitforce_detect_one(devpath);
  116. }
  117. closedir(D);
  118. #endif
  119. }
  120. static void bitforce_detect()
  121. {
  122. struct string_elist *iter, *tmp;
  123. bool found = false;
  124. bool autoscan = false;
  125. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  126. if (!strcmp(iter->string, "auto"))
  127. autoscan = true;
  128. else
  129. if (bitforce_detect_one(iter->string))
  130. {
  131. string_elist_del(iter);
  132. found = true;
  133. }
  134. }
  135. if (autoscan || !found)
  136. bitforce_detect_auto();
  137. }
  138. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  139. {
  140. float gt = bitforce->temp;
  141. if (gt > 0)
  142. tailsprintf(buf, "%5.1fC ", gt);
  143. else
  144. tailsprintf(buf, " ", gt);
  145. tailsprintf(buf, " | ");
  146. }
  147. static bool bitforce_thread_prepare(struct thr_info *thr)
  148. {
  149. struct cgpu_info *bitforce = thr->cgpu;
  150. struct timeval now;
  151. int fdDev = BFopen(bitforce->device_path);
  152. if (unlikely(-1 == fdDev))
  153. {
  154. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  155. return false;
  156. }
  157. #ifndef WIN32
  158. {
  159. struct termios pattr;
  160. tcgetattr(fdDev, &pattr);
  161. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  162. pattr.c_oflag &= ~OPOST;
  163. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  164. pattr.c_cflag &= ~(CSIZE | PARENB);
  165. pattr.c_cflag |= CS8;
  166. tcsetattr(fdDev, TCSANOW, &pattr);
  167. }
  168. #endif
  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->enabled = false;
  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. // .reinit_device = TODO
  267. .get_statline_before = get_bitforce_statline_before,
  268. .thread_prepare = bitforce_thread_prepare,
  269. .scanhash = bitforce_scanhash,
  270. };