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;
102 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
104 ftdi->error_str = NULL;
107 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
108 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
109 ftdi->eeprom = eeprom;
111 /* All fine. Now allocate the readbuffer */
112 return ftdi_read_data_set_chunksize(ftdi, 4096);
116 Allocate and initialize a new ftdi_context
118 \return a pointer to a new ftdi_context, or NULL on failure
120 struct ftdi_context *ftdi_new(void)
122 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
129 if (ftdi_init(ftdi) != 0)
139 Open selected channels on a chip, otherwise use first channel.
141 \param ftdi pointer to ftdi_context
142 \param interface Interface to use for FT2232C/2232H/4232H chips.
145 \retval -1: unknown interface
146 \retval -2: USB device unavailable
148 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
151 ftdi_error_return(-2, "USB device unavailable");
157 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
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 // Set default interface on dual/quad type chips
567 ftdi->index = INTERFACE_A;
573 // Determine maximum packet size
574 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
576 if (ftdi_set_baudrate (ftdi, 9600) != 0)
578 ftdi_usb_close_internal (ftdi);
579 ftdi_error_return(-7, "set baudrate failed");
582 ftdi_error_return(0, "all fine");
586 Opens the first device with a given vendor and product ids.
588 \param ftdi pointer to ftdi_context
589 \param vendor Vendor ID
590 \param product Product ID
592 \retval same as ftdi_usb_open_desc()
594 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
596 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
600 Opens the first device with a given, vendor id, product id,
601 description and serial.
603 \param ftdi pointer to ftdi_context
604 \param vendor Vendor ID
605 \param product Product ID
606 \param description Description to search for. Use NULL if not needed.
607 \param serial Serial to search for. Use NULL if not needed.
610 \retval -3: usb device not found
611 \retval -4: unable to open device
612 \retval -5: unable to claim device
613 \retval -6: reset failed
614 \retval -7: set baudrate failed
615 \retval -8: get product description failed
616 \retval -9: get serial number failed
617 \retval -11: libusb_init() failed
618 \retval -12: libusb_get_device_list() failed
619 \retval -13: libusb_get_device_descriptor() failed
621 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
622 const char* description, const char* serial)
624 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
628 Opens the index-th device with a given, vendor id, product id,
629 description and serial.
631 \param ftdi pointer to ftdi_context
632 \param vendor Vendor ID
633 \param product Product ID
634 \param description Description to search for. Use NULL if not needed.
635 \param serial Serial to search for. Use NULL if not needed.
636 \param index Number of matching device to open if there are more than one, starts with 0.
639 \retval -1: usb_find_busses() failed
640 \retval -2: usb_find_devices() failed
641 \retval -3: usb device not found
642 \retval -4: unable to open device
643 \retval -5: unable to claim device
644 \retval -6: reset failed
645 \retval -7: set baudrate failed
646 \retval -8: get product description failed
647 \retval -9: get serial number failed
648 \retval -10: unable to close device
649 \retval -11: ftdi context invalid
651 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
652 const char* description, const char* serial, unsigned int index)
655 libusb_device **devs;
659 if (libusb_init(&ftdi->usb_ctx) < 0)
660 ftdi_error_return(-11, "libusb_init() failed");
663 ftdi_error_return(-11, "ftdi context invalid");
665 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
666 ftdi_error_return(-12, "libusb_get_device_list() failed");
668 while ((dev = devs[i++]) != NULL)
670 struct libusb_device_descriptor desc;
673 if (libusb_get_device_descriptor(dev, &desc) < 0)
674 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
676 if (desc.idVendor == vendor && desc.idProduct == product)
678 if (libusb_open(dev, &ftdi->usb_dev) < 0)
679 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
681 if (description != NULL)
683 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
685 libusb_close (ftdi->usb_dev);
686 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
688 if (strncmp(string, description, sizeof(string)) != 0)
690 libusb_close (ftdi->usb_dev);
696 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
698 ftdi_usb_close_internal (ftdi);
699 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
701 if (strncmp(string, serial, sizeof(string)) != 0)
703 ftdi_usb_close_internal (ftdi);
708 ftdi_usb_close_internal (ftdi);
716 res = ftdi_usb_open_dev(ftdi, dev);
717 libusb_free_device_list(devs,1);
723 ftdi_error_return_free_device_list(-3, "device not found", devs);
727 Opens the ftdi-device described by a description-string.
728 Intended to be used for parsing a device-description given as commandline argument.
730 \param ftdi pointer to ftdi_context
731 \param description NULL-terminated description-string, using this format:
732 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
733 \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")
734 \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
735 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
737 \note The description format may be extended in later versions.
740 \retval -1: libusb_init() failed
741 \retval -2: libusb_get_device_list() failed
742 \retval -3: usb device not found
743 \retval -4: unable to open device
744 \retval -5: unable to claim device
745 \retval -6: reset failed
746 \retval -7: set baudrate failed
747 \retval -8: get product description failed
748 \retval -9: get serial number failed
749 \retval -10: unable to close device
750 \retval -11: illegal description format
751 \retval -12: ftdi context invalid
753 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
756 ftdi_error_return(-12, "ftdi context invalid");
758 if (description[0] == 0 || description[1] != ':')
759 ftdi_error_return(-11, "illegal description format");
761 if (description[0] == 'd')
764 libusb_device **devs;
765 unsigned int bus_number, device_address;
768 if (libusb_init (&ftdi->usb_ctx) < 0)
769 ftdi_error_return(-1, "libusb_init() failed");
771 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
772 ftdi_error_return(-2, "libusb_get_device_list() failed");
774 /* XXX: This doesn't handle symlinks/odd paths/etc... */
775 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
776 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
778 while ((dev = devs[i++]) != NULL)
781 if (bus_number == libusb_get_bus_number (dev)
782 && device_address == libusb_get_device_address (dev))
784 ret = ftdi_usb_open_dev(ftdi, dev);
785 libusb_free_device_list(devs,1);
791 ftdi_error_return_free_device_list(-3, "device not found", devs);
793 else if (description[0] == 'i' || description[0] == 's')
796 unsigned int product;
797 unsigned int index=0;
798 const char *serial=NULL;
799 const char *startp, *endp;
802 startp=description+2;
803 vendor=strtoul((char*)startp,(char**)&endp,0);
804 if (*endp != ':' || endp == startp || errno != 0)
805 ftdi_error_return(-11, "illegal description format");
808 product=strtoul((char*)startp,(char**)&endp,0);
809 if (endp == startp || errno != 0)
810 ftdi_error_return(-11, "illegal description format");
812 if (description[0] == 'i' && *endp != 0)
814 /* optional index field in i-mode */
816 ftdi_error_return(-11, "illegal description format");
819 index=strtoul((char*)startp,(char**)&endp,0);
820 if (*endp != 0 || endp == startp || errno != 0)
821 ftdi_error_return(-11, "illegal description format");
823 if (description[0] == 's')
826 ftdi_error_return(-11, "illegal description format");
828 /* rest of the description is the serial */
832 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
836 ftdi_error_return(-11, "illegal description format");
841 Resets the ftdi device.
843 \param ftdi pointer to ftdi_context
846 \retval -1: FTDI reset failed
847 \retval -2: USB device unavailable
849 int ftdi_usb_reset(struct ftdi_context *ftdi)
851 if (ftdi == NULL || ftdi->usb_dev == NULL)
852 ftdi_error_return(-2, "USB device unavailable");
854 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
855 SIO_RESET_REQUEST, SIO_RESET_SIO,
856 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
857 ftdi_error_return(-1,"FTDI reset failed");
859 // Invalidate data in the readbuffer
860 ftdi->readbuffer_offset = 0;
861 ftdi->readbuffer_remaining = 0;
867 Clears the read buffer on the chip and the internal read buffer.
869 \param ftdi pointer to ftdi_context
872 \retval -1: read buffer purge failed
873 \retval -2: USB device unavailable
875 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
877 if (ftdi == NULL || ftdi->usb_dev == NULL)
878 ftdi_error_return(-2, "USB device unavailable");
880 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
881 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
882 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
883 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
885 // Invalidate data in the readbuffer
886 ftdi->readbuffer_offset = 0;
887 ftdi->readbuffer_remaining = 0;
893 Clears the write buffer on the chip.
895 \param ftdi pointer to ftdi_context
898 \retval -1: write buffer purge failed
899 \retval -2: USB device unavailable
901 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
903 if (ftdi == NULL || ftdi->usb_dev == NULL)
904 ftdi_error_return(-2, "USB device unavailable");
906 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
907 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
908 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
909 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
915 Clears the buffers on the chip and the internal read buffer.
917 \param ftdi pointer to ftdi_context
920 \retval -1: read buffer purge failed
921 \retval -2: write buffer purge failed
922 \retval -3: USB device unavailable
924 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
928 if (ftdi == NULL || ftdi->usb_dev == NULL)
929 ftdi_error_return(-3, "USB device unavailable");
931 result = ftdi_usb_purge_rx_buffer(ftdi);
935 result = ftdi_usb_purge_tx_buffer(ftdi);
945 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
947 \param ftdi pointer to ftdi_context
950 \retval -1: usb_release failed
951 \retval -3: ftdi context invalid
953 int ftdi_usb_close(struct ftdi_context *ftdi)
958 ftdi_error_return(-3, "ftdi context invalid");
960 if (ftdi->usb_dev != NULL)
961 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
964 ftdi_usb_close_internal (ftdi);
970 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
971 Function is only used internally
974 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
975 unsigned short *value, unsigned short *index)
977 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
978 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
979 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
980 int divisor, best_divisor, best_baud, best_baud_diff;
981 unsigned long encoded_divisor;
990 divisor = 24000000 / baudrate;
992 if (ftdi->type == TYPE_AM)
994 // Round down to supported fraction (AM only)
995 divisor -= am_adjust_dn[divisor & 7];
998 // Try this divisor and the one above it (because division rounds down)
1002 for (i = 0; i < 2; i++)
1004 int try_divisor = divisor + i;
1008 // Round up to supported divisor value
1009 if (try_divisor <= 8)
1011 // Round up to minimum supported divisor
1014 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1016 // BM doesn't support divisors 9 through 11 inclusive
1019 else if (divisor < 16)
1021 // AM doesn't support divisors 9 through 15 inclusive
1026 if (ftdi->type == TYPE_AM)
1028 // Round up to supported fraction (AM only)
1029 try_divisor += am_adjust_up[try_divisor & 7];
1030 if (try_divisor > 0x1FFF8)
1032 // Round down to maximum supported divisor value (for AM)
1033 try_divisor = 0x1FFF8;
1038 if (try_divisor > 0x1FFFF)
1040 // Round down to maximum supported divisor value (for BM)
1041 try_divisor = 0x1FFFF;
1045 // Get estimated baud rate (to nearest integer)
1046 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1047 // Get absolute difference from requested baud rate
1048 if (baud_estimate < baudrate)
1050 baud_diff = baudrate - baud_estimate;
1054 baud_diff = baud_estimate - baudrate;
1056 if (i == 0 || baud_diff < best_baud_diff)
1058 // Closest to requested baud rate so far
1059 best_divisor = try_divisor;
1060 best_baud = baud_estimate;
1061 best_baud_diff = baud_diff;
1064 // Spot on! No point trying
1069 // Encode the best divisor value
1070 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1071 // Deal with special cases for encoded value
1072 if (encoded_divisor == 1)
1074 encoded_divisor = 0; // 3000000 baud
1076 else if (encoded_divisor == 0x4001)
1078 encoded_divisor = 1; // 2000000 baud (BM only)
1080 // Split into "value" and "index" values
1081 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1082 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1084 *index = (unsigned short)(encoded_divisor >> 8);
1086 *index |= ftdi->index;
1089 *index = (unsigned short)(encoded_divisor >> 16);
1091 // Return the nearest baud rate
1096 Sets the chip baud rate
1098 \param ftdi pointer to ftdi_context
1099 \param baudrate baud rate to set
1102 \retval -1: invalid baudrate
1103 \retval -2: setting baudrate failed
1104 \retval -3: USB device unavailable
1106 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1108 unsigned short value, index;
1109 int actual_baudrate;
1111 if (ftdi == NULL || ftdi->usb_dev == NULL)
1112 ftdi_error_return(-3, "USB device unavailable");
1114 if (ftdi->bitbang_enabled)
1116 baudrate = baudrate*4;
1119 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1120 if (actual_baudrate <= 0)
1121 ftdi_error_return (-1, "Silly baudrate <= 0.");
1123 // Check within tolerance (about 5%)
1124 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1125 || ((actual_baudrate < baudrate)
1126 ? (actual_baudrate * 21 < baudrate * 20)
1127 : (baudrate * 21 < actual_baudrate * 20)))
1128 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1130 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1131 SIO_SET_BAUDRATE_REQUEST, value,
1132 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1133 ftdi_error_return (-2, "Setting new baudrate failed");
1135 ftdi->baudrate = baudrate;
1140 Set (RS232) line characteristics.
1141 The break type can only be set via ftdi_set_line_property2()
1142 and defaults to "off".
1144 \param ftdi pointer to ftdi_context
1145 \param bits Number of bits
1146 \param sbit Number of stop bits
1147 \param parity Parity mode
1150 \retval -1: Setting line property failed
1152 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1153 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1155 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1159 Set (RS232) line characteristics
1161 \param ftdi pointer to ftdi_context
1162 \param bits Number of bits
1163 \param sbit Number of stop bits
1164 \param parity Parity mode
1165 \param break_type Break type
1168 \retval -1: Setting line property failed
1169 \retval -2: USB device unavailable
1171 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1172 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1173 enum ftdi_break_type break_type)
1175 unsigned short value = bits;
1177 if (ftdi == NULL || ftdi->usb_dev == NULL)
1178 ftdi_error_return(-2, "USB device unavailable");
1183 value |= (0x00 << 8);
1186 value |= (0x01 << 8);
1189 value |= (0x02 << 8);
1192 value |= (0x03 << 8);
1195 value |= (0x04 << 8);
1202 value |= (0x00 << 11);
1205 value |= (0x01 << 11);
1208 value |= (0x02 << 11);
1215 value |= (0x00 << 14);
1218 value |= (0x01 << 14);
1222 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1223 SIO_SET_DATA_REQUEST, value,
1224 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1225 ftdi_error_return (-1, "Setting new line property failed");
1231 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1233 \param ftdi pointer to ftdi_context
1234 \param buf Buffer with the data
1235 \param size Size of the buffer
1237 \retval -666: USB device unavailable
1238 \retval <0: error code from usb_bulk_write()
1239 \retval >0: number of bytes written
1241 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1246 if (ftdi == NULL || ftdi->usb_dev == NULL)
1247 ftdi_error_return(-666, "USB device unavailable");
1249 while (offset < size)
1251 int write_size = ftdi->writebuffer_chunksize;
1253 if (offset+write_size > size)
1254 write_size = size-offset;
1256 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1257 ftdi_error_return(-1, "usb bulk write failed");
1259 offset += actual_length;
1265 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1267 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1268 struct ftdi_context *ftdi = tc->ftdi;
1269 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1271 packet_size = ftdi->max_packet_size;
1273 actual_length = transfer->actual_length;
1275 if (actual_length > 2)
1277 // skip FTDI status bytes.
1278 // Maybe stored in the future to enable modem use
1279 num_of_chunks = actual_length / packet_size;
1280 chunk_remains = actual_length % packet_size;
1281 //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);
1283 ftdi->readbuffer_offset += 2;
1286 if (actual_length > packet_size - 2)
1288 for (i = 1; i < num_of_chunks; i++)
1289 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1290 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1292 if (chunk_remains > 2)
1294 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1295 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1297 actual_length -= 2*num_of_chunks;
1300 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1303 if (actual_length > 0)
1305 // data still fits in buf?
1306 if (tc->offset + actual_length <= tc->size)
1308 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1309 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1310 tc->offset += actual_length;
1312 ftdi->readbuffer_offset = 0;
1313 ftdi->readbuffer_remaining = 0;
1315 /* Did we read exactly the right amount of bytes? */
1316 if (tc->offset == tc->size)
1318 //printf("read_data exact rem %d offset %d\n",
1319 //ftdi->readbuffer_remaining, offset);
1326 // only copy part of the data or size <= readbuffer_chunksize
1327 int part_size = tc->size - tc->offset;
1328 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1329 tc->offset += part_size;
1331 ftdi->readbuffer_offset += part_size;
1332 ftdi->readbuffer_remaining = actual_length - part_size;
1334 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1335 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1341 ret = libusb_submit_transfer (transfer);
1347 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1349 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1350 struct ftdi_context *ftdi = tc->ftdi;
1352 tc->offset += transfer->actual_length;
1354 if (tc->offset == tc->size)
1360 int write_size = ftdi->writebuffer_chunksize;
1363 if (tc->offset + write_size > tc->size)
1364 write_size = tc->size - tc->offset;
1366 transfer->length = write_size;
1367 transfer->buffer = tc->buf + tc->offset;
1368 ret = libusb_submit_transfer (transfer);
1376 Writes data to the chip. Does not wait for completion of the transfer
1377 nor does it make sure that the transfer was successful.
1379 Use libusb 1.0 asynchronous API.
1381 \param ftdi pointer to ftdi_context
1382 \param buf Buffer with the data
1383 \param size Size of the buffer
1385 \retval NULL: Some error happens when submit transfer
1386 \retval !NULL: Pointer to a ftdi_transfer_control
1389 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1391 struct ftdi_transfer_control *tc;
1392 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1393 int write_size, ret;
1395 if (ftdi == NULL || ftdi->usb_dev == NULL)
1397 libusb_free_transfer(transfer);
1401 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1403 if (!tc || !transfer)
1412 if (size < ftdi->writebuffer_chunksize)
1415 write_size = ftdi->writebuffer_chunksize;
1417 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1418 write_size, ftdi_write_data_cb, tc,
1419 ftdi->usb_write_timeout);
1420 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1422 ret = libusb_submit_transfer(transfer);
1425 libusb_free_transfer(transfer);
1427 tc->transfer = NULL;
1430 tc->transfer = transfer;
1436 Reads data from the chip. Does not wait for completion of the transfer
1437 nor does it make sure that the transfer was successful.
1439 Use libusb 1.0 asynchronous API.
1441 \param ftdi pointer to ftdi_context
1442 \param buf Buffer with the data
1443 \param size Size of the buffer
1445 \retval NULL: Some error happens when submit transfer
1446 \retval !NULL: Pointer to a ftdi_transfer_control
1449 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1451 struct ftdi_transfer_control *tc;
1452 struct libusb_transfer *transfer;
1455 if (ftdi == NULL || ftdi->usb_dev == NULL)
1458 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1466 if (size <= ftdi->readbuffer_remaining)
1468 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1471 ftdi->readbuffer_remaining -= size;
1472 ftdi->readbuffer_offset += size;
1474 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1478 tc->transfer = NULL;
1483 if (ftdi->readbuffer_remaining != 0)
1485 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1487 tc->offset = ftdi->readbuffer_remaining;
1492 transfer = libusb_alloc_transfer(0);
1499 ftdi->readbuffer_remaining = 0;
1500 ftdi->readbuffer_offset = 0;
1502 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);
1503 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1505 ret = libusb_submit_transfer(transfer);
1508 libusb_free_transfer(transfer);
1512 tc->transfer = transfer;
1518 Wait for completion of the transfer.
1520 Use libusb 1.0 asynchronous API.
1522 \param tc pointer to ftdi_transfer_control
1524 \retval < 0: Some error happens
1525 \retval >= 0: Data size transferred
1528 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1532 while (!tc->completed)
1534 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1537 if (ret == LIBUSB_ERROR_INTERRUPTED)
1539 libusb_cancel_transfer(tc->transfer);
1540 while (!tc->completed)
1541 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1543 libusb_free_transfer(tc->transfer);
1551 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1552 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1556 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1558 libusb_free_transfer(tc->transfer);
1565 Configure write buffer chunk size.
1568 \param ftdi pointer to ftdi_context
1569 \param chunksize Chunk size
1572 \retval -1: ftdi context invalid
1574 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1577 ftdi_error_return(-1, "ftdi context invalid");
1579 ftdi->writebuffer_chunksize = chunksize;
1584 Get write buffer chunk size.
1586 \param ftdi pointer to ftdi_context
1587 \param chunksize Pointer to store chunk size in
1590 \retval -1: ftdi context invalid
1592 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1595 ftdi_error_return(-1, "ftdi context invalid");
1597 *chunksize = ftdi->writebuffer_chunksize;
1602 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1604 Automatically strips the two modem status bytes transfered during every read.
1606 \param ftdi pointer to ftdi_context
1607 \param buf Buffer to store data in
1608 \param size Size of the buffer
1610 \retval -666: USB device unavailable
1611 \retval <0: error code from libusb_bulk_transfer()
1612 \retval 0: no data was available
1613 \retval >0: number of bytes read
1616 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1618 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1619 int packet_size = ftdi->max_packet_size;
1620 int actual_length = 1;
1622 if (ftdi == NULL || ftdi->usb_dev == NULL)
1623 ftdi_error_return(-666, "USB device unavailable");
1625 // Packet size sanity check (avoid division by zero)
1626 if (packet_size == 0)
1627 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1629 // everything we want is still in the readbuffer?
1630 if (size <= ftdi->readbuffer_remaining)
1632 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1635 ftdi->readbuffer_remaining -= size;
1636 ftdi->readbuffer_offset += size;
1638 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1642 // something still in the readbuffer, but not enough to satisfy 'size'?
1643 if (ftdi->readbuffer_remaining != 0)
1645 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1648 offset += ftdi->readbuffer_remaining;
1650 // do the actual USB read
1651 while (offset < size && actual_length > 0)
1653 ftdi->readbuffer_remaining = 0;
1654 ftdi->readbuffer_offset = 0;
1655 /* returns how much received */
1656 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1658 ftdi_error_return(ret, "usb bulk read failed");
1660 if (actual_length > 2)
1662 // skip FTDI status bytes.
1663 // Maybe stored in the future to enable modem use
1664 num_of_chunks = actual_length / packet_size;
1665 chunk_remains = actual_length % packet_size;
1666 //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);
1668 ftdi->readbuffer_offset += 2;
1671 if (actual_length > packet_size - 2)
1673 for (i = 1; i < num_of_chunks; i++)
1674 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1675 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1677 if (chunk_remains > 2)
1679 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1680 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1682 actual_length -= 2*num_of_chunks;
1685 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1688 else if (actual_length <= 2)
1690 // no more data to read?
1693 if (actual_length > 0)
1695 // data still fits in buf?
1696 if (offset+actual_length <= size)
1698 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1699 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1700 offset += actual_length;
1702 /* Did we read exactly the right amount of bytes? */
1704 //printf("read_data exact rem %d offset %d\n",
1705 //ftdi->readbuffer_remaining, offset);
1710 // only copy part of the data or size <= readbuffer_chunksize
1711 int part_size = size-offset;
1712 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1714 ftdi->readbuffer_offset += part_size;
1715 ftdi->readbuffer_remaining = actual_length-part_size;
1716 offset += part_size;
1718 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1719 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1730 Configure read buffer chunk size.
1733 Automatically reallocates the buffer.
1735 \param ftdi pointer to ftdi_context
1736 \param chunksize Chunk size
1739 \retval -1: ftdi context invalid
1741 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1743 unsigned char *new_buf;
1746 ftdi_error_return(-1, "ftdi context invalid");
1748 // Invalidate all remaining data
1749 ftdi->readbuffer_offset = 0;
1750 ftdi->readbuffer_remaining = 0;
1752 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1753 which is defined in libusb-1.0. Otherwise, each USB read request will
1754 be divided into multiple URBs. This will cause issues on Linux kernel
1755 older than 2.6.32. */
1756 if (chunksize > 16384)
1760 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1761 ftdi_error_return(-1, "out of memory for readbuffer");
1763 ftdi->readbuffer = new_buf;
1764 ftdi->readbuffer_chunksize = chunksize;
1770 Get read buffer chunk size.
1772 \param ftdi pointer to ftdi_context
1773 \param chunksize Pointer to store chunk size in
1776 \retval -1: FTDI context invalid
1778 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1781 ftdi_error_return(-1, "FTDI context invalid");
1783 *chunksize = ftdi->readbuffer_chunksize;
1789 Enable bitbang mode.
1791 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1793 \param ftdi pointer to ftdi_context
1794 \param bitmask Bitmask to configure lines.
1795 HIGH/ON value configures a line as output.
1798 \retval -1: can't enable bitbang mode
1799 \retval -2: USB device unavailable
1801 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1803 unsigned short usb_val;
1805 if (ftdi == NULL || ftdi->usb_dev == NULL)
1806 ftdi_error_return(-2, "USB device unavailable");
1808 usb_val = bitmask; // low byte: bitmask
1809 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1810 usb_val |= (ftdi->bitbang_mode << 8);
1812 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1813 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1814 NULL, 0, ftdi->usb_write_timeout) < 0)
1815 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1817 ftdi->bitbang_enabled = 1;
1822 Disable bitbang mode.
1824 \param ftdi pointer to ftdi_context
1827 \retval -1: can't disable bitbang mode
1828 \retval -2: USB device unavailable
1830 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1832 if (ftdi == NULL || ftdi->usb_dev == NULL)
1833 ftdi_error_return(-2, "USB device unavailable");
1835 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)
1836 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1838 ftdi->bitbang_enabled = 0;
1843 Enable/disable bitbang modes.
1845 \param ftdi pointer to ftdi_context
1846 \param bitmask Bitmask to configure lines.
1847 HIGH/ON value configures a line as output.
1848 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1851 \retval -1: can't enable bitbang mode
1852 \retval -2: USB device unavailable
1854 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1856 unsigned short usb_val;
1858 if (ftdi == NULL || ftdi->usb_dev == NULL)
1859 ftdi_error_return(-2, "USB device unavailable");
1861 usb_val = bitmask; // low byte: bitmask
1862 usb_val |= (mode << 8);
1863 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)
1864 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1866 ftdi->bitbang_mode = mode;
1867 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1872 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1874 \param ftdi pointer to ftdi_context
1875 \param pins Pointer to store pins into
1878 \retval -1: read pins failed
1879 \retval -2: USB device unavailable
1881 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1883 if (ftdi == NULL || ftdi->usb_dev == NULL)
1884 ftdi_error_return(-2, "USB device unavailable");
1886 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)
1887 ftdi_error_return(-1, "read pins failed");
1895 The FTDI chip keeps data in the internal buffer for a specific
1896 amount of time if the buffer is not full yet to decrease
1897 load on the usb bus.
1899 \param ftdi pointer to ftdi_context
1900 \param latency Value between 1 and 255
1903 \retval -1: latency out of range
1904 \retval -2: unable to set latency timer
1905 \retval -3: USB device unavailable
1907 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1909 unsigned short usb_val;
1912 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1914 if (ftdi == NULL || ftdi->usb_dev == NULL)
1915 ftdi_error_return(-3, "USB device unavailable");
1918 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)
1919 ftdi_error_return(-2, "unable to set latency timer");
1927 \param ftdi pointer to ftdi_context
1928 \param latency Pointer to store latency value in
1931 \retval -1: unable to get latency timer
1932 \retval -2: USB device unavailable
1934 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1936 unsigned short usb_val;
1938 if (ftdi == NULL || ftdi->usb_dev == NULL)
1939 ftdi_error_return(-2, "USB device unavailable");
1941 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)
1942 ftdi_error_return(-1, "reading latency timer failed");
1944 *latency = (unsigned char)usb_val;
1949 Poll modem status information
1951 This function allows the retrieve the two status bytes of the device.
1952 The device sends these bytes also as a header for each read access
1953 where they are discarded by ftdi_read_data(). The chip generates
1954 the two stripped status bytes in the absence of data every 40 ms.
1956 Layout of the first byte:
1957 - B0..B3 - must be 0
1958 - B4 Clear to send (CTS)
1961 - B5 Data set ready (DTS)
1964 - B6 Ring indicator (RI)
1967 - B7 Receive line signal detect (RLSD)
1971 Layout of the second byte:
1972 - B0 Data ready (DR)
1973 - B1 Overrun error (OE)
1974 - B2 Parity error (PE)
1975 - B3 Framing error (FE)
1976 - B4 Break interrupt (BI)
1977 - B5 Transmitter holding register (THRE)
1978 - B6 Transmitter empty (TEMT)
1979 - B7 Error in RCVR FIFO
1981 \param ftdi pointer to ftdi_context
1982 \param status Pointer to store status information in. Must be two bytes.
1985 \retval -1: unable to retrieve status information
1986 \retval -2: USB device unavailable
1988 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1992 if (ftdi == NULL || ftdi->usb_dev == NULL)
1993 ftdi_error_return(-2, "USB device unavailable");
1995 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)
1996 ftdi_error_return(-1, "getting modem status failed");
1998 *status = (usb_val[1] << 8) | usb_val[0];
2004 Set flowcontrol for ftdi chip
2006 \param ftdi pointer to ftdi_context
2007 \param flowctrl flow control to use. should be
2008 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
2011 \retval -1: set flow control failed
2012 \retval -2: USB device unavailable
2014 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2016 if (ftdi == NULL || ftdi->usb_dev == NULL)
2017 ftdi_error_return(-2, "USB device unavailable");
2019 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2020 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2021 NULL, 0, ftdi->usb_write_timeout) < 0)
2022 ftdi_error_return(-1, "set flow control failed");
2030 \param ftdi pointer to ftdi_context
2031 \param state state to set line to (1 or 0)
2034 \retval -1: set dtr failed
2035 \retval -2: USB device unavailable
2037 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2039 unsigned short usb_val;
2041 if (ftdi == NULL || ftdi->usb_dev == NULL)
2042 ftdi_error_return(-2, "USB device unavailable");
2045 usb_val = SIO_SET_DTR_HIGH;
2047 usb_val = SIO_SET_DTR_LOW;
2049 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2050 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2051 NULL, 0, ftdi->usb_write_timeout) < 0)
2052 ftdi_error_return(-1, "set dtr failed");
2060 \param ftdi pointer to ftdi_context
2061 \param state state to set line to (1 or 0)
2064 \retval -1: set rts failed
2065 \retval -2: USB device unavailable
2067 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2069 unsigned short usb_val;
2071 if (ftdi == NULL || ftdi->usb_dev == NULL)
2072 ftdi_error_return(-2, "USB device unavailable");
2075 usb_val = SIO_SET_RTS_HIGH;
2077 usb_val = SIO_SET_RTS_LOW;
2079 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2080 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2081 NULL, 0, ftdi->usb_write_timeout) < 0)
2082 ftdi_error_return(-1, "set of rts failed");
2088 Set dtr and rts line in one pass
2090 \param ftdi pointer to ftdi_context
2091 \param dtr DTR state to set line to (1 or 0)
2092 \param rts RTS state to set line to (1 or 0)
2095 \retval -1: set dtr/rts failed
2096 \retval -2: USB device unavailable
2098 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2100 unsigned short usb_val;
2102 if (ftdi == NULL || ftdi->usb_dev == NULL)
2103 ftdi_error_return(-2, "USB device unavailable");
2106 usb_val = SIO_SET_DTR_HIGH;
2108 usb_val = SIO_SET_DTR_LOW;
2111 usb_val |= SIO_SET_RTS_HIGH;
2113 usb_val |= SIO_SET_RTS_LOW;
2115 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2116 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2117 NULL, 0, ftdi->usb_write_timeout) < 0)
2118 ftdi_error_return(-1, "set of rts/dtr failed");
2124 Set the special event character
2126 \param ftdi pointer to ftdi_context
2127 \param eventch Event character
2128 \param enable 0 to disable the event character, non-zero otherwise
2131 \retval -1: unable to set event character
2132 \retval -2: USB device unavailable
2134 int ftdi_set_event_char(struct ftdi_context *ftdi,
2135 unsigned char eventch, unsigned char enable)
2137 unsigned short usb_val;
2139 if (ftdi == NULL || ftdi->usb_dev == NULL)
2140 ftdi_error_return(-2, "USB device unavailable");
2146 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)
2147 ftdi_error_return(-1, "setting event character failed");
2155 \param ftdi pointer to ftdi_context
2156 \param errorch Error character
2157 \param enable 0 to disable the error character, non-zero otherwise
2160 \retval -1: unable to set error character
2161 \retval -2: USB device unavailable
2163 int ftdi_set_error_char(struct ftdi_context *ftdi,
2164 unsigned char errorch, unsigned char enable)
2166 unsigned short usb_val;
2168 if (ftdi == NULL || ftdi->usb_dev == NULL)
2169 ftdi_error_return(-2, "USB device unavailable");
2175 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)
2176 ftdi_error_return(-1, "setting error character failed");
2182 Init eeprom with default values.
2183 \param ftdi pointer to ftdi_context
2184 \param manufacturer String to use as Manufacturer
2185 \param product String to use as Product description
2186 \param serial String to use as Serial number description
2189 \retval -1: No struct ftdi_context
2190 \retval -2: No struct ftdi_eeprom
2192 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2193 char * product, char * serial)
2195 struct ftdi_eeprom *eeprom;
2198 ftdi_error_return(-1, "No struct ftdi_context");
2200 if (ftdi->eeprom == NULL)
2201 ftdi_error_return(-2,"No struct ftdi_eeprom");
2203 eeprom = ftdi->eeprom;
2204 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2206 eeprom->vendor_id = 0x0403;
2207 eeprom->use_serial = USE_SERIAL_NUM;
2208 if((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2209 (ftdi->type == TYPE_R))
2210 eeprom->product_id = 0x6001;
2212 eeprom->product_id = 0x6010;
2213 if (ftdi->type == TYPE_AM)
2214 eeprom->usb_version = 0x0101;
2216 eeprom->usb_version = 0x0200;
2217 eeprom->max_power = 100;
2219 if (eeprom->manufacturer)
2220 free (eeprom->manufacturer);
2221 eeprom->manufacturer = NULL;
2224 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2225 if (eeprom->manufacturer)
2226 strcpy(eeprom->manufacturer, manufacturer);
2229 if (eeprom->product)
2230 free (eeprom->product);
2231 eeprom->product = NULL;
2233 eeprom->product = malloc(strlen(product)+1);
2234 if (eeprom->product)
2235 strcpy(eeprom->product, product);
2239 free (eeprom->serial);
2240 eeprom->serial = NULL;
2243 eeprom->serial = malloc(strlen(serial)+1);
2245 strcpy(eeprom->serial, serial);
2249 if(ftdi->type == TYPE_R)
2251 eeprom->max_power = 90;
2252 eeprom->size = 0x80;
2253 eeprom->cbus_function[0] = CBUS_TXLED;
2254 eeprom->cbus_function[1] = CBUS_RXLED;
2255 eeprom->cbus_function[2] = CBUS_TXDEN;
2256 eeprom->cbus_function[3] = CBUS_PWREN;
2257 eeprom->cbus_function[4] = CBUS_SLEEP;
2265 Build binary buffer from ftdi_eeprom structure.
2266 Output is suitable for ftdi_write_eeprom().
2268 \param ftdi pointer to ftdi_context
2270 \retval >=0: size of eeprom user area in bytes
2271 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2272 \retval -2: Invalid eeprom pointer
2273 \retval -3: Invalid cbus function setting
2274 \retval -4: Chip doesn't support invert
2275 \retval -5: Chip doesn't support high current drive
2276 \retval -6: No connected EEPROM or EEPROM Type unknown
2278 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2280 unsigned char i, j, eeprom_size_mask;
2281 unsigned short checksum, value;
2282 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2284 struct ftdi_eeprom *eeprom;
2285 unsigned char * output;
2288 ftdi_error_return(-2,"No context");
2289 if (ftdi->eeprom == NULL)
2290 ftdi_error_return(-2,"No eeprom structure");
2292 eeprom= ftdi->eeprom;
2293 output = eeprom->buf;
2295 if(eeprom->chip == -1)
2296 ftdi_error_return(-5,"No connected EEPROM or EEPROM type unknown");
2298 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2299 eeprom->size = 0x100;
2301 eeprom->size = 0x80;
2303 if (eeprom->manufacturer != NULL)
2304 manufacturer_size = strlen(eeprom->manufacturer);
2305 if (eeprom->product != NULL)
2306 product_size = strlen(eeprom->product);
2307 if (eeprom->serial != NULL)
2308 serial_size = strlen(eeprom->serial);
2310 // eeprom size exceeded?
2312 switch (ftdi->type) {
2315 user_area_size = 96;
2318 user_area_size = 94;
2321 user_area_size = 92;
2325 user_area_size = 90;
2328 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2329 if (user_area_size < 0)
2330 ftdi_error_return(-1,"eeprom size exceeded");
2333 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2335 // Bytes and Bits set for all Types
2337 // Addr 02: Vendor ID
2338 output[0x02] = eeprom->vendor_id;
2339 output[0x03] = eeprom->vendor_id >> 8;
2341 // Addr 04: Product ID
2342 output[0x04] = eeprom->product_id;
2343 output[0x05] = eeprom->product_id >> 8;
2345 // Addr 06: Device release number (0400h for BM features)
2346 output[0x06] = 0x00;
2347 switch (ftdi->type) {
2349 output[0x07] = 0x02;
2352 output[0x07] = 0x04;
2355 output[0x07] = 0x05;
2358 output[0x07] = 0x06;
2361 output[0x07] = 0x07;
2364 output[0x07] = 0x08;
2367 output[0x07] = 0x00;
2370 // Addr 08: Config descriptor
2372 // Bit 6: 1 if this device is self powered, 0 if bus powered
2373 // Bit 5: 1 if this device uses remote wakeup
2374 // Bit 4-0: reserved - 0
2376 if (eeprom->self_powered == 1)
2378 if (eeprom->remote_wakeup == 1)
2382 // Addr 09: Max power consumption: max power = value * 2 mA
2383 output[0x09] = eeprom->max_power>>1;
2385 if(ftdi->type != TYPE_AM)
2387 // Addr 0A: Chip configuration
2388 // Bit 7: 0 - reserved
2389 // Bit 6: 0 - reserved
2390 // Bit 5: 0 - reserved
2391 // Bit 4: 1 - Change USB version
2392 // Bit 3: 1 - Use the serial number string
2393 // Bit 2: 1 - Enable suspend pull downs for lower power
2394 // Bit 1: 1 - Out EndPoint is Isochronous
2395 // Bit 0: 1 - In EndPoint is Isochronous
2398 if (eeprom->in_is_isochronous == 1)
2400 if (eeprom->out_is_isochronous == 1)
2406 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2407 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2422 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2423 eeprom_size_mask = eeprom->size -1;
2425 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2426 // Addr 0F: Length of manufacturer string
2427 // Output manufacturer
2428 output[0x0E] = i; // calculate offset
2429 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2430 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2431 for (j = 0; j < manufacturer_size; j++)
2433 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2434 output[i & eeprom_size_mask] = 0x00, i++;
2436 output[0x0F] = manufacturer_size*2 + 2;
2438 // Addr 10: Offset of the product string + 0x80, calculated later
2439 // Addr 11: Length of product string
2440 output[0x10] = i | 0x80; // calculate offset
2441 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2442 output[i & eeprom_size_mask] = 0x03, i++;
2443 for (j = 0; j < product_size; j++)
2445 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2446 output[i & eeprom_size_mask] = 0x00, i++;
2448 output[0x11] = product_size*2 + 2;
2450 // Addr 12: Offset of the serial string + 0x80, calculated later
2451 // Addr 13: Length of serial string
2452 output[0x12] = i | 0x80; // calculate offset
2453 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2454 output[i & eeprom_size_mask] = 0x03, i++;
2455 for (j = 0; j < serial_size; j++)
2457 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2458 output[i & eeprom_size_mask] = 0x00, i++;
2461 // Legacy port name and PnP fields for FT2232 and newer chips
2462 if (ftdi->type > TYPE_BM)
2464 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2466 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2468 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2472 output[0x13] = serial_size*2 + 2;
2474 if(ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2476 if (eeprom->use_serial == USE_SERIAL_NUM )
2477 output[0x0A] |= USE_SERIAL_NUM;
2479 output[0x0A] &= ~USE_SERIAL_NUM;
2482 /* Bytes and Bits specific to (some) types
2483 Write linear, as this allows easier fixing*/
2489 output[0x0C] = eeprom->usb_version & 0xff;
2490 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2494 output[0x00] = (eeprom->channel_a_type);
2495 if ( eeprom->channel_a_driver == DRIVER_VCP)
2496 output[0x00] |= DRIVER_VCP;
2498 output[0x00] &= ~DRIVER_VCP;
2500 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2501 output[0x00] |= HIGH_CURRENT_DRIVE;
2503 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2505 output[0x01] = (eeprom->channel_b_type);
2506 if ( eeprom->channel_b_driver == DRIVER_VCP)
2507 output[0x01] |= DRIVER_VCP;
2509 output[0x01] &= ~DRIVER_VCP;
2511 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2512 output[0x01] |= HIGH_CURRENT_DRIVE;
2514 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2516 if (eeprom->in_is_isochronous == 1)
2517 output[0x0A] |= 0x1;
2519 output[0x0A] &= ~0x1;
2520 if (eeprom->out_is_isochronous == 1)
2521 output[0x0A] |= 0x2;
2523 output[0x0A] &= ~0x2;
2524 if (eeprom->suspend_pull_downs == 1)
2525 output[0x0A] |= 0x4;
2527 output[0x0A] &= ~0x4;
2528 output[0x0C] = eeprom->usb_version & 0xff;
2529 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2530 output[0x14] = eeprom->chip;
2533 if(eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2534 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2535 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2537 if (eeprom->suspend_pull_downs == 1)
2538 output[0x0A] |= 0x4;
2540 output[0x0A] &= ~0x4;
2541 output[0x0B] = eeprom->invert;
2542 output[0x0C] = eeprom->usb_version & 0xff;
2543 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2545 if(eeprom->cbus_function[0] > CBUS_BB)
2546 output[0x14] = CBUS_TXLED;
2548 output[0x14] = eeprom->cbus_function[0];
2550 if(eeprom->cbus_function[1] > CBUS_BB)
2551 output[0x14] |= CBUS_RXLED<<4;
2553 output[0x14] |= eeprom->cbus_function[1]<<4;
2555 if(eeprom->cbus_function[2] > CBUS_BB)
2556 output[0x15] = CBUS_TXDEN;
2558 output[0x15] = eeprom->cbus_function[2];
2560 if(eeprom->cbus_function[3] > CBUS_BB)
2561 output[0x15] |= CBUS_PWREN<<4;
2563 output[0x15] |= eeprom->cbus_function[3]<<4;
2565 if(eeprom->cbus_function[4] > CBUS_CLK6)
2566 output[0x16] = CBUS_SLEEP;
2568 output[0x16] = eeprom->cbus_function[4];
2571 output[0x00] = (eeprom->channel_a_type);
2572 if ( eeprom->channel_a_driver == DRIVER_VCP)
2573 output[0x00] |= DRIVER_VCP;
2575 output[0x00] &= ~DRIVER_VCP;
2577 output[0x01] = (eeprom->channel_b_type);
2578 if ( eeprom->channel_b_driver == DRIVER_VCP)
2579 output[0x01] |= DRIVER_VCP;
2581 output[0x01] &= ~DRIVER_VCP;
2582 if(eeprom->suspend_dbus7 == SUSPEND_DBUS7)
2583 output[0x01] |= SUSPEND_DBUS7;
2585 output[0x01] &= ~SUSPEND_DBUS7;
2587 if (eeprom->suspend_pull_downs == 1)
2588 output[0x0A] |= 0x4;
2590 output[0x0A] &= ~0x4;
2592 if(eeprom->group0_drive > DRIVE_16MA)
2593 output[0x0c] |= DRIVE_16MA;
2595 output[0x0c] |= eeprom->group0_drive;
2596 if (eeprom->group0_schmitt == IS_SCHMITT)
2597 output[0x0c] |= IS_SCHMITT;
2598 if (eeprom->group0_slew == SLOW_SLEW)
2599 output[0x0c] |= SLOW_SLEW;
2601 if(eeprom->group1_drive > DRIVE_16MA)
2602 output[0x0c] |= DRIVE_16MA<<4;
2604 output[0x0c] |= eeprom->group1_drive<<4;
2605 if (eeprom->group1_schmitt == IS_SCHMITT)
2606 output[0x0c] |= IS_SCHMITT<<4;
2607 if (eeprom->group1_slew == SLOW_SLEW)
2608 output[0x0c] |= SLOW_SLEW<<4;
2610 if(eeprom->group2_drive > DRIVE_16MA)
2611 output[0x0d] |= DRIVE_16MA;
2613 output[0x0d] |= eeprom->group2_drive;
2614 if (eeprom->group2_schmitt == IS_SCHMITT)
2615 output[0x0d] |= IS_SCHMITT;
2616 if (eeprom->group2_slew == SLOW_SLEW)
2617 output[0x0d] |= SLOW_SLEW;
2619 if(eeprom->group3_drive > DRIVE_16MA)
2620 output[0x0d] |= DRIVE_16MA<<4;
2622 output[0x0d] |= eeprom->group3_drive<<4;
2623 if (eeprom->group3_schmitt == IS_SCHMITT)
2624 output[0x0d] |= IS_SCHMITT<<4;
2625 if (eeprom->group3_slew == SLOW_SLEW)
2626 output[0x0d] |= SLOW_SLEW<<4;
2628 output[0x18] = eeprom->chip;
2632 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2635 // calculate checksum
2638 for (i = 0; i < eeprom->size/2-1; i++)
2640 value = output[i*2];
2641 value += output[(i*2)+1] << 8;
2643 checksum = value^checksum;
2644 checksum = (checksum << 1) | (checksum >> 15);
2647 output[eeprom->size-2] = checksum;
2648 output[eeprom->size-1] = checksum >> 8;
2650 return user_area_size;
2654 Decode binary EEPROM image into an ftdi_eeprom structure.
2656 \param ftdi pointer to ftdi_context
2657 \param verbose Decode EEPROM on stdout
2660 \retval -1: something went wrong
2662 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2663 FIXME: Strings are malloc'ed here and should be freed somewhere
2665 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2668 unsigned short checksum, eeprom_checksum, value;
2669 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2671 struct ftdi_eeprom *eeprom;
2672 unsigned char *buf = ftdi->eeprom->buf;
2676 ftdi_error_return(-1,"No context");
2677 if (ftdi->eeprom == NULL)
2678 ftdi_error_return(-1,"No eeprom structure");
2680 eeprom = ftdi->eeprom;
2681 eeprom_size = eeprom->size;
2683 // Addr 02: Vendor ID
2684 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2686 // Addr 04: Product ID
2687 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2689 release = buf[0x06] + (buf[0x07]<<8);
2691 // Addr 08: Config descriptor
2693 // Bit 6: 1 if this device is self powered, 0 if bus powered
2694 // Bit 5: 1 if this device uses remote wakeup
2695 eeprom->self_powered = buf[0x08] & 0x40;
2696 eeprom->remote_wakeup = buf[0x08] & 0x20;;
2698 // Addr 09: Max power consumption: max power = value * 2 mA
2699 eeprom->max_power = buf[0x09];
2701 // Addr 0A: Chip configuration
2702 // Bit 7: 0 - reserved
2703 // Bit 6: 0 - reserved
2704 // Bit 5: 0 - reserved
2705 // Bit 4: 1 - Change USB version
2706 // Not seen on FT2232(D)
2707 // Bit 3: 1 - Use the serial number string
2708 // Bit 2: 1 - Enable suspend pull downs for lower power
2709 // Bit 1: 1 - Out EndPoint is Isochronous
2710 // Bit 0: 1 - In EndPoint is Isochronous
2712 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2713 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2714 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2715 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2718 "EEPROM byte[0x0a] Bit 4 unexpected set. If this happened with the EEPROM\n"
2719 "programmed by FTDI tools, please report to libftdi@developer.intra2net.com\n");
2722 // Addr 0C: USB version low byte when 0x0A
2723 // Addr 0D: USB version high byte when 0x0A
2724 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2726 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2727 // Addr 0F: Length of manufacturer string
2728 manufacturer_size = buf[0x0F]/2;
2729 if(eeprom->manufacturer)
2730 free(eeprom->manufacturer);
2731 if (manufacturer_size > 0)
2733 eeprom->manufacturer = malloc(manufacturer_size);
2734 if (eeprom->manufacturer)
2736 // Decode manufacturer
2737 i = buf[0x0E] & (eeprom_size -1); // offset
2738 for (j=0;j<manufacturer_size-1;j++)
2740 eeprom->manufacturer[j] = buf[2*j+i+2];
2742 eeprom->manufacturer[j] = '\0';
2745 else eeprom->manufacturer = NULL;
2747 // Addr 10: Offset of the product string + 0x80, calculated later
2748 // Addr 11: Length of product string
2750 free(eeprom->product);
2751 product_size = buf[0x11]/2;
2752 if (product_size > 0)
2754 eeprom->product = malloc(product_size);
2757 // Decode product name
2758 i = buf[0x10] & (eeprom_size -1); // offset
2759 for (j=0;j<product_size-1;j++)
2761 eeprom->product[j] = buf[2*j+i+2];
2763 eeprom->product[j] = '\0';
2766 else eeprom->product = NULL;
2768 // Addr 12: Offset of the serial string + 0x80, calculated later
2769 // Addr 13: Length of serial string
2771 free(eeprom->serial);
2772 serial_size = buf[0x13]/2;
2773 if (serial_size > 0)
2775 eeprom->serial = malloc(serial_size);
2779 i = buf[0x12] & (eeprom_size -1); // offset
2780 for (j=0;j<serial_size-1;j++)
2782 eeprom->serial[j] = buf[2*j+i+2];
2784 eeprom->serial[j] = '\0';
2787 else eeprom->serial = NULL;
2792 for (i = 0; i < eeprom_size/2-1; i++)
2795 value += buf[(i*2)+1] << 8;
2797 checksum = value^checksum;
2798 checksum = (checksum << 1) | (checksum >> 15);
2801 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2803 if (eeprom_checksum != checksum)
2805 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2806 ftdi_error_return(-1,"EEPROM checksum error");
2809 eeprom->channel_a_type = 0;
2810 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2814 else if(ftdi->type == TYPE_2232C)
2816 eeprom->channel_a_type = buf[0x00] & 0x7;
2817 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2818 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2819 eeprom->channel_b_type = buf[0x01] & 0x7;
2820 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2821 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2822 eeprom->chip = buf[0x14];
2824 else if(ftdi->type == TYPE_R)
2826 /* TYPE_R flags D2XX, not VCP as all others*/
2827 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2828 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2829 if( (buf[0x01]&0x40) != 0x40)
2831 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2832 " If this happened with the\n"
2833 " EEPROM programmed by FTDI tools, please report "
2834 "to libftdi@developer.intra2net.com\n");
2836 eeprom->chip = buf[0x16];
2837 // Addr 0B: Invert data lines
2838 // Works only on FT232R, not FT245R, but no way to distinguish
2839 eeprom->invert = buf[0x0B];
2840 // Addr 14: CBUS function: CBUS0, CBUS1
2841 // Addr 15: CBUS function: CBUS2, CBUS3
2842 // Addr 16: CBUS function: CBUS5
2843 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2844 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2845 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2846 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2847 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2849 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2851 eeprom->channel_a_type = buf[0x00] & 0x7;
2852 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2853 eeprom->channel_b_type = buf[0x01] & 0x7;
2854 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2856 if(ftdi->type == TYPE_2232H)
2857 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7;
2859 eeprom->chip = buf[0x18];
2860 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2861 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2862 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2863 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2864 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2865 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2866 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2867 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2868 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2869 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2870 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2871 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2876 char *channel_mode[] = {"UART","245","CPU", "unknown", "OPTO"};
2877 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
2878 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
2879 fprintf(stdout, "Release: 0x%04x\n",release);
2881 if(eeprom->self_powered)
2882 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
2884 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
2885 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
2886 if(eeprom->manufacturer)
2887 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
2889 fprintf(stdout, "Product: %s\n",eeprom->product);
2891 fprintf(stdout, "Serial: %s\n",eeprom->serial);
2892 fprintf(stdout, "Checksum : %04x\n", checksum);
2893 if (ftdi->type == TYPE_R)
2894 fprintf(stdout, "Internal EEPROM\n");
2895 else if (eeprom->chip >= 0x46)
2896 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
2897 if(eeprom->suspend_dbus7)
2898 fprintf(stdout, "Suspend on DBUS7\n");
2899 if(eeprom->suspend_pull_downs)
2900 fprintf(stdout, "Pull IO pins low during suspend\n");
2901 if(eeprom->remote_wakeup)
2902 fprintf(stdout, "Enable Remote Wake Up\n");
2903 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
2904 if (ftdi->type >= TYPE_2232C)
2905 fprintf(stdout,"Channel A has Mode %s%s%s\n",
2906 channel_mode[eeprom->channel_a_type],
2907 (eeprom->channel_a_driver)?" VCP":"",
2908 (eeprom->high_current_a)?" High Current IO":"");
2909 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R))
2910 fprintf(stdout,"Channel B has Mode %s%s%s\n",
2911 channel_mode[eeprom->channel_b_type],
2912 (eeprom->channel_b_driver)?" VCP":"",
2913 (eeprom->high_current_b)?" High Current IO":"");
2914 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
2916 fprintf(stdout,"%s has %d mA drive%s%s\n",
2917 (ftdi->type == TYPE_2232H)?"AL":"A",
2918 (eeprom->group0_drive+1) *4,
2919 (eeprom->group0_schmitt)?" Schmitt Input":"",
2920 (eeprom->group0_slew)?" Slow Slew":"");
2921 fprintf(stdout,"%s has %d mA drive%s%s\n",
2922 (ftdi->type == TYPE_2232H)?"AH":"B",
2923 (eeprom->group1_drive+1) *4,
2924 (eeprom->group1_schmitt)?" Schmitt Input":"",
2925 (eeprom->group1_slew)?" Slow Slew":"");
2926 fprintf(stdout,"%s has %d mA drive%s%s\n",
2927 (ftdi->type == TYPE_2232H)?"BL":"C",
2928 (eeprom->group2_drive+1) *4,
2929 (eeprom->group2_schmitt)?" Schmitt Input":"",
2930 (eeprom->group2_slew)?" Slow Slew":"");
2931 fprintf(stdout,"%s has %d mA drive%s%s\n",
2932 (ftdi->type == TYPE_2232H)?"BH":"D",
2933 (eeprom->group3_drive+1) *4,
2934 (eeprom->group3_schmitt)?" Schmitt Input":"",
2935 (eeprom->group3_slew)?" Slow Slew":"");
2937 if (ftdi->type == TYPE_R)
2939 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
2940 "SLEEP","CLK48","CLK24","CLK12","CLK6",
2941 "IOMODE","BB_WR","BB_RD"};
2942 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
2947 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
2948 fprintf(stdout,"Inverted bits:");
2950 if((eeprom->invert & (1<<i)) == (1<<i))
2951 fprintf(stdout," %s",r_bits[i]);
2952 fprintf(stdout,"\n");
2956 if(eeprom->cbus_function[i]<CBUS_BB)
2957 fprintf(stdout,"C%d Function: %s\n", i,
2958 cbus_mux[eeprom->cbus_function[i]]);
2960 fprintf(stdout,"C%d BB Function: %s\n", i,
2969 Read eeprom location
2971 \param ftdi pointer to ftdi_context
2972 \param eeprom_addr Address of eeprom location to be read
2973 \param eeprom_val Pointer to store read eeprom location
2976 \retval -1: read failed
2977 \retval -2: USB device unavailable
2979 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2981 if (ftdi == NULL || ftdi->usb_dev == NULL)
2982 ftdi_error_return(-2, "USB device unavailable");
2984 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)
2985 ftdi_error_return(-1, "reading eeprom failed");
2993 \param ftdi pointer to ftdi_context
2996 \retval -1: read failed
2997 \retval -2: USB device unavailable
2999 int ftdi_read_eeprom(struct ftdi_context *ftdi)
3004 if (ftdi == NULL || ftdi->usb_dev == NULL)
3005 ftdi_error_return(-2, "USB device unavailable");
3006 buf = ftdi->eeprom->buf;
3008 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3010 if (libusb_control_transfer(
3011 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3012 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3013 ftdi_error_return(-1, "reading eeprom failed");
3016 if (ftdi->type == TYPE_R)
3017 ftdi->eeprom->size = 0x80;
3018 /* Guesses size of eeprom by comparing halves
3019 - will not work with blank eeprom */
3020 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3021 ftdi->eeprom->size = -1;
3022 else if(memcmp(buf,&buf[0x80],0x80) == 0)
3023 ftdi->eeprom->size = 0x80;
3024 else if(memcmp(buf,&buf[0x40],0x40) == 0)
3025 ftdi->eeprom->size = 0x40;
3027 ftdi->eeprom->size = 0x100;
3032 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3033 Function is only used internally
3036 static unsigned char ftdi_read_chipid_shift(unsigned char value)
3038 return ((value & 1) << 1) |
3039 ((value & 2) << 5) |
3040 ((value & 4) >> 2) |
3041 ((value & 8) << 4) |
3042 ((value & 16) >> 1) |
3043 ((value & 32) >> 1) |
3044 ((value & 64) >> 4) |
3045 ((value & 128) >> 2);
3049 Read the FTDIChip-ID from R-type devices
3051 \param ftdi pointer to ftdi_context
3052 \param chipid Pointer to store FTDIChip-ID
3055 \retval -1: read failed
3056 \retval -2: USB device unavailable
3058 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3060 unsigned int a = 0, b = 0;
3062 if (ftdi == NULL || ftdi->usb_dev == NULL)
3063 ftdi_error_return(-2, "USB device unavailable");
3065 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)
3067 a = a << 8 | a >> 8;
3068 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)
3070 b = b << 8 | b >> 8;
3071 a = (a << 16) | (b & 0xFFFF);
3072 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3073 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3074 *chipid = a ^ 0xa5f0f7d1;
3079 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3083 Write eeprom location
3085 \param ftdi pointer to ftdi_context
3086 \param eeprom_addr Address of eeprom location to be written
3087 \param eeprom_val Value to be written
3090 \retval -1: write failed
3091 \retval -2: USB device unavailable
3092 \retval -3: Invalid access to checksum protected area below 0x80
3093 \retval -4: Device can't access unprotected area
3094 \retval -5: Reading chip type failed
3096 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3097 unsigned short eeprom_val)
3099 int chip_type_location;
3100 unsigned short chip_type;
3102 if (ftdi == NULL || ftdi->usb_dev == NULL)
3103 ftdi_error_return(-2, "USB device unavailable");
3105 if(eeprom_addr <0x80)
3106 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3113 chip_type_location = 0x14;
3117 chip_type_location = 0x18;
3120 ftdi_error_return(-4, "Device can't access unprotected area");
3123 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3124 ftdi_error_return(-5, "Reading failed failed");
3125 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3126 if((chip_type & 0xff) != 0x66)
3128 ftdi_error_return(-6, "EEPROM is not of 93x66");
3131 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3132 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3133 NULL, 0, ftdi->usb_write_timeout) != 0)
3134 ftdi_error_return(-1, "unable to write eeprom");
3142 \param ftdi pointer to ftdi_context
3145 \retval -1: read failed
3146 \retval -2: USB device unavailable
3148 int ftdi_write_eeprom(struct ftdi_context *ftdi)
3150 unsigned short usb_val, status;
3152 unsigned char *eeprom;
3154 if (ftdi == NULL || ftdi->usb_dev == NULL)
3155 ftdi_error_return(-2, "USB device unavailable");
3156 eeprom = ftdi->eeprom->buf;
3158 /* These commands were traced while running MProg */
3159 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3161 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3163 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3166 for (i = 0; i < ftdi->eeprom->size/2; i++)
3168 usb_val = eeprom[i*2];
3169 usb_val += eeprom[(i*2)+1] << 8;
3170 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3171 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3172 NULL, 0, ftdi->usb_write_timeout) < 0)
3173 ftdi_error_return(-1, "unable to write eeprom");
3182 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3184 \param ftdi pointer to ftdi_context
3187 \retval -1: erase failed
3188 \retval -2: USB device unavailable
3189 \retval -3: Writing magic failed
3190 \retval -4: Read EEPROM failed
3191 \retval -5: Unexpected EEPROM value
3193 #define MAGIC 0x55aa
3194 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3196 unsigned short eeprom_value;
3197 if (ftdi == NULL || ftdi->usb_dev == NULL)
3198 ftdi_error_return(-2, "USB device unavailable");
3200 if(ftdi->type == TYPE_R)
3202 ftdi->eeprom->chip = 0;
3206 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3207 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3208 ftdi_error_return(-1, "unable to erase eeprom");
3211 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3212 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3213 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3214 Chip is 93x66 if magic is only read at word position 0xc0*/
3215 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3216 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3217 NULL, 0, ftdi->usb_write_timeout) != 0)
3218 ftdi_error_return(-3, "Writing magic failed");
3219 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3220 ftdi_error_return(-4, "Reading failed failed");
3221 if(eeprom_value == MAGIC)
3223 ftdi->eeprom->chip = 0x46;
3227 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3228 ftdi_error_return(-4, "Reading failed failed");
3229 if(eeprom_value == MAGIC)
3230 ftdi->eeprom->chip = 0x56;
3233 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3234 ftdi_error_return(-4, "Reading failed failed");
3235 if(eeprom_value == MAGIC)
3236 ftdi->eeprom->chip = 0x66;
3239 ftdi->eeprom->chip = -1;
3243 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3244 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3245 ftdi_error_return(-1, "unable to erase eeprom");
3250 Get string representation for last error code
3252 \param ftdi pointer to ftdi_context
3254 \retval Pointer to error string
3256 char *ftdi_get_error_string (struct ftdi_context *ftdi)
3261 return ftdi->error_str;
3264 /* @} end of doxygen libftdi group */