driver-drillbit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2013 Angus Gratton
  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 <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "deviceapi.h"
  15. #include "logging.h"
  16. #include "lowlevel.h"
  17. #include "lowl-vcom.h"
  18. BFG_REGISTER_DRIVER(drillbit_drv)
  19. #define DRILLBIT_MIN_VERSION 2
  20. #define DRILLBIT_MAX_VERSION 3
  21. #define DRILLBIT_MAX_WORK_RESULTS 0x400
  22. #define DRILLBIT_MAX_RESULT_NONCES 0x10
  23. enum drillbit_capability {
  24. DBC_TEMP = 1,
  25. DBC_EXT_CLOCK = 2,
  26. };
  27. enum drillbit_voltagecfg {
  28. DBV_650mV = 0,
  29. DBV_750mV = 2,
  30. DBV_850mV = 1,
  31. DBV_950mV = 3,
  32. };
  33. struct drillbit_board {
  34. enum drillbit_voltagecfg core_voltage_cfg;
  35. unsigned clock_level;
  36. bool clock_div2;
  37. bool use_ext_clock;
  38. unsigned ext_clock_freq;
  39. bool need_reinit;
  40. bool trigger_identify;
  41. uint16_t caps;
  42. };
  43. static
  44. bool drillbit_lowl_match(const struct lowlevel_device_info * const info)
  45. {
  46. if (!lowlevel_match_id(info, &lowl_vcom, 0, 0))
  47. return false;
  48. return (info->manufacturer && strstr(info->manufacturer, "Drillbit"));
  49. }
  50. static
  51. bool drillbit_detect_one(const char * const devpath)
  52. {
  53. uint8_t buf[0x10];
  54. const int fd = serial_open(devpath, 0, 1, true);
  55. if (fd == -1)
  56. applogr(false, LOG_DEBUG, "%s: %s: Failed to open", __func__, devpath);
  57. if (1 != write(fd, "I", 1))
  58. {
  59. applog(LOG_DEBUG, "%s: %s: Error writing 'I'", __func__, devpath);
  60. err:
  61. serial_close(fd);
  62. return false;
  63. }
  64. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  65. {
  66. applog(LOG_DEBUG, "%s: %s: Short read in response to 'I'",
  67. __func__, devpath);
  68. goto err;
  69. }
  70. serial_close(fd);
  71. const unsigned protover = buf[0];
  72. const unsigned long serialno = (uint32_t)buf[9] | ((uint32_t)buf[0xa] << 8) | ((uint32_t)buf[0xb] << 16) | ((uint32_t)buf[0xc] << 24);
  73. char * const product = (void*)&buf[1];
  74. buf[9] = '\0'; // Ensure it is null-terminated (clobbers serial, but we already parsed it)
  75. unsigned chips = buf[0xd];
  76. uint16_t caps = (uint16_t)buf[0xe] | ((uint16_t)buf[0xf] << 8);
  77. if (!product[0])
  78. applogr(false, LOG_DEBUG, "%s: %s: Null product name", __func__, devpath);
  79. if (!serialno)
  80. applogr(false, LOG_DEBUG, "%s: %s: Serial number is zero", __func__, devpath);
  81. if (!chips)
  82. applogr(false, LOG_DEBUG, "%s: %s: No chips found", __func__, devpath);
  83. int loglev = LOG_WARNING;
  84. if (!strcmp(product, "DRILLBIT"))
  85. {
  86. // Hack: first production firmwares all described themselves as DRILLBIT, so fill in the gaps
  87. if (chips == 1)
  88. strcpy(product, "Thumb");
  89. else
  90. strcpy(product, "Eight");
  91. }
  92. else
  93. if (chips == 8 && !strcmp(product, "Eight"))
  94. {} // Known device
  95. else
  96. if (chips == 1 && !strcmp(product, "Thumb"))
  97. {} // Known device
  98. else
  99. loglev = LOG_DEBUG;
  100. if (protover < DRILLBIT_MIN_VERSION || (loglev == LOG_DEBUG && protover > DRILLBIT_MAX_VERSION))
  101. applogr(false, loglev, "%s: %s: Unknown device protocol version %u.",
  102. __func__, devpath, protover);
  103. if (protover > DRILLBIT_MAX_VERSION)
  104. applogr(false, loglev, "%s: %s: Device firmware uses newer Drillbit protocol %u. We only support up to %u. Find a newer BFGMiner!",
  105. __func__, devpath, protover, (unsigned)DRILLBIT_MAX_VERSION);
  106. if (protover == 2 && chips == 1)
  107. // Production firmware Thumbs don't set any capability bits, so fill in the EXT_CLOCK one
  108. caps |= DBC_EXT_CLOCK;
  109. char *serno = malloc(9);
  110. snprintf(serno, 9, "%08lx", serialno);
  111. if (chips > 0x100)
  112. {
  113. applog(LOG_WARNING, "%s: %s: %u chips reported, but driver only supports up to 256",
  114. __func__, devpath, chips);
  115. chips = 0x100;
  116. }
  117. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  118. *cgpu = (struct cgpu_info){
  119. .drv = &drillbit_drv,
  120. .device_path = strdup(devpath),
  121. .dev_product = strdup(product),
  122. .dev_serial = serno,
  123. .deven = DEV_ENABLED,
  124. .procs = chips,
  125. .threads = 1,
  126. .device_data = (void*)(intptr_t)caps,
  127. };
  128. return add_cgpu(cgpu);
  129. }
  130. static
  131. bool drillbit_lowl_probe(const struct lowlevel_device_info * const info)
  132. {
  133. return vcom_lowl_probe_wrapper(info, drillbit_detect_one);
  134. }
  135. static
  136. void drillbit_problem(struct cgpu_info * const dev)
  137. {
  138. struct thr_info * const master_thr = dev->thr[0];
  139. if (dev->device_fd != -1)
  140. {
  141. serial_close(dev->device_fd);
  142. dev->device_fd = -1;
  143. }
  144. timer_set_delay_from_now(&master_thr->tv_poll, 5000000);
  145. }
  146. #define problem(...) do{ \
  147. drillbit_problem(dev); \
  148. applogr(__VA_ARGS__); \
  149. }while(0)
  150. static
  151. bool drillbit_check_response(const char * const repr, const int fd, struct cgpu_info * const dev, const char expect)
  152. {
  153. uint8_t ack;
  154. if (1 != serial_read(fd, &ack, 1))
  155. problem(false, LOG_ERR, "%s: Short read in response to '%c'",
  156. repr, expect);
  157. if (ack != expect)
  158. problem(false, LOG_ERR, "%s: Wrong response to '%c': %u",
  159. dev->dev_repr, expect, (unsigned)ack);
  160. return true;
  161. }
  162. static
  163. bool drillbit_reset(struct cgpu_info * const dev)
  164. {
  165. const int fd = dev->device_fd;
  166. if (unlikely(fd == -1))
  167. return false;
  168. if (1 != write(fd, "R", 1))
  169. problem(false, LOG_ERR, "%s: Error writing reset command", dev->dev_repr);
  170. return drillbit_check_response(dev->dev_repr, fd, dev, 'R');
  171. }
  172. static
  173. bool drillbit_send_config(struct cgpu_info * const dev)
  174. {
  175. const int fd = dev->device_fd;
  176. if (unlikely(fd == -1))
  177. return false;
  178. const struct drillbit_board * const board = dev->device_data;
  179. const uint8_t buf[7] = {'C', board->core_voltage_cfg, board->clock_level, (board->clock_div2 ? 1 : 0), (board->use_ext_clock ? 1 : 0), (board->ext_clock_freq & 0xff), (board->ext_clock_freq >> 8)};
  180. if (sizeof(buf) != write(fd, buf, sizeof(buf)))
  181. problem(false, LOG_ERR, "%s: Error sending config", dev->dev_repr);
  182. return drillbit_check_response(dev->dev_repr, fd, dev, 'C');
  183. }
  184. static bool drillbit_resend_jobs(struct cgpu_info *proc);
  185. static
  186. bool drillbit_reconfigure(struct cgpu_info * const dev, const bool reopen)
  187. {
  188. struct thr_info * const master_thr = dev->thr[0];
  189. int fd = dev->device_fd;
  190. if (reopen || fd == -1)
  191. {
  192. if (fd != -1)
  193. serial_close(fd);
  194. dev->device_fd = fd = serial_open(dev->device_path, 0, 10, true);
  195. if (fd == -1)
  196. return false;
  197. }
  198. if (!(drillbit_reset(dev) && drillbit_send_config(dev)))
  199. {
  200. serial_close(fd);
  201. dev->device_fd = -1;
  202. return false;
  203. }
  204. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  205. drillbit_resend_jobs(proc);
  206. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  207. return true;
  208. }
  209. static
  210. bool drillbit_ensure_configured(struct cgpu_info * const dev)
  211. {
  212. if (dev->device_fd != -1)
  213. return true;
  214. return drillbit_reconfigure(dev, false);
  215. }
  216. static
  217. bool drillbit_init(struct thr_info * const master_thr)
  218. {
  219. struct cgpu_info * const dev = master_thr->cgpu;
  220. dev->device_fd = -1;
  221. struct drillbit_board * const board = malloc(sizeof(*board));
  222. *board = (struct drillbit_board){
  223. .core_voltage_cfg = DBV_850mV,
  224. .clock_level = 40,
  225. .clock_div2 = false,
  226. .use_ext_clock = false,
  227. .ext_clock_freq = 200,
  228. .caps = (intptr_t)dev->device_data,
  229. };
  230. dev->device_data = board;
  231. drillbit_reconfigure(dev, false);
  232. return true;
  233. }
  234. static
  235. bool drillbit_job_prepare(struct thr_info * const thr, struct work * const work, __maybe_unused const uint64_t max_nonce)
  236. {
  237. struct cgpu_info * const proc = thr->cgpu;
  238. const int chipid = proc->proc_id;
  239. struct cgpu_info * const dev = proc->device;
  240. uint8_t buf[0x2f];
  241. if (!drillbit_ensure_configured(dev))
  242. return false;
  243. const int fd = dev->device_fd;
  244. buf[0] = 'W';
  245. buf[1] = chipid;
  246. buf[2] = 0; // high bits of chipid
  247. memcpy(&buf[3], work->midstate, 0x20);
  248. memcpy(&buf[0x23], &work->data[0x40], 0xc);
  249. if (sizeof(buf) != write(fd, buf, sizeof(buf)))
  250. problem(false, LOG_ERR, "%"PRIpreprv": Error sending work %d",
  251. proc->proc_repr, work->id);
  252. if (!drillbit_check_response(proc->proc_repr, fd, dev, 'W'))
  253. problem(false, LOG_ERR, "%"PRIpreprv": Error queuing work %d",
  254. proc->proc_repr, work->id);
  255. applog(LOG_DEBUG, "%"PRIpreprv": Queued work %d",
  256. proc->proc_repr, work->id);
  257. work->blk.nonce = 0xffffffff;
  258. return true;
  259. }
  260. static
  261. bool drillbit_resend_jobs(struct cgpu_info * const proc)
  262. {
  263. struct thr_info * const thr = proc->thr[0];
  264. bool rv = true;
  265. if (thr->work)
  266. if (!drillbit_job_prepare(thr, thr->work, 0))
  267. {
  268. applog(LOG_WARNING, "%"PRIpreprv": Failed to resend %s work",
  269. proc->proc_repr, "current");
  270. rv = false;
  271. }
  272. if (thr->next_work)
  273. {
  274. if (!drillbit_job_prepare(thr, thr->next_work, 0))
  275. {
  276. applog(LOG_WARNING, "%"PRIpreprv": Failed to resend %s work",
  277. proc->proc_repr, "next");
  278. rv = false;
  279. }
  280. if (!rv)
  281. {
  282. // Fake transition so we kinda recover eventually
  283. mt_job_transition(thr);
  284. job_start_complete(thr);
  285. timer_set_now(&thr->tv_morework);
  286. }
  287. }
  288. return rv;
  289. }
  290. static
  291. void drillbit_first_job_start(struct thr_info __maybe_unused * const thr)
  292. {
  293. struct cgpu_info * const proc = thr->cgpu;
  294. if (unlikely(!thr->work))
  295. {
  296. applog(LOG_DEBUG, "%"PRIpreprv": No current work, assuming immediate start",
  297. proc->proc_repr);
  298. mt_job_transition(thr);
  299. job_start_complete(thr);
  300. timer_set_now(&thr->tv_morework);
  301. }
  302. }
  303. static
  304. int64_t drillbit_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  305. {
  306. return 0xbd000000;
  307. }
  308. static
  309. struct cgpu_info *drillbit_find_proc(struct cgpu_info * const dev, int chipid)
  310. {
  311. struct cgpu_info *proc = dev;
  312. for (int i = 0; i < chipid; ++i)
  313. {
  314. proc = proc->next_proc;
  315. if (unlikely(!proc))
  316. return NULL;
  317. }
  318. return proc;
  319. }
  320. static
  321. bool bitfury_fudge_nonce2(struct work * const work, uint32_t * const nonce_p)
  322. {
  323. if (!work)
  324. return false;
  325. const uint32_t m7 = *((uint32_t *)&work->data[64]);
  326. const uint32_t ntime = *((uint32_t *)&work->data[68]);
  327. const uint32_t nbits = *((uint32_t *)&work->data[72]);
  328. return bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, nonce_p);
  329. }
  330. static
  331. bool drillbit_get_work_results(struct cgpu_info * const dev)
  332. {
  333. const int fd = dev->device_fd;
  334. if (fd == -1)
  335. return false;
  336. uint8_t buf[4 + (4 * DRILLBIT_MAX_RESULT_NONCES)];
  337. uint32_t total;
  338. int i, j;
  339. if (1 != write(fd, "E", 1))
  340. problem(false, LOG_ERR, "%s: Error sending request for work results", dev->dev_repr);
  341. if (sizeof(total) != serial_read(fd, &total, sizeof(total)))
  342. problem(false, LOG_ERR, "%s: Short read in response to 'E'", dev->dev_repr);
  343. total = le32toh(total);
  344. if (total > DRILLBIT_MAX_WORK_RESULTS)
  345. problem(false, LOG_ERR, "%s: Impossible number of total work: %lu",
  346. dev->dev_repr, (unsigned long)total);
  347. for (i = 0; i < total; ++i)
  348. {
  349. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  350. problem(false, LOG_ERR, "%s: Short read on %dth total work",
  351. dev->dev_repr, i);
  352. const int chipid = buf[0];
  353. struct cgpu_info * const proc = drillbit_find_proc(dev, chipid);
  354. struct thr_info * const thr = proc->thr[0];
  355. if (unlikely(!proc))
  356. {
  357. applog(LOG_ERR, "%s: Unknown chip id %d", dev->dev_repr, chipid);
  358. continue;
  359. }
  360. const bool is_idle = buf[3];
  361. int nonces = buf[2];
  362. if (nonces > DRILLBIT_MAX_RESULT_NONCES)
  363. {
  364. applog(LOG_ERR, "%"PRIpreprv": More than %d nonces claimed, impossible",
  365. proc->proc_repr, (int)DRILLBIT_MAX_RESULT_NONCES);
  366. nonces = DRILLBIT_MAX_RESULT_NONCES;
  367. }
  368. applog(LOG_DEBUG, "%"PRIpreprv": Handling completion of %d nonces. is_idle=%d work=%p next_work=%p",
  369. proc->proc_repr, nonces, is_idle, thr->work, thr->next_work);
  370. const uint32_t *nonce_p = (void*)&buf[4];
  371. for (j = 0; j < nonces; ++j, ++nonce_p)
  372. {
  373. uint32_t nonce = bitfury_decnonce(*nonce_p);
  374. if (bitfury_fudge_nonce2(thr->work, &nonce))
  375. submit_nonce(thr, thr->work, nonce);
  376. else
  377. if (bitfury_fudge_nonce2(thr->next_work, &nonce))
  378. {
  379. applog(LOG_DEBUG, "%"PRIpreprv": Result for next work, transitioning",
  380. proc->proc_repr);
  381. submit_nonce(thr, thr->next_work, nonce);
  382. mt_job_transition(thr);
  383. job_start_complete(thr);
  384. }
  385. else
  386. if (bitfury_fudge_nonce2(thr->prev_work, &nonce))
  387. {
  388. applog(LOG_DEBUG, "%"PRIpreprv": Result for PREVIOUS work",
  389. proc->proc_repr);
  390. submit_nonce(thr, thr->prev_work, nonce);
  391. }
  392. else
  393. inc_hw_errors(thr, thr->work, nonce);
  394. }
  395. if (is_idle && thr->next_work)
  396. {
  397. applog(LOG_DEBUG, "%"PRIpreprv": Chip went idle without any results for next work",
  398. proc->proc_repr);
  399. mt_job_transition(thr);
  400. job_start_complete(thr);
  401. }
  402. if (!thr->next_work)
  403. timer_set_now(&thr->tv_morework);
  404. }
  405. return true;
  406. }
  407. static
  408. void drillbit_poll(struct thr_info * const master_thr)
  409. {
  410. struct cgpu_info * const dev = master_thr->cgpu;
  411. struct drillbit_board * const board = dev->device_data;
  412. if (!drillbit_ensure_configured(dev))
  413. return;
  414. drillbit_get_work_results(dev);
  415. if (board->need_reinit)
  416. {
  417. applog(LOG_NOTICE, "%s: Reinitialisation needed for configuration changes",
  418. dev->dev_repr);
  419. drillbit_reconfigure(dev, false);
  420. board->need_reinit = false;
  421. }
  422. if (board->trigger_identify)
  423. {
  424. const int fd = dev->device_fd;
  425. applog(LOG_DEBUG, "%s: Sending identify command", dev->dev_repr);
  426. if (1 != write(fd, "L", 1))
  427. applog(LOG_ERR, "%s: Error writing identify command", dev->dev_repr);
  428. drillbit_check_response(dev->dev_repr, fd, dev, 'L');
  429. board->trigger_identify = false;
  430. }
  431. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  432. }
  433. static bool drillbit_identify(struct cgpu_info * const proc)
  434. {
  435. struct cgpu_info * const dev = proc->device;
  436. struct drillbit_board * const board = dev->device_data;
  437. board->trigger_identify = true;
  438. return true;
  439. }
  440. static
  441. bool drillbit_get_stats(struct cgpu_info * const dev)
  442. {
  443. if (dev != dev->device)
  444. return true;
  445. struct drillbit_board * const board = dev->device_data;
  446. if (!(board->caps & DBC_TEMP))
  447. return true;
  448. const int fd = dev->device_fd;
  449. if (fd == -1)
  450. return false;
  451. if (1 != write(fd, "T", 1))
  452. problem(false, LOG_ERR, "%s: Error requesting temperature", dev->dev_repr);
  453. uint8_t buf[2];
  454. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  455. problem(false, LOG_ERR, "%s: Short read in response to 'T'", dev->dev_repr);
  456. float temp = ((uint16_t)buf[0]) | ((uint16_t)buf[1] << 8);
  457. temp /= 10.;
  458. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  459. proc->temp = temp;
  460. return true;
  461. }
  462. static
  463. float drillbit_voltagecfg_volts(const enum drillbit_voltagecfg vcfg)
  464. {
  465. switch (vcfg)
  466. {
  467. case DBV_650mV: return 0.65;
  468. case DBV_750mV: return 0.75;
  469. case DBV_850mV: return 0.85;
  470. case DBV_950mV: return 0.95;
  471. }
  472. return 0;
  473. }
  474. static
  475. void drillbit_clockcfg_str(char * const buf, size_t bufsz, struct drillbit_board * const board)
  476. {
  477. if (board->use_ext_clock)
  478. snprintf(buf, bufsz, "%u", board->ext_clock_freq);
  479. else
  480. snprintf(buf, bufsz, "L%u", board->clock_level);
  481. if (board->clock_div2)
  482. tailsprintf(buf, bufsz, ":2");
  483. }
  484. static
  485. struct api_data *drillbit_api_stats(struct cgpu_info * const proc)
  486. {
  487. struct cgpu_info * const dev = proc->device;
  488. struct drillbit_board * const board = dev->device_data;
  489. struct api_data *root = NULL;
  490. char buf[0x100];
  491. drillbit_clockcfg_str(buf, sizeof(buf), board);
  492. root = api_add_string(root, "ClockCfg", buf, true);
  493. float volts = drillbit_voltagecfg_volts(board->core_voltage_cfg);
  494. root = api_add_volts(root, "Voltage", &volts, true);
  495. return root;
  496. }
  497. static
  498. char *drillbit_set_device(struct cgpu_info * const proc, char * const option, char *setting, char * const replybuf)
  499. {
  500. struct cgpu_info * const dev = proc->device;
  501. struct drillbit_board * const board = dev->device_data;
  502. if (!strcasecmp(option, "help"))
  503. {
  504. sprintf(replybuf,
  505. "voltage: 0.65, 0.75, 0.85, or 0.95 (volts)\n"
  506. "clock: %sL0-L63 for internal clock levels; append :2 to activate div2",
  507. (board->caps & DBC_EXT_CLOCK) ? "0-255 (MHz) using external clock (80-230 recommended), or " : ""
  508. );
  509. return replybuf;
  510. }
  511. if (!strcasecmp(option, "voltage"))
  512. {
  513. // NOTE: Do not use replybuf in here without implementing it in drillbit_tui_handle_choice
  514. if (!setting || !*setting)
  515. return "Missing voltage setting";
  516. const int val = atof(setting) * 1000;
  517. enum drillbit_voltagecfg vcfg;
  518. switch (val)
  519. {
  520. case 650: case 649:
  521. vcfg = DBV_650mV;
  522. break;
  523. case 750: case 749:
  524. vcfg = DBV_750mV;
  525. break;
  526. case 850: case 849:
  527. vcfg = DBV_850mV;
  528. break;
  529. case 950: case 949:
  530. vcfg = DBV_950mV;
  531. break;
  532. default:
  533. return "Invalid voltage value";
  534. }
  535. board->core_voltage_cfg = vcfg;
  536. board->need_reinit = true;
  537. return NULL;
  538. }
  539. if (!strcasecmp(option, "clock"))
  540. {
  541. // NOTE: Do not use replybuf in here without implementing it in drillbit_tui_handle_choice
  542. const bool use_ext_clock = !(setting[0] == 'L');
  543. char *end = &setting[use_ext_clock ? 0 : 1];
  544. const unsigned num = strtol(end, &end, 0);
  545. const bool div2 = (end[0] == ':' && end[1] == '2');
  546. // NOTE: board assignments are ordered such that it is safe to race
  547. if (use_ext_clock)
  548. {
  549. if (!(board->caps & DBC_EXT_CLOCK))
  550. return "External clock not supported by this device";
  551. if (num < 0 || num > 0xffff)
  552. return "External clock frequency out of range (0-65535)";
  553. board->clock_div2 = div2;
  554. board->ext_clock_freq = num;
  555. board->use_ext_clock = true;
  556. }
  557. else
  558. {
  559. if (num < 0 || num > 63)
  560. return "Internal clock level out of range (0-63)";
  561. board->clock_div2 = div2;
  562. board->clock_level = num;
  563. board->use_ext_clock = false;
  564. }
  565. board->need_reinit = true;
  566. return NULL;
  567. }
  568. sprintf(replybuf, "Unknown option: %s", option);
  569. return replybuf;
  570. }
  571. #ifdef HAVE_CURSES
  572. static
  573. void drillbit_tui_wlogprint_choices(struct cgpu_info * const proc)
  574. {
  575. wlogprint("[C]lock [V]oltage ");
  576. }
  577. static
  578. const char *drillbit_tui_handle_choice(struct cgpu_info * const proc, const int input)
  579. {
  580. char *val;
  581. switch (input)
  582. {
  583. case 'c': case 'C':
  584. val = curses_input("Set clock (80-230 MHz using external clock, or L0-L63 for internal clock levels; append :2 to activate div2");
  585. return drillbit_set_device(proc, "clock", val, NULL) ?: "Requesting clock change";
  586. case 'v': case 'V':
  587. val = curses_input("Set voltage (0.65, 0.75, 0.85, or 0.95)");
  588. return drillbit_set_device(proc, "voltage", val, NULL) ?: "Requesting voltage change";
  589. }
  590. return NULL;
  591. }
  592. static
  593. void drillbit_wlogprint_status(struct cgpu_info * const proc)
  594. {
  595. struct cgpu_info * const dev = proc->device;
  596. struct drillbit_board * const board = dev->device_data;
  597. char buf[0x100];
  598. drillbit_clockcfg_str(buf, sizeof(buf), board);
  599. wlogprint("Clock: %s\n", buf);
  600. wlogprint("Voltage: %.2f\n", drillbit_voltagecfg_volts(board->core_voltage_cfg));
  601. }
  602. #endif
  603. struct device_drv drillbit_drv = {
  604. .dname = "drillbit",
  605. .name = "DRB",
  606. .lowl_match = drillbit_lowl_match,
  607. .lowl_probe = drillbit_lowl_probe,
  608. .thread_init = drillbit_init,
  609. .minerloop = minerloop_async,
  610. .job_prepare = drillbit_job_prepare,
  611. .job_start = drillbit_first_job_start,
  612. .job_process_results = drillbit_job_process_results,
  613. .poll = drillbit_poll,
  614. .get_stats = drillbit_get_stats,
  615. .identify_device = drillbit_identify,
  616. .get_api_stats = drillbit_api_stats,
  617. .set_device = drillbit_set_device,
  618. #ifdef HAVE_CURSES
  619. .proc_wlogprint_status = drillbit_wlogprint_status,
  620. .proc_tui_wlogprint_choices = drillbit_tui_wlogprint_choices,
  621. .proc_tui_handle_choice = drillbit_tui_handle_choice,
  622. #endif
  623. };