usbutils.c 48 KB

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