driver-hashratio.c.bak 23 KB

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