Fix memory leakage pathe with no V2 device found
[fw/stlink] / src / stlink-usb.c
index a81676936772ff8c0f7865f3f89b0cd5c39d7368..1320831d1ff3b0e1143a5641f375f037eef8f876 100644 (file)
@@ -150,33 +150,6 @@ static inline int send_only
 }
 
 
-/* Search for a STLINK device, either any or teh one with the given PID
- * Return the protocoll version
- */
-static int is_stlink_device(libusb_device * dev, uint16_t pid) {
-    struct libusb_device_descriptor desc;
-    int version;
-
-    if (libusb_get_device_descriptor(dev, &desc))
-        return 0;
-
-    if (desc.idVendor != USB_ST_VID)
-        return 0;
-
-    if ((desc.idProduct != USB_STLINK_32L_PID) && 
-        (desc.idProduct != USB_STLINK_PID ))
-        return 0;
-
-    if(pid && (pid != desc.idProduct))
-        return 0;
-    if (desc.idProduct == USB_STLINK_PID )
-        version = 1;
-    else
-        version = 2;
-
-    return version;
-}
-
 static int fill_command
 (stlink_t * sl, enum SCSI_Generic_Direction dir, uint32_t len) {
     struct stlink_libusb * const slu = sl->backend_data;
@@ -580,11 +553,7 @@ stlink_t* stlink_open_usb(const int verbose) {
     struct stlink_libusb* slu = NULL;
     int error = -1;
     libusb_device** devs = NULL;
-    libusb_device* dev;
-    ssize_t i;
-    ssize_t count;
     int config;
-    char *iSerial = NULL;
 
     sl = malloc(sizeof (stlink_t));
     slu = malloc(sizeof (struct stlink_libusb));
@@ -599,19 +568,6 @@ stlink_t* stlink_open_usb(const int verbose) {
     
     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
 
-    /* flash memory settings */
-    sl->flash_base = STM32_FLASH_BASE;
-    sl->flash_size = STM32_FLASH_SIZE;
-    sl->flash_pgsz = STM32_FLASH_PGSZ;
-
-    /* system memory */
-    sl->sys_base = STM32_SYSTEM_BASE;
-    sl->sys_size = STM32_SYSTEM_SIZE;
-
-    /* sram memory settings */
-    sl->sram_base = STM32_SRAM_BASE;
-    sl->sram_size = STM32L_SRAM_SIZE;
-
     if (libusb_init(&(slu->libusb_ctx))) {
         WLOG("failed to init libusb context, wrong version of libraries?\n");
         goto on_error;
@@ -620,9 +576,8 @@ stlink_t* stlink_open_usb(const int verbose) {
     slu->usb_handle = libusb_open_device_with_vid_pid(slu->libusb_ctx, USB_ST_VID, USB_STLINK_32L_PID);
     if (slu->usb_handle == NULL) {
                // TODO - free usb context too...
-        free(slu);
                WLOG("Couldn't find any ST-Link/V2 devices");
-        return NULL;
+        goto on_error;
     }
     
     if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
@@ -675,11 +630,19 @@ stlink_t* stlink_open_usb(const int verbose) {
     slu->cmd_len = (slu->protocoll == 1)? STLINK_SG_SIZE: STLINK_CMD_SIZE;
 
     /* success */
+
     if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
       ILOG("-- exit_dfu_mode\n");
       stlink_exit_dfu_mode(sl);
     }
+
+    if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
+      stlink_enter_swd_mode(sl);
+    }
+
     stlink_version(sl);
+    stlink_load_device_params(sl);
+
     error = 0;
 
 on_libusb_error:
@@ -696,6 +659,8 @@ on_libusb_error:
     return sl;
 
 on_error:
+    if( slu->libusb_ctx)
+       libusb_exit(slu->libusb_ctx);
     if (sl != NULL) free(sl);
     if (slu != NULL) free(slu);
     return 0;