ft232r.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "config.h"
  10. #include <errno.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <libusb-1.0/libusb.h>
  15. #include "compat.h"
  16. #include "fpgautils.h"
  17. #include "ft232r.h"
  18. #include "logging.h"
  19. #include "miner.h"
  20. #define FT232R_IDVENDOR 0x0403
  21. #define FT232R_IDPRODUCT 0x6001
  22. struct ft232r_device_info {
  23. libusb_device *libusb_dev;
  24. char *product;
  25. char *serial;
  26. };
  27. static struct ft232r_device_info **ft232r_devinfo_list;
  28. void ft232r_scan_free()
  29. {
  30. if (!ft232r_devinfo_list)
  31. return;
  32. struct ft232r_device_info *info;
  33. for (struct ft232r_device_info **infop = ft232r_devinfo_list; (info = *infop); ++infop) {
  34. libusb_unref_device(info->libusb_dev);
  35. free(info->product);
  36. free(info->serial);
  37. free(info);
  38. }
  39. free(ft232r_devinfo_list);
  40. ft232r_devinfo_list = NULL;
  41. }
  42. void ft232r_scan()
  43. {
  44. ssize_t count, n, i, found = 0;
  45. libusb_device **list;
  46. struct libusb_device_descriptor desc;
  47. libusb_device_handle *handle;
  48. struct ft232r_device_info *info;
  49. int err;
  50. unsigned char buf[0x100];
  51. ft232r_scan_free();
  52. count = libusb_get_device_list(NULL, &list);
  53. if (unlikely(count < 0)) {
  54. applog(LOG_ERR, "ft232r_scan: Error getting USB device list: %s", libusb_error_name(count));
  55. ft232r_devinfo_list = calloc(1, sizeof(struct ft232r_device_info *));
  56. return;
  57. }
  58. ft232r_devinfo_list = malloc(sizeof(struct ft232r_device_info *) * (count + 1));
  59. for (i = 0; i < count; ++i) {
  60. err = libusb_get_device_descriptor(list[i], &desc);
  61. if (unlikely(err)) {
  62. applog(LOG_ERR, "ft232r_scan: Error getting device descriptor: %s", libusb_error_name(err));
  63. continue;
  64. }
  65. if (!(desc.idVendor == FT232R_IDVENDOR && desc.idProduct == FT232R_IDPRODUCT)) {
  66. applog(LOG_DEBUG, "ft232r_scan: Found %04x:%04x - not a ft232r", desc.idVendor, desc.idProduct);
  67. continue;
  68. }
  69. err = libusb_open(list[i], &handle);
  70. if (unlikely(err)) {
  71. applog(LOG_ERR, "ft232r_scan: Error opening device: %s", libusb_error_name(err));
  72. continue;
  73. }
  74. n = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf, sizeof(buf)-1);
  75. if (unlikely(n < 0)) {
  76. libusb_close(handle);
  77. applog(LOG_ERR, "ft232r_scan: Error getting iProduct string: %s", libusb_error_name(n));
  78. continue;
  79. }
  80. buf[n] = '\0';
  81. info = malloc(sizeof(struct ft232r_device_info));
  82. info->product = strdup((char*)buf);
  83. n = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf, sizeof(buf)-1);
  84. libusb_close(handle);
  85. if (unlikely(n < 0)) {
  86. applog(LOG_ERR, "ft232r_scan: Error getting iSerialNumber string: %s", libusb_error_name(n));
  87. n = 0;
  88. }
  89. buf[n] = '\0';
  90. info->serial = strdup((char*)buf);
  91. info->libusb_dev = libusb_ref_device(list[i]);
  92. ft232r_devinfo_list[found++] = info;
  93. applog(LOG_DEBUG, "ft232r_scan: Found \"%s\" serial \"%s\"", info->product, info->serial);
  94. }
  95. ft232r_devinfo_list[found] = NULL;
  96. libusb_free_device_list(list, 1);
  97. }
  98. int ft232r_detect(const char *product_needle, const char *serial, foundusb_func_t cb)
  99. {
  100. struct ft232r_device_info *info;
  101. int found = 0;
  102. for (struct ft232r_device_info **infop = ft232r_devinfo_list; (info = *infop); ++infop) {
  103. if (serial) {
  104. // If we are searching for a specific serial, pay no attention to the product id
  105. if (strcmp(serial, info->serial))
  106. continue;
  107. }
  108. else
  109. if (!strstr(info->product, product_needle))
  110. continue;
  111. if (!info->libusb_dev)
  112. continue;
  113. if (!cb(info->libusb_dev, info->product, info->serial))
  114. continue;
  115. info->libusb_dev = NULL;
  116. ++found;
  117. }
  118. return found;
  119. }
  120. #define FTDI_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
  121. #define FTDI_REQTYPE_IN (FTDI_REQTYPE | LIBUSB_ENDPOINT_IN)
  122. #define FTDI_REQTYPE_OUT (FTDI_REQTYPE | LIBUSB_ENDPOINT_OUT)
  123. #define FTDI_REQUEST_RESET 0
  124. #define FTDI_REQUEST_SET_BAUDRATE 3
  125. #define FTDI_REQUEST_SET_BITMODE 0x0b
  126. #define FTDI_REQUEST_GET_PINS 0x0c
  127. #define FTDI_REQUEST_GET_BITMODE 0x0c
  128. #define FTDI_BAUDRATE_3M 0,0
  129. #define FTDI_INDEX 1
  130. #define FTDI_TIMEOUT 1000
  131. struct ft232r_device_handle {
  132. libusb_device_handle *h;
  133. uint8_t i;
  134. uint8_t o;
  135. unsigned char ibuf[256];
  136. int ibufLen;
  137. uint16_t osz;
  138. unsigned char *obuf;
  139. uint16_t obufsz;
  140. };
  141. struct ft232r_device_handle *ft232r_open(libusb_device *dev)
  142. {
  143. // FIXME: Cleanup on errors
  144. libusb_device_handle *devh;
  145. struct ft232r_device_handle *ftdi;
  146. if (libusb_open(dev, &devh)) {
  147. applog(LOG_ERR, "ft232r_open: Error opening device");
  148. return NULL;
  149. }
  150. libusb_reset_device(devh);
  151. libusb_detach_kernel_driver(devh, 0);
  152. if (libusb_set_configuration(devh, 1)) {
  153. applog(LOG_ERR, "ft232r_open: Error setting configuration");
  154. return NULL;
  155. }
  156. if (libusb_claim_interface(devh, 0)) {
  157. applog(LOG_ERR, "ft232r_open: Error claiming interface");
  158. return NULL;
  159. }
  160. if (libusb_control_transfer(devh, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BAUDRATE, FTDI_BAUDRATE_3M, NULL, 0, FTDI_TIMEOUT) < 0) {
  161. applog(LOG_ERR, "ft232r_open: Error performing control transfer");
  162. return NULL;
  163. }
  164. ftdi = calloc(1, sizeof(*ftdi));
  165. ftdi->h = devh;
  166. struct libusb_config_descriptor *cfg;
  167. if (libusb_get_config_descriptor(dev, 0, &cfg)) {
  168. applog(LOG_ERR, "ft232r_open: Error getting config descriptor");
  169. return NULL;
  170. }
  171. const struct libusb_interface_descriptor *altcfg = &cfg->interface[0].altsetting[0];
  172. if (altcfg->bNumEndpoints < 2) {
  173. applog(LOG_ERR, "ft232r_open: Too few endpoints");
  174. return NULL;
  175. }
  176. ftdi->i = altcfg->endpoint[0].bEndpointAddress;
  177. ftdi->o = altcfg->endpoint[1].bEndpointAddress;
  178. ftdi->osz = 0x1000;
  179. ftdi->obuf = malloc(ftdi->osz);
  180. libusb_free_config_descriptor(cfg);
  181. return ftdi;
  182. }
  183. void ft232r_close(struct ft232r_device_handle *dev)
  184. {
  185. libusb_release_interface(dev->h, 0);
  186. libusb_reset_device(dev->h);
  187. libusb_close(dev->h);
  188. }
  189. bool ft232r_purge_buffers(struct ft232r_device_handle *dev, enum ft232r_reset_purge purge)
  190. {
  191. if (ft232r_flush(dev) < 0)
  192. return false;
  193. if (purge & FTDI_PURGE_RX) {
  194. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_RX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  195. return false;
  196. dev->ibufLen = 0;
  197. }
  198. if (purge & FTDI_PURGE_TX)
  199. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_RESET, FTDI_PURGE_TX, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  200. return false;
  201. return true;
  202. }
  203. bool ft232r_set_bitmode(struct ft232r_device_handle *dev, uint8_t mask, uint8_t mode)
  204. {
  205. if (ft232r_flush(dev) < 0)
  206. return false;
  207. if (libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BITMODE, mask, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT))
  208. return false;
  209. return !libusb_control_transfer(dev->h, FTDI_REQTYPE_OUT, FTDI_REQUEST_SET_BITMODE, (mode << 8) | mask, FTDI_INDEX, NULL, 0, FTDI_TIMEOUT);
  210. }
  211. static ssize_t ft232r_readwrite(struct ft232r_device_handle *dev, unsigned char endpoint, void *data, size_t count)
  212. {
  213. int transferred;
  214. switch (libusb_bulk_transfer(dev->h, endpoint, data, count, &transferred, FTDI_TIMEOUT)) {
  215. case LIBUSB_ERROR_TIMEOUT:
  216. if (!transferred) {
  217. errno = ETIMEDOUT;
  218. return -1;
  219. }
  220. case 0:
  221. return transferred;
  222. default:
  223. errno = EIO;
  224. return -1;
  225. }
  226. }
  227. ssize_t ft232r_flush(struct ft232r_device_handle *dev)
  228. {
  229. if (!dev->obufsz)
  230. return 0;
  231. ssize_t r = ft232r_readwrite(dev, dev->o, dev->obuf, dev->obufsz);
  232. if (r == dev->obufsz) {
  233. dev->obufsz = 0;
  234. } else if (r > 0) {
  235. dev->obufsz -= r;
  236. memmove(dev->obuf, &dev->obuf[r], dev->obufsz);
  237. }
  238. return r;
  239. }
  240. ssize_t ft232r_write(struct ft232r_device_handle *dev, void *data, size_t count)
  241. {
  242. uint16_t bufleft;
  243. ssize_t r;
  244. bufleft = dev->osz - dev->obufsz;
  245. if (count < bufleft) {
  246. // Just add to output buffer
  247. memcpy(&dev->obuf[dev->obufsz], data, count);
  248. dev->obufsz += count;
  249. return count;
  250. }
  251. // Fill up buffer and flush
  252. memcpy(&dev->obuf[dev->obufsz], data, bufleft);
  253. dev->obufsz += bufleft;
  254. r = ft232r_flush(dev);
  255. if (unlikely(r <= 0)) {
  256. // In this case, no bytes were written supposedly, so remove this data from buffer
  257. dev->obufsz -= bufleft;
  258. return r;
  259. }
  260. // Even if not all <bufleft> bytes from this write got out, the remaining are still buffered
  261. return bufleft;
  262. }
  263. typedef ssize_t (*ft232r_rwfunc_t)(struct ft232r_device_handle *, void*, size_t);
  264. static ssize_t ft232r_rw_all(ft232r_rwfunc_t rwfunc, struct ft232r_device_handle *dev, void *data, size_t count)
  265. {
  266. char *p = data;
  267. ssize_t writ, total = 0;
  268. while (count && (writ = rwfunc(dev, p, count)) > 0) {
  269. p += writ;
  270. count -= writ;
  271. total += writ;
  272. }
  273. return total ?: writ;
  274. }
  275. ssize_t ft232r_write_all(struct ft232r_device_handle *dev, void *data, size_t count)
  276. {
  277. return ft232r_rw_all(ft232r_write, dev, data, count);
  278. }
  279. ssize_t ft232r_read(struct ft232r_device_handle *dev, void *data, size_t count)
  280. {
  281. ssize_t r;
  282. int adj;
  283. // Flush any pending output before reading
  284. r = ft232r_flush(dev);
  285. if (r < 0)
  286. return r;
  287. // First 2 bytes of every 0x40 are FTDI status or something
  288. while (dev->ibufLen <= 2) {
  289. // TODO: Implement a timeout for status byte repeating
  290. int transferred = ft232r_readwrite(dev, dev->i, dev->ibuf, sizeof(dev->ibuf));
  291. if (transferred <= 0)
  292. return transferred;
  293. dev->ibufLen = transferred;
  294. for (adj = 0x40; dev->ibufLen > adj; adj += 0x40 - 2) {
  295. dev->ibufLen -= 2;
  296. memmove(&dev->ibuf[adj], &dev->ibuf[adj+2], dev->ibufLen - adj);
  297. }
  298. }
  299. unsigned char *ibufs = &dev->ibuf[2];
  300. size_t ibufsLen = dev->ibufLen - 2;
  301. if (count > ibufsLen)
  302. count = ibufsLen;
  303. memcpy(data, ibufs, count);
  304. dev->ibufLen -= count;
  305. ibufsLen -= count;
  306. if (ibufsLen) {
  307. memmove(ibufs, &ibufs[count], ibufsLen);
  308. applog(LOG_DEBUG, "ft232r_read: %u bytes extra", ibufsLen);
  309. }
  310. return count;
  311. }
  312. ssize_t ft232r_read_all(struct ft232r_device_handle *dev, void *data, size_t count)
  313. {
  314. return ft232r_rw_all(ft232r_read, dev, data, count);
  315. }
  316. bool ft232r_get_pins(struct ft232r_device_handle *dev, uint8_t *pins)
  317. {
  318. return libusb_control_transfer(dev->h, FTDI_REQTYPE_IN, FTDI_REQUEST_GET_PINS, 0, FTDI_INDEX, pins, 1, FTDI_TIMEOUT) == 1;
  319. }
  320. bool ft232r_get_bitmode(struct ft232r_device_handle *dev, uint8_t *out_mode)
  321. {
  322. return libusb_control_transfer(dev->h, FTDI_REQTYPE_IN, FTDI_REQUEST_GET_BITMODE, 0, FTDI_INDEX, out_mode, 1, FTDI_TIMEOUT) == 1;
  323. }
  324. bool ft232r_set_cbus_bits(struct ft232r_device_handle *dev, bool sc, bool cs)
  325. {
  326. uint8_t pin_state = (cs ? (1<<2) : 0)
  327. | (sc ? (1<<3) : 0);
  328. return ft232r_set_bitmode(dev, 0xc0 | pin_state, 0x20);
  329. }
  330. bool ft232r_get_cbus_bits(struct ft232r_device_handle *dev, bool *out_sio0, bool *out_sio1)
  331. {
  332. uint8_t data;
  333. if (!ft232r_get_bitmode(dev, &data))
  334. return false;
  335. *out_sio0 = data & 1;
  336. *out_sio1 = data & 2;
  337. return true;
  338. }
  339. #if 0
  340. int main() {
  341. libusb_init(NULL);
  342. ft232r_scan();
  343. ft232r_scan_free();
  344. libusb_exit(NULL);
  345. }
  346. void applog(int prio, const char *fmt, ...)
  347. {
  348. va_list ap;
  349. va_start(ap, fmt);
  350. vprintf(fmt, ap);
  351. puts("");
  352. va_end(ap);
  353. }
  354. #endif