usbutils.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #ifndef USBUTILS_H
  11. #define USBUTILS_H
  12. #include <libusb.h>
  13. #include "util.h"
  14. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  15. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  16. // For 0x0403:0x6014/0x6001 FT232H (and possibly others?) - BFL, BAS, BLT, LLT, AVA
  17. #define FTDI_TYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
  18. #define FTDI_TYPE_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
  19. #define FTDI_REQUEST_RESET ((uint8_t)0)
  20. #define FTDI_REQUEST_MODEM ((uint8_t)1)
  21. #define FTDI_REQUEST_FLOW ((uint8_t)2)
  22. #define FTDI_REQUEST_BAUD ((uint8_t)3)
  23. #define FTDI_REQUEST_DATA ((uint8_t)4)
  24. #define FTDI_REQUEST_LATENCY ((uint8_t)9)
  25. #define FTDI_REQUEST_BITMODE ((uint8_t)11)
  26. #define FTDI_BITMODE_RESET ((uint8_t)0x00)
  27. #define FTDI_BITMODE_BITBANG ((uint8_t)0x01)
  28. #define FTDI_BITMODE_MPSSE ((uint8_t)0x02)
  29. #define FTDI_BITMODE_SYNCBB ((uint8_t)0x04)
  30. #define FTDI_BITMODE_MCU ((uint8_t)0x08)
  31. #define FTDI_BITMODE_OPTO ((uint8_t)0x10)
  32. #define FTDI_BITMODE_CBUS ((uint8_t)0x20)
  33. #define FTDI_BITMODE_SYNCFF ((uint8_t)0x40)
  34. #define FTDI_BITMODE_FT1284 ((uint8_t)0x80)
  35. #define FTDI_SETDTR_LOW 0x0100
  36. #define FTDI_SETDTR_HIGH 0x0101
  37. #define FTDI_SETRTS_LOW 0x0200
  38. #define FTDI_SETRTS_HIGH 0x0202
  39. #define FTDI_VALUE_RESET 0
  40. #define FTDI_VALUE_PURGE_RX 1
  41. #define FTDI_VALUE_PURGE_TX 2
  42. #define FTDI_VALUE_LATENCY 1
  43. // Baud
  44. #define FTDI_VALUE_BAUD_BFL 0xc068
  45. #define FTDI_INDEX_BAUD_BFL 0x0200
  46. #define FTDI_VALUE_BAUD_BAS FTDI_VALUE_BAUD_BFL
  47. #define FTDI_INDEX_BAUD_BAS FTDI_INDEX_BAUD_BFL
  48. // LLT = BLT (same code)
  49. #define FTDI_VALUE_BAUD_BLT 0x001a
  50. #define FTDI_INDEX_BAUD_BLT 0x0000
  51. // Avalon
  52. #define FTDI_VALUE_BAUD_AVA 0x001A
  53. #define FTDI_INDEX_BAUD_AVA 0x0000
  54. #define FTDI_VALUE_DATA_AVA 8
  55. // BitBurner
  56. #define BITBURNER_REQUEST ((uint8_t)0x42)
  57. #define BITBURNER_VALUE 0x4242
  58. #define BITBURNER_INDEX_SET_VOLTAGE 1
  59. #define BITBURNER_INDEX_GET_VOLTAGE 2
  60. #define BITBURNER_INDEX_GET_VERSION 4
  61. // CMR = 115200 & 57600
  62. #define FTDI_VALUE_BAUD_CMR_115 0xc068
  63. #define FTDI_INDEX_BAUD_CMR_115 0x0200
  64. #define FTDI_VALUE_BAUD_CMR_57 0x80d0
  65. #define FTDI_INDEX_BAUD_CMR_57 0x0200
  66. // FTDI
  67. #define FTDI_VALUE_DATA_BTS 8
  68. #define FTDI_VALUE_BAUD_BTS 0x001A
  69. #define FTDI_INDEX_BAUD_BTS 0x0000
  70. // Data control
  71. #define FTDI_VALUE_DATA_BFL 0
  72. #define FTDI_VALUE_DATA_BAS FTDI_VALUE_DATA_BFL
  73. // LLT = BLT (same code)
  74. #define FTDI_VALUE_DATA_BLT 8
  75. #define FTDI_VALUE_FLOW 0
  76. #define FTDI_VALUE_MODEM 0x0303
  77. // For 0x10c4:0xea60 USB cp210x chip - AMU
  78. #define CP210X_TYPE_OUT 0x41
  79. #define CP210X_REQUEST_IFC_ENABLE 0x00
  80. #define CP210X_REQUEST_DATA 0x07
  81. #define CP210X_REQUEST_BAUD 0x1e
  82. #define CP210X_VALUE_UART_ENABLE 0x0001
  83. #define CP210X_VALUE_DATA 0x0303
  84. #define CP210X_DATA_BAUD 0x0001c200
  85. #define CP210X_SET_LINE_CTL 0x03
  86. #define CP210X_BITS_DATA_MASK 0x0f00
  87. #define CP210X_BITS_DATA_8 0x0800
  88. #define CP210X_BITS_PARITY_MARK 0x0030
  89. #define CP210X_BITS_PARITY_SPACE 0x0040
  90. // For 0x067b:0x2303 Prolific PL2303 - ICA
  91. #define PL2303_CTRL_DTR 0x01
  92. #define PL2303_CTRL_RTS 0x02
  93. #define PL2303_CTRL_OUT 0x21
  94. #define PL2303_VENDOR_OUT 0x40
  95. #define PL2303_REQUEST_CTRL 0x22
  96. #define PL2303_REQUEST_LINE 0x20
  97. #define PL2303_REQUEST_VENDOR 0x01
  98. #define PL2303_REPLY_CTRL 0x21
  99. #define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
  100. #define PL2303_VALUE_LINE 0
  101. #define PL2303_VALUE_LINE0 0x0001c200
  102. #define PL2303_VALUE_LINE1 0x080000
  103. #define PL2303_VALUE_LINE_SIZE 7
  104. #define PL2303_VALUE_VENDOR 0
  105. // Use the device defined timeout
  106. #define DEVTIMEOUT 0
  107. // The default intinfo structure used is the first one
  108. #define DEFAULT_INTINFO 0
  109. // For endpoints defined in usb_find_devices.intinfos.epinfos,
  110. // the first two must be the default IN and OUT and both must always exist
  111. #define DEFAULT_EP_IN 0
  112. #define DEFAULT_EP_OUT 1
  113. struct usb_epinfo {
  114. uint8_t att;
  115. uint16_t size;
  116. unsigned char ep;
  117. uint16_t wMaxPacketSize;
  118. bool found;
  119. };
  120. struct usb_intinfo {
  121. int interface;
  122. int ctrl_transfer;
  123. int epinfo_count;
  124. struct usb_epinfo *epinfos;
  125. };
  126. enum sub_ident {
  127. IDENT_UNK = 0,
  128. IDENT_AMU,
  129. IDENT_ANT,
  130. IDENT_ANU,
  131. IDENT_AS3,
  132. IDENT_AU3,
  133. IDENT_AVA,
  134. IDENT_AV2,
  135. IDENT_AV4,
  136. IDENT_AV7,
  137. IDENT_AV8,
  138. IDENT_AV9,
  139. IDENT_AVLC3,
  140. IDENT_AVM,
  141. IDENT_BAJ,
  142. IDENT_BAL,
  143. IDENT_BAM,
  144. IDENT_BAS,
  145. IDENT_BBF,
  146. IDENT_BET,
  147. IDENT_BF1,
  148. IDENT_BFL,
  149. IDENT_BLT,
  150. IDENT_BMA,
  151. IDENT_BSC,
  152. IDENT_BSD,
  153. IDENT_BSE,
  154. IDENT_BTB,
  155. IDENT_BXF,
  156. IDENT_BXM,
  157. IDENT_CMR1,
  158. IDENT_CMR2,
  159. IDENT_CTA,
  160. IDENT_DRB,
  161. IDENT_GSC,
  162. IDENT_GSD,
  163. IDENT_GSE,
  164. IDENT_GSH,
  165. IDENT_BAX,
  166. IDENT_GSI,
  167. IDENT_GSF,
  168. IDENT_GSFM,
  169. IDENT_HFA,
  170. IDENT_HRO,
  171. IDENT_ICA,
  172. IDENT_KLN,
  173. IDENT_LIN,
  174. IDENT_LLT,
  175. IDENT_MMQ,
  176. IDENT_NFU,
  177. IDENT_OSM
  178. };
  179. struct usb_find_devices {
  180. int drv;
  181. const char *name;
  182. enum sub_ident ident;
  183. uint16_t idVendor;
  184. uint16_t idProduct;
  185. char *iManufacturer;
  186. char *iProduct;
  187. int config;
  188. unsigned int timeout;
  189. uint16_t latency;
  190. int intinfo_count;
  191. struct usb_intinfo *intinfos;
  192. };
  193. /* Latency is set to 32ms to prevent a transfer ever being more than 512 bytes
  194. * +2 bytes of status such as the ftdi chip, when the chips emulate a 115200
  195. * baud rate, to avoid status bytes being interleaved in larger transfers. */
  196. #define LATENCY_UNUSED 0
  197. #define LATENCY_STD 32
  198. #define LATENCY_ANTS1 10
  199. #define LATENCY_ANTS3 10
  200. enum usb_types {
  201. USB_TYPE_STD = 0,
  202. USB_TYPE_FTDI
  203. };
  204. #define USB_MAX_READ 8192
  205. /*
  206. * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
  207. * If a single device ever has multiple end points then it will need
  208. * multiple of these
  209. */
  210. #define USB_READ_BUFSIZE (USB_MAX_READ + 4)
  211. struct cg_usb_device {
  212. struct usb_find_devices *found;
  213. libusb_device_handle *handle;
  214. pthread_mutex_t *mutex;
  215. struct libusb_device_descriptor *descriptor;
  216. enum usb_types usb_type;
  217. enum sub_ident ident;
  218. uint16_t usbver;
  219. char *prod_string;
  220. char *manuf_string;
  221. char *serial_string;
  222. unsigned char fwVersion; // ??
  223. unsigned char interfaceVersion; // ??
  224. char buffer[USB_MAX_READ];
  225. uint32_t bufsiz;
  226. uint32_t bufamt;
  227. bool usb11; // USB 1.1 flag for convenience
  228. bool tt; // Enable the transaction translator
  229. };
  230. #define USB_NOSTAT 0
  231. #define USB_TMO_0 50
  232. #define USB_TMO_1 100
  233. #define USB_TMO_2 500
  234. #define USB_TMOS 3
  235. struct cg_usb_tmo {
  236. uint32_t count;
  237. uint32_t min_tmo;
  238. uint32_t max_tmo;
  239. uint64_t total_over;
  240. uint64_t total_tmo;
  241. };
  242. struct cg_usb_info {
  243. uint8_t bus_number;
  244. uint8_t device_address;
  245. int usbstat;
  246. bool nodev;
  247. bool initialised;
  248. int nodev_count;
  249. struct timeval last_nodev;
  250. uint32_t ioerr_count;
  251. uint32_t continuous_ioerr_count;
  252. /*
  253. * for nodev and cgusb access (read and write)
  254. * it's a pointer so MMQ can have it in multiple devices
  255. *
  256. * N.B. general mining code doesn't need to use the read
  257. * lock for 'nodev' if it calls a usb_read/write/etc function
  258. * that uses the lock - however, all usbutils code MUST use it
  259. * to avoid devices disappearing while in use by multiple threads
  260. */
  261. cglock_t devlock;
  262. time_t last_pipe;
  263. uint64_t pipe_count;
  264. uint64_t clear_err_count;
  265. uint64_t retry_err_count;
  266. uint64_t clear_fail_count;
  267. uint64_t read_delay_count;
  268. double total_read_delay;
  269. uint64_t write_delay_count;
  270. double total_write_delay;
  271. uint64_t tmo_count;
  272. struct cg_usb_tmo usb_tmo[USB_TMOS];
  273. };
  274. #define ENUMERATION(a,b) a,
  275. #define JUMPTABLE(a,b) b,
  276. #define USB_PARSE_COMMANDS(USB_ADD_COMMAND) \
  277. USB_ADD_COMMAND(C_REJECTED, "RejectedNoDevice") \
  278. USB_ADD_COMMAND(C_PING, "Ping") \
  279. USB_ADD_COMMAND(C_CLEAR, "Clear") \
  280. USB_ADD_COMMAND(C_REQUESTVERSION, "RequestVersion") \
  281. USB_ADD_COMMAND(C_GETVERSION, "GetVersion") \
  282. USB_ADD_COMMAND(C_REQUESTFPGACOUNT, "RequestFPGACount") \
  283. USB_ADD_COMMAND(C_GETFPGACOUNT, "GetFPGACount") \
  284. USB_ADD_COMMAND(C_STARTPROGRAM, "StartProgram") \
  285. USB_ADD_COMMAND(C_STARTPROGRAMSTATUS, "StartProgramStatus") \
  286. USB_ADD_COMMAND(C_PROGRAM, "Program") \
  287. USB_ADD_COMMAND(C_PROGRAMSTATUS, "ProgramStatus") \
  288. USB_ADD_COMMAND(C_PROGRAMSTATUS2, "ProgramStatus2") \
  289. USB_ADD_COMMAND(C_FINALPROGRAMSTATUS, "FinalProgramStatus") \
  290. USB_ADD_COMMAND(C_SETCLOCK, "SetClock") \
  291. USB_ADD_COMMAND(C_SETPARITY, "SetParity") \
  292. USB_ADD_COMMAND(C_REPLYSETCLOCK, "ReplySetClock") \
  293. USB_ADD_COMMAND(C_SETVOLT, "SetVolt") \
  294. USB_ADD_COMMAND(C_REPLYSETVOLT, "ReplySetVolt") \
  295. USB_ADD_COMMAND(C_REQUESTUSERCODE, "RequestUserCode") \
  296. USB_ADD_COMMAND(C_GETUSERCODE, "GetUserCode") \
  297. USB_ADD_COMMAND(C_REQUESTTEMPERATURE, "RequestTemperature") \
  298. USB_ADD_COMMAND(C_GETTEMPERATURE, "GetTemperature") \
  299. USB_ADD_COMMAND(C_SENDWORK, "SendWork") \
  300. USB_ADD_COMMAND(C_SENDWORKSTATUS, "SendWorkStatus") \
  301. USB_ADD_COMMAND(C_REQUESTWORKSTATUS, "RequestWorkStatus") \
  302. USB_ADD_COMMAND(C_GETWORKSTATUS, "GetWorkStatus") \
  303. USB_ADD_COMMAND(C_REQUESTIDENTIFY, "RequestIdentify") \
  304. USB_ADD_COMMAND(C_GETIDENTIFY, "GetIdentify") \
  305. USB_ADD_COMMAND(C_REQUESTFLASH, "RequestFlash") \
  306. USB_ADD_COMMAND(C_REQUESTSENDWORK, "RequestSendWork") \
  307. USB_ADD_COMMAND(C_REQUESTSENDWORKSTATUS, "RequestSendWorkStatus") \
  308. USB_ADD_COMMAND(C_RESET, "Reset") \
  309. USB_ADD_COMMAND(C_SETBAUD, "SetBaud") \
  310. USB_ADD_COMMAND(C_SETDATA, "SetDataCtrl") \
  311. USB_ADD_COMMAND(C_SETFLOW, "SetFlowCtrl") \
  312. USB_ADD_COMMAND(C_SETMODEM, "SetModemCtrl") \
  313. USB_ADD_COMMAND(C_PURGERX, "PurgeRx") \
  314. USB_ADD_COMMAND(C_PURGETX, "PurgeTx") \
  315. USB_ADD_COMMAND(C_FLASHREPLY, "FlashReply") \
  316. USB_ADD_COMMAND(C_REQUESTDETAILS, "RequestDetails") \
  317. USB_ADD_COMMAND(C_GETDETAILS, "GetDetails") \
  318. USB_ADD_COMMAND(C_REQUESTRESULTS, "RequestResults") \
  319. USB_ADD_COMMAND(C_GETRESULTS, "GetResults") \
  320. USB_ADD_COMMAND(C_REQUESTQUEJOB, "RequestQueJob") \
  321. USB_ADD_COMMAND(C_REQUESTQUEJOBSTATUS, "RequestQueJobStatus") \
  322. USB_ADD_COMMAND(C_QUEJOB, "QueJob") \
  323. USB_ADD_COMMAND(C_QUEJOBSTATUS, "QueJobStatus") \
  324. USB_ADD_COMMAND(C_QUEFLUSH, "QueFlush") \
  325. USB_ADD_COMMAND(C_QUEFLUSHREPLY, "QueFlushReply") \
  326. USB_ADD_COMMAND(C_REQUESTVOLTS, "RequestVolts") \
  327. USB_ADD_COMMAND(C_GETVOLTS, "GetVolts") \
  328. USB_ADD_COMMAND(C_SENDTESTWORK, "SendTestWork") \
  329. USB_ADD_COMMAND(C_LATENCY, "SetLatency") \
  330. USB_ADD_COMMAND(C_SETLINE, "SetLine") \
  331. USB_ADD_COMMAND(C_VENDOR, "Vendor") \
  332. USB_ADD_COMMAND(C_SETFAN, "SetFan") \
  333. USB_ADD_COMMAND(C_FANREPLY, "GetFan") \
  334. USB_ADD_COMMAND(C_AVALON_TASK, "AvalonTask") \
  335. USB_ADD_COMMAND(C_AVALON_READ, "AvalonRead") \
  336. USB_ADD_COMMAND(C_GET_AVALON_READY, "AvalonReady") \
  337. USB_ADD_COMMAND(C_AVALON_RESET, "AvalonReset") \
  338. USB_ADD_COMMAND(C_GET_AVALON_RESET, "GetAvalonReset") \
  339. USB_ADD_COMMAND(C_FTDI_STATUS, "FTDIStatus") \
  340. USB_ADD_COMMAND(C_ENABLE_UART, "EnableUART") \
  341. USB_ADD_COMMAND(C_ANU_SEND_CMD, "ANUSendcmd") \
  342. USB_ADD_COMMAND(C_ANU_SEND_RDREG, "ANUSendrdreg") \
  343. USB_ADD_COMMAND(C_ANU_SEND_VOLT, "ANUSendvolt") \
  344. USB_ADD_COMMAND(C_BB_SET_VOLTAGE, "SetCoreVoltage") \
  345. USB_ADD_COMMAND(C_BB_GET_VOLTAGE, "GetCoreVoltage") \
  346. USB_ADD_COMMAND(C_BF_RESET, "BFReset") \
  347. USB_ADD_COMMAND(C_BF_OPEN, "BFOpen") \
  348. USB_ADD_COMMAND(C_BF_INIT, "BFInit") \
  349. USB_ADD_COMMAND(C_BF_CLOSE, "BFClose") \
  350. USB_ADD_COMMAND(C_BF_REQINFO, "BFRequestInfo") \
  351. USB_ADD_COMMAND(C_BF_GETINFO, "BFGetInfo") \
  352. USB_ADD_COMMAND(C_BF_REQRESET, "BFRequestReset") \
  353. USB_ADD_COMMAND(C_BF_GETRESET, "BFGetReset") \
  354. USB_ADD_COMMAND(C_BF_REQWORK, "BFRequestWork") \
  355. USB_ADD_COMMAND(C_BF_GETWORK, "BFGetWork") \
  356. USB_ADD_COMMAND(C_BF_GETRES, "BFGetResults") \
  357. USB_ADD_COMMAND(C_BF_FLUSH, "BFFlush") \
  358. USB_ADD_COMMAND(C_BF_IFLUSH, "BFInterruptFlush") \
  359. USB_ADD_COMMAND(C_BF_IDENTIFY, "BFIdentify") \
  360. USB_ADD_COMMAND(C_BF_DETECTCHIPS, "BFDetectChips") \
  361. USB_ADD_COMMAND(C_BF_CONFIG, "BFConfig") \
  362. USB_ADD_COMMAND(C_BF_GETTEMP, "BFGetTemp") \
  363. USB_ADD_COMMAND(C_BF_AUTOTUNE, "BFAutoTune") \
  364. USB_ADD_COMMAND(C_ATMEL_RESET, "AtmelReset") \
  365. USB_ADD_COMMAND(C_ATMEL_OPEN, "AtmelOpen") \
  366. USB_ADD_COMMAND(C_ATMEL_INIT, "AtmelInit") \
  367. USB_ADD_COMMAND(C_ATMEL_CLOSE, "AtmelClose") \
  368. USB_ADD_COMMAND(C_AVA2_READ, "Ava2Read") \
  369. USB_ADD_COMMAND(C_AVA2_WRITE, "Ava2Write") \
  370. USB_ADD_COMMAND(C_AVA4_READ, "Ava4Read") \
  371. USB_ADD_COMMAND(C_AVA4_WRITE, "Ava4Write") \
  372. USB_ADD_COMMAND(C_AVA7_READ, "Ava7Read") \
  373. USB_ADD_COMMAND(C_AVA7_WRITE, "Ava7Write") \
  374. USB_ADD_COMMAND(C_AVA8_READ, "Ava8Read") \
  375. USB_ADD_COMMAND(C_AVA8_WRITE, "Ava8Write") \
  376. USB_ADD_COMMAND(C_AVA9_READ, "Ava9Read") \
  377. USB_ADD_COMMAND(C_AVA9_WRITE, "Ava9Write") \
  378. USB_ADD_COMMAND(C_AVALC3_READ, "Avalc3Read") \
  379. USB_ADD_COMMAND(C_AVALC3_WRITE, "Avalc3Write") \
  380. USB_ADD_COMMAND(C_AVAM_READ, "AvamRead") \
  381. USB_ADD_COMMAND(C_AVAM_WRITE, "AvamWrite") \
  382. USB_ADD_COMMAND(C_BET_WRITE, "BlockErupterWrite") \
  383. USB_ADD_COMMAND(C_BET_READ, "BlockErupterRead") \
  384. USB_ADD_COMMAND(C_BF1_REQINFO, "BF1RequestInfo") \
  385. USB_ADD_COMMAND(C_BF1_GETINFO, "BF1GetInfo") \
  386. USB_ADD_COMMAND(C_BF1_REQRESET, "BF1RequestReset") \
  387. USB_ADD_COMMAND(C_BF1_GETRESET, "BF1GetReset") \
  388. USB_ADD_COMMAND(C_BF1_REQWORK, "BF1RequestWork") \
  389. USB_ADD_COMMAND(C_BF1_GETWORK, "BF1GetWork") \
  390. USB_ADD_COMMAND(C_BF1_GETRES, "BF1GetResults") \
  391. USB_ADD_COMMAND(C_BF1_FLUSH, "BF1Flush") \
  392. USB_ADD_COMMAND(C_BF1_IFLUSH, "BF1InterruptFlush") \
  393. USB_ADD_COMMAND(C_BF1_IDENTIFY, "BF1Identify") \
  394. USB_ADD_COMMAND(C_BXF_READ, "BXFRead") \
  395. USB_ADD_COMMAND(C_BXF_WORK, "BXFWork") \
  396. USB_ADD_COMMAND(C_BXF_TARGET, "BXFTarget") \
  397. USB_ADD_COMMAND(C_BXF_VERSION, "BXFVersion") \
  398. USB_ADD_COMMAND(C_BXF_MAXROLL, "BXFMaxRoll") \
  399. USB_ADD_COMMAND(C_BXF_FLUSH, "BXFFlush") \
  400. USB_ADD_COMMAND(C_BXF_CLOCK, "BXFClock") \
  401. USB_ADD_COMMAND(C_BXF_LEDMODE, "BXFLedMode") \
  402. USB_ADD_COMMAND(C_BXF_DEBUGMODE, "BXFDebugMode") \
  403. USB_ADD_COMMAND(C_BXM_FLUSH, "BXMFlush") \
  404. USB_ADD_COMMAND(C_BXM_SRESET, "BXMSReset") \
  405. USB_ADD_COMMAND(C_BXM_SETLATENCY, "BXMSetLatency") \
  406. USB_ADD_COMMAND(C_BXM_SECR, "BXMSetEventCharRequest") \
  407. USB_ADD_COMMAND(C_BXM_SETBITMODE, "BXMSetBitmodeRequest") \
  408. USB_ADD_COMMAND(C_BXM_CLOCK, "BXMClock") \
  409. USB_ADD_COMMAND(C_BXM_CLOCKDIV, "BXMClockDiv") \
  410. USB_ADD_COMMAND(C_BXM_LOOP, "BXMLoop") \
  411. USB_ADD_COMMAND(C_BXM_ADBUS, "BXMADBus") \
  412. USB_ADD_COMMAND(C_BXM_ACBUS, "BXMACBus") \
  413. USB_ADD_COMMAND(C_BXM_PURGERX, "BXMPurgeRX") \
  414. USB_ADD_COMMAND(C_BXM_PURGETX, "BXMPurgeTX") \
  415. USB_ADD_COMMAND(C_BXM_CSLOW, "BXMCSLow") \
  416. USB_ADD_COMMAND(C_BXM_CSHIGH, "BXMCSHigh") \
  417. USB_ADD_COMMAND(C_BXM_RESET, "BXMReset") \
  418. USB_ADD_COMMAND(C_BXM_SPITX, "BXMSPITX") \
  419. USB_ADD_COMMAND(C_BXM_SPIRX, "BXMSPIRX") \
  420. USB_ADD_COMMAND(C_HF_RESET, "HFReset") \
  421. USB_ADD_COMMAND(C_HF_PLL_CONFIG, "HFPLLConfig") \
  422. USB_ADD_COMMAND(C_HF_ADDRESS, "HFAddress") \
  423. USB_ADD_COMMAND(C_HF_BAUD, "HFBaud") \
  424. USB_ADD_COMMAND(C_HF_HASH, "HFHash") \
  425. USB_ADD_COMMAND(C_HF_NONCE, "HFNonce") \
  426. USB_ADD_COMMAND(C_HF_ABORT, "HFAbort") \
  427. USB_ADD_COMMAND(C_HF_STATUS, "HFStatus") \
  428. USB_ADD_COMMAND(C_HF_CONFIG, "HFConfig") \
  429. USB_ADD_COMMAND(C_HF_STATISTICS, "HFStatistics") \
  430. USB_ADD_COMMAND(C_HF_CLOCKGATE, "HFClockGate") \
  431. USB_ADD_COMMAND(C_HF_USB_INIT, "HFUSBInit") \
  432. USB_ADD_COMMAND(C_HF_DFU, "HFDFU") \
  433. USB_ADD_COMMAND(C_HF_DIE_STATUS, "HFDieStatus") \
  434. USB_ADD_COMMAND(C_HF_GWQ_STATUS, "HFGWQStatus") \
  435. USB_ADD_COMMAND(C_HF_WORK_RESTART, "HFWorkRestart") \
  436. USB_ADD_COMMAND(C_HF_GWQSTATS, "HFGWQStats") \
  437. USB_ADD_COMMAND(C_HF_NOTICE, "HFNotice") \
  438. USB_ADD_COMMAND(C_HF_PING, "HFPing") \
  439. USB_ADD_COMMAND(C_HF_FAN, "HFFan") \
  440. USB_ADD_COMMAND(C_HRO_WRITE, "HROWrite") \
  441. USB_ADD_COMMAND(C_HRO_READ, "HRORead") \
  442. USB_ADD_COMMAND(C_OP_NAME, "HFName") \
  443. USB_ADD_COMMAND(C_HF_GETHEADER, "HFGetHeader") \
  444. USB_ADD_COMMAND(C_HF_GETDATA, "HFGetData") \
  445. USB_ADD_COMMAND(C_HF_CLEAR_READ, "HFClearRead") \
  446. USB_ADD_COMMAND(C_CTA_READ, "CTARead") \
  447. USB_ADD_COMMAND(C_CTA_WRITE, "CTAWrite") \
  448. USB_ADD_COMMAND(C_MCP_GETGPIOSETTING, "MCPGetGPIOSetting") \
  449. USB_ADD_COMMAND(C_MCP_SETGPIOSETTING, "MCPSetGPIOSetting") \
  450. USB_ADD_COMMAND(C_MCP_GETGPIOPINVAL, "MCPGetGPIOPinVal") \
  451. USB_ADD_COMMAND(C_MCP_SETGPIOPINVAL, "MCPSetGPIOPinVal") \
  452. USB_ADD_COMMAND(C_MCP_GETGPIOPINDIR, "MCPGetGPIOPinDir") \
  453. USB_ADD_COMMAND(C_MCP_SETGPIOPINDIR, "MCPSetGPIOPinDir") \
  454. USB_ADD_COMMAND(C_MCP_SETSPISETTING, "MCPSetSPISetting") \
  455. USB_ADD_COMMAND(C_MCP_GETSPISETTING, "MCPGetSPISetting") \
  456. USB_ADD_COMMAND(C_MCP_SPITRANSFER, "MCPSPITransfer") \
  457. USB_ADD_COMMAND(C_MCP_SPICANCEL, "MCPSPICancel") \
  458. USB_ADD_COMMAND(C_BITMAIN_SEND, "BitmainSend") \
  459. USB_ADD_COMMAND(C_BITMAIN_READ, "BitmainRead") \
  460. USB_ADD_COMMAND(C_BITMAIN_TOKEN_TXCONFIG, "BitmainTokenTxConfig") \
  461. USB_ADD_COMMAND(C_BITMAIN_TOKEN_TXTASK, "BitmainTokenTxTask") \
  462. USB_ADD_COMMAND(C_BITMAIN_TOKEN_RXSTATUS, "BitmainTokenRxStatus") \
  463. USB_ADD_COMMAND(C_BITMAIN_DATA_RXSTATUS, "BitmainDataRxStatus") \
  464. USB_ADD_COMMAND(C_BITMAIN_DATA_RXNONCE, "BitmainDataRxNonce")
  465. /* Create usb_cmds enum from USB_PARSE_COMMANDS macro */
  466. enum usb_cmds {
  467. USB_PARSE_COMMANDS(ENUMERATION)
  468. C_MAX
  469. };
  470. struct device_drv;
  471. struct cgpu_info;
  472. bool async_usb_transfers(void);
  473. void cancel_usb_transfers(void);
  474. void usb_all(int level);
  475. void usb_list(void);
  476. const char *usb_cmdname(enum usb_cmds cmd);
  477. void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err);
  478. void blacklist_cgpu(struct cgpu_info *cgpu);
  479. void whitelist_cgpu(struct cgpu_info *cgpu);
  480. void usb_nodev(struct cgpu_info *cgpu);
  481. struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig);
  482. struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
  483. struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu);
  484. void usb_uninit(struct cgpu_info *cgpu);
  485. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  486. void __usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *),
  487. bool single);
  488. #define usb_detect(drv, cgpu) __usb_detect(drv, cgpu, false)
  489. #define usb_detect_one(drv, cgpu) __usb_detect(drv, cgpu, true)
  490. struct api_data *api_usb_stats(int *count);
  491. void update_usb_stats(struct cgpu_info *cgpu);
  492. void usb_reset(struct cgpu_info *cgpu);
  493. int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);
  494. int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, enum usb_cmds);
  495. int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd);
  496. int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, enum usb_cmds cmd);
  497. int usb_ftdi_cts(struct cgpu_info *cgpu);
  498. int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo);
  499. #define usb_ftdi_set_latency(_cgpu) _usb_ftdi_set_latency(_cgpu, DEFAULT_INTINFO)
  500. void usb_buffer_clear(struct cgpu_info *cgpu);
  501. uint32_t usb_buffer_size(struct cgpu_info *cgpu);
  502. int _usb_interface(struct cgpu_info *cgpu, int intinfo);
  503. #define usb_interface(_cgpu) _usb_interface(_cgpu, DEFAULT_INTINFO)
  504. enum sub_ident usb_ident(struct cgpu_info *cgpu);
  505. void usb_set_dev_start(struct cgpu_info *cgpu);
  506. void usb_cleanup();
  507. void usb_initialise();
  508. void *usb_resource_thread(void *userdata);
  509. void initialise_usblocks(void);
  510. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  511. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
  512. #define usb_read_cancellable(cgpu, buf, bufsiz, read, cmd) \
  513. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true)
  514. #define usb_read_ii(cgpu, intinfo, buf, bufsiz, read, cmd) \
  515. _usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
  516. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  517. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
  518. #define usb_read_ii_once(cgpu, intinfo, buf, bufsiz, read, cmd) \
  519. _usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
  520. #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  521. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, false)
  522. #define usb_read_once_timeout_cancellable(cgpu, buf, bufsiz, read, timeout, cmd) \
  523. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, true)
  524. #define usb_read_ii_once_timeout(cgpu, intinfo, buf, bufsiz, read, timeout, cmd) \
  525. _usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, false)
  526. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  527. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false)
  528. #define usb_read_nl_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  529. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, "\n", cmd, false, false)
  530. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  531. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false, false)
  532. #define usb_read_ok_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  533. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, "OK\n", cmd, false, false)
  534. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  535. _usb_read(cgpu, DEFAULT_INTINFO, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
  536. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  537. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
  538. #define usb_read_timeout_cancellable(cgpu, buf, bufsiz, read, timeout, cmd) \
  539. _usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, true)
  540. #define usb_read_ii_timeout(cgpu, intinfo, buf, bufsiz, read, timeout, cmd) \
  541. _usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
  542. #define usb_read_ii_timeout_cancellable(cgpu, intinfo, buf, bufsiz, read, timeout, cmd) \
  543. _usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, true)
  544. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  545. _usb_read(cgpu, DEFAULT_INTINFO, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false)
  546. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  547. _usb_write(cgpu, DEFAULT_INTINFO, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  548. #define usb_write_ii(cgpu, intinfo, buf, bufsiz, wrote, cmd) \
  549. _usb_write(cgpu, intinfo, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  550. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  551. _usb_write(cgpu, DEFAULT_INTINFO, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  552. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  553. _usb_write(cgpu, DEFAULT_INTINFO, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  554. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  555. _usb_write(cgpu, DEFAULT_INTINFO, ep, buf, bufsiz, wrote, timeout, cmd)
  556. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  557. _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
  558. #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
  559. _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
  560. #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
  561. _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
  562. #endif