1 /***************************************************************************
5 copyright : (C) 2003-2011 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 #define ftdi_error_return(code, str) do { \
40 ftdi->error_str = str; \
44 #define ftdi_error_return_free_device_list(code, str, devs) do { \
45 libusb_free_device_list(devs,1); \
46 ftdi->error_str = str; \
52 Internal function to close usb device pointer.
53 Sets ftdi->usb_dev to NULL.
56 \param ftdi pointer to ftdi_context
60 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
62 if (ftdi && ftdi->usb_dev)
64 libusb_close (ftdi->usb_dev);
70 Initializes a ftdi_context.
72 \param ftdi pointer to ftdi_context
75 \retval -1: couldn't allocate read buffer
76 \retval -2: couldn't allocate struct buffer
77 \retval -3: libusb_init() failed
79 \remark This should be called before all functions
81 int ftdi_init(struct ftdi_context *ftdi)
83 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
86 ftdi->usb_read_timeout = 5000;
87 ftdi->usb_write_timeout = 5000;
89 ftdi->type = TYPE_BM; /* chip type */
91 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
93 ftdi->readbuffer = NULL;
94 ftdi->readbuffer_offset = 0;
95 ftdi->readbuffer_remaining = 0;
96 ftdi->writebuffer_chunksize = 4096;
97 ftdi->max_packet_size = 0;
98 ftdi->error_str = NULL;
99 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
101 if (libusb_init(&ftdi->usb_ctx) < 0)
102 ftdi_error_return(-3, "libusb_init() failed");
104 ftdi_set_interface(ftdi, INTERFACE_ANY);
105 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
108 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
109 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
110 ftdi->eeprom = eeprom;
112 /* All fine. Now allocate the readbuffer */
113 return ftdi_read_data_set_chunksize(ftdi, 4096);
117 Allocate and initialize a new ftdi_context
119 \return a pointer to a new ftdi_context, or NULL on failure
121 struct ftdi_context *ftdi_new(void)
123 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
130 if (ftdi_init(ftdi) != 0)
140 Open selected channels on a chip, otherwise use first channel.
142 \param ftdi pointer to ftdi_context
143 \param interface Interface to use for FT2232C/2232H/4232H chips.
146 \retval -1: unknown interface
147 \retval -2: USB device unavailable
149 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
152 ftdi_error_return(-2, "USB device unavailable");
159 ftdi->index = INTERFACE_A;
165 ftdi->index = INTERFACE_B;
171 ftdi->index = INTERFACE_C;
177 ftdi->index = INTERFACE_D;
182 ftdi_error_return(-1, "Unknown interface");
188 Deinitializes a ftdi_context.
190 \param ftdi pointer to ftdi_context
192 void ftdi_deinit(struct ftdi_context *ftdi)
197 ftdi_usb_close_internal (ftdi);
199 if (ftdi->readbuffer != NULL)
201 free(ftdi->readbuffer);
202 ftdi->readbuffer = NULL;
205 if (ftdi->eeprom != NULL)
207 if (ftdi->eeprom->manufacturer != 0)
209 free(ftdi->eeprom->manufacturer);
210 ftdi->eeprom->manufacturer = 0;
212 if (ftdi->eeprom->product != 0)
214 free(ftdi->eeprom->product);
215 ftdi->eeprom->product = 0;
217 if (ftdi->eeprom->serial != 0)
219 free(ftdi->eeprom->serial);
220 ftdi->eeprom->serial = 0;
228 libusb_exit(ftdi->usb_ctx);
229 ftdi->usb_ctx = NULL;
234 Deinitialize and free an ftdi_context.
236 \param ftdi pointer to ftdi_context
238 void ftdi_free(struct ftdi_context *ftdi)
245 Use an already open libusb device.
247 \param ftdi pointer to ftdi_context
248 \param usb libusb libusb_device_handle to use
250 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
260 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
261 needs to be deallocated by ftdi_list_free() after use.
263 \param ftdi pointer to ftdi_context
264 \param devlist Pointer where to store list of found devices
265 \param vendor Vendor ID to search for
266 \param product Product ID to search for
268 \retval >0: number of devices found
269 \retval -3: out of memory
270 \retval -5: libusb_get_device_list() failed
271 \retval -6: libusb_get_device_descriptor() failed
273 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
275 struct ftdi_device_list **curdev;
277 libusb_device **devs;
281 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
282 ftdi_error_return(-5, "libusb_get_device_list() failed");
287 while ((dev = devs[i++]) != NULL)
289 struct libusb_device_descriptor desc;
291 if (libusb_get_device_descriptor(dev, &desc) < 0)
292 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
294 if (desc.idVendor == vendor && desc.idProduct == product)
296 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
298 ftdi_error_return_free_device_list(-3, "out of memory", devs);
300 (*curdev)->next = NULL;
301 (*curdev)->dev = dev;
302 libusb_ref_device(dev);
303 curdev = &(*curdev)->next;
307 libusb_free_device_list(devs,1);
312 Frees a usb device list.
314 \param devlist USB device list created by ftdi_usb_find_all()
316 void ftdi_list_free(struct ftdi_device_list **devlist)
318 struct ftdi_device_list *curdev, *next;
320 for (curdev = *devlist; curdev != NULL;)
323 libusb_unref_device(curdev->dev);
332 Frees a usb device list.
334 \param devlist USB device list created by ftdi_usb_find_all()
336 void ftdi_list_free2(struct ftdi_device_list *devlist)
338 ftdi_list_free(&devlist);
342 Return device ID strings from the usb device.
344 The parameters manufacturer, description and serial may be NULL
345 or pointer to buffers to store the fetched strings.
347 \note Use this function only in combination with ftdi_usb_find_all()
348 as it closes the internal "usb_dev" after use.
350 \param ftdi pointer to ftdi_context
351 \param dev libusb usb_dev to use
352 \param manufacturer Store manufacturer string here if not NULL
353 \param mnf_len Buffer size of manufacturer string
354 \param description Store product description string here if not NULL
355 \param desc_len Buffer size of product description string
356 \param serial Store serial string here if not NULL
357 \param serial_len Buffer size of serial string
360 \retval -1: wrong arguments
361 \retval -4: unable to open device
362 \retval -7: get product manufacturer failed
363 \retval -8: get product description failed
364 \retval -9: get serial number failed
365 \retval -11: libusb_get_device_descriptor() failed
367 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
368 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
370 struct libusb_device_descriptor desc;
372 if ((ftdi==NULL) || (dev==NULL))
375 if (libusb_open(dev, &ftdi->usb_dev) < 0)
376 ftdi_error_return(-4, "libusb_open() failed");
378 if (libusb_get_device_descriptor(dev, &desc) < 0)
379 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
381 if (manufacturer != NULL)
383 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
385 ftdi_usb_close_internal (ftdi);
386 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
390 if (description != NULL)
392 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
394 ftdi_usb_close_internal (ftdi);
395 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
401 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
403 ftdi_usb_close_internal (ftdi);
404 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
408 ftdi_usb_close_internal (ftdi);
414 * Internal function to determine the maximum packet size.
415 * \param ftdi pointer to ftdi_context
416 * \param dev libusb usb_dev to use
417 * \retval Maximum packet size for this device
419 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
421 struct libusb_device_descriptor desc;
422 struct libusb_config_descriptor *config0;
423 unsigned int packet_size;
426 if (ftdi == NULL || dev == NULL)
429 // Determine maximum packet size. Init with default value.
430 // New hi-speed devices from FTDI use a packet size of 512 bytes
431 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
432 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
437 if (libusb_get_device_descriptor(dev, &desc) < 0)
440 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
443 if (desc.bNumConfigurations > 0)
445 if (ftdi->interface < config0->bNumInterfaces)
447 struct libusb_interface interface = config0->interface[ftdi->interface];
448 if (interface.num_altsetting > 0)
450 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
451 if (descriptor.bNumEndpoints > 0)
453 packet_size = descriptor.endpoint[0].wMaxPacketSize;
459 libusb_free_config_descriptor (config0);
464 Opens a ftdi device given by an usb_device.
466 \param ftdi pointer to ftdi_context
467 \param dev libusb usb_dev to use
470 \retval -3: unable to config device
471 \retval -4: unable to open device
472 \retval -5: unable to claim device
473 \retval -6: reset failed
474 \retval -7: set baudrate failed
475 \retval -8: ftdi context invalid
476 \retval -9: libusb_get_device_descriptor() failed
477 \retval -10: libusb_get_config_descriptor() failed
478 \retval -11: libusb_detach_kernel_driver() failed
479 \retval -12: libusb_get_configuration() failed
481 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
483 struct libusb_device_descriptor desc;
484 struct libusb_config_descriptor *config0;
485 int cfg, cfg0, detach_errno = 0;
488 ftdi_error_return(-8, "ftdi context invalid");
490 if (libusb_open(dev, &ftdi->usb_dev) < 0)
491 ftdi_error_return(-4, "libusb_open() failed");
493 if (libusb_get_device_descriptor(dev, &desc) < 0)
494 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
496 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
497 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
498 cfg0 = config0->bConfigurationValue;
499 libusb_free_config_descriptor (config0);
501 // Try to detach ftdi_sio kernel module.
503 // The return code is kept in a separate variable and only parsed
504 // if usb_set_configuration() or usb_claim_interface() fails as the
505 // detach operation might be denied and everything still works fine.
506 // Likely scenario is a static ftdi_sio kernel module.
507 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
509 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
510 detach_errno = errno;
513 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
514 ftdi_error_return(-12, "libusb_get_configuration () failed");
515 // set configuration (needed especially for windows)
516 // tolerate EBUSY: one device with one configuration, but two interfaces
517 // and libftdi sessions to both interfaces (e.g. FT2232)
518 if (desc.bNumConfigurations > 0 && cfg != cfg0)
520 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
522 ftdi_usb_close_internal (ftdi);
523 if (detach_errno == EPERM)
525 ftdi_error_return(-8, "inappropriate permissions on device!");
529 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
534 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
536 ftdi_usb_close_internal (ftdi);
537 if (detach_errno == EPERM)
539 ftdi_error_return(-8, "inappropriate permissions on device!");
543 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
547 if (ftdi_usb_reset (ftdi) != 0)
549 ftdi_usb_close_internal (ftdi);
550 ftdi_error_return(-6, "ftdi_usb_reset failed");
553 // Try to guess chip type
554 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
555 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
556 && desc.iSerialNumber == 0))
557 ftdi->type = TYPE_BM;
558 else if (desc.bcdDevice == 0x200)
559 ftdi->type = TYPE_AM;
560 else if (desc.bcdDevice == 0x500)
561 ftdi->type = TYPE_2232C;
562 else if (desc.bcdDevice == 0x600)
564 else if (desc.bcdDevice == 0x700)
565 ftdi->type = TYPE_2232H;
566 else if (desc.bcdDevice == 0x800)
567 ftdi->type = TYPE_4232H;
568 else if (desc.bcdDevice == 0x900)
569 ftdi->type = TYPE_232H;
571 // Determine maximum packet size
572 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
574 if (ftdi_set_baudrate (ftdi, 9600) != 0)
576 ftdi_usb_close_internal (ftdi);
577 ftdi_error_return(-7, "set baudrate failed");
580 ftdi_error_return(0, "all fine");
584 Opens the first device with a given vendor and product ids.
586 \param ftdi pointer to ftdi_context
587 \param vendor Vendor ID
588 \param product Product ID
590 \retval same as ftdi_usb_open_desc()
592 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
594 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
598 Opens the first device with a given, vendor id, product id,
599 description and serial.
601 \param ftdi pointer to ftdi_context
602 \param vendor Vendor ID
603 \param product Product ID
604 \param description Description to search for. Use NULL if not needed.
605 \param serial Serial to search for. Use NULL if not needed.
608 \retval -3: usb device not found
609 \retval -4: unable to open device
610 \retval -5: unable to claim device
611 \retval -6: reset failed
612 \retval -7: set baudrate failed
613 \retval -8: get product description failed
614 \retval -9: get serial number failed
615 \retval -12: libusb_get_device_list() failed
616 \retval -13: libusb_get_device_descriptor() failed
618 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
619 const char* description, const char* serial)
621 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
625 Opens the index-th device with a given, vendor id, product id,
626 description and serial.
628 \param ftdi pointer to ftdi_context
629 \param vendor Vendor ID
630 \param product Product ID
631 \param description Description to search for. Use NULL if not needed.
632 \param serial Serial to search for. Use NULL if not needed.
633 \param index Number of matching device to open if there are more than one, starts with 0.
636 \retval -1: usb_find_busses() failed
637 \retval -2: usb_find_devices() failed
638 \retval -3: usb device not found
639 \retval -4: unable to open device
640 \retval -5: unable to claim device
641 \retval -6: reset failed
642 \retval -7: set baudrate failed
643 \retval -8: get product description failed
644 \retval -9: get serial number failed
645 \retval -10: unable to close device
646 \retval -11: ftdi context invalid
648 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
649 const char* description, const char* serial, unsigned int index)
652 libusb_device **devs;
657 ftdi_error_return(-11, "ftdi context invalid");
659 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
660 ftdi_error_return(-12, "libusb_get_device_list() failed");
662 while ((dev = devs[i++]) != NULL)
664 struct libusb_device_descriptor desc;
667 if (libusb_get_device_descriptor(dev, &desc) < 0)
668 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
670 if (desc.idVendor == vendor && desc.idProduct == product)
672 if (libusb_open(dev, &ftdi->usb_dev) < 0)
673 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
675 if (description != NULL)
677 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
679 ftdi_usb_close_internal (ftdi);
680 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
682 if (strncmp(string, description, sizeof(string)) != 0)
684 ftdi_usb_close_internal (ftdi);
690 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
692 ftdi_usb_close_internal (ftdi);
693 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
695 if (strncmp(string, serial, sizeof(string)) != 0)
697 ftdi_usb_close_internal (ftdi);
702 ftdi_usb_close_internal (ftdi);
710 res = ftdi_usb_open_dev(ftdi, dev);
711 libusb_free_device_list(devs,1);
717 ftdi_error_return_free_device_list(-3, "device not found", devs);
721 Opens the ftdi-device described by a description-string.
722 Intended to be used for parsing a device-description given as commandline argument.
724 \param ftdi pointer to ftdi_context
725 \param description NULL-terminated description-string, using this format:
726 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
727 \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")
728 \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
729 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
731 \note The description format may be extended in later versions.
734 \retval -2: libusb_get_device_list() failed
735 \retval -3: usb device not found
736 \retval -4: unable to open device
737 \retval -5: unable to claim device
738 \retval -6: reset failed
739 \retval -7: set baudrate failed
740 \retval -8: get product description failed
741 \retval -9: get serial number failed
742 \retval -10: unable to close device
743 \retval -11: illegal description format
744 \retval -12: ftdi context invalid
746 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
749 ftdi_error_return(-12, "ftdi context invalid");
751 if (description[0] == 0 || description[1] != ':')
752 ftdi_error_return(-11, "illegal description format");
754 if (description[0] == 'd')
757 libusb_device **devs;
758 unsigned int bus_number, device_address;
761 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
762 ftdi_error_return(-2, "libusb_get_device_list() failed");
764 /* XXX: This doesn't handle symlinks/odd paths/etc... */
765 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
766 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
768 while ((dev = devs[i++]) != NULL)
771 if (bus_number == libusb_get_bus_number (dev)
772 && device_address == libusb_get_device_address (dev))
774 ret = ftdi_usb_open_dev(ftdi, dev);
775 libusb_free_device_list(devs,1);
781 ftdi_error_return_free_device_list(-3, "device not found", devs);
783 else if (description[0] == 'i' || description[0] == 's')
786 unsigned int product;
787 unsigned int index=0;
788 const char *serial=NULL;
789 const char *startp, *endp;
792 startp=description+2;
793 vendor=strtoul((char*)startp,(char**)&endp,0);
794 if (*endp != ':' || endp == startp || errno != 0)
795 ftdi_error_return(-11, "illegal description format");
798 product=strtoul((char*)startp,(char**)&endp,0);
799 if (endp == startp || errno != 0)
800 ftdi_error_return(-11, "illegal description format");
802 if (description[0] == 'i' && *endp != 0)
804 /* optional index field in i-mode */
806 ftdi_error_return(-11, "illegal description format");
809 index=strtoul((char*)startp,(char**)&endp,0);
810 if (*endp != 0 || endp == startp || errno != 0)
811 ftdi_error_return(-11, "illegal description format");
813 if (description[0] == 's')
816 ftdi_error_return(-11, "illegal description format");
818 /* rest of the description is the serial */
822 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
826 ftdi_error_return(-11, "illegal description format");
831 Resets the ftdi device.
833 \param ftdi pointer to ftdi_context
836 \retval -1: FTDI reset failed
837 \retval -2: USB device unavailable
839 int ftdi_usb_reset(struct ftdi_context *ftdi)
841 if (ftdi == NULL || ftdi->usb_dev == NULL)
842 ftdi_error_return(-2, "USB device unavailable");
844 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
845 SIO_RESET_REQUEST, SIO_RESET_SIO,
846 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
847 ftdi_error_return(-1,"FTDI reset failed");
849 // Invalidate data in the readbuffer
850 ftdi->readbuffer_offset = 0;
851 ftdi->readbuffer_remaining = 0;
857 Clears the read buffer on the chip and the internal read buffer.
859 \param ftdi pointer to ftdi_context
862 \retval -1: read buffer purge failed
863 \retval -2: USB device unavailable
865 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
867 if (ftdi == NULL || ftdi->usb_dev == NULL)
868 ftdi_error_return(-2, "USB device unavailable");
870 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
871 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
872 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
873 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
875 // Invalidate data in the readbuffer
876 ftdi->readbuffer_offset = 0;
877 ftdi->readbuffer_remaining = 0;
883 Clears the write buffer on the chip.
885 \param ftdi pointer to ftdi_context
888 \retval -1: write buffer purge failed
889 \retval -2: USB device unavailable
891 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
893 if (ftdi == NULL || ftdi->usb_dev == NULL)
894 ftdi_error_return(-2, "USB device unavailable");
896 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
897 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
898 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
899 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
905 Clears the buffers on the chip and the internal read buffer.
907 \param ftdi pointer to ftdi_context
910 \retval -1: read buffer purge failed
911 \retval -2: write buffer purge failed
912 \retval -3: USB device unavailable
914 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
918 if (ftdi == NULL || ftdi->usb_dev == NULL)
919 ftdi_error_return(-3, "USB device unavailable");
921 result = ftdi_usb_purge_rx_buffer(ftdi);
925 result = ftdi_usb_purge_tx_buffer(ftdi);
935 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
937 \param ftdi pointer to ftdi_context
940 \retval -1: usb_release failed
941 \retval -3: ftdi context invalid
943 int ftdi_usb_close(struct ftdi_context *ftdi)
948 ftdi_error_return(-3, "ftdi context invalid");
950 if (ftdi->usb_dev != NULL)
951 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
954 ftdi_usb_close_internal (ftdi);
960 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
961 Function is only used internally
964 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
965 unsigned short *value, unsigned short *index)
967 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
968 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
969 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
970 int divisor, best_divisor, best_baud, best_baud_diff;
971 unsigned long encoded_divisor;
980 divisor = 24000000 / baudrate;
982 if (ftdi->type == TYPE_AM)
984 // Round down to supported fraction (AM only)
985 divisor -= am_adjust_dn[divisor & 7];
988 // Try this divisor and the one above it (because division rounds down)
992 for (i = 0; i < 2; i++)
994 int try_divisor = divisor + i;
998 // Round up to supported divisor value
999 if (try_divisor <= 8)
1001 // Round up to minimum supported divisor
1004 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1006 // BM doesn't support divisors 9 through 11 inclusive
1009 else if (divisor < 16)
1011 // AM doesn't support divisors 9 through 15 inclusive
1016 if (ftdi->type == TYPE_AM)
1018 // Round up to supported fraction (AM only)
1019 try_divisor += am_adjust_up[try_divisor & 7];
1020 if (try_divisor > 0x1FFF8)
1022 // Round down to maximum supported divisor value (for AM)
1023 try_divisor = 0x1FFF8;
1028 if (try_divisor > 0x1FFFF)
1030 // Round down to maximum supported divisor value (for BM)
1031 try_divisor = 0x1FFFF;
1035 // Get estimated baud rate (to nearest integer)
1036 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1037 // Get absolute difference from requested baud rate
1038 if (baud_estimate < baudrate)
1040 baud_diff = baudrate - baud_estimate;
1044 baud_diff = baud_estimate - baudrate;
1046 if (i == 0 || baud_diff < best_baud_diff)
1048 // Closest to requested baud rate so far
1049 best_divisor = try_divisor;
1050 best_baud = baud_estimate;
1051 best_baud_diff = baud_diff;
1054 // Spot on! No point trying
1059 // Encode the best divisor value
1060 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1061 // Deal with special cases for encoded value
1062 if (encoded_divisor == 1)
1064 encoded_divisor = 0; // 3000000 baud
1066 else if (encoded_divisor == 0x4001)
1068 encoded_divisor = 1; // 2000000 baud (BM only)
1070 // Split into "value" and "index" values
1071 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1072 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1074 *index = (unsigned short)(encoded_divisor >> 8);
1076 *index |= ftdi->index;
1079 *index = (unsigned short)(encoded_divisor >> 16);
1081 // Return the nearest baud rate
1086 Sets the chip baud rate
1088 \param ftdi pointer to ftdi_context
1089 \param baudrate baud rate to set
1092 \retval -1: invalid baudrate
1093 \retval -2: setting baudrate failed
1094 \retval -3: USB device unavailable
1096 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1098 unsigned short value, index;
1099 int actual_baudrate;
1101 if (ftdi == NULL || ftdi->usb_dev == NULL)
1102 ftdi_error_return(-3, "USB device unavailable");
1104 if (ftdi->bitbang_enabled)
1106 baudrate = baudrate*4;
1109 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1110 if (actual_baudrate <= 0)
1111 ftdi_error_return (-1, "Silly baudrate <= 0.");
1113 // Check within tolerance (about 5%)
1114 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1115 || ((actual_baudrate < baudrate)
1116 ? (actual_baudrate * 21 < baudrate * 20)
1117 : (baudrate * 21 < actual_baudrate * 20)))
1118 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1120 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1121 SIO_SET_BAUDRATE_REQUEST, value,
1122 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1123 ftdi_error_return (-2, "Setting new baudrate failed");
1125 ftdi->baudrate = baudrate;
1130 Set (RS232) line characteristics.
1131 The break type can only be set via ftdi_set_line_property2()
1132 and defaults to "off".
1134 \param ftdi pointer to ftdi_context
1135 \param bits Number of bits
1136 \param sbit Number of stop bits
1137 \param parity Parity mode
1140 \retval -1: Setting line property failed
1142 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1143 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1145 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1149 Set (RS232) line characteristics
1151 \param ftdi pointer to ftdi_context
1152 \param bits Number of bits
1153 \param sbit Number of stop bits
1154 \param parity Parity mode
1155 \param break_type Break type
1158 \retval -1: Setting line property failed
1159 \retval -2: USB device unavailable
1161 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1162 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1163 enum ftdi_break_type break_type)
1165 unsigned short value = bits;
1167 if (ftdi == NULL || ftdi->usb_dev == NULL)
1168 ftdi_error_return(-2, "USB device unavailable");
1173 value |= (0x00 << 8);
1176 value |= (0x01 << 8);
1179 value |= (0x02 << 8);
1182 value |= (0x03 << 8);
1185 value |= (0x04 << 8);
1192 value |= (0x00 << 11);
1195 value |= (0x01 << 11);
1198 value |= (0x02 << 11);
1205 value |= (0x00 << 14);
1208 value |= (0x01 << 14);
1212 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1213 SIO_SET_DATA_REQUEST, value,
1214 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1215 ftdi_error_return (-1, "Setting new line property failed");
1221 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1223 \param ftdi pointer to ftdi_context
1224 \param buf Buffer with the data
1225 \param size Size of the buffer
1227 \retval -666: USB device unavailable
1228 \retval <0: error code from usb_bulk_write()
1229 \retval >0: number of bytes written
1231 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1236 if (ftdi == NULL || ftdi->usb_dev == NULL)
1237 ftdi_error_return(-666, "USB device unavailable");
1239 while (offset < size)
1241 int write_size = ftdi->writebuffer_chunksize;
1243 if (offset+write_size > size)
1244 write_size = size-offset;
1246 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1247 ftdi_error_return(-1, "usb bulk write failed");
1249 offset += actual_length;
1255 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1257 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1258 struct ftdi_context *ftdi = tc->ftdi;
1259 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1261 packet_size = ftdi->max_packet_size;
1263 actual_length = transfer->actual_length;
1265 if (actual_length > 2)
1267 // skip FTDI status bytes.
1268 // Maybe stored in the future to enable modem use
1269 num_of_chunks = actual_length / packet_size;
1270 chunk_remains = actual_length % packet_size;
1271 //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);
1273 ftdi->readbuffer_offset += 2;
1276 if (actual_length > packet_size - 2)
1278 for (i = 1; i < num_of_chunks; i++)
1279 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1280 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1282 if (chunk_remains > 2)
1284 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1285 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1287 actual_length -= 2*num_of_chunks;
1290 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1293 if (actual_length > 0)
1295 // data still fits in buf?
1296 if (tc->offset + actual_length <= tc->size)
1298 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1299 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1300 tc->offset += actual_length;
1302 ftdi->readbuffer_offset = 0;
1303 ftdi->readbuffer_remaining = 0;
1305 /* Did we read exactly the right amount of bytes? */
1306 if (tc->offset == tc->size)
1308 //printf("read_data exact rem %d offset %d\n",
1309 //ftdi->readbuffer_remaining, offset);
1316 // only copy part of the data or size <= readbuffer_chunksize
1317 int part_size = tc->size - tc->offset;
1318 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1319 tc->offset += part_size;
1321 ftdi->readbuffer_offset += part_size;
1322 ftdi->readbuffer_remaining = actual_length - part_size;
1324 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1325 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1331 ret = libusb_submit_transfer (transfer);
1337 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1339 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1340 struct ftdi_context *ftdi = tc->ftdi;
1342 tc->offset += transfer->actual_length;
1344 if (tc->offset == tc->size)
1350 int write_size = ftdi->writebuffer_chunksize;
1353 if (tc->offset + write_size > tc->size)
1354 write_size = tc->size - tc->offset;
1356 transfer->length = write_size;
1357 transfer->buffer = tc->buf + tc->offset;
1358 ret = libusb_submit_transfer (transfer);
1366 Writes data to the chip. Does not wait for completion of the transfer
1367 nor does it make sure that the transfer was successful.
1369 Use libusb 1.0 asynchronous API.
1371 \param ftdi pointer to ftdi_context
1372 \param buf Buffer with the data
1373 \param size Size of the buffer
1375 \retval NULL: Some error happens when submit transfer
1376 \retval !NULL: Pointer to a ftdi_transfer_control
1379 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1381 struct ftdi_transfer_control *tc;
1382 struct libusb_transfer *transfer;
1383 int write_size, ret;
1385 if (ftdi == NULL || ftdi->usb_dev == NULL)
1388 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1392 transfer = libusb_alloc_transfer(0);
1405 if (size < ftdi->writebuffer_chunksize)
1408 write_size = ftdi->writebuffer_chunksize;
1410 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1411 write_size, ftdi_write_data_cb, tc,
1412 ftdi->usb_write_timeout);
1413 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1415 ret = libusb_submit_transfer(transfer);
1418 libusb_free_transfer(transfer);
1422 tc->transfer = transfer;
1428 Reads data from the chip. Does not wait for completion of the transfer
1429 nor does it make sure that the transfer was successful.
1431 Use libusb 1.0 asynchronous API.
1433 \param ftdi pointer to ftdi_context
1434 \param buf Buffer with the data
1435 \param size Size of the buffer
1437 \retval NULL: Some error happens when submit transfer
1438 \retval !NULL: Pointer to a ftdi_transfer_control
1441 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1443 struct ftdi_transfer_control *tc;
1444 struct libusb_transfer *transfer;
1447 if (ftdi == NULL || ftdi->usb_dev == NULL)
1450 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1458 if (size <= ftdi->readbuffer_remaining)
1460 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1463 ftdi->readbuffer_remaining -= size;
1464 ftdi->readbuffer_offset += size;
1466 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1470 tc->transfer = NULL;
1475 if (ftdi->readbuffer_remaining != 0)
1477 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1479 tc->offset = ftdi->readbuffer_remaining;
1484 transfer = libusb_alloc_transfer(0);
1491 ftdi->readbuffer_remaining = 0;
1492 ftdi->readbuffer_offset = 0;
1494 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);
1495 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1497 ret = libusb_submit_transfer(transfer);
1500 libusb_free_transfer(transfer);
1504 tc->transfer = transfer;
1510 Wait for completion of the transfer.
1512 Use libusb 1.0 asynchronous API.
1514 \param tc pointer to ftdi_transfer_control
1516 \retval < 0: Some error happens
1517 \retval >= 0: Data size transferred
1520 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1524 while (!tc->completed)
1526 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1529 if (ret == LIBUSB_ERROR_INTERRUPTED)
1531 libusb_cancel_transfer(tc->transfer);
1532 while (!tc->completed)
1533 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1535 libusb_free_transfer(tc->transfer);
1543 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1544 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1548 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1550 libusb_free_transfer(tc->transfer);
1557 Configure write buffer chunk size.
1560 \param ftdi pointer to ftdi_context
1561 \param chunksize Chunk size
1564 \retval -1: ftdi context invalid
1566 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1569 ftdi_error_return(-1, "ftdi context invalid");
1571 ftdi->writebuffer_chunksize = chunksize;
1576 Get write buffer chunk size.
1578 \param ftdi pointer to ftdi_context
1579 \param chunksize Pointer to store chunk size in
1582 \retval -1: ftdi context invalid
1584 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1587 ftdi_error_return(-1, "ftdi context invalid");
1589 *chunksize = ftdi->writebuffer_chunksize;
1594 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1596 Automatically strips the two modem status bytes transfered during every read.
1598 \param ftdi pointer to ftdi_context
1599 \param buf Buffer to store data in
1600 \param size Size of the buffer
1602 \retval -666: USB device unavailable
1603 \retval <0: error code from libusb_bulk_transfer()
1604 \retval 0: no data was available
1605 \retval >0: number of bytes read
1608 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1610 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1611 int packet_size = ftdi->max_packet_size;
1612 int actual_length = 1;
1614 if (ftdi == NULL || ftdi->usb_dev == NULL)
1615 ftdi_error_return(-666, "USB device unavailable");
1617 // Packet size sanity check (avoid division by zero)
1618 if (packet_size == 0)
1619 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1621 // everything we want is still in the readbuffer?
1622 if (size <= ftdi->readbuffer_remaining)
1624 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1627 ftdi->readbuffer_remaining -= size;
1628 ftdi->readbuffer_offset += size;
1630 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1634 // something still in the readbuffer, but not enough to satisfy 'size'?
1635 if (ftdi->readbuffer_remaining != 0)
1637 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1640 offset += ftdi->readbuffer_remaining;
1642 // do the actual USB read
1643 while (offset < size && actual_length > 0)
1645 ftdi->readbuffer_remaining = 0;
1646 ftdi->readbuffer_offset = 0;
1647 /* returns how much received */
1648 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1650 ftdi_error_return(ret, "usb bulk read failed");
1652 if (actual_length > 2)
1654 // skip FTDI status bytes.
1655 // Maybe stored in the future to enable modem use
1656 num_of_chunks = actual_length / packet_size;
1657 chunk_remains = actual_length % packet_size;
1658 //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);
1660 ftdi->readbuffer_offset += 2;
1663 if (actual_length > packet_size - 2)
1665 for (i = 1; i < num_of_chunks; i++)
1666 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1667 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1669 if (chunk_remains > 2)
1671 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1672 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1674 actual_length -= 2*num_of_chunks;
1677 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1680 else if (actual_length <= 2)
1682 // no more data to read?
1685 if (actual_length > 0)
1687 // data still fits in buf?
1688 if (offset+actual_length <= size)
1690 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1691 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1692 offset += actual_length;
1694 /* Did we read exactly the right amount of bytes? */
1696 //printf("read_data exact rem %d offset %d\n",
1697 //ftdi->readbuffer_remaining, offset);
1702 // only copy part of the data or size <= readbuffer_chunksize
1703 int part_size = size-offset;
1704 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1706 ftdi->readbuffer_offset += part_size;
1707 ftdi->readbuffer_remaining = actual_length-part_size;
1708 offset += part_size;
1710 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1711 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1722 Configure read buffer chunk size.
1725 Automatically reallocates the buffer.
1727 \param ftdi pointer to ftdi_context
1728 \param chunksize Chunk size
1731 \retval -1: ftdi context invalid
1733 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1735 unsigned char *new_buf;
1738 ftdi_error_return(-1, "ftdi context invalid");
1740 // Invalidate all remaining data
1741 ftdi->readbuffer_offset = 0;
1742 ftdi->readbuffer_remaining = 0;
1744 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1745 which is defined in libusb-1.0. Otherwise, each USB read request will
1746 be divided into multiple URBs. This will cause issues on Linux kernel
1747 older than 2.6.32. */
1748 if (chunksize > 16384)
1752 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1753 ftdi_error_return(-1, "out of memory for readbuffer");
1755 ftdi->readbuffer = new_buf;
1756 ftdi->readbuffer_chunksize = chunksize;
1762 Get read buffer chunk size.
1764 \param ftdi pointer to ftdi_context
1765 \param chunksize Pointer to store chunk size in
1768 \retval -1: FTDI context invalid
1770 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1773 ftdi_error_return(-1, "FTDI context invalid");
1775 *chunksize = ftdi->readbuffer_chunksize;
1781 Enable bitbang mode.
1783 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1785 \param ftdi pointer to ftdi_context
1786 \param bitmask Bitmask to configure lines.
1787 HIGH/ON value configures a line as output.
1790 \retval -1: can't enable bitbang mode
1791 \retval -2: USB device unavailable
1793 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1795 unsigned short usb_val;
1797 if (ftdi == NULL || ftdi->usb_dev == NULL)
1798 ftdi_error_return(-2, "USB device unavailable");
1800 usb_val = bitmask; // low byte: bitmask
1801 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1802 usb_val |= (ftdi->bitbang_mode << 8);
1804 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1805 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1806 NULL, 0, ftdi->usb_write_timeout) < 0)
1807 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1809 ftdi->bitbang_enabled = 1;
1814 Disable bitbang mode.
1816 \param ftdi pointer to ftdi_context
1819 \retval -1: can't disable bitbang mode
1820 \retval -2: USB device unavailable
1822 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1824 if (ftdi == NULL || ftdi->usb_dev == NULL)
1825 ftdi_error_return(-2, "USB device unavailable");
1827 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)
1828 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1830 ftdi->bitbang_enabled = 0;
1835 Enable/disable bitbang modes.
1837 \param ftdi pointer to ftdi_context
1838 \param bitmask Bitmask to configure lines.
1839 HIGH/ON value configures a line as output.
1840 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1843 \retval -1: can't enable bitbang mode
1844 \retval -2: USB device unavailable
1846 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1848 unsigned short usb_val;
1850 if (ftdi == NULL || ftdi->usb_dev == NULL)
1851 ftdi_error_return(-2, "USB device unavailable");
1853 usb_val = bitmask; // low byte: bitmask
1854 usb_val |= (mode << 8);
1855 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)
1856 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1858 ftdi->bitbang_mode = mode;
1859 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1864 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1866 \param ftdi pointer to ftdi_context
1867 \param pins Pointer to store pins into
1870 \retval -1: read pins failed
1871 \retval -2: USB device unavailable
1873 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1875 if (ftdi == NULL || ftdi->usb_dev == NULL)
1876 ftdi_error_return(-2, "USB device unavailable");
1878 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)
1879 ftdi_error_return(-1, "read pins failed");
1887 The FTDI chip keeps data in the internal buffer for a specific
1888 amount of time if the buffer is not full yet to decrease
1889 load on the usb bus.
1891 \param ftdi pointer to ftdi_context
1892 \param latency Value between 1 and 255
1895 \retval -1: latency out of range
1896 \retval -2: unable to set latency timer
1897 \retval -3: USB device unavailable
1899 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1901 unsigned short usb_val;
1904 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1906 if (ftdi == NULL || ftdi->usb_dev == NULL)
1907 ftdi_error_return(-3, "USB device unavailable");
1910 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)
1911 ftdi_error_return(-2, "unable to set latency timer");
1919 \param ftdi pointer to ftdi_context
1920 \param latency Pointer to store latency value in
1923 \retval -1: unable to get latency timer
1924 \retval -2: USB device unavailable
1926 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1928 unsigned short usb_val;
1930 if (ftdi == NULL || ftdi->usb_dev == NULL)
1931 ftdi_error_return(-2, "USB device unavailable");
1933 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)
1934 ftdi_error_return(-1, "reading latency timer failed");
1936 *latency = (unsigned char)usb_val;
1941 Poll modem status information
1943 This function allows the retrieve the two status bytes of the device.
1944 The device sends these bytes also as a header for each read access
1945 where they are discarded by ftdi_read_data(). The chip generates
1946 the two stripped status bytes in the absence of data every 40 ms.
1948 Layout of the first byte:
1949 - B0..B3 - must be 0
1950 - B4 Clear to send (CTS)
1953 - B5 Data set ready (DTS)
1956 - B6 Ring indicator (RI)
1959 - B7 Receive line signal detect (RLSD)
1963 Layout of the second byte:
1964 - B0 Data ready (DR)
1965 - B1 Overrun error (OE)
1966 - B2 Parity error (PE)
1967 - B3 Framing error (FE)
1968 - B4 Break interrupt (BI)
1969 - B5 Transmitter holding register (THRE)
1970 - B6 Transmitter empty (TEMT)
1971 - B7 Error in RCVR FIFO
1973 \param ftdi pointer to ftdi_context
1974 \param status Pointer to store status information in. Must be two bytes.
1977 \retval -1: unable to retrieve status information
1978 \retval -2: USB device unavailable
1980 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1984 if (ftdi == NULL || ftdi->usb_dev == NULL)
1985 ftdi_error_return(-2, "USB device unavailable");
1987 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
1988 ftdi_error_return(-1, "getting modem status failed");
1990 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
1996 Set flowcontrol for ftdi chip
1998 \param ftdi pointer to ftdi_context
1999 \param flowctrl flow control to use. should be
2000 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2003 \retval -1: set flow control failed
2004 \retval -2: USB device unavailable
2006 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2008 if (ftdi == NULL || ftdi->usb_dev == NULL)
2009 ftdi_error_return(-2, "USB device unavailable");
2011 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2012 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2013 NULL, 0, ftdi->usb_write_timeout) < 0)
2014 ftdi_error_return(-1, "set flow control failed");
2022 \param ftdi pointer to ftdi_context
2023 \param state state to set line to (1 or 0)
2026 \retval -1: set dtr failed
2027 \retval -2: USB device unavailable
2029 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2031 unsigned short usb_val;
2033 if (ftdi == NULL || ftdi->usb_dev == NULL)
2034 ftdi_error_return(-2, "USB device unavailable");
2037 usb_val = SIO_SET_DTR_HIGH;
2039 usb_val = SIO_SET_DTR_LOW;
2041 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2042 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2043 NULL, 0, ftdi->usb_write_timeout) < 0)
2044 ftdi_error_return(-1, "set dtr failed");
2052 \param ftdi pointer to ftdi_context
2053 \param state state to set line to (1 or 0)
2056 \retval -1: set rts failed
2057 \retval -2: USB device unavailable
2059 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2061 unsigned short usb_val;
2063 if (ftdi == NULL || ftdi->usb_dev == NULL)
2064 ftdi_error_return(-2, "USB device unavailable");
2067 usb_val = SIO_SET_RTS_HIGH;
2069 usb_val = SIO_SET_RTS_LOW;
2071 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2072 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2073 NULL, 0, ftdi->usb_write_timeout) < 0)
2074 ftdi_error_return(-1, "set of rts failed");
2080 Set dtr and rts line in one pass
2082 \param ftdi pointer to ftdi_context
2083 \param dtr DTR state to set line to (1 or 0)
2084 \param rts RTS state to set line to (1 or 0)
2087 \retval -1: set dtr/rts failed
2088 \retval -2: USB device unavailable
2090 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2092 unsigned short usb_val;
2094 if (ftdi == NULL || ftdi->usb_dev == NULL)
2095 ftdi_error_return(-2, "USB device unavailable");
2098 usb_val = SIO_SET_DTR_HIGH;
2100 usb_val = SIO_SET_DTR_LOW;
2103 usb_val |= SIO_SET_RTS_HIGH;
2105 usb_val |= SIO_SET_RTS_LOW;
2107 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2108 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2109 NULL, 0, ftdi->usb_write_timeout) < 0)
2110 ftdi_error_return(-1, "set of rts/dtr failed");
2116 Set the special event character
2118 \param ftdi pointer to ftdi_context
2119 \param eventch Event character
2120 \param enable 0 to disable the event character, non-zero otherwise
2123 \retval -1: unable to set event character
2124 \retval -2: USB device unavailable
2126 int ftdi_set_event_char(struct ftdi_context *ftdi,
2127 unsigned char eventch, unsigned char enable)
2129 unsigned short usb_val;
2131 if (ftdi == NULL || ftdi->usb_dev == NULL)
2132 ftdi_error_return(-2, "USB device unavailable");
2138 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)
2139 ftdi_error_return(-1, "setting event character failed");
2147 \param ftdi pointer to ftdi_context
2148 \param errorch Error character
2149 \param enable 0 to disable the error character, non-zero otherwise
2152 \retval -1: unable to set error character
2153 \retval -2: USB device unavailable
2155 int ftdi_set_error_char(struct ftdi_context *ftdi,
2156 unsigned char errorch, unsigned char enable)
2158 unsigned short usb_val;
2160 if (ftdi == NULL || ftdi->usb_dev == NULL)
2161 ftdi_error_return(-2, "USB device unavailable");
2167 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)
2168 ftdi_error_return(-1, "setting error character failed");
2174 Init eeprom with default values.
2175 \param ftdi pointer to ftdi_context
2176 \param manufacturer String to use as Manufacturer
2177 \param product String to use as Product description
2178 \param serial String to use as Serial number description
2181 \retval -1: No struct ftdi_context
2182 \retval -2: No struct ftdi_eeprom
2184 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2185 char * product, char * serial)
2187 struct ftdi_eeprom *eeprom;
2190 ftdi_error_return(-1, "No struct ftdi_context");
2192 if (ftdi->eeprom == NULL)
2193 ftdi_error_return(-2,"No struct ftdi_eeprom");
2195 eeprom = ftdi->eeprom;
2196 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2198 eeprom->vendor_id = 0x0403;
2199 eeprom->use_serial = USE_SERIAL_NUM;
2200 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2201 (ftdi->type == TYPE_R))
2202 eeprom->product_id = 0x6001;
2203 else if (ftdi->type == TYPE_4232H)
2204 eeprom->product_id = 0x6011;
2205 else if (ftdi->type == TYPE_232H)
2206 eeprom->product_id = 0x6014;
2208 eeprom->product_id = 0x6010;
2209 if (ftdi->type == TYPE_AM)
2210 eeprom->usb_version = 0x0101;
2212 eeprom->usb_version = 0x0200;
2213 eeprom->max_power = 100;
2215 if (eeprom->manufacturer)
2216 free (eeprom->manufacturer);
2217 eeprom->manufacturer = NULL;
2220 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2221 if (eeprom->manufacturer)
2222 strcpy(eeprom->manufacturer, manufacturer);
2225 if (eeprom->product)
2226 free (eeprom->product);
2227 eeprom->product = NULL;
2230 eeprom->product = malloc(strlen(product)+1);
2231 if (eeprom->product)
2232 strcpy(eeprom->product, product);
2236 free (eeprom->serial);
2237 eeprom->serial = NULL;
2240 eeprom->serial = malloc(strlen(serial)+1);
2242 strcpy(eeprom->serial, serial);
2246 if (ftdi->type == TYPE_R)
2248 eeprom->max_power = 90;
2249 eeprom->size = 0x80;
2250 eeprom->cbus_function[0] = CBUS_TXLED;
2251 eeprom->cbus_function[1] = CBUS_RXLED;
2252 eeprom->cbus_function[2] = CBUS_TXDEN;
2253 eeprom->cbus_function[3] = CBUS_PWREN;
2254 eeprom->cbus_function[4] = CBUS_SLEEP;
2258 if(ftdi->type == TYPE_232H)
2261 for (i=0; i<10; i++)
2262 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2268 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2269 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2274 int mode_low, mode_high;
2275 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2276 mode_low = CBUSH_TRISTATE;
2278 mode_low = eeprom->cbus_function[2*i];
2279 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2280 mode_high = CBUSH_TRISTATE;
2282 mode_high = eeprom->cbus_function[2*i];
2284 output[0x18+i] = mode_high <<4 | mode_low;
2288 Build binary buffer from ftdi_eeprom structure.
2289 Output is suitable for ftdi_write_eeprom().
2291 \param ftdi pointer to ftdi_context
2293 \retval >=0: size of eeprom user area in bytes
2294 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2295 \retval -2: Invalid eeprom or ftdi pointer
2296 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2297 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2298 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2299 \retval -6: No connected EEPROM or EEPROM Type unknown
2301 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2303 unsigned char i, j, eeprom_size_mask;
2304 unsigned short checksum, value;
2305 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2307 struct ftdi_eeprom *eeprom;
2308 unsigned char * output;
2311 ftdi_error_return(-2,"No context");
2312 if (ftdi->eeprom == NULL)
2313 ftdi_error_return(-2,"No eeprom structure");
2315 eeprom= ftdi->eeprom;
2316 output = eeprom->buf;
2318 if (eeprom->chip == -1)
2319 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2321 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2322 eeprom->size = 0x100;
2324 eeprom->size = 0x80;
2326 if (eeprom->manufacturer != NULL)
2327 manufacturer_size = strlen(eeprom->manufacturer);
2328 if (eeprom->product != NULL)
2329 product_size = strlen(eeprom->product);
2330 if (eeprom->serial != NULL)
2331 serial_size = strlen(eeprom->serial);
2333 // eeprom size check
2338 user_area_size = 96; // base size for strings (total of 48 characters)
2341 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2344 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2346 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2348 user_area_size = 86;
2354 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2356 if (user_area_size < 0)
2357 ftdi_error_return(-1,"eeprom size exceeded");
2360 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2362 // Bytes and Bits set for all Types
2364 // Addr 02: Vendor ID
2365 output[0x02] = eeprom->vendor_id;
2366 output[0x03] = eeprom->vendor_id >> 8;
2368 // Addr 04: Product ID
2369 output[0x04] = eeprom->product_id;
2370 output[0x05] = eeprom->product_id >> 8;
2372 // Addr 06: Device release number (0400h for BM features)
2373 output[0x06] = 0x00;
2377 output[0x07] = 0x02;
2380 output[0x07] = 0x04;
2383 output[0x07] = 0x05;
2386 output[0x07] = 0x06;
2389 output[0x07] = 0x07;
2392 output[0x07] = 0x08;
2395 output[0x07] = 0x09;
2398 output[0x07] = 0x00;
2401 // Addr 08: Config descriptor
2403 // Bit 6: 1 if this device is self powered, 0 if bus powered
2404 // Bit 5: 1 if this device uses remote wakeup
2405 // Bit 4-0: reserved - 0
2407 if (eeprom->self_powered == 1)
2409 if (eeprom->remote_wakeup == 1)
2413 // Addr 09: Max power consumption: max power = value * 2 mA
2414 output[0x09] = eeprom->max_power>>1;
2416 if (ftdi->type != TYPE_AM)
2418 // Addr 0A: Chip configuration
2419 // Bit 7: 0 - reserved
2420 // Bit 6: 0 - reserved
2421 // Bit 5: 0 - reserved
2422 // Bit 4: 1 - Change USB version
2423 // Bit 3: 1 - Use the serial number string
2424 // Bit 2: 1 - Enable suspend pull downs for lower power
2425 // Bit 1: 1 - Out EndPoint is Isochronous
2426 // Bit 0: 1 - In EndPoint is Isochronous
2429 if (eeprom->in_is_isochronous == 1)
2431 if (eeprom->out_is_isochronous == 1)
2437 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2438 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2456 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2457 eeprom_size_mask = eeprom->size -1;
2459 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2460 // Addr 0F: Length of manufacturer string
2461 // Output manufacturer
2462 output[0x0E] = i; // calculate offset
2463 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2464 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2465 for (j = 0; j < manufacturer_size; j++)
2467 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2468 output[i & eeprom_size_mask] = 0x00, i++;
2470 output[0x0F] = manufacturer_size*2 + 2;
2472 // Addr 10: Offset of the product string + 0x80, calculated later
2473 // Addr 11: Length of product string
2474 output[0x10] = i | 0x80; // calculate offset
2475 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2476 output[i & eeprom_size_mask] = 0x03, i++;
2477 for (j = 0; j < product_size; j++)
2479 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2480 output[i & eeprom_size_mask] = 0x00, i++;
2482 output[0x11] = product_size*2 + 2;
2484 // Addr 12: Offset of the serial string + 0x80, calculated later
2485 // Addr 13: Length of serial string
2486 output[0x12] = i | 0x80; // calculate offset
2487 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2488 output[i & eeprom_size_mask] = 0x03, i++;
2489 for (j = 0; j < serial_size; j++)
2491 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2492 output[i & eeprom_size_mask] = 0x00, i++;
2495 // Legacy port name and PnP fields for FT2232 and newer chips
2496 if (ftdi->type > TYPE_BM)
2498 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2500 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2502 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2506 output[0x13] = serial_size*2 + 2;
2508 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2510 if (eeprom->use_serial == USE_SERIAL_NUM )
2511 output[0x0A] |= USE_SERIAL_NUM;
2513 output[0x0A] &= ~USE_SERIAL_NUM;
2516 /* Bytes and Bits specific to (some) types
2517 Write linear, as this allows easier fixing*/
2523 output[0x0C] = eeprom->usb_version & 0xff;
2524 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2525 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2526 output[0x0A] |= USE_USB_VERSION_BIT;
2528 output[0x0A] &= ~USE_USB_VERSION_BIT;
2533 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0x7):0;
2534 if ( eeprom->channel_a_driver == DRIVER_VCP)
2535 output[0x00] |= DRIVER_VCP;
2537 output[0x00] &= ~DRIVER_VCP;
2539 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2540 output[0x00] |= HIGH_CURRENT_DRIVE;
2542 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2544 output[0x01] = (eeprom->channel_b_type)?((1<<(eeprom->channel_b_type)) & 0x7):0;
2545 if ( eeprom->channel_b_driver == DRIVER_VCP)
2546 output[0x01] |= DRIVER_VCP;
2548 output[0x01] &= ~DRIVER_VCP;
2550 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2551 output[0x01] |= HIGH_CURRENT_DRIVE;
2553 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2555 if (eeprom->in_is_isochronous == 1)
2556 output[0x0A] |= 0x1;
2558 output[0x0A] &= ~0x1;
2559 if (eeprom->out_is_isochronous == 1)
2560 output[0x0A] |= 0x2;
2562 output[0x0A] &= ~0x2;
2563 if (eeprom->suspend_pull_downs == 1)
2564 output[0x0A] |= 0x4;
2566 output[0x0A] &= ~0x4;
2567 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2568 output[0x0A] |= USE_USB_VERSION_BIT;
2570 output[0x0A] &= ~USE_USB_VERSION_BIT;
2572 output[0x0C] = eeprom->usb_version & 0xff;
2573 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2574 output[0x14] = eeprom->chip;
2577 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2578 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2579 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2581 if (eeprom->suspend_pull_downs == 1)
2582 output[0x0A] |= 0x4;
2584 output[0x0A] &= ~0x4;
2585 output[0x0B] = eeprom->invert;
2586 output[0x0C] = eeprom->usb_version & 0xff;
2587 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2589 if (eeprom->cbus_function[0] > CBUS_BB)
2590 output[0x14] = CBUS_TXLED;
2592 output[0x14] = eeprom->cbus_function[0];
2594 if (eeprom->cbus_function[1] > CBUS_BB)
2595 output[0x14] |= CBUS_RXLED<<4;
2597 output[0x14] |= eeprom->cbus_function[1]<<4;
2599 if (eeprom->cbus_function[2] > CBUS_BB)
2600 output[0x15] = CBUS_TXDEN;
2602 output[0x15] = eeprom->cbus_function[2];
2604 if (eeprom->cbus_function[3] > CBUS_BB)
2605 output[0x15] |= CBUS_PWREN<<4;
2607 output[0x15] |= eeprom->cbus_function[3]<<4;
2609 if (eeprom->cbus_function[4] > CBUS_CLK6)
2610 output[0x16] = CBUS_SLEEP;
2612 output[0x16] = eeprom->cbus_function[4];
2615 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0x7):0;
2616 if ( eeprom->channel_a_driver == DRIVER_VCP)
2617 output[0x00] |= DRIVER_VCP;
2619 output[0x00] &= ~DRIVER_VCP;
2621 output[0x01] = (eeprom->channel_b_type)?((1<<(eeprom->channel_b_type)) & 0x7):0;
2622 if ( eeprom->channel_b_driver == DRIVER_VCP)
2623 output[0x01] |= DRIVER_VCP;
2625 output[0x01] &= ~DRIVER_VCP;
2626 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2627 output[0x01] |= SUSPEND_DBUS7_BIT;
2629 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2631 if (eeprom->suspend_pull_downs == 1)
2632 output[0x0A] |= 0x4;
2634 output[0x0A] &= ~0x4;
2636 if (eeprom->group0_drive > DRIVE_16MA)
2637 output[0x0c] |= DRIVE_16MA;
2639 output[0x0c] |= eeprom->group0_drive;
2640 if (eeprom->group0_schmitt == IS_SCHMITT)
2641 output[0x0c] |= IS_SCHMITT;
2642 if (eeprom->group0_slew == SLOW_SLEW)
2643 output[0x0c] |= SLOW_SLEW;
2645 if (eeprom->group1_drive > DRIVE_16MA)
2646 output[0x0c] |= DRIVE_16MA<<4;
2648 output[0x0c] |= eeprom->group1_drive<<4;
2649 if (eeprom->group1_schmitt == IS_SCHMITT)
2650 output[0x0c] |= IS_SCHMITT<<4;
2651 if (eeprom->group1_slew == SLOW_SLEW)
2652 output[0x0c] |= SLOW_SLEW<<4;
2654 if (eeprom->group2_drive > DRIVE_16MA)
2655 output[0x0d] |= DRIVE_16MA;
2657 output[0x0d] |= eeprom->group2_drive;
2658 if (eeprom->group2_schmitt == IS_SCHMITT)
2659 output[0x0d] |= IS_SCHMITT;
2660 if (eeprom->group2_slew == SLOW_SLEW)
2661 output[0x0d] |= SLOW_SLEW;
2663 if (eeprom->group3_drive > DRIVE_16MA)
2664 output[0x0d] |= DRIVE_16MA<<4;
2666 output[0x0d] |= eeprom->group3_drive<<4;
2667 if (eeprom->group3_schmitt == IS_SCHMITT)
2668 output[0x0d] |= IS_SCHMITT<<4;
2669 if (eeprom->group3_slew == SLOW_SLEW)
2670 output[0x0d] |= SLOW_SLEW<<4;
2672 output[0x18] = eeprom->chip;
2676 output[0x18] = eeprom->chip;
2677 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2680 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0xf):0;
2681 if ( eeprom->channel_a_driver == DRIVER_VCP)
2682 output[0x00] |= DRIVER_VCPH;
2684 output[0x00] &= ~DRIVER_VCPH;
2685 if (eeprom->powersave)
2686 output[0x01] |= POWER_SAVE_DISABLE_H;
2688 output[0x01] &= ~POWER_SAVE_DISABLE_H;
2689 if (eeprom->clock_polarity)
2690 output[0x01] |= FT1284_CLK_IDLE_STATE;
2692 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
2693 if (eeprom->data_order)
2694 output[0x01] |= FT1284_DATA_LSB;
2696 output[0x01] &= ~FT1284_DATA_LSB;
2697 if (eeprom->flow_control)
2698 output[0x01] |= FT1284_FLOW_CONTROL;
2700 output[0x01] &= ~FT1284_FLOW_CONTROL;
2701 if (eeprom->group0_drive > DRIVE_16MA)
2702 output[0x0c] |= DRIVE_16MA;
2704 output[0x0c] |= eeprom->group0_drive;
2705 if (eeprom->group0_schmitt == IS_SCHMITT)
2706 output[0x0c] |= IS_SCHMITT;
2707 if (eeprom->group0_slew == SLOW_SLEW)
2708 output[0x0c] |= SLOW_SLEW;
2710 if (eeprom->group1_drive > DRIVE_16MA)
2711 output[0x0d] |= DRIVE_16MA;
2713 output[0x0d] |= eeprom->group1_drive;
2714 if (eeprom->group1_schmitt == IS_SCHMITT)
2715 output[0x0d] |= IS_SCHMITT;
2716 if (eeprom->group1_slew == SLOW_SLEW)
2717 output[0x0d] |= SLOW_SLEW;
2719 set_ft232h_cbus(eeprom, output);
2721 output[0x1e] = eeprom->chip;
2722 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
2727 // calculate checksum
2730 for (i = 0; i < eeprom->size/2-1; i++)
2732 value = output[i*2];
2733 value += output[(i*2)+1] << 8;
2735 checksum = value^checksum;
2736 checksum = (checksum << 1) | (checksum >> 15);
2739 output[eeprom->size-2] = checksum;
2740 output[eeprom->size-1] = checksum >> 8;
2742 return user_area_size;
2744 /* FTD2XX doesn't allow to set multiple bits in the interface mode bitfield*/
2745 unsigned char bit2type(unsigned char bits)
2755 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
2762 Decode binary EEPROM image into an ftdi_eeprom structure.
2764 \param ftdi pointer to ftdi_context
2765 \param verbose Decode EEPROM on stdout
2768 \retval -1: something went wrong
2770 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2771 FIXME: Strings are malloc'ed here and should be freed somewhere
2773 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2776 unsigned short checksum, eeprom_checksum, value;
2777 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2779 struct ftdi_eeprom *eeprom;
2780 unsigned char *buf = ftdi->eeprom->buf;
2784 ftdi_error_return(-1,"No context");
2785 if (ftdi->eeprom == NULL)
2786 ftdi_error_return(-1,"No eeprom structure");
2788 eeprom = ftdi->eeprom;
2789 eeprom_size = eeprom->size;
2791 // Addr 02: Vendor ID
2792 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2794 // Addr 04: Product ID
2795 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2797 release = buf[0x06] + (buf[0x07]<<8);
2799 // Addr 08: Config descriptor
2801 // Bit 6: 1 if this device is self powered, 0 if bus powered
2802 // Bit 5: 1 if this device uses remote wakeup
2803 eeprom->self_powered = buf[0x08] & 0x40;
2804 eeprom->remote_wakeup = buf[0x08] & 0x20;
2806 // Addr 09: Max power consumption: max power = value * 2 mA
2807 eeprom->max_power = buf[0x09];
2809 // Addr 0A: Chip configuration
2810 // Bit 7: 0 - reserved
2811 // Bit 6: 0 - reserved
2812 // Bit 5: 0 - reserved
2813 // Bit 4: 1 - Change USB version on BM and 2232C
2814 // Bit 3: 1 - Use the serial number string
2815 // Bit 2: 1 - Enable suspend pull downs for lower power
2816 // Bit 1: 1 - Out EndPoint is Isochronous
2817 // Bit 0: 1 - In EndPoint is Isochronous
2819 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2820 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2821 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2822 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2823 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2825 // Addr 0C: USB version low byte when 0x0A
2826 // Addr 0D: USB version high byte when 0x0A
2827 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2829 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2830 // Addr 0F: Length of manufacturer string
2831 manufacturer_size = buf[0x0F]/2;
2832 if (eeprom->manufacturer)
2833 free(eeprom->manufacturer);
2834 if (manufacturer_size > 0)
2836 eeprom->manufacturer = malloc(manufacturer_size);
2837 if (eeprom->manufacturer)
2839 // Decode manufacturer
2840 i = buf[0x0E] & (eeprom_size -1); // offset
2841 for (j=0;j<manufacturer_size-1;j++)
2843 eeprom->manufacturer[j] = buf[2*j+i+2];
2845 eeprom->manufacturer[j] = '\0';
2848 else eeprom->manufacturer = NULL;
2850 // Addr 10: Offset of the product string + 0x80, calculated later
2851 // Addr 11: Length of product string
2852 if (eeprom->product)
2853 free(eeprom->product);
2854 product_size = buf[0x11]/2;
2855 if (product_size > 0)
2857 eeprom->product = malloc(product_size);
2858 if (eeprom->product)
2860 // Decode product name
2861 i = buf[0x10] & (eeprom_size -1); // offset
2862 for (j=0;j<product_size-1;j++)
2864 eeprom->product[j] = buf[2*j+i+2];
2866 eeprom->product[j] = '\0';
2869 else eeprom->product = NULL;
2871 // Addr 12: Offset of the serial string + 0x80, calculated later
2872 // Addr 13: Length of serial string
2874 free(eeprom->serial);
2875 serial_size = buf[0x13]/2;
2876 if (serial_size > 0)
2878 eeprom->serial = malloc(serial_size);
2882 i = buf[0x12] & (eeprom_size -1); // offset
2883 for (j=0;j<serial_size-1;j++)
2885 eeprom->serial[j] = buf[2*j+i+2];
2887 eeprom->serial[j] = '\0';
2890 else eeprom->serial = NULL;
2895 for (i = 0; i < eeprom_size/2-1; i++)
2898 value += buf[(i*2)+1] << 8;
2900 checksum = value^checksum;
2901 checksum = (checksum << 1) | (checksum >> 15);
2904 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2906 if (eeprom_checksum != checksum)
2908 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2909 ftdi_error_return(-1,"EEPROM checksum error");
2912 eeprom->channel_a_type = 0;
2913 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2917 else if (ftdi->type == TYPE_2232C)
2919 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2920 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2921 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2922 eeprom->channel_b_type = buf[0x01] & 0x7;
2923 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2924 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2925 eeprom->chip = buf[0x14];
2927 else if (ftdi->type == TYPE_R)
2929 /* TYPE_R flags D2XX, not VCP as all others*/
2930 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2931 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2932 if ( (buf[0x01]&0x40) != 0x40)
2934 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2935 " If this happened with the\n"
2936 " EEPROM programmed by FTDI tools, please report "
2937 "to libftdi@developer.intra2net.com\n");
2939 eeprom->chip = buf[0x16];
2940 // Addr 0B: Invert data lines
2941 // Works only on FT232R, not FT245R, but no way to distinguish
2942 eeprom->invert = buf[0x0B];
2943 // Addr 14: CBUS function: CBUS0, CBUS1
2944 // Addr 15: CBUS function: CBUS2, CBUS3
2945 // Addr 16: CBUS function: CBUS5
2946 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2947 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2948 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2949 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2950 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2952 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2954 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2955 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2956 eeprom->channel_b_type = buf[0x01] & 0x7;
2957 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2959 if (ftdi->type == TYPE_2232H)
2960 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2962 eeprom->chip = buf[0x18];
2963 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2964 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2965 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2966 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2967 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2968 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2969 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2970 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2971 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2972 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2973 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2974 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2976 else if (ftdi->type == TYPE_232H)
2980 eeprom->channel_a_type = buf[0x00] & 0xf;
2981 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
2982 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
2983 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
2984 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
2985 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
2986 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2987 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2988 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2989 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
2990 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
2991 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
2995 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
2996 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
2998 eeprom->chip = buf[0x1e];
2999 /*FIXME: Decipher more values*/
3004 char *channel_mode[] = {"UART","245","CPU", "OPTO", "FT1284"};
3005 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3006 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3007 fprintf(stdout, "Release: 0x%04x\n",release);
3009 if (eeprom->self_powered)
3010 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3012 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
3013 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3014 if (eeprom->manufacturer)
3015 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3016 if (eeprom->product)
3017 fprintf(stdout, "Product: %s\n",eeprom->product);
3019 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3020 fprintf(stdout, "Checksum : %04x\n", checksum);
3021 if (ftdi->type == TYPE_R)
3022 fprintf(stdout, "Internal EEPROM\n");
3023 else if (eeprom->chip >= 0x46)
3024 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3025 if (eeprom->suspend_dbus7)
3026 fprintf(stdout, "Suspend on DBUS7\n");
3027 if (eeprom->suspend_pull_downs)
3028 fprintf(stdout, "Pull IO pins low during suspend\n");
3029 if(eeprom->powersave)
3031 if(ftdi->type >= TYPE_232H)
3032 fprintf(stdout,"Enter low power state on ACBUS7\n");
3034 if (eeprom->remote_wakeup)
3035 fprintf(stdout, "Enable Remote Wake Up\n");
3036 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3037 if (ftdi->type >= TYPE_2232C)
3038 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3039 channel_mode[eeprom->channel_a_type],
3040 (eeprom->channel_a_driver)?" VCP":"",
3041 (eeprom->high_current_a)?" High Current IO":"");
3042 if (ftdi->type >= TYPE_232H)
3044 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3045 (eeprom->clock_polarity)?"HIGH":"LOW",
3046 (eeprom->data_order)?"LSB":"MSB",
3047 (eeprom->flow_control)?"":"No ");
3049 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
3050 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3051 channel_mode[eeprom->channel_b_type],
3052 (eeprom->channel_b_driver)?" VCP":"",
3053 (eeprom->high_current_b)?" High Current IO":"");
3054 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3055 eeprom->use_usb_version == USE_USB_VERSION_BIT)
3056 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3058 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3060 fprintf(stdout,"%s has %d mA drive%s%s\n",
3061 (ftdi->type == TYPE_2232H)?"AL":"A",
3062 (eeprom->group0_drive+1) *4,
3063 (eeprom->group0_schmitt)?" Schmitt Input":"",
3064 (eeprom->group0_slew)?" Slow Slew":"");
3065 fprintf(stdout,"%s has %d mA drive%s%s\n",
3066 (ftdi->type == TYPE_2232H)?"AH":"B",
3067 (eeprom->group1_drive+1) *4,
3068 (eeprom->group1_schmitt)?" Schmitt Input":"",
3069 (eeprom->group1_slew)?" Slow Slew":"");
3070 fprintf(stdout,"%s has %d mA drive%s%s\n",
3071 (ftdi->type == TYPE_2232H)?"BL":"C",
3072 (eeprom->group2_drive+1) *4,
3073 (eeprom->group2_schmitt)?" Schmitt Input":"",
3074 (eeprom->group2_slew)?" Slow Slew":"");
3075 fprintf(stdout,"%s has %d mA drive%s%s\n",
3076 (ftdi->type == TYPE_2232H)?"BH":"D",
3077 (eeprom->group3_drive+1) *4,
3078 (eeprom->group3_schmitt)?" Schmitt Input":"",
3079 (eeprom->group3_slew)?" Slow Slew":"");
3081 else if (ftdi->type == TYPE_232H)
3084 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3085 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3086 "CLK30","CLK15","CLK7_5"
3088 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3089 (eeprom->group0_drive+1) *4,
3090 (eeprom->group0_schmitt)?" Schmitt Input":"",
3091 (eeprom->group0_slew)?" Slow Slew":"");
3092 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3093 (eeprom->group1_drive+1) *4,
3094 (eeprom->group1_schmitt)?" Schmitt Input":"",
3095 (eeprom->group1_slew)?" Slow Slew":"");
3096 for (i=0; i<10; i++)
3098 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3099 fprintf(stdout,"C%d Function: %s\n", i,
3100 cbush_mux[eeprom->cbus_function[i]]);
3105 if (ftdi->type == TYPE_R)
3107 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3108 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3109 "IOMODE","BB_WR","BB_RD"
3111 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3115 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
3116 fprintf(stdout,"Inverted bits:");
3118 if ((eeprom->invert & (1<<i)) == (1<<i))
3119 fprintf(stdout," %s",r_bits[i]);
3120 fprintf(stdout,"\n");
3124 if (eeprom->cbus_function[i]<CBUS_BB)
3125 fprintf(stdout,"C%d Function: %s\n", i,
3126 cbus_mux[eeprom->cbus_function[i]]);
3130 /* Running MPROG show that C0..3 have fixed function Synchronous
3132 fprintf(stdout,"C%d BB Function: %s\n", i,
3135 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3144 Get a value from the decoded EEPROM structure
3146 \param ftdi pointer to ftdi_context
3147 \param value_name Enum of the value to query
3148 \param value Pointer to store read value
3151 \retval -1: Value doesn't exist
3153 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3158 *value = ftdi->eeprom->vendor_id;
3161 *value = ftdi->eeprom->product_id;
3164 *value = ftdi->eeprom->self_powered;
3167 *value = ftdi->eeprom->remote_wakeup;
3170 *value = ftdi->eeprom->is_not_pnp;
3173 *value = ftdi->eeprom->suspend_dbus7;
3175 case IN_IS_ISOCHRONOUS:
3176 *value = ftdi->eeprom->in_is_isochronous;
3178 case SUSPEND_PULL_DOWNS:
3179 *value = ftdi->eeprom->suspend_pull_downs;
3182 *value = ftdi->eeprom->use_serial;
3185 *value = ftdi->eeprom->usb_version;
3188 *value = ftdi->eeprom->max_power;
3190 case CHANNEL_A_TYPE:
3191 *value = ftdi->eeprom->channel_a_type;
3193 case CHANNEL_B_TYPE:
3194 *value = ftdi->eeprom->channel_b_type;
3196 case CHANNEL_A_DRIVER:
3197 *value = ftdi->eeprom->channel_a_driver;
3199 case CHANNEL_B_DRIVER:
3200 *value = ftdi->eeprom->channel_b_driver;
3202 case CBUS_FUNCTION_0:
3203 *value = ftdi->eeprom->cbus_function[0];
3205 case CBUS_FUNCTION_1:
3206 *value = ftdi->eeprom->cbus_function[1];
3208 case CBUS_FUNCTION_2:
3209 *value = ftdi->eeprom->cbus_function[2];
3211 case CBUS_FUNCTION_3:
3212 *value = ftdi->eeprom->cbus_function[3];
3214 case CBUS_FUNCTION_4:
3215 *value = ftdi->eeprom->cbus_function[4];
3217 case CBUS_FUNCTION_5:
3218 *value = ftdi->eeprom->cbus_function[5];
3220 case CBUS_FUNCTION_6:
3221 *value = ftdi->eeprom->cbus_function[6];
3223 case CBUS_FUNCTION_7:
3224 *value = ftdi->eeprom->cbus_function[7];
3226 case CBUS_FUNCTION_8:
3227 *value = ftdi->eeprom->cbus_function[8];
3229 case CBUS_FUNCTION_9:
3230 *value = ftdi->eeprom->cbus_function[8];
3233 *value = ftdi->eeprom->high_current;
3235 case HIGH_CURRENT_A:
3236 *value = ftdi->eeprom->high_current_a;
3238 case HIGH_CURRENT_B:
3239 *value = ftdi->eeprom->high_current_b;
3242 *value = ftdi->eeprom->invert;
3245 *value = ftdi->eeprom->group0_drive;
3247 case GROUP0_SCHMITT:
3248 *value = ftdi->eeprom->group0_schmitt;
3251 *value = ftdi->eeprom->group0_slew;
3254 *value = ftdi->eeprom->group1_drive;
3256 case GROUP1_SCHMITT:
3257 *value = ftdi->eeprom->group1_schmitt;
3260 *value = ftdi->eeprom->group1_slew;
3263 *value = ftdi->eeprom->group2_drive;
3265 case GROUP2_SCHMITT:
3266 *value = ftdi->eeprom->group2_schmitt;
3269 *value = ftdi->eeprom->group2_slew;
3272 *value = ftdi->eeprom->group3_drive;
3274 case GROUP3_SCHMITT:
3275 *value = ftdi->eeprom->group3_schmitt;
3278 *value = ftdi->eeprom->group3_slew;
3281 *value = ftdi->eeprom->powersave;
3283 case CLOCK_POLARITY:
3284 *value = ftdi->eeprom->clock_polarity;
3287 *value = ftdi->eeprom->data_order;
3290 *value = ftdi->eeprom->flow_control;
3293 *value = ftdi->eeprom->chip;
3296 *value = ftdi->eeprom->size;
3299 ftdi_error_return(-1, "Request for unknown EEPROM value");
3305 Set a value in the decoded EEPROM Structure
3306 No parameter checking is performed
3308 \param ftdi pointer to ftdi_context
3309 \param value_name Enum of the value to set
3313 \retval -1: Value doesn't exist
3314 \retval -2: Value not user settable
3316 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3321 ftdi->eeprom->vendor_id = value;
3324 ftdi->eeprom->product_id = value;
3327 ftdi->eeprom->self_powered = value;
3330 ftdi->eeprom->remote_wakeup = value;
3333 ftdi->eeprom->is_not_pnp = value;
3336 ftdi->eeprom->suspend_dbus7 = value;
3338 case IN_IS_ISOCHRONOUS:
3339 ftdi->eeprom->in_is_isochronous = value;
3341 case SUSPEND_PULL_DOWNS:
3342 ftdi->eeprom->suspend_pull_downs = value;
3345 ftdi->eeprom->use_serial = value;
3348 ftdi->eeprom->usb_version = value;
3351 ftdi->eeprom->max_power = value;
3353 case CHANNEL_A_TYPE:
3354 ftdi->eeprom->channel_a_type = value;
3356 case CHANNEL_B_TYPE:
3357 ftdi->eeprom->channel_b_type = value;
3359 case CHANNEL_A_DRIVER:
3360 ftdi->eeprom->channel_a_driver = value;
3362 case CHANNEL_B_DRIVER:
3363 ftdi->eeprom->channel_b_driver = value;
3365 case CBUS_FUNCTION_0:
3366 ftdi->eeprom->cbus_function[0] = value;
3368 case CBUS_FUNCTION_1:
3369 ftdi->eeprom->cbus_function[1] = value;
3371 case CBUS_FUNCTION_2:
3372 ftdi->eeprom->cbus_function[2] = value;
3374 case CBUS_FUNCTION_3:
3375 ftdi->eeprom->cbus_function[3] = value;
3377 case CBUS_FUNCTION_4:
3378 ftdi->eeprom->cbus_function[4] = value;
3380 case CBUS_FUNCTION_5:
3381 ftdi->eeprom->cbus_function[5] = value;
3383 case CBUS_FUNCTION_6:
3384 ftdi->eeprom->cbus_function[6] = value;
3386 case CBUS_FUNCTION_7:
3387 ftdi->eeprom->cbus_function[7] = value;
3389 case CBUS_FUNCTION_8:
3390 ftdi->eeprom->cbus_function[8] = value;
3392 case CBUS_FUNCTION_9:
3393 ftdi->eeprom->cbus_function[9] = value;
3396 ftdi->eeprom->high_current = value;
3398 case HIGH_CURRENT_A:
3399 ftdi->eeprom->high_current_a = value;
3401 case HIGH_CURRENT_B:
3402 ftdi->eeprom->high_current_b = value;
3405 ftdi->eeprom->invert = value;
3408 ftdi->eeprom->group0_drive = value;
3410 case GROUP0_SCHMITT:
3411 ftdi->eeprom->group0_schmitt = value;
3414 ftdi->eeprom->group0_slew = value;
3417 ftdi->eeprom->group1_drive = value;
3419 case GROUP1_SCHMITT:
3420 ftdi->eeprom->group1_schmitt = value;
3423 ftdi->eeprom->group1_slew = value;
3426 ftdi->eeprom->group2_drive = value;
3428 case GROUP2_SCHMITT:
3429 ftdi->eeprom->group2_schmitt = value;
3432 ftdi->eeprom->group2_slew = value;
3435 ftdi->eeprom->group3_drive = value;
3437 case GROUP3_SCHMITT:
3438 ftdi->eeprom->group3_schmitt = value;
3441 ftdi->eeprom->group3_slew = value;
3444 ftdi->eeprom->chip = value;
3447 ftdi->eeprom->powersave = value;
3449 case CLOCK_POLARITY:
3450 ftdi->eeprom->clock_polarity = value;
3453 ftdi->eeprom->data_order = value;
3456 ftdi->eeprom->flow_control = value;
3459 ftdi_error_return(-2, "EEPROM Value can't be changed");
3461 ftdi_error_return(-1, "Request to unknown EEPROM value");
3466 /** Get the read-only buffer to the binary EEPROM content
3468 \param ftdi pointer to ftdi_context
3469 \param buf buffer to receive EEPROM content
3470 \param size Size of receiving buffer
3473 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3474 \retval -2: Not enough room to store eeprom
3476 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3478 if (!ftdi || !(ftdi->eeprom))
3479 ftdi_error_return(-1, "No appropriate structure");
3481 if (!buf || size < ftdi->eeprom->size)
3482 ftdi_error_return(-1, "Not enough room to store eeprom");
3484 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3485 if (size > FTDI_MAX_EEPROM_SIZE)
3486 size = FTDI_MAX_EEPROM_SIZE;
3488 memcpy(buf, ftdi->eeprom->buf, size);
3494 Read eeprom location
3496 \param ftdi pointer to ftdi_context
3497 \param eeprom_addr Address of eeprom location to be read
3498 \param eeprom_val Pointer to store read eeprom location
3501 \retval -1: read failed
3502 \retval -2: USB device unavailable
3504 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3506 if (ftdi == NULL || ftdi->usb_dev == NULL)
3507 ftdi_error_return(-2, "USB device unavailable");
3509 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)
3510 ftdi_error_return(-1, "reading eeprom failed");
3518 \param ftdi pointer to ftdi_context
3521 \retval -1: read failed
3522 \retval -2: USB device unavailable
3524 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3529 if (ftdi == NULL || ftdi->usb_dev == NULL)
3530 ftdi_error_return(-2, "USB device unavailable");
3531 buf = ftdi->eeprom->buf;
3533 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3535 if (libusb_control_transfer(
3536 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3537 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3538 ftdi_error_return(-1, "reading eeprom failed");
3541 if (ftdi->type == TYPE_R)
3542 ftdi->eeprom->size = 0x80;
3543 /* Guesses size of eeprom by comparing halves
3544 - will not work with blank eeprom */
3545 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3546 ftdi->eeprom->size = -1;
3547 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3548 ftdi->eeprom->size = 0x80;
3549 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3550 ftdi->eeprom->size = 0x40;
3552 ftdi->eeprom->size = 0x100;
3557 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3558 Function is only used internally
3561 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3563 return ((value & 1) << 1) |
3564 ((value & 2) << 5) |
3565 ((value & 4) >> 2) |
3566 ((value & 8) << 4) |
3567 ((value & 16) >> 1) |
3568 ((value & 32) >> 1) |
3569 ((value & 64) >> 4) |
3570 ((value & 128) >> 2);
3574 Read the FTDIChip-ID from R-type devices
3576 \param ftdi pointer to ftdi_context
3577 \param chipid Pointer to store FTDIChip-ID
3580 \retval -1: read failed
3581 \retval -2: USB device unavailable
3583 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3585 unsigned int a = 0, b = 0;
3587 if (ftdi == NULL || ftdi->usb_dev == NULL)
3588 ftdi_error_return(-2, "USB device unavailable");
3590 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)
3592 a = a << 8 | a >> 8;
3593 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)
3595 b = b << 8 | b >> 8;
3596 a = (a << 16) | (b & 0xFFFF);
3597 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3598 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3599 *chipid = a ^ 0xa5f0f7d1;
3604 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3608 Write eeprom location
3610 \param ftdi pointer to ftdi_context
3611 \param eeprom_addr Address of eeprom location to be written
3612 \param eeprom_val Value to be written
3615 \retval -1: write failed
3616 \retval -2: USB device unavailable
3617 \retval -3: Invalid access to checksum protected area below 0x80
3618 \retval -4: Device can't access unprotected area
3619 \retval -5: Reading chip type failed
3621 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3622 unsigned short eeprom_val)
3624 int chip_type_location;
3625 unsigned short chip_type;
3627 if (ftdi == NULL || ftdi->usb_dev == NULL)
3628 ftdi_error_return(-2, "USB device unavailable");
3630 if (eeprom_addr <0x80)
3631 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3638 chip_type_location = 0x14;
3642 chip_type_location = 0x18;
3645 chip_type_location = 0x1e;
3648 ftdi_error_return(-4, "Device can't access unprotected area");
3651 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3652 ftdi_error_return(-5, "Reading failed failed");
3653 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3654 if ((chip_type & 0xff) != 0x66)
3656 ftdi_error_return(-6, "EEPROM is not of 93x66");
3659 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3660 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3661 NULL, 0, ftdi->usb_write_timeout) != 0)
3662 ftdi_error_return(-1, "unable to write eeprom");
3670 \param ftdi pointer to ftdi_context
3673 \retval -1: read failed
3674 \retval -2: USB device unavailable
3676 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3678 unsigned short usb_val, status;
3680 unsigned char *eeprom;
3682 if (ftdi == NULL || ftdi->usb_dev == NULL)
3683 ftdi_error_return(-2, "USB device unavailable");
3684 eeprom = ftdi->eeprom->buf;
3686 /* These commands were traced while running MProg */
3687 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3689 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3691 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3694 for (i = 0; i < ftdi->eeprom->size/2; i++)
3696 usb_val = eeprom[i*2];
3697 usb_val += eeprom[(i*2)+1] << 8;
3698 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3699 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3700 NULL, 0, ftdi->usb_write_timeout) < 0)
3701 ftdi_error_return(-1, "unable to write eeprom");
3710 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3712 \param ftdi pointer to ftdi_context
3715 \retval -1: erase failed
3716 \retval -2: USB device unavailable
3717 \retval -3: Writing magic failed
3718 \retval -4: Read EEPROM failed
3719 \retval -5: Unexpected EEPROM value
3721 #define MAGIC 0x55aa
3722 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3724 unsigned short eeprom_value;
3725 if (ftdi == NULL || ftdi->usb_dev == NULL)
3726 ftdi_error_return(-2, "USB device unavailable");
3728 if (ftdi->type == TYPE_R)
3730 ftdi->eeprom->chip = 0;
3734 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3735 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3736 ftdi_error_return(-1, "unable to erase eeprom");
3739 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3740 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3741 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3742 Chip is 93x66 if magic is only read at word position 0xc0*/
3743 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3744 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3745 NULL, 0, ftdi->usb_write_timeout) != 0)
3746 ftdi_error_return(-3, "Writing magic failed");
3747 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3748 ftdi_error_return(-4, "Reading failed failed");
3749 if (eeprom_value == MAGIC)
3751 ftdi->eeprom->chip = 0x46;
3755 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3756 ftdi_error_return(-4, "Reading failed failed");
3757 if (eeprom_value == MAGIC)
3758 ftdi->eeprom->chip = 0x56;
3761 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3762 ftdi_error_return(-4, "Reading failed failed");
3763 if (eeprom_value == MAGIC)
3764 ftdi->eeprom->chip = 0x66;
3767 ftdi->eeprom->chip = -1;
3771 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3772 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3773 ftdi_error_return(-1, "unable to erase eeprom");
3778 Get string representation for last error code
3780 \param ftdi pointer to ftdi_context
3782 \retval Pointer to error string
3784 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3789 return ftdi->error_str;
3792 /* @} end of doxygen libftdi group */