bitforce.c 5.7 KB

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