bitforce.c 5.9 KB

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