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 */
38 /* stuff needed for async write */
39 #ifdef LIBFTDI_LINUX_ASYNC_MODE
40 #include <sys/ioctl.h>
41 #include <sys/select.h>
42 #include <sys/types.h>
44 #include <linux/usbdevice_fs.h>
47 #define ftdi_error_return(code, str) do { \
48 ftdi->error_str = str; \
53 #if defined( __WIN32__) && !defined(__MINGW32__)
55 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
60 int gettimeofday( struct timeval *tv, void null)
63 unsigned __int64 tmpres = 0;
66 GetSystemTimeAsFileTime(&ft);
67 tmpres |= ft.dwHighDateTime;
69 tmpres |= ft.dwLowDateTime;
71 /*converting file time to unix epoch*/
72 tmpres /= 10; /*convert into microseconds*/
73 tmpres -= DELTA_EPOCH_IN_MICROSECS;
74 tv->tv_sec = (LONG)(tmpres / 1000000UL);
75 tv->tv_usec = (LONG)(tmpres % 1000000UL);
77 /* Warning: Timezone not handled (and not needed here) */
81 // Include sys/time.h on non-Windows platforms
82 // as gettimeofday() needs it.
87 Internal function to close usb device pointer.
88 Sets ftdi->usb_dev to NULL.
91 \param ftdi pointer to ftdi_context
93 \retval zero if all is fine, otherwise error code from usb_close()
95 static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
99 if (ftdi && ftdi->usb_dev)
101 ret = usb_close (ftdi->usb_dev);
102 ftdi->usb_dev = NULL;
109 Initializes a ftdi_context.
111 \param ftdi pointer to ftdi_context
114 \retval -1: couldn't allocate read buffer
116 \remark This should be called before all functions
118 int ftdi_init(struct ftdi_context *ftdi)
122 ftdi->usb_dev = NULL;
123 ftdi->usb_read_timeout = 5000;
124 ftdi->usb_write_timeout = 5000;
126 ftdi->type = TYPE_BM; /* chip type */
128 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
130 ftdi->readbuffer = NULL;
131 ftdi->readbuffer_offset = 0;
132 ftdi->readbuffer_remaining = 0;
133 ftdi->writebuffer_chunksize = 4096;
134 ftdi->max_packet_size = 0;
140 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
142 ftdi->error_str = NULL;
144 #ifdef LIBFTDI_LINUX_ASYNC_MODE
145 ftdi->async_usb_buffer_size=10;
146 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
147 ftdi_error_return(-1, "out of memory for async usb buffer");
149 /* initialize async usb buffer with unused-marker */
150 for (i=0; i < ftdi->async_usb_buffer_size; i++)
151 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
153 ftdi->async_usb_buffer_size=0;
154 ftdi->async_usb_buffer = NULL;
157 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
159 /* All fine. Now allocate the readbuffer */
160 return ftdi_read_data_set_chunksize(ftdi, 4096);
164 Allocate and initialize a new ftdi_context
166 \return a pointer to a new ftdi_context, or NULL on failure
168 struct ftdi_context *ftdi_new(void)
170 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
177 if (ftdi_init(ftdi) != 0)
187 Open selected channels on a chip, otherwise use first channel.
189 \param ftdi pointer to ftdi_context
190 \param interface Interface to use for FT2232C/2232H/4232H chips.
193 \retval -1: unknown interface
194 \retval -2: USB device unavailable
196 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
199 ftdi_error_return(-2, "USB device unavailable");
205 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
209 ftdi->index = INTERFACE_B;
215 ftdi->index = INTERFACE_C;
221 ftdi->index = INTERFACE_D;
226 ftdi_error_return(-1, "Unknown interface");
232 Deinitializes a ftdi_context.
234 \param ftdi pointer to ftdi_context
236 void ftdi_deinit(struct ftdi_context *ftdi)
241 ftdi_usb_close_internal (ftdi);
243 if (ftdi->async_usb_buffer != NULL)
245 free(ftdi->async_usb_buffer);
246 ftdi->async_usb_buffer = NULL;
249 if (ftdi->readbuffer != NULL)
251 free(ftdi->readbuffer);
252 ftdi->readbuffer = NULL;
257 Deinitialize and free an ftdi_context.
259 \param ftdi pointer to ftdi_context
261 void ftdi_free(struct ftdi_context *ftdi)
268 Use an already open libusb device.
270 \param ftdi pointer to ftdi_context
271 \param usb libusb usb_dev_handle to use
273 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
283 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
284 needs to be deallocated by ftdi_list_free() after use.
286 \param ftdi pointer to ftdi_context
287 \param devlist Pointer where to store list of found devices
288 \param vendor Vendor ID to search for
289 \param product Product ID to search for
291 \retval >0: number of devices found
292 \retval -1: usb_find_busses() failed
293 \retval -2: usb_find_devices() failed
294 \retval -3: out of memory
296 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
298 struct ftdi_device_list **curdev;
300 struct usb_device *dev;
304 if (usb_find_busses() < 0)
305 ftdi_error_return(-1, "usb_find_busses() failed");
306 if (usb_find_devices() < 0)
307 ftdi_error_return(-2, "usb_find_devices() failed");
311 for (bus = usb_get_busses(); bus; bus = bus->next)
313 for (dev = bus->devices; dev; dev = dev->next)
315 if (dev->descriptor.idVendor == vendor
316 && dev->descriptor.idProduct == product)
318 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
320 ftdi_error_return(-3, "out of memory");
322 (*curdev)->next = NULL;
323 (*curdev)->dev = dev;
325 curdev = &(*curdev)->next;
335 Frees a usb device list.
337 \param devlist USB device list created by ftdi_usb_find_all()
339 void ftdi_list_free(struct ftdi_device_list **devlist)
341 struct ftdi_device_list *curdev, *next;
343 for (curdev = *devlist; curdev != NULL;)
354 Frees a usb device list.
356 \param devlist USB device list created by ftdi_usb_find_all()
358 void ftdi_list_free2(struct ftdi_device_list *devlist)
360 ftdi_list_free(&devlist);
364 Return device ID strings from the usb device.
366 The parameters manufacturer, description and serial may be NULL
367 or pointer to buffers to store the fetched strings.
369 \note Use this function only in combination with ftdi_usb_find_all()
370 as it closes the internal "usb_dev" after use.
372 \param ftdi pointer to ftdi_context
373 \param dev libusb usb_dev to use
374 \param manufacturer Store manufacturer string here if not NULL
375 \param mnf_len Buffer size of manufacturer string
376 \param description Store product description string here if not NULL
377 \param desc_len Buffer size of product description string
378 \param serial Store serial string here if not NULL
379 \param serial_len Buffer size of serial string
382 \retval -1: wrong arguments
383 \retval -4: unable to open device
384 \retval -7: get product manufacturer failed
385 \retval -8: get product description failed
386 \retval -9: get serial number failed
387 \retval -10: unable to close device
389 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
390 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
392 if ((ftdi==NULL) || (dev==NULL))
395 if (!(ftdi->usb_dev = usb_open(dev)))
396 ftdi_error_return(-4, usb_strerror());
398 if (manufacturer != NULL)
400 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
402 ftdi_usb_close_internal (ftdi);
403 ftdi_error_return(-7, usb_strerror());
407 if (description != NULL)
409 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
411 ftdi_usb_close_internal (ftdi);
412 ftdi_error_return(-8, usb_strerror());
418 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
420 ftdi_usb_close_internal (ftdi);
421 ftdi_error_return(-9, usb_strerror());
425 if (ftdi_usb_close_internal (ftdi) != 0)
426 ftdi_error_return(-10, usb_strerror());
432 * Internal function to determine the maximum packet size.
433 * \param ftdi pointer to ftdi_context
434 * \param dev libusb usb_dev to use
435 * \retval Maximum packet size for this device
437 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
439 unsigned int packet_size;
442 if (ftdi == NULL || dev == NULL)
445 // Determine maximum packet size. Init with default value.
446 // New hi-speed devices from FTDI use a packet size of 512 bytes
447 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
448 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
453 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
455 struct usb_config_descriptor config = dev->config[0];
457 if (ftdi->interface < config.bNumInterfaces)
459 struct usb_interface interface = config.interface[ftdi->interface];
460 if (interface.num_altsetting > 0)
462 struct usb_interface_descriptor descriptor = interface.altsetting[0];
463 if (descriptor.bNumEndpoints > 0)
465 packet_size = descriptor.endpoint[0].wMaxPacketSize;
475 Opens a ftdi device given by an usb_device.
477 \param ftdi pointer to ftdi_context
478 \param dev libusb usb_dev to use
481 \retval -3: unable to config device
482 \retval -4: unable to open device
483 \retval -5: unable to claim device
484 \retval -6: reset failed
485 \retval -7: set baudrate failed
486 \retval -8: ftdi context invalid
488 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
490 int detach_errno = 0;
494 ftdi_error_return(-8, "ftdi context invalid");
496 if (!(ftdi->usb_dev = usb_open(dev)))
497 ftdi_error_return(-4, "usb_open() failed");
499 #ifdef LIBUSB_HAS_GET_DRIVER_NP
500 // Try to detach ftdi_sio kernel module.
501 // Returns ENODATA if driver is not loaded.
503 // The return code is kept in a separate variable and only parsed
504 // if usb_set_configuration() or usb_claim_interface() fails as the
505 // detach operation might be denied and everything still works fine.
506 // Likely scenario is a static ftdi_sio kernel module.
507 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
508 detach_errno = errno;
512 // set configuration (needed especially for windows)
513 // tolerate EBUSY: one device with one configuration, but two interfaces
514 // and libftdi sessions to both interfaces (e.g. FT2232)
516 if (dev->descriptor.bNumConfigurations > 0)
518 // libusb-win32 on Windows 64 can return a null pointer for a valid device
520 config_val = dev->config[0].bConfigurationValue;
522 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
525 ftdi_usb_close_internal (ftdi);
526 if (detach_errno == EPERM)
528 ftdi_error_return(-8, "inappropriate permissions on device!");
532 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
538 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
540 ftdi_usb_close_internal (ftdi);
541 if (detach_errno == EPERM)
543 ftdi_error_return(-8, "inappropriate permissions on device!");
547 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
551 if (ftdi_usb_reset (ftdi) != 0)
553 ftdi_usb_close_internal (ftdi);
554 ftdi_error_return(-6, "ftdi_usb_reset failed");
557 // Try to guess chip type
558 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
559 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
560 && dev->descriptor.iSerialNumber == 0))
561 ftdi->type = TYPE_BM;
562 else if (dev->descriptor.bcdDevice == 0x200)
563 ftdi->type = TYPE_AM;
564 else if (dev->descriptor.bcdDevice == 0x500)
565 ftdi->type = TYPE_2232C;
566 else if (dev->descriptor.bcdDevice == 0x600)
568 else if (dev->descriptor.bcdDevice == 0x700)
569 ftdi->type = TYPE_2232H;
570 else if (dev->descriptor.bcdDevice == 0x800)
571 ftdi->type = TYPE_4232H;
573 // Set default interface on dual/quad type chips
580 ftdi->index = INTERFACE_A;
586 // Determine maximum packet size
587 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
589 if (ftdi_set_baudrate (ftdi, 9600) != 0)
591 ftdi_usb_close_internal (ftdi);
592 ftdi_error_return(-7, "set baudrate failed");
595 ftdi_error_return(0, "all fine");
599 Opens the first device with a given vendor and product ids.
601 \param ftdi pointer to ftdi_context
602 \param vendor Vendor ID
603 \param product Product ID
605 \retval same as ftdi_usb_open_desc()
607 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
609 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
613 Opens the first device with a given, vendor id, product id,
614 description and serial.
616 \param ftdi pointer to ftdi_context
617 \param vendor Vendor ID
618 \param product Product ID
619 \param description Description to search for. Use NULL if not needed.
620 \param serial Serial to search for. Use NULL if not needed.
623 \retval -1: usb_find_busses() failed
624 \retval -2: usb_find_devices() failed
625 \retval -3: usb device not found
626 \retval -4: unable to open device
627 \retval -5: unable to claim device
628 \retval -6: reset failed
629 \retval -7: set baudrate failed
630 \retval -8: get product description failed
631 \retval -9: get serial number failed
632 \retval -10: unable to close device
634 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
635 const char* description, const char* serial)
637 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
641 Opens the index-th device with a given, vendor id, product id,
642 description and serial.
644 \param ftdi pointer to ftdi_context
645 \param vendor Vendor ID
646 \param product Product ID
647 \param description Description to search for. Use NULL if not needed.
648 \param serial Serial to search for. Use NULL if not needed.
649 \param index Number of matching device to open if there are more than one, starts with 0.
652 \retval -1: usb_find_busses() failed
653 \retval -2: usb_find_devices() failed
654 \retval -3: usb device not found
655 \retval -4: unable to open device
656 \retval -5: unable to claim device
657 \retval -6: reset failed
658 \retval -7: set baudrate failed
659 \retval -8: get product description failed
660 \retval -9: get serial number failed
661 \retval -10: unable to close device
662 \retval -11: ftdi context invalid
664 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
665 const char* description, const char* serial, unsigned int index)
668 struct usb_device *dev;
673 if (usb_find_busses() < 0)
674 ftdi_error_return(-1, "usb_find_busses() failed");
675 if (usb_find_devices() < 0)
676 ftdi_error_return(-2, "usb_find_devices() failed");
679 ftdi_error_return(-11, "ftdi context invalid");
681 for (bus = usb_get_busses(); bus; bus = bus->next)
683 for (dev = bus->devices; dev; dev = dev->next)
685 if (dev->descriptor.idVendor == vendor
686 && dev->descriptor.idProduct == product)
688 if (!(ftdi->usb_dev = usb_open(dev)))
689 ftdi_error_return(-4, "usb_open() failed");
691 if (description != NULL)
693 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
695 ftdi_usb_close_internal (ftdi);
696 ftdi_error_return(-8, "unable to fetch product description");
698 if (strncmp(string, description, sizeof(string)) != 0)
700 if (ftdi_usb_close_internal (ftdi) != 0)
701 ftdi_error_return(-10, "unable to close device");
707 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
709 ftdi_usb_close_internal (ftdi);
710 ftdi_error_return(-9, "unable to fetch serial number");
712 if (strncmp(string, serial, sizeof(string)) != 0)
714 if (ftdi_usb_close_internal (ftdi) != 0)
715 ftdi_error_return(-10, "unable to close device");
720 if (ftdi_usb_close_internal (ftdi) != 0)
721 ftdi_error_return(-10, "unable to close device");
729 return ftdi_usb_open_dev(ftdi, dev);
735 ftdi_error_return(-3, "device not found");
739 Opens the ftdi-device described by a description-string.
740 Intended to be used for parsing a device-description given as commandline argument.
742 \param ftdi pointer to ftdi_context
743 \param description NULL-terminated description-string, using this format:
744 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
745 \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")
746 \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
747 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
749 \note The description format may be extended in later versions.
752 \retval -1: usb_find_busses() failed
753 \retval -2: usb_find_devices() failed
754 \retval -3: usb device not found
755 \retval -4: unable to open device
756 \retval -5: unable to claim device
757 \retval -6: reset failed
758 \retval -7: set baudrate failed
759 \retval -8: get product description failed
760 \retval -9: get serial number failed
761 \retval -10: unable to close device
762 \retval -11: illegal description format
763 \retval -12: ftdi context invalid
765 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
768 ftdi_error_return(-12, "ftdi context invalid");
770 if (description[0] == 0 || description[1] != ':')
771 ftdi_error_return(-11, "illegal description format");
773 if (description[0] == 'd')
776 struct usb_device *dev;
780 if (usb_find_busses() < 0)
781 ftdi_error_return(-1, "usb_find_busses() failed");
782 if (usb_find_devices() < 0)
783 ftdi_error_return(-2, "usb_find_devices() failed");
785 for (bus = usb_get_busses(); bus; bus = bus->next)
787 for (dev = bus->devices; dev; dev = dev->next)
789 /* XXX: This doesn't handle symlinks/odd paths/etc... */
790 const char *desc = description + 2;
791 size_t len = strlen(bus->dirname);
792 if (strncmp(desc, bus->dirname, len))
798 if (strcmp(desc, dev->filename))
800 return ftdi_usb_open_dev(ftdi, dev);
805 ftdi_error_return(-3, "device not found");
807 else if (description[0] == 'i' || description[0] == 's')
810 unsigned int product;
811 unsigned int index=0;
812 const char *serial=NULL;
813 const char *startp, *endp;
816 startp=description+2;
817 vendor=strtoul((char*)startp,(char**)&endp,0);
818 if (*endp != ':' || endp == startp || errno != 0)
819 ftdi_error_return(-11, "illegal description format");
822 product=strtoul((char*)startp,(char**)&endp,0);
823 if (endp == startp || errno != 0)
824 ftdi_error_return(-11, "illegal description format");
826 if (description[0] == 'i' && *endp != 0)
828 /* optional index field in i-mode */
830 ftdi_error_return(-11, "illegal description format");
833 index=strtoul((char*)startp,(char**)&endp,0);
834 if (*endp != 0 || endp == startp || errno != 0)
835 ftdi_error_return(-11, "illegal description format");
837 if (description[0] == 's')
840 ftdi_error_return(-11, "illegal description format");
842 /* rest of the description is the serial */
846 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
850 ftdi_error_return(-11, "illegal description format");
855 Resets the ftdi device.
857 \param ftdi pointer to ftdi_context
860 \retval -1: FTDI reset failed
861 \retval -2: USB device unavailable
863 int ftdi_usb_reset(struct ftdi_context *ftdi)
865 if (ftdi == NULL || ftdi->usb_dev == NULL)
866 ftdi_error_return(-2, "USB device unavailable");
868 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
869 SIO_RESET_REQUEST, SIO_RESET_SIO,
870 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
871 ftdi_error_return(-1,"FTDI reset failed");
873 // Invalidate data in the readbuffer
874 ftdi->readbuffer_offset = 0;
875 ftdi->readbuffer_remaining = 0;
881 Clears the read buffer on the chip and the internal read buffer.
883 \param ftdi pointer to ftdi_context
886 \retval -1: read buffer purge failed
887 \retval -2: USB device unavailable
889 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
891 if (ftdi == NULL || ftdi->usb_dev == NULL)
892 ftdi_error_return(-2, "USB device unavailable");
894 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
895 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
896 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
897 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
899 // Invalidate data in the readbuffer
900 ftdi->readbuffer_offset = 0;
901 ftdi->readbuffer_remaining = 0;
907 Clears the write buffer on the chip.
909 \param ftdi pointer to ftdi_context
912 \retval -1: write buffer purge failed
913 \retval -2: USB device unavailable
915 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
917 if (ftdi == NULL || ftdi->usb_dev == NULL)
918 ftdi_error_return(-2, "USB device unavailable");
920 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
921 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
922 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
923 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
929 Clears the buffers on the chip and the internal read buffer.
931 \param ftdi pointer to ftdi_context
934 \retval -1: read buffer purge failed
935 \retval -2: write buffer purge failed
936 \retval -3: USB device unavailable
938 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
942 if (ftdi == NULL || ftdi->usb_dev == NULL)
943 ftdi_error_return(-3, "USB device unavailable");
945 result = ftdi_usb_purge_rx_buffer(ftdi);
949 result = ftdi_usb_purge_tx_buffer(ftdi);
959 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
961 \param ftdi pointer to ftdi_context
964 \retval -1: usb_release failed
965 \retval -2: usb_close failed
966 \retval -3: ftdi context invalid
968 int ftdi_usb_close(struct ftdi_context *ftdi)
973 ftdi_error_return(-3, "ftdi context invalid");
975 #ifdef LIBFTDI_LINUX_ASYNC_MODE
976 /* try to release some kernel resources */
977 ftdi_async_complete(ftdi,1);
980 if (ftdi->usb_dev != NULL)
981 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
984 if (ftdi_usb_close_internal (ftdi) != 0)
991 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
992 Function is only used internally
995 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
996 unsigned short *value, unsigned short *index)
998 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
999 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1000 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1001 int divisor, best_divisor, best_baud, best_baud_diff;
1002 unsigned long encoded_divisor;
1011 divisor = 24000000 / baudrate;
1013 if (ftdi->type == TYPE_AM)
1015 // Round down to supported fraction (AM only)
1016 divisor -= am_adjust_dn[divisor & 7];
1019 // Try this divisor and the one above it (because division rounds down)
1023 for (i = 0; i < 2; i++)
1025 int try_divisor = divisor + i;
1029 // Round up to supported divisor value
1030 if (try_divisor <= 8)
1032 // Round up to minimum supported divisor
1035 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1037 // BM doesn't support divisors 9 through 11 inclusive
1040 else if (divisor < 16)
1042 // AM doesn't support divisors 9 through 15 inclusive
1047 if (ftdi->type == TYPE_AM)
1049 // Round up to supported fraction (AM only)
1050 try_divisor += am_adjust_up[try_divisor & 7];
1051 if (try_divisor > 0x1FFF8)
1053 // Round down to maximum supported divisor value (for AM)
1054 try_divisor = 0x1FFF8;
1059 if (try_divisor > 0x1FFFF)
1061 // Round down to maximum supported divisor value (for BM)
1062 try_divisor = 0x1FFFF;
1066 // Get estimated baud rate (to nearest integer)
1067 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1068 // Get absolute difference from requested baud rate
1069 if (baud_estimate < baudrate)
1071 baud_diff = baudrate - baud_estimate;
1075 baud_diff = baud_estimate - baudrate;
1077 if (i == 0 || baud_diff < best_baud_diff)
1079 // Closest to requested baud rate so far
1080 best_divisor = try_divisor;
1081 best_baud = baud_estimate;
1082 best_baud_diff = baud_diff;
1085 // Spot on! No point trying
1090 // Encode the best divisor value
1091 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1092 // Deal with special cases for encoded value
1093 if (encoded_divisor == 1)
1095 encoded_divisor = 0; // 3000000 baud
1097 else if (encoded_divisor == 0x4001)
1099 encoded_divisor = 1; // 2000000 baud (BM only)
1101 // Split into "value" and "index" values
1102 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1103 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1105 *index = (unsigned short)(encoded_divisor >> 8);
1107 *index |= ftdi->index;
1110 *index = (unsigned short)(encoded_divisor >> 16);
1112 // Return the nearest baud rate
1117 Sets the chip baud rate
1119 \param ftdi pointer to ftdi_context
1120 \param baudrate baud rate to set
1123 \retval -1: invalid baudrate
1124 \retval -2: setting baudrate failed
1125 \retval -3: USB device unavailable
1127 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1129 unsigned short value, index;
1130 int actual_baudrate;
1132 if (ftdi == NULL || ftdi->usb_dev == NULL)
1133 ftdi_error_return(-3, "USB device unavailable");
1135 if (ftdi->bitbang_enabled)
1137 baudrate = baudrate*4;
1140 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1141 if (actual_baudrate <= 0)
1142 ftdi_error_return (-1, "Silly baudrate <= 0.");
1144 // Check within tolerance (about 5%)
1145 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1146 || ((actual_baudrate < baudrate)
1147 ? (actual_baudrate * 21 < baudrate * 20)
1148 : (baudrate * 21 < actual_baudrate * 20)))
1149 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1151 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1152 SIO_SET_BAUDRATE_REQUEST, value,
1153 index, NULL, 0, ftdi->usb_write_timeout) != 0)
1154 ftdi_error_return (-2, "Setting new baudrate failed");
1156 ftdi->baudrate = baudrate;
1161 Set (RS232) line characteristics.
1162 The break type can only be set via ftdi_set_line_property2()
1163 and defaults to "off".
1165 \param ftdi pointer to ftdi_context
1166 \param bits Number of bits
1167 \param sbit Number of stop bits
1168 \param parity Parity mode
1171 \retval -1: Setting line property failed
1173 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1174 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1176 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1180 Set (RS232) line characteristics
1182 \param ftdi pointer to ftdi_context
1183 \param bits Number of bits
1184 \param sbit Number of stop bits
1185 \param parity Parity mode
1186 \param break_type Break type
1189 \retval -1: Setting line property failed
1190 \retval -2: USB device unavailable
1192 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1193 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1194 enum ftdi_break_type break_type)
1196 unsigned short value = bits;
1198 if (ftdi == NULL || ftdi->usb_dev == NULL)
1199 ftdi_error_return(-2, "USB device unavailable");
1204 value |= (0x00 << 8);
1207 value |= (0x01 << 8);
1210 value |= (0x02 << 8);
1213 value |= (0x03 << 8);
1216 value |= (0x04 << 8);
1223 value |= (0x00 << 11);
1226 value |= (0x01 << 11);
1229 value |= (0x02 << 11);
1236 value |= (0x00 << 14);
1239 value |= (0x01 << 14);
1243 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1244 SIO_SET_DATA_REQUEST, value,
1245 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1246 ftdi_error_return (-1, "Setting new line property failed");
1252 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1254 \param ftdi pointer to ftdi_context
1255 \param buf Buffer with the data
1256 \param size Size of the buffer
1258 \retval -666: USB device unavailable
1259 \retval <0: error code from usb_bulk_write()
1260 \retval >0: number of bytes written
1262 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1266 int total_written = 0;
1268 if (ftdi == NULL || ftdi->usb_dev == NULL)
1269 ftdi_error_return(-666, "USB device unavailable");
1271 while (offset < size)
1273 int write_size = ftdi->writebuffer_chunksize;
1275 if (offset+write_size > size)
1276 write_size = size-offset;
1278 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
1280 ftdi_error_return(ret, "usb bulk write failed");
1282 total_written += ret;
1283 offset += write_size;
1286 return total_written;
1289 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1290 #ifdef USB_CLASS_PTP
1291 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1293 /* this is strongly dependent on libusb using the same struct layout. If libusb
1294 changes in some later version this may break horribly (this is for libusb 0.1.12) */
1295 struct usb_dev_handle
1298 // some other stuff coming here we don't need
1302 Check for pending async urbs
1305 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
1307 struct usbdevfs_urb *urb;
1311 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1313 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1314 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1322 Wait until one or more async URBs are completed by the kernel and mark their
1323 positions in the async-buffer as unused
1325 \param ftdi pointer to ftdi_context
1326 \param wait_for_more if != 0 wait for more than one write to complete
1327 \param timeout_msec max milliseconds to wait
1331 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
1334 struct usbdevfs_urb *urb=NULL;
1340 FD_SET(ftdi->usb_dev->fd, &writefds);
1342 /* init timeout only once, select writes time left after call */
1343 tv.tv_sec = timeout_msec / 1000;
1344 tv.tv_usec = (timeout_msec % 1000) * 1000;
1348 while (_usb_get_async_urbs_pending(ftdi)
1349 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1352 if (keep_going && !wait_for_more)
1354 /* don't wait if repeating only for keep_going */
1359 /* wait for timeout msec or something written ready */
1360 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1363 if (ret == 0 && urb != NULL)
1365 /* got a free urb, mark it */
1366 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
1368 /* try to get more urbs that are ready now, but don't wait anymore */
1374 /* no more urbs waiting */
1382 Wait until one or more async URBs are completed by the kernel and mark their
1383 positions in the async-buffer as unused.
1385 \param ftdi pointer to ftdi_context
1386 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1388 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1390 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
1394 Stupid libusb does not offer async writes nor does it allow
1395 access to its fd - so we need some hacks here.
1398 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
1400 struct usbdevfs_urb *urb;
1401 int bytesdone = 0, requested;
1402 int ret, cleanup_count;
1407 /* find a free urb buffer we can use */
1409 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1411 if (i==ftdi->async_usb_buffer_size)
1413 /* wait until some buffers are free */
1414 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1417 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1419 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1420 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1421 break; /* found a free urb position */
1426 /* no free urb position found */
1430 requested = size - bytesdone;
1431 if (requested > 4096)
1434 memset(urb,0,sizeof(urb));
1436 urb->type = USBDEVFS_URB_TYPE_BULK;
1439 urb->buffer = bytes + bytesdone;
1440 urb->buffer_length = requested;
1442 urb->actual_length = 0;
1443 urb->number_of_packets = 0;
1444 urb->usercontext = 0;
1448 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1450 while (ret < 0 && errno == EINTR);
1452 return ret; /* the caller can read errno to get more info */
1454 bytesdone += requested;
1456 while (bytesdone < size);
1461 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1462 Does not wait for completion of the transfer nor does it make sure that
1463 the transfer was successful.
1465 This function could be extended to use signals and callbacks to inform the
1466 caller of completion or error - but this is not done yet, volunteers welcome.
1468 Works around libusb and directly accesses functions only available on Linux.
1469 Only available if compiled with --with-async-mode.
1471 \param ftdi pointer to ftdi_context
1472 \param buf Buffer with the data
1473 \param size Size of the buffer
1475 \retval -666: USB device unavailable
1476 \retval <0: error code from usb_bulk_write()
1477 \retval >0: number of bytes written
1479 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1483 int total_written = 0;
1485 if (ftdi == NULL || ftdi->usb_dev == NULL)
1486 ftdi_error_return(-666, "USB device unavailable");
1488 while (offset < size)
1490 int write_size = ftdi->writebuffer_chunksize;
1492 if (offset+write_size > size)
1493 write_size = size-offset;
1495 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1497 ftdi_error_return(ret, "usb bulk write async failed");
1499 total_written += ret;
1500 offset += write_size;
1503 return total_written;
1505 #endif // LIBFTDI_LINUX_ASYNC_MODE
1508 Configure write buffer chunk size.
1511 \param ftdi pointer to ftdi_context
1512 \param chunksize Chunk size
1515 \retval -1: ftdi context invalid
1517 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1520 ftdi_error_return(-1, "ftdi context invalid");
1522 ftdi->writebuffer_chunksize = chunksize;
1527 Get write buffer chunk size.
1529 \param ftdi pointer to ftdi_context
1530 \param chunksize Pointer to store chunk size in
1533 \retval -1: ftdi context invalid
1535 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1538 ftdi_error_return(-1, "ftdi context invalid");
1540 *chunksize = ftdi->writebuffer_chunksize;
1545 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1547 Automatically strips the two modem status bytes transfered during every read.
1549 \param ftdi pointer to ftdi_context
1550 \param buf Buffer to store data in
1551 \param size Size of the buffer
1553 \retval -666: USB device unavailable
1554 \retval <0: error code from usb_bulk_read()
1555 \retval 0: no data was available
1556 \retval >0: number of bytes read
1559 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1561 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1563 struct timeval tv_start, tv_current;
1565 if (ftdi == NULL || ftdi->usb_dev == NULL)
1566 ftdi_error_return(-666, "USB device unavailable");
1568 packet_size = ftdi->max_packet_size;
1569 // Packet size sanity check (avoid division by zero)
1570 if (packet_size == 0)
1571 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1573 // everything we want is still in the readbuffer?
1574 if (size <= ftdi->readbuffer_remaining)
1576 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1579 ftdi->readbuffer_remaining -= size;
1580 ftdi->readbuffer_offset += size;
1582 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1586 // something still in the readbuffer, but not enough to satisfy 'size'?
1587 if (ftdi->readbuffer_remaining != 0)
1589 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1592 offset += ftdi->readbuffer_remaining;
1594 // do the actual USB read
1595 gettimeofday(&tv_start,NULL);
1596 while (offset < size && ret > 0)
1598 ftdi->readbuffer_remaining = 0;
1599 ftdi->readbuffer_offset = 0;
1600 /* returns how much received */
1601 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1603 ftdi_error_return(ret, "usb bulk read failed");
1607 // skip FTDI status bytes.
1608 // Maybe stored in the future to enable modem use
1609 num_of_chunks = ret / packet_size;
1610 chunk_remains = ret % packet_size;
1611 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1613 ftdi->readbuffer_offset += 2;
1616 if (ret > packet_size - 2)
1618 for (i = 1; i < num_of_chunks; i++)
1619 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1620 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1622 if (chunk_remains > 2)
1624 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1625 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1627 ret -= 2*num_of_chunks;
1630 ret -= 2*(num_of_chunks-1)+chunk_remains;
1632 // data still fits in buf?
1633 if (offset+ret <= size)
1635 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1636 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1639 /* Did we read exactly the right amount of bytes? */
1641 //printf("read_data exact rem %d offset %d\n",
1642 //ftdi->readbuffer_remaining, offset);
1647 // only copy part of the data or size <= readbuffer_chunksize
1648 int part_size = size-offset;
1649 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1651 ftdi->readbuffer_offset += part_size;
1652 ftdi->readbuffer_remaining = ret-part_size;
1653 offset += part_size;
1655 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1656 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1661 gettimeofday(&tv_current,NULL);
1662 if(((tv_current.tv_sec - tv_start.tv_sec)*1000000+(tv_current.tv_usec - tv_start.tv_usec))
1663 > ftdi->usb_read_timeout)
1671 Configure read buffer chunk size.
1674 Automatically reallocates the buffer.
1676 \param ftdi pointer to ftdi_context
1677 \param chunksize Chunk size
1680 \retval -1: ftdi context invalid
1682 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1684 unsigned char *new_buf;
1687 ftdi_error_return(-1, "ftdi context invalid");
1689 // Invalidate all remaining data
1690 ftdi->readbuffer_offset = 0;
1691 ftdi->readbuffer_remaining = 0;
1693 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1694 ftdi_error_return(-1, "out of memory for readbuffer");
1696 ftdi->readbuffer = new_buf;
1697 ftdi->readbuffer_chunksize = chunksize;
1703 Get read buffer chunk size.
1705 \param ftdi pointer to ftdi_context
1706 \param chunksize Pointer to store chunk size in
1709 \retval -1: FTDI context invalid
1711 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1714 ftdi_error_return(-1, "FTDI context invalid");
1716 *chunksize = ftdi->readbuffer_chunksize;
1722 Enable bitbang mode.
1724 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1726 \param ftdi pointer to ftdi_context
1727 \param bitmask Bitmask to configure lines.
1728 HIGH/ON value configures a line as output.
1731 \retval -1: can't enable bitbang mode
1732 \retval -2: USB device unavailable
1734 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1736 unsigned short usb_val;
1738 if (ftdi == NULL || ftdi->usb_dev == NULL)
1739 ftdi_error_return(-2, "USB device unavailable");
1741 usb_val = bitmask; // low byte: bitmask
1742 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1743 usb_val |= (ftdi->bitbang_mode << 8);
1745 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1746 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1747 NULL, 0, ftdi->usb_write_timeout) != 0)
1748 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1750 ftdi->bitbang_enabled = 1;
1755 Disable bitbang mode.
1757 \param ftdi pointer to ftdi_context
1760 \retval -1: can't disable bitbang mode
1761 \retval -2: USB device unavailable
1763 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1765 if (ftdi == NULL || ftdi->usb_dev == NULL)
1766 ftdi_error_return(-2, "USB device unavailable");
1768 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1769 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1771 ftdi->bitbang_enabled = 0;
1776 Enable/disable bitbang modes.
1778 \param ftdi pointer to ftdi_context
1779 \param bitmask Bitmask to configure lines.
1780 HIGH/ON value configures a line as output.
1781 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1784 \retval -1: can't enable bitbang mode
1785 \retval -2: USB device unavailable
1787 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1789 unsigned short usb_val;
1791 if (ftdi == NULL || ftdi->usb_dev == NULL)
1792 ftdi_error_return(-2, "USB device unavailable");
1794 usb_val = bitmask; // low byte: bitmask
1795 usb_val |= (mode << 8);
1796 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1797 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1799 ftdi->bitbang_mode = mode;
1800 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1805 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1807 \param ftdi pointer to ftdi_context
1808 \param pins Pointer to store pins into
1811 \retval -1: read pins failed
1812 \retval -2: USB device unavailable
1814 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1816 if (ftdi == NULL || ftdi->usb_dev == NULL)
1817 ftdi_error_return(-2, "USB device unavailable");
1819 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
1820 ftdi_error_return(-1, "read pins failed");
1828 The FTDI chip keeps data in the internal buffer for a specific
1829 amount of time if the buffer is not full yet to decrease
1830 load on the usb bus.
1832 \param ftdi pointer to ftdi_context
1833 \param latency Value between 1 and 255
1836 \retval -1: latency out of range
1837 \retval -2: unable to set latency timer
1838 \retval -3: USB device unavailable
1840 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1842 unsigned short usb_val;
1845 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1847 if (ftdi == NULL || ftdi->usb_dev == NULL)
1848 ftdi_error_return(-3, "USB device unavailable");
1851 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1852 ftdi_error_return(-2, "unable to set latency timer");
1860 \param ftdi pointer to ftdi_context
1861 \param latency Pointer to store latency value in
1864 \retval -1: unable to get latency timer
1865 \retval -2: USB device unavailable
1867 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1869 unsigned short usb_val;
1871 if (ftdi == NULL || ftdi->usb_dev == NULL)
1872 ftdi_error_return(-2, "USB device unavailable");
1874 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1875 ftdi_error_return(-1, "reading latency timer failed");
1877 *latency = (unsigned char)usb_val;
1882 Poll modem status information
1884 This function allows the retrieve the two status bytes of the device.
1885 The device sends these bytes also as a header for each read access
1886 where they are discarded by ftdi_read_data(). The chip generates
1887 the two stripped status bytes in the absence of data every 40 ms.
1889 Layout of the first byte:
1890 - B0..B3 - must be 0
1891 - B4 Clear to send (CTS)
1894 - B5 Data set ready (DTS)
1897 - B6 Ring indicator (RI)
1900 - B7 Receive line signal detect (RLSD)
1904 Layout of the second byte:
1905 - B0 Data ready (DR)
1906 - B1 Overrun error (OE)
1907 - B2 Parity error (PE)
1908 - B3 Framing error (FE)
1909 - B4 Break interrupt (BI)
1910 - B5 Transmitter holding register (THRE)
1911 - B6 Transmitter empty (TEMT)
1912 - B7 Error in RCVR FIFO
1914 \param ftdi pointer to ftdi_context
1915 \param status Pointer to store status information in. Must be two bytes.
1918 \retval -1: unable to retrieve status information
1919 \retval -2: USB device unavailable
1921 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1925 if (ftdi == NULL || ftdi->usb_dev == NULL)
1926 ftdi_error_return(-2, "USB device unavailable");
1928 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, usb_val, 2, ftdi->usb_read_timeout) != 2)
1929 ftdi_error_return(-1, "getting modem status failed");
1931 *status = (usb_val[1] << 8) | usb_val[0];
1937 Set flowcontrol for ftdi chip
1939 \param ftdi pointer to ftdi_context
1940 \param flowctrl flow control to use. should be
1941 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1944 \retval -1: set flow control failed
1945 \retval -2: USB device unavailable
1947 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1949 if (ftdi == NULL || ftdi->usb_dev == NULL)
1950 ftdi_error_return(-2, "USB device unavailable");
1952 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1953 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1954 NULL, 0, ftdi->usb_write_timeout) != 0)
1955 ftdi_error_return(-1, "set flow control failed");
1963 \param ftdi pointer to ftdi_context
1964 \param state state to set line to (1 or 0)
1967 \retval -1: set dtr failed
1968 \retval -2: USB device unavailable
1970 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1972 unsigned short usb_val;
1974 if (ftdi == NULL || ftdi->usb_dev == NULL)
1975 ftdi_error_return(-2, "USB device unavailable");
1978 usb_val = SIO_SET_DTR_HIGH;
1980 usb_val = SIO_SET_DTR_LOW;
1982 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1983 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1984 NULL, 0, ftdi->usb_write_timeout) != 0)
1985 ftdi_error_return(-1, "set dtr failed");
1993 \param ftdi pointer to ftdi_context
1994 \param state state to set line to (1 or 0)
1997 \retval -1: set rts failed
1998 \retval -2: USB device unavailable
2000 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2002 unsigned short usb_val;
2004 if (ftdi == NULL || ftdi->usb_dev == NULL)
2005 ftdi_error_return(-2, "USB device unavailable");
2008 usb_val = SIO_SET_RTS_HIGH;
2010 usb_val = SIO_SET_RTS_LOW;
2012 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2013 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2014 NULL, 0, ftdi->usb_write_timeout) != 0)
2015 ftdi_error_return(-1, "set of rts failed");
2021 Set dtr and rts line in one pass
2023 \param ftdi pointer to ftdi_context
2024 \param dtr DTR state to set line to (1 or 0)
2025 \param rts RTS state to set line to (1 or 0)
2028 \retval -1: set dtr/rts failed
2029 \retval -2: USB device unavailable
2031 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2033 unsigned short usb_val;
2035 if (ftdi == NULL || ftdi->usb_dev == NULL)
2036 ftdi_error_return(-2, "USB device unavailable");
2039 usb_val = SIO_SET_DTR_HIGH;
2041 usb_val = SIO_SET_DTR_LOW;
2044 usb_val |= SIO_SET_RTS_HIGH;
2046 usb_val |= SIO_SET_RTS_LOW;
2048 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2049 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2050 NULL, 0, ftdi->usb_write_timeout) != 0)
2051 ftdi_error_return(-1, "set of rts/dtr failed");
2057 Set the special event character
2059 \param ftdi pointer to ftdi_context
2060 \param eventch Event character
2061 \param enable 0 to disable the event character, non-zero otherwise
2064 \retval -1: unable to set event character
2065 \retval -2: USB device unavailable
2067 int ftdi_set_event_char(struct ftdi_context *ftdi,
2068 unsigned char eventch, unsigned char enable)
2070 unsigned short usb_val;
2072 if (ftdi == NULL || ftdi->usb_dev == NULL)
2073 ftdi_error_return(-2, "USB device unavailable");
2079 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
2080 ftdi_error_return(-1, "setting event character failed");
2088 \param ftdi pointer to ftdi_context
2089 \param errorch Error character
2090 \param enable 0 to disable the error character, non-zero otherwise
2093 \retval -1: unable to set error character
2094 \retval -2: USB device unavailable
2096 int ftdi_set_error_char(struct ftdi_context *ftdi,
2097 unsigned char errorch, unsigned char enable)
2099 unsigned short usb_val;
2101 if (ftdi == NULL || ftdi->usb_dev == NULL)
2102 ftdi_error_return(-2, "USB device unavailable");
2108 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
2109 ftdi_error_return(-1, "setting error character failed");
2117 \param ftdi pointer to ftdi_context
2118 \param eeprom Pointer to ftdi_eeprom
2122 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2127 ftdi->eeprom_size=size;
2132 Init eeprom with default values.
2134 \param eeprom Pointer to ftdi_eeprom
2136 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2143 eeprom->vendor_id = 0x0403;
2144 eeprom->product_id = 0x6001;
2146 eeprom->self_powered = 1;
2147 eeprom->remote_wakeup = 1;
2148 eeprom->chip_type = TYPE_BM;
2150 eeprom->in_is_isochronous = 0;
2151 eeprom->out_is_isochronous = 0;
2152 eeprom->suspend_pull_downs = 0;
2154 eeprom->use_serial = 0;
2155 eeprom->change_usb_version = 0;
2156 eeprom->usb_version = 0x0200;
2157 eeprom->max_power = 0;
2159 eeprom->manufacturer = NULL;
2160 eeprom->product = NULL;
2161 eeprom->serial = NULL;
2162 for (i=0; i < 5; i++)
2164 eeprom->cbus_function[i] = 0;
2166 eeprom->high_current = 0;
2169 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2173 Frees allocated memory in eeprom.
2175 \param eeprom Pointer to ftdi_eeprom
2177 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2182 if (eeprom->manufacturer != 0) {
2183 free(eeprom->manufacturer);
2184 eeprom->manufacturer = 0;
2186 if (eeprom->product != 0) {
2187 free(eeprom->product);
2188 eeprom->product = 0;
2190 if (eeprom->serial != 0) {
2191 free(eeprom->serial);
2197 Build binary output from ftdi_eeprom structure.
2198 Output is suitable for ftdi_write_eeprom().
2200 \note This function doesn't handle FT2232x devices. Only FT232x.
2201 \param eeprom Pointer to ftdi_eeprom
2202 \param output Buffer of 128 bytes to store eeprom image to
2204 \retval >0: free eeprom size
2205 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2206 \retval -2: Invalid eeprom pointer
2207 \retval -3: Invalid cbus function setting
2208 \retval -4: Chip doesn't support invert
2209 \retval -5: Chip doesn't support high current drive
2211 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2214 unsigned short checksum, value;
2215 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2217 const int cbus_max[5] = {13, 13, 13, 13, 9};
2222 if (eeprom->manufacturer != NULL)
2223 manufacturer_size = strlen(eeprom->manufacturer);
2224 if (eeprom->product != NULL)
2225 product_size = strlen(eeprom->product);
2226 if (eeprom->serial != NULL)
2227 serial_size = strlen(eeprom->serial);
2229 // highest allowed cbus value
2230 for (i = 0; i < 5; i++)
2232 if ((eeprom->cbus_function[i] > cbus_max[i]) ||
2233 (eeprom->cbus_function[i] && eeprom->chip_type != TYPE_R)) return -3;
2235 if (eeprom->chip_type != TYPE_R)
2237 if (eeprom->invert) return -4;
2238 if (eeprom->high_current) return -5;
2241 size_check = eeprom->size;
2242 size_check -= 28; // 28 are always in use (fixed)
2244 // Top half of a 256byte eeprom is used just for strings and checksum
2245 // it seems that the FTDI chip will not read these strings from the lower half
2246 // Each string starts with two bytes; offset and type (0x03 for string)
2247 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2248 if (eeprom->size>=256) size_check = 120;
2249 size_check -= manufacturer_size*2;
2250 size_check -= product_size*2;
2251 size_check -= serial_size*2;
2253 // eeprom size exceeded?
2258 memset (output, 0, eeprom->size);
2260 // Addr 00: High current IO
2261 output[0x00] = eeprom->high_current ? HIGH_CURRENT_DRIVE : 0;
2262 // Addr 01: IN endpoint size (for R type devices, different for FT2232)
2263 if (eeprom->chip_type == TYPE_R) {
2264 output[0x01] = 0x40;
2266 // Addr 02: Vendor ID
2267 output[0x02] = eeprom->vendor_id;
2268 output[0x03] = eeprom->vendor_id >> 8;
2270 // Addr 04: Product ID
2271 output[0x04] = eeprom->product_id;
2272 output[0x05] = eeprom->product_id >> 8;
2274 // Addr 06: Device release number (0400h for BM features)
2275 output[0x06] = 0x00;
2276 switch (eeprom->chip_type) {
2278 output[0x07] = 0x02;
2281 output[0x07] = 0x04;
2284 output[0x07] = 0x05;
2287 output[0x07] = 0x06;
2290 output[0x07] = 0x00;
2293 // Addr 08: Config descriptor
2295 // Bit 6: 1 if this device is self powered, 0 if bus powered
2296 // Bit 5: 1 if this device uses remote wakeup
2297 // Bit 4: 1 if this device is battery powered
2299 if (eeprom->self_powered == 1)
2301 if (eeprom->remote_wakeup == 1)
2305 // Addr 09: Max power consumption: max power = value * 2 mA
2306 output[0x09] = eeprom->max_power;
2308 // Addr 0A: Chip configuration
2309 // Bit 7: 0 - reserved
2310 // Bit 6: 0 - reserved
2311 // Bit 5: 0 - reserved
2312 // Bit 4: 1 - Change USB version
2313 // Bit 3: 1 - Use the serial number string
2314 // Bit 2: 1 - Enable suspend pull downs for lower power
2315 // Bit 1: 1 - Out EndPoint is Isochronous
2316 // Bit 0: 1 - In EndPoint is Isochronous
2319 if (eeprom->in_is_isochronous == 1)
2321 if (eeprom->out_is_isochronous == 1)
2323 if (eeprom->suspend_pull_downs == 1)
2325 if (eeprom->use_serial == 1)
2327 if (eeprom->change_usb_version == 1)
2331 // Addr 0B: Invert data lines
2332 output[0x0B] = eeprom->invert & 0xff;
2334 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2335 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2336 if (eeprom->change_usb_version == 1)
2338 output[0x0C] = eeprom->usb_version;
2339 output[0x0D] = eeprom->usb_version >> 8;
2343 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2344 // Addr 0F: Length of manufacturer string
2345 output[0x0F] = manufacturer_size*2 + 2;
2347 // Addr 10: Offset of the product string + 0x80, calculated later
2348 // Addr 11: Length of product string
2349 output[0x11] = product_size*2 + 2;
2351 // Addr 12: Offset of the serial string + 0x80, calculated later
2352 // Addr 13: Length of serial string
2353 output[0x13] = serial_size*2 + 2;
2355 // Addr 14: CBUS function: CBUS0, CBUS1
2356 // Addr 15: CBUS function: CBUS2, CBUS3
2357 // Addr 16: CBUS function: CBUS5
2358 output[0x14] = eeprom->cbus_function[0] | (eeprom->cbus_function[1] << 4);
2359 output[0x15] = eeprom->cbus_function[2] | (eeprom->cbus_function[3] << 4);
2360 output[0x16] = eeprom->cbus_function[4];
2364 // In images produced by FTDI's FT_Prog for FT232R strings start at 0x18
2365 // Space till 0x18 should be considered as reserved.
2366 if (eeprom->chip_type >= TYPE_R) {
2371 if (eeprom->size >= 256) i = 0x80;
2374 // Output manufacturer
2375 output[0x0E] = i | 0x80; // calculate offset
2376 output[i++] = manufacturer_size*2 + 2;
2377 output[i++] = 0x03; // type: string
2378 for (j = 0; j < manufacturer_size; j++)
2380 output[i] = eeprom->manufacturer[j], i++;
2381 output[i] = 0x00, i++;
2384 // Output product name
2385 output[0x10] = i | 0x80; // calculate offset
2386 output[i] = product_size*2 + 2, i++;
2387 output[i] = 0x03, i++;
2388 for (j = 0; j < product_size; j++)
2390 output[i] = eeprom->product[j], i++;
2391 output[i] = 0x00, i++;
2395 output[0x12] = i | 0x80; // calculate offset
2396 output[i] = serial_size*2 + 2, i++;
2397 output[i] = 0x03, i++;
2398 for (j = 0; j < serial_size; j++)
2400 output[i] = eeprom->serial[j], i++;
2401 output[i] = 0x00, i++;
2404 // calculate checksum
2407 for (i = 0; i < eeprom->size/2-1; i++)
2409 value = output[i*2];
2410 value += output[(i*2)+1] << 8;
2412 checksum = value^checksum;
2413 checksum = (checksum << 1) | (checksum >> 15);
2416 output[eeprom->size-2] = checksum;
2417 output[eeprom->size-1] = checksum >> 8;
2423 Decode binary EEPROM image into an ftdi_eeprom structure.
2425 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2426 \param buf Buffer of \a size bytes of raw eeprom data
2427 \param size size size of eeprom data in bytes
2430 \retval -1: something went wrong
2432 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2433 FIXME: Strings are malloc'ed here and should be freed somewhere
2435 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2438 unsigned short checksum, eeprom_checksum, value;
2439 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2441 int eeprom_size = 128;
2446 size_check = eeprom->size;
2447 size_check -= 28; // 28 are always in use (fixed)
2449 // Top half of a 256byte eeprom is used just for strings and checksum
2450 // it seems that the FTDI chip will not read these strings from the lower half
2451 // Each string starts with two bytes; offset and type (0x03 for string)
2452 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2453 if (eeprom->size>=256)size_check = 120;
2454 size_check -= manufacturer_size*2;
2455 size_check -= product_size*2;
2456 size_check -= serial_size*2;
2458 // eeprom size exceeded?
2463 // empty eeprom struct
2464 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2466 // Addr 00: High current IO
2467 eeprom->high_current = (buf[0x02] & HIGH_CURRENT_DRIVE);
2469 // Addr 02: Vendor ID
2470 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2472 // Addr 04: Product ID
2473 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2475 value = buf[0x06] + (buf[0x07]<<8);
2479 eeprom->chip_type = TYPE_R;
2482 eeprom->chip_type = TYPE_BM;
2485 eeprom->chip_type = TYPE_AM;
2487 default: // Unknown device
2488 eeprom->chip_type = 0;
2492 // Addr 08: Config descriptor
2494 // Bit 6: 1 if this device is self powered, 0 if bus powered
2495 // Bit 5: 1 if this device uses remote wakeup
2496 // Bit 4: 1 if this device is battery powered
2498 if (j&0x40) eeprom->self_powered = 1;
2499 if (j&0x20) eeprom->remote_wakeup = 1;
2501 // Addr 09: Max power consumption: max power = value * 2 mA
2502 eeprom->max_power = buf[0x09];
2504 // Addr 0A: Chip configuration
2505 // Bit 7: 0 - reserved
2506 // Bit 6: 0 - reserved
2507 // Bit 5: 0 - reserved
2508 // Bit 4: 1 - Change USB version
2509 // Bit 3: 1 - Use the serial number string
2510 // Bit 2: 1 - Enable suspend pull downs for lower power
2511 // Bit 1: 1 - Out EndPoint is Isochronous
2512 // Bit 0: 1 - In EndPoint is Isochronous
2515 if (j&0x01) eeprom->in_is_isochronous = 1;
2516 if (j&0x02) eeprom->out_is_isochronous = 1;
2517 if (j&0x04) eeprom->suspend_pull_downs = 1;
2518 if (j&0x08) eeprom->use_serial = 1;
2519 if (j&0x10) eeprom->change_usb_version = 1;
2521 // Addr 0B: Invert data lines
2522 eeprom->invert = buf[0x0B];
2524 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2525 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2526 if (eeprom->change_usb_version == 1)
2528 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2531 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2532 // Addr 0F: Length of manufacturer string
2533 manufacturer_size = buf[0x0F]/2;
2534 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2535 else eeprom->manufacturer = NULL;
2537 // Addr 10: Offset of the product string + 0x80, calculated later
2538 // Addr 11: Length of product string
2539 product_size = buf[0x11]/2;
2540 if (product_size > 0) eeprom->product = malloc(product_size);
2541 else eeprom->product = NULL;
2543 // Addr 12: Offset of the serial string + 0x80, calculated later
2544 // Addr 13: Length of serial string
2545 serial_size = buf[0x13]/2;
2546 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2547 else eeprom->serial = NULL;
2549 // Addr 14: CBUS function: CBUS0, CBUS1
2550 // Addr 15: CBUS function: CBUS2, CBUS3
2551 // Addr 16: CBUS function: CBUS5
2552 if (eeprom->chip_type == TYPE_R) {
2553 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2554 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2555 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2556 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2557 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2559 for (j=0; j<5; j++) eeprom->cbus_function[j] = 0;
2562 // Decode manufacturer
2563 i = buf[0x0E] & 0x7f; // offset
2564 for (j=0;j<manufacturer_size-1;j++)
2566 eeprom->manufacturer[j] = buf[2*j+i+2];
2568 eeprom->manufacturer[j] = '\0';
2570 // Decode product name
2571 i = buf[0x10] & 0x7f; // offset
2572 for (j=0;j<product_size-1;j++)
2574 eeprom->product[j] = buf[2*j+i+2];
2576 eeprom->product[j] = '\0';
2579 i = buf[0x12] & 0x7f; // offset
2580 for (j=0;j<serial_size-1;j++)
2582 eeprom->serial[j] = buf[2*j+i+2];
2584 eeprom->serial[j] = '\0';
2589 for (i = 0; i < eeprom_size/2-1; i++)
2592 value += buf[(i*2)+1] << 8;
2594 checksum = value^checksum;
2595 checksum = (checksum << 1) | (checksum >> 15);
2598 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2600 if (eeprom_checksum != checksum)
2602 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2610 Read eeprom location
2612 \param ftdi pointer to ftdi_context
2613 \param eeprom_addr Address of eeprom location to be read
2614 \param eeprom_val Pointer to store read eeprom location
2617 \retval -1: read failed
2618 \retval -2: USB device unavailable
2620 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2622 if (ftdi == NULL || ftdi->usb_dev == NULL)
2623 ftdi_error_return(-2, "USB device unavailable");
2625 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
2626 ftdi_error_return(-1, "reading eeprom failed");
2634 \param ftdi pointer to ftdi_context
2635 \param eeprom Pointer to store eeprom into
2638 \retval -1: read failed
2639 \retval -2: USB device unavailable
2641 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2645 if (ftdi == NULL || ftdi->usb_dev == NULL)
2646 ftdi_error_return(-2, "USB device unavailable");
2648 for (i = 0; i < ftdi->eeprom_size/2; i++)
2650 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2651 ftdi_error_return(-1, "reading eeprom failed");
2658 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2659 Function is only used internally
2662 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2664 return ((value & 1) << 1) |
2665 ((value & 2) << 5) |
2666 ((value & 4) >> 2) |
2667 ((value & 8) << 4) |
2668 ((value & 16) >> 1) |
2669 ((value & 32) >> 1) |
2670 ((value & 64) >> 4) |
2671 ((value & 128) >> 2);
2675 Read the FTDIChip-ID from R-type devices
2677 \param ftdi pointer to ftdi_context
2678 \param chipid Pointer to store FTDIChip-ID
2681 \retval -1: read failed
2682 \retval -2: USB device unavailable
2684 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2686 unsigned int a = 0, b = 0;
2688 if (ftdi == NULL || ftdi->usb_dev == NULL)
2689 ftdi_error_return(-2, "USB device unavailable");
2691 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
2693 a = a << 8 | a >> 8;
2694 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
2696 b = b << 8 | b >> 8;
2697 a = (a << 16) | (b & 0xFFFF);
2698 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2699 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2700 *chipid = a ^ 0xa5f0f7d1;
2705 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2709 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2710 Call this function then do a write then call again to see if size changes, if so write again.
2712 \param ftdi pointer to ftdi_context
2713 \param eeprom Pointer to store eeprom into
2714 \param maxsize the size of the buffer to read into
2716 \retval -1: eeprom read failed
2717 \retval -2: USB device unavailable
2718 \retval >=0: size of eeprom
2720 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2722 int i=0,j,minsize=32;
2725 if (ftdi == NULL || ftdi->usb_dev == NULL)
2726 ftdi_error_return(-2, "USB device unavailable");
2730 for (j = 0; i < maxsize/2 && j<size; j++)
2732 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2733 SIO_READ_EEPROM_REQUEST, 0, i,
2734 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2735 ftdi_error_return(-1, "eeprom read failed");
2740 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2746 Write eeprom location
2748 \param ftdi pointer to ftdi_context
2749 \param eeprom_addr Address of eeprom location to be written
2750 \param eeprom_val Value to be written
2753 \retval -1: read failed
2754 \retval -2: USB device unavailable
2756 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2758 if (ftdi == NULL || ftdi->usb_dev == NULL)
2759 ftdi_error_return(-2, "USB device unavailable");
2761 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2762 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2763 NULL, 0, ftdi->usb_write_timeout) != 0)
2764 ftdi_error_return(-1, "unable to write eeprom");
2772 \param ftdi pointer to ftdi_context
2773 \param eeprom Pointer to read eeprom from
2776 \retval -1: read failed
2777 \retval -2: USB device unavailable
2779 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2781 unsigned short usb_val, status;
2784 if (ftdi == NULL || ftdi->usb_dev == NULL)
2785 ftdi_error_return(-2, "USB device unavailable");
2787 /* These commands were traced while running MProg */
2788 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2790 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2792 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2795 for (i = 0; i < ftdi->eeprom_size/2; i++)
2797 usb_val = eeprom[i*2];
2798 usb_val += eeprom[(i*2)+1] << 8;
2799 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2800 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2801 NULL, 0, ftdi->usb_write_timeout) != 0)
2802 ftdi_error_return(-1, "unable to write eeprom");
2811 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2813 \param ftdi pointer to ftdi_context
2816 \retval -1: erase failed
2817 \retval -2: USB device unavailable
2819 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2821 if (ftdi == NULL || ftdi->usb_dev == NULL)
2822 ftdi_error_return(-2, "USB device unavailable");
2824 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
2825 ftdi_error_return(-1, "unable to erase eeprom");
2831 Get string representation for last error code
2833 \param ftdi pointer to ftdi_context
2835 \retval Pointer to error string
2837 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2842 return ftdi->error_str;
2845 /* @} end of doxygen libftdi group */