libftdi1  1.0
ftdi.c
Go to the documentation of this file.
00001 /***************************************************************************
00002                           ftdi.c  -  description
00003                              -------------------
00004     begin                : Fri Apr 4 2003
00005     copyright            : (C) 2003-2013 by Intra2net AG and the libftdi developers
00006     email                : opensource@intra2net.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU Lesser General Public License           *
00013  *   version 2.1 as published by the Free Software Foundation;             *
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;    /* chip type */
00097     ftdi->baudrate = -1;
00098     ftdi->bitbang_enabled = 0;  /* 0: normal mode 1: any of the bitbang modes enabled */
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; /* when bitbang is enabled this holds the number of the mode  */
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     /* All fine. Now allocate the readbuffer */
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()
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 != 0 && product != 0) && 
00332              desc.idVendor == vendor && desc.idProduct == product) ||
00333             ((vendor == 0 && product == 0) && 
00334              (desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
00335                                           || desc.idProduct == 0x6011 || desc.idProduct == 0x6014)))
00336         {
00337             *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
00338             if (!*curdev)
00339                 ftdi_error_return_free_device_list(-3, "out of memory", devs);
00340 
00341             (*curdev)->next = NULL;
00342             (*curdev)->dev = dev;
00343             libusb_ref_device(dev);
00344             curdev = &(*curdev)->next;
00345             count++;
00346         }
00347     }
00348     libusb_free_device_list(devs,1);
00349     return count;
00350 }
00351 
00357 void ftdi_list_free(struct ftdi_device_list **devlist)
00358 {
00359     struct ftdi_device_list *curdev, *next;
00360 
00361     for (curdev = *devlist; curdev != NULL;)
00362     {
00363         next = curdev->next;
00364         libusb_unref_device(curdev->dev);
00365         free(curdev);
00366         curdev = next;
00367     }
00368 
00369     *devlist = NULL;
00370 }
00371 
00377 void ftdi_list_free2(struct ftdi_device_list *devlist)
00378 {
00379     ftdi_list_free(&devlist);
00380 }
00381 
00408 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
00409                          char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
00410 {
00411     struct libusb_device_descriptor desc;
00412 
00413     if ((ftdi==NULL) || (dev==NULL))
00414         return -1;
00415 
00416     if (libusb_open(dev, &ftdi->usb_dev) < 0)
00417         ftdi_error_return(-4, "libusb_open() failed");
00418 
00419     if (libusb_get_device_descriptor(dev, &desc) < 0)
00420         ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
00421 
00422     if (manufacturer != NULL)
00423     {
00424         if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
00425         {
00426             ftdi_usb_close_internal (ftdi);
00427             ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
00428         }
00429     }
00430 
00431     if (description != NULL)
00432     {
00433         if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
00434         {
00435             ftdi_usb_close_internal (ftdi);
00436             ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
00437         }
00438     }
00439 
00440     if (serial != NULL)
00441     {
00442         if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
00443         {
00444             ftdi_usb_close_internal (ftdi);
00445             ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
00446         }
00447     }
00448 
00449     ftdi_usb_close_internal (ftdi);
00450 
00451     return 0;
00452 }
00453 
00460 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
00461 {
00462     struct libusb_device_descriptor desc;
00463     struct libusb_config_descriptor *config0;
00464     unsigned int packet_size;
00465 
00466     // Sanity check
00467     if (ftdi == NULL || dev == NULL)
00468         return 64;
00469 
00470     // Determine maximum packet size. Init with default value.
00471     // New hi-speed devices from FTDI use a packet size of 512 bytes
00472     // but could be connected to a normal speed USB hub -> 64 bytes packet size.
00473     if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
00474         packet_size = 512;
00475     else
00476         packet_size = 64;
00477 
00478     if (libusb_get_device_descriptor(dev, &desc) < 0)
00479         return packet_size;
00480 
00481     if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
00482         return packet_size;
00483 
00484     if (desc.bNumConfigurations > 0)
00485     {
00486         if (ftdi->interface < config0->bNumInterfaces)
00487         {
00488             struct libusb_interface interface = config0->interface[ftdi->interface];
00489             if (interface.num_altsetting > 0)
00490             {
00491                 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
00492                 if (descriptor.bNumEndpoints > 0)
00493                 {
00494                     packet_size = descriptor.endpoint[0].wMaxPacketSize;
00495                 }
00496             }
00497         }
00498     }
00499 
00500     libusb_free_config_descriptor (config0);
00501     return packet_size;
00502 }
00503 
00522 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
00523 {
00524     struct libusb_device_descriptor desc;
00525     struct libusb_config_descriptor *config0;
00526     int cfg, cfg0, detach_errno = 0;
00527 
00528     if (ftdi == NULL)
00529         ftdi_error_return(-8, "ftdi context invalid");
00530 
00531     if (libusb_open(dev, &ftdi->usb_dev) < 0)
00532         ftdi_error_return(-4, "libusb_open() failed");
00533 
00534     if (libusb_get_device_descriptor(dev, &desc) < 0)
00535         ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
00536 
00537     if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
00538         ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
00539     cfg0 = config0->bConfigurationValue;
00540     libusb_free_config_descriptor (config0);
00541 
00542     // Try to detach ftdi_sio kernel module.
00543     //
00544     // The return code is kept in a separate variable and only parsed
00545     // if usb_set_configuration() or usb_claim_interface() fails as the
00546     // detach operation might be denied and everything still works fine.
00547     // Likely scenario is a static ftdi_sio kernel module.
00548     if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
00549     {
00550         if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
00551             detach_errno = errno;
00552     }
00553 
00554     if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
00555         ftdi_error_return(-12, "libusb_get_configuration () failed");
00556     // set configuration (needed especially for windows)
00557     // tolerate EBUSY: one device with one configuration, but two interfaces
00558     //    and libftdi sessions to both interfaces (e.g. FT2232)
00559     if (desc.bNumConfigurations > 0 && cfg != cfg0)
00560     {
00561         if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
00562         {
00563             ftdi_usb_close_internal (ftdi);
00564             if (detach_errno == EPERM)
00565             {
00566                 ftdi_error_return(-8, "inappropriate permissions on device!");
00567             }
00568             else
00569             {
00570                 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
00571             }
00572         }
00573     }
00574 
00575     if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
00576     {
00577         ftdi_usb_close_internal (ftdi);
00578         if (detach_errno == EPERM)
00579         {
00580             ftdi_error_return(-8, "inappropriate permissions on device!");
00581         }
00582         else
00583         {
00584             ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
00585         }
00586     }
00587 
00588     if (ftdi_usb_reset (ftdi) != 0)
00589     {
00590         ftdi_usb_close_internal (ftdi);
00591         ftdi_error_return(-6, "ftdi_usb_reset failed");
00592     }
00593 
00594     // Try to guess chip type
00595     // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
00596     if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
00597                                     && desc.iSerialNumber == 0))
00598         ftdi->type = TYPE_BM;
00599     else if (desc.bcdDevice == 0x200)
00600         ftdi->type = TYPE_AM;
00601     else if (desc.bcdDevice == 0x500)
00602         ftdi->type = TYPE_2232C;
00603     else if (desc.bcdDevice == 0x600)
00604         ftdi->type = TYPE_R;
00605     else if (desc.bcdDevice == 0x700)
00606         ftdi->type = TYPE_2232H;
00607     else if (desc.bcdDevice == 0x800)
00608         ftdi->type = TYPE_4232H;
00609     else if (desc.bcdDevice == 0x900)
00610         ftdi->type = TYPE_232H;
00611 
00612     // Determine maximum packet size
00613     ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
00614 
00615     if (ftdi_set_baudrate (ftdi, 9600) != 0)
00616     {
00617         ftdi_usb_close_internal (ftdi);
00618         ftdi_error_return(-7, "set baudrate failed");
00619     }
00620 
00621     ftdi_error_return(0, "all fine");
00622 }
00623 
00633 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
00634 {
00635     return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
00636 }
00637 
00659 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00660                        const char* description, const char* serial)
00661 {
00662     return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
00663 }
00664 
00689 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00690                              const char* description, const char* serial, unsigned int index)
00691 {
00692     libusb_device *dev;
00693     libusb_device **devs;
00694     char string[256];
00695     int i = 0;
00696 
00697     if (ftdi == NULL)
00698         ftdi_error_return(-11, "ftdi context invalid");
00699 
00700     if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00701         ftdi_error_return(-12, "libusb_get_device_list() failed");
00702 
00703     while ((dev = devs[i++]) != NULL)
00704     {
00705         struct libusb_device_descriptor desc;
00706         int res;
00707 
00708         if (libusb_get_device_descriptor(dev, &desc) < 0)
00709             ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
00710 
00711         if (desc.idVendor == vendor && desc.idProduct == product)
00712         {
00713             if (libusb_open(dev, &ftdi->usb_dev) < 0)
00714                 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
00715 
00716             if (description != NULL)
00717             {
00718                 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
00719                 {
00720                     ftdi_usb_close_internal (ftdi);
00721                     ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
00722                 }
00723                 if (strncmp(string, description, sizeof(string)) != 0)
00724                 {
00725                     ftdi_usb_close_internal (ftdi);
00726                     continue;
00727                 }
00728             }
00729             if (serial != NULL)
00730             {
00731                 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
00732                 {
00733                     ftdi_usb_close_internal (ftdi);
00734                     ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
00735                 }
00736                 if (strncmp(string, serial, sizeof(string)) != 0)
00737                 {
00738                     ftdi_usb_close_internal (ftdi);
00739                     continue;
00740                 }
00741             }
00742 
00743             ftdi_usb_close_internal (ftdi);
00744 
00745             if (index > 0)
00746             {
00747                 index--;
00748                 continue;
00749             }
00750 
00751             res = ftdi_usb_open_dev(ftdi, dev);
00752             libusb_free_device_list(devs,1);
00753             return res;
00754         }
00755     }
00756 
00757     // device not found
00758     ftdi_error_return_free_device_list(-3, "device not found", devs);
00759 }
00760 
00787 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
00788 {
00789     if (ftdi == NULL)
00790         ftdi_error_return(-12, "ftdi context invalid");
00791 
00792     if (description[0] == 0 || description[1] != ':')
00793         ftdi_error_return(-11, "illegal description format");
00794 
00795     if (description[0] == 'd')
00796     {
00797         libusb_device *dev;
00798         libusb_device **devs;
00799         unsigned int bus_number, device_address;
00800         int i = 0;
00801 
00802         if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
00803             ftdi_error_return(-2, "libusb_get_device_list() failed");
00804 
00805         /* XXX: This doesn't handle symlinks/odd paths/etc... */
00806         if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
00807             ftdi_error_return_free_device_list(-11, "illegal description format", devs);
00808 
00809         while ((dev = devs[i++]) != NULL)
00810         {
00811             int ret;
00812             if (bus_number == libusb_get_bus_number (dev)
00813                     && device_address == libusb_get_device_address (dev))
00814             {
00815                 ret = ftdi_usb_open_dev(ftdi, dev);
00816                 libusb_free_device_list(devs,1);
00817                 return ret;
00818             }
00819         }
00820 
00821         // device not found
00822         ftdi_error_return_free_device_list(-3, "device not found", devs);
00823     }
00824     else if (description[0] == 'i' || description[0] == 's')
00825     {
00826         unsigned int vendor;
00827         unsigned int product;
00828         unsigned int index=0;
00829         const char *serial=NULL;
00830         const char *startp, *endp;
00831 
00832         errno=0;
00833         startp=description+2;
00834         vendor=strtoul((char*)startp,(char**)&endp,0);
00835         if (*endp != ':' || endp == startp || errno != 0)
00836             ftdi_error_return(-11, "illegal description format");
00837 
00838         startp=endp+1;
00839         product=strtoul((char*)startp,(char**)&endp,0);
00840         if (endp == startp || errno != 0)
00841             ftdi_error_return(-11, "illegal description format");
00842 
00843         if (description[0] == 'i' && *endp != 0)
00844         {
00845             /* optional index field in i-mode */
00846             if (*endp != ':')
00847                 ftdi_error_return(-11, "illegal description format");
00848 
00849             startp=endp+1;
00850             index=strtoul((char*)startp,(char**)&endp,0);
00851             if (*endp != 0 || endp == startp || errno != 0)
00852                 ftdi_error_return(-11, "illegal description format");
00853         }
00854         if (description[0] == 's')
00855         {
00856             if (*endp != ':')
00857                 ftdi_error_return(-11, "illegal description format");
00858 
00859             /* rest of the description is the serial */
00860             serial=endp+1;
00861         }
00862 
00863         return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
00864     }
00865     else
00866     {
00867         ftdi_error_return(-11, "illegal description format");
00868     }
00869 }
00870 
00880 int ftdi_usb_reset(struct ftdi_context *ftdi)
00881 {
00882     if (ftdi == NULL || ftdi->usb_dev == NULL)
00883         ftdi_error_return(-2, "USB device unavailable");
00884 
00885     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00886                                 SIO_RESET_REQUEST, SIO_RESET_SIO,
00887                                 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
00888         ftdi_error_return(-1,"FTDI reset failed");
00889 
00890     // Invalidate data in the readbuffer
00891     ftdi->readbuffer_offset = 0;
00892     ftdi->readbuffer_remaining = 0;
00893 
00894     return 0;
00895 }
00896 
00906 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
00907 {
00908     if (ftdi == NULL || ftdi->usb_dev == NULL)
00909         ftdi_error_return(-2, "USB device unavailable");
00910 
00911     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00912                                 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
00913                                 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
00914         ftdi_error_return(-1, "FTDI purge of RX buffer failed");
00915 
00916     // Invalidate data in the readbuffer
00917     ftdi->readbuffer_offset = 0;
00918     ftdi->readbuffer_remaining = 0;
00919 
00920     return 0;
00921 }
00922 
00932 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
00933 {
00934     if (ftdi == NULL || ftdi->usb_dev == NULL)
00935         ftdi_error_return(-2, "USB device unavailable");
00936 
00937     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00938                                 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
00939                                 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
00940         ftdi_error_return(-1, "FTDI purge of TX buffer failed");
00941 
00942     return 0;
00943 }
00944 
00955 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
00956 {
00957     int result;
00958 
00959     if (ftdi == NULL || ftdi->usb_dev == NULL)
00960         ftdi_error_return(-3, "USB device unavailable");
00961 
00962     result = ftdi_usb_purge_rx_buffer(ftdi);
00963     if (result < 0)
00964         return -1;
00965 
00966     result = ftdi_usb_purge_tx_buffer(ftdi);
00967     if (result < 0)
00968         return -2;
00969 
00970     return 0;
00971 }
00972 
00973 
00974 
00984 int ftdi_usb_close(struct ftdi_context *ftdi)
00985 {
00986     int rtn = 0;
00987 
00988     if (ftdi == NULL)
00989         ftdi_error_return(-3, "ftdi context invalid");
00990 
00991     if (ftdi->usb_dev != NULL)
00992         if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
00993             rtn = -1;
00994 
00995     ftdi_usb_close_internal (ftdi);
00996 
00997     return rtn;
00998 }
00999 
01000 /*  ftdi_to_clkbits_AM For the AM device, convert a requested baudrate 
01001                     to encoded divisor and the achievable baudrate
01002     Function is only used internally
01003     \internal
01004 
01005     See AN120
01006    clk/1   -> 0
01007    clk/1.5 -> 1
01008    clk/2   -> 2
01009    From /2, 0.125/ 0.25 and 0.5 steps may be taken
01010    The fractional part has frac_code encoding
01011 */
01012 static int ftdi_to_clkbits_AM(int baudrate, unsigned long *encoded_divisor)
01013 
01014 {
01015     static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
01016     static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
01017     static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
01018     int divisor, best_divisor, best_baud, best_baud_diff;
01019     divisor = 24000000 / baudrate;
01020     int i;
01021 
01022     // Round down to supported fraction (AM only)
01023     divisor -= am_adjust_dn[divisor & 7];
01024 
01025     // Try this divisor and the one above it (because division rounds down)
01026     best_divisor = 0;
01027     best_baud = 0;
01028     best_baud_diff = 0;
01029     for (i = 0; i < 2; i++)
01030     {
01031         int try_divisor = divisor + i;
01032         int baud_estimate;
01033         int baud_diff;
01034 
01035         // Round up to supported divisor value
01036         if (try_divisor <= 8)
01037         {
01038             // Round up to minimum supported divisor
01039             try_divisor = 8;
01040         }
01041         else if (divisor < 16)
01042         {
01043             // AM doesn't support divisors 9 through 15 inclusive
01044             try_divisor = 16;
01045         }
01046         else
01047         {
01048             // Round up to supported fraction (AM only)
01049             try_divisor += am_adjust_up[try_divisor & 7];
01050             if (try_divisor > 0x1FFF8)
01051             {
01052                 // Round down to maximum supported divisor value (for AM)
01053                 try_divisor = 0x1FFF8;
01054             }
01055         }
01056         // Get estimated baud rate (to nearest integer)
01057         baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
01058         // Get absolute difference from requested baud rate
01059         if (baud_estimate < baudrate)
01060         {
01061             baud_diff = baudrate - baud_estimate;
01062         }
01063         else
01064         {
01065             baud_diff = baud_estimate - baudrate;
01066         }
01067         if (i == 0 || baud_diff < best_baud_diff)
01068         {
01069             // Closest to requested baud rate so far
01070             best_divisor = try_divisor;
01071             best_baud = baud_estimate;
01072             best_baud_diff = baud_diff;
01073             if (baud_diff == 0)
01074             {
01075                 // Spot on! No point trying
01076                 break;
01077             }
01078         }
01079     }
01080     // Encode the best divisor value
01081     *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
01082     // Deal with special cases for encoded value
01083     if (*encoded_divisor == 1)
01084     {
01085         *encoded_divisor = 0;    // 3000000 baud
01086     }
01087     else if (*encoded_divisor == 0x4001)
01088     {
01089         *encoded_divisor = 1;    // 2000000 baud (BM only)
01090     }
01091     return best_baud;
01092 }
01093 
01094 /*  ftdi_to_clkbits Convert a requested baudrate for a given system clock  and predivisor
01095                     to encoded divisor and the achievable baudrate
01096     Function is only used internally
01097     \internal
01098 
01099     See AN120
01100    clk/1   -> 0
01101    clk/1.5 -> 1
01102    clk/2   -> 2
01103    From /2, 0.125 steps may be taken.
01104    The fractional part has frac_code encoding
01105 
01106    value[13:0] of value is the divisor
01107    index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
01108 
01109    H Type have all features above with
01110    {index[8],value[15:14]} is the encoded subdivisor
01111 
01112    FT232R, FT2232 and FT232BM have no option for 12 MHz and with 
01113    {index[0],value[15:14]} is the encoded subdivisor
01114 
01115    AM Type chips have only four fractional subdivisors at value[15:14]
01116    for subdivisors 0, 0.5, 0.25, 0.125
01117 */
01118 static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
01119 {
01120     static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
01121     int best_baud = 0;
01122     int divisor, best_divisor;
01123     if (baudrate >=  clk/clk_div)
01124     {
01125         *encoded_divisor = 0;
01126         best_baud = clk/clk_div;
01127     }
01128     else if (baudrate >=  clk/(clk_div + clk_div/2))
01129     {
01130         *encoded_divisor = 1;
01131         best_baud = clk/(clk_div + clk_div/2);
01132     }
01133     else if (baudrate >=  clk/(2*clk_div))
01134     {
01135         *encoded_divisor = 2;
01136         best_baud = clk/(2*clk_div);
01137     }
01138     else
01139     {
01140         /* We divide by 16 to have 3 fractional bits and one bit for rounding */
01141         divisor = clk*16/clk_div / baudrate;
01142         if (divisor & 1) /* Decide if to round up or down*/
01143             best_divisor = divisor /2 +1;
01144         else
01145             best_divisor = divisor/2;
01146         if(best_divisor > 0x20000)
01147             best_divisor = 0x1ffff;
01148         best_baud = clk*16/clk_div/best_divisor;
01149         if (best_baud & 1) /* Decide if to round up or down*/
01150             best_baud = best_baud /2 +1;
01151         else
01152             best_baud = best_baud /2;
01153         *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
01154     }
01155     return best_baud;
01156 } 
01162 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
01163                                  unsigned short *value, unsigned short *index)
01164 {
01165     int best_baud;
01166     unsigned long encoded_divisor;
01167 
01168     if (baudrate <= 0)
01169     {
01170         // Return error
01171         return -1;
01172     }
01173 
01174 #define H_CLK 120000000
01175 #define C_CLK  48000000
01176     if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H ))
01177     {
01178         if(baudrate*10 > H_CLK /0x3fff)
01179         {
01180             /* On H Devices, use 12 000 000 Baudrate when possible
01181                We have a 14 bit divisor, a 1 bit divisor switch (10 or 16) 
01182                three fractional bits and a 120 MHz clock
01183                Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
01184                DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
01185             best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
01186             encoded_divisor |= 0x20000; /* switch on CLK/10*/
01187         }
01188         else
01189             best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
01190     }
01191     else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
01192     {
01193         best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
01194     }
01195     else
01196     {
01197         best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
01198     }
01199     // Split into "value" and "index" values
01200     *value = (unsigned short)(encoded_divisor & 0xFFFF);
01201     if (ftdi->type == TYPE_2232H || 
01202         ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
01203     {
01204         *index = (unsigned short)(encoded_divisor >> 8);
01205         *index &= 0xFF00;
01206         *index |= ftdi->index;
01207     }
01208     else
01209         *index = (unsigned short)(encoded_divisor >> 16);
01210 
01211     // Return the nearest baud rate
01212     return best_baud;
01213 }
01214 
01219 int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
01220                                  unsigned short *value, unsigned short *index)
01221 {
01222     return ftdi_convert_baudrate(baudrate, ftdi, value, index);
01223 }
01224 
01236 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
01237 {
01238     unsigned short value, index;
01239     int actual_baudrate;
01240 
01241     if (ftdi == NULL || ftdi->usb_dev == NULL)
01242         ftdi_error_return(-3, "USB device unavailable");
01243 
01244     if (ftdi->bitbang_enabled)
01245     {
01246         baudrate = baudrate*4;
01247     }
01248 
01249     actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
01250     if (actual_baudrate <= 0)
01251         ftdi_error_return (-1, "Silly baudrate <= 0.");
01252 
01253     // Check within tolerance (about 5%)
01254     if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
01255             || ((actual_baudrate < baudrate)
01256                 ? (actual_baudrate * 21 < baudrate * 20)
01257                 : (baudrate * 21 < actual_baudrate * 20)))
01258         ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
01259 
01260     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01261                                 SIO_SET_BAUDRATE_REQUEST, value,
01262                                 index, NULL, 0, ftdi->usb_write_timeout) < 0)
01263         ftdi_error_return (-2, "Setting new baudrate failed");
01264 
01265     ftdi->baudrate = baudrate;
01266     return 0;
01267 }
01268 
01282 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01283                            enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
01284 {
01285     return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
01286 }
01287 
01301 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01302                             enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
01303                             enum ftdi_break_type break_type)
01304 {
01305     unsigned short value = bits;
01306 
01307     if (ftdi == NULL || ftdi->usb_dev == NULL)
01308         ftdi_error_return(-2, "USB device unavailable");
01309 
01310     switch (parity)
01311     {
01312         case NONE:
01313             value |= (0x00 << 8);
01314             break;
01315         case ODD:
01316             value |= (0x01 << 8);
01317             break;
01318         case EVEN:
01319             value |= (0x02 << 8);
01320             break;
01321         case MARK:
01322             value |= (0x03 << 8);
01323             break;
01324         case SPACE:
01325             value |= (0x04 << 8);
01326             break;
01327     }
01328 
01329     switch (sbit)
01330     {
01331         case STOP_BIT_1:
01332             value |= (0x00 << 11);
01333             break;
01334         case STOP_BIT_15:
01335             value |= (0x01 << 11);
01336             break;
01337         case STOP_BIT_2:
01338             value |= (0x02 << 11);
01339             break;
01340     }
01341 
01342     switch (break_type)
01343     {
01344         case BREAK_OFF:
01345             value |= (0x00 << 14);
01346             break;
01347         case BREAK_ON:
01348             value |= (0x01 << 14);
01349             break;
01350     }
01351 
01352     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01353                                 SIO_SET_DATA_REQUEST, value,
01354                                 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
01355         ftdi_error_return (-1, "Setting new line property failed");
01356 
01357     return 0;
01358 }
01359 
01371 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
01372 {
01373     int offset = 0;
01374     int actual_length;
01375 
01376     if (ftdi == NULL || ftdi->usb_dev == NULL)
01377         ftdi_error_return(-666, "USB device unavailable");
01378 
01379     while (offset < size)
01380     {
01381         int write_size = ftdi->writebuffer_chunksize;
01382 
01383         if (offset+write_size > size)
01384             write_size = size-offset;
01385 
01386         if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
01387             ftdi_error_return(-1, "usb bulk write failed");
01388 
01389         offset += actual_length;
01390     }
01391 
01392     return offset;
01393 }
01394 
01395 static void ftdi_read_data_cb(struct libusb_transfer *transfer)
01396 {
01397     struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
01398     struct ftdi_context *ftdi = tc->ftdi;
01399     int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
01400 
01401     packet_size = ftdi->max_packet_size;
01402 
01403     actual_length = transfer->actual_length;
01404 
01405     if (actual_length > 2)
01406     {
01407         // skip FTDI status bytes.
01408         // Maybe stored in the future to enable modem use
01409         num_of_chunks = actual_length / packet_size;
01410         chunk_remains = actual_length % packet_size;
01411         //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
01412 
01413         ftdi->readbuffer_offset += 2;
01414         actual_length -= 2;
01415 
01416         if (actual_length > packet_size - 2)
01417         {
01418             for (i = 1; i < num_of_chunks; i++)
01419                 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01420                          ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01421                          packet_size - 2);
01422             if (chunk_remains > 2)
01423             {
01424                 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01425                          ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01426                          chunk_remains-2);
01427                 actual_length -= 2*num_of_chunks;
01428             }
01429             else
01430                 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
01431         }
01432 
01433         if (actual_length > 0)
01434         {
01435             // data still fits in buf?
01436             if (tc->offset + actual_length <= tc->size)
01437             {
01438                 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
01439                 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
01440                 tc->offset += actual_length;
01441 
01442                 ftdi->readbuffer_offset = 0;
01443                 ftdi->readbuffer_remaining = 0;
01444 
01445                 /* Did we read exactly the right amount of bytes? */
01446                 if (tc->offset == tc->size)
01447                 {
01448                     //printf("read_data exact rem %d offset %d\n",
01449                     //ftdi->readbuffer_remaining, offset);
01450                     tc->completed = 1;
01451                     return;
01452                 }
01453             }
01454             else
01455             {
01456                 // only copy part of the data or size <= readbuffer_chunksize
01457                 int part_size = tc->size - tc->offset;
01458                 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
01459                 tc->offset += part_size;
01460 
01461                 ftdi->readbuffer_offset += part_size;
01462                 ftdi->readbuffer_remaining = actual_length - part_size;
01463 
01464                 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
01465                 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
01466                 tc->completed = 1;
01467                 return;
01468             }
01469         }
01470     }
01471     ret = libusb_submit_transfer (transfer);
01472     if (ret < 0)
01473         tc->completed = 1;
01474 }
01475 
01476 
01477 static void ftdi_write_data_cb(struct libusb_transfer *transfer)
01478 {
01479     struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
01480     struct ftdi_context *ftdi = tc->ftdi;
01481 
01482     tc->offset += transfer->actual_length;
01483 
01484     if (tc->offset == tc->size)
01485     {
01486         tc->completed = 1;
01487     }
01488     else
01489     {
01490         int write_size = ftdi->writebuffer_chunksize;
01491         int ret;
01492 
01493         if (tc->offset + write_size > tc->size)
01494             write_size = tc->size - tc->offset;
01495 
01496         transfer->length = write_size;
01497         transfer->buffer = tc->buf + tc->offset;
01498         ret = libusb_submit_transfer (transfer);
01499         if (ret < 0)
01500             tc->completed = 1;
01501     }
01502 }
01503 
01504 
01519 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
01520 {
01521     struct ftdi_transfer_control *tc;
01522     struct libusb_transfer *transfer;
01523     int write_size, ret;
01524 
01525     if (ftdi == NULL || ftdi->usb_dev == NULL)
01526         return NULL;
01527 
01528     tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
01529     if (!tc)
01530         return NULL;
01531 
01532     transfer = libusb_alloc_transfer(0);
01533     if (!transfer)
01534     {
01535         free(tc);
01536         return NULL;
01537     }
01538 
01539     tc->ftdi = ftdi;
01540     tc->completed = 0;
01541     tc->buf = buf;
01542     tc->size = size;
01543     tc->offset = 0;
01544 
01545     if (size < ftdi->writebuffer_chunksize)
01546         write_size = size;
01547     else
01548         write_size = ftdi->writebuffer_chunksize;
01549 
01550     libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
01551                               write_size, ftdi_write_data_cb, tc,
01552                               ftdi->usb_write_timeout);
01553     transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01554 
01555     ret = libusb_submit_transfer(transfer);
01556     if (ret < 0)
01557     {
01558         libusb_free_transfer(transfer);
01559         free(tc);
01560         return NULL;
01561     }
01562     tc->transfer = transfer;
01563 
01564     return tc;
01565 }
01566 
01581 struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
01582 {
01583     struct ftdi_transfer_control *tc;
01584     struct libusb_transfer *transfer;
01585     int ret;
01586 
01587     if (ftdi == NULL || ftdi->usb_dev == NULL)
01588         return NULL;
01589 
01590     tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
01591     if (!tc)
01592         return NULL;
01593 
01594     tc->ftdi = ftdi;
01595     tc->buf = buf;
01596     tc->size = size;
01597 
01598     if (size <= ftdi->readbuffer_remaining)
01599     {
01600         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
01601 
01602         // Fix offsets
01603         ftdi->readbuffer_remaining -= size;
01604         ftdi->readbuffer_offset += size;
01605 
01606         /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
01607 
01608         tc->completed = 1;
01609         tc->offset = size;
01610         tc->transfer = NULL;
01611         return tc;
01612     }
01613 
01614     tc->completed = 0;
01615     if (ftdi->readbuffer_remaining != 0)
01616     {
01617         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
01618 
01619         tc->offset = ftdi->readbuffer_remaining;
01620     }
01621     else
01622         tc->offset = 0;
01623 
01624     transfer = libusb_alloc_transfer(0);
01625     if (!transfer)
01626     {
01627         free (tc);
01628         return NULL;
01629     }
01630 
01631     ftdi->readbuffer_remaining = 0;
01632     ftdi->readbuffer_offset = 0;
01633 
01634     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);
01635     transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01636 
01637     ret = libusb_submit_transfer(transfer);
01638     if (ret < 0)
01639     {
01640         libusb_free_transfer(transfer);
01641         free (tc);
01642         return NULL;
01643     }
01644     tc->transfer = transfer;
01645 
01646     return tc;
01647 }
01648 
01660 int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
01661 {
01662     int ret;
01663 
01664     while (!tc->completed)
01665     {
01666         ret = libusb_handle_events(tc->ftdi->usb_ctx);
01667         if (ret < 0)
01668         {
01669             if (ret == LIBUSB_ERROR_INTERRUPTED)
01670                 continue;
01671             libusb_cancel_transfer(tc->transfer);
01672             while (!tc->completed)
01673                 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
01674                     break;
01675             libusb_free_transfer(tc->transfer);
01676             free (tc);
01677             return ret;
01678         }
01679     }
01680 
01681     ret = tc->offset;
01686     if (tc->transfer)
01687     {
01688         if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
01689             ret = -1;
01690         libusb_free_transfer(tc->transfer);
01691     }
01692     free(tc);
01693     return ret;
01694 }
01695 
01706 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
01707 {
01708     if (ftdi == NULL)
01709         ftdi_error_return(-1, "ftdi context invalid");
01710 
01711     ftdi->writebuffer_chunksize = chunksize;
01712     return 0;
01713 }
01714 
01724 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
01725 {
01726     if (ftdi == NULL)
01727         ftdi_error_return(-1, "ftdi context invalid");
01728 
01729     *chunksize = ftdi->writebuffer_chunksize;
01730     return 0;
01731 }
01732 
01748 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
01749 {
01750     int offset = 0, ret, i, num_of_chunks, chunk_remains;
01751     int packet_size = ftdi->max_packet_size;
01752     int actual_length = 1;
01753 
01754     if (ftdi == NULL || ftdi->usb_dev == NULL)
01755         ftdi_error_return(-666, "USB device unavailable");
01756 
01757     // Packet size sanity check (avoid division by zero)
01758     if (packet_size == 0)
01759         ftdi_error_return(-1, "max_packet_size is bogus (zero)");
01760 
01761     // everything we want is still in the readbuffer?
01762     if (size <= ftdi->readbuffer_remaining)
01763     {
01764         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
01765 
01766         // Fix offsets
01767         ftdi->readbuffer_remaining -= size;
01768         ftdi->readbuffer_offset += size;
01769 
01770         /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
01771 
01772         return size;
01773     }
01774     // something still in the readbuffer, but not enough to satisfy 'size'?
01775     if (ftdi->readbuffer_remaining != 0)
01776     {
01777         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
01778 
01779         // Fix offset
01780         offset += ftdi->readbuffer_remaining;
01781     }
01782     // do the actual USB read
01783     while (offset < size && actual_length > 0)
01784     {
01785         ftdi->readbuffer_remaining = 0;
01786         ftdi->readbuffer_offset = 0;
01787         /* returns how much received */
01788         ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
01789         if (ret < 0)
01790             ftdi_error_return(ret, "usb bulk read failed");
01791 
01792         if (actual_length > 2)
01793         {
01794             // skip FTDI status bytes.
01795             // Maybe stored in the future to enable modem use
01796             num_of_chunks = actual_length / packet_size;
01797             chunk_remains = actual_length % packet_size;
01798             //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
01799 
01800             ftdi->readbuffer_offset += 2;
01801             actual_length -= 2;
01802 
01803             if (actual_length > packet_size - 2)
01804             {
01805                 for (i = 1; i < num_of_chunks; i++)
01806                     memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01807                              ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01808                              packet_size - 2);
01809                 if (chunk_remains > 2)
01810                 {
01811                     memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01812                              ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01813                              chunk_remains-2);
01814                     actual_length -= 2*num_of_chunks;
01815                 }
01816                 else
01817                     actual_length -= 2*(num_of_chunks-1)+chunk_remains;
01818             }
01819         }
01820         else if (actual_length <= 2)
01821         {
01822             // no more data to read?
01823             return offset;
01824         }
01825         if (actual_length > 0)
01826         {
01827             // data still fits in buf?
01828             if (offset+actual_length <= size)
01829             {
01830                 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
01831                 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
01832                 offset += actual_length;
01833 
01834                 /* Did we read exactly the right amount of bytes? */
01835                 if (offset == size)
01836                     //printf("read_data exact rem %d offset %d\n",
01837                     //ftdi->readbuffer_remaining, offset);
01838                     return offset;
01839             }
01840             else
01841             {
01842                 // only copy part of the data or size <= readbuffer_chunksize
01843                 int part_size = size-offset;
01844                 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
01845 
01846                 ftdi->readbuffer_offset += part_size;
01847                 ftdi->readbuffer_remaining = actual_length-part_size;
01848                 offset += part_size;
01849 
01850                 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
01851                 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
01852 
01853                 return offset;
01854             }
01855         }
01856     }
01857     // never reached
01858     return -127;
01859 }
01860 
01873 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
01874 {
01875     unsigned char *new_buf;
01876 
01877     if (ftdi == NULL)
01878         ftdi_error_return(-1, "ftdi context invalid");
01879 
01880     // Invalidate all remaining data
01881     ftdi->readbuffer_offset = 0;
01882     ftdi->readbuffer_remaining = 0;
01883 #ifdef __linux__
01884     /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
01885        which is defined in libusb-1.0.  Otherwise, each USB read request will
01886        be divided into multiple URBs.  This will cause issues on Linux kernel
01887        older than 2.6.32.  */
01888     if (chunksize > 16384)
01889         chunksize = 16384;
01890 #endif
01891 
01892     if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
01893         ftdi_error_return(-1, "out of memory for readbuffer");
01894 
01895     ftdi->readbuffer = new_buf;
01896     ftdi->readbuffer_chunksize = chunksize;
01897 
01898     return 0;
01899 }
01900 
01910 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
01911 {
01912     if (ftdi == NULL)
01913         ftdi_error_return(-1, "FTDI context invalid");
01914 
01915     *chunksize = ftdi->readbuffer_chunksize;
01916     return 0;
01917 }
01918 
01931 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
01932 {
01933     unsigned short usb_val;
01934 
01935     if (ftdi == NULL || ftdi->usb_dev == NULL)
01936         ftdi_error_return(-2, "USB device unavailable");
01937 
01938     usb_val = bitmask; // low byte: bitmask
01939     usb_val |= (mode << 8);
01940     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)
01941         ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a BM/2232C type chip?");
01942 
01943     ftdi->bitbang_mode = mode;
01944     ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
01945     return 0;
01946 }
01947 
01957 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
01958 {
01959     if (ftdi == NULL || ftdi->usb_dev == NULL)
01960         ftdi_error_return(-2, "USB device unavailable");
01961 
01962     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)
01963         ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
01964 
01965     ftdi->bitbang_enabled = 0;
01966     return 0;
01967 }
01968 
01969 
01980 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
01981 {
01982     if (ftdi == NULL || ftdi->usb_dev == NULL)
01983         ftdi_error_return(-2, "USB device unavailable");
01984 
01985     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)
01986         ftdi_error_return(-1, "read pins failed");
01987 
01988     return 0;
01989 }
01990 
02006 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
02007 {
02008     unsigned short usb_val;
02009 
02010     if (latency < 1)
02011         ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
02012 
02013     if (ftdi == NULL || ftdi->usb_dev == NULL)
02014         ftdi_error_return(-3, "USB device unavailable");
02015 
02016     usb_val = latency;
02017     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)
02018         ftdi_error_return(-2, "unable to set latency timer");
02019 
02020     return 0;
02021 }
02022 
02033 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
02034 {
02035     unsigned short usb_val;
02036 
02037     if (ftdi == NULL || ftdi->usb_dev == NULL)
02038         ftdi_error_return(-2, "USB device unavailable");
02039 
02040     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)
02041         ftdi_error_return(-1, "reading latency timer failed");
02042 
02043     *latency = (unsigned char)usb_val;
02044     return 0;
02045 }
02046 
02087 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
02088 {
02089     char usb_val[2];
02090 
02091     if (ftdi == NULL || ftdi->usb_dev == NULL)
02092         ftdi_error_return(-2, "USB device unavailable");
02093 
02094     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)
02095         ftdi_error_return(-1, "getting modem status failed");
02096 
02097     *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
02098 
02099     return 0;
02100 }
02101 
02113 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
02114 {
02115     if (ftdi == NULL || ftdi->usb_dev == NULL)
02116         ftdi_error_return(-2, "USB device unavailable");
02117 
02118     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02119                                 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
02120                                 NULL, 0, ftdi->usb_write_timeout) < 0)
02121         ftdi_error_return(-1, "set flow control failed");
02122 
02123     return 0;
02124 }
02125 
02136 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
02137 {
02138     unsigned short usb_val;
02139 
02140     if (ftdi == NULL || ftdi->usb_dev == NULL)
02141         ftdi_error_return(-2, "USB device unavailable");
02142 
02143     if (state)
02144         usb_val = SIO_SET_DTR_HIGH;
02145     else
02146         usb_val = SIO_SET_DTR_LOW;
02147 
02148     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02149                                 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02150                                 NULL, 0, ftdi->usb_write_timeout) < 0)
02151         ftdi_error_return(-1, "set dtr failed");
02152 
02153     return 0;
02154 }
02155 
02166 int ftdi_setrts(struct ftdi_context *ftdi, int state)
02167 {
02168     unsigned short usb_val;
02169 
02170     if (ftdi == NULL || ftdi->usb_dev == NULL)
02171         ftdi_error_return(-2, "USB device unavailable");
02172 
02173     if (state)
02174         usb_val = SIO_SET_RTS_HIGH;
02175     else
02176         usb_val = SIO_SET_RTS_LOW;
02177 
02178     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02179                                 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02180                                 NULL, 0, ftdi->usb_write_timeout) < 0)
02181         ftdi_error_return(-1, "set of rts failed");
02182 
02183     return 0;
02184 }
02185 
02197 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
02198 {
02199     unsigned short usb_val;
02200 
02201     if (ftdi == NULL || ftdi->usb_dev == NULL)
02202         ftdi_error_return(-2, "USB device unavailable");
02203 
02204     if (dtr)
02205         usb_val = SIO_SET_DTR_HIGH;
02206     else
02207         usb_val = SIO_SET_DTR_LOW;
02208 
02209     if (rts)
02210         usb_val |= SIO_SET_RTS_HIGH;
02211     else
02212         usb_val |= SIO_SET_RTS_LOW;
02213 
02214     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02215                                 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02216                                 NULL, 0, ftdi->usb_write_timeout) < 0)
02217         ftdi_error_return(-1, "set of rts/dtr failed");
02218 
02219     return 0;
02220 }
02221 
02233 int ftdi_set_event_char(struct ftdi_context *ftdi,
02234                         unsigned char eventch, unsigned char enable)
02235 {
02236     unsigned short usb_val;
02237 
02238     if (ftdi == NULL || ftdi->usb_dev == NULL)
02239         ftdi_error_return(-2, "USB device unavailable");
02240 
02241     usb_val = eventch;
02242     if (enable)
02243         usb_val |= 1 << 8;
02244 
02245     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)
02246         ftdi_error_return(-1, "setting event character failed");
02247 
02248     return 0;
02249 }
02250 
02262 int ftdi_set_error_char(struct ftdi_context *ftdi,
02263                         unsigned char errorch, unsigned char enable)
02264 {
02265     unsigned short usb_val;
02266 
02267     if (ftdi == NULL || ftdi->usb_dev == NULL)
02268         ftdi_error_return(-2, "USB device unavailable");
02269 
02270     usb_val = errorch;
02271     if (enable)
02272         usb_val |= 1 << 8;
02273 
02274     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)
02275         ftdi_error_return(-1, "setting error character failed");
02276 
02277     return 0;
02278 }
02279 
02292 int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
02293                              char * product, char * serial)
02294 {
02295     struct ftdi_eeprom *eeprom;
02296 
02297     if (ftdi == NULL)
02298         ftdi_error_return(-1, "No struct ftdi_context");
02299 
02300     if (ftdi->eeprom == NULL)
02301         ftdi_error_return(-2,"No struct ftdi_eeprom");
02302 
02303     eeprom = ftdi->eeprom;
02304     memset(eeprom, 0, sizeof(struct ftdi_eeprom));
02305 
02306     if (ftdi->usb_dev == NULL)
02307         ftdi_error_return(-3, "No connected device or device not yet opened");
02308 
02309     eeprom->vendor_id = 0x0403;
02310     eeprom->use_serial = 1;
02311     if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
02312             (ftdi->type == TYPE_R))
02313         eeprom->product_id = 0x6001;
02314     else if (ftdi->type == TYPE_4232H)
02315         eeprom->product_id = 0x6011;
02316     else if (ftdi->type == TYPE_232H)
02317         eeprom->product_id = 0x6014;
02318     else
02319         eeprom->product_id = 0x6010;
02320     if (ftdi->type == TYPE_AM)
02321         eeprom->usb_version = 0x0101;
02322     else
02323         eeprom->usb_version = 0x0200;
02324     eeprom->max_power = 100;
02325 
02326     if (eeprom->manufacturer)
02327         free (eeprom->manufacturer);
02328     eeprom->manufacturer = NULL;
02329     if (manufacturer)
02330     {
02331         eeprom->manufacturer = malloc(strlen(manufacturer)+1);
02332         if (eeprom->manufacturer)
02333             strcpy(eeprom->manufacturer, manufacturer);
02334     }
02335 
02336     if (eeprom->product)
02337         free (eeprom->product);
02338     eeprom->product = NULL;
02339     if(product)
02340     {
02341         eeprom->product = malloc(strlen(product)+1);
02342         if (eeprom->product)
02343             strcpy(eeprom->product, product);
02344     }
02345     else
02346     {
02347         const char* default_product;
02348         switch(ftdi->type)
02349         {
02350         case TYPE_AM:    default_product = "AM"; break;
02351         case TYPE_BM:    default_product = "BM"; break;
02352         case TYPE_2232C: default_product = "Dual RS232"; break;
02353         case TYPE_R:     default_product = "FT232R USB UART"; break;
02354         case TYPE_2232H: default_product = "Dual RS232-HS"; break;
02355         case TYPE_4232H: default_product = "FT4232H"; break;
02356         case TYPE_232H:  default_product = "Single-RS232-HS"; break;
02357         default:
02358         ftdi_error_return(-3, "Unknown chip type");
02359         }
02360         eeprom->product = malloc(strlen(default_product) +1);
02361         if (eeprom->product)
02362             strcpy(eeprom->product, default_product);
02363     }
02364 
02365     if (eeprom->serial)
02366         free (eeprom->serial);
02367     eeprom->serial = NULL;
02368     if (serial)
02369     {
02370         eeprom->serial = malloc(strlen(serial)+1);
02371         if (eeprom->serial)
02372             strcpy(eeprom->serial, serial);
02373     }
02374 
02375     if (ftdi->type == TYPE_R)
02376     {
02377         eeprom->max_power = 90;
02378         eeprom->size = 0x80;
02379         eeprom->cbus_function[0] = CBUS_TXLED;
02380         eeprom->cbus_function[1] = CBUS_RXLED;
02381         eeprom->cbus_function[2] = CBUS_TXDEN;
02382         eeprom->cbus_function[3] = CBUS_PWREN;
02383         eeprom->cbus_function[4] = CBUS_SLEEP;
02384     }
02385     else
02386     {
02387         if(ftdi->type == TYPE_232H)
02388         {
02389             int i;
02390             for (i=0; i<10; i++)
02391                 eeprom->cbus_function[i] = CBUSH_TRISTATE;
02392         }
02393         eeprom->size = -1;
02394     }
02395     eeprom->initialized_for_connected_device = 1;
02396     return 0;
02397 }
02398 /*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
02399 void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
02400 {
02401     int i;
02402     for(i=0; i<5;i++)
02403     {
02404         int mode_low, mode_high;
02405         if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
02406             mode_low = CBUSH_TRISTATE;
02407         else
02408             mode_low = eeprom->cbus_function[2*i];
02409         if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
02410             mode_high = CBUSH_TRISTATE;
02411         else
02412             mode_high = eeprom->cbus_function[2*i+1];
02413 
02414         output[0x18+i] = (mode_high <<4) | mode_low;
02415     }
02416 }
02417 /* Return the bits for the encoded EEPROM Structure of a requested Mode
02418  *
02419  */
02420 static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
02421 {
02422     switch (chip)
02423     {
02424     case TYPE_2232H:
02425     case TYPE_2232C:
02426     {
02427         switch (type)
02428         {
02429         case CHANNEL_IS_UART: return 0;
02430         case CHANNEL_IS_FIFO: return 0x01;
02431         case CHANNEL_IS_OPTO: return 0x02;
02432         case CHANNEL_IS_CPU : return 0x04;
02433         default: return 0;
02434         }
02435     }
02436     case TYPE_232H:
02437     {
02438         switch (type)
02439         {
02440         case CHANNEL_IS_UART   : return 0;
02441         case CHANNEL_IS_FIFO   : return 0x01;
02442         case CHANNEL_IS_OPTO   : return 0x02;
02443         case CHANNEL_IS_CPU    : return 0x04;
02444         case CHANNEL_IS_FT1284 : return 0x08;
02445         default: return 0;
02446         }
02447     }
02448     default: return 0;
02449     }
02450     return 0;
02451 }    
02452 
02467 int ftdi_eeprom_build(struct ftdi_context *ftdi)
02468 {
02469     unsigned char i, j, eeprom_size_mask;
02470     unsigned short checksum, value;
02471     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
02472     int user_area_size;
02473     struct ftdi_eeprom *eeprom;
02474     unsigned char * output;
02475 
02476     if (ftdi == NULL)
02477         ftdi_error_return(-2,"No context");
02478     if (ftdi->eeprom == NULL)
02479         ftdi_error_return(-2,"No eeprom structure");
02480 
02481     eeprom= ftdi->eeprom;
02482     output = eeprom->buf;
02483 
02484     if (eeprom->chip == -1)
02485         ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
02486 
02487     if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
02488         eeprom->size = 0x100;
02489     else
02490         eeprom->size = 0x80;
02491 
02492     if (eeprom->manufacturer != NULL)
02493         manufacturer_size = strlen(eeprom->manufacturer);
02494     if (eeprom->product != NULL)
02495         product_size = strlen(eeprom->product);
02496     if (eeprom->serial != NULL)
02497         serial_size = strlen(eeprom->serial);
02498 
02499     // eeprom size check
02500     switch (ftdi->type)
02501     {
02502         case TYPE_AM:
02503         case TYPE_BM:
02504             user_area_size = 96;    // base size for strings (total of 48 characters)
02505             break;
02506         case TYPE_2232C:
02507             user_area_size = 90;     // two extra config bytes and 4 bytes PnP stuff
02508             break;
02509         case TYPE_R:
02510             user_area_size = 88;     // four extra config bytes + 4 bytes PnP stuff
02511             break;
02512         case TYPE_2232H:            // six extra config bytes + 4 bytes PnP stuff
02513         case TYPE_4232H:
02514             user_area_size = 86;
02515             break;
02516         case TYPE_232H:
02517             user_area_size = 80;
02518             break;
02519         default:
02520             user_area_size = 0;
02521             break;
02522     }
02523     user_area_size  -= (manufacturer_size + product_size + serial_size) * 2;
02524 
02525     if (user_area_size < 0)
02526         ftdi_error_return(-1,"eeprom size exceeded");
02527 
02528     // empty eeprom
02529     memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
02530 
02531     // Bytes and Bits set for all Types
02532 
02533     // Addr 02: Vendor ID
02534     output[0x02] = eeprom->vendor_id;
02535     output[0x03] = eeprom->vendor_id >> 8;
02536 
02537     // Addr 04: Product ID
02538     output[0x04] = eeprom->product_id;
02539     output[0x05] = eeprom->product_id >> 8;
02540 
02541     // Addr 06: Device release number (0400h for BM features)
02542     output[0x06] = 0x00;
02543     switch (ftdi->type)
02544     {
02545         case TYPE_AM:
02546             output[0x07] = 0x02;
02547             break;
02548         case TYPE_BM:
02549             output[0x07] = 0x04;
02550             break;
02551         case TYPE_2232C:
02552             output[0x07] = 0x05;
02553             break;
02554         case TYPE_R:
02555             output[0x07] = 0x06;
02556             break;
02557         case TYPE_2232H:
02558             output[0x07] = 0x07;
02559             break;
02560         case TYPE_4232H:
02561             output[0x07] = 0x08;
02562             break;
02563         case TYPE_232H:
02564             output[0x07] = 0x09;
02565             break;
02566         default:
02567             output[0x07] = 0x00;
02568     }
02569 
02570     // Addr 08: Config descriptor
02571     // Bit 7: always 1
02572     // Bit 6: 1 if this device is self powered, 0 if bus powered
02573     // Bit 5: 1 if this device uses remote wakeup
02574     // Bit 4-0: reserved - 0
02575     j = 0x80;
02576     if (eeprom->self_powered == 1)
02577         j |= 0x40;
02578     if (eeprom->remote_wakeup == 1)
02579         j |= 0x20;
02580     output[0x08] = j;
02581 
02582     // Addr 09: Max power consumption: max power = value * 2 mA
02583     output[0x09] = eeprom->max_power / MAX_POWER_MILLIAMP_PER_UNIT;
02584 
02585     if (ftdi->type != TYPE_AM)
02586     {
02587         // Addr 0A: Chip configuration
02588         // Bit 7: 0 - reserved
02589         // Bit 6: 0 - reserved
02590         // Bit 5: 0 - reserved
02591         // Bit 4: 1 - Change USB version
02592         // Bit 3: 1 - Use the serial number string
02593         // Bit 2: 1 - Enable suspend pull downs for lower power
02594         // Bit 1: 1 - Out EndPoint is Isochronous
02595         // Bit 0: 1 - In EndPoint is Isochronous
02596         //
02597         j = 0;
02598         if (eeprom->in_is_isochronous == 1)
02599             j = j | 1;
02600         if (eeprom->out_is_isochronous == 1)
02601             j = j | 2;
02602         output[0x0A] = j;
02603     }
02604 
02605     // Dynamic content
02606     // Strings start at 0x94 (TYPE_AM, TYPE_BM)
02607     // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
02608     // 0xa0 (TYPE_232H)
02609     i = 0;
02610     switch (ftdi->type)
02611     {
02612         case TYPE_232H:
02613             i += 2;
02614         case TYPE_2232H:
02615         case TYPE_4232H:
02616             i += 2;
02617         case TYPE_R:
02618             i += 2;
02619         case TYPE_2232C:
02620             i += 2;
02621         case TYPE_AM:
02622         case TYPE_BM:
02623             i += 0x94;
02624     }
02625     /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
02626     eeprom_size_mask = eeprom->size -1;
02627 
02628     // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
02629     // Addr 0F: Length of manufacturer string
02630     // Output manufacturer
02631     output[0x0E] = i;  // calculate offset
02632     output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
02633     output[i & eeprom_size_mask] = 0x03, i++; // type: string
02634     for (j = 0; j < manufacturer_size; j++)
02635     {
02636         output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
02637         output[i & eeprom_size_mask] = 0x00, i++;
02638     }
02639     output[0x0F] = manufacturer_size*2 + 2;
02640 
02641     // Addr 10: Offset of the product string + 0x80, calculated later
02642     // Addr 11: Length of product string
02643     output[0x10] = i | 0x80;  // calculate offset
02644     output[i & eeprom_size_mask] = product_size*2 + 2, i++;
02645     output[i & eeprom_size_mask] = 0x03, i++;
02646     for (j = 0; j < product_size; j++)
02647     {
02648         output[i & eeprom_size_mask] = eeprom->product[j], i++;
02649         output[i & eeprom_size_mask] = 0x00, i++;
02650     }
02651     output[0x11] = product_size*2 + 2;
02652 
02653     // Addr 12: Offset of the serial string + 0x80, calculated later
02654     // Addr 13: Length of serial string
02655     output[0x12] = i | 0x80; // calculate offset
02656     output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
02657     output[i & eeprom_size_mask] = 0x03, i++;
02658     for (j = 0; j < serial_size; j++)
02659     {
02660         output[i & eeprom_size_mask] = eeprom->serial[j], i++;
02661         output[i & eeprom_size_mask] = 0x00, i++;
02662     }
02663 
02664     // Legacy port name and PnP fields for FT2232 and newer chips
02665     if (ftdi->type > TYPE_BM)
02666     {
02667         output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
02668         i++;
02669         output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
02670         i++;
02671         output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
02672         i++;
02673     }
02674 
02675     output[0x13] = serial_size*2 + 2;
02676 
02677     if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
02678     {
02679         if (eeprom->use_serial)
02680             output[0x0A] |= USE_SERIAL_NUM;
02681         else
02682             output[0x0A] &= ~USE_SERIAL_NUM;
02683     }
02684 
02685     /* Bytes and Bits specific to (some) types
02686        Write linear, as this allows easier fixing*/
02687     switch (ftdi->type)
02688     {
02689         case TYPE_AM:
02690             break;
02691         case TYPE_BM:
02692             output[0x0C] = eeprom->usb_version & 0xff;
02693             output[0x0D] = (eeprom->usb_version>>8) & 0xff;
02694             if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
02695                 output[0x0A] |= USE_USB_VERSION_BIT;
02696             else
02697                 output[0x0A] &= ~USE_USB_VERSION_BIT;
02698 
02699             break;
02700         case TYPE_2232C:
02701 
02702             output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
02703             if ( eeprom->channel_a_driver == DRIVER_VCP)
02704                 output[0x00] |= DRIVER_VCP;
02705             else
02706                 output[0x00] &= ~DRIVER_VCP;
02707 
02708             if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
02709                 output[0x00] |= HIGH_CURRENT_DRIVE;
02710             else
02711                 output[0x00] &= ~HIGH_CURRENT_DRIVE;
02712 
02713             output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
02714             if ( eeprom->channel_b_driver == DRIVER_VCP)
02715                 output[0x01] |= DRIVER_VCP;
02716             else
02717                 output[0x01] &= ~DRIVER_VCP;
02718 
02719             if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
02720                 output[0x01] |= HIGH_CURRENT_DRIVE;
02721             else
02722                 output[0x01] &= ~HIGH_CURRENT_DRIVE;
02723 
02724             if (eeprom->in_is_isochronous == 1)
02725                 output[0x0A] |= 0x1;
02726             else
02727                 output[0x0A] &= ~0x1;
02728             if (eeprom->out_is_isochronous == 1)
02729                 output[0x0A] |= 0x2;
02730             else
02731                 output[0x0A] &= ~0x2;
02732             if (eeprom->suspend_pull_downs == 1)
02733                 output[0x0A] |= 0x4;
02734             else
02735                 output[0x0A] &= ~0x4;
02736             if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
02737                 output[0x0A] |= USE_USB_VERSION_BIT;
02738             else
02739                 output[0x0A] &= ~USE_USB_VERSION_BIT;
02740 
02741             output[0x0C] = eeprom->usb_version & 0xff;
02742             output[0x0D] = (eeprom->usb_version>>8) & 0xff;
02743             output[0x14] = eeprom->chip;
02744             break;
02745         case TYPE_R:
02746             if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
02747                 output[0x00] |= HIGH_CURRENT_DRIVE_R;
02748             output[0x01] = 0x40; /* Hard coded Endpoint Size*/
02749 
02750             if (eeprom->suspend_pull_downs == 1)
02751                 output[0x0A] |= 0x4;
02752             else
02753                 output[0x0A] &= ~0x4;
02754             output[0x0B] = eeprom->invert;
02755             output[0x0C] = eeprom->usb_version & 0xff;
02756             output[0x0D] = (eeprom->usb_version>>8) & 0xff;
02757 
02758             if (eeprom->cbus_function[0] > CBUS_BB)
02759                 output[0x14] = CBUS_TXLED;
02760             else
02761                 output[0x14] = eeprom->cbus_function[0];
02762 
02763             if (eeprom->cbus_function[1] > CBUS_BB)
02764                 output[0x14] |= CBUS_RXLED<<4;
02765             else
02766                 output[0x14] |= eeprom->cbus_function[1]<<4;
02767 
02768             if (eeprom->cbus_function[2] > CBUS_BB)
02769                 output[0x15] = CBUS_TXDEN;
02770             else
02771                 output[0x15] = eeprom->cbus_function[2];
02772 
02773             if (eeprom->cbus_function[3] > CBUS_BB)
02774                 output[0x15] |= CBUS_PWREN<<4;
02775             else
02776                 output[0x15] |= eeprom->cbus_function[3]<<4;
02777 
02778             if (eeprom->cbus_function[4] > CBUS_CLK6)
02779                 output[0x16] = CBUS_SLEEP;
02780             else
02781                 output[0x16] = eeprom->cbus_function[4];
02782             break;
02783         case TYPE_2232H:
02784             output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
02785             if ( eeprom->channel_a_driver == DRIVER_VCP)
02786                 output[0x00] |= DRIVER_VCP;
02787             else
02788                 output[0x00] &= ~DRIVER_VCP;
02789 
02790             output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
02791             if ( eeprom->channel_b_driver == DRIVER_VCP)
02792                 output[0x01] |= DRIVER_VCP;
02793             else
02794                 output[0x01] &= ~DRIVER_VCP;
02795             if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
02796                 output[0x01] |= SUSPEND_DBUS7_BIT;
02797             else
02798                 output[0x01] &= ~SUSPEND_DBUS7_BIT;
02799 
02800             if (eeprom->suspend_pull_downs == 1)
02801                 output[0x0A] |= 0x4;
02802             else
02803                 output[0x0A] &= ~0x4;
02804 
02805             if (eeprom->group0_drive > DRIVE_16MA)
02806                 output[0x0c] |= DRIVE_16MA;
02807             else
02808                 output[0x0c] |= eeprom->group0_drive;
02809             if (eeprom->group0_schmitt == IS_SCHMITT)
02810                 output[0x0c] |= IS_SCHMITT;
02811             if (eeprom->group0_slew == SLOW_SLEW)
02812                 output[0x0c] |= SLOW_SLEW;
02813 
02814             if (eeprom->group1_drive > DRIVE_16MA)
02815                 output[0x0c] |= DRIVE_16MA<<4;
02816             else
02817                 output[0x0c] |= eeprom->group1_drive<<4;
02818             if (eeprom->group1_schmitt == IS_SCHMITT)
02819                 output[0x0c] |= IS_SCHMITT<<4;
02820             if (eeprom->group1_slew == SLOW_SLEW)
02821                 output[0x0c] |= SLOW_SLEW<<4;
02822 
02823             if (eeprom->group2_drive > DRIVE_16MA)
02824                 output[0x0d] |= DRIVE_16MA;
02825             else
02826                 output[0x0d] |= eeprom->group2_drive;
02827             if (eeprom->group2_schmitt == IS_SCHMITT)
02828                 output[0x0d] |= IS_SCHMITT;
02829             if (eeprom->group2_slew == SLOW_SLEW)
02830                 output[0x0d] |= SLOW_SLEW;
02831 
02832             if (eeprom->group3_drive > DRIVE_16MA)
02833                 output[0x0d] |= DRIVE_16MA<<4;
02834             else
02835                 output[0x0d] |= eeprom->group3_drive<<4;
02836             if (eeprom->group3_schmitt == IS_SCHMITT)
02837                 output[0x0d] |= IS_SCHMITT<<4;
02838             if (eeprom->group3_slew == SLOW_SLEW)
02839                 output[0x0d] |= SLOW_SLEW<<4;
02840 
02841             output[0x18] = eeprom->chip;
02842 
02843             break;
02844         case TYPE_4232H:
02845             if (eeprom->channel_a_driver == DRIVER_VCP)
02846                 output[0x00] |= DRIVER_VCP;
02847             else
02848                 output[0x00] &= ~DRIVER_VCP;
02849             if (eeprom->channel_b_driver == DRIVER_VCP)
02850                 output[0x01] |= DRIVER_VCP;
02851             else
02852                 output[0x01] &= ~DRIVER_VCP;
02853             if (eeprom->channel_c_driver == DRIVER_VCP)
02854                 output[0x00] |= (DRIVER_VCP << 4);
02855             else
02856                 output[0x00] &= ~(DRIVER_VCP << 4);
02857             if (eeprom->channel_d_driver == DRIVER_VCP)
02858                 output[0x01] |= (DRIVER_VCP << 4);
02859             else
02860                 output[0x01] &= ~(DRIVER_VCP << 4);
02861 
02862             if (eeprom->suspend_pull_downs == 1)
02863                 output[0x0a] |= 0x4;
02864             else
02865                 output[0x0a] &= ~0x4;
02866 
02867             if (eeprom->channel_a_rs485enable)
02868                 output[0x0b] |= CHANNEL_IS_RS485 << 0;
02869             else
02870                 output[0x0b] &= ~(CHANNEL_IS_RS485 << 0);
02871             if (eeprom->channel_b_rs485enable)
02872                 output[0x0b] |= CHANNEL_IS_RS485 << 1;
02873             else
02874                 output[0x0b] &= ~(CHANNEL_IS_RS485 << 1);
02875             if (eeprom->channel_c_rs485enable)
02876                 output[0x0b] |= CHANNEL_IS_RS485 << 2;
02877             else
02878                 output[0x0b] &= ~(CHANNEL_IS_RS485 << 2);
02879             if (eeprom->channel_d_rs485enable)
02880                 output[0x0b] |= CHANNEL_IS_RS485 << 3;
02881             else
02882                 output[0x0b] &= ~(CHANNEL_IS_RS485 << 3);
02883 
02884             if (eeprom->group0_drive > DRIVE_16MA)
02885                 output[0x0c] |= DRIVE_16MA;
02886             else
02887                 output[0x0c] |= eeprom->group0_drive;
02888             if (eeprom->group0_schmitt == IS_SCHMITT)
02889                 output[0x0c] |= IS_SCHMITT;
02890             if (eeprom->group0_slew == SLOW_SLEW)
02891                 output[0x0c] |= SLOW_SLEW;
02892 
02893             if (eeprom->group1_drive > DRIVE_16MA)
02894                 output[0x0c] |= DRIVE_16MA<<4;
02895             else
02896                 output[0x0c] |= eeprom->group1_drive<<4;
02897             if (eeprom->group1_schmitt == IS_SCHMITT)
02898                 output[0x0c] |= IS_SCHMITT<<4;
02899             if (eeprom->group1_slew == SLOW_SLEW)
02900                 output[0x0c] |= SLOW_SLEW<<4;
02901 
02902             if (eeprom->group2_drive > DRIVE_16MA)
02903                 output[0x0d] |= DRIVE_16MA;
02904             else
02905                 output[0x0d] |= eeprom->group2_drive;
02906             if (eeprom->group2_schmitt == IS_SCHMITT)
02907                 output[0x0d] |= IS_SCHMITT;
02908             if (eeprom->group2_slew == SLOW_SLEW)
02909                 output[0x0d] |= SLOW_SLEW;
02910 
02911             if (eeprom->group3_drive > DRIVE_16MA)
02912                 output[0x0d] |= DRIVE_16MA<<4;
02913             else
02914                 output[0x0d] |= eeprom->group3_drive<<4;
02915             if (eeprom->group3_schmitt == IS_SCHMITT)
02916                 output[0x0d] |= IS_SCHMITT<<4;
02917             if (eeprom->group3_slew == SLOW_SLEW)
02918                 output[0x0d] |= SLOW_SLEW<<4;
02919 
02920             output[0x18] = eeprom->chip;
02921 
02922             break;
02923         case TYPE_232H:
02924             output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
02925             if ( eeprom->channel_a_driver == DRIVER_VCP)
02926                 output[0x00] |= DRIVER_VCPH;
02927             else
02928                 output[0x00] &= ~DRIVER_VCPH;
02929             if (eeprom->powersave)
02930                 output[0x01] |= POWER_SAVE_DISABLE_H;
02931             else
02932                 output[0x01] &= ~POWER_SAVE_DISABLE_H;
02933             if (eeprom->clock_polarity)
02934                 output[0x01] |= FT1284_CLK_IDLE_STATE;
02935             else
02936                 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
02937             if (eeprom->data_order)
02938                 output[0x01] |= FT1284_DATA_LSB;
02939             else
02940                 output[0x01] &= ~FT1284_DATA_LSB;
02941             if (eeprom->flow_control)
02942                 output[0x01] |= FT1284_FLOW_CONTROL;
02943             else
02944                 output[0x01] &= ~FT1284_FLOW_CONTROL;
02945             if (eeprom->group0_drive > DRIVE_16MA)
02946                 output[0x0c] |= DRIVE_16MA;
02947             else
02948                 output[0x0c] |= eeprom->group0_drive;
02949             if (eeprom->group0_schmitt == IS_SCHMITT)
02950                 output[0x0c] |= IS_SCHMITT;
02951             if (eeprom->group0_slew == SLOW_SLEW)
02952                 output[0x0c] |= SLOW_SLEW;
02953 
02954             if (eeprom->group1_drive > DRIVE_16MA)
02955                 output[0x0d] |= DRIVE_16MA;
02956             else
02957                 output[0x0d] |= eeprom->group1_drive;
02958             if (eeprom->group1_schmitt == IS_SCHMITT)
02959                 output[0x0d] |= IS_SCHMITT;
02960             if (eeprom->group1_slew == SLOW_SLEW)
02961                 output[0x0d] |= SLOW_SLEW;
02962 
02963             set_ft232h_cbus(eeprom, output);
02964 
02965             output[0x1e] = eeprom->chip;
02966             fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
02967             break;
02968               
02969     }
02970 
02971     // calculate checksum
02972     checksum = 0xAAAA;
02973 
02974     for (i = 0; i < eeprom->size/2-1; i++)
02975     {
02976         value = output[i*2];
02977         value += output[(i*2)+1] << 8;
02978 
02979         checksum = value^checksum;
02980         checksum = (checksum << 1) | (checksum >> 15);
02981     }
02982 
02983     output[eeprom->size-2] = checksum;
02984     output[eeprom->size-1] = checksum >> 8;
02985 
02986     return user_area_size;
02987 }
02988 /* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted 
02989  * EEPROM structure
02990  *
02991  * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
02992  */
02993 static unsigned char bit2type(unsigned char bits)
02994 {
02995     switch (bits)
02996     {
02997     case   0: return CHANNEL_IS_UART;
02998     case   1: return CHANNEL_IS_FIFO;
02999     case   2: return CHANNEL_IS_OPTO;
03000     case   4: return CHANNEL_IS_CPU;
03001     case   8: return CHANNEL_IS_FT1284;
03002     default:
03003         fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
03004                 bits);
03005     }
03006     return 0;
03007 }
03020 int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
03021 {
03022     unsigned char i, j;
03023     unsigned short checksum, eeprom_checksum, value;
03024     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
03025     int eeprom_size;
03026     struct ftdi_eeprom *eeprom;
03027     unsigned char *buf = ftdi->eeprom->buf;
03028     int release;
03029 
03030     if (ftdi == NULL)
03031         ftdi_error_return(-1,"No context");
03032     if (ftdi->eeprom == NULL)
03033         ftdi_error_return(-1,"No eeprom structure");
03034 
03035     eeprom = ftdi->eeprom;
03036     eeprom_size = eeprom->size;
03037 
03038     // Addr 02: Vendor ID
03039     eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
03040 
03041     // Addr 04: Product ID
03042     eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
03043 
03044     release = buf[0x06] + (buf[0x07]<<8);
03045 
03046     // Addr 08: Config descriptor
03047     // Bit 7: always 1
03048     // Bit 6: 1 if this device is self powered, 0 if bus powered
03049     // Bit 5: 1 if this device uses remote wakeup
03050     eeprom->self_powered = buf[0x08] & 0x40;
03051     eeprom->remote_wakeup = buf[0x08] & 0x20;
03052 
03053     // Addr 09: Max power consumption: max power = value * 2 mA
03054     eeprom->max_power = MAX_POWER_MILLIAMP_PER_UNIT * buf[0x09];
03055 
03056     // Addr 0A: Chip configuration
03057     // Bit 7: 0 - reserved
03058     // Bit 6: 0 - reserved
03059     // Bit 5: 0 - reserved
03060     // Bit 4: 1 - Change USB version on BM and 2232C
03061     // Bit 3: 1 - Use the serial number string
03062     // Bit 2: 1 - Enable suspend pull downs for lower power
03063     // Bit 1: 1 - Out EndPoint is Isochronous
03064     // Bit 0: 1 - In EndPoint is Isochronous
03065     //
03066     eeprom->in_is_isochronous  = buf[0x0A]&0x01;
03067     eeprom->out_is_isochronous = buf[0x0A]&0x02;
03068     eeprom->suspend_pull_downs = buf[0x0A]&0x04;
03069     eeprom->use_serial         = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
03070     eeprom->use_usb_version    = buf[0x0A] & USE_USB_VERSION_BIT;
03071 
03072     // Addr 0C: USB version low byte when 0x0A
03073     // Addr 0D: USB version high byte when 0x0A
03074     eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
03075 
03076     // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
03077     // Addr 0F: Length of manufacturer string
03078     manufacturer_size = buf[0x0F]/2;
03079     if (eeprom->manufacturer)
03080         free(eeprom->manufacturer);
03081     if (manufacturer_size > 0)
03082     {
03083         eeprom->manufacturer = malloc(manufacturer_size);
03084         if (eeprom->manufacturer)
03085         {
03086             // Decode manufacturer
03087             i = buf[0x0E] & (eeprom_size -1); // offset
03088             for (j=0;j<manufacturer_size-1;j++)
03089             {
03090                 eeprom->manufacturer[j] = buf[2*j+i+2];
03091             }
03092             eeprom->manufacturer[j] = '\0';
03093         }
03094     }
03095     else eeprom->manufacturer = NULL;
03096 
03097     // Addr 10: Offset of the product string + 0x80, calculated later
03098     // Addr 11: Length of product string
03099     if (eeprom->product)
03100         free(eeprom->product);
03101     product_size = buf[0x11]/2;
03102     if (product_size > 0)
03103     {
03104         eeprom->product = malloc(product_size);
03105         if (eeprom->product)
03106         {
03107             // Decode product name
03108             i = buf[0x10] & (eeprom_size -1); // offset
03109             for (j=0;j<product_size-1;j++)
03110             {
03111                 eeprom->product[j] = buf[2*j+i+2];
03112             }
03113             eeprom->product[j] = '\0';
03114         }
03115     }
03116     else eeprom->product = NULL;
03117 
03118     // Addr 12: Offset of the serial string + 0x80, calculated later
03119     // Addr 13: Length of serial string
03120     if (eeprom->serial)
03121         free(eeprom->serial);
03122     serial_size = buf[0x13]/2;
03123     if (serial_size > 0)
03124     {
03125         eeprom->serial = malloc(serial_size);
03126         if (eeprom->serial)
03127         {
03128             // Decode serial
03129             i = buf[0x12] & (eeprom_size -1); // offset
03130             for (j=0;j<serial_size-1;j++)
03131             {
03132                 eeprom->serial[j] = buf[2*j+i+2];
03133             }
03134             eeprom->serial[j] = '\0';
03135         }
03136     }
03137     else eeprom->serial = NULL;
03138 
03139     // verify checksum
03140     checksum = 0xAAAA;
03141 
03142     for (i = 0; i < eeprom_size/2-1; i++)
03143     {
03144         value = buf[i*2];
03145         value += buf[(i*2)+1] << 8;
03146 
03147         checksum = value^checksum;
03148         checksum = (checksum << 1) | (checksum >> 15);
03149     }
03150 
03151     eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
03152 
03153     if (eeprom_checksum != checksum)
03154     {
03155         fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
03156         ftdi_error_return(-1,"EEPROM checksum error");
03157     }
03158 
03159     eeprom->channel_a_type   = 0;
03160     if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
03161     {
03162         eeprom->chip = -1;
03163     }
03164     else if (ftdi->type == TYPE_2232C)
03165     {
03166         eeprom->channel_a_type   = bit2type(buf[0x00] & 0x7);
03167         eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
03168         eeprom->high_current_a   = buf[0x00] & HIGH_CURRENT_DRIVE;
03169         eeprom->channel_b_type   = buf[0x01] & 0x7;
03170         eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
03171         eeprom->high_current_b   = buf[0x01] & HIGH_CURRENT_DRIVE;
03172         eeprom->chip = buf[0x14];
03173     }
03174     else if (ftdi->type == TYPE_R)
03175     {
03176         /* TYPE_R flags D2XX, not VCP as all others*/
03177         eeprom->channel_a_driver = ~buf[0x00] & DRIVER_VCP;
03178         eeprom->high_current     = buf[0x00] & HIGH_CURRENT_DRIVE_R;
03179         if ( (buf[0x01]&0x40) != 0x40)
03180             fprintf(stderr,
03181                     "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
03182                     " If this happened with the\n"
03183                     " EEPROM programmed by FTDI tools, please report "
03184                     "to libftdi@developer.intra2net.com\n");
03185 
03186         eeprom->chip = buf[0x16];
03187         // Addr 0B: Invert data lines
03188         // Works only on FT232R, not FT245R, but no way to distinguish
03189         eeprom->invert = buf[0x0B];
03190         // Addr 14: CBUS function: CBUS0, CBUS1
03191         // Addr 15: CBUS function: CBUS2, CBUS3
03192         // Addr 16: CBUS function: CBUS5
03193         eeprom->cbus_function[0] = buf[0x14] & 0x0f;
03194         eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
03195         eeprom->cbus_function[2] = buf[0x15] & 0x0f;
03196         eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
03197         eeprom->cbus_function[4] = buf[0x16] & 0x0f;
03198     }
03199     else if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
03200     {
03201         eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
03202         eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
03203 
03204         if (ftdi->type == TYPE_2232H)
03205         {
03206             eeprom->channel_a_type   = bit2type(buf[0x00] & 0x7);
03207             eeprom->channel_b_type   = bit2type(buf[0x01] & 0x7);
03208             eeprom->suspend_dbus7    = buf[0x01] & SUSPEND_DBUS7_BIT;
03209         }
03210         else
03211         {
03212             eeprom->channel_c_driver = (buf[0x00] >> 4) & DRIVER_VCP;
03213             eeprom->channel_d_driver = (buf[0x01] >> 4) & DRIVER_VCP;
03214             eeprom->channel_a_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 0);
03215             eeprom->channel_b_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 1);
03216             eeprom->channel_c_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 2);
03217             eeprom->channel_d_rs485enable = buf[0x0b] & (CHANNEL_IS_RS485 << 3);
03218         }
03219 
03220         eeprom->chip = buf[0x18];
03221         eeprom->group0_drive   =  buf[0x0c]       & DRIVE_16MA;
03222         eeprom->group0_schmitt =  buf[0x0c]       & IS_SCHMITT;
03223         eeprom->group0_slew    =  buf[0x0c]       & SLOW_SLEW;
03224         eeprom->group1_drive   = (buf[0x0c] >> 4) & 0x3;
03225         eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
03226         eeprom->group1_slew    = (buf[0x0c] >> 4) & SLOW_SLEW;
03227         eeprom->group2_drive   =  buf[0x0d]       & DRIVE_16MA;
03228         eeprom->group2_schmitt =  buf[0x0d]       & IS_SCHMITT;
03229         eeprom->group2_slew    =  buf[0x0d]       & SLOW_SLEW;
03230         eeprom->group3_drive   = (buf[0x0d] >> 4) & DRIVE_16MA;
03231         eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
03232         eeprom->group3_slew    = (buf[0x0d] >> 4) & SLOW_SLEW;
03233     }
03234     else if (ftdi->type == TYPE_232H)
03235     {
03236         int i;
03237 
03238         eeprom->channel_a_type   = buf[0x00] & 0xf;
03239         eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
03240         eeprom->clock_polarity =  buf[0x01]       & FT1284_CLK_IDLE_STATE;
03241         eeprom->data_order     =  buf[0x01]       & FT1284_DATA_LSB;
03242         eeprom->flow_control   =  buf[0x01]       & FT1284_FLOW_CONTROL;
03243         eeprom->powersave      =  buf[0x01]       & POWER_SAVE_DISABLE_H;
03244         eeprom->group0_drive   =  buf[0x0c]       & DRIVE_16MA;
03245         eeprom->group0_schmitt =  buf[0x0c]       & IS_SCHMITT;
03246         eeprom->group0_slew    =  buf[0x0c]       & SLOW_SLEW;
03247         eeprom->group1_drive   =  buf[0x0d]       & DRIVE_16MA;
03248         eeprom->group1_schmitt =  buf[0x0d]       & IS_SCHMITT;
03249         eeprom->group1_slew    =  buf[0x0d]       & SLOW_SLEW;
03250 
03251         for(i=0; i<5; i++)
03252         {
03253             eeprom->cbus_function[2*i  ] =  buf[0x18+i] & 0x0f;
03254             eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
03255         }
03256         eeprom->chip = buf[0x1e];
03257         /*FIXME: Decipher more values*/
03258     }
03259 
03260     if (verbose)
03261     {
03262         char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
03263         fprintf(stdout, "VID:     0x%04x\n",eeprom->vendor_id);
03264         fprintf(stdout, "PID:     0x%04x\n",eeprom->product_id);
03265         fprintf(stdout, "Release: 0x%04x\n",release);
03266 
03267         if (eeprom->self_powered)
03268             fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
03269         else
03270             fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power,
03271                     (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
03272         if (eeprom->manufacturer)
03273             fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
03274         if (eeprom->product)
03275             fprintf(stdout, "Product:      %s\n",eeprom->product);
03276         if (eeprom->serial)
03277             fprintf(stdout, "Serial:       %s\n",eeprom->serial);
03278         fprintf(stdout,     "Checksum      : %04x\n", checksum);
03279         if (ftdi->type == TYPE_R)
03280             fprintf(stdout,     "Internal EEPROM\n");
03281         else if (eeprom->chip >= 0x46)
03282             fprintf(stdout,     "Attached EEPROM: 93x%02x\n", eeprom->chip);
03283         if (eeprom->suspend_dbus7)
03284             fprintf(stdout, "Suspend on DBUS7\n");
03285         if (eeprom->suspend_pull_downs)
03286             fprintf(stdout, "Pull IO pins low during suspend\n");
03287         if(eeprom->powersave)
03288         {
03289             if(ftdi->type >= TYPE_232H)
03290                 fprintf(stdout,"Enter low power state on ACBUS7\n");
03291         } 
03292         if (eeprom->remote_wakeup)
03293             fprintf(stdout, "Enable Remote Wake Up\n");
03294         fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
03295         if (ftdi->type >= TYPE_2232C)
03296             fprintf(stdout,"Channel A has Mode %s%s%s\n",
03297                     channel_mode[eeprom->channel_a_type],
03298                     (eeprom->channel_a_driver)?" VCP":"",
03299                     (eeprom->high_current_a)?" High Current IO":"");
03300         if (ftdi->type >= TYPE_232H)
03301         {
03302             fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
03303                     (eeprom->clock_polarity)?"HIGH":"LOW",
03304                     (eeprom->data_order)?"LSB":"MSB",
03305                     (eeprom->flow_control)?"":"No ");
03306         }        
03307         if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
03308             fprintf(stdout,"Channel B has Mode %s%s%s\n",
03309                     channel_mode[eeprom->channel_b_type],
03310                     (eeprom->channel_b_driver)?" VCP":"",
03311                     (eeprom->high_current_b)?" High Current IO":"");
03312         if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
03313                 eeprom->use_usb_version == USE_USB_VERSION_BIT)
03314             fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
03315 
03316         if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
03317         {
03318             fprintf(stdout,"%s has %d mA drive%s%s\n",
03319                     (ftdi->type == TYPE_2232H)?"AL":"A",
03320                     (eeprom->group0_drive+1) *4,
03321                     (eeprom->group0_schmitt)?" Schmitt Input":"",
03322                     (eeprom->group0_slew)?" Slow Slew":"");
03323             fprintf(stdout,"%s has %d mA drive%s%s\n",
03324                     (ftdi->type == TYPE_2232H)?"AH":"B",
03325                     (eeprom->group1_drive+1) *4,
03326                     (eeprom->group1_schmitt)?" Schmitt Input":"",
03327                     (eeprom->group1_slew)?" Slow Slew":"");
03328             fprintf(stdout,"%s has %d mA drive%s%s\n",
03329                     (ftdi->type == TYPE_2232H)?"BL":"C",
03330                     (eeprom->group2_drive+1) *4,
03331                     (eeprom->group2_schmitt)?" Schmitt Input":"",
03332                     (eeprom->group2_slew)?" Slow Slew":"");
03333             fprintf(stdout,"%s has %d mA drive%s%s\n",
03334                     (ftdi->type == TYPE_2232H)?"BH":"D",
03335                     (eeprom->group3_drive+1) *4,
03336                     (eeprom->group3_schmitt)?" Schmitt Input":"",
03337                     (eeprom->group3_slew)?" Slow Slew":"");
03338         }
03339         else if (ftdi->type == TYPE_232H)
03340         {
03341             int i;
03342             char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
03343                                 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
03344                                 "CLK30","CLK15","CLK7_5"
03345                                };
03346             fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
03347                     (eeprom->group0_drive+1) *4,
03348                     (eeprom->group0_schmitt)?" Schmitt Input":"",
03349                     (eeprom->group0_slew)?" Slow Slew":"");
03350             fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
03351                     (eeprom->group1_drive+1) *4,
03352                     (eeprom->group1_schmitt)?" Schmitt Input":"",
03353                     (eeprom->group1_slew)?" Slow Slew":"");
03354             for (i=0; i<10; i++)
03355             {
03356                 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
03357                     fprintf(stdout,"C%d Function: %s\n", i,
03358                             cbush_mux[eeprom->cbus_function[i]]);
03359             }
03360         }
03361 
03362         if (ftdi->type == TYPE_R)
03363         {
03364             char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
03365                                 "SLEEP","CLK48","CLK24","CLK12","CLK6",
03366                                 "IOMODE","BB_WR","BB_RD"
03367                                };
03368             char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
03369 
03370             if (eeprom->invert)
03371             {
03372                 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
03373                 fprintf(stdout,"Inverted bits:");
03374                 for (i=0; i<8; i++)
03375                     if ((eeprom->invert & (1<<i)) == (1<<i))
03376                         fprintf(stdout," %s",r_bits[i]);
03377                 fprintf(stdout,"\n");
03378             }
03379             for (i=0; i<5; i++)
03380             {
03381                 if (eeprom->cbus_function[i]<CBUS_BB)
03382                     fprintf(stdout,"C%d Function: %s\n", i,
03383                             cbus_mux[eeprom->cbus_function[i]]);
03384                 else
03385                 {
03386                     if (i < 4)
03387                         /* Running MPROG show that C0..3 have fixed function Synchronous
03388                            Bit Bang mode */
03389                         fprintf(stdout,"C%d BB Function: %s\n", i,
03390                                 cbus_BB[i]);
03391                     else
03392                         fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
03393                 }
03394             }
03395         }
03396     }
03397     return 0;
03398 }
03399 
03410 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
03411 {
03412     switch (value_name)
03413     {
03414         case VENDOR_ID:
03415             *value = ftdi->eeprom->vendor_id;
03416             break;
03417         case PRODUCT_ID:
03418             *value = ftdi->eeprom->product_id;
03419             break;
03420         case SELF_POWERED:
03421             *value = ftdi->eeprom->self_powered;
03422             break;
03423         case REMOTE_WAKEUP:
03424             *value = ftdi->eeprom->remote_wakeup;
03425             break;
03426         case IS_NOT_PNP:
03427             *value = ftdi->eeprom->is_not_pnp;
03428             break;
03429         case SUSPEND_DBUS7:
03430             *value = ftdi->eeprom->suspend_dbus7;
03431             break;
03432         case IN_IS_ISOCHRONOUS:
03433             *value = ftdi->eeprom->in_is_isochronous;
03434             break;
03435         case OUT_IS_ISOCHRONOUS:
03436             *value = ftdi->eeprom->out_is_isochronous;
03437             break;
03438         case SUSPEND_PULL_DOWNS:
03439             *value = ftdi->eeprom->suspend_pull_downs;
03440             break;
03441         case USE_SERIAL:
03442             *value = ftdi->eeprom->use_serial;
03443             break;
03444         case USB_VERSION:
03445             *value = ftdi->eeprom->usb_version;
03446             break;
03447         case USE_USB_VERSION:
03448             *value = ftdi->eeprom->use_usb_version;
03449             break;
03450         case MAX_POWER:
03451             *value = ftdi->eeprom->max_power;
03452             break;
03453         case CHANNEL_A_TYPE:
03454             *value = ftdi->eeprom->channel_a_type;
03455             break;
03456         case CHANNEL_B_TYPE:
03457             *value = ftdi->eeprom->channel_b_type;
03458             break;
03459         case CHANNEL_A_DRIVER:
03460             *value = ftdi->eeprom->channel_a_driver;
03461             break;
03462         case CHANNEL_B_DRIVER:
03463             *value = ftdi->eeprom->channel_b_driver;
03464             break;
03465         case CHANNEL_C_DRIVER:
03466             *value = ftdi->eeprom->channel_c_driver;
03467             break;
03468         case CHANNEL_D_DRIVER:
03469             *value = ftdi->eeprom->channel_d_driver;
03470             break;
03471         case CHANNEL_A_RS485:
03472             *value = ftdi->eeprom->channel_a_rs485enable;
03473             break;
03474         case CHANNEL_B_RS485:
03475             *value = ftdi->eeprom->channel_b_rs485enable;
03476             break;
03477         case CHANNEL_C_RS485:
03478             *value = ftdi->eeprom->channel_c_rs485enable;
03479             break;
03480         case CHANNEL_D_RS485:
03481             *value = ftdi->eeprom->channel_d_rs485enable;
03482             break;
03483         case CBUS_FUNCTION_0:
03484             *value = ftdi->eeprom->cbus_function[0];
03485             break;
03486         case CBUS_FUNCTION_1:
03487             *value = ftdi->eeprom->cbus_function[1];
03488             break;
03489         case CBUS_FUNCTION_2:
03490             *value = ftdi->eeprom->cbus_function[2];
03491             break;
03492         case CBUS_FUNCTION_3:
03493             *value = ftdi->eeprom->cbus_function[3];
03494             break;
03495         case CBUS_FUNCTION_4:
03496             *value = ftdi->eeprom->cbus_function[4];
03497             break;
03498         case CBUS_FUNCTION_5:
03499             *value = ftdi->eeprom->cbus_function[5];
03500             break;
03501         case CBUS_FUNCTION_6:
03502             *value = ftdi->eeprom->cbus_function[6];
03503             break;
03504         case CBUS_FUNCTION_7:
03505             *value = ftdi->eeprom->cbus_function[7];
03506             break;
03507         case CBUS_FUNCTION_8:
03508             *value = ftdi->eeprom->cbus_function[8];
03509             break;
03510         case CBUS_FUNCTION_9:
03511             *value = ftdi->eeprom->cbus_function[8];
03512             break;
03513         case HIGH_CURRENT:
03514             *value = ftdi->eeprom->high_current;
03515             break;
03516         case HIGH_CURRENT_A:
03517             *value = ftdi->eeprom->high_current_a;
03518             break;
03519         case HIGH_CURRENT_B:
03520             *value = ftdi->eeprom->high_current_b;
03521             break;
03522         case INVERT:
03523             *value = ftdi->eeprom->invert;
03524             break;
03525         case GROUP0_DRIVE:
03526             *value = ftdi->eeprom->group0_drive;
03527             break;
03528         case GROUP0_SCHMITT:
03529             *value = ftdi->eeprom->group0_schmitt;
03530             break;
03531         case GROUP0_SLEW:
03532             *value = ftdi->eeprom->group0_slew;
03533             break;
03534         case GROUP1_DRIVE:
03535             *value = ftdi->eeprom->group1_drive;
03536             break;
03537         case GROUP1_SCHMITT:
03538             *value = ftdi->eeprom->group1_schmitt;
03539             break;
03540         case GROUP1_SLEW:
03541             *value = ftdi->eeprom->group1_slew;
03542             break;
03543         case GROUP2_DRIVE:
03544             *value = ftdi->eeprom->group2_drive;
03545             break;
03546         case GROUP2_SCHMITT:
03547             *value = ftdi->eeprom->group2_schmitt;
03548             break;
03549         case GROUP2_SLEW:
03550             *value = ftdi->eeprom->group2_slew;
03551             break;
03552         case GROUP3_DRIVE:
03553             *value = ftdi->eeprom->group3_drive;
03554             break;
03555         case GROUP3_SCHMITT:
03556             *value = ftdi->eeprom->group3_schmitt;
03557             break;
03558         case GROUP3_SLEW:
03559             *value = ftdi->eeprom->group3_slew;
03560             break;
03561          case POWER_SAVE:
03562             *value = ftdi->eeprom->powersave;
03563             break;
03564           case CLOCK_POLARITY:
03565             *value = ftdi->eeprom->clock_polarity;
03566             break;
03567          case DATA_ORDER:
03568             *value = ftdi->eeprom->data_order;
03569             break;
03570          case FLOW_CONTROL:
03571             *value = ftdi->eeprom->flow_control;
03572             break;
03573        case CHIP_TYPE:
03574             *value = ftdi->eeprom->chip;
03575             break;
03576         case CHIP_SIZE:
03577             *value = ftdi->eeprom->size;
03578             break;
03579         default:
03580             ftdi_error_return(-1, "Request for unknown EEPROM value");
03581     }
03582     return 0;
03583 }
03584 
03597 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
03598 {
03599     switch (value_name)
03600     {
03601         case VENDOR_ID:
03602             ftdi->eeprom->vendor_id = value;
03603             break;
03604         case PRODUCT_ID:
03605             ftdi->eeprom->product_id = value;
03606             break;
03607         case SELF_POWERED:
03608             ftdi->eeprom->self_powered = value;
03609             break;
03610         case REMOTE_WAKEUP:
03611             ftdi->eeprom->remote_wakeup = value;
03612             break;
03613         case IS_NOT_PNP:
03614             ftdi->eeprom->is_not_pnp = value;
03615             break;
03616         case SUSPEND_DBUS7:
03617             ftdi->eeprom->suspend_dbus7 = value;
03618             break;
03619         case IN_IS_ISOCHRONOUS:
03620             ftdi->eeprom->in_is_isochronous = value;
03621             break;
03622         case OUT_IS_ISOCHRONOUS:
03623             ftdi->eeprom->out_is_isochronous = value;
03624             break;
03625         case SUSPEND_PULL_DOWNS:
03626             ftdi->eeprom->suspend_pull_downs = value;
03627             break;
03628         case USE_SERIAL:
03629             ftdi->eeprom->use_serial = value;
03630             break;
03631         case USB_VERSION:
03632             ftdi->eeprom->usb_version = value;
03633             break;
03634         case USE_USB_VERSION:
03635             ftdi->eeprom->use_usb_version = value;
03636             break;
03637         case MAX_POWER:
03638             ftdi->eeprom->max_power = value;
03639             break;
03640         case CHANNEL_A_TYPE:
03641             ftdi->eeprom->channel_a_type = value;
03642             break;
03643         case CHANNEL_B_TYPE:
03644             ftdi->eeprom->channel_b_type = value;
03645             break;
03646         case CHANNEL_A_DRIVER:
03647             ftdi->eeprom->channel_a_driver = value;
03648             break;
03649         case CHANNEL_B_DRIVER:
03650             ftdi->eeprom->channel_b_driver = value;
03651             break;
03652         case CHANNEL_C_DRIVER:
03653             ftdi->eeprom->channel_c_driver = value;
03654             break;
03655         case CHANNEL_D_DRIVER:
03656             ftdi->eeprom->channel_d_driver = value;
03657             break;
03658         case CHANNEL_A_RS485:
03659             ftdi->eeprom->channel_a_rs485enable = value;
03660             break;
03661         case CHANNEL_B_RS485:
03662             ftdi->eeprom->channel_b_rs485enable = value;
03663             break;
03664         case CHANNEL_C_RS485:
03665             ftdi->eeprom->channel_c_rs485enable = value;
03666             break;
03667         case CHANNEL_D_RS485:
03668             ftdi->eeprom->channel_d_rs485enable = value;
03669             break;
03670         case CBUS_FUNCTION_0:
03671             ftdi->eeprom->cbus_function[0] = value;
03672             break;
03673         case CBUS_FUNCTION_1:
03674             ftdi->eeprom->cbus_function[1] = value;
03675             break;
03676         case CBUS_FUNCTION_2:
03677             ftdi->eeprom->cbus_function[2] = value;
03678             break;
03679         case CBUS_FUNCTION_3:
03680             ftdi->eeprom->cbus_function[3] = value;
03681             break;
03682         case CBUS_FUNCTION_4:
03683             ftdi->eeprom->cbus_function[4] = value;
03684             break;
03685         case CBUS_FUNCTION_5:
03686             ftdi->eeprom->cbus_function[5] = value;
03687             break;
03688         case CBUS_FUNCTION_6:
03689             ftdi->eeprom->cbus_function[6] = value;
03690             break;
03691         case CBUS_FUNCTION_7:
03692             ftdi->eeprom->cbus_function[7] = value;
03693             break;
03694         case CBUS_FUNCTION_8:
03695             ftdi->eeprom->cbus_function[8] = value;
03696             break;
03697         case CBUS_FUNCTION_9:
03698             ftdi->eeprom->cbus_function[9] = value;
03699             break;
03700         case HIGH_CURRENT:
03701             ftdi->eeprom->high_current = value;
03702             break;
03703         case HIGH_CURRENT_A:
03704             ftdi->eeprom->high_current_a = value;
03705             break;
03706         case HIGH_CURRENT_B:
03707             ftdi->eeprom->high_current_b = value;
03708             break;
03709         case INVERT:
03710             ftdi->eeprom->invert = value;
03711             break;
03712         case GROUP0_DRIVE:
03713             ftdi->eeprom->group0_drive = value;
03714             break;
03715         case GROUP0_SCHMITT:
03716             ftdi->eeprom->group0_schmitt = value;
03717             break;
03718         case GROUP0_SLEW:
03719             ftdi->eeprom->group0_slew = value;
03720             break;
03721         case GROUP1_DRIVE:
03722             ftdi->eeprom->group1_drive = value;
03723             break;
03724         case GROUP1_SCHMITT:
03725             ftdi->eeprom->group1_schmitt = value;
03726             break;
03727         case GROUP1_SLEW:
03728             ftdi->eeprom->group1_slew = value;
03729             break;
03730         case GROUP2_DRIVE:
03731             ftdi->eeprom->group2_drive = value;
03732             break;
03733         case GROUP2_SCHMITT:
03734             ftdi->eeprom->group2_schmitt = value;
03735             break;
03736         case GROUP2_SLEW:
03737             ftdi->eeprom->group2_slew = value;
03738             break;
03739         case GROUP3_DRIVE:
03740             ftdi->eeprom->group3_drive = value;
03741             break;
03742         case GROUP3_SCHMITT:
03743             ftdi->eeprom->group3_schmitt = value;
03744             break;
03745         case GROUP3_SLEW:
03746             ftdi->eeprom->group3_slew = value;
03747             break;
03748         case CHIP_TYPE:
03749             ftdi->eeprom->chip = value;
03750             break;
03751          case POWER_SAVE:
03752             ftdi->eeprom->powersave = value;
03753             break;
03754          case CLOCK_POLARITY:
03755             ftdi->eeprom->clock_polarity = value;
03756             break;
03757          case DATA_ORDER:
03758             ftdi->eeprom->data_order = value;
03759             break;
03760          case FLOW_CONTROL:
03761             ftdi->eeprom->flow_control = value;
03762             break;
03763         case CHIP_SIZE:
03764             ftdi_error_return(-2, "EEPROM Value can't be changed");
03765         default :
03766             ftdi_error_return(-1, "Request to unknown EEPROM value");
03767     }
03768     return 0;
03769 }
03770 
03781 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
03782 {
03783     if (!ftdi || !(ftdi->eeprom))
03784         ftdi_error_return(-1, "No appropriate structure");
03785 
03786     if (!buf || size < ftdi->eeprom->size)
03787         ftdi_error_return(-1, "Not enough room to store eeprom");
03788 
03789     // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
03790     if (size > FTDI_MAX_EEPROM_SIZE)
03791         size = FTDI_MAX_EEPROM_SIZE;
03792 
03793     memcpy(buf, ftdi->eeprom->buf, size);
03794 
03795     return 0;
03796 }
03797 
03807 int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
03808 {
03809     if (!ftdi || !(ftdi->eeprom) || !buf)
03810         ftdi_error_return(-1, "No appropriate structure");
03811 
03812     // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
03813     if (size > FTDI_MAX_EEPROM_SIZE)
03814         size = FTDI_MAX_EEPROM_SIZE;
03815 
03816     memcpy(ftdi->eeprom->buf, buf, size);
03817 
03818     return 0;
03819 }
03820 
03832 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
03833 {
03834     if (ftdi == NULL || ftdi->usb_dev == NULL)
03835         ftdi_error_return(-2, "USB device unavailable");
03836 
03837     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
03838         ftdi_error_return(-1, "reading eeprom failed");
03839 
03840     return 0;
03841 }
03842 
03852 int ftdi_read_eeprom(struct ftdi_context *ftdi)
03853 {
03854     int i;
03855     unsigned char *buf;
03856 
03857     if (ftdi == NULL || ftdi->usb_dev == NULL)
03858         ftdi_error_return(-2, "USB device unavailable");
03859     buf = ftdi->eeprom->buf;
03860 
03861     for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
03862     {
03863         if (libusb_control_transfer(
03864                     ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
03865                     buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
03866             ftdi_error_return(-1, "reading eeprom failed");
03867     }
03868 
03869     if (ftdi->type == TYPE_R)
03870         ftdi->eeprom->size = 0x80;
03871     /*    Guesses size of eeprom by comparing halves
03872           - will not work with blank eeprom */
03873     else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
03874         ftdi->eeprom->size = -1;
03875     else if (memcmp(buf,&buf[0x80],0x80) == 0)
03876         ftdi->eeprom->size = 0x80;
03877     else if (memcmp(buf,&buf[0x40],0x40) == 0)
03878         ftdi->eeprom->size = 0x40;
03879     else
03880         ftdi->eeprom->size = 0x100;
03881     return 0;
03882 }
03883 
03884 /*
03885     ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
03886     Function is only used internally
03887     \internal
03888 */
03889 static unsigned char ftdi_read_chipid_shift(unsigned char value)
03890 {
03891     return ((value & 1) << 1) |
03892            ((value & 2) << 5) |
03893            ((value & 4) >> 2) |
03894            ((value & 8) << 4) |
03895            ((value & 16) >> 1) |
03896            ((value & 32) >> 1) |
03897            ((value & 64) >> 4) |
03898            ((value & 128) >> 2);
03899 }
03900 
03911 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
03912 {
03913     unsigned int a = 0, b = 0;
03914 
03915     if (ftdi == NULL || ftdi->usb_dev == NULL)
03916         ftdi_error_return(-2, "USB device unavailable");
03917 
03918     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)
03919     {
03920         a = a << 8 | a >> 8;
03921         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)
03922         {
03923             b = b << 8 | b >> 8;
03924             a = (a << 16) | (b & 0xFFFF);
03925             a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
03926                 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
03927             *chipid = a ^ 0xa5f0f7d1;
03928             return 0;
03929         }
03930     }
03931 
03932     ftdi_error_return(-1, "read of FTDIChip-ID failed");
03933 }
03934 
03949 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
03950                                unsigned short eeprom_val)
03951 {
03952     int chip_type_location;
03953     unsigned short chip_type;
03954 
03955     if (ftdi == NULL || ftdi->usb_dev == NULL)
03956         ftdi_error_return(-2, "USB device unavailable");
03957 
03958     if (eeprom_addr <0x80)
03959         ftdi_error_return(-2, "Invalid access to checksum protected area  below 0x80");
03960 
03961 
03962     switch (ftdi->type)
03963     {
03964         case TYPE_BM:
03965         case  TYPE_2232C:
03966             chip_type_location = 0x14;
03967             break;
03968         case TYPE_2232H:
03969         case TYPE_4232H:
03970             chip_type_location = 0x18;
03971             break;
03972         case TYPE_232H:
03973             chip_type_location = 0x1e;
03974             break;
03975         default:
03976             ftdi_error_return(-4, "Device can't access unprotected area");
03977     }
03978 
03979     if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
03980         ftdi_error_return(-5, "Reading failed failed");
03981     fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
03982     if ((chip_type & 0xff) != 0x66)
03983     {
03984         ftdi_error_return(-6, "EEPROM is not of 93x66");
03985     }
03986 
03987     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
03988                                 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
03989                                 NULL, 0, ftdi->usb_write_timeout) != 0)
03990         ftdi_error_return(-1, "unable to write eeprom");
03991 
03992     return 0;
03993 }
03994 
04005 int ftdi_write_eeprom(struct ftdi_context *ftdi)
04006 {
04007     unsigned short usb_val, status;
04008     int i, ret;
04009     unsigned char *eeprom;
04010 
04011     if (ftdi == NULL || ftdi->usb_dev == NULL)
04012         ftdi_error_return(-2, "USB device unavailable");
04013 
04014     if(ftdi->eeprom->initialized_for_connected_device == 0)
04015         ftdi_error_return(-3, "EEPROM not initialized for the connected device");
04016 
04017     eeprom = ftdi->eeprom->buf;
04018 
04019     /* These commands were traced while running MProg */
04020     if ((ret = ftdi_usb_reset(ftdi)) != 0)
04021         return ret;
04022     if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
04023         return ret;
04024     if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
04025         return ret;
04026 
04027     for (i = 0; i < ftdi->eeprom->size/2; i++)
04028     {
04029         usb_val = eeprom[i*2];
04030         usb_val += eeprom[(i*2)+1] << 8;
04031         if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
04032                                     SIO_WRITE_EEPROM_REQUEST, usb_val, i,
04033                                     NULL, 0, ftdi->usb_write_timeout) < 0)
04034             ftdi_error_return(-1, "unable to write eeprom");
04035     }
04036 
04037     return 0;
04038 }
04039 
04054 #define MAGIC 0x55aa
04055 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
04056 {
04057     unsigned short eeprom_value;
04058     if (ftdi == NULL || ftdi->usb_dev == NULL)
04059         ftdi_error_return(-2, "USB device unavailable");
04060 
04061     if (ftdi->type == TYPE_R)
04062     {
04063         ftdi->eeprom->chip = 0;
04064         return 0;
04065     }
04066 
04067     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
04068                                 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
04069         ftdi_error_return(-1, "unable to erase eeprom");
04070 
04071 
04072     /* detect chip type by writing 0x55AA as magic at word position 0xc0
04073        Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
04074        Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
04075        Chip is 93x66 if magic is only read at word position 0xc0*/
04076     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
04077                                 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
04078                                 NULL, 0, ftdi->usb_write_timeout) != 0)
04079         ftdi_error_return(-3, "Writing magic failed");
04080     if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
04081         ftdi_error_return(-4, "Reading failed failed");
04082     if (eeprom_value == MAGIC)
04083     {
04084         ftdi->eeprom->chip = 0x46;
04085     }
04086     else
04087     {
04088         if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
04089             ftdi_error_return(-4, "Reading failed failed");
04090         if (eeprom_value == MAGIC)
04091             ftdi->eeprom->chip = 0x56;
04092         else
04093         {
04094             if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
04095                 ftdi_error_return(-4, "Reading failed failed");
04096             if (eeprom_value == MAGIC)
04097                 ftdi->eeprom->chip = 0x66;
04098             else
04099             {
04100                 ftdi->eeprom->chip = -1;
04101             }
04102         }
04103     }
04104     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
04105                                 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
04106         ftdi_error_return(-1, "unable to erase eeprom");
04107     return 0;
04108 }
04109 
04117 char *ftdi_get_error_string (struct ftdi_context *ftdi)
04118 {
04119     if (ftdi == NULL)
04120         return "";
04121 
04122     return ftdi->error_str;
04123 }
04124 
04125 /* @} end of doxygen libftdi group */