libftdi: (tomj) increase for 0.7 release, updated changelog based on diff
[libftdi] / src / ftdi.c
index e234fd1..4c4d31a 100644 (file)
 
 #include <usb.h>
 #include <string.h>
-#include <sys/utsname.h>
 
 #include "ftdi.h"
 
 #define ftdi_error_return(code, str) do {  \
-       ftdi->error_str = str;             \
+        ftdi->error_str = str;             \
         return code;                       \
    } while(0);                 
 
 
-/* ftdi_init return codes:
-   0: all fine
-  -1: couldn't allocate read buffer
+/* ftdi_init
+
+  Initializes a ftdi_context.
+
+  Return codes:
+   0: All fine
+  -1: Couldn't allocate read buffer
 */
 int ftdi_init(struct ftdi_context *ftdi)
 {
@@ -56,8 +59,11 @@ int ftdi_init(struct ftdi_context *ftdi)
     /* All fine. Now allocate the readbuffer */
     return ftdi_read_data_set_chunksize(ftdi, 4096);
 }
-/* ftdi_select_interface
+
+/* ftdi_set_interface
+   
    Call after ftdi_init
+   
    Open selected channels on a chip, otherwise use first channel
     0: all fine
    -1: unknown interface
@@ -81,6 +87,10 @@ int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
     return 0;
 }
 
+/* ftdi_deinit
+
+   Deinitializes a ftdi_context.
+*/
 void ftdi_deinit(struct ftdi_context *ftdi)
 {
     if (ftdi->readbuffer != NULL) {
@@ -89,31 +99,154 @@ void ftdi_deinit(struct ftdi_context *ftdi)
     }
 }
 
-
+/* ftdi_set_usbdev
+   Use an already open device.
+*/
 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
 {
     ftdi->usb_dev = usb;
 }
 
 
-/* ftdi_usb_open return codes:
-   0: all fine
-  -1: usb_find_busses() failed
-  -2: usb_find_devices() failed
-  -3: usb device not found
-  -4: unable to open device
-  -5: unable to claim device
-  -6: reset failed
-  -7: set baudrate failed
-  -8: get product description failed
-  -9: get serial number failed
-  -10: unable to close device
+/* ftdi_usb_find_all
+   Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
+   needs to be deallocated by ftdi_list_free after use.
+
+   Return codes:
+    >0: number of devices found
+    -1: usb_find_busses() failed
+    -2: usb_find_devices() failed
+    -3: out of memory
 */
+int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product) 
+{
+    struct ftdi_device_list **curdev;
+    struct usb_bus *bus;
+    struct usb_device *dev;
+    int count = 0;
+    
+    usb_init();
+    if (usb_find_busses() < 0)
+        ftdi_error_return(-1, "usb_find_busses() failed");
+    if (usb_find_devices() < 0)
+        ftdi_error_return(-2, "usb_find_devices() failed");
+
+    curdev = devlist;
+    for (bus = usb_busses; bus; bus = bus->next) {
+        for (dev = bus->devices; dev; dev = dev->next) {
+            if (dev->descriptor.idVendor == vendor
+                    && dev->descriptor.idProduct == product)
+            {
+                *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
+                if (!*curdev)
+                    ftdi_error_return(-3, "out of memory");
+                
+                (*curdev)->next = NULL;
+                (*curdev)->dev = dev;
+
+                curdev = &(*curdev)->next;
+                count++;
+            }
+        }
+    }
+    
+    return count;
+}
+
+/* ftdi_list_free
+
+   Frees a created device list.
+*/
+void ftdi_list_free(struct ftdi_device_list **devlist) 
+{
+    struct ftdi_device_list **curdev;
+    for (; *devlist == NULL; devlist = curdev) {
+        curdev = &(*devlist)->next;
+        free(*devlist);
+    }
+
+    devlist = NULL;
+}
+
+/* ftdi_usb_open_dev 
+
+   Opens a ftdi device given by a usb_device.
+   
+   Return codes:
+     0: all fine
+    -4: unable to open device
+    -5: unable to claim device
+    -6: reset failed
+    -7: set baudrate failed
+*/
+int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
+{
+    if (!(ftdi->usb_dev = usb_open(dev)))
+        ftdi_error_return(-4, "usb_open() failed");
+    
+    if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
+        usb_close (ftdi->usb_dev);
+        ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
+    }
+
+    if (ftdi_usb_reset (ftdi) != 0) {
+        usb_close (ftdi->usb_dev);
+        ftdi_error_return(-6, "ftdi_usb_reset failed");
+    }
+
+    if (ftdi_set_baudrate (ftdi, 9600) != 0) {
+        usb_close (ftdi->usb_dev);
+        ftdi_error_return(-7, "set baudrate failed");
+    }
+
+    // Try to guess chip type
+    // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
+    if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
+            && dev->descriptor.iSerialNumber == 0))
+        ftdi->type = TYPE_BM;
+    else if (dev->descriptor.bcdDevice == 0x200)
+        ftdi->type = TYPE_AM;
+    else if (dev->descriptor.bcdDevice == 0x500) {
+        ftdi->type = TYPE_2232C;
+        if (!ftdi->index)
+            ftdi->index = INTERFACE_A;
+    }
+
+    ftdi_error_return(0, "all fine");
+}
+
+/* ftdi_usb_open
+   
+   Opens the first device with a given vendor and product ids.
+   
+   Return codes:
+   See ftdi_usb_open_desc()
+*/  
 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
 {
     return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
 }
 
+/* ftdi_usb_open_desc
+
+   Opens the first device with a given, vendor id, product id,
+   description and serial.
+   
+   Return codes:
+     0: all fine
+    -1: usb_find_busses() failed
+    -2: usb_find_devices() failed
+    -3: usb device not found
+    -4: unable to open device
+    -5: unable to claim device
+    -6: reset failed
+    -7: set baudrate failed
+    -8: get product description failed
+    -9: get serial number failed
+    -10: unable to close device
+*/
 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
                        const char* description, const char* serial)
 {
@@ -125,9 +258,8 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
 
     if (usb_find_busses() < 0)
         ftdi_error_return(-1, "usb_find_busses() failed");
-
     if (usb_find_devices() < 0)
-        ftdi_error_return(-2,"usb_find_devices() failed");
+        ftdi_error_return(-2, "usb_find_devices() failed");
 
     for (bus = usb_busses; bus; bus = bus->next) {
         for (dev = bus->devices; dev; dev = dev->next) {
@@ -142,8 +274,8 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
                         ftdi_error_return(-8, "unable to fetch product description");
                     }
                     if (strncmp(string, description, sizeof(string)) != 0) {
-                        if (usb_close (ftdi->usb_dev) < 0)
-                            ftdi_error_return(-10, "product description not matching");
+                        if (usb_close (ftdi->usb_dev) != 0)
+                            ftdi_error_return(-10, "unable to close device");
                         continue;
                     }
                 }
@@ -153,41 +285,16 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
                         ftdi_error_return(-9, "unable to fetch serial number");
                     }
                     if (strncmp(string, serial, sizeof(string)) != 0) {
-                        ftdi->error_str = "serial number not matching\n";
                         if (usb_close (ftdi->usb_dev) != 0)
-                            ftdi_error_return(-10, "unable to fetch serial number");
+                            ftdi_error_return(-10, "unable to close device");
                         continue;
                     }
                 }
 
-                if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
-                    usb_close (ftdi->usb_dev);
-                    ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
-                }
-
-                if (ftdi_usb_reset (ftdi) != 0) {
-                    usb_close (ftdi->usb_dev);
-                    ftdi_error_return(-6, "ftdi_usb_reset failed");
-                }
-
-                if (ftdi_set_baudrate (ftdi, 9600) != 0) {
-                    usb_close (ftdi->usb_dev);
-                    ftdi_error_return(-7, "set baudrate failed");
-                }
-
-                // Try to guess chip type
-                // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
-                if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
-                        && dev->descriptor.iSerialNumber == 0))
-                    ftdi->type = TYPE_BM;
-                else if (dev->descriptor.bcdDevice == 0x200)
-                    ftdi->type = TYPE_AM;
-                else if (dev->descriptor.bcdDevice == 0x500) {
-                    ftdi->type = TYPE_2232C;
-                    if (!ftdi->index)
-                        ftdi->index = INTERFACE_A;
-                }
-                ftdi_error_return(0, "all fine");
+                if (usb_close (ftdi->usb_dev) != 0)
+                    ftdi_error_return(-10, "unable to close device");
+                
+                return ftdi_usb_open_dev(ftdi, dev);
             }
         }
     }
@@ -196,27 +303,19 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
     ftdi_error_return(-3, "device not found");
 }
 
+/* ftdi_usb_reset
 
+   Resets the ftdi device.
+   
+   Return codes:
+     0: all fine
+    -1: FTDI reset failed
+*/
 int ftdi_usb_reset(struct ftdi_context *ftdi)
 {
-#if defined(__linux__)
-    struct utsname kernelver;
-    int k_major, k_minor, k_myver;
-#endif
-
     if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
         ftdi_error_return(-1,"FTDI reset failed");
 
-#if defined(__linux__)
-    /* Kernel 2.6 (maybe higher versions, too) need an additional usb_reset */
-    if (uname(&kernelver) == 0 && sscanf(kernelver.release, "%d.%d", &k_major, &k_minor) == 2) {
-        k_myver = k_major*10 + k_minor;
-
-        if (k_myver >= 26 && usb_reset(ftdi->usb_dev) != 0)
-            ftdi_error_return(-2, "USB reset failed");
-    }
-#endif
-
     // Invalidate data in the readbuffer
     ftdi->readbuffer_offset = 0;
     ftdi->readbuffer_remaining = 0;
@@ -224,6 +323,15 @@ int ftdi_usb_reset(struct ftdi_context *ftdi)
     return 0;
 }
 
+/* ftdi_usb_purge_buffers
+
+   Cleans the buffers of the ftdi device.
+   
+   Return codes:
+     0: all fine
+    -1: write buffer purge failed
+    -2: read buffer purge failed
+*/
 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
 {
     if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
@@ -239,10 +347,14 @@ int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
     return 0;
 }
 
-/* ftdi_usb_close return codes
-    0: all fine
-   -1: usb_release failed
-   -2: usb_close failed
+/* ftdi_usb_close
+   
+   Closes the ftdi device.
+   
+   Return codes:
+     0: all fine
+    -1: usb_release failed
+    -2: usb_close failed
 */
 int ftdi_usb_close(struct ftdi_context *ftdi)
 {
@@ -341,16 +453,16 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
     encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
     // Deal with special cases for encoded value
     if (encoded_divisor == 1) {
-        encoded_divisor = 0;   // 3000000 baud
+        encoded_divisor = 0;    // 3000000 baud
     } else if (encoded_divisor == 0x4001) {
-        encoded_divisor = 1;   // 2000000 baud (BM only)
+        encoded_divisor = 1;    // 2000000 baud (BM only)
     }
     // Split into "value" and "index" values
     *value = (unsigned short)(encoded_divisor & 0xFFFF);
     if(ftdi->type == TYPE_2232C) {
         *index = (unsigned short)(encoded_divisor >> 8);
         *index &= 0xFF00;
-        *index |= ftdi->interface;
+        *index |= ftdi->index;
     }
     else
         *index = (unsigned short)(encoded_divisor >> 16);
@@ -360,7 +472,11 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
 }
 
 /*
-    ftdi_set_baudrate return codes:
+    ftdi_set_baudrate
+    
+    Sets the chip baudrate
+    
+    Return codes:
      0: all fine
     -1: invalid baudrate
     -2: setting baudrate failed
@@ -392,6 +508,55 @@ int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
     return 0;
 }
 
+/*
+    ftdi_set_line_property
+
+    set (RS232) line characteristics by Alain Abbas
+    
+    Return codes:
+     0: all fine
+    -1: Setting line property failed
+*/
+int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
+                    enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
+{
+    unsigned short value = bits;
+
+    switch(parity) {
+    case NONE:
+        value |= (0x00 << 8);
+        break;
+    case ODD:
+        value |= (0x01 << 8);
+        break;
+    case EVEN:
+        value |= (0x02 << 8);
+        break;
+    case MARK:
+        value |= (0x03 << 8);
+        break;
+    case SPACE:
+        value |= (0x04 << 8);
+        break;
+    }
+    
+    switch(sbit) {
+    case STOP_BIT_1:
+        value |= (0x00 << 11);
+        break;
+    case STOP_BIT_15:
+        value |= (0x01 << 11);
+        break;
+    case STOP_BIT_2:
+        value |= (0x02 << 11);
+        break;
+    }
+    
+    if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
+        ftdi_error_return (-1, "Setting new line property failed");
+    
+    return 0;
+}
 
 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
 {
@@ -473,7 +638,7 @@ int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
             ftdi->readbuffer_offset += 2;
             ret -= 2;
 
-            if (ret > 64) {
+            if (ret > 62) {
                 for (i = 1; i < num_of_chunks; i++)
                     memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
                              ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
@@ -651,7 +816,12 @@ void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
 
 
 /*
-    ftdi_eeprom_build return codes:
+    ftdi_eeprom_build
+    
+    Build binary output from ftdi_eeprom structure.
+    Output is suitable for ftdi_write_eeprom.
+    
+    Return codes:
     positive value: used eeprom size
     -1: eeprom size (128 bytes) exceeded by custom strings
 */