usbutils.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  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 (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 (level == 0) {
  389. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
  390. (int)count, (int)bus_number, (int)device_address,
  391. desc.idVendor, desc.idProduct);
  392. } else {
  393. sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
  394. "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
  395. "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
  396. "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
  397. "\tNumConfigurations: %d",
  398. (int)count, (int)bus_number, (int)device_address,
  399. (int)(desc.bLength), destype(desc.bDescriptorType),
  400. desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
  401. (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
  402. desc.idVendor, desc.idProduct, desc.bcdDevice,
  403. (int)(desc.bNumConfigurations));
  404. }
  405. append(buf, tmp, off, len);
  406. err = libusb_open(dev, &handle);
  407. if (err) {
  408. sprintf(tmp, EOL " ** dev %d: Failed to open, err %d", (int)count, err);
  409. append(buf, tmp, off, len);
  410. return;
  411. }
  412. err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
  413. if (err < 0)
  414. sprintf((char *)man, "** err(%d)", err);
  415. err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
  416. if (err < 0)
  417. sprintf((char *)prod, "** err(%d)", err);
  418. if (level == 0) {
  419. libusb_close(handle);
  420. sprintf(tmp, EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
  421. append(buf, tmp, off, len);
  422. return;
  423. }
  424. if (libusb_kernel_driver_active(handle, 0) == 1) {
  425. sprintf(tmp, EOL " * dev %d: kernel attached", (int)count);
  426. append(buf, tmp, off, len);
  427. }
  428. err = libusb_get_active_config_descriptor(dev, &config);
  429. if (err) {
  430. if (!setgetdes(count, dev, handle, &config, 1, buf, off, len)
  431. && !setgetdes(count, dev, handle, &config, 0, buf, off, len)) {
  432. libusb_close(handle);
  433. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d or %d",
  434. (int)count, 1, 0);
  435. append(buf, tmp, off, len);
  436. return;
  437. }
  438. }
  439. sprintf(tmp, EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
  440. "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
  441. "\tAttributes: %d" EOL "\tMaxPower: %d",
  442. (int)count, destype(config->bDescriptorType),
  443. (int)(config->bNumInterfaces), (int)(config->iConfiguration),
  444. (int)(config->bmAttributes), (int)(config->MaxPower));
  445. append(buf, tmp, off, len);
  446. for (i = 0; i < (int)(config->bNumInterfaces); i++) {
  447. for (j = 0; j < config->interface[i].num_altsetting; j++) {
  448. idesc = &(config->interface[i].altsetting[j]);
  449. sprintf(tmp, EOL " _dev %d: Interface Descriptor %d:" EOL
  450. "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
  451. "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
  452. "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
  453. (int)count, j, destype(idesc->bDescriptorType),
  454. (int)(idesc->bInterfaceNumber),
  455. (int)(idesc->bNumEndpoints),
  456. (int)(idesc->bInterfaceClass),
  457. (int)(idesc->bInterfaceSubClass),
  458. (int)(idesc->bInterfaceProtocol));
  459. append(buf, tmp, off, len);
  460. for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
  461. epdesc = &(idesc->endpoint[k]);
  462. sprintf(tmp, EOL " __dev %d: Interface %d Endpoint %d:" EOL
  463. "\tDescriptorType: %s" EOL
  464. "\tEndpointAddress: %s0x%x" EOL
  465. "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
  466. "\tInterval: %d" EOL "\tRefresh: %d",
  467. (int)count, (int)(idesc->bInterfaceNumber), k,
  468. destype(epdesc->bDescriptorType),
  469. epdir(epdesc->bEndpointAddress),
  470. (int)(epdesc->bEndpointAddress),
  471. epatt(epdesc->bmAttributes),
  472. epdesc->wMaxPacketSize,
  473. (int)(epdesc->bInterval),
  474. (int)(epdesc->bRefresh));
  475. append(buf, tmp, off, len);
  476. }
  477. }
  478. }
  479. libusb_free_config_descriptor(config);
  480. config = NULL;
  481. err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
  482. if (err < 0)
  483. sprintf((char *)ser, "** err(%d)", err);
  484. sprintf(tmp, EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
  485. "\tProduct: '%s'" EOL "\tSerial '%s'",
  486. (int)count, man, prod, ser);
  487. append(buf, tmp, off, len);
  488. libusb_close(handle);
  489. }
  490. // Function to dump all USB devices
  491. void usb_all(int level)
  492. {
  493. libusb_device **list;
  494. ssize_t count, i;
  495. char *buf;
  496. size_t len, off;
  497. count = libusb_get_device_list(NULL, &list);
  498. if (count < 0) {
  499. applog(LOG_ERR, "USB all: failed, err %d", (int)count);
  500. return;
  501. }
  502. if (count == 0)
  503. applog(LOG_WARNING, "USB all: found no devices");
  504. else
  505. {
  506. len = 10000;
  507. buf = malloc(len+1);
  508. sprintf(buf, "USB all: found %d devices", (int)count);
  509. off = strlen(buf);
  510. for (i = 0; i < count; i++)
  511. usb_full(i, list[i], &buf, &off, &len, level);
  512. applog(LOG_WARNING, "%s", buf);
  513. free(buf);
  514. }
  515. libusb_free_device_list(list, 1);
  516. }
  517. static void cgusb_check_init()
  518. {
  519. mutex_lock(&cgusb_lock);
  520. if (stats_initialised == false) {
  521. // N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
  522. if (opt_usbdump >= 0) {
  523. libusb_set_debug(NULL, opt_usbdump);
  524. usb_all(opt_usbdump);
  525. }
  526. usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
  527. // use constants so the stat generation is very quick
  528. // and the association between number and name can't
  529. // be missalined easily
  530. usb_commands[C_REJECTED] = C_REJECTED_S;
  531. usb_commands[C_PING] = C_PING_S;
  532. usb_commands[C_CLEAR] = C_CLEAR_S;
  533. usb_commands[C_REQUESTVERSION] = C_REQUESTVERSION_S;
  534. usb_commands[C_GETVERSION] = C_GETVERSION_S;
  535. usb_commands[C_REQUESTFPGACOUNT] = C_REQUESTFPGACOUNT_S;
  536. usb_commands[C_GETFPGACOUNT] = C_GETFPGACOUNT_S;
  537. usb_commands[C_STARTPROGRAM] = C_STARTPROGRAM_S;
  538. usb_commands[C_STARTPROGRAMSTATUS] = C_STARTPROGRAMSTATUS_S;
  539. usb_commands[C_PROGRAM] = C_PROGRAM_S;
  540. usb_commands[C_PROGRAMSTATUS] = C_PROGRAMSTATUS_S;
  541. usb_commands[C_PROGRAMSTATUS2] = C_PROGRAMSTATUS2_S;
  542. usb_commands[C_FINALPROGRAMSTATUS] = C_FINALPROGRAMSTATUS_S;
  543. usb_commands[C_SETCLOCK] = C_SETCLOCK_S;
  544. usb_commands[C_REPLYSETCLOCK] = C_REPLYSETCLOCK_S;
  545. usb_commands[C_REQUESTUSERCODE] = C_REQUESTUSERCODE_S;
  546. usb_commands[C_GETUSERCODE] = C_GETUSERCODE_S;
  547. usb_commands[C_REQUESTTEMPERATURE] = C_REQUESTTEMPERATURE_S;
  548. usb_commands[C_GETTEMPERATURE] = C_GETTEMPERATURE_S;
  549. usb_commands[C_SENDWORK] = C_SENDWORK_S;
  550. usb_commands[C_SENDWORKSTATUS] = C_SENDWORKSTATUS_S;
  551. usb_commands[C_REQUESTWORKSTATUS] = C_REQUESTWORKSTATUS_S;
  552. usb_commands[C_GETWORKSTATUS] = C_GETWORKSTATUS_S;
  553. usb_commands[C_REQUESTIDENTIFY] = C_REQUESTIDENTIFY_S;
  554. usb_commands[C_GETIDENTIFY] = C_GETIDENTIFY_S;
  555. usb_commands[C_REQUESTFLASH] = C_REQUESTFLASH_S;
  556. usb_commands[C_REQUESTSENDWORK] = C_REQUESTSENDWORK_S;
  557. usb_commands[C_REQUESTSENDWORKSTATUS] = C_REQUESTSENDWORKSTATUS_S;
  558. usb_commands[C_RESET] = C_RESET_S;
  559. usb_commands[C_SETBAUD] = C_SETBAUD_S;
  560. usb_commands[C_SETDATA] = C_SETDATA_S;
  561. usb_commands[C_SETFLOW] = C_SETFLOW_S;
  562. usb_commands[C_SETMODEM] = C_SETMODEM_S;
  563. usb_commands[C_PURGERX] = C_PURGERX_S;
  564. usb_commands[C_PURGETX] = C_PURGETX_S;
  565. usb_commands[C_FLASHREPLY] = C_FLASHREPLY_S;
  566. stats_initialised = true;
  567. }
  568. mutex_unlock(&cgusb_lock);
  569. }
  570. #ifndef WIN32
  571. #include <errno.h>
  572. #include <unistd.h>
  573. #include <sys/types.h>
  574. #include <sys/ipc.h>
  575. #include <sys/sem.h>
  576. #include <sys/types.h>
  577. #include <sys/stat.h>
  578. #include <fcntl.h>
  579. union semun {
  580. int sem;
  581. struct semid_ds *seminfo;
  582. ushort *all;
  583. };
  584. #endif
  585. // Any errors should always be printed since they will rarely if ever occur
  586. // and thus it is best to always display them
  587. static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  588. {
  589. #ifdef WIN32
  590. struct cgpu_info *cgpu;
  591. HANDLE usbMutex;
  592. char name[64];
  593. DWORD res;
  594. int i;
  595. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  596. usbMutex = CreateMutex(NULL, FALSE, name);
  597. if (usbMutex == NULL) {
  598. applog(LOG_ERR,
  599. "MTX: %s USB failed to get '%s' err (%d)",
  600. drv->dname, name, GetLastError());
  601. return false;
  602. }
  603. res = WaitForSingleObject(usbMutex, 0);
  604. switch(res) {
  605. case WAIT_OBJECT_0:
  606. case WAIT_ABANDONED:
  607. // Am I using it already?
  608. for (i = 0; i < total_devices; i++) {
  609. cgpu = get_devices(i);
  610. if (cgpu->usbinfo.bus_number == bus_number &&
  611. cgpu->usbinfo.device_address == device_address &&
  612. cgpu->usbinfo.nodev == false) {
  613. if (ReleaseMutex(usbMutex)) {
  614. applog(LOG_WARNING,
  615. "MTX: %s USB can't get '%s' - device in use",
  616. drv->dname, name);
  617. goto fail;
  618. }
  619. applog(LOG_ERR,
  620. "MTX: %s USB can't get '%s' - device in use - failure (%d)",
  621. drv->dname, name, GetLastError());
  622. goto fail;
  623. }
  624. }
  625. return true;
  626. case WAIT_TIMEOUT:
  627. if (!hotplug_mode)
  628. applog(LOG_WARNING,
  629. "MTX: %s USB failed to get '%s' - device in use",
  630. drv->dname, name);
  631. goto fail;
  632. case WAIT_FAILED:
  633. applog(LOG_ERR,
  634. "MTX: %s USB failed to get '%s' err (%d)",
  635. drv->dname, name, GetLastError());
  636. goto fail;
  637. default:
  638. applog(LOG_ERR,
  639. "MTX: %s USB failed to get '%s' unknown reply (%d)",
  640. drv->dname, name, res);
  641. goto fail;
  642. }
  643. CloseHandle(usbMutex);
  644. return true;
  645. fail:
  646. CloseHandle(usbMutex);
  647. return false;
  648. #else
  649. struct semid_ds seminfo;
  650. union semun opt;
  651. char name[64];
  652. key_t key;
  653. int fd, sem, count;
  654. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  655. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  656. if (fd == -1) {
  657. applog(LOG_ERR,
  658. "SEM: %s USB open failed '%s' err (%d) %s",
  659. drv->dname, name, errno, strerror(errno));
  660. return false;
  661. }
  662. close(fd);
  663. key = ftok(name, 'K');
  664. sem = semget(key, 1, IPC_CREAT | IPC_EXCL | 438);
  665. if (sem < 0) {
  666. if (errno != EEXIST) {
  667. applog(LOG_ERR,
  668. "SEM: %s USB failed to get '%s' err (%d) %s",
  669. drv->dname, name, errno, strerror(errno));
  670. return false;
  671. }
  672. sem = semget(key, 1, 0);
  673. if (sem < 0) {
  674. applog(LOG_ERR,
  675. "SEM: %s USB failed to access '%s' err (%d) %s",
  676. drv->dname, name, errno, strerror(errno));
  677. return false;
  678. }
  679. opt.seminfo = &seminfo;
  680. count = 0;
  681. while (++count) {
  682. // Should NEVER take 100ms
  683. if (count > 99) {
  684. applog(LOG_ERR,
  685. "SEM: %s USB timeout waiting for (%d) '%s'",
  686. drv->dname, sem, name);
  687. return false;
  688. }
  689. if (semctl(sem, 0, IPC_STAT, opt) == -1) {
  690. applog(LOG_ERR,
  691. "SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
  692. drv->dname, sem, name, count, errno, strerror(errno));
  693. return false;
  694. }
  695. if (opt.seminfo->sem_otime != 0)
  696. break;
  697. nmsleep(1);
  698. }
  699. }
  700. struct sembuf sops[] = {
  701. { 0, 0, IPC_NOWAIT | SEM_UNDO },
  702. { 0, 1, IPC_NOWAIT | SEM_UNDO }
  703. };
  704. if (semop(sem, sops, 2)) {
  705. if (errno == EAGAIN) {
  706. if (!hotplug_mode)
  707. applog(LOG_WARNING,
  708. "SEM: %s USB failed to get (%d) '%s' - device in use",
  709. drv->dname, sem, name);
  710. } else {
  711. applog(LOG_DEBUG,
  712. "SEM: %s USB failed to get (%d) '%s' err (%d) %s",
  713. drv->dname, sem, name, errno, strerror(errno));
  714. }
  715. return false;
  716. }
  717. return true;
  718. #endif
  719. }
  720. static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
  721. {
  722. return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  723. }
  724. // Any errors should always be printed since they will rarely if ever occur
  725. // and thus it is best to always display them
  726. static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  727. {
  728. #ifdef WIN32
  729. HANDLE usbMutex;
  730. char name[64];
  731. sprintf(name, "cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  732. usbMutex = CreateMutex(NULL, FALSE, name);
  733. if (usbMutex == NULL) {
  734. applog(LOG_ERR,
  735. "MTX: %s USB failed to get '%s' for release err (%d)",
  736. drv->dname, name, GetLastError());
  737. return;
  738. }
  739. if (!ReleaseMutex(usbMutex))
  740. applog(LOG_ERR,
  741. "MTX: %s USB failed to release '%s' err (%d)",
  742. drv->dname, name, GetLastError());
  743. CloseHandle(usbMutex);
  744. return;
  745. #else
  746. char name[64];
  747. key_t key;
  748. int fd, sem;
  749. sprintf(name, "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  750. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  751. if (fd == -1) {
  752. applog(LOG_ERR,
  753. "SEM: %s USB open failed '%s' for release err (%d) %s",
  754. drv->dname, name, errno, strerror(errno));
  755. return;
  756. }
  757. close(fd);
  758. key = ftok(name, 'K');
  759. sem = semget(key, 1, 0);
  760. if (sem < 0) {
  761. applog(LOG_ERR,
  762. "SEM: %s USB failed to get '%s' for release err (%d) %s",
  763. drv->dname, name, errno, strerror(errno));
  764. return;
  765. }
  766. struct sembuf sops[] = {
  767. { 0, -1, SEM_UNDO }
  768. };
  769. // Allow a 10ms timeout
  770. // exceeding this timeout means it would probably never succeed anyway
  771. struct timespec timeout = { 0, 10000000 };
  772. // Wait forever since we shoud be the one who has it
  773. if (semtimedop(sem, sops, 1, &timeout)) {
  774. applog(LOG_ERR,
  775. "SEM: %d USB failed to release '%s' err (%d) %s",
  776. drv->dname, name, errno, strerror(errno));
  777. }
  778. return;
  779. #endif
  780. }
  781. static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
  782. {
  783. cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  784. }
  785. static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
  786. {
  787. if (cgusb->serial_string && cgusb->serial_string != BLANK)
  788. free(cgusb->serial_string);
  789. if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
  790. free(cgusb->manuf_string);
  791. if (cgusb->prod_string && cgusb->prod_string != BLANK)
  792. free(cgusb->prod_string);
  793. free(cgusb->descriptor);
  794. free(cgusb->found);
  795. free(cgusb);
  796. return NULL;
  797. }
  798. void usb_uninit(struct cgpu_info *cgpu)
  799. {
  800. // May have happened already during a failed initialisation
  801. // if release_cgpu() was called due to a USB NODEV(err)
  802. if (!cgpu->usbdev)
  803. return;
  804. libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
  805. libusb_close(cgpu->usbdev->handle);
  806. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  807. }
  808. static void release_cgpu(struct cgpu_info *cgpu)
  809. {
  810. struct cg_usb_device *cgusb = cgpu->usbdev;
  811. struct cgpu_info *lookcgpu;
  812. int i;
  813. // It has already been done
  814. if (cgpu->usbinfo.nodev)
  815. return;
  816. total_count--;
  817. drv_count[cgpu->drv->drv_id].count--;
  818. cgpu->usbinfo.nodev = true;
  819. cgpu->usbinfo.nodev_count++;
  820. gettimeofday(&(cgpu->usbinfo.last_nodev), NULL);
  821. // Any devices sharing the same USB device should be marked also
  822. // Currently only MMQ shares a USB device
  823. for (i = 0; i < total_devices; i++) {
  824. lookcgpu = get_devices(i);
  825. if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
  826. total_count--;
  827. drv_count[lookcgpu->drv->drv_id].count--;
  828. lookcgpu->usbinfo.nodev = true;
  829. lookcgpu->usbinfo.nodev_count++;
  830. memcpy(&(lookcgpu->usbinfo.last_nodev),
  831. &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
  832. lookcgpu->usbdev = NULL;
  833. }
  834. }
  835. usb_uninit(cgpu);
  836. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  837. }
  838. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  839. {
  840. struct cg_usb_device *cgusb = NULL;
  841. struct libusb_config_descriptor *config = NULL;
  842. const struct libusb_interface_descriptor *idesc;
  843. const struct libusb_endpoint_descriptor *epdesc;
  844. unsigned char strbuf[STRBUFLEN+1];
  845. char devstr[STRBUFLEN+1];
  846. int err, i, j, k;
  847. cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
  848. cgpu->usbinfo.device_address = libusb_get_device_address(dev);
  849. sprintf(devstr, "- %s device %d:%d", found->name,
  850. cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  851. cgusb = calloc(1, sizeof(*cgusb));
  852. cgusb->found = found;
  853. cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
  854. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  855. if (err) {
  856. applog(LOG_DEBUG,
  857. "USB init failed to get descriptor, err %d %s",
  858. err, devstr);
  859. goto dame;
  860. }
  861. err = libusb_open(dev, &(cgusb->handle));
  862. if (err) {
  863. switch (err) {
  864. case LIBUSB_ERROR_ACCESS:
  865. applog(LOG_ERR,
  866. "USB init open device failed, err %d, "
  867. "you dont have priviledge to access %s",
  868. err, devstr);
  869. break;
  870. #ifdef WIN32
  871. // Windows specific message
  872. case LIBUSB_ERROR_NOT_SUPPORTED:
  873. applog(LOG_ERR,
  874. "USB init, open device failed, err %d, "
  875. "you need to install a Windows USB driver for %s",
  876. err, devstr);
  877. break;
  878. #endif
  879. default:
  880. applog(LOG_DEBUG,
  881. "USB init, open failed, err %d %s",
  882. err, devstr);
  883. }
  884. goto dame;
  885. }
  886. #ifndef WIN32
  887. if (libusb_kernel_driver_active(cgusb->handle, found->kernel) == 1) {
  888. applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
  889. err = libusb_detach_kernel_driver(cgusb->handle, found->kernel);
  890. if (err == 0) {
  891. applog(LOG_DEBUG,
  892. "USB init, kernel detached successfully %s",
  893. devstr);
  894. } else {
  895. applog(LOG_WARNING,
  896. "USB init, kernel detach failed, err %d in use? %s",
  897. err, devstr);
  898. goto cldame;
  899. }
  900. }
  901. #endif
  902. err = libusb_set_configuration(cgusb->handle, found->config);
  903. if (err) {
  904. switch(err) {
  905. case LIBUSB_ERROR_BUSY:
  906. applog(LOG_WARNING,
  907. "USB init, set config %d in use %s",
  908. found->config, devstr);
  909. break;
  910. default:
  911. applog(LOG_DEBUG,
  912. "USB init, failed to set config to %d, err %d %s",
  913. found->config, err, devstr);
  914. }
  915. goto cldame;
  916. }
  917. err = libusb_get_active_config_descriptor(dev, &config);
  918. if (err) {
  919. applog(LOG_DEBUG,
  920. "USB init, failed to get config descriptor, err %d %s",
  921. err, devstr);
  922. goto cldame;
  923. }
  924. if ((int)(config->bNumInterfaces) <= found->interface)
  925. goto cldame;
  926. for (i = 0; i < found->epcount; i++)
  927. found->eps[i].found = false;
  928. for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
  929. idesc = &(config->interface[found->interface].altsetting[i]);
  930. for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
  931. epdesc = &(idesc->endpoint[j]);
  932. for (k = 0; k < found->epcount; k++) {
  933. if (!found->eps[k].found) {
  934. if (epdesc->bmAttributes == found->eps[k].att
  935. && epdesc->wMaxPacketSize >= found->eps[k].size
  936. && epdesc->bEndpointAddress == found->eps[k].ep) {
  937. found->eps[k].found = true;
  938. break;
  939. }
  940. }
  941. }
  942. }
  943. }
  944. for (i = 0; i < found->epcount; i++)
  945. if (found->eps[i].found == false)
  946. goto cldame;
  947. err = libusb_claim_interface(cgusb->handle, found->interface);
  948. if (err) {
  949. switch(err) {
  950. case LIBUSB_ERROR_BUSY:
  951. applog(LOG_WARNING,
  952. "USB init, claim interface %d in use %s",
  953. found->interface, devstr);
  954. break;
  955. default:
  956. applog(LOG_DEBUG,
  957. "USB init, claim interface %d failed, err %d %s",
  958. found->interface, err, devstr);
  959. }
  960. goto cldame;
  961. }
  962. cgusb->usbver = cgusb->descriptor->bcdUSB;
  963. // TODO: allow this with the right version of the libusb include and running library
  964. // cgusb->speed = libusb_get_device_speed(dev);
  965. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  966. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  967. if (err > 0)
  968. cgusb->prod_string = strdup((char *)strbuf);
  969. else
  970. cgusb->prod_string = (char *)BLANK;
  971. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  972. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  973. if (err > 0)
  974. cgusb->manuf_string = strdup((char *)strbuf);
  975. else
  976. cgusb->manuf_string = (char *)BLANK;
  977. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  978. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  979. if (err > 0)
  980. cgusb->serial_string = strdup((char *)strbuf);
  981. else
  982. cgusb->serial_string = (char *)BLANK;
  983. // TODO: ?
  984. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  985. // cgusb->interfaceVersion
  986. applog(LOG_DEBUG,
  987. "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
  988. devstr, cgusb->usbver, cgusb->prod_string,
  989. cgusb->manuf_string, cgusb->serial_string);
  990. cgpu->usbdev = cgusb;
  991. libusb_free_config_descriptor(config);
  992. // Allow a name change based on the idVendor+idProduct
  993. // N.B. must be done before calling add_cgpu()
  994. if (strcmp(cgpu->drv->name, found->name)) {
  995. if (!cgpu->drv->copy)
  996. cgpu->drv = copy_drv(cgpu->drv);
  997. cgpu->drv->name = (char *)(found->name);
  998. }
  999. return true;
  1000. cldame:
  1001. libusb_close(cgusb->handle);
  1002. dame:
  1003. if (config)
  1004. libusb_free_config_descriptor(config);
  1005. cgusb = free_cgusb(cgusb);
  1006. return false;
  1007. }
  1008. static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
  1009. {
  1010. struct libusb_device_descriptor desc;
  1011. int bus_number, device_address;
  1012. int err, i;
  1013. bool ok;
  1014. err = libusb_get_device_descriptor(dev, &desc);
  1015. if (err) {
  1016. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  1017. return false;
  1018. }
  1019. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  1020. applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
  1021. drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  1022. return false;
  1023. }
  1024. if (busdev_count > 0) {
  1025. bus_number = (int)libusb_get_bus_number(dev);
  1026. device_address = (int)libusb_get_device_address(dev);
  1027. ok = false;
  1028. for (i = 0; i < busdev_count; i++) {
  1029. if (bus_number == busdev[i].bus_number) {
  1030. if (busdev[i].device_address == -1 ||
  1031. device_address == busdev[i].device_address) {
  1032. ok = true;
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. if (!ok) {
  1038. applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
  1039. drv->name, look->name, look->idVendor, look->idProduct,
  1040. bus_number, device_address);
  1041. return false;
  1042. }
  1043. }
  1044. applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
  1045. drv->name, look->name, look->idVendor, look->idProduct);
  1046. return true;
  1047. }
  1048. static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
  1049. {
  1050. struct usb_find_devices *found;
  1051. int i;
  1052. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  1053. if (find_dev[i].drv == drvnum) {
  1054. if (usb_check_device(drv, dev, &(find_dev[i]))) {
  1055. found = malloc(sizeof(*found));
  1056. memcpy(found, &(find_dev[i]), sizeof(*found));
  1057. return found;
  1058. }
  1059. }
  1060. return NULL;
  1061. }
  1062. static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
  1063. {
  1064. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1065. applog(LOG_DEBUG,
  1066. "USB scan devices3: %s limit %d reached",
  1067. drv->dname, drv_count[drv->drv_id].limit);
  1068. return NULL;
  1069. }
  1070. #ifdef USE_BFLSC
  1071. if (drv->drv_id == DRIVER_BFLSC)
  1072. return usb_check_each(DRV_BFLSC, drv, dev);
  1073. #endif
  1074. #ifdef USE_BITFORCE
  1075. if (drv->drv_id == DRIVER_BITFORCE)
  1076. return usb_check_each(DRV_BITFORCE, drv, dev);
  1077. #endif
  1078. #ifdef USE_ICARUS
  1079. if (drv->drv_id == DRIVER_ICARUS)
  1080. return usb_check_each(DRV_ICARUS, drv, dev);
  1081. #endif
  1082. #ifdef USE_MODMINER
  1083. if (drv->drv_id == DRIVER_MODMINER)
  1084. return usb_check_each(DRV_MODMINER, drv, dev);
  1085. #endif
  1086. return NULL;
  1087. }
  1088. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
  1089. {
  1090. libusb_device **list;
  1091. ssize_t count, i;
  1092. struct usb_find_devices *found;
  1093. applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
  1094. if (total_count >= total_limit) {
  1095. applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
  1096. return;
  1097. }
  1098. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1099. applog(LOG_DEBUG,
  1100. "USB scan devices: %s limit %d reached",
  1101. drv->dname, drv_count[drv->drv_id].limit);
  1102. return;
  1103. }
  1104. count = libusb_get_device_list(NULL, &list);
  1105. if (count < 0) {
  1106. applog(LOG_DEBUG, "USB scan devices: failed, err %d", count);
  1107. return;
  1108. }
  1109. if (count == 0)
  1110. applog(LOG_DEBUG, "USB scan devices: found no devices");
  1111. for (i = 0; i < count; i++) {
  1112. if (total_count >= total_limit) {
  1113. applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
  1114. break;
  1115. }
  1116. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  1117. applog(LOG_DEBUG,
  1118. "USB scan devices2: %s limit %d reached",
  1119. drv->dname, drv_count[drv->drv_id].limit);
  1120. break;
  1121. }
  1122. found = usb_check(drv, list[i]);
  1123. if (found != NULL) {
  1124. if (cgminer_usb_lock(drv, list[i]) == false)
  1125. free(found);
  1126. else {
  1127. if (!device_detect(list[i], found))
  1128. cgminer_usb_unlock(drv, list[i]);
  1129. else {
  1130. total_count++;
  1131. drv_count[drv->drv_id].count++;
  1132. }
  1133. }
  1134. }
  1135. }
  1136. libusb_free_device_list(list, 1);
  1137. }
  1138. // Set this to 0 to remove stats processing
  1139. #define DO_USB_STATS 1
  1140. #if DO_USB_STATS
  1141. #define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
  1142. #define STATS_TIMEVAL(tv) gettimeofday(tv, NULL)
  1143. #else
  1144. #define USB_STATS(sgpu, sta, fin, err, cmd, seq)
  1145. #define STATS_TIMEVAL(tv)
  1146. #endif
  1147. // The stat data can be spurious due to not locking it before copying it -
  1148. // however that would require the stat() function to also lock and release
  1149. // a mutex every time a usb read or write is called which would slow
  1150. // things down more
  1151. struct api_data *api_usb_stats(__maybe_unused int *count)
  1152. {
  1153. #if DO_USB_STATS
  1154. struct cg_usb_stats_details *details;
  1155. struct cg_usb_stats *sta;
  1156. struct api_data *root = NULL;
  1157. int device;
  1158. int cmdseq;
  1159. if (next_stat == 0)
  1160. return NULL;
  1161. while (*count < next_stat * C_MAX * 2) {
  1162. device = *count / (C_MAX * 2);
  1163. cmdseq = *count % (C_MAX * 2);
  1164. (*count)++;
  1165. sta = &(usb_stats[device]);
  1166. details = &(sta->details[cmdseq]);
  1167. // Only show stats that have results
  1168. if (details->item[CMD_CMD].count == 0 &&
  1169. details->item[CMD_TIMEOUT].count == 0 &&
  1170. details->item[CMD_ERROR].count == 0)
  1171. continue;
  1172. root = api_add_string(root, "Name", sta->name, false);
  1173. root = api_add_int(root, "ID", &(sta->device_id), false);
  1174. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  1175. root = api_add_int(root, "Seq", &(details->seq), true);
  1176. root = api_add_uint64(root, "Count",
  1177. &(details->item[CMD_CMD].count), true);
  1178. root = api_add_double(root, "Total Delay",
  1179. &(details->item[CMD_CMD].total_delay), true);
  1180. root = api_add_double(root, "Min Delay",
  1181. &(details->item[CMD_CMD].min_delay), true);
  1182. root = api_add_double(root, "Max Delay",
  1183. &(details->item[CMD_CMD].max_delay), true);
  1184. root = api_add_uint64(root, "Timeout Count",
  1185. &(details->item[CMD_TIMEOUT].count), true);
  1186. root = api_add_double(root, "Timeout Total Delay",
  1187. &(details->item[CMD_TIMEOUT].total_delay), true);
  1188. root = api_add_double(root, "Timeout Min Delay",
  1189. &(details->item[CMD_TIMEOUT].min_delay), true);
  1190. root = api_add_double(root, "Timeout Max Delay",
  1191. &(details->item[CMD_TIMEOUT].max_delay), true);
  1192. root = api_add_uint64(root, "Error Count",
  1193. &(details->item[CMD_ERROR].count), true);
  1194. root = api_add_double(root, "Error Total Delay",
  1195. &(details->item[CMD_ERROR].total_delay), true);
  1196. root = api_add_double(root, "Error Min Delay",
  1197. &(details->item[CMD_ERROR].min_delay), true);
  1198. root = api_add_double(root, "Error Max Delay",
  1199. &(details->item[CMD_ERROR].max_delay), true);
  1200. root = api_add_timeval(root, "First Command",
  1201. &(details->item[CMD_CMD].first), true);
  1202. root = api_add_timeval(root, "Last Command",
  1203. &(details->item[CMD_CMD].last), true);
  1204. root = api_add_timeval(root, "First Timeout",
  1205. &(details->item[CMD_TIMEOUT].first), true);
  1206. root = api_add_timeval(root, "Last Timeout",
  1207. &(details->item[CMD_TIMEOUT].last), true);
  1208. root = api_add_timeval(root, "First Error",
  1209. &(details->item[CMD_ERROR].first), true);
  1210. root = api_add_timeval(root, "Last Error",
  1211. &(details->item[CMD_ERROR].last), true);
  1212. return root;
  1213. }
  1214. #endif
  1215. return NULL;
  1216. }
  1217. #if DO_USB_STATS
  1218. static void newstats(struct cgpu_info *cgpu)
  1219. {
  1220. int i;
  1221. mutex_lock(&cgusb_lock);
  1222. cgpu->usbinfo.usbstat = ++next_stat;
  1223. mutex_unlock(&cgusb_lock);
  1224. usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
  1225. usb_stats[next_stat-1].name = cgpu->drv->name;
  1226. usb_stats[next_stat-1].device_id = -1;
  1227. usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
  1228. for (i = 1; i < C_MAX * 2; i += 2)
  1229. usb_stats[next_stat-1].details[i].seq = 1;
  1230. }
  1231. #endif
  1232. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  1233. {
  1234. #if DO_USB_STATS
  1235. if (cgpu->usbinfo.usbstat < 1)
  1236. newstats(cgpu);
  1237. // we don't know the device_id until after add_cgpu()
  1238. usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
  1239. #endif
  1240. }
  1241. #if DO_USB_STATS
  1242. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
  1243. {
  1244. struct cg_usb_stats_details *details;
  1245. double diff;
  1246. int item;
  1247. if (cgpu->usbinfo.usbstat < 1)
  1248. newstats(cgpu);
  1249. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
  1250. diff = tdiff(tv_finish, tv_start);
  1251. switch (err) {
  1252. case LIBUSB_SUCCESS:
  1253. item = CMD_CMD;
  1254. break;
  1255. case LIBUSB_ERROR_TIMEOUT:
  1256. item = CMD_TIMEOUT;
  1257. break;
  1258. default:
  1259. item = CMD_ERROR;
  1260. break;
  1261. }
  1262. if (details->item[item].count == 0) {
  1263. details->item[item].min_delay = diff;
  1264. memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  1265. } else if (diff < details->item[item].min_delay)
  1266. details->item[item].min_delay = diff;
  1267. if (diff > details->item[item].max_delay)
  1268. details->item[item].max_delay = diff;
  1269. details->item[item].total_delay += diff;
  1270. memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
  1271. details->item[item].count++;
  1272. }
  1273. static void rejected_inc(struct cgpu_info *cgpu)
  1274. {
  1275. struct cg_usb_stats_details *details;
  1276. int item = CMD_ERROR;
  1277. if (cgpu->usbinfo.usbstat < 1)
  1278. newstats(cgpu);
  1279. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
  1280. details->item[item].count++;
  1281. }
  1282. #endif
  1283. 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)
  1284. {
  1285. struct cg_usb_device *usbdev = cgpu->usbdev;
  1286. #if DO_USB_STATS
  1287. struct timeval tv_start;
  1288. #endif
  1289. struct timeval read_start, tv_finish;
  1290. unsigned int initial_timeout;
  1291. double max, done;
  1292. int err, got, tot, i;
  1293. bool first = true;
  1294. if (cgpu->usbinfo.nodev) {
  1295. *buf = '\0';
  1296. *processed = 0;
  1297. #if DO_USB_STATS
  1298. rejected_inc(cgpu);
  1299. #endif
  1300. return LIBUSB_ERROR_NO_DEVICE;
  1301. }
  1302. if (timeout == DEVTIMEOUT)
  1303. timeout = usbdev->found->timeout;
  1304. if (eol == -1) {
  1305. got = 0;
  1306. STATS_TIMEVAL(&tv_start);
  1307. err = libusb_bulk_transfer(usbdev->handle,
  1308. usbdev->found->eps[ep].ep,
  1309. (unsigned char *)buf,
  1310. bufsiz, &got, timeout);
  1311. STATS_TIMEVAL(&tv_finish);
  1312. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1313. if (ftdi) {
  1314. // first 2 bytes returned are an FTDI status
  1315. if (got > 2) {
  1316. got -= 2;
  1317. memmove(buf, buf+2, got+1);
  1318. } else {
  1319. got = 0;
  1320. *buf = '\0';
  1321. }
  1322. }
  1323. *processed = got;
  1324. if (NODEV(err))
  1325. release_cgpu(cgpu);
  1326. return err;
  1327. }
  1328. tot = 0;
  1329. err = LIBUSB_SUCCESS;
  1330. initial_timeout = timeout;
  1331. max = ((double)timeout) / 1000.0;
  1332. gettimeofday(&read_start, NULL);
  1333. while (bufsiz) {
  1334. got = 0;
  1335. STATS_TIMEVAL(&tv_start);
  1336. err = libusb_bulk_transfer(usbdev->handle,
  1337. usbdev->found->eps[ep].ep,
  1338. (unsigned char *)buf,
  1339. bufsiz, &got, timeout);
  1340. gettimeofday(&tv_finish, NULL);
  1341. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  1342. if (ftdi) {
  1343. // first 2 bytes returned are an FTDI status
  1344. if (got > 2) {
  1345. got -= 2;
  1346. memmove(buf, buf+2, got+1);
  1347. } else {
  1348. got = 0;
  1349. *buf = '\0';
  1350. }
  1351. }
  1352. tot += got;
  1353. if (err)
  1354. break;
  1355. // WARNING - this will return data past EOL ('if' there is extra data)
  1356. for (i = 0; i < got; i++)
  1357. if (buf[i] == eol)
  1358. goto goteol;
  1359. buf += got;
  1360. bufsiz -= got;
  1361. first = false;
  1362. done = tdiff(&tv_finish, &read_start);
  1363. // N.B. this is return LIBUSB_SUCCESS with whatever size has already been read
  1364. if (unlikely(done >= max))
  1365. break;
  1366. timeout = initial_timeout - (done * 1000);
  1367. }
  1368. goteol:
  1369. *processed = tot;
  1370. if (NODEV(err))
  1371. release_cgpu(cgpu);
  1372. return err;
  1373. }
  1374. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
  1375. {
  1376. struct cg_usb_device *usbdev = cgpu->usbdev;
  1377. #if DO_USB_STATS
  1378. struct timeval tv_start, tv_finish;
  1379. #endif
  1380. int err, sent;
  1381. if (cgpu->usbinfo.nodev) {
  1382. *processed = 0;
  1383. #if DO_USB_STATS
  1384. rejected_inc(cgpu);
  1385. #endif
  1386. return LIBUSB_ERROR_NO_DEVICE;
  1387. }
  1388. sent = 0;
  1389. STATS_TIMEVAL(&tv_start);
  1390. err = libusb_bulk_transfer(usbdev->handle,
  1391. usbdev->found->eps[ep].ep,
  1392. (unsigned char *)buf,
  1393. bufsiz, &sent,
  1394. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1395. STATS_TIMEVAL(&tv_finish);
  1396. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1397. *processed = sent;
  1398. if (NODEV(err))
  1399. release_cgpu(cgpu);
  1400. return err;
  1401. }
  1402. 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)
  1403. {
  1404. struct cg_usb_device *usbdev = cgpu->usbdev;
  1405. #if DO_USB_STATS
  1406. struct timeval tv_start, tv_finish;
  1407. #endif
  1408. int err;
  1409. if (cgpu->usbinfo.nodev) {
  1410. #if DO_USB_STATS
  1411. rejected_inc(cgpu);
  1412. #endif
  1413. return LIBUSB_ERROR_NO_DEVICE;
  1414. }
  1415. STATS_TIMEVAL(&tv_start);
  1416. err = libusb_control_transfer(usbdev->handle, request_type,
  1417. bRequest, wValue, wIndex, NULL, 0,
  1418. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1419. STATS_TIMEVAL(&tv_finish);
  1420. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1421. if (NODEV(err))
  1422. release_cgpu(cgpu);
  1423. return err;
  1424. }
  1425. void usb_cleanup()
  1426. {
  1427. struct cgpu_info *cgpu;
  1428. int i;
  1429. hotplug_time = 0;
  1430. nmsleep(10);
  1431. for (i = 0; i < total_devices; i++) {
  1432. cgpu = devices[i];
  1433. switch (cgpu->drv->drv_id) {
  1434. case DRIVER_BFLSC:
  1435. case DRIVER_BITFORCE:
  1436. case DRIVER_MODMINER:
  1437. release_cgpu(cgpu);
  1438. break;
  1439. default:
  1440. break;
  1441. }
  1442. }
  1443. }
  1444. void usb_initialise()
  1445. {
  1446. char *fre, *ptr, *comma, *colon;
  1447. int bus, dev, lim, i;
  1448. bool found;
  1449. for (i = 0; i < DRIVER_MAX; i++) {
  1450. drv_count[i].count = 0;
  1451. drv_count[i].limit = 999999;
  1452. }
  1453. cgusb_check_init();
  1454. if (opt_usb_select && *opt_usb_select) {
  1455. // Absolute device limit
  1456. if (*opt_usb_select == ':') {
  1457. total_limit = atoi(opt_usb_select+1);
  1458. if (total_limit < 0)
  1459. quit(1, "Invalid --usb total limit");
  1460. // Comma list of bus:dev devices to match
  1461. } else if (isdigit(*opt_usb_select)) {
  1462. fre = ptr = strdup(opt_usb_select);
  1463. do {
  1464. comma = strchr(ptr, ',');
  1465. if (comma)
  1466. *(comma++) = '\0';
  1467. colon = strchr(ptr, ':');
  1468. if (!colon)
  1469. quit(1, "Invalid --usb bus:dev missing ':'");
  1470. *(colon++) = '\0';
  1471. if (!isdigit(*ptr))
  1472. quit(1, "Invalid --usb bus:dev - bus must be a number");
  1473. if (!isdigit(*colon) && *colon != '*')
  1474. quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
  1475. bus = atoi(ptr);
  1476. if (bus <= 0)
  1477. quit(1, "Invalid --usb bus:dev - bus must be > 0");
  1478. if (!colon == '*')
  1479. dev = -1;
  1480. else {
  1481. dev = atoi(colon);
  1482. if (dev <= 0)
  1483. quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
  1484. }
  1485. busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
  1486. busdev[busdev_count-1].bus_number = bus;
  1487. busdev[busdev_count-1].device_address = dev;
  1488. ptr = comma;
  1489. } while (ptr);
  1490. free(fre);
  1491. // Comma list of DRV:limit
  1492. } else {
  1493. fre = ptr = strdup(opt_usb_select);
  1494. do {
  1495. comma = strchr(ptr, ',');
  1496. if (comma)
  1497. *(comma++) = '\0';
  1498. colon = strchr(ptr, ':');
  1499. if (!colon)
  1500. quit(1, "Invalid --usb DRV:limit missing ':'");
  1501. *(colon++) = '\0';
  1502. if (!isdigit(*colon))
  1503. quit(1, "Invalid --usb DRV:limit - limit must be a number");
  1504. lim = atoi(colon);
  1505. if (lim < 0)
  1506. quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
  1507. found = false;
  1508. #ifdef USE_BFLSC
  1509. if (strcasecmp(ptr, bflsc_drv.name) == 0) {
  1510. drv_count[bflsrc_drv.drv_id].limit = lim;
  1511. found = true;
  1512. }
  1513. #endif
  1514. #ifdef USE_BITFORCE
  1515. if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
  1516. drv_count[bitforce_drv.drv_id].limit = lim;
  1517. found = true;
  1518. }
  1519. #endif
  1520. #ifdef USE_MODMINER
  1521. if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
  1522. drv_count[modminer_drv.drv_id].limit = lim;
  1523. found = true;
  1524. }
  1525. #endif
  1526. if (!found)
  1527. quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
  1528. ptr = comma;
  1529. } while (ptr);
  1530. free(fre);
  1531. }
  1532. }
  1533. }