1 /***************************************************************************
5 copyright : (C) 2003-2017 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 https://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;
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 */
114 eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
116 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
117 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
118 ftdi->eeprom = eeprom;
120 /* All fine. Now allocate the readbuffer */
121 return ftdi_read_data_set_chunksize(ftdi, 4096);
125 Allocate and initialize a new ftdi_context
127 \return a pointer to a new ftdi_context, or NULL on failure
129 struct ftdi_context *ftdi_new(void)
131 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
138 if (ftdi_init(ftdi) != 0)
148 Open selected channels on a chip, otherwise use first channel.
150 \param ftdi pointer to ftdi_context
151 \param interface Interface to use for FT2232C/2232H/4232H chips.
154 \retval -1: unknown interface
155 \retval -2: USB device unavailable
156 \retval -3: Device already open, interface can't be set in that state
158 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
161 ftdi_error_return(-2, "USB device unavailable");
163 if (ftdi->usb_dev != NULL)
165 int check_interface = interface;
166 if (check_interface == INTERFACE_ANY)
167 check_interface = INTERFACE_A;
169 if (ftdi->index != check_interface)
170 ftdi_error_return(-3, "Interface can not be changed on an already open device");
178 ftdi->index = INTERFACE_A;
184 ftdi->index = INTERFACE_B;
190 ftdi->index = INTERFACE_C;
196 ftdi->index = INTERFACE_D;
201 ftdi_error_return(-1, "Unknown interface");
207 Deinitializes a ftdi_context.
209 \param ftdi pointer to ftdi_context
211 void ftdi_deinit(struct ftdi_context *ftdi)
216 ftdi_usb_close_internal (ftdi);
218 if (ftdi->readbuffer != NULL)
220 free(ftdi->readbuffer);
221 ftdi->readbuffer = NULL;
224 if (ftdi->eeprom != NULL)
226 if (ftdi->eeprom->manufacturer != 0)
228 free(ftdi->eeprom->manufacturer);
229 ftdi->eeprom->manufacturer = 0;
231 if (ftdi->eeprom->product != 0)
233 free(ftdi->eeprom->product);
234 ftdi->eeprom->product = 0;
236 if (ftdi->eeprom->serial != 0)
238 free(ftdi->eeprom->serial);
239 ftdi->eeprom->serial = 0;
247 libusb_exit(ftdi->usb_ctx);
248 ftdi->usb_ctx = NULL;
253 Deinitialize and free an ftdi_context.
255 \param ftdi pointer to ftdi_context
257 void ftdi_free(struct ftdi_context *ftdi)
264 Use an already open libusb device.
266 \param ftdi pointer to ftdi_context
267 \param usb libusb libusb_device_handle to use
269 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
278 * @brief Get libftdi library version
280 * @return ftdi_version_info Library version information
282 struct ftdi_version_info ftdi_get_library_version(void)
284 struct ftdi_version_info ver;
286 ver.major = FTDI_MAJOR_VERSION;
287 ver.minor = FTDI_MINOR_VERSION;
288 ver.micro = FTDI_MICRO_VERSION;
289 ver.version_str = FTDI_VERSION_STRING;
290 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
296 Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
297 ftdi_device_list which needs to be deallocated by ftdi_list_free() after
298 use. With VID:PID 0:0, search for the default devices
299 (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015)
301 \param ftdi pointer to ftdi_context
302 \param devlist Pointer where to store list of found devices
303 \param vendor Vendor ID to search for
304 \param product Product ID to search for
306 \retval >0: number of devices found
307 \retval -3: out of memory
308 \retval -5: libusb_get_device_list() failed
309 \retval -6: libusb_get_device_descriptor() failed
311 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
313 struct ftdi_device_list **curdev;
315 libusb_device **devs;
319 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
320 ftdi_error_return(-5, "libusb_get_device_list() failed");
325 while ((dev = devs[i++]) != NULL)
327 struct libusb_device_descriptor desc;
329 if (libusb_get_device_descriptor(dev, &desc) < 0)
330 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
332 if (((vendor || product) &&
333 desc.idVendor == vendor && desc.idProduct == product) ||
334 (!(vendor || product) &&
335 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
336 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
337 || desc.idProduct == 0x6015)))
339 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
341 ftdi_error_return_free_device_list(-3, "out of memory", devs);
343 (*curdev)->next = NULL;
344 (*curdev)->dev = dev;
345 libusb_ref_device(dev);
346 curdev = &(*curdev)->next;
350 libusb_free_device_list(devs,1);
355 Frees a usb device list.
357 \param devlist USB device list created by ftdi_usb_find_all()
359 void ftdi_list_free(struct ftdi_device_list **devlist)
361 struct ftdi_device_list *curdev, *next;
363 for (curdev = *devlist; curdev != NULL;)
366 libusb_unref_device(curdev->dev);
375 Frees a usb device list.
377 \param devlist USB device list created by ftdi_usb_find_all()
379 void ftdi_list_free2(struct ftdi_device_list *devlist)
381 ftdi_list_free(&devlist);
385 Return device ID strings from the usb device.
387 The parameters manufacturer, description and serial may be NULL
388 or pointer to buffers to store the fetched strings.
390 \note Use this function only in combination with ftdi_usb_find_all()
391 as it closes the internal "usb_dev" after use.
393 \param ftdi pointer to ftdi_context
394 \param dev libusb usb_dev to use
395 \param manufacturer Store manufacturer string here if not NULL
396 \param mnf_len Buffer size of manufacturer string
397 \param description Store product description string here if not NULL
398 \param desc_len Buffer size of product description string
399 \param serial Store serial string here if not NULL
400 \param serial_len Buffer size of serial string
403 \retval -1: wrong arguments
404 \retval -4: unable to open device
405 \retval -7: get product manufacturer failed
406 \retval -8: get product description failed
407 \retval -9: get serial number failed
408 \retval -11: libusb_get_device_descriptor() failed
410 int ftdi_usb_get_strings(struct ftdi_context *ftdi,
411 struct libusb_device *dev,
412 char *manufacturer, int mnf_len,
413 char *description, int desc_len,
414 char *serial, int serial_len)
418 if ((ftdi==NULL) || (dev==NULL))
421 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
422 ftdi_error_return(-4, "libusb_open() failed");
424 // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
425 // it won't be closed either. This allows us to close it whether we actually
426 // called libusb_open() up above or not. This matches the expected behavior
427 // (and note) for ftdi_usb_get_strings().
428 ret = ftdi_usb_get_strings2(ftdi, dev,
429 manufacturer, mnf_len,
430 description, desc_len,
433 // only close it if it was successful, as all other return codes close
434 // before returning already.
436 ftdi_usb_close_internal(ftdi);
442 Return device ID strings from the usb device.
444 The parameters manufacturer, description and serial may be NULL
445 or pointer to buffers to store the fetched strings.
447 \note The old function ftdi_usb_get_strings() always closes the device.
448 This version only closes the device if it was opened by it.
450 \param ftdi pointer to ftdi_context
451 \param dev libusb usb_dev to use
452 \param manufacturer Store manufacturer string here if not NULL
453 \param mnf_len Buffer size of manufacturer string
454 \param description Store product description string here if not NULL
455 \param desc_len Buffer size of product description string
456 \param serial Store serial string here if not NULL
457 \param serial_len Buffer size of serial string
460 \retval -1: wrong arguments
461 \retval -4: unable to open device
462 \retval -7: get product manufacturer failed
463 \retval -8: get product description failed
464 \retval -9: get serial number failed
465 \retval -11: libusb_get_device_descriptor() failed
467 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
468 char *manufacturer, int mnf_len,
469 char *description, int desc_len,
470 char *serial, int serial_len)
472 struct libusb_device_descriptor desc;
475 if ((ftdi==NULL) || (dev==NULL))
478 need_open = (ftdi->usb_dev == NULL);
479 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
480 ftdi_error_return(-4, "libusb_open() failed");
482 if (libusb_get_device_descriptor(dev, &desc) < 0)
483 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
485 if (manufacturer != NULL)
487 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
489 ftdi_usb_close_internal (ftdi);
490 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
494 if (description != NULL)
496 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
498 ftdi_usb_close_internal (ftdi);
499 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
505 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
507 ftdi_usb_close_internal (ftdi);
508 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
513 ftdi_usb_close_internal (ftdi);
519 * Internal function to determine the maximum packet size.
520 * \param ftdi pointer to ftdi_context
521 * \param dev libusb usb_dev to use
522 * \retval Maximum packet size for this device
524 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
526 struct libusb_device_descriptor desc;
527 struct libusb_config_descriptor *config0;
528 unsigned int packet_size;
531 if (ftdi == NULL || dev == NULL)
534 // Determine maximum packet size. Init with default value.
535 // New hi-speed devices from FTDI use a packet size of 512 bytes
536 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
537 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
542 if (libusb_get_device_descriptor(dev, &desc) < 0)
545 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
548 if (desc.bNumConfigurations > 0)
550 if (ftdi->interface < config0->bNumInterfaces)
552 struct libusb_interface interface = config0->interface[ftdi->interface];
553 if (interface.num_altsetting > 0)
555 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
556 if (descriptor.bNumEndpoints > 0)
558 packet_size = descriptor.endpoint[0].wMaxPacketSize;
564 libusb_free_config_descriptor (config0);
569 Opens a ftdi device given by an usb_device.
571 \param ftdi pointer to ftdi_context
572 \param dev libusb usb_dev to use
575 \retval -3: unable to config device
576 \retval -4: unable to open device
577 \retval -5: unable to claim device
578 \retval -6: reset failed
579 \retval -7: set baudrate failed
580 \retval -8: ftdi context invalid
581 \retval -9: libusb_get_device_descriptor() failed
582 \retval -10: libusb_get_config_descriptor() failed
583 \retval -11: libusb_detach_kernel_driver() failed
584 \retval -12: libusb_get_configuration() failed
586 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
588 struct libusb_device_descriptor desc;
589 struct libusb_config_descriptor *config0;
590 int cfg, cfg0, detach_errno = 0;
593 ftdi_error_return(-8, "ftdi context invalid");
595 if (libusb_open(dev, &ftdi->usb_dev) < 0)
596 ftdi_error_return(-4, "libusb_open() failed");
598 if (libusb_get_device_descriptor(dev, &desc) < 0)
599 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
601 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
602 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
603 cfg0 = config0->bConfigurationValue;
604 libusb_free_config_descriptor (config0);
606 // Try to detach ftdi_sio kernel module.
608 // The return code is kept in a separate variable and only parsed
609 // if usb_set_configuration() or usb_claim_interface() fails as the
610 // detach operation might be denied and everything still works fine.
611 // Likely scenario is a static ftdi_sio kernel module.
612 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
614 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
615 detach_errno = errno;
617 else if (ftdi->module_detach_mode == AUTO_DETACH_REATACH_SIO_MODULE)
619 if (libusb_set_auto_detach_kernel_driver(ftdi->usb_dev, 1) != LIBUSB_SUCCESS)
620 detach_errno = errno;
623 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
624 ftdi_error_return(-12, "libusb_get_configuration () failed");
625 // set configuration (needed especially for windows)
626 // tolerate EBUSY: one device with one configuration, but two interfaces
627 // and libftdi sessions to both interfaces (e.g. FT2232)
628 if (desc.bNumConfigurations > 0 && cfg != cfg0)
630 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
632 ftdi_usb_close_internal (ftdi);
633 if (detach_errno == EPERM)
635 ftdi_error_return(-8, "inappropriate permissions on device!");
639 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
644 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
646 ftdi_usb_close_internal (ftdi);
647 if (detach_errno == EPERM)
649 ftdi_error_return(-8, "inappropriate permissions on device!");
653 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
657 if (ftdi_usb_reset (ftdi) != 0)
659 ftdi_usb_close_internal (ftdi);
660 ftdi_error_return(-6, "ftdi_usb_reset failed");
663 // Try to guess chip type
664 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
665 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
666 && desc.iSerialNumber == 0))
667 ftdi->type = TYPE_BM;
668 else if (desc.bcdDevice == 0x200)
669 ftdi->type = TYPE_AM;
670 else if (desc.bcdDevice == 0x500)
671 ftdi->type = TYPE_2232C;
672 else if (desc.bcdDevice == 0x600)
674 else if (desc.bcdDevice == 0x700)
675 ftdi->type = TYPE_2232H;
676 else if (desc.bcdDevice == 0x800)
677 ftdi->type = TYPE_4232H;
678 else if (desc.bcdDevice == 0x900)
679 ftdi->type = TYPE_232H;
680 else if (desc.bcdDevice == 0x1000)
681 ftdi->type = TYPE_230X;
683 // Determine maximum packet size
684 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
686 if (ftdi_set_baudrate (ftdi, 9600) != 0)
688 ftdi_usb_close_internal (ftdi);
689 ftdi_error_return(-7, "set baudrate failed");
692 ftdi_error_return(0, "all fine");
696 Opens the first device with a given vendor and product ids.
698 \param ftdi pointer to ftdi_context
699 \param vendor Vendor ID
700 \param product Product ID
702 \retval same as ftdi_usb_open_desc()
704 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
706 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
710 Opens the first device with a given, vendor id, product id,
711 description and serial.
713 \param ftdi pointer to ftdi_context
714 \param vendor Vendor ID
715 \param product Product ID
716 \param description Description to search for. Use NULL if not needed.
717 \param serial Serial to search for. Use NULL if not needed.
720 \retval -3: usb device not found
721 \retval -4: unable to open device
722 \retval -5: unable to claim device
723 \retval -6: reset failed
724 \retval -7: set baudrate failed
725 \retval -8: get product description failed
726 \retval -9: get serial number failed
727 \retval -12: libusb_get_device_list() failed
728 \retval -13: libusb_get_device_descriptor() failed
730 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
731 const char* description, const char* serial)
733 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
737 Opens the index-th device with a given, vendor id, product id,
738 description and serial.
740 \param ftdi pointer to ftdi_context
741 \param vendor Vendor ID
742 \param product Product ID
743 \param description Description to search for. Use NULL if not needed.
744 \param serial Serial to search for. Use NULL if not needed.
745 \param index Number of matching device to open if there are more than one, starts with 0.
748 \retval -1: usb_find_busses() failed
749 \retval -2: usb_find_devices() failed
750 \retval -3: usb device not found
751 \retval -4: unable to open device
752 \retval -5: unable to claim device
753 \retval -6: reset failed
754 \retval -7: set baudrate failed
755 \retval -8: get product description failed
756 \retval -9: get serial number failed
757 \retval -10: unable to close device
758 \retval -11: ftdi context invalid
759 \retval -12: libusb_get_device_list() failed
761 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
762 const char* description, const char* serial, unsigned int index)
765 libusb_device **devs;
770 ftdi_error_return(-11, "ftdi context invalid");
772 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
773 ftdi_error_return(-12, "libusb_get_device_list() failed");
775 while ((dev = devs[i++]) != NULL)
777 struct libusb_device_descriptor desc;
780 if (libusb_get_device_descriptor(dev, &desc) < 0)
781 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
783 if (desc.idVendor == vendor && desc.idProduct == product)
785 if (libusb_open(dev, &ftdi->usb_dev) < 0)
786 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
788 if (description != NULL)
790 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
792 ftdi_usb_close_internal (ftdi);
793 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
795 if (strncmp(string, description, sizeof(string)) != 0)
797 ftdi_usb_close_internal (ftdi);
803 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
805 ftdi_usb_close_internal (ftdi);
806 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
808 if (strncmp(string, serial, sizeof(string)) != 0)
810 ftdi_usb_close_internal (ftdi);
815 ftdi_usb_close_internal (ftdi);
823 res = ftdi_usb_open_dev(ftdi, dev);
824 libusb_free_device_list(devs,1);
830 ftdi_error_return_free_device_list(-3, "device not found", devs);
834 Opens the device at a given USB bus and device address.
836 \param ftdi pointer to ftdi_context
837 \param bus Bus number
838 \param addr Device address
841 \retval -1: usb_find_busses() failed
842 \retval -2: usb_find_devices() failed
843 \retval -3: usb device not found
844 \retval -4: unable to open device
845 \retval -5: unable to claim device
846 \retval -6: reset failed
847 \retval -7: set baudrate failed
848 \retval -8: get product description failed
849 \retval -9: get serial number failed
850 \retval -10: unable to close device
851 \retval -11: ftdi context invalid
852 \retval -12: libusb_get_device_list() failed
854 int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
857 libusb_device **devs;
861 ftdi_error_return(-11, "ftdi context invalid");
863 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
864 ftdi_error_return(-12, "libusb_get_device_list() failed");
866 while ((dev = devs[i++]) != NULL)
868 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
871 res = ftdi_usb_open_dev(ftdi, dev);
872 libusb_free_device_list(devs,1);
878 ftdi_error_return_free_device_list(-3, "device not found", devs);
882 Opens the ftdi-device described by a description-string.
883 Intended to be used for parsing a device-description given as commandline argument.
885 \param ftdi pointer to ftdi_context
886 \param description NULL-terminated description-string, using this format:
887 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
888 \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")
889 \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
890 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
892 \note The description format may be extended in later versions.
895 \retval -2: libusb_get_device_list() failed
896 \retval -3: usb device not found
897 \retval -4: unable to open device
898 \retval -5: unable to claim device
899 \retval -6: reset failed
900 \retval -7: set baudrate failed
901 \retval -8: get product description failed
902 \retval -9: get serial number failed
903 \retval -10: unable to close device
904 \retval -11: illegal description format
905 \retval -12: ftdi context invalid
907 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
910 ftdi_error_return(-12, "ftdi context invalid");
912 if (description[0] == 0 || description[1] != ':')
913 ftdi_error_return(-11, "illegal description format");
915 if (description[0] == 'd')
918 libusb_device **devs;
919 unsigned int bus_number, device_address;
922 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
923 ftdi_error_return(-2, "libusb_get_device_list() failed");
925 /* XXX: This doesn't handle symlinks/odd paths/etc... */
926 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
927 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
929 while ((dev = devs[i++]) != NULL)
932 if (bus_number == libusb_get_bus_number (dev)
933 && device_address == libusb_get_device_address (dev))
935 ret = ftdi_usb_open_dev(ftdi, dev);
936 libusb_free_device_list(devs,1);
942 ftdi_error_return_free_device_list(-3, "device not found", devs);
944 else if (description[0] == 'i' || description[0] == 's')
947 unsigned int product;
948 unsigned int index=0;
949 const char *serial=NULL;
950 const char *startp, *endp;
953 startp=description+2;
954 vendor=strtoul((char*)startp,(char**)&endp,0);
955 if (*endp != ':' || endp == startp || errno != 0)
956 ftdi_error_return(-11, "illegal description format");
959 product=strtoul((char*)startp,(char**)&endp,0);
960 if (endp == startp || errno != 0)
961 ftdi_error_return(-11, "illegal description format");
963 if (description[0] == 'i' && *endp != 0)
965 /* optional index field in i-mode */
967 ftdi_error_return(-11, "illegal description format");
970 index=strtoul((char*)startp,(char**)&endp,0);
971 if (*endp != 0 || endp == startp || errno != 0)
972 ftdi_error_return(-11, "illegal description format");
974 if (description[0] == 's')
977 ftdi_error_return(-11, "illegal description format");
979 /* rest of the description is the serial */
983 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
987 ftdi_error_return(-11, "illegal description format");
992 Resets the ftdi device.
994 \param ftdi pointer to ftdi_context
997 \retval -1: FTDI reset failed
998 \retval -2: USB device unavailable
1000 int ftdi_usb_reset(struct ftdi_context *ftdi)
1002 if (ftdi == NULL || ftdi->usb_dev == NULL)
1003 ftdi_error_return(-2, "USB device unavailable");
1005 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1006 SIO_RESET_REQUEST, SIO_RESET_SIO,
1007 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1008 ftdi_error_return(-1,"FTDI reset failed");
1010 // Invalidate data in the readbuffer
1011 ftdi->readbuffer_offset = 0;
1012 ftdi->readbuffer_remaining = 0;
1018 Clears the read buffer on the chip and the internal read buffer.
1020 \param ftdi pointer to ftdi_context
1023 \retval -1: read buffer purge failed
1024 \retval -2: USB device unavailable
1026 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
1028 if (ftdi == NULL || ftdi->usb_dev == NULL)
1029 ftdi_error_return(-2, "USB device unavailable");
1031 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1032 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
1033 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1034 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1036 // Invalidate data in the readbuffer
1037 ftdi->readbuffer_offset = 0;
1038 ftdi->readbuffer_remaining = 0;
1044 Clears the write buffer on the chip.
1046 \param ftdi pointer to ftdi_context
1049 \retval -1: write buffer purge failed
1050 \retval -2: USB device unavailable
1052 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
1054 if (ftdi == NULL || ftdi->usb_dev == NULL)
1055 ftdi_error_return(-2, "USB device unavailable");
1057 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1058 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
1059 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1060 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1066 Clears the buffers on the chip and the internal read buffer.
1068 \param ftdi pointer to ftdi_context
1071 \retval -1: read buffer purge failed
1072 \retval -2: write buffer purge failed
1073 \retval -3: USB device unavailable
1075 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
1079 if (ftdi == NULL || ftdi->usb_dev == NULL)
1080 ftdi_error_return(-3, "USB device unavailable");
1082 result = ftdi_usb_purge_rx_buffer(ftdi);
1086 result = ftdi_usb_purge_tx_buffer(ftdi);
1096 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
1098 \param ftdi pointer to ftdi_context
1101 \retval -1: usb_release failed
1102 \retval -3: ftdi context invalid
1104 int ftdi_usb_close(struct ftdi_context *ftdi)
1109 ftdi_error_return(-3, "ftdi context invalid");
1111 if (ftdi->usb_dev != NULL)
1112 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1115 ftdi_usb_close_internal (ftdi);
1120 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1121 to encoded divisor and the achievable baudrate
1122 Function is only used internally
1129 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1130 The fractional part has frac_code encoding
1132 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1135 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1136 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1137 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1138 int divisor, best_divisor, best_baud, best_baud_diff;
1140 divisor = 24000000 / baudrate;
1142 // Round down to supported fraction (AM only)
1143 divisor -= am_adjust_dn[divisor & 7];
1145 // Try this divisor and the one above it (because division rounds down)
1149 for (i = 0; i < 2; i++)
1151 int try_divisor = divisor + i;
1155 // Round up to supported divisor value
1156 if (try_divisor <= 8)
1158 // Round up to minimum supported divisor
1161 else if (divisor < 16)
1163 // AM doesn't support divisors 9 through 15 inclusive
1168 // Round up to supported fraction (AM only)
1169 try_divisor += am_adjust_up[try_divisor & 7];
1170 if (try_divisor > 0x1FFF8)
1172 // Round down to maximum supported divisor value (for AM)
1173 try_divisor = 0x1FFF8;
1176 // Get estimated baud rate (to nearest integer)
1177 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1178 // Get absolute difference from requested baud rate
1179 if (baud_estimate < baudrate)
1181 baud_diff = baudrate - baud_estimate;
1185 baud_diff = baud_estimate - baudrate;
1187 if (i == 0 || baud_diff < best_baud_diff)
1189 // Closest to requested baud rate so far
1190 best_divisor = try_divisor;
1191 best_baud = baud_estimate;
1192 best_baud_diff = baud_diff;
1195 // Spot on! No point trying
1200 // Encode the best divisor value
1201 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1202 // Deal with special cases for encoded value
1203 if (*encoded_divisor == 1)
1205 *encoded_divisor = 0; // 3000000 baud
1207 else if (*encoded_divisor == 0x4001)
1209 *encoded_divisor = 1; // 2000000 baud (BM only)
1214 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1215 to encoded divisor and the achievable baudrate
1216 Function is only used internally
1223 From /2, 0.125 steps may be taken.
1224 The fractional part has frac_code encoding
1226 value[13:0] of value is the divisor
1227 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1229 H Type have all features above with
1230 {index[8],value[15:14]} is the encoded subdivisor
1232 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1233 {index[0],value[15:14]} is the encoded subdivisor
1235 AM Type chips have only four fractional subdivisors at value[15:14]
1236 for subdivisors 0, 0.5, 0.25, 0.125
1238 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1240 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1242 int divisor, best_divisor;
1243 if (baudrate >= clk/clk_div)
1245 *encoded_divisor = 0;
1246 best_baud = clk/clk_div;
1248 else if (baudrate >= clk/(clk_div + clk_div/2))
1250 *encoded_divisor = 1;
1251 best_baud = clk/(clk_div + clk_div/2);
1253 else if (baudrate >= clk/(2*clk_div))
1255 *encoded_divisor = 2;
1256 best_baud = clk/(2*clk_div);
1260 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1261 divisor = clk*16/clk_div / baudrate;
1262 if (divisor & 1) /* Decide if to round up or down*/
1263 best_divisor = divisor /2 +1;
1265 best_divisor = divisor/2;
1266 if(best_divisor > 0x20000)
1267 best_divisor = 0x1ffff;
1268 best_baud = clk*16/clk_div/best_divisor;
1269 if (best_baud & 1) /* Decide if to round up or down*/
1270 best_baud = best_baud /2 +1;
1272 best_baud = best_baud /2;
1273 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1278 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1279 Function is only used internally
1282 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1283 unsigned short *value, unsigned short *index)
1286 unsigned long encoded_divisor;
1294 #define H_CLK 120000000
1295 #define C_CLK 48000000
1296 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1298 if(baudrate*10 > H_CLK /0x3fff)
1300 /* On H Devices, use 12 000 000 Baudrate when possible
1301 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1302 three fractional bits and a 120 MHz clock
1303 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1304 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1305 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1306 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1309 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1311 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
1313 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1317 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1319 // Split into "value" and "index" values
1320 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1321 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1323 *index = (unsigned short)(encoded_divisor >> 8);
1325 *index |= ftdi->index;
1328 *index = (unsigned short)(encoded_divisor >> 16);
1330 // Return the nearest baud rate
1335 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1336 * Do not use, it's only for the unit test framework
1338 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1339 unsigned short *value, unsigned short *index)
1341 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1345 Sets the chip baud rate
1347 \param ftdi pointer to ftdi_context
1348 \param baudrate baud rate to set
1351 \retval -1: invalid baudrate
1352 \retval -2: setting baudrate failed
1353 \retval -3: USB device unavailable
1355 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1357 unsigned short value, index;
1358 int actual_baudrate;
1360 if (ftdi == NULL || ftdi->usb_dev == NULL)
1361 ftdi_error_return(-3, "USB device unavailable");
1363 if (ftdi->bitbang_enabled)
1365 baudrate = baudrate*4;
1368 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1369 if (actual_baudrate <= 0)
1370 ftdi_error_return (-1, "Silly baudrate <= 0.");
1372 // Check within tolerance (about 5%)
1373 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1374 || ((actual_baudrate < baudrate)
1375 ? (actual_baudrate * 21 < baudrate * 20)
1376 : (baudrate * 21 < actual_baudrate * 20)))
1377 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1379 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1380 SIO_SET_BAUDRATE_REQUEST, value,
1381 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1382 ftdi_error_return (-2, "Setting new baudrate failed");
1384 ftdi->baudrate = baudrate;
1389 Set (RS232) line characteristics.
1390 The break type can only be set via ftdi_set_line_property2()
1391 and defaults to "off".
1393 \param ftdi pointer to ftdi_context
1394 \param bits Number of bits
1395 \param sbit Number of stop bits
1396 \param parity Parity mode
1399 \retval -1: Setting line property failed
1401 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1402 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1404 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1408 Set (RS232) line characteristics
1410 \param ftdi pointer to ftdi_context
1411 \param bits Number of bits
1412 \param sbit Number of stop bits
1413 \param parity Parity mode
1414 \param break_type Break type
1417 \retval -1: Setting line property failed
1418 \retval -2: USB device unavailable
1420 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1421 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1422 enum ftdi_break_type break_type)
1424 unsigned short value = bits;
1426 if (ftdi == NULL || ftdi->usb_dev == NULL)
1427 ftdi_error_return(-2, "USB device unavailable");
1432 value |= (0x00 << 8);
1435 value |= (0x01 << 8);
1438 value |= (0x02 << 8);
1441 value |= (0x03 << 8);
1444 value |= (0x04 << 8);
1451 value |= (0x00 << 11);
1454 value |= (0x01 << 11);
1457 value |= (0x02 << 11);
1464 value |= (0x00 << 14);
1467 value |= (0x01 << 14);
1471 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1472 SIO_SET_DATA_REQUEST, value,
1473 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1474 ftdi_error_return (-1, "Setting new line property failed");
1480 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1482 \param ftdi pointer to ftdi_context
1483 \param buf Buffer with the data
1484 \param size Size of the buffer
1486 \retval -666: USB device unavailable
1487 \retval <0: error code from usb_bulk_write()
1488 \retval >0: number of bytes written
1490 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1495 if (ftdi == NULL || ftdi->usb_dev == NULL)
1496 ftdi_error_return(-666, "USB device unavailable");
1498 while (offset < size)
1500 int write_size = ftdi->writebuffer_chunksize;
1502 if (offset+write_size > size)
1503 write_size = size-offset;
1505 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1506 ftdi_error_return(-1, "usb bulk write failed");
1508 offset += actual_length;
1514 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1516 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1517 struct ftdi_context *ftdi = tc->ftdi;
1518 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1520 packet_size = ftdi->max_packet_size;
1522 actual_length = transfer->actual_length;
1524 if (actual_length > 2)
1526 // skip FTDI status bytes.
1527 // Maybe stored in the future to enable modem use
1528 num_of_chunks = actual_length / packet_size;
1529 chunk_remains = actual_length % packet_size;
1530 //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);
1532 ftdi->readbuffer_offset += 2;
1535 if (actual_length > packet_size - 2)
1537 for (i = 1; i < num_of_chunks; i++)
1538 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1539 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1541 if (chunk_remains > 2)
1543 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1544 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1546 actual_length -= 2*num_of_chunks;
1549 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1552 if (actual_length > 0)
1554 // data still fits in buf?
1555 if (tc->offset + actual_length <= tc->size)
1557 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1558 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1559 tc->offset += actual_length;
1561 ftdi->readbuffer_offset = 0;
1562 ftdi->readbuffer_remaining = 0;
1564 /* Did we read exactly the right amount of bytes? */
1565 if (tc->offset == tc->size)
1567 //printf("read_data exact rem %d offset %d\n",
1568 //ftdi->readbuffer_remaining, offset);
1575 // only copy part of the data or size <= readbuffer_chunksize
1576 int part_size = tc->size - tc->offset;
1577 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1578 tc->offset += part_size;
1580 ftdi->readbuffer_offset += part_size;
1581 ftdi->readbuffer_remaining = actual_length - part_size;
1583 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1584 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1591 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1592 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1595 ret = libusb_submit_transfer (transfer);
1602 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1604 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1605 struct ftdi_context *ftdi = tc->ftdi;
1607 tc->offset += transfer->actual_length;
1609 if (tc->offset == tc->size)
1615 int write_size = ftdi->writebuffer_chunksize;
1618 if (tc->offset + write_size > tc->size)
1619 write_size = tc->size - tc->offset;
1621 transfer->length = write_size;
1622 transfer->buffer = tc->buf + tc->offset;
1624 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1625 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1628 ret = libusb_submit_transfer (transfer);
1637 Writes data to the chip. Does not wait for completion of the transfer
1638 nor does it make sure that the transfer was successful.
1640 Use libusb 1.0 asynchronous API.
1642 \param ftdi pointer to ftdi_context
1643 \param buf Buffer with the data
1644 \param size Size of the buffer
1646 \retval NULL: Some error happens when submit transfer
1647 \retval !NULL: Pointer to a ftdi_transfer_control
1650 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1652 struct ftdi_transfer_control *tc;
1653 struct libusb_transfer *transfer;
1654 int write_size, ret;
1656 if (ftdi == NULL || ftdi->usb_dev == NULL)
1659 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1663 transfer = libusb_alloc_transfer(0);
1676 if (size < (int)ftdi->writebuffer_chunksize)
1679 write_size = ftdi->writebuffer_chunksize;
1681 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1682 write_size, ftdi_write_data_cb, tc,
1683 ftdi->usb_write_timeout);
1684 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1686 ret = libusb_submit_transfer(transfer);
1689 libusb_free_transfer(transfer);
1693 tc->transfer = transfer;
1699 Reads data from the chip. Does not wait for completion of the transfer
1700 nor does it make sure that the transfer was successful.
1702 Use libusb 1.0 asynchronous API.
1704 \param ftdi pointer to ftdi_context
1705 \param buf Buffer with the data
1706 \param size Size of the buffer
1708 \retval NULL: Some error happens when submit transfer
1709 \retval !NULL: Pointer to a ftdi_transfer_control
1712 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1714 struct ftdi_transfer_control *tc;
1715 struct libusb_transfer *transfer;
1718 if (ftdi == NULL || ftdi->usb_dev == NULL)
1721 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1729 if (size <= (int)ftdi->readbuffer_remaining)
1731 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1734 ftdi->readbuffer_remaining -= size;
1735 ftdi->readbuffer_offset += size;
1737 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1741 tc->transfer = NULL;
1746 if (ftdi->readbuffer_remaining != 0)
1748 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1750 tc->offset = ftdi->readbuffer_remaining;
1755 transfer = libusb_alloc_transfer(0);
1762 ftdi->readbuffer_remaining = 0;
1763 ftdi->readbuffer_offset = 0;
1765 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);
1766 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1768 ret = libusb_submit_transfer(transfer);
1771 libusb_free_transfer(transfer);
1775 tc->transfer = transfer;
1781 Wait for completion of the transfer.
1783 Use libusb 1.0 asynchronous API.
1785 \param tc pointer to ftdi_transfer_control
1787 \retval < 0: Some error happens
1788 \retval >= 0: Data size transferred
1791 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1794 struct timeval to = { 0, 0 };
1795 while (!tc->completed)
1797 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1798 &to, &tc->completed);
1801 if (ret == LIBUSB_ERROR_INTERRUPTED)
1803 libusb_cancel_transfer(tc->transfer);
1804 while (!tc->completed)
1805 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1806 &to, &tc->completed) < 0)
1808 libusb_free_transfer(tc->transfer);
1816 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1817 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1821 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1823 libusb_free_transfer(tc->transfer);
1830 Cancel transfer and wait for completion.
1832 Use libusb 1.0 asynchronous API.
1834 \param tc pointer to ftdi_transfer_control
1835 \param to pointer to timeout value or NULL for infinite
1838 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
1839 struct timeval * to)
1841 struct timeval tv = { 0, 0 };
1843 if (!tc->completed && tc->transfer != NULL)
1848 libusb_cancel_transfer(tc->transfer);
1849 while (!tc->completed)
1851 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1857 libusb_free_transfer(tc->transfer);
1863 Configure write buffer chunk size.
1866 \param ftdi pointer to ftdi_context
1867 \param chunksize Chunk size
1870 \retval -1: ftdi context invalid
1872 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1875 ftdi_error_return(-1, "ftdi context invalid");
1877 ftdi->writebuffer_chunksize = chunksize;
1882 Get write buffer chunk size.
1884 \param ftdi pointer to ftdi_context
1885 \param chunksize Pointer to store chunk size in
1888 \retval -1: ftdi context invalid
1890 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1893 ftdi_error_return(-1, "ftdi context invalid");
1895 *chunksize = ftdi->writebuffer_chunksize;
1900 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1902 Automatically strips the two modem status bytes transfered during every read.
1904 \param ftdi pointer to ftdi_context
1905 \param buf Buffer to store data in
1906 \param size Size of the buffer
1908 \retval -666: USB device unavailable
1909 \retval <0: error code from libusb_bulk_transfer()
1910 \retval 0: no data was available
1911 \retval >0: number of bytes read
1914 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1916 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1918 int actual_length = 1;
1920 if (ftdi == NULL || ftdi->usb_dev == NULL)
1921 ftdi_error_return(-666, "USB device unavailable");
1923 // Packet size sanity check (avoid division by zero)
1924 packet_size = ftdi->max_packet_size;
1925 if (packet_size == 0)
1926 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1928 // everything we want is still in the readbuffer?
1929 if (size <= (int)ftdi->readbuffer_remaining)
1931 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1934 ftdi->readbuffer_remaining -= size;
1935 ftdi->readbuffer_offset += size;
1937 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1941 // something still in the readbuffer, but not enough to satisfy 'size'?
1942 if (ftdi->readbuffer_remaining != 0)
1944 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1947 offset += ftdi->readbuffer_remaining;
1949 // do the actual USB read
1950 while (offset < size && actual_length > 0)
1952 ftdi->readbuffer_remaining = 0;
1953 ftdi->readbuffer_offset = 0;
1954 /* returns how much received */
1955 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1957 ftdi_error_return(ret, "usb bulk read failed");
1959 if (actual_length > 2)
1961 // skip FTDI status bytes.
1962 // Maybe stored in the future to enable modem use
1963 num_of_chunks = actual_length / packet_size;
1964 chunk_remains = actual_length % packet_size;
1965 //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);
1967 ftdi->readbuffer_offset += 2;
1970 if (actual_length > packet_size - 2)
1972 for (i = 1; i < num_of_chunks; i++)
1973 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1974 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1976 if (chunk_remains > 2)
1978 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1979 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1981 actual_length -= 2*num_of_chunks;
1984 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1987 else if (actual_length <= 2)
1989 // no more data to read?
1992 if (actual_length > 0)
1994 // data still fits in buf?
1995 if (offset+actual_length <= size)
1997 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1998 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1999 offset += actual_length;
2001 /* Did we read exactly the right amount of bytes? */
2003 //printf("read_data exact rem %d offset %d\n",
2004 //ftdi->readbuffer_remaining, offset);
2009 // only copy part of the data or size <= readbuffer_chunksize
2010 int part_size = size-offset;
2011 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
2013 ftdi->readbuffer_offset += part_size;
2014 ftdi->readbuffer_remaining = actual_length-part_size;
2015 offset += part_size;
2017 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
2018 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
2029 Configure read buffer chunk size.
2032 Automatically reallocates the buffer.
2034 \param ftdi pointer to ftdi_context
2035 \param chunksize Chunk size
2038 \retval -1: ftdi context invalid
2040 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
2042 unsigned char *new_buf;
2045 ftdi_error_return(-1, "ftdi context invalid");
2047 // Invalidate all remaining data
2048 ftdi->readbuffer_offset = 0;
2049 ftdi->readbuffer_remaining = 0;
2051 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
2052 which is defined in libusb-1.0. Otherwise, each USB read request will
2053 be divided into multiple URBs. This will cause issues on Linux kernel
2054 older than 2.6.32. */
2055 if (chunksize > 16384)
2059 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2060 ftdi_error_return(-1, "out of memory for readbuffer");
2062 ftdi->readbuffer = new_buf;
2063 ftdi->readbuffer_chunksize = chunksize;
2069 Get read buffer chunk size.
2071 \param ftdi pointer to ftdi_context
2072 \param chunksize Pointer to store chunk size in
2075 \retval -1: FTDI context invalid
2077 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2080 ftdi_error_return(-1, "FTDI context invalid");
2082 *chunksize = ftdi->readbuffer_chunksize;
2087 Enable/disable bitbang modes.
2089 \param ftdi pointer to ftdi_context
2090 \param bitmask Bitmask to configure lines.
2091 HIGH/ON value configures a line as output.
2092 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
2095 \retval -1: can't enable bitbang mode
2096 \retval -2: USB device unavailable
2098 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2100 unsigned short usb_val;
2102 if (ftdi == NULL || ftdi->usb_dev == NULL)
2103 ftdi_error_return(-2, "USB device unavailable");
2105 usb_val = bitmask; // low byte: bitmask
2106 usb_val |= (mode << 8);
2107 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)
2108 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2110 ftdi->bitbang_mode = mode;
2111 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2116 Disable bitbang mode.
2118 \param ftdi pointer to ftdi_context
2121 \retval -1: can't disable bitbang mode
2122 \retval -2: USB device unavailable
2124 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
2126 if (ftdi == NULL || ftdi->usb_dev == NULL)
2127 ftdi_error_return(-2, "USB device unavailable");
2129 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)
2130 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2132 ftdi->bitbang_enabled = 0;
2138 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
2140 \param ftdi pointer to ftdi_context
2141 \param pins Pointer to store pins into
2144 \retval -1: read pins failed
2145 \retval -2: USB device unavailable
2147 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2149 if (ftdi == NULL || ftdi->usb_dev == NULL)
2150 ftdi_error_return(-2, "USB device unavailable");
2152 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)
2153 ftdi_error_return(-1, "read pins failed");
2161 The FTDI chip keeps data in the internal buffer for a specific
2162 amount of time if the buffer is not full yet to decrease
2163 load on the usb bus.
2165 \param ftdi pointer to ftdi_context
2166 \param latency Value between 1 and 255
2169 \retval -1: latency out of range
2170 \retval -2: unable to set latency timer
2171 \retval -3: USB device unavailable
2173 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2175 unsigned short usb_val;
2178 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2180 if (ftdi == NULL || ftdi->usb_dev == NULL)
2181 ftdi_error_return(-3, "USB device unavailable");
2184 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)
2185 ftdi_error_return(-2, "unable to set latency timer");
2193 \param ftdi pointer to ftdi_context
2194 \param latency Pointer to store latency value in
2197 \retval -1: unable to get latency timer
2198 \retval -2: USB device unavailable
2200 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2202 unsigned short usb_val;
2204 if (ftdi == NULL || ftdi->usb_dev == NULL)
2205 ftdi_error_return(-2, "USB device unavailable");
2207 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)
2208 ftdi_error_return(-1, "reading latency timer failed");
2210 *latency = (unsigned char)usb_val;
2215 Poll modem status information
2217 This function allows the retrieve the two status bytes of the device.
2218 The device sends these bytes also as a header for each read access
2219 where they are discarded by ftdi_read_data(). The chip generates
2220 the two stripped status bytes in the absence of data every 40 ms.
2222 Layout of the first byte:
2223 - B0..B3 - must be 0
2224 - B4 Clear to send (CTS)
2227 - B5 Data set ready (DTS)
2230 - B6 Ring indicator (RI)
2233 - B7 Receive line signal detect (RLSD)
2237 Layout of the second byte:
2238 - B0 Data ready (DR)
2239 - B1 Overrun error (OE)
2240 - B2 Parity error (PE)
2241 - B3 Framing error (FE)
2242 - B4 Break interrupt (BI)
2243 - B5 Transmitter holding register (THRE)
2244 - B6 Transmitter empty (TEMT)
2245 - B7 Error in RCVR FIFO
2247 \param ftdi pointer to ftdi_context
2248 \param status Pointer to store status information in. Must be two bytes.
2251 \retval -1: unable to retrieve status information
2252 \retval -2: USB device unavailable
2254 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2258 if (ftdi == NULL || ftdi->usb_dev == NULL)
2259 ftdi_error_return(-2, "USB device unavailable");
2261 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)
2262 ftdi_error_return(-1, "getting modem status failed");
2264 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2270 Set flowcontrol for ftdi chip
2272 Note: Do not use this function to enable XON/XOFF mode, use ftdi_setflowctrl_xonxoff() instead.
2274 \param ftdi pointer to ftdi_context
2275 \param flowctrl flow control to use. should be
2276 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS
2279 \retval -1: set flow control failed
2280 \retval -2: USB device unavailable
2282 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2284 if (ftdi == NULL || ftdi->usb_dev == NULL)
2285 ftdi_error_return(-2, "USB device unavailable");
2287 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2288 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2289 NULL, 0, ftdi->usb_write_timeout) < 0)
2290 ftdi_error_return(-1, "set flow control failed");
2296 Set XON/XOFF flowcontrol for ftdi chip
2298 \param ftdi pointer to ftdi_context
2299 \param xon character code used to resume transmission
2300 \param xoff character code used to pause transmission
2303 \retval -1: set flow control failed
2304 \retval -2: USB device unavailable
2306 int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
2308 if (ftdi == NULL || ftdi->usb_dev == NULL)
2309 ftdi_error_return(-2, "USB device unavailable");
2311 uint16_t xonxoff = xon | (xoff << 8);
2312 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2313 SIO_SET_FLOW_CTRL_REQUEST, xonxoff, (SIO_XON_XOFF_HS | ftdi->index),
2314 NULL, 0, ftdi->usb_write_timeout) < 0)
2315 ftdi_error_return(-1, "set flow control failed");
2323 \param ftdi pointer to ftdi_context
2324 \param state state to set line to (1 or 0)
2327 \retval -1: set dtr failed
2328 \retval -2: USB device unavailable
2330 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2332 unsigned short usb_val;
2334 if (ftdi == NULL || ftdi->usb_dev == NULL)
2335 ftdi_error_return(-2, "USB device unavailable");
2338 usb_val = SIO_SET_DTR_HIGH;
2340 usb_val = SIO_SET_DTR_LOW;
2342 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2343 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2344 NULL, 0, ftdi->usb_write_timeout) < 0)
2345 ftdi_error_return(-1, "set dtr failed");
2353 \param ftdi pointer to ftdi_context
2354 \param state state to set line to (1 or 0)
2357 \retval -1: set rts failed
2358 \retval -2: USB device unavailable
2360 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2362 unsigned short usb_val;
2364 if (ftdi == NULL || ftdi->usb_dev == NULL)
2365 ftdi_error_return(-2, "USB device unavailable");
2368 usb_val = SIO_SET_RTS_HIGH;
2370 usb_val = SIO_SET_RTS_LOW;
2372 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2373 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2374 NULL, 0, ftdi->usb_write_timeout) < 0)
2375 ftdi_error_return(-1, "set of rts failed");
2381 Set dtr and rts line in one pass
2383 \param ftdi pointer to ftdi_context
2384 \param dtr DTR state to set line to (1 or 0)
2385 \param rts RTS state to set line to (1 or 0)
2388 \retval -1: set dtr/rts failed
2389 \retval -2: USB device unavailable
2391 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2393 unsigned short usb_val;
2395 if (ftdi == NULL || ftdi->usb_dev == NULL)
2396 ftdi_error_return(-2, "USB device unavailable");
2399 usb_val = SIO_SET_DTR_HIGH;
2401 usb_val = SIO_SET_DTR_LOW;
2404 usb_val |= SIO_SET_RTS_HIGH;
2406 usb_val |= SIO_SET_RTS_LOW;
2408 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2409 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2410 NULL, 0, ftdi->usb_write_timeout) < 0)
2411 ftdi_error_return(-1, "set of rts/dtr failed");
2417 Set the special event character
2419 \param ftdi pointer to ftdi_context
2420 \param eventch Event character
2421 \param enable 0 to disable the event character, non-zero otherwise
2424 \retval -1: unable to set event character
2425 \retval -2: USB device unavailable
2427 int ftdi_set_event_char(struct ftdi_context *ftdi,
2428 unsigned char eventch, unsigned char enable)
2430 unsigned short usb_val;
2432 if (ftdi == NULL || ftdi->usb_dev == NULL)
2433 ftdi_error_return(-2, "USB device unavailable");
2439 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)
2440 ftdi_error_return(-1, "setting event character failed");
2448 \param ftdi pointer to ftdi_context
2449 \param errorch Error character
2450 \param enable 0 to disable the error character, non-zero otherwise
2453 \retval -1: unable to set error character
2454 \retval -2: USB device unavailable
2456 int ftdi_set_error_char(struct ftdi_context *ftdi,
2457 unsigned char errorch, unsigned char enable)
2459 unsigned short usb_val;
2461 if (ftdi == NULL || ftdi->usb_dev == NULL)
2462 ftdi_error_return(-2, "USB device unavailable");
2468 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)
2469 ftdi_error_return(-1, "setting error character failed");
2475 Init eeprom with default values for the connected device
2476 \param ftdi pointer to ftdi_context
2477 \param manufacturer String to use as Manufacturer
2478 \param product String to use as Product description
2479 \param serial String to use as Serial number description
2482 \retval -1: No struct ftdi_context
2483 \retval -2: No struct ftdi_eeprom
2484 \retval -3: No connected device or device not yet opened
2486 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2487 char * product, char * serial)
2489 struct ftdi_eeprom *eeprom;
2492 ftdi_error_return(-1, "No struct ftdi_context");
2494 if (ftdi->eeprom == NULL)
2495 ftdi_error_return(-2,"No struct ftdi_eeprom");
2497 eeprom = ftdi->eeprom;
2498 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2500 if (ftdi->usb_dev == NULL)
2501 ftdi_error_return(-3, "No connected device or device not yet opened");
2503 eeprom->vendor_id = 0x0403;
2504 eeprom->use_serial = 1;
2505 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2506 (ftdi->type == TYPE_R))
2507 eeprom->product_id = 0x6001;
2508 else if (ftdi->type == TYPE_4232H)
2509 eeprom->product_id = 0x6011;
2510 else if (ftdi->type == TYPE_232H)
2511 eeprom->product_id = 0x6014;
2512 else if (ftdi->type == TYPE_230X)
2513 eeprom->product_id = 0x6015;
2515 eeprom->product_id = 0x6010;
2517 if (ftdi->type == TYPE_AM)
2518 eeprom->usb_version = 0x0101;
2520 eeprom->usb_version = 0x0200;
2521 eeprom->max_power = 100;
2523 if (eeprom->manufacturer)
2524 free (eeprom->manufacturer);
2525 eeprom->manufacturer = NULL;
2528 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2529 if (eeprom->manufacturer)
2530 strcpy(eeprom->manufacturer, manufacturer);
2533 if (eeprom->product)
2534 free (eeprom->product);
2535 eeprom->product = NULL;
2538 eeprom->product = (char *)malloc(strlen(product)+1);
2539 if (eeprom->product)
2540 strcpy(eeprom->product, product);
2544 const char* default_product;
2547 case TYPE_AM: default_product = "AM"; break;
2548 case TYPE_BM: default_product = "BM"; break;
2549 case TYPE_2232C: default_product = "Dual RS232"; break;
2550 case TYPE_R: default_product = "FT232R USB UART"; break;
2551 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2552 case TYPE_4232H: default_product = "FT4232H"; break;
2553 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2554 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2556 ftdi_error_return(-3, "Unknown chip type");
2558 eeprom->product = (char *)malloc(strlen(default_product) +1);
2559 if (eeprom->product)
2560 strcpy(eeprom->product, default_product);
2564 free (eeprom->serial);
2565 eeprom->serial = NULL;
2568 eeprom->serial = (char *)malloc(strlen(serial)+1);
2570 strcpy(eeprom->serial, serial);
2573 if (ftdi->type == TYPE_R)
2575 eeprom->max_power = 90;
2576 eeprom->size = 0x80;
2577 eeprom->cbus_function[0] = CBUS_TXLED;
2578 eeprom->cbus_function[1] = CBUS_RXLED;
2579 eeprom->cbus_function[2] = CBUS_TXDEN;
2580 eeprom->cbus_function[3] = CBUS_PWREN;
2581 eeprom->cbus_function[4] = CBUS_SLEEP;
2583 else if (ftdi->type == TYPE_230X)
2585 eeprom->max_power = 90;
2586 eeprom->size = 0x100;
2587 eeprom->cbus_function[0] = CBUSX_TXDEN;
2588 eeprom->cbus_function[1] = CBUSX_RXLED;
2589 eeprom->cbus_function[2] = CBUSX_TXLED;
2590 eeprom->cbus_function[3] = CBUSX_SLEEP;
2594 if(ftdi->type == TYPE_232H)
2597 for (i=0; i<10; i++)
2598 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2605 eeprom->release_number = 0x0200;
2608 eeprom->release_number = 0x0400;
2611 eeprom->release_number = 0x0500;
2614 eeprom->release_number = 0x0600;
2617 eeprom->release_number = 0x0700;
2620 eeprom->release_number = 0x0800;
2623 eeprom->release_number = 0x0900;
2626 eeprom->release_number = 0x1000;
2629 eeprom->release_number = 0x00;
2634 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2635 char * product, char * serial)
2637 struct ftdi_eeprom *eeprom;
2640 ftdi_error_return(-1, "No struct ftdi_context");
2642 if (ftdi->eeprom == NULL)
2643 ftdi_error_return(-2,"No struct ftdi_eeprom");
2645 eeprom = ftdi->eeprom;
2647 if (ftdi->usb_dev == NULL)
2648 ftdi_error_return(-3, "No connected device or device not yet opened");
2652 if (eeprom->manufacturer)
2653 free (eeprom->manufacturer);
2654 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2655 if (eeprom->manufacturer)
2656 strcpy(eeprom->manufacturer, manufacturer);
2661 if (eeprom->product)
2662 free (eeprom->product);
2663 eeprom->product = (char *)malloc(strlen(product)+1);
2664 if (eeprom->product)
2665 strcpy(eeprom->product, product);
2671 free (eeprom->serial);
2672 eeprom->serial = (char *)malloc(strlen(serial)+1);
2675 strcpy(eeprom->serial, serial);
2676 eeprom->use_serial = 1;
2683 Return device ID strings from the eeprom. Device needs to be connected.
2685 The parameters manufacturer, description and serial may be NULL
2686 or pointer to buffers to store the fetched strings.
2688 \param ftdi pointer to ftdi_context
2689 \param manufacturer Store manufacturer string here if not NULL
2690 \param mnf_len Buffer size of manufacturer string
2691 \param product Store product description string here if not NULL
2692 \param prod_len Buffer size of product description string
2693 \param serial Store serial string here if not NULL
2694 \param serial_len Buffer size of serial string
2697 \retval -1: ftdi context invalid
2698 \retval -2: ftdi eeprom buffer invalid
2700 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
2701 char *manufacturer, int mnf_len,
2702 char *product, int prod_len,
2703 char *serial, int serial_len)
2705 struct ftdi_eeprom *eeprom;
2708 ftdi_error_return(-1, "No struct ftdi_context");
2709 if (ftdi->eeprom == NULL)
2710 ftdi_error_return(-2, "No struct ftdi_eeprom");
2712 eeprom = ftdi->eeprom;
2716 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
2718 manufacturer[mnf_len - 1] = '\0';
2723 strncpy(product, eeprom->product, prod_len);
2725 product[prod_len - 1] = '\0';
2730 strncpy(serial, eeprom->serial, serial_len);
2732 serial[serial_len - 1] = '\0';
2738 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2739 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2744 int mode_low, mode_high;
2745 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2746 mode_low = CBUSH_TRISTATE;
2748 mode_low = eeprom->cbus_function[2*i];
2749 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2750 mode_high = CBUSH_TRISTATE;
2752 mode_high = eeprom->cbus_function[2*i+1];
2754 output[0x18+i] = (mode_high <<4) | mode_low;
2757 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2760 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2769 case CHANNEL_IS_UART: return 0;
2770 case CHANNEL_IS_FIFO: return 0x01;
2771 case CHANNEL_IS_OPTO: return 0x02;
2772 case CHANNEL_IS_CPU : return 0x04;
2780 case CHANNEL_IS_UART : return 0;
2781 case CHANNEL_IS_FIFO : return 0x01;
2782 case CHANNEL_IS_OPTO : return 0x02;
2783 case CHANNEL_IS_CPU : return 0x04;
2784 case CHANNEL_IS_FT1284 : return 0x08;
2792 case CHANNEL_IS_UART : return 0;
2793 case CHANNEL_IS_FIFO : return 0x01;
2797 case TYPE_230X: /* FT230X is only UART */
2804 Build binary buffer from ftdi_eeprom structure.
2805 Output is suitable for ftdi_write_eeprom().
2807 \param ftdi pointer to ftdi_context
2809 \retval >=0: size of eeprom user area in bytes
2810 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2811 \retval -2: Invalid eeprom or ftdi pointer
2812 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2813 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2814 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2815 \retval -6: No connected EEPROM or EEPROM Type unknown
2817 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2819 unsigned char i, j, eeprom_size_mask;
2820 unsigned short checksum, value;
2821 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2822 int user_area_size, free_start, free_end;
2823 struct ftdi_eeprom *eeprom;
2824 unsigned char * output;
2827 ftdi_error_return(-2,"No context");
2828 if (ftdi->eeprom == NULL)
2829 ftdi_error_return(-2,"No eeprom structure");
2831 eeprom= ftdi->eeprom;
2832 output = eeprom->buf;
2834 if (eeprom->chip == -1)
2835 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2837 if (eeprom->size == -1)
2839 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2840 eeprom->size = 0x100;
2842 eeprom->size = 0x80;
2845 if (eeprom->manufacturer != NULL)
2846 manufacturer_size = strlen(eeprom->manufacturer);
2847 if (eeprom->product != NULL)
2848 product_size = strlen(eeprom->product);
2849 if (eeprom->serial != NULL)
2850 serial_size = strlen(eeprom->serial);
2852 // eeprom size check
2858 user_area_size = 96; // base size for strings (total of 48 characters)
2861 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2864 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2866 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2868 user_area_size = 86;
2871 user_area_size = 80;
2877 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2879 if (user_area_size < 0)
2880 ftdi_error_return(-1,"eeprom size exceeded");
2883 if (ftdi->type == TYPE_230X)
2885 /* FT230X have a reserved section in the middle of the MTP,
2886 which cannot be written to, but must be included in the checksum */
2887 memset(ftdi->eeprom->buf, 0, 0x80);
2888 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2892 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2895 // Bytes and Bits set for all Types
2897 // Addr 02: Vendor ID
2898 output[0x02] = eeprom->vendor_id;
2899 output[0x03] = eeprom->vendor_id >> 8;
2901 // Addr 04: Product ID
2902 output[0x04] = eeprom->product_id;
2903 output[0x05] = eeprom->product_id >> 8;
2905 // Addr 06: Device release number (0400h for BM features)
2906 output[0x06] = eeprom->release_number;
2907 output[0x07] = eeprom->release_number >> 8;
2909 // Addr 08: Config descriptor
2911 // Bit 6: 1 if this device is self powered, 0 if bus powered
2912 // Bit 5: 1 if this device uses remote wakeup
2913 // Bit 4-0: reserved - 0
2915 if (eeprom->self_powered)
2917 if (eeprom->remote_wakeup)
2921 // Addr 09: Max power consumption: max power = value * 2 mA
2922 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2924 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2926 // Addr 0A: Chip configuration
2927 // Bit 7: 0 - reserved
2928 // Bit 6: 0 - reserved
2929 // Bit 5: 0 - reserved
2930 // Bit 4: 1 - Change USB version
2931 // Bit 3: 1 - Use the serial number string
2932 // Bit 2: 1 - Enable suspend pull downs for lower power
2933 // Bit 1: 1 - Out EndPoint is Isochronous
2934 // Bit 0: 1 - In EndPoint is Isochronous
2937 if (eeprom->in_is_isochronous)
2939 if (eeprom->out_is_isochronous)
2945 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2946 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2967 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2968 eeprom_size_mask = eeprom->size -1;
2969 free_end = i & eeprom_size_mask;
2971 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2972 // Addr 0F: Length of manufacturer string
2973 // Output manufacturer
2974 output[0x0E] = i; // calculate offset
2975 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2976 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2977 for (j = 0; j < manufacturer_size; j++)
2979 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2980 output[i & eeprom_size_mask] = 0x00, i++;
2982 output[0x0F] = manufacturer_size*2 + 2;
2984 // Addr 10: Offset of the product string + 0x80, calculated later
2985 // Addr 11: Length of product string
2986 output[0x10] = i | 0x80; // calculate offset
2987 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2988 output[i & eeprom_size_mask] = 0x03, i++;
2989 for (j = 0; j < product_size; j++)
2991 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2992 output[i & eeprom_size_mask] = 0x00, i++;
2994 output[0x11] = product_size*2 + 2;
2996 // Addr 12: Offset of the serial string + 0x80, calculated later
2997 // Addr 13: Length of serial string
2998 output[0x12] = i | 0x80; // calculate offset
2999 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
3000 output[i & eeprom_size_mask] = 0x03, i++;
3001 for (j = 0; j < serial_size; j++)
3003 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
3004 output[i & eeprom_size_mask] = 0x00, i++;
3007 // Legacy port name and PnP fields for FT2232 and newer chips
3008 if (ftdi->type > TYPE_BM)
3010 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
3012 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
3014 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
3018 output[0x13] = serial_size*2 + 2;
3020 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
3022 if (eeprom->use_serial)
3023 output[0x0A] |= USE_SERIAL_NUM;
3025 output[0x0A] &= ~USE_SERIAL_NUM;
3028 /* Bytes and Bits specific to (some) types
3029 Write linear, as this allows easier fixing*/
3035 output[0x0C] = eeprom->usb_version & 0xff;
3036 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3037 if (eeprom->use_usb_version)
3038 output[0x0A] |= USE_USB_VERSION_BIT;
3040 output[0x0A] &= ~USE_USB_VERSION_BIT;
3045 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
3046 if ( eeprom->channel_a_driver == DRIVER_VCP)
3047 output[0x00] |= DRIVER_VCP;
3049 output[0x00] &= ~DRIVER_VCP;
3051 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
3052 output[0x00] |= HIGH_CURRENT_DRIVE;
3054 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3056 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
3057 if ( eeprom->channel_b_driver == DRIVER_VCP)
3058 output[0x01] |= DRIVER_VCP;
3060 output[0x01] &= ~DRIVER_VCP;
3062 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
3063 output[0x01] |= HIGH_CURRENT_DRIVE;
3065 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3067 if (eeprom->in_is_isochronous)
3068 output[0x0A] |= 0x1;
3070 output[0x0A] &= ~0x1;
3071 if (eeprom->out_is_isochronous)
3072 output[0x0A] |= 0x2;
3074 output[0x0A] &= ~0x2;
3075 if (eeprom->suspend_pull_downs)
3076 output[0x0A] |= 0x4;
3078 output[0x0A] &= ~0x4;
3079 if (eeprom->use_usb_version)
3080 output[0x0A] |= USE_USB_VERSION_BIT;
3082 output[0x0A] &= ~USE_USB_VERSION_BIT;
3084 output[0x0C] = eeprom->usb_version & 0xff;
3085 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3086 output[0x14] = eeprom->chip;
3089 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
3090 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
3091 output[0x00] |= HIGH_CURRENT_DRIVE_R;
3092 if (eeprom->external_oscillator)
3093 output[0x00] |= 0x02;
3094 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
3096 if (eeprom->suspend_pull_downs)
3097 output[0x0A] |= 0x4;
3099 output[0x0A] &= ~0x4;
3100 output[0x0B] = eeprom->invert;
3101 output[0x0C] = eeprom->usb_version & 0xff;
3102 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3104 if (eeprom->cbus_function[0] > CBUS_BB_RD)
3105 output[0x14] = CBUS_TXLED;
3107 output[0x14] = eeprom->cbus_function[0];
3109 if (eeprom->cbus_function[1] > CBUS_BB_RD)
3110 output[0x14] |= CBUS_RXLED<<4;
3112 output[0x14] |= eeprom->cbus_function[1]<<4;
3114 if (eeprom->cbus_function[2] > CBUS_BB_RD)
3115 output[0x15] = CBUS_TXDEN;
3117 output[0x15] = eeprom->cbus_function[2];
3119 if (eeprom->cbus_function[3] > CBUS_BB_RD)
3120 output[0x15] |= CBUS_PWREN<<4;
3122 output[0x15] |= eeprom->cbus_function[3]<<4;
3124 if (eeprom->cbus_function[4] > CBUS_CLK6)
3125 output[0x16] = CBUS_SLEEP;
3127 output[0x16] = eeprom->cbus_function[4];
3130 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
3131 if ( eeprom->channel_a_driver == DRIVER_VCP)
3132 output[0x00] |= DRIVER_VCP;
3134 output[0x00] &= ~DRIVER_VCP;
3136 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
3137 if ( eeprom->channel_b_driver == DRIVER_VCP)
3138 output[0x01] |= DRIVER_VCP;
3140 output[0x01] &= ~DRIVER_VCP;
3141 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
3142 output[0x01] |= SUSPEND_DBUS7_BIT;
3144 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3146 if (eeprom->suspend_pull_downs)
3147 output[0x0A] |= 0x4;
3149 output[0x0A] &= ~0x4;
3151 if (eeprom->group0_drive > DRIVE_16MA)
3152 output[0x0c] |= DRIVE_16MA;
3154 output[0x0c] |= eeprom->group0_drive;
3155 if (eeprom->group0_schmitt == IS_SCHMITT)
3156 output[0x0c] |= IS_SCHMITT;
3157 if (eeprom->group0_slew == SLOW_SLEW)
3158 output[0x0c] |= SLOW_SLEW;
3160 if (eeprom->group1_drive > DRIVE_16MA)
3161 output[0x0c] |= DRIVE_16MA<<4;
3163 output[0x0c] |= eeprom->group1_drive<<4;
3164 if (eeprom->group1_schmitt == IS_SCHMITT)
3165 output[0x0c] |= IS_SCHMITT<<4;
3166 if (eeprom->group1_slew == SLOW_SLEW)
3167 output[0x0c] |= SLOW_SLEW<<4;
3169 if (eeprom->group2_drive > DRIVE_16MA)
3170 output[0x0d] |= DRIVE_16MA;
3172 output[0x0d] |= eeprom->group2_drive;
3173 if (eeprom->group2_schmitt == IS_SCHMITT)
3174 output[0x0d] |= IS_SCHMITT;
3175 if (eeprom->group2_slew == SLOW_SLEW)
3176 output[0x0d] |= SLOW_SLEW;
3178 if (eeprom->group3_drive > DRIVE_16MA)
3179 output[0x0d] |= DRIVE_16MA<<4;
3181 output[0x0d] |= eeprom->group3_drive<<4;
3182 if (eeprom->group3_schmitt == IS_SCHMITT)
3183 output[0x0d] |= IS_SCHMITT<<4;
3184 if (eeprom->group3_slew == SLOW_SLEW)
3185 output[0x0d] |= SLOW_SLEW<<4;
3187 output[0x18] = eeprom->chip;
3191 if (eeprom->channel_a_driver == DRIVER_VCP)
3192 output[0x00] |= DRIVER_VCP;
3194 output[0x00] &= ~DRIVER_VCP;
3195 if (eeprom->channel_b_driver == DRIVER_VCP)
3196 output[0x01] |= DRIVER_VCP;
3198 output[0x01] &= ~DRIVER_VCP;
3199 if (eeprom->channel_c_driver == DRIVER_VCP)
3200 output[0x00] |= (DRIVER_VCP << 4);
3202 output[0x00] &= ~(DRIVER_VCP << 4);
3203 if (eeprom->channel_d_driver == DRIVER_VCP)
3204 output[0x01] |= (DRIVER_VCP << 4);
3206 output[0x01] &= ~(DRIVER_VCP << 4);
3208 if (eeprom->suspend_pull_downs)
3209 output[0x0a] |= 0x4;
3211 output[0x0a] &= ~0x4;
3213 if (eeprom->channel_a_rs485enable)
3214 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3216 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3217 if (eeprom->channel_b_rs485enable)
3218 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3220 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3221 if (eeprom->channel_c_rs485enable)
3222 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3224 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3225 if (eeprom->channel_d_rs485enable)
3226 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3228 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3230 if (eeprom->group0_drive > DRIVE_16MA)
3231 output[0x0c] |= DRIVE_16MA;
3233 output[0x0c] |= eeprom->group0_drive;
3234 if (eeprom->group0_schmitt == IS_SCHMITT)
3235 output[0x0c] |= IS_SCHMITT;
3236 if (eeprom->group0_slew == SLOW_SLEW)
3237 output[0x0c] |= SLOW_SLEW;
3239 if (eeprom->group1_drive > DRIVE_16MA)
3240 output[0x0c] |= DRIVE_16MA<<4;
3242 output[0x0c] |= eeprom->group1_drive<<4;
3243 if (eeprom->group1_schmitt == IS_SCHMITT)
3244 output[0x0c] |= IS_SCHMITT<<4;
3245 if (eeprom->group1_slew == SLOW_SLEW)
3246 output[0x0c] |= SLOW_SLEW<<4;
3248 if (eeprom->group2_drive > DRIVE_16MA)
3249 output[0x0d] |= DRIVE_16MA;
3251 output[0x0d] |= eeprom->group2_drive;
3252 if (eeprom->group2_schmitt == IS_SCHMITT)
3253 output[0x0d] |= IS_SCHMITT;
3254 if (eeprom->group2_slew == SLOW_SLEW)
3255 output[0x0d] |= SLOW_SLEW;
3257 if (eeprom->group3_drive > DRIVE_16MA)
3258 output[0x0d] |= DRIVE_16MA<<4;
3260 output[0x0d] |= eeprom->group3_drive<<4;
3261 if (eeprom->group3_schmitt == IS_SCHMITT)
3262 output[0x0d] |= IS_SCHMITT<<4;
3263 if (eeprom->group3_slew == SLOW_SLEW)
3264 output[0x0d] |= SLOW_SLEW<<4;
3266 output[0x18] = eeprom->chip;
3270 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3271 if ( eeprom->channel_a_driver == DRIVER_VCP)
3272 output[0x00] |= DRIVER_VCPH;
3274 output[0x00] &= ~DRIVER_VCPH;
3275 if (eeprom->powersave)
3276 output[0x01] |= POWER_SAVE_DISABLE_H;
3278 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3280 if (eeprom->suspend_pull_downs)
3281 output[0x0a] |= 0x4;
3283 output[0x0a] &= ~0x4;
3285 if (eeprom->clock_polarity)
3286 output[0x01] |= FT1284_CLK_IDLE_STATE;
3288 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3289 if (eeprom->data_order)
3290 output[0x01] |= FT1284_DATA_LSB;
3292 output[0x01] &= ~FT1284_DATA_LSB;
3293 if (eeprom->flow_control)
3294 output[0x01] |= FT1284_FLOW_CONTROL;
3296 output[0x01] &= ~FT1284_FLOW_CONTROL;
3297 if (eeprom->group0_drive > DRIVE_16MA)
3298 output[0x0c] |= DRIVE_16MA;
3300 output[0x0c] |= eeprom->group0_drive;
3301 if (eeprom->group0_schmitt == IS_SCHMITT)
3302 output[0x0c] |= IS_SCHMITT;
3303 if (eeprom->group0_slew == SLOW_SLEW)
3304 output[0x0c] |= SLOW_SLEW;
3306 if (eeprom->group1_drive > DRIVE_16MA)
3307 output[0x0d] |= DRIVE_16MA;
3309 output[0x0d] |= eeprom->group1_drive;
3310 if (eeprom->group1_schmitt == IS_SCHMITT)
3311 output[0x0d] |= IS_SCHMITT;
3312 if (eeprom->group1_slew == SLOW_SLEW)
3313 output[0x0d] |= SLOW_SLEW;
3315 set_ft232h_cbus(eeprom, output);
3317 output[0x1e] = eeprom->chip;
3318 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3321 output[0x00] = 0x80; /* Actually, leave the default value */
3322 /*FIXME: Make DBUS & CBUS Control configurable*/
3323 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3324 for (j = 0; j <= 6; j++)
3326 output[0x1a + j] = eeprom->cbus_function[j];
3328 output[0x0b] = eeprom->invert;
3332 /* First address without use */
3352 /* Arbitrary user data */
3353 if (eeprom->user_data && eeprom->user_data_size >= 0)
3355 if (eeprom->user_data_addr < free_start)
3356 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3357 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3358 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3359 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3360 ftdi_error_return(-1,"eeprom size exceeded");
3361 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3364 // calculate checksum
3367 for (i = 0; i < eeprom->size/2-1; i++)
3369 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3371 /* FT230X has a user section in the MTP which is not part of the checksum */
3374 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3376 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3377 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3383 value = output[i*2];
3384 value += output[(i*2)+1] << 8;
3386 checksum = value^checksum;
3387 checksum = (checksum << 1) | (checksum >> 15);
3390 output[eeprom->size-2] = checksum;
3391 output[eeprom->size-1] = checksum >> 8;
3393 eeprom->initialized_for_connected_device = 1;
3394 return user_area_size;
3396 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3399 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3401 static unsigned char bit2type(unsigned char bits)
3405 case 0: return CHANNEL_IS_UART;
3406 case 1: return CHANNEL_IS_FIFO;
3407 case 2: return CHANNEL_IS_OPTO;
3408 case 4: return CHANNEL_IS_CPU;
3409 case 8: return CHANNEL_IS_FT1284;
3411 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3416 /* Decode 230X / 232R type chips invert bits
3417 * Prints directly to stdout.
3419 static void print_inverted_bits(int invert)
3421 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3424 fprintf(stdout,"Inverted bits:");
3426 if ((invert & (1<<i)) == (1<<i))
3427 fprintf(stdout," %s",r_bits[i]);
3429 fprintf(stdout,"\n");
3432 Decode binary EEPROM image into an ftdi_eeprom structure.
3434 For FT-X devices use AN_201 FT-X MTP memory Configuration to decode.
3436 \param ftdi pointer to ftdi_context
3437 \param verbose Decode EEPROM on stdout
3440 \retval -1: something went wrong
3442 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3443 FIXME: Strings are malloc'ed here and should be freed somewhere
3445 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3448 unsigned short checksum, eeprom_checksum, value;
3449 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3451 struct ftdi_eeprom *eeprom;
3452 unsigned char *buf = NULL;
3455 ftdi_error_return(-1,"No context");
3456 if (ftdi->eeprom == NULL)
3457 ftdi_error_return(-1,"No eeprom structure");
3459 eeprom = ftdi->eeprom;
3460 eeprom_size = eeprom->size;
3461 buf = ftdi->eeprom->buf;
3463 // Addr 02: Vendor ID
3464 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3466 // Addr 04: Product ID
3467 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3469 // Addr 06: Device release number
3470 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3472 // Addr 08: Config descriptor
3474 // Bit 6: 1 if this device is self powered, 0 if bus powered
3475 // Bit 5: 1 if this device uses remote wakeup
3476 eeprom->self_powered = buf[0x08] & 0x40;
3477 eeprom->remote_wakeup = buf[0x08] & 0x20;
3479 // Addr 09: Max power consumption: max power = value * 2 mA
3480 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3482 // Addr 0A: Chip configuration
3483 // Bit 7: 0 - reserved
3484 // Bit 6: 0 - reserved
3485 // Bit 5: 0 - reserved
3486 // Bit 4: 1 - Change USB version on BM and 2232C
3487 // Bit 3: 1 - Use the serial number string
3488 // Bit 2: 1 - Enable suspend pull downs for lower power
3489 // Bit 1: 1 - Out EndPoint is Isochronous
3490 // Bit 0: 1 - In EndPoint is Isochronous
3492 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3493 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3494 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3495 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3496 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3498 // Addr 0C: USB version low byte when 0x0A
3499 // Addr 0D: USB version high byte when 0x0A
3500 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3502 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3503 // Addr 0F: Length of manufacturer string
3504 manufacturer_size = buf[0x0F]/2;
3505 if (eeprom->manufacturer)
3506 free(eeprom->manufacturer);
3507 if (manufacturer_size > 0)
3509 eeprom->manufacturer = (char *)malloc(manufacturer_size);
3510 if (eeprom->manufacturer)
3512 // Decode manufacturer
3513 i = buf[0x0E] & (eeprom_size -1); // offset
3514 for (j=0; j<manufacturer_size-1; j++)
3516 eeprom->manufacturer[j] = buf[2*j+i+2];
3518 eeprom->manufacturer[j] = '\0';
3521 else eeprom->manufacturer = NULL;
3523 // Addr 10: Offset of the product string + 0x80, calculated later
3524 // Addr 11: Length of product string
3525 if (eeprom->product)
3526 free(eeprom->product);
3527 product_size = buf[0x11]/2;
3528 if (product_size > 0)
3530 eeprom->product = (char *)malloc(product_size);
3531 if (eeprom->product)
3533 // Decode product name
3534 i = buf[0x10] & (eeprom_size -1); // offset
3535 for (j=0; j<product_size-1; j++)
3537 eeprom->product[j] = buf[2*j+i+2];
3539 eeprom->product[j] = '\0';
3542 else eeprom->product = NULL;
3544 // Addr 12: Offset of the serial string + 0x80, calculated later
3545 // Addr 13: Length of serial string
3547 free(eeprom->serial);
3548 serial_size = buf[0x13]/2;
3549 if (serial_size > 0)
3551 eeprom->serial = (char *)malloc(serial_size);
3555 i = buf[0x12] & (eeprom_size -1); // offset
3556 for (j=0; j<serial_size-1; j++)
3558 eeprom->serial[j] = buf[2*j+i+2];
3560 eeprom->serial[j] = '\0';
3563 else eeprom->serial = NULL;
3568 for (i = 0; i < eeprom_size/2-1; i++)
3570 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3572 /* FT230X has a user section in the MTP which is not part of the checksum */
3576 value += buf[(i*2)+1] << 8;
3578 checksum = value^checksum;
3579 checksum = (checksum << 1) | (checksum >> 15);
3582 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3584 if (eeprom_checksum != checksum)
3586 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3587 ftdi_error_return(-1,"EEPROM checksum error");
3590 eeprom->channel_a_type = 0;
3591 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3595 else if (ftdi->type == TYPE_2232C)
3597 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3598 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3599 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3600 eeprom->channel_b_type = buf[0x01] & 0x7;
3601 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3602 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3603 eeprom->chip = buf[0x14];
3605 else if (ftdi->type == TYPE_R)
3607 /* TYPE_R flags D2XX, not VCP as all others*/
3608 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3609 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3610 eeprom->external_oscillator = buf[0x00] & 0x02;
3611 if ( (buf[0x01]&0x40) != 0x40)
3613 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3614 " If this happened with the\n"
3615 " EEPROM programmed by FTDI tools, please report "
3616 "to libftdi@developer.intra2net.com\n");
3618 eeprom->chip = buf[0x16];
3619 // Addr 0B: Invert data lines
3620 // Works only on FT232R, not FT245R, but no way to distinguish
3621 eeprom->invert = buf[0x0B];
3622 // Addr 14: CBUS function: CBUS0, CBUS1
3623 // Addr 15: CBUS function: CBUS2, CBUS3
3624 // Addr 16: CBUS function: CBUS5
3625 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3626 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3627 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3628 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3629 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3631 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3633 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3634 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3636 if (ftdi->type == TYPE_2232H)
3638 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3639 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3640 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3644 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3645 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3646 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3647 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3648 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3649 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3652 eeprom->chip = buf[0x18];
3653 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3654 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3655 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3656 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3657 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3658 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3659 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3660 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3661 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3662 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3663 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3664 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3666 else if (ftdi->type == TYPE_232H)
3668 eeprom->channel_a_type = buf[0x00] & 0xf;
3669 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3670 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3671 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3672 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3673 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3674 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3675 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3676 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3677 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3678 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3679 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3683 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3684 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3686 eeprom->chip = buf[0x1e];
3687 /*FIXME: Decipher more values*/
3689 else if (ftdi->type == TYPE_230X)
3693 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3695 eeprom->group0_drive = buf[0x0c] & 0x03;
3696 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3697 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3698 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3699 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3700 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3702 eeprom->invert = buf[0xb];
3707 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3708 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3709 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3710 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3712 if (eeprom->self_powered)
3713 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3715 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3716 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3717 if (eeprom->manufacturer)
3718 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3719 if (eeprom->product)
3720 fprintf(stdout, "Product: %s\n",eeprom->product);
3722 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3723 fprintf(stdout, "Checksum : %04x\n", checksum);
3724 if (ftdi->type == TYPE_R) {
3725 fprintf(stdout, "Internal EEPROM\n");
3726 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3728 else if (eeprom->chip >= 0x46)
3729 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3730 if (eeprom->suspend_dbus7)
3731 fprintf(stdout, "Suspend on DBUS7\n");
3732 if (eeprom->suspend_pull_downs)
3733 fprintf(stdout, "Pull IO pins low during suspend\n");
3734 if(eeprom->powersave)
3736 if(ftdi->type >= TYPE_232H)
3737 fprintf(stdout,"Enter low power state on ACBUS7\n");
3739 if (eeprom->remote_wakeup)
3740 fprintf(stdout, "Enable Remote Wake Up\n");
3741 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3742 if (ftdi->type >= TYPE_2232C)
3743 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3744 channel_mode[eeprom->channel_a_type],
3745 (eeprom->channel_a_driver)?" VCP":"",
3746 (eeprom->high_current_a)?" High Current IO":"");
3747 if (ftdi->type == TYPE_232H)
3749 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3750 (eeprom->clock_polarity)?"HIGH":"LOW",
3751 (eeprom->data_order)?"LSB":"MSB",
3752 (eeprom->flow_control)?"":"No ");
3754 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3755 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3756 channel_mode[eeprom->channel_b_type],
3757 (eeprom->channel_b_driver)?" VCP":"",
3758 (eeprom->high_current_b)?" High Current IO":"");
3759 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3760 eeprom->use_usb_version)
3761 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3763 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3765 fprintf(stdout,"%s has %d mA drive%s%s\n",
3766 (ftdi->type == TYPE_2232H)?"AL":"A",
3767 (eeprom->group0_drive+1) *4,
3768 (eeprom->group0_schmitt)?" Schmitt Input":"",
3769 (eeprom->group0_slew)?" Slow Slew":"");
3770 fprintf(stdout,"%s has %d mA drive%s%s\n",
3771 (ftdi->type == TYPE_2232H)?"AH":"B",
3772 (eeprom->group1_drive+1) *4,
3773 (eeprom->group1_schmitt)?" Schmitt Input":"",
3774 (eeprom->group1_slew)?" Slow Slew":"");
3775 fprintf(stdout,"%s has %d mA drive%s%s\n",
3776 (ftdi->type == TYPE_2232H)?"BL":"C",
3777 (eeprom->group2_drive+1) *4,
3778 (eeprom->group2_schmitt)?" Schmitt Input":"",
3779 (eeprom->group2_slew)?" Slow Slew":"");
3780 fprintf(stdout,"%s has %d mA drive%s%s\n",
3781 (ftdi->type == TYPE_2232H)?"BH":"D",
3782 (eeprom->group3_drive+1) *4,
3783 (eeprom->group3_schmitt)?" Schmitt Input":"",
3784 (eeprom->group3_slew)?" Slow Slew":"");
3786 else if (ftdi->type == TYPE_232H)
3788 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3789 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3790 "CLK30","CLK15","CLK7_5"
3792 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3793 (eeprom->group0_drive+1) *4,
3794 (eeprom->group0_schmitt)?" Schmitt Input":"",
3795 (eeprom->group0_slew)?" Slow Slew":"");
3796 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3797 (eeprom->group1_drive+1) *4,
3798 (eeprom->group1_schmitt)?" Schmitt Input":"",
3799 (eeprom->group1_slew)?" Slow Slew":"");
3800 for (i=0; i<10; i++)
3802 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3803 fprintf(stdout,"C%d Function: %s\n", i,
3804 cbush_mux[eeprom->cbus_function[i]]);
3807 else if (ftdi->type == TYPE_230X)
3809 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3810 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3811 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3812 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3813 "BBRD#", "TIME_STAMP", "AWAKE#",
3815 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3816 (eeprom->group0_drive+1) *4,
3817 (eeprom->group0_schmitt)?" Schmitt Input":"",
3818 (eeprom->group0_slew)?" Slow Slew":"");
3819 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3820 (eeprom->group1_drive+1) *4,
3821 (eeprom->group1_schmitt)?" Schmitt Input":"",
3822 (eeprom->group1_slew)?" Slow Slew":"");
3825 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3826 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3830 print_inverted_bits(eeprom->invert);
3833 if (ftdi->type == TYPE_R)
3835 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3836 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3837 "IOMODE","BB_WR","BB_RD"
3839 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3842 print_inverted_bits(eeprom->invert);
3846 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3847 fprintf(stdout,"C%d Function: %s\n", i,
3848 cbus_mux[eeprom->cbus_function[i]]);
3852 /* Running MPROG show that C0..3 have fixed function Synchronous
3854 fprintf(stdout,"C%d BB Function: %s\n", i,
3857 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3866 Get a value from the decoded EEPROM structure
3868 \param ftdi pointer to ftdi_context
3869 \param value_name Enum of the value to query
3870 \param value Pointer to store read value
3873 \retval -1: Value doesn't exist
3875 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3880 *value = ftdi->eeprom->vendor_id;
3883 *value = ftdi->eeprom->product_id;
3885 case RELEASE_NUMBER:
3886 *value = ftdi->eeprom->release_number;
3889 *value = ftdi->eeprom->self_powered;
3892 *value = ftdi->eeprom->remote_wakeup;
3895 *value = ftdi->eeprom->is_not_pnp;
3898 *value = ftdi->eeprom->suspend_dbus7;
3900 case IN_IS_ISOCHRONOUS:
3901 *value = ftdi->eeprom->in_is_isochronous;
3903 case OUT_IS_ISOCHRONOUS:
3904 *value = ftdi->eeprom->out_is_isochronous;
3906 case SUSPEND_PULL_DOWNS:
3907 *value = ftdi->eeprom->suspend_pull_downs;
3910 *value = ftdi->eeprom->use_serial;
3913 *value = ftdi->eeprom->usb_version;
3915 case USE_USB_VERSION:
3916 *value = ftdi->eeprom->use_usb_version;
3919 *value = ftdi->eeprom->max_power;
3921 case CHANNEL_A_TYPE:
3922 *value = ftdi->eeprom->channel_a_type;
3924 case CHANNEL_B_TYPE:
3925 *value = ftdi->eeprom->channel_b_type;
3927 case CHANNEL_A_DRIVER:
3928 *value = ftdi->eeprom->channel_a_driver;
3930 case CHANNEL_B_DRIVER:
3931 *value = ftdi->eeprom->channel_b_driver;
3933 case CHANNEL_C_DRIVER:
3934 *value = ftdi->eeprom->channel_c_driver;
3936 case CHANNEL_D_DRIVER:
3937 *value = ftdi->eeprom->channel_d_driver;
3939 case CHANNEL_A_RS485:
3940 *value = ftdi->eeprom->channel_a_rs485enable;
3942 case CHANNEL_B_RS485:
3943 *value = ftdi->eeprom->channel_b_rs485enable;
3945 case CHANNEL_C_RS485:
3946 *value = ftdi->eeprom->channel_c_rs485enable;
3948 case CHANNEL_D_RS485:
3949 *value = ftdi->eeprom->channel_d_rs485enable;
3951 case CBUS_FUNCTION_0:
3952 *value = ftdi->eeprom->cbus_function[0];
3954 case CBUS_FUNCTION_1:
3955 *value = ftdi->eeprom->cbus_function[1];
3957 case CBUS_FUNCTION_2:
3958 *value = ftdi->eeprom->cbus_function[2];
3960 case CBUS_FUNCTION_3:
3961 *value = ftdi->eeprom->cbus_function[3];
3963 case CBUS_FUNCTION_4:
3964 *value = ftdi->eeprom->cbus_function[4];
3966 case CBUS_FUNCTION_5:
3967 *value = ftdi->eeprom->cbus_function[5];
3969 case CBUS_FUNCTION_6:
3970 *value = ftdi->eeprom->cbus_function[6];
3972 case CBUS_FUNCTION_7:
3973 *value = ftdi->eeprom->cbus_function[7];
3975 case CBUS_FUNCTION_8:
3976 *value = ftdi->eeprom->cbus_function[8];
3978 case CBUS_FUNCTION_9:
3979 *value = ftdi->eeprom->cbus_function[9];
3982 *value = ftdi->eeprom->high_current;
3984 case HIGH_CURRENT_A:
3985 *value = ftdi->eeprom->high_current_a;
3987 case HIGH_CURRENT_B:
3988 *value = ftdi->eeprom->high_current_b;
3991 *value = ftdi->eeprom->invert;
3994 *value = ftdi->eeprom->group0_drive;
3996 case GROUP0_SCHMITT:
3997 *value = ftdi->eeprom->group0_schmitt;
4000 *value = ftdi->eeprom->group0_slew;
4003 *value = ftdi->eeprom->group1_drive;
4005 case GROUP1_SCHMITT:
4006 *value = ftdi->eeprom->group1_schmitt;
4009 *value = ftdi->eeprom->group1_slew;
4012 *value = ftdi->eeprom->group2_drive;
4014 case GROUP2_SCHMITT:
4015 *value = ftdi->eeprom->group2_schmitt;
4018 *value = ftdi->eeprom->group2_slew;
4021 *value = ftdi->eeprom->group3_drive;
4023 case GROUP3_SCHMITT:
4024 *value = ftdi->eeprom->group3_schmitt;
4027 *value = ftdi->eeprom->group3_slew;
4030 *value = ftdi->eeprom->powersave;
4032 case CLOCK_POLARITY:
4033 *value = ftdi->eeprom->clock_polarity;
4036 *value = ftdi->eeprom->data_order;
4039 *value = ftdi->eeprom->flow_control;
4042 *value = ftdi->eeprom->chip;
4045 *value = ftdi->eeprom->size;
4047 case EXTERNAL_OSCILLATOR:
4048 *value = ftdi->eeprom->external_oscillator;
4051 ftdi_error_return(-1, "Request for unknown EEPROM value");
4057 Set a value in the decoded EEPROM Structure
4058 No parameter checking is performed
4060 \param ftdi pointer to ftdi_context
4061 \param value_name Enum of the value to set
4065 \retval -1: Value doesn't exist
4066 \retval -2: Value not user settable
4068 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
4073 ftdi->eeprom->vendor_id = value;
4076 ftdi->eeprom->product_id = value;
4078 case RELEASE_NUMBER:
4079 ftdi->eeprom->release_number = value;
4082 ftdi->eeprom->self_powered = value;
4085 ftdi->eeprom->remote_wakeup = value;
4088 ftdi->eeprom->is_not_pnp = value;
4091 ftdi->eeprom->suspend_dbus7 = value;
4093 case IN_IS_ISOCHRONOUS:
4094 ftdi->eeprom->in_is_isochronous = value;
4096 case OUT_IS_ISOCHRONOUS:
4097 ftdi->eeprom->out_is_isochronous = value;
4099 case SUSPEND_PULL_DOWNS:
4100 ftdi->eeprom->suspend_pull_downs = value;
4103 ftdi->eeprom->use_serial = value;
4106 ftdi->eeprom->usb_version = value;
4108 case USE_USB_VERSION:
4109 ftdi->eeprom->use_usb_version = value;
4112 ftdi->eeprom->max_power = value;
4114 case CHANNEL_A_TYPE:
4115 ftdi->eeprom->channel_a_type = value;
4117 case CHANNEL_B_TYPE:
4118 ftdi->eeprom->channel_b_type = value;
4120 case CHANNEL_A_DRIVER:
4121 ftdi->eeprom->channel_a_driver = value;
4123 case CHANNEL_B_DRIVER:
4124 ftdi->eeprom->channel_b_driver = value;
4126 case CHANNEL_C_DRIVER:
4127 ftdi->eeprom->channel_c_driver = value;
4129 case CHANNEL_D_DRIVER:
4130 ftdi->eeprom->channel_d_driver = value;
4132 case CHANNEL_A_RS485:
4133 ftdi->eeprom->channel_a_rs485enable = value;
4135 case CHANNEL_B_RS485:
4136 ftdi->eeprom->channel_b_rs485enable = value;
4138 case CHANNEL_C_RS485:
4139 ftdi->eeprom->channel_c_rs485enable = value;
4141 case CHANNEL_D_RS485:
4142 ftdi->eeprom->channel_d_rs485enable = value;
4144 case CBUS_FUNCTION_0:
4145 ftdi->eeprom->cbus_function[0] = value;
4147 case CBUS_FUNCTION_1:
4148 ftdi->eeprom->cbus_function[1] = value;
4150 case CBUS_FUNCTION_2:
4151 ftdi->eeprom->cbus_function[2] = value;
4153 case CBUS_FUNCTION_3:
4154 ftdi->eeprom->cbus_function[3] = value;
4156 case CBUS_FUNCTION_4:
4157 ftdi->eeprom->cbus_function[4] = value;
4159 case CBUS_FUNCTION_5:
4160 ftdi->eeprom->cbus_function[5] = value;
4162 case CBUS_FUNCTION_6:
4163 ftdi->eeprom->cbus_function[6] = value;
4165 case CBUS_FUNCTION_7:
4166 ftdi->eeprom->cbus_function[7] = value;
4168 case CBUS_FUNCTION_8:
4169 ftdi->eeprom->cbus_function[8] = value;
4171 case CBUS_FUNCTION_9:
4172 ftdi->eeprom->cbus_function[9] = value;
4175 ftdi->eeprom->high_current = value;
4177 case HIGH_CURRENT_A:
4178 ftdi->eeprom->high_current_a = value;
4180 case HIGH_CURRENT_B:
4181 ftdi->eeprom->high_current_b = value;
4184 ftdi->eeprom->invert = value;
4187 ftdi->eeprom->group0_drive = value;
4189 case GROUP0_SCHMITT:
4190 ftdi->eeprom->group0_schmitt = value;
4193 ftdi->eeprom->group0_slew = value;
4196 ftdi->eeprom->group1_drive = value;
4198 case GROUP1_SCHMITT:
4199 ftdi->eeprom->group1_schmitt = value;
4202 ftdi->eeprom->group1_slew = value;
4205 ftdi->eeprom->group2_drive = value;
4207 case GROUP2_SCHMITT:
4208 ftdi->eeprom->group2_schmitt = value;
4211 ftdi->eeprom->group2_slew = value;
4214 ftdi->eeprom->group3_drive = value;
4216 case GROUP3_SCHMITT:
4217 ftdi->eeprom->group3_schmitt = value;
4220 ftdi->eeprom->group3_slew = value;
4223 ftdi->eeprom->chip = value;
4226 ftdi->eeprom->powersave = value;
4228 case CLOCK_POLARITY:
4229 ftdi->eeprom->clock_polarity = value;
4232 ftdi->eeprom->data_order = value;
4235 ftdi->eeprom->flow_control = value;
4238 ftdi_error_return(-2, "EEPROM Value can't be changed");
4240 case EXTERNAL_OSCILLATOR:
4241 ftdi->eeprom->external_oscillator = value;
4243 case USER_DATA_ADDR:
4244 ftdi->eeprom->user_data_addr = value;
4248 ftdi_error_return(-1, "Request to unknown EEPROM value");
4250 ftdi->eeprom->initialized_for_connected_device = 0;
4254 /** Get the read-only buffer to the binary EEPROM content
4256 \param ftdi pointer to ftdi_context
4257 \param buf buffer to receive EEPROM content
4258 \param size Size of receiving buffer
4261 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
4262 \retval -2: Not enough room to store eeprom
4264 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4266 if (!ftdi || !(ftdi->eeprom))
4267 ftdi_error_return(-1, "No appropriate structure");
4269 if (!buf || size < ftdi->eeprom->size)
4270 ftdi_error_return(-1, "Not enough room to store eeprom");
4272 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4273 if (size > FTDI_MAX_EEPROM_SIZE)
4274 size = FTDI_MAX_EEPROM_SIZE;
4276 memcpy(buf, ftdi->eeprom->buf, size);
4281 /** Set the EEPROM content from the user-supplied prefilled buffer
4283 \param ftdi pointer to ftdi_context
4284 \param buf buffer to read EEPROM content
4285 \param size Size of buffer
4288 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4290 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4292 if (!ftdi || !(ftdi->eeprom) || !buf)
4293 ftdi_error_return(-1, "No appropriate structure");
4295 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4296 if (size > FTDI_MAX_EEPROM_SIZE)
4297 size = FTDI_MAX_EEPROM_SIZE;
4299 memcpy(ftdi->eeprom->buf, buf, size);
4304 /** Set the EEPROM user data content from the user-supplied prefilled buffer
4306 \param ftdi pointer to ftdi_context
4307 \param buf buffer to read EEPROM user data content
4308 \param size Size of buffer
4311 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4313 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4315 if (!ftdi || !(ftdi->eeprom) || !buf)
4316 ftdi_error_return(-1, "No appropriate structure");
4318 ftdi->eeprom->user_data_size = size;
4319 ftdi->eeprom->user_data = buf;
4324 Read eeprom location
4326 \param ftdi pointer to ftdi_context
4327 \param eeprom_addr Address of eeprom location to be read
4328 \param eeprom_val Pointer to store read eeprom location
4331 \retval -1: read failed
4332 \retval -2: USB device unavailable
4334 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4336 unsigned char buf[2];
4338 if (ftdi == NULL || ftdi->usb_dev == NULL)
4339 ftdi_error_return(-2, "USB device unavailable");
4341 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)
4342 ftdi_error_return(-1, "reading eeprom failed");
4344 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
4352 \param ftdi pointer to ftdi_context
4355 \retval -1: read failed
4356 \retval -2: USB device unavailable
4358 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4363 if (ftdi == NULL || ftdi->usb_dev == NULL)
4364 ftdi_error_return(-2, "USB device unavailable");
4365 buf = ftdi->eeprom->buf;
4367 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4369 if (libusb_control_transfer(
4370 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4371 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4372 ftdi_error_return(-1, "reading eeprom failed");
4375 if (ftdi->type == TYPE_R)
4376 ftdi->eeprom->size = 0x80;
4377 /* Guesses size of eeprom by comparing halves
4378 - will not work with blank eeprom */
4379 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4380 ftdi->eeprom->size = -1;
4381 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4382 ftdi->eeprom->size = 0x80;
4383 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4384 ftdi->eeprom->size = 0x40;
4386 ftdi->eeprom->size = 0x100;
4391 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4392 Function is only used internally
4395 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4397 return ((value & 1) << 1) |
4398 ((value & 2) << 5) |
4399 ((value & 4) >> 2) |
4400 ((value & 8) << 4) |
4401 ((value & 16) >> 1) |
4402 ((value & 32) >> 1) |
4403 ((value & 64) >> 4) |
4404 ((value & 128) >> 2);
4408 Read the FTDIChip-ID from R-type devices
4410 \param ftdi pointer to ftdi_context
4411 \param chipid Pointer to store FTDIChip-ID
4414 \retval -1: read failed
4415 \retval -2: USB device unavailable
4417 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4419 unsigned int a = 0, b = 0;
4421 if (ftdi == NULL || ftdi->usb_dev == NULL)
4422 ftdi_error_return(-2, "USB device unavailable");
4424 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)
4426 a = a << 8 | a >> 8;
4427 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)
4429 b = b << 8 | b >> 8;
4430 a = (a << 16) | (b & 0xFFFF);
4431 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4432 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4433 *chipid = a ^ 0xa5f0f7d1;
4438 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4442 Write eeprom location
4444 \param ftdi pointer to ftdi_context
4445 \param eeprom_addr Address of eeprom location to be written
4446 \param eeprom_val Value to be written
4449 \retval -1: write failed
4450 \retval -2: USB device unavailable
4451 \retval -3: Invalid access to checksum protected area below 0x80
4452 \retval -4: Device can't access unprotected area
4453 \retval -5: Reading chip type failed
4455 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4456 unsigned short eeprom_val)
4458 int chip_type_location;
4459 unsigned short chip_type;
4461 if (ftdi == NULL || ftdi->usb_dev == NULL)
4462 ftdi_error_return(-2, "USB device unavailable");
4464 if (eeprom_addr <0x80)
4465 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4472 chip_type_location = 0x14;
4476 chip_type_location = 0x18;
4479 chip_type_location = 0x1e;
4482 ftdi_error_return(-4, "Device can't access unprotected area");
4485 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4486 ftdi_error_return(-5, "Reading failed");
4487 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4488 if ((chip_type & 0xff) != 0x66)
4490 ftdi_error_return(-6, "EEPROM is not of 93x66");
4493 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4494 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4495 NULL, 0, ftdi->usb_write_timeout) != 0)
4496 ftdi_error_return(-1, "unable to write eeprom");
4504 \param ftdi pointer to ftdi_context
4507 \retval -1: read failed
4508 \retval -2: USB device unavailable
4509 \retval -3: EEPROM not initialized for the connected device;
4511 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4513 unsigned short usb_val, status;
4515 unsigned char *eeprom;
4517 if (ftdi == NULL || ftdi->usb_dev == NULL)
4518 ftdi_error_return(-2, "USB device unavailable");
4520 if(ftdi->eeprom->initialized_for_connected_device == 0)
4521 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4523 eeprom = ftdi->eeprom->buf;
4525 /* These commands were traced while running MProg */
4526 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4528 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4530 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4533 for (i = 0; i < ftdi->eeprom->size/2; i++)
4535 /* Do not try to write to reserved area */
4536 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4540 usb_val = eeprom[i*2];
4541 usb_val += eeprom[(i*2)+1] << 8;
4542 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4543 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4544 NULL, 0, ftdi->usb_write_timeout) < 0)
4545 ftdi_error_return(-1, "unable to write eeprom");
4554 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4556 \param ftdi pointer to ftdi_context
4559 \retval -1: erase failed
4560 \retval -2: USB device unavailable
4561 \retval -3: Writing magic failed
4562 \retval -4: Read EEPROM failed
4563 \retval -5: Unexpected EEPROM value
4565 #define MAGIC 0x55aa
4566 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4568 unsigned short eeprom_value;
4569 if (ftdi == NULL || ftdi->usb_dev == NULL)
4570 ftdi_error_return(-2, "USB device unavailable");
4572 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4574 ftdi->eeprom->chip = 0;
4578 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4579 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4580 ftdi_error_return(-1, "unable to erase eeprom");
4583 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4584 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4585 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4586 Chip is 93x66 if magic is only read at word position 0xc0*/
4587 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4588 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4589 NULL, 0, ftdi->usb_write_timeout) != 0)
4590 ftdi_error_return(-3, "Writing magic failed");
4591 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4592 ftdi_error_return(-4, "Reading failed");
4593 if (eeprom_value == MAGIC)
4595 ftdi->eeprom->chip = 0x46;
4599 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4600 ftdi_error_return(-4, "Reading failed");
4601 if (eeprom_value == MAGIC)
4602 ftdi->eeprom->chip = 0x56;
4605 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4606 ftdi_error_return(-4, "Reading failed");
4607 if (eeprom_value == MAGIC)
4608 ftdi->eeprom->chip = 0x66;
4611 ftdi->eeprom->chip = -1;
4615 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4616 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4617 ftdi_error_return(-1, "unable to erase eeprom");
4622 Get string representation for last error code
4624 \param ftdi pointer to ftdi_context
4626 \retval Pointer to error string
4628 const char *ftdi_get_error_string (struct ftdi_context *ftdi)
4633 return ftdi->error_str;
4636 /* @} end of doxygen libftdi group */