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 with given VID:PID on the usb bus. Creates a new
261 ftdi_device_list which needs to be deallocated by ftdi_list_free() after
262 use. With VID:PID 0:0, search for the default devices
263 (0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014)
265 \param ftdi pointer to ftdi_context
266 \param devlist Pointer where to store list of found devices
267 \param vendor Vendor ID to search for
268 \param product Product ID to search for
270 \retval >0: number of devices found
271 \retval -3: out of memory
272 \retval -5: libusb_get_device_list() failed
273 \retval -6: libusb_get_device_descriptor() failed
275 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
277 struct ftdi_device_list **curdev;
279 libusb_device **devs;
283 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
284 ftdi_error_return(-5, "libusb_get_device_list() failed");
289 while ((dev = devs[i++]) != NULL)
291 struct libusb_device_descriptor desc;
293 if (libusb_get_device_descriptor(dev, &desc) < 0)
294 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
296 if ((vendor != 0 && product != 0 && desc.idVendor == vendor && desc.idProduct == product) ||
297 ((desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
298 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014)))
300 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
302 ftdi_error_return_free_device_list(-3, "out of memory", devs);
304 (*curdev)->next = NULL;
305 (*curdev)->dev = dev;
306 libusb_ref_device(dev);
307 curdev = &(*curdev)->next;
311 libusb_free_device_list(devs,1);
316 Frees a usb device list.
318 \param devlist USB device list created by ftdi_usb_find_all()
320 void ftdi_list_free(struct ftdi_device_list **devlist)
322 struct ftdi_device_list *curdev, *next;
324 for (curdev = *devlist; curdev != NULL;)
327 libusb_unref_device(curdev->dev);
336 Frees a usb device list.
338 \param devlist USB device list created by ftdi_usb_find_all()
340 void ftdi_list_free2(struct ftdi_device_list *devlist)
342 ftdi_list_free(&devlist);
346 Return device ID strings from the usb device.
348 The parameters manufacturer, description and serial may be NULL
349 or pointer to buffers to store the fetched strings.
351 \note Use this function only in combination with ftdi_usb_find_all()
352 as it closes the internal "usb_dev" after use.
354 \param ftdi pointer to ftdi_context
355 \param dev libusb usb_dev to use
356 \param manufacturer Store manufacturer string here if not NULL
357 \param mnf_len Buffer size of manufacturer string
358 \param description Store product description string here if not NULL
359 \param desc_len Buffer size of product description string
360 \param serial Store serial string here if not NULL
361 \param serial_len Buffer size of serial string
364 \retval -1: wrong arguments
365 \retval -4: unable to open device
366 \retval -7: get product manufacturer failed
367 \retval -8: get product description failed
368 \retval -9: get serial number failed
369 \retval -11: libusb_get_device_descriptor() failed
371 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
372 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
374 struct libusb_device_descriptor desc;
376 if ((ftdi==NULL) || (dev==NULL))
379 if (libusb_open(dev, &ftdi->usb_dev) < 0)
380 ftdi_error_return(-4, "libusb_open() failed");
382 if (libusb_get_device_descriptor(dev, &desc) < 0)
383 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
385 if (manufacturer != NULL)
387 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
389 ftdi_usb_close_internal (ftdi);
390 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
394 if (description != NULL)
396 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
398 ftdi_usb_close_internal (ftdi);
399 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
405 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
407 ftdi_usb_close_internal (ftdi);
408 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
412 ftdi_usb_close_internal (ftdi);
418 * Internal function to determine the maximum packet size.
419 * \param ftdi pointer to ftdi_context
420 * \param dev libusb usb_dev to use
421 * \retval Maximum packet size for this device
423 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
425 struct libusb_device_descriptor desc;
426 struct libusb_config_descriptor *config0;
427 unsigned int packet_size;
430 if (ftdi == NULL || dev == NULL)
433 // Determine maximum packet size. Init with default value.
434 // New hi-speed devices from FTDI use a packet size of 512 bytes
435 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
436 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
441 if (libusb_get_device_descriptor(dev, &desc) < 0)
444 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
447 if (desc.bNumConfigurations > 0)
449 if (ftdi->interface < config0->bNumInterfaces)
451 struct libusb_interface interface = config0->interface[ftdi->interface];
452 if (interface.num_altsetting > 0)
454 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
455 if (descriptor.bNumEndpoints > 0)
457 packet_size = descriptor.endpoint[0].wMaxPacketSize;
463 libusb_free_config_descriptor (config0);
468 Opens a ftdi device given by an usb_device.
470 \param ftdi pointer to ftdi_context
471 \param dev libusb usb_dev to use
474 \retval -3: unable to config device
475 \retval -4: unable to open device
476 \retval -5: unable to claim device
477 \retval -6: reset failed
478 \retval -7: set baudrate failed
479 \retval -8: ftdi context invalid
480 \retval -9: libusb_get_device_descriptor() failed
481 \retval -10: libusb_get_config_descriptor() failed
482 \retval -11: libusb_detach_kernel_driver() failed
483 \retval -12: libusb_get_configuration() failed
485 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
487 struct libusb_device_descriptor desc;
488 struct libusb_config_descriptor *config0;
489 int cfg, cfg0, detach_errno = 0;
492 ftdi_error_return(-8, "ftdi context invalid");
494 if (libusb_open(dev, &ftdi->usb_dev) < 0)
495 ftdi_error_return(-4, "libusb_open() failed");
497 if (libusb_get_device_descriptor(dev, &desc) < 0)
498 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
500 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
501 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
502 cfg0 = config0->bConfigurationValue;
503 libusb_free_config_descriptor (config0);
505 // Try to detach ftdi_sio kernel module.
507 // The return code is kept in a separate variable and only parsed
508 // if usb_set_configuration() or usb_claim_interface() fails as the
509 // detach operation might be denied and everything still works fine.
510 // Likely scenario is a static ftdi_sio kernel module.
511 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
513 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
514 detach_errno = errno;
517 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
518 ftdi_error_return(-12, "libusb_get_configuration () failed");
519 // set configuration (needed especially for windows)
520 // tolerate EBUSY: one device with one configuration, but two interfaces
521 // and libftdi sessions to both interfaces (e.g. FT2232)
522 if (desc.bNumConfigurations > 0 && cfg != cfg0)
524 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
526 ftdi_usb_close_internal (ftdi);
527 if (detach_errno == EPERM)
529 ftdi_error_return(-8, "inappropriate permissions on device!");
533 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
538 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
540 ftdi_usb_close_internal (ftdi);
541 if (detach_errno == EPERM)
543 ftdi_error_return(-8, "inappropriate permissions on device!");
547 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
551 if (ftdi_usb_reset (ftdi) != 0)
553 ftdi_usb_close_internal (ftdi);
554 ftdi_error_return(-6, "ftdi_usb_reset failed");
557 // Try to guess chip type
558 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
559 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
560 && desc.iSerialNumber == 0))
561 ftdi->type = TYPE_BM;
562 else if (desc.bcdDevice == 0x200)
563 ftdi->type = TYPE_AM;
564 else if (desc.bcdDevice == 0x500)
565 ftdi->type = TYPE_2232C;
566 else if (desc.bcdDevice == 0x600)
568 else if (desc.bcdDevice == 0x700)
569 ftdi->type = TYPE_2232H;
570 else if (desc.bcdDevice == 0x800)
571 ftdi->type = TYPE_4232H;
572 else if (desc.bcdDevice == 0x900)
573 ftdi->type = TYPE_232H;
575 // Determine maximum packet size
576 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
578 if (ftdi_set_baudrate (ftdi, 9600) != 0)
580 ftdi_usb_close_internal (ftdi);
581 ftdi_error_return(-7, "set baudrate failed");
584 ftdi_error_return(0, "all fine");
588 Opens the first device with a given vendor and product ids.
590 \param ftdi pointer to ftdi_context
591 \param vendor Vendor ID
592 \param product Product ID
594 \retval same as ftdi_usb_open_desc()
596 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
598 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
602 Opens the first device with a given, vendor id, product id,
603 description and serial.
605 \param ftdi pointer to ftdi_context
606 \param vendor Vendor ID
607 \param product Product ID
608 \param description Description to search for. Use NULL if not needed.
609 \param serial Serial to search for. Use NULL if not needed.
612 \retval -3: usb device not found
613 \retval -4: unable to open device
614 \retval -5: unable to claim device
615 \retval -6: reset failed
616 \retval -7: set baudrate failed
617 \retval -8: get product description failed
618 \retval -9: get serial number failed
619 \retval -12: libusb_get_device_list() failed
620 \retval -13: libusb_get_device_descriptor() failed
622 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
623 const char* description, const char* serial)
625 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
629 Opens the index-th device with a given, vendor id, product id,
630 description and serial.
632 \param ftdi pointer to ftdi_context
633 \param vendor Vendor ID
634 \param product Product ID
635 \param description Description to search for. Use NULL if not needed.
636 \param serial Serial to search for. Use NULL if not needed.
637 \param index Number of matching device to open if there are more than one, starts with 0.
640 \retval -1: usb_find_busses() failed
641 \retval -2: usb_find_devices() failed
642 \retval -3: usb device not found
643 \retval -4: unable to open device
644 \retval -5: unable to claim device
645 \retval -6: reset failed
646 \retval -7: set baudrate failed
647 \retval -8: get product description failed
648 \retval -9: get serial number failed
649 \retval -10: unable to close device
650 \retval -11: ftdi context invalid
652 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
653 const char* description, const char* serial, unsigned int index)
656 libusb_device **devs;
661 ftdi_error_return(-11, "ftdi context invalid");
663 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
664 ftdi_error_return(-12, "libusb_get_device_list() failed");
666 while ((dev = devs[i++]) != NULL)
668 struct libusb_device_descriptor desc;
671 if (libusb_get_device_descriptor(dev, &desc) < 0)
672 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
674 if (desc.idVendor == vendor && desc.idProduct == product)
676 if (libusb_open(dev, &ftdi->usb_dev) < 0)
677 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
679 if (description != NULL)
681 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
683 ftdi_usb_close_internal (ftdi);
684 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
686 if (strncmp(string, description, sizeof(string)) != 0)
688 ftdi_usb_close_internal (ftdi);
694 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
696 ftdi_usb_close_internal (ftdi);
697 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
699 if (strncmp(string, serial, sizeof(string)) != 0)
701 ftdi_usb_close_internal (ftdi);
706 ftdi_usb_close_internal (ftdi);
714 res = ftdi_usb_open_dev(ftdi, dev);
715 libusb_free_device_list(devs,1);
721 ftdi_error_return_free_device_list(-3, "device not found", devs);
725 Opens the ftdi-device described by a description-string.
726 Intended to be used for parsing a device-description given as commandline argument.
728 \param ftdi pointer to ftdi_context
729 \param description NULL-terminated description-string, using this format:
730 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
731 \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")
732 \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
733 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
735 \note The description format may be extended in later versions.
738 \retval -2: libusb_get_device_list() failed
739 \retval -3: usb device not found
740 \retval -4: unable to open device
741 \retval -5: unable to claim device
742 \retval -6: reset failed
743 \retval -7: set baudrate failed
744 \retval -8: get product description failed
745 \retval -9: get serial number failed
746 \retval -10: unable to close device
747 \retval -11: illegal description format
748 \retval -12: ftdi context invalid
750 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
753 ftdi_error_return(-12, "ftdi context invalid");
755 if (description[0] == 0 || description[1] != ':')
756 ftdi_error_return(-11, "illegal description format");
758 if (description[0] == 'd')
761 libusb_device **devs;
762 unsigned int bus_number, device_address;
765 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
766 ftdi_error_return(-2, "libusb_get_device_list() failed");
768 /* XXX: This doesn't handle symlinks/odd paths/etc... */
769 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
770 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
772 while ((dev = devs[i++]) != NULL)
775 if (bus_number == libusb_get_bus_number (dev)
776 && device_address == libusb_get_device_address (dev))
778 ret = ftdi_usb_open_dev(ftdi, dev);
779 libusb_free_device_list(devs,1);
785 ftdi_error_return_free_device_list(-3, "device not found", devs);
787 else if (description[0] == 'i' || description[0] == 's')
790 unsigned int product;
791 unsigned int index=0;
792 const char *serial=NULL;
793 const char *startp, *endp;
796 startp=description+2;
797 vendor=strtoul((char*)startp,(char**)&endp,0);
798 if (*endp != ':' || endp == startp || errno != 0)
799 ftdi_error_return(-11, "illegal description format");
802 product=strtoul((char*)startp,(char**)&endp,0);
803 if (endp == startp || errno != 0)
804 ftdi_error_return(-11, "illegal description format");
806 if (description[0] == 'i' && *endp != 0)
808 /* optional index field in i-mode */
810 ftdi_error_return(-11, "illegal description format");
813 index=strtoul((char*)startp,(char**)&endp,0);
814 if (*endp != 0 || endp == startp || errno != 0)
815 ftdi_error_return(-11, "illegal description format");
817 if (description[0] == 's')
820 ftdi_error_return(-11, "illegal description format");
822 /* rest of the description is the serial */
826 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
830 ftdi_error_return(-11, "illegal description format");
835 Resets the ftdi device.
837 \param ftdi pointer to ftdi_context
840 \retval -1: FTDI reset failed
841 \retval -2: USB device unavailable
843 int ftdi_usb_reset(struct ftdi_context *ftdi)
845 if (ftdi == NULL || ftdi->usb_dev == NULL)
846 ftdi_error_return(-2, "USB device unavailable");
848 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
849 SIO_RESET_REQUEST, SIO_RESET_SIO,
850 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
851 ftdi_error_return(-1,"FTDI reset failed");
853 // Invalidate data in the readbuffer
854 ftdi->readbuffer_offset = 0;
855 ftdi->readbuffer_remaining = 0;
861 Clears the read buffer on the chip and the internal read buffer.
863 \param ftdi pointer to ftdi_context
866 \retval -1: read buffer purge failed
867 \retval -2: USB device unavailable
869 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
871 if (ftdi == NULL || ftdi->usb_dev == NULL)
872 ftdi_error_return(-2, "USB device unavailable");
874 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
875 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
876 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
877 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
879 // Invalidate data in the readbuffer
880 ftdi->readbuffer_offset = 0;
881 ftdi->readbuffer_remaining = 0;
887 Clears the write buffer on the chip.
889 \param ftdi pointer to ftdi_context
892 \retval -1: write buffer purge failed
893 \retval -2: USB device unavailable
895 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
897 if (ftdi == NULL || ftdi->usb_dev == NULL)
898 ftdi_error_return(-2, "USB device unavailable");
900 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
901 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
902 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
903 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
909 Clears the buffers on the chip and the internal read buffer.
911 \param ftdi pointer to ftdi_context
914 \retval -1: read buffer purge failed
915 \retval -2: write buffer purge failed
916 \retval -3: USB device unavailable
918 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
922 if (ftdi == NULL || ftdi->usb_dev == NULL)
923 ftdi_error_return(-3, "USB device unavailable");
925 result = ftdi_usb_purge_rx_buffer(ftdi);
929 result = ftdi_usb_purge_tx_buffer(ftdi);
939 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
941 \param ftdi pointer to ftdi_context
944 \retval -1: usb_release failed
945 \retval -3: ftdi context invalid
947 int ftdi_usb_close(struct ftdi_context *ftdi)
952 ftdi_error_return(-3, "ftdi context invalid");
954 if (ftdi->usb_dev != NULL)
955 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
958 ftdi_usb_close_internal (ftdi);
964 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
965 Function is only used internally
968 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
969 unsigned short *value, unsigned short *index)
971 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
972 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
973 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
974 int divisor, best_divisor, best_baud, best_baud_diff;
975 unsigned long encoded_divisor;
984 divisor = 24000000 / baudrate;
986 if (ftdi->type == TYPE_AM)
988 // Round down to supported fraction (AM only)
989 divisor -= am_adjust_dn[divisor & 7];
992 // Try this divisor and the one above it (because division rounds down)
996 for (i = 0; i < 2; i++)
998 int try_divisor = divisor + i;
1002 // Round up to supported divisor value
1003 if (try_divisor <= 8)
1005 // Round up to minimum supported divisor
1008 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1010 // BM doesn't support divisors 9 through 11 inclusive
1013 else if (divisor < 16)
1015 // AM doesn't support divisors 9 through 15 inclusive
1020 if (ftdi->type == TYPE_AM)
1022 // Round up to supported fraction (AM only)
1023 try_divisor += am_adjust_up[try_divisor & 7];
1024 if (try_divisor > 0x1FFF8)
1026 // Round down to maximum supported divisor value (for AM)
1027 try_divisor = 0x1FFF8;
1032 if (try_divisor > 0x1FFFF)
1034 // Round down to maximum supported divisor value (for BM)
1035 try_divisor = 0x1FFFF;
1039 // Get estimated baud rate (to nearest integer)
1040 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1041 // Get absolute difference from requested baud rate
1042 if (baud_estimate < baudrate)
1044 baud_diff = baudrate - baud_estimate;
1048 baud_diff = baud_estimate - baudrate;
1050 if (i == 0 || baud_diff < best_baud_diff)
1052 // Closest to requested baud rate so far
1053 best_divisor = try_divisor;
1054 best_baud = baud_estimate;
1055 best_baud_diff = baud_diff;
1058 // Spot on! No point trying
1063 // Encode the best divisor value
1064 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1065 // Deal with special cases for encoded value
1066 if (encoded_divisor == 1)
1068 encoded_divisor = 0; // 3000000 baud
1070 else if (encoded_divisor == 0x4001)
1072 encoded_divisor = 1; // 2000000 baud (BM only)
1074 // Split into "value" and "index" values
1075 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1076 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1078 *index = (unsigned short)(encoded_divisor >> 8);
1080 *index |= ftdi->index;
1083 *index = (unsigned short)(encoded_divisor >> 16);
1085 // Return the nearest baud rate
1090 Sets the chip baud rate
1092 \param ftdi pointer to ftdi_context
1093 \param baudrate baud rate to set
1096 \retval -1: invalid baudrate
1097 \retval -2: setting baudrate failed
1098 \retval -3: USB device unavailable
1100 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1102 unsigned short value, index;
1103 int actual_baudrate;
1105 if (ftdi == NULL || ftdi->usb_dev == NULL)
1106 ftdi_error_return(-3, "USB device unavailable");
1108 if (ftdi->bitbang_enabled)
1110 baudrate = baudrate*4;
1113 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1114 if (actual_baudrate <= 0)
1115 ftdi_error_return (-1, "Silly baudrate <= 0.");
1117 // Check within tolerance (about 5%)
1118 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1119 || ((actual_baudrate < baudrate)
1120 ? (actual_baudrate * 21 < baudrate * 20)
1121 : (baudrate * 21 < actual_baudrate * 20)))
1122 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1124 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1125 SIO_SET_BAUDRATE_REQUEST, value,
1126 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1127 ftdi_error_return (-2, "Setting new baudrate failed");
1129 ftdi->baudrate = baudrate;
1134 Set (RS232) line characteristics.
1135 The break type can only be set via ftdi_set_line_property2()
1136 and defaults to "off".
1138 \param ftdi pointer to ftdi_context
1139 \param bits Number of bits
1140 \param sbit Number of stop bits
1141 \param parity Parity mode
1144 \retval -1: Setting line property failed
1146 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1147 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1149 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1153 Set (RS232) line characteristics
1155 \param ftdi pointer to ftdi_context
1156 \param bits Number of bits
1157 \param sbit Number of stop bits
1158 \param parity Parity mode
1159 \param break_type Break type
1162 \retval -1: Setting line property failed
1163 \retval -2: USB device unavailable
1165 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1166 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1167 enum ftdi_break_type break_type)
1169 unsigned short value = bits;
1171 if (ftdi == NULL || ftdi->usb_dev == NULL)
1172 ftdi_error_return(-2, "USB device unavailable");
1177 value |= (0x00 << 8);
1180 value |= (0x01 << 8);
1183 value |= (0x02 << 8);
1186 value |= (0x03 << 8);
1189 value |= (0x04 << 8);
1196 value |= (0x00 << 11);
1199 value |= (0x01 << 11);
1202 value |= (0x02 << 11);
1209 value |= (0x00 << 14);
1212 value |= (0x01 << 14);
1216 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1217 SIO_SET_DATA_REQUEST, value,
1218 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1219 ftdi_error_return (-1, "Setting new line property failed");
1225 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1227 \param ftdi pointer to ftdi_context
1228 \param buf Buffer with the data
1229 \param size Size of the buffer
1231 \retval -666: USB device unavailable
1232 \retval <0: error code from usb_bulk_write()
1233 \retval >0: number of bytes written
1235 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1240 if (ftdi == NULL || ftdi->usb_dev == NULL)
1241 ftdi_error_return(-666, "USB device unavailable");
1243 while (offset < size)
1245 int write_size = ftdi->writebuffer_chunksize;
1247 if (offset+write_size > size)
1248 write_size = size-offset;
1250 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1251 ftdi_error_return(-1, "usb bulk write failed");
1253 offset += actual_length;
1259 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1261 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1262 struct ftdi_context *ftdi = tc->ftdi;
1263 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1265 packet_size = ftdi->max_packet_size;
1267 actual_length = transfer->actual_length;
1269 if (actual_length > 2)
1271 // skip FTDI status bytes.
1272 // Maybe stored in the future to enable modem use
1273 num_of_chunks = actual_length / packet_size;
1274 chunk_remains = actual_length % packet_size;
1275 //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);
1277 ftdi->readbuffer_offset += 2;
1280 if (actual_length > packet_size - 2)
1282 for (i = 1; i < num_of_chunks; i++)
1283 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1284 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1286 if (chunk_remains > 2)
1288 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1289 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1291 actual_length -= 2*num_of_chunks;
1294 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1297 if (actual_length > 0)
1299 // data still fits in buf?
1300 if (tc->offset + actual_length <= tc->size)
1302 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1303 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1304 tc->offset += actual_length;
1306 ftdi->readbuffer_offset = 0;
1307 ftdi->readbuffer_remaining = 0;
1309 /* Did we read exactly the right amount of bytes? */
1310 if (tc->offset == tc->size)
1312 //printf("read_data exact rem %d offset %d\n",
1313 //ftdi->readbuffer_remaining, offset);
1320 // only copy part of the data or size <= readbuffer_chunksize
1321 int part_size = tc->size - tc->offset;
1322 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1323 tc->offset += part_size;
1325 ftdi->readbuffer_offset += part_size;
1326 ftdi->readbuffer_remaining = actual_length - part_size;
1328 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1329 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1335 ret = libusb_submit_transfer (transfer);
1341 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1343 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1344 struct ftdi_context *ftdi = tc->ftdi;
1346 tc->offset += transfer->actual_length;
1348 if (tc->offset == tc->size)
1354 int write_size = ftdi->writebuffer_chunksize;
1357 if (tc->offset + write_size > tc->size)
1358 write_size = tc->size - tc->offset;
1360 transfer->length = write_size;
1361 transfer->buffer = tc->buf + tc->offset;
1362 ret = libusb_submit_transfer (transfer);
1370 Writes data to the chip. Does not wait for completion of the transfer
1371 nor does it make sure that the transfer was successful.
1373 Use libusb 1.0 asynchronous API.
1375 \param ftdi pointer to ftdi_context
1376 \param buf Buffer with the data
1377 \param size Size of the buffer
1379 \retval NULL: Some error happens when submit transfer
1380 \retval !NULL: Pointer to a ftdi_transfer_control
1383 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1385 struct ftdi_transfer_control *tc;
1386 struct libusb_transfer *transfer;
1387 int write_size, ret;
1389 if (ftdi == NULL || ftdi->usb_dev == NULL)
1392 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1396 transfer = libusb_alloc_transfer(0);
1409 if (size < ftdi->writebuffer_chunksize)
1412 write_size = ftdi->writebuffer_chunksize;
1414 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1415 write_size, ftdi_write_data_cb, tc,
1416 ftdi->usb_write_timeout);
1417 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1419 ret = libusb_submit_transfer(transfer);
1422 libusb_free_transfer(transfer);
1426 tc->transfer = transfer;
1432 Reads data from the chip. Does not wait for completion of the transfer
1433 nor does it make sure that the transfer was successful.
1435 Use libusb 1.0 asynchronous API.
1437 \param ftdi pointer to ftdi_context
1438 \param buf Buffer with the data
1439 \param size Size of the buffer
1441 \retval NULL: Some error happens when submit transfer
1442 \retval !NULL: Pointer to a ftdi_transfer_control
1445 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1447 struct ftdi_transfer_control *tc;
1448 struct libusb_transfer *transfer;
1451 if (ftdi == NULL || ftdi->usb_dev == NULL)
1454 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1462 if (size <= ftdi->readbuffer_remaining)
1464 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1467 ftdi->readbuffer_remaining -= size;
1468 ftdi->readbuffer_offset += size;
1470 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1474 tc->transfer = NULL;
1479 if (ftdi->readbuffer_remaining != 0)
1481 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1483 tc->offset = ftdi->readbuffer_remaining;
1488 transfer = libusb_alloc_transfer(0);
1495 ftdi->readbuffer_remaining = 0;
1496 ftdi->readbuffer_offset = 0;
1498 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);
1499 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1501 ret = libusb_submit_transfer(transfer);
1504 libusb_free_transfer(transfer);
1508 tc->transfer = transfer;
1514 Wait for completion of the transfer.
1516 Use libusb 1.0 asynchronous API.
1518 \param tc pointer to ftdi_transfer_control
1520 \retval < 0: Some error happens
1521 \retval >= 0: Data size transferred
1524 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1528 while (!tc->completed)
1530 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1533 if (ret == LIBUSB_ERROR_INTERRUPTED)
1535 libusb_cancel_transfer(tc->transfer);
1536 while (!tc->completed)
1537 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1539 libusb_free_transfer(tc->transfer);
1547 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1548 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1552 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1554 libusb_free_transfer(tc->transfer);
1561 Configure write buffer chunk size.
1564 \param ftdi pointer to ftdi_context
1565 \param chunksize Chunk size
1568 \retval -1: ftdi context invalid
1570 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1573 ftdi_error_return(-1, "ftdi context invalid");
1575 ftdi->writebuffer_chunksize = chunksize;
1580 Get write buffer chunk size.
1582 \param ftdi pointer to ftdi_context
1583 \param chunksize Pointer to store chunk size in
1586 \retval -1: ftdi context invalid
1588 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1591 ftdi_error_return(-1, "ftdi context invalid");
1593 *chunksize = ftdi->writebuffer_chunksize;
1598 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1600 Automatically strips the two modem status bytes transfered during every read.
1602 \param ftdi pointer to ftdi_context
1603 \param buf Buffer to store data in
1604 \param size Size of the buffer
1606 \retval -666: USB device unavailable
1607 \retval <0: error code from libusb_bulk_transfer()
1608 \retval 0: no data was available
1609 \retval >0: number of bytes read
1612 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1614 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1615 int packet_size = ftdi->max_packet_size;
1616 int actual_length = 1;
1618 if (ftdi == NULL || ftdi->usb_dev == NULL)
1619 ftdi_error_return(-666, "USB device unavailable");
1621 // Packet size sanity check (avoid division by zero)
1622 if (packet_size == 0)
1623 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1625 // everything we want is still in the readbuffer?
1626 if (size <= ftdi->readbuffer_remaining)
1628 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1631 ftdi->readbuffer_remaining -= size;
1632 ftdi->readbuffer_offset += size;
1634 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1638 // something still in the readbuffer, but not enough to satisfy 'size'?
1639 if (ftdi->readbuffer_remaining != 0)
1641 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1644 offset += ftdi->readbuffer_remaining;
1646 // do the actual USB read
1647 while (offset < size && actual_length > 0)
1649 ftdi->readbuffer_remaining = 0;
1650 ftdi->readbuffer_offset = 0;
1651 /* returns how much received */
1652 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1654 ftdi_error_return(ret, "usb bulk read failed");
1656 if (actual_length > 2)
1658 // skip FTDI status bytes.
1659 // Maybe stored in the future to enable modem use
1660 num_of_chunks = actual_length / packet_size;
1661 chunk_remains = actual_length % packet_size;
1662 //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);
1664 ftdi->readbuffer_offset += 2;
1667 if (actual_length > packet_size - 2)
1669 for (i = 1; i < num_of_chunks; i++)
1670 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1671 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1673 if (chunk_remains > 2)
1675 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1676 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1678 actual_length -= 2*num_of_chunks;
1681 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1684 else if (actual_length <= 2)
1686 // no more data to read?
1689 if (actual_length > 0)
1691 // data still fits in buf?
1692 if (offset+actual_length <= size)
1694 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1695 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1696 offset += actual_length;
1698 /* Did we read exactly the right amount of bytes? */
1700 //printf("read_data exact rem %d offset %d\n",
1701 //ftdi->readbuffer_remaining, offset);
1706 // only copy part of the data or size <= readbuffer_chunksize
1707 int part_size = size-offset;
1708 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1710 ftdi->readbuffer_offset += part_size;
1711 ftdi->readbuffer_remaining = actual_length-part_size;
1712 offset += part_size;
1714 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1715 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1726 Configure read buffer chunk size.
1729 Automatically reallocates the buffer.
1731 \param ftdi pointer to ftdi_context
1732 \param chunksize Chunk size
1735 \retval -1: ftdi context invalid
1737 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1739 unsigned char *new_buf;
1742 ftdi_error_return(-1, "ftdi context invalid");
1744 // Invalidate all remaining data
1745 ftdi->readbuffer_offset = 0;
1746 ftdi->readbuffer_remaining = 0;
1748 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1749 which is defined in libusb-1.0. Otherwise, each USB read request will
1750 be divided into multiple URBs. This will cause issues on Linux kernel
1751 older than 2.6.32. */
1752 if (chunksize > 16384)
1756 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1757 ftdi_error_return(-1, "out of memory for readbuffer");
1759 ftdi->readbuffer = new_buf;
1760 ftdi->readbuffer_chunksize = chunksize;
1766 Get read buffer chunk size.
1768 \param ftdi pointer to ftdi_context
1769 \param chunksize Pointer to store chunk size in
1772 \retval -1: FTDI context invalid
1774 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1777 ftdi_error_return(-1, "FTDI context invalid");
1779 *chunksize = ftdi->readbuffer_chunksize;
1785 Enable bitbang mode.
1787 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1789 \param ftdi pointer to ftdi_context
1790 \param bitmask Bitmask to configure lines.
1791 HIGH/ON value configures a line as output.
1794 \retval -1: can't enable bitbang mode
1795 \retval -2: USB device unavailable
1797 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1799 unsigned short usb_val;
1801 if (ftdi == NULL || ftdi->usb_dev == NULL)
1802 ftdi_error_return(-2, "USB device unavailable");
1804 usb_val = bitmask; // low byte: bitmask
1805 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1806 usb_val |= (ftdi->bitbang_mode << 8);
1808 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1809 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1810 NULL, 0, ftdi->usb_write_timeout) < 0)
1811 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1813 ftdi->bitbang_enabled = 1;
1818 Disable bitbang mode.
1820 \param ftdi pointer to ftdi_context
1823 \retval -1: can't disable bitbang mode
1824 \retval -2: USB device unavailable
1826 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1828 if (ftdi == NULL || ftdi->usb_dev == NULL)
1829 ftdi_error_return(-2, "USB device unavailable");
1831 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)
1832 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1834 ftdi->bitbang_enabled = 0;
1839 Enable/disable bitbang modes.
1841 \param ftdi pointer to ftdi_context
1842 \param bitmask Bitmask to configure lines.
1843 HIGH/ON value configures a line as output.
1844 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1847 \retval -1: can't enable bitbang mode
1848 \retval -2: USB device unavailable
1850 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1852 unsigned short usb_val;
1854 if (ftdi == NULL || ftdi->usb_dev == NULL)
1855 ftdi_error_return(-2, "USB device unavailable");
1857 usb_val = bitmask; // low byte: bitmask
1858 usb_val |= (mode << 8);
1859 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)
1860 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1862 ftdi->bitbang_mode = mode;
1863 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1868 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1870 \param ftdi pointer to ftdi_context
1871 \param pins Pointer to store pins into
1874 \retval -1: read pins failed
1875 \retval -2: USB device unavailable
1877 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1879 if (ftdi == NULL || ftdi->usb_dev == NULL)
1880 ftdi_error_return(-2, "USB device unavailable");
1882 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)
1883 ftdi_error_return(-1, "read pins failed");
1891 The FTDI chip keeps data in the internal buffer for a specific
1892 amount of time if the buffer is not full yet to decrease
1893 load on the usb bus.
1895 \param ftdi pointer to ftdi_context
1896 \param latency Value between 1 and 255
1899 \retval -1: latency out of range
1900 \retval -2: unable to set latency timer
1901 \retval -3: USB device unavailable
1903 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1905 unsigned short usb_val;
1908 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1910 if (ftdi == NULL || ftdi->usb_dev == NULL)
1911 ftdi_error_return(-3, "USB device unavailable");
1914 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)
1915 ftdi_error_return(-2, "unable to set latency timer");
1923 \param ftdi pointer to ftdi_context
1924 \param latency Pointer to store latency value in
1927 \retval -1: unable to get latency timer
1928 \retval -2: USB device unavailable
1930 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1932 unsigned short usb_val;
1934 if (ftdi == NULL || ftdi->usb_dev == NULL)
1935 ftdi_error_return(-2, "USB device unavailable");
1937 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)
1938 ftdi_error_return(-1, "reading latency timer failed");
1940 *latency = (unsigned char)usb_val;
1945 Poll modem status information
1947 This function allows the retrieve the two status bytes of the device.
1948 The device sends these bytes also as a header for each read access
1949 where they are discarded by ftdi_read_data(). The chip generates
1950 the two stripped status bytes in the absence of data every 40 ms.
1952 Layout of the first byte:
1953 - B0..B3 - must be 0
1954 - B4 Clear to send (CTS)
1957 - B5 Data set ready (DTS)
1960 - B6 Ring indicator (RI)
1963 - B7 Receive line signal detect (RLSD)
1967 Layout of the second byte:
1968 - B0 Data ready (DR)
1969 - B1 Overrun error (OE)
1970 - B2 Parity error (PE)
1971 - B3 Framing error (FE)
1972 - B4 Break interrupt (BI)
1973 - B5 Transmitter holding register (THRE)
1974 - B6 Transmitter empty (TEMT)
1975 - B7 Error in RCVR FIFO
1977 \param ftdi pointer to ftdi_context
1978 \param status Pointer to store status information in. Must be two bytes.
1981 \retval -1: unable to retrieve status information
1982 \retval -2: USB device unavailable
1984 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1988 if (ftdi == NULL || ftdi->usb_dev == NULL)
1989 ftdi_error_return(-2, "USB device unavailable");
1991 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)
1992 ftdi_error_return(-1, "getting modem status failed");
1994 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2000 Set flowcontrol for ftdi chip
2002 \param ftdi pointer to ftdi_context
2003 \param flowctrl flow control to use. should be
2004 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2007 \retval -1: set flow control failed
2008 \retval -2: USB device unavailable
2010 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2012 if (ftdi == NULL || ftdi->usb_dev == NULL)
2013 ftdi_error_return(-2, "USB device unavailable");
2015 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2016 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2017 NULL, 0, ftdi->usb_write_timeout) < 0)
2018 ftdi_error_return(-1, "set flow control failed");
2026 \param ftdi pointer to ftdi_context
2027 \param state state to set line to (1 or 0)
2030 \retval -1: set dtr failed
2031 \retval -2: USB device unavailable
2033 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2035 unsigned short usb_val;
2037 if (ftdi == NULL || ftdi->usb_dev == NULL)
2038 ftdi_error_return(-2, "USB device unavailable");
2041 usb_val = SIO_SET_DTR_HIGH;
2043 usb_val = SIO_SET_DTR_LOW;
2045 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2046 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2047 NULL, 0, ftdi->usb_write_timeout) < 0)
2048 ftdi_error_return(-1, "set dtr failed");
2056 \param ftdi pointer to ftdi_context
2057 \param state state to set line to (1 or 0)
2060 \retval -1: set rts failed
2061 \retval -2: USB device unavailable
2063 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2065 unsigned short usb_val;
2067 if (ftdi == NULL || ftdi->usb_dev == NULL)
2068 ftdi_error_return(-2, "USB device unavailable");
2071 usb_val = SIO_SET_RTS_HIGH;
2073 usb_val = SIO_SET_RTS_LOW;
2075 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2076 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2077 NULL, 0, ftdi->usb_write_timeout) < 0)
2078 ftdi_error_return(-1, "set of rts failed");
2084 Set dtr and rts line in one pass
2086 \param ftdi pointer to ftdi_context
2087 \param dtr DTR state to set line to (1 or 0)
2088 \param rts RTS state to set line to (1 or 0)
2091 \retval -1: set dtr/rts failed
2092 \retval -2: USB device unavailable
2094 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2096 unsigned short usb_val;
2098 if (ftdi == NULL || ftdi->usb_dev == NULL)
2099 ftdi_error_return(-2, "USB device unavailable");
2102 usb_val = SIO_SET_DTR_HIGH;
2104 usb_val = SIO_SET_DTR_LOW;
2107 usb_val |= SIO_SET_RTS_HIGH;
2109 usb_val |= SIO_SET_RTS_LOW;
2111 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2112 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2113 NULL, 0, ftdi->usb_write_timeout) < 0)
2114 ftdi_error_return(-1, "set of rts/dtr failed");
2120 Set the special event character
2122 \param ftdi pointer to ftdi_context
2123 \param eventch Event character
2124 \param enable 0 to disable the event character, non-zero otherwise
2127 \retval -1: unable to set event character
2128 \retval -2: USB device unavailable
2130 int ftdi_set_event_char(struct ftdi_context *ftdi,
2131 unsigned char eventch, unsigned char enable)
2133 unsigned short usb_val;
2135 if (ftdi == NULL || ftdi->usb_dev == NULL)
2136 ftdi_error_return(-2, "USB device unavailable");
2142 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)
2143 ftdi_error_return(-1, "setting event character failed");
2151 \param ftdi pointer to ftdi_context
2152 \param errorch Error character
2153 \param enable 0 to disable the error character, non-zero otherwise
2156 \retval -1: unable to set error character
2157 \retval -2: USB device unavailable
2159 int ftdi_set_error_char(struct ftdi_context *ftdi,
2160 unsigned char errorch, unsigned char enable)
2162 unsigned short usb_val;
2164 if (ftdi == NULL || ftdi->usb_dev == NULL)
2165 ftdi_error_return(-2, "USB device unavailable");
2171 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)
2172 ftdi_error_return(-1, "setting error character failed");
2178 Init eeprom with default values.
2179 \param ftdi pointer to ftdi_context
2180 \param manufacturer String to use as Manufacturer
2181 \param product String to use as Product description
2182 \param serial String to use as Serial number description
2185 \retval -1: No struct ftdi_context
2186 \retval -2: No struct ftdi_eeprom
2188 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2189 char * product, char * serial)
2191 struct ftdi_eeprom *eeprom;
2194 ftdi_error_return(-1, "No struct ftdi_context");
2196 if (ftdi->eeprom == NULL)
2197 ftdi_error_return(-2,"No struct ftdi_eeprom");
2199 eeprom = ftdi->eeprom;
2200 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2202 eeprom->vendor_id = 0x0403;
2203 eeprom->use_serial = USE_SERIAL_NUM;
2204 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2205 (ftdi->type == TYPE_R))
2206 eeprom->product_id = 0x6001;
2207 else if (ftdi->type == TYPE_4232H)
2208 eeprom->product_id = 0x6011;
2209 else if (ftdi->type == TYPE_232H)
2210 eeprom->product_id = 0x6014;
2212 eeprom->product_id = 0x6010;
2213 if (ftdi->type == TYPE_AM)
2214 eeprom->usb_version = 0x0101;
2216 eeprom->usb_version = 0x0200;
2217 eeprom->max_power = 100;
2219 if (eeprom->manufacturer)
2220 free (eeprom->manufacturer);
2221 eeprom->manufacturer = NULL;
2224 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2225 if (eeprom->manufacturer)
2226 strcpy(eeprom->manufacturer, manufacturer);
2229 if (eeprom->product)
2230 free (eeprom->product);
2231 eeprom->product = NULL;
2234 eeprom->product = malloc(strlen(product)+1);
2235 if (eeprom->product)
2236 strcpy(eeprom->product, product);
2240 free (eeprom->serial);
2241 eeprom->serial = NULL;
2244 eeprom->serial = malloc(strlen(serial)+1);
2246 strcpy(eeprom->serial, serial);
2250 if (ftdi->type == TYPE_R)
2252 eeprom->max_power = 90;
2253 eeprom->size = 0x80;
2254 eeprom->cbus_function[0] = CBUS_TXLED;
2255 eeprom->cbus_function[1] = CBUS_RXLED;
2256 eeprom->cbus_function[2] = CBUS_TXDEN;
2257 eeprom->cbus_function[3] = CBUS_PWREN;
2258 eeprom->cbus_function[4] = CBUS_SLEEP;
2262 if(ftdi->type == TYPE_232H)
2265 for (i=0; i<10; i++)
2266 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2272 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2273 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2278 int mode_low, mode_high;
2279 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2280 mode_low = CBUSH_TRISTATE;
2282 mode_low = eeprom->cbus_function[2*i];
2283 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2284 mode_high = CBUSH_TRISTATE;
2286 mode_high = eeprom->cbus_function[2*i];
2288 output[0x18+i] = mode_high <<4 | mode_low;
2292 Build binary buffer from ftdi_eeprom structure.
2293 Output is suitable for ftdi_write_eeprom().
2295 \param ftdi pointer to ftdi_context
2297 \retval >=0: size of eeprom user area in bytes
2298 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2299 \retval -2: Invalid eeprom or ftdi pointer
2300 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2301 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2302 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2303 \retval -6: No connected EEPROM or EEPROM Type unknown
2305 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2307 unsigned char i, j, eeprom_size_mask;
2308 unsigned short checksum, value;
2309 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2311 struct ftdi_eeprom *eeprom;
2312 unsigned char * output;
2315 ftdi_error_return(-2,"No context");
2316 if (ftdi->eeprom == NULL)
2317 ftdi_error_return(-2,"No eeprom structure");
2319 eeprom= ftdi->eeprom;
2320 output = eeprom->buf;
2322 if (eeprom->chip == -1)
2323 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2325 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2326 eeprom->size = 0x100;
2328 eeprom->size = 0x80;
2330 if (eeprom->manufacturer != NULL)
2331 manufacturer_size = strlen(eeprom->manufacturer);
2332 if (eeprom->product != NULL)
2333 product_size = strlen(eeprom->product);
2334 if (eeprom->serial != NULL)
2335 serial_size = strlen(eeprom->serial);
2337 // eeprom size check
2342 user_area_size = 96; // base size for strings (total of 48 characters)
2345 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2348 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2350 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2352 user_area_size = 86;
2358 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2360 if (user_area_size < 0)
2361 ftdi_error_return(-1,"eeprom size exceeded");
2364 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2366 // Bytes and Bits set for all Types
2368 // Addr 02: Vendor ID
2369 output[0x02] = eeprom->vendor_id;
2370 output[0x03] = eeprom->vendor_id >> 8;
2372 // Addr 04: Product ID
2373 output[0x04] = eeprom->product_id;
2374 output[0x05] = eeprom->product_id >> 8;
2376 // Addr 06: Device release number (0400h for BM features)
2377 output[0x06] = 0x00;
2381 output[0x07] = 0x02;
2384 output[0x07] = 0x04;
2387 output[0x07] = 0x05;
2390 output[0x07] = 0x06;
2393 output[0x07] = 0x07;
2396 output[0x07] = 0x08;
2399 output[0x07] = 0x09;
2402 output[0x07] = 0x00;
2405 // Addr 08: Config descriptor
2407 // Bit 6: 1 if this device is self powered, 0 if bus powered
2408 // Bit 5: 1 if this device uses remote wakeup
2409 // Bit 4-0: reserved - 0
2411 if (eeprom->self_powered == 1)
2413 if (eeprom->remote_wakeup == 1)
2417 // Addr 09: Max power consumption: max power = value * 2 mA
2418 output[0x09] = eeprom->max_power>>1;
2420 if (ftdi->type != TYPE_AM)
2422 // Addr 0A: Chip configuration
2423 // Bit 7: 0 - reserved
2424 // Bit 6: 0 - reserved
2425 // Bit 5: 0 - reserved
2426 // Bit 4: 1 - Change USB version
2427 // Bit 3: 1 - Use the serial number string
2428 // Bit 2: 1 - Enable suspend pull downs for lower power
2429 // Bit 1: 1 - Out EndPoint is Isochronous
2430 // Bit 0: 1 - In EndPoint is Isochronous
2433 if (eeprom->in_is_isochronous == 1)
2435 if (eeprom->out_is_isochronous == 1)
2441 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2442 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2460 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2461 eeprom_size_mask = eeprom->size -1;
2463 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2464 // Addr 0F: Length of manufacturer string
2465 // Output manufacturer
2466 output[0x0E] = i; // calculate offset
2467 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2468 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2469 for (j = 0; j < manufacturer_size; j++)
2471 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2472 output[i & eeprom_size_mask] = 0x00, i++;
2474 output[0x0F] = manufacturer_size*2 + 2;
2476 // Addr 10: Offset of the product string + 0x80, calculated later
2477 // Addr 11: Length of product string
2478 output[0x10] = i | 0x80; // calculate offset
2479 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2480 output[i & eeprom_size_mask] = 0x03, i++;
2481 for (j = 0; j < product_size; j++)
2483 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2484 output[i & eeprom_size_mask] = 0x00, i++;
2486 output[0x11] = product_size*2 + 2;
2488 // Addr 12: Offset of the serial string + 0x80, calculated later
2489 // Addr 13: Length of serial string
2490 output[0x12] = i | 0x80; // calculate offset
2491 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2492 output[i & eeprom_size_mask] = 0x03, i++;
2493 for (j = 0; j < serial_size; j++)
2495 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2496 output[i & eeprom_size_mask] = 0x00, i++;
2499 // Legacy port name and PnP fields for FT2232 and newer chips
2500 if (ftdi->type > TYPE_BM)
2502 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2504 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2506 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2510 output[0x13] = serial_size*2 + 2;
2512 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2514 if (eeprom->use_serial == USE_SERIAL_NUM )
2515 output[0x0A] |= USE_SERIAL_NUM;
2517 output[0x0A] &= ~USE_SERIAL_NUM;
2520 /* Bytes and Bits specific to (some) types
2521 Write linear, as this allows easier fixing*/
2527 output[0x0C] = eeprom->usb_version & 0xff;
2528 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2529 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2530 output[0x0A] |= USE_USB_VERSION_BIT;
2532 output[0x0A] &= ~USE_USB_VERSION_BIT;
2537 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0x7):0;
2538 if ( eeprom->channel_a_driver == DRIVER_VCP)
2539 output[0x00] |= DRIVER_VCP;
2541 output[0x00] &= ~DRIVER_VCP;
2543 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2544 output[0x00] |= HIGH_CURRENT_DRIVE;
2546 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2548 output[0x01] = (eeprom->channel_b_type)?((1<<(eeprom->channel_b_type)) & 0x7):0;
2549 if ( eeprom->channel_b_driver == DRIVER_VCP)
2550 output[0x01] |= DRIVER_VCP;
2552 output[0x01] &= ~DRIVER_VCP;
2554 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2555 output[0x01] |= HIGH_CURRENT_DRIVE;
2557 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2559 if (eeprom->in_is_isochronous == 1)
2560 output[0x0A] |= 0x1;
2562 output[0x0A] &= ~0x1;
2563 if (eeprom->out_is_isochronous == 1)
2564 output[0x0A] |= 0x2;
2566 output[0x0A] &= ~0x2;
2567 if (eeprom->suspend_pull_downs == 1)
2568 output[0x0A] |= 0x4;
2570 output[0x0A] &= ~0x4;
2571 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2572 output[0x0A] |= USE_USB_VERSION_BIT;
2574 output[0x0A] &= ~USE_USB_VERSION_BIT;
2576 output[0x0C] = eeprom->usb_version & 0xff;
2577 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2578 output[0x14] = eeprom->chip;
2581 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2582 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2583 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2585 if (eeprom->suspend_pull_downs == 1)
2586 output[0x0A] |= 0x4;
2588 output[0x0A] &= ~0x4;
2589 output[0x0B] = eeprom->invert;
2590 output[0x0C] = eeprom->usb_version & 0xff;
2591 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2593 if (eeprom->cbus_function[0] > CBUS_BB)
2594 output[0x14] = CBUS_TXLED;
2596 output[0x14] = eeprom->cbus_function[0];
2598 if (eeprom->cbus_function[1] > CBUS_BB)
2599 output[0x14] |= CBUS_RXLED<<4;
2601 output[0x14] |= eeprom->cbus_function[1]<<4;
2603 if (eeprom->cbus_function[2] > CBUS_BB)
2604 output[0x15] = CBUS_TXDEN;
2606 output[0x15] = eeprom->cbus_function[2];
2608 if (eeprom->cbus_function[3] > CBUS_BB)
2609 output[0x15] |= CBUS_PWREN<<4;
2611 output[0x15] |= eeprom->cbus_function[3]<<4;
2613 if (eeprom->cbus_function[4] > CBUS_CLK6)
2614 output[0x16] = CBUS_SLEEP;
2616 output[0x16] = eeprom->cbus_function[4];
2619 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0x7):0;
2620 if ( eeprom->channel_a_driver == DRIVER_VCP)
2621 output[0x00] |= DRIVER_VCP;
2623 output[0x00] &= ~DRIVER_VCP;
2625 output[0x01] = (eeprom->channel_b_type)?((1<<(eeprom->channel_b_type)) & 0x7):0;
2626 if ( eeprom->channel_b_driver == DRIVER_VCP)
2627 output[0x01] |= DRIVER_VCP;
2629 output[0x01] &= ~DRIVER_VCP;
2630 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2631 output[0x01] |= SUSPEND_DBUS7_BIT;
2633 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2635 if (eeprom->suspend_pull_downs == 1)
2636 output[0x0A] |= 0x4;
2638 output[0x0A] &= ~0x4;
2640 if (eeprom->group0_drive > DRIVE_16MA)
2641 output[0x0c] |= DRIVE_16MA;
2643 output[0x0c] |= eeprom->group0_drive;
2644 if (eeprom->group0_schmitt == IS_SCHMITT)
2645 output[0x0c] |= IS_SCHMITT;
2646 if (eeprom->group0_slew == SLOW_SLEW)
2647 output[0x0c] |= SLOW_SLEW;
2649 if (eeprom->group1_drive > DRIVE_16MA)
2650 output[0x0c] |= DRIVE_16MA<<4;
2652 output[0x0c] |= eeprom->group1_drive<<4;
2653 if (eeprom->group1_schmitt == IS_SCHMITT)
2654 output[0x0c] |= IS_SCHMITT<<4;
2655 if (eeprom->group1_slew == SLOW_SLEW)
2656 output[0x0c] |= SLOW_SLEW<<4;
2658 if (eeprom->group2_drive > DRIVE_16MA)
2659 output[0x0d] |= DRIVE_16MA;
2661 output[0x0d] |= eeprom->group2_drive;
2662 if (eeprom->group2_schmitt == IS_SCHMITT)
2663 output[0x0d] |= IS_SCHMITT;
2664 if (eeprom->group2_slew == SLOW_SLEW)
2665 output[0x0d] |= SLOW_SLEW;
2667 if (eeprom->group3_drive > DRIVE_16MA)
2668 output[0x0d] |= DRIVE_16MA<<4;
2670 output[0x0d] |= eeprom->group3_drive<<4;
2671 if (eeprom->group3_schmitt == IS_SCHMITT)
2672 output[0x0d] |= IS_SCHMITT<<4;
2673 if (eeprom->group3_slew == SLOW_SLEW)
2674 output[0x0d] |= SLOW_SLEW<<4;
2676 output[0x18] = eeprom->chip;
2680 output[0x18] = eeprom->chip;
2681 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2684 output[0x00] = (eeprom->channel_a_type)?((1<<(eeprom->channel_a_type)) & 0xf):0;
2685 if ( eeprom->channel_a_driver == DRIVER_VCP)
2686 output[0x00] |= DRIVER_VCPH;
2688 output[0x00] &= ~DRIVER_VCPH;
2689 if (eeprom->powersave)
2690 output[0x01] |= POWER_SAVE_DISABLE_H;
2692 output[0x01] &= ~POWER_SAVE_DISABLE_H;
2693 if (eeprom->clock_polarity)
2694 output[0x01] |= FT1284_CLK_IDLE_STATE;
2696 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
2697 if (eeprom->data_order)
2698 output[0x01] |= FT1284_DATA_LSB;
2700 output[0x01] &= ~FT1284_DATA_LSB;
2701 if (eeprom->flow_control)
2702 output[0x01] |= FT1284_FLOW_CONTROL;
2704 output[0x01] &= ~FT1284_FLOW_CONTROL;
2705 if (eeprom->group0_drive > DRIVE_16MA)
2706 output[0x0c] |= DRIVE_16MA;
2708 output[0x0c] |= eeprom->group0_drive;
2709 if (eeprom->group0_schmitt == IS_SCHMITT)
2710 output[0x0c] |= IS_SCHMITT;
2711 if (eeprom->group0_slew == SLOW_SLEW)
2712 output[0x0c] |= SLOW_SLEW;
2714 if (eeprom->group1_drive > DRIVE_16MA)
2715 output[0x0d] |= DRIVE_16MA;
2717 output[0x0d] |= eeprom->group1_drive;
2718 if (eeprom->group1_schmitt == IS_SCHMITT)
2719 output[0x0d] |= IS_SCHMITT;
2720 if (eeprom->group1_slew == SLOW_SLEW)
2721 output[0x0d] |= SLOW_SLEW;
2723 set_ft232h_cbus(eeprom, output);
2725 output[0x1e] = eeprom->chip;
2726 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
2731 // calculate checksum
2734 for (i = 0; i < eeprom->size/2-1; i++)
2736 value = output[i*2];
2737 value += output[(i*2)+1] << 8;
2739 checksum = value^checksum;
2740 checksum = (checksum << 1) | (checksum >> 15);
2743 output[eeprom->size-2] = checksum;
2744 output[eeprom->size-1] = checksum >> 8;
2746 return user_area_size;
2748 /* FTD2XX doesn't allow to set multiple bits in the interface mode bitfield*/
2749 unsigned char bit2type(unsigned char bits)
2759 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
2766 Decode binary EEPROM image into an ftdi_eeprom structure.
2768 \param ftdi pointer to ftdi_context
2769 \param verbose Decode EEPROM on stdout
2772 \retval -1: something went wrong
2774 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2775 FIXME: Strings are malloc'ed here and should be freed somewhere
2777 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2780 unsigned short checksum, eeprom_checksum, value;
2781 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2783 struct ftdi_eeprom *eeprom;
2784 unsigned char *buf = ftdi->eeprom->buf;
2788 ftdi_error_return(-1,"No context");
2789 if (ftdi->eeprom == NULL)
2790 ftdi_error_return(-1,"No eeprom structure");
2792 eeprom = ftdi->eeprom;
2793 eeprom_size = eeprom->size;
2795 // Addr 02: Vendor ID
2796 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2798 // Addr 04: Product ID
2799 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2801 release = buf[0x06] + (buf[0x07]<<8);
2803 // Addr 08: Config descriptor
2805 // Bit 6: 1 if this device is self powered, 0 if bus powered
2806 // Bit 5: 1 if this device uses remote wakeup
2807 eeprom->self_powered = buf[0x08] & 0x40;
2808 eeprom->remote_wakeup = buf[0x08] & 0x20;
2810 // Addr 09: Max power consumption: max power = value * 2 mA
2811 eeprom->max_power = buf[0x09];
2813 // Addr 0A: Chip configuration
2814 // Bit 7: 0 - reserved
2815 // Bit 6: 0 - reserved
2816 // Bit 5: 0 - reserved
2817 // Bit 4: 1 - Change USB version on BM and 2232C
2818 // Bit 3: 1 - Use the serial number string
2819 // Bit 2: 1 - Enable suspend pull downs for lower power
2820 // Bit 1: 1 - Out EndPoint is Isochronous
2821 // Bit 0: 1 - In EndPoint is Isochronous
2823 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2824 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2825 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2826 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2827 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2829 // Addr 0C: USB version low byte when 0x0A
2830 // Addr 0D: USB version high byte when 0x0A
2831 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2833 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2834 // Addr 0F: Length of manufacturer string
2835 manufacturer_size = buf[0x0F]/2;
2836 if (eeprom->manufacturer)
2837 free(eeprom->manufacturer);
2838 if (manufacturer_size > 0)
2840 eeprom->manufacturer = malloc(manufacturer_size);
2841 if (eeprom->manufacturer)
2843 // Decode manufacturer
2844 i = buf[0x0E] & (eeprom_size -1); // offset
2845 for (j=0;j<manufacturer_size-1;j++)
2847 eeprom->manufacturer[j] = buf[2*j+i+2];
2849 eeprom->manufacturer[j] = '\0';
2852 else eeprom->manufacturer = NULL;
2854 // Addr 10: Offset of the product string + 0x80, calculated later
2855 // Addr 11: Length of product string
2856 if (eeprom->product)
2857 free(eeprom->product);
2858 product_size = buf[0x11]/2;
2859 if (product_size > 0)
2861 eeprom->product = malloc(product_size);
2862 if (eeprom->product)
2864 // Decode product name
2865 i = buf[0x10] & (eeprom_size -1); // offset
2866 for (j=0;j<product_size-1;j++)
2868 eeprom->product[j] = buf[2*j+i+2];
2870 eeprom->product[j] = '\0';
2873 else eeprom->product = NULL;
2875 // Addr 12: Offset of the serial string + 0x80, calculated later
2876 // Addr 13: Length of serial string
2878 free(eeprom->serial);
2879 serial_size = buf[0x13]/2;
2880 if (serial_size > 0)
2882 eeprom->serial = malloc(serial_size);
2886 i = buf[0x12] & (eeprom_size -1); // offset
2887 for (j=0;j<serial_size-1;j++)
2889 eeprom->serial[j] = buf[2*j+i+2];
2891 eeprom->serial[j] = '\0';
2894 else eeprom->serial = NULL;
2899 for (i = 0; i < eeprom_size/2-1; i++)
2902 value += buf[(i*2)+1] << 8;
2904 checksum = value^checksum;
2905 checksum = (checksum << 1) | (checksum >> 15);
2908 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2910 if (eeprom_checksum != checksum)
2912 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2913 ftdi_error_return(-1,"EEPROM checksum error");
2916 eeprom->channel_a_type = 0;
2917 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2921 else if (ftdi->type == TYPE_2232C)
2923 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2924 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2925 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2926 eeprom->channel_b_type = buf[0x01] & 0x7;
2927 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2928 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2929 eeprom->chip = buf[0x14];
2931 else if (ftdi->type == TYPE_R)
2933 /* TYPE_R flags D2XX, not VCP as all others*/
2934 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2935 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2936 if ( (buf[0x01]&0x40) != 0x40)
2938 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2939 " If this happened with the\n"
2940 " EEPROM programmed by FTDI tools, please report "
2941 "to libftdi@developer.intra2net.com\n");
2943 eeprom->chip = buf[0x16];
2944 // Addr 0B: Invert data lines
2945 // Works only on FT232R, not FT245R, but no way to distinguish
2946 eeprom->invert = buf[0x0B];
2947 // Addr 14: CBUS function: CBUS0, CBUS1
2948 // Addr 15: CBUS function: CBUS2, CBUS3
2949 // Addr 16: CBUS function: CBUS5
2950 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2951 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2952 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2953 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2954 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2956 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2958 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2959 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2960 eeprom->channel_b_type = buf[0x01] & 0x7;
2961 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2963 if (ftdi->type == TYPE_2232H)
2964 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2966 eeprom->chip = buf[0x18];
2967 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2968 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2969 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2970 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2971 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2972 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2973 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2974 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2975 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2976 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2977 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2978 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2980 else if (ftdi->type == TYPE_232H)
2984 eeprom->channel_a_type = buf[0x00] & 0xf;
2985 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
2986 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
2987 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
2988 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
2989 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
2990 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2991 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2992 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2993 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
2994 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
2995 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
2999 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3000 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3002 eeprom->chip = buf[0x1e];
3003 /*FIXME: Decipher more values*/
3008 char *channel_mode[] = {"UART","245","CPU", "OPTO", "FT1284"};
3009 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3010 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3011 fprintf(stdout, "Release: 0x%04x\n",release);
3013 if (eeprom->self_powered)
3014 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3016 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
3017 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3018 if (eeprom->manufacturer)
3019 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3020 if (eeprom->product)
3021 fprintf(stdout, "Product: %s\n",eeprom->product);
3023 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3024 fprintf(stdout, "Checksum : %04x\n", checksum);
3025 if (ftdi->type == TYPE_R)
3026 fprintf(stdout, "Internal EEPROM\n");
3027 else if (eeprom->chip >= 0x46)
3028 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3029 if (eeprom->suspend_dbus7)
3030 fprintf(stdout, "Suspend on DBUS7\n");
3031 if (eeprom->suspend_pull_downs)
3032 fprintf(stdout, "Pull IO pins low during suspend\n");
3033 if(eeprom->powersave)
3035 if(ftdi->type >= TYPE_232H)
3036 fprintf(stdout,"Enter low power state on ACBUS7\n");
3038 if (eeprom->remote_wakeup)
3039 fprintf(stdout, "Enable Remote Wake Up\n");
3040 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3041 if (ftdi->type >= TYPE_2232C)
3042 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3043 channel_mode[eeprom->channel_a_type],
3044 (eeprom->channel_a_driver)?" VCP":"",
3045 (eeprom->high_current_a)?" High Current IO":"");
3046 if (ftdi->type >= TYPE_232H)
3048 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3049 (eeprom->clock_polarity)?"HIGH":"LOW",
3050 (eeprom->data_order)?"LSB":"MSB",
3051 (eeprom->flow_control)?"":"No ");
3053 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
3054 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3055 channel_mode[eeprom->channel_b_type],
3056 (eeprom->channel_b_driver)?" VCP":"",
3057 (eeprom->high_current_b)?" High Current IO":"");
3058 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3059 eeprom->use_usb_version == USE_USB_VERSION_BIT)
3060 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3062 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3064 fprintf(stdout,"%s has %d mA drive%s%s\n",
3065 (ftdi->type == TYPE_2232H)?"AL":"A",
3066 (eeprom->group0_drive+1) *4,
3067 (eeprom->group0_schmitt)?" Schmitt Input":"",
3068 (eeprom->group0_slew)?" Slow Slew":"");
3069 fprintf(stdout,"%s has %d mA drive%s%s\n",
3070 (ftdi->type == TYPE_2232H)?"AH":"B",
3071 (eeprom->group1_drive+1) *4,
3072 (eeprom->group1_schmitt)?" Schmitt Input":"",
3073 (eeprom->group1_slew)?" Slow Slew":"");
3074 fprintf(stdout,"%s has %d mA drive%s%s\n",
3075 (ftdi->type == TYPE_2232H)?"BL":"C",
3076 (eeprom->group2_drive+1) *4,
3077 (eeprom->group2_schmitt)?" Schmitt Input":"",
3078 (eeprom->group2_slew)?" Slow Slew":"");
3079 fprintf(stdout,"%s has %d mA drive%s%s\n",
3080 (ftdi->type == TYPE_2232H)?"BH":"D",
3081 (eeprom->group3_drive+1) *4,
3082 (eeprom->group3_schmitt)?" Schmitt Input":"",
3083 (eeprom->group3_slew)?" Slow Slew":"");
3085 else if (ftdi->type == TYPE_232H)
3088 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3089 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3090 "CLK30","CLK15","CLK7_5"
3092 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3093 (eeprom->group0_drive+1) *4,
3094 (eeprom->group0_schmitt)?" Schmitt Input":"",
3095 (eeprom->group0_slew)?" Slow Slew":"");
3096 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3097 (eeprom->group1_drive+1) *4,
3098 (eeprom->group1_schmitt)?" Schmitt Input":"",
3099 (eeprom->group1_slew)?" Slow Slew":"");
3100 for (i=0; i<10; i++)
3102 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3103 fprintf(stdout,"C%d Function: %s\n", i,
3104 cbush_mux[eeprom->cbus_function[i]]);
3109 if (ftdi->type == TYPE_R)
3111 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3112 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3113 "IOMODE","BB_WR","BB_RD"
3115 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3119 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
3120 fprintf(stdout,"Inverted bits:");
3122 if ((eeprom->invert & (1<<i)) == (1<<i))
3123 fprintf(stdout," %s",r_bits[i]);
3124 fprintf(stdout,"\n");
3128 if (eeprom->cbus_function[i]<CBUS_BB)
3129 fprintf(stdout,"C%d Function: %s\n", i,
3130 cbus_mux[eeprom->cbus_function[i]]);
3134 /* Running MPROG show that C0..3 have fixed function Synchronous
3136 fprintf(stdout,"C%d BB Function: %s\n", i,
3139 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3148 Get a value from the decoded EEPROM structure
3150 \param ftdi pointer to ftdi_context
3151 \param value_name Enum of the value to query
3152 \param value Pointer to store read value
3155 \retval -1: Value doesn't exist
3157 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3162 *value = ftdi->eeprom->vendor_id;
3165 *value = ftdi->eeprom->product_id;
3168 *value = ftdi->eeprom->self_powered;
3171 *value = ftdi->eeprom->remote_wakeup;
3174 *value = ftdi->eeprom->is_not_pnp;
3177 *value = ftdi->eeprom->suspend_dbus7;
3179 case IN_IS_ISOCHRONOUS:
3180 *value = ftdi->eeprom->in_is_isochronous;
3182 case SUSPEND_PULL_DOWNS:
3183 *value = ftdi->eeprom->suspend_pull_downs;
3186 *value = ftdi->eeprom->use_serial;
3189 *value = ftdi->eeprom->usb_version;
3192 *value = ftdi->eeprom->max_power;
3194 case CHANNEL_A_TYPE:
3195 *value = ftdi->eeprom->channel_a_type;
3197 case CHANNEL_B_TYPE:
3198 *value = ftdi->eeprom->channel_b_type;
3200 case CHANNEL_A_DRIVER:
3201 *value = ftdi->eeprom->channel_a_driver;
3203 case CHANNEL_B_DRIVER:
3204 *value = ftdi->eeprom->channel_b_driver;
3206 case CBUS_FUNCTION_0:
3207 *value = ftdi->eeprom->cbus_function[0];
3209 case CBUS_FUNCTION_1:
3210 *value = ftdi->eeprom->cbus_function[1];
3212 case CBUS_FUNCTION_2:
3213 *value = ftdi->eeprom->cbus_function[2];
3215 case CBUS_FUNCTION_3:
3216 *value = ftdi->eeprom->cbus_function[3];
3218 case CBUS_FUNCTION_4:
3219 *value = ftdi->eeprom->cbus_function[4];
3221 case CBUS_FUNCTION_5:
3222 *value = ftdi->eeprom->cbus_function[5];
3224 case CBUS_FUNCTION_6:
3225 *value = ftdi->eeprom->cbus_function[6];
3227 case CBUS_FUNCTION_7:
3228 *value = ftdi->eeprom->cbus_function[7];
3230 case CBUS_FUNCTION_8:
3231 *value = ftdi->eeprom->cbus_function[8];
3233 case CBUS_FUNCTION_9:
3234 *value = ftdi->eeprom->cbus_function[8];
3237 *value = ftdi->eeprom->high_current;
3239 case HIGH_CURRENT_A:
3240 *value = ftdi->eeprom->high_current_a;
3242 case HIGH_CURRENT_B:
3243 *value = ftdi->eeprom->high_current_b;
3246 *value = ftdi->eeprom->invert;
3249 *value = ftdi->eeprom->group0_drive;
3251 case GROUP0_SCHMITT:
3252 *value = ftdi->eeprom->group0_schmitt;
3255 *value = ftdi->eeprom->group0_slew;
3258 *value = ftdi->eeprom->group1_drive;
3260 case GROUP1_SCHMITT:
3261 *value = ftdi->eeprom->group1_schmitt;
3264 *value = ftdi->eeprom->group1_slew;
3267 *value = ftdi->eeprom->group2_drive;
3269 case GROUP2_SCHMITT:
3270 *value = ftdi->eeprom->group2_schmitt;
3273 *value = ftdi->eeprom->group2_slew;
3276 *value = ftdi->eeprom->group3_drive;
3278 case GROUP3_SCHMITT:
3279 *value = ftdi->eeprom->group3_schmitt;
3282 *value = ftdi->eeprom->group3_slew;
3285 *value = ftdi->eeprom->powersave;
3287 case CLOCK_POLARITY:
3288 *value = ftdi->eeprom->clock_polarity;
3291 *value = ftdi->eeprom->data_order;
3294 *value = ftdi->eeprom->flow_control;
3297 *value = ftdi->eeprom->chip;
3300 *value = ftdi->eeprom->size;
3303 ftdi_error_return(-1, "Request for unknown EEPROM value");
3309 Set a value in the decoded EEPROM Structure
3310 No parameter checking is performed
3312 \param ftdi pointer to ftdi_context
3313 \param value_name Enum of the value to set
3317 \retval -1: Value doesn't exist
3318 \retval -2: Value not user settable
3320 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3325 ftdi->eeprom->vendor_id = value;
3328 ftdi->eeprom->product_id = value;
3331 ftdi->eeprom->self_powered = value;
3334 ftdi->eeprom->remote_wakeup = value;
3337 ftdi->eeprom->is_not_pnp = value;
3340 ftdi->eeprom->suspend_dbus7 = value;
3342 case IN_IS_ISOCHRONOUS:
3343 ftdi->eeprom->in_is_isochronous = value;
3345 case SUSPEND_PULL_DOWNS:
3346 ftdi->eeprom->suspend_pull_downs = value;
3349 ftdi->eeprom->use_serial = value;
3352 ftdi->eeprom->usb_version = value;
3355 ftdi->eeprom->max_power = value;
3357 case CHANNEL_A_TYPE:
3358 ftdi->eeprom->channel_a_type = value;
3360 case CHANNEL_B_TYPE:
3361 ftdi->eeprom->channel_b_type = value;
3363 case CHANNEL_A_DRIVER:
3364 ftdi->eeprom->channel_a_driver = value;
3366 case CHANNEL_B_DRIVER:
3367 ftdi->eeprom->channel_b_driver = value;
3369 case CBUS_FUNCTION_0:
3370 ftdi->eeprom->cbus_function[0] = value;
3372 case CBUS_FUNCTION_1:
3373 ftdi->eeprom->cbus_function[1] = value;
3375 case CBUS_FUNCTION_2:
3376 ftdi->eeprom->cbus_function[2] = value;
3378 case CBUS_FUNCTION_3:
3379 ftdi->eeprom->cbus_function[3] = value;
3381 case CBUS_FUNCTION_4:
3382 ftdi->eeprom->cbus_function[4] = value;
3384 case CBUS_FUNCTION_5:
3385 ftdi->eeprom->cbus_function[5] = value;
3387 case CBUS_FUNCTION_6:
3388 ftdi->eeprom->cbus_function[6] = value;
3390 case CBUS_FUNCTION_7:
3391 ftdi->eeprom->cbus_function[7] = value;
3393 case CBUS_FUNCTION_8:
3394 ftdi->eeprom->cbus_function[8] = value;
3396 case CBUS_FUNCTION_9:
3397 ftdi->eeprom->cbus_function[9] = value;
3400 ftdi->eeprom->high_current = value;
3402 case HIGH_CURRENT_A:
3403 ftdi->eeprom->high_current_a = value;
3405 case HIGH_CURRENT_B:
3406 ftdi->eeprom->high_current_b = value;
3409 ftdi->eeprom->invert = value;
3412 ftdi->eeprom->group0_drive = value;
3414 case GROUP0_SCHMITT:
3415 ftdi->eeprom->group0_schmitt = value;
3418 ftdi->eeprom->group0_slew = value;
3421 ftdi->eeprom->group1_drive = value;
3423 case GROUP1_SCHMITT:
3424 ftdi->eeprom->group1_schmitt = value;
3427 ftdi->eeprom->group1_slew = value;
3430 ftdi->eeprom->group2_drive = value;
3432 case GROUP2_SCHMITT:
3433 ftdi->eeprom->group2_schmitt = value;
3436 ftdi->eeprom->group2_slew = value;
3439 ftdi->eeprom->group3_drive = value;
3441 case GROUP3_SCHMITT:
3442 ftdi->eeprom->group3_schmitt = value;
3445 ftdi->eeprom->group3_slew = value;
3448 ftdi->eeprom->chip = value;
3451 ftdi->eeprom->powersave = value;
3453 case CLOCK_POLARITY:
3454 ftdi->eeprom->clock_polarity = value;
3457 ftdi->eeprom->data_order = value;
3460 ftdi->eeprom->flow_control = value;
3463 ftdi_error_return(-2, "EEPROM Value can't be changed");
3465 ftdi_error_return(-1, "Request to unknown EEPROM value");
3470 /** Get the read-only buffer to the binary EEPROM content
3472 \param ftdi pointer to ftdi_context
3473 \param buf buffer to receive EEPROM content
3474 \param size Size of receiving buffer
3477 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3478 \retval -2: Not enough room to store eeprom
3480 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3482 if (!ftdi || !(ftdi->eeprom))
3483 ftdi_error_return(-1, "No appropriate structure");
3485 if (!buf || size < ftdi->eeprom->size)
3486 ftdi_error_return(-1, "Not enough room to store eeprom");
3488 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3489 if (size > FTDI_MAX_EEPROM_SIZE)
3490 size = FTDI_MAX_EEPROM_SIZE;
3492 memcpy(buf, ftdi->eeprom->buf, size);
3498 Read eeprom location
3500 \param ftdi pointer to ftdi_context
3501 \param eeprom_addr Address of eeprom location to be read
3502 \param eeprom_val Pointer to store read eeprom location
3505 \retval -1: read failed
3506 \retval -2: USB device unavailable
3508 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3510 if (ftdi == NULL || ftdi->usb_dev == NULL)
3511 ftdi_error_return(-2, "USB device unavailable");
3513 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)
3514 ftdi_error_return(-1, "reading eeprom failed");
3522 \param ftdi pointer to ftdi_context
3525 \retval -1: read failed
3526 \retval -2: USB device unavailable
3528 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3533 if (ftdi == NULL || ftdi->usb_dev == NULL)
3534 ftdi_error_return(-2, "USB device unavailable");
3535 buf = ftdi->eeprom->buf;
3537 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3539 if (libusb_control_transfer(
3540 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3541 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3542 ftdi_error_return(-1, "reading eeprom failed");
3545 if (ftdi->type == TYPE_R)
3546 ftdi->eeprom->size = 0x80;
3547 /* Guesses size of eeprom by comparing halves
3548 - will not work with blank eeprom */
3549 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3550 ftdi->eeprom->size = -1;
3551 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3552 ftdi->eeprom->size = 0x80;
3553 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3554 ftdi->eeprom->size = 0x40;
3556 ftdi->eeprom->size = 0x100;
3561 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3562 Function is only used internally
3565 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3567 return ((value & 1) << 1) |
3568 ((value & 2) << 5) |
3569 ((value & 4) >> 2) |
3570 ((value & 8) << 4) |
3571 ((value & 16) >> 1) |
3572 ((value & 32) >> 1) |
3573 ((value & 64) >> 4) |
3574 ((value & 128) >> 2);
3578 Read the FTDIChip-ID from R-type devices
3580 \param ftdi pointer to ftdi_context
3581 \param chipid Pointer to store FTDIChip-ID
3584 \retval -1: read failed
3585 \retval -2: USB device unavailable
3587 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3589 unsigned int a = 0, b = 0;
3591 if (ftdi == NULL || ftdi->usb_dev == NULL)
3592 ftdi_error_return(-2, "USB device unavailable");
3594 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)
3596 a = a << 8 | a >> 8;
3597 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)
3599 b = b << 8 | b >> 8;
3600 a = (a << 16) | (b & 0xFFFF);
3601 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3602 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3603 *chipid = a ^ 0xa5f0f7d1;
3608 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3612 Write eeprom location
3614 \param ftdi pointer to ftdi_context
3615 \param eeprom_addr Address of eeprom location to be written
3616 \param eeprom_val Value to be written
3619 \retval -1: write failed
3620 \retval -2: USB device unavailable
3621 \retval -3: Invalid access to checksum protected area below 0x80
3622 \retval -4: Device can't access unprotected area
3623 \retval -5: Reading chip type failed
3625 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3626 unsigned short eeprom_val)
3628 int chip_type_location;
3629 unsigned short chip_type;
3631 if (ftdi == NULL || ftdi->usb_dev == NULL)
3632 ftdi_error_return(-2, "USB device unavailable");
3634 if (eeprom_addr <0x80)
3635 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3642 chip_type_location = 0x14;
3646 chip_type_location = 0x18;
3649 chip_type_location = 0x1e;
3652 ftdi_error_return(-4, "Device can't access unprotected area");
3655 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3656 ftdi_error_return(-5, "Reading failed failed");
3657 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3658 if ((chip_type & 0xff) != 0x66)
3660 ftdi_error_return(-6, "EEPROM is not of 93x66");
3663 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3664 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3665 NULL, 0, ftdi->usb_write_timeout) != 0)
3666 ftdi_error_return(-1, "unable to write eeprom");
3674 \param ftdi pointer to ftdi_context
3677 \retval -1: read failed
3678 \retval -2: USB device unavailable
3680 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3682 unsigned short usb_val, status;
3684 unsigned char *eeprom;
3686 if (ftdi == NULL || ftdi->usb_dev == NULL)
3687 ftdi_error_return(-2, "USB device unavailable");
3688 eeprom = ftdi->eeprom->buf;
3690 /* These commands were traced while running MProg */
3691 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3693 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3695 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3698 for (i = 0; i < ftdi->eeprom->size/2; i++)
3700 usb_val = eeprom[i*2];
3701 usb_val += eeprom[(i*2)+1] << 8;
3702 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3703 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3704 NULL, 0, ftdi->usb_write_timeout) < 0)
3705 ftdi_error_return(-1, "unable to write eeprom");
3714 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3716 \param ftdi pointer to ftdi_context
3719 \retval -1: erase failed
3720 \retval -2: USB device unavailable
3721 \retval -3: Writing magic failed
3722 \retval -4: Read EEPROM failed
3723 \retval -5: Unexpected EEPROM value
3725 #define MAGIC 0x55aa
3726 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3728 unsigned short eeprom_value;
3729 if (ftdi == NULL || ftdi->usb_dev == NULL)
3730 ftdi_error_return(-2, "USB device unavailable");
3732 if (ftdi->type == TYPE_R)
3734 ftdi->eeprom->chip = 0;
3738 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3739 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3740 ftdi_error_return(-1, "unable to erase eeprom");
3743 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3744 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3745 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3746 Chip is 93x66 if magic is only read at word position 0xc0*/
3747 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3748 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3749 NULL, 0, ftdi->usb_write_timeout) != 0)
3750 ftdi_error_return(-3, "Writing magic failed");
3751 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3752 ftdi_error_return(-4, "Reading failed failed");
3753 if (eeprom_value == MAGIC)
3755 ftdi->eeprom->chip = 0x46;
3759 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3760 ftdi_error_return(-4, "Reading failed failed");
3761 if (eeprom_value == MAGIC)
3762 ftdi->eeprom->chip = 0x56;
3765 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3766 ftdi_error_return(-4, "Reading failed failed");
3767 if (eeprom_value == MAGIC)
3768 ftdi->eeprom->chip = 0x66;
3771 ftdi->eeprom->chip = -1;
3775 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3776 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3777 ftdi_error_return(-1, "unable to erase eeprom");
3782 Get string representation for last error code
3784 \param ftdi pointer to ftdi_context
3786 \retval Pointer to error string
3788 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3793 return ftdi->error_str;
3796 /* @} end of doxygen libftdi group */