1 /***************************************************************************
5 copyright : (C) 2003-2020 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 SPDX-License-Identifier: LGPL-2.1-only
8 ***************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU Lesser General Public License *
14 * version 2.1 as published by the Free Software Foundation; *
16 ***************************************************************************/
19 \mainpage libftdi API documentation
21 Library to talk to FTDI chips. You find the latest versions of libftdi at
22 https://www.intra2net.com/en/developer/libftdi/
24 The library is easy to use. Have a look at this short example:
27 More examples can be found in the "examples" directory.
29 /** \addtogroup libftdi */
39 /* Prevent deprecated messages when building library */
40 #define _FTDI_DISABLE_DEPRECATED
42 #include "ftdi_version_i.h"
44 #define ftdi_error_return(code, str) do { \
46 ftdi->error_str = str; \
48 fprintf(stderr, str); \
52 #define ftdi_error_return_free_device_list(code, str, devs) do { \
53 libusb_free_device_list(devs,1); \
54 ftdi->error_str = str; \
60 Internal function to close usb device pointer.
61 Sets ftdi->usb_dev to NULL.
64 \param ftdi pointer to ftdi_context
68 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
70 if (ftdi && ftdi->usb_dev)
72 libusb_close (ftdi->usb_dev);
75 ftdi->eeprom->initialized_for_connected_device = 0;
80 Initializes a ftdi_context.
82 \param ftdi pointer to ftdi_context
85 \retval -1: couldn't allocate read buffer
86 \retval -2: couldn't allocate struct buffer
87 \retval -3: libusb_init() failed
89 \remark This should be called before all functions
91 int ftdi_init(struct ftdi_context *ftdi)
93 struct ftdi_eeprom* eeprom;
96 ftdi->usb_read_timeout = 5000;
97 ftdi->usb_write_timeout = 5000;
99 ftdi->type = TYPE_BM; /* chip type */
101 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
103 ftdi->readbuffer = NULL;
104 ftdi->readbuffer_offset = 0;
105 ftdi->readbuffer_remaining = 0;
106 ftdi->writebuffer_chunksize = 4096;
107 ftdi->max_packet_size = 0;
108 ftdi->error_str = NULL;
109 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
111 if (libusb_init(&ftdi->usb_ctx) < 0)
112 ftdi_error_return(-3, "libusb_init() failed");
114 ftdi_set_interface(ftdi, INTERFACE_ANY);
115 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
117 eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
119 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
120 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
121 ftdi->eeprom = eeprom;
123 /* All fine. Now allocate the readbuffer */
124 return ftdi_read_data_set_chunksize(ftdi, 4096);
128 Allocate and initialize a new ftdi_context
130 \return a pointer to a new ftdi_context, or NULL on failure
132 struct ftdi_context *ftdi_new(void)
134 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
141 if (ftdi_init(ftdi) != 0)
151 Open selected channels on a chip, otherwise use first channel.
153 \param ftdi pointer to ftdi_context
154 \param interface Interface to use for FT2232C/2232H/4232H chips.
157 \retval -1: unknown interface
158 \retval -2: USB device unavailable
159 \retval -3: Device already open, interface can't be set in that state
161 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
164 ftdi_error_return(-2, "USB device unavailable");
166 if (ftdi->usb_dev != NULL)
168 int check_interface = interface;
169 if (check_interface == INTERFACE_ANY)
170 check_interface = INTERFACE_A;
172 if (ftdi->index != check_interface)
173 ftdi_error_return(-3, "Interface can not be changed on an already open device");
181 ftdi->index = INTERFACE_A;
187 ftdi->index = INTERFACE_B;
193 ftdi->index = INTERFACE_C;
199 ftdi->index = INTERFACE_D;
204 ftdi_error_return(-1, "Unknown interface");
210 Deinitializes a ftdi_context.
212 \param ftdi pointer to ftdi_context
214 void ftdi_deinit(struct ftdi_context *ftdi)
219 ftdi_usb_close_internal (ftdi);
221 if (ftdi->readbuffer != NULL)
223 free(ftdi->readbuffer);
224 ftdi->readbuffer = NULL;
227 if (ftdi->eeprom != NULL)
229 if (ftdi->eeprom->manufacturer != 0)
231 free(ftdi->eeprom->manufacturer);
232 ftdi->eeprom->manufacturer = 0;
234 if (ftdi->eeprom->product != 0)
236 free(ftdi->eeprom->product);
237 ftdi->eeprom->product = 0;
239 if (ftdi->eeprom->serial != 0)
241 free(ftdi->eeprom->serial);
242 ftdi->eeprom->serial = 0;
250 libusb_exit(ftdi->usb_ctx);
251 ftdi->usb_ctx = NULL;
256 Deinitialize and free an ftdi_context.
258 \param ftdi pointer to ftdi_context
260 void ftdi_free(struct ftdi_context *ftdi)
267 Use an already open libusb device.
269 \param ftdi pointer to ftdi_context
270 \param usb libusb libusb_device_handle to use
272 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
281 * @brief Get libftdi library version
283 * @return ftdi_version_info Library version information
285 struct ftdi_version_info ftdi_get_library_version(void)
287 struct ftdi_version_info ver;
289 ver.major = FTDI_MAJOR_VERSION;
290 ver.minor = FTDI_MINOR_VERSION;
291 ver.micro = FTDI_MICRO_VERSION;
292 ver.version_str = FTDI_VERSION_STRING;
293 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
299 Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
300 ftdi_device_list which needs to be deallocated by ftdi_list_free() after
301 use. With VID:PID 0:0, search for the default devices
302 (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015)
304 \param ftdi pointer to ftdi_context
305 \param devlist Pointer where to store list of found devices
306 \param vendor Vendor ID to search for
307 \param product Product ID to search for
309 \retval >0: number of devices found
310 \retval -3: out of memory
311 \retval -5: libusb_get_device_list() failed
312 \retval -6: libusb_get_device_descriptor() failed
314 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
316 struct ftdi_device_list **curdev;
318 libusb_device **devs;
322 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
323 ftdi_error_return(-5, "libusb_get_device_list() failed");
328 while ((dev = devs[i++]) != NULL)
330 struct libusb_device_descriptor desc;
332 if (libusb_get_device_descriptor(dev, &desc) < 0)
333 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
335 if (((vendor || product) &&
336 desc.idVendor == vendor && desc.idProduct == product) ||
337 (!(vendor || product) &&
338 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
339 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
340 || desc.idProduct == 0x6015)))
342 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
344 ftdi_error_return_free_device_list(-3, "out of memory", devs);
346 (*curdev)->next = NULL;
347 (*curdev)->dev = dev;
348 libusb_ref_device(dev);
349 curdev = &(*curdev)->next;
353 libusb_free_device_list(devs,1);
358 Frees a usb device list.
360 \param devlist USB device list created by ftdi_usb_find_all()
362 void ftdi_list_free(struct ftdi_device_list **devlist)
364 struct ftdi_device_list *curdev, *next;
366 for (curdev = *devlist; curdev != NULL;)
369 libusb_unref_device(curdev->dev);
378 Frees a usb device list.
380 \param devlist USB device list created by ftdi_usb_find_all()
382 void ftdi_list_free2(struct ftdi_device_list *devlist)
384 ftdi_list_free(&devlist);
388 Return device ID strings from the usb device.
390 The parameters manufacturer, description and serial may be NULL
391 or pointer to buffers to store the fetched strings.
393 \note Use this function only in combination with ftdi_usb_find_all()
394 as it closes the internal "usb_dev" after use.
396 \param ftdi pointer to ftdi_context
397 \param dev libusb usb_dev to use
398 \param manufacturer Store manufacturer string here if not NULL
399 \param mnf_len Buffer size of manufacturer string
400 \param description Store product description string here if not NULL
401 \param desc_len Buffer size of product description string
402 \param serial Store serial string here if not NULL
403 \param serial_len Buffer size of serial string
406 \retval -1: wrong arguments
407 \retval -4: unable to open device
408 \retval -7: get product manufacturer failed
409 \retval -8: get product description failed
410 \retval -9: get serial number failed
411 \retval -11: libusb_get_device_descriptor() failed
413 int ftdi_usb_get_strings(struct ftdi_context *ftdi,
414 struct libusb_device *dev,
415 char *manufacturer, int mnf_len,
416 char *description, int desc_len,
417 char *serial, int serial_len)
421 if ((ftdi==NULL) || (dev==NULL))
424 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
425 ftdi_error_return(-4, "libusb_open() failed");
427 // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
428 // it won't be closed either. This allows us to close it whether we actually
429 // called libusb_open() up above or not. This matches the expected behavior
430 // (and note) for ftdi_usb_get_strings().
431 ret = ftdi_usb_get_strings2(ftdi, dev,
432 manufacturer, mnf_len,
433 description, desc_len,
436 // only close it if it was successful, as all other return codes close
437 // before returning already.
439 ftdi_usb_close_internal(ftdi);
445 Return device ID strings from the usb device.
447 The parameters manufacturer, description and serial may be NULL
448 or pointer to buffers to store the fetched strings.
450 \note The old function ftdi_usb_get_strings() always closes the device.
451 This version only closes the device if it was opened by it.
453 \param ftdi pointer to ftdi_context
454 \param dev libusb usb_dev to use
455 \param manufacturer Store manufacturer string here if not NULL
456 \param mnf_len Buffer size of manufacturer string
457 \param description Store product description string here if not NULL
458 \param desc_len Buffer size of product description string
459 \param serial Store serial string here if not NULL
460 \param serial_len Buffer size of serial string
463 \retval -1: wrong arguments
464 \retval -4: unable to open device
465 \retval -7: get product manufacturer failed
466 \retval -8: get product description failed
467 \retval -9: get serial number failed
468 \retval -11: libusb_get_device_descriptor() failed
470 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
471 char *manufacturer, int mnf_len,
472 char *description, int desc_len,
473 char *serial, int serial_len)
475 struct libusb_device_descriptor desc;
478 if ((ftdi==NULL) || (dev==NULL))
481 need_open = (ftdi->usb_dev == NULL);
482 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
483 ftdi_error_return(-4, "libusb_open() failed");
485 if (libusb_get_device_descriptor(dev, &desc) < 0)
486 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
488 if (manufacturer != NULL && mnf_len > 0)
490 if (desc.iManufacturer == 0)
492 manufacturer[0] = '\0';
494 else if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
496 ftdi_usb_close_internal (ftdi);
497 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
501 if (description != NULL && desc_len > 0)
503 if (desc.iProduct == 0)
505 description[0] = '\0';
507 else if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
509 ftdi_usb_close_internal (ftdi);
510 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
514 if (serial != NULL && serial_len > 0)
516 if (desc.iSerialNumber == 0)
520 else if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
522 ftdi_usb_close_internal (ftdi);
523 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
528 ftdi_usb_close_internal (ftdi);
534 * Internal function to determine the maximum packet size.
535 * \param ftdi pointer to ftdi_context
536 * \param dev libusb usb_dev to use
537 * \retval Maximum packet size for this device
539 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
541 struct libusb_device_descriptor desc;
542 struct libusb_config_descriptor *config0;
543 unsigned int packet_size;
546 if (ftdi == NULL || dev == NULL)
549 // Determine maximum packet size. Init with default value.
550 // New hi-speed devices from FTDI use a packet size of 512 bytes
551 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
552 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
557 if (libusb_get_device_descriptor(dev, &desc) < 0)
560 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
563 if (desc.bNumConfigurations > 0)
565 if (ftdi->interface < config0->bNumInterfaces)
567 struct libusb_interface interface = config0->interface[ftdi->interface];
568 if (interface.num_altsetting > 0)
570 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
571 if (descriptor.bNumEndpoints > 0)
573 packet_size = descriptor.endpoint[0].wMaxPacketSize;
579 libusb_free_config_descriptor (config0);
584 Opens a ftdi device given by an usb_device.
586 \param ftdi pointer to ftdi_context
587 \param dev libusb usb_dev to use
590 \retval -3: unable to config device
591 \retval -4: unable to open device
592 \retval -5: unable to claim device
593 \retval -6: reset failed
594 \retval -7: set baudrate failed
595 \retval -8: ftdi context invalid
596 \retval -9: libusb_get_device_descriptor() failed
597 \retval -10: libusb_get_config_descriptor() failed
598 \retval -11: libusb_detach_kernel_driver() failed
599 \retval -12: libusb_get_configuration() failed
601 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
603 struct libusb_device_descriptor desc;
604 struct libusb_config_descriptor *config0;
605 int cfg, cfg0, detach_errno = 0;
608 ftdi_error_return(-8, "ftdi context invalid");
610 if (libusb_open(dev, &ftdi->usb_dev) < 0)
611 ftdi_error_return(-4, "libusb_open() failed");
613 if (libusb_get_device_descriptor(dev, &desc) < 0)
614 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
616 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
617 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
618 cfg0 = config0->bConfigurationValue;
619 libusb_free_config_descriptor (config0);
621 // Try to detach ftdi_sio kernel module.
623 // The return code is kept in a separate variable and only parsed
624 // if usb_set_configuration() or usb_claim_interface() fails as the
625 // detach operation might be denied and everything still works fine.
626 // Likely scenario is a static ftdi_sio kernel module.
627 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
629 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
630 detach_errno = errno;
632 else if (ftdi->module_detach_mode == AUTO_DETACH_REATACH_SIO_MODULE)
634 if (libusb_set_auto_detach_kernel_driver(ftdi->usb_dev, 1) != LIBUSB_SUCCESS)
635 detach_errno = errno;
638 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
639 ftdi_error_return(-12, "libusb_get_configuration () failed");
640 // set configuration (needed especially for windows)
641 // tolerate EBUSY: one device with one configuration, but two interfaces
642 // and libftdi sessions to both interfaces (e.g. FT2232)
643 if (desc.bNumConfigurations > 0 && cfg != cfg0)
645 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
647 ftdi_usb_close_internal (ftdi);
648 if (detach_errno == EPERM)
650 ftdi_error_return(-8, "inappropriate permissions on device!");
654 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
659 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
661 ftdi_usb_close_internal (ftdi);
662 if (detach_errno == EPERM)
664 ftdi_error_return(-8, "inappropriate permissions on device!");
668 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
672 if (ftdi_usb_reset (ftdi) != 0)
674 ftdi_usb_close_internal (ftdi);
675 ftdi_error_return(-6, "ftdi_usb_reset failed");
678 // Try to guess chip type
679 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
680 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
681 && desc.iSerialNumber == 0))
682 ftdi->type = TYPE_BM;
683 else if (desc.bcdDevice == 0x200)
684 ftdi->type = TYPE_AM;
685 else if (desc.bcdDevice == 0x500)
686 ftdi->type = TYPE_2232C;
687 else if (desc.bcdDevice == 0x600)
689 else if (desc.bcdDevice == 0x700)
690 ftdi->type = TYPE_2232H;
691 else if (desc.bcdDevice == 0x800)
692 ftdi->type = TYPE_4232H;
693 else if (desc.bcdDevice == 0x900)
694 ftdi->type = TYPE_232H;
695 else if (desc.bcdDevice == 0x1000)
696 ftdi->type = TYPE_230X;
698 // Determine maximum packet size
699 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
701 if (ftdi_set_baudrate (ftdi, 9600) != 0)
703 ftdi_usb_close_internal (ftdi);
704 ftdi_error_return(-7, "set baudrate failed");
707 ftdi_error_return(0, "all fine");
711 Opens the first device with a given vendor and product ids.
713 \param ftdi pointer to ftdi_context
714 \param vendor Vendor ID
715 \param product Product ID
717 \retval same as ftdi_usb_open_desc()
719 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
721 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
725 Opens the first device with a given, vendor id, product id,
726 description and serial.
728 \param ftdi pointer to ftdi_context
729 \param vendor Vendor ID
730 \param product Product ID
731 \param description Description to search for. Use NULL if not needed.
732 \param serial Serial to search for. Use NULL if not needed.
735 \retval -3: usb device not found
736 \retval -4: unable to open device
737 \retval -5: unable to claim device
738 \retval -6: reset failed
739 \retval -7: set baudrate failed
740 \retval -8: get product description failed
741 \retval -9: get serial number failed
742 \retval -12: libusb_get_device_list() failed
743 \retval -13: libusb_get_device_descriptor() failed
745 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
746 const char* description, const char* serial)
748 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
752 Opens the index-th device with a given, vendor id, product id,
753 description and serial.
755 \param ftdi pointer to ftdi_context
756 \param vendor Vendor ID
757 \param product Product ID
758 \param description Description to search for. Use NULL if not needed.
759 \param serial Serial to search for. Use NULL if not needed.
760 \param index Number of matching device to open if there are more than one, starts with 0.
763 \retval -1: usb_find_busses() failed
764 \retval -2: usb_find_devices() failed
765 \retval -3: usb device not found
766 \retval -4: unable to open device
767 \retval -5: unable to claim device
768 \retval -6: reset failed
769 \retval -7: set baudrate failed
770 \retval -8: get product description failed
771 \retval -9: get serial number failed
772 \retval -10: unable to close device
773 \retval -11: ftdi context invalid
774 \retval -12: libusb_get_device_list() failed
776 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
777 const char* description, const char* serial, unsigned int index)
780 libusb_device **devs;
785 ftdi_error_return(-11, "ftdi context invalid");
787 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
788 ftdi_error_return(-12, "libusb_get_device_list() failed");
790 while ((dev = devs[i++]) != NULL)
792 struct libusb_device_descriptor desc;
795 if (libusb_get_device_descriptor(dev, &desc) < 0)
796 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
798 if (desc.idVendor == vendor && desc.idProduct == product)
800 if (libusb_open(dev, &ftdi->usb_dev) < 0)
801 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
803 if (description != NULL)
805 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
807 ftdi_usb_close_internal (ftdi);
808 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
810 if (strncmp(string, description, sizeof(string)) != 0)
812 ftdi_usb_close_internal (ftdi);
818 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
820 ftdi_usb_close_internal (ftdi);
821 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
823 if (strncmp(string, serial, sizeof(string)) != 0)
825 ftdi_usb_close_internal (ftdi);
830 ftdi_usb_close_internal (ftdi);
838 res = ftdi_usb_open_dev(ftdi, dev);
839 libusb_free_device_list(devs,1);
845 ftdi_error_return_free_device_list(-3, "device not found", devs);
849 Opens the device at a given USB bus and device address.
851 \param ftdi pointer to ftdi_context
852 \param bus Bus number
853 \param addr Device address
856 \retval -1: usb_find_busses() failed
857 \retval -2: usb_find_devices() failed
858 \retval -3: usb device not found
859 \retval -4: unable to open device
860 \retval -5: unable to claim device
861 \retval -6: reset failed
862 \retval -7: set baudrate failed
863 \retval -8: get product description failed
864 \retval -9: get serial number failed
865 \retval -10: unable to close device
866 \retval -11: ftdi context invalid
867 \retval -12: libusb_get_device_list() failed
869 int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
872 libusb_device **devs;
876 ftdi_error_return(-11, "ftdi context invalid");
878 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
879 ftdi_error_return(-12, "libusb_get_device_list() failed");
881 while ((dev = devs[i++]) != NULL)
883 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
886 res = ftdi_usb_open_dev(ftdi, dev);
887 libusb_free_device_list(devs,1);
893 ftdi_error_return_free_device_list(-3, "device not found", devs);
897 Opens the ftdi-device described by a description-string.
898 Intended to be used for parsing a device-description given as commandline argument.
900 \param ftdi pointer to ftdi_context
901 \param description NULL-terminated description-string, using this format:
902 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
903 \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")
904 \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
905 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
907 \note The description format may be extended in later versions.
910 \retval -2: libusb_get_device_list() failed
911 \retval -3: usb device not found
912 \retval -4: unable to open device
913 \retval -5: unable to claim device
914 \retval -6: reset failed
915 \retval -7: set baudrate failed
916 \retval -8: get product description failed
917 \retval -9: get serial number failed
918 \retval -10: unable to close device
919 \retval -11: illegal description format
920 \retval -12: ftdi context invalid
922 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
925 ftdi_error_return(-12, "ftdi context invalid");
927 if (description[0] == 0 || description[1] != ':')
928 ftdi_error_return(-11, "illegal description format");
930 if (description[0] == 'd')
933 libusb_device **devs;
934 unsigned int bus_number, device_address;
937 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
938 ftdi_error_return(-2, "libusb_get_device_list() failed");
940 /* XXX: This doesn't handle symlinks/odd paths/etc... */
941 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
942 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
944 while ((dev = devs[i++]) != NULL)
947 if (bus_number == libusb_get_bus_number (dev)
948 && device_address == libusb_get_device_address (dev))
950 ret = ftdi_usb_open_dev(ftdi, dev);
951 libusb_free_device_list(devs,1);
957 ftdi_error_return_free_device_list(-3, "device not found", devs);
959 else if (description[0] == 'i' || description[0] == 's')
962 unsigned int product;
963 unsigned int index=0;
964 const char *serial=NULL;
965 const char *startp, *endp;
968 startp=description+2;
969 vendor=strtoul((char*)startp,(char**)&endp,0);
970 if (*endp != ':' || endp == startp || errno != 0)
971 ftdi_error_return(-11, "illegal description format");
974 product=strtoul((char*)startp,(char**)&endp,0);
975 if (endp == startp || errno != 0)
976 ftdi_error_return(-11, "illegal description format");
978 if (description[0] == 'i' && *endp != 0)
980 /* optional index field in i-mode */
982 ftdi_error_return(-11, "illegal description format");
985 index=strtoul((char*)startp,(char**)&endp,0);
986 if (*endp != 0 || endp == startp || errno != 0)
987 ftdi_error_return(-11, "illegal description format");
989 if (description[0] == 's')
992 ftdi_error_return(-11, "illegal description format");
994 /* rest of the description is the serial */
998 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
1002 ftdi_error_return(-11, "illegal description format");
1007 Resets the ftdi device.
1009 \param ftdi pointer to ftdi_context
1012 \retval -1: FTDI reset failed
1013 \retval -2: USB device unavailable
1015 int ftdi_usb_reset(struct ftdi_context *ftdi)
1017 if (ftdi == NULL || ftdi->usb_dev == NULL)
1018 ftdi_error_return(-2, "USB device unavailable");
1020 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1021 SIO_RESET_REQUEST, SIO_RESET_SIO,
1022 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1023 ftdi_error_return(-1,"FTDI reset failed");
1025 // Invalidate data in the readbuffer
1026 ftdi->readbuffer_offset = 0;
1027 ftdi->readbuffer_remaining = 0;
1033 Clears the read buffer on the chip and the internal read buffer.
1034 This is the correct behavior for an RX flush.
1036 \param ftdi pointer to ftdi_context
1039 \retval -1: read buffer purge failed
1040 \retval -2: USB device unavailable
1042 int ftdi_tciflush(struct ftdi_context *ftdi)
1044 if (ftdi == NULL || ftdi->usb_dev == NULL)
1045 ftdi_error_return(-2, "USB device unavailable");
1047 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1048 SIO_RESET_REQUEST, SIO_TCIFLUSH,
1049 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1050 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1052 // Invalidate data in the readbuffer
1053 ftdi->readbuffer_offset = 0;
1054 ftdi->readbuffer_remaining = 0;
1061 Clears the write buffer on the chip and the internal read buffer.
1062 This is incorrect behavior for an RX flush.
1064 \param ftdi pointer to ftdi_context
1067 \retval -1: write buffer purge failed
1068 \retval -2: USB device unavailable
1070 \deprecated Use \ref ftdi_tciflush(struct ftdi_context *ftdi)
1072 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
1074 if (ftdi == NULL || ftdi->usb_dev == NULL)
1075 ftdi_error_return(-2, "USB device unavailable");
1077 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1078 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
1079 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1080 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1082 // Invalidate data in the readbuffer
1083 ftdi->readbuffer_offset = 0;
1084 ftdi->readbuffer_remaining = 0;
1090 Clears the write buffer on the chip.
1091 This is correct behavior for a TX flush.
1093 \param ftdi pointer to ftdi_context
1096 \retval -1: write buffer purge failed
1097 \retval -2: USB device unavailable
1099 int ftdi_tcoflush(struct ftdi_context *ftdi)
1101 if (ftdi == NULL || ftdi->usb_dev == NULL)
1102 ftdi_error_return(-2, "USB device unavailable");
1104 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1105 SIO_RESET_REQUEST, SIO_TCOFLUSH,
1106 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1107 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1114 Clears the read buffer on the chip.
1115 This is incorrect behavior for a TX flush.
1117 \param ftdi pointer to ftdi_context
1120 \retval -1: read buffer purge failed
1121 \retval -2: USB device unavailable
1123 \deprecated Use \ref ftdi_tcoflush(struct ftdi_context *ftdi)
1125 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
1127 if (ftdi == NULL || ftdi->usb_dev == NULL)
1128 ftdi_error_return(-2, "USB device unavailable");
1130 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1131 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
1132 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1133 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1139 Clears the RX and TX FIFOs on the chip and the internal read buffer.
1140 This is correct behavior for both RX and TX flush.
1142 \param ftdi pointer to ftdi_context
1145 \retval -1: read buffer purge failed
1146 \retval -2: write buffer purge failed
1147 \retval -3: USB device unavailable
1149 int ftdi_tcioflush(struct ftdi_context *ftdi)
1153 if (ftdi == NULL || ftdi->usb_dev == NULL)
1154 ftdi_error_return(-3, "USB device unavailable");
1156 result = ftdi_tcoflush(ftdi);
1160 result = ftdi_tciflush(ftdi);
1168 Clears the buffers on the chip and the internal read buffer.
1169 While coded incorrectly, the result is satisfactory.
1171 \param ftdi pointer to ftdi_context
1174 \retval -1: read buffer purge failed
1175 \retval -2: write buffer purge failed
1176 \retval -3: USB device unavailable
1178 \deprecated Use \ref ftdi_tcioflush(struct ftdi_context *ftdi)
1180 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
1184 if (ftdi == NULL || ftdi->usb_dev == NULL)
1185 ftdi_error_return(-3, "USB device unavailable");
1187 result = ftdi_usb_purge_rx_buffer(ftdi);
1191 result = ftdi_usb_purge_tx_buffer(ftdi);
1201 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
1203 \param ftdi pointer to ftdi_context
1206 \retval -1: usb_release failed
1207 \retval -3: ftdi context invalid
1209 int ftdi_usb_close(struct ftdi_context *ftdi)
1214 ftdi_error_return(-3, "ftdi context invalid");
1216 if (ftdi->usb_dev != NULL)
1217 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1220 ftdi_usb_close_internal (ftdi);
1225 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1226 to encoded divisor and the achievable baudrate
1227 Function is only used internally
1234 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1235 The fractional part has frac_code encoding
1237 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1240 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1241 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1242 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1243 int divisor, best_divisor, best_baud, best_baud_diff;
1245 divisor = 24000000 / baudrate;
1247 // Round down to supported fraction (AM only)
1248 divisor -= am_adjust_dn[divisor & 7];
1250 // Try this divisor and the one above it (because division rounds down)
1254 for (i = 0; i < 2; i++)
1256 int try_divisor = divisor + i;
1260 // Round up to supported divisor value
1261 if (try_divisor <= 8)
1263 // Round up to minimum supported divisor
1266 else if (divisor < 16)
1268 // AM doesn't support divisors 9 through 15 inclusive
1273 // Round up to supported fraction (AM only)
1274 try_divisor += am_adjust_up[try_divisor & 7];
1275 if (try_divisor > 0x1FFF8)
1277 // Round down to maximum supported divisor value (for AM)
1278 try_divisor = 0x1FFF8;
1281 // Get estimated baud rate (to nearest integer)
1282 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1283 // Get absolute difference from requested baud rate
1284 if (baud_estimate < baudrate)
1286 baud_diff = baudrate - baud_estimate;
1290 baud_diff = baud_estimate - baudrate;
1292 if (i == 0 || baud_diff < best_baud_diff)
1294 // Closest to requested baud rate so far
1295 best_divisor = try_divisor;
1296 best_baud = baud_estimate;
1297 best_baud_diff = baud_diff;
1300 // Spot on! No point trying
1305 // Encode the best divisor value
1306 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1307 // Deal with special cases for encoded value
1308 if (*encoded_divisor == 1)
1310 *encoded_divisor = 0; // 3000000 baud
1312 else if (*encoded_divisor == 0x4001)
1314 *encoded_divisor = 1; // 2000000 baud (BM only)
1319 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1320 to encoded divisor and the achievable baudrate
1321 Function is only used internally
1328 From /2, 0.125 steps may be taken.
1329 The fractional part has frac_code encoding
1331 value[13:0] of value is the divisor
1332 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1334 H Type have all features above with
1335 {index[8],value[15:14]} is the encoded subdivisor
1337 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1338 {index[0],value[15:14]} is the encoded subdivisor
1340 AM Type chips have only four fractional subdivisors at value[15:14]
1341 for subdivisors 0, 0.5, 0.25, 0.125
1343 static int ftdi_to_clkbits(int baudrate, int clk, int clk_div, unsigned long *encoded_divisor)
1345 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1347 int divisor, best_divisor;
1348 if (baudrate >= clk/clk_div)
1350 *encoded_divisor = 0;
1351 best_baud = clk/clk_div;
1353 else if (baudrate >= clk/(clk_div + clk_div/2))
1355 *encoded_divisor = 1;
1356 best_baud = clk/(clk_div + clk_div/2);
1358 else if (baudrate >= clk/(2*clk_div))
1360 *encoded_divisor = 2;
1361 best_baud = clk/(2*clk_div);
1365 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1366 divisor = clk*16/clk_div / baudrate;
1367 if (divisor & 1) /* Decide if to round up or down*/
1368 best_divisor = divisor /2 +1;
1370 best_divisor = divisor/2;
1371 if(best_divisor > 0x20000)
1372 best_divisor = 0x1ffff;
1373 best_baud = clk*16/clk_div/best_divisor;
1374 if (best_baud & 1) /* Decide if to round up or down*/
1375 best_baud = best_baud /2 +1;
1377 best_baud = best_baud /2;
1378 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1383 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1384 Function is only used internally
1387 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1388 unsigned short *value, unsigned short *index)
1391 unsigned long encoded_divisor;
1399 #define H_CLK 120000000
1400 #define C_CLK 48000000
1401 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1403 if(baudrate*10 > H_CLK /0x3fff)
1405 /* On H Devices, use 12 000 000 Baudrate when possible
1406 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1407 three fractional bits and a 120 MHz clock
1408 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1409 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1410 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1411 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1414 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1416 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
1418 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1422 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1424 // Split into "value" and "index" values
1425 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1426 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1428 *index = (unsigned short)(encoded_divisor >> 8);
1430 *index |= ftdi->index;
1433 *index = (unsigned short)(encoded_divisor >> 16);
1435 // Return the nearest baud rate
1440 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1441 * Do not use, it's only for the unit test framework
1443 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1444 unsigned short *value, unsigned short *index)
1446 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1450 Sets the chip baud rate
1452 \param ftdi pointer to ftdi_context
1453 \param baudrate baud rate to set
1456 \retval -1: invalid baudrate
1457 \retval -2: setting baudrate failed
1458 \retval -3: USB device unavailable
1460 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1462 unsigned short value, index;
1463 int actual_baudrate;
1465 if (ftdi == NULL || ftdi->usb_dev == NULL)
1466 ftdi_error_return(-3, "USB device unavailable");
1468 if (ftdi->bitbang_enabled)
1470 baudrate = baudrate*4;
1473 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1474 if (actual_baudrate <= 0)
1475 ftdi_error_return (-1, "Silly baudrate <= 0.");
1477 // Check within tolerance (about 5%)
1478 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1479 || ((actual_baudrate < baudrate)
1480 ? (actual_baudrate * 21 < baudrate * 20)
1481 : (baudrate * 21 < actual_baudrate * 20)))
1482 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1484 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1485 SIO_SET_BAUDRATE_REQUEST, value,
1486 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1487 ftdi_error_return (-2, "Setting new baudrate failed");
1489 ftdi->baudrate = baudrate;
1494 Set (RS232) line characteristics.
1495 The break type can only be set via ftdi_set_line_property2()
1496 and defaults to "off".
1498 \param ftdi pointer to ftdi_context
1499 \param bits Number of bits
1500 \param sbit Number of stop bits
1501 \param parity Parity mode
1504 \retval -1: Setting line property failed
1506 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1507 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1509 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1513 Set (RS232) line characteristics
1515 \param ftdi pointer to ftdi_context
1516 \param bits Number of bits
1517 \param sbit Number of stop bits
1518 \param parity Parity mode
1519 \param break_type Break type
1522 \retval -1: Setting line property failed
1523 \retval -2: USB device unavailable
1525 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1526 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1527 enum ftdi_break_type break_type)
1529 unsigned short value = bits;
1531 if (ftdi == NULL || ftdi->usb_dev == NULL)
1532 ftdi_error_return(-2, "USB device unavailable");
1537 value |= (0x00 << 8);
1540 value |= (0x01 << 8);
1543 value |= (0x02 << 8);
1546 value |= (0x03 << 8);
1549 value |= (0x04 << 8);
1556 value |= (0x00 << 11);
1559 value |= (0x01 << 11);
1562 value |= (0x02 << 11);
1569 value |= (0x00 << 14);
1572 value |= (0x01 << 14);
1576 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1577 SIO_SET_DATA_REQUEST, value,
1578 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1579 ftdi_error_return (-1, "Setting new line property failed");
1585 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1587 \param ftdi pointer to ftdi_context
1588 \param buf Buffer with the data
1589 \param size Size of the buffer
1591 \retval -666: USB device unavailable
1592 \retval <0: error code from usb_bulk_write()
1593 \retval >0: number of bytes written
1595 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1600 if (ftdi == NULL || ftdi->usb_dev == NULL)
1601 ftdi_error_return(-666, "USB device unavailable");
1603 while (offset < size)
1605 int write_size = ftdi->writebuffer_chunksize;
1607 if (offset+write_size > size)
1608 write_size = size-offset;
1610 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1611 ftdi_error_return(-1, "usb bulk write failed");
1613 offset += actual_length;
1619 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1621 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1622 struct ftdi_context *ftdi = tc->ftdi;
1623 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1625 packet_size = ftdi->max_packet_size;
1627 actual_length = transfer->actual_length;
1629 if (actual_length > 2)
1631 // skip FTDI status bytes.
1632 // Maybe stored in the future to enable modem use
1633 num_of_chunks = actual_length / packet_size;
1634 chunk_remains = actual_length % packet_size;
1635 //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);
1637 ftdi->readbuffer_offset += 2;
1640 if (actual_length > packet_size - 2)
1642 for (i = 1; i < num_of_chunks; i++)
1643 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1644 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1646 if (chunk_remains > 2)
1648 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1649 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1651 actual_length -= 2*num_of_chunks;
1654 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1657 if (actual_length > 0)
1659 // data still fits in buf?
1660 if (tc->offset + actual_length <= tc->size)
1662 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1663 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1664 tc->offset += actual_length;
1666 ftdi->readbuffer_offset = 0;
1667 ftdi->readbuffer_remaining = 0;
1669 /* Did we read exactly the right amount of bytes? */
1670 if (tc->offset == tc->size)
1672 //printf("read_data exact rem %d offset %d\n",
1673 //ftdi->readbuffer_remaining, offset);
1680 // only copy part of the data or size <= readbuffer_chunksize
1681 int part_size = tc->size - tc->offset;
1682 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1683 tc->offset += part_size;
1685 ftdi->readbuffer_offset += part_size;
1686 ftdi->readbuffer_remaining = actual_length - part_size;
1688 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1689 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1696 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1697 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1700 ret = libusb_submit_transfer (transfer);
1707 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1709 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1710 struct ftdi_context *ftdi = tc->ftdi;
1712 tc->offset += transfer->actual_length;
1714 if (tc->offset == tc->size)
1720 int write_size = ftdi->writebuffer_chunksize;
1723 if (tc->offset + write_size > tc->size)
1724 write_size = tc->size - tc->offset;
1726 transfer->length = write_size;
1727 transfer->buffer = tc->buf + tc->offset;
1729 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1730 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1733 ret = libusb_submit_transfer (transfer);
1742 Writes data to the chip. Does not wait for completion of the transfer
1743 nor does it make sure that the transfer was successful.
1745 Use libusb 1.0 asynchronous API.
1747 \param ftdi pointer to ftdi_context
1748 \param buf Buffer with the data
1749 \param size Size of the buffer
1751 \retval NULL: Some error happens when submit transfer
1752 \retval !NULL: Pointer to a ftdi_transfer_control
1755 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1757 struct ftdi_transfer_control *tc;
1758 struct libusb_transfer *transfer;
1759 int write_size, ret;
1761 if (ftdi == NULL || ftdi->usb_dev == NULL)
1764 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1768 transfer = libusb_alloc_transfer(0);
1781 if (size < (int)ftdi->writebuffer_chunksize)
1784 write_size = ftdi->writebuffer_chunksize;
1786 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1787 write_size, ftdi_write_data_cb, tc,
1788 ftdi->usb_write_timeout);
1789 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1791 ret = libusb_submit_transfer(transfer);
1794 libusb_free_transfer(transfer);
1798 tc->transfer = transfer;
1804 Reads data from the chip. Does not wait for completion of the transfer
1805 nor does it make sure that the transfer was successful.
1807 Use libusb 1.0 asynchronous API.
1809 \param ftdi pointer to ftdi_context
1810 \param buf Buffer with the data
1811 \param size Size of the buffer
1813 \retval NULL: Some error happens when submit transfer
1814 \retval !NULL: Pointer to a ftdi_transfer_control
1817 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1819 struct ftdi_transfer_control *tc;
1820 struct libusb_transfer *transfer;
1823 if (ftdi == NULL || ftdi->usb_dev == NULL)
1826 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1834 if (size <= (int)ftdi->readbuffer_remaining)
1836 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1839 ftdi->readbuffer_remaining -= size;
1840 ftdi->readbuffer_offset += size;
1842 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1846 tc->transfer = NULL;
1851 if (ftdi->readbuffer_remaining != 0)
1853 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1855 tc->offset = ftdi->readbuffer_remaining;
1860 transfer = libusb_alloc_transfer(0);
1867 ftdi->readbuffer_remaining = 0;
1868 ftdi->readbuffer_offset = 0;
1870 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);
1871 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1873 ret = libusb_submit_transfer(transfer);
1876 libusb_free_transfer(transfer);
1880 tc->transfer = transfer;
1886 Wait for completion of the transfer.
1888 Use libusb 1.0 asynchronous API.
1890 \param tc pointer to ftdi_transfer_control
1892 \retval < 0: Some error happens
1893 \retval >= 0: Data size transferred
1896 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1899 struct timeval to = { 0, 0 };
1900 while (!tc->completed)
1902 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1903 &to, &tc->completed);
1906 if (ret == LIBUSB_ERROR_INTERRUPTED)
1908 libusb_cancel_transfer(tc->transfer);
1909 while (!tc->completed)
1910 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1911 &to, &tc->completed) < 0)
1913 libusb_free_transfer(tc->transfer);
1921 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1922 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1926 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1928 libusb_free_transfer(tc->transfer);
1935 Cancel transfer and wait for completion.
1937 Use libusb 1.0 asynchronous API.
1939 \param tc pointer to ftdi_transfer_control
1940 \param to pointer to timeout value or NULL for infinite
1943 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
1944 struct timeval * to)
1946 struct timeval tv = { 0, 0 };
1948 if (!tc->completed && tc->transfer != NULL)
1953 libusb_cancel_transfer(tc->transfer);
1954 while (!tc->completed)
1956 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1962 libusb_free_transfer(tc->transfer);
1968 Configure write buffer chunk size.
1971 \param ftdi pointer to ftdi_context
1972 \param chunksize Chunk size
1975 \retval -1: ftdi context invalid
1977 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1980 ftdi_error_return(-1, "ftdi context invalid");
1982 ftdi->writebuffer_chunksize = chunksize;
1987 Get write buffer chunk size.
1989 \param ftdi pointer to ftdi_context
1990 \param chunksize Pointer to store chunk size in
1993 \retval -1: ftdi context invalid
1995 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1998 ftdi_error_return(-1, "ftdi context invalid");
2000 *chunksize = ftdi->writebuffer_chunksize;
2005 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
2007 Automatically strips the two modem status bytes transferred during every read.
2009 \param ftdi pointer to ftdi_context
2010 \param buf Buffer to store data in
2011 \param size Size of the buffer
2013 \retval -666: USB device unavailable
2014 \retval <0: error code from libusb_bulk_transfer()
2015 \retval 0: no data was available
2016 \retval >0: number of bytes read
2019 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
2021 int offset = 0, ret, i, num_of_chunks, chunk_remains;
2023 int actual_length = 1;
2025 if (ftdi == NULL || ftdi->usb_dev == NULL)
2026 ftdi_error_return(-666, "USB device unavailable");
2028 // Packet size sanity check (avoid division by zero)
2029 packet_size = ftdi->max_packet_size;
2030 if (packet_size == 0)
2031 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
2033 // everything we want is still in the readbuffer?
2034 if (size <= (int)ftdi->readbuffer_remaining)
2036 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
2039 ftdi->readbuffer_remaining -= size;
2040 ftdi->readbuffer_offset += size;
2042 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
2046 // something still in the readbuffer, but not enough to satisfy 'size'?
2047 if (ftdi->readbuffer_remaining != 0)
2049 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
2052 offset += ftdi->readbuffer_remaining;
2054 // do the actual USB read
2055 while (offset < size && actual_length > 0)
2057 ftdi->readbuffer_remaining = 0;
2058 ftdi->readbuffer_offset = 0;
2059 /* returns how much received */
2060 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
2062 ftdi_error_return(ret, "usb bulk read failed");
2064 if (actual_length > 2)
2066 // skip FTDI status bytes.
2067 // Maybe stored in the future to enable modem use
2068 num_of_chunks = actual_length / packet_size;
2069 chunk_remains = actual_length % packet_size;
2070 //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);
2072 ftdi->readbuffer_offset += 2;
2075 if (actual_length > packet_size - 2)
2077 for (i = 1; i < num_of_chunks; i++)
2078 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
2079 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
2081 if (chunk_remains > 2)
2083 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
2084 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
2086 actual_length -= 2*num_of_chunks;
2089 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
2092 else if (actual_length <= 2)
2094 // no more data to read?
2097 if (actual_length > 0)
2099 // data still fits in buf?
2100 if (offset+actual_length <= size)
2102 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
2103 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
2104 offset += actual_length;
2106 /* Did we read exactly the right amount of bytes? */
2108 //printf("read_data exact rem %d offset %d\n",
2109 //ftdi->readbuffer_remaining, offset);
2114 // only copy part of the data or size <= readbuffer_chunksize
2115 int part_size = size-offset;
2116 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
2118 ftdi->readbuffer_offset += part_size;
2119 ftdi->readbuffer_remaining = actual_length-part_size;
2120 offset += part_size;
2122 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
2123 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
2134 Configure read buffer chunk size.
2137 Automatically reallocates the buffer.
2139 \param ftdi pointer to ftdi_context
2140 \param chunksize Chunk size
2143 \retval -1: ftdi context invalid
2145 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
2147 unsigned char *new_buf;
2150 ftdi_error_return(-1, "ftdi context invalid");
2152 // Invalidate all remaining data
2153 ftdi->readbuffer_offset = 0;
2154 ftdi->readbuffer_remaining = 0;
2156 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
2157 which is defined in libusb-1.0. Otherwise, each USB read request will
2158 be divided into multiple URBs. This will cause issues on Linux kernel
2159 older than 2.6.32. */
2160 if (chunksize > 16384)
2164 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2165 ftdi_error_return(-1, "out of memory for readbuffer");
2167 ftdi->readbuffer = new_buf;
2168 ftdi->readbuffer_chunksize = chunksize;
2174 Get read buffer chunk size.
2176 \param ftdi pointer to ftdi_context
2177 \param chunksize Pointer to store chunk size in
2180 \retval -1: FTDI context invalid
2182 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2185 ftdi_error_return(-1, "FTDI context invalid");
2187 *chunksize = ftdi->readbuffer_chunksize;
2192 Enable/disable bitbang modes.
2194 \param ftdi pointer to ftdi_context
2195 \param bitmask Bitmask to configure lines.
2196 HIGH/ON value configures a line as output.
2197 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
2200 \retval -1: can't enable bitbang mode
2201 \retval -2: USB device unavailable
2203 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2205 unsigned short usb_val;
2207 if (ftdi == NULL || ftdi->usb_dev == NULL)
2208 ftdi_error_return(-2, "USB device unavailable");
2210 usb_val = bitmask; // low byte: bitmask
2211 usb_val |= (mode << 8);
2212 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)
2213 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2215 ftdi->bitbang_mode = mode;
2216 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2221 Disable bitbang mode.
2223 \param ftdi pointer to ftdi_context
2226 \retval -1: can't disable bitbang mode
2227 \retval -2: USB device unavailable
2229 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
2231 if (ftdi == NULL || ftdi->usb_dev == NULL)
2232 ftdi_error_return(-2, "USB device unavailable");
2234 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)
2235 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2237 ftdi->bitbang_enabled = 0;
2243 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
2245 \param ftdi pointer to ftdi_context
2246 \param pins Pointer to store pins into
2249 \retval -1: read pins failed
2250 \retval -2: USB device unavailable
2252 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2254 if (ftdi == NULL || ftdi->usb_dev == NULL)
2255 ftdi_error_return(-2, "USB device unavailable");
2257 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)
2258 ftdi_error_return(-1, "read pins failed");
2266 The FTDI chip keeps data in the internal buffer for a specific
2267 amount of time if the buffer is not full yet to decrease
2268 load on the usb bus.
2270 \param ftdi pointer to ftdi_context
2271 \param latency Value between 1 and 255
2274 \retval -1: latency out of range
2275 \retval -2: unable to set latency timer
2276 \retval -3: USB device unavailable
2278 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2280 unsigned short usb_val;
2283 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2285 if (ftdi == NULL || ftdi->usb_dev == NULL)
2286 ftdi_error_return(-3, "USB device unavailable");
2289 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)
2290 ftdi_error_return(-2, "unable to set latency timer");
2298 \param ftdi pointer to ftdi_context
2299 \param latency Pointer to store latency value in
2302 \retval -1: unable to get latency timer
2303 \retval -2: USB device unavailable
2305 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2307 unsigned short usb_val;
2309 if (ftdi == NULL || ftdi->usb_dev == NULL)
2310 ftdi_error_return(-2, "USB device unavailable");
2312 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)
2313 ftdi_error_return(-1, "reading latency timer failed");
2315 *latency = (unsigned char)usb_val;
2320 Poll modem status information
2322 This function allows the retrieve the two status bytes of the device.
2323 The device sends these bytes also as a header for each read access
2324 where they are discarded by ftdi_read_data(). The chip generates
2325 the two stripped status bytes in the absence of data every 40 ms.
2327 Layout of the first byte:
2328 - B0..B3 - must be 0
2329 - B4 Clear to send (CTS)
2332 - B5 Data set ready (DTS)
2335 - B6 Ring indicator (RI)
2338 - B7 Receive line signal detect (RLSD)
2342 Layout of the second byte:
2343 - B0 Data ready (DR)
2344 - B1 Overrun error (OE)
2345 - B2 Parity error (PE)
2346 - B3 Framing error (FE)
2347 - B4 Break interrupt (BI)
2348 - B5 Transmitter holding register (THRE)
2349 - B6 Transmitter empty (TEMT)
2350 - B7 Error in RCVR FIFO
2352 \param ftdi pointer to ftdi_context
2353 \param status Pointer to store status information in. Must be two bytes.
2356 \retval -1: unable to retrieve status information
2357 \retval -2: USB device unavailable
2359 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2363 if (ftdi == NULL || ftdi->usb_dev == NULL)
2364 ftdi_error_return(-2, "USB device unavailable");
2366 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)
2367 ftdi_error_return(-1, "getting modem status failed");
2369 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2375 Set flowcontrol for ftdi chip
2377 Note: Do not use this function to enable XON/XOFF mode, use ftdi_setflowctrl_xonxoff() instead.
2379 \param ftdi pointer to ftdi_context
2380 \param flowctrl flow control to use. should be
2381 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS
2384 \retval -1: set flow control failed
2385 \retval -2: USB device unavailable
2387 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2389 if (ftdi == NULL || ftdi->usb_dev == NULL)
2390 ftdi_error_return(-2, "USB device unavailable");
2392 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2393 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2394 NULL, 0, ftdi->usb_write_timeout) < 0)
2395 ftdi_error_return(-1, "set flow control failed");
2401 Set XON/XOFF flowcontrol for ftdi chip
2403 \param ftdi pointer to ftdi_context
2404 \param xon character code used to resume transmission
2405 \param xoff character code used to pause transmission
2408 \retval -1: set flow control failed
2409 \retval -2: USB device unavailable
2411 int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
2413 if (ftdi == NULL || ftdi->usb_dev == NULL)
2414 ftdi_error_return(-2, "USB device unavailable");
2416 uint16_t xonxoff = xon | (xoff << 8);
2417 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2418 SIO_SET_FLOW_CTRL_REQUEST, xonxoff, (SIO_XON_XOFF_HS | ftdi->index),
2419 NULL, 0, ftdi->usb_write_timeout) < 0)
2420 ftdi_error_return(-1, "set flow control failed");
2428 \param ftdi pointer to ftdi_context
2429 \param state state to set line to (1 or 0)
2432 \retval -1: set dtr failed
2433 \retval -2: USB device unavailable
2435 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2437 unsigned short usb_val;
2439 if (ftdi == NULL || ftdi->usb_dev == NULL)
2440 ftdi_error_return(-2, "USB device unavailable");
2443 usb_val = SIO_SET_DTR_HIGH;
2445 usb_val = SIO_SET_DTR_LOW;
2447 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2448 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2449 NULL, 0, ftdi->usb_write_timeout) < 0)
2450 ftdi_error_return(-1, "set dtr failed");
2458 \param ftdi pointer to ftdi_context
2459 \param state state to set line to (1 or 0)
2462 \retval -1: set rts failed
2463 \retval -2: USB device unavailable
2465 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2467 unsigned short usb_val;
2469 if (ftdi == NULL || ftdi->usb_dev == NULL)
2470 ftdi_error_return(-2, "USB device unavailable");
2473 usb_val = SIO_SET_RTS_HIGH;
2475 usb_val = SIO_SET_RTS_LOW;
2477 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2478 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2479 NULL, 0, ftdi->usb_write_timeout) < 0)
2480 ftdi_error_return(-1, "set of rts failed");
2486 Set dtr and rts line in one pass
2488 \param ftdi pointer to ftdi_context
2489 \param dtr DTR state to set line to (1 or 0)
2490 \param rts RTS state to set line to (1 or 0)
2493 \retval -1: set dtr/rts failed
2494 \retval -2: USB device unavailable
2496 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2498 unsigned short usb_val;
2500 if (ftdi == NULL || ftdi->usb_dev == NULL)
2501 ftdi_error_return(-2, "USB device unavailable");
2504 usb_val = SIO_SET_DTR_HIGH;
2506 usb_val = SIO_SET_DTR_LOW;
2509 usb_val |= SIO_SET_RTS_HIGH;
2511 usb_val |= SIO_SET_RTS_LOW;
2513 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2514 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2515 NULL, 0, ftdi->usb_write_timeout) < 0)
2516 ftdi_error_return(-1, "set of rts/dtr failed");
2522 Set the special event character
2524 \param ftdi pointer to ftdi_context
2525 \param eventch Event character
2526 \param enable 0 to disable the event character, non-zero otherwise
2529 \retval -1: unable to set event character
2530 \retval -2: USB device unavailable
2532 int ftdi_set_event_char(struct ftdi_context *ftdi,
2533 unsigned char eventch, unsigned char enable)
2535 unsigned short usb_val;
2537 if (ftdi == NULL || ftdi->usb_dev == NULL)
2538 ftdi_error_return(-2, "USB device unavailable");
2544 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)
2545 ftdi_error_return(-1, "setting event character failed");
2553 \param ftdi pointer to ftdi_context
2554 \param errorch Error character
2555 \param enable 0 to disable the error character, non-zero otherwise
2558 \retval -1: unable to set error character
2559 \retval -2: USB device unavailable
2561 int ftdi_set_error_char(struct ftdi_context *ftdi,
2562 unsigned char errorch, unsigned char enable)
2564 unsigned short usb_val;
2566 if (ftdi == NULL || ftdi->usb_dev == NULL)
2567 ftdi_error_return(-2, "USB device unavailable");
2573 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)
2574 ftdi_error_return(-1, "setting error character failed");
2580 Init eeprom with default values for the connected device
2581 \param ftdi pointer to ftdi_context
2582 \param manufacturer String to use as Manufacturer
2583 \param product String to use as Product description
2584 \param serial String to use as Serial number description
2587 \retval -1: No struct ftdi_context
2588 \retval -2: No struct ftdi_eeprom
2589 \retval -3: No connected device or device not yet opened
2591 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, const char * manufacturer,
2592 const char * product, const char * serial)
2594 struct ftdi_eeprom *eeprom;
2597 ftdi_error_return(-1, "No struct ftdi_context");
2599 if (ftdi->eeprom == NULL)
2600 ftdi_error_return(-2,"No struct ftdi_eeprom");
2602 eeprom = ftdi->eeprom;
2603 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2605 if (ftdi->usb_dev == NULL)
2606 ftdi_error_return(-3, "No connected device or device not yet opened");
2608 eeprom->vendor_id = 0x0403;
2609 eeprom->use_serial = (serial != NULL);
2610 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2611 (ftdi->type == TYPE_R))
2612 eeprom->product_id = 0x6001;
2613 else if (ftdi->type == TYPE_4232H)
2614 eeprom->product_id = 0x6011;
2615 else if (ftdi->type == TYPE_232H)
2616 eeprom->product_id = 0x6014;
2617 else if (ftdi->type == TYPE_230X)
2618 eeprom->product_id = 0x6015;
2620 eeprom->product_id = 0x6010;
2622 if (ftdi->type == TYPE_AM)
2623 eeprom->usb_version = 0x0101;
2625 eeprom->usb_version = 0x0200;
2626 eeprom->max_power = 100;
2628 if (eeprom->manufacturer)
2629 free (eeprom->manufacturer);
2630 eeprom->manufacturer = NULL;
2633 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2634 if (eeprom->manufacturer)
2635 strcpy(eeprom->manufacturer, manufacturer);
2638 if (eeprom->product)
2639 free (eeprom->product);
2640 eeprom->product = NULL;
2643 eeprom->product = (char *)malloc(strlen(product)+1);
2644 if (eeprom->product)
2645 strcpy(eeprom->product, product);
2649 const char* default_product;
2652 case TYPE_AM: default_product = "AM"; break;
2653 case TYPE_BM: default_product = "BM"; break;
2654 case TYPE_2232C: default_product = "Dual RS232"; break;
2655 case TYPE_R: default_product = "FT232R USB UART"; break;
2656 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2657 case TYPE_4232H: default_product = "FT4232H"; break;
2658 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2659 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2661 ftdi_error_return(-3, "Unknown chip type");
2663 eeprom->product = (char *)malloc(strlen(default_product) +1);
2664 if (eeprom->product)
2665 strcpy(eeprom->product, default_product);
2669 free (eeprom->serial);
2670 eeprom->serial = NULL;
2673 eeprom->serial = (char *)malloc(strlen(serial)+1);
2675 strcpy(eeprom->serial, serial);
2678 if (ftdi->type == TYPE_R)
2680 eeprom->max_power = 90;
2681 eeprom->size = 0x80;
2682 eeprom->cbus_function[0] = CBUS_TXLED;
2683 eeprom->cbus_function[1] = CBUS_RXLED;
2684 eeprom->cbus_function[2] = CBUS_TXDEN;
2685 eeprom->cbus_function[3] = CBUS_PWREN;
2686 eeprom->cbus_function[4] = CBUS_SLEEP;
2688 else if (ftdi->type == TYPE_230X)
2690 eeprom->max_power = 90;
2691 eeprom->size = 0x100;
2692 eeprom->cbus_function[0] = CBUSX_TXDEN;
2693 eeprom->cbus_function[1] = CBUSX_RXLED;
2694 eeprom->cbus_function[2] = CBUSX_TXLED;
2695 eeprom->cbus_function[3] = CBUSX_SLEEP;
2699 if(ftdi->type == TYPE_232H)
2702 for (i=0; i<10; i++)
2703 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2710 eeprom->release_number = 0x0200;
2713 eeprom->release_number = 0x0400;
2716 eeprom->release_number = 0x0500;
2719 eeprom->release_number = 0x0600;
2722 eeprom->release_number = 0x0700;
2725 eeprom->release_number = 0x0800;
2728 eeprom->release_number = 0x0900;
2731 eeprom->release_number = 0x1000;
2734 eeprom->release_number = 0x00;
2739 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, const char * manufacturer,
2740 const char * product, const char * serial)
2742 struct ftdi_eeprom *eeprom;
2745 ftdi_error_return(-1, "No struct ftdi_context");
2747 if (ftdi->eeprom == NULL)
2748 ftdi_error_return(-2,"No struct ftdi_eeprom");
2750 eeprom = ftdi->eeprom;
2752 if (ftdi->usb_dev == NULL)
2753 ftdi_error_return(-3, "No connected device or device not yet opened");
2757 if (eeprom->manufacturer)
2758 free (eeprom->manufacturer);
2759 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2760 if (eeprom->manufacturer)
2761 strcpy(eeprom->manufacturer, manufacturer);
2766 if (eeprom->product)
2767 free (eeprom->product);
2768 eeprom->product = (char *)malloc(strlen(product)+1);
2769 if (eeprom->product)
2770 strcpy(eeprom->product, product);
2776 free (eeprom->serial);
2777 eeprom->serial = (char *)malloc(strlen(serial)+1);
2780 strcpy(eeprom->serial, serial);
2781 eeprom->use_serial = 1;
2788 Return device ID strings from the eeprom. Device needs to be connected.
2790 The parameters manufacturer, description and serial may be NULL
2791 or pointer to buffers to store the fetched strings.
2793 \param ftdi pointer to ftdi_context
2794 \param manufacturer Store manufacturer string here if not NULL
2795 \param mnf_len Buffer size of manufacturer string
2796 \param product Store product description string here if not NULL
2797 \param prod_len Buffer size of product description string
2798 \param serial Store serial string here if not NULL
2799 \param serial_len Buffer size of serial string
2802 \retval -1: ftdi context invalid
2803 \retval -2: ftdi eeprom buffer invalid
2805 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
2806 char *manufacturer, int mnf_len,
2807 char *product, int prod_len,
2808 char *serial, int serial_len)
2810 struct ftdi_eeprom *eeprom;
2813 ftdi_error_return(-1, "No struct ftdi_context");
2814 if (ftdi->eeprom == NULL)
2815 ftdi_error_return(-2, "No struct ftdi_eeprom");
2817 eeprom = ftdi->eeprom;
2821 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
2823 manufacturer[mnf_len - 1] = '\0';
2828 strncpy(product, eeprom->product, prod_len);
2830 product[prod_len - 1] = '\0';
2835 strncpy(serial, eeprom->serial, serial_len);
2837 serial[serial_len - 1] = '\0';
2843 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2844 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2849 int mode_low, mode_high;
2850 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2851 mode_low = CBUSH_TRISTATE;
2853 mode_low = eeprom->cbus_function[2*i];
2854 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2855 mode_high = CBUSH_TRISTATE;
2857 mode_high = eeprom->cbus_function[2*i+1];
2859 output[0x18+i] = (mode_high <<4) | mode_low;
2862 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2865 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2874 case CHANNEL_IS_UART: return 0;
2875 case CHANNEL_IS_FIFO: return 0x01;
2876 case CHANNEL_IS_OPTO: return 0x02;
2877 case CHANNEL_IS_CPU : return 0x04;
2885 case CHANNEL_IS_UART : return 0;
2886 case CHANNEL_IS_FIFO : return 0x01;
2887 case CHANNEL_IS_OPTO : return 0x02;
2888 case CHANNEL_IS_CPU : return 0x04;
2889 case CHANNEL_IS_FT1284 : return 0x08;
2897 case CHANNEL_IS_UART : return 0;
2898 case CHANNEL_IS_FIFO : return 0x01;
2902 case TYPE_230X: /* FT230X is only UART */
2909 Build binary buffer from ftdi_eeprom structure.
2910 Output is suitable for ftdi_write_eeprom().
2912 \param ftdi pointer to ftdi_context
2914 \retval >=0: size of eeprom user area in bytes
2915 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2916 \retval -2: Invalid eeprom or ftdi pointer
2917 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2918 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2919 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2920 \retval -6: No connected EEPROM or EEPROM Type unknown
2922 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2924 unsigned char i, j, eeprom_size_mask;
2925 unsigned short checksum, value;
2926 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2927 int user_area_size, free_start, free_end;
2928 struct ftdi_eeprom *eeprom;
2929 unsigned char * output;
2932 ftdi_error_return(-2,"No context");
2933 if (ftdi->eeprom == NULL)
2934 ftdi_error_return(-2,"No eeprom structure");
2936 eeprom= ftdi->eeprom;
2937 output = eeprom->buf;
2939 if (eeprom->chip == -1)
2940 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2942 if (eeprom->size == -1)
2944 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2945 eeprom->size = 0x100;
2947 eeprom->size = 0x80;
2950 if (eeprom->manufacturer != NULL)
2951 manufacturer_size = strlen(eeprom->manufacturer);
2952 if (eeprom->product != NULL)
2953 product_size = strlen(eeprom->product);
2954 if (eeprom->serial != NULL)
2955 serial_size = strlen(eeprom->serial);
2957 // eeprom size check
2963 user_area_size = 96; // base size for strings (total of 48 characters)
2966 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2969 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2971 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2973 user_area_size = 86;
2976 user_area_size = 80;
2982 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2984 if (user_area_size < 0)
2985 ftdi_error_return(-1,"eeprom size exceeded");
2988 if (ftdi->type == TYPE_230X)
2990 /* FT230X have a reserved section in the middle of the MTP,
2991 which cannot be written to, but must be included in the checksum */
2992 memset(ftdi->eeprom->buf, 0, 0x80);
2993 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2997 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
3000 // Bytes and Bits set for all Types
3002 // Addr 02: Vendor ID
3003 output[0x02] = eeprom->vendor_id;
3004 output[0x03] = eeprom->vendor_id >> 8;
3006 // Addr 04: Product ID
3007 output[0x04] = eeprom->product_id;
3008 output[0x05] = eeprom->product_id >> 8;
3010 // Addr 06: Device release number (0400h for BM features)
3011 output[0x06] = eeprom->release_number;
3012 output[0x07] = eeprom->release_number >> 8;
3014 // Addr 08: Config descriptor
3016 // Bit 6: 1 if this device is self powered, 0 if bus powered
3017 // Bit 5: 1 if this device uses remote wakeup
3018 // Bit 4-0: reserved - 0
3020 if (eeprom->self_powered)
3022 if (eeprom->remote_wakeup)
3026 // Addr 09: Max power consumption: max power = value * 2 mA
3027 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
3029 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
3031 // Addr 0A: Chip configuration
3032 // Bit 7: 0 - reserved
3033 // Bit 6: 0 - reserved
3034 // Bit 5: 0 - reserved
3035 // Bit 4: 1 - Change USB version
3036 // Bit 3: 1 - Use the serial number string
3037 // Bit 2: 1 - Enable suspend pull downs for lower power
3038 // Bit 1: 1 - Out EndPoint is Isochronous
3039 // Bit 0: 1 - In EndPoint is Isochronous
3042 if (eeprom->in_is_isochronous)
3044 if (eeprom->out_is_isochronous)
3050 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
3051 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
3052 // 0xa0 (TYPE_232H, TYPE_230X)
3075 /* Wrap around 0x80 for 128 byte EEPROMS (Internal and 93x46) */
3076 eeprom_size_mask = eeprom->size -1;
3077 free_end = i & eeprom_size_mask;
3079 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3080 // Addr 0F: Length of manufacturer string
3081 // Output manufacturer
3082 output[0x0E] = i; // calculate offset
3083 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
3084 output[i & eeprom_size_mask] = 0x03, i++; // type: string
3085 for (j = 0; j < manufacturer_size; j++)
3087 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
3088 output[i & eeprom_size_mask] = 0x00, i++;
3090 output[0x0F] = manufacturer_size*2 + 2;
3092 // Addr 10: Offset of the product string + 0x80, calculated later
3093 // Addr 11: Length of product string
3094 output[0x10] = i | 0x80; // calculate offset
3095 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
3096 output[i & eeprom_size_mask] = 0x03, i++;
3097 for (j = 0; j < product_size; j++)
3099 output[i & eeprom_size_mask] = eeprom->product[j], i++;
3100 output[i & eeprom_size_mask] = 0x00, i++;
3102 output[0x11] = product_size*2 + 2;
3104 if (eeprom->use_serial) {
3105 // Addr 12: Offset of the serial string + 0x80, calculated later
3106 // Addr 13: Length of serial string
3107 output[0x12] = i | 0x80; // calculate offset
3108 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
3109 output[i & eeprom_size_mask] = 0x03, i++;
3110 for (j = 0; j < serial_size; j++)
3112 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
3113 output[i & eeprom_size_mask] = 0x00, i++;
3115 output[0x13] = serial_size*2 + 2;
3118 // Legacy port name and PnP fields for FT2232 and newer chips
3119 // It doesn't appear when written with FT_Prog for FT4232H chip.
3120 if (ftdi->type > TYPE_BM && ftdi->type != TYPE_4232H)
3122 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
3124 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
3126 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
3128 output[i & eeprom_size_mask] = 0x00;
3132 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
3134 if (eeprom->use_serial)
3135 output[0x0A] |= USE_SERIAL_NUM;
3137 output[0x0A] &= ~USE_SERIAL_NUM;
3140 /* Bytes and Bits specific to (some) types
3141 Write linear, as this allows easier fixing */
3147 output[0x0C] = eeprom->usb_version & 0xff;
3148 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3149 if (eeprom->use_usb_version)
3150 output[0x0A] |= USE_USB_VERSION_BIT;
3152 output[0x0A] &= ~USE_USB_VERSION_BIT;
3157 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
3158 if (eeprom->channel_a_driver)
3159 output[0x00] |= DRIVER_VCP;
3161 output[0x00] &= ~DRIVER_VCP;
3163 if (eeprom->high_current_a)
3164 output[0x00] |= HIGH_CURRENT_DRIVE;
3166 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3168 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
3169 if (eeprom->channel_b_driver)
3170 output[0x01] |= DRIVER_VCP;
3172 output[0x01] &= ~DRIVER_VCP;
3174 if (eeprom->high_current_b)
3175 output[0x01] |= HIGH_CURRENT_DRIVE;
3177 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3179 if (eeprom->in_is_isochronous)
3180 output[0x0A] |= 0x1;
3182 output[0x0A] &= ~0x1;
3183 if (eeprom->out_is_isochronous)
3184 output[0x0A] |= 0x2;
3186 output[0x0A] &= ~0x2;
3187 if (eeprom->suspend_pull_downs)
3188 output[0x0A] |= 0x4;
3190 output[0x0A] &= ~0x4;
3191 if (eeprom->use_usb_version)
3192 output[0x0A] |= USE_USB_VERSION_BIT;
3194 output[0x0A] &= ~USE_USB_VERSION_BIT;
3196 output[0x0C] = eeprom->usb_version & 0xff;
3197 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3198 output[0x14] = eeprom->chip;
3201 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
3202 if (eeprom->high_current)
3203 output[0x00] |= HIGH_CURRENT_DRIVE_R;
3205 /* Field is inverted for TYPE_R: Bit 00.3 set to 1 is D2XX, VCP is 0 */
3206 if (eeprom->channel_a_driver)
3207 output[0x00] &= ~DRIVER_VCP;
3209 output[0x00] |= DRIVER_VCP;
3211 if (eeprom->external_oscillator)
3212 output[0x00] |= 0x02;
3213 output[0x01] = 0x40; /* Hard coded Endpoint Size */
3215 if (eeprom->suspend_pull_downs)
3216 output[0x0A] |= 0x4;
3218 output[0x0A] &= ~0x4;
3219 output[0x0B] = eeprom->invert;
3220 output[0x0C] = eeprom->usb_version & 0xff;
3221 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3223 if (eeprom->cbus_function[0] > CBUS_BB_RD)
3224 output[0x14] = CBUS_TXLED;
3226 output[0x14] = eeprom->cbus_function[0];
3228 if (eeprom->cbus_function[1] > CBUS_BB_RD)
3229 output[0x14] |= CBUS_RXLED<<4;
3231 output[0x14] |= eeprom->cbus_function[1]<<4;
3233 if (eeprom->cbus_function[2] > CBUS_BB_RD)
3234 output[0x15] = CBUS_TXDEN;
3236 output[0x15] = eeprom->cbus_function[2];
3238 if (eeprom->cbus_function[3] > CBUS_BB_RD)
3239 output[0x15] |= CBUS_PWREN<<4;
3241 output[0x15] |= eeprom->cbus_function[3]<<4;
3243 if (eeprom->cbus_function[4] > CBUS_CLK6)
3244 output[0x16] = CBUS_SLEEP;
3246 output[0x16] = eeprom->cbus_function[4];
3249 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
3250 if (eeprom->channel_a_driver)
3251 output[0x00] |= DRIVER_VCP;
3253 output[0x00] &= ~DRIVER_VCP;
3255 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
3256 if (eeprom->channel_b_driver)
3257 output[0x01] |= DRIVER_VCP;
3259 output[0x01] &= ~DRIVER_VCP;
3261 if (eeprom->suspend_dbus7)
3262 output[0x01] |= SUSPEND_DBUS7_BIT;
3264 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3266 if (eeprom->suspend_pull_downs)
3267 output[0x0A] |= 0x4;
3269 output[0x0A] &= ~0x4;
3271 if (eeprom->group0_drive > DRIVE_16MA)
3272 output[0x0c] |= DRIVE_16MA;
3274 output[0x0c] |= eeprom->group0_drive;
3275 if (eeprom->group0_schmitt)
3276 output[0x0c] |= IS_SCHMITT;
3277 if (eeprom->group0_slew)
3278 output[0x0c] |= SLOW_SLEW;
3280 if (eeprom->group1_drive > DRIVE_16MA)
3281 output[0x0c] |= DRIVE_16MA<<4;
3283 output[0x0c] |= eeprom->group1_drive<<4;
3284 if (eeprom->group1_schmitt)
3285 output[0x0c] |= IS_SCHMITT<<4;
3286 if (eeprom->group1_slew)
3287 output[0x0c] |= SLOW_SLEW<<4;
3289 if (eeprom->group2_drive > DRIVE_16MA)
3290 output[0x0d] |= DRIVE_16MA;
3292 output[0x0d] |= eeprom->group2_drive;
3293 if (eeprom->group2_schmitt)
3294 output[0x0d] |= IS_SCHMITT;
3295 if (eeprom->group2_slew)
3296 output[0x0d] |= SLOW_SLEW;
3298 if (eeprom->group3_drive > DRIVE_16MA)
3299 output[0x0d] |= DRIVE_16MA<<4;
3301 output[0x0d] |= eeprom->group3_drive<<4;
3302 if (eeprom->group3_schmitt)
3303 output[0x0d] |= IS_SCHMITT<<4;
3304 if (eeprom->group3_slew)
3305 output[0x0d] |= SLOW_SLEW<<4;
3307 output[0x18] = eeprom->chip;
3311 if (eeprom->channel_a_driver)
3312 output[0x00] |= DRIVER_VCP;
3314 output[0x00] &= ~DRIVER_VCP;
3315 if (eeprom->channel_b_driver)
3316 output[0x01] |= DRIVER_VCP;
3318 output[0x01] &= ~DRIVER_VCP;
3319 if (eeprom->channel_c_driver)
3320 output[0x00] |= (DRIVER_VCP << 4);
3322 output[0x00] &= ~(DRIVER_VCP << 4);
3323 if (eeprom->channel_d_driver)
3324 output[0x01] |= (DRIVER_VCP << 4);
3326 output[0x01] &= ~(DRIVER_VCP << 4);
3328 if (eeprom->suspend_pull_downs)
3329 output[0x0a] |= 0x4;
3331 output[0x0a] &= ~0x4;
3333 if (eeprom->channel_a_rs485enable)
3334 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3336 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3337 if (eeprom->channel_b_rs485enable)
3338 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3340 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3341 if (eeprom->channel_c_rs485enable)
3342 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3344 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3345 if (eeprom->channel_d_rs485enable)
3346 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3348 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3350 if (eeprom->group0_drive > DRIVE_16MA)
3351 output[0x0c] |= DRIVE_16MA;
3353 output[0x0c] |= eeprom->group0_drive;
3354 if (eeprom->group0_schmitt)
3355 output[0x0c] |= IS_SCHMITT;
3356 if (eeprom->group0_slew)
3357 output[0x0c] |= SLOW_SLEW;
3359 if (eeprom->group1_drive > DRIVE_16MA)
3360 output[0x0c] |= DRIVE_16MA<<4;
3362 output[0x0c] |= eeprom->group1_drive<<4;
3363 if (eeprom->group1_schmitt)
3364 output[0x0c] |= IS_SCHMITT<<4;
3365 if (eeprom->group1_slew)
3366 output[0x0c] |= SLOW_SLEW<<4;
3368 if (eeprom->group2_drive > DRIVE_16MA)
3369 output[0x0d] |= DRIVE_16MA;
3371 output[0x0d] |= eeprom->group2_drive;
3372 if (eeprom->group2_schmitt)
3373 output[0x0d] |= IS_SCHMITT;
3374 if (eeprom->group2_slew)
3375 output[0x0d] |= SLOW_SLEW;
3377 if (eeprom->group3_drive > DRIVE_16MA)
3378 output[0x0d] |= DRIVE_16MA<<4;
3380 output[0x0d] |= eeprom->group3_drive<<4;
3381 if (eeprom->group3_schmitt)
3382 output[0x0d] |= IS_SCHMITT<<4;
3383 if (eeprom->group3_slew)
3384 output[0x0d] |= SLOW_SLEW<<4;
3386 output[0x18] = eeprom->chip;
3390 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3391 if (eeprom->channel_a_driver)
3392 output[0x00] |= DRIVER_VCPH;
3394 output[0x00] &= ~DRIVER_VCPH;
3396 if (eeprom->powersave)
3397 output[0x01] |= POWER_SAVE_DISABLE_H;
3399 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3401 if (eeprom->suspend_pull_downs)
3402 output[0x0a] |= 0x4;
3404 output[0x0a] &= ~0x4;
3406 if (eeprom->clock_polarity)
3407 output[0x01] |= FT1284_CLK_IDLE_STATE;
3409 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3410 if (eeprom->data_order)
3411 output[0x01] |= FT1284_DATA_LSB;
3413 output[0x01] &= ~FT1284_DATA_LSB;
3414 if (eeprom->flow_control)
3415 output[0x01] |= FT1284_FLOW_CONTROL;
3417 output[0x01] &= ~FT1284_FLOW_CONTROL;
3419 if (eeprom->group0_drive > DRIVE_16MA)
3420 output[0x0c] |= DRIVE_16MA;
3422 output[0x0c] |= eeprom->group0_drive;
3423 if (eeprom->group0_schmitt)
3424 output[0x0c] |= IS_SCHMITT;
3425 if (eeprom->group0_slew)
3426 output[0x0c] |= SLOW_SLEW;
3428 if (eeprom->group1_drive > DRIVE_16MA)
3429 output[0x0d] |= DRIVE_16MA;
3431 output[0x0d] |= eeprom->group1_drive;
3432 if (eeprom->group1_schmitt)
3433 output[0x0d] |= IS_SCHMITT;
3434 if (eeprom->group1_slew)
3435 output[0x0d] |= SLOW_SLEW;
3437 set_ft232h_cbus(eeprom, output);
3439 output[0x1e] = eeprom->chip;
3440 /* FIXME: Build FT232H specific EEPROM settings */
3443 output[0x00] = 0x80; /* Actually, leave the default value */
3444 /*FIXME: Make DBUS & CBUS Control configurable*/
3445 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4mA like factory default */
3446 for (j = 0; j <= 6; j++)
3448 output[0x1a + j] = eeprom->cbus_function[j];
3450 output[0x0b] = eeprom->invert;
3454 /* First address without use */
3479 /* Arbitrary user data */
3480 if (eeprom->user_data && eeprom->user_data_size >= 0)
3482 if (eeprom->user_data_addr < free_start)
3483 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3484 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3485 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3486 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3487 ftdi_error_return(-1,"eeprom size exceeded");
3488 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3491 // calculate checksum
3494 for (i = 0; i < eeprom->size/2-1; i++)
3496 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3498 /* FT230X has a user section in the MTP which is not part of the checksum */
3501 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3503 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3504 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3508 output[i * 2] = data;
3509 output[(i * 2) + 1] = data >> 8;
3512 value = output[i*2];
3513 value += output[(i*2)+1] << 8;
3515 checksum = value^checksum;
3516 checksum = (checksum << 1) | (checksum >> 15);
3519 output[eeprom->size-2] = checksum;
3520 output[eeprom->size-1] = checksum >> 8;
3522 eeprom->initialized_for_connected_device = 1;
3523 return user_area_size;
3525 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3528 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3530 static unsigned char bit2type(unsigned char bits)
3534 case 0: return CHANNEL_IS_UART;
3535 case 1: return CHANNEL_IS_FIFO;
3536 case 2: return CHANNEL_IS_OPTO;
3537 case 4: return CHANNEL_IS_CPU;
3538 case 8: return CHANNEL_IS_FT1284;
3540 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3545 /* Decode 230X / 232R type chips invert bits
3546 * Prints directly to stdout.
3548 static void print_inverted_bits(int invert)
3550 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3553 fprintf(stdout,"Inverted bits:");
3555 if ((invert & (1<<i)) == (1<<i))
3556 fprintf(stdout," %s",r_bits[i]);
3558 fprintf(stdout,"\n");
3561 Decode binary EEPROM image into an ftdi_eeprom structure.
3563 For FT-X devices use AN_201 FT-X MTP memory Configuration to decode.
3565 \param ftdi pointer to ftdi_context
3566 \param verbose Decode EEPROM on stdout
3569 \retval -1: something went wrong
3571 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3572 FIXME: Strings are malloc'ed here and should be freed somewhere
3574 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3577 unsigned short checksum, eeprom_checksum, value;
3578 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3580 struct ftdi_eeprom *eeprom;
3581 unsigned char *buf = NULL;
3584 ftdi_error_return(-1,"No context");
3585 if (ftdi->eeprom == NULL)
3586 ftdi_error_return(-1,"No eeprom structure");
3588 eeprom = ftdi->eeprom;
3589 eeprom_size = eeprom->size;
3590 buf = ftdi->eeprom->buf;
3592 // Addr 02: Vendor ID
3593 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3595 // Addr 04: Product ID
3596 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3598 // Addr 06: Device release number
3599 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3601 // Addr 08: Config descriptor
3603 // Bit 6: 1 if this device is self powered, 0 if bus powered
3604 // Bit 5: 1 if this device uses remote wakeup
3605 eeprom->self_powered = !!(buf[0x08] & 0x40);
3606 eeprom->remote_wakeup = !!(buf[0x08] & 0x20);
3608 // Addr 09: Max power consumption: max power = value * 2 mA
3609 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3611 // Addr 0A: Chip configuration
3612 // Bit 7: 0 - reserved
3613 // Bit 6: 0 - reserved
3614 // Bit 5: 0 - reserved
3615 // Bit 4: 1 - Change USB version on BM and 2232C
3616 // Bit 3: 1 - Use the serial number string
3617 // Bit 2: 1 - Enable suspend pull downs for lower power
3618 // Bit 1: 1 - Out EndPoint is Isochronous
3619 // Bit 0: 1 - In EndPoint is Isochronous
3621 eeprom->in_is_isochronous = !!(buf[0x0A]&0x01);
3622 eeprom->out_is_isochronous = !!(buf[0x0A]&0x02);
3623 eeprom->suspend_pull_downs = !!(buf[0x0A]&0x04);
3624 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3625 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3627 // Addr 0C: USB version low byte when 0x0A
3628 // Addr 0D: USB version high byte when 0x0A
3629 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3631 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3632 // Addr 0F: Length of manufacturer string
3633 manufacturer_size = buf[0x0F]/2;
3634 if (eeprom->manufacturer)
3635 free(eeprom->manufacturer);
3636 if (manufacturer_size > 0)
3638 eeprom->manufacturer = (char *)malloc(manufacturer_size);
3639 if (eeprom->manufacturer)
3641 // Decode manufacturer
3642 i = buf[0x0E] & (eeprom_size -1); // offset
3643 for (j=0; j<manufacturer_size-1; j++)
3645 eeprom->manufacturer[j] = buf[2*j+i+2];
3647 eeprom->manufacturer[j] = '\0';
3650 else eeprom->manufacturer = NULL;
3652 // Addr 10: Offset of the product string + 0x80, calculated later
3653 // Addr 11: Length of product string
3654 if (eeprom->product)
3655 free(eeprom->product);
3656 product_size = buf[0x11]/2;
3657 if (product_size > 0)
3659 eeprom->product = (char *)malloc(product_size);
3660 if (eeprom->product)
3662 // Decode product name
3663 i = buf[0x10] & (eeprom_size -1); // offset
3664 for (j=0; j<product_size-1; j++)
3666 eeprom->product[j] = buf[2*j+i+2];
3668 eeprom->product[j] = '\0';
3671 else eeprom->product = NULL;
3673 // Addr 12: Offset of the serial string + 0x80, calculated later
3674 // Addr 13: Length of serial string
3676 free(eeprom->serial);
3677 serial_size = buf[0x13]/2;
3678 if (serial_size > 0)
3680 eeprom->serial = (char *)malloc(serial_size);
3684 i = buf[0x12] & (eeprom_size -1); // offset
3685 for (j=0; j<serial_size-1; j++)
3687 eeprom->serial[j] = buf[2*j+i+2];
3689 eeprom->serial[j] = '\0';
3692 else eeprom->serial = NULL;
3697 for (i = 0; i < eeprom_size/2-1; i++)
3699 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3701 /* FT230X has a user section in the MTP which is not part of the checksum */
3705 value += buf[(i*2)+1] << 8;
3707 checksum = value^checksum;
3708 checksum = (checksum << 1) | (checksum >> 15);
3711 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3713 if (eeprom_checksum != checksum)
3715 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3716 ftdi_error_return(-1,"EEPROM checksum error");
3719 eeprom->channel_a_type = 0;
3720 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3724 else if (ftdi->type == TYPE_2232C)
3726 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3727 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCP);
3728 eeprom->high_current_a = !!(buf[0x00] & HIGH_CURRENT_DRIVE);
3729 eeprom->channel_b_type = buf[0x01] & 0x7;
3730 eeprom->channel_b_driver = !!(buf[0x01] & DRIVER_VCP);
3731 eeprom->high_current_b = !!(buf[0x01] & HIGH_CURRENT_DRIVE);
3732 eeprom->chip = buf[0x14];
3734 else if (ftdi->type == TYPE_R)
3736 /* TYPE_R flags D2XX, not VCP as all others */
3737 eeprom->channel_a_driver = !(buf[0x00] & DRIVER_VCP); /* note: inverted flag, use a single NOT */
3738 eeprom->high_current = !!(buf[0x00] & HIGH_CURRENT_DRIVE_R);
3739 eeprom->external_oscillator = !!(buf[0x00] & 0x02);
3740 if ( (buf[0x01]&0x40) != 0x40)
3742 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3743 " If this happened with the\n"
3744 " EEPROM programmed by FTDI tools, please report "
3745 "to libftdi@developer.intra2net.com\n");
3747 eeprom->chip = buf[0x16];
3748 // Addr 0B: Invert data lines
3749 // Works only on FT232R, not FT245R, but no way to distinguish
3750 eeprom->invert = buf[0x0B]; /* note: not a bitflag */
3751 // Addr 14: CBUS function: CBUS0, CBUS1
3752 // Addr 15: CBUS function: CBUS2, CBUS3
3753 // Addr 16: CBUS function: CBUS5
3754 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3755 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3756 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3757 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3758 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3760 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3762 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCP);
3763 eeprom->channel_b_driver = !!(buf[0x01] & DRIVER_VCP);
3765 if (ftdi->type == TYPE_2232H)
3767 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3768 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3769 eeprom->suspend_dbus7 = !!(buf[0x01] & SUSPEND_DBUS7_BIT);
3773 eeprom->channel_c_driver = !!((buf[0x00] >> 4) & DRIVER_VCP);
3774 eeprom->channel_d_driver = !!((buf[0x01] >> 4) & DRIVER_VCP);
3775 eeprom->channel_a_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 0));
3776 eeprom->channel_b_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 1));
3777 eeprom->channel_c_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 2));
3778 eeprom->channel_d_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 3));
3781 eeprom->chip = buf[0x18];
3782 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3783 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3784 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3785 eeprom->group1_drive = (buf[0x0c] >> 4) & DRIVE_16MA; /* not a bitflag */
3786 eeprom->group1_schmitt = !!((buf[0x0c] >> 4) & IS_SCHMITT);
3787 eeprom->group1_slew = !!((buf[0x0c] >> 4) & SLOW_SLEW);
3788 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA; /* not a bitflag */
3789 eeprom->group2_schmitt = !!(buf[0x0d] & IS_SCHMITT);
3790 eeprom->group2_slew = !!(buf[0x0d] & SLOW_SLEW);
3791 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA; /* not a bitflag */
3792 eeprom->group3_schmitt = !!((buf[0x0d] >> 4) & IS_SCHMITT);
3793 eeprom->group3_slew = !!((buf[0x0d] >> 4) & SLOW_SLEW);
3795 else if (ftdi->type == TYPE_232H)
3797 eeprom->channel_a_type = buf[0x00] & 0xf;
3798 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCPH);
3799 eeprom->clock_polarity = !!(buf[0x01] & FT1284_CLK_IDLE_STATE);
3800 eeprom->data_order = !!(buf[0x01] & FT1284_DATA_LSB);
3801 eeprom->flow_control = !!(buf[0x01] & FT1284_FLOW_CONTROL);
3802 eeprom->powersave = !!(buf[0x01] & POWER_SAVE_DISABLE_H);
3803 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3804 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3805 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3806 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA; /* not a bitflag */
3807 eeprom->group1_schmitt = !!(buf[0x0d] & IS_SCHMITT);
3808 eeprom->group1_slew = !!(buf[0x0d] & SLOW_SLEW);
3812 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3813 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3815 eeprom->chip = buf[0x1e];
3816 /*FIXME: Decipher more values*/
3818 else if (ftdi->type == TYPE_230X)
3822 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3824 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3825 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3826 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3827 eeprom->group1_drive = (buf[0x0c] >> 4) & DRIVE_16MA; /* not a bitflag */
3828 eeprom->group1_schmitt = !!((buf[0x0c] >> 4) & IS_SCHMITT);
3829 eeprom->group1_slew = !!((buf[0x0c] >> 4) & SLOW_SLEW);
3831 eeprom->invert = buf[0xb]; /* not a bitflag */
3836 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3837 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3838 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3839 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3841 if (eeprom->self_powered)
3842 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3844 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3845 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3846 if (eeprom->manufacturer)
3847 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3848 if (eeprom->product)
3849 fprintf(stdout, "Product: %s\n",eeprom->product);
3851 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3852 fprintf(stdout, "Checksum : %04x\n", checksum);
3853 if (ftdi->type == TYPE_R) {
3854 fprintf(stdout, "Internal EEPROM\n");
3855 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3857 else if (eeprom->chip >= 0x46)
3858 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3859 if (eeprom->suspend_dbus7)
3860 fprintf(stdout, "Suspend on DBUS7\n");
3861 if (eeprom->suspend_pull_downs)
3862 fprintf(stdout, "Pull IO pins low during suspend\n");
3863 if(eeprom->powersave)
3865 if(ftdi->type >= TYPE_232H)
3866 fprintf(stdout,"Enter low power state on ACBUS7\n");
3868 if (eeprom->remote_wakeup)
3869 fprintf(stdout, "Enable Remote Wake Up\n");
3870 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3871 if (ftdi->type >= TYPE_2232C)
3872 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3873 channel_mode[eeprom->channel_a_type],
3874 (eeprom->channel_a_driver)?" VCP":"",
3875 (eeprom->high_current_a)?" High Current IO":"");
3876 if (ftdi->type == TYPE_232H)
3878 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3879 (eeprom->clock_polarity)?"HIGH":"LOW",
3880 (eeprom->data_order)?"LSB":"MSB",
3881 (eeprom->flow_control)?"":"No ");
3883 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_2232C))
3884 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3885 channel_mode[eeprom->channel_b_type],
3886 (eeprom->channel_b_driver)?" VCP":"",
3887 (eeprom->high_current_b)?" High Current IO":"");
3888 if (ftdi->type == TYPE_4232H)
3890 fprintf(stdout,"Channel C has Mode UART%s\n",
3891 (eeprom->channel_c_driver)?" VCP":"");
3892 fprintf(stdout,"Channel D has Mode UART%s\n",
3893 (eeprom->channel_d_driver)?" VCP":"");
3895 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3896 eeprom->use_usb_version)
3897 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3899 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3901 fprintf(stdout,"%s has %d mA drive%s%s\n",
3902 (ftdi->type == TYPE_2232H)?"AL":"A",
3903 (eeprom->group0_drive+1) *4,
3904 (eeprom->group0_schmitt)?" Schmitt Input":"",
3905 (eeprom->group0_slew)?" Slow Slew":"");
3906 fprintf(stdout,"%s has %d mA drive%s%s\n",
3907 (ftdi->type == TYPE_2232H)?"AH":"B",
3908 (eeprom->group1_drive+1) *4,
3909 (eeprom->group1_schmitt)?" Schmitt Input":"",
3910 (eeprom->group1_slew)?" Slow Slew":"");
3911 fprintf(stdout,"%s has %d mA drive%s%s\n",
3912 (ftdi->type == TYPE_2232H)?"BL":"C",
3913 (eeprom->group2_drive+1) *4,
3914 (eeprom->group2_schmitt)?" Schmitt Input":"",
3915 (eeprom->group2_slew)?" Slow Slew":"");
3916 fprintf(stdout,"%s has %d mA drive%s%s\n",
3917 (ftdi->type == TYPE_2232H)?"BH":"D",
3918 (eeprom->group3_drive+1) *4,
3919 (eeprom->group3_schmitt)?" Schmitt Input":"",
3920 (eeprom->group3_slew)?" Slow Slew":"");
3922 else if (ftdi->type == TYPE_232H)
3924 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3925 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3926 "CLK30","CLK15","CLK7_5"
3928 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3929 (eeprom->group0_drive+1) *4,
3930 (eeprom->group0_schmitt)?" Schmitt Input":"",
3931 (eeprom->group0_slew)?" Slow Slew":"");
3932 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3933 (eeprom->group1_drive+1) *4,
3934 (eeprom->group1_schmitt)?" Schmitt Input":"",
3935 (eeprom->group1_slew)?" Slow Slew":"");
3936 for (i=0; i<10; i++)
3938 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3939 fprintf(stdout,"C%d Function: %s\n", i,
3940 cbush_mux[eeprom->cbus_function[i]]);
3943 else if (ftdi->type == TYPE_230X)
3945 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3946 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3947 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3948 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3949 "BBRD#", "TIME_STAMP", "AWAKE#",
3951 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3952 (eeprom->group0_drive+1) *4,
3953 (eeprom->group0_schmitt)?" Schmitt Input":"",
3954 (eeprom->group0_slew)?" Slow Slew":"");
3955 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3956 (eeprom->group1_drive+1) *4,
3957 (eeprom->group1_schmitt)?" Schmitt Input":"",
3958 (eeprom->group1_slew)?" Slow Slew":"");
3961 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3962 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3966 print_inverted_bits(eeprom->invert);
3969 if (ftdi->type == TYPE_R)
3971 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3972 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3973 "IOMODE","BB_WR","BB_RD"
3975 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3978 print_inverted_bits(eeprom->invert);
3982 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3983 fprintf(stdout,"C%d Function: %s\n", i,
3984 cbus_mux[eeprom->cbus_function[i]]);
3988 /* Running MPROG show that C0..3 have fixed function Synchronous
3990 fprintf(stdout,"C%d BB Function: %s\n", i,
3993 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
4002 Get a value from the decoded EEPROM structure
4004 \param ftdi pointer to ftdi_context
4005 \param value_name Enum of the value to query
4006 \param value Pointer to store read value
4009 \retval -1: Value doesn't exist
4011 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
4016 *value = ftdi->eeprom->vendor_id;
4019 *value = ftdi->eeprom->product_id;
4021 case RELEASE_NUMBER:
4022 *value = ftdi->eeprom->release_number;
4025 *value = ftdi->eeprom->self_powered;
4028 *value = ftdi->eeprom->remote_wakeup;
4031 *value = ftdi->eeprom->is_not_pnp;
4034 *value = ftdi->eeprom->suspend_dbus7;
4036 case IN_IS_ISOCHRONOUS:
4037 *value = ftdi->eeprom->in_is_isochronous;
4039 case OUT_IS_ISOCHRONOUS:
4040 *value = ftdi->eeprom->out_is_isochronous;
4042 case SUSPEND_PULL_DOWNS:
4043 *value = ftdi->eeprom->suspend_pull_downs;
4046 *value = ftdi->eeprom->use_serial;
4049 *value = ftdi->eeprom->usb_version;
4051 case USE_USB_VERSION:
4052 *value = ftdi->eeprom->use_usb_version;
4055 *value = ftdi->eeprom->max_power;
4057 case CHANNEL_A_TYPE:
4058 *value = ftdi->eeprom->channel_a_type;
4060 case CHANNEL_B_TYPE:
4061 *value = ftdi->eeprom->channel_b_type;
4063 case CHANNEL_A_DRIVER:
4064 *value = ftdi->eeprom->channel_a_driver;
4066 case CHANNEL_B_DRIVER:
4067 *value = ftdi->eeprom->channel_b_driver;
4069 case CHANNEL_C_DRIVER:
4070 *value = ftdi->eeprom->channel_c_driver;
4072 case CHANNEL_D_DRIVER:
4073 *value = ftdi->eeprom->channel_d_driver;
4075 case CHANNEL_A_RS485:
4076 *value = ftdi->eeprom->channel_a_rs485enable;
4078 case CHANNEL_B_RS485:
4079 *value = ftdi->eeprom->channel_b_rs485enable;
4081 case CHANNEL_C_RS485:
4082 *value = ftdi->eeprom->channel_c_rs485enable;
4084 case CHANNEL_D_RS485:
4085 *value = ftdi->eeprom->channel_d_rs485enable;
4087 case CBUS_FUNCTION_0:
4088 *value = ftdi->eeprom->cbus_function[0];
4090 case CBUS_FUNCTION_1:
4091 *value = ftdi->eeprom->cbus_function[1];
4093 case CBUS_FUNCTION_2:
4094 *value = ftdi->eeprom->cbus_function[2];
4096 case CBUS_FUNCTION_3:
4097 *value = ftdi->eeprom->cbus_function[3];
4099 case CBUS_FUNCTION_4:
4100 *value = ftdi->eeprom->cbus_function[4];
4102 case CBUS_FUNCTION_5:
4103 *value = ftdi->eeprom->cbus_function[5];
4105 case CBUS_FUNCTION_6:
4106 *value = ftdi->eeprom->cbus_function[6];
4108 case CBUS_FUNCTION_7:
4109 *value = ftdi->eeprom->cbus_function[7];
4111 case CBUS_FUNCTION_8:
4112 *value = ftdi->eeprom->cbus_function[8];
4114 case CBUS_FUNCTION_9:
4115 *value = ftdi->eeprom->cbus_function[9];
4118 *value = ftdi->eeprom->high_current;
4120 case HIGH_CURRENT_A:
4121 *value = ftdi->eeprom->high_current_a;
4123 case HIGH_CURRENT_B:
4124 *value = ftdi->eeprom->high_current_b;
4127 *value = ftdi->eeprom->invert;
4130 *value = ftdi->eeprom->group0_drive;
4132 case GROUP0_SCHMITT:
4133 *value = ftdi->eeprom->group0_schmitt;
4136 *value = ftdi->eeprom->group0_slew;
4139 *value = ftdi->eeprom->group1_drive;
4141 case GROUP1_SCHMITT:
4142 *value = ftdi->eeprom->group1_schmitt;
4145 *value = ftdi->eeprom->group1_slew;
4148 *value = ftdi->eeprom->group2_drive;
4150 case GROUP2_SCHMITT:
4151 *value = ftdi->eeprom->group2_schmitt;
4154 *value = ftdi->eeprom->group2_slew;
4157 *value = ftdi->eeprom->group3_drive;
4159 case GROUP3_SCHMITT:
4160 *value = ftdi->eeprom->group3_schmitt;
4163 *value = ftdi->eeprom->group3_slew;
4166 *value = ftdi->eeprom->powersave;
4168 case CLOCK_POLARITY:
4169 *value = ftdi->eeprom->clock_polarity;
4172 *value = ftdi->eeprom->data_order;
4175 *value = ftdi->eeprom->flow_control;
4178 *value = ftdi->eeprom->chip;
4181 *value = ftdi->eeprom->size;
4183 case EXTERNAL_OSCILLATOR:
4184 *value = ftdi->eeprom->external_oscillator;
4186 case USER_DATA_ADDR:
4187 *value = ftdi->eeprom->user_data_addr;
4190 ftdi_error_return(-1, "Request for unknown EEPROM value");
4196 Set a value in the decoded EEPROM Structure
4197 No parameter checking is performed
4199 \param ftdi pointer to ftdi_context
4200 \param value_name Enum of the value to set
4204 \retval -1: Value doesn't exist
4205 \retval -2: Value not user settable
4207 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
4212 ftdi->eeprom->vendor_id = value;
4215 ftdi->eeprom->product_id = value;
4217 case RELEASE_NUMBER:
4218 ftdi->eeprom->release_number = value;
4221 ftdi->eeprom->self_powered = value;
4224 ftdi->eeprom->remote_wakeup = value;
4227 ftdi->eeprom->is_not_pnp = value;
4230 ftdi->eeprom->suspend_dbus7 = value;
4232 case IN_IS_ISOCHRONOUS:
4233 ftdi->eeprom->in_is_isochronous = value;
4235 case OUT_IS_ISOCHRONOUS:
4236 ftdi->eeprom->out_is_isochronous = value;
4238 case SUSPEND_PULL_DOWNS:
4239 ftdi->eeprom->suspend_pull_downs = value;
4242 ftdi->eeprom->use_serial = value;
4245 ftdi->eeprom->usb_version = value;
4247 case USE_USB_VERSION:
4248 ftdi->eeprom->use_usb_version = value;
4251 ftdi->eeprom->max_power = value;
4253 case CHANNEL_A_TYPE:
4254 ftdi->eeprom->channel_a_type = value;
4256 case CHANNEL_B_TYPE:
4257 ftdi->eeprom->channel_b_type = value;
4259 case CHANNEL_A_DRIVER:
4260 ftdi->eeprom->channel_a_driver = value;
4262 case CHANNEL_B_DRIVER:
4263 ftdi->eeprom->channel_b_driver = value;
4265 case CHANNEL_C_DRIVER:
4266 ftdi->eeprom->channel_c_driver = value;
4268 case CHANNEL_D_DRIVER:
4269 ftdi->eeprom->channel_d_driver = value;
4271 case CHANNEL_A_RS485:
4272 ftdi->eeprom->channel_a_rs485enable = value;
4274 case CHANNEL_B_RS485:
4275 ftdi->eeprom->channel_b_rs485enable = value;
4277 case CHANNEL_C_RS485:
4278 ftdi->eeprom->channel_c_rs485enable = value;
4280 case CHANNEL_D_RS485:
4281 ftdi->eeprom->channel_d_rs485enable = value;
4283 case CBUS_FUNCTION_0:
4284 ftdi->eeprom->cbus_function[0] = value;
4286 case CBUS_FUNCTION_1:
4287 ftdi->eeprom->cbus_function[1] = value;
4289 case CBUS_FUNCTION_2:
4290 ftdi->eeprom->cbus_function[2] = value;
4292 case CBUS_FUNCTION_3:
4293 ftdi->eeprom->cbus_function[3] = value;
4295 case CBUS_FUNCTION_4:
4296 ftdi->eeprom->cbus_function[4] = value;
4298 case CBUS_FUNCTION_5:
4299 ftdi->eeprom->cbus_function[5] = value;
4301 case CBUS_FUNCTION_6:
4302 ftdi->eeprom->cbus_function[6] = value;
4304 case CBUS_FUNCTION_7:
4305 ftdi->eeprom->cbus_function[7] = value;
4307 case CBUS_FUNCTION_8:
4308 ftdi->eeprom->cbus_function[8] = value;
4310 case CBUS_FUNCTION_9:
4311 ftdi->eeprom->cbus_function[9] = value;
4314 ftdi->eeprom->high_current = value;
4316 case HIGH_CURRENT_A:
4317 ftdi->eeprom->high_current_a = value;
4319 case HIGH_CURRENT_B:
4320 ftdi->eeprom->high_current_b = value;
4323 ftdi->eeprom->invert = value;
4326 ftdi->eeprom->group0_drive = value;
4328 case GROUP0_SCHMITT:
4329 ftdi->eeprom->group0_schmitt = value;
4332 ftdi->eeprom->group0_slew = value;
4335 ftdi->eeprom->group1_drive = value;
4337 case GROUP1_SCHMITT:
4338 ftdi->eeprom->group1_schmitt = value;
4341 ftdi->eeprom->group1_slew = value;
4344 ftdi->eeprom->group2_drive = value;
4346 case GROUP2_SCHMITT:
4347 ftdi->eeprom->group2_schmitt = value;
4350 ftdi->eeprom->group2_slew = value;
4353 ftdi->eeprom->group3_drive = value;
4355 case GROUP3_SCHMITT:
4356 ftdi->eeprom->group3_schmitt = value;
4359 ftdi->eeprom->group3_slew = value;
4362 ftdi->eeprom->chip = value;
4365 ftdi->eeprom->powersave = value;
4367 case CLOCK_POLARITY:
4368 ftdi->eeprom->clock_polarity = value;
4371 ftdi->eeprom->data_order = value;
4374 ftdi->eeprom->flow_control = value;
4377 ftdi_error_return(-2, "EEPROM Value can't be changed");
4379 case EXTERNAL_OSCILLATOR:
4380 ftdi->eeprom->external_oscillator = value;
4382 case USER_DATA_ADDR:
4383 ftdi->eeprom->user_data_addr = value;
4387 ftdi_error_return(-1, "Request to unknown EEPROM value");
4389 ftdi->eeprom->initialized_for_connected_device = 0;
4393 /** Get the read-only buffer to the binary EEPROM content
4395 \param ftdi pointer to ftdi_context
4396 \param buf buffer to receive EEPROM content
4397 \param size Size of receiving buffer
4400 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
4401 \retval -2: Not enough room to store eeprom
4403 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4405 if (!ftdi || !(ftdi->eeprom))
4406 ftdi_error_return(-1, "No appropriate structure");
4408 if (!buf || size < ftdi->eeprom->size)
4409 ftdi_error_return(-1, "Not enough room to store eeprom");
4411 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4412 if (size > FTDI_MAX_EEPROM_SIZE)
4413 size = FTDI_MAX_EEPROM_SIZE;
4415 memcpy(buf, ftdi->eeprom->buf, size);
4420 /** Set the EEPROM content from the user-supplied prefilled buffer
4422 \param ftdi pointer to ftdi_context
4423 \param buf buffer to read EEPROM content
4424 \param size Size of buffer
4427 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4429 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4431 if (!ftdi || !(ftdi->eeprom) || !buf)
4432 ftdi_error_return(-1, "No appropriate structure");
4434 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4435 if (size > FTDI_MAX_EEPROM_SIZE)
4436 size = FTDI_MAX_EEPROM_SIZE;
4438 memcpy(ftdi->eeprom->buf, buf, size);
4443 /** Set the EEPROM user data content from the user-supplied prefilled buffer
4445 \param ftdi pointer to ftdi_context
4446 \param buf buffer to read EEPROM user data content
4447 \param size Size of buffer
4450 \retval -1: struct ftdi_context or ftdi_eeprom or buf missing
4452 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4454 if (!ftdi || !(ftdi->eeprom) || !buf)
4455 ftdi_error_return(-1, "No appropriate structure");
4457 ftdi->eeprom->user_data_size = size;
4458 ftdi->eeprom->user_data = buf;
4463 Read eeprom location
4465 \param ftdi pointer to ftdi_context
4466 \param eeprom_addr Address of eeprom location to be read
4467 \param eeprom_val Pointer to store read eeprom location
4470 \retval -1: read failed
4471 \retval -2: USB device unavailable
4473 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4475 unsigned char buf[2];
4477 if (ftdi == NULL || ftdi->usb_dev == NULL)
4478 ftdi_error_return(-2, "USB device unavailable");
4480 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)
4481 ftdi_error_return(-1, "reading eeprom failed");
4483 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
4491 \param ftdi pointer to ftdi_context
4494 \retval -1: read failed
4495 \retval -2: USB device unavailable
4497 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4502 if (ftdi == NULL || ftdi->usb_dev == NULL)
4503 ftdi_error_return(-2, "USB device unavailable");
4504 buf = ftdi->eeprom->buf;
4506 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4508 if (libusb_control_transfer(
4509 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4510 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4511 ftdi_error_return(-1, "reading eeprom failed");
4514 if (ftdi->type == TYPE_R)
4515 ftdi->eeprom->size = 0x80;
4516 /* Guesses size of eeprom by comparing halves
4517 - will not work with blank eeprom */
4518 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4519 ftdi->eeprom->size = -1;
4520 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4521 ftdi->eeprom->size = 0x80;
4522 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4523 ftdi->eeprom->size = 0x40;
4525 ftdi->eeprom->size = 0x100;
4530 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4531 Function is only used internally
4534 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4536 return ((value & 1) << 1) |
4537 ((value & 2) << 5) |
4538 ((value & 4) >> 2) |
4539 ((value & 8) << 4) |
4540 ((value & 16) >> 1) |
4541 ((value & 32) >> 1) |
4542 ((value & 64) >> 4) |
4543 ((value & 128) >> 2);
4547 Read the FTDIChip-ID from R-type devices
4549 \param ftdi pointer to ftdi_context
4550 \param chipid Pointer to store FTDIChip-ID
4553 \retval -1: read failed
4554 \retval -2: USB device unavailable
4556 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4558 unsigned int a = 0, b = 0;
4560 if (ftdi == NULL || ftdi->usb_dev == NULL)
4561 ftdi_error_return(-2, "USB device unavailable");
4563 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)
4565 a = a << 8 | a >> 8;
4566 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)
4568 b = b << 8 | b >> 8;
4569 a = (a << 16) | (b & 0xFFFF);
4570 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4571 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4572 *chipid = a ^ 0xa5f0f7d1;
4577 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4581 Write eeprom location
4583 \param ftdi pointer to ftdi_context
4584 \param eeprom_addr Address of eeprom location to be written
4585 \param eeprom_val Value to be written
4588 \retval -1: write failed
4589 \retval -2: USB device unavailable
4590 \retval -3: Invalid access to checksum protected area below 0x80
4591 \retval -4: Device can't access unprotected area
4592 \retval -5: Reading chip type failed
4594 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4595 unsigned short eeprom_val)
4597 int chip_type_location;
4598 unsigned short chip_type;
4600 if (ftdi == NULL || ftdi->usb_dev == NULL)
4601 ftdi_error_return(-2, "USB device unavailable");
4603 if (eeprom_addr <0x80)
4604 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4611 chip_type_location = 0x14;
4615 chip_type_location = 0x18;
4618 chip_type_location = 0x1e;
4621 ftdi_error_return(-4, "Device can't access unprotected area");
4624 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4625 ftdi_error_return(-5, "Reading failed");
4626 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4627 if ((chip_type & 0xff) != 0x66)
4629 ftdi_error_return(-6, "EEPROM is not of 93x66");
4632 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4633 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4634 NULL, 0, ftdi->usb_write_timeout) != 0)
4635 ftdi_error_return(-1, "unable to write eeprom");
4643 \param ftdi pointer to ftdi_context
4646 \retval -1: read failed
4647 \retval -2: USB device unavailable
4648 \retval -3: EEPROM not initialized for the connected device;
4650 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4652 unsigned short usb_val, status;
4654 unsigned char *eeprom;
4656 if (ftdi == NULL || ftdi->usb_dev == NULL)
4657 ftdi_error_return(-2, "USB device unavailable");
4659 if(ftdi->eeprom->initialized_for_connected_device == 0)
4660 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4662 eeprom = ftdi->eeprom->buf;
4664 /* These commands were traced while running MProg */
4665 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4667 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4669 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4672 for (i = 0; i < ftdi->eeprom->size/2; i++)
4674 /* Do not try to write to reserved area */
4675 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4679 usb_val = eeprom[i*2];
4680 usb_val += eeprom[(i*2)+1] << 8;
4681 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4682 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4683 NULL, 0, ftdi->usb_write_timeout) < 0)
4684 ftdi_error_return(-1, "unable to write eeprom");
4693 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4695 \param ftdi pointer to ftdi_context
4698 \retval -1: erase failed
4699 \retval -2: USB device unavailable
4700 \retval -3: Writing magic failed
4701 \retval -4: Read EEPROM failed
4702 \retval -5: Unexpected EEPROM value
4704 #define MAGIC 0x55aa
4705 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4707 unsigned short eeprom_value;
4708 if (ftdi == NULL || ftdi->usb_dev == NULL)
4709 ftdi_error_return(-2, "USB device unavailable");
4711 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4713 ftdi->eeprom->chip = 0;
4717 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4718 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4719 ftdi_error_return(-1, "unable to erase eeprom");
4722 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4723 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4724 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4725 Chip is 93x66 if magic is only read at word position 0xc0*/
4726 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4727 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4728 NULL, 0, ftdi->usb_write_timeout) != 0)
4729 ftdi_error_return(-3, "Writing magic failed");
4730 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4731 ftdi_error_return(-4, "Reading failed");
4732 if (eeprom_value == MAGIC)
4734 ftdi->eeprom->chip = 0x46;
4738 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4739 ftdi_error_return(-4, "Reading failed");
4740 if (eeprom_value == MAGIC)
4741 ftdi->eeprom->chip = 0x56;
4744 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4745 ftdi_error_return(-4, "Reading failed");
4746 if (eeprom_value == MAGIC)
4747 ftdi->eeprom->chip = 0x66;
4750 ftdi->eeprom->chip = -1;
4754 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4755 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4756 ftdi_error_return(-1, "unable to erase eeprom");
4761 Get string representation for last error code
4763 \param ftdi pointer to ftdi_context
4765 \retval Pointer to error string
4767 const char *ftdi_get_error_string (struct ftdi_context *ftdi)
4772 return ftdi->error_str;
4775 /* @} end of doxygen libftdi group */