driver-bitmain.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * Copyright 2012-2014 Lingchao Xu
  3. * Copyright 2015 Luke Dashjr
  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 <math.h>
  13. #include <pthread.h>
  14. #include <stdio.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <dirent.h>
  18. #include <unistd.h>
  19. #ifndef WIN32
  20. #include <sys/select.h>
  21. #include <termios.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #ifndef O_CLOEXEC
  25. #define O_CLOEXEC 0
  26. #endif
  27. #else
  28. #include "compat.h"
  29. #include <windows.h>
  30. #include <io.h>
  31. #endif
  32. #include <curl/curl.h>
  33. #include <uthash.h>
  34. #include "deviceapi.h"
  35. #include "miner.h"
  36. #include "driver-bitmain.h"
  37. #include "lowl-vcom.h"
  38. #include "util.h"
  39. const bool opt_bitmain_hwerror = true;
  40. BFG_REGISTER_DRIVER(bitmain_drv)
  41. static const struct bfg_set_device_definition bitmain_set_device_funcs_init[];
  42. #define htole8(x) (x)
  43. #define BITMAIN_USING_CURL -2
  44. static
  45. struct cgpu_info *btm_alloc_cgpu(struct device_drv *drv, int threads)
  46. {
  47. struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
  48. if (unlikely(!cgpu))
  49. quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname);
  50. cgpu->drv = drv;
  51. cgpu->deven = DEV_ENABLED;
  52. cgpu->threads = threads;
  53. cgpu->device_fd = -1;
  54. struct bitmain_info *info = malloc(sizeof(*info));
  55. if (unlikely(!info))
  56. quit(1, "Failed to calloc bitmain_info data");
  57. cgpu->device_data = info;
  58. *info = (struct bitmain_info){
  59. .baud = BITMAIN_IO_SPEED,
  60. .chain_num = BITMAIN_DEFAULT_CHAIN_NUM,
  61. .asic_num = BITMAIN_DEFAULT_ASIC_NUM,
  62. .timeout = BITMAIN_DEFAULT_TIMEOUT,
  63. .frequency = BITMAIN_DEFAULT_FREQUENCY,
  64. .voltage[0] = BITMAIN_DEFAULT_VOLTAGE0,
  65. .voltage[1] = BITMAIN_DEFAULT_VOLTAGE1,
  66. .diff = 255,
  67. .lowest_goal_diff = 255,
  68. };
  69. sprintf(info->frequency_t, "%d", BITMAIN_DEFAULT_FREQUENCY),
  70. strcpy(info->voltage_t, BITMAIN_DEFAULT_VOLTAGE_T);
  71. return cgpu;
  72. }
  73. static curl_socket_t bitmain_grab_socket_opensocket_cb(void *clientp, __maybe_unused curlsocktype purpose, struct curl_sockaddr *addr)
  74. {
  75. struct bitmain_info * const info = clientp;
  76. curl_socket_t sck = bfg_socket(addr->family, addr->socktype, addr->protocol);
  77. info->curl_sock = sck;
  78. return sck;
  79. }
  80. static
  81. bool btm_init(struct cgpu_info *cgpu, const char * devpath)
  82. {
  83. applog(LOG_DEBUG, "btm_init cgpu->device_fd=%d", cgpu->device_fd);
  84. int fd = -1;
  85. if(cgpu->device_fd >= 0) {
  86. return false;
  87. }
  88. struct bitmain_info *info = cgpu->device_data;
  89. if (!strncmp(devpath, "ip:", 3)) {
  90. CURL *curl = curl_easy_init();
  91. if (!curl)
  92. applogr(false, LOG_ERR, "%s: curl_easy_init failed", cgpu->drv->dname);
  93. // CURLINFO_LASTSOCKET is broken on Win64 (which has a wider SOCKET type than curl_easy_getinfo returns), so we use this hack for now
  94. info->curl_sock = -1;
  95. curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, bitmain_grab_socket_opensocket_cb);
  96. curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, info);
  97. curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
  98. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5);
  99. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  100. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  101. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
  102. curl_easy_setopt(curl, CURLOPT_URL, &devpath[3]);
  103. if (curl_easy_perform(curl)) {
  104. curl_easy_cleanup(curl);
  105. applogr(false, LOG_ERR, "%s: curl_easy_perform failed for %s", cgpu->drv->dname, &devpath[3]);
  106. }
  107. cgpu->device_path = strdup(devpath);
  108. cgpu->device_fd = BITMAIN_USING_CURL;
  109. info->device_curl = curl;
  110. return true;
  111. }
  112. fd = serial_open(devpath, info->baud, 1, true);
  113. if(fd == -1) {
  114. applog(LOG_DEBUG, "%s open %s error %d",
  115. cgpu->drv->dname, devpath, errno);
  116. return false;
  117. }
  118. cgpu->device_path = strdup(devpath);
  119. cgpu->device_fd = fd;
  120. applog(LOG_DEBUG, "btm_init open device fd = %d", cgpu->device_fd);
  121. return true;
  122. }
  123. static
  124. void btm_uninit(struct cgpu_info *cgpu)
  125. {
  126. struct bitmain_info * const info = cgpu->device_data;
  127. applog(LOG_DEBUG, "BTM uninit %s%i", cgpu->drv->name, cgpu->device_fd);
  128. // May have happened already during a failed initialisation
  129. // if release_cgpu() was called due to a USB NODEV(err)
  130. if (cgpu->device_fd >= 0) {
  131. serial_close(cgpu->device_fd);
  132. cgpu->device_fd = -1;
  133. }
  134. if (info->device_curl) {
  135. curl_easy_cleanup(info->device_curl);
  136. info->device_curl = NULL;
  137. }
  138. if(cgpu->device_path) {
  139. free((char*)cgpu->device_path);
  140. cgpu->device_path = NULL;
  141. }
  142. }
  143. bool bitmain_curl_all(const bool is_recv, const int fd, CURL * const curl, void *p, size_t remsz)
  144. {
  145. CURLcode (* const func)(CURL *, void *, size_t, size_t *) = is_recv ? (void*)curl_easy_recv : (void*)curl_easy_send;
  146. CURLcode r;
  147. size_t sz;
  148. while (remsz) {
  149. fd_set otherfds, thisfds;
  150. FD_ZERO(&otherfds);
  151. FD_ZERO(&thisfds);
  152. FD_SET(fd, &thisfds);
  153. select(fd + 1, is_recv ? &thisfds : &otherfds, is_recv ? &otherfds : &thisfds, &thisfds, NULL);
  154. r = func(curl, p, remsz, &sz);
  155. switch (r) {
  156. case CURLE_OK:
  157. remsz -= sz;
  158. p += sz;
  159. break;
  160. case CURLE_AGAIN:
  161. break;
  162. default:
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. static
  169. int btm_read(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
  170. {
  171. int err = 0;
  172. //applog(LOG_DEBUG, "btm_read ----- %d -----", bufsize);
  173. if (unlikely(cgpu->device_fd == BITMAIN_USING_CURL)) {
  174. struct bitmain_info * const info = cgpu->device_data;
  175. uint8_t headbuf[5];
  176. headbuf[0] = 0;
  177. pk_u32be(headbuf, 1, bufsize);
  178. if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, headbuf, sizeof(headbuf)))
  179. return -1;
  180. if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, headbuf, 4))
  181. return -1;
  182. if (headbuf[0] == 0xff && headbuf[1] == 0xff && headbuf[2] == 0xff && headbuf[3] == 0xff)
  183. return -1;
  184. size_t sz = upk_u32be(headbuf, 0);
  185. if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, buf, sz))
  186. return -1;
  187. return sz;
  188. }
  189. err = read(cgpu->device_fd, buf, bufsize);
  190. return err;
  191. }
  192. static
  193. int btm_write(struct cgpu_info * const cgpu, void * const buf, const size_t bufsize)
  194. {
  195. int err = 0;
  196. //applog(LOG_DEBUG, "btm_write ----- %d -----", bufsize);
  197. if (unlikely(cgpu->device_fd == BITMAIN_USING_CURL)) {
  198. struct bitmain_info * const info = cgpu->device_data;
  199. uint8_t headbuf[5];
  200. headbuf[0] = 1;
  201. pk_u32be(headbuf, 1, bufsize);
  202. if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, headbuf, sizeof(headbuf)))
  203. return -1;
  204. if (!bitmain_curl_all(false, info->curl_sock, info->device_curl, buf, bufsize))
  205. return -1;
  206. if (!bitmain_curl_all( true, info->curl_sock, info->device_curl, headbuf, 4))
  207. return -1;
  208. if (headbuf[0] == 0xff && headbuf[1] == 0xff && headbuf[2] == 0xff && headbuf[3] == 0xff)
  209. return -1;
  210. return upk_u32be(headbuf, 0);
  211. }
  212. err = write(cgpu->device_fd, buf, bufsize);
  213. return err;
  214. }
  215. #define BITMAIN_CALC_DIFF1 1
  216. #ifdef WIN32
  217. #define BITMAIN_TEST
  218. #endif
  219. #define BITMAIN_TEST_PRINT_WORK 0
  220. #ifdef BITMAIN_TEST
  221. #define BITMAIN_TEST_NUM 19
  222. #define BITMAIN_TEST_USENUM 1
  223. int g_test_index = 0;
  224. const char btm_work_test_data[BITMAIN_TEST_NUM][256] = {
  225. "00000002ddc1ce5579dbec17f17fbb8f31ae218a814b2a0c1900f0d90000000100000000b58aa6ca86546b07a5a46698f736c7ca9c0eedc756d8f28ac33c20cc24d792675276f879190afc85b6888022000000800000000000000000000000000000000000000000000000000000000000000000",
  226. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000eb2d45233c5b02de50ddcb9049ba16040e0ba00e9750a474eec75891571d925b52dfda4a190266667145b02f000000800000000000000000000000000000000000000000000000000000000000000000",
  227. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000090c7d3743e0b0562e4f56d3dd35cece3c5e8275d0abb21bf7e503cb72bd7ed3b52dfda4a190266667bbb58d7000000800000000000000000000000000000000000000000000000000000000000000000",
  228. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000006e0561da06022bfbb42c5ecd74a46bfd91934f201b777e9155cc6c3674724ec652dfda4a19026666a0cd827b000000800000000000000000000000000000000000000000000000000000000000000000",
  229. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000000312f42ce4964cc23f2d8c039f106f25ddd58e10a1faed21b3bba4b0e621807b52dfda4a1902666629c9497d000000800000000000000000000000000000000000000000000000000000000000000000",
  230. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000033093a6540dbe8f7f3d19e3d2af05585ac58dafad890fa9a942e977334a23d6e52dfda4a190266665ae95079000000800000000000000000000000000000000000000000000000000000000000000000",
  231. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000bd7893057d06e69705bddf9a89c7bac6b40c5b32f15e2295fc8c5edf491ea24952dfda4a190266664b89b4d3000000800000000000000000000000000000000000000000000000000000000000000000",
  232. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000075e66f533e53837d14236a793ee4e493985642bc39e016b9e63adf14a584a2aa52dfda4a19026666ab5d638d000000800000000000000000000000000000000000000000000000000000000000000000",
  233. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d936f90c5db5f0fe1d017344443854fbf9e40a07a9b7e74fedc8661c23162bff52dfda4a19026666338e79cb000000800000000000000000000000000000000000000000000000000000000000000000",
  234. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000d2c1a7d279a4355b017bc0a4b0a9425707786729f21ee18add3fda4252a31a4152dfda4a190266669bc90806000000800000000000000000000000000000000000000000000000000000000000000000",
  235. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000ad36d19f33d04ca779942843890bc3b083cec83a4b60b6c45cf7d21fc187746552dfda4a1902666675d81ab7000000800000000000000000000000000000000000000000000000000000000000000000",
  236. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000093b809cf82b76082eacb55bc35b79f31882ed0976fd102ef54783cd24341319b52dfda4a1902666642ab4e42000000800000000000000000000000000000000000000000000000000000000000000000",
  237. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000007411ff315430a7bbf41de8a685d457e82d5177c05640d6a4436a40f39e99667852dfda4a190266662affa4b5000000800000000000000000000000000000000000000000000000000000000000000000",
  238. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000001ad0db5b9e1e2b57c8d3654c160f5a51067521eab7e340a270639d97f00a3fa252dfda4a1902666601a47bb6000000800000000000000000000000000000000000000000000000000000000000000000",
  239. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b19000000000000000022e055c442c46bbe16df68603a26891f6e4cf85b90102b39fd7cadb602b4e34552dfda4a1902666695d33cea000000800000000000000000000000000000000000000000000000000000000000000000",
  240. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b1900000000000000009c8baf5a8a1e16de2d6ae949d5fec3ed751f10dcd4c99810f2ce08040fb9e31d52dfda4a19026666fe78849d000000800000000000000000000000000000000000000000000000000000000000000000",
  241. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000e5655532b414887f35eb4652bc7b11ebac12891f65bc08cbe0ce5b277b9e795152dfda4a19026666fcc0d1d1000000800000000000000000000000000000000000000000000000000000000000000000",
  242. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000f272c5508704e2b62dd1c30ea970372c40bf00f9203f9bf69d456b4a7fbfffe352dfda4a19026666c03d4399000000800000000000000000000000000000000000000000000000000000000000000000",
  243. "0000000256ccc4c8aeae2b1e41490bc352893605f284e4be043f7b190000000000000000fca3b4531ba627ad9b0e23cdd84c888952c23810df196e9c6db0bcecba6a830952dfda4a19026666c14009cb000000800000000000000000000000000000000000000000000000000000000000000000"
  244. };
  245. const char btm_work_test_midstate[BITMAIN_TEST_NUM][256] = {
  246. "2d8738e7f5bcf76dcb8316fec772e20e240cd58c88d47f2d3f5a6a9547ed0a35",
  247. "d31b6ce09c0bfc2af6f3fe3a03475ebefa5aa191fa70a327a354b2c22f9692f1",
  248. "84a8c8224b80d36caeb42eff2a100f634e1ff873e83fd02ef1306a34abef9dbe",
  249. "059882159439b9b32968c79a93c5521e769dbea9d840f56c2a17b9ad87e530b8",
  250. "17fa435d05012574f8f1da26994cc87b6cb9660b5e82072dc6a0881cec150a0d",
  251. "92a28cc5ec4ba6a2688471dfe2032b5fe97c805ca286c503e447d6749796c6af",
  252. "1677a03516d6e9509ac37e273d2482da9af6e077abe8392cdca6a30e916a7ae9",
  253. "50bbe09f1b8ac18c97aeb745d5d2c3b5d669b6ac7803e646f65ac7b763a392d1",
  254. "e46a0022ebdc303a7fb1a0ebfa82b523946c312e745e5b8a116b17ae6b4ce981",
  255. "8f2f61e7f5b4d76d854e6d266acfff4d40347548216838ccc4ef3b9e43d3c9ea",
  256. "0a450588ae99f75d676a08d0326e1ea874a3497f696722c78a80c7b6ee961ea6",
  257. "3c4c0fc2cf040b806c51b46de9ec0dcc678a7cc5cf3eff11c6c03de3bc7818cc",
  258. "f6c7c785ab5daddb8f98e5f854f2cb41879fcaf47289eb2b4196fefc1b28316f",
  259. "005312351ccb0d0794779f5023e4335b5cad221accf0dfa3da7b881266fa9f5a",
  260. "7b26d189c6bba7add54143179aadbba7ccaeff6887bd8d5bec9597d5716126e6",
  261. "a4718f4c801e7ddf913a9474eb71774993525684ffea1915f767ab16e05e6889",
  262. "6b6226a8c18919d0e55684638d33a6892a00d22492cc2f5906ca7a4ac21c74a7",
  263. "383114dccd1cb824b869158aa2984d157fcb02f46234ceca65943e919329e697",
  264. "d4d478df3016852b27cb1ae9e1e98d98617f8d0943bf9dc1217f47f817236222"
  265. };
  266. #endif
  267. bool opt_bitmain_checkall = false;
  268. bool opt_bitmain_nobeeper = false;
  269. bool opt_bitmain_notempoverctrl = false;
  270. bool opt_bitmain_homemode = false;
  271. bool opt_bitmain_auto;
  272. // --------------------------------------------------------------
  273. // CRC16 check table
  274. // --------------------------------------------------------------
  275. static
  276. const uint8_t chCRCHTalbe[] = // CRC high byte table
  277. {
  278. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  279. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  280. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  281. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  282. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  283. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  284. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  285. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  286. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  287. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  288. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  289. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  290. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  291. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  292. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  293. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  294. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  295. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  296. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  297. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  298. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  299. 0x00, 0xC1, 0x81, 0x40
  300. };
  301. static
  302. const uint8_t chCRCLTalbe[] = // CRC low byte table
  303. {
  304. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  305. 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  306. 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  307. 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  308. 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  309. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  310. 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  311. 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  312. 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  313. 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  314. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  315. 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  316. 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  317. 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  318. 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  319. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  320. 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  321. 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  322. 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  323. 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  324. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  325. 0x41, 0x81, 0x80, 0x40
  326. };
  327. static uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
  328. {
  329. uint8_t chCRCHi = 0xFF; // CRC high byte initialize
  330. uint8_t chCRCLo = 0xFF; // CRC low byte initialize
  331. uint16_t wIndex = 0; // CRC cycling index
  332. while (w_len--) {
  333. wIndex = chCRCLo ^ *p_data++;
  334. chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
  335. chCRCHi = chCRCLTalbe[wIndex];
  336. }
  337. return ((chCRCHi << 8) | chCRCLo);
  338. }
  339. static uint32_t num2bit(int num) {
  340. return 1L << (31 - num);
  341. }
  342. static int bitmain_set_txconfig(struct bitmain_txconfig_token *bm,
  343. uint8_t reset, uint8_t fan_eft, uint8_t timeout_eft, uint8_t frequency_eft,
  344. uint8_t voltage_eft, uint8_t chain_check_time_eft, uint8_t chip_config_eft, uint8_t hw_error_eft,
  345. uint8_t beeper_ctrl, uint8_t temp_over_ctrl,uint8_t fan_home_mode,
  346. uint8_t chain_num, uint8_t asic_num, uint8_t fan_pwm_data, uint8_t timeout_data,
  347. uint16_t frequency, uint8_t * voltage, uint8_t chain_check_time,
  348. uint8_t chip_address, uint8_t reg_address, uint8_t * reg_data)
  349. {
  350. uint16_t crc = 0;
  351. int datalen = 0;
  352. uint8_t version = 0;
  353. uint8_t * sendbuf = (uint8_t *)bm;
  354. if (unlikely(!bm)) {
  355. applog(LOG_WARNING, "bitmain_set_txconfig bitmain_txconfig_token is null");
  356. return -1;
  357. }
  358. if (unlikely(timeout_data <= 0 || asic_num <= 0 || chain_num <= 0)) {
  359. applog(LOG_WARNING, "bitmain_set_txconfig parameter invalid timeout_data(%d) asic_num(%d) chain_num(%d)",
  360. timeout_data, asic_num, chain_num);
  361. return -1;
  362. }
  363. datalen = sizeof(struct bitmain_txconfig_token);
  364. memset(bm, 0, datalen);
  365. bm->token_type = BITMAIN_TOKEN_TYPE_TXCONFIG;
  366. bm->version = version;
  367. bm->length = datalen-4;
  368. bm->length = htole16(bm->length);
  369. bm->reset = reset;
  370. bm->fan_eft = fan_eft;
  371. bm->timeout_eft = timeout_eft;
  372. bm->frequency_eft = frequency_eft;
  373. bm->voltage_eft = voltage_eft;
  374. bm->chain_check_time_eft = chain_check_time_eft;
  375. bm->chip_config_eft = chip_config_eft;
  376. bm->hw_error_eft = hw_error_eft;
  377. bm->beeper_ctrl = beeper_ctrl;
  378. bm->temp_over_ctrl = temp_over_ctrl;
  379. bm->fan_home_mode = fan_home_mode;
  380. sendbuf[4] = htole8(sendbuf[4]);
  381. sendbuf[5] = htole8(sendbuf[5]);
  382. bm->chain_num = chain_num;
  383. bm->asic_num = asic_num;
  384. bm->fan_pwm_data = fan_pwm_data;
  385. bm->timeout_data = timeout_data;
  386. bm->frequency = htole16(frequency);
  387. memcpy(bm->voltage, voltage, 2);
  388. bm->chain_check_time = chain_check_time;
  389. memcpy(bm->reg_data, reg_data, 4);
  390. bm->chip_address = chip_address;
  391. bm->reg_address = reg_address;
  392. crc = CRC16((uint8_t *)bm, datalen-2);
  393. bm->crc = htole16(crc);
  394. applog(LOG_ERR, "BTM TxConfigToken:v(%d) reset(%d) fan_e(%d) tout_e(%d) fq_e(%d) vt_e(%d) chainc_e(%d) chipc_e(%d) hw_e(%d) b_c(%d) t_c(%d) f_m(%d) mnum(%d) anum(%d) fanpwmdata(%d) toutdata(%d) freq(%d) volt(%02x%02x) chainctime(%d) regdata(%02x%02x%02x%02x) chipaddr(%02x) regaddr(%02x) crc(%04x)",
  395. version, reset, fan_eft, timeout_eft, frequency_eft, voltage_eft,
  396. chain_check_time_eft, chip_config_eft, hw_error_eft, beeper_ctrl, temp_over_ctrl,fan_home_mode,chain_num, asic_num,
  397. fan_pwm_data, timeout_data, frequency, voltage[0], voltage[1],
  398. chain_check_time, reg_data[0], reg_data[1], reg_data[2], reg_data[3], chip_address, reg_address, crc);
  399. return datalen;
  400. }
  401. static int bitmain_set_rxstatus(struct bitmain_rxstatus_token *bm,
  402. uint8_t chip_status_eft, uint8_t detect_get, uint8_t chip_address, uint8_t reg_address)
  403. {
  404. uint16_t crc = 0;
  405. uint8_t version = 0;
  406. int datalen = 0;
  407. uint8_t * sendbuf = (uint8_t *)bm;
  408. if (unlikely(!bm)) {
  409. applog(LOG_WARNING, "bitmain_set_rxstatus bitmain_rxstatus_token is null");
  410. return -1;
  411. }
  412. datalen = sizeof(struct bitmain_rxstatus_token);
  413. memset(bm, 0, datalen);
  414. bm->token_type = BITMAIN_TOKEN_TYPE_RXSTATUS;
  415. bm->version = version;
  416. bm->length = datalen-4;
  417. bm->length = htole16(bm->length);
  418. bm->chip_status_eft = chip_status_eft;
  419. bm->detect_get = detect_get;
  420. sendbuf[4] = htole8(sendbuf[4]);
  421. bm->chip_address = chip_address;
  422. bm->reg_address = reg_address;
  423. crc = CRC16((uint8_t *)bm, datalen-2);
  424. bm->crc = htole16(crc);
  425. applog(LOG_ERR, "BitMain RxStatus Token: v(%d) chip_status_eft(%d) detect_get(%d) chip_address(%02x) reg_address(%02x) crc(%04x)",
  426. version, chip_status_eft, detect_get, chip_address, reg_address, crc);
  427. return datalen;
  428. }
  429. static int bitmain_parse_rxstatus(const uint8_t * data, int datalen, struct bitmain_rxstatus_data *bm)
  430. {
  431. uint16_t crc = 0;
  432. uint8_t version = 0;
  433. int i = 0, j = 0;
  434. int asic_num = 0;
  435. int dataindex = 0;
  436. uint8_t tmp = 0x01;
  437. if (unlikely(!bm)) {
  438. applog(LOG_WARNING, "bitmain_parse_rxstatus bitmain_rxstatus_data is null");
  439. return -1;
  440. }
  441. if (unlikely(!data || datalen <= 0)) {
  442. applog(LOG_WARNING, "bitmain_parse_rxstatus parameter invalid data is null or datalen(%d) error", datalen);
  443. return -1;
  444. }
  445. memset(bm, 0, sizeof(struct bitmain_rxstatus_data));
  446. memcpy(bm, data, 28);
  447. if (bm->data_type != BITMAIN_DATA_TYPE_RXSTATUS) {
  448. applog(LOG_ERR, "bitmain_parse_rxstatus datatype(%02x) error", bm->data_type);
  449. return -1;
  450. }
  451. if (bm->version != version) {
  452. applog(LOG_ERR, "bitmain_parse_rxstatus version(%02x) error", bm->version);
  453. return -1;
  454. }
  455. bm->length = htole16(bm->length);
  456. if (bm->length+4 != datalen) {
  457. applog(LOG_ERR, "bitmain_parse_rxstatus length(%d) datalen(%d) error", bm->length, datalen);
  458. return -1;
  459. }
  460. crc = CRC16(data, datalen-2);
  461. memcpy(&(bm->crc), data+datalen-2, 2);
  462. bm->crc = htole16(bm->crc);
  463. if(crc != bm->crc) {
  464. applog(LOG_ERR, "bitmain_parse_rxstatus check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
  465. return -1;
  466. }
  467. bm->fifo_space = htole16(bm->fifo_space);
  468. bm->fan_exist = htole16(bm->fan_exist);
  469. bm->temp_exist = htole32(bm->temp_exist);
  470. bm->nonce_error = htole32(bm->nonce_error);
  471. if(bm->chain_num > BITMAIN_MAX_CHAIN_NUM) {
  472. applog(LOG_ERR, "bitmain_parse_rxstatus chain_num=%d error", bm->chain_num);
  473. return -1;
  474. }
  475. dataindex = 28;
  476. if(bm->chain_num > 0) {
  477. memcpy(bm->chain_asic_num, data+datalen-2-bm->chain_num-bm->temp_num-bm->fan_num, bm->chain_num);
  478. }
  479. for(i = 0; i < bm->chain_num; i++) {
  480. asic_num = bm->chain_asic_num[i];
  481. if(asic_num <= 0) {
  482. asic_num = 1;
  483. } else {
  484. if(asic_num % 32 == 0) {
  485. asic_num = asic_num / 32;
  486. } else {
  487. asic_num = asic_num / 32 + 1;
  488. }
  489. }
  490. memcpy((uint8_t *)bm->chain_asic_exist+i*32, data+dataindex, asic_num*4);
  491. dataindex += asic_num*4;
  492. }
  493. for(i = 0; i < bm->chain_num; i++) {
  494. asic_num = bm->chain_asic_num[i];
  495. if(asic_num <= 0) {
  496. asic_num = 1;
  497. } else {
  498. if(asic_num % 32 == 0) {
  499. asic_num = asic_num / 32;
  500. } else {
  501. asic_num = asic_num / 32 + 1;
  502. }
  503. }
  504. memcpy((uint8_t *)bm->chain_asic_status+i*32, data+dataindex, asic_num*4);
  505. dataindex += asic_num*4;
  506. }
  507. dataindex += bm->chain_num;
  508. if(dataindex + bm->temp_num + bm->fan_num + 2 != datalen) {
  509. applog(LOG_ERR, "bitmain_parse_rxstatus dataindex(%d) chain_num(%d) temp_num(%d) fan_num(%d) not match datalen(%d)",
  510. dataindex, bm->chain_num, bm->temp_num, bm->fan_num, datalen);
  511. return -1;
  512. }
  513. for(i = 0; i < bm->chain_num; i++) {
  514. //bm->chain_asic_status[i] = swab32(bm->chain_asic_status[i]);
  515. for(j = 0; j < 8; j++) {
  516. bm->chain_asic_exist[i*8+j] = htole32(bm->chain_asic_exist[i*8+j]);
  517. bm->chain_asic_status[i*8+j] = htole32(bm->chain_asic_status[i*8+j]);
  518. }
  519. }
  520. if(bm->temp_num > 0) {
  521. memcpy(bm->temp, data+dataindex, bm->temp_num);
  522. dataindex += bm->temp_num;
  523. }
  524. if(bm->fan_num > 0) {
  525. memcpy(bm->fan, data+dataindex, bm->fan_num);
  526. dataindex += bm->fan_num;
  527. }
  528. if(!opt_bitmain_checkall){
  529. if(tmp != htole8(tmp)){
  530. applog(LOG_ERR, "BitMain RxStatus byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
  531. memcpy(&tmp,data+4,1);
  532. bm->chip_value_eft = tmp >>7;
  533. bm->get_blk_num = tmp >> 4;
  534. bm->reserved1 = ((tmp << 4) & 0xff) >> 5;
  535. }
  536. found_blocks = bm->get_blk_num;
  537. applog(LOG_ERR, "BitMain RxStatus tmp :0x%02x byte4 0x%02x chip_value_eft %d reserved %d get_blk_num %d ",tmp,*((uint8_t* )bm +4),bm->chip_value_eft,bm->reserved1,bm->get_blk_num);
  538. }
  539. applog(LOG_DEBUG, "BitMain RxStatusData: chipv_e(%d) chainnum(%d) fifos(%d) v1(%d) v2(%d) v3(%d) v4(%d) fann(%d) tempn(%d) fanet(%04x) tempet(%08x) ne(%d) regvalue(%d) crc(%04x)",
  540. bm->chip_value_eft, bm->chain_num, bm->fifo_space, bm->hw_version[0], bm->hw_version[1], bm->hw_version[2], bm->hw_version[3], bm->fan_num, bm->temp_num, bm->fan_exist, bm->temp_exist, bm->nonce_error, bm->reg_value, bm->crc);
  541. applog(LOG_DEBUG, "BitMain RxStatus Data chain info:");
  542. for(i = 0; i < bm->chain_num; i++) {
  543. applog(LOG_DEBUG, "BitMain RxStatus Data chain(%d) asic num=%d asic_exist=%08x asic_status=%08x", i+1, bm->chain_asic_num[i], bm->chain_asic_exist[i*8], bm->chain_asic_status[i*8]);
  544. }
  545. applog(LOG_DEBUG, "BitMain RxStatus Data temp info:");
  546. for(i = 0; i < bm->temp_num; i++) {
  547. applog(LOG_DEBUG, "BitMain RxStatus Data temp(%d) temp=%d", i+1, bm->temp[i]);
  548. }
  549. applog(LOG_DEBUG, "BitMain RxStatus Data fan info:");
  550. for(i = 0; i < bm->fan_num; i++) {
  551. applog(LOG_DEBUG, "BitMain RxStatus Data fan(%d) fan=%d", i+1, bm->fan[i]);
  552. }
  553. return 0;
  554. }
  555. static int bitmain_parse_rxnonce(const uint8_t * data, int datalen, struct bitmain_rxnonce_data *bm, int * nonce_num)
  556. {
  557. int i = 0;
  558. uint16_t crc = 0;
  559. uint8_t version = 0;
  560. int curnoncenum = 0;
  561. if (unlikely(!bm)) {
  562. applog(LOG_ERR, "bitmain_parse_rxnonce bitmain_rxstatus_data null");
  563. return -1;
  564. }
  565. if (unlikely(!data || datalen <= 0)) {
  566. applog(LOG_ERR, "bitmain_parse_rxnonce data null or datalen(%d) error", datalen);
  567. return -1;
  568. }
  569. memcpy(bm, data, sizeof(struct bitmain_rxnonce_data));
  570. if (bm->data_type != BITMAIN_DATA_TYPE_RXNONCE) {
  571. applog(LOG_ERR, "bitmain_parse_rxnonce datatype(%02x) error", bm->data_type);
  572. return -1;
  573. }
  574. if (bm->version != version) {
  575. applog(LOG_ERR, "bitmain_parse_rxnonce version(%02x) error", bm->version);
  576. return -1;
  577. }
  578. bm->length = htole16(bm->length);
  579. if (bm->length+4 != datalen) {
  580. applog(LOG_ERR, "bitmain_parse_rxnonce length(%d) error", bm->length);
  581. return -1;
  582. }
  583. crc = CRC16(data, datalen-2);
  584. memcpy(&(bm->crc), data+datalen-2, 2);
  585. bm->crc = htole16(bm->crc);
  586. if(crc != bm->crc) {
  587. applog(LOG_ERR, "bitmain_parse_rxnonce check crc(%d) != bm crc(%d) datalen(%d)", crc, bm->crc, datalen);
  588. return -1;
  589. }
  590. bm->fifo_space = htole16(bm->fifo_space);
  591. bm->diff = htole16(bm->diff);
  592. bm->total_nonce_num = htole64(bm->total_nonce_num);
  593. curnoncenum = (datalen-14)/8;
  594. applog(LOG_DEBUG, "BitMain RxNonce Data: nonce_num(%d) fifo_space(%d) diff(%d) tnn(%"PRIu64")", curnoncenum, bm->fifo_space, bm->diff, bm->total_nonce_num);
  595. for(i = 0; i < curnoncenum; i++) {
  596. bm->nonces[i].work_id = htole32(bm->nonces[i].work_id);
  597. bm->nonces[i].nonce = htole32(bm->nonces[i].nonce);
  598. applog(LOG_DEBUG, "BitMain RxNonce Data %d: work_id(%d) nonce(%08x)(%d)",
  599. i, bm->nonces[i].work_id, bm->nonces[i].nonce, bm->nonces[i].nonce);
  600. }
  601. *nonce_num = curnoncenum;
  602. return 0;
  603. }
  604. static int bitmain_read(struct cgpu_info *bitmain, unsigned char *buf,
  605. size_t bufsize, int timeout)
  606. {
  607. int err = 0;
  608. size_t total = 0;
  609. if(bitmain == NULL || buf == NULL || bufsize <= 0) {
  610. applog(LOG_WARNING, "bitmain_read parameter error bufsize(%llu)", (unsigned long long)bufsize);
  611. return -1;
  612. }
  613. {
  614. err = btm_read(bitmain, buf, bufsize);
  615. total = err;
  616. }
  617. return total;
  618. }
  619. static int bitmain_write(struct cgpu_info *bitmain, char *buf, ssize_t len)
  620. {
  621. int err;
  622. {
  623. int havelen = 0;
  624. while(havelen < len) {
  625. err = btm_write(bitmain, buf+havelen, len-havelen);
  626. if(err < 0) {
  627. applog(LOG_DEBUG, "%s%i: btm_write got err %d", bitmain->drv->name,
  628. bitmain->device_id, err);
  629. applog(LOG_WARNING, "usb_write error on bitmain_write");
  630. return BTM_SEND_ERROR;
  631. } else {
  632. havelen += err;
  633. }
  634. }
  635. }
  636. return BTM_SEND_OK;
  637. }
  638. static int bitmain_send_data(const uint8_t * data, int datalen, struct cgpu_info *bitmain)
  639. {
  640. int ret;
  641. if(datalen <= 0) {
  642. return 0;
  643. }
  644. //struct bitmain_info *info = bitmain->device_data;
  645. //int delay;
  646. //delay = datalen * 10 * 1000000;
  647. //delay = delay / info->baud;
  648. //delay += 4000;
  649. if(opt_debug) {
  650. char hex[(datalen * 2) + 1];
  651. bin2hex(hex, data, datalen);
  652. applog(LOG_DEBUG, "BitMain: Sent(%d): %s", datalen, hex);
  653. }
  654. //cgtimer_t ts_start;
  655. //cgsleep_prepare_r(&ts_start);
  656. //applog(LOG_DEBUG, "----bitmain_send_data start");
  657. ret = bitmain_write(bitmain, (char *)data, datalen);
  658. applog(LOG_DEBUG, "----bitmain_send_data stop ret=%d datalen=%d", ret, datalen);
  659. //cgsleep_us_r(&ts_start, delay);
  660. //applog(LOG_DEBUG, "BitMain: Sent: Buffer delay: %dus", delay);
  661. return ret;
  662. }
  663. static void bitmain_inc_nvw(struct bitmain_info *info, struct thr_info *thr)
  664. {
  665. applog(LOG_INFO, "%s%d: No matching work - HW error",
  666. thr->cgpu->drv->name, thr->cgpu->device_id);
  667. inc_hw_errors_only(thr);
  668. info->no_matching_work++;
  669. }
  670. static inline void record_temp_fan(struct bitmain_info *info, struct bitmain_rxstatus_data *bm, float *temp)
  671. {
  672. int i = 0;
  673. int maxfan = 0, maxtemp = 0;
  674. int temp_avg = 0;
  675. info->fan_num = bm->fan_num;
  676. for(i = 0; i < bm->fan_num; i++) {
  677. info->fan[i] = bm->fan[i] * BITMAIN_FAN_FACTOR;
  678. if(info->fan[i] > maxfan)
  679. maxfan = info->fan[i];
  680. }
  681. info->temp_num = bm->temp_num;
  682. for(i = 0; i < bm->temp_num; i++) {
  683. info->temp[i] = bm->temp[i];
  684. /*
  685. if(bm->temp[i] & 0x80) {
  686. bm->temp[i] &= 0x7f;
  687. info->temp[i] = 0 - ((~bm->temp[i] & 0x7f) + 1);
  688. }*/
  689. temp_avg += info->temp[i];
  690. if(info->temp[i] > info->temp_max) {
  691. info->temp_max = info->temp[i];
  692. }
  693. if(info->temp[i] > maxtemp)
  694. maxtemp = info->temp[i];
  695. }
  696. if(bm->temp_num > 0) {
  697. temp_avg /= bm->temp_num;
  698. info->temp_avg = temp_avg;
  699. }
  700. *temp = maxtemp;
  701. }
  702. static void bitmain_update_temps(struct cgpu_info *bitmain, struct bitmain_info *info,
  703. struct bitmain_rxstatus_data *bm)
  704. {
  705. char tmp[64] = {0};
  706. char msg[10240] = {0};
  707. int i = 0;
  708. record_temp_fan(info, bm, &(bitmain->temp));
  709. strcpy(msg, "BitMain: ");
  710. for(i = 0; i < bm->fan_num; i++) {
  711. if(i != 0) {
  712. strcat(msg, ", ");
  713. }
  714. sprintf(tmp, "Fan%d: %d/m", i+1, info->fan[i]);
  715. strcat(msg, tmp);
  716. }
  717. strcat(msg, "\t");
  718. for(i = 0; i < bm->temp_num; i++) {
  719. if(i != 0) {
  720. strcat(msg, ", ");
  721. }
  722. sprintf(tmp, "Temp%d: %dC", i+1, info->temp[i]);
  723. strcat(msg, tmp);
  724. }
  725. sprintf(tmp, ", TempMAX: %dC", info->temp_max);
  726. strcat(msg, tmp);
  727. applog(LOG_INFO, "%s", msg);
  728. info->temp_history_index++;
  729. info->temp_sum += bitmain->temp;
  730. applog(LOG_DEBUG, "BitMain: temp_index: %d, temp_count: %d, temp_old: %d",
  731. info->temp_history_index, info->temp_history_count, info->temp_old);
  732. if (info->temp_history_index == info->temp_history_count) {
  733. info->temp_history_index = 0;
  734. info->temp_sum = 0;
  735. }
  736. }
  737. static void bitmain_set_fifo_space(struct cgpu_info * const dev, const int fifo_space)
  738. {
  739. struct thr_info * const master_thr = dev->thr[0];
  740. struct bitmain_info * const info = dev->device_data;
  741. info->fifo_space = fifo_space;
  742. master_thr->queue_full = !fifo_space;
  743. }
  744. static void bitmain_parse_results(struct cgpu_info *bitmain, struct bitmain_info *info,
  745. struct thr_info *thr, uint8_t *buf, int *offset)
  746. {
  747. int i, j, n, m, r, errordiff, spare = BITMAIN_READ_SIZE;
  748. uint32_t checkbit = 0x00000000;
  749. bool found = false;
  750. struct work *work = NULL;
  751. struct bitmain_packet_head packethead;
  752. int asicnum = 0;
  753. int idiff = 0;
  754. int mod = 0,tmp = 0;
  755. for (i = 0; i <= spare; i++) {
  756. if(buf[i] == 0xa1) {
  757. struct bitmain_rxstatus_data rxstatusdata;
  758. applog(LOG_DEBUG, "bitmain_parse_results RxStatus Data");
  759. if(*offset < 4) {
  760. return;
  761. }
  762. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  763. packethead.length = htole16(packethead.length);
  764. if(packethead.length > 1130) {
  765. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus datalen=%d error", packethead.length+4);
  766. continue;
  767. }
  768. if(*offset < packethead.length + 4) {
  769. return;
  770. }
  771. if(bitmain_parse_rxstatus(buf+i, packethead.length+4, &rxstatusdata) != 0) {
  772. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxstatus error len=%d", packethead.length+4);
  773. } else {
  774. mutex_lock(&info->qlock);
  775. info->chain_num = rxstatusdata.chain_num;
  776. bitmain_set_fifo_space(bitmain, rxstatusdata.fifo_space);
  777. info->hw_version[0] = rxstatusdata.hw_version[0];
  778. info->hw_version[1] = rxstatusdata.hw_version[1];
  779. info->hw_version[2] = rxstatusdata.hw_version[2];
  780. info->hw_version[3] = rxstatusdata.hw_version[3];
  781. info->nonce_error = rxstatusdata.nonce_error;
  782. errordiff = info->nonce_error-info->last_nonce_error;
  783. //sprintf(g_miner_version, "%d.%d.%d.%d", info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3]);
  784. applog(LOG_ERR, "bitmain_parse_results v=%d chain=%d fifo=%d hwv1=%d hwv2=%d hwv3=%d hwv4=%d nerr=%d-%d freq=%d chain info:",
  785. rxstatusdata.version, info->chain_num, info->fifo_space, info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3],
  786. info->last_nonce_error, info->nonce_error, info->frequency);
  787. memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
  788. memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
  789. for(n = 0; n < rxstatusdata.chain_num; n++) {
  790. info->chain_asic_num[n] = rxstatusdata.chain_asic_num[n];
  791. memset(info->chain_asic_status_t[n], 0, 320);
  792. j = 0;
  793. mod = 0;
  794. if(info->chain_asic_num[n] <= 0) {
  795. asicnum = 0;
  796. } else {
  797. mod = info->chain_asic_num[n] % 32;
  798. if(mod == 0) {
  799. asicnum = info->chain_asic_num[n] / 32;
  800. } else {
  801. asicnum = info->chain_asic_num[n] / 32 + 1;
  802. }
  803. }
  804. if(asicnum > 0) {
  805. for(m = asicnum-1; m >= 0; m--) {
  806. tmp = mod ? (32-mod): 0;
  807. for(r = tmp;r < 32;r++){
  808. if((r-tmp)%8 == 0 && (r-tmp) !=0){
  809. info->chain_asic_status_t[n][j] = ' ';
  810. j++;
  811. }
  812. checkbit = num2bit(r);
  813. if(rxstatusdata.chain_asic_exist[n*8+m] & checkbit) {
  814. if(rxstatusdata.chain_asic_status[n*8+m] & checkbit) {
  815. info->chain_asic_status_t[n][j] = 'o';
  816. } else {
  817. info->chain_asic_status_t[n][j] = 'x';
  818. }
  819. } else {
  820. info->chain_asic_status_t[n][j] = '-';
  821. }
  822. j++;
  823. }
  824. info->chain_asic_status_t[n][j] = ' ';
  825. j++;
  826. mod = 0;
  827. }
  828. }
  829. applog(LOG_DEBUG, "bitmain_parse_results chain(%d) asic_num=%d asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  830. n, info->chain_asic_num[n],
  831. info->chain_asic_exist[n*8+0], info->chain_asic_exist[n*8+1], info->chain_asic_exist[n*8+2], info->chain_asic_exist[n*8+3], info->chain_asic_exist[n*8+4], info->chain_asic_exist[n*8+5], info->chain_asic_exist[n*8+6], info->chain_asic_exist[n*8+7],
  832. info->chain_asic_status[n*8+0], info->chain_asic_status[n*8+1], info->chain_asic_status[n*8+2], info->chain_asic_status[n*8+3], info->chain_asic_status[n*8+4], info->chain_asic_status[n*8+5], info->chain_asic_status[n*8+6], info->chain_asic_status[n*8+7]);
  833. applog(LOG_ERR, "bitmain_parse_results chain(%d) asic_num=%d asic_status=%s", n, info->chain_asic_num[n], info->chain_asic_status_t[n]);
  834. }
  835. mutex_unlock(&info->qlock);
  836. if(errordiff > 0) {
  837. for(j = 0; j < errordiff; j++) {
  838. bitmain_inc_nvw(info, thr);
  839. }
  840. mutex_lock(&info->qlock);
  841. info->last_nonce_error += errordiff;
  842. mutex_unlock(&info->qlock);
  843. }
  844. bitmain_update_temps(bitmain, info, &rxstatusdata);
  845. }
  846. found = true;
  847. spare = packethead.length + 4 + i;
  848. if(spare > *offset) {
  849. applog(LOG_ERR, "bitmain_parse_rxresults space(%d) > offset(%d)", spare, *offset);
  850. spare = *offset;
  851. }
  852. break;
  853. } else if(buf[i] == 0xa2) {
  854. struct bitmain_rxnonce_data rxnoncedata;
  855. int nonce_num = 0;
  856. applog(LOG_DEBUG, "bitmain_parse_results RxNonce Data");
  857. if(*offset < 4) {
  858. return;
  859. }
  860. memcpy(&packethead, buf+i, sizeof(struct bitmain_packet_head));
  861. packethead.length = htole16(packethead.length);
  862. if(packethead.length > 1038) {
  863. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce datalen=%d error", packethead.length+4);
  864. continue;
  865. }
  866. if(*offset < packethead.length + 4) {
  867. return;
  868. }
  869. if(bitmain_parse_rxnonce(buf+i, packethead.length+4, &rxnoncedata, &nonce_num) != 0) {
  870. applog(LOG_ERR, "bitmain_parse_results bitmain_parse_rxnonce error len=%d", packethead.length+4);
  871. } else {
  872. const float nonce_diff = 1 << rxnoncedata.diff;
  873. for(j = 0; j < nonce_num; j++) {
  874. const work_device_id_t work_id = rxnoncedata.nonces[j].work_id;
  875. HASH_FIND(hh, thr->work_list, &work_id, sizeof(work_id), work);
  876. if(work) {
  877. if(BITMAIN_TEST_PRINT_WORK) {
  878. applog(LOG_ERR, "bitmain_parse_results nonce find work(%d-%d)(%08x)", work->id, rxnoncedata.nonces[j].work_id, rxnoncedata.nonces[j].nonce);
  879. char ob_hex[(32 * 2) + 1];
  880. bin2hex(ob_hex, work->midstate, 32);
  881. applog(LOG_ERR, "work %d midstate: %s", work->id, ob_hex);
  882. bin2hex(ob_hex, &work->data[64], 12);
  883. applog(LOG_ERR, "work %d data2: %s", work->id, ob_hex);
  884. }
  885. {
  886. const uint32_t nonce = rxnoncedata.nonces[j].nonce;
  887. applog(LOG_DEBUG, "BitMain: submit nonce = %08lx", (unsigned long)nonce);
  888. work->nonce_diff = nonce_diff;
  889. if (submit_nonce(thr, work, nonce)) {
  890. mutex_lock(&info->qlock);
  891. hashes_done2(thr, 0x100000000 * work->nonce_diff, NULL);
  892. info->auto_nonces++;
  893. mutex_unlock(&info->qlock);
  894. } else {
  895. //bitmain_inc_nvw(info, thr);
  896. applog(LOG_ERR, "BitMain: bitmain_decode_nonce error work(%d)", rxnoncedata.nonces[j].work_id);
  897. }
  898. }
  899. } else {
  900. //bitmain_inc_nvw(info, thr);
  901. applog(LOG_ERR, "BitMain: Nonce not find work(%d)", rxnoncedata.nonces[j].work_id);
  902. }
  903. }
  904. #ifdef BITMAIN_CALC_DIFF1
  905. if(opt_bitmain_hwerror) {
  906. int difftmp = 0;
  907. idiff = nonce_diff;
  908. mutex_lock(&info->qlock);
  909. difftmp = idiff*(rxnoncedata.total_nonce_num-info->total_nonce_num);
  910. if(difftmp < 0)
  911. difftmp = 0;
  912. hashes_done2(thr, 0x100000000 * difftmp, NULL);
  913. info->auto_nonces = info->auto_nonces+difftmp;
  914. info->total_nonce_num = rxnoncedata.total_nonce_num;
  915. bitmain_set_fifo_space(bitmain, rxnoncedata.fifo_space);
  916. mutex_unlock(&info->qlock);
  917. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d diff=%d rxtnn=%"PRIu64" tnn=%"PRIu64, info->fifo_space, idiff, rxnoncedata.total_nonce_num, info->total_nonce_num);
  918. } else {
  919. mutex_lock(&info->qlock);
  920. bitmain_set_fifo_space(bitmain, rxnoncedata.fifo_space);
  921. mutex_unlock(&info->qlock);
  922. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);
  923. }
  924. #else
  925. mutex_lock(&info->qlock);
  926. bitmain_set_fifo_space(bitmain, rxnoncedata.fifo_space);
  927. mutex_unlock(&info->qlock);
  928. applog(LOG_DEBUG, "bitmain_parse_rxnonce fifo space=%d", info->fifo_space);
  929. #endif
  930. #ifndef WIN32
  931. if(nonce_num < BITMAIN_MAX_NONCE_NUM)
  932. cgsleep_ms(5);
  933. #endif
  934. }
  935. found = true;
  936. spare = packethead.length + 4 + i;
  937. if(spare > *offset) {
  938. applog(LOG_ERR, "bitmain_parse_rxnonce space(%d) > offset(%d)", spare, *offset);
  939. spare = *offset;
  940. }
  941. break;
  942. } else {
  943. applog(LOG_ERR, "bitmain_parse_results data type error=%02x", buf[i]);
  944. }
  945. }
  946. if (!found) {
  947. spare = *offset - BITMAIN_READ_SIZE;
  948. /* We are buffering and haven't accumulated one more corrupt
  949. * work result. */
  950. if (spare < (int)BITMAIN_READ_SIZE)
  951. return;
  952. bitmain_inc_nvw(info, thr);
  953. }
  954. *offset -= spare;
  955. memmove(buf, buf + spare, *offset);
  956. }
  957. static void bitmain_running_reset(struct cgpu_info *bitmain, struct bitmain_info *info)
  958. {
  959. bitmain->results = 0;
  960. info->reset = false;
  961. }
  962. static void bitmain_poll(struct thr_info * const thr)
  963. {
  964. struct cgpu_info *bitmain = thr->cgpu;
  965. struct bitmain_info *info = bitmain->device_data;
  966. int offset = info->readbuf_offset, ret = 0;
  967. const int rsize = BITMAIN_FTDI_READSIZE;
  968. uint8_t * const readbuf = info->readbuf;
  969. {
  970. unsigned char buf[rsize];
  971. if (unlikely(info->reset)) {
  972. bitmain_running_reset(bitmain, info);
  973. /* Discard anything in the buffer */
  974. offset = 0;
  975. }
  976. //cgsleep_prepare_r(&ts_start);
  977. //applog(LOG_DEBUG, "======start bitmain_get_results bitmain_read");
  978. ret = bitmain_read(bitmain, buf, rsize, BITMAIN_READ_TIMEOUT);
  979. //applog(LOG_DEBUG, "======stop bitmain_get_results bitmain_read=%d", ret);
  980. if ((ret < 1) || (ret == 18)) {
  981. ++info->errorcount2;
  982. #ifdef WIN32
  983. if(info->errorcount2 > 200) {
  984. //applog(LOG_ERR, "bitmain_read errorcount ret=%d", ret);
  985. cgsleep_ms(20);
  986. info->errorcount2 = 0;
  987. }
  988. #else
  989. if(info->errorcount2 > 3) {
  990. //applog(LOG_ERR, "bitmain_read errorcount ret=%d", ret);
  991. cgsleep_ms(20);
  992. info->errorcount2 = 0;
  993. }
  994. #endif
  995. if(ret < 1)
  996. return;
  997. }
  998. if (opt_debug) {
  999. char hex[(ret * 2) + 1];
  1000. bin2hex(hex, buf, ret);
  1001. applog(LOG_DEBUG, "BitMain: get: %s", hex);
  1002. }
  1003. memcpy(readbuf+offset, buf, ret);
  1004. offset += ret;
  1005. //applog(LOG_DEBUG, "+++++++bitmain_get_results offset=%d", offset);
  1006. if (offset >= (int)BITMAIN_READ_SIZE) {
  1007. //applog(LOG_DEBUG, "======start bitmain_get_results ");
  1008. bitmain_parse_results(bitmain, info, thr, readbuf, &offset);
  1009. //applog(LOG_DEBUG, "======stop bitmain_get_results ");
  1010. }
  1011. if (unlikely(offset + rsize >= BITMAIN_READBUF_SIZE)) {
  1012. /* This should never happen */
  1013. applog(LOG_DEBUG, "BitMain readbuf overflow, resetting buffer");
  1014. offset = 0;
  1015. }
  1016. /* As the usb read returns after just 1ms, sleep long enough
  1017. * to leave the interface idle for writes to occur, but do not
  1018. * sleep if we have been receiving data as more may be coming. */
  1019. //if (offset == 0) {
  1020. // cgsleep_ms_r(&ts_start, BITMAIN_READ_TIMEOUT);
  1021. //}
  1022. }
  1023. info->readbuf_offset = offset;
  1024. }
  1025. static void bitmain_init(struct cgpu_info *bitmain)
  1026. {
  1027. applog(LOG_INFO, "BitMain: Opened on %s", bitmain->device_path);
  1028. }
  1029. static bool bitmain_prepare(struct thr_info *thr)
  1030. {
  1031. struct cgpu_info *bitmain = thr->cgpu;
  1032. struct bitmain_info *info = bitmain->device_data;
  1033. free(bitmain->works);
  1034. bitmain->works = calloc(BITMAIN_MAX_WORK_NUM * sizeof(struct work *),
  1035. BITMAIN_ARRAY_SIZE);
  1036. if (!bitmain->works)
  1037. quit(1, "Failed to calloc bitmain works in bitmain_prepare");
  1038. info->thr = thr;
  1039. mutex_init(&info->lock);
  1040. mutex_init(&info->qlock);
  1041. if (unlikely(pthread_cond_init(&info->qcond, NULL)))
  1042. quit(1, "Failed to pthread_cond_init bitmain qcond");
  1043. // To initialise queue_full
  1044. bitmain_set_fifo_space(bitmain, info->fifo_space);
  1045. bitmain_init(bitmain);
  1046. timer_set_now(&thr->tv_poll);
  1047. return true;
  1048. }
  1049. static int bitmain_initialize(struct cgpu_info *bitmain)
  1050. {
  1051. uint8_t data[BITMAIN_READBUF_SIZE];
  1052. struct bitmain_info *info = NULL;
  1053. int ret = 0;
  1054. uint8_t sendbuf[BITMAIN_SENDBUF_SIZE];
  1055. int readlen = 0;
  1056. int sendlen = 0;
  1057. int trycount = 3;
  1058. struct timespec p;
  1059. struct bitmain_rxstatus_data rxstatusdata;
  1060. int i = 0, j = 0, m = 0, r = 0, statusok = 0;
  1061. uint32_t checkbit = 0x00000000;
  1062. int hwerror_eft = 0;
  1063. int beeper_ctrl = 1;
  1064. int tempover_ctrl = 1;
  1065. int home_mode = 0;
  1066. struct bitmain_packet_head packethead;
  1067. int asicnum = 0;
  1068. int mod = 0,tmp = 0;
  1069. /* Send reset, then check for result */
  1070. if(!bitmain) {
  1071. applog(LOG_WARNING, "bitmain_initialize cgpu_info is null");
  1072. return -1;
  1073. }
  1074. info = bitmain->device_data;
  1075. /* clear read buf */
  1076. ret = bitmain_read(bitmain, data, BITMAIN_READBUF_SIZE,
  1077. BITMAIN_RESET_TIMEOUT);
  1078. if(ret > 0) {
  1079. if (opt_debug) {
  1080. char hex[(ret * 2) + 1];
  1081. bin2hex(hex, data, ret);
  1082. applog(LOG_DEBUG, "BTM%d Clear Read(%d): %s", bitmain->device_id, ret, hex);
  1083. }
  1084. }
  1085. sendlen = bitmain_set_rxstatus((struct bitmain_rxstatus_token *)sendbuf, 0, 1, 0, 0);
  1086. if(sendlen <= 0) {
  1087. applog(LOG_ERR, "bitmain_initialize bitmain_set_rxstatus error(%d)", sendlen);
  1088. return -1;
  1089. }
  1090. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1091. if (unlikely(ret == BTM_SEND_ERROR)) {
  1092. applog(LOG_ERR, "bitmain_initialize bitmain_send_data error");
  1093. return -1;
  1094. }
  1095. while(trycount >= 0) {
  1096. ret = bitmain_read(bitmain, data+readlen, BITMAIN_READBUF_SIZE, BITMAIN_RESET_TIMEOUT);
  1097. if(ret > 0) {
  1098. readlen += ret;
  1099. if(readlen > BITMAIN_READ_SIZE) {
  1100. for(i = 0; i < readlen; i++) {
  1101. if(data[i] == 0xa1) {
  1102. if (opt_debug) {
  1103. char hex[(readlen * 2) + 1];
  1104. bin2hex(hex, data, readlen);
  1105. applog(LOG_DEBUG, "%s%d initset: get: %s", bitmain->drv->name, bitmain->device_id, hex);
  1106. }
  1107. memcpy(&packethead, data+i, sizeof(struct bitmain_packet_head));
  1108. packethead.length = htole16(packethead.length);
  1109. if(packethead.length > 1130) {
  1110. applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d error", packethead.length+4);
  1111. continue;
  1112. }
  1113. if(readlen-i < packethead.length+4) {
  1114. applog(LOG_ERR, "bitmain_initialize rxstatus datalen=%d<%d low", readlen-i, packethead.length+4);
  1115. continue;
  1116. }
  1117. if (bitmain_parse_rxstatus(data+i, packethead.length+4, &rxstatusdata) != 0) {
  1118. applog(LOG_ERR, "bitmain_initialize bitmain_parse_rxstatus error");
  1119. continue;
  1120. }
  1121. info->chain_num = rxstatusdata.chain_num;
  1122. // NOTE: This is before thr_info is allocated, so we cannot use bitmain_set_fifo_space (bitmain_prepare will re-set it for us)
  1123. info->fifo_space = rxstatusdata.fifo_space;
  1124. info->hw_version[0] = rxstatusdata.hw_version[0];
  1125. info->hw_version[1] = rxstatusdata.hw_version[1];
  1126. info->hw_version[2] = rxstatusdata.hw_version[2];
  1127. info->hw_version[3] = rxstatusdata.hw_version[3];
  1128. info->nonce_error = 0;
  1129. info->last_nonce_error = 0;
  1130. sprintf(info->g_miner_version, "%d.%d.%d.%d", info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3]);
  1131. applog(LOG_ERR, "bitmain_initialize rxstatus v(%d) chain(%d) fifo(%d) hwv1(%d) hwv2(%d) hwv3(%d) hwv4(%d) nerr(%d) freq=%d",
  1132. rxstatusdata.version, info->chain_num, info->fifo_space, info->hw_version[0], info->hw_version[1], info->hw_version[2], info->hw_version[3],
  1133. rxstatusdata.nonce_error, info->frequency);
  1134. memcpy(info->chain_asic_exist, rxstatusdata.chain_asic_exist, BITMAIN_MAX_CHAIN_NUM*32);
  1135. memcpy(info->chain_asic_status, rxstatusdata.chain_asic_status, BITMAIN_MAX_CHAIN_NUM*32);
  1136. for(i = 0; i < rxstatusdata.chain_num; i++) {
  1137. info->chain_asic_num[i] = rxstatusdata.chain_asic_num[i];
  1138. memset(info->chain_asic_status_t[i], 0, 320);
  1139. j = 0;
  1140. mod = 0;
  1141. if(info->chain_asic_num[i] <= 0) {
  1142. asicnum = 0;
  1143. } else {
  1144. mod = info->chain_asic_num[i] % 32;
  1145. if(mod == 0) {
  1146. asicnum = info->chain_asic_num[i] / 32;
  1147. } else {
  1148. asicnum = info->chain_asic_num[i] / 32 + 1;
  1149. }
  1150. }
  1151. if(asicnum > 0) {
  1152. for(m = asicnum-1; m >= 0; m--) {
  1153. tmp = mod ? (32-mod):0;
  1154. for(r = tmp;r < 32;r++){
  1155. if((r-tmp)%8 == 0 && (r-tmp) !=0){
  1156. info->chain_asic_status_t[i][j] = ' ';
  1157. j++;
  1158. }
  1159. checkbit = num2bit(r);
  1160. if(rxstatusdata.chain_asic_exist[i*8+m] & checkbit) {
  1161. if(rxstatusdata.chain_asic_status[i*8+m] & checkbit) {
  1162. info->chain_asic_status_t[i][j] = 'o';
  1163. } else {
  1164. info->chain_asic_status_t[i][j] = 'x';
  1165. }
  1166. } else {
  1167. info->chain_asic_status_t[i][j] = '-';
  1168. }
  1169. j++;
  1170. }
  1171. info->chain_asic_status_t[i][j] = ' ';
  1172. j++;
  1173. mod = 0;
  1174. }
  1175. }
  1176. applog(LOG_DEBUG, "bitmain_initialize chain(%d) asic_num=%d asic_exist=%08x%08x%08x%08x%08x%08x%08x%08x asic_status=%08x%08x%08x%08x%08x%08x%08x%08x",
  1177. i, info->chain_asic_num[i],
  1178. info->chain_asic_exist[i*8+0], info->chain_asic_exist[i*8+1], info->chain_asic_exist[i*8+2], info->chain_asic_exist[i*8+3], info->chain_asic_exist[i*8+4], info->chain_asic_exist[i*8+5], info->chain_asic_exist[i*8+6], info->chain_asic_exist[i*8+7],
  1179. info->chain_asic_status[i*8+0], info->chain_asic_status[i*8+1], info->chain_asic_status[i*8+2], info->chain_asic_status[i*8+3], info->chain_asic_status[i*8+4], info->chain_asic_status[i*8+5], info->chain_asic_status[i*8+6], info->chain_asic_status[i*8+7]);
  1180. applog(LOG_ERR, "bitmain_initialize chain(%d) asic_num=%d asic_status=%s", i, info->chain_asic_num[i], info->chain_asic_status_t[i]);
  1181. }
  1182. bitmain_update_temps(bitmain, info, &rxstatusdata);
  1183. statusok = 1;
  1184. break;
  1185. }
  1186. }
  1187. if(statusok) {
  1188. break;
  1189. }
  1190. }
  1191. }
  1192. trycount--;
  1193. p.tv_sec = 0;
  1194. p.tv_nsec = BITMAIN_RESET_PITCH;
  1195. nanosleep(&p, NULL);
  1196. }
  1197. p.tv_sec = 0;
  1198. p.tv_nsec = BITMAIN_RESET_PITCH;
  1199. nanosleep(&p, NULL);
  1200. cgtime(&info->last_status_time);
  1201. if(statusok) {
  1202. applog(LOG_ERR, "bitmain_initialize start send txconfig");
  1203. if(opt_bitmain_hwerror)
  1204. hwerror_eft = 1;
  1205. else
  1206. hwerror_eft = 0;
  1207. if(opt_bitmain_nobeeper)
  1208. beeper_ctrl = 0;
  1209. else
  1210. beeper_ctrl = 1;
  1211. if(opt_bitmain_notempoverctrl)
  1212. tempover_ctrl = 0;
  1213. else
  1214. tempover_ctrl = 1;
  1215. if(opt_bitmain_homemode)
  1216. home_mode= 1;
  1217. else
  1218. home_mode= 0;
  1219. sendlen = bitmain_set_txconfig((struct bitmain_txconfig_token *)sendbuf, 1, 1, 1, 1, 1, 0, 1, hwerror_eft, beeper_ctrl, tempover_ctrl,home_mode,
  1220. info->chain_num, info->asic_num, BITMAIN_DEFAULT_FAN_MAX_PWM, info->timeout,
  1221. info->frequency, info->voltage, 0, 0, 0x04, info->reg_data);
  1222. if(sendlen <= 0) {
  1223. applog(LOG_ERR, "bitmain_initialize bitmain_set_txconfig error(%d)", sendlen);
  1224. return -1;
  1225. }
  1226. ret = bitmain_send_data(sendbuf, sendlen, bitmain);
  1227. if (unlikely(ret == BTM_SEND_ERROR)) {
  1228. applog(LOG_ERR, "bitmain_initialize bitmain_send_data error");
  1229. return -1;
  1230. }
  1231. applog(LOG_WARNING, "BMM%d: InitSet succeeded", bitmain->device_id);
  1232. } else {
  1233. applog(LOG_WARNING, "BMS%d: InitSet error", bitmain->device_id);
  1234. return -1;
  1235. }
  1236. return 0;
  1237. }
  1238. static bool bitmain_detect_one(const char * devpath)
  1239. {
  1240. struct bitmain_info *info;
  1241. struct cgpu_info *bitmain;
  1242. int ret;
  1243. bitmain = btm_alloc_cgpu(&bitmain_drv, BITMAIN_MINER_THREADS);
  1244. info = bitmain->device_data;
  1245. drv_set_defaults(&bitmain_drv, bitmain_set_device_funcs_init, info, devpath, NULL, 1);
  1246. if (!btm_init(bitmain, devpath))
  1247. goto shin;
  1248. applog(LOG_ERR, "bitmain_detect_one btm init ok");
  1249. info->fan_pwm = BITMAIN_DEFAULT_FAN_MIN_PWM;
  1250. info->temp_max = 0;
  1251. /* This is for check the temp/fan every 3~4s */
  1252. info->temp_history_count = (4 / (float)((float)info->timeout * ((float)1.67/0x32))) + 1;
  1253. if (info->temp_history_count <= 0)
  1254. info->temp_history_count = 1;
  1255. info->temp_history_index = 0;
  1256. info->temp_sum = 0;
  1257. info->temp_old = 0;
  1258. if (!add_cgpu(bitmain))
  1259. goto unshin;
  1260. ret = bitmain_initialize(bitmain);
  1261. applog(LOG_ERR, "bitmain_detect_one stop bitmain_initialize %d", ret);
  1262. if (ret)
  1263. goto unshin;
  1264. info->errorcount = 0;
  1265. applog(LOG_ERR, "BitMain Detected: %s "
  1266. "(chain_num=%d asic_num=%d timeout=%d freq=%d-%s volt=%02x%02x-%s)",
  1267. bitmain->device_path, info->chain_num, info->asic_num, info->timeout,
  1268. info->frequency, info->frequency_t, info->voltage[0], info->voltage[1], info->voltage_t);
  1269. return true;
  1270. unshin:
  1271. btm_uninit(bitmain);
  1272. shin:
  1273. free(bitmain->device_data);
  1274. bitmain->device_data = NULL;
  1275. free(bitmain);
  1276. return false;
  1277. }
  1278. static int bitmain_detect_auto(void)
  1279. {
  1280. const char * const auto_bitmain_dev = "/dev/bitmain-asic";
  1281. applog(LOG_DEBUG, "BTM detect dev: %s", auto_bitmain_dev);
  1282. return bitmain_detect_one(auto_bitmain_dev) ? 1 : 0;
  1283. }
  1284. static void bitmain_detect()
  1285. {
  1286. generic_detect(&bitmain_drv, bitmain_detect_one, bitmain_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  1287. }
  1288. static void do_bitmain_close(struct thr_info *thr)
  1289. {
  1290. struct cgpu_info *bitmain = thr->cgpu;
  1291. struct bitmain_info *info = bitmain->device_data;
  1292. pthread_join(info->read_thr, NULL);
  1293. bitmain_running_reset(bitmain, info);
  1294. info->no_matching_work = 0;
  1295. }
  1296. static uint8_t diff_to_bitmain(float diff)
  1297. {
  1298. uint8_t res = 0;
  1299. if (diff > UINT64_MAX)
  1300. diff = UINT64_MAX;
  1301. for (uint64_t tmp = diff; tmp >>= 1; ) {
  1302. if (++res == UINT8_MAX)
  1303. break;
  1304. }
  1305. return res;
  1306. }
  1307. static bool bitmain_queue_append(struct thr_info * const thr, struct work * const work)
  1308. {
  1309. struct cgpu_info * const proc = thr->cgpu;
  1310. struct cgpu_info * const dev = proc->device;
  1311. struct thr_info * const master_thr = dev->thr[0];
  1312. struct bitmain_info * const info = dev->device_data;
  1313. const struct pool * const pool = work->pool;
  1314. const struct mining_goal_info * const goal = pool->goal;
  1315. applog(LOG_DEBUG, "%s: %s with fifo_space=%d", dev->dev_repr, __func__, info->fifo_space);
  1316. if (!info->fifo_space) {
  1317. thr->queue_full = true;
  1318. return false;
  1319. }
  1320. const size_t buflen = 4 + 0x30 + 6;
  1321. uint8_t buf[buflen];
  1322. buf[0] = BITMAIN_TOKEN_TYPE_TXTASK;
  1323. buf[1] = 0; // packet version
  1324. pk_u16le(buf, 2, buflen - 4); // length of data after this field (including CRC)
  1325. buf[4] = 0; // set to (0x80 or 1??) to clear work queues (and skip fifo_space check above)
  1326. const int work_nonce_bmdiff = diff_to_bitmain(work->nonce_diff);
  1327. if (work_nonce_bmdiff < info->diff)
  1328. info->diff = work_nonce_bmdiff;
  1329. buf[5] = info->diff;
  1330. if (goal->current_diff < info->lowest_goal_diff)
  1331. info->lowest_goal_diff = goal->current_diff;
  1332. pk_u16le(buf, 6, diff_to_bitmain(info->lowest_goal_diff));
  1333. work->device_id = info->next_work_id++;
  1334. pk_u32le(buf, 8, work->device_id);
  1335. memcpy(&buf[0xc], work->midstate, 0x20);
  1336. memcpy(&buf[0x2c], &work->data[0x40], 0xc);
  1337. pk_u16le(buf, 0x38, CRC16(buf, 0x38));
  1338. int sendret = bitmain_send_data(buf, buflen, proc);
  1339. if (unlikely(sendret == BTM_SEND_ERROR)) {
  1340. applog(LOG_ERR, "%s: Comms error(buffer)", dev->dev_repr);
  1341. //dev_error(bitmain, REASON_DEV_COMMS_ERROR);
  1342. info->reset = true;
  1343. info->errorcount++;
  1344. if (info->errorcount > 1000) {
  1345. info->errorcount = 0;
  1346. applog(LOG_ERR, "%s: Device disappeared, shutting down thread", dev->dev_repr);
  1347. dev->shutdown = true;
  1348. }
  1349. return false;
  1350. } else {
  1351. applog(LOG_DEBUG, "bitmain_send_data send ret=%d", sendret);
  1352. info->errorcount = 0;
  1353. }
  1354. HASH_ADD(hh, master_thr->work_list, device_id, sizeof(work->device_id), work);
  1355. --info->fifo_space;
  1356. return true;
  1357. }
  1358. static void bitmain_queue_flush(struct thr_info * const thr)
  1359. {
  1360. // TODO
  1361. }
  1362. static struct api_data *bitmain_api_stats(struct cgpu_info *cgpu)
  1363. {
  1364. struct api_data *root = NULL;
  1365. struct bitmain_info *info = cgpu->device_data;
  1366. double hwp = (cgpu->hw_errors + cgpu->diff1) ?
  1367. (double)(cgpu->hw_errors) / (double)(cgpu->hw_errors + cgpu->diff1) : 0;
  1368. root = api_add_int(root, "baud", &(info->baud), false);
  1369. root = api_add_int(root, "miner_count", &(info->chain_num), false);
  1370. root = api_add_int(root, "asic_count", &(info->asic_num), false);
  1371. root = api_add_int(root, "timeout", &(info->timeout), false);
  1372. root = api_add_string(root, "frequency", info->frequency_t, false);
  1373. root = api_add_string(root, "voltage", info->voltage_t, false);
  1374. root = api_add_int(root, "hwv1", &(info->hw_version[0]), false);
  1375. root = api_add_int(root, "hwv2", &(info->hw_version[1]), false);
  1376. root = api_add_int(root, "hwv3", &(info->hw_version[2]), false);
  1377. root = api_add_int(root, "hwv4", &(info->hw_version[3]), false);
  1378. root = api_add_int(root, "fan_num", &(info->fan_num), false);
  1379. root = api_add_int(root, "fan1", &(info->fan[0]), false);
  1380. root = api_add_int(root, "fan2", &(info->fan[1]), false);
  1381. root = api_add_int(root, "fan3", &(info->fan[2]), false);
  1382. root = api_add_int(root, "fan4", &(info->fan[3]), false);
  1383. root = api_add_int(root, "fan5", &(info->fan[4]), false);
  1384. root = api_add_int(root, "fan6", &(info->fan[5]), false);
  1385. root = api_add_int(root, "fan7", &(info->fan[6]), false);
  1386. root = api_add_int(root, "fan8", &(info->fan[7]), false);
  1387. root = api_add_int(root, "fan9", &(info->fan[8]), false);
  1388. root = api_add_int(root, "fan10", &(info->fan[9]), false);
  1389. root = api_add_int(root, "fan11", &(info->fan[10]), false);
  1390. root = api_add_int(root, "fan12", &(info->fan[11]), false);
  1391. root = api_add_int(root, "fan13", &(info->fan[12]), false);
  1392. root = api_add_int(root, "fan14", &(info->fan[13]), false);
  1393. root = api_add_int(root, "fan15", &(info->fan[14]), false);
  1394. root = api_add_int(root, "fan16", &(info->fan[15]), false);
  1395. root = api_add_int(root, "temp_num", &(info->temp_num), false);
  1396. root = api_add_int(root, "temp1", &(info->temp[0]), false);
  1397. root = api_add_int(root, "temp2", &(info->temp[1]), false);
  1398. root = api_add_int(root, "temp3", &(info->temp[2]), false);
  1399. root = api_add_int(root, "temp4", &(info->temp[3]), false);
  1400. root = api_add_int(root, "temp5", &(info->temp[4]), false);
  1401. root = api_add_int(root, "temp6", &(info->temp[5]), false);
  1402. root = api_add_int(root, "temp7", &(info->temp[6]), false);
  1403. root = api_add_int(root, "temp8", &(info->temp[7]), false);
  1404. root = api_add_int(root, "temp9", &(info->temp[8]), false);
  1405. root = api_add_int(root, "temp10", &(info->temp[9]), false);
  1406. root = api_add_int(root, "temp11", &(info->temp[10]), false);
  1407. root = api_add_int(root, "temp12", &(info->temp[11]), false);
  1408. root = api_add_int(root, "temp13", &(info->temp[12]), false);
  1409. root = api_add_int(root, "temp14", &(info->temp[13]), false);
  1410. root = api_add_int(root, "temp15", &(info->temp[14]), false);
  1411. root = api_add_int(root, "temp16", &(info->temp[15]), false);
  1412. root = api_add_int(root, "temp_avg", &(info->temp_avg), false);
  1413. root = api_add_int(root, "temp_max", &(info->temp_max), false);
  1414. root = api_add_percent(root, "Device Hardware%", &hwp, true);
  1415. root = api_add_int(root, "no_matching_work", &(info->no_matching_work), false);
  1416. /*
  1417. for (int i = 0; i < info->chain_num; ++i) {
  1418. char mcw[24];
  1419. sprintf(mcw, "match_work_count%d", i + 1);
  1420. root = api_add_int(root, mcw, &(info->matching_work[i]), false);
  1421. }*/
  1422. root = api_add_int(root, "chain_acn1", &(info->chain_asic_num[0]), false);
  1423. root = api_add_int(root, "chain_acn2", &(info->chain_asic_num[1]), false);
  1424. root = api_add_int(root, "chain_acn3", &(info->chain_asic_num[2]), false);
  1425. root = api_add_int(root, "chain_acn4", &(info->chain_asic_num[3]), false);
  1426. root = api_add_int(root, "chain_acn5", &(info->chain_asic_num[4]), false);
  1427. root = api_add_int(root, "chain_acn6", &(info->chain_asic_num[5]), false);
  1428. root = api_add_int(root, "chain_acn7", &(info->chain_asic_num[6]), false);
  1429. root = api_add_int(root, "chain_acn8", &(info->chain_asic_num[7]), false);
  1430. root = api_add_int(root, "chain_acn9", &(info->chain_asic_num[8]), false);
  1431. root = api_add_int(root, "chain_acn10", &(info->chain_asic_num[9]), false);
  1432. root = api_add_int(root, "chain_acn11", &(info->chain_asic_num[10]), false);
  1433. root = api_add_int(root, "chain_acn12", &(info->chain_asic_num[11]), false);
  1434. root = api_add_int(root, "chain_acn13", &(info->chain_asic_num[12]), false);
  1435. root = api_add_int(root, "chain_acn14", &(info->chain_asic_num[13]), false);
  1436. root = api_add_int(root, "chain_acn15", &(info->chain_asic_num[14]), false);
  1437. root = api_add_int(root, "chain_acn16", &(info->chain_asic_num[15]), false);
  1438. //applog(LOG_ERR, "chain asic status:%s", info->chain_asic_status_t[0]);
  1439. root = api_add_string(root, "chain_acs1", info->chain_asic_status_t[0], false);
  1440. root = api_add_string(root, "chain_acs2", info->chain_asic_status_t[1], false);
  1441. root = api_add_string(root, "chain_acs3", info->chain_asic_status_t[2], false);
  1442. root = api_add_string(root, "chain_acs4", info->chain_asic_status_t[3], false);
  1443. root = api_add_string(root, "chain_acs5", info->chain_asic_status_t[4], false);
  1444. root = api_add_string(root, "chain_acs6", info->chain_asic_status_t[5], false);
  1445. root = api_add_string(root, "chain_acs7", info->chain_asic_status_t[6], false);
  1446. root = api_add_string(root, "chain_acs8", info->chain_asic_status_t[7], false);
  1447. root = api_add_string(root, "chain_acs9", info->chain_asic_status_t[8], false);
  1448. root = api_add_string(root, "chain_acs10", info->chain_asic_status_t[9], false);
  1449. root = api_add_string(root, "chain_acs11", info->chain_asic_status_t[10], false);
  1450. root = api_add_string(root, "chain_acs12", info->chain_asic_status_t[11], false);
  1451. root = api_add_string(root, "chain_acs13", info->chain_asic_status_t[12], false);
  1452. root = api_add_string(root, "chain_acs14", info->chain_asic_status_t[13], false);
  1453. root = api_add_string(root, "chain_acs15", info->chain_asic_status_t[14], false);
  1454. root = api_add_string(root, "chain_acs16", info->chain_asic_status_t[15], false);
  1455. //root = api_add_int(root, "chain_acs1", &(info->chain_asic_status[0]), false);
  1456. //root = api_add_int(root, "chain_acs2", &(info->chain_asic_status[1]), false);
  1457. //root = api_add_int(root, "chain_acs3", &(info->chain_asic_status[2]), false);
  1458. //root = api_add_int(root, "chain_acs4", &(info->chain_asic_status[3]), false);
  1459. return root;
  1460. }
  1461. static void bitmain_shutdown(struct thr_info *thr)
  1462. {
  1463. do_bitmain_close(thr);
  1464. }
  1465. static
  1466. const char *bitmain_set_baud(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1467. {
  1468. struct bitmain_info *info = proc->device_data;
  1469. const int baud = atoi(newvalue);
  1470. if (!valid_baud(baud))
  1471. return "Invalid baud setting";
  1472. info->baud = baud;
  1473. return NULL;
  1474. }
  1475. static
  1476. const char *bitmain_set_layout(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1477. {
  1478. struct bitmain_info *info = proc->device_data;
  1479. char *endptr, *next_field;
  1480. const long int n_chains = strtol(newvalue, &endptr, 0);
  1481. if (endptr == newvalue || n_chains < 1)
  1482. return "Missing chain count";
  1483. long int n_asics = 0;
  1484. if (endptr[0] == ':' || endptr[1] == ',')
  1485. {
  1486. next_field = &endptr[1];
  1487. n_asics = strtol(next_field, &endptr, 0);
  1488. }
  1489. if (n_asics < 1)
  1490. return "Missing ASIC count";
  1491. if (n_asics > BITMAIN_DEFAULT_ASIC_NUM)
  1492. return "ASIC count too high";
  1493. info->chain_num = n_chains;
  1494. info->asic_num = n_asics;
  1495. return NULL;
  1496. }
  1497. static
  1498. const char *bitmain_set_timeout(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1499. {
  1500. struct bitmain_info *info = proc->device_data;
  1501. const int timeout = atoi(newvalue);
  1502. if (timeout < 0 || timeout > 0xff)
  1503. return "Invalid timeout setting";
  1504. info->timeout = timeout;
  1505. return NULL;
  1506. }
  1507. static
  1508. const char *bitmain_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1509. {
  1510. struct bitmain_info *info = proc->device_data;
  1511. const int freq = atoi(newvalue);
  1512. if (freq < BITMAIN_MIN_FREQUENCY || freq > BITMAIN_MAX_FREQUENCY)
  1513. return "Invalid clock frequency";
  1514. info->frequency = freq;
  1515. sprintf(info->frequency_t, "%d", freq);
  1516. return NULL;
  1517. }
  1518. static
  1519. const char *bitmain_set_reg_data(struct cgpu_info * const proc, const char * const optname, const char *newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1520. {
  1521. struct bitmain_info *info = proc->device_data;
  1522. uint8_t reg_data[4] = {0};
  1523. if (newvalue[0] == 'x')
  1524. ++newvalue;
  1525. size_t nvlen = strlen(newvalue);
  1526. if (nvlen > (sizeof(reg_data) * 2) || !nvlen || nvlen % 2)
  1527. return "reg_data must be a hex string of 2-8 digits (1-4 bytes)";
  1528. if (!hex2bin(reg_data, newvalue, nvlen / 2))
  1529. return "Invalid reg data hex";
  1530. memcpy(info->reg_data, reg_data, sizeof(reg_data));
  1531. return NULL;
  1532. }
  1533. static
  1534. const char *bitmain_set_voltage(struct cgpu_info * const proc, const char * const optname, const char *newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1535. {
  1536. struct bitmain_info *info = proc->device_data;
  1537. uint8_t voltage_data[2] = {0};
  1538. if (newvalue[0] == 'x')
  1539. ++newvalue;
  1540. else
  1541. voltage_usage:
  1542. return "voltage must be 'x' followed by a hex string of 1-4 digits (1-2 bytes)";
  1543. size_t nvlen = strlen(newvalue);
  1544. if (nvlen > (sizeof(voltage_data) * 2) || !nvlen || nvlen % 2)
  1545. goto voltage_usage;
  1546. if (!hex2bin(voltage_data, newvalue, nvlen / 2))
  1547. return "Invalid voltage data hex";
  1548. memcpy(info->voltage, voltage_data, sizeof(voltage_data));
  1549. bin2hex(info->voltage_t, voltage_data, 2);
  1550. info->voltage_t[5] = 0;
  1551. info->voltage_t[4] = info->voltage_t[3];
  1552. info->voltage_t[3] = info->voltage_t[2];
  1553. info->voltage_t[2] = info->voltage_t[1];
  1554. info->voltage_t[1] = '.';
  1555. return NULL;
  1556. }
  1557. static const struct bfg_set_device_definition bitmain_set_device_funcs_init[] = {
  1558. {"baud", bitmain_set_baud, "serial baud rate"},
  1559. {"layout", bitmain_set_layout, "number of chains ':' number of ASICs per chain (eg: 32:8)"},
  1560. {"timeout", bitmain_set_timeout, "timeout"},
  1561. {"clock", bitmain_set_clock, "clock frequency"},
  1562. {"reg_data", bitmain_set_reg_data, "reg_data (eg: x0d82)"},
  1563. {"voltage", bitmain_set_voltage, "voltage (must be specified as 'x' and hex data; eg: x0725)"},
  1564. {NULL},
  1565. };
  1566. struct device_drv bitmain_drv = {
  1567. .dname = "bitmain",
  1568. .name = "BTM",
  1569. .drv_detect = bitmain_detect,
  1570. .thread_prepare = bitmain_prepare,
  1571. .minerloop = minerloop_queue,
  1572. .queue_append = bitmain_queue_append,
  1573. .queue_flush = bitmain_queue_flush,
  1574. .poll = bitmain_poll,
  1575. .get_api_stats = bitmain_api_stats,
  1576. .reinit_device = bitmain_init,
  1577. .thread_shutdown = bitmain_shutdown,
  1578. };