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)
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 != 0 && product != 0) &&
332 desc.idVendor == vendor && desc.idProduct == product) ||
333 ((vendor == 0 && product == 0) &&
334 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
335 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014)))
337 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
339 ftdi_error_return_free_device_list(-3, "out of memory", devs);
341 (*curdev)->next = NULL;
342 (*curdev)->dev = dev;
343 libusb_ref_device(dev);
344 curdev = &(*curdev)->next;
348 libusb_free_device_list(devs,1);
353 Frees a usb device list.
355 \param devlist USB device list created by ftdi_usb_find_all()
357 void ftdi_list_free(struct ftdi_device_list **devlist)
359 struct ftdi_device_list *curdev, *next;
361 for (curdev = *devlist; curdev != NULL;)
364 libusb_unref_device(curdev->dev);
373 Frees a usb device list.
375 \param devlist USB device list created by ftdi_usb_find_all()
377 void ftdi_list_free2(struct ftdi_device_list *devlist)
379 ftdi_list_free(&devlist);
383 Return device ID strings from the usb device.
385 The parameters manufacturer, description and serial may be NULL
386 or pointer to buffers to store the fetched strings.
388 \note Use this function only in combination with ftdi_usb_find_all()
389 as it closes the internal "usb_dev" after use.
391 \param ftdi pointer to ftdi_context
392 \param dev libusb usb_dev to use
393 \param manufacturer Store manufacturer string here if not NULL
394 \param mnf_len Buffer size of manufacturer string
395 \param description Store product description string here if not NULL
396 \param desc_len Buffer size of product description string
397 \param serial Store serial string here if not NULL
398 \param serial_len Buffer size of serial string
401 \retval -1: wrong arguments
402 \retval -4: unable to open device
403 \retval -7: get product manufacturer failed
404 \retval -8: get product description failed
405 \retval -9: get serial number failed
406 \retval -11: libusb_get_device_descriptor() failed
408 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
409 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
411 struct libusb_device_descriptor desc;
413 if ((ftdi==NULL) || (dev==NULL))
416 if (libusb_open(dev, &ftdi->usb_dev) < 0)
417 ftdi_error_return(-4, "libusb_open() failed");
419 if (libusb_get_device_descriptor(dev, &desc) < 0)
420 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
422 if (manufacturer != NULL)
424 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
426 ftdi_usb_close_internal (ftdi);
427 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
431 if (description != NULL)
433 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
435 ftdi_usb_close_internal (ftdi);
436 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
442 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
444 ftdi_usb_close_internal (ftdi);
445 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
449 ftdi_usb_close_internal (ftdi);
455 * Internal function to determine the maximum packet size.
456 * \param ftdi pointer to ftdi_context
457 * \param dev libusb usb_dev to use
458 * \retval Maximum packet size for this device
460 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
462 struct libusb_device_descriptor desc;
463 struct libusb_config_descriptor *config0;
464 unsigned int packet_size;
467 if (ftdi == NULL || dev == NULL)
470 // Determine maximum packet size. Init with default value.
471 // New hi-speed devices from FTDI use a packet size of 512 bytes
472 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
473 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H || ftdi->type == TYPE_230X)
478 if (libusb_get_device_descriptor(dev, &desc) < 0)
481 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
484 if (desc.bNumConfigurations > 0)
486 if (ftdi->interface < config0->bNumInterfaces)
488 struct libusb_interface interface = config0->interface[ftdi->interface];
489 if (interface.num_altsetting > 0)
491 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
492 if (descriptor.bNumEndpoints > 0)
494 packet_size = descriptor.endpoint[0].wMaxPacketSize;
500 libusb_free_config_descriptor (config0);
505 Opens a ftdi device given by an usb_device.
507 \param ftdi pointer to ftdi_context
508 \param dev libusb usb_dev to use
511 \retval -3: unable to config device
512 \retval -4: unable to open device
513 \retval -5: unable to claim device
514 \retval -6: reset failed
515 \retval -7: set baudrate failed
516 \retval -8: ftdi context invalid
517 \retval -9: libusb_get_device_descriptor() failed
518 \retval -10: libusb_get_config_descriptor() failed
519 \retval -11: libusb_detach_kernel_driver() failed
520 \retval -12: libusb_get_configuration() failed
522 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
524 struct libusb_device_descriptor desc;
525 struct libusb_config_descriptor *config0;
526 int cfg, cfg0, detach_errno = 0;
529 ftdi_error_return(-8, "ftdi context invalid");
531 if (libusb_open(dev, &ftdi->usb_dev) < 0)
532 ftdi_error_return(-4, "libusb_open() failed");
534 if (libusb_get_device_descriptor(dev, &desc) < 0)
535 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
537 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
538 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
539 cfg0 = config0->bConfigurationValue;
540 libusb_free_config_descriptor (config0);
542 // Try to detach ftdi_sio kernel module.
544 // The return code is kept in a separate variable and only parsed
545 // if usb_set_configuration() or usb_claim_interface() fails as the
546 // detach operation might be denied and everything still works fine.
547 // Likely scenario is a static ftdi_sio kernel module.
548 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
550 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
551 detach_errno = errno;
554 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
555 ftdi_error_return(-12, "libusb_get_configuration () failed");
556 // set configuration (needed especially for windows)
557 // tolerate EBUSY: one device with one configuration, but two interfaces
558 // and libftdi sessions to both interfaces (e.g. FT2232)
559 if (desc.bNumConfigurations > 0 && cfg != cfg0)
561 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
563 ftdi_usb_close_internal (ftdi);
564 if (detach_errno == EPERM)
566 ftdi_error_return(-8, "inappropriate permissions on device!");
570 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
575 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
577 ftdi_usb_close_internal (ftdi);
578 if (detach_errno == EPERM)
580 ftdi_error_return(-8, "inappropriate permissions on device!");
584 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
588 if (ftdi_usb_reset (ftdi) != 0)
590 ftdi_usb_close_internal (ftdi);
591 ftdi_error_return(-6, "ftdi_usb_reset failed");
594 // Try to guess chip type
595 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
596 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
597 && desc.iSerialNumber == 0))
598 ftdi->type = TYPE_BM;
599 else if (desc.bcdDevice == 0x200)
600 ftdi->type = TYPE_AM;
601 else if (desc.bcdDevice == 0x500)
602 ftdi->type = TYPE_2232C;
603 else if (desc.bcdDevice == 0x600)
605 else if (desc.bcdDevice == 0x700)
606 ftdi->type = TYPE_2232H;
607 else if (desc.bcdDevice == 0x800)
608 ftdi->type = TYPE_4232H;
609 else if (desc.bcdDevice == 0x900)
610 ftdi->type = TYPE_232H;
611 else if (desc.bcdDevice == 0x1000)
612 ftdi->type = TYPE_230X;
614 // Determine maximum packet size
615 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
617 if (ftdi_set_baudrate (ftdi, 9600) != 0)
619 ftdi_usb_close_internal (ftdi);
620 ftdi_error_return(-7, "set baudrate failed");
623 ftdi_error_return(0, "all fine");
627 Opens the first device with a given vendor and product ids.
629 \param ftdi pointer to ftdi_context
630 \param vendor Vendor ID
631 \param product Product ID
633 \retval same as ftdi_usb_open_desc()
635 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
637 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
641 Opens the first device with a given, vendor id, product id,
642 description and serial.
644 \param ftdi pointer to ftdi_context
645 \param vendor Vendor ID
646 \param product Product ID
647 \param description Description to search for. Use NULL if not needed.
648 \param serial Serial to search for. Use NULL if not needed.
651 \retval -3: usb device not found
652 \retval -4: unable to open device
653 \retval -5: unable to claim device
654 \retval -6: reset failed
655 \retval -7: set baudrate failed
656 \retval -8: get product description failed
657 \retval -9: get serial number failed
658 \retval -12: libusb_get_device_list() failed
659 \retval -13: libusb_get_device_descriptor() failed
661 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
662 const char* description, const char* serial)
664 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
668 Opens the index-th device with a given, vendor id, product id,
669 description and serial.
671 \param ftdi pointer to ftdi_context
672 \param vendor Vendor ID
673 \param product Product ID
674 \param description Description to search for. Use NULL if not needed.
675 \param serial Serial to search for. Use NULL if not needed.
676 \param index Number of matching device to open if there are more than one, starts with 0.
679 \retval -1: usb_find_busses() failed
680 \retval -2: usb_find_devices() failed
681 \retval -3: usb device not found
682 \retval -4: unable to open device
683 \retval -5: unable to claim device
684 \retval -6: reset failed
685 \retval -7: set baudrate failed
686 \retval -8: get product description failed
687 \retval -9: get serial number failed
688 \retval -10: unable to close device
689 \retval -11: ftdi context invalid
691 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
692 const char* description, const char* serial, unsigned int index)
695 libusb_device **devs;
700 ftdi_error_return(-11, "ftdi context invalid");
702 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
703 ftdi_error_return(-12, "libusb_get_device_list() failed");
705 while ((dev = devs[i++]) != NULL)
707 struct libusb_device_descriptor desc;
710 if (libusb_get_device_descriptor(dev, &desc) < 0)
711 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
713 if (desc.idVendor == vendor && desc.idProduct == product)
715 if (libusb_open(dev, &ftdi->usb_dev) < 0)
716 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
718 if (description != NULL)
720 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
722 ftdi_usb_close_internal (ftdi);
723 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
725 if (strncmp(string, description, sizeof(string)) != 0)
727 ftdi_usb_close_internal (ftdi);
733 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
735 ftdi_usb_close_internal (ftdi);
736 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
738 if (strncmp(string, serial, sizeof(string)) != 0)
740 ftdi_usb_close_internal (ftdi);
745 ftdi_usb_close_internal (ftdi);
753 res = ftdi_usb_open_dev(ftdi, dev);
754 libusb_free_device_list(devs,1);
760 ftdi_error_return_free_device_list(-3, "device not found", devs);
764 Opens the ftdi-device described by a description-string.
765 Intended to be used for parsing a device-description given as commandline argument.
767 \param ftdi pointer to ftdi_context
768 \param description NULL-terminated description-string, using this format:
769 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
770 \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")
771 \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
772 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
774 \note The description format may be extended in later versions.
777 \retval -2: libusb_get_device_list() failed
778 \retval -3: usb device not found
779 \retval -4: unable to open device
780 \retval -5: unable to claim device
781 \retval -6: reset failed
782 \retval -7: set baudrate failed
783 \retval -8: get product description failed
784 \retval -9: get serial number failed
785 \retval -10: unable to close device
786 \retval -11: illegal description format
787 \retval -12: ftdi context invalid
789 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
792 ftdi_error_return(-12, "ftdi context invalid");
794 if (description[0] == 0 || description[1] != ':')
795 ftdi_error_return(-11, "illegal description format");
797 if (description[0] == 'd')
800 libusb_device **devs;
801 unsigned int bus_number, device_address;
804 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
805 ftdi_error_return(-2, "libusb_get_device_list() failed");
807 /* XXX: This doesn't handle symlinks/odd paths/etc... */
808 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
809 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
811 while ((dev = devs[i++]) != NULL)
814 if (bus_number == libusb_get_bus_number (dev)
815 && device_address == libusb_get_device_address (dev))
817 ret = ftdi_usb_open_dev(ftdi, dev);
818 libusb_free_device_list(devs,1);
824 ftdi_error_return_free_device_list(-3, "device not found", devs);
826 else if (description[0] == 'i' || description[0] == 's')
829 unsigned int product;
830 unsigned int index=0;
831 const char *serial=NULL;
832 const char *startp, *endp;
835 startp=description+2;
836 vendor=strtoul((char*)startp,(char**)&endp,0);
837 if (*endp != ':' || endp == startp || errno != 0)
838 ftdi_error_return(-11, "illegal description format");
841 product=strtoul((char*)startp,(char**)&endp,0);
842 if (endp == startp || errno != 0)
843 ftdi_error_return(-11, "illegal description format");
845 if (description[0] == 'i' && *endp != 0)
847 /* optional index field in i-mode */
849 ftdi_error_return(-11, "illegal description format");
852 index=strtoul((char*)startp,(char**)&endp,0);
853 if (*endp != 0 || endp == startp || errno != 0)
854 ftdi_error_return(-11, "illegal description format");
856 if (description[0] == 's')
859 ftdi_error_return(-11, "illegal description format");
861 /* rest of the description is the serial */
865 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
869 ftdi_error_return(-11, "illegal description format");
874 Resets the ftdi device.
876 \param ftdi pointer to ftdi_context
879 \retval -1: FTDI reset failed
880 \retval -2: USB device unavailable
882 int ftdi_usb_reset(struct ftdi_context *ftdi)
884 if (ftdi == NULL || ftdi->usb_dev == NULL)
885 ftdi_error_return(-2, "USB device unavailable");
887 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
888 SIO_RESET_REQUEST, SIO_RESET_SIO,
889 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
890 ftdi_error_return(-1,"FTDI reset failed");
892 // Invalidate data in the readbuffer
893 ftdi->readbuffer_offset = 0;
894 ftdi->readbuffer_remaining = 0;
900 Clears the read buffer on the chip and the internal read buffer.
902 \param ftdi pointer to ftdi_context
905 \retval -1: read buffer purge failed
906 \retval -2: USB device unavailable
908 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
910 if (ftdi == NULL || ftdi->usb_dev == NULL)
911 ftdi_error_return(-2, "USB device unavailable");
913 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
914 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
915 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
916 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
918 // Invalidate data in the readbuffer
919 ftdi->readbuffer_offset = 0;
920 ftdi->readbuffer_remaining = 0;
926 Clears the write buffer on the chip.
928 \param ftdi pointer to ftdi_context
931 \retval -1: write buffer purge failed
932 \retval -2: USB device unavailable
934 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
936 if (ftdi == NULL || ftdi->usb_dev == NULL)
937 ftdi_error_return(-2, "USB device unavailable");
939 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
940 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
941 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
942 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
948 Clears the buffers on the chip and the internal read buffer.
950 \param ftdi pointer to ftdi_context
953 \retval -1: read buffer purge failed
954 \retval -2: write buffer purge failed
955 \retval -3: USB device unavailable
957 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
961 if (ftdi == NULL || ftdi->usb_dev == NULL)
962 ftdi_error_return(-3, "USB device unavailable");
964 result = ftdi_usb_purge_rx_buffer(ftdi);
968 result = ftdi_usb_purge_tx_buffer(ftdi);
978 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
980 \param ftdi pointer to ftdi_context
983 \retval -1: usb_release failed
984 \retval -3: ftdi context invalid
986 int ftdi_usb_close(struct ftdi_context *ftdi)
991 ftdi_error_return(-3, "ftdi context invalid");
993 if (ftdi->usb_dev != NULL)
994 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
997 ftdi_usb_close_internal (ftdi);
1002 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1003 to encoded divisor and the achievable baudrate
1004 Function is only used internally
1011 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1012 The fractional part has frac_code encoding
1014 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1017 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1018 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1019 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1020 int divisor, best_divisor, best_baud, best_baud_diff;
1021 divisor = 24000000 / baudrate;
1024 // Round down to supported fraction (AM only)
1025 divisor -= am_adjust_dn[divisor & 7];
1027 // Try this divisor and the one above it (because division rounds down)
1031 for (i = 0; i < 2; i++)
1033 int try_divisor = divisor + i;
1037 // Round up to supported divisor value
1038 if (try_divisor <= 8)
1040 // Round up to minimum supported divisor
1043 else if (divisor < 16)
1045 // AM doesn't support divisors 9 through 15 inclusive
1050 // Round up to supported fraction (AM only)
1051 try_divisor += am_adjust_up[try_divisor & 7];
1052 if (try_divisor > 0x1FFF8)
1054 // Round down to maximum supported divisor value (for AM)
1055 try_divisor = 0x1FFF8;
1058 // Get estimated baud rate (to nearest integer)
1059 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1060 // Get absolute difference from requested baud rate
1061 if (baud_estimate < baudrate)
1063 baud_diff = baudrate - baud_estimate;
1067 baud_diff = baud_estimate - baudrate;
1069 if (i == 0 || baud_diff < best_baud_diff)
1071 // Closest to requested baud rate so far
1072 best_divisor = try_divisor;
1073 best_baud = baud_estimate;
1074 best_baud_diff = baud_diff;
1077 // Spot on! No point trying
1082 // Encode the best divisor value
1083 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1084 // Deal with special cases for encoded value
1085 if (*encoded_divisor == 1)
1087 *encoded_divisor = 0; // 3000000 baud
1089 else if (*encoded_divisor == 0x4001)
1091 *encoded_divisor = 1; // 2000000 baud (BM only)
1096 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1097 to encoded divisor and the achievable baudrate
1098 Function is only used internally
1105 From /2, 0.125 steps may be taken.
1106 The fractional part has frac_code encoding
1108 value[13:0] of value is the divisor
1109 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1111 H Type have all features above with
1112 {index[8],value[15:14]} is the encoded subdivisor
1114 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1115 {index[0],value[15:14]} is the encoded subdivisor
1117 AM Type chips have only four fractional subdivisors at value[15:14]
1118 for subdivisors 0, 0.5, 0.25, 0.125
1120 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1122 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1124 int divisor, best_divisor;
1125 if (baudrate >= clk/clk_div)
1127 *encoded_divisor = 0;
1128 best_baud = clk/clk_div;
1130 else if (baudrate >= clk/(clk_div + clk_div/2))
1132 *encoded_divisor = 1;
1133 best_baud = clk/(clk_div + clk_div/2);
1135 else if (baudrate >= clk/(2*clk_div))
1137 *encoded_divisor = 2;
1138 best_baud = clk/(2*clk_div);
1142 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1143 divisor = clk*16/clk_div / baudrate;
1144 if (divisor & 1) /* Decide if to round up or down*/
1145 best_divisor = divisor /2 +1;
1147 best_divisor = divisor/2;
1148 if(best_divisor > 0x20000)
1149 best_divisor = 0x1ffff;
1150 best_baud = clk*16/clk_div/best_divisor;
1151 if (best_baud & 1) /* Decide if to round up or down*/
1152 best_baud = best_baud /2 +1;
1154 best_baud = best_baud /2;
1155 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1160 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1161 Function is only used internally
1164 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1165 unsigned short *value, unsigned short *index)
1168 unsigned long encoded_divisor;
1176 #define H_CLK 120000000
1177 #define C_CLK 48000000
1178 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H) || (ftdi->type == TYPE_230X))
1180 if(baudrate*10 > H_CLK /0x3fff)
1182 /* On H Devices, use 12 000 000 Baudrate when possible
1183 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1184 three fractional bits and a 120 MHz clock
1185 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1186 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1187 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1188 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1191 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1193 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
1195 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1199 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1201 // Split into "value" and "index" values
1202 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1203 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H || ftdi->type == TYPE_230X)
1205 *index = (unsigned short)(encoded_divisor >> 8);
1207 *index |= ftdi->index;
1210 *index = (unsigned short)(encoded_divisor >> 16);
1212 // Return the nearest baud rate
1217 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1218 * Do not use, it's only for the unit test framework
1220 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1221 unsigned short *value, unsigned short *index)
1223 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1227 Sets the chip baud rate
1229 \param ftdi pointer to ftdi_context
1230 \param baudrate baud rate to set
1233 \retval -1: invalid baudrate
1234 \retval -2: setting baudrate failed
1235 \retval -3: USB device unavailable
1237 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1239 unsigned short value, index;
1240 int actual_baudrate;
1242 if (ftdi == NULL || ftdi->usb_dev == NULL)
1243 ftdi_error_return(-3, "USB device unavailable");
1245 if (ftdi->bitbang_enabled)
1247 baudrate = baudrate*4;
1250 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1251 if (actual_baudrate <= 0)
1252 ftdi_error_return (-1, "Silly baudrate <= 0.");
1254 // Check within tolerance (about 5%)
1255 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1256 || ((actual_baudrate < baudrate)
1257 ? (actual_baudrate * 21 < baudrate * 20)
1258 : (baudrate * 21 < actual_baudrate * 20)))
1259 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1261 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1262 SIO_SET_BAUDRATE_REQUEST, value,
1263 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1264 ftdi_error_return (-2, "Setting new baudrate failed");
1266 ftdi->baudrate = baudrate;
1271 Set (RS232) line characteristics.
1272 The break type can only be set via ftdi_set_line_property2()
1273 and defaults to "off".
1275 \param ftdi pointer to ftdi_context
1276 \param bits Number of bits
1277 \param sbit Number of stop bits
1278 \param parity Parity mode
1281 \retval -1: Setting line property failed
1283 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1284 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1286 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1290 Set (RS232) line characteristics
1292 \param ftdi pointer to ftdi_context
1293 \param bits Number of bits
1294 \param sbit Number of stop bits
1295 \param parity Parity mode
1296 \param break_type Break type
1299 \retval -1: Setting line property failed
1300 \retval -2: USB device unavailable
1302 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1303 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1304 enum ftdi_break_type break_type)
1306 unsigned short value = bits;
1308 if (ftdi == NULL || ftdi->usb_dev == NULL)
1309 ftdi_error_return(-2, "USB device unavailable");
1314 value |= (0x00 << 8);
1317 value |= (0x01 << 8);
1320 value |= (0x02 << 8);
1323 value |= (0x03 << 8);
1326 value |= (0x04 << 8);
1333 value |= (0x00 << 11);
1336 value |= (0x01 << 11);
1339 value |= (0x02 << 11);
1346 value |= (0x00 << 14);
1349 value |= (0x01 << 14);
1353 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1354 SIO_SET_DATA_REQUEST, value,
1355 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1356 ftdi_error_return (-1, "Setting new line property failed");
1362 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1364 \param ftdi pointer to ftdi_context
1365 \param buf Buffer with the data
1366 \param size Size of the buffer
1368 \retval -666: USB device unavailable
1369 \retval <0: error code from usb_bulk_write()
1370 \retval >0: number of bytes written
1372 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1377 if (ftdi == NULL || ftdi->usb_dev == NULL)
1378 ftdi_error_return(-666, "USB device unavailable");
1380 while (offset < size)
1382 int write_size = ftdi->writebuffer_chunksize;
1384 if (offset+write_size > size)
1385 write_size = size-offset;
1387 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1388 ftdi_error_return(-1, "usb bulk write failed");
1390 offset += actual_length;
1396 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1398 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1399 struct ftdi_context *ftdi = tc->ftdi;
1400 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1402 packet_size = ftdi->max_packet_size;
1404 actual_length = transfer->actual_length;
1406 if (actual_length > 2)
1408 // skip FTDI status bytes.
1409 // Maybe stored in the future to enable modem use
1410 num_of_chunks = actual_length / packet_size;
1411 chunk_remains = actual_length % packet_size;
1412 //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);
1414 ftdi->readbuffer_offset += 2;
1417 if (actual_length > packet_size - 2)
1419 for (i = 1; i < num_of_chunks; i++)
1420 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1421 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1423 if (chunk_remains > 2)
1425 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1426 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1428 actual_length -= 2*num_of_chunks;
1431 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1434 if (actual_length > 0)
1436 // data still fits in buf?
1437 if (tc->offset + actual_length <= tc->size)
1439 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1440 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1441 tc->offset += actual_length;
1443 ftdi->readbuffer_offset = 0;
1444 ftdi->readbuffer_remaining = 0;
1446 /* Did we read exactly the right amount of bytes? */
1447 if (tc->offset == tc->size)
1449 //printf("read_data exact rem %d offset %d\n",
1450 //ftdi->readbuffer_remaining, offset);
1457 // only copy part of the data or size <= readbuffer_chunksize
1458 int part_size = tc->size - tc->offset;
1459 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1460 tc->offset += part_size;
1462 ftdi->readbuffer_offset += part_size;
1463 ftdi->readbuffer_remaining = actual_length - part_size;
1465 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1466 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1472 ret = libusb_submit_transfer (transfer);
1478 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1480 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1481 struct ftdi_context *ftdi = tc->ftdi;
1483 tc->offset += transfer->actual_length;
1485 if (tc->offset == tc->size)
1491 int write_size = ftdi->writebuffer_chunksize;
1494 if (tc->offset + write_size > tc->size)
1495 write_size = tc->size - tc->offset;
1497 transfer->length = write_size;
1498 transfer->buffer = tc->buf + tc->offset;
1499 ret = libusb_submit_transfer (transfer);
1507 Writes data to the chip. Does not wait for completion of the transfer
1508 nor does it make sure that the transfer was successful.
1510 Use libusb 1.0 asynchronous API.
1512 \param ftdi pointer to ftdi_context
1513 \param buf Buffer with the data
1514 \param size Size of the buffer
1516 \retval NULL: Some error happens when submit transfer
1517 \retval !NULL: Pointer to a ftdi_transfer_control
1520 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1522 struct ftdi_transfer_control *tc;
1523 struct libusb_transfer *transfer;
1524 int write_size, ret;
1526 if (ftdi == NULL || ftdi->usb_dev == NULL)
1529 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1533 transfer = libusb_alloc_transfer(0);
1546 if (size < (int)ftdi->writebuffer_chunksize)
1549 write_size = ftdi->writebuffer_chunksize;
1551 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1552 write_size, ftdi_write_data_cb, tc,
1553 ftdi->usb_write_timeout);
1554 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1556 ret = libusb_submit_transfer(transfer);
1559 libusb_free_transfer(transfer);
1563 tc->transfer = transfer;
1569 Reads data from the chip. Does not wait for completion of the transfer
1570 nor does it make sure that the transfer was successful.
1572 Use libusb 1.0 asynchronous API.
1574 \param ftdi pointer to ftdi_context
1575 \param buf Buffer with the data
1576 \param size Size of the buffer
1578 \retval NULL: Some error happens when submit transfer
1579 \retval !NULL: Pointer to a ftdi_transfer_control
1582 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1584 struct ftdi_transfer_control *tc;
1585 struct libusb_transfer *transfer;
1588 if (ftdi == NULL || ftdi->usb_dev == NULL)
1591 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1599 if (size <= (int)ftdi->readbuffer_remaining)
1601 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1604 ftdi->readbuffer_remaining -= size;
1605 ftdi->readbuffer_offset += size;
1607 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1611 tc->transfer = NULL;
1616 if (ftdi->readbuffer_remaining != 0)
1618 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1620 tc->offset = ftdi->readbuffer_remaining;
1625 transfer = libusb_alloc_transfer(0);
1632 ftdi->readbuffer_remaining = 0;
1633 ftdi->readbuffer_offset = 0;
1635 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);
1636 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1638 ret = libusb_submit_transfer(transfer);
1641 libusb_free_transfer(transfer);
1645 tc->transfer = transfer;
1651 Wait for completion of the transfer.
1653 Use libusb 1.0 asynchronous API.
1655 \param tc pointer to ftdi_transfer_control
1657 \retval < 0: Some error happens
1658 \retval >= 0: Data size transferred
1661 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1665 while (!tc->completed)
1667 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1670 if (ret == LIBUSB_ERROR_INTERRUPTED)
1672 libusb_cancel_transfer(tc->transfer);
1673 while (!tc->completed)
1674 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1676 libusb_free_transfer(tc->transfer);
1684 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1685 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1689 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1691 libusb_free_transfer(tc->transfer);
1698 Configure write buffer chunk size.
1701 \param ftdi pointer to ftdi_context
1702 \param chunksize Chunk size
1705 \retval -1: ftdi context invalid
1707 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1710 ftdi_error_return(-1, "ftdi context invalid");
1712 ftdi->writebuffer_chunksize = chunksize;
1717 Get write buffer chunk size.
1719 \param ftdi pointer to ftdi_context
1720 \param chunksize Pointer to store chunk size in
1723 \retval -1: ftdi context invalid
1725 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1728 ftdi_error_return(-1, "ftdi context invalid");
1730 *chunksize = ftdi->writebuffer_chunksize;
1735 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1737 Automatically strips the two modem status bytes transfered during every read.
1739 \param ftdi pointer to ftdi_context
1740 \param buf Buffer to store data in
1741 \param size Size of the buffer
1743 \retval -666: USB device unavailable
1744 \retval <0: error code from libusb_bulk_transfer()
1745 \retval 0: no data was available
1746 \retval >0: number of bytes read
1749 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1751 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1752 int packet_size = ftdi->max_packet_size;
1753 int actual_length = 1;
1755 if (ftdi == NULL || ftdi->usb_dev == NULL)
1756 ftdi_error_return(-666, "USB device unavailable");
1758 // Packet size sanity check (avoid division by zero)
1759 if (packet_size == 0)
1760 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1762 // everything we want is still in the readbuffer?
1763 if (size <= (int)ftdi->readbuffer_remaining)
1765 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1768 ftdi->readbuffer_remaining -= size;
1769 ftdi->readbuffer_offset += size;
1771 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1775 // something still in the readbuffer, but not enough to satisfy 'size'?
1776 if (ftdi->readbuffer_remaining != 0)
1778 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1781 offset += ftdi->readbuffer_remaining;
1783 // do the actual USB read
1784 while (offset < size && actual_length > 0)
1786 ftdi->readbuffer_remaining = 0;
1787 ftdi->readbuffer_offset = 0;
1788 /* returns how much received */
1789 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1791 ftdi_error_return(ret, "usb bulk read failed");
1793 if (actual_length > 2)
1795 // skip FTDI status bytes.
1796 // Maybe stored in the future to enable modem use
1797 num_of_chunks = actual_length / packet_size;
1798 chunk_remains = actual_length % packet_size;
1799 //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);
1801 ftdi->readbuffer_offset += 2;
1804 if (actual_length > packet_size - 2)
1806 for (i = 1; i < num_of_chunks; i++)
1807 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1808 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1810 if (chunk_remains > 2)
1812 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1813 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1815 actual_length -= 2*num_of_chunks;
1818 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1821 else if (actual_length <= 2)
1823 // no more data to read?
1826 if (actual_length > 0)
1828 // data still fits in buf?
1829 if (offset+actual_length <= size)
1831 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1832 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1833 offset += actual_length;
1835 /* Did we read exactly the right amount of bytes? */
1837 //printf("read_data exact rem %d offset %d\n",
1838 //ftdi->readbuffer_remaining, offset);
1843 // only copy part of the data or size <= readbuffer_chunksize
1844 int part_size = size-offset;
1845 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1847 ftdi->readbuffer_offset += part_size;
1848 ftdi->readbuffer_remaining = actual_length-part_size;
1849 offset += part_size;
1851 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1852 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1863 Configure read buffer chunk size.
1866 Automatically reallocates the buffer.
1868 \param ftdi pointer to ftdi_context
1869 \param chunksize Chunk size
1872 \retval -1: ftdi context invalid
1874 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1876 unsigned char *new_buf;
1879 ftdi_error_return(-1, "ftdi context invalid");
1881 // Invalidate all remaining data
1882 ftdi->readbuffer_offset = 0;
1883 ftdi->readbuffer_remaining = 0;
1885 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1886 which is defined in libusb-1.0. Otherwise, each USB read request will
1887 be divided into multiple URBs. This will cause issues on Linux kernel
1888 older than 2.6.32. */
1889 if (chunksize > 16384)
1893 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1894 ftdi_error_return(-1, "out of memory for readbuffer");
1896 ftdi->readbuffer = new_buf;
1897 ftdi->readbuffer_chunksize = chunksize;
1903 Get read buffer chunk size.
1905 \param ftdi pointer to ftdi_context
1906 \param chunksize Pointer to store chunk size in
1909 \retval -1: FTDI context invalid
1911 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1914 ftdi_error_return(-1, "FTDI context invalid");
1916 *chunksize = ftdi->readbuffer_chunksize;
1921 Enable/disable bitbang modes.
1923 \param ftdi pointer to ftdi_context
1924 \param bitmask Bitmask to configure lines.
1925 HIGH/ON value configures a line as output.
1926 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1929 \retval -1: can't enable bitbang mode
1930 \retval -2: USB device unavailable
1932 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1934 unsigned short usb_val;
1936 if (ftdi == NULL || ftdi->usb_dev == NULL)
1937 ftdi_error_return(-2, "USB device unavailable");
1939 usb_val = bitmask; // low byte: bitmask
1940 usb_val |= (mode << 8);
1941 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)
1942 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
1944 ftdi->bitbang_mode = mode;
1945 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1950 Disable bitbang mode.
1952 \param ftdi pointer to ftdi_context
1955 \retval -1: can't disable bitbang mode
1956 \retval -2: USB device unavailable
1958 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1960 if (ftdi == NULL || ftdi->usb_dev == NULL)
1961 ftdi_error_return(-2, "USB device unavailable");
1963 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)
1964 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1966 ftdi->bitbang_enabled = 0;
1972 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1974 \param ftdi pointer to ftdi_context
1975 \param pins Pointer to store pins into
1978 \retval -1: read pins failed
1979 \retval -2: USB device unavailable
1981 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1983 if (ftdi == NULL || ftdi->usb_dev == NULL)
1984 ftdi_error_return(-2, "USB device unavailable");
1986 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)
1987 ftdi_error_return(-1, "read pins failed");
1995 The FTDI chip keeps data in the internal buffer for a specific
1996 amount of time if the buffer is not full yet to decrease
1997 load on the usb bus.
1999 \param ftdi pointer to ftdi_context
2000 \param latency Value between 1 and 255
2003 \retval -1: latency out of range
2004 \retval -2: unable to set latency timer
2005 \retval -3: USB device unavailable
2007 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2009 unsigned short usb_val;
2012 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2014 if (ftdi == NULL || ftdi->usb_dev == NULL)
2015 ftdi_error_return(-3, "USB device unavailable");
2018 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)
2019 ftdi_error_return(-2, "unable to set latency timer");
2027 \param ftdi pointer to ftdi_context
2028 \param latency Pointer to store latency value in
2031 \retval -1: unable to get latency timer
2032 \retval -2: USB device unavailable
2034 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2036 unsigned short usb_val;
2038 if (ftdi == NULL || ftdi->usb_dev == NULL)
2039 ftdi_error_return(-2, "USB device unavailable");
2041 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)
2042 ftdi_error_return(-1, "reading latency timer failed");
2044 *latency = (unsigned char)usb_val;
2049 Poll modem status information
2051 This function allows the retrieve the two status bytes of the device.
2052 The device sends these bytes also as a header for each read access
2053 where they are discarded by ftdi_read_data(). The chip generates
2054 the two stripped status bytes in the absence of data every 40 ms.
2056 Layout of the first byte:
2057 - B0..B3 - must be 0
2058 - B4 Clear to send (CTS)
2061 - B5 Data set ready (DTS)
2064 - B6 Ring indicator (RI)
2067 - B7 Receive line signal detect (RLSD)
2071 Layout of the second byte:
2072 - B0 Data ready (DR)
2073 - B1 Overrun error (OE)
2074 - B2 Parity error (PE)
2075 - B3 Framing error (FE)
2076 - B4 Break interrupt (BI)
2077 - B5 Transmitter holding register (THRE)
2078 - B6 Transmitter empty (TEMT)
2079 - B7 Error in RCVR FIFO
2081 \param ftdi pointer to ftdi_context
2082 \param status Pointer to store status information in. Must be two bytes.
2085 \retval -1: unable to retrieve status information
2086 \retval -2: USB device unavailable
2088 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2092 if (ftdi == NULL || ftdi->usb_dev == NULL)
2093 ftdi_error_return(-2, "USB device unavailable");
2095 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)
2096 ftdi_error_return(-1, "getting modem status failed");
2098 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2104 Set flowcontrol for ftdi chip
2106 \param ftdi pointer to ftdi_context
2107 \param flowctrl flow control to use. should be
2108 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2111 \retval -1: set flow control failed
2112 \retval -2: USB device unavailable
2114 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2116 if (ftdi == NULL || ftdi->usb_dev == NULL)
2117 ftdi_error_return(-2, "USB device unavailable");
2119 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2120 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2121 NULL, 0, ftdi->usb_write_timeout) < 0)
2122 ftdi_error_return(-1, "set flow control failed");
2130 \param ftdi pointer to ftdi_context
2131 \param state state to set line to (1 or 0)
2134 \retval -1: set dtr failed
2135 \retval -2: USB device unavailable
2137 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2139 unsigned short usb_val;
2141 if (ftdi == NULL || ftdi->usb_dev == NULL)
2142 ftdi_error_return(-2, "USB device unavailable");
2145 usb_val = SIO_SET_DTR_HIGH;
2147 usb_val = SIO_SET_DTR_LOW;
2149 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2150 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2151 NULL, 0, ftdi->usb_write_timeout) < 0)
2152 ftdi_error_return(-1, "set dtr failed");
2160 \param ftdi pointer to ftdi_context
2161 \param state state to set line to (1 or 0)
2164 \retval -1: set rts failed
2165 \retval -2: USB device unavailable
2167 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2169 unsigned short usb_val;
2171 if (ftdi == NULL || ftdi->usb_dev == NULL)
2172 ftdi_error_return(-2, "USB device unavailable");
2175 usb_val = SIO_SET_RTS_HIGH;
2177 usb_val = SIO_SET_RTS_LOW;
2179 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2180 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2181 NULL, 0, ftdi->usb_write_timeout) < 0)
2182 ftdi_error_return(-1, "set of rts failed");
2188 Set dtr and rts line in one pass
2190 \param ftdi pointer to ftdi_context
2191 \param dtr DTR state to set line to (1 or 0)
2192 \param rts RTS state to set line to (1 or 0)
2195 \retval -1: set dtr/rts failed
2196 \retval -2: USB device unavailable
2198 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2200 unsigned short usb_val;
2202 if (ftdi == NULL || ftdi->usb_dev == NULL)
2203 ftdi_error_return(-2, "USB device unavailable");
2206 usb_val = SIO_SET_DTR_HIGH;
2208 usb_val = SIO_SET_DTR_LOW;
2211 usb_val |= SIO_SET_RTS_HIGH;
2213 usb_val |= SIO_SET_RTS_LOW;
2215 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2216 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2217 NULL, 0, ftdi->usb_write_timeout) < 0)
2218 ftdi_error_return(-1, "set of rts/dtr failed");
2224 Set the special event character
2226 \param ftdi pointer to ftdi_context
2227 \param eventch Event character
2228 \param enable 0 to disable the event character, non-zero otherwise
2231 \retval -1: unable to set event character
2232 \retval -2: USB device unavailable
2234 int ftdi_set_event_char(struct ftdi_context *ftdi,
2235 unsigned char eventch, unsigned char enable)
2237 unsigned short usb_val;
2239 if (ftdi == NULL || ftdi->usb_dev == NULL)
2240 ftdi_error_return(-2, "USB device unavailable");
2246 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)
2247 ftdi_error_return(-1, "setting event character failed");
2255 \param ftdi pointer to ftdi_context
2256 \param errorch Error character
2257 \param enable 0 to disable the error character, non-zero otherwise
2260 \retval -1: unable to set error character
2261 \retval -2: USB device unavailable
2263 int ftdi_set_error_char(struct ftdi_context *ftdi,
2264 unsigned char errorch, unsigned char enable)
2266 unsigned short usb_val;
2268 if (ftdi == NULL || ftdi->usb_dev == NULL)
2269 ftdi_error_return(-2, "USB device unavailable");
2275 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)
2276 ftdi_error_return(-1, "setting error character failed");
2282 Init eeprom with default values for the connected device
2283 \param ftdi pointer to ftdi_context
2284 \param manufacturer String to use as Manufacturer
2285 \param product String to use as Product description
2286 \param serial String to use as Serial number description
2289 \retval -1: No struct ftdi_context
2290 \retval -2: No struct ftdi_eeprom
2291 \retval -3: No connected device or device not yet opened
2293 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2294 char * product, char * serial)
2296 struct ftdi_eeprom *eeprom;
2299 ftdi_error_return(-1, "No struct ftdi_context");
2301 if (ftdi->eeprom == NULL)
2302 ftdi_error_return(-2,"No struct ftdi_eeprom");
2304 eeprom = ftdi->eeprom;
2305 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2307 if (ftdi->usb_dev == NULL)
2308 ftdi_error_return(-3, "No connected device or device not yet opened");
2310 eeprom->vendor_id = 0x0403;
2311 eeprom->use_serial = 1;
2312 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2313 (ftdi->type == TYPE_R))
2314 eeprom->product_id = 0x6001;
2315 else if (ftdi->type == TYPE_4232H)
2316 eeprom->product_id = 0x6011;
2317 else if (ftdi->type == TYPE_232H)
2318 eeprom->product_id = 0x6014;
2319 else if (ftdi->type == TYPE_230X)
2320 eeprom->product_id = 0x6015;
2322 eeprom->product_id = 0x6010;
2324 if (ftdi->type == TYPE_AM)
2325 eeprom->usb_version = 0x0101;
2327 eeprom->usb_version = 0x0200;
2328 eeprom->max_power = 100;
2330 if (eeprom->manufacturer)
2331 free (eeprom->manufacturer);
2332 eeprom->manufacturer = NULL;
2335 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2336 if (eeprom->manufacturer)
2337 strcpy(eeprom->manufacturer, manufacturer);
2340 if (eeprom->product)
2341 free (eeprom->product);
2342 eeprom->product = NULL;
2345 eeprom->product = malloc(strlen(product)+1);
2346 if (eeprom->product)
2347 strcpy(eeprom->product, product);
2351 const char* default_product;
2354 case TYPE_AM: default_product = "AM"; break;
2355 case TYPE_BM: default_product = "BM"; break;
2356 case TYPE_2232C: default_product = "Dual RS232"; break;
2357 case TYPE_R: default_product = "FT232R USB UART"; break;
2358 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2359 case TYPE_4232H: default_product = "FT4232H"; break;
2360 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2361 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2363 ftdi_error_return(-3, "Unknown chip type");
2365 eeprom->product = malloc(strlen(default_product) +1);
2366 if (eeprom->product)
2367 strcpy(eeprom->product, default_product);
2371 free (eeprom->serial);
2372 eeprom->serial = NULL;
2375 eeprom->serial = malloc(strlen(serial)+1);
2377 strcpy(eeprom->serial, serial);
2380 if (ftdi->type == TYPE_R)
2382 eeprom->max_power = 90;
2383 eeprom->size = 0x80;
2384 eeprom->cbus_function[0] = CBUS_TXLED;
2385 eeprom->cbus_function[1] = CBUS_RXLED;
2386 eeprom->cbus_function[2] = CBUS_TXDEN;
2387 eeprom->cbus_function[3] = CBUS_PWREN;
2388 eeprom->cbus_function[4] = CBUS_SLEEP;
2390 else if (ftdi->type == TYPE_230X)
2392 eeprom->max_power = 90;
2393 eeprom->size = 0x100;
2394 eeprom->cbus_function[0] = CBUSH_TXDEN;
2395 eeprom->cbus_function[1] = CBUSH_RXLED;
2396 eeprom->cbus_function[2] = CBUSH_TXLED;
2397 eeprom->cbus_function[3] = CBUSH_SLEEP;
2401 if(ftdi->type == TYPE_232H)
2404 for (i=0; i<10; i++)
2405 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2412 eeprom->release_number = 0x0200;
2415 eeprom->release_number = 0x0400;
2418 eeprom->release_number = 0x0500;
2421 eeprom->release_number = 0x0600;
2424 eeprom->release_number = 0x0700;
2427 eeprom->release_number = 0x0800;
2430 eeprom->release_number = 0x0900;
2433 eeprom->release_number = 0x1000;
2436 eeprom->release_number = 0x00;
2441 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2442 char * product, char * serial)
2444 struct ftdi_eeprom *eeprom;
2447 ftdi_error_return(-1, "No struct ftdi_context");
2449 if (ftdi->eeprom == NULL)
2450 ftdi_error_return(-2,"No struct ftdi_eeprom");
2452 eeprom = ftdi->eeprom;
2454 if (ftdi->usb_dev == NULL)
2455 ftdi_error_return(-3, "No connected device or device not yet opened");
2459 if (eeprom->manufacturer)
2460 free (eeprom->manufacturer);
2461 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2462 if (eeprom->manufacturer)
2463 strcpy(eeprom->manufacturer, manufacturer);
2468 if (eeprom->product)
2469 free (eeprom->product);
2470 eeprom->product = malloc(strlen(product)+1);
2471 if (eeprom->product)
2472 strcpy(eeprom->product, product);
2478 free (eeprom->serial);
2479 eeprom->serial = malloc(strlen(serial)+1);
2482 strcpy(eeprom->serial, serial);
2483 eeprom->use_serial = 1;
2490 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2491 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2496 int mode_low, mode_high;
2497 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2498 mode_low = CBUSH_TRISTATE;
2500 mode_low = eeprom->cbus_function[2*i];
2501 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2502 mode_high = CBUSH_TRISTATE;
2504 mode_high = eeprom->cbus_function[2*i+1];
2506 output[0x18+i] = (mode_high <<4) | mode_low;
2509 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2512 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2521 case CHANNEL_IS_UART: return 0;
2522 case CHANNEL_IS_FIFO: return 0x01;
2523 case CHANNEL_IS_OPTO: return 0x02;
2524 case CHANNEL_IS_CPU : return 0x04;
2532 case CHANNEL_IS_UART : return 0;
2533 case CHANNEL_IS_FIFO : return 0x01;
2534 case CHANNEL_IS_OPTO : return 0x02;
2535 case CHANNEL_IS_CPU : return 0x04;
2536 case CHANNEL_IS_FT1284 : return 0x08;
2540 case TYPE_230X: /* FT230X is only UART */
2547 Build binary buffer from ftdi_eeprom structure.
2548 Output is suitable for ftdi_write_eeprom().
2550 \param ftdi pointer to ftdi_context
2552 \retval >=0: size of eeprom user area in bytes
2553 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2554 \retval -2: Invalid eeprom or ftdi pointer
2555 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2556 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2557 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2558 \retval -6: No connected EEPROM or EEPROM Type unknown
2560 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2562 unsigned char i, j, eeprom_size_mask;
2563 unsigned short checksum, value;
2564 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2566 struct ftdi_eeprom *eeprom;
2567 unsigned char * output;
2570 ftdi_error_return(-2,"No context");
2571 if (ftdi->eeprom == NULL)
2572 ftdi_error_return(-2,"No eeprom structure");
2574 eeprom= ftdi->eeprom;
2575 output = eeprom->buf;
2577 if (eeprom->chip == -1)
2578 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2580 if (eeprom->size == -1)
2582 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2583 eeprom->size = 0x100;
2585 eeprom->size = 0x80;
2588 if (eeprom->manufacturer != NULL)
2589 manufacturer_size = strlen(eeprom->manufacturer);
2590 if (eeprom->product != NULL)
2591 product_size = strlen(eeprom->product);
2592 if (eeprom->serial != NULL)
2593 serial_size = strlen(eeprom->serial);
2595 // eeprom size check
2600 user_area_size = 96; // base size for strings (total of 48 characters)
2603 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2607 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2609 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2611 user_area_size = 86;
2614 user_area_size = 80;
2620 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2622 if (user_area_size < 0)
2623 ftdi_error_return(-1,"eeprom size exceeded");
2626 if (ftdi->type == TYPE_230X)
2628 /* FT230X have a reserved section in the middle of the MTP,
2629 which cannot be written to, but must be included in the checksum */
2630 memset(ftdi->eeprom->buf, 0, 0x80);
2631 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2635 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2638 // Bytes and Bits set for all Types
2640 // Addr 02: Vendor ID
2641 output[0x02] = eeprom->vendor_id;
2642 output[0x03] = eeprom->vendor_id >> 8;
2644 // Addr 04: Product ID
2645 output[0x04] = eeprom->product_id;
2646 output[0x05] = eeprom->product_id >> 8;
2648 // Addr 06: Device release number (0400h for BM features)
2649 output[0x06] = eeprom->release_number;
2650 output[0x07] = eeprom->release_number >> 8;
2652 // Addr 08: Config descriptor
2654 // Bit 6: 1 if this device is self powered, 0 if bus powered
2655 // Bit 5: 1 if this device uses remote wakeup
2656 // Bit 4-0: reserved - 0
2658 if (eeprom->self_powered)
2660 if (eeprom->remote_wakeup)
2664 // Addr 09: Max power consumption: max power = value * 2 mA
2665 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2667 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2669 // Addr 0A: Chip configuration
2670 // Bit 7: 0 - reserved
2671 // Bit 6: 0 - reserved
2672 // Bit 5: 0 - reserved
2673 // Bit 4: 1 - Change USB version
2674 // Bit 3: 1 - Use the serial number string
2675 // Bit 2: 1 - Enable suspend pull downs for lower power
2676 // Bit 1: 1 - Out EndPoint is Isochronous
2677 // Bit 0: 1 - In EndPoint is Isochronous
2680 if (eeprom->in_is_isochronous)
2682 if (eeprom->out_is_isochronous)
2688 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2689 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2710 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2711 eeprom_size_mask = eeprom->size -1;
2713 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2714 // Addr 0F: Length of manufacturer string
2715 // Output manufacturer
2716 output[0x0E] = i; // calculate offset
2717 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2718 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2719 for (j = 0; j < manufacturer_size; j++)
2721 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2722 output[i & eeprom_size_mask] = 0x00, i++;
2724 output[0x0F] = manufacturer_size*2 + 2;
2726 // Addr 10: Offset of the product string + 0x80, calculated later
2727 // Addr 11: Length of product string
2728 output[0x10] = i | 0x80; // calculate offset
2729 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2730 output[i & eeprom_size_mask] = 0x03, i++;
2731 for (j = 0; j < product_size; j++)
2733 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2734 output[i & eeprom_size_mask] = 0x00, i++;
2736 output[0x11] = product_size*2 + 2;
2738 // Addr 12: Offset of the serial string + 0x80, calculated later
2739 // Addr 13: Length of serial string
2740 output[0x12] = i | 0x80; // calculate offset
2741 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2742 output[i & eeprom_size_mask] = 0x03, i++;
2743 for (j = 0; j < serial_size; j++)
2745 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2746 output[i & eeprom_size_mask] = 0x00, i++;
2749 // Legacy port name and PnP fields for FT2232 and newer chips
2750 if (ftdi->type > TYPE_BM)
2752 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2754 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2756 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2760 output[0x13] = serial_size*2 + 2;
2762 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2764 if (eeprom->use_serial)
2765 output[0x0A] |= USE_SERIAL_NUM;
2767 output[0x0A] &= ~USE_SERIAL_NUM;
2770 /* Bytes and Bits specific to (some) types
2771 Write linear, as this allows easier fixing*/
2777 output[0x0C] = eeprom->usb_version & 0xff;
2778 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2779 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2780 output[0x0A] |= USE_USB_VERSION_BIT;
2782 output[0x0A] &= ~USE_USB_VERSION_BIT;
2787 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
2788 if ( eeprom->channel_a_driver == DRIVER_VCP)
2789 output[0x00] |= DRIVER_VCP;
2791 output[0x00] &= ~DRIVER_VCP;
2793 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2794 output[0x00] |= HIGH_CURRENT_DRIVE;
2796 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2798 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
2799 if ( eeprom->channel_b_driver == DRIVER_VCP)
2800 output[0x01] |= DRIVER_VCP;
2802 output[0x01] &= ~DRIVER_VCP;
2804 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2805 output[0x01] |= HIGH_CURRENT_DRIVE;
2807 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2809 if (eeprom->in_is_isochronous)
2810 output[0x0A] |= 0x1;
2812 output[0x0A] &= ~0x1;
2813 if (eeprom->out_is_isochronous)
2814 output[0x0A] |= 0x2;
2816 output[0x0A] &= ~0x2;
2817 if (eeprom->suspend_pull_downs)
2818 output[0x0A] |= 0x4;
2820 output[0x0A] &= ~0x4;
2821 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2822 output[0x0A] |= USE_USB_VERSION_BIT;
2824 output[0x0A] &= ~USE_USB_VERSION_BIT;
2826 output[0x0C] = eeprom->usb_version & 0xff;
2827 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2828 output[0x14] = eeprom->chip;
2831 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2832 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2833 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2835 if (eeprom->suspend_pull_downs)
2836 output[0x0A] |= 0x4;
2838 output[0x0A] &= ~0x4;
2839 output[0x0B] = eeprom->invert;
2840 output[0x0C] = eeprom->usb_version & 0xff;
2841 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2843 if (eeprom->cbus_function[0] > CBUS_BB)
2844 output[0x14] = CBUS_TXLED;
2846 output[0x14] = eeprom->cbus_function[0];
2848 if (eeprom->cbus_function[1] > CBUS_BB)
2849 output[0x14] |= CBUS_RXLED<<4;
2851 output[0x14] |= eeprom->cbus_function[1]<<4;
2853 if (eeprom->cbus_function[2] > CBUS_BB)
2854 output[0x15] = CBUS_TXDEN;
2856 output[0x15] = eeprom->cbus_function[2];
2858 if (eeprom->cbus_function[3] > CBUS_BB)
2859 output[0x15] |= CBUS_PWREN<<4;
2861 output[0x15] |= eeprom->cbus_function[3]<<4;
2863 if (eeprom->cbus_function[4] > CBUS_CLK6)
2864 output[0x16] = CBUS_SLEEP;
2866 output[0x16] = eeprom->cbus_function[4];
2869 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
2870 if ( eeprom->channel_a_driver == DRIVER_VCP)
2871 output[0x00] |= DRIVER_VCP;
2873 output[0x00] &= ~DRIVER_VCP;
2875 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
2876 if ( eeprom->channel_b_driver == DRIVER_VCP)
2877 output[0x01] |= DRIVER_VCP;
2879 output[0x01] &= ~DRIVER_VCP;
2880 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2881 output[0x01] |= SUSPEND_DBUS7_BIT;
2883 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2885 if (eeprom->suspend_pull_downs)
2886 output[0x0A] |= 0x4;
2888 output[0x0A] &= ~0x4;
2890 if (eeprom->group0_drive > DRIVE_16MA)
2891 output[0x0c] |= DRIVE_16MA;
2893 output[0x0c] |= eeprom->group0_drive;
2894 if (eeprom->group0_schmitt == IS_SCHMITT)
2895 output[0x0c] |= IS_SCHMITT;
2896 if (eeprom->group0_slew == SLOW_SLEW)
2897 output[0x0c] |= SLOW_SLEW;
2899 if (eeprom->group1_drive > DRIVE_16MA)
2900 output[0x0c] |= DRIVE_16MA<<4;
2902 output[0x0c] |= eeprom->group1_drive<<4;
2903 if (eeprom->group1_schmitt == IS_SCHMITT)
2904 output[0x0c] |= IS_SCHMITT<<4;
2905 if (eeprom->group1_slew == SLOW_SLEW)
2906 output[0x0c] |= SLOW_SLEW<<4;
2908 if (eeprom->group2_drive > DRIVE_16MA)
2909 output[0x0d] |= DRIVE_16MA;
2911 output[0x0d] |= eeprom->group2_drive;
2912 if (eeprom->group2_schmitt == IS_SCHMITT)
2913 output[0x0d] |= IS_SCHMITT;
2914 if (eeprom->group2_slew == SLOW_SLEW)
2915 output[0x0d] |= SLOW_SLEW;
2917 if (eeprom->group3_drive > DRIVE_16MA)
2918 output[0x0d] |= DRIVE_16MA<<4;
2920 output[0x0d] |= eeprom->group3_drive<<4;
2921 if (eeprom->group3_schmitt == IS_SCHMITT)
2922 output[0x0d] |= IS_SCHMITT<<4;
2923 if (eeprom->group3_slew == SLOW_SLEW)
2924 output[0x0d] |= SLOW_SLEW<<4;
2926 output[0x18] = eeprom->chip;
2930 if (eeprom->channel_a_driver == DRIVER_VCP)
2931 output[0x00] |= DRIVER_VCP;
2933 output[0x00] &= ~DRIVER_VCP;
2934 if (eeprom->channel_b_driver == DRIVER_VCP)
2935 output[0x01] |= DRIVER_VCP;
2937 output[0x01] &= ~DRIVER_VCP;
2938 if (eeprom->channel_c_driver == DRIVER_VCP)
2939 output[0x00] |= (DRIVER_VCP << 4);
2941 output[0x00] &= ~(DRIVER_VCP << 4);
2942 if (eeprom->channel_d_driver == DRIVER_VCP)
2943 output[0x01] |= (DRIVER_VCP << 4);
2945 output[0x01] &= ~(DRIVER_VCP << 4);
2947 if (eeprom->suspend_pull_downs)
2948 output[0x0a] |= 0x4;
2950 output[0x0a] &= ~0x4;
2952 if (eeprom->channel_a_rs485enable)
2953 output[0x0b] |= CHANNEL_IS_RS485 << 0;
2955 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
2956 if (eeprom->channel_b_rs485enable)
2957 output[0x0b] |= CHANNEL_IS_RS485 << 1;
2959 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
2960 if (eeprom->channel_c_rs485enable)
2961 output[0x0b] |= CHANNEL_IS_RS485 << 2;
2963 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
2964 if (eeprom->channel_d_rs485enable)
2965 output[0x0b] |= CHANNEL_IS_RS485 << 3;
2967 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
2969 if (eeprom->group0_drive > DRIVE_16MA)
2970 output[0x0c] |= DRIVE_16MA;
2972 output[0x0c] |= eeprom->group0_drive;
2973 if (eeprom->group0_schmitt == IS_SCHMITT)
2974 output[0x0c] |= IS_SCHMITT;
2975 if (eeprom->group0_slew == SLOW_SLEW)
2976 output[0x0c] |= SLOW_SLEW;
2978 if (eeprom->group1_drive > DRIVE_16MA)
2979 output[0x0c] |= DRIVE_16MA<<4;
2981 output[0x0c] |= eeprom->group1_drive<<4;
2982 if (eeprom->group1_schmitt == IS_SCHMITT)
2983 output[0x0c] |= IS_SCHMITT<<4;
2984 if (eeprom->group1_slew == SLOW_SLEW)
2985 output[0x0c] |= SLOW_SLEW<<4;
2987 if (eeprom->group2_drive > DRIVE_16MA)
2988 output[0x0d] |= DRIVE_16MA;
2990 output[0x0d] |= eeprom->group2_drive;
2991 if (eeprom->group2_schmitt == IS_SCHMITT)
2992 output[0x0d] |= IS_SCHMITT;
2993 if (eeprom->group2_slew == SLOW_SLEW)
2994 output[0x0d] |= SLOW_SLEW;
2996 if (eeprom->group3_drive > DRIVE_16MA)
2997 output[0x0d] |= DRIVE_16MA<<4;
2999 output[0x0d] |= eeprom->group3_drive<<4;
3000 if (eeprom->group3_schmitt == IS_SCHMITT)
3001 output[0x0d] |= IS_SCHMITT<<4;
3002 if (eeprom->group3_slew == SLOW_SLEW)
3003 output[0x0d] |= SLOW_SLEW<<4;
3005 output[0x18] = eeprom->chip;
3009 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3010 if ( eeprom->channel_a_driver == DRIVER_VCP)
3011 output[0x00] |= DRIVER_VCPH;
3013 output[0x00] &= ~DRIVER_VCPH;
3014 if (eeprom->powersave)
3015 output[0x01] |= POWER_SAVE_DISABLE_H;
3017 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3019 if (eeprom->suspend_pull_downs)
3020 output[0x0a] |= 0x4;
3022 output[0x0a] &= ~0x4;
3024 if (eeprom->clock_polarity)
3025 output[0x01] |= FT1284_CLK_IDLE_STATE;
3027 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3028 if (eeprom->data_order)
3029 output[0x01] |= FT1284_DATA_LSB;
3031 output[0x01] &= ~FT1284_DATA_LSB;
3032 if (eeprom->flow_control)
3033 output[0x01] |= FT1284_FLOW_CONTROL;
3035 output[0x01] &= ~FT1284_FLOW_CONTROL;
3036 if (eeprom->group0_drive > DRIVE_16MA)
3037 output[0x0c] |= DRIVE_16MA;
3039 output[0x0c] |= eeprom->group0_drive;
3040 if (eeprom->group0_schmitt == IS_SCHMITT)
3041 output[0x0c] |= IS_SCHMITT;
3042 if (eeprom->group0_slew == SLOW_SLEW)
3043 output[0x0c] |= SLOW_SLEW;
3045 if (eeprom->group1_drive > DRIVE_16MA)
3046 output[0x0d] |= DRIVE_16MA;
3048 output[0x0d] |= eeprom->group1_drive;
3049 if (eeprom->group1_schmitt == IS_SCHMITT)
3050 output[0x0d] |= IS_SCHMITT;
3051 if (eeprom->group1_slew == SLOW_SLEW)
3052 output[0x0d] |= SLOW_SLEW;
3054 set_ft232h_cbus(eeprom, output);
3056 output[0x1e] = eeprom->chip;
3057 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3060 output[0x00] = 0x80; /* Actually, leave the default value */
3061 output[0x0a] = 0x08; /* Enable USB Serial Number */
3062 output[0x0c] = (0x01) | (0x3 << 4); /* DBUS drive 4mA, CBUS drive 16mA */
3063 for (j = 0; j <= 6; j++)
3065 output[0x1a + j] = eeprom->cbus_function[j];
3070 // calculate checksum
3073 for (i = 0; i < eeprom->size/2-1; i++)
3075 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3077 /* FT230X has a user section in the MTP which is not part of the checksum */
3080 value = output[i*2];
3081 value += output[(i*2)+1] << 8;
3083 checksum = value^checksum;
3084 checksum = (checksum << 1) | (checksum >> 15);
3087 output[eeprom->size-2] = checksum;
3088 output[eeprom->size-1] = checksum >> 8;
3090 eeprom->initialized_for_connected_device = 1;
3091 return user_area_size;
3093 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3096 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3098 static unsigned char bit2type(unsigned char bits)
3102 case 0: return CHANNEL_IS_UART;
3103 case 1: return CHANNEL_IS_FIFO;
3104 case 2: return CHANNEL_IS_OPTO;
3105 case 4: return CHANNEL_IS_CPU;
3106 case 8: return CHANNEL_IS_FT1284;
3108 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3114 Decode binary EEPROM image into an ftdi_eeprom structure.
3116 \param ftdi pointer to ftdi_context
3117 \param verbose Decode EEPROM on stdout
3120 \retval -1: something went wrong
3122 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
3123 FIXME: Strings are malloc'ed here and should be freed somewhere
3125 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3128 unsigned short checksum, eeprom_checksum, value;
3129 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3131 struct ftdi_eeprom *eeprom;
3132 unsigned char *buf = NULL;
3135 ftdi_error_return(-1,"No context");
3136 if (ftdi->eeprom == NULL)
3137 ftdi_error_return(-1,"No eeprom structure");
3139 eeprom = ftdi->eeprom;
3140 eeprom_size = eeprom->size;
3141 buf = ftdi->eeprom->buf;
3143 // Addr 02: Vendor ID
3144 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3146 // Addr 04: Product ID
3147 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3149 // Addr 06: Device release number
3150 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3152 // Addr 08: Config descriptor
3154 // Bit 6: 1 if this device is self powered, 0 if bus powered
3155 // Bit 5: 1 if this device uses remote wakeup
3156 eeprom->self_powered = buf[0x08] & 0x40;
3157 eeprom->remote_wakeup = buf[0x08] & 0x20;
3159 // Addr 09: Max power consumption: max power = value * 2 mA
3160 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3162 // Addr 0A: Chip configuration
3163 // Bit 7: 0 - reserved
3164 // Bit 6: 0 - reserved
3165 // Bit 5: 0 - reserved
3166 // Bit 4: 1 - Change USB version on BM and 2232C
3167 // Bit 3: 1 - Use the serial number string
3168 // Bit 2: 1 - Enable suspend pull downs for lower power
3169 // Bit 1: 1 - Out EndPoint is Isochronous
3170 // Bit 0: 1 - In EndPoint is Isochronous
3172 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3173 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3174 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3175 eeprom->use_serial = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
3176 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
3178 // Addr 0C: USB version low byte when 0x0A
3179 // Addr 0D: USB version high byte when 0x0A
3180 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3182 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3183 // Addr 0F: Length of manufacturer string
3184 manufacturer_size = buf[0x0F]/2;
3185 if (eeprom->manufacturer)
3186 free(eeprom->manufacturer);
3187 if (manufacturer_size > 0)
3189 eeprom->manufacturer = malloc(manufacturer_size);
3190 if (eeprom->manufacturer)
3192 // Decode manufacturer
3193 i = buf[0x0E] & (eeprom_size -1); // offset
3194 for (j=0; j<manufacturer_size-1; j++)
3196 eeprom->manufacturer[j] = buf[2*j+i+2];
3198 eeprom->manufacturer[j] = '\0';
3201 else eeprom->manufacturer = NULL;
3203 // Addr 10: Offset of the product string + 0x80, calculated later
3204 // Addr 11: Length of product string
3205 if (eeprom->product)
3206 free(eeprom->product);
3207 product_size = buf[0x11]/2;
3208 if (product_size > 0)
3210 eeprom->product = malloc(product_size);
3211 if (eeprom->product)
3213 // Decode product name
3214 i = buf[0x10] & (eeprom_size -1); // offset
3215 for (j=0; j<product_size-1; j++)
3217 eeprom->product[j] = buf[2*j+i+2];
3219 eeprom->product[j] = '\0';
3222 else eeprom->product = NULL;
3224 // Addr 12: Offset of the serial string + 0x80, calculated later
3225 // Addr 13: Length of serial string
3227 free(eeprom->serial);
3228 serial_size = buf[0x13]/2;
3229 if (serial_size > 0)
3231 eeprom->serial = malloc(serial_size);
3235 i = buf[0x12] & (eeprom_size -1); // offset
3236 for (j=0; j<serial_size-1; j++)
3238 eeprom->serial[j] = buf[2*j+i+2];
3240 eeprom->serial[j] = '\0';
3243 else eeprom->serial = NULL;
3248 for (i = 0; i < eeprom_size/2-1; i++)
3250 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3252 /* FT230X has a user section in the MTP which is not part of the checksum */
3256 value += buf[(i*2)+1] << 8;
3258 checksum = value^checksum;
3259 checksum = (checksum << 1) | (checksum >> 15);
3262 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3264 if (eeprom_checksum != checksum)
3266 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3267 ftdi_error_return(-1,"EEPROM checksum error");
3270 eeprom->channel_a_type = 0;
3271 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3275 else if (ftdi->type == TYPE_2232C)
3277 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3278 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3279 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3280 eeprom->channel_b_type = buf[0x01] & 0x7;
3281 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3282 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3283 eeprom->chip = buf[0x14];
3285 else if (ftdi->type == TYPE_R)
3287 /* TYPE_R flags D2XX, not VCP as all others*/
3288 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3289 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3290 if ( (buf[0x01]&0x40) != 0x40)
3292 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3293 " If this happened with the\n"
3294 " EEPROM programmed by FTDI tools, please report "
3295 "to libftdi@developer.intra2net.com\n");
3297 eeprom->chip = buf[0x16];
3298 // Addr 0B: Invert data lines
3299 // Works only on FT232R, not FT245R, but no way to distinguish
3300 eeprom->invert = buf[0x0B];
3301 // Addr 14: CBUS function: CBUS0, CBUS1
3302 // Addr 15: CBUS function: CBUS2, CBUS3
3303 // Addr 16: CBUS function: CBUS5
3304 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3305 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3306 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3307 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3308 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3310 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3312 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3313 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3315 if (ftdi->type == TYPE_2232H)
3317 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3318 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3319 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3323 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3324 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3325 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3326 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3327 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3328 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3331 eeprom->chip = buf[0x18];
3332 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3333 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3334 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3335 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3336 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3337 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3338 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3339 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3340 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3341 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3342 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3343 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3345 else if (ftdi->type == TYPE_232H)
3349 eeprom->channel_a_type = buf[0x00] & 0xf;
3350 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3351 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3352 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3353 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3354 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3355 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3356 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3357 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3358 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3359 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3360 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3364 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3365 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3367 eeprom->chip = buf[0x1e];
3368 /*FIXME: Decipher more values*/
3370 else if (ftdi->type == TYPE_230X)
3374 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3376 eeprom->group0_drive = buf[0x0c] & 0x03;
3377 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3378 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3379 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3380 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3381 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3386 char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3387 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3388 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3389 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3391 if (eeprom->self_powered)
3392 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3394 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3395 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3396 if (eeprom->manufacturer)
3397 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3398 if (eeprom->product)
3399 fprintf(stdout, "Product: %s\n",eeprom->product);
3401 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3402 fprintf(stdout, "Checksum : %04x\n", checksum);
3403 if (ftdi->type == TYPE_R)
3404 fprintf(stdout, "Internal EEPROM\n");
3405 else if (eeprom->chip >= 0x46)
3406 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3407 if (eeprom->suspend_dbus7)
3408 fprintf(stdout, "Suspend on DBUS7\n");
3409 if (eeprom->suspend_pull_downs)
3410 fprintf(stdout, "Pull IO pins low during suspend\n");
3411 if(eeprom->powersave)
3413 if(ftdi->type >= TYPE_232H)
3414 fprintf(stdout,"Enter low power state on ACBUS7\n");
3416 if (eeprom->remote_wakeup)
3417 fprintf(stdout, "Enable Remote Wake Up\n");
3418 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3419 if (ftdi->type >= TYPE_2232C)
3420 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3421 channel_mode[eeprom->channel_a_type],
3422 (eeprom->channel_a_driver)?" VCP":"",
3423 (eeprom->high_current_a)?" High Current IO":"");
3424 if (ftdi->type >= TYPE_232H)
3426 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3427 (eeprom->clock_polarity)?"HIGH":"LOW",
3428 (eeprom->data_order)?"LSB":"MSB",
3429 (eeprom->flow_control)?"":"No ");
3431 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
3432 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3433 channel_mode[eeprom->channel_b_type],
3434 (eeprom->channel_b_driver)?" VCP":"",
3435 (eeprom->high_current_b)?" High Current IO":"");
3436 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3437 eeprom->use_usb_version == USE_USB_VERSION_BIT)
3438 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3440 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3442 fprintf(stdout,"%s has %d mA drive%s%s\n",
3443 (ftdi->type == TYPE_2232H)?"AL":"A",
3444 (eeprom->group0_drive+1) *4,
3445 (eeprom->group0_schmitt)?" Schmitt Input":"",
3446 (eeprom->group0_slew)?" Slow Slew":"");
3447 fprintf(stdout,"%s has %d mA drive%s%s\n",
3448 (ftdi->type == TYPE_2232H)?"AH":"B",
3449 (eeprom->group1_drive+1) *4,
3450 (eeprom->group1_schmitt)?" Schmitt Input":"",
3451 (eeprom->group1_slew)?" Slow Slew":"");
3452 fprintf(stdout,"%s has %d mA drive%s%s\n",
3453 (ftdi->type == TYPE_2232H)?"BL":"C",
3454 (eeprom->group2_drive+1) *4,
3455 (eeprom->group2_schmitt)?" Schmitt Input":"",
3456 (eeprom->group2_slew)?" Slow Slew":"");
3457 fprintf(stdout,"%s has %d mA drive%s%s\n",
3458 (ftdi->type == TYPE_2232H)?"BH":"D",
3459 (eeprom->group3_drive+1) *4,
3460 (eeprom->group3_schmitt)?" Schmitt Input":"",
3461 (eeprom->group3_slew)?" Slow Slew":"");
3463 else if (ftdi->type == TYPE_232H)
3466 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3467 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3468 "CLK30","CLK15","CLK7_5"
3470 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3471 (eeprom->group0_drive+1) *4,
3472 (eeprom->group0_schmitt)?" Schmitt Input":"",
3473 (eeprom->group0_slew)?" Slow Slew":"");
3474 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3475 (eeprom->group1_drive+1) *4,
3476 (eeprom->group1_schmitt)?" Schmitt Input":"",
3477 (eeprom->group1_slew)?" Slow Slew":"");
3478 for (i=0; i<10; i++)
3480 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3481 fprintf(stdout,"C%d Function: %s\n", i,
3482 cbush_mux[eeprom->cbus_function[i]]);
3485 else if (ftdi->type == TYPE_230X)
3488 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3489 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3490 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3491 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3492 "BBRD#", "TIME_STAMP", "AWAKE#",
3494 fprintf(stdout,"IOBUS has %d mA drive%s%s\n",
3495 (eeprom->group0_drive+1) *4,
3496 (eeprom->group0_schmitt)?" Schmitt Input":"",
3497 (eeprom->group0_slew)?" Slow Slew":"");
3498 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3499 (eeprom->group1_drive+1) *4,
3500 (eeprom->group1_schmitt)?" Schmitt Input":"",
3501 (eeprom->group1_slew)?" Slow Slew":"");
3504 if (eeprom->cbus_function[i]<= CBUSH_AWAKE)
3505 fprintf(stdout,"CBUS%d Function: %s\n", i, cbush_mux[eeprom->cbus_function[i]]);
3509 if (ftdi->type == TYPE_R)
3511 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3512 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3513 "IOMODE","BB_WR","BB_RD"
3515 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3519 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
3520 fprintf(stdout,"Inverted bits:");
3522 if ((eeprom->invert & (1<<i)) == (1<<i))
3523 fprintf(stdout," %s",r_bits[i]);
3524 fprintf(stdout,"\n");
3528 if (eeprom->cbus_function[i]<CBUS_BB)
3529 fprintf(stdout,"C%d Function: %s\n", i,
3530 cbus_mux[eeprom->cbus_function[i]]);
3534 /* Running MPROG show that C0..3 have fixed function Synchronous
3536 fprintf(stdout,"C%d BB Function: %s\n", i,
3539 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3548 Get a value from the decoded EEPROM structure
3550 \param ftdi pointer to ftdi_context
3551 \param value_name Enum of the value to query
3552 \param value Pointer to store read value
3555 \retval -1: Value doesn't exist
3557 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3562 *value = ftdi->eeprom->vendor_id;
3565 *value = ftdi->eeprom->product_id;
3567 case RELEASE_NUMBER:
3568 *value = ftdi->eeprom->release_number;
3571 *value = ftdi->eeprom->self_powered;
3574 *value = ftdi->eeprom->remote_wakeup;
3577 *value = ftdi->eeprom->is_not_pnp;
3580 *value = ftdi->eeprom->suspend_dbus7;
3582 case IN_IS_ISOCHRONOUS:
3583 *value = ftdi->eeprom->in_is_isochronous;
3585 case OUT_IS_ISOCHRONOUS:
3586 *value = ftdi->eeprom->out_is_isochronous;
3588 case SUSPEND_PULL_DOWNS:
3589 *value = ftdi->eeprom->suspend_pull_downs;
3592 *value = ftdi->eeprom->use_serial;
3595 *value = ftdi->eeprom->usb_version;
3597 case USE_USB_VERSION:
3598 *value = ftdi->eeprom->use_usb_version;
3601 *value = ftdi->eeprom->max_power;
3603 case CHANNEL_A_TYPE:
3604 *value = ftdi->eeprom->channel_a_type;
3606 case CHANNEL_B_TYPE:
3607 *value = ftdi->eeprom->channel_b_type;
3609 case CHANNEL_A_DRIVER:
3610 *value = ftdi->eeprom->channel_a_driver;
3612 case CHANNEL_B_DRIVER:
3613 *value = ftdi->eeprom->channel_b_driver;
3615 case CHANNEL_C_DRIVER:
3616 *value = ftdi->eeprom->channel_c_driver;
3618 case CHANNEL_D_DRIVER:
3619 *value = ftdi->eeprom->channel_d_driver;
3621 case CHANNEL_A_RS485:
3622 *value = ftdi->eeprom->channel_a_rs485enable;
3624 case CHANNEL_B_RS485:
3625 *value = ftdi->eeprom->channel_b_rs485enable;
3627 case CHANNEL_C_RS485:
3628 *value = ftdi->eeprom->channel_c_rs485enable;
3630 case CHANNEL_D_RS485:
3631 *value = ftdi->eeprom->channel_d_rs485enable;
3633 case CBUS_FUNCTION_0:
3634 *value = ftdi->eeprom->cbus_function[0];
3636 case CBUS_FUNCTION_1:
3637 *value = ftdi->eeprom->cbus_function[1];
3639 case CBUS_FUNCTION_2:
3640 *value = ftdi->eeprom->cbus_function[2];
3642 case CBUS_FUNCTION_3:
3643 *value = ftdi->eeprom->cbus_function[3];
3645 case CBUS_FUNCTION_4:
3646 *value = ftdi->eeprom->cbus_function[4];
3648 case CBUS_FUNCTION_5:
3649 *value = ftdi->eeprom->cbus_function[5];
3651 case CBUS_FUNCTION_6:
3652 *value = ftdi->eeprom->cbus_function[6];
3654 case CBUS_FUNCTION_7:
3655 *value = ftdi->eeprom->cbus_function[7];
3657 case CBUS_FUNCTION_8:
3658 *value = ftdi->eeprom->cbus_function[8];
3660 case CBUS_FUNCTION_9:
3661 *value = ftdi->eeprom->cbus_function[8];
3664 *value = ftdi->eeprom->high_current;
3666 case HIGH_CURRENT_A:
3667 *value = ftdi->eeprom->high_current_a;
3669 case HIGH_CURRENT_B:
3670 *value = ftdi->eeprom->high_current_b;
3673 *value = ftdi->eeprom->invert;
3676 *value = ftdi->eeprom->group0_drive;
3678 case GROUP0_SCHMITT:
3679 *value = ftdi->eeprom->group0_schmitt;
3682 *value = ftdi->eeprom->group0_slew;
3685 *value = ftdi->eeprom->group1_drive;
3687 case GROUP1_SCHMITT:
3688 *value = ftdi->eeprom->group1_schmitt;
3691 *value = ftdi->eeprom->group1_slew;
3694 *value = ftdi->eeprom->group2_drive;
3696 case GROUP2_SCHMITT:
3697 *value = ftdi->eeprom->group2_schmitt;
3700 *value = ftdi->eeprom->group2_slew;
3703 *value = ftdi->eeprom->group3_drive;
3705 case GROUP3_SCHMITT:
3706 *value = ftdi->eeprom->group3_schmitt;
3709 *value = ftdi->eeprom->group3_slew;
3712 *value = ftdi->eeprom->powersave;
3714 case CLOCK_POLARITY:
3715 *value = ftdi->eeprom->clock_polarity;
3718 *value = ftdi->eeprom->data_order;
3721 *value = ftdi->eeprom->flow_control;
3724 *value = ftdi->eeprom->chip;
3727 *value = ftdi->eeprom->size;
3730 ftdi_error_return(-1, "Request for unknown EEPROM value");
3736 Set a value in the decoded EEPROM Structure
3737 No parameter checking is performed
3739 \param ftdi pointer to ftdi_context
3740 \param value_name Enum of the value to set
3744 \retval -1: Value doesn't exist
3745 \retval -2: Value not user settable
3747 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3752 ftdi->eeprom->vendor_id = value;
3755 ftdi->eeprom->product_id = value;
3757 case RELEASE_NUMBER:
3758 ftdi->eeprom->release_number = value;
3761 ftdi->eeprom->self_powered = value;
3764 ftdi->eeprom->remote_wakeup = value;
3767 ftdi->eeprom->is_not_pnp = value;
3770 ftdi->eeprom->suspend_dbus7 = value;
3772 case IN_IS_ISOCHRONOUS:
3773 ftdi->eeprom->in_is_isochronous = value;
3775 case OUT_IS_ISOCHRONOUS:
3776 ftdi->eeprom->out_is_isochronous = value;
3778 case SUSPEND_PULL_DOWNS:
3779 ftdi->eeprom->suspend_pull_downs = value;
3782 ftdi->eeprom->use_serial = value;
3785 ftdi->eeprom->usb_version = value;
3787 case USE_USB_VERSION:
3788 ftdi->eeprom->use_usb_version = value;
3791 ftdi->eeprom->max_power = value;
3793 case CHANNEL_A_TYPE:
3794 ftdi->eeprom->channel_a_type = value;
3796 case CHANNEL_B_TYPE:
3797 ftdi->eeprom->channel_b_type = value;
3799 case CHANNEL_A_DRIVER:
3800 ftdi->eeprom->channel_a_driver = value;
3802 case CHANNEL_B_DRIVER:
3803 ftdi->eeprom->channel_b_driver = value;
3805 case CHANNEL_C_DRIVER:
3806 ftdi->eeprom->channel_c_driver = value;
3808 case CHANNEL_D_DRIVER:
3809 ftdi->eeprom->channel_d_driver = value;
3811 case CHANNEL_A_RS485:
3812 ftdi->eeprom->channel_a_rs485enable = value;
3814 case CHANNEL_B_RS485:
3815 ftdi->eeprom->channel_b_rs485enable = value;
3817 case CHANNEL_C_RS485:
3818 ftdi->eeprom->channel_c_rs485enable = value;
3820 case CHANNEL_D_RS485:
3821 ftdi->eeprom->channel_d_rs485enable = value;
3823 case CBUS_FUNCTION_0:
3824 ftdi->eeprom->cbus_function[0] = value;
3826 case CBUS_FUNCTION_1:
3827 ftdi->eeprom->cbus_function[1] = value;
3829 case CBUS_FUNCTION_2:
3830 ftdi->eeprom->cbus_function[2] = value;
3832 case CBUS_FUNCTION_3:
3833 ftdi->eeprom->cbus_function[3] = value;
3835 case CBUS_FUNCTION_4:
3836 ftdi->eeprom->cbus_function[4] = value;
3838 case CBUS_FUNCTION_5:
3839 ftdi->eeprom->cbus_function[5] = value;
3841 case CBUS_FUNCTION_6:
3842 ftdi->eeprom->cbus_function[6] = value;
3844 case CBUS_FUNCTION_7:
3845 ftdi->eeprom->cbus_function[7] = value;
3847 case CBUS_FUNCTION_8:
3848 ftdi->eeprom->cbus_function[8] = value;
3850 case CBUS_FUNCTION_9:
3851 ftdi->eeprom->cbus_function[9] = value;
3854 ftdi->eeprom->high_current = value;
3856 case HIGH_CURRENT_A:
3857 ftdi->eeprom->high_current_a = value;
3859 case HIGH_CURRENT_B:
3860 ftdi->eeprom->high_current_b = value;
3863 ftdi->eeprom->invert = value;
3866 ftdi->eeprom->group0_drive = value;
3868 case GROUP0_SCHMITT:
3869 ftdi->eeprom->group0_schmitt = value;
3872 ftdi->eeprom->group0_slew = value;
3875 ftdi->eeprom->group1_drive = value;
3877 case GROUP1_SCHMITT:
3878 ftdi->eeprom->group1_schmitt = value;
3881 ftdi->eeprom->group1_slew = value;
3884 ftdi->eeprom->group2_drive = value;
3886 case GROUP2_SCHMITT:
3887 ftdi->eeprom->group2_schmitt = value;
3890 ftdi->eeprom->group2_slew = value;
3893 ftdi->eeprom->group3_drive = value;
3895 case GROUP3_SCHMITT:
3896 ftdi->eeprom->group3_schmitt = value;
3899 ftdi->eeprom->group3_slew = value;
3902 ftdi->eeprom->chip = value;
3905 ftdi->eeprom->powersave = value;
3907 case CLOCK_POLARITY:
3908 ftdi->eeprom->clock_polarity = value;
3911 ftdi->eeprom->data_order = value;
3914 ftdi->eeprom->flow_control = value;
3917 ftdi_error_return(-2, "EEPROM Value can't be changed");
3919 ftdi_error_return(-1, "Request to unknown EEPROM value");
3921 ftdi->eeprom->initialized_for_connected_device = 0;
3925 /** Get the read-only buffer to the binary EEPROM content
3927 \param ftdi pointer to ftdi_context
3928 \param buf buffer to receive EEPROM content
3929 \param size Size of receiving buffer
3932 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3933 \retval -2: Not enough room to store eeprom
3935 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3937 if (!ftdi || !(ftdi->eeprom))
3938 ftdi_error_return(-1, "No appropriate structure");
3940 if (!buf || size < ftdi->eeprom->size)
3941 ftdi_error_return(-1, "Not enough room to store eeprom");
3943 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3944 if (size > FTDI_MAX_EEPROM_SIZE)
3945 size = FTDI_MAX_EEPROM_SIZE;
3947 memcpy(buf, ftdi->eeprom->buf, size);
3952 /** Set the EEPROM content from the user-supplied prefilled buffer
3954 \param ftdi pointer to ftdi_context
3955 \param buf buffer to read EEPROM content
3956 \param size Size of buffer
3959 \retval -1: struct ftdi_contxt or ftdi_eeprom of buf missing
3961 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
3963 if (!ftdi || !(ftdi->eeprom) || !buf)
3964 ftdi_error_return(-1, "No appropriate structure");
3966 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3967 if (size > FTDI_MAX_EEPROM_SIZE)
3968 size = FTDI_MAX_EEPROM_SIZE;
3970 memcpy(ftdi->eeprom->buf, buf, size);
3976 Read eeprom location
3978 \param ftdi pointer to ftdi_context
3979 \param eeprom_addr Address of eeprom location to be read
3980 \param eeprom_val Pointer to store read eeprom location
3983 \retval -1: read failed
3984 \retval -2: USB device unavailable
3986 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3988 if (ftdi == NULL || ftdi->usb_dev == NULL)
3989 ftdi_error_return(-2, "USB device unavailable");
3991 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)
3992 ftdi_error_return(-1, "reading eeprom failed");
4000 \param ftdi pointer to ftdi_context
4003 \retval -1: read failed
4004 \retval -2: USB device unavailable
4006 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4011 if (ftdi == NULL || ftdi->usb_dev == NULL)
4012 ftdi_error_return(-2, "USB device unavailable");
4013 buf = ftdi->eeprom->buf;
4015 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4017 if (libusb_control_transfer(
4018 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4019 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4020 ftdi_error_return(-1, "reading eeprom failed");
4023 if (ftdi->type == TYPE_R)
4024 ftdi->eeprom->size = 0x80;
4025 /* Guesses size of eeprom by comparing halves
4026 - will not work with blank eeprom */
4027 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4028 ftdi->eeprom->size = -1;
4029 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4030 ftdi->eeprom->size = 0x80;
4031 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4032 ftdi->eeprom->size = 0x40;
4034 ftdi->eeprom->size = 0x100;
4039 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4040 Function is only used internally
4043 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4045 return ((value & 1) << 1) |
4046 ((value & 2) << 5) |
4047 ((value & 4) >> 2) |
4048 ((value & 8) << 4) |
4049 ((value & 16) >> 1) |
4050 ((value & 32) >> 1) |
4051 ((value & 64) >> 4) |
4052 ((value & 128) >> 2);
4056 Read the FTDIChip-ID from R-type devices
4058 \param ftdi pointer to ftdi_context
4059 \param chipid Pointer to store FTDIChip-ID
4062 \retval -1: read failed
4063 \retval -2: USB device unavailable
4065 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4067 unsigned int a = 0, b = 0;
4069 if (ftdi == NULL || ftdi->usb_dev == NULL)
4070 ftdi_error_return(-2, "USB device unavailable");
4072 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)
4074 a = a << 8 | a >> 8;
4075 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)
4077 b = b << 8 | b >> 8;
4078 a = (a << 16) | (b & 0xFFFF);
4079 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4080 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4081 *chipid = a ^ 0xa5f0f7d1;
4086 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4090 Write eeprom location
4092 \param ftdi pointer to ftdi_context
4093 \param eeprom_addr Address of eeprom location to be written
4094 \param eeprom_val Value to be written
4097 \retval -1: write failed
4098 \retval -2: USB device unavailable
4099 \retval -3: Invalid access to checksum protected area below 0x80
4100 \retval -4: Device can't access unprotected area
4101 \retval -5: Reading chip type failed
4103 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4104 unsigned short eeprom_val)
4106 int chip_type_location;
4107 unsigned short chip_type;
4109 if (ftdi == NULL || ftdi->usb_dev == NULL)
4110 ftdi_error_return(-2, "USB device unavailable");
4112 if (eeprom_addr <0x80)
4113 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4120 chip_type_location = 0x14;
4124 chip_type_location = 0x18;
4127 chip_type_location = 0x1e;
4130 ftdi_error_return(-4, "Device can't access unprotected area");
4133 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4134 ftdi_error_return(-5, "Reading failed");
4135 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4136 if ((chip_type & 0xff) != 0x66)
4138 ftdi_error_return(-6, "EEPROM is not of 93x66");
4141 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4142 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4143 NULL, 0, ftdi->usb_write_timeout) != 0)
4144 ftdi_error_return(-1, "unable to write eeprom");
4152 \param ftdi pointer to ftdi_context
4155 \retval -1: read failed
4156 \retval -2: USB device unavailable
4157 \retval -3: EEPROM not initialized for the connected device;
4159 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4161 unsigned short usb_val, status;
4163 unsigned char *eeprom;
4165 if (ftdi == NULL || ftdi->usb_dev == NULL)
4166 ftdi_error_return(-2, "USB device unavailable");
4168 if(ftdi->eeprom->initialized_for_connected_device == 0)
4169 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4171 eeprom = ftdi->eeprom->buf;
4173 /* These commands were traced while running MProg */
4174 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4176 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4178 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4181 for (i = 0; i < ftdi->eeprom->size/2; i++)
4183 /* Do not try to write to reserved area */
4184 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4188 usb_val = eeprom[i*2];
4189 usb_val += eeprom[(i*2)+1] << 8;
4190 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4191 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4192 NULL, 0, ftdi->usb_write_timeout) < 0)
4193 ftdi_error_return(-1, "unable to write eeprom");
4202 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
4204 \param ftdi pointer to ftdi_context
4207 \retval -1: erase failed
4208 \retval -2: USB device unavailable
4209 \retval -3: Writing magic failed
4210 \retval -4: Read EEPROM failed
4211 \retval -5: Unexpected EEPROM value
4213 #define MAGIC 0x55aa
4214 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4216 unsigned short eeprom_value;
4217 if (ftdi == NULL || ftdi->usb_dev == NULL)
4218 ftdi_error_return(-2, "USB device unavailable");
4220 if (ftdi->type == TYPE_R)
4222 ftdi->eeprom->chip = 0;
4226 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4227 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4228 ftdi_error_return(-1, "unable to erase eeprom");
4231 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4232 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4233 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4234 Chip is 93x66 if magic is only read at word position 0xc0*/
4235 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4236 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4237 NULL, 0, ftdi->usb_write_timeout) != 0)
4238 ftdi_error_return(-3, "Writing magic failed");
4239 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4240 ftdi_error_return(-4, "Reading failed");
4241 if (eeprom_value == MAGIC)
4243 ftdi->eeprom->chip = 0x46;
4247 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4248 ftdi_error_return(-4, "Reading failed");
4249 if (eeprom_value == MAGIC)
4250 ftdi->eeprom->chip = 0x56;
4253 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4254 ftdi_error_return(-4, "Reading failed");
4255 if (eeprom_value == MAGIC)
4256 ftdi->eeprom->chip = 0x66;
4259 ftdi->eeprom->chip = -1;
4263 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4264 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4265 ftdi_error_return(-1, "unable to erase eeprom");
4270 Get string representation for last error code
4272 \param ftdi pointer to ftdi_context
4274 \retval Pointer to error string
4276 char *ftdi_get_error_string (struct ftdi_context *ftdi)
4281 return ftdi->error_str;
4284 /* @} end of doxygen libftdi group */