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 && mnf_len > 0)
490 if (desc.iManufacturer == 0)
492 manufacturer[0] =
'\0';
494 else if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iManufacturer, (
unsigned char *)manufacturer, mnf_len) < 0)
496 ftdi_usb_close_internal (ftdi);
501 if (description != NULL && desc_len > 0)
503 if (desc.iProduct == 0)
505 description[0] =
'\0';
507 else if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)description, desc_len) < 0)
509 ftdi_usb_close_internal (ftdi);
514 if (serial != NULL && serial_len > 0)
516 if (desc.iSerialNumber == 0)
520 else if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)serial, serial_len) < 0)
522 ftdi_usb_close_internal (ftdi);
528 ftdi_usb_close_internal (ftdi);
539static unsigned int _ftdi_determine_max_packet_size(
struct ftdi_context *ftdi, libusb_device *dev)
541 struct libusb_device_descriptor desc;
542 struct libusb_config_descriptor *config0;
543 unsigned int packet_size;
546 if (ftdi == NULL || dev == NULL)
557 if (libusb_get_device_descriptor(dev, &desc) < 0)
560 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
563 if (desc.bNumConfigurations > 0)
565 if (ftdi->
interface < config0->bNumInterfaces)
567 struct libusb_interface interface = config0->interface[ftdi->
interface];
568 if (interface.num_altsetting > 0)
570 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
571 if (descriptor.bNumEndpoints > 0)
573 packet_size = descriptor.endpoint[0].wMaxPacketSize;
579 libusb_free_config_descriptor (config0);
603 struct libusb_device_descriptor desc;
604 struct libusb_config_descriptor *config0;
605 int cfg, cfg0, detach_errno = 0;
610 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
613 if (libusb_get_device_descriptor(dev, &desc) < 0)
616 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
618 cfg0 = config0->bConfigurationValue;
619 libusb_free_config_descriptor (config0);
630 detach_errno = errno;
634 if (libusb_set_auto_detach_kernel_driver(ftdi->
usb_dev, 1) != LIBUSB_SUCCESS)
635 detach_errno = errno;
638 if (libusb_get_configuration (ftdi->
usb_dev, &cfg) < 0)
643 if (desc.bNumConfigurations > 0 && cfg != cfg0)
645 if (libusb_set_configuration(ftdi->
usb_dev, cfg0) < 0)
647 ftdi_usb_close_internal (ftdi);
648 if (detach_errno == EPERM)
654 ftdi_error_return(-3,
"unable to set usb configuration. Make sure the default FTDI driver is not in use");
661 ftdi_usb_close_internal (ftdi);
662 if (detach_errno == EPERM)
668 ftdi_error_return(-5,
"unable to claim usb device. Make sure the default FTDI driver is not in use");
674 ftdi_usb_close_internal (ftdi);
680 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
681 && desc.iSerialNumber == 0))
683 else if (desc.bcdDevice == 0x200)
685 else if (desc.bcdDevice == 0x500)
687 else if (desc.bcdDevice == 0x600)
689 else if (desc.bcdDevice == 0x700)
691 else if (desc.bcdDevice == 0x800)
693 else if (desc.bcdDevice == 0x900)
695 else if (desc.bcdDevice == 0x1000)
703 ftdi_usb_close_internal (ftdi);
746 const char* description,
const char* serial)
777 const char* description,
const char* serial,
unsigned int index)
780 libusb_device **devs;
787 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
790 while ((dev = devs[i++]) != NULL)
792 struct libusb_device_descriptor desc;
795 if (libusb_get_device_descriptor(dev, &desc) < 0)
798 if (desc.idVendor == vendor && desc.idProduct == product)
800 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
803 if (description != NULL)
805 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)
string,
sizeof(
string)) < 0)
807 ftdi_usb_close_internal (ftdi);
810 if (strncmp(
string, description,
sizeof(
string)) != 0)
812 ftdi_usb_close_internal (ftdi);
818 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)
string,
sizeof(
string)) < 0)
820 ftdi_usb_close_internal (ftdi);
823 if (strncmp(
string, serial,
sizeof(
string)) != 0)
825 ftdi_usb_close_internal (ftdi);
830 ftdi_usb_close_internal (ftdi);
839 libusb_free_device_list(devs,1);
872 libusb_device **devs;
878 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
881 while ((dev = devs[i++]) != NULL)
883 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
887 libusb_free_device_list(devs,1);
927 if (description[0] == 0 || description[1] !=
':')
930 if (description[0] ==
'd')
933 libusb_device **devs;
934 unsigned int bus_number, device_address;
937 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
941 if (sscanf (description + 2,
"%u/%u", &bus_number, &device_address) != 2)
944 while ((dev = devs[i++]) != NULL)
947 if (bus_number == libusb_get_bus_number (dev)
948 && device_address == libusb_get_device_address (dev))
951 libusb_free_device_list(devs,1);
959 else if (description[0] ==
'i' || description[0] ==
's')
962 unsigned int product;
963 unsigned int index=0;
964 const char *serial=NULL;
965 const char *startp, *endp;
968 startp=description+2;
969 vendor=strtoul((
char*)startp,(
char**)&endp,0);
970 if (*endp !=
':' || endp == startp || errno != 0)
974 product=strtoul((
char*)startp,(
char**)&endp,0);
975 if (endp == startp || errno != 0)
978 if (description[0] ==
'i' && *endp != 0)
985 index=strtoul((
char*)startp,(
char**)&endp,0);
986 if (*endp != 0 || endp == startp || errno != 0)
989 if (description[0] ==
's')
1017 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1044 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1074 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1101 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1127 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1153 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1184 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1220 ftdi_usb_close_internal (ftdi);
1237static int ftdi_to_clkbits_AM(
int baudrate,
unsigned long *encoded_divisor)
1240 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1241 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1242 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1243 int divisor, best_divisor, best_baud, best_baud_diff;
1245 divisor = 24000000 / baudrate;
1248 divisor -= am_adjust_dn[divisor & 7];
1254 for (i = 0; i < 2; i++)
1256 int try_divisor = divisor + i;
1261 if (try_divisor <= 8)
1266 else if (divisor < 16)
1274 try_divisor += am_adjust_up[try_divisor & 7];
1275 if (try_divisor > 0x1FFF8)
1278 try_divisor = 0x1FFF8;
1282 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1284 if (baud_estimate < baudrate)
1286 baud_diff = baudrate - baud_estimate;
1290 baud_diff = baud_estimate - baudrate;
1292 if (i == 0 || baud_diff < best_baud_diff)
1295 best_divisor = try_divisor;
1296 best_baud = baud_estimate;
1297 best_baud_diff = baud_diff;
1306 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1308 if (*encoded_divisor == 1)
1310 *encoded_divisor = 0;
1312 else if (*encoded_divisor == 0x4001)
1314 *encoded_divisor = 1;
1343static int ftdi_to_clkbits(
int baudrate,
int clk,
int clk_div,
unsigned long *encoded_divisor)
1345 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1347 int divisor, best_divisor;
1348 if (baudrate >= clk/clk_div)
1350 *encoded_divisor = 0;
1351 best_baud = clk/clk_div;
1353 else if (baudrate >= clk/(clk_div + clk_div/2))
1355 *encoded_divisor = 1;
1356 best_baud = clk/(clk_div + clk_div/2);
1358 else if (baudrate >= clk/(2*clk_div))
1360 *encoded_divisor = 2;
1361 best_baud = clk/(2*clk_div);
1366 divisor = clk*16/clk_div / baudrate;
1368 best_divisor = divisor /2 +1;
1370 best_divisor = divisor/2;
1371 if(best_divisor > 0x20000)
1372 best_divisor = 0x1ffff;
1373 best_baud = clk*16/clk_div/best_divisor;
1375 best_baud = best_baud /2 +1;
1377 best_baud = best_baud /2;
1378 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1387static int ftdi_convert_baudrate(
int baudrate,
struct ftdi_context *ftdi,
1388 unsigned short *value,
unsigned short *index)
1391 unsigned long encoded_divisor;
1399#define H_CLK 120000000
1400#define C_CLK 48000000
1403 if(baudrate*10 >
H_CLK /0x3fff)
1410 best_baud = ftdi_to_clkbits(baudrate,
H_CLK, 10, &encoded_divisor);
1411 encoded_divisor |= 0x20000;
1414 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1418 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1422 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1425 *value = (
unsigned short)(encoded_divisor & 0xFFFF);
1428 *index = (
unsigned short)(encoded_divisor >> 8);
1430 *index |= ftdi->
index;
1433 *index = (
unsigned short)(encoded_divisor >> 16);
1444 unsigned short *value,
unsigned short *index)
1446 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1462 unsigned short value, index;
1463 int actual_baudrate;
1465 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1470 baudrate = baudrate*4;
1473 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1474 if (actual_baudrate <= 0)
1478 if ((actual_baudrate * 2 < baudrate )
1479 || ((actual_baudrate < baudrate)
1480 ? (actual_baudrate * 21 < baudrate * 20)
1481 : (baudrate * 21 < actual_baudrate * 20)))
1482 ftdi_error_return (-1,
"Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1529 unsigned short value = bits;
1531 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1537 value |= (0x00 << 8);
1540 value |= (0x01 << 8);
1543 value |= (0x02 << 8);
1546 value |= (0x03 << 8);
1549 value |= (0x04 << 8);
1556 value |= (0x00 << 11);
1559 value |= (0x01 << 11);
1562 value |= (0x02 << 11);
1569 value |= (0x00 << 14);
1572 value |= (0x01 << 14);
1600 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1603 while (offset < size)
1607 if (offset+write_size > size)
1608 write_size = size-offset;
1613 offset += actual_length;
1619static void LIBUSB_CALL ftdi_read_data_cb(
struct libusb_transfer *transfer)
1623 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1627 actual_length = transfer->actual_length;
1629 if (actual_length > 2)
1633 num_of_chunks = actual_length / packet_size;
1634 chunk_remains = actual_length % packet_size;
1640 if (actual_length > packet_size - 2)
1642 for (i = 1; i < num_of_chunks; i++)
1646 if (chunk_remains > 2)
1651 actual_length -= 2*num_of_chunks;
1654 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1657 if (actual_length > 0)
1660 if (tc->offset + actual_length <= tc->size)
1664 tc->offset += actual_length;
1670 if (tc->offset == tc->size)
1681 int part_size = tc->size - tc->offset;
1683 tc->offset += part_size;
1696 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1697 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1700 ret = libusb_submit_transfer (transfer);
1707static void LIBUSB_CALL ftdi_write_data_cb(
struct libusb_transfer *transfer)
1712 tc->offset += transfer->actual_length;
1714 if (tc->offset == tc->size)
1723 if (tc->offset + write_size > tc->size)
1724 write_size = tc->size - tc->offset;
1726 transfer->length = write_size;
1727 transfer->buffer = tc->buf + tc->offset;
1729 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1730 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1733 ret = libusb_submit_transfer (transfer);
1758 struct libusb_transfer *transfer;
1759 int write_size, ret;
1761 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1768 transfer = libusb_alloc_transfer(0);
1787 write_size, ftdi_write_data_cb, tc,
1789 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1791 ret = libusb_submit_transfer(
transfer);
1820 struct libusb_transfer *transfer;
1823 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1860 transfer = libusb_alloc_transfer(0);
1871 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1873 ret = libusb_submit_transfer(
transfer);
1899 struct timeval to = { 0, 0 };
1902 ret = libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1906 if (ret == LIBUSB_ERROR_INTERRUPTED)
1908 libusb_cancel_transfer(tc->
transfer);
1910 if (libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1913 libusb_free_transfer(tc->
transfer);
1926 if (tc->
transfer->status != LIBUSB_TRANSFER_COMPLETED)
1928 libusb_free_transfer(tc->
transfer);
1944 struct timeval * to)
1946 struct timeval tv = { 0, 0 };
1953 libusb_cancel_transfer(tc->
transfer);
1962 libusb_free_transfer(tc->
transfer);
2021 int offset = 0, ret, i, num_of_chunks, chunk_remains;
2023 int actual_length = 1;
2025 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2030 if (packet_size == 0)
2055 while (offset < size && actual_length > 0)
2064 if (actual_length > 2)
2068 num_of_chunks = actual_length / packet_size;
2069 chunk_remains = actual_length % packet_size;
2075 if (actual_length > packet_size - 2)
2077 for (i = 1; i < num_of_chunks; i++)
2081 if (chunk_remains > 2)
2086 actual_length -= 2*num_of_chunks;
2089 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
2092 else if (actual_length <= 2)
2097 if (actual_length > 0)
2100 if (offset+actual_length <= size)
2104 offset += actual_length;
2115 int part_size = size-offset;
2120 offset += part_size;
2147 unsigned char *new_buf;
2160 if (chunksize > 16384)
2164 if ((new_buf = (
unsigned char *)realloc(ftdi->
readbuffer, chunksize)) == NULL)
2205 unsigned short usb_val;
2207 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2211 usb_val |= (mode << 8);
2213 ftdi_error_return(-1,
"unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2249 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2253 ftdi_error_return(-1,
"unable to leave bitbang mode. Perhaps not a BM type chip?");
2272 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2298 unsigned short usb_val;
2303 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2325 unsigned short usb_val;
2327 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2333 *latency = (
unsigned char)usb_val;
2381 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2387 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2407 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2431 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2434 uint16_t xonxoff = xon | (xoff << 8);
2455 unsigned short usb_val;
2457 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2485 unsigned short usb_val;
2487 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2516 unsigned short usb_val;
2518 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2551 unsigned char eventch,
unsigned char enable)
2553 unsigned short usb_val;
2555 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2580 unsigned char errorch,
unsigned char enable)
2582 unsigned short usb_val;
2584 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2610 const char * product,
const char * serial)
2617 if (ftdi->
eeprom == NULL)
2667 const char* default_product;
2670 case TYPE_AM: default_product =
"AM";
break;
2671 case TYPE_BM: default_product =
"BM";
break;
2672 case TYPE_2232C: default_product =
"Dual RS232";
break;
2673 case TYPE_R: default_product =
"FT232R USB UART";
break;
2674 case TYPE_2232H: default_product =
"Dual RS232-HS";
break;
2675 case TYPE_4232H: default_product =
"FT4232H";
break;
2676 case TYPE_232H: default_product =
"Single-RS232-HS";
break;
2677 case TYPE_230X: default_product =
"FT230X Basic UART";
break;
2681 eeprom->
product = (
char *)malloc(strlen(default_product) +1);
2683 strcpy(eeprom->
product, default_product);
2699 eeprom->
size = 0x80;
2709 eeprom->
size = 0x100;
2720 for (i=0; i<10; i++)
2765 if (ftdi->
eeprom == NULL)
2826 char *
serial,
int serial_len)
2832 if (ftdi->
eeprom == NULL)
2855 serial[serial_len - 1] =
'\0';
2867 int mode_low, mode_high;
2877 output[0x18+i] = (mode_high <<4) | mode_low;
2946 unsigned char i, j, eeprom_size_mask;
2947 unsigned short checksum, value;
2948 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2949 int user_area_size, free_start, free_end;
2951 unsigned char * output;
2955 if (ftdi->
eeprom == NULL)
2959 output = eeprom->
buf;
2961 if (eeprom->
chip == -1)
2964 if (eeprom->
size == -1)
2966 if ((eeprom->
chip == 0x56) || (eeprom->
chip == 0x66))
2967 eeprom->
size = 0x100;
2969 eeprom->
size = 0x80;
2975 product_size = strlen(eeprom->
product);
2976 if (eeprom->
serial != NULL)
2977 serial_size = strlen(eeprom->
serial);
2985 user_area_size = 96;
2988 user_area_size = 90;
2991 user_area_size = 88;
2995 user_area_size = 86;
2998 user_area_size = 80;
3004 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
3006 if (user_area_size < 0)
3098 eeprom_size_mask = eeprom->
size -1;
3099 free_end = i & eeprom_size_mask;
3105 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
3106 output[i & eeprom_size_mask] = 0x03, i++;
3107 for (j = 0; j < manufacturer_size; j++)
3109 output[i & eeprom_size_mask] = eeprom->
manufacturer[j], i++;
3110 output[i & eeprom_size_mask] = 0x00, i++;
3112 output[0x0F] = manufacturer_size*2 + 2;
3116 output[0x10] = i | 0x80;
3117 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
3118 output[i & eeprom_size_mask] = 0x03, i++;
3119 for (j = 0; j < product_size; j++)
3121 output[i & eeprom_size_mask] = eeprom->
product[j], i++;
3122 output[i & eeprom_size_mask] = 0x00, i++;
3124 output[0x11] = product_size*2 + 2;
3129 output[0x12] = i | 0x80;
3130 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
3131 output[i & eeprom_size_mask] = 0x03, i++;
3132 for (j = 0; j < serial_size; j++)
3134 output[i & eeprom_size_mask] = eeprom->
serial[j], i++;
3135 output[i & eeprom_size_mask] = 0x00, i++;
3137 output[0x13] = serial_size*2 + 2;
3144 output[i & eeprom_size_mask] = 0x02;
3146 output[i & eeprom_size_mask] = 0x03;
3148 output[i & eeprom_size_mask] = eeprom->
is_not_pnp;
3150 output[i & eeprom_size_mask] = 0x00;
3202 output[0x0A] |= 0x1;
3204 output[0x0A] &= ~0x1;
3206 output[0x0A] |= 0x2;
3208 output[0x0A] &= ~0x2;
3210 output[0x0A] |= 0x4;
3212 output[0x0A] &= ~0x4;
3220 output[0x14] = eeprom->
chip;
3234 output[0x00] |= 0x02;
3235 output[0x01] = 0x40;
3238 output[0x0A] |= 0x4;
3240 output[0x0A] &= ~0x4;
3241 output[0x0B] = eeprom->
invert;
3289 output[0x0A] |= 0x4;
3291 output[0x0A] &= ~0x4;
3329 output[0x18] = eeprom->
chip;
3351 output[0x0a] |= 0x4;
3353 output[0x0a] &= ~0x4;
3408 output[0x18] = eeprom->
chip;
3424 output[0x0a] |= 0x4;
3426 output[0x0a] &= ~0x4;
3461 output[0x1e] = eeprom->
chip;
3465 output[0x00] = 0x80;
3468 for (j = 0; j <= 6; j++)
3472 output[0x0b] = eeprom->
invert;
3505 fprintf(stderr,
"Warning, user data starts inside the generated data!\n");
3507 fprintf(stderr,
"Warning, user data overlaps the strings area!\n");
3516 for (i = 0; i < eeprom->
size/2-1; i++)
3523 if ((ftdi->
type ==
TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3526 fprintf(stderr,
"Reading Factory Configuration Data failed\n");
3530 output[i * 2] = data;
3531 output[(i * 2) + 1] = data >> 8;
3534 value = output[i*2];
3535 value += output[(i*2)+1] << 8;
3537 checksum = value^checksum;
3538 checksum = (checksum << 1) | (checksum >> 15);
3541 output[eeprom->
size-2] = checksum;
3542 output[eeprom->
size-1] = checksum >> 8;
3545 return user_area_size;
3552static unsigned char bit2type(
unsigned char bits)
3562 fprintf(stderr,
" Unexpected value %d for Hardware Interface type\n",
3570static void print_inverted_bits(
int invert)
3572 const char *r_bits[] = {
"TXD",
"RXD",
"RTS",
"CTS",
"DTR",
"DSR",
"DCD",
"RI"};
3575 fprintf(stdout,
"Inverted bits:");
3577 if ((
invert & (1<<i)) == (1<<i))
3578 fprintf(stdout,
" %s",r_bits[i]);
3580 fprintf(stdout,
"\n");
3599 unsigned short checksum, eeprom_checksum, value;
3600 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3603 unsigned char *
buf = NULL;
3607 if (ftdi->
eeprom == NULL)
3611 eeprom_size = eeprom->
size;
3655 manufacturer_size =
buf[0x0F]/2;
3658 if (manufacturer_size > 0)
3660 eeprom->
manufacturer = (
char *)malloc(manufacturer_size);
3664 i =
buf[0x0E] & (eeprom_size -1);
3665 for (j=0; j<manufacturer_size-1; j++)
3678 product_size =
buf[0x11]/2;
3679 if (product_size > 0)
3681 eeprom->
product = (
char *)malloc(product_size);
3685 i =
buf[0x10] & (eeprom_size -1);
3686 for (j=0; j<product_size-1; j++)
3699 serial_size =
buf[0x13]/2;
3700 if (serial_size > 0)
3702 eeprom->
serial = (
char *)malloc(serial_size);
3706 i =
buf[0x12] & (eeprom_size -1);
3707 for (j=0; j<serial_size-1; j++)
3711 eeprom->
serial[j] =
'\0';
3714 else eeprom->
serial = NULL;
3719 for (i = 0; i < eeprom_size/2-1; i++)
3727 value +=
buf[(i*2)+1] << 8;
3729 checksum = value^checksum;
3730 checksum = (checksum << 1) | (checksum >> 15);
3733 eeprom_checksum =
buf[eeprom_size-2] + (
buf[eeprom_size-1] << 8);
3735 if (eeprom_checksum != checksum)
3737 fprintf(stderr,
"Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3762 if ( (
buf[0x01]&0x40) != 0x40)
3764 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3765 " If this happened with the\n"
3766 " EEPROM programmed by FTDI tools, please report "
3767 "to libftdi@developer.intra2net.com\n");
3858 const char *channel_mode[] = {
"UART",
"FIFO",
"CPU",
"OPTO",
"FT1284"};
3859 fprintf(stdout,
"VID: 0x%04x\n",eeprom->
vendor_id);
3860 fprintf(stdout,
"PID: 0x%04x\n",eeprom->
product_id);
3864 fprintf(stdout,
"Self-Powered%s", (eeprom->
remote_wakeup)?
", USB Remote Wake Up\n":
"\n");
3866 fprintf(stdout,
"Bus Powered: %3d mA%s", eeprom->
max_power,
3869 fprintf(stdout,
"Manufacturer: %s\n",eeprom->
manufacturer);
3871 fprintf(stdout,
"Product: %s\n",eeprom->
product);
3873 fprintf(stdout,
"Serial: %s\n",eeprom->
serial);
3874 fprintf(stdout,
"Checksum : %04x\n", checksum);
3876 fprintf(stdout,
"Internal EEPROM\n");
3879 else if (eeprom->
chip >= 0x46)
3880 fprintf(stdout,
"Attached EEPROM: 93x%02x\n", eeprom->
chip);
3882 fprintf(stdout,
"Suspend on DBUS7\n");
3884 fprintf(stdout,
"Pull IO pins low during suspend\n");
3888 fprintf(stdout,
"Enter low power state on ACBUS7\n");
3891 fprintf(stdout,
"Enable Remote Wake Up\n");
3892 fprintf(stdout,
"PNP: %d\n",(eeprom->
is_not_pnp)?0:1);
3894 fprintf(stdout,
"Channel A has Mode %s%s%s\n",
3900 fprintf(stdout,
"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3906 fprintf(stdout,
"Channel B has Mode %s%s%s\n",
3912 fprintf(stdout,
"Channel C has Mode UART%s\n",
3914 fprintf(stdout,
"Channel D has Mode UART%s\n",
3919 fprintf(stdout,
"Use explicit USB Version %04x\n",eeprom->
usb_version);
3923 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3928 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3933 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3938 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3946 const char *cbush_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3947 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3948 "CLK30",
"CLK15",
"CLK7_5"
3950 fprintf(stdout,
"ACBUS has %d mA drive%s%s\n",
3954 fprintf(stdout,
"ADBUS has %d mA drive%s%s\n",
3958 for (i=0; i<10; i++)
3961 fprintf(stdout,
"C%d Function: %s\n", i,
3967 const char *cbusx_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3968 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3969 "CLK24",
"CLK12",
"CLK6",
"BAT_DETECT",
"BAT_DETECT#",
3970 "I2C_TXE#",
"I2C_RXF#",
"VBUS_SENSE",
"BB_WR#",
3971 "BBRD#",
"TIME_STAMP",
"AWAKE#",
3973 fprintf(stdout,
"DBUS has %d mA drive%s%s\n",
3977 fprintf(stdout,
"CBUS has %d mA drive%s%s\n",
3984 fprintf(stdout,
"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->
cbus_function[i]]);
3988 print_inverted_bits(eeprom->
invert);
3993 const char *cbus_mux[] = {
"TXDEN",
"PWREN",
"RXLED",
"TXLED",
"TX+RXLED",
3994 "SLEEP",
"CLK48",
"CLK24",
"CLK12",
"CLK6",
3995 "IOMODE",
"BB_WR",
"BB_RD"
3997 const char *cbus_BB[] = {
"RXF",
"TXE",
"RD",
"WR"};
4000 print_inverted_bits(eeprom->
invert);
4005 fprintf(stdout,
"C%d Function: %s\n", i,
4012 fprintf(stdout,
"C%d BB Function: %s\n", i,
4015 fprintf(stdout,
"Unknown CBUS mode. Might be special mode?\n");
4427 if (!ftdi || !(ftdi->
eeprom))
4497 unsigned char buf[2];
4499 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4505 *eeprom_val = (0xff &
buf[0]) | (
buf[1] << 8);
4524 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4530 if (libusb_control_transfer(
4542 else if (memcmp(
buf,&
buf[0x80],0x80) == 0)
4544 else if (memcmp(
buf,&
buf[0x40],0x40) == 0)
4556static unsigned char ftdi_read_chipid_shift(
unsigned char value)
4558 return ((value & 1) << 1) |
4559 ((value & 2) << 5) |
4560 ((value & 4) >> 2) |
4561 ((value & 8) << 4) |
4562 ((value & 16) >> 1) |
4563 ((value & 32) >> 1) |
4564 ((value & 64) >> 4) |
4565 ((value & 128) >> 2);
4580 unsigned int a = 0, b = 0;
4582 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4587 a = a << 8 | a >> 8;
4590 b = b << 8 | b >> 8;
4591 a = (a << 16) | (b & 0xFFFF);
4592 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4593 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4594 *chipid = a ^ 0xa5f0f7d1;
4617 unsigned short eeprom_val)
4619 int chip_type_location;
4620 unsigned short chip_type;
4622 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4625 if (eeprom_addr <0x80)
4633 chip_type_location = 0x14;
4637 chip_type_location = 0x18;
4640 chip_type_location = 0x1e;
4651 fprintf(stderr,
" loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4652 if ((chip_type & 0xff) != 0x66)
4677 unsigned short usb_val, status;
4679 unsigned char *eeprom;
4681 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4704 usb_val = eeprom[i*2];
4705 usb_val += eeprom[(i*2)+1] << 8;
4732 unsigned short eeprom_value;
4733 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4757 if (eeprom_value ==
MAGIC)
4765 if (eeprom_value ==
MAGIC)
4771 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)
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_eeprom_initdefaults(struct ftdi_context *ftdi, const char *manufacturer, const char *product, const char *serial)
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_module_detach_mode(struct ftdi_context *ftdi, enum ftdi_module_detach_mode mode)
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