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>
42 #include <sys/select.h>
43 #include <sys/types.h>
45 #include <linux/usbdevice_fs.h>
48 #define ftdi_error_return(code, str) do { \
49 ftdi->error_str = str; \
54 #if defined( __WIN32__) && !defined(__MINGW32__)
56 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
61 int gettimeofday( struct timeval *tv, void null)
64 unsigned __int64 tmpres = 0;
67 GetSystemTimeAsFileTime(&ft);
68 tmpres |= ft.dwHighDateTime;
70 tmpres |= ft.dwLowDateTime;
72 /*converting file time to unix epoch*/
73 tmpres /= 10; /*convert into microseconds*/
74 tmpres -= DELTA_EPOCH_IN_MICROSECS;
75 tv->tv_sec = (LONG)(tmpres / 1000000UL);
76 tv->tv_usec = (LONG)(tmpres % 1000000UL);
78 /* Warning: Timezone not handled (and not needed here) */
84 Internal function to close usb device pointer.
85 Sets ftdi->usb_dev to NULL.
88 \param ftdi pointer to ftdi_context
90 \retval zero if all is fine, otherwise error code from usb_close()
92 static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
96 if (ftdi && ftdi->usb_dev)
98 ret = usb_close (ftdi->usb_dev);
106 Initializes a ftdi_context.
108 \param ftdi pointer to ftdi_context
111 \retval -1: couldn't allocate read buffer
113 \remark This should be called before all functions
115 int ftdi_init(struct ftdi_context *ftdi)
119 ftdi->usb_dev = NULL;
120 ftdi->usb_read_timeout = 5000;
121 ftdi->usb_write_timeout = 5000;
123 ftdi->type = TYPE_BM; /* chip type */
125 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
127 ftdi->readbuffer = NULL;
128 ftdi->readbuffer_offset = 0;
129 ftdi->readbuffer_remaining = 0;
130 ftdi->writebuffer_chunksize = 4096;
131 ftdi->max_packet_size = 0;
137 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
139 ftdi->error_str = NULL;
141 #ifdef LIBFTDI_LINUX_ASYNC_MODE
142 ftdi->async_usb_buffer_size=10;
143 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
144 ftdi_error_return(-1, "out of memory for async usb buffer");
146 /* initialize async usb buffer with unused-marker */
147 for (i=0; i < ftdi->async_usb_buffer_size; i++)
148 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
150 ftdi->async_usb_buffer_size=0;
151 ftdi->async_usb_buffer = NULL;
154 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
156 /* All fine. Now allocate the readbuffer */
157 return ftdi_read_data_set_chunksize(ftdi, 4096);
161 Allocate and initialize a new ftdi_context
163 \return a pointer to a new ftdi_context, or NULL on failure
165 struct ftdi_context *ftdi_new(void)
167 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
174 if (ftdi_init(ftdi) != 0)
184 Open selected channels on a chip, otherwise use first channel.
186 \param ftdi pointer to ftdi_context
187 \param interface Interface to use for FT2232C/2232H/4232H chips.
190 \retval -1: unknown interface
191 \retval -2: USB device unavailable
193 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
196 ftdi_error_return(-2, "USB device unavailable");
202 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
206 ftdi->index = INTERFACE_B;
212 ftdi->index = INTERFACE_C;
218 ftdi->index = INTERFACE_D;
223 ftdi_error_return(-1, "Unknown interface");
229 Deinitializes a ftdi_context.
231 \param ftdi pointer to ftdi_context
233 void ftdi_deinit(struct ftdi_context *ftdi)
238 ftdi_usb_close_internal (ftdi);
240 if (ftdi->async_usb_buffer != NULL)
242 free(ftdi->async_usb_buffer);
243 ftdi->async_usb_buffer = NULL;
246 if (ftdi->readbuffer != NULL)
248 free(ftdi->readbuffer);
249 ftdi->readbuffer = NULL;
254 Deinitialize and free an ftdi_context.
256 \param ftdi pointer to ftdi_context
258 void ftdi_free(struct ftdi_context *ftdi)
265 Use an already open libusb device.
267 \param ftdi pointer to ftdi_context
268 \param usb libusb usb_dev_handle to use
270 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
280 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
281 needs to be deallocated by ftdi_list_free() after use.
283 \param ftdi pointer to ftdi_context
284 \param devlist Pointer where to store list of found devices
285 \param vendor Vendor ID to search for
286 \param product Product ID to search for
288 \retval >0: number of devices found
289 \retval -1: usb_find_busses() failed
290 \retval -2: usb_find_devices() failed
291 \retval -3: out of memory
293 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
295 struct ftdi_device_list **curdev;
297 struct usb_device *dev;
301 if (usb_find_busses() < 0)
302 ftdi_error_return(-1, "usb_find_busses() failed");
303 if (usb_find_devices() < 0)
304 ftdi_error_return(-2, "usb_find_devices() failed");
308 for (bus = usb_get_busses(); bus; bus = bus->next)
310 for (dev = bus->devices; dev; dev = dev->next)
312 if (dev->descriptor.idVendor == vendor
313 && dev->descriptor.idProduct == product)
315 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
317 ftdi_error_return(-3, "out of memory");
319 (*curdev)->next = NULL;
320 (*curdev)->dev = dev;
322 curdev = &(*curdev)->next;
332 Frees a usb device list.
334 \param devlist USB device list created by ftdi_usb_find_all()
336 void ftdi_list_free(struct ftdi_device_list **devlist)
338 struct ftdi_device_list *curdev, *next;
340 for (curdev = *devlist; curdev != NULL;)
351 Frees a usb device list.
353 \param devlist USB device list created by ftdi_usb_find_all()
355 void ftdi_list_free2(struct ftdi_device_list *devlist)
357 ftdi_list_free(&devlist);
361 Return device ID strings from the usb device.
363 The parameters manufacturer, description and serial may be NULL
364 or pointer to buffers to store the fetched strings.
366 \note Use this function only in combination with ftdi_usb_find_all()
367 as it closes the internal "usb_dev" after use.
369 \param ftdi pointer to ftdi_context
370 \param dev libusb usb_dev to use
371 \param manufacturer Store manufacturer string here if not NULL
372 \param mnf_len Buffer size of manufacturer string
373 \param description Store product description string here if not NULL
374 \param desc_len Buffer size of product description string
375 \param serial Store serial string here if not NULL
376 \param serial_len Buffer size of serial string
379 \retval -1: wrong arguments
380 \retval -4: unable to open device
381 \retval -7: get product manufacturer failed
382 \retval -8: get product description failed
383 \retval -9: get serial number failed
384 \retval -10: unable to close device
386 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
387 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
389 if ((ftdi==NULL) || (dev==NULL))
392 if (!(ftdi->usb_dev = usb_open(dev)))
393 ftdi_error_return(-4, usb_strerror());
395 if (manufacturer != NULL)
397 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
399 ftdi_usb_close_internal (ftdi);
400 ftdi_error_return(-7, usb_strerror());
404 if (description != NULL)
406 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
408 ftdi_usb_close_internal (ftdi);
409 ftdi_error_return(-8, usb_strerror());
415 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
417 ftdi_usb_close_internal (ftdi);
418 ftdi_error_return(-9, usb_strerror());
422 if (ftdi_usb_close_internal (ftdi) != 0)
423 ftdi_error_return(-10, usb_strerror());
429 * Internal function to determine the maximum packet size.
430 * \param ftdi pointer to ftdi_context
431 * \param dev libusb usb_dev to use
432 * \retval Maximum packet size for this device
434 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
436 unsigned int packet_size;
439 if (ftdi == NULL || dev == NULL)
442 // Determine maximum packet size. Init with default value.
443 // New hi-speed devices from FTDI use a packet size of 512 bytes
444 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
445 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
450 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
452 struct usb_config_descriptor config = dev->config[0];
454 if (ftdi->interface < config.bNumInterfaces)
456 struct usb_interface interface = config.interface[ftdi->interface];
457 if (interface.num_altsetting > 0)
459 struct usb_interface_descriptor descriptor = interface.altsetting[0];
460 if (descriptor.bNumEndpoints > 0)
462 packet_size = descriptor.endpoint[0].wMaxPacketSize;
472 Opens a ftdi device given by an usb_device.
474 \param ftdi pointer to ftdi_context
475 \param dev libusb usb_dev to use
478 \retval -3: unable to config device
479 \retval -4: unable to open device
480 \retval -5: unable to claim device
481 \retval -6: reset failed
482 \retval -7: set baudrate failed
483 \retval -8: ftdi context invalid
485 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
487 int detach_errno = 0;
491 ftdi_error_return(-8, "ftdi context invalid");
493 if (!(ftdi->usb_dev = usb_open(dev)))
494 ftdi_error_return(-4, "usb_open() failed");
496 #ifdef LIBUSB_HAS_GET_DRIVER_NP
497 // Try to detach ftdi_sio kernel module.
498 // Returns ENODATA if driver is not loaded.
500 // The return code is kept in a separate variable and only parsed
501 // if usb_set_configuration() or usb_claim_interface() fails as the
502 // detach operation might be denied and everything still works fine.
503 // Likely scenario is a static ftdi_sio kernel module.
504 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
505 detach_errno = errno;
509 // set configuration (needed especially for windows)
510 // tolerate EBUSY: one device with one configuration, but two interfaces
511 // and libftdi sessions to both interfaces (e.g. FT2232)
513 if (dev->descriptor.bNumConfigurations > 0)
515 // libusb-win32 on Windows 64 can return a null pointer for a valid device
517 config_val = dev->config[0].bConfigurationValue;
519 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
522 ftdi_usb_close_internal (ftdi);
523 if (detach_errno == EPERM)
525 ftdi_error_return(-8, "inappropriate permissions on device!");
529 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
535 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
537 ftdi_usb_close_internal (ftdi);
538 if (detach_errno == EPERM)
540 ftdi_error_return(-8, "inappropriate permissions on device!");
544 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
548 if (ftdi_usb_reset (ftdi) != 0)
550 ftdi_usb_close_internal (ftdi);
551 ftdi_error_return(-6, "ftdi_usb_reset failed");
554 // Try to guess chip type
555 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
556 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
557 && dev->descriptor.iSerialNumber == 0))
558 ftdi->type = TYPE_BM;
559 else if (dev->descriptor.bcdDevice == 0x200)
560 ftdi->type = TYPE_AM;
561 else if (dev->descriptor.bcdDevice == 0x500)
562 ftdi->type = TYPE_2232C;
563 else if (dev->descriptor.bcdDevice == 0x600)
565 else if (dev->descriptor.bcdDevice == 0x700)
566 ftdi->type = TYPE_2232H;
567 else if (dev->descriptor.bcdDevice == 0x800)
568 ftdi->type = TYPE_4232H;
570 // Set default interface on dual/quad type chips
577 ftdi->index = INTERFACE_A;
583 // Determine maximum packet size
584 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
586 if (ftdi_set_baudrate (ftdi, 9600) != 0)
588 ftdi_usb_close_internal (ftdi);
589 ftdi_error_return(-7, "set baudrate failed");
592 ftdi_error_return(0, "all fine");
596 Opens the first device with a given vendor and product ids.
598 \param ftdi pointer to ftdi_context
599 \param vendor Vendor ID
600 \param product Product ID
602 \retval same as ftdi_usb_open_desc()
604 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
606 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
610 Opens the first device with a given, vendor id, product id,
611 description and serial.
613 \param ftdi pointer to ftdi_context
614 \param vendor Vendor ID
615 \param product Product ID
616 \param description Description to search for. Use NULL if not needed.
617 \param serial Serial to search for. Use NULL if not needed.
620 \retval -1: usb_find_busses() failed
621 \retval -2: usb_find_devices() failed
622 \retval -3: usb device not found
623 \retval -4: unable to open device
624 \retval -5: unable to claim device
625 \retval -6: reset failed
626 \retval -7: set baudrate failed
627 \retval -8: get product description failed
628 \retval -9: get serial number failed
629 \retval -10: unable to close device
631 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
632 const char* description, const char* serial)
634 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
638 Opens the index-th device with a given, vendor id, product id,
639 description and serial.
641 \param ftdi pointer to ftdi_context
642 \param vendor Vendor ID
643 \param product Product ID
644 \param description Description to search for. Use NULL if not needed.
645 \param serial Serial to search for. Use NULL if not needed.
646 \param index Number of matching device to open if there are more than one, starts with 0.
649 \retval -1: usb_find_busses() failed
650 \retval -2: usb_find_devices() failed
651 \retval -3: usb device not found
652 \retval -4: unable to open device
653 \retval -5: unable to claim device
654 \retval -6: reset failed
655 \retval -7: set baudrate failed
656 \retval -8: get product description failed
657 \retval -9: get serial number failed
658 \retval -10: unable to close device
659 \retval -11: ftdi context invalid
661 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
662 const char* description, const char* serial, unsigned int index)
665 struct usb_device *dev;
670 if (usb_find_busses() < 0)
671 ftdi_error_return(-1, "usb_find_busses() failed");
672 if (usb_find_devices() < 0)
673 ftdi_error_return(-2, "usb_find_devices() failed");
676 ftdi_error_return(-11, "ftdi context invalid");
678 for (bus = usb_get_busses(); bus; bus = bus->next)
680 for (dev = bus->devices; dev; dev = dev->next)
682 if (dev->descriptor.idVendor == vendor
683 && dev->descriptor.idProduct == product)
685 if (!(ftdi->usb_dev = usb_open(dev)))
686 ftdi_error_return(-4, "usb_open() failed");
688 if (description != NULL)
690 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
692 ftdi_usb_close_internal (ftdi);
693 ftdi_error_return(-8, "unable to fetch product description");
695 if (strncmp(string, description, sizeof(string)) != 0)
697 if (ftdi_usb_close_internal (ftdi) != 0)
698 ftdi_error_return(-10, "unable to close device");
704 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
706 ftdi_usb_close_internal (ftdi);
707 ftdi_error_return(-9, "unable to fetch serial number");
709 if (strncmp(string, serial, sizeof(string)) != 0)
711 if (ftdi_usb_close_internal (ftdi) != 0)
712 ftdi_error_return(-10, "unable to close device");
717 if (ftdi_usb_close_internal (ftdi) != 0)
718 ftdi_error_return(-10, "unable to close device");
726 return ftdi_usb_open_dev(ftdi, dev);
732 ftdi_error_return(-3, "device not found");
736 Opens the ftdi-device described by a description-string.
737 Intended to be used for parsing a device-description given as commandline argument.
739 \param ftdi pointer to ftdi_context
740 \param description NULL-terminated description-string, using this format:
741 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
742 \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")
743 \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
744 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
746 \note The description format may be extended in later versions.
749 \retval -1: usb_find_busses() failed
750 \retval -2: usb_find_devices() failed
751 \retval -3: usb device not found
752 \retval -4: unable to open device
753 \retval -5: unable to claim device
754 \retval -6: reset failed
755 \retval -7: set baudrate failed
756 \retval -8: get product description failed
757 \retval -9: get serial number failed
758 \retval -10: unable to close device
759 \retval -11: illegal description format
760 \retval -12: ftdi context invalid
762 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
765 ftdi_error_return(-12, "ftdi context invalid");
767 if (description[0] == 0 || description[1] != ':')
768 ftdi_error_return(-11, "illegal description format");
770 if (description[0] == 'd')
773 struct usb_device *dev;
777 if (usb_find_busses() < 0)
778 ftdi_error_return(-1, "usb_find_busses() failed");
779 if (usb_find_devices() < 0)
780 ftdi_error_return(-2, "usb_find_devices() failed");
782 for (bus = usb_get_busses(); bus; bus = bus->next)
784 for (dev = bus->devices; dev; dev = dev->next)
786 /* XXX: This doesn't handle symlinks/odd paths/etc... */
787 const char *desc = description + 2;
788 size_t len = strlen(bus->dirname);
789 if (strncmp(desc, bus->dirname, len))
795 if (strcmp(desc, dev->filename))
797 return ftdi_usb_open_dev(ftdi, dev);
802 ftdi_error_return(-3, "device not found");
804 else if (description[0] == 'i' || description[0] == 's')
807 unsigned int product;
808 unsigned int index=0;
809 const char *serial=NULL;
810 const char *startp, *endp;
813 startp=description+2;
814 vendor=strtoul((char*)startp,(char**)&endp,0);
815 if (*endp != ':' || endp == startp || errno != 0)
816 ftdi_error_return(-11, "illegal description format");
819 product=strtoul((char*)startp,(char**)&endp,0);
820 if (endp == startp || errno != 0)
821 ftdi_error_return(-11, "illegal description format");
823 if (description[0] == 'i' && *endp != 0)
825 /* optional index field in i-mode */
827 ftdi_error_return(-11, "illegal description format");
830 index=strtoul((char*)startp,(char**)&endp,0);
831 if (*endp != 0 || endp == startp || errno != 0)
832 ftdi_error_return(-11, "illegal description format");
834 if (description[0] == 's')
837 ftdi_error_return(-11, "illegal description format");
839 /* rest of the description is the serial */
843 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
847 ftdi_error_return(-11, "illegal description format");
852 Resets the ftdi device.
854 \param ftdi pointer to ftdi_context
857 \retval -1: FTDI reset failed
858 \retval -2: USB device unavailable
860 int ftdi_usb_reset(struct ftdi_context *ftdi)
862 if (ftdi == NULL || ftdi->usb_dev == NULL)
863 ftdi_error_return(-2, "USB device unavailable");
865 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
866 SIO_RESET_REQUEST, SIO_RESET_SIO,
867 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
868 ftdi_error_return(-1,"FTDI reset failed");
870 // Invalidate data in the readbuffer
871 ftdi->readbuffer_offset = 0;
872 ftdi->readbuffer_remaining = 0;
878 Clears the read buffer on the chip and the internal read buffer.
880 \param ftdi pointer to ftdi_context
883 \retval -1: read buffer purge failed
884 \retval -2: USB device unavailable
886 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
888 if (ftdi == NULL || ftdi->usb_dev == NULL)
889 ftdi_error_return(-2, "USB device unavailable");
891 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
892 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
893 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
894 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
896 // Invalidate data in the readbuffer
897 ftdi->readbuffer_offset = 0;
898 ftdi->readbuffer_remaining = 0;
904 Clears the write buffer on the chip.
906 \param ftdi pointer to ftdi_context
909 \retval -1: write buffer purge failed
910 \retval -2: USB device unavailable
912 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
914 if (ftdi == NULL || ftdi->usb_dev == NULL)
915 ftdi_error_return(-2, "USB device unavailable");
917 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
918 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
919 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
920 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
926 Clears the buffers on the chip and the internal read buffer.
928 \param ftdi pointer to ftdi_context
931 \retval -1: read buffer purge failed
932 \retval -2: write buffer purge failed
933 \retval -3: USB device unavailable
935 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
939 if (ftdi == NULL || ftdi->usb_dev == NULL)
940 ftdi_error_return(-3, "USB device unavailable");
942 result = ftdi_usb_purge_rx_buffer(ftdi);
946 result = ftdi_usb_purge_tx_buffer(ftdi);
956 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
958 \param ftdi pointer to ftdi_context
961 \retval -1: usb_release failed
962 \retval -2: usb_close failed
963 \retval -3: ftdi context invalid
965 int ftdi_usb_close(struct ftdi_context *ftdi)
970 ftdi_error_return(-3, "ftdi context invalid");
972 #ifdef LIBFTDI_LINUX_ASYNC_MODE
973 /* try to release some kernel resources */
974 ftdi_async_complete(ftdi,1);
977 if (ftdi->usb_dev != NULL)
978 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
981 if (ftdi_usb_close_internal (ftdi) != 0)
988 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
989 Function is only used internally
992 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
993 unsigned short *value, unsigned short *index)
995 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
996 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
997 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
998 int divisor, best_divisor, best_baud, best_baud_diff;
999 unsigned long encoded_divisor;
1008 divisor = 24000000 / baudrate;
1010 if (ftdi->type == TYPE_AM)
1012 // Round down to supported fraction (AM only)
1013 divisor -= am_adjust_dn[divisor & 7];
1016 // Try this divisor and the one above it (because division rounds down)
1020 for (i = 0; i < 2; i++)
1022 int try_divisor = divisor + i;
1026 // Round up to supported divisor value
1027 if (try_divisor <= 8)
1029 // Round up to minimum supported divisor
1032 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1034 // BM doesn't support divisors 9 through 11 inclusive
1037 else if (divisor < 16)
1039 // AM doesn't support divisors 9 through 15 inclusive
1044 if (ftdi->type == TYPE_AM)
1046 // Round up to supported fraction (AM only)
1047 try_divisor += am_adjust_up[try_divisor & 7];
1048 if (try_divisor > 0x1FFF8)
1050 // Round down to maximum supported divisor value (for AM)
1051 try_divisor = 0x1FFF8;
1056 if (try_divisor > 0x1FFFF)
1058 // Round down to maximum supported divisor value (for BM)
1059 try_divisor = 0x1FFFF;
1063 // Get estimated baud rate (to nearest integer)
1064 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1065 // Get absolute difference from requested baud rate
1066 if (baud_estimate < baudrate)
1068 baud_diff = baudrate - baud_estimate;
1072 baud_diff = baud_estimate - baudrate;
1074 if (i == 0 || baud_diff < best_baud_diff)
1076 // Closest to requested baud rate so far
1077 best_divisor = try_divisor;
1078 best_baud = baud_estimate;
1079 best_baud_diff = baud_diff;
1082 // Spot on! No point trying
1087 // Encode the best divisor value
1088 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1089 // Deal with special cases for encoded value
1090 if (encoded_divisor == 1)
1092 encoded_divisor = 0; // 3000000 baud
1094 else if (encoded_divisor == 0x4001)
1096 encoded_divisor = 1; // 2000000 baud (BM only)
1098 // Split into "value" and "index" values
1099 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1100 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1102 *index = (unsigned short)(encoded_divisor >> 8);
1104 *index |= ftdi->index;
1107 *index = (unsigned short)(encoded_divisor >> 16);
1109 // Return the nearest baud rate
1114 Sets the chip baud rate
1116 \param ftdi pointer to ftdi_context
1117 \param baudrate baud rate to set
1120 \retval -1: invalid baudrate
1121 \retval -2: setting baudrate failed
1122 \retval -3: USB device unavailable
1124 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1126 unsigned short value, index;
1127 int actual_baudrate;
1129 if (ftdi == NULL || ftdi->usb_dev == NULL)
1130 ftdi_error_return(-3, "USB device unavailable");
1132 if (ftdi->bitbang_enabled)
1134 baudrate = baudrate*4;
1137 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1138 if (actual_baudrate <= 0)
1139 ftdi_error_return (-1, "Silly baudrate <= 0.");
1141 // Check within tolerance (about 5%)
1142 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1143 || ((actual_baudrate < baudrate)
1144 ? (actual_baudrate * 21 < baudrate * 20)
1145 : (baudrate * 21 < actual_baudrate * 20)))
1146 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1148 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1149 SIO_SET_BAUDRATE_REQUEST, value,
1150 index, NULL, 0, ftdi->usb_write_timeout) != 0)
1151 ftdi_error_return (-2, "Setting new baudrate failed");
1153 ftdi->baudrate = baudrate;
1158 Set (RS232) line characteristics.
1159 The break type can only be set via ftdi_set_line_property2()
1160 and defaults to "off".
1162 \param ftdi pointer to ftdi_context
1163 \param bits Number of bits
1164 \param sbit Number of stop bits
1165 \param parity Parity mode
1168 \retval -1: Setting line property failed
1170 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1171 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1173 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1177 Set (RS232) line characteristics
1179 \param ftdi pointer to ftdi_context
1180 \param bits Number of bits
1181 \param sbit Number of stop bits
1182 \param parity Parity mode
1183 \param break_type Break type
1186 \retval -1: Setting line property failed
1187 \retval -2: USB device unavailable
1189 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1190 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1191 enum ftdi_break_type break_type)
1193 unsigned short value = bits;
1195 if (ftdi == NULL || ftdi->usb_dev == NULL)
1196 ftdi_error_return(-2, "USB device unavailable");
1201 value |= (0x00 << 8);
1204 value |= (0x01 << 8);
1207 value |= (0x02 << 8);
1210 value |= (0x03 << 8);
1213 value |= (0x04 << 8);
1220 value |= (0x00 << 11);
1223 value |= (0x01 << 11);
1226 value |= (0x02 << 11);
1233 value |= (0x00 << 14);
1236 value |= (0x01 << 14);
1240 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1241 SIO_SET_DATA_REQUEST, value,
1242 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1243 ftdi_error_return (-1, "Setting new line property failed");
1249 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1251 \param ftdi pointer to ftdi_context
1252 \param buf Buffer with the data
1253 \param size Size of the buffer
1255 \retval -666: USB device unavailable
1256 \retval <0: error code from usb_bulk_write()
1257 \retval >0: number of bytes written
1259 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1263 int total_written = 0;
1265 if (ftdi == NULL || ftdi->usb_dev == NULL)
1266 ftdi_error_return(-666, "USB device unavailable");
1268 while (offset < size)
1270 int write_size = ftdi->writebuffer_chunksize;
1272 if (offset+write_size > size)
1273 write_size = size-offset;
1275 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
1277 ftdi_error_return(ret, "usb bulk write failed");
1279 total_written += ret;
1280 offset += write_size;
1283 return total_written;
1286 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1287 #ifdef USB_CLASS_PTP
1288 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1290 /* this is strongly dependent on libusb using the same struct layout. If libusb
1291 changes in some later version this may break horribly (this is for libusb 0.1.12) */
1292 struct usb_dev_handle
1295 // some other stuff coming here we don't need
1299 Check for pending async urbs
1302 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
1304 struct usbdevfs_urb *urb;
1308 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1310 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1311 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1319 Wait until one or more async URBs are completed by the kernel and mark their
1320 positions in the async-buffer as unused
1322 \param ftdi pointer to ftdi_context
1323 \param wait_for_more if != 0 wait for more than one write to complete
1324 \param timeout_msec max milliseconds to wait
1328 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
1331 struct usbdevfs_urb *urb=NULL;
1337 FD_SET(ftdi->usb_dev->fd, &writefds);
1339 /* init timeout only once, select writes time left after call */
1340 tv.tv_sec = timeout_msec / 1000;
1341 tv.tv_usec = (timeout_msec % 1000) * 1000;
1345 while (_usb_get_async_urbs_pending(ftdi)
1346 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1349 if (keep_going && !wait_for_more)
1351 /* don't wait if repeating only for keep_going */
1356 /* wait for timeout msec or something written ready */
1357 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1360 if (ret == 0 && urb != NULL)
1362 /* got a free urb, mark it */
1363 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
1365 /* try to get more urbs that are ready now, but don't wait anymore */
1371 /* no more urbs waiting */
1379 Wait until one or more async URBs are completed by the kernel and mark their
1380 positions in the async-buffer as unused.
1382 \param ftdi pointer to ftdi_context
1383 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1385 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1387 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
1391 Stupid libusb does not offer async writes nor does it allow
1392 access to its fd - so we need some hacks here.
1395 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
1397 struct usbdevfs_urb *urb;
1398 int bytesdone = 0, requested;
1399 int ret, cleanup_count;
1404 /* find a free urb buffer we can use */
1406 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1408 if (i==ftdi->async_usb_buffer_size)
1410 /* wait until some buffers are free */
1411 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1414 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1416 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1417 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1418 break; /* found a free urb position */
1423 /* no free urb position found */
1427 requested = size - bytesdone;
1428 if (requested > 4096)
1431 memset(urb,0,sizeof(urb));
1433 urb->type = USBDEVFS_URB_TYPE_BULK;
1436 urb->buffer = bytes + bytesdone;
1437 urb->buffer_length = requested;
1439 urb->actual_length = 0;
1440 urb->number_of_packets = 0;
1441 urb->usercontext = 0;
1445 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1447 while (ret < 0 && errno == EINTR);
1449 return ret; /* the caller can read errno to get more info */
1451 bytesdone += requested;
1453 while (bytesdone < size);
1458 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1459 Does not wait for completion of the transfer nor does it make sure that
1460 the transfer was successful.
1462 This function could be extended to use signals and callbacks to inform the
1463 caller of completion or error - but this is not done yet, volunteers welcome.
1465 Works around libusb and directly accesses functions only available on Linux.
1466 Only available if compiled with --with-async-mode.
1468 \param ftdi pointer to ftdi_context
1469 \param buf Buffer with the data
1470 \param size Size of the buffer
1472 \retval -666: USB device unavailable
1473 \retval <0: error code from usb_bulk_write()
1474 \retval >0: number of bytes written
1476 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1480 int total_written = 0;
1482 if (ftdi == NULL || ftdi->usb_dev == NULL)
1483 ftdi_error_return(-666, "USB device unavailable");
1485 while (offset < size)
1487 int write_size = ftdi->writebuffer_chunksize;
1489 if (offset+write_size > size)
1490 write_size = size-offset;
1492 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1494 ftdi_error_return(ret, "usb bulk write async failed");
1496 total_written += ret;
1497 offset += write_size;
1500 return total_written;
1502 #endif // LIBFTDI_LINUX_ASYNC_MODE
1505 Configure write buffer chunk size.
1508 \param ftdi pointer to ftdi_context
1509 \param chunksize Chunk size
1512 \retval -1: ftdi context invalid
1514 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1517 ftdi_error_return(-1, "ftdi context invalid");
1519 ftdi->writebuffer_chunksize = chunksize;
1524 Get write buffer chunk size.
1526 \param ftdi pointer to ftdi_context
1527 \param chunksize Pointer to store chunk size in
1530 \retval -1: ftdi context invalid
1532 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1535 ftdi_error_return(-1, "ftdi context invalid");
1537 *chunksize = ftdi->writebuffer_chunksize;
1542 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1544 Automatically strips the two modem status bytes transfered during every read.
1546 \param ftdi pointer to ftdi_context
1547 \param buf Buffer to store data in
1548 \param size Size of the buffer
1550 \retval -666: USB device unavailable
1551 \retval <0: error code from usb_bulk_read()
1552 \retval 0: no data was available
1553 \retval >0: number of bytes read
1556 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1558 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1560 struct timeval tv_start, tv_current;
1562 if (ftdi == NULL || ftdi->usb_dev == NULL)
1563 ftdi_error_return(-666, "USB device unavailable");
1565 packet_size = ftdi->max_packet_size;
1566 // Packet size sanity check (avoid division by zero)
1567 if (packet_size == 0)
1568 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1570 // everything we want is still in the readbuffer?
1571 if (size <= ftdi->readbuffer_remaining)
1573 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1576 ftdi->readbuffer_remaining -= size;
1577 ftdi->readbuffer_offset += size;
1579 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1583 // something still in the readbuffer, but not enough to satisfy 'size'?
1584 if (ftdi->readbuffer_remaining != 0)
1586 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1589 offset += ftdi->readbuffer_remaining;
1591 // do the actual USB read
1592 gettimeofday(&tv_start,NULL);
1593 while (offset < size && ret > 0)
1595 ftdi->readbuffer_remaining = 0;
1596 ftdi->readbuffer_offset = 0;
1597 /* returns how much received */
1598 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1600 ftdi_error_return(ret, "usb bulk read failed");
1604 // skip FTDI status bytes.
1605 // Maybe stored in the future to enable modem use
1606 num_of_chunks = ret / packet_size;
1607 chunk_remains = ret % packet_size;
1608 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1610 ftdi->readbuffer_offset += 2;
1613 if (ret > packet_size - 2)
1615 for (i = 1; i < num_of_chunks; i++)
1616 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1617 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1619 if (chunk_remains > 2)
1621 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1622 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1624 ret -= 2*num_of_chunks;
1627 ret -= 2*(num_of_chunks-1)+chunk_remains;
1629 // data still fits in buf?
1630 if (offset+ret <= size)
1632 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1633 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1636 /* Did we read exactly the right amount of bytes? */
1638 //printf("read_data exact rem %d offset %d\n",
1639 //ftdi->readbuffer_remaining, offset);
1644 // only copy part of the data or size <= readbuffer_chunksize
1645 int part_size = size-offset;
1646 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1648 ftdi->readbuffer_offset += part_size;
1649 ftdi->readbuffer_remaining = ret-part_size;
1650 offset += part_size;
1652 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1653 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1657 gettimeofday(&tv_current,NULL);
1658 if(((tv_current.tv_sec - tv_start.tv_sec)*1000000+(tv_current.tv_usec - tv_start.tv_usec))
1659 > ftdi->usb_read_timeout)
1667 Configure read buffer chunk size.
1670 Automatically reallocates the buffer.
1672 \param ftdi pointer to ftdi_context
1673 \param chunksize Chunk size
1676 \retval -1: ftdi context invalid
1678 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1680 unsigned char *new_buf;
1683 ftdi_error_return(-1, "ftdi context invalid");
1685 // Invalidate all remaining data
1686 ftdi->readbuffer_offset = 0;
1687 ftdi->readbuffer_remaining = 0;
1689 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1690 ftdi_error_return(-1, "out of memory for readbuffer");
1692 ftdi->readbuffer = new_buf;
1693 ftdi->readbuffer_chunksize = chunksize;
1699 Get read buffer chunk size.
1701 \param ftdi pointer to ftdi_context
1702 \param chunksize Pointer to store chunk size in
1705 \retval -1: FTDI context invalid
1707 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1710 ftdi_error_return(-1, "FTDI context invalid");
1712 *chunksize = ftdi->readbuffer_chunksize;
1718 Enable bitbang mode.
1720 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1722 \param ftdi pointer to ftdi_context
1723 \param bitmask Bitmask to configure lines.
1724 HIGH/ON value configures a line as output.
1727 \retval -1: can't enable bitbang mode
1728 \retval -2: USB device unavailable
1730 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1732 unsigned short usb_val;
1734 if (ftdi == NULL || ftdi->usb_dev == NULL)
1735 ftdi_error_return(-2, "USB device unavailable");
1737 usb_val = bitmask; // low byte: bitmask
1738 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1739 usb_val |= (ftdi->bitbang_mode << 8);
1741 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1742 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1743 NULL, 0, ftdi->usb_write_timeout) != 0)
1744 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1746 ftdi->bitbang_enabled = 1;
1751 Disable bitbang mode.
1753 \param ftdi pointer to ftdi_context
1756 \retval -1: can't disable bitbang mode
1757 \retval -2: USB device unavailable
1759 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1761 if (ftdi == NULL || ftdi->usb_dev == NULL)
1762 ftdi_error_return(-2, "USB device unavailable");
1764 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)
1765 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1767 ftdi->bitbang_enabled = 0;
1772 Enable/disable bitbang modes.
1774 \param ftdi pointer to ftdi_context
1775 \param bitmask Bitmask to configure lines.
1776 HIGH/ON value configures a line as output.
1777 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1780 \retval -1: can't enable bitbang mode
1781 \retval -2: USB device unavailable
1783 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1785 unsigned short usb_val;
1787 if (ftdi == NULL || ftdi->usb_dev == NULL)
1788 ftdi_error_return(-2, "USB device unavailable");
1790 usb_val = bitmask; // low byte: bitmask
1791 usb_val |= (mode << 8);
1792 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)
1793 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1795 ftdi->bitbang_mode = mode;
1796 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1801 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1803 \param ftdi pointer to ftdi_context
1804 \param pins Pointer to store pins into
1807 \retval -1: read pins failed
1808 \retval -2: USB device unavailable
1810 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1812 if (ftdi == NULL || ftdi->usb_dev == NULL)
1813 ftdi_error_return(-2, "USB device unavailable");
1815 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)
1816 ftdi_error_return(-1, "read pins failed");
1824 The FTDI chip keeps data in the internal buffer for a specific
1825 amount of time if the buffer is not full yet to decrease
1826 load on the usb bus.
1828 \param ftdi pointer to ftdi_context
1829 \param latency Value between 1 and 255
1832 \retval -1: latency out of range
1833 \retval -2: unable to set latency timer
1834 \retval -3: USB device unavailable
1836 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1838 unsigned short usb_val;
1841 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1843 if (ftdi == NULL || ftdi->usb_dev == NULL)
1844 ftdi_error_return(-3, "USB device unavailable");
1847 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)
1848 ftdi_error_return(-2, "unable to set latency timer");
1856 \param ftdi pointer to ftdi_context
1857 \param latency Pointer to store latency value in
1860 \retval -1: unable to get latency timer
1861 \retval -2: USB device unavailable
1863 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1865 unsigned short usb_val;
1867 if (ftdi == NULL || ftdi->usb_dev == NULL)
1868 ftdi_error_return(-2, "USB device unavailable");
1870 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)
1871 ftdi_error_return(-1, "reading latency timer failed");
1873 *latency = (unsigned char)usb_val;
1878 Poll modem status information
1880 This function allows the retrieve the two status bytes of the device.
1881 The device sends these bytes also as a header for each read access
1882 where they are discarded by ftdi_read_data(). The chip generates
1883 the two stripped status bytes in the absence of data every 40 ms.
1885 Layout of the first byte:
1886 - B0..B3 - must be 0
1887 - B4 Clear to send (CTS)
1890 - B5 Data set ready (DTS)
1893 - B6 Ring indicator (RI)
1896 - B7 Receive line signal detect (RLSD)
1900 Layout of the second byte:
1901 - B0 Data ready (DR)
1902 - B1 Overrun error (OE)
1903 - B2 Parity error (PE)
1904 - B3 Framing error (FE)
1905 - B4 Break interrupt (BI)
1906 - B5 Transmitter holding register (THRE)
1907 - B6 Transmitter empty (TEMT)
1908 - B7 Error in RCVR FIFO
1910 \param ftdi pointer to ftdi_context
1911 \param status Pointer to store status information in. Must be two bytes.
1914 \retval -1: unable to retrieve status information
1915 \retval -2: USB device unavailable
1917 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1921 if (ftdi == NULL || ftdi->usb_dev == NULL)
1922 ftdi_error_return(-2, "USB device unavailable");
1924 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)
1925 ftdi_error_return(-1, "getting modem status failed");
1927 *status = (usb_val[1] << 8) | usb_val[0];
1933 Set flowcontrol for ftdi chip
1935 \param ftdi pointer to ftdi_context
1936 \param flowctrl flow control to use. should be
1937 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1940 \retval -1: set flow control failed
1941 \retval -2: USB device unavailable
1943 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1945 if (ftdi == NULL || ftdi->usb_dev == NULL)
1946 ftdi_error_return(-2, "USB device unavailable");
1948 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1949 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1950 NULL, 0, ftdi->usb_write_timeout) != 0)
1951 ftdi_error_return(-1, "set flow control failed");
1959 \param ftdi pointer to ftdi_context
1960 \param state state to set line to (1 or 0)
1963 \retval -1: set dtr failed
1964 \retval -2: USB device unavailable
1966 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1968 unsigned short usb_val;
1970 if (ftdi == NULL || ftdi->usb_dev == NULL)
1971 ftdi_error_return(-2, "USB device unavailable");
1974 usb_val = SIO_SET_DTR_HIGH;
1976 usb_val = SIO_SET_DTR_LOW;
1978 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1979 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1980 NULL, 0, ftdi->usb_write_timeout) != 0)
1981 ftdi_error_return(-1, "set dtr failed");
1989 \param ftdi pointer to ftdi_context
1990 \param state state to set line to (1 or 0)
1993 \retval -1: set rts failed
1994 \retval -2: USB device unavailable
1996 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1998 unsigned short usb_val;
2000 if (ftdi == NULL || ftdi->usb_dev == NULL)
2001 ftdi_error_return(-2, "USB device unavailable");
2004 usb_val = SIO_SET_RTS_HIGH;
2006 usb_val = SIO_SET_RTS_LOW;
2008 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2009 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2010 NULL, 0, ftdi->usb_write_timeout) != 0)
2011 ftdi_error_return(-1, "set of rts failed");
2017 Set dtr and rts line in one pass
2019 \param ftdi pointer to ftdi_context
2020 \param dtr DTR state to set line to (1 or 0)
2021 \param rts RTS state to set line to (1 or 0)
2024 \retval -1: set dtr/rts failed
2025 \retval -2: USB device unavailable
2027 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2029 unsigned short usb_val;
2031 if (ftdi == NULL || ftdi->usb_dev == NULL)
2032 ftdi_error_return(-2, "USB device unavailable");
2035 usb_val = SIO_SET_DTR_HIGH;
2037 usb_val = SIO_SET_DTR_LOW;
2040 usb_val |= SIO_SET_RTS_HIGH;
2042 usb_val |= SIO_SET_RTS_LOW;
2044 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2045 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2046 NULL, 0, ftdi->usb_write_timeout) != 0)
2047 ftdi_error_return(-1, "set of rts/dtr failed");
2053 Set the special event character
2055 \param ftdi pointer to ftdi_context
2056 \param eventch Event character
2057 \param enable 0 to disable the event character, non-zero otherwise
2060 \retval -1: unable to set event character
2061 \retval -2: USB device unavailable
2063 int ftdi_set_event_char(struct ftdi_context *ftdi,
2064 unsigned char eventch, unsigned char enable)
2066 unsigned short usb_val;
2068 if (ftdi == NULL || ftdi->usb_dev == NULL)
2069 ftdi_error_return(-2, "USB device unavailable");
2075 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)
2076 ftdi_error_return(-1, "setting event character failed");
2084 \param ftdi pointer to ftdi_context
2085 \param errorch Error character
2086 \param enable 0 to disable the error character, non-zero otherwise
2089 \retval -1: unable to set error character
2090 \retval -2: USB device unavailable
2092 int ftdi_set_error_char(struct ftdi_context *ftdi,
2093 unsigned char errorch, unsigned char enable)
2095 unsigned short usb_val;
2097 if (ftdi == NULL || ftdi->usb_dev == NULL)
2098 ftdi_error_return(-2, "USB device unavailable");
2104 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)
2105 ftdi_error_return(-1, "setting error character failed");
2113 \param ftdi pointer to ftdi_context
2114 \param eeprom Pointer to ftdi_eeprom
2118 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2123 ftdi->eeprom_size=size;
2128 Init eeprom with default values.
2130 \param eeprom Pointer to ftdi_eeprom
2132 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2137 eeprom->vendor_id = 0x0403;
2138 eeprom->product_id = 0x6001;
2140 eeprom->self_powered = 1;
2141 eeprom->remote_wakeup = 1;
2142 eeprom->BM_type_chip = 1;
2144 eeprom->in_is_isochronous = 0;
2145 eeprom->out_is_isochronous = 0;
2146 eeprom->suspend_pull_downs = 0;
2148 eeprom->use_serial = 0;
2149 eeprom->change_usb_version = 0;
2150 eeprom->usb_version = 0x0200;
2151 eeprom->max_power = 0;
2153 eeprom->manufacturer = NULL;
2154 eeprom->product = NULL;
2155 eeprom->serial = NULL;
2157 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2161 Frees allocated memory in eeprom.
2163 \param eeprom Pointer to ftdi_eeprom
2165 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2170 if (eeprom->manufacturer != 0) {
2171 free(eeprom->manufacturer);
2172 eeprom->manufacturer = 0;
2174 if (eeprom->product != 0) {
2175 free(eeprom->product);
2176 eeprom->product = 0;
2178 if (eeprom->serial != 0) {
2179 free(eeprom->serial);
2185 Build binary output from ftdi_eeprom structure.
2186 Output is suitable for ftdi_write_eeprom().
2188 \param eeprom Pointer to ftdi_eeprom
2189 \param output Buffer of 128 bytes to store eeprom image to
2191 \retval >0: used eeprom size
2192 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2193 \retval -2: Invalid eeprom pointer
2195 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2198 unsigned short checksum, value;
2199 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2205 if (eeprom->manufacturer != NULL)
2206 manufacturer_size = strlen(eeprom->manufacturer);
2207 if (eeprom->product != NULL)
2208 product_size = strlen(eeprom->product);
2209 if (eeprom->serial != NULL)
2210 serial_size = strlen(eeprom->serial);
2212 size_check = eeprom->size;
2213 size_check -= 28; // 28 are always in use (fixed)
2215 // Top half of a 256byte eeprom is used just for strings and checksum
2216 // it seems that the FTDI chip will not read these strings from the lower half
2217 // Each string starts with two bytes; offset and type (0x03 for string)
2218 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2219 if (eeprom->size>=256)size_check = 120;
2220 size_check -= manufacturer_size*2;
2221 size_check -= product_size*2;
2222 size_check -= serial_size*2;
2224 // eeprom size exceeded?
2229 memset (output, 0, eeprom->size);
2231 // Addr 00: Stay 00 00
2232 // Addr 02: Vendor ID
2233 output[0x02] = eeprom->vendor_id;
2234 output[0x03] = eeprom->vendor_id >> 8;
2236 // Addr 04: Product ID
2237 output[0x04] = eeprom->product_id;
2238 output[0x05] = eeprom->product_id >> 8;
2240 // Addr 06: Device release number (0400h for BM features)
2241 output[0x06] = 0x00;
2243 if (eeprom->BM_type_chip == 1)
2244 output[0x07] = 0x04;
2246 output[0x07] = 0x02;
2248 // Addr 08: Config descriptor
2250 // Bit 6: 1 if this device is self powered, 0 if bus powered
2251 // Bit 5: 1 if this device uses remote wakeup
2252 // Bit 4: 1 if this device is battery powered
2254 if (eeprom->self_powered == 1)
2256 if (eeprom->remote_wakeup == 1)
2260 // Addr 09: Max power consumption: max power = value * 2 mA
2261 output[0x09] = eeprom->max_power;
2263 // Addr 0A: Chip configuration
2264 // Bit 7: 0 - reserved
2265 // Bit 6: 0 - reserved
2266 // Bit 5: 0 - reserved
2267 // Bit 4: 1 - Change USB version
2268 // Bit 3: 1 - Use the serial number string
2269 // Bit 2: 1 - Enable suspend pull downs for lower power
2270 // Bit 1: 1 - Out EndPoint is Isochronous
2271 // Bit 0: 1 - In EndPoint is Isochronous
2274 if (eeprom->in_is_isochronous == 1)
2276 if (eeprom->out_is_isochronous == 1)
2278 if (eeprom->suspend_pull_downs == 1)
2280 if (eeprom->use_serial == 1)
2282 if (eeprom->change_usb_version == 1)
2286 // Addr 0B: reserved
2287 output[0x0B] = 0x00;
2289 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2290 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2291 if (eeprom->change_usb_version == 1)
2293 output[0x0C] = eeprom->usb_version;
2294 output[0x0D] = eeprom->usb_version >> 8;
2298 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2299 // Addr 0F: Length of manufacturer string
2300 output[0x0F] = manufacturer_size*2 + 2;
2302 // Addr 10: Offset of the product string + 0x80, calculated later
2303 // Addr 11: Length of product string
2304 output[0x11] = product_size*2 + 2;
2306 // Addr 12: Offset of the serial string + 0x80, calculated later
2307 // Addr 13: Length of serial string
2308 output[0x13] = serial_size*2 + 2;
2312 if (eeprom->size>=256) i = 0x80;
2315 // Output manufacturer
2316 output[0x0E] = i | 0x80; // calculate offset
2317 output[i++] = manufacturer_size*2 + 2;
2318 output[i++] = 0x03; // type: string
2319 for (j = 0; j < manufacturer_size; j++)
2321 output[i] = eeprom->manufacturer[j], i++;
2322 output[i] = 0x00, i++;
2325 // Output product name
2326 output[0x10] = i | 0x80; // calculate offset
2327 output[i] = product_size*2 + 2, i++;
2328 output[i] = 0x03, i++;
2329 for (j = 0; j < product_size; j++)
2331 output[i] = eeprom->product[j], i++;
2332 output[i] = 0x00, i++;
2336 output[0x12] = i | 0x80; // calculate offset
2337 output[i] = serial_size*2 + 2, i++;
2338 output[i] = 0x03, i++;
2339 for (j = 0; j < serial_size; j++)
2341 output[i] = eeprom->serial[j], i++;
2342 output[i] = 0x00, i++;
2345 // calculate checksum
2348 for (i = 0; i < eeprom->size/2-1; i++)
2350 value = output[i*2];
2351 value += output[(i*2)+1] << 8;
2353 checksum = value^checksum;
2354 checksum = (checksum << 1) | (checksum >> 15);
2357 output[eeprom->size-2] = checksum;
2358 output[eeprom->size-1] = checksum >> 8;
2364 Decode binary EEPROM image into an ftdi_eeprom structure.
2366 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2367 \param buf Buffer of \a size bytes of raw eeprom data
2368 \param size size size of eeprom data in bytes
2371 \retval -1: something went wrong
2373 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2374 FIXME: Strings are malloc'ed here and should be freed somewhere
2376 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2379 unsigned short checksum, eeprom_checksum, value;
2380 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2382 int eeprom_size = 128;
2387 size_check = eeprom->size;
2388 size_check -= 28; // 28 are always in use (fixed)
2390 // Top half of a 256byte eeprom is used just for strings and checksum
2391 // it seems that the FTDI chip will not read these strings from the lower half
2392 // Each string starts with two bytes; offset and type (0x03 for string)
2393 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2394 if (eeprom->size>=256)size_check = 120;
2395 size_check -= manufacturer_size*2;
2396 size_check -= product_size*2;
2397 size_check -= serial_size*2;
2399 // eeprom size exceeded?
2404 // empty eeprom struct
2405 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2407 // Addr 00: Stay 00 00
2409 // Addr 02: Vendor ID
2410 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2412 // Addr 04: Product ID
2413 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2415 value = buf[0x06] + (buf[0x07]<<8);
2419 eeprom->BM_type_chip = 1;
2422 eeprom->BM_type_chip = 0;
2424 default: // Unknown device
2425 eeprom->BM_type_chip = 0;
2429 // Addr 08: Config descriptor
2431 // Bit 6: 1 if this device is self powered, 0 if bus powered
2432 // Bit 5: 1 if this device uses remote wakeup
2433 // Bit 4: 1 if this device is battery powered
2435 if (j&0x40) eeprom->self_powered = 1;
2436 if (j&0x20) eeprom->remote_wakeup = 1;
2438 // Addr 09: Max power consumption: max power = value * 2 mA
2439 eeprom->max_power = buf[0x09];
2441 // Addr 0A: Chip configuration
2442 // Bit 7: 0 - reserved
2443 // Bit 6: 0 - reserved
2444 // Bit 5: 0 - reserved
2445 // Bit 4: 1 - Change USB version
2446 // Bit 3: 1 - Use the serial number string
2447 // Bit 2: 1 - Enable suspend pull downs for lower power
2448 // Bit 1: 1 - Out EndPoint is Isochronous
2449 // Bit 0: 1 - In EndPoint is Isochronous
2452 if (j&0x01) eeprom->in_is_isochronous = 1;
2453 if (j&0x02) eeprom->out_is_isochronous = 1;
2454 if (j&0x04) eeprom->suspend_pull_downs = 1;
2455 if (j&0x08) eeprom->use_serial = 1;
2456 if (j&0x10) eeprom->change_usb_version = 1;
2458 // Addr 0B: reserved
2460 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2461 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2462 if (eeprom->change_usb_version == 1)
2464 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2467 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2468 // Addr 0F: Length of manufacturer string
2469 manufacturer_size = buf[0x0F]/2;
2470 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2471 else eeprom->manufacturer = NULL;
2473 // Addr 10: Offset of the product string + 0x80, calculated later
2474 // Addr 11: Length of product string
2475 product_size = buf[0x11]/2;
2476 if (product_size > 0) eeprom->product = malloc(product_size);
2477 else eeprom->product = NULL;
2479 // Addr 12: Offset of the serial string + 0x80, calculated later
2480 // Addr 13: Length of serial string
2481 serial_size = buf[0x13]/2;
2482 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2483 else eeprom->serial = NULL;
2485 // Decode manufacturer
2486 i = buf[0x0E] & 0x7f; // offset
2487 for (j=0;j<manufacturer_size-1;j++)
2489 eeprom->manufacturer[j] = buf[2*j+i+2];
2491 eeprom->manufacturer[j] = '\0';
2493 // Decode product name
2494 i = buf[0x10] & 0x7f; // offset
2495 for (j=0;j<product_size-1;j++)
2497 eeprom->product[j] = buf[2*j+i+2];
2499 eeprom->product[j] = '\0';
2502 i = buf[0x12] & 0x7f; // offset
2503 for (j=0;j<serial_size-1;j++)
2505 eeprom->serial[j] = buf[2*j+i+2];
2507 eeprom->serial[j] = '\0';
2512 for (i = 0; i < eeprom_size/2-1; i++)
2515 value += buf[(i*2)+1] << 8;
2517 checksum = value^checksum;
2518 checksum = (checksum << 1) | (checksum >> 15);
2521 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2523 if (eeprom_checksum != checksum)
2525 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2533 Read eeprom location
2535 \param ftdi pointer to ftdi_context
2536 \param eeprom_addr Address of eeprom location to be read
2537 \param eeprom_val Pointer to store read eeprom location
2540 \retval -1: read failed
2541 \retval -2: USB device unavailable
2543 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2545 if (ftdi == NULL || ftdi->usb_dev == NULL)
2546 ftdi_error_return(-2, "USB device unavailable");
2548 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)
2549 ftdi_error_return(-1, "reading eeprom failed");
2557 \param ftdi pointer to ftdi_context
2558 \param eeprom Pointer to store eeprom into
2561 \retval -1: read failed
2562 \retval -2: USB device unavailable
2564 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2568 if (ftdi == NULL || ftdi->usb_dev == NULL)
2569 ftdi_error_return(-2, "USB device unavailable");
2571 for (i = 0; i < ftdi->eeprom_size/2; i++)
2573 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)
2574 ftdi_error_return(-1, "reading eeprom failed");
2581 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2582 Function is only used internally
2585 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2587 return ((value & 1) << 1) |
2588 ((value & 2) << 5) |
2589 ((value & 4) >> 2) |
2590 ((value & 8) << 4) |
2591 ((value & 16) >> 1) |
2592 ((value & 32) >> 1) |
2593 ((value & 64) >> 4) |
2594 ((value & 128) >> 2);
2598 Read the FTDIChip-ID from R-type devices
2600 \param ftdi pointer to ftdi_context
2601 \param chipid Pointer to store FTDIChip-ID
2604 \retval -1: read failed
2605 \retval -2: USB device unavailable
2607 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2609 unsigned int a = 0, b = 0;
2611 if (ftdi == NULL || ftdi->usb_dev == NULL)
2612 ftdi_error_return(-2, "USB device unavailable");
2614 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)
2616 a = a << 8 | a >> 8;
2617 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)
2619 b = b << 8 | b >> 8;
2620 a = (a << 16) | (b & 0xFFFF);
2621 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2622 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2623 *chipid = a ^ 0xa5f0f7d1;
2628 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2632 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2633 Call this function then do a write then call again to see if size changes, if so write again.
2635 \param ftdi pointer to ftdi_context
2636 \param eeprom Pointer to store eeprom into
2637 \param maxsize the size of the buffer to read into
2639 \retval -1: eeprom read failed
2640 \retval -2: USB device unavailable
2641 \retval >=0: size of eeprom
2643 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2645 int i=0,j,minsize=32;
2648 if (ftdi == NULL || ftdi->usb_dev == NULL)
2649 ftdi_error_return(-2, "USB device unavailable");
2653 for (j = 0; i < maxsize/2 && j<size; j++)
2655 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2656 SIO_READ_EEPROM_REQUEST, 0, i,
2657 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2658 ftdi_error_return(-1, "eeprom read failed");
2663 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2669 Write eeprom location
2671 \param ftdi pointer to ftdi_context
2672 \param eeprom_addr Address of eeprom location to be written
2673 \param eeprom_val Value to be written
2676 \retval -1: read failed
2677 \retval -2: USB device unavailable
2679 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2681 if (ftdi == NULL || ftdi->usb_dev == NULL)
2682 ftdi_error_return(-2, "USB device unavailable");
2684 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2685 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2686 NULL, 0, ftdi->usb_write_timeout) != 0)
2687 ftdi_error_return(-1, "unable to write eeprom");
2695 \param ftdi pointer to ftdi_context
2696 \param eeprom Pointer to read eeprom from
2699 \retval -1: read failed
2700 \retval -2: USB device unavailable
2702 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2704 unsigned short usb_val, status;
2707 if (ftdi == NULL || ftdi->usb_dev == NULL)
2708 ftdi_error_return(-2, "USB device unavailable");
2710 /* These commands were traced while running MProg */
2711 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2713 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2715 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2718 for (i = 0; i < ftdi->eeprom_size/2; i++)
2720 usb_val = eeprom[i*2];
2721 usb_val += eeprom[(i*2)+1] << 8;
2722 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2723 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2724 NULL, 0, ftdi->usb_write_timeout) != 0)
2725 ftdi_error_return(-1, "unable to write eeprom");
2734 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2736 \param ftdi pointer to ftdi_context
2739 \retval -1: erase failed
2740 \retval -2: USB device unavailable
2742 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2744 if (ftdi == NULL || ftdi->usb_dev == NULL)
2745 ftdi_error_return(-2, "USB device unavailable");
2747 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
2748 ftdi_error_return(-1, "unable to erase eeprom");
2754 Get string representation for last error code
2756 \param ftdi pointer to ftdi_context
2758 \retval Pointer to error string
2760 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2765 return ftdi->error_str;
2768 /* @} end of doxygen libftdi group */