libftdi1  1.2
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)
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;
107 
108  if (libusb_init(&ftdi->usb_ctx) < 0)
109  ftdi_error_return(-3, "libusb_init() failed");
110 
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 
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 
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, struct libusb_device * dev,
410  char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
411 {
412  struct libusb_device_descriptor desc;
413 
414  if ((ftdi==NULL) || (dev==NULL))
415  return -1;
416 
417  if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
418  ftdi_error_return(-4, "libusb_open() failed");
419 
420  if (libusb_get_device_descriptor(dev, &desc) < 0)
421  ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
422 
423  if (manufacturer != NULL)
424  {
425  if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
426  {
427  ftdi_usb_close_internal (ftdi);
428  ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
429  }
430  }
431 
432  if (description != NULL)
433  {
434  if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
435  {
436  ftdi_usb_close_internal (ftdi);
437  ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
438  }
439  }
440 
441  if (serial != NULL)
442  {
443  if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
444  {
445  ftdi_usb_close_internal (ftdi);
446  ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
447  }
448  }
449 
450  ftdi_usb_close_internal (ftdi);
451 
452  return 0;
453 }
454 
461 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
462 {
463  struct libusb_device_descriptor desc;
464  struct libusb_config_descriptor *config0;
465  unsigned int packet_size;
466 
467  // Sanity check
468  if (ftdi == NULL || dev == NULL)
469  return 64;
470 
471  // Determine maximum packet size. Init with default value.
472  // New hi-speed devices from FTDI use a packet size of 512 bytes
473  // but could be connected to a normal speed USB hub -> 64 bytes packet size.
474  if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
475  packet_size = 512;
476  else
477  packet_size = 64;
478 
479  if (libusb_get_device_descriptor(dev, &desc) < 0)
480  return packet_size;
481 
482  if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
483  return packet_size;
484 
485  if (desc.bNumConfigurations > 0)
486  {
487  if (ftdi->interface < config0->bNumInterfaces)
488  {
489  struct libusb_interface interface = config0->interface[ftdi->interface];
490  if (interface.num_altsetting > 0)
491  {
492  struct libusb_interface_descriptor descriptor = interface.altsetting[0];
493  if (descriptor.bNumEndpoints > 0)
494  {
495  packet_size = descriptor.endpoint[0].wMaxPacketSize;
496  }
497  }
498  }
499  }
500 
501  libusb_free_config_descriptor (config0);
502  return packet_size;
503 }
504 
523 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
524 {
525  struct libusb_device_descriptor desc;
526  struct libusb_config_descriptor *config0;
527  int cfg, cfg0, detach_errno = 0;
528 
529  if (ftdi == NULL)
530  ftdi_error_return(-8, "ftdi context invalid");
531 
532  if (libusb_open(dev, &ftdi->usb_dev) < 0)
533  ftdi_error_return(-4, "libusb_open() failed");
534 
535  if (libusb_get_device_descriptor(dev, &desc) < 0)
536  ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
537 
538  if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
539  ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
540  cfg0 = config0->bConfigurationValue;
541  libusb_free_config_descriptor (config0);
542 
543  // Try to detach ftdi_sio kernel module.
544  //
545  // The return code is kept in a separate variable and only parsed
546  // if usb_set_configuration() or usb_claim_interface() fails as the
547  // detach operation might be denied and everything still works fine.
548  // Likely scenario is a static ftdi_sio kernel module.
550  {
551  if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
552  detach_errno = errno;
553  }
554 
555  if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
556  ftdi_error_return(-12, "libusb_get_configuration () failed");
557  // set configuration (needed especially for windows)
558  // tolerate EBUSY: one device with one configuration, but two interfaces
559  // and libftdi sessions to both interfaces (e.g. FT2232)
560  if (desc.bNumConfigurations > 0 && cfg != cfg0)
561  {
562  if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
563  {
564  ftdi_usb_close_internal (ftdi);
565  if (detach_errno == EPERM)
566  {
567  ftdi_error_return(-8, "inappropriate permissions on device!");
568  }
569  else
570  {
571  ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
572  }
573  }
574  }
575 
576  if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
577  {
578  ftdi_usb_close_internal (ftdi);
579  if (detach_errno == EPERM)
580  {
581  ftdi_error_return(-8, "inappropriate permissions on device!");
582  }
583  else
584  {
585  ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
586  }
587  }
588 
589  if (ftdi_usb_reset (ftdi) != 0)
590  {
591  ftdi_usb_close_internal (ftdi);
592  ftdi_error_return(-6, "ftdi_usb_reset failed");
593  }
594 
595  // Try to guess chip type
596  // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
597  if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
598  && desc.iSerialNumber == 0))
599  ftdi->type = TYPE_BM;
600  else if (desc.bcdDevice == 0x200)
601  ftdi->type = TYPE_AM;
602  else if (desc.bcdDevice == 0x500)
603  ftdi->type = TYPE_2232C;
604  else if (desc.bcdDevice == 0x600)
605  ftdi->type = TYPE_R;
606  else if (desc.bcdDevice == 0x700)
607  ftdi->type = TYPE_2232H;
608  else if (desc.bcdDevice == 0x800)
609  ftdi->type = TYPE_4232H;
610  else if (desc.bcdDevice == 0x900)
611  ftdi->type = TYPE_232H;
612  else if (desc.bcdDevice == 0x1000)
613  ftdi->type = TYPE_230X;
614 
615  // Determine maximum packet size
616  ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
617 
618  if (ftdi_set_baudrate (ftdi, 9600) != 0)
619  {
620  ftdi_usb_close_internal (ftdi);
621  ftdi_error_return(-7, "set baudrate failed");
622  }
623 
624  ftdi_error_return(0, "all fine");
625 }
626 
636 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
637 {
638  return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
639 }
640 
662 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
663  const char* description, const char* serial)
664 {
665  return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
666 }
667 
692 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
693  const char* description, const char* serial, unsigned int index)
694 {
695  libusb_device *dev;
696  libusb_device **devs;
697  char string[256];
698  int i = 0;
699 
700  if (ftdi == NULL)
701  ftdi_error_return(-11, "ftdi context invalid");
702 
703  if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
704  ftdi_error_return(-12, "libusb_get_device_list() failed");
705 
706  while ((dev = devs[i++]) != NULL)
707  {
708  struct libusb_device_descriptor desc;
709  int res;
710 
711  if (libusb_get_device_descriptor(dev, &desc) < 0)
712  ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
713 
714  if (desc.idVendor == vendor && desc.idProduct == product)
715  {
716  if (libusb_open(dev, &ftdi->usb_dev) < 0)
717  ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
718 
719  if (description != NULL)
720  {
721  if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
722  {
723  ftdi_usb_close_internal (ftdi);
724  ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
725  }
726  if (strncmp(string, description, sizeof(string)) != 0)
727  {
728  ftdi_usb_close_internal (ftdi);
729  continue;
730  }
731  }
732  if (serial != NULL)
733  {
734  if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
735  {
736  ftdi_usb_close_internal (ftdi);
737  ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
738  }
739  if (strncmp(string, serial, sizeof(string)) != 0)
740  {
741  ftdi_usb_close_internal (ftdi);
742  continue;
743  }
744  }
745 
746  ftdi_usb_close_internal (ftdi);
747 
748  if (index > 0)
749  {
750  index--;
751  continue;
752  }
753 
754  res = ftdi_usb_open_dev(ftdi, dev);
755  libusb_free_device_list(devs,1);
756  return res;
757  }
758  }
759 
760  // device not found
761  ftdi_error_return_free_device_list(-3, "device not found", devs);
762 }
763 
790 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
791 {
792  if (ftdi == NULL)
793  ftdi_error_return(-12, "ftdi context invalid");
794 
795  if (description[0] == 0 || description[1] != ':')
796  ftdi_error_return(-11, "illegal description format");
797 
798  if (description[0] == 'd')
799  {
800  libusb_device *dev;
801  libusb_device **devs;
802  unsigned int bus_number, device_address;
803  int i = 0;
804 
805  if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
806  ftdi_error_return(-2, "libusb_get_device_list() failed");
807 
808  /* XXX: This doesn't handle symlinks/odd paths/etc... */
809  if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
810  ftdi_error_return_free_device_list(-11, "illegal description format", devs);
811 
812  while ((dev = devs[i++]) != NULL)
813  {
814  int ret;
815  if (bus_number == libusb_get_bus_number (dev)
816  && device_address == libusb_get_device_address (dev))
817  {
818  ret = ftdi_usb_open_dev(ftdi, dev);
819  libusb_free_device_list(devs,1);
820  return ret;
821  }
822  }
823 
824  // device not found
825  ftdi_error_return_free_device_list(-3, "device not found", devs);
826  }
827  else if (description[0] == 'i' || description[0] == 's')
828  {
829  unsigned int vendor;
830  unsigned int product;
831  unsigned int index=0;
832  const char *serial=NULL;
833  const char *startp, *endp;
834 
835  errno=0;
836  startp=description+2;
837  vendor=strtoul((char*)startp,(char**)&endp,0);
838  if (*endp != ':' || endp == startp || errno != 0)
839  ftdi_error_return(-11, "illegal description format");
840 
841  startp=endp+1;
842  product=strtoul((char*)startp,(char**)&endp,0);
843  if (endp == startp || errno != 0)
844  ftdi_error_return(-11, "illegal description format");
845 
846  if (description[0] == 'i' && *endp != 0)
847  {
848  /* optional index field in i-mode */
849  if (*endp != ':')
850  ftdi_error_return(-11, "illegal description format");
851 
852  startp=endp+1;
853  index=strtoul((char*)startp,(char**)&endp,0);
854  if (*endp != 0 || endp == startp || errno != 0)
855  ftdi_error_return(-11, "illegal description format");
856  }
857  if (description[0] == 's')
858  {
859  if (*endp != ':')
860  ftdi_error_return(-11, "illegal description format");
861 
862  /* rest of the description is the serial */
863  serial=endp+1;
864  }
865 
866  return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
867  }
868  else
869  {
870  ftdi_error_return(-11, "illegal description format");
871  }
872 }
873 
883 int ftdi_usb_reset(struct ftdi_context *ftdi)
884 {
885  if (ftdi == NULL || ftdi->usb_dev == NULL)
886  ftdi_error_return(-2, "USB device unavailable");
887 
888  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
890  ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
891  ftdi_error_return(-1,"FTDI reset failed");
892 
893  // Invalidate data in the readbuffer
894  ftdi->readbuffer_offset = 0;
895  ftdi->readbuffer_remaining = 0;
896 
897  return 0;
898 }
899 
910 {
911  if (ftdi == NULL || ftdi->usb_dev == NULL)
912  ftdi_error_return(-2, "USB device unavailable");
913 
914  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
916  ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
917  ftdi_error_return(-1, "FTDI purge of RX buffer failed");
918 
919  // Invalidate data in the readbuffer
920  ftdi->readbuffer_offset = 0;
921  ftdi->readbuffer_remaining = 0;
922 
923  return 0;
924 }
925 
936 {
937  if (ftdi == NULL || ftdi->usb_dev == NULL)
938  ftdi_error_return(-2, "USB device unavailable");
939 
940  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
942  ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
943  ftdi_error_return(-1, "FTDI purge of TX buffer failed");
944 
945  return 0;
946 }
947 
959 {
960  int result;
961 
962  if (ftdi == NULL || ftdi->usb_dev == NULL)
963  ftdi_error_return(-3, "USB device unavailable");
964 
965  result = ftdi_usb_purge_rx_buffer(ftdi);
966  if (result < 0)
967  return -1;
968 
969  result = ftdi_usb_purge_tx_buffer(ftdi);
970  if (result < 0)
971  return -2;
972 
973  return 0;
974 }
975 
976 
977 
987 int ftdi_usb_close(struct ftdi_context *ftdi)
988 {
989  int rtn = 0;
990 
991  if (ftdi == NULL)
992  ftdi_error_return(-3, "ftdi context invalid");
993 
994  if (ftdi->usb_dev != NULL)
995  if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
996  rtn = -1;
997 
998  ftdi_usb_close_internal (ftdi);
999 
1000  return rtn;
1001 }
1002 
1003 /* ftdi_to_clkbits_AM For the AM device, convert a requested baudrate
1004  to encoded divisor and the achievable baudrate
1005  Function is only used internally
1006  \internal
1007 
1008  See AN120
1009  clk/1 -> 0
1010  clk/1.5 -> 1
1011  clk/2 -> 2
1012  From /2, 0.125/ 0.25 and 0.5 steps may be taken
1013  The fractional part has frac_code encoding
1014 */
1015 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
1016 
1017 {
1018  static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1019  static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
1020  static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
1021  int divisor, best_divisor, best_baud, best_baud_diff;
1022  int i;
1023  divisor = 24000000 / baudrate;
1024 
1025  // Round down to supported fraction (AM only)
1026  divisor -= am_adjust_dn[divisor & 7];
1027 
1028  // Try this divisor and the one above it (because division rounds down)
1029  best_divisor = 0;
1030  best_baud = 0;
1031  best_baud_diff = 0;
1032  for (i = 0; i < 2; i++)
1033  {
1034  int try_divisor = divisor + i;
1035  int baud_estimate;
1036  int baud_diff;
1037 
1038  // Round up to supported divisor value
1039  if (try_divisor <= 8)
1040  {
1041  // Round up to minimum supported divisor
1042  try_divisor = 8;
1043  }
1044  else if (divisor < 16)
1045  {
1046  // AM doesn't support divisors 9 through 15 inclusive
1047  try_divisor = 16;
1048  }
1049  else
1050  {
1051  // Round up to supported fraction (AM only)
1052  try_divisor += am_adjust_up[try_divisor & 7];
1053  if (try_divisor > 0x1FFF8)
1054  {
1055  // Round down to maximum supported divisor value (for AM)
1056  try_divisor = 0x1FFF8;
1057  }
1058  }
1059  // Get estimated baud rate (to nearest integer)
1060  baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1061  // Get absolute difference from requested baud rate
1062  if (baud_estimate < baudrate)
1063  {
1064  baud_diff = baudrate - baud_estimate;
1065  }
1066  else
1067  {
1068  baud_diff = baud_estimate - baudrate;
1069  }
1070  if (i == 0 || baud_diff < best_baud_diff)
1071  {
1072  // Closest to requested baud rate so far
1073  best_divisor = try_divisor;
1074  best_baud = baud_estimate;
1075  best_baud_diff = baud_diff;
1076  if (baud_diff == 0)
1077  {
1078  // Spot on! No point trying
1079  break;
1080  }
1081  }
1082  }
1083  // Encode the best divisor value
1084  *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1085  // Deal with special cases for encoded value
1086  if (*encoded_divisor == 1)
1087  {
1088  *encoded_divisor = 0; // 3000000 baud
1089  }
1090  else if (*encoded_divisor == 0x4001)
1091  {
1092  *encoded_divisor = 1; // 2000000 baud (BM only)
1093  }
1094  return best_baud;
1095 }
1096 
1097 /* ftdi_to_clkbits Convert a requested baudrate for a given system clock and predivisor
1098  to encoded divisor and the achievable baudrate
1099  Function is only used internally
1100  \internal
1101 
1102  See AN120
1103  clk/1 -> 0
1104  clk/1.5 -> 1
1105  clk/2 -> 2
1106  From /2, 0.125 steps may be taken.
1107  The fractional part has frac_code encoding
1108 
1109  value[13:0] of value is the divisor
1110  index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1111 
1112  H Type have all features above with
1113  {index[8],value[15:14]} is the encoded subdivisor
1114 
1115  FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1116  {index[0],value[15:14]} is the encoded subdivisor
1117 
1118  AM Type chips have only four fractional subdivisors at value[15:14]
1119  for subdivisors 0, 0.5, 0.25, 0.125
1120 */
1121 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1122 {
1123  static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1124  int best_baud = 0;
1125  int divisor, best_divisor;
1126  if (baudrate >= clk/clk_div)
1127  {
1128  *encoded_divisor = 0;
1129  best_baud = clk/clk_div;
1130  }
1131  else if (baudrate >= clk/(clk_div + clk_div/2))
1132  {
1133  *encoded_divisor = 1;
1134  best_baud = clk/(clk_div + clk_div/2);
1135  }
1136  else if (baudrate >= clk/(2*clk_div))
1137  {
1138  *encoded_divisor = 2;
1139  best_baud = clk/(2*clk_div);
1140  }
1141  else
1142  {
1143  /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1144  divisor = clk*16/clk_div / baudrate;
1145  if (divisor & 1) /* Decide if to round up or down*/
1146  best_divisor = divisor /2 +1;
1147  else
1148  best_divisor = divisor/2;
1149  if(best_divisor > 0x20000)
1150  best_divisor = 0x1ffff;
1151  best_baud = clk*16/clk_div/best_divisor;
1152  if (best_baud & 1) /* Decide if to round up or down*/
1153  best_baud = best_baud /2 +1;
1154  else
1155  best_baud = best_baud /2;
1156  *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1157  }
1158  return best_baud;
1159 }
1165 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1166  unsigned short *value, unsigned short *index)
1167 {
1168  int best_baud;
1169  unsigned long encoded_divisor;
1170 
1171  if (baudrate <= 0)
1172  {
1173  // Return error
1174  return -1;
1175  }
1176 
1177 #define H_CLK 120000000
1178 #define C_CLK 48000000
1179  if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
1180  {
1181  if(baudrate*10 > H_CLK /0x3fff)
1182  {
1183  /* On H Devices, use 12 000 000 Baudrate when possible
1184  We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1185  three fractional bits and a 120 MHz clock
1186  Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1187  DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1188  best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1189  encoded_divisor |= 0x20000; /* switch on CLK/10*/
1190  }
1191  else
1192  best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1193  }
1194  else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
1195  {
1196  best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1197  }
1198  else
1199  {
1200  best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
1201  }
1202  // Split into "value" and "index" values
1203  *value = (unsigned short)(encoded_divisor & 0xFFFF);
1204  if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
1205  {
1206  *index = (unsigned short)(encoded_divisor >> 8);
1207  *index &= 0xFF00;
1208  *index |= ftdi->index;
1209  }
1210  else
1211  *index = (unsigned short)(encoded_divisor >> 16);
1212 
1213  // Return the nearest baud rate
1214  return best_baud;
1215 }
1216 
1221 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1222  unsigned short *value, unsigned short *index)
1223 {
1224  return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1225 }
1226 
1238 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1239 {
1240  unsigned short value, index;
1241  int actual_baudrate;
1242 
1243  if (ftdi == NULL || ftdi->usb_dev == NULL)
1244  ftdi_error_return(-3, "USB device unavailable");
1245 
1246  if (ftdi->bitbang_enabled)
1247  {
1248  baudrate = baudrate*4;
1249  }
1250 
1251  actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1252  if (actual_baudrate <= 0)
1253  ftdi_error_return (-1, "Silly baudrate <= 0.");
1254 
1255  // Check within tolerance (about 5%)
1256  if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1257  || ((actual_baudrate < baudrate)
1258  ? (actual_baudrate * 21 < baudrate * 20)
1259  : (baudrate * 21 < actual_baudrate * 20)))
1260  ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1261 
1262  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1263  SIO_SET_BAUDRATE_REQUEST, value,
1264  index, NULL, 0, ftdi->usb_write_timeout) < 0)
1265  ftdi_error_return (-2, "Setting new baudrate failed");
1266 
1267  ftdi->baudrate = baudrate;
1268  return 0;
1269 }
1270 
1285  enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1286 {
1287  return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1288 }
1289 
1304  enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1305  enum ftdi_break_type break_type)
1306 {
1307  unsigned short value = bits;
1308 
1309  if (ftdi == NULL || ftdi->usb_dev == NULL)
1310  ftdi_error_return(-2, "USB device unavailable");
1311 
1312  switch (parity)
1313  {
1314  case NONE:
1315  value |= (0x00 << 8);
1316  break;
1317  case ODD:
1318  value |= (0x01 << 8);
1319  break;
1320  case EVEN:
1321  value |= (0x02 << 8);
1322  break;
1323  case MARK:
1324  value |= (0x03 << 8);
1325  break;
1326  case SPACE:
1327  value |= (0x04 << 8);
1328  break;
1329  }
1330 
1331  switch (sbit)
1332  {
1333  case STOP_BIT_1:
1334  value |= (0x00 << 11);
1335  break;
1336  case STOP_BIT_15:
1337  value |= (0x01 << 11);
1338  break;
1339  case STOP_BIT_2:
1340  value |= (0x02 << 11);
1341  break;
1342  }
1343 
1344  switch (break_type)
1345  {
1346  case BREAK_OFF:
1347  value |= (0x00 << 14);
1348  break;
1349  case BREAK_ON:
1350  value |= (0x01 << 14);
1351  break;
1352  }
1353 
1354  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1355  SIO_SET_DATA_REQUEST, value,
1356  ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1357  ftdi_error_return (-1, "Setting new line property failed");
1358 
1359  return 0;
1360 }
1361 
1373 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
1374 {
1375  int offset = 0;
1376  int actual_length;
1377 
1378  if (ftdi == NULL || ftdi->usb_dev == NULL)
1379  ftdi_error_return(-666, "USB device unavailable");
1380 
1381  while (offset < size)
1382  {
1383  int write_size = ftdi->writebuffer_chunksize;
1384 
1385  if (offset+write_size > size)
1386  write_size = size-offset;
1387 
1388  if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1389  ftdi_error_return(-1, "usb bulk write failed");
1390 
1391  offset += actual_length;
1392  }
1393 
1394  return offset;
1395 }
1396 
1397 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
1398 {
1399  struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1400  struct ftdi_context *ftdi = tc->ftdi;
1401  int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1402 
1403  packet_size = ftdi->max_packet_size;
1404 
1405  actual_length = transfer->actual_length;
1406 
1407  if (actual_length > 2)
1408  {
1409  // skip FTDI status bytes.
1410  // Maybe stored in the future to enable modem use
1411  num_of_chunks = actual_length / packet_size;
1412  chunk_remains = actual_length % packet_size;
1413  //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);
1414 
1415  ftdi->readbuffer_offset += 2;
1416  actual_length -= 2;
1417 
1418  if (actual_length > packet_size - 2)
1419  {
1420  for (i = 1; i < num_of_chunks; i++)
1421  memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1422  ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1423  packet_size - 2);
1424  if (chunk_remains > 2)
1425  {
1426  memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1427  ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1428  chunk_remains-2);
1429  actual_length -= 2*num_of_chunks;
1430  }
1431  else
1432  actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1433  }
1434 
1435  if (actual_length > 0)
1436  {
1437  // data still fits in buf?
1438  if (tc->offset + actual_length <= tc->size)
1439  {
1440  memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1441  //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1442  tc->offset += actual_length;
1443 
1444  ftdi->readbuffer_offset = 0;
1445  ftdi->readbuffer_remaining = 0;
1446 
1447  /* Did we read exactly the right amount of bytes? */
1448  if (tc->offset == tc->size)
1449  {
1450  //printf("read_data exact rem %d offset %d\n",
1451  //ftdi->readbuffer_remaining, offset);
1452  tc->completed = 1;
1453  return;
1454  }
1455  }
1456  else
1457  {
1458  // only copy part of the data or size <= readbuffer_chunksize
1459  int part_size = tc->size - tc->offset;
1460  memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1461  tc->offset += part_size;
1462 
1463  ftdi->readbuffer_offset += part_size;
1464  ftdi->readbuffer_remaining = actual_length - part_size;
1465 
1466  /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1467  part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1468  tc->completed = 1;
1469  return;
1470  }
1471  }
1472  }
1473  ret = libusb_submit_transfer (transfer);
1474  if (ret < 0)
1475  tc->completed = 1;
1476 }
1477 
1478 
1479 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
1480 {
1481  struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1482  struct ftdi_context *ftdi = tc->ftdi;
1483 
1484  tc->offset += transfer->actual_length;
1485 
1486  if (tc->offset == tc->size)
1487  {
1488  tc->completed = 1;
1489  }
1490  else
1491  {
1492  int write_size = ftdi->writebuffer_chunksize;
1493  int ret;
1494 
1495  if (tc->offset + write_size > tc->size)
1496  write_size = tc->size - tc->offset;
1497 
1498  transfer->length = write_size;
1499  transfer->buffer = tc->buf + tc->offset;
1500  ret = libusb_submit_transfer (transfer);
1501  if (ret < 0)
1502  tc->completed = 1;
1503  }
1504 }
1505 
1506 
1521 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1522 {
1523  struct ftdi_transfer_control *tc;
1524  struct libusb_transfer *transfer;
1525  int write_size, ret;
1526 
1527  if (ftdi == NULL || ftdi->usb_dev == NULL)
1528  return NULL;
1529 
1530  tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1531  if (!tc)
1532  return NULL;
1533 
1534  transfer = libusb_alloc_transfer(0);
1535  if (!transfer)
1536  {
1537  free(tc);
1538  return NULL;
1539  }
1540 
1541  tc->ftdi = ftdi;
1542  tc->completed = 0;
1543  tc->buf = buf;
1544  tc->size = size;
1545  tc->offset = 0;
1546 
1547  if (size < (int)ftdi->writebuffer_chunksize)
1548  write_size = size;
1549  else
1550  write_size = ftdi->writebuffer_chunksize;
1551 
1552  libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1553  write_size, ftdi_write_data_cb, tc,
1554  ftdi->usb_write_timeout);
1555  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1556 
1557  ret = libusb_submit_transfer(transfer);
1558  if (ret < 0)
1559  {
1560  libusb_free_transfer(transfer);
1561  free(tc);
1562  return NULL;
1563  }
1564  tc->transfer = transfer;
1565 
1566  return tc;
1567 }
1568 
1583 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1584 {
1585  struct ftdi_transfer_control *tc;
1586  struct libusb_transfer *transfer;
1587  int ret;
1588 
1589  if (ftdi == NULL || ftdi->usb_dev == NULL)
1590  return NULL;
1591 
1592  tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1593  if (!tc)
1594  return NULL;
1595 
1596  tc->ftdi = ftdi;
1597  tc->buf = buf;
1598  tc->size = size;
1599 
1600  if (size <= (int)ftdi->readbuffer_remaining)
1601  {
1602  memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1603 
1604  // Fix offsets
1605  ftdi->readbuffer_remaining -= size;
1606  ftdi->readbuffer_offset += size;
1607 
1608  /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1609 
1610  tc->completed = 1;
1611  tc->offset = size;
1612  tc->transfer = NULL;
1613  return tc;
1614  }
1615 
1616  tc->completed = 0;
1617  if (ftdi->readbuffer_remaining != 0)
1618  {
1619  memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1620 
1621  tc->offset = ftdi->readbuffer_remaining;
1622  }
1623  else
1624  tc->offset = 0;
1625 
1626  transfer = libusb_alloc_transfer(0);
1627  if (!transfer)
1628  {
1629  free (tc);
1630  return NULL;
1631  }
1632 
1633  ftdi->readbuffer_remaining = 0;
1634  ftdi->readbuffer_offset = 0;
1635 
1636  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);
1637  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1638 
1639  ret = libusb_submit_transfer(transfer);
1640  if (ret < 0)
1641  {
1642  libusb_free_transfer(transfer);
1643  free (tc);
1644  return NULL;
1645  }
1646  tc->transfer = transfer;
1647 
1648  return tc;
1649 }
1650 
1663 {
1664  int ret;
1665 
1666  while (!tc->completed)
1667  {
1668  ret = libusb_handle_events(tc->ftdi->usb_ctx);
1669  if (ret < 0)
1670  {
1671  if (ret == LIBUSB_ERROR_INTERRUPTED)
1672  continue;
1673  libusb_cancel_transfer(tc->transfer);
1674  while (!tc->completed)
1675  if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1676  break;
1677  libusb_free_transfer(tc->transfer);
1678  free (tc);
1679  return ret;
1680  }
1681  }
1682 
1683  ret = tc->offset;
1688  if (tc->transfer)
1689  {
1690  if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1691  ret = -1;
1692  libusb_free_transfer(tc->transfer);
1693  }
1694  free(tc);
1695  return ret;
1696 }
1697 
1708 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1709 {
1710  if (ftdi == NULL)
1711  ftdi_error_return(-1, "ftdi context invalid");
1712 
1713  ftdi->writebuffer_chunksize = chunksize;
1714  return 0;
1715 }
1716 
1726 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1727 {
1728  if (ftdi == NULL)
1729  ftdi_error_return(-1, "ftdi context invalid");
1730 
1731  *chunksize = ftdi->writebuffer_chunksize;
1732  return 0;
1733 }
1734 
1750 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1751 {
1752  int offset = 0, ret, i, num_of_chunks, chunk_remains;
1753  int packet_size = ftdi->max_packet_size;
1754  int actual_length = 1;
1755 
1756  if (ftdi == NULL || ftdi->usb_dev == NULL)
1757  ftdi_error_return(-666, "USB device unavailable");
1758 
1759  // Packet size sanity check (avoid division by zero)
1760  if (packet_size == 0)
1761  ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1762 
1763  // everything we want is still in the readbuffer?
1764  if (size <= (int)ftdi->readbuffer_remaining)
1765  {
1766  memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1767 
1768  // Fix offsets
1769  ftdi->readbuffer_remaining -= size;
1770  ftdi->readbuffer_offset += size;
1771 
1772  /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1773 
1774  return size;
1775  }
1776  // something still in the readbuffer, but not enough to satisfy 'size'?
1777  if (ftdi->readbuffer_remaining != 0)
1778  {
1779  memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1780 
1781  // Fix offset
1782  offset += ftdi->readbuffer_remaining;
1783  }
1784  // do the actual USB read
1785  while (offset < size && actual_length > 0)
1786  {
1787  ftdi->readbuffer_remaining = 0;
1788  ftdi->readbuffer_offset = 0;
1789  /* returns how much received */
1790  ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1791  if (ret < 0)
1792  ftdi_error_return(ret, "usb bulk read failed");
1793 
1794  if (actual_length > 2)
1795  {
1796  // skip FTDI status bytes.
1797  // Maybe stored in the future to enable modem use
1798  num_of_chunks = actual_length / packet_size;
1799  chunk_remains = actual_length % packet_size;
1800  //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);
1801 
1802  ftdi->readbuffer_offset += 2;
1803  actual_length -= 2;
1804 
1805  if (actual_length > packet_size - 2)
1806  {
1807  for (i = 1; i < num_of_chunks; i++)
1808  memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1809  ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1810  packet_size - 2);
1811  if (chunk_remains > 2)
1812  {
1813  memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1814  ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1815  chunk_remains-2);
1816  actual_length -= 2*num_of_chunks;
1817  }
1818  else
1819  actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1820  }
1821  }
1822  else if (actual_length <= 2)
1823  {
1824  // no more data to read?
1825  return offset;
1826  }
1827  if (actual_length > 0)
1828  {
1829  // data still fits in buf?
1830  if (offset+actual_length <= size)
1831  {
1832  memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1833  //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1834  offset += actual_length;
1835 
1836  /* Did we read exactly the right amount of bytes? */
1837  if (offset == size)
1838  //printf("read_data exact rem %d offset %d\n",
1839  //ftdi->readbuffer_remaining, offset);
1840  return offset;
1841  }
1842  else
1843  {
1844  // only copy part of the data or size <= readbuffer_chunksize
1845  int part_size = size-offset;
1846  memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1847 
1848  ftdi->readbuffer_offset += part_size;
1849  ftdi->readbuffer_remaining = actual_length-part_size;
1850  offset += part_size;
1851 
1852  /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1853  part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1854 
1855  return offset;
1856  }
1857  }
1858  }
1859  // never reached
1860  return -127;
1861 }
1862 
1875 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1876 {
1877  unsigned char *new_buf;
1878 
1879  if (ftdi == NULL)
1880  ftdi_error_return(-1, "ftdi context invalid");
1881 
1882  // Invalidate all remaining data
1883  ftdi->readbuffer_offset = 0;
1884  ftdi->readbuffer_remaining = 0;
1885 #ifdef __linux__
1886  /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1887  which is defined in libusb-1.0. Otherwise, each USB read request will
1888  be divided into multiple URBs. This will cause issues on Linux kernel
1889  older than 2.6.32. */
1890  if (chunksize > 16384)
1891  chunksize = 16384;
1892 #endif
1893 
1894  if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1895  ftdi_error_return(-1, "out of memory for readbuffer");
1896 
1897  ftdi->readbuffer = new_buf;
1898  ftdi->readbuffer_chunksize = chunksize;
1899 
1900  return 0;
1901 }
1902 
1912 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1913 {
1914  if (ftdi == NULL)
1915  ftdi_error_return(-1, "FTDI context invalid");
1916 
1917  *chunksize = ftdi->readbuffer_chunksize;
1918  return 0;
1919 }
1920 
1933 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1934 {
1935  unsigned short usb_val;
1936 
1937  if (ftdi == NULL || ftdi->usb_dev == NULL)
1938  ftdi_error_return(-2, "USB device unavailable");
1939 
1940  usb_val = bitmask; // low byte: bitmask
1941  usb_val |= (mode << 8);
1942  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)
1943  ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
1944 
1945  ftdi->bitbang_mode = mode;
1946  ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1947  return 0;
1948 }
1949 
1960 {
1961  if (ftdi == NULL || ftdi->usb_dev == NULL)
1962  ftdi_error_return(-2, "USB device unavailable");
1963 
1964  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)
1965  ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1966 
1967  ftdi->bitbang_enabled = 0;
1968  return 0;
1969 }
1970 
1971 
1982 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1983 {
1984  if (ftdi == NULL || ftdi->usb_dev == NULL)
1985  ftdi_error_return(-2, "USB device unavailable");
1986 
1987  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)
1988  ftdi_error_return(-1, "read pins failed");
1989 
1990  return 0;
1991 }
1992 
2008 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2009 {
2010  unsigned short usb_val;
2011 
2012  if (latency < 1)
2013  ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
2014 
2015  if (ftdi == NULL || ftdi->usb_dev == NULL)
2016  ftdi_error_return(-3, "USB device unavailable");
2017 
2018  usb_val = latency;
2019  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)
2020  ftdi_error_return(-2, "unable to set latency timer");
2021 
2022  return 0;
2023 }
2024 
2035 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2036 {
2037  unsigned short usb_val;
2038 
2039  if (ftdi == NULL || ftdi->usb_dev == NULL)
2040  ftdi_error_return(-2, "USB device unavailable");
2041 
2042  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)
2043  ftdi_error_return(-1, "reading latency timer failed");
2044 
2045  *latency = (unsigned char)usb_val;
2046  return 0;
2047 }
2048 
2089 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2090 {
2091  char usb_val[2];
2092 
2093  if (ftdi == NULL || ftdi->usb_dev == NULL)
2094  ftdi_error_return(-2, "USB device unavailable");
2095 
2096  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)
2097  ftdi_error_return(-1, "getting modem status failed");
2098 
2099  *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
2100 
2101  return 0;
2102 }
2103 
2115 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2116 {
2117  if (ftdi == NULL || ftdi->usb_dev == NULL)
2118  ftdi_error_return(-2, "USB device unavailable");
2119 
2120  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2121  SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2122  NULL, 0, ftdi->usb_write_timeout) < 0)
2123  ftdi_error_return(-1, "set flow control failed");
2124 
2125  return 0;
2126 }
2127 
2138 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2139 {
2140  unsigned short usb_val;
2141 
2142  if (ftdi == NULL || ftdi->usb_dev == NULL)
2143  ftdi_error_return(-2, "USB device unavailable");
2144 
2145  if (state)
2146  usb_val = SIO_SET_DTR_HIGH;
2147  else
2148  usb_val = SIO_SET_DTR_LOW;
2149 
2150  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2151  SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2152  NULL, 0, ftdi->usb_write_timeout) < 0)
2153  ftdi_error_return(-1, "set dtr failed");
2154 
2155  return 0;
2156 }
2157 
2168 int ftdi_setrts(struct ftdi_context *ftdi, int state)
2169 {
2170  unsigned short usb_val;
2171 
2172  if (ftdi == NULL || ftdi->usb_dev == NULL)
2173  ftdi_error_return(-2, "USB device unavailable");
2174 
2175  if (state)
2176  usb_val = SIO_SET_RTS_HIGH;
2177  else
2178  usb_val = SIO_SET_RTS_LOW;
2179 
2180  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2181  SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2182  NULL, 0, ftdi->usb_write_timeout) < 0)
2183  ftdi_error_return(-1, "set of rts failed");
2184 
2185  return 0;
2186 }
2187 
2199 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2200 {
2201  unsigned short usb_val;
2202 
2203  if (ftdi == NULL || ftdi->usb_dev == NULL)
2204  ftdi_error_return(-2, "USB device unavailable");
2205 
2206  if (dtr)
2207  usb_val = SIO_SET_DTR_HIGH;
2208  else
2209  usb_val = SIO_SET_DTR_LOW;
2210 
2211  if (rts)
2212  usb_val |= SIO_SET_RTS_HIGH;
2213  else
2214  usb_val |= SIO_SET_RTS_LOW;
2215 
2216  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2217  SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2218  NULL, 0, ftdi->usb_write_timeout) < 0)
2219  ftdi_error_return(-1, "set of rts/dtr failed");
2220 
2221  return 0;
2222 }
2223 
2236  unsigned char eventch, unsigned char enable)
2237 {
2238  unsigned short usb_val;
2239 
2240  if (ftdi == NULL || ftdi->usb_dev == NULL)
2241  ftdi_error_return(-2, "USB device unavailable");
2242 
2243  usb_val = eventch;
2244  if (enable)
2245  usb_val |= 1 << 8;
2246 
2247  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)
2248  ftdi_error_return(-1, "setting event character failed");
2249 
2250  return 0;
2251 }
2252 
2265  unsigned char errorch, unsigned char enable)
2266 {
2267  unsigned short usb_val;
2268 
2269  if (ftdi == NULL || ftdi->usb_dev == NULL)
2270  ftdi_error_return(-2, "USB device unavailable");
2271 
2272  usb_val = errorch;
2273  if (enable)
2274  usb_val |= 1 << 8;
2275 
2276  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)
2277  ftdi_error_return(-1, "setting error character failed");
2278 
2279  return 0;
2280 }
2281 
2294 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2295  char * product, char * serial)
2296 {
2297  struct ftdi_eeprom *eeprom;
2298 
2299  if (ftdi == NULL)
2300  ftdi_error_return(-1, "No struct ftdi_context");
2301 
2302  if (ftdi->eeprom == NULL)
2303  ftdi_error_return(-2,"No struct ftdi_eeprom");
2304 
2305  eeprom = ftdi->eeprom;
2306  memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2307 
2308  if (ftdi->usb_dev == NULL)
2309  ftdi_error_return(-3, "No connected device or device not yet opened");
2310 
2311  eeprom->vendor_id = 0x0403;
2312  eeprom->use_serial = 1;
2313  if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2314  (ftdi->type == TYPE_R))
2315  eeprom->product_id = 0x6001;
2316  else if (ftdi->type == TYPE_4232H)
2317  eeprom->product_id = 0x6011;
2318  else if (ftdi->type == TYPE_232H)
2319  eeprom->product_id = 0x6014;
2320  else if (ftdi->type == TYPE_230X)
2321  eeprom->product_id = 0x6015;
2322  else
2323  eeprom->product_id = 0x6010;
2324 
2325  if (ftdi->type == TYPE_AM)
2326  eeprom->usb_version = 0x0101;
2327  else
2328  eeprom->usb_version = 0x0200;
2329  eeprom->max_power = 100;
2330 
2331  if (eeprom->manufacturer)
2332  free (eeprom->manufacturer);
2333  eeprom->manufacturer = NULL;
2334  if (manufacturer)
2335  {
2336  eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2337  if (eeprom->manufacturer)
2338  strcpy(eeprom->manufacturer, manufacturer);
2339  }
2340 
2341  if (eeprom->product)
2342  free (eeprom->product);
2343  eeprom->product = NULL;
2344  if(product)
2345  {
2346  eeprom->product = malloc(strlen(product)+1);
2347  if (eeprom->product)
2348  strcpy(eeprom->product, product);
2349  }
2350  else
2351  {
2352  const char* default_product;
2353  switch(ftdi->type)
2354  {
2355  case TYPE_AM: default_product = "AM"; break;
2356  case TYPE_BM: default_product = "BM"; break;
2357  case TYPE_2232C: default_product = "Dual RS232"; break;
2358  case TYPE_R: default_product = "FT232R USB UART"; break;
2359  case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2360  case TYPE_4232H: default_product = "FT4232H"; break;
2361  case TYPE_232H: default_product = "Single-RS232-HS"; break;
2362  case TYPE_230X: default_product = "FT230X Basic UART"; break;
2363  default:
2364  ftdi_error_return(-3, "Unknown chip type");
2365  }
2366  eeprom->product = malloc(strlen(default_product) +1);
2367  if (eeprom->product)
2368  strcpy(eeprom->product, default_product);
2369  }
2370 
2371  if (eeprom->serial)
2372  free (eeprom->serial);
2373  eeprom->serial = NULL;
2374  if (serial)
2375  {
2376  eeprom->serial = malloc(strlen(serial)+1);
2377  if (eeprom->serial)
2378  strcpy(eeprom->serial, serial);
2379  }
2380 
2381  if (ftdi->type == TYPE_R)
2382  {
2383  eeprom->max_power = 90;
2384  eeprom->size = 0x80;
2385  eeprom->cbus_function[0] = CBUS_TXLED;
2386  eeprom->cbus_function[1] = CBUS_RXLED;
2387  eeprom->cbus_function[2] = CBUS_TXDEN;
2388  eeprom->cbus_function[3] = CBUS_PWREN;
2389  eeprom->cbus_function[4] = CBUS_SLEEP;
2390  }
2391  else if (ftdi->type == TYPE_230X)
2392  {
2393  eeprom->max_power = 90;
2394  eeprom->size = 0x100;
2395  eeprom->cbus_function[0] = CBUSH_TXDEN;
2396  eeprom->cbus_function[1] = CBUSH_RXLED;
2397  eeprom->cbus_function[2] = CBUSH_TXLED;
2398  eeprom->cbus_function[3] = CBUSH_SLEEP;
2399  }
2400  else
2401  {
2402  if(ftdi->type == TYPE_232H)
2403  {
2404  int i;
2405  for (i=0; i<10; i++)
2406  eeprom->cbus_function[i] = CBUSH_TRISTATE;
2407  }
2408  eeprom->size = -1;
2409  }
2410  switch (ftdi->type)
2411  {
2412  case TYPE_AM:
2413  eeprom->release_number = 0x0200;
2414  break;
2415  case TYPE_BM:
2416  eeprom->release_number = 0x0400;
2417  break;
2418  case TYPE_2232C:
2419  eeprom->release_number = 0x0500;
2420  break;
2421  case TYPE_R:
2422  eeprom->release_number = 0x0600;
2423  break;
2424  case TYPE_2232H:
2425  eeprom->release_number = 0x0700;
2426  break;
2427  case TYPE_4232H:
2428  eeprom->release_number = 0x0800;
2429  break;
2430  case TYPE_232H:
2431  eeprom->release_number = 0x0900;
2432  break;
2433  case TYPE_230X:
2434  eeprom->release_number = 0x1000;
2435  break;
2436  default:
2437  eeprom->release_number = 0x00;
2438  }
2439  return 0;
2440 }
2441 
2443  char * product, char * serial)
2444 {
2445  struct ftdi_eeprom *eeprom;
2446 
2447  if (ftdi == NULL)
2448  ftdi_error_return(-1, "No struct ftdi_context");
2449 
2450  if (ftdi->eeprom == NULL)
2451  ftdi_error_return(-2,"No struct ftdi_eeprom");
2452 
2453  eeprom = ftdi->eeprom;
2454 
2455  if (ftdi->usb_dev == NULL)
2456  ftdi_error_return(-3, "No connected device or device not yet opened");
2457 
2458  if (manufacturer)
2459  {
2460  if (eeprom->manufacturer)
2461  free (eeprom->manufacturer);
2462  eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2463  if (eeprom->manufacturer)
2464  strcpy(eeprom->manufacturer, manufacturer);
2465  }
2466 
2467  if(product)
2468  {
2469  if (eeprom->product)
2470  free (eeprom->product);
2471  eeprom->product = malloc(strlen(product)+1);
2472  if (eeprom->product)
2473  strcpy(eeprom->product, product);
2474  }
2475 
2476  if (serial)
2477  {
2478  if (eeprom->serial)
2479  free (eeprom->serial);
2480  eeprom->serial = malloc(strlen(serial)+1);
2481  if (eeprom->serial)
2482  {
2483  strcpy(eeprom->serial, serial);
2484  eeprom->use_serial = 1;
2485  }
2486  }
2487  return 0;
2488 }
2489 
2490 
2491 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2492 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2493 {
2494  int i;
2495  for(i=0; i<5; i++)
2496  {
2497  int mode_low, mode_high;
2498  if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2499  mode_low = CBUSH_TRISTATE;
2500  else
2501  mode_low = eeprom->cbus_function[2*i];
2502  if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2503  mode_high = CBUSH_TRISTATE;
2504  else
2505  mode_high = eeprom->cbus_function[2*i+1];
2506 
2507  output[0x18+i] = (mode_high <<4) | mode_low;
2508  }
2509 }
2510 /* Return the bits for the encoded EEPROM Structure of a requested Mode
2511  *
2512  */
2513 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2514 {
2515  switch (chip)
2516  {
2517  case TYPE_2232H:
2518  case TYPE_2232C:
2519  {
2520  switch (type)
2521  {
2522  case CHANNEL_IS_UART: return 0;
2523  case CHANNEL_IS_FIFO: return 0x01;
2524  case CHANNEL_IS_OPTO: return 0x02;
2525  case CHANNEL_IS_CPU : return 0x04;
2526  default: return 0;
2527  }
2528  }
2529  case TYPE_232H:
2530  {
2531  switch (type)
2532  {
2533  case CHANNEL_IS_UART : return 0;
2534  case CHANNEL_IS_FIFO : return 0x01;
2535  case CHANNEL_IS_OPTO : return 0x02;
2536  case CHANNEL_IS_CPU : return 0x04;
2537  case CHANNEL_IS_FT1284 : return 0x08;
2538  default: return 0;
2539  }
2540  }
2541  case TYPE_230X: /* FT230X is only UART */
2542  default: return 0;
2543  }
2544  return 0;
2545 }
2546 
2562 {
2563  unsigned char i, j, eeprom_size_mask;
2564  unsigned short checksum, value;
2565  unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2566  int user_area_size;
2567  struct ftdi_eeprom *eeprom;
2568  unsigned char * output;
2569 
2570  if (ftdi == NULL)
2571  ftdi_error_return(-2,"No context");
2572  if (ftdi->eeprom == NULL)
2573  ftdi_error_return(-2,"No eeprom structure");
2574 
2575  eeprom= ftdi->eeprom;
2576  output = eeprom->buf;
2577 
2578  if (eeprom->chip == -1)
2579  ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2580 
2581  if (eeprom->size == -1)
2582  {
2583  if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2584  eeprom->size = 0x100;
2585  else
2586  eeprom->size = 0x80;
2587  }
2588 
2589  if (eeprom->manufacturer != NULL)
2590  manufacturer_size = strlen(eeprom->manufacturer);
2591  if (eeprom->product != NULL)
2592  product_size = strlen(eeprom->product);
2593  if (eeprom->serial != NULL)
2594  serial_size = strlen(eeprom->serial);
2595 
2596  // eeprom size check
2597  switch (ftdi->type)
2598  {
2599  case TYPE_AM:
2600  case TYPE_BM:
2601  user_area_size = 96; // base size for strings (total of 48 characters)
2602  break;
2603  case TYPE_2232C:
2604  user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2605  break;
2606  case TYPE_R:
2607  case TYPE_230X:
2608  user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2609  break;
2610  case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2611  case TYPE_4232H:
2612  user_area_size = 86;
2613  break;
2614  case TYPE_232H:
2615  user_area_size = 80;
2616  break;
2617  default:
2618  user_area_size = 0;
2619  break;
2620  }
2621  user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2622 
2623  if (user_area_size < 0)
2624  ftdi_error_return(-1,"eeprom size exceeded");
2625 
2626  // empty eeprom
2627  if (ftdi->type == TYPE_230X)
2628  {
2629  /* FT230X have a reserved section in the middle of the MTP,
2630  which cannot be written to, but must be included in the checksum */
2631  memset(ftdi->eeprom->buf, 0, 0x80);
2632  memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
2633  }
2634  else
2635  {
2636  memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2637  }
2638 
2639  // Bytes and Bits set for all Types
2640 
2641  // Addr 02: Vendor ID
2642  output[0x02] = eeprom->vendor_id;
2643  output[0x03] = eeprom->vendor_id >> 8;
2644 
2645  // Addr 04: Product ID
2646  output[0x04] = eeprom->product_id;
2647  output[0x05] = eeprom->product_id >> 8;
2648 
2649  // Addr 06: Device release number (0400h for BM features)
2650  output[0x06] = eeprom->release_number;
2651  output[0x07] = eeprom->release_number >> 8;
2652 
2653  // Addr 08: Config descriptor
2654  // Bit 7: always 1
2655  // Bit 6: 1 if this device is self powered, 0 if bus powered
2656  // Bit 5: 1 if this device uses remote wakeup
2657  // Bit 4-0: reserved - 0
2658  j = 0x80;
2659  if (eeprom->self_powered)
2660  j |= 0x40;
2661  if (eeprom->remote_wakeup)
2662  j |= 0x20;
2663  output[0x08] = j;
2664 
2665  // Addr 09: Max power consumption: max power = value * 2 mA
2666  output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
2667 
2668  if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
2669  {
2670  // Addr 0A: Chip configuration
2671  // Bit 7: 0 - reserved
2672  // Bit 6: 0 - reserved
2673  // Bit 5: 0 - reserved
2674  // Bit 4: 1 - Change USB version
2675  // Bit 3: 1 - Use the serial number string
2676  // Bit 2: 1 - Enable suspend pull downs for lower power
2677  // Bit 1: 1 - Out EndPoint is Isochronous
2678  // Bit 0: 1 - In EndPoint is Isochronous
2679  //
2680  j = 0;
2681  if (eeprom->in_is_isochronous)
2682  j = j | 1;
2683  if (eeprom->out_is_isochronous)
2684  j = j | 2;
2685  output[0x0A] = j;
2686  }
2687 
2688  // Dynamic content
2689  // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2690  // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2691  // 0xa0 (TYPE_232H)
2692  i = 0;
2693  switch (ftdi->type)
2694  {
2695  case TYPE_2232H:
2696  case TYPE_4232H:
2697  i += 2;
2698  case TYPE_R:
2699  i += 2;
2700  case TYPE_2232C:
2701  i += 2;
2702  case TYPE_AM:
2703  case TYPE_BM:
2704  i += 0x94;
2705  break;
2706  case TYPE_232H:
2707  case TYPE_230X:
2708  i = 0xa0;
2709  break;
2710  }
2711  /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2712  eeprom_size_mask = eeprom->size -1;
2713 
2714  // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2715  // Addr 0F: Length of manufacturer string
2716  // Output manufacturer
2717  output[0x0E] = i; // calculate offset
2718  output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2719  output[i & eeprom_size_mask] = 0x03, i++; // type: string
2720  for (j = 0; j < manufacturer_size; j++)
2721  {
2722  output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2723  output[i & eeprom_size_mask] = 0x00, i++;
2724  }
2725  output[0x0F] = manufacturer_size*2 + 2;
2726 
2727  // Addr 10: Offset of the product string + 0x80, calculated later
2728  // Addr 11: Length of product string
2729  output[0x10] = i | 0x80; // calculate offset
2730  output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2731  output[i & eeprom_size_mask] = 0x03, i++;
2732  for (j = 0; j < product_size; j++)
2733  {
2734  output[i & eeprom_size_mask] = eeprom->product[j], i++;
2735  output[i & eeprom_size_mask] = 0x00, i++;
2736  }
2737  output[0x11] = product_size*2 + 2;
2738 
2739  // Addr 12: Offset of the serial string + 0x80, calculated later
2740  // Addr 13: Length of serial string
2741  output[0x12] = i | 0x80; // calculate offset
2742  output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2743  output[i & eeprom_size_mask] = 0x03, i++;
2744  for (j = 0; j < serial_size; j++)
2745  {
2746  output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2747  output[i & eeprom_size_mask] = 0x00, i++;
2748  }
2749 
2750  // Legacy port name and PnP fields for FT2232 and newer chips
2751  if (ftdi->type > TYPE_BM)
2752  {
2753  output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2754  i++;
2755  output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2756  i++;
2757  output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2758  i++;
2759  }
2760 
2761  output[0x13] = serial_size*2 + 2;
2762 
2763  if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2764  {
2765  if (eeprom->use_serial)
2766  output[0x0A] |= USE_SERIAL_NUM;
2767  else
2768  output[0x0A] &= ~USE_SERIAL_NUM;
2769  }
2770 
2771  /* Bytes and Bits specific to (some) types
2772  Write linear, as this allows easier fixing*/
2773  switch (ftdi->type)
2774  {
2775  case TYPE_AM:
2776  break;
2777  case TYPE_BM:
2778  output[0x0C] = eeprom->usb_version & 0xff;
2779  output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2780  if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2781  output[0x0A] |= USE_USB_VERSION_BIT;
2782  else
2783  output[0x0A] &= ~USE_USB_VERSION_BIT;
2784 
2785  break;
2786  case TYPE_2232C:
2787 
2788  output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
2789  if ( eeprom->channel_a_driver == DRIVER_VCP)
2790  output[0x00] |= DRIVER_VCP;
2791  else
2792  output[0x00] &= ~DRIVER_VCP;
2793 
2794  if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2795  output[0x00] |= HIGH_CURRENT_DRIVE;
2796  else
2797  output[0x00] &= ~HIGH_CURRENT_DRIVE;
2798 
2799  output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
2800  if ( eeprom->channel_b_driver == DRIVER_VCP)
2801  output[0x01] |= DRIVER_VCP;
2802  else
2803  output[0x01] &= ~DRIVER_VCP;
2804 
2805  if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2806  output[0x01] |= HIGH_CURRENT_DRIVE;
2807  else
2808  output[0x01] &= ~HIGH_CURRENT_DRIVE;
2809 
2810  if (eeprom->in_is_isochronous)
2811  output[0x0A] |= 0x1;
2812  else
2813  output[0x0A] &= ~0x1;
2814  if (eeprom->out_is_isochronous)
2815  output[0x0A] |= 0x2;
2816  else
2817  output[0x0A] &= ~0x2;
2818  if (eeprom->suspend_pull_downs)
2819  output[0x0A] |= 0x4;
2820  else
2821  output[0x0A] &= ~0x4;
2822  if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2823  output[0x0A] |= USE_USB_VERSION_BIT;
2824  else
2825  output[0x0A] &= ~USE_USB_VERSION_BIT;
2826 
2827  output[0x0C] = eeprom->usb_version & 0xff;
2828  output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2829  output[0x14] = eeprom->chip;
2830  break;
2831  case TYPE_R:
2832  if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2833  output[0x00] |= HIGH_CURRENT_DRIVE_R;
2834  output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2835 
2836  if (eeprom->suspend_pull_downs)
2837  output[0x0A] |= 0x4;
2838  else
2839  output[0x0A] &= ~0x4;
2840  output[0x0B] = eeprom->invert;
2841  output[0x0C] = eeprom->usb_version & 0xff;
2842  output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2843 
2844  if (eeprom->cbus_function[0] > CBUS_BB)
2845  output[0x14] = CBUS_TXLED;
2846  else
2847  output[0x14] = eeprom->cbus_function[0];
2848 
2849  if (eeprom->cbus_function[1] > CBUS_BB)
2850  output[0x14] |= CBUS_RXLED<<4;
2851  else
2852  output[0x14] |= eeprom->cbus_function[1]<<4;
2853 
2854  if (eeprom->cbus_function[2] > CBUS_BB)
2855  output[0x15] = CBUS_TXDEN;
2856  else
2857  output[0x15] = eeprom->cbus_function[2];
2858 
2859  if (eeprom->cbus_function[3] > CBUS_BB)
2860  output[0x15] |= CBUS_PWREN<<4;
2861  else
2862  output[0x15] |= eeprom->cbus_function[3]<<4;
2863 
2864  if (eeprom->cbus_function[4] > CBUS_CLK6)
2865  output[0x16] = CBUS_SLEEP;
2866  else
2867  output[0x16] = eeprom->cbus_function[4];
2868  break;
2869  case TYPE_2232H:
2870  output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
2871  if ( eeprom->channel_a_driver == DRIVER_VCP)
2872  output[0x00] |= DRIVER_VCP;
2873  else
2874  output[0x00] &= ~DRIVER_VCP;
2875 
2876  output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
2877  if ( eeprom->channel_b_driver == DRIVER_VCP)
2878  output[0x01] |= DRIVER_VCP;
2879  else
2880  output[0x01] &= ~DRIVER_VCP;
2881  if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2882  output[0x01] |= SUSPEND_DBUS7_BIT;
2883  else
2884  output[0x01] &= ~SUSPEND_DBUS7_BIT;
2885 
2886  if (eeprom->suspend_pull_downs)
2887  output[0x0A] |= 0x4;
2888  else
2889  output[0x0A] &= ~0x4;
2890 
2891  if (eeprom->group0_drive > DRIVE_16MA)
2892  output[0x0c] |= DRIVE_16MA;
2893  else
2894  output[0x0c] |= eeprom->group0_drive;
2895  if (eeprom->group0_schmitt == IS_SCHMITT)
2896  output[0x0c] |= IS_SCHMITT;
2897  if (eeprom->group0_slew == SLOW_SLEW)
2898  output[0x0c] |= SLOW_SLEW;
2899 
2900  if (eeprom->group1_drive > DRIVE_16MA)
2901  output[0x0c] |= DRIVE_16MA<<4;
2902  else
2903  output[0x0c] |= eeprom->group1_drive<<4;
2904  if (eeprom->group1_schmitt == IS_SCHMITT)
2905  output[0x0c] |= IS_SCHMITT<<4;
2906  if (eeprom->group1_slew == SLOW_SLEW)
2907  output[0x0c] |= SLOW_SLEW<<4;
2908 
2909  if (eeprom->group2_drive > DRIVE_16MA)
2910  output[0x0d] |= DRIVE_16MA;
2911  else
2912  output[0x0d] |= eeprom->group2_drive;
2913  if (eeprom->group2_schmitt == IS_SCHMITT)
2914  output[0x0d] |= IS_SCHMITT;
2915  if (eeprom->group2_slew == SLOW_SLEW)
2916  output[0x0d] |= SLOW_SLEW;
2917 
2918  if (eeprom->group3_drive > DRIVE_16MA)
2919  output[0x0d] |= DRIVE_16MA<<4;
2920  else
2921  output[0x0d] |= eeprom->group3_drive<<4;
2922  if (eeprom->group3_schmitt == IS_SCHMITT)
2923  output[0x0d] |= IS_SCHMITT<<4;
2924  if (eeprom->group3_slew == SLOW_SLEW)
2925  output[0x0d] |= SLOW_SLEW<<4;
2926 
2927  output[0x18] = eeprom->chip;
2928 
2929  break;
2930  case TYPE_4232H:
2931  if (eeprom->channel_a_driver == DRIVER_VCP)
2932  output[0x00] |= DRIVER_VCP;
2933  else
2934  output[0x00] &= ~DRIVER_VCP;
2935  if (eeprom->channel_b_driver == DRIVER_VCP)
2936  output[0x01] |= DRIVER_VCP;
2937  else
2938  output[0x01] &= ~DRIVER_VCP;
2939  if (eeprom->channel_c_driver == DRIVER_VCP)
2940  output[0x00] |= (DRIVER_VCP << 4);
2941  else
2942  output[0x00] &= ~(DRIVER_VCP << 4);
2943  if (eeprom->channel_d_driver == DRIVER_VCP)
2944  output[0x01] |= (DRIVER_VCP << 4);
2945  else
2946  output[0x01] &= ~(DRIVER_VCP << 4);
2947 
2948  if (eeprom->suspend_pull_downs)
2949  output[0x0a] |= 0x4;
2950  else
2951  output[0x0a] &= ~0x4;
2952 
2953  if (eeprom->channel_a_rs485enable)
2954  output[0x0b] |= CHANNEL_IS_RS485 << 0;
2955  else
2956  output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
2957  if (eeprom->channel_b_rs485enable)
2958  output[0x0b] |= CHANNEL_IS_RS485 << 1;
2959  else
2960  output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
2961  if (eeprom->channel_c_rs485enable)
2962  output[0x0b] |= CHANNEL_IS_RS485 << 2;
2963  else
2964  output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
2965  if (eeprom->channel_d_rs485enable)
2966  output[0x0b] |= CHANNEL_IS_RS485 << 3;
2967  else
2968  output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
2969 
2970  if (eeprom->group0_drive > DRIVE_16MA)
2971  output[0x0c] |= DRIVE_16MA;
2972  else
2973  output[0x0c] |= eeprom->group0_drive;
2974  if (eeprom->group0_schmitt == IS_SCHMITT)
2975  output[0x0c] |= IS_SCHMITT;
2976  if (eeprom->group0_slew == SLOW_SLEW)
2977  output[0x0c] |= SLOW_SLEW;
2978 
2979  if (eeprom->group1_drive > DRIVE_16MA)
2980  output[0x0c] |= DRIVE_16MA<<4;
2981  else
2982  output[0x0c] |= eeprom->group1_drive<<4;
2983  if (eeprom->group1_schmitt == IS_SCHMITT)
2984  output[0x0c] |= IS_SCHMITT<<4;
2985  if (eeprom->group1_slew == SLOW_SLEW)
2986  output[0x0c] |= SLOW_SLEW<<4;
2987 
2988  if (eeprom->group2_drive > DRIVE_16MA)
2989  output[0x0d] |= DRIVE_16MA;
2990  else
2991  output[0x0d] |= eeprom->group2_drive;
2992  if (eeprom->group2_schmitt == IS_SCHMITT)
2993  output[0x0d] |= IS_SCHMITT;
2994  if (eeprom->group2_slew == SLOW_SLEW)
2995  output[0x0d] |= SLOW_SLEW;
2996 
2997  if (eeprom->group3_drive > DRIVE_16MA)
2998  output[0x0d] |= DRIVE_16MA<<4;
2999  else
3000  output[0x0d] |= eeprom->group3_drive<<4;
3001  if (eeprom->group3_schmitt == IS_SCHMITT)
3002  output[0x0d] |= IS_SCHMITT<<4;
3003  if (eeprom->group3_slew == SLOW_SLEW)
3004  output[0x0d] |= SLOW_SLEW<<4;
3005 
3006  output[0x18] = eeprom->chip;
3007 
3008  break;
3009  case TYPE_232H:
3010  output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
3011  if ( eeprom->channel_a_driver == DRIVER_VCP)
3012  output[0x00] |= DRIVER_VCPH;
3013  else
3014  output[0x00] &= ~DRIVER_VCPH;
3015  if (eeprom->powersave)
3016  output[0x01] |= POWER_SAVE_DISABLE_H;
3017  else
3018  output[0x01] &= ~POWER_SAVE_DISABLE_H;
3019 
3020  if (eeprom->suspend_pull_downs)
3021  output[0x0a] |= 0x4;
3022  else
3023  output[0x0a] &= ~0x4;
3024 
3025  if (eeprom->clock_polarity)
3026  output[0x01] |= FT1284_CLK_IDLE_STATE;
3027  else
3028  output[0x01] &= ~FT1284_CLK_IDLE_STATE;
3029  if (eeprom->data_order)
3030  output[0x01] |= FT1284_DATA_LSB;
3031  else
3032  output[0x01] &= ~FT1284_DATA_LSB;
3033  if (eeprom->flow_control)
3034  output[0x01] |= FT1284_FLOW_CONTROL;
3035  else
3036  output[0x01] &= ~FT1284_FLOW_CONTROL;
3037  if (eeprom->group0_drive > DRIVE_16MA)
3038  output[0x0c] |= DRIVE_16MA;
3039  else
3040  output[0x0c] |= eeprom->group0_drive;
3041  if (eeprom->group0_schmitt == IS_SCHMITT)
3042  output[0x0c] |= IS_SCHMITT;
3043  if (eeprom->group0_slew == SLOW_SLEW)
3044  output[0x0c] |= SLOW_SLEW;
3045 
3046  if (eeprom->group1_drive > DRIVE_16MA)
3047  output[0x0d] |= DRIVE_16MA;
3048  else
3049  output[0x0d] |= eeprom->group1_drive;
3050  if (eeprom->group1_schmitt == IS_SCHMITT)
3051  output[0x0d] |= IS_SCHMITT;
3052  if (eeprom->group1_slew == SLOW_SLEW)
3053  output[0x0d] |= SLOW_SLEW;
3054 
3055  set_ft232h_cbus(eeprom, output);
3056 
3057  output[0x1e] = eeprom->chip;
3058  fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
3059  break;
3060  case TYPE_230X:
3061  output[0x00] = 0x80; /* Actually, leave the default value */
3062  output[0x0a] = 0x08; /* Enable USB Serial Number */
3063  /*FIXME: Make DBUS & CBUS Control configurable*/
3064  output[0x0c] = 0; /* DBUS drive 4mA, CBUS drive 4 mA like factory default */
3065  for (j = 0; j <= 6; j++)
3066  {
3067  output[0x1a + j] = eeprom->cbus_function[j];
3068  }
3069  output[0x0b] = eeprom->invert;
3070  break;
3071  }
3072 
3073  // calculate checksum
3074  checksum = 0xAAAA;
3075 
3076  for (i = 0; i < eeprom->size/2-1; i++)
3077  {
3078  if ((ftdi->type == TYPE_230X) && (i == 0x12))
3079  {
3080  /* FT230X has a user section in the MTP which is not part of the checksum */
3081  i = 0x40;
3082  }
3083  if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
3084  uint16_t data;
3085  if (ftdi_read_eeprom_location(ftdi, i, &data)) {
3086  fprintf(stderr, "Reading Factory Configuration Data failed\n");
3087  i = 0x50;
3088  }
3089  value = data;
3090  }
3091  else {
3092  value = output[i*2];
3093  value += output[(i*2)+1] << 8;
3094  }
3095  checksum = value^checksum;
3096  checksum = (checksum << 1) | (checksum >> 15);
3097  }
3098 
3099  output[eeprom->size-2] = checksum;
3100  output[eeprom->size-1] = checksum >> 8;
3101 
3103  return user_area_size;
3104 }
3105 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
3106  * EEPROM structure
3107  *
3108  * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
3109  */
3110 static unsigned char bit2type(unsigned char bits)
3111 {
3112  switch (bits)
3113  {
3114  case 0: return CHANNEL_IS_UART;
3115  case 1: return CHANNEL_IS_FIFO;
3116  case 2: return CHANNEL_IS_OPTO;
3117  case 4: return CHANNEL_IS_CPU;
3118  case 8: return CHANNEL_IS_FT1284;
3119  default:
3120  fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
3121  bits);
3122  }
3123  return 0;
3124 }
3125 /* Decode 230X / 232R type chips invert bits
3126  * Prints directly to stdout.
3127 */
3128 static void print_inverted_bits(int invert)
3129 {
3130  char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
3131  int i;
3132 
3133  fprintf(stdout,"Inverted bits:");
3134  for (i=0; i<8; i++)
3135  if ((invert & (1<<i)) == (1<<i))
3136  fprintf(stdout," %s",r_bits[i]);
3137 
3138  fprintf(stdout,"\n");
3139 }
3154 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
3155 {
3156  int i, j;
3157  unsigned short checksum, eeprom_checksum, value;
3158  unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
3159  int eeprom_size;
3160  struct ftdi_eeprom *eeprom;
3161  unsigned char *buf = NULL;
3162 
3163  if (ftdi == NULL)
3164  ftdi_error_return(-1,"No context");
3165  if (ftdi->eeprom == NULL)
3166  ftdi_error_return(-1,"No eeprom structure");
3167 
3168  eeprom = ftdi->eeprom;
3169  eeprom_size = eeprom->size;
3170  buf = ftdi->eeprom->buf;
3171 
3172  // Addr 02: Vendor ID
3173  eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
3174 
3175  // Addr 04: Product ID
3176  eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
3177 
3178  // Addr 06: Device release number
3179  eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
3180 
3181  // Addr 08: Config descriptor
3182  // Bit 7: always 1
3183  // Bit 6: 1 if this device is self powered, 0 if bus powered
3184  // Bit 5: 1 if this device uses remote wakeup
3185  eeprom->self_powered = buf[0x08] & 0x40;
3186  eeprom->remote_wakeup = buf[0x08] & 0x20;
3187 
3188  // Addr 09: Max power consumption: max power = value * 2 mA
3189  eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
3190 
3191  // Addr 0A: Chip configuration
3192  // Bit 7: 0 - reserved
3193  // Bit 6: 0 - reserved
3194  // Bit 5: 0 - reserved
3195  // Bit 4: 1 - Change USB version on BM and 2232C
3196  // Bit 3: 1 - Use the serial number string
3197  // Bit 2: 1 - Enable suspend pull downs for lower power
3198  // Bit 1: 1 - Out EndPoint is Isochronous
3199  // Bit 0: 1 - In EndPoint is Isochronous
3200  //
3201  eeprom->in_is_isochronous = buf[0x0A]&0x01;
3202  eeprom->out_is_isochronous = buf[0x0A]&0x02;
3203  eeprom->suspend_pull_downs = buf[0x0A]&0x04;
3204  eeprom->use_serial = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
3205  eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
3206 
3207  // Addr 0C: USB version low byte when 0x0A
3208  // Addr 0D: USB version high byte when 0x0A
3209  eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
3210 
3211  // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
3212  // Addr 0F: Length of manufacturer string
3213  manufacturer_size = buf[0x0F]/2;
3214  if (eeprom->manufacturer)
3215  free(eeprom->manufacturer);
3216  if (manufacturer_size > 0)
3217  {
3218  eeprom->manufacturer = malloc(manufacturer_size);
3219  if (eeprom->manufacturer)
3220  {
3221  // Decode manufacturer
3222  i = buf[0x0E] & (eeprom_size -1); // offset
3223  for (j=0; j<manufacturer_size-1; j++)
3224  {
3225  eeprom->manufacturer[j] = buf[2*j+i+2];
3226  }
3227  eeprom->manufacturer[j] = '\0';
3228  }
3229  }
3230  else eeprom->manufacturer = NULL;
3231 
3232  // Addr 10: Offset of the product string + 0x80, calculated later
3233  // Addr 11: Length of product string
3234  if (eeprom->product)
3235  free(eeprom->product);
3236  product_size = buf[0x11]/2;
3237  if (product_size > 0)
3238  {
3239  eeprom->product = malloc(product_size);
3240  if (eeprom->product)
3241  {
3242  // Decode product name
3243  i = buf[0x10] & (eeprom_size -1); // offset
3244  for (j=0; j<product_size-1; j++)
3245  {
3246  eeprom->product[j] = buf[2*j+i+2];
3247  }
3248  eeprom->product[j] = '\0';
3249  }
3250  }
3251  else eeprom->product = NULL;
3252 
3253  // Addr 12: Offset of the serial string + 0x80, calculated later
3254  // Addr 13: Length of serial string
3255  if (eeprom->serial)
3256  free(eeprom->serial);
3257  serial_size = buf[0x13]/2;
3258  if (serial_size > 0)
3259  {
3260  eeprom->serial = malloc(serial_size);
3261  if (eeprom->serial)
3262  {
3263  // Decode serial
3264  i = buf[0x12] & (eeprom_size -1); // offset
3265  for (j=0; j<serial_size-1; j++)
3266  {
3267  eeprom->serial[j] = buf[2*j+i+2];
3268  }
3269  eeprom->serial[j] = '\0';
3270  }
3271  }
3272  else eeprom->serial = NULL;
3273 
3274  // verify checksum
3275  checksum = 0xAAAA;
3276 
3277  for (i = 0; i < eeprom_size/2-1; i++)
3278  {
3279  if ((ftdi->type == TYPE_230X) && (i == 0x12))
3280  {
3281  /* FT230X has a user section in the MTP which is not part of the checksum */
3282  i = 0x40;
3283  }
3284  value = buf[i*2];
3285  value += buf[(i*2)+1] << 8;
3286 
3287  checksum = value^checksum;
3288  checksum = (checksum << 1) | (checksum >> 15);
3289  }
3290 
3291  eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3292 
3293  if (eeprom_checksum != checksum)
3294  {
3295  fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
3296  ftdi_error_return(-1,"EEPROM checksum error");
3297  }
3298 
3299  eeprom->channel_a_type = 0;
3300  if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
3301  {
3302  eeprom->chip = -1;
3303  }
3304  else if (ftdi->type == TYPE_2232C)
3305  {
3306  eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3307  eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3308  eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3309  eeprom->channel_b_type = buf[0x01] & 0x7;
3310  eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3311  eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
3312  eeprom->chip = buf[0x14];
3313  }
3314  else if (ftdi->type == TYPE_R)
3315  {
3316  /* TYPE_R flags D2XX, not VCP as all others*/
3317  eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
3318  eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
3319  if ( (buf[0x01]&0x40) != 0x40)
3320  fprintf(stderr,
3321  "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3322  " If this happened with the\n"
3323  " EEPROM programmed by FTDI tools, please report "
3324  "to libftdi@developer.intra2net.com\n");
3325 
3326  eeprom->chip = buf[0x16];
3327  // Addr 0B: Invert data lines
3328  // Works only on FT232R, not FT245R, but no way to distinguish
3329  eeprom->invert = buf[0x0B];
3330  // Addr 14: CBUS function: CBUS0, CBUS1
3331  // Addr 15: CBUS function: CBUS2, CBUS3
3332  // Addr 16: CBUS function: CBUS5
3333  eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3334  eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3335  eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3336  eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3337  eeprom->cbus_function[4] = buf[0x16] & 0x0f;
3338  }
3339  else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3340  {
3341  eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3342  eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3343 
3344  if (ftdi->type == TYPE_2232H)
3345  {
3346  eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
3347  eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
3348  eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
3349  }
3350  else
3351  {
3352  eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
3353  eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
3354  eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
3355  eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
3356  eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
3357  eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
3358  }
3359 
3360  eeprom->chip = buf[0x18];
3361  eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3362  eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3363  eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3364  eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3365  eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3366  eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3367  eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3368  eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3369  eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3370  eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3371  eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3372  eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
3373  }
3374  else if (ftdi->type == TYPE_232H)
3375  {
3376  eeprom->channel_a_type = buf[0x00] & 0xf;
3377  eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
3378  eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3379  eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3380  eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
3381  eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
3382  eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3383  eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3384  eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3385  eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3386  eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3387  eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3388 
3389  for(i=0; i<5; i++)
3390  {
3391  eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3392  eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3393  }
3394  eeprom->chip = buf[0x1e];
3395  /*FIXME: Decipher more values*/
3396  }
3397  else if (ftdi->type == TYPE_230X)
3398  {
3399  for(i=0; i<4; i++)
3400  {
3401  eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
3402  }
3403  eeprom->group0_drive = buf[0x0c] & 0x03;
3404  eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3405  eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3406  eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
3407  eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3408  eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3409 
3410  eeprom->invert = buf[0xb];
3411  }
3412 
3413  if (verbose)
3414  {
3415  char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
3416  fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3417  fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
3418  fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
3419 
3420  if (eeprom->self_powered)
3421  fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3422  else
3423  fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
3424  (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
3425  if (eeprom->manufacturer)
3426  fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
3427  if (eeprom->product)
3428  fprintf(stdout, "Product: %s\n",eeprom->product);
3429  if (eeprom->serial)
3430  fprintf(stdout, "Serial: %s\n",eeprom->serial);
3431  fprintf(stdout, "Checksum : %04x\n", checksum);
3432  if (ftdi->type == TYPE_R)
3433  fprintf(stdout, "Internal EEPROM\n");
3434  else if (eeprom->chip >= 0x46)
3435  fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
3436  if (eeprom->suspend_dbus7)
3437  fprintf(stdout, "Suspend on DBUS7\n");
3438  if (eeprom->suspend_pull_downs)
3439  fprintf(stdout, "Pull IO pins low during suspend\n");
3440  if(eeprom->powersave)
3441  {
3442  if(ftdi->type >= TYPE_232H)
3443  fprintf(stdout,"Enter low power state on ACBUS7\n");
3444  }
3445  if (eeprom->remote_wakeup)
3446  fprintf(stdout, "Enable Remote Wake Up\n");
3447  fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
3448  if (ftdi->type >= TYPE_2232C)
3449  fprintf(stdout,"Channel A has Mode %s%s%s\n",
3450  channel_mode[eeprom->channel_a_type],
3451  (eeprom->channel_a_driver)?" VCP":"",
3452  (eeprom->high_current_a)?" High Current IO":"");
3453  if (ftdi->type == TYPE_232H)
3454  {
3455  fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3456  (eeprom->clock_polarity)?"HIGH":"LOW",
3457  (eeprom->data_order)?"LSB":"MSB",
3458  (eeprom->flow_control)?"":"No ");
3459  }
3460  if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3461  fprintf(stdout,"Channel B has Mode %s%s%s\n",
3462  channel_mode[eeprom->channel_b_type],
3463  (eeprom->channel_b_driver)?" VCP":"",
3464  (eeprom->high_current_b)?" High Current IO":"");
3465  if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
3466  eeprom->use_usb_version == USE_USB_VERSION_BIT)
3467  fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3468 
3469  if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
3470  {
3471  fprintf(stdout,"%s has %d mA drive%s%s\n",
3472  (ftdi->type == TYPE_2232H)?"AL":"A",
3473  (eeprom->group0_drive+1) *4,
3474  (eeprom->group0_schmitt)?" Schmitt Input":"",
3475  (eeprom->group0_slew)?" Slow Slew":"");
3476  fprintf(stdout,"%s has %d mA drive%s%s\n",
3477  (ftdi->type == TYPE_2232H)?"AH":"B",
3478  (eeprom->group1_drive+1) *4,
3479  (eeprom->group1_schmitt)?" Schmitt Input":"",
3480  (eeprom->group1_slew)?" Slow Slew":"");
3481  fprintf(stdout,"%s has %d mA drive%s%s\n",
3482  (ftdi->type == TYPE_2232H)?"BL":"C",
3483  (eeprom->group2_drive+1) *4,
3484  (eeprom->group2_schmitt)?" Schmitt Input":"",
3485  (eeprom->group2_slew)?" Slow Slew":"");
3486  fprintf(stdout,"%s has %d mA drive%s%s\n",
3487  (ftdi->type == TYPE_2232H)?"BH":"D",
3488  (eeprom->group3_drive+1) *4,
3489  (eeprom->group3_schmitt)?" Schmitt Input":"",
3490  (eeprom->group3_slew)?" Slow Slew":"");
3491  }
3492  else if (ftdi->type == TYPE_232H)
3493  {
3494  char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3495  "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3496  "CLK30","CLK15","CLK7_5"
3497  };
3498  fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3499  (eeprom->group0_drive+1) *4,
3500  (eeprom->group0_schmitt)?" Schmitt Input":"",
3501  (eeprom->group0_slew)?" Slow Slew":"");
3502  fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3503  (eeprom->group1_drive+1) *4,
3504  (eeprom->group1_schmitt)?" Schmitt Input":"",
3505  (eeprom->group1_slew)?" Slow Slew":"");
3506  for (i=0; i<10; i++)
3507  {
3508  if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3509  fprintf(stdout,"C%d Function: %s\n", i,
3510  cbush_mux[eeprom->cbus_function[i]]);
3511  }
3512  }
3513  else if (ftdi->type == TYPE_230X)
3514  {
3515  char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3516  "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3517  "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
3518  "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
3519  "BBRD#", "TIME_STAMP", "AWAKE#",
3520  };
3521  fprintf(stdout,"DBUS has %d mA drive%s%s\n",
3522  (eeprom->group0_drive+1) *4,
3523  (eeprom->group0_schmitt)?" Schmitt Input":"",
3524  (eeprom->group0_slew)?" Slow Slew":"");
3525  fprintf(stdout,"CBUS has %d mA drive%s%s\n",
3526  (eeprom->group1_drive+1) *4,
3527  (eeprom->group1_schmitt)?" Schmitt Input":"",
3528  (eeprom->group1_slew)?" Slow Slew":"");
3529  for (i=0; i<4; i++)
3530  {
3531  if (eeprom->cbus_function[i]<= CBUSH_AWAKE)
3532  fprintf(stdout,"CBUS%d Function: %s\n", i, cbush_mux[eeprom->cbus_function[i]]);
3533  }
3534 
3535  if (eeprom->invert)
3536  print_inverted_bits(eeprom->invert);
3537  }
3538 
3539  if (ftdi->type == TYPE_R)
3540  {
3541  char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
3542  "SLEEP","CLK48","CLK24","CLK12","CLK6",
3543  "IOMODE","BB_WR","BB_RD"
3544  };
3545  char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
3546 
3547  if (eeprom->invert)
3548  print_inverted_bits(eeprom->invert);
3549 
3550  for (i=0; i<5; i++)
3551  {
3552  if (eeprom->cbus_function[i]<CBUS_BB)
3553  fprintf(stdout,"C%d Function: %s\n", i,
3554  cbus_mux[eeprom->cbus_function[i]]);
3555  else
3556  {
3557  if (i < 4)
3558  /* Running MPROG show that C0..3 have fixed function Synchronous
3559  Bit Bang mode */
3560  fprintf(stdout,"C%d BB Function: %s\n", i,
3561  cbus_BB[i]);
3562  else
3563  fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
3564  }
3565  }
3566  }
3567  }
3568  return 0;
3569 }
3570 
3581 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3582 {
3583  switch (value_name)
3584  {
3585  case VENDOR_ID:
3586  *value = ftdi->eeprom->vendor_id;
3587  break;
3588  case PRODUCT_ID:
3589  *value = ftdi->eeprom->product_id;
3590  break;
3591  case RELEASE_NUMBER:
3592  *value = ftdi->eeprom->release_number;
3593  break;
3594  case SELF_POWERED:
3595  *value = ftdi->eeprom->self_powered;
3596  break;
3597  case REMOTE_WAKEUP:
3598  *value = ftdi->eeprom->remote_wakeup;
3599  break;
3600  case IS_NOT_PNP:
3601  *value = ftdi->eeprom->is_not_pnp;
3602  break;
3603  case SUSPEND_DBUS7:
3604  *value = ftdi->eeprom->suspend_dbus7;
3605  break;
3606  case IN_IS_ISOCHRONOUS:
3607  *value = ftdi->eeprom->in_is_isochronous;
3608  break;
3609  case OUT_IS_ISOCHRONOUS:
3610  *value = ftdi->eeprom->out_is_isochronous;
3611  break;
3612  case SUSPEND_PULL_DOWNS:
3613  *value = ftdi->eeprom->suspend_pull_downs;
3614  break;
3615  case USE_SERIAL:
3616  *value = ftdi->eeprom->use_serial;
3617  break;
3618  case USB_VERSION:
3619  *value = ftdi->eeprom->usb_version;
3620  break;
3621  case USE_USB_VERSION:
3622  *value = ftdi->eeprom->use_usb_version;
3623  break;
3624  case MAX_POWER:
3625  *value = ftdi->eeprom->max_power;
3626  break;
3627  case CHANNEL_A_TYPE:
3628  *value = ftdi->eeprom->channel_a_type;
3629  break;
3630  case CHANNEL_B_TYPE:
3631  *value = ftdi->eeprom->channel_b_type;
3632  break;
3633  case CHANNEL_A_DRIVER:
3634  *value = ftdi->eeprom->channel_a_driver;
3635  break;
3636  case CHANNEL_B_DRIVER:
3637  *value = ftdi->eeprom->channel_b_driver;
3638  break;
3639  case CHANNEL_C_DRIVER:
3640  *value = ftdi->eeprom->channel_c_driver;
3641  break;
3642  case CHANNEL_D_DRIVER:
3643  *value = ftdi->eeprom->channel_d_driver;
3644  break;
3645  case CHANNEL_A_RS485:
3646  *value = ftdi->eeprom->channel_a_rs485enable;
3647  break;
3648  case CHANNEL_B_RS485:
3649  *value = ftdi->eeprom->channel_b_rs485enable;
3650  break;
3651  case CHANNEL_C_RS485:
3652  *value = ftdi->eeprom->channel_c_rs485enable;
3653  break;
3654  case CHANNEL_D_RS485:
3655  *value = ftdi->eeprom->channel_d_rs485enable;
3656  break;
3657  case CBUS_FUNCTION_0:
3658  *value = ftdi->eeprom->cbus_function[0];
3659  break;
3660  case CBUS_FUNCTION_1:
3661  *value = ftdi->eeprom->cbus_function[1];
3662  break;
3663  case CBUS_FUNCTION_2:
3664  *value = ftdi->eeprom->cbus_function[2];
3665  break;
3666  case CBUS_FUNCTION_3:
3667  *value = ftdi->eeprom->cbus_function[3];
3668  break;
3669  case CBUS_FUNCTION_4:
3670  *value = ftdi->eeprom->cbus_function[4];
3671  break;
3672  case CBUS_FUNCTION_5:
3673  *value = ftdi->eeprom->cbus_function[5];
3674  break;
3675  case CBUS_FUNCTION_6:
3676  *value = ftdi->eeprom->cbus_function[6];
3677  break;
3678  case CBUS_FUNCTION_7:
3679  *value = ftdi->eeprom->cbus_function[7];
3680  break;
3681  case CBUS_FUNCTION_8:
3682  *value = ftdi->eeprom->cbus_function[8];
3683  break;
3684  case CBUS_FUNCTION_9:
3685  *value = ftdi->eeprom->cbus_function[8];
3686  break;
3687  case HIGH_CURRENT:
3688  *value = ftdi->eeprom->high_current;
3689  break;
3690  case HIGH_CURRENT_A:
3691  *value = ftdi->eeprom->high_current_a;
3692  break;
3693  case HIGH_CURRENT_B:
3694  *value = ftdi->eeprom->high_current_b;
3695  break;
3696  case INVERT:
3697  *value = ftdi->eeprom->invert;
3698  break;
3699  case GROUP0_DRIVE:
3700  *value = ftdi->eeprom->group0_drive;
3701  break;
3702  case GROUP0_SCHMITT:
3703  *value = ftdi->eeprom->group0_schmitt;
3704  break;
3705  case GROUP0_SLEW:
3706  *value = ftdi->eeprom->group0_slew;
3707  break;
3708  case GROUP1_DRIVE:
3709  *value = ftdi->eeprom->group1_drive;
3710  break;
3711  case GROUP1_SCHMITT:
3712  *value = ftdi->eeprom->group1_schmitt;
3713  break;
3714  case GROUP1_SLEW:
3715  *value = ftdi->eeprom->group1_slew;
3716  break;
3717  case GROUP2_DRIVE:
3718  *value = ftdi->eeprom->group2_drive;
3719  break;
3720  case GROUP2_SCHMITT:
3721  *value = ftdi->eeprom->group2_schmitt;
3722  break;
3723  case GROUP2_SLEW:
3724  *value = ftdi->eeprom->group2_slew;
3725  break;
3726  case GROUP3_DRIVE:
3727  *value = ftdi->eeprom->group3_drive;
3728  break;
3729  case GROUP3_SCHMITT:
3730  *value = ftdi->eeprom->group3_schmitt;
3731  break;
3732  case GROUP3_SLEW:
3733  *value = ftdi->eeprom->group3_slew;
3734  break;
3735  case POWER_SAVE:
3736  *value = ftdi->eeprom->powersave;
3737  break;
3738  case CLOCK_POLARITY:
3739  *value = ftdi->eeprom->clock_polarity;
3740  break;
3741  case DATA_ORDER:
3742  *value = ftdi->eeprom->data_order;
3743  break;
3744  case FLOW_CONTROL:
3745  *value = ftdi->eeprom->flow_control;
3746  break;
3747  case CHIP_TYPE:
3748  *value = ftdi->eeprom->chip;
3749  break;
3750  case CHIP_SIZE:
3751  *value = ftdi->eeprom->size;
3752  break;
3753  default:
3754  ftdi_error_return(-1, "Request for unknown EEPROM value");
3755  }
3756  return 0;
3757 }
3758 
3771 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3772 {
3773  switch (value_name)
3774  {
3775  case VENDOR_ID:
3776  ftdi->eeprom->vendor_id = value;
3777  break;
3778  case PRODUCT_ID:
3779  ftdi->eeprom->product_id = value;
3780  break;
3781  case RELEASE_NUMBER:
3782  ftdi->eeprom->release_number = value;
3783  break;
3784  case SELF_POWERED:
3785  ftdi->eeprom->self_powered = value;
3786  break;
3787  case REMOTE_WAKEUP:
3788  ftdi->eeprom->remote_wakeup = value;
3789  break;
3790  case IS_NOT_PNP:
3791  ftdi->eeprom->is_not_pnp = value;
3792  break;
3793  case SUSPEND_DBUS7:
3794  ftdi->eeprom->suspend_dbus7 = value;
3795  break;
3796  case IN_IS_ISOCHRONOUS:
3797  ftdi->eeprom->in_is_isochronous = value;
3798  break;
3799  case OUT_IS_ISOCHRONOUS:
3800  ftdi->eeprom->out_is_isochronous = value;
3801  break;
3802  case SUSPEND_PULL_DOWNS:
3803  ftdi->eeprom->suspend_pull_downs = value;
3804  break;
3805  case USE_SERIAL:
3806  ftdi->eeprom->use_serial = value;
3807  break;
3808  case USB_VERSION:
3809  ftdi->eeprom->usb_version = value;
3810  break;
3811  case USE_USB_VERSION:
3812  ftdi->eeprom->use_usb_version = value;
3813  break;
3814  case MAX_POWER:
3815  ftdi->eeprom->max_power = value;
3816  break;
3817  case CHANNEL_A_TYPE:
3818  ftdi->eeprom->channel_a_type = value;
3819  break;
3820  case CHANNEL_B_TYPE:
3821  ftdi->eeprom->channel_b_type = value;
3822  break;
3823  case CHANNEL_A_DRIVER:
3824  ftdi->eeprom->channel_a_driver = value;
3825  break;
3826  case CHANNEL_B_DRIVER:
3827  ftdi->eeprom->channel_b_driver = value;
3828  break;
3829  case CHANNEL_C_DRIVER:
3830  ftdi->eeprom->channel_c_driver = value;
3831  break;
3832  case CHANNEL_D_DRIVER:
3833  ftdi->eeprom->channel_d_driver = value;
3834  break;
3835  case CHANNEL_A_RS485:
3836  ftdi->eeprom->channel_a_rs485enable = value;
3837  break;
3838  case CHANNEL_B_RS485:
3839  ftdi->eeprom->channel_b_rs485enable = value;
3840  break;
3841  case CHANNEL_C_RS485:
3842  ftdi->eeprom->channel_c_rs485enable = value;
3843  break;
3844  case CHANNEL_D_RS485:
3845  ftdi->eeprom->channel_d_rs485enable = value;
3846  break;
3847  case CBUS_FUNCTION_0:
3848  ftdi->eeprom->cbus_function[0] = value;
3849  break;
3850  case CBUS_FUNCTION_1:
3851  ftdi->eeprom->cbus_function[1] = value;
3852  break;
3853  case CBUS_FUNCTION_2:
3854  ftdi->eeprom->cbus_function[2] = value;
3855  break;
3856  case CBUS_FUNCTION_3:
3857  ftdi->eeprom->cbus_function[3] = value;
3858  break;
3859  case CBUS_FUNCTION_4:
3860  ftdi->eeprom->cbus_function[4] = value;
3861  break;
3862  case CBUS_FUNCTION_5:
3863  ftdi->eeprom->cbus_function[5] = value;
3864  break;
3865  case CBUS_FUNCTION_6:
3866  ftdi->eeprom->cbus_function[6] = value;
3867  break;
3868  case CBUS_FUNCTION_7:
3869  ftdi->eeprom->cbus_function[7] = value;
3870  break;
3871  case CBUS_FUNCTION_8:
3872  ftdi->eeprom->cbus_function[8] = value;
3873  break;
3874  case CBUS_FUNCTION_9:
3875  ftdi->eeprom->cbus_function[9] = value;
3876  break;
3877  case HIGH_CURRENT:
3878  ftdi->eeprom->high_current = value;
3879  break;
3880  case HIGH_CURRENT_A:
3881  ftdi->eeprom->high_current_a = value;
3882  break;
3883  case HIGH_CURRENT_B:
3884  ftdi->eeprom->high_current_b = value;
3885  break;
3886  case INVERT:
3887  ftdi->eeprom->invert = value;
3888  break;
3889  case GROUP0_DRIVE:
3890  ftdi->eeprom->group0_drive = value;
3891  break;
3892  case GROUP0_SCHMITT:
3893  ftdi->eeprom->group0_schmitt = value;
3894  break;
3895  case GROUP0_SLEW:
3896  ftdi->eeprom->group0_slew = value;
3897  break;
3898  case GROUP1_DRIVE:
3899  ftdi->eeprom->group1_drive = value;
3900  break;
3901  case GROUP1_SCHMITT:
3902  ftdi->eeprom->group1_schmitt = value;
3903  break;
3904  case GROUP1_SLEW:
3905  ftdi->eeprom->group1_slew = value;
3906  break;
3907  case GROUP2_DRIVE:
3908  ftdi->eeprom->group2_drive = value;
3909  break;
3910  case GROUP2_SCHMITT:
3911  ftdi->eeprom->group2_schmitt = value;
3912  break;
3913  case GROUP2_SLEW:
3914  ftdi->eeprom->group2_slew = value;
3915  break;
3916  case GROUP3_DRIVE:
3917  ftdi->eeprom->group3_drive = value;
3918  break;
3919  case GROUP3_SCHMITT:
3920  ftdi->eeprom->group3_schmitt = value;
3921  break;
3922  case GROUP3_SLEW:
3923  ftdi->eeprom->group3_slew = value;
3924  break;
3925  case CHIP_TYPE:
3926  ftdi->eeprom->chip = value;
3927  break;
3928  case POWER_SAVE:
3929  ftdi->eeprom->powersave = value;
3930  break;
3931  case CLOCK_POLARITY:
3932  ftdi->eeprom->clock_polarity = value;
3933  break;
3934  case DATA_ORDER:
3935  ftdi->eeprom->data_order = value;
3936  break;
3937  case FLOW_CONTROL:
3938  ftdi->eeprom->flow_control = value;
3939  break;
3940  case CHIP_SIZE:
3941  ftdi_error_return(-2, "EEPROM Value can't be changed");
3942  break;
3943 
3944  default :
3945  ftdi_error_return(-1, "Request to unknown EEPROM value");
3946  }
3948  return 0;
3949 }
3950 
3961 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3962 {
3963  if (!ftdi || !(ftdi->eeprom))
3964  ftdi_error_return(-1, "No appropriate structure");
3965 
3966  if (!buf || size < ftdi->eeprom->size)
3967  ftdi_error_return(-1, "Not enough room to store eeprom");
3968 
3969  // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3970  if (size > FTDI_MAX_EEPROM_SIZE)
3971  size = FTDI_MAX_EEPROM_SIZE;
3972 
3973  memcpy(buf, ftdi->eeprom->buf, size);
3974 
3975  return 0;
3976 }
3977 
3987 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
3988 {
3989  if (!ftdi || !(ftdi->eeprom) || !buf)
3990  ftdi_error_return(-1, "No appropriate structure");
3991 
3992  // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3993  if (size > FTDI_MAX_EEPROM_SIZE)
3994  size = FTDI_MAX_EEPROM_SIZE;
3995 
3996  memcpy(ftdi->eeprom->buf, buf, size);
3997 
3998  return 0;
3999 }
4000 
4012 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
4013 {
4014  if (ftdi == NULL || ftdi->usb_dev == NULL)
4015  ftdi_error_return(-2, "USB device unavailable");
4016 
4017  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)
4018  ftdi_error_return(-1, "reading eeprom failed");
4019 
4020  return 0;
4021 }
4022 
4033 {
4034  int i;
4035  unsigned char *buf;
4036 
4037  if (ftdi == NULL || ftdi->usb_dev == NULL)
4038  ftdi_error_return(-2, "USB device unavailable");
4039  buf = ftdi->eeprom->buf;
4040 
4041  for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
4042  {
4043  if (libusb_control_transfer(
4045  buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
4046  ftdi_error_return(-1, "reading eeprom failed");
4047  }
4048 
4049  if (ftdi->type == TYPE_R)
4050  ftdi->eeprom->size = 0x80;
4051  /* Guesses size of eeprom by comparing halves
4052  - will not work with blank eeprom */
4053  else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
4054  ftdi->eeprom->size = -1;
4055  else if (memcmp(buf,&buf[0x80],0x80) == 0)
4056  ftdi->eeprom->size = 0x80;
4057  else if (memcmp(buf,&buf[0x40],0x40) == 0)
4058  ftdi->eeprom->size = 0x40;
4059  else
4060  ftdi->eeprom->size = 0x100;
4061  return 0;
4062 }
4063 
4064 /*
4065  ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
4066  Function is only used internally
4067  \internal
4068 */
4069 static unsigned char ftdi_read_chipid_shift(unsigned char value)
4070 {
4071  return ((value & 1) << 1) |
4072  ((value & 2) << 5) |
4073  ((value & 4) >> 2) |
4074  ((value & 8) << 4) |
4075  ((value & 16) >> 1) |
4076  ((value & 32) >> 1) |
4077  ((value & 64) >> 4) |
4078  ((value & 128) >> 2);
4079 }
4080 
4091 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
4092 {
4093  unsigned int a = 0, b = 0;
4094 
4095  if (ftdi == NULL || ftdi->usb_dev == NULL)
4096  ftdi_error_return(-2, "USB device unavailable");
4097 
4098  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)
4099  {
4100  a = a << 8 | a >> 8;
4101  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)
4102  {
4103  b = b << 8 | b >> 8;
4104  a = (a << 16) | (b & 0xFFFF);
4105  a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
4106  | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
4107  *chipid = a ^ 0xa5f0f7d1;
4108  return 0;
4109  }
4110  }
4111 
4112  ftdi_error_return(-1, "read of FTDIChip-ID failed");
4113 }
4114 
4129 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
4130  unsigned short eeprom_val)
4131 {
4132  int chip_type_location;
4133  unsigned short chip_type;
4134 
4135  if (ftdi == NULL || ftdi->usb_dev == NULL)
4136  ftdi_error_return(-2, "USB device unavailable");
4137 
4138  if (eeprom_addr <0x80)
4139  ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
4140 
4141 
4142  switch (ftdi->type)
4143  {
4144  case TYPE_BM:
4145  case TYPE_2232C:
4146  chip_type_location = 0x14;
4147  break;
4148  case TYPE_2232H:
4149  case TYPE_4232H:
4150  chip_type_location = 0x18;
4151  break;
4152  case TYPE_232H:
4153  chip_type_location = 0x1e;
4154  break;
4155  default:
4156  ftdi_error_return(-4, "Device can't access unprotected area");
4157  }
4158 
4159  if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
4160  ftdi_error_return(-5, "Reading failed");
4161  fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
4162  if ((chip_type & 0xff) != 0x66)
4163  {
4164  ftdi_error_return(-6, "EEPROM is not of 93x66");
4165  }
4166 
4167  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4168  SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
4169  NULL, 0, ftdi->usb_write_timeout) != 0)
4170  ftdi_error_return(-1, "unable to write eeprom");
4171 
4172  return 0;
4173 }
4174 
4186 {
4187  unsigned short usb_val, status;
4188  int i, ret;
4189  unsigned char *eeprom;
4190 
4191  if (ftdi == NULL || ftdi->usb_dev == NULL)
4192  ftdi_error_return(-2, "USB device unavailable");
4193 
4195  ftdi_error_return(-3, "EEPROM not initialized for the connected device");
4196 
4197  eeprom = ftdi->eeprom->buf;
4198 
4199  /* These commands were traced while running MProg */
4200  if ((ret = ftdi_usb_reset(ftdi)) != 0)
4201  return ret;
4202  if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
4203  return ret;
4204  if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
4205  return ret;
4206 
4207  for (i = 0; i < ftdi->eeprom->size/2; i++)
4208  {
4209  /* Do not try to write to reserved area */
4210  if ((ftdi->type == TYPE_230X) && (i == 0x40))
4211  {
4212  i = 0x50;
4213  }
4214  usb_val = eeprom[i*2];
4215  usb_val += eeprom[(i*2)+1] << 8;
4216  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4217  SIO_WRITE_EEPROM_REQUEST, usb_val, i,
4218  NULL, 0, ftdi->usb_write_timeout) < 0)
4219  ftdi_error_return(-1, "unable to write eeprom");
4220  }
4221 
4222  return 0;
4223 }
4224 
4239 #define MAGIC 0x55aa
4241 {
4242  unsigned short eeprom_value;
4243  if (ftdi == NULL || ftdi->usb_dev == NULL)
4244  ftdi_error_return(-2, "USB device unavailable");
4245 
4246  if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
4247  {
4248  ftdi->eeprom->chip = 0;
4249  return 0;
4250  }
4251 
4252  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4253  0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4254  ftdi_error_return(-1, "unable to erase eeprom");
4255 
4256 
4257  /* detect chip type by writing 0x55AA as magic at word position 0xc0
4258  Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
4259  Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
4260  Chip is 93x66 if magic is only read at word position 0xc0*/
4261  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
4263  NULL, 0, ftdi->usb_write_timeout) != 0)
4264  ftdi_error_return(-3, "Writing magic failed");
4265  if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
4266  ftdi_error_return(-4, "Reading failed");
4267  if (eeprom_value == MAGIC)
4268  {
4269  ftdi->eeprom->chip = 0x46;
4270  }
4271  else
4272  {
4273  if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
4274  ftdi_error_return(-4, "Reading failed");
4275  if (eeprom_value == MAGIC)
4276  ftdi->eeprom->chip = 0x56;
4277  else
4278  {
4279  if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
4280  ftdi_error_return(-4, "Reading failed");
4281  if (eeprom_value == MAGIC)
4282  ftdi->eeprom->chip = 0x66;
4283  else
4284  {
4285  ftdi->eeprom->chip = -1;
4286  }
4287  }
4288  }
4289  if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
4290  0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
4291  ftdi_error_return(-1, "unable to erase eeprom");
4292  return 0;
4293 }
4294 
4303 {
4304  if (ftdi == NULL)
4305  return "";
4306 
4307  return ftdi->error_str;
4308 }
4309 
4310 /* @} end of doxygen libftdi group */
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:1221
#define CHANNEL_IS_FT1284
Definition: ftdi.h:391
int powersave
Definition: ftdi_i.h:120
struct ftdi_transfer_control * ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1521
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
Definition: ftdi.c:1982
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
Definition: ftdi.c:636
#define FTDI_DEVICE_OUT_REQTYPE
Definition: ftdi.h:152
enum ftdi_module_detach_mode module_detach_mode
Definition: ftdi.h:271
#define SIO_SET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:164
#define FT1284_CLK_IDLE_STATE
Definition: ftdi.h:347
int out_is_isochronous
Definition: ftdi_i.h:54
ftdi_chip_type
Definition: ftdi.h:36
Definition: ftdi.h:48
list of usb devices created by ftdi_usb_find_all()
Definition: ftdi.h:340
#define SLOW_SLEW
Definition: ftdi.h:399
struct ftdi_device_list * next
Definition: ftdi.h:343
int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
Definition: ftdi.c:2442
int usb_version
Definition: ftdi_i.h:61
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:1912
void ftdi_list_free2(struct ftdi_device_list *devlist)
Definition: ftdi.c:378
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:935
#define SIO_SET_EVENT_CHAR_REQUEST
Definition: ftdi.h:162
int ftdi_read_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
Definition: ftdi.c:4012
int clock_polarity
Definition: ftdi_i.h:122
void ftdi_deinit(struct ftdi_context *ftdi)
Definition: ftdi.c:210
#define HIGH_CURRENT_DRIVE
Definition: ftdi.h:411
void ftdi_set_usbdev(struct ftdi_context *ftdi, libusb_device_handle *usb)
Definition: ftdi.c:268
int remote_wakeup
Definition: ftdi_i.h:44
#define SIO_READ_PINS_REQUEST
Definition: ftdi.h:167
#define SIO_RESET_REQUEST
Definition: ftdi.h:156
ftdi_eeprom_value
Definition: ftdi.h:277
ftdi_interface
Definition: ftdi.h:72
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:692
#define SUSPEND_DBUS7_BIT
Definition: ftdi.h:408
Definition: ftdi.h:54
void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char *output)
Definition: ftdi.c:2492
#define SIO_SET_BAUDRATE_REQUEST
Definition: ftdi.h:157
struct libusb_transfer * transfer
Definition: ftdi.h:211
#define DRIVE_16MA
Definition: ftdi.h:398
int channel_b_driver
Definition: ftdi_i.h:81
int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
Definition: ftdi.c:1238
#define SIO_SET_DATA_REQUEST
Definition: ftdi.h:158
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:1373
#define SIO_WRITE_EEPROM_REQUEST
Definition: ftdi.h:169
int group3_drive
Definition: ftdi_i.h:116
Main context structure for all libftdi functions.
Definition: ftdi.h:219
int channel_c_driver
Definition: ftdi_i.h:82
const char * version_str
Definition: ftdi.h:449
int group3_schmitt
Definition: ftdi_i.h:117
int ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable)
Definition: ftdi.c:2235
#define DRIVER_VCP
Definition: ftdi.h:403
int max_power
Definition: ftdi_i.h:65
Definition: ftdi.h:41
#define MAX_POWER_MILLIAMP_PER_UNIT
Definition: ftdi_i.h:25
int group1_schmitt
Definition: ftdi_i.h:111
int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
Definition: ftdi.c:1933
void ftdi_list_free(struct ftdi_device_list **devlist)
Definition: ftdi.c:358
enum ftdi_chip_type type
Definition: ftdi.h:233
char * serial
Definition: ftdi_i.h:72
#define CHANNEL_IS_UART
Definition: ftdi.h:387
#define FTDI_DEVICE_IN_REQTYPE
Definition: ftdi.h:153
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:1284
int channel_b_rs485enable
Definition: ftdi_i.h:86
int cbus_function[10]
Definition: ftdi_i.h:92
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
Definition: ftdi.c:909
#define DRIVER_VCPH
Definition: ftdi.h:404
struct ftdi_context * ftdi_new(void)
Definition: ftdi.c:128
int ftdi_erase_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4240
Definition: ftdi.h:45
Definition: ftdi.h:39
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
Definition: ftdi.c:157
#define C_CLK
int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char *manufacturer, char *product, char *serial)
Definition: ftdi.c:2294
int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
Definition: ftdi.c:1662
int size
Definition: ftdi_i.h:128
unsigned int readbuffer_remaining
Definition: ftdi.h:243
int group0_slew
Definition: ftdi_i.h:109
int ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable)
Definition: ftdi.c:2264
#define USE_SERIAL_NUM
Definition: ftdi.h:352
int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
Definition: ftdi.c:3154
int vendor_id
Definition: ftdi_i.h:33
int in_is_isochronous
Definition: ftdi_i.h:52
int out_ep
Definition: ftdi.h:259
#define CHANNEL_IS_FIFO
Definition: ftdi.h:388
struct libusb_device * dev
Definition: ftdi.h:345
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
Definition: ftdi.c:3581
int chip
Definition: ftdi_i.h:130
#define SIO_GET_LATENCY_TIMER_REQUEST
Definition: ftdi.h:165
int initialized_for_connected_device
Definition: ftdi_i.h:39
ftdi_bits_type
Definition: ftdi.h:52
Definition: ftdi.h:44
int high_current_b
Definition: ftdi_i.h:98
#define FT1284_FLOW_CONTROL
Definition: ftdi.h:349
int channel_c_rs485enable
Definition: ftdi_i.h:87
int ftdi_init(struct ftdi_context *ftdi)
Definition: ftdi.c:88
char * error_str
Definition: ftdi.h:268
int group0_schmitt
Definition: ftdi_i.h:108
int flow_control
Definition: ftdi_i.h:124
unsigned char * readbuffer
Definition: ftdi.h:239
#define USE_USB_VERSION_BIT
Definition: ftdi.h:406
struct ftdi_transfer_control * ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1583
struct libusb_context * usb_ctx
Definition: ftdi.h:223
unsigned char buf[FTDI_MAX_EEPROM_SIZE]
Definition: ftdi_i.h:131
int usb_write_timeout
Definition: ftdi.h:229
int suspend_dbus7
Definition: ftdi_i.h:49
unsigned int writebuffer_chunksize
Definition: ftdi.h:247
#define CHANNEL_IS_RS485
Definition: ftdi.h:393
Definition: ftdi.h:38
#define SIO_READ_EEPROM_REQUEST
Definition: ftdi.h:168
Definition: ftdi.h:48
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
Definition: ftdi.c:2089
#define FTDI_MAX_EEPROM_SIZE
Definition: ftdi_i.h:22
int group1_drive
Definition: ftdi_i.h:110
int usb_read_timeout
Definition: ftdi.h:227
int index
Definition: ftdi.h:255
int channel_d_driver
Definition: ftdi_i.h:83
int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
Definition: ftdi.c:2199
int in_ep
Definition: ftdi.h:258
#define HIGH_CURRENT_DRIVE_R
Definition: ftdi.h:412
#define IS_SCHMITT
Definition: ftdi.h:400
#define SIO_SET_FLOW_CTRL_REQUEST
Definition: ftdi.h:159
int high_current_a
Definition: ftdi_i.h:96
#define FT1284_DATA_LSB
Definition: ftdi.h:348
char * manufacturer
Definition: ftdi_i.h:68
#define SIO_SET_BITMODE_REQUEST
Definition: ftdi.h:166
int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char *buf, int size)
Definition: ftdi.c:3987
#define SIO_SET_ERROR_CHAR_REQUEST
Definition: ftdi.h:163
int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:1708
int channel_b_type
Definition: ftdi_i.h:78
unsigned char bitbang_mode
Definition: ftdi.h:262
#define CHANNEL_IS_CPU
Definition: ftdi.h:390
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
Definition: ftdi.c:523
struct ftdi_context * ftdi
Definition: ftdi.h:210
int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
Definition: ftdi.c:3771
Definition: ftdi.h:48
int group0_drive
Definition: ftdi_i.h:107
int group2_slew
Definition: ftdi_i.h:115
int suspend_pull_downs
Definition: ftdi_i.h:56
const char * snapshot_str
Definition: ftdi.h:450
#define POWER_SAVE_DISABLE_H
Definition: ftdi.h:350
int product_id
Definition: ftdi_i.h:35
#define SIO_SET_MODEM_CTRL_REQUEST
Definition: ftdi.h:160
#define SIO_POLL_MODEM_STATUS_REQUEST
Definition: ftdi.h:161
Definition: ftdi.h:48
int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
Definition: ftdi.c:2115
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char *description, const char *serial)
Definition: ftdi.c:662
char * product
Definition: ftdi_i.h:70
int invert
Definition: ftdi_i.h:100
Definition: ftdi.h:48
struct libusb_device_handle * usb_dev
Definition: ftdi.h:225
int interface
Definition: ftdi.h:253
ftdi_break_type
Definition: ftdi.h:54
int ftdi_usb_close(struct ftdi_context *ftdi)
Definition: ftdi.c:987
int channel_d_rs485enable
Definition: ftdi_i.h:88
unsigned int max_packet_size
Definition: ftdi.h:249
int ftdi_setrts(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2168
int ftdi_usb_reset(struct ftdi_context *ftdi)
Definition: ftdi.c:883
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
Definition: ftdi.c:2035
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
int channel_a_type
Definition: ftdi_i.h:77
ftdi_parity_type
Definition: ftdi.h:48
int use_serial
Definition: ftdi_i.h:59
int group2_schmitt
Definition: ftdi_i.h:114
unsigned char * buf
Definition: ftdi.h:207
struct ftdi_version_info ftdi_get_library_version(void)
Get libftdi library version.
Definition: ftdi.c:281
int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
Definition: ftdi.c:1875
#define SIO_RESET_PURGE_RX
Definition: ftdi.h:174
int data_order
Definition: ftdi_i.h:123
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
Definition: ftdi.c:1726
#define SIO_SET_DTR_HIGH
Definition: ftdi.h:183
#define SIO_RESET_SIO
Definition: ftdi.h:173
unsigned char bitbang_enabled
Definition: ftdi.h:237
int channel_a_rs485enable
Definition: ftdi_i.h:85
int ftdi_write_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4185
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char *description)
Definition: ftdi.c:790
int ftdi_disable_bitbang(struct ftdi_context *ftdi)
Definition: ftdi.c:1959
int self_powered
Definition: ftdi_i.h:42
int ftdi_setdtr(struct ftdi_context *ftdi, int state)
Definition: ftdi.c:2138
int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
Definition: ftdi.c:2008
ftdi_stopbits_type
Definition: ftdi.h:50
FTDI eeprom structure.
Definition: ftdi_i.h:30
#define CHANNEL_IS_OPTO
Definition: ftdi.h:389
#define SIO_RESET_PURGE_TX
Definition: ftdi.h:175
Definition: ftdi.h:357
int high_current
Definition: ftdi_i.h:94
unsigned int readbuffer_chunksize
Definition: ftdi.h:245
int channel_a_driver
Definition: ftdi_i.h:80
int ftdi_read_eeprom(struct ftdi_context *ftdi)
Definition: ftdi.c:4032
char * ftdi_get_error_string(struct ftdi_context *ftdi)
Definition: ftdi.c:4302
int baudrate
Definition: ftdi.h:235
#define SIO_ERASE_EEPROM_REQUEST
Definition: ftdi.h:170
int ftdi_eeprom_build(struct ftdi_context *ftdi)
Definition: ftdi.c:2561
Definition: ftdi.h:309
int release_number
Definition: ftdi_i.h:134
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
Definition: ftdi.c:958
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:1750
int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
Definition: ftdi.c:4129
#define ftdi_error_return_free_device_list(code, str, devs)
Definition: ftdi.c:49
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char *buf, int size)
Definition: ftdi.c:3961
int group3_slew
Definition: ftdi_i.h:118
#define H_CLK
Definition: ftdi.h:54
#define ftdi_error_return(code, str)
Definition: ftdi.c:41
unsigned int readbuffer_offset
Definition: ftdi.h:241
int group1_slew
Definition: ftdi_i.h:112
void ftdi_free(struct ftdi_context *ftdi)
Definition: ftdi.c:256
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
Definition: ftdi.c:4091
int group2_drive
Definition: ftdi_i.h:113
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
Definition: ftdi.c:310
#define SIO_SET_RTS_HIGH
Definition: ftdi.h:186
#define SIO_SET_RTS_LOW
Definition: ftdi.h:187
#define MAGIC
Definition: ftdi.c:4239
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:1303
int is_not_pnp
Definition: ftdi_i.h:46
#define SIO_SET_DTR_LOW
Definition: ftdi.h:184
struct ftdi_eeprom * eeprom
Definition: ftdi.h:265
int use_usb_version
Definition: ftdi_i.h:63