usbutils.h 7.4 KB

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