ft232r.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2012 Luke Dashjr
  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. #include <errno.h>
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <libusb-1.0/libusb.h>
  13. #include "fpgautils.h"
  14. #include "ft232r.h"
  15. #include "logging.h"
  16. #include "miner.h"
  17. #define FT232R_IDVENDOR 0x0403
  18. #define FT232R_IDPRODUCT 0x6001
  19. struct ft232r_device_info {
  20. libusb_device *libusb_dev;
  21. char *product;
  22. char *serial;
  23. };
  24. static struct ft232r_device_info **ft232r_devinfo_list;
  25. void ft232r_scan_free()
  26. {
  27. if (!ft232r_devinfo_list)
  28. return;
  29. struct ft232r_device_info *info;
  30. for (struct ft232r_device_info **infop = ft232r_devinfo_list; (info = *infop); ++infop) {
  31. libusb_unref_device(info->libusb_dev);
  32. free(info->product);
  33. free(info->serial);
  34. free(info);
  35. }
  36. free(ft232r_devinfo_list);
  37. ft232r_devinfo_list = NULL;
  38. }
  39. void ft232r_scan()
  40. {
  41. ssize_t n, i, found = 0;
  42. libusb_device **list;
  43. struct libusb_device_descriptor desc;
  44. libusb_device_handle *handle;
  45. struct ft232r_device_info *info;
  46. int err;
  47. unsigned char buf[0x100];
  48. ft232r_scan_free();
  49. n = libusb_get_device_list(NULL, &list);
  50. if (unlikely(n < 0)) {
  51. applog(LOG_ERR, "ft232r_scan: Error getting USB device list: %s", libusb_error_name(n));
  52. ft232r_devinfo_list = calloc(1, sizeof(struct ft232r_device_info *));
  53. return;
  54. }
  55. ft232r_devinfo_list = malloc(sizeof(struct ft232r_device_info *) * (n + 1));
  56. for (i = 0; i < n; ++i) {
  57. err = libusb_get_device_descriptor(list[i], &desc);
  58. if (unlikely(err)) {
  59. applog(LOG_ERR, "ft232r_scan: Error getting device descriptor: %s", libusb_error_name(err));
  60. continue;
  61. }
  62. if (!(desc.idVendor == FT232R_IDVENDOR && desc.idProduct == FT232R_IDPRODUCT))
  63. continue;
  64. err = libusb_open(list[i], &handle);
  65. if (unlikely(err)) {
  66. applog(LOG_ERR, "ft232r_scan: Error opening device: %s", libusb_error_name(err));
  67. continue;
  68. }
  69. n = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf, sizeof(buf)-1);
  70. if (unlikely(n < 0)) {
  71. libusb_close(handle);
  72. applog(LOG_ERR, "ft232r_scan: Error getting iProduct string: %s", libusb_error_name(n));
  73. continue;
  74. }
  75. buf[n] = '\0';
  76. info = malloc(sizeof(struct ft232r_device_info));
  77. info->product = strdup((char*)buf);
  78. n = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf, sizeof(buf)-1);
  79. libusb_close(handle);
  80. if (unlikely(n < 0)) {
  81. applog(LOG_ERR, "ft232r_scan: Error getting iSerialNumber string: %s", libusb_error_name(n));
  82. n = 0;
  83. }
  84. buf[n] = '\0';
  85. info->serial = strdup((char*)buf);
  86. info->libusb_dev = libusb_ref_device(list[i]);
  87. ft232r_devinfo_list[found++] = info;
  88. applog(LOG_DEBUG, "ft232r_scan: Found \"%s\" serial \"%s\"", info->product, info->serial);
  89. }
  90. ft232r_devinfo_list[found] = NULL;
  91. libusb_free_device_list(list, 1);
  92. }
  93. int ft232r_detect(const char *product_needle, const char *serial, foundusb_func_t cb)
  94. {
  95. struct ft232r_device_info *info;
  96. int found = 0;
  97. for (struct ft232r_device_info **infop = ft232r_devinfo_list; (info = *infop); ++infop) {
  98. if (!strstr(info->product, product_needle))
  99. continue;
  100. if (serial && strcmp(serial, info->serial))
  101. continue;
  102. if (!info->libusb_dev)
  103. continue;
  104. if (!cb(info->libusb_dev, info->product, info->serial))
  105. continue;
  106. info->libusb_dev = NULL;
  107. ++found;
  108. }
  109. return found;
  110. }
  111. #define FTDI_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
  112. #define FTDI_REQTYPE_IN (FTDI_REQTYPE | LIBUSB_ENDPOINT_IN)
  113. #define FTDI_REQTYPE_OUT (FTDI_REQTYPE | LIBUSB_ENDPOINT_OUT)
  114. #define FTDI_REQUEST_RESET 0
  115. #define FTDI_REQUEST_SET_BAUDRATE 3
  116. #define FTDI_REQUEST_SET_BITMODE 0x0b
  117. #define FTDI_BAUDRATE_3M 0,0
  118. #define FTDI_INDEX 1
  119. #define FTDI_TIMEOUT 1000
  120. struct ft232r_device_handle {
  121. libusb_device_handle *h;
  122. uint8_t i;
  123. uint8_t o;
  124. };
  125. struct ft232r_device_handle *ft232r_open(libusb_device *dev)
  126. {
  127. // FIXME: Cleanup on errors
  128. libusb_device_handle *devh;
  129. struct ft232r_device_handle *ftdi;
  130. if (libusb_open(dev, &devh)) {
  131. applog(LOG_ERR, "ft232r_open: Error opening device");
  132. return NULL;
  133. }
  134. libusb_detach_kernel_driver(devh, 0);
  135. if (libusb_set_configuration(devh, 1)) {
  136. applog(LOG_ERR, "ft232r_open: Error setting configuration");
  137. return NULL;
  138. }
  139. if (libusb_claim_interface(devh, 0)) {
  140. applog(LOG_ERR, "ft232r_open: Error claiming interface");
  141. return NULL;
  142. }
  143. if (libusb_set_interface_alt_setting(devh, 0, 0)) {
  144. applog(LOG_ERR, "ft232r_open: Error setting interface alt");
  145. return NULL;
  146. }
  147. if (libusb_control_transfer(devh, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BAUDRATE, FTDI_BAUDRATE_3M, NULL, 0, FTDI_TIMEOUT) < 0) {
  148. applog(LOG_ERR, "ft232r_open: Error performing control transfer");
  149. return NULL;
  150. }
  151. ftdi = calloc(1, sizeof(*ftdi));
  152. ftdi->h = devh;
  153. struct libusb_config_descriptor *cfg;
  154. if (libusb_get_config_descriptor(dev, 1, &cfg)) {
  155. applog(LOG_ERR, "ft232r_open: Error getting config descriptor");
  156. return NULL;
  157. }
  158. const struct libusb_interface_descriptor *altcfg = &cfg->interface[0].altsetting[0];
  159. if (altcfg->bNumEndpoints < 2) {
  160. applog(LOG_ERR, "ft232r_open: Too few endpoints");
  161. return NULL;
  162. }
  163. ftdi->i = altcfg->endpoint[0].bEndpointAddress;
  164. ftdi->o = altcfg->endpoint[1].bEndpointAddress;
  165. libusb_free_config_descriptor(cfg);
  166. return ftdi;
  167. }
  168. bool ft232r_purge_buffers(struct ft232r_device_handle *dev, enum ft232r_reset_purge purge)
  169. {
  170. if (purge & FTDI_PURGE_RX)
  171. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_RX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  172. return false;
  173. if (purge & FTDI_PURGE_TX)
  174. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_TX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  175. return false;
  176. return true;
  177. }
  178. bool ft232r_set_bitmode(struct ft232r_device_handle *dev, uint8_t mask, uint8_t mode)
  179. {
  180. return !libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BITMODE, (mode << 8) | mask, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT);
  181. }
  182. static ssize_t ft232r_readwrite(struct ft232r_device_handle *dev, unsigned char endpoint, void *data, size_t count)
  183. {
  184. int transferred;
  185. switch (libusb_bulk_transfer(dev->h, endpoint, data, count, &transferred, FTDI_TIMEOUT)) {
  186. case LIBUSB_ERROR_TIMEOUT:
  187. if (!transferred) {
  188. errno = ETIMEDOUT;
  189. return -1;
  190. }
  191. case 0:
  192. return transferred;
  193. default:
  194. errno = EIO;
  195. return -1;
  196. }
  197. }
  198. ssize_t ft232r_write(struct ft232r_device_handle *dev, void *data, size_t count)
  199. {
  200. return ft232r_readwrite(dev, dev->o, data, count);
  201. }
  202. ssize_t ft232r_write_all(struct ft232r_device_handle *dev, void *data, size_t count)
  203. {
  204. char *p = data;
  205. ssize_t writ, total = 0;
  206. while (count && (writ = ft232r_write(dev, p, count)) > 0) {
  207. p += writ;
  208. count -= writ;
  209. total += writ;
  210. }
  211. return total ?: writ;
  212. }
  213. // Caveat: Reads of less than 512 bytes may fail :(
  214. ssize_t ft232r_read(struct ft232r_device_handle *dev, void *buf, size_t count)
  215. {
  216. return ft232r_readwrite(dev, dev->i, buf, count);
  217. }
  218. #if 0
  219. int main() {
  220. libusb_init(NULL);
  221. ft232r_scan();
  222. ft232r_scan_free();
  223. libusb_exit(NULL);
  224. }
  225. void applog(int prio, const char *fmt, ...)
  226. {
  227. va_list ap;
  228. va_start(ap, fmt);
  229. vprintf(fmt, ap);
  230. puts("");
  231. va_end(ap);
  232. }
  233. #endif