usbutils.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_DATA 0x07
  55. #define CP210X_REQUEST_BAUD 0x1e
  56. #define CP210X_VALUE_DATA 0x0303
  57. #define CP210X_DATA_BAUD 0x0001c200
  58. // For 0x067b:0x2303 Prolific PL2303 - ICA
  59. #define PL2303_CTRL_DTR 0x01
  60. #define PL2303_CTRL_RTS 0x02
  61. #define PL2303_CTRL_OUT 0x21
  62. #define PL2303_VENDOR_OUT 0x40
  63. #define PL2303_REQUEST_CTRL 0x22
  64. #define PL2303_REQUEST_LINE 0x20
  65. #define PL2303_REQUEST_VENDOR 0x01
  66. #define PL2303_REPLY_CTRL 0x21
  67. #define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
  68. #define PL2303_VALUE_LINE 0
  69. #define PL2303_VALUE_LINE0 0x0001c200
  70. #define PL2303_VALUE_LINE1 0x080000
  71. #define PL2303_VALUE_LINE_SIZE 7
  72. #define PL2303_VALUE_VENDOR 0
  73. // Use the device defined timeout
  74. #define DEVTIMEOUT 0
  75. // For endpoints defined in usb_find_devices.eps,
  76. // the first two must be the default IN and OUT
  77. #define DEFAULT_EP_IN 0
  78. #define DEFAULT_EP_OUT 1
  79. struct usb_endpoints {
  80. uint8_t att;
  81. uint16_t size;
  82. unsigned char ep;
  83. bool found;
  84. };
  85. enum sub_ident {
  86. IDENT_UNK = 0,
  87. IDENT_BAJ,
  88. IDENT_BAL,
  89. IDENT_BAS,
  90. IDENT_BAM,
  91. IDENT_BFL,
  92. IDENT_MMQ,
  93. IDENT_AVA,
  94. IDENT_ICA,
  95. IDENT_AMU,
  96. IDENT_BLT,
  97. IDENT_LLT,
  98. IDENT_CMR1,
  99. IDENT_CMR2,
  100. IDENT_ZTX
  101. };
  102. struct usb_find_devices {
  103. int drv;
  104. const char *name;
  105. enum sub_ident ident;
  106. uint16_t idVendor;
  107. uint16_t idProduct;
  108. char *iManufacturer;
  109. char *iProduct;
  110. int kernel;
  111. int config;
  112. int interface;
  113. unsigned int timeout;
  114. int epcount;
  115. struct usb_endpoints *eps;
  116. };
  117. enum usb_types {
  118. USB_TYPE_STD = 0,
  119. USB_TYPE_FTDI
  120. };
  121. struct cg_usb_device {
  122. struct usb_find_devices *found;
  123. libusb_device_handle *handle;
  124. pthread_mutex_t *mutex;
  125. struct libusb_device_descriptor *descriptor;
  126. enum usb_types usb_type;
  127. enum sub_ident ident;
  128. uint16_t usbver;
  129. int speed;
  130. char *prod_string;
  131. char *manuf_string;
  132. char *serial_string;
  133. unsigned char fwVersion; // ??
  134. unsigned char interfaceVersion; // ??
  135. };
  136. struct cg_usb_info {
  137. uint8_t bus_number;
  138. uint8_t device_address;
  139. int usbstat;
  140. bool nodev;
  141. int nodev_count;
  142. struct timeval last_nodev;
  143. uint32_t ioerr_count;
  144. uint32_t continuous_ioerr_count;
  145. };
  146. enum usb_cmds {
  147. C_REJECTED = 0,
  148. C_PING,
  149. C_CLEAR,
  150. C_REQUESTVERSION,
  151. C_GETVERSION,
  152. C_REQUESTFPGACOUNT,
  153. C_GETFPGACOUNT,
  154. C_STARTPROGRAM,
  155. C_STARTPROGRAMSTATUS,
  156. C_PROGRAM,
  157. C_PROGRAMSTATUS,
  158. C_PROGRAMSTATUS2,
  159. C_FINALPROGRAMSTATUS,
  160. C_SETCLOCK,
  161. C_REPLYSETCLOCK,
  162. C_REQUESTUSERCODE,
  163. C_GETUSERCODE,
  164. C_REQUESTTEMPERATURE,
  165. C_GETTEMPERATURE,
  166. C_SENDWORK,
  167. C_SENDWORKSTATUS,
  168. C_REQUESTWORKSTATUS,
  169. C_GETWORKSTATUS,
  170. C_REQUESTIDENTIFY,
  171. C_GETIDENTIFY,
  172. C_REQUESTFLASH,
  173. C_REQUESTSENDWORK,
  174. C_REQUESTSENDWORKSTATUS,
  175. C_RESET,
  176. C_SETBAUD,
  177. C_SETDATA,
  178. C_SETFLOW,
  179. C_SETMODEM,
  180. C_PURGERX,
  181. C_PURGETX,
  182. C_FLASHREPLY,
  183. C_REQUESTDETAILS,
  184. C_GETDETAILS,
  185. C_REQUESTRESULTS,
  186. C_GETRESULTS,
  187. C_REQUESTQUEJOB,
  188. C_REQUESTQUEJOBSTATUS,
  189. C_QUEJOB,
  190. C_QUEJOBSTATUS,
  191. C_QUEFLUSH,
  192. C_QUEFLUSHREPLY,
  193. C_REQUESTVOLTS,
  194. C_SENDTESTWORK,
  195. C_LATENCY,
  196. C_SETLINE,
  197. C_VENDOR,
  198. C_AVALON_TASK,
  199. C_AVALON_READ,
  200. C_GET_AVALON_READY,
  201. C_AVALON_RESET,
  202. C_GET_AVALON_RESET,
  203. C_FTDI_STATUS,
  204. C_MAX
  205. };
  206. struct device_drv;
  207. struct cgpu_info;
  208. void usb_all(int level);
  209. const char *usb_cmdname(enum usb_cmds cmd);
  210. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  211. void usb_uninit(struct cgpu_info *cgpu);
  212. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  213. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  214. struct api_data *api_usb_stats(int *count);
  215. void update_usb_stats(struct cgpu_info *cgpu);
  216. 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);
  217. int usb_ftdi_cts(struct cgpu_info *cgpu);
  218. int usb_ftdi_ctw(struct cgpu_info *cgpu);
  219. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  220. 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);
  221. 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);
  222. void usb_cleanup();
  223. void usb_initialise();
  224. void *usb_resource_thread(void *userdata);
  225. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  226. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  227. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  228. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
  229. #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  230. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true)
  231. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  232. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
  233. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  234. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
  235. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  236. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  237. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  238. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
  239. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  240. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
  241. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  242. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  243. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  244. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  245. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  246. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  247. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  248. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  249. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  250. _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
  251. #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
  252. _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
  253. #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
  254. _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
  255. #endif