usbutils.h 4.7 KB

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