driver-bitfury.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Anatoly Legkodymov
  4. * Copyright 2013 Luke Dashjr
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include <limits.h>
  26. #include "miner.h"
  27. #include <unistd.h>
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <sha2.h>
  31. #include "deviceapi.h"
  32. #include "driver-bitfury.h"
  33. #include "libbitfury.h"
  34. #include "util.h"
  35. #include "spidevc.h"
  36. struct device_drv bitfury_drv;
  37. static
  38. int bitfury_autodetect()
  39. {
  40. RUNONCE(0);
  41. int chip_n;
  42. struct cgpu_info *bitfury_info;
  43. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  44. bitfury_info->drv = &bitfury_drv;
  45. bitfury_info->threads = 1;
  46. applog(LOG_INFO, "INFO: bitfury_detect");
  47. spi_init();
  48. if (!sys_spi)
  49. return 0;
  50. chip_n = libbitfury_detectChips1(sys_spi);
  51. if (!chip_n) {
  52. applog(LOG_WARNING, "No Bitfury chips detected!");
  53. return 0;
  54. } else {
  55. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  56. }
  57. bitfury_info->procs = chip_n;
  58. add_cgpu(bitfury_info);
  59. return 1;
  60. }
  61. static void bitfury_detect(void)
  62. {
  63. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  64. }
  65. static
  66. void *bitfury_just_io(struct bitfury_device * const bitfury)
  67. {
  68. struct spi_port * const spi = bitfury->spi;
  69. const int chip = bitfury->fasync;
  70. void *rv;
  71. spi_clear_buf(spi);
  72. spi_emit_break(spi);
  73. spi_emit_fasync(spi, chip);
  74. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  75. spi_txrx(spi);
  76. return rv;
  77. }
  78. static
  79. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  80. {
  81. const struct bitfury_device * const bitfury = proc->device_data;
  82. const int active = bitfury->active;
  83. char s[((1 + 8) * 0x10) + 1];
  84. char *sp = s;
  85. for (int i = 0; i < 0x10; ++i)
  86. sp += sprintf(sp, "%c%08lx",
  87. (active == i) ? '>' : ' ',
  88. (unsigned long)bitfury_decnonce(inp[i]));
  89. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  90. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  91. }
  92. static
  93. bool bitfury_init_oldbuf(struct cgpu_info * const proc, const uint32_t *inp)
  94. {
  95. struct bitfury_device * const bitfury = proc->device_data;
  96. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  97. uint32_t * const buf = &bitfury->newbuf[0];
  98. int i, differ, tried = 0;
  99. if (!inp)
  100. inp = bitfury_just_io(bitfury);
  101. tryagain:
  102. if (tried > 3)
  103. {
  104. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  105. proc->proc_repr, __func__, tried);
  106. bitfury->desync_counter = 99;
  107. return false;
  108. }
  109. ++tried;
  110. memcpy(buf, inp, 0x10 * 4);
  111. inp = bitfury_just_io(bitfury);
  112. differ = -1;
  113. for (i = 0; i < 0x10; ++i)
  114. {
  115. if (inp[i] != buf[i])
  116. {
  117. if (differ != -1)
  118. {
  119. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  120. proc->proc_repr, __func__, i);
  121. goto tryagain;
  122. }
  123. differ = i;
  124. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  125. proc->proc_repr, __func__, i);
  126. if (tried > 3)
  127. break;
  128. }
  129. }
  130. if (-1 == differ)
  131. {
  132. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  133. proc->proc_repr, __func__);
  134. goto tryagain;
  135. }
  136. bitfury->active = differ;
  137. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  138. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  139. bitfury->oldjob = inp[0x10];
  140. bitfury->desync_counter = 0;
  141. if (opt_debug)
  142. bitfury_debug_nonce_array(proc, "Init", inp);
  143. return true;
  144. }
  145. bool bitfury_init_chip(struct cgpu_info * const proc)
  146. {
  147. struct bitfury_device * const bitfury = proc->device_data;
  148. struct bitfury_payload payload = {
  149. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  150. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  151. .m7 = 0xafbd0b42,
  152. .ntime = 0xb6c24563,
  153. .nbits = 0x6dfa4352,
  154. };
  155. bitfury_payload_to_atrvec(bitfury->atrvec, &payload);
  156. return bitfury_init_oldbuf(proc, NULL);
  157. }
  158. static
  159. bool bitfury_init(struct thr_info *thr)
  160. {
  161. struct cgpu_info *proc;
  162. struct bitfury_device *bitfury;
  163. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  164. {
  165. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  166. *bitfury = (struct bitfury_device){
  167. .spi = sys_spi,
  168. .fasync = proc->proc_id,
  169. };
  170. bitfury_init_chip(proc);
  171. bitfury->osc6_bits = 50;
  172. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  173. }
  174. timer_set_now(&thr->tv_poll);
  175. return true;
  176. }
  177. void bitfury_shutdown(struct thr_info *thr) {
  178. struct cgpu_info *cgpu = thr->cgpu, *proc;
  179. struct bitfury_device *bitfury;
  180. applog(LOG_INFO, "INFO bitfury_shutdown");
  181. for (proc = cgpu; proc; proc = proc->next_proc)
  182. {
  183. bitfury = proc->device_data;
  184. bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  185. }
  186. }
  187. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  188. {
  189. struct cgpu_info * const proc = thr->cgpu;
  190. struct bitfury_device * const bitfury = proc->device_data;
  191. if (opt_debug)
  192. {
  193. char hex[153];
  194. bin2hex(hex, &work->data[0], 76);
  195. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  196. proc->proc_repr, hex);
  197. }
  198. work_to_bitfury_payload(&bitfury->payload, work);
  199. bitfury_payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  200. work->blk.nonce = 0xffffffff;
  201. return true;
  202. }
  203. static
  204. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  205. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  206. uint32_t nonce;
  207. int i;
  208. if (unlikely(!work))
  209. return false;
  210. for (i = 0; i < 6; ++i)
  211. {
  212. nonce = *nonce_p + offsets[i];
  213. if (test_nonce(work, nonce, false))
  214. {
  215. *nonce_p = nonce;
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  222. {
  223. }
  224. // freq_stat->{mh,s} are allocated such that [osc6_min] is the first valid index and [0] falls outside the allocation
  225. void bitfury_init_freq_stat(struct freq_stat * const c, const int osc6_min, const int osc6_max)
  226. {
  227. const int osc6_values = (osc6_max + 1 - osc6_min);
  228. void * const p = malloc(osc6_values * (sizeof(*c->mh) + sizeof(*c->s)));
  229. c->mh = p - (sizeof(*c->mh) * osc6_min);
  230. c->s = p + (sizeof(*c->mh) * osc6_values) - (sizeof(*c->s) * osc6_min);
  231. c->osc6_min = osc6_min;
  232. c->osc6_max = osc6_max;
  233. }
  234. void bitfury_clean_freq_stat(struct freq_stat * const c)
  235. {
  236. free(&c->mh[c->osc6_min]);
  237. }
  238. #define HOP_DONE 600
  239. typedef uint32_t bitfury_inp_t[0x11];
  240. static
  241. int bitfury_select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
  242. int freq;
  243. int random;
  244. int i;
  245. bool all_done;
  246. struct freq_stat *c;
  247. c = &bitfury->chip_stat;
  248. if (c->best_done) {
  249. freq = c->best_osc;
  250. } else {
  251. random = (int)(bitfury->mhz * 1000.0) & 1;
  252. freq = (bitfury->osc6_bits == c->osc6_max) ? c->osc6_min : bitfury->osc6_bits + random;
  253. all_done = true;
  254. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  255. if (c->s[i] <= HOP_DONE)
  256. {
  257. all_done = false;
  258. break;
  259. }
  260. if (all_done)
  261. {
  262. double mh_max = 0.0;
  263. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  264. {
  265. const double mh_actual = c->mh[i] / c->s[i];
  266. if (mh_max >= mh_actual)
  267. continue;
  268. mh_max = mh_actual;
  269. freq = i;
  270. }
  271. c->best_done = 1;
  272. c->best_osc = freq;
  273. applog(LOG_DEBUG, "%"PRIpreprv": best_osc = %d",
  274. proc->proc_repr, freq);
  275. }
  276. }
  277. applog(LOG_DEBUG, "%"PRIpreprv": Changing osc6_bits to %d",
  278. proc->proc_repr, freq);
  279. bitfury->osc6_bits = freq;
  280. bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  281. return 0;
  282. }
  283. void bitfury_do_io(struct thr_info * const master_thr)
  284. {
  285. struct cgpu_info *proc;
  286. struct thr_info *thr;
  287. struct bitfury_device *bitfury;
  288. struct freq_stat *c;
  289. const uint32_t *inp;
  290. int n, i, j;
  291. bool newjob;
  292. uint32_t nonce;
  293. int n_chips = 0, lastchip = 0;
  294. struct spi_port *spi = NULL;
  295. bool should_be_running;
  296. struct timeval tv_now;
  297. uint32_t counter;
  298. struct timeval *tvp_stat;
  299. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  300. ++n_chips;
  301. struct cgpu_info *procs[n_chips];
  302. void *rxbuf[n_chips];
  303. bitfury_inp_t rxbuf_copy[n_chips];
  304. // NOTE: This code assumes:
  305. // 1) that chips on the same SPI bus are grouped together
  306. // 2) that chips are in sequential fasync order
  307. n_chips = 0;
  308. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  309. {
  310. thr = proc->thr[0];
  311. bitfury = proc->device_data;
  312. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  313. if (should_be_running)
  314. {
  315. if (spi != bitfury->spi)
  316. {
  317. if (spi)
  318. spi_txrx(spi);
  319. spi = bitfury->spi;
  320. spi_clear_buf(spi);
  321. spi_emit_break(spi);
  322. lastchip = 0;
  323. }
  324. procs[n_chips] = proc;
  325. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  326. lastchip = bitfury->fasync;
  327. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  328. ++n_chips;
  329. }
  330. else
  331. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  332. ;//FIXME: shutdown chip
  333. }
  334. timer_set_now(&tv_now);
  335. spi_txrx(spi);
  336. for (j = 0; j < n_chips; ++j)
  337. {
  338. memcpy(rxbuf_copy[j], rxbuf[j], 0x11 * 4);
  339. rxbuf[j] = rxbuf_copy[j];
  340. }
  341. for (j = 0; j < n_chips; ++j)
  342. {
  343. proc = procs[j];
  344. thr = proc->thr[0];
  345. bitfury = proc->device_data;
  346. tvp_stat = &bitfury->tv_stat;
  347. c = &bitfury->chip_stat;
  348. uint32_t * const newbuf = &bitfury->newbuf[0];
  349. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  350. inp = rxbuf[j];
  351. if (unlikely(bitfury->desync_counter == 99))
  352. {
  353. bitfury_init_oldbuf(proc, inp);
  354. goto out;
  355. }
  356. if (opt_debug)
  357. bitfury_debug_nonce_array(proc, "Read", inp);
  358. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  359. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  360. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  361. newjob = inp[0x10];
  362. if (newbuf[0xf] != oldbuf[0xf])
  363. {
  364. inc_hw_errors2(thr, NULL, NULL);
  365. if (unlikely(++bitfury->desync_counter >= 4))
  366. {
  367. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  368. proc->proc_repr);
  369. bitfury_init_oldbuf(proc, inp);
  370. continue;
  371. }
  372. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  373. proc->proc_repr);
  374. goto out;
  375. }
  376. else
  377. bitfury->desync_counter = 0;
  378. if (bitfury->oldjob != newjob && thr->next_work)
  379. {
  380. mt_job_transition(thr);
  381. // TODO: Delay morework until right before it's needed
  382. timer_set_now(&thr->tv_morework);
  383. job_start_complete(thr);
  384. }
  385. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  386. {
  387. if (unlikely(n >= 0xf))
  388. {
  389. inc_hw_errors2(thr, NULL, NULL);
  390. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  391. proc->proc_repr);
  392. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  393. bitfury->desync_counter = 99;
  394. goto out;
  395. }
  396. }
  397. counter = bitfury_decnonce(newbuf[n]);
  398. if ((counter & 0xFFC00000) == 0xdf800000)
  399. {
  400. counter &= 0x003fffff;
  401. int32_t cycles = counter - bitfury->counter1;
  402. if (cycles < 0)
  403. cycles += 0x00400000;
  404. if (cycles & 0x00200000)
  405. {
  406. long long unsigned int period;
  407. double ns;
  408. struct timeval d_time;
  409. timersub(&(tv_now), &(bitfury->timer1), &d_time);
  410. period = timeval_to_us(&d_time) * 1000ULL;
  411. ns = (double)period / (double)(cycles);
  412. bitfury->mhz = 1.0 / ns * 65.0 * 1000.0;
  413. bitfury->counter1 = counter;
  414. copy_time(&(bitfury->timer1), &tv_now);
  415. }
  416. }
  417. if (tvp_stat->tv_sec == 0 && tvp_stat->tv_usec == 0) {
  418. copy_time(tvp_stat, &tv_now);
  419. }
  420. if (c->osc6_max)
  421. {
  422. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  423. {
  424. double mh_diff, s_diff;
  425. const int osc = bitfury->osc6_bits;
  426. // Copy current statistics
  427. mh_diff = bitfury->counter2 - c->omh;
  428. s_diff = total_secs - c->os;
  429. applog(LOG_DEBUG, "%"PRIpreprv": %.0f completed in %f seconds",
  430. proc->proc_repr, mh_diff, s_diff);
  431. if (osc >= c->osc6_min && osc <= c->osc6_max)
  432. {
  433. c->mh[osc] += mh_diff;
  434. c->s[osc] += s_diff;
  435. }
  436. c->omh = bitfury->counter2;
  437. c->os = total_secs;
  438. if (opt_debug && !c->best_done)
  439. {
  440. char logbuf[0x100];
  441. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  442. tailsprintf(logbuf, sizeof(logbuf), " %d=%.3f/%3.0fs",
  443. i, c->mh[i] / c->s[i], c->s[i]);
  444. applog(LOG_DEBUG, "%"PRIpreprv":%s",
  445. proc->proc_repr, logbuf);
  446. }
  447. // Change freq;
  448. if (!c->best_done) {
  449. bitfury_select_freq(bitfury, proc);
  450. } else {
  451. applog(LOG_DEBUG, "%"PRIpreprv": Stable freq, osc6_bits: %d",
  452. proc->proc_repr, bitfury->osc6_bits);
  453. }
  454. }
  455. }
  456. if (n)
  457. {
  458. for (i = 0; i < n; ++i)
  459. {
  460. nonce = bitfury_decnonce(newbuf[i]);
  461. if (fudge_nonce(thr->work, &nonce))
  462. {
  463. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  464. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  465. submit_nonce(thr, thr->work, nonce);
  466. bitfury->counter2 += 1;
  467. }
  468. else
  469. if (fudge_nonce(thr->prev_work, &nonce))
  470. {
  471. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  472. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  473. submit_nonce(thr, thr->prev_work, nonce);
  474. bitfury->counter2 += 1;
  475. }
  476. else
  477. {
  478. inc_hw_errors(thr, thr->work, nonce);
  479. ++bitfury->sample_hwe;
  480. bitfury->strange_counter += 1;
  481. }
  482. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  483. {
  484. if (bitfury->sample_hwe >= 8)
  485. {
  486. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  487. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  488. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  489. bitfury->desync_counter = 99;
  490. }
  491. bitfury->sample_tot = bitfury->sample_hwe = 0;
  492. }
  493. }
  494. bitfury->active = (bitfury->active + n) % 0x10;
  495. }
  496. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  497. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  498. bitfury->oldjob = newjob;
  499. out:
  500. if (unlikely(bitfury->force_reinit))
  501. {
  502. applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
  503. proc->proc_repr);
  504. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  505. bitfury->desync_counter = 99;
  506. bitfury->force_reinit = false;
  507. }
  508. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  509. copy_time(tvp_stat, &tv_now);
  510. }
  511. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  512. }
  513. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  514. {
  515. // Bitfury chips process only 768/1024 of the nonce range
  516. return 0xbd000000;
  517. }
  518. struct api_data *bitfury_api_device_detail(struct cgpu_info * const cgpu)
  519. {
  520. struct bitfury_device * const bitfury = cgpu->device_data;
  521. struct api_data *root = NULL;
  522. root = api_add_uint(root, "fasync", &bitfury->fasync, false);
  523. return root;
  524. }
  525. struct api_data *bitfury_api_device_status(struct cgpu_info * const cgpu)
  526. {
  527. struct bitfury_device * const bitfury = cgpu->device_data;
  528. struct api_data *root = NULL;
  529. int clock_bits = bitfury->osc6_bits;
  530. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  531. root = api_add_freq(root, "Frequency", &bitfury->mhz, false);
  532. return root;
  533. }
  534. static
  535. bool _bitfury_set_device_parse_setting(uint32_t * const rv, char * const setting, char * const replybuf, const int maxval)
  536. {
  537. char *p;
  538. long int nv;
  539. if (!setting || !*setting)
  540. {
  541. sprintf(replybuf, "missing setting");
  542. return false;
  543. }
  544. nv = strtol(setting, &p, 0);
  545. if (nv > maxval || nv < 1)
  546. {
  547. sprintf(replybuf, "invalid setting");
  548. return false;
  549. }
  550. *rv = nv;
  551. return true;
  552. }
  553. char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  554. {
  555. struct bitfury_device * const bitfury = proc->device_data;
  556. uint32_t newval;
  557. if (!strcasecmp(option, "help"))
  558. {
  559. sprintf(replybuf, "baud: SPI baud rate\nosc6_bits: range 1-%d (slow to fast)", BITFURY_MAX_OSC6_BITS);
  560. return replybuf;
  561. }
  562. if (!strcasecmp(option, "baud"))
  563. {
  564. if (!_bitfury_set_device_parse_setting(&bitfury->spi->speed, setting, replybuf, INT_MAX))
  565. return replybuf;
  566. return NULL;
  567. }
  568. if (!strcasecmp(option, "osc6_bits"))
  569. {
  570. struct freq_stat * const c = &bitfury->chip_stat;
  571. newval = bitfury->osc6_bits;
  572. if (!_bitfury_set_device_parse_setting(&newval, setting, replybuf, BITFURY_MAX_OSC6_BITS))
  573. return replybuf;
  574. bitfury->osc6_bits = newval;
  575. bitfury->force_reinit = true;
  576. c->osc6_max = 0;
  577. return NULL;
  578. }
  579. sprintf(replybuf, "Unknown option: %s", option);
  580. return replybuf;
  581. }
  582. #ifdef HAVE_CURSES
  583. void bitfury_tui_wlogprint_choices(struct cgpu_info *cgpu)
  584. {
  585. wlogprint("[O]scillator bits ");
  586. }
  587. const char *bitfury_tui_handle_choice(struct cgpu_info *cgpu, int input)
  588. {
  589. struct bitfury_device * const bitfury = cgpu->device_data;
  590. char buf[0x100];
  591. switch (input)
  592. {
  593. case 'o': case 'O':
  594. {
  595. struct freq_stat * const c = &bitfury->chip_stat;
  596. int val;
  597. char *intvar;
  598. sprintf(buf, "Set oscillator bits (range 1-%d; slow to fast)", BITFURY_MAX_OSC6_BITS);
  599. intvar = curses_input(buf);
  600. if (!intvar)
  601. return "Invalid oscillator bits\n";
  602. val = atoi(intvar);
  603. free(intvar);
  604. if (val < 1 || val > BITFURY_MAX_OSC6_BITS)
  605. return "Invalid oscillator bits\n";
  606. bitfury->osc6_bits = val;
  607. bitfury->force_reinit = true;
  608. c->osc6_max = 0;
  609. return "Oscillator bits changing\n";
  610. }
  611. }
  612. return NULL;
  613. }
  614. void bitfury_wlogprint_status(struct cgpu_info *cgpu)
  615. {
  616. struct bitfury_device * const bitfury = cgpu->device_data;
  617. wlogprint("Oscillator bits: %d\n", bitfury->osc6_bits);
  618. }
  619. #endif
  620. struct device_drv bitfury_drv = {
  621. .dname = "bitfury_gpio",
  622. .name = "BFY",
  623. .drv_detect = bitfury_detect,
  624. .thread_init = bitfury_init,
  625. .thread_shutdown = bitfury_shutdown,
  626. .minerloop = minerloop_async,
  627. .job_prepare = bitfury_job_prepare,
  628. .job_start = bitfury_noop_job_start,
  629. .poll = bitfury_do_io,
  630. .job_process_results = bitfury_job_process_results,
  631. .get_api_extra_device_detail = bitfury_api_device_detail,
  632. .get_api_extra_device_status = bitfury_api_device_status,
  633. .set_device = bitfury_set_device,
  634. #ifdef HAVE_CURSES
  635. .proc_wlogprint_status = bitfury_wlogprint_status,
  636. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  637. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  638. #endif
  639. };