1 /***************************************************************************
5 copyright : (C) 2003-2011 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
18 \mainpage libftdi API documentation
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/en/developer/libftdi/
23 The library is easy to use. Have a look at this short example:
26 More examples can be found in the "examples" directory.
28 /** \addtogroup libftdi */
39 #define ftdi_error_return(code, str) do { \
40 ftdi->error_str = str; \
44 #define ftdi_error_return_free_device_list(code, str, devs) do { \
45 libusb_free_device_list(devs,1); \
46 ftdi->error_str = str; \
52 Internal function to close usb device pointer.
53 Sets ftdi->usb_dev to NULL.
56 \param ftdi pointer to ftdi_context
60 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
62 if (ftdi && ftdi->usb_dev)
64 libusb_close (ftdi->usb_dev);
70 Initializes a ftdi_context.
72 \param ftdi pointer to ftdi_context
75 \retval -1: couldn't allocate read buffer
76 \retval -2: couldn't allocate struct buffer
77 \retval -3: libusb_init() failed
79 \remark This should be called before all functions
81 int ftdi_init(struct ftdi_context *ftdi)
83 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
86 ftdi->usb_read_timeout = 5000;
87 ftdi->usb_write_timeout = 5000;
89 ftdi->type = TYPE_BM; /* chip type */
91 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
93 ftdi->readbuffer = NULL;
94 ftdi->readbuffer_offset = 0;
95 ftdi->readbuffer_remaining = 0;
96 ftdi->writebuffer_chunksize = 4096;
97 ftdi->max_packet_size = 0;
98 ftdi->error_str = NULL;
99 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
101 if (libusb_init(&ftdi->usb_ctx) < 0)
102 ftdi_error_return(-3, "libusb_init() failed");
104 ftdi_set_interface(ftdi, INTERFACE_ANY);
105 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
108 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
109 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
110 ftdi->eeprom = eeprom;
112 /* All fine. Now allocate the readbuffer */
113 return ftdi_read_data_set_chunksize(ftdi, 4096);
117 Allocate and initialize a new ftdi_context
119 \return a pointer to a new ftdi_context, or NULL on failure
121 struct ftdi_context *ftdi_new(void)
123 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
130 if (ftdi_init(ftdi) != 0)
140 Open selected channels on a chip, otherwise use first channel.
142 \param ftdi pointer to ftdi_context
143 \param interface Interface to use for FT2232C/2232H/4232H chips.
146 \retval -1: unknown interface
147 \retval -2: USB device unavailable
149 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
152 ftdi_error_return(-2, "USB device unavailable");
159 ftdi->index = INTERFACE_A;
165 ftdi->index = INTERFACE_B;
171 ftdi->index = INTERFACE_C;
177 ftdi->index = INTERFACE_D;
182 ftdi_error_return(-1, "Unknown interface");
188 Deinitializes a ftdi_context.
190 \param ftdi pointer to ftdi_context
192 void ftdi_deinit(struct ftdi_context *ftdi)
197 ftdi_usb_close_internal (ftdi);
199 if (ftdi->readbuffer != NULL)
201 free(ftdi->readbuffer);
202 ftdi->readbuffer = NULL;
205 if (ftdi->eeprom != NULL)
207 if (ftdi->eeprom->manufacturer != 0)
209 free(ftdi->eeprom->manufacturer);
210 ftdi->eeprom->manufacturer = 0;
212 if (ftdi->eeprom->product != 0)
214 free(ftdi->eeprom->product);
215 ftdi->eeprom->product = 0;
217 if (ftdi->eeprom->serial != 0)
219 free(ftdi->eeprom->serial);
220 ftdi->eeprom->serial = 0;
228 libusb_exit(ftdi->usb_ctx);
229 ftdi->usb_ctx = NULL;
234 Deinitialize and free an ftdi_context.
236 \param ftdi pointer to ftdi_context
238 void ftdi_free(struct ftdi_context *ftdi)
245 Use an already open libusb device.
247 \param ftdi pointer to ftdi_context
248 \param usb libusb libusb_device_handle to use
250 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
260 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
261 needs to be deallocated by ftdi_list_free() after use.
263 \param ftdi pointer to ftdi_context
264 \param devlist Pointer where to store list of found devices
265 \param vendor Vendor ID to search for
266 \param product Product ID to search for
268 \retval >0: number of devices found
269 \retval -3: out of memory
270 \retval -5: libusb_get_device_list() failed
271 \retval -6: libusb_get_device_descriptor() failed
273 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
275 struct ftdi_device_list **curdev;
277 libusb_device **devs;
281 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
282 ftdi_error_return(-5, "libusb_get_device_list() failed");
287 while ((dev = devs[i++]) != NULL)
289 struct libusb_device_descriptor desc;
291 if (libusb_get_device_descriptor(dev, &desc) < 0)
292 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
294 if (desc.idVendor == vendor && desc.idProduct == product)
296 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
298 ftdi_error_return_free_device_list(-3, "out of memory", devs);
300 (*curdev)->next = NULL;
301 (*curdev)->dev = dev;
303 curdev = &(*curdev)->next;
307 libusb_free_device_list(devs,1);
312 Frees a usb device list.
314 \param devlist USB device list created by ftdi_usb_find_all()
316 void ftdi_list_free(struct ftdi_device_list **devlist)
318 struct ftdi_device_list *curdev, *next;
320 for (curdev = *devlist; curdev != NULL;)
331 Frees a usb device list.
333 \param devlist USB device list created by ftdi_usb_find_all()
335 void ftdi_list_free2(struct ftdi_device_list *devlist)
337 ftdi_list_free(&devlist);
341 Return device ID strings from the usb device.
343 The parameters manufacturer, description and serial may be NULL
344 or pointer to buffers to store the fetched strings.
346 \note Use this function only in combination with ftdi_usb_find_all()
347 as it closes the internal "usb_dev" after use.
349 \param ftdi pointer to ftdi_context
350 \param dev libusb usb_dev to use
351 \param manufacturer Store manufacturer string here if not NULL
352 \param mnf_len Buffer size of manufacturer string
353 \param description Store product description string here if not NULL
354 \param desc_len Buffer size of product description string
355 \param serial Store serial string here if not NULL
356 \param serial_len Buffer size of serial string
359 \retval -1: wrong arguments
360 \retval -4: unable to open device
361 \retval -7: get product manufacturer failed
362 \retval -8: get product description failed
363 \retval -9: get serial number failed
364 \retval -11: libusb_get_device_descriptor() failed
366 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
367 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
369 struct libusb_device_descriptor desc;
371 if ((ftdi==NULL) || (dev==NULL))
374 if (libusb_open(dev, &ftdi->usb_dev) < 0)
375 ftdi_error_return(-4, "libusb_open() failed");
377 if (libusb_get_device_descriptor(dev, &desc) < 0)
378 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
380 if (manufacturer != NULL)
382 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
384 ftdi_usb_close_internal (ftdi);
385 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
389 if (description != NULL)
391 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
393 ftdi_usb_close_internal (ftdi);
394 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
400 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
402 ftdi_usb_close_internal (ftdi);
403 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
407 ftdi_usb_close_internal (ftdi);
413 * Internal function to determine the maximum packet size.
414 * \param ftdi pointer to ftdi_context
415 * \param dev libusb usb_dev to use
416 * \retval Maximum packet size for this device
418 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
420 struct libusb_device_descriptor desc;
421 struct libusb_config_descriptor *config0;
422 unsigned int packet_size;
425 if (ftdi == NULL || dev == NULL)
428 // Determine maximum packet size. Init with default value.
429 // New hi-speed devices from FTDI use a packet size of 512 bytes
430 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
431 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
436 if (libusb_get_device_descriptor(dev, &desc) < 0)
439 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
442 if (desc.bNumConfigurations > 0)
444 if (ftdi->interface < config0->bNumInterfaces)
446 struct libusb_interface interface = config0->interface[ftdi->interface];
447 if (interface.num_altsetting > 0)
449 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
450 if (descriptor.bNumEndpoints > 0)
452 packet_size = descriptor.endpoint[0].wMaxPacketSize;
458 libusb_free_config_descriptor (config0);
463 Opens a ftdi device given by an usb_device.
465 \param ftdi pointer to ftdi_context
466 \param dev libusb usb_dev to use
469 \retval -3: unable to config device
470 \retval -4: unable to open device
471 \retval -5: unable to claim device
472 \retval -6: reset failed
473 \retval -7: set baudrate failed
474 \retval -8: ftdi context invalid
475 \retval -9: libusb_get_device_descriptor() failed
476 \retval -10: libusb_get_config_descriptor() failed
477 \retval -11: libusb_detach_kernel_driver() failed
478 \retval -12: libusb_get_configuration() failed
480 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
482 struct libusb_device_descriptor desc;
483 struct libusb_config_descriptor *config0;
484 int cfg, cfg0, detach_errno = 0;
487 ftdi_error_return(-8, "ftdi context invalid");
489 if (libusb_open(dev, &ftdi->usb_dev) < 0)
490 ftdi_error_return(-4, "libusb_open() failed");
492 if (libusb_get_device_descriptor(dev, &desc) < 0)
493 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
495 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
496 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
497 cfg0 = config0->bConfigurationValue;
498 libusb_free_config_descriptor (config0);
500 // Try to detach ftdi_sio kernel module.
502 // The return code is kept in a separate variable and only parsed
503 // if usb_set_configuration() or usb_claim_interface() fails as the
504 // detach operation might be denied and everything still works fine.
505 // Likely scenario is a static ftdi_sio kernel module.
506 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
508 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
509 detach_errno = errno;
512 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
513 ftdi_error_return(-12, "libusb_get_configuration () failed");
514 // set configuration (needed especially for windows)
515 // tolerate EBUSY: one device with one configuration, but two interfaces
516 // and libftdi sessions to both interfaces (e.g. FT2232)
517 if (desc.bNumConfigurations > 0 && cfg != cfg0)
519 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
521 ftdi_usb_close_internal (ftdi);
522 if (detach_errno == EPERM)
524 ftdi_error_return(-8, "inappropriate permissions on device!");
528 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
533 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
535 ftdi_usb_close_internal (ftdi);
536 if (detach_errno == EPERM)
538 ftdi_error_return(-8, "inappropriate permissions on device!");
542 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
546 if (ftdi_usb_reset (ftdi) != 0)
548 ftdi_usb_close_internal (ftdi);
549 ftdi_error_return(-6, "ftdi_usb_reset failed");
552 // Try to guess chip type
553 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
554 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
555 && desc.iSerialNumber == 0))
556 ftdi->type = TYPE_BM;
557 else if (desc.bcdDevice == 0x200)
558 ftdi->type = TYPE_AM;
559 else if (desc.bcdDevice == 0x500)
560 ftdi->type = TYPE_2232C;
561 else if (desc.bcdDevice == 0x600)
563 else if (desc.bcdDevice == 0x700)
564 ftdi->type = TYPE_2232H;
565 else if (desc.bcdDevice == 0x800)
566 ftdi->type = TYPE_4232H;
567 else if (desc.bcdDevice == 0x900)
568 ftdi->type = TYPE_232H;
570 // Determine maximum packet size
571 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
573 if (ftdi_set_baudrate (ftdi, 9600) != 0)
575 ftdi_usb_close_internal (ftdi);
576 ftdi_error_return(-7, "set baudrate failed");
579 ftdi_error_return(0, "all fine");
583 Opens the first device with a given vendor and product ids.
585 \param ftdi pointer to ftdi_context
586 \param vendor Vendor ID
587 \param product Product ID
589 \retval same as ftdi_usb_open_desc()
591 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
593 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
597 Opens the first device with a given, vendor id, product id,
598 description and serial.
600 \param ftdi pointer to ftdi_context
601 \param vendor Vendor ID
602 \param product Product ID
603 \param description Description to search for. Use NULL if not needed.
604 \param serial Serial to search for. Use NULL if not needed.
607 \retval -3: usb device not found
608 \retval -4: unable to open device
609 \retval -5: unable to claim device
610 \retval -6: reset failed
611 \retval -7: set baudrate failed
612 \retval -8: get product description failed
613 \retval -9: get serial number failed
614 \retval -11: libusb_init() failed
615 \retval -12: libusb_get_device_list() failed
616 \retval -13: libusb_get_device_descriptor() failed
618 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
619 const char* description, const char* serial)
621 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
625 Opens the index-th device with a given, vendor id, product id,
626 description and serial.
628 \param ftdi pointer to ftdi_context
629 \param vendor Vendor ID
630 \param product Product ID
631 \param description Description to search for. Use NULL if not needed.
632 \param serial Serial to search for. Use NULL if not needed.
633 \param index Number of matching device to open if there are more than one, starts with 0.
636 \retval -1: usb_find_busses() failed
637 \retval -2: usb_find_devices() failed
638 \retval -3: usb device not found
639 \retval -4: unable to open device
640 \retval -5: unable to claim device
641 \retval -6: reset failed
642 \retval -7: set baudrate failed
643 \retval -8: get product description failed
644 \retval -9: get serial number failed
645 \retval -10: unable to close device
646 \retval -11: ftdi context invalid
648 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
649 const char* description, const char* serial, unsigned int index)
652 libusb_device **devs;
657 ftdi_error_return(-11, "ftdi context invalid");
659 if (libusb_init(&ftdi->usb_ctx) < 0)
660 ftdi_error_return(-11, "libusb_init() failed");
662 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
663 ftdi_error_return(-12, "libusb_get_device_list() failed");
665 while ((dev = devs[i++]) != NULL)
667 struct libusb_device_descriptor desc;
670 if (libusb_get_device_descriptor(dev, &desc) < 0)
671 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
673 if (desc.idVendor == vendor && desc.idProduct == product)
675 if (libusb_open(dev, &ftdi->usb_dev) < 0)
676 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
678 if (description != NULL)
680 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
682 ftdi_usb_close_internal (ftdi);
683 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
685 if (strncmp(string, description, sizeof(string)) != 0)
687 ftdi_usb_close_internal (ftdi);
693 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
695 ftdi_usb_close_internal (ftdi);
696 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
698 if (strncmp(string, serial, sizeof(string)) != 0)
700 ftdi_usb_close_internal (ftdi);
705 ftdi_usb_close_internal (ftdi);
713 res = ftdi_usb_open_dev(ftdi, dev);
714 libusb_free_device_list(devs,1);
720 ftdi_error_return_free_device_list(-3, "device not found", devs);
724 Opens the ftdi-device described by a description-string.
725 Intended to be used for parsing a device-description given as commandline argument.
727 \param ftdi pointer to ftdi_context
728 \param description NULL-terminated description-string, using this format:
729 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
730 \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")
731 \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
732 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
734 \note The description format may be extended in later versions.
737 \retval -1: libusb_init() failed
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_init (&ftdi->usb_ctx) < 0)
766 ftdi_error_return(-1, "libusb_init() failed");
768 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
769 ftdi_error_return(-2, "libusb_get_device_list() failed");
771 /* XXX: This doesn't handle symlinks/odd paths/etc... */
772 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
773 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
775 while ((dev = devs[i++]) != NULL)
778 if (bus_number == libusb_get_bus_number (dev)
779 && device_address == libusb_get_device_address (dev))
781 ret = ftdi_usb_open_dev(ftdi, dev);
782 libusb_free_device_list(devs,1);
788 ftdi_error_return_free_device_list(-3, "device not found", devs);
790 else if (description[0] == 'i' || description[0] == 's')
793 unsigned int product;
794 unsigned int index=0;
795 const char *serial=NULL;
796 const char *startp, *endp;
799 startp=description+2;
800 vendor=strtoul((char*)startp,(char**)&endp,0);
801 if (*endp != ':' || endp == startp || errno != 0)
802 ftdi_error_return(-11, "illegal description format");
805 product=strtoul((char*)startp,(char**)&endp,0);
806 if (endp == startp || errno != 0)
807 ftdi_error_return(-11, "illegal description format");
809 if (description[0] == 'i' && *endp != 0)
811 /* optional index field in i-mode */
813 ftdi_error_return(-11, "illegal description format");
816 index=strtoul((char*)startp,(char**)&endp,0);
817 if (*endp != 0 || endp == startp || errno != 0)
818 ftdi_error_return(-11, "illegal description format");
820 if (description[0] == 's')
823 ftdi_error_return(-11, "illegal description format");
825 /* rest of the description is the serial */
829 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
833 ftdi_error_return(-11, "illegal description format");
838 Resets the ftdi device.
840 \param ftdi pointer to ftdi_context
843 \retval -1: FTDI reset failed
844 \retval -2: USB device unavailable
846 int ftdi_usb_reset(struct ftdi_context *ftdi)
848 if (ftdi == NULL || ftdi->usb_dev == NULL)
849 ftdi_error_return(-2, "USB device unavailable");
851 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
852 SIO_RESET_REQUEST, SIO_RESET_SIO,
853 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
854 ftdi_error_return(-1,"FTDI reset failed");
856 // Invalidate data in the readbuffer
857 ftdi->readbuffer_offset = 0;
858 ftdi->readbuffer_remaining = 0;
864 Clears the read buffer on the chip and the internal read buffer.
866 \param ftdi pointer to ftdi_context
869 \retval -1: read buffer purge failed
870 \retval -2: USB device unavailable
872 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
874 if (ftdi == NULL || ftdi->usb_dev == NULL)
875 ftdi_error_return(-2, "USB device unavailable");
877 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
878 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
879 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
880 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
882 // Invalidate data in the readbuffer
883 ftdi->readbuffer_offset = 0;
884 ftdi->readbuffer_remaining = 0;
890 Clears the write buffer on the chip.
892 \param ftdi pointer to ftdi_context
895 \retval -1: write buffer purge failed
896 \retval -2: USB device unavailable
898 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
900 if (ftdi == NULL || ftdi->usb_dev == NULL)
901 ftdi_error_return(-2, "USB device unavailable");
903 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
904 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
905 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
906 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
912 Clears the buffers on the chip and the internal read buffer.
914 \param ftdi pointer to ftdi_context
917 \retval -1: read buffer purge failed
918 \retval -2: write buffer purge failed
919 \retval -3: USB device unavailable
921 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
925 if (ftdi == NULL || ftdi->usb_dev == NULL)
926 ftdi_error_return(-3, "USB device unavailable");
928 result = ftdi_usb_purge_rx_buffer(ftdi);
932 result = ftdi_usb_purge_tx_buffer(ftdi);
942 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
944 \param ftdi pointer to ftdi_context
947 \retval -1: usb_release failed
948 \retval -3: ftdi context invalid
950 int ftdi_usb_close(struct ftdi_context *ftdi)
955 ftdi_error_return(-3, "ftdi context invalid");
957 if (ftdi->usb_dev != NULL)
958 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
961 ftdi_usb_close_internal (ftdi);
967 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
968 Function is only used internally
971 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
972 unsigned short *value, unsigned short *index)
974 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
975 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
976 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
977 int divisor, best_divisor, best_baud, best_baud_diff;
978 unsigned long encoded_divisor;
987 divisor = 24000000 / baudrate;
989 if (ftdi->type == TYPE_AM)
991 // Round down to supported fraction (AM only)
992 divisor -= am_adjust_dn[divisor & 7];
995 // Try this divisor and the one above it (because division rounds down)
999 for (i = 0; i < 2; i++)
1001 int try_divisor = divisor + i;
1005 // Round up to supported divisor value
1006 if (try_divisor <= 8)
1008 // Round up to minimum supported divisor
1011 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1013 // BM doesn't support divisors 9 through 11 inclusive
1016 else if (divisor < 16)
1018 // AM doesn't support divisors 9 through 15 inclusive
1023 if (ftdi->type == TYPE_AM)
1025 // Round up to supported fraction (AM only)
1026 try_divisor += am_adjust_up[try_divisor & 7];
1027 if (try_divisor > 0x1FFF8)
1029 // Round down to maximum supported divisor value (for AM)
1030 try_divisor = 0x1FFF8;
1035 if (try_divisor > 0x1FFFF)
1037 // Round down to maximum supported divisor value (for BM)
1038 try_divisor = 0x1FFFF;
1042 // Get estimated baud rate (to nearest integer)
1043 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1044 // Get absolute difference from requested baud rate
1045 if (baud_estimate < baudrate)
1047 baud_diff = baudrate - baud_estimate;
1051 baud_diff = baud_estimate - baudrate;
1053 if (i == 0 || baud_diff < best_baud_diff)
1055 // Closest to requested baud rate so far
1056 best_divisor = try_divisor;
1057 best_baud = baud_estimate;
1058 best_baud_diff = baud_diff;
1061 // Spot on! No point trying
1066 // Encode the best divisor value
1067 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1068 // Deal with special cases for encoded value
1069 if (encoded_divisor == 1)
1071 encoded_divisor = 0; // 3000000 baud
1073 else if (encoded_divisor == 0x4001)
1075 encoded_divisor = 1; // 2000000 baud (BM only)
1077 // Split into "value" and "index" values
1078 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1079 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1081 *index = (unsigned short)(encoded_divisor >> 8);
1083 *index |= ftdi->index;
1086 *index = (unsigned short)(encoded_divisor >> 16);
1088 // Return the nearest baud rate
1093 Sets the chip baud rate
1095 \param ftdi pointer to ftdi_context
1096 \param baudrate baud rate to set
1099 \retval -1: invalid baudrate
1100 \retval -2: setting baudrate failed
1101 \retval -3: USB device unavailable
1103 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1105 unsigned short value, index;
1106 int actual_baudrate;
1108 if (ftdi == NULL || ftdi->usb_dev == NULL)
1109 ftdi_error_return(-3, "USB device unavailable");
1111 if (ftdi->bitbang_enabled)
1113 baudrate = baudrate*4;
1116 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1117 if (actual_baudrate <= 0)
1118 ftdi_error_return (-1, "Silly baudrate <= 0.");
1120 // Check within tolerance (about 5%)
1121 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1122 || ((actual_baudrate < baudrate)
1123 ? (actual_baudrate * 21 < baudrate * 20)
1124 : (baudrate * 21 < actual_baudrate * 20)))
1125 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1127 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1128 SIO_SET_BAUDRATE_REQUEST, value,
1129 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1130 ftdi_error_return (-2, "Setting new baudrate failed");
1132 ftdi->baudrate = baudrate;
1137 Set (RS232) line characteristics.
1138 The break type can only be set via ftdi_set_line_property2()
1139 and defaults to "off".
1141 \param ftdi pointer to ftdi_context
1142 \param bits Number of bits
1143 \param sbit Number of stop bits
1144 \param parity Parity mode
1147 \retval -1: Setting line property failed
1149 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1150 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1152 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1156 Set (RS232) line characteristics
1158 \param ftdi pointer to ftdi_context
1159 \param bits Number of bits
1160 \param sbit Number of stop bits
1161 \param parity Parity mode
1162 \param break_type Break type
1165 \retval -1: Setting line property failed
1166 \retval -2: USB device unavailable
1168 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1169 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1170 enum ftdi_break_type break_type)
1172 unsigned short value = bits;
1174 if (ftdi == NULL || ftdi->usb_dev == NULL)
1175 ftdi_error_return(-2, "USB device unavailable");
1180 value |= (0x00 << 8);
1183 value |= (0x01 << 8);
1186 value |= (0x02 << 8);
1189 value |= (0x03 << 8);
1192 value |= (0x04 << 8);
1199 value |= (0x00 << 11);
1202 value |= (0x01 << 11);
1205 value |= (0x02 << 11);
1212 value |= (0x00 << 14);
1215 value |= (0x01 << 14);
1219 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1220 SIO_SET_DATA_REQUEST, value,
1221 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1222 ftdi_error_return (-1, "Setting new line property failed");
1228 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1230 \param ftdi pointer to ftdi_context
1231 \param buf Buffer with the data
1232 \param size Size of the buffer
1234 \retval -666: USB device unavailable
1235 \retval <0: error code from usb_bulk_write()
1236 \retval >0: number of bytes written
1238 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1243 if (ftdi == NULL || ftdi->usb_dev == NULL)
1244 ftdi_error_return(-666, "USB device unavailable");
1246 while (offset < size)
1248 int write_size = ftdi->writebuffer_chunksize;
1250 if (offset+write_size > size)
1251 write_size = size-offset;
1253 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1254 ftdi_error_return(-1, "usb bulk write failed");
1256 offset += actual_length;
1262 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1264 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1265 struct ftdi_context *ftdi = tc->ftdi;
1266 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1268 packet_size = ftdi->max_packet_size;
1270 actual_length = transfer->actual_length;
1272 if (actual_length > 2)
1274 // skip FTDI status bytes.
1275 // Maybe stored in the future to enable modem use
1276 num_of_chunks = actual_length / packet_size;
1277 chunk_remains = actual_length % packet_size;
1278 //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);
1280 ftdi->readbuffer_offset += 2;
1283 if (actual_length > packet_size - 2)
1285 for (i = 1; i < num_of_chunks; i++)
1286 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1287 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1289 if (chunk_remains > 2)
1291 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1292 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1294 actual_length -= 2*num_of_chunks;
1297 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1300 if (actual_length > 0)
1302 // data still fits in buf?
1303 if (tc->offset + actual_length <= tc->size)
1305 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1306 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1307 tc->offset += actual_length;
1309 ftdi->readbuffer_offset = 0;
1310 ftdi->readbuffer_remaining = 0;
1312 /* Did we read exactly the right amount of bytes? */
1313 if (tc->offset == tc->size)
1315 //printf("read_data exact rem %d offset %d\n",
1316 //ftdi->readbuffer_remaining, offset);
1323 // only copy part of the data or size <= readbuffer_chunksize
1324 int part_size = tc->size - tc->offset;
1325 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1326 tc->offset += part_size;
1328 ftdi->readbuffer_offset += part_size;
1329 ftdi->readbuffer_remaining = actual_length - part_size;
1331 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1332 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1338 ret = libusb_submit_transfer (transfer);
1344 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1346 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1347 struct ftdi_context *ftdi = tc->ftdi;
1349 tc->offset += transfer->actual_length;
1351 if (tc->offset == tc->size)
1357 int write_size = ftdi->writebuffer_chunksize;
1360 if (tc->offset + write_size > tc->size)
1361 write_size = tc->size - tc->offset;
1363 transfer->length = write_size;
1364 transfer->buffer = tc->buf + tc->offset;
1365 ret = libusb_submit_transfer (transfer);
1373 Writes data to the chip. Does not wait for completion of the transfer
1374 nor does it make sure that the transfer was successful.
1376 Use libusb 1.0 asynchronous API.
1378 \param ftdi pointer to ftdi_context
1379 \param buf Buffer with the data
1380 \param size Size of the buffer
1382 \retval NULL: Some error happens when submit transfer
1383 \retval !NULL: Pointer to a ftdi_transfer_control
1386 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1388 struct ftdi_transfer_control *tc;
1389 struct libusb_transfer *transfer;
1390 int write_size, ret;
1392 if (ftdi == NULL || ftdi->usb_dev == NULL)
1395 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1399 transfer = libusb_alloc_transfer(0);
1412 if (size < ftdi->writebuffer_chunksize)
1415 write_size = ftdi->writebuffer_chunksize;
1417 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1418 write_size, ftdi_write_data_cb, tc,
1419 ftdi->usb_write_timeout);
1420 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1422 ret = libusb_submit_transfer(transfer);
1425 libusb_free_transfer(transfer);
1429 tc->transfer = transfer;
1435 Reads data from the chip. Does not wait for completion of the transfer
1436 nor does it make sure that the transfer was successful.
1438 Use libusb 1.0 asynchronous API.
1440 \param ftdi pointer to ftdi_context
1441 \param buf Buffer with the data
1442 \param size Size of the buffer
1444 \retval NULL: Some error happens when submit transfer
1445 \retval !NULL: Pointer to a ftdi_transfer_control
1448 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1450 struct ftdi_transfer_control *tc;
1451 struct libusb_transfer *transfer;
1454 if (ftdi == NULL || ftdi->usb_dev == NULL)
1457 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1465 if (size <= ftdi->readbuffer_remaining)
1467 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1470 ftdi->readbuffer_remaining -= size;
1471 ftdi->readbuffer_offset += size;
1473 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1477 tc->transfer = NULL;
1482 if (ftdi->readbuffer_remaining != 0)
1484 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1486 tc->offset = ftdi->readbuffer_remaining;
1491 transfer = libusb_alloc_transfer(0);
1498 ftdi->readbuffer_remaining = 0;
1499 ftdi->readbuffer_offset = 0;
1501 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);
1502 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1504 ret = libusb_submit_transfer(transfer);
1507 libusb_free_transfer(transfer);
1511 tc->transfer = transfer;
1517 Wait for completion of the transfer.
1519 Use libusb 1.0 asynchronous API.
1521 \param tc pointer to ftdi_transfer_control
1523 \retval < 0: Some error happens
1524 \retval >= 0: Data size transferred
1527 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1531 while (!tc->completed)
1533 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1536 if (ret == LIBUSB_ERROR_INTERRUPTED)
1538 libusb_cancel_transfer(tc->transfer);
1539 while (!tc->completed)
1540 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1542 libusb_free_transfer(tc->transfer);
1550 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1551 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1555 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1557 libusb_free_transfer(tc->transfer);
1564 Configure write buffer chunk size.
1567 \param ftdi pointer to ftdi_context
1568 \param chunksize Chunk size
1571 \retval -1: ftdi context invalid
1573 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1576 ftdi_error_return(-1, "ftdi context invalid");
1578 ftdi->writebuffer_chunksize = chunksize;
1583 Get write buffer chunk size.
1585 \param ftdi pointer to ftdi_context
1586 \param chunksize Pointer to store chunk size in
1589 \retval -1: ftdi context invalid
1591 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1594 ftdi_error_return(-1, "ftdi context invalid");
1596 *chunksize = ftdi->writebuffer_chunksize;
1601 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1603 Automatically strips the two modem status bytes transfered during every read.
1605 \param ftdi pointer to ftdi_context
1606 \param buf Buffer to store data in
1607 \param size Size of the buffer
1609 \retval -666: USB device unavailable
1610 \retval <0: error code from libusb_bulk_transfer()
1611 \retval 0: no data was available
1612 \retval >0: number of bytes read
1615 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1617 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1618 int packet_size = ftdi->max_packet_size;
1619 int actual_length = 1;
1621 if (ftdi == NULL || ftdi->usb_dev == NULL)
1622 ftdi_error_return(-666, "USB device unavailable");
1624 // Packet size sanity check (avoid division by zero)
1625 if (packet_size == 0)
1626 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1628 // everything we want is still in the readbuffer?
1629 if (size <= ftdi->readbuffer_remaining)
1631 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1634 ftdi->readbuffer_remaining -= size;
1635 ftdi->readbuffer_offset += size;
1637 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1641 // something still in the readbuffer, but not enough to satisfy 'size'?
1642 if (ftdi->readbuffer_remaining != 0)
1644 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1647 offset += ftdi->readbuffer_remaining;
1649 // do the actual USB read
1650 while (offset < size && actual_length > 0)
1652 ftdi->readbuffer_remaining = 0;
1653 ftdi->readbuffer_offset = 0;
1654 /* returns how much received */
1655 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1657 ftdi_error_return(ret, "usb bulk read failed");
1659 if (actual_length > 2)
1661 // skip FTDI status bytes.
1662 // Maybe stored in the future to enable modem use
1663 num_of_chunks = actual_length / packet_size;
1664 chunk_remains = actual_length % packet_size;
1665 //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);
1667 ftdi->readbuffer_offset += 2;
1670 if (actual_length > packet_size - 2)
1672 for (i = 1; i < num_of_chunks; i++)
1673 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1674 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1676 if (chunk_remains > 2)
1678 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1679 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1681 actual_length -= 2*num_of_chunks;
1684 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1687 else if (actual_length <= 2)
1689 // no more data to read?
1692 if (actual_length > 0)
1694 // data still fits in buf?
1695 if (offset+actual_length <= size)
1697 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1698 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1699 offset += actual_length;
1701 /* Did we read exactly the right amount of bytes? */
1703 //printf("read_data exact rem %d offset %d\n",
1704 //ftdi->readbuffer_remaining, offset);
1709 // only copy part of the data or size <= readbuffer_chunksize
1710 int part_size = size-offset;
1711 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1713 ftdi->readbuffer_offset += part_size;
1714 ftdi->readbuffer_remaining = actual_length-part_size;
1715 offset += part_size;
1717 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1718 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1729 Configure read buffer chunk size.
1732 Automatically reallocates the buffer.
1734 \param ftdi pointer to ftdi_context
1735 \param chunksize Chunk size
1738 \retval -1: ftdi context invalid
1740 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1742 unsigned char *new_buf;
1745 ftdi_error_return(-1, "ftdi context invalid");
1747 // Invalidate all remaining data
1748 ftdi->readbuffer_offset = 0;
1749 ftdi->readbuffer_remaining = 0;
1751 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1752 which is defined in libusb-1.0. Otherwise, each USB read request will
1753 be divided into multiple URBs. This will cause issues on Linux kernel
1754 older than 2.6.32. */
1755 if (chunksize > 16384)
1759 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1760 ftdi_error_return(-1, "out of memory for readbuffer");
1762 ftdi->readbuffer = new_buf;
1763 ftdi->readbuffer_chunksize = chunksize;
1769 Get read buffer chunk size.
1771 \param ftdi pointer to ftdi_context
1772 \param chunksize Pointer to store chunk size in
1775 \retval -1: FTDI context invalid
1777 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1780 ftdi_error_return(-1, "FTDI context invalid");
1782 *chunksize = ftdi->readbuffer_chunksize;
1788 Enable bitbang mode.
1790 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1792 \param ftdi pointer to ftdi_context
1793 \param bitmask Bitmask to configure lines.
1794 HIGH/ON value configures a line as output.
1797 \retval -1: can't enable bitbang mode
1798 \retval -2: USB device unavailable
1800 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1802 unsigned short usb_val;
1804 if (ftdi == NULL || ftdi->usb_dev == NULL)
1805 ftdi_error_return(-2, "USB device unavailable");
1807 usb_val = bitmask; // low byte: bitmask
1808 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1809 usb_val |= (ftdi->bitbang_mode << 8);
1811 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1812 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1813 NULL, 0, ftdi->usb_write_timeout) < 0)
1814 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1816 ftdi->bitbang_enabled = 1;
1821 Disable bitbang mode.
1823 \param ftdi pointer to ftdi_context
1826 \retval -1: can't disable bitbang mode
1827 \retval -2: USB device unavailable
1829 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1831 if (ftdi == NULL || ftdi->usb_dev == NULL)
1832 ftdi_error_return(-2, "USB device unavailable");
1834 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)
1835 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1837 ftdi->bitbang_enabled = 0;
1842 Enable/disable bitbang modes.
1844 \param ftdi pointer to ftdi_context
1845 \param bitmask Bitmask to configure lines.
1846 HIGH/ON value configures a line as output.
1847 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1850 \retval -1: can't enable bitbang mode
1851 \retval -2: USB device unavailable
1853 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1855 unsigned short usb_val;
1857 if (ftdi == NULL || ftdi->usb_dev == NULL)
1858 ftdi_error_return(-2, "USB device unavailable");
1860 usb_val = bitmask; // low byte: bitmask
1861 usb_val |= (mode << 8);
1862 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)
1863 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1865 ftdi->bitbang_mode = mode;
1866 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1871 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1873 \param ftdi pointer to ftdi_context
1874 \param pins Pointer to store pins into
1877 \retval -1: read pins failed
1878 \retval -2: USB device unavailable
1880 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1882 if (ftdi == NULL || ftdi->usb_dev == NULL)
1883 ftdi_error_return(-2, "USB device unavailable");
1885 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)
1886 ftdi_error_return(-1, "read pins failed");
1894 The FTDI chip keeps data in the internal buffer for a specific
1895 amount of time if the buffer is not full yet to decrease
1896 load on the usb bus.
1898 \param ftdi pointer to ftdi_context
1899 \param latency Value between 1 and 255
1902 \retval -1: latency out of range
1903 \retval -2: unable to set latency timer
1904 \retval -3: USB device unavailable
1906 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1908 unsigned short usb_val;
1911 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1913 if (ftdi == NULL || ftdi->usb_dev == NULL)
1914 ftdi_error_return(-3, "USB device unavailable");
1917 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)
1918 ftdi_error_return(-2, "unable to set latency timer");
1926 \param ftdi pointer to ftdi_context
1927 \param latency Pointer to store latency value in
1930 \retval -1: unable to get latency timer
1931 \retval -2: USB device unavailable
1933 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1935 unsigned short usb_val;
1937 if (ftdi == NULL || ftdi->usb_dev == NULL)
1938 ftdi_error_return(-2, "USB device unavailable");
1940 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)
1941 ftdi_error_return(-1, "reading latency timer failed");
1943 *latency = (unsigned char)usb_val;
1948 Poll modem status information
1950 This function allows the retrieve the two status bytes of the device.
1951 The device sends these bytes also as a header for each read access
1952 where they are discarded by ftdi_read_data(). The chip generates
1953 the two stripped status bytes in the absence of data every 40 ms.
1955 Layout of the first byte:
1956 - B0..B3 - must be 0
1957 - B4 Clear to send (CTS)
1960 - B5 Data set ready (DTS)
1963 - B6 Ring indicator (RI)
1966 - B7 Receive line signal detect (RLSD)
1970 Layout of the second byte:
1971 - B0 Data ready (DR)
1972 - B1 Overrun error (OE)
1973 - B2 Parity error (PE)
1974 - B3 Framing error (FE)
1975 - B4 Break interrupt (BI)
1976 - B5 Transmitter holding register (THRE)
1977 - B6 Transmitter empty (TEMT)
1978 - B7 Error in RCVR FIFO
1980 \param ftdi pointer to ftdi_context
1981 \param status Pointer to store status information in. Must be two bytes.
1984 \retval -1: unable to retrieve status information
1985 \retval -2: USB device unavailable
1987 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1991 if (ftdi == NULL || ftdi->usb_dev == NULL)
1992 ftdi_error_return(-2, "USB device unavailable");
1994 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)
1995 ftdi_error_return(-1, "getting modem status failed");
1997 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2003 Set flowcontrol for ftdi chip
2005 \param ftdi pointer to ftdi_context
2006 \param flowctrl flow control to use. should be
2007 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2010 \retval -1: set flow control failed
2011 \retval -2: USB device unavailable
2013 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2015 if (ftdi == NULL || ftdi->usb_dev == NULL)
2016 ftdi_error_return(-2, "USB device unavailable");
2018 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2019 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2020 NULL, 0, ftdi->usb_write_timeout) < 0)
2021 ftdi_error_return(-1, "set flow control failed");
2029 \param ftdi pointer to ftdi_context
2030 \param state state to set line to (1 or 0)
2033 \retval -1: set dtr failed
2034 \retval -2: USB device unavailable
2036 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2038 unsigned short usb_val;
2040 if (ftdi == NULL || ftdi->usb_dev == NULL)
2041 ftdi_error_return(-2, "USB device unavailable");
2044 usb_val = SIO_SET_DTR_HIGH;
2046 usb_val = SIO_SET_DTR_LOW;
2048 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2049 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2050 NULL, 0, ftdi->usb_write_timeout) < 0)
2051 ftdi_error_return(-1, "set dtr failed");
2059 \param ftdi pointer to ftdi_context
2060 \param state state to set line to (1 or 0)
2063 \retval -1: set rts failed
2064 \retval -2: USB device unavailable
2066 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2068 unsigned short usb_val;
2070 if (ftdi == NULL || ftdi->usb_dev == NULL)
2071 ftdi_error_return(-2, "USB device unavailable");
2074 usb_val = SIO_SET_RTS_HIGH;
2076 usb_val = SIO_SET_RTS_LOW;
2078 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2079 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2080 NULL, 0, ftdi->usb_write_timeout) < 0)
2081 ftdi_error_return(-1, "set of rts failed");
2087 Set dtr and rts line in one pass
2089 \param ftdi pointer to ftdi_context
2090 \param dtr DTR state to set line to (1 or 0)
2091 \param rts RTS state to set line to (1 or 0)
2094 \retval -1: set dtr/rts failed
2095 \retval -2: USB device unavailable
2097 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2099 unsigned short usb_val;
2101 if (ftdi == NULL || ftdi->usb_dev == NULL)
2102 ftdi_error_return(-2, "USB device unavailable");
2105 usb_val = SIO_SET_DTR_HIGH;
2107 usb_val = SIO_SET_DTR_LOW;
2110 usb_val |= SIO_SET_RTS_HIGH;
2112 usb_val |= SIO_SET_RTS_LOW;
2114 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2115 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2116 NULL, 0, ftdi->usb_write_timeout) < 0)
2117 ftdi_error_return(-1, "set of rts/dtr failed");
2123 Set the special event character
2125 \param ftdi pointer to ftdi_context
2126 \param eventch Event character
2127 \param enable 0 to disable the event character, non-zero otherwise
2130 \retval -1: unable to set event character
2131 \retval -2: USB device unavailable
2133 int ftdi_set_event_char(struct ftdi_context *ftdi,
2134 unsigned char eventch, unsigned char enable)
2136 unsigned short usb_val;
2138 if (ftdi == NULL || ftdi->usb_dev == NULL)
2139 ftdi_error_return(-2, "USB device unavailable");
2145 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)
2146 ftdi_error_return(-1, "setting event character failed");
2154 \param ftdi pointer to ftdi_context
2155 \param errorch Error character
2156 \param enable 0 to disable the error character, non-zero otherwise
2159 \retval -1: unable to set error character
2160 \retval -2: USB device unavailable
2162 int ftdi_set_error_char(struct ftdi_context *ftdi,
2163 unsigned char errorch, unsigned char enable)
2165 unsigned short usb_val;
2167 if (ftdi == NULL || ftdi->usb_dev == NULL)
2168 ftdi_error_return(-2, "USB device unavailable");
2174 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)
2175 ftdi_error_return(-1, "setting error character failed");
2181 Init eeprom with default values.
2182 \param ftdi pointer to ftdi_context
2183 \param manufacturer String to use as Manufacturer
2184 \param product String to use as Product description
2185 \param serial String to use as Serial number description
2188 \retval -1: No struct ftdi_context
2189 \retval -2: No struct ftdi_eeprom
2191 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2192 char * product, char * serial)
2194 struct ftdi_eeprom *eeprom;
2197 ftdi_error_return(-1, "No struct ftdi_context");
2199 if (ftdi->eeprom == NULL)
2200 ftdi_error_return(-2,"No struct ftdi_eeprom");
2202 eeprom = ftdi->eeprom;
2203 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2205 eeprom->vendor_id = 0x0403;
2206 eeprom->use_serial = USE_SERIAL_NUM;
2207 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2208 (ftdi->type == TYPE_R))
2209 eeprom->product_id = 0x6001;
2210 else if (ftdi->type == TYPE_4232H)
2211 eeprom->product_id = 0x6011;
2212 else if (ftdi->type == TYPE_232H)
2213 eeprom->product_id = 0x6014;
2215 eeprom->product_id = 0x6010;
2216 if (ftdi->type == TYPE_AM)
2217 eeprom->usb_version = 0x0101;
2219 eeprom->usb_version = 0x0200;
2220 eeprom->max_power = 100;
2222 if (eeprom->manufacturer)
2223 free (eeprom->manufacturer);
2224 eeprom->manufacturer = NULL;
2227 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2228 if (eeprom->manufacturer)
2229 strcpy(eeprom->manufacturer, manufacturer);
2232 if (eeprom->product)
2233 free (eeprom->product);
2234 eeprom->product = NULL;
2237 eeprom->product = malloc(strlen(product)+1);
2238 if (eeprom->product)
2239 strcpy(eeprom->product, product);
2243 free (eeprom->serial);
2244 eeprom->serial = NULL;
2247 eeprom->serial = malloc(strlen(serial)+1);
2249 strcpy(eeprom->serial, serial);
2253 if (ftdi->type == TYPE_R)
2255 eeprom->max_power = 90;
2256 eeprom->size = 0x80;
2257 eeprom->cbus_function[0] = CBUS_TXLED;
2258 eeprom->cbus_function[1] = CBUS_RXLED;
2259 eeprom->cbus_function[2] = CBUS_TXDEN;
2260 eeprom->cbus_function[3] = CBUS_PWREN;
2261 eeprom->cbus_function[4] = CBUS_SLEEP;
2265 if(ftdi->type == TYPE_232H)
2268 for (i=0; i<10; i++)
2269 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2275 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2276 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2281 int mode_low, mode_high;
2282 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2283 mode_low = CBUSH_TRISTATE;
2285 mode_low = eeprom->cbus_function[2*i];
2286 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2287 mode_high = CBUSH_TRISTATE;
2289 mode_high = eeprom->cbus_function[2*i];
2291 output[0x18+i] = mode_high <<4 | mode_low;
2295 Build binary buffer from ftdi_eeprom structure.
2296 Output is suitable for ftdi_write_eeprom().
2298 \param ftdi pointer to ftdi_context
2300 \retval >=0: size of eeprom user area in bytes
2301 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2302 \retval -2: Invalid eeprom or ftdi pointer
2303 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2304 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2305 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2306 \retval -6: No connected EEPROM or EEPROM Type unknown
2308 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2310 unsigned char i, j, eeprom_size_mask;
2311 unsigned short checksum, value;
2312 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2314 struct ftdi_eeprom *eeprom;
2315 unsigned char * output;
2318 ftdi_error_return(-2,"No context");
2319 if (ftdi->eeprom == NULL)
2320 ftdi_error_return(-2,"No eeprom structure");
2322 eeprom= ftdi->eeprom;
2323 output = eeprom->buf;
2325 if (eeprom->chip == -1)
2326 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2328 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2329 eeprom->size = 0x100;
2331 eeprom->size = 0x80;
2333 if (eeprom->manufacturer != NULL)
2334 manufacturer_size = strlen(eeprom->manufacturer);
2335 if (eeprom->product != NULL)
2336 product_size = strlen(eeprom->product);
2337 if (eeprom->serial != NULL)
2338 serial_size = strlen(eeprom->serial);
2340 // eeprom size check
2345 user_area_size = 96; // base size for strings (total of 48 characters)
2348 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2351 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2353 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2355 user_area_size = 86;
2361 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2363 if (user_area_size < 0)
2364 ftdi_error_return(-1,"eeprom size exceeded");
2367 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2369 // Bytes and Bits set for all Types
2371 // Addr 02: Vendor ID
2372 output[0x02] = eeprom->vendor_id;
2373 output[0x03] = eeprom->vendor_id >> 8;
2375 // Addr 04: Product ID
2376 output[0x04] = eeprom->product_id;
2377 output[0x05] = eeprom->product_id >> 8;
2379 // Addr 06: Device release number (0400h for BM features)
2380 output[0x06] = 0x00;
2384 output[0x07] = 0x02;
2387 output[0x07] = 0x04;
2390 output[0x07] = 0x05;
2393 output[0x07] = 0x06;
2396 output[0x07] = 0x07;
2399 output[0x07] = 0x08;
2402 output[0x07] = 0x09;
2405 output[0x07] = 0x00;
2408 // Addr 08: Config descriptor
2410 // Bit 6: 1 if this device is self powered, 0 if bus powered
2411 // Bit 5: 1 if this device uses remote wakeup
2412 // Bit 4-0: reserved - 0
2414 if (eeprom->self_powered == 1)
2416 if (eeprom->remote_wakeup == 1)
2420 // Addr 09: Max power consumption: max power = value * 2 mA
2421 output[0x09] = eeprom->max_power>>1;
2423 if (ftdi->type != TYPE_AM)
2425 // Addr 0A: Chip configuration
2426 // Bit 7: 0 - reserved
2427 // Bit 6: 0 - reserved
2428 // Bit 5: 0 - reserved
2429 // Bit 4: 1 - Change USB version
2430 // Bit 3: 1 - Use the serial number string
2431 // Bit 2: 1 - Enable suspend pull downs for lower power
2432 // Bit 1: 1 - Out EndPoint is Isochronous
2433 // Bit 0: 1 - In EndPoint is Isochronous
2436 if (eeprom->in_is_isochronous == 1)
2438 if (eeprom->out_is_isochronous == 1)
2444 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2445 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2463 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2464 eeprom_size_mask = eeprom->size -1;
2466 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2467 // Addr 0F: Length of manufacturer string
2468 // Output manufacturer
2469 output[0x0E] = i; // calculate offset
2470 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2471 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2472 for (j = 0; j < manufacturer_size; j++)
2474 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2475 output[i & eeprom_size_mask] = 0x00, i++;
2477 output[0x0F] = manufacturer_size*2 + 2;
2479 // Addr 10: Offset of the product string + 0x80, calculated later
2480 // Addr 11: Length of product string
2481 output[0x10] = i | 0x80; // calculate offset
2482 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2483 output[i & eeprom_size_mask] = 0x03, i++;
2484 for (j = 0; j < product_size; j++)
2486 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2487 output[i & eeprom_size_mask] = 0x00, i++;
2489 output[0x11] = product_size*2 + 2;
2491 // Addr 12: Offset of the serial string + 0x80, calculated later
2492 // Addr 13: Length of serial string
2493 output[0x12] = i | 0x80; // calculate offset
2494 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2495 output[i & eeprom_size_mask] = 0x03, i++;
2496 for (j = 0; j < serial_size; j++)
2498 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2499 output[i & eeprom_size_mask] = 0x00, i++;
2502 // Legacy port name and PnP fields for FT2232 and newer chips
2503 if (ftdi->type > TYPE_BM)
2505 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2507 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2509 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2513 output[0x13] = serial_size*2 + 2;
2515 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2517 if (eeprom->use_serial == USE_SERIAL_NUM )
2518 output[0x0A] |= USE_SERIAL_NUM;
2520 output[0x0A] &= ~USE_SERIAL_NUM;
2523 /* Bytes and Bits specific to (some) types
2524 Write linear, as this allows easier fixing*/
2530 output[0x0C] = eeprom->usb_version & 0xff;
2531 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2532 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2533 output[0x0A] |= USE_USB_VERSION_BIT;
2535 output[0x0A] &= ~USE_USB_VERSION_BIT;
2540 output[0x00] = (eeprom->channel_a_type);
2541 if ( eeprom->channel_a_driver == DRIVER_VCP)
2542 output[0x00] |= DRIVER_VCP;
2544 output[0x00] &= ~DRIVER_VCP;
2546 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2547 output[0x00] |= HIGH_CURRENT_DRIVE;
2549 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2551 output[0x01] = (eeprom->channel_b_type);
2552 if ( eeprom->channel_b_driver == DRIVER_VCP)
2553 output[0x01] |= DRIVER_VCP;
2555 output[0x01] &= ~DRIVER_VCP;
2557 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2558 output[0x01] |= HIGH_CURRENT_DRIVE;
2560 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2562 if (eeprom->in_is_isochronous == 1)
2563 output[0x0A] |= 0x1;
2565 output[0x0A] &= ~0x1;
2566 if (eeprom->out_is_isochronous == 1)
2567 output[0x0A] |= 0x2;
2569 output[0x0A] &= ~0x2;
2570 if (eeprom->suspend_pull_downs == 1)
2571 output[0x0A] |= 0x4;
2573 output[0x0A] &= ~0x4;
2574 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2575 output[0x0A] |= USE_USB_VERSION_BIT;
2577 output[0x0A] &= ~USE_USB_VERSION_BIT;
2579 output[0x0C] = eeprom->usb_version & 0xff;
2580 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2581 output[0x14] = eeprom->chip;
2584 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2585 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2586 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2588 if (eeprom->suspend_pull_downs == 1)
2589 output[0x0A] |= 0x4;
2591 output[0x0A] &= ~0x4;
2592 output[0x0B] = eeprom->invert;
2593 output[0x0C] = eeprom->usb_version & 0xff;
2594 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2596 if (eeprom->cbus_function[0] > CBUS_BB)
2597 output[0x14] = CBUS_TXLED;
2599 output[0x14] = eeprom->cbus_function[0];
2601 if (eeprom->cbus_function[1] > CBUS_BB)
2602 output[0x14] |= CBUS_RXLED<<4;
2604 output[0x14] |= eeprom->cbus_function[1]<<4;
2606 if (eeprom->cbus_function[2] > CBUS_BB)
2607 output[0x15] = CBUS_TXDEN;
2609 output[0x15] = eeprom->cbus_function[2];
2611 if (eeprom->cbus_function[3] > CBUS_BB)
2612 output[0x15] |= CBUS_PWREN<<4;
2614 output[0x15] |= eeprom->cbus_function[3]<<4;
2616 if (eeprom->cbus_function[4] > CBUS_CLK6)
2617 output[0x16] = CBUS_SLEEP;
2619 output[0x16] = eeprom->cbus_function[4];
2622 output[0x00] = (eeprom->channel_a_type);
2623 if ( eeprom->channel_a_driver == DRIVER_VCP)
2624 output[0x00] |= DRIVER_VCP;
2626 output[0x00] &= ~DRIVER_VCP;
2628 output[0x01] = (eeprom->channel_b_type);
2629 if ( eeprom->channel_b_driver == DRIVER_VCP)
2630 output[0x01] |= DRIVER_VCP;
2632 output[0x01] &= ~DRIVER_VCP;
2633 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2634 output[0x01] |= SUSPEND_DBUS7_BIT;
2636 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2638 if (eeprom->suspend_pull_downs == 1)
2639 output[0x0A] |= 0x4;
2641 output[0x0A] &= ~0x4;
2643 if (eeprom->group0_drive > DRIVE_16MA)
2644 output[0x0c] |= DRIVE_16MA;
2646 output[0x0c] |= eeprom->group0_drive;
2647 if (eeprom->group0_schmitt == IS_SCHMITT)
2648 output[0x0c] |= IS_SCHMITT;
2649 if (eeprom->group0_slew == SLOW_SLEW)
2650 output[0x0c] |= SLOW_SLEW;
2652 if (eeprom->group1_drive > DRIVE_16MA)
2653 output[0x0c] |= DRIVE_16MA<<4;
2655 output[0x0c] |= eeprom->group1_drive<<4;
2656 if (eeprom->group1_schmitt == IS_SCHMITT)
2657 output[0x0c] |= IS_SCHMITT<<4;
2658 if (eeprom->group1_slew == SLOW_SLEW)
2659 output[0x0c] |= SLOW_SLEW<<4;
2661 if (eeprom->group2_drive > DRIVE_16MA)
2662 output[0x0d] |= DRIVE_16MA;
2664 output[0x0d] |= eeprom->group2_drive;
2665 if (eeprom->group2_schmitt == IS_SCHMITT)
2666 output[0x0d] |= IS_SCHMITT;
2667 if (eeprom->group2_slew == SLOW_SLEW)
2668 output[0x0d] |= SLOW_SLEW;
2670 if (eeprom->group3_drive > DRIVE_16MA)
2671 output[0x0d] |= DRIVE_16MA<<4;
2673 output[0x0d] |= eeprom->group3_drive<<4;
2674 if (eeprom->group3_schmitt == IS_SCHMITT)
2675 output[0x0d] |= IS_SCHMITT<<4;
2676 if (eeprom->group3_slew == SLOW_SLEW)
2677 output[0x0d] |= SLOW_SLEW<<4;
2679 output[0x18] = eeprom->chip;
2683 output[0x18] = eeprom->chip;
2684 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2687 output[0x00] = (eeprom->channel_a_type);
2688 if ( eeprom->channel_a_driver == DRIVER_VCP)
2689 output[0x00] |= DRIVER_VCPH;
2691 output[0x00] &= ~DRIVER_VCPH;
2692 if (eeprom->powersave)
2693 output[0x01] |= POWER_SAVE_DISABLE_H;
2695 output[0x01] &= ~POWER_SAVE_DISABLE_H;
2696 if (eeprom->clock_polarity)
2697 output[0x01] |= FT1284_CLK_IDLE_STATE;
2699 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
2700 if (eeprom->data_order)
2701 output[0x01] |= FT1284_DATA_LSB;
2703 output[0x01] &= ~FT1284_DATA_LSB;
2704 if (eeprom->flow_control)
2705 output[0x01] |= FT1284_FLOW_CONTROL;
2707 output[0x01] &= ~FT1284_FLOW_CONTROL;
2708 if (eeprom->group0_drive > DRIVE_16MA)
2709 output[0x0c] |= DRIVE_16MA;
2711 output[0x0c] |= eeprom->group0_drive;
2712 if (eeprom->group0_schmitt == IS_SCHMITT)
2713 output[0x0c] |= IS_SCHMITT;
2714 if (eeprom->group0_slew == SLOW_SLEW)
2715 output[0x0c] |= SLOW_SLEW;
2717 if (eeprom->group1_drive > DRIVE_16MA)
2718 output[0x0d] |= DRIVE_16MA;
2720 output[0x0d] |= eeprom->group1_drive;
2721 if (eeprom->group1_schmitt == IS_SCHMITT)
2722 output[0x0d] |= IS_SCHMITT;
2723 if (eeprom->group1_slew == SLOW_SLEW)
2724 output[0x0d] |= SLOW_SLEW;
2726 set_ft232h_cbus(eeprom, output);
2728 output[0x1e] = eeprom->chip;
2729 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
2734 // calculate checksum
2737 for (i = 0; i < eeprom->size/2-1; i++)
2739 value = output[i*2];
2740 value += output[(i*2)+1] << 8;
2742 checksum = value^checksum;
2743 checksum = (checksum << 1) | (checksum >> 15);
2746 output[eeprom->size-2] = checksum;
2747 output[eeprom->size-1] = checksum >> 8;
2749 return user_area_size;
2753 Decode binary EEPROM image into an ftdi_eeprom structure.
2755 \param ftdi pointer to ftdi_context
2756 \param verbose Decode EEPROM on stdout
2759 \retval -1: something went wrong
2761 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2762 FIXME: Strings are malloc'ed here and should be freed somewhere
2764 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2767 unsigned short checksum, eeprom_checksum, value;
2768 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2770 struct ftdi_eeprom *eeprom;
2771 unsigned char *buf = ftdi->eeprom->buf;
2775 ftdi_error_return(-1,"No context");
2776 if (ftdi->eeprom == NULL)
2777 ftdi_error_return(-1,"No eeprom structure");
2779 eeprom = ftdi->eeprom;
2780 eeprom_size = eeprom->size;
2782 // Addr 02: Vendor ID
2783 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2785 // Addr 04: Product ID
2786 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2788 release = buf[0x06] + (buf[0x07]<<8);
2790 // Addr 08: Config descriptor
2792 // Bit 6: 1 if this device is self powered, 0 if bus powered
2793 // Bit 5: 1 if this device uses remote wakeup
2794 eeprom->self_powered = buf[0x08] & 0x40;
2795 eeprom->remote_wakeup = buf[0x08] & 0x20;
2797 // Addr 09: Max power consumption: max power = value * 2 mA
2798 eeprom->max_power = buf[0x09];
2800 // Addr 0A: Chip configuration
2801 // Bit 7: 0 - reserved
2802 // Bit 6: 0 - reserved
2803 // Bit 5: 0 - reserved
2804 // Bit 4: 1 - Change USB version on BM and 2232C
2805 // Bit 3: 1 - Use the serial number string
2806 // Bit 2: 1 - Enable suspend pull downs for lower power
2807 // Bit 1: 1 - Out EndPoint is Isochronous
2808 // Bit 0: 1 - In EndPoint is Isochronous
2810 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2811 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2812 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2813 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2814 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2816 // Addr 0C: USB version low byte when 0x0A
2817 // Addr 0D: USB version high byte when 0x0A
2818 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2820 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2821 // Addr 0F: Length of manufacturer string
2822 manufacturer_size = buf[0x0F]/2;
2823 if (eeprom->manufacturer)
2824 free(eeprom->manufacturer);
2825 if (manufacturer_size > 0)
2827 eeprom->manufacturer = malloc(manufacturer_size);
2828 if (eeprom->manufacturer)
2830 // Decode manufacturer
2831 i = buf[0x0E] & (eeprom_size -1); // offset
2832 for (j=0;j<manufacturer_size-1;j++)
2834 eeprom->manufacturer[j] = buf[2*j+i+2];
2836 eeprom->manufacturer[j] = '\0';
2839 else eeprom->manufacturer = NULL;
2841 // Addr 10: Offset of the product string + 0x80, calculated later
2842 // Addr 11: Length of product string
2843 if (eeprom->product)
2844 free(eeprom->product);
2845 product_size = buf[0x11]/2;
2846 if (product_size > 0)
2848 eeprom->product = malloc(product_size);
2849 if (eeprom->product)
2851 // Decode product name
2852 i = buf[0x10] & (eeprom_size -1); // offset
2853 for (j=0;j<product_size-1;j++)
2855 eeprom->product[j] = buf[2*j+i+2];
2857 eeprom->product[j] = '\0';
2860 else eeprom->product = NULL;
2862 // Addr 12: Offset of the serial string + 0x80, calculated later
2863 // Addr 13: Length of serial string
2865 free(eeprom->serial);
2866 serial_size = buf[0x13]/2;
2867 if (serial_size > 0)
2869 eeprom->serial = malloc(serial_size);
2873 i = buf[0x12] & (eeprom_size -1); // offset
2874 for (j=0;j<serial_size-1;j++)
2876 eeprom->serial[j] = buf[2*j+i+2];
2878 eeprom->serial[j] = '\0';
2881 else eeprom->serial = NULL;
2886 for (i = 0; i < eeprom_size/2-1; i++)
2889 value += buf[(i*2)+1] << 8;
2891 checksum = value^checksum;
2892 checksum = (checksum << 1) | (checksum >> 15);
2895 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2897 if (eeprom_checksum != checksum)
2899 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2900 ftdi_error_return(-1,"EEPROM checksum error");
2903 eeprom->channel_a_type = 0;
2904 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2908 else if (ftdi->type == TYPE_2232C)
2910 eeprom->channel_a_type = buf[0x00] & 0x7;
2911 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2912 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2913 eeprom->channel_b_type = buf[0x01] & 0x7;
2914 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2915 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2916 eeprom->chip = buf[0x14];
2918 else if (ftdi->type == TYPE_R)
2920 /* TYPE_R flags D2XX, not VCP as all others*/
2921 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2922 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2923 if ( (buf[0x01]&0x40) != 0x40)
2925 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2926 " If this happened with the\n"
2927 " EEPROM programmed by FTDI tools, please report "
2928 "to libftdi@developer.intra2net.com\n");
2930 eeprom->chip = buf[0x16];
2931 // Addr 0B: Invert data lines
2932 // Works only on FT232R, not FT245R, but no way to distinguish
2933 eeprom->invert = buf[0x0B];
2934 // Addr 14: CBUS function: CBUS0, CBUS1
2935 // Addr 15: CBUS function: CBUS2, CBUS3
2936 // Addr 16: CBUS function: CBUS5
2937 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2938 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2939 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2940 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2941 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2943 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2945 eeprom->channel_a_type = buf[0x00] & 0x7;
2946 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2947 eeprom->channel_b_type = buf[0x01] & 0x7;
2948 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2950 if (ftdi->type == TYPE_2232H)
2951 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2953 eeprom->chip = buf[0x18];
2954 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2955 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2956 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2957 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2958 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2959 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2960 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2961 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2962 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2963 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2964 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2965 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2967 else if (ftdi->type == TYPE_232H)
2971 eeprom->channel_a_type = buf[0x00] & 0xf;
2972 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
2973 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
2974 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
2975 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
2976 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
2977 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2978 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2979 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2980 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
2981 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
2982 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
2986 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
2987 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
2989 eeprom->chip = buf[0x1e];
2990 /*FIXME: Decipher more values*/
2995 char *channel_mode[] = {"UART","245","CPU", "unknown", "OPTO", "unknown1","unknown2","unknown3","FT1284"};
2996 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
2997 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
2998 fprintf(stdout, "Release: 0x%04x\n",release);
3000 if (eeprom->self_powered)
3001 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3003 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
3004 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3005 if (eeprom->manufacturer)
3006 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3007 if (eeprom->product)
3008 fprintf(stdout, "Product: %s\n",eeprom->product);
3010 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3011 fprintf(stdout, "Checksum : %04x\n", checksum);
3012 if (ftdi->type == TYPE_R)
3013 fprintf(stdout, "Internal EEPROM\n");
3014 else if (eeprom->chip >= 0x46)
3015 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3016 if (eeprom->suspend_dbus7)
3017 fprintf(stdout, "Suspend on DBUS7\n");
3018 if (eeprom->suspend_pull_downs)
3019 fprintf(stdout, "Pull IO pins low during suspend\n");
3020 if(eeprom->powersave)
3022 if(ftdi->type >= TYPE_232H)
3023 fprintf(stdout,"Enter low power state on ACBUS7\n");
3025 if (eeprom->remote_wakeup)
3026 fprintf(stdout, "Enable Remote Wake Up\n");
3027 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3028 if (ftdi->type >= TYPE_2232C)
3029 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3030 channel_mode[eeprom->channel_a_type],
3031 (eeprom->channel_a_driver)?" VCP":"",
3032 (eeprom->high_current_a)?" High Current IO":"");
3033 if (ftdi->type >= TYPE_232H)
3035 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3036 (eeprom->clock_polarity)?"HIGH":"LOW",
3037 (eeprom->data_order)?"LSB":"MSB",
3038 (eeprom->flow_control)?"":"No ");
3040 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
3041 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3042 channel_mode[eeprom->channel_b_type],
3043 (eeprom->channel_b_driver)?" VCP":"",
3044 (eeprom->high_current_b)?" High Current IO":"");
3045 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3046 eeprom->use_usb_version == USE_USB_VERSION_BIT)
3047 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3049 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3051 fprintf(stdout,"%s has %d mA drive%s%s\n",
3052 (ftdi->type == TYPE_2232H)?"AL":"A",
3053 (eeprom->group0_drive+1) *4,
3054 (eeprom->group0_schmitt)?" Schmitt Input":"",
3055 (eeprom->group0_slew)?" Slow Slew":"");
3056 fprintf(stdout,"%s has %d mA drive%s%s\n",
3057 (ftdi->type == TYPE_2232H)?"AH":"B",
3058 (eeprom->group1_drive+1) *4,
3059 (eeprom->group1_schmitt)?" Schmitt Input":"",
3060 (eeprom->group1_slew)?" Slow Slew":"");
3061 fprintf(stdout,"%s has %d mA drive%s%s\n",
3062 (ftdi->type == TYPE_2232H)?"BL":"C",
3063 (eeprom->group2_drive+1) *4,
3064 (eeprom->group2_schmitt)?" Schmitt Input":"",
3065 (eeprom->group2_slew)?" Slow Slew":"");
3066 fprintf(stdout,"%s has %d mA drive%s%s\n",
3067 (ftdi->type == TYPE_2232H)?"BH":"D",
3068 (eeprom->group3_drive+1) *4,
3069 (eeprom->group3_schmitt)?" Schmitt Input":"",
3070 (eeprom->group3_slew)?" Slow Slew":"");
3072 else if (ftdi->type == TYPE_232H)
3075 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3076 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3077 "CLK30","CLK15","CLK7_5"
3079 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3080 (eeprom->group0_drive+1) *4,
3081 (eeprom->group0_schmitt)?" Schmitt Input":"",
3082 (eeprom->group0_slew)?" Slow Slew":"");
3083 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3084 (eeprom->group1_drive+1) *4,
3085 (eeprom->group1_schmitt)?" Schmitt Input":"",
3086 (eeprom->group1_slew)?" Slow Slew":"");
3087 for (i=0; i<10; i++)
3089 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3090 fprintf(stdout,"C%d Function: %s\n", i,
3091 cbush_mux[eeprom->cbus_function[i]]);
3096 if (ftdi->type == TYPE_R)
3098 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3099 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3100 "IOMODE","BB_WR","BB_RD"
3102 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3106 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
3107 fprintf(stdout,"Inverted bits:");
3109 if ((eeprom->invert & (1<<i)) == (1<<i))
3110 fprintf(stdout," %s",r_bits[i]);
3111 fprintf(stdout,"\n");
3115 if (eeprom->cbus_function[i]<CBUS_BB)
3116 fprintf(stdout,"C%d Function: %s\n", i,
3117 cbus_mux[eeprom->cbus_function[i]]);
3121 /* Running MPROG show that C0..3 have fixed function Synchronous
3123 fprintf(stdout,"C%d BB Function: %s\n", i,
3126 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3135 Get a value from the decoded EEPROM structure
3137 \param ftdi pointer to ftdi_context
3138 \param value_name Enum of the value to query
3139 \param value Pointer to store read value
3142 \retval -1: Value doesn't exist
3144 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3149 *value = ftdi->eeprom->vendor_id;
3152 *value = ftdi->eeprom->product_id;
3155 *value = ftdi->eeprom->self_powered;
3158 *value = ftdi->eeprom->remote_wakeup;
3161 *value = ftdi->eeprom->is_not_pnp;
3164 *value = ftdi->eeprom->suspend_dbus7;
3166 case IN_IS_ISOCHRONOUS:
3167 *value = ftdi->eeprom->in_is_isochronous;
3169 case SUSPEND_PULL_DOWNS:
3170 *value = ftdi->eeprom->suspend_pull_downs;
3173 *value = ftdi->eeprom->use_serial;
3176 *value = ftdi->eeprom->usb_version;
3179 *value = ftdi->eeprom->max_power;
3181 case CHANNEL_A_TYPE:
3182 *value = ftdi->eeprom->channel_a_type;
3184 case CHANNEL_B_TYPE:
3185 *value = ftdi->eeprom->channel_b_type;
3187 case CHANNEL_A_DRIVER:
3188 *value = ftdi->eeprom->channel_a_driver;
3190 case CHANNEL_B_DRIVER:
3191 *value = ftdi->eeprom->channel_b_driver;
3193 case CBUS_FUNCTION_0:
3194 *value = ftdi->eeprom->cbus_function[0];
3196 case CBUS_FUNCTION_1:
3197 *value = ftdi->eeprom->cbus_function[1];
3199 case CBUS_FUNCTION_2:
3200 *value = ftdi->eeprom->cbus_function[2];
3202 case CBUS_FUNCTION_3:
3203 *value = ftdi->eeprom->cbus_function[3];
3205 case CBUS_FUNCTION_4:
3206 *value = ftdi->eeprom->cbus_function[4];
3208 case CBUS_FUNCTION_5:
3209 *value = ftdi->eeprom->cbus_function[5];
3211 case CBUS_FUNCTION_6:
3212 *value = ftdi->eeprom->cbus_function[6];
3214 case CBUS_FUNCTION_7:
3215 *value = ftdi->eeprom->cbus_function[7];
3217 case CBUS_FUNCTION_8:
3218 *value = ftdi->eeprom->cbus_function[8];
3220 case CBUS_FUNCTION_9:
3221 *value = ftdi->eeprom->cbus_function[8];
3224 *value = ftdi->eeprom->high_current;
3226 case HIGH_CURRENT_A:
3227 *value = ftdi->eeprom->high_current_a;
3229 case HIGH_CURRENT_B:
3230 *value = ftdi->eeprom->high_current_b;
3233 *value = ftdi->eeprom->invert;
3236 *value = ftdi->eeprom->group0_drive;
3238 case GROUP0_SCHMITT:
3239 *value = ftdi->eeprom->group0_schmitt;
3242 *value = ftdi->eeprom->group0_slew;
3245 *value = ftdi->eeprom->group1_drive;
3247 case GROUP1_SCHMITT:
3248 *value = ftdi->eeprom->group1_schmitt;
3251 *value = ftdi->eeprom->group1_slew;
3254 *value = ftdi->eeprom->group2_drive;
3256 case GROUP2_SCHMITT:
3257 *value = ftdi->eeprom->group2_schmitt;
3260 *value = ftdi->eeprom->group2_slew;
3263 *value = ftdi->eeprom->group3_drive;
3265 case GROUP3_SCHMITT:
3266 *value = ftdi->eeprom->group3_schmitt;
3269 *value = ftdi->eeprom->group3_slew;
3272 *value = ftdi->eeprom->powersave;
3274 case CLOCK_POLARITY:
3275 *value = ftdi->eeprom->clock_polarity;
3278 *value = ftdi->eeprom->data_order;
3281 *value = ftdi->eeprom->flow_control;
3284 *value = ftdi->eeprom->chip;
3287 *value = ftdi->eeprom->size;
3290 ftdi_error_return(-1, "Request for unknown EEPROM value");
3296 Set a value in the decoded EEPROM Structure
3297 No parameter checking is performed
3299 \param ftdi pointer to ftdi_context
3300 \param value_name Enum of the value to set
3304 \retval -1: Value doesn't exist
3305 \retval -2: Value not user settable
3307 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3312 ftdi->eeprom->vendor_id = value;
3315 ftdi->eeprom->product_id = value;
3318 ftdi->eeprom->self_powered = value;
3321 ftdi->eeprom->remote_wakeup = value;
3324 ftdi->eeprom->is_not_pnp = value;
3327 ftdi->eeprom->suspend_dbus7 = value;
3329 case IN_IS_ISOCHRONOUS:
3330 ftdi->eeprom->in_is_isochronous = value;
3332 case SUSPEND_PULL_DOWNS:
3333 ftdi->eeprom->suspend_pull_downs = value;
3336 ftdi->eeprom->use_serial = value;
3339 ftdi->eeprom->usb_version = value;
3342 ftdi->eeprom->max_power = value;
3344 case CHANNEL_A_TYPE:
3345 ftdi->eeprom->channel_a_type = value;
3347 case CHANNEL_B_TYPE:
3348 ftdi->eeprom->channel_b_type = value;
3350 case CHANNEL_A_DRIVER:
3351 ftdi->eeprom->channel_a_driver = value;
3353 case CHANNEL_B_DRIVER:
3354 ftdi->eeprom->channel_b_driver = value;
3356 case CBUS_FUNCTION_0:
3357 ftdi->eeprom->cbus_function[0] = value;
3359 case CBUS_FUNCTION_1:
3360 ftdi->eeprom->cbus_function[1] = value;
3362 case CBUS_FUNCTION_2:
3363 ftdi->eeprom->cbus_function[2] = value;
3365 case CBUS_FUNCTION_3:
3366 ftdi->eeprom->cbus_function[3] = value;
3368 case CBUS_FUNCTION_4:
3369 ftdi->eeprom->cbus_function[4] = value;
3371 case CBUS_FUNCTION_5:
3372 ftdi->eeprom->cbus_function[5] = value;
3374 case CBUS_FUNCTION_6:
3375 ftdi->eeprom->cbus_function[6] = value;
3377 case CBUS_FUNCTION_7:
3378 ftdi->eeprom->cbus_function[7] = value;
3380 case CBUS_FUNCTION_8:
3381 ftdi->eeprom->cbus_function[8] = value;
3383 case CBUS_FUNCTION_9:
3384 ftdi->eeprom->cbus_function[9] = value;
3387 ftdi->eeprom->high_current = value;
3389 case HIGH_CURRENT_A:
3390 ftdi->eeprom->high_current_a = value;
3392 case HIGH_CURRENT_B:
3393 ftdi->eeprom->high_current_b = value;
3396 ftdi->eeprom->invert = value;
3399 ftdi->eeprom->group0_drive = value;
3401 case GROUP0_SCHMITT:
3402 ftdi->eeprom->group0_schmitt = value;
3405 ftdi->eeprom->group0_slew = value;
3408 ftdi->eeprom->group1_drive = value;
3410 case GROUP1_SCHMITT:
3411 ftdi->eeprom->group1_schmitt = value;
3414 ftdi->eeprom->group1_slew = value;
3417 ftdi->eeprom->group2_drive = value;
3419 case GROUP2_SCHMITT:
3420 ftdi->eeprom->group2_schmitt = value;
3423 ftdi->eeprom->group2_slew = value;
3426 ftdi->eeprom->group3_drive = value;
3428 case GROUP3_SCHMITT:
3429 ftdi->eeprom->group3_schmitt = value;
3432 ftdi->eeprom->group3_slew = value;
3435 ftdi->eeprom->chip = value;
3438 ftdi->eeprom->powersave = value;
3440 case CLOCK_POLARITY:
3441 ftdi->eeprom->clock_polarity = value;
3444 ftdi->eeprom->data_order = value;
3447 ftdi->eeprom->flow_control = value;
3450 ftdi_error_return(-2, "EEPROM Value can't be changed");
3452 ftdi_error_return(-1, "Request to unknown EEPROM value");
3457 /** Get the read-only buffer to the binary EEPROM content
3459 \param ftdi pointer to ftdi_context
3460 \param buf buffer to receive EEPROM content
3461 \param size Size of receiving buffer
3464 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3465 \retval -2: Not enough room to store eeprom
3467 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3469 if (!ftdi || !(ftdi->eeprom))
3470 ftdi_error_return(-1, "No appropriate structure");
3472 if (!buf || size < ftdi->eeprom->size)
3473 ftdi_error_return(-1, "Not enough room to store eeprom");
3475 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3476 if (size > FTDI_MAX_EEPROM_SIZE)
3477 size = FTDI_MAX_EEPROM_SIZE;
3479 memcpy(buf, ftdi->eeprom->buf, size);
3485 Read eeprom location
3487 \param ftdi pointer to ftdi_context
3488 \param eeprom_addr Address of eeprom location to be read
3489 \param eeprom_val Pointer to store read eeprom location
3492 \retval -1: read failed
3493 \retval -2: USB device unavailable
3495 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3497 if (ftdi == NULL || ftdi->usb_dev == NULL)
3498 ftdi_error_return(-2, "USB device unavailable");
3500 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)
3501 ftdi_error_return(-1, "reading eeprom failed");
3509 \param ftdi pointer to ftdi_context
3512 \retval -1: read failed
3513 \retval -2: USB device unavailable
3515 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3520 if (ftdi == NULL || ftdi->usb_dev == NULL)
3521 ftdi_error_return(-2, "USB device unavailable");
3522 buf = ftdi->eeprom->buf;
3524 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3526 if (libusb_control_transfer(
3527 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3528 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3529 ftdi_error_return(-1, "reading eeprom failed");
3532 if (ftdi->type == TYPE_R)
3533 ftdi->eeprom->size = 0x80;
3534 /* Guesses size of eeprom by comparing halves
3535 - will not work with blank eeprom */
3536 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3537 ftdi->eeprom->size = -1;
3538 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3539 ftdi->eeprom->size = 0x80;
3540 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3541 ftdi->eeprom->size = 0x40;
3543 ftdi->eeprom->size = 0x100;
3548 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3549 Function is only used internally
3552 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3554 return ((value & 1) << 1) |
3555 ((value & 2) << 5) |
3556 ((value & 4) >> 2) |
3557 ((value & 8) << 4) |
3558 ((value & 16) >> 1) |
3559 ((value & 32) >> 1) |
3560 ((value & 64) >> 4) |
3561 ((value & 128) >> 2);
3565 Read the FTDIChip-ID from R-type devices
3567 \param ftdi pointer to ftdi_context
3568 \param chipid Pointer to store FTDIChip-ID
3571 \retval -1: read failed
3572 \retval -2: USB device unavailable
3574 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3576 unsigned int a = 0, b = 0;
3578 if (ftdi == NULL || ftdi->usb_dev == NULL)
3579 ftdi_error_return(-2, "USB device unavailable");
3581 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)
3583 a = a << 8 | a >> 8;
3584 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)
3586 b = b << 8 | b >> 8;
3587 a = (a << 16) | (b & 0xFFFF);
3588 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3589 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3590 *chipid = a ^ 0xa5f0f7d1;
3595 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3599 Write eeprom location
3601 \param ftdi pointer to ftdi_context
3602 \param eeprom_addr Address of eeprom location to be written
3603 \param eeprom_val Value to be written
3606 \retval -1: write failed
3607 \retval -2: USB device unavailable
3608 \retval -3: Invalid access to checksum protected area below 0x80
3609 \retval -4: Device can't access unprotected area
3610 \retval -5: Reading chip type failed
3612 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3613 unsigned short eeprom_val)
3615 int chip_type_location;
3616 unsigned short chip_type;
3618 if (ftdi == NULL || ftdi->usb_dev == NULL)
3619 ftdi_error_return(-2, "USB device unavailable");
3621 if (eeprom_addr <0x80)
3622 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3629 chip_type_location = 0x14;
3633 chip_type_location = 0x18;
3636 chip_type_location = 0x1e;
3639 ftdi_error_return(-4, "Device can't access unprotected area");
3642 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3643 ftdi_error_return(-5, "Reading failed failed");
3644 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3645 if ((chip_type & 0xff) != 0x66)
3647 ftdi_error_return(-6, "EEPROM is not of 93x66");
3650 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3651 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3652 NULL, 0, ftdi->usb_write_timeout) != 0)
3653 ftdi_error_return(-1, "unable to write eeprom");
3661 \param ftdi pointer to ftdi_context
3664 \retval -1: read failed
3665 \retval -2: USB device unavailable
3667 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3669 unsigned short usb_val, status;
3671 unsigned char *eeprom;
3673 if (ftdi == NULL || ftdi->usb_dev == NULL)
3674 ftdi_error_return(-2, "USB device unavailable");
3675 eeprom = ftdi->eeprom->buf;
3677 /* These commands were traced while running MProg */
3678 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3680 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3682 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3685 for (i = 0; i < ftdi->eeprom->size/2; i++)
3687 usb_val = eeprom[i*2];
3688 usb_val += eeprom[(i*2)+1] << 8;
3689 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3690 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3691 NULL, 0, ftdi->usb_write_timeout) < 0)
3692 ftdi_error_return(-1, "unable to write eeprom");
3701 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3703 \param ftdi pointer to ftdi_context
3706 \retval -1: erase failed
3707 \retval -2: USB device unavailable
3708 \retval -3: Writing magic failed
3709 \retval -4: Read EEPROM failed
3710 \retval -5: Unexpected EEPROM value
3712 #define MAGIC 0x55aa
3713 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3715 unsigned short eeprom_value;
3716 if (ftdi == NULL || ftdi->usb_dev == NULL)
3717 ftdi_error_return(-2, "USB device unavailable");
3719 if (ftdi->type == TYPE_R)
3721 ftdi->eeprom->chip = 0;
3725 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3726 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3727 ftdi_error_return(-1, "unable to erase eeprom");
3730 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3731 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3732 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3733 Chip is 93x66 if magic is only read at word position 0xc0*/
3734 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3735 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3736 NULL, 0, ftdi->usb_write_timeout) != 0)
3737 ftdi_error_return(-3, "Writing magic failed");
3738 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3739 ftdi_error_return(-4, "Reading failed failed");
3740 if (eeprom_value == MAGIC)
3742 ftdi->eeprom->chip = 0x46;
3746 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3747 ftdi_error_return(-4, "Reading failed failed");
3748 if (eeprom_value == MAGIC)
3749 ftdi->eeprom->chip = 0x56;
3752 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3753 ftdi_error_return(-4, "Reading failed failed");
3754 if (eeprom_value == MAGIC)
3755 ftdi->eeprom->chip = 0x66;
3758 ftdi->eeprom->chip = -1;
3762 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3763 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3764 ftdi_error_return(-1, "unable to erase eeprom");
3769 Get string representation for last error code
3771 \param ftdi pointer to ftdi_context
3773 \retval Pointer to error string
3775 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3780 return ftdi->error_str;
3783 /* @} end of doxygen libftdi group */