usbutils.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <ctype.h>
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include "logging.h"
  14. #include "miner.h"
  15. #include "usbutils.h"
  16. #define NODEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
  17. (err) == LIBUSB_ERROR_PIPE || \
  18. (err) == LIBUSB_ERROR_OTHER)
  19. #ifdef USE_BFLSC
  20. #define DRV_BFLSC 1
  21. #endif
  22. #ifdef USE_BITFORCE
  23. #define DRV_BITFORCE 2
  24. #endif
  25. #ifdef USE_MODMINER
  26. #define DRV_MODMINER 3
  27. #endif
  28. #ifdef USE_ZTEX
  29. #define DRV_ZTEX 4
  30. #endif
  31. #ifdef USE_ICARUS
  32. #define DRV_ICARUS 5
  33. #endif
  34. #ifdef USE_AVALON
  35. #define DRV_AVALON 6
  36. #endif
  37. #define DRV_LAST -1
  38. #define USB_CONFIG 1
  39. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  40. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  41. #ifdef WIN32
  42. #define BFLSC_TIMEOUT_MS 500
  43. #define BITFORCE_TIMEOUT_MS 999
  44. #define MODMINER_TIMEOUT_MS 200
  45. #define AVALON_TIMEOUT_MS 500
  46. #define ICARUS_TIMEOUT_MS 500
  47. #else
  48. #define BFLSC_TIMEOUT_MS 200
  49. #define BITFORCE_TIMEOUT_MS 200
  50. #define MODMINER_TIMEOUT_MS 100
  51. #define AVALON_TIMEOUT_MS 200
  52. #define ICARUS_TIMEOUT_MS 200
  53. #endif
  54. #ifdef USE_BFLSC
  55. // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
  56. static struct usb_endpoints bas_eps[] = {
  57. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  58. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  59. };
  60. #endif
  61. #ifdef USE_BITFORCE
  62. // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
  63. static struct usb_endpoints bfl_eps[] = {
  64. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  65. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  66. };
  67. #endif
  68. #ifdef USE_MODMINER
  69. static struct usb_endpoints mmq_eps[] = {
  70. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
  71. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0 }
  72. };
  73. #endif
  74. #ifdef USE_AVALON
  75. static struct usb_endpoints ava_eps[] = {
  76. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  77. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  78. };
  79. #endif
  80. #ifdef USE_ICARUS
  81. static struct usb_endpoints ica_eps[] = {
  82. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
  83. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  84. };
  85. static struct usb_endpoints amu_eps[] = {
  86. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  87. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0 }
  88. };
  89. static struct usb_endpoints llt_eps[] = {
  90. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  91. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  92. };
  93. static struct usb_endpoints cmr_eps[] = {
  94. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  95. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  96. };
  97. #endif
  98. #define IDVENDOR_FTDI 0x0403
  99. // TODO: Add support for (at least) Isochronous endpoints
  100. static struct usb_find_devices find_dev[] = {
  101. #ifdef USE_BFLSC
  102. {
  103. .drv = DRV_BFLSC,
  104. .name = "BAS",
  105. .ident = IDENT_BAS,
  106. .idVendor = IDVENDOR_FTDI,
  107. .idProduct = 0x6014,
  108. .iManufacturer = "Butterfly Labs",
  109. .iProduct = "BitFORCE SHA256 SC",
  110. .kernel = 0,
  111. .config = 1,
  112. .interface = 0,
  113. .timeout = BFLSC_TIMEOUT_MS,
  114. .epcount = ARRAY_SIZE(bas_eps),
  115. .eps = bas_eps },
  116. #endif
  117. #ifdef USE_BITFORCE
  118. {
  119. .drv = DRV_BITFORCE,
  120. .name = "BFL",
  121. .ident = IDENT_BFL,
  122. .idVendor = IDVENDOR_FTDI,
  123. .idProduct = 0x6014,
  124. .iManufacturer = "Butterfly Labs Inc.",
  125. .iProduct = "BitFORCE SHA256",
  126. .kernel = 0,
  127. .config = 1,
  128. .interface = 0,
  129. .timeout = BITFORCE_TIMEOUT_MS,
  130. .epcount = ARRAY_SIZE(bfl_eps),
  131. .eps = bfl_eps },
  132. #endif
  133. #ifdef USE_MODMINER
  134. {
  135. .drv = DRV_MODMINER,
  136. .name = "MMQ",
  137. .ident = IDENT_MMQ,
  138. .idVendor = 0x1fc9,
  139. .idProduct = 0x0003,
  140. .kernel = 0,
  141. .config = 1,
  142. .interface = 1,
  143. .timeout = MODMINER_TIMEOUT_MS,
  144. .epcount = ARRAY_SIZE(mmq_eps),
  145. .eps = mmq_eps },
  146. #endif
  147. #ifdef USE_AVALON
  148. {
  149. .drv = DRV_AVALON,
  150. .name = "AVA",
  151. .ident = IDENT_AVA,
  152. .idVendor = IDVENDOR_FTDI,
  153. .idProduct = 0x6001,
  154. .kernel = 0,
  155. .config = 1,
  156. .interface = 0,
  157. .timeout = AVALON_TIMEOUT_MS,
  158. .epcount = ARRAY_SIZE(ava_eps),
  159. .eps = ava_eps },
  160. #endif
  161. #ifdef USE_ICARUS
  162. {
  163. .drv = DRV_ICARUS,
  164. .name = "ICA",
  165. .ident = IDENT_ICA,
  166. .idVendor = 0x067b,
  167. .idProduct = 0x2303,
  168. .kernel = 0,
  169. .config = 1,
  170. .interface = 0,
  171. .timeout = ICARUS_TIMEOUT_MS,
  172. .epcount = ARRAY_SIZE(ica_eps),
  173. .eps = ica_eps },
  174. {
  175. .drv = DRV_ICARUS,
  176. .name = "AMU",
  177. .ident = IDENT_AMU,
  178. .idVendor = 0x10c4,
  179. .idProduct = 0xea60,
  180. .kernel = 0,
  181. .config = 1,
  182. .interface = 0,
  183. .timeout = ICARUS_TIMEOUT_MS,
  184. .epcount = ARRAY_SIZE(amu_eps),
  185. .eps = amu_eps },
  186. {
  187. .drv = DRV_ICARUS,
  188. .name = "BLT",
  189. .ident = IDENT_BLT,
  190. .idVendor = IDVENDOR_FTDI,
  191. .idProduct = 0x6001,
  192. .iProduct = "FT232R USB UART",
  193. .kernel = 0,
  194. .config = 1,
  195. .interface = 0,
  196. .timeout = ICARUS_TIMEOUT_MS,
  197. .epcount = ARRAY_SIZE(llt_eps),
  198. .eps = llt_eps },
  199. // For any that don't match the above "BLT"
  200. {
  201. .drv = DRV_ICARUS,
  202. .name = "LLT",
  203. .ident = IDENT_LLT,
  204. .idVendor = IDVENDOR_FTDI,
  205. .idProduct = 0x6001,
  206. .kernel = 0,
  207. .config = 1,
  208. .interface = 0,
  209. .timeout = ICARUS_TIMEOUT_MS,
  210. .epcount = ARRAY_SIZE(llt_eps),
  211. .eps = llt_eps },
  212. {
  213. .drv = DRV_ICARUS,
  214. .name = "CMR",
  215. .ident = IDENT_CMR,
  216. .idVendor = 0x067b,
  217. .idProduct = 0x0230,
  218. .iProduct = "Cairnsmore1",
  219. .kernel = 0,
  220. .config = 1,
  221. .interface = 0,
  222. .timeout = ICARUS_TIMEOUT_MS,
  223. .epcount = ARRAY_SIZE(cmr_eps),
  224. .eps = cmr_eps },
  225. #endif
  226. #ifdef USE_ZTEX
  227. // This is here so cgminer -n shows them
  228. // the ztex driver (as at 201303) doesn't use usbutils
  229. {
  230. .drv = DRV_ZTEX,
  231. .name = "ZTX",
  232. .ident = IDENT_ZTX,
  233. .idVendor = 0x221a,
  234. .idProduct = 0x0100,
  235. .kernel = 0,
  236. .config = 1,
  237. .interface = 1,
  238. .timeout = 100,
  239. .epcount = 0,
  240. .eps = NULL },
  241. #endif
  242. { DRV_LAST, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, NULL }
  243. };
  244. #ifdef USE_BFLSC
  245. extern struct device_drv bflsc_drv;
  246. #endif
  247. #ifdef USE_BITFORCE
  248. extern struct device_drv bitforce_drv;
  249. #endif
  250. #ifdef USE_MODMINER
  251. extern struct device_drv modminer_drv;
  252. #endif
  253. #ifdef USE_ICARUS
  254. extern struct device_drv icarus_drv;
  255. #endif
  256. #ifdef USE_AVALON
  257. extern struct device_drv avalon_drv;
  258. #endif
  259. #define STRBUFLEN 256
  260. static const char *BLANK = "";
  261. static const char *space = " ";
  262. static const char *nodatareturned = "no data returned ";
  263. #if 0 // enable USBDEBUG - only during development testing
  264. static const char *debug_true_str = "true";
  265. static const char *debug_false_str = "false";
  266. static const char *nodevstr = "=NODEV";
  267. #define bool_str(boo) ((boo) ? debug_true_str : debug_false_str)
  268. #define isnodev(err) (NODEV(err) ? nodevstr : BLANK)
  269. #define USBDEBUG(fmt, ...) applog(LOG_WARNING, fmt, ##__VA_ARGS__)
  270. #else
  271. #define USBDEBUG(fmt, ...)
  272. #endif
  273. // For device limits by driver
  274. static struct driver_count {
  275. uint32_t count;
  276. uint32_t limit;
  277. } drv_count[DRIVER_MAX];
  278. // For device limits by list of bus/dev
  279. static struct usb_busdev {
  280. int bus_number;
  281. int device_address;
  282. } *busdev;
  283. static int busdev_count = 0;
  284. // Total device limit
  285. static int total_count = 0;
  286. static int total_limit = 999999;
  287. struct usb_in_use_list {
  288. struct usb_busdev in_use;
  289. struct usb_in_use_list *prev;
  290. struct usb_in_use_list *next;
  291. };
  292. // List of in use devices
  293. static struct usb_in_use_list *in_use_head = NULL;
  294. static bool stats_initialised = false;
  295. struct cg_usb_stats_item {
  296. uint64_t count;
  297. double total_delay;
  298. double min_delay;
  299. double max_delay;
  300. struct timeval first;
  301. struct timeval last;
  302. };
  303. #define CMD_CMD 0
  304. #define CMD_TIMEOUT 1
  305. #define CMD_ERROR 2
  306. struct cg_usb_stats_details {
  307. int seq;
  308. struct cg_usb_stats_item item[CMD_ERROR+1];
  309. };
  310. struct cg_usb_stats {
  311. char *name;
  312. int device_id;
  313. struct cg_usb_stats_details *details;
  314. };
  315. #define SEQ0 0
  316. #define SEQ1 1
  317. static struct cg_usb_stats *usb_stats = NULL;
  318. static int next_stat = 0;
  319. static const char **usb_commands;
  320. static const char *C_REJECTED_S = "RejectedNoDevice";
  321. static const char *C_PING_S = "Ping";
  322. static const char *C_CLEAR_S = "Clear";
  323. static const char *C_REQUESTVERSION_S = "RequestVersion";
  324. static const char *C_GETVERSION_S = "GetVersion";
  325. static const char *C_REQUESTFPGACOUNT_S = "RequestFPGACount";
  326. static const char *C_GETFPGACOUNT_S = "GetFPGACount";
  327. static const char *C_STARTPROGRAM_S = "StartProgram";
  328. static const char *C_STARTPROGRAMSTATUS_S = "StartProgramStatus";
  329. static const char *C_PROGRAM_S = "Program";
  330. static const char *C_PROGRAMSTATUS_S = "ProgramStatus";
  331. static const char *C_PROGRAMSTATUS2_S = "ProgramStatus2";
  332. static const char *C_FINALPROGRAMSTATUS_S = "FinalProgramStatus";
  333. static const char *C_SETCLOCK_S = "SetClock";
  334. static const char *C_REPLYSETCLOCK_S = "ReplySetClock";
  335. static const char *C_REQUESTUSERCODE_S = "RequestUserCode";
  336. static const char *C_GETUSERCODE_S = "GetUserCode";
  337. static const char *C_REQUESTTEMPERATURE_S = "RequestTemperature";
  338. static const char *C_GETTEMPERATURE_S = "GetTemperature";
  339. static const char *C_SENDWORK_S = "SendWork";
  340. static const char *C_SENDWORKSTATUS_S = "SendWorkStatus";
  341. static const char *C_REQUESTWORKSTATUS_S = "RequestWorkStatus";
  342. static const char *C_GETWORKSTATUS_S = "GetWorkStatus";
  343. static const char *C_REQUESTIDENTIFY_S = "RequestIdentify";
  344. static const char *C_GETIDENTIFY_S = "GetIdentify";
  345. static const char *C_REQUESTFLASH_S = "RequestFlash";
  346. static const char *C_REQUESTSENDWORK_S = "RequestSendWork";
  347. static const char *C_REQUESTSENDWORKSTATUS_S = "RequestSendWorkStatus";
  348. static const char *C_RESET_S = "Reset";
  349. static const char *C_SETBAUD_S = "SetBaud";
  350. static const char *C_SETDATA_S = "SetDataCtrl";
  351. static const char *C_SETFLOW_S = "SetFlowCtrl";
  352. static const char *C_SETMODEM_S = "SetModemCtrl";
  353. static const char *C_PURGERX_S = "PurgeRx";
  354. static const char *C_PURGETX_S = "PurgeTx";
  355. static const char *C_FLASHREPLY_S = "FlashReply";
  356. static const char *C_REQUESTDETAILS_S = "RequestDetails";
  357. static const char *C_GETDETAILS_S = "GetDetails";
  358. static const char *C_REQUESTRESULTS_S = "RequestResults";
  359. static const char *C_GETRESULTS_S = "GetResults";
  360. static const char *C_REQUESTQUEJOB_S = "RequestQueJob";
  361. static const char *C_REQUESTQUEJOBSTATUS_S = "RequestQueJobStatus";
  362. static const char *C_QUEJOB_S = "QueJob";
  363. static const char *C_QUEJOBSTATUS_S = "QueJobStatus";
  364. static const char *C_QUEFLUSH_S = "QueFlush";
  365. static const char *C_QUEFLUSHREPLY_S = "QueFlushReply";
  366. static const char *C_REQUESTVOLTS_S = "RequestVolts";
  367. static const char *C_SENDTESTWORK_S = "SendTestWork";
  368. static const char *C_LATENCY_S = "SetLatency";
  369. #ifdef EOL
  370. #undef EOL
  371. #endif
  372. #define EOL "\n"
  373. static const char *DESDEV = "Device";
  374. static const char *DESCON = "Config";
  375. static const char *DESSTR = "String";
  376. static const char *DESINT = "Interface";
  377. static const char *DESEP = "Endpoint";
  378. static const char *DESHID = "HID";
  379. static const char *DESRPT = "Report";
  380. static const char *DESPHY = "Physical";
  381. static const char *DESHUB = "Hub";
  382. static const char *EPIN = "In: ";
  383. static const char *EPOUT = "Out: ";
  384. static const char *EPX = "?: ";
  385. static const char *CONTROL = "Control";
  386. static const char *ISOCHRONOUS_X = "Isochronous+?";
  387. static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
  388. static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
  389. static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
  390. static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
  391. static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
  392. static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
  393. static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
  394. static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
  395. static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
  396. static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
  397. static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
  398. static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
  399. static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
  400. static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
  401. static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
  402. static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
  403. static const char *BULK = "Bulk";
  404. static const char *INTERRUPT = "Interrupt";
  405. static const char *UNKNOWN = "Unknown";
  406. static const char *err_io_str = " IO Error";
  407. static const char *err_access_str = " Access Denied-a";
  408. static const char *err_timeout_str = " Reply Timeout";
  409. static const char *err_pipe_str = " Access denied-p";
  410. static const char *err_other_str = " Access denied-o";
  411. static const char *usberrstr(int err)
  412. {
  413. switch (err) {
  414. case LIBUSB_ERROR_IO:
  415. return err_io_str;
  416. case LIBUSB_ERROR_ACCESS:
  417. return err_access_str;
  418. case LIBUSB_ERROR_TIMEOUT:
  419. return err_timeout_str;
  420. case LIBUSB_ERROR_PIPE:
  421. return err_pipe_str;
  422. case LIBUSB_ERROR_OTHER:
  423. return err_other_str;
  424. }
  425. return BLANK;
  426. }
  427. static const char *destype(uint8_t bDescriptorType)
  428. {
  429. switch (bDescriptorType) {
  430. case LIBUSB_DT_DEVICE:
  431. return DESDEV;
  432. case LIBUSB_DT_CONFIG:
  433. return DESCON;
  434. case LIBUSB_DT_STRING:
  435. return DESSTR;
  436. case LIBUSB_DT_INTERFACE:
  437. return DESINT;
  438. case LIBUSB_DT_ENDPOINT:
  439. return DESEP;
  440. case LIBUSB_DT_HID:
  441. return DESHID;
  442. case LIBUSB_DT_REPORT:
  443. return DESRPT;
  444. case LIBUSB_DT_PHYSICAL:
  445. return DESPHY;
  446. case LIBUSB_DT_HUB:
  447. return DESHUB;
  448. }
  449. return UNKNOWN;
  450. }
  451. static const char *epdir(uint8_t bEndpointAddress)
  452. {
  453. switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
  454. case LIBUSB_ENDPOINT_IN:
  455. return EPIN;
  456. case LIBUSB_ENDPOINT_OUT:
  457. return EPOUT;
  458. }
  459. return EPX;
  460. }
  461. static const char *epatt(uint8_t bmAttributes)
  462. {
  463. switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
  464. case LIBUSB_TRANSFER_TYPE_CONTROL:
  465. return CONTROL;
  466. case LIBUSB_TRANSFER_TYPE_BULK:
  467. return BULK;
  468. case LIBUSB_TRANSFER_TYPE_INTERRUPT:
  469. return INTERRUPT;
  470. case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
  471. switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
  472. case LIBUSB_ISO_SYNC_TYPE_NONE:
  473. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  474. case LIBUSB_ISO_USAGE_TYPE_DATA:
  475. return ISOCHRONOUS_N_D;
  476. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  477. return ISOCHRONOUS_N_F;
  478. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  479. return ISOCHRONOUS_N_I;
  480. }
  481. return ISOCHRONOUS_N_X;
  482. case LIBUSB_ISO_SYNC_TYPE_ASYNC:
  483. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  484. case LIBUSB_ISO_USAGE_TYPE_DATA:
  485. return ISOCHRONOUS_A_D;
  486. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  487. return ISOCHRONOUS_A_F;
  488. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  489. return ISOCHRONOUS_A_I;
  490. }
  491. return ISOCHRONOUS_A_X;
  492. case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
  493. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  494. case LIBUSB_ISO_USAGE_TYPE_DATA:
  495. return ISOCHRONOUS_D_D;
  496. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  497. return ISOCHRONOUS_D_F;
  498. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  499. return ISOCHRONOUS_D_I;
  500. }
  501. return ISOCHRONOUS_D_X;
  502. case LIBUSB_ISO_SYNC_TYPE_SYNC:
  503. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  504. case LIBUSB_ISO_USAGE_TYPE_DATA:
  505. return ISOCHRONOUS_S_D;
  506. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  507. return ISOCHRONOUS_S_F;
  508. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  509. return ISOCHRONOUS_S_I;
  510. }
  511. return ISOCHRONOUS_S_X;
  512. }
  513. return ISOCHRONOUS_X;
  514. }
  515. return UNKNOWN;
  516. }
  517. static void append(char **buf, char *append, size_t *off, size_t *len)
  518. {
  519. int new = strlen(append);
  520. if ((new + *off) >= *len)
  521. {
  522. *len *= 2;
  523. *buf = realloc(*buf, *len);
  524. }
  525. strcpy(*buf + *off, append);
  526. *off += new;
  527. }
  528. static bool setgetdes(ssize_t count, libusb_device *dev, struct libusb_device_handle *handle, struct libusb_config_descriptor **config, int cd, char **buf, size_t *off, size_t *len)
  529. {
  530. char tmp[512];
  531. int err;
  532. err = libusb_set_configuration(handle, cd);
  533. if (err) {
  534. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
  535. (int)count, cd, err);
  536. append(buf, tmp, off, len);
  537. return false;
  538. }
  539. err = libusb_get_active_config_descriptor(dev, config);
  540. if (err) {
  541. sprintf(tmp, EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
  542. (int)count, cd, err);
  543. append(buf, tmp, off, len);
  544. return false;
  545. }
  546. sprintf(tmp, EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
  547. (int)count, cd, err);
  548. append(buf, tmp, off, len);
  549. return true;
  550. }
  551. static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
  552. {
  553. struct libusb_device_descriptor desc;
  554. uint8_t bus_number;
  555. uint8_t device_address;
  556. struct libusb_device_handle *handle;
  557. struct libusb_config_descriptor *config;
  558. const struct libusb_interface_descriptor *idesc;
  559. const struct libusb_endpoint_descriptor *epdesc;
  560. unsigned char man[STRBUFLEN+1];
  561. unsigned char prod[STRBUFLEN+1];
  562. unsigned char ser[STRBUFLEN+1];
  563. char tmp[512];
  564. int err, i, j, k;
  565. err = libusb_get_device_descriptor(dev, &desc);
  566. if (opt_usb_list_all && err) {
  567. sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
  568. (int)(++(*count)), err);
  569. append(buf, tmp, off, len);
  570. return;
  571. }
  572. bus_number = libusb_get_bus_number(dev);
  573. device_address = libusb_get_device_address(dev);
  574. if (!opt_usb_list_all) {
  575. bool known = false;
  576. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  577. if ((find_dev[i].idVendor == desc.idVendor) &&
  578. (find_dev[i].idProduct == desc.idProduct)) {
  579. known = true;
  580. break;
  581. }
  582. if (!known)
  583. return;
  584. }
  585. (*count)++;
  586. if (level == 0) {
  587. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
  588. (int)(*count), (int)bus_number, (int)device_address,
  589. desc.idVendor, desc.idProduct);
  590. } else {
  591. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
  592. "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
  593. "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
  594. "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
  595. "\tNumConfigurations: %d",
  596. (int)(*count), (int)bus_number, (int)device_address,
  597. (int)(desc.bLength), destype(desc.bDescriptorType),
  598. desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
  599. (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
  600. desc.idVendor, desc.idProduct, desc.bcdDevice,
  601. (int)(desc.bNumConfigurations));
  602. }
  603. append(buf, tmp, off, len);
  604. err = libusb_open(dev, &handle);
  605. if (err) {
  606. sprintf(tmp, EOL " ** dev %d: Failed to open, err %d", (int)(*count), err);
  607. append(buf, tmp, off, len);
  608. return;
  609. }
  610. err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
  611. if (err < 0)
  612. sprintf((char *)man, "** err(%d)%s", err, usberrstr(err));
  613. err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
  614. if (err < 0)
  615. sprintf((char *)prod, "** err(%d)%s", err, usberrstr(err));
  616. if (level == 0) {
  617. libusb_close(handle);
  618. sprintf(tmp, EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
  619. append(buf, tmp, off, len);
  620. return;
  621. }
  622. if (libusb_kernel_driver_active(handle, 0) == 1) {
  623. sprintf(tmp, EOL " * dev %d: kernel attached", (int)(*count));
  624. append(buf, tmp, off, len);
  625. }
  626. err = libusb_get_active_config_descriptor(dev, &config);
  627. if (err) {
  628. if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
  629. && !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
  630. libusb_close(handle);
  631. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d or %d",
  632. (int)(*count), 1, 0);
  633. append(buf, tmp, off, len);
  634. return;
  635. }
  636. }
  637. sprintf(tmp, EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
  638. "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
  639. "\tAttributes: %d" EOL "\tMaxPower: %d",
  640. (int)(*count), destype(config->bDescriptorType),
  641. (int)(config->bNumInterfaces), (int)(config->iConfiguration),
  642. (int)(config->bmAttributes), (int)(config->MaxPower));
  643. append(buf, tmp, off, len);
  644. for (i = 0; i < (int)(config->bNumInterfaces); i++) {
  645. for (j = 0; j < config->interface[i].num_altsetting; j++) {
  646. idesc = &(config->interface[i].altsetting[j]);
  647. sprintf(tmp, EOL " _dev %d: Interface Descriptor %d:" EOL
  648. "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
  649. "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
  650. "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
  651. (int)(*count), j, destype(idesc->bDescriptorType),
  652. (int)(idesc->bInterfaceNumber),
  653. (int)(idesc->bNumEndpoints),
  654. (int)(idesc->bInterfaceClass),
  655. (int)(idesc->bInterfaceSubClass),
  656. (int)(idesc->bInterfaceProtocol));
  657. append(buf, tmp, off, len);
  658. for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
  659. epdesc = &(idesc->endpoint[k]);
  660. sprintf(tmp, EOL " __dev %d: Interface %d Endpoint %d:" EOL
  661. "\tDescriptorType: %s" EOL
  662. "\tEndpointAddress: %s0x%x" EOL
  663. "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
  664. "\tInterval: %d" EOL "\tRefresh: %d",
  665. (int)(*count), (int)(idesc->bInterfaceNumber), k,
  666. destype(epdesc->bDescriptorType),
  667. epdir(epdesc->bEndpointAddress),
  668. (int)(epdesc->bEndpointAddress),
  669. epatt(epdesc->bmAttributes),
  670. epdesc->wMaxPacketSize,
  671. (int)(epdesc->bInterval),
  672. (int)(epdesc->bRefresh));
  673. append(buf, tmp, off, len);
  674. }
  675. }
  676. }
  677. libusb_free_config_descriptor(config);
  678. config = NULL;
  679. err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
  680. if (err < 0)
  681. sprintf((char *)ser, "** err(%d)%s", err, usberrstr(err));
  682. sprintf(tmp, EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
  683. "\tProduct: '%s'" EOL "\tSerial '%s'",
  684. (int)(*count), man, prod, ser);
  685. append(buf, tmp, off, len);
  686. libusb_close(handle);
  687. }
  688. // Function to dump all USB devices
  689. void usb_all(int level)
  690. {
  691. libusb_device **list;
  692. ssize_t count, i, j;
  693. char *buf;
  694. size_t len, off;
  695. count = libusb_get_device_list(NULL, &list);
  696. if (count < 0) {
  697. applog(LOG_ERR, "USB all: failed, err %d%s", (int)count, usberrstr((int)count));
  698. return;
  699. }
  700. if (count == 0)
  701. applog(LOG_WARNING, "USB all: found no devices");
  702. else
  703. {
  704. len = 10000;
  705. buf = malloc(len+1);
  706. sprintf(buf, "USB all: found %d devices", (int)count);
  707. off = strlen(buf);
  708. if (!opt_usb_list_all)
  709. append(&buf, " - listing known devices", &off, &len);
  710. j = -1;
  711. for (i = 0; i < count; i++)
  712. usb_full(&j, list[i], &buf, &off, &len, level);
  713. applog(LOG_WARNING, "%s", buf);
  714. free(buf);
  715. if (j == -1)
  716. applog(LOG_WARNING, "No known USB devices");
  717. else
  718. applog(LOG_WARNING, "%d %sUSB devices",
  719. (int)(++j), opt_usb_list_all ? BLANK : "known ");
  720. }
  721. libusb_free_device_list(list, 1);
  722. }
  723. static void cgusb_check_init()
  724. {
  725. mutex_lock(&cgusb_lock);
  726. if (stats_initialised == false) {
  727. // N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
  728. if (opt_usbdump >= 0) {
  729. libusb_set_debug(NULL, opt_usbdump);
  730. usb_all(opt_usbdump);
  731. }
  732. usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
  733. // use constants so the stat generation is very quick
  734. // and the association between number and name can't
  735. // be missalined easily
  736. usb_commands[C_REJECTED] = C_REJECTED_S;
  737. usb_commands[C_PING] = C_PING_S;
  738. usb_commands[C_CLEAR] = C_CLEAR_S;
  739. usb_commands[C_REQUESTVERSION] = C_REQUESTVERSION_S;
  740. usb_commands[C_GETVERSION] = C_GETVERSION_S;
  741. usb_commands[C_REQUESTFPGACOUNT] = C_REQUESTFPGACOUNT_S;
  742. usb_commands[C_GETFPGACOUNT] = C_GETFPGACOUNT_S;
  743. usb_commands[C_STARTPROGRAM] = C_STARTPROGRAM_S;
  744. usb_commands[C_STARTPROGRAMSTATUS] = C_STARTPROGRAMSTATUS_S;
  745. usb_commands[C_PROGRAM] = C_PROGRAM_S;
  746. usb_commands[C_PROGRAMSTATUS] = C_PROGRAMSTATUS_S;
  747. usb_commands[C_PROGRAMSTATUS2] = C_PROGRAMSTATUS2_S;
  748. usb_commands[C_FINALPROGRAMSTATUS] = C_FINALPROGRAMSTATUS_S;
  749. usb_commands[C_SETCLOCK] = C_SETCLOCK_S;
  750. usb_commands[C_REPLYSETCLOCK] = C_REPLYSETCLOCK_S;
  751. usb_commands[C_REQUESTUSERCODE] = C_REQUESTUSERCODE_S;
  752. usb_commands[C_GETUSERCODE] = C_GETUSERCODE_S;
  753. usb_commands[C_REQUESTTEMPERATURE] = C_REQUESTTEMPERATURE_S;
  754. usb_commands[C_GETTEMPERATURE] = C_GETTEMPERATURE_S;
  755. usb_commands[C_SENDWORK] = C_SENDWORK_S;
  756. usb_commands[C_SENDWORKSTATUS] = C_SENDWORKSTATUS_S;
  757. usb_commands[C_REQUESTWORKSTATUS] = C_REQUESTWORKSTATUS_S;
  758. usb_commands[C_GETWORKSTATUS] = C_GETWORKSTATUS_S;
  759. usb_commands[C_REQUESTIDENTIFY] = C_REQUESTIDENTIFY_S;
  760. usb_commands[C_GETIDENTIFY] = C_GETIDENTIFY_S;
  761. usb_commands[C_REQUESTFLASH] = C_REQUESTFLASH_S;
  762. usb_commands[C_REQUESTSENDWORK] = C_REQUESTSENDWORK_S;
  763. usb_commands[C_REQUESTSENDWORKSTATUS] = C_REQUESTSENDWORKSTATUS_S;
  764. usb_commands[C_RESET] = C_RESET_S;
  765. usb_commands[C_SETBAUD] = C_SETBAUD_S;
  766. usb_commands[C_SETDATA] = C_SETDATA_S;
  767. usb_commands[C_SETFLOW] = C_SETFLOW_S;
  768. usb_commands[C_SETMODEM] = C_SETMODEM_S;
  769. usb_commands[C_PURGERX] = C_PURGERX_S;
  770. usb_commands[C_PURGETX] = C_PURGETX_S;
  771. usb_commands[C_FLASHREPLY] = C_FLASHREPLY_S;
  772. usb_commands[C_REQUESTDETAILS] = C_REQUESTDETAILS_S;
  773. usb_commands[C_GETDETAILS] = C_GETDETAILS_S;
  774. usb_commands[C_REQUESTRESULTS] = C_REQUESTRESULTS_S;
  775. usb_commands[C_GETRESULTS] = C_GETRESULTS_S;
  776. usb_commands[C_REQUESTQUEJOB] = C_REQUESTQUEJOB_S;
  777. usb_commands[C_REQUESTQUEJOBSTATUS] = C_REQUESTQUEJOBSTATUS_S;
  778. usb_commands[C_QUEJOB] = C_QUEJOB_S;
  779. usb_commands[C_QUEJOBSTATUS] = C_QUEJOBSTATUS_S;
  780. usb_commands[C_QUEFLUSH] = C_QUEFLUSH_S;
  781. usb_commands[C_QUEFLUSHREPLY] = C_QUEFLUSHREPLY_S;
  782. usb_commands[C_REQUESTVOLTS] = C_REQUESTVOLTS_S;
  783. usb_commands[C_SENDTESTWORK] = C_SENDTESTWORK_S;
  784. usb_commands[C_LATENCY] = C_LATENCY_S;
  785. stats_initialised = true;
  786. }
  787. mutex_unlock(&cgusb_lock);
  788. }
  789. const char *usb_cmdname(enum usb_cmds cmd)
  790. {
  791. cgusb_check_init();
  792. return usb_commands[cmd];
  793. }
  794. void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err)
  795. {
  796. if (msg && !*msg)
  797. msg = NULL;
  798. if (!msg && amount == 0 && err == LIBUSB_SUCCESS)
  799. msg = (char *)nodatareturned;
  800. applog(LOG_ERR, "%s%i: %s failed%s%s (err=%d amt=%d)",
  801. cgpu->drv->name, cgpu->device_id,
  802. usb_cmdname(cmd),
  803. msg ? space : BLANK, msg ? msg : BLANK,
  804. err, amount);
  805. }
  806. static bool __is_in_use(uint8_t bus_number, uint8_t device_address)
  807. {
  808. struct usb_in_use_list *in_use_tmp;
  809. bool ret = false;
  810. in_use_tmp = in_use_head;
  811. while (in_use_tmp) {
  812. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  813. in_use_tmp->in_use.device_address == (int)device_address) {
  814. ret = true;
  815. break;
  816. }
  817. in_use_tmp = in_use_tmp->next;
  818. }
  819. return ret;
  820. }
  821. static bool is_in_use_bd(uint8_t bus_number, uint8_t device_address)
  822. {
  823. bool ret;
  824. mutex_lock(&cgusb_lock);
  825. ret = __is_in_use(bus_number, device_address);
  826. mutex_unlock(&cgusb_lock);
  827. return ret;
  828. }
  829. static bool is_in_use(libusb_device *dev)
  830. {
  831. return is_in_use_bd(libusb_get_bus_number(dev), libusb_get_device_address(dev));
  832. }
  833. static void add_in_use(uint8_t bus_number, uint8_t device_address)
  834. {
  835. struct usb_in_use_list *in_use_tmp;
  836. bool found = false;
  837. mutex_lock(&cgusb_lock);
  838. if (unlikely(__is_in_use(bus_number, device_address))) {
  839. found = true;
  840. goto nofway;
  841. }
  842. in_use_tmp = calloc(1, sizeof(*in_use_tmp));
  843. in_use_tmp->in_use.bus_number = (int)bus_number;
  844. in_use_tmp->in_use.device_address = (int)device_address;
  845. in_use_tmp->next = in_use_head;
  846. if (in_use_head)
  847. in_use_head->prev = in_use_tmp;
  848. in_use_head = in_use_tmp;
  849. nofway:
  850. mutex_unlock(&cgusb_lock);
  851. if (found)
  852. applog(LOG_ERR, "FAIL: USB add already in use (%d:%d)",
  853. (int)bus_number, (int)device_address);
  854. }
  855. static void remove_in_use(uint8_t bus_number, uint8_t device_address)
  856. {
  857. struct usb_in_use_list *in_use_tmp;
  858. bool found = false;
  859. mutex_lock(&cgusb_lock);
  860. in_use_tmp = in_use_head;
  861. while (in_use_tmp) {
  862. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  863. in_use_tmp->in_use.device_address == (int)device_address) {
  864. found = true;
  865. if (in_use_tmp == in_use_head) {
  866. in_use_head = in_use_head->next;
  867. if (in_use_head)
  868. in_use_head->prev = NULL;
  869. } else {
  870. in_use_tmp->prev->next = in_use_tmp->next;
  871. if (in_use_tmp->next)
  872. in_use_tmp->next->prev = in_use_tmp->prev;
  873. }
  874. free(in_use_tmp);
  875. break;
  876. }
  877. in_use_tmp = in_use_tmp->next;
  878. }
  879. mutex_unlock(&cgusb_lock);
  880. if (!found)
  881. applog(LOG_ERR, "FAIL: USB remove not already in use (%d:%d)",
  882. (int)bus_number, (int)device_address);
  883. }
  884. #ifndef WIN32
  885. #include <errno.h>
  886. #include <unistd.h>
  887. #include <sys/types.h>
  888. #include <sys/ipc.h>
  889. #include <sys/sem.h>
  890. #include <sys/types.h>
  891. #include <sys/stat.h>
  892. #include <fcntl.h>
  893. union semun {
  894. int sem;
  895. struct semid_ds *seminfo;
  896. ushort *all;
  897. };
  898. #endif
  899. // Any errors should always be printed since they will rarely if ever occur
  900. // and thus it is best to always display them
  901. static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  902. {
  903. applog(LOG_DEBUG, "USB lock %s %d-%d", drv->name, (int)bus_number, (int)device_address);
  904. #ifdef WIN32
  905. struct cgpu_info *cgpu;
  906. HANDLE usbMutex;
  907. char name[64];
  908. DWORD res;
  909. int i;
  910. if (is_in_use_bd(bus_number, device_address))
  911. return false;
  912. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  913. usbMutex = CreateMutex(NULL, FALSE, name);
  914. if (usbMutex == NULL) {
  915. applog(LOG_ERR,
  916. "MTX: %s USB failed to get '%s' err (%d)",
  917. drv->dname, name, GetLastError());
  918. return false;
  919. }
  920. res = WaitForSingleObject(usbMutex, 0);
  921. switch(res) {
  922. case WAIT_OBJECT_0:
  923. case WAIT_ABANDONED:
  924. // Am I using it already?
  925. for (i = 0; i < total_devices; i++) {
  926. cgpu = get_devices(i);
  927. if (cgpu->usbinfo.bus_number == bus_number &&
  928. cgpu->usbinfo.device_address == device_address &&
  929. cgpu->usbinfo.nodev == false) {
  930. if (ReleaseMutex(usbMutex)) {
  931. applog(LOG_WARNING,
  932. "MTX: %s USB can't get '%s' - device in use",
  933. drv->dname, name);
  934. goto fail;
  935. }
  936. applog(LOG_ERR,
  937. "MTX: %s USB can't get '%s' - device in use - failure (%d)",
  938. drv->dname, name, GetLastError());
  939. goto fail;
  940. }
  941. }
  942. add_in_use(bus_number, device_address);
  943. return true;
  944. case WAIT_TIMEOUT:
  945. if (!hotplug_mode)
  946. applog(LOG_WARNING,
  947. "MTX: %s USB failed to get '%s' - device in use",
  948. drv->dname, name);
  949. goto fail;
  950. case WAIT_FAILED:
  951. applog(LOG_ERR,
  952. "MTX: %s USB failed to get '%s' err (%d)",
  953. drv->dname, name, GetLastError());
  954. goto fail;
  955. default:
  956. applog(LOG_ERR,
  957. "MTX: %s USB failed to get '%s' unknown reply (%d)",
  958. drv->dname, name, res);
  959. goto fail;
  960. }
  961. CloseHandle(usbMutex);
  962. add_in_use(bus_number, device_address);
  963. return true;
  964. fail:
  965. CloseHandle(usbMutex);
  966. return false;
  967. #else
  968. struct semid_ds seminfo;
  969. union semun opt;
  970. char name[64];
  971. key_t key;
  972. int fd, sem, count;
  973. if (is_in_use_bd(bus_number, device_address))
  974. return false;
  975. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  976. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  977. if (fd == -1) {
  978. applog(LOG_ERR,
  979. "SEM: %s USB open failed '%s' err (%d) %s",
  980. drv->dname, name, errno, strerror(errno));
  981. return false;
  982. }
  983. close(fd);
  984. key = ftok(name, 'K');
  985. sem = semget(key, 1, IPC_CREAT | IPC_EXCL | 438);
  986. if (sem < 0) {
  987. if (errno != EEXIST) {
  988. applog(LOG_ERR,
  989. "SEM: %s USB failed to get '%s' err (%d) %s",
  990. drv->dname, name, errno, strerror(errno));
  991. return false;
  992. }
  993. sem = semget(key, 1, 0);
  994. if (sem < 0) {
  995. applog(LOG_ERR,
  996. "SEM: %s USB failed to access '%s' err (%d) %s",
  997. drv->dname, name, errno, strerror(errno));
  998. return false;
  999. }
  1000. opt.seminfo = &seminfo;
  1001. count = 0;
  1002. while (++count) {
  1003. // Should NEVER take 100ms
  1004. if (count > 99) {
  1005. applog(LOG_ERR,
  1006. "SEM: %s USB timeout waiting for (%d) '%s'",
  1007. drv->dname, sem, name);
  1008. return false;
  1009. }
  1010. if (semctl(sem, 0, IPC_STAT, opt) == -1) {
  1011. applog(LOG_ERR,
  1012. "SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
  1013. drv->dname, sem, name, count, errno, strerror(errno));
  1014. return false;
  1015. }
  1016. if (opt.seminfo->sem_otime != 0)
  1017. break;
  1018. nmsleep(1);
  1019. }
  1020. }
  1021. struct sembuf sops[] = {
  1022. { 0, 0, IPC_NOWAIT | SEM_UNDO },
  1023. { 0, 1, IPC_NOWAIT | SEM_UNDO }
  1024. };
  1025. if (semop(sem, sops, 2)) {
  1026. if (errno == EAGAIN) {
  1027. if (!hotplug_mode)
  1028. applog(LOG_WARNING,
  1029. "SEM: %s USB failed to get (%d) '%s' - device in use",
  1030. drv->dname, sem, name);
  1031. } else {
  1032. applog(LOG_DEBUG,
  1033. "SEM: %s USB failed to get (%d) '%s' err (%d) %s",
  1034. drv->dname, sem, name, errno, strerror(errno));
  1035. }
  1036. return false;
  1037. }
  1038. add_in_use(bus_number, device_address);
  1039. return true;
  1040. #endif
  1041. }
  1042. static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
  1043. {
  1044. return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  1045. }
  1046. // Any errors should always be printed since they will rarely if ever occur
  1047. // and thus it is best to always display them
  1048. static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  1049. {
  1050. applog(LOG_DEBUG, "USB unlock %s %d-%d", drv->name, (int)bus_number, (int)device_address);
  1051. #ifdef WIN32
  1052. HANDLE usbMutex;
  1053. char name[64];
  1054. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  1055. usbMutex = CreateMutex(NULL, FALSE, name);
  1056. if (usbMutex == NULL) {
  1057. applog(LOG_ERR,
  1058. "MTX: %s USB failed to get '%s' for release err (%d)",
  1059. drv->dname, name, GetLastError());
  1060. return;
  1061. }
  1062. if (!ReleaseMutex(usbMutex))
  1063. applog(LOG_ERR,
  1064. "MTX: %s USB failed to release '%s' err (%d)",
  1065. drv->dname, name, GetLastError());
  1066. CloseHandle(usbMutex);
  1067. remove_in_use(bus_number, device_address);
  1068. return;
  1069. #else
  1070. char name[64];
  1071. key_t key;
  1072. int fd, sem;
  1073. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  1074. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  1075. if (fd == -1) {
  1076. applog(LOG_ERR,
  1077. "SEM: %s USB open failed '%s' for release err (%d) %s",
  1078. drv->dname, name, errno, strerror(errno));
  1079. return;
  1080. }
  1081. close(fd);
  1082. key = ftok(name, 'K');
  1083. sem = semget(key, 1, 0);
  1084. if (sem < 0) {
  1085. applog(LOG_ERR,
  1086. "SEM: %s USB failed to get '%s' for release err (%d) %s",
  1087. drv->dname, name, errno, strerror(errno));
  1088. return;
  1089. }
  1090. struct sembuf sops[] = {
  1091. { 0, -1, SEM_UNDO }
  1092. };
  1093. // Allow a 10ms timeout
  1094. // exceeding this timeout means it would probably never succeed anyway
  1095. struct timespec timeout = { 0, 10000000 };
  1096. // Wait forever since we shoud be the one who has it
  1097. if (semtimedop(sem, sops, 1, &timeout)) {
  1098. applog(LOG_ERR,
  1099. "SEM: %s USB failed to release '%s' err (%d) %s",
  1100. drv->dname, name, errno, strerror(errno));
  1101. }
  1102. remove_in_use(bus_number, device_address);
  1103. return;
  1104. #endif
  1105. }
  1106. static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
  1107. {
  1108. cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  1109. }
  1110. static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
  1111. {
  1112. applog(LOG_DEBUG, "USB free %s", cgusb->found->name);
  1113. if (cgusb->serial_string && cgusb->serial_string != BLANK)
  1114. free(cgusb->serial_string);
  1115. if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
  1116. free(cgusb->manuf_string);
  1117. if (cgusb->prod_string && cgusb->prod_string != BLANK)
  1118. free(cgusb->prod_string);
  1119. free(cgusb->descriptor);
  1120. free(cgusb->found);
  1121. free(cgusb);
  1122. return NULL;
  1123. }
  1124. void usb_uninit(struct cgpu_info *cgpu)
  1125. {
  1126. applog(LOG_DEBUG, "USB uninit %s%i",
  1127. cgpu->drv->name, cgpu->device_id);
  1128. // May have happened already during a failed initialisation
  1129. // if release_cgpu() was called due to a USB NODEV(err)
  1130. if (!cgpu->usbdev)
  1131. return;
  1132. libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
  1133. libusb_close(cgpu->usbdev->handle);
  1134. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  1135. }
  1136. static void release_cgpu(struct cgpu_info *cgpu)
  1137. {
  1138. struct cg_usb_device *cgusb = cgpu->usbdev;
  1139. struct cgpu_info *lookcgpu;
  1140. int i;
  1141. applog(LOG_DEBUG, "USB release %s%i",
  1142. cgpu->drv->name, cgpu->device_id);
  1143. // It has already been done
  1144. if (cgpu->usbinfo.nodev)
  1145. return;
  1146. total_count--;
  1147. drv_count[cgpu->drv->drv_id].count--;
  1148. cgpu->usbinfo.nodev = true;
  1149. cgpu->usbinfo.nodev_count++;
  1150. cgtime(&cgpu->usbinfo.last_nodev);
  1151. // Any devices sharing the same USB device should be marked also
  1152. // Currently only MMQ shares a USB device
  1153. for (i = 0; i < total_devices; i++) {
  1154. lookcgpu = get_devices(i);
  1155. if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
  1156. total_count--;
  1157. drv_count[lookcgpu->drv->drv_id].count--;
  1158. lookcgpu->usbinfo.nodev = true;
  1159. lookcgpu->usbinfo.nodev_count++;
  1160. memcpy(&(lookcgpu->usbinfo.last_nodev),
  1161. &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
  1162. lookcgpu->usbdev = NULL;
  1163. }
  1164. }
  1165. usb_uninit(cgpu);
  1166. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  1167. }
  1168. #define USB_INIT_FAIL 0
  1169. #define USB_INIT_OK 1
  1170. #define USB_INIT_IGNORE 2
  1171. static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  1172. {
  1173. struct cg_usb_device *cgusb = NULL;
  1174. struct libusb_config_descriptor *config = NULL;
  1175. const struct libusb_interface_descriptor *idesc;
  1176. const struct libusb_endpoint_descriptor *epdesc;
  1177. unsigned char strbuf[STRBUFLEN+1];
  1178. char devstr[STRBUFLEN+1];
  1179. int err, i, j, k;
  1180. int bad = USB_INIT_FAIL;
  1181. cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
  1182. cgpu->usbinfo.device_address = libusb_get_device_address(dev);
  1183. sprintf(devstr, "- %s device %d:%d", found->name,
  1184. cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  1185. cgusb = calloc(1, sizeof(*cgusb));
  1186. cgusb->found = found;
  1187. if (found->idVendor == IDVENDOR_FTDI)
  1188. cgusb->usb_type = USB_TYPE_FTDI;
  1189. cgusb->ident = found->ident;
  1190. cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
  1191. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  1192. if (err) {
  1193. applog(LOG_DEBUG,
  1194. "USB init failed to get descriptor, err %d %s",
  1195. err, devstr);
  1196. goto dame;
  1197. }
  1198. err = libusb_open(dev, &(cgusb->handle));
  1199. if (err) {
  1200. switch (err) {
  1201. case LIBUSB_ERROR_ACCESS:
  1202. applog(LOG_ERR,
  1203. "USB init open device failed, err %d, "
  1204. "you dont have priviledge to access %s",
  1205. err, devstr);
  1206. break;
  1207. #ifdef WIN32
  1208. // Windows specific message
  1209. case LIBUSB_ERROR_NOT_SUPPORTED:
  1210. applog(LOG_ERR,
  1211. "USB init, open device failed, err %d, "
  1212. "you need to install a Windows USB driver for %s",
  1213. err, devstr);
  1214. break;
  1215. #endif
  1216. default:
  1217. applog(LOG_DEBUG,
  1218. "USB init, open failed, err %d %s",
  1219. err, devstr);
  1220. }
  1221. goto dame;
  1222. }
  1223. #ifndef WIN32
  1224. if (libusb_kernel_driver_active(cgusb->handle, found->kernel) == 1) {
  1225. applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
  1226. err = libusb_detach_kernel_driver(cgusb->handle, found->kernel);
  1227. if (err == 0) {
  1228. applog(LOG_DEBUG,
  1229. "USB init, kernel detached successfully %s",
  1230. devstr);
  1231. } else {
  1232. applog(LOG_WARNING,
  1233. "USB init, kernel detach failed, err %d in use? %s",
  1234. err, devstr);
  1235. goto cldame;
  1236. }
  1237. }
  1238. #endif
  1239. if (found->iManufacturer) {
  1240. unsigned char man[STRBUFLEN+1];
  1241. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1242. cgusb->descriptor->iManufacturer,
  1243. man, STRBUFLEN);
  1244. if (err < 0) {
  1245. applog(LOG_DEBUG,
  1246. "USB init, failed to get iManufacturer, err %d %s",
  1247. err, devstr);
  1248. goto cldame;
  1249. }
  1250. if (strcmp((char *)man, found->iManufacturer)) {
  1251. applog(LOG_DEBUG, "USB init, iManufacturer mismatch %s",
  1252. devstr);
  1253. bad = USB_INIT_IGNORE;
  1254. goto cldame;
  1255. }
  1256. }
  1257. if (found->iProduct) {
  1258. unsigned char prod[STRBUFLEN+1];
  1259. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1260. cgusb->descriptor->iProduct,
  1261. prod, STRBUFLEN);
  1262. if (err < 0) {
  1263. applog(LOG_DEBUG,
  1264. "USB init, failed to get iProduct, err %d %s",
  1265. err, devstr);
  1266. goto cldame;
  1267. }
  1268. if (strcmp((char *)prod, found->iProduct)) {
  1269. applog(LOG_DEBUG, "USB init, iProduct mismatch %s",
  1270. devstr);
  1271. bad = USB_INIT_IGNORE;
  1272. goto cldame;
  1273. }
  1274. }
  1275. err = libusb_set_configuration(cgusb->handle, found->config);
  1276. if (err) {
  1277. switch(err) {
  1278. case LIBUSB_ERROR_BUSY:
  1279. applog(LOG_WARNING,
  1280. "USB init, set config %d in use %s",
  1281. found->config, devstr);
  1282. break;
  1283. default:
  1284. applog(LOG_DEBUG,
  1285. "USB init, failed to set config to %d, err %d %s",
  1286. found->config, err, devstr);
  1287. }
  1288. goto cldame;
  1289. }
  1290. err = libusb_get_active_config_descriptor(dev, &config);
  1291. if (err) {
  1292. applog(LOG_DEBUG,
  1293. "USB init, failed to get config descriptor, err %d %s",
  1294. err, devstr);
  1295. goto cldame;
  1296. }
  1297. if ((int)(config->bNumInterfaces) <= found->interface) {
  1298. applog(LOG_DEBUG, "USB init bNumInterfaces <= interface %s",
  1299. devstr);
  1300. goto cldame;
  1301. }
  1302. for (i = 0; i < found->epcount; i++)
  1303. found->eps[i].found = false;
  1304. for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
  1305. idesc = &(config->interface[found->interface].altsetting[i]);
  1306. for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
  1307. epdesc = &(idesc->endpoint[j]);
  1308. for (k = 0; k < found->epcount; k++) {
  1309. if (!found->eps[k].found) {
  1310. if (epdesc->bmAttributes == found->eps[k].att
  1311. && epdesc->wMaxPacketSize >= found->eps[k].size
  1312. && epdesc->bEndpointAddress == found->eps[k].ep) {
  1313. found->eps[k].found = true;
  1314. break;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. }
  1320. for (i = 0; i < found->epcount; i++) {
  1321. if (found->eps[i].found == false) {
  1322. applog(LOG_DEBUG, "USB init found == false %s",
  1323. devstr);
  1324. goto cldame;
  1325. }
  1326. }
  1327. err = libusb_claim_interface(cgusb->handle, found->interface);
  1328. if (err) {
  1329. switch(err) {
  1330. case LIBUSB_ERROR_BUSY:
  1331. applog(LOG_WARNING,
  1332. "USB init, claim interface %d in use %s",
  1333. found->interface, devstr);
  1334. break;
  1335. default:
  1336. applog(LOG_DEBUG,
  1337. "USB init, claim interface %d failed, err %d %s",
  1338. found->interface, err, devstr);
  1339. }
  1340. goto cldame;
  1341. }
  1342. cgusb->usbver = cgusb->descriptor->bcdUSB;
  1343. // TODO: allow this with the right version of the libusb include and running library
  1344. // cgusb->speed = libusb_get_device_speed(dev);
  1345. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1346. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  1347. if (err > 0)
  1348. cgusb->prod_string = strdup((char *)strbuf);
  1349. else
  1350. cgusb->prod_string = (char *)BLANK;
  1351. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1352. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  1353. if (err > 0)
  1354. cgusb->manuf_string = strdup((char *)strbuf);
  1355. else
  1356. cgusb->manuf_string = (char *)BLANK;
  1357. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1358. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  1359. if (err > 0)
  1360. cgusb->serial_string = strdup((char *)strbuf);
  1361. else
  1362. cgusb->serial_string = (char *)BLANK;
  1363. // TODO: ?
  1364. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  1365. // cgusb->interfaceVersion
  1366. applog(LOG_DEBUG,
  1367. "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
  1368. devstr, cgusb->usbver, cgusb->prod_string,
  1369. cgusb->manuf_string, cgusb->serial_string);
  1370. cgpu->usbdev = cgusb;
  1371. libusb_free_config_descriptor(config);
  1372. // Allow a name change based on the idVendor+idProduct
  1373. // N.B. must be done before calling add_cgpu()
  1374. if (strcmp(cgpu->drv->name, found->name)) {
  1375. if (!cgpu->drv->copy)
  1376. cgpu->drv = copy_drv(cgpu->drv);
  1377. cgpu->drv->name = (char *)(found->name);
  1378. }
  1379. return USB_INIT_OK;
  1380. cldame:
  1381. libusb_close(cgusb->handle);
  1382. dame:
  1383. if (config)
  1384. libusb_free_config_descriptor(config);
  1385. cgusb = free_cgusb(cgusb);
  1386. return bad;
  1387. }
  1388. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  1389. {
  1390. int ret;
  1391. ret = _usb_init(cgpu, dev, found);
  1392. if (ret == USB_INIT_FAIL)
  1393. applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
  1394. cgpu->drv->dname,
  1395. (int)(cgpu->usbinfo.bus_number),
  1396. (int)(cgpu->usbinfo.device_address));
  1397. return (ret == USB_INIT_OK);
  1398. }
  1399. static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
  1400. {
  1401. struct libusb_device_descriptor desc;
  1402. int bus_number, device_address;
  1403. int err, i;
  1404. bool ok;
  1405. err = libusb_get_device_descriptor(dev, &desc);
  1406. if (err) {
  1407. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  1408. return false;
  1409. }
  1410. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  1411. applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
  1412. drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  1413. return false;
  1414. }
  1415. if (busdev_count > 0) {
  1416. bus_number = (int)libusb_get_bus_number(dev);
  1417. device_address = (int)libusb_get_device_address(dev);
  1418. ok = false;
  1419. for (i = 0; i < busdev_count; i++) {
  1420. if (bus_number == busdev[i].bus_number) {
  1421. if (busdev[i].device_address == -1 ||
  1422. device_address == busdev[i].device_address) {
  1423. ok = true;
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. if (!ok) {
  1429. applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
  1430. drv->name, look->name, look->idVendor, look->idProduct,
  1431. bus_number, device_address);
  1432. return false;
  1433. }
  1434. }
  1435. applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
  1436. drv->name, look->name, look->idVendor, look->idProduct);
  1437. return true;
  1438. }
  1439. static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
  1440. {
  1441. struct usb_find_devices *found;
  1442. int i;
  1443. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  1444. if (find_dev[i].drv == drvnum) {
  1445. if (usb_check_device(drv, dev, &(find_dev[i]))) {
  1446. found = malloc(sizeof(*found));
  1447. memcpy(found, &(find_dev[i]), sizeof(*found));
  1448. return found;
  1449. }
  1450. }
  1451. return NULL;
  1452. }
  1453. static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
  1454. {
  1455. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1456. applog(LOG_DEBUG,
  1457. "USB scan devices3: %s limit %d reached",
  1458. drv->dname, drv_count[drv->drv_id].limit);
  1459. return NULL;
  1460. }
  1461. #ifdef USE_BFLSC
  1462. if (drv->drv_id == DRIVER_BFLSC)
  1463. return usb_check_each(DRV_BFLSC, drv, dev);
  1464. #endif
  1465. #ifdef USE_BITFORCE
  1466. if (drv->drv_id == DRIVER_BITFORCE)
  1467. return usb_check_each(DRV_BITFORCE, drv, dev);
  1468. #endif
  1469. #ifdef USE_MODMINER
  1470. if (drv->drv_id == DRIVER_MODMINER)
  1471. return usb_check_each(DRV_MODMINER, drv, dev);
  1472. #endif
  1473. #ifdef USE_ICARUS
  1474. if (drv->drv_id == DRIVER_ICARUS)
  1475. return usb_check_each(DRV_ICARUS, drv, dev);
  1476. #endif
  1477. #ifdef USE_AVALON
  1478. if (drv->drv_id == DRIVER_AVALON)
  1479. return usb_check_each(DRV_AVALON, drv, dev);
  1480. #endif
  1481. return NULL;
  1482. }
  1483. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
  1484. {
  1485. libusb_device **list;
  1486. ssize_t count, i;
  1487. struct usb_find_devices *found;
  1488. applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
  1489. if (total_count >= total_limit) {
  1490. applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
  1491. return;
  1492. }
  1493. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1494. applog(LOG_DEBUG,
  1495. "USB scan devices: %s limit %d reached",
  1496. drv->dname, drv_count[drv->drv_id].limit);
  1497. return;
  1498. }
  1499. count = libusb_get_device_list(NULL, &list);
  1500. if (count < 0) {
  1501. applog(LOG_DEBUG, "USB scan devices: failed, err %zd", count);
  1502. return;
  1503. }
  1504. if (count == 0)
  1505. applog(LOG_DEBUG, "USB scan devices: found no devices");
  1506. for (i = 0; i < count; i++) {
  1507. if (total_count >= total_limit) {
  1508. applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
  1509. break;
  1510. }
  1511. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1512. applog(LOG_DEBUG,
  1513. "USB scan devices2: %s limit %d reached",
  1514. drv->dname, drv_count[drv->drv_id].limit);
  1515. break;
  1516. }
  1517. found = usb_check(drv, list[i]);
  1518. if (found != NULL) {
  1519. if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
  1520. free(found);
  1521. else {
  1522. if (!device_detect(list[i], found))
  1523. cgminer_usb_unlock(drv, list[i]);
  1524. else {
  1525. total_count++;
  1526. drv_count[drv->drv_id].count++;
  1527. }
  1528. }
  1529. }
  1530. }
  1531. libusb_free_device_list(list, 1);
  1532. }
  1533. // Set this to 0 to remove stats processing
  1534. #define DO_USB_STATS 1
  1535. #if DO_USB_STATS
  1536. #define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
  1537. #define STATS_TIMEVAL(tv) cgtime(tv)
  1538. #else
  1539. #define USB_STATS(sgpu, sta, fin, err, cmd, seq)
  1540. #define STATS_TIMEVAL(tv)
  1541. #endif
  1542. // The stat data can be spurious due to not locking it before copying it -
  1543. // however that would require the stat() function to also lock and release
  1544. // a mutex every time a usb read or write is called which would slow
  1545. // things down more
  1546. struct api_data *api_usb_stats(__maybe_unused int *count)
  1547. {
  1548. #if DO_USB_STATS
  1549. struct cg_usb_stats_details *details;
  1550. struct cg_usb_stats *sta;
  1551. struct api_data *root = NULL;
  1552. int device;
  1553. int cmdseq;
  1554. if (next_stat == 0)
  1555. return NULL;
  1556. while (*count < next_stat * C_MAX * 2) {
  1557. device = *count / (C_MAX * 2);
  1558. cmdseq = *count % (C_MAX * 2);
  1559. (*count)++;
  1560. sta = &(usb_stats[device]);
  1561. details = &(sta->details[cmdseq]);
  1562. // Only show stats that have results
  1563. if (details->item[CMD_CMD].count == 0 &&
  1564. details->item[CMD_TIMEOUT].count == 0 &&
  1565. details->item[CMD_ERROR].count == 0)
  1566. continue;
  1567. root = api_add_string(root, "Name", sta->name, false);
  1568. root = api_add_int(root, "ID", &(sta->device_id), false);
  1569. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  1570. root = api_add_int(root, "Seq", &(details->seq), true);
  1571. root = api_add_uint64(root, "Count",
  1572. &(details->item[CMD_CMD].count), true);
  1573. root = api_add_double(root, "Total Delay",
  1574. &(details->item[CMD_CMD].total_delay), true);
  1575. root = api_add_double(root, "Min Delay",
  1576. &(details->item[CMD_CMD].min_delay), true);
  1577. root = api_add_double(root, "Max Delay",
  1578. &(details->item[CMD_CMD].max_delay), true);
  1579. root = api_add_uint64(root, "Timeout Count",
  1580. &(details->item[CMD_TIMEOUT].count), true);
  1581. root = api_add_double(root, "Timeout Total Delay",
  1582. &(details->item[CMD_TIMEOUT].total_delay), true);
  1583. root = api_add_double(root, "Timeout Min Delay",
  1584. &(details->item[CMD_TIMEOUT].min_delay), true);
  1585. root = api_add_double(root, "Timeout Max Delay",
  1586. &(details->item[CMD_TIMEOUT].max_delay), true);
  1587. root = api_add_uint64(root, "Error Count",
  1588. &(details->item[CMD_ERROR].count), true);
  1589. root = api_add_double(root, "Error Total Delay",
  1590. &(details->item[CMD_ERROR].total_delay), true);
  1591. root = api_add_double(root, "Error Min Delay",
  1592. &(details->item[CMD_ERROR].min_delay), true);
  1593. root = api_add_double(root, "Error Max Delay",
  1594. &(details->item[CMD_ERROR].max_delay), true);
  1595. root = api_add_timeval(root, "First Command",
  1596. &(details->item[CMD_CMD].first), true);
  1597. root = api_add_timeval(root, "Last Command",
  1598. &(details->item[CMD_CMD].last), true);
  1599. root = api_add_timeval(root, "First Timeout",
  1600. &(details->item[CMD_TIMEOUT].first), true);
  1601. root = api_add_timeval(root, "Last Timeout",
  1602. &(details->item[CMD_TIMEOUT].last), true);
  1603. root = api_add_timeval(root, "First Error",
  1604. &(details->item[CMD_ERROR].first), true);
  1605. root = api_add_timeval(root, "Last Error",
  1606. &(details->item[CMD_ERROR].last), true);
  1607. return root;
  1608. }
  1609. #endif
  1610. return NULL;
  1611. }
  1612. #if DO_USB_STATS
  1613. static void newstats(struct cgpu_info *cgpu)
  1614. {
  1615. int i;
  1616. mutex_lock(&cgusb_lock);
  1617. cgpu->usbinfo.usbstat = ++next_stat;
  1618. mutex_unlock(&cgusb_lock);
  1619. usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
  1620. usb_stats[next_stat-1].name = cgpu->drv->name;
  1621. usb_stats[next_stat-1].device_id = -1;
  1622. usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
  1623. for (i = 1; i < C_MAX * 2; i += 2)
  1624. usb_stats[next_stat-1].details[i].seq = 1;
  1625. }
  1626. #endif
  1627. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  1628. {
  1629. #if DO_USB_STATS
  1630. if (cgpu->usbinfo.usbstat < 1)
  1631. newstats(cgpu);
  1632. // we don't know the device_id until after add_cgpu()
  1633. usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
  1634. #endif
  1635. }
  1636. #if DO_USB_STATS
  1637. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
  1638. {
  1639. struct cg_usb_stats_details *details;
  1640. double diff;
  1641. int item;
  1642. if (cgpu->usbinfo.usbstat < 1)
  1643. newstats(cgpu);
  1644. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
  1645. diff = tdiff(tv_finish, tv_start);
  1646. switch (err) {
  1647. case LIBUSB_SUCCESS:
  1648. item = CMD_CMD;
  1649. break;
  1650. case LIBUSB_ERROR_TIMEOUT:
  1651. item = CMD_TIMEOUT;
  1652. break;
  1653. default:
  1654. item = CMD_ERROR;
  1655. break;
  1656. }
  1657. if (details->item[item].count == 0) {
  1658. details->item[item].min_delay = diff;
  1659. memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  1660. } else if (diff < details->item[item].min_delay)
  1661. details->item[item].min_delay = diff;
  1662. if (diff > details->item[item].max_delay)
  1663. details->item[item].max_delay = diff;
  1664. details->item[item].total_delay += diff;
  1665. memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
  1666. details->item[item].count++;
  1667. }
  1668. static void rejected_inc(struct cgpu_info *cgpu)
  1669. {
  1670. struct cg_usb_stats_details *details;
  1671. int item = CMD_ERROR;
  1672. if (cgpu->usbinfo.usbstat < 1)
  1673. newstats(cgpu);
  1674. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
  1675. details->item[item].count++;
  1676. }
  1677. #endif
  1678. #define USB_MAX_READ 8192
  1679. int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
  1680. {
  1681. struct cg_usb_device *usbdev = cgpu->usbdev;
  1682. bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
  1683. #if DO_USB_STATS
  1684. struct timeval tv_start;
  1685. #endif
  1686. struct timeval read_start, tv_finish;
  1687. unsigned int initial_timeout;
  1688. double max, done;
  1689. int bufleft, err, got, tot;
  1690. bool first = true;
  1691. unsigned char *search;
  1692. int endlen;
  1693. // We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
  1694. unsigned char usbbuf[USB_MAX_READ+4], *ptr;
  1695. size_t usbbufread;
  1696. USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
  1697. if (bufsiz > USB_MAX_READ)
  1698. quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, bufsiz, USB_MAX_READ);
  1699. if (cgpu->usbinfo.nodev) {
  1700. *buf = '\0';
  1701. *processed = 0;
  1702. #if DO_USB_STATS
  1703. rejected_inc(cgpu);
  1704. #endif
  1705. return LIBUSB_ERROR_NO_DEVICE;
  1706. }
  1707. if (timeout == DEVTIMEOUT)
  1708. timeout = usbdev->found->timeout;
  1709. if (end == NULL) {
  1710. tot = 0;
  1711. ptr = usbbuf;
  1712. bufleft = bufsiz;
  1713. err = LIBUSB_SUCCESS;
  1714. initial_timeout = timeout;
  1715. max = ((double)timeout) / 1000.0;
  1716. cgtime(&read_start);
  1717. while (bufleft > 0) {
  1718. if (ftdi)
  1719. usbbufread = bufleft + 2;
  1720. else
  1721. usbbufread = bufleft;
  1722. got = 0;
  1723. STATS_TIMEVAL(&tv_start);
  1724. err = libusb_bulk_transfer(usbdev->handle,
  1725. usbdev->found->eps[ep].ep,
  1726. ptr, usbbufread, &got, timeout);
  1727. STATS_TIMEVAL(&tv_finish);
  1728. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1729. ptr[got] = '\0';
  1730. USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
  1731. if (ftdi) {
  1732. // first 2 bytes returned are an FTDI status
  1733. if (got > 2) {
  1734. got -= 2;
  1735. memmove(ptr, ptr+2, got+1);
  1736. } else {
  1737. got = 0;
  1738. *ptr = '\0';
  1739. }
  1740. }
  1741. tot += got;
  1742. if (err || readonce)
  1743. break;
  1744. ptr += got;
  1745. bufleft -= got;
  1746. first = false;
  1747. done = tdiff(&tv_finish, &read_start);
  1748. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1749. if (unlikely(done >= max))
  1750. break;
  1751. timeout = initial_timeout - (done * 1000);
  1752. }
  1753. *processed = tot;
  1754. memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
  1755. if (NODEV(err))
  1756. release_cgpu(cgpu);
  1757. return err;
  1758. }
  1759. tot = 0;
  1760. ptr = usbbuf;
  1761. bufleft = bufsiz;
  1762. endlen = strlen(end);
  1763. err = LIBUSB_SUCCESS;
  1764. initial_timeout = timeout;
  1765. max = ((double)timeout) / 1000.0;
  1766. cgtime(&read_start);
  1767. while (bufleft > 0) {
  1768. if (ftdi)
  1769. usbbufread = bufleft + 2;
  1770. else
  1771. usbbufread = bufleft;
  1772. got = 0;
  1773. STATS_TIMEVAL(&tv_start);
  1774. err = libusb_bulk_transfer(usbdev->handle,
  1775. usbdev->found->eps[ep].ep,
  1776. ptr, usbbufread, &got, timeout);
  1777. cgtime(&tv_finish);
  1778. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1779. ptr[got] = '\0';
  1780. USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
  1781. if (ftdi) {
  1782. // first 2 bytes returned are an FTDI status
  1783. if (got > 2) {
  1784. got -= 2;
  1785. memmove(ptr, ptr+2, got+1);
  1786. } else {
  1787. got = 0;
  1788. *ptr = '\0';
  1789. }
  1790. }
  1791. tot += got;
  1792. if (err || readonce)
  1793. break;
  1794. // WARNING - this will return data past END ('if' there is extra data)
  1795. if (endlen <= tot) {
  1796. // If END is only 1 char - do a faster search
  1797. if (endlen == 1) {
  1798. if (strchr((char *)ptr, *end))
  1799. break;
  1800. } else {
  1801. // must allow END to have been chopped in 2 transfers
  1802. if ((tot - got) >= (endlen - 1))
  1803. search = ptr - (endlen - 1);
  1804. else
  1805. search = ptr - (tot - got);
  1806. if (strstr((char *)search, end))
  1807. break;
  1808. }
  1809. }
  1810. ptr += got;
  1811. bufleft -= got;
  1812. first = false;
  1813. done = tdiff(&tv_finish, &read_start);
  1814. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1815. if (unlikely(done >= max))
  1816. break;
  1817. timeout = initial_timeout - (done * 1000);
  1818. }
  1819. *processed = tot;
  1820. memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
  1821. if (NODEV(err))
  1822. release_cgpu(cgpu);
  1823. return err;
  1824. }
  1825. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
  1826. {
  1827. struct cg_usb_device *usbdev = cgpu->usbdev;
  1828. #if DO_USB_STATS
  1829. struct timeval tv_start, tv_finish;
  1830. #endif
  1831. int err, sent;
  1832. USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd));
  1833. if (cgpu->usbinfo.nodev) {
  1834. *processed = 0;
  1835. #if DO_USB_STATS
  1836. rejected_inc(cgpu);
  1837. #endif
  1838. return LIBUSB_ERROR_NO_DEVICE;
  1839. }
  1840. sent = 0;
  1841. STATS_TIMEVAL(&tv_start);
  1842. err = libusb_bulk_transfer(usbdev->handle,
  1843. usbdev->found->eps[ep].ep,
  1844. (unsigned char *)buf,
  1845. bufsiz, &sent,
  1846. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1847. STATS_TIMEVAL(&tv_finish);
  1848. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1849. USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
  1850. *processed = sent;
  1851. if (NODEV(err))
  1852. release_cgpu(cgpu);
  1853. return err;
  1854. }
  1855. int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd)
  1856. {
  1857. struct cg_usb_device *usbdev = cgpu->usbdev;
  1858. #if DO_USB_STATS
  1859. struct timeval tv_start, tv_finish;
  1860. #endif
  1861. int err;
  1862. USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, timeout, usb_cmdname(cmd));
  1863. if (cgpu->usbinfo.nodev) {
  1864. #if DO_USB_STATS
  1865. rejected_inc(cgpu);
  1866. #endif
  1867. return LIBUSB_ERROR_NO_DEVICE;
  1868. }
  1869. STATS_TIMEVAL(&tv_start);
  1870. err = libusb_control_transfer(usbdev->handle, request_type,
  1871. bRequest, htole16(wValue), htole16(wIndex), NULL, 0,
  1872. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1873. STATS_TIMEVAL(&tv_finish);
  1874. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1875. USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
  1876. if (NODEV(err))
  1877. release_cgpu(cgpu);
  1878. return err;
  1879. }
  1880. void usb_cleanup()
  1881. {
  1882. struct cgpu_info *cgpu;
  1883. int i;
  1884. hotplug_time = 0;
  1885. nmsleep(10);
  1886. for (i = 0; i < total_devices; i++) {
  1887. cgpu = devices[i];
  1888. switch (cgpu->drv->drv_id) {
  1889. case DRIVER_BFLSC:
  1890. case DRIVER_BITFORCE:
  1891. case DRIVER_MODMINER:
  1892. case DRIVER_ICARUS:
  1893. case DRIVER_AVALON:
  1894. release_cgpu(cgpu);
  1895. break;
  1896. default:
  1897. break;
  1898. }
  1899. }
  1900. }
  1901. void usb_initialise()
  1902. {
  1903. char *fre, *ptr, *comma, *colon;
  1904. int bus, dev, lim, i;
  1905. bool found;
  1906. for (i = 0; i < DRIVER_MAX; i++) {
  1907. drv_count[i].count = 0;
  1908. drv_count[i].limit = 999999;
  1909. }
  1910. cgusb_check_init();
  1911. if (opt_usb_select && *opt_usb_select) {
  1912. // Absolute device limit
  1913. if (*opt_usb_select == ':') {
  1914. total_limit = atoi(opt_usb_select+1);
  1915. if (total_limit < 0)
  1916. quit(1, "Invalid --usb total limit");
  1917. // Comma list of bus:dev devices to match
  1918. } else if (isdigit(*opt_usb_select)) {
  1919. fre = ptr = strdup(opt_usb_select);
  1920. do {
  1921. comma = strchr(ptr, ',');
  1922. if (comma)
  1923. *(comma++) = '\0';
  1924. colon = strchr(ptr, ':');
  1925. if (!colon)
  1926. quit(1, "Invalid --usb bus:dev missing ':'");
  1927. *(colon++) = '\0';
  1928. if (!isdigit(*ptr))
  1929. quit(1, "Invalid --usb bus:dev - bus must be a number");
  1930. if (!isdigit(*colon) && *colon != '*')
  1931. quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
  1932. bus = atoi(ptr);
  1933. if (bus <= 0)
  1934. quit(1, "Invalid --usb bus:dev - bus must be > 0");
  1935. if (!colon == '*')
  1936. dev = -1;
  1937. else {
  1938. dev = atoi(colon);
  1939. if (dev <= 0)
  1940. quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
  1941. }
  1942. busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
  1943. busdev[busdev_count-1].bus_number = bus;
  1944. busdev[busdev_count-1].device_address = dev;
  1945. ptr = comma;
  1946. } while (ptr);
  1947. free(fre);
  1948. // Comma list of DRV:limit
  1949. } else {
  1950. fre = ptr = strdup(opt_usb_select);
  1951. do {
  1952. comma = strchr(ptr, ',');
  1953. if (comma)
  1954. *(comma++) = '\0';
  1955. colon = strchr(ptr, ':');
  1956. if (!colon)
  1957. quit(1, "Invalid --usb DRV:limit missing ':'");
  1958. *(colon++) = '\0';
  1959. if (!isdigit(*colon))
  1960. quit(1, "Invalid --usb DRV:limit - limit must be a number");
  1961. lim = atoi(colon);
  1962. if (lim < 0)
  1963. quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
  1964. found = false;
  1965. #ifdef USE_BFLSC
  1966. if (strcasecmp(ptr, bflsc_drv.name) == 0) {
  1967. drv_count[bflsc_drv.drv_id].limit = lim;
  1968. found = true;
  1969. }
  1970. #endif
  1971. #ifdef USE_BITFORCE
  1972. if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
  1973. drv_count[bitforce_drv.drv_id].limit = lim;
  1974. found = true;
  1975. }
  1976. #endif
  1977. #ifdef USE_MODMINER
  1978. if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
  1979. drv_count[modminer_drv.drv_id].limit = lim;
  1980. found = true;
  1981. }
  1982. #endif
  1983. #ifdef USE_ICARUS
  1984. if (!found && strcasecmp(ptr, icarus_drv.name) == 0) {
  1985. drv_count[icarus_drv.drv_id].limit = lim;
  1986. found = true;
  1987. }
  1988. #endif
  1989. #ifdef USE_AVALON
  1990. if (!found && strcasecmp(ptr, avalon_drv.name) == 0) {
  1991. drv_count[avalon_drv.drv_id].limit = lim;
  1992. found = true;
  1993. }
  1994. #endif
  1995. if (!found)
  1996. quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
  1997. ptr = comma;
  1998. } while (ptr);
  1999. free(fre);
  2000. }
  2001. }
  2002. }