bitforce.c 5.0 KB

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