driver-blockerupter.c 13 KB

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