bitforce.c 6.7 KB

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