usbutils.h 6.4 KB

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