driver-hashratio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Copyright 2013-2015 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2012-2014 Xiangfu <xiangfu@openmobilefree.com>
  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 <limits.h>
  12. #include <pthread.h>
  13. #include <stdio.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #include <unistd.h>
  18. #ifndef WIN32
  19. #include <termios.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #ifndef O_CLOEXEC
  23. #define O_CLOEXEC 0
  24. #endif
  25. #else
  26. #include <io.h>
  27. #endif
  28. #include "elist.h"
  29. #include "miner.h"
  30. #include "driver-hashratio.h"
  31. #include "crc.h"
  32. #include "usbutils.h"
  33. static int opt_hashratio_fan_min = HRTO_DEFAULT_FAN_MIN;
  34. static int opt_hashratio_fan_max = HRTO_DEFAULT_FAN_MAX;
  35. static int hashratio_freq = HRTO_DEFAULT_FREQUENCY;
  36. //static int get_fan_pwm(int temp) {
  37. // int pwm;
  38. // uint8_t fan_pwm_arr[] = {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
  39. // 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
  40. // 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
  41. // 30, 37, 49, 61, 73, 85, 88, 91, 94, 97, 100, 100, 100, 100, 100, 100,
  42. // 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  43. // 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  44. // 100, 100, 100, 100, 100, 100, 100};
  45. // if (temp < 0 || temp >= sizeof(fan_pwm_arr)/sizeof(fan_pwm_arr[0]) ||
  46. // fan_pwm_arr[temp] > opt_hashratio_fan_max) {
  47. // return opt_hashratio_fan_max;
  48. // }
  49. // pwm = HRTO_PWM_MAX - fan_pwm_arr[temp] * HRTO_PWM_MAX / 100;
  50. //
  51. // if (pwm < opt_hashratio_fan_min) {
  52. // return opt_hashratio_fan_min;
  53. // }
  54. // if (pwm > opt_hashratio_fan_max) {
  55. // return opt_hashratio_fan_max;
  56. // }
  57. // return pwm;
  58. //}
  59. char *set_hashratio_freq(char *arg)
  60. {
  61. int val, ret;
  62. ret = sscanf(arg, "%d", &val);
  63. if (ret != 1)
  64. return "No values passed to hashratio-freq";
  65. if (val < HRTO_DEFAULT_FREQUENCY_MIN || val > HRTO_DEFAULT_FREQUENCY_MAX)
  66. return "Invalid value passed to hashratio-freq";
  67. hashratio_freq = val;
  68. return NULL;
  69. }
  70. char *set_hashratio_fan(char *arg)
  71. {
  72. int val1, val2, ret;
  73. ret = sscanf(arg, "%d-%d", &val1, &val2);
  74. if (ret < 1)
  75. return "No values passed to hashratio-fan";
  76. if (ret == 1)
  77. val2 = val1;
  78. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  79. return "Invalid value passed to hashratio-fan";
  80. opt_hashratio_fan_min = val1 * HRTO_PWM_MAX / 100;
  81. opt_hashratio_fan_max = val2 * HRTO_PWM_MAX / 100;
  82. return NULL;
  83. }
  84. static int hashratio_init_pkg(struct hashratio_pkg *pkg, uint8_t type,
  85. uint8_t idx, uint8_t cnt)
  86. {
  87. unsigned short crc;
  88. pkg->head[0] = HRTO_H1;
  89. pkg->head[1] = HRTO_H2;
  90. pkg->type = type;
  91. pkg->idx = idx;
  92. pkg->cnt = cnt;
  93. crc = crc16(pkg->data, HRTO_P_DATA_LEN);
  94. pkg->crc[0] = (crc & 0xff00) >> 8;
  95. pkg->crc[1] = crc & 0x00ff;
  96. return 0;
  97. }
  98. static int job_idcmp(uint8_t *job_id, char *pool_job_id)
  99. {
  100. int job_id_len;
  101. unsigned short crc, crc_expect;
  102. if (!pool_job_id)
  103. return 1;
  104. job_id_len = strlen(pool_job_id);
  105. crc_expect = crc16((const unsigned char *)pool_job_id, job_id_len);
  106. crc = job_id[0] << 8 | job_id[1];
  107. if (crc_expect == crc)
  108. return 0;
  109. applog(LOG_DEBUG, "Hashratio: job_id not match! [%04x:%04x (%s)]",
  110. crc, crc_expect, pool_job_id);
  111. return 1;
  112. }
  113. static int decode_pkg(struct thr_info *thr, struct hashratio_ret *ar, uint8_t *pkg)
  114. {
  115. struct cgpu_info *hashratio = thr->cgpu;
  116. struct hashratio_info *info = hashratio->device_data;
  117. struct pool *pool, *real_pool, *pool_stratum = &info->pool;
  118. unsigned int expected_crc;
  119. unsigned int actual_crc;
  120. uint32_t nonce, nonce2, miner;
  121. int pool_no;
  122. uint8_t job_id[4];
  123. int tmp;
  124. int type = HRTO_GETS_ERROR;
  125. memcpy((uint8_t *)ar, pkg, HRTO_READ_SIZE);
  126. // applog(LOG_DEBUG, "pkg.type, hex: %02x, dec: %d", ar->type, ar->type);
  127. if (ar->head[0] == HRTO_H1 && ar->head[1] == HRTO_H2) {
  128. expected_crc = crc16(ar->data, HRTO_P_DATA_LEN);
  129. actual_crc = (ar->crc[0] & 0xff) |
  130. ((ar->crc[1] & 0xff) << 8);
  131. type = ar->type;
  132. applog(LOG_DEBUG, "hashratio: %d: expected crc(%04x), actual_crc(%04x)", type, expected_crc, actual_crc);
  133. if (expected_crc != actual_crc)
  134. goto out;
  135. switch(type) {
  136. case HRTO_P_NONCE:
  137. applog(LOG_DEBUG, "Hashratio: HRTO_P_NONCE");
  138. memcpy(&miner, ar->data + 0, 4);
  139. memcpy(&pool_no, ar->data + 4, 4);
  140. memcpy(&nonce2, ar->data + 8, 4);
  141. /* Calc time ar->data + 12 */
  142. memcpy(&nonce, ar->data + 12, 4);
  143. memcpy(job_id, ar->data + 16, 4);
  144. miner = be32toh(miner);
  145. pool_no = be32toh(pool_no);
  146. if (miner >= HRTO_DEFAULT_MINERS || pool_no >= total_pools || pool_no < 0) {
  147. applog(LOG_DEBUG, "hashratio: Wrong miner/pool/id no %d,%d", miner, pool_no);
  148. break;
  149. } else
  150. info->matching_work[miner]++;
  151. nonce2 = be32toh(nonce2);
  152. nonce = be32toh(nonce);
  153. applog(LOG_DEBUG, "hashratio: Found! [%s] %d:(%08x) (%08x)",
  154. job_id, pool_no, nonce2, nonce);
  155. real_pool = pool = pools[pool_no];
  156. if (job_idcmp(job_id, pool->swork.job_id)) {
  157. if (!job_idcmp(job_id, pool_stratum->swork.job_id)) {
  158. applog(LOG_DEBUG, "Hashratio: Match to previous stratum! (%s)", pool_stratum->swork.job_id);
  159. pool = pool_stratum;
  160. } else {
  161. applog(LOG_DEBUG, "Hashratio Cannot match to any stratum! (%s)", pool->swork.job_id);
  162. break;
  163. }
  164. }
  165. submit_nonce2_nonce(thr, pool, real_pool, nonce2, nonce, 0);
  166. break;
  167. case HRTO_P_STATUS:
  168. applog(LOG_DEBUG, "Hashratio: HRTO_P_STATUS");
  169. memcpy(&tmp, ar->data, 4);
  170. tmp = be32toh(tmp);
  171. info->temp = (tmp & 0x00f0) >> 8;
  172. if (info->temp_max < info->temp) {
  173. info->temp_max = info->temp;
  174. }
  175. // info->temp[1] = tmp & 0xffff;
  176. memcpy(&tmp, ar->data + 4, 4);
  177. tmp = be32toh(tmp);
  178. info->fan[0] = tmp >> 16;
  179. info->fan[1] = tmp & 0xffff;
  180. // local_work
  181. memcpy(&tmp, ar->data + 8, 4);
  182. tmp = be32toh(tmp);
  183. info->local_work = tmp;
  184. info->local_works += tmp;
  185. // hw_work
  186. memcpy(&tmp, ar->data + 12, 4);
  187. tmp = be32toh(tmp);
  188. info->hw_works += tmp;
  189. hashratio->temp = info->temp;
  190. break;
  191. case HRTO_P_ACKDETECT:
  192. applog(LOG_DEBUG, "Hashratio: HRTO_P_ACKDETECT");
  193. break;
  194. case HRTO_P_ACK:
  195. applog(LOG_DEBUG, "Hashratio: HRTO_P_ACK");
  196. break;
  197. case HRTO_P_NAK:
  198. applog(LOG_DEBUG, "Hashratio: HRTO_P_NAK");
  199. break;
  200. default:
  201. applog(LOG_DEBUG, "Hashratio: HRTO_GETS_ERROR");
  202. type = HRTO_GETS_ERROR;
  203. break;
  204. }
  205. }
  206. out:
  207. return type;
  208. }
  209. static inline int hashratio_gets(struct cgpu_info *hashratio, uint8_t *buf)
  210. {
  211. int i;
  212. int read_amount = HRTO_READ_SIZE;
  213. uint8_t buf_tmp[HRTO_READ_SIZE];
  214. uint8_t buf_copy[2 * HRTO_READ_SIZE];
  215. uint8_t *buf_back = buf;
  216. int ret = 0;
  217. while (true) {
  218. int err;
  219. do {
  220. memset(buf, 0, read_amount);
  221. err = usb_read(hashratio, (char *)buf, read_amount, &ret, C_HRO_READ);
  222. if (unlikely(err < 0 || ret != read_amount)) {
  223. applog(LOG_ERR, "hashratio: Error on read in hashratio_gets got %d", ret);
  224. return HRTO_GETS_ERROR;
  225. }
  226. if (likely(ret >= read_amount)) {
  227. for (i = 1; i < read_amount; i++) {
  228. if (buf_back[i - 1] == HRTO_H1 && buf_back[i] == HRTO_H2)
  229. break;
  230. }
  231. i -= 1;
  232. if (i) {
  233. err = usb_read(hashratio, (char *)buf, read_amount, &ret, C_HRO_READ);
  234. if (unlikely(err < 0 || ret != read_amount)) {
  235. applog(LOG_ERR, "hashratio: Error on 2nd read in hashratio_gets got %d", ret);
  236. return HRTO_GETS_ERROR;
  237. }
  238. memcpy(buf_copy, buf_back + i, HRTO_READ_SIZE - i);
  239. memcpy(buf_copy + HRTO_READ_SIZE - i, buf_tmp, i);
  240. memcpy(buf_back, buf_copy, HRTO_READ_SIZE);
  241. }
  242. return HRTO_GETS_OK;
  243. }
  244. buf += ret;
  245. read_amount -= ret;
  246. continue;
  247. } while (ret > 0);
  248. return HRTO_GETS_TIMEOUT;
  249. }
  250. }
  251. static int hashratio_send_pkg(struct cgpu_info *hashratio, const struct hashratio_pkg *pkg)
  252. {
  253. int err, amount;
  254. uint8_t buf[HRTO_WRITE_SIZE];
  255. int nr_len = HRTO_WRITE_SIZE;
  256. memcpy(buf, pkg, HRTO_WRITE_SIZE);
  257. // if (opt_debug) {
  258. // applog(LOG_DEBUG, "hashratio: Sent(%d):", nr_len);
  259. // hexdump((uint8_t *)buf, nr_len);
  260. // }
  261. if (unlikely(hashratio->usbinfo.nodev))
  262. return HRTO_SEND_ERROR;
  263. err = usb_write(hashratio, (char *)buf, nr_len, &amount, C_HRO_WRITE);
  264. if (err || amount != nr_len) {
  265. applog(LOG_DEBUG, "hashratio: Send(%d)!", amount);
  266. return HRTO_SEND_ERROR;
  267. }
  268. return HRTO_SEND_OK;
  269. }
  270. static int hashratio_send_pkgs(struct cgpu_info *hashratio, const struct hashratio_pkg *pkg)
  271. {
  272. int ret;
  273. do {
  274. if (unlikely(hashratio->usbinfo.nodev))
  275. return -1;
  276. ret = hashratio_send_pkg(hashratio, pkg);
  277. } while (ret != HRTO_SEND_OK);
  278. return 0;
  279. }
  280. static void hashratio_stratum_pkgs(struct cgpu_info *hashratio, struct pool *pool)
  281. {
  282. const int merkle_offset = 36;
  283. struct hashratio_pkg pkg;
  284. int i, a, b, tmp;
  285. unsigned char target[32];
  286. int job_id_len;
  287. unsigned short crc;
  288. /* Send out the first stratum message STATIC */
  289. applog(LOG_DEBUG, "hashratio: Pool stratum message STATIC: %d, %d, %d, %d, %d, %d",
  290. pool->coinbase_len,
  291. pool->nonce2_offset,
  292. pool->n2size,
  293. merkle_offset,
  294. pool->merkles,
  295. pool->pool_no);
  296. memset(pkg.data, 0, HRTO_P_DATA_LEN);
  297. tmp = be32toh(pool->coinbase_len);
  298. memcpy(pkg.data, &tmp, 4);
  299. tmp = be32toh(pool->nonce2_offset);
  300. memcpy(pkg.data + 4, &tmp, 4);
  301. tmp = be32toh(pool->n2size);
  302. memcpy(pkg.data + 8, &tmp, 4);
  303. tmp = be32toh(merkle_offset);
  304. memcpy(pkg.data + 12, &tmp, 4);
  305. tmp = be32toh(pool->merkles);
  306. memcpy(pkg.data + 16, &tmp, 4);
  307. tmp = be32toh((int)pool->sdiff);
  308. memcpy(pkg.data + 20, &tmp, 4);
  309. tmp = be32toh((int)pool->pool_no);
  310. memcpy(pkg.data + 24, &tmp, 4);
  311. hashratio_init_pkg(&pkg, HRTO_P_STATIC, 1, 1);
  312. if (hashratio_send_pkgs(hashratio, &pkg))
  313. return;
  314. set_target(target, pool->sdiff);
  315. memcpy(pkg.data, target, 32);
  316. if (opt_debug) {
  317. char *target_str;
  318. target_str = bin2hex(target, 32);
  319. applog(LOG_DEBUG, "hashratio: Pool stratum target: %s", target_str);
  320. free(target_str);
  321. }
  322. hashratio_init_pkg(&pkg, HRTO_P_TARGET, 1, 1);
  323. if (hashratio_send_pkgs(hashratio, &pkg))
  324. return;
  325. applog(LOG_DEBUG, "hashratio: Pool stratum message JOBS_ID: %s",
  326. pool->swork.job_id);
  327. memset(pkg.data, 0, HRTO_P_DATA_LEN);
  328. job_id_len = strlen(pool->swork.job_id);
  329. crc = crc16((const unsigned char *)pool->swork.job_id, job_id_len);
  330. pkg.data[0] = (crc & 0xff00) >> 8;
  331. pkg.data[1] = crc & 0x00ff;
  332. hashratio_init_pkg(&pkg, HRTO_P_JOB_ID, 1, 1);
  333. if (hashratio_send_pkgs(hashratio, &pkg))
  334. return;
  335. a = pool->coinbase_len / HRTO_P_DATA_LEN;
  336. b = pool->coinbase_len % HRTO_P_DATA_LEN;
  337. applog(LOG_DEBUG, "pool->coinbase_len: %d", pool->coinbase_len);
  338. applog(LOG_DEBUG, "hashratio: Pool stratum message COINBASE: %d %d", a, b);
  339. for (i = 0; i < a; i++) {
  340. memcpy(pkg.data, pool->coinbase + i * 32, 32);
  341. hashratio_init_pkg(&pkg, HRTO_P_COINBASE, i + 1, a + (b ? 1 : 0));
  342. if (hashratio_send_pkgs(hashratio, &pkg))
  343. return;
  344. if (i % 25 == 0) {
  345. cgsleep_ms(2);
  346. }
  347. }
  348. if (b) {
  349. memset(pkg.data, 0, HRTO_P_DATA_LEN);
  350. memcpy(pkg.data, pool->coinbase + i * 32, b);
  351. hashratio_init_pkg(&pkg, HRTO_P_COINBASE, i + 1, i + 1);
  352. if (hashratio_send_pkgs(hashratio, &pkg))
  353. return;
  354. }
  355. b = pool->merkles;
  356. applog(LOG_DEBUG, "hashratio: Pool stratum message MERKLES: %d", b);
  357. for (i = 0; i < b; i++) {
  358. memset(pkg.data, 0, HRTO_P_DATA_LEN);
  359. memcpy(pkg.data, pool->swork.merkle_bin[i], 32);
  360. hashratio_init_pkg(&pkg, HRTO_P_MERKLES, i + 1, b);
  361. if (hashratio_send_pkgs(hashratio, &pkg))
  362. return;
  363. }
  364. applog(LOG_DEBUG, "hashratio: Pool stratum message HEADER: 4");
  365. for (i = 0; i < 4; i++) {
  366. memset(pkg.data, 0, HRTO_P_HEADER);
  367. memcpy(pkg.data, pool->header_bin + i * 32, 32);
  368. hashratio_init_pkg(&pkg, HRTO_P_HEADER, i + 1, 4);
  369. if (hashratio_send_pkgs(hashratio, &pkg))
  370. return;
  371. }
  372. }
  373. static int hashratio_get_result(struct thr_info *thr, struct hashratio_ret *ar)
  374. {
  375. struct cgpu_info *hashratio = thr->cgpu;
  376. uint8_t result[HRTO_READ_SIZE];
  377. int ret;
  378. memset(result, 0, HRTO_READ_SIZE);
  379. ret = hashratio_gets(hashratio, result);
  380. if (ret != HRTO_GETS_OK)
  381. return ret;
  382. // if (opt_debug) {
  383. // applog(LOG_DEBUG, "hashratio: Get(ret = %d):", ret);
  384. // hexdump((uint8_t *)result, HRTO_READ_SIZE);
  385. // }
  386. return decode_pkg(thr, ar, result);
  387. }
  388. #define HASHRATIO_LATENCY 5
  389. static void hashratio_initialise(struct cgpu_info *hashratio)
  390. {
  391. int err, interface;
  392. if (hashratio->usbinfo.nodev)
  393. return;
  394. interface = usb_interface(hashratio);
  395. // Reset
  396. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  397. FTDI_VALUE_RESET, interface, C_RESET);
  398. applog(LOG_DEBUG, "%s%i: reset got err %d",
  399. hashratio->drv->name, hashratio->device_id, err);
  400. if (hashratio->usbinfo.nodev)
  401. return;
  402. // Set latency
  403. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  404. HASHRATIO_LATENCY, interface, C_LATENCY);
  405. applog(LOG_DEBUG, "%s%i: latency got err %d",
  406. hashratio->drv->name, hashratio->device_id, err);
  407. if (hashratio->usbinfo.nodev)
  408. return;
  409. // Set data
  410. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  411. FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
  412. applog(LOG_DEBUG, "%s%i: data got err %d",
  413. hashratio->drv->name, hashratio->device_id, err);
  414. if (hashratio->usbinfo.nodev)
  415. return;
  416. // Set the baud
  417. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
  418. (FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
  419. C_SETBAUD);
  420. applog(LOG_DEBUG, "%s%i: setbaud got err %d",
  421. hashratio->drv->name, hashratio->device_id, err);
  422. if (hashratio->usbinfo.nodev)
  423. return;
  424. // Set Modem Control
  425. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  426. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  427. applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
  428. hashratio->drv->name, hashratio->device_id, err);
  429. if (hashratio->usbinfo.nodev)
  430. return;
  431. // Set Flow Control
  432. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  433. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  434. applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
  435. hashratio->drv->name, hashratio->device_id, err);
  436. if (hashratio->usbinfo.nodev)
  437. return;
  438. /* hashratio repeats the following */
  439. // Set Modem Control
  440. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  441. FTDI_VALUE_MODEM, interface, C_SETMODEM);
  442. applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
  443. hashratio->drv->name, hashratio->device_id, err);
  444. if (hashratio->usbinfo.nodev)
  445. return;
  446. // Set Flow Control
  447. err = usb_transfer(hashratio, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  448. FTDI_VALUE_FLOW, interface, C_SETFLOW);
  449. applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
  450. hashratio->drv->name, hashratio->device_id, err);
  451. }
  452. static struct cgpu_info *hashratio_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  453. {
  454. struct hashratio_info *info;
  455. int err, amount;
  456. int ackdetect;
  457. char mm_version[16];
  458. struct cgpu_info *hashratio = usb_alloc_cgpu(&hashratio_drv, 1);
  459. struct hashratio_pkg detect_pkg;
  460. struct hashratio_ret ret_pkg;
  461. if (!usb_init(hashratio, dev, found)) {
  462. applog(LOG_ERR, "Hashratio failed usb_init");
  463. hashratio = usb_free_cgpu(hashratio);
  464. return NULL;
  465. }
  466. hashratio_initialise(hashratio);
  467. strcpy(mm_version, "NONE");
  468. /* Send out detect pkg */
  469. memset(detect_pkg.data, 0, HRTO_P_DATA_LEN);
  470. hashratio_init_pkg(&detect_pkg, HRTO_P_DETECT, 1, 1);
  471. hashratio_send_pkg(hashratio, &detect_pkg);
  472. err = usb_read(hashratio, (char *)&ret_pkg, HRTO_READ_SIZE, &amount, C_HRO_READ);
  473. if (err || amount != HRTO_READ_SIZE) {
  474. applog(LOG_ERR, "%s %d: Hashratio failed usb_read with err %d amount %d",
  475. hashratio->drv->name, hashratio->device_id, err, amount);
  476. usb_uninit(hashratio);
  477. usb_free_cgpu(hashratio);
  478. return NULL;
  479. }
  480. ackdetect = ret_pkg.type;
  481. applog(LOG_DEBUG, "hashratio Detect ID: %d", ackdetect);
  482. if (ackdetect != HRTO_P_ACKDETECT) {
  483. applog(LOG_DEBUG, "Not a hashratio device");
  484. usb_uninit(hashratio);
  485. usb_free_cgpu(hashratio);
  486. return NULL;
  487. }
  488. memcpy(mm_version, ret_pkg.data, 15);
  489. mm_version[15] = '\0';
  490. /* We have a real Hashratio! */
  491. hashratio->threads = HRTO_MINER_THREADS;
  492. add_cgpu(hashratio);
  493. update_usb_stats(hashratio);
  494. applog(LOG_INFO, "%s%d: Found at %s", hashratio->drv->name, hashratio->device_id,
  495. hashratio->device_path);
  496. hashratio->device_data = cgcalloc(sizeof(struct hashratio_info), 1);
  497. info = hashratio->device_data;
  498. strcpy(info->mm_version, mm_version);
  499. info->fan_pwm = HRTO_DEFAULT_FAN / 100 * HRTO_PWM_MAX;
  500. info->temp_max = 0;
  501. info->temp_history_index = 0;
  502. info->temp_sum = 0;
  503. info->temp_old = 0;
  504. info->default_freq = hashratio_freq;
  505. return hashratio;
  506. }
  507. static inline void hashratio_detect(bool __maybe_unused hotplug)
  508. {
  509. usb_detect(&hashratio_drv, hashratio_detect_one);
  510. }
  511. static bool hashratio_prepare(struct thr_info *thr)
  512. {
  513. struct cgpu_info *hashratio = thr->cgpu;
  514. struct hashratio_info *info = hashratio->device_data;
  515. cglock_init(&info->pool.data_lock);
  516. return true;
  517. }
  518. static void copy_pool_stratum(struct hashratio_info *info, struct pool *pool)
  519. {
  520. int i;
  521. int merkles = pool->merkles;
  522. size_t coinbase_len = pool->coinbase_len;
  523. struct pool *pool_stratum = &info->pool;
  524. if (!job_idcmp((uint8_t *)pool->swork.job_id, pool_stratum->swork.job_id))
  525. return;
  526. cg_wlock(&(pool_stratum->data_lock));
  527. free(pool_stratum->swork.job_id);
  528. free(pool_stratum->nonce1);
  529. free(pool_stratum->coinbase);
  530. pool_stratum->coinbase = cgcalloc(coinbase_len, 1);
  531. memcpy(pool_stratum->coinbase, pool->coinbase, coinbase_len);
  532. for (i = 0; i < pool_stratum->merkles; i++)
  533. free(pool_stratum->swork.merkle_bin[i]);
  534. if (merkles) {
  535. pool_stratum->swork.merkle_bin = cgrealloc(pool_stratum->swork.merkle_bin,
  536. sizeof(char *) * merkles + 1);
  537. for (i = 0; i < merkles; i++) {
  538. pool_stratum->swork.merkle_bin[i] = cgmalloc(32);
  539. memcpy(pool_stratum->swork.merkle_bin[i], pool->swork.merkle_bin[i], 32);
  540. }
  541. }
  542. pool_stratum->sdiff = pool->sdiff;
  543. pool_stratum->coinbase_len = pool->coinbase_len;
  544. pool_stratum->nonce2_offset = pool->nonce2_offset;
  545. pool_stratum->n2size = pool->n2size;
  546. pool_stratum->merkles = pool->merkles;
  547. pool_stratum->swork.job_id = strdup(pool->swork.job_id);
  548. pool_stratum->nonce1 = strdup(pool->nonce1);
  549. memcpy(pool_stratum->ntime, pool->ntime, sizeof(pool_stratum->ntime));
  550. memcpy(pool_stratum->header_bin, pool->header_bin, sizeof(pool_stratum->header_bin));
  551. cg_wunlock(&(pool_stratum->data_lock));
  552. }
  553. static void hashratio_update_work(struct cgpu_info *hashratio)
  554. {
  555. struct hashratio_info *info = hashratio->device_data;
  556. struct thr_info *thr = hashratio->thr[0];
  557. struct hashratio_pkg send_pkg;
  558. uint32_t tmp, range, start;
  559. struct work *work;
  560. struct pool *pool;
  561. applog(LOG_DEBUG, "hashratio: New stratum: restart: %d, update: %d",
  562. thr->work_restart, thr->work_update);
  563. thr->work_update = false;
  564. thr->work_restart = false;
  565. work = get_work(thr, thr->id); /* Make sure pool is ready */
  566. discard_work(work); /* Don't leak memory */
  567. pool = current_pool();
  568. if (!pool->has_stratum)
  569. quit(1, "hashratio: Miner Manager have to use stratum pool");
  570. if (pool->coinbase_len > HRTO_P_COINBASE_SIZE)
  571. quit(1, "hashratio: Miner Manager pool coinbase length have to less then %d", HRTO_P_COINBASE_SIZE);
  572. if (pool->merkles > HRTO_P_MERKLES_COUNT)
  573. quit(1, "hashratio: Miner Manager merkles have to less then %d", HRTO_P_MERKLES_COUNT);
  574. info->pool_no = pool->pool_no;
  575. cgtime(&info->last_stratum);
  576. cg_rlock(&pool->data_lock);
  577. info->pool_no = pool->pool_no;
  578. copy_pool_stratum(info, pool);
  579. hashratio_stratum_pkgs(hashratio, pool);
  580. cg_runlock(&pool->data_lock);
  581. /* Configure the parameter from outside */
  582. memset(send_pkg.data, 0, HRTO_P_DATA_LEN);
  583. // fan. We're not measuring temperature so set a safe but not max value
  584. info->fan_pwm = HRTO_PWM_MAX * 2 / 3;
  585. tmp = be32toh(info->fan_pwm);
  586. memcpy(send_pkg.data, &tmp, 4);
  587. // freq
  588. tmp = be32toh(info->default_freq);
  589. memcpy(send_pkg.data + 4, &tmp, 4);
  590. applog(LOG_DEBUG, "set freq: %d", info->default_freq);
  591. /* Configure the nonce2 offset and range */
  592. range = 0xffffffff / (total_devices + 1);
  593. start = range * (hashratio->device_id + 1);
  594. tmp = be32toh(start);
  595. memcpy(send_pkg.data + 8, &tmp, 4);
  596. tmp = be32toh(range);
  597. memcpy(send_pkg.data + 12, &tmp, 4);
  598. /* Package the data */
  599. hashratio_init_pkg(&send_pkg, HRTO_P_SET, 1, 1);
  600. hashratio_send_pkgs(hashratio, &send_pkg);
  601. }
  602. static int64_t hashratio_scanhash(struct thr_info *thr)
  603. {
  604. struct cgpu_info *hashratio = thr->cgpu;
  605. struct hashratio_info *info = hashratio->device_data;
  606. struct hashratio_pkg send_pkg;
  607. struct hashratio_ret ar;
  608. memset(send_pkg.data, 0, HRTO_P_DATA_LEN);
  609. hashratio_init_pkg(&send_pkg, HRTO_P_POLLING, 1, 1);
  610. if (unlikely(hashratio->usbinfo.nodev || hashratio_send_pkgs(hashratio, &send_pkg))) {
  611. applog(LOG_ERR, "%s%d: Device disappeared, shutting down thread",
  612. hashratio->drv->name, hashratio->device_id);
  613. return -1;
  614. }
  615. hashratio_get_result(thr, &ar);
  616. return (int64_t)info->local_work * 64 * 0xffffffff;
  617. }
  618. static struct api_data *hashratio_api_stats(struct cgpu_info *cgpu)
  619. {
  620. struct api_data *root = NULL;
  621. struct hashratio_info *info = cgpu->device_data;
  622. char buf[24];
  623. char buf2[256];
  624. double hwp;
  625. int i;
  626. // mm version
  627. sprintf(buf, "MM Version");
  628. root = api_add_string(root, buf, info->mm_version, false);
  629. // asic freq
  630. sprintf(buf, "Asic Freq (MHz)");
  631. root = api_add_int(root, buf, &(info->default_freq), false);
  632. // match work count
  633. for (i = 0; i < HRTO_DEFAULT_MODULARS; i++) {
  634. sprintf(buf, "Match work Modular %02d", i + 1);
  635. memset(buf2, 0, sizeof(buf2));
  636. snprintf(buf2, sizeof(buf2),
  637. "%02d:%08d %02d:%08d %02d:%08d %02d:%08d "
  638. "%02d:%08d %02d:%08d %02d:%08d %02d:%08d "
  639. "%02d:%08d %02d:%08d %02d:%08d %02d:%08d "
  640. "%02d:%08d %02d:%08d %02d:%08d %02d:%08d",
  641. i*16 + 1, info->matching_work[i*16 + 0],
  642. i*16 + 2, info->matching_work[i*16 + 1],
  643. i*16 + 3, info->matching_work[i*16 + 2],
  644. i*16 + 4, info->matching_work[i*16 + 3],
  645. i*16 + 5, info->matching_work[i*16 + 4],
  646. i*16 + 6, info->matching_work[i*16 + 5],
  647. i*16 + 7, info->matching_work[i*16 + 6],
  648. i*16 + 8, info->matching_work[i*16 + 7],
  649. i*16 + 9, info->matching_work[i*16 + 8],
  650. i*16 + 10, info->matching_work[i*16 + 9],
  651. i*16 + 11, info->matching_work[i*16 + 10],
  652. i*16 + 12, info->matching_work[i*16 + 11],
  653. i*16 + 13, info->matching_work[i*16 + 12],
  654. i*16 + 14, info->matching_work[i*16 + 13],
  655. i*16 + 15, info->matching_work[i*16 + 14],
  656. i*16 + 16, info->matching_work[i*16 + 15]);
  657. root = api_add_string(root, buf, buf2, true);
  658. }
  659. // local works
  660. sprintf(buf, "Local works");
  661. root = api_add_int(root, buf, &(info->local_works), false);
  662. // hardware error works
  663. sprintf(buf, "Hardware error works");
  664. root = api_add_int(root, buf, &(info->hw_works), false);
  665. // device hardware error %
  666. hwp = info->local_works ? ((double)info->hw_works / (double)info->local_works) : 0;
  667. sprintf(buf, "Device hardware error%%");
  668. root = api_add_percent(root, buf, &hwp, true);
  669. // Temperature
  670. sprintf(buf, "Temperature");
  671. root = api_add_int(root, buf, &(info->temp), false);
  672. // Fan
  673. for (i = 0; i < HRTO_FAN_COUNT; i++) {
  674. sprintf(buf, "Fan%d", i+1);
  675. root = api_add_int(root, buf, &(info->fan[i]), false);
  676. }
  677. return root;
  678. }
  679. static void hashratio_shutdown(struct thr_info __maybe_unused *thr)
  680. {
  681. }
  682. struct device_drv hashratio_drv = {
  683. .drv_id = DRIVER_hashratio,
  684. .dname = "hashratio",
  685. .name = "HRO",
  686. .get_api_stats = hashratio_api_stats,
  687. .drv_detect = hashratio_detect,
  688. .thread_prepare = hashratio_prepare,
  689. .hash_work = hash_driver_work,
  690. .scanwork = hashratio_scanhash,
  691. .flush_work = hashratio_update_work,
  692. .update_work = hashratio_update_work,
  693. .thread_shutdown = hashratio_shutdown,
  694. };