usbutils.c 61 KB

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