usbutils.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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_ICARUS
  29. #define DRV_ICARUS 4
  30. #endif
  31. #define DRV_LAST -1
  32. #define USB_CONFIG 1
  33. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  34. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  35. #ifdef WIN32
  36. #define BFLSC_TIMEOUT_MS 500
  37. #define BITFORCE_TIMEOUT_MS 500
  38. #define MODMINER_TIMEOUT_MS 200
  39. #else
  40. #define BFLSC_TIMEOUT_MS 200
  41. #define BITFORCE_TIMEOUT_MS 200
  42. #define MODMINER_TIMEOUT_MS 100
  43. #endif
  44. #ifdef USE_BFLSC
  45. // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
  46. static struct usb_endpoints bas_eps[] = {
  47. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  48. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  49. };
  50. #endif
  51. #ifdef USE_BITFORCE
  52. // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
  53. static struct usb_endpoints bfl_eps[] = {
  54. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
  55. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
  56. };
  57. #endif
  58. #ifdef USE_MODMINER
  59. static struct usb_endpoints mmq_eps[] = {
  60. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
  61. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0 }
  62. };
  63. #endif
  64. // TODO: Add support for (at least) Isochronous endpoints
  65. static struct usb_find_devices find_dev[] = {
  66. /*
  67. #ifdef USE_ICARUS
  68. { DRV_ICARUS, "ICA", 0x067b, 0x0230, true, EPI(3), EPO(2), 1 },
  69. { DRV_ICARUS, "LOT", 0x0403, 0x6001, false, EPI(0), EPO(0), 1 },
  70. { DRV_ICARUS, "CM1", 0x067b, 0x0230, false, EPI(0), EPO(0), 1 },
  71. #endif
  72. */
  73. #ifdef USE_BFLSC
  74. {
  75. .drv = DRV_BFLSC,
  76. .name = "BAS",
  77. .idVendor = 0x0403,
  78. .idProduct = 0x6014,
  79. .kernel = 0,
  80. .config = 1,
  81. .interface = 0,
  82. .timeout = BFLSC_TIMEOUT_MS,
  83. .epcount = ARRAY_SIZE(bas_eps),
  84. .eps = bas_eps },
  85. #endif
  86. #ifdef USE_BITFORCE
  87. {
  88. .drv = DRV_BITFORCE,
  89. .name = "BFL",
  90. .idVendor = 0x0403,
  91. .idProduct = 0x6014,
  92. .kernel = 0,
  93. .config = 1,
  94. .interface = 0,
  95. .timeout = BITFORCE_TIMEOUT_MS,
  96. .epcount = ARRAY_SIZE(bfl_eps),
  97. .eps = bfl_eps },
  98. #endif
  99. #ifdef USE_MODMINER
  100. {
  101. .drv = DRV_MODMINER,
  102. .name = "MMQ",
  103. .idVendor = 0x1fc9,
  104. .idProduct = 0x0003,
  105. .kernel = 0,
  106. .config = 1,
  107. .interface = 1,
  108. .timeout = MODMINER_TIMEOUT_MS,
  109. .epcount = ARRAY_SIZE(mmq_eps),
  110. .eps = mmq_eps },
  111. #endif
  112. { DRV_LAST, NULL, 0, 0, 0, 0, 0, 0, 0, NULL }
  113. };
  114. #ifdef USE_BFLSC
  115. extern struct device_drv bflsc_drv;
  116. #endif
  117. #ifdef USE_BITFORCE
  118. extern struct device_drv bitforce_drv;
  119. #endif
  120. #ifdef USE_MODMINER
  121. extern struct device_drv modminer_drv;
  122. #endif
  123. #ifdef USE_ICARUS
  124. extern struct device_drv icarus_drv;
  125. #endif
  126. #define STRBUFLEN 256
  127. static const char *BLANK = "";
  128. // For device limits by driver
  129. static struct driver_count {
  130. uint32_t count;
  131. uint32_t limit;
  132. } drv_count[DRIVER_MAX];
  133. // For device limits by list of bus/dev
  134. static struct usb_busdev {
  135. int bus_number;
  136. int device_address;
  137. } *busdev;
  138. static int busdev_count = 0;
  139. // Total device limit
  140. static int total_count = 0;
  141. static int total_limit = 999999;
  142. static bool stats_initialised = false;
  143. struct cg_usb_stats_item {
  144. uint64_t count;
  145. double total_delay;
  146. double min_delay;
  147. double max_delay;
  148. struct timeval first;
  149. struct timeval last;
  150. };
  151. #define CMD_CMD 0
  152. #define CMD_TIMEOUT 1
  153. #define CMD_ERROR 2
  154. struct cg_usb_stats_details {
  155. int seq;
  156. struct cg_usb_stats_item item[CMD_ERROR+1];
  157. };
  158. struct cg_usb_stats {
  159. char *name;
  160. int device_id;
  161. struct cg_usb_stats_details *details;
  162. };
  163. #define SEQ0 0
  164. #define SEQ1 1
  165. static struct cg_usb_stats *usb_stats = NULL;
  166. static int next_stat = 0;
  167. static const char **usb_commands;
  168. static const char *C_REJECTED_S = "RejectedNoDevice";
  169. static const char *C_PING_S = "Ping";
  170. static const char *C_CLEAR_S = "Clear";
  171. static const char *C_REQUESTVERSION_S = "RequestVersion";
  172. static const char *C_GETVERSION_S = "GetVersion";
  173. static const char *C_REQUESTFPGACOUNT_S = "RequestFPGACount";
  174. static const char *C_GETFPGACOUNT_S = "GetFPGACount";
  175. static const char *C_STARTPROGRAM_S = "StartProgram";
  176. static const char *C_STARTPROGRAMSTATUS_S = "StartProgramStatus";
  177. static const char *C_PROGRAM_S = "Program";
  178. static const char *C_PROGRAMSTATUS_S = "ProgramStatus";
  179. static const char *C_PROGRAMSTATUS2_S = "ProgramStatus2";
  180. static const char *C_FINALPROGRAMSTATUS_S = "FinalProgramStatus";
  181. static const char *C_SETCLOCK_S = "SetClock";
  182. static const char *C_REPLYSETCLOCK_S = "ReplySetClock";
  183. static const char *C_REQUESTUSERCODE_S = "RequestUserCode";
  184. static const char *C_GETUSERCODE_S = "GetUserCode";
  185. static const char *C_REQUESTTEMPERATURE_S = "RequestTemperature";
  186. static const char *C_GETTEMPERATURE_S = "GetTemperature";
  187. static const char *C_SENDWORK_S = "SendWork";
  188. static const char *C_SENDWORKSTATUS_S = "SendWorkStatus";
  189. static const char *C_REQUESTWORKSTATUS_S = "RequestWorkStatus";
  190. static const char *C_GETWORKSTATUS_S = "GetWorkStatus";
  191. static const char *C_REQUESTIDENTIFY_S = "RequestIdentify";
  192. static const char *C_GETIDENTIFY_S = "GetIdentify";
  193. static const char *C_REQUESTFLASH_S = "RequestFlash";
  194. static const char *C_REQUESTSENDWORK_S = "RequestSendWork";
  195. static const char *C_REQUESTSENDWORKSTATUS_S = "RequestSendWorkStatus";
  196. static const char *C_RESET_S = "Reset";
  197. static const char *C_SETBAUD_S = "SetBaud";
  198. static const char *C_SETDATA_S = "SetDataCtrl";
  199. static const char *C_SETFLOW_S = "SetFlowCtrl";
  200. static const char *C_SETMODEM_S = "SetModemCtrl";
  201. static const char *C_PURGERX_S = "PurgeRx";
  202. static const char *C_PURGETX_S = "PurgeTx";
  203. static const char *C_FLASHREPLY_S = "FlashReply";
  204. #ifdef EOL
  205. #undef EOL
  206. #endif
  207. #define EOL "\n"
  208. static const char *DESDEV = "Device";
  209. static const char *DESCON = "Config";
  210. static const char *DESSTR = "String";
  211. static const char *DESINT = "Interface";
  212. static const char *DESEP = "Endpoint";
  213. static const char *DESHID = "HID";
  214. static const char *DESRPT = "Report";
  215. static const char *DESPHY = "Physical";
  216. static const char *DESHUB = "Hub";
  217. static const char *EPIN = "In: ";
  218. static const char *EPOUT = "Out: ";
  219. static const char *EPX = "?: ";
  220. static const char *CONTROL = "Control";
  221. static const char *ISOCHRONOUS_X = "Isochronous+?";
  222. static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
  223. static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
  224. static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
  225. static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
  226. static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
  227. static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
  228. static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
  229. static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
  230. static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
  231. static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
  232. static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
  233. static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
  234. static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
  235. static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
  236. static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
  237. static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
  238. static const char *BULK = "Bulk";
  239. static const char *INTERRUPT = "Interrupt";
  240. static const char *UNKNOWN = "Unknown";
  241. static const char *destype(uint8_t bDescriptorType)
  242. {
  243. switch (bDescriptorType) {
  244. case LIBUSB_DT_DEVICE:
  245. return DESDEV;
  246. case LIBUSB_DT_CONFIG:
  247. return DESCON;
  248. case LIBUSB_DT_STRING:
  249. return DESSTR;
  250. case LIBUSB_DT_INTERFACE:
  251. return DESINT;
  252. case LIBUSB_DT_ENDPOINT:
  253. return DESEP;
  254. case LIBUSB_DT_HID:
  255. return DESHID;
  256. case LIBUSB_DT_REPORT:
  257. return DESRPT;
  258. case LIBUSB_DT_PHYSICAL:
  259. return DESPHY;
  260. case LIBUSB_DT_HUB:
  261. return DESHUB;
  262. }
  263. return UNKNOWN;
  264. }
  265. static const char *epdir(uint8_t bEndpointAddress)
  266. {
  267. switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
  268. case LIBUSB_ENDPOINT_IN:
  269. return EPIN;
  270. case LIBUSB_ENDPOINT_OUT:
  271. return EPOUT;
  272. }
  273. return EPX;
  274. }
  275. static const char *epatt(uint8_t bmAttributes)
  276. {
  277. switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
  278. case LIBUSB_TRANSFER_TYPE_CONTROL:
  279. return CONTROL;
  280. case LIBUSB_TRANSFER_TYPE_BULK:
  281. return BULK;
  282. case LIBUSB_TRANSFER_TYPE_INTERRUPT:
  283. return INTERRUPT;
  284. case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
  285. switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
  286. case LIBUSB_ISO_SYNC_TYPE_NONE:
  287. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  288. case LIBUSB_ISO_USAGE_TYPE_DATA:
  289. return ISOCHRONOUS_N_D;
  290. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  291. return ISOCHRONOUS_N_F;
  292. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  293. return ISOCHRONOUS_N_I;
  294. }
  295. return ISOCHRONOUS_N_X;
  296. case LIBUSB_ISO_SYNC_TYPE_ASYNC:
  297. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  298. case LIBUSB_ISO_USAGE_TYPE_DATA:
  299. return ISOCHRONOUS_A_D;
  300. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  301. return ISOCHRONOUS_A_F;
  302. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  303. return ISOCHRONOUS_A_I;
  304. }
  305. return ISOCHRONOUS_A_X;
  306. case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
  307. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  308. case LIBUSB_ISO_USAGE_TYPE_DATA:
  309. return ISOCHRONOUS_D_D;
  310. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  311. return ISOCHRONOUS_D_F;
  312. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  313. return ISOCHRONOUS_D_I;
  314. }
  315. return ISOCHRONOUS_D_X;
  316. case LIBUSB_ISO_SYNC_TYPE_SYNC:
  317. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  318. case LIBUSB_ISO_USAGE_TYPE_DATA:
  319. return ISOCHRONOUS_S_D;
  320. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  321. return ISOCHRONOUS_S_F;
  322. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  323. return ISOCHRONOUS_S_I;
  324. }
  325. return ISOCHRONOUS_S_X;
  326. }
  327. return ISOCHRONOUS_X;
  328. }
  329. return UNKNOWN;
  330. }
  331. static void append(char **buf, char *append, size_t *off, size_t *len)
  332. {
  333. int new = strlen(append);
  334. if ((new + *off) >= *len)
  335. {
  336. *len *= 2;
  337. *buf = realloc(*buf, *len);
  338. }
  339. strcpy(*buf + *off, append);
  340. *off += new;
  341. }
  342. 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)
  343. {
  344. char tmp[512];
  345. int err;
  346. err = libusb_set_configuration(handle, cd);
  347. if (err) {
  348. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
  349. (int)count, cd, err);
  350. append(buf, tmp, off, len);
  351. return false;
  352. }
  353. err = libusb_get_active_config_descriptor(dev, config);
  354. if (err) {
  355. sprintf(tmp, EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
  356. (int)count, cd, err);
  357. append(buf, tmp, off, len);
  358. return false;
  359. }
  360. sprintf(tmp, EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
  361. (int)count, cd, err);
  362. append(buf, tmp, off, len);
  363. return true;
  364. }
  365. static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
  366. {
  367. struct libusb_device_descriptor desc;
  368. uint8_t bus_number;
  369. uint8_t device_address;
  370. struct libusb_device_handle *handle;
  371. struct libusb_config_descriptor *config;
  372. const struct libusb_interface_descriptor *idesc;
  373. const struct libusb_endpoint_descriptor *epdesc;
  374. unsigned char man[STRBUFLEN+1];
  375. unsigned char prod[STRBUFLEN+1];
  376. unsigned char ser[STRBUFLEN+1];
  377. char tmp[512];
  378. int err, i, j, k;
  379. err = libusb_get_device_descriptor(dev, &desc);
  380. if (opt_usb_list_all && err) {
  381. sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
  382. (int)(++(*count)), err);
  383. append(buf, tmp, off, len);
  384. return;
  385. }
  386. bus_number = libusb_get_bus_number(dev);
  387. device_address = libusb_get_device_address(dev);
  388. if (!opt_usb_list_all) {
  389. bool known = false;
  390. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  391. if ((find_dev[i].idVendor == desc.idVendor) &&
  392. (find_dev[i].idProduct == desc.idProduct)) {
  393. known = true;
  394. break;
  395. }
  396. if (!known)
  397. return;
  398. }
  399. (*count)++;
  400. if (level == 0) {
  401. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
  402. (int)(*count), (int)bus_number, (int)device_address,
  403. desc.idVendor, desc.idProduct);
  404. } else {
  405. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
  406. "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
  407. "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
  408. "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
  409. "\tNumConfigurations: %d",
  410. (int)(*count), (int)bus_number, (int)device_address,
  411. (int)(desc.bLength), destype(desc.bDescriptorType),
  412. desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
  413. (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
  414. desc.idVendor, desc.idProduct, desc.bcdDevice,
  415. (int)(desc.bNumConfigurations));
  416. }
  417. append(buf, tmp, off, len);
  418. err = libusb_open(dev, &handle);
  419. if (err) {
  420. sprintf(tmp, EOL " ** dev %d: Failed to open, err %d", (int)(*count), err);
  421. append(buf, tmp, off, len);
  422. return;
  423. }
  424. err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
  425. if (err < 0)
  426. sprintf((char *)man, "** err(%d)", err);
  427. err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
  428. if (err < 0)
  429. sprintf((char *)prod, "** err(%d)", err);
  430. if (level == 0) {
  431. libusb_close(handle);
  432. sprintf(tmp, EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
  433. append(buf, tmp, off, len);
  434. return;
  435. }
  436. if (libusb_kernel_driver_active(handle, 0) == 1) {
  437. sprintf(tmp, EOL " * dev %d: kernel attached", (int)(*count));
  438. append(buf, tmp, off, len);
  439. }
  440. err = libusb_get_active_config_descriptor(dev, &config);
  441. if (err) {
  442. if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
  443. && !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
  444. libusb_close(handle);
  445. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d or %d",
  446. (int)(*count), 1, 0);
  447. append(buf, tmp, off, len);
  448. return;
  449. }
  450. }
  451. sprintf(tmp, EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
  452. "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
  453. "\tAttributes: %d" EOL "\tMaxPower: %d",
  454. (int)(*count), destype(config->bDescriptorType),
  455. (int)(config->bNumInterfaces), (int)(config->iConfiguration),
  456. (int)(config->bmAttributes), (int)(config->MaxPower));
  457. append(buf, tmp, off, len);
  458. for (i = 0; i < (int)(config->bNumInterfaces); i++) {
  459. for (j = 0; j < config->interface[i].num_altsetting; j++) {
  460. idesc = &(config->interface[i].altsetting[j]);
  461. sprintf(tmp, EOL " _dev %d: Interface Descriptor %d:" EOL
  462. "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
  463. "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
  464. "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
  465. (int)(*count), j, destype(idesc->bDescriptorType),
  466. (int)(idesc->bInterfaceNumber),
  467. (int)(idesc->bNumEndpoints),
  468. (int)(idesc->bInterfaceClass),
  469. (int)(idesc->bInterfaceSubClass),
  470. (int)(idesc->bInterfaceProtocol));
  471. append(buf, tmp, off, len);
  472. for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
  473. epdesc = &(idesc->endpoint[k]);
  474. sprintf(tmp, EOL " __dev %d: Interface %d Endpoint %d:" EOL
  475. "\tDescriptorType: %s" EOL
  476. "\tEndpointAddress: %s0x%x" EOL
  477. "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
  478. "\tInterval: %d" EOL "\tRefresh: %d",
  479. (int)(*count), (int)(idesc->bInterfaceNumber), k,
  480. destype(epdesc->bDescriptorType),
  481. epdir(epdesc->bEndpointAddress),
  482. (int)(epdesc->bEndpointAddress),
  483. epatt(epdesc->bmAttributes),
  484. epdesc->wMaxPacketSize,
  485. (int)(epdesc->bInterval),
  486. (int)(epdesc->bRefresh));
  487. append(buf, tmp, off, len);
  488. }
  489. }
  490. }
  491. libusb_free_config_descriptor(config);
  492. config = NULL;
  493. err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
  494. if (err < 0)
  495. sprintf((char *)ser, "** err(%d)", err);
  496. sprintf(tmp, EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
  497. "\tProduct: '%s'" EOL "\tSerial '%s'",
  498. (int)(*count), man, prod, ser);
  499. append(buf, tmp, off, len);
  500. libusb_close(handle);
  501. }
  502. // Function to dump all USB devices
  503. void usb_all(int level)
  504. {
  505. libusb_device **list;
  506. ssize_t count, i, j;
  507. char *buf;
  508. size_t len, off;
  509. count = libusb_get_device_list(NULL, &list);
  510. if (count < 0) {
  511. applog(LOG_ERR, "USB all: failed, err %d", (int)count);
  512. return;
  513. }
  514. if (count == 0)
  515. applog(LOG_WARNING, "USB all: found no devices");
  516. else
  517. {
  518. len = 10000;
  519. buf = malloc(len+1);
  520. sprintf(buf, "USB all: found %d devices", (int)count);
  521. off = strlen(buf);
  522. if (!opt_usb_list_all)
  523. append(&buf, " - listing known devices", &off, &len);
  524. j = -1;
  525. for (i = 0; i < count; i++)
  526. usb_full(&j, list[i], &buf, &off, &len, level);
  527. applog(LOG_WARNING, "%s", buf);
  528. free(buf);
  529. if (j == -1)
  530. applog(LOG_WARNING, "No known USB devices");
  531. else
  532. applog(LOG_WARNING, "%d %sUSB devices",
  533. (int)(++j), opt_usb_list_all ? BLANK : "known ");
  534. }
  535. libusb_free_device_list(list, 1);
  536. }
  537. static void cgusb_check_init()
  538. {
  539. mutex_lock(&cgusb_lock);
  540. if (stats_initialised == false) {
  541. // N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
  542. if (opt_usbdump >= 0) {
  543. libusb_set_debug(NULL, opt_usbdump);
  544. usb_all(opt_usbdump);
  545. }
  546. usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
  547. // use constants so the stat generation is very quick
  548. // and the association between number and name can't
  549. // be missalined easily
  550. usb_commands[C_REJECTED] = C_REJECTED_S;
  551. usb_commands[C_PING] = C_PING_S;
  552. usb_commands[C_CLEAR] = C_CLEAR_S;
  553. usb_commands[C_REQUESTVERSION] = C_REQUESTVERSION_S;
  554. usb_commands[C_GETVERSION] = C_GETVERSION_S;
  555. usb_commands[C_REQUESTFPGACOUNT] = C_REQUESTFPGACOUNT_S;
  556. usb_commands[C_GETFPGACOUNT] = C_GETFPGACOUNT_S;
  557. usb_commands[C_STARTPROGRAM] = C_STARTPROGRAM_S;
  558. usb_commands[C_STARTPROGRAMSTATUS] = C_STARTPROGRAMSTATUS_S;
  559. usb_commands[C_PROGRAM] = C_PROGRAM_S;
  560. usb_commands[C_PROGRAMSTATUS] = C_PROGRAMSTATUS_S;
  561. usb_commands[C_PROGRAMSTATUS2] = C_PROGRAMSTATUS2_S;
  562. usb_commands[C_FINALPROGRAMSTATUS] = C_FINALPROGRAMSTATUS_S;
  563. usb_commands[C_SETCLOCK] = C_SETCLOCK_S;
  564. usb_commands[C_REPLYSETCLOCK] = C_REPLYSETCLOCK_S;
  565. usb_commands[C_REQUESTUSERCODE] = C_REQUESTUSERCODE_S;
  566. usb_commands[C_GETUSERCODE] = C_GETUSERCODE_S;
  567. usb_commands[C_REQUESTTEMPERATURE] = C_REQUESTTEMPERATURE_S;
  568. usb_commands[C_GETTEMPERATURE] = C_GETTEMPERATURE_S;
  569. usb_commands[C_SENDWORK] = C_SENDWORK_S;
  570. usb_commands[C_SENDWORKSTATUS] = C_SENDWORKSTATUS_S;
  571. usb_commands[C_REQUESTWORKSTATUS] = C_REQUESTWORKSTATUS_S;
  572. usb_commands[C_GETWORKSTATUS] = C_GETWORKSTATUS_S;
  573. usb_commands[C_REQUESTIDENTIFY] = C_REQUESTIDENTIFY_S;
  574. usb_commands[C_GETIDENTIFY] = C_GETIDENTIFY_S;
  575. usb_commands[C_REQUESTFLASH] = C_REQUESTFLASH_S;
  576. usb_commands[C_REQUESTSENDWORK] = C_REQUESTSENDWORK_S;
  577. usb_commands[C_REQUESTSENDWORKSTATUS] = C_REQUESTSENDWORKSTATUS_S;
  578. usb_commands[C_RESET] = C_RESET_S;
  579. usb_commands[C_SETBAUD] = C_SETBAUD_S;
  580. usb_commands[C_SETDATA] = C_SETDATA_S;
  581. usb_commands[C_SETFLOW] = C_SETFLOW_S;
  582. usb_commands[C_SETMODEM] = C_SETMODEM_S;
  583. usb_commands[C_PURGERX] = C_PURGERX_S;
  584. usb_commands[C_PURGETX] = C_PURGETX_S;
  585. usb_commands[C_FLASHREPLY] = C_FLASHREPLY_S;
  586. stats_initialised = true;
  587. }
  588. mutex_unlock(&cgusb_lock);
  589. }
  590. #ifndef WIN32
  591. #include <errno.h>
  592. #include <unistd.h>
  593. #include <sys/types.h>
  594. #include <sys/ipc.h>
  595. #include <sys/sem.h>
  596. #include <sys/types.h>
  597. #include <sys/stat.h>
  598. #include <fcntl.h>
  599. union semun {
  600. int sem;
  601. struct semid_ds *seminfo;
  602. ushort *all;
  603. };
  604. #endif
  605. // Any errors should always be printed since they will rarely if ever occur
  606. // and thus it is best to always display them
  607. static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  608. {
  609. #ifdef WIN32
  610. struct cgpu_info *cgpu;
  611. HANDLE usbMutex;
  612. char name[64];
  613. DWORD res;
  614. int i;
  615. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  616. usbMutex = CreateMutex(NULL, FALSE, name);
  617. if (usbMutex == NULL) {
  618. applog(LOG_ERR,
  619. "MTX: %s USB failed to get '%s' err (%d)",
  620. drv->dname, name, GetLastError());
  621. return false;
  622. }
  623. res = WaitForSingleObject(usbMutex, 0);
  624. switch(res) {
  625. case WAIT_OBJECT_0:
  626. case WAIT_ABANDONED:
  627. // Am I using it already?
  628. for (i = 0; i < total_devices; i++) {
  629. cgpu = get_devices(i);
  630. if (cgpu->usbinfo.bus_number == bus_number &&
  631. cgpu->usbinfo.device_address == device_address &&
  632. cgpu->usbinfo.nodev == false) {
  633. if (ReleaseMutex(usbMutex)) {
  634. applog(LOG_WARNING,
  635. "MTX: %s USB can't get '%s' - device in use",
  636. drv->dname, name);
  637. goto fail;
  638. }
  639. applog(LOG_ERR,
  640. "MTX: %s USB can't get '%s' - device in use - failure (%d)",
  641. drv->dname, name, GetLastError());
  642. goto fail;
  643. }
  644. }
  645. return true;
  646. case WAIT_TIMEOUT:
  647. if (!hotplug_mode)
  648. applog(LOG_WARNING,
  649. "MTX: %s USB failed to get '%s' - device in use",
  650. drv->dname, name);
  651. goto fail;
  652. case WAIT_FAILED:
  653. applog(LOG_ERR,
  654. "MTX: %s USB failed to get '%s' err (%d)",
  655. drv->dname, name, GetLastError());
  656. goto fail;
  657. default:
  658. applog(LOG_ERR,
  659. "MTX: %s USB failed to get '%s' unknown reply (%d)",
  660. drv->dname, name, res);
  661. goto fail;
  662. }
  663. CloseHandle(usbMutex);
  664. return true;
  665. fail:
  666. CloseHandle(usbMutex);
  667. return false;
  668. #else
  669. struct semid_ds seminfo;
  670. union semun opt;
  671. char name[64];
  672. key_t key;
  673. int fd, sem, count;
  674. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  675. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  676. if (fd == -1) {
  677. applog(LOG_ERR,
  678. "SEM: %s USB open failed '%s' err (%d) %s",
  679. drv->dname, name, errno, strerror(errno));
  680. return false;
  681. }
  682. close(fd);
  683. key = ftok(name, 'K');
  684. sem = semget(key, 1, IPC_CREAT | IPC_EXCL | 438);
  685. if (sem < 0) {
  686. if (errno != EEXIST) {
  687. applog(LOG_ERR,
  688. "SEM: %s USB failed to get '%s' err (%d) %s",
  689. drv->dname, name, errno, strerror(errno));
  690. return false;
  691. }
  692. sem = semget(key, 1, 0);
  693. if (sem < 0) {
  694. applog(LOG_ERR,
  695. "SEM: %s USB failed to access '%s' err (%d) %s",
  696. drv->dname, name, errno, strerror(errno));
  697. return false;
  698. }
  699. opt.seminfo = &seminfo;
  700. count = 0;
  701. while (++count) {
  702. // Should NEVER take 100ms
  703. if (count > 99) {
  704. applog(LOG_ERR,
  705. "SEM: %s USB timeout waiting for (%d) '%s'",
  706. drv->dname, sem, name);
  707. return false;
  708. }
  709. if (semctl(sem, 0, IPC_STAT, opt) == -1) {
  710. applog(LOG_ERR,
  711. "SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
  712. drv->dname, sem, name, count, errno, strerror(errno));
  713. return false;
  714. }
  715. if (opt.seminfo->sem_otime != 0)
  716. break;
  717. nmsleep(1);
  718. }
  719. }
  720. struct sembuf sops[] = {
  721. { 0, 0, IPC_NOWAIT | SEM_UNDO },
  722. { 0, 1, IPC_NOWAIT | SEM_UNDO }
  723. };
  724. if (semop(sem, sops, 2)) {
  725. if (errno == EAGAIN) {
  726. if (!hotplug_mode)
  727. applog(LOG_WARNING,
  728. "SEM: %s USB failed to get (%d) '%s' - device in use",
  729. drv->dname, sem, name);
  730. } else {
  731. applog(LOG_DEBUG,
  732. "SEM: %s USB failed to get (%d) '%s' err (%d) %s",
  733. drv->dname, sem, name, errno, strerror(errno));
  734. }
  735. return false;
  736. }
  737. return true;
  738. #endif
  739. }
  740. static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
  741. {
  742. return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  743. }
  744. // Any errors should always be printed since they will rarely if ever occur
  745. // and thus it is best to always display them
  746. static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  747. {
  748. #ifdef WIN32
  749. HANDLE usbMutex;
  750. char name[64];
  751. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  752. usbMutex = CreateMutex(NULL, FALSE, name);
  753. if (usbMutex == NULL) {
  754. applog(LOG_ERR,
  755. "MTX: %s USB failed to get '%s' for release err (%d)",
  756. drv->dname, name, GetLastError());
  757. return;
  758. }
  759. if (!ReleaseMutex(usbMutex))
  760. applog(LOG_ERR,
  761. "MTX: %s USB failed to release '%s' err (%d)",
  762. drv->dname, name, GetLastError());
  763. CloseHandle(usbMutex);
  764. return;
  765. #else
  766. char name[64];
  767. key_t key;
  768. int fd, sem;
  769. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  770. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  771. if (fd == -1) {
  772. applog(LOG_ERR,
  773. "SEM: %s USB open failed '%s' for release err (%d) %s",
  774. drv->dname, name, errno, strerror(errno));
  775. return;
  776. }
  777. close(fd);
  778. key = ftok(name, 'K');
  779. sem = semget(key, 1, 0);
  780. if (sem < 0) {
  781. applog(LOG_ERR,
  782. "SEM: %s USB failed to get '%s' for release err (%d) %s",
  783. drv->dname, name, errno, strerror(errno));
  784. return;
  785. }
  786. struct sembuf sops[] = {
  787. { 0, -1, SEM_UNDO }
  788. };
  789. // Allow a 10ms timeout
  790. // exceeding this timeout means it would probably never succeed anyway
  791. struct timespec timeout = { 0, 10000000 };
  792. // Wait forever since we shoud be the one who has it
  793. if (semtimedop(sem, sops, 1, &timeout)) {
  794. applog(LOG_ERR,
  795. "SEM: %d USB failed to release '%s' err (%d) %s",
  796. drv->dname, name, errno, strerror(errno));
  797. }
  798. return;
  799. #endif
  800. }
  801. static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
  802. {
  803. cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  804. }
  805. static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
  806. {
  807. if (cgusb->serial_string && cgusb->serial_string != BLANK)
  808. free(cgusb->serial_string);
  809. if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
  810. free(cgusb->manuf_string);
  811. if (cgusb->prod_string && cgusb->prod_string != BLANK)
  812. free(cgusb->prod_string);
  813. free(cgusb->descriptor);
  814. free(cgusb->found);
  815. free(cgusb);
  816. return NULL;
  817. }
  818. void usb_uninit(struct cgpu_info *cgpu)
  819. {
  820. // May have happened already during a failed initialisation
  821. // if release_cgpu() was called due to a USB NODEV(err)
  822. if (!cgpu->usbdev)
  823. return;
  824. libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
  825. libusb_close(cgpu->usbdev->handle);
  826. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  827. }
  828. static void release_cgpu(struct cgpu_info *cgpu)
  829. {
  830. struct cg_usb_device *cgusb = cgpu->usbdev;
  831. struct cgpu_info *lookcgpu;
  832. int i;
  833. // It has already been done
  834. if (cgpu->usbinfo.nodev)
  835. return;
  836. total_count--;
  837. drv_count[cgpu->drv->drv_id].count--;
  838. cgpu->usbinfo.nodev = true;
  839. cgpu->usbinfo.nodev_count++;
  840. gettimeofday(&(cgpu->usbinfo.last_nodev), NULL);
  841. // Any devices sharing the same USB device should be marked also
  842. // Currently only MMQ shares a USB device
  843. for (i = 0; i < total_devices; i++) {
  844. lookcgpu = get_devices(i);
  845. if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
  846. total_count--;
  847. drv_count[lookcgpu->drv->drv_id].count--;
  848. lookcgpu->usbinfo.nodev = true;
  849. lookcgpu->usbinfo.nodev_count++;
  850. memcpy(&(lookcgpu->usbinfo.last_nodev),
  851. &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
  852. lookcgpu->usbdev = NULL;
  853. }
  854. }
  855. usb_uninit(cgpu);
  856. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  857. }
  858. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  859. {
  860. struct cg_usb_device *cgusb = NULL;
  861. struct libusb_config_descriptor *config = NULL;
  862. const struct libusb_interface_descriptor *idesc;
  863. const struct libusb_endpoint_descriptor *epdesc;
  864. unsigned char strbuf[STRBUFLEN+1];
  865. char devstr[STRBUFLEN+1];
  866. int err, i, j, k;
  867. cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
  868. cgpu->usbinfo.device_address = libusb_get_device_address(dev);
  869. sprintf(devstr, "- %s device %d:%d", found->name,
  870. cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  871. cgusb = calloc(1, sizeof(*cgusb));
  872. cgusb->found = found;
  873. cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
  874. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  875. if (err) {
  876. applog(LOG_DEBUG,
  877. "USB init failed to get descriptor, err %d %s",
  878. err, devstr);
  879. goto dame;
  880. }
  881. err = libusb_open(dev, &(cgusb->handle));
  882. if (err) {
  883. switch (err) {
  884. case LIBUSB_ERROR_ACCESS:
  885. applog(LOG_ERR,
  886. "USB init open device failed, err %d, "
  887. "you dont have priviledge to access %s",
  888. err, devstr);
  889. break;
  890. #ifdef WIN32
  891. // Windows specific message
  892. case LIBUSB_ERROR_NOT_SUPPORTED:
  893. applog(LOG_ERR,
  894. "USB init, open device failed, err %d, "
  895. "you need to install a Windows USB driver for %s",
  896. err, devstr);
  897. break;
  898. #endif
  899. default:
  900. applog(LOG_DEBUG,
  901. "USB init, open failed, err %d %s",
  902. err, devstr);
  903. }
  904. goto dame;
  905. }
  906. #ifndef WIN32
  907. if (libusb_kernel_driver_active(cgusb->handle, found->kernel) == 1) {
  908. applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
  909. err = libusb_detach_kernel_driver(cgusb->handle, found->kernel);
  910. if (err == 0) {
  911. applog(LOG_DEBUG,
  912. "USB init, kernel detached successfully %s",
  913. devstr);
  914. } else {
  915. applog(LOG_WARNING,
  916. "USB init, kernel detach failed, err %d in use? %s",
  917. err, devstr);
  918. goto cldame;
  919. }
  920. }
  921. #endif
  922. err = libusb_set_configuration(cgusb->handle, found->config);
  923. if (err) {
  924. switch(err) {
  925. case LIBUSB_ERROR_BUSY:
  926. applog(LOG_WARNING,
  927. "USB init, set config %d in use %s",
  928. found->config, devstr);
  929. break;
  930. default:
  931. applog(LOG_DEBUG,
  932. "USB init, failed to set config to %d, err %d %s",
  933. found->config, err, devstr);
  934. }
  935. goto cldame;
  936. }
  937. err = libusb_get_active_config_descriptor(dev, &config);
  938. if (err) {
  939. applog(LOG_DEBUG,
  940. "USB init, failed to get config descriptor, err %d %s",
  941. err, devstr);
  942. goto cldame;
  943. }
  944. if ((int)(config->bNumInterfaces) <= found->interface)
  945. goto cldame;
  946. for (i = 0; i < found->epcount; i++)
  947. found->eps[i].found = false;
  948. for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
  949. idesc = &(config->interface[found->interface].altsetting[i]);
  950. for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
  951. epdesc = &(idesc->endpoint[j]);
  952. for (k = 0; k < found->epcount; k++) {
  953. if (!found->eps[k].found) {
  954. if (epdesc->bmAttributes == found->eps[k].att
  955. && epdesc->wMaxPacketSize >= found->eps[k].size
  956. && epdesc->bEndpointAddress == found->eps[k].ep) {
  957. found->eps[k].found = true;
  958. break;
  959. }
  960. }
  961. }
  962. }
  963. }
  964. for (i = 0; i < found->epcount; i++)
  965. if (found->eps[i].found == false)
  966. goto cldame;
  967. err = libusb_claim_interface(cgusb->handle, found->interface);
  968. if (err) {
  969. switch(err) {
  970. case LIBUSB_ERROR_BUSY:
  971. applog(LOG_WARNING,
  972. "USB init, claim interface %d in use %s",
  973. found->interface, devstr);
  974. break;
  975. default:
  976. applog(LOG_DEBUG,
  977. "USB init, claim interface %d failed, err %d %s",
  978. found->interface, err, devstr);
  979. }
  980. goto cldame;
  981. }
  982. cgusb->usbver = cgusb->descriptor->bcdUSB;
  983. // TODO: allow this with the right version of the libusb include and running library
  984. // cgusb->speed = libusb_get_device_speed(dev);
  985. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  986. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  987. if (err > 0)
  988. cgusb->prod_string = strdup((char *)strbuf);
  989. else
  990. cgusb->prod_string = (char *)BLANK;
  991. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  992. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  993. if (err > 0)
  994. cgusb->manuf_string = strdup((char *)strbuf);
  995. else
  996. cgusb->manuf_string = (char *)BLANK;
  997. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  998. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  999. if (err > 0)
  1000. cgusb->serial_string = strdup((char *)strbuf);
  1001. else
  1002. cgusb->serial_string = (char *)BLANK;
  1003. // TODO: ?
  1004. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  1005. // cgusb->interfaceVersion
  1006. applog(LOG_DEBUG,
  1007. "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
  1008. devstr, cgusb->usbver, cgusb->prod_string,
  1009. cgusb->manuf_string, cgusb->serial_string);
  1010. cgpu->usbdev = cgusb;
  1011. libusb_free_config_descriptor(config);
  1012. // Allow a name change based on the idVendor+idProduct
  1013. // N.B. must be done before calling add_cgpu()
  1014. if (strcmp(cgpu->drv->name, found->name)) {
  1015. if (!cgpu->drv->copy)
  1016. cgpu->drv = copy_drv(cgpu->drv);
  1017. cgpu->drv->name = (char *)(found->name);
  1018. }
  1019. return true;
  1020. cldame:
  1021. libusb_close(cgusb->handle);
  1022. dame:
  1023. if (config)
  1024. libusb_free_config_descriptor(config);
  1025. cgusb = free_cgusb(cgusb);
  1026. return false;
  1027. }
  1028. static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
  1029. {
  1030. struct libusb_device_descriptor desc;
  1031. int bus_number, device_address;
  1032. int err, i;
  1033. bool ok;
  1034. err = libusb_get_device_descriptor(dev, &desc);
  1035. if (err) {
  1036. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  1037. return false;
  1038. }
  1039. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  1040. applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
  1041. drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  1042. return false;
  1043. }
  1044. if (busdev_count > 0) {
  1045. bus_number = (int)libusb_get_bus_number(dev);
  1046. device_address = (int)libusb_get_device_address(dev);
  1047. ok = false;
  1048. for (i = 0; i < busdev_count; i++) {
  1049. if (bus_number == busdev[i].bus_number) {
  1050. if (busdev[i].device_address == -1 ||
  1051. device_address == busdev[i].device_address) {
  1052. ok = true;
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. if (!ok) {
  1058. applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
  1059. drv->name, look->name, look->idVendor, look->idProduct,
  1060. bus_number, device_address);
  1061. return false;
  1062. }
  1063. }
  1064. applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
  1065. drv->name, look->name, look->idVendor, look->idProduct);
  1066. return true;
  1067. }
  1068. static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
  1069. {
  1070. struct usb_find_devices *found;
  1071. int i;
  1072. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  1073. if (find_dev[i].drv == drvnum) {
  1074. if (usb_check_device(drv, dev, &(find_dev[i]))) {
  1075. found = malloc(sizeof(*found));
  1076. memcpy(found, &(find_dev[i]), sizeof(*found));
  1077. return found;
  1078. }
  1079. }
  1080. return NULL;
  1081. }
  1082. static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
  1083. {
  1084. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1085. applog(LOG_DEBUG,
  1086. "USB scan devices3: %s limit %d reached",
  1087. drv->dname, drv_count[drv->drv_id].limit);
  1088. return NULL;
  1089. }
  1090. #ifdef USE_BFLSC
  1091. if (drv->drv_id == DRIVER_BFLSC)
  1092. return usb_check_each(DRV_BFLSC, drv, dev);
  1093. #endif
  1094. #ifdef USE_BITFORCE
  1095. if (drv->drv_id == DRIVER_BITFORCE)
  1096. return usb_check_each(DRV_BITFORCE, drv, dev);
  1097. #endif
  1098. #ifdef USE_ICARUS
  1099. if (drv->drv_id == DRIVER_ICARUS)
  1100. return usb_check_each(DRV_ICARUS, drv, dev);
  1101. #endif
  1102. #ifdef USE_MODMINER
  1103. if (drv->drv_id == DRIVER_MODMINER)
  1104. return usb_check_each(DRV_MODMINER, drv, dev);
  1105. #endif
  1106. return NULL;
  1107. }
  1108. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
  1109. {
  1110. libusb_device **list;
  1111. ssize_t count, i;
  1112. struct usb_find_devices *found;
  1113. applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
  1114. if (total_count >= total_limit) {
  1115. applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
  1116. return;
  1117. }
  1118. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1119. applog(LOG_DEBUG,
  1120. "USB scan devices: %s limit %d reached",
  1121. drv->dname, drv_count[drv->drv_id].limit);
  1122. return;
  1123. }
  1124. count = libusb_get_device_list(NULL, &list);
  1125. if (count < 0) {
  1126. applog(LOG_DEBUG, "USB scan devices: failed, err %d", count);
  1127. return;
  1128. }
  1129. if (count == 0)
  1130. applog(LOG_DEBUG, "USB scan devices: found no devices");
  1131. for (i = 0; i < count; i++) {
  1132. if (total_count >= total_limit) {
  1133. applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
  1134. break;
  1135. }
  1136. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1137. applog(LOG_DEBUG,
  1138. "USB scan devices2: %s limit %d reached",
  1139. drv->dname, drv_count[drv->drv_id].limit);
  1140. break;
  1141. }
  1142. found = usb_check(drv, list[i]);
  1143. if (found != NULL) {
  1144. if (cgminer_usb_lock(drv, list[i]) == false)
  1145. free(found);
  1146. else {
  1147. if (!device_detect(list[i], found))
  1148. cgminer_usb_unlock(drv, list[i]);
  1149. else {
  1150. total_count++;
  1151. drv_count[drv->drv_id].count++;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. libusb_free_device_list(list, 1);
  1157. }
  1158. // Set this to 0 to remove stats processing
  1159. #define DO_USB_STATS 1
  1160. #if DO_USB_STATS
  1161. #define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
  1162. #define STATS_TIMEVAL(tv) gettimeofday(tv, NULL)
  1163. #else
  1164. #define USB_STATS(sgpu, sta, fin, err, cmd, seq)
  1165. #define STATS_TIMEVAL(tv)
  1166. #endif
  1167. // The stat data can be spurious due to not locking it before copying it -
  1168. // however that would require the stat() function to also lock and release
  1169. // a mutex every time a usb read or write is called which would slow
  1170. // things down more
  1171. struct api_data *api_usb_stats(__maybe_unused int *count)
  1172. {
  1173. #if DO_USB_STATS
  1174. struct cg_usb_stats_details *details;
  1175. struct cg_usb_stats *sta;
  1176. struct api_data *root = NULL;
  1177. int device;
  1178. int cmdseq;
  1179. if (next_stat == 0)
  1180. return NULL;
  1181. while (*count < next_stat * C_MAX * 2) {
  1182. device = *count / (C_MAX * 2);
  1183. cmdseq = *count % (C_MAX * 2);
  1184. (*count)++;
  1185. sta = &(usb_stats[device]);
  1186. details = &(sta->details[cmdseq]);
  1187. // Only show stats that have results
  1188. if (details->item[CMD_CMD].count == 0 &&
  1189. details->item[CMD_TIMEOUT].count == 0 &&
  1190. details->item[CMD_ERROR].count == 0)
  1191. continue;
  1192. root = api_add_string(root, "Name", sta->name, false);
  1193. root = api_add_int(root, "ID", &(sta->device_id), false);
  1194. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  1195. root = api_add_int(root, "Seq", &(details->seq), true);
  1196. root = api_add_uint64(root, "Count",
  1197. &(details->item[CMD_CMD].count), true);
  1198. root = api_add_double(root, "Total Delay",
  1199. &(details->item[CMD_CMD].total_delay), true);
  1200. root = api_add_double(root, "Min Delay",
  1201. &(details->item[CMD_CMD].min_delay), true);
  1202. root = api_add_double(root, "Max Delay",
  1203. &(details->item[CMD_CMD].max_delay), true);
  1204. root = api_add_uint64(root, "Timeout Count",
  1205. &(details->item[CMD_TIMEOUT].count), true);
  1206. root = api_add_double(root, "Timeout Total Delay",
  1207. &(details->item[CMD_TIMEOUT].total_delay), true);
  1208. root = api_add_double(root, "Timeout Min Delay",
  1209. &(details->item[CMD_TIMEOUT].min_delay), true);
  1210. root = api_add_double(root, "Timeout Max Delay",
  1211. &(details->item[CMD_TIMEOUT].max_delay), true);
  1212. root = api_add_uint64(root, "Error Count",
  1213. &(details->item[CMD_ERROR].count), true);
  1214. root = api_add_double(root, "Error Total Delay",
  1215. &(details->item[CMD_ERROR].total_delay), true);
  1216. root = api_add_double(root, "Error Min Delay",
  1217. &(details->item[CMD_ERROR].min_delay), true);
  1218. root = api_add_double(root, "Error Max Delay",
  1219. &(details->item[CMD_ERROR].max_delay), true);
  1220. root = api_add_timeval(root, "First Command",
  1221. &(details->item[CMD_CMD].first), true);
  1222. root = api_add_timeval(root, "Last Command",
  1223. &(details->item[CMD_CMD].last), true);
  1224. root = api_add_timeval(root, "First Timeout",
  1225. &(details->item[CMD_TIMEOUT].first), true);
  1226. root = api_add_timeval(root, "Last Timeout",
  1227. &(details->item[CMD_TIMEOUT].last), true);
  1228. root = api_add_timeval(root, "First Error",
  1229. &(details->item[CMD_ERROR].first), true);
  1230. root = api_add_timeval(root, "Last Error",
  1231. &(details->item[CMD_ERROR].last), true);
  1232. return root;
  1233. }
  1234. #endif
  1235. return NULL;
  1236. }
  1237. #if DO_USB_STATS
  1238. static void newstats(struct cgpu_info *cgpu)
  1239. {
  1240. int i;
  1241. mutex_lock(&cgusb_lock);
  1242. cgpu->usbinfo.usbstat = ++next_stat;
  1243. mutex_unlock(&cgusb_lock);
  1244. usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
  1245. usb_stats[next_stat-1].name = cgpu->drv->name;
  1246. usb_stats[next_stat-1].device_id = -1;
  1247. usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
  1248. for (i = 1; i < C_MAX * 2; i += 2)
  1249. usb_stats[next_stat-1].details[i].seq = 1;
  1250. }
  1251. #endif
  1252. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  1253. {
  1254. #if DO_USB_STATS
  1255. if (cgpu->usbinfo.usbstat < 1)
  1256. newstats(cgpu);
  1257. // we don't know the device_id until after add_cgpu()
  1258. usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
  1259. #endif
  1260. }
  1261. #if DO_USB_STATS
  1262. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
  1263. {
  1264. struct cg_usb_stats_details *details;
  1265. double diff;
  1266. int item;
  1267. if (cgpu->usbinfo.usbstat < 1)
  1268. newstats(cgpu);
  1269. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
  1270. diff = tdiff(tv_finish, tv_start);
  1271. switch (err) {
  1272. case LIBUSB_SUCCESS:
  1273. item = CMD_CMD;
  1274. break;
  1275. case LIBUSB_ERROR_TIMEOUT:
  1276. item = CMD_TIMEOUT;
  1277. break;
  1278. default:
  1279. item = CMD_ERROR;
  1280. break;
  1281. }
  1282. if (details->item[item].count == 0) {
  1283. details->item[item].min_delay = diff;
  1284. memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  1285. } else if (diff < details->item[item].min_delay)
  1286. details->item[item].min_delay = diff;
  1287. if (diff > details->item[item].max_delay)
  1288. details->item[item].max_delay = diff;
  1289. details->item[item].total_delay += diff;
  1290. memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
  1291. details->item[item].count++;
  1292. }
  1293. static void rejected_inc(struct cgpu_info *cgpu)
  1294. {
  1295. struct cg_usb_stats_details *details;
  1296. int item = CMD_ERROR;
  1297. if (cgpu->usbinfo.usbstat < 1)
  1298. newstats(cgpu);
  1299. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
  1300. details->item[item].count++;
  1301. }
  1302. #endif
  1303. int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, int eol, enum usb_cmds cmd, bool ftdi)
  1304. {
  1305. struct cg_usb_device *usbdev = cgpu->usbdev;
  1306. #if DO_USB_STATS
  1307. struct timeval tv_start;
  1308. #endif
  1309. struct timeval read_start, tv_finish;
  1310. unsigned int initial_timeout;
  1311. double max, done;
  1312. int err, got, tot, i;
  1313. bool first = true;
  1314. if (cgpu->usbinfo.nodev) {
  1315. *buf = '\0';
  1316. *processed = 0;
  1317. #if DO_USB_STATS
  1318. rejected_inc(cgpu);
  1319. #endif
  1320. return LIBUSB_ERROR_NO_DEVICE;
  1321. }
  1322. if (timeout == DEVTIMEOUT)
  1323. timeout = usbdev->found->timeout;
  1324. if (eol == -1) {
  1325. got = 0;
  1326. STATS_TIMEVAL(&tv_start);
  1327. err = libusb_bulk_transfer(usbdev->handle,
  1328. usbdev->found->eps[ep].ep,
  1329. (unsigned char *)buf,
  1330. bufsiz, &got, timeout);
  1331. STATS_TIMEVAL(&tv_finish);
  1332. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1333. if (ftdi) {
  1334. // first 2 bytes returned are an FTDI status
  1335. if (got > 2) {
  1336. got -= 2;
  1337. memmove(buf, buf+2, got+1);
  1338. } else {
  1339. got = 0;
  1340. *buf = '\0';
  1341. }
  1342. }
  1343. *processed = got;
  1344. if (NODEV(err))
  1345. release_cgpu(cgpu);
  1346. return err;
  1347. }
  1348. tot = 0;
  1349. err = LIBUSB_SUCCESS;
  1350. initial_timeout = timeout;
  1351. max = ((double)timeout) / 1000.0;
  1352. gettimeofday(&read_start, NULL);
  1353. while (bufsiz) {
  1354. got = 0;
  1355. STATS_TIMEVAL(&tv_start);
  1356. err = libusb_bulk_transfer(usbdev->handle,
  1357. usbdev->found->eps[ep].ep,
  1358. (unsigned char *)buf,
  1359. bufsiz, &got, timeout);
  1360. gettimeofday(&tv_finish, NULL);
  1361. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1362. if (ftdi) {
  1363. // first 2 bytes returned are an FTDI status
  1364. if (got > 2) {
  1365. got -= 2;
  1366. memmove(buf, buf+2, got+1);
  1367. } else {
  1368. got = 0;
  1369. *buf = '\0';
  1370. }
  1371. }
  1372. tot += got;
  1373. if (err)
  1374. break;
  1375. // WARNING - this will return data past EOL ('if' there is extra data)
  1376. for (i = 0; i < got; i++)
  1377. if (buf[i] == eol)
  1378. goto goteol;
  1379. buf += got;
  1380. bufsiz -= got;
  1381. first = false;
  1382. done = tdiff(&tv_finish, &read_start);
  1383. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1384. if (unlikely(done >= max))
  1385. break;
  1386. timeout = initial_timeout - (done * 1000);
  1387. }
  1388. goteol:
  1389. *processed = tot;
  1390. if (NODEV(err))
  1391. release_cgpu(cgpu);
  1392. return err;
  1393. }
  1394. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
  1395. {
  1396. struct cg_usb_device *usbdev = cgpu->usbdev;
  1397. #if DO_USB_STATS
  1398. struct timeval tv_start, tv_finish;
  1399. #endif
  1400. int err, sent;
  1401. if (cgpu->usbinfo.nodev) {
  1402. *processed = 0;
  1403. #if DO_USB_STATS
  1404. rejected_inc(cgpu);
  1405. #endif
  1406. return LIBUSB_ERROR_NO_DEVICE;
  1407. }
  1408. sent = 0;
  1409. STATS_TIMEVAL(&tv_start);
  1410. err = libusb_bulk_transfer(usbdev->handle,
  1411. usbdev->found->eps[ep].ep,
  1412. (unsigned char *)buf,
  1413. bufsiz, &sent,
  1414. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1415. STATS_TIMEVAL(&tv_finish);
  1416. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1417. *processed = sent;
  1418. if (NODEV(err))
  1419. release_cgpu(cgpu);
  1420. return err;
  1421. }
  1422. 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)
  1423. {
  1424. struct cg_usb_device *usbdev = cgpu->usbdev;
  1425. #if DO_USB_STATS
  1426. struct timeval tv_start, tv_finish;
  1427. #endif
  1428. int err;
  1429. if (cgpu->usbinfo.nodev) {
  1430. #if DO_USB_STATS
  1431. rejected_inc(cgpu);
  1432. #endif
  1433. return LIBUSB_ERROR_NO_DEVICE;
  1434. }
  1435. STATS_TIMEVAL(&tv_start);
  1436. err = libusb_control_transfer(usbdev->handle, request_type,
  1437. bRequest, wValue, wIndex, NULL, 0,
  1438. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1439. STATS_TIMEVAL(&tv_finish);
  1440. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1441. if (NODEV(err))
  1442. release_cgpu(cgpu);
  1443. return err;
  1444. }
  1445. void usb_cleanup()
  1446. {
  1447. struct cgpu_info *cgpu;
  1448. int i;
  1449. hotplug_time = 0;
  1450. nmsleep(10);
  1451. for (i = 0; i < total_devices; i++) {
  1452. cgpu = devices[i];
  1453. switch (cgpu->drv->drv_id) {
  1454. case DRIVER_BFLSC:
  1455. case DRIVER_BITFORCE:
  1456. case DRIVER_MODMINER:
  1457. release_cgpu(cgpu);
  1458. break;
  1459. default:
  1460. break;
  1461. }
  1462. }
  1463. }
  1464. void usb_initialise()
  1465. {
  1466. char *fre, *ptr, *comma, *colon;
  1467. int bus, dev, lim, i;
  1468. bool found;
  1469. for (i = 0; i < DRIVER_MAX; i++) {
  1470. drv_count[i].count = 0;
  1471. drv_count[i].limit = 999999;
  1472. }
  1473. cgusb_check_init();
  1474. if (opt_usb_select && *opt_usb_select) {
  1475. // Absolute device limit
  1476. if (*opt_usb_select == ':') {
  1477. total_limit = atoi(opt_usb_select+1);
  1478. if (total_limit < 0)
  1479. quit(1, "Invalid --usb total limit");
  1480. // Comma list of bus:dev devices to match
  1481. } else if (isdigit(*opt_usb_select)) {
  1482. fre = ptr = strdup(opt_usb_select);
  1483. do {
  1484. comma = strchr(ptr, ',');
  1485. if (comma)
  1486. *(comma++) = '\0';
  1487. colon = strchr(ptr, ':');
  1488. if (!colon)
  1489. quit(1, "Invalid --usb bus:dev missing ':'");
  1490. *(colon++) = '\0';
  1491. if (!isdigit(*ptr))
  1492. quit(1, "Invalid --usb bus:dev - bus must be a number");
  1493. if (!isdigit(*colon) && *colon != '*')
  1494. quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
  1495. bus = atoi(ptr);
  1496. if (bus <= 0)
  1497. quit(1, "Invalid --usb bus:dev - bus must be > 0");
  1498. if (!colon == '*')
  1499. dev = -1;
  1500. else {
  1501. dev = atoi(colon);
  1502. if (dev <= 0)
  1503. quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
  1504. }
  1505. busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
  1506. busdev[busdev_count-1].bus_number = bus;
  1507. busdev[busdev_count-1].device_address = dev;
  1508. ptr = comma;
  1509. } while (ptr);
  1510. free(fre);
  1511. // Comma list of DRV:limit
  1512. } else {
  1513. fre = ptr = strdup(opt_usb_select);
  1514. do {
  1515. comma = strchr(ptr, ',');
  1516. if (comma)
  1517. *(comma++) = '\0';
  1518. colon = strchr(ptr, ':');
  1519. if (!colon)
  1520. quit(1, "Invalid --usb DRV:limit missing ':'");
  1521. *(colon++) = '\0';
  1522. if (!isdigit(*colon))
  1523. quit(1, "Invalid --usb DRV:limit - limit must be a number");
  1524. lim = atoi(colon);
  1525. if (lim < 0)
  1526. quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
  1527. found = false;
  1528. #ifdef USE_BFLSC
  1529. if (strcasecmp(ptr, bflsc_drv.name) == 0) {
  1530. drv_count[bflsrc_drv.drv_id].limit = lim;
  1531. found = true;
  1532. }
  1533. #endif
  1534. #ifdef USE_BITFORCE
  1535. if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
  1536. drv_count[bitforce_drv.drv_id].limit = lim;
  1537. found = true;
  1538. }
  1539. #endif
  1540. #ifdef USE_MODMINER
  1541. if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
  1542. drv_count[modminer_drv.drv_id].limit = lim;
  1543. found = true;
  1544. }
  1545. #endif
  1546. if (!found)
  1547. quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
  1548. ptr = comma;
  1549. } while (ptr);
  1550. free(fre);
  1551. }
  1552. }
  1553. }