ft232r.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_REQUEST_GET_PINS 0x0c
  118. #define FTDI_BAUDRATE_3M 0,0
  119. #define FTDI_INDEX 1
  120. #define FTDI_TIMEOUT 1000
  121. struct ft232r_device_handle {
  122. libusb_device_handle *h;
  123. uint8_t i;
  124. uint8_t o;
  125. unsigned char ibuf[256];
  126. uint8_t ibufLen;
  127. };
  128. struct ft232r_device_handle *ft232r_open(libusb_device *dev)
  129. {
  130. // FIXME: Cleanup on errors
  131. libusb_device_handle *devh;
  132. struct ft232r_device_handle *ftdi;
  133. if (libusb_open(dev, &devh)) {
  134. applog(LOG_ERR, "ft232r_open: Error opening device");
  135. return NULL;
  136. }
  137. libusb_reset_device(devh);
  138. libusb_detach_kernel_driver(devh, 0);
  139. if (libusb_set_configuration(devh, 1)) {
  140. applog(LOG_ERR, "ft232r_open: Error setting configuration");
  141. return NULL;
  142. }
  143. if (libusb_claim_interface(devh, 0)) {
  144. applog(LOG_ERR, "ft232r_open: Error claiming interface");
  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, 0, &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. void ft232r_close(struct ft232r_device_handle *dev)
  169. {
  170. libusb_release_interface(dev->h, 0);
  171. libusb_reset_device(dev->h);
  172. libusb_close(dev->h);
  173. }
  174. bool ft232r_purge_buffers(struct ft232r_device_handle *dev, enum ft232r_reset_purge purge)
  175. {
  176. if (purge & FTDI_PURGE_RX)
  177. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_RX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  178. return false;
  179. if (purge & FTDI_PURGE_TX)
  180. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_TX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  181. return false;
  182. return true;
  183. }
  184. bool ft232r_set_bitmode(struct ft232r_device_handle *dev, uint8_t mask, uint8_t mode)
  185. {
  186. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BITMODE, mask, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  187. return false;
  188. return !libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BITMODE, (mode << 8) | mask, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT);
  189. }
  190. static ssize_t ft232r_readwrite(struct ft232r_device_handle *dev, unsigned char endpoint, void *data, size_t count)
  191. {
  192. int transferred;
  193. switch (libusb_bulk_transfer(dev->h, endpoint, data, count, &transferred, FTDI_TIMEOUT)) {
  194. case LIBUSB_ERROR_TIMEOUT:
  195. if (!transferred) {
  196. errno = ETIMEDOUT;
  197. return -1;
  198. }
  199. case 0:
  200. return transferred;
  201. default:
  202. errno = EIO;
  203. return -1;
  204. }
  205. }
  206. ssize_t ft232r_write(struct ft232r_device_handle *dev, void *data, size_t count)
  207. {
  208. return ft232r_readwrite(dev, dev->o, data, count);
  209. }
  210. typedef ssize_t (*ft232r_rwfunc_t)(struct ft232r_device_handle *, void*, size_t);
  211. static ssize_t ft232r_rw_all(ft232r_rwfunc_t rwfunc, struct ft232r_device_handle *dev, void *data, size_t count)
  212. {
  213. char *p = data;
  214. ssize_t writ, total = 0;
  215. while (count && (writ = rwfunc(dev, p, count)) > 0) {
  216. p += writ;
  217. count -= writ;
  218. total += writ;
  219. }
  220. return total ?: writ;
  221. }
  222. ssize_t ft232r_write_all(struct ft232r_device_handle *dev, void *data, size_t count)
  223. {
  224. return ft232r_rw_all(ft232r_write, dev, data, count);
  225. }
  226. ssize_t ft232r_read(struct ft232r_device_handle *dev, void *data, size_t count)
  227. {
  228. // First 2 bytes are FTDI status or something
  229. while (dev->ibufLen <= 2) {
  230. // TODO: Implement a timeout for status byte repeating
  231. int transferred = ft232r_readwrite(dev, dev->i, dev->ibuf, sizeof(dev->ibuf));
  232. if (transferred <= 0)
  233. return transferred;
  234. dev->ibufLen = transferred;
  235. }
  236. unsigned char *ibufs = &dev->ibuf[2];
  237. size_t ibufsLen = dev->ibufLen - 2;
  238. if (count > ibufsLen)
  239. count = ibufsLen;
  240. memcpy(data, ibufs, count);
  241. dev->ibufLen -= count;
  242. ibufsLen -= count;
  243. if (ibufsLen)
  244. memmove(ibufs, &ibufs[count], ibufsLen);
  245. return count;
  246. }
  247. ssize_t ft232r_read_all(struct ft232r_device_handle *dev, void *data, size_t count)
  248. {
  249. return ft232r_rw_all(ft232r_read, dev, data, count);
  250. }
  251. bool ft232r_get_pins(struct ft232r_device_handle *dev, uint8_t *pins)
  252. {
  253. return libusb_control_transfer(dev->h, FTDI_REQTYPE_IN, FTDI_REQUEST_GET_PINS, 0, FTDI_INDEX, pins, 1, FTDI_TIMEOUT) == 1;
  254. }
  255. #if 0
  256. int main() {
  257. libusb_init(NULL);
  258. ft232r_scan();
  259. ft232r_scan_free();
  260. libusb_exit(NULL);
  261. }
  262. void applog(int prio, const char *fmt, ...)
  263. {
  264. va_list ap;
  265. va_start(ap, fmt);
  266. vprintf(fmt, ap);
  267. puts("");
  268. va_end(ap);
  269. }
  270. #endif