driver-drillbit.c 19 KB

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