driver-drillbit.c 19 KB

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