libftdi: (tomj) autodetection of chip type
[libftdi] / ftdi / ftdi.c
index 37d4385..11dd3bc 100644 (file)
@@ -104,6 +104,16 @@ int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
                     if (ftdi_set_baudrate (ftdi, 9600) != 0)
                         return -7;
 
+                   // 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;
+
                     return 0;
                 } else {
                     ftdi->error_str = "usb_open() failed";
@@ -255,7 +265,7 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
     }
     // Split into "value" and "index" values
     *value = (unsigned short)(encoded_divisor & 0xFFFF);
-    if(ftdi->type == TYPE_FT2232C) {
+    if(ftdi->type == TYPE_2232C) {
         *index = (unsigned short)(encoded_divisor >> 8);
         *index &= 0xFF00;
         *index |= ftdi->interface;
@@ -510,8 +520,8 @@ int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
 
 
 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
-    eeprom->vendor_id = 0403;
-    eeprom->product_id = 6001;
+    eeprom->vendor_id = 0x0403;
+    eeprom->product_id = 0x6001;
 
     eeprom->self_powered = 1;
     eeprom->remote_wakeup = 1;
@@ -523,7 +533,7 @@ void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
 
     eeprom->use_serial = 0;
     eeprom->change_usb_version = 0;
-    eeprom->usb_version = 200;
+    eeprom->usb_version = 0x0200;
     eeprom->max_power = 0;
 
     eeprom->manufacturer = NULL;