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;
618 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
619 ftdi_error_return(-12, "libusb_get_configuration () failed");
620 // set configuration (needed especially for windows)
621 // tolerate EBUSY: one device with one configuration, but two interfaces
622 // and libftdi sessions to both interfaces (e.g. FT2232)
623 if (desc.bNumConfigurations > 0 && cfg != cfg0)
625 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
627 ftdi_usb_close_internal (ftdi);
628 if (detach_errno == EPERM)
630 ftdi_error_return(-8, "inappropriate permissions on device!");
634 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
639 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
641 ftdi_usb_close_internal (ftdi);
642 if (detach_errno == EPERM)
644 ftdi_error_return(-8, "inappropriate permissions on device!");
648 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
652 if (ftdi_usb_reset (ftdi) != 0)
654 ftdi_usb_close_internal (ftdi);
655 ftdi_error_return(-6, "ftdi_usb_reset failed");
658 // Try to guess chip type
659 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
660 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
661 && desc.iSerialNumber == 0))
662 ftdi->type = TYPE_BM;
663 else if (desc.bcdDevice == 0x200)
664 ftdi->type = TYPE_AM;
665 else if (desc.bcdDevice == 0x500)
666 ftdi->type = TYPE_2232C;
667 else if (desc.bcdDevice == 0x600)
669 else if (desc.bcdDevice == 0x700)
670 ftdi->type = TYPE_2232H;
671 else if (desc.bcdDevice == 0x800)
672 ftdi->type = TYPE_4232H;
673 else if (desc.bcdDevice == 0x900)
674 ftdi->type = TYPE_232H;
675 else if (desc.bcdDevice == 0x1000)
676 ftdi->type = TYPE_230X;
678 // Determine maximum packet size
679 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
681 if (ftdi_set_baudrate (ftdi, 9600) != 0)
683 ftdi_usb_close_internal (ftdi);
684 ftdi_error_return(-7, "set baudrate failed");
687 ftdi_error_return(0, "all fine");
691 Opens the first device with a given vendor and product ids.
693 \param ftdi pointer to ftdi_context
694 \param vendor Vendor ID
695 \param product Product ID
697 \retval same as ftdi_usb_open_desc()
699 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
701 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
705 Opens the first device with a given, vendor id, product id,
706 description and serial.
708 \param ftdi pointer to ftdi_context
709 \param vendor Vendor ID
710 \param product Product ID
711 \param description Description to search for. Use NULL if not needed.
712 \param serial Serial to search for. Use NULL if not needed.
715 \retval -3: usb device not found
716 \retval -4: unable to open device
717 \retval -5: unable to claim device
718 \retval -6: reset failed
719 \retval -7: set baudrate failed
720 \retval -8: get product description failed
721 \retval -9: get serial number failed
722 \retval -12: libusb_get_device_list() failed
723 \retval -13: libusb_get_device_descriptor() failed
725 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
726 const char* description, const char* serial)
728 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
732 Opens the index-th device with a given, vendor id, product id,
733 description and serial.
735 \param ftdi pointer to ftdi_context
736 \param vendor Vendor ID
737 \param product Product ID
738 \param description Description to search for. Use NULL if not needed.
739 \param serial Serial to search for. Use NULL if not needed.
740 \param index Number of matching device to open if there are more than one, starts with 0.
743 \retval -1: usb_find_busses() failed
744 \retval -2: usb_find_devices() failed
745 \retval -3: usb device not found
746 \retval -4: unable to open device
747 \retval -5: unable to claim device
748 \retval -6: reset failed
749 \retval -7: set baudrate failed
750 \retval -8: get product description failed
751 \retval -9: get serial number failed
752 \retval -10: unable to close device
753 \retval -11: ftdi context invalid
754 \retval -12: libusb_get_device_list() failed
756 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
757 const char* description, const char* serial, unsigned int index)
760 libusb_device **devs;
765 ftdi_error_return(-11, "ftdi context invalid");
767 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
768 ftdi_error_return(-12, "libusb_get_device_list() failed");
770 while ((dev = devs[i++]) != NULL)
772 struct libusb_device_descriptor desc;
775 if (libusb_get_device_descriptor(dev, &desc) < 0)
776 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
778 if (desc.idVendor == vendor && desc.idProduct == product)
780 if (libusb_open(dev, &ftdi->usb_dev) < 0)
781 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
783 if (description != NULL)
785 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
787 ftdi_usb_close_internal (ftdi);
788 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
790 if (strncmp(string, description, sizeof(string)) != 0)
792 ftdi_usb_close_internal (ftdi);
798 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
800 ftdi_usb_close_internal (ftdi);
801 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
803 if (strncmp(string, serial, sizeof(string)) != 0)
805 ftdi_usb_close_internal (ftdi);
810 ftdi_usb_close_internal (ftdi);
818 res = ftdi_usb_open_dev(ftdi, dev);
819 libusb_free_device_list(devs,1);
825 ftdi_error_return_free_device_list(-3, "device not found", devs);
829 Opens the device at a given USB bus and device address.
831 \param ftdi pointer to ftdi_context
832 \param bus Bus number
833 \param addr Device address
836 \retval -1: usb_find_busses() failed
837 \retval -2: usb_find_devices() failed
838 \retval -3: usb device not found
839 \retval -4: unable to open device
840 \retval -5: unable to claim device
841 \retval -6: reset failed
842 \retval -7: set baudrate failed
843 \retval -8: get product description failed
844 \retval -9: get serial number failed
845 \retval -10: unable to close device
846 \retval -11: ftdi context invalid
847 \retval -12: libusb_get_device_list() failed
849 int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
852 libusb_device **devs;
856 ftdi_error_return(-11, "ftdi context invalid");
858 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
859 ftdi_error_return(-12, "libusb_get_device_list() failed");
861 while ((dev = devs[i++]) != NULL)
863 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
866 res = ftdi_usb_open_dev(ftdi, dev);
867 libusb_free_device_list(devs,1);
873 ftdi_error_return_free_device_list(-3, "device not found", devs);
877 Opens the ftdi-device described by a description-string.
878 Intended to be used for parsing a device-description given as commandline argument.
880 \param ftdi pointer to ftdi_context
881 \param description NULL-terminated description-string, using this format:
882 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
883 \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")
884 \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
885 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
887 \note The description format may be extended in later versions.
890 \retval -2: libusb_get_device_list() failed
891 \retval -3: usb device not found
892 \retval -4: unable to open device
893 \retval -5: unable to claim device
894 \retval -6: reset failed
895 \retval -7: set baudrate failed
896 \retval -8: get product description failed
897 \retval -9: get serial number failed
898 \retval -10: unable to close device
899 \retval -11: illegal description format
900 \retval -12: ftdi context invalid
902 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
905 ftdi_error_return(-12, "ftdi context invalid");
907 if (description[0] == 0 || description[1] != ':')
908 ftdi_error_return(-11, "illegal description format");
910 if (description[0] == 'd')
913 libusb_device **devs;
914 unsigned int bus_number, device_address;
917 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
918 ftdi_error_return(-2, "libusb_get_device_list() failed");
920 /* XXX: This doesn't handle symlinks/odd paths/etc... */
921 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
922 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
924 while ((dev = devs[i++]) != NULL)
927 if (bus_number == libusb_get_bus_number (dev)
928 && device_address == libusb_get_device_address (dev))
930 ret = ftdi_usb_open_dev(ftdi, dev);
931 libusb_free_device_list(devs,1);
937 ftdi_error_return_free_device_list(-3, "device not found", devs);
939 else if (description[0] == 'i' || description[0] == 's')
942 unsigned int product;
943 unsigned int index=0;
944 const char *serial=NULL;
945 const char *startp, *endp;
948 startp=description+2;
949 vendor=strtoul((char*)startp,(char**)&endp,0);
950 if (*endp != ':' || endp == startp || errno != 0)
951 ftdi_error_return(-11, "illegal description format");
954 product=strtoul((char*)startp,(char**)&endp,0);
955 if (endp == startp || errno != 0)
956 ftdi_error_return(-11, "illegal description format");
958 if (description[0] == 'i' && *endp != 0)
960 /* optional index field in i-mode */
962 ftdi_error_return(-11, "illegal description format");
965 index=strtoul((char*)startp,(char**)&endp,0);
966 if (*endp != 0 || endp == startp || errno != 0)
967 ftdi_error_return(-11, "illegal description format");
969 if (description[0] == 's')
972 ftdi_error_return(-11, "illegal description format");
974 /* rest of the description is the serial */
978 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
982 ftdi_error_return(-11, "illegal description format");
987 Resets the ftdi device.
989 \param ftdi pointer to ftdi_context
992 \retval -1: FTDI reset failed
993 \retval -2: USB device unavailable
995 int ftdi_usb_reset(struct ftdi_context *ftdi)
997 if (ftdi == NULL || ftdi->usb_dev == NULL)
998 ftdi_error_return(-2, "USB device unavailable");
1000 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1001 SIO_RESET_REQUEST, SIO_RESET_SIO,
1002 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1003 ftdi_error_return(-1,"FTDI reset failed");
1005 // Invalidate data in the readbuffer
1006 ftdi->readbuffer_offset = 0;
1007 ftdi->readbuffer_remaining = 0;
1013 Clears the read buffer on the chip and the internal read buffer.
1015 \param ftdi pointer to ftdi_context
1018 \retval -1: read buffer purge failed
1019 \retval -2: USB device unavailable
1021 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
1023 if (ftdi == NULL || ftdi->usb_dev == NULL)
1024 ftdi_error_return(-2, "USB device unavailable");
1026 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1027 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
1028 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1029 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1031 // Invalidate data in the readbuffer
1032 ftdi->readbuffer_offset = 0;
1033 ftdi->readbuffer_remaining = 0;
1039 Clears the write buffer on the chip.
1041 \param ftdi pointer to ftdi_context
1044 \retval -1: write buffer purge failed
1045 \retval -2: USB device unavailable
1047 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
1049 if (ftdi == NULL || ftdi->usb_dev == NULL)
1050 ftdi_error_return(-2, "USB device unavailable");
1052 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1053 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
1054 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1055 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1061 Clears the buffers on the chip and the internal read buffer.
1063 \param ftdi pointer to ftdi_context
1066 \retval -1: read buffer purge failed
1067 \retval -2: write buffer purge failed
1068 \retval -3: USB device unavailable
1070 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
1074 if (ftdi == NULL || ftdi->usb_dev == NULL)
1075 ftdi_error_return(-3, "USB device unavailable");
1077 result = ftdi_usb_purge_rx_buffer(ftdi);
1081 result = ftdi_usb_purge_tx_buffer(ftdi);
1091 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
1093 \param ftdi pointer to ftdi_context
1096 \retval -1: usb_release failed
1097 \retval -3: ftdi context invalid
1099 int ftdi_usb_close(struct ftdi_context *ftdi)
1104 ftdi_error_return(-3, "ftdi context invalid");
1106 if (ftdi->usb_dev != NULL)
1107 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1110 ftdi_usb_close_internal (ftdi);
1115 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1116 to encoded divisor and the achievable baudrate
1117 Function is only used internally
1124 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1125 The fractional part has frac_code encoding
1127 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1130 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1131 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1132 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1133 int divisor, best_divisor, best_baud, best_baud_diff;
1135 divisor = 24000000 / baudrate;
1137 // Round down to supported fraction (AM only)
1138 divisor -= am_adjust_dn[divisor & 7];
1140 // Try this divisor and the one above it (because division rounds down)
1144 for (i = 0; i < 2; i++)
1146 int try_divisor = divisor + i;
1150 // Round up to supported divisor value
1151 if (try_divisor <= 8)
1153 // Round up to minimum supported divisor
1156 else if (divisor < 16)
1158 // AM doesn't support divisors 9 through 15 inclusive
1163 // Round up to supported fraction (AM only)
1164 try_divisor += am_adjust_up[try_divisor & 7];
1165 if (try_divisor > 0x1FFF8)
1167 // Round down to maximum supported divisor value (for AM)
1168 try_divisor = 0x1FFF8;
1171 // Get estimated baud rate (to nearest integer)
1172 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1173 // Get absolute difference from requested baud rate
1174 if (baud_estimate < baudrate)
1176 baud_diff = baudrate - baud_estimate;
1180 baud_diff = baud_estimate - baudrate;
1182 if (i == 0 || baud_diff < best_baud_diff)
1184 // Closest to requested baud rate so far
1185 best_divisor = try_divisor;
1186 best_baud = baud_estimate;
1187 best_baud_diff = baud_diff;
1190 // Spot on! No point trying
1195 // Encode the best divisor value
1196 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1197 // Deal with special cases for encoded value
1198 if (*encoded_divisor == 1)
1200 *encoded_divisor = 0; // 3000000 baud
1202 else if (*encoded_divisor == 0x4001)
1204 *encoded_divisor = 1; // 2000000 baud (BM only)
1209 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1210 to encoded divisor and the achievable baudrate
1211 Function is only used internally
1218 From /2, 0.125 steps may be taken.
1219 The fractional part has frac_code encoding
1221 value[13:0] of value is the divisor
1222 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1224 H Type have all features above with
1225 {index[8],value[15:14]} is the encoded subdivisor
1227 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1228 {index[0],value[15:14]} is the encoded subdivisor
1230 AM Type chips have only four fractional subdivisors at value[15:14]
1231 for subdivisors 0, 0.5, 0.25, 0.125
1233 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1235 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1237 int divisor, best_divisor;
1238 if (baudrate >= clk/clk_div)
1240 *encoded_divisor = 0;
1241 best_baud = clk/clk_div;
1243 else if (baudrate >= clk/(clk_div + clk_div/2))
1245 *encoded_divisor = 1;
1246 best_baud = clk/(clk_div + clk_div/2);
1248 else if (baudrate >= clk/(2*clk_div))
1250 *encoded_divisor = 2;
1251 best_baud = clk/(2*clk_div);
1255 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1256 divisor = clk*16/clk_div / baudrate;
1257 if (divisor & 1) /* Decide if to round up or down*/
1258 best_divisor = divisor /2 +1;
1260 best_divisor = divisor/2;
1261 if(best_divisor > 0x20000)
1262 best_divisor = 0x1ffff;
1263 best_baud = clk*16/clk_div/best_divisor;
1264 if (best_baud & 1) /* Decide if to round up or down*/
1265 best_baud = best_baud /2 +1;
1267 best_baud = best_baud /2;
1268 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1273 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1274 Function is only used internally
1277 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1278 unsigned short *value, unsigned short *index)
1281 unsigned long encoded_divisor;
1289 #define H_CLK 120000000
1290 #define C_CLK 48000000
1291 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1293 if(baudrate*10 > H_CLK /0x3fff)
1295 /* On H Devices, use 12 000 000 Baudrate when possible
1296 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1297 three fractional bits and a 120 MHz clock
1298 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1299 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1300 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1301 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1304 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1306 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
1308 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1312 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1314 // Split into "value" and "index" values
1315 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1316 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1318 *index = (unsigned short)(encoded_divisor >> 8);
1320 *index |= ftdi->index;
1323 *index = (unsigned short)(encoded_divisor >> 16);
1325 // Return the nearest baud rate
1330 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1331 * Do not use, it's only for the unit test framework
1333 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1334 unsigned short *value, unsigned short *index)
1336 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1340 Sets the chip baud rate
1342 \param ftdi pointer to ftdi_context
1343 \param baudrate baud rate to set
1346 \retval -1: invalid baudrate
1347 \retval -2: setting baudrate failed
1348 \retval -3: USB device unavailable
1350 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1352 unsigned short value, index;
1353 int actual_baudrate;
1355 if (ftdi == NULL || ftdi->usb_dev == NULL)
1356 ftdi_error_return(-3, "USB device unavailable");
1358 if (ftdi->bitbang_enabled)
1360 baudrate = baudrate*4;
1363 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1364 if (actual_baudrate <= 0)
1365 ftdi_error_return (-1, "Silly baudrate <= 0.");
1367 // Check within tolerance (about 5%)
1368 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1369 || ((actual_baudrate < baudrate)
1370 ? (actual_baudrate * 21 < baudrate * 20)
1371 : (baudrate * 21 < actual_baudrate * 20)))
1372 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1374 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1375 SIO_SET_BAUDRATE_REQUEST, value,
1376 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1377 ftdi_error_return (-2, "Setting new baudrate failed");
1379 ftdi->baudrate = baudrate;
1384 Set (RS232) line characteristics.
1385 The break type can only be set via ftdi_set_line_property2()
1386 and defaults to "off".
1388 \param ftdi pointer to ftdi_context
1389 \param bits Number of bits
1390 \param sbit Number of stop bits
1391 \param parity Parity mode
1394 \retval -1: Setting line property failed
1396 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1397 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1399 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1403 Set (RS232) line characteristics
1405 \param ftdi pointer to ftdi_context
1406 \param bits Number of bits
1407 \param sbit Number of stop bits
1408 \param parity Parity mode
1409 \param break_type Break type
1412 \retval -1: Setting line property failed
1413 \retval -2: USB device unavailable
1415 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1416 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1417 enum ftdi_break_type break_type)
1419 unsigned short value = bits;
1421 if (ftdi == NULL || ftdi->usb_dev == NULL)
1422 ftdi_error_return(-2, "USB device unavailable");
1427 value |= (0x00 << 8);
1430 value |= (0x01 << 8);
1433 value |= (0x02 << 8);
1436 value |= (0x03 << 8);
1439 value |= (0x04 << 8);
1446 value |= (0x00 << 11);
1449 value |= (0x01 << 11);
1452 value |= (0x02 << 11);
1459 value |= (0x00 << 14);
1462 value |= (0x01 << 14);
1466 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1467 SIO_SET_DATA_REQUEST, value,
1468 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1469 ftdi_error_return (-1, "Setting new line property failed");
1475 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1477 \param ftdi pointer to ftdi_context
1478 \param buf Buffer with the data
1479 \param size Size of the buffer
1481 \retval -666: USB device unavailable
1482 \retval <0: error code from usb_bulk_write()
1483 \retval >0: number of bytes written
1485 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1490 if (ftdi == NULL || ftdi->usb_dev == NULL)
1491 ftdi_error_return(-666, "USB device unavailable");
1493 while (offset < size)
1495 int write_size = ftdi->writebuffer_chunksize;
1497 if (offset+write_size > size)
1498 write_size = size-offset;
1500 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1501 ftdi_error_return(-1, "usb bulk write failed");
1503 offset += actual_length;
1509 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1511 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1512 struct ftdi_context *ftdi = tc->ftdi;
1513 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1515 packet_size = ftdi->max_packet_size;
1517 actual_length = transfer->actual_length;
1519 if (actual_length > 2)
1521 // skip FTDI status bytes.
1522 // Maybe stored in the future to enable modem use
1523 num_of_chunks = actual_length / packet_size;
1524 chunk_remains = actual_length % packet_size;
1525 //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);
1527 ftdi->readbuffer_offset += 2;
1530 if (actual_length > packet_size - 2)
1532 for (i = 1; i < num_of_chunks; i++)
1533 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1534 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1536 if (chunk_remains > 2)
1538 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1539 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1541 actual_length -= 2*num_of_chunks;
1544 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1547 if (actual_length > 0)
1549 // data still fits in buf?
1550 if (tc->offset + actual_length <= tc->size)
1552 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1553 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1554 tc->offset += actual_length;
1556 ftdi->readbuffer_offset = 0;
1557 ftdi->readbuffer_remaining = 0;
1559 /* Did we read exactly the right amount of bytes? */
1560 if (tc->offset == tc->size)
1562 //printf("read_data exact rem %d offset %d\n",
1563 //ftdi->readbuffer_remaining, offset);
1570 // only copy part of the data or size <= readbuffer_chunksize
1571 int part_size = tc->size - tc->offset;
1572 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1573 tc->offset += part_size;
1575 ftdi->readbuffer_offset += part_size;
1576 ftdi->readbuffer_remaining = actual_length - part_size;
1578 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1579 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1586 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1587 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1590 ret = libusb_submit_transfer (transfer);
1597 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1599 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1600 struct ftdi_context *ftdi = tc->ftdi;
1602 tc->offset += transfer->actual_length;
1604 if (tc->offset == tc->size)
1610 int write_size = ftdi->writebuffer_chunksize;
1613 if (tc->offset + write_size > tc->size)
1614 write_size = tc->size - tc->offset;
1616 transfer->length = write_size;
1617 transfer->buffer = tc->buf + tc->offset;
1619 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1620 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1623 ret = libusb_submit_transfer (transfer);
1632 Writes data to the chip. Does not wait for completion of the transfer
1633 nor does it make sure that the transfer was successful.
1635 Use libusb 1.0 asynchronous API.
1637 \param ftdi pointer to ftdi_context
1638 \param buf Buffer with the data
1639 \param size Size of the buffer
1641 \retval NULL: Some error happens when submit transfer
1642 \retval !NULL: Pointer to a ftdi_transfer_control
1645 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1647 struct ftdi_transfer_control *tc;
1648 struct libusb_transfer *transfer;
1649 int write_size, ret;
1651 if (ftdi == NULL || ftdi->usb_dev == NULL)
1654 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1658 transfer = libusb_alloc_transfer(0);
1671 if (size < (int)ftdi->writebuffer_chunksize)
1674 write_size = ftdi->writebuffer_chunksize;
1676 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1677 write_size, ftdi_write_data_cb, tc,
1678 ftdi->usb_write_timeout);
1679 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1681 ret = libusb_submit_transfer(transfer);
1684 libusb_free_transfer(transfer);
1688 tc->transfer = transfer;
1694 Reads data from the chip. Does not wait for completion of the transfer
1695 nor does it make sure that the transfer was successful.
1697 Use libusb 1.0 asynchronous API.
1699 \param ftdi pointer to ftdi_context
1700 \param buf Buffer with the data
1701 \param size Size of the buffer
1703 \retval NULL: Some error happens when submit transfer
1704 \retval !NULL: Pointer to a ftdi_transfer_control
1707 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1709 struct ftdi_transfer_control *tc;
1710 struct libusb_transfer *transfer;
1713 if (ftdi == NULL || ftdi->usb_dev == NULL)
1716 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1724 if (size <= (int)ftdi->readbuffer_remaining)
1726 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1729 ftdi->readbuffer_remaining -= size;
1730 ftdi->readbuffer_offset += size;
1732 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1736 tc->transfer = NULL;
1741 if (ftdi->readbuffer_remaining != 0)
1743 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1745 tc->offset = ftdi->readbuffer_remaining;
1750 transfer = libusb_alloc_transfer(0);
1757 ftdi->readbuffer_remaining = 0;
1758 ftdi->readbuffer_offset = 0;
1760 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);
1761 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1763 ret = libusb_submit_transfer(transfer);
1766 libusb_free_transfer(transfer);
1770 tc->transfer = transfer;
1776 Wait for completion of the transfer.
1778 Use libusb 1.0 asynchronous API.
1780 \param tc pointer to ftdi_transfer_control
1782 \retval < 0: Some error happens
1783 \retval >= 0: Data size transferred
1786 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1789 struct timeval to = { 0, 0 };
1790 while (!tc->completed)
1792 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1793 &to, &tc->completed);
1796 if (ret == LIBUSB_ERROR_INTERRUPTED)
1798 libusb_cancel_transfer(tc->transfer);
1799 while (!tc->completed)
1800 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1801 &to, &tc->completed) < 0)
1803 libusb_free_transfer(tc->transfer);
1811 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1812 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1816 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1818 libusb_free_transfer(tc->transfer);
1825 Cancel transfer and wait for completion.
1827 Use libusb 1.0 asynchronous API.
1829 \param tc pointer to ftdi_transfer_control
1830 \param to pointer to timeout value or NULL for infinite
1833 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
1834 struct timeval * to)
1836 struct timeval tv = { 0, 0 };
1838 if (!tc->completed && tc->transfer != NULL)
1843 libusb_cancel_transfer(tc->transfer);
1844 while (!tc->completed)
1846 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1852 libusb_free_transfer(tc->transfer);
1858 Configure write buffer chunk size.
1861 \param ftdi pointer to ftdi_context
1862 \param chunksize Chunk size
1865 \retval -1: ftdi context invalid
1867 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1870 ftdi_error_return(-1, "ftdi context invalid");
1872 ftdi->writebuffer_chunksize = chunksize;
1877 Get write buffer chunk size.
1879 \param ftdi pointer to ftdi_context
1880 \param chunksize Pointer to store chunk size in
1883 \retval -1: ftdi context invalid
1885 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1888 ftdi_error_return(-1, "ftdi context invalid");
1890 *chunksize = ftdi->writebuffer_chunksize;
1895 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1897 Automatically strips the two modem status bytes transfered during every read.
1899 \param ftdi pointer to ftdi_context
1900 \param buf Buffer to store data in
1901 \param size Size of the buffer
1903 \retval -666: USB device unavailable
1904 \retval <0: error code from libusb_bulk_transfer()
1905 \retval 0: no data was available
1906 \retval >0: number of bytes read
1909 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1911 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1913 int actual_length = 1;
1915 if (ftdi == NULL || ftdi->usb_dev == NULL)
1916 ftdi_error_return(-666, "USB device unavailable");
1918 // Packet size sanity check (avoid division by zero)
1919 packet_size = ftdi->max_packet_size;
1920 if (packet_size == 0)
1921 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1923 // everything we want is still in the readbuffer?
1924 if (size <= (int)ftdi->readbuffer_remaining)
1926 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1929 ftdi->readbuffer_remaining -= size;
1930 ftdi->readbuffer_offset += size;
1932 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1936 // something still in the readbuffer, but not enough to satisfy 'size'?
1937 if (ftdi->readbuffer_remaining != 0)
1939 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1942 offset += ftdi->readbuffer_remaining;
1944 // do the actual USB read
1945 while (offset < size && actual_length > 0)
1947 ftdi->readbuffer_remaining = 0;
1948 ftdi->readbuffer_offset = 0;
1949 /* returns how much received */
1950 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1952 ftdi_error_return(ret, "usb bulk read failed");
1954 if (actual_length > 2)
1956 // skip FTDI status bytes.
1957 // Maybe stored in the future to enable modem use
1958 num_of_chunks = actual_length / packet_size;
1959 chunk_remains = actual_length % packet_size;
1960 //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);
1962 ftdi->readbuffer_offset += 2;
1965 if (actual_length > packet_size - 2)
1967 for (i = 1; i < num_of_chunks; i++)
1968 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1969 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1971 if (chunk_remains > 2)
1973 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1974 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1976 actual_length -= 2*num_of_chunks;
1979 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1982 else if (actual_length <= 2)
1984 // no more data to read?
1987 if (actual_length > 0)
1989 // data still fits in buf?
1990 if (offset+actual_length <= size)
1992 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1993 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1994 offset += actual_length;
1996 /* Did we read exactly the right amount of bytes? */
1998 //printf("read_data exact rem %d offset %d\n",
1999 //ftdi->readbuffer_remaining, offset);
2004 // only copy part of the data or size <= readbuffer_chunksize
2005 int part_size = size-offset;
2006 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
2008 ftdi->readbuffer_offset += part_size;
2009 ftdi->readbuffer_remaining = actual_length-part_size;
2010 offset += part_size;
2012 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
2013 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
2024 Configure read buffer chunk size.
2027 Automatically reallocates the buffer.
2029 \param ftdi pointer to ftdi_context
2030 \param chunksize Chunk size
2033 \retval -1: ftdi context invalid
2035 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
2037 unsigned char *new_buf;
2040 ftdi_error_return(-1, "ftdi context invalid");
2042 // Invalidate all remaining data
2043 ftdi->readbuffer_offset = 0;
2044 ftdi->readbuffer_remaining = 0;
2046 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
2047 which is defined in libusb-1.0. Otherwise, each USB read request will
2048 be divided into multiple URBs. This will cause issues on Linux kernel
2049 older than 2.6.32. */
2050 if (chunksize > 16384)
2054 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2055 ftdi_error_return(-1, "out of memory for readbuffer");
2057 ftdi->readbuffer = new_buf;
2058 ftdi->readbuffer_chunksize = chunksize;
2064 Get read buffer chunk size.
2066 \param ftdi pointer to ftdi_context
2067 \param chunksize Pointer to store chunk size in
2070 \retval -1: FTDI context invalid
2072 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2075 ftdi_error_return(-1, "FTDI context invalid");
2077 *chunksize = ftdi->readbuffer_chunksize;
2082 Enable/disable bitbang modes.
2084 \param ftdi pointer to ftdi_context
2085 \param bitmask Bitmask to configure lines.
2086 HIGH/ON value configures a line as output.
2087 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
2090 \retval -1: can't enable bitbang mode
2091 \retval -2: USB device unavailable
2093 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2095 unsigned short usb_val;
2097 if (ftdi == NULL || ftdi->usb_dev == NULL)
2098 ftdi_error_return(-2, "USB device unavailable");
2100 usb_val = bitmask; // low byte: bitmask
2101 usb_val |= (mode << 8);
2102 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)
2103 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2105 ftdi->bitbang_mode = mode;
2106 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2111 Disable bitbang mode.
2113 \param ftdi pointer to ftdi_context
2116 \retval -1: can't disable bitbang mode
2117 \retval -2: USB device unavailable
2119 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
2121 if (ftdi == NULL || ftdi->usb_dev == NULL)
2122 ftdi_error_return(-2, "USB device unavailable");
2124 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)
2125 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2127 ftdi->bitbang_enabled = 0;
2133 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
2135 \param ftdi pointer to ftdi_context
2136 \param pins Pointer to store pins into
2139 \retval -1: read pins failed
2140 \retval -2: USB device unavailable
2142 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2144 if (ftdi == NULL || ftdi->usb_dev == NULL)
2145 ftdi_error_return(-2, "USB device unavailable");
2147 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)
2148 ftdi_error_return(-1, "read pins failed");
2156 The FTDI chip keeps data in the internal buffer for a specific
2157 amount of time if the buffer is not full yet to decrease
2158 load on the usb bus.
2160 \param ftdi pointer to ftdi_context
2161 \param latency Value between 1 and 255
2164 \retval -1: latency out of range
2165 \retval -2: unable to set latency timer
2166 \retval -3: USB device unavailable
2168 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2170 unsigned short usb_val;
2173 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2175 if (ftdi == NULL || ftdi->usb_dev == NULL)
2176 ftdi_error_return(-3, "USB device unavailable");
2179 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)
2180 ftdi_error_return(-2, "unable to set latency timer");
2188 \param ftdi pointer to ftdi_context
2189 \param latency Pointer to store latency value in
2192 \retval -1: unable to get latency timer
2193 \retval -2: USB device unavailable
2195 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2197 unsigned short usb_val;
2199 if (ftdi == NULL || ftdi->usb_dev == NULL)
2200 ftdi_error_return(-2, "USB device unavailable");
2202 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)
2203 ftdi_error_return(-1, "reading latency timer failed");
2205 *latency = (unsigned char)usb_val;
2210 Poll modem status information
2212 This function allows the retrieve the two status bytes of the device.
2213 The device sends these bytes also as a header for each read access
2214 where they are discarded by ftdi_read_data(). The chip generates
2215 the two stripped status bytes in the absence of data every 40 ms.
2217 Layout of the first byte:
2218 - B0..B3 - must be 0
2219 - B4 Clear to send (CTS)
2222 - B5 Data set ready (DTS)
2225 - B6 Ring indicator (RI)
2228 - B7 Receive line signal detect (RLSD)
2232 Layout of the second byte:
2233 - B0 Data ready (DR)
2234 - B1 Overrun error (OE)
2235 - B2 Parity error (PE)
2236 - B3 Framing error (FE)
2237 - B4 Break interrupt (BI)
2238 - B5 Transmitter holding register (THRE)
2239 - B6 Transmitter empty (TEMT)
2240 - B7 Error in RCVR FIFO
2242 \param ftdi pointer to ftdi_context
2243 \param status Pointer to store status information in. Must be two bytes.
2246 \retval -1: unable to retrieve status information
2247 \retval -2: USB device unavailable
2249 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2253 if (ftdi == NULL || ftdi->usb_dev == NULL)
2254 ftdi_error_return(-2, "USB device unavailable");
2256 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)
2257 ftdi_error_return(-1, "getting modem status failed");
2259 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2265 Set flowcontrol for ftdi chip
2267 Note: Do not use this function to enable XON/XOFF mode, use ftdi_setflowctrl_xonxoff() instead.
2269 \param ftdi pointer to ftdi_context
2270 \param flowctrl flow control to use. should be
2271 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS
2274 \retval -1: set flow control failed
2275 \retval -2: USB device unavailable
2277 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2279 if (ftdi == NULL || ftdi->usb_dev == NULL)
2280 ftdi_error_return(-2, "USB device unavailable");
2282 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2283 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2284 NULL, 0, ftdi->usb_write_timeout) < 0)
2285 ftdi_error_return(-1, "set flow control failed");
2291 Set XON/XOFF flowcontrol for ftdi chip
2293 \param ftdi pointer to ftdi_context
2294 \param xon character code used to resume transmission
2295 \param xoff character code used to pause transmission
2298 \retval -1: set flow control failed
2299 \retval -2: USB device unavailable
2301 int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
2303 if (ftdi == NULL || ftdi->usb_dev == NULL)
2304 ftdi_error_return(-2, "USB device unavailable");
2306 uint16_t xonxoff = xon | (xoff << 8);
2307 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2308 SIO_SET_FLOW_CTRL_REQUEST, xonxoff, (SIO_XON_XOFF_HS | ftdi->index),
2309 NULL, 0, ftdi->usb_write_timeout) < 0)
2310 ftdi_error_return(-1, "set flow control failed");
2318 \param ftdi pointer to ftdi_context
2319 \param state state to set line to (1 or 0)
2322 \retval -1: set dtr failed
2323 \retval -2: USB device unavailable
2325 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2327 unsigned short usb_val;
2329 if (ftdi == NULL || ftdi->usb_dev == NULL)
2330 ftdi_error_return(-2, "USB device unavailable");
2333 usb_val = SIO_SET_DTR_HIGH;
2335 usb_val = SIO_SET_DTR_LOW;
2337 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2338 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2339 NULL, 0, ftdi->usb_write_timeout) < 0)
2340 ftdi_error_return(-1, "set dtr failed");
2348 \param ftdi pointer to ftdi_context
2349 \param state state to set line to (1 or 0)
2352 \retval -1: set rts failed
2353 \retval -2: USB device unavailable
2355 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2357 unsigned short usb_val;
2359 if (ftdi == NULL || ftdi->usb_dev == NULL)
2360 ftdi_error_return(-2, "USB device unavailable");
2363 usb_val = SIO_SET_RTS_HIGH;
2365 usb_val = SIO_SET_RTS_LOW;
2367 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2368 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2369 NULL, 0, ftdi->usb_write_timeout) < 0)
2370 ftdi_error_return(-1, "set of rts failed");
2376 Set dtr and rts line in one pass
2378 \param ftdi pointer to ftdi_context
2379 \param dtr DTR state to set line to (1 or 0)
2380 \param rts RTS state to set line to (1 or 0)
2383 \retval -1: set dtr/rts failed
2384 \retval -2: USB device unavailable
2386 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2388 unsigned short usb_val;
2390 if (ftdi == NULL || ftdi->usb_dev == NULL)
2391 ftdi_error_return(-2, "USB device unavailable");
2394 usb_val = SIO_SET_DTR_HIGH;
2396 usb_val = SIO_SET_DTR_LOW;
2399 usb_val |= SIO_SET_RTS_HIGH;
2401 usb_val |= SIO_SET_RTS_LOW;
2403 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2404 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2405 NULL, 0, ftdi->usb_write_timeout) < 0)
2406 ftdi_error_return(-1, "set of rts/dtr failed");
2412 Set the special event character
2414 \param ftdi pointer to ftdi_context
2415 \param eventch Event character
2416 \param enable 0 to disable the event character, non-zero otherwise
2419 \retval -1: unable to set event character
2420 \retval -2: USB device unavailable
2422 int ftdi_set_event_char(struct ftdi_context *ftdi,
2423 unsigned char eventch, unsigned char enable)
2425 unsigned short usb_val;
2427 if (ftdi == NULL || ftdi->usb_dev == NULL)
2428 ftdi_error_return(-2, "USB device unavailable");
2434 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)
2435 ftdi_error_return(-1, "setting event character failed");
2443 \param ftdi pointer to ftdi_context
2444 \param errorch Error character
2445 \param enable 0 to disable the error character, non-zero otherwise
2448 \retval -1: unable to set error character
2449 \retval -2: USB device unavailable
2451 int ftdi_set_error_char(struct ftdi_context *ftdi,
2452 unsigned char errorch, unsigned char enable)
2454 unsigned short usb_val;
2456 if (ftdi == NULL || ftdi->usb_dev == NULL)
2457 ftdi_error_return(-2, "USB device unavailable");
2463 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)
2464 ftdi_error_return(-1, "setting error character failed");
2470 Init eeprom with default values for the connected device
2471 \param ftdi pointer to ftdi_context
2472 \param manufacturer String to use as Manufacturer
2473 \param product String to use as Product description
2474 \param serial String to use as Serial number description
2477 \retval -1: No struct ftdi_context
2478 \retval -2: No struct ftdi_eeprom
2479 \retval -3: No connected device or device not yet opened
2481 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2482 char * product, char * serial)
2484 struct ftdi_eeprom *eeprom;
2487 ftdi_error_return(-1, "No struct ftdi_context");
2489 if (ftdi->eeprom == NULL)
2490 ftdi_error_return(-2,"No struct ftdi_eeprom");
2492 eeprom = ftdi->eeprom;
2493 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2495 if (ftdi->usb_dev == NULL)
2496 ftdi_error_return(-3, "No connected device or device not yet opened");
2498 eeprom->vendor_id = 0x0403;
2499 eeprom->use_serial = 1;
2500 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2501 (ftdi->type == TYPE_R))
2502 eeprom->product_id = 0x6001;
2503 else if (ftdi->type == TYPE_4232H)
2504 eeprom->product_id = 0x6011;
2505 else if (ftdi->type == TYPE_232H)
2506 eeprom->product_id = 0x6014;
2507 else if (ftdi->type == TYPE_230X)
2508 eeprom->product_id = 0x6015;
2510 eeprom->product_id = 0x6010;
2512 if (ftdi->type == TYPE_AM)
2513 eeprom->usb_version = 0x0101;
2515 eeprom->usb_version = 0x0200;
2516 eeprom->max_power = 100;
2518 if (eeprom->manufacturer)
2519 free (eeprom->manufacturer);
2520 eeprom->manufacturer = NULL;
2523 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2524 if (eeprom->manufacturer)
2525 strcpy(eeprom->manufacturer, manufacturer);
2528 if (eeprom->product)
2529 free (eeprom->product);
2530 eeprom->product = NULL;
2533 eeprom->product = (char *)malloc(strlen(product)+1);
2534 if (eeprom->product)
2535 strcpy(eeprom->product, product);
2539 const char* default_product;
2542 case TYPE_AM: default_product = "AM"; break;
2543 case TYPE_BM: default_product = "BM"; break;
2544 case TYPE_2232C: default_product = "Dual RS232"; break;
2545 case TYPE_R: default_product = "FT232R USB UART"; break;
2546 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2547 case TYPE_4232H: default_product = "FT4232H"; break;
2548 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2549 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2551 ftdi_error_return(-3, "Unknown chip type");
2553 eeprom->product = (char *)malloc(strlen(default_product) +1);
2554 if (eeprom->product)
2555 strcpy(eeprom->product, default_product);
2559 free (eeprom->serial);
2560 eeprom->serial = NULL;
2563 eeprom->serial = (char *)malloc(strlen(serial)+1);
2565 strcpy(eeprom->serial, serial);
2568 if (ftdi->type == TYPE_R)
2570 eeprom->max_power = 90;
2571 eeprom->size = 0x80;
2572 eeprom->cbus_function[0] = CBUS_TXLED;
2573 eeprom->cbus_function[1] = CBUS_RXLED;
2574 eeprom->cbus_function[2] = CBUS_TXDEN;
2575 eeprom->cbus_function[3] = CBUS_PWREN;
2576 eeprom->cbus_function[4] = CBUS_SLEEP;
2578 else if (ftdi->type == TYPE_230X)
2580 eeprom->max_power = 90;
2581 eeprom->size = 0x100;
2582 eeprom->cbus_function[0] = CBUSX_TXDEN;
2583 eeprom->cbus_function[1] = CBUSX_RXLED;
2584 eeprom->cbus_function[2] = CBUSX_TXLED;
2585 eeprom->cbus_function[3] = CBUSX_SLEEP;
2589 if(ftdi->type == TYPE_232H)
2592 for (i=0; i<10; i++)
2593 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2600 eeprom->release_number = 0x0200;
2603 eeprom->release_number = 0x0400;
2606 eeprom->release_number = 0x0500;
2609 eeprom->release_number = 0x0600;
2612 eeprom->release_number = 0x0700;
2615 eeprom->release_number = 0x0800;
2618 eeprom->release_number = 0x0900;
2621 eeprom->release_number = 0x1000;
2624 eeprom->release_number = 0x00;
2629 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2630 char * product, char * serial)
2632 struct ftdi_eeprom *eeprom;
2635 ftdi_error_return(-1, "No struct ftdi_context");
2637 if (ftdi->eeprom == NULL)
2638 ftdi_error_return(-2,"No struct ftdi_eeprom");
2640 eeprom = ftdi->eeprom;
2642 if (ftdi->usb_dev == NULL)
2643 ftdi_error_return(-3, "No connected device or device not yet opened");
2647 if (eeprom->manufacturer)
2648 free (eeprom->manufacturer);
2649 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2650 if (eeprom->manufacturer)
2651 strcpy(eeprom->manufacturer, manufacturer);
2656 if (eeprom->product)
2657 free (eeprom->product);
2658 eeprom->product = (char *)malloc(strlen(product)+1);
2659 if (eeprom->product)
2660 strcpy(eeprom->product, product);
2666 free (eeprom->serial);
2667 eeprom->serial = (char *)malloc(strlen(serial)+1);
2670 strcpy(eeprom->serial, serial);
2671 eeprom->use_serial = 1;
2678 Return device ID strings from the eeprom. Device needs to be connected.
2680 The parameters manufacturer, description and serial may be NULL
2681 or pointer to buffers to store the fetched strings.
2683 \param ftdi pointer to ftdi_context
2684 \param manufacturer Store manufacturer string here if not NULL
2685 \param mnf_len Buffer size of manufacturer string
2686 \param product Store product description string here if not NULL
2687 \param prod_len Buffer size of product description string
2688 \param serial Store serial string here if not NULL
2689 \param serial_len Buffer size of serial string
2692 \retval -1: ftdi context invalid
2693 \retval -2: ftdi eeprom buffer invalid
2695 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
2696 char *manufacturer, int mnf_len,
2697 char *product, int prod_len,
2698 char *serial, int serial_len)
2700 struct ftdi_eeprom *eeprom;
2703 ftdi_error_return(-1, "No struct ftdi_context");
2704 if (ftdi->eeprom == NULL)
2705 ftdi_error_return(-2, "No struct ftdi_eeprom");
2707 eeprom = ftdi->eeprom;
2711 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
2713 manufacturer[mnf_len - 1] = '\0';
2718 strncpy(product, eeprom->product, prod_len);
2720 product[prod_len - 1] = '\0';
2725 strncpy(serial, eeprom->serial, serial_len);
2727 serial[serial_len - 1] = '\0';
2733 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2734 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2739 int mode_low, mode_high;
2740 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2741 mode_low = CBUSH_TRISTATE;
2743 mode_low = eeprom->cbus_function[2*i];
2744 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2745 mode_high = CBUSH_TRISTATE;
2747 mode_high = eeprom->cbus_function[2*i+1];
2749 output[0x18+i] = (mode_high <<4) | mode_low;
2752 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2755 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2764 case CHANNEL_IS_UART: return 0;
2765 case CHANNEL_IS_FIFO: return 0x01;
2766 case CHANNEL_IS_OPTO: return 0x02;
2767 case CHANNEL_IS_CPU : return 0x04;
2775 case CHANNEL_IS_UART : return 0;
2776 case CHANNEL_IS_FIFO : return 0x01;
2777 case CHANNEL_IS_OPTO : return 0x02;
2778 case CHANNEL_IS_CPU : return 0x04;
2779 case CHANNEL_IS_FT1284 : return 0x08;
2787 case CHANNEL_IS_UART : return 0;
2788 case CHANNEL_IS_FIFO : return 0x01;
2792 case TYPE_230X: /* FT230X is only UART */
2799 Build binary buffer from ftdi_eeprom structure.
2800 Output is suitable for ftdi_write_eeprom().
2802 \param ftdi pointer to ftdi_context
2804 \retval >=0: size of eeprom user area in bytes
2805 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2806 \retval -2: Invalid eeprom or ftdi pointer
2807 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2808 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2809 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2810 \retval -6: No connected EEPROM or EEPROM Type unknown
2812 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2814 unsigned char i, j, eeprom_size_mask;
2815 unsigned short checksum, value;
2816 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2817 int user_area_size, free_start, free_end;
2818 struct ftdi_eeprom *eeprom;
2819 unsigned char * output;
2822 ftdi_error_return(-2,"No context");
2823 if (ftdi->eeprom == NULL)
2824 ftdi_error_return(-2,"No eeprom structure");
2826 eeprom= ftdi->eeprom;
2827 output = eeprom->buf;
2829 if (eeprom->chip == -1)
2830 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2832 if (eeprom->size == -1)
2834 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2835 eeprom->size = 0x100;
2837 eeprom->size = 0x80;
2840 if (eeprom->manufacturer != NULL)
2841 manufacturer_size = strlen(eeprom->manufacturer);
2842 if (eeprom->product != NULL)
2843 product_size = strlen(eeprom->product);
2844 if (eeprom->serial != NULL)
2845 serial_size = strlen(eeprom->serial);
2847 // eeprom size check
2853 user_area_size = 96; // base size for strings (total of 48 characters)
2856 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2859 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2861 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2863 user_area_size = 86;
2866 user_area_size = 80;
2872 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2874 if (user_area_size < 0)
2875 ftdi_error_return(-1,"eeprom size exceeded");
2878 if (ftdi->type == TYPE_230X)
2880 /* FT230X have a reserved section in the middle of the MTP,
2881 which cannot be written to, but must be included in the checksum */
2882 memset(ftdi->eeprom->buf, 0, 0x80);
2883 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2887 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2890 // Bytes and Bits set for all Types
2892 // Addr 02: Vendor ID
2893 output[0x02] = eeprom->vendor_id;
2894 output[0x03] = eeprom->vendor_id >> 8;
2896 // Addr 04: Product ID
2897 output[0x04] = eeprom->product_id;
2898 output[0x05] = eeprom->product_id >> 8;
2900 // Addr 06: Device release number (0400h for BM features)
2901 output[0x06] = eeprom->release_number;
2902 output[0x07] = eeprom->release_number >> 8;
2904 // Addr 08: Config descriptor
2906 // Bit 6: 1 if this device is self powered, 0 if bus powered
2907 // Bit 5: 1 if this device uses remote wakeup
2908 // Bit 4-0: reserved - 0
2910 if (eeprom->self_powered)
2912 if (eeprom->remote_wakeup)
2916 // Addr 09: Max power consumption: max power = value * 2 mA
2917 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2919 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2921 // Addr 0A: Chip configuration
2922 // Bit 7: 0 - reserved
2923 // Bit 6: 0 - reserved
2924 // Bit 5: 0 - reserved
2925 // Bit 4: 1 - Change USB version
2926 // Bit 3: 1 - Use the serial number string
2927 // Bit 2: 1 - Enable suspend pull downs for lower power
2928 // Bit 1: 1 - Out EndPoint is Isochronous
2929 // Bit 0: 1 - In EndPoint is Isochronous
2932 if (eeprom->in_is_isochronous)
2934 if (eeprom->out_is_isochronous)
2940 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2941 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2962 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2963 eeprom_size_mask = eeprom->size -1;
2964 free_end = i & eeprom_size_mask;
2966 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2967 // Addr 0F: Length of manufacturer string
2968 // Output manufacturer
2969 output[0x0E] = i; // calculate offset
2970 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2971 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2972 for (j = 0; j < manufacturer_size; j++)
2974 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2975 output[i & eeprom_size_mask] = 0x00, i++;
2977 output[0x0F] = manufacturer_size*2 + 2;
2979 // Addr 10: Offset of the product string + 0x80, calculated later
2980 // Addr 11: Length of product string
2981 output[0x10] = i | 0x80; // calculate offset
2982 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2983 output[i & eeprom_size_mask] = 0x03, i++;
2984 for (j = 0; j < product_size; j++)
2986 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2987 output[i & eeprom_size_mask] = 0x00, i++;
2989 output[0x11] = product_size*2 + 2;
2991 // Addr 12: Offset of the serial string + 0x80, calculated later
2992 // Addr 13: Length of serial string
2993 output[0x12] = i | 0x80; // calculate offset
2994 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2995 output[i & eeprom_size_mask] = 0x03, i++;
2996 for (j = 0; j < serial_size; j++)
2998 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2999 output[i & eeprom_size_mask] = 0x00, i++;
3002 // Legacy port name and PnP fields for FT2232 and newer chips
3003 if (ftdi->type > TYPE_BM)
3005 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
3007 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
3009 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
3013 output[0x13] = serial_size*2 + 2;
3015 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
3017 if (eeprom->use_serial)
3018 output[0x0A] |= USE_SERIAL_NUM;
3020 output[0x0A] &= ~USE_SERIAL_NUM;
3023 /* Bytes and Bits specific to (some) types
3024 Write linear, as this allows easier fixing*/
3030 output[0x0C] = eeprom->usb_version & 0xff;
3031 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3032 if (eeprom->use_usb_version)
3033 output[0x0A] |= USE_USB_VERSION_BIT;
3035 output[0x0A] &= ~USE_USB_VERSION_BIT;
3040 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
3041 if ( eeprom->channel_a_driver == DRIVER_VCP)
3042 output[0x00] |= DRIVER_VCP;
3044 output[0x00] &= ~DRIVER_VCP;
3046 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
3047 output[0x00] |= HIGH_CURRENT_DRIVE;
3049 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3051 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
3052 if ( eeprom->channel_b_driver == DRIVER_VCP)
3053 output[0x01] |= DRIVER_VCP;
3055 output[0x01] &= ~DRIVER_VCP;
3057 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
3058 output[0x01] |= HIGH_CURRENT_DRIVE;
3060 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3062 if (eeprom->in_is_isochronous)
3063 output[0x0A] |= 0x1;
3065 output[0x0A] &= ~0x1;
3066 if (eeprom->out_is_isochronous)
3067 output[0x0A] |= 0x2;
3069 output[0x0A] &= ~0x2;
3070 if (eeprom->suspend_pull_downs)
3071 output[0x0A] |= 0x4;
3073 output[0x0A] &= ~0x4;
3074 if (eeprom->use_usb_version)
3075 output[0x0A] |= USE_USB_VERSION_BIT;
3077 output[0x0A] &= ~USE_USB_VERSION_BIT;
3079 output[0x0C] = eeprom->usb_version & 0xff;
3080 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3081 output[0x14] = eeprom->chip;
3084 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
3085 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
3086 output[0x00] |= HIGH_CURRENT_DRIVE_R;
3087 if (eeprom->external_oscillator)
3088 output[0x00] |= 0x02;
3089 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
3091 if (eeprom->suspend_pull_downs)
3092 output[0x0A] |= 0x4;
3094 output[0x0A] &= ~0x4;
3095 output[0x0B] = eeprom->invert;
3096 output[0x0C] = eeprom->usb_version & 0xff;
3097 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3099 if (eeprom->cbus_function[0] > CBUS_BB_RD)
3100 output[0x14] = CBUS_TXLED;
3102 output[0x14] = eeprom->cbus_function[0];
3104 if (eeprom->cbus_function[1] > CBUS_BB_RD)
3105 output[0x14] |= CBUS_RXLED<<4;
3107 output[0x14] |= eeprom->cbus_function[1]<<4;
3109 if (eeprom->cbus_function[2] > CBUS_BB_RD)
3110 output[0x15] = CBUS_TXDEN;
3112 output[0x15] = eeprom->cbus_function[2];
3114 if (eeprom->cbus_function[3] > CBUS_BB_RD)
3115 output[0x15] |= CBUS_PWREN<<4;
3117 output[0x15] |= eeprom->cbus_function[3]<<4;
3119 if (eeprom->cbus_function[4] > CBUS_CLK6)
3120 output[0x16] = CBUS_SLEEP;
3122 output[0x16] = eeprom->cbus_function[4];
3125 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
3126 if ( eeprom->channel_a_driver == DRIVER_VCP)
3127 output[0x00] |= DRIVER_VCP;
3129 output[0x00] &= ~DRIVER_VCP;
3131 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
3132 if ( eeprom->channel_b_driver == DRIVER_VCP)
3133 output[0x01] |= DRIVER_VCP;
3135 output[0x01] &= ~DRIVER_VCP;
3136 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
3137 output[0x01] |= SUSPEND_DBUS7_BIT;
3139 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3141 if (eeprom->suspend_pull_downs)
3142 output[0x0A] |= 0x4;
3144 output[0x0A] &= ~0x4;
3146 if (eeprom->group0_drive > DRIVE_16MA)
3147 output[0x0c] |= DRIVE_16MA;
3149 output[0x0c] |= eeprom->group0_drive;
3150 if (eeprom->group0_schmitt == IS_SCHMITT)
3151 output[0x0c] |= IS_SCHMITT;
3152 if (eeprom->group0_slew == SLOW_SLEW)
3153 output[0x0c] |= SLOW_SLEW;
3155 if (eeprom->group1_drive > DRIVE_16MA)
3156 output[0x0c] |= DRIVE_16MA<<4;
3158 output[0x0c] |= eeprom->group1_drive<<4;
3159 if (eeprom->group1_schmitt == IS_SCHMITT)
3160 output[0x0c] |= IS_SCHMITT<<4;
3161 if (eeprom->group1_slew == SLOW_SLEW)
3162 output[0x0c] |= SLOW_SLEW<<4;
3164 if (eeprom->group2_drive > DRIVE_16MA)
3165 output[0x0d] |= DRIVE_16MA;
3167 output[0x0d] |= eeprom->group2_drive;
3168 if (eeprom->group2_schmitt == IS_SCHMITT)
3169 output[0x0d] |= IS_SCHMITT;
3170 if (eeprom->group2_slew == SLOW_SLEW)
3171 output[0x0d] |= SLOW_SLEW;
3173 if (eeprom->group3_drive > DRIVE_16MA)
3174 output[0x0d] |= DRIVE_16MA<<4;
3176 output[0x0d] |= eeprom->group3_drive<<4;
3177 if (eeprom->group3_schmitt == IS_SCHMITT)
3178 output[0x0d] |= IS_SCHMITT<<4;
3179 if (eeprom->group3_slew == SLOW_SLEW)
3180 output[0x0d] |= SLOW_SLEW<<4;
3182 output[0x18] = eeprom->chip;
3186 if (eeprom->channel_a_driver == DRIVER_VCP)
3187 output[0x00] |= DRIVER_VCP;
3189 output[0x00] &= ~DRIVER_VCP;
3190 if (eeprom->channel_b_driver == DRIVER_VCP)
3191 output[0x01] |= DRIVER_VCP;
3193 output[0x01] &= ~DRIVER_VCP;
3194 if (eeprom->channel_c_driver == DRIVER_VCP)
3195 output[0x00] |= (DRIVER_VCP << 4);
3197 output[0x00] &= ~(DRIVER_VCP << 4);
3198 if (eeprom->channel_d_driver == DRIVER_VCP)
3199 output[0x01] |= (DRIVER_VCP << 4);
3201 output[0x01] &= ~(DRIVER_VCP << 4);
3203 if (eeprom->suspend_pull_downs)
3204 output[0x0a] |= 0x4;
3206 output[0x0a] &= ~0x4;
3208 if (eeprom->channel_a_rs485enable)
3209 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3211 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3212 if (eeprom->channel_b_rs485enable)
3213 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3215 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3216 if (eeprom->channel_c_rs485enable)
3217 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3219 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3220 if (eeprom->channel_d_rs485enable)
3221 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3223 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3225 if (eeprom->group0_drive > DRIVE_16MA)
3226 output[0x0c] |= DRIVE_16MA;
3228 output[0x0c] |= eeprom->group0_drive;
3229 if (eeprom->group0_schmitt == IS_SCHMITT)
3230 output[0x0c] |= IS_SCHMITT;
3231 if (eeprom->group0_slew == SLOW_SLEW)
3232 output[0x0c] |= SLOW_SLEW;
3234 if (eeprom->group1_drive > DRIVE_16MA)
3235 output[0x0c] |= DRIVE_16MA<<4;
3237 output[0x0c] |= eeprom->group1_drive<<4;
3238 if (eeprom->group1_schmitt == IS_SCHMITT)
3239 output[0x0c] |= IS_SCHMITT<<4;
3240 if (eeprom->group1_slew == SLOW_SLEW)
3241 output[0x0c] |= SLOW_SLEW<<4;
3243 if (eeprom->group2_drive > DRIVE_16MA)
3244 output[0x0d] |= DRIVE_16MA;
3246 output[0x0d] |= eeprom->group2_drive;
3247 if (eeprom->group2_schmitt == IS_SCHMITT)
3248 output[0x0d] |= IS_SCHMITT;
3249 if (eeprom->group2_slew == SLOW_SLEW)
3250 output[0x0d] |= SLOW_SLEW;
3252 if (eeprom->group3_drive > DRIVE_16MA)
3253 output[0x0d] |= DRIVE_16MA<<4;
3255 output[0x0d] |= eeprom->group3_drive<<4;
3256 if (eeprom->group3_schmitt == IS_SCHMITT)
3257 output[0x0d] |= IS_SCHMITT<<4;
3258 if (eeprom->group3_slew == SLOW_SLEW)
3259 output[0x0d] |= SLOW_SLEW<<4;
3261 output[0x18] = eeprom->chip;
3265 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3266 if ( eeprom->channel_a_driver == DRIVER_VCP)
3267 output[0x00] |= DRIVER_VCPH;
3269 output[0x00] &= ~DRIVER_VCPH;
3270 if (eeprom->powersave)
3271 output[0x01] |= POWER_SAVE_DISABLE_H;
3273 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3275 if (eeprom->suspend_pull_downs)
3276 output[0x0a] |= 0x4;
3278 output[0x0a] &= ~0x4;
3280 if (eeprom->clock_polarity)
3281 output[0x01] |= FT1284_CLK_IDLE_STATE;
3283 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3284 if (eeprom->data_order)
3285 output[0x01] |= FT1284_DATA_LSB;
3287 output[0x01] &= ~FT1284_DATA_LSB;
3288 if (eeprom->flow_control)
3289 output[0x01] |= FT1284_FLOW_CONTROL;
3291 output[0x01] &= ~FT1284_FLOW_CONTROL;
3292 if (eeprom->group0_drive > DRIVE_16MA)
3293 output[0x0c] |= DRIVE_16MA;
3295 output[0x0c] |= eeprom->group0_drive;
3296 if (eeprom->group0_schmitt == IS_SCHMITT)
3297 output[0x0c] |= IS_SCHMITT;
3298 if (eeprom->group0_slew == SLOW_SLEW)
3299 output[0x0c] |= SLOW_SLEW;
3301 if (eeprom->group1_drive > DRIVE_16MA)
3302 output[0x0d] |= DRIVE_16MA;
3304 output[0x0d] |= eeprom->group1_drive;
3305 if (eeprom->group1_schmitt == IS_SCHMITT)
3306 output[0x0d] |= IS_SCHMITT;
3307 if (eeprom->group1_slew == SLOW_SLEW)
3308 output[0x0d] |= SLOW_SLEW;
3310 set_ft232h_cbus(eeprom, output);
3312 output[0x1e] = eeprom->chip;
3313 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3316 output[0x00] = 0x80; /* Actually, leave the default value */
3317 /*FIXME: Make DBUS & CBUS Control configurable*/
3318 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3319 for (j = 0; j <= 6; j++)
3321 output[0x1a + j] = eeprom->cbus_function[j];
3323 output[0x0b] = eeprom->invert;
3327 /* First address without use */
3347 /* Arbitrary user data */
3348 if (eeprom->user_data && eeprom->user_data_size >= 0)
3350 if (eeprom->user_data_addr < free_start)
3351 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3352 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3353 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3354 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3355 ftdi_error_return(-1,"eeprom size exceeded");
3356 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3359 // calculate checksum
3362 for (i = 0; i < eeprom->size/2-1; i++)
3364 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3366 /* FT230X has a user section in the MTP which is not part of the checksum */
3369 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3371 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3372 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3378 value = output[i*2];
3379 value += output[(i*2)+1] << 8;
3381 checksum = value^checksum;
3382 checksum = (checksum << 1) | (checksum >> 15);
3385 output[eeprom->size-2] = checksum;
3386 output[eeprom->size-1] = checksum >> 8;
3388 eeprom->initialized_for_connected_device = 1;
3389 return user_area_size;
3391 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3394 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3396 static unsigned char bit2type(unsigned char bits)
3400 case 0: return CHANNEL_IS_UART;
3401 case 1: return CHANNEL_IS_FIFO;
3402 case 2: return CHANNEL_IS_OPTO;
3403 case 4: return CHANNEL_IS_CPU;
3404 case 8: return CHANNEL_IS_FT1284;
3406 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3411 /* Decode 230X / 232R type chips invert bits
3412 * Prints directly to stdout.
3414 static void print_inverted_bits(int invert)
3416 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3419 fprintf(stdout,"Inverted bits:");
3421 if ((invert & (1<<i)) == (1<<i))
3422 fprintf(stdout," %s",r_bits[i]);
3424 fprintf(stdout,"\n");
3427 Decode binary EEPROM image into an ftdi_eeprom structure.
3429 For FT-X devices use AN_201 FT-X MTP memory Configuration to decode.
3431 \param ftdi pointer to ftdi_context
3432 \param verbose Decode EEPROM on stdout
3435 \retval -1: something went wrong
3437 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3438 FIXME: Strings are malloc'ed here and should be freed somewhere
3440 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3443 unsigned short checksum, eeprom_checksum, value;
3444 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3446 struct ftdi_eeprom *eeprom;
3447 unsigned char *buf = NULL;
3450 ftdi_error_return(-1,"No context");
3451 if (ftdi->eeprom == NULL)
3452 ftdi_error_return(-1,"No eeprom structure");
3454 eeprom = ftdi->eeprom;
3455 eeprom_size = eeprom->size;
3456 buf = ftdi->eeprom->buf;
3458 // Addr 02: Vendor ID
3459 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3461 // Addr 04: Product ID
3462 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3464 // Addr 06: Device release number
3465 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3467 // Addr 08: Config descriptor
3469 // Bit 6: 1 if this device is self powered, 0 if bus powered
3470 // Bit 5: 1 if this device uses remote wakeup
3471 eeprom->self_powered = buf[0x08] & 0x40;
3472 eeprom->remote_wakeup = buf[0x08] & 0x20;
3474 // Addr 09: Max power consumption: max power = value * 2 mA
3475 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3477 // Addr 0A: Chip configuration
3478 // Bit 7: 0 - reserved
3479 // Bit 6: 0 - reserved
3480 // Bit 5: 0 - reserved
3481 // Bit 4: 1 - Change USB version on BM and 2232C
3482 // Bit 3: 1 - Use the serial number string
3483 // Bit 2: 1 - Enable suspend pull downs for lower power
3484 // Bit 1: 1 - Out EndPoint is Isochronous
3485 // Bit 0: 1 - In EndPoint is Isochronous
3487 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3488 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3489 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3490 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3491 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3493 // Addr 0C: USB version low byte when 0x0A
3494 // Addr 0D: USB version high byte when 0x0A
3495 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3497 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3498 // Addr 0F: Length of manufacturer string
3499 manufacturer_size = buf[0x0F]/2;
3500 if (eeprom->manufacturer)
3501 free(eeprom->manufacturer);
3502 if (manufacturer_size > 0)
3504 eeprom->manufacturer = (char *)malloc(manufacturer_size);
3505 if (eeprom->manufacturer)
3507 // Decode manufacturer
3508 i = buf[0x0E] & (eeprom_size -1); // offset
3509 for (j=0; j<manufacturer_size-1; j++)
3511 eeprom->manufacturer[j] = buf[2*j+i+2];
3513 eeprom->manufacturer[j] = '\0';
3516 else eeprom->manufacturer = NULL;
3518 // Addr 10: Offset of the product string + 0x80, calculated later
3519 // Addr 11: Length of product string
3520 if (eeprom->product)
3521 free(eeprom->product);
3522 product_size = buf[0x11]/2;
3523 if (product_size > 0)
3525 eeprom->product = (char *)malloc(product_size);
3526 if (eeprom->product)
3528 // Decode product name
3529 i = buf[0x10] & (eeprom_size -1); // offset
3530 for (j=0; j<product_size-1; j++)
3532 eeprom->product[j] = buf[2*j+i+2];
3534 eeprom->product[j] = '\0';
3537 else eeprom->product = NULL;
3539 // Addr 12: Offset of the serial string + 0x80, calculated later
3540 // Addr 13: Length of serial string
3542 free(eeprom->serial);
3543 serial_size = buf[0x13]/2;
3544 if (serial_size > 0)
3546 eeprom->serial = (char *)malloc(serial_size);
3550 i = buf[0x12] & (eeprom_size -1); // offset
3551 for (j=0; j<serial_size-1; j++)
3553 eeprom->serial[j] = buf[2*j+i+2];
3555 eeprom->serial[j] = '\0';
3558 else eeprom->serial = NULL;
3563 for (i = 0; i < eeprom_size/2-1; i++)
3565 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3567 /* FT230X has a user section in the MTP which is not part of the checksum */
3571 value += buf[(i*2)+1] << 8;
3573 checksum = value^checksum;
3574 checksum = (checksum << 1) | (checksum >> 15);
3577 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3579 if (eeprom_checksum != checksum)
3581 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3582 ftdi_error_return(-1,"EEPROM checksum error");
3585 eeprom->channel_a_type = 0;
3586 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3590 else if (ftdi->type == TYPE_2232C)
3592 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3593 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3594 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3595 eeprom->channel_b_type = buf[0x01] & 0x7;
3596 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3597 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3598 eeprom->chip = buf[0x14];
3600 else if (ftdi->type == TYPE_R)
3602 /* TYPE_R flags D2XX, not VCP as all others*/
3603 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3604 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3605 eeprom->external_oscillator = buf[0x00] & 0x02;
3606 if ( (buf[0x01]&0x40) != 0x40)
3608 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3609 " If this happened with the\n"
3610 " EEPROM programmed by FTDI tools, please report "
3611 "to libftdi@developer.intra2net.com\n");
3613 eeprom->chip = buf[0x16];
3614 // Addr 0B: Invert data lines
3615 // Works only on FT232R, not FT245R, but no way to distinguish
3616 eeprom->invert = buf[0x0B];
3617 // Addr 14: CBUS function: CBUS0, CBUS1
3618 // Addr 15: CBUS function: CBUS2, CBUS3
3619 // Addr 16: CBUS function: CBUS5
3620 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3621 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3622 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3623 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3624 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3626 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3628 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3629 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3631 if (ftdi->type == TYPE_2232H)
3633 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3634 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3635 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3639 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3640 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3641 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3642 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3643 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3644 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3647 eeprom->chip = buf[0x18];
3648 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3649 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3650 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3651 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3652 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3653 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3654 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3655 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3656 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3657 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3658 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3659 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3661 else if (ftdi->type == TYPE_232H)
3663 eeprom->channel_a_type = buf[0x00] & 0xf;
3664 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3665 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3666 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3667 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3668 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3669 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3670 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3671 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3672 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3673 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3674 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3678 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3679 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3681 eeprom->chip = buf[0x1e];
3682 /*FIXME: Decipher more values*/
3684 else if (ftdi->type == TYPE_230X)
3688 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3690 eeprom->group0_drive = buf[0x0c] & 0x03;
3691 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3692 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3693 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3694 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3695 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3697 eeprom->invert = buf[0xb];
3702 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3703 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3704 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3705 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3707 if (eeprom->self_powered)
3708 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3710 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3711 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3712 if (eeprom->manufacturer)
3713 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3714 if (eeprom->product)
3715 fprintf(stdout, "Product: %s\n",eeprom->product);
3717 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3718 fprintf(stdout, "Checksum : %04x\n", checksum);
3719 if (ftdi->type == TYPE_R) {
3720 fprintf(stdout, "Internal EEPROM\n");
3721 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3723 else if (eeprom->chip >= 0x46)
3724 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3725 if (eeprom->suspend_dbus7)
3726 fprintf(stdout, "Suspend on DBUS7\n");
3727 if (eeprom->suspend_pull_downs)
3728 fprintf(stdout, "Pull IO pins low during suspend\n");
3729 if(eeprom->powersave)
3731 if(ftdi->type >= TYPE_232H)
3732 fprintf(stdout,"Enter low power state on ACBUS7\n");
3734 if (eeprom->remote_wakeup)
3735 fprintf(stdout, "Enable Remote Wake Up\n");
3736 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3737 if (ftdi->type >= TYPE_2232C)
3738 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3739 channel_mode[eeprom->channel_a_type],
3740 (eeprom->channel_a_driver)?" VCP":"",
3741 (eeprom->high_current_a)?" High Current IO":"");
3742 if (ftdi->type == TYPE_232H)
3744 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3745 (eeprom->clock_polarity)?"HIGH":"LOW",
3746 (eeprom->data_order)?"LSB":"MSB",
3747 (eeprom->flow_control)?"":"No ");
3749 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3750 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3751 channel_mode[eeprom->channel_b_type],
3752 (eeprom->channel_b_driver)?" VCP":"",
3753 (eeprom->high_current_b)?" High Current IO":"");
3754 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3755 eeprom->use_usb_version)
3756 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3758 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3760 fprintf(stdout,"%s has %d mA drive%s%s\n",
3761 (ftdi->type == TYPE_2232H)?"AL":"A",
3762 (eeprom->group0_drive+1) *4,
3763 (eeprom->group0_schmitt)?" Schmitt Input":"",
3764 (eeprom->group0_slew)?" Slow Slew":"");
3765 fprintf(stdout,"%s has %d mA drive%s%s\n",
3766 (ftdi->type == TYPE_2232H)?"AH":"B",
3767 (eeprom->group1_drive+1) *4,
3768 (eeprom->group1_schmitt)?" Schmitt Input":"",
3769 (eeprom->group1_slew)?" Slow Slew":"");
3770 fprintf(stdout,"%s has %d mA drive%s%s\n",
3771 (ftdi->type == TYPE_2232H)?"BL":"C",
3772 (eeprom->group2_drive+1) *4,
3773 (eeprom->group2_schmitt)?" Schmitt Input":"",
3774 (eeprom->group2_slew)?" Slow Slew":"");
3775 fprintf(stdout,"%s has %d mA drive%s%s\n",
3776 (ftdi->type == TYPE_2232H)?"BH":"D",
3777 (eeprom->group3_drive+1) *4,
3778 (eeprom->group3_schmitt)?" Schmitt Input":"",
3779 (eeprom->group3_slew)?" Slow Slew":"");
3781 else if (ftdi->type == TYPE_232H)
3783 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3784 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3785 "CLK30","CLK15","CLK7_5"
3787 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3788 (eeprom->group0_drive+1) *4,
3789 (eeprom->group0_schmitt)?" Schmitt Input":"",
3790 (eeprom->group0_slew)?" Slow Slew":"");
3791 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3792 (eeprom->group1_drive+1) *4,
3793 (eeprom->group1_schmitt)?" Schmitt Input":"",
3794 (eeprom->group1_slew)?" Slow Slew":"");
3795 for (i=0; i<10; i++)
3797 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3798 fprintf(stdout,"C%d Function: %s\n", i,
3799 cbush_mux[eeprom->cbus_function[i]]);
3802 else if (ftdi->type == TYPE_230X)
3804 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3805 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3806 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3807 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3808 "BBRD#", "TIME_STAMP", "AWAKE#",
3810 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3811 (eeprom->group0_drive+1) *4,
3812 (eeprom->group0_schmitt)?" Schmitt Input":"",
3813 (eeprom->group0_slew)?" Slow Slew":"");
3814 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3815 (eeprom->group1_drive+1) *4,
3816 (eeprom->group1_schmitt)?" Schmitt Input":"",
3817 (eeprom->group1_slew)?" Slow Slew":"");
3820 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3821 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3825 print_inverted_bits(eeprom->invert);
3828 if (ftdi->type == TYPE_R)
3830 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3831 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3832 "IOMODE","BB_WR","BB_RD"
3834 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3837 print_inverted_bits(eeprom->invert);
3841 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3842 fprintf(stdout,"C%d Function: %s\n", i,
3843 cbus_mux[eeprom->cbus_function[i]]);
3847 /* Running MPROG show that C0..3 have fixed function Synchronous
3849 fprintf(stdout,"C%d BB Function: %s\n", i,
3852 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3861 Get a value from the decoded EEPROM structure
3863 \param ftdi pointer to ftdi_context
3864 \param value_name Enum of the value to query
3865 \param value Pointer to store read value
3868 \retval -1: Value doesn't exist
3870 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3875 *value = ftdi->eeprom->vendor_id;
3878 *value = ftdi->eeprom->product_id;
3880 case RELEASE_NUMBER:
3881 *value = ftdi->eeprom->release_number;
3884 *value = ftdi->eeprom->self_powered;
3887 *value = ftdi->eeprom->remote_wakeup;
3890 *value = ftdi->eeprom->is_not_pnp;
3893 *value = ftdi->eeprom->suspend_dbus7;
3895 case IN_IS_ISOCHRONOUS:
3896 *value = ftdi->eeprom->in_is_isochronous;
3898 case OUT_IS_ISOCHRONOUS:
3899 *value = ftdi->eeprom->out_is_isochronous;
3901 case SUSPEND_PULL_DOWNS:
3902 *value = ftdi->eeprom->suspend_pull_downs;
3905 *value = ftdi->eeprom->use_serial;
3908 *value = ftdi->eeprom->usb_version;
3910 case USE_USB_VERSION:
3911 *value = ftdi->eeprom->use_usb_version;
3914 *value = ftdi->eeprom->max_power;
3916 case CHANNEL_A_TYPE:
3917 *value = ftdi->eeprom->channel_a_type;
3919 case CHANNEL_B_TYPE:
3920 *value = ftdi->eeprom->channel_b_type;
3922 case CHANNEL_A_DRIVER:
3923 *value = ftdi->eeprom->channel_a_driver;
3925 case CHANNEL_B_DRIVER:
3926 *value = ftdi->eeprom->channel_b_driver;
3928 case CHANNEL_C_DRIVER:
3929 *value = ftdi->eeprom->channel_c_driver;
3931 case CHANNEL_D_DRIVER:
3932 *value = ftdi->eeprom->channel_d_driver;
3934 case CHANNEL_A_RS485:
3935 *value = ftdi->eeprom->channel_a_rs485enable;
3937 case CHANNEL_B_RS485:
3938 *value = ftdi->eeprom->channel_b_rs485enable;
3940 case CHANNEL_C_RS485:
3941 *value = ftdi->eeprom->channel_c_rs485enable;
3943 case CHANNEL_D_RS485:
3944 *value = ftdi->eeprom->channel_d_rs485enable;
3946 case CBUS_FUNCTION_0:
3947 *value = ftdi->eeprom->cbus_function[0];
3949 case CBUS_FUNCTION_1:
3950 *value = ftdi->eeprom->cbus_function[1];
3952 case CBUS_FUNCTION_2:
3953 *value = ftdi->eeprom->cbus_function[2];
3955 case CBUS_FUNCTION_3:
3956 *value = ftdi->eeprom->cbus_function[3];
3958 case CBUS_FUNCTION_4:
3959 *value = ftdi->eeprom->cbus_function[4];
3961 case CBUS_FUNCTION_5:
3962 *value = ftdi->eeprom->cbus_function[5];
3964 case CBUS_FUNCTION_6:
3965 *value = ftdi->eeprom->cbus_function[6];
3967 case CBUS_FUNCTION_7:
3968 *value = ftdi->eeprom->cbus_function[7];
3970 case CBUS_FUNCTION_8:
3971 *value = ftdi->eeprom->cbus_function[8];
3973 case CBUS_FUNCTION_9:
3974 *value = ftdi->eeprom->cbus_function[9];
3977 *value = ftdi->eeprom->high_current;
3979 case HIGH_CURRENT_A:
3980 *value = ftdi->eeprom->high_current_a;
3982 case HIGH_CURRENT_B:
3983 *value = ftdi->eeprom->high_current_b;
3986 *value = ftdi->eeprom->invert;
3989 *value = ftdi->eeprom->group0_drive;
3991 case GROUP0_SCHMITT:
3992 *value = ftdi->eeprom->group0_schmitt;
3995 *value = ftdi->eeprom->group0_slew;
3998 *value = ftdi->eeprom->group1_drive;
4000 case GROUP1_SCHMITT:
4001 *value = ftdi->eeprom->group1_schmitt;
4004 *value = ftdi->eeprom->group1_slew;
4007 *value = ftdi->eeprom->group2_drive;
4009 case GROUP2_SCHMITT:
4010 *value = ftdi->eeprom->group2_schmitt;
4013 *value = ftdi->eeprom->group2_slew;
4016 *value = ftdi->eeprom->group3_drive;
4018 case GROUP3_SCHMITT:
4019 *value = ftdi->eeprom->group3_schmitt;
4022 *value = ftdi->eeprom->group3_slew;
4025 *value = ftdi->eeprom->powersave;
4027 case CLOCK_POLARITY:
4028 *value = ftdi->eeprom->clock_polarity;
4031 *value = ftdi->eeprom->data_order;
4034 *value = ftdi->eeprom->flow_control;
4037 *value = ftdi->eeprom->chip;
4040 *value = ftdi->eeprom->size;
4042 case EXTERNAL_OSCILLATOR:
4043 *value = ftdi->eeprom->external_oscillator;
4046 ftdi_error_return(-1, "Request for unknown EEPROM value");
4052 Set a value in the decoded EEPROM Structure
4053 No parameter checking is performed
4055 \param ftdi pointer to ftdi_context
4056 \param value_name Enum of the value to set
4060 \retval -1: Value doesn't exist
4061 \retval -2: Value not user settable
4063 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
4068 ftdi->eeprom->vendor_id = value;
4071 ftdi->eeprom->product_id = value;
4073 case RELEASE_NUMBER:
4074 ftdi->eeprom->release_number = value;
4077 ftdi->eeprom->self_powered = value;
4080 ftdi->eeprom->remote_wakeup = value;
4083 ftdi->eeprom->is_not_pnp = value;
4086 ftdi->eeprom->suspend_dbus7 = value;
4088 case IN_IS_ISOCHRONOUS:
4089 ftdi->eeprom->in_is_isochronous = value;
4091 case OUT_IS_ISOCHRONOUS:
4092 ftdi->eeprom->out_is_isochronous = value;
4094 case SUSPEND_PULL_DOWNS:
4095 ftdi->eeprom->suspend_pull_downs = value;
4098 ftdi->eeprom->use_serial = value;
4101 ftdi->eeprom->usb_version = value;
4103 case USE_USB_VERSION:
4104 ftdi->eeprom->use_usb_version = value;
4107 ftdi->eeprom->max_power = value;
4109 case CHANNEL_A_TYPE:
4110 ftdi->eeprom->channel_a_type = value;
4112 case CHANNEL_B_TYPE:
4113 ftdi->eeprom->channel_b_type = value;
4115 case CHANNEL_A_DRIVER:
4116 ftdi->eeprom->channel_a_driver = value;
4118 case CHANNEL_B_DRIVER:
4119 ftdi->eeprom->channel_b_driver = value;
4121 case CHANNEL_C_DRIVER:
4122 ftdi->eeprom->channel_c_driver = value;
4124 case CHANNEL_D_DRIVER:
4125 ftdi->eeprom->channel_d_driver = value;
4127 case CHANNEL_A_RS485:
4128 ftdi->eeprom->channel_a_rs485enable = value;
4130 case CHANNEL_B_RS485:
4131 ftdi->eeprom->channel_b_rs485enable = value;
4133 case CHANNEL_C_RS485:
4134 ftdi->eeprom->channel_c_rs485enable = value;
4136 case CHANNEL_D_RS485:
4137 ftdi->eeprom->channel_d_rs485enable = value;
4139 case CBUS_FUNCTION_0:
4140 ftdi->eeprom->cbus_function[0] = value;
4142 case CBUS_FUNCTION_1:
4143 ftdi->eeprom->cbus_function[1] = value;
4145 case CBUS_FUNCTION_2:
4146 ftdi->eeprom->cbus_function[2] = value;
4148 case CBUS_FUNCTION_3:
4149 ftdi->eeprom->cbus_function[3] = value;
4151 case CBUS_FUNCTION_4:
4152 ftdi->eeprom->cbus_function[4] = value;
4154 case CBUS_FUNCTION_5:
4155 ftdi->eeprom->cbus_function[5] = value;
4157 case CBUS_FUNCTION_6:
4158 ftdi->eeprom->cbus_function[6] = value;
4160 case CBUS_FUNCTION_7:
4161 ftdi->eeprom->cbus_function[7] = value;
4163 case CBUS_FUNCTION_8:
4164 ftdi->eeprom->cbus_function[8] = value;
4166 case CBUS_FUNCTION_9:
4167 ftdi->eeprom->cbus_function[9] = value;
4170 ftdi->eeprom->high_current = value;
4172 case HIGH_CURRENT_A:
4173 ftdi->eeprom->high_current_a = value;
4175 case HIGH_CURRENT_B:
4176 ftdi->eeprom->high_current_b = value;
4179 ftdi->eeprom->invert = value;
4182 ftdi->eeprom->group0_drive = value;
4184 case GROUP0_SCHMITT:
4185 ftdi->eeprom->group0_schmitt = value;
4188 ftdi->eeprom->group0_slew = value;
4191 ftdi->eeprom->group1_drive = value;
4193 case GROUP1_SCHMITT:
4194 ftdi->eeprom->group1_schmitt = value;
4197 ftdi->eeprom->group1_slew = value;
4200 ftdi->eeprom->group2_drive = value;
4202 case GROUP2_SCHMITT:
4203 ftdi->eeprom->group2_schmitt = value;
4206 ftdi->eeprom->group2_slew = value;
4209 ftdi->eeprom->group3_drive = value;
4211 case GROUP3_SCHMITT:
4212 ftdi->eeprom->group3_schmitt = value;
4215 ftdi->eeprom->group3_slew = value;
4218 ftdi->eeprom->chip = value;
4221 ftdi->eeprom->powersave = value;
4223 case CLOCK_POLARITY:
4224 ftdi->eeprom->clock_polarity = value;
4227 ftdi->eeprom->data_order = value;
4230 ftdi->eeprom->flow_control = value;
4233 ftdi_error_return(-2, "EEPROM Value can't be changed");
4235 case EXTERNAL_OSCILLATOR:
4236 ftdi->eeprom->external_oscillator = value;
4238 case USER_DATA_ADDR:
4239 ftdi->eeprom->user_data_addr = value;
4243 ftdi_error_return(-1, "Request to unknown EEPROM value");
4245 ftdi->eeprom->initialized_for_connected_device = 0;
4249 /** Get the read-only buffer to the binary EEPROM content
4251 \param ftdi pointer to ftdi_context
4252 \param buf buffer to receive EEPROM content
4253 \param size Size of receiving buffer
4256 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
4257 \retval -2: Not enough room to store eeprom
4259 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4261 if (!ftdi || !(ftdi->eeprom))
4262 ftdi_error_return(-1, "No appropriate structure");
4264 if (!buf || size < ftdi->eeprom->size)
4265 ftdi_error_return(-1, "Not enough room to store eeprom");
4267 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4268 if (size > FTDI_MAX_EEPROM_SIZE)
4269 size = FTDI_MAX_EEPROM_SIZE;
4271 memcpy(buf, ftdi->eeprom->buf, size);
4276 /** Set the EEPROM content from the user-supplied prefilled buffer
4278 \param ftdi pointer to ftdi_context
4279 \param buf buffer to read EEPROM content
4280 \param size Size of buffer
4283 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4285 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4287 if (!ftdi || !(ftdi->eeprom) || !buf)
4288 ftdi_error_return(-1, "No appropriate structure");
4290 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4291 if (size > FTDI_MAX_EEPROM_SIZE)
4292 size = FTDI_MAX_EEPROM_SIZE;
4294 memcpy(ftdi->eeprom->buf, buf, size);
4299 /** Set the EEPROM user data content from the user-supplied prefilled buffer
4301 \param ftdi pointer to ftdi_context
4302 \param buf buffer to read EEPROM user data content
4303 \param size Size of buffer
4306 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4308 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4310 if (!ftdi || !(ftdi->eeprom) || !buf)
4311 ftdi_error_return(-1, "No appropriate structure");
4313 ftdi->eeprom->user_data_size = size;
4314 ftdi->eeprom->user_data = buf;
4319 Read eeprom location
4321 \param ftdi pointer to ftdi_context
4322 \param eeprom_addr Address of eeprom location to be read
4323 \param eeprom_val Pointer to store read eeprom location
4326 \retval -1: read failed
4327 \retval -2: USB device unavailable
4329 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4331 unsigned char buf[2];
4333 if (ftdi == NULL || ftdi->usb_dev == NULL)
4334 ftdi_error_return(-2, "USB device unavailable");
4336 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)
4337 ftdi_error_return(-1, "reading eeprom failed");
4339 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
4347 \param ftdi pointer to ftdi_context
4350 \retval -1: read failed
4351 \retval -2: USB device unavailable
4353 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4358 if (ftdi == NULL || ftdi->usb_dev == NULL)
4359 ftdi_error_return(-2, "USB device unavailable");
4360 buf = ftdi->eeprom->buf;
4362 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4364 if (libusb_control_transfer(
4365 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4366 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4367 ftdi_error_return(-1, "reading eeprom failed");
4370 if (ftdi->type == TYPE_R)
4371 ftdi->eeprom->size = 0x80;
4372 /* Guesses size of eeprom by comparing halves
4373 - will not work with blank eeprom */
4374 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4375 ftdi->eeprom->size = -1;
4376 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4377 ftdi->eeprom->size = 0x80;
4378 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4379 ftdi->eeprom->size = 0x40;
4381 ftdi->eeprom->size = 0x100;
4386 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4387 Function is only used internally
4390 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4392 return ((value & 1) << 1) |
4393 ((value & 2) << 5) |
4394 ((value & 4) >> 2) |
4395 ((value & 8) << 4) |
4396 ((value & 16) >> 1) |
4397 ((value & 32) >> 1) |
4398 ((value & 64) >> 4) |
4399 ((value & 128) >> 2);
4403 Read the FTDIChip-ID from R-type devices
4405 \param ftdi pointer to ftdi_context
4406 \param chipid Pointer to store FTDIChip-ID
4409 \retval -1: read failed
4410 \retval -2: USB device unavailable
4412 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4414 unsigned int a = 0, b = 0;
4416 if (ftdi == NULL || ftdi->usb_dev == NULL)
4417 ftdi_error_return(-2, "USB device unavailable");
4419 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)
4421 a = a << 8 | a >> 8;
4422 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)
4424 b = b << 8 | b >> 8;
4425 a = (a << 16) | (b & 0xFFFF);
4426 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4427 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4428 *chipid = a ^ 0xa5f0f7d1;
4433 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4437 Write eeprom location
4439 \param ftdi pointer to ftdi_context
4440 \param eeprom_addr Address of eeprom location to be written
4441 \param eeprom_val Value to be written
4444 \retval -1: write failed
4445 \retval -2: USB device unavailable
4446 \retval -3: Invalid access to checksum protected area below 0x80
4447 \retval -4: Device can't access unprotected area
4448 \retval -5: Reading chip type failed
4450 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4451 unsigned short eeprom_val)
4453 int chip_type_location;
4454 unsigned short chip_type;
4456 if (ftdi == NULL || ftdi->usb_dev == NULL)
4457 ftdi_error_return(-2, "USB device unavailable");
4459 if (eeprom_addr <0x80)
4460 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4467 chip_type_location = 0x14;
4471 chip_type_location = 0x18;
4474 chip_type_location = 0x1e;
4477 ftdi_error_return(-4, "Device can't access unprotected area");
4480 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4481 ftdi_error_return(-5, "Reading failed");
4482 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4483 if ((chip_type & 0xff) != 0x66)
4485 ftdi_error_return(-6, "EEPROM is not of 93x66");
4488 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4489 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4490 NULL, 0, ftdi->usb_write_timeout) != 0)
4491 ftdi_error_return(-1, "unable to write eeprom");
4499 \param ftdi pointer to ftdi_context
4502 \retval -1: read failed
4503 \retval -2: USB device unavailable
4504 \retval -3: EEPROM not initialized for the connected device;
4506 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4508 unsigned short usb_val, status;
4510 unsigned char *eeprom;
4512 if (ftdi == NULL || ftdi->usb_dev == NULL)
4513 ftdi_error_return(-2, "USB device unavailable");
4515 if(ftdi->eeprom->initialized_for_connected_device == 0)
4516 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4518 eeprom = ftdi->eeprom->buf;
4520 /* These commands were traced while running MProg */
4521 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4523 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4525 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4528 for (i = 0; i < ftdi->eeprom->size/2; i++)
4530 /* Do not try to write to reserved area */
4531 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4535 usb_val = eeprom[i*2];
4536 usb_val += eeprom[(i*2)+1] << 8;
4537 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4538 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4539 NULL, 0, ftdi->usb_write_timeout) < 0)
4540 ftdi_error_return(-1, "unable to write eeprom");
4549 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4551 \param ftdi pointer to ftdi_context
4554 \retval -1: erase failed
4555 \retval -2: USB device unavailable
4556 \retval -3: Writing magic failed
4557 \retval -4: Read EEPROM failed
4558 \retval -5: Unexpected EEPROM value
4560 #define MAGIC 0x55aa
4561 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4563 unsigned short eeprom_value;
4564 if (ftdi == NULL || ftdi->usb_dev == NULL)
4565 ftdi_error_return(-2, "USB device unavailable");
4567 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4569 ftdi->eeprom->chip = 0;
4573 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4574 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4575 ftdi_error_return(-1, "unable to erase eeprom");
4578 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4579 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4580 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4581 Chip is 93x66 if magic is only read at word position 0xc0*/
4582 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4583 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4584 NULL, 0, ftdi->usb_write_timeout) != 0)
4585 ftdi_error_return(-3, "Writing magic failed");
4586 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4587 ftdi_error_return(-4, "Reading failed");
4588 if (eeprom_value == MAGIC)
4590 ftdi->eeprom->chip = 0x46;
4594 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4595 ftdi_error_return(-4, "Reading failed");
4596 if (eeprom_value == MAGIC)
4597 ftdi->eeprom->chip = 0x56;
4600 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4601 ftdi_error_return(-4, "Reading failed");
4602 if (eeprom_value == MAGIC)
4603 ftdi->eeprom->chip = 0x66;
4606 ftdi->eeprom->chip = -1;
4610 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4611 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4612 ftdi_error_return(-1, "unable to erase eeprom");
4617 Get string representation for last error code
4619 \param ftdi pointer to ftdi_context
4621 \retval Pointer to error string
4623 const char *ftdi_get_error_string (struct ftdi_context *ftdi)
4628 return ftdi->error_str;
4631 /* @} end of doxygen libftdi group */