usbutils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int kernel;
  48. int config;
  49. int interface;
  50. unsigned int timeout;
  51. int epcount;
  52. struct usb_endpoints *eps;
  53. };
  54. struct cg_usb_device {
  55. struct usb_find_devices *found;
  56. libusb_device_handle *handle;
  57. pthread_mutex_t *mutex;
  58. struct libusb_device_descriptor *descriptor;
  59. uint16_t usbver;
  60. int speed;
  61. char *prod_string;
  62. char *manuf_string;
  63. char *serial_string;
  64. unsigned char fwVersion; // ??
  65. unsigned char interfaceVersion; // ??
  66. };
  67. struct cg_usb_info {
  68. uint8_t bus_number;
  69. uint8_t device_address;
  70. int usbstat;
  71. bool nodev;
  72. int nodev_count;
  73. struct timeval last_nodev;
  74. };
  75. enum usb_cmds {
  76. C_REJECTED = 0,
  77. C_PING,
  78. C_CLEAR,
  79. C_REQUESTVERSION,
  80. C_GETVERSION,
  81. C_REQUESTFPGACOUNT,
  82. C_GETFPGACOUNT,
  83. C_STARTPROGRAM,
  84. C_STARTPROGRAMSTATUS,
  85. C_PROGRAM,
  86. C_PROGRAMSTATUS,
  87. C_PROGRAMSTATUS2,
  88. C_FINALPROGRAMSTATUS,
  89. C_SETCLOCK,
  90. C_REPLYSETCLOCK,
  91. C_REQUESTUSERCODE,
  92. C_GETUSERCODE,
  93. C_REQUESTTEMPERATURE,
  94. C_GETTEMPERATURE,
  95. C_SENDWORK,
  96. C_SENDWORKSTATUS,
  97. C_REQUESTWORKSTATUS,
  98. C_GETWORKSTATUS,
  99. C_REQUESTIDENTIFY,
  100. C_GETIDENTIFY,
  101. C_REQUESTFLASH,
  102. C_REQUESTSENDWORK,
  103. C_REQUESTSENDWORKSTATUS,
  104. C_RESET,
  105. C_SETBAUD,
  106. C_SETDATA,
  107. C_SETFLOW,
  108. C_SETMODEM,
  109. C_PURGERX,
  110. C_PURGETX,
  111. C_FLASHREPLY,
  112. C_MAX
  113. };
  114. struct device_drv;
  115. struct cgpu_info;
  116. void usb_uninit(struct cgpu_info *cgpu);
  117. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
  118. void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
  119. struct api_data *api_usb_stats(int *count);
  120. void update_usb_stats(struct cgpu_info *cgpu);
  121. int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, int eol, enum usb_cmds, bool ftdi);
  122. int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
  123. 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);
  124. void usb_cleanup();
  125. #define usb_read(cgpu, buf, bufsiz, read, cmd) \
  126. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, -1, cmd, false)
  127. #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
  128. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, '\n', cmd, false)
  129. #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
  130. _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, -1, cmd, false)
  131. #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
  132. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, -1, cmd, false)
  133. #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
  134. _usb_read(cgpu, ep, buf, bufsiz, read, timeout, -1, cmd, false)
  135. #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
  136. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  137. #define usb_write_ep(cgpu, ep, buf, bufsiz, wrote, cmd) \
  138. _usb_write(cgpu, ep, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
  139. #define usb_write_timeout(cgpu, buf, bufsiz, wrote, timeout, cmd) \
  140. _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, timeout, cmd)
  141. #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
  142. _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
  143. #define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
  144. _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, '\n', cmd, true)
  145. #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
  146. _usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)
  147. #endif