1 /***************************************************************************
5 copyright : (C) 2003-2010 by Intra2net AG
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
18 \mainpage libftdi API documentation
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/en/developer/libftdi/
23 The library is easy to use. Have a look at this short example:
26 More examples can be found in the "examples" directory.
28 /** \addtogroup libftdi */
38 /* stuff needed for async write */
39 #ifdef LIBFTDI_LINUX_ASYNC_MODE
40 #include <sys/ioctl.h>
41 #include <sys/select.h>
42 #include <sys/types.h>
44 #include <linux/usbdevice_fs.h>
47 #define ftdi_error_return(code, str) do { \
48 ftdi->error_str = str; \
54 Internal function to close usb device pointer.
55 Sets ftdi->usb_dev to NULL.
58 \param ftdi pointer to ftdi_context
60 \retval zero if all is fine, otherwise error code from usb_close()
62 static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
66 if (ftdi && ftdi->usb_dev)
68 ret = usb_close (ftdi->usb_dev);
76 Initializes a ftdi_context.
78 \param ftdi pointer to ftdi_context
81 \retval -1: couldn't allocate read buffer
83 \remark This should be called before all functions
85 int ftdi_init(struct ftdi_context *ftdi)
90 ftdi->usb_read_timeout = 5000;
91 ftdi->usb_write_timeout = 5000;
93 ftdi->type = TYPE_BM; /* chip type */
95 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
97 ftdi->readbuffer = NULL;
98 ftdi->readbuffer_offset = 0;
99 ftdi->readbuffer_remaining = 0;
100 ftdi->writebuffer_chunksize = 4096;
101 ftdi->max_packet_size = 0;
107 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
109 ftdi->error_str = NULL;
111 #ifdef LIBFTDI_LINUX_ASYNC_MODE
112 ftdi->async_usb_buffer_size=10;
113 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
114 ftdi_error_return(-1, "out of memory for async usb buffer");
116 /* initialize async usb buffer with unused-marker */
117 for (i=0; i < ftdi->async_usb_buffer_size; i++)
118 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
120 ftdi->async_usb_buffer_size=0;
121 ftdi->async_usb_buffer = NULL;
124 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
126 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
128 /* All fine. Now allocate the readbuffer */
129 return ftdi_read_data_set_chunksize(ftdi, 4096);
133 Allocate and initialize a new ftdi_context
135 \return a pointer to a new ftdi_context, or NULL on failure
137 struct ftdi_context *ftdi_new(void)
139 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
146 if (ftdi_init(ftdi) != 0)
156 Open selected channels on a chip, otherwise use first channel.
158 \param ftdi pointer to ftdi_context
159 \param interface Interface to use for FT2232C/2232H/4232H chips.
162 \retval -1: unknown interface
163 \retval -2: USB device unavailable
165 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
168 ftdi_error_return(-2, "USB device unavailable");
174 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
178 ftdi->index = INTERFACE_B;
184 ftdi->index = INTERFACE_C;
190 ftdi->index = INTERFACE_D;
195 ftdi_error_return(-1, "Unknown interface");
201 Deinitializes a ftdi_context.
203 \param ftdi pointer to ftdi_context
205 void ftdi_deinit(struct ftdi_context *ftdi)
210 ftdi_usb_close_internal (ftdi);
212 if (ftdi->async_usb_buffer != NULL)
214 free(ftdi->async_usb_buffer);
215 ftdi->async_usb_buffer = NULL;
218 if (ftdi->readbuffer != NULL)
220 free(ftdi->readbuffer);
221 ftdi->readbuffer = NULL;
226 Deinitialize and free an ftdi_context.
228 \param ftdi pointer to ftdi_context
230 void ftdi_free(struct ftdi_context *ftdi)
237 Use an already open libusb device.
239 \param ftdi pointer to ftdi_context
240 \param usb libusb usb_dev_handle to use
242 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
252 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
253 needs to be deallocated by ftdi_list_free() after use.
255 \param ftdi pointer to ftdi_context
256 \param devlist Pointer where to store list of found devices
257 \param vendor Vendor ID to search for
258 \param product Product ID to search for
260 \retval >0: number of devices found
261 \retval -1: usb_find_busses() failed
262 \retval -2: usb_find_devices() failed
263 \retval -3: out of memory
265 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
267 struct ftdi_device_list **curdev;
269 struct usb_device *dev;
273 if (usb_find_busses() < 0)
274 ftdi_error_return(-1, "usb_find_busses() failed");
275 if (usb_find_devices() < 0)
276 ftdi_error_return(-2, "usb_find_devices() failed");
280 for (bus = usb_get_busses(); bus; bus = bus->next)
282 for (dev = bus->devices; dev; dev = dev->next)
284 if (dev->descriptor.idVendor == vendor
285 && dev->descriptor.idProduct == product)
287 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
289 ftdi_error_return(-3, "out of memory");
291 (*curdev)->next = NULL;
292 (*curdev)->dev = dev;
294 curdev = &(*curdev)->next;
304 Frees a usb device list.
306 \param devlist USB device list created by ftdi_usb_find_all()
308 void ftdi_list_free(struct ftdi_device_list **devlist)
310 struct ftdi_device_list *curdev, *next;
312 for (curdev = *devlist; curdev != NULL;)
323 Frees a usb device list.
325 \param devlist USB device list created by ftdi_usb_find_all()
327 void ftdi_list_free2(struct ftdi_device_list *devlist)
329 ftdi_list_free(&devlist);
333 Return device ID strings from the usb device.
335 The parameters manufacturer, description and serial may be NULL
336 or pointer to buffers to store the fetched strings.
338 \note Use this function only in combination with ftdi_usb_find_all()
339 as it closes the internal "usb_dev" after use.
341 \param ftdi pointer to ftdi_context
342 \param dev libusb usb_dev to use
343 \param manufacturer Store manufacturer string here if not NULL
344 \param mnf_len Buffer size of manufacturer string
345 \param description Store product description string here if not NULL
346 \param desc_len Buffer size of product description string
347 \param serial Store serial string here if not NULL
348 \param serial_len Buffer size of serial string
351 \retval -1: wrong arguments
352 \retval -4: unable to open device
353 \retval -7: get product manufacturer failed
354 \retval -8: get product description failed
355 \retval -9: get serial number failed
356 \retval -10: unable to close device
358 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
359 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
361 if ((ftdi==NULL) || (dev==NULL))
364 if (!(ftdi->usb_dev = usb_open(dev)))
365 ftdi_error_return(-4, usb_strerror());
367 if (manufacturer != NULL)
369 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
371 ftdi_usb_close_internal (ftdi);
372 ftdi_error_return(-7, usb_strerror());
376 if (description != NULL)
378 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
380 ftdi_usb_close_internal (ftdi);
381 ftdi_error_return(-8, usb_strerror());
387 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
389 ftdi_usb_close_internal (ftdi);
390 ftdi_error_return(-9, usb_strerror());
394 if (ftdi_usb_close_internal (ftdi) != 0)
395 ftdi_error_return(-10, usb_strerror());
401 * Internal function to determine the maximum packet size.
402 * \param ftdi pointer to ftdi_context
403 * \param dev libusb usb_dev to use
404 * \retval Maximum packet size for this device
406 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
408 unsigned int packet_size;
411 if (ftdi == NULL || dev == NULL)
414 // Determine maximum packet size. Init with default value.
415 // New hi-speed devices from FTDI use a packet size of 512 bytes
416 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
417 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
422 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
424 struct usb_config_descriptor config = dev->config[0];
426 if (ftdi->interface < config.bNumInterfaces)
428 struct usb_interface interface = config.interface[ftdi->interface];
429 if (interface.num_altsetting > 0)
431 struct usb_interface_descriptor descriptor = interface.altsetting[0];
432 if (descriptor.bNumEndpoints > 0)
434 packet_size = descriptor.endpoint[0].wMaxPacketSize;
444 Opens a ftdi device given by an usb_device.
446 \param ftdi pointer to ftdi_context
447 \param dev libusb usb_dev to use
450 \retval -3: unable to config device
451 \retval -4: unable to open device
452 \retval -5: unable to claim device
453 \retval -6: reset failed
454 \retval -7: set baudrate failed
455 \retval -8: ftdi context invalid
457 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
459 int detach_errno = 0;
463 ftdi_error_return(-8, "ftdi context invalid");
465 if (!(ftdi->usb_dev = usb_open(dev)))
466 ftdi_error_return(-4, "usb_open() failed");
468 #ifdef LIBUSB_HAS_GET_DRIVER_NP
469 // Try to detach ftdi_sio kernel module.
470 // Returns ENODATA if driver is not loaded.
472 // The return code is kept in a separate variable and only parsed
473 // if usb_set_configuration() or usb_claim_interface() fails as the
474 // detach operation might be denied and everything still works fine.
475 // Likely scenario is a static ftdi_sio kernel module.
476 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
478 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
479 detach_errno = errno;
484 // set configuration (needed especially for windows)
485 // tolerate EBUSY: one device with one configuration, but two interfaces
486 // and libftdi sessions to both interfaces (e.g. FT2232)
488 if (dev->descriptor.bNumConfigurations > 0)
490 // libusb-win32 on Windows 64 can return a null pointer for a valid device
492 config_val = dev->config[0].bConfigurationValue;
494 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
497 ftdi_usb_close_internal (ftdi);
498 if (detach_errno == EPERM)
500 ftdi_error_return(-8, "inappropriate permissions on device!");
504 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
510 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
512 ftdi_usb_close_internal (ftdi);
513 if (detach_errno == EPERM)
515 ftdi_error_return(-8, "inappropriate permissions on device!");
519 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
523 if (ftdi_usb_reset (ftdi) != 0)
525 ftdi_usb_close_internal (ftdi);
526 ftdi_error_return(-6, "ftdi_usb_reset failed");
529 // Try to guess chip type
530 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
531 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
532 && dev->descriptor.iSerialNumber == 0))
533 ftdi->type = TYPE_BM;
534 else if (dev->descriptor.bcdDevice == 0x200)
535 ftdi->type = TYPE_AM;
536 else if (dev->descriptor.bcdDevice == 0x500)
537 ftdi->type = TYPE_2232C;
538 else if (dev->descriptor.bcdDevice == 0x600)
540 else if (dev->descriptor.bcdDevice == 0x700)
541 ftdi->type = TYPE_2232H;
542 else if (dev->descriptor.bcdDevice == 0x800)
543 ftdi->type = TYPE_4232H;
545 // Set default interface on dual/quad type chips
552 ftdi->index = INTERFACE_A;
558 // Determine maximum packet size
559 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
561 if (ftdi_set_baudrate (ftdi, 9600) != 0)
563 ftdi_usb_close_internal (ftdi);
564 ftdi_error_return(-7, "set baudrate failed");
567 ftdi_error_return(0, "all fine");
571 Opens the first device with a given vendor and product ids.
573 \param ftdi pointer to ftdi_context
574 \param vendor Vendor ID
575 \param product Product ID
577 \retval same as ftdi_usb_open_desc()
579 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
581 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
585 Opens the first device with a given, vendor id, product id,
586 description and serial.
588 \param ftdi pointer to ftdi_context
589 \param vendor Vendor ID
590 \param product Product ID
591 \param description Description to search for. Use NULL if not needed.
592 \param serial Serial to search for. Use NULL if not needed.
595 \retval -1: usb_find_busses() failed
596 \retval -2: usb_find_devices() failed
597 \retval -3: usb device not found
598 \retval -4: unable to open device
599 \retval -5: unable to claim device
600 \retval -6: reset failed
601 \retval -7: set baudrate failed
602 \retval -8: get product description failed
603 \retval -9: get serial number failed
604 \retval -10: unable to close device
606 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
607 const char* description, const char* serial)
609 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
613 Opens the index-th device with a given, vendor id, product id,
614 description and serial.
616 \param ftdi pointer to ftdi_context
617 \param vendor Vendor ID
618 \param product Product ID
619 \param description Description to search for. Use NULL if not needed.
620 \param serial Serial to search for. Use NULL if not needed.
621 \param index Number of matching device to open if there are more than one, starts with 0.
624 \retval -1: usb_find_busses() failed
625 \retval -2: usb_find_devices() failed
626 \retval -3: usb device not found
627 \retval -4: unable to open device
628 \retval -5: unable to claim device
629 \retval -6: reset failed
630 \retval -7: set baudrate failed
631 \retval -8: get product description failed
632 \retval -9: get serial number failed
633 \retval -10: unable to close device
634 \retval -11: ftdi context invalid
636 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
637 const char* description, const char* serial, unsigned int index)
640 struct usb_device *dev;
645 if (usb_find_busses() < 0)
646 ftdi_error_return(-1, "usb_find_busses() failed");
647 if (usb_find_devices() < 0)
648 ftdi_error_return(-2, "usb_find_devices() failed");
651 ftdi_error_return(-11, "ftdi context invalid");
653 for (bus = usb_get_busses(); bus; bus = bus->next)
655 for (dev = bus->devices; dev; dev = dev->next)
657 if (dev->descriptor.idVendor == vendor
658 && dev->descriptor.idProduct == product)
660 if (!(ftdi->usb_dev = usb_open(dev)))
661 ftdi_error_return(-4, "usb_open() failed");
663 if (description != NULL)
665 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
667 ftdi_usb_close_internal (ftdi);
668 ftdi_error_return(-8, "unable to fetch product description");
670 if (strncmp(string, description, sizeof(string)) != 0)
672 if (ftdi_usb_close_internal (ftdi) != 0)
673 ftdi_error_return(-10, "unable to close device");
679 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
681 ftdi_usb_close_internal (ftdi);
682 ftdi_error_return(-9, "unable to fetch serial number");
684 if (strncmp(string, serial, sizeof(string)) != 0)
686 if (ftdi_usb_close_internal (ftdi) != 0)
687 ftdi_error_return(-10, "unable to close device");
692 if (ftdi_usb_close_internal (ftdi) != 0)
693 ftdi_error_return(-10, "unable to close device");
701 return ftdi_usb_open_dev(ftdi, dev);
707 ftdi_error_return(-3, "device not found");
711 Opens the ftdi-device described by a description-string.
712 Intended to be used for parsing a device-description given as commandline argument.
714 \param ftdi pointer to ftdi_context
715 \param description NULL-terminated description-string, using this format:
716 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
717 \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")
718 \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
719 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
721 \note The description format may be extended in later versions.
724 \retval -1: usb_find_busses() failed
725 \retval -2: usb_find_devices() failed
726 \retval -3: usb device not found
727 \retval -4: unable to open device
728 \retval -5: unable to claim device
729 \retval -6: reset failed
730 \retval -7: set baudrate failed
731 \retval -8: get product description failed
732 \retval -9: get serial number failed
733 \retval -10: unable to close device
734 \retval -11: illegal description format
735 \retval -12: ftdi context invalid
737 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
740 ftdi_error_return(-12, "ftdi context invalid");
742 if (description[0] == 0 || description[1] != ':')
743 ftdi_error_return(-11, "illegal description format");
745 if (description[0] == 'd')
748 struct usb_device *dev;
752 if (usb_find_busses() < 0)
753 ftdi_error_return(-1, "usb_find_busses() failed");
754 if (usb_find_devices() < 0)
755 ftdi_error_return(-2, "usb_find_devices() failed");
757 for (bus = usb_get_busses(); bus; bus = bus->next)
759 for (dev = bus->devices; dev; dev = dev->next)
761 /* XXX: This doesn't handle symlinks/odd paths/etc... */
762 const char *desc = description + 2;
763 size_t len = strlen(bus->dirname);
764 if (strncmp(desc, bus->dirname, len))
770 if (strcmp(desc, dev->filename))
772 return ftdi_usb_open_dev(ftdi, dev);
777 ftdi_error_return(-3, "device not found");
779 else if (description[0] == 'i' || description[0] == 's')
782 unsigned int product;
783 unsigned int index=0;
784 const char *serial=NULL;
785 const char *startp, *endp;
788 startp=description+2;
789 vendor=strtoul((char*)startp,(char**)&endp,0);
790 if (*endp != ':' || endp == startp || errno != 0)
791 ftdi_error_return(-11, "illegal description format");
794 product=strtoul((char*)startp,(char**)&endp,0);
795 if (endp == startp || errno != 0)
796 ftdi_error_return(-11, "illegal description format");
798 if (description[0] == 'i' && *endp != 0)
800 /* optional index field in i-mode */
802 ftdi_error_return(-11, "illegal description format");
805 index=strtoul((char*)startp,(char**)&endp,0);
806 if (*endp != 0 || endp == startp || errno != 0)
807 ftdi_error_return(-11, "illegal description format");
809 if (description[0] == 's')
812 ftdi_error_return(-11, "illegal description format");
814 /* rest of the description is the serial */
818 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
822 ftdi_error_return(-11, "illegal description format");
827 Resets the ftdi device.
829 \param ftdi pointer to ftdi_context
832 \retval -1: FTDI reset failed
833 \retval -2: USB device unavailable
835 int ftdi_usb_reset(struct ftdi_context *ftdi)
837 if (ftdi == NULL || ftdi->usb_dev == NULL)
838 ftdi_error_return(-2, "USB device unavailable");
840 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
841 SIO_RESET_REQUEST, SIO_RESET_SIO,
842 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
843 ftdi_error_return(-1,"FTDI reset failed");
845 // Invalidate data in the readbuffer
846 ftdi->readbuffer_offset = 0;
847 ftdi->readbuffer_remaining = 0;
853 Clears the read buffer on the chip and the internal read buffer.
855 \param ftdi pointer to ftdi_context
858 \retval -1: read buffer purge failed
859 \retval -2: USB device unavailable
861 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
863 if (ftdi == NULL || ftdi->usb_dev == NULL)
864 ftdi_error_return(-2, "USB device unavailable");
866 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
867 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
868 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
869 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
871 // Invalidate data in the readbuffer
872 ftdi->readbuffer_offset = 0;
873 ftdi->readbuffer_remaining = 0;
879 Clears the write buffer on the chip.
881 \param ftdi pointer to ftdi_context
884 \retval -1: write buffer purge failed
885 \retval -2: USB device unavailable
887 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
889 if (ftdi == NULL || ftdi->usb_dev == NULL)
890 ftdi_error_return(-2, "USB device unavailable");
892 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
893 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
894 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
895 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
901 Clears the buffers on the chip and the internal read buffer.
903 \param ftdi pointer to ftdi_context
906 \retval -1: read buffer purge failed
907 \retval -2: write buffer purge failed
908 \retval -3: USB device unavailable
910 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
914 if (ftdi == NULL || ftdi->usb_dev == NULL)
915 ftdi_error_return(-3, "USB device unavailable");
917 result = ftdi_usb_purge_rx_buffer(ftdi);
921 result = ftdi_usb_purge_tx_buffer(ftdi);
931 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
933 \param ftdi pointer to ftdi_context
936 \retval -1: usb_release failed
937 \retval -2: usb_close failed
938 \retval -3: ftdi context invalid
940 int ftdi_usb_close(struct ftdi_context *ftdi)
945 ftdi_error_return(-3, "ftdi context invalid");
947 #ifdef LIBFTDI_LINUX_ASYNC_MODE
948 /* try to release some kernel resources */
949 ftdi_async_complete(ftdi,1);
952 if (ftdi->usb_dev != NULL)
953 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
956 if (ftdi_usb_close_internal (ftdi) != 0)
963 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
964 Function is only used internally
967 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
968 unsigned short *value, unsigned short *index)
970 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
971 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
972 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
973 int divisor, best_divisor, best_baud, best_baud_diff;
974 unsigned long encoded_divisor;
983 divisor = 24000000 / baudrate;
985 if (ftdi->type == TYPE_AM)
987 // Round down to supported fraction (AM only)
988 divisor -= am_adjust_dn[divisor & 7];
991 // Try this divisor and the one above it (because division rounds down)
995 for (i = 0; i < 2; i++)
997 int try_divisor = divisor + i;
1001 // Round up to supported divisor value
1002 if (try_divisor <= 8)
1004 // Round up to minimum supported divisor
1007 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1009 // BM doesn't support divisors 9 through 11 inclusive
1012 else if (divisor < 16)
1014 // AM doesn't support divisors 9 through 15 inclusive
1019 if (ftdi->type == TYPE_AM)
1021 // Round up to supported fraction (AM only)
1022 try_divisor += am_adjust_up[try_divisor & 7];
1023 if (try_divisor > 0x1FFF8)
1025 // Round down to maximum supported divisor value (for AM)
1026 try_divisor = 0x1FFF8;
1031 if (try_divisor > 0x1FFFF)
1033 // Round down to maximum supported divisor value (for BM)
1034 try_divisor = 0x1FFFF;
1038 // Get estimated baud rate (to nearest integer)
1039 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1040 // Get absolute difference from requested baud rate
1041 if (baud_estimate < baudrate)
1043 baud_diff = baudrate - baud_estimate;
1047 baud_diff = baud_estimate - baudrate;
1049 if (i == 0 || baud_diff < best_baud_diff)
1051 // Closest to requested baud rate so far
1052 best_divisor = try_divisor;
1053 best_baud = baud_estimate;
1054 best_baud_diff = baud_diff;
1057 // Spot on! No point trying
1062 // Encode the best divisor value
1063 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1064 // Deal with special cases for encoded value
1065 if (encoded_divisor == 1)
1067 encoded_divisor = 0; // 3000000 baud
1069 else if (encoded_divisor == 0x4001)
1071 encoded_divisor = 1; // 2000000 baud (BM only)
1073 // Split into "value" and "index" values
1074 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1075 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1077 *index = (unsigned short)(encoded_divisor >> 8);
1079 *index |= ftdi->index;
1082 *index = (unsigned short)(encoded_divisor >> 16);
1084 // Return the nearest baud rate
1089 Sets the chip baud rate
1091 \param ftdi pointer to ftdi_context
1092 \param baudrate baud rate to set
1095 \retval -1: invalid baudrate
1096 \retval -2: setting baudrate failed
1097 \retval -3: USB device unavailable
1099 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1101 unsigned short value, index;
1102 int actual_baudrate;
1104 if (ftdi == NULL || ftdi->usb_dev == NULL)
1105 ftdi_error_return(-3, "USB device unavailable");
1107 if (ftdi->bitbang_enabled)
1109 baudrate = baudrate*4;
1112 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1113 if (actual_baudrate <= 0)
1114 ftdi_error_return (-1, "Silly baudrate <= 0.");
1116 // Check within tolerance (about 5%)
1117 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1118 || ((actual_baudrate < baudrate)
1119 ? (actual_baudrate * 21 < baudrate * 20)
1120 : (baudrate * 21 < actual_baudrate * 20)))
1121 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1123 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1124 SIO_SET_BAUDRATE_REQUEST, value,
1125 index, NULL, 0, ftdi->usb_write_timeout) != 0)
1126 ftdi_error_return (-2, "Setting new baudrate failed");
1128 ftdi->baudrate = baudrate;
1133 Set (RS232) line characteristics.
1134 The break type can only be set via ftdi_set_line_property2()
1135 and defaults to "off".
1137 \param ftdi pointer to ftdi_context
1138 \param bits Number of bits
1139 \param sbit Number of stop bits
1140 \param parity Parity mode
1143 \retval -1: Setting line property failed
1145 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1146 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1148 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1152 Set (RS232) line characteristics
1154 \param ftdi pointer to ftdi_context
1155 \param bits Number of bits
1156 \param sbit Number of stop bits
1157 \param parity Parity mode
1158 \param break_type Break type
1161 \retval -1: Setting line property failed
1162 \retval -2: USB device unavailable
1164 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1165 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1166 enum ftdi_break_type break_type)
1168 unsigned short value = bits;
1170 if (ftdi == NULL || ftdi->usb_dev == NULL)
1171 ftdi_error_return(-2, "USB device unavailable");
1176 value |= (0x00 << 8);
1179 value |= (0x01 << 8);
1182 value |= (0x02 << 8);
1185 value |= (0x03 << 8);
1188 value |= (0x04 << 8);
1195 value |= (0x00 << 11);
1198 value |= (0x01 << 11);
1201 value |= (0x02 << 11);
1208 value |= (0x00 << 14);
1211 value |= (0x01 << 14);
1215 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1216 SIO_SET_DATA_REQUEST, value,
1217 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1218 ftdi_error_return (-1, "Setting new line property failed");
1224 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1226 \param ftdi pointer to ftdi_context
1227 \param buf Buffer with the data
1228 \param size Size of the buffer
1230 \retval -666: USB device unavailable
1231 \retval <0: error code from usb_bulk_write()
1232 \retval >0: number of bytes written
1234 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1238 int total_written = 0;
1240 if (ftdi == NULL || ftdi->usb_dev == NULL)
1241 ftdi_error_return(-666, "USB device unavailable");
1243 while (offset < size)
1245 int write_size = ftdi->writebuffer_chunksize;
1247 if (offset+write_size > size)
1248 write_size = size-offset;
1250 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
1252 ftdi_error_return(ret, "usb bulk write failed");
1254 total_written += ret;
1255 offset += write_size;
1258 return total_written;
1261 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1262 #ifdef USB_CLASS_PTP
1263 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1265 /* this is strongly dependent on libusb using the same struct layout. If libusb
1266 changes in some later version this may break horribly (this is for libusb 0.1.12) */
1267 struct usb_dev_handle
1270 // some other stuff coming here we don't need
1274 Check for pending async urbs
1277 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
1279 struct usbdevfs_urb *urb;
1283 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1285 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1286 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1294 Wait until one or more async URBs are completed by the kernel and mark their
1295 positions in the async-buffer as unused
1297 \param ftdi pointer to ftdi_context
1298 \param wait_for_more if != 0 wait for more than one write to complete
1299 \param timeout_msec max milliseconds to wait
1303 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
1306 struct usbdevfs_urb *urb;
1312 FD_SET(ftdi->usb_dev->fd, &writefds);
1314 /* init timeout only once, select writes time left after call */
1315 tv.tv_sec = timeout_msec / 1000;
1316 tv.tv_usec = (timeout_msec % 1000) * 1000;
1323 while (_usb_get_async_urbs_pending(ftdi)
1324 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1327 if (keep_going && !wait_for_more)
1329 /* don't wait if repeating only for keep_going */
1334 /* wait for timeout msec or something written ready */
1335 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1338 if (ret == 0 && urb != NULL)
1340 /* got a free urb, mark it */
1341 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
1343 /* try to get more urbs that are ready now, but don't wait anymore */
1348 /* no more urbs waiting */
1356 Wait until one or more async URBs are completed by the kernel and mark their
1357 positions in the async-buffer as unused.
1359 \param ftdi pointer to ftdi_context
1360 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1362 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1364 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
1368 Stupid libusb does not offer async writes nor does it allow
1369 access to its fd - so we need some hacks here.
1372 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
1374 struct usbdevfs_urb *urb;
1375 int bytesdone = 0, requested;
1376 int ret, cleanup_count;
1381 /* find a free urb buffer we can use */
1384 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1386 if (i==ftdi->async_usb_buffer_size)
1388 /* wait until some buffers are free */
1389 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1392 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1394 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1395 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1396 break; /* found a free urb position */
1401 /* no free urb position found */
1405 requested = size - bytesdone;
1406 if (requested > 4096)
1409 memset(urb,0,sizeof(urb));
1411 urb->type = USBDEVFS_URB_TYPE_BULK;
1414 urb->buffer = bytes + bytesdone;
1415 urb->buffer_length = requested;
1417 urb->actual_length = 0;
1418 urb->number_of_packets = 0;
1419 urb->usercontext = 0;
1423 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1425 while (ret < 0 && errno == EINTR);
1427 return ret; /* the caller can read errno to get more info */
1429 bytesdone += requested;
1431 while (bytesdone < size);
1436 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1437 Does not wait for completion of the transfer nor does it make sure that
1438 the transfer was successful.
1440 This function could be extended to use signals and callbacks to inform the
1441 caller of completion or error - but this is not done yet, volunteers welcome.
1443 Works around libusb and directly accesses functions only available on Linux.
1444 Only available if compiled with --with-async-mode.
1446 \param ftdi pointer to ftdi_context
1447 \param buf Buffer with the data
1448 \param size Size of the buffer
1450 \retval -666: USB device unavailable
1451 \retval <0: error code from usb_bulk_write()
1452 \retval >0: number of bytes written
1454 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1458 int total_written = 0;
1460 if (ftdi == NULL || ftdi->usb_dev == NULL)
1461 ftdi_error_return(-666, "USB device unavailable");
1463 while (offset < size)
1465 int write_size = ftdi->writebuffer_chunksize;
1467 if (offset+write_size > size)
1468 write_size = size-offset;
1470 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1472 ftdi_error_return(ret, "usb bulk write async failed");
1474 total_written += ret;
1475 offset += write_size;
1478 return total_written;
1480 #endif // LIBFTDI_LINUX_ASYNC_MODE
1483 Configure write buffer chunk size.
1486 \param ftdi pointer to ftdi_context
1487 \param chunksize Chunk size
1490 \retval -1: ftdi context invalid
1492 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1495 ftdi_error_return(-1, "ftdi context invalid");
1497 ftdi->writebuffer_chunksize = chunksize;
1502 Get write buffer chunk size.
1504 \param ftdi pointer to ftdi_context
1505 \param chunksize Pointer to store chunk size in
1508 \retval -1: ftdi context invalid
1510 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1513 ftdi_error_return(-1, "ftdi context invalid");
1515 *chunksize = ftdi->writebuffer_chunksize;
1520 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1522 Returns when at least one byte is available or when the latency timer has elapsed
1523 Automatically strips the two modem status bytes transfered during every read.
1525 \param ftdi pointer to ftdi_context
1526 \param buf Buffer to store data in
1527 \param size Size of the buffer
1529 \retval -666: USB device unavailable
1530 \retval <0: error code from usb_bulk_read()
1531 \retval 0: no data was available
1532 \retval >0: number of bytes read
1535 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1537 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1540 if (ftdi == NULL || ftdi->usb_dev == NULL)
1541 ftdi_error_return(-666, "USB device unavailable");
1543 packet_size = ftdi->max_packet_size;
1544 // Packet size sanity check (avoid division by zero)
1545 if (packet_size == 0)
1546 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1548 // everything we want is still in the readbuffer?
1549 if (size <= ftdi->readbuffer_remaining)
1551 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1554 ftdi->readbuffer_remaining -= size;
1555 ftdi->readbuffer_offset += size;
1557 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1561 // something still in the readbuffer, but not enough to satisfy 'size'?
1562 if (ftdi->readbuffer_remaining != 0)
1564 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1567 offset += ftdi->readbuffer_remaining;
1569 // do the actual USB read
1570 while (offset < size && ret > 0)
1572 ftdi->readbuffer_remaining = 0;
1573 ftdi->readbuffer_offset = 0;
1574 /* returns how much received */
1575 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1577 ftdi_error_return(ret, "usb bulk read failed");
1581 // skip FTDI status bytes.
1582 // Maybe stored in the future to enable modem use
1583 num_of_chunks = ret / packet_size;
1584 chunk_remains = ret % packet_size;
1585 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1587 ftdi->readbuffer_offset += 2;
1590 if (ret > packet_size - 2)
1592 for (i = 1; i < num_of_chunks; i++)
1593 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1594 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1596 if (chunk_remains > 2)
1598 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1599 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1601 ret -= 2*num_of_chunks;
1604 ret -= 2*(num_of_chunks-1)+chunk_remains;
1609 // no more data to read?
1614 // data still fits in buf?
1615 if (offset+ret <= size)
1617 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1618 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1621 /* Did we read exactly the right amount of bytes? */
1623 //printf("read_data exact rem %d offset %d\n",
1624 //ftdi->readbuffer_remaining, offset);
1629 // only copy part of the data or size <= readbuffer_chunksize
1630 int part_size = size-offset;
1631 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1633 ftdi->readbuffer_offset += part_size;
1634 ftdi->readbuffer_remaining = ret-part_size;
1635 offset += part_size;
1637 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1638 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1649 Configure read buffer chunk size.
1652 Automatically reallocates the buffer.
1654 \param ftdi pointer to ftdi_context
1655 \param chunksize Chunk size
1658 \retval -1: ftdi context invalid
1660 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1662 unsigned char *new_buf;
1665 ftdi_error_return(-1, "ftdi context invalid");
1667 // Invalidate all remaining data
1668 ftdi->readbuffer_offset = 0;
1669 ftdi->readbuffer_remaining = 0;
1671 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1672 ftdi_error_return(-1, "out of memory for readbuffer");
1674 ftdi->readbuffer = new_buf;
1675 ftdi->readbuffer_chunksize = chunksize;
1681 Get read buffer chunk size.
1683 \param ftdi pointer to ftdi_context
1684 \param chunksize Pointer to store chunk size in
1687 \retval -1: FTDI context invalid
1689 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1692 ftdi_error_return(-1, "FTDI context invalid");
1694 *chunksize = ftdi->readbuffer_chunksize;
1700 Enable bitbang mode.
1702 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1704 \param ftdi pointer to ftdi_context
1705 \param bitmask Bitmask to configure lines.
1706 HIGH/ON value configures a line as output.
1709 \retval -1: can't enable bitbang mode
1710 \retval -2: USB device unavailable
1712 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1714 unsigned short usb_val;
1716 if (ftdi == NULL || ftdi->usb_dev == NULL)
1717 ftdi_error_return(-2, "USB device unavailable");
1719 usb_val = bitmask; // low byte: bitmask
1720 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1721 usb_val |= (ftdi->bitbang_mode << 8);
1723 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1724 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1725 NULL, 0, ftdi->usb_write_timeout) != 0)
1726 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1728 ftdi->bitbang_enabled = 1;
1733 Disable bitbang mode.
1735 \param ftdi pointer to ftdi_context
1738 \retval -1: can't disable bitbang mode
1739 \retval -2: USB device unavailable
1741 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1743 if (ftdi == NULL || ftdi->usb_dev == NULL)
1744 ftdi_error_return(-2, "USB device unavailable");
1746 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)
1747 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1749 ftdi->bitbang_enabled = 0;
1754 Enable/disable bitbang modes.
1756 \param ftdi pointer to ftdi_context
1757 \param bitmask Bitmask to configure lines.
1758 HIGH/ON value configures a line as output.
1759 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1762 \retval -1: can't enable bitbang mode
1763 \retval -2: USB device unavailable
1765 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1767 unsigned short usb_val;
1769 if (ftdi == NULL || ftdi->usb_dev == NULL)
1770 ftdi_error_return(-2, "USB device unavailable");
1772 usb_val = bitmask; // low byte: bitmask
1773 usb_val |= (mode << 8);
1774 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)
1775 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1777 ftdi->bitbang_mode = mode;
1778 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1783 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1785 \param ftdi pointer to ftdi_context
1786 \param pins Pointer to store pins into
1789 \retval -1: read pins failed
1790 \retval -2: USB device unavailable
1792 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1794 if (ftdi == NULL || ftdi->usb_dev == NULL)
1795 ftdi_error_return(-2, "USB device unavailable");
1797 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)
1798 ftdi_error_return(-1, "read pins failed");
1806 The FTDI chip keeps data in the internal buffer for a specific
1807 amount of time if the buffer is not full yet to decrease
1808 load on the usb bus.
1810 \param ftdi pointer to ftdi_context
1811 \param latency Value between 1 and 255
1814 \retval -1: latency out of range
1815 \retval -2: unable to set latency timer
1816 \retval -3: USB device unavailable
1818 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1820 unsigned short usb_val;
1823 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1825 if (ftdi == NULL || ftdi->usb_dev == NULL)
1826 ftdi_error_return(-3, "USB device unavailable");
1829 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)
1830 ftdi_error_return(-2, "unable to set latency timer");
1838 \param ftdi pointer to ftdi_context
1839 \param latency Pointer to store latency value in
1842 \retval -1: unable to get latency timer
1843 \retval -2: USB device unavailable
1845 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1847 unsigned short usb_val;
1849 if (ftdi == NULL || ftdi->usb_dev == NULL)
1850 ftdi_error_return(-2, "USB device unavailable");
1852 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)
1853 ftdi_error_return(-1, "reading latency timer failed");
1855 *latency = (unsigned char)usb_val;
1860 Poll modem status information
1862 This function allows the retrieve the two status bytes of the device.
1863 The device sends these bytes also as a header for each read access
1864 where they are discarded by ftdi_read_data(). The chip generates
1865 the two stripped status bytes in the absence of data every 40 ms.
1867 Layout of the first byte:
1868 - B0..B3 - must be 0
1869 - B4 Clear to send (CTS)
1872 - B5 Data set ready (DTS)
1875 - B6 Ring indicator (RI)
1878 - B7 Receive line signal detect (RLSD)
1882 Layout of the second byte:
1883 - B0 Data ready (DR)
1884 - B1 Overrun error (OE)
1885 - B2 Parity error (PE)
1886 - B3 Framing error (FE)
1887 - B4 Break interrupt (BI)
1888 - B5 Transmitter holding register (THRE)
1889 - B6 Transmitter empty (TEMT)
1890 - B7 Error in RCVR FIFO
1892 \param ftdi pointer to ftdi_context
1893 \param status Pointer to store status information in. Must be two bytes.
1896 \retval -1: unable to retrieve status information
1897 \retval -2: USB device unavailable
1899 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1903 if (ftdi == NULL || ftdi->usb_dev == NULL)
1904 ftdi_error_return(-2, "USB device unavailable");
1906 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)
1907 ftdi_error_return(-1, "getting modem status failed");
1909 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
1915 Set flowcontrol for ftdi chip
1917 \param ftdi pointer to ftdi_context
1918 \param flowctrl flow control to use. should be
1919 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1922 \retval -1: set flow control failed
1923 \retval -2: USB device unavailable
1925 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1927 if (ftdi == NULL || ftdi->usb_dev == NULL)
1928 ftdi_error_return(-2, "USB device unavailable");
1930 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1931 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1932 NULL, 0, ftdi->usb_write_timeout) != 0)
1933 ftdi_error_return(-1, "set flow control failed");
1941 \param ftdi pointer to ftdi_context
1942 \param state state to set line to (1 or 0)
1945 \retval -1: set dtr failed
1946 \retval -2: USB device unavailable
1948 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1950 unsigned short usb_val;
1952 if (ftdi == NULL || ftdi->usb_dev == NULL)
1953 ftdi_error_return(-2, "USB device unavailable");
1956 usb_val = SIO_SET_DTR_HIGH;
1958 usb_val = SIO_SET_DTR_LOW;
1960 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1961 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1962 NULL, 0, ftdi->usb_write_timeout) != 0)
1963 ftdi_error_return(-1, "set dtr failed");
1971 \param ftdi pointer to ftdi_context
1972 \param state state to set line to (1 or 0)
1975 \retval -1: set rts failed
1976 \retval -2: USB device unavailable
1978 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1980 unsigned short usb_val;
1982 if (ftdi == NULL || ftdi->usb_dev == NULL)
1983 ftdi_error_return(-2, "USB device unavailable");
1986 usb_val = SIO_SET_RTS_HIGH;
1988 usb_val = SIO_SET_RTS_LOW;
1990 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1991 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1992 NULL, 0, ftdi->usb_write_timeout) != 0)
1993 ftdi_error_return(-1, "set of rts failed");
1999 Set dtr and rts line in one pass
2001 \param ftdi pointer to ftdi_context
2002 \param dtr DTR state to set line to (1 or 0)
2003 \param rts RTS state to set line to (1 or 0)
2006 \retval -1: set dtr/rts failed
2007 \retval -2: USB device unavailable
2009 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2011 unsigned short usb_val;
2013 if (ftdi == NULL || ftdi->usb_dev == NULL)
2014 ftdi_error_return(-2, "USB device unavailable");
2017 usb_val = SIO_SET_DTR_HIGH;
2019 usb_val = SIO_SET_DTR_LOW;
2022 usb_val |= SIO_SET_RTS_HIGH;
2024 usb_val |= SIO_SET_RTS_LOW;
2026 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2027 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2028 NULL, 0, ftdi->usb_write_timeout) != 0)
2029 ftdi_error_return(-1, "set of rts/dtr failed");
2035 Set the special event character
2037 \param ftdi pointer to ftdi_context
2038 \param eventch Event character
2039 \param enable 0 to disable the event character, non-zero otherwise
2042 \retval -1: unable to set event character
2043 \retval -2: USB device unavailable
2045 int ftdi_set_event_char(struct ftdi_context *ftdi,
2046 unsigned char eventch, unsigned char enable)
2048 unsigned short usb_val;
2050 if (ftdi == NULL || ftdi->usb_dev == NULL)
2051 ftdi_error_return(-2, "USB device unavailable");
2057 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)
2058 ftdi_error_return(-1, "setting event character failed");
2066 \param ftdi pointer to ftdi_context
2067 \param errorch Error character
2068 \param enable 0 to disable the error character, non-zero otherwise
2071 \retval -1: unable to set error character
2072 \retval -2: USB device unavailable
2074 int ftdi_set_error_char(struct ftdi_context *ftdi,
2075 unsigned char errorch, unsigned char enable)
2077 unsigned short usb_val;
2079 if (ftdi == NULL || ftdi->usb_dev == NULL)
2080 ftdi_error_return(-2, "USB device unavailable");
2086 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)
2087 ftdi_error_return(-1, "setting error character failed");
2095 \param ftdi pointer to ftdi_context
2096 \param eeprom Pointer to ftdi_eeprom
2100 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2105 ftdi->eeprom_size=size;
2110 Init eeprom with default values.
2112 \param eeprom Pointer to ftdi_eeprom
2114 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2121 eeprom->vendor_id = 0x0403;
2122 eeprom->product_id = 0x6001;
2124 eeprom->self_powered = 1;
2125 eeprom->remote_wakeup = 1;
2126 eeprom->chip_type = TYPE_BM;
2128 eeprom->in_is_isochronous = 0;
2129 eeprom->out_is_isochronous = 0;
2130 eeprom->suspend_pull_downs = 0;
2132 eeprom->use_serial = 0;
2133 eeprom->change_usb_version = 0;
2134 eeprom->usb_version = 0x0200;
2135 eeprom->max_power = 0;
2137 eeprom->manufacturer = NULL;
2138 eeprom->product = NULL;
2139 eeprom->serial = NULL;
2140 for (i=0; i < 5; i++)
2142 eeprom->cbus_function[i] = 0;
2144 eeprom->high_current = 0;
2147 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2151 Frees allocated memory in eeprom.
2153 \param eeprom Pointer to ftdi_eeprom
2155 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2160 if (eeprom->manufacturer != 0) {
2161 free(eeprom->manufacturer);
2162 eeprom->manufacturer = 0;
2164 if (eeprom->product != 0) {
2165 free(eeprom->product);
2166 eeprom->product = 0;
2168 if (eeprom->serial != 0) {
2169 free(eeprom->serial);
2175 Build binary output from ftdi_eeprom structure.
2176 Output is suitable for ftdi_write_eeprom().
2178 \note This function doesn't handle FT2232x devices. Only FT232x.
2179 \param eeprom Pointer to ftdi_eeprom
2180 \param output Buffer of 128 bytes to store eeprom image to
2182 \retval >0: free eeprom size
2183 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2184 \retval -2: Invalid eeprom pointer
2185 \retval -3: Invalid cbus function setting
2186 \retval -4: Chip doesn't support invert
2187 \retval -5: Chip doesn't support high current drive
2189 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2192 unsigned short checksum, value;
2193 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2195 const int cbus_max[5] = {13, 13, 13, 13, 9};
2200 if (eeprom->manufacturer != NULL)
2201 manufacturer_size = strlen(eeprom->manufacturer);
2202 if (eeprom->product != NULL)
2203 product_size = strlen(eeprom->product);
2204 if (eeprom->serial != NULL)
2205 serial_size = strlen(eeprom->serial);
2207 // highest allowed cbus value
2208 for (i = 0; i < 5; i++)
2210 if ((eeprom->cbus_function[i] > cbus_max[i]) ||
2211 (eeprom->cbus_function[i] && eeprom->chip_type != TYPE_R)) return -3;
2213 if (eeprom->chip_type != TYPE_R)
2215 if (eeprom->invert) return -4;
2216 if (eeprom->high_current) return -5;
2219 size_check = eeprom->size;
2220 size_check -= 28; // 28 are always in use (fixed)
2222 // Top half of a 256byte eeprom is used just for strings and checksum
2223 // it seems that the FTDI chip will not read these strings from the lower half
2224 // Each string starts with two bytes; offset and type (0x03 for string)
2225 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2226 if (eeprom->size>=256) size_check = 120;
2227 size_check -= manufacturer_size*2;
2228 size_check -= product_size*2;
2229 size_check -= serial_size*2;
2231 // eeprom size exceeded?
2236 memset (output, 0, eeprom->size);
2238 // Addr 00: High current IO
2239 output[0x00] = eeprom->high_current ? HIGH_CURRENT_DRIVE : 0;
2240 // Addr 01: IN endpoint size (for R type devices, different for FT2232)
2241 if (eeprom->chip_type == TYPE_R) {
2242 output[0x01] = 0x40;
2244 // Addr 02: Vendor ID
2245 output[0x02] = eeprom->vendor_id;
2246 output[0x03] = eeprom->vendor_id >> 8;
2248 // Addr 04: Product ID
2249 output[0x04] = eeprom->product_id;
2250 output[0x05] = eeprom->product_id >> 8;
2252 // Addr 06: Device release number (0400h for BM features)
2253 output[0x06] = 0x00;
2254 switch (eeprom->chip_type) {
2256 output[0x07] = 0x02;
2259 output[0x07] = 0x04;
2262 output[0x07] = 0x05;
2265 output[0x07] = 0x06;
2268 output[0x07] = 0x00;
2271 // Addr 08: Config descriptor
2273 // Bit 6: 1 if this device is self powered, 0 if bus powered
2274 // Bit 5: 1 if this device uses remote wakeup
2275 // Bit 4: 1 if this device is battery powered
2277 if (eeprom->self_powered == 1)
2279 if (eeprom->remote_wakeup == 1)
2283 // Addr 09: Max power consumption: max power = value * 2 mA
2284 output[0x09] = eeprom->max_power;
2286 // Addr 0A: Chip configuration
2287 // Bit 7: 0 - reserved
2288 // Bit 6: 0 - reserved
2289 // Bit 5: 0 - reserved
2290 // Bit 4: 1 - Change USB version
2291 // Bit 3: 1 - Use the serial number string
2292 // Bit 2: 1 - Enable suspend pull downs for lower power
2293 // Bit 1: 1 - Out EndPoint is Isochronous
2294 // Bit 0: 1 - In EndPoint is Isochronous
2297 if (eeprom->in_is_isochronous == 1)
2299 if (eeprom->out_is_isochronous == 1)
2301 if (eeprom->suspend_pull_downs == 1)
2303 if (eeprom->use_serial == 1)
2305 if (eeprom->change_usb_version == 1)
2309 // Addr 0B: Invert data lines
2310 output[0x0B] = eeprom->invert & 0xff;
2312 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2313 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2314 if (eeprom->change_usb_version == 1)
2316 output[0x0C] = eeprom->usb_version;
2317 output[0x0D] = eeprom->usb_version >> 8;
2321 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2322 // Addr 0F: Length of manufacturer string
2323 output[0x0F] = manufacturer_size*2 + 2;
2325 // Addr 10: Offset of the product string + 0x80, calculated later
2326 // Addr 11: Length of product string
2327 output[0x11] = product_size*2 + 2;
2329 // Addr 12: Offset of the serial string + 0x80, calculated later
2330 // Addr 13: Length of serial string
2331 output[0x13] = serial_size*2 + 2;
2333 // Addr 14: CBUS function: CBUS0, CBUS1
2334 // Addr 15: CBUS function: CBUS2, CBUS3
2335 // Addr 16: CBUS function: CBUS5
2336 output[0x14] = eeprom->cbus_function[0] | (eeprom->cbus_function[1] << 4);
2337 output[0x15] = eeprom->cbus_function[2] | (eeprom->cbus_function[3] << 4);
2338 output[0x16] = eeprom->cbus_function[4];
2342 // In images produced by FTDI's FT_Prog for FT232R strings start at 0x18
2343 // Space till 0x18 should be considered as reserved.
2344 if (eeprom->chip_type >= TYPE_R) {
2349 if (eeprom->size >= 256) i = 0x80;
2352 // Output manufacturer
2353 output[0x0E] = i | 0x80; // calculate offset
2354 output[i++] = manufacturer_size*2 + 2;
2355 output[i++] = 0x03; // type: string
2356 for (j = 0; j < manufacturer_size; j++)
2358 output[i] = eeprom->manufacturer[j], i++;
2359 output[i] = 0x00, i++;
2362 // Output product name
2363 output[0x10] = i | 0x80; // calculate offset
2364 output[i] = product_size*2 + 2, i++;
2365 output[i] = 0x03, i++;
2366 for (j = 0; j < product_size; j++)
2368 output[i] = eeprom->product[j], i++;
2369 output[i] = 0x00, i++;
2373 output[0x12] = i | 0x80; // calculate offset
2374 output[i] = serial_size*2 + 2, i++;
2375 output[i] = 0x03, i++;
2376 for (j = 0; j < serial_size; j++)
2378 output[i] = eeprom->serial[j], i++;
2379 output[i] = 0x00, i++;
2382 // calculate checksum
2385 for (i = 0; i < eeprom->size/2-1; i++)
2387 value = output[i*2];
2388 value += output[(i*2)+1] << 8;
2390 checksum = value^checksum;
2391 checksum = (checksum << 1) | (checksum >> 15);
2394 output[eeprom->size-2] = checksum;
2395 output[eeprom->size-1] = checksum >> 8;
2401 Decode binary EEPROM image into an ftdi_eeprom structure.
2403 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2404 \param buf Buffer of \a size bytes of raw eeprom data
2405 \param size size size of eeprom data in bytes
2408 \retval -1: something went wrong
2410 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2411 FIXME: Strings are malloc'ed here and should be freed somewhere
2413 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2416 unsigned short checksum, eeprom_checksum, value;
2417 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2419 int eeprom_size = 128;
2424 size_check = eeprom->size;
2425 size_check -= 28; // 28 are always in use (fixed)
2427 // Top half of a 256byte eeprom is used just for strings and checksum
2428 // it seems that the FTDI chip will not read these strings from the lower half
2429 // Each string starts with two bytes; offset and type (0x03 for string)
2430 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2431 if (eeprom->size>=256)size_check = 120;
2432 size_check -= manufacturer_size*2;
2433 size_check -= product_size*2;
2434 size_check -= serial_size*2;
2436 // eeprom size exceeded?
2441 // empty eeprom struct
2442 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2444 // Addr 00: High current IO
2445 eeprom->high_current = (buf[0x02] & HIGH_CURRENT_DRIVE);
2447 // Addr 02: Vendor ID
2448 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2450 // Addr 04: Product ID
2451 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2453 value = buf[0x06] + (buf[0x07]<<8);
2457 eeprom->chip_type = TYPE_R;
2460 eeprom->chip_type = TYPE_BM;
2463 eeprom->chip_type = TYPE_AM;
2465 default: // Unknown device
2466 eeprom->chip_type = 0;
2470 // Addr 08: Config descriptor
2472 // Bit 6: 1 if this device is self powered, 0 if bus powered
2473 // Bit 5: 1 if this device uses remote wakeup
2474 // Bit 4: 1 if this device is battery powered
2476 if (j&0x40) eeprom->self_powered = 1;
2477 if (j&0x20) eeprom->remote_wakeup = 1;
2479 // Addr 09: Max power consumption: max power = value * 2 mA
2480 eeprom->max_power = buf[0x09];
2482 // Addr 0A: Chip configuration
2483 // Bit 7: 0 - reserved
2484 // Bit 6: 0 - reserved
2485 // Bit 5: 0 - reserved
2486 // Bit 4: 1 - Change USB version
2487 // Bit 3: 1 - Use the serial number string
2488 // Bit 2: 1 - Enable suspend pull downs for lower power
2489 // Bit 1: 1 - Out EndPoint is Isochronous
2490 // Bit 0: 1 - In EndPoint is Isochronous
2493 if (j&0x01) eeprom->in_is_isochronous = 1;
2494 if (j&0x02) eeprom->out_is_isochronous = 1;
2495 if (j&0x04) eeprom->suspend_pull_downs = 1;
2496 if (j&0x08) eeprom->use_serial = 1;
2497 if (j&0x10) eeprom->change_usb_version = 1;
2499 // Addr 0B: Invert data lines
2500 eeprom->invert = buf[0x0B];
2502 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2503 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2504 if (eeprom->change_usb_version == 1)
2506 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2509 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2510 // Addr 0F: Length of manufacturer string
2511 manufacturer_size = buf[0x0F]/2;
2512 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2513 else eeprom->manufacturer = NULL;
2515 // Addr 10: Offset of the product string + 0x80, calculated later
2516 // Addr 11: Length of product string
2517 product_size = buf[0x11]/2;
2518 if (product_size > 0) eeprom->product = malloc(product_size);
2519 else eeprom->product = NULL;
2521 // Addr 12: Offset of the serial string + 0x80, calculated later
2522 // Addr 13: Length of serial string
2523 serial_size = buf[0x13]/2;
2524 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2525 else eeprom->serial = NULL;
2527 // Addr 14: CBUS function: CBUS0, CBUS1
2528 // Addr 15: CBUS function: CBUS2, CBUS3
2529 // Addr 16: CBUS function: CBUS5
2530 if (eeprom->chip_type == TYPE_R) {
2531 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2532 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2533 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2534 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2535 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2537 for (j=0; j<5; j++) eeprom->cbus_function[j] = 0;
2540 // Decode manufacturer
2541 i = buf[0x0E] & 0x7f; // offset
2542 for (j=0;j<manufacturer_size-1;j++)
2544 eeprom->manufacturer[j] = buf[2*j+i+2];
2546 eeprom->manufacturer[j] = '\0';
2548 // Decode product name
2549 i = buf[0x10] & 0x7f; // offset
2550 for (j=0;j<product_size-1;j++)
2552 eeprom->product[j] = buf[2*j+i+2];
2554 eeprom->product[j] = '\0';
2557 i = buf[0x12] & 0x7f; // offset
2558 for (j=0;j<serial_size-1;j++)
2560 eeprom->serial[j] = buf[2*j+i+2];
2562 eeprom->serial[j] = '\0';
2567 for (i = 0; i < eeprom_size/2-1; i++)
2570 value += buf[(i*2)+1] << 8;
2572 checksum = value^checksum;
2573 checksum = (checksum << 1) | (checksum >> 15);
2576 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2578 if (eeprom_checksum != checksum)
2580 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2588 Read eeprom location
2590 \param ftdi pointer to ftdi_context
2591 \param eeprom_addr Address of eeprom location to be read
2592 \param eeprom_val Pointer to store read eeprom location
2595 \retval -1: read failed
2596 \retval -2: USB device unavailable
2598 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2600 if (ftdi == NULL || ftdi->usb_dev == NULL)
2601 ftdi_error_return(-2, "USB device unavailable");
2603 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)
2604 ftdi_error_return(-1, "reading eeprom failed");
2612 \param ftdi pointer to ftdi_context
2613 \param eeprom Pointer to store eeprom into
2616 \retval -1: read failed
2617 \retval -2: USB device unavailable
2619 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2623 if (ftdi == NULL || ftdi->usb_dev == NULL)
2624 ftdi_error_return(-2, "USB device unavailable");
2626 for (i = 0; i < ftdi->eeprom_size/2; i++)
2628 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)
2629 ftdi_error_return(-1, "reading eeprom failed");
2636 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2637 Function is only used internally
2640 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2642 return ((value & 1) << 1) |
2643 ((value & 2) << 5) |
2644 ((value & 4) >> 2) |
2645 ((value & 8) << 4) |
2646 ((value & 16) >> 1) |
2647 ((value & 32) >> 1) |
2648 ((value & 64) >> 4) |
2649 ((value & 128) >> 2);
2653 Read the FTDIChip-ID from R-type devices
2655 \param ftdi pointer to ftdi_context
2656 \param chipid Pointer to store FTDIChip-ID
2659 \retval -1: read failed
2660 \retval -2: USB device unavailable
2662 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2664 unsigned int a = 0, b = 0;
2666 if (ftdi == NULL || ftdi->usb_dev == NULL)
2667 ftdi_error_return(-2, "USB device unavailable");
2669 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)
2671 a = a << 8 | a >> 8;
2672 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)
2674 b = b << 8 | b >> 8;
2675 a = (a << 16) | (b & 0xFFFF);
2676 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2677 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2678 *chipid = a ^ 0xa5f0f7d1;
2683 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2687 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2688 Call this function then do a write then call again to see if size changes, if so write again.
2690 \param ftdi pointer to ftdi_context
2691 \param eeprom Pointer to store eeprom into
2692 \param maxsize the size of the buffer to read into
2694 \retval -1: eeprom read failed
2695 \retval -2: USB device unavailable
2696 \retval >=0: size of eeprom
2698 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2700 int i=0,j,minsize=32;
2703 if (ftdi == NULL || ftdi->usb_dev == NULL)
2704 ftdi_error_return(-2, "USB device unavailable");
2708 for (j = 0; i < maxsize/2 && j<size; j++)
2710 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2711 SIO_READ_EEPROM_REQUEST, 0, i,
2712 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2713 ftdi_error_return(-1, "eeprom read failed");
2718 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2724 Write eeprom location
2726 \param ftdi pointer to ftdi_context
2727 \param eeprom_addr Address of eeprom location to be written
2728 \param eeprom_val Value to be written
2731 \retval -1: read failed
2732 \retval -2: USB device unavailable
2734 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2736 if (ftdi == NULL || ftdi->usb_dev == NULL)
2737 ftdi_error_return(-2, "USB device unavailable");
2739 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2740 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2741 NULL, 0, ftdi->usb_write_timeout) != 0)
2742 ftdi_error_return(-1, "unable to write eeprom");
2750 \param ftdi pointer to ftdi_context
2751 \param eeprom Pointer to read eeprom from
2754 \retval -1: read failed
2755 \retval -2: USB device unavailable
2757 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2759 unsigned short usb_val, status;
2762 if (ftdi == NULL || ftdi->usb_dev == NULL)
2763 ftdi_error_return(-2, "USB device unavailable");
2765 /* These commands were traced while running MProg */
2766 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2768 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2770 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2773 for (i = 0; i < ftdi->eeprom_size/2; i++)
2775 usb_val = eeprom[i*2];
2776 usb_val += eeprom[(i*2)+1] << 8;
2777 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2778 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2779 NULL, 0, ftdi->usb_write_timeout) != 0)
2780 ftdi_error_return(-1, "unable to write eeprom");
2789 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2791 \param ftdi pointer to ftdi_context
2794 \retval -1: erase failed
2795 \retval -2: USB device unavailable
2797 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2799 if (ftdi == NULL || ftdi->usb_dev == NULL)
2800 ftdi_error_return(-2, "USB device unavailable");
2802 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
2803 ftdi_error_return(-1, "unable to erase eeprom");
2809 Get string representation for last error code
2811 \param ftdi pointer to ftdi_context
2813 \retval Pointer to error string
2815 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2820 return ftdi->error_str;
2823 /* @} end of doxygen libftdi group */