usbutils.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // CMR = 115200 & 57600
  41. #define FTDI_VALUE_BAUD_CMR_115 0xc068
  42. #define FTDI_INDEX_BAUD_CMR_115 0x0200
  43. #define FTDI_VALUE_BAUD_CMR_57 0x80d0
  44. #define FTDI_INDEX_BAUD_CMR_57 0x0200
  45. // Data control
  46. #define FTDI_VALUE_DATA_BFL 0
  47. #define FTDI_VALUE_DATA_BAS FTDI_VALUE_DATA_BFL
  48. // LLT = BLT (same code)
  49. #define FTDI_VALUE_DATA_BLT 8
  50. #define FTDI_VALUE_FLOW 0
  51. #define FTDI_VALUE_MODEM 0x0303
  52. // For 0x10c4:0xea60 USB cp210x chip - AMU
  53. #define CP210X_TYPE_OUT 0x41
  54. #define CP210X_REQUEST_IFC_ENABLE 0x00
  55. #define CP210X_REQUEST_DATA 0x07
  56. #define CP210X_REQUEST_BAUD 0x1e
  57. #define CP210X_VALUE_UART_ENABLE 0x0001
  58. #define CP210X_VALUE_DATA 0x0303
  59. #define CP210X_DATA_BAUD 0x0001c200
  60. // For 0x067b:0x2303 Prolific PL2303 - ICA
  61. #define PL2303_CTRL_DTR 0x01
  62. #define PL2303_CTRL_RTS 0x02
  63. #define PL2303_CTRL_OUT 0x21
  64. #define PL2303_VENDOR_OUT 0x40
  65. #define PL2303_REQUEST_CTRL 0x22
  66. #define PL2303_REQUEST_LINE 0x20
  67. #define PL2303_REQUEST_VENDOR 0x01
  68. #define PL2303_REPLY_CTRL 0x21
  69. #define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
  70. #define PL2303_VALUE_LINE 0
  71. #define PL2303_VALUE_LINE0 0x0001c200
  72. #define PL2303_VALUE_LINE1 0x080000
  73. #define PL2303_VALUE_LINE_SIZE 7
  74. #define PL2303_VALUE_VENDOR 0
  75. // Use the device defined timeout
  76. #define DEVTIMEOUT 0
  77. // For endpoints defined in usb_find_devices.eps,
  78. // the first two must be the default IN and OUT
  79. #define DEFAULT_EP_IN 0
  80. #define DEFAULT_EP_OUT 1
  81. struct usb_endpoints {
  82. uint8_t att;
  83. uint16_t size;
  84. unsigned char ep;
  85. bool found;
  86. };
  87. enum sub_ident {
  88. IDENT_UNK = 0,
  89. IDENT_BAJ,
  90. IDENT_BAL,
  91. IDENT_BAS,
  92. IDENT_BAM,
  93. IDENT_BFL,
  94. IDENT_MMQ,
  95. IDENT_AVA,
  96. IDENT_ICA,
  97. IDENT_AMU,
  98. IDENT_BLT,
  99. IDENT_LLT,
  100. IDENT_CMR1,
  101. IDENT_CMR2,
  102. IDENT_ZTX
  103. };
  104. struct usb_find_devices {
  105. int drv;
  106. const char *name;
  107. enum sub_ident ident;
  108. uint16_t idVendor;
  109. uint16_t idProduct;
  110. char *iManufacturer;
  111. char *iProduct;
  112. int kernel;
  113. int config;
  114. int interface;
  115. unsigned int timeout;
  116. uint16_t latency;
  117. int epcount;
  118. struct usb_endpoints *eps;
  119. };
  120. /* Latency is set to 32ms to prevent a transfer ever being more than 512 bytes
  121. * +2 bytes of status such as the ftdi chip, when the chips emulate a 115200
  122. * baud rate, to avoid status bytes being interleaved in larger transfers. */
  123. #define LATENCY_UNUSED 0
  124. #define LATENCY_STD 32
  125. enum usb_types {
  126. USB_TYPE_STD = 0,
  127. USB_TYPE_FTDI
  128. };
  129. struct cg_usb_device {
  130. struct usb_find_devices *found;
  131. libusb_device_handle *handle;
  132. pthread_mutex_t *mutex;
  133. struct libusb_device_descriptor *descriptor;
  134. enum usb_types usb_type;
  135. enum sub_ident ident;
  136. uint16_t usbver;
  137. int speed;
  138. char *prod_string;
  139. char *manuf_string;
  140. char *serial_string;
  141. unsigned char fwVersion; // ??
  142. unsigned char interfaceVersion; // ??
  143. char *buffer;
  144. uint32_t bufsiz;
  145. uint32_t bufamt;
  146. };
  147. #define USB_NOSTAT 0
  148. struct cg_usb_info {
  149. uint8_t bus_number;
  150. uint8_t device_address;
  151. int usbstat;
  152. bool nodev;
  153. int nodev_count;
  154. struct timeval last_nodev;
  155. uint32_t ioerr_count;
  156. uint32_t continuous_ioerr_count;
  157. /*
  158. * for nodev and cgusb access (read and write)
  159. * it's a pointer so MMQ can have it in multiple devices
  160. *
  161. * N.B. general mining code doesn't need to use the read
  162. * lock for 'nodev' if it calls a usb_read/write/etc function
  163. * that uses the lock - however, all usbutils code MUST use it
  164. * to avoid devices disappearing while in use by multiple threads
  165. */
  166. pthread_rwlock_t *devlock;
  167. };
  168. enum usb_cmds {
  169. C_REJECTED = 0,
  170. C_PING,
  171. C_CLEAR,
  172. C_REQUESTVERSION,
  173. C_GETVERSION,
  174. C_REQUESTFPGACOUNT,
  175. C_GETFPGACOUNT,
  176. C_STARTPROGRAM,
  177. C_STARTPROGRAMSTATUS,
  178. C_PROGRAM,
  179. C_PROGRAMSTATUS,
  180. C_PROGRAMSTATUS2,
  181. C_FINALPROGRAMSTATUS,
  182. C_SETCLOCK,
  183. C_REPLYSETCLOCK,
  184. C_REQUESTUSERCODE,
  185. C_GETUSERCODE,
  186. C_REQUESTTEMPERATURE,
  187. C_GETTEMPERATURE,
  188. C_SENDWORK,
  189. C_SENDWORKSTATUS,
  190. C_REQUESTWORKSTATUS,
  191. C_GETWORKSTATUS,
  192. C_REQUESTIDENTIFY,
  193. C_GETIDENTIFY,
  194. C_REQUESTFLASH,
  195. C_REQUESTSENDWORK,
  196. C_REQUESTSENDWORKSTATUS,
  197. C_RESET,
  198. C_SETBAUD,
  199. C_SETDATA,
  200. C_SETFLOW,
  201. C_SETMODEM,
  202. C_PURGERX,
  203. C_PURGETX,
  204. C_FLASHREPLY,
  205. C_REQUESTDETAILS,
  206. C_GETDETAILS,
  207. C_REQUESTRESULTS,
  208. C_GETRESULTS,
  209. C_REQUESTQUEJOB,
  210. C_REQUESTQUEJOBSTATUS,
  211. C_QUEJOB,
  212. C_QUEJOBSTATUS,
  213. C_QUEFLUSH,
  214. C_QUEFLUSHREPLY,
  215. C_REQUESTVOLTS,
  216. C_SENDTESTWORK,
  217. C_LATENCY,
  218. C_SETLINE,
  219. C_VENDOR,
  220. C_SETFAN,
  221. C_FANREPLY,
  222. C_AVALON_TASK,
  223. C_AVALON_READ,
  224. C_GET_AVALON_READY,
  225. C_AVALON_RESET,
  226. C_GET_AVALON_RESET,
  227. C_FTDI_STATUS,
  228. C_ENABLE_UART,
  229. C_MAX
  230. };
  231. struct device_drv;
  232. struct cgpu_info;
  233. void usb_all(int level);
  234. const char *usb_cmdname(enum usb_cmds cmd);
  235. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  236. struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig);
  237. struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
  238. struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock);
  239. #define usb_free_cgpu(cgpu) usb_free_cgpu_devlock(cgpu, true)
  240. void usb_uninit(struct cgpu_info *cgpu);
  241. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  242. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  243. struct api_data *api_usb_stats(int *count);
  244. void update_usb_stats(struct cgpu_info *cgpu);
  245. 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);
  246. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  247. 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);
  248. 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);
  249. int usb_ftdi_cts(struct cgpu_info *cgpu);
  250. int usb_ftdi_set_latency(struct cgpu_info *cgpu);
  251. void usb_buffer_enable(struct cgpu_info *cgpu);
  252. void usb_buffer_disable(struct cgpu_info *cgpu);
  253. void usb_buffer_clear(struct cgpu_info *cgpu);
  254. uint32_t usb_buffer_size(struct cgpu_info *cgpu);
  255. void usb_cleanup();
  256. void usb_initialise();
  257. void *usb_resource_thread(void *userdata);
  258. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  259. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  260. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  261. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
  262. #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  263. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true)
  264. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  265. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
  266. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  267. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
  268. #define usb_read_ok_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  269. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, "OK\n", cmd, false)
  270. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  271. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  272. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  273. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
  274. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  275. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
  276. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  277. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  278. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  279. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  280. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  281. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  282. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  283. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  284. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  285. _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
  286. #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
  287. _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
  288. #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
  289. _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
  290. #endif