driver-drillbit.c 19 KB

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