libftdi1 1.3

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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-2014 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
14 * *
15 ***************************************************************************/
16
29 /* @{ */
30
31 #include <libusb.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include "ftdi_i.h"
38 #include "ftdi.h"
39 #include "ftdi_version_i.h"
40
41 #define ftdi_error_return(code, str) do { \
42 if ( ftdi ) \
43 ftdi->error_str = str; \
44 else \
45 fprintf(stderr, str); \
46 return code; \
47 } while(0);
48
49 #define ftdi_error_return_free_device_list(code, str, devs) do { \
50 libusb_free_device_list(devs,1); \
51 ftdi->error_str = str; \
52 return code; \
53 } while(0);
54
55
65 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
66 {
67 if (ftdi && ftdi->usb_dev)
68 {
69 libusb_close (ftdi->usb_dev);
70 ftdi->usb_dev = NULL;
71 if(ftdi->eeprom)
72 ftdi->eeprom->initialized_for_connected_device = 0;
73 }
74 }
75
88 int ftdi_init(struct ftdi_context *ftdi)
89 {
90 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
91 ftdi->usb_ctx = NULL;
92 ftdi->usb_dev = NULL;
93 ftdi->usb_read_timeout = 5000;
94 ftdi->usb_write_timeout = 5000;
95
96 ftdi->type = TYPE_BM; /* chip type */
97 ftdi->baudrate = -1;
98 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
99
100 ftdi->readbuffer = NULL;
101 ftdi->readbuffer_offset = 0;
102 ftdi->readbuffer_remaining = 0;
103 ftdi->writebuffer_chunksize = 4096;
104 ftdi->max_packet_size = 0;
105 ftdi->error_str = NULL;
106 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
107
108 if (libusb_init(&ftdi->usb_ctx) < 0)
109 ftdi_error_return(-3, "libusb_init() failed");
110
111 ftdi_set_interface(ftdi, INTERFACE_ANY);
112 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
113
114 if (eeprom == 0)
115 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
116 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
117 ftdi->eeprom = eeprom;
118
119 /* All fine. Now allocate the readbuffer */
120 return ftdi_read_data_set_chunksize(ftdi, 4096);
121 }
122
128 struct ftdi_context *ftdi_new(void)
129 {
130 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
131
132 if (ftdi == NULL)
133 {
134 return NULL;
135 }
136
137 if (ftdi_init(ftdi) != 0)
138 {
139 free(ftdi);
140 return NULL;
141 }
142
143 return ftdi;
144 }
145
157 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
158 {
159 if (ftdi == NULL)
160 ftdi_error_return(-2, "USB device unavailable");
161
162 if (ftdi->usb_dev != NULL)
163 {
164 int check_interface = interface;
165 if (check_interface == INTERFACE_ANY)
166 check_interface = INTERFACE_A;
167
168 if (ftdi->index != check_interface)
169 ftdi_error_return(-3, "Interface can not be changed on an already open device");
170 }
171
172 switch (interface)
173 {
174 case INTERFACE_ANY:
175 case INTERFACE_A:
176 ftdi->interface = 0;
177 ftdi->index = INTERFACE_A;
178 ftdi->in_ep = 0x02;
179 ftdi->out_ep = 0x81;
180 break;
181 case INTERFACE_B:
182 ftdi->interface = 1;
183 ftdi->index = INTERFACE_B;
184 ftdi->in_ep = 0x04;
185 ftdi->out_ep = 0x83;
186 break;
187 case INTERFACE_C:
188 ftdi->interface = 2;
189 ftdi->index = INTERFACE_C;
190 ftdi->in_ep = 0x06;
191 ftdi->out_ep = 0x85;
192 break;
193 case INTERFACE_D:
194 ftdi->interface = 3;
195 ftdi->index = INTERFACE_D;
196 ftdi->in_ep = 0x08;
197 ftdi->out_ep = 0x87;
198 break;
199 default:
200 ftdi_error_return(-1, "Unknown interface");
201 }
202 return 0;
203 }
204
210 void ftdi_deinit(struct ftdi_context *ftdi)
211 {
212 if (ftdi == NULL)
213 return;
214
215 ftdi_usb_close_internal (ftdi);
216
217 if (ftdi->readbuffer != NULL)
218 {
219 free(ftdi->readbuffer);
220 ftdi->readbuffer = NULL;
221 }
222
223 if (ftdi->eeprom != NULL)
224 {
225 if (ftdi->eeprom->manufacturer != 0)
226 {
227 free(ftdi->eeprom->manufacturer);
228 ftdi->eeprom->manufacturer = 0;
229 }
230 if (ftdi->eeprom->product != 0)
231 {
232 free(ftdi->eeprom->product);
233 ftdi->eeprom->product = 0;
234 }
235 if (ftdi->eeprom->serial != 0)
236 {
237 free(ftdi->eeprom->serial);
238 ftdi->eeprom->serial = 0;
239 }
240 free(ftdi->eeprom);
241 ftdi->eeprom = NULL;
242 }
243
244 if (ftdi->usb_ctx)
245 {
246 libusb_exit(ftdi->usb_ctx);
247 ftdi->usb_ctx = NULL;
248 }
249 }
250
256 void ftdi_free(struct ftdi_context *ftdi)
257 {
258 ftdi_deinit(ftdi);
259 free(ftdi);
260 }
261
268 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
269 {
270 if (ftdi == NULL)
271 return;
272
273 ftdi->usb_dev = usb;
274 }
275
281 struct ftdi_version_info ftdi_get_library_version(void)
282 {
283 struct ftdi_version_info ver;
284
285 ver.major = FTDI_MAJOR_VERSION;
286 ver.minor = FTDI_MINOR_VERSION;
287 ver.micro = FTDI_MICRO_VERSION;
288 ver.version_str = FTDI_VERSION_STRING;
289 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
290
291 return ver;
292 }
293
310 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
311 {
312 struct ftdi_device_list **curdev;
313 libusb_device *dev;
314 libusb_device **devs;
315 int count = 0;
316 int i = 0;
317
318 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
319 ftdi_error_return(-5, "libusb_get_device_list() failed");
320
321 curdev = devlist;
322 *curdev = NULL;
323
324 while ((dev = devs[i++]) != NULL)
325 {
326 struct libusb_device_descriptor desc;
327
328 if (libusb_get_device_descriptor(dev, &desc) < 0)
329 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
330
331 if (((vendor || product) &&
332 desc.idVendor == vendor && desc.idProduct == product) ||
333 (!(vendor || product) &&
334 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
335 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
336 || desc.idProduct == 0x6015)))
337 {
338 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
339 if (!*curdev)
340 ftdi_error_return_free_device_list(-3, "out of memory", devs);
341
342 (*curdev)->next = NULL;
343 (*curdev)->dev = dev;
344 libusb_ref_device(dev);
345 curdev = &(*curdev)->next;
346 count++;
347 }
348 }
349 libusb_free_device_list(devs,1);
350 return count;
351 }
352
358 void ftdi_list_free(struct ftdi_device_list **devlist)
359 {
360 struct ftdi_device_list *curdev, *next;
361
362 for (curdev = *devlist; curdev != NULL;)
363 {
364 next = curdev->next;
365 libusb_unref_device(curdev->dev);
366 free(curdev);
367 curdev = next;
368 }
369
370 *devlist = NULL;
371 }
372
378 void ftdi_list_free2(struct ftdi_device_list *devlist)
379 {
380 ftdi_list_free(&devlist);
381 }
382
409 int ftdi_usb_get_strings(struct ftdi_context *ftdi,
410 struct libusb_device *dev,
411 char *manufacturer, int mnf_len,
412 char *description, int desc_len,
413 char *serial, int serial_len)
414 {
415 int ret;
416
417 if ((ftdi==NULL) || (dev==NULL))
418 return -1;
419
420 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
421 ftdi_error_return(-4, "libusb_open() failed");
422
423 // ftdi->usb_dev will not be NULL when entering ftdi_usb_get_strings2(), so
424 // it won't be closed either. This allows us to close it whether we actually
425 // called libusb_open() up above or not. This matches the expected behavior
426 // (and note) for ftdi_usb_get_strings().
427 ret = ftdi_usb_get_strings2(ftdi, dev,
428 manufacturer, mnf_len,
429 description, desc_len,
430 serial, serial_len);
431
432 // only close it if it was successful, as all other return codes close
433 // before returning already.
434 if (ret == 0)
435 ftdi_usb_close_internal(ftdi);
436
437 return ret;
438 }
439
466 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
467 char *manufacturer, int mnf_len,
468 char *description, int desc_len,
469 char *serial, int serial_len)
470 {
471 struct libusb_device_descriptor desc;
472
473 if ((ftdi==NULL) || (dev==NULL))
474 return -1;
475
476 char need_open = (ftdi->usb_dev == NULL);
477 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
478 ftdi_error_return(-4, "libusb_open() failed");
479
480 if (libusb_get_device_descriptor(dev, &desc) < 0)
481 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
482
483 if (manufacturer != NULL)
484 {
485 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
486 {
487 ftdi_usb_close_internal (ftdi);
488 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
489 }
490 }
491
492 if (description != NULL)
493 {
494 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
495 {
496 ftdi_usb_close_internal (ftdi);
497 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
498 }
499 }
500
501 if (serial != NULL)
502 {
503 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
504 {
505 ftdi_usb_close_internal (ftdi);
506 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
507 }
508 }
509
510 if (need_open)
511 ftdi_usb_close_internal (ftdi);
512
513 return 0;
514 }
515
522 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
523 {
524 struct libusb_device_descriptor desc;
525 struct libusb_config_descriptor *config0;
526 unsigned int packet_size;
527
528 // Sanity check
529 if (ftdi == NULL || dev == NULL)
530 return 64;
531
532 // Determine maximum packet size. Init with default value.
533 // New hi-speed devices from FTDI use a packet size of 512 bytes
534 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
535 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
536 packet_size = 512;
537 else
538 packet_size = 64;
539
540 if (libusb_get_device_descriptor(dev, &desc) < 0)
541 return packet_size;
542
543 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
544 return packet_size;
545
546 if (desc.bNumConfigurations > 0)
547 {
548 if (ftdi->interface < config0->bNumInterfaces)
549 {
550 struct libusb_interface interface = config0->interface[ftdi->interface];
551 if (interface.num_altsetting > 0)
552 {
553 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
554 if (descriptor.bNumEndpoints > 0)
555 {
556 packet_size = descriptor.endpoint[0].wMaxPacketSize;
557 }
558 }
559 }
560 }
561
562 libusb_free_config_descriptor (config0);
563 return packet_size;
564 }
565
584 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
585 {
586 struct libusb_device_descriptor desc;
587 struct libusb_config_descriptor *config0;
588 int cfg, cfg0, detach_errno = 0;
589
590 if (ftdi == NULL)
591 ftdi_error_return(-8, "ftdi context invalid");
592
593 if (libusb_open(dev, &ftdi->usb_dev) < 0)
594 ftdi_error_return(-4, "libusb_open() failed");
595
596 if (libusb_get_device_descriptor(dev, &desc) < 0)
597 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
598
599 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
600 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
601 cfg0 = config0->bConfigurationValue;
602 libusb_free_config_descriptor (config0);
603
604 // Try to detach ftdi_sio kernel module.
605 //
606 // The return code is kept in a separate variable and only parsed
607 // if usb_set_configuration() or usb_claim_interface() fails as the
608 // detach operation might be denied and everything still works fine.
609 // Likely scenario is a static ftdi_sio kernel module.
610 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
611 {
612 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
613 detach_errno = errno;
614 }
615
616 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
617 ftdi_error_return(-12, "libusb_get_configuration () failed");
618 // set configuration (needed especially for windows)
619 // tolerate EBUSY: one device with one configuration, but two interfaces
620 // and libftdi sessions to both interfaces (e.g. FT2232)
621 if (desc.bNumConfigurations > 0 && cfg != cfg0)
622 {
623 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
624 {
625 ftdi_usb_close_internal (ftdi);
626 if (detach_errno == EPERM)
627 {
628 ftdi_error_return(-8, "inappropriate permissions on device!");
629 }
630 else
631 {
632 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
633 }
634 }
635 }
636
637 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
638 {
639 ftdi_usb_close_internal (ftdi);
640 if (detach_errno == EPERM)
641 {
642 ftdi_error_return(-8, "inappropriate permissions on device!");
643 }
644 else
645 {
646 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
647 }
648 }
649
650 if (ftdi_usb_reset (ftdi) != 0)
651 {
652 ftdi_usb_close_internal (ftdi);
653 ftdi_error_return(-6, "ftdi_usb_reset failed");
654 }
655
656 // Try to guess chip type
657 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
658 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
659 && desc.iSerialNumber == 0))
660 ftdi->type = TYPE_BM;
661 else if (desc.bcdDevice == 0x200)
662 ftdi->type = TYPE_AM;
663 else if (desc.bcdDevice == 0x500)
664 ftdi->type = TYPE_2232C;
665 else if (desc.bcdDevice == 0x600)
666 ftdi->type = TYPE_R;
667 else if (desc.bcdDevice == 0x700)
668 ftdi->type = TYPE_2232H;
669 else if (desc.bcdDevice == 0x800)
670 ftdi->type = TYPE_4232H;
671 else if (desc.bcdDevice == 0x900)
672 ftdi->type = TYPE_232H;
673 else if (desc.bcdDevice == 0x1000)
674 ftdi->type = TYPE_230X;
675
676 // Determine maximum packet size
677 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
678
679 if (ftdi_set_baudrate (ftdi, 9600) != 0)
680 {
681 ftdi_usb_close_internal (ftdi);
682 ftdi_error_return(-7, "set baudrate failed");
683 }
684
685 ftdi_error_return(0, "all fine");
686 }
687
697 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
698 {
699 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
700 }
701
723 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
724 const char* description, const char* serial)
725 {
726 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
727 }
728
753 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
754 const char* description, const char* serial, unsigned int index)
755 {
756 libusb_device *dev;
757 libusb_device **devs;
758 char string[256];
759 int i = 0;
760
761 if (ftdi == NULL)
762 ftdi_error_return(-11, "ftdi context invalid");
763
764 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
765 ftdi_error_return(-12, "libusb_get_device_list() failed");
766
767 while ((dev = devs[i++]) != NULL)
768 {
769 struct libusb_device_descriptor desc;
770 int res;
771
772 if (libusb_get_device_descriptor(dev, &desc) < 0)
773 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
774
775 if (desc.idVendor == vendor && desc.idProduct == product)
776 {
777 if (libusb_open(dev, &ftdi->usb_dev) < 0)
778 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
779
780 if (description != NULL)
781 {
782 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
783 {
784 ftdi_usb_close_internal (ftdi);
785 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
786 }
787 if (strncmp(string, description, sizeof(string)) != 0)
788 {
789 ftdi_usb_close_internal (ftdi);
790 continue;
791 }
792 }
793 if (serial != NULL)
794 {
795 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
796 {
797 ftdi_usb_close_internal (ftdi);
798 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
799 }
800 if (strncmp(string, serial, sizeof(string)) != 0)
801 {
802 ftdi_usb_close_internal (ftdi);
803 continue;
804 }
805 }
806
807 ftdi_usb_close_internal (ftdi);
808
809 if (index > 0)
810 {
811 index--;
812 continue;
813 }
814
815 res = ftdi_usb_open_dev(ftdi, dev);
816 libusb_free_device_list(devs,1);
817 return res;
818 }
819 }
820
821 // device not found
822 ftdi_error_return_free_device_list(-3, "device not found", devs);
823 }
824
851 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
852 {
853 if (ftdi == NULL)
854 ftdi_error_return(-12, "ftdi context invalid");
855
856 if (description[0] == 0 || description[1] != ':')
857 ftdi_error_return(-11, "illegal description format");
858
859 if (description[0] == 'd')
860 {
861 libusb_device *dev;
862 libusb_device **devs;
863 unsigned int bus_number, device_address;
864 int i = 0;
865
866 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
867 ftdi_error_return(-2, "libusb_get_device_list() failed");
868
869 /* XXX: This doesn't handle symlinks/odd paths/etc... */
870 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
871 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
872
873 while ((dev = devs[i++]) != NULL)
874 {
875 int ret;
876 if (bus_number == libusb_get_bus_number (dev)
877 && device_address == libusb_get_device_address (dev))
878 {
879 ret = ftdi_usb_open_dev(ftdi, dev);
880 libusb_free_device_list(devs,1);
881 return ret;
882 }
883 }
884
885 // device not found
886 ftdi_error_return_free_device_list(-3, "device not found", devs);
887 }
888 else if (description[0] == 'i' || description[0] == 's')
889 {
890 unsigned int vendor;
891 unsigned int product;
892 unsigned int index=0;
893 const char *serial=NULL;
894 const char *startp, *endp;
895
896 errno=0;
897 startp=description+2;
898 vendor=strtoul((char*)startp,(char**)&endp,0);
899 if (*endp != ':' || endp == startp || errno != 0)
900 ftdi_error_return(-11, "illegal description format");
901
902 startp=endp+1;
903 product=strtoul((char*)startp,(char**)&endp,0);
904 if (endp == startp || errno != 0)
905 ftdi_error_return(-11, "illegal description format");
906
907 if (description[0] == 'i' && *endp != 0)
908 {
909 /* optional index field in i-mode */
910 if (*endp != ':')
911 ftdi_error_return(-11, "illegal description format");
912
913 startp=endp+1;
914 index=strtoul((char*)startp,(char**)&endp,0);
915 if (*endp != 0 || endp == startp || errno != 0)
916 ftdi_error_return(-11, "illegal description format");
917 }
918 if (description[0] == 's')
919 {
920 if (*endp != ':')
921 ftdi_error_return(-11, "illegal description format");
922
923 /* rest of the description is the serial */
924 serial=endp+1;
925 }
926
927 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
928 }
929 else
930 {
931 ftdi_error_return(-11, "illegal description format");
932 }
933 }
934
944 int ftdi_usb_reset(struct ftdi_context *ftdi)
945 {
946 if (ftdi == NULL || ftdi->usb_dev == NULL)
947 ftdi_error_return(-2, "USB device unavailable");
948
949 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
950 SIO_RESET_REQUEST, SIO_RESET_SIO,
951 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
952 ftdi_error_return(-1,"FTDI reset failed");
953
954 // Invalidate data in the readbuffer
955 ftdi->readbuffer_offset = 0;
956 ftdi->readbuffer_remaining = 0;
957
958 return 0;
959 }
960
970 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
971 {
972 if (ftdi == NULL || ftdi->usb_dev == NULL)
973 ftdi_error_return(-2, "USB device unavailable");
974
975 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
976 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
977 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
978 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
979
980 // Invalidate data in the readbuffer
981 ftdi->readbuffer_offset = 0;
982 ftdi->readbuffer_remaining = 0;
983
984 return 0;
985 }
986
996 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
997 {
998 if (ftdi == NULL || ftdi->usb_dev == NULL)
999 ftdi_error_return(-2, "USB device unavailable");
1000
1001 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1002 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
1003 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1004 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
1005
1006 return 0;
1007 }
1008
1019 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
1020 {
1021 int result;
1022
1023 if (ftdi == NULL || ftdi->usb_dev == NULL)
1024 ftdi_error_return(-3, "USB device unavailable");
1025
1026 result = ftdi_usb_purge_rx_buffer(ftdi);
1027 if (result < 0)
1028 return -1;
1029
1030 result = ftdi_usb_purge_tx_buffer(ftdi);
1031 if (result < 0)
1032 return -2;
1033
1034 return 0;
1035 }
1036
1037
1038
1048 int ftdi_usb_close(struct ftdi_context *ftdi)
1049 {
1050 int rtn = 0;
1051
1052 if (ftdi == NULL)
1053 ftdi_error_return(-3, "ftdi context invalid");
1054
1055 if (ftdi->usb_dev != NULL)
1056 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
1057 rtn = -1;
1058
1059 ftdi_usb_close_internal (ftdi);
1060
1061 return rtn;
1062 }
1063
1064 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1065 to encoded divisor and the achievable baudrate
1066 Function is only used internally
1067 \internal
1068
1069 See AN120
1070 clk/1 -> 0
1071 clk/1.5 -> 1
1072 clk/2 -> 2
1073 From /2, 0.125/ 0.25 and 0.5 steps may be taken
1074 The fractional part has frac_code encoding
1075 */
1076 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1077
1078 {
1079 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1080 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1081 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1082 int divisor, best_divisor, best_baud, best_baud_diff;
1083 int i;
1084 divisor = 24000000 / baudrate;
1085
1086 // Round down to supported fraction (AM only)
1087 divisor -= am_adjust_dn[divisor & 7];
1088
1089 // Try this divisor and the one above it (because division rounds down)
1090 best_divisor = 0;
1091 best_baud = 0;
1092 best_baud_diff = 0;
1093 for (i = 0; i < 2; i++)
1094 {
1095 int try_divisor = divisor + i;
1096 int baud_estimate;
1097 int baud_diff;
1098
1099 // Round up to supported divisor value
1100 if (try_divisor <= 8)
1101 {
1102 // Round up to minimum supported divisor
1103 try_divisor = 8;
1104 }
1105 else if (divisor < 16)
1106 {
1107 // AM doesn't support divisors 9 through 15 inclusive
1108 try_divisor = 16;
1109 }
1110 else
1111 {
1112 // Round up to supported fraction (AM only)
1113 try_divisor += am_adjust_up[try_divisor & 7];
1114 if (try_divisor > 0x1FFF8)
1115 {
1116 // Round down to maximum supported divisor value (for AM)
1117 try_divisor = 0x1FFF8;
1118 }
1119 }
1120 // Get estimated baud rate (to nearest integer)
1121 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1122 // Get absolute difference from requested baud rate
1123 if (baud_estimate < baudrate)
1124 {
1125 baud_diff = baudrate - baud_estimate;
1126 }
1127 else
1128 {
1129 baud_diff = baud_estimate - baudrate;
1130 }
1131 if (i == 0 || baud_diff < best_baud_diff)
1132 {
1133 // Closest to requested baud rate so far
1134 best_divisor = try_divisor;
1135 best_baud = baud_estimate;
1136 best_baud_diff = baud_diff;
1137 if (baud_diff == 0)
1138 {
1139 // Spot on! No point trying
1140 break;
1141 }
1142 }
1143 }
1144 // Encode the best divisor value
1145 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1146 // Deal with special cases for encoded value
1147 if (*encoded_divisor == 1)
1148 {
1149 *encoded_divisor = 0; // 3000000 baud
1150 }
1151 else if (*encoded_divisor == 0x4001)
1152 {
1153 *encoded_divisor = 1; // 2000000 baud (BM only)
1154 }
1155 return best_baud;
1156 }
1157
1158 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1159 to encoded divisor and the achievable baudrate
1160 Function is only used internally
1161 \internal
1162
1163 See AN120
1164 clk/1 -> 0
1165 clk/1.5 -> 1
1166 clk/2 -> 2
1167 From /2, 0.125 steps may be taken.
1168 The fractional part has frac_code encoding
1169
1170 value[13:0] of value is the divisor
1171 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1172
1173 H Type have all features above with
1174 {index[8],value[15:14]} is the encoded subdivisor
1175
1176 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1177 {index[0],value[15:14]} is the encoded subdivisor
1178
1179 AM Type chips have only four fractional subdivisors at value[15:14]
1180 for subdivisors 0, 0.5, 0.25, 0.125
1181 */
1182 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1183 {
1184 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1185 int best_baud = 0;
1186 int divisor, best_divisor;
1187 if (baudrate >= clk/clk_div)
1188 {
1189 *encoded_divisor = 0;
1190 best_baud = clk/clk_div;
1191 }
1192 else if (baudrate >= clk/(clk_div + clk_div/2))
1193 {
1194 *encoded_divisor = 1;
1195 best_baud = clk/(clk_div + clk_div/2);
1196 }
1197 else if (baudrate >= clk/(2*clk_div))
1198 {
1199 *encoded_divisor = 2;
1200 best_baud = clk/(2*clk_div);
1201 }
1202 else
1203 {
1204 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1205 divisor = clk*16/clk_div / baudrate;
1206 if (divisor & 1) /* Decide if to round up or down*/
1207 best_divisor = divisor /2 +1;
1208 else
1209 best_divisor = divisor/2;
1210 if(best_divisor > 0x20000)
1211 best_divisor = 0x1ffff;
1212 best_baud = clk*16/clk_div/best_divisor;
1213 if (best_baud & 1) /* Decide if to round up or down*/
1214 best_baud = best_baud /2 +1;
1215 else
1216 best_baud = best_baud /2;
1217 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1218 }
1219 return best_baud;
1220 }
1226 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1227 unsigned short *value, unsigned short *index)
1228 {
1229 int best_baud;
1230 unsigned long encoded_divisor;
1231
1232 if (baudrate <= 0)
1233 {
1234 // Return error
1235 return -1;
1236 }
1237
1238 #define H_CLK 120000000
1239 #define C_CLK 48000000
1240 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1241 {
1242 if(baudrate*10 > H_CLK /0x3fff)
1243 {
1244 /* On H Devices, use 12 000 000 Baudrate when possible
1245 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1246 three fractional bits and a 120 MHz clock
1247 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1248 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1249 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1250 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1251 }
1252 else
1253 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1254 }
1255 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
1256 {
1257 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1258 }
1259 else
1260 {
1261 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1262 }
1263 // Split into "value" and "index" values
1264 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1265 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1266 {
1267 *index = (unsigned short)(encoded_divisor >> 8);
1268 *index &= 0xFF00;
1269 *index |= ftdi->index;
1270 }
1271 else
1272 *index = (unsigned short)(encoded_divisor >> 16);
1273
1274 // Return the nearest baud rate
1275 return best_baud;
1276 }
1277
1282 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1283 unsigned short *value, unsigned short *index)
1284 {
1285 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1286 }
1287
1299 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1300 {
1301 unsigned short value, index;
1302 int actual_baudrate;
1303
1304 if (ftdi == NULL || ftdi->usb_dev == NULL)
1305 ftdi_error_return(-3, "USB device unavailable");
1306
1307 if (ftdi->bitbang_enabled)
1308 {
1309 baudrate = baudrate*4;
1310 }
1311
1312 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1313 if (actual_baudrate <= 0)
1314 ftdi_error_return (-1, "Silly baudrate <= 0.");
1315
1316 // Check within tolerance (about 5%)
1317 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1318 || ((actual_baudrate < baudrate)
1319 ? (actual_baudrate * 21 < baudrate * 20)
1320 : (baudrate * 21 < actual_baudrate * 20)))
1321 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1322
1323 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1324 SIO_SET_BAUDRATE_REQUEST, value,
1325 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1326 ftdi_error_return (-2, "Setting new baudrate failed");
1327
1328 ftdi->baudrate = baudrate;
1329 return 0;
1330 }
1331
1345 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1346 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1347 {
1348 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1349 }
1350
1364 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1365 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1366 enum ftdi_break_type break_type)
1367 {
1368 unsigned short value = bits;
1369
1370 if (ftdi == NULL || ftdi->usb_dev == NULL)
1371 ftdi_error_return(-2, "USB device unavailable");
1372
1373 switch (parity)
1374 {
1375 case NONE:
1376 value |= (0x00 << 8);
1377 break;
1378 case ODD:
1379 value |= (0x01 << 8);
1380 break;
1381 case EVEN:
1382 value |= (0x02 << 8);
1383 break;
1384 case MARK:
1385 value |= (0x03 << 8);
1386 break;
1387 case SPACE:
1388 value |= (0x04 << 8);
1389 break;
1390 }
1391
1392 switch (sbit)
1393 {
1394 case STOP_BIT_1:
1395 value |= (0x00 << 11);
1396 break;
1397 case STOP_BIT_15:
1398 value |= (0x01 << 11);
1399 break;
1400 case STOP_BIT_2:
1401 value |= (0x02 << 11);
1402 break;
1403 }
1404
1405 switch (break_type)
1406 {
1407 case BREAK_OFF:
1408 value |= (0x00 << 14);
1409 break;
1410 case BREAK_ON:
1411 value |= (0x01 << 14);
1412 break;
1413 }
1414
1415 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1416 SIO_SET_DATA_REQUEST, value,
1417 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1418 ftdi_error_return (-1, "Setting new line property failed");
1419
1420 return 0;
1421 }
1422
1434 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1435 {
1436 int offset = 0;
1437 int actual_length;
1438
1439 if (ftdi == NULL || ftdi->usb_dev == NULL)
1440 ftdi_error_return(-666, "USB device unavailable");
1441
1442 while (offset < size)
1443 {
1444 int write_size = ftdi->writebuffer_chunksize;
1445
1446 if (offset+write_size > size)
1447 write_size = size-offset;
1448
1449 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1450 ftdi_error_return(-1, "usb bulk write failed");
1451
1452 offset += actual_length;
1453 }
1454
1455 return offset;
1456 }
1457
1458 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1459 {
1460 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1461 struct ftdi_context *ftdi = tc->ftdi;
1462 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1463
1464 packet_size = ftdi->max_packet_size;
1465
1466 actual_length = transfer->actual_length;
1467
1468 if (actual_length > 2)
1469 {
1470 // skip FTDI status bytes.
1471 // Maybe stored in the future to enable modem use
1472 num_of_chunks = actual_length / packet_size;
1473 chunk_remains = actual_length % packet_size;
1474 //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);
1475
1476 ftdi->readbuffer_offset += 2;
1477 actual_length -= 2;
1478
1479 if (actual_length > packet_size - 2)
1480 {
1481 for (i = 1; i < num_of_chunks; i++)
1482 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1483 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1484 packet_size - 2);
1485 if (chunk_remains > 2)
1486 {
1487 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1488 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1489 chunk_remains-2);
1490 actual_length -= 2*num_of_chunks;
1491 }
1492 else
1493 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1494 }
1495
1496 if (actual_length > 0)
1497 {
1498 // data still fits in buf?
1499 if (tc->offset + actual_length <= tc->size)
1500 {
1501 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1502 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1503 tc->offset += actual_length;
1504
1505 ftdi->readbuffer_offset = 0;
1506 ftdi->readbuffer_remaining = 0;
1507
1508 /* Did we read exactly the right amount of bytes? */
1509 if (tc->offset == tc->size)
1510 {
1511 //printf("read_data exact rem %d offset %d\n",
1512 //ftdi->readbuffer_remaining, offset);
1513 tc->completed = 1;
1514 return;
1515 }
1516 }
1517 else
1518 {
1519 // only copy part of the data or size <= readbuffer_chunksize
1520 int part_size = tc->size - tc->offset;
1521 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1522 tc->offset += part_size;
1523
1524 ftdi->readbuffer_offset += part_size;
1525 ftdi->readbuffer_remaining = actual_length - part_size;
1526
1527 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1528 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1529 tc->completed = 1;
1530 return;
1531 }
1532 }
1533 }
1534
1535 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1536 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1537 else
1538 {
1539 ret = libusb_submit_transfer (transfer);
1540 if (ret < 0)
1541 tc->completed = 1;
1542 }
1543 }
1544
1545
1546 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1547 {
1548 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1549 struct ftdi_context *ftdi = tc->ftdi;
1550
1551 tc->offset += transfer->actual_length;
1552
1553 if (tc->offset == tc->size)
1554 {
1555 tc->completed = 1;
1556 }
1557 else
1558 {
1559 int write_size = ftdi->writebuffer_chunksize;
1560 int ret;
1561
1562 if (tc->offset + write_size > tc->size)
1563 write_size = tc->size - tc->offset;
1564
1565 transfer->length = write_size;
1566 transfer->buffer = tc->buf + tc->offset;
1567
1568 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
1569 tc->completed = LIBUSB_TRANSFER_CANCELLED;
1570 else
1571 {
1572 ret = libusb_submit_transfer (transfer);
1573 if (ret < 0)
1574 tc->completed = 1;
1575 }
1576 }
1577 }
1578
1579
1594 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1595 {
1596 struct ftdi_transfer_control *tc;
1597 struct libusb_transfer *transfer;
1598 int write_size, ret;
1599
1600 if (ftdi == NULL || ftdi->usb_dev == NULL)
1601 return NULL;
1602
1603 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1604 if (!tc)
1605 return NULL;
1606
1607 transfer = libusb_alloc_transfer(0);
1608 if (!transfer)
1609 {
1610 free(tc);
1611 return NULL;
1612 }
1613
1614 tc->ftdi = ftdi;
1615 tc->completed = 0;
1616 tc->buf = buf;
1617 tc->size = size;
1618 tc->offset = 0;
1619
1620 if (size < (int)ftdi->writebuffer_chunksize)
1621 write_size = size;
1622 else
1623 write_size = ftdi->writebuffer_chunksize;
1624
1625 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1626 write_size, ftdi_write_data_cb, tc,
1627 ftdi->usb_write_timeout);
1628 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1629
1630 ret = libusb_submit_transfer(transfer);
1631 if (ret < 0)
1632 {
1633 libusb_free_transfer(transfer);
1634 free(tc);
1635 return NULL;
1636 }
1637 tc->transfer = transfer;
1638
1639 return tc;
1640 }
1641
1656 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1657 {
1658 struct ftdi_transfer_control *tc;
1659 struct libusb_transfer *transfer;
1660 int ret;
1661
1662 if (ftdi == NULL || ftdi->usb_dev == NULL)
1663 return NULL;
1664
1665 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1666 if (!tc)
1667 return NULL;
1668
1669 tc->ftdi = ftdi;
1670 tc->buf = buf;
1671 tc->size = size;
1672
1673 if (size <= (int)ftdi->readbuffer_remaining)
1674 {
1675 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1676
1677 // Fix offsets
1678 ftdi->readbuffer_remaining -= size;
1679 ftdi->readbuffer_offset += size;
1680
1681 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1682
1683 tc->completed = 1;
1684 tc->offset = size;
1685 tc->transfer = NULL;
1686 return tc;
1687 }
1688
1689 tc->completed = 0;
1690 if (ftdi->readbuffer_remaining != 0)
1691 {
1692 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1693
1694 tc->offset = ftdi->readbuffer_remaining;
1695 }
1696 else
1697 tc->offset = 0;
1698
1699 transfer = libusb_alloc_transfer(0);
1700 if (!transfer)
1701 {
1702 free (tc);
1703 return NULL;
1704 }
1705
1706 ftdi->readbuffer_remaining = 0;
1707 ftdi->readbuffer_offset = 0;
1708
1709 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);
1710 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1711
1712 ret = libusb_submit_transfer(transfer);
1713 if (ret < 0)
1714 {
1715 libusb_free_transfer(transfer);
1716 free (tc);
1717 return NULL;
1718 }
1719 tc->transfer = transfer;
1720
1721 return tc;
1722 }
1723
1735 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1736 {
1737 int ret;
1738 struct timeval to = { 0, 0 };
1739 while (!tc->completed)
1740 {
1741 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1742 &to, &tc->completed);
1743 if (ret < 0)
1744 {
1745 if (ret == LIBUSB_ERROR_INTERRUPTED)
1746 continue;
1747 libusb_cancel_transfer(tc->transfer);
1748 while (!tc->completed)
1749 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
1750 &to, &tc->completed) < 0)
1751 break;
1752 libusb_free_transfer(tc->transfer);
1753 free (tc);
1754 return ret;
1755 }
1756 }
1757
1758 ret = tc->offset;
1763 if (tc->transfer)
1764 {
1765 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1766 ret = -1;
1767 libusb_free_transfer(tc->transfer);
1768 }
1769 free(tc);
1770 return ret;
1771 }
1772
1782 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
1783 struct timeval * to)
1784 {
1785 struct timeval tv = { 0, 0 };
1786
1787 if (!tc->completed && tc->transfer != NULL)
1788 {
1789 if (to == NULL)
1790 to = &tv;
1791
1792 libusb_cancel_transfer(tc->transfer);
1793 while (!tc->completed)
1794 {
1795 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
1796 break;
1797 }
1798 }
1799
1800 if (tc->transfer)
1801 libusb_free_transfer(tc->transfer);
1802
1803 free (tc);
1804 }
1805
1816 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1817 {
1818 if (ftdi == NULL)
1819 ftdi_error_return(-1, "ftdi context invalid");
1820
1821 ftdi->writebuffer_chunksize = chunksize;
1822 return 0;
1823 }
1824
1834 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1835 {
1836 if (ftdi == NULL)
1837 ftdi_error_return(-1, "ftdi context invalid");
1838
1839 *chunksize = ftdi->writebuffer_chunksize;
1840 return 0;
1841 }
1842
1858 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1859 {
1860 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1861 int packet_size = ftdi->max_packet_size;
1862 int actual_length = 1;
1863
1864 if (ftdi == NULL || ftdi->usb_dev == NULL)
1865 ftdi_error_return(-666, "USB device unavailable");
1866
1867 // Packet size sanity check (avoid division by zero)
1868 if (packet_size == 0)
1869 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1870
1871 // everything we want is still in the readbuffer?
1872 if (size <= (int)ftdi->readbuffer_remaining)
1873 {
1874 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1875
1876 // Fix offsets
1877 ftdi->readbuffer_remaining -= size;
1878 ftdi->readbuffer_offset += size;
1879
1880 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1881
1882 return size;
1883 }
1884 // something still in the readbuffer, but not enough to satisfy 'size'?
1885 if (ftdi->readbuffer_remaining != 0)
1886 {
1887 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1888
1889 // Fix offset
1890 offset += ftdi->readbuffer_remaining;
1891 }
1892 // do the actual USB read
1893 while (offset < size && actual_length > 0)
1894 {
1895 ftdi->readbuffer_remaining = 0;
1896 ftdi->readbuffer_offset = 0;
1897 /* returns how much received */
1898 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1899 if (ret < 0)
1900 ftdi_error_return(ret, "usb bulk read failed");
1901
1902 if (actual_length > 2)
1903 {
1904 // skip FTDI status bytes.
1905 // Maybe stored in the future to enable modem use
1906 num_of_chunks = actual_length / packet_size;
1907 chunk_remains = actual_length % packet_size;
1908 //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);
1909
1910 ftdi->readbuffer_offset += 2;
1911 actual_length -= 2;
1912
1913 if (actual_length > packet_size - 2)
1914 {
1915 for (i = 1; i < num_of_chunks; i++)
1916 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1917 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1918 packet_size - 2);
1919 if (chunk_remains > 2)
1920 {
1921 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1922 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1923 chunk_remains-2);
1924 actual_length -= 2*num_of_chunks;
1925 }
1926 else
1927 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1928 }
1929 }
1930 else if (actual_length <= 2)
1931 {
1932 // no more data to read?
1933 return offset;
1934 }
1935 if (actual_length > 0)
1936 {
1937 // data still fits in buf?
1938 if (offset+actual_length <= size)
1939 {
1940 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1941 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1942 offset += actual_length;
1943
1944 /* Did we read exactly the right amount of bytes? */
1945 if (offset == size)
1946 //printf("read_data exact rem %d offset %d\n",
1947 //ftdi->readbuffer_remaining, offset);
1948 return offset;
1949 }
1950 else
1951 {
1952 // only copy part of the data or size <= readbuffer_chunksize
1953 int part_size = size-offset;
1954 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1955
1956 ftdi->readbuffer_offset += part_size;
1957 ftdi->readbuffer_remaining = actual_length-part_size;
1958 offset += part_size;
1959
1960 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1961 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1962
1963 return offset;
1964 }
1965 }
1966 }
1967 // never reached
1968 return -127;
1969 }
1970
1983 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1984 {
1985 unsigned char *new_buf;
1986
1987 if (ftdi == NULL)
1988 ftdi_error_return(-1, "ftdi context invalid");
1989
1990 // Invalidate all remaining data
1991 ftdi->readbuffer_offset = 0;
1992 ftdi->readbuffer_remaining = 0;
1993 #ifdef __linux__
1994 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1995 which is defined in libusb-1.0. Otherwise, each USB read request will
1996 be divided into multiple URBs. This will cause issues on Linux kernel
1997 older than 2.6.32. */
1998 if (chunksize > 16384)
1999 chunksize = 16384;
2000 #endif
2001
2002 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
2003 ftdi_error_return(-1, "out of memory for readbuffer");
2004
2005 ftdi->readbuffer = new_buf;
2006 ftdi->readbuffer_chunksize = chunksize;
2007
2008 return 0;
2009 }
2010
2020 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
2021 {
2022 if (ftdi == NULL)
2023 ftdi_error_return(-1, "FTDI context invalid");
2024
2025 *chunksize = ftdi->readbuffer_chunksize;
2026 return 0;
2027 }
2028
2041 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
2042 {
2043 unsigned short usb_val;
2044
2045 if (ftdi == NULL || ftdi->usb_dev == NULL)
2046 ftdi_error_return(-2, "USB device unavailable");
2047
2048 usb_val = bitmask; // low byte: bitmask
2049 usb_val |= (mode << 8);
2050 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)
2051 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
2052
2053 ftdi->bitbang_mode = mode;
2054 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
2055 return 0;
2056 }
2057
2067 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
2068 {
2069 if (ftdi == NULL || ftdi->usb_dev == NULL)
2070 ftdi_error_return(-2, "USB device unavailable");
2071
2072 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)
2073 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
2074
2075 ftdi->bitbang_enabled = 0;
2076 return 0;
2077 }
2078
2079
2090 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
2091 {
2092 if (ftdi == NULL || ftdi->usb_dev == NULL)
2093 ftdi_error_return(-2, "USB device unavailable");
2094
2095 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)
2096 ftdi_error_return(-1, "read pins failed");
2097
2098 return 0;
2099 }
2100
2116 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2117 {
2118 unsigned short usb_val;
2119
2120 if (latency < 1)
2121 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2122
2123 if (ftdi == NULL || ftdi->usb_dev == NULL)
2124 ftdi_error_return(-3, "USB device unavailable");
2125
2126 usb_val = latency;
2127 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)
2128 ftdi_error_return(-2, "unable to set latency timer");
2129
2130 return 0;
2131 }
2132
2143 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2144 {
2145 unsigned short usb_val;
2146
2147 if (ftdi == NULL || ftdi->usb_dev == NULL)
2148 ftdi_error_return(-2, "USB device unavailable");
2149
2150 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)
2151 ftdi_error_return(-1, "reading latency timer failed");
2152
2153 *latency = (unsigned char)usb_val;
2154 return 0;
2155 }
2156
2197 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2198 {
2199 char usb_val[2];
2200
2201 if (ftdi == NULL || ftdi->usb_dev == NULL)
2202 ftdi_error_return(-2, "USB device unavailable");
2203
2204 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)
2205 ftdi_error_return(-1, "getting modem status failed");
2206
2207 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2208
2209 return 0;
2210 }
2211
2223 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2224 {
2225 if (ftdi == NULL || ftdi->usb_dev == NULL)
2226 ftdi_error_return(-2, "USB device unavailable");
2227
2228 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2229 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2230 NULL, 0, ftdi->usb_write_timeout) < 0)
2231 ftdi_error_return(-1, "set flow control failed");
2232
2233 return 0;
2234 }
2235
2246 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2247 {
2248 unsigned short usb_val;
2249
2250 if (ftdi == NULL || ftdi->usb_dev == NULL)
2251 ftdi_error_return(-2, "USB device unavailable");
2252
2253 if (state)
2254 usb_val = SIO_SET_DTR_HIGH;
2255 else
2256 usb_val = SIO_SET_DTR_LOW;
2257
2258 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2259 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2260 NULL, 0, ftdi->usb_write_timeout) < 0)
2261 ftdi_error_return(-1, "set dtr failed");
2262
2263 return 0;
2264 }
2265
2276 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2277 {
2278 unsigned short usb_val;
2279
2280 if (ftdi == NULL || ftdi->usb_dev == NULL)
2281 ftdi_error_return(-2, "USB device unavailable");
2282
2283 if (state)
2284 usb_val = SIO_SET_RTS_HIGH;
2285 else
2286 usb_val = SIO_SET_RTS_LOW;
2287
2288 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2289 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2290 NULL, 0, ftdi->usb_write_timeout) < 0)
2291 ftdi_error_return(-1, "set of rts failed");
2292
2293 return 0;
2294 }
2295
2307 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2308 {
2309 unsigned short usb_val;
2310
2311 if (ftdi == NULL || ftdi->usb_dev == NULL)
2312 ftdi_error_return(-2, "USB device unavailable");
2313
2314 if (dtr)
2315 usb_val = SIO_SET_DTR_HIGH;
2316 else
2317 usb_val = SIO_SET_DTR_LOW;
2318
2319 if (rts)
2320 usb_val |= SIO_SET_RTS_HIGH;
2321 else
2322 usb_val |= SIO_SET_RTS_LOW;
2323
2324 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2325 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2326 NULL, 0, ftdi->usb_write_timeout) < 0)
2327 ftdi_error_return(-1, "set of rts/dtr failed");
2328
2329 return 0;
2330 }
2331
2343 int ftdi_set_event_char(struct ftdi_context *ftdi,
2344 unsigned char eventch, unsigned char enable)
2345 {
2346 unsigned short usb_val;
2347
2348 if (ftdi == NULL || ftdi->usb_dev == NULL)
2349 ftdi_error_return(-2, "USB device unavailable");
2350
2351 usb_val = eventch;
2352 if (enable)
2353 usb_val |= 1 << 8;
2354
2355 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)
2356 ftdi_error_return(-1, "setting event character failed");
2357
2358 return 0;
2359 }
2360
2372 int ftdi_set_error_char(struct ftdi_context *ftdi,
2373 unsigned char errorch, unsigned char enable)
2374 {
2375 unsigned short usb_val;
2376
2377 if (ftdi == NULL || ftdi->usb_dev == NULL)
2378 ftdi_error_return(-2, "USB device unavailable");
2379
2380 usb_val = errorch;
2381 if (enable)
2382 usb_val |= 1 << 8;
2383
2384 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)
2385 ftdi_error_return(-1, "setting error character failed");
2386
2387 return 0;
2388 }
2389
2402 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2403 char * product, char * serial)
2404 {
2405 struct ftdi_eeprom *eeprom;
2406
2407 if (ftdi == NULL)
2408 ftdi_error_return(-1, "No struct ftdi_context");
2409
2410 if (ftdi->eeprom == NULL)
2411 ftdi_error_return(-2,"No struct ftdi_eeprom");
2412
2413 eeprom = ftdi->eeprom;
2414 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2415
2416 if (ftdi->usb_dev == NULL)
2417 ftdi_error_return(-3, "No connected device or device not yet opened");
2418
2419 eeprom->vendor_id = 0x0403;
2420 eeprom->use_serial = 1;
2421 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2422 (ftdi->type == TYPE_R))
2423 eeprom->product_id = 0x6001;
2424 else if (ftdi->type == TYPE_4232H)
2425 eeprom->product_id = 0x6011;
2426 else if (ftdi->type == TYPE_232H)
2427 eeprom->product_id = 0x6014;
2428 else if (ftdi->type == TYPE_230X)
2429 eeprom->product_id = 0x6015;
2430 else
2431 eeprom->product_id = 0x6010;
2432
2433 if (ftdi->type == TYPE_AM)
2434 eeprom->usb_version = 0x0101;
2435 else
2436 eeprom->usb_version = 0x0200;
2437 eeprom->max_power = 100;
2438
2439 if (eeprom->manufacturer)
2440 free (eeprom->manufacturer);
2441 eeprom->manufacturer = NULL;
2442 if (manufacturer)
2443 {
2444 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2445 if (eeprom->manufacturer)
2446 strcpy(eeprom->manufacturer, manufacturer);
2447 }
2448
2449 if (eeprom->product)
2450 free (eeprom->product);
2451 eeprom->product = NULL;
2452 if(product)
2453 {
2454 eeprom->product = malloc(strlen(product)+1);
2455 if (eeprom->product)
2456 strcpy(eeprom->product, product);
2457 }
2458 else
2459 {
2460 const char* default_product;
2461 switch(ftdi->type)
2462 {
2463 case TYPE_AM: default_product = "AM"; break;
2464 case TYPE_BM: default_product = "BM"; break;
2465 case TYPE_2232C: default_product = "Dual RS232"; break;
2466 case TYPE_R: default_product = "FT232R USB UART"; break;
2467 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2468 case TYPE_4232H: default_product = "FT4232H"; break;
2469 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2470 case TYPE_230X: default_product = "FT230X Basic UART"; break;
2471 default:
2472 ftdi_error_return(-3, "Unknown chip type");
2473 }
2474 eeprom->product = malloc(strlen(default_product) +1);
2475 if (eeprom->product)
2476 strcpy(eeprom->product, default_product);
2477 }
2478
2479 if (eeprom->serial)
2480 free (eeprom->serial);
2481 eeprom->serial = NULL;
2482 if (serial)
2483 {
2484 eeprom->serial = malloc(strlen(serial)+1);
2485 if (eeprom->serial)
2486 strcpy(eeprom->serial, serial);
2487 }
2488
2489 if (ftdi->type == TYPE_R)
2490 {
2491 eeprom->max_power = 90;
2492 eeprom->size = 0x80;
2493 eeprom->cbus_function[0] = CBUS_TXLED;
2494 eeprom->cbus_function[1] = CBUS_RXLED;
2495 eeprom->cbus_function[2] = CBUS_TXDEN;
2496 eeprom->cbus_function[3] = CBUS_PWREN;
2497 eeprom->cbus_function[4] = CBUS_SLEEP;
2498 }
2499 else if (ftdi->type == TYPE_230X)
2500 {
2501 eeprom->max_power = 90;
2502 eeprom->size = 0x100;
2503 eeprom->cbus_function[0] = CBUSX_TXDEN;
2504 eeprom->cbus_function[1] = CBUSX_RXLED;
2505 eeprom->cbus_function[2] = CBUSX_TXLED;
2506 eeprom->cbus_function[3] = CBUSX_SLEEP;
2507 }
2508 else
2509 {
2510 if(ftdi->type == TYPE_232H)
2511 {
2512 int i;
2513 for (i=0; i<10; i++)
2514 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2515 }
2516 eeprom->size = -1;
2517 }
2518 switch (ftdi->type)
2519 {
2520 case TYPE_AM:
2521 eeprom->release_number = 0x0200;
2522 break;
2523 case TYPE_BM:
2524 eeprom->release_number = 0x0400;
2525 break;
2526 case TYPE_2232C:
2527 eeprom->release_number = 0x0500;
2528 break;
2529 case TYPE_R:
2530 eeprom->release_number = 0x0600;
2531 break;
2532 case TYPE_2232H:
2533 eeprom->release_number = 0x0700;
2534 break;
2535 case TYPE_4232H:
2536 eeprom->release_number = 0x0800;
2537 break;
2538 case TYPE_232H:
2539 eeprom->release_number = 0x0900;
2540 break;
2541 case TYPE_230X:
2542 eeprom->release_number = 0x1000;
2543 break;
2544 default:
2545 eeprom->release_number = 0x00;
2546 }
2547 return 0;
2548 }
2549
2550 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
2551 char * product, char * serial)
2552 {
2553 struct ftdi_eeprom *eeprom;
2554
2555 if (ftdi == NULL)
2556 ftdi_error_return(-1, "No struct ftdi_context");
2557
2558 if (ftdi->eeprom == NULL)
2559 ftdi_error_return(-2,"No struct ftdi_eeprom");
2560
2561 eeprom = ftdi->eeprom;
2562
2563 if (ftdi->usb_dev == NULL)
2564 ftdi_error_return(-3, "No connected device or device not yet opened");
2565
2566 if (manufacturer)
2567 {
2568 if (eeprom->manufacturer)
2569 free (eeprom->manufacturer);
2570 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2571 if (eeprom->manufacturer)
2572 strcpy(eeprom->manufacturer, manufacturer);
2573 }
2574
2575 if(product)
2576 {
2577 if (eeprom->product)
2578 free (eeprom->product);
2579 eeprom->product = malloc(strlen(product)+1);
2580 if (eeprom->product)
2581 strcpy(eeprom->product, product);
2582 }
2583
2584 if (serial)
2585 {
2586 if (eeprom->serial)
2587 free (eeprom->serial);
2588 eeprom->serial = malloc(strlen(serial)+1);
2589 if (eeprom->serial)
2590 {
2591 strcpy(eeprom->serial, serial);
2592 eeprom->use_serial = 1;
2593 }
2594 }
2595 return 0;
2596 }
2597
2598
2599 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal options*/
2600 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2601 {
2602 int i;
2603 for(i=0; i<5; i++)
2604 {
2605 int mode_low, mode_high;
2606 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2607 mode_low = CBUSH_TRISTATE;
2608 else
2609 mode_low = eeprom->cbus_function[2*i];
2610 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2611 mode_high = CBUSH_TRISTATE;
2612 else
2613 mode_high = eeprom->cbus_function[2*i+1];
2614
2615 output[0x18+i] = (mode_high <<4) | mode_low;
2616 }
2617 }
2618 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2619 *
2620 */
2621 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2622 {
2623 switch (chip)
2624 {
2625 case TYPE_2232H:
2626 case TYPE_2232C:
2627 {
2628 switch (type)
2629 {
2630 case CHANNEL_IS_UART: return 0;
2631 case CHANNEL_IS_FIFO: return 0x01;
2632 case CHANNEL_IS_OPTO: return 0x02;
2633 case CHANNEL_IS_CPU : return 0x04;
2634 default: return 0;
2635 }
2636 }
2637 case TYPE_232H:
2638 {
2639 switch (type)
2640 {
2641 case CHANNEL_IS_UART : return 0;
2642 case CHANNEL_IS_FIFO : return 0x01;
2643 case CHANNEL_IS_OPTO : return 0x02;
2644 case CHANNEL_IS_CPU : return 0x04;
2645 case CHANNEL_IS_FT1284 : return 0x08;
2646 default: return 0;
2647 }
2648 }
2649 case TYPE_230X: /* FT230X is only UART */
2650 default: return 0;
2651 }
2652 return 0;
2653 }
2654
2669 int ftdi_eeprom_build(struct ftdi_context *ftdi)
2670 {
2671 unsigned char i, j, eeprom_size_mask;
2672 unsigned short checksum, value;
2673 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2674 int user_area_size, free_start, free_end;
2675 struct ftdi_eeprom *eeprom;
2676 unsigned char * output;
2677
2678 if (ftdi == NULL)
2679 ftdi_error_return(-2,"No context");
2680 if (ftdi->eeprom == NULL)
2681 ftdi_error_return(-2,"No eeprom structure");
2682
2683 eeprom= ftdi->eeprom;
2684 output = eeprom->buf;
2685
2686 if (eeprom->chip == -1)
2687 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2688
2689 if (eeprom->size == -1)
2690 {
2691 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2692 eeprom->size = 0x100;
2693 else
2694 eeprom->size = 0x80;
2695 }
2696
2697 if (eeprom->manufacturer != NULL)
2698 manufacturer_size = strlen(eeprom->manufacturer);
2699 if (eeprom->product != NULL)
2700 product_size = strlen(eeprom->product);
2701 if (eeprom->serial != NULL)
2702 serial_size = strlen(eeprom->serial);
2703
2704 // eeprom size check
2705 switch (ftdi->type)
2706 {
2707 case TYPE_AM:
2708 case TYPE_BM:
2709 case TYPE_R:
2710 user_area_size = 96; // base size for strings (total of 48 characters)
2711 break;
2712 case TYPE_2232C:
2713 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2714 break;
2715 case TYPE_230X:
2716 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2717 break;
2718 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2719 case TYPE_4232H:
2720 user_area_size = 86;
2721 break;
2722 case TYPE_232H:
2723 user_area_size = 80;
2724 break;
2725 default:
2726 user_area_size = 0;
2727 break;
2728 }
2729 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2730
2731 if (user_area_size < 0)
2732 ftdi_error_return(-1,"eeprom size exceeded");
2733
2734 // empty eeprom
2735 if (ftdi->type == TYPE_230X)
2736 {
2737 /* FT230X have a reserved section in the middle of the MTP,
2738 which cannot be written to, but must be included in the checksum */
2739 memset(ftdi->eeprom->buf, 0, 0x80);
2740 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2741 }
2742 else
2743 {
2744 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2745 }
2746
2747 // Bytes and Bits set for all Types
2748
2749 // Addr 02: Vendor ID
2750 output[0x02] = eeprom->vendor_id;
2751 output[0x03] = eeprom->vendor_id >> 8;
2752
2753 // Addr 04: Product ID
2754 output[0x04] = eeprom->product_id;
2755 output[0x05] = eeprom->product_id >> 8;
2756
2757 // Addr 06: Device release number (0400h for BM features)
2758 output[0x06] = eeprom->release_number;
2759 output[0x07] = eeprom->release_number >> 8;
2760
2761 // Addr 08: Config descriptor
2762 // Bit 7: always 1
2763 // Bit 6: 1 if this device is self powered, 0 if bus powered
2764 // Bit 5: 1 if this device uses remote wakeup
2765 // Bit 4-0: reserved - 0
2766 j = 0x80;
2767 if (eeprom->self_powered)
2768 j |= 0x40;
2769 if (eeprom->remote_wakeup)
2770 j |= 0x20;
2771 output[0x08] = j;
2772
2773 // Addr 09: Max power consumption: max power = value * 2 mA
2774 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2775
2776 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2777 {
2778 // Addr 0A: Chip configuration
2779 // Bit 7: 0 - reserved
2780 // Bit 6: 0 - reserved
2781 // Bit 5: 0 - reserved
2782 // Bit 4: 1 - Change USB version
2783 // Bit 3: 1 - Use the serial number string
2784 // Bit 2: 1 - Enable suspend pull downs for lower power
2785 // Bit 1: 1 - Out EndPoint is Isochronous
2786 // Bit 0: 1 - In EndPoint is Isochronous
2787 //
2788 j = 0;
2789 if (eeprom->in_is_isochronous)
2790 j = j | 1;
2791 if (eeprom->out_is_isochronous)
2792 j = j | 2;
2793 output[0x0A] = j;
2794 }
2795
2796 // Dynamic content
2797 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2798 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2799 // 0xa0 (TYPE_232H)
2800 i = 0;
2801 switch (ftdi->type)
2802 {
2803 case TYPE_2232H:
2804 case TYPE_4232H:
2805 i += 2;
2806 case TYPE_R:
2807 i += 2;
2808 case TYPE_2232C:
2809 i += 2;
2810 case TYPE_AM:
2811 case TYPE_BM:
2812 i += 0x94;
2813 break;
2814 case TYPE_232H:
2815 case TYPE_230X:
2816 i = 0xa0;
2817 break;
2818 }
2819 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2820 eeprom_size_mask = eeprom->size -1;
2821 free_end = i & eeprom_size_mask;
2822
2823 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2824 // Addr 0F: Length of manufacturer string
2825 // Output manufacturer
2826 output[0x0E] = i; // calculate offset
2827 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2828 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2829 for (j = 0; j < manufacturer_size; j++)
2830 {
2831 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2832 output[i & eeprom_size_mask] = 0x00, i++;
2833 }
2834 output[0x0F] = manufacturer_size*2 + 2;
2835
2836 // Addr 10: Offset of the product string + 0x80, calculated later
2837 // Addr 11: Length of product string
2838 output[0x10] = i | 0x80; // calculate offset
2839 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2840 output[i & eeprom_size_mask] = 0x03, i++;
2841 for (j = 0; j < product_size; j++)
2842 {
2843 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2844 output[i & eeprom_size_mask] = 0x00, i++;
2845 }
2846 output[0x11] = product_size*2 + 2;
2847
2848 // Addr 12: Offset of the serial string + 0x80, calculated later
2849 // Addr 13: Length of serial string
2850 output[0x12] = i | 0x80; // calculate offset
2851 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2852 output[i & eeprom_size_mask] = 0x03, i++;
2853 for (j = 0; j < serial_size; j++)
2854 {
2855 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2856 output[i & eeprom_size_mask] = 0x00, i++;
2857 }
2858
2859 // Legacy port name and PnP fields for FT2232 and newer chips
2860 if (ftdi->type > TYPE_BM)
2861 {
2862 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2863 i++;
2864 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2865 i++;
2866 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2867 i++;
2868 }
2869
2870 output[0x13] = serial_size*2 + 2;
2871
2872 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2873 {
2874 if (eeprom->use_serial)
2875 output[0x0A] |= USE_SERIAL_NUM;
2876 else
2877 output[0x0A] &= ~USE_SERIAL_NUM;
2878 }
2879
2880 /* Bytes and Bits specific to (some) types
2881 Write linear, as this allows easier fixing*/
2882 switch (ftdi->type)
2883 {
2884 case TYPE_AM:
2885 break;
2886 case TYPE_BM:
2887 output[0x0C] = eeprom->usb_version & 0xff;
2888 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2889 if (eeprom->use_usb_version)
2890 output[0x0A] |= USE_USB_VERSION_BIT;
2891 else
2892 output[0x0A] &= ~USE_USB_VERSION_BIT;
2893
2894 break;
2895 case TYPE_2232C:
2896
2897 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
2898 if ( eeprom->channel_a_driver == DRIVER_VCP)
2899 output[0x00] |= DRIVER_VCP;
2900 else
2901 output[0x00] &= ~DRIVER_VCP;
2902
2903 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2904 output[0x00] |= HIGH_CURRENT_DRIVE;
2905 else
2906 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2907
2908 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
2909 if ( eeprom->channel_b_driver == DRIVER_VCP)
2910 output[0x01] |= DRIVER_VCP;
2911 else
2912 output[0x01] &= ~DRIVER_VCP;
2913
2914 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2915 output[0x01] |= HIGH_CURRENT_DRIVE;
2916 else
2917 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2918
2919 if (eeprom->in_is_isochronous)
2920 output[0x0A] |= 0x1;
2921 else
2922 output[0x0A] &= ~0x1;
2923 if (eeprom->out_is_isochronous)
2924 output[0x0A] |= 0x2;
2925 else
2926 output[0x0A] &= ~0x2;
2927 if (eeprom->suspend_pull_downs)
2928 output[0x0A] |= 0x4;
2929 else
2930 output[0x0A] &= ~0x4;
2931 if (eeprom->use_usb_version)
2932 output[0x0A] |= USE_USB_VERSION_BIT;
2933 else
2934 output[0x0A] &= ~USE_USB_VERSION_BIT;
2935
2936 output[0x0C] = eeprom->usb_version & 0xff;
2937 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2938 output[0x14] = eeprom->chip;
2939 break;
2940 case TYPE_R:
2941 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2942 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2943 if (eeprom->external_oscillator)
2944 output[0x00] |= 0x02;
2945 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2946
2947 if (eeprom->suspend_pull_downs)
2948 output[0x0A] |= 0x4;
2949 else
2950 output[0x0A] &= ~0x4;
2951 output[0x0B] = eeprom->invert;
2952 output[0x0C] = eeprom->usb_version & 0xff;
2953 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2954
2955 if (eeprom->cbus_function[0] > CBUS_BB_RD)
2956 output[0x14] = CBUS_TXLED;
2957 else
2958 output[0x14] = eeprom->cbus_function[0];
2959
2960 if (eeprom->cbus_function[1] > CBUS_BB_RD)
2961 output[0x14] |= CBUS_RXLED<<4;
2962 else
2963 output[0x14] |= eeprom->cbus_function[1]<<4;
2964
2965 if (eeprom->cbus_function[2] > CBUS_BB_RD)
2966 output[0x15] = CBUS_TXDEN;
2967 else
2968 output[0x15] = eeprom->cbus_function[2];
2969
2970 if (eeprom->cbus_function[3] > CBUS_BB_RD)
2971 output[0x15] |= CBUS_PWREN<<4;
2972 else
2973 output[0x15] |= eeprom->cbus_function[3]<<4;
2974
2975 if (eeprom->cbus_function[4] > CBUS_CLK6)
2976 output[0x16] = CBUS_SLEEP;
2977 else
2978 output[0x16] = eeprom->cbus_function[4];
2979 break;
2980 case TYPE_2232H:
2981 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
2982 if ( eeprom->channel_a_driver == DRIVER_VCP)
2983 output[0x00] |= DRIVER_VCP;
2984 else
2985 output[0x00] &= ~DRIVER_VCP;
2986
2987 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
2988 if ( eeprom->channel_b_driver == DRIVER_VCP)
2989 output[0x01] |= DRIVER_VCP;
2990 else
2991 output[0x01] &= ~DRIVER_VCP;
2992 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2993 output[0x01] |= SUSPEND_DBUS7_BIT;
2994 else
2995 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2996
2997 if (eeprom->suspend_pull_downs)
2998 output[0x0A] |= 0x4;
2999 else
3000 output[0x0A] &= ~0x4;
3001
3002 if (eeprom->group0_drive > DRIVE_16MA)
3003 output[0x0c] |= DRIVE_16MA;
3004 else
3005 output[0x0c] |= eeprom->group0_drive;
3006 if (eeprom->group0_schmitt == IS_SCHMITT)
3007 output[0x0c] |= IS_SCHMITT;
3008 if (eeprom->group0_slew == SLOW_SLEW)
3009 output[0x0c] |= SLOW_SLEW;
3010
3011 if (eeprom->group1_drive > DRIVE_16MA)
3012 output[0x0c] |= DRIVE_16MA<<4;
3013 else
3014 output[0x0c] |= eeprom->group1_drive<<4;
3015 if (eeprom->group1_schmitt == IS_SCHMITT)
3016 output[0x0c] |= IS_SCHMITT<<4;
3017 if (eeprom->group1_slew == SLOW_SLEW)
3018 output[0x0c] |= SLOW_SLEW<<4;
3019
3020 if (eeprom->group2_drive > DRIVE_16MA)
3021 output[0x0d] |= DRIVE_16MA;
3022 else
3023 output[0x0d] |= eeprom->group2_drive;
3024 if (eeprom->group2_schmitt == IS_SCHMITT)
3025 output[0x0d] |= IS_SCHMITT;
3026 if (eeprom->group2_slew == SLOW_SLEW)
3027 output[0x0d] |= SLOW_SLEW;
3028
3029 if (eeprom->group3_drive > DRIVE_16MA)
3030 output[0x0d] |= DRIVE_16MA<<4;
3031 else
3032 output[0x0d] |= eeprom->group3_drive<<4;
3033 if (eeprom->group3_schmitt == IS_SCHMITT)
3034 output[0x0d] |= IS_SCHMITT<<4;
3035 if (eeprom->group3_slew == SLOW_SLEW)
3036 output[0x0d] |= SLOW_SLEW<<4;
3037
3038 output[0x18] = eeprom->chip;
3039
3040 break;
3041 case TYPE_4232H:
3042 if (eeprom->channel_a_driver == DRIVER_VCP)
3043 output[0x00] |= DRIVER_VCP;
3044 else
3045 output[0x00] &= ~DRIVER_VCP;
3046 if (eeprom->channel_b_driver == DRIVER_VCP)
3047 output[0x01] |= DRIVER_VCP;
3048 else
3049 output[0x01] &= ~DRIVER_VCP;
3050 if (eeprom->channel_c_driver == DRIVER_VCP)
3051 output[0x00] |= (DRIVER_VCP << 4);
3052 else
3053 output[0x00] &= ~(DRIVER_VCP << 4);
3054 if (eeprom->channel_d_driver == DRIVER_VCP)
3055 output[0x01] |= (DRIVER_VCP << 4);
3056 else
3057 output[0x01] &= ~(DRIVER_VCP << 4);
3058
3059 if (eeprom->suspend_pull_downs)
3060 output[0x0a] |= 0x4;
3061 else
3062 output[0x0a] &= ~0x4;
3063
3064 if (eeprom->channel_a_rs485enable)
3065 output[0x0b] |= CHANNEL_IS_RS485 << 0;
3066 else
3067 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
3068 if (eeprom->channel_b_rs485enable)
3069 output[0x0b] |= CHANNEL_IS_RS485 << 1;
3070 else
3071 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
3072 if (eeprom->channel_c_rs485enable)
3073 output[0x0b] |= CHANNEL_IS_RS485 << 2;
3074 else
3075 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
3076 if (eeprom->channel_d_rs485enable)
3077 output[0x0b] |= CHANNEL_IS_RS485 << 3;
3078 else
3079 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
3080
3081 if (eeprom->group0_drive > DRIVE_16MA)
3082 output[0x0c] |= DRIVE_16MA;
3083 else
3084 output[0x0c] |= eeprom->group0_drive;
3085 if (eeprom->group0_schmitt == IS_SCHMITT)
3086 output[0x0c] |= IS_SCHMITT;
3087 if (eeprom->group0_slew == SLOW_SLEW)
3088 output[0x0c] |= SLOW_SLEW;
3089
3090 if (eeprom->group1_drive > DRIVE_16MA)
3091 output[0x0c] |= DRIVE_16MA<<4;
3092 else
3093 output[0x0c] |= eeprom->group1_drive<<4;
3094 if (eeprom->group1_schmitt == IS_SCHMITT)
3095 output[0x0c] |= IS_SCHMITT<<4;
3096 if (eeprom->group1_slew == SLOW_SLEW)
3097 output[0x0c] |= SLOW_SLEW<<4;
3098
3099 if (eeprom->group2_drive > DRIVE_16MA)
3100 output[0x0d] |= DRIVE_16MA;
3101 else
3102 output[0x0d] |= eeprom->group2_drive;
3103 if (eeprom->group2_schmitt == IS_SCHMITT)
3104 output[0x0d] |= IS_SCHMITT;
3105 if (eeprom->group2_slew == SLOW_SLEW)
3106 output[0x0d] |= SLOW_SLEW;
3107
3108 if (eeprom->group3_drive > DRIVE_16MA)
3109 output[0x0d] |= DRIVE_16MA<<4;
3110 else
3111 output[0x0d] |= eeprom->group3_drive<<4;
3112 if (eeprom->group3_schmitt == IS_SCHMITT)
3113 output[0x0d] |= IS_SCHMITT<<4;
3114 if (eeprom->group3_slew == SLOW_SLEW)
3115 output[0x0d] |= SLOW_SLEW<<4;
3116
3117 output[0x18] = eeprom->chip;
3118
3119 break;
3120 case TYPE_232H:
3121 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3122 if ( eeprom->channel_a_driver == DRIVER_VCP)
3123 output[0x00] |= DRIVER_VCPH;
3124 else
3125 output[0x00] &= ~DRIVER_VCPH;
3126 if (eeprom->powersave)
3127 output[0x01] |= POWER_SAVE_DISABLE_H;
3128 else
3129 output[0x01] &= ~POWER_SAVE_DISABLE_H;
3130
3131 if (eeprom->suspend_pull_downs)
3132 output[0x0a] |= 0x4;
3133 else
3134 output[0x0a] &= ~0x4;
3135
3136 if (eeprom->clock_polarity)
3137 output[0x01] |= FT1284_CLK_IDLE_STATE;
3138 else
3139 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3140 if (eeprom->data_order)
3141 output[0x01] |= FT1284_DATA_LSB;
3142 else
3143 output[0x01] &= ~FT1284_DATA_LSB;
3144 if (eeprom->flow_control)
3145 output[0x01] |= FT1284_FLOW_CONTROL;
3146 else
3147 output[0x01] &= ~FT1284_FLOW_CONTROL;
3148 if (eeprom->group0_drive > DRIVE_16MA)
3149 output[0x0c] |= DRIVE_16MA;
3150 else
3151 output[0x0c] |= eeprom->group0_drive;
3152 if (eeprom->group0_schmitt == IS_SCHMITT)
3153 output[0x0c] |= IS_SCHMITT;
3154 if (eeprom->group0_slew == SLOW_SLEW)
3155 output[0x0c] |= SLOW_SLEW;
3156
3157 if (eeprom->group1_drive > DRIVE_16MA)
3158 output[0x0d] |= DRIVE_16MA;
3159 else
3160 output[0x0d] |= eeprom->group1_drive;
3161 if (eeprom->group1_schmitt == IS_SCHMITT)
3162 output[0x0d] |= IS_SCHMITT;
3163 if (eeprom->group1_slew == SLOW_SLEW)
3164 output[0x0d] |= SLOW_SLEW;
3165
3166 set_ft232h_cbus(eeprom, output);
3167
3168 output[0x1e] = eeprom->chip;
3169 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3170 break;
3171 case TYPE_230X:
3172 output[0x00] = 0x80; /* Actually, leave the default value */
3173 output[0x0a] = 0x08; /* Enable USB Serial Number */
3174 /*FIXME: Make DBUS & CBUS Control configurable*/
3175 output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3176 for (j = 0; j <= 6; j++)
3177 {
3178 output[0x1a + j] = eeprom->cbus_function[j];
3179 }
3180 output[0x0b] = eeprom->invert;
3181 break;
3182 }
3183
3184 /* First address without use */
3185 free_start = 0;
3186 switch (ftdi->type)
3187 {
3188 case TYPE_230X:
3189 free_start += 2;
3190 case TYPE_232H:
3191 free_start += 6;
3192 case TYPE_2232H:
3193 case TYPE_4232H:
3194 free_start += 2;
3195 case TYPE_R:
3196 free_start += 2;
3197 case TYPE_2232C:
3198 free_start++;
3199 case TYPE_AM:
3200 case TYPE_BM:
3201 free_start += 0x14;
3202 }
3203
3204 /* Arbitrary user data */
3205 if (eeprom->user_data && eeprom->user_data_size >= 0)
3206 {
3207 if (eeprom->user_data_addr < free_start)
3208 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
3209 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
3210 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
3211 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
3212 ftdi_error_return(-1,"eeprom size exceeded");
3213 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
3214 }
3215
3216 // calculate checksum
3217 checksum = 0xAAAA;
3218
3219 for (i = 0; i < eeprom->size/2-1; i++)
3220 {
3221 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3222 {
3223 /* FT230X has a user section in the MTP which is not part of the checksum */
3224 i = 0x40;
3225 }
3226 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3227 uint16_t data;
3228 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3229 fprintf(stderr, "Reading Factory Configuration Data failed\n");
3230 i = 0x50;
3231 }
3232 value = data;
3233 }
3234 else {
3235 value = output[i*2];
3236 value += output[(i*2)+1] << 8;
3237 }
3238 checksum = value^checksum;
3239 checksum = (checksum << 1) | (checksum >> 15);
3240 }
3241
3242 output[eeprom->size-2] = checksum;
3243 output[eeprom->size-1] = checksum >> 8;
3244
3245 eeprom->initialized_for_connected_device = 1;
3246 return user_area_size;
3247 }
3248 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3249 * EEPROM structure
3250 *
3251 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3252 */
3253 static unsigned char bit2type(unsigned char bits)
3254 {
3255 switch (bits)
3256 {
3257 case 0: return CHANNEL_IS_UART;
3258 case 1: return CHANNEL_IS_FIFO;
3259 case 2: return CHANNEL_IS_OPTO;
3260 case 4: return CHANNEL_IS_CPU;
3261 case 8: return CHANNEL_IS_FT1284;
3262 default:
3263 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3264 bits);
3265 }
3266 return 0;
3267 }
3268 /* Decode 230X / 232R type chips invert bits
3269 * Prints directly to stdout.
3270 */
3271 static void print_inverted_bits(int invert)
3272 {
3273 char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3274 int i;
3275
3276 fprintf(stdout,"Inverted bits:");
3277 for (i=0; i<8; i++)
3278 if ((invert & (1<<i)) == (1<<i))
3279 fprintf(stdout," %s",r_bits[i]);
3280
3281 fprintf(stdout,"\n");
3282 }
3297 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3298 {
3299 int i, j;
3300 unsigned short checksum, eeprom_checksum, value;
3301 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3302 int eeprom_size;
3303 struct ftdi_eeprom *eeprom;
3304 unsigned char *buf = NULL;
3305
3306 if (ftdi == NULL)
3307 ftdi_error_return(-1,"No context");
3308 if (ftdi->eeprom == NULL)
3309 ftdi_error_return(-1,"No eeprom structure");
3310
3311 eeprom = ftdi->eeprom;
3312 eeprom_size = eeprom->size;
3313 buf = ftdi->eeprom->buf;
3314
3315 // Addr 02: Vendor ID
3316 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3317
3318 // Addr 04: Product ID
3319 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3320
3321 // Addr 06: Device release number
3322 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3323
3324 // Addr 08: Config descriptor
3325 // Bit 7: always 1
3326 // Bit 6: 1 if this device is self powered, 0 if bus powered
3327 // Bit 5: 1 if this device uses remote wakeup
3328 eeprom->self_powered = buf[0x08] & 0x40;
3329 eeprom->remote_wakeup = buf[0x08] & 0x20;
3330
3331 // Addr 09: Max power consumption: max power = value * 2 mA
3332 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3333
3334 // Addr 0A: Chip configuration
3335 // Bit 7: 0 - reserved
3336 // Bit 6: 0 - reserved
3337 // Bit 5: 0 - reserved
3338 // Bit 4: 1 - Change USB version on BM and 2232C
3339 // Bit 3: 1 - Use the serial number string
3340 // Bit 2: 1 - Enable suspend pull downs for lower power
3341 // Bit 1: 1 - Out EndPoint is Isochronous
3342 // Bit 0: 1 - In EndPoint is Isochronous
3343 //
3344 eeprom->in_is_isochronous = buf[0x0A]&0x01;
3345 eeprom->out_is_isochronous = buf[0x0A]&0x02;
3346 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3347 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
3348 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
3349
3350 // Addr 0C: USB version low byte when 0x0A
3351 // Addr 0D: USB version high byte when 0x0A
3352 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3353
3354 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3355 // Addr 0F: Length of manufacturer string
3356 manufacturer_size = buf[0x0F]/2;
3357 if (eeprom->manufacturer)
3358 free(eeprom->manufacturer);
3359 if (manufacturer_size > 0)
3360 {
3361 eeprom->manufacturer = malloc(manufacturer_size);
3362 if (eeprom->manufacturer)
3363 {
3364 // Decode manufacturer
3365 i = buf[0x0E] & (eeprom_size -1); // offset
3366 for (j=0; j<manufacturer_size-1; j++)
3367 {
3368 eeprom->manufacturer[j] = buf[2*j+i+2];
3369 }
3370 eeprom->manufacturer[j] = '\0';
3371 }
3372 }
3373 else eeprom->manufacturer = NULL;
3374
3375 // Addr 10: Offset of the product string + 0x80, calculated later
3376 // Addr 11: Length of product string
3377 if (eeprom->product)
3378 free(eeprom->product);
3379 product_size = buf[0x11]/2;
3380 if (product_size > 0)
3381 {
3382 eeprom->product = malloc(product_size);
3383 if (eeprom->product)
3384 {
3385 // Decode product name
3386 i = buf[0x10] & (eeprom_size -1); // offset
3387 for (j=0; j<product_size-1; j++)
3388 {
3389 eeprom->product[j] = buf[2*j+i+2];
3390 }
3391 eeprom->product[j] = '\0';
3392 }
3393 }
3394 else eeprom->product = NULL;
3395
3396 // Addr 12: Offset of the serial string + 0x80, calculated later
3397 // Addr 13: Length of serial string
3398 if (eeprom->serial)
3399 free(eeprom->serial);
3400 serial_size = buf[0x13]/2;
3401 if (serial_size > 0)
3402 {
3403 eeprom->serial = malloc(serial_size);
3404 if (eeprom->serial)
3405 {
3406 // Decode serial
3407 i = buf[0x12] & (eeprom_size -1); // offset
3408 for (j=0; j<serial_size-1; j++)
3409 {
3410 eeprom->serial[j] = buf[2*j+i+2];
3411 }
3412 eeprom->serial[j] = '\0';
3413 }
3414 }
3415 else eeprom->serial = NULL;
3416
3417 // verify checksum
3418 checksum = 0xAAAA;
3419
3420 for (i = 0; i < eeprom_size/2-1; i++)
3421 {
3422 if ((ftdi->type == TYPE_230X) && (i == 0x12))
3423 {
3424 /* FT230X has a user section in the MTP which is not part of the checksum */
3425 i = 0x40;
3426 }
3427 value = buf[i*2];
3428 value += buf[(i*2)+1] << 8;
3429
3430 checksum = value^checksum;
3431 checksum = (checksum << 1) | (checksum >> 15);
3432 }
3433
3434 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3435
3436 if (eeprom_checksum != checksum)
3437 {
3438 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3439 ftdi_error_return(-1,"EEPROM checksum error");
3440 }
3441
3442 eeprom->channel_a_type = 0;
3443 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3444 {
3445 eeprom->chip = -1;
3446 }
3447 else if (ftdi->type == TYPE_2232C)
3448 {
3449 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3450 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3451 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3452 eeprom->channel_b_type = buf[0x01] & 0x7;
3453 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3454 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3455 eeprom->chip = buf[0x14];
3456 }
3457 else if (ftdi->type == TYPE_R)
3458 {
3459 /* TYPE_R flags D2XX, not VCP as all others*/
3460 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3461 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3462 eeprom->external_oscillator = buf[0x00] & 0x02;
3463 if ( (buf[0x01]&0x40) != 0x40)
3464 fprintf(stderr,
3465 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3466 " If this happened with the\n"
3467 " EEPROM programmed by FTDI tools, please report "
3468 "to libftdi@developer.intra2net.com\n");
3469
3470 eeprom->chip = buf[0x16];
3471 // Addr 0B: Invert data lines
3472 // Works only on FT232R, not FT245R, but no way to distinguish
3473 eeprom->invert = buf[0x0B];
3474 // Addr 14: CBUS function: CBUS0, CBUS1
3475 // Addr 15: CBUS function: CBUS2, CBUS3
3476 // Addr 16: CBUS function: CBUS5
3477 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3478 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3479 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3480 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3481 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3482 }
3483 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3484 {
3485 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3486 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3487
3488 if (ftdi->type == TYPE_2232H)
3489 {
3490 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3491 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3492 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3493 }
3494 else
3495 {
3496 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3497 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3498 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3499 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3500 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3501 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3502 }
3503
3504 eeprom->chip = buf[0x18];
3505 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3506 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3507 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3508 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3509 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3510 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3511 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3512 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3513 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3514 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3515 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3516 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3517 }
3518 else if (ftdi->type == TYPE_232H)
3519 {
3520 eeprom->channel_a_type = buf[0x00] & 0xf;
3521 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3522 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3523 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3524 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3525 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3526 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3527 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3528 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3529 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3530 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3531 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3532
3533 for(i=0; i<5; i++)
3534 {
3535 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3536 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3537 }
3538 eeprom->chip = buf[0x1e];
3539 /*FIXME: Decipher more values*/
3540 }
3541 else if (ftdi->type == TYPE_230X)
3542 {
3543 for(i=0; i<4; i++)
3544 {
3545 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3546 }
3547 eeprom->group0_drive = buf[0x0c] & 0x03;
3548 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3549 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3550 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3551 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3552 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3553
3554 eeprom->invert = buf[0xb];
3555 }
3556
3557 if (verbose)
3558 {
3559 char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3560 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3561 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3562 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3563
3564 if (eeprom->self_powered)
3565 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3566 else
3567 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3568 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3569 if (eeprom->manufacturer)
3570 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3571 if (eeprom->product)
3572 fprintf(stdout, "Product: %s\n",eeprom->product);
3573 if (eeprom->serial)
3574 fprintf(stdout, "Serial: %s\n",eeprom->serial);
3575 fprintf(stdout, "Checksum : %04x\n", checksum);
3576 if (ftdi->type == TYPE_R) {
3577 fprintf(stdout, "Internal EEPROM\n");
3578 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
3579 }
3580 else if (eeprom->chip >= 0x46)
3581 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3582 if (eeprom->suspend_dbus7)
3583 fprintf(stdout, "Suspend on DBUS7\n");
3584 if (eeprom->suspend_pull_downs)
3585 fprintf(stdout, "Pull IO pins low during suspend\n");
3586 if(eeprom->powersave)
3587 {
3588 if(ftdi->type >= TYPE_232H)
3589 fprintf(stdout,"Enter low power state on ACBUS7\n");
3590 }
3591 if (eeprom->remote_wakeup)
3592 fprintf(stdout, "Enable Remote Wake Up\n");
3593 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3594 if (ftdi->type >= TYPE_2232C)
3595 fprintf(stdout,"Channel A has Mode %s%s%s\n",
3596 channel_mode[eeprom->channel_a_type],
3597 (eeprom->channel_a_driver)?" VCP":"",
3598 (eeprom->high_current_a)?" High Current IO":"");
3599 if (ftdi->type == TYPE_232H)
3600 {
3601 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3602 (eeprom->clock_polarity)?"HIGH":"LOW",
3603 (eeprom->data_order)?"LSB":"MSB",
3604 (eeprom->flow_control)?"":"No ");
3605 }
3606 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3607 fprintf(stdout,"Channel B has Mode %s%s%s\n",
3608 channel_mode[eeprom->channel_b_type],
3609 (eeprom->channel_b_driver)?" VCP":"",
3610 (eeprom->high_current_b)?" High Current IO":"");
3611 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3612 eeprom->use_usb_version)
3613 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3614
3615 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3616 {
3617 fprintf(stdout,"%s has %d mA drive%s%s\n",
3618 (ftdi->type == TYPE_2232H)?"AL":"A",
3619 (eeprom->group0_drive+1) *4,
3620 (eeprom->group0_schmitt)?" Schmitt Input":"",
3621 (eeprom->group0_slew)?" Slow Slew":"");
3622 fprintf(stdout,"%s has %d mA drive%s%s\n",
3623 (ftdi->type == TYPE_2232H)?"AH":"B",
3624 (eeprom->group1_drive+1) *4,
3625 (eeprom->group1_schmitt)?" Schmitt Input":"",
3626 (eeprom->group1_slew)?" Slow Slew":"");
3627 fprintf(stdout,"%s has %d mA drive%s%s\n",
3628 (ftdi->type == TYPE_2232H)?"BL":"C",
3629 (eeprom->group2_drive+1) *4,
3630 (eeprom->group2_schmitt)?" Schmitt Input":"",
3631 (eeprom->group2_slew)?" Slow Slew":"");
3632 fprintf(stdout,"%s has %d mA drive%s%s\n",
3633 (ftdi->type == TYPE_2232H)?"BH":"D",
3634 (eeprom->group3_drive+1) *4,
3635 (eeprom->group3_schmitt)?" Schmitt Input":"",
3636 (eeprom->group3_slew)?" Slow Slew":"");
3637 }
3638 else if (ftdi->type == TYPE_232H)
3639 {
3640 char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3641 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3642 "CLK30","CLK15","CLK7_5"
3643 };
3644 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3645 (eeprom->group0_drive+1) *4,
3646 (eeprom->group0_schmitt)?" Schmitt Input":"",
3647 (eeprom->group0_slew)?" Slow Slew":"");
3648 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3649 (eeprom->group1_drive+1) *4,
3650 (eeprom->group1_schmitt)?" Schmitt Input":"",
3651 (eeprom->group1_slew)?" Slow Slew":"");
3652 for (i=0; i<10; i++)
3653 {
3654 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3655 fprintf(stdout,"C%d Function: %s\n", i,
3656 cbush_mux[eeprom->cbus_function[i]]);
3657 }
3658 }
3659 else if (ftdi->type == TYPE_230X)
3660 {
3661 char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
3662 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3663 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3664 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3665 "BBRD#", "TIME_STAMP", "AWAKE#",
3666 };
3667 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3668 (eeprom->group0_drive+1) *4,
3669 (eeprom->group0_schmitt)?" Schmitt Input":"",
3670 (eeprom->group0_slew)?" Slow Slew":"");
3671 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3672 (eeprom->group1_drive+1) *4,
3673 (eeprom->group1_schmitt)?" Schmitt Input":"",
3674 (eeprom->group1_slew)?" Slow Slew":"");
3675 for (i=0; i<4; i++)
3676 {
3677 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
3678 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
3679 }
3680
3681 if (eeprom->invert)
3682 print_inverted_bits(eeprom->invert);
3683 }
3684
3685 if (ftdi->type == TYPE_R)
3686 {
3687 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3688 "SLEEP","CLK48","CLK24","CLK12","CLK6",
3689 "IOMODE","BB_WR","BB_RD"
3690 };
3691 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3692
3693 if (eeprom->invert)
3694 print_inverted_bits(eeprom->invert);
3695
3696 for (i=0; i<5; i++)
3697 {
3698 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
3699 fprintf(stdout,"C%d Function: %s\n", i,
3700 cbus_mux[eeprom->cbus_function[i]]);
3701 else
3702 {
3703 if (i < 4)
3704 /* Running MPROG show that C0..3 have fixed function Synchronous
3705 Bit Bang mode */
3706 fprintf(stdout,"C%d BB Function: %s\n", i,
3707 cbus_BB[i]);
3708 else
3709 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3710 }
3711 }
3712 }
3713 }
3714 return 0;
3715 }
3716
3727 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3728 {
3729 switch (value_name)
3730 {
3731 case VENDOR_ID:
3732 *value = ftdi->eeprom->vendor_id;
3733 break;
3734 case PRODUCT_ID:
3735 *value = ftdi->eeprom->product_id;
3736 break;
3737 case RELEASE_NUMBER:
3738 *value = ftdi->eeprom->release_number;
3739 break;
3740 case SELF_POWERED:
3741 *value = ftdi->eeprom->self_powered;
3742 break;
3743 case REMOTE_WAKEUP:
3744 *value = ftdi->eeprom->remote_wakeup;
3745 break;
3746 case IS_NOT_PNP:
3747 *value = ftdi->eeprom->is_not_pnp;
3748 break;
3749 case SUSPEND_DBUS7:
3750 *value = ftdi->eeprom->suspend_dbus7;
3751 break;
3752 case IN_IS_ISOCHRONOUS:
3753 *value = ftdi->eeprom->in_is_isochronous;
3754 break;
3755 case OUT_IS_ISOCHRONOUS:
3756 *value = ftdi->eeprom->out_is_isochronous;
3757 break;
3758 case SUSPEND_PULL_DOWNS:
3759 *value = ftdi->eeprom->suspend_pull_downs;
3760 break;
3761 case USE_SERIAL:
3762 *value = ftdi->eeprom->use_serial;
3763 break;
3764 case USB_VERSION:
3765 *value = ftdi->eeprom->usb_version;
3766 break;
3767 case USE_USB_VERSION:
3768 *value = ftdi->eeprom->use_usb_version;
3769 break;
3770 case MAX_POWER:
3771 *value = ftdi->eeprom->max_power;
3772 break;
3773 case CHANNEL_A_TYPE:
3774 *value = ftdi->eeprom->channel_a_type;
3775 break;
3776 case CHANNEL_B_TYPE:
3777 *value = ftdi->eeprom->channel_b_type;
3778 break;
3779 case CHANNEL_A_DRIVER:
3780 *value = ftdi->eeprom->channel_a_driver;
3781 break;
3782 case CHANNEL_B_DRIVER:
3783 *value = ftdi->eeprom->channel_b_driver;
3784 break;
3785 case CHANNEL_C_DRIVER:
3786 *value = ftdi->eeprom->channel_c_driver;
3787 break;
3788 case CHANNEL_D_DRIVER:
3789 *value = ftdi->eeprom->channel_d_driver;
3790 break;
3791 case CHANNEL_A_RS485:
3792 *value = ftdi->eeprom->channel_a_rs485enable;
3793 break;
3794 case CHANNEL_B_RS485:
3795 *value = ftdi->eeprom->channel_b_rs485enable;
3796 break;
3797 case CHANNEL_C_RS485:
3798 *value = ftdi->eeprom->channel_c_rs485enable;
3799 break;
3800 case CHANNEL_D_RS485:
3801 *value = ftdi->eeprom->channel_d_rs485enable;
3802 break;
3803 case CBUS_FUNCTION_0:
3804 *value = ftdi->eeprom->cbus_function[0];
3805 break;
3806 case CBUS_FUNCTION_1:
3807 *value = ftdi->eeprom->cbus_function[1];
3808 break;
3809 case CBUS_FUNCTION_2:
3810 *value = ftdi->eeprom->cbus_function[2];
3811 break;
3812 case CBUS_FUNCTION_3:
3813 *value = ftdi->eeprom->cbus_function[3];
3814 break;
3815 case CBUS_FUNCTION_4:
3816 *value = ftdi->eeprom->cbus_function[4];
3817 break;
3818 case CBUS_FUNCTION_5:
3819 *value = ftdi->eeprom->cbus_function[5];
3820 break;
3821 case CBUS_FUNCTION_6:
3822 *value = ftdi->eeprom->cbus_function[6];
3823 break;
3824 case CBUS_FUNCTION_7:
3825 *value = ftdi->eeprom->cbus_function[7];
3826 break;
3827 case CBUS_FUNCTION_8:
3828 *value = ftdi->eeprom->cbus_function[8];
3829 break;
3830 case CBUS_FUNCTION_9:
3831 *value = ftdi->eeprom->cbus_function[9];
3832 break;
3833 case HIGH_CURRENT:
3834 *value = ftdi->eeprom->high_current;
3835 break;
3836 case HIGH_CURRENT_A:
3837 *value = ftdi->eeprom->high_current_a;
3838 break;
3839 case HIGH_CURRENT_B:
3840 *value = ftdi->eeprom->high_current_b;
3841 break;
3842 case INVERT:
3843 *value = ftdi->eeprom->invert;
3844 break;
3845 case GROUP0_DRIVE:
3846 *value = ftdi->eeprom->group0_drive;
3847 break;
3848 case GROUP0_SCHMITT:
3849 *value = ftdi->eeprom->group0_schmitt;
3850 break;
3851 case GROUP0_SLEW:
3852 *value = ftdi->eeprom->group0_slew;
3853 break;
3854 case GROUP1_DRIVE:
3855 *value = ftdi->eeprom->group1_drive;
3856 break;
3857 case GROUP1_SCHMITT:
3858 *value = ftdi->eeprom->group1_schmitt;
3859 break;
3860 case GROUP1_SLEW:
3861 *value = ftdi->eeprom->group1_slew;
3862 break;
3863 case GROUP2_DRIVE:
3864 *value = ftdi->eeprom->group2_drive;
3865 break;
3866 case GROUP2_SCHMITT:
3867 *value = ftdi->eeprom->group2_schmitt;
3868 break;
3869 case GROUP2_SLEW:
3870 *value = ftdi->eeprom->group2_slew;
3871 break;
3872 case GROUP3_DRIVE:
3873 *value = ftdi->eeprom->group3_drive;
3874 break;
3875 case GROUP3_SCHMITT:
3876 *value = ftdi->eeprom->group3_schmitt;
3877 break;
3878 case GROUP3_SLEW:
3879 *value = ftdi->eeprom->group3_slew;
3880 break;
3881 case POWER_SAVE:
3882 *value = ftdi->eeprom->powersave;
3883 break;
3884 case CLOCK_POLARITY:
3885 *value = ftdi->eeprom->clock_polarity;
3886 break;
3887 case DATA_ORDER:
3888 *value = ftdi->eeprom->data_order;
3889 break;
3890 case FLOW_CONTROL:
3891 *value = ftdi->eeprom->flow_control;
3892 break;
3893 case CHIP_TYPE:
3894 *value = ftdi->eeprom->chip;
3895 break;
3896 case CHIP_SIZE:
3897 *value = ftdi->eeprom->size;
3898 break;
3899 case EXTERNAL_OSCILLATOR:
3900 *value = ftdi->eeprom->external_oscillator;
3901 break;
3902 default:
3903 ftdi_error_return(-1, "Request for unknown EEPROM value");
3904 }
3905 return 0;
3906 }
3907
3920 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3921 {
3922 switch (value_name)
3923 {
3924 case VENDOR_ID:
3925 ftdi->eeprom->vendor_id = value;
3926 break;
3927 case PRODUCT_ID:
3928 ftdi->eeprom->product_id = value;
3929 break;
3930 case RELEASE_NUMBER:
3931 ftdi->eeprom->release_number = value;
3932 break;
3933 case SELF_POWERED:
3934 ftdi->eeprom->self_powered = value;
3935 break;
3936 case REMOTE_WAKEUP:
3937 ftdi->eeprom->remote_wakeup = value;
3938 break;
3939 case IS_NOT_PNP:
3940 ftdi->eeprom->is_not_pnp = value;
3941 break;
3942 case SUSPEND_DBUS7:
3943 ftdi->eeprom->suspend_dbus7 = value;
3944 break;
3945 case IN_IS_ISOCHRONOUS:
3946 ftdi->eeprom->in_is_isochronous = value;
3947 break;
3948 case OUT_IS_ISOCHRONOUS:
3949 ftdi->eeprom->out_is_isochronous = value;
3950 break;
3951 case SUSPEND_PULL_DOWNS:
3952 ftdi->eeprom->suspend_pull_downs = value;
3953 break;
3954 case USE_SERIAL:
3955 ftdi->eeprom->use_serial = value;
3956 break;
3957 case USB_VERSION:
3958 ftdi->eeprom->usb_version = value;
3959 break;
3960 case USE_USB_VERSION:
3961 ftdi->eeprom->use_usb_version = value;
3962 break;
3963 case MAX_POWER:
3964 ftdi->eeprom->max_power = value;
3965 break;
3966 case CHANNEL_A_TYPE:
3967 ftdi->eeprom->channel_a_type = value;
3968 break;
3969 case CHANNEL_B_TYPE:
3970 ftdi->eeprom->channel_b_type = value;
3971 break;
3972 case CHANNEL_A_DRIVER:
3973 ftdi->eeprom->channel_a_driver = value;
3974 break;
3975 case CHANNEL_B_DRIVER:
3976 ftdi->eeprom->channel_b_driver = value;
3977 break;
3978 case CHANNEL_C_DRIVER:
3979 ftdi->eeprom->channel_c_driver = value;
3980 break;
3981 case CHANNEL_D_DRIVER:
3982 ftdi->eeprom->channel_d_driver = value;
3983 break;
3984 case CHANNEL_A_RS485:
3985 ftdi->eeprom->channel_a_rs485enable = value;
3986 break;
3987 case CHANNEL_B_RS485:
3988 ftdi->eeprom->channel_b_rs485enable = value;
3989 break;
3990 case CHANNEL_C_RS485:
3991 ftdi->eeprom->channel_c_rs485enable = value;
3992 break;
3993 case CHANNEL_D_RS485:
3994 ftdi->eeprom->channel_d_rs485enable = value;
3995 break;
3996 case CBUS_FUNCTION_0:
3997 ftdi->eeprom->cbus_function[0] = value;
3998 break;
3999 case CBUS_FUNCTION_1:
4000 ftdi->eeprom->cbus_function[1] = value;
4001 break;
4002 case CBUS_FUNCTION_2:
4003 ftdi->eeprom->cbus_function[2] = value;
4004 break;
4005 case CBUS_FUNCTION_3:
4006 ftdi->eeprom->cbus_function[3] = value;
4007 break;
4008 case CBUS_FUNCTION_4:
4009 ftdi->eeprom->cbus_function[4] = value;
4010 break;
4011 case CBUS_FUNCTION_5:
4012 ftdi->eeprom->cbus_function[5] = value;
4013 break;
4014 case CBUS_FUNCTION_6:
4015 ftdi->eeprom->cbus_function[6] = value;
4016 break;
4017 case CBUS_FUNCTION_7:
4018 ftdi->eeprom->cbus_function[7] = value;
4019 break;
4020 case CBUS_FUNCTION_8:
4021 ftdi->eeprom->cbus_function[8] = value;
4022 break;
4023 case CBUS_FUNCTION_9:
4024 ftdi->eeprom->cbus_function[9] = value;
4025 break;
4026 case HIGH_CURRENT:
4027 ftdi->eeprom->high_current = value;
4028 break;
4029 case HIGH_CURRENT_A:
4030 ftdi->eeprom->high_current_a = value;
4031 break;
4032 case HIGH_CURRENT_B:
4033 ftdi->eeprom->high_current_b = value;
4034 break;
4035 case INVERT:
4036 ftdi->eeprom->invert = value;
4037 break;
4038 case GROUP0_DRIVE:
4039 ftdi->eeprom->group0_drive = value;
4040 break;
4041 case GROUP0_SCHMITT:
4042 ftdi->eeprom->group0_schmitt = value;
4043 break;
4044 case GROUP0_SLEW:
4045 ftdi->eeprom->group0_slew = value;
4046 break;
4047 case GROUP1_DRIVE:
4048 ftdi->eeprom->group1_drive = value;
4049 break;
4050 case GROUP1_SCHMITT:
4051 ftdi->eeprom->group1_schmitt = value;
4052 break;
4053 case GROUP1_SLEW:
4054 ftdi->eeprom->group1_slew = value;
4055 break;
4056 case GROUP2_DRIVE:
4057 ftdi->eeprom->group2_drive = value;
4058 break;
4059 case GROUP2_SCHMITT:
4060 ftdi->eeprom->group2_schmitt = value;
4061 break;
4062 case GROUP2_SLEW:
4063 ftdi->eeprom->group2_slew = value;
4064 break;
4065 case GROUP3_DRIVE:
4066 ftdi->eeprom->group3_drive = value;
4067 break;
4068 case GROUP3_SCHMITT:
4069 ftdi->eeprom->group3_schmitt = value;
4070 break;
4071 case GROUP3_SLEW:
4072 ftdi->eeprom->group3_slew = value;
4073 break;
4074 case CHIP_TYPE:
4075 ftdi->eeprom->chip = value;
4076 break;
4077 case POWER_SAVE:
4078 ftdi->eeprom->powersave = value;
4079 break;
4080 case CLOCK_POLARITY:
4081 ftdi->eeprom->clock_polarity = value;
4082 break;
4083 case DATA_ORDER:
4084 ftdi->eeprom->data_order = value;
4085 break;
4086 case FLOW_CONTROL:
4087 ftdi->eeprom->flow_control = value;
4088 break;
4089 case CHIP_SIZE:
4090 ftdi_error_return(-2, "EEPROM Value can't be changed");
4091 break;
4092 case EXTERNAL_OSCILLATOR:
4093 ftdi->eeprom->external_oscillator = value;
4094 break;
4095 case USER_DATA_ADDR:
4096 ftdi->eeprom->user_data_addr = value;
4097 break;
4098
4099 default :
4100 ftdi_error_return(-1, "Request to unknown EEPROM value");
4101 }
4102 ftdi->eeprom->initialized_for_connected_device = 0;
4103 return 0;
4104 }
4105
4116 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
4117 {
4118 if (!ftdi || !(ftdi->eeprom))
4119 ftdi_error_return(-1, "No appropriate structure");
4120
4121 if (!buf || size < ftdi->eeprom->size)
4122 ftdi_error_return(-1, "Not enough room to store eeprom");
4123
4124 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4125 if (size > FTDI_MAX_EEPROM_SIZE)
4126 size = FTDI_MAX_EEPROM_SIZE;
4127
4128 memcpy(buf, ftdi->eeprom->buf, size);
4129
4130 return 0;
4131 }
4132
4142 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
4143 {
4144 if (!ftdi || !(ftdi->eeprom) || !buf)
4145 ftdi_error_return(-1, "No appropriate structure");
4146
4147 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
4148 if (size > FTDI_MAX_EEPROM_SIZE)
4149 size = FTDI_MAX_EEPROM_SIZE;
4150
4151 memcpy(ftdi->eeprom->buf, buf, size);
4152
4153 return 0;
4154 }
4155
4165 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
4166 {
4167 if (!ftdi || !(ftdi->eeprom) || !buf)
4168 ftdi_error_return(-1, "No appropriate structure");
4169
4170 ftdi->eeprom->user_data_size = size;
4171 ftdi->eeprom->user_data = buf;
4172 return 0;
4173 }
4174
4186 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4187 {
4188 if (ftdi == NULL || ftdi->usb_dev == NULL)
4189 ftdi_error_return(-2, "USB device unavailable");
4190
4191 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
4192 ftdi_error_return(-1, "reading eeprom failed");
4193
4194 return 0;
4195 }
4196
4206 int ftdi_read_eeprom(struct ftdi_context *ftdi)
4207 {
4208 int i;
4209 unsigned char *buf;
4210
4211 if (ftdi == NULL || ftdi->usb_dev == NULL)
4212 ftdi_error_return(-2, "USB device unavailable");
4213 buf = ftdi->eeprom->buf;
4214
4215 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4216 {
4217 if (libusb_control_transfer(
4218 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
4219 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4220 ftdi_error_return(-1, "reading eeprom failed");
4221 }
4222
4223 if (ftdi->type == TYPE_R)
4224 ftdi->eeprom->size = 0x80;
4225 /* Guesses size of eeprom by comparing halves
4226 - will not work with blank eeprom */
4227 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4228 ftdi->eeprom->size = -1;
4229 else if (memcmp(buf,&buf[0x80],0x80) == 0)
4230 ftdi->eeprom->size = 0x80;
4231 else if (memcmp(buf,&buf[0x40],0x40) == 0)
4232 ftdi->eeprom->size = 0x40;
4233 else
4234 ftdi->eeprom->size = 0x100;
4235 return 0;
4236 }
4237
4238 /*
4239 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4240 Function is only used internally
4241 \internal
4242 */
4243 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4244 {
4245 return ((value & 1) << 1) |
4246 ((value & 2) << 5) |
4247 ((value & 4) >> 2) |
4248 ((value & 8) << 4) |
4249 ((value & 16) >> 1) |
4250 ((value & 32) >> 1) |
4251 ((value & 64) >> 4) |
4252 ((value & 128) >> 2);
4253 }
4254
4265 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4266 {
4267 unsigned int a = 0, b = 0;
4268
4269 if (ftdi == NULL || ftdi->usb_dev == NULL)
4270 ftdi_error_return(-2, "USB device unavailable");
4271
4272 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)
4273 {
4274 a = a << 8 | a >> 8;
4275 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)
4276 {
4277 b = b << 8 | b >> 8;
4278 a = (a << 16) | (b & 0xFFFF);
4279 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4280 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4281 *chipid = a ^ 0xa5f0f7d1;
4282 return 0;
4283 }
4284 }
4285
4286 ftdi_error_return(-1, "read of FTDIChip-ID failed");
4287 }
4288
4303 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4304 unsigned short eeprom_val)
4305 {
4306 int chip_type_location;
4307 unsigned short chip_type;
4308
4309 if (ftdi == NULL || ftdi->usb_dev == NULL)
4310 ftdi_error_return(-2, "USB device unavailable");
4311
4312 if (eeprom_addr <0x80)
4313 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4314
4315
4316 switch (ftdi->type)
4317 {
4318 case TYPE_BM:
4319 case TYPE_2232C:
4320 chip_type_location = 0x14;
4321 break;
4322 case TYPE_2232H:
4323 case TYPE_4232H:
4324 chip_type_location = 0x18;
4325 break;
4326 case TYPE_232H:
4327 chip_type_location = 0x1e;
4328 break;
4329 default:
4330 ftdi_error_return(-4, "Device can't access unprotected area");
4331 }
4332
4333 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4334 ftdi_error_return(-5, "Reading failed");
4335 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4336 if ((chip_type & 0xff) != 0x66)
4337 {
4338 ftdi_error_return(-6, "EEPROM is not of 93x66");
4339 }
4340
4341 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4342 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4343 NULL, 0, ftdi->usb_write_timeout) != 0)
4344 ftdi_error_return(-1, "unable to write eeprom");
4345
4346 return 0;
4347 }
4348
4359 int ftdi_write_eeprom(struct ftdi_context *ftdi)
4360 {
4361 unsigned short usb_val, status;
4362 int i, ret;
4363 unsigned char *eeprom;
4364
4365 if (ftdi == NULL || ftdi->usb_dev == NULL)
4366 ftdi_error_return(-2, "USB device unavailable");
4367
4368 if(ftdi->eeprom->initialized_for_connected_device == 0)
4369 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4370
4371 eeprom = ftdi->eeprom->buf;
4372
4373 /* These commands were traced while running MProg */
4374 if ((ret = ftdi_usb_reset(ftdi)) != 0)
4375 return ret;
4376 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4377 return ret;
4378 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4379 return ret;
4380
4381 for (i = 0; i < ftdi->eeprom->size/2; i++)
4382 {
4383 /* Do not try to write to reserved area */
4384 if ((ftdi->type == TYPE_230X) && (i == 0x40))
4385 {
4386 i = 0x50;
4387 }
4388 usb_val = eeprom[i*2];
4389 usb_val += eeprom[(i*2)+1] << 8;
4390 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4391 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4392 NULL, 0, ftdi->usb_write_timeout) < 0)
4393 ftdi_error_return(-1, "unable to write eeprom");
4394 }
4395
4396 return 0;
4397 }
4398
4413 #define MAGIC 0x55aa
4414 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
4415 {
4416 unsigned short eeprom_value;
4417 if (ftdi == NULL || ftdi->usb_dev == NULL)
4418 ftdi_error_return(-2, "USB device unavailable");
4419
4420 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4421 {
4422 ftdi->eeprom->chip = 0;
4423 return 0;
4424 }
4425
4426 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4427 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4428 ftdi_error_return(-1, "unable to erase eeprom");
4429
4430
4431 /* detect chip type by writing 0x55AA as magic at word position 0xc0
4432 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4433 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4434 Chip is 93x66 if magic is only read at word position 0xc0*/
4435 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4436 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
4437 NULL, 0, ftdi->usb_write_timeout) != 0)
4438 ftdi_error_return(-3, "Writing magic failed");
4439 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4440 ftdi_error_return(-4, "Reading failed");
4441 if (eeprom_value == MAGIC)
4442 {
4443 ftdi->eeprom->chip = 0x46;
4444 }
4445 else
4446 {
4447 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4448 ftdi_error_return(-4, "Reading failed");
4449 if (eeprom_value == MAGIC)
4450 ftdi->eeprom->chip = 0x56;
4451 else
4452 {
4453 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4454 ftdi_error_return(-4, "Reading failed");
4455 if (eeprom_value == MAGIC)
4456 ftdi->eeprom->chip = 0x66;
4457 else
4458 {
4459 ftdi->eeprom->chip = -1;
4460 }
4461 }
4462 }
4463 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4464 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4465 ftdi_error_return(-1, "unable to erase eeprom");
4466 return 0;
4467 }
4468
4476 char *ftdi_get_error_string (struct ftdi_context *ftdi)
4477 {
4478 if (ftdi == NULL)
4479 return "";
4480
4481 return ftdi->error_str;
4482 }
4483
4484 /* @} end of doxygen libftdi group */
convert_baudrate_UT_export
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:1282
CBUSX_TXDEN
Definition: ftdi.h:372
CHANNEL_IS_FT1284
#define CHANNEL_IS_FT1284
Definition: ftdi.h:400
ftdi_eeprom::powersave
int powersave
Definition: ftdi_i.h:122
ftdi_write_data_submit
struct ftdi_transfer_control * ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1594
ftdi_read_pins
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
Definition: ftdi.c:2090
IN_IS_ISOCHRONOUS
Definition: ftdi.h:285
ftdi_usb_open
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
Definition: ftdi.c:697
FTDI_DEVICE_OUT_REQTYPE
#define FTDI_DEVICE_OUT_REQTYPE
Definition: ftdi.h:152
ftdi_transfer_control::completed
int completed
Definition: ftdi.h:206
ftdi_context::module_detach_mode
enum ftdi_module_detach_mode module_detach_mode
Definition: ftdi.h:271
SIO_SET_LATENCY_TIMER_REQUEST
#define SIO_SET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:164
FT1284_CLK_IDLE_STATE
#define FT1284_CLK_IDLE_STATE
Definition: ftdi.h:349
ftdi_eeprom::out_is_isochronous
int out_is_isochronous
Definition: ftdi_i.h:54
ftdi_chip_type
ftdi_chip_type
Definition: ftdi.h:36
CBUS_FUNCTION_9
Definition: ftdi.h:305
NONE
Definition: ftdi.h:48
CHANNEL_B_TYPE
Definition: ftdi.h:293
POWER_SAVE
Definition: ftdi.h:324
ftdi_device_list
list of usb devices created by ftdi_usb_find_all()
Definition: ftdi.h:342
SLOW_SLEW
#define SLOW_SLEW
Definition: ftdi.h:408
REMOTE_WAKEUP
Definition: ftdi.h:282
CBUSX_TXLED
Definition: ftdi.h:371
STOP_BIT_1
Definition: ftdi.h:50
ftdi_device_list::next
struct ftdi_device_list * next
Definition: ftdi.h:345
CBUS_FUNCTION_6
Definition: ftdi.h:302
ftdi_version_info::minor
int minor
Definition: ftdi.h:456
ftdi_eeprom_set_strings
int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
Definition: ftdi.c:2550
ftdi_eeprom::usb_version
int usb_version
Definition: ftdi_i.h:61
ftdi_read_data_get_chunksize
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:2020
DATA_ORDER
Definition: ftdi.h:326
ftdi_transfer_data_cancel
void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc, struct timeval *to)
Definition: ftdi.c:1782
ftdi_list_free2
void ftdi_list_free2(struct ftdi_device_list *devlist)
Definition: ftdi.c:378
GROUP0_SCHMITT
Definition: ftdi.h:311
ftdi_usb_purge_tx_buffer
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:996
ftdi_eeprom::external_oscillator
int external_oscillator
Definition: ftdi_i.h:102
SIO_SET_EVENT_CHAR_REQUEST
#define SIO_SET_EVENT_CHAR_REQUEST
Definition: ftdi.h:162
ftdi_read_eeprom_location
int ftdi_read_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
Definition: ftdi.c:4186
ftdi_eeprom::clock_polarity
int clock_polarity
Definition: ftdi_i.h:124
ftdi_deinit
void ftdi_deinit(struct ftdi_context *ftdi)
Definition: ftdi.c:210
USER_DATA_ADDR
Definition: ftdi.h:336
HIGH_CURRENT_DRIVE
#define HIGH_CURRENT_DRIVE
Definition: ftdi.h:420
ftdi_set_usbdev
void ftdi_set_usbdev(struct ftdi_context *ftdi, libusb_device_handle *usb)
Definition: ftdi.c:268
USE_USB_VERSION
Definition: ftdi.h:290
ftdi_eeprom::remote_wakeup
int remote_wakeup
Definition: ftdi_i.h:44
SIO_READ_PINS_REQUEST
#define SIO_READ_PINS_REQUEST
Definition: ftdi.h:167
USE_SERIAL
Definition: ftdi.h:288
SIO_RESET_REQUEST
#define SIO_RESET_REQUEST
Definition: ftdi.h:156
ftdi_eeprom_value
ftdi_eeprom_value
Definition: ftdi.h:277
SUSPEND_DBUS7
Definition: ftdi.h:284
ftdi_interface
ftdi_interface
Definition: ftdi.h:72
GROUP2_SLEW
Definition: ftdi.h:318
GROUP1_DRIVE
Definition: ftdi.h:313
ftdi_usb_open_desc_index
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:753
SUSPEND_DBUS7_BIT
#define SUSPEND_DBUS7_BIT
Definition: ftdi.h:417
BREAK_ON
Definition: ftdi.h:54
set_ft232h_cbus
void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char *output)
Definition: ftdi.c:2600
SIO_SET_BAUDRATE_REQUEST
#define SIO_SET_BAUDRATE_REQUEST
Definition: ftdi.h:157
ftdi_transfer_control::transfer
struct libusb_transfer * transfer
Definition: ftdi.h:211
DRIVE_16MA
#define DRIVE_16MA
Definition: ftdi.h:407
ftdi_eeprom::channel_b_driver
int channel_b_driver
Definition: ftdi_i.h:81
ftdi_set_baudrate
int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
Definition: ftdi.c:1299
TYPE_2232H
Definition: ftdi.h:42
SIO_SET_DATA_REQUEST
#define SIO_SET_DATA_REQUEST
Definition: ftdi.h:158
MAX_POWER
Definition: ftdi.h:291
ftdi_write_data
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:1434
CBUS_FUNCTION_0
Definition: ftdi.h:296
CBUS_TXLED
Definition: ftdi.h:357
CBUS_FUNCTION_8
Definition: ftdi.h:304
GROUP2_DRIVE
Definition: ftdi.h:316
SIO_WRITE_EEPROM_REQUEST
#define SIO_WRITE_EEPROM_REQUEST
Definition: ftdi.h:169
ftdi_eeprom::group3_drive
int group3_drive
Definition: ftdi_i.h:118
ftdi_context
Main context structure for all libftdi functions.
Definition: ftdi.h:219
PRODUCT_ID
Definition: ftdi.h:280
GROUP0_DRIVE
Definition: ftdi.h:310
CBUSX_SLEEP
Definition: ftdi.h:372
ftdi_eeprom::channel_c_driver
int channel_c_driver
Definition: ftdi_i.h:82
ftdi_version_info::version_str
const char * version_str
Definition: ftdi.h:458
ftdi_eeprom::group3_schmitt
int group3_schmitt
Definition: ftdi_i.h:119
ftdi_set_event_char
int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable)
Definition: ftdi.c:2343
DRIVER_VCP
#define DRIVER_VCP
Definition: ftdi.h:412
CBUS_BB_RD
Definition: ftdi.h:359
ftdi_eeprom::max_power
int max_power
Definition: ftdi_i.h:65
HIGH_CURRENT_B
Definition: ftdi.h:308
TYPE_R
Definition: ftdi.h:41
MAX_POWER_MILLIAMP_PER_UNIT
#define MAX_POWER_MILLIAMP_PER_UNIT
Definition: ftdi_i.h:25
ftdi_eeprom::group1_schmitt
int group1_schmitt
Definition: ftdi_i.h:113
ftdi_eeprom::user_data_size
int user_data_size
Definition: ftdi_i.h:130
USB_VERSION
Definition: ftdi.h:289
ftdi_set_bitmode
int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
Definition: ftdi.c:2041
ftdi_list_free
void ftdi_list_free(struct ftdi_device_list **devlist)
Definition: ftdi.c:358
ftdi_context::type
enum ftdi_chip_type type
Definition: ftdi.h:233
ftdi_eeprom::serial
char * serial
Definition: ftdi_i.h:72
CHANNEL_IS_UART
#define CHANNEL_IS_UART
Definition: ftdi.h:396
FTDI_DEVICE_IN_REQTYPE
#define FTDI_DEVICE_IN_REQTYPE
Definition: ftdi.h:153
ftdi_set_line_property
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:1345
ftdi_eeprom::channel_b_rs485enable
int channel_b_rs485enable
Definition: ftdi_i.h:86
ftdi_eeprom::cbus_function
int cbus_function[10]
Definition: ftdi_i.h:92
INTERFACE_D
Definition: ftdi.h:78
ftdi_usb_purge_rx_buffer
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:970
DRIVER_VCPH
#define DRIVER_VCPH
Definition: ftdi.h:413
ftdi_new
struct ftdi_context * ftdi_new(void)
Definition: ftdi.c:128
GROUP3_SLEW
Definition: ftdi.h:321
CHANNEL_C_DRIVER
Definition: ftdi.h:328
ftdi_version_info::major
int major
Definition: ftdi.h:455
ftdi_erase_eeprom
int ftdi_erase_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4414
TYPE_230X
Definition: ftdi.h:45
CBUS_RXLED
Definition: ftdi.h:357
TYPE_BM
Definition: ftdi.h:39
ftdi_set_eeprom_user_data
int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char *buf, int size)
Definition: ftdi.c:4165
ftdi_set_interface
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
Definition: ftdi.c:157
CBUS_FUNCTION_4
Definition: ftdi.h:300
SUSPEND_PULL_DOWNS
Definition: ftdi.h:287
C_CLK
#define C_CLK
ftdi_eeprom_initdefaults
int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
Definition: ftdi.c:2402
CBUSX_RXLED
Definition: ftdi.h:371
ftdi_transfer_data_done
int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
Definition: ftdi.c:1735
ftdi_eeprom::size
int size
Definition: ftdi_i.h:135
ftdi_context::readbuffer_remaining
unsigned int readbuffer_remaining
Definition: ftdi.h:243
ftdi_version_info::micro
int micro
Definition: ftdi.h:457
CHIP_SIZE
Definition: ftdi.h:322
ftdi_eeprom::group0_slew
int group0_slew
Definition: ftdi_i.h:111
ftdi_set_error_char
int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable)
Definition: ftdi.c:2372
GROUP1_SLEW
Definition: ftdi.h:315
USE_SERIAL_NUM
#define USE_SERIAL_NUM
Definition: ftdi.h:354
ftdi_eeprom_decode
int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
Definition: ftdi.c:3297
INTERFACE_A
Definition: ftdi.h:75
ftdi_eeprom::vendor_id
int vendor_id
Definition: ftdi_i.h:33
GROUP3_DRIVE
Definition: ftdi.h:319
HIGH_CURRENT
Definition: ftdi.h:306
ftdi_eeprom::in_is_isochronous
int in_is_isochronous
Definition: ftdi_i.h:52
ftdi_transfer_control::offset
int offset
Definition: ftdi.h:209
ftdi_context::out_ep
int out_ep
Definition: ftdi.h:259
CHANNEL_A_RS485
Definition: ftdi.h:330
CHANNEL_IS_FIFO
#define CHANNEL_IS_FIFO
Definition: ftdi.h:397
CBUS_PWREN
Definition: ftdi.h:357
ftdi_device_list::dev
struct libusb_device * dev
Definition: ftdi.h:347
ftdi_get_eeprom_value
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
Definition: ftdi.c:3727
ftdi_eeprom::chip
int chip
Definition: ftdi_i.h:137
FLOW_CONTROL
Definition: ftdi.h:327
SIO_GET_LATENCY_TIMER_REQUEST
#define SIO_GET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:165
CBUS_TXDEN
Definition: ftdi.h:357
ftdi_eeprom::initialized_for_connected_device
int initialized_for_connected_device
Definition: ftdi_i.h:39
ftdi_bits_type
ftdi_bits_type
Definition: ftdi.h:52
CHANNEL_A_TYPE
Definition: ftdi.h:292
TYPE_232H
Definition: ftdi.h:44
CHANNEL_B_DRIVER
Definition: ftdi.h:295
ftdi_eeprom::high_current_b
int high_current_b
Definition: ftdi_i.h:98
EXTERNAL_OSCILLATOR
Definition: ftdi.h:335
FT1284_FLOW_CONTROL
#define FT1284_FLOW_CONTROL
Definition: ftdi.h:351
ftdi_eeprom::channel_c_rs485enable
int channel_c_rs485enable
Definition: ftdi_i.h:87
ftdi_init
int ftdi_init(struct ftdi_context *ftdi)
Definition: ftdi.c:88
ftdi_context::error_str
char * error_str
Definition: ftdi.h:268
ftdi_eeprom::group0_schmitt
int group0_schmitt
Definition: ftdi_i.h:110
ftdi_eeprom::flow_control
int flow_control
Definition: ftdi_i.h:126
ftdi_context::readbuffer
unsigned char * readbuffer
Definition: ftdi.h:239
USE_USB_VERSION_BIT
#define USE_USB_VERSION_BIT
Definition: ftdi.h:415
ftdi_eeprom::user_data
const char * user_data
Definition: ftdi_i.h:131
ftdi_read_data_submit
struct ftdi_transfer_control * ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1656
ftdi_context::usb_ctx
struct libusb_context * usb_ctx
Definition: ftdi.h:223
ftdi_eeprom::buf
unsigned char buf[FTDI_MAX_EEPROM_SIZE]
Definition: ftdi_i.h:138
ftdi_context::usb_write_timeout
int usb_write_timeout
Definition: ftdi.h:229
ftdi_eeprom::suspend_dbus7
int suspend_dbus7
Definition: ftdi_i.h:49
ftdi_context::writebuffer_chunksize
unsigned int writebuffer_chunksize
Definition: ftdi.h:247
CHANNEL_IS_RS485
#define CHANNEL_IS_RS485
Definition: ftdi.h:402
TYPE_AM
Definition: ftdi.h:38
HIGH_CURRENT_A
Definition: ftdi.h:307
SIO_READ_EEPROM_REQUEST
#define SIO_READ_EEPROM_REQUEST
Definition: ftdi.h:168
ODD
Definition: ftdi.h:48
CBUSH_CLK7_5
Definition: ftdi.h:366
ftdi_poll_modem_status
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
Definition: ftdi.c:2197
ftdi_eeprom::user_data_addr
int user_data_addr
Definition: ftdi_i.h:129
FTDI_MAX_EEPROM_SIZE
#define FTDI_MAX_EEPROM_SIZE
Definition: ftdi_i.h:22
OUT_IS_ISOCHRONOUS
Definition: ftdi.h:286
ftdi_eeprom::group1_drive
int group1_drive
Definition: ftdi_i.h:112
ftdi_context::usb_read_timeout
int usb_read_timeout
Definition: ftdi.h:227
ftdi_context::index
int index
Definition: ftdi.h:255
ftdi_eeprom::channel_d_driver
int channel_d_driver
Definition: ftdi_i.h:83
ftdi_setdtr_rts
int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
Definition: ftdi.c:2307
STOP_BIT_15
Definition: ftdi.h:50
ftdi_context::in_ep
int in_ep
Definition: ftdi.h:258
CBUS_FUNCTION_1
Definition: ftdi.h:297
HIGH_CURRENT_DRIVE_R
#define HIGH_CURRENT_DRIVE_R
Definition: ftdi.h:421
IS_SCHMITT
#define IS_SCHMITT
Definition: ftdi.h:409
CLOCK_POLARITY
Definition: ftdi.h:325
SIO_SET_FLOW_CTRL_REQUEST
#define SIO_SET_FLOW_CTRL_REQUEST
Definition: ftdi.h:159
ftdi_eeprom::high_current_a
int high_current_a
Definition: ftdi_i.h:96
FT1284_DATA_LSB
#define FT1284_DATA_LSB
Definition: ftdi.h:350
CBUS_CLK6
Definition: ftdi.h:358
ftdi_eeprom::manufacturer
char * manufacturer
Definition: ftdi_i.h:68
SIO_SET_BITMODE_REQUEST
#define SIO_SET_BITMODE_REQUEST
Definition: ftdi.h:166
CBUS_FUNCTION_2
Definition: ftdi.h:298
ftdi_set_eeprom_buf
int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:4142
CBUSX_AWAKE
Definition: ftdi.h:375
SIO_SET_ERROR_CHAR_REQUEST
#define SIO_SET_ERROR_CHAR_REQUEST
Definition: ftdi.h:163
ftdi_write_data_set_chunksize
int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:1816
ftdi_eeprom::channel_b_type
int channel_b_type
Definition: ftdi_i.h:78
ftdi_context::bitbang_mode
unsigned char bitbang_mode
Definition: ftdi.h:262
ftdi.h
CHANNEL_IS_CPU
#define CHANNEL_IS_CPU
Definition: ftdi.h:399
ftdi_usb_open_dev
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
Definition: ftdi.c:584
ftdi_transfer_control::ftdi
struct ftdi_context * ftdi
Definition: ftdi.h:210
ftdi_set_eeprom_value
int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
Definition: ftdi.c:3920
VENDOR_ID
Definition: ftdi.h:279
SPACE
Definition: ftdi.h:48
ftdi_eeprom::group0_drive
int group0_drive
Definition: ftdi_i.h:109
ftdi_eeprom::group2_slew
int group2_slew
Definition: ftdi_i.h:117
STOP_BIT_2
Definition: ftdi.h:50
ftdi_eeprom::suspend_pull_downs
int suspend_pull_downs
Definition: ftdi_i.h:56
ftdi_version_info::snapshot_str
const char * snapshot_str
Definition: ftdi.h:459
INTERFACE_ANY
Definition: ftdi.h:74
CBUSH_TRISTATE
Definition: ftdi.h:364
POWER_SAVE_DISABLE_H
#define POWER_SAVE_DISABLE_H
Definition: ftdi.h:352
ftdi_eeprom::product_id
int product_id
Definition: ftdi_i.h:35
INTERFACE_C
Definition: ftdi.h:77
SIO_SET_MODEM_CTRL_REQUEST
#define SIO_SET_MODEM_CTRL_REQUEST
Definition: ftdi.h:160
SIO_POLL_MODEM_STATUS_REQUEST
#define SIO_POLL_MODEM_STATUS_REQUEST
Definition: ftdi.h:161
EVEN
Definition: ftdi.h:48
ftdi_transfer_control::size
int size
Definition: ftdi.h:208
ftdi_setflowctrl
int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
Definition: ftdi.c:2223
CBUS_SLEEP
Definition: ftdi.h:358
ftdi_usb_open_desc
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial)
Definition: ftdi.c:723
INTERFACE_B
Definition: ftdi.h:76
ftdi_eeprom::product
char * product
Definition: ftdi_i.h:70
SELF_POWERED
Definition: ftdi.h:281
ftdi_eeprom::invert
int invert
Definition: ftdi_i.h:100
MARK
Definition: ftdi.h:48
GROUP3_SCHMITT
Definition: ftdi.h:320
ftdi_context::usb_dev
struct libusb_device_handle * usb_dev
Definition: ftdi.h:225
ftdi_context::interface
int interface
Definition: ftdi.h:253
ftdi_break_type
ftdi_break_type
Definition: ftdi.h:54
RELEASE_NUMBER
Definition: ftdi.h:334
TYPE_2232C
Definition: ftdi.h:40
CHANNEL_D_DRIVER
Definition: ftdi.h:329
ftdi_version_info
Definition: ftdi.h:453
ftdi_usb_close
int ftdi_usb_close(struct ftdi_context *ftdi)
Definition: ftdi.c:1048
ftdi_eeprom::channel_d_rs485enable
int channel_d_rs485enable
Definition: ftdi_i.h:88
ftdi_context::max_packet_size
unsigned int max_packet_size
Definition: ftdi.h:249
ftdi_setrts
int ftdi_setrts(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2276
IS_NOT_PNP
Definition: ftdi.h:283
ftdi_usb_reset
int ftdi_usb_reset(struct ftdi_context *ftdi)
Definition: ftdi.c:944
ftdi_get_latency_timer
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
Definition: ftdi.c:2143
CHANNEL_C_RS485
Definition: ftdi.h:332
ftdi_usb_get_strings
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:409
CBUS_FUNCTION_5
Definition: ftdi.h:301
ftdi_eeprom::channel_a_type
int channel_a_type
Definition: ftdi_i.h:77
ftdi_parity_type
ftdi_parity_type
Definition: ftdi.h:48
ftdi_usb_get_strings2
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:466
CHANNEL_D_RS485
Definition: ftdi.h:333
CHIP_TYPE
Definition: ftdi.h:323
ftdi_eeprom::use_serial
int use_serial
Definition: ftdi_i.h:59
ftdi_eeprom::group2_schmitt
int group2_schmitt
Definition: ftdi_i.h:116
ftdi_transfer_control::buf
unsigned char * buf
Definition: ftdi.h:207
ftdi_get_library_version
struct ftdi_version_info ftdi_get_library_version(void)
Get libftdi library version.
Definition: ftdi.c:281
ftdi_read_data_set_chunksize
int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:1983
SIO_RESET_PURGE_RX
#define SIO_RESET_PURGE_RX
Definition: ftdi.h:174
ftdi_eeprom::data_order
int data_order
Definition: ftdi_i.h:125
ftdi_write_data_get_chunksize
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:1834
SIO_SET_DTR_HIGH
#define SIO_SET_DTR_HIGH
Definition: ftdi.h:183
SIO_RESET_SIO
#define SIO_RESET_SIO
Definition: ftdi.h:173
ftdi_context::bitbang_enabled
unsigned char bitbang_enabled
Definition: ftdi.h:237
ftdi_eeprom::channel_a_rs485enable
int channel_a_rs485enable
Definition: ftdi_i.h:85
ftdi_write_eeprom
int ftdi_write_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4359
ftdi_usb_open_string
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char *description)
Definition: ftdi.c:851
ftdi_disable_bitbang
int ftdi_disable_bitbang(struct ftdi_context *ftdi)
Definition: ftdi.c:2067
ftdi_eeprom::self_powered
int self_powered
Definition: ftdi_i.h:42
ftdi_setdtr
int ftdi_setdtr(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2246
ftdi_set_latency_timer
int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
Definition: ftdi.c:2116
ftdi_stopbits_type
ftdi_stopbits_type
Definition: ftdi.h:50
ftdi_eeprom
FTDI eeprom structure.
Definition: ftdi_i.h:30
CHANNEL_IS_OPTO
#define CHANNEL_IS_OPTO
Definition: ftdi.h:398
SIO_RESET_PURGE_TX
#define SIO_RESET_PURGE_TX
Definition: ftdi.h:175
GROUP0_SLEW
Definition: ftdi.h:312
ftdi_eeprom::high_current
int high_current
Definition: ftdi_i.h:94
ftdi_context::readbuffer_chunksize
unsigned int readbuffer_chunksize
Definition: ftdi.h:245
ftdi_eeprom::channel_a_driver
int channel_a_driver
Definition: ftdi_i.h:80
ftdi_read_eeprom
int ftdi_read_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4206
ftdi_get_error_string
char * ftdi_get_error_string(struct ftdi_context *ftdi)
Definition: ftdi.c:4476
GROUP1_SCHMITT
Definition: ftdi.h:314
ftdi_context::baudrate
int baudrate
Definition: ftdi.h:235
CBUS_FUNCTION_7
Definition: ftdi.h:303
SIO_ERASE_EEPROM_REQUEST
#define SIO_ERASE_EEPROM_REQUEST
Definition: ftdi.h:170
ftdi_eeprom_build
int ftdi_eeprom_build(struct ftdi_context *ftdi)
Definition: ftdi.c:2669
ftdi_i.h
INVERT
Definition: ftdi.h:309
ftdi_eeprom::release_number
int release_number
Definition: ftdi_i.h:141
ftdi_usb_purge_buffers
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
Definition: ftdi.c:1019
GROUP2_SCHMITT
Definition: ftdi.h:317
ftdi_read_data
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1858
ftdi_write_eeprom_location
int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
Definition: ftdi.c:4303
TYPE_4232H
Definition: ftdi.h:43
ftdi_error_return_free_device_list
#define ftdi_error_return_free_device_list(code, str, devs)
Definition: ftdi.c:49
ftdi_get_eeprom_buf
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:4116
ftdi_eeprom::group3_slew
int group3_slew
Definition: ftdi_i.h:120
H_CLK
#define H_CLK
BREAK_OFF
Definition: ftdi.h:54
ftdi_error_return
#define ftdi_error_return(code, str)
Definition: ftdi.c:41
ftdi_context::readbuffer_offset
unsigned int readbuffer_offset
Definition: ftdi.h:241
ftdi_eeprom::group1_slew
int group1_slew
Definition: ftdi_i.h:114
ftdi_free
void ftdi_free(struct ftdi_context *ftdi)
Definition: ftdi.c:256
ftdi_read_chipid
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
Definition: ftdi.c:4265
ftdi_eeprom::group2_drive
int group2_drive
Definition: ftdi_i.h:115
ftdi_usb_find_all
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
Definition: ftdi.c:310
SIO_SET_RTS_HIGH
#define SIO_SET_RTS_HIGH
Definition: ftdi.h:186
AUTO_DETACH_SIO_MODULE
Definition: ftdi.h:84
CHANNEL_A_DRIVER
Definition: ftdi.h:294
SIO_SET_RTS_LOW
#define SIO_SET_RTS_LOW
Definition: ftdi.h:187
MAGIC
#define MAGIC
Definition: ftdi.c:4413
ftdi_set_line_property2
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:1364
ftdi_eeprom::is_not_pnp
int is_not_pnp
Definition: ftdi_i.h:46
CBUS_FUNCTION_3
Definition: ftdi.h:299
BITMODE_RESET
Definition: ftdi.h:59
SIO_SET_DTR_LOW
#define SIO_SET_DTR_LOW
Definition: ftdi.h:184
ftdi_context::eeprom
struct ftdi_eeprom * eeprom
Definition: ftdi.h:265
ftdi_eeprom::use_usb_version
int use_usb_version
Definition: ftdi_i.h:63
ftdi_transfer_control
Definition: ftdi.h:204
CHANNEL_B_RS485
Definition: ftdi.h:331