bitforce.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <sys/time.h>
  13. #include <sys/types.h>
  14. #include <dirent.h>
  15. #ifndef WIN32
  16. #include <termios.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #ifndef O_CLOEXEC
  20. #define O_CLOEXEC 0
  21. #endif
  22. #else
  23. #include <windows.h>
  24. #include <io.h>
  25. #endif
  26. #include <unistd.h>
  27. #include "elist.h"
  28. #include "miner.h"
  29. struct device_api bitforce_api;
  30. #ifdef WIN32
  31. static int BFopen(const char *devpath)
  32. {
  33. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  34. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  35. return -1;
  36. return _open_osfhandle((LONG)hSerial, 0);
  37. }
  38. #else
  39. static int BFopen(const char *devpath)
  40. {
  41. return open(devpath, O_CLOEXEC | O_NOCTTY);
  42. }
  43. #endif
  44. static void BFgets(char *buf, size_t bufLen, int fd)
  45. {
  46. do
  47. --bufLen;
  48. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  49. ;
  50. buf[0] = '\0';
  51. }
  52. #define BFwrite(fd, buf, bufLen) write(fd, buf, bufLen)
  53. #define BFclose(fd) close(fd)
  54. static bool bitforce_detect_one(const char *devpath)
  55. {
  56. char pdevbuf[0x100];
  57. int i = 0;
  58. if (total_devices == MAX_DEVICES)
  59. return false;
  60. int fdDev = BFopen(devpath);
  61. if (unlikely(fdDev == -1))
  62. {
  63. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  64. return false;
  65. }
  66. BFwrite(fdDev, "ZGX", 3);
  67. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  68. if (unlikely(!pdevbuf[0]))
  69. {
  70. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  71. return 0;
  72. }
  73. BFclose(fdDev);
  74. if (unlikely(!strstr(pdevbuf, "SHA256")))
  75. {
  76. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  77. return false;
  78. }
  79. // We have a real BitForce!
  80. struct cgpu_info *bitforce;
  81. bitforce = calloc(1, sizeof(*bitforce));
  82. devices[total_devices++] = bitforce;
  83. bitforce->api = &bitforce_api;
  84. bitforce->device_id = i++;
  85. bitforce->device_path = strdup(devpath);
  86. bitforce->enabled = true;
  87. bitforce->threads = 1;
  88. return true;
  89. }
  90. static void bitforce_detect_auto()
  91. {
  92. #ifndef WIN32
  93. DIR *D;
  94. struct dirent *de;
  95. const char udevdir[] = "/dev/serial/by-id";
  96. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  97. char *devfile = devpath + sizeof(udevdir);
  98. D = opendir(udevdir);
  99. if (!D)
  100. return;
  101. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  102. devpath[sizeof(udevdir) - 1] = '/';
  103. while ( (de = readdir(D)) ) {
  104. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  105. continue;
  106. strcpy(devfile, de->d_name);
  107. bitforce_detect_one(devpath);
  108. }
  109. closedir(D);
  110. #endif
  111. }
  112. static void bitforce_detect()
  113. {
  114. struct string_elist *iter, *tmp;
  115. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  116. if (bitforce_detect_one(iter->string))
  117. string_elist_del(iter);
  118. }
  119. bitforce_detect_auto();
  120. }
  121. static bool bitforce_thread_prepare(struct thr_info *thr)
  122. {
  123. struct cgpu_info *bitforce = thr->cgpu;
  124. struct timeval now;
  125. int fdDev = BFopen(bitforce->device_path);
  126. if (unlikely(-1 == fdDev))
  127. {
  128. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  129. return false;
  130. }
  131. #ifndef WIN32
  132. {
  133. struct termios pattr;
  134. tcgetattr(fdDev, &pattr);
  135. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  136. pattr.c_oflag &= ~OPOST;
  137. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  138. pattr.c_cflag &= ~(CSIZE | PARENB);
  139. pattr.c_cflag |= CS8;
  140. tcsetattr(fdDev, TCSANOW, &pattr);
  141. }
  142. #endif
  143. bitforce->device_fd = fdDev;
  144. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  145. gettimeofday(&now, NULL);
  146. get_datestamp(bitforce->init, &now);
  147. return true;
  148. }
  149. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t max_nonce)
  150. {
  151. struct cgpu_info *bitforce = thr->cgpu;
  152. int fdDev = bitforce->device_fd;
  153. char pdevbuf[0x100];
  154. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  155. int i;
  156. char *pnoncebuf;
  157. uint32_t nonce;
  158. BFwrite(fdDev, "ZDX", 3);
  159. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  160. if (unlikely(!pdevbuf[0])) {
  161. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  162. return 0;
  163. }
  164. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  165. {
  166. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  167. return 0;
  168. }
  169. memcpy(ob + 8, work->midstate, 32);
  170. memcpy(ob + 8 + 32, work->data + 64, 12);
  171. BFwrite(fdDev, ob, 60);
  172. applog(LOG_DEBUG, "BitForce block data: %s", bin2hex(ob + 8, 44));
  173. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  174. if (unlikely(!pdevbuf[0]))
  175. {
  176. applog(LOG_ERR, "Error reading from BitForce (block data)");
  177. return 0;
  178. }
  179. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K'))
  180. {
  181. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  182. return 0;
  183. }
  184. usleep(4500000);
  185. i = 4500;
  186. while (1) {
  187. BFwrite(fdDev, "ZFX", 3);
  188. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  189. if (unlikely(!pdevbuf[0]))
  190. {
  191. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  192. return 0;
  193. }
  194. if (pdevbuf[0] != 'B')
  195. break;
  196. usleep(10000);
  197. i += 10;
  198. }
  199. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  200. work->blk.nonce = 0xffffffff;
  201. if (pdevbuf[2] == '-')
  202. return 0xffffffff;
  203. else
  204. if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  205. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  206. return 0;
  207. }
  208. pnoncebuf = &pdevbuf[12];
  209. while (1) {
  210. hex2bin((void*)&nonce, pnoncebuf, 4);
  211. #ifndef __BIG_ENDIAN__
  212. nonce = swab32(nonce);
  213. #endif
  214. submit_nonce(thr, work, nonce);
  215. if (pnoncebuf[8] != ',')
  216. break;
  217. pnoncebuf += 9;
  218. }
  219. return 0xffffffff;
  220. }
  221. struct device_api bitforce_api = {
  222. .name = "BFL",
  223. .api_detect = bitforce_detect,
  224. // .reinit_device = TODO
  225. .thread_prepare = bitforce_thread_prepare,
  226. .scanhash = bitforce_scanhash,
  227. };