usbutils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2013 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. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  14. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  15. // For 0x0403:0x6014/0x6001 FT232H (and possibly others?) - BFL, BAS, BLT, LLT, AVA
  16. #define FTDI_TYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
  17. #define FTDI_TYPE_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
  18. #define FTDI_REQUEST_RESET ((uint8_t)0)
  19. #define FTDI_REQUEST_MODEM ((uint8_t)1)
  20. #define FTDI_REQUEST_FLOW ((uint8_t)2)
  21. #define FTDI_REQUEST_BAUD ((uint8_t)3)
  22. #define FTDI_REQUEST_DATA ((uint8_t)4)
  23. #define FTDI_REQUEST_LATENCY ((uint8_t)9)
  24. #define FTDI_VALUE_RESET 0
  25. #define FTDI_VALUE_PURGE_RX 1
  26. #define FTDI_VALUE_PURGE_TX 2
  27. #define FTDI_VALUE_LATENCY 1
  28. // Baud
  29. #define FTDI_VALUE_BAUD_BFL 0xc068
  30. #define FTDI_INDEX_BAUD_BFL 0x0200
  31. #define FTDI_VALUE_BAUD_BAS FTDI_VALUE_BAUD_BFL
  32. #define FTDI_INDEX_BAUD_BAS FTDI_INDEX_BAUD_BFL
  33. // LLT = BLT (same code)
  34. #define FTDI_VALUE_BAUD_BLT 0x001a
  35. #define FTDI_INDEX_BAUD_BLT 0x0000
  36. // Avalon
  37. #define FTDI_VALUE_BAUD_AVA 0x001A
  38. #define FTDI_INDEX_BAUD_AVA 0x0000
  39. #define FTDI_VALUE_DATA_AVA 8
  40. // BitBurner
  41. #define BITBURNER_REQUEST ((uint8_t)0x42)
  42. #define BITBURNER_VALUE 0x4242
  43. #define BITBURNER_INDEX_SET_VOLTAGE 1
  44. #define BITBURNER_INDEX_GET_VOLTAGE 2
  45. #define BITBURNER_INDEX_GET_VERSION 4
  46. // CMR = 115200 & 57600
  47. #define FTDI_VALUE_BAUD_CMR_115 0xc068
  48. #define FTDI_INDEX_BAUD_CMR_115 0x0200
  49. #define FTDI_VALUE_BAUD_CMR_57 0x80d0
  50. #define FTDI_INDEX_BAUD_CMR_57 0x0200
  51. // Data control
  52. #define FTDI_VALUE_DATA_BFL 0
  53. #define FTDI_VALUE_DATA_BAS FTDI_VALUE_DATA_BFL
  54. // LLT = BLT (same code)
  55. #define FTDI_VALUE_DATA_BLT 8
  56. #define FTDI_VALUE_FLOW 0
  57. #define FTDI_VALUE_MODEM 0x0303
  58. // For 0x10c4:0xea60 USB cp210x chip - AMU
  59. #define CP210X_TYPE_OUT 0x41
  60. #define CP210X_REQUEST_IFC_ENABLE 0x00
  61. #define CP210X_REQUEST_DATA 0x07
  62. #define CP210X_REQUEST_BAUD 0x1e
  63. #define CP210X_VALUE_UART_ENABLE 0x0001
  64. #define CP210X_VALUE_DATA 0x0303
  65. #define CP210X_DATA_BAUD 0x0001c200
  66. // For 0x067b:0x2303 Prolific PL2303 - ICA
  67. #define PL2303_CTRL_DTR 0x01
  68. #define PL2303_CTRL_RTS 0x02
  69. #define PL2303_CTRL_OUT 0x21
  70. #define PL2303_VENDOR_OUT 0x40
  71. #define PL2303_REQUEST_CTRL 0x22
  72. #define PL2303_REQUEST_LINE 0x20
  73. #define PL2303_REQUEST_VENDOR 0x01
  74. #define PL2303_REPLY_CTRL 0x21
  75. #define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
  76. #define PL2303_VALUE_LINE 0
  77. #define PL2303_VALUE_LINE0 0x0001c200
  78. #define PL2303_VALUE_LINE1 0x080000
  79. #define PL2303_VALUE_LINE_SIZE 7
  80. #define PL2303_VALUE_VENDOR 0
  81. // Use the device defined timeout
  82. #define DEVTIMEOUT 0
  83. // For endpoints defined in usb_find_devices.eps,
  84. // the first two must be the default IN and OUT
  85. #define DEFAULT_EP_IN 0
  86. #define DEFAULT_EP_OUT 1
  87. struct usb_endpoints {
  88. uint8_t att;
  89. uint16_t size;
  90. unsigned char ep;
  91. bool found;
  92. };
  93. enum sub_ident {
  94. IDENT_UNK = 0,
  95. IDENT_BAJ,
  96. IDENT_BAL,
  97. IDENT_BAS,
  98. IDENT_BAM,
  99. IDENT_BFL,
  100. IDENT_MMQ,
  101. IDENT_AVA,
  102. IDENT_BTB,
  103. IDENT_ICA,
  104. IDENT_AMU,
  105. IDENT_BLT,
  106. IDENT_LLT,
  107. IDENT_CMR1,
  108. IDENT_CMR2,
  109. IDENT_ZTX
  110. };
  111. struct usb_find_devices {
  112. int drv;
  113. const char *name;
  114. enum sub_ident ident;
  115. uint16_t idVendor;
  116. uint16_t idProduct;
  117. char *iManufacturer;
  118. char *iProduct;
  119. int kernel;
  120. int config;
  121. int interface;
  122. unsigned int timeout;
  123. uint16_t wMaxPacketSize;
  124. uint16_t latency;
  125. int epcount;
  126. struct usb_endpoints *eps;
  127. };
  128. /* Latency is set to 32ms to prevent a transfer ever being more than 512 bytes
  129. * +2 bytes of status such as the ftdi chip, when the chips emulate a 115200
  130. * baud rate, to avoid status bytes being interleaved in larger transfers. */
  131. #define LATENCY_UNUSED 0
  132. #define LATENCY_STD 32
  133. enum usb_types {
  134. USB_TYPE_STD = 0,
  135. USB_TYPE_FTDI
  136. };
  137. struct cg_usb_device {
  138. struct usb_find_devices *found;
  139. libusb_device_handle *handle;
  140. pthread_mutex_t *mutex;
  141. struct libusb_device_descriptor *descriptor;
  142. enum usb_types usb_type;
  143. enum sub_ident ident;
  144. uint16_t usbver;
  145. int cps;
  146. bool usecps;
  147. char *prod_string;
  148. char *manuf_string;
  149. char *serial_string;
  150. unsigned char fwVersion; // ??
  151. unsigned char interfaceVersion; // ??
  152. char *buffer;
  153. uint32_t bufsiz;
  154. uint32_t bufamt;
  155. uint16_t PrefPacketSize;
  156. struct timeval last_write_tv;
  157. size_t last_write_siz;
  158. };
  159. #define USB_NOSTAT 0
  160. #define USB_MAX_READ 8192
  161. #define USB_TMO_0 50
  162. #define USB_TMO_1 100
  163. #define USB_TMO_2 500
  164. #define USB_TMOS 3
  165. struct cg_usb_tmo {
  166. uint32_t count;
  167. uint32_t min_tmo;
  168. uint32_t max_tmo;
  169. uint64_t total_over;
  170. uint64_t total_tmo;
  171. };
  172. struct cg_usb_info {
  173. uint8_t bus_number;
  174. uint8_t device_address;
  175. int usbstat;
  176. bool nodev;
  177. int nodev_count;
  178. struct timeval last_nodev;
  179. uint32_t ioerr_count;
  180. uint32_t continuous_ioerr_count;
  181. /*
  182. * for nodev and cgusb access (read and write)
  183. * it's a pointer so MMQ can have it in multiple devices
  184. *
  185. * N.B. general mining code doesn't need to use the read
  186. * lock for 'nodev' if it calls a usb_read/write/etc function
  187. * that uses the lock - however, all usbutils code MUST use it
  188. * to avoid devices disappearing while in use by multiple threads
  189. */
  190. pthread_rwlock_t *devlock;
  191. time_t last_pipe;
  192. uint64_t pipe_count;
  193. uint64_t clear_err_count;
  194. uint64_t retry_err_count;
  195. uint64_t clear_fail_count;
  196. uint64_t read_delay_count;
  197. double total_read_delay;
  198. uint64_t write_delay_count;
  199. double total_write_delay;
  200. /*
  201. * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
  202. * If a single device ever has multiple end points then it will need
  203. * multiple of these
  204. */
  205. unsigned char bulkbuf[USB_MAX_READ+4];
  206. uint64_t tmo_count;
  207. struct cg_usb_tmo usb_tmo[USB_TMOS];
  208. };
  209. enum usb_cmds {
  210. C_REJECTED = 0,
  211. C_PING,
  212. C_CLEAR,
  213. C_REQUESTVERSION,
  214. C_GETVERSION,
  215. C_REQUESTFPGACOUNT,
  216. C_GETFPGACOUNT,
  217. C_STARTPROGRAM,
  218. C_STARTPROGRAMSTATUS,
  219. C_PROGRAM,
  220. C_PROGRAMSTATUS,
  221. C_PROGRAMSTATUS2,
  222. C_FINALPROGRAMSTATUS,
  223. C_SETCLOCK,
  224. C_REPLYSETCLOCK,
  225. C_REQUESTUSERCODE,
  226. C_GETUSERCODE,
  227. C_REQUESTTEMPERATURE,
  228. C_GETTEMPERATURE,
  229. C_SENDWORK,
  230. C_SENDWORKSTATUS,
  231. C_REQUESTWORKSTATUS,
  232. C_GETWORKSTATUS,
  233. C_REQUESTIDENTIFY,
  234. C_GETIDENTIFY,
  235. C_REQUESTFLASH,
  236. C_REQUESTSENDWORK,
  237. C_REQUESTSENDWORKSTATUS,
  238. C_RESET,
  239. C_SETBAUD,
  240. C_SETDATA,
  241. C_SETFLOW,
  242. C_SETMODEM,
  243. C_PURGERX,
  244. C_PURGETX,
  245. C_FLASHREPLY,
  246. C_REQUESTDETAILS,
  247. C_GETDETAILS,
  248. C_REQUESTRESULTS,
  249. C_GETRESULTS,
  250. C_REQUESTQUEJOB,
  251. C_REQUESTQUEJOBSTATUS,
  252. C_QUEJOB,
  253. C_QUEJOBSTATUS,
  254. C_QUEFLUSH,
  255. C_QUEFLUSHREPLY,
  256. C_REQUESTVOLTS,
  257. C_GETVOLTS,
  258. C_SENDTESTWORK,
  259. C_LATENCY,
  260. C_SETLINE,
  261. C_VENDOR,
  262. C_SETFAN,
  263. C_FANREPLY,
  264. C_AVALON_TASK,
  265. C_AVALON_READ,
  266. C_GET_AVALON_READY,
  267. C_AVALON_RESET,
  268. C_GET_AVALON_RESET,
  269. C_FTDI_STATUS,
  270. C_ENABLE_UART,
  271. C_BB_SET_VOLTAGE,
  272. C_BB_GET_VOLTAGE,
  273. C_MAX
  274. };
  275. struct device_drv;
  276. struct cgpu_info;
  277. void usb_all(int level);
  278. const char *usb_cmdname(enum usb_cmds cmd);
  279. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  280. struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig);
  281. struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
  282. struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock);
  283. #define usb_free_cgpu(cgpu) usb_free_cgpu_devlock(cgpu, true)
  284. void usb_uninit(struct cgpu_info *cgpu);
  285. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  286. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  287. struct api_data *api_usb_stats(int *count);
  288. void update_usb_stats(struct cgpu_info *cgpu);
  289. int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce);
  290. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  291. 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);
  292. 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);
  293. int usb_ftdi_cts(struct cgpu_info *cgpu);
  294. int usb_ftdi_set_latency(struct cgpu_info *cgpu);
  295. void usb_buffer_enable(struct cgpu_info *cgpu);
  296. void usb_buffer_disable(struct cgpu_info *cgpu);
  297. void usb_buffer_clear(struct cgpu_info *cgpu);
  298. uint32_t usb_buffer_size(struct cgpu_info *cgpu);
  299. void usb_set_cps(struct cgpu_info *cgpu, int cps);
  300. void usb_enable_cps(struct cgpu_info *cgpu);
  301. void usb_disable_cps(struct cgpu_info *cgpu);
  302. int usb_interface(struct cgpu_info *cgpu);
  303. enum sub_ident usb_ident(struct cgpu_info *cgpu);
  304. void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize);
  305. void usb_set_dev_start(struct cgpu_info *cgpu);
  306. void usb_cleanup();
  307. void usb_initialise();
  308. void *usb_resource_thread(void *userdata);
  309. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  310. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  311. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  312. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
  313. #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  314. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true)
  315. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  316. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
  317. #define usb_read_nl_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  318. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, "\n", cmd, false)
  319. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  320. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
  321. #define usb_read_ok_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  322. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, "OK\n", cmd, false)
  323. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  324. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  325. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  326. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
  327. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  328. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
  329. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  330. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  331. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  332. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  333. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  334. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  335. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  336. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  337. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  338. _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
  339. #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
  340. _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
  341. #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
  342. _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
  343. #endif