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 */
39 #define ftdi_error_return(code, str) do { \
40 ftdi->error_str = str; \
44 #define ftdi_error_return_free_device_list(code, str, devs) do { \
45 libusb_free_device_list(devs,1); \
46 ftdi->error_str = str; \
52 Internal function to close usb device pointer.
53 Sets ftdi->usb_dev to NULL.
56 \param ftdi pointer to ftdi_context
60 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
62 if (ftdi && ftdi->usb_dev)
64 libusb_close (ftdi->usb_dev);
70 Initializes a ftdi_context.
72 \param ftdi pointer to ftdi_context
75 \retval -1: couldn't allocate read buffer
77 \remark This should be called before all functions
79 int ftdi_init(struct ftdi_context *ftdi)
82 ftdi->usb_read_timeout = 5000;
83 ftdi->usb_write_timeout = 5000;
85 ftdi->type = TYPE_BM; /* chip type */
87 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
89 ftdi->readbuffer = NULL;
90 ftdi->readbuffer_offset = 0;
91 ftdi->readbuffer_remaining = 0;
92 ftdi->writebuffer_chunksize = 4096;
93 ftdi->max_packet_size = 0;
99 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
101 ftdi->error_str = NULL;
103 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
105 /* All fine. Now allocate the readbuffer */
106 return ftdi_read_data_set_chunksize(ftdi, 4096);
110 Allocate and initialize a new ftdi_context
112 \return a pointer to a new ftdi_context, or NULL on failure
114 struct ftdi_context *ftdi_new(void)
116 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
123 if (ftdi_init(ftdi) != 0)
133 Open selected channels on a chip, otherwise use first channel.
135 \param ftdi pointer to ftdi_context
136 \param interface Interface to use for FT2232C/2232H/4232H chips.
139 \retval -1: unknown interface
140 \retval -2: USB device unavailable
142 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
145 ftdi_error_return(-2, "USB device unavailable");
151 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
155 ftdi->index = INTERFACE_B;
161 ftdi->index = INTERFACE_C;
167 ftdi->index = INTERFACE_D;
172 ftdi_error_return(-1, "Unknown interface");
178 Deinitializes a ftdi_context.
180 \param ftdi pointer to ftdi_context
182 void ftdi_deinit(struct ftdi_context *ftdi)
187 ftdi_usb_close_internal (ftdi);
189 if (ftdi->readbuffer != NULL)
191 free(ftdi->readbuffer);
192 ftdi->readbuffer = NULL;
198 Deinitialize and free an ftdi_context.
200 \param ftdi pointer to ftdi_context
202 void ftdi_free(struct ftdi_context *ftdi)
209 Use an already open libusb device.
211 \param ftdi pointer to ftdi_context
212 \param usb libusb libusb_device_handle to use
214 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
224 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
225 needs to be deallocated by ftdi_list_free() after use.
227 \param ftdi pointer to ftdi_context
228 \param devlist Pointer where to store list of found devices
229 \param vendor Vendor ID to search for
230 \param product Product ID to search for
232 \retval >0: number of devices found
233 \retval -3: out of memory
234 \retval -4: libusb_init() failed
235 \retval -5: libusb_get_device_list() failed
236 \retval -6: libusb_get_device_descriptor() failed
238 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
240 struct ftdi_device_list **curdev;
242 libusb_device **devs;
246 if (libusb_init(NULL) < 0)
247 ftdi_error_return(-4, "libusb_init() failed");
249 if (libusb_get_device_list(NULL, &devs) < 0)
250 ftdi_error_return(-5, "libusb_get_device_list() failed");
255 while ((dev = devs[i++]) != NULL)
257 struct libusb_device_descriptor desc;
259 if (libusb_get_device_descriptor(dev, &desc) < 0)
260 ftdi_error_return(-6, "libusb_get_device_descriptor() failed");
262 if (desc.idVendor == vendor && desc.idProduct == product)
264 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
266 ftdi_error_return(-3, "out of memory");
268 (*curdev)->next = NULL;
269 (*curdev)->dev = dev;
271 curdev = &(*curdev)->next;
280 Frees a usb device list.
282 \param devlist USB device list created by ftdi_usb_find_all()
284 void ftdi_list_free(struct ftdi_device_list **devlist)
286 struct ftdi_device_list *curdev, *next;
288 for (curdev = *devlist; curdev != NULL;)
299 Frees a usb device list.
301 \param devlist USB device list created by ftdi_usb_find_all()
303 void ftdi_list_free2(struct ftdi_device_list *devlist)
305 ftdi_list_free(&devlist);
309 Return device ID strings from the usb device.
311 The parameters manufacturer, description and serial may be NULL
312 or pointer to buffers to store the fetched strings.
314 \note Use this function only in combination with ftdi_usb_find_all()
315 as it closes the internal "usb_dev" after use.
317 \param ftdi pointer to ftdi_context
318 \param dev libusb usb_dev to use
319 \param manufacturer Store manufacturer string here if not NULL
320 \param mnf_len Buffer size of manufacturer string
321 \param description Store product description string here if not NULL
322 \param desc_len Buffer size of product description string
323 \param serial Store serial string here if not NULL
324 \param serial_len Buffer size of serial string
327 \retval -1: wrong arguments
328 \retval -4: unable to open device
329 \retval -7: get product manufacturer failed
330 \retval -8: get product description failed
331 \retval -9: get serial number failed
332 \retval -11: libusb_get_device_descriptor() failed
334 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
335 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
337 struct libusb_device_descriptor desc;
339 if ((ftdi==NULL) || (dev==NULL))
342 if (libusb_open(dev, &ftdi->usb_dev) < 0)
343 ftdi_error_return(-4, "libusb_open() failed");
345 if (libusb_get_device_descriptor(dev, &desc) < 0)
346 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
348 if (manufacturer != NULL)
350 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
352 ftdi_usb_close_internal (ftdi);
353 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
357 if (description != NULL)
359 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
361 ftdi_usb_close_internal (ftdi);
362 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
368 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
370 ftdi_usb_close_internal (ftdi);
371 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
375 ftdi_usb_close_internal (ftdi);
381 * Internal function to determine the maximum packet size.
382 * \param ftdi pointer to ftdi_context
383 * \param dev libusb usb_dev to use
384 * \retval Maximum packet size for this device
386 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
388 struct libusb_device_descriptor desc;
389 struct libusb_config_descriptor *config0;
390 unsigned int packet_size;
393 if (ftdi == NULL || dev == NULL)
396 // Determine maximum packet size. Init with default value.
397 // New hi-speed devices from FTDI use a packet size of 512 bytes
398 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
399 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
404 if (libusb_get_device_descriptor(dev, &desc) < 0)
407 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
410 if (desc.bNumConfigurations > 0)
412 if (ftdi->interface < config0->bNumInterfaces)
414 struct libusb_interface interface = config0->interface[ftdi->interface];
415 if (interface.num_altsetting > 0)
417 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
418 if (descriptor.bNumEndpoints > 0)
420 packet_size = descriptor.endpoint[0].wMaxPacketSize;
426 libusb_free_config_descriptor (config0);
431 Opens a ftdi device given by an usb_device.
433 \param ftdi pointer to ftdi_context
434 \param dev libusb usb_dev to use
437 \retval -3: unable to config device
438 \retval -4: unable to open device
439 \retval -5: unable to claim device
440 \retval -6: reset failed
441 \retval -7: set baudrate failed
442 \retval -8: ftdi context invalid
443 \retval -9: libusb_get_device_descriptor() failed
444 \retval -10: libusb_get_config_descriptor() failed
445 \retval -11: libusb_etach_kernel_driver() failed
446 \retval -12: libusb_get_configuration() failed
448 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
450 struct libusb_device_descriptor desc;
451 struct libusb_config_descriptor *config0;
452 int cfg, cfg0, detach_errno = 0;
455 ftdi_error_return(-8, "ftdi context invalid");
457 if (libusb_open(dev, &ftdi->usb_dev) < 0)
458 ftdi_error_return(-4, "libusb_open() failed");
460 if (libusb_get_device_descriptor(dev, &desc) < 0)
461 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
463 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
464 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
465 cfg0 = config0->bConfigurationValue;
466 libusb_free_config_descriptor (config0);
468 // Try to detach ftdi_sio kernel module.
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 (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
475 detach_errno = errno;
477 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
478 ftdi_error_return(-12, "libusb_get_configuration () failed");
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)
482 if (desc.bNumConfigurations > 0 && cfg != cfg0)
484 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
486 ftdi_usb_close_internal (ftdi);
487 if(detach_errno == EPERM)
489 ftdi_error_return(-8, "inappropriate permissions on device!");
493 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
498 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
500 ftdi_usb_close_internal (ftdi);
501 if(detach_errno == EPERM)
503 ftdi_error_return(-8, "inappropriate permissions on device!");
507 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
511 if (ftdi_usb_reset (ftdi) != 0)
513 ftdi_usb_close_internal (ftdi);
514 ftdi_error_return(-6, "ftdi_usb_reset failed");
517 // Try to guess chip type
518 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
519 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
520 && desc.iSerialNumber == 0))
521 ftdi->type = TYPE_BM;
522 else if (desc.bcdDevice == 0x200)
523 ftdi->type = TYPE_AM;
524 else if (desc.bcdDevice == 0x500)
525 ftdi->type = TYPE_2232C;
526 else if (desc.bcdDevice == 0x600)
528 else if (desc.bcdDevice == 0x700)
529 ftdi->type = TYPE_2232H;
530 else if (desc.bcdDevice == 0x800)
531 ftdi->type = TYPE_4232H;
533 // Set default interface on dual/quad type chips
540 ftdi->index = INTERFACE_A;
546 // Determine maximum packet size
547 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
549 if (ftdi_set_baudrate (ftdi, 9600) != 0)
551 ftdi_usb_close_internal (ftdi);
552 ftdi_error_return(-7, "set baudrate failed");
555 ftdi_error_return(0, "all fine");
559 Opens the first device with a given vendor and product ids.
561 \param ftdi pointer to ftdi_context
562 \param vendor Vendor ID
563 \param product Product ID
565 \retval same as ftdi_usb_open_desc()
567 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
569 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
573 Opens the first device with a given, vendor id, product id,
574 description and serial.
576 \param ftdi pointer to ftdi_context
577 \param vendor Vendor ID
578 \param product Product ID
579 \param description Description to search for. Use NULL if not needed.
580 \param serial Serial to search for. Use NULL if not needed.
583 \retval -3: usb device not found
584 \retval -4: unable to open device
585 \retval -5: unable to claim device
586 \retval -6: reset failed
587 \retval -7: set baudrate failed
588 \retval -8: get product description failed
589 \retval -9: get serial number failed
590 \retval -11: libusb_init() failed
591 \retval -12: libusb_get_device_list() failed
592 \retval -13: libusb_get_device_descriptor() failed
594 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
595 const char* description, const char* serial)
597 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
601 Opens the index-th device with a given, vendor id, product id,
602 description and serial.
604 \param ftdi pointer to ftdi_context
605 \param vendor Vendor ID
606 \param product Product ID
607 \param description Description to search for. Use NULL if not needed.
608 \param serial Serial to search for. Use NULL if not needed.
609 \param index Number of matching device to open if there are more than one, starts with 0.
612 \retval -1: usb_find_busses() failed
613 \retval -2: usb_find_devices() failed
614 \retval -3: usb device not found
615 \retval -4: unable to open device
616 \retval -5: unable to claim device
617 \retval -6: reset failed
618 \retval -7: set baudrate failed
619 \retval -8: get product description failed
620 \retval -9: get serial number failed
621 \retval -10: unable to close device
622 \retval -11: ftdi context invalid
624 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
625 const char* description, const char* serial, unsigned int index)
628 libusb_device **devs;
632 if (libusb_init(NULL) < 0)
633 ftdi_error_return(-11, "libusb_init() failed");
636 ftdi_error_return(-11, "ftdi context invalid");
638 if (libusb_get_device_list(NULL, &devs) < 0)
639 ftdi_error_return(-12, "libusb_get_device_list() failed");
641 while ((dev = devs[i++]) != NULL)
643 struct libusb_device_descriptor desc;
646 if (libusb_get_device_descriptor(dev, &desc) < 0)
647 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
649 if (desc.idVendor == vendor && desc.idProduct == product)
651 if (libusb_open(dev, &ftdi->usb_dev) < 0)
652 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
654 if (description != NULL)
656 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
658 libusb_close (ftdi->usb_dev);
659 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
661 if (strncmp(string, description, sizeof(string)) != 0)
663 libusb_close (ftdi->usb_dev);
669 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
671 ftdi_usb_close_internal (ftdi);
672 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
674 if (strncmp(string, serial, sizeof(string)) != 0)
676 ftdi_usb_close_internal (ftdi);
681 ftdi_usb_close_internal (ftdi);
689 res = ftdi_usb_open_dev(ftdi, dev);
690 libusb_free_device_list(devs,1);
696 ftdi_error_return_free_device_list(-3, "device not found", devs);
700 Opens the ftdi-device described by a description-string.
701 Intended to be used for parsing a device-description given as commandline argument.
703 \param ftdi pointer to ftdi_context
704 \param description NULL-terminated description-string, using this format:
705 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
706 \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")
707 \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
708 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
710 \note The description format may be extended in later versions.
713 \retval -1: libusb_init() failed
714 \retval -2: libusb_get_device_list() failed
715 \retval -3: usb device not found
716 \retval -4: unable to open device
717 \retval -5: unable to claim device
718 \retval -6: reset failed
719 \retval -7: set baudrate failed
720 \retval -8: get product description failed
721 \retval -9: get serial number failed
722 \retval -10: unable to close device
723 \retval -11: illegal description format
724 \retval -12: ftdi context invalid
726 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
729 ftdi_error_return(-12, "ftdi context invalid");
731 if (description[0] == 0 || description[1] != ':')
732 ftdi_error_return(-11, "illegal description format");
734 if (description[0] == 'd')
737 libusb_device **devs;
738 unsigned int bus_number, device_address;
741 if (libusb_init (NULL) < 0)
742 ftdi_error_return(-1, "libusb_init() failed");
744 if (libusb_get_device_list(NULL, &devs) < 0)
745 ftdi_error_return(-2, "libusb_get_device_list() failed");
747 /* XXX: This doesn't handle symlinks/odd paths/etc... */
748 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
749 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
751 while ((dev = devs[i++]) != NULL)
754 if (bus_number == libusb_get_bus_number (dev)
755 && device_address == libusb_get_device_address (dev))
757 ret = ftdi_usb_open_dev(ftdi, dev);
758 libusb_free_device_list(devs,1);
764 ftdi_error_return_free_device_list(-3, "device not found", devs);
766 else if (description[0] == 'i' || description[0] == 's')
769 unsigned int product;
770 unsigned int index=0;
771 const char *serial=NULL;
772 const char *startp, *endp;
775 startp=description+2;
776 vendor=strtoul((char*)startp,(char**)&endp,0);
777 if (*endp != ':' || endp == startp || errno != 0)
778 ftdi_error_return(-11, "illegal description format");
781 product=strtoul((char*)startp,(char**)&endp,0);
782 if (endp == startp || errno != 0)
783 ftdi_error_return(-11, "illegal description format");
785 if (description[0] == 'i' && *endp != 0)
787 /* optional index field in i-mode */
789 ftdi_error_return(-11, "illegal description format");
792 index=strtoul((char*)startp,(char**)&endp,0);
793 if (*endp != 0 || endp == startp || errno != 0)
794 ftdi_error_return(-11, "illegal description format");
796 if (description[0] == 's')
799 ftdi_error_return(-11, "illegal description format");
801 /* rest of the description is the serial */
805 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
809 ftdi_error_return(-11, "illegal description format");
814 Resets the ftdi device.
816 \param ftdi pointer to ftdi_context
819 \retval -1: FTDI reset failed
820 \retval -2: USB device unavailable
822 int ftdi_usb_reset(struct ftdi_context *ftdi)
824 if (ftdi == NULL || ftdi->usb_dev == NULL)
825 ftdi_error_return(-2, "USB device unavailable");
827 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
828 SIO_RESET_REQUEST, SIO_RESET_SIO,
829 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
830 ftdi_error_return(-1,"FTDI reset failed");
832 // Invalidate data in the readbuffer
833 ftdi->readbuffer_offset = 0;
834 ftdi->readbuffer_remaining = 0;
840 Clears the read buffer on the chip and the internal read buffer.
842 \param ftdi pointer to ftdi_context
845 \retval -1: read buffer purge failed
846 \retval -2: USB device unavailable
848 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
850 if (ftdi == NULL || ftdi->usb_dev == NULL)
851 ftdi_error_return(-2, "USB device unavailable");
853 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
854 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
855 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
856 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
858 // Invalidate data in the readbuffer
859 ftdi->readbuffer_offset = 0;
860 ftdi->readbuffer_remaining = 0;
866 Clears the write buffer on the chip.
868 \param ftdi pointer to ftdi_context
871 \retval -1: write buffer purge failed
872 \retval -2: USB device unavailable
874 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
876 if (ftdi == NULL || ftdi->usb_dev == NULL)
877 ftdi_error_return(-2, "USB device unavailable");
879 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
880 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
881 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
882 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
888 Clears the buffers on the chip and the internal read buffer.
890 \param ftdi pointer to ftdi_context
893 \retval -1: read buffer purge failed
894 \retval -2: write buffer purge failed
895 \retval -3: USB device unavailable
897 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
901 if (ftdi == NULL || ftdi->usb_dev == NULL)
902 ftdi_error_return(-3, "USB device unavailable");
904 result = ftdi_usb_purge_rx_buffer(ftdi);
908 result = ftdi_usb_purge_tx_buffer(ftdi);
918 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
920 \param ftdi pointer to ftdi_context
923 \retval -1: usb_release failed
924 \retval -3: ftdi context invalid
926 int ftdi_usb_close(struct ftdi_context *ftdi)
931 ftdi_error_return(-3, "ftdi context invalid");
933 if (ftdi->usb_dev != NULL)
934 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
937 ftdi_usb_close_internal (ftdi);
943 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
944 Function is only used internally
947 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
948 unsigned short *value, unsigned short *index)
950 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
951 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
952 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
953 int divisor, best_divisor, best_baud, best_baud_diff;
954 unsigned long encoded_divisor;
963 divisor = 24000000 / baudrate;
965 if (ftdi->type == TYPE_AM)
967 // Round down to supported fraction (AM only)
968 divisor -= am_adjust_dn[divisor & 7];
971 // Try this divisor and the one above it (because division rounds down)
975 for (i = 0; i < 2; i++)
977 int try_divisor = divisor + i;
981 // Round up to supported divisor value
982 if (try_divisor <= 8)
984 // Round up to minimum supported divisor
987 else if (ftdi->type != TYPE_AM && try_divisor < 12)
989 // BM doesn't support divisors 9 through 11 inclusive
992 else if (divisor < 16)
994 // AM doesn't support divisors 9 through 15 inclusive
999 if (ftdi->type == TYPE_AM)
1001 // Round up to supported fraction (AM only)
1002 try_divisor += am_adjust_up[try_divisor & 7];
1003 if (try_divisor > 0x1FFF8)
1005 // Round down to maximum supported divisor value (for AM)
1006 try_divisor = 0x1FFF8;
1011 if (try_divisor > 0x1FFFF)
1013 // Round down to maximum supported divisor value (for BM)
1014 try_divisor = 0x1FFFF;
1018 // Get estimated baud rate (to nearest integer)
1019 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1020 // Get absolute difference from requested baud rate
1021 if (baud_estimate < baudrate)
1023 baud_diff = baudrate - baud_estimate;
1027 baud_diff = baud_estimate - baudrate;
1029 if (i == 0 || baud_diff < best_baud_diff)
1031 // Closest to requested baud rate so far
1032 best_divisor = try_divisor;
1033 best_baud = baud_estimate;
1034 best_baud_diff = baud_diff;
1037 // Spot on! No point trying
1042 // Encode the best divisor value
1043 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1044 // Deal with special cases for encoded value
1045 if (encoded_divisor == 1)
1047 encoded_divisor = 0; // 3000000 baud
1049 else if (encoded_divisor == 0x4001)
1051 encoded_divisor = 1; // 2000000 baud (BM only)
1053 // Split into "value" and "index" values
1054 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1055 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1057 *index = (unsigned short)(encoded_divisor >> 8);
1059 *index |= ftdi->index;
1062 *index = (unsigned short)(encoded_divisor >> 16);
1064 // Return the nearest baud rate
1069 Sets the chip baud rate
1071 \param ftdi pointer to ftdi_context
1072 \param baudrate baud rate to set
1075 \retval -1: invalid baudrate
1076 \retval -2: setting baudrate failed
1077 \retval -3: USB device unavailable
1079 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1081 unsigned short value, index;
1082 int actual_baudrate;
1084 if (ftdi == NULL || ftdi->usb_dev == NULL)
1085 ftdi_error_return(-3, "USB device unavailable");
1087 if (ftdi->bitbang_enabled)
1089 baudrate = baudrate*4;
1092 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1093 if (actual_baudrate <= 0)
1094 ftdi_error_return (-1, "Silly baudrate <= 0.");
1096 // Check within tolerance (about 5%)
1097 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1098 || ((actual_baudrate < baudrate)
1099 ? (actual_baudrate * 21 < baudrate * 20)
1100 : (baudrate * 21 < actual_baudrate * 20)))
1101 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1103 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1104 SIO_SET_BAUDRATE_REQUEST, value,
1105 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1106 ftdi_error_return (-2, "Setting new baudrate failed");
1108 ftdi->baudrate = baudrate;
1113 Set (RS232) line characteristics.
1114 The break type can only be set via ftdi_set_line_property2()
1115 and defaults to "off".
1117 \param ftdi pointer to ftdi_context
1118 \param bits Number of bits
1119 \param sbit Number of stop bits
1120 \param parity Parity mode
1123 \retval -1: Setting line property failed
1125 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1126 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1128 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1132 Set (RS232) line characteristics
1134 \param ftdi pointer to ftdi_context
1135 \param bits Number of bits
1136 \param sbit Number of stop bits
1137 \param parity Parity mode
1138 \param break_type Break type
1141 \retval -1: Setting line property failed
1142 \retval -2: USB device unavailable
1144 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1145 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1146 enum ftdi_break_type break_type)
1148 unsigned short value = bits;
1150 if (ftdi == NULL || ftdi->usb_dev == NULL)
1151 ftdi_error_return(-2, "USB device unavailable");
1156 value |= (0x00 << 8);
1159 value |= (0x01 << 8);
1162 value |= (0x02 << 8);
1165 value |= (0x03 << 8);
1168 value |= (0x04 << 8);
1175 value |= (0x00 << 11);
1178 value |= (0x01 << 11);
1181 value |= (0x02 << 11);
1188 value |= (0x00 << 14);
1191 value |= (0x01 << 14);
1195 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1196 SIO_SET_DATA_REQUEST, value,
1197 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1198 ftdi_error_return (-1, "Setting new line property failed");
1204 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1206 \param ftdi pointer to ftdi_context
1207 \param buf Buffer with the data
1208 \param size Size of the buffer
1210 \retval -666: USB device unavailable
1211 \retval <0: error code from usb_bulk_write()
1212 \retval >0: number of bytes written
1214 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1219 if (ftdi == NULL || ftdi->usb_dev == NULL)
1220 ftdi_error_return(-666, "USB device unavailable");
1222 while (offset < size)
1224 int write_size = ftdi->writebuffer_chunksize;
1226 if (offset+write_size > size)
1227 write_size = size-offset;
1229 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1230 ftdi_error_return(-1, "usb bulk write failed");
1232 offset += actual_length;
1238 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1239 #ifdef USB_CLASS_PTP
1240 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1242 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1244 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1245 struct ftdi_context *ftdi = tc->ftdi;
1246 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1248 packet_size = ftdi->max_packet_size;
1250 actual_length = transfer->actual_length;
1252 if (actual_length > 2)
1254 // skip FTDI status bytes.
1255 // Maybe stored in the future to enable modem use
1256 num_of_chunks = actual_length / packet_size;
1257 chunk_remains = actual_length % packet_size;
1258 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1260 ftdi->readbuffer_offset += 2;
1263 if (actual_length > packet_size - 2)
1265 for (i = 1; i < num_of_chunks; i++)
1266 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1267 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1269 if (chunk_remains > 2)
1271 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1272 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1274 actual_length -= 2*num_of_chunks;
1277 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1280 if (actual_length > 0)
1282 // data still fits in buf?
1283 if (tc->offset + actual_length <= tc->size)
1285 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1286 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1287 tc->offset += actual_length;
1289 ftdi->readbuffer_offset = 0;
1290 ftdi->readbuffer_remaining = 0;
1292 /* Did we read exactly the right amount of bytes? */
1293 if (tc->offset == tc->size)
1295 //printf("read_data exact rem %d offset %d\n",
1296 //ftdi->readbuffer_remaining, offset);
1303 // only copy part of the data or size <= readbuffer_chunksize
1304 int part_size = tc->size - tc->offset;
1305 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1306 tc->offset += part_size;
1308 ftdi->readbuffer_offset += part_size;
1309 ftdi->readbuffer_remaining = actual_length - part_size;
1311 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1312 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1318 ret = libusb_submit_transfer (transfer);
1324 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1326 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1327 struct ftdi_context *ftdi = tc->ftdi;
1329 tc->offset = transfer->actual_length;
1331 if (tc->offset == tc->size)
1337 int write_size = ftdi->writebuffer_chunksize;
1340 if (tc->offset + write_size > tc->size)
1341 write_size = tc->size - tc->offset;
1343 transfer->length = write_size;
1344 transfer->buffer = tc->buf + tc->offset;
1345 ret = libusb_submit_transfer (transfer);
1353 Writes data to the chip. Does not wait for completion of the transfer
1354 nor does it make sure that the transfer was successful.
1356 Use libusb 1.0 Asynchronous API.
1357 Only available if compiled with --with-async-mode.
1359 \param ftdi pointer to ftdi_context
1360 \param buf Buffer with the data
1361 \param size Size of the buffer
1363 \retval NULL: Some error happens when submit transfer
1364 \retval !NULL: Pointer to a ftdi_transfer_control
1367 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1369 struct ftdi_transfer_control *tc;
1370 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1371 int write_size, ret;
1373 if (ftdi == NULL || ftdi->usb_dev == NULL)
1375 libusb_free_transfer(transfer);
1379 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1381 if (!tc || !transfer)
1390 if (size < ftdi->writebuffer_chunksize)
1393 write_size = ftdi->writebuffer_chunksize;
1395 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf, write_size, ftdi_write_data_cb, tc, ftdi->usb_write_timeout);
1396 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1398 ret = libusb_submit_transfer(transfer);
1401 libusb_free_transfer(transfer);
1403 tc->transfer = NULL;
1406 tc->transfer = transfer;
1412 Reads data from the chip. Does not wait for completion of the transfer
1413 nor does it make sure that the transfer was successful.
1415 Use libusb 1.0 Asynchronous API.
1416 Only available if compiled with --with-async-mode.
1418 \param ftdi pointer to ftdi_context
1419 \param buf Buffer with the data
1420 \param size Size of the buffer
1422 \retval NULL: Some error happens when submit transfer
1423 \retval !NULL: Pointer to a ftdi_transfer_control
1426 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1428 struct ftdi_transfer_control *tc;
1429 struct libusb_transfer *transfer;
1432 if (ftdi == NULL || ftdi->usb_dev == NULL)
1435 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1443 if (size <= ftdi->readbuffer_remaining)
1445 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1448 ftdi->readbuffer_remaining -= size;
1449 ftdi->readbuffer_offset += size;
1451 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1455 tc->transfer = NULL;
1460 if (ftdi->readbuffer_remaining != 0)
1462 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1464 tc->offset = ftdi->readbuffer_remaining;
1469 transfer = libusb_alloc_transfer(0);
1476 ftdi->readbuffer_remaining = 0;
1477 ftdi->readbuffer_offset = 0;
1479 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout);
1480 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1482 ret = libusb_submit_transfer(transfer);
1485 libusb_free_transfer(transfer);
1489 tc->transfer = transfer;
1495 Wait for completion of the transfer.
1497 Use libusb 1.0 Asynchronous API.
1498 Only available if compiled with --with-async-mode.
1500 \param tc pointer to ftdi_transfer_control
1502 \retval < 0: Some error happens
1503 \retval >= 0: Data size transferred
1506 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1510 while (!tc->completed)
1512 ret = libusb_handle_events(NULL);
1515 if (ret == LIBUSB_ERROR_INTERRUPTED)
1517 libusb_cancel_transfer(tc->transfer);
1518 while (!tc->completed)
1519 if (libusb_handle_events(NULL) < 0)
1521 libusb_free_transfer(tc->transfer);
1528 if (tc->transfer->status == LIBUSB_TRANSFER_COMPLETED)
1533 libusb_free_transfer(tc->transfer);
1538 #endif // LIBFTDI_LINUX_ASYNC_MODE
1541 Configure write buffer chunk size.
1544 \param ftdi pointer to ftdi_context
1545 \param chunksize Chunk size
1548 \retval -1: ftdi context invalid
1550 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1553 ftdi_error_return(-1, "ftdi context invalid");
1555 ftdi->writebuffer_chunksize = chunksize;
1560 Get write buffer chunk size.
1562 \param ftdi pointer to ftdi_context
1563 \param chunksize Pointer to store chunk size in
1566 \retval -1: ftdi context invalid
1568 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1571 ftdi_error_return(-1, "ftdi context invalid");
1573 *chunksize = ftdi->writebuffer_chunksize;
1578 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1580 Automatically strips the two modem status bytes transfered during every read.
1582 \param ftdi pointer to ftdi_context
1583 \param buf Buffer to store data in
1584 \param size Size of the buffer
1586 \retval -666: USB device unavailable
1587 \retval <0: error code from libusb_bulk_transfer()
1588 \retval 0: no data was available
1589 \retval >0: number of bytes read
1592 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1594 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1595 int packet_size = ftdi->max_packet_size;
1596 int actual_length = 1;
1598 if (ftdi == NULL || ftdi->usb_dev == NULL)
1599 ftdi_error_return(-666, "USB device unavailable");
1601 // Packet size sanity check (avoid division by zero)
1602 if (packet_size == 0)
1603 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1605 // everything we want is still in the readbuffer?
1606 if (size <= ftdi->readbuffer_remaining)
1608 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1611 ftdi->readbuffer_remaining -= size;
1612 ftdi->readbuffer_offset += size;
1614 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1618 // something still in the readbuffer, but not enough to satisfy 'size'?
1619 if (ftdi->readbuffer_remaining != 0)
1621 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1624 offset += ftdi->readbuffer_remaining;
1626 // do the actual USB read
1627 while (offset < size && actual_length > 0)
1629 ftdi->readbuffer_remaining = 0;
1630 ftdi->readbuffer_offset = 0;
1631 /* returns how much received */
1632 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1634 ftdi_error_return(ret, "usb bulk read failed");
1636 if (actual_length > 2)
1638 // skip FTDI status bytes.
1639 // Maybe stored in the future to enable modem use
1640 num_of_chunks = actual_length / packet_size;
1641 chunk_remains = actual_length % packet_size;
1642 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1644 ftdi->readbuffer_offset += 2;
1647 if (actual_length > packet_size - 2)
1649 for (i = 1; i < num_of_chunks; i++)
1650 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1651 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1653 if (chunk_remains > 2)
1655 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1656 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1658 actual_length -= 2*num_of_chunks;
1661 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1664 else if (actual_length <= 2)
1666 // no more data to read?
1669 if (actual_length > 0)
1671 // data still fits in buf?
1672 if (offset+actual_length <= size)
1674 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1675 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1676 offset += actual_length;
1678 /* Did we read exactly the right amount of bytes? */
1680 //printf("read_data exact rem %d offset %d\n",
1681 //ftdi->readbuffer_remaining, offset);
1686 // only copy part of the data or size <= readbuffer_chunksize
1687 int part_size = size-offset;
1688 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1690 ftdi->readbuffer_offset += part_size;
1691 ftdi->readbuffer_remaining = actual_length-part_size;
1692 offset += part_size;
1694 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1695 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1706 Configure read buffer chunk size.
1709 Automatically reallocates the buffer.
1711 \param ftdi pointer to ftdi_context
1712 \param chunksize Chunk size
1715 \retval -1: ftdi context invalid
1717 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1719 unsigned char *new_buf;
1722 ftdi_error_return(-1, "ftdi context invalid");
1724 // Invalidate all remaining data
1725 ftdi->readbuffer_offset = 0;
1726 ftdi->readbuffer_remaining = 0;
1728 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1729 which is defined in libusb-1.0. Otherwise, each USB read request will
1730 be divided into multiple URBs. This will cause issues on Linux kernel
1731 older than 2.6.32. */
1732 if (chunksize > 16384)
1736 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1737 ftdi_error_return(-1, "out of memory for readbuffer");
1739 ftdi->readbuffer = new_buf;
1740 ftdi->readbuffer_chunksize = chunksize;
1746 Get read buffer chunk size.
1748 \param ftdi pointer to ftdi_context
1749 \param chunksize Pointer to store chunk size in
1752 \retval -1: FTDI context invalid
1754 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1757 ftdi_error_return(-1, "FTDI context invalid");
1759 *chunksize = ftdi->readbuffer_chunksize;
1765 Enable bitbang mode.
1767 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1769 \param ftdi pointer to ftdi_context
1770 \param bitmask Bitmask to configure lines.
1771 HIGH/ON value configures a line as output.
1774 \retval -1: can't enable bitbang mode
1775 \retval -2: USB device unavailable
1777 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1779 unsigned short usb_val;
1781 if (ftdi == NULL || ftdi->usb_dev == NULL)
1782 ftdi_error_return(-2, "USB device unavailable");
1784 usb_val = bitmask; // low byte: bitmask
1785 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1786 usb_val |= (ftdi->bitbang_mode << 8);
1788 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1789 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1790 NULL, 0, ftdi->usb_write_timeout) < 0)
1791 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1793 ftdi->bitbang_enabled = 1;
1798 Disable bitbang mode.
1800 \param ftdi pointer to ftdi_context
1803 \retval -1: can't disable bitbang mode
1804 \retval -2: USB device unavailable
1806 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1808 if (ftdi == NULL || ftdi->usb_dev == NULL)
1809 ftdi_error_return(-2, "USB device unavailable");
1811 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1812 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1814 ftdi->bitbang_enabled = 0;
1819 Enable/disable bitbang modes.
1821 \param ftdi pointer to ftdi_context
1822 \param bitmask Bitmask to configure lines.
1823 HIGH/ON value configures a line as output.
1824 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1827 \retval -1: can't enable bitbang mode
1828 \retval -2: USB device unavailable
1830 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1832 unsigned short usb_val;
1834 if (ftdi == NULL || ftdi->usb_dev == NULL)
1835 ftdi_error_return(-2, "USB device unavailable");
1837 usb_val = bitmask; // low byte: bitmask
1838 usb_val |= (mode << 8);
1839 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1840 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1842 ftdi->bitbang_mode = mode;
1843 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1848 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1850 \param ftdi pointer to ftdi_context
1851 \param pins Pointer to store pins into
1854 \retval -1: read pins failed
1855 \retval -2: USB device unavailable
1857 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1859 if (ftdi == NULL || ftdi->usb_dev == NULL)
1860 ftdi_error_return(-2, "USB device unavailable");
1862 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1)
1863 ftdi_error_return(-1, "read pins failed");
1871 The FTDI chip keeps data in the internal buffer for a specific
1872 amount of time if the buffer is not full yet to decrease
1873 load on the usb bus.
1875 \param ftdi pointer to ftdi_context
1876 \param latency Value between 1 and 255
1879 \retval -1: latency out of range
1880 \retval -2: unable to set latency timer
1881 \retval -3: USB device unavailable
1883 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1885 unsigned short usb_val;
1888 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1890 if (ftdi == NULL || ftdi->usb_dev == NULL)
1891 ftdi_error_return(-3, "USB device unavailable");
1894 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1895 ftdi_error_return(-2, "unable to set latency timer");
1903 \param ftdi pointer to ftdi_context
1904 \param latency Pointer to store latency value in
1907 \retval -1: unable to get latency timer
1908 \retval -2: USB device unavailable
1910 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1912 unsigned short usb_val;
1914 if (ftdi == NULL || ftdi->usb_dev == NULL)
1915 ftdi_error_return(-2, "USB device unavailable");
1917 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1918 ftdi_error_return(-1, "reading latency timer failed");
1920 *latency = (unsigned char)usb_val;
1925 Poll modem status information
1927 This function allows the retrieve the two status bytes of the device.
1928 The device sends these bytes also as a header for each read access
1929 where they are discarded by ftdi_read_data(). The chip generates
1930 the two stripped status bytes in the absence of data every 40 ms.
1932 Layout of the first byte:
1933 - B0..B3 - must be 0
1934 - B4 Clear to send (CTS)
1937 - B5 Data set ready (DTS)
1940 - B6 Ring indicator (RI)
1943 - B7 Receive line signal detect (RLSD)
1947 Layout of the second byte:
1948 - B0 Data ready (DR)
1949 - B1 Overrun error (OE)
1950 - B2 Parity error (PE)
1951 - B3 Framing error (FE)
1952 - B4 Break interrupt (BI)
1953 - B5 Transmitter holding register (THRE)
1954 - B6 Transmitter empty (TEMT)
1955 - B7 Error in RCVR FIFO
1957 \param ftdi pointer to ftdi_context
1958 \param status Pointer to store status information in. Must be two bytes.
1961 \retval -1: unable to retrieve status information
1962 \retval -2: USB device unavailable
1964 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1968 if (ftdi == NULL || ftdi->usb_dev == NULL)
1969 ftdi_error_return(-2, "USB device unavailable");
1971 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
1972 ftdi_error_return(-1, "getting modem status failed");
1974 *status = (usb_val[1] << 8) | usb_val[0];
1980 Set flowcontrol for ftdi chip
1982 \param ftdi pointer to ftdi_context
1983 \param flowctrl flow control to use. should be
1984 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1987 \retval -1: set flow control failed
1988 \retval -2: USB device unavailable
1990 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1992 if (ftdi == NULL || ftdi->usb_dev == NULL)
1993 ftdi_error_return(-2, "USB device unavailable");
1995 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1996 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1997 NULL, 0, ftdi->usb_write_timeout) < 0)
1998 ftdi_error_return(-1, "set flow control failed");
2006 \param ftdi pointer to ftdi_context
2007 \param state state to set line to (1 or 0)
2010 \retval -1: set dtr failed
2011 \retval -2: USB device unavailable
2013 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2015 unsigned short usb_val;
2017 if (ftdi == NULL || ftdi->usb_dev == NULL)
2018 ftdi_error_return(-2, "USB device unavailable");
2021 usb_val = SIO_SET_DTR_HIGH;
2023 usb_val = SIO_SET_DTR_LOW;
2025 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2026 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2027 NULL, 0, ftdi->usb_write_timeout) < 0)
2028 ftdi_error_return(-1, "set dtr failed");
2036 \param ftdi pointer to ftdi_context
2037 \param state state to set line to (1 or 0)
2040 \retval -1: set rts failed
2041 \retval -2: USB device unavailable
2043 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2045 unsigned short usb_val;
2047 if (ftdi == NULL || ftdi->usb_dev == NULL)
2048 ftdi_error_return(-2, "USB device unavailable");
2051 usb_val = SIO_SET_RTS_HIGH;
2053 usb_val = SIO_SET_RTS_LOW;
2055 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2056 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2057 NULL, 0, ftdi->usb_write_timeout) < 0)
2058 ftdi_error_return(-1, "set of rts failed");
2064 Set dtr and rts line in one pass
2066 \param ftdi pointer to ftdi_context
2067 \param dtr DTR state to set line to (1 or 0)
2068 \param rts RTS state to set line to (1 or 0)
2071 \retval -1: set dtr/rts failed
2072 \retval -2: USB device unavailable
2074 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2076 unsigned short usb_val;
2078 if (ftdi == NULL || ftdi->usb_dev == NULL)
2079 ftdi_error_return(-2, "USB device unavailable");
2082 usb_val = SIO_SET_DTR_HIGH;
2084 usb_val = SIO_SET_DTR_LOW;
2087 usb_val |= SIO_SET_RTS_HIGH;
2089 usb_val |= SIO_SET_RTS_LOW;
2091 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2092 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2093 NULL, 0, ftdi->usb_write_timeout) < 0)
2094 ftdi_error_return(-1, "set of rts/dtr failed");
2100 Set the special event character
2102 \param ftdi pointer to ftdi_context
2103 \param eventch Event character
2104 \param enable 0 to disable the event character, non-zero otherwise
2107 \retval -1: unable to set event character
2108 \retval -2: USB device unavailable
2110 int ftdi_set_event_char(struct ftdi_context *ftdi,
2111 unsigned char eventch, unsigned char enable)
2113 unsigned short usb_val;
2115 if (ftdi == NULL || ftdi->usb_dev == NULL)
2116 ftdi_error_return(-2, "USB device unavailable");
2122 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2123 ftdi_error_return(-1, "setting event character failed");
2131 \param ftdi pointer to ftdi_context
2132 \param errorch Error character
2133 \param enable 0 to disable the error character, non-zero otherwise
2136 \retval -1: unable to set error character
2137 \retval -2: USB device unavailable
2139 int ftdi_set_error_char(struct ftdi_context *ftdi,
2140 unsigned char errorch, unsigned char enable)
2142 unsigned short usb_val;
2144 if (ftdi == NULL || ftdi->usb_dev == NULL)
2145 ftdi_error_return(-2, "USB device unavailable");
2151 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2152 ftdi_error_return(-1, "setting error character failed");
2160 \param ftdi pointer to ftdi_context
2161 \param eeprom Pointer to ftdi_eeprom
2165 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2170 ftdi->eeprom_size=size;
2175 Init eeprom with default values.
2177 \param eeprom Pointer to ftdi_eeprom
2179 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2184 eeprom->vendor_id = 0x0403;
2185 eeprom->product_id = 0x6001;
2187 eeprom->self_powered = 1;
2188 eeprom->remote_wakeup = 1;
2189 eeprom->BM_type_chip = 1;
2191 eeprom->in_is_isochronous = 0;
2192 eeprom->out_is_isochronous = 0;
2193 eeprom->suspend_pull_downs = 0;
2195 eeprom->use_serial = 0;
2196 eeprom->change_usb_version = 0;
2197 eeprom->usb_version = 0x0200;
2198 eeprom->max_power = 0;
2200 eeprom->manufacturer = NULL;
2201 eeprom->product = NULL;
2202 eeprom->serial = NULL;
2204 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
2208 Frees allocated memory in eeprom.
2210 \param eeprom Pointer to ftdi_eeprom
2212 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2214 if (eeprom->manufacturer != 0) {
2215 free(eeprom->manufacturer);
2216 eeprom->manufacturer = 0;
2218 if (eeprom->product != 0) {
2219 free(eeprom->product);
2220 eeprom->product = 0;
2222 if (eeprom->serial != 0) {
2223 free(eeprom->serial);
2229 Build binary output from ftdi_eeprom structure.
2230 Output is suitable for ftdi_write_eeprom().
2232 \param eeprom Pointer to ftdi_eeprom
2233 \param output Buffer of 128 bytes to store eeprom image to
2235 \retval >0: used eeprom size
2236 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2237 \retval -2: Invalid eeprom pointer
2239 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2242 unsigned short checksum, value;
2243 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2249 if (eeprom->manufacturer != NULL)
2250 manufacturer_size = strlen(eeprom->manufacturer);
2251 if (eeprom->product != NULL)
2252 product_size = strlen(eeprom->product);
2253 if (eeprom->serial != NULL)
2254 serial_size = strlen(eeprom->serial);
2256 size_check = eeprom->size;
2257 size_check -= 28; // 28 are always in use (fixed)
2259 // Top half of a 256byte eeprom is used just for strings and checksum
2260 // it seems that the FTDI chip will not read these strings from the lower half
2261 // Each string starts with two bytes; offset and type (0x03 for string)
2262 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2263 if (eeprom->size>=256)size_check = 120;
2264 size_check -= manufacturer_size*2;
2265 size_check -= product_size*2;
2266 size_check -= serial_size*2;
2268 // eeprom size exceeded?
2273 memset (output, 0, eeprom->size);
2275 // Addr 00: Stay 00 00
2276 // Addr 02: Vendor ID
2277 output[0x02] = eeprom->vendor_id;
2278 output[0x03] = eeprom->vendor_id >> 8;
2280 // Addr 04: Product ID
2281 output[0x04] = eeprom->product_id;
2282 output[0x05] = eeprom->product_id >> 8;
2284 // Addr 06: Device release number (0400h for BM features)
2285 output[0x06] = 0x00;
2287 if (eeprom->BM_type_chip == 1)
2288 output[0x07] = 0x04;
2290 output[0x07] = 0x02;
2292 // Addr 08: Config descriptor
2294 // Bit 6: 1 if this device is self powered, 0 if bus powered
2295 // Bit 5: 1 if this device uses remote wakeup
2296 // Bit 4: 1 if this device is battery powered
2298 if (eeprom->self_powered == 1)
2300 if (eeprom->remote_wakeup == 1)
2304 // Addr 09: Max power consumption: max power = value * 2 mA
2305 output[0x09] = eeprom->max_power;
2307 // Addr 0A: Chip configuration
2308 // Bit 7: 0 - reserved
2309 // Bit 6: 0 - reserved
2310 // Bit 5: 0 - reserved
2311 // Bit 4: 1 - Change USB version
2312 // Bit 3: 1 - Use the serial number string
2313 // Bit 2: 1 - Enable suspend pull downs for lower power
2314 // Bit 1: 1 - Out EndPoint is Isochronous
2315 // Bit 0: 1 - In EndPoint is Isochronous
2318 if (eeprom->in_is_isochronous == 1)
2320 if (eeprom->out_is_isochronous == 1)
2322 if (eeprom->suspend_pull_downs == 1)
2324 if (eeprom->use_serial == 1)
2326 if (eeprom->change_usb_version == 1)
2330 // Addr 0B: reserved
2331 output[0x0B] = 0x00;
2333 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2334 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2335 if (eeprom->change_usb_version == 1)
2337 output[0x0C] = eeprom->usb_version;
2338 output[0x0D] = eeprom->usb_version >> 8;
2342 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2343 // Addr 0F: Length of manufacturer string
2344 output[0x0F] = manufacturer_size*2 + 2;
2346 // Addr 10: Offset of the product string + 0x80, calculated later
2347 // Addr 11: Length of product string
2348 output[0x11] = product_size*2 + 2;
2350 // Addr 12: Offset of the serial string + 0x80, calculated later
2351 // Addr 13: Length of serial string
2352 output[0x13] = serial_size*2 + 2;
2356 if (eeprom->size>=256) i = 0x80;
2359 // Output manufacturer
2360 output[0x0E] = i | 0x80; // calculate offset
2361 output[i++] = manufacturer_size*2 + 2;
2362 output[i++] = 0x03; // type: string
2363 for (j = 0; j < manufacturer_size; j++)
2365 output[i] = eeprom->manufacturer[j], i++;
2366 output[i] = 0x00, i++;
2369 // Output product name
2370 output[0x10] = i | 0x80; // calculate offset
2371 output[i] = product_size*2 + 2, i++;
2372 output[i] = 0x03, i++;
2373 for (j = 0; j < product_size; j++)
2375 output[i] = eeprom->product[j], i++;
2376 output[i] = 0x00, i++;
2380 output[0x12] = i | 0x80; // calculate offset
2381 output[i] = serial_size*2 + 2, i++;
2382 output[i] = 0x03, i++;
2383 for (j = 0; j < serial_size; j++)
2385 output[i] = eeprom->serial[j], i++;
2386 output[i] = 0x00, i++;
2389 // calculate checksum
2392 for (i = 0; i < eeprom->size/2-1; i++)
2394 value = output[i*2];
2395 value += output[(i*2)+1] << 8;
2397 checksum = value^checksum;
2398 checksum = (checksum << 1) | (checksum >> 15);
2401 output[eeprom->size-2] = checksum;
2402 output[eeprom->size-1] = checksum >> 8;
2408 Decode binary EEPROM image into an ftdi_eeprom structure.
2410 \param eeprom Pointer to ftdi_eeprom which will be filled in.
2411 \param buf Buffer of \a size bytes of raw eeprom data
2412 \param size size size of eeprom data in bytes
2415 \retval -1: something went wrong
2417 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2418 FIXME: Strings are malloc'ed here and should be freed somewhere
2420 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
2423 unsigned short checksum, eeprom_checksum, value;
2424 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2425 int eeprom_size = 128;
2430 size_check = eeprom->size;
2431 size_check -= 28; // 28 are always in use (fixed)
2433 // Top half of a 256byte eeprom is used just for strings and checksum
2434 // it seems that the FTDI chip will not read these strings from the lower half
2435 // Each string starts with two bytes; offset and type (0x03 for string)
2436 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
2437 if (eeprom->size>=256)size_check = 120;
2438 size_check -= manufacturer_size*2;
2439 size_check -= product_size*2;
2440 size_check -= serial_size*2;
2442 // eeprom size exceeded?
2447 // empty eeprom struct
2448 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2450 // Addr 00: Stay 00 00
2452 // Addr 02: Vendor ID
2453 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2455 // Addr 04: Product ID
2456 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2458 value = buf[0x06] + (buf[0x07]<<8);
2462 eeprom->BM_type_chip = 1;
2465 eeprom->BM_type_chip = 0;
2467 default: // Unknown device
2468 eeprom->BM_type_chip = 0;
2472 // Addr 08: Config descriptor
2474 // Bit 6: 1 if this device is self powered, 0 if bus powered
2475 // Bit 5: 1 if this device uses remote wakeup
2476 // Bit 4: 1 if this device is battery powered
2478 if (j&0x40) eeprom->self_powered = 1;
2479 if (j&0x20) eeprom->remote_wakeup = 1;
2481 // Addr 09: Max power consumption: max power = value * 2 mA
2482 eeprom->max_power = buf[0x09];
2484 // Addr 0A: Chip configuration
2485 // Bit 7: 0 - reserved
2486 // Bit 6: 0 - reserved
2487 // Bit 5: 0 - reserved
2488 // Bit 4: 1 - Change USB version
2489 // Bit 3: 1 - Use the serial number string
2490 // Bit 2: 1 - Enable suspend pull downs for lower power
2491 // Bit 1: 1 - Out EndPoint is Isochronous
2492 // Bit 0: 1 - In EndPoint is Isochronous
2495 if (j&0x01) eeprom->in_is_isochronous = 1;
2496 if (j&0x02) eeprom->out_is_isochronous = 1;
2497 if (j&0x04) eeprom->suspend_pull_downs = 1;
2498 if (j&0x08) eeprom->use_serial = 1;
2499 if (j&0x10) eeprom->change_usb_version = 1;
2501 // Addr 0B: reserved
2503 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2504 // Addr 0D: USB version high byte when 0x0A bit 4 is set
2505 if (eeprom->change_usb_version == 1)
2507 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2510 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2511 // Addr 0F: Length of manufacturer string
2512 manufacturer_size = buf[0x0F]/2;
2513 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2514 else eeprom->manufacturer = NULL;
2516 // Addr 10: Offset of the product string + 0x80, calculated later
2517 // Addr 11: Length of product string
2518 product_size = buf[0x11]/2;
2519 if (product_size > 0) eeprom->product = malloc(product_size);
2520 else eeprom->product = NULL;
2522 // Addr 12: Offset of the serial string + 0x80, calculated later
2523 // Addr 13: Length of serial string
2524 serial_size = buf[0x13]/2;
2525 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2526 else eeprom->serial = NULL;
2528 // Decode manufacturer
2529 i = buf[0x0E] & 0x7f; // offset
2530 for (j=0;j<manufacturer_size-1;j++)
2532 eeprom->manufacturer[j] = buf[2*j+i+2];
2534 eeprom->manufacturer[j] = '\0';
2536 // Decode product name
2537 i = buf[0x10] & 0x7f; // offset
2538 for (j=0;j<product_size-1;j++)
2540 eeprom->product[j] = buf[2*j+i+2];
2542 eeprom->product[j] = '\0';
2545 i = buf[0x12] & 0x7f; // offset
2546 for (j=0;j<serial_size-1;j++)
2548 eeprom->serial[j] = buf[2*j+i+2];
2550 eeprom->serial[j] = '\0';
2555 for (i = 0; i < eeprom_size/2-1; i++)
2558 value += buf[(i*2)+1] << 8;
2560 checksum = value^checksum;
2561 checksum = (checksum << 1) | (checksum >> 15);
2564 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2566 if (eeprom_checksum != checksum)
2568 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2576 Read eeprom location
2578 \param ftdi pointer to ftdi_context
2579 \param eeprom_addr Address of eeprom location to be read
2580 \param eeprom_val Pointer to store read eeprom location
2583 \retval -1: read failed
2584 \retval -2: USB device unavailable
2586 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2588 if (ftdi == NULL || ftdi->usb_dev == NULL)
2589 ftdi_error_return(-2, "USB device unavailable");
2591 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
2592 ftdi_error_return(-1, "reading eeprom failed");
2600 \param ftdi pointer to ftdi_context
2601 \param eeprom Pointer to store eeprom into
2604 \retval -1: read failed
2605 \retval -2: USB device unavailable
2607 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2611 if (ftdi == NULL || ftdi->usb_dev == NULL)
2612 ftdi_error_return(-2, "USB device unavailable");
2614 for (i = 0; i < ftdi->eeprom_size/2; i++)
2616 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2617 ftdi_error_return(-1, "reading eeprom failed");
2624 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2625 Function is only used internally
2628 static unsigned char ftdi_read_chipid_shift(unsigned char value)
2630 return ((value & 1) << 1) |
2631 ((value & 2) << 5) |
2632 ((value & 4) >> 2) |
2633 ((value & 8) << 4) |
2634 ((value & 16) >> 1) |
2635 ((value & 32) >> 1) |
2636 ((value & 64) >> 4) |
2637 ((value & 128) >> 2);
2641 Read the FTDIChip-ID from R-type devices
2643 \param ftdi pointer to ftdi_context
2644 \param chipid Pointer to store FTDIChip-ID
2647 \retval -1: read failed
2648 \retval -2: USB device unavailable
2650 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2652 unsigned int a = 0, b = 0;
2654 if (ftdi == NULL || ftdi->usb_dev == NULL)
2655 ftdi_error_return(-2, "USB device unavailable");
2657 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2)
2659 a = a << 8 | a >> 8;
2660 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2)
2662 b = b << 8 | b >> 8;
2663 a = (a << 16) | (b & 0xFFFF);
2664 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2665 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2666 *chipid = a ^ 0xa5f0f7d1;
2671 ftdi_error_return(-1, "read of FTDIChip-ID failed");
2675 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2676 Call this function then do a write then call again to see if size changes, if so write again.
2678 \param ftdi pointer to ftdi_context
2679 \param eeprom Pointer to store eeprom into
2680 \param maxsize the size of the buffer to read into
2682 \retval -1: eeprom read failed
2683 \retval -2: USB device unavailable
2684 \retval >=0: size of eeprom
2686 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2688 int i=0,j,minsize=32;
2691 if (ftdi == NULL || ftdi->usb_dev == NULL)
2692 ftdi_error_return(-2, "USB device unavailable");
2696 for (j = 0; i < maxsize/2 && j<size; j++)
2698 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2699 SIO_READ_EEPROM_REQUEST, 0, i,
2700 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2701 ftdi_error_return(-1, "eeprom read failed");
2706 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2712 Write eeprom location
2714 \param ftdi pointer to ftdi_context
2715 \param eeprom_addr Address of eeprom location to be written
2716 \param eeprom_val Value to be written
2719 \retval -1: read failed
2720 \retval -2: USB device unavailable
2722 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2724 if (ftdi == NULL || ftdi->usb_dev == NULL)
2725 ftdi_error_return(-2, "USB device unavailable");
2727 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2728 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2729 NULL, 0, ftdi->usb_write_timeout) != 0)
2730 ftdi_error_return(-1, "unable to write eeprom");
2738 \param ftdi pointer to ftdi_context
2739 \param eeprom Pointer to read eeprom from
2742 \retval -1: read failed
2743 \retval -2: USB device unavailable
2745 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2747 unsigned short usb_val, status;
2750 if (ftdi == NULL || ftdi->usb_dev == NULL)
2751 ftdi_error_return(-2, "USB device unavailable");
2753 /* These commands were traced while running MProg */
2754 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2756 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2758 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2761 for (i = 0; i < ftdi->eeprom_size/2; i++)
2763 usb_val = eeprom[i*2];
2764 usb_val += eeprom[(i*2)+1] << 8;
2765 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2766 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2767 NULL, 0, ftdi->usb_write_timeout) < 0)
2768 ftdi_error_return(-1, "unable to write eeprom");
2777 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2779 \param ftdi pointer to ftdi_context
2782 \retval -1: erase failed
2783 \retval -2: USB device unavailable
2785 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2787 if (ftdi == NULL || ftdi->usb_dev == NULL)
2788 ftdi_error_return(-2, "USB device unavailable");
2790 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
2791 ftdi_error_return(-1, "unable to erase eeprom");
2797 Get string representation for last error code
2799 \param ftdi pointer to ftdi_context
2801 \retval Pointer to error string
2803 char *ftdi_get_error_string (struct ftdi_context *ftdi)
2808 return ftdi->error_str;
2811 /* @} end of doxygen libftdi group */