Use the PNP flag as seen with FTD2XX
[libftdi] / src / ftdi.c
index 63e2a36..5ffad8f 100644 (file)
@@ -199,6 +199,21 @@ void ftdi_deinit(struct ftdi_context *ftdi)
 
     if (ftdi->eeprom != NULL)
     {
+        if (ftdi->eeprom->manufacturer != 0)
+        {
+            free(ftdi->eeprom->manufacturer);
+            ftdi->eeprom->manufacturer = 0;
+        }
+        if (ftdi->eeprom->product != 0)
+        {
+            free(ftdi->eeprom->product);
+            ftdi->eeprom->product = 0;
+        }
+        if (ftdi->eeprom->serial != 0)
+        {
+            free(ftdi->eeprom->serial);
+            ftdi->eeprom->serial = 0;
+        }
         free(ftdi->eeprom);
         ftdi->eeprom = NULL;
     }
@@ -2165,17 +2180,25 @@ int ftdi_set_error_char(struct ftdi_context *ftdi,
 /**
     Init eeprom with default values.
     \param ftdi pointer to ftdi_context
-
+    \param manufacturer String to use as Manufacturer
+    \param product String to use as Product description
+    \param serial String to use as Serial number description
+    
+    \retval  0: all fine
+    \retval -1: No struct ftdi_context
+    \retval -2: No struct ftdi_eeprom
 */
-void ftdi_eeprom_initdefaults(struct ftdi_context *ftdi)
+int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
+                              char * product, char * serial)
 {
     struct ftdi_eeprom *eeprom;
 
     if (ftdi == NULL)
-        return;
+        ftdi_error_return(-1, "No struct ftdi_context");
+        
 
     if (ftdi->eeprom == NULL)
-        return;
+        ftdi_error_return(-2,"No struct ftdi_eeprom"); 
 
     eeprom = ftdi->eeprom;
     memset(eeprom, 0, sizeof(struct ftdi_eeprom));
@@ -2193,9 +2216,35 @@ void ftdi_eeprom_initdefaults(struct ftdi_context *ftdi)
         eeprom->usb_version = 0x0200;
     eeprom->max_power = 50;
 
+    if (eeprom->manufacturer)
+        free (eeprom->manufacturer);
     eeprom->manufacturer = NULL;
+    if (manufacturer)
+    {
+        eeprom->manufacturer = malloc(strlen(manufacturer)+1);
+        if (eeprom->manufacturer)
+            strcpy(eeprom->manufacturer, manufacturer);
+    }
+
+    if (eeprom->product)
+        free (eeprom->product);
     eeprom->product = NULL;
+    {
+        eeprom->product = malloc(strlen(product)+1);
+        if (eeprom->product)
+            strcpy(eeprom->product, product);
+    }
+
+    if (eeprom->serial)
+        free (eeprom->serial);
     eeprom->serial = NULL;
+    if (serial)
+    {
+        eeprom->serial = malloc(strlen(serial)+1);
+        if (eeprom->serial)
+            strcpy(eeprom->serial, serial);
+    }
+
 
     if(ftdi->type == TYPE_R)
     {
@@ -2209,34 +2258,7 @@ void ftdi_eeprom_initdefaults(struct ftdi_context *ftdi)
     }
     else
         eeprom->size = -1;
-}
-
-/**
-    Frees allocated memory in eeprom.
-
-    \param ftdi pointer to ftdi_context
-*/
-void ftdi_eeprom_free(struct ftdi_context *ftdi)
-{
-    if (!ftdi)
-        return;
-    if (ftdi->eeprom)
-    {
-        struct ftdi_eeprom *eeprom = ftdi->eeprom;
-
-        if (eeprom->manufacturer != 0) {
-            free(eeprom->manufacturer);
-            eeprom->manufacturer = 0;
-        }
-        if (eeprom->product != 0) {
-            free(eeprom->product);
-            eeprom->product = 0;
-        }
-        if (eeprom->serial != 0) {
-            free(eeprom->serial);
-            eeprom->serial = 0;
-        }
-    }
+    return 0;
 }
 
 /**
@@ -2305,7 +2327,7 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
     size_check -= serial_size*2;
 
     /* Space for the string type and pointer bytes */
-    size_check -= -6;
+    size_check -= -9;
 
     // eeprom size exceeded?
     if (size_check < 0)
@@ -2439,6 +2461,13 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
         output[i & k] = eeprom->serial[j], i++;
         output[i & k] = 0x00, i++;
     }
+    output[i & k] = 0x02; /* as seen when written with FTD2XX */
+    i++;
+    output[i & k] = 0x03; /* as seen when written with FTD2XX */
+    i++;
+    output[i & k] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
+    i++;
+
     output[0x13] = serial_size*2 + 2;
 
     /* Fixme: ftd2xx seems to append 0x02, 0x03 and 0x01 for PnP = 0 or 0x00 else */
@@ -2698,6 +2727,8 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
     // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
     // Addr 0F: Length of manufacturer string
     manufacturer_size = buf[0x0F]/2;
+    if(eeprom->manufacturer)
+        free(eeprom->manufacturer);
     if (manufacturer_size > 0) 
     {
         eeprom->manufacturer = malloc(manufacturer_size);
@@ -2716,6 +2747,8 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
 
     // Addr 10: Offset of the product string + 0x80, calculated later
     // Addr 11: Length of product string
+    if(eeprom->product)
+        free(eeprom->product);
     product_size = buf[0x11]/2;
     if (product_size > 0)
     {
@@ -2735,6 +2768,8 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
 
     // Addr 12: Offset of the serial string + 0x80, calculated later
     // Addr 13: Length of serial string
+    if(eeprom->serial)
+        free(eeprom->serial);
     serial_size = buf[0x13]/2;
     if (serial_size > 0)
     {
@@ -2866,6 +2901,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
             fprintf(stdout, "Pull IO pins low during suspend\n");
         if(eeprom->remote_wakeup)
             fprintf(stdout, "Enable Remote Wake Up\n");
+        fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
         if (ftdi->type >= TYPE_2232C)
             fprintf(stdout,"Channel A has Mode %s%s%s\n", 
                     channel_mode[eeprom->channel_a_type],