driver-blockerupter.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "config.h"
  2. #include <limits.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <dirent.h>
  8. #include <unistd.h>
  9. #include <math.h>
  10. #ifndef WIN32
  11. #include <termios.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #ifndef O_CLOEXEC
  15. #define O_CLOEXEC 0
  16. #endif
  17. #else
  18. #include <windows.h>
  19. #include <io.h>
  20. #endif
  21. #include "elist.h"
  22. #include "miner.h"
  23. #include "driver-blockerupter.h"
  24. #include "usbutils.h"
  25. static void blockerupter_space_mode(struct cgpu_info *blockerupter)
  26. {
  27. int interface;
  28. unsigned int bits = 0;
  29. interface = usb_interface(blockerupter);
  30. bits |= CP210X_BITS_DATA_8;
  31. bits |= CP210X_BITS_PARITY_SPACE;
  32. usb_transfer_data(blockerupter, CP210X_TYPE_OUT, CP210X_SET_LINE_CTL, bits, interface, NULL, 0, C_SETPARITY);
  33. }
  34. static void blockerupter_mark_mode(struct cgpu_info *blockerupter)
  35. {
  36. int interface;
  37. unsigned int bits = 0;
  38. interface = usb_interface(blockerupter);
  39. bits |= CP210X_BITS_DATA_8;
  40. bits |= CP210X_BITS_PARITY_MARK;
  41. usb_transfer_data(blockerupter, CP210X_TYPE_OUT, CP210X_SET_LINE_CTL, bits, interface, NULL, 0, C_SETPARITY);
  42. }
  43. static void blockerupter_init_com(struct cgpu_info *blockerupter)
  44. {
  45. uint32_t baudrate;
  46. int interface;
  47. if (blockerupter->usbinfo.nodev)
  48. return;
  49. interface = usb_interface(blockerupter);
  50. // Enable the UART
  51. usb_transfer_data(blockerupter, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  52. CP210X_VALUE_UART_ENABLE, interface, NULL, 0, C_ENABLE_UART);
  53. if (blockerupter->usbinfo.nodev)
  54. return;
  55. // Set data control
  56. usb_transfer_data(blockerupter, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
  57. interface, NULL, 0, C_SETDATA);
  58. if (blockerupter->usbinfo.nodev)
  59. return;
  60. // Set the baud
  61. baudrate = BET_BAUD;
  62. usb_transfer_data(blockerupter, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
  63. interface, &baudrate, sizeof (baudrate), C_SETBAUD);
  64. // Set space mode
  65. blockerupter_space_mode(blockerupter);
  66. }
  67. static int blockerupter_send(struct cgpu_info *blockerupter, char *data, int len)
  68. {
  69. int err;
  70. int bytes_sent;
  71. if (unlikely(blockerupter->usbinfo.nodev))
  72. return SEND_FAIL;
  73. err = usb_write(blockerupter, data, len, &bytes_sent, C_BET_WRITE);
  74. if (err || bytes_sent != len) {
  75. applog(LOG_DEBUG, "blockerupter: Send (%d/%d)", bytes_sent, len);
  76. return SEND_FAIL;
  77. }
  78. return SEND_OK;
  79. }
  80. static int blockerupter_read(struct cgpu_info *blockerupter, char *data, int len)
  81. {
  82. int err;
  83. int bytes_read;
  84. if (unlikely(blockerupter->usbinfo.nodev))
  85. return READ_FAIL;
  86. err = usb_read_timeout(blockerupter, data, len, &bytes_read, 2, C_BET_READ);
  87. if (err || bytes_read != len) {
  88. applog(LOG_DEBUG, "blockerupter: Read (%d/%d)", bytes_read, len);
  89. return READ_FAIL;
  90. }
  91. return READ_OK;
  92. }
  93. static void blockerupter_setclock(struct cgpu_info *blockerupter, uint8_t clock)
  94. {
  95. struct blockerupter_info *info;
  96. info = blockerupter->device_data;
  97. char command;
  98. int err;
  99. command = C_GCK | clock;
  100. info->clock = clock;
  101. err = blockerupter_send(blockerupter, &command, 1);
  102. if (!err)
  103. applog(LOG_DEBUG, "%s%d: Set Clock to %d MHz", blockerupter->drv->name,
  104. blockerupter->device_id, (clock + 1) * 10 / 2);
  105. }
  106. static void blockerupter_setdiff(struct cgpu_info *blockerupter, int diff)
  107. {
  108. struct blockerupter_info *info;
  109. info = blockerupter->device_data;
  110. char command,bits;
  111. int err;
  112. int local_diff;
  113. // min_diff for driver is 64
  114. if (diff >= 262144) {
  115. bits = 3;
  116. local_diff = 262144;
  117. } else if (diff >= 4096) {
  118. bits = 2;
  119. local_diff = 4096;
  120. } else {
  121. bits = 1;
  122. local_diff = 64;
  123. }
  124. if (local_diff == info->diff)
  125. return;
  126. command = C_DIF | bits;
  127. err = blockerupter_send(blockerupter, &command, 1);
  128. if (!err) {
  129. applog(LOG_DEBUG, "%s%d: Set Diff Bits to %d", blockerupter->drv->name,
  130. blockerupter->device_id, bits);
  131. info->diff = local_diff;
  132. }
  133. }
  134. static void blockerupter_setrolling(struct cgpu_info *blockerupter, uint8_t rolling)
  135. {
  136. struct blockerupter_info *info;
  137. info = blockerupter->device_data;
  138. char command;
  139. int err;
  140. command = C_LPO | rolling;
  141. err = blockerupter_send(blockerupter, &command, 1);
  142. if (!err) {
  143. applog(LOG_DEBUG, "%s%d: Set nTime Rolling to %d seconds", blockerupter->drv->name,
  144. blockerupter->device_id, (rolling + 1) * 30);
  145. info->rolling = (rolling + 1) * 30;
  146. }
  147. }
  148. static void blockerupter_init(struct cgpu_info *blockerupter)
  149. {
  150. struct blockerupter_info *info;
  151. info = blockerupter->device_data;
  152. // Set Clock
  153. if (!opt_bet_clk || opt_bet_clk< 19 || opt_bet_clk > 31) {
  154. opt_bet_clk = BET_CLOCK_DEFAULT;
  155. }
  156. blockerupter_setclock(blockerupter, opt_bet_clk);
  157. info->clock = (opt_bet_clk + 1) * 10;
  158. info->expected = info->clock * 24 * 32 * info->found / 1000.0;
  159. // Set Diff
  160. blockerupter_setdiff(blockerupter, BET_DIFF_DEFAULT);
  161. info->diff = BET_DIFF_DEFAULT;
  162. // Set nTime Rolling
  163. blockerupter_setrolling(blockerupter, BET_ROLLING_DEFAULT);
  164. info->rolling = (BET_ROLLING_DEFAULT + 1) * 30;
  165. cgtime(&info->start_time);
  166. }
  167. static struct cgpu_info *blockerupter_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  168. {
  169. struct blockerupter_info *info;
  170. struct cgpu_info *blockerupter = usb_alloc_cgpu(&blockerupter_drv, 1);
  171. int i, err;
  172. char reset = C_RES;
  173. if (!usb_init(blockerupter, dev, found)) {
  174. applog(LOG_ERR, "Blockerupter usb init failed");
  175. blockerupter = usb_free_cgpu(blockerupter);
  176. return NULL;
  177. }
  178. blockerupter->device_data = (struct blockerupter_info *) malloc(sizeof(struct blockerupter_info));
  179. info = blockerupter->device_data;
  180. memset(info, 0, sizeof(blockerupter_info));
  181. blockerupter_init_com(blockerupter);
  182. err = blockerupter_send(blockerupter, &reset, 1);
  183. if (err) {
  184. applog(LOG_ERR, "Blockerupter detect failed");
  185. blockerupter = usb_free_cgpu(blockerupter);
  186. return NULL;
  187. }
  188. cgsleep_ms(5000);
  189. for (i = 0; i < BET_MAXBOARDS; i++) {
  190. char detect, answer;
  191. answer = 0;
  192. detect = C_ASK | (uint8_t)i;
  193. blockerupter_send(blockerupter, &detect, 1);
  194. blockerupter_read(blockerupter, &answer, 1);
  195. if (answer == A_WAL) {
  196. applog(LOG_DEBUG, "BlockErupter found Board: %d", i);
  197. info->boards[i] = 1;
  198. info->found++;
  199. } else {
  200. applog(LOG_DEBUG, "BlockErupter missing board: %d, received %02x",
  201. i, answer);
  202. }
  203. }
  204. if (!info->found) {
  205. usb_free_cgpu(blockerupter);
  206. free(info);
  207. return NULL;
  208. } else {
  209. blockerupter->threads = 1;
  210. add_cgpu(blockerupter);
  211. applog(LOG_DEBUG, "Add BlockErupter with %d/%d Boards", info->found,
  212. BET_MAXBOARDS);
  213. blockerupter_init(blockerupter);
  214. return blockerupter;
  215. }
  216. }
  217. static inline void blockerupter_detect(bool __maybe_unused hotplug)
  218. {
  219. usb_detect(&blockerupter_drv, blockerupter_detect_one);
  220. }
  221. static struct api_data *blockerupter_api_stats(struct cgpu_info *blockerupter)
  222. {
  223. struct blockerupter_info *info = blockerupter->device_data;
  224. struct api_data *root = NULL;
  225. struct timeval now, elapsed;
  226. char buf[32];
  227. int i;
  228. cgtime(&now);
  229. timersub(&now, &info->start_time, &elapsed);
  230. info->hashrate = elapsed.tv_sec ? info->hashes * 4.295 / elapsed.tv_sec : 0;
  231. info->eff = info->hashrate / info->expected;
  232. root = api_add_int(root, "Nonces", &info->nonces, false);
  233. root = api_add_uint8(root, "Board", &info->found, false);
  234. root = api_add_int(root, "Clock", &info->clock, false);
  235. root = api_add_int(root,"Accepted", &info->accepted, false);
  236. root = api_add_double(root, "HashRate", &info->hashrate , false);
  237. root = api_add_double(root, "Expected", &info->expected , false);
  238. root = api_add_double(root, "Efficiency", &info->eff, false);
  239. for (i = 0; i < BET_MAXBOARDS; i++) {
  240. double brd_hashrate;
  241. if (info->boards[i]) {
  242. sprintf(buf, "Board%02d accepted", i);
  243. root = api_add_int(root, buf, &info->b_info[i].accepted, false);
  244. sprintf(buf, "Board%02d nonces", i);
  245. root = api_add_int(root, buf, &info->b_info[i].nonces, false);
  246. sprintf(buf, "Board%02d hwerror", i);
  247. root = api_add_double(root, buf, &info->b_info[i].hwe, false);
  248. sprintf(buf, "Board%02d hashrate", i);
  249. brd_hashrate = elapsed.tv_sec ? info->b_info[i].hashes * 4.295 / elapsed.tv_sec : 0;
  250. root = api_add_double(root, buf, &brd_hashrate, false);
  251. }
  252. }
  253. return root;
  254. }
  255. static bool blockerupter_prepare(struct thr_info *thr)
  256. {
  257. struct cgpu_info *blockerupter = thr->cgpu;
  258. struct blockerupter_info *info = blockerupter->device_data;
  259. cglock_init(&(info->pool.data_lock));
  260. return true;
  261. }
  262. static void blockerupter_sendjob(struct cgpu_info *blockerupter, int board)
  263. {
  264. struct blockerupter_info *info = blockerupter->device_data;
  265. struct thr_info *thr = blockerupter->thr[0];
  266. struct work *work;
  267. uint8_t command, answer;
  268. int err;
  269. work = get_work(thr, thr->id);
  270. memcpy(&info->works[info->work_idx],work,sizeof(struct work));
  271. blockerupter_setdiff(blockerupter,floor(work->work_difficulty));
  272. command = C_JOB | (uint8_t)board;
  273. blockerupter_send(blockerupter, (char *)&command, 1);
  274. blockerupter_mark_mode(blockerupter);
  275. cgsleep_ms(1);
  276. blockerupter_send(blockerupter, (char *)(work->midstate), 32);
  277. blockerupter_send(blockerupter, (char *)&(work->data[64]), 12);
  278. blockerupter_send(blockerupter, (char *)&work->nonce2, 4);
  279. blockerupter_send(blockerupter, (char *)&info->work_idx, 1);
  280. cgsleep_ms(1);
  281. blockerupter_space_mode(blockerupter);
  282. answer = 0;
  283. err = blockerupter_read(blockerupter, (char *)&answer, 1);
  284. cgtime(&info->last_job);
  285. if (err || answer != A_GET) {
  286. applog(LOG_ERR, "%s%d: Sync Error", blockerupter->drv->name, blockerupter->device_id);
  287. } else {
  288. info->b_info[board].job_count++;
  289. applog(LOG_DEBUG, "%s%d: Sent work %d to board %d", blockerupter->drv->name,
  290. blockerupter->device_id, info->work_idx, board);
  291. }
  292. info->work_idx++;
  293. if (info->work_idx >= BET_WORK_FIFO)
  294. info->work_idx = 0;
  295. }
  296. static uint64_t blockerupter_checknonce(struct cgpu_info *blockerupter, struct blockerupter_response *resp, int board)
  297. {
  298. uint8_t test;
  299. struct blockerupter_info *info;
  300. struct thr_info *thr = blockerupter->thr[0];
  301. struct work work;
  302. uint32_t nonce;
  303. uint64_t hashes=0;
  304. int i;
  305. struct board_info *cur_brd;
  306. struct asic_info *cur_asic;
  307. info = blockerupter->device_data;
  308. work = info->works[resp->work_idx];
  309. nonce = *(uint32_t *)resp->nonce;
  310. applog(LOG_DEBUG, "%s%d: Nonce %08x from board %d, asic %d for work %d",
  311. blockerupter->drv->name, blockerupter->device_id, *(uint32_t *) resp->nonce,
  312. board, resp->chip, resp->work_idx);
  313. memcpy(work.data + 4 + 32 + 32, resp->ntime, 4);
  314. __bin2hex(work.ntime, resp->ntime, 4);
  315. info->nonces++;
  316. cur_brd = &info->b_info[board];
  317. cur_brd->nonces++;
  318. cur_asic = &info->b_info[board].asics[resp->chip];
  319. cur_asic->nonces++;
  320. for (i = 0; i < BET_NONCE_FIX; i++) {
  321. test = test_nonce_diff(&work, nonce + i, (double)info->diff);
  322. if (test) {
  323. applog(LOG_DEBUG, "%s%d: Nonce Fix Pass @%d", blockerupter->drv->name,
  324. blockerupter->device_id, i);
  325. info->hashes += info->diff;
  326. cur_brd->hashes += info->diff;
  327. cur_asic->hashes += info->diff;
  328. if (test_nonce_diff(&work, nonce + i, work.work_difficulty)) {
  329. if (submit_nonce(thr, &work, nonce + i)) {
  330. hashes += floor(work.work_difficulty) * (uint64_t) 0xffffffff;
  331. info->accepted++;
  332. cur_brd->accepted++;
  333. cur_asic->accepted++;
  334. }
  335. }
  336. break;
  337. }
  338. }
  339. if (i == BET_NONCE_FIX) {
  340. applog(LOG_DEBUG, "%s%d: Nonce Fix Failed", blockerupter->drv->name,
  341. blockerupter->device_id);
  342. cur_brd->bad++;
  343. cur_brd->hwe = cur_brd->nonces ? (double)cur_brd->bad / cur_brd->nonces : 0;
  344. cur_asic->bad++;
  345. cur_asic->hwe = cur_asic->nonces ? (double)cur_asic->bad / cur_asic->nonces : 0;
  346. }
  347. return hashes;
  348. }
  349. static uint64_t blockerupter_getresp(struct cgpu_info *blockerupter, int board)
  350. {
  351. struct blockerupter_response *resp;
  352. int err;
  353. uint64_t hashes = 0;
  354. resp = (struct blockerupter_response *) malloc(BET_RESP_SZ);
  355. err = blockerupter_read(blockerupter, (char *)resp, BET_RESP_SZ);
  356. if (!err)
  357. hashes = blockerupter_checknonce(blockerupter, resp, board);
  358. free(resp);
  359. return hashes;
  360. }
  361. static int64_t blockerupter_scanhash(struct thr_info *thr)
  362. {
  363. struct cgpu_info *blockerupter = thr->cgpu;
  364. struct blockerupter_info *info = blockerupter->device_data;
  365. char ask;
  366. uint8_t answer;
  367. int i;
  368. int64_t hashes=0;
  369. if (unlikely(blockerupter->usbinfo.nodev)) {
  370. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  371. blockerupter->drv->name, blockerupter->device_id);
  372. return -1;
  373. }
  374. for (i = 0; i < BET_MAXBOARDS; i++) {
  375. if (!info->boards[i])
  376. continue;
  377. ask = C_ASK | (uint8_t)i;
  378. blockerupter_send(blockerupter, &ask, 1);
  379. cgsleep_ms(1);
  380. answer = 0;
  381. blockerupter_read(blockerupter, (char *)&answer, 1);
  382. switch (answer) {
  383. case A_WAL:
  384. blockerupter_sendjob(blockerupter, i);
  385. break;
  386. case A_YES:
  387. hashes += blockerupter_getresp(blockerupter, i);
  388. break;
  389. case A_NO:
  390. break;
  391. default:
  392. applog(LOG_ERR, "%s%d: Unexpected value %02x received", blockerupter->drv->name,
  393. blockerupter->device_id, answer);
  394. break;
  395. }
  396. }
  397. return hashes;
  398. }
  399. static void blockerupter_flush_work(struct cgpu_info *blockerupter)
  400. {
  401. uint8_t command = C_LPO | BET_ROLLING_DEFAULT;
  402. blockerupter_send(blockerupter, (char *)&command, 1);
  403. }
  404. struct device_drv blockerupter_drv = {
  405. .drv_id = DRIVER_blockerupter,
  406. .dname = "blockerupter",
  407. .name = "BET",
  408. .min_diff = 64,
  409. .get_api_stats = blockerupter_api_stats,
  410. .drv_detect = blockerupter_detect,
  411. .thread_prepare = blockerupter_prepare,
  412. .hash_work = hash_driver_work,
  413. .flush_work = blockerupter_flush_work,
  414. .scanwork = blockerupter_scanhash
  415. };