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(-6, "libusb_get_device_descriptor() failed");
294 if (desc.idVendor == vendor && desc.idProduct == product)
296 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
298 ftdi_error_return(-3, "out of memory");
300 (*curdev)->next = NULL;
301 (*curdev)->dev = dev;
303 curdev = &(*curdev)->next;
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)
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;
568 // Determine maximum packet size
569 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
571 if (ftdi_set_baudrate (ftdi, 9600) != 0)
573 ftdi_usb_close_internal (ftdi);
574 ftdi_error_return(-7, "set baudrate failed");
577 ftdi_error_return(0, "all fine");
581 Opens the first device with a given vendor and product ids.
583 \param ftdi pointer to ftdi_context
584 \param vendor Vendor ID
585 \param product Product ID
587 \retval same as ftdi_usb_open_desc()
589 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
591 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
595 Opens the first device with a given, vendor id, product id,
596 description and serial.
598 \param ftdi pointer to ftdi_context
599 \param vendor Vendor ID
600 \param product Product ID
601 \param description Description to search for. Use NULL if not needed.
602 \param serial Serial to search for. Use NULL if not needed.
605 \retval -3: usb device not found
606 \retval -4: unable to open device
607 \retval -5: unable to claim device
608 \retval -6: reset failed
609 \retval -7: set baudrate failed
610 \retval -8: get product description failed
611 \retval -9: get serial number failed
612 \retval -11: libusb_init() failed
613 \retval -12: libusb_get_device_list() failed
614 \retval -13: libusb_get_device_descriptor() failed
616 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
617 const char* description, const char* serial)
619 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
623 Opens the index-th device with a given, vendor id, product id,
624 description and serial.
626 \param ftdi pointer to ftdi_context
627 \param vendor Vendor ID
628 \param product Product ID
629 \param description Description to search for. Use NULL if not needed.
630 \param serial Serial to search for. Use NULL if not needed.
631 \param index Number of matching device to open if there are more than one, starts with 0.
634 \retval -1: usb_find_busses() failed
635 \retval -2: usb_find_devices() failed
636 \retval -3: usb device not found
637 \retval -4: unable to open device
638 \retval -5: unable to claim device
639 \retval -6: reset failed
640 \retval -7: set baudrate failed
641 \retval -8: get product description failed
642 \retval -9: get serial number failed
643 \retval -10: unable to close device
644 \retval -11: ftdi context invalid
646 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
647 const char* description, const char* serial, unsigned int index)
650 libusb_device **devs;
655 ftdi_error_return(-11, "ftdi context invalid");
657 if (libusb_init(&ftdi->usb_ctx) < 0)
658 ftdi_error_return(-11, "libusb_init() failed");
660 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
661 ftdi_error_return(-12, "libusb_get_device_list() failed");
663 while ((dev = devs[i++]) != NULL)
665 struct libusb_device_descriptor desc;
668 if (libusb_get_device_descriptor(dev, &desc) < 0)
669 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
671 if (desc.idVendor == vendor && desc.idProduct == product)
673 if (libusb_open(dev, &ftdi->usb_dev) < 0)
674 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
676 if (description != NULL)
678 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
680 ftdi_usb_close_internal (ftdi);
681 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
683 if (strncmp(string, description, sizeof(string)) != 0)
685 ftdi_usb_close_internal (ftdi);
691 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
693 ftdi_usb_close_internal (ftdi);
694 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
696 if (strncmp(string, serial, sizeof(string)) != 0)
698 ftdi_usb_close_internal (ftdi);
703 ftdi_usb_close_internal (ftdi);
711 res = ftdi_usb_open_dev(ftdi, dev);
712 libusb_free_device_list(devs,1);
718 ftdi_error_return_free_device_list(-3, "device not found", devs);
722 Opens the ftdi-device described by a description-string.
723 Intended to be used for parsing a device-description given as commandline argument.
725 \param ftdi pointer to ftdi_context
726 \param description NULL-terminated description-string, using this format:
727 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
728 \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")
729 \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
730 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
732 \note The description format may be extended in later versions.
735 \retval -1: libusb_init() failed
736 \retval -2: libusb_get_device_list() failed
737 \retval -3: usb device not found
738 \retval -4: unable to open device
739 \retval -5: unable to claim device
740 \retval -6: reset failed
741 \retval -7: set baudrate failed
742 \retval -8: get product description failed
743 \retval -9: get serial number failed
744 \retval -10: unable to close device
745 \retval -11: illegal description format
746 \retval -12: ftdi context invalid
748 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
751 ftdi_error_return(-12, "ftdi context invalid");
753 if (description[0] == 0 || description[1] != ':')
754 ftdi_error_return(-11, "illegal description format");
756 if (description[0] == 'd')
759 libusb_device **devs;
760 unsigned int bus_number, device_address;
763 if (libusb_init (&ftdi->usb_ctx) < 0)
764 ftdi_error_return(-1, "libusb_init() failed");
766 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
767 ftdi_error_return(-2, "libusb_get_device_list() failed");
769 /* XXX: This doesn't handle symlinks/odd paths/etc... */
770 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
771 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
773 while ((dev = devs[i++]) != NULL)
776 if (bus_number == libusb_get_bus_number (dev)
777 && device_address == libusb_get_device_address (dev))
779 ret = ftdi_usb_open_dev(ftdi, dev);
780 libusb_free_device_list(devs,1);
786 ftdi_error_return_free_device_list(-3, "device not found", devs);
788 else if (description[0] == 'i' || description[0] == 's')
791 unsigned int product;
792 unsigned int index=0;
793 const char *serial=NULL;
794 const char *startp, *endp;
797 startp=description+2;
798 vendor=strtoul((char*)startp,(char**)&endp,0);
799 if (*endp != ':' || endp == startp || errno != 0)
800 ftdi_error_return(-11, "illegal description format");
803 product=strtoul((char*)startp,(char**)&endp,0);
804 if (endp == startp || errno != 0)
805 ftdi_error_return(-11, "illegal description format");
807 if (description[0] == 'i' && *endp != 0)
809 /* optional index field in i-mode */
811 ftdi_error_return(-11, "illegal description format");
814 index=strtoul((char*)startp,(char**)&endp,0);
815 if (*endp != 0 || endp == startp || errno != 0)
816 ftdi_error_return(-11, "illegal description format");
818 if (description[0] == 's')
821 ftdi_error_return(-11, "illegal description format");
823 /* rest of the description is the serial */
827 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
831 ftdi_error_return(-11, "illegal description format");
836 Resets the ftdi device.
838 \param ftdi pointer to ftdi_context
841 \retval -1: FTDI reset failed
842 \retval -2: USB device unavailable
844 int ftdi_usb_reset(struct ftdi_context *ftdi)
846 if (ftdi == NULL || ftdi->usb_dev == NULL)
847 ftdi_error_return(-2, "USB device unavailable");
849 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
850 SIO_RESET_REQUEST, SIO_RESET_SIO,
851 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
852 ftdi_error_return(-1,"FTDI reset failed");
854 // Invalidate data in the readbuffer
855 ftdi->readbuffer_offset = 0;
856 ftdi->readbuffer_remaining = 0;
862 Clears the read buffer on the chip and the internal read buffer.
864 \param ftdi pointer to ftdi_context
867 \retval -1: read buffer purge failed
868 \retval -2: USB device unavailable
870 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
872 if (ftdi == NULL || ftdi->usb_dev == NULL)
873 ftdi_error_return(-2, "USB device unavailable");
875 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
876 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
877 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
878 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
880 // Invalidate data in the readbuffer
881 ftdi->readbuffer_offset = 0;
882 ftdi->readbuffer_remaining = 0;
888 Clears the write buffer on the chip.
890 \param ftdi pointer to ftdi_context
893 \retval -1: write buffer purge failed
894 \retval -2: USB device unavailable
896 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
898 if (ftdi == NULL || ftdi->usb_dev == NULL)
899 ftdi_error_return(-2, "USB device unavailable");
901 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
902 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
903 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
904 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
910 Clears the buffers on the chip and the internal read buffer.
912 \param ftdi pointer to ftdi_context
915 \retval -1: read buffer purge failed
916 \retval -2: write buffer purge failed
917 \retval -3: USB device unavailable
919 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
923 if (ftdi == NULL || ftdi->usb_dev == NULL)
924 ftdi_error_return(-3, "USB device unavailable");
926 result = ftdi_usb_purge_rx_buffer(ftdi);
930 result = ftdi_usb_purge_tx_buffer(ftdi);
940 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
942 \param ftdi pointer to ftdi_context
945 \retval -1: usb_release failed
946 \retval -3: ftdi context invalid
948 int ftdi_usb_close(struct ftdi_context *ftdi)
953 ftdi_error_return(-3, "ftdi context invalid");
955 if (ftdi->usb_dev != NULL)
956 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
959 ftdi_usb_close_internal (ftdi);
965 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
966 Function is only used internally
969 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
970 unsigned short *value, unsigned short *index)
972 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
973 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
974 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
975 int divisor, best_divisor, best_baud, best_baud_diff;
976 unsigned long encoded_divisor;
985 divisor = 24000000 / baudrate;
987 if (ftdi->type == TYPE_AM)
989 // Round down to supported fraction (AM only)
990 divisor -= am_adjust_dn[divisor & 7];
993 // Try this divisor and the one above it (because division rounds down)
997 for (i = 0; i < 2; i++)
999 int try_divisor = divisor + i;
1003 // Round up to supported divisor value
1004 if (try_divisor <= 8)
1006 // Round up to minimum supported divisor
1009 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1011 // BM doesn't support divisors 9 through 11 inclusive
1014 else if (divisor < 16)
1016 // AM doesn't support divisors 9 through 15 inclusive
1021 if (ftdi->type == TYPE_AM)
1023 // Round up to supported fraction (AM only)
1024 try_divisor += am_adjust_up[try_divisor & 7];
1025 if (try_divisor > 0x1FFF8)
1027 // Round down to maximum supported divisor value (for AM)
1028 try_divisor = 0x1FFF8;
1033 if (try_divisor > 0x1FFFF)
1035 // Round down to maximum supported divisor value (for BM)
1036 try_divisor = 0x1FFFF;
1040 // Get estimated baud rate (to nearest integer)
1041 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1042 // Get absolute difference from requested baud rate
1043 if (baud_estimate < baudrate)
1045 baud_diff = baudrate - baud_estimate;
1049 baud_diff = baud_estimate - baudrate;
1051 if (i == 0 || baud_diff < best_baud_diff)
1053 // Closest to requested baud rate so far
1054 best_divisor = try_divisor;
1055 best_baud = baud_estimate;
1056 best_baud_diff = baud_diff;
1059 // Spot on! No point trying
1064 // Encode the best divisor value
1065 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1066 // Deal with special cases for encoded value
1067 if (encoded_divisor == 1)
1069 encoded_divisor = 0; // 3000000 baud
1071 else if (encoded_divisor == 0x4001)
1073 encoded_divisor = 1; // 2000000 baud (BM only)
1075 // Split into "value" and "index" values
1076 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1077 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1079 *index = (unsigned short)(encoded_divisor >> 8);
1081 *index |= ftdi->index;
1084 *index = (unsigned short)(encoded_divisor >> 16);
1086 // Return the nearest baud rate
1091 Sets the chip baud rate
1093 \param ftdi pointer to ftdi_context
1094 \param baudrate baud rate to set
1097 \retval -1: invalid baudrate
1098 \retval -2: setting baudrate failed
1099 \retval -3: USB device unavailable
1101 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1103 unsigned short value, index;
1104 int actual_baudrate;
1106 if (ftdi == NULL || ftdi->usb_dev == NULL)
1107 ftdi_error_return(-3, "USB device unavailable");
1109 if (ftdi->bitbang_enabled)
1111 baudrate = baudrate*4;
1114 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1115 if (actual_baudrate <= 0)
1116 ftdi_error_return (-1, "Silly baudrate <= 0.");
1118 // Check within tolerance (about 5%)
1119 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1120 || ((actual_baudrate < baudrate)
1121 ? (actual_baudrate * 21 < baudrate * 20)
1122 : (baudrate * 21 < actual_baudrate * 20)))
1123 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1125 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1126 SIO_SET_BAUDRATE_REQUEST, value,
1127 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1128 ftdi_error_return (-2, "Setting new baudrate failed");
1130 ftdi->baudrate = baudrate;
1135 Set (RS232) line characteristics.
1136 The break type can only be set via ftdi_set_line_property2()
1137 and defaults to "off".
1139 \param ftdi pointer to ftdi_context
1140 \param bits Number of bits
1141 \param sbit Number of stop bits
1142 \param parity Parity mode
1145 \retval -1: Setting line property failed
1147 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1148 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1150 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1154 Set (RS232) line characteristics
1156 \param ftdi pointer to ftdi_context
1157 \param bits Number of bits
1158 \param sbit Number of stop bits
1159 \param parity Parity mode
1160 \param break_type Break type
1163 \retval -1: Setting line property failed
1164 \retval -2: USB device unavailable
1166 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1167 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1168 enum ftdi_break_type break_type)
1170 unsigned short value = bits;
1172 if (ftdi == NULL || ftdi->usb_dev == NULL)
1173 ftdi_error_return(-2, "USB device unavailable");
1178 value |= (0x00 << 8);
1181 value |= (0x01 << 8);
1184 value |= (0x02 << 8);
1187 value |= (0x03 << 8);
1190 value |= (0x04 << 8);
1197 value |= (0x00 << 11);
1200 value |= (0x01 << 11);
1203 value |= (0x02 << 11);
1210 value |= (0x00 << 14);
1213 value |= (0x01 << 14);
1217 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1218 SIO_SET_DATA_REQUEST, value,
1219 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1220 ftdi_error_return (-1, "Setting new line property failed");
1226 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1228 \param ftdi pointer to ftdi_context
1229 \param buf Buffer with the data
1230 \param size Size of the buffer
1232 \retval -666: USB device unavailable
1233 \retval <0: error code from usb_bulk_write()
1234 \retval >0: number of bytes written
1236 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1241 if (ftdi == NULL || ftdi->usb_dev == NULL)
1242 ftdi_error_return(-666, "USB device unavailable");
1244 while (offset < size)
1246 int write_size = ftdi->writebuffer_chunksize;
1248 if (offset+write_size > size)
1249 write_size = size-offset;
1251 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1252 ftdi_error_return(-1, "usb bulk write failed");
1254 offset += actual_length;
1260 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1262 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1263 struct ftdi_context *ftdi = tc->ftdi;
1264 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1266 packet_size = ftdi->max_packet_size;
1268 actual_length = transfer->actual_length;
1270 if (actual_length > 2)
1272 // skip FTDI status bytes.
1273 // Maybe stored in the future to enable modem use
1274 num_of_chunks = actual_length / packet_size;
1275 chunk_remains = actual_length % packet_size;
1276 //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);
1278 ftdi->readbuffer_offset += 2;
1281 if (actual_length > packet_size - 2)
1283 for (i = 1; i < num_of_chunks; i++)
1284 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1285 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1287 if (chunk_remains > 2)
1289 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1290 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1292 actual_length -= 2*num_of_chunks;
1295 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1298 if (actual_length > 0)
1300 // data still fits in buf?
1301 if (tc->offset + actual_length <= tc->size)
1303 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1304 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1305 tc->offset += actual_length;
1307 ftdi->readbuffer_offset = 0;
1308 ftdi->readbuffer_remaining = 0;
1310 /* Did we read exactly the right amount of bytes? */
1311 if (tc->offset == tc->size)
1313 //printf("read_data exact rem %d offset %d\n",
1314 //ftdi->readbuffer_remaining, offset);
1321 // only copy part of the data or size <= readbuffer_chunksize
1322 int part_size = tc->size - tc->offset;
1323 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1324 tc->offset += part_size;
1326 ftdi->readbuffer_offset += part_size;
1327 ftdi->readbuffer_remaining = actual_length - part_size;
1329 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1330 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1336 ret = libusb_submit_transfer (transfer);
1342 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1344 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1345 struct ftdi_context *ftdi = tc->ftdi;
1347 tc->offset += transfer->actual_length;
1349 if (tc->offset == tc->size)
1355 int write_size = ftdi->writebuffer_chunksize;
1358 if (tc->offset + write_size > tc->size)
1359 write_size = tc->size - tc->offset;
1361 transfer->length = write_size;
1362 transfer->buffer = tc->buf + tc->offset;
1363 ret = libusb_submit_transfer (transfer);
1371 Writes data to the chip. Does not wait for completion of the transfer
1372 nor does it make sure that the transfer was successful.
1374 Use libusb 1.0 asynchronous API.
1376 \param ftdi pointer to ftdi_context
1377 \param buf Buffer with the data
1378 \param size Size of the buffer
1380 \retval NULL: Some error happens when submit transfer
1381 \retval !NULL: Pointer to a ftdi_transfer_control
1384 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1386 struct ftdi_transfer_control *tc;
1387 struct libusb_transfer *transfer;
1388 int write_size, ret;
1390 if (ftdi == NULL || ftdi->usb_dev == NULL)
1393 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1397 transfer = libusb_alloc_transfer(0);
1410 if (size < ftdi->writebuffer_chunksize)
1413 write_size = ftdi->writebuffer_chunksize;
1415 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1416 write_size, ftdi_write_data_cb, tc,
1417 ftdi->usb_write_timeout);
1418 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1420 ret = libusb_submit_transfer(transfer);
1423 libusb_free_transfer(transfer);
1427 tc->transfer = transfer;
1433 Reads data from the chip. Does not wait for completion of the transfer
1434 nor does it make sure that the transfer was successful.
1436 Use libusb 1.0 asynchronous API.
1438 \param ftdi pointer to ftdi_context
1439 \param buf Buffer with the data
1440 \param size Size of the buffer
1442 \retval NULL: Some error happens when submit transfer
1443 \retval !NULL: Pointer to a ftdi_transfer_control
1446 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1448 struct ftdi_transfer_control *tc;
1449 struct libusb_transfer *transfer;
1452 if (ftdi == NULL || ftdi->usb_dev == NULL)
1455 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1463 if (size <= ftdi->readbuffer_remaining)
1465 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1468 ftdi->readbuffer_remaining -= size;
1469 ftdi->readbuffer_offset += size;
1471 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1475 tc->transfer = NULL;
1480 if (ftdi->readbuffer_remaining != 0)
1482 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1484 tc->offset = ftdi->readbuffer_remaining;
1489 transfer = libusb_alloc_transfer(0);
1496 ftdi->readbuffer_remaining = 0;
1497 ftdi->readbuffer_offset = 0;
1499 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);
1500 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1502 ret = libusb_submit_transfer(transfer);
1505 libusb_free_transfer(transfer);
1509 tc->transfer = transfer;
1515 Wait for completion of the transfer.
1517 Use libusb 1.0 asynchronous API.
1519 \param tc pointer to ftdi_transfer_control
1521 \retval < 0: Some error happens
1522 \retval >= 0: Data size transferred
1525 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1529 while (!tc->completed)
1531 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1534 if (ret == LIBUSB_ERROR_INTERRUPTED)
1536 libusb_cancel_transfer(tc->transfer);
1537 while (!tc->completed)
1538 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1540 libusb_free_transfer(tc->transfer);
1548 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1549 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1553 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1555 libusb_free_transfer(tc->transfer);
1562 Configure write buffer chunk size.
1565 \param ftdi pointer to ftdi_context
1566 \param chunksize Chunk size
1569 \retval -1: ftdi context invalid
1571 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1574 ftdi_error_return(-1, "ftdi context invalid");
1576 ftdi->writebuffer_chunksize = chunksize;
1581 Get write buffer chunk size.
1583 \param ftdi pointer to ftdi_context
1584 \param chunksize Pointer to store chunk size in
1587 \retval -1: ftdi context invalid
1589 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1592 ftdi_error_return(-1, "ftdi context invalid");
1594 *chunksize = ftdi->writebuffer_chunksize;
1599 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1601 Automatically strips the two modem status bytes transfered during every read.
1603 \param ftdi pointer to ftdi_context
1604 \param buf Buffer to store data in
1605 \param size Size of the buffer
1607 \retval -666: USB device unavailable
1608 \retval <0: error code from libusb_bulk_transfer()
1609 \retval 0: no data was available
1610 \retval >0: number of bytes read
1613 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1615 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1616 int packet_size = ftdi->max_packet_size;
1617 int actual_length = 1;
1619 if (ftdi == NULL || ftdi->usb_dev == NULL)
1620 ftdi_error_return(-666, "USB device unavailable");
1622 // Packet size sanity check (avoid division by zero)
1623 if (packet_size == 0)
1624 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1626 // everything we want is still in the readbuffer?
1627 if (size <= ftdi->readbuffer_remaining)
1629 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1632 ftdi->readbuffer_remaining -= size;
1633 ftdi->readbuffer_offset += size;
1635 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1639 // something still in the readbuffer, but not enough to satisfy 'size'?
1640 if (ftdi->readbuffer_remaining != 0)
1642 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1645 offset += ftdi->readbuffer_remaining;
1647 // do the actual USB read
1648 while (offset < size && actual_length > 0)
1650 ftdi->readbuffer_remaining = 0;
1651 ftdi->readbuffer_offset = 0;
1652 /* returns how much received */
1653 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1655 ftdi_error_return(ret, "usb bulk read failed");
1657 if (actual_length > 2)
1659 // skip FTDI status bytes.
1660 // Maybe stored in the future to enable modem use
1661 num_of_chunks = actual_length / packet_size;
1662 chunk_remains = actual_length % packet_size;
1663 //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);
1665 ftdi->readbuffer_offset += 2;
1668 if (actual_length > packet_size - 2)
1670 for (i = 1; i < num_of_chunks; i++)
1671 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1672 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1674 if (chunk_remains > 2)
1676 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1677 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1679 actual_length -= 2*num_of_chunks;
1682 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1685 else if (actual_length <= 2)
1687 // no more data to read?
1690 if (actual_length > 0)
1692 // data still fits in buf?
1693 if (offset+actual_length <= size)
1695 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1696 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1697 offset += actual_length;
1699 /* Did we read exactly the right amount of bytes? */
1701 //printf("read_data exact rem %d offset %d\n",
1702 //ftdi->readbuffer_remaining, offset);
1707 // only copy part of the data or size <= readbuffer_chunksize
1708 int part_size = size-offset;
1709 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1711 ftdi->readbuffer_offset += part_size;
1712 ftdi->readbuffer_remaining = actual_length-part_size;
1713 offset += part_size;
1715 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1716 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1727 Configure read buffer chunk size.
1730 Automatically reallocates the buffer.
1732 \param ftdi pointer to ftdi_context
1733 \param chunksize Chunk size
1736 \retval -1: ftdi context invalid
1738 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1740 unsigned char *new_buf;
1743 ftdi_error_return(-1, "ftdi context invalid");
1745 // Invalidate all remaining data
1746 ftdi->readbuffer_offset = 0;
1747 ftdi->readbuffer_remaining = 0;
1749 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1750 which is defined in libusb-1.0. Otherwise, each USB read request will
1751 be divided into multiple URBs. This will cause issues on Linux kernel
1752 older than 2.6.32. */
1753 if (chunksize > 16384)
1757 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1758 ftdi_error_return(-1, "out of memory for readbuffer");
1760 ftdi->readbuffer = new_buf;
1761 ftdi->readbuffer_chunksize = chunksize;
1767 Get read buffer chunk size.
1769 \param ftdi pointer to ftdi_context
1770 \param chunksize Pointer to store chunk size in
1773 \retval -1: FTDI context invalid
1775 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1778 ftdi_error_return(-1, "FTDI context invalid");
1780 *chunksize = ftdi->readbuffer_chunksize;
1786 Enable bitbang mode.
1788 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1790 \param ftdi pointer to ftdi_context
1791 \param bitmask Bitmask to configure lines.
1792 HIGH/ON value configures a line as output.
1795 \retval -1: can't enable bitbang mode
1796 \retval -2: USB device unavailable
1798 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1800 unsigned short usb_val;
1802 if (ftdi == NULL || ftdi->usb_dev == NULL)
1803 ftdi_error_return(-2, "USB device unavailable");
1805 usb_val = bitmask; // low byte: bitmask
1806 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1807 usb_val |= (ftdi->bitbang_mode << 8);
1809 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1810 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1811 NULL, 0, ftdi->usb_write_timeout) < 0)
1812 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1814 ftdi->bitbang_enabled = 1;
1819 Disable bitbang mode.
1821 \param ftdi pointer to ftdi_context
1824 \retval -1: can't disable bitbang mode
1825 \retval -2: USB device unavailable
1827 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1829 if (ftdi == NULL || ftdi->usb_dev == NULL)
1830 ftdi_error_return(-2, "USB device unavailable");
1832 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)
1833 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1835 ftdi->bitbang_enabled = 0;
1840 Enable/disable bitbang modes.
1842 \param ftdi pointer to ftdi_context
1843 \param bitmask Bitmask to configure lines.
1844 HIGH/ON value configures a line as output.
1845 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1848 \retval -1: can't enable bitbang mode
1849 \retval -2: USB device unavailable
1851 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1853 unsigned short usb_val;
1855 if (ftdi == NULL || ftdi->usb_dev == NULL)
1856 ftdi_error_return(-2, "USB device unavailable");
1858 usb_val = bitmask; // low byte: bitmask
1859 usb_val |= (mode << 8);
1860 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)
1861 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1863 ftdi->bitbang_mode = mode;
1864 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1869 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1871 \param ftdi pointer to ftdi_context
1872 \param pins Pointer to store pins into
1875 \retval -1: read pins failed
1876 \retval -2: USB device unavailable
1878 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1880 if (ftdi == NULL || ftdi->usb_dev == NULL)
1881 ftdi_error_return(-2, "USB device unavailable");
1883 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)
1884 ftdi_error_return(-1, "read pins failed");
1892 The FTDI chip keeps data in the internal buffer for a specific
1893 amount of time if the buffer is not full yet to decrease
1894 load on the usb bus.
1896 \param ftdi pointer to ftdi_context
1897 \param latency Value between 1 and 255
1900 \retval -1: latency out of range
1901 \retval -2: unable to set latency timer
1902 \retval -3: USB device unavailable
1904 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1906 unsigned short usb_val;
1909 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1911 if (ftdi == NULL || ftdi->usb_dev == NULL)
1912 ftdi_error_return(-3, "USB device unavailable");
1915 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)
1916 ftdi_error_return(-2, "unable to set latency timer");
1924 \param ftdi pointer to ftdi_context
1925 \param latency Pointer to store latency value in
1928 \retval -1: unable to get latency timer
1929 \retval -2: USB device unavailable
1931 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1933 unsigned short usb_val;
1935 if (ftdi == NULL || ftdi->usb_dev == NULL)
1936 ftdi_error_return(-2, "USB device unavailable");
1938 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)
1939 ftdi_error_return(-1, "reading latency timer failed");
1941 *latency = (unsigned char)usb_val;
1946 Poll modem status information
1948 This function allows the retrieve the two status bytes of the device.
1949 The device sends these bytes also as a header for each read access
1950 where they are discarded by ftdi_read_data(). The chip generates
1951 the two stripped status bytes in the absence of data every 40 ms.
1953 Layout of the first byte:
1954 - B0..B3 - must be 0
1955 - B4 Clear to send (CTS)
1958 - B5 Data set ready (DTS)
1961 - B6 Ring indicator (RI)
1964 - B7 Receive line signal detect (RLSD)
1968 Layout of the second byte:
1969 - B0 Data ready (DR)
1970 - B1 Overrun error (OE)
1971 - B2 Parity error (PE)
1972 - B3 Framing error (FE)
1973 - B4 Break interrupt (BI)
1974 - B5 Transmitter holding register (THRE)
1975 - B6 Transmitter empty (TEMT)
1976 - B7 Error in RCVR FIFO
1978 \param ftdi pointer to ftdi_context
1979 \param status Pointer to store status information in. Must be two bytes.
1982 \retval -1: unable to retrieve status information
1983 \retval -2: USB device unavailable
1985 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1989 if (ftdi == NULL || ftdi->usb_dev == NULL)
1990 ftdi_error_return(-2, "USB device unavailable");
1992 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)
1993 ftdi_error_return(-1, "getting modem status failed");
1995 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2001 Set flowcontrol for ftdi chip
2003 \param ftdi pointer to ftdi_context
2004 \param flowctrl flow control to use. should be
2005 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2008 \retval -1: set flow control failed
2009 \retval -2: USB device unavailable
2011 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2013 if (ftdi == NULL || ftdi->usb_dev == NULL)
2014 ftdi_error_return(-2, "USB device unavailable");
2016 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2017 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2018 NULL, 0, ftdi->usb_write_timeout) < 0)
2019 ftdi_error_return(-1, "set flow control failed");
2027 \param ftdi pointer to ftdi_context
2028 \param state state to set line to (1 or 0)
2031 \retval -1: set dtr failed
2032 \retval -2: USB device unavailable
2034 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2036 unsigned short usb_val;
2038 if (ftdi == NULL || ftdi->usb_dev == NULL)
2039 ftdi_error_return(-2, "USB device unavailable");
2042 usb_val = SIO_SET_DTR_HIGH;
2044 usb_val = SIO_SET_DTR_LOW;
2046 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2047 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2048 NULL, 0, ftdi->usb_write_timeout) < 0)
2049 ftdi_error_return(-1, "set dtr failed");
2057 \param ftdi pointer to ftdi_context
2058 \param state state to set line to (1 or 0)
2061 \retval -1: set rts failed
2062 \retval -2: USB device unavailable
2064 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2066 unsigned short usb_val;
2068 if (ftdi == NULL || ftdi->usb_dev == NULL)
2069 ftdi_error_return(-2, "USB device unavailable");
2072 usb_val = SIO_SET_RTS_HIGH;
2074 usb_val = SIO_SET_RTS_LOW;
2076 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2077 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2078 NULL, 0, ftdi->usb_write_timeout) < 0)
2079 ftdi_error_return(-1, "set of rts failed");
2085 Set dtr and rts line in one pass
2087 \param ftdi pointer to ftdi_context
2088 \param dtr DTR state to set line to (1 or 0)
2089 \param rts RTS state to set line to (1 or 0)
2092 \retval -1: set dtr/rts failed
2093 \retval -2: USB device unavailable
2095 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2097 unsigned short usb_val;
2099 if (ftdi == NULL || ftdi->usb_dev == NULL)
2100 ftdi_error_return(-2, "USB device unavailable");
2103 usb_val = SIO_SET_DTR_HIGH;
2105 usb_val = SIO_SET_DTR_LOW;
2108 usb_val |= SIO_SET_RTS_HIGH;
2110 usb_val |= SIO_SET_RTS_LOW;
2112 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2113 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2114 NULL, 0, ftdi->usb_write_timeout) < 0)
2115 ftdi_error_return(-1, "set of rts/dtr failed");
2121 Set the special event character
2123 \param ftdi pointer to ftdi_context
2124 \param eventch Event character
2125 \param enable 0 to disable the event character, non-zero otherwise
2128 \retval -1: unable to set event character
2129 \retval -2: USB device unavailable
2131 int ftdi_set_event_char(struct ftdi_context *ftdi,
2132 unsigned char eventch, unsigned char enable)
2134 unsigned short usb_val;
2136 if (ftdi == NULL || ftdi->usb_dev == NULL)
2137 ftdi_error_return(-2, "USB device unavailable");
2143 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)
2144 ftdi_error_return(-1, "setting event character failed");
2152 \param ftdi pointer to ftdi_context
2153 \param errorch Error character
2154 \param enable 0 to disable the error character, non-zero otherwise
2157 \retval -1: unable to set error character
2158 \retval -2: USB device unavailable
2160 int ftdi_set_error_char(struct ftdi_context *ftdi,
2161 unsigned char errorch, unsigned char enable)
2163 unsigned short usb_val;
2165 if (ftdi == NULL || ftdi->usb_dev == NULL)
2166 ftdi_error_return(-2, "USB device unavailable");
2172 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)
2173 ftdi_error_return(-1, "setting error character failed");
2179 Init eeprom with default values.
2180 \param ftdi pointer to ftdi_context
2181 \param manufacturer String to use as Manufacturer
2182 \param product String to use as Product description
2183 \param serial String to use as Serial number description
2186 \retval -1: No struct ftdi_context
2187 \retval -2: No struct ftdi_eeprom
2189 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2190 char * product, char * serial)
2192 struct ftdi_eeprom *eeprom;
2195 ftdi_error_return(-1, "No struct ftdi_context");
2197 if (ftdi->eeprom == NULL)
2198 ftdi_error_return(-2,"No struct ftdi_eeprom");
2200 eeprom = ftdi->eeprom;
2201 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2203 eeprom->vendor_id = 0x0403;
2204 eeprom->use_serial = USE_SERIAL_NUM;
2205 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2206 (ftdi->type == TYPE_R))
2207 eeprom->product_id = 0x6001;
2209 eeprom->product_id = 0x6010;
2210 if (ftdi->type == TYPE_AM)
2211 eeprom->usb_version = 0x0101;
2213 eeprom->usb_version = 0x0200;
2214 eeprom->max_power = 100;
2216 if (eeprom->manufacturer)
2217 free (eeprom->manufacturer);
2218 eeprom->manufacturer = NULL;
2221 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2222 if (eeprom->manufacturer)
2223 strcpy(eeprom->manufacturer, manufacturer);
2226 if (eeprom->product)
2227 free (eeprom->product);
2228 eeprom->product = NULL;
2231 eeprom->product = malloc(strlen(product)+1);
2232 if (eeprom->product)
2233 strcpy(eeprom->product, product);
2237 free (eeprom->serial);
2238 eeprom->serial = NULL;
2241 eeprom->serial = malloc(strlen(serial)+1);
2243 strcpy(eeprom->serial, serial);
2247 if (ftdi->type == TYPE_R)
2249 eeprom->max_power = 90;
2250 eeprom->size = 0x80;
2251 eeprom->cbus_function[0] = CBUS_TXLED;
2252 eeprom->cbus_function[1] = CBUS_RXLED;
2253 eeprom->cbus_function[2] = CBUS_TXDEN;
2254 eeprom->cbus_function[3] = CBUS_PWREN;
2255 eeprom->cbus_function[4] = CBUS_SLEEP;
2263 Build binary buffer from ftdi_eeprom structure.
2264 Output is suitable for ftdi_write_eeprom().
2266 \param ftdi pointer to ftdi_context
2268 \retval >=0: size of eeprom user area in bytes
2269 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2270 \retval -2: Invalid eeprom or ftdi pointer
2271 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2272 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2273 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2274 \retval -6: No connected EEPROM or EEPROM Type unknown
2276 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2278 unsigned char i, j, eeprom_size_mask;
2279 unsigned short checksum, value;
2280 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2282 struct ftdi_eeprom *eeprom;
2283 unsigned char * output;
2286 ftdi_error_return(-2,"No context");
2287 if (ftdi->eeprom == NULL)
2288 ftdi_error_return(-2,"No eeprom structure");
2290 eeprom= ftdi->eeprom;
2291 output = eeprom->buf;
2293 if (eeprom->chip == -1)
2294 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2296 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2297 eeprom->size = 0x100;
2299 eeprom->size = 0x80;
2301 if (eeprom->manufacturer != NULL)
2302 manufacturer_size = strlen(eeprom->manufacturer);
2303 if (eeprom->product != NULL)
2304 product_size = strlen(eeprom->product);
2305 if (eeprom->serial != NULL)
2306 serial_size = strlen(eeprom->serial);
2308 // eeprom size check
2313 user_area_size = 96; // base size for strings (total of 48 characters)
2316 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2319 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2321 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2323 user_area_size = 86;
2329 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2331 if (user_area_size < 0)
2332 ftdi_error_return(-1,"eeprom size exceeded");
2335 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2337 // Bytes and Bits set for all Types
2339 // Addr 02: Vendor ID
2340 output[0x02] = eeprom->vendor_id;
2341 output[0x03] = eeprom->vendor_id >> 8;
2343 // Addr 04: Product ID
2344 output[0x04] = eeprom->product_id;
2345 output[0x05] = eeprom->product_id >> 8;
2347 // Addr 06: Device release number (0400h for BM features)
2348 output[0x06] = 0x00;
2352 output[0x07] = 0x02;
2355 output[0x07] = 0x04;
2358 output[0x07] = 0x05;
2361 output[0x07] = 0x06;
2364 output[0x07] = 0x07;
2367 output[0x07] = 0x08;
2370 output[0x07] = 0x00;
2373 // Addr 08: Config descriptor
2375 // Bit 6: 1 if this device is self powered, 0 if bus powered
2376 // Bit 5: 1 if this device uses remote wakeup
2377 // Bit 4-0: reserved - 0
2379 if (eeprom->self_powered == 1)
2381 if (eeprom->remote_wakeup == 1)
2385 // Addr 09: Max power consumption: max power = value * 2 mA
2386 output[0x09] = eeprom->max_power>>1;
2388 if (ftdi->type != TYPE_AM)
2390 // Addr 0A: Chip configuration
2391 // Bit 7: 0 - reserved
2392 // Bit 6: 0 - reserved
2393 // Bit 5: 0 - reserved
2394 // Bit 4: 1 - Change USB version
2395 // Bit 3: 1 - Use the serial number string
2396 // Bit 2: 1 - Enable suspend pull downs for lower power
2397 // Bit 1: 1 - Out EndPoint is Isochronous
2398 // Bit 0: 1 - In EndPoint is Isochronous
2401 if (eeprom->in_is_isochronous == 1)
2403 if (eeprom->out_is_isochronous == 1)
2409 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2410 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2425 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2426 eeprom_size_mask = eeprom->size -1;
2428 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2429 // Addr 0F: Length of manufacturer string
2430 // Output manufacturer
2431 output[0x0E] = i; // calculate offset
2432 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2433 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2434 for (j = 0; j < manufacturer_size; j++)
2436 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2437 output[i & eeprom_size_mask] = 0x00, i++;
2439 output[0x0F] = manufacturer_size*2 + 2;
2441 // Addr 10: Offset of the product string + 0x80, calculated later
2442 // Addr 11: Length of product string
2443 output[0x10] = i | 0x80; // calculate offset
2444 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2445 output[i & eeprom_size_mask] = 0x03, i++;
2446 for (j = 0; j < product_size; j++)
2448 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2449 output[i & eeprom_size_mask] = 0x00, i++;
2451 output[0x11] = product_size*2 + 2;
2453 // Addr 12: Offset of the serial string + 0x80, calculated later
2454 // Addr 13: Length of serial string
2455 output[0x12] = i | 0x80; // calculate offset
2456 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2457 output[i & eeprom_size_mask] = 0x03, i++;
2458 for (j = 0; j < serial_size; j++)
2460 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2461 output[i & eeprom_size_mask] = 0x00, i++;
2464 // Legacy port name and PnP fields for FT2232 and newer chips
2465 if (ftdi->type > TYPE_BM)
2467 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2469 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2471 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2475 output[0x13] = serial_size*2 + 2;
2477 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2479 if (eeprom->use_serial == USE_SERIAL_NUM )
2480 output[0x0A] |= USE_SERIAL_NUM;
2482 output[0x0A] &= ~USE_SERIAL_NUM;
2485 /* Bytes and Bits specific to (some) types
2486 Write linear, as this allows easier fixing*/
2492 output[0x0C] = eeprom->usb_version & 0xff;
2493 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2494 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2495 output[0x0A] |= USE_USB_VERSION_BIT;
2497 output[0x0A] &= ~USE_USB_VERSION_BIT;
2502 output[0x00] = (eeprom->channel_a_type);
2503 if ( eeprom->channel_a_driver == DRIVER_VCP)
2504 output[0x00] |= DRIVER_VCP;
2506 output[0x00] &= ~DRIVER_VCP;
2508 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2509 output[0x00] |= HIGH_CURRENT_DRIVE;
2511 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2513 output[0x01] = (eeprom->channel_b_type);
2514 if ( eeprom->channel_b_driver == DRIVER_VCP)
2515 output[0x01] |= DRIVER_VCP;
2517 output[0x01] &= ~DRIVER_VCP;
2519 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2520 output[0x01] |= HIGH_CURRENT_DRIVE;
2522 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2524 if (eeprom->in_is_isochronous == 1)
2525 output[0x0A] |= 0x1;
2527 output[0x0A] &= ~0x1;
2528 if (eeprom->out_is_isochronous == 1)
2529 output[0x0A] |= 0x2;
2531 output[0x0A] &= ~0x2;
2532 if (eeprom->suspend_pull_downs == 1)
2533 output[0x0A] |= 0x4;
2535 output[0x0A] &= ~0x4;
2536 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2537 output[0x0A] |= USE_USB_VERSION_BIT;
2539 output[0x0A] &= ~USE_USB_VERSION_BIT;
2541 output[0x0C] = eeprom->usb_version & 0xff;
2542 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2543 output[0x14] = eeprom->chip;
2546 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2547 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2548 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2550 if (eeprom->suspend_pull_downs == 1)
2551 output[0x0A] |= 0x4;
2553 output[0x0A] &= ~0x4;
2554 output[0x0B] = eeprom->invert;
2555 output[0x0C] = eeprom->usb_version & 0xff;
2556 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2558 if (eeprom->cbus_function[0] > CBUS_BB)
2559 output[0x14] = CBUS_TXLED;
2561 output[0x14] = eeprom->cbus_function[0];
2563 if (eeprom->cbus_function[1] > CBUS_BB)
2564 output[0x14] |= CBUS_RXLED<<4;
2566 output[0x14] |= eeprom->cbus_function[1]<<4;
2568 if (eeprom->cbus_function[2] > CBUS_BB)
2569 output[0x15] = CBUS_TXDEN;
2571 output[0x15] = eeprom->cbus_function[2];
2573 if (eeprom->cbus_function[3] > CBUS_BB)
2574 output[0x15] |= CBUS_PWREN<<4;
2576 output[0x15] |= eeprom->cbus_function[3]<<4;
2578 if (eeprom->cbus_function[4] > CBUS_CLK6)
2579 output[0x16] = CBUS_SLEEP;
2581 output[0x16] = eeprom->cbus_function[4];
2584 output[0x00] = (eeprom->channel_a_type);
2585 if ( eeprom->channel_a_driver == DRIVER_VCP)
2586 output[0x00] |= DRIVER_VCP;
2588 output[0x00] &= ~DRIVER_VCP;
2590 output[0x01] = (eeprom->channel_b_type);
2591 if ( eeprom->channel_b_driver == DRIVER_VCP)
2592 output[0x01] |= DRIVER_VCP;
2594 output[0x01] &= ~DRIVER_VCP;
2595 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2596 output[0x01] |= SUSPEND_DBUS7_BIT;
2598 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2600 if (eeprom->suspend_pull_downs == 1)
2601 output[0x0A] |= 0x4;
2603 output[0x0A] &= ~0x4;
2605 if (eeprom->group0_drive > DRIVE_16MA)
2606 output[0x0c] |= DRIVE_16MA;
2608 output[0x0c] |= eeprom->group0_drive;
2609 if (eeprom->group0_schmitt == IS_SCHMITT)
2610 output[0x0c] |= IS_SCHMITT;
2611 if (eeprom->group0_slew == SLOW_SLEW)
2612 output[0x0c] |= SLOW_SLEW;
2614 if (eeprom->group1_drive > DRIVE_16MA)
2615 output[0x0c] |= DRIVE_16MA<<4;
2617 output[0x0c] |= eeprom->group1_drive<<4;
2618 if (eeprom->group1_schmitt == IS_SCHMITT)
2619 output[0x0c] |= IS_SCHMITT<<4;
2620 if (eeprom->group1_slew == SLOW_SLEW)
2621 output[0x0c] |= SLOW_SLEW<<4;
2623 if (eeprom->group2_drive > DRIVE_16MA)
2624 output[0x0d] |= DRIVE_16MA;
2626 output[0x0d] |= eeprom->group2_drive;
2627 if (eeprom->group2_schmitt == IS_SCHMITT)
2628 output[0x0d] |= IS_SCHMITT;
2629 if (eeprom->group2_slew == SLOW_SLEW)
2630 output[0x0d] |= SLOW_SLEW;
2632 if (eeprom->group3_drive > DRIVE_16MA)
2633 output[0x0d] |= DRIVE_16MA<<4;
2635 output[0x0d] |= eeprom->group3_drive<<4;
2636 if (eeprom->group3_schmitt == IS_SCHMITT)
2637 output[0x0d] |= IS_SCHMITT<<4;
2638 if (eeprom->group3_slew == SLOW_SLEW)
2639 output[0x0d] |= SLOW_SLEW<<4;
2641 output[0x18] = eeprom->chip;
2645 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2648 // calculate checksum
2651 for (i = 0; i < eeprom->size/2-1; i++)
2653 value = output[i*2];
2654 value += output[(i*2)+1] << 8;
2656 checksum = value^checksum;
2657 checksum = (checksum << 1) | (checksum >> 15);
2660 output[eeprom->size-2] = checksum;
2661 output[eeprom->size-1] = checksum >> 8;
2663 return user_area_size;
2667 Decode binary EEPROM image into an ftdi_eeprom structure.
2669 \param ftdi pointer to ftdi_context
2670 \param verbose Decode EEPROM on stdout
2673 \retval -1: something went wrong
2675 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2676 FIXME: Strings are malloc'ed here and should be freed somewhere
2678 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2681 unsigned short checksum, eeprom_checksum, value;
2682 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2684 struct ftdi_eeprom *eeprom;
2685 unsigned char *buf = ftdi->eeprom->buf;
2689 ftdi_error_return(-1,"No context");
2690 if (ftdi->eeprom == NULL)
2691 ftdi_error_return(-1,"No eeprom structure");
2693 eeprom = ftdi->eeprom;
2694 eeprom_size = eeprom->size;
2696 // Addr 02: Vendor ID
2697 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2699 // Addr 04: Product ID
2700 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2702 release = buf[0x06] + (buf[0x07]<<8);
2704 // Addr 08: Config descriptor
2706 // Bit 6: 1 if this device is self powered, 0 if bus powered
2707 // Bit 5: 1 if this device uses remote wakeup
2708 eeprom->self_powered = buf[0x08] & 0x40;
2709 eeprom->remote_wakeup = buf[0x08] & 0x20;
2711 // Addr 09: Max power consumption: max power = value * 2 mA
2712 eeprom->max_power = buf[0x09];
2714 // Addr 0A: Chip configuration
2715 // Bit 7: 0 - reserved
2716 // Bit 6: 0 - reserved
2717 // Bit 5: 0 - reserved
2718 // Bit 4: 1 - Change USB version on BM and 2232C
2719 // Bit 3: 1 - Use the serial number string
2720 // Bit 2: 1 - Enable suspend pull downs for lower power
2721 // Bit 1: 1 - Out EndPoint is Isochronous
2722 // Bit 0: 1 - In EndPoint is Isochronous
2724 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2725 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2726 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2727 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2728 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2730 // Addr 0C: USB version low byte when 0x0A
2731 // Addr 0D: USB version high byte when 0x0A
2732 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2734 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2735 // Addr 0F: Length of manufacturer string
2736 manufacturer_size = buf[0x0F]/2;
2737 if (eeprom->manufacturer)
2738 free(eeprom->manufacturer);
2739 if (manufacturer_size > 0)
2741 eeprom->manufacturer = malloc(manufacturer_size);
2742 if (eeprom->manufacturer)
2744 // Decode manufacturer
2745 i = buf[0x0E] & (eeprom_size -1); // offset
2746 for (j=0;j<manufacturer_size-1;j++)
2748 eeprom->manufacturer[j] = buf[2*j+i+2];
2750 eeprom->manufacturer[j] = '\0';
2753 else eeprom->manufacturer = NULL;
2755 // Addr 10: Offset of the product string + 0x80, calculated later
2756 // Addr 11: Length of product string
2757 if (eeprom->product)
2758 free(eeprom->product);
2759 product_size = buf[0x11]/2;
2760 if (product_size > 0)
2762 eeprom->product = malloc(product_size);
2763 if (eeprom->product)
2765 // Decode product name
2766 i = buf[0x10] & (eeprom_size -1); // offset
2767 for (j=0;j<product_size-1;j++)
2769 eeprom->product[j] = buf[2*j+i+2];
2771 eeprom->product[j] = '\0';
2774 else eeprom->product = NULL;
2776 // Addr 12: Offset of the serial string + 0x80, calculated later
2777 // Addr 13: Length of serial string
2779 free(eeprom->serial);
2780 serial_size = buf[0x13]/2;
2781 if (serial_size > 0)
2783 eeprom->serial = malloc(serial_size);
2787 i = buf[0x12] & (eeprom_size -1); // offset
2788 for (j=0;j<serial_size-1;j++)
2790 eeprom->serial[j] = buf[2*j+i+2];
2792 eeprom->serial[j] = '\0';
2795 else eeprom->serial = NULL;
2800 for (i = 0; i < eeprom_size/2-1; i++)
2803 value += buf[(i*2)+1] << 8;
2805 checksum = value^checksum;
2806 checksum = (checksum << 1) | (checksum >> 15);
2809 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2811 if (eeprom_checksum != checksum)
2813 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2814 ftdi_error_return(-1,"EEPROM checksum error");
2817 eeprom->channel_a_type = 0;
2818 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2822 else if (ftdi->type == TYPE_2232C)
2824 eeprom->channel_a_type = buf[0x00] & 0x7;
2825 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2826 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2827 eeprom->channel_b_type = buf[0x01] & 0x7;
2828 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2829 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2830 eeprom->chip = buf[0x14];
2832 else if (ftdi->type == TYPE_R)
2834 /* TYPE_R flags D2XX, not VCP as all others*/
2835 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2836 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2837 if ( (buf[0x01]&0x40) != 0x40)
2839 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2840 " If this happened with the\n"
2841 " EEPROM programmed by FTDI tools, please report "
2842 "to libftdi@developer.intra2net.com\n");
2844 eeprom->chip = buf[0x16];
2845 // Addr 0B: Invert data lines
2846 // Works only on FT232R, not FT245R, but no way to distinguish
2847 eeprom->invert = buf[0x0B];
2848 // Addr 14: CBUS function: CBUS0, CBUS1
2849 // Addr 15: CBUS function: CBUS2, CBUS3
2850 // Addr 16: CBUS function: CBUS5
2851 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2852 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2853 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2854 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2855 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2857 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2859 eeprom->channel_a_type = buf[0x00] & 0x7;
2860 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2861 eeprom->channel_b_type = buf[0x01] & 0x7;
2862 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2864 if (ftdi->type == TYPE_2232H)
2865 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2867 eeprom->chip = buf[0x18];
2868 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2869 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2870 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2871 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2872 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2873 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2874 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2875 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2876 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2877 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2878 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2879 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2884 char *channel_mode[] = {"UART","245","CPU", "unknown", "OPTO"};
2885 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
2886 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
2887 fprintf(stdout, "Release: 0x%04x\n",release);
2889 if (eeprom->self_powered)
2890 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
2892 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
2893 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
2894 if (eeprom->manufacturer)
2895 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
2896 if (eeprom->product)
2897 fprintf(stdout, "Product: %s\n",eeprom->product);
2899 fprintf(stdout, "Serial: %s\n",eeprom->serial);
2900 fprintf(stdout, "Checksum : %04x\n", checksum);
2901 if (ftdi->type == TYPE_R)
2902 fprintf(stdout, "Internal EEPROM\n");
2903 else if (eeprom->chip >= 0x46)
2904 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
2905 if (eeprom->suspend_dbus7)
2906 fprintf(stdout, "Suspend on DBUS7\n");
2907 if (eeprom->suspend_pull_downs)
2908 fprintf(stdout, "Pull IO pins low during suspend\n");
2909 if (eeprom->remote_wakeup)
2910 fprintf(stdout, "Enable Remote Wake Up\n");
2911 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
2912 if (ftdi->type >= TYPE_2232C)
2913 fprintf(stdout,"Channel A has Mode %s%s%s\n",
2914 channel_mode[eeprom->channel_a_type],
2915 (eeprom->channel_a_driver)?" VCP":"",
2916 (eeprom->high_current_a)?" High Current IO":"");
2917 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R))
2918 fprintf(stdout,"Channel B has Mode %s%s%s\n",
2919 channel_mode[eeprom->channel_b_type],
2920 (eeprom->channel_b_driver)?" VCP":"",
2921 (eeprom->high_current_b)?" High Current IO":"");
2922 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
2923 eeprom->use_usb_version == USE_USB_VERSION_BIT)
2924 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
2926 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
2928 fprintf(stdout,"%s has %d mA drive%s%s\n",
2929 (ftdi->type == TYPE_2232H)?"AL":"A",
2930 (eeprom->group0_drive+1) *4,
2931 (eeprom->group0_schmitt)?" Schmitt Input":"",
2932 (eeprom->group0_slew)?" Slow Slew":"");
2933 fprintf(stdout,"%s has %d mA drive%s%s\n",
2934 (ftdi->type == TYPE_2232H)?"AH":"B",
2935 (eeprom->group1_drive+1) *4,
2936 (eeprom->group1_schmitt)?" Schmitt Input":"",
2937 (eeprom->group1_slew)?" Slow Slew":"");
2938 fprintf(stdout,"%s has %d mA drive%s%s\n",
2939 (ftdi->type == TYPE_2232H)?"BL":"C",
2940 (eeprom->group2_drive+1) *4,
2941 (eeprom->group2_schmitt)?" Schmitt Input":"",
2942 (eeprom->group2_slew)?" Slow Slew":"");
2943 fprintf(stdout,"%s has %d mA drive%s%s\n",
2944 (ftdi->type == TYPE_2232H)?"BH":"D",
2945 (eeprom->group3_drive+1) *4,
2946 (eeprom->group3_schmitt)?" Schmitt Input":"",
2947 (eeprom->group3_slew)?" Slow Slew":"");
2949 if (ftdi->type == TYPE_R)
2951 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
2952 "SLEEP","CLK48","CLK24","CLK12","CLK6",
2953 "IOMODE","BB_WR","BB_RD"
2955 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
2959 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
2960 fprintf(stdout,"Inverted bits:");
2962 if ((eeprom->invert & (1<<i)) == (1<<i))
2963 fprintf(stdout," %s",r_bits[i]);
2964 fprintf(stdout,"\n");
2968 if (eeprom->cbus_function[i]<CBUS_BB)
2969 fprintf(stdout,"C%d Function: %s\n", i,
2970 cbus_mux[eeprom->cbus_function[i]]);
2974 /* Running MPROG show that C0..3 have fixed function Synchronous
2976 fprintf(stdout,"C%d BB Function: %s\n", i,
2979 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
2988 Get a value from the decoded EEPROM structure
2990 \param ftdi pointer to ftdi_context
2991 \param value_name Enum of the value to query
2992 \param value Pointer to store read value
2995 \retval -1: Value doesn't exist
2997 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3002 *value = ftdi->eeprom->vendor_id;
3005 *value = ftdi->eeprom->product_id;
3008 *value = ftdi->eeprom->self_powered;
3011 *value = ftdi->eeprom->remote_wakeup;
3014 *value = ftdi->eeprom->is_not_pnp;
3017 *value = ftdi->eeprom->suspend_dbus7;
3019 case IN_IS_ISOCHRONOUS:
3020 *value = ftdi->eeprom->in_is_isochronous;
3022 case SUSPEND_PULL_DOWNS:
3023 *value = ftdi->eeprom->suspend_pull_downs;
3026 *value = ftdi->eeprom->use_serial;
3029 *value = ftdi->eeprom->usb_version;
3032 *value = ftdi->eeprom->max_power;
3034 case CHANNEL_A_TYPE:
3035 *value = ftdi->eeprom->channel_a_type;
3037 case CHANNEL_B_TYPE:
3038 *value = ftdi->eeprom->channel_b_type;
3040 case CHANNEL_A_DRIVER:
3041 *value = ftdi->eeprom->channel_a_driver;
3043 case CHANNEL_B_DRIVER:
3044 *value = ftdi->eeprom->channel_b_driver;
3046 case CBUS_FUNCTION_0:
3047 *value = ftdi->eeprom->cbus_function[0];
3049 case CBUS_FUNCTION_1:
3050 *value = ftdi->eeprom->cbus_function[1];
3052 case CBUS_FUNCTION_2:
3053 *value = ftdi->eeprom->cbus_function[2];
3055 case CBUS_FUNCTION_3:
3056 *value = ftdi->eeprom->cbus_function[3];
3058 case CBUS_FUNCTION_4:
3059 *value = ftdi->eeprom->cbus_function[4];
3062 *value = ftdi->eeprom->high_current;
3064 case HIGH_CURRENT_A:
3065 *value = ftdi->eeprom->high_current_a;
3067 case HIGH_CURRENT_B:
3068 *value = ftdi->eeprom->high_current_b;
3071 *value = ftdi->eeprom->invert;
3074 *value = ftdi->eeprom->group0_drive;
3076 case GROUP0_SCHMITT:
3077 *value = ftdi->eeprom->group0_schmitt;
3080 *value = ftdi->eeprom->group0_slew;
3083 *value = ftdi->eeprom->group1_drive;
3085 case GROUP1_SCHMITT:
3086 *value = ftdi->eeprom->group1_schmitt;
3089 *value = ftdi->eeprom->group1_slew;
3092 *value = ftdi->eeprom->group2_drive;
3094 case GROUP2_SCHMITT:
3095 *value = ftdi->eeprom->group2_schmitt;
3098 *value = ftdi->eeprom->group2_slew;
3101 *value = ftdi->eeprom->group3_drive;
3103 case GROUP3_SCHMITT:
3104 *value = ftdi->eeprom->group3_schmitt;
3107 *value = ftdi->eeprom->group3_slew;
3110 *value = ftdi->eeprom->chip;
3113 *value = ftdi->eeprom->size;
3116 ftdi_error_return(-1, "Request for unknown EEPROM value");
3122 Set a value in the decoded EEPROM Structure
3123 No parameter checking is performed
3125 \param ftdi pointer to ftdi_context
3126 \param value_name Enum of the value to set
3130 \retval -1: Value doesn't exist
3131 \retval -2: Value not user settable
3133 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3138 ftdi->eeprom->vendor_id = value;
3141 ftdi->eeprom->product_id = value;
3144 ftdi->eeprom->self_powered = value;
3147 ftdi->eeprom->remote_wakeup = value;
3150 ftdi->eeprom->is_not_pnp = value;
3153 ftdi->eeprom->suspend_dbus7 = value;
3155 case IN_IS_ISOCHRONOUS:
3156 ftdi->eeprom->in_is_isochronous = value;
3158 case SUSPEND_PULL_DOWNS:
3159 ftdi->eeprom->suspend_pull_downs = value;
3162 ftdi->eeprom->use_serial = value;
3165 ftdi->eeprom->usb_version = value;
3168 ftdi->eeprom->max_power = value;
3170 case CHANNEL_A_TYPE:
3171 ftdi->eeprom->channel_a_type = value;
3173 case CHANNEL_B_TYPE:
3174 ftdi->eeprom->channel_b_type = value;
3176 case CHANNEL_A_DRIVER:
3177 ftdi->eeprom->channel_a_driver = value;
3179 case CHANNEL_B_DRIVER:
3180 ftdi->eeprom->channel_b_driver = value;
3182 case CBUS_FUNCTION_0:
3183 ftdi->eeprom->cbus_function[0] = value;
3185 case CBUS_FUNCTION_1:
3186 ftdi->eeprom->cbus_function[1] = value;
3188 case CBUS_FUNCTION_2:
3189 ftdi->eeprom->cbus_function[2] = value;
3191 case CBUS_FUNCTION_3:
3192 ftdi->eeprom->cbus_function[3] = value;
3194 case CBUS_FUNCTION_4:
3195 ftdi->eeprom->cbus_function[4] = value;
3198 ftdi->eeprom->high_current = value;
3200 case HIGH_CURRENT_A:
3201 ftdi->eeprom->high_current_a = value;
3203 case HIGH_CURRENT_B:
3204 ftdi->eeprom->high_current_b = value;
3207 ftdi->eeprom->invert = value;
3210 ftdi->eeprom->group0_drive = value;
3212 case GROUP0_SCHMITT:
3213 ftdi->eeprom->group0_schmitt = value;
3216 ftdi->eeprom->group0_slew = value;
3219 ftdi->eeprom->group1_drive = value;
3221 case GROUP1_SCHMITT:
3222 ftdi->eeprom->group1_schmitt = value;
3225 ftdi->eeprom->group1_slew = value;
3228 ftdi->eeprom->group2_drive = value;
3230 case GROUP2_SCHMITT:
3231 ftdi->eeprom->group2_schmitt = value;
3234 ftdi->eeprom->group2_slew = value;
3237 ftdi->eeprom->group3_drive = value;
3239 case GROUP3_SCHMITT:
3240 ftdi->eeprom->group3_schmitt = value;
3243 ftdi->eeprom->group3_slew = value;
3246 ftdi->eeprom->chip = value;
3249 ftdi_error_return(-2, "EEPROM Value can't be changed");
3251 ftdi_error_return(-1, "Request to unknown EEPROM value");
3256 /** Get the read-only buffer to the binary EEPROM content
3258 \param ftdi pointer to ftdi_context
3259 \param buf buffer to receive EEPROM content
3260 \param size Size of receiving buffer
3263 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3264 \retval -2: Not enough room to store eeprom
3266 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3268 if (!ftdi || !(ftdi->eeprom))
3269 ftdi_error_return(-1, "No appropriate structure");
3271 if (!buf || size < ftdi->eeprom->size)
3272 ftdi_error_return(-1, "Not enough room to store eeprom");
3274 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3275 if (size > FTDI_MAX_EEPROM_SIZE)
3276 size = FTDI_MAX_EEPROM_SIZE;
3278 memcpy(buf, ftdi->eeprom->buf, size);
3284 Read eeprom location
3286 \param ftdi pointer to ftdi_context
3287 \param eeprom_addr Address of eeprom location to be read
3288 \param eeprom_val Pointer to store read eeprom location
3291 \retval -1: read failed
3292 \retval -2: USB device unavailable
3294 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3296 if (ftdi == NULL || ftdi->usb_dev == NULL)
3297 ftdi_error_return(-2, "USB device unavailable");
3299 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)
3300 ftdi_error_return(-1, "reading eeprom failed");
3308 \param ftdi pointer to ftdi_context
3311 \retval -1: read failed
3312 \retval -2: USB device unavailable
3314 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3319 if (ftdi == NULL || ftdi->usb_dev == NULL)
3320 ftdi_error_return(-2, "USB device unavailable");
3321 buf = ftdi->eeprom->buf;
3323 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3325 if (libusb_control_transfer(
3326 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3327 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3328 ftdi_error_return(-1, "reading eeprom failed");
3331 if (ftdi->type == TYPE_R)
3332 ftdi->eeprom->size = 0x80;
3333 /* Guesses size of eeprom by comparing halves
3334 - will not work with blank eeprom */
3335 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3336 ftdi->eeprom->size = -1;
3337 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3338 ftdi->eeprom->size = 0x80;
3339 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3340 ftdi->eeprom->size = 0x40;
3342 ftdi->eeprom->size = 0x100;
3347 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3348 Function is only used internally
3351 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3353 return ((value & 1) << 1) |
3354 ((value & 2) << 5) |
3355 ((value & 4) >> 2) |
3356 ((value & 8) << 4) |
3357 ((value & 16) >> 1) |
3358 ((value & 32) >> 1) |
3359 ((value & 64) >> 4) |
3360 ((value & 128) >> 2);
3364 Read the FTDIChip-ID from R-type devices
3366 \param ftdi pointer to ftdi_context
3367 \param chipid Pointer to store FTDIChip-ID
3370 \retval -1: read failed
3371 \retval -2: USB device unavailable
3373 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3375 unsigned int a = 0, b = 0;
3377 if (ftdi == NULL || ftdi->usb_dev == NULL)
3378 ftdi_error_return(-2, "USB device unavailable");
3380 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)
3382 a = a << 8 | a >> 8;
3383 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)
3385 b = b << 8 | b >> 8;
3386 a = (a << 16) | (b & 0xFFFF);
3387 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3388 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3389 *chipid = a ^ 0xa5f0f7d1;
3394 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3398 Write eeprom location
3400 \param ftdi pointer to ftdi_context
3401 \param eeprom_addr Address of eeprom location to be written
3402 \param eeprom_val Value to be written
3405 \retval -1: write failed
3406 \retval -2: USB device unavailable
3407 \retval -3: Invalid access to checksum protected area below 0x80
3408 \retval -4: Device can't access unprotected area
3409 \retval -5: Reading chip type failed
3411 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3412 unsigned short eeprom_val)
3414 int chip_type_location;
3415 unsigned short chip_type;
3417 if (ftdi == NULL || ftdi->usb_dev == NULL)
3418 ftdi_error_return(-2, "USB device unavailable");
3420 if (eeprom_addr <0x80)
3421 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3428 chip_type_location = 0x14;
3432 chip_type_location = 0x18;
3435 ftdi_error_return(-4, "Device can't access unprotected area");
3438 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3439 ftdi_error_return(-5, "Reading failed failed");
3440 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3441 if ((chip_type & 0xff) != 0x66)
3443 ftdi_error_return(-6, "EEPROM is not of 93x66");
3446 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3447 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3448 NULL, 0, ftdi->usb_write_timeout) != 0)
3449 ftdi_error_return(-1, "unable to write eeprom");
3457 \param ftdi pointer to ftdi_context
3460 \retval -1: read failed
3461 \retval -2: USB device unavailable
3463 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3465 unsigned short usb_val, status;
3467 unsigned char *eeprom;
3469 if (ftdi == NULL || ftdi->usb_dev == NULL)
3470 ftdi_error_return(-2, "USB device unavailable");
3471 eeprom = ftdi->eeprom->buf;
3473 /* These commands were traced while running MProg */
3474 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3476 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3478 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3481 for (i = 0; i < ftdi->eeprom->size/2; i++)
3483 usb_val = eeprom[i*2];
3484 usb_val += eeprom[(i*2)+1] << 8;
3485 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3486 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3487 NULL, 0, ftdi->usb_write_timeout) < 0)
3488 ftdi_error_return(-1, "unable to write eeprom");
3497 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3499 \param ftdi pointer to ftdi_context
3502 \retval -1: erase failed
3503 \retval -2: USB device unavailable
3504 \retval -3: Writing magic failed
3505 \retval -4: Read EEPROM failed
3506 \retval -5: Unexpected EEPROM value
3508 #define MAGIC 0x55aa
3509 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3511 unsigned short eeprom_value;
3512 if (ftdi == NULL || ftdi->usb_dev == NULL)
3513 ftdi_error_return(-2, "USB device unavailable");
3515 if (ftdi->type == TYPE_R)
3517 ftdi->eeprom->chip = 0;
3521 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3522 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3523 ftdi_error_return(-1, "unable to erase eeprom");
3526 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3527 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3528 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3529 Chip is 93x66 if magic is only read at word position 0xc0*/
3530 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3531 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3532 NULL, 0, ftdi->usb_write_timeout) != 0)
3533 ftdi_error_return(-3, "Writing magic failed");
3534 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3535 ftdi_error_return(-4, "Reading failed failed");
3536 if (eeprom_value == MAGIC)
3538 ftdi->eeprom->chip = 0x46;
3542 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3543 ftdi_error_return(-4, "Reading failed failed");
3544 if (eeprom_value == MAGIC)
3545 ftdi->eeprom->chip = 0x56;
3548 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3549 ftdi_error_return(-4, "Reading failed failed");
3550 if (eeprom_value == MAGIC)
3551 ftdi->eeprom->chip = 0x66;
3554 ftdi->eeprom->chip = -1;
3558 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3559 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3560 ftdi_error_return(-1, "unable to erase eeprom");
3565 Get string representation for last error code
3567 \param ftdi pointer to ftdi_context
3569 \retval Pointer to error string
3571 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3576 return ftdi->error_str;
3579 /* @} end of doxygen libftdi group */