libftdi1 1.5
ftdi.c
Go to the documentation of this file.
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
5 copyright : (C) 2003-2020 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 SPDX-License-Identifier: LGPL-2.1-only
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU Lesser General Public License *
14 * version 2.1 as published by the Free Software Foundation; *
15 * *
16 ***************************************************************************/
17
30/* @{ */
31
32#include <libusb.h>
33#include <string.h>
34#include <errno.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38#include "ftdi_i.h"
39/* Prevent deprecated messages when building library */
40#define _FTDI_DISABLE_DEPRECATED
41#include "ftdi.h"
42#include "ftdi_version_i.h"
43
44#define ftdi_error_return(code, str) do { \
45 if ( ftdi ) \
46 ftdi->error_str = str; \
47 else \
48 fprintf(stderr, str); \
49 return code; \
50 } while(0);
51
52#define ftdi_error_return_free_device_list(code, str, devs) do { \
53 libusb_free_device_list(devs,1); \
54 ftdi->error_str = str; \
55 return code; \
56 } while(0);
57
58
68static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
69{
70 if (ftdi && ftdi->usb_dev)
71 {
72 libusb_close (ftdi->usb_dev);
73 ftdi->usb_dev = NULL;
74 if(ftdi->eeprom)
76 }
77}
78
91int ftdi_init(struct ftdi_context *ftdi)
92{
93 struct ftdi_eeprom* eeprom;
94 ftdi->usb_ctx = NULL;
95 ftdi->usb_dev = NULL;
96 ftdi->usb_read_timeout = 5000;
97 ftdi->usb_write_timeout = 5000;
98
99 ftdi->type = TYPE_BM; /* chip type */
100 ftdi->baudrate = -1;
101 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
102
103 ftdi->readbuffer = NULL;
104 ftdi->readbuffer_offset = 0;
105 ftdi->readbuffer_remaining = 0;
106 ftdi->writebuffer_chunksize = 4096;
107 ftdi->max_packet_size = 0;
108 ftdi->error_str = NULL;
110
111 if (libusb_init(&ftdi->usb_ctx) < 0)
112 ftdi_error_return(-3, "libusb_init() failed");
113
115 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
116
117 eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
118 if (eeprom == 0)
119 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
120 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
121 ftdi->eeprom = eeprom;
122
123 /* All fine. Now allocate the readbuffer */
124 return ftdi_read_data_set_chunksize(ftdi, 4096);
125}
126
133{
134 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
135
136 if (ftdi == NULL)
137 {
138 return NULL;
139 }
140
141 if (ftdi_init(ftdi) != 0)
142 {
143 free(ftdi);
144 return NULL;
145 }
146
147 return ftdi;
148}
149
162{
163 if (ftdi == NULL)
164 ftdi_error_return(-2, "USB device unavailable");
165
166 if (ftdi->usb_dev != NULL)
167 {
168 int check_interface = interface;
169 if (check_interface == INTERFACE_ANY)
170 check_interface = INTERFACE_A;
171
172 if (ftdi->index != check_interface)
173 ftdi_error_return(-3, "Interface can not be changed on an already open device");
174 }
175
176 switch (interface)
177 {
178 case INTERFACE_ANY:
179 case INTERFACE_A:
180 ftdi->interface = 0;
181 ftdi->index = INTERFACE_A;
182 ftdi->in_ep = 0x02;
183 ftdi->out_ep = 0x81;
184 break;
185 case INTERFACE_B:
186 ftdi->interface = 1;
187 ftdi->index = INTERFACE_B;
188 ftdi->in_ep = 0x04;
189 ftdi->out_ep = 0x83;
190 break;
191 case INTERFACE_C:
192 ftdi->interface = 2;
193 ftdi->index = INTERFACE_C;
194 ftdi->in_ep = 0x06;
195 ftdi->out_ep = 0x85;
196 break;
197 case INTERFACE_D:
198 ftdi->interface = 3;
199 ftdi->index = INTERFACE_D;
200 ftdi->in_ep = 0x08;
201 ftdi->out_ep = 0x87;
202 break;
203 default:
204 ftdi_error_return(-1, "Unknown interface");
205 }
206 return 0;
207}
208
214void ftdi_deinit(struct ftdi_context *ftdi)
215{
216 if (ftdi == NULL)
217 return;
218
219 ftdi_usb_close_internal (ftdi);
220
221 if (ftdi->readbuffer != NULL)
222 {
223 free(ftdi->readbuffer);
224 ftdi->readbuffer = NULL;
225 }
226
227 if (ftdi->eeprom != NULL)
228 {
229 if (ftdi->eeprom->manufacturer != 0)
230 {
231 free(ftdi->eeprom->manufacturer);
232 ftdi->eeprom->manufacturer = 0;
233 }
234 if (ftdi->eeprom->product != 0)
235 {
236 free(ftdi->eeprom->product);
237 ftdi->eeprom->product = 0;
238 }
239 if (ftdi->eeprom->serial != 0)
240 {
241 free(ftdi->eeprom->serial);
242 ftdi->eeprom->serial = 0;
243 }
244 free(ftdi->eeprom);
245 ftdi->eeprom = NULL;
246 }
247
248 if (ftdi->usb_ctx)
249 {
250 libusb_exit(ftdi->usb_ctx);
251 ftdi->usb_ctx = NULL;
252 }
253}
254
260void ftdi_free(struct ftdi_context *ftdi)
261{
262 ftdi_deinit(ftdi);
263 free(ftdi);
264}
265
272void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
273{
274 if (ftdi == NULL)
275 return;
276
277 ftdi->usb_dev = usb;
278}
279
286{
287 struct ftdi_version_info ver;
288
289 ver.major = FTDI_MAJOR_VERSION;
290 ver.minor = FTDI_MINOR_VERSION;
291 ver.micro = FTDI_MICRO_VERSION;
292 ver.version_str = FTDI_VERSION_STRING;
293 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
294
295 return ver;
296}
297
314int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
315{
316 struct ftdi_device_list **curdev;
317 libusb_device *dev;
318 libusb_device **devs;
319 int count = 0;
320 int i = 0;
321
322 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
323 ftdi_error_return(-5, "libusb_get_device_list() failed");
324
325 curdev = devlist;
326 *curdev = NULL;
327
328 while ((dev = devs[i++]) != NULL)
329 {
330 struct libusb_device_descriptor desc;
331
332 if (libusb_get_device_descriptor(dev, &desc) < 0)
333 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
334
335 if (((vendor || product) &&
336 desc.idVendor == vendor && desc.idProduct == product) ||
337 (!(vendor || product) &&
338 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
339 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
340 || desc.idProduct == 0x6015)))
341 {
342 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
343 if (!*curdev)
344 ftdi_error_return_free_device_list(-3, "out of memory", devs);
345
346 (*curdev)->next = NULL;
347 (*curdev)->dev = dev;
348 libusb_ref_device(dev);
349 curdev = &(*curdev)->next;
350 count++;
351 }
352 }
353 libusb_free_device_list(devs,1);
354 return count;
355}
356
362void ftdi_list_free(struct ftdi_device_list **devlist)
363{
364 struct ftdi_device_list *curdev, *next;
365
366 for (curdev = *devlist; curdev != NULL;)
367 {
368 next = curdev->next;
369 libusb_unref_device(curdev->dev);
370 free(curdev);
371 curdev = next;
372 }
373
374 *devlist = NULL;
375}
376
383{
384 ftdi_list_free(&devlist);
385}
386
414 struct libusb_device *dev,
415 char *manufacturer, int mnf_len,
416 char *description, int desc_len,
417 char *serial, int serial_len)
418{
419 int ret;
420
421 if ((ftdi==NULL) || (dev==NULL))
422 return -1;
423
424 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
425 ftdi_error_return(-4, "libusb_open() failed");
426
427 // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
428 // it won't be closed either. This allows us to close it whether we actually
429 // called libusb_open() up above or not. This matches the expected behavior
430 // (and note) for ftdi_usb_get_strings().
431 ret = ftdi_usb_get_strings2(ftdi, dev,
432 manufacturer, mnf_len,
433 description, desc_len,
434 serial, serial_len);
435
436 // only close it if it was successful, as all other return codes close
437 // before returning already.
438 if (ret == 0)
439 ftdi_usb_close_internal(ftdi);
440
441 return ret;
442}
443
470int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
471 char *manufacturer, int mnf_len,
472 char *description, int desc_len,
473 char *serial, int serial_len)
474{
475 struct libusb_device_descriptor desc;
476 char need_open;
477
478 if ((ftdi==NULL) || (dev==NULL))
479 return -1;
480
481 need_open = (ftdi->usb_dev == NULL);
482 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
483 ftdi_error_return(-4, "libusb_open() failed");
484
485 if (libusb_get_device_descriptor(dev, &desc) < 0)
486 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
487
488 if (manufacturer != NULL)
489 {
490 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
491 {
492 ftdi_usb_close_internal (ftdi);
493 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
494 }
495 }
496
497 if (description != NULL)
498 {
499 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
500 {
501 ftdi_usb_close_internal (ftdi);
502 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
503 }
504 }
505
506 if (serial != NULL)
507 {
508 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
509 {
510 ftdi_usb_close_internal (ftdi);
511 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
512 }
513 }
514
515 if (need_open)
516 ftdi_usb_close_internal (ftdi);
517
518 return 0;
519}
520
527static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
528{
529 struct libusb_device_descriptor desc;
530 struct libusb_config_descriptor *config0;
531 unsigned int packet_size;
532
533 // Sanity check
534 if (ftdi == NULL || dev == NULL)
535 return 64;
536
537 // Determine maximum packet size. Init with default value.
538 // New hi-speed devices from FTDI use a packet size of 512 bytes
539 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
540 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
541 packet_size = 512;
542 else
543 packet_size = 64;
544
545 if (libusb_get_device_descriptor(dev, &desc) < 0)
546 return packet_size;
547
548 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
549 return packet_size;
550
551 if (desc.bNumConfigurations > 0)
552 {
553 if (ftdi->interface < config0->bNumInterfaces)
554 {
555 struct libusb_interface interface = config0->interface[ftdi->interface];
556 if (interface.num_altsetting > 0)
557 {
558 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
559 if (descriptor.bNumEndpoints > 0)
560 {
561 packet_size = descriptor.endpoint[0].wMaxPacketSize;
562 }
563 }
564 }
565 }
566
567 libusb_free_config_descriptor (config0);
568 return packet_size;
569}
570
589int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
590{
591 struct libusb_device_descriptor desc;
592 struct libusb_config_descriptor *config0;
593 int cfg, cfg0, detach_errno = 0;
594
595 if (ftdi == NULL)
596 ftdi_error_return(-8, "ftdi context invalid");
597
598 if (libusb_open(dev, &ftdi->usb_dev) < 0)
599 ftdi_error_return(-4, "libusb_open() failed");
600
601 if (libusb_get_device_descriptor(dev, &desc) < 0)
602 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
603
604 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
605 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
606 cfg0 = config0->bConfigurationValue;
607 libusb_free_config_descriptor (config0);
608
609 // Try to detach ftdi_sio kernel module.
610 //
611 // The return code is kept in a separate variable and only parsed
612 // if usb_set_configuration() or usb_claim_interface() fails as the
613 // detach operation might be denied and everything still works fine.
614 // Likely scenario is a static ftdi_sio kernel module.
616 {
617 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
618 detach_errno = errno;
619 }
621 {
622 if (libusb_set_auto_detach_kernel_driver(ftdi->usb_dev, 1) != LIBUSB_SUCCESS)
623 detach_errno = errno;
624 }
625
626 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
627 ftdi_error_return(-12, "libusb_get_configuration () failed");
628 // set configuration (needed especially for windows)
629 // tolerate EBUSY: one device with one configuration, but two interfaces
630 // and libftdi sessions to both interfaces (e.g. FT2232)
631 if (desc.bNumConfigurations > 0 && cfg != cfg0)
632 {
633 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
634 {
635 ftdi_usb_close_internal (ftdi);
636 if (detach_errno == EPERM)
637 {
638 ftdi_error_return(-8, "inappropriate permissions on device!");
639 }
640 else
641 {
642 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
643 }
644 }
645 }
646
647 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
648 {
649 ftdi_usb_close_internal (ftdi);
650 if (detach_errno == EPERM)
651 {
652 ftdi_error_return(-8, "inappropriate permissions on device!");
653 }
654 else
655 {
656 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
657 }
658 }
659
660 if (ftdi_usb_reset (ftdi) != 0)
661 {
662 ftdi_usb_close_internal (ftdi);
663 ftdi_error_return(-6, "ftdi_usb_reset failed");
664 }
665
666 // Try to guess chip type
667 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
668 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
669 && desc.iSerialNumber == 0))
670 ftdi->type = TYPE_BM;
671 else if (desc.bcdDevice == 0x200)
672 ftdi->type = TYPE_AM;
673 else if (desc.bcdDevice == 0x500)
674 ftdi->type = TYPE_2232C;
675 else if (desc.bcdDevice == 0x600)
676 ftdi->type = TYPE_R;
677 else if (desc.bcdDevice == 0x700)
678 ftdi->type = TYPE_2232H;
679 else if (desc.bcdDevice == 0x800)
680 ftdi->type = TYPE_4232H;
681 else if (desc.bcdDevice == 0x900)
682 ftdi->type = TYPE_232H;
683 else if (desc.bcdDevice == 0x1000)
684 ftdi->type = TYPE_230X;
685
686 // Determine maximum packet size
687 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
688
689 if (ftdi_set_baudrate (ftdi, 9600) != 0)
690 {
691 ftdi_usb_close_internal (ftdi);
692 ftdi_error_return(-7, "set baudrate failed");
693 }
694
695 ftdi_error_return(0, "all fine");
696}
697
707int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
708{
709 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
710}
711
733int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
734 const char* description, const char* serial)
735{
736 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
737}
738
764int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
765 const char* description, const char* serial, unsigned int index)
766{
767 libusb_device *dev;
768 libusb_device **devs;
769 char string[256];
770 int i = 0;
771
772 if (ftdi == NULL)
773 ftdi_error_return(-11, "ftdi context invalid");
774
775 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
776 ftdi_error_return(-12, "libusb_get_device_list() failed");
777
778 while ((dev = devs[i++]) != NULL)
779 {
780 struct libusb_device_descriptor desc;
781 int res;
782
783 if (libusb_get_device_descriptor(dev, &desc) < 0)
784 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
785
786 if (desc.idVendor == vendor && desc.idProduct == product)
787 {
788 if (libusb_open(dev, &ftdi->usb_dev) < 0)
789 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
790
791 if (description != NULL)
792 {
793 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
794 {
795 ftdi_usb_close_internal (ftdi);
796 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
797 }
798 if (strncmp(string, description, sizeof(string)) != 0)
799 {
800 ftdi_usb_close_internal (ftdi);
801 continue;
802 }
803 }
804 if (serial != NULL)
805 {
806 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
807 {
808 ftdi_usb_close_internal (ftdi);
809 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
810 }
811 if (strncmp(string, serial, sizeof(string)) != 0)
812 {
813 ftdi_usb_close_internal (ftdi);
814 continue;
815 }
816 }
817
818 ftdi_usb_close_internal (ftdi);
819
820 if (index > 0)
821 {
822 index--;
823 continue;
824 }
825
826 res = ftdi_usb_open_dev(ftdi, dev);
827 libusb_free_device_list(devs,1);
828 return res;
829 }
830 }
831
832 // device not found
833 ftdi_error_return_free_device_list(-3, "device not found", devs);
834}
835
857int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
858{
859 libusb_device *dev;
860 libusb_device **devs;
861 int i = 0;
862
863 if (ftdi == NULL)
864 ftdi_error_return(-11, "ftdi context invalid");
865
866 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
867 ftdi_error_return(-12, "libusb_get_device_list() failed");
868
869 while ((dev = devs[i++]) != NULL)
870 {
871 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
872 {
873 int res;
874 res = ftdi_usb_open_dev(ftdi, dev);
875 libusb_free_device_list(devs,1);
876 return res;
877 }
878 }
879
880 // device not found
881 ftdi_error_return_free_device_list(-3, "device not found", devs);
882}
883
910int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
911{
912 if (ftdi == NULL)
913 ftdi_error_return(-12, "ftdi context invalid");
914
915 if (description[0] == 0 || description[1] != ':')
916 ftdi_error_return(-11, "illegal description format");
917
918 if (description[0] == 'd')
919 {
920 libusb_device *dev;
921 libusb_device **devs;
922 unsigned int bus_number, device_address;
923 int i = 0;
924
925 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
926 ftdi_error_return(-2, "libusb_get_device_list() failed");
927
928 /* XXX: This doesn't handle symlinks/odd paths/etc... */
929 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
930 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
931
932 while ((dev = devs[i++]) != NULL)
933 {
934 int ret;
935 if (bus_number == libusb_get_bus_number (dev)
936 && device_address == libusb_get_device_address (dev))
937 {
938 ret = ftdi_usb_open_dev(ftdi, dev);
939 libusb_free_device_list(devs,1);
940 return ret;
941 }
942 }
943
944 // device not found
945 ftdi_error_return_free_device_list(-3, "device not found", devs);
946 }
947 else if (description[0] == 'i' || description[0] == 's')
948 {
949 unsigned int vendor;
950 unsigned int product;
951 unsigned int index=0;
952 const char *serial=NULL;
953 const char *startp, *endp;
954
955 errno=0;
956 startp=description+2;
957 vendor=strtoul((char*)startp,(char**)&endp,0);
958 if (*endp != ':' || endp == startp || errno != 0)
959 ftdi_error_return(-11, "illegal description format");
960
961 startp=endp+1;
962 product=strtoul((char*)startp,(char**)&endp,0);
963 if (endp == startp || errno != 0)
964 ftdi_error_return(-11, "illegal description format");
965
966 if (description[0] == 'i' && *endp != 0)
967 {
968 /* optional index field in i-mode */
969 if (*endp != ':')
970 ftdi_error_return(-11, "illegal description format");
971
972 startp=endp+1;
973 index=strtoul((char*)startp,(char**)&endp,0);
974 if (*endp != 0 || endp == startp || errno != 0)
975 ftdi_error_return(-11, "illegal description format");
976 }
977 if (description[0] == 's')
978 {
979 if (*endp != ':')
980 ftdi_error_return(-11, "illegal description format");
981
982 /* rest of the description is the serial */
983 serial=endp+1;
984 }
985
986 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
987 }
988 else
989 {
990 ftdi_error_return(-11, "illegal description format");
991 }
992}
993
1004{
1005 if (ftdi == NULL || ftdi->usb_dev == NULL)
1006 ftdi_error_return(-2, "USB device unavailable");
1007
1008 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1010 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1011 ftdi_error_return(-1,"FTDI reset failed");
1012
1013 // Invalidate data in the readbuffer
1014 ftdi->readbuffer_offset = 0;
1015 ftdi->readbuffer_remaining = 0;
1016
1017 return 0;
1018}
1019
1031{
1032 if (ftdi == NULL || ftdi->usb_dev == NULL)
1033 ftdi_error_return(-2, "USB device unavailable");
1034
1035 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1037 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1038 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1039
1040 // Invalidate data in the readbuffer
1041 ftdi->readbuffer_offset = 0;
1042 ftdi->readbuffer_remaining = 0;
1043
1044 return 0;
1045}
1046
1047
1061{
1062 if (ftdi == NULL || ftdi->usb_dev == NULL)
1063 ftdi_error_return(-2, "USB device unavailable");
1064
1065 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1067 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1068 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
1069
1070 // Invalidate data in the readbuffer
1071 ftdi->readbuffer_offset = 0;
1072 ftdi->readbuffer_remaining = 0;
1073
1074 return 0;
1075}
1076
1088{
1089 if (ftdi == NULL || ftdi->usb_dev == NULL)
1090 ftdi_error_return(-2, "USB device unavailable");
1091
1092 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1094 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1095 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1096
1097 return 0;
1098}
1099
1100
1114{
1115 if (ftdi == NULL || ftdi->usb_dev == NULL)
1116 ftdi_error_return(-2, "USB device unavailable");
1117
1118 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1120 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1121 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1122
1123 return 0;
1124}
1125
1138{
1139 int result;
1140
1141 if (ftdi == NULL || ftdi->usb_dev == NULL)
1142 ftdi_error_return(-3, "USB device unavailable");
1143
1144 result = ftdi_tcoflush(ftdi);
1145 if (result < 0)
1146 return -1;
1147
1148 result = ftdi_tciflush(ftdi);
1149 if (result < 0)
1150 return -2;
1151
1152 return 0;
1153}
1154
1169{
1170 int result;
1171
1172 if (ftdi == NULL || ftdi->usb_dev == NULL)
1173 ftdi_error_return(-3, "USB device unavailable");
1174
1175 result = ftdi_usb_purge_rx_buffer(ftdi);
1176 if (result < 0)
1177 return -1;
1178
1179 result = ftdi_usb_purge_tx_buffer(ftdi);
1180 if (result < 0)
1181 return -2;
1182
1183 return 0;
1184}
1185
1186
1187
1198{
1199 int rtn = 0;
1200
1201 if (ftdi == NULL)
1202 ftdi_error_return(-3, "ftdi context invalid");
1203
1204 if (ftdi->usb_dev != NULL)
1205 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1206 rtn = -1;
1207
1208 ftdi_usb_close_internal (ftdi);
1209
1210 return rtn;
1211}
1212
1213/* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1214 to encoded divisor and the achievable baudrate
1215 Function is only used internally
1216 \internal
1217
1218 See AN120
1219 clk/1 -> 0
1220 clk/1.5 -> 1
1221 clk/2 -> 2
1222 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1223 The fractional part has frac_code encoding
1224*/
1225static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1226
1227{
1228 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1229 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1230 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1231 int divisor, best_divisor, best_baud, best_baud_diff;
1232 int i;
1233 divisor = 24000000 / baudrate;
1234
1235 // Round down to supported fraction (AM only)
1236 divisor -= am_adjust_dn[divisor & 7];
1237
1238 // Try this divisor and the one above it (because division rounds down)
1239 best_divisor = 0;
1240 best_baud = 0;
1241 best_baud_diff = 0;
1242 for (i = 0; i < 2; i++)
1243 {
1244 int try_divisor = divisor + i;
1245 int baud_estimate;
1246 int baud_diff;
1247
1248 // Round up to supported divisor value
1249 if (try_divisor <= 8)
1250 {
1251 // Round up to minimum supported divisor
1252 try_divisor = 8;
1253 }
1254 else if (divisor < 16)
1255 {
1256 // AM doesn't support divisors 9 through 15 inclusive
1257 try_divisor = 16;
1258 }
1259 else
1260 {
1261 // Round up to supported fraction (AM only)
1262 try_divisor += am_adjust_up[try_divisor & 7];
1263 if (try_divisor > 0x1FFF8)
1264 {
1265 // Round down to maximum supported divisor value (for AM)
1266 try_divisor = 0x1FFF8;
1267 }
1268 }
1269 // Get estimated baud rate (to nearest integer)
1270 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1271 // Get absolute difference from requested baud rate
1272 if (baud_estimate < baudrate)
1273 {
1274 baud_diff = baudrate - baud_estimate;
1275 }
1276 else
1277 {
1278 baud_diff = baud_estimate - baudrate;
1279 }
1280 if (i == 0 || baud_diff < best_baud_diff)
1281 {
1282 // Closest to requested baud rate so far
1283 best_divisor = try_divisor;
1284 best_baud = baud_estimate;
1285 best_baud_diff = baud_diff;
1286 if (baud_diff == 0)
1287 {
1288 // Spot on! No point trying
1289 break;
1290 }
1291 }
1292 }
1293 // Encode the best divisor value
1294 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1295 // Deal with special cases for encoded value
1296 if (*encoded_divisor == 1)
1297 {
1298 *encoded_divisor = 0; // 3000000 baud
1299 }
1300 else if (*encoded_divisor == 0x4001)
1301 {
1302 *encoded_divisor = 1; // 2000000 baud (BM only)
1303 }
1304 return best_baud;
1305}
1306
1307/* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1308 to encoded divisor and the achievable baudrate
1309 Function is only used internally
1310 \internal
1311
1312 See AN120
1313 clk/1 -> 0
1314 clk/1.5 -> 1
1315 clk/2 -> 2
1316 From /2, 0.125 steps may be taken.
1317 The fractional part has frac_code encoding
1318
1319 value[13:0] of value is the divisor
1320 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1321
1322 H Type have all features above with
1323 {index[8],value[15:14]} is the encoded subdivisor
1324
1325 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1326 {index[0],value[15:14]} is the encoded subdivisor
1327
1328 AM Type chips have only four fractional subdivisors at value[15:14]
1329 for subdivisors 0, 0.5, 0.25, 0.125
1330*/
1331static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1332{
1333 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1334 int best_baud = 0;
1335 int divisor, best_divisor;
1336 if (baudrate >= clk/clk_div)
1337 {
1338 *encoded_divisor = 0;
1339 best_baud = clk/clk_div;
1340 }
1341 else if (baudrate >= clk/(clk_div + clk_div/2))
1342 {
1343 *encoded_divisor = 1;
1344 best_baud = clk/(clk_div + clk_div/2);
1345 }
1346 else if (baudrate >= clk/(2*clk_div))
1347 {
1348 *encoded_divisor = 2;
1349 best_baud = clk/(2*clk_div);
1350 }
1351 else
1352 {
1353 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1354 divisor = clk*16/clk_div / baudrate;
1355 if (divisor & 1) /* Decide if to round up or down*/
1356 best_divisor = divisor /2 +1;
1357 else
1358 best_divisor = divisor/2;
1359 if(best_divisor > 0x20000)
1360 best_divisor = 0x1ffff;
1361 best_baud = clk*16/clk_div/best_divisor;
1362 if (best_baud & 1) /* Decide if to round up or down*/
1363 best_baud = best_baud /2 +1;
1364 else
1365 best_baud = best_baud /2;
1366 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1367 }
1368 return best_baud;
1369}
1375static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1376 unsigned short *value, unsigned short *index)
1377{
1378 int best_baud;
1379 unsigned long encoded_divisor;
1380
1381 if (baudrate <= 0)
1382 {
1383 // Return error
1384 return -1;
1385 }
1386
1387#define H_CLK 120000000
1388#define C_CLK 48000000
1389 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1390 {
1391 if(baudrate*10 > H_CLK /0x3fff)
1392 {
1393 /* On H Devices, use 12 000 000 Baudrate when possible
1394 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1395 three fractional bits and a 120 MHz clock
1396 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1397 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1398 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1399 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1400 }
1401 else
1402 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1403 }
1404 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
1405 {
1406 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1407 }
1408 else
1409 {
1410 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1411 }
1412 // Split into "value" and "index" values
1413 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1414 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1415 {
1416 *index = (unsigned short)(encoded_divisor >> 8);
1417 *index &= 0xFF00;
1418 *index |= ftdi->index;
1419 }
1420 else
1421 *index = (unsigned short)(encoded_divisor >> 16);
1422
1423 // Return the nearest baud rate
1424 return best_baud;
1425}
1426
1431int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1432 unsigned short *value, unsigned short *index)
1433{
1434 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1435}
1436
1448int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1449{
1450 unsigned short value, index;
1451 int actual_baudrate;
1452
1453 if (ftdi == NULL || ftdi->usb_dev == NULL)
1454 ftdi_error_return(-3, "USB device unavailable");
1455
1456 if (ftdi->bitbang_enabled)
1457 {
1458 baudrate = baudrate*4;
1459 }
1460
1461 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1462 if (actual_baudrate <= 0)
1463 ftdi_error_return (-1, "Silly baudrate <= 0.");
1464
1465 // Check within tolerance (about 5%)
1466 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1467 || ((actual_baudrate < baudrate)
1468 ? (actual_baudrate * 21 < baudrate * 20)
1469 : (baudrate * 21 < actual_baudrate * 20)))
1470 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1471
1472 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1474 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1475 ftdi_error_return (-2, "Setting new baudrate failed");
1476
1477 ftdi->baudrate = baudrate;
1478 return 0;
1479}
1480
1495 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1496{
1497 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1498}
1499
1514 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1515 enum ftdi_break_type break_type)
1516{
1517 unsigned short value = bits;
1518
1519 if (ftdi == NULL || ftdi->usb_dev == NULL)
1520 ftdi_error_return(-2, "USB device unavailable");
1521
1522 switch (parity)
1523 {
1524 case NONE:
1525 value |= (0x00 << 8);
1526 break;
1527 case ODD:
1528 value |= (0x01 << 8);
1529 break;
1530 case EVEN:
1531 value |= (0x02 << 8);
1532 break;
1533 case MARK:
1534 value |= (0x03 << 8);
1535 break;
1536 case SPACE:
1537 value |= (0x04 << 8);
1538 break;
1539 }
1540
1541 switch (sbit)
1542 {
1543 case STOP_BIT_1:
1544 value |= (0x00 << 11);
1545 break;
1546 case STOP_BIT_15:
1547 value |= (0x01 << 11);
1548 break;
1549 case STOP_BIT_2:
1550 value |= (0x02 << 11);
1551 break;
1552 }
1553
1554 switch (break_type)
1555 {
1556 case BREAK_OFF:
1557 value |= (0x00 << 14);
1558 break;
1559 case BREAK_ON:
1560 value |= (0x01 << 14);
1561 break;
1562 }
1563
1564 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1565 SIO_SET_DATA_REQUEST, value,
1566 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1567 ftdi_error_return (-1, "Setting new line property failed");
1568
1569 return 0;
1570}
1571
1583int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1584{
1585 int offset = 0;
1586 int actual_length;
1587
1588 if (ftdi == NULL || ftdi->usb_dev == NULL)
1589 ftdi_error_return(-666, "USB device unavailable");
1590
1591 while (offset < size)
1592 {
1593 int write_size = ftdi->writebuffer_chunksize;
1594
1595 if (offset+write_size > size)
1596 write_size = size-offset;
1597
1598 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1599 ftdi_error_return(-1, "usb bulk write failed");
1600
1601 offset += actual_length;
1602 }
1603
1604 return offset;
1605}
1606
1607static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1608{
1609 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1610 struct ftdi_context *ftdi = tc->ftdi;
1611 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1612
1613 packet_size = ftdi->max_packet_size;
1614
1615 actual_length = transfer->actual_length;
1616
1617 if (actual_length > 2)
1618 {
1619 // skip FTDI status bytes.
1620 // Maybe stored in the future to enable modem use
1621 num_of_chunks = actual_length / packet_size;
1622 chunk_remains = actual_length % packet_size;
1623 //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);
1624
1625 ftdi->readbuffer_offset += 2;
1626 actual_length -= 2;
1627
1628 if (actual_length > packet_size - 2)
1629 {
1630 for (i = 1; i < num_of_chunks; i++)
1631 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1632 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1633 packet_size - 2);
1634 if (chunk_remains > 2)
1635 {
1636 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1637 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1638 chunk_remains-2);
1639 actual_length -= 2*num_of_chunks;
1640 }
1641 else
1642 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1643 }
1644
1645 if (actual_length > 0)
1646 {
1647 // data still fits in buf?
1648 if (tc->offset + actual_length <= tc->size)
1649 {
1650 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1651 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1652 tc->offset += actual_length;
1653
1656
1657 /* Did we read exactly the right amount of bytes? */
1658 if (tc->offset == tc->size)
1659 {
1660 //printf("read_data exact rem %d offset %d\n",
1661 //ftdi->readbuffer_remaining, offset);
1662 tc->completed = 1;
1663 return;
1664 }
1665 }
1666 else
1667 {
1668 // only copy part of the data or size <= readbuffer_chunksize
1669 int part_size = tc->size - tc->offset;
1670 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1671 tc->offset += part_size;
1672
1673 ftdi->readbuffer_offset += part_size;
1674 ftdi->readbuffer_remaining = actual_length - part_size;
1675
1676 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1677 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1678 tc->completed = 1;
1679 return;
1680 }
1681 }
1682 }
1683
1684 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1685 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1686 else
1687 {
1688 ret = libusb_submit_transfer (transfer);
1689 if (ret < 0)
1690 tc->completed = 1;
1691 }
1692}
1693
1694
1695static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1696{
1697 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1698 struct ftdi_context *ftdi = tc->ftdi;
1699
1700 tc->offset += transfer->actual_length;
1701
1702 if (tc->offset == tc->size)
1703 {
1704 tc->completed = 1;
1705 }
1706 else
1707 {
1708 int write_size = ftdi->writebuffer_chunksize;
1709 int ret;
1710
1711 if (tc->offset + write_size > tc->size)
1712 write_size = tc->size - tc->offset;
1713
1714 transfer->length = write_size;
1715 transfer->buffer = tc->buf + tc->offset;
1716
1717 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1718 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1719 else
1720 {
1721 ret = libusb_submit_transfer (transfer);
1722 if (ret < 0)
1723 tc->completed = 1;
1724 }
1725 }
1726}
1727
1728
1744{
1745 struct ftdi_transfer_control *tc;
1746 struct libusb_transfer *transfer;
1747 int write_size, ret;
1748
1749 if (ftdi == NULL || ftdi->usb_dev == NULL)
1750 return NULL;
1751
1752 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1753 if (!tc)
1754 return NULL;
1755
1756 transfer = libusb_alloc_transfer(0);
1757 if (!transfer)
1758 {
1759 free(tc);
1760 return NULL;
1761 }
1762
1763 tc->ftdi = ftdi;
1764 tc->completed = 0;
1765 tc->buf = buf;
1766 tc->size = size;
1767 tc->offset = 0;
1768
1769 if (size < (int)ftdi->writebuffer_chunksize)
1770 write_size = size;
1771 else
1772 write_size = ftdi->writebuffer_chunksize;
1773
1774 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1775 write_size, ftdi_write_data_cb, tc,
1777 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1778
1779 ret = libusb_submit_transfer(transfer);
1780 if (ret < 0)
1781 {
1782 libusb_free_transfer(transfer);
1783 free(tc);
1784 return NULL;
1785 }
1786 tc->transfer = transfer;
1787
1788 return tc;
1789}
1790
1806{
1807 struct ftdi_transfer_control *tc;
1808 struct libusb_transfer *transfer;
1809 int ret;
1810
1811 if (ftdi == NULL || ftdi->usb_dev == NULL)
1812 return NULL;
1813
1814 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1815 if (!tc)
1816 return NULL;
1817
1818 tc->ftdi = ftdi;
1819 tc->buf = buf;
1820 tc->size = size;
1821
1822 if (size <= (int)ftdi->readbuffer_remaining)
1823 {
1825
1826 // Fix offsets
1829
1830 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1831
1832 tc->completed = 1;
1833 tc->offset = size;
1834 tc->transfer = NULL;
1835 return tc;
1836 }
1837
1838 tc->completed = 0;
1839 if (ftdi->readbuffer_remaining != 0)
1840 {
1842
1844 }
1845 else
1846 tc->offset = 0;
1847
1848 transfer = libusb_alloc_transfer(0);
1849 if (!transfer)
1850 {
1851 free (tc);
1852 return NULL;
1853 }
1854
1857
1858 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);
1859 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1860
1861 ret = libusb_submit_transfer(transfer);
1862 if (ret < 0)
1863 {
1864 libusb_free_transfer(transfer);
1865 free (tc);
1866 return NULL;
1867 }
1868 tc->transfer = transfer;
1869
1870 return tc;
1871}
1872
1885{
1886 int ret;
1887 struct timeval to = { 0, 0 };
1888 while (!tc->completed)
1889 {
1890 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1891 &to, &tc->completed);
1892 if (ret < 0)
1893 {
1894 if (ret == LIBUSB_ERROR_INTERRUPTED)
1895 continue;
1896 libusb_cancel_transfer(tc->transfer);
1897 while (!tc->completed)
1898 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1899 &to, &tc->completed) < 0)
1900 break;
1901 libusb_free_transfer(tc->transfer);
1902 free (tc);
1903 return ret;
1904 }
1905 }
1906
1907 ret = tc->offset;
1912 if (tc->transfer)
1913 {
1914 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1915 ret = -1;
1916 libusb_free_transfer(tc->transfer);
1917 }
1918 free(tc);
1919 return ret;
1920}
1921
1932 struct timeval * to)
1933{
1934 struct timeval tv = { 0, 0 };
1935
1936 if (!tc->completed && tc->transfer != NULL)
1937 {
1938 if (to == NULL)
1939 to = &tv;
1940
1941 libusb_cancel_transfer(tc->transfer);
1942 while (!tc->completed)
1943 {
1944 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1945 break;
1946 }
1947 }
1948
1949 if (tc->transfer)
1950 libusb_free_transfer(tc->transfer);
1951
1952 free (tc);
1953}
1954
1965int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1966{
1967 if (ftdi == NULL)
1968 ftdi_error_return(-1, "ftdi context invalid");
1969
1970 ftdi->writebuffer_chunksize = chunksize;
1971 return 0;
1972}
1973
1983int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1984{
1985 if (ftdi == NULL)
1986 ftdi_error_return(-1, "ftdi context invalid");
1987
1988 *chunksize = ftdi->writebuffer_chunksize;
1989 return 0;
1990}
1991
2007int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
2008{
2009 int offset = 0, ret, i, num_of_chunks, chunk_remains;
2010 int packet_size;
2011 int actual_length = 1;
2012
2013 if (ftdi == NULL || ftdi->usb_dev == NULL)
2014 ftdi_error_return(-666, "USB device unavailable");
2015
2016 // Packet size sanity check (avoid division by zero)
2017 packet_size = ftdi->max_packet_size;
2018 if (packet_size == 0)
2019 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
2020
2021 // everything we want is still in the readbuffer?
2022 if (size <= (int)ftdi->readbuffer_remaining)
2023 {
2024 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
2025
2026 // Fix offsets
2027 ftdi->readbuffer_remaining -= size;
2028 ftdi->readbuffer_offset += size;
2029
2030 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
2031
2032 return size;
2033 }
2034 // something still in the readbuffer, but not enough to satisfy 'size'?
2035 if (ftdi->readbuffer_remaining != 0)
2036 {
2037 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
2038
2039 // Fix offset
2040 offset += ftdi->readbuffer_remaining;
2041 }
2042 // do the actual USB read
2043 while (offset < size && actual_length > 0)
2044 {
2045 ftdi->readbuffer_remaining = 0;
2046 ftdi->readbuffer_offset = 0;
2047 /* returns how much received */
2048 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
2049 if (ret < 0)
2050 ftdi_error_return(ret, "usb bulk read failed");
2051
2052 if (actual_length > 2)
2053 {
2054 // skip FTDI status bytes.
2055 // Maybe stored in the future to enable modem use
2056 num_of_chunks = actual_length / packet_size;
2057 chunk_remains = actual_length % packet_size;
2058 //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);
2059
2060 ftdi->readbuffer_offset += 2;
2061 actual_length -= 2;
2062
2063 if (actual_length > packet_size - 2)
2064 {
2065 for (i = 1; i < num_of_chunks; i++)
2066 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
2067 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
2068 packet_size - 2);
2069 if (chunk_remains > 2)
2070 {
2071 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
2072 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
2073 chunk_remains-2);
2074 actual_length -= 2*num_of_chunks;
2075 }
2076 else
2077 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
2078 }
2079 }
2080 else if (actual_length <= 2)
2081 {
2082 // no more data to read?
2083 return offset;
2084 }
2085 if (actual_length > 0)
2086 {
2087 // data still fits in buf?
2088 if (offset+actual_length <= size)
2089 {
2090 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
2091 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
2092 offset += actual_length;
2093
2094 /* Did we read exactly the right amount of bytes? */
2095 if (offset == size)
2096 //printf("read_data exact rem %d offset %d\n",
2097 //ftdi->readbuffer_remaining, offset);
2098 return offset;
2099 }
2100 else
2101 {
2102 // only copy part of the data or size <= readbuffer_chunksize
2103 int part_size = size-offset;
2104 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
2105
2106 ftdi->readbuffer_offset += part_size;
2107 ftdi->readbuffer_remaining = actual_length-part_size;
2108 offset += part_size;
2109
2110 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
2111 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
2112
2113 return offset;
2114 }
2115 }
2116 }
2117 // never reached
2118 return -127;
2119}
2120
2133int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
2134{
2135 unsigned char *new_buf;
2136
2137 if (ftdi == NULL)
2138 ftdi_error_return(-1, "ftdi context invalid");
2139
2140 // Invalidate all remaining data
2141 ftdi->readbuffer_offset = 0;
2142 ftdi->readbuffer_remaining = 0;
2143#ifdef __linux__
2144 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
2145 which is defined in libusb-1.0. Otherwise, each USB read request will
2146 be divided into multiple URBs. This will cause issues on Linux kernel
2147 older than 2.6.32. */
2148 if (chunksize > 16384)
2149 chunksize = 16384;
2150#endif
2151
2152 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2153 ftdi_error_return(-1, "out of memory for readbuffer");
2154
2155 ftdi->readbuffer = new_buf;
2156 ftdi->readbuffer_chunksize = chunksize;
2157
2158 return 0;
2159}
2160
2170int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2171{
2172 if (ftdi == NULL)
2173 ftdi_error_return(-1, "FTDI context invalid");
2174
2175 *chunksize = ftdi->readbuffer_chunksize;
2176 return 0;
2177}
2178
2191int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2192{
2193 unsigned short usb_val;
2194
2195 if (ftdi == NULL || ftdi->usb_dev == NULL)
2196 ftdi_error_return(-2, "USB device unavailable");
2197
2198 usb_val = bitmask; // low byte: bitmask
2199 usb_val |= (mode << 8);
2200 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)
2201 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2202
2203 ftdi->bitbang_mode = mode;
2204 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2205 return 0;
2206}
2207
2218{
2219 if (ftdi == NULL || ftdi->usb_dev == NULL)
2220 ftdi_error_return(-2, "USB device unavailable");
2221
2222 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)
2223 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2224
2225 ftdi->bitbang_enabled = 0;
2226 return 0;
2227}
2228
2229
2240int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2241{
2242 if (ftdi == NULL || ftdi->usb_dev == NULL)
2243 ftdi_error_return(-2, "USB device unavailable");
2244
2245 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)
2246 ftdi_error_return(-1, "read pins failed");
2247
2248 return 0;
2249}
2250
2266int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2267{
2268 unsigned short usb_val;
2269
2270 if (latency < 1)
2271 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2272
2273 if (ftdi == NULL || ftdi->usb_dev == NULL)
2274 ftdi_error_return(-3, "USB device unavailable");
2275
2276 usb_val = latency;
2277 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)
2278 ftdi_error_return(-2, "unable to set latency timer");
2279
2280 return 0;
2281}
2282
2293int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2294{
2295 unsigned short usb_val;
2296
2297 if (ftdi == NULL || ftdi->usb_dev == NULL)
2298 ftdi_error_return(-2, "USB device unavailable");
2299
2300 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)
2301 ftdi_error_return(-1, "reading latency timer failed");
2302
2303 *latency = (unsigned char)usb_val;
2304 return 0;
2305}
2306
2347int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2348{
2349 char usb_val[2];
2350
2351 if (ftdi == NULL || ftdi->usb_dev == NULL)
2352 ftdi_error_return(-2, "USB device unavailable");
2353
2354 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)
2355 ftdi_error_return(-1, "getting modem status failed");
2356
2357 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2358
2359 return 0;
2360}
2361
2375int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2376{
2377 if (ftdi == NULL || ftdi->usb_dev == NULL)
2378 ftdi_error_return(-2, "USB device unavailable");
2379
2380 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2381 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2382 NULL, 0, ftdi->usb_write_timeout) < 0)
2383 ftdi_error_return(-1, "set flow control failed");
2384
2385 return 0;
2386}
2387
2399int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
2400{
2401 if (ftdi == NULL || ftdi->usb_dev == NULL)
2402 ftdi_error_return(-2, "USB device unavailable");
2403
2404 uint16_t xonxoff = xon | (xoff << 8);
2405 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2407 NULL, 0, ftdi->usb_write_timeout) < 0)
2408 ftdi_error_return(-1, "set flow control failed");
2409
2410 return 0;
2411}
2412
2423int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2424{
2425 unsigned short usb_val;
2426
2427 if (ftdi == NULL || ftdi->usb_dev == NULL)
2428 ftdi_error_return(-2, "USB device unavailable");
2429
2430 if (state)
2431 usb_val = SIO_SET_DTR_HIGH;
2432 else
2433 usb_val = SIO_SET_DTR_LOW;
2434
2435 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2436 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2437 NULL, 0, ftdi->usb_write_timeout) < 0)
2438 ftdi_error_return(-1, "set dtr failed");
2439
2440 return 0;
2441}
2442
2453int ftdi_setrts(struct ftdi_context *ftdi, int state)
2454{
2455 unsigned short usb_val;
2456
2457 if (ftdi == NULL || ftdi->usb_dev == NULL)
2458 ftdi_error_return(-2, "USB device unavailable");
2459
2460 if (state)
2461 usb_val = SIO_SET_RTS_HIGH;
2462 else
2463 usb_val = SIO_SET_RTS_LOW;
2464
2465 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2466 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2467 NULL, 0, ftdi->usb_write_timeout) < 0)
2468 ftdi_error_return(-1, "set of rts failed");
2469
2470 return 0;
2471}
2472
2484int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2485{
2486 unsigned short usb_val;
2487
2488 if (ftdi == NULL || ftdi->usb_dev == NULL)
2489 ftdi_error_return(-2, "USB device unavailable");
2490
2491 if (dtr)
2492 usb_val = SIO_SET_DTR_HIGH;
2493 else
2494 usb_val = SIO_SET_DTR_LOW;
2495
2496 if (rts)
2497 usb_val |= SIO_SET_RTS_HIGH;
2498 else
2499 usb_val |= SIO_SET_RTS_LOW;
2500
2501 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2502 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2503 NULL, 0, ftdi->usb_write_timeout) < 0)
2504 ftdi_error_return(-1, "set of rts/dtr failed");
2505
2506 return 0;
2507}
2508
2521 unsigned char eventch, unsigned char enable)
2522{
2523 unsigned short usb_val;
2524
2525 if (ftdi == NULL || ftdi->usb_dev == NULL)
2526 ftdi_error_return(-2, "USB device unavailable");
2527
2528 usb_val = eventch;
2529 if (enable)
2530 usb_val |= 1 << 8;
2531
2532 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)
2533 ftdi_error_return(-1, "setting event character failed");
2534
2535 return 0;
2536}
2537
2550 unsigned char errorch, unsigned char enable)
2551{
2552 unsigned short usb_val;
2553
2554 if (ftdi == NULL || ftdi->usb_dev == NULL)
2555 ftdi_error_return(-2, "USB device unavailable");
2556
2557 usb_val = errorch;
2558 if (enable)
2559 usb_val |= 1 << 8;
2560
2561 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)
2562 ftdi_error_return(-1, "setting error character failed");
2563
2564 return 0;
2565}
2566
2579int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2580 char * product, char * serial)
2581{
2582 struct ftdi_eeprom *eeprom;
2583
2584 if (ftdi == NULL)
2585 ftdi_error_return(-1, "No struct ftdi_context");
2586
2587 if (ftdi->eeprom == NULL)
2588 ftdi_error_return(-2,"No struct ftdi_eeprom");
2589
2590 eeprom = ftdi->eeprom;
2591 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2592
2593 if (ftdi->usb_dev == NULL)
2594 ftdi_error_return(-3, "No connected device or device not yet opened");
2595
2596 eeprom->vendor_id = 0x0403;
2597 eeprom->use_serial = 1;
2598 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2599 (ftdi->type == TYPE_R))
2600 eeprom->product_id = 0x6001;
2601 else if (ftdi->type == TYPE_4232H)
2602 eeprom->product_id = 0x6011;
2603 else if (ftdi->type == TYPE_232H)
2604 eeprom->product_id = 0x6014;
2605 else if (ftdi->type == TYPE_230X)
2606 eeprom->product_id = 0x6015;
2607 else
2608 eeprom->product_id = 0x6010;
2609
2610 if (ftdi->type == TYPE_AM)
2611 eeprom->usb_version = 0x0101;
2612 else
2613 eeprom->usb_version = 0x0200;
2614 eeprom->max_power = 100;
2615
2616 if (eeprom->manufacturer)
2617 free (eeprom->manufacturer);
2618 eeprom->manufacturer = NULL;
2619 if (manufacturer)
2620 {
2621 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2622 if (eeprom->manufacturer)
2623 strcpy(eeprom->manufacturer, manufacturer);
2624 }
2625
2626 if (eeprom->product)
2627 free (eeprom->product);
2628 eeprom->product = NULL;
2629 if(product)
2630 {
2631 eeprom->product = (char *)malloc(strlen(product)+1);
2632 if (eeprom->product)
2633 strcpy(eeprom->product, product);
2634 }
2635 else
2636 {
2637 const char* default_product;
2638 switch(ftdi->type)
2639 {
2640 case TYPE_AM: default_product = "AM"; break;
2641 case TYPE_BM: default_product = "BM"; break;
2642 case TYPE_2232C: default_product = "Dual RS232"; break;
2643 case TYPE_R: default_product = "FT232R USB UART"; break;
2644 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2645 case TYPE_4232H: default_product = "FT4232H"; break;
2646 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2647 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2648 default:
2649 ftdi_error_return(-3, "Unknown chip type");
2650 }
2651 eeprom->product = (char *)malloc(strlen(default_product) +1);
2652 if (eeprom->product)
2653 strcpy(eeprom->product, default_product);
2654 }
2655
2656 if (eeprom->serial)
2657 free (eeprom->serial);
2658 eeprom->serial = NULL;
2659 if (serial)
2660 {
2661 eeprom->serial = (char *)malloc(strlen(serial)+1);
2662 if (eeprom->serial)
2663 strcpy(eeprom->serial, serial);
2664 }
2665
2666 if (ftdi->type == TYPE_R)
2667 {
2668 eeprom->max_power = 90;
2669 eeprom->size = 0x80;
2670 eeprom->cbus_function[0] = CBUS_TXLED;
2671 eeprom->cbus_function[1] = CBUS_RXLED;
2672 eeprom->cbus_function[2] = CBUS_TXDEN;
2673 eeprom->cbus_function[3] = CBUS_PWREN;
2674 eeprom->cbus_function[4] = CBUS_SLEEP;
2675 }
2676 else if (ftdi->type == TYPE_230X)
2677 {
2678 eeprom->max_power = 90;
2679 eeprom->size = 0x100;
2680 eeprom->cbus_function[0] = CBUSX_TXDEN;
2681 eeprom->cbus_function[1] = CBUSX_RXLED;
2682 eeprom->cbus_function[2] = CBUSX_TXLED;
2683 eeprom->cbus_function[3] = CBUSX_SLEEP;
2684 }
2685 else
2686 {
2687 if(ftdi->type == TYPE_232H)
2688 {
2689 int i;
2690 for (i=0; i<10; i++)
2691 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2692 }
2693 eeprom->size = -1;
2694 }
2695 switch (ftdi->type)
2696 {
2697 case TYPE_AM:
2698 eeprom->release_number = 0x0200;
2699 break;
2700 case TYPE_BM:
2701 eeprom->release_number = 0x0400;
2702 break;
2703 case TYPE_2232C:
2704 eeprom->release_number = 0x0500;
2705 break;
2706 case TYPE_R:
2707 eeprom->release_number = 0x0600;
2708 break;
2709 case TYPE_2232H:
2710 eeprom->release_number = 0x0700;
2711 break;
2712 case TYPE_4232H:
2713 eeprom->release_number = 0x0800;
2714 break;
2715 case TYPE_232H:
2716 eeprom->release_number = 0x0900;
2717 break;
2718 case TYPE_230X:
2719 eeprom->release_number = 0x1000;
2720 break;
2721 default:
2722 eeprom->release_number = 0x00;
2723 }
2724 return 0;
2725}
2726
2728 const char * product, const char * serial)
2729{
2730 struct ftdi_eeprom *eeprom;
2731
2732 if (ftdi == NULL)
2733 ftdi_error_return(-1, "No struct ftdi_context");
2734
2735 if (ftdi->eeprom == NULL)
2736 ftdi_error_return(-2,"No struct ftdi_eeprom");
2737
2738 eeprom = ftdi->eeprom;
2739
2740 if (ftdi->usb_dev == NULL)
2741 ftdi_error_return(-3, "No connected device or device not yet opened");
2742
2743 if (manufacturer)
2744 {
2745 if (eeprom->manufacturer)
2746 free (eeprom->manufacturer);
2747 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
2748 if (eeprom->manufacturer)
2749 strcpy(eeprom->manufacturer, manufacturer);
2750 }
2751
2752 if(product)
2753 {
2754 if (eeprom->product)
2755 free (eeprom->product);
2756 eeprom->product = (char *)malloc(strlen(product)+1);
2757 if (eeprom->product)
2758 strcpy(eeprom->product, product);
2759 }
2760
2761 if (serial)
2762 {
2763 if (eeprom->serial)
2764 free (eeprom->serial);
2765 eeprom->serial = (char *)malloc(strlen(serial)+1);
2766 if (eeprom->serial)
2767 {
2768 strcpy(eeprom->serial, serial);
2769 eeprom->use_serial = 1;
2770 }
2771 }
2772 return 0;
2773}
2774
2794 char *manufacturer, int mnf_len,
2795 char *product, int prod_len,
2796 char *serial, int serial_len)
2797{
2798 struct ftdi_eeprom *eeprom;
2799
2800 if (ftdi == NULL)
2801 ftdi_error_return(-1, "No struct ftdi_context");
2802 if (ftdi->eeprom == NULL)
2803 ftdi_error_return(-2, "No struct ftdi_eeprom");
2804
2805 eeprom = ftdi->eeprom;
2806
2807 if (manufacturer)
2808 {
2809 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
2810 if (mnf_len > 0)
2811 manufacturer[mnf_len - 1] = '\0';
2812 }
2813
2814 if (product)
2815 {
2816 strncpy(product, eeprom->product, prod_len);
2817 if (prod_len > 0)
2818 product[prod_len - 1] = '\0';
2819 }
2820
2821 if (serial)
2822 {
2823 strncpy(serial, eeprom->serial, serial_len);
2824 if (serial_len > 0)
2825 serial[serial_len - 1] = '\0';
2826 }
2827
2828 return 0;
2829}
2830
2831/*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2832void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2833{
2834 int i;
2835 for(i=0; i<5; i++)
2836 {
2837 int mode_low, mode_high;
2838 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2839 mode_low = CBUSH_TRISTATE;
2840 else
2841 mode_low = eeprom->cbus_function[2*i];
2842 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2843 mode_high = CBUSH_TRISTATE;
2844 else
2845 mode_high = eeprom->cbus_function[2*i+1];
2846
2847 output[0x18+i] = (mode_high <<4) | mode_low;
2848 }
2849}
2850/* Return the bits for the encoded EEPROM Structure of a requested Mode
2851 *
2852 */
2853static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2854{
2855 switch (chip)
2856 {
2857 case TYPE_2232H:
2858 case TYPE_2232C:
2859 {
2860 switch (type)
2861 {
2862 case CHANNEL_IS_UART: return 0;
2863 case CHANNEL_IS_FIFO: return 0x01;
2864 case CHANNEL_IS_OPTO: return 0x02;
2865 case CHANNEL_IS_CPU : return 0x04;
2866 default: return 0;
2867 }
2868 }
2869 case TYPE_232H:
2870 {
2871 switch (type)
2872 {
2873 case CHANNEL_IS_UART : return 0;
2874 case CHANNEL_IS_FIFO : return 0x01;
2875 case CHANNEL_IS_OPTO : return 0x02;
2876 case CHANNEL_IS_CPU : return 0x04;
2877 case CHANNEL_IS_FT1284 : return 0x08;
2878 default: return 0;
2879 }
2880 }
2881 case TYPE_R:
2882 {
2883 switch (type)
2884 {
2885 case CHANNEL_IS_UART : return 0;
2886 case CHANNEL_IS_FIFO : return 0x01;
2887 default: return 0;
2888 }
2889 }
2890 case TYPE_230X: /* FT230X is only UART */
2891 default: return 0;
2892 }
2893 return 0;
2894}
2895
2911{
2912 unsigned char i, j, eeprom_size_mask;
2913 unsigned short checksum, value;
2914 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2915 int user_area_size, free_start, free_end;
2916 struct ftdi_eeprom *eeprom;
2917 unsigned char * output;
2918
2919 if (ftdi == NULL)
2920 ftdi_error_return(-2,"No context");
2921 if (ftdi->eeprom == NULL)
2922 ftdi_error_return(-2,"No eeprom structure");
2923
2924 eeprom= ftdi->eeprom;
2925 output = eeprom->buf;
2926
2927 if (eeprom->chip == -1)
2928 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2929
2930 if (eeprom->size == -1)
2931 {
2932 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2933 eeprom->size = 0x100;
2934 else
2935 eeprom->size = 0x80;
2936 }
2937
2938 if (eeprom->manufacturer != NULL)
2939 manufacturer_size = strlen(eeprom->manufacturer);
2940 if (eeprom->product != NULL)
2941 product_size = strlen(eeprom->product);
2942 if (eeprom->serial != NULL)
2943 serial_size = strlen(eeprom->serial);
2944
2945 // eeprom size check
2946 switch (ftdi->type)
2947 {
2948 case TYPE_AM:
2949 case TYPE_BM:
2950 case TYPE_R:
2951 user_area_size = 96; // base size for strings (total of 48 characters)
2952 break;
2953 case TYPE_2232C:
2954 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2955 break;
2956 case TYPE_230X:
2957 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2958 break;
2959 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2960 case TYPE_4232H:
2961 user_area_size = 86;
2962 break;
2963 case TYPE_232H:
2964 user_area_size = 80;
2965 break;
2966 default:
2967 user_area_size = 0;
2968 break;
2969 }
2970 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2971
2972 if (user_area_size < 0)
2973 ftdi_error_return(-1,"eeprom size exceeded");
2974
2975 // empty eeprom
2976 if (ftdi->type == TYPE_230X)
2977 {
2978 /* FT230X have a reserved section in the middle of the MTP,
2979 which cannot be written to, but must be included in the checksum */
2980 memset(ftdi->eeprom->buf, 0, 0x80);
2981 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2982 }
2983 else
2984 {
2985 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2986 }
2987
2988 // Bytes and Bits set for all Types
2989
2990 // Addr 02: Vendor ID
2991 output[0x02] = eeprom->vendor_id;
2992 output[0x03] = eeprom->vendor_id >> 8;
2993
2994 // Addr 04: Product ID
2995 output[0x04] = eeprom->product_id;
2996 output[0x05] = eeprom->product_id >> 8;
2997
2998 // Addr 06: Device release number (0400h for BM features)
2999 output[0x06] = eeprom->release_number;
3000 output[0x07] = eeprom->release_number >> 8;
3001
3002 // Addr 08: Config descriptor
3003 // Bit 7: always 1
3004 // Bit 6: 1 if this device is self powered, 0 if bus powered
3005 // Bit 5: 1 if this device uses remote wakeup
3006 // Bit 4-0: reserved - 0
3007 j = 0x80;
3008 if (eeprom->self_powered)
3009 j |= 0x40;
3010 if (eeprom->remote_wakeup)
3011 j |= 0x20;
3012 output[0x08] = j;
3013
3014 // Addr 09: Max power consumption: max power = value * 2 mA
3015 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
3016
3017 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
3018 {
3019 // Addr 0A: Chip configuration
3020 // Bit 7: 0 - reserved
3021 // Bit 6: 0 - reserved
3022 // Bit 5: 0 - reserved
3023 // Bit 4: 1 - Change USB version
3024 // Bit 3: 1 - Use the serial number string
3025 // Bit 2: 1 - Enable suspend pull downs for lower power
3026 // Bit 1: 1 - Out EndPoint is Isochronous
3027 // Bit 0: 1 - In EndPoint is Isochronous
3028 //
3029 j = 0;
3030 if (eeprom->in_is_isochronous)
3031 j = j | 1;
3032 if (eeprom->out_is_isochronous)
3033 j = j | 2;
3034 output[0x0A] = j;
3035 }
3036
3037 // Dynamic content
3038 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
3039 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
3040 // 0xa0 (TYPE_232H)
3041 i = 0;
3042 switch (ftdi->type)
3043 {
3044 case TYPE_2232H:
3045 case TYPE_4232H:
3046 i += 2;
3047 case TYPE_R:
3048 i += 2;
3049 case TYPE_2232C:
3050 i += 2;
3051 case TYPE_AM:
3052 case TYPE_BM:
3053 i += 0x94;
3054 break;
3055 case TYPE_232H:
3056 case TYPE_230X:
3057 i = 0xa0;
3058 break;
3059 }
3060 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
3061 eeprom_size_mask = eeprom->size -1;
3062 free_end = i & eeprom_size_mask;
3063
3064 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3065 // Addr 0F: Length of manufacturer string
3066 // Output manufacturer
3067 output[0x0E] = i; // calculate offset
3068 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
3069 output[i & eeprom_size_mask] = 0x03, i++; // type: string
3070 for (j = 0; j < manufacturer_size; j++)
3071 {
3072 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
3073 output[i & eeprom_size_mask] = 0x00, i++;
3074 }
3075 output[0x0F] = manufacturer_size*2 + 2;
3076
3077 // Addr 10: Offset of the product string + 0x80, calculated later
3078 // Addr 11: Length of product string
3079 output[0x10] = i | 0x80; // calculate offset
3080 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
3081 output[i & eeprom_size_mask] = 0x03, i++;
3082 for (j = 0; j < product_size; j++)
3083 {
3084 output[i & eeprom_size_mask] = eeprom->product[j], i++;
3085 output[i & eeprom_size_mask] = 0x00, i++;
3086 }
3087 output[0x11] = product_size*2 + 2;
3088
3089 // Addr 12: Offset of the serial string + 0x80, calculated later
3090 // Addr 13: Length of serial string
3091 output[0x12] = i | 0x80; // calculate offset
3092 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
3093 output[i & eeprom_size_mask] = 0x03, i++;
3094 for (j = 0; j < serial_size; j++)
3095 {
3096 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
3097 output[i & eeprom_size_mask] = 0x00, i++;
3098 }
3099
3100 // Legacy port name and PnP fields for FT2232 and newer chips
3101 if (ftdi->type > TYPE_BM)
3102 {
3103 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
3104 i++;
3105 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
3106 i++;
3107 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
3108 i++;
3109 }
3110
3111 output[0x13] = serial_size*2 + 2;
3112
3113 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
3114 {
3115 if (eeprom->use_serial)
3116 output[0x0A] |= USE_SERIAL_NUM;
3117 else
3118 output[0x0A] &= ~USE_SERIAL_NUM;
3119 }
3120
3121 /* Bytes and Bits specific to (some) types
3122 Write linear, as this allows easier fixing*/
3123 switch (ftdi->type)
3124 {
3125 case TYPE_AM:
3126 break;
3127 case TYPE_BM:
3128 output[0x0C] = eeprom->usb_version & 0xff;
3129 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3130 if (eeprom->use_usb_version)
3131 output[0x0A] |= USE_USB_VERSION_BIT;
3132 else
3133 output[0x0A] &= ~USE_USB_VERSION_BIT;
3134
3135 break;
3136 case TYPE_2232C:
3137
3138 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
3139 if (eeprom->channel_a_driver)
3140 output[0x00] |= DRIVER_VCP;
3141 else
3142 output[0x00] &= ~DRIVER_VCP;
3143
3144 if (eeprom->high_current_a)
3145 output[0x00] |= HIGH_CURRENT_DRIVE;
3146 else
3147 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3148
3149 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
3150 if (eeprom->channel_b_driver)
3151 output[0x01] |= DRIVER_VCP;
3152 else
3153 output[0x01] &= ~DRIVER_VCP;
3154
3155 if (eeprom->high_current_b)
3156 output[0x01] |= HIGH_CURRENT_DRIVE;
3157 else
3158 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3159
3160 if (eeprom->in_is_isochronous)
3161 output[0x0A] |= 0x1;
3162 else
3163 output[0x0A] &= ~0x1;
3164 if (eeprom->out_is_isochronous)
3165 output[0x0A] |= 0x2;
3166 else
3167 output[0x0A] &= ~0x2;
3168 if (eeprom->suspend_pull_downs)
3169 output[0x0A] |= 0x4;
3170 else
3171 output[0x0A] &= ~0x4;
3172 if (eeprom->use_usb_version)
3173 output[0x0A] |= USE_USB_VERSION_BIT;
3174 else
3175 output[0x0A] &= ~USE_USB_VERSION_BIT;
3176
3177 output[0x0C] = eeprom->usb_version & 0xff;
3178 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3179 output[0x14] = eeprom->chip;
3180 break;
3181 case TYPE_R:
3182 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
3183 if (eeprom->high_current)
3184 output[0x00] |= HIGH_CURRENT_DRIVE_R;
3185
3186 /* Field is inverted for TYPE_R: Bit 00.3 set to 1 is D2XX, VCP is 0 */
3187 if (eeprom->channel_a_driver)
3188 output[0x00] &= ~DRIVER_VCP;
3189 else
3190 output[0x00] |= DRIVER_VCP;
3191
3192 if (eeprom->external_oscillator)
3193 output[0x00] |= 0x02;
3194 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
3195
3196 if (eeprom->suspend_pull_downs)
3197 output[0x0A] |= 0x4;
3198 else
3199 output[0x0A] &= ~0x4;
3200 output[0x0B] = eeprom->invert;
3201 output[0x0C] = eeprom->usb_version & 0xff;
3202 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
3203
3204 if (eeprom->cbus_function[0] > CBUS_BB_RD)
3205 output[0x14] = CBUS_TXLED;
3206 else
3207 output[0x14] = eeprom->cbus_function[0];
3208
3209 if (eeprom->cbus_function[1] > CBUS_BB_RD)
3210 output[0x14] |= CBUS_RXLED<<4;
3211 else
3212 output[0x14] |= eeprom->cbus_function[1]<<4;
3213
3214 if (eeprom->cbus_function[2] > CBUS_BB_RD)
3215 output[0x15] = CBUS_TXDEN;
3216 else
3217 output[0x15] = eeprom->cbus_function[2];
3218
3219 if (eeprom->cbus_function[3] > CBUS_BB_RD)
3220 output[0x15] |= CBUS_PWREN<<4;
3221 else
3222 output[0x15] |= eeprom->cbus_function[3]<<4;
3223
3224 if (eeprom->cbus_function[4] > CBUS_CLK6)
3225 output[0x16] = CBUS_SLEEP;
3226 else
3227 output[0x16] = eeprom->cbus_function[4];
3228 break;
3229 case TYPE_2232H:
3230 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
3231 if (eeprom->channel_a_driver)
3232 output[0x00] |= DRIVER_VCP;
3233 else
3234 output[0x00] &= ~DRIVER_VCP;
3235
3236 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
3237 if (eeprom->channel_b_driver)
3238 output[0x01] |= DRIVER_VCP;
3239 else
3240 output[0x01] &= ~DRIVER_VCP;
3241
3242 if (eeprom->suspend_dbus7)
3243 output[0x01] |= SUSPEND_DBUS7_BIT;
3244 else
3245 output[0x01] &= ~SUSPEND_DBUS7_BIT;
3246
3247 if (eeprom->suspend_pull_downs)
3248 output[0x0A] |= 0x4;
3249 else
3250 output[0x0A] &= ~0x4;
3251
3252 if (eeprom->group0_drive > DRIVE_16MA)
3253 output[0x0c] |= DRIVE_16MA;
3254 else
3255 output[0x0c] |= eeprom->group0_drive;
3256 if (eeprom->group0_schmitt)
3257 output[0x0c] |= IS_SCHMITT;
3258 if (eeprom->group0_slew)
3259 output[0x0c] |= SLOW_SLEW;
3260
3261 if (eeprom->group1_drive > DRIVE_16MA)
3262 output[0x0c] |= DRIVE_16MA<<4;
3263 else
3264 output[0x0c] |= eeprom->group1_drive<<4;
3265 if (eeprom->group1_schmitt)
3266 output[0x0c] |= IS_SCHMITT<<4;
3267 if (eeprom->group1_slew)
3268 output[0x0c] |= SLOW_SLEW<<4;
3269
3270 if (eeprom->group2_drive > DRIVE_16MA)
3271 output[0x0d] |= DRIVE_16MA;
3272 else
3273 output[0x0d] |= eeprom->group2_drive;
3274 if (eeprom->group2_schmitt)
3275 output[0x0d] |= IS_SCHMITT;
3276 if (eeprom->group2_slew)
3277 output[0x0d] |= SLOW_SLEW;
3278
3279 if (eeprom->group3_drive > DRIVE_16MA)
3280 output[0x0d] |= DRIVE_16MA<<4;
3281 else
3282 output[0x0d] |= eeprom->group3_drive<<4;
3283 if (eeprom->group3_schmitt)
3284 output[0x0d] |= IS_SCHMITT<<4;
3285 if (eeprom->group3_slew)
3286 output[0x0d] |= SLOW_SLEW<<4;
3287
3288 output[0x18] = eeprom->chip;
3289
3290 break;
3291 case TYPE_4232H:
3292 if (eeprom->channel_a_driver)
3293 output[0x00] |= DRIVER_VCP;
3294 else
3295 output[0x00] &= ~DRIVER_VCP;
3296 if (eeprom->channel_b_driver)
3297 output[0x01] |= DRIVER_VCP;
3298 else
3299 output[0x01] &= ~DRIVER_VCP;
3300 if (eeprom->channel_c_driver)
3301 output[0x00] |= (DRIVER_VCP << 4);
3302 else
3303 output[0x00] &= ~(DRIVER_VCP << 4);
3304 if (eeprom->channel_d_driver)
3305 output[0x01] |= (DRIVER_VCP << 4);
3306 else
3307 output[0x01] &= ~(DRIVER_VCP << 4);
3308
3309 if (eeprom->suspend_pull_downs)
3310 output[0x0a] |= 0x4;
3311 else
3312 output[0x0a] &= ~0x4;
3313
3314 if (eeprom->channel_a_rs485enable)
3315 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3316 else
3317 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3318 if (eeprom->channel_b_rs485enable)
3319 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3320 else
3321 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3322 if (eeprom->channel_c_rs485enable)
3323 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3324 else
3325 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3326 if (eeprom->channel_d_rs485enable)
3327 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3328 else
3329 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3330
3331 if (eeprom->group0_drive > DRIVE_16MA)
3332 output[0x0c] |= DRIVE_16MA;
3333 else
3334 output[0x0c] |= eeprom->group0_drive;
3335 if (eeprom->group0_schmitt)
3336 output[0x0c] |= IS_SCHMITT;
3337 if (eeprom->group0_slew)
3338 output[0x0c] |= SLOW_SLEW;
3339
3340 if (eeprom->group1_drive > DRIVE_16MA)
3341 output[0x0c] |= DRIVE_16MA<<4;
3342 else
3343 output[0x0c] |= eeprom->group1_drive<<4;
3344 if (eeprom->group1_schmitt)
3345 output[0x0c] |= IS_SCHMITT<<4;
3346 if (eeprom->group1_slew)
3347 output[0x0c] |= SLOW_SLEW<<4;
3348
3349 if (eeprom->group2_drive > DRIVE_16MA)
3350 output[0x0d] |= DRIVE_16MA;
3351 else
3352 output[0x0d] |= eeprom->group2_drive;
3353 if (eeprom->group2_schmitt)
3354 output[0x0d] |= IS_SCHMITT;
3355 if (eeprom->group2_slew)
3356 output[0x0d] |= SLOW_SLEW;
3357
3358 if (eeprom->group3_drive > DRIVE_16MA)
3359 output[0x0d] |= DRIVE_16MA<<4;
3360 else
3361 output[0x0d] |= eeprom->group3_drive<<4;
3362 if (eeprom->group3_schmitt)
3363 output[0x0d] |= IS_SCHMITT<<4;
3364 if (eeprom->group3_slew)
3365 output[0x0d] |= SLOW_SLEW<<4;
3366
3367 output[0x18] = eeprom->chip;
3368
3369 break;
3370 case TYPE_232H:
3371 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3372 if (eeprom->channel_a_driver)
3373 output[0x00] |= DRIVER_VCPH;
3374 else
3375 output[0x00] &= ~DRIVER_VCPH;
3376
3377 if (eeprom->powersave)
3378 output[0x01] |= POWER_SAVE_DISABLE_H;
3379 else
3380 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3381
3382 if (eeprom->suspend_pull_downs)
3383 output[0x0a] |= 0x4;
3384 else
3385 output[0x0a] &= ~0x4;
3386
3387 if (eeprom->clock_polarity)
3388 output[0x01] |= FT1284_CLK_IDLE_STATE;
3389 else
3390 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3391 if (eeprom->data_order)
3392 output[0x01] |= FT1284_DATA_LSB;
3393 else
3394 output[0x01] &= ~FT1284_DATA_LSB;
3395 if (eeprom->flow_control)
3396 output[0x01] |= FT1284_FLOW_CONTROL;
3397 else
3398 output[0x01] &= ~FT1284_FLOW_CONTROL;
3399
3400 if (eeprom->group0_drive > DRIVE_16MA)
3401 output[0x0c] |= DRIVE_16MA;
3402 else
3403 output[0x0c] |= eeprom->group0_drive;
3404 if (eeprom->group0_schmitt)
3405 output[0x0c] |= IS_SCHMITT;
3406 if (eeprom->group0_slew)
3407 output[0x0c] |= SLOW_SLEW;
3408
3409 if (eeprom->group1_drive > DRIVE_16MA)
3410 output[0x0d] |= DRIVE_16MA;
3411 else
3412 output[0x0d] |= eeprom->group1_drive;
3413 if (eeprom->group1_schmitt)
3414 output[0x0d] |= IS_SCHMITT;
3415 if (eeprom->group1_slew)
3416 output[0x0d] |= SLOW_SLEW;
3417
3418 set_ft232h_cbus(eeprom, output);
3419
3420 output[0x1e] = eeprom->chip;
3421 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3422 break;
3423 case TYPE_230X:
3424 output[0x00] = 0x80; /* Actually, leave the default value */
3425 /*FIXME: Make DBUS & CBUS Control configurable*/
3426 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3427 for (j = 0; j <= 6; j++)
3428 {
3429 output[0x1a + j] = eeprom->cbus_function[j];
3430 }
3431 output[0x0b] = eeprom->invert;
3432 break;
3433 }
3434
3435 /* First address without use */
3436 free_start = 0;
3437 switch (ftdi->type)
3438 {
3439 case TYPE_230X:
3440 free_start += 2;
3441 case TYPE_232H:
3442 free_start += 6;
3443 case TYPE_2232H:
3444 case TYPE_4232H:
3445 free_start += 2;
3446 case TYPE_R:
3447 free_start += 2;
3448 case TYPE_2232C:
3449 free_start++;
3450 case TYPE_AM:
3451 case TYPE_BM:
3452 free_start += 0x14;
3453 }
3454
3455 /* Arbitrary user data */
3456 if (eeprom->user_data && eeprom->user_data_size >= 0)
3457 {
3458 if (eeprom->user_data_addr < free_start)
3459 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3460 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3461 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3462 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3463 ftdi_error_return(-1,"eeprom size exceeded");
3464 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3465 }
3466
3467 // calculate checksum
3468 checksum = 0xAAAA;
3469
3470 for (i = 0; i < eeprom->size/2-1; i++)
3471 {
3472 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3473 {
3474 /* FT230X has a user section in the MTP which is not part of the checksum */
3475 i = 0x40;
3476 }
3477 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3478 uint16_t data;
3479 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3480 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3481 i = 0x50;
3482 }
3483 value = data;
3484 }
3485 else {
3486 value = output[i*2];
3487 value += output[(i*2)+1] << 8;
3488 }
3489 checksum = value^checksum;
3490 checksum = (checksum << 1) | (checksum >> 15);
3491 }
3492
3493 output[eeprom->size-2] = checksum;
3494 output[eeprom->size-1] = checksum >> 8;
3495
3497 return user_area_size;
3498}
3499/* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3500 * EEPROM structure
3501 *
3502 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3503 */
3504static unsigned char bit2type(unsigned char bits)
3505{
3506 switch (bits)
3507 {
3508 case 0: return CHANNEL_IS_UART;
3509 case 1: return CHANNEL_IS_FIFO;
3510 case 2: return CHANNEL_IS_OPTO;
3511 case 4: return CHANNEL_IS_CPU;
3512 case 8: return CHANNEL_IS_FT1284;
3513 default:
3514 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3515 bits);
3516 }
3517 return 0;
3518}
3519/* Decode 230X / 232R type chips invert bits
3520 * Prints directly to stdout.
3521*/
3522static void print_inverted_bits(int invert)
3523{
3524 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3525 int i;
3526
3527 fprintf(stdout,"Inverted bits:");
3528 for (i=0; i<8; i++)
3529 if ((invert & (1<<i)) == (1<<i))
3530 fprintf(stdout," %s",r_bits[i]);
3531
3532 fprintf(stdout,"\n");
3533}
3548int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3549{
3550 int i, j;
3551 unsigned short checksum, eeprom_checksum, value;
3552 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3553 int eeprom_size;
3554 struct ftdi_eeprom *eeprom;
3555 unsigned char *buf = NULL;
3556
3557 if (ftdi == NULL)
3558 ftdi_error_return(-1,"No context");
3559 if (ftdi->eeprom == NULL)
3560 ftdi_error_return(-1,"No eeprom structure");
3561
3562 eeprom = ftdi->eeprom;
3563 eeprom_size = eeprom->size;
3564 buf = ftdi->eeprom->buf;
3565
3566 // Addr 02: Vendor ID
3567 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3568
3569 // Addr 04: Product ID
3570 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3571
3572 // Addr 06: Device release number
3573 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3574
3575 // Addr 08: Config descriptor
3576 // Bit 7: always 1
3577 // Bit 6: 1 if this device is self powered, 0 if bus powered
3578 // Bit 5: 1 if this device uses remote wakeup
3579 eeprom->self_powered = !!(buf[0x08] & 0x40);
3580 eeprom->remote_wakeup = !!(buf[0x08] & 0x20);
3581
3582 // Addr 09: Max power consumption: max power = value * 2 mA
3583 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3584
3585 // Addr 0A: Chip configuration
3586 // Bit 7: 0 - reserved
3587 // Bit 6: 0 - reserved
3588 // Bit 5: 0 - reserved
3589 // Bit 4: 1 - Change USB version on BM and 2232C
3590 // Bit 3: 1 - Use the serial number string
3591 // Bit 2: 1 - Enable suspend pull downs for lower power
3592 // Bit 1: 1 - Out EndPoint is Isochronous
3593 // Bit 0: 1 - In EndPoint is Isochronous
3594 //
3595 eeprom->in_is_isochronous = !!(buf[0x0A]&0x01);
3596 eeprom->out_is_isochronous = !!(buf[0x0A]&0x02);
3597 eeprom->suspend_pull_downs = !!(buf[0x0A]&0x04);
3598 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3599 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3600
3601 // Addr 0C: USB version low byte when 0x0A
3602 // Addr 0D: USB version high byte when 0x0A
3603 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3604
3605 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3606 // Addr 0F: Length of manufacturer string
3607 manufacturer_size = buf[0x0F]/2;
3608 if (eeprom->manufacturer)
3609 free(eeprom->manufacturer);
3610 if (manufacturer_size > 0)
3611 {
3612 eeprom->manufacturer = (char *)malloc(manufacturer_size);
3613 if (eeprom->manufacturer)
3614 {
3615 // Decode manufacturer
3616 i = buf[0x0E] & (eeprom_size -1); // offset
3617 for (j=0; j<manufacturer_size-1; j++)
3618 {
3619 eeprom->manufacturer[j] = buf[2*j+i+2];
3620 }
3621 eeprom->manufacturer[j] = '\0';
3622 }
3623 }
3624 else eeprom->manufacturer = NULL;
3625
3626 // Addr 10: Offset of the product string + 0x80, calculated later
3627 // Addr 11: Length of product string
3628 if (eeprom->product)
3629 free(eeprom->product);
3630 product_size = buf[0x11]/2;
3631 if (product_size > 0)
3632 {
3633 eeprom->product = (char *)malloc(product_size);
3634 if (eeprom->product)
3635 {
3636 // Decode product name
3637 i = buf[0x10] & (eeprom_size -1); // offset
3638 for (j=0; j<product_size-1; j++)
3639 {
3640 eeprom->product[j] = buf[2*j+i+2];
3641 }
3642 eeprom->product[j] = '\0';
3643 }
3644 }
3645 else eeprom->product = NULL;
3646
3647 // Addr 12: Offset of the serial string + 0x80, calculated later
3648 // Addr 13: Length of serial string
3649 if (eeprom->serial)
3650 free(eeprom->serial);
3651 serial_size = buf[0x13]/2;
3652 if (serial_size > 0)
3653 {
3654 eeprom->serial = (char *)malloc(serial_size);
3655 if (eeprom->serial)
3656 {
3657 // Decode serial
3658 i = buf[0x12] & (eeprom_size -1); // offset
3659 for (j=0; j<serial_size-1; j++)
3660 {
3661 eeprom->serial[j] = buf[2*j+i+2];
3662 }
3663 eeprom->serial[j] = '\0';
3664 }
3665 }
3666 else eeprom->serial = NULL;
3667
3668 // verify checksum
3669 checksum = 0xAAAA;
3670
3671 for (i = 0; i < eeprom_size/2-1; i++)
3672 {
3673 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3674 {
3675 /* FT230X has a user section in the MTP which is not part of the checksum */
3676 i = 0x40;
3677 }
3678 value = buf[i*2];
3679 value += buf[(i*2)+1] << 8;
3680
3681 checksum = value^checksum;
3682 checksum = (checksum << 1) | (checksum >> 15);
3683 }
3684
3685 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3686
3687 if (eeprom_checksum != checksum)
3688 {
3689 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3690 ftdi_error_return(-1,"EEPROM checksum error");
3691 }
3692
3693 eeprom->channel_a_type = 0;
3694 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3695 {
3696 eeprom->chip = -1;
3697 }
3698 else if (ftdi->type == TYPE_2232C)
3699 {
3700 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3701 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCP);
3702 eeprom->high_current_a = !!(buf[0x00] & HIGH_CURRENT_DRIVE);
3703 eeprom->channel_b_type = buf[0x01] & 0x7;
3704 eeprom->channel_b_driver = !!(buf[0x01] & DRIVER_VCP);
3705 eeprom->high_current_b = !!(buf[0x01] & HIGH_CURRENT_DRIVE);
3706 eeprom->chip = buf[0x14];
3707 }
3708 else if (ftdi->type == TYPE_R)
3709 {
3710 /* TYPE_R flags D2XX, not VCP as all others */
3711 eeprom->channel_a_driver = !(buf[0x00] & DRIVER_VCP); /* note: inverted flag, use a single NOT */
3712 eeprom->high_current = !!(buf[0x00] & HIGH_CURRENT_DRIVE_R);
3713 eeprom->external_oscillator = !!(buf[0x00] & 0x02);
3714 if ( (buf[0x01]&0x40) != 0x40)
3715 fprintf(stderr,
3716 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3717 " If this happened with the\n"
3718 " EEPROM programmed by FTDI tools, please report "
3719 "to libftdi@developer.intra2net.com\n");
3720
3721 eeprom->chip = buf[0x16];
3722 // Addr 0B: Invert data lines
3723 // Works only on FT232R, not FT245R, but no way to distinguish
3724 eeprom->invert = buf[0x0B]; /* note: not a bitflag */
3725 // Addr 14: CBUS function: CBUS0, CBUS1
3726 // Addr 15: CBUS function: CBUS2, CBUS3
3727 // Addr 16: CBUS function: CBUS5
3728 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3729 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3730 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3731 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3732 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3733 }
3734 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3735 {
3736 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCP);
3737 eeprom->channel_b_driver = !!(buf[0x01] & DRIVER_VCP);
3738
3739 if (ftdi->type == TYPE_2232H)
3740 {
3741 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3742 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3743 eeprom->suspend_dbus7 = !!(buf[0x01] & SUSPEND_DBUS7_BIT);
3744 }
3745 else
3746 {
3747 eeprom->channel_c_driver = !!((buf[0x00] >> 4) & DRIVER_VCP);
3748 eeprom->channel_d_driver = !!((buf[0x01] >> 4) & DRIVER_VCP);
3749 eeprom->channel_a_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 0));
3750 eeprom->channel_b_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 1));
3751 eeprom->channel_c_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 2));
3752 eeprom->channel_d_rs485enable = !!(buf[0x0b] & (CHANNEL_IS_RS485 << 3));
3753 }
3754
3755 eeprom->chip = buf[0x18];
3756 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3757 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3758 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3759 eeprom->group1_drive = (buf[0x0c] >> 4) & DRIVE_16MA; /* not a bitflag */
3760 eeprom->group1_schmitt = !!((buf[0x0c] >> 4) & IS_SCHMITT);
3761 eeprom->group1_slew = !!((buf[0x0c] >> 4) & SLOW_SLEW);
3762 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA; /* not a bitflag */
3763 eeprom->group2_schmitt = !!(buf[0x0d] & IS_SCHMITT);
3764 eeprom->group2_slew = !!(buf[0x0d] & SLOW_SLEW);
3765 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA; /* not a bitflag */
3766 eeprom->group3_schmitt = !!((buf[0x0d] >> 4) & IS_SCHMITT);
3767 eeprom->group3_slew = !!((buf[0x0d] >> 4) & SLOW_SLEW);
3768 }
3769 else if (ftdi->type == TYPE_232H)
3770 {
3771 eeprom->channel_a_type = buf[0x00] & 0xf;
3772 eeprom->channel_a_driver = !!(buf[0x00] & DRIVER_VCPH);
3773 eeprom->clock_polarity = !!(buf[0x01] & FT1284_CLK_IDLE_STATE);
3774 eeprom->data_order = !!(buf[0x01] & FT1284_DATA_LSB);
3775 eeprom->flow_control = !!(buf[0x01] & FT1284_FLOW_CONTROL);
3776 eeprom->powersave = !!(buf[0x01] & POWER_SAVE_DISABLE_H);
3777 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3778 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3779 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3780 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA; /* not a bitflag */
3781 eeprom->group1_schmitt = !!(buf[0x0d] & IS_SCHMITT);
3782 eeprom->group1_slew = !!(buf[0x0d] & SLOW_SLEW);
3783
3784 for(i=0; i<5; i++)
3785 {
3786 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3787 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3788 }
3789 eeprom->chip = buf[0x1e];
3790 /*FIXME: Decipher more values*/
3791 }
3792 else if (ftdi->type == TYPE_230X)
3793 {
3794 for(i=0; i<4; i++)
3795 {
3796 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3797 }
3798 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA; /* not a bitflag */
3799 eeprom->group0_schmitt = !!(buf[0x0c] & IS_SCHMITT);
3800 eeprom->group0_slew = !!(buf[0x0c] & SLOW_SLEW);
3801 eeprom->group1_drive = (buf[0x0c] >> 4) & DRIVE_16MA; /* not a bitflag */
3802 eeprom->group1_schmitt = !!((buf[0x0c] >> 4) & IS_SCHMITT);
3803 eeprom->group1_slew = !!((buf[0x0c] >> 4) & SLOW_SLEW);
3804
3805 eeprom->invert = buf[0xb]; /* not a bitflag */
3806 }
3807
3808 if (verbose)
3809 {
3810 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3811 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3812 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3813 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3814
3815 if (eeprom->self_powered)
3816 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3817 else
3818 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3819 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3820 if (eeprom->manufacturer)
3821 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3822 if (eeprom->product)
3823 fprintf(stdout, "Product: %s\n",eeprom->product);
3824 if (eeprom->serial)
3825 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3826 fprintf(stdout, "Checksum : %04x\n", checksum);
3827 if (ftdi->type == TYPE_R) {
3828 fprintf(stdout, "Internal EEPROM\n");
3829 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3830 }
3831 else if (eeprom->chip >= 0x46)
3832 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3833 if (eeprom->suspend_dbus7)
3834 fprintf(stdout, "Suspend on DBUS7\n");
3835 if (eeprom->suspend_pull_downs)
3836 fprintf(stdout, "Pull IO pins low during suspend\n");
3837 if(eeprom->powersave)
3838 {
3839 if(ftdi->type >= TYPE_232H)
3840 fprintf(stdout,"Enter low power state on ACBUS7\n");
3841 }
3842 if (eeprom->remote_wakeup)
3843 fprintf(stdout, "Enable Remote Wake Up\n");
3844 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3845 if (ftdi->type >= TYPE_2232C)
3846 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3847 channel_mode[eeprom->channel_a_type],
3848 (eeprom->channel_a_driver)?" VCP":"",
3849 (eeprom->high_current_a)?" High Current IO":"");
3850 if (ftdi->type == TYPE_232H)
3851 {
3852 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3853 (eeprom->clock_polarity)?"HIGH":"LOW",
3854 (eeprom->data_order)?"LSB":"MSB",
3855 (eeprom->flow_control)?"":"No ");
3856 }
3857 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3858 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3859 channel_mode[eeprom->channel_b_type],
3860 (eeprom->channel_b_driver)?" VCP":"",
3861 (eeprom->high_current_b)?" High Current IO":"");
3862 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3863 eeprom->use_usb_version)
3864 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3865
3866 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3867 {
3868 fprintf(stdout,"%s has %d mA drive%s%s\n",
3869 (ftdi->type == TYPE_2232H)?"AL":"A",
3870 (eeprom->group0_drive+1) *4,
3871 (eeprom->group0_schmitt)?" Schmitt Input":"",
3872 (eeprom->group0_slew)?" Slow Slew":"");
3873 fprintf(stdout,"%s has %d mA drive%s%s\n",
3874 (ftdi->type == TYPE_2232H)?"AH":"B",
3875 (eeprom->group1_drive+1) *4,
3876 (eeprom->group1_schmitt)?" Schmitt Input":"",
3877 (eeprom->group1_slew)?" Slow Slew":"");
3878 fprintf(stdout,"%s has %d mA drive%s%s\n",
3879 (ftdi->type == TYPE_2232H)?"BL":"C",
3880 (eeprom->group2_drive+1) *4,
3881 (eeprom->group2_schmitt)?" Schmitt Input":"",
3882 (eeprom->group2_slew)?" Slow Slew":"");
3883 fprintf(stdout,"%s has %d mA drive%s%s\n",
3884 (ftdi->type == TYPE_2232H)?"BH":"D",
3885 (eeprom->group3_drive+1) *4,
3886 (eeprom->group3_schmitt)?" Schmitt Input":"",
3887 (eeprom->group3_slew)?" Slow Slew":"");
3888 }
3889 else if (ftdi->type == TYPE_232H)
3890 {
3891 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3892 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3893 "CLK30","CLK15","CLK7_5"
3894 };
3895 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3896 (eeprom->group0_drive+1) *4,
3897 (eeprom->group0_schmitt)?" Schmitt Input":"",
3898 (eeprom->group0_slew)?" Slow Slew":"");
3899 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3900 (eeprom->group1_drive+1) *4,
3901 (eeprom->group1_schmitt)?" Schmitt Input":"",
3902 (eeprom->group1_slew)?" Slow Slew":"");
3903 for (i=0; i<10; i++)
3904 {
3905 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3906 fprintf(stdout,"C%d Function: %s\n", i,
3907 cbush_mux[eeprom->cbus_function[i]]);
3908 }
3909 }
3910 else if (ftdi->type == TYPE_230X)
3911 {
3912 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3913 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3914 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3915 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3916 "BBRD#", "TIME_STAMP", "AWAKE#",
3917 };
3918 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3919 (eeprom->group0_drive+1) *4,
3920 (eeprom->group0_schmitt)?" Schmitt Input":"",
3921 (eeprom->group0_slew)?" Slow Slew":"");
3922 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3923 (eeprom->group1_drive+1) *4,
3924 (eeprom->group1_schmitt)?" Schmitt Input":"",
3925 (eeprom->group1_slew)?" Slow Slew":"");
3926 for (i=0; i<4; i++)
3927 {
3928 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3929 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3930 }
3931
3932 if (eeprom->invert)
3933 print_inverted_bits(eeprom->invert);
3934 }
3935
3936 if (ftdi->type == TYPE_R)
3937 {
3938 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3939 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3940 "IOMODE","BB_WR","BB_RD"
3941 };
3942 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3943
3944 if (eeprom->invert)
3945 print_inverted_bits(eeprom->invert);
3946
3947 for (i=0; i<5; i++)
3948 {
3949 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3950 fprintf(stdout,"C%d Function: %s\n", i,
3951 cbus_mux[eeprom->cbus_function[i]]);
3952 else
3953 {
3954 if (i < 4)
3955 /* Running MPROG show that C0..3 have fixed function Synchronous
3956 Bit Bang mode */
3957 fprintf(stdout,"C%d BB Function: %s\n", i,
3958 cbus_BB[i]);
3959 else
3960 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3961 }
3962 }
3963 }
3964 }
3965 return 0;
3966}
3967
3978int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3979{
3980 switch (value_name)
3981 {
3982 case VENDOR_ID:
3983 *value = ftdi->eeprom->vendor_id;
3984 break;
3985 case PRODUCT_ID:
3986 *value = ftdi->eeprom->product_id;
3987 break;
3988 case RELEASE_NUMBER:
3989 *value = ftdi->eeprom->release_number;
3990 break;
3991 case SELF_POWERED:
3992 *value = ftdi->eeprom->self_powered;
3993 break;
3994 case REMOTE_WAKEUP:
3995 *value = ftdi->eeprom->remote_wakeup;
3996 break;
3997 case IS_NOT_PNP:
3998 *value = ftdi->eeprom->is_not_pnp;
3999 break;
4000 case SUSPEND_DBUS7:
4001 *value = ftdi->eeprom->suspend_dbus7;
4002 break;
4003 case IN_IS_ISOCHRONOUS:
4004 *value = ftdi->eeprom->in_is_isochronous;
4005 break;
4006 case OUT_IS_ISOCHRONOUS:
4007 *value = ftdi->eeprom->out_is_isochronous;
4008 break;
4009 case SUSPEND_PULL_DOWNS:
4010 *value = ftdi->eeprom->suspend_pull_downs;
4011 break;
4012 case USE_SERIAL:
4013 *value = ftdi->eeprom->use_serial;
4014 break;
4015 case USB_VERSION:
4016 *value = ftdi->eeprom->usb_version;
4017 break;
4018 case USE_USB_VERSION:
4019 *value = ftdi->eeprom->use_usb_version;
4020 break;
4021 case MAX_POWER:
4022 *value = ftdi->eeprom->max_power;
4023 break;
4024 case CHANNEL_A_TYPE:
4025 *value = ftdi->eeprom->channel_a_type;
4026 break;
4027 case CHANNEL_B_TYPE:
4028 *value = ftdi->eeprom->channel_b_type;
4029 break;
4030 case CHANNEL_A_DRIVER:
4031 *value = ftdi->eeprom->channel_a_driver;
4032 break;
4033 case CHANNEL_B_DRIVER:
4034 *value = ftdi->eeprom->channel_b_driver;
4035 break;
4036 case CHANNEL_C_DRIVER:
4037 *value = ftdi->eeprom->channel_c_driver;
4038 break;
4039 case CHANNEL_D_DRIVER:
4040 *value = ftdi->eeprom->channel_d_driver;
4041 break;
4042 case CHANNEL_A_RS485:
4043 *value = ftdi->eeprom->channel_a_rs485enable;
4044 break;
4045 case CHANNEL_B_RS485:
4046 *value = ftdi->eeprom->channel_b_rs485enable;
4047 break;
4048 case CHANNEL_C_RS485:
4049 *value = ftdi->eeprom->channel_c_rs485enable;
4050 break;
4051 case CHANNEL_D_RS485:
4052 *value = ftdi->eeprom->channel_d_rs485enable;
4053 break;
4054 case CBUS_FUNCTION_0:
4055 *value = ftdi->eeprom->cbus_function[0];
4056 break;
4057 case CBUS_FUNCTION_1:
4058 *value = ftdi->eeprom->cbus_function[1];
4059 break;
4060 case CBUS_FUNCTION_2:
4061 *value = ftdi->eeprom->cbus_function[2];
4062 break;
4063 case CBUS_FUNCTION_3:
4064 *value = ftdi->eeprom->cbus_function[3];
4065 break;
4066 case CBUS_FUNCTION_4:
4067 *value = ftdi->eeprom->cbus_function[4];
4068 break;
4069 case CBUS_FUNCTION_5:
4070 *value = ftdi->eeprom->cbus_function[5];
4071 break;
4072 case CBUS_FUNCTION_6:
4073 *value = ftdi->eeprom->cbus_function[6];
4074 break;
4075 case CBUS_FUNCTION_7:
4076 *value = ftdi->eeprom->cbus_function[7];
4077 break;
4078 case CBUS_FUNCTION_8:
4079 *value = ftdi->eeprom->cbus_function[8];
4080 break;
4081 case CBUS_FUNCTION_9:
4082 *value = ftdi->eeprom->cbus_function[9];
4083 break;
4084 case HIGH_CURRENT:
4085 *value = ftdi->eeprom->high_current;
4086 break;
4087 case HIGH_CURRENT_A:
4088 *value = ftdi->eeprom->high_current_a;
4089 break;
4090 case HIGH_CURRENT_B:
4091 *value = ftdi->eeprom->high_current_b;
4092 break;
4093 case INVERT:
4094 *value = ftdi->eeprom->invert;
4095 break;
4096 case GROUP0_DRIVE:
4097 *value = ftdi->eeprom->group0_drive;
4098 break;
4099 case GROUP0_SCHMITT:
4100 *value = ftdi->eeprom->group0_schmitt;
4101 break;
4102 case GROUP0_SLEW:
4103 *value = ftdi->eeprom->group0_slew;
4104 break;
4105 case GROUP1_DRIVE:
4106 *value = ftdi->eeprom->group1_drive;
4107 break;
4108 case GROUP1_SCHMITT:
4109 *value = ftdi->eeprom->group1_schmitt;
4110 break;
4111 case GROUP1_SLEW:
4112 *value = ftdi->eeprom->group1_slew;
4113 break;
4114 case GROUP2_DRIVE:
4115 *value = ftdi->eeprom->group2_drive;
4116 break;
4117 case GROUP2_SCHMITT:
4118 *value = ftdi->eeprom->group2_schmitt;
4119 break;
4120 case GROUP2_SLEW:
4121 *value = ftdi->eeprom->group2_slew;
4122 break;
4123 case GROUP3_DRIVE:
4124 *value = ftdi->eeprom->group3_drive;
4125 break;
4126 case GROUP3_SCHMITT:
4127 *value = ftdi->eeprom->group3_schmitt;
4128 break;
4129 case GROUP3_SLEW:
4130 *value = ftdi->eeprom->group3_slew;
4131 break;
4132 case POWER_SAVE:
4133 *value = ftdi->eeprom->powersave;
4134 break;
4135 case CLOCK_POLARITY:
4136 *value = ftdi->eeprom->clock_polarity;
4137 break;
4138 case DATA_ORDER:
4139 *value = ftdi->eeprom->data_order;
4140 break;
4141 case FLOW_CONTROL:
4142 *value = ftdi->eeprom->flow_control;
4143 break;
4144 case CHIP_TYPE:
4145 *value = ftdi->eeprom->chip;
4146 break;
4147 case CHIP_SIZE:
4148 *value = ftdi->eeprom->size;
4149 break;
4151 *value = ftdi->eeprom->external_oscillator;
4152 break;
4153 default:
4154 ftdi_error_return(-1, "Request for unknown EEPROM value");
4155 }
4156 return 0;
4157}
4158
4171int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
4172{
4173 switch (value_name)
4174 {
4175 case VENDOR_ID:
4176 ftdi->eeprom->vendor_id = value;
4177 break;
4178 case PRODUCT_ID:
4179 ftdi->eeprom->product_id = value;
4180 break;
4181 case RELEASE_NUMBER:
4182 ftdi->eeprom->release_number = value;
4183 break;
4184 case SELF_POWERED:
4185 ftdi->eeprom->self_powered = value;
4186 break;
4187 case REMOTE_WAKEUP:
4188 ftdi->eeprom->remote_wakeup = value;
4189 break;
4190 case IS_NOT_PNP:
4191 ftdi->eeprom->is_not_pnp = value;
4192 break;
4193 case SUSPEND_DBUS7:
4194 ftdi->eeprom->suspend_dbus7 = value;
4195 break;
4196 case IN_IS_ISOCHRONOUS:
4197 ftdi->eeprom->in_is_isochronous = value;
4198 break;
4199 case OUT_IS_ISOCHRONOUS:
4200 ftdi->eeprom->out_is_isochronous = value;
4201 break;
4202 case SUSPEND_PULL_DOWNS:
4203 ftdi->eeprom->suspend_pull_downs = value;
4204 break;
4205 case USE_SERIAL:
4206 ftdi->eeprom->use_serial = value;
4207 break;
4208 case USB_VERSION:
4209 ftdi->eeprom->usb_version = value;
4210 break;
4211 case USE_USB_VERSION:
4212 ftdi->eeprom->use_usb_version = value;
4213 break;
4214 case MAX_POWER:
4215 ftdi->eeprom->max_power = value;
4216 break;
4217 case CHANNEL_A_TYPE:
4218 ftdi->eeprom->channel_a_type = value;
4219 break;
4220 case CHANNEL_B_TYPE:
4221 ftdi->eeprom->channel_b_type = value;
4222 break;
4223 case CHANNEL_A_DRIVER:
4224 ftdi->eeprom->channel_a_driver = value;
4225 break;
4226 case CHANNEL_B_DRIVER:
4227 ftdi->eeprom->channel_b_driver = value;
4228 break;
4229 case CHANNEL_C_DRIVER:
4230 ftdi->eeprom->channel_c_driver = value;
4231 break;
4232 case CHANNEL_D_DRIVER:
4233 ftdi->eeprom->channel_d_driver = value;
4234 break;
4235 case CHANNEL_A_RS485:
4236 ftdi->eeprom->channel_a_rs485enable = value;
4237 break;
4238 case CHANNEL_B_RS485:
4239 ftdi->eeprom->channel_b_rs485enable = value;
4240 break;
4241 case CHANNEL_C_RS485:
4242 ftdi->eeprom->channel_c_rs485enable = value;
4243 break;
4244 case CHANNEL_D_RS485:
4245 ftdi->eeprom->channel_d_rs485enable = value;
4246 break;
4247 case CBUS_FUNCTION_0:
4248 ftdi->eeprom->cbus_function[0] = value;
4249 break;
4250 case CBUS_FUNCTION_1:
4251 ftdi->eeprom->cbus_function[1] = value;
4252 break;
4253 case CBUS_FUNCTION_2:
4254 ftdi->eeprom->cbus_function[2] = value;
4255 break;
4256 case CBUS_FUNCTION_3:
4257 ftdi->eeprom->cbus_function[3] = value;
4258 break;
4259 case CBUS_FUNCTION_4:
4260 ftdi->eeprom->cbus_function[4] = value;
4261 break;
4262 case CBUS_FUNCTION_5:
4263 ftdi->eeprom->cbus_function[5] = value;
4264 break;
4265 case CBUS_FUNCTION_6:
4266 ftdi->eeprom->cbus_function[6] = value;
4267 break;
4268 case CBUS_FUNCTION_7:
4269 ftdi->eeprom->cbus_function[7] = value;
4270 break;
4271 case CBUS_FUNCTION_8:
4272 ftdi->eeprom->cbus_function[8] = value;
4273 break;
4274 case CBUS_FUNCTION_9:
4275 ftdi->eeprom->cbus_function[9] = value;
4276 break;
4277 case HIGH_CURRENT:
4278 ftdi->eeprom->high_current = value;
4279 break;
4280 case HIGH_CURRENT_A:
4281 ftdi->eeprom->high_current_a = value;
4282 break;
4283 case HIGH_CURRENT_B:
4284 ftdi->eeprom->high_current_b = value;
4285 break;
4286 case INVERT:
4287 ftdi->eeprom->invert = value;
4288 break;
4289 case GROUP0_DRIVE:
4290 ftdi->eeprom->group0_drive = value;
4291 break;
4292 case GROUP0_SCHMITT:
4293 ftdi->eeprom->group0_schmitt = value;
4294 break;
4295 case GROUP0_SLEW:
4296 ftdi->eeprom->group0_slew = value;
4297 break;
4298 case GROUP1_DRIVE:
4299 ftdi->eeprom->group1_drive = value;
4300 break;
4301 case GROUP1_SCHMITT:
4302 ftdi->eeprom->group1_schmitt = value;
4303 break;
4304 case GROUP1_SLEW:
4305 ftdi->eeprom->group1_slew = value;
4306 break;
4307 case GROUP2_DRIVE:
4308 ftdi->eeprom->group2_drive = value;
4309 break;
4310 case GROUP2_SCHMITT:
4311 ftdi->eeprom->group2_schmitt = value;
4312 break;
4313 case GROUP2_SLEW:
4314 ftdi->eeprom->group2_slew = value;
4315 break;
4316 case GROUP3_DRIVE:
4317 ftdi->eeprom->group3_drive = value;
4318 break;
4319 case GROUP3_SCHMITT:
4320 ftdi->eeprom->group3_schmitt = value;
4321 break;
4322 case GROUP3_SLEW:
4323 ftdi->eeprom->group3_slew = value;
4324 break;
4325 case CHIP_TYPE:
4326 ftdi->eeprom->chip = value;
4327 break;
4328 case POWER_SAVE:
4329 ftdi->eeprom->powersave = value;
4330 break;
4331 case CLOCK_POLARITY:
4332 ftdi->eeprom->clock_polarity = value;
4333 break;
4334 case DATA_ORDER:
4335 ftdi->eeprom->data_order = value;
4336 break;
4337 case FLOW_CONTROL:
4338 ftdi->eeprom->flow_control = value;
4339 break;
4340 case CHIP_SIZE:
4341 ftdi_error_return(-2, "EEPROM Value can't be changed");
4342 break;
4344 ftdi->eeprom->external_oscillator = value;
4345 break;
4346 case USER_DATA_ADDR:
4347 ftdi->eeprom->user_data_addr = value;
4348 break;
4349
4350 default :
4351 ftdi_error_return(-1, "Request to unknown EEPROM value");
4352 }
4354 return 0;
4355}
4356
4367int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4368{
4369 if (!ftdi || !(ftdi->eeprom))
4370 ftdi_error_return(-1, "No appropriate structure");
4371
4372 if (!buf || size < ftdi->eeprom->size)
4373 ftdi_error_return(-1, "Not enough room to store eeprom");
4374
4375 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4378
4379 memcpy(buf, ftdi->eeprom->buf, size);
4380
4381 return 0;
4382}
4383
4393int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4394{
4395 if (!ftdi || !(ftdi->eeprom) || !buf)
4396 ftdi_error_return(-1, "No appropriate structure");
4397
4398 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4401
4402 memcpy(ftdi->eeprom->buf, buf, size);
4403
4404 return 0;
4405}
4406
4416int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4417{
4418 if (!ftdi || !(ftdi->eeprom) || !buf)
4419 ftdi_error_return(-1, "No appropriate structure");
4420
4421 ftdi->eeprom->user_data_size = size;
4422 ftdi->eeprom->user_data = buf;
4423 return 0;
4424}
4425
4437int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4438{
4439 unsigned char buf[2];
4440
4441 if (ftdi == NULL || ftdi->usb_dev == NULL)
4442 ftdi_error_return(-2, "USB device unavailable");
4443
4444 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, buf, 2, ftdi->usb_read_timeout) != 2)
4445 ftdi_error_return(-1, "reading eeprom failed");
4446
4447 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
4448
4449 return 0;
4450}
4451
4462{
4463 int i;
4464 unsigned char *buf;
4465
4466 if (ftdi == NULL || ftdi->usb_dev == NULL)
4467 ftdi_error_return(-2, "USB device unavailable");
4468 buf = ftdi->eeprom->buf;
4469
4470 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4471 {
4472 if (libusb_control_transfer(
4474 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4475 ftdi_error_return(-1, "reading eeprom failed");
4476 }
4477
4478 if (ftdi->type == TYPE_R)
4479 ftdi->eeprom->size = 0x80;
4480 /* Guesses size of eeprom by comparing halves
4481 - will not work with blank eeprom */
4482 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4483 ftdi->eeprom->size = -1;
4484 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4485 ftdi->eeprom->size = 0x80;
4486 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4487 ftdi->eeprom->size = 0x40;
4488 else
4489 ftdi->eeprom->size = 0x100;
4490 return 0;
4491}
4492
4493/*
4494 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4495 Function is only used internally
4496 \internal
4497*/
4498static unsigned char ftdi_read_chipid_shift(unsigned char value)
4499{
4500 return ((value & 1) << 1) |
4501 ((value & 2) << 5) |
4502 ((value & 4) >> 2) |
4503 ((value & 8) << 4) |
4504 ((value & 16) >> 1) |
4505 ((value & 32) >> 1) |
4506 ((value & 64) >> 4) |
4507 ((value & 128) >> 2);
4508}
4509
4520int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4521{
4522 unsigned int a = 0, b = 0;
4523
4524 if (ftdi == NULL || ftdi->usb_dev == NULL)
4525 ftdi_error_return(-2, "USB device unavailable");
4526
4527 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)
4528 {
4529 a = a << 8 | a >> 8;
4530 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)
4531 {
4532 b = b << 8 | b >> 8;
4533 a = (a << 16) | (b & 0xFFFF);
4534 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4535 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4536 *chipid = a ^ 0xa5f0f7d1;
4537 return 0;
4538 }
4539 }
4540
4541 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4542}
4543
4558int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4559 unsigned short eeprom_val)
4560{
4561 int chip_type_location;
4562 unsigned short chip_type;
4563
4564 if (ftdi == NULL || ftdi->usb_dev == NULL)
4565 ftdi_error_return(-2, "USB device unavailable");
4566
4567 if (eeprom_addr <0x80)
4568 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4569
4570
4571 switch (ftdi->type)
4572 {
4573 case TYPE_BM:
4574 case TYPE_2232C:
4575 chip_type_location = 0x14;
4576 break;
4577 case TYPE_2232H:
4578 case TYPE_4232H:
4579 chip_type_location = 0x18;
4580 break;
4581 case TYPE_232H:
4582 chip_type_location = 0x1e;
4583 break;
4584 default:
4585 ftdi_error_return(-4, "Device can't access unprotected area");
4586 }
4587
4588 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4589 ftdi_error_return(-5, "Reading failed");
4590 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4591 if ((chip_type & 0xff) != 0x66)
4592 {
4593 ftdi_error_return(-6, "EEPROM is not of 93x66");
4594 }
4595
4596 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4597 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4598 NULL, 0, ftdi->usb_write_timeout) != 0)
4599 ftdi_error_return(-1, "unable to write eeprom");
4600
4601 return 0;
4602}
4603
4615{
4616 unsigned short usb_val, status;
4617 int i, ret;
4618 unsigned char *eeprom;
4619
4620 if (ftdi == NULL || ftdi->usb_dev == NULL)
4621 ftdi_error_return(-2, "USB device unavailable");
4622
4624 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4625
4626 eeprom = ftdi->eeprom->buf;
4627
4628 /* These commands were traced while running MProg */
4629 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4630 return ret;
4631 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4632 return ret;
4633 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4634 return ret;
4635
4636 for (i = 0; i < ftdi->eeprom->size/2; i++)
4637 {
4638 /* Do not try to write to reserved area */
4639 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4640 {
4641 i = 0x50;
4642 }
4643 usb_val = eeprom[i*2];
4644 usb_val += eeprom[(i*2)+1] << 8;
4645 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4646 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4647 NULL, 0, ftdi->usb_write_timeout) < 0)
4648 ftdi_error_return(-1, "unable to write eeprom");
4649 }
4650
4651 return 0;
4652}
4653
4668#define MAGIC 0x55aa
4670{
4671 unsigned short eeprom_value;
4672 if (ftdi == NULL || ftdi->usb_dev == NULL)
4673 ftdi_error_return(-2, "USB device unavailable");
4674
4675 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4676 {
4677 ftdi->eeprom->chip = 0;
4678 return 0;
4679 }
4680
4681 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4682 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4683 ftdi_error_return(-1, "unable to erase eeprom");
4684
4685
4686 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4687 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4688 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4689 Chip is 93x66 if magic is only read at word position 0xc0*/
4690 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4692 NULL, 0, ftdi->usb_write_timeout) != 0)
4693 ftdi_error_return(-3, "Writing magic failed");
4694 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4695 ftdi_error_return(-4, "Reading failed");
4696 if (eeprom_value == MAGIC)
4697 {
4698 ftdi->eeprom->chip = 0x46;
4699 }
4700 else
4701 {
4702 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4703 ftdi_error_return(-4, "Reading failed");
4704 if (eeprom_value == MAGIC)
4705 ftdi->eeprom->chip = 0x56;
4706 else
4707 {
4708 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4709 ftdi_error_return(-4, "Reading failed");
4710 if (eeprom_value == MAGIC)
4711 ftdi->eeprom->chip = 0x66;
4712 else
4713 {
4714 ftdi->eeprom->chip = -1;
4715 }
4716 }
4717 }
4718 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4719 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4720 ftdi_error_return(-1, "unable to erase eeprom");
4721 return 0;
4722}
4723
4731const char *ftdi_get_error_string (struct ftdi_context *ftdi)
4732{
4733 if (ftdi == NULL)
4734 return "";
4735
4736 return ftdi->error_str;
4737}
4738
4739/* @} end of doxygen libftdi group */
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:1583
int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
Definition: ftdi.c:1494
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
Definition: ftdi.c:589
int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
Definition: ftdi.c:2579
struct ftdi_transfer_control * ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1805
int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev, char *manufacturer, int mnf_len, char *description, int desc_len, char *serial, int serial_len)
Definition: ftdi.c:413
int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char *buf, int size)
Definition: ftdi.c:4416
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:1113
void ftdi_set_usbdev(struct ftdi_context *ftdi, libusb_device_handle *usb)
Definition: ftdi.c:272
int ftdi_init(struct ftdi_context *ftdi)
Definition: ftdi.c:91
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:1060
int ftdi_erase_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4669
int ftdi_usb_reset(struct ftdi_context *ftdi)
Definition: ftdi.c:1003
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
Definition: ftdi.c:314
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
Definition: ftdi.c:1168
void ftdi_list_free(struct ftdi_device_list **devlist)
Definition: ftdi.c:362
int ftdi_tciflush(struct ftdi_context *ftdi)
Definition: ftdi.c:1030
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
Definition: ftdi.c:2347
int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial, unsigned int index)
Definition: ftdi.c:764
void ftdi_free(struct ftdi_context *ftdi)
Definition: ftdi.c:260
int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
Definition: ftdi.c:2266
int ftdi_disable_bitbang(struct ftdi_context *ftdi)
Definition: ftdi.c:2217
int ftdi_setdtr(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2423
int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type)
Definition: ftdi.c:1513
void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char *output)
Definition: ftdi.c:2832
#define ftdi_error_return_free_device_list(code, str, devs)
Definition: ftdi.c:52
int ftdi_setrts(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2453
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:1983
int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
Definition: ftdi.c:2484
int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
Definition: ftdi.c:4558
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
Definition: ftdi.c:3978
int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, const char *manufacturer, const char *product, const char *serial)
Definition: ftdi.c:2727
int ftdi_read_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
Definition: ftdi.c:4437
int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:2133
struct ftdi_transfer_control * ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1743
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
Definition: ftdi.c:161
int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable)
Definition: ftdi.c:2520
void ftdi_deinit(struct ftdi_context *ftdi)
Definition: ftdi.c:214
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:2007
int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
Definition: ftdi.c:2191
const char * ftdi_get_error_string(struct ftdi_context *ftdi)
Definition: ftdi.c:4731
int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:1965
#define ftdi_error_return(code, str)
Definition: ftdi.c:44
int ftdi_write_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4614
int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
Definition: ftdi.c:4171
#define H_CLK
int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi, unsigned short *value, unsigned short *index)
Wrapper function to export ftdi_convert_baudrate() to the unit test Do not use, it's only for the uni...
Definition: ftdi.c:1431
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:4367
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial)
Definition: ftdi.c:733
#define MAGIC
Definition: ftdi.c:4668
int ftdi_usb_close(struct ftdi_context *ftdi)
Definition: ftdi.c:1197
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
Definition: ftdi.c:707
void ftdi_list_free2(struct ftdi_device_list *devlist)
Definition: ftdi.c:382
int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
Definition: ftdi.c:857
int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:4393
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char *description)
Definition: ftdi.c:910
#define C_CLK
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:2170
struct ftdi_context * ftdi_new(void)
Definition: ftdi.c:132
int ftdi_tcoflush(struct ftdi_context *ftdi)
Definition: ftdi.c:1087
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
Definition: ftdi.c:2240
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
Definition: ftdi.c:4520
int ftdi_read_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4461
int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
Definition: ftdi.c:2375
int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
Definition: ftdi.c:3548
void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, struct timeval *to)
Definition: ftdi.c:1931
int ftdi_eeprom_build(struct ftdi_context *ftdi)
Definition: ftdi.c:2910
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
Definition: ftdi.c:2293
int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev, char *manufacturer, int mnf_len, char *description, int desc_len, char *serial, int serial_len)
Definition: ftdi.c:470
int ftdi_setflowctrl_xonxoff(struct ftdi_context *ftdi, unsigned char xon, unsigned char xoff)
Definition: ftdi.c:2399
int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
Definition: ftdi.c:1448
int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable)
Definition: ftdi.c:2549
int ftdi_tcioflush(struct ftdi_context *ftdi)
Definition: ftdi.c:1137
int ftdi_eeprom_get_strings(struct ftdi_context *ftdi, char *manufacturer, int mnf_len, char *product, int prod_len, char *serial, int serial_len)
Definition: ftdi.c:2793
struct ftdi_version_info ftdi_get_library_version(void)
Get libftdi library version.
Definition: ftdi.c:285
int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
Definition: ftdi.c:1884
ftdi_chip_type
Definition: ftdi.h:47
@ TYPE_2232H
Definition: ftdi.h:52
@ TYPE_BM
Definition: ftdi.h:49
@ TYPE_R
Definition: ftdi.h:51
@ TYPE_2232C
Definition: ftdi.h:50
@ TYPE_230X
Definition: ftdi.h:55
@ TYPE_4232H
Definition: ftdi.h:53
@ TYPE_AM
Definition: ftdi.h:48
@ TYPE_232H
Definition: ftdi.h:54
#define HIGH_CURRENT_DRIVE
Definition: ftdi.h:472
#define SIO_SET_DTR_LOW
Definition: ftdi.h:232
#define SLOW_SLEW
Definition: ftdi.h:460
#define USE_SERIAL_NUM
Definition: ftdi.h:406
#define FT1284_CLK_IDLE_STATE
Definition: ftdi.h:401
@ BITMODE_RESET
Definition: ftdi.h:69
#define SIO_SET_DATA_REQUEST
Definition: ftdi.h:169
#define SIO_RESET_PURGE_TX
Definition: ftdi.h:219
ftdi_stopbits_type
Definition: ftdi.h:60
@ STOP_BIT_1
Definition: ftdi.h:60
@ STOP_BIT_2
Definition: ftdi.h:60
@ STOP_BIT_15
Definition: ftdi.h:60
#define CHANNEL_IS_FT1284
Definition: ftdi.h:452
#define SIO_SET_EVENT_CHAR_REQUEST
Definition: ftdi.h:173
#define DRIVER_VCP
Definition: ftdi.h:464
#define SIO_RESET_REQUEST
Definition: ftdi.h:167
#define FT1284_DATA_LSB
Definition: ftdi.h:402
@ CBUSH_CLK7_5
Definition: ftdi.h:418
@ CBUSH_TRISTATE
Definition: ftdi.h:416
#define SIO_SET_ERROR_CHAR_REQUEST
Definition: ftdi.h:174
#define DRIVER_VCPH
Definition: ftdi.h:465
#define SIO_SET_MODEM_CTRL_REQUEST
Definition: ftdi.h:171
#define SIO_READ_PINS_REQUEST
Definition: ftdi.h:178
#define SIO_SET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:175
#define CHANNEL_IS_CPU
Definition: ftdi.h:451
#define POWER_SAVE_DISABLE_H
Definition: ftdi.h:404
ftdi_bits_type
Definition: ftdi.h:62
#define SIO_ERASE_EEPROM_REQUEST
Definition: ftdi.h:181
#define CHANNEL_IS_RS485
Definition: ftdi.h:454
#define FT1284_FLOW_CONTROL
Definition: ftdi.h:403
#define SIO_SET_BAUDRATE_REQUEST
Definition: ftdi.h:168
#define SIO_READ_EEPROM_REQUEST
Definition: ftdi.h:179
ftdi_interface
Definition: ftdi.h:83
@ INTERFACE_C
Definition: ftdi.h:87
@ INTERFACE_D
Definition: ftdi.h:88
@ INTERFACE_B
Definition: ftdi.h:86
@ INTERFACE_ANY
Definition: ftdi.h:84
@ INTERFACE_A
Definition: ftdi.h:85
#define SIO_TCIFLUSH
Definition: ftdi.h:222
#define CHANNEL_IS_OPTO
Definition: ftdi.h:450
#define SIO_POLL_MODEM_STATUS_REQUEST
Definition: ftdi.h:172
#define SIO_WRITE_EEPROM_REQUEST
Definition: ftdi.h:180
ftdi_parity_type
Definition: ftdi.h:58
@ EVEN
Definition: ftdi.h:58
@ ODD
Definition: ftdi.h:58
@ MARK
Definition: ftdi.h:58
@ SPACE
Definition: ftdi.h:58
@ NONE
Definition: ftdi.h:58
#define SIO_XON_XOFF_HS
Definition: ftdi.h:228
@ CBUSX_TXDEN
Definition: ftdi.h:424
@ CBUSX_AWAKE
Definition: ftdi.h:427
@ CBUSX_SLEEP
Definition: ftdi.h:424
@ CBUSX_RXLED
Definition: ftdi.h:423
@ CBUSX_TXLED
Definition: ftdi.h:423
#define SIO_SET_DTR_HIGH
Definition: ftdi.h:231
#define FTDI_DEVICE_IN_REQTYPE
Definition: ftdi.h:164
#define SIO_RESET_SIO
Definition: ftdi.h:184
#define SIO_SET_FLOW_CTRL_REQUEST
Definition: ftdi.h:170
ftdi_eeprom_value
Definition: ftdi.h:330
@ CHANNEL_B_RS485
Definition: ftdi.h:383
@ GROUP1_SCHMITT
Definition: ftdi.h:366
@ GROUP2_DRIVE
Definition: ftdi.h:368
@ CBUS_FUNCTION_6
Definition: ftdi.h:354
@ CBUS_FUNCTION_9
Definition: ftdi.h:357
@ IN_IS_ISOCHRONOUS
Definition: ftdi.h:337
@ SUSPEND_DBUS7
Definition: ftdi.h:336
@ GROUP2_SCHMITT
Definition: ftdi.h:369
@ FLOW_CONTROL
Definition: ftdi.h:379
@ CBUS_FUNCTION_8
Definition: ftdi.h:356
@ DATA_ORDER
Definition: ftdi.h:378
@ CHANNEL_D_DRIVER
Definition: ftdi.h:381
@ CHANNEL_B_DRIVER
Definition: ftdi.h:347
@ USE_USB_VERSION
Definition: ftdi.h:342
@ CHANNEL_C_RS485
Definition: ftdi.h:384
@ CBUS_FUNCTION_2
Definition: ftdi.h:350
@ CHIP_SIZE
Definition: ftdi.h:374
@ POWER_SAVE
Definition: ftdi.h:376
@ CHANNEL_C_DRIVER
Definition: ftdi.h:380
@ CLOCK_POLARITY
Definition: ftdi.h:377
@ USB_VERSION
Definition: ftdi.h:341
@ CBUS_FUNCTION_4
Definition: ftdi.h:352
@ GROUP0_SLEW
Definition: ftdi.h:364
@ REMOTE_WAKEUP
Definition: ftdi.h:334
@ SUSPEND_PULL_DOWNS
Definition: ftdi.h:339
@ PRODUCT_ID
Definition: ftdi.h:332
@ HIGH_CURRENT
Definition: ftdi.h:358
@ USE_SERIAL
Definition: ftdi.h:340
@ CHANNEL_D_RS485
Definition: ftdi.h:385
@ CHANNEL_B_TYPE
Definition: ftdi.h:345
@ CBUS_FUNCTION_1
Definition: ftdi.h:349
@ IS_NOT_PNP
Definition: ftdi.h:335
@ MAX_POWER
Definition: ftdi.h:343
@ CHANNEL_A_TYPE
Definition: ftdi.h:344
@ SELF_POWERED
Definition: ftdi.h:333
@ GROUP1_SLEW
Definition: ftdi.h:367
@ HIGH_CURRENT_A
Definition: ftdi.h:359
@ GROUP3_DRIVE
Definition: ftdi.h:371
@ CHANNEL_A_DRIVER
Definition: ftdi.h:346
@ HIGH_CURRENT_B
Definition: ftdi.h:360
@ VENDOR_ID
Definition: ftdi.h:331
@ GROUP0_SCHMITT
Definition: ftdi.h:363
@ CHIP_TYPE
Definition: ftdi.h:375
@ INVERT
Definition: ftdi.h:361
@ GROUP2_SLEW
Definition: ftdi.h:370
@ CBUS_FUNCTION_3
Definition: ftdi.h:351
@ RELEASE_NUMBER
Definition: ftdi.h:386
@ OUT_IS_ISOCHRONOUS
Definition: ftdi.h:338
@ CBUS_FUNCTION_7
Definition: ftdi.h:355
@ EXTERNAL_OSCILLATOR
Definition: ftdi.h:387
@ CBUS_FUNCTION_5
Definition: ftdi.h:353
@ CBUS_FUNCTION_0
Definition: ftdi.h:348
@ GROUP0_DRIVE
Definition: ftdi.h:362
@ GROUP3_SLEW
Definition: ftdi.h:373
@ USER_DATA_ADDR
Definition: ftdi.h:388
@ CHANNEL_A_RS485
Definition: ftdi.h:382
@ GROUP1_DRIVE
Definition: ftdi.h:365
@ GROUP3_SCHMITT
Definition: ftdi.h:372
#define SIO_SET_RTS_LOW
Definition: ftdi.h:235
@ CBUS_TXLED
Definition: ftdi.h:409
@ CBUS_PWREN
Definition: ftdi.h:409
@ CBUS_SLEEP
Definition: ftdi.h:410
@ CBUS_RXLED
Definition: ftdi.h:409
@ CBUS_TXDEN
Definition: ftdi.h:409
@ CBUS_BB_RD
Definition: ftdi.h:411
@ CBUS_CLK6
Definition: ftdi.h:410
#define SIO_GET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:176
#define SUSPEND_DBUS7_BIT
Definition: ftdi.h:469
@ AUTO_DETACH_SIO_MODULE
Definition: ftdi.h:94
@ AUTO_DETACH_REATACH_SIO_MODULE
Definition: ftdi.h:96
#define HIGH_CURRENT_DRIVE_R
Definition: ftdi.h:473
ftdi_break_type
Definition: ftdi.h:64
@ BREAK_OFF
Definition: ftdi.h:64
@ BREAK_ON
Definition: ftdi.h:64
#define CHANNEL_IS_UART
Definition: ftdi.h:448
#define SIO_RESET_PURGE_RX
Definition: ftdi.h:218
#define IS_SCHMITT
Definition: ftdi.h:461
#define USE_USB_VERSION_BIT
Definition: ftdi.h:467
#define DRIVE_16MA
Definition: ftdi.h:459
#define SIO_SET_RTS_HIGH
Definition: ftdi.h:234
#define SIO_TCOFLUSH
Definition: ftdi.h:223
#define CHANNEL_IS_FIFO
Definition: ftdi.h:449
#define SIO_SET_BITMODE_REQUEST
Definition: ftdi.h:177
#define FTDI_DEVICE_OUT_REQTYPE
Definition: ftdi.h:163
#define FTDI_MAX_EEPROM_SIZE
Definition: ftdi_i.h:23
#define MAX_POWER_MILLIAMP_PER_UNIT
Definition: ftdi_i.h:26
Main context structure for all libftdi functions.
Definition: ftdi.h:272
unsigned char bitbang_mode
Definition: ftdi.h:314
struct ftdi_eeprom * eeprom
Definition: ftdi.h:317
struct libusb_context * usb_ctx
Definition: ftdi.h:275
enum ftdi_module_detach_mode module_detach_mode
Definition: ftdi.h:323
int out_ep
Definition: ftdi.h:311
unsigned int writebuffer_chunksize
Definition: ftdi.h:299
unsigned char bitbang_enabled
Definition: ftdi.h:289
struct libusb_device_handle * usb_dev
Definition: ftdi.h:277
enum ftdi_chip_type type
Definition: ftdi.h:285
unsigned int max_packet_size
Definition: ftdi.h:301
unsigned char * readbuffer
Definition: ftdi.h:291
unsigned int readbuffer_chunksize
Definition: ftdi.h:297
const char * error_str
Definition: ftdi.h:320
int in_ep
Definition: ftdi.h:310
int index
Definition: ftdi.h:307
int baudrate
Definition: ftdi.h:287
unsigned int readbuffer_remaining
Definition: ftdi.h:295
int interface
Definition: ftdi.h:305
unsigned int readbuffer_offset
Definition: ftdi.h:293
int usb_write_timeout
Definition: ftdi.h:281
int usb_read_timeout
Definition: ftdi.h:279
list of usb devices created by ftdi_usb_find_all()
Definition: ftdi.h:395
struct ftdi_device_list * next
Definition: ftdi.h:397
struct libusb_device * dev
Definition: ftdi.h:399
FTDI eeprom structure.
Definition: ftdi_i.h:32
int user_data_addr
Definition: ftdi_i.h:130
int self_powered
Definition: ftdi_i.h:43
int user_data_size
Definition: ftdi_i.h:131
unsigned char buf[FTDI_MAX_EEPROM_SIZE]
Definition: ftdi_i.h:139
int channel_b_driver
Definition: ftdi_i.h:82
int group3_slew
Definition: ftdi_i.h:121
int external_oscillator
Definition: ftdi_i.h:103
int channel_c_rs485enable
Definition: ftdi_i.h:88
int channel_b_type
Definition: ftdi_i.h:79
int is_not_pnp
Definition: ftdi_i.h:47
int group0_schmitt
Definition: ftdi_i.h:111
int invert
Definition: ftdi_i.h:101
int high_current_a
Definition: ftdi_i.h:97
int high_current_b
Definition: ftdi_i.h:99
int in_is_isochronous
Definition: ftdi_i.h:53
int use_usb_version
Definition: ftdi_i.h:64
int clock_polarity
Definition: ftdi_i.h:125
int group2_drive
Definition: ftdi_i.h:116
int group1_drive
Definition: ftdi_i.h:113
int channel_a_driver
Definition: ftdi_i.h:81
const char * user_data
Definition: ftdi_i.h:132
int channel_d_rs485enable
Definition: ftdi_i.h:89
int size
Definition: ftdi_i.h:136
int chip
Definition: ftdi_i.h:138
int suspend_pull_downs
Definition: ftdi_i.h:57
int initialized_for_connected_device
Definition: ftdi_i.h:40
int group3_schmitt
Definition: ftdi_i.h:120
int usb_version
Definition: ftdi_i.h:62
int flow_control
Definition: ftdi_i.h:127
int data_order
Definition: ftdi_i.h:126
int max_power
Definition: ftdi_i.h:66
int group1_slew
Definition: ftdi_i.h:115
int remote_wakeup
Definition: ftdi_i.h:45
char * product
Definition: ftdi_i.h:71
int group2_schmitt
Definition: ftdi_i.h:117
char * serial
Definition: ftdi_i.h:73
int release_number
Definition: ftdi_i.h:142
int product_id
Definition: ftdi_i.h:36
int cbus_function[10]
Definition: ftdi_i.h:93
int group2_slew
Definition: ftdi_i.h:118
int group0_slew
Definition: ftdi_i.h:112
int out_is_isochronous
Definition: ftdi_i.h:55
int group0_drive
Definition: ftdi_i.h:110
int use_serial
Definition: ftdi_i.h:60
int group1_schmitt
Definition: ftdi_i.h:114
int channel_a_rs485enable
Definition: ftdi_i.h:86
char * manufacturer
Definition: ftdi_i.h:69
int powersave
Definition: ftdi_i.h:123
int high_current
Definition: ftdi_i.h:95
int channel_c_driver
Definition: ftdi_i.h:83
int channel_a_type
Definition: ftdi_i.h:78
int group3_drive
Definition: ftdi_i.h:119
int channel_d_driver
Definition: ftdi_i.h:84
int channel_b_rs485enable
Definition: ftdi_i.h:87
int vendor_id
Definition: ftdi_i.h:34
int suspend_dbus7
Definition: ftdi_i.h:50
struct libusb_transfer * transfer
Definition: ftdi.h:263
unsigned char * buf
Definition: ftdi.h:259
struct ftdi_context * ftdi
Definition: ftdi.h:262
const char * snapshot_str
Definition: ftdi.h:511
const char * version_str
Definition: ftdi.h:510