stlink_core_id: No need to return core_id value
[fw/stlink] / src / stlink-usb.c
index 23ff19176e82fe7c12a0e26a0b3e2bb6b1d82eef..927e44e9681649372c505fb580a330b0544bbde1 100644 (file)
@@ -5,43 +5,19 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <libusb.h>
+#include <errno.h>
+#include <unistd.h>
 
 #include "stlink-common.h"
 #include "stlink-usb.h"
 #include "uglylogging.h"
 
-#define LOG_TAG __FILE__
-#define DLOG(format, args...)         ugly_log(UDEBUG, LOG_TAG, format, ## args)
-#define ILOG(format, args...)         ugly_log(UINFO, LOG_TAG, format, ## args)
-#define WLOG(format, args...)         ugly_log(UWARN, LOG_TAG, format, ## args)
-#define fatal(format, args...)        ugly_log(UFATAL, LOG_TAG, format, ## args)
-
-/* code from bsd timersub.h
-http://www.gnu-darwin.org/www001/src/ports/net/libevnet/work/libevnet-0.3.8/libnostd/bsd/sys/time/timersub.h.html
-*/
-#if !defined timersub
-#define        timersub(a, b, r) do {                                  \
-    (r)->tv_sec        = (a)->tv_sec - (b)->tv_sec;            \
-    (r)->tv_usec       = (a)->tv_usec - (b)->tv_usec;          \
-    if ((r)->tv_usec < 0) {                                    \
-        --(r)->tv_sec;                                 \
-        (r)->tv_usec += 1000000;                       \
-    }                                                  \
-} while (0)
-#endif
-
 enum SCSI_Generic_Direction {SG_DXFER_TO_DEV=0, SG_DXFER_FROM_DEV=0x80};
 
 void _stlink_usb_close(stlink_t* sl) {
     struct stlink_libusb * const handle = sl->backend_data;
     // maybe we couldn't even get the usb device?
     if (handle != NULL) {
-        if (handle->req_trans != NULL)
-            libusb_free_transfer(handle->req_trans);
-
-        if (handle->rep_trans != NULL)
-            libusb_free_transfer(handle->rep_trans);
-
         if (handle->usb_handle != NULL) {
             libusb_close(handle->usb_handle);
         }
@@ -51,111 +27,42 @@ void _stlink_usb_close(stlink_t* sl) {
     }
 }
 
-
-struct trans_ctx {
-#define TRANS_FLAGS_IS_DONE (1 << 0)
-#define TRANS_FLAGS_HAS_ERROR (1 << 1)
-    volatile unsigned long flags;
-};
-
-#ifndef LIBUSB_CALL
-# define LIBUSB_CALL
-#endif
-
-static void LIBUSB_CALL on_trans_done(struct libusb_transfer * trans) {
-    struct trans_ctx * const ctx = trans->user_data;
-
-    if (trans->status != LIBUSB_TRANSFER_COMPLETED)
-        ctx->flags |= TRANS_FLAGS_HAS_ERROR;
-
-    ctx->flags |= TRANS_FLAGS_IS_DONE;
-}
-
-int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) {
-    struct timeval start;
-    struct timeval now;
-    struct timeval diff;
-    struct trans_ctx trans_ctx;
-    enum libusb_error error;
-
-    trans_ctx.flags = 0;
-
-    /* brief intrusion inside the libusb interface */
-    trans->callback = on_trans_done;
-    trans->user_data = &trans_ctx;
-
-    if ((error = libusb_submit_transfer(trans))) {
-        printf("libusb_submit_transfer(%d)\n", error);
-        return -1;
-    }
-
-    gettimeofday(&start, NULL);
-
-    while (trans_ctx.flags == 0) {
-        struct timeval timeout;
-        timeout.tv_sec = 3;
-        timeout.tv_usec = 0;
-        if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) {
-            printf("libusb_handle_events()\n");
-            return -1;
-        }
-
-        gettimeofday(&now, NULL);
-        timersub(&now, &start, &diff);
-        if (diff.tv_sec >= 3) {
-            printf("libusb_handle_events() timeout\n");
-            return -1;
-        }
-    }
-
-    if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) {
-        printf("libusb_handle_events() | has_error\n");
-        return -1;
-    }
-
-    return 0;
-}
-
 ssize_t send_recv(struct stlink_libusb* handle, int terminate,
         unsigned char* txbuf, size_t txsize,
         unsigned char* rxbuf, size_t rxsize) {
     /* note: txbuf and rxbuf can point to the same area */
     int res = 0;
 
-    libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle,
-            handle->ep_req,
-            txbuf, txsize,
-            NULL, NULL,
-            0
-            );
-
-    if (submit_wait(handle, handle->req_trans)) return -1;
+    if (libusb_bulk_transfer(handle->usb_handle, handle->ep_req,
+            txbuf,
+            txsize,
+            &res,
+            3000))
+        return -1;
 
-    /* send_only */
     if (rxsize != 0) {
-
-        /* read the response */
-
-        libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle,
-                handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0);
-
-        if (submit_wait(handle, handle->rep_trans)) return -1;
-        res = handle->rep_trans->actual_length;
+        if (libusb_bulk_transfer(handle->usb_handle, handle->ep_rep,
+                rxbuf,
+                rxsize,
+                &res,
+                3000))
+            return -1;
     }
 
     if ((handle->protocoll == 1) && terminate) {
         /* Read the SG reply */
         unsigned char sg_buf[13];
-        libusb_fill_bulk_transfer
-            (handle->rep_trans, handle->usb_handle,
-             handle->ep_rep, sg_buf, 13, NULL, NULL, 0);
-        res = submit_wait(handle, handle->rep_trans);
+        if (libusb_bulk_transfer(handle->usb_handle, handle->ep_rep,
+                sg_buf,
+                13,
+                &res,
+                3000))
+            return -1;
         /* The STLink doesn't seem to evaluate the sequence number */
         handle->sg_transfer_idx++;
-        if (res ) return -1;
     }
 
-    return handle->rep_trans->actual_length;
+    return res;
 }
 
 static inline int send_only
@@ -231,7 +138,7 @@ int32_t _stlink_usb_target_voltage(stlink_t *sl) {
     return voltage;
 }
 
-uint32_t _stlink_usb_read_debug32(stlink_t *sl, uint32_t addr) {
+void _stlink_usb_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data) {
     struct stlink_libusb * const slu = sl->backend_data;
     unsigned char* const rdata = sl->q_buf;
     unsigned char* const cmd  = sl->c_buf;
@@ -245,9 +152,10 @@ uint32_t _stlink_usb_read_debug32(stlink_t *sl, uint32_t addr) {
     size = send_recv(slu, 1, cmd, slu->cmd_len, rdata, rep_len);
     if (size == -1) {
         printf("[!] send_recv\n");
-        return 0;
+        return;
     }
-    return read_uint32(rdata, 4);
+    *data = read_uint32(rdata, 4);
+    return;
 }
 
 void _stlink_usb_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data) {
@@ -352,6 +260,7 @@ void _stlink_usb_status(stlink_t * sl) {
         printf("[!] send_recv\n");
         return;
     }
+    sl->q_len = (size_t) size;
 }
 
 void _stlink_usb_force_debug(stlink_t *sl) {
@@ -438,7 +347,7 @@ void _stlink_usb_jtag_reset(stlink_t * sl, int value) {
 
     cmd[i++] = STLINK_DEBUG_COMMAND;
     cmd[i++] = STLINK_JTAG_DRIVE_NRST;
-    cmd[i++] = (value)?0:1;
+    cmd[i++] = value;
 
     size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len);
     if (size == -1) {
@@ -733,11 +642,10 @@ stlink_backend_t _stlink_usb_backend = {
 };
 
 
-stlink_t* stlink_open_usb(const int verbose, int reset) {
+stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
     stlink_t* sl = NULL;
     struct stlink_libusb* slu = NULL;
     int error = -1;
-    libusb_device** devs = NULL;
     int config;
 
     sl = malloc(sizeof (stlink_t));
@@ -752,7 +660,6 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
     sl->backend_data = slu;
 
     sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
-
     if (libusb_init(&(slu->libusb_ctx))) {
         WLOG("failed to init libusb context, wrong version of libraries?\n");
         goto on_error;
@@ -776,13 +683,27 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
         devAddr=atoi(c);
         ILOG("bus %03d dev %03d\n",devBus, devAddr);
     }
-    while (cnt){
-        cnt--;
+    while (cnt--){
         libusb_get_device_descriptor( list[cnt], &desc );
         if (desc.idVendor!=USB_ST_VID) continue;
         if (devBus && devAddr)
             if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) continue;
-        if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) break;
+        if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ){
+            if ((p_usb_iserial != NULL)){
+                unsigned char buffer[13];
+                struct libusb_device_handle* handle;
+                libusb_open(list[cnt], &handle);
+                libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buffer, 13);
+                libusb_close(handle);
+                if (memcmp(p_usb_iserial,&buffer,12) == 0){
+                    break;
+                }else{
+                    continue;
+                }
+            }else{
+                break;
+            }
+        }
         if (desc.idProduct == USB_STLINK_PID) {
             slu->protocoll = 1;
             break;
@@ -795,7 +716,8 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
     } else {
         int error = libusb_open(list[cnt], &slu->usb_handle);
         if( error !=0 ) {
-            WLOG("Error %d opening ST-Link/V2 device %03d:%03d\n", error, libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
+            WLOG("Error %d (%s) opening ST-Link/V2 device %03d:%03d\n", 
+               error, strerror (errno), libusb_get_bus_number(list[cnt]), libusb_get_device_address(list[cnt]));
             goto on_error;
         }
     }
@@ -833,18 +755,6 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
         goto on_libusb_error;
     }
 
-    slu->req_trans = libusb_alloc_transfer(0);
-    if (slu->req_trans == NULL) {
-        WLOG("libusb_alloc_transfer failed\n");
-        goto on_libusb_error;
-    }
-
-    slu->rep_trans = libusb_alloc_transfer(0);
-    if (slu->rep_trans == NULL) {
-        WLOG("libusb_alloc_transfer failed\n");
-        goto on_libusb_error;
-    }
-
     // TODO - could use the scanning techniq from stm8 code here...
     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
     if (desc.idProduct == USB_STLINK_NUCLEO_PID) {
@@ -870,17 +780,12 @@ stlink_t* stlink_open_usb(const int verbose, int reset) {
 
     if (reset) {
         stlink_reset(sl);
+        usleep(10000);
     }
-    stlink_load_device_params(sl);
     stlink_version(sl);
-
-    error = 0;
+    error = stlink_load_device_params(sl);
 
 on_libusb_error:
-    if (devs != NULL) {
-        libusb_free_device_list(devs, 1);
-    }
-
     if (error == -1) {
         stlink_close(sl);
         return NULL;
@@ -894,6 +799,6 @@ on_error:
         libusb_exit(slu->libusb_ctx);
     if (sl != NULL) free(sl);
     if (slu != NULL) free(slu);
-    return 0;
+    return NULL;
 }