41 #define ftdi_error_return(code, str) do { \
43 ftdi->error_str = str; \
45 fprintf(stderr, str); \
49 #define ftdi_error_return_free_device_list(code, str, devs) do { \
50 libusb_free_device_list(devs,1); \
51 ftdi->error_str = str; \
65 static void ftdi_usb_close_internal (
struct ftdi_context *ftdi)
108 if (libusb_init(&ftdi->
usb_ctx) < 0)
168 if (ftdi->
index != check_interface)
215 ftdi_usb_close_internal (ftdi);
314 libusb_device **devs;
318 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
324 while ((dev = devs[i++]) != NULL)
326 struct libusb_device_descriptor desc;
328 if (libusb_get_device_descriptor(dev, &desc) < 0)
331 if (((vendor || product) &&
332 desc.idVendor == vendor && desc.idProduct == product) ||
333 (!(vendor || product) &&
334 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
335 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
336 || desc.idProduct == 0x6015)))
342 (*curdev)->next = NULL;
343 (*curdev)->dev =
dev;
344 libusb_ref_device(dev);
345 curdev = &(*curdev)->
next;
349 libusb_free_device_list(devs,1);
362 for (curdev = *devlist; curdev != NULL;)
365 libusb_unref_device(curdev->
dev);
410 struct libusb_device *
dev,
411 char *manufacturer,
int mnf_len,
412 char *description,
int desc_len,
413 char *serial,
int serial_len)
417 if ((ftdi==NULL) || (dev==NULL))
420 if (ftdi->
usb_dev == NULL && libusb_open(dev, &ftdi->
usb_dev) < 0)
428 manufacturer, mnf_len,
429 description, desc_len,
435 ftdi_usb_close_internal(ftdi);
467 char *manufacturer,
int mnf_len,
468 char *description,
int desc_len,
469 char *serial,
int serial_len)
471 struct libusb_device_descriptor desc;
473 if ((ftdi==NULL) || (dev==NULL))
476 char need_open = (ftdi->
usb_dev == NULL);
477 if (need_open && libusb_open(dev, &ftdi->
usb_dev) < 0)
480 if (libusb_get_device_descriptor(dev, &desc) < 0)
483 if (manufacturer != NULL)
485 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iManufacturer, (
unsigned char *)manufacturer, mnf_len) < 0)
487 ftdi_usb_close_internal (ftdi);
492 if (description != NULL)
494 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)description, desc_len) < 0)
496 ftdi_usb_close_internal (ftdi);
503 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)serial, serial_len) < 0)
505 ftdi_usb_close_internal (ftdi);
511 ftdi_usb_close_internal (ftdi);
522 static unsigned int _ftdi_determine_max_packet_size(
struct ftdi_context *ftdi, libusb_device *dev)
524 struct libusb_device_descriptor desc;
525 struct libusb_config_descriptor *config0;
526 unsigned int packet_size;
529 if (ftdi == NULL || dev == NULL)
540 if (libusb_get_device_descriptor(dev, &desc) < 0)
543 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
546 if (desc.bNumConfigurations > 0)
548 if (ftdi->
interface < config0->bNumInterfaces)
550 struct libusb_interface interface = config0->interface[ftdi->
interface];
551 if (interface.num_altsetting > 0)
553 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
554 if (descriptor.bNumEndpoints > 0)
556 packet_size = descriptor.endpoint[0].wMaxPacketSize;
562 libusb_free_config_descriptor (config0);
586 struct libusb_device_descriptor desc;
587 struct libusb_config_descriptor *config0;
588 int cfg, cfg0, detach_errno = 0;
593 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
596 if (libusb_get_device_descriptor(dev, &desc) < 0)
599 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
601 cfg0 = config0->bConfigurationValue;
602 libusb_free_config_descriptor (config0);
613 detach_errno = errno;
616 if (libusb_get_configuration (ftdi->
usb_dev, &cfg) < 0)
621 if (desc.bNumConfigurations > 0 && cfg != cfg0)
623 if (libusb_set_configuration(ftdi->
usb_dev, cfg0) < 0)
625 ftdi_usb_close_internal (ftdi);
626 if (detach_errno == EPERM)
632 ftdi_error_return(-3,
"unable to set usb configuration. Make sure the default FTDI driver is not in use");
639 ftdi_usb_close_internal (ftdi);
640 if (detach_errno == EPERM)
646 ftdi_error_return(-5,
"unable to claim usb device. Make sure the default FTDI driver is not in use");
652 ftdi_usb_close_internal (ftdi);
658 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
659 && desc.iSerialNumber == 0))
661 else if (desc.bcdDevice == 0x200)
663 else if (desc.bcdDevice == 0x500)
665 else if (desc.bcdDevice == 0x600)
667 else if (desc.bcdDevice == 0x700)
669 else if (desc.bcdDevice == 0x800)
671 else if (desc.bcdDevice == 0x900)
673 else if (desc.bcdDevice == 0x1000)
681 ftdi_usb_close_internal (ftdi);
724 const char* description,
const char* serial)
754 const char* description,
const char* serial,
unsigned int index)
757 libusb_device **devs;
764 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
767 while ((dev = devs[i++]) != NULL)
769 struct libusb_device_descriptor desc;
772 if (libusb_get_device_descriptor(dev, &desc) < 0)
775 if (desc.idVendor == vendor && desc.idProduct == product)
777 if (libusb_open(dev, &ftdi->
usb_dev) < 0)
780 if (description != NULL)
782 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iProduct, (
unsigned char *)
string,
sizeof(
string)) < 0)
784 ftdi_usb_close_internal (ftdi);
787 if (strncmp(
string, description,
sizeof(
string)) != 0)
789 ftdi_usb_close_internal (ftdi);
795 if (libusb_get_string_descriptor_ascii(ftdi->
usb_dev, desc.iSerialNumber, (
unsigned char *)
string,
sizeof(
string)) < 0)
797 ftdi_usb_close_internal (ftdi);
800 if (strncmp(
string, serial,
sizeof(
string)) != 0)
802 ftdi_usb_close_internal (ftdi);
807 ftdi_usb_close_internal (ftdi);
816 libusb_free_device_list(devs,1);
856 if (description[0] == 0 || description[1] !=
':')
859 if (description[0] ==
'd')
862 libusb_device **devs;
863 unsigned int bus_number, device_address;
866 if (libusb_get_device_list(ftdi->
usb_ctx, &devs) < 0)
870 if (sscanf (description + 2,
"%u/%u", &bus_number, &device_address) != 2)
873 while ((dev = devs[i++]) != NULL)
876 if (bus_number == libusb_get_bus_number (dev)
877 && device_address == libusb_get_device_address (dev))
880 libusb_free_device_list(devs,1);
888 else if (description[0] ==
'i' || description[0] ==
's')
891 unsigned int product;
892 unsigned int index=0;
893 const char *serial=NULL;
894 const char *startp, *endp;
897 startp=description+2;
898 vendor=strtoul((
char*)startp,(
char**)&endp,0);
899 if (*endp !=
':' || endp == startp || errno != 0)
903 product=strtoul((
char*)startp,(
char**)&endp,0);
904 if (endp == startp || errno != 0)
907 if (description[0] ==
'i' && *endp != 0)
914 index=strtoul((
char*)startp,(
char**)&endp,0);
915 if (*endp != 0 || endp == startp || errno != 0)
918 if (description[0] ==
's')
946 if (ftdi == NULL || ftdi->
usb_dev == NULL)
972 if (ftdi == NULL || ftdi->
usb_dev == NULL)
998 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1023 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1059 ftdi_usb_close_internal (ftdi);
1076 static int ftdi_to_clkbits_AM(
int baudrate,
unsigned long *encoded_divisor)
1079 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1080 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1081 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1082 int divisor, best_divisor, best_baud, best_baud_diff;
1084 divisor = 24000000 / baudrate;
1087 divisor -= am_adjust_dn[divisor & 7];
1093 for (i = 0; i < 2; i++)
1095 int try_divisor = divisor + i;
1100 if (try_divisor <= 8)
1105 else if (divisor < 16)
1113 try_divisor += am_adjust_up[try_divisor & 7];
1114 if (try_divisor > 0x1FFF8)
1117 try_divisor = 0x1FFF8;
1121 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1123 if (baud_estimate < baudrate)
1125 baud_diff = baudrate - baud_estimate;
1129 baud_diff = baud_estimate - baudrate;
1131 if (i == 0 || baud_diff < best_baud_diff)
1134 best_divisor = try_divisor;
1135 best_baud = baud_estimate;
1136 best_baud_diff = baud_diff;
1145 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1147 if (*encoded_divisor == 1)
1149 *encoded_divisor = 0;
1151 else if (*encoded_divisor == 0x4001)
1153 *encoded_divisor = 1;
1182 static int ftdi_to_clkbits(
int baudrate,
unsigned int clk,
int clk_div,
unsigned long *encoded_divisor)
1184 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1186 int divisor, best_divisor;
1187 if (baudrate >= clk/clk_div)
1189 *encoded_divisor = 0;
1190 best_baud = clk/clk_div;
1192 else if (baudrate >= clk/(clk_div + clk_div/2))
1194 *encoded_divisor = 1;
1195 best_baud = clk/(clk_div + clk_div/2);
1197 else if (baudrate >= clk/(2*clk_div))
1199 *encoded_divisor = 2;
1200 best_baud = clk/(2*clk_div);
1205 divisor = clk*16/clk_div / baudrate;
1207 best_divisor = divisor /2 +1;
1209 best_divisor = divisor/2;
1210 if(best_divisor > 0x20000)
1211 best_divisor = 0x1ffff;
1212 best_baud = clk*16/clk_div/best_divisor;
1214 best_baud = best_baud /2 +1;
1216 best_baud = best_baud /2;
1217 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1226 static int ftdi_convert_baudrate(
int baudrate,
struct ftdi_context *ftdi,
1227 unsigned short *value,
unsigned short *index)
1230 unsigned long encoded_divisor;
1238 #define H_CLK 120000000
1239 #define C_CLK 48000000
1242 if(baudrate*10 >
H_CLK /0x3fff)
1249 best_baud = ftdi_to_clkbits(baudrate,
H_CLK, 10, &encoded_divisor);
1250 encoded_divisor |= 0x20000;
1253 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1257 best_baud = ftdi_to_clkbits(baudrate,
C_CLK, 16, &encoded_divisor);
1261 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1264 *value = (
unsigned short)(encoded_divisor & 0xFFFF);
1267 *index = (
unsigned short)(encoded_divisor >> 8);
1269 *index |= ftdi->
index;
1272 *index = (
unsigned short)(encoded_divisor >> 16);
1283 unsigned short *value,
unsigned short *index)
1285 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1301 unsigned short value, index;
1302 int actual_baudrate;
1304 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1309 baudrate = baudrate*4;
1312 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1313 if (actual_baudrate <= 0)
1317 if ((actual_baudrate * 2 < baudrate )
1318 || ((actual_baudrate < baudrate)
1319 ? (actual_baudrate * 21 < baudrate * 20)
1320 : (baudrate * 21 < actual_baudrate * 20)))
1321 ftdi_error_return (-1,
"Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1368 unsigned short value = bits;
1370 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1376 value |= (0x00 << 8);
1379 value |= (0x01 << 8);
1382 value |= (0x02 << 8);
1385 value |= (0x03 << 8);
1388 value |= (0x04 << 8);
1395 value |= (0x00 << 11);
1398 value |= (0x01 << 11);
1401 value |= (0x02 << 11);
1408 value |= (0x00 << 14);
1411 value |= (0x01 << 14);
1439 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1442 while (offset < size)
1446 if (offset+write_size > size)
1447 write_size = size-offset;
1452 offset += actual_length;
1458 static void LIBUSB_CALL ftdi_read_data_cb(
struct libusb_transfer *transfer)
1462 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1466 actual_length = transfer->actual_length;
1468 if (actual_length > 2)
1472 num_of_chunks = actual_length / packet_size;
1473 chunk_remains = actual_length % packet_size;
1479 if (actual_length > packet_size - 2)
1481 for (i = 1; i < num_of_chunks; i++)
1485 if (chunk_remains > 2)
1490 actual_length -= 2*num_of_chunks;
1493 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1496 if (actual_length > 0)
1503 tc->
offset += actual_length;
1535 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1536 tc->
completed = LIBUSB_TRANSFER_CANCELLED;
1539 ret = libusb_submit_transfer (transfer);
1546 static void LIBUSB_CALL ftdi_write_data_cb(
struct libusb_transfer *transfer)
1551 tc->
offset += transfer->actual_length;
1565 transfer->length = write_size;
1566 transfer->buffer = tc->
buf + tc->
offset;
1568 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1569 tc->
completed = LIBUSB_TRANSFER_CANCELLED;
1572 ret = libusb_submit_transfer (transfer);
1597 struct libusb_transfer *transfer;
1598 int write_size, ret;
1600 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1607 transfer = libusb_alloc_transfer(0);
1625 libusb_fill_bulk_transfer(transfer, ftdi->
usb_dev, ftdi->
in_ep, buf,
1626 write_size, ftdi_write_data_cb, tc,
1628 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1630 ret = libusb_submit_transfer(transfer);
1633 libusb_free_transfer(transfer);
1659 struct libusb_transfer *transfer;
1662 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1699 transfer = libusb_alloc_transfer(0);
1710 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1712 ret = libusb_submit_transfer(transfer);
1715 libusb_free_transfer(transfer);
1738 struct timeval to = { 0, 0 };
1741 ret = libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1745 if (ret == LIBUSB_ERROR_INTERRUPTED)
1747 libusb_cancel_transfer(tc->
transfer);
1749 if (libusb_handle_events_timeout_completed(tc->
ftdi->
usb_ctx,
1752 libusb_free_transfer(tc->
transfer);
1765 if (tc->
transfer->status != LIBUSB_TRANSFER_COMPLETED)
1767 libusb_free_transfer(tc->
transfer);
1783 struct timeval * to)
1785 struct timeval tv = { 0, 0 };
1792 libusb_cancel_transfer(tc->
transfer);
1801 libusb_free_transfer(tc->
transfer);
1860 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1862 int actual_length = 1;
1864 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1868 if (packet_size == 0)
1893 while (offset < size && actual_length > 0)
1902 if (actual_length > 2)
1906 num_of_chunks = actual_length / packet_size;
1907 chunk_remains = actual_length % packet_size;
1913 if (actual_length > packet_size - 2)
1915 for (i = 1; i < num_of_chunks; i++)
1919 if (chunk_remains > 2)
1924 actual_length -= 2*num_of_chunks;
1927 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1930 else if (actual_length <= 2)
1935 if (actual_length > 0)
1938 if (offset+actual_length <= size)
1942 offset += actual_length;
1953 int part_size = size-offset;
1958 offset += part_size;
1985 unsigned char *new_buf;
1998 if (chunksize > 16384)
2002 if ((new_buf = (
unsigned char *)realloc(ftdi->
readbuffer, chunksize)) == NULL)
2043 unsigned short usb_val;
2045 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2049 usb_val |= (mode << 8);
2051 ftdi_error_return(-1,
"unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2069 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2073 ftdi_error_return(-1,
"unable to leave bitbang mode. Perhaps not a BM type chip?");
2092 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2118 unsigned short usb_val;
2123 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2145 unsigned short usb_val;
2147 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2153 *latency = (
unsigned char)usb_val;
2201 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2207 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2225 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2248 unsigned short usb_val;
2250 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2278 unsigned short usb_val;
2280 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2309 unsigned short usb_val;
2311 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2344 unsigned char eventch,
unsigned char enable)
2346 unsigned short usb_val;
2348 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2373 unsigned char errorch,
unsigned char enable)
2375 unsigned short usb_val;
2377 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2403 char * product,
char * serial)
2410 if (ftdi->
eeprom == NULL)
2454 eeprom->
product = malloc(strlen(product)+1);
2456 strcpy(eeprom->
product, product);
2460 const char* default_product;
2463 case TYPE_AM: default_product =
"AM";
break;
2464 case TYPE_BM: default_product =
"BM";
break;
2465 case TYPE_2232C: default_product =
"Dual RS232";
break;
2466 case TYPE_R: default_product =
"FT232R USB UART";
break;
2467 case TYPE_2232H: default_product =
"Dual RS232-HS";
break;
2468 case TYPE_4232H: default_product =
"FT4232H";
break;
2469 case TYPE_232H: default_product =
"Single-RS232-HS";
break;
2470 case TYPE_230X: default_product =
"FT230X Basic UART";
break;
2474 eeprom->
product = malloc(strlen(default_product) +1);
2476 strcpy(eeprom->
product, default_product);
2484 eeprom->
serial = malloc(strlen(serial)+1);
2486 strcpy(eeprom->
serial, serial);
2492 eeprom->
size = 0x80;
2502 eeprom->
size = 0x100;
2513 for (i=0; i<10; i++)
2558 if (ftdi->
eeprom == NULL)
2579 eeprom->
product = malloc(strlen(product)+1);
2581 strcpy(eeprom->
product, product);
2588 eeprom->
serial = malloc(strlen(serial)+1);
2591 strcpy(eeprom->
serial, serial);
2605 int mode_low, mode_high;
2615 output[0x18+i] = (mode_high <<4) | mode_low;
2671 unsigned char i, j, eeprom_size_mask;
2672 unsigned short checksum, value;
2673 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2674 int user_area_size, free_start, free_end;
2676 unsigned char * output;
2680 if (ftdi->
eeprom == NULL)
2684 output = eeprom->
buf;
2686 if (eeprom->
chip == -1)
2689 if (eeprom->
size == -1)
2691 if ((eeprom->
chip == 0x56) || (eeprom->
chip == 0x66))
2692 eeprom->
size = 0x100;
2694 eeprom->
size = 0x80;
2700 product_size = strlen(eeprom->
product);
2701 if (eeprom->
serial != NULL)
2702 serial_size = strlen(eeprom->
serial);
2710 user_area_size = 96;
2713 user_area_size = 90;
2716 user_area_size = 88;
2720 user_area_size = 86;
2723 user_area_size = 80;
2729 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2731 if (user_area_size < 0)
2820 eeprom_size_mask = eeprom->
size -1;
2821 free_end = i & eeprom_size_mask;
2827 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2828 output[i & eeprom_size_mask] = 0x03, i++;
2829 for (j = 0; j < manufacturer_size; j++)
2831 output[i & eeprom_size_mask] = eeprom->
manufacturer[j], i++;
2832 output[i & eeprom_size_mask] = 0x00, i++;
2834 output[0x0F] = manufacturer_size*2 + 2;
2838 output[0x10] = i | 0x80;
2839 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2840 output[i & eeprom_size_mask] = 0x03, i++;
2841 for (j = 0; j < product_size; j++)
2843 output[i & eeprom_size_mask] = eeprom->
product[j], i++;
2844 output[i & eeprom_size_mask] = 0x00, i++;
2846 output[0x11] = product_size*2 + 2;
2850 output[0x12] = i | 0x80;
2851 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2852 output[i & eeprom_size_mask] = 0x03, i++;
2853 for (j = 0; j < serial_size; j++)
2855 output[i & eeprom_size_mask] = eeprom->
serial[j], i++;
2856 output[i & eeprom_size_mask] = 0x00, i++;
2862 output[i & eeprom_size_mask] = 0x02;
2864 output[i & eeprom_size_mask] = 0x03;
2866 output[i & eeprom_size_mask] = eeprom->
is_not_pnp;
2870 output[0x13] = serial_size*2 + 2;
2920 output[0x0A] |= 0x1;
2922 output[0x0A] &= ~0x1;
2924 output[0x0A] |= 0x2;
2926 output[0x0A] &= ~0x2;
2928 output[0x0A] |= 0x4;
2930 output[0x0A] &= ~0x4;
2938 output[0x14] = eeprom->
chip;
2944 output[0x00] |= 0x02;
2945 output[0x01] = 0x40;
2948 output[0x0A] |= 0x4;
2950 output[0x0A] &= ~0x4;
2951 output[0x0B] = eeprom->
invert;
2998 output[0x0A] |= 0x4;
3000 output[0x0A] &= ~0x4;
3038 output[0x18] = eeprom->
chip;
3060 output[0x0a] |= 0x4;
3062 output[0x0a] &= ~0x4;
3117 output[0x18] = eeprom->
chip;
3132 output[0x0a] |= 0x4;
3134 output[0x0a] &= ~0x4;
3168 output[0x1e] = eeprom->
chip;
3169 fprintf(stderr,
"FIXME: Build FT232H specific EEPROM settings\n");
3172 output[0x00] = 0x80;
3173 output[0x0a] = 0x08;
3176 for (j = 0; j <= 6; j++)
3180 output[0x0b] = eeprom->
invert;
3208 fprintf(stderr,
"Warning, user data starts inside the generated data!\n");
3210 fprintf(stderr,
"Warning, user data overlaps the strings area!\n");
3219 for (i = 0; i < eeprom->
size/2-1; i++)
3226 if ((ftdi->
type ==
TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3229 fprintf(stderr,
"Reading Factory Configuration Data failed\n");
3235 value = output[i*2];
3236 value += output[(i*2)+1] << 8;
3238 checksum = value^checksum;
3239 checksum = (checksum << 1) | (checksum >> 15);
3242 output[eeprom->
size-2] = checksum;
3243 output[eeprom->
size-1] = checksum >> 8;
3246 return user_area_size;
3253 static unsigned char bit2type(
unsigned char bits)
3263 fprintf(stderr,
" Unexpected value %d for Hardware Interface type\n",
3271 static void print_inverted_bits(
int invert)
3273 char *r_bits[] = {
"TXD",
"RXD",
"RTS",
"CTS",
"DTR",
"DSR",
"DCD",
"RI"};
3276 fprintf(stdout,
"Inverted bits:");
3278 if ((invert & (1<<i)) == (1<<i))
3279 fprintf(stdout,
" %s",r_bits[i]);
3281 fprintf(stdout,
"\n");
3300 unsigned short checksum, eeprom_checksum, value;
3301 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3304 unsigned char *
buf = NULL;
3308 if (ftdi->
eeprom == NULL)
3312 eeprom_size = eeprom->
size;
3316 eeprom->
vendor_id = buf[0x02] + (buf[0x03] << 8);
3319 eeprom->
product_id = buf[0x04] + (buf[0x05] << 8);
3352 eeprom->
usb_version = buf[0x0C] + (buf[0x0D] << 8);
3356 manufacturer_size = buf[0x0F]/2;
3359 if (manufacturer_size > 0)
3365 i = buf[0x0E] & (eeprom_size -1);
3366 for (j=0; j<manufacturer_size-1; j++)
3379 product_size = buf[0x11]/2;
3380 if (product_size > 0)
3382 eeprom->
product = malloc(product_size);
3386 i = buf[0x10] & (eeprom_size -1);
3387 for (j=0; j<product_size-1; j++)
3389 eeprom->
product[j] = buf[2*j+i+2];
3400 serial_size = buf[0x13]/2;
3401 if (serial_size > 0)
3403 eeprom->
serial = malloc(serial_size);
3407 i = buf[0x12] & (eeprom_size -1);
3408 for (j=0; j<serial_size-1; j++)
3410 eeprom->
serial[j] = buf[2*j+i+2];
3412 eeprom->
serial[j] =
'\0';
3415 else eeprom->
serial = NULL;
3420 for (i = 0; i < eeprom_size/2-1; i++)
3428 value += buf[(i*2)+1] << 8;
3430 checksum = value^checksum;
3431 checksum = (checksum << 1) | (checksum >> 15);
3434 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3436 if (eeprom_checksum != checksum)
3438 fprintf(stderr,
"Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3455 eeprom->
chip = buf[0x14];
3463 if ( (buf[0x01]&0x40) != 0x40)
3465 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3466 " If this happened with the\n"
3467 " EEPROM programmed by FTDI tools, please report "
3468 "to libftdi@developer.intra2net.com\n");
3470 eeprom->
chip = buf[0x16];
3473 eeprom->
invert = buf[0x0B];
3504 eeprom->
chip = buf[0x18];
3510 eeprom->
group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3516 eeprom->
group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3538 eeprom->
chip = buf[0x1e];
3552 eeprom->
group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3554 eeprom->
invert = buf[0xb];
3559 char *channel_mode[] = {
"UART",
"FIFO",
"CPU",
"OPTO",
"FT1284"};
3560 fprintf(stdout,
"VID: 0x%04x\n",eeprom->
vendor_id);
3561 fprintf(stdout,
"PID: 0x%04x\n",eeprom->
product_id);
3565 fprintf(stdout,
"Self-Powered%s", (eeprom->
remote_wakeup)?
", USB Remote Wake Up\n":
"\n");
3567 fprintf(stdout,
"Bus Powered: %3d mA%s", eeprom->
max_power,
3570 fprintf(stdout,
"Manufacturer: %s\n",eeprom->
manufacturer);
3572 fprintf(stdout,
"Product: %s\n",eeprom->
product);
3574 fprintf(stdout,
"Serial: %s\n",eeprom->
serial);
3575 fprintf(stdout,
"Checksum : %04x\n", checksum);
3577 fprintf(stdout,
"Internal EEPROM\n");
3580 else if (eeprom->
chip >= 0x46)
3581 fprintf(stdout,
"Attached EEPROM: 93x%02x\n", eeprom->
chip);
3583 fprintf(stdout,
"Suspend on DBUS7\n");
3585 fprintf(stdout,
"Pull IO pins low during suspend\n");
3589 fprintf(stdout,
"Enter low power state on ACBUS7\n");
3592 fprintf(stdout,
"Enable Remote Wake Up\n");
3593 fprintf(stdout,
"PNP: %d\n",(eeprom->
is_not_pnp)?0:1);
3595 fprintf(stdout,
"Channel A has Mode %s%s%s\n",
3601 fprintf(stdout,
"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3607 fprintf(stdout,
"Channel B has Mode %s%s%s\n",
3613 fprintf(stdout,
"Use explicit USB Version %04x\n",eeprom->
usb_version);
3617 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3622 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3627 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3632 fprintf(stdout,
"%s has %d mA drive%s%s\n",
3640 char *cbush_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3641 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3642 "CLK30",
"CLK15",
"CLK7_5"
3644 fprintf(stdout,
"ACBUS has %d mA drive%s%s\n",
3648 fprintf(stdout,
"ADBUS has %d mA drive%s%s\n",
3652 for (i=0; i<10; i++)
3655 fprintf(stdout,
"C%d Function: %s\n", i,
3661 char *cbusx_mux[] = {
"TRISTATE",
"TXLED",
"RXLED",
"TXRXLED",
"PWREN",
3662 "SLEEP",
"DRIVE_0",
"DRIVE_1",
"IOMODE",
"TXDEN",
3663 "CLK24",
"CLK12",
"CLK6",
"BAT_DETECT",
"BAT_DETECT#",
3664 "I2C_TXE#",
"I2C_RXF#",
"VBUS_SENSE",
"BB_WR#",
3665 "BBRD#",
"TIME_STAMP",
"AWAKE#",
3667 fprintf(stdout,
"DBUS has %d mA drive%s%s\n",
3671 fprintf(stdout,
"CBUS has %d mA drive%s%s\n",
3678 fprintf(stdout,
"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->
cbus_function[i]]);
3682 print_inverted_bits(eeprom->
invert);
3687 char *cbus_mux[] = {
"TXDEN",
"PWREN",
"RXLED",
"TXLED",
"TX+RXLED",
3688 "SLEEP",
"CLK48",
"CLK24",
"CLK12",
"CLK6",
3689 "IOMODE",
"BB_WR",
"BB_RD"
3691 char *cbus_BB[] = {
"RXF",
"TXE",
"RD",
"WR"};
3694 print_inverted_bits(eeprom->
invert);
3699 fprintf(stdout,
"C%d Function: %s\n", i,
3706 fprintf(stdout,
"C%d BB Function: %s\n", i,
3709 fprintf(stdout,
"Unknown CBUS mode. Might be special mode?\n");
4118 if (!ftdi || !(ftdi->
eeprom))
4121 if (!buf || size < ftdi->eeprom->size)
4144 if (!ftdi || !(ftdi->
eeprom) || !buf)
4167 if (!ftdi || !(ftdi->
eeprom) || !buf)
4188 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4211 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4217 if (libusb_control_transfer(
4229 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4231 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4243 static unsigned char ftdi_read_chipid_shift(
unsigned char value)
4245 return ((value & 1) << 1) |
4246 ((value & 2) << 5) |
4247 ((value & 4) >> 2) |
4248 ((value & 8) << 4) |
4249 ((value & 16) >> 1) |
4250 ((value & 32) >> 1) |
4251 ((value & 64) >> 4) |
4252 ((value & 128) >> 2);
4267 unsigned int a = 0, b = 0;
4269 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4274 a = a << 8 | a >> 8;
4277 b = b << 8 | b >> 8;
4278 a = (a << 16) | (b & 0xFFFF);
4279 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4280 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4281 *chipid = a ^ 0xa5f0f7d1;
4304 unsigned short eeprom_val)
4306 int chip_type_location;
4307 unsigned short chip_type;
4309 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4312 if (eeprom_addr <0x80)
4320 chip_type_location = 0x14;
4324 chip_type_location = 0x18;
4327 chip_type_location = 0x1e;
4335 fprintf(stderr,
" loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4336 if ((chip_type & 0xff) != 0x66)
4361 unsigned short usb_val, status;
4363 unsigned char *eeprom;
4365 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4388 usb_val = eeprom[i*2];
4389 usb_val += eeprom[(i*2)+1] << 8;
4413 #define MAGIC 0x55aa
4416 unsigned short eeprom_value;
4417 if (ftdi == NULL || ftdi->
usb_dev == NULL)
4441 if (eeprom_value ==
MAGIC)
4449 if (eeprom_value ==
MAGIC)
4455 if (eeprom_value ==
MAGIC)
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...
#define CHANNEL_IS_FT1284
struct ftdi_transfer_control * ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
#define FTDI_DEVICE_OUT_REQTYPE
enum ftdi_module_detach_mode module_detach_mode
#define SIO_SET_LATENCY_TIMER_REQUEST
#define FT1284_CLK_IDLE_STATE
list of usb devices created by ftdi_usb_find_all()
struct ftdi_device_list * next
int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, struct timeval *to)
void ftdi_list_free2(struct ftdi_device_list *devlist)
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
#define SIO_SET_EVENT_CHAR_REQUEST
int ftdi_read_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
void ftdi_deinit(struct ftdi_context *ftdi)
#define HIGH_CURRENT_DRIVE
void ftdi_set_usbdev(struct ftdi_context *ftdi, libusb_device_handle *usb)
#define SIO_READ_PINS_REQUEST
#define SIO_RESET_REQUEST
int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial, unsigned int index)
#define SUSPEND_DBUS7_BIT
#define FTDI_MINOR_VERSION
void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char *output)
#define SIO_SET_BAUDRATE_REQUEST
struct libusb_transfer * transfer
int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
#define FTDI_MAJOR_VERSION
#define SIO_SET_DATA_REQUEST
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
#define SIO_WRITE_EEPROM_REQUEST
Main context structure for all libftdi functions.
int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable)
#define MAX_POWER_MILLIAMP_PER_UNIT
int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
void ftdi_list_free(struct ftdi_device_list **devlist)
#define FTDI_DEVICE_IN_REQTYPE
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 channel_b_rs485enable
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
struct ftdi_context * ftdi_new(void)
int ftdi_erase_eeprom(struct ftdi_context *ftdi)
int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char *buf, int size)
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
unsigned int readbuffer_remaining
int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable)
int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
struct libusb_device * dev
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
#define SIO_GET_LATENCY_TIMER_REQUEST
int initialized_for_connected_device
#define FTDI_MICRO_VERSION
#define FT1284_FLOW_CONTROL
int channel_c_rs485enable
int ftdi_init(struct ftdi_context *ftdi)
unsigned char * readbuffer
#define USE_USB_VERSION_BIT
struct ftdi_transfer_control * ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
struct libusb_context * usb_ctx
unsigned char buf[FTDI_MAX_EEPROM_SIZE]
unsigned int writebuffer_chunksize
#define SIO_READ_EEPROM_REQUEST
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
#define FTDI_MAX_EEPROM_SIZE
int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
#define HIGH_CURRENT_DRIVE_R
#define SIO_SET_FLOW_CTRL_REQUEST
#define SIO_SET_BITMODE_REQUEST
int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char *buf, int size)
#define SIO_SET_ERROR_CHAR_REQUEST
int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
unsigned char bitbang_mode
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
struct ftdi_context * ftdi
int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
const char * snapshot_str
#define POWER_SAVE_DISABLE_H
#define SIO_SET_MODEM_CTRL_REQUEST
#define SIO_POLL_MODEM_STATUS_REQUEST
int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial)
struct libusb_device_handle * usb_dev
int ftdi_usb_close(struct ftdi_context *ftdi)
int channel_d_rs485enable
unsigned int max_packet_size
int ftdi_setrts(struct ftdi_context *ftdi, int state)
int ftdi_usb_reset(struct ftdi_context *ftdi)
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
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_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)
struct ftdi_version_info ftdi_get_library_version(void)
Get libftdi library version.
int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
#define SIO_RESET_PURGE_RX
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
unsigned char bitbang_enabled
int channel_a_rs485enable
int ftdi_write_eeprom(struct ftdi_context *ftdi)
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char *description)
int ftdi_disable_bitbang(struct ftdi_context *ftdi)
int ftdi_setdtr(struct ftdi_context *ftdi, int state)
int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
#define SIO_RESET_PURGE_TX
unsigned int readbuffer_chunksize
int ftdi_read_eeprom(struct ftdi_context *ftdi)
char * ftdi_get_error_string(struct ftdi_context *ftdi)
#define SIO_ERASE_EEPROM_REQUEST
int ftdi_eeprom_build(struct ftdi_context *ftdi)
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
#define ftdi_error_return_free_device_list(code, str, devs)
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char *buf, int size)
#define ftdi_error_return(code, str)
unsigned int readbuffer_offset
void ftdi_free(struct ftdi_context *ftdi)
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
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)
const char FTDI_VERSION_STRING[]
const char FTDI_SNAPSHOT_VERSION[]
struct ftdi_eeprom * eeprom