40#define _FTDI_DISABLE_DEPRECATED
42#include "ftdi_version_i.h"
44#define ftdi_error_return(code, str) do { \
46 ftdi->error_str = str; \
48 fprintf(stderr, str); \
52#define ftdi_error_return_free_device_list(code, str, devs) do { \
53 libusb_free_device_list(devs,1); \
54 ftdi->error_str = str; \
68static void ftdi_usb_close_internal (
struct ftdi_context *ftdi)
111 if (libusb_init(&ftdi->
usb_ctx) < 0)
172 if (ftdi->
index != check_interface)
219 ftdi_usb_close_internal (ftdi);
289 ver.
major = FTDI_MAJOR_VERSION;
290 ver.
minor = FTDI_MINOR_VERSION;
291 ver.
micro = FTDI_MICRO_VERSION;
318 libusb_device **devs;
322 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
328 while ((
dev = devs[i++]) != NULL)
330 struct libusb_device_descriptor desc;
332 if (libusb_get_device_descriptor(dev, &desc) < 0)
335 if (((vendor || product) &&
336 desc.idVendor == vendor && desc.idProduct == product) ||
337 (!(vendor || product) &&
338 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
339 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
340 || desc.idProduct == 0x6015)))
346 (*curdev)->next = NULL;
347 (*curdev)->dev =
dev;
348 libusb_ref_device(
dev);
349 curdev = &(*curdev)->
next;
353 libusb_free_device_list(devs,1);
366 for (curdev = *devlist; curdev != NULL;)
369 libusb_unref_device(curdev->
dev);
414 struct libusb_device *
dev,
415 char *manufacturer,
int mnf_len,
416 char *description,
int desc_len,
417 char *serial,
int serial_len)
421 if ((ftdi==NULL) || (
dev==NULL))
432 manufacturer, mnf_len,
433 description, desc_len,
439 ftdi_usb_close_internal(ftdi);
471 char *manufacturer,
int mnf_len,
472 char *description,
int desc_len,
473 char *serial,
int serial_len)
475 struct libusb_device_descriptor desc;
478 if ((ftdi==NULL) || (dev==NULL))
481 need_open = (ftdi->
usb_dev == NULL);
482 if (need_open && libusb_open(dev, &ftdi->
usb_dev) < 0)
485 if (libusb_get_device_descriptor(dev, &desc) < 0)
488 if (manufacturer != NULL)
490 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iManufacturer, (
unsigned char *)manufacturer, mnf_len) < 0)
492 ftdi_usb_close_internal (ftdi);
497 if (description != NULL)
499 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)description, desc_len) < 0)
501 ftdi_usb_close_internal (ftdi);
508 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)serial, serial_len) < 0)
510 ftdi_usb_close_internal (ftdi);
516 ftdi_usb_close_internal (ftdi);
527static unsigned int _ftdi_determine_max_packet_size(
struct ftdi_context *ftdi, libusb_device *dev)
529 struct libusb_device_descriptor desc;
530 struct libusb_config_descriptor *config0;
531 unsigned int packet_size;
534 if (ftdi == NULL || dev == NULL)
545 if (libusb_get_device_descriptor(dev, &desc) < 0)
548 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
551 if (desc.bNumConfigurations > 0)
553 if (ftdi->
interface < config0->bNumInterfaces)
555 struct libusb_interface interface = config0->interface[ftdi->
interface];
556 if (interface.num_altsetting > 0)
558 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
559 if (descriptor.bNumEndpoints > 0)
561 packet_size = descriptor.endpoint[0].wMaxPacketSize;
567 libusb_free_config_descriptor (config0);
591 struct libusb_device_descriptor desc;
592 struct libusb_config_descriptor *config0;
593 int cfg, cfg0, detach_errno = 0;
598 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
601 if (libusb_get_device_descriptor(dev, &desc) < 0)
604 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
606 cfg0 = config0->bConfigurationValue;
607 libusb_free_config_descriptor (config0);
618 detach_errno = errno;
622 if (libusb_set_auto_detach_kernel_driver(ftdi->
usb_dev, 1) != LIBUSB_SUCCESS)
623 detach_errno = errno;
626 if (libusb_get_configuration (ftdi->
usb_dev, &cfg) < 0)
631 if (desc.bNumConfigurations > 0 && cfg != cfg0)
633 if (libusb_set_configuration(ftdi->
usb_dev, cfg0) < 0)
635 ftdi_usb_close_internal (ftdi);
636 if (detach_errno == EPERM)
642 ftdi_error_return(-3,
"unable to set usb configuration. Make sure the default FTDI driver is not in use");
649 ftdi_usb_close_internal (ftdi);
650 if (detach_errno == EPERM)
656 ftdi_error_return(-5,
"unable to claim usb device. Make sure the default FTDI driver is not in use");
662 ftdi_usb_close_internal (ftdi);
668 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
669 && desc.iSerialNumber == 0))
671 else if (desc.bcdDevice == 0x200)
673 else if (desc.bcdDevice == 0x500)
675 else if (desc.bcdDevice == 0x600)
677 else if (desc.bcdDevice == 0x700)
679 else if (desc.bcdDevice == 0x800)
681 else if (desc.bcdDevice == 0x900)
683 else if (desc.bcdDevice == 0x1000)
691 ftdi_usb_close_internal (ftdi);
734 const char* description,
const char* serial)
765 const char* description,
const char* serial,
unsigned int index)
768 libusb_device **devs;
775 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
778 while ((dev = devs[i++]) != NULL)
780 struct libusb_device_descriptor desc;
783 if (libusb_get_device_descriptor(dev, &desc) < 0)
786 if (desc.idVendor == vendor && desc.idProduct == product)
788 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
791 if (description != NULL)
793 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)
string,
sizeof(
string)) < 0)
795 ftdi_usb_close_internal (ftdi);
798 if (strncmp(
string, description,
sizeof(
string)) != 0)
800 ftdi_usb_close_internal (ftdi);
806 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)
string,
sizeof(
string)) < 0)
808 ftdi_usb_close_internal (ftdi);
811 if (strncmp(
string, serial,
sizeof(
string)) != 0)
813 ftdi_usb_close_internal (ftdi);
818 ftdi_usb_close_internal (ftdi);
827 libusb_free_device_list(devs,1);
860 libusb_device **devs;
866 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
869 while ((dev = devs[i++]) != NULL)
871 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
875 libusb_free_device_list(devs,1);
915 if (description[0] == 0 || description[1] !=
':')
918 if (description[0] ==
'd')
921 libusb_device **devs;
922 unsigned int bus_number, device_address;
925 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
929 if (sscanf (description + 2,
"%u/%u", &bus_number, &device_address) != 2)
932 while ((dev = devs[i++]) != NULL)
935 if (bus_number == libusb_get_bus_number (dev)
936 && device_address == libusb_get_device_address (dev))
939 libusb_free_device_list(devs,1);
947 else if (description[0] ==
'i' || description[0] ==
's')
950 unsigned int product;
951 unsigned int index=0;
952 const char *serial=NULL;
953 const char *startp, *endp;
956 startp=description+2;
957 vendor=strtoul((
char*)startp,(
char**)&endp,0);
958 if (*endp !=
':' || endp == startp || errno != 0)
962 product=strtoul((
char*)startp,(
char**)&endp,0);
963 if (endp == startp || errno != 0)
966 if (description[0] ==
'i' && *endp != 0)
973 index=strtoul((
char*)startp,(
char**)&endp,0);
974 if (*endp != 0 || endp == startp || errno != 0)
977 if (description[0] ==
's')
1005 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1032 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1062 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1089 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1115 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1141 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1172 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1208 ftdi_usb_close_internal (ftdi);
1225static int ftdi_to_clkbits_AM(
int baudrate,
unsigned long *encoded_divisor)
1228 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1229 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1230 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1231 int divisor, best_divisor, best_baud, best_baud_diff;
1233 divisor = 24000000 / baudrate;
1236 divisor -= am_adjust_dn[divisor & 7];
1242 for (i = 0; i < 2; i++)
1244 int try_divisor = divisor + i;
1249 if (try_divisor <= 8)
1254 else if (divisor < 16)
1262 try_divisor += am_adjust_up[try_divisor & 7];
1263 if (try_divisor > 0x1FFF8)
1266 try_divisor = 0x1FFF8;
1270 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1272 if (baud_estimate < baudrate)
1274 baud_diff = baudrate - baud_estimate;
1278 baud_diff = baud_estimate - baudrate;
1280 if (i == 0 || baud_diff < best_baud_diff)
1283 best_divisor = try_divisor;
1284 best_baud = baud_estimate;
1285 best_baud_diff = baud_diff;
1294 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1296 if (*encoded_divisor == 1)
1298 *encoded_divisor = 0;
1300 else if (*encoded_divisor == 0x4001)
1302 *encoded_divisor = 1;
1331static int ftdi_to_clkbits(
int baudrate,
unsigned int clk,
int clk_div,
unsigned long *encoded_divisor)
1333 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1335 int divisor, best_divisor;
1336 if (baudrate >= clk/clk_div)
1338 *encoded_divisor = 0;
1339 best_baud = clk/clk_div;
1341 else if (baudrate >= clk/(clk_div + clk_div/2))
1343 *encoded_divisor = 1;
1344 best_baud = clk/(clk_div + clk_div/2);
1346 else if (baudrate >= clk/(2*clk_div))
1348 *encoded_divisor = 2;
1349 best_baud = clk/(2*clk_div);
1354 divisor = clk*16/clk_div / baudrate;
1356 best_divisor = divisor /2 +1;
1358 best_divisor = divisor/2;
1359 if(best_divisor > 0x20000)
1360 best_divisor = 0x1ffff;
1361 best_baud = clk*16/clk_div/best_divisor;
1363 best_baud = best_baud /2 +1;
1365 best_baud = best_baud /2;
1366 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1375static int ftdi_convert_baudrate(
int baudrate,
struct ftdi_context *ftdi,
1376 unsigned short *value,
unsigned short *index)
1379 unsigned long encoded_divisor;
1387#define H_CLK 120000000
1388#define C_CLK 48000000
1391 if(baudrate*10 >
H_CLK /0x3fff)
1398 best_baud = ftdi_to_clkbits(baudrate,
H_CLK, 10, &encoded_divisor);
1399 encoded_divisor |= 0x20000;
1402 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1406 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1410 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1413 *value = (
unsigned short)(encoded_divisor & 0xFFFF);
1416 *index = (
unsigned short)(encoded_divisor >> 8);
1418 *index |= ftdi->
index;
1421 *index = (
unsigned short)(encoded_divisor >> 16);
1432 unsigned short *value,
unsigned short *index)
1434 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1450 unsigned short value, index;
1451 int actual_baudrate;
1453 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1458 baudrate = baudrate*4;
1461 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1462 if (actual_baudrate <= 0)
1466 if ((actual_baudrate * 2 < baudrate )
1467 || ((actual_baudrate < baudrate)
1468 ? (actual_baudrate * 21 < baudrate * 20)
1469 : (baudrate * 21 < actual_baudrate * 20)))
1470 ftdi_error_return (-1,
"Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1517 unsigned short value = bits;
1519 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1525 value |= (0x00 << 8);
1528 value |= (0x01 << 8);
1531 value |= (0x02 << 8);
1534 value |= (0x03 << 8);
1537 value |= (0x04 << 8);
1544 value |= (0x00 << 11);
1547 value |= (0x01 << 11);
1550 value |= (0x02 << 11);
1557 value |= (0x00 << 14);
1560 value |= (0x01 << 14);
1588 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1591 while (offset < size)
1595 if (offset+write_size > size)
1596 write_size = size-offset;
1601 offset += actual_length;
1607static void LIBUSB_CALL ftdi_read_data_cb(
struct libusb_transfer *transfer)
1611 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1615 actual_length =
transfer->actual_length;
1617 if (actual_length > 2)
1621 num_of_chunks = actual_length / packet_size;
1622 chunk_remains = actual_length % packet_size;
1628 if (actual_length > packet_size - 2)
1630 for (i = 1; i < num_of_chunks; i++)
1634 if (chunk_remains > 2)
1639 actual_length -= 2*num_of_chunks;
1642 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1645 if (actual_length > 0)
1652 tc->
offset += actual_length;
1684 if (
transfer->status == LIBUSB_TRANSFER_CANCELLED)
1685 tc->
completed = LIBUSB_TRANSFER_CANCELLED;
1688 ret = libusb_submit_transfer (
transfer);
1695static void LIBUSB_CALL ftdi_write_data_cb(
struct libusb_transfer *
transfer)
1717 if (
transfer->status == LIBUSB_TRANSFER_CANCELLED)
1718 tc->
completed = LIBUSB_TRANSFER_CANCELLED;
1721 ret = libusb_submit_transfer (
transfer);
1746 struct libusb_transfer *transfer;
1747 int write_size, ret;
1749 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1756 transfer = libusb_alloc_transfer(0);
1775 write_size, ftdi_write_data_cb, tc,
1777 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1779 ret = libusb_submit_transfer(
transfer);
1808 struct libusb_transfer *transfer;
1811 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1848 transfer = libusb_alloc_transfer(0);
1859 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1861 ret = libusb_submit_transfer(
transfer);
1887 struct timeval to = { 0, 0 };
1890 ret = libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1894 if (ret == LIBUSB_ERROR_INTERRUPTED)
1896 libusb_cancel_transfer(tc->
transfer);
1898 if (libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1901 libusb_free_transfer(tc->
transfer);
1914 if (tc->
transfer->status != LIBUSB_TRANSFER_COMPLETED)
1916 libusb_free_transfer(tc->
transfer);
1932 struct timeval * to)
1934 struct timeval tv = { 0, 0 };
1941 libusb_cancel_transfer(tc->
transfer);
1950 libusb_free_transfer(tc->
transfer);
2009 int offset = 0, ret, i, num_of_chunks, chunk_remains;
2011 int actual_length = 1;
2013 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2018 if (packet_size == 0)
2043 while (offset < size && actual_length > 0)
2052 if (actual_length > 2)
2056 num_of_chunks = actual_length / packet_size;
2057 chunk_remains = actual_length % packet_size;
2063 if (actual_length > packet_size - 2)
2065 for (i = 1; i < num_of_chunks; i++)
2069 if (chunk_remains > 2)
2074 actual_length -= 2*num_of_chunks;
2077 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
2080 else if (actual_length <= 2)
2085 if (actual_length > 0)
2088 if (offset+actual_length <= size)
2092 offset += actual_length;
2103 int part_size = size-offset;
2108 offset += part_size;
2135 unsigned char *new_buf;
2148 if (chunksize > 16384)
2152 if ((new_buf = (
unsigned char *)realloc(ftdi->
readbuffer, chunksize)) == NULL)
2193 unsigned short usb_val;
2195 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2199 usb_val |= (mode << 8);
2201 ftdi_error_return(-1,
"unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2219 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2223 ftdi_error_return(-1,
"unable to leave bitbang mode. Perhaps not a BM type chip?");
2242 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2268 unsigned short usb_val;
2273 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2295 unsigned short usb_val;
2297 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2303 *latency = (
unsigned char)usb_val;
2351 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2357 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2377 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2401 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2404 uint16_t xonxoff = xon | (xoff << 8);
2425 unsigned short usb_val;
2427 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2455 unsigned short usb_val;
2457 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2486 unsigned short usb_val;
2488 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2521 unsigned char eventch,
unsigned char enable)
2523 unsigned short usb_val;
2525 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2550 unsigned char errorch,
unsigned char enable)
2552 unsigned short usb_val;
2554 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2580 char * product,
char * serial)
2587 if (ftdi->
eeprom == NULL)
2637 const char* default_product;
2640 case TYPE_AM: default_product =
"AM";
break;
2641 case TYPE_BM: default_product =
"BM";
break;
2642 case TYPE_2232C: default_product =
"Dual RS232";
break;
2643 case TYPE_R: default_product =
"FT232R USB UART";
break;
2644 case TYPE_2232H: default_product =
"Dual RS232-HS";
break;
2645 case TYPE_4232H: default_product =
"FT4232H";
break;
2646 case TYPE_232H: default_product =
"Single-RS232-HS";
break;
2647 case TYPE_230X: default_product =
"FT230X Basic UART";
break;
2651 eeprom->
product = (
char *)malloc(strlen(default_product) +1);
2653 strcpy(eeprom->
product, default_product);
2669 eeprom->
size = 0x80;
2679 eeprom->
size = 0x100;
2690 for (i=0; i<10; i++)
2735 if (ftdi->
eeprom == NULL)
2796 char *
serial,
int serial_len)
2802 if (ftdi->
eeprom == NULL)
2825 serial[serial_len - 1] =
'\0';
2837 int mode_low, mode_high;
2847 output[0x18+i] = (mode_high <<4) | mode_low;
2912 unsigned char i, j, eeprom_size_mask;
2913 unsigned short checksum, value;
2914 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2915 int user_area_size, free_start, free_end;
2917 unsigned char * output;
2921 if (ftdi->
eeprom == NULL)
2925 output = eeprom->
buf;
2927 if (eeprom->
chip == -1)
2930 if (eeprom->
size == -1)
2932 if ((eeprom->
chip == 0x56) || (eeprom->
chip == 0x66))
2933 eeprom->
size = 0x100;
2935 eeprom->
size = 0x80;
2941 product_size = strlen(eeprom->
product);
2942 if (eeprom->
serial != NULL)
2943 serial_size = strlen(eeprom->
serial);
2951 user_area_size = 96;
2954 user_area_size = 90;
2957 user_area_size = 88;
2961 user_area_size = 86;
2964 user_area_size = 80;
2970 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2972 if (user_area_size < 0)
3061 eeprom_size_mask = eeprom->
size -1;
3062 free_end = i & eeprom_size_mask;
3068 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
3069 output[i & eeprom_size_mask] = 0x03, i++;
3070 for (j = 0; j < manufacturer_size; j++)
3072 output[i & eeprom_size_mask] = eeprom->
manufacturer[j], i++;
3073 output[i & eeprom_size_mask] = 0x00, i++;
3075 output[0x0F] = manufacturer_size*2 + 2;
3079 output[0x10] = i | 0x80;
3080 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
3081 output[i & eeprom_size_mask] = 0x03, i++;
3082 for (j = 0; j < product_size; j++)
3084 output[i & eeprom_size_mask] = eeprom->
product[j], i++;
3085 output[i & eeprom_size_mask] = 0x00, i++;
3087 output[0x11] = product_size*2 + 2;
3091 output[0x12] = i | 0x80;
3092 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
3093 output[i & eeprom_size_mask] = 0x03, i++;
3094 for (j = 0; j < serial_size; j++)
3096 output[i & eeprom_size_mask] = eeprom->
serial[j], i++;
3097 output[i & eeprom_size_mask] = 0x00, i++;
3103 output[i & eeprom_size_mask] = 0x02;
3105 output[i & eeprom_size_mask] = 0x03;
3107 output[i & eeprom_size_mask] = eeprom->
is_not_pnp;
3111 output[0x13] = serial_size*2 + 2;
3118 output[0x0A] &= ~USE_SERIAL_NUM;
3133 output[0x0A] &= ~USE_USB_VERSION_BIT;
3142 output[0x00] &= ~DRIVER_VCP;
3147 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3153 output[0x01] &= ~DRIVER_VCP;
3158 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3161 output[0x0A] |= 0x1;
3163 output[0x0A] &= ~0x1;
3165 output[0x0A] |= 0x2;
3167 output[0x0A] &= ~0x2;
3169 output[0x0A] |= 0x4;
3171 output[0x0A] &= ~0x4;
3175 output[0x0A] &= ~USE_USB_VERSION_BIT;
3179 output[0x14] = eeprom->
chip;
3188 output[0x00] &= ~DRIVER_VCP;
3193 output[0x00] |= 0x02;
3194 output[0x01] = 0x40;
3197 output[0x0A] |= 0x4;
3199 output[0x0A] &= ~0x4;
3200 output[0x0B] = eeprom->
invert;
3234 output[0x00] &= ~DRIVER_VCP;
3240 output[0x01] &= ~DRIVER_VCP;
3245 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3248 output[0x0A] |= 0x4;
3250 output[0x0A] &= ~0x4;
3288 output[0x18] = eeprom->
chip;
3295 output[0x00] &= ~DRIVER_VCP;
3299 output[0x01] &= ~DRIVER_VCP;
3310 output[0x0a] |= 0x4;
3312 output[0x0a] &= ~0x4;
3367 output[0x18] = eeprom->
chip;
3375 output[0x00] &= ~DRIVER_VCPH;
3380 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3383 output[0x0a] |= 0x4;
3385 output[0x0a] &= ~0x4;
3390 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3394 output[0x01] &= ~FT1284_DATA_LSB;
3398 output[0x01] &= ~FT1284_FLOW_CONTROL;
3420 output[0x1e] = eeprom->
chip;
3421 fprintf(stderr,
"FIXME: Build FT232H specific EEPROM settings\n");
3424 output[0x00] = 0x80;
3427 for (j = 0; j <= 6; j++)
3431 output[0x0b] = eeprom->
invert;
3459 fprintf(stderr,
"Warning, user data starts inside the generated data!\n");
3461 fprintf(stderr,
"Warning, user data overlaps the strings area!\n");
3470 for (i = 0; i < eeprom->
size/2-1; i++)
3477 if ((ftdi->
type ==
TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3480 fprintf(stderr,
"Reading Factory Configuration Data failed\n");
3486 value = output[i*2];
3487 value += output[(i*2)+1] << 8;
3489 checksum = value^checksum;
3490 checksum = (checksum << 1) | (checksum >> 15);
3493 output[eeprom->
size-2] = checksum;
3494 output[eeprom->
size-1] = checksum >> 8;
3497 return user_area_size;
3504static unsigned char bit2type(
unsigned char bits)
3514 fprintf(stderr,
" Unexpected value %d for Hardware Interface type\n",
3522static void print_inverted_bits(
int invert)
3524 const char *r_bits[] = {
"TXD",
"RXD",
"RTS",
"CTS",
"DTR",
"DSR",
"DCD",
"RI"};
3527 fprintf(stdout,
"Inverted bits:");
3529 if ((
invert & (1<<i)) == (1<<i))
3530 fprintf(stdout,
" %s",r_bits[i]);
3532 fprintf(stdout,
"\n");
3551 unsigned short checksum, eeprom_checksum, value;
3552 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3555 unsigned char *
buf = NULL;
3559 if (ftdi->
eeprom == NULL)
3563 eeprom_size = eeprom->
size;
3607 manufacturer_size =
buf[0x0F]/2;
3610 if (manufacturer_size > 0)
3612 eeprom->
manufacturer = (
char *)malloc(manufacturer_size);
3616 i =
buf[0x0E] & (eeprom_size -1);
3617 for (j=0; j<manufacturer_size-1; j++)
3630 product_size =
buf[0x11]/2;
3631 if (product_size > 0)
3633 eeprom->
product = (
char *)malloc(product_size);
3637 i =
buf[0x10] & (eeprom_size -1);
3638 for (j=0; j<product_size-1; j++)
3651 serial_size =
buf[0x13]/2;
3652 if (serial_size > 0)
3654 eeprom->
serial = (
char *)malloc(serial_size);
3658 i =
buf[0x12] & (eeprom_size -1);
3659 for (j=0; j<serial_size-1; j++)
3663 eeprom->
serial[j] =
'\0';
3666 else eeprom->
serial = NULL;
3671 for (i = 0; i < eeprom_size/2-1; i++)
3679 value +=
buf[(i*2)+1] << 8;
3681 checksum = value^checksum;
3682 checksum = (checksum << 1) | (checksum >> 15);
3685 eeprom_checksum =
buf[eeprom_size-2] + (
buf[eeprom_size-1] << 8);
3687 if (eeprom_checksum != checksum)
3689 fprintf(stderr,
"Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3714 if ( (
buf[0x01]&0x40) != 0x40)
3716 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3717 " If this happened with the\n"
3718 " EEPROM programmed by FTDI tools, please report "
3719 "to libftdi@developer.intra2net.com\n");
3810 const char *channel_mode[] = {
"UART",
"FIFO",
"CPU",
"OPTO",
"FT1284"};
3811 fprintf(stdout,
"VID: 0x%04x\n",eeprom->
vendor_id);
3812 fprintf(stdout,
"PID: 0x%04x\n",eeprom->
product_id);
3816 fprintf(stdout,
"Self-Powered%s", (eeprom->
remote_wakeup)?
", USB Remote Wake Up\n":
"\n");
3818 fprintf(stdout,
"Bus Powered: %3d mA%s", eeprom->
max_power,
3821 fprintf(stdout,
"Manufacturer: %s\n",eeprom->
manufacturer);
3823 fprintf(stdout,
"Product: %s\n",eeprom->
product);
3825 fprintf(stdout,
"Serial: %s\n",eeprom->
serial);
3826 fprintf(stdout,
"Checksum : %04x\n", checksum);
3828 fprintf(stdout,
"Internal EEPROM\n");
3831 else if (eeprom->
chip >= 0x46)
3832 fprintf(stdout,
"Attached EEPROM: 93x%02x\n", eeprom->
chip);
3834 fprintf(stdout,
"Suspend on DBUS7\n");
3836 fprintf(stdout,
"Pull IO pins low during suspend\n");
3840 fprintf(stdout,
"Enter low power state on ACBUS7\n");
3843 fprintf(stdout,
"Enable Remote Wake Up\n");
3844 fprintf(stdout,
"PNP: %d\n",(eeprom->
is_not_pnp)?0:1);
3846 fprintf(stdout,
"Channel A has Mode %s%s%s\n",
3852 fprintf(stdout,
"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3858 fprintf(stdout,
"Channel B has Mode %s%s%s\n",
3864 fprintf(stdout,
"Use explicit USB Version %04x\n",eeprom->
usb_version);
3868 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3873 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3878 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3883 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3891 const char *cbush_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3892 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3893 "CLK30",
"CLK15",
"CLK7_5"
3895 fprintf(stdout,
"ACBUS has %d mA drive%s%s\n",
3899 fprintf(stdout,
"ADBUS has %d mA drive%s%s\n",
3903 for (i=0; i<10; i++)
3906 fprintf(stdout,
"C%d Function: %s\n", i,
3912 const char *cbusx_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3913 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3914 "CLK24",
"CLK12",
"CLK6",
"BAT_DETECT",
"BAT_DETECT#",
3915 "I2C_TXE#",
"I2C_RXF#",
"VBUS_SENSE",
"BB_WR#",
3916 "BBRD#",
"TIME_STAMP",
"AWAKE#",
3918 fprintf(stdout,
"DBUS has %d mA drive%s%s\n",
3922 fprintf(stdout,
"CBUS has %d mA drive%s%s\n",
3929 fprintf(stdout,
"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->
cbus_function[i]]);
3933 print_inverted_bits(eeprom->
invert);
3938 const char *cbus_mux[] = {
"TXDEN",
"PWREN",
"RXLED",
"TXLED",
"TX+RXLED",
3939 "SLEEP",
"CLK48",
"CLK24",
"CLK12",
"CLK6",
3940 "IOMODE",
"BB_WR",
"BB_RD"
3942 const char *cbus_BB[] = {
"RXF",
"TXE",
"RD",
"WR"};
3945 print_inverted_bits(eeprom->
invert);
3950 fprintf(stdout,
"C%d Function: %s\n", i,
3957 fprintf(stdout,
"C%d BB Function: %s\n", i,
3960 fprintf(stdout,
"Unknown CBUS mode. Might be special mode?\n");
4369 if (!ftdi || !(ftdi->
eeprom))
4372 if (!
buf || size < ftdi->eeprom->size)
4439 unsigned char buf[2];
4441 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4447 *eeprom_val = (0xff &
buf[0]) | (
buf[1] << 8);
4466 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4472 if (libusb_control_transfer(
4484 else if (memcmp(
buf,&
buf[0x80],0x80) == 0)
4486 else if (memcmp(
buf,&
buf[0x40],0x40) == 0)
4498static unsigned char ftdi_read_chipid_shift(
unsigned char value)
4500 return ((value & 1) << 1) |
4501 ((value & 2) << 5) |
4502 ((value & 4) >> 2) |
4503 ((value & 8) << 4) |
4504 ((value & 16) >> 1) |
4505 ((value & 32) >> 1) |
4506 ((value & 64) >> 4) |
4507 ((value & 128) >> 2);
4522 unsigned int a = 0, b = 0;
4524 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4529 a = a << 8 | a >> 8;
4532 b = b << 8 | b >> 8;
4533 a = (a << 16) | (b & 0xFFFF);
4534 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4535 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4536 *chipid = a ^ 0xa5f0f7d1;
4559 unsigned short eeprom_val)
4561 int chip_type_location;
4562 unsigned short chip_type;
4564 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4567 if (eeprom_addr <0x80)
4575 chip_type_location = 0x14;
4579 chip_type_location = 0x18;
4582 chip_type_location = 0x1e;
4590 fprintf(stderr,
" loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4591 if ((chip_type & 0xff) != 0x66)
4616 unsigned short usb_val, status;
4618 unsigned char *eeprom;
4620 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4643 usb_val = eeprom[i*2];
4644 usb_val += eeprom[(i*2)+1] << 8;
4671 unsigned short eeprom_value;
4672 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4696 if (eeprom_value ==
MAGIC)
4704 if (eeprom_value ==
MAGIC)
4710 if (eeprom_value ==
MAGIC)
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
struct ftdi_transfer_control * ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev, char *manufacturer, int mnf_len, char *description, int desc_len, char *serial, int serial_len)
int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char *buf, int size)
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
void ftdi_set_usbdev(struct ftdi_context *ftdi, libusb_device_handle *usb)
int ftdi_init(struct ftdi_context *ftdi)
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
int ftdi_erase_eeprom(struct ftdi_context *ftdi)
int ftdi_usb_reset(struct ftdi_context *ftdi)
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
void ftdi_list_free(struct ftdi_device_list **devlist)
int ftdi_tciflush(struct ftdi_context *ftdi)
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial, unsigned int index)
void ftdi_free(struct ftdi_context *ftdi)
int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
int ftdi_disable_bitbang(struct ftdi_context *ftdi)
int ftdi_setdtr(struct ftdi_context *ftdi, int state)
int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type)
void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char *output)
#define ftdi_error_return_free_device_list(code, str, devs)
int ftdi_setrts(struct ftdi_context *ftdi, int state)
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, const char *manufacturer, const char *product, const char *serial)
int ftdi_read_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
struct ftdi_transfer_control * ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable)
void ftdi_deinit(struct ftdi_context *ftdi)
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
const char * ftdi_get_error_string(struct ftdi_context *ftdi)
int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
#define ftdi_error_return(code, str)
int ftdi_write_eeprom(struct ftdi_context *ftdi)
int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi, unsigned short *value, unsigned short *index)
Wrapper function to export ftdi_convert_baudrate() to the unit test Do not use, it's only for the uni...
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial)
int ftdi_usb_close(struct ftdi_context *ftdi)
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
void ftdi_list_free2(struct ftdi_device_list *devlist)
int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char *buf, int size)
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char *description)
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
struct ftdi_context * ftdi_new(void)
int ftdi_tcoflush(struct ftdi_context *ftdi)
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
int ftdi_read_eeprom(struct ftdi_context *ftdi)
int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, struct timeval *to)
int ftdi_eeprom_build(struct ftdi_context *ftdi)
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev, char *manufacturer, int mnf_len, char *description, int desc_len, char *serial, int serial_len)
int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable)
int ftdi_tcioflush(struct ftdi_context *ftdi)
int ftdi_eeprom_get_strings(struct ftdi_context *ftdi, char *manufacturer, int mnf_len, char *product, int prod_len, char *serial, int serial_len)
struct ftdi_version_info ftdi_get_library_version(void)
Get libftdi library version.
int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
#define HIGH_CURRENT_DRIVE
#define FT1284_CLK_IDLE_STATE
#define SIO_SET_DATA_REQUEST
#define SIO_RESET_PURGE_TX
#define CHANNEL_IS_FT1284
#define SIO_SET_EVENT_CHAR_REQUEST
#define SIO_RESET_REQUEST
#define SIO_SET_ERROR_CHAR_REQUEST
#define SIO_SET_MODEM_CTRL_REQUEST
#define SIO_READ_PINS_REQUEST
#define SIO_SET_LATENCY_TIMER_REQUEST
#define POWER_SAVE_DISABLE_H
#define SIO_ERASE_EEPROM_REQUEST
#define FT1284_FLOW_CONTROL
#define SIO_SET_BAUDRATE_REQUEST
#define SIO_READ_EEPROM_REQUEST
#define SIO_POLL_MODEM_STATUS_REQUEST
#define SIO_WRITE_EEPROM_REQUEST
#define FTDI_DEVICE_IN_REQTYPE
#define SIO_SET_FLOW_CTRL_REQUEST
#define SIO_GET_LATENCY_TIMER_REQUEST
#define SUSPEND_DBUS7_BIT
@ AUTO_DETACH_REATACH_SIO_MODULE
#define HIGH_CURRENT_DRIVE_R
#define SIO_RESET_PURGE_RX
#define USE_USB_VERSION_BIT
#define SIO_SET_BITMODE_REQUEST
#define FTDI_DEVICE_OUT_REQTYPE
#define FTDI_MAX_EEPROM_SIZE
#define MAX_POWER_MILLIAMP_PER_UNIT
Main context structure for all libftdi functions.
unsigned char bitbang_mode
struct ftdi_eeprom * eeprom
struct libusb_context * usb_ctx
enum ftdi_module_detach_mode module_detach_mode
unsigned int writebuffer_chunksize
unsigned char bitbang_enabled
struct libusb_device_handle * usb_dev
unsigned int max_packet_size
unsigned char * readbuffer
unsigned int readbuffer_chunksize
unsigned int readbuffer_remaining
unsigned int readbuffer_offset
list of usb devices created by ftdi_usb_find_all()
struct ftdi_device_list * next
struct libusb_device * dev
unsigned char buf[FTDI_MAX_EEPROM_SIZE]
int channel_c_rs485enable
int channel_d_rs485enable
int initialized_for_connected_device
int channel_a_rs485enable
int channel_b_rs485enable
struct libusb_transfer * transfer
struct ftdi_context * ftdi
const char * snapshot_str