Print undecode chip_id too
[fw/stlink] / src / stlink-sg.c
index b6ac4963bf7cf8b3ec1c3d8a19f6a9ab1894cc7b..394417b1819f4e6075b7f5060cd8021fab128008 100644 (file)
@@ -111,6 +111,8 @@ static void clear_cdb(struct stlink_libsg *sl) {
  */void _stlink_sg_close(stlink_t *sl) {
     if (sl) {
         struct stlink_libsg *slsg = sl->backend_data;
+        libusb_close(slsg->usb_handle);
+        libusb_exit(slsg->libusb_ctx);
         free(slsg);
     }
 }
@@ -141,7 +143,7 @@ static int get_usb_mass_storage_status(libusb_device_handle *handle, uint8_t end
 
     uint32_t rsig = read_uint32(csw, 0);
     uint32_t rtag = read_uint32(csw, 4);
-    uint32_t residue = read_uint32(csw, 8);
+    /* uint32_t residue = read_uint32(csw, 8); */
 #define USB_CSW_SIGNATURE 0x53425355  // 'U' 'S' 'B' 'S' (reversed)
     if (rsig != USB_CSW_SIGNATURE) {
         WLOG("status signature was invalid: %#x\n", rsig);
@@ -297,7 +299,7 @@ int send_usb_data_only(libusb_device_handle *handle, unsigned char endpoint_out,
     unsigned char endpoint_in, unsigned char *cbuf, unsigned int length) {
     int ret;
     int real_transferred;
-    int try;
+    int try = 0;
     do {
         ret = libusb_bulk_transfer(handle, endpoint_out, cbuf, length,
                                    &real_transferred, SG_TIMEOUT_MSEC);
@@ -855,6 +857,7 @@ static stlink_t* stlink_open(const int verbose) {
     if (slsg->usb_handle == NULL) {
         WLOG("Failed to find an stlink v1 by VID:PID\n");
         libusb_close(slsg->usb_handle);
+        libusb_exit(slsg->libusb_ctx);
         free(sl);
         free(slsg);
         return NULL;
@@ -869,6 +872,7 @@ static stlink_t* stlink_open(const int verbose) {
         if (r < 0) {
             WLOG("libusb_detach_kernel_driver(() error %s\n", strerror(-r));
             libusb_close(slsg->usb_handle);
+            libusb_exit(slsg->libusb_ctx);
             free(sl);
             free(slsg);
             return NULL;
@@ -881,6 +885,7 @@ static stlink_t* stlink_open(const int verbose) {
         /* this may fail for a previous configured device */
         WLOG("libusb_get_configuration()\n");
         libusb_close(slsg->usb_handle);
+        libusb_exit(slsg->libusb_ctx);
         free(sl);
         free(slsg);
         return NULL;
@@ -895,6 +900,7 @@ static stlink_t* stlink_open(const int verbose) {
             /* this may fail for a previous configured device */
             WLOG("libusb_set_configuration() failed\n");
             libusb_close(slsg->usb_handle);
+            libusb_exit(slsg->libusb_ctx);
             free(sl);
             free(slsg);
             return NULL;
@@ -904,6 +910,7 @@ static stlink_t* stlink_open(const int verbose) {
     if (libusb_claim_interface(slsg->usb_handle, 0)) {
         WLOG("libusb_claim_interface() failed\n");
         libusb_close(slsg->usb_handle);
+        libusb_exit(slsg->libusb_ctx);
         free(sl);
         free(slsg);
         return NULL;