libftdi: (tomj) FTDICHIP-ID read support
[libftdi] / src / ftdi.c
index 950e84e..b029b01 100644 (file)
@@ -158,6 +158,7 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli
         ftdi_error_return(-2, "usb_find_devices() failed");
 
     curdev = devlist;
+    *curdev = NULL;
     for (bus = usb_busses; bus; bus = bus->next) {
         for (dev = bus->devices; dev; dev = dev->next) {
             if (dev->descriptor.idVendor == vendor
@@ -186,13 +187,15 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli
 */
 void ftdi_list_free(struct ftdi_device_list **devlist)
 {
-    struct ftdi_device_list **curdev;
-    for (; *devlist == NULL; devlist = curdev) {
-        curdev = &(*devlist)->next;
-        free(*devlist);
+    struct ftdi_device_list *curdev, *next;
+
+    for (curdev = *devlist; curdev != NULL;) {
+        next = curdev->next;
+        free(curdev);
+        curdev = next;
     }
 
-    devlist = NULL;
+    *devlist = NULL;
 }
 
 /**
@@ -312,7 +315,8 @@ int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
         ftdi->type = TYPE_2232C;
         if (!ftdi->index)
             ftdi->index = INTERFACE_A;
-    }
+    } else if (dev->descriptor.bcdDevice == 0x600)
+        ftdi->type = TYPE_R;
 
     ftdi_error_return(0, "all fine");
 }
@@ -738,6 +742,7 @@ int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunk
     \param size Size of the buffer
 
     \retval <0: error code from usb_bulk_read()
+    \retval  0: no data was available
     \retval >0: number of bytes read
 
     \remark This function is not useful in bitbang mode.
@@ -1223,6 +1228,56 @@ int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
     return 0;
 }
 
+/*
+    ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
+    Function is only used internally
+    \internal
+*/
+static unsigned char ftdi_read_chipid_shift(unsigned char value)
+{
+    return ((value & 1) << 1) |
+            ((value & 2) << 5) |
+            ((value & 4) >> 2) |
+            ((value & 8) << 4) |
+            ((value & 16) >> 1) |
+            ((value & 32) >> 1) |
+            ((value & 64) >> 4) |
+            ((value & 128) >> 2);
+}
+
+/**
+    Read the FTDIChip-ID from R-type devices
+
+    \param ftdi pointer to ftdi_context
+    \param chipid Pointer to store FTDIChip-ID
+
+    \retval  0: all fine
+    \retval -1: read failed
+*/
+int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
+{
+    unsigned int a = 0, b = 0, result = -1;
+
+    if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
+    {
+        a = a << 8 | a >> 8;
+        if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
+        {
+            b = b << 8 | b >> 8;
+            a = (a << 16) | b;
+            a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8;
+            a |= ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
+            *chipid = a ^ 0xa5f0f7d1;
+            result = 0;
+        }
+    }
+
+    if (result != 0)
+        ftdi_error_return(result, "read of FTDIChip-ID failed");
+
+    return 0;
+}
+
 /**
     Write eeprom