usbutils.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  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. applog(LOG_DEBUG, "USB uninit %s%i",
  1125. cgpu->drv->name, cgpu->device_id);
  1126. // May have happened already during a failed initialisation
  1127. // if release_cgpu() was called due to a USB NODEV(err)
  1128. if (!cgpu->usbdev)
  1129. return;
  1130. libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
  1131. libusb_close(cgpu->usbdev->handle);
  1132. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  1133. }
  1134. static void release_cgpu(struct cgpu_info *cgpu)
  1135. {
  1136. struct cg_usb_device *cgusb = cgpu->usbdev;
  1137. struct cgpu_info *lookcgpu;
  1138. int i;
  1139. applog(LOG_DEBUG, "USB release %s%i",
  1140. cgpu->drv->name, cgpu->device_id);
  1141. // It has already been done
  1142. if (cgpu->usbinfo.nodev)
  1143. return;
  1144. total_count--;
  1145. drv_count[cgpu->drv->drv_id].count--;
  1146. cgpu->usbinfo.nodev = true;
  1147. cgpu->usbinfo.nodev_count++;
  1148. cgtime(&cgpu->usbinfo.last_nodev);
  1149. // Any devices sharing the same USB device should be marked also
  1150. // Currently only MMQ shares a USB device
  1151. for (i = 0; i < total_devices; i++) {
  1152. lookcgpu = get_devices(i);
  1153. if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
  1154. total_count--;
  1155. drv_count[lookcgpu->drv->drv_id].count--;
  1156. lookcgpu->usbinfo.nodev = true;
  1157. lookcgpu->usbinfo.nodev_count++;
  1158. memcpy(&(lookcgpu->usbinfo.last_nodev),
  1159. &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
  1160. lookcgpu->usbdev = NULL;
  1161. }
  1162. }
  1163. usb_uninit(cgpu);
  1164. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  1165. }
  1166. #define USB_INIT_FAIL 0
  1167. #define USB_INIT_OK 1
  1168. #define USB_INIT_IGNORE 2
  1169. static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  1170. {
  1171. struct cg_usb_device *cgusb = NULL;
  1172. struct libusb_config_descriptor *config = NULL;
  1173. const struct libusb_interface_descriptor *idesc;
  1174. const struct libusb_endpoint_descriptor *epdesc;
  1175. unsigned char strbuf[STRBUFLEN+1];
  1176. char devstr[STRBUFLEN+1];
  1177. int err, i, j, k;
  1178. int bad = USB_INIT_FAIL;
  1179. cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
  1180. cgpu->usbinfo.device_address = libusb_get_device_address(dev);
  1181. sprintf(devstr, "- %s device %d:%d", found->name,
  1182. cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  1183. cgusb = calloc(1, sizeof(*cgusb));
  1184. cgusb->found = found;
  1185. if (found->idVendor == IDVENDOR_FTDI)
  1186. cgusb->usb_type = USB_TYPE_FTDI;
  1187. cgusb->ident = found->ident;
  1188. cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
  1189. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  1190. if (err) {
  1191. applog(LOG_DEBUG,
  1192. "USB init failed to get descriptor, err %d %s",
  1193. err, devstr);
  1194. goto dame;
  1195. }
  1196. err = libusb_open(dev, &(cgusb->handle));
  1197. if (err) {
  1198. switch (err) {
  1199. case LIBUSB_ERROR_ACCESS:
  1200. applog(LOG_ERR,
  1201. "USB init open device failed, err %d, "
  1202. "you dont have priviledge to access %s",
  1203. err, devstr);
  1204. break;
  1205. #ifdef WIN32
  1206. // Windows specific message
  1207. case LIBUSB_ERROR_NOT_SUPPORTED:
  1208. applog(LOG_ERR,
  1209. "USB init, open device failed, err %d, "
  1210. "you need to install a Windows USB driver for %s",
  1211. err, devstr);
  1212. break;
  1213. #endif
  1214. default:
  1215. applog(LOG_DEBUG,
  1216. "USB init, open failed, err %d %s",
  1217. err, devstr);
  1218. }
  1219. goto dame;
  1220. }
  1221. #ifndef WIN32
  1222. if (libusb_kernel_driver_active(cgusb->handle, found->kernel) == 1) {
  1223. applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
  1224. err = libusb_detach_kernel_driver(cgusb->handle, found->kernel);
  1225. if (err == 0) {
  1226. applog(LOG_DEBUG,
  1227. "USB init, kernel detached successfully %s",
  1228. devstr);
  1229. } else {
  1230. applog(LOG_WARNING,
  1231. "USB init, kernel detach failed, err %d in use? %s",
  1232. err, devstr);
  1233. goto cldame;
  1234. }
  1235. }
  1236. #endif
  1237. if (found->iManufacturer) {
  1238. unsigned char man[STRBUFLEN+1];
  1239. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1240. cgusb->descriptor->iManufacturer,
  1241. man, STRBUFLEN);
  1242. if (err < 0) {
  1243. applog(LOG_DEBUG,
  1244. "USB init, failed to get iManufacturer, err %d %s",
  1245. err, devstr);
  1246. goto cldame;
  1247. }
  1248. if (strcmp((char *)man, found->iManufacturer)) {
  1249. applog(LOG_DEBUG, "USB init, iManufacturer mismatch %s",
  1250. devstr);
  1251. bad = USB_INIT_IGNORE;
  1252. goto cldame;
  1253. }
  1254. }
  1255. if (found->iProduct) {
  1256. unsigned char prod[STRBUFLEN+1];
  1257. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1258. cgusb->descriptor->iProduct,
  1259. prod, STRBUFLEN);
  1260. if (err < 0) {
  1261. applog(LOG_DEBUG,
  1262. "USB init, failed to get iProduct, err %d %s",
  1263. err, devstr);
  1264. goto cldame;
  1265. }
  1266. if (strcmp((char *)prod, found->iProduct)) {
  1267. applog(LOG_DEBUG, "USB init, iProduct mismatch %s",
  1268. devstr);
  1269. bad = USB_INIT_IGNORE;
  1270. goto cldame;
  1271. }
  1272. }
  1273. err = libusb_set_configuration(cgusb->handle, found->config);
  1274. if (err) {
  1275. switch(err) {
  1276. case LIBUSB_ERROR_BUSY:
  1277. applog(LOG_WARNING,
  1278. "USB init, set config %d in use %s",
  1279. found->config, devstr);
  1280. break;
  1281. default:
  1282. applog(LOG_DEBUG,
  1283. "USB init, failed to set config to %d, err %d %s",
  1284. found->config, err, devstr);
  1285. }
  1286. goto cldame;
  1287. }
  1288. err = libusb_get_active_config_descriptor(dev, &config);
  1289. if (err) {
  1290. applog(LOG_DEBUG,
  1291. "USB init, failed to get config descriptor, err %d %s",
  1292. err, devstr);
  1293. goto cldame;
  1294. }
  1295. if ((int)(config->bNumInterfaces) <= found->interface) {
  1296. applog(LOG_DEBUG, "USB init bNumInterfaces <= interface %s",
  1297. devstr);
  1298. goto cldame;
  1299. }
  1300. for (i = 0; i < found->epcount; i++)
  1301. found->eps[i].found = false;
  1302. for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
  1303. idesc = &(config->interface[found->interface].altsetting[i]);
  1304. for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
  1305. epdesc = &(idesc->endpoint[j]);
  1306. for (k = 0; k < found->epcount; k++) {
  1307. if (!found->eps[k].found) {
  1308. if (epdesc->bmAttributes == found->eps[k].att
  1309. && epdesc->wMaxPacketSize >= found->eps[k].size
  1310. && epdesc->bEndpointAddress == found->eps[k].ep) {
  1311. found->eps[k].found = true;
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. for (i = 0; i < found->epcount; i++) {
  1319. if (found->eps[i].found == false) {
  1320. applog(LOG_DEBUG, "USB init found == false %s",
  1321. devstr);
  1322. goto cldame;
  1323. }
  1324. }
  1325. err = libusb_claim_interface(cgusb->handle, found->interface);
  1326. if (err) {
  1327. switch(err) {
  1328. case LIBUSB_ERROR_BUSY:
  1329. applog(LOG_WARNING,
  1330. "USB init, claim interface %d in use %s",
  1331. found->interface, devstr);
  1332. break;
  1333. default:
  1334. applog(LOG_DEBUG,
  1335. "USB init, claim interface %d failed, err %d %s",
  1336. found->interface, err, devstr);
  1337. }
  1338. goto cldame;
  1339. }
  1340. cgusb->usbver = cgusb->descriptor->bcdUSB;
  1341. // TODO: allow this with the right version of the libusb include and running library
  1342. // cgusb->speed = libusb_get_device_speed(dev);
  1343. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1344. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  1345. if (err > 0)
  1346. cgusb->prod_string = strdup((char *)strbuf);
  1347. else
  1348. cgusb->prod_string = (char *)BLANK;
  1349. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1350. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  1351. if (err > 0)
  1352. cgusb->manuf_string = strdup((char *)strbuf);
  1353. else
  1354. cgusb->manuf_string = (char *)BLANK;
  1355. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  1356. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  1357. if (err > 0)
  1358. cgusb->serial_string = strdup((char *)strbuf);
  1359. else
  1360. cgusb->serial_string = (char *)BLANK;
  1361. // TODO: ?
  1362. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  1363. // cgusb->interfaceVersion
  1364. applog(LOG_DEBUG,
  1365. "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
  1366. devstr, cgusb->usbver, cgusb->prod_string,
  1367. cgusb->manuf_string, cgusb->serial_string);
  1368. cgpu->usbdev = cgusb;
  1369. libusb_free_config_descriptor(config);
  1370. // Allow a name change based on the idVendor+idProduct
  1371. // N.B. must be done before calling add_cgpu()
  1372. if (strcmp(cgpu->drv->name, found->name)) {
  1373. if (!cgpu->drv->copy)
  1374. cgpu->drv = copy_drv(cgpu->drv);
  1375. cgpu->drv->name = (char *)(found->name);
  1376. }
  1377. return USB_INIT_OK;
  1378. cldame:
  1379. libusb_close(cgusb->handle);
  1380. dame:
  1381. if (config)
  1382. libusb_free_config_descriptor(config);
  1383. cgusb = free_cgusb(cgusb);
  1384. return bad;
  1385. }
  1386. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  1387. {
  1388. int ret;
  1389. ret = _usb_init(cgpu, dev, found);
  1390. if (ret == USB_INIT_FAIL)
  1391. applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
  1392. cgpu->drv->dname,
  1393. (int)(cgpu->usbinfo.bus_number),
  1394. (int)(cgpu->usbinfo.device_address));
  1395. return (ret == USB_INIT_OK);
  1396. }
  1397. static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
  1398. {
  1399. struct libusb_device_descriptor desc;
  1400. int bus_number, device_address;
  1401. int err, i;
  1402. bool ok;
  1403. err = libusb_get_device_descriptor(dev, &desc);
  1404. if (err) {
  1405. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  1406. return false;
  1407. }
  1408. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  1409. applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
  1410. drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  1411. return false;
  1412. }
  1413. if (busdev_count > 0) {
  1414. bus_number = (int)libusb_get_bus_number(dev);
  1415. device_address = (int)libusb_get_device_address(dev);
  1416. ok = false;
  1417. for (i = 0; i < busdev_count; i++) {
  1418. if (bus_number == busdev[i].bus_number) {
  1419. if (busdev[i].device_address == -1 ||
  1420. device_address == busdev[i].device_address) {
  1421. ok = true;
  1422. break;
  1423. }
  1424. }
  1425. }
  1426. if (!ok) {
  1427. applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
  1428. drv->name, look->name, look->idVendor, look->idProduct,
  1429. bus_number, device_address);
  1430. return false;
  1431. }
  1432. }
  1433. applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
  1434. drv->name, look->name, look->idVendor, look->idProduct);
  1435. return true;
  1436. }
  1437. static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
  1438. {
  1439. struct usb_find_devices *found;
  1440. int i;
  1441. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  1442. if (find_dev[i].drv == drvnum) {
  1443. if (usb_check_device(drv, dev, &(find_dev[i]))) {
  1444. found = malloc(sizeof(*found));
  1445. memcpy(found, &(find_dev[i]), sizeof(*found));
  1446. return found;
  1447. }
  1448. }
  1449. return NULL;
  1450. }
  1451. static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
  1452. {
  1453. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1454. applog(LOG_DEBUG,
  1455. "USB scan devices3: %s limit %d reached",
  1456. drv->dname, drv_count[drv->drv_id].limit);
  1457. return NULL;
  1458. }
  1459. #ifdef USE_BFLSC
  1460. if (drv->drv_id == DRIVER_BFLSC)
  1461. return usb_check_each(DRV_BFLSC, drv, dev);
  1462. #endif
  1463. #ifdef USE_BITFORCE
  1464. if (drv->drv_id == DRIVER_BITFORCE)
  1465. return usb_check_each(DRV_BITFORCE, drv, dev);
  1466. #endif
  1467. #ifdef USE_MODMINER
  1468. if (drv->drv_id == DRIVER_MODMINER)
  1469. return usb_check_each(DRV_MODMINER, drv, dev);
  1470. #endif
  1471. #ifdef USE_ICARUS
  1472. if (drv->drv_id == DRIVER_ICARUS)
  1473. return usb_check_each(DRV_ICARUS, drv, dev);
  1474. #endif
  1475. #ifdef USE_AVALON
  1476. if (drv->drv_id == DRIVER_AVALON)
  1477. return usb_check_each(DRV_AVALON, drv, dev);
  1478. #endif
  1479. return NULL;
  1480. }
  1481. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
  1482. {
  1483. libusb_device **list;
  1484. ssize_t count, i;
  1485. struct usb_find_devices *found;
  1486. applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
  1487. if (total_count >= total_limit) {
  1488. applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
  1489. return;
  1490. }
  1491. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1492. applog(LOG_DEBUG,
  1493. "USB scan devices: %s limit %d reached",
  1494. drv->dname, drv_count[drv->drv_id].limit);
  1495. return;
  1496. }
  1497. count = libusb_get_device_list(NULL, &list);
  1498. if (count < 0) {
  1499. applog(LOG_DEBUG, "USB scan devices: failed, err %zd", count);
  1500. return;
  1501. }
  1502. if (count == 0)
  1503. applog(LOG_DEBUG, "USB scan devices: found no devices");
  1504. for (i = 0; i < count; i++) {
  1505. if (total_count >= total_limit) {
  1506. applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
  1507. break;
  1508. }
  1509. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1510. applog(LOG_DEBUG,
  1511. "USB scan devices2: %s limit %d reached",
  1512. drv->dname, drv_count[drv->drv_id].limit);
  1513. break;
  1514. }
  1515. found = usb_check(drv, list[i]);
  1516. if (found != NULL) {
  1517. if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
  1518. free(found);
  1519. else {
  1520. if (!device_detect(list[i], found))
  1521. cgminer_usb_unlock(drv, list[i]);
  1522. else {
  1523. total_count++;
  1524. drv_count[drv->drv_id].count++;
  1525. }
  1526. }
  1527. }
  1528. }
  1529. libusb_free_device_list(list, 1);
  1530. }
  1531. // Set this to 0 to remove stats processing
  1532. #define DO_USB_STATS 1
  1533. #if DO_USB_STATS
  1534. #define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
  1535. #define STATS_TIMEVAL(tv) cgtime(tv)
  1536. #else
  1537. #define USB_STATS(sgpu, sta, fin, err, cmd, seq)
  1538. #define STATS_TIMEVAL(tv)
  1539. #endif
  1540. // The stat data can be spurious due to not locking it before copying it -
  1541. // however that would require the stat() function to also lock and release
  1542. // a mutex every time a usb read or write is called which would slow
  1543. // things down more
  1544. struct api_data *api_usb_stats(__maybe_unused int *count)
  1545. {
  1546. #if DO_USB_STATS
  1547. struct cg_usb_stats_details *details;
  1548. struct cg_usb_stats *sta;
  1549. struct api_data *root = NULL;
  1550. int device;
  1551. int cmdseq;
  1552. if (next_stat == 0)
  1553. return NULL;
  1554. while (*count < next_stat * C_MAX * 2) {
  1555. device = *count / (C_MAX * 2);
  1556. cmdseq = *count % (C_MAX * 2);
  1557. (*count)++;
  1558. sta = &(usb_stats[device]);
  1559. details = &(sta->details[cmdseq]);
  1560. // Only show stats that have results
  1561. if (details->item[CMD_CMD].count == 0 &&
  1562. details->item[CMD_TIMEOUT].count == 0 &&
  1563. details->item[CMD_ERROR].count == 0)
  1564. continue;
  1565. root = api_add_string(root, "Name", sta->name, false);
  1566. root = api_add_int(root, "ID", &(sta->device_id), false);
  1567. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  1568. root = api_add_int(root, "Seq", &(details->seq), true);
  1569. root = api_add_uint64(root, "Count",
  1570. &(details->item[CMD_CMD].count), true);
  1571. root = api_add_double(root, "Total Delay",
  1572. &(details->item[CMD_CMD].total_delay), true);
  1573. root = api_add_double(root, "Min Delay",
  1574. &(details->item[CMD_CMD].min_delay), true);
  1575. root = api_add_double(root, "Max Delay",
  1576. &(details->item[CMD_CMD].max_delay), true);
  1577. root = api_add_uint64(root, "Timeout Count",
  1578. &(details->item[CMD_TIMEOUT].count), true);
  1579. root = api_add_double(root, "Timeout Total Delay",
  1580. &(details->item[CMD_TIMEOUT].total_delay), true);
  1581. root = api_add_double(root, "Timeout Min Delay",
  1582. &(details->item[CMD_TIMEOUT].min_delay), true);
  1583. root = api_add_double(root, "Timeout Max Delay",
  1584. &(details->item[CMD_TIMEOUT].max_delay), true);
  1585. root = api_add_uint64(root, "Error Count",
  1586. &(details->item[CMD_ERROR].count), true);
  1587. root = api_add_double(root, "Error Total Delay",
  1588. &(details->item[CMD_ERROR].total_delay), true);
  1589. root = api_add_double(root, "Error Min Delay",
  1590. &(details->item[CMD_ERROR].min_delay), true);
  1591. root = api_add_double(root, "Error Max Delay",
  1592. &(details->item[CMD_ERROR].max_delay), true);
  1593. root = api_add_timeval(root, "First Command",
  1594. &(details->item[CMD_CMD].first), true);
  1595. root = api_add_timeval(root, "Last Command",
  1596. &(details->item[CMD_CMD].last), true);
  1597. root = api_add_timeval(root, "First Timeout",
  1598. &(details->item[CMD_TIMEOUT].first), true);
  1599. root = api_add_timeval(root, "Last Timeout",
  1600. &(details->item[CMD_TIMEOUT].last), true);
  1601. root = api_add_timeval(root, "First Error",
  1602. &(details->item[CMD_ERROR].first), true);
  1603. root = api_add_timeval(root, "Last Error",
  1604. &(details->item[CMD_ERROR].last), true);
  1605. return root;
  1606. }
  1607. #endif
  1608. return NULL;
  1609. }
  1610. #if DO_USB_STATS
  1611. static void newstats(struct cgpu_info *cgpu)
  1612. {
  1613. int i;
  1614. mutex_lock(&cgusb_lock);
  1615. cgpu->usbinfo.usbstat = ++next_stat;
  1616. mutex_unlock(&cgusb_lock);
  1617. usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
  1618. usb_stats[next_stat-1].name = cgpu->drv->name;
  1619. usb_stats[next_stat-1].device_id = -1;
  1620. usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
  1621. for (i = 1; i < C_MAX * 2; i += 2)
  1622. usb_stats[next_stat-1].details[i].seq = 1;
  1623. }
  1624. #endif
  1625. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  1626. {
  1627. #if DO_USB_STATS
  1628. if (cgpu->usbinfo.usbstat < 1)
  1629. newstats(cgpu);
  1630. // we don't know the device_id until after add_cgpu()
  1631. usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
  1632. #endif
  1633. }
  1634. #if DO_USB_STATS
  1635. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
  1636. {
  1637. struct cg_usb_stats_details *details;
  1638. double diff;
  1639. int item;
  1640. if (cgpu->usbinfo.usbstat < 1)
  1641. newstats(cgpu);
  1642. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
  1643. diff = tdiff(tv_finish, tv_start);
  1644. switch (err) {
  1645. case LIBUSB_SUCCESS:
  1646. item = CMD_CMD;
  1647. break;
  1648. case LIBUSB_ERROR_TIMEOUT:
  1649. item = CMD_TIMEOUT;
  1650. break;
  1651. default:
  1652. item = CMD_ERROR;
  1653. break;
  1654. }
  1655. if (details->item[item].count == 0) {
  1656. details->item[item].min_delay = diff;
  1657. memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  1658. } else if (diff < details->item[item].min_delay)
  1659. details->item[item].min_delay = diff;
  1660. if (diff > details->item[item].max_delay)
  1661. details->item[item].max_delay = diff;
  1662. details->item[item].total_delay += diff;
  1663. memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
  1664. details->item[item].count++;
  1665. }
  1666. static void rejected_inc(struct cgpu_info *cgpu)
  1667. {
  1668. struct cg_usb_stats_details *details;
  1669. int item = CMD_ERROR;
  1670. if (cgpu->usbinfo.usbstat < 1)
  1671. newstats(cgpu);
  1672. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
  1673. details->item[item].count++;
  1674. }
  1675. #endif
  1676. #define USB_MAX_READ 8192
  1677. 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)
  1678. {
  1679. struct cg_usb_device *usbdev = cgpu->usbdev;
  1680. bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
  1681. #if DO_USB_STATS
  1682. struct timeval tv_start;
  1683. #endif
  1684. struct timeval read_start, tv_finish;
  1685. unsigned int initial_timeout;
  1686. double max, done;
  1687. int bufleft, err, got, tot;
  1688. bool first = true;
  1689. unsigned char *search;
  1690. int endlen;
  1691. // We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
  1692. unsigned char usbbuf[USB_MAX_READ+4], *ptr;
  1693. size_t usbbufread;
  1694. 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));
  1695. if (bufsiz > USB_MAX_READ)
  1696. quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, bufsiz, USB_MAX_READ);
  1697. if (cgpu->usbinfo.nodev) {
  1698. *buf = '\0';
  1699. *processed = 0;
  1700. #if DO_USB_STATS
  1701. rejected_inc(cgpu);
  1702. #endif
  1703. return LIBUSB_ERROR_NO_DEVICE;
  1704. }
  1705. if (timeout == DEVTIMEOUT)
  1706. timeout = usbdev->found->timeout;
  1707. if (end == NULL) {
  1708. tot = 0;
  1709. ptr = usbbuf;
  1710. bufleft = bufsiz;
  1711. err = LIBUSB_SUCCESS;
  1712. initial_timeout = timeout;
  1713. max = ((double)timeout) / 1000.0;
  1714. cgtime(&read_start);
  1715. while (bufleft > 0) {
  1716. if (ftdi)
  1717. usbbufread = bufleft + 2;
  1718. else
  1719. usbbufread = bufleft;
  1720. got = 0;
  1721. STATS_TIMEVAL(&tv_start);
  1722. err = libusb_bulk_transfer(usbdev->handle,
  1723. usbdev->found->eps[ep].ep,
  1724. ptr, usbbufread, &got, timeout);
  1725. STATS_TIMEVAL(&tv_finish);
  1726. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1727. ptr[got] = '\0';
  1728. 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);
  1729. if (ftdi) {
  1730. // first 2 bytes returned are an FTDI status
  1731. if (got > 2) {
  1732. got -= 2;
  1733. memmove(ptr, ptr+2, got+1);
  1734. } else {
  1735. got = 0;
  1736. *ptr = '\0';
  1737. }
  1738. }
  1739. tot += got;
  1740. if (err || readonce)
  1741. break;
  1742. ptr += got;
  1743. bufleft -= got;
  1744. first = false;
  1745. done = tdiff(&tv_finish, &read_start);
  1746. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1747. if (unlikely(done >= max))
  1748. break;
  1749. timeout = initial_timeout - (done * 1000);
  1750. }
  1751. *processed = tot;
  1752. memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
  1753. if (NODEV(err))
  1754. release_cgpu(cgpu);
  1755. return err;
  1756. }
  1757. tot = 0;
  1758. ptr = usbbuf;
  1759. bufleft = bufsiz;
  1760. endlen = strlen(end);
  1761. err = LIBUSB_SUCCESS;
  1762. initial_timeout = timeout;
  1763. max = ((double)timeout) / 1000.0;
  1764. cgtime(&read_start);
  1765. while (bufleft > 0) {
  1766. if (ftdi)
  1767. usbbufread = bufleft + 2;
  1768. else
  1769. usbbufread = bufleft;
  1770. got = 0;
  1771. STATS_TIMEVAL(&tv_start);
  1772. err = libusb_bulk_transfer(usbdev->handle,
  1773. usbdev->found->eps[ep].ep,
  1774. ptr, usbbufread, &got, timeout);
  1775. cgtime(&tv_finish);
  1776. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1777. ptr[got] = '\0';
  1778. 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);
  1779. if (ftdi) {
  1780. // first 2 bytes returned are an FTDI status
  1781. if (got > 2) {
  1782. got -= 2;
  1783. memmove(ptr, ptr+2, got+1);
  1784. } else {
  1785. got = 0;
  1786. *ptr = '\0';
  1787. }
  1788. }
  1789. tot += got;
  1790. if (err || readonce)
  1791. break;
  1792. // WARNING - this will return data past END ('if' there is extra data)
  1793. if (endlen <= tot) {
  1794. // If END is only 1 char - do a faster search
  1795. if (endlen == 1) {
  1796. if (strchr((char *)ptr, *end))
  1797. break;
  1798. } else {
  1799. // must allow END to have been chopped in 2 transfers
  1800. if ((tot - got) >= (endlen - 1))
  1801. search = ptr - (endlen - 1);
  1802. else
  1803. search = ptr - (tot - got);
  1804. if (strstr((char *)search, end))
  1805. break;
  1806. }
  1807. }
  1808. ptr += got;
  1809. bufleft -= got;
  1810. first = false;
  1811. done = tdiff(&tv_finish, &read_start);
  1812. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1813. if (unlikely(done >= max))
  1814. break;
  1815. timeout = initial_timeout - (done * 1000);
  1816. }
  1817. *processed = tot;
  1818. memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
  1819. if (NODEV(err))
  1820. release_cgpu(cgpu);
  1821. return err;
  1822. }
  1823. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
  1824. {
  1825. struct cg_usb_device *usbdev = cgpu->usbdev;
  1826. #if DO_USB_STATS
  1827. struct timeval tv_start, tv_finish;
  1828. #endif
  1829. int err, sent;
  1830. 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));
  1831. if (cgpu->usbinfo.nodev) {
  1832. *processed = 0;
  1833. #if DO_USB_STATS
  1834. rejected_inc(cgpu);
  1835. #endif
  1836. return LIBUSB_ERROR_NO_DEVICE;
  1837. }
  1838. sent = 0;
  1839. STATS_TIMEVAL(&tv_start);
  1840. err = libusb_bulk_transfer(usbdev->handle,
  1841. usbdev->found->eps[ep].ep,
  1842. (unsigned char *)buf,
  1843. bufsiz, &sent,
  1844. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1845. STATS_TIMEVAL(&tv_finish);
  1846. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1847. 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);
  1848. *processed = sent;
  1849. if (NODEV(err))
  1850. release_cgpu(cgpu);
  1851. return err;
  1852. }
  1853. 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)
  1854. {
  1855. struct cg_usb_device *usbdev = cgpu->usbdev;
  1856. #if DO_USB_STATS
  1857. struct timeval tv_start, tv_finish;
  1858. #endif
  1859. int err;
  1860. 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));
  1861. if (cgpu->usbinfo.nodev) {
  1862. #if DO_USB_STATS
  1863. rejected_inc(cgpu);
  1864. #endif
  1865. return LIBUSB_ERROR_NO_DEVICE;
  1866. }
  1867. STATS_TIMEVAL(&tv_start);
  1868. err = libusb_control_transfer(usbdev->handle, request_type,
  1869. bRequest, wValue, wIndex, NULL, 0,
  1870. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1871. STATS_TIMEVAL(&tv_finish);
  1872. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1873. USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
  1874. if (NODEV(err))
  1875. release_cgpu(cgpu);
  1876. return err;
  1877. }
  1878. void usb_cleanup()
  1879. {
  1880. struct cgpu_info *cgpu;
  1881. int i;
  1882. hotplug_time = 0;
  1883. nmsleep(10);
  1884. for (i = 0; i < total_devices; i++) {
  1885. cgpu = devices[i];
  1886. switch (cgpu->drv->drv_id) {
  1887. case DRIVER_BFLSC:
  1888. case DRIVER_BITFORCE:
  1889. case DRIVER_MODMINER:
  1890. case DRIVER_ICARUS:
  1891. case DRIVER_AVALON:
  1892. release_cgpu(cgpu);
  1893. break;
  1894. default:
  1895. break;
  1896. }
  1897. }
  1898. }
  1899. void usb_initialise()
  1900. {
  1901. char *fre, *ptr, *comma, *colon;
  1902. int bus, dev, lim, i;
  1903. bool found;
  1904. for (i = 0; i < DRIVER_MAX; i++) {
  1905. drv_count[i].count = 0;
  1906. drv_count[i].limit = 999999;
  1907. }
  1908. cgusb_check_init();
  1909. if (opt_usb_select && *opt_usb_select) {
  1910. // Absolute device limit
  1911. if (*opt_usb_select == ':') {
  1912. total_limit = atoi(opt_usb_select+1);
  1913. if (total_limit < 0)
  1914. quit(1, "Invalid --usb total limit");
  1915. // Comma list of bus:dev devices to match
  1916. } else if (isdigit(*opt_usb_select)) {
  1917. fre = ptr = strdup(opt_usb_select);
  1918. do {
  1919. comma = strchr(ptr, ',');
  1920. if (comma)
  1921. *(comma++) = '\0';
  1922. colon = strchr(ptr, ':');
  1923. if (!colon)
  1924. quit(1, "Invalid --usb bus:dev missing ':'");
  1925. *(colon++) = '\0';
  1926. if (!isdigit(*ptr))
  1927. quit(1, "Invalid --usb bus:dev - bus must be a number");
  1928. if (!isdigit(*colon) && *colon != '*')
  1929. quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
  1930. bus = atoi(ptr);
  1931. if (bus <= 0)
  1932. quit(1, "Invalid --usb bus:dev - bus must be > 0");
  1933. if (!colon == '*')
  1934. dev = -1;
  1935. else {
  1936. dev = atoi(colon);
  1937. if (dev <= 0)
  1938. quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
  1939. }
  1940. busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
  1941. busdev[busdev_count-1].bus_number = bus;
  1942. busdev[busdev_count-1].device_address = dev;
  1943. ptr = comma;
  1944. } while (ptr);
  1945. free(fre);
  1946. // Comma list of DRV:limit
  1947. } else {
  1948. fre = ptr = strdup(opt_usb_select);
  1949. do {
  1950. comma = strchr(ptr, ',');
  1951. if (comma)
  1952. *(comma++) = '\0';
  1953. colon = strchr(ptr, ':');
  1954. if (!colon)
  1955. quit(1, "Invalid --usb DRV:limit missing ':'");
  1956. *(colon++) = '\0';
  1957. if (!isdigit(*colon))
  1958. quit(1, "Invalid --usb DRV:limit - limit must be a number");
  1959. lim = atoi(colon);
  1960. if (lim < 0)
  1961. quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
  1962. found = false;
  1963. #ifdef USE_BFLSC
  1964. if (strcasecmp(ptr, bflsc_drv.name) == 0) {
  1965. drv_count[bflsc_drv.drv_id].limit = lim;
  1966. found = true;
  1967. }
  1968. #endif
  1969. #ifdef USE_BITFORCE
  1970. if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
  1971. drv_count[bitforce_drv.drv_id].limit = lim;
  1972. found = true;
  1973. }
  1974. #endif
  1975. #ifdef USE_MODMINER
  1976. if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
  1977. drv_count[modminer_drv.drv_id].limit = lim;
  1978. found = true;
  1979. }
  1980. #endif
  1981. #ifdef USE_ICARUS
  1982. if (!found && strcasecmp(ptr, icarus_drv.name) == 0) {
  1983. drv_count[icarus_drv.drv_id].limit = lim;
  1984. found = true;
  1985. }
  1986. #endif
  1987. #ifdef USE_AVALON
  1988. if (!found && strcasecmp(ptr, avalon_drv.name) == 0) {
  1989. drv_count[avalon_drv.drv_id].limit = lim;
  1990. found = true;
  1991. }
  1992. #endif
  1993. if (!found)
  1994. quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
  1995. ptr = comma;
  1996. } while (ptr);
  1997. free(fre);
  1998. }
  1999. }
  2000. }