00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00029
00030
00031 #include <libusb.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036
00037 #include "ftdi_i.h"
00038 #include "ftdi.h"
00039 #include "ftdi_version_i.h"
00040
00041 #define ftdi_error_return(code, str) do { \
00042 if ( ftdi ) \
00043 ftdi->error_str = str; \
00044 else \
00045 fprintf(stderr, str); \
00046 return code; \
00047 } while(0);
00048
00049 #define ftdi_error_return_free_device_list(code, str, devs) do { \
00050 libusb_free_device_list(devs,1); \
00051 ftdi->error_str = str; \
00052 return code; \
00053 } while(0);
00054
00055
00065 static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
00066 {
00067 if (ftdi && ftdi->usb_dev)
00068 {
00069 libusb_close (ftdi->usb_dev);
00070 ftdi->usb_dev = NULL;
00071 if(ftdi->eeprom)
00072 ftdi->eeprom->initialized_for_connected_device = 0;
00073 }
00074 }
00075
00088 int ftdi_init(struct ftdi_context *ftdi)
00089 {
00090 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
00091 ftdi->usb_ctx = NULL;
00092 ftdi->usb_dev = NULL;
00093 ftdi->usb_read_timeout = 5000;
00094 ftdi->usb_write_timeout = 5000;
00095
00096 ftdi->type = TYPE_BM;
00097 ftdi->baudrate = -1;
00098 ftdi->bitbang_enabled = 0;
00099
00100 ftdi->readbuffer = NULL;
00101 ftdi->readbuffer_offset = 0;
00102 ftdi->readbuffer_remaining = 0;
00103 ftdi->writebuffer_chunksize = 4096;
00104 ftdi->max_packet_size = 0;
00105 ftdi->error_str = NULL;
00106 ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
00107
00108 if (libusb_init(&ftdi->usb_ctx) < 0)
00109 ftdi_error_return(-3, "libusb_init() failed");
00110
00111 ftdi_set_interface(ftdi, INTERFACE_ANY);
00112 ftdi->bitbang_mode = 1;
00113
00114 if (eeprom == 0)
00115 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
00116 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
00117 ftdi->eeprom = eeprom;
00118
00119
00120 return ftdi_read_data_set_chunksize(ftdi, 4096);
00121 }
00122
00128 struct ftdi_context *ftdi_new(void)
00129 {
00130 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
00131
00132 if (ftdi == NULL)
00133 {
00134 return NULL;
00135 }
00136
00137 if (ftdi_init(ftdi) != 0)
00138 {
00139 free(ftdi);
00140 return NULL;
00141 }
00142
00143 return ftdi;
00144 }
00145
00157 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
00158 {
00159 if (ftdi == NULL)
00160 ftdi_error_return(-2, "USB device unavailable");
00161
00162 if (ftdi->usb_dev != NULL)
00163 {
00164 int check_interface = interface;
00165 if (check_interface == INTERFACE_ANY)
00166 check_interface = INTERFACE_A;
00167
00168 if (ftdi->index != check_interface)
00169 ftdi_error_return(-3, "Interface can not be changed on an already open device");
00170 }
00171
00172 switch (interface)
00173 {
00174 case INTERFACE_ANY:
00175 case INTERFACE_A:
00176 ftdi->interface = 0;
00177 ftdi->index = INTERFACE_A;
00178 ftdi->in_ep = 0x02;
00179 ftdi->out_ep = 0x81;
00180 break;
00181 case INTERFACE_B:
00182 ftdi->interface = 1;
00183 ftdi->index = INTERFACE_B;
00184 ftdi->in_ep = 0x04;
00185 ftdi->out_ep = 0x83;
00186 break;
00187 case INTERFACE_C:
00188 ftdi->interface = 2;
00189 ftdi->index = INTERFACE_C;
00190 ftdi->in_ep = 0x06;
00191 ftdi->out_ep = 0x85;
00192 break;
00193 case INTERFACE_D:
00194 ftdi->interface = 3;
00195 ftdi->index = INTERFACE_D;
00196 ftdi->in_ep = 0x08;
00197 ftdi->out_ep = 0x87;
00198 break;
00199 default:
00200 ftdi_error_return(-1, "Unknown interface");
00201 }
00202 return 0;
00203 }
00204
00210 void ftdi_deinit(struct ftdi_context *ftdi)
00211 {
00212 if (ftdi == NULL)
00213 return;
00214
00215 ftdi_usb_close_internal (ftdi);
00216
00217 if (ftdi->readbuffer != NULL)
00218 {
00219 free(ftdi->readbuffer);
00220 ftdi->readbuffer = NULL;
00221 }
00222
00223 if (ftdi->eeprom != NULL)
00224 {
00225 if (ftdi->eeprom->manufacturer != 0)
00226 {
00227 free(ftdi->eeprom->manufacturer);
00228 ftdi->eeprom->manufacturer = 0;
00229 }
00230 if (ftdi->eeprom->product != 0)
00231 {
00232 free(ftdi->eeprom->product);
00233 ftdi->eeprom->product = 0;
00234 }
00235 if (ftdi->eeprom->serial != 0)
00236 {
00237 free(ftdi->eeprom->serial);
00238 ftdi->eeprom->serial = 0;
00239 }
00240 free(ftdi->eeprom);
00241 ftdi->eeprom = NULL;
00242 }
00243
00244 if (ftdi->usb_ctx)
00245 {
00246 libusb_exit(ftdi->usb_ctx);
00247 ftdi->usb_ctx = NULL;
00248 }
00249 }
00250
00256 void ftdi_free(struct ftdi_context *ftdi)
00257 {
00258 ftdi_deinit(ftdi);
00259 free(ftdi);
00260 }
00261
00268 void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
00269 {
00270 if (ftdi == NULL)
00271 return;
00272
00273 ftdi->usb_dev = usb;
00274 }
00275
00281 struct ftdi_version_info ftdi_get_library_version(void)
00282 {
00283 struct ftdi_version_info ver;
00284
00285 ver.major = FTDI_MAJOR_VERSION;
00286 ver.minor = FTDI_MINOR_VERSION;
00287 ver.micro = FTDI_MICRO_VERSION;
00288 ver.version_str = FTDI_VERSION_STRING;
00289 ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
00290
00291 return ver;
00292 }
00293
00310 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
00311 {
00312 struct ftdi_device_list **curdev;
00313 libusb_device *dev;
00314 libusb_device **devs;
00315 int count = 0;
00316 int i = 0;
00317
00318 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00319 ftdi_error_return(-5, "libusb_get_device_list() failed");
00320
00321 curdev = devlist;
00322 *curdev = NULL;
00323
00324 while ((dev = devs[i++]) != NULL)
00325 {
00326 struct libusb_device_descriptor desc;
00327
00328 if (libusb_get_device_descriptor(dev, &desc) < 0)
00329 ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
00330
00331 if (((vendor || product) &&
00332 desc.idVendor == vendor && desc.idProduct == product) ||
00333 (!(vendor || product) &&
00334 (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
00335 || desc.idProduct == 0x6011 || desc.idProduct == 0x6014
00336 || desc.idProduct == 0x6015)))
00337 {
00338 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
00339 if (!*curdev)
00340 ftdi_error_return_free_device_list(-3, "out of memory", devs);
00341
00342 (*curdev)->next = NULL;
00343 (*curdev)->dev = dev;
00344 libusb_ref_device(dev);
00345 curdev = &(*curdev)->next;
00346 count++;
00347 }
00348 }
00349 libusb_free_device_list(devs,1);
00350 return count;
00351 }
00352
00358 void ftdi_list_free(struct ftdi_device_list **devlist)
00359 {
00360 struct ftdi_device_list *curdev, *next;
00361
00362 for (curdev = *devlist; curdev != NULL;)
00363 {
00364 next = curdev->next;
00365 libusb_unref_device(curdev->dev);
00366 free(curdev);
00367 curdev = next;
00368 }
00369
00370 *devlist = NULL;
00371 }
00372
00378 void ftdi_list_free2(struct ftdi_device_list *devlist)
00379 {
00380 ftdi_list_free(&devlist);
00381 }
00382
00409 int ftdi_usb_get_strings(struct ftdi_context *ftdi,
00410 struct libusb_device *dev,
00411 char *manufacturer, int mnf_len,
00412 char *description, int desc_len,
00413 char *serial, int serial_len)
00414 {
00415 int ret;
00416
00417 if ((ftdi==NULL) || (dev==NULL))
00418 return -1;
00419
00420 if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
00421 ftdi_error_return(-4, "libusb_open() failed");
00422
00423
00424
00425
00426
00427 ret = ftdi_usb_get_strings2(ftdi, dev,
00428 manufacturer, mnf_len,
00429 description, desc_len,
00430 serial, serial_len);
00431
00432
00433
00434 if (ret == 0)
00435 ftdi_usb_close_internal(ftdi);
00436
00437 return ret;
00438 }
00439
00466 int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev,
00467 char *manufacturer, int mnf_len,
00468 char *description, int desc_len,
00469 char *serial, int serial_len)
00470 {
00471 struct libusb_device_descriptor desc;
00472 char need_open;
00473
00474 if ((ftdi==NULL) || (dev==NULL))
00475 return -1;
00476
00477 need_open = (ftdi->usb_dev == NULL);
00478 if (need_open && libusb_open(dev, &ftdi->usb_dev) < 0)
00479 ftdi_error_return(-4, "libusb_open() failed");
00480
00481 if (libusb_get_device_descriptor(dev, &desc) < 0)
00482 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
00483
00484 if (manufacturer != NULL)
00485 {
00486 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
00487 {
00488 ftdi_usb_close_internal (ftdi);
00489 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
00490 }
00491 }
00492
00493 if (description != NULL)
00494 {
00495 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
00496 {
00497 ftdi_usb_close_internal (ftdi);
00498 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
00499 }
00500 }
00501
00502 if (serial != NULL)
00503 {
00504 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
00505 {
00506 ftdi_usb_close_internal (ftdi);
00507 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
00508 }
00509 }
00510
00511 if (need_open)
00512 ftdi_usb_close_internal (ftdi);
00513
00514 return 0;
00515 }
00516
00523 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
00524 {
00525 struct libusb_device_descriptor desc;
00526 struct libusb_config_descriptor *config0;
00527 unsigned int packet_size;
00528
00529
00530 if (ftdi == NULL || dev == NULL)
00531 return 64;
00532
00533
00534
00535
00536 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
00537 packet_size = 512;
00538 else
00539 packet_size = 64;
00540
00541 if (libusb_get_device_descriptor(dev, &desc) < 0)
00542 return packet_size;
00543
00544 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
00545 return packet_size;
00546
00547 if (desc.bNumConfigurations > 0)
00548 {
00549 if (ftdi->interface < config0->bNumInterfaces)
00550 {
00551 struct libusb_interface interface = config0->interface[ftdi->interface];
00552 if (interface.num_altsetting > 0)
00553 {
00554 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
00555 if (descriptor.bNumEndpoints > 0)
00556 {
00557 packet_size = descriptor.endpoint[0].wMaxPacketSize;
00558 }
00559 }
00560 }
00561 }
00562
00563 libusb_free_config_descriptor (config0);
00564 return packet_size;
00565 }
00566
00585 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
00586 {
00587 struct libusb_device_descriptor desc;
00588 struct libusb_config_descriptor *config0;
00589 int cfg, cfg0, detach_errno = 0;
00590
00591 if (ftdi == NULL)
00592 ftdi_error_return(-8, "ftdi context invalid");
00593
00594 if (libusb_open(dev, &ftdi->usb_dev) < 0)
00595 ftdi_error_return(-4, "libusb_open() failed");
00596
00597 if (libusb_get_device_descriptor(dev, &desc) < 0)
00598 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
00599
00600 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
00601 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
00602 cfg0 = config0->bConfigurationValue;
00603 libusb_free_config_descriptor (config0);
00604
00605
00606
00607
00608
00609
00610
00611 if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
00612 {
00613 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
00614 detach_errno = errno;
00615 }
00616
00617 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
00618 ftdi_error_return(-12, "libusb_get_configuration () failed");
00619
00620
00621
00622 if (desc.bNumConfigurations > 0 && cfg != cfg0)
00623 {
00624 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
00625 {
00626 ftdi_usb_close_internal (ftdi);
00627 if (detach_errno == EPERM)
00628 {
00629 ftdi_error_return(-8, "inappropriate permissions on device!");
00630 }
00631 else
00632 {
00633 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
00634 }
00635 }
00636 }
00637
00638 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
00639 {
00640 ftdi_usb_close_internal (ftdi);
00641 if (detach_errno == EPERM)
00642 {
00643 ftdi_error_return(-8, "inappropriate permissions on device!");
00644 }
00645 else
00646 {
00647 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
00648 }
00649 }
00650
00651 if (ftdi_usb_reset (ftdi) != 0)
00652 {
00653 ftdi_usb_close_internal (ftdi);
00654 ftdi_error_return(-6, "ftdi_usb_reset failed");
00655 }
00656
00657
00658
00659 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
00660 && desc.iSerialNumber == 0))
00661 ftdi->type = TYPE_BM;
00662 else if (desc.bcdDevice == 0x200)
00663 ftdi->type = TYPE_AM;
00664 else if (desc.bcdDevice == 0x500)
00665 ftdi->type = TYPE_2232C;
00666 else if (desc.bcdDevice == 0x600)
00667 ftdi->type = TYPE_R;
00668 else if (desc.bcdDevice == 0x700)
00669 ftdi->type = TYPE_2232H;
00670 else if (desc.bcdDevice == 0x800)
00671 ftdi->type = TYPE_4232H;
00672 else if (desc.bcdDevice == 0x900)
00673 ftdi->type = TYPE_232H;
00674 else if (desc.bcdDevice == 0x1000)
00675 ftdi->type = TYPE_230X;
00676
00677
00678 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
00679
00680 if (ftdi_set_baudrate (ftdi, 9600) != 0)
00681 {
00682 ftdi_usb_close_internal (ftdi);
00683 ftdi_error_return(-7, "set baudrate failed");
00684 }
00685
00686 ftdi_error_return(0, "all fine");
00687 }
00688
00698 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
00699 {
00700 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
00701 }
00702
00724 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00725 const char* description, const char* serial)
00726 {
00727 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
00728 }
00729
00755 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00756 const char* description, const char* serial, unsigned int index)
00757 {
00758 libusb_device *dev;
00759 libusb_device **devs;
00760 char string[256];
00761 int i = 0;
00762
00763 if (ftdi == NULL)
00764 ftdi_error_return(-11, "ftdi context invalid");
00765
00766 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00767 ftdi_error_return(-12, "libusb_get_device_list() failed");
00768
00769 while ((dev = devs[i++]) != NULL)
00770 {
00771 struct libusb_device_descriptor desc;
00772 int res;
00773
00774 if (libusb_get_device_descriptor(dev, &desc) < 0)
00775 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
00776
00777 if (desc.idVendor == vendor && desc.idProduct == product)
00778 {
00779 if (libusb_open(dev, &ftdi->usb_dev) < 0)
00780 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
00781
00782 if (description != NULL)
00783 {
00784 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
00785 {
00786 ftdi_usb_close_internal (ftdi);
00787 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
00788 }
00789 if (strncmp(string, description, sizeof(string)) != 0)
00790 {
00791 ftdi_usb_close_internal (ftdi);
00792 continue;
00793 }
00794 }
00795 if (serial != NULL)
00796 {
00797 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
00798 {
00799 ftdi_usb_close_internal (ftdi);
00800 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
00801 }
00802 if (strncmp(string, serial, sizeof(string)) != 0)
00803 {
00804 ftdi_usb_close_internal (ftdi);
00805 continue;
00806 }
00807 }
00808
00809 ftdi_usb_close_internal (ftdi);
00810
00811 if (index > 0)
00812 {
00813 index--;
00814 continue;
00815 }
00816
00817 res = ftdi_usb_open_dev(ftdi, dev);
00818 libusb_free_device_list(devs,1);
00819 return res;
00820 }
00821 }
00822
00823
00824 ftdi_error_return_free_device_list(-3, "device not found", devs);
00825 }
00826
00848 int ftdi_usb_open_bus_addr(struct ftdi_context *ftdi, uint8_t bus, uint8_t addr)
00849 {
00850 libusb_device *dev;
00851 libusb_device **devs;
00852 int i = 0;
00853
00854 if (ftdi == NULL)
00855 ftdi_error_return(-11, "ftdi context invalid");
00856
00857 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00858 ftdi_error_return(-12, "libusb_get_device_list() failed");
00859
00860 while ((dev = devs[i++]) != NULL)
00861 {
00862 if (libusb_get_bus_number(dev) == bus && libusb_get_device_address(dev) == addr)
00863 {
00864 int res;
00865 res = ftdi_usb_open_dev(ftdi, dev);
00866 libusb_free_device_list(devs,1);
00867 return res;
00868 }
00869 }
00870
00871
00872 ftdi_error_return_free_device_list(-3, "device not found", devs);
00873 }
00874
00901 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
00902 {
00903 if (ftdi == NULL)
00904 ftdi_error_return(-12, "ftdi context invalid");
00905
00906 if (description[0] == 0 || description[1] != ':')
00907 ftdi_error_return(-11, "illegal description format");
00908
00909 if (description[0] == 'd')
00910 {
00911 libusb_device *dev;
00912 libusb_device **devs;
00913 unsigned int bus_number, device_address;
00914 int i = 0;
00915
00916 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00917 ftdi_error_return(-2, "libusb_get_device_list() failed");
00918
00919
00920 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
00921 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
00922
00923 while ((dev = devs[i++]) != NULL)
00924 {
00925 int ret;
00926 if (bus_number == libusb_get_bus_number (dev)
00927 && device_address == libusb_get_device_address (dev))
00928 {
00929 ret = ftdi_usb_open_dev(ftdi, dev);
00930 libusb_free_device_list(devs,1);
00931 return ret;
00932 }
00933 }
00934
00935
00936 ftdi_error_return_free_device_list(-3, "device not found", devs);
00937 }
00938 else if (description[0] == 'i' || description[0] == 's')
00939 {
00940 unsigned int vendor;
00941 unsigned int product;
00942 unsigned int index=0;
00943 const char *serial=NULL;
00944 const char *startp, *endp;
00945
00946 errno=0;
00947 startp=description+2;
00948 vendor=strtoul((char*)startp,(char**)&endp,0);
00949 if (*endp != ':' || endp == startp || errno != 0)
00950 ftdi_error_return(-11, "illegal description format");
00951
00952 startp=endp+1;
00953 product=strtoul((char*)startp,(char**)&endp,0);
00954 if (endp == startp || errno != 0)
00955 ftdi_error_return(-11, "illegal description format");
00956
00957 if (description[0] == 'i' && *endp != 0)
00958 {
00959
00960 if (*endp != ':')
00961 ftdi_error_return(-11, "illegal description format");
00962
00963 startp=endp+1;
00964 index=strtoul((char*)startp,(char**)&endp,0);
00965 if (*endp != 0 || endp == startp || errno != 0)
00966 ftdi_error_return(-11, "illegal description format");
00967 }
00968 if (description[0] == 's')
00969 {
00970 if (*endp != ':')
00971 ftdi_error_return(-11, "illegal description format");
00972
00973
00974 serial=endp+1;
00975 }
00976
00977 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
00978 }
00979 else
00980 {
00981 ftdi_error_return(-11, "illegal description format");
00982 }
00983 }
00984
00994 int ftdi_usb_reset(struct ftdi_context *ftdi)
00995 {
00996 if (ftdi == NULL || ftdi->usb_dev == NULL)
00997 ftdi_error_return(-2, "USB device unavailable");
00998
00999 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01000 SIO_RESET_REQUEST, SIO_RESET_SIO,
01001 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
01002 ftdi_error_return(-1,"FTDI reset failed");
01003
01004
01005 ftdi->readbuffer_offset = 0;
01006 ftdi->readbuffer_remaining = 0;
01007
01008 return 0;
01009 }
01010
01020 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
01021 {
01022 if (ftdi == NULL || ftdi->usb_dev == NULL)
01023 ftdi_error_return(-2, "USB device unavailable");
01024
01025 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01026 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
01027 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
01028 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
01029
01030
01031 ftdi->readbuffer_offset = 0;
01032 ftdi->readbuffer_remaining = 0;
01033
01034 return 0;
01035 }
01036
01046 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
01047 {
01048 if (ftdi == NULL || ftdi->usb_dev == NULL)
01049 ftdi_error_return(-2, "USB device unavailable");
01050
01051 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01052 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
01053 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
01054 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
01055
01056 return 0;
01057 }
01058
01069 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
01070 {
01071 int result;
01072
01073 if (ftdi == NULL || ftdi->usb_dev == NULL)
01074 ftdi_error_return(-3, "USB device unavailable");
01075
01076 result = ftdi_usb_purge_rx_buffer(ftdi);
01077 if (result < 0)
01078 return -1;
01079
01080 result = ftdi_usb_purge_tx_buffer(ftdi);
01081 if (result < 0)
01082 return -2;
01083
01084 return 0;
01085 }
01086
01087
01088
01098 int ftdi_usb_close(struct ftdi_context *ftdi)
01099 {
01100 int rtn = 0;
01101
01102 if (ftdi == NULL)
01103 ftdi_error_return(-3, "ftdi context invalid");
01104
01105 if (ftdi->usb_dev != NULL)
01106 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
01107 rtn = -1;
01108
01109 ftdi_usb_close_internal (ftdi);
01110
01111 return rtn;
01112 }
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
01127
01128 {
01129 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
01130 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
01131 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
01132 int divisor, best_divisor, best_baud, best_baud_diff;
01133 int i;
01134 divisor = 24000000 / baudrate;
01135
01136
01137 divisor -= am_adjust_dn[divisor & 7];
01138
01139
01140 best_divisor = 0;
01141 best_baud = 0;
01142 best_baud_diff = 0;
01143 for (i = 0; i < 2; i++)
01144 {
01145 int try_divisor = divisor + i;
01146 int baud_estimate;
01147 int baud_diff;
01148
01149
01150 if (try_divisor <= 8)
01151 {
01152
01153 try_divisor = 8;
01154 }
01155 else if (divisor < 16)
01156 {
01157
01158 try_divisor = 16;
01159 }
01160 else
01161 {
01162
01163 try_divisor += am_adjust_up[try_divisor & 7];
01164 if (try_divisor > 0x1FFF8)
01165 {
01166
01167 try_divisor = 0x1FFF8;
01168 }
01169 }
01170
01171 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
01172
01173 if (baud_estimate < baudrate)
01174 {
01175 baud_diff = baudrate - baud_estimate;
01176 }
01177 else
01178 {
01179 baud_diff = baud_estimate - baudrate;
01180 }
01181 if (i == 0 || baud_diff < best_baud_diff)
01182 {
01183
01184 best_divisor = try_divisor;
01185 best_baud = baud_estimate;
01186 best_baud_diff = baud_diff;
01187 if (baud_diff == 0)
01188 {
01189
01190 break;
01191 }
01192 }
01193 }
01194
01195 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
01196
01197 if (*encoded_divisor == 1)
01198 {
01199 *encoded_divisor = 0;
01200 }
01201 else if (*encoded_divisor == 0x4001)
01202 {
01203 *encoded_divisor = 1;
01204 }
01205 return best_baud;
01206 }
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
01233 {
01234 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
01235 int best_baud = 0;
01236 int divisor, best_divisor;
01237 if (baudrate >= clk/clk_div)
01238 {
01239 *encoded_divisor = 0;
01240 best_baud = clk/clk_div;
01241 }
01242 else if (baudrate >= clk/(clk_div + clk_div/2))
01243 {
01244 *encoded_divisor = 1;
01245 best_baud = clk/(clk_div + clk_div/2);
01246 }
01247 else if (baudrate >= clk/(2*clk_div))
01248 {
01249 *encoded_divisor = 2;
01250 best_baud = clk/(2*clk_div);
01251 }
01252 else
01253 {
01254
01255 divisor = clk*16/clk_div / baudrate;
01256 if (divisor & 1)
01257 best_divisor = divisor /2 +1;
01258 else
01259 best_divisor = divisor/2;
01260 if(best_divisor > 0x20000)
01261 best_divisor = 0x1ffff;
01262 best_baud = clk*16/clk_div/best_divisor;
01263 if (best_baud & 1)
01264 best_baud = best_baud /2 +1;
01265 else
01266 best_baud = best_baud /2;
01267 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
01268 }
01269 return best_baud;
01270 }
01276 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
01277 unsigned short *value, unsigned short *index)
01278 {
01279 int best_baud;
01280 unsigned long encoded_divisor;
01281
01282 if (baudrate <= 0)
01283 {
01284
01285 return -1;
01286 }
01287
01288 #define H_CLK 120000000
01289 #define C_CLK 48000000
01290 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H))
01291 {
01292 if(baudrate*10 > H_CLK /0x3fff)
01293 {
01294
01295
01296
01297
01298
01299 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
01300 encoded_divisor |= 0x20000;
01301 }
01302 else
01303 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
01304 }
01305 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
01306 {
01307 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
01308 }
01309 else
01310 {
01311 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
01312 }
01313
01314 *value = (unsigned short)(encoded_divisor & 0xFFFF);
01315 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
01316 {
01317 *index = (unsigned short)(encoded_divisor >> 8);
01318 *index &= 0xFF00;
01319 *index |= ftdi->index;
01320 }
01321 else
01322 *index = (unsigned short)(encoded_divisor >> 16);
01323
01324
01325 return best_baud;
01326 }
01327
01332 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
01333 unsigned short *value, unsigned short *index)
01334 {
01335 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
01336 }
01337
01349 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
01350 {
01351 unsigned short value, index;
01352 int actual_baudrate;
01353
01354 if (ftdi == NULL || ftdi->usb_dev == NULL)
01355 ftdi_error_return(-3, "USB device unavailable");
01356
01357 if (ftdi->bitbang_enabled)
01358 {
01359 baudrate = baudrate*4;
01360 }
01361
01362 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
01363 if (actual_baudrate <= 0)
01364 ftdi_error_return (-1, "Silly baudrate <= 0.");
01365
01366
01367 if ((actual_baudrate * 2 < baudrate )
01368 || ((actual_baudrate < baudrate)
01369 ? (actual_baudrate * 21 < baudrate * 20)
01370 : (baudrate * 21 < actual_baudrate * 20)))
01371 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
01372
01373 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01374 SIO_SET_BAUDRATE_REQUEST, value,
01375 index, NULL, 0, ftdi->usb_write_timeout) < 0)
01376 ftdi_error_return (-2, "Setting new baudrate failed");
01377
01378 ftdi->baudrate = baudrate;
01379 return 0;
01380 }
01381
01395 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01396 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
01397 {
01398 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
01399 }
01400
01414 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01415 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
01416 enum ftdi_break_type break_type)
01417 {
01418 unsigned short value = bits;
01419
01420 if (ftdi == NULL || ftdi->usb_dev == NULL)
01421 ftdi_error_return(-2, "USB device unavailable");
01422
01423 switch (parity)
01424 {
01425 case NONE:
01426 value |= (0x00 << 8);
01427 break;
01428 case ODD:
01429 value |= (0x01 << 8);
01430 break;
01431 case EVEN:
01432 value |= (0x02 << 8);
01433 break;
01434 case MARK:
01435 value |= (0x03 << 8);
01436 break;
01437 case SPACE:
01438 value |= (0x04 << 8);
01439 break;
01440 }
01441
01442 switch (sbit)
01443 {
01444 case STOP_BIT_1:
01445 value |= (0x00 << 11);
01446 break;
01447 case STOP_BIT_15:
01448 value |= (0x01 << 11);
01449 break;
01450 case STOP_BIT_2:
01451 value |= (0x02 << 11);
01452 break;
01453 }
01454
01455 switch (break_type)
01456 {
01457 case BREAK_OFF:
01458 value |= (0x00 << 14);
01459 break;
01460 case BREAK_ON:
01461 value |= (0x01 << 14);
01462 break;
01463 }
01464
01465 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01466 SIO_SET_DATA_REQUEST, value,
01467 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
01468 ftdi_error_return (-1, "Setting new line property failed");
01469
01470 return 0;
01471 }
01472
01484 int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)
01485 {
01486 int offset = 0;
01487 int actual_length;
01488
01489 if (ftdi == NULL || ftdi->usb_dev == NULL)
01490 ftdi_error_return(-666, "USB device unavailable");
01491
01492 while (offset < size)
01493 {
01494 int write_size = ftdi->writebuffer_chunksize;
01495
01496 if (offset+write_size > size)
01497 write_size = size-offset;
01498
01499 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
01500 ftdi_error_return(-1, "usb bulk write failed");
01501
01502 offset += actual_length;
01503 }
01504
01505 return offset;
01506 }
01507
01508 static void LIBUSB_CALL ftdi_read_data_cb(struct libusb_transfer *transfer)
01509 {
01510 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
01511 struct ftdi_context *ftdi = tc->ftdi;
01512 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
01513
01514 packet_size = ftdi->max_packet_size;
01515
01516 actual_length = transfer->actual_length;
01517
01518 if (actual_length > 2)
01519 {
01520
01521
01522 num_of_chunks = actual_length / packet_size;
01523 chunk_remains = actual_length % packet_size;
01524
01525
01526 ftdi->readbuffer_offset += 2;
01527 actual_length -= 2;
01528
01529 if (actual_length > packet_size - 2)
01530 {
01531 for (i = 1; i < num_of_chunks; i++)
01532 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01533 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01534 packet_size - 2);
01535 if (chunk_remains > 2)
01536 {
01537 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01538 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01539 chunk_remains-2);
01540 actual_length -= 2*num_of_chunks;
01541 }
01542 else
01543 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
01544 }
01545
01546 if (actual_length > 0)
01547 {
01548
01549 if (tc->offset + actual_length <= tc->size)
01550 {
01551 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
01552
01553 tc->offset += actual_length;
01554
01555 ftdi->readbuffer_offset = 0;
01556 ftdi->readbuffer_remaining = 0;
01557
01558
01559 if (tc->offset == tc->size)
01560 {
01561
01562
01563 tc->completed = 1;
01564 return;
01565 }
01566 }
01567 else
01568 {
01569
01570 int part_size = tc->size - tc->offset;
01571 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
01572 tc->offset += part_size;
01573
01574 ftdi->readbuffer_offset += part_size;
01575 ftdi->readbuffer_remaining = actual_length - part_size;
01576
01577
01578
01579 tc->completed = 1;
01580 return;
01581 }
01582 }
01583 }
01584
01585 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
01586 tc->completed = LIBUSB_TRANSFER_CANCELLED;
01587 else
01588 {
01589 ret = libusb_submit_transfer (transfer);
01590 if (ret < 0)
01591 tc->completed = 1;
01592 }
01593 }
01594
01595
01596 static void LIBUSB_CALL ftdi_write_data_cb(struct libusb_transfer *transfer)
01597 {
01598 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
01599 struct ftdi_context *ftdi = tc->ftdi;
01600
01601 tc->offset += transfer->actual_length;
01602
01603 if (tc->offset == tc->size)
01604 {
01605 tc->completed = 1;
01606 }
01607 else
01608 {
01609 int write_size = ftdi->writebuffer_chunksize;
01610 int ret;
01611
01612 if (tc->offset + write_size > tc->size)
01613 write_size = tc->size - tc->offset;
01614
01615 transfer->length = write_size;
01616 transfer->buffer = tc->buf + tc->offset;
01617
01618 if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
01619 tc->completed = LIBUSB_TRANSFER_CANCELLED;
01620 else
01621 {
01622 ret = libusb_submit_transfer (transfer);
01623 if (ret < 0)
01624 tc->completed = 1;
01625 }
01626 }
01627 }
01628
01629
01644 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
01645 {
01646 struct ftdi_transfer_control *tc;
01647 struct libusb_transfer *transfer;
01648 int write_size, ret;
01649
01650 if (ftdi == NULL || ftdi->usb_dev == NULL)
01651 return NULL;
01652
01653 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
01654 if (!tc)
01655 return NULL;
01656
01657 transfer = libusb_alloc_transfer(0);
01658 if (!transfer)
01659 {
01660 free(tc);
01661 return NULL;
01662 }
01663
01664 tc->ftdi = ftdi;
01665 tc->completed = 0;
01666 tc->buf = buf;
01667 tc->size = size;
01668 tc->offset = 0;
01669
01670 if (size < (int)ftdi->writebuffer_chunksize)
01671 write_size = size;
01672 else
01673 write_size = ftdi->writebuffer_chunksize;
01674
01675 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
01676 write_size, ftdi_write_data_cb, tc,
01677 ftdi->usb_write_timeout);
01678 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01679
01680 ret = libusb_submit_transfer(transfer);
01681 if (ret < 0)
01682 {
01683 libusb_free_transfer(transfer);
01684 free(tc);
01685 return NULL;
01686 }
01687 tc->transfer = transfer;
01688
01689 return tc;
01690 }
01691
01706 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
01707 {
01708 struct ftdi_transfer_control *tc;
01709 struct libusb_transfer *transfer;
01710 int ret;
01711
01712 if (ftdi == NULL || ftdi->usb_dev == NULL)
01713 return NULL;
01714
01715 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
01716 if (!tc)
01717 return NULL;
01718
01719 tc->ftdi = ftdi;
01720 tc->buf = buf;
01721 tc->size = size;
01722
01723 if (size <= (int)ftdi->readbuffer_remaining)
01724 {
01725 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
01726
01727
01728 ftdi->readbuffer_remaining -= size;
01729 ftdi->readbuffer_offset += size;
01730
01731
01732
01733 tc->completed = 1;
01734 tc->offset = size;
01735 tc->transfer = NULL;
01736 return tc;
01737 }
01738
01739 tc->completed = 0;
01740 if (ftdi->readbuffer_remaining != 0)
01741 {
01742 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
01743
01744 tc->offset = ftdi->readbuffer_remaining;
01745 }
01746 else
01747 tc->offset = 0;
01748
01749 transfer = libusb_alloc_transfer(0);
01750 if (!transfer)
01751 {
01752 free (tc);
01753 return NULL;
01754 }
01755
01756 ftdi->readbuffer_remaining = 0;
01757 ftdi->readbuffer_offset = 0;
01758
01759 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);
01760 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01761
01762 ret = libusb_submit_transfer(transfer);
01763 if (ret < 0)
01764 {
01765 libusb_free_transfer(transfer);
01766 free (tc);
01767 return NULL;
01768 }
01769 tc->transfer = transfer;
01770
01771 return tc;
01772 }
01773
01785 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
01786 {
01787 int ret;
01788 struct timeval to = { 0, 0 };
01789 while (!tc->completed)
01790 {
01791 ret = libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
01792 &to, &tc->completed);
01793 if (ret < 0)
01794 {
01795 if (ret == LIBUSB_ERROR_INTERRUPTED)
01796 continue;
01797 libusb_cancel_transfer(tc->transfer);
01798 while (!tc->completed)
01799 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx,
01800 &to, &tc->completed) < 0)
01801 break;
01802 libusb_free_transfer(tc->transfer);
01803 free (tc);
01804 return ret;
01805 }
01806 }
01807
01808 ret = tc->offset;
01813 if (tc->transfer)
01814 {
01815 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
01816 ret = -1;
01817 libusb_free_transfer(tc->transfer);
01818 }
01819 free(tc);
01820 return ret;
01821 }
01822
01832 void ftdi_transfer_data_cancel(struct ftdi_transfer_control *tc,
01833 struct timeval * to)
01834 {
01835 struct timeval tv = { 0, 0 };
01836
01837 if (!tc->completed && tc->transfer != NULL)
01838 {
01839 if (to == NULL)
01840 to = &tv;
01841
01842 libusb_cancel_transfer(tc->transfer);
01843 while (!tc->completed)
01844 {
01845 if (libusb_handle_events_timeout_completed(tc->ftdi->usb_ctx, to, &tc->completed) < 0)
01846 break;
01847 }
01848 }
01849
01850 if (tc->transfer)
01851 libusb_free_transfer(tc->transfer);
01852
01853 free (tc);
01854 }
01855
01866 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
01867 {
01868 if (ftdi == NULL)
01869 ftdi_error_return(-1, "ftdi context invalid");
01870
01871 ftdi->writebuffer_chunksize = chunksize;
01872 return 0;
01873 }
01874
01884 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
01885 {
01886 if (ftdi == NULL)
01887 ftdi_error_return(-1, "ftdi context invalid");
01888
01889 *chunksize = ftdi->writebuffer_chunksize;
01890 return 0;
01891 }
01892
01908 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
01909 {
01910 int offset = 0, ret, i, num_of_chunks, chunk_remains;
01911 int packet_size = ftdi->max_packet_size;
01912 int actual_length = 1;
01913
01914 if (ftdi == NULL || ftdi->usb_dev == NULL)
01915 ftdi_error_return(-666, "USB device unavailable");
01916
01917
01918 if (packet_size == 0)
01919 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
01920
01921
01922 if (size <= (int)ftdi->readbuffer_remaining)
01923 {
01924 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
01925
01926
01927 ftdi->readbuffer_remaining -= size;
01928 ftdi->readbuffer_offset += size;
01929
01930
01931
01932 return size;
01933 }
01934
01935 if (ftdi->readbuffer_remaining != 0)
01936 {
01937 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
01938
01939
01940 offset += ftdi->readbuffer_remaining;
01941 }
01942
01943 while (offset < size && actual_length > 0)
01944 {
01945 ftdi->readbuffer_remaining = 0;
01946 ftdi->readbuffer_offset = 0;
01947
01948 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
01949 if (ret < 0)
01950 ftdi_error_return(ret, "usb bulk read failed");
01951
01952 if (actual_length > 2)
01953 {
01954
01955
01956 num_of_chunks = actual_length / packet_size;
01957 chunk_remains = actual_length % packet_size;
01958
01959
01960 ftdi->readbuffer_offset += 2;
01961 actual_length -= 2;
01962
01963 if (actual_length > packet_size - 2)
01964 {
01965 for (i = 1; i < num_of_chunks; i++)
01966 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01967 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01968 packet_size - 2);
01969 if (chunk_remains > 2)
01970 {
01971 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01972 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01973 chunk_remains-2);
01974 actual_length -= 2*num_of_chunks;
01975 }
01976 else
01977 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
01978 }
01979 }
01980 else if (actual_length <= 2)
01981 {
01982
01983 return offset;
01984 }
01985 if (actual_length > 0)
01986 {
01987
01988 if (offset+actual_length <= size)
01989 {
01990 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
01991
01992 offset += actual_length;
01993
01994
01995 if (offset == size)
01996
01997
01998 return offset;
01999 }
02000 else
02001 {
02002
02003 int part_size = size-offset;
02004 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
02005
02006 ftdi->readbuffer_offset += part_size;
02007 ftdi->readbuffer_remaining = actual_length-part_size;
02008 offset += part_size;
02009
02010
02011
02012
02013 return offset;
02014 }
02015 }
02016 }
02017
02018 return -127;
02019 }
02020
02033 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
02034 {
02035 unsigned char *new_buf;
02036
02037 if (ftdi == NULL)
02038 ftdi_error_return(-1, "ftdi context invalid");
02039
02040
02041 ftdi->readbuffer_offset = 0;
02042 ftdi->readbuffer_remaining = 0;
02043 #ifdef __linux__
02044
02045
02046
02047
02048 if (chunksize > 16384)
02049 chunksize = 16384;
02050 #endif
02051
02052 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
02053 ftdi_error_return(-1, "out of memory for readbuffer");
02054
02055 ftdi->readbuffer = new_buf;
02056 ftdi->readbuffer_chunksize = chunksize;
02057
02058 return 0;
02059 }
02060
02070 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
02071 {
02072 if (ftdi == NULL)
02073 ftdi_error_return(-1, "FTDI context invalid");
02074
02075 *chunksize = ftdi->readbuffer_chunksize;
02076 return 0;
02077 }
02078
02091 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
02092 {
02093 unsigned short usb_val;
02094
02095 if (ftdi == NULL || ftdi->usb_dev == NULL)
02096 ftdi_error_return(-2, "USB device unavailable");
02097
02098 usb_val = bitmask;
02099 usb_val |= (mode << 8);
02100 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)
02101 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
02102
02103 ftdi->bitbang_mode = mode;
02104 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
02105 return 0;
02106 }
02107
02117 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
02118 {
02119 if (ftdi == NULL || ftdi->usb_dev == NULL)
02120 ftdi_error_return(-2, "USB device unavailable");
02121
02122 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)
02123 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
02124
02125 ftdi->bitbang_enabled = 0;
02126 return 0;
02127 }
02128
02129
02140 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
02141 {
02142 if (ftdi == NULL || ftdi->usb_dev == NULL)
02143 ftdi_error_return(-2, "USB device unavailable");
02144
02145 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)
02146 ftdi_error_return(-1, "read pins failed");
02147
02148 return 0;
02149 }
02150
02166 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
02167 {
02168 unsigned short usb_val;
02169
02170 if (latency < 1)
02171 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
02172
02173 if (ftdi == NULL || ftdi->usb_dev == NULL)
02174 ftdi_error_return(-3, "USB device unavailable");
02175
02176 usb_val = latency;
02177 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)
02178 ftdi_error_return(-2, "unable to set latency timer");
02179
02180 return 0;
02181 }
02182
02193 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
02194 {
02195 unsigned short usb_val;
02196
02197 if (ftdi == NULL || ftdi->usb_dev == NULL)
02198 ftdi_error_return(-2, "USB device unavailable");
02199
02200 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)
02201 ftdi_error_return(-1, "reading latency timer failed");
02202
02203 *latency = (unsigned char)usb_val;
02204 return 0;
02205 }
02206
02247 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
02248 {
02249 char usb_val[2];
02250
02251 if (ftdi == NULL || ftdi->usb_dev == NULL)
02252 ftdi_error_return(-2, "USB device unavailable");
02253
02254 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)
02255 ftdi_error_return(-1, "getting modem status failed");
02256
02257 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
02258
02259 return 0;
02260 }
02261
02273 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
02274 {
02275 if (ftdi == NULL || ftdi->usb_dev == NULL)
02276 ftdi_error_return(-2, "USB device unavailable");
02277
02278 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02279 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
02280 NULL, 0, ftdi->usb_write_timeout) < 0)
02281 ftdi_error_return(-1, "set flow control failed");
02282
02283 return 0;
02284 }
02285
02296 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
02297 {
02298 unsigned short usb_val;
02299
02300 if (ftdi == NULL || ftdi->usb_dev == NULL)
02301 ftdi_error_return(-2, "USB device unavailable");
02302
02303 if (state)
02304 usb_val = SIO_SET_DTR_HIGH;
02305 else
02306 usb_val = SIO_SET_DTR_LOW;
02307
02308 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02309 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02310 NULL, 0, ftdi->usb_write_timeout) < 0)
02311 ftdi_error_return(-1, "set dtr failed");
02312
02313 return 0;
02314 }
02315
02326 int ftdi_setrts(struct ftdi_context *ftdi, int state)
02327 {
02328 unsigned short usb_val;
02329
02330 if (ftdi == NULL || ftdi->usb_dev == NULL)
02331 ftdi_error_return(-2, "USB device unavailable");
02332
02333 if (state)
02334 usb_val = SIO_SET_RTS_HIGH;
02335 else
02336 usb_val = SIO_SET_RTS_LOW;
02337
02338 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02339 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02340 NULL, 0, ftdi->usb_write_timeout) < 0)
02341 ftdi_error_return(-1, "set of rts failed");
02342
02343 return 0;
02344 }
02345
02357 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
02358 {
02359 unsigned short usb_val;
02360
02361 if (ftdi == NULL || ftdi->usb_dev == NULL)
02362 ftdi_error_return(-2, "USB device unavailable");
02363
02364 if (dtr)
02365 usb_val = SIO_SET_DTR_HIGH;
02366 else
02367 usb_val = SIO_SET_DTR_LOW;
02368
02369 if (rts)
02370 usb_val |= SIO_SET_RTS_HIGH;
02371 else
02372 usb_val |= SIO_SET_RTS_LOW;
02373
02374 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02375 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02376 NULL, 0, ftdi->usb_write_timeout) < 0)
02377 ftdi_error_return(-1, "set of rts/dtr failed");
02378
02379 return 0;
02380 }
02381
02393 int ftdi_set_event_char(struct ftdi_context *ftdi,
02394 unsigned char eventch, unsigned char enable)
02395 {
02396 unsigned short usb_val;
02397
02398 if (ftdi == NULL || ftdi->usb_dev == NULL)
02399 ftdi_error_return(-2, "USB device unavailable");
02400
02401 usb_val = eventch;
02402 if (enable)
02403 usb_val |= 1 << 8;
02404
02405 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)
02406 ftdi_error_return(-1, "setting event character failed");
02407
02408 return 0;
02409 }
02410
02422 int ftdi_set_error_char(struct ftdi_context *ftdi,
02423 unsigned char errorch, unsigned char enable)
02424 {
02425 unsigned short usb_val;
02426
02427 if (ftdi == NULL || ftdi->usb_dev == NULL)
02428 ftdi_error_return(-2, "USB device unavailable");
02429
02430 usb_val = errorch;
02431 if (enable)
02432 usb_val |= 1 << 8;
02433
02434 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)
02435 ftdi_error_return(-1, "setting error character failed");
02436
02437 return 0;
02438 }
02439
02452 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
02453 char * product, char * serial)
02454 {
02455 struct ftdi_eeprom *eeprom;
02456
02457 if (ftdi == NULL)
02458 ftdi_error_return(-1, "No struct ftdi_context");
02459
02460 if (ftdi->eeprom == NULL)
02461 ftdi_error_return(-2,"No struct ftdi_eeprom");
02462
02463 eeprom = ftdi->eeprom;
02464 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
02465
02466 if (ftdi->usb_dev == NULL)
02467 ftdi_error_return(-3, "No connected device or device not yet opened");
02468
02469 eeprom->vendor_id = 0x0403;
02470 eeprom->use_serial = 1;
02471 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
02472 (ftdi->type == TYPE_R))
02473 eeprom->product_id = 0x6001;
02474 else if (ftdi->type == TYPE_4232H)
02475 eeprom->product_id = 0x6011;
02476 else if (ftdi->type == TYPE_232H)
02477 eeprom->product_id = 0x6014;
02478 else if (ftdi->type == TYPE_230X)
02479 eeprom->product_id = 0x6015;
02480 else
02481 eeprom->product_id = 0x6010;
02482
02483 if (ftdi->type == TYPE_AM)
02484 eeprom->usb_version = 0x0101;
02485 else
02486 eeprom->usb_version = 0x0200;
02487 eeprom->max_power = 100;
02488
02489 if (eeprom->manufacturer)
02490 free (eeprom->manufacturer);
02491 eeprom->manufacturer = NULL;
02492 if (manufacturer)
02493 {
02494 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
02495 if (eeprom->manufacturer)
02496 strcpy(eeprom->manufacturer, manufacturer);
02497 }
02498
02499 if (eeprom->product)
02500 free (eeprom->product);
02501 eeprom->product = NULL;
02502 if(product)
02503 {
02504 eeprom->product = (char *)malloc(strlen(product)+1);
02505 if (eeprom->product)
02506 strcpy(eeprom->product, product);
02507 }
02508 else
02509 {
02510 const char* default_product;
02511 switch(ftdi->type)
02512 {
02513 case TYPE_AM: default_product = "AM"; break;
02514 case TYPE_BM: default_product = "BM"; break;
02515 case TYPE_2232C: default_product = "Dual RS232"; break;
02516 case TYPE_R: default_product = "FT232R USB UART"; break;
02517 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
02518 case TYPE_4232H: default_product = "FT4232H"; break;
02519 case TYPE_232H: default_product = "Single-RS232-HS"; break;
02520 case TYPE_230X: default_product = "FT230X Basic UART"; break;
02521 default:
02522 ftdi_error_return(-3, "Unknown chip type");
02523 }
02524 eeprom->product = (char *)malloc(strlen(default_product) +1);
02525 if (eeprom->product)
02526 strcpy(eeprom->product, default_product);
02527 }
02528
02529 if (eeprom->serial)
02530 free (eeprom->serial);
02531 eeprom->serial = NULL;
02532 if (serial)
02533 {
02534 eeprom->serial = (char *)malloc(strlen(serial)+1);
02535 if (eeprom->serial)
02536 strcpy(eeprom->serial, serial);
02537 }
02538
02539 if (ftdi->type == TYPE_R)
02540 {
02541 eeprom->max_power = 90;
02542 eeprom->size = 0x80;
02543 eeprom->cbus_function[0] = CBUS_TXLED;
02544 eeprom->cbus_function[1] = CBUS_RXLED;
02545 eeprom->cbus_function[2] = CBUS_TXDEN;
02546 eeprom->cbus_function[3] = CBUS_PWREN;
02547 eeprom->cbus_function[4] = CBUS_SLEEP;
02548 }
02549 else if (ftdi->type == TYPE_230X)
02550 {
02551 eeprom->max_power = 90;
02552 eeprom->size = 0x100;
02553 eeprom->cbus_function[0] = CBUSX_TXDEN;
02554 eeprom->cbus_function[1] = CBUSX_RXLED;
02555 eeprom->cbus_function[2] = CBUSX_TXLED;
02556 eeprom->cbus_function[3] = CBUSX_SLEEP;
02557 }
02558 else
02559 {
02560 if(ftdi->type == TYPE_232H)
02561 {
02562 int i;
02563 for (i=0; i<10; i++)
02564 eeprom->cbus_function[i] = CBUSH_TRISTATE;
02565 }
02566 eeprom->size = -1;
02567 }
02568 switch (ftdi->type)
02569 {
02570 case TYPE_AM:
02571 eeprom->release_number = 0x0200;
02572 break;
02573 case TYPE_BM:
02574 eeprom->release_number = 0x0400;
02575 break;
02576 case TYPE_2232C:
02577 eeprom->release_number = 0x0500;
02578 break;
02579 case TYPE_R:
02580 eeprom->release_number = 0x0600;
02581 break;
02582 case TYPE_2232H:
02583 eeprom->release_number = 0x0700;
02584 break;
02585 case TYPE_4232H:
02586 eeprom->release_number = 0x0800;
02587 break;
02588 case TYPE_232H:
02589 eeprom->release_number = 0x0900;
02590 break;
02591 case TYPE_230X:
02592 eeprom->release_number = 0x1000;
02593 break;
02594 default:
02595 eeprom->release_number = 0x00;
02596 }
02597 return 0;
02598 }
02599
02600 int ftdi_eeprom_set_strings(struct ftdi_context *ftdi, char * manufacturer,
02601 char * product, char * serial)
02602 {
02603 struct ftdi_eeprom *eeprom;
02604
02605 if (ftdi == NULL)
02606 ftdi_error_return(-1, "No struct ftdi_context");
02607
02608 if (ftdi->eeprom == NULL)
02609 ftdi_error_return(-2,"No struct ftdi_eeprom");
02610
02611 eeprom = ftdi->eeprom;
02612
02613 if (ftdi->usb_dev == NULL)
02614 ftdi_error_return(-3, "No connected device or device not yet opened");
02615
02616 if (manufacturer)
02617 {
02618 if (eeprom->manufacturer)
02619 free (eeprom->manufacturer);
02620 eeprom->manufacturer = (char *)malloc(strlen(manufacturer)+1);
02621 if (eeprom->manufacturer)
02622 strcpy(eeprom->manufacturer, manufacturer);
02623 }
02624
02625 if(product)
02626 {
02627 if (eeprom->product)
02628 free (eeprom->product);
02629 eeprom->product = (char *)malloc(strlen(product)+1);
02630 if (eeprom->product)
02631 strcpy(eeprom->product, product);
02632 }
02633
02634 if (serial)
02635 {
02636 if (eeprom->serial)
02637 free (eeprom->serial);
02638 eeprom->serial = (char *)malloc(strlen(serial)+1);
02639 if (eeprom->serial)
02640 {
02641 strcpy(eeprom->serial, serial);
02642 eeprom->use_serial = 1;
02643 }
02644 }
02645 return 0;
02646 }
02647
02666 int ftdi_eeprom_get_strings(struct ftdi_context *ftdi,
02667 char *manufacturer, int mnf_len,
02668 char *product, int prod_len,
02669 char *serial, int serial_len)
02670 {
02671 struct ftdi_eeprom *eeprom;
02672
02673 if (ftdi == NULL)
02674 ftdi_error_return(-1, "No struct ftdi_context");
02675 if (ftdi->eeprom == NULL)
02676 ftdi_error_return(-2, "No struct ftdi_eeprom");
02677
02678 eeprom = ftdi->eeprom;
02679
02680 if (manufacturer)
02681 {
02682 strncpy(manufacturer, eeprom->manufacturer, mnf_len);
02683 if (mnf_len > 0)
02684 manufacturer[mnf_len - 1] = '\0';
02685 }
02686
02687 if (product)
02688 {
02689 strncpy(product, eeprom->product, prod_len);
02690 if (prod_len > 0)
02691 product[prod_len - 1] = '\0';
02692 }
02693
02694 if (serial)
02695 {
02696 strncpy(serial, eeprom->serial, serial_len);
02697 if (serial_len > 0)
02698 serial[serial_len - 1] = '\0';
02699 }
02700
02701 return 0;
02702 }
02703
02704
02705 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
02706 {
02707 int i;
02708 for(i=0; i<5; i++)
02709 {
02710 int mode_low, mode_high;
02711 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
02712 mode_low = CBUSH_TRISTATE;
02713 else
02714 mode_low = eeprom->cbus_function[2*i];
02715 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
02716 mode_high = CBUSH_TRISTATE;
02717 else
02718 mode_high = eeprom->cbus_function[2*i+1];
02719
02720 output[0x18+i] = (mode_high <<4) | mode_low;
02721 }
02722 }
02723
02724
02725
02726 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
02727 {
02728 switch (chip)
02729 {
02730 case TYPE_2232H:
02731 case TYPE_2232C:
02732 {
02733 switch (type)
02734 {
02735 case CHANNEL_IS_UART: return 0;
02736 case CHANNEL_IS_FIFO: return 0x01;
02737 case CHANNEL_IS_OPTO: return 0x02;
02738 case CHANNEL_IS_CPU : return 0x04;
02739 default: return 0;
02740 }
02741 }
02742 case TYPE_232H:
02743 {
02744 switch (type)
02745 {
02746 case CHANNEL_IS_UART : return 0;
02747 case CHANNEL_IS_FIFO : return 0x01;
02748 case CHANNEL_IS_OPTO : return 0x02;
02749 case CHANNEL_IS_CPU : return 0x04;
02750 case CHANNEL_IS_FT1284 : return 0x08;
02751 default: return 0;
02752 }
02753 }
02754 case TYPE_R:
02755 {
02756 switch (type)
02757 {
02758 case CHANNEL_IS_UART : return 0;
02759 case CHANNEL_IS_FIFO : return 0x01;
02760 default: return 0;
02761 }
02762 }
02763 case TYPE_230X:
02764 default: return 0;
02765 }
02766 return 0;
02767 }
02768
02783 int ftdi_eeprom_build(struct ftdi_context *ftdi)
02784 {
02785 unsigned char i, j, eeprom_size_mask;
02786 unsigned short checksum, value;
02787 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
02788 int user_area_size, free_start, free_end;
02789 struct ftdi_eeprom *eeprom;
02790 unsigned char * output;
02791
02792 if (ftdi == NULL)
02793 ftdi_error_return(-2,"No context");
02794 if (ftdi->eeprom == NULL)
02795 ftdi_error_return(-2,"No eeprom structure");
02796
02797 eeprom= ftdi->eeprom;
02798 output = eeprom->buf;
02799
02800 if (eeprom->chip == -1)
02801 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
02802
02803 if (eeprom->size == -1)
02804 {
02805 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
02806 eeprom->size = 0x100;
02807 else
02808 eeprom->size = 0x80;
02809 }
02810
02811 if (eeprom->manufacturer != NULL)
02812 manufacturer_size = strlen(eeprom->manufacturer);
02813 if (eeprom->product != NULL)
02814 product_size = strlen(eeprom->product);
02815 if (eeprom->serial != NULL)
02816 serial_size = strlen(eeprom->serial);
02817
02818
02819 switch (ftdi->type)
02820 {
02821 case TYPE_AM:
02822 case TYPE_BM:
02823 case TYPE_R:
02824 user_area_size = 96;
02825 break;
02826 case TYPE_2232C:
02827 user_area_size = 90;
02828 break;
02829 case TYPE_230X:
02830 user_area_size = 88;
02831 break;
02832 case TYPE_2232H:
02833 case TYPE_4232H:
02834 user_area_size = 86;
02835 break;
02836 case TYPE_232H:
02837 user_area_size = 80;
02838 break;
02839 default:
02840 user_area_size = 0;
02841 break;
02842 }
02843 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
02844
02845 if (user_area_size < 0)
02846 ftdi_error_return(-1,"eeprom size exceeded");
02847
02848
02849 if (ftdi->type == TYPE_230X)
02850 {
02851
02852
02853 memset(ftdi->eeprom->buf, 0, 0x80);
02854 memset((ftdi->eeprom->buf + 0xa0), 0, (FTDI_MAX_EEPROM_SIZE - 0xa0));
02855 }
02856 else
02857 {
02858 memset(ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
02859 }
02860
02861
02862
02863
02864 output[0x02] = eeprom->vendor_id;
02865 output[0x03] = eeprom->vendor_id >> 8;
02866
02867
02868 output[0x04] = eeprom->product_id;
02869 output[0x05] = eeprom->product_id >> 8;
02870
02871
02872 output[0x06] = eeprom->release_number;
02873 output[0x07] = eeprom->release_number >> 8;
02874
02875
02876
02877
02878
02879
02880 j = 0x80;
02881 if (eeprom->self_powered)
02882 j |= 0x40;
02883 if (eeprom->remote_wakeup)
02884 j |= 0x20;
02885 output[0x08] = j;
02886
02887
02888 output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
02889
02890 if ((ftdi->type != TYPE_AM) && (ftdi->type != TYPE_230X))
02891 {
02892
02893
02894
02895
02896
02897
02898
02899
02900
02901
02902 j = 0;
02903 if (eeprom->in_is_isochronous)
02904 j = j | 1;
02905 if (eeprom->out_is_isochronous)
02906 j = j | 2;
02907 output[0x0A] = j;
02908 }
02909
02910
02911
02912
02913
02914 i = 0;
02915 switch (ftdi->type)
02916 {
02917 case TYPE_2232H:
02918 case TYPE_4232H:
02919 i += 2;
02920 case TYPE_R:
02921 i += 2;
02922 case TYPE_2232C:
02923 i += 2;
02924 case TYPE_AM:
02925 case TYPE_BM:
02926 i += 0x94;
02927 break;
02928 case TYPE_232H:
02929 case TYPE_230X:
02930 i = 0xa0;
02931 break;
02932 }
02933
02934 eeprom_size_mask = eeprom->size -1;
02935 free_end = i & eeprom_size_mask;
02936
02937
02938
02939
02940 output[0x0E] = i;
02941 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
02942 output[i & eeprom_size_mask] = 0x03, i++;
02943 for (j = 0; j < manufacturer_size; j++)
02944 {
02945 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
02946 output[i & eeprom_size_mask] = 0x00, i++;
02947 }
02948 output[0x0F] = manufacturer_size*2 + 2;
02949
02950
02951
02952 output[0x10] = i | 0x80;
02953 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
02954 output[i & eeprom_size_mask] = 0x03, i++;
02955 for (j = 0; j < product_size; j++)
02956 {
02957 output[i & eeprom_size_mask] = eeprom->product[j], i++;
02958 output[i & eeprom_size_mask] = 0x00, i++;
02959 }
02960 output[0x11] = product_size*2 + 2;
02961
02962
02963
02964 output[0x12] = i | 0x80;
02965 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
02966 output[i & eeprom_size_mask] = 0x03, i++;
02967 for (j = 0; j < serial_size; j++)
02968 {
02969 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
02970 output[i & eeprom_size_mask] = 0x00, i++;
02971 }
02972
02973
02974 if (ftdi->type > TYPE_BM)
02975 {
02976 output[i & eeprom_size_mask] = 0x02;
02977 i++;
02978 output[i & eeprom_size_mask] = 0x03;
02979 i++;
02980 output[i & eeprom_size_mask] = eeprom->is_not_pnp;
02981 i++;
02982 }
02983
02984 output[0x13] = serial_size*2 + 2;
02985
02986 if (ftdi->type > TYPE_AM)
02987 {
02988 if (eeprom->use_serial)
02989 output[0x0A] |= USE_SERIAL_NUM;
02990 else
02991 output[0x0A] &= ~USE_SERIAL_NUM;
02992 }
02993
02994
02995
02996 switch (ftdi->type)
02997 {
02998 case TYPE_AM:
02999 break;
03000 case TYPE_BM:
03001 output[0x0C] = eeprom->usb_version & 0xff;
03002 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
03003 if (eeprom->use_usb_version)
03004 output[0x0A] |= USE_USB_VERSION_BIT;
03005 else
03006 output[0x0A] &= ~USE_USB_VERSION_BIT;
03007
03008 break;
03009 case TYPE_2232C:
03010
03011 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
03012 if ( eeprom->channel_a_driver == DRIVER_VCP)
03013 output[0x00] |= DRIVER_VCP;
03014 else
03015 output[0x00] &= ~DRIVER_VCP;
03016
03017 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
03018 output[0x00] |= HIGH_CURRENT_DRIVE;
03019 else
03020 output[0x00] &= ~HIGH_CURRENT_DRIVE;
03021
03022 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
03023 if ( eeprom->channel_b_driver == DRIVER_VCP)
03024 output[0x01] |= DRIVER_VCP;
03025 else
03026 output[0x01] &= ~DRIVER_VCP;
03027
03028 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
03029 output[0x01] |= HIGH_CURRENT_DRIVE;
03030 else
03031 output[0x01] &= ~HIGH_CURRENT_DRIVE;
03032
03033 if (eeprom->in_is_isochronous)
03034 output[0x0A] |= 0x1;
03035 else
03036 output[0x0A] &= ~0x1;
03037 if (eeprom->out_is_isochronous)
03038 output[0x0A] |= 0x2;
03039 else
03040 output[0x0A] &= ~0x2;
03041 if (eeprom->suspend_pull_downs)
03042 output[0x0A] |= 0x4;
03043 else
03044 output[0x0A] &= ~0x4;
03045 if (eeprom->use_usb_version)
03046 output[0x0A] |= USE_USB_VERSION_BIT;
03047 else
03048 output[0x0A] &= ~USE_USB_VERSION_BIT;
03049
03050 output[0x0C] = eeprom->usb_version & 0xff;
03051 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
03052 output[0x14] = eeprom->chip;
03053 break;
03054 case TYPE_R:
03055 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_R);
03056 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
03057 output[0x00] |= HIGH_CURRENT_DRIVE_R;
03058 if (eeprom->external_oscillator)
03059 output[0x00] |= 0x02;
03060 output[0x01] = 0x40;
03061
03062 if (eeprom->suspend_pull_downs)
03063 output[0x0A] |= 0x4;
03064 else
03065 output[0x0A] &= ~0x4;
03066 output[0x0B] = eeprom->invert;
03067 output[0x0C] = eeprom->usb_version & 0xff;
03068 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
03069
03070 if (eeprom->cbus_function[0] > CBUS_BB_RD)
03071 output[0x14] = CBUS_TXLED;
03072 else
03073 output[0x14] = eeprom->cbus_function[0];
03074
03075 if (eeprom->cbus_function[1] > CBUS_BB_RD)
03076 output[0x14] |= CBUS_RXLED<<4;
03077 else
03078 output[0x14] |= eeprom->cbus_function[1]<<4;
03079
03080 if (eeprom->cbus_function[2] > CBUS_BB_RD)
03081 output[0x15] = CBUS_TXDEN;
03082 else
03083 output[0x15] = eeprom->cbus_function[2];
03084
03085 if (eeprom->cbus_function[3] > CBUS_BB_RD)
03086 output[0x15] |= CBUS_PWREN<<4;
03087 else
03088 output[0x15] |= eeprom->cbus_function[3]<<4;
03089
03090 if (eeprom->cbus_function[4] > CBUS_CLK6)
03091 output[0x16] = CBUS_SLEEP;
03092 else
03093 output[0x16] = eeprom->cbus_function[4];
03094 break;
03095 case TYPE_2232H:
03096 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
03097 if ( eeprom->channel_a_driver == DRIVER_VCP)
03098 output[0x00] |= DRIVER_VCP;
03099 else
03100 output[0x00] &= ~DRIVER_VCP;
03101
03102 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
03103 if ( eeprom->channel_b_driver == DRIVER_VCP)
03104 output[0x01] |= DRIVER_VCP;
03105 else
03106 output[0x01] &= ~DRIVER_VCP;
03107 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
03108 output[0x01] |= SUSPEND_DBUS7_BIT;
03109 else
03110 output[0x01] &= ~SUSPEND_DBUS7_BIT;
03111
03112 if (eeprom->suspend_pull_downs)
03113 output[0x0A] |= 0x4;
03114 else
03115 output[0x0A] &= ~0x4;
03116
03117 if (eeprom->group0_drive > DRIVE_16MA)
03118 output[0x0c] |= DRIVE_16MA;
03119 else
03120 output[0x0c] |= eeprom->group0_drive;
03121 if (eeprom->group0_schmitt == IS_SCHMITT)
03122 output[0x0c] |= IS_SCHMITT;
03123 if (eeprom->group0_slew == SLOW_SLEW)
03124 output[0x0c] |= SLOW_SLEW;
03125
03126 if (eeprom->group1_drive > DRIVE_16MA)
03127 output[0x0c] |= DRIVE_16MA<<4;
03128 else
03129 output[0x0c] |= eeprom->group1_drive<<4;
03130 if (eeprom->group1_schmitt == IS_SCHMITT)
03131 output[0x0c] |= IS_SCHMITT<<4;
03132 if (eeprom->group1_slew == SLOW_SLEW)
03133 output[0x0c] |= SLOW_SLEW<<4;
03134
03135 if (eeprom->group2_drive > DRIVE_16MA)
03136 output[0x0d] |= DRIVE_16MA;
03137 else
03138 output[0x0d] |= eeprom->group2_drive;
03139 if (eeprom->group2_schmitt == IS_SCHMITT)
03140 output[0x0d] |= IS_SCHMITT;
03141 if (eeprom->group2_slew == SLOW_SLEW)
03142 output[0x0d] |= SLOW_SLEW;
03143
03144 if (eeprom->group3_drive > DRIVE_16MA)
03145 output[0x0d] |= DRIVE_16MA<<4;
03146 else
03147 output[0x0d] |= eeprom->group3_drive<<4;
03148 if (eeprom->group3_schmitt == IS_SCHMITT)
03149 output[0x0d] |= IS_SCHMITT<<4;
03150 if (eeprom->group3_slew == SLOW_SLEW)
03151 output[0x0d] |= SLOW_SLEW<<4;
03152
03153 output[0x18] = eeprom->chip;
03154
03155 break;
03156 case TYPE_4232H:
03157 if (eeprom->channel_a_driver == DRIVER_VCP)
03158 output[0x00] |= DRIVER_VCP;
03159 else
03160 output[0x00] &= ~DRIVER_VCP;
03161 if (eeprom->channel_b_driver == DRIVER_VCP)
03162 output[0x01] |= DRIVER_VCP;
03163 else
03164 output[0x01] &= ~DRIVER_VCP;
03165 if (eeprom->channel_c_driver == DRIVER_VCP)
03166 output[0x00] |= (DRIVER_VCP << 4);
03167 else
03168 output[0x00] &= ~(DRIVER_VCP << 4);
03169 if (eeprom->channel_d_driver == DRIVER_VCP)
03170 output[0x01] |= (DRIVER_VCP << 4);
03171 else
03172 output[0x01] &= ~(DRIVER_VCP << 4);
03173
03174 if (eeprom->suspend_pull_downs)
03175 output[0x0a] |= 0x4;
03176 else
03177 output[0x0a] &= ~0x4;
03178
03179 if (eeprom->channel_a_rs485enable)
03180 output[0x0b] |= CHANNEL_IS_RS485 << 0;
03181 else
03182 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
03183 if (eeprom->channel_b_rs485enable)
03184 output[0x0b] |= CHANNEL_IS_RS485 << 1;
03185 else
03186 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
03187 if (eeprom->channel_c_rs485enable)
03188 output[0x0b] |= CHANNEL_IS_RS485 << 2;
03189 else
03190 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
03191 if (eeprom->channel_d_rs485enable)
03192 output[0x0b] |= CHANNEL_IS_RS485 << 3;
03193 else
03194 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
03195
03196 if (eeprom->group0_drive > DRIVE_16MA)
03197 output[0x0c] |= DRIVE_16MA;
03198 else
03199 output[0x0c] |= eeprom->group0_drive;
03200 if (eeprom->group0_schmitt == IS_SCHMITT)
03201 output[0x0c] |= IS_SCHMITT;
03202 if (eeprom->group0_slew == SLOW_SLEW)
03203 output[0x0c] |= SLOW_SLEW;
03204
03205 if (eeprom->group1_drive > DRIVE_16MA)
03206 output[0x0c] |= DRIVE_16MA<<4;
03207 else
03208 output[0x0c] |= eeprom->group1_drive<<4;
03209 if (eeprom->group1_schmitt == IS_SCHMITT)
03210 output[0x0c] |= IS_SCHMITT<<4;
03211 if (eeprom->group1_slew == SLOW_SLEW)
03212 output[0x0c] |= SLOW_SLEW<<4;
03213
03214 if (eeprom->group2_drive > DRIVE_16MA)
03215 output[0x0d] |= DRIVE_16MA;
03216 else
03217 output[0x0d] |= eeprom->group2_drive;
03218 if (eeprom->group2_schmitt == IS_SCHMITT)
03219 output[0x0d] |= IS_SCHMITT;
03220 if (eeprom->group2_slew == SLOW_SLEW)
03221 output[0x0d] |= SLOW_SLEW;
03222
03223 if (eeprom->group3_drive > DRIVE_16MA)
03224 output[0x0d] |= DRIVE_16MA<<4;
03225 else
03226 output[0x0d] |= eeprom->group3_drive<<4;
03227 if (eeprom->group3_schmitt == IS_SCHMITT)
03228 output[0x0d] |= IS_SCHMITT<<4;
03229 if (eeprom->group3_slew == SLOW_SLEW)
03230 output[0x0d] |= SLOW_SLEW<<4;
03231
03232 output[0x18] = eeprom->chip;
03233
03234 break;
03235 case TYPE_232H:
03236 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
03237 if ( eeprom->channel_a_driver == DRIVER_VCP)
03238 output[0x00] |= DRIVER_VCPH;
03239 else
03240 output[0x00] &= ~DRIVER_VCPH;
03241 if (eeprom->powersave)
03242 output[0x01] |= POWER_SAVE_DISABLE_H;
03243 else
03244 output[0x01] &= ~POWER_SAVE_DISABLE_H;
03245
03246 if (eeprom->suspend_pull_downs)
03247 output[0x0a] |= 0x4;
03248 else
03249 output[0x0a] &= ~0x4;
03250
03251 if (eeprom->clock_polarity)
03252 output[0x01] |= FT1284_CLK_IDLE_STATE;
03253 else
03254 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
03255 if (eeprom->data_order)
03256 output[0x01] |= FT1284_DATA_LSB;
03257 else
03258 output[0x01] &= ~FT1284_DATA_LSB;
03259 if (eeprom->flow_control)
03260 output[0x01] |= FT1284_FLOW_CONTROL;
03261 else
03262 output[0x01] &= ~FT1284_FLOW_CONTROL;
03263 if (eeprom->group0_drive > DRIVE_16MA)
03264 output[0x0c] |= DRIVE_16MA;
03265 else
03266 output[0x0c] |= eeprom->group0_drive;
03267 if (eeprom->group0_schmitt == IS_SCHMITT)
03268 output[0x0c] |= IS_SCHMITT;
03269 if (eeprom->group0_slew == SLOW_SLEW)
03270 output[0x0c] |= SLOW_SLEW;
03271
03272 if (eeprom->group1_drive > DRIVE_16MA)
03273 output[0x0d] |= DRIVE_16MA;
03274 else
03275 output[0x0d] |= eeprom->group1_drive;
03276 if (eeprom->group1_schmitt == IS_SCHMITT)
03277 output[0x0d] |= IS_SCHMITT;
03278 if (eeprom->group1_slew == SLOW_SLEW)
03279 output[0x0d] |= SLOW_SLEW;
03280
03281 set_ft232h_cbus(eeprom, output);
03282
03283 output[0x1e] = eeprom->chip;
03284 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
03285 break;
03286 case TYPE_230X:
03287 output[0x00] = 0x80;
03288
03289 output[0x0c] = 0;
03290 for (j = 0; j <= 6; j++)
03291 {
03292 output[0x1a + j] = eeprom->cbus_function[j];
03293 }
03294 output[0x0b] = eeprom->invert;
03295 break;
03296 }
03297
03298
03299 free_start = 0;
03300 switch (ftdi->type)
03301 {
03302 case TYPE_230X:
03303 free_start += 2;
03304 case TYPE_232H:
03305 free_start += 6;
03306 case TYPE_2232H:
03307 case TYPE_4232H:
03308 free_start += 2;
03309 case TYPE_R:
03310 free_start += 2;
03311 case TYPE_2232C:
03312 free_start++;
03313 case TYPE_AM:
03314 case TYPE_BM:
03315 free_start += 0x14;
03316 }
03317
03318
03319 if (eeprom->user_data && eeprom->user_data_size >= 0)
03320 {
03321 if (eeprom->user_data_addr < free_start)
03322 fprintf(stderr,"Warning, user data starts inside the generated data!\n");
03323 if (eeprom->user_data_addr + eeprom->user_data_size >= free_end)
03324 fprintf(stderr,"Warning, user data overlaps the strings area!\n");
03325 if (eeprom->user_data_addr + eeprom->user_data_size > eeprom->size)
03326 ftdi_error_return(-1,"eeprom size exceeded");
03327 memcpy(output + eeprom->user_data_addr, eeprom->user_data, eeprom->user_data_size);
03328 }
03329
03330
03331 checksum = 0xAAAA;
03332
03333 for (i = 0; i < eeprom->size/2-1; i++)
03334 {
03335 if ((ftdi->type == TYPE_230X) && (i == 0x12))
03336 {
03337
03338 i = 0x40;
03339 }
03340 if ((ftdi->type == TYPE_230X) && (i >= 0x40) && (i < 0x50)) {
03341 uint16_t data;
03342 if (ftdi_read_eeprom_location(ftdi, i, &data)) {
03343 fprintf(stderr, "Reading Factory Configuration Data failed\n");
03344 i = 0x50;
03345 }
03346 value = data;
03347 }
03348 else {
03349 value = output[i*2];
03350 value += output[(i*2)+1] << 8;
03351 }
03352 checksum = value^checksum;
03353 checksum = (checksum << 1) | (checksum >> 15);
03354 }
03355
03356 output[eeprom->size-2] = checksum;
03357 output[eeprom->size-1] = checksum >> 8;
03358
03359 eeprom->initialized_for_connected_device = 1;
03360 return user_area_size;
03361 }
03362
03363
03364
03365
03366
03367 static unsigned char bit2type(unsigned char bits)
03368 {
03369 switch (bits)
03370 {
03371 case 0: return CHANNEL_IS_UART;
03372 case 1: return CHANNEL_IS_FIFO;
03373 case 2: return CHANNEL_IS_OPTO;
03374 case 4: return CHANNEL_IS_CPU;
03375 case 8: return CHANNEL_IS_FT1284;
03376 default:
03377 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
03378 bits);
03379 }
03380 return 0;
03381 }
03382
03383
03384
03385 static void print_inverted_bits(int invert)
03386 {
03387 const char *r_bits[] = {"TXD","RXD","RTS","CTS","DTR","DSR","DCD","RI"};
03388 int i;
03389
03390 fprintf(stdout,"Inverted bits:");
03391 for (i=0; i<8; i++)
03392 if ((invert & (1<<i)) == (1<<i))
03393 fprintf(stdout," %s",r_bits[i]);
03394
03395 fprintf(stdout,"\n");
03396 }
03411 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
03412 {
03413 int i, j;
03414 unsigned short checksum, eeprom_checksum, value;
03415 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
03416 int eeprom_size;
03417 struct ftdi_eeprom *eeprom;
03418 unsigned char *buf = NULL;
03419
03420 if (ftdi == NULL)
03421 ftdi_error_return(-1,"No context");
03422 if (ftdi->eeprom == NULL)
03423 ftdi_error_return(-1,"No eeprom structure");
03424
03425 eeprom = ftdi->eeprom;
03426 eeprom_size = eeprom->size;
03427 buf = ftdi->eeprom->buf;
03428
03429
03430 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
03431
03432
03433 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
03434
03435
03436 eeprom->release_number = buf[0x06] + (buf[0x07]<<8);
03437
03438
03439
03440
03441
03442 eeprom->self_powered = buf[0x08] & 0x40;
03443 eeprom->remote_wakeup = buf[0x08] & 0x20;
03444
03445
03446 eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
03447
03448
03449
03450
03451
03452
03453
03454
03455
03456
03457
03458 eeprom->in_is_isochronous = buf[0x0A]&0x01;
03459 eeprom->out_is_isochronous = buf[0x0A]&0x02;
03460 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
03461 eeprom->use_serial = !!(buf[0x0A] & USE_SERIAL_NUM);
03462 eeprom->use_usb_version = !!(buf[0x0A] & USE_USB_VERSION_BIT);
03463
03464
03465
03466 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
03467
03468
03469
03470 manufacturer_size = buf[0x0F]/2;
03471 if (eeprom->manufacturer)
03472 free(eeprom->manufacturer);
03473 if (manufacturer_size > 0)
03474 {
03475 eeprom->manufacturer = (char *)malloc(manufacturer_size);
03476 if (eeprom->manufacturer)
03477 {
03478
03479 i = buf[0x0E] & (eeprom_size -1);
03480 for (j=0; j<manufacturer_size-1; j++)
03481 {
03482 eeprom->manufacturer[j] = buf[2*j+i+2];
03483 }
03484 eeprom->manufacturer[j] = '\0';
03485 }
03486 }
03487 else eeprom->manufacturer = NULL;
03488
03489
03490
03491 if (eeprom->product)
03492 free(eeprom->product);
03493 product_size = buf[0x11]/2;
03494 if (product_size > 0)
03495 {
03496 eeprom->product = (char *)malloc(product_size);
03497 if (eeprom->product)
03498 {
03499
03500 i = buf[0x10] & (eeprom_size -1);
03501 for (j=0; j<product_size-1; j++)
03502 {
03503 eeprom->product[j] = buf[2*j+i+2];
03504 }
03505 eeprom->product[j] = '\0';
03506 }
03507 }
03508 else eeprom->product = NULL;
03509
03510
03511
03512 if (eeprom->serial)
03513 free(eeprom->serial);
03514 serial_size = buf[0x13]/2;
03515 if (serial_size > 0)
03516 {
03517 eeprom->serial = (char *)malloc(serial_size);
03518 if (eeprom->serial)
03519 {
03520
03521 i = buf[0x12] & (eeprom_size -1);
03522 for (j=0; j<serial_size-1; j++)
03523 {
03524 eeprom->serial[j] = buf[2*j+i+2];
03525 }
03526 eeprom->serial[j] = '\0';
03527 }
03528 }
03529 else eeprom->serial = NULL;
03530
03531
03532 checksum = 0xAAAA;
03533
03534 for (i = 0; i < eeprom_size/2-1; i++)
03535 {
03536 if ((ftdi->type == TYPE_230X) && (i == 0x12))
03537 {
03538
03539 i = 0x40;
03540 }
03541 value = buf[i*2];
03542 value += buf[(i*2)+1] << 8;
03543
03544 checksum = value^checksum;
03545 checksum = (checksum << 1) | (checksum >> 15);
03546 }
03547
03548 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
03549
03550 if (eeprom_checksum != checksum)
03551 {
03552 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
03553 ftdi_error_return(-1,"EEPROM checksum error");
03554 }
03555
03556 eeprom->channel_a_type = 0;
03557 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
03558 {
03559 eeprom->chip = -1;
03560 }
03561 else if (ftdi->type == TYPE_2232C)
03562 {
03563 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
03564 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
03565 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
03566 eeprom->channel_b_type = buf[0x01] & 0x7;
03567 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
03568 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
03569 eeprom->chip = buf[0x14];
03570 }
03571 else if (ftdi->type == TYPE_R)
03572 {
03573
03574 eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
03575 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
03576 eeprom->external_oscillator = buf[0x00] & 0x02;
03577 if ( (buf[0x01]&0x40) != 0x40)
03578 fprintf(stderr,
03579 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
03580 " If this happened with the\n"
03581 " EEPROM programmed by FTDI tools, please report "
03582 "to libftdi@developer.intra2net.com\n");
03583
03584 eeprom->chip = buf[0x16];
03585
03586
03587 eeprom->invert = buf[0x0B];
03588
03589
03590
03591 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
03592 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
03593 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
03594 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
03595 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
03596 }
03597 else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
03598 {
03599 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
03600 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
03601
03602 if (ftdi->type == TYPE_2232H)
03603 {
03604 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
03605 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
03606 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
03607 }
03608 else
03609 {
03610 eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
03611 eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
03612 eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
03613 eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
03614 eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
03615 eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
03616 }
03617
03618 eeprom->chip = buf[0x18];
03619 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
03620 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
03621 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
03622 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
03623 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
03624 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
03625 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
03626 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
03627 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
03628 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
03629 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
03630 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
03631 }
03632 else if (ftdi->type == TYPE_232H)
03633 {
03634 eeprom->channel_a_type = buf[0x00] & 0xf;
03635 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
03636 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
03637 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
03638 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
03639 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
03640 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
03641 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
03642 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
03643 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
03644 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
03645 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
03646
03647 for(i=0; i<5; i++)
03648 {
03649 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
03650 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
03651 }
03652 eeprom->chip = buf[0x1e];
03653
03654 }
03655 else if (ftdi->type == TYPE_230X)
03656 {
03657 for(i=0; i<4; i++)
03658 {
03659 eeprom->cbus_function[i] = buf[0x1a + i] & 0xFF;
03660 }
03661 eeprom->group0_drive = buf[0x0c] & 0x03;
03662 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
03663 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
03664 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x03;
03665 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
03666 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
03667
03668 eeprom->invert = buf[0xb];
03669 }
03670
03671 if (verbose)
03672 {
03673 const char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
03674 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
03675 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
03676 fprintf(stdout, "Release: 0x%04x\n",eeprom->release_number);
03677
03678 if (eeprom->self_powered)
03679 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
03680 else
03681 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
03682 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
03683 if (eeprom->manufacturer)
03684 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
03685 if (eeprom->product)
03686 fprintf(stdout, "Product: %s\n",eeprom->product);
03687 if (eeprom->serial)
03688 fprintf(stdout, "Serial: %s\n",eeprom->serial);
03689 fprintf(stdout, "Checksum : %04x\n", checksum);
03690 if (ftdi->type == TYPE_R) {
03691 fprintf(stdout, "Internal EEPROM\n");
03692 fprintf(stdout,"Oscillator: %s\n", eeprom->external_oscillator?"External":"Internal");
03693 }
03694 else if (eeprom->chip >= 0x46)
03695 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
03696 if (eeprom->suspend_dbus7)
03697 fprintf(stdout, "Suspend on DBUS7\n");
03698 if (eeprom->suspend_pull_downs)
03699 fprintf(stdout, "Pull IO pins low during suspend\n");
03700 if(eeprom->powersave)
03701 {
03702 if(ftdi->type >= TYPE_232H)
03703 fprintf(stdout,"Enter low power state on ACBUS7\n");
03704 }
03705 if (eeprom->remote_wakeup)
03706 fprintf(stdout, "Enable Remote Wake Up\n");
03707 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
03708 if (ftdi->type >= TYPE_2232C)
03709 fprintf(stdout,"Channel A has Mode %s%s%s\n",
03710 channel_mode[eeprom->channel_a_type],
03711 (eeprom->channel_a_driver)?" VCP":"",
03712 (eeprom->high_current_a)?" High Current IO":"");
03713 if (ftdi->type == TYPE_232H)
03714 {
03715 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
03716 (eeprom->clock_polarity)?"HIGH":"LOW",
03717 (eeprom->data_order)?"LSB":"MSB",
03718 (eeprom->flow_control)?"":"No ");
03719 }
03720 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
03721 fprintf(stdout,"Channel B has Mode %s%s%s\n",
03722 channel_mode[eeprom->channel_b_type],
03723 (eeprom->channel_b_driver)?" VCP":"",
03724 (eeprom->high_current_b)?" High Current IO":"");
03725 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
03726 eeprom->use_usb_version)
03727 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
03728
03729 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
03730 {
03731 fprintf(stdout,"%s has %d mA drive%s%s\n",
03732 (ftdi->type == TYPE_2232H)?"AL":"A",
03733 (eeprom->group0_drive+1) *4,
03734 (eeprom->group0_schmitt)?" Schmitt Input":"",
03735 (eeprom->group0_slew)?" Slow Slew":"");
03736 fprintf(stdout,"%s has %d mA drive%s%s\n",
03737 (ftdi->type == TYPE_2232H)?"AH":"B",
03738 (eeprom->group1_drive+1) *4,
03739 (eeprom->group1_schmitt)?" Schmitt Input":"",
03740 (eeprom->group1_slew)?" Slow Slew":"");
03741 fprintf(stdout,"%s has %d mA drive%s%s\n",
03742 (ftdi->type == TYPE_2232H)?"BL":"C",
03743 (eeprom->group2_drive+1) *4,
03744 (eeprom->group2_schmitt)?" Schmitt Input":"",
03745 (eeprom->group2_slew)?" Slow Slew":"");
03746 fprintf(stdout,"%s has %d mA drive%s%s\n",
03747 (ftdi->type == TYPE_2232H)?"BH":"D",
03748 (eeprom->group3_drive+1) *4,
03749 (eeprom->group3_schmitt)?" Schmitt Input":"",
03750 (eeprom->group3_slew)?" Slow Slew":"");
03751 }
03752 else if (ftdi->type == TYPE_232H)
03753 {
03754 const char *cbush_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
03755 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
03756 "CLK30","CLK15","CLK7_5"
03757 };
03758 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
03759 (eeprom->group0_drive+1) *4,
03760 (eeprom->group0_schmitt)?" Schmitt Input":"",
03761 (eeprom->group0_slew)?" Slow Slew":"");
03762 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
03763 (eeprom->group1_drive+1) *4,
03764 (eeprom->group1_schmitt)?" Schmitt Input":"",
03765 (eeprom->group1_slew)?" Slow Slew":"");
03766 for (i=0; i<10; i++)
03767 {
03768 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
03769 fprintf(stdout,"C%d Function: %s\n", i,
03770 cbush_mux[eeprom->cbus_function[i]]);
03771 }
03772 }
03773 else if (ftdi->type == TYPE_230X)
03774 {
03775 const char *cbusx_mux[] = {"TRISTATE","TXLED","RXLED", "TXRXLED","PWREN",
03776 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
03777 "CLK24","CLK12","CLK6","BAT_DETECT","BAT_DETECT#",
03778 "I2C_TXE#", "I2C_RXF#", "VBUS_SENSE", "BB_WR#",
03779 "BBRD#", "TIME_STAMP", "AWAKE#",
03780 };
03781 fprintf(stdout,"DBUS has %d mA drive%s%s\n",
03782 (eeprom->group0_drive+1) *4,
03783 (eeprom->group0_schmitt)?" Schmitt Input":"",
03784 (eeprom->group0_slew)?" Slow Slew":"");
03785 fprintf(stdout,"CBUS has %d mA drive%s%s\n",
03786 (eeprom->group1_drive+1) *4,
03787 (eeprom->group1_schmitt)?" Schmitt Input":"",
03788 (eeprom->group1_slew)?" Slow Slew":"");
03789 for (i=0; i<4; i++)
03790 {
03791 if (eeprom->cbus_function[i]<= CBUSX_AWAKE)
03792 fprintf(stdout,"CBUS%d Function: %s\n", i, cbusx_mux[eeprom->cbus_function[i]]);
03793 }
03794
03795 if (eeprom->invert)
03796 print_inverted_bits(eeprom->invert);
03797 }
03798
03799 if (ftdi->type == TYPE_R)
03800 {
03801 const char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
03802 "SLEEP","CLK48","CLK24","CLK12","CLK6",
03803 "IOMODE","BB_WR","BB_RD"
03804 };
03805 const char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
03806
03807 if (eeprom->invert)
03808 print_inverted_bits(eeprom->invert);
03809
03810 for (i=0; i<5; i++)
03811 {
03812 if (eeprom->cbus_function[i]<=CBUS_BB_RD)
03813 fprintf(stdout,"C%d Function: %s\n", i,
03814 cbus_mux[eeprom->cbus_function[i]]);
03815 else
03816 {
03817 if (i < 4)
03818
03819
03820 fprintf(stdout,"C%d BB Function: %s\n", i,
03821 cbus_BB[i]);
03822 else
03823 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
03824 }
03825 }
03826 }
03827 }
03828 return 0;
03829 }
03830
03841 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
03842 {
03843 switch (value_name)
03844 {
03845 case VENDOR_ID:
03846 *value = ftdi->eeprom->vendor_id;
03847 break;
03848 case PRODUCT_ID:
03849 *value = ftdi->eeprom->product_id;
03850 break;
03851 case RELEASE_NUMBER:
03852 *value = ftdi->eeprom->release_number;
03853 break;
03854 case SELF_POWERED:
03855 *value = ftdi->eeprom->self_powered;
03856 break;
03857 case REMOTE_WAKEUP:
03858 *value = ftdi->eeprom->remote_wakeup;
03859 break;
03860 case IS_NOT_PNP:
03861 *value = ftdi->eeprom->is_not_pnp;
03862 break;
03863 case SUSPEND_DBUS7:
03864 *value = ftdi->eeprom->suspend_dbus7;
03865 break;
03866 case IN_IS_ISOCHRONOUS:
03867 *value = ftdi->eeprom->in_is_isochronous;
03868 break;
03869 case OUT_IS_ISOCHRONOUS:
03870 *value = ftdi->eeprom->out_is_isochronous;
03871 break;
03872 case SUSPEND_PULL_DOWNS:
03873 *value = ftdi->eeprom->suspend_pull_downs;
03874 break;
03875 case USE_SERIAL:
03876 *value = ftdi->eeprom->use_serial;
03877 break;
03878 case USB_VERSION:
03879 *value = ftdi->eeprom->usb_version;
03880 break;
03881 case USE_USB_VERSION:
03882 *value = ftdi->eeprom->use_usb_version;
03883 break;
03884 case MAX_POWER:
03885 *value = ftdi->eeprom->max_power;
03886 break;
03887 case CHANNEL_A_TYPE:
03888 *value = ftdi->eeprom->channel_a_type;
03889 break;
03890 case CHANNEL_B_TYPE:
03891 *value = ftdi->eeprom->channel_b_type;
03892 break;
03893 case CHANNEL_A_DRIVER:
03894 *value = ftdi->eeprom->channel_a_driver;
03895 break;
03896 case CHANNEL_B_DRIVER:
03897 *value = ftdi->eeprom->channel_b_driver;
03898 break;
03899 case CHANNEL_C_DRIVER:
03900 *value = ftdi->eeprom->channel_c_driver;
03901 break;
03902 case CHANNEL_D_DRIVER:
03903 *value = ftdi->eeprom->channel_d_driver;
03904 break;
03905 case CHANNEL_A_RS485:
03906 *value = ftdi->eeprom->channel_a_rs485enable;
03907 break;
03908 case CHANNEL_B_RS485:
03909 *value = ftdi->eeprom->channel_b_rs485enable;
03910 break;
03911 case CHANNEL_C_RS485:
03912 *value = ftdi->eeprom->channel_c_rs485enable;
03913 break;
03914 case CHANNEL_D_RS485:
03915 *value = ftdi->eeprom->channel_d_rs485enable;
03916 break;
03917 case CBUS_FUNCTION_0:
03918 *value = ftdi->eeprom->cbus_function[0];
03919 break;
03920 case CBUS_FUNCTION_1:
03921 *value = ftdi->eeprom->cbus_function[1];
03922 break;
03923 case CBUS_FUNCTION_2:
03924 *value = ftdi->eeprom->cbus_function[2];
03925 break;
03926 case CBUS_FUNCTION_3:
03927 *value = ftdi->eeprom->cbus_function[3];
03928 break;
03929 case CBUS_FUNCTION_4:
03930 *value = ftdi->eeprom->cbus_function[4];
03931 break;
03932 case CBUS_FUNCTION_5:
03933 *value = ftdi->eeprom->cbus_function[5];
03934 break;
03935 case CBUS_FUNCTION_6:
03936 *value = ftdi->eeprom->cbus_function[6];
03937 break;
03938 case CBUS_FUNCTION_7:
03939 *value = ftdi->eeprom->cbus_function[7];
03940 break;
03941 case CBUS_FUNCTION_8:
03942 *value = ftdi->eeprom->cbus_function[8];
03943 break;
03944 case CBUS_FUNCTION_9:
03945 *value = ftdi->eeprom->cbus_function[9];
03946 break;
03947 case HIGH_CURRENT:
03948 *value = ftdi->eeprom->high_current;
03949 break;
03950 case HIGH_CURRENT_A:
03951 *value = ftdi->eeprom->high_current_a;
03952 break;
03953 case HIGH_CURRENT_B:
03954 *value = ftdi->eeprom->high_current_b;
03955 break;
03956 case INVERT:
03957 *value = ftdi->eeprom->invert;
03958 break;
03959 case GROUP0_DRIVE:
03960 *value = ftdi->eeprom->group0_drive;
03961 break;
03962 case GROUP0_SCHMITT:
03963 *value = ftdi->eeprom->group0_schmitt;
03964 break;
03965 case GROUP0_SLEW:
03966 *value = ftdi->eeprom->group0_slew;
03967 break;
03968 case GROUP1_DRIVE:
03969 *value = ftdi->eeprom->group1_drive;
03970 break;
03971 case GROUP1_SCHMITT:
03972 *value = ftdi->eeprom->group1_schmitt;
03973 break;
03974 case GROUP1_SLEW:
03975 *value = ftdi->eeprom->group1_slew;
03976 break;
03977 case GROUP2_DRIVE:
03978 *value = ftdi->eeprom->group2_drive;
03979 break;
03980 case GROUP2_SCHMITT:
03981 *value = ftdi->eeprom->group2_schmitt;
03982 break;
03983 case GROUP2_SLEW:
03984 *value = ftdi->eeprom->group2_slew;
03985 break;
03986 case GROUP3_DRIVE:
03987 *value = ftdi->eeprom->group3_drive;
03988 break;
03989 case GROUP3_SCHMITT:
03990 *value = ftdi->eeprom->group3_schmitt;
03991 break;
03992 case GROUP3_SLEW:
03993 *value = ftdi->eeprom->group3_slew;
03994 break;
03995 case POWER_SAVE:
03996 *value = ftdi->eeprom->powersave;
03997 break;
03998 case CLOCK_POLARITY:
03999 *value = ftdi->eeprom->clock_polarity;
04000 break;
04001 case DATA_ORDER:
04002 *value = ftdi->eeprom->data_order;
04003 break;
04004 case FLOW_CONTROL:
04005 *value = ftdi->eeprom->flow_control;
04006 break;
04007 case CHIP_TYPE:
04008 *value = ftdi->eeprom->chip;
04009 break;
04010 case CHIP_SIZE:
04011 *value = ftdi->eeprom->size;
04012 break;
04013 case EXTERNAL_OSCILLATOR:
04014 *value = ftdi->eeprom->external_oscillator;
04015 break;
04016 default:
04017 ftdi_error_return(-1, "Request for unknown EEPROM value");
04018 }
04019 return 0;
04020 }
04021
04034 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
04035 {
04036 switch (value_name)
04037 {
04038 case VENDOR_ID:
04039 ftdi->eeprom->vendor_id = value;
04040 break;
04041 case PRODUCT_ID:
04042 ftdi->eeprom->product_id = value;
04043 break;
04044 case RELEASE_NUMBER:
04045 ftdi->eeprom->release_number = value;
04046 break;
04047 case SELF_POWERED:
04048 ftdi->eeprom->self_powered = value;
04049 break;
04050 case REMOTE_WAKEUP:
04051 ftdi->eeprom->remote_wakeup = value;
04052 break;
04053 case IS_NOT_PNP:
04054 ftdi->eeprom->is_not_pnp = value;
04055 break;
04056 case SUSPEND_DBUS7:
04057 ftdi->eeprom->suspend_dbus7 = value;
04058 break;
04059 case IN_IS_ISOCHRONOUS:
04060 ftdi->eeprom->in_is_isochronous = value;
04061 break;
04062 case OUT_IS_ISOCHRONOUS:
04063 ftdi->eeprom->out_is_isochronous = value;
04064 break;
04065 case SUSPEND_PULL_DOWNS:
04066 ftdi->eeprom->suspend_pull_downs = value;
04067 break;
04068 case USE_SERIAL:
04069 ftdi->eeprom->use_serial = value;
04070 break;
04071 case USB_VERSION:
04072 ftdi->eeprom->usb_version = value;
04073 break;
04074 case USE_USB_VERSION:
04075 ftdi->eeprom->use_usb_version = value;
04076 break;
04077 case MAX_POWER:
04078 ftdi->eeprom->max_power = value;
04079 break;
04080 case CHANNEL_A_TYPE:
04081 ftdi->eeprom->channel_a_type = value;
04082 break;
04083 case CHANNEL_B_TYPE:
04084 ftdi->eeprom->channel_b_type = value;
04085 break;
04086 case CHANNEL_A_DRIVER:
04087 ftdi->eeprom->channel_a_driver = value;
04088 break;
04089 case CHANNEL_B_DRIVER:
04090 ftdi->eeprom->channel_b_driver = value;
04091 break;
04092 case CHANNEL_C_DRIVER:
04093 ftdi->eeprom->channel_c_driver = value;
04094 break;
04095 case CHANNEL_D_DRIVER:
04096 ftdi->eeprom->channel_d_driver = value;
04097 break;
04098 case CHANNEL_A_RS485:
04099 ftdi->eeprom->channel_a_rs485enable = value;
04100 break;
04101 case CHANNEL_B_RS485:
04102 ftdi->eeprom->channel_b_rs485enable = value;
04103 break;
04104 case CHANNEL_C_RS485:
04105 ftdi->eeprom->channel_c_rs485enable = value;
04106 break;
04107 case CHANNEL_D_RS485:
04108 ftdi->eeprom->channel_d_rs485enable = value;
04109 break;
04110 case CBUS_FUNCTION_0:
04111 ftdi->eeprom->cbus_function[0] = value;
04112 break;
04113 case CBUS_FUNCTION_1:
04114 ftdi->eeprom->cbus_function[1] = value;
04115 break;
04116 case CBUS_FUNCTION_2:
04117 ftdi->eeprom->cbus_function[2] = value;
04118 break;
04119 case CBUS_FUNCTION_3:
04120 ftdi->eeprom->cbus_function[3] = value;
04121 break;
04122 case CBUS_FUNCTION_4:
04123 ftdi->eeprom->cbus_function[4] = value;
04124 break;
04125 case CBUS_FUNCTION_5:
04126 ftdi->eeprom->cbus_function[5] = value;
04127 break;
04128 case CBUS_FUNCTION_6:
04129 ftdi->eeprom->cbus_function[6] = value;
04130 break;
04131 case CBUS_FUNCTION_7:
04132 ftdi->eeprom->cbus_function[7] = value;
04133 break;
04134 case CBUS_FUNCTION_8:
04135 ftdi->eeprom->cbus_function[8] = value;
04136 break;
04137 case CBUS_FUNCTION_9:
04138 ftdi->eeprom->cbus_function[9] = value;
04139 break;
04140 case HIGH_CURRENT:
04141 ftdi->eeprom->high_current = value;
04142 break;
04143 case HIGH_CURRENT_A:
04144 ftdi->eeprom->high_current_a = value;
04145 break;
04146 case HIGH_CURRENT_B:
04147 ftdi->eeprom->high_current_b = value;
04148 break;
04149 case INVERT:
04150 ftdi->eeprom->invert = value;
04151 break;
04152 case GROUP0_DRIVE:
04153 ftdi->eeprom->group0_drive = value;
04154 break;
04155 case GROUP0_SCHMITT:
04156 ftdi->eeprom->group0_schmitt = value;
04157 break;
04158 case GROUP0_SLEW:
04159 ftdi->eeprom->group0_slew = value;
04160 break;
04161 case GROUP1_DRIVE:
04162 ftdi->eeprom->group1_drive = value;
04163 break;
04164 case GROUP1_SCHMITT:
04165 ftdi->eeprom->group1_schmitt = value;
04166 break;
04167 case GROUP1_SLEW:
04168 ftdi->eeprom->group1_slew = value;
04169 break;
04170 case GROUP2_DRIVE:
04171 ftdi->eeprom->group2_drive = value;
04172 break;
04173 case GROUP2_SCHMITT:
04174 ftdi->eeprom->group2_schmitt = value;
04175 break;
04176 case GROUP2_SLEW:
04177 ftdi->eeprom->group2_slew = value;
04178 break;
04179 case GROUP3_DRIVE:
04180 ftdi->eeprom->group3_drive = value;
04181 break;
04182 case GROUP3_SCHMITT:
04183 ftdi->eeprom->group3_schmitt = value;
04184 break;
04185 case GROUP3_SLEW:
04186 ftdi->eeprom->group3_slew = value;
04187 break;
04188 case CHIP_TYPE:
04189 ftdi->eeprom->chip = value;
04190 break;
04191 case POWER_SAVE:
04192 ftdi->eeprom->powersave = value;
04193 break;
04194 case CLOCK_POLARITY:
04195 ftdi->eeprom->clock_polarity = value;
04196 break;
04197 case DATA_ORDER:
04198 ftdi->eeprom->data_order = value;
04199 break;
04200 case FLOW_CONTROL:
04201 ftdi->eeprom->flow_control = value;
04202 break;
04203 case CHIP_SIZE:
04204 ftdi_error_return(-2, "EEPROM Value can't be changed");
04205 break;
04206 case EXTERNAL_OSCILLATOR:
04207 ftdi->eeprom->external_oscillator = value;
04208 break;
04209 case USER_DATA_ADDR:
04210 ftdi->eeprom->user_data_addr = value;
04211 break;
04212
04213 default :
04214 ftdi_error_return(-1, "Request to unknown EEPROM value");
04215 }
04216 ftdi->eeprom->initialized_for_connected_device = 0;
04217 return 0;
04218 }
04219
04230 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
04231 {
04232 if (!ftdi || !(ftdi->eeprom))
04233 ftdi_error_return(-1, "No appropriate structure");
04234
04235 if (!buf || size < ftdi->eeprom->size)
04236 ftdi_error_return(-1, "Not enough room to store eeprom");
04237
04238
04239 if (size > FTDI_MAX_EEPROM_SIZE)
04240 size = FTDI_MAX_EEPROM_SIZE;
04241
04242 memcpy(buf, ftdi->eeprom->buf, size);
04243
04244 return 0;
04245 }
04246
04256 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
04257 {
04258 if (!ftdi || !(ftdi->eeprom) || !buf)
04259 ftdi_error_return(-1, "No appropriate structure");
04260
04261
04262 if (size > FTDI_MAX_EEPROM_SIZE)
04263 size = FTDI_MAX_EEPROM_SIZE;
04264
04265 memcpy(ftdi->eeprom->buf, buf, size);
04266
04267 return 0;
04268 }
04269
04279 int ftdi_set_eeprom_user_data(struct ftdi_context *ftdi, const char * buf, int size)
04280 {
04281 if (!ftdi || !(ftdi->eeprom) || !buf)
04282 ftdi_error_return(-1, "No appropriate structure");
04283
04284 ftdi->eeprom->user_data_size = size;
04285 ftdi->eeprom->user_data = buf;
04286 return 0;
04287 }
04288
04300 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
04301 {
04302 unsigned char buf[2];
04303
04304 if (ftdi == NULL || ftdi->usb_dev == NULL)
04305 ftdi_error_return(-2, "USB device unavailable");
04306
04307 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, buf, 2, ftdi->usb_read_timeout) != 2)
04308 ftdi_error_return(-1, "reading eeprom failed");
04309
04310 *eeprom_val = (0xff & buf[0]) | (buf[1] << 8);
04311
04312 return 0;
04313 }
04314
04324 int ftdi_read_eeprom(struct ftdi_context *ftdi)
04325 {
04326 int i;
04327 unsigned char *buf;
04328
04329 if (ftdi == NULL || ftdi->usb_dev == NULL)
04330 ftdi_error_return(-2, "USB device unavailable");
04331 buf = ftdi->eeprom->buf;
04332
04333 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
04334 {
04335 if (libusb_control_transfer(
04336 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
04337 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
04338 ftdi_error_return(-1, "reading eeprom failed");
04339 }
04340
04341 if (ftdi->type == TYPE_R)
04342 ftdi->eeprom->size = 0x80;
04343
04344
04345 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
04346 ftdi->eeprom->size = -1;
04347 else if (memcmp(buf,&buf[0x80],0x80) == 0)
04348 ftdi->eeprom->size = 0x80;
04349 else if (memcmp(buf,&buf[0x40],0x40) == 0)
04350 ftdi->eeprom->size = 0x40;
04351 else
04352 ftdi->eeprom->size = 0x100;
04353 return 0;
04354 }
04355
04356
04357
04358
04359
04360
04361 static unsigned char ftdi_read_chipid_shift(unsigned char value)
04362 {
04363 return ((value & 1) << 1) |
04364 ((value & 2) << 5) |
04365 ((value & 4) >> 2) |
04366 ((value & 8) << 4) |
04367 ((value & 16) >> 1) |
04368 ((value & 32) >> 1) |
04369 ((value & 64) >> 4) |
04370 ((value & 128) >> 2);
04371 }
04372
04383 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
04384 {
04385 unsigned int a = 0, b = 0;
04386
04387 if (ftdi == NULL || ftdi->usb_dev == NULL)
04388 ftdi_error_return(-2, "USB device unavailable");
04389
04390 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)
04391 {
04392 a = a << 8 | a >> 8;
04393 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)
04394 {
04395 b = b << 8 | b >> 8;
04396 a = (a << 16) | (b & 0xFFFF);
04397 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
04398 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
04399 *chipid = a ^ 0xa5f0f7d1;
04400 return 0;
04401 }
04402 }
04403
04404 ftdi_error_return(-1, "read of FTDIChip-ID failed");
04405 }
04406
04421 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
04422 unsigned short eeprom_val)
04423 {
04424 int chip_type_location;
04425 unsigned short chip_type;
04426
04427 if (ftdi == NULL || ftdi->usb_dev == NULL)
04428 ftdi_error_return(-2, "USB device unavailable");
04429
04430 if (eeprom_addr <0x80)
04431 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
04432
04433
04434 switch (ftdi->type)
04435 {
04436 case TYPE_BM:
04437 case TYPE_2232C:
04438 chip_type_location = 0x14;
04439 break;
04440 case TYPE_2232H:
04441 case TYPE_4232H:
04442 chip_type_location = 0x18;
04443 break;
04444 case TYPE_232H:
04445 chip_type_location = 0x1e;
04446 break;
04447 default:
04448 ftdi_error_return(-4, "Device can't access unprotected area");
04449 }
04450
04451 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
04452 ftdi_error_return(-5, "Reading failed");
04453 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
04454 if ((chip_type & 0xff) != 0x66)
04455 {
04456 ftdi_error_return(-6, "EEPROM is not of 93x66");
04457 }
04458
04459 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
04460 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
04461 NULL, 0, ftdi->usb_write_timeout) != 0)
04462 ftdi_error_return(-1, "unable to write eeprom");
04463
04464 return 0;
04465 }
04466
04477 int ftdi_write_eeprom(struct ftdi_context *ftdi)
04478 {
04479 unsigned short usb_val, status;
04480 int i, ret;
04481 unsigned char *eeprom;
04482
04483 if (ftdi == NULL || ftdi->usb_dev == NULL)
04484 ftdi_error_return(-2, "USB device unavailable");
04485
04486 if(ftdi->eeprom->initialized_for_connected_device == 0)
04487 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
04488
04489 eeprom = ftdi->eeprom->buf;
04490
04491
04492 if ((ret = ftdi_usb_reset(ftdi)) != 0)
04493 return ret;
04494 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
04495 return ret;
04496 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
04497 return ret;
04498
04499 for (i = 0; i < ftdi->eeprom->size/2; i++)
04500 {
04501
04502 if ((ftdi->type == TYPE_230X) && (i == 0x40))
04503 {
04504 i = 0x50;
04505 }
04506 usb_val = eeprom[i*2];
04507 usb_val += eeprom[(i*2)+1] << 8;
04508 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
04509 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
04510 NULL, 0, ftdi->usb_write_timeout) < 0)
04511 ftdi_error_return(-1, "unable to write eeprom");
04512 }
04513
04514 return 0;
04515 }
04516
04531 #define MAGIC 0x55aa
04532 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
04533 {
04534 unsigned short eeprom_value;
04535 if (ftdi == NULL || ftdi->usb_dev == NULL)
04536 ftdi_error_return(-2, "USB device unavailable");
04537
04538 if ((ftdi->type == TYPE_R) || (ftdi->type == TYPE_230X))
04539 {
04540 ftdi->eeprom->chip = 0;
04541 return 0;
04542 }
04543
04544 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
04545 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
04546 ftdi_error_return(-1, "unable to erase eeprom");
04547
04548
04549
04550
04551
04552
04553 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
04554 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
04555 NULL, 0, ftdi->usb_write_timeout) != 0)
04556 ftdi_error_return(-3, "Writing magic failed");
04557 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
04558 ftdi_error_return(-4, "Reading failed");
04559 if (eeprom_value == MAGIC)
04560 {
04561 ftdi->eeprom->chip = 0x46;
04562 }
04563 else
04564 {
04565 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
04566 ftdi_error_return(-4, "Reading failed");
04567 if (eeprom_value == MAGIC)
04568 ftdi->eeprom->chip = 0x56;
04569 else
04570 {
04571 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
04572 ftdi_error_return(-4, "Reading failed");
04573 if (eeprom_value == MAGIC)
04574 ftdi->eeprom->chip = 0x66;
04575 else
04576 {
04577 ftdi->eeprom->chip = -1;
04578 }
04579 }
04580 }
04581 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
04582 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
04583 ftdi_error_return(-1, "unable to erase eeprom");
04584 return 0;
04585 }
04586
04594 const char *ftdi_get_error_string (struct ftdi_context *ftdi)
04595 {
04596 if (ftdi == NULL)
04597 return "";
04598
04599 return ftdi->error_str;
04600 }
04601
04602