Clean up on error
[libftdi] / src / ftdi.c
index 1e92dc5..0893421 100644 (file)
@@ -2,7 +2,7 @@
                           ftdi.c  -  description
                              -------------------
     begin                : Fri Apr 4 2003
-    copyright            : (C) 2003-2010 by Intra2net AG
+    copyright            : (C) 2003-2011 by Intra2net AG and the libftdi developers
     email                : opensource@intra2net.com
  ***************************************************************************/
 
@@ -74,6 +74,7 @@ static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
     \retval  0: all fine
     \retval -1: couldn't allocate read buffer
     \retval -2: couldn't allocate struct  buffer
+    \retval -3: libusb_init() failed
 
     \remark This should be called before all functions
 */
@@ -94,12 +95,15 @@ int ftdi_init(struct ftdi_context *ftdi)
     ftdi->readbuffer_remaining = 0;
     ftdi->writebuffer_chunksize = 4096;
     ftdi->max_packet_size = 0;
+    ftdi->error_str = NULL;
+    ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
+
+    if (libusb_init(&ftdi->usb_ctx) < 0)
+        ftdi_error_return(-3, "libusb_init() failed");
 
     ftdi_set_interface(ftdi, INTERFACE_ANY);
     ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode  */
 
-    ftdi->error_str = NULL;
-
     if (eeprom == 0)
         ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
     memset(eeprom, 0, sizeof(struct ftdi_eeprom));
@@ -218,7 +222,12 @@ void ftdi_deinit(struct ftdi_context *ftdi)
         free(ftdi->eeprom);
         ftdi->eeprom = NULL;
     }
-    libusb_exit(ftdi->usb_ctx);
+
+    if (ftdi->usb_ctx)
+    {
+        libusb_exit(ftdi->usb_ctx);
+        ftdi->usb_ctx = NULL;
+    }
 }
 
 /**
@@ -258,7 +267,6 @@ void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
 
     \retval >0: number of devices found
     \retval -3: out of memory
-    \retval -4: libusb_init() failed
     \retval -5: libusb_get_device_list() failed
     \retval -6: libusb_get_device_descriptor() failed
 */
@@ -270,9 +278,6 @@ int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devli
     int count = 0;
     int i = 0;
 
-    if (libusb_init(&ftdi->usb_ctx) < 0)
-        ftdi_error_return(-4, "libusb_init() failed");
-
     if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
         ftdi_error_return(-5, "libusb_get_device_list() failed");
 
@@ -469,7 +474,7 @@ static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, l
     \retval -8: ftdi context invalid
     \retval -9: libusb_get_device_descriptor() failed
     \retval -10: libusb_get_config_descriptor() failed
-    \retval -11: libusb_etach_kernel_driver() failed
+    \retval -11: libusb_detach_kernel_driver() failed
     \retval -12: libusb_get_configuration() failed
 */
 int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
@@ -498,8 +503,11 @@ int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
     // if usb_set_configuration() or usb_claim_interface() fails as the
     // detach operation might be denied and everything still works fine.
     // Likely scenario is a static ftdi_sio kernel module.
-    if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
-        detach_errno = errno;
+    if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
+    {
+        if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
+            detach_errno = errno;
+    }
 
     if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
         ftdi_error_return(-12, "libusb_get_configuration () failed");
@@ -643,12 +651,12 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
     char string[256];
     int i = 0;
 
-    if (libusb_init(&ftdi->usb_ctx) < 0)
-        ftdi_error_return(-11, "libusb_init() failed");
-
     if (ftdi == NULL)
         ftdi_error_return(-11, "ftdi context invalid");
 
+    if (libusb_init(&ftdi->usb_ctx) < 0)
+        ftdi_error_return(-11, "libusb_init() failed");
+
     if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
         ftdi_error_return(-12, "libusb_get_device_list() failed");
 
@@ -669,12 +677,12 @@ int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
             {
                 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
                 {
-                    libusb_close (ftdi->usb_dev);
+                    ftdi_usb_close_internal (ftdi);
                     ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
                 }
                 if (strncmp(string, description, sizeof(string)) != 0)
                 {
-                    libusb_close (ftdi->usb_dev);
+                    ftdi_usb_close_internal (ftdi);
                     continue;
                 }
             }
@@ -1376,19 +1384,22 @@ static void ftdi_write_data_cb(struct libusb_transfer *transfer)
 struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
 {
     struct ftdi_transfer_control *tc;
-    struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+    struct libusb_transfer *transfer;
     int write_size, ret;
 
     if (ftdi == NULL || ftdi->usb_dev == NULL)
-    {
-        libusb_free_transfer(transfer);
         return NULL;
-    }
 
     tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
+    if (!tc)
+        return NULL;
 
-    if (!tc || !transfer)
+    transfer = libusb_alloc_transfer(0);
+    if (!transfer)
+    {
+        free(tc);
         return NULL;
+    }
 
     tc->ftdi = ftdi;
     tc->completed = 0;
@@ -1410,8 +1421,7 @@ struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi,
     if (ret < 0)
     {
         libusb_free_transfer(transfer);
-        tc->completed = 1;
-        tc->transfer = NULL;
+        free(tc);
         return NULL;
     }
     tc->transfer = transfer;
@@ -1982,7 +1992,7 @@ int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
     if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
         ftdi_error_return(-1, "getting modem status failed");
 
-    *status = (usb_val[1] << 8) | usb_val[0];
+    *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
 
     return 0;
 }
@@ -2216,6 +2226,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
     if (eeprom->product)
         free (eeprom->product);
     eeprom->product = NULL;
+    if(product)
     {
         eeprom->product = malloc(strlen(product)+1);
         if (eeprom->product)
@@ -2310,6 +2321,7 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
         case TYPE_2232H:            // six extra config bytes + 4 bytes PnP stuff
         case TYPE_4232H:
             user_area_size = 86;
+            break;
         default:
             user_area_size = 0;
             break;
@@ -2941,7 +2953,6 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
                                 "IOMODE","BB_WR","BB_RD"
                                };
             char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
-            int i;
 
             if (eeprom->invert)
             {
@@ -2959,13 +2970,13 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
                             cbus_mux[eeprom->cbus_function[i]]);
                 else
                 {
-                    /* FIXME for Uwe: This results in an access above array bounds.
-                       Also I couldn't find documentation about this mode.
-                    fprintf(stdout,"C%d BB Function: %s\n", i,
-                            cbus_BB[i]);
-                    */
-                    fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
-                    (void)cbus_BB;
+                    if (i < 4)
+                        /* Running MPROG show that C0..3 have fixed function Synchronous
+                           Bit Bang mode */
+                        fprintf(stdout,"C%d BB Function: %s\n", i,
+                                cbus_BB[i]);
+                    else
+                        fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
                 }
             }
         }
@@ -2976,12 +2987,12 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
 /**
    Get a value from the decoded EEPROM structure
 
-   \\param ftdi pointer to ftdi_context
-   \\param value_name Enum of the value to query
-   \\param Pointer to store read value
+   \param ftdi pointer to ftdi_context
+   \param value_name Enum of the value to query
+   \param value Pointer to store read value
 
-   \\retval 0: all fine
-   \\retval -1: Value doesn't exist
+   \retval 0: all fine
+   \retval -1: Value doesn't exist
 */
 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
 {
@@ -3111,13 +3122,13 @@ int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value valu
    Set a value in the decoded EEPROM Structure
    No parameter checking is performed
 
-   \\param ftdi pointer to ftdi_context
-   \\param value_name Enum of the value to query
-   \\param Value to set
+   \param ftdi pointer to ftdi_context
+   \param value_name Enum of the value to set
+   \param value to set
 
-   \\retval 0: all fine
-   \\retval -1: Value doesn't exist
-   \\retval -2: Value not user settable
+   \retval 0: all fine
+   \retval -1: Value doesn't exist
+   \retval -2: Value not user settable
 */
 int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
 {
@@ -3245,17 +3256,27 @@ int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value valu
 /** Get the read-only buffer to the binary EEPROM content
 
     \param ftdi pointer to ftdi_context
-    \param ftdi buffer to receive EEPROM content
+    \param buf buffer to receive EEPROM content
     \param size Size of receiving buffer
 
     \retval 0: All fine
     \retval -1: struct ftdi_contxt or ftdi_eeprom missing
+    \retval -2: Not enough room to store eeprom
 */
 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
 {
     if (!ftdi || !(ftdi->eeprom))
         ftdi_error_return(-1, "No appropriate structure");
+
+    if (!buf || size < ftdi->eeprom->size)
+        ftdi_error_return(-1, "Not enough room to store eeprom");
+
+    // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
+    if (size > FTDI_MAX_EEPROM_SIZE)
+        size = FTDI_MAX_EEPROM_SIZE;
+
     memcpy(buf, ftdi->eeprom->buf, size);
+
     return 0;
 }