driver-hashratio.c 24 KB

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