1 /***************************************************************************
5 copyright : (C) 2003-2008 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/de/produkte/opensource/ftdi/
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 */
37 /* stuff needed for async write */
38 #ifdef LIBFTDI_LINUX_ASYNC_MODE
39 #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 Initializes a ftdi_context.
56 \param ftdi pointer to ftdi_context
59 \retval -1: couldn't allocate read buffer
61 \remark This should be called before all functions
63 int ftdi_init(struct ftdi_context *ftdi)
68 ftdi->usb_read_timeout = 5000;
69 ftdi->usb_write_timeout = 5000;
71 ftdi->type = TYPE_BM; /* chip type */
73 ftdi->bitbang_enabled = 0;
75 ftdi->readbuffer = NULL;
76 ftdi->readbuffer_offset = 0;
77 ftdi->readbuffer_remaining = 0;
78 ftdi->writebuffer_chunksize = 4096;
84 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
86 ftdi->error_str = NULL;
88 #ifdef LIBFTDI_LINUX_ASYNC_MODE
89 ftdi->async_usb_buffer_size=10;
90 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
91 ftdi_error_return(-1, "out of memory for async usb buffer");
93 /* initialize async usb buffer with unused-marker */
94 for (i=0; i < ftdi->async_usb_buffer_size; i++)
95 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
97 ftdi->async_usb_buffer_size=0;
98 ftdi->async_usb_buffer = NULL;
101 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
103 /* All fine. Now allocate the readbuffer */
104 return ftdi_read_data_set_chunksize(ftdi, 4096);
108 Allocate and initialize a new ftdi_context
110 \return a pointer to a new ftdi_context, or NULL on failure
112 struct ftdi_context *ftdi_new()
114 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
120 if (ftdi_init(ftdi) != 0) {
129 Open selected channels on a chip, otherwise use first channel.
131 \param ftdi pointer to ftdi_context
132 \param interface Interface to use for FT2232C chips.
135 \retval -1: unknown interface
137 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
142 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
146 ftdi->index = INTERFACE_B;
151 ftdi_error_return(-1, "Unknown interface");
157 Deinitializes a ftdi_context.
159 \param ftdi pointer to ftdi_context
161 void ftdi_deinit(struct ftdi_context *ftdi)
163 if (ftdi->async_usb_buffer != NULL) {
164 free(ftdi->async_usb_buffer);
165 ftdi->async_usb_buffer = NULL;
168 if (ftdi->readbuffer != NULL) {
169 free(ftdi->readbuffer);
170 ftdi->readbuffer = NULL;
175 Deinitialize and free an ftdi_context.
177 \param ftdi pointer to ftdi_context
179 void ftdi_free(struct ftdi_context *ftdi)
186 Use an already open libusb device.
188 \param ftdi pointer to ftdi_context
189 \param usb libusb usb_dev_handle to use
191 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
198 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
199 needs to be deallocated by ftdi_list_free() after use.
201 \param ftdi pointer to ftdi_context
202 \param devlist Pointer where to store list of found devices
203 \param vendor Vendor ID to search for
204 \param product Product ID to search for
206 \retval >0: number of devices found
207 \retval -1: usb_find_busses() failed
208 \retval -2: usb_find_devices() failed
209 \retval -3: out of memory
211 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
213 struct ftdi_device_list **curdev;
215 struct usb_device *dev;
219 if (usb_find_busses() < 0)
220 ftdi_error_return(-1, "usb_find_busses() failed");
221 if (usb_find_devices() < 0)
222 ftdi_error_return(-2, "usb_find_devices() failed");
226 for (bus = usb_get_busses(); bus; bus = bus->next) {
227 for (dev = bus->devices; dev; dev = dev->next) {
228 if (dev->descriptor.idVendor == vendor
229 && dev->descriptor.idProduct == product)
231 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
233 ftdi_error_return(-3, "out of memory");
235 (*curdev)->next = NULL;
236 (*curdev)->dev = dev;
238 curdev = &(*curdev)->next;
248 Frees a usb device list.
250 \param devlist USB device list created by ftdi_usb_find_all()
252 void ftdi_list_free(struct ftdi_device_list **devlist)
254 struct ftdi_device_list *curdev, *next;
256 for (curdev = *devlist; curdev != NULL;) {
266 Frees a usb device list.
268 \param devlist USB device list created by ftdi_usb_find_all()
270 void ftdi_list_free2(struct ftdi_device_list *devlist)
272 ftdi_list_free(&devlist);
276 Return device ID strings from the usb device.
278 The parameters manufacturer, description and serial may be NULL
279 or pointer to buffers to store the fetched strings.
281 \note Use this function only in combination with ftdi_usb_find_all()
282 as it closes the internal "usb_dev" after use.
284 \param ftdi pointer to ftdi_context
285 \param dev libusb usb_dev to use
286 \param manufacturer Store manufacturer string here if not NULL
287 \param mnf_len Buffer size of manufacturer string
288 \param description Store product description string here if not NULL
289 \param desc_len Buffer size of product description string
290 \param serial Store serial string here if not NULL
291 \param serial_len Buffer size of serial string
294 \retval -1: wrong arguments
295 \retval -4: unable to open device
296 \retval -7: get product manufacturer failed
297 \retval -8: get product description failed
298 \retval -9: get serial number failed
299 \retval -10: unable to close device
301 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
302 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
304 if ((ftdi==NULL) || (dev==NULL))
307 if (!(ftdi->usb_dev = usb_open(dev)))
308 ftdi_error_return(-4, usb_strerror());
310 if (manufacturer != NULL) {
311 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) {
312 usb_close (ftdi->usb_dev);
313 ftdi_error_return(-7, usb_strerror());
317 if (description != NULL) {
318 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) {
319 usb_close (ftdi->usb_dev);
320 ftdi_error_return(-8, usb_strerror());
324 if (serial != NULL) {
325 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) {
326 usb_close (ftdi->usb_dev);
327 ftdi_error_return(-9, usb_strerror());
331 if (usb_close (ftdi->usb_dev) != 0)
332 ftdi_error_return(-10, usb_strerror());
338 Opens a ftdi device given by a usb_device.
340 \param ftdi pointer to ftdi_context
341 \param dev libusb usb_dev to use
344 \retval -3: unable to config device
345 \retval -4: unable to open device
346 \retval -5: unable to claim device
347 \retval -6: reset failed
348 \retval -7: set baudrate failed
350 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
352 int detach_errno = 0;
353 if (!(ftdi->usb_dev = usb_open(dev)))
354 ftdi_error_return(-4, "usb_open() failed");
356 #ifdef LIBUSB_HAS_GET_DRIVER_NP
357 // Try to detach ftdi_sio kernel module
358 // Returns ENODATA if driver is not loaded
359 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
360 detach_errno = errno;
363 if (usb_set_configuration(ftdi->usb_dev, dev->config[0].bConfigurationValue)) {
364 usb_close (ftdi->usb_dev);
365 if (detach_errno == EPERM) {
366 ftdi_error_return(-8, "inappropriate permissions on device!");
368 ftdi_error_return(-3, "unable to set usb configuration. Make sure ftdi_sio is unloaded!");
372 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
373 usb_close (ftdi->usb_dev);
374 if (detach_errno == EPERM) {
375 ftdi_error_return(-8, "inappropriate permissions on device!");
377 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
381 if (ftdi_usb_reset (ftdi) != 0) {
382 usb_close (ftdi->usb_dev);
383 ftdi_error_return(-6, "ftdi_usb_reset failed");
386 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
387 usb_close (ftdi->usb_dev);
388 ftdi_error_return(-7, "set baudrate failed");
391 // Try to guess chip type
392 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
393 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
394 && dev->descriptor.iSerialNumber == 0))
395 ftdi->type = TYPE_BM;
396 else if (dev->descriptor.bcdDevice == 0x200)
397 ftdi->type = TYPE_AM;
398 else if (dev->descriptor.bcdDevice == 0x500) {
399 ftdi->type = TYPE_2232C;
401 ftdi->index = INTERFACE_A;
402 } else if (dev->descriptor.bcdDevice == 0x600)
405 ftdi_error_return(0, "all fine");
409 Opens the first device with a given vendor and product ids.
411 \param ftdi pointer to ftdi_context
412 \param vendor Vendor ID
413 \param product Product ID
415 \retval same as ftdi_usb_open_desc()
417 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
419 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
423 Opens the first device with a given, vendor id, product id,
424 description and serial.
426 \param ftdi pointer to ftdi_context
427 \param vendor Vendor ID
428 \param product Product ID
429 \param description Description to search for. Use NULL if not needed.
430 \param serial Serial to search for. Use NULL if not needed.
433 \retval -1: usb_find_busses() failed
434 \retval -2: usb_find_devices() failed
435 \retval -3: usb device not found
436 \retval -4: unable to open device
437 \retval -5: unable to claim device
438 \retval -6: reset failed
439 \retval -7: set baudrate failed
440 \retval -8: get product description failed
441 \retval -9: get serial number failed
442 \retval -10: unable to close device
444 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
445 const char* description, const char* serial)
448 struct usb_device *dev;
453 if (usb_find_busses() < 0)
454 ftdi_error_return(-1, "usb_find_busses() failed");
455 if (usb_find_devices() < 0)
456 ftdi_error_return(-2, "usb_find_devices() failed");
458 for (bus = usb_get_busses(); bus; bus = bus->next) {
459 for (dev = bus->devices; dev; dev = dev->next) {
460 if (dev->descriptor.idVendor == vendor
461 && dev->descriptor.idProduct == product) {
462 if (!(ftdi->usb_dev = usb_open(dev)))
463 ftdi_error_return(-4, "usb_open() failed");
465 if (description != NULL) {
466 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
467 usb_close (ftdi->usb_dev);
468 ftdi_error_return(-8, "unable to fetch product description");
470 if (strncmp(string, description, sizeof(string)) != 0) {
471 if (usb_close (ftdi->usb_dev) != 0)
472 ftdi_error_return(-10, "unable to close device");
476 if (serial != NULL) {
477 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
478 usb_close (ftdi->usb_dev);
479 ftdi_error_return(-9, "unable to fetch serial number");
481 if (strncmp(string, serial, sizeof(string)) != 0) {
482 if (usb_close (ftdi->usb_dev) != 0)
483 ftdi_error_return(-10, "unable to close device");
488 if (usb_close (ftdi->usb_dev) != 0)
489 ftdi_error_return(-10, "unable to close device");
491 return ftdi_usb_open_dev(ftdi, dev);
497 ftdi_error_return(-3, "device not found");
501 Resets the ftdi device.
503 \param ftdi pointer to ftdi_context
506 \retval -1: FTDI reset failed
508 int ftdi_usb_reset(struct ftdi_context *ftdi)
510 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
511 ftdi_error_return(-1,"FTDI reset failed");
513 // Invalidate data in the readbuffer
514 ftdi->readbuffer_offset = 0;
515 ftdi->readbuffer_remaining = 0;
521 Clears the read buffer on the chip and the internal read buffer.
523 \param ftdi pointer to ftdi_context
526 \retval -1: read buffer purge failed
528 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
530 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
531 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
533 // Invalidate data in the readbuffer
534 ftdi->readbuffer_offset = 0;
535 ftdi->readbuffer_remaining = 0;
541 Clears the write buffer on the chip.
543 \param ftdi pointer to ftdi_context
546 \retval -1: write buffer purge failed
548 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
550 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
551 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
557 Clears the buffers on the chip and the internal read buffer.
559 \param ftdi pointer to ftdi_context
562 \retval -1: read buffer purge failed
563 \retval -2: write buffer purge failed
565 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
569 result = ftdi_usb_purge_rx_buffer(ftdi);
573 result = ftdi_usb_purge_tx_buffer(ftdi);
581 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
583 \param ftdi pointer to ftdi_context
586 \retval -1: usb_release failed
587 \retval -2: usb_close failed
589 int ftdi_usb_close(struct ftdi_context *ftdi)
593 #ifdef LIBFTDI_LINUX_ASYNC_MODE
594 /* try to release some kernel resources */
595 ftdi_async_complete(ftdi,1);
598 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
601 if (usb_close (ftdi->usb_dev) != 0)
608 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
609 Function is only used internally
612 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
613 unsigned short *value, unsigned short *index)
615 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
616 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
617 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
618 int divisor, best_divisor, best_baud, best_baud_diff;
619 unsigned long encoded_divisor;
627 divisor = 24000000 / baudrate;
629 if (ftdi->type == TYPE_AM) {
630 // Round down to supported fraction (AM only)
631 divisor -= am_adjust_dn[divisor & 7];
634 // Try this divisor and the one above it (because division rounds down)
638 for (i = 0; i < 2; i++) {
639 int try_divisor = divisor + i;
643 // Round up to supported divisor value
644 if (try_divisor <= 8) {
645 // Round up to minimum supported divisor
647 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
648 // BM doesn't support divisors 9 through 11 inclusive
650 } else if (divisor < 16) {
651 // AM doesn't support divisors 9 through 15 inclusive
654 if (ftdi->type == TYPE_AM) {
655 // Round up to supported fraction (AM only)
656 try_divisor += am_adjust_up[try_divisor & 7];
657 if (try_divisor > 0x1FFF8) {
658 // Round down to maximum supported divisor value (for AM)
659 try_divisor = 0x1FFF8;
662 if (try_divisor > 0x1FFFF) {
663 // Round down to maximum supported divisor value (for BM)
664 try_divisor = 0x1FFFF;
668 // Get estimated baud rate (to nearest integer)
669 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
670 // Get absolute difference from requested baud rate
671 if (baud_estimate < baudrate) {
672 baud_diff = baudrate - baud_estimate;
674 baud_diff = baud_estimate - baudrate;
676 if (i == 0 || baud_diff < best_baud_diff) {
677 // Closest to requested baud rate so far
678 best_divisor = try_divisor;
679 best_baud = baud_estimate;
680 best_baud_diff = baud_diff;
681 if (baud_diff == 0) {
682 // Spot on! No point trying
687 // Encode the best divisor value
688 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
689 // Deal with special cases for encoded value
690 if (encoded_divisor == 1) {
691 encoded_divisor = 0; // 3000000 baud
692 } else if (encoded_divisor == 0x4001) {
693 encoded_divisor = 1; // 2000000 baud (BM only)
695 // Split into "value" and "index" values
696 *value = (unsigned short)(encoded_divisor & 0xFFFF);
697 if(ftdi->type == TYPE_2232C) {
698 *index = (unsigned short)(encoded_divisor >> 8);
700 *index |= ftdi->index;
703 *index = (unsigned short)(encoded_divisor >> 16);
705 // Return the nearest baud rate
710 Sets the chip baud rate
712 \param ftdi pointer to ftdi_context
713 \param baudrate baud rate to set
716 \retval -1: invalid baudrate
717 \retval -2: setting baudrate failed
719 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
721 unsigned short value, index;
724 if (ftdi->bitbang_enabled) {
725 baudrate = baudrate*4;
728 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
729 if (actual_baudrate <= 0)
730 ftdi_error_return (-1, "Silly baudrate <= 0.");
732 // Check within tolerance (about 5%)
733 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
734 || ((actual_baudrate < baudrate)
735 ? (actual_baudrate * 21 < baudrate * 20)
736 : (baudrate * 21 < actual_baudrate * 20)))
737 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
739 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
740 ftdi_error_return (-2, "Setting new baudrate failed");
742 ftdi->baudrate = baudrate;
747 Set (RS232) line characteristics by Alain Abbas
749 \param ftdi pointer to ftdi_context
750 \param bits Number of bits
751 \param sbit Number of stop bits
752 \param parity Parity mode
755 \retval -1: Setting line property failed
757 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
758 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
760 unsigned short value = bits;
764 value |= (0x00 << 8);
767 value |= (0x01 << 8);
770 value |= (0x02 << 8);
773 value |= (0x03 << 8);
776 value |= (0x04 << 8);
782 value |= (0x00 << 11);
785 value |= (0x01 << 11);
788 value |= (0x02 << 11);
792 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
793 ftdi_error_return (-1, "Setting new line property failed");
799 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
801 \param ftdi pointer to ftdi_context
802 \param buf Buffer with the data
803 \param size Size of the buffer
805 \retval <0: error code from usb_bulk_write()
806 \retval >0: number of bytes written
808 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
812 int total_written = 0;
814 while (offset < size) {
815 int write_size = ftdi->writebuffer_chunksize;
817 if (offset+write_size > size)
818 write_size = size-offset;
820 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
822 ftdi_error_return(ret, "usb bulk write failed");
824 total_written += ret;
825 offset += write_size;
828 return total_written;
831 #ifdef LIBFTDI_LINUX_ASYNC_MODE
832 /* this is strongly dependent on libusb using the same struct layout. If libusb
833 changes in some later version this may break horribly (this is for libusb 0.1.12) */
834 struct usb_dev_handle {
836 // some other stuff coming here we don't need
840 Check for pending async urbs
843 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
845 struct usbdevfs_urb *urb;
849 for (i=0; i < ftdi->async_usb_buffer_size; i++) {
850 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
851 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
859 Wait until one or more async URBs are completed by the kernel and mark their
860 positions in the async-buffer as unused
862 \param ftdi pointer to ftdi_context
863 \param wait_for_more if != 0 wait for more than one write to complete
864 \param timeout_msec max milliseconds to wait
868 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
871 struct usbdevfs_urb *urb=NULL;
877 FD_SET(ftdi->usb_dev->fd, &writefds);
879 /* init timeout only once, select writes time left after call */
880 tv.tv_sec = timeout_msec / 1000;
881 tv.tv_usec = (timeout_msec % 1000) * 1000;
884 while (_usb_get_async_urbs_pending(ftdi)
885 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
888 if (keep_going && !wait_for_more) {
889 /* don't wait if repeating only for keep_going */
894 /* wait for timeout msec or something written ready */
895 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
898 if (ret == 0 && urb != NULL) {
899 /* got a free urb, mark it */
900 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
902 /* try to get more urbs that are ready now, but don't wait anymore */
906 /* no more urbs waiting */
909 } while (keep_going);
913 Wait until one or more async URBs are completed by the kernel and mark their
914 positions in the async-buffer as unused.
916 \param ftdi pointer to ftdi_context
917 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
919 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
921 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
925 Stupid libusb does not offer async writes nor does it allow
926 access to its fd - so we need some hacks here.
929 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
931 struct usbdevfs_urb *urb;
932 int bytesdone = 0, requested;
937 /* find a free urb buffer we can use */
939 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
941 if (i==ftdi->async_usb_buffer_size) {
942 /* wait until some buffers are free */
943 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
946 for (i=0; i < ftdi->async_usb_buffer_size; i++) {
947 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
948 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
949 break; /* found a free urb position */
954 /* no free urb position found */
958 requested = size - bytesdone;
959 if (requested > 4096)
962 memset(urb,0,sizeof(urb));
964 urb->type = USBDEVFS_URB_TYPE_BULK;
967 urb->buffer = bytes + bytesdone;
968 urb->buffer_length = requested;
970 urb->actual_length = 0;
971 urb->number_of_packets = 0;
972 urb->usercontext = 0;
975 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
976 } while (ret < 0 && errno == EINTR);
978 return ret; /* the caller can read errno to get more info */
980 bytesdone += requested;
981 } while (bytesdone < size);
986 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
987 Does not wait for completion of the transfer nor does it make sure that
988 the transfer was successful.
990 This function could be extended to use signals and callbacks to inform the
991 caller of completion or error - but this is not done yet, volunteers welcome.
993 Works around libusb and directly accesses functions only available on Linux.
994 Only available if compiled with --with-async-mode.
996 \param ftdi pointer to ftdi_context
997 \param buf Buffer with the data
998 \param size Size of the buffer
1000 \retval <0: error code from usb_bulk_write()
1001 \retval >0: number of bytes written
1003 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1007 int total_written = 0;
1009 while (offset < size) {
1010 int write_size = ftdi->writebuffer_chunksize;
1012 if (offset+write_size > size)
1013 write_size = size-offset;
1015 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
1017 ftdi_error_return(ret, "usb bulk write async failed");
1019 total_written += ret;
1020 offset += write_size;
1023 return total_written;
1025 #endif // LIBFTDI_LINUX_ASYNC_MODE
1028 Configure write buffer chunk size.
1031 \param ftdi pointer to ftdi_context
1032 \param chunksize Chunk size
1036 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1038 ftdi->writebuffer_chunksize = chunksize;
1043 Get write buffer chunk size.
1045 \param ftdi pointer to ftdi_context
1046 \param chunksize Pointer to store chunk size in
1050 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1052 *chunksize = ftdi->writebuffer_chunksize;
1057 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1059 Automatically strips the two modem status bytes transfered during every read.
1061 \param ftdi pointer to ftdi_context
1062 \param buf Buffer to store data in
1063 \param size Size of the buffer
1065 \retval <0: error code from usb_bulk_read()
1066 \retval 0: no data was available
1067 \retval >0: number of bytes read
1069 \remark This function is not useful in bitbang mode.
1070 Use ftdi_read_pins() to get the current state of the pins.
1072 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1074 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1076 // everything we want is still in the readbuffer?
1077 if (size <= ftdi->readbuffer_remaining) {
1078 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1081 ftdi->readbuffer_remaining -= size;
1082 ftdi->readbuffer_offset += size;
1084 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1088 // something still in the readbuffer, but not enough to satisfy 'size'?
1089 if (ftdi->readbuffer_remaining != 0) {
1090 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1093 offset += ftdi->readbuffer_remaining;
1095 // do the actual USB read
1096 while (offset < size && ret > 0) {
1097 ftdi->readbuffer_remaining = 0;
1098 ftdi->readbuffer_offset = 0;
1099 /* returns how much received */
1100 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
1102 ftdi_error_return(ret, "usb bulk read failed");
1105 // skip FTDI status bytes.
1106 // Maybe stored in the future to enable modem use
1107 num_of_chunks = ret / 64;
1108 chunk_remains = ret % 64;
1109 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1111 ftdi->readbuffer_offset += 2;
1115 for (i = 1; i < num_of_chunks; i++)
1116 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
1117 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
1119 if (chunk_remains > 2) {
1120 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
1121 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
1123 ret -= 2*num_of_chunks;
1125 ret -= 2*(num_of_chunks-1)+chunk_remains;
1127 } else if (ret <= 2) {
1128 // no more data to read?
1132 // data still fits in buf?
1133 if (offset+ret <= size) {
1134 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
1135 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1138 /* Did we read exactly the right amount of bytes? */
1140 //printf("read_data exact rem %d offset %d\n",
1141 //ftdi->readbuffer_remaining, offset);
1144 // only copy part of the data or size <= readbuffer_chunksize
1145 int part_size = size-offset;
1146 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1148 ftdi->readbuffer_offset += part_size;
1149 ftdi->readbuffer_remaining = ret-part_size;
1150 offset += part_size;
1152 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1153 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
1164 Configure read buffer chunk size.
1167 Automatically reallocates the buffer.
1169 \param ftdi pointer to ftdi_context
1170 \param chunksize Chunk size
1174 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1176 unsigned char *new_buf;
1178 // Invalidate all remaining data
1179 ftdi->readbuffer_offset = 0;
1180 ftdi->readbuffer_remaining = 0;
1182 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1183 ftdi_error_return(-1, "out of memory for readbuffer");
1185 ftdi->readbuffer = new_buf;
1186 ftdi->readbuffer_chunksize = chunksize;
1192 Get read buffer chunk size.
1194 \param ftdi pointer to ftdi_context
1195 \param chunksize Pointer to store chunk size in
1199 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1201 *chunksize = ftdi->readbuffer_chunksize;
1207 Enable bitbang mode.
1209 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
1211 \param ftdi pointer to ftdi_context
1212 \param bitmask Bitmask to configure lines.
1213 HIGH/ON value configures a line as output.
1216 \retval -1: can't enable bitbang mode
1218 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1220 unsigned short usb_val;
1222 usb_val = bitmask; // low byte: bitmask
1223 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1224 usb_val |= (ftdi->bitbang_mode << 8);
1226 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1227 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1229 ftdi->bitbang_enabled = 1;
1234 Disable bitbang mode.
1236 \param ftdi pointer to ftdi_context
1239 \retval -1: can't disable bitbang mode
1241 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1243 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1244 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1246 ftdi->bitbang_enabled = 0;
1251 Enable advanced bitbang mode for FT2232C chips.
1253 \param ftdi pointer to ftdi_context
1254 \param bitmask Bitmask to configure lines.
1255 HIGH/ON value configures a line as output.
1256 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
1259 \retval -1: can't enable bitbang mode
1261 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1263 unsigned short usb_val;
1265 usb_val = bitmask; // low byte: bitmask
1266 usb_val |= (mode << 8);
1267 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1268 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1270 ftdi->bitbang_mode = mode;
1271 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
1276 Directly read pin state. Useful for bitbang mode.
1278 \param ftdi pointer to ftdi_context
1279 \param pins Pointer to store pins into
1282 \retval -1: read pins failed
1284 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1286 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
1287 ftdi_error_return(-1, "read pins failed");
1295 The FTDI chip keeps data in the internal buffer for a specific
1296 amount of time if the buffer is not full yet to decrease
1297 load on the usb bus.
1299 \param ftdi pointer to ftdi_context
1300 \param latency Value between 1 and 255
1303 \retval -1: latency out of range
1304 \retval -2: unable to set latency timer
1306 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1308 unsigned short usb_val;
1311 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1314 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1315 ftdi_error_return(-2, "unable to set latency timer");
1323 \param ftdi pointer to ftdi_context
1324 \param latency Pointer to store latency value in
1327 \retval -1: unable to get latency timer
1329 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1331 unsigned short usb_val;
1332 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1333 ftdi_error_return(-1, "reading latency timer failed");
1335 *latency = (unsigned char)usb_val;
1340 Poll modem status information
1342 This function allows the retrieve the two status bytes of the device.
1343 The device sends these bytes also as a header for each read access
1344 where they are discarded by ftdi_read_data(). The chip generates
1345 the two stripped status bytes in the absence of data every 40 ms.
1347 Layout of the first byte:
1348 - B0..B3 - must be 0
1349 - B4 Clear to send (CTS)
1352 - B5 Data set ready (DTS)
1355 - B6 Ring indicator (RI)
1358 - B7 Receive line signal detect (RLSD)
1362 Layout of the second byte:
1363 - B0 Data ready (DR)
1364 - B1 Overrun error (OE)
1365 - B2 Parity error (PE)
1366 - B3 Framing error (FE)
1367 - B4 Break interrupt (BI)
1368 - B5 Transmitter holding register (THRE)
1369 - B6 Transmitter empty (TEMT)
1370 - B7 Error in RCVR FIFO
1372 \param ftdi pointer to ftdi_context
1373 \param status Pointer to store status information in. Must be two bytes.
1376 \retval -1: unable to retrieve status information
1378 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1382 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x05, 0, ftdi->index, usb_val, 2, ftdi->usb_read_timeout) != 2)
1383 ftdi_error_return(-1, "getting modem status failed");
1385 *status = (usb_val[1] << 8) | usb_val[0];
1391 Set the special event character
1393 \param ftdi pointer to ftdi_context
1394 \param eventch Event character
1395 \param enable 0 to disable the event character, non-zero otherwise
1398 \retval -1: unable to set event character
1400 int ftdi_set_event_char(struct ftdi_context *ftdi,
1401 unsigned char eventch, unsigned char enable)
1403 unsigned short usb_val;
1409 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x06, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1410 ftdi_error_return(-1, "setting event character failed");
1418 \param ftdi pointer to ftdi_context
1419 \param errorch Error character
1420 \param enable 0 to disable the error character, non-zero otherwise
1423 \retval -1: unable to set error character
1425 int ftdi_set_error_char(struct ftdi_context *ftdi,
1426 unsigned char errorch, unsigned char enable)
1428 unsigned short usb_val;
1434 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x07, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1435 ftdi_error_return(-1, "setting error character failed");
1443 \param ftdi pointer to ftdi_context
1444 \param eeprom Pointer to ftdi_eeprom
1448 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
1450 ftdi->eeprom_size=size;
1455 Init eeprom with default values.
1457 \param eeprom Pointer to ftdi_eeprom
1459 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
1461 eeprom->vendor_id = 0x0403;
1462 eeprom->product_id = 0x6001;
1464 eeprom->self_powered = 1;
1465 eeprom->remote_wakeup = 1;
1466 eeprom->BM_type_chip = 1;
1468 eeprom->in_is_isochronous = 0;
1469 eeprom->out_is_isochronous = 0;
1470 eeprom->suspend_pull_downs = 0;
1472 eeprom->use_serial = 0;
1473 eeprom->change_usb_version = 0;
1474 eeprom->usb_version = 0x0200;
1475 eeprom->max_power = 0;
1477 eeprom->manufacturer = NULL;
1478 eeprom->product = NULL;
1479 eeprom->serial = NULL;
1481 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
1485 Build binary output from ftdi_eeprom structure.
1486 Output is suitable for ftdi_write_eeprom().
1488 \param eeprom Pointer to ftdi_eeprom
1489 \param output Buffer of 128 bytes to store eeprom image to
1491 \retval >0: used eeprom size
1492 \retval -1: eeprom size (128 bytes) exceeded by custom strings
1494 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
1497 unsigned short checksum, value;
1498 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1501 if (eeprom->manufacturer != NULL)
1502 manufacturer_size = strlen(eeprom->manufacturer);
1503 if (eeprom->product != NULL)
1504 product_size = strlen(eeprom->product);
1505 if (eeprom->serial != NULL)
1506 serial_size = strlen(eeprom->serial);
1508 size_check = eeprom->size;
1509 size_check -= 28; // 28 are always in use (fixed)
1511 // Top half of a 256byte eeprom is used just for strings and checksum
1512 // it seems that the FTDI chip will not read these strings from the lower half
1513 // Each string starts with two bytes; offset and type (0x03 for string)
1514 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
1515 if(eeprom->size>=256)size_check = 120;
1516 size_check -= manufacturer_size*2;
1517 size_check -= product_size*2;
1518 size_check -= serial_size*2;
1520 // eeprom size exceeded?
1525 memset (output, 0, eeprom->size);
1527 // Addr 00: Stay 00 00
1528 // Addr 02: Vendor ID
1529 output[0x02] = eeprom->vendor_id;
1530 output[0x03] = eeprom->vendor_id >> 8;
1532 // Addr 04: Product ID
1533 output[0x04] = eeprom->product_id;
1534 output[0x05] = eeprom->product_id >> 8;
1536 // Addr 06: Device release number (0400h for BM features)
1537 output[0x06] = 0x00;
1539 if (eeprom->BM_type_chip == 1)
1540 output[0x07] = 0x04;
1542 output[0x07] = 0x02;
1544 // Addr 08: Config descriptor
1546 // Bit 6: 1 if this device is self powered, 0 if bus powered
1547 // Bit 5: 1 if this device uses remote wakeup
1548 // Bit 4: 1 if this device is battery powered
1550 if (eeprom->self_powered == 1)
1552 if (eeprom->remote_wakeup == 1)
1556 // Addr 09: Max power consumption: max power = value * 2 mA
1557 output[0x09] = eeprom->max_power;
1559 // Addr 0A: Chip configuration
1560 // Bit 7: 0 - reserved
1561 // Bit 6: 0 - reserved
1562 // Bit 5: 0 - reserved
1563 // Bit 4: 1 - Change USB version
1564 // Bit 3: 1 - Use the serial number string
1565 // Bit 2: 1 - Enable suspend pull downs for lower power
1566 // Bit 1: 1 - Out EndPoint is Isochronous
1567 // Bit 0: 1 - In EndPoint is Isochronous
1570 if (eeprom->in_is_isochronous == 1)
1572 if (eeprom->out_is_isochronous == 1)
1574 if (eeprom->suspend_pull_downs == 1)
1576 if (eeprom->use_serial == 1)
1578 if (eeprom->change_usb_version == 1)
1582 // Addr 0B: reserved
1583 output[0x0B] = 0x00;
1585 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1586 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1587 if (eeprom->change_usb_version == 1) {
1588 output[0x0C] = eeprom->usb_version;
1589 output[0x0D] = eeprom->usb_version >> 8;
1593 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
1594 // Addr 0F: Length of manufacturer string
1595 output[0x0F] = manufacturer_size*2 + 2;
1597 // Addr 10: Offset of the product string + 0x80, calculated later
1598 // Addr 11: Length of product string
1599 output[0x11] = product_size*2 + 2;
1601 // Addr 12: Offset of the serial string + 0x80, calculated later
1602 // Addr 13: Length of serial string
1603 output[0x13] = serial_size*2 + 2;
1607 if(eeprom->size>=256) i = 0x80;
1610 // Output manufacturer
1611 output[0x0E] = i | 0x80; // calculate offset
1612 output[i++] = manufacturer_size*2 + 2;
1613 output[i++] = 0x03; // type: string
1614 for (j = 0; j < manufacturer_size; j++) {
1615 output[i] = eeprom->manufacturer[j], i++;
1616 output[i] = 0x00, i++;
1619 // Output product name
1620 output[0x10] = i | 0x80; // calculate offset
1621 output[i] = product_size*2 + 2, i++;
1622 output[i] = 0x03, i++;
1623 for (j = 0; j < product_size; j++) {
1624 output[i] = eeprom->product[j], i++;
1625 output[i] = 0x00, i++;
1629 output[0x12] = i | 0x80; // calculate offset
1630 output[i] = serial_size*2 + 2, i++;
1631 output[i] = 0x03, i++;
1632 for (j = 0; j < serial_size; j++) {
1633 output[i] = eeprom->serial[j], i++;
1634 output[i] = 0x00, i++;
1637 // calculate checksum
1640 for (i = 0; i < eeprom->size/2-1; i++) {
1641 value = output[i*2];
1642 value += output[(i*2)+1] << 8;
1644 checksum = value^checksum;
1645 checksum = (checksum << 1) | (checksum >> 15);
1648 output[eeprom->size-2] = checksum;
1649 output[eeprom->size-1] = checksum >> 8;
1657 \param ftdi pointer to ftdi_context
1658 \param eeprom Pointer to store eeprom into
1661 \retval -1: read failed
1663 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1667 for (i = 0; i < ftdi->eeprom_size/2; i++) {
1668 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1669 ftdi_error_return(-1, "reading eeprom failed");
1676 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
1677 Function is only used internally
1680 static unsigned char ftdi_read_chipid_shift(unsigned char value)
1682 return ((value & 1) << 1) |
1683 ((value & 2) << 5) |
1684 ((value & 4) >> 2) |
1685 ((value & 8) << 4) |
1686 ((value & 16) >> 1) |
1687 ((value & 32) >> 1) |
1688 ((value & 64) >> 4) |
1689 ((value & 128) >> 2);
1693 Read the FTDIChip-ID from R-type devices
1695 \param ftdi pointer to ftdi_context
1696 \param chipid Pointer to store FTDIChip-ID
1699 \retval -1: read failed
1701 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
1703 unsigned int a = 0, b = 0;
1705 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
1707 a = a << 8 | a >> 8;
1708 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
1710 b = b << 8 | b >> 8;
1712 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
1713 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
1714 *chipid = a ^ 0xa5f0f7d1;
1719 ftdi_error_return(-1, "read of FTDIChip-ID failed");
1723 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
1724 Call this function then do a write then call again to see if size changes, if so write again.
1726 \param ftdi pointer to ftdi_context
1727 \param eeprom Pointer to store eeprom into
1728 \param maxsize the size of the buffer to read into
1730 \retval size of eeprom
1732 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
1734 int i=0,j,minsize=32;
1738 for (j = 0; i < maxsize/2 && j<size; j++) {
1739 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1740 ftdi_error_return(-1, "reading eeprom failed");
1744 }while(size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
1752 \param ftdi pointer to ftdi_context
1753 \param eeprom Pointer to read eeprom from
1756 \retval -1: read failed
1758 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1760 unsigned short usb_val;
1763 for (i = 0; i < ftdi->eeprom_size/2; i++) {
1764 usb_val = eeprom[i*2];
1765 usb_val += eeprom[(i*2)+1] << 8;
1766 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1767 ftdi_error_return(-1, "unable to write eeprom");
1776 \param ftdi pointer to ftdi_context
1779 \retval -1: erase failed
1781 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1783 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1784 ftdi_error_return(-1, "unable to erase eeprom");
1790 Get string representation for last error code
1792 \param ftdi pointer to ftdi_context
1794 \retval Pointer to error string
1796 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1798 return ftdi->error_str;
1802 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1803 and Matthias Kranz (matthias@hcilab.org)
1806 Set flowcontrol for ftdi chip
1808 \param ftdi pointer to ftdi_context
1809 \param flowctrl flow control to use. should be
1810 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1813 \retval -1: set flow control failed
1815 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1817 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1818 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1819 NULL, 0, ftdi->usb_write_timeout) != 0)
1820 ftdi_error_return(-1, "set flow control failed");
1828 \param ftdi pointer to ftdi_context
1829 \param state state to set line to (1 or 0)
1832 \retval -1: set dtr failed
1834 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1836 unsigned short usb_val;
1839 usb_val = SIO_SET_DTR_HIGH;
1841 usb_val = SIO_SET_DTR_LOW;
1843 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1844 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1845 NULL, 0, ftdi->usb_write_timeout) != 0)
1846 ftdi_error_return(-1, "set dtr failed");
1854 \param ftdi pointer to ftdi_context
1855 \param state state to set line to (1 or 0)
1858 \retval -1 set rts failed
1860 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1862 unsigned short usb_val;
1865 usb_val = SIO_SET_RTS_HIGH;
1867 usb_val = SIO_SET_RTS_LOW;
1869 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1870 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1871 NULL, 0, ftdi->usb_write_timeout) != 0)
1872 ftdi_error_return(-1, "set of rts failed");
1877 /* @} end of doxygen libftdi group */