1 /***************************************************************************
5 copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
18 \mainpage libftdi API documentation
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/en/developer/libftdi/
23 The library is easy to use. Have a look at this short example:
26 More examples can be found in the "examples" directory.
28 /** \addtogroup libftdi */
39 #include "ftdi_version_i.h"
41 #define ftdi_error_return(code, str) do { \
43 ftdi->error_str = str; \
45 fprintf(stderr, str); \
49 #define ftdi_error_return_free_device_list(code, str, devs) do { \
50 libusb_free_device_list(devs,1); \
51 ftdi->error_str = str; \
57 Internal function to close usb device pointer.
58 Sets ftdi->usb_dev to NULL.
61 \param ftdi pointer to ftdi_context
65 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
67 if (ftdi && ftdi->usb_dev)
69 libusb_close (ftdi->usb_dev);
72 ftdi->eeprom->initialized_for_connected_device = 0;
77 Initializes a ftdi_context.
79 \param ftdi pointer to ftdi_context
82 \retval -1: couldn't allocate read buffer
83 \retval -2: couldn't allocate struct buffer
84 \retval -3: libusb_init() failed
86 \remark This should be called before all functions
88 int ftdi_init(struct ftdi_context *ftdi)
90 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
93 ftdi->usb_read_timeout = 5000;
94 ftdi->usb_write_timeout = 5000;
96 ftdi->type = TYPE_BM; /* chip type */
98 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
100 ftdi->readbuffer = NULL;
101 ftdi->readbuffer_offset = 0;
102 ftdi->readbuffer_remaining = 0;
103 ftdi->writebuffer_chunksize = 4096;
104 ftdi->max_packet_size = 0;
105 ftdi->error_str = NULL;
106 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
108 if (libusb_init(&ftdi->usb_ctx) < 0)
109 ftdi_error_return(-3, "libusb_init() failed");
111 ftdi_set_interface(ftdi, INTERFACE_ANY);
112 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
115 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
116 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
117 ftdi->eeprom = eeprom;
119 /* All fine. Now allocate the readbuffer */
120 return ftdi_read_data_set_chunksize(ftdi, 4096);
124 Allocate and initialize a new ftdi_context
126 \return a pointer to a new ftdi_context, or NULL on failure
128 struct ftdi_context *ftdi_new(void)
130 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
137 if (ftdi_init(ftdi) != 0)
147 Open selected channels on a chip, otherwise use first channel.
149 \param ftdi pointer to ftdi_context
150 \param interface Interface to use for FT2232C/2232H/4232H chips.
153 \retval -1: unknown interface
154 \retval -2: USB device unavailable
155 \retval -3: Device already open, interface can't be set in that state
157 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
160 ftdi_error_return(-2, "USB device unavailable");
162 if (ftdi->usb_dev != NULL)
164 int check_interface = interface;
165 if (check_interface == INTERFACE_ANY)
166 check_interface = INTERFACE_A;
168 if (ftdi->index != check_interface)
169 ftdi_error_return(-3, "Interface can not be changed on an already open device");
177 ftdi->index = INTERFACE_A;
183 ftdi->index = INTERFACE_B;
189 ftdi->index = INTERFACE_C;
195 ftdi->index = INTERFACE_D;
200 ftdi_error_return(-1, "Unknown interface");
206 Deinitializes a ftdi_context.
208 \param ftdi pointer to ftdi_context
210 void ftdi_deinit(struct ftdi_context *ftdi)
215 ftdi_usb_close_internal (ftdi);
217 if (ftdi->readbuffer != NULL)
219 free(ftdi->readbuffer);
220 ftdi->readbuffer = NULL;
223 if (ftdi->eeprom != NULL)
225 if (ftdi->eeprom->manufacturer != 0)
227 free(ftdi->eeprom->manufacturer);
228 ftdi->eeprom->manufacturer = 0;
230 if (ftdi->eeprom->product != 0)
232 free(ftdi->eeprom->product);
233 ftdi->eeprom->product = 0;
235 if (ftdi->eeprom->serial != 0)
237 free(ftdi->eeprom->serial);
238 ftdi->eeprom->serial = 0;
246 libusb_exit(ftdi->usb_ctx);
247 ftdi->usb_ctx = NULL;
252 Deinitialize and free an ftdi_context.
254 \param ftdi pointer to ftdi_context
256 void ftdi_free(struct ftdi_context *ftdi)
263 Use an already open libusb device.
265 \param ftdi pointer to ftdi_context
266 \param usb libusb libusb_device_handle to use
268 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
277 * @brief Get libftdi library version
279 * @return ftdi_version_info Library version information
281 struct ftdi_version_info ftdi_get_library_version(void)
283 struct ftdi_version_info ver;
285 ver.major = FTDI_MAJOR_VERSION;
286 ver.minor = FTDI_MINOR_VERSION;
287 ver.micro = FTDI_MICRO_VERSION;
288 ver.version_str = FTDI_VERSION_STRING;
289 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
295 Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
296 ftdi_device_list which needs to be deallocated by ftdi_list_free() after
297 use. With VID:PID 0:0, search for the default devices
298 (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015)
300 \param ftdi pointer to ftdi_context
301 \param devlist Pointer where to store list of found devices
302 \param vendor Vendor ID to search for
303 \param product Product ID to search for
305 \retval >0: number of devices found
306 \retval -3: out of memory
307 \retval -5: libusb_get_device_list() failed
308 \retval -6: libusb_get_device_descriptor() failed
310 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
312 struct ftdi_device_list **curdev;
314 libusb_device **devs;
318 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
319 ftdi_error_return(-5, "libusb_get_device_list() failed");
324 while ((dev = devs[i++]) != NULL)
326 struct libusb_device_descriptor desc;
328 if (libusb_get_device_descriptor(dev, &desc) < 0)
329 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
331 if (((vendor || product) &&
332 desc.idVendor == vendor && desc.idProduct == product) ||
333 (!(vendor || product) &&
334 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
335 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
336 || desc.idProduct == 0x6015)))
338 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
340 ftdi_error_return_free_device_list(-3, "out of memory", devs);
342 (*curdev)->next = NULL;
343 (*curdev)->dev = dev;
344 libusb_ref_device(dev);
345 curdev = &(*curdev)->next;
349 libusb_free_device_list(devs,1);
354 Frees a usb device list.
356 \param devlist USB device list created by ftdi_usb_find_all()
358 void ftdi_list_free(struct ftdi_device_list **devlist)
360 struct ftdi_device_list *curdev, *next;
362 for (curdev = *devlist; curdev != NULL;)
365 libusb_unref_device(curdev->dev);
374 Frees a usb device list.
376 \param devlist USB device list created by ftdi_usb_find_all()
378 void ftdi_list_free2(struct ftdi_device_list *devlist)
380 ftdi_list_free(&devlist);
384 Return device ID strings from the usb device.
386 The parameters manufacturer, description and serial may be NULL
387 or pointer to buffers to store the fetched strings.
389 \note Use this function only in combination with ftdi_usb_find_all()
390 as it closes the internal "usb_dev" after use.
392 \param ftdi pointer to ftdi_context
393 \param dev libusb usb_dev to use
394 \param manufacturer Store manufacturer string here if not NULL
395 \param mnf_len Buffer size of manufacturer string
396 \param description Store product description string here if not NULL
397 \param desc_len Buffer size of product description string
398 \param serial Store serial string here if not NULL
399 \param serial_len Buffer size of serial string
402 \retval -1: wrong arguments
403 \retval -4: unable to open device
404 \retval -7: get product manufacturer failed
405 \retval -8: get product description failed
406 \retval -9: get serial number failed
407 \retval -11: libusb_get_device_descriptor() failed
409 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
410 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
412 struct libusb_device_descriptor desc;
414 if ((ftdi==NULL) || (dev==NULL))
417 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
418 ftdi_error_return(-4, "libusb_open() failed");
420 if (libusb_get_device_descriptor(dev, &desc) < 0)
421 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
423 if (manufacturer != NULL)
425 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
427 ftdi_usb_close_internal (ftdi);
428 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
432 if (description != NULL)
434 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
436 ftdi_usb_close_internal (ftdi);
437 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
443 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
445 ftdi_usb_close_internal (ftdi);
446 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
450 ftdi_usb_close_internal (ftdi);
456 * Internal function to determine the maximum packet size.
457 * \param ftdi pointer to ftdi_context
458 * \param dev libusb usb_dev to use
459 * \retval Maximum packet size for this device
461 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
463 struct libusb_device_descriptor desc;
464 struct libusb_config_descriptor *config0;
465 unsigned int packet_size;
468 if (ftdi == NULL || dev == NULL)
471 // Determine maximum packet size. Init with default value.
472 // New hi-speed devices from FTDI use a packet size of 512 bytes
473 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
474 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
479 if (libusb_get_device_descriptor(dev, &desc) < 0)
482 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
485 if (desc.bNumConfigurations > 0)
487 if (ftdi->interface < config0->bNumInterfaces)
489 struct libusb_interface interface = config0->interface[ftdi->interface];
490 if (interface.num_altsetting > 0)
492 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
493 if (descriptor.bNumEndpoints > 0)
495 packet_size = descriptor.endpoint[0].wMaxPacketSize;
501 libusb_free_config_descriptor (config0);
506 Opens a ftdi device given by an usb_device.
508 \param ftdi pointer to ftdi_context
509 \param dev libusb usb_dev to use
512 \retval -3: unable to config device
513 \retval -4: unable to open device
514 \retval -5: unable to claim device
515 \retval -6: reset failed
516 \retval -7: set baudrate failed
517 \retval -8: ftdi context invalid
518 \retval -9: libusb_get_device_descriptor() failed
519 \retval -10: libusb_get_config_descriptor() failed
520 \retval -11: libusb_detach_kernel_driver() failed
521 \retval -12: libusb_get_configuration() failed
523 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
525 struct libusb_device_descriptor desc;
526 struct libusb_config_descriptor *config0;
527 int cfg, cfg0, detach_errno = 0;
530 ftdi_error_return(-8, "ftdi context invalid");
532 if (libusb_open(dev, &ftdi->usb_dev) < 0)
533 ftdi_error_return(-4, "libusb_open() failed");
535 if (libusb_get_device_descriptor(dev, &desc) < 0)
536 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
538 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
539 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
540 cfg0 = config0->bConfigurationValue;
541 libusb_free_config_descriptor (config0);
543 // Try to detach ftdi_sio kernel module.
545 // The return code is kept in a separate variable and only parsed
546 // if usb_set_configuration() or usb_claim_interface() fails as the
547 // detach operation might be denied and everything still works fine.
548 // Likely scenario is a static ftdi_sio kernel module.
549 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
551 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
552 detach_errno = errno;
555 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
556 ftdi_error_return(-12, "libusb_get_configuration () failed");
557 // set configuration (needed especially for windows)
558 // tolerate EBUSY: one device with one configuration, but two interfaces
559 // and libftdi sessions to both interfaces (e.g. FT2232)
560 if (desc.bNumConfigurations > 0 && cfg != cfg0)
562 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
564 ftdi_usb_close_internal (ftdi);
565 if (detach_errno == EPERM)
567 ftdi_error_return(-8, "inappropriate permissions on device!");
571 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
576 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
578 ftdi_usb_close_internal (ftdi);
579 if (detach_errno == EPERM)
581 ftdi_error_return(-8, "inappropriate permissions on device!");
585 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
589 if (ftdi_usb_reset (ftdi) != 0)
591 ftdi_usb_close_internal (ftdi);
592 ftdi_error_return(-6, "ftdi_usb_reset failed");
595 // Try to guess chip type
596 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
597 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
598 && desc.iSerialNumber == 0))
599 ftdi->type = TYPE_BM;
600 else if (desc.bcdDevice == 0x200)
601 ftdi->type = TYPE_AM;
602 else if (desc.bcdDevice == 0x500)
603 ftdi->type = TYPE_2232C;
604 else if (desc.bcdDevice == 0x600)
606 else if (desc.bcdDevice == 0x700)
607 ftdi->type = TYPE_2232H;
608 else if (desc.bcdDevice == 0x800)
609 ftdi->type = TYPE_4232H;
610 else if (desc.bcdDevice == 0x900)
611 ftdi->type = TYPE_232H;
612 else if (desc.bcdDevice == 0x1000)
613 ftdi->type = TYPE_230X;
615 // Determine maximum packet size
616 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
618 if (ftdi_set_baudrate (ftdi, 9600) != 0)
620 ftdi_usb_close_internal (ftdi);
621 ftdi_error_return(-7, "set baudrate failed");
624 ftdi_error_return(0, "all fine");
628 Opens the first device with a given vendor and product ids.
630 \param ftdi pointer to ftdi_context
631 \param vendor Vendor ID
632 \param product Product ID
634 \retval same as ftdi_usb_open_desc()
636 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
638 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
642 Opens the first device with a given, vendor id, product id,
643 description and serial.
645 \param ftdi pointer to ftdi_context
646 \param vendor Vendor ID
647 \param product Product ID
648 \param description Description to search for. Use NULL if not needed.
649 \param serial Serial to search for. Use NULL if not needed.
652 \retval -3: usb device not found
653 \retval -4: unable to open device
654 \retval -5: unable to claim device
655 \retval -6: reset failed
656 \retval -7: set baudrate failed
657 \retval -8: get product description failed
658 \retval -9: get serial number failed
659 \retval -12: libusb_get_device_list() failed
660 \retval -13: libusb_get_device_descriptor() failed
662 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
663 const char* description, const char* serial)
665 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
669 Opens the index-th device with a given, vendor id, product id,
670 description and serial.
672 \param ftdi pointer to ftdi_context
673 \param vendor Vendor ID
674 \param product Product ID
675 \param description Description to search for. Use NULL if not needed.
676 \param serial Serial to search for. Use NULL if not needed.
677 \param index Number of matching device to open if there are more than one, starts with 0.
680 \retval -1: usb_find_busses() failed
681 \retval -2: usb_find_devices() failed
682 \retval -3: usb device not found
683 \retval -4: unable to open device
684 \retval -5: unable to claim device
685 \retval -6: reset failed
686 \retval -7: set baudrate failed
687 \retval -8: get product description failed
688 \retval -9: get serial number failed
689 \retval -10: unable to close device
690 \retval -11: ftdi context invalid
692 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
693 const char* description, const char* serial, unsigned int index)
696 libusb_device **devs;
701 ftdi_error_return(-11, "ftdi context invalid");
703 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
704 ftdi_error_return(-12, "libusb_get_device_list() failed");
706 while ((dev = devs[i++]) != NULL)
708 struct libusb_device_descriptor desc;
711 if (libusb_get_device_descriptor(dev, &desc) < 0)
712 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
714 if (desc.idVendor == vendor && desc.idProduct == product)
716 if (libusb_open(dev, &ftdi->usb_dev) < 0)
717 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
719 if (description != NULL)
721 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
723 ftdi_usb_close_internal (ftdi);
724 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
726 if (strncmp(string, description, sizeof(string)) != 0)
728 ftdi_usb_close_internal (ftdi);
734 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
736 ftdi_usb_close_internal (ftdi);
737 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
739 if (strncmp(string, serial, sizeof(string)) != 0)
741 ftdi_usb_close_internal (ftdi);
746 ftdi_usb_close_internal (ftdi);
754 res = ftdi_usb_open_dev(ftdi, dev);
755 libusb_free_device_list(devs,1);
761 ftdi_error_return_free_device_list(-3, "device not found", devs);
765 Opens the ftdi-device described by a description-string.
766 Intended to be used for parsing a device-description given as commandline argument.
768 \param ftdi pointer to ftdi_context
769 \param description NULL-terminated description-string, using this format:
770 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
771 \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")
772 \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
773 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
775 \note The description format may be extended in later versions.
778 \retval -2: libusb_get_device_list() failed
779 \retval -3: usb device not found
780 \retval -4: unable to open device
781 \retval -5: unable to claim device
782 \retval -6: reset failed
783 \retval -7: set baudrate failed
784 \retval -8: get product description failed
785 \retval -9: get serial number failed
786 \retval -10: unable to close device
787 \retval -11: illegal description format
788 \retval -12: ftdi context invalid
790 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
793 ftdi_error_return(-12, "ftdi context invalid");
795 if (description[0] == 0 || description[1] != ':')
796 ftdi_error_return(-11, "illegal description format");
798 if (description[0] == 'd')
801 libusb_device **devs;
802 unsigned int bus_number, device_address;
805 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
806 ftdi_error_return(-2, "libusb_get_device_list() failed");
808 /* XXX: This doesn't handle symlinks/odd paths/etc... */
809 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
810 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
812 while ((dev = devs[i++]) != NULL)
815 if (bus_number == libusb_get_bus_number (dev)
816 && device_address == libusb_get_device_address (dev))
818 ret = ftdi_usb_open_dev(ftdi, dev);
819 libusb_free_device_list(devs,1);
825 ftdi_error_return_free_device_list(-3, "device not found", devs);
827 else if (description[0] == 'i' || description[0] == 's')
830 unsigned int product;
831 unsigned int index=0;
832 const char *serial=NULL;
833 const char *startp, *endp;
836 startp=description+2;
837 vendor=strtoul((char*)startp,(char**)&endp,0);
838 if (*endp != ':' || endp == startp || errno != 0)
839 ftdi_error_return(-11, "illegal description format");
842 product=strtoul((char*)startp,(char**)&endp,0);
843 if (endp == startp || errno != 0)
844 ftdi_error_return(-11, "illegal description format");
846 if (description[0] == 'i' && *endp != 0)
848 /* optional index field in i-mode */
850 ftdi_error_return(-11, "illegal description format");
853 index=strtoul((char*)startp,(char**)&endp,0);
854 if (*endp != 0 || endp == startp || errno != 0)
855 ftdi_error_return(-11, "illegal description format");
857 if (description[0] == 's')
860 ftdi_error_return(-11, "illegal description format");
862 /* rest of the description is the serial */
866 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
870 ftdi_error_return(-11, "illegal description format");
875 Resets the ftdi device.
877 \param ftdi pointer to ftdi_context
880 \retval -1: FTDI reset failed
881 \retval -2: USB device unavailable
883 int ftdi_usb_reset(struct ftdi_context *ftdi)
885 if (ftdi == NULL || ftdi->usb_dev == NULL)
886 ftdi_error_return(-2, "USB device unavailable");
888 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
889 SIO_RESET_REQUEST, SIO_RESET_SIO,
890 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
891 ftdi_error_return(-1,"FTDI reset failed");
893 // Invalidate data in the readbuffer
894 ftdi->readbuffer_offset = 0;
895 ftdi->readbuffer_remaining = 0;
901 Clears the read buffer on the chip and the internal read buffer.
903 \param ftdi pointer to ftdi_context
906 \retval -1: read buffer purge failed
907 \retval -2: USB device unavailable
909 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
911 if (ftdi == NULL || ftdi->usb_dev == NULL)
912 ftdi_error_return(-2, "USB device unavailable");
914 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
915 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
916 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
917 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
919 // Invalidate data in the readbuffer
920 ftdi->readbuffer_offset = 0;
921 ftdi->readbuffer_remaining = 0;
927 Clears the write buffer on the chip.
929 \param ftdi pointer to ftdi_context
932 \retval -1: write buffer purge failed
933 \retval -2: USB device unavailable
935 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
937 if (ftdi == NULL || ftdi->usb_dev == NULL)
938 ftdi_error_return(-2, "USB device unavailable");
940 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
941 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
942 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
943 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
949 Clears the buffers on the chip and the internal read buffer.
951 \param ftdi pointer to ftdi_context
954 \retval -1: read buffer purge failed
955 \retval -2: write buffer purge failed
956 \retval -3: USB device unavailable
958 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
962 if (ftdi == NULL || ftdi->usb_dev == NULL)
963 ftdi_error_return(-3, "USB device unavailable");
965 result = ftdi_usb_purge_rx_buffer(ftdi);
969 result = ftdi_usb_purge_tx_buffer(ftdi);
979 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
981 \param ftdi pointer to ftdi_context
984 \retval -1: usb_release failed
985 \retval -3: ftdi context invalid
987 int ftdi_usb_close(struct ftdi_context *ftdi)
992 ftdi_error_return(-3, "ftdi context invalid");
994 if (ftdi->usb_dev != NULL)
995 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
998 ftdi_usb_close_internal (ftdi);
1003 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1004 to encoded divisor and the achievable baudrate
1005 Function is only used internally
1012 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1013 The fractional part has frac_code encoding
1015 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1018 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1019 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1020 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1021 int divisor, best_divisor, best_baud, best_baud_diff;
1023 divisor = 24000000 / baudrate;
1025 // Round down to supported fraction (AM only)
1026 divisor -= am_adjust_dn[divisor & 7];
1028 // Try this divisor and the one above it (because division rounds down)
1032 for (i = 0; i < 2; i++)
1034 int try_divisor = divisor + i;
1038 // Round up to supported divisor value
1039 if (try_divisor <= 8)
1041 // Round up to minimum supported divisor
1044 else if (divisor < 16)
1046 // AM doesn't support divisors 9 through 15 inclusive
1051 // Round up to supported fraction (AM only)
1052 try_divisor += am_adjust_up[try_divisor & 7];
1053 if (try_divisor > 0x1FFF8)
1055 // Round down to maximum supported divisor value (for AM)
1056 try_divisor = 0x1FFF8;
1059 // Get estimated baud rate (to nearest integer)
1060 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1061 // Get absolute difference from requested baud rate
1062 if (baud_estimate < baudrate)
1064 baud_diff = baudrate - baud_estimate;
1068 baud_diff = baud_estimate - baudrate;
1070 if (i == 0 || baud_diff < best_baud_diff)
1072 // Closest to requested baud rate so far
1073 best_divisor = try_divisor;
1074 best_baud = baud_estimate;
1075 best_baud_diff = baud_diff;
1078 // Spot on! No point trying
1083 // Encode the best divisor value
1084 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1085 // Deal with special cases for encoded value
1086 if (*encoded_divisor == 1)
1088 *encoded_divisor = 0; // 3000000 baud
1090 else if (*encoded_divisor == 0x4001)
1092 *encoded_divisor = 1; // 2000000 baud (BM only)
1097 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1098 to encoded divisor and the achievable baudrate
1099 Function is only used internally
1106 From /2, 0.125 steps may be taken.
1107 The fractional part has frac_code encoding
1109 value[13:0] of value is the divisor
1110 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1112 H Type have all features above with
1113 {index[8],value[15:14]} is the encoded subdivisor
1115 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1116 {index[0],value[15:14]} is the encoded subdivisor
1118 AM Type chips have only four fractional subdivisors at value[15:14]
1119 for subdivisors 0, 0.5, 0.25, 0.125
1121 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1123 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1125 int divisor, best_divisor;
1126 if (baudrate >= clk/clk_div)
1128 *encoded_divisor = 0;
1129 best_baud = clk/clk_div;
1131 else if (baudrate >= clk/(clk_div + clk_div/2))
1133 *encoded_divisor = 1;
1134 best_baud = clk/(clk_div + clk_div/2);
1136 else if (baudrate >= clk/(2*clk_div))
1138 *encoded_divisor = 2;
1139 best_baud = clk/(2*clk_div);
1143 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1144 divisor = clk*16/clk_div / baudrate;
1145 if (divisor & 1) /* Decide if to round up or down*/
1146 best_divisor = divisor /2 +1;
1148 best_divisor = divisor/2;
1149 if(best_divisor > 0x20000)
1150 best_divisor = 0x1ffff;
1151 best_baud = clk*16/clk_div/best_divisor;
1152 if (best_baud & 1) /* Decide if to round up or down*/
1153 best_baud = best_baud /2 +1;
1155 best_baud = best_baud /2;
1156 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1161 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1162 Function is only used internally
1165 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1166 unsigned short *value, unsigned short *index)
1169 unsigned long encoded_divisor;
1177 #define H_CLK 120000000
1178 #define C_CLK 48000000
1179 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1181 if(baudrate*10 > H_CLK /0x3fff)
1183 /* On H Devices, use 12 000 000 Baudrate when possible
1184 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1185 three fractional bits and a 120 MHz clock
1186 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1187 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1188 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1189 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1192 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1194 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
1196 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1200 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1202 // Split into "value" and "index" values
1203 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1204 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1206 *index = (unsigned short)(encoded_divisor >> 8);
1208 *index |= ftdi->index;
1211 *index = (unsigned short)(encoded_divisor >> 16);
1213 // Return the nearest baud rate
1218 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1219 * Do not use, it's only for the unit test framework
1221 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1222 unsigned short *value, unsigned short *index)
1224 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1228 Sets the chip baud rate
1230 \param ftdi pointer to ftdi_context
1231 \param baudrate baud rate to set
1234 \retval -1: invalid baudrate
1235 \retval -2: setting baudrate failed
1236 \retval -3: USB device unavailable
1238 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1240 unsigned short value, index;
1241 int actual_baudrate;
1243 if (ftdi == NULL || ftdi->usb_dev == NULL)
1244 ftdi_error_return(-3, "USB device unavailable");
1246 if (ftdi->bitbang_enabled)
1248 baudrate = baudrate*4;
1251 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1252 if (actual_baudrate <= 0)
1253 ftdi_error_return (-1, "Silly baudrate <= 0.");
1255 // Check within tolerance (about 5%)
1256 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1257 || ((actual_baudrate < baudrate)
1258 ? (actual_baudrate * 21 < baudrate * 20)
1259 : (baudrate * 21 < actual_baudrate * 20)))
1260 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1262 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1263 SIO_SET_BAUDRATE_REQUEST, value,
1264 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1265 ftdi_error_return (-2, "Setting new baudrate failed");
1267 ftdi->baudrate = baudrate;
1272 Set (RS232) line characteristics.
1273 The break type can only be set via ftdi_set_line_property2()
1274 and defaults to "off".
1276 \param ftdi pointer to ftdi_context
1277 \param bits Number of bits
1278 \param sbit Number of stop bits
1279 \param parity Parity mode
1282 \retval -1: Setting line property failed
1284 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1285 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1287 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1291 Set (RS232) line characteristics
1293 \param ftdi pointer to ftdi_context
1294 \param bits Number of bits
1295 \param sbit Number of stop bits
1296 \param parity Parity mode
1297 \param break_type Break type
1300 \retval -1: Setting line property failed
1301 \retval -2: USB device unavailable
1303 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1304 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1305 enum ftdi_break_type break_type)
1307 unsigned short value = bits;
1309 if (ftdi == NULL || ftdi->usb_dev == NULL)
1310 ftdi_error_return(-2, "USB device unavailable");
1315 value |= (0x00 << 8);
1318 value |= (0x01 << 8);
1321 value |= (0x02 << 8);
1324 value |= (0x03 << 8);
1327 value |= (0x04 << 8);
1334 value |= (0x00 << 11);
1337 value |= (0x01 << 11);
1340 value |= (0x02 << 11);
1347 value |= (0x00 << 14);
1350 value |= (0x01 << 14);
1354 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1355 SIO_SET_DATA_REQUEST, value,
1356 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1357 ftdi_error_return (-1, "Setting new line property failed");
1363 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1365 \param ftdi pointer to ftdi_context
1366 \param buf Buffer with the data
1367 \param size Size of the buffer
1369 \retval -666: USB device unavailable
1370 \retval <0: error code from usb_bulk_write()
1371 \retval >0: number of bytes written
1373 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1378 if (ftdi == NULL || ftdi->usb_dev == NULL)
1379 ftdi_error_return(-666, "USB device unavailable");
1381 while (offset < size)
1383 int write_size = ftdi->writebuffer_chunksize;
1385 if (offset+write_size > size)
1386 write_size = size-offset;
1388 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1389 ftdi_error_return(-1, "usb bulk write failed");
1391 offset += actual_length;
1397 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1399 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1400 struct ftdi_context *ftdi = tc->ftdi;
1401 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1403 packet_size = ftdi->max_packet_size;
1405 actual_length = transfer->actual_length;
1407 if (actual_length > 2)
1409 // skip FTDI status bytes.
1410 // Maybe stored in the future to enable modem use
1411 num_of_chunks = actual_length / packet_size;
1412 chunk_remains = actual_length % packet_size;
1413 //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);
1415 ftdi->readbuffer_offset += 2;
1418 if (actual_length > packet_size - 2)
1420 for (i = 1; i < num_of_chunks; i++)
1421 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1422 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1424 if (chunk_remains > 2)
1426 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1427 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1429 actual_length -= 2*num_of_chunks;
1432 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1435 if (actual_length > 0)
1437 // data still fits in buf?
1438 if (tc->offset + actual_length <= tc->size)
1440 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1441 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1442 tc->offset += actual_length;
1444 ftdi->readbuffer_offset = 0;
1445 ftdi->readbuffer_remaining = 0;
1447 /* Did we read exactly the right amount of bytes? */
1448 if (tc->offset == tc->size)
1450 //printf("read_data exact rem %d offset %d\n",
1451 //ftdi->readbuffer_remaining, offset);
1458 // only copy part of the data or size <= readbuffer_chunksize
1459 int part_size = tc->size - tc->offset;
1460 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1461 tc->offset += part_size;
1463 ftdi->readbuffer_offset += part_size;
1464 ftdi->readbuffer_remaining = actual_length - part_size;
1466 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1467 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1473 ret = libusb_submit_transfer (transfer);
1479 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1481 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1482 struct ftdi_context *ftdi = tc->ftdi;
1484 tc->offset += transfer->actual_length;
1486 if (tc->offset == tc->size)
1492 int write_size = ftdi->writebuffer_chunksize;
1495 if (tc->offset + write_size > tc->size)
1496 write_size = tc->size - tc->offset;
1498 transfer->length = write_size;
1499 transfer->buffer = tc->buf + tc->offset;
1500 ret = libusb_submit_transfer (transfer);
1508 Writes data to the chip. Does not wait for completion of the transfer
1509 nor does it make sure that the transfer was successful.
1511 Use libusb 1.0 asynchronous API.
1513 \param ftdi pointer to ftdi_context
1514 \param buf Buffer with the data
1515 \param size Size of the buffer
1517 \retval NULL: Some error happens when submit transfer
1518 \retval !NULL: Pointer to a ftdi_transfer_control
1521 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1523 struct ftdi_transfer_control *tc;
1524 struct libusb_transfer *transfer;
1525 int write_size, ret;
1527 if (ftdi == NULL || ftdi->usb_dev == NULL)
1530 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1534 transfer = libusb_alloc_transfer(0);
1547 if (size < (int)ftdi->writebuffer_chunksize)
1550 write_size = ftdi->writebuffer_chunksize;
1552 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1553 write_size, ftdi_write_data_cb, tc,
1554 ftdi->usb_write_timeout);
1555 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1557 ret = libusb_submit_transfer(transfer);
1560 libusb_free_transfer(transfer);
1564 tc->transfer = transfer;
1570 Reads data from the chip. Does not wait for completion of the transfer
1571 nor does it make sure that the transfer was successful.
1573 Use libusb 1.0 asynchronous API.
1575 \param ftdi pointer to ftdi_context
1576 \param buf Buffer with the data
1577 \param size Size of the buffer
1579 \retval NULL: Some error happens when submit transfer
1580 \retval !NULL: Pointer to a ftdi_transfer_control
1583 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1585 struct ftdi_transfer_control *tc;
1586 struct libusb_transfer *transfer;
1589 if (ftdi == NULL || ftdi->usb_dev == NULL)
1592 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1600 if (size <= (int)ftdi->readbuffer_remaining)
1602 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1605 ftdi->readbuffer_remaining -= size;
1606 ftdi->readbuffer_offset += size;
1608 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1612 tc->transfer = NULL;
1617 if (ftdi->readbuffer_remaining != 0)
1619 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1621 tc->offset = ftdi->readbuffer_remaining;
1626 transfer = libusb_alloc_transfer(0);
1633 ftdi->readbuffer_remaining = 0;
1634 ftdi->readbuffer_offset = 0;
1636 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);
1637 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1639 ret = libusb_submit_transfer(transfer);
1642 libusb_free_transfer(transfer);
1646 tc->transfer = transfer;
1652 Wait for completion of the transfer.
1654 Use libusb 1.0 asynchronous API.
1656 \param tc pointer to ftdi_transfer_control
1658 \retval < 0: Some error happens
1659 \retval >= 0: Data size transferred
1662 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1666 while (!tc->completed)
1668 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1671 if (ret == LIBUSB_ERROR_INTERRUPTED)
1673 libusb_cancel_transfer(tc->transfer);
1674 while (!tc->completed)
1675 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1677 libusb_free_transfer(tc->transfer);
1685 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1686 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1690 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1692 libusb_free_transfer(tc->transfer);
1699 Configure write buffer chunk size.
1702 \param ftdi pointer to ftdi_context
1703 \param chunksize Chunk size
1706 \retval -1: ftdi context invalid
1708 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1711 ftdi_error_return(-1, "ftdi context invalid");
1713 ftdi->writebuffer_chunksize = chunksize;
1718 Get write buffer chunk size.
1720 \param ftdi pointer to ftdi_context
1721 \param chunksize Pointer to store chunk size in
1724 \retval -1: ftdi context invalid
1726 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1729 ftdi_error_return(-1, "ftdi context invalid");
1731 *chunksize = ftdi->writebuffer_chunksize;
1736 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1738 Automatically strips the two modem status bytes transfered during every read.
1740 \param ftdi pointer to ftdi_context
1741 \param buf Buffer to store data in
1742 \param size Size of the buffer
1744 \retval -666: USB device unavailable
1745 \retval <0: error code from libusb_bulk_transfer()
1746 \retval 0: no data was available
1747 \retval >0: number of bytes read
1750 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1752 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1753 int packet_size = ftdi->max_packet_size;
1754 int actual_length = 1;
1756 if (ftdi == NULL || ftdi->usb_dev == NULL)
1757 ftdi_error_return(-666, "USB device unavailable");
1759 // Packet size sanity check (avoid division by zero)
1760 if (packet_size == 0)
1761 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1763 // everything we want is still in the readbuffer?
1764 if (size <= (int)ftdi->readbuffer_remaining)
1766 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1769 ftdi->readbuffer_remaining -= size;
1770 ftdi->readbuffer_offset += size;
1772 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1776 // something still in the readbuffer, but not enough to satisfy 'size'?
1777 if (ftdi->readbuffer_remaining != 0)
1779 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1782 offset += ftdi->readbuffer_remaining;
1784 // do the actual USB read
1785 while (offset < size && actual_length > 0)
1787 ftdi->readbuffer_remaining = 0;
1788 ftdi->readbuffer_offset = 0;
1789 /* returns how much received */
1790 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1792 ftdi_error_return(ret, "usb bulk read failed");
1794 if (actual_length > 2)
1796 // skip FTDI status bytes.
1797 // Maybe stored in the future to enable modem use
1798 num_of_chunks = actual_length / packet_size;
1799 chunk_remains = actual_length % packet_size;
1800 //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);
1802 ftdi->readbuffer_offset += 2;
1805 if (actual_length > packet_size - 2)
1807 for (i = 1; i < num_of_chunks; i++)
1808 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1809 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1811 if (chunk_remains > 2)
1813 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1814 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1816 actual_length -= 2*num_of_chunks;
1819 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1822 else if (actual_length <= 2)
1824 // no more data to read?
1827 if (actual_length > 0)
1829 // data still fits in buf?
1830 if (offset+actual_length <= size)
1832 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1833 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1834 offset += actual_length;
1836 /* Did we read exactly the right amount of bytes? */
1838 //printf("read_data exact rem %d offset %d\n",
1839 //ftdi->readbuffer_remaining, offset);
1844 // only copy part of the data or size <= readbuffer_chunksize
1845 int part_size = size-offset;
1846 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1848 ftdi->readbuffer_offset += part_size;
1849 ftdi->readbuffer_remaining = actual_length-part_size;
1850 offset += part_size;
1852 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1853 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1864 Configure read buffer chunk size.
1867 Automatically reallocates the buffer.
1869 \param ftdi pointer to ftdi_context
1870 \param chunksize Chunk size
1873 \retval -1: ftdi context invalid
1875 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1877 unsigned char *new_buf;
1880 ftdi_error_return(-1, "ftdi context invalid");
1882 // Invalidate all remaining data
1883 ftdi->readbuffer_offset = 0;
1884 ftdi->readbuffer_remaining = 0;
1886 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1887 which is defined in libusb-1.0. Otherwise, each USB read request will
1888 be divided into multiple URBs. This will cause issues on Linux kernel
1889 older than 2.6.32. */
1890 if (chunksize > 16384)
1894 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1895 ftdi_error_return(-1, "out of memory for readbuffer");
1897 ftdi->readbuffer = new_buf;
1898 ftdi->readbuffer_chunksize = chunksize;
1904 Get read buffer chunk size.
1906 \param ftdi pointer to ftdi_context
1907 \param chunksize Pointer to store chunk size in
1910 \retval -1: FTDI context invalid
1912 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1915 ftdi_error_return(-1, "FTDI context invalid");
1917 *chunksize = ftdi->readbuffer_chunksize;
1922 Enable/disable bitbang modes.
1924 \param ftdi pointer to ftdi_context
1925 \param bitmask Bitmask to configure lines.
1926 HIGH/ON value configures a line as output.
1927 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1930 \retval -1: can't enable bitbang mode
1931 \retval -2: USB device unavailable
1933 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1935 unsigned short usb_val;
1937 if (ftdi == NULL || ftdi->usb_dev == NULL)
1938 ftdi_error_return(-2, "USB device unavailable");
1940 usb_val = bitmask; // low byte: bitmask
1941 usb_val |= (mode << 8);
1942 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)
1943 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
1945 ftdi->bitbang_mode = mode;
1946 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1951 Disable bitbang mode.
1953 \param ftdi pointer to ftdi_context
1956 \retval -1: can't disable bitbang mode
1957 \retval -2: USB device unavailable
1959 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1961 if (ftdi == NULL || ftdi->usb_dev == NULL)
1962 ftdi_error_return(-2, "USB device unavailable");
1964 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)
1965 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1967 ftdi->bitbang_enabled = 0;
1973 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1975 \param ftdi pointer to ftdi_context
1976 \param pins Pointer to store pins into
1979 \retval -1: read pins failed
1980 \retval -2: USB device unavailable
1982 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1984 if (ftdi == NULL || ftdi->usb_dev == NULL)
1985 ftdi_error_return(-2, "USB device unavailable");
1987 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)
1988 ftdi_error_return(-1, "read pins failed");
1996 The FTDI chip keeps data in the internal buffer for a specific
1997 amount of time if the buffer is not full yet to decrease
1998 load on the usb bus.
2000 \param ftdi pointer to ftdi_context
2001 \param latency Value between 1 and 255
2004 \retval -1: latency out of range
2005 \retval -2: unable to set latency timer
2006 \retval -3: USB device unavailable
2008 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2010 unsigned short usb_val;
2013 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2015 if (ftdi == NULL || ftdi->usb_dev == NULL)
2016 ftdi_error_return(-3, "USB device unavailable");
2019 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)
2020 ftdi_error_return(-2, "unable to set latency timer");
2028 \param ftdi pointer to ftdi_context
2029 \param latency Pointer to store latency value in
2032 \retval -1: unable to get latency timer
2033 \retval -2: USB device unavailable
2035 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2037 unsigned short usb_val;
2039 if (ftdi == NULL || ftdi->usb_dev == NULL)
2040 ftdi_error_return(-2, "USB device unavailable");
2042 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)
2043 ftdi_error_return(-1, "reading latency timer failed");
2045 *latency = (unsigned char)usb_val;
2050 Poll modem status information
2052 This function allows the retrieve the two status bytes of the device.
2053 The device sends these bytes also as a header for each read access
2054 where they are discarded by ftdi_read_data(). The chip generates
2055 the two stripped status bytes in the absence of data every 40 ms.
2057 Layout of the first byte:
2058 - B0..B3 - must be 0
2059 - B4 Clear to send (CTS)
2062 - B5 Data set ready (DTS)
2065 - B6 Ring indicator (RI)
2068 - B7 Receive line signal detect (RLSD)
2072 Layout of the second byte:
2073 - B0 Data ready (DR)
2074 - B1 Overrun error (OE)
2075 - B2 Parity error (PE)
2076 - B3 Framing error (FE)
2077 - B4 Break interrupt (BI)
2078 - B5 Transmitter holding register (THRE)
2079 - B6 Transmitter empty (TEMT)
2080 - B7 Error in RCVR FIFO
2082 \param ftdi pointer to ftdi_context
2083 \param status Pointer to store status information in. Must be two bytes.
2086 \retval -1: unable to retrieve status information
2087 \retval -2: USB device unavailable
2089 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2093 if (ftdi == NULL || ftdi->usb_dev == NULL)
2094 ftdi_error_return(-2, "USB device unavailable");
2096 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
2097 ftdi_error_return(-1, "getting modem status failed");
2099 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2105 Set flowcontrol for ftdi chip
2107 \param ftdi pointer to ftdi_context
2108 \param flowctrl flow control to use. should be
2109 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2112 \retval -1: set flow control failed
2113 \retval -2: USB device unavailable
2115 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2117 if (ftdi == NULL || ftdi->usb_dev == NULL)
2118 ftdi_error_return(-2, "USB device unavailable");
2120 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2121 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2122 NULL, 0, ftdi->usb_write_timeout) < 0)
2123 ftdi_error_return(-1, "set flow control failed");
2131 \param ftdi pointer to ftdi_context
2132 \param state state to set line to (1 or 0)
2135 \retval -1: set dtr failed
2136 \retval -2: USB device unavailable
2138 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2140 unsigned short usb_val;
2142 if (ftdi == NULL || ftdi->usb_dev == NULL)
2143 ftdi_error_return(-2, "USB device unavailable");
2146 usb_val = SIO_SET_DTR_HIGH;
2148 usb_val = SIO_SET_DTR_LOW;
2150 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2151 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2152 NULL, 0, ftdi->usb_write_timeout) < 0)
2153 ftdi_error_return(-1, "set dtr failed");
2161 \param ftdi pointer to ftdi_context
2162 \param state state to set line to (1 or 0)
2165 \retval -1: set rts failed
2166 \retval -2: USB device unavailable
2168 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2170 unsigned short usb_val;
2172 if (ftdi == NULL || ftdi->usb_dev == NULL)
2173 ftdi_error_return(-2, "USB device unavailable");
2176 usb_val = SIO_SET_RTS_HIGH;
2178 usb_val = SIO_SET_RTS_LOW;
2180 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2181 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2182 NULL, 0, ftdi->usb_write_timeout) < 0)
2183 ftdi_error_return(-1, "set of rts failed");
2189 Set dtr and rts line in one pass
2191 \param ftdi pointer to ftdi_context
2192 \param dtr DTR state to set line to (1 or 0)
2193 \param rts RTS state to set line to (1 or 0)
2196 \retval -1: set dtr/rts failed
2197 \retval -2: USB device unavailable
2199 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2201 unsigned short usb_val;
2203 if (ftdi == NULL || ftdi->usb_dev == NULL)
2204 ftdi_error_return(-2, "USB device unavailable");
2207 usb_val = SIO_SET_DTR_HIGH;
2209 usb_val = SIO_SET_DTR_LOW;
2212 usb_val |= SIO_SET_RTS_HIGH;
2214 usb_val |= SIO_SET_RTS_LOW;
2216 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2217 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2218 NULL, 0, ftdi->usb_write_timeout) < 0)
2219 ftdi_error_return(-1, "set of rts/dtr failed");
2225 Set the special event character
2227 \param ftdi pointer to ftdi_context
2228 \param eventch Event character
2229 \param enable 0 to disable the event character, non-zero otherwise
2232 \retval -1: unable to set event character
2233 \retval -2: USB device unavailable
2235 int ftdi_set_event_char(struct ftdi_context *ftdi,
2236 unsigned char eventch, unsigned char enable)
2238 unsigned short usb_val;
2240 if (ftdi == NULL || ftdi->usb_dev == NULL)
2241 ftdi_error_return(-2, "USB device unavailable");
2247 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)
2248 ftdi_error_return(-1, "setting event character failed");
2256 \param ftdi pointer to ftdi_context
2257 \param errorch Error character
2258 \param enable 0 to disable the error character, non-zero otherwise
2261 \retval -1: unable to set error character
2262 \retval -2: USB device unavailable
2264 int ftdi_set_error_char(struct ftdi_context *ftdi,
2265 unsigned char errorch, unsigned char enable)
2267 unsigned short usb_val;
2269 if (ftdi == NULL || ftdi->usb_dev == NULL)
2270 ftdi_error_return(-2, "USB device unavailable");
2276 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)
2277 ftdi_error_return(-1, "setting error character failed");
2283 Init eeprom with default values for the connected device
2284 \param ftdi pointer to ftdi_context
2285 \param manufacturer String to use as Manufacturer
2286 \param product String to use as Product description
2287 \param serial String to use as Serial number description
2290 \retval -1: No struct ftdi_context
2291 \retval -2: No struct ftdi_eeprom
2292 \retval -3: No connected device or device not yet opened
2294 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2295 char * product, char * serial)
2297 struct ftdi_eeprom *eeprom;
2300 ftdi_error_return(-1, "No struct ftdi_context");
2302 if (ftdi->eeprom == NULL)
2303 ftdi_error_return(-2,"No struct ftdi_eeprom");
2305 eeprom = ftdi->eeprom;
2306 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2308 if (ftdi->usb_dev == NULL)
2309 ftdi_error_return(-3, "No connected device or device not yet opened");
2311 eeprom->vendor_id = 0x0403;
2312 eeprom->use_serial = 1;
2313 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2314 (ftdi->type == TYPE_R))
2315 eeprom->product_id = 0x6001;
2316 else if (ftdi->type == TYPE_4232H)
2317 eeprom->product_id = 0x6011;
2318 else if (ftdi->type == TYPE_232H)
2319 eeprom->product_id = 0x6014;
2320 else if (ftdi->type == TYPE_230X)
2321 eeprom->product_id = 0x6015;
2323 eeprom->product_id = 0x6010;
2325 if (ftdi->type == TYPE_AM)
2326 eeprom->usb_version = 0x0101;
2328 eeprom->usb_version = 0x0200;
2329 eeprom->max_power = 100;
2331 if (eeprom->manufacturer)
2332 free (eeprom->manufacturer);
2333 eeprom->manufacturer = NULL;
2336 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2337 if (eeprom->manufacturer)
2338 strcpy(eeprom->manufacturer, manufacturer);
2341 if (eeprom->product)
2342 free (eeprom->product);
2343 eeprom->product = NULL;
2346 eeprom->product = malloc(strlen(product)+1);
2347 if (eeprom->product)
2348 strcpy(eeprom->product, product);
2352 const char* default_product;
2355 case TYPE_AM: default_product = "AM"; break;
2356 case TYPE_BM: default_product = "BM"; break;
2357 case TYPE_2232C: default_product = "Dual RS232"; break;
2358 case TYPE_R: default_product = "FT232R USB UART"; break;
2359 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2360 case TYPE_4232H: default_product = "FT4232H"; break;
2361 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2362 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2364 ftdi_error_return(-3, "Unknown chip type");
2366 eeprom->product = malloc(strlen(default_product) +1);
2367 if (eeprom->product)
2368 strcpy(eeprom->product, default_product);
2372 free (eeprom->serial);
2373 eeprom->serial = NULL;
2376 eeprom->serial = malloc(strlen(serial)+1);
2378 strcpy(eeprom->serial, serial);
2381 if (ftdi->type == TYPE_R)
2383 eeprom->max_power = 90;
2384 eeprom->size = 0x80;
2385 eeprom->cbus_function[0] = CBUS_TXLED;
2386 eeprom->cbus_function[1] = CBUS_RXLED;
2387 eeprom->cbus_function[2] = CBUS_TXDEN;
2388 eeprom->cbus_function[3] = CBUS_PWREN;
2389 eeprom->cbus_function[4] = CBUS_SLEEP;
2391 else if (ftdi->type == TYPE_230X)
2393 eeprom->max_power = 90;
2394 eeprom->size = 0x100;
2395 eeprom->cbus_function[0] = CBUSX_TXDEN;
2396 eeprom->cbus_function[1] = CBUSX_RXLED;
2397 eeprom->cbus_function[2] = CBUSX_TXLED;
2398 eeprom->cbus_function[3] = CBUSX_SLEEP;
2402 if(ftdi->type == TYPE_232H)
2405 for (i=0; i<10; i++)
2406 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2413 eeprom->release_number = 0x0200;
2416 eeprom->release_number = 0x0400;
2419 eeprom->release_number = 0x0500;
2422 eeprom->release_number = 0x0600;
2425 eeprom->release_number = 0x0700;
2428 eeprom->release_number = 0x0800;
2431 eeprom->release_number = 0x0900;
2434 eeprom->release_number = 0x1000;
2437 eeprom->release_number = 0x00;
2442 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2443 char * product, char * serial)
2445 struct ftdi_eeprom *eeprom;
2448 ftdi_error_return(-1, "No struct ftdi_context");
2450 if (ftdi->eeprom == NULL)
2451 ftdi_error_return(-2,"No struct ftdi_eeprom");
2453 eeprom = ftdi->eeprom;
2455 if (ftdi->usb_dev == NULL)
2456 ftdi_error_return(-3, "No connected device or device not yet opened");
2460 if (eeprom->manufacturer)
2461 free (eeprom->manufacturer);
2462 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2463 if (eeprom->manufacturer)
2464 strcpy(eeprom->manufacturer, manufacturer);
2469 if (eeprom->product)
2470 free (eeprom->product);
2471 eeprom->product = malloc(strlen(product)+1);
2472 if (eeprom->product)
2473 strcpy(eeprom->product, product);
2479 free (eeprom->serial);
2480 eeprom->serial = malloc(strlen(serial)+1);
2483 strcpy(eeprom->serial, serial);
2484 eeprom->use_serial = 1;
2491 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2492 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2497 int mode_low, mode_high;
2498 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2499 mode_low = CBUSH_TRISTATE;
2501 mode_low = eeprom->cbus_function[2*i];
2502 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2503 mode_high = CBUSH_TRISTATE;
2505 mode_high = eeprom->cbus_function[2*i+1];
2507 output[0x18+i] = (mode_high <<4) | mode_low;
2510 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2513 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2522 case CHANNEL_IS_UART: return 0;
2523 case CHANNEL_IS_FIFO: return 0x01;
2524 case CHANNEL_IS_OPTO: return 0x02;
2525 case CHANNEL_IS_CPU : return 0x04;
2533 case CHANNEL_IS_UART : return 0;
2534 case CHANNEL_IS_FIFO : return 0x01;
2535 case CHANNEL_IS_OPTO : return 0x02;
2536 case CHANNEL_IS_CPU : return 0x04;
2537 case CHANNEL_IS_FT1284 : return 0x08;
2541 case TYPE_230X: /* FT230X is only UART */
2548 Build binary buffer from ftdi_eeprom structure.
2549 Output is suitable for ftdi_write_eeprom().
2551 \param ftdi pointer to ftdi_context
2553 \retval >=0: size of eeprom user area in bytes
2554 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2555 \retval -2: Invalid eeprom or ftdi pointer
2556 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2557 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2558 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2559 \retval -6: No connected EEPROM or EEPROM Type unknown
2561 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2563 unsigned char i, j, eeprom_size_mask;
2564 unsigned short checksum, value;
2565 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2567 struct ftdi_eeprom *eeprom;
2568 unsigned char * output;
2571 ftdi_error_return(-2,"No context");
2572 if (ftdi->eeprom == NULL)
2573 ftdi_error_return(-2,"No eeprom structure");
2575 eeprom= ftdi->eeprom;
2576 output = eeprom->buf;
2578 if (eeprom->chip == -1)
2579 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2581 if (eeprom->size == -1)
2583 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2584 eeprom->size = 0x100;
2586 eeprom->size = 0x80;
2589 if (eeprom->manufacturer != NULL)
2590 manufacturer_size = strlen(eeprom->manufacturer);
2591 if (eeprom->product != NULL)
2592 product_size = strlen(eeprom->product);
2593 if (eeprom->serial != NULL)
2594 serial_size = strlen(eeprom->serial);
2596 // eeprom size check
2601 user_area_size = 96; // base size for strings (total of 48 characters)
2604 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2607 user_area_size = 96;
2610 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2612 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2614 user_area_size = 86;
2617 user_area_size = 80;
2623 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2625 if (user_area_size < 0)
2626 ftdi_error_return(-1,"eeprom size exceeded");
2629 if (ftdi->type == TYPE_230X)
2631 /* FT230X have a reserved section in the middle of the MTP,
2632 which cannot be written to, but must be included in the checksum */
2633 memset(ftdi->eeprom->buf, 0, 0x80);
2634 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2638 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2641 // Bytes and Bits set for all Types
2643 // Addr 02: Vendor ID
2644 output[0x02] = eeprom->vendor_id;
2645 output[0x03] = eeprom->vendor_id >> 8;
2647 // Addr 04: Product ID
2648 output[0x04] = eeprom->product_id;
2649 output[0x05] = eeprom->product_id >> 8;
2651 // Addr 06: Device release number (0400h for BM features)
2652 output[0x06] = eeprom->release_number;
2653 output[0x07] = eeprom->release_number >> 8;
2655 // Addr 08: Config descriptor
2657 // Bit 6: 1 if this device is self powered, 0 if bus powered
2658 // Bit 5: 1 if this device uses remote wakeup
2659 // Bit 4-0: reserved - 0
2661 if (eeprom->self_powered)
2663 if (eeprom->remote_wakeup)
2667 // Addr 09: Max power consumption: max power = value * 2 mA
2668 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2670 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2672 // Addr 0A: Chip configuration
2673 // Bit 7: 0 - reserved
2674 // Bit 6: 0 - reserved
2675 // Bit 5: 0 - reserved
2676 // Bit 4: 1 - Change USB version
2677 // Bit 3: 1 - Use the serial number string
2678 // Bit 2: 1 - Enable suspend pull downs for lower power
2679 // Bit 1: 1 - Out EndPoint is Isochronous
2680 // Bit 0: 1 - In EndPoint is Isochronous
2683 if (eeprom->in_is_isochronous)
2685 if (eeprom->out_is_isochronous)
2691 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2692 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2713 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2714 eeprom_size_mask = eeprom->size -1;
2716 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2717 // Addr 0F: Length of manufacturer string
2718 // Output manufacturer
2719 output[0x0E] = i; // calculate offset
2720 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2721 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2722 for (j = 0; j < manufacturer_size; j++)
2724 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2725 output[i & eeprom_size_mask] = 0x00, i++;
2727 output[0x0F] = manufacturer_size*2 + 2;
2729 // Addr 10: Offset of the product string + 0x80, calculated later
2730 // Addr 11: Length of product string
2731 output[0x10] = i | 0x80; // calculate offset
2732 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2733 output[i & eeprom_size_mask] = 0x03, i++;
2734 for (j = 0; j < product_size; j++)
2736 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2737 output[i & eeprom_size_mask] = 0x00, i++;
2739 output[0x11] = product_size*2 + 2;
2741 // Addr 12: Offset of the serial string + 0x80, calculated later
2742 // Addr 13: Length of serial string
2743 output[0x12] = i | 0x80; // calculate offset
2744 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2745 output[i & eeprom_size_mask] = 0x03, i++;
2746 for (j = 0; j < serial_size; j++)
2748 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2749 output[i & eeprom_size_mask] = 0x00, i++;
2752 // Legacy port name and PnP fields for FT2232 and newer chips
2753 if (ftdi->type > TYPE_BM)
2755 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2757 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2759 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2763 output[0x13] = serial_size*2 + 2;
2765 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2767 if (eeprom->use_serial)
2768 output[0x0A] |= USE_SERIAL_NUM;
2770 output[0x0A] &= ~USE_SERIAL_NUM;
2773 /* Bytes and Bits specific to (some) types
2774 Write linear, as this allows easier fixing*/
2780 output[0x0C] = eeprom->usb_version & 0xff;
2781 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2782 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2783 output[0x0A] |= USE_USB_VERSION_BIT;
2785 output[0x0A] &= ~USE_USB_VERSION_BIT;
2790 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
2791 if ( eeprom->channel_a_driver == DRIVER_VCP)
2792 output[0x00] |= DRIVER_VCP;
2794 output[0x00] &= ~DRIVER_VCP;
2796 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2797 output[0x00] |= HIGH_CURRENT_DRIVE;
2799 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2801 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
2802 if ( eeprom->channel_b_driver == DRIVER_VCP)
2803 output[0x01] |= DRIVER_VCP;
2805 output[0x01] &= ~DRIVER_VCP;
2807 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2808 output[0x01] |= HIGH_CURRENT_DRIVE;
2810 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2812 if (eeprom->in_is_isochronous)
2813 output[0x0A] |= 0x1;
2815 output[0x0A] &= ~0x1;
2816 if (eeprom->out_is_isochronous)
2817 output[0x0A] |= 0x2;
2819 output[0x0A] &= ~0x2;
2820 if (eeprom->suspend_pull_downs)
2821 output[0x0A] |= 0x4;
2823 output[0x0A] &= ~0x4;
2824 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2825 output[0x0A] |= USE_USB_VERSION_BIT;
2827 output[0x0A] &= ~USE_USB_VERSION_BIT;
2829 output[0x0C] = eeprom->usb_version & 0xff;
2830 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2831 output[0x14] = eeprom->chip;
2834 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2835 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2836 if (eeprom->external_oscillator)
2837 output[0x00] |= 0x02;
2838 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2840 if (eeprom->suspend_pull_downs)
2841 output[0x0A] |= 0x4;
2843 output[0x0A] &= ~0x4;
2844 output[0x0B] = eeprom->invert;
2845 output[0x0C] = eeprom->usb_version & 0xff;
2846 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2848 if (eeprom->cbus_function[0] > CBUS_BB_RD)
2849 output[0x14] = CBUS_TXLED;
2851 output[0x14] = eeprom->cbus_function[0];
2853 if (eeprom->cbus_function[1] > CBUS_BB_RD)
2854 output[0x14] |= CBUS_RXLED<<4;
2856 output[0x14] |= eeprom->cbus_function[1]<<4;
2858 if (eeprom->cbus_function[2] > CBUS_BB_RD)
2859 output[0x15] = CBUS_TXDEN;
2861 output[0x15] = eeprom->cbus_function[2];
2863 if (eeprom->cbus_function[3] > CBUS_BB_RD)
2864 output[0x15] |= CBUS_PWREN<<4;
2866 output[0x15] |= eeprom->cbus_function[3]<<4;
2868 if (eeprom->cbus_function[4] > CBUS_CLK6)
2869 output[0x16] = CBUS_SLEEP;
2871 output[0x16] = eeprom->cbus_function[4];
2874 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
2875 if ( eeprom->channel_a_driver == DRIVER_VCP)
2876 output[0x00] |= DRIVER_VCP;
2878 output[0x00] &= ~DRIVER_VCP;
2880 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
2881 if ( eeprom->channel_b_driver == DRIVER_VCP)
2882 output[0x01] |= DRIVER_VCP;
2884 output[0x01] &= ~DRIVER_VCP;
2885 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2886 output[0x01] |= SUSPEND_DBUS7_BIT;
2888 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2890 if (eeprom->suspend_pull_downs)
2891 output[0x0A] |= 0x4;
2893 output[0x0A] &= ~0x4;
2895 if (eeprom->group0_drive > DRIVE_16MA)
2896 output[0x0c] |= DRIVE_16MA;
2898 output[0x0c] |= eeprom->group0_drive;
2899 if (eeprom->group0_schmitt == IS_SCHMITT)
2900 output[0x0c] |= IS_SCHMITT;
2901 if (eeprom->group0_slew == SLOW_SLEW)
2902 output[0x0c] |= SLOW_SLEW;
2904 if (eeprom->group1_drive > DRIVE_16MA)
2905 output[0x0c] |= DRIVE_16MA<<4;
2907 output[0x0c] |= eeprom->group1_drive<<4;
2908 if (eeprom->group1_schmitt == IS_SCHMITT)
2909 output[0x0c] |= IS_SCHMITT<<4;
2910 if (eeprom->group1_slew == SLOW_SLEW)
2911 output[0x0c] |= SLOW_SLEW<<4;
2913 if (eeprom->group2_drive > DRIVE_16MA)
2914 output[0x0d] |= DRIVE_16MA;
2916 output[0x0d] |= eeprom->group2_drive;
2917 if (eeprom->group2_schmitt == IS_SCHMITT)
2918 output[0x0d] |= IS_SCHMITT;
2919 if (eeprom->group2_slew == SLOW_SLEW)
2920 output[0x0d] |= SLOW_SLEW;
2922 if (eeprom->group3_drive > DRIVE_16MA)
2923 output[0x0d] |= DRIVE_16MA<<4;
2925 output[0x0d] |= eeprom->group3_drive<<4;
2926 if (eeprom->group3_schmitt == IS_SCHMITT)
2927 output[0x0d] |= IS_SCHMITT<<4;
2928 if (eeprom->group3_slew == SLOW_SLEW)
2929 output[0x0d] |= SLOW_SLEW<<4;
2931 output[0x18] = eeprom->chip;
2935 if (eeprom->channel_a_driver == DRIVER_VCP)
2936 output[0x00] |= DRIVER_VCP;
2938 output[0x00] &= ~DRIVER_VCP;
2939 if (eeprom->channel_b_driver == DRIVER_VCP)
2940 output[0x01] |= DRIVER_VCP;
2942 output[0x01] &= ~DRIVER_VCP;
2943 if (eeprom->channel_c_driver == DRIVER_VCP)
2944 output[0x00] |= (DRIVER_VCP << 4);
2946 output[0x00] &= ~(DRIVER_VCP << 4);
2947 if (eeprom->channel_d_driver == DRIVER_VCP)
2948 output[0x01] |= (DRIVER_VCP << 4);
2950 output[0x01] &= ~(DRIVER_VCP << 4);
2952 if (eeprom->suspend_pull_downs)
2953 output[0x0a] |= 0x4;
2955 output[0x0a] &= ~0x4;
2957 if (eeprom->channel_a_rs485enable)
2958 output[0x0b] |= CHANNEL_IS_RS485 << 0;
2960 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
2961 if (eeprom->channel_b_rs485enable)
2962 output[0x0b] |= CHANNEL_IS_RS485 << 1;
2964 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
2965 if (eeprom->channel_c_rs485enable)
2966 output[0x0b] |= CHANNEL_IS_RS485 << 2;
2968 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
2969 if (eeprom->channel_d_rs485enable)
2970 output[0x0b] |= CHANNEL_IS_RS485 << 3;
2972 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
2974 if (eeprom->group0_drive > DRIVE_16MA)
2975 output[0x0c] |= DRIVE_16MA;
2977 output[0x0c] |= eeprom->group0_drive;
2978 if (eeprom->group0_schmitt == IS_SCHMITT)
2979 output[0x0c] |= IS_SCHMITT;
2980 if (eeprom->group0_slew == SLOW_SLEW)
2981 output[0x0c] |= SLOW_SLEW;
2983 if (eeprom->group1_drive > DRIVE_16MA)
2984 output[0x0c] |= DRIVE_16MA<<4;
2986 output[0x0c] |= eeprom->group1_drive<<4;
2987 if (eeprom->group1_schmitt == IS_SCHMITT)
2988 output[0x0c] |= IS_SCHMITT<<4;
2989 if (eeprom->group1_slew == SLOW_SLEW)
2990 output[0x0c] |= SLOW_SLEW<<4;
2992 if (eeprom->group2_drive > DRIVE_16MA)
2993 output[0x0d] |= DRIVE_16MA;
2995 output[0x0d] |= eeprom->group2_drive;
2996 if (eeprom->group2_schmitt == IS_SCHMITT)
2997 output[0x0d] |= IS_SCHMITT;
2998 if (eeprom->group2_slew == SLOW_SLEW)
2999 output[0x0d] |= SLOW_SLEW;
3001 if (eeprom->group3_drive > DRIVE_16MA)
3002 output[0x0d] |= DRIVE_16MA<<4;
3004 output[0x0d] |= eeprom->group3_drive<<4;
3005 if (eeprom->group3_schmitt == IS_SCHMITT)
3006 output[0x0d] |= IS_SCHMITT<<4;
3007 if (eeprom->group3_slew == SLOW_SLEW)
3008 output[0x0d] |= SLOW_SLEW<<4;
3010 output[0x18] = eeprom->chip;
3014 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3015 if ( eeprom->channel_a_driver == DRIVER_VCP)
3016 output[0x00] |= DRIVER_VCPH;
3018 output[0x00] &= ~DRIVER_VCPH;
3019 if (eeprom->powersave)
3020 output[0x01] |= POWER_SAVE_DISABLE_H;
3022 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3024 if (eeprom->suspend_pull_downs)
3025 output[0x0a] |= 0x4;
3027 output[0x0a] &= ~0x4;
3029 if (eeprom->clock_polarity)
3030 output[0x01] |= FT1284_CLK_IDLE_STATE;
3032 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3033 if (eeprom->data_order)
3034 output[0x01] |= FT1284_DATA_LSB;
3036 output[0x01] &= ~FT1284_DATA_LSB;
3037 if (eeprom->flow_control)
3038 output[0x01] |= FT1284_FLOW_CONTROL;
3040 output[0x01] &= ~FT1284_FLOW_CONTROL;
3041 if (eeprom->group0_drive > DRIVE_16MA)
3042 output[0x0c] |= DRIVE_16MA;
3044 output[0x0c] |= eeprom->group0_drive;
3045 if (eeprom->group0_schmitt == IS_SCHMITT)
3046 output[0x0c] |= IS_SCHMITT;
3047 if (eeprom->group0_slew == SLOW_SLEW)
3048 output[0x0c] |= SLOW_SLEW;
3050 if (eeprom->group1_drive > DRIVE_16MA)
3051 output[0x0d] |= DRIVE_16MA;
3053 output[0x0d] |= eeprom->group1_drive;
3054 if (eeprom->group1_schmitt == IS_SCHMITT)
3055 output[0x0d] |= IS_SCHMITT;
3056 if (eeprom->group1_slew == SLOW_SLEW)
3057 output[0x0d] |= SLOW_SLEW;
3059 set_ft232h_cbus(eeprom, output);
3061 output[0x1e] = eeprom->chip;
3062 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3065 output[0x00] = 0x80; /* Actually, leave the default value */
3066 output[0x0a] = 0x08; /* Enable USB Serial Number */
3067 /*FIXME: Make DBUS & CBUS Control configurable*/
3068 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3069 for (j = 0; j <= 6; j++)
3071 output[0x1a + j] = eeprom->cbus_function[j];
3073 output[0x0b] = eeprom->invert;
3077 // calculate checksum
3080 for (i = 0; i < eeprom->size/2-1; i++)
3082 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3084 /* FT230X has a user section in the MTP which is not part of the checksum */
3087 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3089 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3090 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3096 value = output[i*2];
3097 value += output[(i*2)+1] << 8;
3099 checksum = value^checksum;
3100 checksum = (checksum << 1) | (checksum >> 15);
3103 output[eeprom->size-2] = checksum;
3104 output[eeprom->size-1] = checksum >> 8;
3106 eeprom->initialized_for_connected_device = 1;
3107 return user_area_size;
3109 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3112 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3114 static unsigned char bit2type(unsigned char bits)
3118 case 0: return CHANNEL_IS_UART;
3119 case 1: return CHANNEL_IS_FIFO;
3120 case 2: return CHANNEL_IS_OPTO;
3121 case 4: return CHANNEL_IS_CPU;
3122 case 8: return CHANNEL_IS_FT1284;
3124 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3129 /* Decode 230X / 232R type chips invert bits
3130 * Prints directly to stdout.
3132 static void print_inverted_bits(int invert)
3134 char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3137 fprintf(stdout,"Inverted bits:");
3139 if ((invert & (1<<i)) == (1<<i))
3140 fprintf(stdout," %s",r_bits[i]);
3142 fprintf(stdout,"\n");
3145 Decode binary EEPROM image into an ftdi_eeprom structure.
3147 For FT-X devices use AN_201 FT-X MTP memory Configuration to decode.
3149 \param ftdi pointer to ftdi_context
3150 \param verbose Decode EEPROM on stdout
3153 \retval -1: something went wrong
3155 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3156 FIXME: Strings are malloc'ed here and should be freed somewhere
3158 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3161 unsigned short checksum, eeprom_checksum, value;
3162 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3164 struct ftdi_eeprom *eeprom;
3165 unsigned char *buf = NULL;
3168 ftdi_error_return(-1,"No context");
3169 if (ftdi->eeprom == NULL)
3170 ftdi_error_return(-1,"No eeprom structure");
3172 eeprom = ftdi->eeprom;
3173 eeprom_size = eeprom->size;
3174 buf = ftdi->eeprom->buf;
3176 // Addr 02: Vendor ID
3177 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3179 // Addr 04: Product ID
3180 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3182 // Addr 06: Device release number
3183 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3185 // Addr 08: Config descriptor
3187 // Bit 6: 1 if this device is self powered, 0 if bus powered
3188 // Bit 5: 1 if this device uses remote wakeup
3189 eeprom->self_powered = buf[0x08] & 0x40;
3190 eeprom->remote_wakeup = buf[0x08] & 0x20;
3192 // Addr 09: Max power consumption: max power = value * 2 mA
3193 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3195 // Addr 0A: Chip configuration
3196 // Bit 7: 0 - reserved
3197 // Bit 6: 0 - reserved
3198 // Bit 5: 0 - reserved
3199 // Bit 4: 1 - Change USB version on BM and 2232C
3200 // Bit 3: 1 - Use the serial number string
3201 // Bit 2: 1 - Enable suspend pull downs for lower power
3202 // Bit 1: 1 - Out EndPoint is Isochronous
3203 // Bit 0: 1 - In EndPoint is Isochronous
3205 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3206 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3207 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3208 eeprom->use_serial = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
3209 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
3211 // Addr 0C: USB version low byte when 0x0A
3212 // Addr 0D: USB version high byte when 0x0A
3213 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3215 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3216 // Addr 0F: Length of manufacturer string
3217 manufacturer_size = buf[0x0F]/2;
3218 if (eeprom->manufacturer)
3219 free(eeprom->manufacturer);
3220 if (manufacturer_size > 0)
3222 eeprom->manufacturer = malloc(manufacturer_size);
3223 if (eeprom->manufacturer)
3225 // Decode manufacturer
3226 i = buf[0x0E] & (eeprom_size -1); // offset
3227 for (j=0; j<manufacturer_size-1; j++)
3229 eeprom->manufacturer[j] = buf[2*j+i+2];
3231 eeprom->manufacturer[j] = '\0';
3234 else eeprom->manufacturer = NULL;
3236 // Addr 10: Offset of the product string + 0x80, calculated later
3237 // Addr 11: Length of product string
3238 if (eeprom->product)
3239 free(eeprom->product);
3240 product_size = buf[0x11]/2;
3241 if (product_size > 0)
3243 eeprom->product = malloc(product_size);
3244 if (eeprom->product)
3246 // Decode product name
3247 i = buf[0x10] & (eeprom_size -1); // offset
3248 for (j=0; j<product_size-1; j++)
3250 eeprom->product[j] = buf[2*j+i+2];
3252 eeprom->product[j] = '\0';
3255 else eeprom->product = NULL;
3257 // Addr 12: Offset of the serial string + 0x80, calculated later
3258 // Addr 13: Length of serial string
3260 free(eeprom->serial);
3261 serial_size = buf[0x13]/2;
3262 if (serial_size > 0)
3264 eeprom->serial = malloc(serial_size);
3268 i = buf[0x12] & (eeprom_size -1); // offset
3269 for (j=0; j<serial_size-1; j++)
3271 eeprom->serial[j] = buf[2*j+i+2];
3273 eeprom->serial[j] = '\0';
3276 else eeprom->serial = NULL;
3281 for (i = 0; i < eeprom_size/2-1; i++)
3283 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3285 /* FT230X has a user section in the MTP which is not part of the checksum */
3289 value += buf[(i*2)+1] << 8;
3291 checksum = value^checksum;
3292 checksum = (checksum << 1) | (checksum >> 15);
3295 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3297 if (eeprom_checksum != checksum)
3299 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3300 ftdi_error_return(-1,"EEPROM checksum error");
3303 eeprom->channel_a_type = 0;
3304 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3308 else if (ftdi->type == TYPE_2232C)
3310 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3311 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3312 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3313 eeprom->channel_b_type = buf[0x01] & 0x7;
3314 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3315 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3316 eeprom->chip = buf[0x14];
3318 else if (ftdi->type == TYPE_R)
3320 /* TYPE_R flags D2XX, not VCP as all others*/
3321 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3322 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3323 eeprom->external_oscillator = buf[0x00] & 0x02;
3324 if ( (buf[0x01]&0x40) != 0x40)
3326 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3327 " If this happened with the\n"
3328 " EEPROM programmed by FTDI tools, please report "
3329 "to libftdi@developer.intra2net.com\n");
3331 eeprom->chip = buf[0x16];
3332 // Addr 0B: Invert data lines
3333 // Works only on FT232R, not FT245R, but no way to distinguish
3334 eeprom->invert = buf[0x0B];
3335 // Addr 14: CBUS function: CBUS0, CBUS1
3336 // Addr 15: CBUS function: CBUS2, CBUS3
3337 // Addr 16: CBUS function: CBUS5
3338 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3339 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3340 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3341 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3342 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3344 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3346 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3347 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3349 if (ftdi->type == TYPE_2232H)
3351 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3352 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3353 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3357 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3358 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3359 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3360 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3361 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3362 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3365 eeprom->chip = buf[0x18];
3366 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3367 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3368 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3369 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3370 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3371 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3372 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3373 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3374 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3375 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3376 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3377 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3379 else if (ftdi->type == TYPE_232H)
3381 eeprom->channel_a_type = buf[0x00] & 0xf;
3382 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3383 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3384 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3385 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3386 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3387 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3388 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3389 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3390 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3391 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3392 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3396 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3397 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3399 eeprom->chip = buf[0x1e];
3400 /*FIXME: Decipher more values*/
3402 else if (ftdi->type == TYPE_230X)
3406 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3408 eeprom->group0_drive = buf[0x0c] & 0x03;
3409 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3410 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3411 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3412 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3413 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3415 eeprom->invert = buf[0xb];
3420 char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3421 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3422 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3423 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3425 if (eeprom->self_powered)
3426 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3428 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3429 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3430 if (eeprom->manufacturer)
3431 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3432 if (eeprom->product)
3433 fprintf(stdout, "Product: %s\n",eeprom->product);
3435 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3436 fprintf(stdout, "Checksum : %04x\n", checksum);
3437 if (ftdi->type == TYPE_R) {
3438 fprintf(stdout, "Internal EEPROM\n");
3439 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3441 else if (eeprom->chip >= 0x46)
3442 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3443 if (eeprom->suspend_dbus7)
3444 fprintf(stdout, "Suspend on DBUS7\n");
3445 if (eeprom->suspend_pull_downs)
3446 fprintf(stdout, "Pull IO pins low during suspend\n");
3447 if(eeprom->powersave)
3449 if(ftdi->type >= TYPE_232H)
3450 fprintf(stdout,"Enter low power state on ACBUS7\n");
3452 if (eeprom->remote_wakeup)
3453 fprintf(stdout, "Enable Remote Wake Up\n");
3454 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3455 if (ftdi->type >= TYPE_2232C)
3456 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3457 channel_mode[eeprom->channel_a_type],
3458 (eeprom->channel_a_driver)?" VCP":"",
3459 (eeprom->high_current_a)?" High Current IO":"");
3460 if (ftdi->type == TYPE_232H)
3462 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3463 (eeprom->clock_polarity)?"HIGH":"LOW",
3464 (eeprom->data_order)?"LSB":"MSB",
3465 (eeprom->flow_control)?"":"No ");
3467 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3468 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3469 channel_mode[eeprom->channel_b_type],
3470 (eeprom->channel_b_driver)?" VCP":"",
3471 (eeprom->high_current_b)?" High Current IO":"");
3472 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3473 eeprom->use_usb_version == USE_USB_VERSION_BIT)
3474 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3476 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3478 fprintf(stdout,"%s has %d mA drive%s%s\n",
3479 (ftdi->type == TYPE_2232H)?"AL":"A",
3480 (eeprom->group0_drive+1) *4,
3481 (eeprom->group0_schmitt)?" Schmitt Input":"",
3482 (eeprom->group0_slew)?" Slow Slew":"");
3483 fprintf(stdout,"%s has %d mA drive%s%s\n",
3484 (ftdi->type == TYPE_2232H)?"AH":"B",
3485 (eeprom->group1_drive+1) *4,
3486 (eeprom->group1_schmitt)?" Schmitt Input":"",
3487 (eeprom->group1_slew)?" Slow Slew":"");
3488 fprintf(stdout,"%s has %d mA drive%s%s\n",
3489 (ftdi->type == TYPE_2232H)?"BL":"C",
3490 (eeprom->group2_drive+1) *4,
3491 (eeprom->group2_schmitt)?" Schmitt Input":"",
3492 (eeprom->group2_slew)?" Slow Slew":"");
3493 fprintf(stdout,"%s has %d mA drive%s%s\n",
3494 (ftdi->type == TYPE_2232H)?"BH":"D",
3495 (eeprom->group3_drive+1) *4,
3496 (eeprom->group3_schmitt)?" Schmitt Input":"",
3497 (eeprom->group3_slew)?" Slow Slew":"");
3499 else if (ftdi->type == TYPE_232H)
3501 char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3502 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3503 "CLK30","CLK15","CLK7_5"
3505 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3506 (eeprom->group0_drive+1) *4,
3507 (eeprom->group0_schmitt)?" Schmitt Input":"",
3508 (eeprom->group0_slew)?" Slow Slew":"");
3509 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3510 (eeprom->group1_drive+1) *4,
3511 (eeprom->group1_schmitt)?" Schmitt Input":"",
3512 (eeprom->group1_slew)?" Slow Slew":"");
3513 for (i=0; i<10; i++)
3515 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3516 fprintf(stdout,"C%d Function: %s\n", i,
3517 cbush_mux[eeprom->cbus_function[i]]);
3520 else if (ftdi->type == TYPE_230X)
3522 char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3523 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3524 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3525 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3526 "BBRD#", "TIME_STAMP", "AWAKE#",
3528 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3529 (eeprom->group0_drive+1) *4,
3530 (eeprom->group0_schmitt)?" Schmitt Input":"",
3531 (eeprom->group0_slew)?" Slow Slew":"");
3532 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3533 (eeprom->group1_drive+1) *4,
3534 (eeprom->group1_schmitt)?" Schmitt Input":"",
3535 (eeprom->group1_slew)?" Slow Slew":"");
3538 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3539 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3543 print_inverted_bits(eeprom->invert);
3546 if (ftdi->type == TYPE_R)
3548 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3549 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3550 "IOMODE","BB_WR","BB_RD"
3552 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3555 print_inverted_bits(eeprom->invert);
3559 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3560 fprintf(stdout,"C%d Function: %s\n", i,
3561 cbus_mux[eeprom->cbus_function[i]]);
3565 /* Running MPROG show that C0..3 have fixed function Synchronous
3567 fprintf(stdout,"C%d BB Function: %s\n", i,
3570 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3579 Get a value from the decoded EEPROM structure
3581 \param ftdi pointer to ftdi_context
3582 \param value_name Enum of the value to query
3583 \param value Pointer to store read value
3586 \retval -1: Value doesn't exist
3588 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3593 *value = ftdi->eeprom->vendor_id;
3596 *value = ftdi->eeprom->product_id;
3598 case RELEASE_NUMBER:
3599 *value = ftdi->eeprom->release_number;
3602 *value = ftdi->eeprom->self_powered;
3605 *value = ftdi->eeprom->remote_wakeup;
3608 *value = ftdi->eeprom->is_not_pnp;
3611 *value = ftdi->eeprom->suspend_dbus7;
3613 case IN_IS_ISOCHRONOUS:
3614 *value = ftdi->eeprom->in_is_isochronous;
3616 case OUT_IS_ISOCHRONOUS:
3617 *value = ftdi->eeprom->out_is_isochronous;
3619 case SUSPEND_PULL_DOWNS:
3620 *value = ftdi->eeprom->suspend_pull_downs;
3623 *value = ftdi->eeprom->use_serial;
3626 *value = ftdi->eeprom->usb_version;
3628 case USE_USB_VERSION:
3629 *value = ftdi->eeprom->use_usb_version;
3632 *value = ftdi->eeprom->max_power;
3634 case CHANNEL_A_TYPE:
3635 *value = ftdi->eeprom->channel_a_type;
3637 case CHANNEL_B_TYPE:
3638 *value = ftdi->eeprom->channel_b_type;
3640 case CHANNEL_A_DRIVER:
3641 *value = ftdi->eeprom->channel_a_driver;
3643 case CHANNEL_B_DRIVER:
3644 *value = ftdi->eeprom->channel_b_driver;
3646 case CHANNEL_C_DRIVER:
3647 *value = ftdi->eeprom->channel_c_driver;
3649 case CHANNEL_D_DRIVER:
3650 *value = ftdi->eeprom->channel_d_driver;
3652 case CHANNEL_A_RS485:
3653 *value = ftdi->eeprom->channel_a_rs485enable;
3655 case CHANNEL_B_RS485:
3656 *value = ftdi->eeprom->channel_b_rs485enable;
3658 case CHANNEL_C_RS485:
3659 *value = ftdi->eeprom->channel_c_rs485enable;
3661 case CHANNEL_D_RS485:
3662 *value = ftdi->eeprom->channel_d_rs485enable;
3664 case CBUS_FUNCTION_0:
3665 *value = ftdi->eeprom->cbus_function[0];
3667 case CBUS_FUNCTION_1:
3668 *value = ftdi->eeprom->cbus_function[1];
3670 case CBUS_FUNCTION_2:
3671 *value = ftdi->eeprom->cbus_function[2];
3673 case CBUS_FUNCTION_3:
3674 *value = ftdi->eeprom->cbus_function[3];
3676 case CBUS_FUNCTION_4:
3677 *value = ftdi->eeprom->cbus_function[4];
3679 case CBUS_FUNCTION_5:
3680 *value = ftdi->eeprom->cbus_function[5];
3682 case CBUS_FUNCTION_6:
3683 *value = ftdi->eeprom->cbus_function[6];
3685 case CBUS_FUNCTION_7:
3686 *value = ftdi->eeprom->cbus_function[7];
3688 case CBUS_FUNCTION_8:
3689 *value = ftdi->eeprom->cbus_function[8];
3691 case CBUS_FUNCTION_9:
3692 *value = ftdi->eeprom->cbus_function[9];
3695 *value = ftdi->eeprom->high_current;
3697 case HIGH_CURRENT_A:
3698 *value = ftdi->eeprom->high_current_a;
3700 case HIGH_CURRENT_B:
3701 *value = ftdi->eeprom->high_current_b;
3704 *value = ftdi->eeprom->invert;
3707 *value = ftdi->eeprom->group0_drive;
3709 case GROUP0_SCHMITT:
3710 *value = ftdi->eeprom->group0_schmitt;
3713 *value = ftdi->eeprom->group0_slew;
3716 *value = ftdi->eeprom->group1_drive;
3718 case GROUP1_SCHMITT:
3719 *value = ftdi->eeprom->group1_schmitt;
3722 *value = ftdi->eeprom->group1_slew;
3725 *value = ftdi->eeprom->group2_drive;
3727 case GROUP2_SCHMITT:
3728 *value = ftdi->eeprom->group2_schmitt;
3731 *value = ftdi->eeprom->group2_slew;
3734 *value = ftdi->eeprom->group3_drive;
3736 case GROUP3_SCHMITT:
3737 *value = ftdi->eeprom->group3_schmitt;
3740 *value = ftdi->eeprom->group3_slew;
3743 *value = ftdi->eeprom->powersave;
3745 case CLOCK_POLARITY:
3746 *value = ftdi->eeprom->clock_polarity;
3749 *value = ftdi->eeprom->data_order;
3752 *value = ftdi->eeprom->flow_control;
3755 *value = ftdi->eeprom->chip;
3758 *value = ftdi->eeprom->size;
3760 case EXTERNAL_OSCILLATOR:
3761 *value = ftdi->eeprom->external_oscillator;
3764 ftdi_error_return(-1, "Request for unknown EEPROM value");
3770 Set a value in the decoded EEPROM Structure
3771 No parameter checking is performed
3773 \param ftdi pointer to ftdi_context
3774 \param value_name Enum of the value to set
3778 \retval -1: Value doesn't exist
3779 \retval -2: Value not user settable
3781 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3786 ftdi->eeprom->vendor_id = value;
3789 ftdi->eeprom->product_id = value;
3791 case RELEASE_NUMBER:
3792 ftdi->eeprom->release_number = value;
3795 ftdi->eeprom->self_powered = value;
3798 ftdi->eeprom->remote_wakeup = value;
3801 ftdi->eeprom->is_not_pnp = value;
3804 ftdi->eeprom->suspend_dbus7 = value;
3806 case IN_IS_ISOCHRONOUS:
3807 ftdi->eeprom->in_is_isochronous = value;
3809 case OUT_IS_ISOCHRONOUS:
3810 ftdi->eeprom->out_is_isochronous = value;
3812 case SUSPEND_PULL_DOWNS:
3813 ftdi->eeprom->suspend_pull_downs = value;
3816 ftdi->eeprom->use_serial = value;
3819 ftdi->eeprom->usb_version = value;
3821 case USE_USB_VERSION:
3822 ftdi->eeprom->use_usb_version = value;
3825 ftdi->eeprom->max_power = value;
3827 case CHANNEL_A_TYPE:
3828 ftdi->eeprom->channel_a_type = value;
3830 case CHANNEL_B_TYPE:
3831 ftdi->eeprom->channel_b_type = value;
3833 case CHANNEL_A_DRIVER:
3834 ftdi->eeprom->channel_a_driver = value;
3836 case CHANNEL_B_DRIVER:
3837 ftdi->eeprom->channel_b_driver = value;
3839 case CHANNEL_C_DRIVER:
3840 ftdi->eeprom->channel_c_driver = value;
3842 case CHANNEL_D_DRIVER:
3843 ftdi->eeprom->channel_d_driver = value;
3845 case CHANNEL_A_RS485:
3846 ftdi->eeprom->channel_a_rs485enable = value;
3848 case CHANNEL_B_RS485:
3849 ftdi->eeprom->channel_b_rs485enable = value;
3851 case CHANNEL_C_RS485:
3852 ftdi->eeprom->channel_c_rs485enable = value;
3854 case CHANNEL_D_RS485:
3855 ftdi->eeprom->channel_d_rs485enable = value;
3857 case CBUS_FUNCTION_0:
3858 ftdi->eeprom->cbus_function[0] = value;
3860 case CBUS_FUNCTION_1:
3861 ftdi->eeprom->cbus_function[1] = value;
3863 case CBUS_FUNCTION_2:
3864 ftdi->eeprom->cbus_function[2] = value;
3866 case CBUS_FUNCTION_3:
3867 ftdi->eeprom->cbus_function[3] = value;
3869 case CBUS_FUNCTION_4:
3870 ftdi->eeprom->cbus_function[4] = value;
3872 case CBUS_FUNCTION_5:
3873 ftdi->eeprom->cbus_function[5] = value;
3875 case CBUS_FUNCTION_6:
3876 ftdi->eeprom->cbus_function[6] = value;
3878 case CBUS_FUNCTION_7:
3879 ftdi->eeprom->cbus_function[7] = value;
3881 case CBUS_FUNCTION_8:
3882 ftdi->eeprom->cbus_function[8] = value;
3884 case CBUS_FUNCTION_9:
3885 ftdi->eeprom->cbus_function[9] = value;
3888 ftdi->eeprom->high_current = value;
3890 case HIGH_CURRENT_A:
3891 ftdi->eeprom->high_current_a = value;
3893 case HIGH_CURRENT_B:
3894 ftdi->eeprom->high_current_b = value;
3897 ftdi->eeprom->invert = value;
3900 ftdi->eeprom->group0_drive = value;
3902 case GROUP0_SCHMITT:
3903 ftdi->eeprom->group0_schmitt = value;
3906 ftdi->eeprom->group0_slew = value;
3909 ftdi->eeprom->group1_drive = value;
3911 case GROUP1_SCHMITT:
3912 ftdi->eeprom->group1_schmitt = value;
3915 ftdi->eeprom->group1_slew = value;
3918 ftdi->eeprom->group2_drive = value;
3920 case GROUP2_SCHMITT:
3921 ftdi->eeprom->group2_schmitt = value;
3924 ftdi->eeprom->group2_slew = value;
3927 ftdi->eeprom->group3_drive = value;
3929 case GROUP3_SCHMITT:
3930 ftdi->eeprom->group3_schmitt = value;
3933 ftdi->eeprom->group3_slew = value;
3936 ftdi->eeprom->chip = value;
3939 ftdi->eeprom->powersave = value;
3941 case CLOCK_POLARITY:
3942 ftdi->eeprom->clock_polarity = value;
3945 ftdi->eeprom->data_order = value;
3948 ftdi->eeprom->flow_control = value;
3951 ftdi_error_return(-2, "EEPROM Value can't be changed");
3953 case EXTERNAL_OSCILLATOR:
3954 ftdi->eeprom->external_oscillator = value;
3958 ftdi_error_return(-1, "Request to unknown EEPROM value");
3960 ftdi->eeprom->initialized_for_connected_device = 0;
3964 /** Get the read-only buffer to the binary EEPROM content
3966 \param ftdi pointer to ftdi_context
3967 \param buf buffer to receive EEPROM content
3968 \param size Size of receiving buffer
3971 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3972 \retval -2: Not enough room to store eeprom
3974 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3976 if (!ftdi || !(ftdi->eeprom))
3977 ftdi_error_return(-1, "No appropriate structure");
3979 if (!buf || size < ftdi->eeprom->size)
3980 ftdi_error_return(-1, "Not enough room to store eeprom");
3982 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3983 if (size > FTDI_MAX_EEPROM_SIZE)
3984 size = FTDI_MAX_EEPROM_SIZE;
3986 memcpy(buf, ftdi->eeprom->buf, size);
3991 /** Set the EEPROM content from the user-supplied prefilled buffer
3993 \param ftdi pointer to ftdi_context
3994 \param buf buffer to read EEPROM content
3995 \param size Size of buffer
3998 \retval -1: struct ftdi_contxt or ftdi_eeprom of buf missing
4000 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4002 if (!ftdi || !(ftdi->eeprom) || !buf)
4003 ftdi_error_return(-1, "No appropriate structure");
4005 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4006 if (size > FTDI_MAX_EEPROM_SIZE)
4007 size = FTDI_MAX_EEPROM_SIZE;
4009 memcpy(ftdi->eeprom->buf, buf, size);
4015 Read eeprom location
4017 \param ftdi pointer to ftdi_context
4018 \param eeprom_addr Address of eeprom location to be read
4019 \param eeprom_val Pointer to store read eeprom location
4022 \retval -1: read failed
4023 \retval -2: USB device unavailable
4025 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4027 if (ftdi == NULL || ftdi->usb_dev == NULL)
4028 ftdi_error_return(-2, "USB device unavailable");
4030 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
4031 ftdi_error_return(-1, "reading eeprom failed");
4039 \param ftdi pointer to ftdi_context
4042 \retval -1: read failed
4043 \retval -2: USB device unavailable
4045 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4050 if (ftdi == NULL || ftdi->usb_dev == NULL)
4051 ftdi_error_return(-2, "USB device unavailable");
4052 buf = ftdi->eeprom->buf;
4054 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4056 if (libusb_control_transfer(
4057 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4058 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4059 ftdi_error_return(-1, "reading eeprom failed");
4062 if (ftdi->type == TYPE_R)
4063 ftdi->eeprom->size = 0x80;
4064 /* Guesses size of eeprom by comparing halves
4065 - will not work with blank eeprom */
4066 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4067 ftdi->eeprom->size = -1;
4068 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4069 ftdi->eeprom->size = 0x80;
4070 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4071 ftdi->eeprom->size = 0x40;
4073 ftdi->eeprom->size = 0x100;
4078 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4079 Function is only used internally
4082 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4084 return ((value & 1) << 1) |
4085 ((value & 2) << 5) |
4086 ((value & 4) >> 2) |
4087 ((value & 8) << 4) |
4088 ((value & 16) >> 1) |
4089 ((value & 32) >> 1) |
4090 ((value & 64) >> 4) |
4091 ((value & 128) >> 2);
4095 Read the FTDIChip-ID from R-type devices
4097 \param ftdi pointer to ftdi_context
4098 \param chipid Pointer to store FTDIChip-ID
4101 \retval -1: read failed
4102 \retval -2: USB device unavailable
4104 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4106 unsigned int a = 0, b = 0;
4108 if (ftdi == NULL || ftdi->usb_dev == NULL)
4109 ftdi_error_return(-2, "USB device unavailable");
4111 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)
4113 a = a << 8 | a >> 8;
4114 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)
4116 b = b << 8 | b >> 8;
4117 a = (a << 16) | (b & 0xFFFF);
4118 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4119 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4120 *chipid = a ^ 0xa5f0f7d1;
4125 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4129 Write eeprom location
4131 \param ftdi pointer to ftdi_context
4132 \param eeprom_addr Address of eeprom location to be written
4133 \param eeprom_val Value to be written
4136 \retval -1: write failed
4137 \retval -2: USB device unavailable
4138 \retval -3: Invalid access to checksum protected area below 0x80
4139 \retval -4: Device can't access unprotected area
4140 \retval -5: Reading chip type failed
4142 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4143 unsigned short eeprom_val)
4145 int chip_type_location;
4146 unsigned short chip_type;
4148 if (ftdi == NULL || ftdi->usb_dev == NULL)
4149 ftdi_error_return(-2, "USB device unavailable");
4151 if (eeprom_addr <0x80)
4152 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4159 chip_type_location = 0x14;
4163 chip_type_location = 0x18;
4166 chip_type_location = 0x1e;
4169 ftdi_error_return(-4, "Device can't access unprotected area");
4172 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4173 ftdi_error_return(-5, "Reading failed");
4174 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4175 if ((chip_type & 0xff) != 0x66)
4177 ftdi_error_return(-6, "EEPROM is not of 93x66");
4180 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4181 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4182 NULL, 0, ftdi->usb_write_timeout) != 0)
4183 ftdi_error_return(-1, "unable to write eeprom");
4191 \param ftdi pointer to ftdi_context
4194 \retval -1: read failed
4195 \retval -2: USB device unavailable
4196 \retval -3: EEPROM not initialized for the connected device;
4198 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4200 unsigned short usb_val, status;
4202 unsigned char *eeprom;
4204 if (ftdi == NULL || ftdi->usb_dev == NULL)
4205 ftdi_error_return(-2, "USB device unavailable");
4207 if(ftdi->eeprom->initialized_for_connected_device == 0)
4208 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4210 eeprom = ftdi->eeprom->buf;
4212 /* These commands were traced while running MProg */
4213 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4215 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4217 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4220 for (i = 0; i < ftdi->eeprom->size/2; i++)
4222 /* Do not try to write to reserved area */
4223 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4227 usb_val = eeprom[i*2];
4228 usb_val += eeprom[(i*2)+1] << 8;
4229 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4230 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4231 NULL, 0, ftdi->usb_write_timeout) < 0)
4232 ftdi_error_return(-1, "unable to write eeprom");
4241 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4243 \param ftdi pointer to ftdi_context
4246 \retval -1: erase failed
4247 \retval -2: USB device unavailable
4248 \retval -3: Writing magic failed
4249 \retval -4: Read EEPROM failed
4250 \retval -5: Unexpected EEPROM value
4252 #define MAGIC 0x55aa
4253 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4255 unsigned short eeprom_value;
4256 if (ftdi == NULL || ftdi->usb_dev == NULL)
4257 ftdi_error_return(-2, "USB device unavailable");
4259 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4261 ftdi->eeprom->chip = 0;
4265 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4266 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4267 ftdi_error_return(-1, "unable to erase eeprom");
4270 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4271 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4272 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4273 Chip is 93x66 if magic is only read at word position 0xc0*/
4274 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4275 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4276 NULL, 0, ftdi->usb_write_timeout) != 0)
4277 ftdi_error_return(-3, "Writing magic failed");
4278 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4279 ftdi_error_return(-4, "Reading failed");
4280 if (eeprom_value == MAGIC)
4282 ftdi->eeprom->chip = 0x46;
4286 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4287 ftdi_error_return(-4, "Reading failed");
4288 if (eeprom_value == MAGIC)
4289 ftdi->eeprom->chip = 0x56;
4292 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4293 ftdi_error_return(-4, "Reading failed");
4294 if (eeprom_value == MAGIC)
4295 ftdi->eeprom->chip = 0x66;
4298 ftdi->eeprom->chip = -1;
4302 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4303 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4304 ftdi_error_return(-1, "unable to erase eeprom");
4309 Get string representation for last error code
4311 \param ftdi pointer to ftdi_context
4313 \retval Pointer to error string
4315 char *ftdi_get_error_string (struct ftdi_context *ftdi)
4320 return ftdi->error_str;
4323 /* @} end of doxygen libftdi group */