usbutils.c 49 KB

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