usbutils.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 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. // Use the device defined timeout
  40. #define DEVTIMEOUT 0
  41. // For endpoints defined in usb_find_devices.eps,
  42. // the first two must be the default IN and OUT
  43. #define DEFAULT_EP_IN 0
  44. #define DEFAULT_EP_OUT 1
  45. struct usb_endpoints {
  46. uint8_t att;
  47. uint16_t size;
  48. unsigned char ep;
  49. bool found;
  50. };
  51. enum sub_ident {
  52. IDENT_BAJ,
  53. IDENT_BAL,
  54. IDENT_BAS,
  55. IDENT_BAM,
  56. IDENT_BFL,
  57. IDENT_MMQ,
  58. IDENT_AVA,
  59. IDENT_ICA,
  60. IDENT_AMU,
  61. IDENT_BLT,
  62. IDENT_LLT,
  63. IDENT_CMR,
  64. IDENT_ZTX
  65. };
  66. struct usb_find_devices {
  67. int drv;
  68. const char *name;
  69. enum sub_ident ident;
  70. uint16_t idVendor;
  71. uint16_t idProduct;
  72. char *iManufacturer;
  73. char *iProduct;
  74. int kernel;
  75. int config;
  76. int interface;
  77. unsigned int timeout;
  78. int epcount;
  79. struct usb_endpoints *eps;
  80. };
  81. enum usb_types {
  82. USB_TYPE_STD = 0,
  83. USB_TYPE_FTDI
  84. };
  85. struct cg_usb_device {
  86. struct usb_find_devices *found;
  87. libusb_device_handle *handle;
  88. pthread_mutex_t *mutex;
  89. struct libusb_device_descriptor *descriptor;
  90. enum usb_types usb_type;
  91. enum sub_ident ident;
  92. uint16_t usbver;
  93. int speed;
  94. char *prod_string;
  95. char *manuf_string;
  96. char *serial_string;
  97. unsigned char fwVersion; // ??
  98. unsigned char interfaceVersion; // ??
  99. };
  100. struct cg_usb_info {
  101. uint8_t bus_number;
  102. uint8_t device_address;
  103. int usbstat;
  104. bool nodev;
  105. int nodev_count;
  106. struct timeval last_nodev;
  107. };
  108. enum usb_cmds {
  109. C_REJECTED = 0,
  110. C_PING,
  111. C_CLEAR,
  112. C_REQUESTVERSION,
  113. C_GETVERSION,
  114. C_REQUESTFPGACOUNT,
  115. C_GETFPGACOUNT,
  116. C_STARTPROGRAM,
  117. C_STARTPROGRAMSTATUS,
  118. C_PROGRAM,
  119. C_PROGRAMSTATUS,
  120. C_PROGRAMSTATUS2,
  121. C_FINALPROGRAMSTATUS,
  122. C_SETCLOCK,
  123. C_REPLYSETCLOCK,
  124. C_REQUESTUSERCODE,
  125. C_GETUSERCODE,
  126. C_REQUESTTEMPERATURE,
  127. C_GETTEMPERATURE,
  128. C_SENDWORK,
  129. C_SENDWORKSTATUS,
  130. C_REQUESTWORKSTATUS,
  131. C_GETWORKSTATUS,
  132. C_REQUESTIDENTIFY,
  133. C_GETIDENTIFY,
  134. C_REQUESTFLASH,
  135. C_REQUESTSENDWORK,
  136. C_REQUESTSENDWORKSTATUS,
  137. C_RESET,
  138. C_SETBAUD,
  139. C_SETDATA,
  140. C_SETFLOW,
  141. C_SETMODEM,
  142. C_PURGERX,
  143. C_PURGETX,
  144. C_FLASHREPLY,
  145. C_REQUESTDETAILS,
  146. C_GETDETAILS,
  147. C_REQUESTRESULTS,
  148. C_GETRESULTS,
  149. C_REQUESTQUEJOB,
  150. C_REQUESTQUEJOBSTATUS,
  151. C_QUEJOB,
  152. C_QUEJOBSTATUS,
  153. C_QUEFLUSH,
  154. C_QUEFLUSHREPLY,
  155. C_REQUESTVOLTS,
  156. C_SENDTESTWORK,
  157. C_LATENCY,
  158. C_MAX
  159. };
  160. struct device_drv;
  161. struct cgpu_info;
  162. void usb_all(int level);
  163. const char *usb_cmdname(enum usb_cmds cmd);
  164. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  165. void usb_uninit(struct cgpu_info *cgpu);
  166. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  167. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  168. struct api_data *api_usb_stats(int *count);
  169. void update_usb_stats(struct cgpu_info *cgpu);
  170. 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);
  171. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  172. int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd);
  173. void usb_cleanup();
  174. void usb_initialise();
  175. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  176. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  177. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  178. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
  179. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  180. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
  181. #define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
  182. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
  183. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  184. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
  185. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  186. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
  187. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  188. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
  189. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  190. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  191. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  192. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  193. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  194. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  195. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  196. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  197. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  198. _usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)
  199. #endif