libftdi: (tomj) ported set_line_property patch to HEAD
[libftdi] / src / ftdi.c
index 908d420..e10e052 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);                 
 
@@ -56,18 +55,18 @@ 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
+    0: all fine
+   -1: unknown interface
 */
-int ftdi_select_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
+int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
 {
     switch (interface) {
     case INTERFACE_ANY:
     case INTERFACE_A:
-        /* ftdi_usb_open_desc cares to set the right index, depending on the found chip*/
+        /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
         break;
     case INTERFACE_B:
         ftdi->interface = 1;
@@ -199,24 +198,9 @@ int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
 
 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;
@@ -392,6 +376,50 @@ int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
     return 0;
 }
 
+/*
+    set (RS232) line characteristics
+    by Alain Abbas - 
+*/
+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 +501,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,