usbutils.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * Copyright 2012 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 <stdint.h>
  11. #include <stdbool.h>
  12. #include "logging.h"
  13. #include "miner.h"
  14. #include "usbutils.h"
  15. #ifdef USE_ICARUS
  16. #define DRV_ICARUS 1
  17. #endif
  18. #ifdef USE_AVALON
  19. #define DRV_AVALON 1
  20. #endif
  21. #ifdef USE_BITFORCE
  22. #define DRV_BITFORCE 2
  23. #endif
  24. #ifdef USE_MODMINER
  25. #define DRV_MODMINER 3
  26. #endif
  27. #define DRV_LAST -1
  28. #define USB_CONFIG 1
  29. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  30. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  31. #ifdef WIN32
  32. #define MODMINER_TIMEOUT_MS 200
  33. #else
  34. #define MODMINER_TIMEOUT_MS 100
  35. #endif
  36. #ifdef USE_MODMINER
  37. static struct usb_endpoints mmq_eps[] = {
  38. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
  39. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0 }
  40. };
  41. #endif
  42. // TODO: Add support for (at least) Interrupt endpoints
  43. static struct usb_find_devices find_dev[] = {
  44. /*
  45. #ifdef USE_ICARUS
  46. { DRV_ICARUS, "ICA", 0x067b, 0x0230, true, EPI(3), EPO(2), 1 },
  47. { DRV_ICARUS, "LOT", 0x0403, 0x6001, false, EPI(0), EPO(0), 1 },
  48. { DRV_ICARUS, "CM1", 0x067b, 0x0230, false, EPI(0), EPO(0), 1 },
  49. #endif
  50. #ifdef USE_BITFORCE
  51. { DRV_BITFORCE, "BFL", 0x0403, 0x6014, true, EPI(1), EPO(2), 1 },
  52. #endif
  53. */
  54. #ifdef USE_MODMINER
  55. {
  56. .drv = DRV_MODMINER,
  57. .name = "MMQ",
  58. .idVendor = 0x1fc9,
  59. .idProduct = 0x0003,
  60. .config = 1,
  61. .interface = 1,
  62. .timeout = MODMINER_TIMEOUT_MS,
  63. .epcount = ARRAY_SIZE(mmq_eps),
  64. .eps = mmq_eps },
  65. #endif
  66. { DRV_LAST, NULL, 0, 0, 0, 0, 0, 0, NULL }
  67. };
  68. #ifdef USE_BITFORCE
  69. extern struct device_api bitforce_api;
  70. #endif
  71. #ifdef USE_ICARUS
  72. extern struct device_api icarus_api;
  73. #endif
  74. #ifdef USE_AVALON
  75. extern struct device_api avalon_api;
  76. #endif
  77. #ifdef USE_MODMINER
  78. extern struct device_api modminer_api;
  79. #endif
  80. /*
  81. * Our own internal list of used USB devices
  82. * So two drivers or a single driver searching
  83. * can't touch the same device during detection
  84. */
  85. struct usb_list {
  86. uint8_t bus_number;
  87. uint8_t device_address;
  88. uint8_t filler[2];
  89. struct usb_list *prev;
  90. struct usb_list *next;
  91. };
  92. #define STRBUFLEN 256
  93. static const char *BLANK = "";
  94. static pthread_mutex_t *list_lock = NULL;
  95. static struct usb_list *usb_head = NULL;
  96. struct cg_usb_stats_item {
  97. uint64_t count;
  98. double total_delay;
  99. double min_delay;
  100. double max_delay;
  101. struct timeval first;
  102. struct timeval last;
  103. };
  104. #define CMD_CMD 0
  105. #define CMD_TIMEOUT 1
  106. #define CMD_ERROR 2
  107. struct cg_usb_stats_details {
  108. int seq;
  109. struct cg_usb_stats_item item[CMD_ERROR+1];
  110. };
  111. struct cg_usb_stats {
  112. char *name;
  113. int device_id;
  114. struct cg_usb_stats_details *details;
  115. };
  116. #define SEQ0 0
  117. #define SEQ1 1
  118. static struct cg_usb_stats *usb_stats = NULL;
  119. static int next_stat = 0;
  120. static const char **usb_commands;
  121. static const char *C_PING_S = "Ping";
  122. static const char *C_CLEAR_S = "Clear";
  123. static const char *C_REQUESTVERSION_S = "RequestVersion";
  124. static const char *C_GETVERSION_S = "GetVersion";
  125. static const char *C_REQUESTFPGACOUNT_S = "RequestFPGACount";
  126. static const char *C_GETFPGACOUNT_S = "GetFPGACount";
  127. static const char *C_STARTPROGRAM_S = "StartProgram";
  128. static const char *C_STARTPROGRAMSTATUS_S = "StartProgramStatus";
  129. static const char *C_PROGRAM_S = "Program";
  130. static const char *C_PROGRAMSTATUS_S = "ProgramStatus";
  131. static const char *C_PROGRAMSTATUS2_S = "ProgramStatus2";
  132. static const char *C_FINALPROGRAMSTATUS_S = "FinalProgramStatus";
  133. static const char *C_SETCLOCK_S = "SetClock";
  134. static const char *C_REPLYSETCLOCK_S = "ReplySetClock";
  135. static const char *C_REQUESTUSERCODE_S = "RequestUserCode";
  136. static const char *C_GETUSERCODE_S = "GetUserCode";
  137. static const char *C_REQUESTTEMPERATURE_S = "RequestTemperature";
  138. static const char *C_GETTEMPERATURE_S = "GetTemperature";
  139. static const char *C_SENDWORK_S = "SendWork";
  140. static const char *C_SENDWORKSTATUS_S = "SendWorkStatus";
  141. static const char *C_REQUESTWORKSTATUS_S = "RequestWorkStatus";
  142. static const char *C_GETWORKSTATUS_S = "GetWorkStatus";
  143. #ifdef EOL
  144. #undef EOL
  145. #endif
  146. #define EOL "\n"
  147. static const char *DESDEV = "Device";
  148. static const char *DESCON = "Config";
  149. static const char *DESSTR = "String";
  150. static const char *DESINT = "Interface";
  151. static const char *DESEP = "Endpoint";
  152. static const char *DESHID = "HID";
  153. static const char *DESRPT = "Report";
  154. static const char *DESPHY = "Physical";
  155. static const char *DESHUB = "Hub";
  156. static const char *EPIN = "In: ";
  157. static const char *EPOUT = "Out: ";
  158. static const char *EPX = "?: ";
  159. static const char *CONTROL = "Control";
  160. static const char *ISOCHRONOUS_X = "Isochronous+?";
  161. static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
  162. static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
  163. static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
  164. static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
  165. static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
  166. static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
  167. static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
  168. static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
  169. static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
  170. static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
  171. static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
  172. static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
  173. static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
  174. static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
  175. static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
  176. static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
  177. static const char *BULK = "Bulk";
  178. static const char *INTERRUPT = "Interrupt";
  179. static const char *UNKNOWN = "Unknown";
  180. static const char *destype(uint8_t bDescriptorType)
  181. {
  182. switch (bDescriptorType) {
  183. case LIBUSB_DT_DEVICE:
  184. return DESDEV;
  185. case LIBUSB_DT_CONFIG:
  186. return DESCON;
  187. case LIBUSB_DT_STRING:
  188. return DESSTR;
  189. case LIBUSB_DT_INTERFACE:
  190. return DESINT;
  191. case LIBUSB_DT_ENDPOINT:
  192. return DESEP;
  193. case LIBUSB_DT_HID:
  194. return DESHID;
  195. case LIBUSB_DT_REPORT:
  196. return DESRPT;
  197. case LIBUSB_DT_PHYSICAL:
  198. return DESPHY;
  199. case LIBUSB_DT_HUB:
  200. return DESHUB;
  201. }
  202. return UNKNOWN;
  203. }
  204. static const char *epdir(uint8_t bEndpointAddress)
  205. {
  206. switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
  207. case LIBUSB_ENDPOINT_IN:
  208. return EPIN;
  209. case LIBUSB_ENDPOINT_OUT:
  210. return EPOUT;
  211. }
  212. return EPX;
  213. }
  214. static const char *epatt(uint8_t bmAttributes)
  215. {
  216. switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
  217. case LIBUSB_TRANSFER_TYPE_CONTROL:
  218. return CONTROL;
  219. case LIBUSB_TRANSFER_TYPE_BULK:
  220. return BULK;
  221. case LIBUSB_TRANSFER_TYPE_INTERRUPT:
  222. return INTERRUPT;
  223. case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
  224. switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
  225. case LIBUSB_ISO_SYNC_TYPE_NONE:
  226. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  227. case LIBUSB_ISO_USAGE_TYPE_DATA:
  228. return ISOCHRONOUS_N_D;
  229. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  230. return ISOCHRONOUS_N_F;
  231. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  232. return ISOCHRONOUS_N_I;
  233. }
  234. return ISOCHRONOUS_N_X;
  235. case LIBUSB_ISO_SYNC_TYPE_ASYNC:
  236. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  237. case LIBUSB_ISO_USAGE_TYPE_DATA:
  238. return ISOCHRONOUS_A_D;
  239. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  240. return ISOCHRONOUS_A_F;
  241. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  242. return ISOCHRONOUS_A_I;
  243. }
  244. return ISOCHRONOUS_A_X;
  245. case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
  246. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  247. case LIBUSB_ISO_USAGE_TYPE_DATA:
  248. return ISOCHRONOUS_D_D;
  249. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  250. return ISOCHRONOUS_D_F;
  251. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  252. return ISOCHRONOUS_D_I;
  253. }
  254. return ISOCHRONOUS_D_X;
  255. case LIBUSB_ISO_SYNC_TYPE_SYNC:
  256. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  257. case LIBUSB_ISO_USAGE_TYPE_DATA:
  258. return ISOCHRONOUS_S_D;
  259. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  260. return ISOCHRONOUS_S_F;
  261. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  262. return ISOCHRONOUS_S_I;
  263. }
  264. return ISOCHRONOUS_S_X;
  265. }
  266. return ISOCHRONOUS_X;
  267. }
  268. return UNKNOWN;
  269. }
  270. static void append(char **buf, char *append, size_t *off, size_t *len)
  271. {
  272. int new = strlen(append);
  273. if ((new + *off) >= *len)
  274. {
  275. *len *= 2;
  276. *buf = realloc(*buf, *len);
  277. }
  278. strcpy(*buf + *off, append);
  279. *off += new;
  280. }
  281. 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)
  282. {
  283. char tmp[512];
  284. int err;
  285. err = libusb_set_configuration(handle, cd);
  286. if (err) {
  287. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
  288. (int)count, cd, err);
  289. append(buf, tmp, off, len);
  290. return false;
  291. }
  292. err = libusb_get_active_config_descriptor(dev, config);
  293. if (err) {
  294. sprintf(tmp, EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
  295. (int)count, cd, err);
  296. append(buf, tmp, off, len);
  297. return false;
  298. }
  299. sprintf(tmp, EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
  300. (int)count, cd, err);
  301. append(buf, tmp, off, len);
  302. return true;
  303. }
  304. static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off, size_t *len)
  305. {
  306. struct libusb_device_descriptor desc;
  307. struct libusb_device_handle *handle;
  308. struct libusb_config_descriptor *config;
  309. const struct libusb_interface_descriptor *idesc;
  310. const struct libusb_endpoint_descriptor *epdesc;
  311. unsigned char man[STRBUFLEN+1];
  312. unsigned char prod[STRBUFLEN+1];
  313. unsigned char ser[STRBUFLEN+1];
  314. char tmp[512];
  315. int err, i, j, k;
  316. err = libusb_get_device_descriptor(dev, &desc);
  317. if (err) {
  318. sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
  319. (int)count, err);
  320. append(buf, tmp, off, len);
  321. return;
  322. }
  323. sprintf(tmp, EOL ".USB dev %d: Device Descriptor:" EOL "\tLength: %d" EOL
  324. "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
  325. "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
  326. "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
  327. "\tNumConfigurations: %d",
  328. (int)count, (int)(desc.bLength), destype(desc.bDescriptorType),
  329. desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
  330. (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
  331. desc.idVendor, desc.idProduct, desc.bcdDevice,
  332. (int)(desc.bNumConfigurations));
  333. append(buf, tmp, off, len);
  334. err = libusb_open(dev, &handle);
  335. if (err) {
  336. sprintf(tmp, EOL " ** dev %d: Failed to open, err %d", (int)count, err);
  337. append(buf, tmp, off, len);
  338. return;
  339. }
  340. if (libusb_kernel_driver_active(handle, 0) == 1) {
  341. sprintf(tmp, EOL " * dev %d: kernel attached", (int)count);
  342. append(buf, tmp, off, len);
  343. }
  344. err = libusb_get_active_config_descriptor(dev, &config);
  345. if (err) {
  346. if (!setgetdes(count, dev, handle, &config, 1, buf, off, len)
  347. && !setgetdes(count, dev, handle, &config, 0, buf, off, len)) {
  348. libusb_close(handle);
  349. sprintf(tmp, EOL " ** dev %d: Failed to set config descriptor to %d or %d",
  350. (int)count, 1, 0);
  351. append(buf, tmp, off, len);
  352. return;
  353. }
  354. }
  355. sprintf(tmp, EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
  356. "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
  357. "\tAttributes: %d" EOL "\tMaxPower: %d",
  358. (int)count, destype(config->bDescriptorType),
  359. (int)(config->bNumInterfaces), (int)(config->iConfiguration),
  360. (int)(config->bmAttributes), (int)(config->MaxPower));
  361. append(buf, tmp, off, len);
  362. for (i = 0; i < (int)(config->bNumInterfaces); i++) {
  363. for (j = 0; j < config->interface[i].num_altsetting; j++) {
  364. idesc = &(config->interface[i].altsetting[j]);
  365. sprintf(tmp, EOL " _dev %d: Interface Descriptor %d:" EOL
  366. "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
  367. "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
  368. "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
  369. (int)count, j, destype(idesc->bDescriptorType),
  370. (int)(idesc->bInterfaceNumber),
  371. (int)(idesc->bNumEndpoints),
  372. (int)(idesc->bInterfaceClass),
  373. (int)(idesc->bInterfaceSubClass),
  374. (int)(idesc->bInterfaceProtocol));
  375. append(buf, tmp, off, len);
  376. for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
  377. epdesc = &(idesc->endpoint[k]);
  378. sprintf(tmp, EOL " __dev %d: Interface %d Endpoint %d:" EOL
  379. "\tDescriptorType: %s" EOL
  380. "\tEndpointAddress: %s0x%x" EOL
  381. "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
  382. "\tInterval: %d" EOL "\tRefresh: %d",
  383. (int)count, (int)(idesc->bInterfaceNumber), k,
  384. destype(epdesc->bDescriptorType),
  385. epdir(epdesc->bEndpointAddress),
  386. (int)(epdesc->bEndpointAddress),
  387. epatt(epdesc->bmAttributes),
  388. epdesc->wMaxPacketSize,
  389. (int)(epdesc->bInterval),
  390. (int)(epdesc->bRefresh));
  391. append(buf, tmp, off, len);
  392. }
  393. }
  394. }
  395. libusb_free_config_descriptor(config);
  396. config = NULL;
  397. err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
  398. if (err < 0)
  399. sprintf((char *)man, "** err(%d)", err);
  400. err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
  401. if (err < 0)
  402. sprintf((char *)prod, "** err(%d)", err);
  403. err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
  404. if (err < 0)
  405. sprintf((char *)ser, "** err(%d)", err);
  406. sprintf(tmp, EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
  407. "\tProduct: '%s'" EOL "\tSerial '%s'",
  408. (int)count, man, prod, ser);
  409. append(buf, tmp, off, len);
  410. libusb_close(handle);
  411. }
  412. // Function to dump all USB devices
  413. static void usb_all()
  414. {
  415. libusb_device **list;
  416. ssize_t count, i;
  417. char *buf;
  418. size_t len, off;
  419. count = libusb_get_device_list(NULL, &list);
  420. if (count < 0) {
  421. applog(LOG_ERR, "USB all: failed, err %d", (int)count);
  422. return;
  423. }
  424. if (count == 0)
  425. applog(LOG_WARNING, "USB all: found no devices");
  426. else
  427. {
  428. len = 10000;
  429. buf = malloc(len+1);
  430. sprintf(buf, "USB all: found %d devices", (int)count);
  431. off = strlen(buf);
  432. for (i = 0; i < count; i++)
  433. usb_full(i, list[i], &buf, &off, &len);
  434. applog(LOG_WARNING, "%s", buf);
  435. free(buf);
  436. }
  437. libusb_free_device_list(list, 1);
  438. }
  439. static void cgusb_check_init()
  440. {
  441. mutex_lock(&cgusb_lock);
  442. if (list_lock == NULL) {
  443. list_lock = calloc(1, sizeof(*list_lock));
  444. mutex_init(list_lock);
  445. if (opt_usbdump >= 0) {
  446. libusb_set_debug(NULL, opt_usbdump);
  447. usb_all();
  448. }
  449. usb_commands = malloc(sizeof(*usb_commands) * C_MAX);
  450. // use constants so the stat generation is very quick
  451. // and the association between number and name can't
  452. // be missalined easily
  453. usb_commands[C_PING] = C_PING_S;
  454. usb_commands[C_CLEAR] = C_CLEAR_S;
  455. usb_commands[C_REQUESTVERSION] = C_REQUESTVERSION_S;
  456. usb_commands[C_GETVERSION] = C_GETVERSION_S;
  457. usb_commands[C_REQUESTFPGACOUNT] = C_REQUESTFPGACOUNT_S;
  458. usb_commands[C_GETFPGACOUNT] = C_GETFPGACOUNT_S;
  459. usb_commands[C_STARTPROGRAM] = C_STARTPROGRAM_S;
  460. usb_commands[C_STARTPROGRAMSTATUS] = C_STARTPROGRAMSTATUS_S;
  461. usb_commands[C_PROGRAM] = C_PROGRAM_S;
  462. usb_commands[C_PROGRAMSTATUS] = C_PROGRAMSTATUS_S;
  463. usb_commands[C_PROGRAMSTATUS2] = C_PROGRAMSTATUS2_S;
  464. usb_commands[C_FINALPROGRAMSTATUS] = C_FINALPROGRAMSTATUS_S;
  465. usb_commands[C_SETCLOCK] = C_SETCLOCK_S;
  466. usb_commands[C_REPLYSETCLOCK] = C_REPLYSETCLOCK_S;
  467. usb_commands[C_REQUESTUSERCODE] = C_REQUESTUSERCODE_S;
  468. usb_commands[C_GETUSERCODE] = C_GETUSERCODE_S;
  469. usb_commands[C_REQUESTTEMPERATURE] = C_REQUESTTEMPERATURE_S;
  470. usb_commands[C_GETTEMPERATURE] = C_GETTEMPERATURE_S;
  471. usb_commands[C_SENDWORK] = C_SENDWORK_S;
  472. usb_commands[C_SENDWORKSTATUS] = C_SENDWORKSTATUS_S;
  473. usb_commands[C_REQUESTWORKSTATUS] = C_REQUESTWORKSTATUS_S;
  474. usb_commands[C_GETWORKSTATUS] = C_GETWORKSTATUS_S;
  475. }
  476. mutex_unlock(&cgusb_lock);
  477. }
  478. static bool in_use(libusb_device *dev, bool lock)
  479. {
  480. struct usb_list *usb_tmp;
  481. bool used = false;
  482. uint8_t bus_number;
  483. uint8_t device_address;
  484. bus_number = libusb_get_bus_number(dev);
  485. device_address = libusb_get_device_address(dev);
  486. if (lock)
  487. mutex_lock(list_lock);
  488. if ((usb_tmp = usb_head))
  489. do {
  490. if (bus_number == usb_tmp->bus_number
  491. && device_address == usb_tmp->device_address) {
  492. used = true;
  493. break;
  494. }
  495. usb_tmp = usb_tmp->next;
  496. } while (usb_tmp != usb_head);
  497. if (lock)
  498. mutex_unlock(list_lock);
  499. return used;
  500. }
  501. static void add_used(libusb_device *dev, bool lock)
  502. {
  503. struct usb_list *usb_tmp;
  504. char buf[128];
  505. uint8_t bus_number;
  506. uint8_t device_address;
  507. bus_number = libusb_get_bus_number(dev);
  508. device_address = libusb_get_device_address(dev);
  509. if (lock)
  510. mutex_lock(list_lock);
  511. if (in_use(dev, false)) {
  512. if (lock)
  513. mutex_unlock(list_lock);
  514. sprintf(buf, "add_used() duplicate bus_number %d device_address %d",
  515. bus_number, device_address);
  516. quit(1, buf);
  517. }
  518. usb_tmp = malloc(sizeof(*usb_tmp));
  519. usb_tmp->bus_number = bus_number;
  520. usb_tmp->device_address = device_address;
  521. if (usb_head) {
  522. // add to end
  523. usb_tmp->prev = usb_head->prev;
  524. usb_tmp->next = usb_head;
  525. usb_head->prev = usb_tmp;
  526. usb_tmp->prev->next = usb_tmp;
  527. } else {
  528. usb_tmp->prev = usb_tmp;
  529. usb_tmp->next = usb_tmp;
  530. usb_head = usb_tmp;
  531. }
  532. if (lock)
  533. mutex_unlock(list_lock);
  534. }
  535. static void release(uint8_t bus_number, uint8_t device_address, bool lock)
  536. {
  537. struct usb_list *usb_tmp;
  538. bool found = false;
  539. char buf[128];
  540. if (lock)
  541. mutex_lock(list_lock);
  542. usb_tmp = usb_head;
  543. if (usb_tmp)
  544. do {
  545. if (bus_number == usb_tmp->bus_number
  546. && device_address == usb_tmp->device_address) {
  547. found = true;
  548. break;
  549. }
  550. usb_tmp = usb_tmp->next;
  551. } while (usb_tmp != usb_head);
  552. if (!found) {
  553. if (lock)
  554. mutex_unlock(list_lock);
  555. sprintf(buf, "release() unknown: bus_number %d device_address %d",
  556. bus_number, device_address);
  557. quit(1, buf);
  558. }
  559. if (usb_tmp->next == usb_tmp) {
  560. usb_head = NULL;
  561. } else {
  562. usb_tmp->next->prev = usb_tmp->prev;
  563. usb_tmp->prev->next = usb_tmp->next;
  564. }
  565. if (lock)
  566. mutex_unlock(list_lock);
  567. free(usb_tmp);
  568. }
  569. static void release_dev(libusb_device *dev, bool lock)
  570. {
  571. uint8_t bus_number;
  572. uint8_t device_address;
  573. bus_number = libusb_get_bus_number(dev);
  574. device_address = libusb_get_device_address(dev);
  575. release(bus_number, device_address, lock);
  576. }
  577. #if 0
  578. static void release_cgusb(struct cg_usb_device *cgusb, bool lock)
  579. {
  580. release(cgusb->bus_number, cgusb->device_address, lock);
  581. }
  582. #endif
  583. static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
  584. {
  585. if (cgusb->serial_string && cgusb->serial_string != BLANK)
  586. free(cgusb->serial_string);
  587. if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
  588. free(cgusb->manuf_string);
  589. if (cgusb->prod_string && cgusb->prod_string != BLANK)
  590. free(cgusb->prod_string);
  591. free(cgusb->descriptor);
  592. free(cgusb);
  593. return NULL;
  594. }
  595. void usb_uninit(struct cgpu_info *cgpu)
  596. {
  597. libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface);
  598. libusb_close(cgpu->usbdev->handle);
  599. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  600. }
  601. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  602. {
  603. struct cg_usb_device *cgusb = NULL;
  604. struct libusb_config_descriptor *config = NULL;
  605. const struct libusb_interface_descriptor *idesc;
  606. const struct libusb_endpoint_descriptor *epdesc;
  607. unsigned char strbuf[STRBUFLEN+1];
  608. int err, i, j, k;
  609. cgusb = calloc(1, sizeof(*cgusb));
  610. cgusb->found = found;
  611. cgusb->bus_number = libusb_get_bus_number(dev);
  612. cgusb->device_address = libusb_get_device_address(dev);
  613. cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
  614. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  615. if (err) {
  616. applog(LOG_ERR, "USB init failed to get descriptor, err %d", err);
  617. goto dame;
  618. }
  619. err = libusb_open(dev, &(cgusb->handle));
  620. if (err) {
  621. switch (err) {
  622. case LIBUSB_ERROR_ACCESS:
  623. applog(LOG_ERR, "USB init open device failed, err %d, you dont have priviledge to access the device", err);
  624. break;
  625. #ifdef WIN32
  626. // Windows specific message
  627. case LIBUSB_ERROR_NOT_SUPPORTED:
  628. applog(LOG_ERR, "USB init, open device failed, err %d, you need to install a Windows USB driver for the device", err);
  629. break;
  630. #endif
  631. default:
  632. applog(LOG_ERR, "USB init, open device failed, err %d", err);
  633. }
  634. goto dame;
  635. }
  636. if (libusb_kernel_driver_active(cgusb->handle, 0) == 1) {
  637. applog(LOG_WARNING, "USB init, kernel attached ...");
  638. if (libusb_detach_kernel_driver(cgusb->handle, 0) == 0)
  639. applog(LOG_WARNING, "USB init, kernel detached successfully");
  640. else
  641. applog(LOG_WARNING, "USB init, kernel detach failed :(");
  642. }
  643. err = libusb_set_configuration(cgusb->handle, found->config);
  644. if (err) {
  645. switch(err) {
  646. case LIBUSB_ERROR_BUSY:
  647. applog(LOG_WARNING, "USB init, %s device %d:%d in use",
  648. found->name, cgusb->bus_number, cgusb->device_address);
  649. break;
  650. default:
  651. applog(LOG_DEBUG, "USB init, failed to set config to %d, err %d",
  652. found->config, err);
  653. }
  654. goto cldame;
  655. }
  656. err = libusb_get_active_config_descriptor(dev, &config);
  657. if (err) {
  658. applog(LOG_DEBUG, "USB init, failed to get config descriptor %d, err %d",
  659. found->config, err);
  660. goto cldame;
  661. }
  662. if ((int)(config->bNumInterfaces) < found->interface)
  663. goto cldame;
  664. for (i = 0; i < found->epcount; i++)
  665. found->eps[i].found = false;
  666. for (i = 0; i < config->interface[found->interface].num_altsetting; i++) {
  667. idesc = &(config->interface[found->interface].altsetting[i]);
  668. for (j = 0; j < (int)(idesc->bNumEndpoints); j++) {
  669. epdesc = &(idesc->endpoint[j]);
  670. for (k = 0; k < found->epcount; k++) {
  671. if (!found->eps[k].found) {
  672. if (epdesc->bmAttributes == found->eps[k].att
  673. && epdesc->wMaxPacketSize >= found->eps[k].size
  674. && epdesc->bEndpointAddress == found->eps[k].ep) {
  675. found->eps[k].found = true;
  676. break;
  677. }
  678. }
  679. }
  680. }
  681. }
  682. for (i = 0; i < found->epcount; i++)
  683. if (found->eps[i].found == false)
  684. goto cldame;
  685. err = libusb_claim_interface(cgusb->handle, found->interface);
  686. if (err) {
  687. applog(LOG_DEBUG, "USB init, claim interface %d failed, err %d",
  688. found->interface, err);
  689. goto cldame;
  690. }
  691. cgusb->usbver = cgusb->descriptor->bcdUSB;
  692. // TODO: allow this with the right version of the libusb include and running library
  693. // cgusb->speed = libusb_get_device_speed(dev);
  694. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  695. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  696. if (err > 0)
  697. cgusb->prod_string = strdup((char *)strbuf);
  698. else
  699. cgusb->prod_string = (char *)BLANK;
  700. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  701. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  702. if (err > 0)
  703. cgusb->manuf_string = strdup((char *)strbuf);
  704. else
  705. cgusb->manuf_string = (char *)BLANK;
  706. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  707. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  708. if (err > 0)
  709. cgusb->serial_string = strdup((char *)strbuf);
  710. else
  711. cgusb->serial_string = (char *)BLANK;
  712. // TODO: ?
  713. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  714. // cgusb->interfaceVersion
  715. applog(LOG_DEBUG, "USB init device bus_number=%d device_address=%d usbver=%04x prod='%s' manuf='%s' serial='%s'", (int)(cgusb->bus_number), (int)(cgusb->device_address), cgusb->usbver, cgusb->prod_string, cgusb->manuf_string, cgusb->serial_string);
  716. cgpu->usbdev = cgusb;
  717. libusb_free_config_descriptor(config);
  718. return true;
  719. cldame:
  720. libusb_close(cgusb->handle);
  721. dame:
  722. if (config)
  723. libusb_free_config_descriptor(config);
  724. cgusb = free_cgusb(cgusb);
  725. return false;
  726. }
  727. static bool usb_check_device(struct device_api *api, struct libusb_device *dev, struct usb_find_devices *look)
  728. {
  729. struct libusb_device_descriptor desc;
  730. int err;
  731. err = libusb_get_device_descriptor(dev, &desc);
  732. if (err) {
  733. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  734. return false;
  735. }
  736. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  737. applog(LOG_DEBUG, "%s looking for %04x:%04x but found %04x:%04x instead",
  738. api->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  739. return false;
  740. }
  741. applog(LOG_DEBUG, "%s looking for and found %04x:%04x",
  742. api->name, look->idVendor, look->idProduct);
  743. return true;
  744. }
  745. static struct usb_find_devices *usb_check_each(int drv, struct device_api *api, struct libusb_device *dev)
  746. {
  747. struct usb_find_devices *found;
  748. int i;
  749. for (i = 0; find_dev[i].drv != DRV_LAST; i++)
  750. if (find_dev[i].drv == drv) {
  751. if (usb_check_device(api, dev, &(find_dev[i]))) {
  752. found = malloc(sizeof(*found));
  753. memcpy(found, &(find_dev[i]), sizeof(*found));
  754. return found;
  755. }
  756. }
  757. return NULL;
  758. }
  759. static struct usb_find_devices *usb_check(__maybe_unused struct device_api *api, __maybe_unused struct libusb_device *dev)
  760. {
  761. #ifdef USE_BITFORCE
  762. if (api == &bitforce_api)
  763. return usb_check_each(DRV_BITFORCE, api, dev);
  764. #endif
  765. #ifdef USE_ICARUS
  766. if (api == &icarus_api)
  767. return usb_check_each(DRV_ICARUS, api, dev);
  768. #endif
  769. #ifdef USE_AVALON
  770. if (api == &avalon_api)
  771. return usb_check_each(DRV_AVALON, api, dev);
  772. #endif
  773. #ifdef USE_MODMINER
  774. if (api == &modminer_api)
  775. return usb_check_each(DRV_MODMINER, api, dev);
  776. #endif
  777. return NULL;
  778. }
  779. void usb_detect(struct device_api *api, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
  780. {
  781. libusb_device **list;
  782. ssize_t count, i;
  783. struct usb_find_devices *found;
  784. cgusb_check_init();
  785. count = libusb_get_device_list(NULL, &list);
  786. if (count < 0) {
  787. applog(LOG_DEBUG, "USB scan devices: failed, err %d", count);
  788. return;
  789. }
  790. if (count == 0)
  791. applog(LOG_DEBUG, "USB scan devices: found no devices");
  792. for (i = 0; i < count; i++) {
  793. mutex_lock(list_lock);
  794. if (in_use(list[i], false))
  795. mutex_unlock(list_lock);
  796. else {
  797. add_used(list[i], false);
  798. mutex_unlock(list_lock);
  799. found = usb_check(api, list[i]);
  800. if (!found)
  801. release_dev(list[i], true);
  802. else
  803. if (!device_detect(list[i], found))
  804. release_dev(list[i], true);
  805. }
  806. }
  807. libusb_free_device_list(list, 1);
  808. }
  809. // Set this to 0 to remove stats processing
  810. #define DO_USB_STATS 1
  811. #if DO_USB_STATS
  812. #define USB_STATS(sgpu, sta, fin, err, cmd, seq) stats(cgpu, sta, fin, err, cmd, seq)
  813. #define STATS_TIMEVAL(tv) gettimeofday(tv, NULL)
  814. #else
  815. #define USB_STATS(sgpu, sta, fin, err, cmd, seq)
  816. #define STATS_TIMEVAL(tv)
  817. #endif
  818. // The stat data can be spurious due to not locking it before copying it -
  819. // however that would require the stat() function to also lock and release
  820. // a mutex every time a usb read or write is called which would slow
  821. // things down more
  822. struct api_data *api_usb_stats(__maybe_unused int *count)
  823. {
  824. #if DO_USB_STATS
  825. struct cg_usb_stats_details *details;
  826. struct cg_usb_stats *sta;
  827. struct api_data *root = NULL;
  828. int device;
  829. int cmdseq;
  830. cgusb_check_init();
  831. if (next_stat == 0)
  832. return NULL;
  833. while (*count < next_stat * C_MAX * 2) {
  834. device = *count / (C_MAX * 2);
  835. cmdseq = *count % (C_MAX * 2);
  836. (*count)++;
  837. sta = &(usb_stats[device]);
  838. details = &(sta->details[cmdseq]);
  839. // Only show stats that have results
  840. if (details->item[CMD_CMD].count == 0 &&
  841. details->item[CMD_TIMEOUT].count == 0 &&
  842. details->item[CMD_ERROR].count == 0)
  843. continue;
  844. root = api_add_string(root, "Name", sta->name, false);
  845. root = api_add_int(root, "ID", &(sta->device_id), false);
  846. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  847. root = api_add_int(root, "Seq", &(details->seq), true);
  848. root = api_add_uint64(root, "Count",
  849. &(details->item[CMD_CMD].count), true);
  850. root = api_add_double(root, "Total Delay",
  851. &(details->item[CMD_CMD].total_delay), true);
  852. root = api_add_double(root, "Min Delay",
  853. &(details->item[CMD_CMD].min_delay), true);
  854. root = api_add_double(root, "Max Delay",
  855. &(details->item[CMD_CMD].max_delay), true);
  856. root = api_add_uint64(root, "Timeout Count",
  857. &(details->item[CMD_TIMEOUT].count), true);
  858. root = api_add_double(root, "Timeout Total Delay",
  859. &(details->item[CMD_TIMEOUT].total_delay), true);
  860. root = api_add_double(root, "Timeout Min Delay",
  861. &(details->item[CMD_TIMEOUT].min_delay), true);
  862. root = api_add_double(root, "Timeout Max Delay",
  863. &(details->item[CMD_TIMEOUT].max_delay), true);
  864. root = api_add_uint64(root, "Error Count",
  865. &(details->item[CMD_ERROR].count), true);
  866. root = api_add_double(root, "Error Total Delay",
  867. &(details->item[CMD_ERROR].total_delay), true);
  868. root = api_add_double(root, "Error Min Delay",
  869. &(details->item[CMD_ERROR].min_delay), true);
  870. root = api_add_double(root, "Error Max Delay",
  871. &(details->item[CMD_ERROR].max_delay), true);
  872. root = api_add_timeval(root, "First Command",
  873. &(details->item[CMD_CMD].first), true);
  874. root = api_add_timeval(root, "Last Command",
  875. &(details->item[CMD_CMD].last), true);
  876. root = api_add_timeval(root, "First Timeout",
  877. &(details->item[CMD_TIMEOUT].first), true);
  878. root = api_add_timeval(root, "Last Timeout",
  879. &(details->item[CMD_TIMEOUT].last), true);
  880. root = api_add_timeval(root, "First Error",
  881. &(details->item[CMD_ERROR].first), true);
  882. root = api_add_timeval(root, "Last Error",
  883. &(details->item[CMD_ERROR].last), true);
  884. return root;
  885. }
  886. #endif
  887. return NULL;
  888. }
  889. #if DO_USB_STATS
  890. static void newstats(struct cgpu_info *cgpu)
  891. {
  892. int i;
  893. cgpu->usbstat = ++next_stat;
  894. usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
  895. usb_stats[next_stat-1].name = cgpu->api->name;
  896. usb_stats[next_stat-1].device_id = -1;
  897. usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
  898. for (i = 1; i < C_MAX * 2; i += 2)
  899. usb_stats[next_stat-1].details[i].seq = 1;
  900. }
  901. #endif
  902. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  903. {
  904. #if DO_USB_STATS
  905. if (cgpu->usbstat < 1)
  906. newstats(cgpu);
  907. // we don't know the device_id until after add_cgpu()
  908. usb_stats[cgpu->usbstat - 1].device_id = cgpu->device_id;
  909. #endif
  910. }
  911. #if DO_USB_STATS
  912. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, enum usb_cmds cmd, int seq)
  913. {
  914. struct cg_usb_stats_details *details;
  915. double diff;
  916. int item;
  917. if (cgpu->usbstat < 1)
  918. newstats(cgpu);
  919. details = &(usb_stats[cgpu->usbstat - 1].details[cmd * 2 + seq]);
  920. diff = tdiff(tv_finish, tv_start);
  921. switch (err) {
  922. case LIBUSB_SUCCESS:
  923. item = CMD_CMD;
  924. break;
  925. case LIBUSB_ERROR_TIMEOUT:
  926. item = CMD_TIMEOUT;
  927. break;
  928. default:
  929. item = CMD_ERROR;
  930. break;
  931. }
  932. if (details->item[item].count == 0) {
  933. details->item[item].min_delay = diff;
  934. memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  935. } else if (diff < details->item[item].min_delay)
  936. details->item[item].min_delay = diff;
  937. if (diff > details->item[item].max_delay)
  938. details->item[item].max_delay = diff;
  939. details->item[item].total_delay += diff;
  940. memcpy(&(details->item[item].last), tv_start, sizeof(tv_start));
  941. details->item[item].count++;
  942. }
  943. #endif
  944. 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)
  945. {
  946. struct cg_usb_device *usbdev = cgpu->usbdev;
  947. #if DO_USB_STATS
  948. struct timeval tv_start, tv_finish;
  949. #endif
  950. int err, got, tot;
  951. bool first = true;
  952. if (eol == -1) {
  953. got = 0;
  954. STATS_TIMEVAL(&tv_start);
  955. err = libusb_bulk_transfer(usbdev->handle,
  956. usbdev->found->eps[ep].ep,
  957. (unsigned char *)buf,
  958. bufsiz, &got,
  959. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  960. STATS_TIMEVAL(&tv_finish);
  961. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  962. *processed = got;
  963. return err;
  964. }
  965. tot = 0;
  966. err = LIBUSB_SUCCESS;
  967. while (bufsiz) {
  968. got = 0;
  969. STATS_TIMEVAL(&tv_start);
  970. err = libusb_bulk_transfer(usbdev->handle,
  971. usbdev->found->eps[ep].ep,
  972. (unsigned char *)buf,
  973. 1, &got,
  974. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  975. STATS_TIMEVAL(&tv_finish);
  976. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, first ? SEQ0 : SEQ1);
  977. tot += got;
  978. if (err)
  979. break;
  980. if (eol == buf[0])
  981. break;
  982. buf += got;
  983. bufsiz -= got;
  984. first = false;
  985. }
  986. *processed = tot;
  987. return err;
  988. }
  989. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
  990. {
  991. struct cg_usb_device *usbdev = cgpu->usbdev;
  992. #if DO_USB_STATS
  993. struct timeval tv_start, tv_finish;
  994. #endif
  995. int err, sent;
  996. sent = 0;
  997. STATS_TIMEVAL(&tv_start);
  998. err = libusb_bulk_transfer(usbdev->handle,
  999. usbdev->found->eps[ep].ep,
  1000. (unsigned char *)buf,
  1001. bufsiz, &sent,
  1002. timeout == DEVTIMEOUT ? usbdev->found->timeout : timeout);
  1003. STATS_TIMEVAL(&tv_finish);
  1004. USB_STATS(cgpu, &tv_start, &tv_finish, err, cmd, SEQ0);
  1005. *processed = sent;
  1006. return err;
  1007. }
  1008. void usb_cleanup()
  1009. {
  1010. // TODO:
  1011. }