usbutils.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2012-2013 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. #ifndef USBUTILS_H
  10. #define USBUTILS_H
  11. #include <libusb.h>
  12. #define EPI(x) (LIBUSB_ENDPOINT_IN | (unsigned char)(x))
  13. #define EPO(x) (LIBUSB_ENDPOINT_OUT | (unsigned char)(x))
  14. // For 0x0403:0x6014/0x6001 FT232H (and possibly others?) - BFL, BAS, BLT, LLT, AVA
  15. #define FTDI_TYPE_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT)
  16. #define FTDI_TYPE_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN)
  17. #define FTDI_REQUEST_RESET ((uint8_t)0)
  18. #define FTDI_REQUEST_MODEM ((uint8_t)1)
  19. #define FTDI_REQUEST_FLOW ((uint8_t)2)
  20. #define FTDI_REQUEST_BAUD ((uint8_t)3)
  21. #define FTDI_REQUEST_DATA ((uint8_t)4)
  22. #define FTDI_REQUEST_LATENCY ((uint8_t)9)
  23. #define FTDI_VALUE_RESET 0
  24. #define FTDI_VALUE_PURGE_RX 1
  25. #define FTDI_VALUE_PURGE_TX 2
  26. #define FTDI_VALUE_LATENCY 1
  27. // Baud
  28. #define FTDI_VALUE_BAUD_BFL 0xc068
  29. #define FTDI_INDEX_BAUD_BFL 0x0200
  30. #define FTDI_VALUE_BAUD_BAS FTDI_VALUE_BAUD_BFL
  31. #define FTDI_INDEX_BAUD_BAS FTDI_INDEX_BAUD_BFL
  32. // LLT = BLT (same code)
  33. #define FTDI_VALUE_BAUD_BLT 0x001a
  34. #define FTDI_INDEX_BAUD_BLT 0x0000
  35. #define FTDI_VALUE_BAUD_AVA 0x001A
  36. #define FTDI_INDEX_BAUD_AVA 0x0000
  37. #define FTDI_VALUE_DATA_AVA 8
  38. // Data control
  39. #define FTDI_VALUE_DATA_BFL 0
  40. #define FTDI_VALUE_DATA_BAS FTDI_VALUE_DATA_BFL
  41. // LLT = BLT (same code)
  42. #define FTDI_VALUE_DATA_BLT 8
  43. #define FTDI_VALUE_FLOW 0
  44. #define FTDI_VALUE_MODEM 0x0303
  45. // For 0x10c4:0xea60 USB cp210x chip - AMU
  46. #define CP210X_TYPE_OUT 0x41
  47. #define CP210X_REQUEST_DATA 0x07
  48. #define CP210X_REQUEST_BAUD 0x1e
  49. #define CP210X_VALUE_DATA 0x0303
  50. #define CP210X_DATA_BAUD 0x0001c200
  51. // For 0x067b:0x2303 Prolific PL2303 - ICA
  52. #define PL2303_CTRL_DTR 0x01
  53. #define PL2303_CTRL_RTS 0x02
  54. #define PL2303_CTRL_OUT 0x21
  55. #define PL2303_VENDOR_OUT 0x40
  56. #define PL2303_REQUEST_CTRL 0x22
  57. #define PL2303_REQUEST_LINE 0x20
  58. #define PL2303_REQUEST_VENDOR 0x01
  59. #define PL2303_REPLY_CTRL 0x21
  60. #define PL2303_VALUE_CTRL (PL2303_CTRL_DTR | PL2303_CTRL_RTS)
  61. #define PL2303_VALUE_LINE 0
  62. #define PL2303_VALUE_LINE0 0x0001c200
  63. #define PL2303_VALUE_LINE1 0x080000
  64. #define PL2303_VALUE_LINE_SIZE 7
  65. #define PL2303_VALUE_VENDOR 0
  66. // Use the device defined timeout
  67. #define DEVTIMEOUT 0
  68. // For endpoints defined in usb_find_devices.eps,
  69. // the first two must be the default IN and OUT
  70. #define DEFAULT_EP_IN 0
  71. #define DEFAULT_EP_OUT 1
  72. struct usb_endpoints {
  73. uint8_t att;
  74. uint16_t size;
  75. unsigned char ep;
  76. bool found;
  77. };
  78. enum sub_ident {
  79. IDENT_UNK = 0,
  80. IDENT_BAJ,
  81. IDENT_BAL,
  82. IDENT_BAS,
  83. IDENT_BAM,
  84. IDENT_BFL,
  85. IDENT_MMQ,
  86. IDENT_AVA,
  87. IDENT_ICA,
  88. IDENT_AMU,
  89. IDENT_BLT,
  90. IDENT_LLT,
  91. IDENT_CMR,
  92. IDENT_ZTX
  93. };
  94. struct usb_find_devices {
  95. int drv;
  96. const char *name;
  97. enum sub_ident ident;
  98. uint16_t idVendor;
  99. uint16_t idProduct;
  100. char *iManufacturer;
  101. char *iProduct;
  102. int kernel;
  103. int config;
  104. int interface;
  105. unsigned int timeout;
  106. int epcount;
  107. struct usb_endpoints *eps;
  108. };
  109. enum usb_types {
  110. USB_TYPE_STD = 0,
  111. USB_TYPE_FTDI
  112. };
  113. struct cg_usb_device {
  114. struct usb_find_devices *found;
  115. libusb_device_handle *handle;
  116. pthread_mutex_t *mutex;
  117. struct libusb_device_descriptor *descriptor;
  118. enum usb_types usb_type;
  119. enum sub_ident ident;
  120. uint16_t usbver;
  121. int speed;
  122. char *prod_string;
  123. char *manuf_string;
  124. char *serial_string;
  125. unsigned char fwVersion; // ??
  126. unsigned char interfaceVersion; // ??
  127. };
  128. struct cg_usb_info {
  129. uint8_t bus_number;
  130. uint8_t device_address;
  131. int usbstat;
  132. bool nodev;
  133. int nodev_count;
  134. struct timeval last_nodev;
  135. };
  136. enum usb_cmds {
  137. C_REJECTED = 0,
  138. C_PING,
  139. C_CLEAR,
  140. C_REQUESTVERSION,
  141. C_GETVERSION,
  142. C_REQUESTFPGACOUNT,
  143. C_GETFPGACOUNT,
  144. C_STARTPROGRAM,
  145. C_STARTPROGRAMSTATUS,
  146. C_PROGRAM,
  147. C_PROGRAMSTATUS,
  148. C_PROGRAMSTATUS2,
  149. C_FINALPROGRAMSTATUS,
  150. C_SETCLOCK,
  151. C_REPLYSETCLOCK,
  152. C_REQUESTUSERCODE,
  153. C_GETUSERCODE,
  154. C_REQUESTTEMPERATURE,
  155. C_GETTEMPERATURE,
  156. C_SENDWORK,
  157. C_SENDWORKSTATUS,
  158. C_REQUESTWORKSTATUS,
  159. C_GETWORKSTATUS,
  160. C_REQUESTIDENTIFY,
  161. C_GETIDENTIFY,
  162. C_REQUESTFLASH,
  163. C_REQUESTSENDWORK,
  164. C_REQUESTSENDWORKSTATUS,
  165. C_RESET,
  166. C_SETBAUD,
  167. C_SETDATA,
  168. C_SETFLOW,
  169. C_SETMODEM,
  170. C_PURGERX,
  171. C_PURGETX,
  172. C_FLASHREPLY,
  173. C_REQUESTDETAILS,
  174. C_GETDETAILS,
  175. C_REQUESTRESULTS,
  176. C_GETRESULTS,
  177. C_REQUESTQUEJOB,
  178. C_REQUESTQUEJOBSTATUS,
  179. C_QUEJOB,
  180. C_QUEJOBSTATUS,
  181. C_QUEFLUSH,
  182. C_QUEFLUSHREPLY,
  183. C_REQUESTVOLTS,
  184. C_SENDTESTWORK,
  185. C_LATENCY,
  186. C_SETLINE,
  187. C_VENDOR,
  188. C_AVALON_TASK,
  189. C_AVALON_READ,
  190. C_GET_AVALON_READY,
  191. C_AVALON_RESET,
  192. C_GET_AR,
  193. C_FTDI_STATUS,
  194. C_MAX
  195. };
  196. struct device_drv;
  197. struct cgpu_info;
  198. void usb_all(int level);
  199. const char *usb_cmdname(enum usb_cmds cmd);
  200. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  201. void usb_uninit(struct cgpu_info *cgpu);
  202. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  203. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  204. struct api_data *api_usb_stats(int *count);
  205. void update_usb_stats(struct cgpu_info *cgpu);
  206. 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);
  207. int usb_ftdi_cts(struct cgpu_info *cgpu);
  208. int usb_ftdi_ctw(struct cgpu_info *cgpu);
  209. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  210. 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);
  211. 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);
  212. void usb_cleanup();
  213. void usb_initialise();
  214. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  215. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  216. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  217. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
  218. #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  219. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true)
  220. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  221. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
  222. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  223. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
  224. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  225. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  226. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  227. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
  228. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  229. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
  230. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  231. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  232. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  233. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  234. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  235. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  236. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  237. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  238. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  239. _usb_transfer(cgpu, typ, req, val, idx, NULL, 0, DEVTIMEOUT, cmd)
  240. #define usb_transfer_data(cgpu, typ, req, val, idx, data, len, cmd) \
  241. _usb_transfer(cgpu, typ, req, val, idx, data, len, DEVTIMEOUT, cmd)
  242. #define usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, cmd) \
  243. _usb_transfer_read(cgpu, typ, req, val, idx, buf, bufsiz, read, DEVTIMEOUT, cmd)
  244. #endif