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