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 /* All fine. Now allocate the readbuffer */
127 return ftdi_read_data_set_chunksize(ftdi, 4096);
131 Allocate and initialize a new ftdi_context
133 \return a pointer to a new ftdi_context, or NULL on failure
135 struct ftdi_context *ftdi_new(void)
137 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
144 if (ftdi_init(ftdi) != 0)
154 Open selected channels on a chip, otherwise use first channel.
156 \param ftdi pointer to ftdi_context
157 \param interface Interface to use for FT2232C/2232H/4232H chips.
160 \retval -1: unknown interface
161 \retval -2: USB device unavailable
163 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
166 ftdi_error_return(-2, "USB device unavailable");
172 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
176 ftdi->index = INTERFACE_B;
182 ftdi->index = INTERFACE_C;
188 ftdi->index = INTERFACE_D;
193 ftdi_error_return(-1, "Unknown interface");
199 Deinitializes a ftdi_context.
201 \param ftdi pointer to ftdi_context
203 void ftdi_deinit(struct ftdi_context *ftdi)
208 ftdi_usb_close_internal (ftdi);
210 if (ftdi->async_usb_buffer != NULL)
212 free(ftdi->async_usb_buffer);
213 ftdi->async_usb_buffer = NULL;
216 if (ftdi->readbuffer != NULL)
218 free(ftdi->readbuffer);
219 ftdi->readbuffer = NULL;
224 Deinitialize and free an ftdi_context.
226 \param ftdi pointer to ftdi_context
228 void ftdi_free(struct ftdi_context *ftdi)
235 Use an already open libusb device.
237 \param ftdi pointer to ftdi_context
238 \param usb libusb usb_dev_handle to use
240 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
250 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
251 needs to be deallocated by ftdi_list_free() after use.
253 \param ftdi pointer to ftdi_context
254 \param devlist Pointer where to store list of found devices
255 \param vendor Vendor ID to search for
256 \param product Product ID to search for
258 \retval >0: number of devices found
259 \retval -1: usb_find_busses() failed
260 \retval -2: usb_find_devices() failed
261 \retval -3: out of memory
263 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
265 struct ftdi_device_list **curdev;
267 struct usb_device *dev;
271 if (usb_find_busses() < 0)
272 ftdi_error_return(-1, "usb_find_busses() failed");
273 if (usb_find_devices() < 0)
274 ftdi_error_return(-2, "usb_find_devices() failed");
278 for (bus = usb_get_busses(); bus; bus = bus->next)
280 for (dev = bus->devices; dev; dev = dev->next)
282 if (dev->descriptor.idVendor == vendor
283 && dev->descriptor.idProduct == product)
285 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
287 ftdi_error_return(-3, "out of memory");
289 (*curdev)->next = NULL;
290 (*curdev)->dev = dev;
292 curdev = &(*curdev)->next;
302 Frees a usb device list.
304 \param devlist USB device list created by ftdi_usb_find_all()
306 void ftdi_list_free(struct ftdi_device_list **devlist)
308 struct ftdi_device_list *curdev, *next;
310 for (curdev = *devlist; curdev != NULL;)
321 Frees a usb device list.
323 \param devlist USB device list created by ftdi_usb_find_all()
325 void ftdi_list_free2(struct ftdi_device_list *devlist)
327 ftdi_list_free(&devlist);
331 Return device ID strings from the usb device.
333 The parameters manufacturer, description and serial may be NULL
334 or pointer to buffers to store the fetched strings.
336 \note Use this function only in combination with ftdi_usb_find_all()
337 as it closes the internal "usb_dev" after use.
339 \param ftdi pointer to ftdi_context
340 \param dev libusb usb_dev to use
341 \param manufacturer Store manufacturer string here if not NULL
342 \param mnf_len Buffer size of manufacturer string
343 \param description Store product description string here if not NULL
344 \param desc_len Buffer size of product description string
345 \param serial Store serial string here if not NULL
346 \param serial_len Buffer size of serial string
349 \retval -1: wrong arguments
350 \retval -4: unable to open device
351 \retval -7: get product manufacturer failed
352 \retval -8: get product description failed
353 \retval -9: get serial number failed
354 \retval -10: unable to close device
356 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
357 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
359 if ((ftdi==NULL) || (dev==NULL))
362 if (!(ftdi->usb_dev = usb_open(dev)))
363 ftdi_error_return(-4, usb_strerror());
365 if (manufacturer != NULL)
367 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
369 ftdi_usb_close_internal (ftdi);
370 ftdi_error_return(-7, usb_strerror());
374 if (description != NULL)
376 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
378 ftdi_usb_close_internal (ftdi);
379 ftdi_error_return(-8, usb_strerror());
385 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
387 ftdi_usb_close_internal (ftdi);
388 ftdi_error_return(-9, usb_strerror());
392 if (ftdi_usb_close_internal (ftdi) != 0)
393 ftdi_error_return(-10, usb_strerror());
399 * Internal function to determine the maximum packet size.
400 * \param ftdi pointer to ftdi_context
401 * \param dev libusb usb_dev to use
402 * \retval Maximum packet size for this device
404 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
406 unsigned int packet_size;
409 if (ftdi == NULL || dev == NULL)
412 // Determine maximum packet size. Init with default value.
413 // New hi-speed devices from FTDI use a packet size of 512 bytes
414 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
415 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
420 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
422 struct usb_config_descriptor config = dev->config[0];
424 if (ftdi->interface < config.bNumInterfaces)
426 struct usb_interface interface = config.interface[ftdi->interface];
427 if (interface.num_altsetting > 0)
429 struct usb_interface_descriptor descriptor = interface.altsetting[0];
430 if (descriptor.bNumEndpoints > 0)
432 packet_size = descriptor.endpoint[0].wMaxPacketSize;
442 Opens a ftdi device given by an usb_device.
444 \param ftdi pointer to ftdi_context
445 \param dev libusb usb_dev to use
448 \retval -3: unable to config device
449 \retval -4: unable to open device
450 \retval -5: unable to claim device
451 \retval -6: reset failed
452 \retval -7: set baudrate failed
453 \retval -8: ftdi context invalid
455 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
457 int detach_errno = 0;
461 ftdi_error_return(-8, "ftdi context invalid");
463 if (!(ftdi->usb_dev = usb_open(dev)))
464 ftdi_error_return(-4, "usb_open() failed");
466 #ifdef LIBUSB_HAS_GET_DRIVER_NP
467 // Try to detach ftdi_sio kernel module.
468 // Returns ENODATA if driver is not loaded.
470 // The return code is kept in a separate variable and only parsed
471 // if usb_set_configuration() or usb_claim_interface() fails as the
472 // detach operation might be denied and everything still works fine.
473 // Likely scenario is a static ftdi_sio kernel module.
474 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
475 detach_errno = errno;
479 // set configuration (needed especially for windows)
480 // tolerate EBUSY: one device with one configuration, but two interfaces
481 // and libftdi sessions to both interfaces (e.g. FT2232)
483 if (dev->descriptor.bNumConfigurations > 0)
485 // libusb-win32 on Windows 64 can return a null pointer for a valid device
487 config_val = dev->config[0].bConfigurationValue;
489 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
492 ftdi_usb_close_internal (ftdi);
493 if (detach_errno == EPERM)
495 ftdi_error_return(-8, "inappropriate permissions on device!");
499 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
505 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
507 ftdi_usb_close_internal (ftdi);
508 if (detach_errno == EPERM)
510 ftdi_error_return(-8, "inappropriate permissions on device!");
514 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
518 if (ftdi_usb_reset (ftdi) != 0)
520 ftdi_usb_close_internal (ftdi);
521 ftdi_error_return(-6, "ftdi_usb_reset failed");
524 // Try to guess chip type
525 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
526 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
527 && dev->descriptor.iSerialNumber == 0))
528 ftdi->type = TYPE_BM;
529 else if (dev->descriptor.bcdDevice == 0x200)
530 ftdi->type = TYPE_AM;
531 else if (dev->descriptor.bcdDevice == 0x500)
532 ftdi->type = TYPE_2232C;
533 else if (dev->descriptor.bcdDevice == 0x600)
535 else if (dev->descriptor.bcdDevice == 0x700)
536 ftdi->type = TYPE_2232H;
537 else if (dev->descriptor.bcdDevice == 0x800)
538 ftdi->type = TYPE_4232H;
540 // Set default interface on dual/quad type chips
547 ftdi->index = INTERFACE_A;
553 // Determine maximum packet size
554 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
556 if (ftdi_set_baudrate (ftdi, 9600) != 0)
558 ftdi_usb_close_internal (ftdi);
559 ftdi_error_return(-7, "set baudrate failed");
562 ftdi_error_return(0, "all fine");
566 Opens the first device with a given vendor and product ids.
568 \param ftdi pointer to ftdi_context
569 \param vendor Vendor ID
570 \param product Product ID
572 \retval same as ftdi_usb_open_desc()
574 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
576 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
580 Opens the first device with a given, vendor id, product id,
581 description and serial.
583 \param ftdi pointer to ftdi_context
584 \param vendor Vendor ID
585 \param product Product ID
586 \param description Description to search for. Use NULL if not needed.
587 \param serial Serial to search for. Use NULL if not needed.
590 \retval -1: usb_find_busses() failed
591 \retval -2: usb_find_devices() failed
592 \retval -3: usb device not found
593 \retval -4: unable to open device
594 \retval -5: unable to claim device
595 \retval -6: reset failed
596 \retval -7: set baudrate failed
597 \retval -8: get product description failed
598 \retval -9: get serial number failed
599 \retval -10: unable to close device
601 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
602 const char* description, const char* serial)
604 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
608 Opens the index-th device with a given, vendor id, product id,
609 description and serial.
611 \param ftdi pointer to ftdi_context
612 \param vendor Vendor ID
613 \param product Product ID
614 \param description Description to search for. Use NULL if not needed.
615 \param serial Serial to search for. Use NULL if not needed.
616 \param index Number of matching device to open if there are more than one, starts with 0.
619 \retval -1: usb_find_busses() failed
620 \retval -2: usb_find_devices() failed
621 \retval -3: usb device not found
622 \retval -4: unable to open device
623 \retval -5: unable to claim device
624 \retval -6: reset failed
625 \retval -7: set baudrate failed
626 \retval -8: get product description failed
627 \retval -9: get serial number failed
628 \retval -10: unable to close device
629 \retval -11: ftdi context invalid
631 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
632 const char* description, const char* serial, unsigned int index)
635 struct usb_device *dev;
640 if (usb_find_busses() < 0)
641 ftdi_error_return(-1, "usb_find_busses() failed");
642 if (usb_find_devices() < 0)
643 ftdi_error_return(-2, "usb_find_devices() failed");
646 ftdi_error_return(-11, "ftdi context invalid");
648 for (bus = usb_get_busses(); bus; bus = bus->next)
650 for (dev = bus->devices; dev; dev = dev->next)
652 if (dev->descriptor.idVendor == vendor
653 && dev->descriptor.idProduct == product)
655 if (!(ftdi->usb_dev = usb_open(dev)))
656 ftdi_error_return(-4, "usb_open() failed");
658 if (description != NULL)
660 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
662 ftdi_usb_close_internal (ftdi);
663 ftdi_error_return(-8, "unable to fetch product description");
665 if (strncmp(string, description, sizeof(string)) != 0)
667 if (ftdi_usb_close_internal (ftdi) != 0)
668 ftdi_error_return(-10, "unable to close device");
674 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
676 ftdi_usb_close_internal (ftdi);
677 ftdi_error_return(-9, "unable to fetch serial number");
679 if (strncmp(string, serial, sizeof(string)) != 0)
681 if (ftdi_usb_close_internal (ftdi) != 0)
682 ftdi_error_return(-10, "unable to close device");
687 if (ftdi_usb_close_internal (ftdi) != 0)
688 ftdi_error_return(-10, "unable to close device");
696 return ftdi_usb_open_dev(ftdi, dev);
702 ftdi_error_return(-3, "device not found");
706 Opens the ftdi-device described by a description-string.
707 Intended to be used for parsing a device-description given as commandline argument.
709 \param ftdi pointer to ftdi_context
710 \param description NULL-terminated description-string, using this format:
711 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
712 \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")
713 \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
714 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
716 \note The description format may be extended in later versions.
719 \retval -1: usb_find_busses() failed
720 \retval -2: usb_find_devices() failed
721 \retval -3: usb device not found
722 \retval -4: unable to open device
723 \retval -5: unable to claim device
724 \retval -6: reset failed
725 \retval -7: set baudrate failed
726 \retval -8: get product description failed
727 \retval -9: get serial number failed
728 \retval -10: unable to close device
729 \retval -11: illegal description format
730 \retval -12: ftdi context invalid
732 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
735 ftdi_error_return(-12, "ftdi context invalid");
737 if (description[0] == 0 || description[1] != ':')
738 ftdi_error_return(-11, "illegal description format");
740 if (description[0] == 'd')
743 struct usb_device *dev;
747 if (usb_find_busses() < 0)
748 ftdi_error_return(-1, "usb_find_busses() failed");
749 if (usb_find_devices() < 0)
750 ftdi_error_return(-2, "usb_find_devices() failed");
752 for (bus = usb_get_busses(); bus; bus = bus->next)
754 for (dev = bus->devices; dev; dev = dev->next)
756 /* XXX: This doesn't handle symlinks/odd paths/etc... */
757 const char *desc = description + 2;
758 size_t len = strlen(bus->dirname);
759 if (strncmp(desc, bus->dirname, len))
765 if (strcmp(desc, dev->filename))
767 return ftdi_usb_open_dev(ftdi, dev);
772 ftdi_error_return(-3, "device not found");
774 else if (description[0] == 'i' || description[0] == 's')
777 unsigned int product;
778 unsigned int index=0;
779 const char *serial=NULL;
780 const char *startp, *endp;
783 startp=description+2;
784 vendor=strtoul((char*)startp,(char**)&endp,0);
785 if (*endp != ':' || endp == startp || errno != 0)
786 ftdi_error_return(-11, "illegal description format");
789 product=strtoul((char*)startp,(char**)&endp,0);
790 if (endp == startp || errno != 0)
791 ftdi_error_return(-11, "illegal description format");
793 if (description[0] == 'i' && *endp != 0)
795 /* optional index field in i-mode */
797 ftdi_error_return(-11, "illegal description format");
800 index=strtoul((char*)startp,(char**)&endp,0);
801 if (*endp != 0 || endp == startp || errno != 0)
802 ftdi_error_return(-11, "illegal description format");
804 if (description[0] == 's')
807 ftdi_error_return(-11, "illegal description format");
809 /* rest of the description is the serial */
813 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
817 ftdi_error_return(-11, "illegal description format");
822 Resets the ftdi device.
824 \param ftdi pointer to ftdi_context
827 \retval -1: FTDI reset failed
828 \retval -2: USB device unavailable
830 int ftdi_usb_reset(struct ftdi_context *ftdi)
832 if (ftdi == NULL || ftdi->usb_dev == NULL)
833 ftdi_error_return(-2, "USB device unavailable");
835 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
836 SIO_RESET_REQUEST, SIO_RESET_SIO,
837 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
838 ftdi_error_return(-1,"FTDI reset failed");
840 // Invalidate data in the readbuffer
841 ftdi->readbuffer_offset = 0;
842 ftdi->readbuffer_remaining = 0;
848 Clears the read buffer on the chip and the internal read buffer.
850 \param ftdi pointer to ftdi_context
853 \retval -1: read buffer purge failed
854 \retval -2: USB device unavailable
856 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
858 if (ftdi == NULL || ftdi->usb_dev == NULL)
859 ftdi_error_return(-2, "USB device unavailable");
861 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
862 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
863 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
864 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
866 // Invalidate data in the readbuffer
867 ftdi->readbuffer_offset = 0;
868 ftdi->readbuffer_remaining = 0;
874 Clears the write buffer on the chip.
876 \param ftdi pointer to ftdi_context
879 \retval -1: write buffer purge failed
880 \retval -2: USB device unavailable
882 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
884 if (ftdi == NULL || ftdi->usb_dev == NULL)
885 ftdi_error_return(-2, "USB device unavailable");
887 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
888 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
889 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
890 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
896 Clears the buffers on the chip and the internal read buffer.
898 \param ftdi pointer to ftdi_context
901 \retval -1: read buffer purge failed
902 \retval -2: write buffer purge failed
903 \retval -3: USB device unavailable
905 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
909 if (ftdi == NULL || ftdi->usb_dev == NULL)
910 ftdi_error_return(-3, "USB device unavailable");
912 result = ftdi_usb_purge_rx_buffer(ftdi);
916 result = ftdi_usb_purge_tx_buffer(ftdi);
926 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
928 \param ftdi pointer to ftdi_context
931 \retval -1: usb_release failed
932 \retval -2: usb_close failed
933 \retval -3: ftdi context invalid
935 int ftdi_usb_close(struct ftdi_context *ftdi)
940 ftdi_error_return(-3, "ftdi context invalid");
942 #ifdef LIBFTDI_LINUX_ASYNC_MODE
943 /* try to release some kernel resources */
944 ftdi_async_complete(ftdi,1);
947 if (ftdi->usb_dev != NULL)
948 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
951 if (ftdi_usb_close_internal (ftdi) != 0)
958 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
959 Function is only used internally
962 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
963 unsigned short *value, unsigned short *index)
965 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
966 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
967 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
968 int divisor, best_divisor, best_baud, best_baud_diff;
969 unsigned long encoded_divisor;
978 divisor = 24000000 / baudrate;
980 if (ftdi->type == TYPE_AM)
982 // Round down to supported fraction (AM only)
983 divisor -= am_adjust_dn[divisor & 7];
986 // Try this divisor and the one above it (because division rounds down)
990 for (i = 0; i < 2; i++)
992 int try_divisor = divisor + i;
996 // Round up to supported divisor value
997 if (try_divisor <= 8)
999 // Round up to minimum supported divisor
1002 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1004 // BM doesn't support divisors 9 through 11 inclusive
1007 else if (divisor < 16)
1009 // AM doesn't support divisors 9 through 15 inclusive
1014 if (ftdi->type == TYPE_AM)
1016 // Round up to supported fraction (AM only)
1017 try_divisor += am_adjust_up[try_divisor & 7];
1018 if (try_divisor > 0x1FFF8)
1020 // Round down to maximum supported divisor value (for AM)
1021 try_divisor = 0x1FFF8;
1026 if (try_divisor > 0x1FFFF)
1028 // Round down to maximum supported divisor value (for BM)
1029 try_divisor = 0x1FFFF;
1033 // Get estimated baud rate (to nearest integer)
1034 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1035 // Get absolute difference from requested baud rate
1036 if (baud_estimate < baudrate)
1038 baud_diff = baudrate - baud_estimate;
1042 baud_diff = baud_estimate - baudrate;
1044 if (i == 0 || baud_diff < best_baud_diff)
1046 // Closest to requested baud rate so far
1047 best_divisor = try_divisor;
1048 best_baud = baud_estimate;
1049 best_baud_diff = baud_diff;
1052 // Spot on! No point trying
1057 // Encode the best divisor value
1058 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1059 // Deal with special cases for encoded value
1060 if (encoded_divisor == 1)
1062 encoded_divisor = 0; // 3000000 baud
1064 else if (encoded_divisor == 0x4001)
1066 encoded_divisor = 1; // 2000000 baud (BM only)
1068 // Split into "value" and "index" values
1069 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1070 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1072 *index = (unsigned short)(encoded_divisor >> 8);
1074 *index |= ftdi->index;
1077 *index = (unsigned short)(encoded_divisor >> 16);
1079 // Return the nearest baud rate
1084 Sets the chip baud rate
1086 \param ftdi pointer to ftdi_context
1087 \param baudrate baud rate to set
1090 \retval -1: invalid baudrate
1091 \retval -2: setting baudrate failed
1092 \retval -3: USB device unavailable
1094 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1096 unsigned short value, index;
1097 int actual_baudrate;
1099 if (ftdi == NULL || ftdi->usb_dev == NULL)
1100 ftdi_error_return(-3, "USB device unavailable");
1102 if (ftdi->bitbang_enabled)
1104 baudrate = baudrate*4;
1107 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1108 if (actual_baudrate <= 0)
1109 ftdi_error_return (-1, "Silly baudrate <= 0.");
1111 // Check within tolerance (about 5%)
1112 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1113 || ((actual_baudrate < baudrate)
1114 ? (actual_baudrate * 21 < baudrate * 20)
1115 : (baudrate * 21 < actual_baudrate * 20)))
1116 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1118 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1119 SIO_SET_BAUDRATE_REQUEST, value,
1120 index, NULL, 0, ftdi->usb_write_timeout) != 0)
1121 ftdi_error_return (-2, "Setting new baudrate failed");
1123 ftdi->baudrate = baudrate;
1128 Set (RS232) line characteristics.
1129 The break type can only be set via ftdi_set_line_property2()
1130 and defaults to "off".
1132 \param ftdi pointer to ftdi_context
1133 \param bits Number of bits
1134 \param sbit Number of stop bits
1135 \param parity Parity mode
1138 \retval -1: Setting line property failed
1140 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1141 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1143 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1147 Set (RS232) line characteristics
1149 \param ftdi pointer to ftdi_context
1150 \param bits Number of bits
1151 \param sbit Number of stop bits
1152 \param parity Parity mode
1153 \param break_type Break type
1156 \retval -1: Setting line property failed
1157 \retval -2: USB device unavailable
1159 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1160 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1161 enum ftdi_break_type break_type)
1163 unsigned short value = bits;
1165 if (ftdi == NULL || ftdi->usb_dev == NULL)
1166 ftdi_error_return(-2, "USB device unavailable");
1171 value |= (0x00 << 8);
1174 value |= (0x01 << 8);
1177 value |= (0x02 << 8);
1180 value |= (0x03 << 8);
1183 value |= (0x04 << 8);
1190 value |= (0x00 << 11);
1193 value |= (0x01 << 11);
1196 value |= (0x02 << 11);
1203 value |= (0x00 << 14);
1206 value |= (0x01 << 14);
1210 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1211 SIO_SET_DATA_REQUEST, value,
1212 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1213 ftdi_error_return (-1, "Setting new line property failed");
1219 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1221 \param ftdi pointer to ftdi_context
1222 \param buf Buffer with the data
1223 \param size Size of the buffer
1225 \retval -666: USB device unavailable
1226 \retval <0: error code from usb_bulk_write()
1227 \retval >0: number of bytes written
1229 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1233 int total_written = 0;
1235 if (ftdi == NULL || ftdi->usb_dev == NULL)
1236 ftdi_error_return(-666, "USB device unavailable");
1238 while (offset < size)
1240 int write_size = ftdi->writebuffer_chunksize;
1242 if (offset+write_size > size)
1243 write_size = size-offset;
1245 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
1247 ftdi_error_return(ret, "usb bulk write failed");
1249 total_written += ret;
1250 offset += write_size;
1253 return total_written;
1256 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1257 #ifdef USB_CLASS_PTP
1258 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1260 /* this is strongly dependent on libusb using the same struct layout. If libusb
1261 changes in some later version this may break horribly (this is for libusb 0.1.12) */
1262 struct usb_dev_handle
1265 // some other stuff coming here we don't need
1269 Check for pending async urbs
1272 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
1274 struct usbdevfs_urb *urb;
1278 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1280 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1281 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1289 Wait until one or more async URBs are completed by the kernel and mark their
1290 positions in the async-buffer as unused
1292 \param ftdi pointer to ftdi_context
1293 \param wait_for_more if != 0 wait for more than one write to complete
1294 \param timeout_msec max milliseconds to wait
1298 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
1301 struct usbdevfs_urb *urb;
1307 FD_SET(ftdi->usb_dev->fd, &writefds);
1309 /* init timeout only once, select writes time left after call */
1310 tv.tv_sec = timeout_msec / 1000;
1311 tv.tv_usec = (timeout_msec % 1000) * 1000;
1318 while (_usb_get_async_urbs_pending(ftdi)
1319 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1322 if (keep_going && !wait_for_more)
1324 /* don't wait if repeating only for keep_going */
1329 /* wait for timeout msec or something written ready */
1330 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1333 if (ret == 0 && urb != NULL)
1335 /* got a free urb, mark it */
1336 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
1338 /* try to get more urbs that are ready now, but don't wait anymore */
1343 /* no more urbs waiting */
1351 Wait until one or more async URBs are completed by the kernel and mark their
1352 positions in the async-buffer as unused.
1354 \param ftdi pointer to ftdi_context
1355 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1357 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1359 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
1363 Stupid libusb does not offer async writes nor does it allow
1364 access to its fd - so we need some hacks here.
1367 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
1369 struct usbdevfs_urb *urb;
1370 int bytesdone = 0, requested;
1371 int ret, cleanup_count;
1376 /* find a free urb buffer we can use */
1379 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1381 if (i==ftdi->async_usb_buffer_size)
1383 /* wait until some buffers are free */
1384 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1387 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1389 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1390 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1391 break; /* found a free urb position */
1396 /* no free urb position found */
1400 requested = size - bytesdone;
1401 if (requested > 4096)
1404 memset(urb,0,sizeof(urb));
1406 urb->type = USBDEVFS_URB_TYPE_BULK;
1409 urb->buffer = bytes + bytesdone;
1410 urb->buffer_length = requested;
1412 urb->actual_length = 0;
1413 urb->number_of_packets = 0;
1414 urb->usercontext = 0;
1418 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1420 while (ret < 0 && errno == EINTR);
1422 return ret; /* the caller can read errno to get more info */
1424 bytesdone += requested;
1426 while (bytesdone < size);
1431 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1432 Does not wait for completion of the transfer nor does it make sure that
1433 the transfer was successful.
1435 This function could be extended to use signals and callbacks to inform the
1436 caller of completion or error - but this is not done yet, volunteers welcome.
1438 Works around libusb and directly accesses functions only available on Linux.
1439 Only available if compiled with --with-async-mode.
1441 \param ftdi pointer to ftdi_context
1442 \param buf Buffer with the data
1443 \param size Size of the buffer
1445 \retval -666: USB device unavailable
1446 \retval <0: error code from usb_bulk_write()
1447 \retval >0: number of bytes written
1449 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1453 int total_written = 0;
1455 if (ftdi == NULL || ftdi->usb_dev == NULL)
1456 ftdi_error_return(-666, "USB device unavailable");
1458 while (offset < size)
1460 int write_size = ftdi->writebuffer_chunksize;
1462 if (offset+write_size > size)
1463 write_size = size-offset;
1465 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1467 ftdi_error_return(ret, "usb bulk write async failed");
1469 total_written += ret;
1470 offset += write_size;
1473 return total_written;
1475 #endif // LIBFTDI_LINUX_ASYNC_MODE
1478 Configure write buffer chunk size.
1481 \param ftdi pointer to ftdi_context
1482 \param chunksize Chunk size
1485 \retval -1: ftdi context invalid
1487 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1490 ftdi_error_return(-1, "ftdi context invalid");
1492 ftdi->writebuffer_chunksize = chunksize;
1497 Get write buffer chunk size.
1499 \param ftdi pointer to ftdi_context
1500 \param chunksize Pointer to store chunk size in
1503 \retval -1: ftdi context invalid
1505 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1508 ftdi_error_return(-1, "ftdi context invalid");
1510 *chunksize = ftdi->writebuffer_chunksize;
1515 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1517 Returns when at least one byte is available or when the latency timer has elapsed
1518 Automatically strips the two modem status bytes transfered during every read.
1520 \param ftdi pointer to ftdi_context
1521 \param buf Buffer to store data in
1522 \param size Size of the buffer
1524 \retval -666: USB device unavailable
1525 \retval <0: error code from usb_bulk_read()
1526 \retval 0: no data was available
1527 \retval >0: number of bytes read
1530 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1532 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1535 if (ftdi == NULL || ftdi->usb_dev == NULL)
1536 ftdi_error_return(-666, "USB device unavailable");
1538 packet_size = ftdi->max_packet_size;
1539 // Packet size sanity check (avoid division by zero)
1540 if (packet_size == 0)
1541 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1543 // everything we want is still in the readbuffer?
1544 if (size <= ftdi->readbuffer_remaining)
1546 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1549 ftdi->readbuffer_remaining -= size;
1550 ftdi->readbuffer_offset += size;
1552 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1556 // something still in the readbuffer, but not enough to satisfy 'size'?
1557 if (ftdi->readbuffer_remaining != 0)
1559 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1562 offset += ftdi->readbuffer_remaining;
1564 // do the actual USB read
1565 while (offset < size && ret > 0)
1567 ftdi->readbuffer_remaining = 0;
1568 ftdi->readbuffer_offset = 0;
1569 /* returns how much received */
1570 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1572 ftdi_error_return(ret, "usb bulk read failed");
1576 // skip FTDI status bytes.
1577 // Maybe stored in the future to enable modem use
1578 num_of_chunks = ret / packet_size;
1579 chunk_remains = ret % packet_size;
1580 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1582 ftdi->readbuffer_offset += 2;
1585 if (ret > packet_size - 2)
1587 for (i = 1; i < num_of_chunks; i++)
1588 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1589 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1591 if (chunk_remains > 2)
1593 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1594 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1596 ret -= 2*num_of_chunks;
1599 ret -= 2*(num_of_chunks-1)+chunk_remains;
1604 // no more data to read?
1609 // data still fits in buf?
1610 if (offset+ret <= size)
1612 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1613 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1616 /* Did we read exactly the right amount of bytes? */
1618 //printf("read_data exact rem %d offset %d\n",
1619 //ftdi->readbuffer_remaining, offset);
1624 // only copy part of the data or size <= readbuffer_chunksize
1625 int part_size = size-offset;
1626 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1628 ftdi->readbuffer_offset += part_size;
1629 ftdi->readbuffer_remaining = ret-part_size;
1630 offset += part_size;
1632 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1633 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1644 Configure read buffer chunk size.
1647 Automatically reallocates the buffer.
1649 \param ftdi pointer to ftdi_context
1650 \param chunksize Chunk size
1653 \retval -1: ftdi context invalid
1655 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1657 unsigned char *new_buf;
1660 ftdi_error_return(-1, "ftdi context invalid");
1662 // Invalidate all remaining data
1663 ftdi->readbuffer_offset = 0;
1664 ftdi->readbuffer_remaining = 0;
1666 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1667 ftdi_error_return(-1, "out of memory for readbuffer");
1669 ftdi->readbuffer = new_buf;
1670 ftdi->readbuffer_chunksize = chunksize;
1676 Get read buffer chunk size.
1678 \param ftdi pointer to ftdi_context
1679 \param chunksize Pointer to store chunk size in
1682 \retval -1: FTDI context invalid
1684 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1687 ftdi_error_return(-1, "FTDI context invalid");
1689 *chunksize = ftdi->readbuffer_chunksize;
1695 Enable bitbang mode.
1697 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1699 \param ftdi pointer to ftdi_context
1700 \param bitmask Bitmask to configure lines.
1701 HIGH/ON value configures a line as output.
1704 \retval -1: can't enable bitbang mode
1705 \retval -2: USB device unavailable
1707 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1709 unsigned short usb_val;
1711 if (ftdi == NULL || ftdi->usb_dev == NULL)
1712 ftdi_error_return(-2, "USB device unavailable");
1714 usb_val = bitmask; // low byte: bitmask
1715 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1716 usb_val |= (ftdi->bitbang_mode << 8);
1718 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1719 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1720 NULL, 0, ftdi->usb_write_timeout) != 0)
1721 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1723 ftdi->bitbang_enabled = 1;
1728 Disable bitbang mode.
1730 \param ftdi pointer to ftdi_context
1733 \retval -1: can't disable bitbang mode
1734 \retval -2: USB device unavailable
1736 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1738 if (ftdi == NULL || ftdi->usb_dev == NULL)
1739 ftdi_error_return(-2, "USB device unavailable");
1741 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)
1742 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1744 ftdi->bitbang_enabled = 0;
1749 Enable/disable bitbang modes.
1751 \param ftdi pointer to ftdi_context
1752 \param bitmask Bitmask to configure lines.
1753 HIGH/ON value configures a line as output.
1754 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1757 \retval -1: can't enable bitbang mode
1758 \retval -2: USB device unavailable
1760 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1762 unsigned short usb_val;
1764 if (ftdi == NULL || ftdi->usb_dev == NULL)
1765 ftdi_error_return(-2, "USB device unavailable");
1767 usb_val = bitmask; // low byte: bitmask
1768 usb_val |= (mode << 8);
1769 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)
1770 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1772 ftdi->bitbang_mode = mode;
1773 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1778 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1780 \param ftdi pointer to ftdi_context
1781 \param pins Pointer to store pins into
1784 \retval -1: read pins failed
1785 \retval -2: USB device unavailable
1787 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1789 if (ftdi == NULL || ftdi->usb_dev == NULL)
1790 ftdi_error_return(-2, "USB device unavailable");
1792 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)
1793 ftdi_error_return(-1, "read pins failed");
1801 The FTDI chip keeps data in the internal buffer for a specific
1802 amount of time if the buffer is not full yet to decrease
1803 load on the usb bus.
1805 \param ftdi pointer to ftdi_context
1806 \param latency Value between 1 and 255
1809 \retval -1: latency out of range
1810 \retval -2: unable to set latency timer
1811 \retval -3: USB device unavailable
1813 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1815 unsigned short usb_val;
1818 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1820 if (ftdi == NULL || ftdi->usb_dev == NULL)
1821 ftdi_error_return(-3, "USB device unavailable");
1824 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)
1825 ftdi_error_return(-2, "unable to set latency timer");
1833 \param ftdi pointer to ftdi_context
1834 \param latency Pointer to store latency value in
1837 \retval -1: unable to get latency timer
1838 \retval -2: USB device unavailable
1840 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1842 unsigned short usb_val;
1844 if (ftdi == NULL || ftdi->usb_dev == NULL)
1845 ftdi_error_return(-2, "USB device unavailable");
1847 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)
1848 ftdi_error_return(-1, "reading latency timer failed");
1850 *latency = (unsigned char)usb_val;
1855 Poll modem status information
1857 This function allows the retrieve the two status bytes of the device.
1858 The device sends these bytes also as a header for each read access
1859 where they are discarded by ftdi_read_data(). The chip generates
1860 the two stripped status bytes in the absence of data every 40 ms.
1862 Layout of the first byte:
1863 - B0..B3 - must be 0
1864 - B4 Clear to send (CTS)
1867 - B5 Data set ready (DTS)
1870 - B6 Ring indicator (RI)
1873 - B7 Receive line signal detect (RLSD)
1877 Layout of the second byte:
1878 - B0 Data ready (DR)
1879 - B1 Overrun error (OE)
1880 - B2 Parity error (PE)
1881 - B3 Framing error (FE)
1882 - B4 Break interrupt (BI)
1883 - B5 Transmitter holding register (THRE)
1884 - B6 Transmitter empty (TEMT)
1885 - B7 Error in RCVR FIFO
1887 \param ftdi pointer to ftdi_context
1888 \param status Pointer to store status information in. Must be two bytes.
1891 \retval -1: unable to retrieve status information
1892 \retval -2: USB device unavailable
1894 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1898 if (ftdi == NULL || ftdi->usb_dev == NULL)
1899 ftdi_error_return(-2, "USB device unavailable");
1901 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)
1902 ftdi_error_return(-1, "getting modem status failed");
1904 *status = (usb_val[1] << 8) | usb_val[0];
1910 Set flowcontrol for ftdi chip
1912 \param ftdi pointer to ftdi_context
1913 \param flowctrl flow control to use. should be
1914 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1917 \retval -1: set flow control failed
1918 \retval -2: USB device unavailable
1920 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1922 if (ftdi == NULL || ftdi->usb_dev == NULL)
1923 ftdi_error_return(-2, "USB device unavailable");
1925 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1926 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1927 NULL, 0, ftdi->usb_write_timeout) != 0)
1928 ftdi_error_return(-1, "set flow control failed");
1936 \param ftdi pointer to ftdi_context
1937 \param state state to set line to (1 or 0)
1940 \retval -1: set dtr failed
1941 \retval -2: USB device unavailable
1943 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1945 unsigned short usb_val;
1947 if (ftdi == NULL || ftdi->usb_dev == NULL)
1948 ftdi_error_return(-2, "USB device unavailable");
1951 usb_val = SIO_SET_DTR_HIGH;
1953 usb_val = SIO_SET_DTR_LOW;
1955 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1956 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1957 NULL, 0, ftdi->usb_write_timeout) != 0)
1958 ftdi_error_return(-1, "set dtr failed");
1966 \param ftdi pointer to ftdi_context
1967 \param state state to set line to (1 or 0)
1970 \retval -1: set rts failed
1971 \retval -2: USB device unavailable
1973 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1975 unsigned short usb_val;
1977 if (ftdi == NULL || ftdi->usb_dev == NULL)
1978 ftdi_error_return(-2, "USB device unavailable");
1981 usb_val = SIO_SET_RTS_HIGH;
1983 usb_val = SIO_SET_RTS_LOW;
1985 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1986 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1987 NULL, 0, ftdi->usb_write_timeout) != 0)
1988 ftdi_error_return(-1, "set of rts failed");
1994 Set dtr and rts line in one pass
1996 \param ftdi pointer to ftdi_context
1997 \param dtr DTR state to set line to (1 or 0)
1998 \param rts RTS state to set line to (1 or 0)
2001 \retval -1: set dtr/rts failed
2002 \retval -2: USB device unavailable
2004 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2006 unsigned short usb_val;
2008 if (ftdi == NULL || ftdi->usb_dev == NULL)
2009 ftdi_error_return(-2, "USB device unavailable");
2012 usb_val = SIO_SET_DTR_HIGH;
2014 usb_val = SIO_SET_DTR_LOW;
2017 usb_val |= SIO_SET_RTS_HIGH;
2019 usb_val |= SIO_SET_RTS_LOW;
2021 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2022 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2023 NULL, 0, ftdi->usb_write_timeout) != 0)
2024 ftdi_error_return(-1, "set of rts/dtr failed");
2030 Set the special event character
2032 \param ftdi pointer to ftdi_context
2033 \param eventch Event character
2034 \param enable 0 to disable the event character, non-zero otherwise
2037 \retval -1: unable to set event character
2038 \retval -2: USB device unavailable
2040 int ftdi_set_event_char(struct ftdi_context *ftdi,
2041 unsigned char eventch, unsigned char enable)
2043 unsigned short usb_val;
2045 if (ftdi == NULL || ftdi->usb_dev == NULL)
2046 ftdi_error_return(-2, "USB device unavailable");
2052 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)
2053 ftdi_error_return(-1, "setting event character failed");
2061 \param ftdi pointer to ftdi_context
2062 \param errorch Error character
2063 \param enable 0 to disable the error character, non-zero otherwise
2066 \retval -1: unable to set error character
2067 \retval -2: USB device unavailable
2069 int ftdi_set_error_char(struct ftdi_context *ftdi,
2070 unsigned char errorch, unsigned char enable)
2072 unsigned short usb_val;
2074 if (ftdi == NULL || ftdi->usb_dev == NULL)
2075 ftdi_error_return(-2, "USB device unavailable");
2081 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)
2082 ftdi_error_return(-1, "setting error character failed");
2090 \param ftdi pointer to ftdi_context
2091 \param eeprom Pointer to ftdi_eeprom
2095 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2100 ftdi->eeprom_size=size;
2105 Init eeprom with default values.
2107 \param eeprom Pointer to ftdi_eeprom
2109 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2116 eeprom->vendor_id = 0x0403;
2117 eeprom->product_id = 0x6001;
2119 eeprom->self_powered = 1;
2120 eeprom->remote_wakeup = 1;
2121 eeprom->chip_type = TYPE_BM;
2123 eeprom->in_is_isochronous = 0;
2124 eeprom->out_is_isochronous = 0;
2125 eeprom->suspend_pull_downs = 0;
2127 eeprom->use_serial = 0;
2128 eeprom->change_usb_version = 0;
2129 eeprom->usb_version = 0x0200;
2130 eeprom->max_power = 0;
2132 eeprom->manufacturer = NULL;
2133 eeprom->product = NULL;
2134 eeprom->serial = NULL;
2135 for (i=0; i < 5; i++)
2137 eeprom->cbus_function[i] = 0;
2139 eeprom->high_current = 0;
2142 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2146 Frees allocated memory in eeprom.
2148 \param eeprom Pointer to ftdi_eeprom
2150 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2155 if (eeprom->manufacturer != 0) {
2156 free(eeprom->manufacturer);
2157 eeprom->manufacturer = 0;
2159 if (eeprom->product != 0) {
2160 free(eeprom->product);
2161 eeprom->product = 0;
2163 if (eeprom->serial != 0) {
2164 free(eeprom->serial);
2170 Build binary output from ftdi_eeprom structure.
2171 Output is suitable for ftdi_write_eeprom().
2173 \note This function doesn't handle FT2232x devices. Only FT232x.
2174 \param eeprom Pointer to ftdi_eeprom
2175 \param output Buffer of 128 bytes to store eeprom image to
2177 \retval >0: free eeprom size
2178 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2179 \retval -2: Invalid eeprom pointer
2180 \retval -3: Invalid cbus function setting
2181 \retval -4: Chip doesn't support invert
2182 \retval -5: Chip doesn't support high current drive
2184 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2187 unsigned short checksum, value;
2188 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2190 const int cbus_max[5] = {13, 13, 13, 13, 9};
2195 if (eeprom->manufacturer != NULL)
2196 manufacturer_size = strlen(eeprom->manufacturer);
2197 if (eeprom->product != NULL)
2198 product_size = strlen(eeprom->product);
2199 if (eeprom->serial != NULL)
2200 serial_size = strlen(eeprom->serial);
2202 // highest allowed cbus value
2203 for (i = 0; i < 5; i++)
2205 if ((eeprom->cbus_function[i] > cbus_max[i]) ||
2206 (eeprom->cbus_function[i] && eeprom->chip_type != TYPE_R)) return -3;
2208 if (eeprom->chip_type != TYPE_R)
2210 if (eeprom->invert) return -4;
2211 if (eeprom->high_current) return -5;
2214 size_check = eeprom->size;
2215 size_check -= 28; // 28 are always in use (fixed)
2217 // Top half of a 256byte eeprom is used just for strings and checksum
2218 // it seems that the FTDI chip will not read these strings from the lower half
2219 // Each string starts with two bytes; offset and type (0x03 for string)
2220 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2221 if (eeprom->size>=256) size_check = 120;
2222 size_check -= manufacturer_size*2;
2223 size_check -= product_size*2;
2224 size_check -= serial_size*2;
2226 // eeprom size exceeded?
2231 memset (output, 0, eeprom->size);
2233 // Addr 00: High current IO
2234 output[0x00] = eeprom->high_current ? HIGH_CURRENT_DRIVE : 0;
2235 // Addr 01: IN endpoint size (for R type devices, different for FT2232)
2236 if (eeprom->chip_type == TYPE_R) {
2237 output[0x01] = 0x40;
2239 // Addr 02: Vendor ID
2240 output[0x02] = eeprom->vendor_id;
2241 output[0x03] = eeprom->vendor_id >> 8;
2243 // Addr 04: Product ID
2244 output[0x04] = eeprom->product_id;
2245 output[0x05] = eeprom->product_id >> 8;
2247 // Addr 06: Device release number (0400h for BM features)
2248 output[0x06] = 0x00;
2249 switch (eeprom->chip_type) {
2251 output[0x07] = 0x02;
2254 output[0x07] = 0x04;
2257 output[0x07] = 0x05;
2260 output[0x07] = 0x06;
2263 output[0x07] = 0x00;
2266 // Addr 08: Config descriptor
2268 // Bit 6: 1 if this device is self powered, 0 if bus powered
2269 // Bit 5: 1 if this device uses remote wakeup
2270 // Bit 4: 1 if this device is battery powered
2272 if (eeprom->self_powered == 1)
2274 if (eeprom->remote_wakeup == 1)
2278 // Addr 09: Max power consumption: max power = value * 2 mA
2279 output[0x09] = eeprom->max_power;
2281 // Addr 0A: Chip configuration
2282 // Bit 7: 0 - reserved
2283 // Bit 6: 0 - reserved
2284 // Bit 5: 0 - reserved
2285 // Bit 4: 1 - Change USB version
2286 // Bit 3: 1 - Use the serial number string
2287 // Bit 2: 1 - Enable suspend pull downs for lower power
2288 // Bit 1: 1 - Out EndPoint is Isochronous
2289 // Bit 0: 1 - In EndPoint is Isochronous
2292 if (eeprom->in_is_isochronous == 1)
2294 if (eeprom->out_is_isochronous == 1)
2296 if (eeprom->suspend_pull_downs == 1)
2298 if (eeprom->use_serial == 1)
2300 if (eeprom->change_usb_version == 1)
2304 // Addr 0B: Invert data lines
2305 output[0x0B] = eeprom->invert & 0xff;
2307 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2308 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2309 if (eeprom->change_usb_version == 1)
2311 output[0x0C] = eeprom->usb_version;
2312 output[0x0D] = eeprom->usb_version >> 8;
2316 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2317 // Addr 0F: Length of manufacturer string
2318 output[0x0F] = manufacturer_size*2 + 2;
2320 // Addr 10: Offset of the product string + 0x80, calculated later
2321 // Addr 11: Length of product string
2322 output[0x11] = product_size*2 + 2;
2324 // Addr 12: Offset of the serial string + 0x80, calculated later
2325 // Addr 13: Length of serial string
2326 output[0x13] = serial_size*2 + 2;
2328 // Addr 14: CBUS function: CBUS0, CBUS1
2329 // Addr 15: CBUS function: CBUS2, CBUS3
2330 // Addr 16: CBUS function: CBUS5
2331 output[0x14] = eeprom->cbus_function[0] | (eeprom->cbus_function[1] << 4);
2332 output[0x15] = eeprom->cbus_function[2] | (eeprom->cbus_function[3] << 4);
2333 output[0x16] = eeprom->cbus_function[4];
2337 // In images produced by FTDI's FT_Prog for FT232R strings start at 0x18
2338 // Space till 0x18 should be considered as reserved.
2339 if (eeprom->chip_type >= TYPE_R) {
2344 if (eeprom->size >= 256) i = 0x80;
2347 // Output manufacturer
2348 output[0x0E] = i | 0x80; // calculate offset
2349 output[i++] = manufacturer_size*2 + 2;
2350 output[i++] = 0x03; // type: string
2351 for (j = 0; j < manufacturer_size; j++)
2353 output[i] = eeprom->manufacturer[j], i++;
2354 output[i] = 0x00, i++;
2357 // Output product name
2358 output[0x10] = i | 0x80; // calculate offset
2359 output[i] = product_size*2 + 2, i++;
2360 output[i] = 0x03, i++;
2361 for (j = 0; j < product_size; j++)
2363 output[i] = eeprom->product[j], i++;
2364 output[i] = 0x00, i++;
2368 output[0x12] = i | 0x80; // calculate offset
2369 output[i] = serial_size*2 + 2, i++;
2370 output[i] = 0x03, i++;
2371 for (j = 0; j < serial_size; j++)
2373 output[i] = eeprom->serial[j], i++;
2374 output[i] = 0x00, i++;
2377 // calculate checksum
2380 for (i = 0; i < eeprom->size/2-1; i++)
2382 value = output[i*2];
2383 value += output[(i*2)+1] << 8;
2385 checksum = value^checksum;
2386 checksum = (checksum << 1) | (checksum >> 15);
2389 output[eeprom->size-2] = checksum;
2390 output[eeprom->size-1] = checksum >> 8;
2396 Decode binary EEPROM image into an ftdi_eeprom structure.
2398 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2399 \param buf Buffer of \a size bytes of raw eeprom data
2400 \param size size size of eeprom data in bytes
2403 \retval -1: something went wrong
2405 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2406 FIXME: Strings are malloc'ed here and should be freed somewhere
2408 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2411 unsigned short checksum, eeprom_checksum, value;
2412 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2414 int eeprom_size = 128;
2419 size_check = eeprom->size;
2420 size_check -= 28; // 28 are always in use (fixed)
2422 // Top half of a 256byte eeprom is used just for strings and checksum
2423 // it seems that the FTDI chip will not read these strings from the lower half
2424 // Each string starts with two bytes; offset and type (0x03 for string)
2425 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2426 if (eeprom->size>=256)size_check = 120;
2427 size_check -= manufacturer_size*2;
2428 size_check -= product_size*2;
2429 size_check -= serial_size*2;
2431 // eeprom size exceeded?
2436 // empty eeprom struct
2437 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2439 // Addr 00: High current IO
2440 eeprom->high_current = (buf[0x02] & HIGH_CURRENT_DRIVE);
2442 // Addr 02: Vendor ID
2443 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2445 // Addr 04: Product ID
2446 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2448 value = buf[0x06] + (buf[0x07]<<8);
2452 eeprom->chip_type = TYPE_R;
2455 eeprom->chip_type = TYPE_BM;
2458 eeprom->chip_type = TYPE_AM;
2460 default: // Unknown device
2461 eeprom->chip_type = 0;
2465 // Addr 08: Config descriptor
2467 // Bit 6: 1 if this device is self powered, 0 if bus powered
2468 // Bit 5: 1 if this device uses remote wakeup
2469 // Bit 4: 1 if this device is battery powered
2471 if (j&0x40) eeprom->self_powered = 1;
2472 if (j&0x20) eeprom->remote_wakeup = 1;
2474 // Addr 09: Max power consumption: max power = value * 2 mA
2475 eeprom->max_power = buf[0x09];
2477 // Addr 0A: Chip configuration
2478 // Bit 7: 0 - reserved
2479 // Bit 6: 0 - reserved
2480 // Bit 5: 0 - reserved
2481 // Bit 4: 1 - Change USB version
2482 // Bit 3: 1 - Use the serial number string
2483 // Bit 2: 1 - Enable suspend pull downs for lower power
2484 // Bit 1: 1 - Out EndPoint is Isochronous
2485 // Bit 0: 1 - In EndPoint is Isochronous
2488 if (j&0x01) eeprom->in_is_isochronous = 1;
2489 if (j&0x02) eeprom->out_is_isochronous = 1;
2490 if (j&0x04) eeprom->suspend_pull_downs = 1;
2491 if (j&0x08) eeprom->use_serial = 1;
2492 if (j&0x10) eeprom->change_usb_version = 1;
2494 // Addr 0B: Invert data lines
2495 eeprom->invert = buf[0x0B];
2497 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2498 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2499 if (eeprom->change_usb_version == 1)
2501 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2504 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2505 // Addr 0F: Length of manufacturer string
2506 manufacturer_size = buf[0x0F]/2;
2507 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2508 else eeprom->manufacturer = NULL;
2510 // Addr 10: Offset of the product string + 0x80, calculated later
2511 // Addr 11: Length of product string
2512 product_size = buf[0x11]/2;
2513 if (product_size > 0) eeprom->product = malloc(product_size);
2514 else eeprom->product = NULL;
2516 // Addr 12: Offset of the serial string + 0x80, calculated later
2517 // Addr 13: Length of serial string
2518 serial_size = buf[0x13]/2;
2519 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2520 else eeprom->serial = NULL;
2522 // Addr 14: CBUS function: CBUS0, CBUS1
2523 // Addr 15: CBUS function: CBUS2, CBUS3
2524 // Addr 16: CBUS function: CBUS5
2525 if (eeprom->chip_type == TYPE_R) {
2526 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2527 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2528 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2529 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2530 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2532 for (j=0; j<5; j++) eeprom->cbus_function[j] = 0;
2535 // Decode manufacturer
2536 i = buf[0x0E] & 0x7f; // offset
2537 for (j=0;j<manufacturer_size-1;j++)
2539 eeprom->manufacturer[j] = buf[2*j+i+2];
2541 eeprom->manufacturer[j] = '\0';
2543 // Decode product name
2544 i = buf[0x10] & 0x7f; // offset
2545 for (j=0;j<product_size-1;j++)
2547 eeprom->product[j] = buf[2*j+i+2];
2549 eeprom->product[j] = '\0';
2552 i = buf[0x12] & 0x7f; // offset
2553 for (j=0;j<serial_size-1;j++)
2555 eeprom->serial[j] = buf[2*j+i+2];
2557 eeprom->serial[j] = '\0';
2562 for (i = 0; i < eeprom_size/2-1; i++)
2565 value += buf[(i*2)+1] << 8;
2567 checksum = value^checksum;
2568 checksum = (checksum << 1) | (checksum >> 15);
2571 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2573 if (eeprom_checksum != checksum)
2575 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2583 Read eeprom location
2585 \param ftdi pointer to ftdi_context
2586 \param eeprom_addr Address of eeprom location to be read
2587 \param eeprom_val Pointer to store read eeprom location
2590 \retval -1: read failed
2591 \retval -2: USB device unavailable
2593 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2595 if (ftdi == NULL || ftdi->usb_dev == NULL)
2596 ftdi_error_return(-2, "USB device unavailable");
2598 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)
2599 ftdi_error_return(-1, "reading eeprom failed");
2607 \param ftdi pointer to ftdi_context
2608 \param eeprom Pointer to store eeprom into
2611 \retval -1: read failed
2612 \retval -2: USB device unavailable
2614 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2618 if (ftdi == NULL || ftdi->usb_dev == NULL)
2619 ftdi_error_return(-2, "USB device unavailable");
2621 for (i = 0; i < ftdi->eeprom_size/2; i++)
2623 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)
2624 ftdi_error_return(-1, "reading eeprom failed");
2631 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2632 Function is only used internally
2635 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2637 return ((value & 1) << 1) |
2638 ((value & 2) << 5) |
2639 ((value & 4) >> 2) |
2640 ((value & 8) << 4) |
2641 ((value & 16) >> 1) |
2642 ((value & 32) >> 1) |
2643 ((value & 64) >> 4) |
2644 ((value & 128) >> 2);
2648 Read the FTDIChip-ID from R-type devices
2650 \param ftdi pointer to ftdi_context
2651 \param chipid Pointer to store FTDIChip-ID
2654 \retval -1: read failed
2655 \retval -2: USB device unavailable
2657 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2659 unsigned int a = 0, b = 0;
2661 if (ftdi == NULL || ftdi->usb_dev == NULL)
2662 ftdi_error_return(-2, "USB device unavailable");
2664 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)
2666 a = a << 8 | a >> 8;
2667 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)
2669 b = b << 8 | b >> 8;
2670 a = (a << 16) | (b & 0xFFFF);
2671 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2672 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2673 *chipid = a ^ 0xa5f0f7d1;
2678 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2682 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2683 Call this function then do a write then call again to see if size changes, if so write again.
2685 \param ftdi pointer to ftdi_context
2686 \param eeprom Pointer to store eeprom into
2687 \param maxsize the size of the buffer to read into
2689 \retval -1: eeprom read failed
2690 \retval -2: USB device unavailable
2691 \retval >=0: size of eeprom
2693 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2695 int i=0,j,minsize=32;
2698 if (ftdi == NULL || ftdi->usb_dev == NULL)
2699 ftdi_error_return(-2, "USB device unavailable");
2703 for (j = 0; i < maxsize/2 && j<size; j++)
2705 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2706 SIO_READ_EEPROM_REQUEST, 0, i,
2707 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2708 ftdi_error_return(-1, "eeprom read failed");
2713 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2719 Write eeprom location
2721 \param ftdi pointer to ftdi_context
2722 \param eeprom_addr Address of eeprom location to be written
2723 \param eeprom_val Value to be written
2726 \retval -1: read failed
2727 \retval -2: USB device unavailable
2729 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2731 if (ftdi == NULL || ftdi->usb_dev == NULL)
2732 ftdi_error_return(-2, "USB device unavailable");
2734 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2735 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2736 NULL, 0, ftdi->usb_write_timeout) != 0)
2737 ftdi_error_return(-1, "unable to write eeprom");
2745 \param ftdi pointer to ftdi_context
2746 \param eeprom Pointer to read eeprom from
2749 \retval -1: read failed
2750 \retval -2: USB device unavailable
2752 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2754 unsigned short usb_val, status;
2757 if (ftdi == NULL || ftdi->usb_dev == NULL)
2758 ftdi_error_return(-2, "USB device unavailable");
2760 /* These commands were traced while running MProg */
2761 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2763 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2765 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2768 for (i = 0; i < ftdi->eeprom_size/2; i++)
2770 usb_val = eeprom[i*2];
2771 usb_val += eeprom[(i*2)+1] << 8;
2772 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2773 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2774 NULL, 0, ftdi->usb_write_timeout) != 0)
2775 ftdi_error_return(-1, "unable to write eeprom");
2784 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2786 \param ftdi pointer to ftdi_context
2789 \retval -1: erase failed
2790 \retval -2: USB device unavailable
2792 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2794 if (ftdi == NULL || ftdi->usb_dev == NULL)
2795 ftdi_error_return(-2, "USB device unavailable");
2797 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
2798 ftdi_error_return(-1, "unable to erase eeprom");
2804 Get string representation for last error code
2806 \param ftdi pointer to ftdi_context
2808 \retval Pointer to error string
2810 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2815 return ftdi->error_str;
2818 /* @} end of doxygen libftdi group */