driver-drillbit.c 19 KB

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