usbutils.h 5.7 KB

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