driver-drillbit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. intptr_t caps = buf[0xe] | ((intptr_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 == 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, LOG_ERR, "%s: %s: Device firmware uses newer Drillbit protocol %u. We only support up to %u. Find a newer BFGMiner!",
  96. __func__, devpath, protover, (unsigned)DRILLBIT_MAX_VERSION);
  97. return false;
  98. }
  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. caps |= ((intptr_t)protover) << 16; // Store protocol version in capability field, temporarily
  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. 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 *)(intptr_t)caps,
  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. unsigned caps = (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 = caps,
  251. .protover = caps >> 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. if (1 != write(fd, "E", 1))
  363. problem(false, LOG_ERR, "%s: Error sending request for work results", dev->dev_repr);
  364. if (sizeof(total) != serial_read(fd, &total, sizeof(total)))
  365. problem(false, LOG_ERR, "%s: Short read in response to 'E'", dev->dev_repr);
  366. total = le32toh(total);
  367. if (total > DRILLBIT_MAX_WORK_RESULTS)
  368. problem(false, LOG_ERR, "%s: Impossible number of total work: %lu",
  369. dev->dev_repr, (unsigned long)total);
  370. for (i = 0; i < total; ++i)
  371. {
  372. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  373. problem(false, LOG_ERR, "%s: Short read on %dth total work",
  374. dev->dev_repr, i);
  375. const int chipid = buf[0];
  376. struct cgpu_info * const proc = drillbit_find_proc(dev, chipid);
  377. struct thr_info * const thr = proc->thr[0];
  378. if (unlikely(!proc))
  379. {
  380. applog(LOG_ERR, "%s: Unknown chip id %d", dev->dev_repr, chipid);
  381. continue;
  382. }
  383. const bool is_idle = buf[3];
  384. int nonces = buf[2];
  385. if (nonces > DRILLBIT_MAX_RESULT_NONCES)
  386. {
  387. applog(LOG_ERR, "%"PRIpreprv": More than %d nonces claimed, impossible",
  388. proc->proc_repr, (int)DRILLBIT_MAX_RESULT_NONCES);
  389. nonces = DRILLBIT_MAX_RESULT_NONCES;
  390. }
  391. applog(LOG_DEBUG, "%"PRIpreprv": Handling completion of %d nonces. is_idle=%d work=%p next_work=%p",
  392. proc->proc_repr, nonces, is_idle, thr->work, thr->next_work);
  393. const uint32_t *nonce_p = (void*)&buf[4];
  394. for (j = 0; j < nonces; ++j, ++nonce_p)
  395. {
  396. uint32_t nonce = bitfury_decnonce(*nonce_p);
  397. if (bitfury_fudge_nonce2(thr->work, &nonce))
  398. submit_nonce(thr, thr->work, nonce);
  399. else
  400. if (bitfury_fudge_nonce2(thr->next_work, &nonce))
  401. {
  402. applog(LOG_DEBUG, "%"PRIpreprv": Result for next work, transitioning",
  403. proc->proc_repr);
  404. submit_nonce(thr, thr->next_work, nonce);
  405. mt_job_transition(thr);
  406. job_start_complete(thr);
  407. }
  408. else
  409. if (bitfury_fudge_nonce2(thr->prev_work, &nonce))
  410. {
  411. applog(LOG_DEBUG, "%"PRIpreprv": Result for PREVIOUS work",
  412. proc->proc_repr);
  413. submit_nonce(thr, thr->prev_work, nonce);
  414. }
  415. else
  416. inc_hw_errors(thr, thr->work, nonce);
  417. }
  418. if (is_idle && thr->next_work)
  419. {
  420. applog(LOG_DEBUG, "%"PRIpreprv": Chip went idle without any results for next work",
  421. proc->proc_repr);
  422. mt_job_transition(thr);
  423. job_start_complete(thr);
  424. }
  425. if (!thr->next_work)
  426. timer_set_now(&thr->tv_morework);
  427. }
  428. return true;
  429. }
  430. static
  431. void drillbit_poll(struct thr_info * const master_thr)
  432. {
  433. struct cgpu_info * const dev = master_thr->cgpu;
  434. struct drillbit_board * const board = dev->device_data;
  435. if (!drillbit_ensure_configured(dev))
  436. return;
  437. drillbit_get_work_results(dev);
  438. if (board->need_reinit)
  439. {
  440. applog(LOG_NOTICE, "%s: Reinitialisation needed for configuration changes",
  441. dev->dev_repr);
  442. drillbit_reconfigure(dev, false);
  443. board->need_reinit = false;
  444. }
  445. if (board->trigger_identify)
  446. {
  447. const int fd = dev->device_fd;
  448. applog(LOG_DEBUG, "%s: Sending identify command", dev->dev_repr);
  449. if (1 != write(fd, "L", 1))
  450. applog(LOG_ERR, "%s: Error writing identify command", dev->dev_repr);
  451. drillbit_check_response(dev->dev_repr, fd, dev, 'L');
  452. board->trigger_identify = false;
  453. }
  454. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  455. }
  456. static bool drillbit_identify(struct cgpu_info * const proc)
  457. {
  458. struct cgpu_info * const dev = proc->device;
  459. struct drillbit_board * const board = dev->device_data;
  460. board->trigger_identify = true;
  461. return true;
  462. }
  463. static
  464. bool drillbit_get_stats(struct cgpu_info * const dev)
  465. {
  466. if (dev != dev->device)
  467. return true;
  468. struct drillbit_board * const board = dev->device_data;
  469. if (!(board->caps & DBC_TEMP))
  470. return true;
  471. const int fd = dev->device_fd;
  472. if (fd == -1)
  473. return false;
  474. if (1 != write(fd, "T", 1))
  475. problem(false, LOG_ERR, "%s: Error requesting temperature", dev->dev_repr);
  476. uint8_t buf[2];
  477. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  478. problem(false, LOG_ERR, "%s: Short read in response to 'T'", dev->dev_repr);
  479. float temp = ((uint16_t)buf[0]) | ((uint16_t)buf[1] << 8);
  480. temp /= 10.;
  481. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  482. proc->temp = temp;
  483. return true;
  484. }
  485. static
  486. void drillbit_clockcfg_str(char * const buf, size_t bufsz, struct drillbit_board * const board)
  487. {
  488. snprintf(buf, bufsz, "%u", board->clock_freq);
  489. if (board->clock_div2)
  490. tailsprintf(buf, bufsz, ":2");
  491. }
  492. static
  493. struct api_data *drillbit_api_stats(struct cgpu_info * const proc)
  494. {
  495. struct cgpu_info * const dev = proc->device;
  496. struct drillbit_board * const board = dev->device_data;
  497. struct api_data *root = NULL;
  498. char buf[0x100];
  499. drillbit_clockcfg_str(buf, sizeof(buf), board);
  500. root = api_add_string(root, "ClockCfg", buf, true);
  501. float volts = board->core_voltage / 1000.0;
  502. root = api_add_volts(root, "Voltage", &volts, true);
  503. return root;
  504. }
  505. static
  506. char *drillbit_set_device(struct cgpu_info * const proc, char * const option, char *setting, char * const replybuf)
  507. {
  508. struct cgpu_info * const dev = proc->device;
  509. struct drillbit_board * const board = dev->device_data;
  510. if (!strcasecmp(option, "help"))
  511. {
  512. sprintf(replybuf,
  513. "voltage: 0.65, 0.75, 0.85, or 0.95 (volts)\n"
  514. "clock: %sL0-L63 for internal clock levels; append :2 to activate div2",
  515. (board->caps & DBC_EXT_CLOCK) ? "0-255 (MHz) using external clock (80-230 recommended), or " : ""
  516. );
  517. return replybuf;
  518. }
  519. if (!strcasecmp(option, "voltage"))
  520. {
  521. // NOTE: Do not use replybuf in here without implementing it in drillbit_tui_handle_choice
  522. if (!setting || !*setting)
  523. return "Missing voltage setting";
  524. const int val = atof(setting) * 1000;
  525. board->core_voltage = val;
  526. board->need_reinit = true;
  527. return NULL;
  528. }
  529. if (!strcasecmp(option, "clock"))
  530. {
  531. // NOTE: Do not use replybuf in here without implementing it in drillbit_tui_handle_choice
  532. const bool use_ext_clock = !(setting[0] == 'L');
  533. char *end = &setting[use_ext_clock ? 0 : 1];
  534. const long int num = strtol(end, &end, 0);
  535. const bool div2 = (end[0] == ':' && end[1] == '2');
  536. // NOTE: board assignments are ordered such that it is safe to race
  537. if (use_ext_clock)
  538. {
  539. if (!(board->caps & DBC_EXT_CLOCK))
  540. return "External clock not supported by this device";
  541. board->use_ext_clock = true;
  542. }
  543. if (num < 0 || num > 0xffff)
  544. return "Clock frequency out of range (0-65535)";
  545. board->clock_div2 = div2;
  546. board->clock_freq = num;
  547. board->need_reinit = true;
  548. return NULL;
  549. }
  550. sprintf(replybuf, "Unknown option: %s", option);
  551. return replybuf;
  552. }
  553. #ifdef HAVE_CURSES
  554. static
  555. void drillbit_tui_wlogprint_choices(struct cgpu_info * const proc)
  556. {
  557. wlogprint("[C]lock [V]oltage ");
  558. }
  559. static
  560. const char *drillbit_tui_handle_choice(struct cgpu_info * const proc, const int input)
  561. {
  562. char *val;
  563. switch (input)
  564. {
  565. case 'c': case 'C':
  566. val = curses_input("Set clock (80-230 MHz using external clock, or L0-L63 for internal clock levels; append :2 to activate div2");
  567. return drillbit_set_device(proc, "clock", val, NULL) ?: "Requesting clock change";
  568. case 'v': case 'V':
  569. val = curses_input("Set voltage (0.65, 0.75, 0.85, or 0.95)");
  570. return drillbit_set_device(proc, "voltage", val, NULL) ?: "Requesting voltage change";
  571. }
  572. return NULL;
  573. }
  574. static
  575. void drillbit_wlogprint_status(struct cgpu_info * const proc)
  576. {
  577. struct cgpu_info * const dev = proc->device;
  578. struct drillbit_board * const board = dev->device_data;
  579. char buf[0x100];
  580. drillbit_clockcfg_str(buf, sizeof(buf), board);
  581. wlogprint("Clock: %s\n", buf);
  582. wlogprint("Voltage: %.2f\n", board->core_voltage / 1000.0);
  583. }
  584. #endif
  585. struct device_drv drillbit_drv = {
  586. .dname = "drillbit",
  587. .name = "DRB",
  588. .lowl_match = drillbit_lowl_match,
  589. .lowl_probe = drillbit_lowl_probe,
  590. .thread_init = drillbit_init,
  591. .minerloop = minerloop_async,
  592. .job_prepare = drillbit_job_prepare,
  593. .job_start = drillbit_first_job_start,
  594. .job_process_results = drillbit_job_process_results,
  595. .poll = drillbit_poll,
  596. .get_stats = drillbit_get_stats,
  597. .identify_device = drillbit_identify,
  598. .get_api_stats = drillbit_api_stats,
  599. .set_device = drillbit_set_device,
  600. #ifdef HAVE_CURSES
  601. .proc_wlogprint_status = drillbit_wlogprint_status,
  602. .proc_tui_wlogprint_choices = drillbit_tui_wlogprint_choices,
  603. .proc_tui_handle_choice = drillbit_tui_handle_choice,
  604. #endif
  605. };