bitforce.c 5.8 KB

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