1 /***************************************************************************
5 copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
18 \mainpage libftdi API documentation
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/en/developer/libftdi/
23 The library is easy to use. Have a look at this short example:
26 More examples can be found in the "examples" directory.
28 /** \addtogroup libftdi */
39 #include "ftdi_version_i.h"
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; \
57 Internal function to close usb device pointer.
58 Sets ftdi->usb_dev to NULL.
61 \param ftdi pointer to ftdi_context
65 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
67 if (ftdi && ftdi->usb_dev)
69 libusb_close (ftdi->usb_dev);
72 ftdi->eeprom->initialized_for_connected_device = 0;
77 Initializes a ftdi_context.
79 \param ftdi pointer to ftdi_context
82 \retval -1: couldn't allocate read buffer
83 \retval -2: couldn't allocate struct buffer
84 \retval -3: libusb_init() failed
86 \remark This should be called before all functions
88 int ftdi_init(struct ftdi_context *ftdi)
90 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
93 ftdi->usb_read_timeout = 5000;
94 ftdi->usb_write_timeout = 5000;
96 ftdi->type = TYPE_BM; /* chip type */
98 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
100 ftdi->readbuffer = NULL;
101 ftdi->readbuffer_offset = 0;
102 ftdi->readbuffer_remaining = 0;
103 ftdi->writebuffer_chunksize = 4096;
104 ftdi->max_packet_size = 0;
105 ftdi->error_str = NULL;
106 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
108 if (libusb_init(&ftdi->usb_ctx) < 0)
109 ftdi_error_return(-3, "libusb_init() failed");
111 ftdi_set_interface(ftdi, INTERFACE_ANY);
112 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
115 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
116 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
117 ftdi->eeprom = eeprom;
119 /* All fine. Now allocate the readbuffer */
120 return ftdi_read_data_set_chunksize(ftdi, 4096);
124 Allocate and initialize a new ftdi_context
126 \return a pointer to a new ftdi_context, or NULL on failure
128 struct ftdi_context *ftdi_new(void)
130 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
137 if (ftdi_init(ftdi) != 0)
147 Open selected channels on a chip, otherwise use first channel.
149 \param ftdi pointer to ftdi_context
150 \param interface Interface to use for FT2232C/2232H/4232H chips.
153 \retval -1: unknown interface
154 \retval -2: USB device unavailable
155 \retval -3: Device already open, interface can't be set in that state
157 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
160 ftdi_error_return(-2, "USB device unavailable");
162 if (ftdi->usb_dev != NULL)
164 int check_interface = interface;
165 if (check_interface == INTERFACE_ANY)
166 check_interface = INTERFACE_A;
168 if (ftdi->index != check_interface)
169 ftdi_error_return(-3, "Interface can not be changed on an already open device");
177 ftdi->index = INTERFACE_A;
183 ftdi->index = INTERFACE_B;
189 ftdi->index = INTERFACE_C;
195 ftdi->index = INTERFACE_D;
200 ftdi_error_return(-1, "Unknown interface");
206 Deinitializes a ftdi_context.
208 \param ftdi pointer to ftdi_context
210 void ftdi_deinit(struct ftdi_context *ftdi)
215 ftdi_usb_close_internal (ftdi);
217 if (ftdi->readbuffer != NULL)
219 free(ftdi->readbuffer);
220 ftdi->readbuffer = NULL;
223 if (ftdi->eeprom != NULL)
225 if (ftdi->eeprom->manufacturer != 0)
227 free(ftdi->eeprom->manufacturer);
228 ftdi->eeprom->manufacturer = 0;
230 if (ftdi->eeprom->product != 0)
232 free(ftdi->eeprom->product);
233 ftdi->eeprom->product = 0;
235 if (ftdi->eeprom->serial != 0)
237 free(ftdi->eeprom->serial);
238 ftdi->eeprom->serial = 0;
246 libusb_exit(ftdi->usb_ctx);
247 ftdi->usb_ctx = NULL;
252 Deinitialize and free an ftdi_context.
254 \param ftdi pointer to ftdi_context
256 void ftdi_free(struct ftdi_context *ftdi)
263 Use an already open libusb device.
265 \param ftdi pointer to ftdi_context
266 \param usb libusb libusb_device_handle to use
268 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
277 * @brief Get libftdi library version
279 * @return ftdi_version_info Library version information
281 struct ftdi_version_info ftdi_get_library_version(void)
283 struct ftdi_version_info ver;
285 ver.major = FTDI_MAJOR_VERSION;
286 ver.minor = FTDI_MINOR_VERSION;
287 ver.micro = FTDI_MICRO_VERSION;
288 ver.version_str = FTDI_VERSION_STRING;
289 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
295 Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
296 ftdi_device_list which needs to be deallocated by ftdi_list_free() after
297 use. With VID:PID 0:0, search for the default devices
298 (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015)
300 \param ftdi pointer to ftdi_context
301 \param devlist Pointer where to store list of found devices
302 \param vendor Vendor ID to search for
303 \param product Product ID to search for
305 \retval >0: number of devices found
306 \retval -3: out of memory
307 \retval -5: libusb_get_device_list() failed
308 \retval -6: libusb_get_device_descriptor() failed
310 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
312 struct ftdi_device_list **curdev;
314 libusb_device **devs;
318 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
319 ftdi_error_return(-5, "libusb_get_device_list() failed");
324 while ((dev = devs[i++]) != NULL)
326 struct libusb_device_descriptor desc;
328 if (libusb_get_device_descriptor(dev, &desc) < 0)
329 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
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)))
338 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
340 ftdi_error_return_free_device_list(-3, "out of memory", devs);
342 (*curdev)->next = NULL;
343 (*curdev)->dev = dev;
344 libusb_ref_device(dev);
345 curdev = &(*curdev)->next;
349 libusb_free_device_list(devs,1);
354 Frees a usb device list.
356 \param devlist USB device list created by ftdi_usb_find_all()
358 void ftdi_list_free(struct ftdi_device_list **devlist)
360 struct ftdi_device_list *curdev, *next;
362 for (curdev = *devlist; curdev != NULL;)
365 libusb_unref_device(curdev->dev);
374 Frees a usb device list.
376 \param devlist USB device list created by ftdi_usb_find_all()
378 void ftdi_list_free2(struct ftdi_device_list *devlist)
380 ftdi_list_free(&devlist);
384 Return device ID strings from the usb device.
386 The parameters manufacturer, description and serial may be NULL
387 or pointer to buffers to store the fetched strings.
389 \note Use this function only in combination with ftdi_usb_find_all()
390 as it closes the internal "usb_dev" after use.
392 \param ftdi pointer to ftdi_context
393 \param dev libusb usb_dev to use
394 \param manufacturer Store manufacturer string here if not NULL
395 \param mnf_len Buffer size of manufacturer string
396 \param description Store product description string here if not NULL
397 \param desc_len Buffer size of product description string
398 \param serial Store serial string here if not NULL
399 \param serial_len Buffer size of serial string
402 \retval -1: wrong arguments
403 \retval -4: unable to open device
404 \retval -7: get product manufacturer failed
405 \retval -8: get product description failed
406 \retval -9: get serial number failed
407 \retval -11: libusb_get_device_descriptor() failed
409 int ftdi_usb_get_strings(struct ftdi_context *ftdi,
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)
421 ftdi_error_return(-4, "libusb_open() failed");
423 // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
424 // it won't be closed either. This allows us to close it whether we actually
425 // called libusb_open() up above or not. This matches the expected behavior
426 // (and note) for ftdi_usb_get_strings().
427 ret = ftdi_usb_get_strings2(ftdi, dev,
428 manufacturer, mnf_len,
429 description, desc_len,
432 // only close it if it was successful, as all other return codes close
433 // before returning already.
435 ftdi_usb_close_internal(ftdi);
441 Return device ID strings from the usb device.
443 The parameters manufacturer, description and serial may be NULL
444 or pointer to buffers to store the fetched strings.
446 \note The old function ftdi_usb_get_strings() always closes the device.
447 This version only closes the device if it was opened by it.
449 \param ftdi pointer to ftdi_context
450 \param dev libusb usb_dev to use
451 \param manufacturer Store manufacturer string here if not NULL
452 \param mnf_len Buffer size of manufacturer string
453 \param description Store product description string here if not NULL
454 \param desc_len Buffer size of product description string
455 \param serial Store serial string here if not NULL
456 \param serial_len Buffer size of serial string
459 \retval -1: wrong arguments
460 \retval -4: unable to open device
461 \retval -7: get product manufacturer failed
462 \retval -8: get product description failed
463 \retval -9: get serial number failed
464 \retval -11: libusb_get_device_descriptor() failed
466 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
467 char *manufacturer, int mnf_len,
468 char *description, int desc_len,
469 char *serial, int serial_len)
471 struct libusb_device_descriptor desc;
474 if ((ftdi==NULL) || (dev==NULL))
477 need_open = (ftdi->usb_dev == NULL);
478 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
479 ftdi_error_return(-4, "libusb_open() failed");
481 if (libusb_get_device_descriptor(dev, &desc) < 0)
482 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
484 if (manufacturer != NULL)
486 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
488 ftdi_usb_close_internal (ftdi);
489 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
493 if (description != NULL)
495 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
497 ftdi_usb_close_internal (ftdi);
498 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
504 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
506 ftdi_usb_close_internal (ftdi);
507 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
512 ftdi_usb_close_internal (ftdi);
518 * Internal function to determine the maximum packet size.
519 * \param ftdi pointer to ftdi_context
520 * \param dev libusb usb_dev to use
521 * \retval Maximum packet size for this device
523 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
525 struct libusb_device_descriptor desc;
526 struct libusb_config_descriptor *config0;
527 unsigned int packet_size;
530 if (ftdi == NULL || dev == NULL)
533 // Determine maximum packet size. Init with default value.
534 // New hi-speed devices from FTDI use a packet size of 512 bytes
535 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
536 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
541 if (libusb_get_device_descriptor(dev, &desc) < 0)
544 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
547 if (desc.bNumConfigurations > 0)
549 if (ftdi->interface < config0->bNumInterfaces)
551 struct libusb_interface interface = config0->interface[ftdi->interface];
552 if (interface.num_altsetting > 0)
554 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
555 if (descriptor.bNumEndpoints > 0)
557 packet_size = descriptor.endpoint[0].wMaxPacketSize;
563 libusb_free_config_descriptor (config0);
568 Opens a ftdi device given by an usb_device.
570 \param ftdi pointer to ftdi_context
571 \param dev libusb usb_dev to use
574 \retval -3: unable to config device
575 \retval -4: unable to open device
576 \retval -5: unable to claim device
577 \retval -6: reset failed
578 \retval -7: set baudrate failed
579 \retval -8: ftdi context invalid
580 \retval -9: libusb_get_device_descriptor() failed
581 \retval -10: libusb_get_config_descriptor() failed
582 \retval -11: libusb_detach_kernel_driver() failed
583 \retval -12: libusb_get_configuration() failed
585 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
587 struct libusb_device_descriptor desc;
588 struct libusb_config_descriptor *config0;
589 int cfg, cfg0, detach_errno = 0;
592 ftdi_error_return(-8, "ftdi context invalid");
594 if (libusb_open(dev, &ftdi->usb_dev) < 0)
595 ftdi_error_return(-4, "libusb_open() failed");
597 if (libusb_get_device_descriptor(dev, &desc) < 0)
598 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
600 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
601 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
602 cfg0 = config0->bConfigurationValue;
603 libusb_free_config_descriptor (config0);
605 // Try to detach ftdi_sio kernel module.
607 // The return code is kept in a separate variable and only parsed
608 // if usb_set_configuration() or usb_claim_interface() fails as the
609 // detach operation might be denied and everything still works fine.
610 // Likely scenario is a static ftdi_sio kernel module.
611 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
613 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
614 detach_errno = errno;
617 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
618 ftdi_error_return(-12, "libusb_get_configuration () failed");
619 // set configuration (needed especially for windows)
620 // tolerate EBUSY: one device with one configuration, but two interfaces
621 // and libftdi sessions to both interfaces (e.g. FT2232)
622 if (desc.bNumConfigurations > 0 && cfg != cfg0)
624 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
626 ftdi_usb_close_internal (ftdi);
627 if (detach_errno == EPERM)
629 ftdi_error_return(-8, "inappropriate permissions on device!");
633 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
638 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
640 ftdi_usb_close_internal (ftdi);
641 if (detach_errno == EPERM)
643 ftdi_error_return(-8, "inappropriate permissions on device!");
647 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
651 if (ftdi_usb_reset (ftdi) != 0)
653 ftdi_usb_close_internal (ftdi);
654 ftdi_error_return(-6, "ftdi_usb_reset failed");
657 // Try to guess chip type
658 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
659 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
660 && desc.iSerialNumber == 0))
661 ftdi->type = TYPE_BM;
662 else if (desc.bcdDevice == 0x200)
663 ftdi->type = TYPE_AM;
664 else if (desc.bcdDevice == 0x500)
665 ftdi->type = TYPE_2232C;
666 else if (desc.bcdDevice == 0x600)
668 else if (desc.bcdDevice == 0x700)
669 ftdi->type = TYPE_2232H;
670 else if (desc.bcdDevice == 0x800)
671 ftdi->type = TYPE_4232H;
672 else if (desc.bcdDevice == 0x900)
673 ftdi->type = TYPE_232H;
674 else if (desc.bcdDevice == 0x1000)
675 ftdi->type = TYPE_230X;
677 // Determine maximum packet size
678 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
680 if (ftdi_set_baudrate (ftdi, 9600) != 0)
682 ftdi_usb_close_internal (ftdi);
683 ftdi_error_return(-7, "set baudrate failed");
686 ftdi_error_return(0, "all fine");
690 Opens the first device with a given vendor and product ids.
692 \param ftdi pointer to ftdi_context
693 \param vendor Vendor ID
694 \param product Product ID
696 \retval same as ftdi_usb_open_desc()
698 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
700 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
704 Opens the first device with a given, vendor id, product id,
705 description and serial.
707 \param ftdi pointer to ftdi_context
708 \param vendor Vendor ID
709 \param product Product ID
710 \param description Description to search for. Use NULL if not needed.
711 \param serial Serial to search for. Use NULL if not needed.
714 \retval -3: usb device not found
715 \retval -4: unable to open device
716 \retval -5: unable to claim device
717 \retval -6: reset failed
718 \retval -7: set baudrate failed
719 \retval -8: get product description failed
720 \retval -9: get serial number failed
721 \retval -12: libusb_get_device_list() failed
722 \retval -13: libusb_get_device_descriptor() failed
724 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
725 const char* description, const char* serial)
727 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
731 Opens the index-th device with a given, vendor id, product id,
732 description and serial.
734 \param ftdi pointer to ftdi_context
735 \param vendor Vendor ID
736 \param product Product ID
737 \param description Description to search for. Use NULL if not needed.
738 \param serial Serial to search for. Use NULL if not needed.
739 \param index Number of matching device to open if there are more than one, starts with 0.
742 \retval -1: usb_find_busses() failed
743 \retval -2: usb_find_devices() failed
744 \retval -3: usb device not found
745 \retval -4: unable to open device
746 \retval -5: unable to claim device
747 \retval -6: reset failed
748 \retval -7: set baudrate failed
749 \retval -8: get product description failed
750 \retval -9: get serial number failed
751 \retval -10: unable to close device
752 \retval -11: ftdi context invalid
754 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
755 const char* description, const char* serial, unsigned int index)
758 libusb_device **devs;
763 ftdi_error_return(-11, "ftdi context invalid");
765 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
766 ftdi_error_return(-12, "libusb_get_device_list() failed");
768 while ((dev = devs[i++]) != NULL)
770 struct libusb_device_descriptor desc;
773 if (libusb_get_device_descriptor(dev, &desc) < 0)
774 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
776 if (desc.idVendor == vendor && desc.idProduct == product)
778 if (libusb_open(dev, &ftdi->usb_dev) < 0)
779 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
781 if (description != NULL)
783 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
785 ftdi_usb_close_internal (ftdi);
786 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
788 if (strncmp(string, description, sizeof(string)) != 0)
790 ftdi_usb_close_internal (ftdi);
796 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
798 ftdi_usb_close_internal (ftdi);
799 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
801 if (strncmp(string, serial, sizeof(string)) != 0)
803 ftdi_usb_close_internal (ftdi);
808 ftdi_usb_close_internal (ftdi);
816 res = ftdi_usb_open_dev(ftdi, dev);
817 libusb_free_device_list(devs,1);
823 ftdi_error_return_free_device_list(-3, "device not found", devs);
827 Opens the ftdi-device described by a description-string.
828 Intended to be used for parsing a device-description given as commandline argument.
830 \param ftdi pointer to ftdi_context
831 \param description NULL-terminated description-string, using this format:
832 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
833 \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
834 \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one
835 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
837 \note The description format may be extended in later versions.
840 \retval -2: libusb_get_device_list() failed
841 \retval -3: usb device not found
842 \retval -4: unable to open device
843 \retval -5: unable to claim device
844 \retval -6: reset failed
845 \retval -7: set baudrate failed
846 \retval -8: get product description failed
847 \retval -9: get serial number failed
848 \retval -10: unable to close device
849 \retval -11: illegal description format
850 \retval -12: ftdi context invalid
852 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
855 ftdi_error_return(-12, "ftdi context invalid");
857 if (description[0] == 0 || description[1] != ':')
858 ftdi_error_return(-11, "illegal description format");
860 if (description[0] == 'd')
863 libusb_device **devs;
864 unsigned int bus_number, device_address;
867 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
868 ftdi_error_return(-2, "libusb_get_device_list() failed");
870 /* XXX: This doesn't handle symlinks/odd paths/etc... */
871 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
872 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
874 while ((dev = devs[i++]) != NULL)
877 if (bus_number == libusb_get_bus_number (dev)
878 && device_address == libusb_get_device_address (dev))
880 ret = ftdi_usb_open_dev(ftdi, dev);
881 libusb_free_device_list(devs,1);
887 ftdi_error_return_free_device_list(-3, "device not found", devs);
889 else if (description[0] == 'i' || description[0] == 's')
892 unsigned int product;
893 unsigned int index=0;
894 const char *serial=NULL;
895 const char *startp, *endp;
898 startp=description+2;
899 vendor=strtoul((char*)startp,(char**)&endp,0);
900 if (*endp != ':' || endp == startp || errno != 0)
901 ftdi_error_return(-11, "illegal description format");
904 product=strtoul((char*)startp,(char**)&endp,0);
905 if (endp == startp || errno != 0)
906 ftdi_error_return(-11, "illegal description format");
908 if (description[0] == 'i' && *endp != 0)
910 /* optional index field in i-mode */
912 ftdi_error_return(-11, "illegal description format");
915 index=strtoul((char*)startp,(char**)&endp,0);
916 if (*endp != 0 || endp == startp || errno != 0)
917 ftdi_error_return(-11, "illegal description format");
919 if (description[0] == 's')
922 ftdi_error_return(-11, "illegal description format");
924 /* rest of the description is the serial */
928 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
932 ftdi_error_return(-11, "illegal description format");
937 Resets the ftdi device.
939 \param ftdi pointer to ftdi_context
942 \retval -1: FTDI reset failed
943 \retval -2: USB device unavailable
945 int ftdi_usb_reset(struct ftdi_context *ftdi)
947 if (ftdi == NULL || ftdi->usb_dev == NULL)
948 ftdi_error_return(-2, "USB device unavailable");
950 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
951 SIO_RESET_REQUEST, SIO_RESET_SIO,
952 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
953 ftdi_error_return(-1,"FTDI reset failed");
955 // Invalidate data in the readbuffer
956 ftdi->readbuffer_offset = 0;
957 ftdi->readbuffer_remaining = 0;
963 Clears the read buffer on the chip and the internal read buffer.
965 \param ftdi pointer to ftdi_context
968 \retval -1: read buffer purge failed
969 \retval -2: USB device unavailable
971 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
973 if (ftdi == NULL || ftdi->usb_dev == NULL)
974 ftdi_error_return(-2, "USB device unavailable");
976 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
977 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
978 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
979 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
981 // Invalidate data in the readbuffer
982 ftdi->readbuffer_offset = 0;
983 ftdi->readbuffer_remaining = 0;
989 Clears the write buffer on the chip.
991 \param ftdi pointer to ftdi_context
994 \retval -1: write buffer purge failed
995 \retval -2: USB device unavailable
997 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
999 if (ftdi == NULL || ftdi->usb_dev == NULL)
1000 ftdi_error_return(-2, "USB device unavailable");
1002 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1003 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
1004 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1005 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1011 Clears the buffers on the chip and the internal read buffer.
1013 \param ftdi pointer to ftdi_context
1016 \retval -1: read buffer purge failed
1017 \retval -2: write buffer purge failed
1018 \retval -3: USB device unavailable
1020 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
1024 if (ftdi == NULL || ftdi->usb_dev == NULL)
1025 ftdi_error_return(-3, "USB device unavailable");
1027 result = ftdi_usb_purge_rx_buffer(ftdi);
1031 result = ftdi_usb_purge_tx_buffer(ftdi);
1041 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
1043 \param ftdi pointer to ftdi_context
1046 \retval -1: usb_release failed
1047 \retval -3: ftdi context invalid
1049 int ftdi_usb_close(struct ftdi_context *ftdi)
1054 ftdi_error_return(-3, "ftdi context invalid");
1056 if (ftdi->usb_dev != NULL)
1057 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1060 ftdi_usb_close_internal (ftdi);
1065 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1066 to encoded divisor and the achievable baudrate
1067 Function is only used internally
1074 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1075 The fractional part has frac_code encoding
1077 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1080 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1081 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1082 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1083 int divisor, best_divisor, best_baud, best_baud_diff;
1085 divisor = 24000000 / baudrate;
1087 // Round down to supported fraction (AM only)
1088 divisor -= am_adjust_dn[divisor & 7];
1090 // Try this divisor and the one above it (because division rounds down)
1094 for (i = 0; i < 2; i++)
1096 int try_divisor = divisor + i;
1100 // Round up to supported divisor value
1101 if (try_divisor <= 8)
1103 // Round up to minimum supported divisor
1106 else if (divisor < 16)
1108 // AM doesn't support divisors 9 through 15 inclusive
1113 // Round up to supported fraction (AM only)
1114 try_divisor += am_adjust_up[try_divisor & 7];
1115 if (try_divisor > 0x1FFF8)
1117 // Round down to maximum supported divisor value (for AM)
1118 try_divisor = 0x1FFF8;
1121 // Get estimated baud rate (to nearest integer)
1122 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1123 // Get absolute difference from requested baud rate
1124 if (baud_estimate < baudrate)
1126 baud_diff = baudrate - baud_estimate;
1130 baud_diff = baud_estimate - baudrate;
1132 if (i == 0 || baud_diff < best_baud_diff)
1134 // Closest to requested baud rate so far
1135 best_divisor = try_divisor;
1136 best_baud = baud_estimate;
1137 best_baud_diff = baud_diff;
1140 // Spot on! No point trying
1145 // Encode the best divisor value
1146 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1147 // Deal with special cases for encoded value
1148 if (*encoded_divisor == 1)
1150 *encoded_divisor = 0; // 3000000 baud
1152 else if (*encoded_divisor == 0x4001)
1154 *encoded_divisor = 1; // 2000000 baud (BM only)
1159 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1160 to encoded divisor and the achievable baudrate
1161 Function is only used internally
1168 From /2, 0.125 steps may be taken.
1169 The fractional part has frac_code encoding
1171 value[13:0] of value is the divisor
1172 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1174 H Type have all features above with
1175 {index[8],value[15:14]} is the encoded subdivisor
1177 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1178 {index[0],value[15:14]} is the encoded subdivisor
1180 AM Type chips have only four fractional subdivisors at value[15:14]
1181 for subdivisors 0, 0.5, 0.25, 0.125
1183 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1185 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1187 int divisor, best_divisor;
1188 if (baudrate >= clk/clk_div)
1190 *encoded_divisor = 0;
1191 best_baud = clk/clk_div;
1193 else if (baudrate >= clk/(clk_div + clk_div/2))
1195 *encoded_divisor = 1;
1196 best_baud = clk/(clk_div + clk_div/2);
1198 else if (baudrate >= clk/(2*clk_div))
1200 *encoded_divisor = 2;
1201 best_baud = clk/(2*clk_div);
1205 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1206 divisor = clk*16/clk_div / baudrate;
1207 if (divisor & 1) /* Decide if to round up or down*/
1208 best_divisor = divisor /2 +1;
1210 best_divisor = divisor/2;
1211 if(best_divisor > 0x20000)
1212 best_divisor = 0x1ffff;
1213 best_baud = clk*16/clk_div/best_divisor;
1214 if (best_baud & 1) /* Decide if to round up or down*/
1215 best_baud = best_baud /2 +1;
1217 best_baud = best_baud /2;
1218 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1223 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1224 Function is only used internally
1227 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1228 unsigned short *value, unsigned short *index)
1231 unsigned long encoded_divisor;
1239 #define H_CLK 120000000
1240 #define C_CLK 48000000
1241 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1243 if(baudrate*10 > H_CLK /0x3fff)
1245 /* On H Devices, use 12 000 000 Baudrate when possible
1246 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1247 three fractional bits and a 120 MHz clock
1248 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1249 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1250 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1251 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1254 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1256 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
1258 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1262 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1264 // Split into "value" and "index" values
1265 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1266 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1268 *index = (unsigned short)(encoded_divisor >> 8);
1270 *index |= ftdi->index;
1273 *index = (unsigned short)(encoded_divisor >> 16);
1275 // Return the nearest baud rate
1280 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1281 * Do not use, it's only for the unit test framework
1283 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1284 unsigned short *value, unsigned short *index)
1286 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1290 Sets the chip baud rate
1292 \param ftdi pointer to ftdi_context
1293 \param baudrate baud rate to set
1296 \retval -1: invalid baudrate
1297 \retval -2: setting baudrate failed
1298 \retval -3: USB device unavailable
1300 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1302 unsigned short value, index;
1303 int actual_baudrate;
1305 if (ftdi == NULL || ftdi->usb_dev == NULL)
1306 ftdi_error_return(-3, "USB device unavailable");
1308 if (ftdi->bitbang_enabled)
1310 baudrate = baudrate*4;
1313 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1314 if (actual_baudrate <= 0)
1315 ftdi_error_return (-1, "Silly baudrate <= 0.");
1317 // Check within tolerance (about 5%)
1318 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1319 || ((actual_baudrate < baudrate)
1320 ? (actual_baudrate * 21 < baudrate * 20)
1321 : (baudrate * 21 < actual_baudrate * 20)))
1322 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1324 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1325 SIO_SET_BAUDRATE_REQUEST, value,
1326 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1327 ftdi_error_return (-2, "Setting new baudrate failed");
1329 ftdi->baudrate = baudrate;
1334 Set (RS232) line characteristics.
1335 The break type can only be set via ftdi_set_line_property2()
1336 and defaults to "off".
1338 \param ftdi pointer to ftdi_context
1339 \param bits Number of bits
1340 \param sbit Number of stop bits
1341 \param parity Parity mode
1344 \retval -1: Setting line property failed
1346 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1347 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1349 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1353 Set (RS232) line characteristics
1355 \param ftdi pointer to ftdi_context
1356 \param bits Number of bits
1357 \param sbit Number of stop bits
1358 \param parity Parity mode
1359 \param break_type Break type
1362 \retval -1: Setting line property failed
1363 \retval -2: USB device unavailable
1365 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1366 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1367 enum ftdi_break_type break_type)
1369 unsigned short value = bits;
1371 if (ftdi == NULL || ftdi->usb_dev == NULL)
1372 ftdi_error_return(-2, "USB device unavailable");
1377 value |= (0x00 << 8);
1380 value |= (0x01 << 8);
1383 value |= (0x02 << 8);
1386 value |= (0x03 << 8);
1389 value |= (0x04 << 8);
1396 value |= (0x00 << 11);
1399 value |= (0x01 << 11);
1402 value |= (0x02 << 11);
1409 value |= (0x00 << 14);
1412 value |= (0x01 << 14);
1416 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1417 SIO_SET_DATA_REQUEST, value,
1418 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1419 ftdi_error_return (-1, "Setting new line property failed");
1425 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1427 \param ftdi pointer to ftdi_context
1428 \param buf Buffer with the data
1429 \param size Size of the buffer
1431 \retval -666: USB device unavailable
1432 \retval <0: error code from usb_bulk_write()
1433 \retval >0: number of bytes written
1435 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1440 if (ftdi == NULL || ftdi->usb_dev == NULL)
1441 ftdi_error_return(-666, "USB device unavailable");
1443 while (offset < size)
1445 int write_size = ftdi->writebuffer_chunksize;
1447 if (offset+write_size > size)
1448 write_size = size-offset;
1450 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1451 ftdi_error_return(-1, "usb bulk write failed");
1453 offset += actual_length;
1459 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1461 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1462 struct ftdi_context *ftdi = tc->ftdi;
1463 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1465 packet_size = ftdi->max_packet_size;
1467 actual_length = transfer->actual_length;
1469 if (actual_length > 2)
1471 // skip FTDI status bytes.
1472 // Maybe stored in the future to enable modem use
1473 num_of_chunks = actual_length / packet_size;
1474 chunk_remains = actual_length % packet_size;
1475 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1477 ftdi->readbuffer_offset += 2;
1480 if (actual_length > packet_size - 2)
1482 for (i = 1; i < num_of_chunks; i++)
1483 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1484 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1486 if (chunk_remains > 2)
1488 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1489 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1491 actual_length -= 2*num_of_chunks;
1494 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1497 if (actual_length > 0)
1499 // data still fits in buf?
1500 if (tc->offset + actual_length <= tc->size)
1502 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1503 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1504 tc->offset += actual_length;
1506 ftdi->readbuffer_offset = 0;
1507 ftdi->readbuffer_remaining = 0;
1509 /* Did we read exactly the right amount of bytes? */
1510 if (tc->offset == tc->size)
1512 //printf("read_data exact rem %d offset %d\n",
1513 //ftdi->readbuffer_remaining, offset);
1520 // only copy part of the data or size <= readbuffer_chunksize
1521 int part_size = tc->size - tc->offset;
1522 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1523 tc->offset += part_size;
1525 ftdi->readbuffer_offset += part_size;
1526 ftdi->readbuffer_remaining = actual_length - part_size;
1528 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1529 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1536 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1537 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1540 ret = libusb_submit_transfer (transfer);
1547 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1549 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1550 struct ftdi_context *ftdi = tc->ftdi;
1552 tc->offset += transfer->actual_length;
1554 if (tc->offset == tc->size)
1560 int write_size = ftdi->writebuffer_chunksize;
1563 if (tc->offset + write_size > tc->size)
1564 write_size = tc->size - tc->offset;
1566 transfer->length = write_size;
1567 transfer->buffer = tc->buf + tc->offset;
1569 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1570 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1573 ret = libusb_submit_transfer (transfer);
1582 Writes data to the chip. Does not wait for completion of the transfer
1583 nor does it make sure that the transfer was successful.
1585 Use libusb 1.0 asynchronous API.
1587 \param ftdi pointer to ftdi_context
1588 \param buf Buffer with the data
1589 \param size Size of the buffer
1591 \retval NULL: Some error happens when submit transfer
1592 \retval !NULL: Pointer to a ftdi_transfer_control
1595 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1597 struct ftdi_transfer_control *tc;
1598 struct libusb_transfer *transfer;
1599 int write_size, ret;
1601 if (ftdi == NULL || ftdi->usb_dev == NULL)
1604 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1608 transfer = libusb_alloc_transfer(0);
1621 if (size < (int)ftdi->writebuffer_chunksize)
1624 write_size = ftdi->writebuffer_chunksize;
1626 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1627 write_size, ftdi_write_data_cb, tc,
1628 ftdi->usb_write_timeout);
1629 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1631 ret = libusb_submit_transfer(transfer);
1634 libusb_free_transfer(transfer);
1638 tc->transfer = transfer;
1644 Reads data from the chip. Does not wait for completion of the transfer
1645 nor does it make sure that the transfer was successful.
1647 Use libusb 1.0 asynchronous API.
1649 \param ftdi pointer to ftdi_context
1650 \param buf Buffer with the data
1651 \param size Size of the buffer
1653 \retval NULL: Some error happens when submit transfer
1654 \retval !NULL: Pointer to a ftdi_transfer_control
1657 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1659 struct ftdi_transfer_control *tc;
1660 struct libusb_transfer *transfer;
1663 if (ftdi == NULL || ftdi->usb_dev == NULL)
1666 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1674 if (size <= (int)ftdi->readbuffer_remaining)
1676 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1679 ftdi->readbuffer_remaining -= size;
1680 ftdi->readbuffer_offset += size;
1682 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1686 tc->transfer = NULL;
1691 if (ftdi->readbuffer_remaining != 0)
1693 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1695 tc->offset = ftdi->readbuffer_remaining;
1700 transfer = libusb_alloc_transfer(0);
1707 ftdi->readbuffer_remaining = 0;
1708 ftdi->readbuffer_offset = 0;
1710 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout);
1711 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1713 ret = libusb_submit_transfer(transfer);
1716 libusb_free_transfer(transfer);
1720 tc->transfer = transfer;
1726 Wait for completion of the transfer.
1728 Use libusb 1.0 asynchronous API.
1730 \param tc pointer to ftdi_transfer_control
1732 \retval < 0: Some error happens
1733 \retval >= 0: Data size transferred
1736 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1739 struct timeval to = { 0, 0 };
1740 while (!tc->completed)
1742 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1743 &to, &tc->completed);
1746 if (ret == LIBUSB_ERROR_INTERRUPTED)
1748 libusb_cancel_transfer(tc->transfer);
1749 while (!tc->completed)
1750 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1751 &to, &tc->completed) < 0)
1753 libusb_free_transfer(tc->transfer);
1761 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1762 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1766 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1768 libusb_free_transfer(tc->transfer);
1775 Cancel transfer and wait for completion.
1777 Use libusb 1.0 asynchronous API.
1779 \param tc pointer to ftdi_transfer_control
1780 \param to pointer to timeout value or NULL for infinite
1783 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
1784 struct timeval * to)
1786 struct timeval tv = { 0, 0 };
1788 if (!tc->completed && tc->transfer != NULL)
1793 libusb_cancel_transfer(tc->transfer);
1794 while (!tc->completed)
1796 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1802 libusb_free_transfer(tc->transfer);
1808 Configure write buffer chunk size.
1811 \param ftdi pointer to ftdi_context
1812 \param chunksize Chunk size
1815 \retval -1: ftdi context invalid
1817 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1820 ftdi_error_return(-1, "ftdi context invalid");
1822 ftdi->writebuffer_chunksize = chunksize;
1827 Get write buffer chunk size.
1829 \param ftdi pointer to ftdi_context
1830 \param chunksize Pointer to store chunk size in
1833 \retval -1: ftdi context invalid
1835 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1838 ftdi_error_return(-1, "ftdi context invalid");
1840 *chunksize = ftdi->writebuffer_chunksize;
1845 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1847 Automatically strips the two modem status bytes transfered during every read.
1849 \param ftdi pointer to ftdi_context
1850 \param buf Buffer to store data in
1851 \param size Size of the buffer
1853 \retval -666: USB device unavailable
1854 \retval <0: error code from libusb_bulk_transfer()
1855 \retval 0: no data was available
1856 \retval >0: number of bytes read
1859 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1861 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1862 int packet_size = ftdi->max_packet_size;
1863 int actual_length = 1;
1865 if (ftdi == NULL || ftdi->usb_dev == NULL)
1866 ftdi_error_return(-666, "USB device unavailable");
1868 // Packet size sanity check (avoid division by zero)
1869 if (packet_size == 0)
1870 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1872 // everything we want is still in the readbuffer?
1873 if (size <= (int)ftdi->readbuffer_remaining)
1875 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1878 ftdi->readbuffer_remaining -= size;
1879 ftdi->readbuffer_offset += size;
1881 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1885 // something still in the readbuffer, but not enough to satisfy 'size'?
1886 if (ftdi->readbuffer_remaining != 0)
1888 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1891 offset += ftdi->readbuffer_remaining;
1893 // do the actual USB read
1894 while (offset < size && actual_length > 0)
1896 ftdi->readbuffer_remaining = 0;
1897 ftdi->readbuffer_offset = 0;
1898 /* returns how much received */
1899 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1901 ftdi_error_return(ret, "usb bulk read failed");
1903 if (actual_length > 2)
1905 // skip FTDI status bytes.
1906 // Maybe stored in the future to enable modem use
1907 num_of_chunks = actual_length / packet_size;
1908 chunk_remains = actual_length % packet_size;
1909 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1911 ftdi->readbuffer_offset += 2;
1914 if (actual_length > packet_size - 2)
1916 for (i = 1; i < num_of_chunks; i++)
1917 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1918 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1920 if (chunk_remains > 2)
1922 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1923 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1925 actual_length -= 2*num_of_chunks;
1928 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1931 else if (actual_length <= 2)
1933 // no more data to read?
1936 if (actual_length > 0)
1938 // data still fits in buf?
1939 if (offset+actual_length <= size)
1941 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1942 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1943 offset += actual_length;
1945 /* Did we read exactly the right amount of bytes? */
1947 //printf("read_data exact rem %d offset %d\n",
1948 //ftdi->readbuffer_remaining, offset);
1953 // only copy part of the data or size <= readbuffer_chunksize
1954 int part_size = size-offset;
1955 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1957 ftdi->readbuffer_offset += part_size;
1958 ftdi->readbuffer_remaining = actual_length-part_size;
1959 offset += part_size;
1961 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1962 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1973 Configure read buffer chunk size.
1976 Automatically reallocates the buffer.
1978 \param ftdi pointer to ftdi_context
1979 \param chunksize Chunk size
1982 \retval -1: ftdi context invalid
1984 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1986 unsigned char *new_buf;
1989 ftdi_error_return(-1, "ftdi context invalid");
1991 // Invalidate all remaining data
1992 ftdi->readbuffer_offset = 0;
1993 ftdi->readbuffer_remaining = 0;
1995 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1996 which is defined in libusb-1.0. Otherwise, each USB read request will
1997 be divided into multiple URBs. This will cause issues on Linux kernel
1998 older than 2.6.32. */
1999 if (chunksize > 16384)
2003 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2004 ftdi_error_return(-1, "out of memory for readbuffer");
2006 ftdi->readbuffer = new_buf;
2007 ftdi->readbuffer_chunksize = chunksize;
2013 Get read buffer chunk size.
2015 \param ftdi pointer to ftdi_context
2016 \param chunksize Pointer to store chunk size in
2019 \retval -1: FTDI context invalid
2021 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2024 ftdi_error_return(-1, "FTDI context invalid");
2026 *chunksize = ftdi->readbuffer_chunksize;
2031 Enable/disable bitbang modes.
2033 \param ftdi pointer to ftdi_context
2034 \param bitmask Bitmask to configure lines.
2035 HIGH/ON value configures a line as output.
2036 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
2039 \retval -1: can't enable bitbang mode
2040 \retval -2: USB device unavailable
2042 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2044 unsigned short usb_val;
2046 if (ftdi == NULL || ftdi->usb_dev == NULL)
2047 ftdi_error_return(-2, "USB device unavailable");
2049 usb_val = bitmask; // low byte: bitmask
2050 usb_val |= (mode << 8);
2051 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2052 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2054 ftdi->bitbang_mode = mode;
2055 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2060 Disable bitbang mode.
2062 \param ftdi pointer to ftdi_context
2065 \retval -1: can't disable bitbang mode
2066 \retval -2: USB device unavailable
2068 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
2070 if (ftdi == NULL || ftdi->usb_dev == NULL)
2071 ftdi_error_return(-2, "USB device unavailable");
2073 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2074 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2076 ftdi->bitbang_enabled = 0;
2082 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
2084 \param ftdi pointer to ftdi_context
2085 \param pins Pointer to store pins into
2088 \retval -1: read pins failed
2089 \retval -2: USB device unavailable
2091 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2093 if (ftdi == NULL || ftdi->usb_dev == NULL)
2094 ftdi_error_return(-2, "USB device unavailable");
2096 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1)
2097 ftdi_error_return(-1, "read pins failed");
2105 The FTDI chip keeps data in the internal buffer for a specific
2106 amount of time if the buffer is not full yet to decrease
2107 load on the usb bus.
2109 \param ftdi pointer to ftdi_context
2110 \param latency Value between 1 and 255
2113 \retval -1: latency out of range
2114 \retval -2: unable to set latency timer
2115 \retval -3: USB device unavailable
2117 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2119 unsigned short usb_val;
2122 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2124 if (ftdi == NULL || ftdi->usb_dev == NULL)
2125 ftdi_error_return(-3, "USB device unavailable");
2128 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2129 ftdi_error_return(-2, "unable to set latency timer");
2137 \param ftdi pointer to ftdi_context
2138 \param latency Pointer to store latency value in
2141 \retval -1: unable to get latency timer
2142 \retval -2: USB device unavailable
2144 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2146 unsigned short usb_val;
2148 if (ftdi == NULL || ftdi->usb_dev == NULL)
2149 ftdi_error_return(-2, "USB device unavailable");
2151 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
2152 ftdi_error_return(-1, "reading latency timer failed");
2154 *latency = (unsigned char)usb_val;
2159 Poll modem status information
2161 This function allows the retrieve the two status bytes of the device.
2162 The device sends these bytes also as a header for each read access
2163 where they are discarded by ftdi_read_data(). The chip generates
2164 the two stripped status bytes in the absence of data every 40 ms.
2166 Layout of the first byte:
2167 - B0..B3 - must be 0
2168 - B4 Clear to send (CTS)
2171 - B5 Data set ready (DTS)
2174 - B6 Ring indicator (RI)
2177 - B7 Receive line signal detect (RLSD)
2181 Layout of the second byte:
2182 - B0 Data ready (DR)
2183 - B1 Overrun error (OE)
2184 - B2 Parity error (PE)
2185 - B3 Framing error (FE)
2186 - B4 Break interrupt (BI)
2187 - B5 Transmitter holding register (THRE)
2188 - B6 Transmitter empty (TEMT)
2189 - B7 Error in RCVR FIFO
2191 \param ftdi pointer to ftdi_context
2192 \param status Pointer to store status information in. Must be two bytes.
2195 \retval -1: unable to retrieve status information
2196 \retval -2: USB device unavailable
2198 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2202 if (ftdi == NULL || ftdi->usb_dev == NULL)
2203 ftdi_error_return(-2, "USB device unavailable");
2205 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
2206 ftdi_error_return(-1, "getting modem status failed");
2208 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2214 Set flowcontrol for ftdi chip
2216 \param ftdi pointer to ftdi_context
2217 \param flowctrl flow control to use. should be
2218 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2221 \retval -1: set flow control failed
2222 \retval -2: USB device unavailable
2224 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2226 if (ftdi == NULL || ftdi->usb_dev == NULL)
2227 ftdi_error_return(-2, "USB device unavailable");
2229 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2230 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2231 NULL, 0, ftdi->usb_write_timeout) < 0)
2232 ftdi_error_return(-1, "set flow control failed");
2240 \param ftdi pointer to ftdi_context
2241 \param state state to set line to (1 or 0)
2244 \retval -1: set dtr failed
2245 \retval -2: USB device unavailable
2247 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2249 unsigned short usb_val;
2251 if (ftdi == NULL || ftdi->usb_dev == NULL)
2252 ftdi_error_return(-2, "USB device unavailable");
2255 usb_val = SIO_SET_DTR_HIGH;
2257 usb_val = SIO_SET_DTR_LOW;
2259 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2260 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2261 NULL, 0, ftdi->usb_write_timeout) < 0)
2262 ftdi_error_return(-1, "set dtr failed");
2270 \param ftdi pointer to ftdi_context
2271 \param state state to set line to (1 or 0)
2274 \retval -1: set rts failed
2275 \retval -2: USB device unavailable
2277 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2279 unsigned short usb_val;
2281 if (ftdi == NULL || ftdi->usb_dev == NULL)
2282 ftdi_error_return(-2, "USB device unavailable");
2285 usb_val = SIO_SET_RTS_HIGH;
2287 usb_val = SIO_SET_RTS_LOW;
2289 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2290 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2291 NULL, 0, ftdi->usb_write_timeout) < 0)
2292 ftdi_error_return(-1, "set of rts failed");
2298 Set dtr and rts line in one pass
2300 \param ftdi pointer to ftdi_context
2301 \param dtr DTR state to set line to (1 or 0)
2302 \param rts RTS state to set line to (1 or 0)
2305 \retval -1: set dtr/rts failed
2306 \retval -2: USB device unavailable
2308 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2310 unsigned short usb_val;
2312 if (ftdi == NULL || ftdi->usb_dev == NULL)
2313 ftdi_error_return(-2, "USB device unavailable");
2316 usb_val = SIO_SET_DTR_HIGH;
2318 usb_val = SIO_SET_DTR_LOW;
2321 usb_val |= SIO_SET_RTS_HIGH;
2323 usb_val |= SIO_SET_RTS_LOW;
2325 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2326 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2327 NULL, 0, ftdi->usb_write_timeout) < 0)
2328 ftdi_error_return(-1, "set of rts/dtr failed");
2334 Set the special event character
2336 \param ftdi pointer to ftdi_context
2337 \param eventch Event character
2338 \param enable 0 to disable the event character, non-zero otherwise
2341 \retval -1: unable to set event character
2342 \retval -2: USB device unavailable
2344 int ftdi_set_event_char(struct ftdi_context *ftdi,
2345 unsigned char eventch, unsigned char enable)
2347 unsigned short usb_val;
2349 if (ftdi == NULL || ftdi->usb_dev == NULL)
2350 ftdi_error_return(-2, "USB device unavailable");
2356 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2357 ftdi_error_return(-1, "setting event character failed");
2365 \param ftdi pointer to ftdi_context
2366 \param errorch Error character
2367 \param enable 0 to disable the error character, non-zero otherwise
2370 \retval -1: unable to set error character
2371 \retval -2: USB device unavailable
2373 int ftdi_set_error_char(struct ftdi_context *ftdi,
2374 unsigned char errorch, unsigned char enable)
2376 unsigned short usb_val;
2378 if (ftdi == NULL || ftdi->usb_dev == NULL)
2379 ftdi_error_return(-2, "USB device unavailable");
2385 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2386 ftdi_error_return(-1, "setting error character failed");
2392 Init eeprom with default values for the connected device
2393 \param ftdi pointer to ftdi_context
2394 \param manufacturer String to use as Manufacturer
2395 \param product String to use as Product description
2396 \param serial String to use as Serial number description
2399 \retval -1: No struct ftdi_context
2400 \retval -2: No struct ftdi_eeprom
2401 \retval -3: No connected device or device not yet opened
2403 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2404 char * product, char * serial)
2406 struct ftdi_eeprom *eeprom;
2409 ftdi_error_return(-1, "No struct ftdi_context");
2411 if (ftdi->eeprom == NULL)
2412 ftdi_error_return(-2,"No struct ftdi_eeprom");
2414 eeprom = ftdi->eeprom;
2415 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2417 if (ftdi->usb_dev == NULL)
2418 ftdi_error_return(-3, "No connected device or device not yet opened");
2420 eeprom->vendor_id = 0x0403;
2421 eeprom->use_serial = 1;
2422 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2423 (ftdi->type == TYPE_R))
2424 eeprom->product_id = 0x6001;
2425 else if (ftdi->type == TYPE_4232H)
2426 eeprom->product_id = 0x6011;
2427 else if (ftdi->type == TYPE_232H)
2428 eeprom->product_id = 0x6014;
2429 else if (ftdi->type == TYPE_230X)
2430 eeprom->product_id = 0x6015;
2432 eeprom->product_id = 0x6010;
2434 if (ftdi->type == TYPE_AM)
2435 eeprom->usb_version = 0x0101;
2437 eeprom->usb_version = 0x0200;
2438 eeprom->max_power = 100;
2440 if (eeprom->manufacturer)
2441 free (eeprom->manufacturer);
2442 eeprom->manufacturer = NULL;
2445 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2446 if (eeprom->manufacturer)
2447 strcpy(eeprom->manufacturer, manufacturer);
2450 if (eeprom->product)
2451 free (eeprom->product);
2452 eeprom->product = NULL;
2455 eeprom->product = (char *)malloc(strlen(product)+1);
2456 if (eeprom->product)
2457 strcpy(eeprom->product, product);
2461 const char* default_product;
2464 case TYPE_AM: default_product = "AM"; break;
2465 case TYPE_BM: default_product = "BM"; break;
2466 case TYPE_2232C: default_product = "Dual RS232"; break;
2467 case TYPE_R: default_product = "FT232R USB UART"; break;
2468 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2469 case TYPE_4232H: default_product = "FT4232H"; break;
2470 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2471 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2473 ftdi_error_return(-3, "Unknown chip type");
2475 eeprom->product = (char *)malloc(strlen(default_product) +1);
2476 if (eeprom->product)
2477 strcpy(eeprom->product, default_product);
2481 free (eeprom->serial);
2482 eeprom->serial = NULL;
2485 eeprom->serial = (char *)malloc(strlen(serial)+1);
2487 strcpy(eeprom->serial, serial);
2490 if (ftdi->type == TYPE_R)
2492 eeprom->max_power = 90;
2493 eeprom->size = 0x80;
2494 eeprom->cbus_function[0] = CBUS_TXLED;
2495 eeprom->cbus_function[1] = CBUS_RXLED;
2496 eeprom->cbus_function[2] = CBUS_TXDEN;
2497 eeprom->cbus_function[3] = CBUS_PWREN;
2498 eeprom->cbus_function[4] = CBUS_SLEEP;
2500 else if (ftdi->type == TYPE_230X)
2502 eeprom->max_power = 90;
2503 eeprom->size = 0x100;
2504 eeprom->cbus_function[0] = CBUSX_TXDEN;
2505 eeprom->cbus_function[1] = CBUSX_RXLED;
2506 eeprom->cbus_function[2] = CBUSX_TXLED;
2507 eeprom->cbus_function[3] = CBUSX_SLEEP;
2511 if(ftdi->type == TYPE_232H)
2514 for (i=0; i<10; i++)
2515 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2522 eeprom->release_number = 0x0200;
2525 eeprom->release_number = 0x0400;
2528 eeprom->release_number = 0x0500;
2531 eeprom->release_number = 0x0600;
2534 eeprom->release_number = 0x0700;
2537 eeprom->release_number = 0x0800;
2540 eeprom->release_number = 0x0900;
2543 eeprom->release_number = 0x1000;
2546 eeprom->release_number = 0x00;
2551 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2552 char * product, char * serial)
2554 struct ftdi_eeprom *eeprom;
2557 ftdi_error_return(-1, "No struct ftdi_context");
2559 if (ftdi->eeprom == NULL)
2560 ftdi_error_return(-2,"No struct ftdi_eeprom");
2562 eeprom = ftdi->eeprom;
2564 if (ftdi->usb_dev == NULL)
2565 ftdi_error_return(-3, "No connected device or device not yet opened");
2569 if (eeprom->manufacturer)
2570 free (eeprom->manufacturer);
2571 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2572 if (eeprom->manufacturer)
2573 strcpy(eeprom->manufacturer, manufacturer);
2578 if (eeprom->product)
2579 free (eeprom->product);
2580 eeprom->product = (char *)malloc(strlen(product)+1);
2581 if (eeprom->product)
2582 strcpy(eeprom->product, product);
2588 free (eeprom->serial);
2589 eeprom->serial = (char *)malloc(strlen(serial)+1);
2592 strcpy(eeprom->serial, serial);
2593 eeprom->use_serial = 1;
2600 Return device ID strings from the eeprom. Device needs to be connected.
2602 The parameters manufacturer, description and serial may be NULL
2603 or pointer to buffers to store the fetched strings.
2605 \param ftdi pointer to ftdi_context
2606 \param manufacturer Store manufacturer string here if not NULL
2607 \param mnf_len Buffer size of manufacturer string
2608 \param product Store product description string here if not NULL
2609 \param prod_len Buffer size of product description string
2610 \param serial Store serial string here if not NULL
2611 \param serial_len Buffer size of serial string
2614 \retval -1: ftdi context invalid
2615 \retval -2: ftdi eeprom buffer invalid
2616 \retval -3: no USB device connected
2618 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
2619 char *manufacturer, int mnf_len,
2620 char *product, int prod_len,
2621 char *serial, int serial_len)
2623 struct ftdi_eeprom *eeprom;
2626 ftdi_error_return(-1, "No struct ftdi_context");
2628 if (ftdi->eeprom == NULL)
2629 ftdi_error_return(-2,"No struct ftdi_eeprom");
2631 eeprom = ftdi->eeprom;
2633 if (ftdi->usb_dev == NULL)
2634 ftdi_error_return(-3, "No connected device or device not yet opened");
2638 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
2640 manufacturer[mnf_len - 1] = '\0';
2645 strncpy(product, eeprom->product, prod_len);
2647 product[prod_len - 1] = '\0';
2652 strncpy(serial, eeprom->serial, serial_len);
2654 serial[serial_len - 1] = '\0';
2660 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2661 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2666 int mode_low, mode_high;
2667 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2668 mode_low = CBUSH_TRISTATE;
2670 mode_low = eeprom->cbus_function[2*i];
2671 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2672 mode_high = CBUSH_TRISTATE;
2674 mode_high = eeprom->cbus_function[2*i+1];
2676 output[0x18+i] = (mode_high <<4) | mode_low;
2679 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2682 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2691 case CHANNEL_IS_UART: return 0;
2692 case CHANNEL_IS_FIFO: return 0x01;
2693 case CHANNEL_IS_OPTO: return 0x02;
2694 case CHANNEL_IS_CPU : return 0x04;
2702 case CHANNEL_IS_UART : return 0;
2703 case CHANNEL_IS_FIFO : return 0x01;
2704 case CHANNEL_IS_OPTO : return 0x02;
2705 case CHANNEL_IS_CPU : return 0x04;
2706 case CHANNEL_IS_FT1284 : return 0x08;
2714 case CHANNEL_IS_UART : return 0;
2715 case CHANNEL_IS_FIFO : return 0x01;
2719 case TYPE_230X: /* FT230X is only UART */
2726 Build binary buffer from ftdi_eeprom structure.
2727 Output is suitable for ftdi_write_eeprom().
2729 \param ftdi pointer to ftdi_context
2731 \retval >=0: size of eeprom user area in bytes
2732 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2733 \retval -2: Invalid eeprom or ftdi pointer
2734 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2735 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2736 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2737 \retval -6: No connected EEPROM or EEPROM Type unknown
2739 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2741 unsigned char i, j, eeprom_size_mask;
2742 unsigned short checksum, value;
2743 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2744 int user_area_size, free_start, free_end;
2745 struct ftdi_eeprom *eeprom;
2746 unsigned char * output;
2749 ftdi_error_return(-2,"No context");
2750 if (ftdi->eeprom == NULL)
2751 ftdi_error_return(-2,"No eeprom structure");
2753 eeprom= ftdi->eeprom;
2754 output = eeprom->buf;
2756 if (eeprom->chip == -1)
2757 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2759 if (eeprom->size == -1)
2761 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2762 eeprom->size = 0x100;
2764 eeprom->size = 0x80;
2767 if (eeprom->manufacturer != NULL)
2768 manufacturer_size = strlen(eeprom->manufacturer);
2769 if (eeprom->product != NULL)
2770 product_size = strlen(eeprom->product);
2771 if (eeprom->serial != NULL)
2772 serial_size = strlen(eeprom->serial);
2774 // eeprom size check
2780 user_area_size = 96; // base size for strings (total of 48 characters)
2783 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2786 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2788 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2790 user_area_size = 86;
2793 user_area_size = 80;
2799 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2801 if (user_area_size < 0)
2802 ftdi_error_return(-1,"eeprom size exceeded");
2805 if (ftdi->type == TYPE_230X)
2807 /* FT230X have a reserved section in the middle of the MTP,
2808 which cannot be written to, but must be included in the checksum */
2809 memset(ftdi->eeprom->buf, 0, 0x80);
2810 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2814 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2817 // Bytes and Bits set for all Types
2819 // Addr 02: Vendor ID
2820 output[0x02] = eeprom->vendor_id;
2821 output[0x03] = eeprom->vendor_id >> 8;
2823 // Addr 04: Product ID
2824 output[0x04] = eeprom->product_id;
2825 output[0x05] = eeprom->product_id >> 8;
2827 // Addr 06: Device release number (0400h for BM features)
2828 output[0x06] = eeprom->release_number;
2829 output[0x07] = eeprom->release_number >> 8;
2831 // Addr 08: Config descriptor
2833 // Bit 6: 1 if this device is self powered, 0 if bus powered
2834 // Bit 5: 1 if this device uses remote wakeup
2835 // Bit 4-0: reserved - 0
2837 if (eeprom->self_powered)
2839 if (eeprom->remote_wakeup)
2843 // Addr 09: Max power consumption: max power = value * 2 mA
2844 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2846 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2848 // Addr 0A: Chip configuration
2849 // Bit 7: 0 - reserved
2850 // Bit 6: 0 - reserved
2851 // Bit 5: 0 - reserved
2852 // Bit 4: 1 - Change USB version
2853 // Bit 3: 1 - Use the serial number string
2854 // Bit 2: 1 - Enable suspend pull downs for lower power
2855 // Bit 1: 1 - Out EndPoint is Isochronous
2856 // Bit 0: 1 - In EndPoint is Isochronous
2859 if (eeprom->in_is_isochronous)
2861 if (eeprom->out_is_isochronous)
2867 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2868 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2889 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2890 eeprom_size_mask = eeprom->size -1;
2891 free_end = i & eeprom_size_mask;
2893 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2894 // Addr 0F: Length of manufacturer string
2895 // Output manufacturer
2896 output[0x0E] = i; // calculate offset
2897 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2898 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2899 for (j = 0; j < manufacturer_size; j++)
2901 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2902 output[i & eeprom_size_mask] = 0x00, i++;
2904 output[0x0F] = manufacturer_size*2 + 2;
2906 // Addr 10: Offset of the product string + 0x80, calculated later
2907 // Addr 11: Length of product string
2908 output[0x10] = i | 0x80; // calculate offset
2909 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2910 output[i & eeprom_size_mask] = 0x03, i++;
2911 for (j = 0; j < product_size; j++)
2913 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2914 output[i & eeprom_size_mask] = 0x00, i++;
2916 output[0x11] = product_size*2 + 2;
2918 // Addr 12: Offset of the serial string + 0x80, calculated later
2919 // Addr 13: Length of serial string
2920 output[0x12] = i | 0x80; // calculate offset
2921 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2922 output[i & eeprom_size_mask] = 0x03, i++;
2923 for (j = 0; j < serial_size; j++)
2925 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2926 output[i & eeprom_size_mask] = 0x00, i++;
2929 // Legacy port name and PnP fields for FT2232 and newer chips
2930 if (ftdi->type > TYPE_BM)
2932 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2934 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2936 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2940 output[0x13] = serial_size*2 + 2;
2942 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2944 if (eeprom->use_serial)
2945 output[0x0A] |= USE_SERIAL_NUM;
2947 output[0x0A] &= ~USE_SERIAL_NUM;
2950 /* Bytes and Bits specific to (some) types
2951 Write linear, as this allows easier fixing*/
2957 output[0x0C] = eeprom->usb_version & 0xff;
2958 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2959 if (eeprom->use_usb_version)
2960 output[0x0A] |= USE_USB_VERSION_BIT;
2962 output[0x0A] &= ~USE_USB_VERSION_BIT;
2967 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
2968 if ( eeprom->channel_a_driver == DRIVER_VCP)
2969 output[0x00] |= DRIVER_VCP;
2971 output[0x00] &= ~DRIVER_VCP;
2973 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2974 output[0x00] |= HIGH_CURRENT_DRIVE;
2976 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2978 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
2979 if ( eeprom->channel_b_driver == DRIVER_VCP)
2980 output[0x01] |= DRIVER_VCP;
2982 output[0x01] &= ~DRIVER_VCP;
2984 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2985 output[0x01] |= HIGH_CURRENT_DRIVE;
2987 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2989 if (eeprom->in_is_isochronous)
2990 output[0x0A] |= 0x1;
2992 output[0x0A] &= ~0x1;
2993 if (eeprom->out_is_isochronous)
2994 output[0x0A] |= 0x2;
2996 output[0x0A] &= ~0x2;
2997 if (eeprom->suspend_pull_downs)
2998 output[0x0A] |= 0x4;
3000 output[0x0A] &= ~0x4;
3001 if (eeprom->use_usb_version)
3002 output[0x0A] |= USE_USB_VERSION_BIT;
3004 output[0x0A] &= ~USE_USB_VERSION_BIT;
3006 output[0x0C] = eeprom->usb_version & 0xff;
3007 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3008 output[0x14] = eeprom->chip;
3011 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
3012 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
3013 output[0x00] |= HIGH_CURRENT_DRIVE_R;
3014 if (eeprom->external_oscillator)
3015 output[0x00] |= 0x02;
3016 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
3018 if (eeprom->suspend_pull_downs)
3019 output[0x0A] |= 0x4;
3021 output[0x0A] &= ~0x4;
3022 output[0x0B] = eeprom->invert;
3023 output[0x0C] = eeprom->usb_version & 0xff;
3024 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3026 if (eeprom->cbus_function[0] > CBUS_BB_RD)
3027 output[0x14] = CBUS_TXLED;
3029 output[0x14] = eeprom->cbus_function[0];
3031 if (eeprom->cbus_function[1] > CBUS_BB_RD)
3032 output[0x14] |= CBUS_RXLED<<4;
3034 output[0x14] |= eeprom->cbus_function[1]<<4;
3036 if (eeprom->cbus_function[2] > CBUS_BB_RD)
3037 output[0x15] = CBUS_TXDEN;
3039 output[0x15] = eeprom->cbus_function[2];
3041 if (eeprom->cbus_function[3] > CBUS_BB_RD)
3042 output[0x15] |= CBUS_PWREN<<4;
3044 output[0x15] |= eeprom->cbus_function[3]<<4;
3046 if (eeprom->cbus_function[4] > CBUS_CLK6)
3047 output[0x16] = CBUS_SLEEP;
3049 output[0x16] = eeprom->cbus_function[4];
3052 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
3053 if ( eeprom->channel_a_driver == DRIVER_VCP)
3054 output[0x00] |= DRIVER_VCP;
3056 output[0x00] &= ~DRIVER_VCP;
3058 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
3059 if ( eeprom->channel_b_driver == DRIVER_VCP)
3060 output[0x01] |= DRIVER_VCP;
3062 output[0x01] &= ~DRIVER_VCP;
3063 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
3064 output[0x01] |= SUSPEND_DBUS7_BIT;
3066 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3068 if (eeprom->suspend_pull_downs)
3069 output[0x0A] |= 0x4;
3071 output[0x0A] &= ~0x4;
3073 if (eeprom->group0_drive > DRIVE_16MA)
3074 output[0x0c] |= DRIVE_16MA;
3076 output[0x0c] |= eeprom->group0_drive;
3077 if (eeprom->group0_schmitt == IS_SCHMITT)
3078 output[0x0c] |= IS_SCHMITT;
3079 if (eeprom->group0_slew == SLOW_SLEW)
3080 output[0x0c] |= SLOW_SLEW;
3082 if (eeprom->group1_drive > DRIVE_16MA)
3083 output[0x0c] |= DRIVE_16MA<<4;
3085 output[0x0c] |= eeprom->group1_drive<<4;
3086 if (eeprom->group1_schmitt == IS_SCHMITT)
3087 output[0x0c] |= IS_SCHMITT<<4;
3088 if (eeprom->group1_slew == SLOW_SLEW)
3089 output[0x0c] |= SLOW_SLEW<<4;
3091 if (eeprom->group2_drive > DRIVE_16MA)
3092 output[0x0d] |= DRIVE_16MA;
3094 output[0x0d] |= eeprom->group2_drive;
3095 if (eeprom->group2_schmitt == IS_SCHMITT)
3096 output[0x0d] |= IS_SCHMITT;
3097 if (eeprom->group2_slew == SLOW_SLEW)
3098 output[0x0d] |= SLOW_SLEW;
3100 if (eeprom->group3_drive > DRIVE_16MA)
3101 output[0x0d] |= DRIVE_16MA<<4;
3103 output[0x0d] |= eeprom->group3_drive<<4;
3104 if (eeprom->group3_schmitt == IS_SCHMITT)
3105 output[0x0d] |= IS_SCHMITT<<4;
3106 if (eeprom->group3_slew == SLOW_SLEW)
3107 output[0x0d] |= SLOW_SLEW<<4;
3109 output[0x18] = eeprom->chip;
3113 if (eeprom->channel_a_driver == DRIVER_VCP)
3114 output[0x00] |= DRIVER_VCP;
3116 output[0x00] &= ~DRIVER_VCP;
3117 if (eeprom->channel_b_driver == DRIVER_VCP)
3118 output[0x01] |= DRIVER_VCP;
3120 output[0x01] &= ~DRIVER_VCP;
3121 if (eeprom->channel_c_driver == DRIVER_VCP)
3122 output[0x00] |= (DRIVER_VCP << 4);
3124 output[0x00] &= ~(DRIVER_VCP << 4);
3125 if (eeprom->channel_d_driver == DRIVER_VCP)
3126 output[0x01] |= (DRIVER_VCP << 4);
3128 output[0x01] &= ~(DRIVER_VCP << 4);
3130 if (eeprom->suspend_pull_downs)
3131 output[0x0a] |= 0x4;
3133 output[0x0a] &= ~0x4;
3135 if (eeprom->channel_a_rs485enable)
3136 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3138 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3139 if (eeprom->channel_b_rs485enable)
3140 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3142 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3143 if (eeprom->channel_c_rs485enable)
3144 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3146 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3147 if (eeprom->channel_d_rs485enable)
3148 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3150 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3152 if (eeprom->group0_drive > DRIVE_16MA)
3153 output[0x0c] |= DRIVE_16MA;
3155 output[0x0c] |= eeprom->group0_drive;
3156 if (eeprom->group0_schmitt == IS_SCHMITT)
3157 output[0x0c] |= IS_SCHMITT;
3158 if (eeprom->group0_slew == SLOW_SLEW)
3159 output[0x0c] |= SLOW_SLEW;
3161 if (eeprom->group1_drive > DRIVE_16MA)
3162 output[0x0c] |= DRIVE_16MA<<4;
3164 output[0x0c] |= eeprom->group1_drive<<4;
3165 if (eeprom->group1_schmitt == IS_SCHMITT)
3166 output[0x0c] |= IS_SCHMITT<<4;
3167 if (eeprom->group1_slew == SLOW_SLEW)
3168 output[0x0c] |= SLOW_SLEW<<4;
3170 if (eeprom->group2_drive > DRIVE_16MA)
3171 output[0x0d] |= DRIVE_16MA;
3173 output[0x0d] |= eeprom->group2_drive;
3174 if (eeprom->group2_schmitt == IS_SCHMITT)
3175 output[0x0d] |= IS_SCHMITT;
3176 if (eeprom->group2_slew == SLOW_SLEW)
3177 output[0x0d] |= SLOW_SLEW;
3179 if (eeprom->group3_drive > DRIVE_16MA)
3180 output[0x0d] |= DRIVE_16MA<<4;
3182 output[0x0d] |= eeprom->group3_drive<<4;
3183 if (eeprom->group3_schmitt == IS_SCHMITT)
3184 output[0x0d] |= IS_SCHMITT<<4;
3185 if (eeprom->group3_slew == SLOW_SLEW)
3186 output[0x0d] |= SLOW_SLEW<<4;
3188 output[0x18] = eeprom->chip;
3192 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3193 if ( eeprom->channel_a_driver == DRIVER_VCP)
3194 output[0x00] |= DRIVER_VCPH;
3196 output[0x00] &= ~DRIVER_VCPH;
3197 if (eeprom->powersave)
3198 output[0x01] |= POWER_SAVE_DISABLE_H;
3200 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3202 if (eeprom->suspend_pull_downs)
3203 output[0x0a] |= 0x4;
3205 output[0x0a] &= ~0x4;
3207 if (eeprom->clock_polarity)
3208 output[0x01] |= FT1284_CLK_IDLE_STATE;
3210 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3211 if (eeprom->data_order)
3212 output[0x01] |= FT1284_DATA_LSB;
3214 output[0x01] &= ~FT1284_DATA_LSB;
3215 if (eeprom->flow_control)
3216 output[0x01] |= FT1284_FLOW_CONTROL;
3218 output[0x01] &= ~FT1284_FLOW_CONTROL;
3219 if (eeprom->group0_drive > DRIVE_16MA)
3220 output[0x0c] |= DRIVE_16MA;
3222 output[0x0c] |= eeprom->group0_drive;
3223 if (eeprom->group0_schmitt == IS_SCHMITT)
3224 output[0x0c] |= IS_SCHMITT;
3225 if (eeprom->group0_slew == SLOW_SLEW)
3226 output[0x0c] |= SLOW_SLEW;
3228 if (eeprom->group1_drive > DRIVE_16MA)
3229 output[0x0d] |= DRIVE_16MA;
3231 output[0x0d] |= eeprom->group1_drive;
3232 if (eeprom->group1_schmitt == IS_SCHMITT)
3233 output[0x0d] |= IS_SCHMITT;
3234 if (eeprom->group1_slew == SLOW_SLEW)
3235 output[0x0d] |= SLOW_SLEW;
3237 set_ft232h_cbus(eeprom, output);
3239 output[0x1e] = eeprom->chip;
3240 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3243 output[0x00] = 0x80; /* Actually, leave the default value */
3244 /*FIXME: Make DBUS & CBUS Control configurable*/
3245 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3246 for (j = 0; j <= 6; j++)
3248 output[0x1a + j] = eeprom->cbus_function[j];
3250 output[0x0b] = eeprom->invert;
3254 /* First address without use */
3274 /* Arbitrary user data */
3275 if (eeprom->user_data && eeprom->user_data_size >= 0)
3277 if (eeprom->user_data_addr < free_start)
3278 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3279 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3280 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3281 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3282 ftdi_error_return(-1,"eeprom size exceeded");
3283 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3286 // calculate checksum
3289 for (i = 0; i < eeprom->size/2-1; i++)
3291 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3293 /* FT230X has a user section in the MTP which is not part of the checksum */
3296 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3298 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3299 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3305 value = output[i*2];
3306 value += output[(i*2)+1] << 8;
3308 checksum = value^checksum;
3309 checksum = (checksum << 1) | (checksum >> 15);
3312 output[eeprom->size-2] = checksum;
3313 output[eeprom->size-1] = checksum >> 8;
3315 eeprom->initialized_for_connected_device = 1;
3316 return user_area_size;
3318 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3321 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3323 static unsigned char bit2type(unsigned char bits)
3327 case 0: return CHANNEL_IS_UART;
3328 case 1: return CHANNEL_IS_FIFO;
3329 case 2: return CHANNEL_IS_OPTO;
3330 case 4: return CHANNEL_IS_CPU;
3331 case 8: return CHANNEL_IS_FT1284;
3333 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3338 /* Decode 230X / 232R type chips invert bits
3339 * Prints directly to stdout.
3341 static void print_inverted_bits(int invert)
3343 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3346 fprintf(stdout,"Inverted bits:");
3348 if ((invert & (1<<i)) == (1<<i))
3349 fprintf(stdout," %s",r_bits[i]);
3351 fprintf(stdout,"\n");
3354 Decode binary EEPROM image into an ftdi_eeprom structure.
3356 For FT-X devices use AN_201 FT-X MTP memory Configuration to decode.
3358 \param ftdi pointer to ftdi_context
3359 \param verbose Decode EEPROM on stdout
3362 \retval -1: something went wrong
3364 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3365 FIXME: Strings are malloc'ed here and should be freed somewhere
3367 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3370 unsigned short checksum, eeprom_checksum, value;
3371 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3373 struct ftdi_eeprom *eeprom;
3374 unsigned char *buf = NULL;
3377 ftdi_error_return(-1,"No context");
3378 if (ftdi->eeprom == NULL)
3379 ftdi_error_return(-1,"No eeprom structure");
3381 eeprom = ftdi->eeprom;
3382 eeprom_size = eeprom->size;
3383 buf = ftdi->eeprom->buf;
3385 // Addr 02: Vendor ID
3386 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3388 // Addr 04: Product ID
3389 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3391 // Addr 06: Device release number
3392 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3394 // Addr 08: Config descriptor
3396 // Bit 6: 1 if this device is self powered, 0 if bus powered
3397 // Bit 5: 1 if this device uses remote wakeup
3398 eeprom->self_powered = buf[0x08] & 0x40;
3399 eeprom->remote_wakeup = buf[0x08] & 0x20;
3401 // Addr 09: Max power consumption: max power = value * 2 mA
3402 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3404 // Addr 0A: Chip configuration
3405 // Bit 7: 0 - reserved
3406 // Bit 6: 0 - reserved
3407 // Bit 5: 0 - reserved
3408 // Bit 4: 1 - Change USB version on BM and 2232C
3409 // Bit 3: 1 - Use the serial number string
3410 // Bit 2: 1 - Enable suspend pull downs for lower power
3411 // Bit 1: 1 - Out EndPoint is Isochronous
3412 // Bit 0: 1 - In EndPoint is Isochronous
3414 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3415 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3416 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3417 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3418 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3420 // Addr 0C: USB version low byte when 0x0A
3421 // Addr 0D: USB version high byte when 0x0A
3422 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3424 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3425 // Addr 0F: Length of manufacturer string
3426 manufacturer_size = buf[0x0F]/2;
3427 if (eeprom->manufacturer)
3428 free(eeprom->manufacturer);
3429 if (manufacturer_size > 0)
3431 eeprom->manufacturer = (char *)malloc(manufacturer_size);
3432 if (eeprom->manufacturer)
3434 // Decode manufacturer
3435 i = buf[0x0E] & (eeprom_size -1); // offset
3436 for (j=0; j<manufacturer_size-1; j++)
3438 eeprom->manufacturer[j] = buf[2*j+i+2];
3440 eeprom->manufacturer[j] = '\0';
3443 else eeprom->manufacturer = NULL;
3445 // Addr 10: Offset of the product string + 0x80, calculated later
3446 // Addr 11: Length of product string
3447 if (eeprom->product)
3448 free(eeprom->product);
3449 product_size = buf[0x11]/2;
3450 if (product_size > 0)
3452 eeprom->product = (char *)malloc(product_size);
3453 if (eeprom->product)
3455 // Decode product name
3456 i = buf[0x10] & (eeprom_size -1); // offset
3457 for (j=0; j<product_size-1; j++)
3459 eeprom->product[j] = buf[2*j+i+2];
3461 eeprom->product[j] = '\0';
3464 else eeprom->product = NULL;
3466 // Addr 12: Offset of the serial string + 0x80, calculated later
3467 // Addr 13: Length of serial string
3469 free(eeprom->serial);
3470 serial_size = buf[0x13]/2;
3471 if (serial_size > 0)
3473 eeprom->serial = (char *)malloc(serial_size);
3477 i = buf[0x12] & (eeprom_size -1); // offset
3478 for (j=0; j<serial_size-1; j++)
3480 eeprom->serial[j] = buf[2*j+i+2];
3482 eeprom->serial[j] = '\0';
3485 else eeprom->serial = NULL;
3490 for (i = 0; i < eeprom_size/2-1; i++)
3492 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3494 /* FT230X has a user section in the MTP which is not part of the checksum */
3498 value += buf[(i*2)+1] << 8;
3500 checksum = value^checksum;
3501 checksum = (checksum << 1) | (checksum >> 15);
3504 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3506 if (eeprom_checksum != checksum)
3508 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3509 ftdi_error_return(-1,"EEPROM checksum error");
3512 eeprom->channel_a_type = 0;
3513 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3517 else if (ftdi->type == TYPE_2232C)
3519 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3520 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3521 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3522 eeprom->channel_b_type = buf[0x01] & 0x7;
3523 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3524 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3525 eeprom->chip = buf[0x14];
3527 else if (ftdi->type == TYPE_R)
3529 /* TYPE_R flags D2XX, not VCP as all others*/
3530 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3531 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3532 eeprom->external_oscillator = buf[0x00] & 0x02;
3533 if ( (buf[0x01]&0x40) != 0x40)
3535 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3536 " If this happened with the\n"
3537 " EEPROM programmed by FTDI tools, please report "
3538 "to libftdi@developer.intra2net.com\n");
3540 eeprom->chip = buf[0x16];
3541 // Addr 0B: Invert data lines
3542 // Works only on FT232R, not FT245R, but no way to distinguish
3543 eeprom->invert = buf[0x0B];
3544 // Addr 14: CBUS function: CBUS0, CBUS1
3545 // Addr 15: CBUS function: CBUS2, CBUS3
3546 // Addr 16: CBUS function: CBUS5
3547 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3548 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3549 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3550 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3551 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3553 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3555 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3556 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3558 if (ftdi->type == TYPE_2232H)
3560 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3561 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3562 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3566 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3567 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3568 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3569 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3570 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3571 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3574 eeprom->chip = buf[0x18];
3575 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3576 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3577 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3578 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3579 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3580 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3581 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3582 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3583 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3584 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3585 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3586 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3588 else if (ftdi->type == TYPE_232H)
3590 eeprom->channel_a_type = buf[0x00] & 0xf;
3591 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3592 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3593 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3594 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3595 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3596 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3597 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3598 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3599 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3600 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3601 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3605 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3606 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3608 eeprom->chip = buf[0x1e];
3609 /*FIXME: Decipher more values*/
3611 else if (ftdi->type == TYPE_230X)
3615 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3617 eeprom->group0_drive = buf[0x0c] & 0x03;
3618 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3619 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3620 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3621 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3622 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3624 eeprom->invert = buf[0xb];
3629 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3630 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3631 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3632 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3634 if (eeprom->self_powered)
3635 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3637 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3638 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3639 if (eeprom->manufacturer)
3640 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3641 if (eeprom->product)
3642 fprintf(stdout, "Product: %s\n",eeprom->product);
3644 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3645 fprintf(stdout, "Checksum : %04x\n", checksum);
3646 if (ftdi->type == TYPE_R) {
3647 fprintf(stdout, "Internal EEPROM\n");
3648 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3650 else if (eeprom->chip >= 0x46)
3651 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3652 if (eeprom->suspend_dbus7)
3653 fprintf(stdout, "Suspend on DBUS7\n");
3654 if (eeprom->suspend_pull_downs)
3655 fprintf(stdout, "Pull IO pins low during suspend\n");
3656 if(eeprom->powersave)
3658 if(ftdi->type >= TYPE_232H)
3659 fprintf(stdout,"Enter low power state on ACBUS7\n");
3661 if (eeprom->remote_wakeup)
3662 fprintf(stdout, "Enable Remote Wake Up\n");
3663 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3664 if (ftdi->type >= TYPE_2232C)
3665 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3666 channel_mode[eeprom->channel_a_type],
3667 (eeprom->channel_a_driver)?" VCP":"",
3668 (eeprom->high_current_a)?" High Current IO":"");
3669 if (ftdi->type == TYPE_232H)
3671 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3672 (eeprom->clock_polarity)?"HIGH":"LOW",
3673 (eeprom->data_order)?"LSB":"MSB",
3674 (eeprom->flow_control)?"":"No ");
3676 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3677 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3678 channel_mode[eeprom->channel_b_type],
3679 (eeprom->channel_b_driver)?" VCP":"",
3680 (eeprom->high_current_b)?" High Current IO":"");
3681 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3682 eeprom->use_usb_version)
3683 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3685 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3687 fprintf(stdout,"%s has %d mA drive%s%s\n",
3688 (ftdi->type == TYPE_2232H)?"AL":"A",
3689 (eeprom->group0_drive+1) *4,
3690 (eeprom->group0_schmitt)?" Schmitt Input":"",
3691 (eeprom->group0_slew)?" Slow Slew":"");
3692 fprintf(stdout,"%s has %d mA drive%s%s\n",
3693 (ftdi->type == TYPE_2232H)?"AH":"B",
3694 (eeprom->group1_drive+1) *4,
3695 (eeprom->group1_schmitt)?" Schmitt Input":"",
3696 (eeprom->group1_slew)?" Slow Slew":"");
3697 fprintf(stdout,"%s has %d mA drive%s%s\n",
3698 (ftdi->type == TYPE_2232H)?"BL":"C",
3699 (eeprom->group2_drive+1) *4,
3700 (eeprom->group2_schmitt)?" Schmitt Input":"",
3701 (eeprom->group2_slew)?" Slow Slew":"");
3702 fprintf(stdout,"%s has %d mA drive%s%s\n",
3703 (ftdi->type == TYPE_2232H)?"BH":"D",
3704 (eeprom->group3_drive+1) *4,
3705 (eeprom->group3_schmitt)?" Schmitt Input":"",
3706 (eeprom->group3_slew)?" Slow Slew":"");
3708 else if (ftdi->type == TYPE_232H)
3710 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3711 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3712 "CLK30","CLK15","CLK7_5"
3714 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3715 (eeprom->group0_drive+1) *4,
3716 (eeprom->group0_schmitt)?" Schmitt Input":"",
3717 (eeprom->group0_slew)?" Slow Slew":"");
3718 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3719 (eeprom->group1_drive+1) *4,
3720 (eeprom->group1_schmitt)?" Schmitt Input":"",
3721 (eeprom->group1_slew)?" Slow Slew":"");
3722 for (i=0; i<10; i++)
3724 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3725 fprintf(stdout,"C%d Function: %s\n", i,
3726 cbush_mux[eeprom->cbus_function[i]]);
3729 else if (ftdi->type == TYPE_230X)
3731 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3732 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3733 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3734 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3735 "BBRD#", "TIME_STAMP", "AWAKE#",
3737 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3738 (eeprom->group0_drive+1) *4,
3739 (eeprom->group0_schmitt)?" Schmitt Input":"",
3740 (eeprom->group0_slew)?" Slow Slew":"");
3741 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3742 (eeprom->group1_drive+1) *4,
3743 (eeprom->group1_schmitt)?" Schmitt Input":"",
3744 (eeprom->group1_slew)?" Slow Slew":"");
3747 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3748 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3752 print_inverted_bits(eeprom->invert);
3755 if (ftdi->type == TYPE_R)
3757 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3758 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3759 "IOMODE","BB_WR","BB_RD"
3761 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3764 print_inverted_bits(eeprom->invert);
3768 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3769 fprintf(stdout,"C%d Function: %s\n", i,
3770 cbus_mux[eeprom->cbus_function[i]]);
3774 /* Running MPROG show that C0..3 have fixed function Synchronous
3776 fprintf(stdout,"C%d BB Function: %s\n", i,
3779 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3788 Get a value from the decoded EEPROM structure
3790 \param ftdi pointer to ftdi_context
3791 \param value_name Enum of the value to query
3792 \param value Pointer to store read value
3795 \retval -1: Value doesn't exist
3797 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3802 *value = ftdi->eeprom->vendor_id;
3805 *value = ftdi->eeprom->product_id;
3807 case RELEASE_NUMBER:
3808 *value = ftdi->eeprom->release_number;
3811 *value = ftdi->eeprom->self_powered;
3814 *value = ftdi->eeprom->remote_wakeup;
3817 *value = ftdi->eeprom->is_not_pnp;
3820 *value = ftdi->eeprom->suspend_dbus7;
3822 case IN_IS_ISOCHRONOUS:
3823 *value = ftdi->eeprom->in_is_isochronous;
3825 case OUT_IS_ISOCHRONOUS:
3826 *value = ftdi->eeprom->out_is_isochronous;
3828 case SUSPEND_PULL_DOWNS:
3829 *value = ftdi->eeprom->suspend_pull_downs;
3832 *value = ftdi->eeprom->use_serial;
3835 *value = ftdi->eeprom->usb_version;
3837 case USE_USB_VERSION:
3838 *value = ftdi->eeprom->use_usb_version;
3841 *value = ftdi->eeprom->max_power;
3843 case CHANNEL_A_TYPE:
3844 *value = ftdi->eeprom->channel_a_type;
3846 case CHANNEL_B_TYPE:
3847 *value = ftdi->eeprom->channel_b_type;
3849 case CHANNEL_A_DRIVER:
3850 *value = ftdi->eeprom->channel_a_driver;
3852 case CHANNEL_B_DRIVER:
3853 *value = ftdi->eeprom->channel_b_driver;
3855 case CHANNEL_C_DRIVER:
3856 *value = ftdi->eeprom->channel_c_driver;
3858 case CHANNEL_D_DRIVER:
3859 *value = ftdi->eeprom->channel_d_driver;
3861 case CHANNEL_A_RS485:
3862 *value = ftdi->eeprom->channel_a_rs485enable;
3864 case CHANNEL_B_RS485:
3865 *value = ftdi->eeprom->channel_b_rs485enable;
3867 case CHANNEL_C_RS485:
3868 *value = ftdi->eeprom->channel_c_rs485enable;
3870 case CHANNEL_D_RS485:
3871 *value = ftdi->eeprom->channel_d_rs485enable;
3873 case CBUS_FUNCTION_0:
3874 *value = ftdi->eeprom->cbus_function[0];
3876 case CBUS_FUNCTION_1:
3877 *value = ftdi->eeprom->cbus_function[1];
3879 case CBUS_FUNCTION_2:
3880 *value = ftdi->eeprom->cbus_function[2];
3882 case CBUS_FUNCTION_3:
3883 *value = ftdi->eeprom->cbus_function[3];
3885 case CBUS_FUNCTION_4:
3886 *value = ftdi->eeprom->cbus_function[4];
3888 case CBUS_FUNCTION_5:
3889 *value = ftdi->eeprom->cbus_function[5];
3891 case CBUS_FUNCTION_6:
3892 *value = ftdi->eeprom->cbus_function[6];
3894 case CBUS_FUNCTION_7:
3895 *value = ftdi->eeprom->cbus_function[7];
3897 case CBUS_FUNCTION_8:
3898 *value = ftdi->eeprom->cbus_function[8];
3900 case CBUS_FUNCTION_9:
3901 *value = ftdi->eeprom->cbus_function[9];
3904 *value = ftdi->eeprom->high_current;
3906 case HIGH_CURRENT_A:
3907 *value = ftdi->eeprom->high_current_a;
3909 case HIGH_CURRENT_B:
3910 *value = ftdi->eeprom->high_current_b;
3913 *value = ftdi->eeprom->invert;
3916 *value = ftdi->eeprom->group0_drive;
3918 case GROUP0_SCHMITT:
3919 *value = ftdi->eeprom->group0_schmitt;
3922 *value = ftdi->eeprom->group0_slew;
3925 *value = ftdi->eeprom->group1_drive;
3927 case GROUP1_SCHMITT:
3928 *value = ftdi->eeprom->group1_schmitt;
3931 *value = ftdi->eeprom->group1_slew;
3934 *value = ftdi->eeprom->group2_drive;
3936 case GROUP2_SCHMITT:
3937 *value = ftdi->eeprom->group2_schmitt;
3940 *value = ftdi->eeprom->group2_slew;
3943 *value = ftdi->eeprom->group3_drive;
3945 case GROUP3_SCHMITT:
3946 *value = ftdi->eeprom->group3_schmitt;
3949 *value = ftdi->eeprom->group3_slew;
3952 *value = ftdi->eeprom->powersave;
3954 case CLOCK_POLARITY:
3955 *value = ftdi->eeprom->clock_polarity;
3958 *value = ftdi->eeprom->data_order;
3961 *value = ftdi->eeprom->flow_control;
3964 *value = ftdi->eeprom->chip;
3967 *value = ftdi->eeprom->size;
3969 case EXTERNAL_OSCILLATOR:
3970 *value = ftdi->eeprom->external_oscillator;
3973 ftdi_error_return(-1, "Request for unknown EEPROM value");
3979 Set a value in the decoded EEPROM Structure
3980 No parameter checking is performed
3982 \param ftdi pointer to ftdi_context
3983 \param value_name Enum of the value to set
3987 \retval -1: Value doesn't exist
3988 \retval -2: Value not user settable
3990 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3995 ftdi->eeprom->vendor_id = value;
3998 ftdi->eeprom->product_id = value;
4000 case RELEASE_NUMBER:
4001 ftdi->eeprom->release_number = value;
4004 ftdi->eeprom->self_powered = value;
4007 ftdi->eeprom->remote_wakeup = value;
4010 ftdi->eeprom->is_not_pnp = value;
4013 ftdi->eeprom->suspend_dbus7 = value;
4015 case IN_IS_ISOCHRONOUS:
4016 ftdi->eeprom->in_is_isochronous = value;
4018 case OUT_IS_ISOCHRONOUS:
4019 ftdi->eeprom->out_is_isochronous = value;
4021 case SUSPEND_PULL_DOWNS:
4022 ftdi->eeprom->suspend_pull_downs = value;
4025 ftdi->eeprom->use_serial = value;
4028 ftdi->eeprom->usb_version = value;
4030 case USE_USB_VERSION:
4031 ftdi->eeprom->use_usb_version = value;
4034 ftdi->eeprom->max_power = value;
4036 case CHANNEL_A_TYPE:
4037 ftdi->eeprom->channel_a_type = value;
4039 case CHANNEL_B_TYPE:
4040 ftdi->eeprom->channel_b_type = value;
4042 case CHANNEL_A_DRIVER:
4043 ftdi->eeprom->channel_a_driver = value;
4045 case CHANNEL_B_DRIVER:
4046 ftdi->eeprom->channel_b_driver = value;
4048 case CHANNEL_C_DRIVER:
4049 ftdi->eeprom->channel_c_driver = value;
4051 case CHANNEL_D_DRIVER:
4052 ftdi->eeprom->channel_d_driver = value;
4054 case CHANNEL_A_RS485:
4055 ftdi->eeprom->channel_a_rs485enable = value;
4057 case CHANNEL_B_RS485:
4058 ftdi->eeprom->channel_b_rs485enable = value;
4060 case CHANNEL_C_RS485:
4061 ftdi->eeprom->channel_c_rs485enable = value;
4063 case CHANNEL_D_RS485:
4064 ftdi->eeprom->channel_d_rs485enable = value;
4066 case CBUS_FUNCTION_0:
4067 ftdi->eeprom->cbus_function[0] = value;
4069 case CBUS_FUNCTION_1:
4070 ftdi->eeprom->cbus_function[1] = value;
4072 case CBUS_FUNCTION_2:
4073 ftdi->eeprom->cbus_function[2] = value;
4075 case CBUS_FUNCTION_3:
4076 ftdi->eeprom->cbus_function[3] = value;
4078 case CBUS_FUNCTION_4:
4079 ftdi->eeprom->cbus_function[4] = value;
4081 case CBUS_FUNCTION_5:
4082 ftdi->eeprom->cbus_function[5] = value;
4084 case CBUS_FUNCTION_6:
4085 ftdi->eeprom->cbus_function[6] = value;
4087 case CBUS_FUNCTION_7:
4088 ftdi->eeprom->cbus_function[7] = value;
4090 case CBUS_FUNCTION_8:
4091 ftdi->eeprom->cbus_function[8] = value;
4093 case CBUS_FUNCTION_9:
4094 ftdi->eeprom->cbus_function[9] = value;
4097 ftdi->eeprom->high_current = value;
4099 case HIGH_CURRENT_A:
4100 ftdi->eeprom->high_current_a = value;
4102 case HIGH_CURRENT_B:
4103 ftdi->eeprom->high_current_b = value;
4106 ftdi->eeprom->invert = value;
4109 ftdi->eeprom->group0_drive = value;
4111 case GROUP0_SCHMITT:
4112 ftdi->eeprom->group0_schmitt = value;
4115 ftdi->eeprom->group0_slew = value;
4118 ftdi->eeprom->group1_drive = value;
4120 case GROUP1_SCHMITT:
4121 ftdi->eeprom->group1_schmitt = value;
4124 ftdi->eeprom->group1_slew = value;
4127 ftdi->eeprom->group2_drive = value;
4129 case GROUP2_SCHMITT:
4130 ftdi->eeprom->group2_schmitt = value;
4133 ftdi->eeprom->group2_slew = value;
4136 ftdi->eeprom->group3_drive = value;
4138 case GROUP3_SCHMITT:
4139 ftdi->eeprom->group3_schmitt = value;
4142 ftdi->eeprom->group3_slew = value;
4145 ftdi->eeprom->chip = value;
4148 ftdi->eeprom->powersave = value;
4150 case CLOCK_POLARITY:
4151 ftdi->eeprom->clock_polarity = value;
4154 ftdi->eeprom->data_order = value;
4157 ftdi->eeprom->flow_control = value;
4160 ftdi_error_return(-2, "EEPROM Value can't be changed");
4162 case EXTERNAL_OSCILLATOR:
4163 ftdi->eeprom->external_oscillator = value;
4165 case USER_DATA_ADDR:
4166 ftdi->eeprom->user_data_addr = value;
4170 ftdi_error_return(-1, "Request to unknown EEPROM value");
4172 ftdi->eeprom->initialized_for_connected_device = 0;
4176 /** Get the read-only buffer to the binary EEPROM content
4178 \param ftdi pointer to ftdi_context
4179 \param buf buffer to receive EEPROM content
4180 \param size Size of receiving buffer
4183 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
4184 \retval -2: Not enough room to store eeprom
4186 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4188 if (!ftdi || !(ftdi->eeprom))
4189 ftdi_error_return(-1, "No appropriate structure");
4191 if (!buf || size < ftdi->eeprom->size)
4192 ftdi_error_return(-1, "Not enough room to store eeprom");
4194 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4195 if (size > FTDI_MAX_EEPROM_SIZE)
4196 size = FTDI_MAX_EEPROM_SIZE;
4198 memcpy(buf, ftdi->eeprom->buf, size);
4203 /** Set the EEPROM content from the user-supplied prefilled buffer
4205 \param ftdi pointer to ftdi_context
4206 \param buf buffer to read EEPROM content
4207 \param size Size of buffer
4210 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4212 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4214 if (!ftdi || !(ftdi->eeprom) || !buf)
4215 ftdi_error_return(-1, "No appropriate structure");
4217 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4218 if (size > FTDI_MAX_EEPROM_SIZE)
4219 size = FTDI_MAX_EEPROM_SIZE;
4221 memcpy(ftdi->eeprom->buf, buf, size);
4226 /** Set the EEPROM user data content from the user-supplied prefilled buffer
4228 \param ftdi pointer to ftdi_context
4229 \param buf buffer to read EEPROM user data content
4230 \param size Size of buffer
4233 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4235 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4237 if (!ftdi || !(ftdi->eeprom) || !buf)
4238 ftdi_error_return(-1, "No appropriate structure");
4240 ftdi->eeprom->user_data_size = size;
4241 ftdi->eeprom->user_data = buf;
4246 Read eeprom location
4248 \param ftdi pointer to ftdi_context
4249 \param eeprom_addr Address of eeprom location to be read
4250 \param eeprom_val Pointer to store read eeprom location
4253 \retval -1: read failed
4254 \retval -2: USB device unavailable
4256 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4258 unsigned char buf[2];
4260 if (ftdi == NULL || ftdi->usb_dev == NULL)
4261 ftdi_error_return(-2, "USB device unavailable");
4263 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, buf, 2, ftdi->usb_read_timeout) != 2)
4264 ftdi_error_return(-1, "reading eeprom failed");
4266 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
4274 \param ftdi pointer to ftdi_context
4277 \retval -1: read failed
4278 \retval -2: USB device unavailable
4280 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4285 if (ftdi == NULL || ftdi->usb_dev == NULL)
4286 ftdi_error_return(-2, "USB device unavailable");
4287 buf = ftdi->eeprom->buf;
4289 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4291 if (libusb_control_transfer(
4292 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4293 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4294 ftdi_error_return(-1, "reading eeprom failed");
4297 if (ftdi->type == TYPE_R)
4298 ftdi->eeprom->size = 0x80;
4299 /* Guesses size of eeprom by comparing halves
4300 - will not work with blank eeprom */
4301 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4302 ftdi->eeprom->size = -1;
4303 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4304 ftdi->eeprom->size = 0x80;
4305 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4306 ftdi->eeprom->size = 0x40;
4308 ftdi->eeprom->size = 0x100;
4313 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4314 Function is only used internally
4317 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4319 return ((value & 1) << 1) |
4320 ((value & 2) << 5) |
4321 ((value & 4) >> 2) |
4322 ((value & 8) << 4) |
4323 ((value & 16) >> 1) |
4324 ((value & 32) >> 1) |
4325 ((value & 64) >> 4) |
4326 ((value & 128) >> 2);
4330 Read the FTDIChip-ID from R-type devices
4332 \param ftdi pointer to ftdi_context
4333 \param chipid Pointer to store FTDIChip-ID
4336 \retval -1: read failed
4337 \retval -2: USB device unavailable
4339 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4341 unsigned int a = 0, b = 0;
4343 if (ftdi == NULL || ftdi->usb_dev == NULL)
4344 ftdi_error_return(-2, "USB device unavailable");
4346 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2)
4348 a = a << 8 | a >> 8;
4349 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2)
4351 b = b << 8 | b >> 8;
4352 a = (a << 16) | (b & 0xFFFF);
4353 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4354 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4355 *chipid = a ^ 0xa5f0f7d1;
4360 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4364 Write eeprom location
4366 \param ftdi pointer to ftdi_context
4367 \param eeprom_addr Address of eeprom location to be written
4368 \param eeprom_val Value to be written
4371 \retval -1: write failed
4372 \retval -2: USB device unavailable
4373 \retval -3: Invalid access to checksum protected area below 0x80
4374 \retval -4: Device can't access unprotected area
4375 \retval -5: Reading chip type failed
4377 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4378 unsigned short eeprom_val)
4380 int chip_type_location;
4381 unsigned short chip_type;
4383 if (ftdi == NULL || ftdi->usb_dev == NULL)
4384 ftdi_error_return(-2, "USB device unavailable");
4386 if (eeprom_addr <0x80)
4387 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4394 chip_type_location = 0x14;
4398 chip_type_location = 0x18;
4401 chip_type_location = 0x1e;
4404 ftdi_error_return(-4, "Device can't access unprotected area");
4407 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4408 ftdi_error_return(-5, "Reading failed");
4409 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4410 if ((chip_type & 0xff) != 0x66)
4412 ftdi_error_return(-6, "EEPROM is not of 93x66");
4415 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4416 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4417 NULL, 0, ftdi->usb_write_timeout) != 0)
4418 ftdi_error_return(-1, "unable to write eeprom");
4426 \param ftdi pointer to ftdi_context
4429 \retval -1: read failed
4430 \retval -2: USB device unavailable
4431 \retval -3: EEPROM not initialized for the connected device;
4433 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4435 unsigned short usb_val, status;
4437 unsigned char *eeprom;
4439 if (ftdi == NULL || ftdi->usb_dev == NULL)
4440 ftdi_error_return(-2, "USB device unavailable");
4442 if(ftdi->eeprom->initialized_for_connected_device == 0)
4443 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4445 eeprom = ftdi->eeprom->buf;
4447 /* These commands were traced while running MProg */
4448 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4450 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4452 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4455 for (i = 0; i < ftdi->eeprom->size/2; i++)
4457 /* Do not try to write to reserved area */
4458 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4462 usb_val = eeprom[i*2];
4463 usb_val += eeprom[(i*2)+1] << 8;
4464 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4465 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4466 NULL, 0, ftdi->usb_write_timeout) < 0)
4467 ftdi_error_return(-1, "unable to write eeprom");
4476 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4478 \param ftdi pointer to ftdi_context
4481 \retval -1: erase failed
4482 \retval -2: USB device unavailable
4483 \retval -3: Writing magic failed
4484 \retval -4: Read EEPROM failed
4485 \retval -5: Unexpected EEPROM value
4487 #define MAGIC 0x55aa
4488 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4490 unsigned short eeprom_value;
4491 if (ftdi == NULL || ftdi->usb_dev == NULL)
4492 ftdi_error_return(-2, "USB device unavailable");
4494 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4496 ftdi->eeprom->chip = 0;
4500 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4501 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4502 ftdi_error_return(-1, "unable to erase eeprom");
4505 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4506 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4507 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4508 Chip is 93x66 if magic is only read at word position 0xc0*/
4509 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4510 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4511 NULL, 0, ftdi->usb_write_timeout) != 0)
4512 ftdi_error_return(-3, "Writing magic failed");
4513 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4514 ftdi_error_return(-4, "Reading failed");
4515 if (eeprom_value == MAGIC)
4517 ftdi->eeprom->chip = 0x46;
4521 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4522 ftdi_error_return(-4, "Reading failed");
4523 if (eeprom_value == MAGIC)
4524 ftdi->eeprom->chip = 0x56;
4527 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4528 ftdi_error_return(-4, "Reading failed");
4529 if (eeprom_value == MAGIC)
4530 ftdi->eeprom->chip = 0x66;
4533 ftdi->eeprom->chip = -1;
4537 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4538 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4539 ftdi_error_return(-1, "unable to erase eeprom");
4544 Get string representation for last error code
4546 \param ftdi pointer to ftdi_context
4548 \retval Pointer to error string
4550 const char *ftdi_get_error_string (struct ftdi_context *ftdi)
4555 return ftdi->error_str;
4558 /* @} end of doxygen libftdi group */