driver-icarus.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Icarus found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include <float.h>
  31. #include <limits.h>
  32. #include <pthread.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <strings.h>
  36. #include <sys/time.h>
  37. #include <unistd.h>
  38. #include <math.h>
  39. #include "config.h"
  40. #ifdef WIN32
  41. #include <windows.h>
  42. #endif
  43. #include "compat.h"
  44. #include "miner.h"
  45. #include "usbutils.h"
  46. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  47. #define ICARUS_IO_SPEED 115200
  48. #define ICARUS_BUF_SIZE 8
  49. // The size of a successful nonce read
  50. #define ANT_READ_SIZE 5
  51. #define ICARUS_READ_SIZE 4
  52. #define ROCK_READ_SIZE 8
  53. // Ensure the sizes are correct for the Serial read
  54. #if (ICARUS_READ_SIZE != 4)
  55. #error ICARUS_READ_SIZE must be 4
  56. #endif
  57. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  58. ASSERT1(sizeof(uint32_t) == 4);
  59. // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
  60. // or even use that number? 1/2
  61. // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  62. // maybe 1ms?
  63. #define ICARUS_READ_TIME(baud) (0.001)
  64. // USB ms timeout to wait - user specified timeouts are multiples of this
  65. #define ICARUS_WAIT_TIMEOUT 100
  66. #define ANT_WAIT_TIMEOUT 10
  67. #define ICARUS_CMR2_TIMEOUT 1
  68. // Defined in multiples of ICARUS_WAIT_TIMEOUT
  69. // Must of course be greater than ICARUS_READ_COUNT_TIMING/ICARUS_WAIT_TIMEOUT
  70. // There's no need to have this bigger, since the overhead/latency of extra work
  71. // is pretty small once you get beyond a 10s nonce range time and 10s also
  72. // means that nothing slower than 429MH/s can go idle so most icarus devices
  73. // will always mine without idling
  74. #define ICARUS_READ_TIME_LIMIT_MAX 100
  75. // In timing mode: Default starting value until an estimate can be obtained
  76. // 5000 ms allows for up to a ~840MH/s device
  77. #define ICARUS_READ_COUNT_TIMING 5000
  78. // Antminer USB is > 1GH/s so use a shorter limit
  79. // 1000 ms allows for up to ~4GH/s device
  80. #define ANTUSB_READ_COUNT_TIMING 1000
  81. #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
  82. #define SECTOMS(s) ((int)((s) * 1000))
  83. // How many ms below the expected completion time to abort work
  84. // extra in case the last read is delayed
  85. #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
  86. // For a standard Icarus REV3 (to 5 places)
  87. // Since this rounds up a the last digit - it is a slight overestimate
  88. // Thus the hash rate will be a VERY slight underestimate
  89. // (by a lot less than the displayed accuracy)
  90. // Minor inaccuracy of these numbers doesn't affect the work done,
  91. // only the displayed MH/s
  92. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  93. #define LANCELOT_HASH_TIME 0.0000000025000
  94. #define ASICMINERUSB_HASH_TIME 0.0000000029761
  95. // TODO: What is it?
  96. #define CAIRNSMORE1_HASH_TIME 0.0000000027000
  97. // Per FPGA
  98. #define CAIRNSMORE2_HASH_TIME 0.0000000066600
  99. #define NANOSEC 1000000000.0
  100. #define ANTMINERUSB_HASH_MHZ 0.000000125
  101. #define ANTMINERUSB_HASH_TIME (ANTMINERUSB_HASH_MHZ / (double)(opt_anu_freq))
  102. #define CAIRNSMORE2_INTS 4
  103. // Icarus Rev3 doesn't send a completion message when it finishes
  104. // the full nonce range, so to avoid being idle we must abort the
  105. // work (by starting a new work item) shortly before it finishes
  106. //
  107. // Thus we need to estimate 2 things:
  108. // 1) How many hashes were done if the work was aborted
  109. // 2) How high can the timeout be before the Icarus is idle,
  110. // to minimise the number of work items started
  111. // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
  112. // to ensure the estimate ends before idle
  113. //
  114. // The simple calculation used is:
  115. // Tn = Total time in seconds to calculate n hashes
  116. // Hs = seconds per hash
  117. // Xn = number of hashes
  118. // W = code/usb overhead per work
  119. //
  120. // Rough but reasonable estimate:
  121. // Tn = Hs * Xn + W (of the form y = mx + b)
  122. //
  123. // Thus:
  124. // Line of best fit (using least squares)
  125. //
  126. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  127. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  128. //
  129. // N.B. W is less when aborting work since we aren't waiting for the reply
  130. // to be transferred back (ICARUS_READ_TIME)
  131. // Calculating the hashes aborted at n seconds is thus just n/Hs
  132. // (though this is still a slight overestimate due to code delays)
  133. //
  134. // Both below must be exceeded to complete a set of data
  135. // Minimum how long after the first, the last data point must be
  136. #define HISTORY_SEC 60
  137. // Minimum how many points a single ICARUS_HISTORY should have
  138. #define MIN_DATA_COUNT 5
  139. // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
  140. #define MAX_MIN_DATA_COUNT 100
  141. static struct timeval history_sec = { HISTORY_SEC, 0 };
  142. // Store the last INFO_HISTORY data sets
  143. // [0] = current data, not yet ready to be included as an estimate
  144. // Each new data set throws the last old set off the end thus
  145. // keeping a ongoing average of recent data
  146. #define INFO_HISTORY 10
  147. struct ICARUS_HISTORY {
  148. struct timeval finish;
  149. double sumXiTi;
  150. double sumXi;
  151. double sumTi;
  152. double sumXi2;
  153. uint32_t values;
  154. uint32_t hash_count_min;
  155. uint32_t hash_count_max;
  156. };
  157. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  158. static const char *MODE_DEFAULT_STR = "default";
  159. static const char *MODE_SHORT_STR = "short";
  160. static const char *MODE_SHORT_STREQ = "short=";
  161. static const char *MODE_LONG_STR = "long";
  162. static const char *MODE_LONG_STREQ = "long=";
  163. static const char *MODE_VALUE_STR = "value";
  164. static const char *MODE_UNKNOWN_STR = "unknown";
  165. #define MAX_DEVICE_NUM 100
  166. #define MAX_WORK_BUFFER_SIZE 2
  167. #define MAX_CHIP_NUM 24
  168. // Set it to 3 or 9
  169. #define NONCE_CORRECTION_TIMES 3
  170. #define MAX_TRIES 4
  171. #define RM_CMD_MASK 0x0F
  172. #define RM_STATUS_MASK 0xF0
  173. #define RM_CHIP_MASK 0x3F
  174. #define RM_PRODUCT_MASK 0xC0
  175. #define RM_PRODUCT_RBOX 0x00
  176. #define RM_PRODUCT_T1 0x40
  177. #define RM_PRODUCT_T2 0x80
  178. #define RM_PRODUCT_TEST 0xC0
  179. #if (NONCE_CORRECTION_TIMES == 9)
  180. static int32_t rbox_corr_values[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
  181. #endif
  182. #if (NONCE_CORRECTION_TIMES == 3)
  183. static int32_t rbox_corr_values[] = {0, 1, -1};
  184. #endif
  185. typedef enum {
  186. NONCE_DATA1_OFFSET = 0,
  187. NONCE_DATA2_OFFSET,
  188. NONCE_DATA3_OFFSET,
  189. NONCE_DATA4_OFFSET,
  190. NONCE_TASK_CMD_OFFSET,
  191. NONCE_CHIP_NO_OFFSET,
  192. NONCE_TASK_NO_OFFSET,
  193. NONCE_COMMAND_OFFSET,
  194. NONCE_MAX_OFFSET
  195. } NONCE_OFFSET;
  196. typedef enum {
  197. NONCE_DATA_CMD = 0,
  198. NONCE_TASK_COMPLETE_CMD,
  199. NONCE_GET_TASK_CMD,
  200. } NONCE_COMMAND;
  201. typedef struct nonce_data {
  202. int chip_no;
  203. unsigned int task_no ;
  204. unsigned char work_state;
  205. int cmd_value;
  206. } NONCE_DATA;
  207. typedef enum {
  208. ROCKMINER_RBOX = 0,
  209. ROCKMINER_T1,
  210. ROCKMINER_T2,
  211. ROCKMINER_MAX
  212. } ROCKMINER_PRODUCT_T;
  213. typedef struct rockminer_chip_info {
  214. unsigned char freq;
  215. int error_cnt;
  216. time_t last_received_task_complete_time;
  217. } ROCKMINER_CHIP_INFO;
  218. typedef struct rockminer_device_info {
  219. unsigned char detect_chip_no;
  220. unsigned char chip_max;
  221. unsigned char product_id;
  222. float min_frq;
  223. float def_frq;
  224. float max_frq;
  225. ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM];
  226. time_t dev_detect_time;
  227. } ROCKMINER_DEVICE_INFO;
  228. struct ICARUS_INFO {
  229. enum sub_ident ident;
  230. int intinfo;
  231. // time to calculate the golden_ob
  232. uint64_t golden_hashes;
  233. struct timeval golden_tv;
  234. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  235. uint32_t min_data_count;
  236. int timeout;
  237. // seconds per Hash
  238. double Hs;
  239. // ms til we abort
  240. int read_time;
  241. // ms limit for (short=/long=) read_time
  242. int read_time_limit;
  243. // How long without hashes is considered a failed device
  244. int fail_time;
  245. enum timing_mode timing_mode;
  246. bool do_icarus_timing;
  247. double fullnonce;
  248. int count;
  249. double W;
  250. uint32_t values;
  251. uint64_t hash_count_range;
  252. // Determine the cost of history processing
  253. // (which will only affect W)
  254. uint64_t history_count;
  255. struct timeval history_time;
  256. // icarus-options
  257. int baud;
  258. int work_division;
  259. int fpga_count;
  260. uint32_t nonce_mask;
  261. uint8_t cmr2_speed;
  262. bool speed_next_work;
  263. bool flash_next_work;
  264. int nonce_size;
  265. bool failing;
  266. ROCKMINER_DEVICE_INFO rmdev;
  267. struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
  268. char rock_init[64];
  269. uint64_t nonces_checked;
  270. uint64_t nonces_correction_times;
  271. uint64_t nonces_correction_tests;
  272. uint64_t nonces_fail;
  273. uint64_t nonces_correction[NONCE_CORRECTION_TIMES];
  274. };
  275. #define ICARUS_MIDSTATE_SIZE 32
  276. #define ICARUS_UNUSED_SIZE 16
  277. #define ICARUS_WORK_SIZE 12
  278. #define ICARUS_WORK_DATA_OFFSET 64
  279. #define ICARUS_CMR2_SPEED_FACTOR 2.5
  280. #define ICARUS_CMR2_SPEED_MIN_INT 100
  281. #define ICARUS_CMR2_SPEED_DEF_INT 180
  282. #define ICARUS_CMR2_SPEED_MAX_INT 220
  283. #define CMR2_INT_TO_SPEED(_speed) ((uint8_t)((float)_speed / ICARUS_CMR2_SPEED_FACTOR))
  284. #define ICARUS_CMR2_SPEED_MIN CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MIN_INT)
  285. #define ICARUS_CMR2_SPEED_DEF CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_DEF_INT)
  286. #define ICARUS_CMR2_SPEED_MAX CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MAX_INT)
  287. #define ICARUS_CMR2_SPEED_INC 1
  288. #define ICARUS_CMR2_SPEED_DEC -1
  289. #define ICARUS_CMR2_SPEED_FAIL -10
  290. #define ICARUS_CMR2_PREFIX ((uint8_t)0xB7)
  291. #define ICARUS_CMR2_CMD_SPEED ((uint8_t)0)
  292. #define ICARUS_CMR2_CMD_FLASH ((uint8_t)1)
  293. #define ICARUS_CMR2_DATA_FLASH_OFF ((uint8_t)0)
  294. #define ICARUS_CMR2_DATA_FLASH_ON ((uint8_t)1)
  295. #define ICARUS_CMR2_CHECK ((uint8_t)0x6D)
  296. struct ICARUS_WORK {
  297. uint8_t midstate[ICARUS_MIDSTATE_SIZE];
  298. // These 4 bytes are for CMR2 bitstreams that handle MHz adjustment
  299. uint8_t check;
  300. uint8_t data;
  301. uint8_t cmd;
  302. uint8_t prefix;
  303. uint8_t unused[ICARUS_UNUSED_SIZE];
  304. uint8_t work[ICARUS_WORK_SIZE];
  305. };
  306. #define END_CONDITION 0x0000ffff
  307. // Looking for options in --icarus-timing and --icarus-options:
  308. //
  309. // Code increments this each time we start to look at a device
  310. // However, this means that if other devices are checked by
  311. // the Icarus code (e.g. Avalon only as at 20130517)
  312. // they will count in the option offset
  313. //
  314. // This, however, is deterministic so that's OK
  315. //
  316. // If we were to increment after successfully finding an Icarus
  317. // that would be random since an Icarus may fail and thus we'd
  318. // not be able to predict the option order
  319. //
  320. // Devices are checked in the order libusb finds them which is ?
  321. //
  322. static int option_offset = -1;
  323. /*
  324. #define ICA_BUFSIZ (0x200)
  325. static void transfer_read(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, enum usb_cmds cmd)
  326. {
  327. int err;
  328. err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd);
  329. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  330. icarus->drv->name, icarus->cgminer_id,
  331. usb_cmdname(cmd), err);
  332. }
  333. */
  334. static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
  335. {
  336. int err;
  337. err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd);
  338. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  339. icarus->drv->name, icarus->cgminer_id,
  340. usb_cmdname(cmd), err);
  341. }
  342. #define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \
  343. _transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd)
  344. static void icarus_initialise(struct cgpu_info *icarus, int baud)
  345. {
  346. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  347. uint16_t wValue, wIndex;
  348. enum sub_ident ident;
  349. int interface;
  350. if (icarus->usbinfo.nodev)
  351. return;
  352. interface = _usb_interface(icarus, info->intinfo);
  353. ident = usb_ident(icarus);
  354. switch (ident) {
  355. case IDENT_BLT:
  356. case IDENT_LLT:
  357. case IDENT_CMR1:
  358. case IDENT_CMR2:
  359. // Reset
  360. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
  361. interface, C_RESET);
  362. if (icarus->usbinfo.nodev)
  363. return;
  364. // Latency
  365. _usb_ftdi_set_latency(icarus, info->intinfo);
  366. if (icarus->usbinfo.nodev)
  367. return;
  368. // Set data control
  369. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
  370. interface, C_SETDATA);
  371. if (icarus->usbinfo.nodev)
  372. return;
  373. // default to BLT/LLT 115200
  374. wValue = FTDI_VALUE_BAUD_BLT;
  375. wIndex = FTDI_INDEX_BAUD_BLT;
  376. if (ident == IDENT_CMR1 || ident == IDENT_CMR2) {
  377. switch (baud) {
  378. case 115200:
  379. wValue = FTDI_VALUE_BAUD_CMR_115;
  380. wIndex = FTDI_INDEX_BAUD_CMR_115;
  381. break;
  382. case 57600:
  383. wValue = FTDI_VALUE_BAUD_CMR_57;
  384. wIndex = FTDI_INDEX_BAUD_CMR_57;
  385. break;
  386. default:
  387. quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud);
  388. break;
  389. }
  390. }
  391. // Set the baud
  392. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
  393. (wIndex & 0xff00) | interface, C_SETBAUD);
  394. if (icarus->usbinfo.nodev)
  395. return;
  396. // Set Modem Control
  397. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
  398. interface, C_SETMODEM);
  399. if (icarus->usbinfo.nodev)
  400. return;
  401. // Set Flow Control
  402. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
  403. interface, C_SETFLOW);
  404. if (icarus->usbinfo.nodev)
  405. return;
  406. // Clear any sent data
  407. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
  408. interface, C_PURGETX);
  409. if (icarus->usbinfo.nodev)
  410. return;
  411. // Clear any received data
  412. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
  413. interface, C_PURGERX);
  414. break;
  415. case IDENT_ICA:
  416. // Set Data Control
  417. transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
  418. interface, C_SETDATA);
  419. if (icarus->usbinfo.nodev)
  420. return;
  421. // Set Line Control
  422. uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  423. _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  424. interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
  425. if (icarus->usbinfo.nodev)
  426. return;
  427. // Vendor
  428. transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  429. interface, C_VENDOR);
  430. break;
  431. case IDENT_AMU:
  432. case IDENT_ANU:
  433. case IDENT_LIN:
  434. // Enable the UART
  435. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  436. CP210X_VALUE_UART_ENABLE,
  437. interface, C_ENABLE_UART);
  438. if (icarus->usbinfo.nodev)
  439. return;
  440. // Set data control
  441. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
  442. interface, C_SETDATA);
  443. if (icarus->usbinfo.nodev)
  444. return;
  445. // Set the baud
  446. uint32_t data = CP210X_DATA_BAUD;
  447. _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
  448. interface, &data, sizeof(data), C_SETBAUD);
  449. break;
  450. case IDENT_AVA:
  451. break;
  452. default:
  453. quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
  454. icarus->drv->name, icarus->cgminer_id, ident);
  455. }
  456. }
  457. static void rev(unsigned char *s, size_t l)
  458. {
  459. size_t i, j;
  460. unsigned char t;
  461. for (i = 0, j = l - 1; i < j; i++, j--) {
  462. t = s[i];
  463. s[i] = s[j];
  464. s[j] = t;
  465. }
  466. }
  467. #define ICA_NONCE_ERROR -1
  468. #define ICA_NONCE_OK 0
  469. #define ICA_NONCE_RESTART 1
  470. #define ICA_NONCE_TIMEOUT 2
  471. static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start,
  472. struct timeval *tv_finish, struct thr_info *thr, int read_time)
  473. {
  474. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  475. int err, amt, rc;
  476. if (icarus->usbinfo.nodev)
  477. return ICA_NONCE_ERROR;
  478. cgtime(tv_start);
  479. err = usb_read_ii_timeout_cancellable(icarus, info->intinfo, (char *)buf,
  480. info->nonce_size, &amt, read_time,
  481. C_GETRESULTS);
  482. cgtime(tv_finish);
  483. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  484. applog(LOG_ERR, "%s%i: Comms error (rerr=%d amt=%d)", icarus->drv->name,
  485. icarus->device_id, err, amt);
  486. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  487. return ICA_NONCE_ERROR;
  488. }
  489. if (amt >= ICARUS_READ_SIZE)
  490. return ICA_NONCE_OK;
  491. rc = SECTOMS(tdiff(tv_finish, tv_start));
  492. if (thr && thr->work_restart) {
  493. applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
  494. return ICA_NONCE_RESTART;
  495. }
  496. if (amt > 0)
  497. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  498. else
  499. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  500. return ICA_NONCE_TIMEOUT;
  501. }
  502. static const char *timing_mode_str(enum timing_mode timing_mode)
  503. {
  504. switch(timing_mode) {
  505. case MODE_DEFAULT:
  506. return MODE_DEFAULT_STR;
  507. case MODE_SHORT:
  508. return MODE_SHORT_STR;
  509. case MODE_LONG:
  510. return MODE_LONG_STR;
  511. case MODE_VALUE:
  512. return MODE_VALUE_STR;
  513. default:
  514. return MODE_UNKNOWN_STR;
  515. }
  516. }
  517. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  518. {
  519. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  520. int read_count_timing = 0;
  521. enum sub_ident ident;
  522. double Hs, fail_time;
  523. char buf[BUFSIZ+1];
  524. char *ptr, *comma, *eq;
  525. size_t max;
  526. int i;
  527. if (opt_icarus_timing == NULL)
  528. buf[0] = '\0';
  529. else {
  530. ptr = opt_icarus_timing;
  531. for (i = 0; i < this_option_offset; i++) {
  532. comma = strchr(ptr, ',');
  533. if (comma == NULL)
  534. break;
  535. ptr = comma + 1;
  536. }
  537. comma = strchr(ptr, ',');
  538. if (comma == NULL)
  539. max = strlen(ptr);
  540. else
  541. max = comma - ptr;
  542. if (max > BUFSIZ)
  543. max = BUFSIZ;
  544. strncpy(buf, ptr, max);
  545. buf[max] = '\0';
  546. }
  547. ident = usb_ident(icarus);
  548. switch (ident) {
  549. case IDENT_ICA:
  550. case IDENT_AVA:
  551. info->Hs = ICARUS_REV3_HASH_TIME;
  552. read_count_timing = ICARUS_READ_COUNT_TIMING;
  553. break;
  554. case IDENT_BLT:
  555. case IDENT_LLT:
  556. info->Hs = LANCELOT_HASH_TIME;
  557. read_count_timing = ICARUS_READ_COUNT_TIMING;
  558. break;
  559. case IDENT_AMU:
  560. info->Hs = ASICMINERUSB_HASH_TIME;
  561. read_count_timing = ICARUS_READ_COUNT_TIMING;
  562. break;
  563. case IDENT_CMR1:
  564. info->Hs = CAIRNSMORE1_HASH_TIME;
  565. read_count_timing = ICARUS_READ_COUNT_TIMING;
  566. break;
  567. case IDENT_CMR2:
  568. info->Hs = CAIRNSMORE2_HASH_TIME;
  569. read_count_timing = ICARUS_READ_COUNT_TIMING;
  570. break;
  571. case IDENT_ANU:
  572. info->Hs = ANTMINERUSB_HASH_TIME;
  573. read_count_timing = ANTUSB_READ_COUNT_TIMING;
  574. break;
  575. default:
  576. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  577. icarus->drv->name, ident);
  578. }
  579. info->read_time = 0;
  580. info->read_time_limit = 0; // 0 = no limit
  581. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  582. // short
  583. info->read_time = read_count_timing;
  584. info->timing_mode = MODE_SHORT;
  585. info->do_icarus_timing = true;
  586. } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
  587. // short=limit
  588. info->read_time = read_count_timing;
  589. info->timing_mode = MODE_SHORT;
  590. info->do_icarus_timing = true;
  591. info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
  592. if (info->read_time_limit < 0)
  593. info->read_time_limit = 0;
  594. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  595. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  596. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  597. // long
  598. info->read_time = read_count_timing;
  599. info->timing_mode = MODE_LONG;
  600. info->do_icarus_timing = true;
  601. } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
  602. // long=limit
  603. info->read_time = read_count_timing;
  604. info->timing_mode = MODE_LONG;
  605. info->do_icarus_timing = true;
  606. info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
  607. if (info->read_time_limit < 0)
  608. info->read_time_limit = 0;
  609. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  610. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  611. } else if ((Hs = atof(buf)) != 0) {
  612. // ns[=read_time]
  613. info->Hs = Hs / NANOSEC;
  614. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  615. if ((eq = strchr(buf, '=')) != NULL)
  616. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  617. if (info->read_time < ICARUS_READ_COUNT_MIN)
  618. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  619. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  620. info->read_time = ICARUS_READ_COUNT_MIN;
  621. info->timing_mode = MODE_VALUE;
  622. info->do_icarus_timing = false;
  623. } else {
  624. // Anything else in buf just uses DEFAULT mode
  625. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  626. if ((eq = strchr(buf, '=')) != NULL)
  627. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  628. if (info->read_time < ICARUS_READ_COUNT_MIN)
  629. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  630. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  631. info->read_time = ICARUS_READ_COUNT_MIN;
  632. info->timing_mode = MODE_DEFAULT;
  633. info->do_icarus_timing = false;
  634. }
  635. info->min_data_count = MIN_DATA_COUNT;
  636. // All values are in multiples of ICARUS_WAIT_TIMEOUT
  637. info->read_time_limit *= ICARUS_WAIT_TIMEOUT;
  638. applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms limit=%dms Hs=%e",
  639. icarus->drv->name, icarus->cgminer_id,
  640. timing_mode_str(info->timing_mode),
  641. info->read_time, info->read_time_limit, info->Hs);
  642. /* Set the time to detect a dead device to 25 full nonce ranges. */
  643. fail_time = info->Hs * 0xffffffffull * 25.0;
  644. /* Integer accuracy is definitely enough. */
  645. info->fail_time = fail_time;
  646. }
  647. static uint32_t mask(int work_division)
  648. {
  649. uint32_t nonce_mask = 0x7fffffff;
  650. // yes we can calculate these, but this way it's easy to see what they are
  651. switch (work_division) {
  652. case 1:
  653. nonce_mask = 0xffffffff;
  654. break;
  655. case 2:
  656. nonce_mask = 0x7fffffff;
  657. break;
  658. case 4:
  659. nonce_mask = 0x3fffffff;
  660. break;
  661. case 8:
  662. nonce_mask = 0x1fffffff;
  663. break;
  664. default:
  665. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  666. }
  667. return nonce_mask;
  668. }
  669. static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
  670. {
  671. char buf[BUFSIZ+1];
  672. char *ptr, *comma, *colon, *colon2;
  673. enum sub_ident ident;
  674. size_t max;
  675. int i, tmp;
  676. if (opt_icarus_options == NULL)
  677. buf[0] = '\0';
  678. else {
  679. ptr = opt_icarus_options;
  680. for (i = 0; i < this_option_offset; i++) {
  681. comma = strchr(ptr, ',');
  682. if (comma == NULL)
  683. break;
  684. ptr = comma + 1;
  685. }
  686. comma = strchr(ptr, ',');
  687. if (comma == NULL)
  688. max = strlen(ptr);
  689. else
  690. max = comma - ptr;
  691. if (max > BUFSIZ)
  692. max = BUFSIZ;
  693. strncpy(buf, ptr, max);
  694. buf[max] = '\0';
  695. }
  696. ident = usb_ident(icarus);
  697. switch (ident) {
  698. case IDENT_ICA:
  699. case IDENT_BLT:
  700. case IDENT_LLT:
  701. case IDENT_AVA:
  702. *baud = ICARUS_IO_SPEED;
  703. *work_division = 2;
  704. *fpga_count = 2;
  705. break;
  706. case IDENT_AMU:
  707. case IDENT_ANU:
  708. *baud = ICARUS_IO_SPEED;
  709. *work_division = 1;
  710. *fpga_count = 1;
  711. break;
  712. case IDENT_CMR1:
  713. *baud = ICARUS_IO_SPEED;
  714. *work_division = 2;
  715. *fpga_count = 2;
  716. break;
  717. case IDENT_CMR2:
  718. *baud = ICARUS_IO_SPEED;
  719. *work_division = 1;
  720. *fpga_count = 1;
  721. break;
  722. default:
  723. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  724. icarus->drv->name, ident);
  725. }
  726. if (*buf) {
  727. colon = strchr(buf, ':');
  728. if (colon)
  729. *(colon++) = '\0';
  730. if (*buf) {
  731. tmp = atoi(buf);
  732. switch (tmp) {
  733. case 115200:
  734. *baud = 115200;
  735. break;
  736. case 57600:
  737. *baud = 57600;
  738. break;
  739. default:
  740. quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  741. }
  742. }
  743. if (colon && *colon) {
  744. colon2 = strchr(colon, ':');
  745. if (colon2)
  746. *(colon2++) = '\0';
  747. if (*colon) {
  748. tmp = atoi(colon);
  749. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  750. *work_division = tmp;
  751. *fpga_count = tmp; // default to the same
  752. } else {
  753. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  754. }
  755. }
  756. if (colon2 && *colon2) {
  757. tmp = atoi(colon2);
  758. if (tmp > 0 && tmp <= *work_division)
  759. *fpga_count = tmp;
  760. else {
  761. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  762. }
  763. }
  764. }
  765. }
  766. }
  767. unsigned char crc5(unsigned char *ptr, unsigned char len)
  768. {
  769. unsigned char i, j, k;
  770. unsigned char crc = 0x1f;
  771. unsigned char crcin[5] = {1, 1, 1, 1, 1};
  772. unsigned char crcout[5] = {1, 1, 1, 1, 1};
  773. unsigned char din = 0;
  774. j = 0x80;
  775. k = 0;
  776. for (i = 0; i < len; i++) {
  777. if (*ptr & j)
  778. din = 1;
  779. else
  780. din = 0;
  781. crcout[0] = crcin[4] ^ din;
  782. crcout[1] = crcin[0];
  783. crcout[2] = crcin[1] ^ crcin[4] ^ din;
  784. crcout[3] = crcin[2];
  785. crcout[4] = crcin[3];
  786. j = j >> 1;
  787. k++;
  788. if (k == 8) {
  789. j = 0x80;
  790. k = 0;
  791. ptr++;
  792. }
  793. memcpy(crcin, crcout, 5);
  794. }
  795. crc = 0;
  796. if (crcin[4])
  797. crc |= 0x10;
  798. if (crcin[3])
  799. crc |= 0x08;
  800. if (crcin[2])
  801. crc |= 0x04;
  802. if (crcin[1])
  803. crc |= 0x02;
  804. if (crcin[0])
  805. crc |= 0x01;
  806. return crc;
  807. }
  808. static bool anu_freqfound = false;
  809. static uint16_t anu_freq_hex;
  810. static void anu_find_freqhex(void)
  811. {
  812. float fout, best_fout = opt_anu_freq;
  813. int od, nf, nr, no, n, m, bs;
  814. float best_diff = 1000;
  815. anu_freqfound = true;
  816. for (od = 0; od < 4; od++) {
  817. no = 1 << od;
  818. for (n = 0; n < 16; n++) {
  819. nr = n + 1;
  820. for (m = 0; m < 64; m++) {
  821. nf = m + 1;
  822. fout = 25 * (float)nf /((float)(nr) * (float)(no));
  823. if (fabsf(fout - opt_anu_freq) > best_diff)
  824. continue;
  825. if (500 <= (fout * no) && (fout * no) <= 1000)
  826. bs = 1;
  827. else
  828. bs = 0;
  829. best_diff = fabsf(fout - opt_anu_freq);
  830. best_fout = fout;
  831. anu_freq_hex = (bs << 14) | (m << 7) | (n << 2) | od;
  832. if (fout == opt_anu_freq) {
  833. applog(LOG_DEBUG, "ANU found exact frequency %.1f with hex %04x",
  834. opt_anu_freq, anu_freq_hex);
  835. return;
  836. }
  837. }
  838. }
  839. }
  840. opt_anu_freq = best_fout;
  841. applog(LOG_NOTICE, "ANU found nearest frequency %.1f with hex %04x", opt_anu_freq,
  842. anu_freq_hex);
  843. }
  844. static bool set_anu_freq(struct cgpu_info *icarus, struct ICARUS_INFO *info)
  845. {
  846. unsigned char cmd_buf[4], rdreg_buf[4];
  847. int amount, err;
  848. char buf[512];
  849. if (!anu_freqfound)
  850. anu_find_freqhex();
  851. memset(cmd_buf, 0, 4);
  852. memset(rdreg_buf, 0, 4);
  853. cmd_buf[0] = 2 | 0x80;
  854. cmd_buf[1] = (anu_freq_hex & 0xff00u) >> 8;
  855. cmd_buf[2] = (anu_freq_hex & 0x00ffu);
  856. cmd_buf[3] = crc5(cmd_buf, 27);
  857. rdreg_buf[0] = 4 | 0x80;
  858. rdreg_buf[1] = 0; //16-23
  859. rdreg_buf[2] = 0x04; //8-15
  860. rdreg_buf[3] = crc5(rdreg_buf, 27);
  861. applog(LOG_DEBUG, "%s%i: Send frequency %02x%02x%02x%02x", icarus->drv->name, icarus->device_id,
  862. cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]);
  863. err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_CMD);
  864. if (err != LIBUSB_SUCCESS || amount != 4) {
  865. applog(LOG_ERR, "%s%i: Write freq Comms error (werr=%d amount=%d)",
  866. icarus->drv->name, icarus->device_id, err, amount);
  867. return false;
  868. }
  869. err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS);
  870. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  871. applog(LOG_ERR, "%s%i: Read freq Comms error (rerr=%d amount=%d)",
  872. icarus->drv->name, icarus->device_id, err, amount);
  873. return false;
  874. }
  875. applog(LOG_DEBUG, "%s%i: Send freq getstatus %02x%02x%02x%02x", icarus->drv->name, icarus->device_id,
  876. rdreg_buf[0], rdreg_buf[1], rdreg_buf[2], rdreg_buf[3]);
  877. err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_RDREG);
  878. if (err != LIBUSB_SUCCESS || amount != 4) {
  879. applog(LOG_ERR, "%s%i: Write freq Comms error (werr=%d amount=%d)",
  880. icarus->drv->name, icarus->device_id, err, amount);
  881. return false;
  882. }
  883. err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS);
  884. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  885. applog(LOG_ERR, "%s%i: Read freq Comms error (rerr=%d amount=%d)",
  886. icarus->drv->name, icarus->device_id, err, amount);
  887. return false;
  888. }
  889. return true;
  890. }
  891. static void rock_init_last_received_task_complete_time(struct ICARUS_INFO *info)
  892. {
  893. int i;
  894. if (opt_rock_freq < info->rmdev.min_frq ||
  895. opt_rock_freq > info->rmdev.max_frq)
  896. opt_rock_freq = info->rmdev.def_frq;
  897. for (i = 0; i < MAX_CHIP_NUM; ++i) {
  898. info->rmdev.chip[i].last_received_task_complete_time = time(NULL);
  899. info->rmdev.chip[i].freq = opt_rock_freq/10 - 1;
  900. info->rmdev.chip[i].error_cnt = 0;
  901. }
  902. info->rmdev.dev_detect_time = time(NULL);
  903. }
  904. static void icarus_clear(struct cgpu_info *icarus, struct ICARUS_INFO *info)
  905. {
  906. char buf[512];
  907. int amt;
  908. do {
  909. usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amt, 100, C_GETRESULTS);
  910. } while (amt > 0);
  911. }
  912. static struct cgpu_info *icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  913. {
  914. int this_option_offset = ++option_offset;
  915. struct ICARUS_INFO *info;
  916. struct timeval tv_start, tv_finish;
  917. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  918. // N.B. golden_ob MUST take less time to calculate
  919. // than the timeout set in icarus_open()
  920. // This one takes ~0.53ms on Rev3 Icarus
  921. const char golden_ob[] =
  922. "4679ba4ec99876bf4bfe086082b40025"
  923. "4df6c356451471139a3afa71e48f544a"
  924. "00000000000000000000000000000000"
  925. "0000000087320b1a1426674f2fa722ce";
  926. const char golden_nonce[] = "000187a2";
  927. const uint32_t golden_nonce_val = 0x000187a2;
  928. unsigned char nonce_bin[ICARUS_READ_SIZE];
  929. struct ICARUS_WORK workdata;
  930. char *nonce_hex;
  931. int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
  932. struct cgpu_info *icarus;
  933. int ret, err, amount, tries, i;
  934. bool ok;
  935. bool cmr2_ok[CAIRNSMORE2_INTS];
  936. bool anu_freqset = false;
  937. int cmr2_count;
  938. if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
  939. quithere(1, "Data and golden_ob sizes don't match");
  940. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  941. if (!usb_init(icarus, dev, found))
  942. goto shin;
  943. get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
  944. hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
  945. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  946. if (unlikely(!info))
  947. quit(1, "Failed to malloc ICARUS_INFO");
  948. icarus->device_data = (void *)info;
  949. info->ident = usb_ident(icarus);
  950. switch (info->ident) {
  951. case IDENT_ICA:
  952. case IDENT_AVA:
  953. case IDENT_BLT:
  954. case IDENT_LLT:
  955. case IDENT_AMU:
  956. case IDENT_CMR1:
  957. info->timeout = ICARUS_WAIT_TIMEOUT;
  958. break;
  959. case IDENT_ANU:
  960. info->timeout = ANT_WAIT_TIMEOUT;
  961. break;
  962. case IDENT_CMR2:
  963. if (found->intinfo_count != CAIRNSMORE2_INTS) {
  964. quithere(1, "CMR2 Interface count (%d) isn't expected: %d",
  965. found->intinfo_count,
  966. CAIRNSMORE2_INTS);
  967. }
  968. info->timeout = ICARUS_CMR2_TIMEOUT;
  969. cmr2_count = 0;
  970. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  971. cmr2_ok[i] = false;
  972. break;
  973. default:
  974. quit(1, "%s icarus_detect_one() invalid %s ident=%d",
  975. icarus->drv->dname, icarus->drv->dname, info->ident);
  976. }
  977. info->nonce_size = ICARUS_READ_SIZE;
  978. // For CMR2 test each USB Interface
  979. cmr2_retry:
  980. tries = 2;
  981. ok = false;
  982. while (!ok && tries-- > 0) {
  983. icarus_clear(icarus, info);
  984. icarus_initialise(icarus, baud);
  985. if (info->ident == IDENT_ANU && !set_anu_freq(icarus, info)) {
  986. applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?",
  987. icarus->drv->name, icarus->device_id);
  988. continue;
  989. }
  990. err = usb_write_ii(icarus, info->intinfo,
  991. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  992. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  993. continue;
  994. memset(nonce_bin, 0, sizeof(nonce_bin));
  995. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 300);
  996. if (ret != ICA_NONCE_OK)
  997. continue;
  998. if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 4) {
  999. applog(LOG_DEBUG, "%s %i: Detected Rockminer, deferring detection",
  1000. icarus->drv->name, icarus->device_id);
  1001. usb_buffer_clear(icarus);
  1002. break;
  1003. }
  1004. if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 1) {
  1005. usb_buffer_clear(icarus);
  1006. icarus->usbdev->ident = info->ident = IDENT_ANU;
  1007. info->nonce_size = ANT_READ_SIZE;
  1008. info->Hs = ANTMINERUSB_HASH_TIME;
  1009. icarus->drv->name = "ANU";
  1010. applog(LOG_DEBUG, "%s %i: Detected Antminer U1/2, changing nonce size to %d",
  1011. icarus->drv->name, icarus->device_id, ANT_READ_SIZE);
  1012. }
  1013. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  1014. if (strncmp(nonce_hex, golden_nonce, 8) == 0) {
  1015. if (info->ident == IDENT_ANU && !anu_freqset)
  1016. anu_freqset = true;
  1017. else
  1018. ok = true;
  1019. } else {
  1020. if (tries < 0 && info->ident != IDENT_CMR2) {
  1021. applog(LOG_ERR,
  1022. "Icarus Detect: "
  1023. "Test failed at %s: get %s, should: %s",
  1024. icarus->device_path, nonce_hex, golden_nonce);
  1025. }
  1026. }
  1027. free(nonce_hex);
  1028. }
  1029. if (!ok) {
  1030. if (info->ident != IDENT_CMR2)
  1031. goto unshin;
  1032. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  1033. info->intinfo++;
  1034. goto cmr2_retry;
  1035. }
  1036. } else {
  1037. if (info->ident == IDENT_CMR2) {
  1038. applog(LOG_DEBUG,
  1039. "Icarus Detect: "
  1040. "Test succeeded at %s i%d: got %s",
  1041. icarus->device_path, info->intinfo, golden_nonce);
  1042. cmr2_ok[info->intinfo] = true;
  1043. cmr2_count++;
  1044. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  1045. info->intinfo++;
  1046. goto cmr2_retry;
  1047. }
  1048. }
  1049. }
  1050. if (info->ident == IDENT_CMR2) {
  1051. if (cmr2_count == 0) {
  1052. applog(LOG_ERR,
  1053. "Icarus Detect: Test failed at %s: for all %d CMR2 Interfaces",
  1054. icarus->device_path, CAIRNSMORE2_INTS);
  1055. goto unshin;
  1056. }
  1057. // set the interface to the first one that succeeded
  1058. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  1059. if (cmr2_ok[i]) {
  1060. info->intinfo = i;
  1061. break;
  1062. }
  1063. } else {
  1064. applog(LOG_DEBUG,
  1065. "Icarus Detect: "
  1066. "Test succeeded at %s: got %s",
  1067. icarus->device_path, golden_nonce);
  1068. }
  1069. /* We have a real Icarus! */
  1070. if (!add_cgpu(icarus))
  1071. goto unshin;
  1072. update_usb_stats(icarus);
  1073. applog(LOG_INFO, "%s%d: Found at %s",
  1074. icarus->drv->name, icarus->device_id, icarus->device_path);
  1075. if (info->ident == IDENT_CMR2) {
  1076. applog(LOG_INFO, "%s%d: with %d Interface%s",
  1077. icarus->drv->name, icarus->device_id,
  1078. cmr2_count, cmr2_count > 1 ? "s" : "");
  1079. // Assume 1 or 2 are running FPGA pairs
  1080. if (cmr2_count < 3) {
  1081. work_division = fpga_count = 2;
  1082. info->Hs /= 2;
  1083. }
  1084. }
  1085. applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
  1086. icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
  1087. info->baud = baud;
  1088. info->work_division = work_division;
  1089. info->fpga_count = fpga_count;
  1090. info->nonce_mask = mask(work_division);
  1091. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  1092. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  1093. set_timing_mode(this_option_offset, icarus);
  1094. if (info->ident == IDENT_CMR2) {
  1095. int i;
  1096. for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count; i++) {
  1097. struct cgpu_info *cgtmp;
  1098. struct ICARUS_INFO *intmp;
  1099. if (!cmr2_ok[i])
  1100. continue;
  1101. cgtmp = usb_copy_cgpu(icarus);
  1102. if (!cgtmp) {
  1103. applog(LOG_ERR, "%s%d: Init failed initinfo %d",
  1104. icarus->drv->name, icarus->device_id, i);
  1105. continue;
  1106. }
  1107. cgtmp->usbinfo.usbstat = USB_NOSTAT;
  1108. intmp = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  1109. if (unlikely(!intmp))
  1110. quit(1, "Failed2 to malloc ICARUS_INFO");
  1111. cgtmp->device_data = (void *)intmp;
  1112. // Initialise everything to match
  1113. memcpy(intmp, info, sizeof(struct ICARUS_INFO));
  1114. intmp->intinfo = i;
  1115. icarus_initialise(cgtmp, baud);
  1116. if (!add_cgpu(cgtmp)) {
  1117. usb_uninit(cgtmp);
  1118. free(intmp);
  1119. continue;
  1120. }
  1121. update_usb_stats(cgtmp);
  1122. }
  1123. }
  1124. return icarus;
  1125. unshin:
  1126. usb_uninit(icarus);
  1127. free(info);
  1128. icarus->device_data = NULL;
  1129. shin:
  1130. icarus = usb_free_cgpu(icarus);
  1131. return NULL;
  1132. }
  1133. static int64_t rock_scanwork(struct thr_info *thr);
  1134. static void rock_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  1135. {
  1136. if (cgpu->temp)
  1137. tailsprintf(buf, bufsiz, "%3.0fMHz %3.0fC", opt_rock_freq, cgpu->temp);
  1138. else
  1139. tailsprintf(buf, bufsiz, "%.0fMHz", opt_rock_freq);
  1140. }
  1141. static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  1142. {
  1143. struct ICARUS_INFO *info;
  1144. struct timeval tv_start, tv_finish;
  1145. char *ob_hex = NULL;
  1146. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  1147. // N.B. golden_ob MUST take less time to calculate
  1148. // than the timeout set in icarus_open()
  1149. // This one takes ~0.53ms on Rev3 Icarus
  1150. const char golden_ob[] =
  1151. "4679ba4ec99876bf4bfe086082b40025"
  1152. "4df6c356451471139a3afa71e48f544a"
  1153. "00000000000000000000000000000000"
  1154. "aa1ff05587320b1a1426674f2fa722ce";
  1155. const char golden_nonce[] = "000187a2";
  1156. const uint32_t golden_nonce_val = 0x000187a2;
  1157. unsigned char nonce_bin[ROCK_READ_SIZE];
  1158. struct ICARUS_WORK workdata;
  1159. char *nonce_hex;
  1160. struct cgpu_info *icarus;
  1161. int ret, err, amount, tries;
  1162. bool ok;
  1163. int correction_times = 0;
  1164. NONCE_DATA nonce_data;
  1165. uint32_t nonce;
  1166. char *newname = NULL;
  1167. if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
  1168. quithere(1, "Data and golden_ob sizes don't match");
  1169. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  1170. if (!usb_init(icarus, dev, found))
  1171. goto shin;
  1172. hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
  1173. rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
  1174. rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
  1175. if (opt_debug) {
  1176. ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
  1177. applog(LOG_WARNING, "%s%d: send_gold_nonce %s",
  1178. icarus->drv->name, icarus->device_id, ob_hex);
  1179. free(ob_hex);
  1180. }
  1181. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  1182. if (unlikely(!info))
  1183. quit(1, "Failed to malloc ICARUS_INFO");
  1184. (void)memset(info, 0, sizeof(struct ICARUS_INFO));
  1185. icarus->device_data = (void *)info;
  1186. icarus->usbdev->ident = info->ident = IDENT_LIN;
  1187. info->nonce_size = ROCK_READ_SIZE;
  1188. info->fail_time = 10;
  1189. info->nonce_mask = 0xffffffff;
  1190. update_usb_stats(icarus);
  1191. tries = MAX_TRIES;
  1192. ok = false;
  1193. while (!ok && tries-- > 0) {
  1194. icarus_initialise(icarus, info->baud);
  1195. applog(LOG_DEBUG, "tries: %d", tries);
  1196. workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
  1197. workdata.unused[ICARUS_UNUSED_SIZE - 2] = (MAX_TRIES-1-tries);
  1198. info->rmdev.detect_chip_no++;
  1199. if (info->rmdev.detect_chip_no >= MAX_TRIES)
  1200. info->rmdev.detect_chip_no = 0;
  1201. //g_detect_chip_no = (g_detect_chip_no + 1) & MAX_CHIP_NUM;
  1202. err = usb_write_ii(icarus, info->intinfo,
  1203. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1204. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  1205. continue;
  1206. memset(nonce_bin, 0, sizeof(nonce_bin));
  1207. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  1208. applog(LOG_DEBUG, "Rockminer nonce_bin: %02x %02x %02x %02x %02x %02x %02x %02x",
  1209. nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3],
  1210. nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
  1211. if (ret != ICA_NONCE_OK) {
  1212. applog(LOG_DEBUG, "detect_one get_gold_nonce error, tries = %d", tries);
  1213. continue;
  1214. }
  1215. newname = NULL;
  1216. switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) {
  1217. case RM_PRODUCT_T1:
  1218. newname = "LIR"; // Rocketbox
  1219. info->rmdev.product_id = ROCKMINER_T1;
  1220. info->rmdev.chip_max = 12;
  1221. info->rmdev.min_frq = 200;
  1222. info->rmdev.def_frq = 300;
  1223. info->rmdev.max_frq = 400;
  1224. break;
  1225. case RM_PRODUCT_T2: // what's this?
  1226. newname = "LIX";
  1227. info->rmdev.product_id = ROCKMINER_T2;
  1228. info->rmdev.chip_max = 16;
  1229. info->rmdev.min_frq = 200;
  1230. info->rmdev.def_frq = 300;
  1231. info->rmdev.max_frq = 400;
  1232. break;
  1233. case RM_PRODUCT_RBOX:
  1234. newname = "LIN"; // R-Box
  1235. info->rmdev.product_id = ROCKMINER_RBOX;
  1236. info->rmdev.chip_max = 4;
  1237. info->rmdev.min_frq = 200;
  1238. info->rmdev.def_frq = 270;
  1239. info->rmdev.max_frq = 400;
  1240. break;
  1241. default:
  1242. continue;
  1243. }
  1244. snprintf(info->rock_init, sizeof(info->rock_init), "%02x %02x %02x %02x",
  1245. nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]);
  1246. nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
  1247. if (nonce_data.chip_no >= info->rmdev.chip_max)
  1248. nonce_data.chip_no = 0;
  1249. nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
  1250. if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
  1251. applog(LOG_DEBUG, "complete g_detect_chip_no: %d", info->rmdev.detect_chip_no);
  1252. workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1;
  1253. workdata.unused[ICARUS_UNUSED_SIZE - 2] = info->rmdev.detect_chip_no;
  1254. info->rmdev.detect_chip_no++;
  1255. if (info->rmdev.detect_chip_no >= MAX_TRIES)
  1256. info->rmdev.detect_chip_no = 0;
  1257. err = usb_write_ii(icarus, info->intinfo,
  1258. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1259. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  1260. continue;
  1261. applog(LOG_DEBUG, "send_gold_nonce usb_write_ii");
  1262. continue;
  1263. }
  1264. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1265. nonce = htobe32(nonce);
  1266. applog(LOG_DEBUG, "Rockminer nonce: %08X", nonce);
  1267. correction_times = 0;
  1268. while (correction_times < NONCE_CORRECTION_TIMES) {
  1269. nonce_hex = bin2hex(nonce_bin, 4);
  1270. if (golden_nonce_val == nonce + rbox_corr_values[correction_times]) {
  1271. memset(&(info->g_work[0]), 0, sizeof(info->g_work));
  1272. rock_init_last_received_task_complete_time(info);
  1273. ok = true;
  1274. break;
  1275. } else {
  1276. applog(LOG_DEBUG, "detect_one gold_nonce compare error times = %d",
  1277. correction_times);
  1278. if (tries < 0 && info->ident != IDENT_CMR2) {
  1279. applog(LOG_WARNING,
  1280. "Icarus Detect: "
  1281. "Test failed at %s: get %s, should: %s",
  1282. icarus->device_path, nonce_hex, golden_nonce);
  1283. }
  1284. if (nonce == 0)
  1285. break;
  1286. }
  1287. free(nonce_hex);
  1288. correction_times++;
  1289. }
  1290. }
  1291. if (!ok)
  1292. goto unshin;
  1293. if (newname) {
  1294. if (!icarus->drv->copy)
  1295. icarus->drv = copy_drv(icarus->drv);
  1296. icarus->drv->name = newname;
  1297. }
  1298. applog(LOG_DEBUG, "Icarus Detect: Test succeeded at %s: got %s",
  1299. icarus->device_path, golden_nonce);
  1300. /* We have a real Rockminer! */
  1301. if (!add_cgpu(icarus))
  1302. goto unshin;
  1303. icarus->drv->scanwork = rock_scanwork;
  1304. icarus->drv->dname = "Rockminer";
  1305. icarus->drv->get_statline_before = &rock_statline_before;
  1306. applog(LOG_INFO, "%s%d: Found at %s",
  1307. icarus->drv->name, icarus->device_id,
  1308. icarus->device_path);
  1309. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  1310. return icarus;
  1311. unshin:
  1312. usb_uninit(icarus);
  1313. free(info);
  1314. icarus->device_data = NULL;
  1315. shin:
  1316. icarus = usb_free_cgpu(icarus);
  1317. return NULL;
  1318. }
  1319. static void icarus_detect(bool __maybe_unused hotplug)
  1320. {
  1321. usb_detect(&icarus_drv, rock_detect_one);
  1322. usb_detect(&icarus_drv, icarus_detect_one);
  1323. }
  1324. static bool icarus_prepare(__maybe_unused struct thr_info *thr)
  1325. {
  1326. // struct cgpu_info *icarus = thr->cgpu;
  1327. return true;
  1328. }
  1329. static void cmr2_command(struct cgpu_info *icarus, uint8_t cmd, uint8_t data)
  1330. {
  1331. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1332. struct ICARUS_WORK workdata;
  1333. int amount;
  1334. memset((void *)(&workdata), 0, sizeof(workdata));
  1335. workdata.prefix = ICARUS_CMR2_PREFIX;
  1336. workdata.cmd = cmd;
  1337. workdata.data = data;
  1338. workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^ ICARUS_CMR2_CHECK;
  1339. usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1340. }
  1341. static void cmr2_commands(struct cgpu_info *icarus)
  1342. {
  1343. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1344. if (info->speed_next_work) {
  1345. info->speed_next_work = false;
  1346. cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed);
  1347. return;
  1348. }
  1349. if (info->flash_next_work) {
  1350. info->flash_next_work = false;
  1351. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  1352. cgsleep_ms(250);
  1353. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  1354. cgsleep_ms(250);
  1355. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  1356. cgsleep_ms(250);
  1357. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  1358. return;
  1359. }
  1360. }
  1361. void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct thr_info *thr)
  1362. {
  1363. struct cgpu_info *icarus = thr->cgpu;
  1364. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1365. int err, amount;
  1366. struct ICARUS_WORK workdata;
  1367. char *ob_hex;
  1368. struct work *work = NULL;
  1369. if (info->g_work[chip_no][current_task_id] == NULL) {
  1370. work = get_work(thr, thr->id);
  1371. if (work == NULL)
  1372. return;
  1373. info->g_work[chip_no][current_task_id] = work;
  1374. } else {
  1375. work = info->g_work[chip_no][current_task_id];
  1376. applog(LOG_DEBUG, "::resend work");
  1377. }
  1378. memset((void *)(&workdata), 0, sizeof(workdata));
  1379. memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
  1380. memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
  1381. workdata.unused[ICARUS_UNUSED_SIZE - 4] = 0xaa;
  1382. if (info->rmdev.chip[chip_no].freq > (info->rmdev.max_frq/10 - 1) ||
  1383. info->rmdev.chip[chip_no].freq < (info->rmdev.min_frq/10 - 1))
  1384. rock_init_last_received_task_complete_time(info);
  1385. workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq; //icarus->freq/10 - 1; ;
  1386. workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ;
  1387. workdata.unused[ICARUS_UNUSED_SIZE - 1] = 0x55;
  1388. if (opt_debug) {
  1389. ob_hex = bin2hex((void *)(work->data), 128);
  1390. applog(LOG_WARNING, "%s%d: work->data %s",
  1391. icarus->drv->name, icarus->device_id, ob_hex);
  1392. free(ob_hex);
  1393. }
  1394. // We only want results for the work we are about to send
  1395. usb_buffer_clear(icarus);
  1396. err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1397. if (err < 0 || amount != sizeof(workdata)) {
  1398. applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
  1399. icarus->drv->name, icarus->device_id, err, amount);
  1400. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  1401. icarus_initialise(icarus, info->baud);
  1402. if (info->g_work[chip_no][current_task_id])
  1403. {
  1404. free_work(info->g_work[chip_no][current_task_id]);
  1405. info->g_work[chip_no][current_task_id] = NULL;
  1406. }
  1407. return;
  1408. }
  1409. return;
  1410. }
  1411. static void process_history(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint32_t nonce,
  1412. uint64_t hash_count, struct timeval *elapsed, struct timeval *tv_start)
  1413. {
  1414. struct ICARUS_HISTORY *history0, *history;
  1415. struct timeval tv_history_start, tv_history_finish;
  1416. int count;
  1417. double Hs, W, fullnonce;
  1418. int read_time, i;
  1419. bool limited;
  1420. uint32_t values;
  1421. int64_t hash_count_range;
  1422. double Ti, Xi;
  1423. // Ignore possible end condition values ...
  1424. // TODO: set limitations on calculated values depending on the device
  1425. // to avoid crap values caused by CPU/Task Switching/Swapping/etc
  1426. if ((nonce & info->nonce_mask) <= END_CONDITION ||
  1427. (nonce & info->nonce_mask) >= (info->nonce_mask & ~END_CONDITION))
  1428. return;
  1429. cgtime(&tv_history_start);
  1430. history0 = &(info->history[0]);
  1431. if (history0->values == 0)
  1432. timeradd(tv_start, &history_sec, &(history0->finish));
  1433. Ti = (double)(elapsed->tv_sec)
  1434. + ((double)(elapsed->tv_usec))/((double)1000000)
  1435. - ((double)ICARUS_READ_TIME(info->baud));
  1436. Xi = (double)hash_count;
  1437. history0->sumXiTi += Xi * Ti;
  1438. history0->sumXi += Xi;
  1439. history0->sumTi += Ti;
  1440. history0->sumXi2 += Xi * Xi;
  1441. history0->values++;
  1442. if (history0->hash_count_max < hash_count)
  1443. history0->hash_count_max = hash_count;
  1444. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  1445. history0->hash_count_min = hash_count;
  1446. if (history0->values >= info->min_data_count
  1447. && timercmp(tv_start, &(history0->finish), >)) {
  1448. for (i = INFO_HISTORY; i > 0; i--)
  1449. memcpy(&(info->history[i]),
  1450. &(info->history[i-1]),
  1451. sizeof(struct ICARUS_HISTORY));
  1452. // Initialise history0 to zero for summary calculation
  1453. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1454. // We just completed a history data set
  1455. // So now recalc read_time based on the whole history thus we will
  1456. // initially get more accurate until it completes INFO_HISTORY
  1457. // total data sets
  1458. count = 0;
  1459. for (i = 1 ; i <= INFO_HISTORY; i++) {
  1460. history = &(info->history[i]);
  1461. if (history->values >= MIN_DATA_COUNT) {
  1462. count++;
  1463. history0->sumXiTi += history->sumXiTi;
  1464. history0->sumXi += history->sumXi;
  1465. history0->sumTi += history->sumTi;
  1466. history0->sumXi2 += history->sumXi2;
  1467. history0->values += history->values;
  1468. if (history0->hash_count_max < history->hash_count_max)
  1469. history0->hash_count_max = history->hash_count_max;
  1470. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  1471. history0->hash_count_min = history->hash_count_min;
  1472. }
  1473. }
  1474. // All history data
  1475. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  1476. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  1477. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  1478. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  1479. values = history0->values;
  1480. // Initialise history0 to zero for next data set
  1481. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1482. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  1483. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  1484. if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
  1485. read_time = info->read_time_limit;
  1486. limited = true;
  1487. } else
  1488. limited = false;
  1489. info->Hs = Hs;
  1490. info->read_time = read_time;
  1491. info->fullnonce = fullnonce;
  1492. info->count = count;
  1493. info->W = W;
  1494. info->values = values;
  1495. info->hash_count_range = hash_count_range;
  1496. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  1497. info->min_data_count *= 2;
  1498. else if (info->timing_mode == MODE_SHORT)
  1499. info->do_icarus_timing = false;
  1500. applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
  1501. icarus->drv->name, icarus->device_id, Hs, W, read_time,
  1502. limited ? " (limited)" : "", fullnonce);
  1503. }
  1504. info->history_count++;
  1505. cgtime(&tv_history_finish);
  1506. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  1507. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  1508. }
  1509. static int64_t icarus_scanwork(struct thr_info *thr)
  1510. {
  1511. struct cgpu_info *icarus = thr->cgpu;
  1512. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1513. int ret, err, amount;
  1514. unsigned char nonce_bin[ICARUS_BUF_SIZE];
  1515. struct ICARUS_WORK workdata;
  1516. char *ob_hex;
  1517. uint32_t nonce;
  1518. int64_t hash_count = 0;
  1519. struct timeval tv_start, tv_finish, elapsed;
  1520. int curr_hw_errors;
  1521. bool was_hw_error;
  1522. struct work *work;
  1523. int64_t estimate_hashes;
  1524. if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
  1525. if (info->failing) {
  1526. if (share_work_tdiff(icarus) > info->fail_time + 60) {
  1527. applog(LOG_ERR, "%s %d: Device failed to respond to restart",
  1528. icarus->drv->name, icarus->device_id);
  1529. usb_nodev(icarus);
  1530. return -1;
  1531. }
  1532. } else {
  1533. applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset",
  1534. icarus->drv->name, icarus->device_id, info->fail_time);
  1535. usb_reset(icarus);
  1536. info->failing = true;
  1537. }
  1538. }
  1539. // Device is gone
  1540. if (icarus->usbinfo.nodev)
  1541. return -1;
  1542. elapsed.tv_sec = elapsed.tv_usec = 0;
  1543. work = get_work(thr, thr->id);
  1544. memset((void *)(&workdata), 0, sizeof(workdata));
  1545. memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
  1546. memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
  1547. rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
  1548. rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
  1549. if (info->speed_next_work || info->flash_next_work)
  1550. cmr2_commands(icarus);
  1551. // We only want results for the work we are about to send
  1552. usb_buffer_clear(icarus);
  1553. err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  1554. if (err < 0 || amount != sizeof(workdata)) {
  1555. applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
  1556. icarus->drv->name, icarus->device_id, err, amount);
  1557. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  1558. icarus_initialise(icarus, info->baud);
  1559. goto out;
  1560. }
  1561. if (opt_debug) {
  1562. ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
  1563. applog(LOG_DEBUG, "%s%d: sent %s",
  1564. icarus->drv->name, icarus->device_id, ob_hex);
  1565. free(ob_hex);
  1566. }
  1567. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  1568. memset(nonce_bin, 0, sizeof(nonce_bin));
  1569. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  1570. if (ret == ICA_NONCE_ERROR)
  1571. goto out;
  1572. // aborted before becoming idle, get new work
  1573. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  1574. timersub(&tv_finish, &tv_start, &elapsed);
  1575. // ONLY up to just when it aborted
  1576. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  1577. estimate_hashes = ((double)(elapsed.tv_sec)
  1578. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  1579. // If some Serial-USB delay allowed the full nonce range to
  1580. // complete it can't have done more than a full nonce
  1581. if (unlikely(estimate_hashes > 0xffffffff))
  1582. estimate_hashes = 0xffffffff;
  1583. applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  1584. icarus->drv->name, icarus->device_id,
  1585. (long unsigned int)estimate_hashes,
  1586. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1587. hash_count = estimate_hashes;
  1588. goto out;
  1589. }
  1590. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1591. nonce = htobe32(nonce);
  1592. curr_hw_errors = icarus->hw_errors;
  1593. if (submit_nonce(thr, work, nonce))
  1594. info->failing = false;
  1595. was_hw_error = (curr_hw_errors < icarus->hw_errors);
  1596. if (was_hw_error)
  1597. hash_count = 0;
  1598. else {
  1599. hash_count = (nonce & info->nonce_mask);
  1600. hash_count++;
  1601. hash_count *= info->fpga_count;
  1602. }
  1603. #if 0
  1604. // This appears to only return zero nonce values
  1605. if (usb_buffer_size(icarus) > 3) {
  1606. memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin));
  1607. nonce = htobe32(nonce);
  1608. applog(LOG_WARNING, "%s%d: attempting to submit 2nd nonce = 0x%08lX",
  1609. icarus->drv->name, icarus->device_id,
  1610. (long unsigned int)nonce);
  1611. curr_hw_errors = icarus->hw_errors;
  1612. submit_nonce(thr, work, nonce);
  1613. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  1614. }
  1615. #endif
  1616. if (opt_debug || info->do_icarus_timing)
  1617. timersub(&tv_finish, &tv_start, &elapsed);
  1618. applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  1619. icarus->drv->name, icarus->device_id,
  1620. nonce, (long unsigned int)hash_count,
  1621. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1622. if (info->do_icarus_timing && !was_hw_error)
  1623. process_history(icarus, info, nonce, hash_count, &elapsed, &tv_start);
  1624. out:
  1625. free_work(work);
  1626. return hash_count;
  1627. }
  1628. static int64_t rock_scanwork(struct thr_info *thr)
  1629. {
  1630. struct cgpu_info *icarus = thr->cgpu;
  1631. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  1632. int ret;
  1633. unsigned char nonce_bin[ICARUS_BUF_SIZE];
  1634. uint32_t nonce;
  1635. int64_t hash_count = 0;
  1636. struct timeval tv_start, tv_finish, elapsed;
  1637. struct work *work = NULL;
  1638. int64_t estimate_hashes;
  1639. int correction_times = 0;
  1640. NONCE_DATA nonce_data;
  1641. int chip_no = 0;
  1642. time_t recv_time = 0;
  1643. if (unlikely(share_work_tdiff(icarus) > info->fail_time)) {
  1644. if (info->failing) {
  1645. if (share_work_tdiff(icarus) > info->fail_time + 60) {
  1646. applog(LOG_ERR, "%s %d: Device failed to respond to restart",
  1647. icarus->drv->name, icarus->device_id);
  1648. usb_nodev(icarus);
  1649. return -1;
  1650. }
  1651. } else {
  1652. applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset",
  1653. icarus->drv->name, icarus->device_id, info->fail_time);
  1654. usb_reset(icarus);
  1655. info->failing = true;
  1656. }
  1657. }
  1658. // Device is gone
  1659. if (icarus->usbinfo.nodev)
  1660. return -1;
  1661. elapsed.tv_sec = elapsed.tv_usec = 0;
  1662. for (chip_no = 0; chip_no < info->rmdev.chip_max; chip_no++) {
  1663. recv_time = time(NULL);
  1664. if (recv_time > info->rmdev.chip[chip_no].last_received_task_complete_time + 1) {
  1665. info->rmdev.chip[chip_no].last_received_task_complete_time = recv_time;
  1666. rock_send_task(chip_no, 0,thr);
  1667. break;
  1668. }
  1669. }
  1670. memset(nonce_bin, 0, sizeof(nonce_bin));
  1671. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, 3000);//info->read_time);
  1672. nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK;
  1673. if (nonce_data.chip_no >= info->rmdev.chip_max)
  1674. nonce_data.chip_no = 0;
  1675. nonce_data.task_no = (nonce_bin[NONCE_TASK_NO_OFFSET] >= 2) ? 0 : nonce_bin[NONCE_TASK_NO_OFFSET];
  1676. nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK;
  1677. nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_STATUS_MASK;
  1678. icarus->temp = (double)nonce_bin[NONCE_COMMAND_OFFSET];
  1679. if (icarus->temp == 128)
  1680. icarus->temp = 0;
  1681. if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
  1682. info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL);
  1683. if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) {
  1684. free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]);
  1685. info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL;
  1686. }
  1687. goto out;
  1688. }
  1689. if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) {
  1690. rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
  1691. goto out;
  1692. }
  1693. if (ret == ICA_NONCE_TIMEOUT)
  1694. rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
  1695. work = info->g_work[nonce_data.chip_no][nonce_data.task_no];
  1696. if (work == NULL)
  1697. goto out;
  1698. if (ret == ICA_NONCE_ERROR)
  1699. goto out;
  1700. // aborted before becoming idle, get new work
  1701. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  1702. timersub(&tv_finish, &tv_start, &elapsed);
  1703. // ONLY up to just when it aborted
  1704. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  1705. estimate_hashes = ((double)(elapsed.tv_sec)
  1706. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  1707. // If some Serial-USB delay allowed the full nonce range to
  1708. // complete it can't have done more than a full nonce
  1709. if (unlikely(estimate_hashes > 0xffffffff))
  1710. estimate_hashes = 0xffffffff;
  1711. applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  1712. icarus->drv->name, icarus->device_id,
  1713. (long unsigned int)estimate_hashes,
  1714. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1715. goto out;
  1716. }
  1717. memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE);
  1718. nonce = htobe32(nonce);
  1719. recv_time = time(NULL);
  1720. if ((recv_time-info->rmdev.dev_detect_time) >= 60) {
  1721. unsigned char i;
  1722. info->rmdev.dev_detect_time = recv_time;
  1723. for (i = 0; i < info->rmdev.chip_max; i ++) {
  1724. if (info->rmdev.chip[i].error_cnt >= 12) {
  1725. if (info->rmdev.chip[i].freq > info->rmdev.min_frq)
  1726. info->rmdev.chip[i].freq--;
  1727. } else if (info->rmdev.chip[i].error_cnt <= 1) {
  1728. if (info->rmdev.chip[i].freq < (info->rmdev.def_frq / 10 - 1))
  1729. info->rmdev.chip[i].freq++;
  1730. }
  1731. info->rmdev.chip[i].error_cnt = 0;
  1732. }
  1733. }
  1734. correction_times = 0;
  1735. info->nonces_checked++;
  1736. while (correction_times < NONCE_CORRECTION_TIMES) {
  1737. if (correction_times > 0) {
  1738. info->nonces_correction_tests++;
  1739. if (correction_times == 1)
  1740. info->nonces_correction_times++;
  1741. }
  1742. if (submit_nonce(thr, work, nonce + rbox_corr_values[correction_times])) {
  1743. info->nonces_correction[correction_times]++;
  1744. hash_count++;
  1745. info->failing = false;
  1746. applog(LOG_DEBUG, "Rockminer nonce :::OK:::");
  1747. break;
  1748. } else {
  1749. applog(LOG_DEBUG, "Rockminer nonce error times = %d", correction_times);
  1750. if (nonce == 0)
  1751. break;
  1752. }
  1753. correction_times++;
  1754. }
  1755. if (correction_times >= NONCE_CORRECTION_TIMES)
  1756. info->nonces_fail++;
  1757. hash_count = (hash_count * info->nonce_mask);
  1758. if (opt_debug || info->do_icarus_timing)
  1759. timersub(&tv_finish, &tv_start, &elapsed);
  1760. applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  1761. icarus->drv->name, icarus->device_id,
  1762. nonce, (long unsigned int)hash_count,
  1763. (long)elapsed.tv_sec, (long)elapsed.tv_usec);
  1764. out:
  1765. return hash_count;
  1766. }
  1767. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  1768. {
  1769. struct api_data *root = NULL;
  1770. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1771. char data[4096];
  1772. int i, off;
  1773. size_t len;
  1774. float avg;
  1775. // Warning, access to these is not locked - but we don't really
  1776. // care since hashing performance is way more important than
  1777. // locking access to displaying API debug 'stats'
  1778. // If locking becomes an issue for any of them, use copy_data=true also
  1779. root = api_add_int(root, "read_time", &(info->read_time), false);
  1780. root = api_add_int(root, "read_time_limit", &(info->read_time_limit), false);
  1781. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1782. root = api_add_int(root, "count", &(info->count), false);
  1783. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1784. root = api_add_double(root, "W", &(info->W), false);
  1785. root = api_add_uint(root, "total_values", &(info->values), false);
  1786. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1787. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1788. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1789. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1790. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1791. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1792. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1793. root = api_add_int(root, "baud", &(info->baud), false);
  1794. root = api_add_int(root, "work_division", &(info->work_division), false);
  1795. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1796. if (info->ident == IDENT_LIN) {
  1797. root = api_add_string(root, "rock_init", info->rock_init, false);
  1798. root = api_add_uint8(root, "rock_chips", &(info->rmdev.detect_chip_no), false);
  1799. root = api_add_uint8(root, "rock_chip_max", &(info->rmdev.chip_max), false);
  1800. root = api_add_uint8(root, "rock_prod_id", &(info->rmdev.product_id), false);
  1801. root = api_add_avg(root, "rock_min_freq", &(info->rmdev.min_frq), false);
  1802. root = api_add_avg(root, "rock_max_freq", &(info->rmdev.max_frq), false);
  1803. root = api_add_uint64(root, "rock_check", &(info->nonces_checked), false);
  1804. root = api_add_uint64(root, "rock_corr", &(info->nonces_correction_times), false);
  1805. root = api_add_uint64(root, "rock_corr_tests", &(info->nonces_correction_tests), false);
  1806. root = api_add_uint64(root, "rock_corr_fail", &(info->nonces_fail), false);
  1807. if (info->nonces_checked <= 0)
  1808. avg = 0;
  1809. else
  1810. avg = (float)(info->nonces_correction_tests) / (float)(info->nonces_checked);
  1811. root = api_add_avg(root, "rock_corr_avg", &avg, true);
  1812. data[0] = '\0';
  1813. off = 0;
  1814. for (i = 0; i < NONCE_CORRECTION_TIMES; i++) {
  1815. len = snprintf(data+off, sizeof(data)-off,
  1816. "%s%"PRIu64,
  1817. i > 0 ? "/" : "",
  1818. info->nonces_correction[i]);
  1819. if (len >= (sizeof(data)-off))
  1820. off = sizeof(data)-1;
  1821. else {
  1822. if (len > 0)
  1823. off += len;
  1824. }
  1825. }
  1826. root = api_add_string(root, "rock_corr_finds", data, true);
  1827. }
  1828. return root;
  1829. }
  1830. static void icarus_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  1831. {
  1832. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1833. if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0)
  1834. tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) * ICARUS_CMR2_SPEED_FACTOR);
  1835. }
  1836. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  1837. {
  1838. // TODO: ?
  1839. }
  1840. static void icarus_identify(struct cgpu_info *cgpu)
  1841. {
  1842. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1843. if (info->ident == IDENT_CMR2)
  1844. info->flash_next_work = true;
  1845. }
  1846. static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  1847. {
  1848. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1849. int val;
  1850. if (info->ident != IDENT_CMR2) {
  1851. strcpy(replybuf, "no set options available");
  1852. return replybuf;
  1853. }
  1854. if (strcasecmp(option, "help") == 0) {
  1855. sprintf(replybuf, "clock: range %d-%d",
  1856. ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT);
  1857. return replybuf;
  1858. }
  1859. if (strcasecmp(option, "clock") == 0) {
  1860. if (!setting || !*setting) {
  1861. sprintf(replybuf, "missing clock setting");
  1862. return replybuf;
  1863. }
  1864. val = atoi(setting);
  1865. if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT) {
  1866. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
  1867. setting,
  1868. ICARUS_CMR2_SPEED_MIN_INT,
  1869. ICARUS_CMR2_SPEED_MAX_INT);
  1870. }
  1871. info->cmr2_speed = CMR2_INT_TO_SPEED(val);
  1872. info->speed_next_work = true;
  1873. return NULL;
  1874. }
  1875. sprintf(replybuf, "Unknown option: %s", option);
  1876. return replybuf;
  1877. }
  1878. struct device_drv icarus_drv = {
  1879. .drv_id = DRIVER_icarus,
  1880. .dname = "Icarus",
  1881. .name = "ICA",
  1882. .drv_detect = icarus_detect,
  1883. .hash_work = &hash_driver_work,
  1884. .get_api_stats = icarus_api_stats,
  1885. .get_statline_before = icarus_statline_before,
  1886. .set_device = icarus_set,
  1887. .identify_device = icarus_identify,
  1888. .thread_prepare = icarus_prepare,
  1889. .scanwork = icarus_scanwork,
  1890. .thread_shutdown = icarus_shutdown,
  1891. };