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-2010 by Intra2net AG
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 <usb.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <stdio.h>
00035 
00036 #include "ftdi.h"
00037 
00038 /* stuff needed for async write */
00039 #ifdef LIBFTDI_LINUX_ASYNC_MODE
00040 #include <sys/ioctl.h>
00041 #include <sys/time.h>
00042 #include <sys/select.h>
00043 #include <sys/types.h>
00044 #include <unistd.h>
00045 #include <linux/usbdevice_fs.h>
00046 #endif
00047 
00048 #define ftdi_error_return(code, str) do {  \
00049         ftdi->error_str = str;             \
00050         return code;                       \
00051    } while(0);
00052 
00053 
00063 static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
00064 {
00065     int ret = 0;
00066 
00067     if (ftdi && ftdi->usb_dev)
00068     {
00069        ret = usb_close (ftdi->usb_dev);
00070        ftdi->usb_dev = NULL;
00071     }
00072 
00073     return ret;
00074 }
00075 
00086 int ftdi_init(struct ftdi_context *ftdi)
00087 {
00088     unsigned int i;
00089 
00090     ftdi->usb_dev = NULL;
00091     ftdi->usb_read_timeout = 5000;
00092     ftdi->usb_write_timeout = 5000;
00093 
00094     ftdi->type = TYPE_BM;    /* chip type */
00095     ftdi->baudrate = -1;
00096     ftdi->bitbang_enabled = 0;  /* 0: normal mode 1: any of the bitbang modes enabled */
00097 
00098     ftdi->readbuffer = NULL;
00099     ftdi->readbuffer_offset = 0;
00100     ftdi->readbuffer_remaining = 0;
00101     ftdi->writebuffer_chunksize = 4096;
00102     ftdi->max_packet_size = 0;
00103 
00104     ftdi->interface = 0;
00105     ftdi->index = 0;
00106     ftdi->in_ep = 0x02;
00107     ftdi->out_ep = 0x81;
00108     ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode  */
00109 
00110     ftdi->error_str = NULL;
00111 
00112 #ifdef LIBFTDI_LINUX_ASYNC_MODE
00113     ftdi->async_usb_buffer_size=10;
00114     if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
00115         ftdi_error_return(-1, "out of memory for async usb buffer");
00116 
00117     /* initialize async usb buffer with unused-marker */
00118     for (i=0; i < ftdi->async_usb_buffer_size; i++)
00119         ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
00120 #else
00121     ftdi->async_usb_buffer_size=0;
00122     ftdi->async_usb_buffer = NULL;
00123 #endif
00124 
00125     ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
00126 
00127     /* All fine. Now allocate the readbuffer */
00128     return ftdi_read_data_set_chunksize(ftdi, 4096);
00129 }
00130 
00136 struct ftdi_context *ftdi_new(void)
00137 {
00138     struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
00139 
00140     if (ftdi == NULL)
00141     {
00142         return NULL;
00143     }
00144 
00145     if (ftdi_init(ftdi) != 0)
00146     {
00147         free(ftdi);
00148         return NULL;
00149     }
00150 
00151     return ftdi;
00152 }
00153 
00164 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
00165 {
00166     if (ftdi == NULL)
00167         ftdi_error_return(-2, "USB device unavailable");
00168 
00169     switch (interface)
00170     {
00171         case INTERFACE_ANY:
00172         case INTERFACE_A:
00173             /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
00174             break;
00175         case INTERFACE_B:
00176             ftdi->interface = 1;
00177             ftdi->index     = INTERFACE_B;
00178             ftdi->in_ep     = 0x04;
00179             ftdi->out_ep    = 0x83;
00180             break;
00181         case INTERFACE_C:
00182             ftdi->interface = 2;
00183             ftdi->index     = INTERFACE_C;
00184             ftdi->in_ep     = 0x06;
00185             ftdi->out_ep    = 0x85;
00186             break;
00187         case INTERFACE_D:
00188             ftdi->interface = 3;
00189             ftdi->index     = INTERFACE_D;
00190             ftdi->in_ep     = 0x08;
00191             ftdi->out_ep    = 0x87;
00192             break;
00193         default:
00194             ftdi_error_return(-1, "Unknown interface");
00195     }
00196     return 0;
00197 }
00198 
00204 void ftdi_deinit(struct ftdi_context *ftdi)
00205 {
00206     if (ftdi == NULL)
00207         return;
00208 
00209     ftdi_usb_close_internal (ftdi);
00210 
00211     if (ftdi->async_usb_buffer != NULL)
00212     {
00213         free(ftdi->async_usb_buffer);
00214         ftdi->async_usb_buffer = NULL;
00215     }
00216 
00217     if (ftdi->readbuffer != NULL)
00218     {
00219         free(ftdi->readbuffer);
00220         ftdi->readbuffer = NULL;
00221     }
00222 }
00223 
00229 void ftdi_free(struct ftdi_context *ftdi)
00230 {
00231     ftdi_deinit(ftdi);
00232     free(ftdi);
00233 }
00234 
00241 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
00242 {
00243     if (ftdi == NULL)
00244         return;
00245 
00246     ftdi->usb_dev = usb;
00247 }
00248 
00249 
00264 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
00265 {
00266     struct ftdi_device_list **curdev;
00267     struct usb_bus *bus;
00268     struct usb_device *dev;
00269     int count = 0;
00270 
00271     usb_init();
00272     if (usb_find_busses() < 0)
00273         ftdi_error_return(-1, "usb_find_busses() failed");
00274     if (usb_find_devices() < 0)
00275         ftdi_error_return(-2, "usb_find_devices() failed");
00276 
00277     curdev = devlist;
00278     *curdev = NULL;
00279     for (bus = usb_get_busses(); bus; bus = bus->next)
00280     {
00281         for (dev = bus->devices; dev; dev = dev->next)
00282         {
00283             if (dev->descriptor.idVendor == vendor
00284                     && dev->descriptor.idProduct == product)
00285             {
00286                 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
00287                 if (!*curdev)
00288                     ftdi_error_return(-3, "out of memory");
00289 
00290                 (*curdev)->next = NULL;
00291                 (*curdev)->dev = dev;
00292 
00293                 curdev = &(*curdev)->next;
00294                 count++;
00295             }
00296         }
00297     }
00298 
00299     return count;
00300 }
00301 
00307 void ftdi_list_free(struct ftdi_device_list **devlist)
00308 {
00309     struct ftdi_device_list *curdev, *next;
00310 
00311     for (curdev = *devlist; curdev != NULL;)
00312     {
00313         next = curdev->next;
00314         free(curdev);
00315         curdev = next;
00316     }
00317 
00318     *devlist = NULL;
00319 }
00320 
00326 void ftdi_list_free2(struct ftdi_device_list *devlist)
00327 {
00328     ftdi_list_free(&devlist);
00329 }
00330 
00357 int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
00358                          char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
00359 {
00360     if ((ftdi==NULL) || (dev==NULL))
00361         return -1;
00362 
00363     if (!(ftdi->usb_dev = usb_open(dev)))
00364         ftdi_error_return(-4, usb_strerror());
00365 
00366     if (manufacturer != NULL)
00367     {
00368         if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
00369         {
00370             ftdi_usb_close_internal (ftdi);
00371             ftdi_error_return(-7, usb_strerror());
00372         }
00373     }
00374 
00375     if (description != NULL)
00376     {
00377         if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
00378         {
00379             ftdi_usb_close_internal (ftdi);
00380             ftdi_error_return(-8, usb_strerror());
00381         }
00382     }
00383 
00384     if (serial != NULL)
00385     {
00386         if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
00387         {
00388             ftdi_usb_close_internal (ftdi);
00389             ftdi_error_return(-9, usb_strerror());
00390         }
00391     }
00392 
00393     if (ftdi_usb_close_internal (ftdi) != 0)
00394         ftdi_error_return(-10, usb_strerror());
00395 
00396     return 0;
00397 }
00398 
00405 static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, struct usb_device *dev)
00406 {
00407     unsigned int packet_size;
00408 
00409     // Sanity check
00410     if (ftdi == NULL || dev == NULL)
00411         return 64;
00412 
00413     // Determine maximum packet size. Init with default value.
00414     // New hi-speed devices from FTDI use a packet size of 512 bytes
00415     // but could be connected to a normal speed USB hub -> 64 bytes packet size.
00416     if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
00417         packet_size = 512;
00418     else
00419         packet_size = 64;
00420 
00421     if (dev->descriptor.bNumConfigurations > 0 && dev->config)
00422     {
00423         struct usb_config_descriptor config = dev->config[0];
00424 
00425         if (ftdi->interface < config.bNumInterfaces)
00426         {
00427             struct usb_interface interface = config.interface[ftdi->interface];
00428             if (interface.num_altsetting > 0)
00429             {
00430                 struct usb_interface_descriptor descriptor = interface.altsetting[0];
00431                 if (descriptor.bNumEndpoints > 0)
00432                 {
00433                     packet_size = descriptor.endpoint[0].wMaxPacketSize;
00434                 }
00435             }
00436         }
00437     }
00438 
00439     return packet_size;
00440 }
00441 
00456 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
00457 {
00458     int detach_errno = 0;
00459     int config_val = 1;
00460 
00461     if (ftdi == NULL)
00462         ftdi_error_return(-8, "ftdi context invalid");
00463 
00464     if (!(ftdi->usb_dev = usb_open(dev)))
00465         ftdi_error_return(-4, "usb_open() failed");
00466 
00467 #ifdef LIBUSB_HAS_GET_DRIVER_NP
00468     // Try to detach ftdi_sio kernel module.
00469     // Returns ENODATA if driver is not loaded.
00470     //
00471     // The return code is kept in a separate variable and only parsed
00472     // if usb_set_configuration() or usb_claim_interface() fails as the
00473     // detach operation might be denied and everything still works fine.
00474     // Likely scenario is a static ftdi_sio kernel module.
00475     if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
00476         detach_errno = errno;
00477 #endif
00478 
00479 #ifdef __WIN32__
00480     // set configuration (needed especially for windows)
00481     // tolerate EBUSY: one device with one configuration, but two interfaces
00482     //    and libftdi sessions to both interfaces (e.g. FT2232)
00483 
00484     if (dev->descriptor.bNumConfigurations > 0)
00485     {
00486         // libusb-win32 on Windows 64 can return a null pointer for a valid device
00487         if (dev->config)
00488             config_val = dev->config[0].bConfigurationValue;
00489 
00490         if (usb_set_configuration(ftdi->usb_dev, config_val) &&
00491             errno != EBUSY)
00492         {
00493             ftdi_usb_close_internal (ftdi);
00494             if (detach_errno == EPERM)
00495             {
00496                 ftdi_error_return(-8, "inappropriate permissions on device!");
00497             }
00498             else
00499             {
00500                 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
00501             }
00502         }
00503     }
00504 #endif
00505 
00506     if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
00507     {
00508         ftdi_usb_close_internal (ftdi);
00509         if (detach_errno == EPERM)
00510         {
00511             ftdi_error_return(-8, "inappropriate permissions on device!");
00512         }
00513         else
00514         {
00515             ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
00516         }
00517     }
00518 
00519     if (ftdi_usb_reset (ftdi) != 0)
00520     {
00521         ftdi_usb_close_internal (ftdi);
00522         ftdi_error_return(-6, "ftdi_usb_reset failed");
00523     }
00524 
00525     // Try to guess chip type
00526     // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
00527     if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
00528             && dev->descriptor.iSerialNumber == 0))
00529         ftdi->type = TYPE_BM;
00530     else if (dev->descriptor.bcdDevice == 0x200)
00531         ftdi->type = TYPE_AM;
00532     else if (dev->descriptor.bcdDevice == 0x500)
00533         ftdi->type = TYPE_2232C;
00534     else if (dev->descriptor.bcdDevice == 0x600)
00535         ftdi->type = TYPE_R;
00536     else if (dev->descriptor.bcdDevice == 0x700)
00537         ftdi->type = TYPE_2232H;
00538     else if (dev->descriptor.bcdDevice == 0x800)
00539         ftdi->type = TYPE_4232H;
00540 
00541     // Set default interface on dual/quad type chips
00542     switch(ftdi->type)
00543     {
00544         case TYPE_2232C:
00545         case TYPE_2232H:
00546         case TYPE_4232H:
00547             if (!ftdi->index)
00548                 ftdi->index = INTERFACE_A;
00549             break;
00550         default:
00551             break;
00552     }
00553 
00554     // Determine maximum packet size
00555     ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
00556 
00557     if (ftdi_set_baudrate (ftdi, 9600) != 0)
00558     {
00559         ftdi_usb_close_internal (ftdi);
00560         ftdi_error_return(-7, "set baudrate failed");
00561     }
00562 
00563     ftdi_error_return(0, "all fine");
00564 }
00565 
00575 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
00576 {
00577     return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
00578 }
00579 
00602 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
00603                        const char* description, const char* serial)
00604 {
00605     return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
00606 }
00607 
00632 int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
00633                        const char* description, const char* serial, unsigned int index)
00634 {
00635     struct usb_bus *bus;
00636     struct usb_device *dev;
00637     char string[256];
00638 
00639     usb_init();
00640 
00641     if (usb_find_busses() < 0)
00642         ftdi_error_return(-1, "usb_find_busses() failed");
00643     if (usb_find_devices() < 0)
00644         ftdi_error_return(-2, "usb_find_devices() failed");
00645 
00646     if (ftdi == NULL)
00647         ftdi_error_return(-11, "ftdi context invalid");
00648 
00649     for (bus = usb_get_busses(); bus; bus = bus->next)
00650     {
00651         for (dev = bus->devices; dev; dev = dev->next)
00652         {
00653             if (dev->descriptor.idVendor == vendor
00654                     && dev->descriptor.idProduct == product)
00655             {
00656                 if (!(ftdi->usb_dev = usb_open(dev)))
00657                     ftdi_error_return(-4, "usb_open() failed");
00658 
00659                 if (description != NULL)
00660                 {
00661                     if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
00662                     {
00663                         ftdi_usb_close_internal (ftdi);
00664                         ftdi_error_return(-8, "unable to fetch product description");
00665                     }
00666                     if (strncmp(string, description, sizeof(string)) != 0)
00667                     {
00668                         if (ftdi_usb_close_internal (ftdi) != 0)
00669                             ftdi_error_return(-10, "unable to close device");
00670                         continue;
00671                     }
00672                 }
00673                 if (serial != NULL)
00674                 {
00675                     if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
00676                     {
00677                         ftdi_usb_close_internal (ftdi);
00678                         ftdi_error_return(-9, "unable to fetch serial number");
00679                     }
00680                     if (strncmp(string, serial, sizeof(string)) != 0)
00681                     {
00682                         if (ftdi_usb_close_internal (ftdi) != 0)
00683                             ftdi_error_return(-10, "unable to close device");
00684                         continue;
00685                     }
00686                 }
00687 
00688                 if (ftdi_usb_close_internal (ftdi) != 0)
00689                     ftdi_error_return(-10, "unable to close device");
00690 
00691                 if (index > 0)
00692                 {
00693                     index--;
00694                     continue;
00695                 }
00696 
00697                 return ftdi_usb_open_dev(ftdi, dev);
00698             }
00699         }
00700     }
00701 
00702     // device not found
00703     ftdi_error_return(-3, "device not found");
00704 }
00705 
00733 int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
00734 {
00735     if (ftdi == NULL)
00736         ftdi_error_return(-12, "ftdi context invalid");
00737 
00738     if (description[0] == 0 || description[1] != ':')
00739         ftdi_error_return(-11, "illegal description format");
00740 
00741     if (description[0] == 'd')
00742     {
00743         struct usb_bus *bus;
00744         struct usb_device *dev;
00745 
00746         usb_init();
00747 
00748         if (usb_find_busses() < 0)
00749             ftdi_error_return(-1, "usb_find_busses() failed");
00750         if (usb_find_devices() < 0)
00751             ftdi_error_return(-2, "usb_find_devices() failed");
00752 
00753         for (bus = usb_get_busses(); bus; bus = bus->next)
00754         {
00755             for (dev = bus->devices; dev; dev = dev->next)
00756             {
00757                 /* XXX: This doesn't handle symlinks/odd paths/etc... */
00758                 const char *desc = description + 2;
00759                 size_t len = strlen(bus->dirname);
00760                 if (strncmp(desc, bus->dirname, len))
00761                     continue;
00762                 desc += len;
00763                 if (desc[0] != '/')
00764                     continue;
00765                 ++desc;
00766                 if (strcmp(desc, dev->filename))
00767                     continue;
00768                 return ftdi_usb_open_dev(ftdi, dev);
00769             }
00770         }
00771 
00772         // device not found
00773         ftdi_error_return(-3, "device not found");
00774     }
00775     else if (description[0] == 'i' || description[0] == 's')
00776     {
00777         unsigned int vendor;
00778         unsigned int product;
00779         unsigned int index=0;
00780         const char *serial=NULL;
00781         const char *startp, *endp;
00782 
00783         errno=0;
00784         startp=description+2;
00785         vendor=strtoul((char*)startp,(char**)&endp,0);
00786         if (*endp != ':' || endp == startp || errno != 0)
00787             ftdi_error_return(-11, "illegal description format");
00788 
00789         startp=endp+1;
00790         product=strtoul((char*)startp,(char**)&endp,0);
00791         if (endp == startp || errno != 0)
00792             ftdi_error_return(-11, "illegal description format");
00793 
00794         if (description[0] == 'i' && *endp != 0)
00795         {
00796             /* optional index field in i-mode */
00797             if (*endp != ':')
00798                 ftdi_error_return(-11, "illegal description format");
00799 
00800             startp=endp+1;
00801             index=strtoul((char*)startp,(char**)&endp,0);
00802             if (*endp != 0 || endp == startp || errno != 0)
00803                 ftdi_error_return(-11, "illegal description format");
00804         }
00805         if (description[0] == 's')
00806         {
00807             if (*endp != ':')
00808                 ftdi_error_return(-11, "illegal description format");
00809 
00810             /* rest of the description is the serial */
00811             serial=endp+1;
00812         }
00813 
00814         return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
00815     }
00816     else
00817     {
00818         ftdi_error_return(-11, "illegal description format");
00819     }
00820 }
00821 
00831 int ftdi_usb_reset(struct ftdi_context *ftdi)
00832 {
00833     if (ftdi == NULL || ftdi->usb_dev == NULL)
00834         ftdi_error_return(-2, "USB device unavailable");
00835 
00836     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00837                         SIO_RESET_REQUEST, SIO_RESET_SIO,
00838                         ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
00839         ftdi_error_return(-1,"FTDI reset failed");
00840 
00841     // Invalidate data in the readbuffer
00842     ftdi->readbuffer_offset = 0;
00843     ftdi->readbuffer_remaining = 0;
00844 
00845     return 0;
00846 }
00847 
00857 int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
00858 {
00859     if (ftdi == NULL || ftdi->usb_dev == NULL)
00860         ftdi_error_return(-2, "USB device unavailable");
00861 
00862     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00863                         SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
00864                         ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
00865         ftdi_error_return(-1, "FTDI purge of RX buffer failed");
00866 
00867     // Invalidate data in the readbuffer
00868     ftdi->readbuffer_offset = 0;
00869     ftdi->readbuffer_remaining = 0;
00870 
00871     return 0;
00872 }
00873 
00883 int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
00884 {
00885     if (ftdi == NULL || ftdi->usb_dev == NULL)
00886         ftdi_error_return(-2, "USB device unavailable");
00887 
00888     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
00889                         SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
00890                         ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
00891         ftdi_error_return(-1, "FTDI purge of TX buffer failed");
00892 
00893     return 0;
00894 }
00895 
00906 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
00907 {
00908     int result;
00909 
00910     if (ftdi == NULL || ftdi->usb_dev == NULL)
00911         ftdi_error_return(-3, "USB device unavailable");
00912 
00913     result = ftdi_usb_purge_rx_buffer(ftdi);
00914     if (result < 0)
00915         return -1;
00916 
00917     result = ftdi_usb_purge_tx_buffer(ftdi);
00918     if (result < 0)
00919         return -2;
00920 
00921     return 0;
00922 }
00923 
00924 
00925 
00936 int ftdi_usb_close(struct ftdi_context *ftdi)
00937 {
00938     int rtn = 0;
00939 
00940     if (ftdi == NULL)
00941         ftdi_error_return(-3, "ftdi context invalid");
00942 
00943 #ifdef LIBFTDI_LINUX_ASYNC_MODE
00944     /* try to release some kernel resources */
00945     ftdi_async_complete(ftdi,1);
00946 #endif
00947 
00948     if (ftdi->usb_dev != NULL)
00949         if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
00950             rtn = -1;
00951 
00952     if (ftdi_usb_close_internal (ftdi) != 0)
00953         rtn = -2;
00954 
00955     return rtn;
00956 }
00957 
00963 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
00964                                  unsigned short *value, unsigned short *index)
00965 {
00966     static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
00967     static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
00968     static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
00969     int divisor, best_divisor, best_baud, best_baud_diff;
00970     unsigned long encoded_divisor;
00971     int i;
00972 
00973     if (baudrate <= 0)
00974     {
00975         // Return error
00976         return -1;
00977     }
00978 
00979     divisor = 24000000 / baudrate;
00980 
00981     if (ftdi->type == TYPE_AM)
00982     {
00983         // Round down to supported fraction (AM only)
00984         divisor -= am_adjust_dn[divisor & 7];
00985     }
00986 
00987     // Try this divisor and the one above it (because division rounds down)
00988     best_divisor = 0;
00989     best_baud = 0;
00990     best_baud_diff = 0;
00991     for (i = 0; i < 2; i++)
00992     {
00993         int try_divisor = divisor + i;
00994         int baud_estimate;
00995         int baud_diff;
00996 
00997         // Round up to supported divisor value
00998         if (try_divisor <= 8)
00999         {
01000             // Round up to minimum supported divisor
01001             try_divisor = 8;
01002         }
01003         else if (ftdi->type != TYPE_AM && try_divisor < 12)
01004         {
01005             // BM doesn't support divisors 9 through 11 inclusive
01006             try_divisor = 12;
01007         }
01008         else if (divisor < 16)
01009         {
01010             // AM doesn't support divisors 9 through 15 inclusive
01011             try_divisor = 16;
01012         }
01013         else
01014         {
01015             if (ftdi->type == TYPE_AM)
01016             {
01017                 // Round up to supported fraction (AM only)
01018                 try_divisor += am_adjust_up[try_divisor & 7];
01019                 if (try_divisor > 0x1FFF8)
01020                 {
01021                     // Round down to maximum supported divisor value (for AM)
01022                     try_divisor = 0x1FFF8;
01023                 }
01024             }
01025             else
01026             {
01027                 if (try_divisor > 0x1FFFF)
01028                 {
01029                     // Round down to maximum supported divisor value (for BM)
01030                     try_divisor = 0x1FFFF;
01031                 }
01032             }
01033         }
01034         // Get estimated baud rate (to nearest integer)
01035         baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
01036         // Get absolute difference from requested baud rate
01037         if (baud_estimate < baudrate)
01038         {
01039             baud_diff = baudrate - baud_estimate;
01040         }
01041         else
01042         {
01043             baud_diff = baud_estimate - baudrate;
01044         }
01045         if (i == 0 || baud_diff < best_baud_diff)
01046         {
01047             // Closest to requested baud rate so far
01048             best_divisor = try_divisor;
01049             best_baud = baud_estimate;
01050             best_baud_diff = baud_diff;
01051             if (baud_diff == 0)
01052             {
01053                 // Spot on! No point trying
01054                 break;
01055             }
01056         }
01057     }
01058     // Encode the best divisor value
01059     encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
01060     // Deal with special cases for encoded value
01061     if (encoded_divisor == 1)
01062     {
01063         encoded_divisor = 0;    // 3000000 baud
01064     }
01065     else if (encoded_divisor == 0x4001)
01066     {
01067         encoded_divisor = 1;    // 2000000 baud (BM only)
01068     }
01069     // Split into "value" and "index" values
01070     *value = (unsigned short)(encoded_divisor & 0xFFFF);
01071     if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
01072     {
01073         *index = (unsigned short)(encoded_divisor >> 8);
01074         *index &= 0xFF00;
01075         *index |= ftdi->index;
01076     }
01077     else
01078         *index = (unsigned short)(encoded_divisor >> 16);
01079 
01080     // Return the nearest baud rate
01081     return best_baud;
01082 }
01083 
01095 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
01096 {
01097     unsigned short value, index;
01098     int actual_baudrate;
01099 
01100     if (ftdi == NULL || ftdi->usb_dev == NULL)
01101         ftdi_error_return(-3, "USB device unavailable");
01102 
01103     if (ftdi->bitbang_enabled)
01104     {
01105         baudrate = baudrate*4;
01106     }
01107 
01108     actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
01109     if (actual_baudrate <= 0)
01110         ftdi_error_return (-1, "Silly baudrate <= 0.");
01111 
01112     // Check within tolerance (about 5%)
01113     if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
01114             || ((actual_baudrate < baudrate)
01115                 ? (actual_baudrate * 21 < baudrate * 20)
01116                 : (baudrate * 21 < actual_baudrate * 20)))
01117         ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
01118 
01119     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01120                         SIO_SET_BAUDRATE_REQUEST, value,
01121                         index, NULL, 0, ftdi->usb_write_timeout) != 0)
01122         ftdi_error_return (-2, "Setting new baudrate failed");
01123 
01124     ftdi->baudrate = baudrate;
01125     return 0;
01126 }
01127 
01141 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01142                            enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
01143 {
01144     return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
01145 }
01146 
01160 int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
01161                             enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
01162                             enum ftdi_break_type break_type)
01163 {
01164     unsigned short value = bits;
01165 
01166     if (ftdi == NULL || ftdi->usb_dev == NULL)
01167         ftdi_error_return(-2, "USB device unavailable");
01168 
01169     switch (parity)
01170     {
01171         case NONE:
01172             value |= (0x00 << 8);
01173             break;
01174         case ODD:
01175             value |= (0x01 << 8);
01176             break;
01177         case EVEN:
01178             value |= (0x02 << 8);
01179             break;
01180         case MARK:
01181             value |= (0x03 << 8);
01182             break;
01183         case SPACE:
01184             value |= (0x04 << 8);
01185             break;
01186     }
01187 
01188     switch (sbit)
01189     {
01190         case STOP_BIT_1:
01191             value |= (0x00 << 11);
01192             break;
01193         case STOP_BIT_15:
01194             value |= (0x01 << 11);
01195             break;
01196         case STOP_BIT_2:
01197             value |= (0x02 << 11);
01198             break;
01199     }
01200 
01201     switch (break_type)
01202     {
01203         case BREAK_OFF:
01204             value |= (0x00 << 14);
01205             break;
01206         case BREAK_ON:
01207             value |= (0x01 << 14);
01208             break;
01209     }
01210 
01211     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01212                         SIO_SET_DATA_REQUEST, value,
01213                         ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
01214         ftdi_error_return (-1, "Setting new line property failed");
01215 
01216     return 0;
01217 }
01218 
01230 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
01231 {
01232     int ret;
01233     int offset = 0;
01234     int total_written = 0;
01235 
01236     if (ftdi == NULL || ftdi->usb_dev == NULL)
01237         ftdi_error_return(-666, "USB device unavailable");
01238 
01239     while (offset < size)
01240     {
01241         int write_size = ftdi->writebuffer_chunksize;
01242 
01243         if (offset+write_size > size)
01244             write_size = size-offset;
01245 
01246         ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
01247         if (ret < 0)
01248             ftdi_error_return(ret, "usb bulk write failed");
01249 
01250         total_written += ret;
01251         offset += write_size;
01252     }
01253 
01254     return total_written;
01255 }
01256 
01257 #ifdef LIBFTDI_LINUX_ASYNC_MODE
01258 #ifdef USB_CLASS_PTP
01259 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
01260 #endif
01261 /* this is strongly dependent on libusb using the same struct layout. If libusb
01262    changes in some later version this may break horribly (this is for libusb 0.1.12) */
01263 struct usb_dev_handle
01264 {
01265     int fd;
01266     // some other stuff coming here we don't need
01267 };
01268 
01273 static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
01274 {
01275     struct usbdevfs_urb *urb;
01276     int pending=0;
01277     unsigned int i;
01278 
01279     for (i=0; i < ftdi->async_usb_buffer_size; i++)
01280     {
01281         urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
01282         if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
01283             pending++;
01284     }
01285 
01286     return pending;
01287 }
01288 
01299 static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
01300 {
01301     struct timeval tv;
01302     struct usbdevfs_urb *urb=NULL;
01303     int ret;
01304     fd_set writefds;
01305     int keep_going=0;
01306 
01307     FD_ZERO(&writefds);
01308     FD_SET(ftdi->usb_dev->fd, &writefds);
01309 
01310     /* init timeout only once, select writes time left after call */
01311     tv.tv_sec = timeout_msec / 1000;
01312     tv.tv_usec = (timeout_msec % 1000) * 1000;
01313 
01314     do
01315     {
01316         while (_usb_get_async_urbs_pending(ftdi)
01317                 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
01318                 && errno == EAGAIN)
01319         {
01320             if (keep_going && !wait_for_more)
01321             {
01322                 /* don't wait if repeating only for keep_going */
01323                 keep_going=0;
01324                 break;
01325             }
01326 
01327             /* wait for timeout msec or something written ready */
01328             select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
01329         }
01330 
01331         if (ret == 0 && urb != NULL)
01332         {
01333             /* got a free urb, mark it */
01334             urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
01335 
01336             /* try to get more urbs that are ready now, but don't wait anymore */
01337             urb=NULL;
01338             keep_going=1;
01339         }
01340         else
01341         {
01342             /* no more urbs waiting */
01343             keep_going=0;
01344         }
01345     }
01346     while (keep_going);
01347 }
01348 
01356 void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
01357 {
01358     _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
01359 }
01360 
01366 static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
01367 {
01368     struct usbdevfs_urb *urb;
01369     int bytesdone = 0, requested;
01370     int ret, cleanup_count;
01371     unsigned int i;
01372 
01373     do
01374     {
01375         /* find a free urb buffer we can use */
01376         urb=NULL;
01377         for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
01378         {
01379             if (i==ftdi->async_usb_buffer_size)
01380             {
01381                 /* wait until some buffers are free */
01382                 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
01383             }
01384 
01385             for (i=0; i < ftdi->async_usb_buffer_size; i++)
01386             {
01387                 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
01388                 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
01389                     break;  /* found a free urb position */
01390                 urb=NULL;
01391             }
01392         }
01393 
01394         /* no free urb position found */
01395         if (urb==NULL)
01396             return -1;
01397 
01398         requested = size - bytesdone;
01399         if (requested > 4096)
01400             requested = 4096;
01401 
01402         memset(urb,0,sizeof(urb));
01403 
01404         urb->type = USBDEVFS_URB_TYPE_BULK;
01405         urb->endpoint = ep;
01406         urb->flags = 0;
01407         urb->buffer = bytes + bytesdone;
01408         urb->buffer_length = requested;
01409         urb->signr = 0;
01410         urb->actual_length = 0;
01411         urb->number_of_packets = 0;
01412         urb->usercontext = 0;
01413 
01414         do
01415         {
01416             ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
01417         }
01418         while (ret < 0 && errno == EINTR);
01419         if (ret < 0)
01420             return ret;       /* the caller can read errno to get more info */
01421 
01422         bytesdone += requested;
01423     }
01424     while (bytesdone < size);
01425     return bytesdone;
01426 }
01427 
01447 int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
01448 {
01449     int ret;
01450     int offset = 0;
01451     int total_written = 0;
01452 
01453     if (ftdi == NULL || ftdi->usb_dev == NULL)
01454         ftdi_error_return(-666, "USB device unavailable");
01455 
01456     while (offset < size)
01457     {
01458         int write_size = ftdi->writebuffer_chunksize;
01459 
01460         if (offset+write_size > size)
01461             write_size = size-offset;
01462 
01463         ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
01464         if (ret < 0)
01465             ftdi_error_return(ret, "usb bulk write async failed");
01466 
01467         total_written += ret;
01468         offset += write_size;
01469     }
01470 
01471     return total_written;
01472 }
01473 #endif // LIBFTDI_LINUX_ASYNC_MODE
01474 
01485 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
01486 {
01487     if (ftdi == NULL)
01488         ftdi_error_return(-1, "ftdi context invalid");
01489 
01490     ftdi->writebuffer_chunksize = chunksize;
01491     return 0;
01492 }
01493 
01503 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
01504 {
01505     if (ftdi == NULL)
01506         ftdi_error_return(-1, "ftdi context invalid");
01507 
01508     *chunksize = ftdi->writebuffer_chunksize;
01509     return 0;
01510 }
01511 
01527 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
01528 {
01529     int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
01530     int packet_size;
01531 
01532     if (ftdi == NULL || ftdi->usb_dev == NULL)
01533         ftdi_error_return(-666, "USB device unavailable");
01534 
01535     packet_size = ftdi->max_packet_size;
01536     // Packet size sanity check (avoid division by zero)
01537     if (packet_size == 0)
01538         ftdi_error_return(-1, "max_packet_size is bogus (zero)");
01539 
01540     // everything we want is still in the readbuffer?
01541     if (size <= ftdi->readbuffer_remaining)
01542     {
01543         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
01544 
01545         // Fix offsets
01546         ftdi->readbuffer_remaining -= size;
01547         ftdi->readbuffer_offset += size;
01548 
01549         /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
01550 
01551         return size;
01552     }
01553     // something still in the readbuffer, but not enough to satisfy 'size'?
01554     if (ftdi->readbuffer_remaining != 0)
01555     {
01556         memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
01557 
01558         // Fix offset
01559         offset += ftdi->readbuffer_remaining;
01560     }
01561     // do the actual USB read
01562     while (offset < size && ret > 0)
01563     {
01564         ftdi->readbuffer_remaining = 0;
01565         ftdi->readbuffer_offset = 0;
01566         /* returns how much received */
01567         ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
01568         if (ret < 0)
01569             ftdi_error_return(ret, "usb bulk read failed");
01570 
01571         if (ret > 2)
01572         {
01573             // skip FTDI status bytes.
01574             // Maybe stored in the future to enable modem use
01575             num_of_chunks = ret / packet_size;
01576             chunk_remains = ret % packet_size;
01577             //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
01578 
01579             ftdi->readbuffer_offset += 2;
01580             ret -= 2;
01581 
01582             if (ret > packet_size - 2)
01583             {
01584                 for (i = 1; i < num_of_chunks; i++)
01585                     memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01586                              ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01587                              packet_size - 2);
01588                 if (chunk_remains > 2)
01589                 {
01590                     memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
01591                              ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
01592                              chunk_remains-2);
01593                     ret -= 2*num_of_chunks;
01594                 }
01595                 else
01596                     ret -= 2*(num_of_chunks-1)+chunk_remains;
01597             }
01598         }
01599         else if (ret <= 2)
01600         {
01601             // no more data to read?
01602             return offset;
01603         }
01604         if (ret > 0)
01605         {
01606             // data still fits in buf?
01607             if (offset+ret <= size)
01608             {
01609                 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
01610                 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
01611                 offset += ret;
01612 
01613                 /* Did we read exactly the right amount of bytes? */
01614                 if (offset == size)
01615                     //printf("read_data exact rem %d offset %d\n",
01616                     //ftdi->readbuffer_remaining, offset);
01617                     return offset;
01618             }
01619             else
01620             {
01621                 // only copy part of the data or size <= readbuffer_chunksize
01622                 int part_size = size-offset;
01623                 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
01624 
01625                 ftdi->readbuffer_offset += part_size;
01626                 ftdi->readbuffer_remaining = ret-part_size;
01627                 offset += part_size;
01628 
01629                 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
01630                 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
01631 
01632                 return offset;
01633             }
01634         }
01635     }
01636     // never reached
01637     return -127;
01638 }
01639 
01652 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
01653 {
01654     unsigned char *new_buf;
01655 
01656     if (ftdi == NULL)
01657         ftdi_error_return(-1, "ftdi context invalid");
01658 
01659     // Invalidate all remaining data
01660     ftdi->readbuffer_offset = 0;
01661     ftdi->readbuffer_remaining = 0;
01662 
01663     if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
01664         ftdi_error_return(-1, "out of memory for readbuffer");
01665 
01666     ftdi->readbuffer = new_buf;
01667     ftdi->readbuffer_chunksize = chunksize;
01668 
01669     return 0;
01670 }
01671 
01681 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
01682 {
01683     if (ftdi == NULL)
01684         ftdi_error_return(-1, "FTDI context invalid");
01685 
01686     *chunksize = ftdi->readbuffer_chunksize;
01687     return 0;
01688 }
01689 
01690 
01704 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
01705 {
01706     unsigned short usb_val;
01707 
01708     if (ftdi == NULL || ftdi->usb_dev == NULL)
01709         ftdi_error_return(-2, "USB device unavailable");
01710 
01711     usb_val = bitmask; // low byte: bitmask
01712     /* FT2232C: Set bitbang_mode to 2 to enable SPI */
01713     usb_val |= (ftdi->bitbang_mode << 8);
01714 
01715     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01716                         SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
01717                         NULL, 0, ftdi->usb_write_timeout) != 0)
01718         ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
01719 
01720     ftdi->bitbang_enabled = 1;
01721     return 0;
01722 }
01723 
01733 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
01734 {
01735     if (ftdi == NULL || ftdi->usb_dev == NULL)
01736         ftdi_error_return(-2, "USB device unavailable");
01737 
01738     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
01739         ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
01740 
01741     ftdi->bitbang_enabled = 0;
01742     return 0;
01743 }
01744 
01757 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
01758 {
01759     unsigned short usb_val;
01760 
01761     if (ftdi == NULL || ftdi->usb_dev == NULL)
01762         ftdi_error_return(-2, "USB device unavailable");
01763 
01764     usb_val = bitmask; // low byte: bitmask
01765     usb_val |= (mode << 8);
01766     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
01767         ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
01768 
01769     ftdi->bitbang_mode = mode;
01770     ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
01771     return 0;
01772 }
01773 
01784 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
01785 {
01786     if (ftdi == NULL || ftdi->usb_dev == NULL)
01787         ftdi_error_return(-2, "USB device unavailable");
01788 
01789     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
01790         ftdi_error_return(-1, "read pins failed");
01791 
01792     return 0;
01793 }
01794 
01810 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
01811 {
01812     unsigned short usb_val;
01813 
01814     if (latency < 1)
01815         ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
01816 
01817     if (ftdi == NULL || ftdi->usb_dev == NULL)
01818         ftdi_error_return(-3, "USB device unavailable");
01819 
01820     usb_val = latency;
01821     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
01822         ftdi_error_return(-2, "unable to set latency timer");
01823 
01824     return 0;
01825 }
01826 
01837 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
01838 {
01839     unsigned short usb_val;
01840 
01841     if (ftdi == NULL || ftdi->usb_dev == NULL)
01842         ftdi_error_return(-2, "USB device unavailable");
01843 
01844     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
01845         ftdi_error_return(-1, "reading latency timer failed");
01846 
01847     *latency = (unsigned char)usb_val;
01848     return 0;
01849 }
01850 
01891 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
01892 {
01893     char usb_val[2];
01894 
01895     if (ftdi == NULL || ftdi->usb_dev == NULL)
01896         ftdi_error_return(-2, "USB device unavailable");
01897 
01898     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, usb_val, 2, ftdi->usb_read_timeout) != 2)
01899         ftdi_error_return(-1, "getting modem status failed");
01900 
01901     *status = (usb_val[1] << 8) | usb_val[0];
01902 
01903     return 0;
01904 }
01905 
01917 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
01918 {
01919     if (ftdi == NULL || ftdi->usb_dev == NULL)
01920         ftdi_error_return(-2, "USB device unavailable");
01921 
01922     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01923                         SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
01924                         NULL, 0, ftdi->usb_write_timeout) != 0)
01925         ftdi_error_return(-1, "set flow control failed");
01926 
01927     return 0;
01928 }
01929 
01940 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
01941 {
01942     unsigned short usb_val;
01943 
01944     if (ftdi == NULL || ftdi->usb_dev == NULL)
01945         ftdi_error_return(-2, "USB device unavailable");
01946 
01947     if (state)
01948         usb_val = SIO_SET_DTR_HIGH;
01949     else
01950         usb_val = SIO_SET_DTR_LOW;
01951 
01952     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01953                         SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
01954                         NULL, 0, ftdi->usb_write_timeout) != 0)
01955         ftdi_error_return(-1, "set dtr failed");
01956 
01957     return 0;
01958 }
01959 
01970 int ftdi_setrts(struct ftdi_context *ftdi, int state)
01971 {
01972     unsigned short usb_val;
01973 
01974     if (ftdi == NULL || ftdi->usb_dev == NULL)
01975         ftdi_error_return(-2, "USB device unavailable");
01976 
01977     if (state)
01978         usb_val = SIO_SET_RTS_HIGH;
01979     else
01980         usb_val = SIO_SET_RTS_LOW;
01981 
01982     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
01983                         SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
01984                         NULL, 0, ftdi->usb_write_timeout) != 0)
01985         ftdi_error_return(-1, "set of rts failed");
01986 
01987     return 0;
01988 }
01989 
02001 int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
02002 {
02003     unsigned short usb_val;
02004 
02005     if (ftdi == NULL || ftdi->usb_dev == NULL)
02006         ftdi_error_return(-2, "USB device unavailable");
02007 
02008     if (dtr)
02009         usb_val = SIO_SET_DTR_HIGH;
02010     else
02011         usb_val = SIO_SET_DTR_LOW;
02012 
02013     if (rts)
02014         usb_val |= SIO_SET_RTS_HIGH;
02015     else
02016         usb_val |= SIO_SET_RTS_LOW;
02017 
02018     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02019                         SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
02020                         NULL, 0, ftdi->usb_write_timeout) != 0)
02021         ftdi_error_return(-1, "set of rts/dtr failed");
02022 
02023     return 0;
02024 }
02025 
02037 int ftdi_set_event_char(struct ftdi_context *ftdi,
02038                         unsigned char eventch, unsigned char enable)
02039 {
02040     unsigned short usb_val;
02041 
02042     if (ftdi == NULL || ftdi->usb_dev == NULL)
02043         ftdi_error_return(-2, "USB device unavailable");
02044 
02045     usb_val = eventch;
02046     if (enable)
02047         usb_val |= 1 << 8;
02048 
02049     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
02050         ftdi_error_return(-1, "setting event character failed");
02051 
02052     return 0;
02053 }
02054 
02066 int ftdi_set_error_char(struct ftdi_context *ftdi,
02067                         unsigned char errorch, unsigned char enable)
02068 {
02069     unsigned short usb_val;
02070 
02071     if (ftdi == NULL || ftdi->usb_dev == NULL)
02072         ftdi_error_return(-2, "USB device unavailable");
02073 
02074     usb_val = errorch;
02075     if (enable)
02076         usb_val |= 1 << 8;
02077 
02078     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
02079         ftdi_error_return(-1, "setting error character failed");
02080 
02081     return 0;
02082 }
02083 
02092 void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
02093 {
02094     if (ftdi == NULL)
02095         return;
02096 
02097     ftdi->eeprom_size=size;
02098     eeprom->size=size;
02099 }
02100 
02106 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
02107 {
02108     if (eeprom == NULL)
02109         return;
02110 
02111     eeprom->vendor_id = 0x0403;
02112     eeprom->product_id = 0x6001;
02113 
02114     eeprom->self_powered = 1;
02115     eeprom->remote_wakeup = 1;
02116     eeprom->BM_type_chip = 1;
02117 
02118     eeprom->in_is_isochronous = 0;
02119     eeprom->out_is_isochronous = 0;
02120     eeprom->suspend_pull_downs = 0;
02121 
02122     eeprom->use_serial = 0;
02123     eeprom->change_usb_version = 0;
02124     eeprom->usb_version = 0x0200;
02125     eeprom->max_power = 0;
02126 
02127     eeprom->manufacturer = NULL;
02128     eeprom->product = NULL;
02129     eeprom->serial = NULL;
02130 
02131     eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
02132 }
02133 
02139 void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
02140 {
02141     if (!eeprom)
02142         return;
02143 
02144     if (eeprom->manufacturer != 0) {
02145         free(eeprom->manufacturer);
02146         eeprom->manufacturer = 0;
02147     }
02148     if (eeprom->product != 0) {
02149         free(eeprom->product);
02150         eeprom->product = 0;
02151     }
02152     if (eeprom->serial != 0) {
02153         free(eeprom->serial);
02154         eeprom->serial = 0;
02155     }
02156 }
02157 
02169 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
02170 {
02171     unsigned char i, j;
02172     unsigned short checksum, value;
02173     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
02174     int size_check;
02175 
02176     if (eeprom == NULL)
02177         return -2;
02178 
02179     if (eeprom->manufacturer != NULL)
02180         manufacturer_size = strlen(eeprom->manufacturer);
02181     if (eeprom->product != NULL)
02182         product_size = strlen(eeprom->product);
02183     if (eeprom->serial != NULL)
02184         serial_size = strlen(eeprom->serial);
02185 
02186     size_check = eeprom->size;
02187     size_check -= 28; // 28 are always in use (fixed)
02188 
02189     // Top half of a 256byte eeprom is used just for strings and checksum
02190     // it seems that the FTDI chip will not read these strings from the lower half
02191     // Each string starts with two bytes; offset and type (0x03 for string)
02192     // the checksum needs two bytes, so without the string data that 8 bytes from the top half
02193     if (eeprom->size>=256)size_check = 120;
02194     size_check -= manufacturer_size*2;
02195     size_check -= product_size*2;
02196     size_check -= serial_size*2;
02197 
02198     // eeprom size exceeded?
02199     if (size_check < 0)
02200         return (-1);
02201 
02202     // empty eeprom
02203     memset (output, 0, eeprom->size);
02204 
02205     // Addr 00: Stay 00 00
02206     // Addr 02: Vendor ID
02207     output[0x02] = eeprom->vendor_id;
02208     output[0x03] = eeprom->vendor_id >> 8;
02209 
02210     // Addr 04: Product ID
02211     output[0x04] = eeprom->product_id;
02212     output[0x05] = eeprom->product_id >> 8;
02213 
02214     // Addr 06: Device release number (0400h for BM features)
02215     output[0x06] = 0x00;
02216 
02217     if (eeprom->BM_type_chip == 1)
02218         output[0x07] = 0x04;
02219     else
02220         output[0x07] = 0x02;
02221 
02222     // Addr 08: Config descriptor
02223     // Bit 7: always 1
02224     // Bit 6: 1 if this device is self powered, 0 if bus powered
02225     // Bit 5: 1 if this device uses remote wakeup
02226     // Bit 4: 1 if this device is battery powered
02227     j = 0x80;
02228     if (eeprom->self_powered == 1)
02229         j |= 0x40;
02230     if (eeprom->remote_wakeup == 1)
02231         j |= 0x20;
02232     output[0x08] = j;
02233 
02234     // Addr 09: Max power consumption: max power = value * 2 mA
02235     output[0x09] = eeprom->max_power;
02236 
02237     // Addr 0A: Chip configuration
02238     // Bit 7: 0 - reserved
02239     // Bit 6: 0 - reserved
02240     // Bit 5: 0 - reserved
02241     // Bit 4: 1 - Change USB version
02242     // Bit 3: 1 - Use the serial number string
02243     // Bit 2: 1 - Enable suspend pull downs for lower power
02244     // Bit 1: 1 - Out EndPoint is Isochronous
02245     // Bit 0: 1 - In EndPoint is Isochronous
02246     //
02247     j = 0;
02248     if (eeprom->in_is_isochronous == 1)
02249         j = j | 1;
02250     if (eeprom->out_is_isochronous == 1)
02251         j = j | 2;
02252     if (eeprom->suspend_pull_downs == 1)
02253         j = j | 4;
02254     if (eeprom->use_serial == 1)
02255         j = j | 8;
02256     if (eeprom->change_usb_version == 1)
02257         j = j | 16;
02258     output[0x0A] = j;
02259 
02260     // Addr 0B: reserved
02261     output[0x0B] = 0x00;
02262 
02263     // Addr 0C: USB version low byte when 0x0A bit 4 is set
02264     // Addr 0D: USB version high byte when 0x0A bit 4 is set
02265     if (eeprom->change_usb_version == 1)
02266     {
02267         output[0x0C] = eeprom->usb_version;
02268         output[0x0D] = eeprom->usb_version >> 8;
02269     }
02270 
02271 
02272     // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
02273     // Addr 0F: Length of manufacturer string
02274     output[0x0F] = manufacturer_size*2 + 2;
02275 
02276     // Addr 10: Offset of the product string + 0x80, calculated later
02277     // Addr 11: Length of product string
02278     output[0x11] = product_size*2 + 2;
02279 
02280     // Addr 12: Offset of the serial string + 0x80, calculated later
02281     // Addr 13: Length of serial string
02282     output[0x13] = serial_size*2 + 2;
02283 
02284     // Dynamic content
02285     i=0x14;
02286     if (eeprom->size>=256) i = 0x80;
02287 
02288 
02289     // Output manufacturer
02290     output[0x0E] = i | 0x80;  // calculate offset
02291     output[i++] = manufacturer_size*2 + 2;
02292     output[i++] = 0x03; // type: string
02293     for (j = 0; j < manufacturer_size; j++)
02294     {
02295         output[i] = eeprom->manufacturer[j], i++;
02296         output[i] = 0x00, i++;
02297     }
02298 
02299     // Output product name
02300     output[0x10] = i | 0x80;  // calculate offset
02301     output[i] = product_size*2 + 2, i++;
02302     output[i] = 0x03, i++;
02303     for (j = 0; j < product_size; j++)
02304     {
02305         output[i] = eeprom->product[j], i++;
02306         output[i] = 0x00, i++;
02307     }
02308 
02309     // Output serial
02310     output[0x12] = i | 0x80; // calculate offset
02311     output[i] = serial_size*2 + 2, i++;
02312     output[i] = 0x03, i++;
02313     for (j = 0; j < serial_size; j++)
02314     {
02315         output[i] = eeprom->serial[j], i++;
02316         output[i] = 0x00, i++;
02317     }
02318 
02319     // calculate checksum
02320     checksum = 0xAAAA;
02321 
02322     for (i = 0; i < eeprom->size/2-1; i++)
02323     {
02324         value = output[i*2];
02325         value += output[(i*2)+1] << 8;
02326 
02327         checksum = value^checksum;
02328         checksum = (checksum << 1) | (checksum >> 15);
02329     }
02330 
02331     output[eeprom->size-2] = checksum;
02332     output[eeprom->size-1] = checksum >> 8;
02333 
02334     return size_check;
02335 }
02336 
02350 int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
02351 {
02352     unsigned char i, j;
02353     unsigned short checksum, eeprom_checksum, value;
02354     unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
02355     int size_check;
02356     int eeprom_size = 128;
02357 
02358     if (eeprom == NULL)
02359         return -1;
02360 #if 0
02361     size_check = eeprom->size;
02362     size_check -= 28; // 28 are always in use (fixed)
02363 
02364     // Top half of a 256byte eeprom is used just for strings and checksum
02365     // it seems that the FTDI chip will not read these strings from the lower half
02366     // Each string starts with two bytes; offset and type (0x03 for string)
02367     // the checksum needs two bytes, so without the string data that 8 bytes from the top half
02368     if (eeprom->size>=256)size_check = 120;
02369     size_check -= manufacturer_size*2;
02370     size_check -= product_size*2;
02371     size_check -= serial_size*2;
02372 
02373     // eeprom size exceeded?
02374     if (size_check < 0)
02375         return (-1);
02376 #endif
02377 
02378     // empty eeprom struct
02379     memset(eeprom, 0, sizeof(struct ftdi_eeprom));
02380 
02381     // Addr 00: Stay 00 00
02382 
02383     // Addr 02: Vendor ID
02384     eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
02385 
02386     // Addr 04: Product ID
02387     eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
02388 
02389     value = buf[0x06] + (buf[0x07]<<8);
02390     switch (value)
02391     {
02392         case 0x0400:
02393             eeprom->BM_type_chip = 1;
02394             break;
02395         case 0x0200:
02396             eeprom->BM_type_chip = 0;
02397             break;
02398         default: // Unknown device
02399             eeprom->BM_type_chip = 0;
02400             break;
02401     }
02402 
02403     // Addr 08: Config descriptor
02404     // Bit 7: always 1
02405     // Bit 6: 1 if this device is self powered, 0 if bus powered
02406     // Bit 5: 1 if this device uses remote wakeup
02407     // Bit 4: 1 if this device is battery powered
02408     j = buf[0x08];
02409     if (j&0x40) eeprom->self_powered = 1;
02410     if (j&0x20) eeprom->remote_wakeup = 1;
02411 
02412     // Addr 09: Max power consumption: max power = value * 2 mA
02413     eeprom->max_power = buf[0x09];
02414 
02415     // Addr 0A: Chip configuration
02416     // Bit 7: 0 - reserved
02417     // Bit 6: 0 - reserved
02418     // Bit 5: 0 - reserved
02419     // Bit 4: 1 - Change USB version
02420     // Bit 3: 1 - Use the serial number string
02421     // Bit 2: 1 - Enable suspend pull downs for lower power
02422     // Bit 1: 1 - Out EndPoint is Isochronous
02423     // Bit 0: 1 - In EndPoint is Isochronous
02424     //
02425     j = buf[0x0A];
02426     if (j&0x01) eeprom->in_is_isochronous = 1;
02427     if (j&0x02) eeprom->out_is_isochronous = 1;
02428     if (j&0x04) eeprom->suspend_pull_downs = 1;
02429     if (j&0x08) eeprom->use_serial = 1;
02430     if (j&0x10) eeprom->change_usb_version = 1;
02431 
02432     // Addr 0B: reserved
02433 
02434     // Addr 0C: USB version low byte when 0x0A bit 4 is set
02435     // Addr 0D: USB version high byte when 0x0A bit 4 is set
02436     if (eeprom->change_usb_version == 1)
02437     {
02438         eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
02439     }
02440 
02441     // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
02442     // Addr 0F: Length of manufacturer string
02443     manufacturer_size = buf[0x0F]/2;
02444     if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
02445     else eeprom->manufacturer = NULL;
02446 
02447     // Addr 10: Offset of the product string + 0x80, calculated later
02448     // Addr 11: Length of product string
02449     product_size = buf[0x11]/2;
02450     if (product_size > 0) eeprom->product = malloc(product_size);
02451     else eeprom->product = NULL;
02452 
02453     // Addr 12: Offset of the serial string + 0x80, calculated later
02454     // Addr 13: Length of serial string
02455     serial_size = buf[0x13]/2;
02456     if (serial_size > 0) eeprom->serial = malloc(serial_size);
02457     else eeprom->serial = NULL;
02458 
02459     // Decode manufacturer
02460     i = buf[0x0E] & 0x7f; // offset
02461     for (j=0;j<manufacturer_size-1;j++)
02462     {
02463         eeprom->manufacturer[j] = buf[2*j+i+2];
02464     }
02465     eeprom->manufacturer[j] = '\0';
02466 
02467     // Decode product name
02468     i = buf[0x10] & 0x7f; // offset
02469     for (j=0;j<product_size-1;j++)
02470     {
02471         eeprom->product[j] = buf[2*j+i+2];
02472     }
02473     eeprom->product[j] = '\0';
02474 
02475     // Decode serial
02476     i = buf[0x12] & 0x7f; // offset
02477     for (j=0;j<serial_size-1;j++)
02478     {
02479         eeprom->serial[j] = buf[2*j+i+2];
02480     }
02481     eeprom->serial[j] = '\0';
02482 
02483     // verify checksum
02484     checksum = 0xAAAA;
02485 
02486     for (i = 0; i < eeprom_size/2-1; i++)
02487     {
02488         value = buf[i*2];
02489         value += buf[(i*2)+1] << 8;
02490 
02491         checksum = value^checksum;
02492         checksum = (checksum << 1) | (checksum >> 15);
02493     }
02494 
02495     eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
02496 
02497     if (eeprom_checksum != checksum)
02498     {
02499         fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
02500         return -1;
02501     }
02502 
02503     return 0;
02504 }
02505 
02517 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
02518 {
02519     if (ftdi == NULL || ftdi->usb_dev == NULL)
02520         ftdi_error_return(-2, "USB device unavailable");
02521 
02522     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
02523         ftdi_error_return(-1, "reading eeprom failed");
02524 
02525     return 0;
02526 }
02527 
02538 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
02539 {
02540     int i;
02541 
02542     if (ftdi == NULL || ftdi->usb_dev == NULL)
02543         ftdi_error_return(-2, "USB device unavailable");
02544 
02545     for (i = 0; i < ftdi->eeprom_size/2; i++)
02546     {
02547         if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
02548             ftdi_error_return(-1, "reading eeprom failed");
02549     }
02550 
02551     return 0;
02552 }
02553 
02554 /*
02555     ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
02556     Function is only used internally
02557     \internal
02558 */
02559 static unsigned char ftdi_read_chipid_shift(unsigned char value)
02560 {
02561     return ((value & 1) << 1) |
02562            ((value & 2) << 5) |
02563            ((value & 4) >> 2) |
02564            ((value & 8) << 4) |
02565            ((value & 16) >> 1) |
02566            ((value & 32) >> 1) |
02567            ((value & 64) >> 4) |
02568            ((value & 128) >> 2);
02569 }
02570 
02581 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
02582 {
02583     unsigned int a = 0, b = 0;
02584 
02585     if (ftdi == NULL || ftdi->usb_dev == NULL)
02586         ftdi_error_return(-2, "USB device unavailable");
02587 
02588     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
02589     {
02590         a = a << 8 | a >> 8;
02591         if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
02592         {
02593             b = b << 8 | b >> 8;
02594             a = (a << 16) | (b & 0xFFFF);
02595             a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
02596                 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
02597             *chipid = a ^ 0xa5f0f7d1;
02598             return 0;
02599         }
02600     }
02601 
02602     ftdi_error_return(-1, "read of FTDIChip-ID failed");
02603 }
02604 
02617 int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
02618 {
02619     int i=0,j,minsize=32;
02620     int size=minsize;
02621 
02622     if (ftdi == NULL || ftdi->usb_dev == NULL)
02623         ftdi_error_return(-2, "USB device unavailable");
02624 
02625     do
02626     {
02627         for (j = 0; i < maxsize/2 && j<size; j++)
02628         {
02629             if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
02630                                 SIO_READ_EEPROM_REQUEST, 0, i,
02631                                 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
02632                 ftdi_error_return(-1, "eeprom read failed");
02633             i++;
02634         }
02635         size*=2;
02636     }
02637     while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
02638 
02639     return size/2;
02640 }
02641 
02653 int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
02654 {
02655     if (ftdi == NULL || ftdi->usb_dev == NULL)
02656         ftdi_error_return(-2, "USB device unavailable");
02657 
02658     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02659                                     SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
02660                                     NULL, 0, ftdi->usb_write_timeout) != 0)
02661         ftdi_error_return(-1, "unable to write eeprom");
02662 
02663     return 0;
02664 }
02665 
02676 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
02677 {
02678     unsigned short usb_val, status;
02679     int i, ret;
02680 
02681     if (ftdi == NULL || ftdi->usb_dev == NULL)
02682         ftdi_error_return(-2, "USB device unavailable");
02683 
02684     /* These commands were traced while running MProg */
02685     if ((ret = ftdi_usb_reset(ftdi)) != 0)
02686         return ret;
02687     if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
02688         return ret;
02689     if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
02690         return ret;
02691 
02692     for (i = 0; i < ftdi->eeprom_size/2; i++)
02693     {
02694         usb_val = eeprom[i*2];
02695         usb_val += eeprom[(i*2)+1] << 8;
02696         if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
02697                             SIO_WRITE_EEPROM_REQUEST, usb_val, i,
02698                             NULL, 0, ftdi->usb_write_timeout) != 0)
02699             ftdi_error_return(-1, "unable to write eeprom");
02700     }
02701 
02702     return 0;
02703 }
02704 
02716 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
02717 {
02718     if (ftdi == NULL || ftdi->usb_dev == NULL)
02719         ftdi_error_return(-2, "USB device unavailable");
02720 
02721     if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
02722         ftdi_error_return(-1, "unable to erase eeprom");
02723 
02724     return 0;
02725 }
02726 
02734 char *ftdi_get_error_string (struct ftdi_context *ftdi)
02735 {
02736     if (ftdi == NULL)
02737         return "";
02738 
02739     return ftdi->error_str;
02740 }
02741 
02742 /* @} end of doxygen libftdi group */

Generated on Mon Apr 23 20:57:22 2012 for libftdi by  doxygen 1.4.7