driver-drillbit.c 19 KB

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