usbutils.c 32 KB

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