1 /***************************************************************************
5 copyright : (C) 2003-2010 by Intra2net AG
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
78 \remark This should be called before all functions
80 int ftdi_init(struct ftdi_context *ftdi)
82 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
85 ftdi->usb_read_timeout = 5000;
86 ftdi->usb_write_timeout = 5000;
88 ftdi->type = TYPE_BM; /* chip type */
90 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
92 ftdi->readbuffer = NULL;
93 ftdi->readbuffer_offset = 0;
94 ftdi->readbuffer_remaining = 0;
95 ftdi->writebuffer_chunksize = 4096;
96 ftdi->max_packet_size = 0;
98 ftdi_set_interface(ftdi, INTERFACE_ANY);
99 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
101 ftdi->error_str = NULL;
104 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
105 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
106 ftdi->eeprom = eeprom;
108 /* All fine. Now allocate the readbuffer */
109 return ftdi_read_data_set_chunksize(ftdi, 4096);
113 Allocate and initialize a new ftdi_context
115 \return a pointer to a new ftdi_context, or NULL on failure
117 struct ftdi_context *ftdi_new(void)
119 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
126 if (ftdi_init(ftdi) != 0)
136 Open selected channels on a chip, otherwise use first channel.
138 \param ftdi pointer to ftdi_context
139 \param interface Interface to use for FT2232C/2232H/4232H chips.
142 \retval -1: unknown interface
143 \retval -2: USB device unavailable
145 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
148 ftdi_error_return(-2, "USB device unavailable");
155 ftdi->index = INTERFACE_A;
161 ftdi->index = INTERFACE_B;
167 ftdi->index = INTERFACE_C;
173 ftdi->index = INTERFACE_D;
178 ftdi_error_return(-1, "Unknown interface");
184 Deinitializes a ftdi_context.
186 \param ftdi pointer to ftdi_context
188 void ftdi_deinit(struct ftdi_context *ftdi)
193 ftdi_usb_close_internal (ftdi);
195 if (ftdi->readbuffer != NULL)
197 free(ftdi->readbuffer);
198 ftdi->readbuffer = NULL;
201 if (ftdi->eeprom != NULL)
203 if (ftdi->eeprom->manufacturer != 0)
205 free(ftdi->eeprom->manufacturer);
206 ftdi->eeprom->manufacturer = 0;
208 if (ftdi->eeprom->product != 0)
210 free(ftdi->eeprom->product);
211 ftdi->eeprom->product = 0;
213 if (ftdi->eeprom->serial != 0)
215 free(ftdi->eeprom->serial);
216 ftdi->eeprom->serial = 0;
221 libusb_exit(ftdi->usb_ctx);
225 Deinitialize and free an ftdi_context.
227 \param ftdi pointer to ftdi_context
229 void ftdi_free(struct ftdi_context *ftdi)
236 Use an already open libusb device.
238 \param ftdi pointer to ftdi_context
239 \param usb libusb libusb_device_handle to use
241 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
251 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
252 needs to be deallocated by ftdi_list_free() after use.
254 \param ftdi pointer to ftdi_context
255 \param devlist Pointer where to store list of found devices
256 \param vendor Vendor ID to search for
257 \param product Product ID to search for
259 \retval >0: number of devices found
260 \retval -3: out of memory
261 \retval -4: libusb_init() failed
262 \retval -5: libusb_get_device_list() failed
263 \retval -6: libusb_get_device_descriptor() failed
265 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
267 struct ftdi_device_list **curdev;
269 libusb_device **devs;
273 if (libusb_init(&ftdi->usb_ctx) < 0)
274 ftdi_error_return(-4, "libusb_init() failed");
276 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
277 ftdi_error_return(-5, "libusb_get_device_list() failed");
282 while ((dev = devs[i++]) != NULL)
284 struct libusb_device_descriptor desc;
286 if (libusb_get_device_descriptor(dev, &desc) < 0)
287 ftdi_error_return(-6, "libusb_get_device_descriptor() failed");
289 if (desc.idVendor == vendor && desc.idProduct == product)
291 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
293 ftdi_error_return(-3, "out of memory");
295 (*curdev)->next = NULL;
296 (*curdev)->dev = dev;
298 curdev = &(*curdev)->next;
307 Frees a usb device list.
309 \param devlist USB device list created by ftdi_usb_find_all()
311 void ftdi_list_free(struct ftdi_device_list **devlist)
313 struct ftdi_device_list *curdev, *next;
315 for (curdev = *devlist; curdev != NULL;)
326 Frees a usb device list.
328 \param devlist USB device list created by ftdi_usb_find_all()
330 void ftdi_list_free2(struct ftdi_device_list *devlist)
332 ftdi_list_free(&devlist);
336 Return device ID strings from the usb device.
338 The parameters manufacturer, description and serial may be NULL
339 or pointer to buffers to store the fetched strings.
341 \note Use this function only in combination with ftdi_usb_find_all()
342 as it closes the internal "usb_dev" after use.
344 \param ftdi pointer to ftdi_context
345 \param dev libusb usb_dev to use
346 \param manufacturer Store manufacturer string here if not NULL
347 \param mnf_len Buffer size of manufacturer string
348 \param description Store product description string here if not NULL
349 \param desc_len Buffer size of product description string
350 \param serial Store serial string here if not NULL
351 \param serial_len Buffer size of serial string
354 \retval -1: wrong arguments
355 \retval -4: unable to open device
356 \retval -7: get product manufacturer failed
357 \retval -8: get product description failed
358 \retval -9: get serial number failed
359 \retval -11: libusb_get_device_descriptor() failed
361 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
362 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
364 struct libusb_device_descriptor desc;
366 if ((ftdi==NULL) || (dev==NULL))
369 if (libusb_open(dev, &ftdi->usb_dev) < 0)
370 ftdi_error_return(-4, "libusb_open() failed");
372 if (libusb_get_device_descriptor(dev, &desc) < 0)
373 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
375 if (manufacturer != NULL)
377 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
379 ftdi_usb_close_internal (ftdi);
380 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
384 if (description != NULL)
386 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
388 ftdi_usb_close_internal (ftdi);
389 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
395 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
397 ftdi_usb_close_internal (ftdi);
398 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
402 ftdi_usb_close_internal (ftdi);
408 * Internal function to determine the maximum packet size.
409 * \param ftdi pointer to ftdi_context
410 * \param dev libusb usb_dev to use
411 * \retval Maximum packet size for this device
413 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
415 struct libusb_device_descriptor desc;
416 struct libusb_config_descriptor *config0;
417 unsigned int packet_size;
420 if (ftdi == NULL || dev == NULL)
423 // Determine maximum packet size. Init with default value.
424 // New hi-speed devices from FTDI use a packet size of 512 bytes
425 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
426 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
431 if (libusb_get_device_descriptor(dev, &desc) < 0)
434 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
437 if (desc.bNumConfigurations > 0)
439 if (ftdi->interface < config0->bNumInterfaces)
441 struct libusb_interface interface = config0->interface[ftdi->interface];
442 if (interface.num_altsetting > 0)
444 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
445 if (descriptor.bNumEndpoints > 0)
447 packet_size = descriptor.endpoint[0].wMaxPacketSize;
453 libusb_free_config_descriptor (config0);
458 Opens a ftdi device given by an usb_device.
460 \param ftdi pointer to ftdi_context
461 \param dev libusb usb_dev to use
464 \retval -3: unable to config device
465 \retval -4: unable to open device
466 \retval -5: unable to claim device
467 \retval -6: reset failed
468 \retval -7: set baudrate failed
469 \retval -8: ftdi context invalid
470 \retval -9: libusb_get_device_descriptor() failed
471 \retval -10: libusb_get_config_descriptor() failed
472 \retval -11: libusb_etach_kernel_driver() failed
473 \retval -12: libusb_get_configuration() failed
475 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
477 struct libusb_device_descriptor desc;
478 struct libusb_config_descriptor *config0;
479 int cfg, cfg0, detach_errno = 0;
482 ftdi_error_return(-8, "ftdi context invalid");
484 if (libusb_open(dev, &ftdi->usb_dev) < 0)
485 ftdi_error_return(-4, "libusb_open() failed");
487 if (libusb_get_device_descriptor(dev, &desc) < 0)
488 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
490 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
491 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
492 cfg0 = config0->bConfigurationValue;
493 libusb_free_config_descriptor (config0);
495 // Try to detach ftdi_sio kernel module.
497 // The return code is kept in a separate variable and only parsed
498 // if usb_set_configuration() or usb_claim_interface() fails as the
499 // detach operation might be denied and everything still works fine.
500 // Likely scenario is a static ftdi_sio kernel module.
501 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
502 detach_errno = errno;
504 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
505 ftdi_error_return(-12, "libusb_get_configuration () failed");
506 // set configuration (needed especially for windows)
507 // tolerate EBUSY: one device with one configuration, but two interfaces
508 // and libftdi sessions to both interfaces (e.g. FT2232)
509 if (desc.bNumConfigurations > 0 && cfg != cfg0)
511 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
513 ftdi_usb_close_internal (ftdi);
514 if (detach_errno == EPERM)
516 ftdi_error_return(-8, "inappropriate permissions on device!");
520 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
525 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
527 ftdi_usb_close_internal (ftdi);
528 if (detach_errno == EPERM)
530 ftdi_error_return(-8, "inappropriate permissions on device!");
534 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
538 if (ftdi_usb_reset (ftdi) != 0)
540 ftdi_usb_close_internal (ftdi);
541 ftdi_error_return(-6, "ftdi_usb_reset failed");
544 // Try to guess chip type
545 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
546 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
547 && desc.iSerialNumber == 0))
548 ftdi->type = TYPE_BM;
549 else if (desc.bcdDevice == 0x200)
550 ftdi->type = TYPE_AM;
551 else if (desc.bcdDevice == 0x500)
552 ftdi->type = TYPE_2232C;
553 else if (desc.bcdDevice == 0x600)
555 else if (desc.bcdDevice == 0x700)
556 ftdi->type = TYPE_2232H;
557 else if (desc.bcdDevice == 0x800)
558 ftdi->type = TYPE_4232H;
560 // Determine maximum packet size
561 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
563 if (ftdi_set_baudrate (ftdi, 9600) != 0)
565 ftdi_usb_close_internal (ftdi);
566 ftdi_error_return(-7, "set baudrate failed");
569 ftdi_error_return(0, "all fine");
573 Opens the first device with a given vendor and product ids.
575 \param ftdi pointer to ftdi_context
576 \param vendor Vendor ID
577 \param product Product ID
579 \retval same as ftdi_usb_open_desc()
581 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
583 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
587 Opens the first device with a given, vendor id, product id,
588 description and serial.
590 \param ftdi pointer to ftdi_context
591 \param vendor Vendor ID
592 \param product Product ID
593 \param description Description to search for. Use NULL if not needed.
594 \param serial Serial to search for. Use NULL if not needed.
597 \retval -3: usb device not found
598 \retval -4: unable to open device
599 \retval -5: unable to claim device
600 \retval -6: reset failed
601 \retval -7: set baudrate failed
602 \retval -8: get product description failed
603 \retval -9: get serial number failed
604 \retval -11: libusb_init() failed
605 \retval -12: libusb_get_device_list() failed
606 \retval -13: libusb_get_device_descriptor() failed
608 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
609 const char* description, const char* serial)
611 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
615 Opens the index-th device with a given, vendor id, product id,
616 description and serial.
618 \param ftdi pointer to ftdi_context
619 \param vendor Vendor ID
620 \param product Product ID
621 \param description Description to search for. Use NULL if not needed.
622 \param serial Serial to search for. Use NULL if not needed.
623 \param index Number of matching device to open if there are more than one, starts with 0.
626 \retval -1: usb_find_busses() failed
627 \retval -2: usb_find_devices() failed
628 \retval -3: usb device not found
629 \retval -4: unable to open device
630 \retval -5: unable to claim device
631 \retval -6: reset failed
632 \retval -7: set baudrate failed
633 \retval -8: get product description failed
634 \retval -9: get serial number failed
635 \retval -10: unable to close device
636 \retval -11: ftdi context invalid
638 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
639 const char* description, const char* serial, unsigned int index)
642 libusb_device **devs;
646 if (libusb_init(&ftdi->usb_ctx) < 0)
647 ftdi_error_return(-11, "libusb_init() failed");
650 ftdi_error_return(-11, "ftdi context invalid");
652 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
653 ftdi_error_return(-12, "libusb_get_device_list() failed");
655 while ((dev = devs[i++]) != NULL)
657 struct libusb_device_descriptor desc;
660 if (libusb_get_device_descriptor(dev, &desc) < 0)
661 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
663 if (desc.idVendor == vendor && desc.idProduct == product)
665 if (libusb_open(dev, &ftdi->usb_dev) < 0)
666 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
668 if (description != NULL)
670 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
672 libusb_close (ftdi->usb_dev);
673 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
675 if (strncmp(string, description, sizeof(string)) != 0)
677 libusb_close (ftdi->usb_dev);
683 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
685 ftdi_usb_close_internal (ftdi);
686 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
688 if (strncmp(string, serial, sizeof(string)) != 0)
690 ftdi_usb_close_internal (ftdi);
695 ftdi_usb_close_internal (ftdi);
703 res = ftdi_usb_open_dev(ftdi, dev);
704 libusb_free_device_list(devs,1);
710 ftdi_error_return_free_device_list(-3, "device not found", devs);
714 Opens the ftdi-device described by a description-string.
715 Intended to be used for parsing a device-description given as commandline argument.
717 \param ftdi pointer to ftdi_context
718 \param description NULL-terminated description-string, using this format:
719 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
720 \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")
721 \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
722 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
724 \note The description format may be extended in later versions.
727 \retval -1: libusb_init() failed
728 \retval -2: libusb_get_device_list() failed
729 \retval -3: usb device not found
730 \retval -4: unable to open device
731 \retval -5: unable to claim device
732 \retval -6: reset failed
733 \retval -7: set baudrate failed
734 \retval -8: get product description failed
735 \retval -9: get serial number failed
736 \retval -10: unable to close device
737 \retval -11: illegal description format
738 \retval -12: ftdi context invalid
740 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
743 ftdi_error_return(-12, "ftdi context invalid");
745 if (description[0] == 0 || description[1] != ':')
746 ftdi_error_return(-11, "illegal description format");
748 if (description[0] == 'd')
751 libusb_device **devs;
752 unsigned int bus_number, device_address;
755 if (libusb_init (&ftdi->usb_ctx) < 0)
756 ftdi_error_return(-1, "libusb_init() failed");
758 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
759 ftdi_error_return(-2, "libusb_get_device_list() failed");
761 /* XXX: This doesn't handle symlinks/odd paths/etc... */
762 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
763 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
765 while ((dev = devs[i++]) != NULL)
768 if (bus_number == libusb_get_bus_number (dev)
769 && device_address == libusb_get_device_address (dev))
771 ret = ftdi_usb_open_dev(ftdi, dev);
772 libusb_free_device_list(devs,1);
778 ftdi_error_return_free_device_list(-3, "device not found", devs);
780 else if (description[0] == 'i' || description[0] == 's')
783 unsigned int product;
784 unsigned int index=0;
785 const char *serial=NULL;
786 const char *startp, *endp;
789 startp=description+2;
790 vendor=strtoul((char*)startp,(char**)&endp,0);
791 if (*endp != ':' || endp == startp || errno != 0)
792 ftdi_error_return(-11, "illegal description format");
795 product=strtoul((char*)startp,(char**)&endp,0);
796 if (endp == startp || errno != 0)
797 ftdi_error_return(-11, "illegal description format");
799 if (description[0] == 'i' && *endp != 0)
801 /* optional index field in i-mode */
803 ftdi_error_return(-11, "illegal description format");
806 index=strtoul((char*)startp,(char**)&endp,0);
807 if (*endp != 0 || endp == startp || errno != 0)
808 ftdi_error_return(-11, "illegal description format");
810 if (description[0] == 's')
813 ftdi_error_return(-11, "illegal description format");
815 /* rest of the description is the serial */
819 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
823 ftdi_error_return(-11, "illegal description format");
828 Resets the ftdi device.
830 \param ftdi pointer to ftdi_context
833 \retval -1: FTDI reset failed
834 \retval -2: USB device unavailable
836 int ftdi_usb_reset(struct ftdi_context *ftdi)
838 if (ftdi == NULL || ftdi->usb_dev == NULL)
839 ftdi_error_return(-2, "USB device unavailable");
841 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
842 SIO_RESET_REQUEST, SIO_RESET_SIO,
843 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
844 ftdi_error_return(-1,"FTDI reset failed");
846 // Invalidate data in the readbuffer
847 ftdi->readbuffer_offset = 0;
848 ftdi->readbuffer_remaining = 0;
854 Clears the read buffer on the chip and the internal read buffer.
856 \param ftdi pointer to ftdi_context
859 \retval -1: read buffer purge failed
860 \retval -2: USB device unavailable
862 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
864 if (ftdi == NULL || ftdi->usb_dev == NULL)
865 ftdi_error_return(-2, "USB device unavailable");
867 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
868 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
869 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
870 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
872 // Invalidate data in the readbuffer
873 ftdi->readbuffer_offset = 0;
874 ftdi->readbuffer_remaining = 0;
880 Clears the write buffer on the chip.
882 \param ftdi pointer to ftdi_context
885 \retval -1: write buffer purge failed
886 \retval -2: USB device unavailable
888 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
890 if (ftdi == NULL || ftdi->usb_dev == NULL)
891 ftdi_error_return(-2, "USB device unavailable");
893 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
894 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
895 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
896 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
902 Clears the buffers on the chip and the internal read buffer.
904 \param ftdi pointer to ftdi_context
907 \retval -1: read buffer purge failed
908 \retval -2: write buffer purge failed
909 \retval -3: USB device unavailable
911 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
915 if (ftdi == NULL || ftdi->usb_dev == NULL)
916 ftdi_error_return(-3, "USB device unavailable");
918 result = ftdi_usb_purge_rx_buffer(ftdi);
922 result = ftdi_usb_purge_tx_buffer(ftdi);
932 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
934 \param ftdi pointer to ftdi_context
937 \retval -1: usb_release failed
938 \retval -3: ftdi context invalid
940 int ftdi_usb_close(struct ftdi_context *ftdi)
945 ftdi_error_return(-3, "ftdi context invalid");
947 if (ftdi->usb_dev != NULL)
948 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
951 ftdi_usb_close_internal (ftdi);
957 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
958 Function is only used internally
961 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
962 unsigned short *value, unsigned short *index)
964 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
965 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
966 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
967 int divisor, best_divisor, best_baud, best_baud_diff;
968 unsigned long encoded_divisor;
977 divisor = 24000000 / baudrate;
979 if (ftdi->type == TYPE_AM)
981 // Round down to supported fraction (AM only)
982 divisor -= am_adjust_dn[divisor & 7];
985 // Try this divisor and the one above it (because division rounds down)
989 for (i = 0; i < 2; i++)
991 int try_divisor = divisor + i;
995 // Round up to supported divisor value
996 if (try_divisor <= 8)
998 // Round up to minimum supported divisor
1001 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1003 // BM doesn't support divisors 9 through 11 inclusive
1006 else if (divisor < 16)
1008 // AM doesn't support divisors 9 through 15 inclusive
1013 if (ftdi->type == TYPE_AM)
1015 // Round up to supported fraction (AM only)
1016 try_divisor += am_adjust_up[try_divisor & 7];
1017 if (try_divisor > 0x1FFF8)
1019 // Round down to maximum supported divisor value (for AM)
1020 try_divisor = 0x1FFF8;
1025 if (try_divisor > 0x1FFFF)
1027 // Round down to maximum supported divisor value (for BM)
1028 try_divisor = 0x1FFFF;
1032 // Get estimated baud rate (to nearest integer)
1033 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1034 // Get absolute difference from requested baud rate
1035 if (baud_estimate < baudrate)
1037 baud_diff = baudrate - baud_estimate;
1041 baud_diff = baud_estimate - baudrate;
1043 if (i == 0 || baud_diff < best_baud_diff)
1045 // Closest to requested baud rate so far
1046 best_divisor = try_divisor;
1047 best_baud = baud_estimate;
1048 best_baud_diff = baud_diff;
1051 // Spot on! No point trying
1056 // Encode the best divisor value
1057 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1058 // Deal with special cases for encoded value
1059 if (encoded_divisor == 1)
1061 encoded_divisor = 0; // 3000000 baud
1063 else if (encoded_divisor == 0x4001)
1065 encoded_divisor = 1; // 2000000 baud (BM only)
1067 // Split into "value" and "index" values
1068 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1069 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1071 *index = (unsigned short)(encoded_divisor >> 8);
1073 *index |= ftdi->index;
1076 *index = (unsigned short)(encoded_divisor >> 16);
1078 // Return the nearest baud rate
1083 Sets the chip baud rate
1085 \param ftdi pointer to ftdi_context
1086 \param baudrate baud rate to set
1089 \retval -1: invalid baudrate
1090 \retval -2: setting baudrate failed
1091 \retval -3: USB device unavailable
1093 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1095 unsigned short value, index;
1096 int actual_baudrate;
1098 if (ftdi == NULL || ftdi->usb_dev == NULL)
1099 ftdi_error_return(-3, "USB device unavailable");
1101 if (ftdi->bitbang_enabled)
1103 baudrate = baudrate*4;
1106 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1107 if (actual_baudrate <= 0)
1108 ftdi_error_return (-1, "Silly baudrate <= 0.");
1110 // Check within tolerance (about 5%)
1111 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1112 || ((actual_baudrate < baudrate)
1113 ? (actual_baudrate * 21 < baudrate * 20)
1114 : (baudrate * 21 < actual_baudrate * 20)))
1115 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1117 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1118 SIO_SET_BAUDRATE_REQUEST, value,
1119 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1120 ftdi_error_return (-2, "Setting new baudrate failed");
1122 ftdi->baudrate = baudrate;
1127 Set (RS232) line characteristics.
1128 The break type can only be set via ftdi_set_line_property2()
1129 and defaults to "off".
1131 \param ftdi pointer to ftdi_context
1132 \param bits Number of bits
1133 \param sbit Number of stop bits
1134 \param parity Parity mode
1137 \retval -1: Setting line property failed
1139 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1140 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1142 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1146 Set (RS232) line characteristics
1148 \param ftdi pointer to ftdi_context
1149 \param bits Number of bits
1150 \param sbit Number of stop bits
1151 \param parity Parity mode
1152 \param break_type Break type
1155 \retval -1: Setting line property failed
1156 \retval -2: USB device unavailable
1158 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1159 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1160 enum ftdi_break_type break_type)
1162 unsigned short value = bits;
1164 if (ftdi == NULL || ftdi->usb_dev == NULL)
1165 ftdi_error_return(-2, "USB device unavailable");
1170 value |= (0x00 << 8);
1173 value |= (0x01 << 8);
1176 value |= (0x02 << 8);
1179 value |= (0x03 << 8);
1182 value |= (0x04 << 8);
1189 value |= (0x00 << 11);
1192 value |= (0x01 << 11);
1195 value |= (0x02 << 11);
1202 value |= (0x00 << 14);
1205 value |= (0x01 << 14);
1209 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1210 SIO_SET_DATA_REQUEST, value,
1211 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1212 ftdi_error_return (-1, "Setting new line property failed");
1218 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1220 \param ftdi pointer to ftdi_context
1221 \param buf Buffer with the data
1222 \param size Size of the buffer
1224 \retval -666: USB device unavailable
1225 \retval <0: error code from usb_bulk_write()
1226 \retval >0: number of bytes written
1228 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1233 if (ftdi == NULL || ftdi->usb_dev == NULL)
1234 ftdi_error_return(-666, "USB device unavailable");
1236 while (offset < size)
1238 int write_size = ftdi->writebuffer_chunksize;
1240 if (offset+write_size > size)
1241 write_size = size-offset;
1243 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1244 ftdi_error_return(-1, "usb bulk write failed");
1246 offset += actual_length;
1252 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1254 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1255 struct ftdi_context *ftdi = tc->ftdi;
1256 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1258 packet_size = ftdi->max_packet_size;
1260 actual_length = transfer->actual_length;
1262 if (actual_length > 2)
1264 // skip FTDI status bytes.
1265 // Maybe stored in the future to enable modem use
1266 num_of_chunks = actual_length / packet_size;
1267 chunk_remains = actual_length % packet_size;
1268 //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);
1270 ftdi->readbuffer_offset += 2;
1273 if (actual_length > packet_size - 2)
1275 for (i = 1; i < num_of_chunks; i++)
1276 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1277 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1279 if (chunk_remains > 2)
1281 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1282 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1284 actual_length -= 2*num_of_chunks;
1287 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1290 if (actual_length > 0)
1292 // data still fits in buf?
1293 if (tc->offset + actual_length <= tc->size)
1295 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1296 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1297 tc->offset += actual_length;
1299 ftdi->readbuffer_offset = 0;
1300 ftdi->readbuffer_remaining = 0;
1302 /* Did we read exactly the right amount of bytes? */
1303 if (tc->offset == tc->size)
1305 //printf("read_data exact rem %d offset %d\n",
1306 //ftdi->readbuffer_remaining, offset);
1313 // only copy part of the data or size <= readbuffer_chunksize
1314 int part_size = tc->size - tc->offset;
1315 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1316 tc->offset += part_size;
1318 ftdi->readbuffer_offset += part_size;
1319 ftdi->readbuffer_remaining = actual_length - part_size;
1321 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1322 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1328 ret = libusb_submit_transfer (transfer);
1334 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1336 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1337 struct ftdi_context *ftdi = tc->ftdi;
1339 tc->offset += transfer->actual_length;
1341 if (tc->offset == tc->size)
1347 int write_size = ftdi->writebuffer_chunksize;
1350 if (tc->offset + write_size > tc->size)
1351 write_size = tc->size - tc->offset;
1353 transfer->length = write_size;
1354 transfer->buffer = tc->buf + tc->offset;
1355 ret = libusb_submit_transfer (transfer);
1363 Writes data to the chip. Does not wait for completion of the transfer
1364 nor does it make sure that the transfer was successful.
1366 Use libusb 1.0 asynchronous API.
1368 \param ftdi pointer to ftdi_context
1369 \param buf Buffer with the data
1370 \param size Size of the buffer
1372 \retval NULL: Some error happens when submit transfer
1373 \retval !NULL: Pointer to a ftdi_transfer_control
1376 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1378 struct ftdi_transfer_control *tc;
1379 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1380 int write_size, ret;
1382 if (ftdi == NULL || ftdi->usb_dev == NULL)
1384 libusb_free_transfer(transfer);
1388 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1390 if (!tc || !transfer)
1399 if (size < ftdi->writebuffer_chunksize)
1402 write_size = ftdi->writebuffer_chunksize;
1404 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1405 write_size, ftdi_write_data_cb, tc,
1406 ftdi->usb_write_timeout);
1407 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1409 ret = libusb_submit_transfer(transfer);
1412 libusb_free_transfer(transfer);
1414 tc->transfer = NULL;
1417 tc->transfer = transfer;
1423 Reads data from the chip. Does not wait for completion of the transfer
1424 nor does it make sure that the transfer was successful.
1426 Use libusb 1.0 asynchronous API.
1428 \param ftdi pointer to ftdi_context
1429 \param buf Buffer with the data
1430 \param size Size of the buffer
1432 \retval NULL: Some error happens when submit transfer
1433 \retval !NULL: Pointer to a ftdi_transfer_control
1436 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1438 struct ftdi_transfer_control *tc;
1439 struct libusb_transfer *transfer;
1442 if (ftdi == NULL || ftdi->usb_dev == NULL)
1445 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1453 if (size <= ftdi->readbuffer_remaining)
1455 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1458 ftdi->readbuffer_remaining -= size;
1459 ftdi->readbuffer_offset += size;
1461 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1465 tc->transfer = NULL;
1470 if (ftdi->readbuffer_remaining != 0)
1472 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1474 tc->offset = ftdi->readbuffer_remaining;
1479 transfer = libusb_alloc_transfer(0);
1486 ftdi->readbuffer_remaining = 0;
1487 ftdi->readbuffer_offset = 0;
1489 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);
1490 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1492 ret = libusb_submit_transfer(transfer);
1495 libusb_free_transfer(transfer);
1499 tc->transfer = transfer;
1505 Wait for completion of the transfer.
1507 Use libusb 1.0 asynchronous API.
1509 \param tc pointer to ftdi_transfer_control
1511 \retval < 0: Some error happens
1512 \retval >= 0: Data size transferred
1515 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1519 while (!tc->completed)
1521 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1524 if (ret == LIBUSB_ERROR_INTERRUPTED)
1526 libusb_cancel_transfer(tc->transfer);
1527 while (!tc->completed)
1528 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1530 libusb_free_transfer(tc->transfer);
1538 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1539 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1543 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1545 libusb_free_transfer(tc->transfer);
1552 Configure write buffer chunk size.
1555 \param ftdi pointer to ftdi_context
1556 \param chunksize Chunk size
1559 \retval -1: ftdi context invalid
1561 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1564 ftdi_error_return(-1, "ftdi context invalid");
1566 ftdi->writebuffer_chunksize = chunksize;
1571 Get write buffer chunk size.
1573 \param ftdi pointer to ftdi_context
1574 \param chunksize Pointer to store chunk size in
1577 \retval -1: ftdi context invalid
1579 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1582 ftdi_error_return(-1, "ftdi context invalid");
1584 *chunksize = ftdi->writebuffer_chunksize;
1589 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1591 Automatically strips the two modem status bytes transfered during every read.
1593 \param ftdi pointer to ftdi_context
1594 \param buf Buffer to store data in
1595 \param size Size of the buffer
1597 \retval -666: USB device unavailable
1598 \retval <0: error code from libusb_bulk_transfer()
1599 \retval 0: no data was available
1600 \retval >0: number of bytes read
1603 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1605 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1606 int packet_size = ftdi->max_packet_size;
1607 int actual_length = 1;
1609 if (ftdi == NULL || ftdi->usb_dev == NULL)
1610 ftdi_error_return(-666, "USB device unavailable");
1612 // Packet size sanity check (avoid division by zero)
1613 if (packet_size == 0)
1614 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1616 // everything we want is still in the readbuffer?
1617 if (size <= ftdi->readbuffer_remaining)
1619 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1622 ftdi->readbuffer_remaining -= size;
1623 ftdi->readbuffer_offset += size;
1625 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1629 // something still in the readbuffer, but not enough to satisfy 'size'?
1630 if (ftdi->readbuffer_remaining != 0)
1632 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1635 offset += ftdi->readbuffer_remaining;
1637 // do the actual USB read
1638 while (offset < size && actual_length > 0)
1640 ftdi->readbuffer_remaining = 0;
1641 ftdi->readbuffer_offset = 0;
1642 /* returns how much received */
1643 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1645 ftdi_error_return(ret, "usb bulk read failed");
1647 if (actual_length > 2)
1649 // skip FTDI status bytes.
1650 // Maybe stored in the future to enable modem use
1651 num_of_chunks = actual_length / packet_size;
1652 chunk_remains = actual_length % packet_size;
1653 //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);
1655 ftdi->readbuffer_offset += 2;
1658 if (actual_length > packet_size - 2)
1660 for (i = 1; i < num_of_chunks; i++)
1661 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1662 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1664 if (chunk_remains > 2)
1666 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1667 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1669 actual_length -= 2*num_of_chunks;
1672 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1675 else if (actual_length <= 2)
1677 // no more data to read?
1680 if (actual_length > 0)
1682 // data still fits in buf?
1683 if (offset+actual_length <= size)
1685 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1686 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1687 offset += actual_length;
1689 /* Did we read exactly the right amount of bytes? */
1691 //printf("read_data exact rem %d offset %d\n",
1692 //ftdi->readbuffer_remaining, offset);
1697 // only copy part of the data or size <= readbuffer_chunksize
1698 int part_size = size-offset;
1699 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1701 ftdi->readbuffer_offset += part_size;
1702 ftdi->readbuffer_remaining = actual_length-part_size;
1703 offset += part_size;
1705 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1706 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1717 Configure read buffer chunk size.
1720 Automatically reallocates the buffer.
1722 \param ftdi pointer to ftdi_context
1723 \param chunksize Chunk size
1726 \retval -1: ftdi context invalid
1728 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1730 unsigned char *new_buf;
1733 ftdi_error_return(-1, "ftdi context invalid");
1735 // Invalidate all remaining data
1736 ftdi->readbuffer_offset = 0;
1737 ftdi->readbuffer_remaining = 0;
1739 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1740 which is defined in libusb-1.0. Otherwise, each USB read request will
1741 be divided into multiple URBs. This will cause issues on Linux kernel
1742 older than 2.6.32. */
1743 if (chunksize > 16384)
1747 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1748 ftdi_error_return(-1, "out of memory for readbuffer");
1750 ftdi->readbuffer = new_buf;
1751 ftdi->readbuffer_chunksize = chunksize;
1757 Get read buffer chunk size.
1759 \param ftdi pointer to ftdi_context
1760 \param chunksize Pointer to store chunk size in
1763 \retval -1: FTDI context invalid
1765 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1768 ftdi_error_return(-1, "FTDI context invalid");
1770 *chunksize = ftdi->readbuffer_chunksize;
1776 Enable bitbang mode.
1778 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1780 \param ftdi pointer to ftdi_context
1781 \param bitmask Bitmask to configure lines.
1782 HIGH/ON value configures a line as output.
1785 \retval -1: can't enable bitbang mode
1786 \retval -2: USB device unavailable
1788 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1790 unsigned short usb_val;
1792 if (ftdi == NULL || ftdi->usb_dev == NULL)
1793 ftdi_error_return(-2, "USB device unavailable");
1795 usb_val = bitmask; // low byte: bitmask
1796 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1797 usb_val |= (ftdi->bitbang_mode << 8);
1799 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1800 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1801 NULL, 0, ftdi->usb_write_timeout) < 0)
1802 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1804 ftdi->bitbang_enabled = 1;
1809 Disable bitbang mode.
1811 \param ftdi pointer to ftdi_context
1814 \retval -1: can't disable bitbang mode
1815 \retval -2: USB device unavailable
1817 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1819 if (ftdi == NULL || ftdi->usb_dev == NULL)
1820 ftdi_error_return(-2, "USB device unavailable");
1822 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)
1823 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1825 ftdi->bitbang_enabled = 0;
1830 Enable/disable bitbang modes.
1832 \param ftdi pointer to ftdi_context
1833 \param bitmask Bitmask to configure lines.
1834 HIGH/ON value configures a line as output.
1835 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1838 \retval -1: can't enable bitbang mode
1839 \retval -2: USB device unavailable
1841 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1843 unsigned short usb_val;
1845 if (ftdi == NULL || ftdi->usb_dev == NULL)
1846 ftdi_error_return(-2, "USB device unavailable");
1848 usb_val = bitmask; // low byte: bitmask
1849 usb_val |= (mode << 8);
1850 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)
1851 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1853 ftdi->bitbang_mode = mode;
1854 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1859 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1861 \param ftdi pointer to ftdi_context
1862 \param pins Pointer to store pins into
1865 \retval -1: read pins failed
1866 \retval -2: USB device unavailable
1868 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1870 if (ftdi == NULL || ftdi->usb_dev == NULL)
1871 ftdi_error_return(-2, "USB device unavailable");
1873 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)
1874 ftdi_error_return(-1, "read pins failed");
1882 The FTDI chip keeps data in the internal buffer for a specific
1883 amount of time if the buffer is not full yet to decrease
1884 load on the usb bus.
1886 \param ftdi pointer to ftdi_context
1887 \param latency Value between 1 and 255
1890 \retval -1: latency out of range
1891 \retval -2: unable to set latency timer
1892 \retval -3: USB device unavailable
1894 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1896 unsigned short usb_val;
1899 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1901 if (ftdi == NULL || ftdi->usb_dev == NULL)
1902 ftdi_error_return(-3, "USB device unavailable");
1905 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)
1906 ftdi_error_return(-2, "unable to set latency timer");
1914 \param ftdi pointer to ftdi_context
1915 \param latency Pointer to store latency value in
1918 \retval -1: unable to get latency timer
1919 \retval -2: USB device unavailable
1921 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1923 unsigned short usb_val;
1925 if (ftdi == NULL || ftdi->usb_dev == NULL)
1926 ftdi_error_return(-2, "USB device unavailable");
1928 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)
1929 ftdi_error_return(-1, "reading latency timer failed");
1931 *latency = (unsigned char)usb_val;
1936 Poll modem status information
1938 This function allows the retrieve the two status bytes of the device.
1939 The device sends these bytes also as a header for each read access
1940 where they are discarded by ftdi_read_data(). The chip generates
1941 the two stripped status bytes in the absence of data every 40 ms.
1943 Layout of the first byte:
1944 - B0..B3 - must be 0
1945 - B4 Clear to send (CTS)
1948 - B5 Data set ready (DTS)
1951 - B6 Ring indicator (RI)
1954 - B7 Receive line signal detect (RLSD)
1958 Layout of the second byte:
1959 - B0 Data ready (DR)
1960 - B1 Overrun error (OE)
1961 - B2 Parity error (PE)
1962 - B3 Framing error (FE)
1963 - B4 Break interrupt (BI)
1964 - B5 Transmitter holding register (THRE)
1965 - B6 Transmitter empty (TEMT)
1966 - B7 Error in RCVR FIFO
1968 \param ftdi pointer to ftdi_context
1969 \param status Pointer to store status information in. Must be two bytes.
1972 \retval -1: unable to retrieve status information
1973 \retval -2: USB device unavailable
1975 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1979 if (ftdi == NULL || ftdi->usb_dev == NULL)
1980 ftdi_error_return(-2, "USB device unavailable");
1982 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)
1983 ftdi_error_return(-1, "getting modem status failed");
1985 *status = (usb_val[1] << 8) | usb_val[0];
1991 Set flowcontrol for ftdi chip
1993 \param ftdi pointer to ftdi_context
1994 \param flowctrl flow control to use. should be
1995 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1998 \retval -1: set flow control failed
1999 \retval -2: USB device unavailable
2001 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2003 if (ftdi == NULL || ftdi->usb_dev == NULL)
2004 ftdi_error_return(-2, "USB device unavailable");
2006 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2007 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2008 NULL, 0, ftdi->usb_write_timeout) < 0)
2009 ftdi_error_return(-1, "set flow control failed");
2017 \param ftdi pointer to ftdi_context
2018 \param state state to set line to (1 or 0)
2021 \retval -1: set dtr failed
2022 \retval -2: USB device unavailable
2024 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2026 unsigned short usb_val;
2028 if (ftdi == NULL || ftdi->usb_dev == NULL)
2029 ftdi_error_return(-2, "USB device unavailable");
2032 usb_val = SIO_SET_DTR_HIGH;
2034 usb_val = SIO_SET_DTR_LOW;
2036 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2037 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2038 NULL, 0, ftdi->usb_write_timeout) < 0)
2039 ftdi_error_return(-1, "set dtr failed");
2047 \param ftdi pointer to ftdi_context
2048 \param state state to set line to (1 or 0)
2051 \retval -1: set rts failed
2052 \retval -2: USB device unavailable
2054 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2056 unsigned short usb_val;
2058 if (ftdi == NULL || ftdi->usb_dev == NULL)
2059 ftdi_error_return(-2, "USB device unavailable");
2062 usb_val = SIO_SET_RTS_HIGH;
2064 usb_val = SIO_SET_RTS_LOW;
2066 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2067 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2068 NULL, 0, ftdi->usb_write_timeout) < 0)
2069 ftdi_error_return(-1, "set of rts failed");
2075 Set dtr and rts line in one pass
2077 \param ftdi pointer to ftdi_context
2078 \param dtr DTR state to set line to (1 or 0)
2079 \param rts RTS state to set line to (1 or 0)
2082 \retval -1: set dtr/rts failed
2083 \retval -2: USB device unavailable
2085 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2087 unsigned short usb_val;
2089 if (ftdi == NULL || ftdi->usb_dev == NULL)
2090 ftdi_error_return(-2, "USB device unavailable");
2093 usb_val = SIO_SET_DTR_HIGH;
2095 usb_val = SIO_SET_DTR_LOW;
2098 usb_val |= SIO_SET_RTS_HIGH;
2100 usb_val |= SIO_SET_RTS_LOW;
2102 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2103 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2104 NULL, 0, ftdi->usb_write_timeout) < 0)
2105 ftdi_error_return(-1, "set of rts/dtr failed");
2111 Set the special event character
2113 \param ftdi pointer to ftdi_context
2114 \param eventch Event character
2115 \param enable 0 to disable the event character, non-zero otherwise
2118 \retval -1: unable to set event character
2119 \retval -2: USB device unavailable
2121 int ftdi_set_event_char(struct ftdi_context *ftdi,
2122 unsigned char eventch, unsigned char enable)
2124 unsigned short usb_val;
2126 if (ftdi == NULL || ftdi->usb_dev == NULL)
2127 ftdi_error_return(-2, "USB device unavailable");
2133 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)
2134 ftdi_error_return(-1, "setting event character failed");
2142 \param ftdi pointer to ftdi_context
2143 \param errorch Error character
2144 \param enable 0 to disable the error character, non-zero otherwise
2147 \retval -1: unable to set error character
2148 \retval -2: USB device unavailable
2150 int ftdi_set_error_char(struct ftdi_context *ftdi,
2151 unsigned char errorch, unsigned char enable)
2153 unsigned short usb_val;
2155 if (ftdi == NULL || ftdi->usb_dev == NULL)
2156 ftdi_error_return(-2, "USB device unavailable");
2162 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)
2163 ftdi_error_return(-1, "setting error character failed");
2169 Init eeprom with default values.
2170 \param ftdi pointer to ftdi_context
2171 \param manufacturer String to use as Manufacturer
2172 \param product String to use as Product description
2173 \param serial String to use as Serial number description
2176 \retval -1: No struct ftdi_context
2177 \retval -2: No struct ftdi_eeprom
2179 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2180 char * product, char * serial)
2182 struct ftdi_eeprom *eeprom;
2185 ftdi_error_return(-1, "No struct ftdi_context");
2187 if (ftdi->eeprom == NULL)
2188 ftdi_error_return(-2,"No struct ftdi_eeprom");
2190 eeprom = ftdi->eeprom;
2191 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2193 eeprom->vendor_id = 0x0403;
2194 eeprom->use_serial = USE_SERIAL_NUM;
2195 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2196 (ftdi->type == TYPE_R))
2197 eeprom->product_id = 0x6001;
2199 eeprom->product_id = 0x6010;
2200 if (ftdi->type == TYPE_AM)
2201 eeprom->usb_version = 0x0101;
2203 eeprom->usb_version = 0x0200;
2204 eeprom->max_power = 100;
2206 if (eeprom->manufacturer)
2207 free (eeprom->manufacturer);
2208 eeprom->manufacturer = NULL;
2211 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2212 if (eeprom->manufacturer)
2213 strcpy(eeprom->manufacturer, manufacturer);
2216 if (eeprom->product)
2217 free (eeprom->product);
2218 eeprom->product = NULL;
2220 eeprom->product = malloc(strlen(product)+1);
2221 if (eeprom->product)
2222 strcpy(eeprom->product, product);
2226 free (eeprom->serial);
2227 eeprom->serial = NULL;
2230 eeprom->serial = malloc(strlen(serial)+1);
2232 strcpy(eeprom->serial, serial);
2236 if (ftdi->type == TYPE_R)
2238 eeprom->max_power = 90;
2239 eeprom->size = 0x80;
2240 eeprom->cbus_function[0] = CBUS_TXLED;
2241 eeprom->cbus_function[1] = CBUS_RXLED;
2242 eeprom->cbus_function[2] = CBUS_TXDEN;
2243 eeprom->cbus_function[3] = CBUS_PWREN;
2244 eeprom->cbus_function[4] = CBUS_SLEEP;
2252 Build binary buffer from ftdi_eeprom structure.
2253 Output is suitable for ftdi_write_eeprom().
2255 \param ftdi pointer to ftdi_context
2257 \retval >=0: size of eeprom user area in bytes
2258 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2259 \retval -2: Invalid eeprom or ftdi pointer
2260 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2261 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2262 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2263 \retval -6: No connected EEPROM or EEPROM Type unknown
2265 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2267 unsigned char i, j, eeprom_size_mask;
2268 unsigned short checksum, value;
2269 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2271 struct ftdi_eeprom *eeprom;
2272 unsigned char * output;
2275 ftdi_error_return(-2,"No context");
2276 if (ftdi->eeprom == NULL)
2277 ftdi_error_return(-2,"No eeprom structure");
2279 eeprom= ftdi->eeprom;
2280 output = eeprom->buf;
2282 if (eeprom->chip == -1)
2283 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2285 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2286 eeprom->size = 0x100;
2288 eeprom->size = 0x80;
2290 if (eeprom->manufacturer != NULL)
2291 manufacturer_size = strlen(eeprom->manufacturer);
2292 if (eeprom->product != NULL)
2293 product_size = strlen(eeprom->product);
2294 if (eeprom->serial != NULL)
2295 serial_size = strlen(eeprom->serial);
2297 // eeprom size check
2302 user_area_size = 96; // base size for strings (total of 48 characters)
2305 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2308 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2310 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2312 user_area_size = 86;
2318 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2320 if (user_area_size < 0)
2321 ftdi_error_return(-1,"eeprom size exceeded");
2324 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2326 // Bytes and Bits set for all Types
2328 // Addr 02: Vendor ID
2329 output[0x02] = eeprom->vendor_id;
2330 output[0x03] = eeprom->vendor_id >> 8;
2332 // Addr 04: Product ID
2333 output[0x04] = eeprom->product_id;
2334 output[0x05] = eeprom->product_id >> 8;
2336 // Addr 06: Device release number (0400h for BM features)
2337 output[0x06] = 0x00;
2341 output[0x07] = 0x02;
2344 output[0x07] = 0x04;
2347 output[0x07] = 0x05;
2350 output[0x07] = 0x06;
2353 output[0x07] = 0x07;
2356 output[0x07] = 0x08;
2359 output[0x07] = 0x00;
2362 // Addr 08: Config descriptor
2364 // Bit 6: 1 if this device is self powered, 0 if bus powered
2365 // Bit 5: 1 if this device uses remote wakeup
2366 // Bit 4-0: reserved - 0
2368 if (eeprom->self_powered == 1)
2370 if (eeprom->remote_wakeup == 1)
2374 // Addr 09: Max power consumption: max power = value * 2 mA
2375 output[0x09] = eeprom->max_power>>1;
2377 if (ftdi->type != TYPE_AM)
2379 // Addr 0A: Chip configuration
2380 // Bit 7: 0 - reserved
2381 // Bit 6: 0 - reserved
2382 // Bit 5: 0 - reserved
2383 // Bit 4: 1 - Change USB version
2384 // Bit 3: 1 - Use the serial number string
2385 // Bit 2: 1 - Enable suspend pull downs for lower power
2386 // Bit 1: 1 - Out EndPoint is Isochronous
2387 // Bit 0: 1 - In EndPoint is Isochronous
2390 if (eeprom->in_is_isochronous == 1)
2392 if (eeprom->out_is_isochronous == 1)
2398 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2399 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2414 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2415 eeprom_size_mask = eeprom->size -1;
2417 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2418 // Addr 0F: Length of manufacturer string
2419 // Output manufacturer
2420 output[0x0E] = i; // calculate offset
2421 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2422 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2423 for (j = 0; j < manufacturer_size; j++)
2425 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2426 output[i & eeprom_size_mask] = 0x00, i++;
2428 output[0x0F] = manufacturer_size*2 + 2;
2430 // Addr 10: Offset of the product string + 0x80, calculated later
2431 // Addr 11: Length of product string
2432 output[0x10] = i | 0x80; // calculate offset
2433 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2434 output[i & eeprom_size_mask] = 0x03, i++;
2435 for (j = 0; j < product_size; j++)
2437 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2438 output[i & eeprom_size_mask] = 0x00, i++;
2440 output[0x11] = product_size*2 + 2;
2442 // Addr 12: Offset of the serial string + 0x80, calculated later
2443 // Addr 13: Length of serial string
2444 output[0x12] = i | 0x80; // calculate offset
2445 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2446 output[i & eeprom_size_mask] = 0x03, i++;
2447 for (j = 0; j < serial_size; j++)
2449 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2450 output[i & eeprom_size_mask] = 0x00, i++;
2453 // Legacy port name and PnP fields for FT2232 and newer chips
2454 if (ftdi->type > TYPE_BM)
2456 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2458 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2460 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2464 output[0x13] = serial_size*2 + 2;
2466 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2468 if (eeprom->use_serial == USE_SERIAL_NUM )
2469 output[0x0A] |= USE_SERIAL_NUM;
2471 output[0x0A] &= ~USE_SERIAL_NUM;
2474 /* Bytes and Bits specific to (some) types
2475 Write linear, as this allows easier fixing*/
2481 output[0x0C] = eeprom->usb_version & 0xff;
2482 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2483 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2484 output[0x0A] |= USE_USB_VERSION_BIT;
2486 output[0x0A] &= ~USE_USB_VERSION_BIT;
2491 output[0x00] = (eeprom->channel_a_type);
2492 if ( eeprom->channel_a_driver == DRIVER_VCP)
2493 output[0x00] |= DRIVER_VCP;
2495 output[0x00] &= ~DRIVER_VCP;
2497 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2498 output[0x00] |= HIGH_CURRENT_DRIVE;
2500 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2502 output[0x01] = (eeprom->channel_b_type);
2503 if ( eeprom->channel_b_driver == DRIVER_VCP)
2504 output[0x01] |= DRIVER_VCP;
2506 output[0x01] &= ~DRIVER_VCP;
2508 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2509 output[0x01] |= HIGH_CURRENT_DRIVE;
2511 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2513 if (eeprom->in_is_isochronous == 1)
2514 output[0x0A] |= 0x1;
2516 output[0x0A] &= ~0x1;
2517 if (eeprom->out_is_isochronous == 1)
2518 output[0x0A] |= 0x2;
2520 output[0x0A] &= ~0x2;
2521 if (eeprom->suspend_pull_downs == 1)
2522 output[0x0A] |= 0x4;
2524 output[0x0A] &= ~0x4;
2525 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2526 output[0x0A] |= USE_USB_VERSION_BIT;
2528 output[0x0A] &= ~USE_USB_VERSION_BIT;
2530 output[0x0C] = eeprom->usb_version & 0xff;
2531 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2532 output[0x14] = eeprom->chip;
2535 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2536 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2537 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2539 if (eeprom->suspend_pull_downs == 1)
2540 output[0x0A] |= 0x4;
2542 output[0x0A] &= ~0x4;
2543 output[0x0B] = eeprom->invert;
2544 output[0x0C] = eeprom->usb_version & 0xff;
2545 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2547 if (eeprom->cbus_function[0] > CBUS_BB)
2548 output[0x14] = CBUS_TXLED;
2550 output[0x14] = eeprom->cbus_function[0];
2552 if (eeprom->cbus_function[1] > CBUS_BB)
2553 output[0x14] |= CBUS_RXLED<<4;
2555 output[0x14] |= eeprom->cbus_function[1]<<4;
2557 if (eeprom->cbus_function[2] > CBUS_BB)
2558 output[0x15] = CBUS_TXDEN;
2560 output[0x15] = eeprom->cbus_function[2];
2562 if (eeprom->cbus_function[3] > CBUS_BB)
2563 output[0x15] |= CBUS_PWREN<<4;
2565 output[0x15] |= eeprom->cbus_function[3]<<4;
2567 if (eeprom->cbus_function[4] > CBUS_CLK6)
2568 output[0x16] = CBUS_SLEEP;
2570 output[0x16] = eeprom->cbus_function[4];
2573 output[0x00] = (eeprom->channel_a_type);
2574 if ( eeprom->channel_a_driver == DRIVER_VCP)
2575 output[0x00] |= DRIVER_VCP;
2577 output[0x00] &= ~DRIVER_VCP;
2579 output[0x01] = (eeprom->channel_b_type);
2580 if ( eeprom->channel_b_driver == DRIVER_VCP)
2581 output[0x01] |= DRIVER_VCP;
2583 output[0x01] &= ~DRIVER_VCP;
2584 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2585 output[0x01] |= SUSPEND_DBUS7_BIT;
2587 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2589 if (eeprom->suspend_pull_downs == 1)
2590 output[0x0A] |= 0x4;
2592 output[0x0A] &= ~0x4;
2594 if (eeprom->group0_drive > DRIVE_16MA)
2595 output[0x0c] |= DRIVE_16MA;
2597 output[0x0c] |= eeprom->group0_drive;
2598 if (eeprom->group0_schmitt == IS_SCHMITT)
2599 output[0x0c] |= IS_SCHMITT;
2600 if (eeprom->group0_slew == SLOW_SLEW)
2601 output[0x0c] |= SLOW_SLEW;
2603 if (eeprom->group1_drive > DRIVE_16MA)
2604 output[0x0c] |= DRIVE_16MA<<4;
2606 output[0x0c] |= eeprom->group1_drive<<4;
2607 if (eeprom->group1_schmitt == IS_SCHMITT)
2608 output[0x0c] |= IS_SCHMITT<<4;
2609 if (eeprom->group1_slew == SLOW_SLEW)
2610 output[0x0c] |= SLOW_SLEW<<4;
2612 if (eeprom->group2_drive > DRIVE_16MA)
2613 output[0x0d] |= DRIVE_16MA;
2615 output[0x0d] |= eeprom->group2_drive;
2616 if (eeprom->group2_schmitt == IS_SCHMITT)
2617 output[0x0d] |= IS_SCHMITT;
2618 if (eeprom->group2_slew == SLOW_SLEW)
2619 output[0x0d] |= SLOW_SLEW;
2621 if (eeprom->group3_drive > DRIVE_16MA)
2622 output[0x0d] |= DRIVE_16MA<<4;
2624 output[0x0d] |= eeprom->group3_drive<<4;
2625 if (eeprom->group3_schmitt == IS_SCHMITT)
2626 output[0x0d] |= IS_SCHMITT<<4;
2627 if (eeprom->group3_slew == SLOW_SLEW)
2628 output[0x0d] |= SLOW_SLEW<<4;
2630 output[0x18] = eeprom->chip;
2634 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2637 // calculate checksum
2640 for (i = 0; i < eeprom->size/2-1; i++)
2642 value = output[i*2];
2643 value += output[(i*2)+1] << 8;
2645 checksum = value^checksum;
2646 checksum = (checksum << 1) | (checksum >> 15);
2649 output[eeprom->size-2] = checksum;
2650 output[eeprom->size-1] = checksum >> 8;
2652 return user_area_size;
2656 Decode binary EEPROM image into an ftdi_eeprom structure.
2658 \param ftdi pointer to ftdi_context
2659 \param verbose Decode EEPROM on stdout
2662 \retval -1: something went wrong
2664 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2665 FIXME: Strings are malloc'ed here and should be freed somewhere
2667 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2670 unsigned short checksum, eeprom_checksum, value;
2671 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2673 struct ftdi_eeprom *eeprom;
2674 unsigned char *buf = ftdi->eeprom->buf;
2678 ftdi_error_return(-1,"No context");
2679 if (ftdi->eeprom == NULL)
2680 ftdi_error_return(-1,"No eeprom structure");
2682 eeprom = ftdi->eeprom;
2683 eeprom_size = eeprom->size;
2685 // Addr 02: Vendor ID
2686 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2688 // Addr 04: Product ID
2689 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2691 release = buf[0x06] + (buf[0x07]<<8);
2693 // Addr 08: Config descriptor
2695 // Bit 6: 1 if this device is self powered, 0 if bus powered
2696 // Bit 5: 1 if this device uses remote wakeup
2697 eeprom->self_powered = buf[0x08] & 0x40;
2698 eeprom->remote_wakeup = buf[0x08] & 0x20;
2700 // Addr 09: Max power consumption: max power = value * 2 mA
2701 eeprom->max_power = buf[0x09];
2703 // Addr 0A: Chip configuration
2704 // Bit 7: 0 - reserved
2705 // Bit 6: 0 - reserved
2706 // Bit 5: 0 - reserved
2707 // Bit 4: 1 - Change USB version on BM and 2232C
2708 // Bit 3: 1 - Use the serial number string
2709 // Bit 2: 1 - Enable suspend pull downs for lower power
2710 // Bit 1: 1 - Out EndPoint is Isochronous
2711 // Bit 0: 1 - In EndPoint is Isochronous
2713 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2714 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2715 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2716 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2717 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2719 // Addr 0C: USB version low byte when 0x0A
2720 // Addr 0D: USB version high byte when 0x0A
2721 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2723 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2724 // Addr 0F: Length of manufacturer string
2725 manufacturer_size = buf[0x0F]/2;
2726 if (eeprom->manufacturer)
2727 free(eeprom->manufacturer);
2728 if (manufacturer_size > 0)
2730 eeprom->manufacturer = malloc(manufacturer_size);
2731 if (eeprom->manufacturer)
2733 // Decode manufacturer
2734 i = buf[0x0E] & (eeprom_size -1); // offset
2735 for (j=0;j<manufacturer_size-1;j++)
2737 eeprom->manufacturer[j] = buf[2*j+i+2];
2739 eeprom->manufacturer[j] = '\0';
2742 else eeprom->manufacturer = NULL;
2744 // Addr 10: Offset of the product string + 0x80, calculated later
2745 // Addr 11: Length of product string
2746 if (eeprom->product)
2747 free(eeprom->product);
2748 product_size = buf[0x11]/2;
2749 if (product_size > 0)
2751 eeprom->product = malloc(product_size);
2752 if (eeprom->product)
2754 // Decode product name
2755 i = buf[0x10] & (eeprom_size -1); // offset
2756 for (j=0;j<product_size-1;j++)
2758 eeprom->product[j] = buf[2*j+i+2];
2760 eeprom->product[j] = '\0';
2763 else eeprom->product = NULL;
2765 // Addr 12: Offset of the serial string + 0x80, calculated later
2766 // Addr 13: Length of serial string
2768 free(eeprom->serial);
2769 serial_size = buf[0x13]/2;
2770 if (serial_size > 0)
2772 eeprom->serial = malloc(serial_size);
2776 i = buf[0x12] & (eeprom_size -1); // offset
2777 for (j=0;j<serial_size-1;j++)
2779 eeprom->serial[j] = buf[2*j+i+2];
2781 eeprom->serial[j] = '\0';
2784 else eeprom->serial = NULL;
2789 for (i = 0; i < eeprom_size/2-1; i++)
2792 value += buf[(i*2)+1] << 8;
2794 checksum = value^checksum;
2795 checksum = (checksum << 1) | (checksum >> 15);
2798 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2800 if (eeprom_checksum != checksum)
2802 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2803 ftdi_error_return(-1,"EEPROM checksum error");
2806 eeprom->channel_a_type = 0;
2807 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2811 else if (ftdi->type == TYPE_2232C)
2813 eeprom->channel_a_type = buf[0x00] & 0x7;
2814 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2815 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2816 eeprom->channel_b_type = buf[0x01] & 0x7;
2817 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2818 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2819 eeprom->chip = buf[0x14];
2821 else if (ftdi->type == TYPE_R)
2823 /* TYPE_R flags D2XX, not VCP as all others*/
2824 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2825 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2826 if ( (buf[0x01]&0x40) != 0x40)
2828 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2829 " If this happened with the\n"
2830 " EEPROM programmed by FTDI tools, please report "
2831 "to libftdi@developer.intra2net.com\n");
2833 eeprom->chip = buf[0x16];
2834 // Addr 0B: Invert data lines
2835 // Works only on FT232R, not FT245R, but no way to distinguish
2836 eeprom->invert = buf[0x0B];
2837 // Addr 14: CBUS function: CBUS0, CBUS1
2838 // Addr 15: CBUS function: CBUS2, CBUS3
2839 // Addr 16: CBUS function: CBUS5
2840 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2841 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2842 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2843 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2844 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2846 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2848 eeprom->channel_a_type = buf[0x00] & 0x7;
2849 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2850 eeprom->channel_b_type = buf[0x01] & 0x7;
2851 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2853 if (ftdi->type == TYPE_2232H)
2854 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2856 eeprom->chip = buf[0x18];
2857 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2858 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2859 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2860 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2861 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2862 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2863 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2864 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2865 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2866 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2867 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2868 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2873 char *channel_mode[] = {"UART","245","CPU", "unknown", "OPTO"};
2874 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
2875 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
2876 fprintf(stdout, "Release: 0x%04x\n",release);
2878 if (eeprom->self_powered)
2879 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
2881 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
2882 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
2883 if (eeprom->manufacturer)
2884 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
2885 if (eeprom->product)
2886 fprintf(stdout, "Product: %s\n",eeprom->product);
2888 fprintf(stdout, "Serial: %s\n",eeprom->serial);
2889 fprintf(stdout, "Checksum : %04x\n", checksum);
2890 if (ftdi->type == TYPE_R)
2891 fprintf(stdout, "Internal EEPROM\n");
2892 else if (eeprom->chip >= 0x46)
2893 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
2894 if (eeprom->suspend_dbus7)
2895 fprintf(stdout, "Suspend on DBUS7\n");
2896 if (eeprom->suspend_pull_downs)
2897 fprintf(stdout, "Pull IO pins low during suspend\n");
2898 if (eeprom->remote_wakeup)
2899 fprintf(stdout, "Enable Remote Wake Up\n");
2900 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
2901 if (ftdi->type >= TYPE_2232C)
2902 fprintf(stdout,"Channel A has Mode %s%s%s\n",
2903 channel_mode[eeprom->channel_a_type],
2904 (eeprom->channel_a_driver)?" VCP":"",
2905 (eeprom->high_current_a)?" High Current IO":"");
2906 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R))
2907 fprintf(stdout,"Channel B has Mode %s%s%s\n",
2908 channel_mode[eeprom->channel_b_type],
2909 (eeprom->channel_b_driver)?" VCP":"",
2910 (eeprom->high_current_b)?" High Current IO":"");
2911 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
2912 eeprom->use_usb_version == USE_USB_VERSION_BIT)
2913 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
2915 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
2917 fprintf(stdout,"%s has %d mA drive%s%s\n",
2918 (ftdi->type == TYPE_2232H)?"AL":"A",
2919 (eeprom->group0_drive+1) *4,
2920 (eeprom->group0_schmitt)?" Schmitt Input":"",
2921 (eeprom->group0_slew)?" Slow Slew":"");
2922 fprintf(stdout,"%s has %d mA drive%s%s\n",
2923 (ftdi->type == TYPE_2232H)?"AH":"B",
2924 (eeprom->group1_drive+1) *4,
2925 (eeprom->group1_schmitt)?" Schmitt Input":"",
2926 (eeprom->group1_slew)?" Slow Slew":"");
2927 fprintf(stdout,"%s has %d mA drive%s%s\n",
2928 (ftdi->type == TYPE_2232H)?"BL":"C",
2929 (eeprom->group2_drive+1) *4,
2930 (eeprom->group2_schmitt)?" Schmitt Input":"",
2931 (eeprom->group2_slew)?" Slow Slew":"");
2932 fprintf(stdout,"%s has %d mA drive%s%s\n",
2933 (ftdi->type == TYPE_2232H)?"BH":"D",
2934 (eeprom->group3_drive+1) *4,
2935 (eeprom->group3_schmitt)?" Schmitt Input":"",
2936 (eeprom->group3_slew)?" Slow Slew":"");
2938 if (ftdi->type == TYPE_R)
2940 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
2941 "SLEEP","CLK48","CLK24","CLK12","CLK6",
2942 "IOMODE","BB_WR","BB_RD"
2944 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
2949 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
2950 fprintf(stdout,"Inverted bits:");
2952 if ((eeprom->invert & (1<<i)) == (1<<i))
2953 fprintf(stdout," %s",r_bits[i]);
2954 fprintf(stdout,"\n");
2958 if (eeprom->cbus_function[i]<CBUS_BB)
2959 fprintf(stdout,"C%d Function: %s\n", i,
2960 cbus_mux[eeprom->cbus_function[i]]);
2963 /* FIXME for Uwe: This results in an access above array bounds.
2964 Also I couldn't find documentation about this mode.
2965 fprintf(stdout,"C%d BB Function: %s\n", i,
2968 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
2978 Get a value from the decoded EEPROM structure
2980 \param ftdi pointer to ftdi_context
2981 \param value_name Enum of the value to query
2982 \param value Pointer to store read value
2985 \retval -1: Value doesn't exist
2987 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
2992 *value = ftdi->eeprom->vendor_id;
2995 *value = ftdi->eeprom->product_id;
2998 *value = ftdi->eeprom->self_powered;
3001 *value = ftdi->eeprom->remote_wakeup;
3004 *value = ftdi->eeprom->is_not_pnp;
3007 *value = ftdi->eeprom->suspend_dbus7;
3009 case IN_IS_ISOCHRONOUS:
3010 *value = ftdi->eeprom->in_is_isochronous;
3012 case SUSPEND_PULL_DOWNS:
3013 *value = ftdi->eeprom->suspend_pull_downs;
3016 *value = ftdi->eeprom->use_serial;
3019 *value = ftdi->eeprom->usb_version;
3022 *value = ftdi->eeprom->max_power;
3024 case CHANNEL_A_TYPE:
3025 *value = ftdi->eeprom->channel_a_type;
3027 case CHANNEL_B_TYPE:
3028 *value = ftdi->eeprom->channel_b_type;
3030 case CHANNEL_A_DRIVER:
3031 *value = ftdi->eeprom->channel_a_driver;
3033 case CHANNEL_B_DRIVER:
3034 *value = ftdi->eeprom->channel_b_driver;
3036 case CBUS_FUNCTION_0:
3037 *value = ftdi->eeprom->cbus_function[0];
3039 case CBUS_FUNCTION_1:
3040 *value = ftdi->eeprom->cbus_function[1];
3042 case CBUS_FUNCTION_2:
3043 *value = ftdi->eeprom->cbus_function[2];
3045 case CBUS_FUNCTION_3:
3046 *value = ftdi->eeprom->cbus_function[3];
3048 case CBUS_FUNCTION_4:
3049 *value = ftdi->eeprom->cbus_function[4];
3052 *value = ftdi->eeprom->high_current;
3054 case HIGH_CURRENT_A:
3055 *value = ftdi->eeprom->high_current_a;
3057 case HIGH_CURRENT_B:
3058 *value = ftdi->eeprom->high_current_b;
3061 *value = ftdi->eeprom->invert;
3064 *value = ftdi->eeprom->group0_drive;
3066 case GROUP0_SCHMITT:
3067 *value = ftdi->eeprom->group0_schmitt;
3070 *value = ftdi->eeprom->group0_slew;
3073 *value = ftdi->eeprom->group1_drive;
3075 case GROUP1_SCHMITT:
3076 *value = ftdi->eeprom->group1_schmitt;
3079 *value = ftdi->eeprom->group1_slew;
3082 *value = ftdi->eeprom->group2_drive;
3084 case GROUP2_SCHMITT:
3085 *value = ftdi->eeprom->group2_schmitt;
3088 *value = ftdi->eeprom->group2_slew;
3091 *value = ftdi->eeprom->group3_drive;
3093 case GROUP3_SCHMITT:
3094 *value = ftdi->eeprom->group3_schmitt;
3097 *value = ftdi->eeprom->group3_slew;
3100 *value = ftdi->eeprom->chip;
3103 *value = ftdi->eeprom->size;
3106 ftdi_error_return(-1, "Request for unknown EEPROM value");
3112 Set a value in the decoded EEPROM Structure
3113 No parameter checking is performed
3115 \param ftdi pointer to ftdi_context
3116 \param value_name Enum of the value to query
3120 \retval -1: Value doesn't exist
3121 \retval -2: Value not user settable
3123 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3128 ftdi->eeprom->vendor_id = value;
3131 ftdi->eeprom->product_id = value;
3134 ftdi->eeprom->self_powered = value;
3137 ftdi->eeprom->remote_wakeup = value;
3140 ftdi->eeprom->is_not_pnp = value;
3143 ftdi->eeprom->suspend_dbus7 = value;
3145 case IN_IS_ISOCHRONOUS:
3146 ftdi->eeprom->in_is_isochronous = value;
3148 case SUSPEND_PULL_DOWNS:
3149 ftdi->eeprom->suspend_pull_downs = value;
3152 ftdi->eeprom->use_serial = value;
3155 ftdi->eeprom->usb_version = value;
3158 ftdi->eeprom->max_power = value;
3160 case CHANNEL_A_TYPE:
3161 ftdi->eeprom->channel_a_type = value;
3163 case CHANNEL_B_TYPE:
3164 ftdi->eeprom->channel_b_type = value;
3166 case CHANNEL_A_DRIVER:
3167 ftdi->eeprom->channel_a_driver = value;
3169 case CHANNEL_B_DRIVER:
3170 ftdi->eeprom->channel_b_driver = value;
3172 case CBUS_FUNCTION_0:
3173 ftdi->eeprom->cbus_function[0] = value;
3175 case CBUS_FUNCTION_1:
3176 ftdi->eeprom->cbus_function[1] = value;
3178 case CBUS_FUNCTION_2:
3179 ftdi->eeprom->cbus_function[2] = value;
3181 case CBUS_FUNCTION_3:
3182 ftdi->eeprom->cbus_function[3] = value;
3184 case CBUS_FUNCTION_4:
3185 ftdi->eeprom->cbus_function[4] = value;
3188 ftdi->eeprom->high_current = value;
3190 case HIGH_CURRENT_A:
3191 ftdi->eeprom->high_current_a = value;
3193 case HIGH_CURRENT_B:
3194 ftdi->eeprom->high_current_b = value;
3197 ftdi->eeprom->invert = value;
3200 ftdi->eeprom->group0_drive = value;
3202 case GROUP0_SCHMITT:
3203 ftdi->eeprom->group0_schmitt = value;
3206 ftdi->eeprom->group0_slew = value;
3209 ftdi->eeprom->group1_drive = value;
3211 case GROUP1_SCHMITT:
3212 ftdi->eeprom->group1_schmitt = value;
3215 ftdi->eeprom->group1_slew = value;
3218 ftdi->eeprom->group2_drive = value;
3220 case GROUP2_SCHMITT:
3221 ftdi->eeprom->group2_schmitt = value;
3224 ftdi->eeprom->group2_slew = value;
3227 ftdi->eeprom->group3_drive = value;
3229 case GROUP3_SCHMITT:
3230 ftdi->eeprom->group3_schmitt = value;
3233 ftdi->eeprom->group3_slew = value;
3236 ftdi->eeprom->chip = value;
3239 ftdi_error_return(-2, "EEPROM Value can't be changed");
3241 ftdi_error_return(-1, "Request to unknown EEPROM value");
3246 /** Get the read-only buffer to the binary EEPROM content
3248 \param ftdi pointer to ftdi_context
3249 \param buf buffer to receive EEPROM content
3250 \param size Size of receiving buffer
3253 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3255 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3257 if (!ftdi || !(ftdi->eeprom))
3258 ftdi_error_return(-1, "No appropriate structure");
3259 memcpy(buf, ftdi->eeprom->buf, size);
3264 Read eeprom location
3266 \param ftdi pointer to ftdi_context
3267 \param eeprom_addr Address of eeprom location to be read
3268 \param eeprom_val Pointer to store read eeprom location
3271 \retval -1: read failed
3272 \retval -2: USB device unavailable
3274 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3276 if (ftdi == NULL || ftdi->usb_dev == NULL)
3277 ftdi_error_return(-2, "USB device unavailable");
3279 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)
3280 ftdi_error_return(-1, "reading eeprom failed");
3288 \param ftdi pointer to ftdi_context
3291 \retval -1: read failed
3292 \retval -2: USB device unavailable
3294 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3299 if (ftdi == NULL || ftdi->usb_dev == NULL)
3300 ftdi_error_return(-2, "USB device unavailable");
3301 buf = ftdi->eeprom->buf;
3303 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3305 if (libusb_control_transfer(
3306 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3307 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3308 ftdi_error_return(-1, "reading eeprom failed");
3311 if (ftdi->type == TYPE_R)
3312 ftdi->eeprom->size = 0x80;
3313 /* Guesses size of eeprom by comparing halves
3314 - will not work with blank eeprom */
3315 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3316 ftdi->eeprom->size = -1;
3317 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3318 ftdi->eeprom->size = 0x80;
3319 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3320 ftdi->eeprom->size = 0x40;
3322 ftdi->eeprom->size = 0x100;
3327 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3328 Function is only used internally
3331 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3333 return ((value & 1) << 1) |
3334 ((value & 2) << 5) |
3335 ((value & 4) >> 2) |
3336 ((value & 8) << 4) |
3337 ((value & 16) >> 1) |
3338 ((value & 32) >> 1) |
3339 ((value & 64) >> 4) |
3340 ((value & 128) >> 2);
3344 Read the FTDIChip-ID from R-type devices
3346 \param ftdi pointer to ftdi_context
3347 \param chipid Pointer to store FTDIChip-ID
3350 \retval -1: read failed
3351 \retval -2: USB device unavailable
3353 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3355 unsigned int a = 0, b = 0;
3357 if (ftdi == NULL || ftdi->usb_dev == NULL)
3358 ftdi_error_return(-2, "USB device unavailable");
3360 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)
3362 a = a << 8 | a >> 8;
3363 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)
3365 b = b << 8 | b >> 8;
3366 a = (a << 16) | (b & 0xFFFF);
3367 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3368 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3369 *chipid = a ^ 0xa5f0f7d1;
3374 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3378 Write eeprom location
3380 \param ftdi pointer to ftdi_context
3381 \param eeprom_addr Address of eeprom location to be written
3382 \param eeprom_val Value to be written
3385 \retval -1: write failed
3386 \retval -2: USB device unavailable
3387 \retval -3: Invalid access to checksum protected area below 0x80
3388 \retval -4: Device can't access unprotected area
3389 \retval -5: Reading chip type failed
3391 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3392 unsigned short eeprom_val)
3394 int chip_type_location;
3395 unsigned short chip_type;
3397 if (ftdi == NULL || ftdi->usb_dev == NULL)
3398 ftdi_error_return(-2, "USB device unavailable");
3400 if (eeprom_addr <0x80)
3401 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3408 chip_type_location = 0x14;
3412 chip_type_location = 0x18;
3415 ftdi_error_return(-4, "Device can't access unprotected area");
3418 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3419 ftdi_error_return(-5, "Reading failed failed");
3420 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3421 if ((chip_type & 0xff) != 0x66)
3423 ftdi_error_return(-6, "EEPROM is not of 93x66");
3426 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3427 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3428 NULL, 0, ftdi->usb_write_timeout) != 0)
3429 ftdi_error_return(-1, "unable to write eeprom");
3437 \param ftdi pointer to ftdi_context
3440 \retval -1: read failed
3441 \retval -2: USB device unavailable
3443 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3445 unsigned short usb_val, status;
3447 unsigned char *eeprom;
3449 if (ftdi == NULL || ftdi->usb_dev == NULL)
3450 ftdi_error_return(-2, "USB device unavailable");
3451 eeprom = ftdi->eeprom->buf;
3453 /* These commands were traced while running MProg */
3454 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3456 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3458 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3461 for (i = 0; i < ftdi->eeprom->size/2; i++)
3463 usb_val = eeprom[i*2];
3464 usb_val += eeprom[(i*2)+1] << 8;
3465 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3466 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3467 NULL, 0, ftdi->usb_write_timeout) < 0)
3468 ftdi_error_return(-1, "unable to write eeprom");
3477 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3479 \param ftdi pointer to ftdi_context
3482 \retval -1: erase failed
3483 \retval -2: USB device unavailable
3484 \retval -3: Writing magic failed
3485 \retval -4: Read EEPROM failed
3486 \retval -5: Unexpected EEPROM value
3488 #define MAGIC 0x55aa
3489 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3491 unsigned short eeprom_value;
3492 if (ftdi == NULL || ftdi->usb_dev == NULL)
3493 ftdi_error_return(-2, "USB device unavailable");
3495 if (ftdi->type == TYPE_R)
3497 ftdi->eeprom->chip = 0;
3501 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3502 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3503 ftdi_error_return(-1, "unable to erase eeprom");
3506 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3507 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3508 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3509 Chip is 93x66 if magic is only read at word position 0xc0*/
3510 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3511 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3512 NULL, 0, ftdi->usb_write_timeout) != 0)
3513 ftdi_error_return(-3, "Writing magic failed");
3514 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3515 ftdi_error_return(-4, "Reading failed failed");
3516 if (eeprom_value == MAGIC)
3518 ftdi->eeprom->chip = 0x46;
3522 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3523 ftdi_error_return(-4, "Reading failed failed");
3524 if (eeprom_value == MAGIC)
3525 ftdi->eeprom->chip = 0x56;
3528 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3529 ftdi_error_return(-4, "Reading failed failed");
3530 if (eeprom_value == MAGIC)
3531 ftdi->eeprom->chip = 0x66;
3534 ftdi->eeprom->chip = -1;
3538 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3539 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3540 ftdi_error_return(-1, "unable to erase eeprom");
3545 Get string representation for last error code
3547 \param ftdi pointer to ftdi_context
3549 \retval Pointer to error string
3551 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3556 return ftdi->error_str;
3559 /* @} end of doxygen libftdi group */