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