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; \
55 Internal function to close usb device pointer.
56 Sets ftdi->usb_dev to NULL.
59 \param ftdi pointer to ftdi_context
61 \retval zero if all is fine, otherwise error code from usb_close()
63 static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
67 if (ftdi && ftdi->usb_dev)
69 ret = usb_close (ftdi->usb_dev);
77 Initializes a ftdi_context.
79 \param ftdi pointer to ftdi_context
82 \retval -1: couldn't allocate read buffer
84 \remark This should be called before all functions
86 int ftdi_init(struct ftdi_context *ftdi)
91 ftdi->usb_read_timeout = 5000;
92 ftdi->usb_write_timeout = 5000;
94 ftdi->type = TYPE_BM; /* chip type */
96 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
98 ftdi->readbuffer = NULL;
99 ftdi->readbuffer_offset = 0;
100 ftdi->readbuffer_remaining = 0;
101 ftdi->writebuffer_chunksize = 4096;
102 ftdi->max_packet_size = 0;
108 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
110 ftdi->error_str = NULL;
112 #ifdef LIBFTDI_LINUX_ASYNC_MODE
113 ftdi->async_usb_buffer_size=10;
114 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
115 ftdi_error_return(-1, "out of memory for async usb buffer");
117 /* initialize async usb buffer with unused-marker */
118 for (i=0; i < ftdi->async_usb_buffer_size; i++)
119 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
121 ftdi->async_usb_buffer_size=0;
122 ftdi->async_usb_buffer = NULL;
125 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
127 /* All fine. Now allocate the readbuffer */
128 return ftdi_read_data_set_chunksize(ftdi, 4096);
132 Allocate and initialize a new ftdi_context
134 \return a pointer to a new ftdi_context, or NULL on failure
136 struct ftdi_context *ftdi_new(void)
138 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
145 if (ftdi_init(ftdi) != 0)
155 Open selected channels on a chip, otherwise use first channel.
157 \param ftdi pointer to ftdi_context
158 \param interface Interface to use for FT2232C/2232H/4232H chips.
161 \retval -1: unknown interface
162 \retval -2: USB device unavailable
164 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
167 ftdi_error_return(-2, "USB device unavailable");
173 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
177 ftdi->index = INTERFACE_B;
183 ftdi->index = INTERFACE_C;
189 ftdi->index = INTERFACE_D;
194 ftdi_error_return(-1, "Unknown interface");
200 Deinitializes a ftdi_context.
202 \param ftdi pointer to ftdi_context
204 void ftdi_deinit(struct ftdi_context *ftdi)
209 ftdi_usb_close_internal (ftdi);
211 if (ftdi->async_usb_buffer != NULL)
213 free(ftdi->async_usb_buffer);
214 ftdi->async_usb_buffer = NULL;
217 if (ftdi->readbuffer != NULL)
219 free(ftdi->readbuffer);
220 ftdi->readbuffer = NULL;
225 Deinitialize and free an ftdi_context.
227 \param ftdi pointer to ftdi_context
229 void ftdi_free(struct ftdi_context *ftdi)
236 Use an already open libusb device.
238 \param ftdi pointer to ftdi_context
239 \param usb libusb usb_dev_handle to use
241 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
251 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
252 needs to be deallocated by ftdi_list_free() after use.
254 \param ftdi pointer to ftdi_context
255 \param devlist Pointer where to store list of found devices
256 \param vendor Vendor ID to search for
257 \param product Product ID to search for
259 \retval >0: number of devices found
260 \retval -1: usb_find_busses() failed
261 \retval -2: usb_find_devices() failed
262 \retval -3: out of memory
264 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
266 struct ftdi_device_list **curdev;
268 struct usb_device *dev;
272 if (usb_find_busses() < 0)
273 ftdi_error_return(-1, "usb_find_busses() failed");
274 if (usb_find_devices() < 0)
275 ftdi_error_return(-2, "usb_find_devices() failed");
279 for (bus = usb_get_busses(); bus; bus = bus->next)
281 for (dev = bus->devices; dev; dev = dev->next)
283 if (dev->descriptor.idVendor == vendor
284 && dev->descriptor.idProduct == product)
286 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
288 ftdi_error_return(-3, "out of memory");
290 (*curdev)->next = NULL;
291 (*curdev)->dev = dev;
293 curdev = &(*curdev)->next;
303 Frees a usb device list.
305 \param devlist USB device list created by ftdi_usb_find_all()
307 void ftdi_list_free(struct ftdi_device_list **devlist)
309 struct ftdi_device_list *curdev, *next;
311 for (curdev = *devlist; curdev != NULL;)
322 Frees a usb device list.
324 \param devlist USB device list created by ftdi_usb_find_all()
326 void ftdi_list_free2(struct ftdi_device_list *devlist)
328 ftdi_list_free(&devlist);
332 Return device ID strings from the usb device.
334 The parameters manufacturer, description and serial may be NULL
335 or pointer to buffers to store the fetched strings.
337 \note Use this function only in combination with ftdi_usb_find_all()
338 as it closes the internal "usb_dev" after use.
340 \param ftdi pointer to ftdi_context
341 \param dev libusb usb_dev to use
342 \param manufacturer Store manufacturer string here if not NULL
343 \param mnf_len Buffer size of manufacturer string
344 \param description Store product description string here if not NULL
345 \param desc_len Buffer size of product description string
346 \param serial Store serial string here if not NULL
347 \param serial_len Buffer size of serial string
350 \retval -1: wrong arguments
351 \retval -4: unable to open device
352 \retval -7: get product manufacturer failed
353 \retval -8: get product description failed
354 \retval -9: get serial number failed
355 \retval -10: unable to close device
357 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
358 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
360 if ((ftdi==NULL) || (dev==NULL))
363 if (!(ftdi->usb_dev = usb_open(dev)))
364 ftdi_error_return(-4, usb_strerror());
366 if (manufacturer != NULL)
368 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
370 ftdi_usb_close_internal (ftdi);
371 ftdi_error_return(-7, usb_strerror());
375 if (description != NULL)
377 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
379 ftdi_usb_close_internal (ftdi);
380 ftdi_error_return(-8, usb_strerror());
386 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
388 ftdi_usb_close_internal (ftdi);
389 ftdi_error_return(-9, usb_strerror());
393 if (ftdi_usb_close_internal (ftdi) != 0)
394 ftdi_error_return(-10, usb_strerror());
400 * Internal function to determine the maximum packet size.
401 * \param ftdi pointer to ftdi_context
402 * \param dev libusb usb_dev to use
403 * \retval Maximum packet size for this device
405 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
407 unsigned int packet_size;
410 if (ftdi == NULL || dev == NULL)
413 // Determine maximum packet size. Init with default value.
414 // New hi-speed devices from FTDI use a packet size of 512 bytes
415 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
416 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
421 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
423 struct usb_config_descriptor config = dev->config[0];
425 if (ftdi->interface < config.bNumInterfaces)
427 struct usb_interface interface = config.interface[ftdi->interface];
428 if (interface.num_altsetting > 0)
430 struct usb_interface_descriptor descriptor = interface.altsetting[0];
431 if (descriptor.bNumEndpoints > 0)
433 packet_size = descriptor.endpoint[0].wMaxPacketSize;
443 Opens a ftdi device given by an usb_device.
445 \param ftdi pointer to ftdi_context
446 \param dev libusb usb_dev to use
449 \retval -3: unable to config device
450 \retval -4: unable to open device
451 \retval -5: unable to claim device
452 \retval -6: reset failed
453 \retval -7: set baudrate failed
454 \retval -8: ftdi context invalid
456 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
458 int detach_errno = 0;
462 ftdi_error_return(-8, "ftdi context invalid");
464 if (!(ftdi->usb_dev = usb_open(dev)))
465 ftdi_error_return(-4, "usb_open() failed");
467 #ifdef LIBUSB_HAS_GET_DRIVER_NP
468 // Try to detach ftdi_sio kernel module.
469 // Returns ENODATA if driver is not loaded.
471 // The return code is kept in a separate variable and only parsed
472 // if usb_set_configuration() or usb_claim_interface() fails as the
473 // detach operation might be denied and everything still works fine.
474 // Likely scenario is a static ftdi_sio kernel module.
475 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
476 detach_errno = errno;
480 // set configuration (needed especially for windows)
481 // tolerate EBUSY: one device with one configuration, but two interfaces
482 // and libftdi sessions to both interfaces (e.g. FT2232)
484 if (dev->descriptor.bNumConfigurations > 0)
486 // libusb-win32 on Windows 64 can return a null pointer for a valid device
488 config_val = dev->config[0].bConfigurationValue;
490 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
493 ftdi_usb_close_internal (ftdi);
494 if (detach_errno == EPERM)
496 ftdi_error_return(-8, "inappropriate permissions on device!");
500 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
506 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
508 ftdi_usb_close_internal (ftdi);
509 if (detach_errno == EPERM)
511 ftdi_error_return(-8, "inappropriate permissions on device!");
515 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
519 if (ftdi_usb_reset (ftdi) != 0)
521 ftdi_usb_close_internal (ftdi);
522 ftdi_error_return(-6, "ftdi_usb_reset failed");
525 // Try to guess chip type
526 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
527 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
528 && dev->descriptor.iSerialNumber == 0))
529 ftdi->type = TYPE_BM;
530 else if (dev->descriptor.bcdDevice == 0x200)
531 ftdi->type = TYPE_AM;
532 else if (dev->descriptor.bcdDevice == 0x500)
533 ftdi->type = TYPE_2232C;
534 else if (dev->descriptor.bcdDevice == 0x600)
536 else if (dev->descriptor.bcdDevice == 0x700)
537 ftdi->type = TYPE_2232H;
538 else if (dev->descriptor.bcdDevice == 0x800)
539 ftdi->type = TYPE_4232H;
541 // Set default interface on dual/quad type chips
548 ftdi->index = INTERFACE_A;
554 // Determine maximum packet size
555 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
557 if (ftdi_set_baudrate (ftdi, 9600) != 0)
559 ftdi_usb_close_internal (ftdi);
560 ftdi_error_return(-7, "set baudrate failed");
563 ftdi_error_return(0, "all fine");
567 Opens the first device with a given vendor and product ids.
569 \param ftdi pointer to ftdi_context
570 \param vendor Vendor ID
571 \param product Product ID
573 \retval same as ftdi_usb_open_desc()
575 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
577 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
581 Opens the first device with a given, vendor id, product id,
582 description and serial.
584 \param ftdi pointer to ftdi_context
585 \param vendor Vendor ID
586 \param product Product ID
587 \param description Description to search for. Use NULL if not needed.
588 \param serial Serial to search for. Use NULL if not needed.
591 \retval -1: usb_find_busses() failed
592 \retval -2: usb_find_devices() failed
593 \retval -3: usb device not found
594 \retval -4: unable to open device
595 \retval -5: unable to claim device
596 \retval -6: reset failed
597 \retval -7: set baudrate failed
598 \retval -8: get product description failed
599 \retval -9: get serial number failed
600 \retval -10: unable to close device
602 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
603 const char* description, const char* serial)
605 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
609 Opens the index-th device with a given, vendor id, product id,
610 description and serial.
612 \param ftdi pointer to ftdi_context
613 \param vendor Vendor ID
614 \param product Product ID
615 \param description Description to search for. Use NULL if not needed.
616 \param serial Serial to search for. Use NULL if not needed.
617 \param index Number of matching device to open if there are more than one, starts with 0.
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
630 \retval -11: ftdi context invalid
632 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
633 const char* description, const char* serial, unsigned int index)
636 struct usb_device *dev;
641 if (usb_find_busses() < 0)
642 ftdi_error_return(-1, "usb_find_busses() failed");
643 if (usb_find_devices() < 0)
644 ftdi_error_return(-2, "usb_find_devices() failed");
647 ftdi_error_return(-11, "ftdi context invalid");
649 for (bus = usb_get_busses(); bus; bus = bus->next)
651 for (dev = bus->devices; dev; dev = dev->next)
653 if (dev->descriptor.idVendor == vendor
654 && dev->descriptor.idProduct == product)
656 if (!(ftdi->usb_dev = usb_open(dev)))
657 ftdi_error_return(-4, "usb_open() failed");
659 if (description != NULL)
661 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
663 ftdi_usb_close_internal (ftdi);
664 ftdi_error_return(-8, "unable to fetch product description");
666 if (strncmp(string, description, sizeof(string)) != 0)
668 if (ftdi_usb_close_internal (ftdi) != 0)
669 ftdi_error_return(-10, "unable to close device");
675 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
677 ftdi_usb_close_internal (ftdi);
678 ftdi_error_return(-9, "unable to fetch serial number");
680 if (strncmp(string, serial, sizeof(string)) != 0)
682 if (ftdi_usb_close_internal (ftdi) != 0)
683 ftdi_error_return(-10, "unable to close device");
688 if (ftdi_usb_close_internal (ftdi) != 0)
689 ftdi_error_return(-10, "unable to close device");
697 return ftdi_usb_open_dev(ftdi, dev);
703 ftdi_error_return(-3, "device not found");
707 Opens the ftdi-device described by a description-string.
708 Intended to be used for parsing a device-description given as commandline argument.
710 \param ftdi pointer to ftdi_context
711 \param description NULL-terminated description-string, using this format:
712 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
713 \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")
714 \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
715 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
717 \note The description format may be extended in later versions.
720 \retval -1: usb_find_busses() failed
721 \retval -2: usb_find_devices() failed
722 \retval -3: usb device not found
723 \retval -4: unable to open device
724 \retval -5: unable to claim device
725 \retval -6: reset failed
726 \retval -7: set baudrate failed
727 \retval -8: get product description failed
728 \retval -9: get serial number failed
729 \retval -10: unable to close device
730 \retval -11: illegal description format
731 \retval -12: ftdi context invalid
733 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
736 ftdi_error_return(-12, "ftdi context invalid");
738 if (description[0] == 0 || description[1] != ':')
739 ftdi_error_return(-11, "illegal description format");
741 if (description[0] == 'd')
744 struct usb_device *dev;
748 if (usb_find_busses() < 0)
749 ftdi_error_return(-1, "usb_find_busses() failed");
750 if (usb_find_devices() < 0)
751 ftdi_error_return(-2, "usb_find_devices() failed");
753 for (bus = usb_get_busses(); bus; bus = bus->next)
755 for (dev = bus->devices; dev; dev = dev->next)
757 /* XXX: This doesn't handle symlinks/odd paths/etc... */
758 const char *desc = description + 2;
759 size_t len = strlen(bus->dirname);
760 if (strncmp(desc, bus->dirname, len))
766 if (strcmp(desc, dev->filename))
768 return ftdi_usb_open_dev(ftdi, dev);
773 ftdi_error_return(-3, "device not found");
775 else if (description[0] == 'i' || description[0] == 's')
778 unsigned int product;
779 unsigned int index=0;
780 const char *serial=NULL;
781 const char *startp, *endp;
784 startp=description+2;
785 vendor=strtoul((char*)startp,(char**)&endp,0);
786 if (*endp != ':' || endp == startp || errno != 0)
787 ftdi_error_return(-11, "illegal description format");
790 product=strtoul((char*)startp,(char**)&endp,0);
791 if (endp == startp || errno != 0)
792 ftdi_error_return(-11, "illegal description format");
794 if (description[0] == 'i' && *endp != 0)
796 /* optional index field in i-mode */
798 ftdi_error_return(-11, "illegal description format");
801 index=strtoul((char*)startp,(char**)&endp,0);
802 if (*endp != 0 || endp == startp || errno != 0)
803 ftdi_error_return(-11, "illegal description format");
805 if (description[0] == 's')
808 ftdi_error_return(-11, "illegal description format");
810 /* rest of the description is the serial */
814 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
818 ftdi_error_return(-11, "illegal description format");
823 Resets the ftdi device.
825 \param ftdi pointer to ftdi_context
828 \retval -1: FTDI reset failed
829 \retval -2: USB device unavailable
831 int ftdi_usb_reset(struct ftdi_context *ftdi)
833 if (ftdi == NULL || ftdi->usb_dev == NULL)
834 ftdi_error_return(-2, "USB device unavailable");
836 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
837 SIO_RESET_REQUEST, SIO_RESET_SIO,
838 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
839 ftdi_error_return(-1,"FTDI reset failed");
841 // Invalidate data in the readbuffer
842 ftdi->readbuffer_offset = 0;
843 ftdi->readbuffer_remaining = 0;
849 Clears the read buffer on the chip and the internal read buffer.
851 \param ftdi pointer to ftdi_context
854 \retval -1: read buffer purge failed
855 \retval -2: USB device unavailable
857 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
859 if (ftdi == NULL || ftdi->usb_dev == NULL)
860 ftdi_error_return(-2, "USB device unavailable");
862 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
863 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
864 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
865 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
867 // Invalidate data in the readbuffer
868 ftdi->readbuffer_offset = 0;
869 ftdi->readbuffer_remaining = 0;
875 Clears the write buffer on the chip.
877 \param ftdi pointer to ftdi_context
880 \retval -1: write buffer purge failed
881 \retval -2: USB device unavailable
883 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
885 if (ftdi == NULL || ftdi->usb_dev == NULL)
886 ftdi_error_return(-2, "USB device unavailable");
888 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
889 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
890 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
891 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
897 Clears the buffers on the chip and the internal read buffer.
899 \param ftdi pointer to ftdi_context
902 \retval -1: read buffer purge failed
903 \retval -2: write buffer purge failed
904 \retval -3: USB device unavailable
906 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
910 if (ftdi == NULL || ftdi->usb_dev == NULL)
911 ftdi_error_return(-3, "USB device unavailable");
913 result = ftdi_usb_purge_rx_buffer(ftdi);
917 result = ftdi_usb_purge_tx_buffer(ftdi);
927 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
929 \param ftdi pointer to ftdi_context
932 \retval -1: usb_release failed
933 \retval -2: usb_close failed
934 \retval -3: ftdi context invalid
936 int ftdi_usb_close(struct ftdi_context *ftdi)
941 ftdi_error_return(-3, "ftdi context invalid");
943 #ifdef LIBFTDI_LINUX_ASYNC_MODE
944 /* try to release some kernel resources */
945 ftdi_async_complete(ftdi,1);
948 if (ftdi->usb_dev != NULL)
949 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
952 if (ftdi_usb_close_internal (ftdi) != 0)
959 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
960 Function is only used internally
963 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
964 unsigned short *value, unsigned short *index)
966 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
967 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
968 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
969 int divisor, best_divisor, best_baud, best_baud_diff;
970 unsigned long encoded_divisor;
979 divisor = 24000000 / baudrate;
981 if (ftdi->type == TYPE_AM)
983 // Round down to supported fraction (AM only)
984 divisor -= am_adjust_dn[divisor & 7];
987 // Try this divisor and the one above it (because division rounds down)
991 for (i = 0; i < 2; i++)
993 int try_divisor = divisor + i;
997 // Round up to supported divisor value
998 if (try_divisor <= 8)
1000 // Round up to minimum supported divisor
1003 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1005 // BM doesn't support divisors 9 through 11 inclusive
1008 else if (divisor < 16)
1010 // AM doesn't support divisors 9 through 15 inclusive
1015 if (ftdi->type == TYPE_AM)
1017 // Round up to supported fraction (AM only)
1018 try_divisor += am_adjust_up[try_divisor & 7];
1019 if (try_divisor > 0x1FFF8)
1021 // Round down to maximum supported divisor value (for AM)
1022 try_divisor = 0x1FFF8;
1027 if (try_divisor > 0x1FFFF)
1029 // Round down to maximum supported divisor value (for BM)
1030 try_divisor = 0x1FFFF;
1034 // Get estimated baud rate (to nearest integer)
1035 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1036 // Get absolute difference from requested baud rate
1037 if (baud_estimate < baudrate)
1039 baud_diff = baudrate - baud_estimate;
1043 baud_diff = baud_estimate - baudrate;
1045 if (i == 0 || baud_diff < best_baud_diff)
1047 // Closest to requested baud rate so far
1048 best_divisor = try_divisor;
1049 best_baud = baud_estimate;
1050 best_baud_diff = baud_diff;
1053 // Spot on! No point trying
1058 // Encode the best divisor value
1059 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1060 // Deal with special cases for encoded value
1061 if (encoded_divisor == 1)
1063 encoded_divisor = 0; // 3000000 baud
1065 else if (encoded_divisor == 0x4001)
1067 encoded_divisor = 1; // 2000000 baud (BM only)
1069 // Split into "value" and "index" values
1070 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1071 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1073 *index = (unsigned short)(encoded_divisor >> 8);
1075 *index |= ftdi->index;
1078 *index = (unsigned short)(encoded_divisor >> 16);
1080 // Return the nearest baud rate
1085 Sets the chip baud rate
1087 \param ftdi pointer to ftdi_context
1088 \param baudrate baud rate to set
1091 \retval -1: invalid baudrate
1092 \retval -2: setting baudrate failed
1093 \retval -3: USB device unavailable
1095 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1097 unsigned short value, index;
1098 int actual_baudrate;
1100 if (ftdi == NULL || ftdi->usb_dev == NULL)
1101 ftdi_error_return(-3, "USB device unavailable");
1103 if (ftdi->bitbang_enabled)
1105 baudrate = baudrate*4;
1108 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1109 if (actual_baudrate <= 0)
1110 ftdi_error_return (-1, "Silly baudrate <= 0.");
1112 // Check within tolerance (about 5%)
1113 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1114 || ((actual_baudrate < baudrate)
1115 ? (actual_baudrate * 21 < baudrate * 20)
1116 : (baudrate * 21 < actual_baudrate * 20)))
1117 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1119 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1120 SIO_SET_BAUDRATE_REQUEST, value,
1121 index, NULL, 0, ftdi->usb_write_timeout) != 0)
1122 ftdi_error_return (-2, "Setting new baudrate failed");
1124 ftdi->baudrate = baudrate;
1129 Set (RS232) line characteristics.
1130 The break type can only be set via ftdi_set_line_property2()
1131 and defaults to "off".
1133 \param ftdi pointer to ftdi_context
1134 \param bits Number of bits
1135 \param sbit Number of stop bits
1136 \param parity Parity mode
1139 \retval -1: Setting line property failed
1141 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1142 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1144 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1148 Set (RS232) line characteristics
1150 \param ftdi pointer to ftdi_context
1151 \param bits Number of bits
1152 \param sbit Number of stop bits
1153 \param parity Parity mode
1154 \param break_type Break type
1157 \retval -1: Setting line property failed
1158 \retval -2: USB device unavailable
1160 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1161 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1162 enum ftdi_break_type break_type)
1164 unsigned short value = bits;
1166 if (ftdi == NULL || ftdi->usb_dev == NULL)
1167 ftdi_error_return(-2, "USB device unavailable");
1172 value |= (0x00 << 8);
1175 value |= (0x01 << 8);
1178 value |= (0x02 << 8);
1181 value |= (0x03 << 8);
1184 value |= (0x04 << 8);
1191 value |= (0x00 << 11);
1194 value |= (0x01 << 11);
1197 value |= (0x02 << 11);
1204 value |= (0x00 << 14);
1207 value |= (0x01 << 14);
1211 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1212 SIO_SET_DATA_REQUEST, value,
1213 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1214 ftdi_error_return (-1, "Setting new line property failed");
1220 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1222 \param ftdi pointer to ftdi_context
1223 \param buf Buffer with the data
1224 \param size Size of the buffer
1226 \retval -666: USB device unavailable
1227 \retval <0: error code from usb_bulk_write()
1228 \retval >0: number of bytes written
1230 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1234 int total_written = 0;
1236 if (ftdi == NULL || ftdi->usb_dev == NULL)
1237 ftdi_error_return(-666, "USB device unavailable");
1239 while (offset < size)
1241 int write_size = ftdi->writebuffer_chunksize;
1243 if (offset+write_size > size)
1244 write_size = size-offset;
1246 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
1248 ftdi_error_return(ret, "usb bulk write failed");
1250 total_written += ret;
1251 offset += write_size;
1254 return total_written;
1257 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1258 #ifdef USB_CLASS_PTP
1259 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1261 /* this is strongly dependent on libusb using the same struct layout. If libusb
1262 changes in some later version this may break horribly (this is for libusb 0.1.12) */
1263 struct usb_dev_handle
1266 // some other stuff coming here we don't need
1270 Check for pending async urbs
1273 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
1275 struct usbdevfs_urb *urb;
1279 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1281 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1282 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1290 Wait until one or more async URBs are completed by the kernel and mark their
1291 positions in the async-buffer as unused
1293 \param ftdi pointer to ftdi_context
1294 \param wait_for_more if != 0 wait for more than one write to complete
1295 \param timeout_msec max milliseconds to wait
1299 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
1302 struct usbdevfs_urb *urb=NULL;
1308 FD_SET(ftdi->usb_dev->fd, &writefds);
1310 /* init timeout only once, select writes time left after call */
1311 tv.tv_sec = timeout_msec / 1000;
1312 tv.tv_usec = (timeout_msec % 1000) * 1000;
1316 while (_usb_get_async_urbs_pending(ftdi)
1317 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1320 if (keep_going && !wait_for_more)
1322 /* don't wait if repeating only for keep_going */
1327 /* wait for timeout msec or something written ready */
1328 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1331 if (ret == 0 && urb != NULL)
1333 /* got a free urb, mark it */
1334 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
1336 /* try to get more urbs that are ready now, but don't wait anymore */
1342 /* no more urbs waiting */
1350 Wait until one or more async URBs are completed by the kernel and mark their
1351 positions in the async-buffer as unused.
1353 \param ftdi pointer to ftdi_context
1354 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1356 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1358 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
1362 Stupid libusb does not offer async writes nor does it allow
1363 access to its fd - so we need some hacks here.
1366 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
1368 struct usbdevfs_urb *urb;
1369 int bytesdone = 0, requested;
1370 int ret, cleanup_count;
1375 /* find a free urb buffer we can use */
1377 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1379 if (i==ftdi->async_usb_buffer_size)
1381 /* wait until some buffers are free */
1382 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1385 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1387 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1388 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1389 break; /* found a free urb position */
1394 /* no free urb position found */
1398 requested = size - bytesdone;
1399 if (requested > 4096)
1402 memset(urb,0,sizeof(urb));
1404 urb->type = USBDEVFS_URB_TYPE_BULK;
1407 urb->buffer = bytes + bytesdone;
1408 urb->buffer_length = requested;
1410 urb->actual_length = 0;
1411 urb->number_of_packets = 0;
1412 urb->usercontext = 0;
1416 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1418 while (ret < 0 && errno == EINTR);
1420 return ret; /* the caller can read errno to get more info */
1422 bytesdone += requested;
1424 while (bytesdone < size);
1429 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1430 Does not wait for completion of the transfer nor does it make sure that
1431 the transfer was successful.
1433 This function could be extended to use signals and callbacks to inform the
1434 caller of completion or error - but this is not done yet, volunteers welcome.
1436 Works around libusb and directly accesses functions only available on Linux.
1437 Only available if compiled with --with-async-mode.
1439 \param ftdi pointer to ftdi_context
1440 \param buf Buffer with the data
1441 \param size Size of the buffer
1443 \retval -666: USB device unavailable
1444 \retval <0: error code from usb_bulk_write()
1445 \retval >0: number of bytes written
1447 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1451 int total_written = 0;
1453 if (ftdi == NULL || ftdi->usb_dev == NULL)
1454 ftdi_error_return(-666, "USB device unavailable");
1456 while (offset < size)
1458 int write_size = ftdi->writebuffer_chunksize;
1460 if (offset+write_size > size)
1461 write_size = size-offset;
1463 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1465 ftdi_error_return(ret, "usb bulk write async failed");
1467 total_written += ret;
1468 offset += write_size;
1471 return total_written;
1473 #endif // LIBFTDI_LINUX_ASYNC_MODE
1476 Configure write buffer chunk size.
1479 \param ftdi pointer to ftdi_context
1480 \param chunksize Chunk size
1483 \retval -1: ftdi context invalid
1485 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1488 ftdi_error_return(-1, "ftdi context invalid");
1490 ftdi->writebuffer_chunksize = chunksize;
1495 Get write buffer chunk size.
1497 \param ftdi pointer to ftdi_context
1498 \param chunksize Pointer to store chunk size in
1501 \retval -1: ftdi context invalid
1503 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1506 ftdi_error_return(-1, "ftdi context invalid");
1508 *chunksize = ftdi->writebuffer_chunksize;
1513 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1515 Automatically strips the two modem status bytes transfered during every read.
1517 \param ftdi pointer to ftdi_context
1518 \param buf Buffer to store data in
1519 \param size Size of the buffer
1521 \retval -666: USB device unavailable
1522 \retval <0: error code from usb_bulk_read()
1523 \retval 0: no data was available
1524 \retval >0: number of bytes read
1527 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1529 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1532 if (ftdi == NULL || ftdi->usb_dev == NULL)
1533 ftdi_error_return(-666, "USB device unavailable");
1535 packet_size = ftdi->max_packet_size;
1536 // Packet size sanity check (avoid division by zero)
1537 if (packet_size == 0)
1538 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1540 // everything we want is still in the readbuffer?
1541 if (size <= ftdi->readbuffer_remaining)
1543 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1546 ftdi->readbuffer_remaining -= size;
1547 ftdi->readbuffer_offset += size;
1549 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1553 // something still in the readbuffer, but not enough to satisfy 'size'?
1554 if (ftdi->readbuffer_remaining != 0)
1556 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1559 offset += ftdi->readbuffer_remaining;
1561 // do the actual USB read
1562 while (offset < size && ret > 0)
1564 ftdi->readbuffer_remaining = 0;
1565 ftdi->readbuffer_offset = 0;
1566 /* returns how much received */
1567 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1569 ftdi_error_return(ret, "usb bulk read failed");
1573 // skip FTDI status bytes.
1574 // Maybe stored in the future to enable modem use
1575 num_of_chunks = ret / packet_size;
1576 chunk_remains = ret % packet_size;
1577 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1579 ftdi->readbuffer_offset += 2;
1582 if (ret > packet_size - 2)
1584 for (i = 1; i < num_of_chunks; i++)
1585 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1586 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1588 if (chunk_remains > 2)
1590 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1591 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1593 ret -= 2*num_of_chunks;
1596 ret -= 2*(num_of_chunks-1)+chunk_remains;
1601 // no more data to read?
1606 // data still fits in buf?
1607 if (offset+ret <= size)
1609 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1610 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1613 /* Did we read exactly the right amount of bytes? */
1615 //printf("read_data exact rem %d offset %d\n",
1616 //ftdi->readbuffer_remaining, offset);
1621 // only copy part of the data or size <= readbuffer_chunksize
1622 int part_size = size-offset;
1623 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1625 ftdi->readbuffer_offset += part_size;
1626 ftdi->readbuffer_remaining = ret-part_size;
1627 offset += part_size;
1629 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1630 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1641 Configure read buffer chunk size.
1644 Automatically reallocates the buffer.
1646 \param ftdi pointer to ftdi_context
1647 \param chunksize Chunk size
1650 \retval -1: ftdi context invalid
1652 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1654 unsigned char *new_buf;
1657 ftdi_error_return(-1, "ftdi context invalid");
1659 // Invalidate all remaining data
1660 ftdi->readbuffer_offset = 0;
1661 ftdi->readbuffer_remaining = 0;
1663 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1664 ftdi_error_return(-1, "out of memory for readbuffer");
1666 ftdi->readbuffer = new_buf;
1667 ftdi->readbuffer_chunksize = chunksize;
1673 Get read buffer chunk size.
1675 \param ftdi pointer to ftdi_context
1676 \param chunksize Pointer to store chunk size in
1679 \retval -1: FTDI context invalid
1681 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1684 ftdi_error_return(-1, "FTDI context invalid");
1686 *chunksize = ftdi->readbuffer_chunksize;
1692 Enable bitbang mode.
1694 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1696 \param ftdi pointer to ftdi_context
1697 \param bitmask Bitmask to configure lines.
1698 HIGH/ON value configures a line as output.
1701 \retval -1: can't enable bitbang mode
1702 \retval -2: USB device unavailable
1704 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1706 unsigned short usb_val;
1708 if (ftdi == NULL || ftdi->usb_dev == NULL)
1709 ftdi_error_return(-2, "USB device unavailable");
1711 usb_val = bitmask; // low byte: bitmask
1712 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1713 usb_val |= (ftdi->bitbang_mode << 8);
1715 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1716 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1717 NULL, 0, ftdi->usb_write_timeout) != 0)
1718 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1720 ftdi->bitbang_enabled = 1;
1725 Disable bitbang mode.
1727 \param ftdi pointer to ftdi_context
1730 \retval -1: can't disable bitbang mode
1731 \retval -2: USB device unavailable
1733 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1735 if (ftdi == NULL || ftdi->usb_dev == NULL)
1736 ftdi_error_return(-2, "USB device unavailable");
1738 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)
1739 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1741 ftdi->bitbang_enabled = 0;
1746 Enable/disable bitbang modes.
1748 \param ftdi pointer to ftdi_context
1749 \param bitmask Bitmask to configure lines.
1750 HIGH/ON value configures a line as output.
1751 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1754 \retval -1: can't enable bitbang mode
1755 \retval -2: USB device unavailable
1757 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1759 unsigned short usb_val;
1761 if (ftdi == NULL || ftdi->usb_dev == NULL)
1762 ftdi_error_return(-2, "USB device unavailable");
1764 usb_val = bitmask; // low byte: bitmask
1765 usb_val |= (mode << 8);
1766 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)
1767 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1769 ftdi->bitbang_mode = mode;
1770 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1775 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1777 \param ftdi pointer to ftdi_context
1778 \param pins Pointer to store pins into
1781 \retval -1: read pins failed
1782 \retval -2: USB device unavailable
1784 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1786 if (ftdi == NULL || ftdi->usb_dev == NULL)
1787 ftdi_error_return(-2, "USB device unavailable");
1789 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)
1790 ftdi_error_return(-1, "read pins failed");
1798 The FTDI chip keeps data in the internal buffer for a specific
1799 amount of time if the buffer is not full yet to decrease
1800 load on the usb bus.
1802 \param ftdi pointer to ftdi_context
1803 \param latency Value between 1 and 255
1806 \retval -1: latency out of range
1807 \retval -2: unable to set latency timer
1808 \retval -3: USB device unavailable
1810 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1812 unsigned short usb_val;
1815 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1817 if (ftdi == NULL || ftdi->usb_dev == NULL)
1818 ftdi_error_return(-3, "USB device unavailable");
1821 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)
1822 ftdi_error_return(-2, "unable to set latency timer");
1830 \param ftdi pointer to ftdi_context
1831 \param latency Pointer to store latency value in
1834 \retval -1: unable to get latency timer
1835 \retval -2: USB device unavailable
1837 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1839 unsigned short usb_val;
1841 if (ftdi == NULL || ftdi->usb_dev == NULL)
1842 ftdi_error_return(-2, "USB device unavailable");
1844 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)
1845 ftdi_error_return(-1, "reading latency timer failed");
1847 *latency = (unsigned char)usb_val;
1852 Poll modem status information
1854 This function allows the retrieve the two status bytes of the device.
1855 The device sends these bytes also as a header for each read access
1856 where they are discarded by ftdi_read_data(). The chip generates
1857 the two stripped status bytes in the absence of data every 40 ms.
1859 Layout of the first byte:
1860 - B0..B3 - must be 0
1861 - B4 Clear to send (CTS)
1864 - B5 Data set ready (DTS)
1867 - B6 Ring indicator (RI)
1870 - B7 Receive line signal detect (RLSD)
1874 Layout of the second byte:
1875 - B0 Data ready (DR)
1876 - B1 Overrun error (OE)
1877 - B2 Parity error (PE)
1878 - B3 Framing error (FE)
1879 - B4 Break interrupt (BI)
1880 - B5 Transmitter holding register (THRE)
1881 - B6 Transmitter empty (TEMT)
1882 - B7 Error in RCVR FIFO
1884 \param ftdi pointer to ftdi_context
1885 \param status Pointer to store status information in. Must be two bytes.
1888 \retval -1: unable to retrieve status information
1889 \retval -2: USB device unavailable
1891 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1895 if (ftdi == NULL || ftdi->usb_dev == NULL)
1896 ftdi_error_return(-2, "USB device unavailable");
1898 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)
1899 ftdi_error_return(-1, "getting modem status failed");
1901 *status = (usb_val[1] << 8) | usb_val[0];
1907 Set flowcontrol for ftdi chip
1909 \param ftdi pointer to ftdi_context
1910 \param flowctrl flow control to use. should be
1911 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1914 \retval -1: set flow control failed
1915 \retval -2: USB device unavailable
1917 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1919 if (ftdi == NULL || ftdi->usb_dev == NULL)
1920 ftdi_error_return(-2, "USB device unavailable");
1922 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1923 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1924 NULL, 0, ftdi->usb_write_timeout) != 0)
1925 ftdi_error_return(-1, "set flow control failed");
1933 \param ftdi pointer to ftdi_context
1934 \param state state to set line to (1 or 0)
1937 \retval -1: set dtr failed
1938 \retval -2: USB device unavailable
1940 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1942 unsigned short usb_val;
1944 if (ftdi == NULL || ftdi->usb_dev == NULL)
1945 ftdi_error_return(-2, "USB device unavailable");
1948 usb_val = SIO_SET_DTR_HIGH;
1950 usb_val = SIO_SET_DTR_LOW;
1952 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1953 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1954 NULL, 0, ftdi->usb_write_timeout) != 0)
1955 ftdi_error_return(-1, "set dtr failed");
1963 \param ftdi pointer to ftdi_context
1964 \param state state to set line to (1 or 0)
1967 \retval -1: set rts failed
1968 \retval -2: USB device unavailable
1970 int ftdi_setrts(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_RTS_HIGH;
1980 usb_val = SIO_SET_RTS_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 of rts failed");
1991 Set dtr and rts line in one pass
1993 \param ftdi pointer to ftdi_context
1994 \param dtr DTR state to set line to (1 or 0)
1995 \param rts RTS state to set line to (1 or 0)
1998 \retval -1: set dtr/rts failed
1999 \retval -2: USB device unavailable
2001 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2003 unsigned short usb_val;
2005 if (ftdi == NULL || ftdi->usb_dev == NULL)
2006 ftdi_error_return(-2, "USB device unavailable");
2009 usb_val = SIO_SET_DTR_HIGH;
2011 usb_val = SIO_SET_DTR_LOW;
2014 usb_val |= SIO_SET_RTS_HIGH;
2016 usb_val |= SIO_SET_RTS_LOW;
2018 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2019 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2020 NULL, 0, ftdi->usb_write_timeout) != 0)
2021 ftdi_error_return(-1, "set of rts/dtr failed");
2027 Set the special event character
2029 \param ftdi pointer to ftdi_context
2030 \param eventch Event character
2031 \param enable 0 to disable the event character, non-zero otherwise
2034 \retval -1: unable to set event character
2035 \retval -2: USB device unavailable
2037 int ftdi_set_event_char(struct ftdi_context *ftdi,
2038 unsigned char eventch, unsigned char enable)
2040 unsigned short usb_val;
2042 if (ftdi == NULL || ftdi->usb_dev == NULL)
2043 ftdi_error_return(-2, "USB device unavailable");
2049 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)
2050 ftdi_error_return(-1, "setting event character failed");
2058 \param ftdi pointer to ftdi_context
2059 \param errorch Error character
2060 \param enable 0 to disable the error character, non-zero otherwise
2063 \retval -1: unable to set error character
2064 \retval -2: USB device unavailable
2066 int ftdi_set_error_char(struct ftdi_context *ftdi,
2067 unsigned char errorch, unsigned char enable)
2069 unsigned short usb_val;
2071 if (ftdi == NULL || ftdi->usb_dev == NULL)
2072 ftdi_error_return(-2, "USB device unavailable");
2078 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)
2079 ftdi_error_return(-1, "setting error character failed");
2087 \param ftdi pointer to ftdi_context
2088 \param eeprom Pointer to ftdi_eeprom
2092 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2097 ftdi->eeprom_size=size;
2102 Init eeprom with default values.
2104 \param eeprom Pointer to ftdi_eeprom
2106 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2111 eeprom->vendor_id = 0x0403;
2112 eeprom->product_id = 0x6001;
2114 eeprom->self_powered = 1;
2115 eeprom->remote_wakeup = 1;
2116 eeprom->BM_type_chip = 1;
2118 eeprom->in_is_isochronous = 0;
2119 eeprom->out_is_isochronous = 0;
2120 eeprom->suspend_pull_downs = 0;
2122 eeprom->use_serial = 0;
2123 eeprom->change_usb_version = 0;
2124 eeprom->usb_version = 0x0200;
2125 eeprom->max_power = 0;
2127 eeprom->manufacturer = NULL;
2128 eeprom->product = NULL;
2129 eeprom->serial = NULL;
2131 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2135 Frees allocated memory in eeprom.
2137 \param eeprom Pointer to ftdi_eeprom
2139 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2141 if (eeprom->manufacturer != 0) {
2142 free(eeprom->manufacturer);
2143 eeprom->manufacturer = 0;
2145 if (eeprom->product != 0) {
2146 free(eeprom->product);
2147 eeprom->product = 0;
2149 if (eeprom->serial != 0) {
2150 free(eeprom->serial);
2156 Build binary output from ftdi_eeprom structure.
2157 Output is suitable for ftdi_write_eeprom().
2159 \param eeprom Pointer to ftdi_eeprom
2160 \param output Buffer of 128 bytes to store eeprom image to
2162 \retval >0: used eeprom size
2163 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2164 \retval -2: Invalid eeprom pointer
2166 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2169 unsigned short checksum, value;
2170 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2176 if (eeprom->manufacturer != NULL)
2177 manufacturer_size = strlen(eeprom->manufacturer);
2178 if (eeprom->product != NULL)
2179 product_size = strlen(eeprom->product);
2180 if (eeprom->serial != NULL)
2181 serial_size = strlen(eeprom->serial);
2183 size_check = eeprom->size;
2184 size_check -= 28; // 28 are always in use (fixed)
2186 // Top half of a 256byte eeprom is used just for strings and checksum
2187 // it seems that the FTDI chip will not read these strings from the lower half
2188 // Each string starts with two bytes; offset and type (0x03 for string)
2189 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2190 if (eeprom->size>=256)size_check = 120;
2191 size_check -= manufacturer_size*2;
2192 size_check -= product_size*2;
2193 size_check -= serial_size*2;
2195 // eeprom size exceeded?
2200 memset (output, 0, eeprom->size);
2202 // Addr 00: Stay 00 00
2203 // Addr 02: Vendor ID
2204 output[0x02] = eeprom->vendor_id;
2205 output[0x03] = eeprom->vendor_id >> 8;
2207 // Addr 04: Product ID
2208 output[0x04] = eeprom->product_id;
2209 output[0x05] = eeprom->product_id >> 8;
2211 // Addr 06: Device release number (0400h for BM features)
2212 output[0x06] = 0x00;
2214 if (eeprom->BM_type_chip == 1)
2215 output[0x07] = 0x04;
2217 output[0x07] = 0x02;
2219 // Addr 08: Config descriptor
2221 // Bit 6: 1 if this device is self powered, 0 if bus powered
2222 // Bit 5: 1 if this device uses remote wakeup
2223 // Bit 4: 1 if this device is battery powered
2225 if (eeprom->self_powered == 1)
2227 if (eeprom->remote_wakeup == 1)
2231 // Addr 09: Max power consumption: max power = value * 2 mA
2232 output[0x09] = eeprom->max_power;
2234 // Addr 0A: Chip configuration
2235 // Bit 7: 0 - reserved
2236 // Bit 6: 0 - reserved
2237 // Bit 5: 0 - reserved
2238 // Bit 4: 1 - Change USB version
2239 // Bit 3: 1 - Use the serial number string
2240 // Bit 2: 1 - Enable suspend pull downs for lower power
2241 // Bit 1: 1 - Out EndPoint is Isochronous
2242 // Bit 0: 1 - In EndPoint is Isochronous
2245 if (eeprom->in_is_isochronous == 1)
2247 if (eeprom->out_is_isochronous == 1)
2249 if (eeprom->suspend_pull_downs == 1)
2251 if (eeprom->use_serial == 1)
2253 if (eeprom->change_usb_version == 1)
2257 // Addr 0B: reserved
2258 output[0x0B] = 0x00;
2260 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2261 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2262 if (eeprom->change_usb_version == 1)
2264 output[0x0C] = eeprom->usb_version;
2265 output[0x0D] = eeprom->usb_version >> 8;
2269 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2270 // Addr 0F: Length of manufacturer string
2271 output[0x0F] = manufacturer_size*2 + 2;
2273 // Addr 10: Offset of the product string + 0x80, calculated later
2274 // Addr 11: Length of product string
2275 output[0x11] = product_size*2 + 2;
2277 // Addr 12: Offset of the serial string + 0x80, calculated later
2278 // Addr 13: Length of serial string
2279 output[0x13] = serial_size*2 + 2;
2283 if (eeprom->size>=256) i = 0x80;
2286 // Output manufacturer
2287 output[0x0E] = i | 0x80; // calculate offset
2288 output[i++] = manufacturer_size*2 + 2;
2289 output[i++] = 0x03; // type: string
2290 for (j = 0; j < manufacturer_size; j++)
2292 output[i] = eeprom->manufacturer[j], i++;
2293 output[i] = 0x00, i++;
2296 // Output product name
2297 output[0x10] = i | 0x80; // calculate offset
2298 output[i] = product_size*2 + 2, i++;
2299 output[i] = 0x03, i++;
2300 for (j = 0; j < product_size; j++)
2302 output[i] = eeprom->product[j], i++;
2303 output[i] = 0x00, i++;
2307 output[0x12] = i | 0x80; // calculate offset
2308 output[i] = serial_size*2 + 2, i++;
2309 output[i] = 0x03, i++;
2310 for (j = 0; j < serial_size; j++)
2312 output[i] = eeprom->serial[j], i++;
2313 output[i] = 0x00, i++;
2316 // calculate checksum
2319 for (i = 0; i < eeprom->size/2-1; i++)
2321 value = output[i*2];
2322 value += output[(i*2)+1] << 8;
2324 checksum = value^checksum;
2325 checksum = (checksum << 1) | (checksum >> 15);
2328 output[eeprom->size-2] = checksum;
2329 output[eeprom->size-1] = checksum >> 8;
2335 Decode binary EEPROM image into an ftdi_eeprom structure.
2337 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2338 \param buf Buffer of \a size bytes of raw eeprom data
2339 \param size size size of eeprom data in bytes
2342 \retval -1: something went wrong
2344 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2345 FIXME: Strings are malloc'ed here and should be freed somewhere
2347 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2350 unsigned short checksum, eeprom_checksum, value;
2351 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2353 int eeprom_size = 128;
2358 size_check = eeprom->size;
2359 size_check -= 28; // 28 are always in use (fixed)
2361 // Top half of a 256byte eeprom is used just for strings and checksum
2362 // it seems that the FTDI chip will not read these strings from the lower half
2363 // Each string starts with two bytes; offset and type (0x03 for string)
2364 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2365 if (eeprom->size>=256)size_check = 120;
2366 size_check -= manufacturer_size*2;
2367 size_check -= product_size*2;
2368 size_check -= serial_size*2;
2370 // eeprom size exceeded?
2375 // empty eeprom struct
2376 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2378 // Addr 00: Stay 00 00
2380 // Addr 02: Vendor ID
2381 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2383 // Addr 04: Product ID
2384 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2386 value = buf[0x06] + (buf[0x07]<<8);
2390 eeprom->BM_type_chip = 1;
2393 eeprom->BM_type_chip = 0;
2395 default: // Unknown device
2396 eeprom->BM_type_chip = 0;
2400 // Addr 08: Config descriptor
2402 // Bit 6: 1 if this device is self powered, 0 if bus powered
2403 // Bit 5: 1 if this device uses remote wakeup
2404 // Bit 4: 1 if this device is battery powered
2406 if (j&0x40) eeprom->self_powered = 1;
2407 if (j&0x20) eeprom->remote_wakeup = 1;
2409 // Addr 09: Max power consumption: max power = value * 2 mA
2410 eeprom->max_power = buf[0x09];
2412 // Addr 0A: Chip configuration
2413 // Bit 7: 0 - reserved
2414 // Bit 6: 0 - reserved
2415 // Bit 5: 0 - reserved
2416 // Bit 4: 1 - Change USB version
2417 // Bit 3: 1 - Use the serial number string
2418 // Bit 2: 1 - Enable suspend pull downs for lower power
2419 // Bit 1: 1 - Out EndPoint is Isochronous
2420 // Bit 0: 1 - In EndPoint is Isochronous
2423 if (j&0x01) eeprom->in_is_isochronous = 1;
2424 if (j&0x02) eeprom->out_is_isochronous = 1;
2425 if (j&0x04) eeprom->suspend_pull_downs = 1;
2426 if (j&0x08) eeprom->use_serial = 1;
2427 if (j&0x10) eeprom->change_usb_version = 1;
2429 // Addr 0B: reserved
2431 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2432 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2433 if (eeprom->change_usb_version == 1)
2435 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2438 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2439 // Addr 0F: Length of manufacturer string
2440 manufacturer_size = buf[0x0F]/2;
2441 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2442 else eeprom->manufacturer = NULL;
2444 // Addr 10: Offset of the product string + 0x80, calculated later
2445 // Addr 11: Length of product string
2446 product_size = buf[0x11]/2;
2447 if (product_size > 0) eeprom->product = malloc(product_size);
2448 else eeprom->product = NULL;
2450 // Addr 12: Offset of the serial string + 0x80, calculated later
2451 // Addr 13: Length of serial string
2452 serial_size = buf[0x13]/2;
2453 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2454 else eeprom->serial = NULL;
2456 // Decode manufacturer
2457 i = buf[0x0E] & 0x7f; // offset
2458 for (j=0;j<manufacturer_size-1;j++)
2460 eeprom->manufacturer[j] = buf[2*j+i+2];
2462 eeprom->manufacturer[j] = '\0';
2464 // Decode product name
2465 i = buf[0x10] & 0x7f; // offset
2466 for (j=0;j<product_size-1;j++)
2468 eeprom->product[j] = buf[2*j+i+2];
2470 eeprom->product[j] = '\0';
2473 i = buf[0x12] & 0x7f; // offset
2474 for (j=0;j<serial_size-1;j++)
2476 eeprom->serial[j] = buf[2*j+i+2];
2478 eeprom->serial[j] = '\0';
2483 for (i = 0; i < eeprom_size/2-1; i++)
2486 value += buf[(i*2)+1] << 8;
2488 checksum = value^checksum;
2489 checksum = (checksum << 1) | (checksum >> 15);
2492 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2494 if (eeprom_checksum != checksum)
2496 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2504 Read eeprom location
2506 \param ftdi pointer to ftdi_context
2507 \param eeprom_addr Address of eeprom location to be read
2508 \param eeprom_val Pointer to store read eeprom location
2511 \retval -1: read failed
2512 \retval -2: USB device unavailable
2514 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2516 if (ftdi == NULL || ftdi->usb_dev == NULL)
2517 ftdi_error_return(-2, "USB device unavailable");
2519 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)
2520 ftdi_error_return(-1, "reading eeprom failed");
2528 \param ftdi pointer to ftdi_context
2529 \param eeprom Pointer to store eeprom into
2532 \retval -1: read failed
2533 \retval -2: USB device unavailable
2535 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2539 if (ftdi == NULL || ftdi->usb_dev == NULL)
2540 ftdi_error_return(-2, "USB device unavailable");
2542 for (i = 0; i < ftdi->eeprom_size/2; i++)
2544 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)
2545 ftdi_error_return(-1, "reading eeprom failed");
2552 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2553 Function is only used internally
2556 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2558 return ((value & 1) << 1) |
2559 ((value & 2) << 5) |
2560 ((value & 4) >> 2) |
2561 ((value & 8) << 4) |
2562 ((value & 16) >> 1) |
2563 ((value & 32) >> 1) |
2564 ((value & 64) >> 4) |
2565 ((value & 128) >> 2);
2569 Read the FTDIChip-ID from R-type devices
2571 \param ftdi pointer to ftdi_context
2572 \param chipid Pointer to store FTDIChip-ID
2575 \retval -1: read failed
2576 \retval -2: USB device unavailable
2578 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2580 unsigned int a = 0, b = 0;
2582 if (ftdi == NULL || ftdi->usb_dev == NULL)
2583 ftdi_error_return(-2, "USB device unavailable");
2585 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)
2587 a = a << 8 | a >> 8;
2588 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)
2590 b = b << 8 | b >> 8;
2591 a = (a << 16) | (b & 0xFFFF);
2592 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2593 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2594 *chipid = a ^ 0xa5f0f7d1;
2599 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2603 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2604 Call this function then do a write then call again to see if size changes, if so write again.
2606 \param ftdi pointer to ftdi_context
2607 \param eeprom Pointer to store eeprom into
2608 \param maxsize the size of the buffer to read into
2610 \retval -1: eeprom read failed
2611 \retval -2: USB device unavailable
2612 \retval >=0: size of eeprom
2614 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2616 int i=0,j,minsize=32;
2619 if (ftdi == NULL || ftdi->usb_dev == NULL)
2620 ftdi_error_return(-2, "USB device unavailable");
2624 for (j = 0; i < maxsize/2 && j<size; j++)
2626 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2627 SIO_READ_EEPROM_REQUEST, 0, i,
2628 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2629 ftdi_error_return(-1, "eeprom read failed");
2634 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2640 Write eeprom location
2642 \param ftdi pointer to ftdi_context
2643 \param eeprom_addr Address of eeprom location to be written
2644 \param eeprom_val Value to be written
2647 \retval -1: read failed
2648 \retval -2: USB device unavailable
2650 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2652 if (ftdi == NULL || ftdi->usb_dev == NULL)
2653 ftdi_error_return(-2, "USB device unavailable");
2655 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2656 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2657 NULL, 0, ftdi->usb_write_timeout) != 0)
2658 ftdi_error_return(-1, "unable to write eeprom");
2666 \param ftdi pointer to ftdi_context
2667 \param eeprom Pointer to read eeprom from
2670 \retval -1: read failed
2671 \retval -2: USB device unavailable
2673 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2675 unsigned short usb_val, status;
2678 if (ftdi == NULL || ftdi->usb_dev == NULL)
2679 ftdi_error_return(-2, "USB device unavailable");
2681 /* These commands were traced while running MProg */
2682 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2684 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2686 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2689 for (i = 0; i < ftdi->eeprom_size/2; i++)
2691 usb_val = eeprom[i*2];
2692 usb_val += eeprom[(i*2)+1] << 8;
2693 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2694 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2695 NULL, 0, ftdi->usb_write_timeout) != 0)
2696 ftdi_error_return(-1, "unable to write eeprom");
2705 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2707 \param ftdi pointer to ftdi_context
2710 \retval -1: erase failed
2711 \retval -2: USB device unavailable
2713 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2715 if (ftdi == NULL || ftdi->usb_dev == NULL)
2716 ftdi_error_return(-2, "USB device unavailable");
2718 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
2719 ftdi_error_return(-1, "unable to erase eeprom");
2725 Get string representation for last error code
2727 \param ftdi pointer to ftdi_context
2729 \retval Pointer to error string
2731 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2736 return ftdi->error_str;
2739 /* @} end of doxygen libftdi group */