usbutils.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. struct usb_find_devices {
  43. int drv;
  44. const char *name;
  45. uint16_t idVendor;
  46. uint16_t idProduct;
  47. char *iManufacturer;
  48. char *iProduct;
  49. int kernel;
  50. int config;
  51. int interface;
  52. unsigned int timeout;
  53. int epcount;
  54. struct usb_endpoints *eps;
  55. };
  56. struct cg_usb_device {
  57. struct usb_find_devices *found;
  58. libusb_device_handle *handle;
  59. pthread_mutex_t *mutex;
  60. struct libusb_device_descriptor *descriptor;
  61. uint16_t usbver;
  62. int speed;
  63. char *prod_string;
  64. char *manuf_string;
  65. char *serial_string;
  66. unsigned char fwVersion; // ??
  67. unsigned char interfaceVersion; // ??
  68. };
  69. struct cg_usb_info {
  70. uint8_t bus_number;
  71. uint8_t device_address;
  72. int usbstat;
  73. bool nodev;
  74. int nodev_count;
  75. struct timeval last_nodev;
  76. };
  77. enum usb_cmds {
  78. C_REJECTED = 0,
  79. C_PING,
  80. C_CLEAR,
  81. C_REQUESTVERSION,
  82. C_GETVERSION,
  83. C_REQUESTFPGACOUNT,
  84. C_GETFPGACOUNT,
  85. C_STARTPROGRAM,
  86. C_STARTPROGRAMSTATUS,
  87. C_PROGRAM,
  88. C_PROGRAMSTATUS,
  89. C_PROGRAMSTATUS2,
  90. C_FINALPROGRAMSTATUS,
  91. C_SETCLOCK,
  92. C_REPLYSETCLOCK,
  93. C_REQUESTUSERCODE,
  94. C_GETUSERCODE,
  95. C_REQUESTTEMPERATURE,
  96. C_GETTEMPERATURE,
  97. C_SENDWORK,
  98. C_SENDWORKSTATUS,
  99. C_REQUESTWORKSTATUS,
  100. C_GETWORKSTATUS,
  101. C_REQUESTIDENTIFY,
  102. C_GETIDENTIFY,
  103. C_REQUESTFLASH,
  104. C_REQUESTSENDWORK,
  105. C_REQUESTSENDWORKSTATUS,
  106. C_RESET,
  107. C_SETBAUD,
  108. C_SETDATA,
  109. C_SETFLOW,
  110. C_SETMODEM,
  111. C_PURGERX,
  112. C_PURGETX,
  113. C_FLASHREPLY,
  114. C_REQUESTDETAILS,
  115. C_GETDETAILS,
  116. C_REQUESTRESULTS,
  117. C_GETRESULTS,
  118. C_REQUESTQUEJOB,
  119. C_REQUESTQUEJOBSTATUS,
  120. C_QUEJOB,
  121. C_QUEJOBSTATUS,
  122. C_QUEFLUSH,
  123. C_QUEFLUSHREPLY,
  124. C_REQUESTVOLTS,
  125. C_MAX
  126. };
  127. struct device_drv;
  128. struct cgpu_info;
  129. void usb_all(int level);
  130. const char *usb_cmdname(enum usb_cmds cmd);
  131. void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
  132. void usb_uninit(struct cgpu_info *cgpu);
  133. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  134. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  135. struct api_data *api_usb_stats(int *count);
  136. void update_usb_stats(struct cgpu_info *cgpu);
  137. 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 ftdi, bool readonce);
  138. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  139. 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);
  140. void usb_cleanup();
  141. void usb_initialise();
  142. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  143. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
  144. #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
  145. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true)
  146. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  147. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false)
  148. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  149. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
  150. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  151. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
  152. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  153. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false)
  154. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  155. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  156. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  157. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  158. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  159. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  160. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  161. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  162. #define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \
  163. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
  164. #define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
  165. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
  166. #define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \
  167. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false)
  168. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  169. _usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)
  170. #endif