driver-bitforce.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <limits.h>
  11. #include <pthread.h>
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #ifndef WIN32
  18. #include <termios.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #ifndef O_CLOEXEC
  22. #define O_CLOEXEC 0
  23. #endif
  24. #else
  25. #include <windows.h>
  26. #include <io.h>
  27. #endif
  28. #include <unistd.h>
  29. #include "config.h"
  30. #ifdef HAVE_LIBUDEV
  31. #include <libudev.h>
  32. #endif
  33. #include "elist.h"
  34. #include "miner.h"
  35. struct device_api bitforce_api;
  36. static int BFopen(const char *devpath)
  37. {
  38. #ifdef WIN32
  39. HANDLE hSerial = CreateFile(devpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  40. if (unlikely(hSerial == INVALID_HANDLE_VALUE))
  41. return -1;
  42. COMMTIMEOUTS cto = {30000, 0, 30000, 0, 30000};
  43. SetCommTimeouts(hSerial, &cto);
  44. return _open_osfhandle((LONG)hSerial, 0);
  45. #else
  46. int fdDev = open(devpath, O_RDWR | O_CLOEXEC | O_NOCTTY);
  47. if (likely(fdDev != -1)) {
  48. struct termios pattr;
  49. tcgetattr(fdDev, &pattr);
  50. pattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  51. pattr.c_oflag &= ~OPOST;
  52. pattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  53. pattr.c_cflag &= ~(CSIZE | PARENB);
  54. pattr.c_cflag |= CS8;
  55. tcsetattr(fdDev, TCSANOW, &pattr);
  56. }
  57. tcflush(fdDev, TCOFLUSH);
  58. tcflush(fdDev, TCIFLUSH);
  59. return fdDev;
  60. #endif
  61. }
  62. static void BFgets(char *buf, size_t bufLen, int fd)
  63. {
  64. do
  65. --bufLen;
  66. while (likely(bufLen && read(fd, buf, 1) && (buf++)[0] != '\n'))
  67. ;
  68. buf[0] = '\0';
  69. }
  70. static void BFwrite(int fd, const void *buf, ssize_t bufLen)
  71. {
  72. ssize_t ret = write(fd, buf, bufLen);
  73. if (unlikely(ret != bufLen))
  74. quit(1, "BFwrite failed");
  75. }
  76. #define BFclose(fd) close(fd)
  77. static bool bitforce_detect_one(const char *devpath)
  78. {
  79. char pdevbuf[0x100];
  80. if (total_devices == MAX_DEVICES)
  81. return false;
  82. int fdDev = BFopen(devpath);
  83. if (unlikely(fdDev == -1)) {
  84. applog(LOG_DEBUG, "BitForce Detect: Failed to open %s", devpath);
  85. return false;
  86. }
  87. BFwrite(fdDev, "ZGX", 3);
  88. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  89. if (unlikely(!pdevbuf[0])) {
  90. applog(LOG_ERR, "Error reading from BitForce (ZGX)");
  91. return 0;
  92. }
  93. BFclose(fdDev);
  94. if (unlikely(!strstr(pdevbuf, "SHA256"))) {
  95. applog(LOG_DEBUG, "BitForce Detect: Didn't recognise BitForce on %s", devpath);
  96. return false;
  97. }
  98. // We have a real BitForce!
  99. struct cgpu_info *bitforce;
  100. bitforce = calloc(1, sizeof(*bitforce));
  101. bitforce->api = &bitforce_api;
  102. bitforce->device_path = strdup(devpath);
  103. bitforce->deven = DEV_ENABLED;
  104. bitforce->threads = 1;
  105. return add_cgpu(bitforce);
  106. }
  107. static bool bitforce_detect_auto_udev()
  108. {
  109. #ifdef HAVE_LIBUDEV
  110. struct udev *udev = udev_new();
  111. struct udev_enumerate *enumerate = udev_enumerate_new(udev);
  112. struct udev_list_entry *list_entry;
  113. bool foundany = false;
  114. udev_enumerate_add_match_subsystem(enumerate, "tty");
  115. udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
  116. udev_enumerate_scan_devices(enumerate);
  117. udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
  118. struct udev_device *device = udev_device_new_from_syspath(
  119. udev_enumerate_get_udev(enumerate),
  120. udev_list_entry_get_name(list_entry)
  121. );
  122. if (!device)
  123. continue;
  124. const char *devpath = udev_device_get_devnode(device);
  125. if (devpath) {
  126. foundany = true;
  127. bitforce_detect_one(devpath);
  128. }
  129. udev_device_unref(device);
  130. }
  131. udev_enumerate_unref(enumerate);
  132. udev_unref(udev);
  133. return foundany;
  134. #else
  135. return false;
  136. #endif
  137. }
  138. static bool bitforce_detect_auto_devserial()
  139. {
  140. #ifndef WIN32
  141. DIR *D;
  142. struct dirent *de;
  143. const char udevdir[] = "/dev/serial/by-id";
  144. char devpath[sizeof(udevdir) + 1 + NAME_MAX];
  145. char *devfile = devpath + sizeof(udevdir);
  146. bool foundany = false;
  147. D = opendir(udevdir);
  148. if (!D)
  149. return false;
  150. memcpy(devpath, udevdir, sizeof(udevdir) - 1);
  151. devpath[sizeof(udevdir) - 1] = '/';
  152. while ( (de = readdir(D)) ) {
  153. if (!strstr(de->d_name, "BitFORCE_SHA256"))
  154. continue;
  155. foundany = true;
  156. strcpy(devfile, de->d_name);
  157. bitforce_detect_one(devpath);
  158. }
  159. closedir(D);
  160. return foundany;
  161. #else
  162. return false;
  163. #endif
  164. }
  165. static void bitforce_detect_auto()
  166. {
  167. bitforce_detect_auto_udev() ?:
  168. bitforce_detect_auto_devserial() ?:
  169. 0;
  170. }
  171. static void bitforce_detect()
  172. {
  173. struct string_elist *iter, *tmp;
  174. bool found = false;
  175. bool autoscan = false;
  176. list_for_each_entry_safe(iter, tmp, &scan_devices, list) {
  177. if (!strcmp(iter->string, "auto"))
  178. autoscan = true;
  179. else if (bitforce_detect_one(iter->string)) {
  180. string_elist_del(iter);
  181. found = true;
  182. }
  183. }
  184. if (autoscan || !found)
  185. bitforce_detect_auto();
  186. }
  187. static void get_bitforce_statline_before(char *buf, struct cgpu_info *bitforce)
  188. {
  189. float gt = bitforce->temp;
  190. if (gt > 0)
  191. tailsprintf(buf, "%5.1fC ", gt);
  192. else
  193. tailsprintf(buf, " ", gt);
  194. tailsprintf(buf, " | ");
  195. }
  196. static bool bitforce_thread_prepare(struct thr_info *thr)
  197. {
  198. struct cgpu_info *bitforce = thr->cgpu;
  199. struct timeval now;
  200. int fdDev = BFopen(bitforce->device_path);
  201. if (unlikely(-1 == fdDev)) {
  202. applog(LOG_ERR, "Failed to open BitForce on %s", bitforce->device_path);
  203. return false;
  204. }
  205. bitforce->device_fd = fdDev;
  206. applog(LOG_INFO, "Opened BitForce on %s", bitforce->device_path);
  207. gettimeofday(&now, NULL);
  208. get_datestamp(bitforce->init, &now);
  209. return true;
  210. }
  211. static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint64_t __maybe_unused max_nonce)
  212. {
  213. struct cgpu_info *bitforce = thr->cgpu;
  214. int fdDev = bitforce->device_fd;
  215. char pdevbuf[0x100];
  216. unsigned char ob[61] = ">>>>>>>>12345678901234567890123456789012123456789012>>>>>>>>";
  217. int i;
  218. char *pnoncebuf;
  219. char *s;
  220. uint32_t nonce;
  221. BFwrite(fdDev, "ZDX", 3);
  222. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  223. if (unlikely(!pdevbuf[0])) {
  224. applog(LOG_ERR, "Error reading from BitForce (ZDX)");
  225. return 0;
  226. }
  227. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  228. applog(LOG_ERR, "BitForce ZDX reports: %s", pdevbuf);
  229. return 0;
  230. }
  231. memcpy(ob + 8, work->midstate, 32);
  232. memcpy(ob + 8 + 32, work->data + 64, 12);
  233. BFwrite(fdDev, ob, 60);
  234. if (opt_debug) {
  235. s = bin2hex(ob + 8, 44);
  236. applog(LOG_DEBUG, "BitForce block data: %s", s);
  237. free(s);
  238. }
  239. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  240. if (unlikely(!pdevbuf[0])) {
  241. applog(LOG_ERR, "Error reading from BitForce (block data)");
  242. return 0;
  243. }
  244. if (unlikely(pdevbuf[0] != 'O' || pdevbuf[1] != 'K')) {
  245. applog(LOG_ERR, "BitForce block data reports: %s", pdevbuf);
  246. return 0;
  247. }
  248. BFwrite(fdDev, "ZLX", 3);
  249. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  250. if (unlikely(!pdevbuf[0])) {
  251. applog(LOG_ERR, "Error reading from BitForce (ZKX)");
  252. return 0;
  253. }
  254. if ((!strncasecmp(pdevbuf, "TEMP", 4)) && (s = strchr(pdevbuf + 4, ':'))) {
  255. float temp = strtof(s + 1, NULL);
  256. if (temp > 0) {
  257. bitforce->temp = temp;
  258. if (temp > bitforce->cutofftemp) {
  259. applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
  260. bitforce->deven = DEV_RECOVER;
  261. }
  262. }
  263. }
  264. usleep(4500000);
  265. i = 4500;
  266. while (1) {
  267. BFwrite(fdDev, "ZFX", 3);
  268. BFgets(pdevbuf, sizeof(pdevbuf), fdDev);
  269. if (unlikely(!pdevbuf[0])) {
  270. applog(LOG_ERR, "Error reading from BitForce (ZFX)");
  271. return 0;
  272. }
  273. if (pdevbuf[0] != 'B')
  274. break;
  275. usleep(10000);
  276. i += 10;
  277. }
  278. applog(LOG_DEBUG, "BitForce waited %dms until %s\n", i, pdevbuf);
  279. work->blk.nonce = 0xffffffff;
  280. if (pdevbuf[2] == '-')
  281. return 0xffffffff;
  282. else if (strncasecmp(pdevbuf, "NONCE-FOUND", 11)) {
  283. applog(LOG_ERR, "BitForce result reports: %s", pdevbuf);
  284. return 0;
  285. }
  286. pnoncebuf = &pdevbuf[12];
  287. while (1) {
  288. hex2bin((void*)&nonce, pnoncebuf, 4);
  289. #ifndef __BIG_ENDIAN__
  290. nonce = swab32(nonce);
  291. #endif
  292. submit_nonce(thr, work, nonce);
  293. if (pnoncebuf[8] != ',')
  294. break;
  295. pnoncebuf += 9;
  296. }
  297. return 0xffffffff;
  298. }
  299. struct device_api bitforce_api = {
  300. .dname = "bitforce",
  301. .name = "PGA",
  302. .api_detect = bitforce_detect,
  303. .get_statline_before = get_bitforce_statline_before,
  304. .thread_prepare = bitforce_thread_prepare,
  305. .scanhash = bitforce_scanhash,
  306. };