stlink: add generic open error routine
[openocd.git] / src / jtag / drivers / stlink_usb.c
index 2a78f7956a088da176150e9c01f147f066e90c2a..0a16f2c07691b6d1b903b1cb7be4771c2478011a 100644 (file)
@@ -1142,6 +1142,21 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
        return stlink_usb_get_rw_status(handle);
 }
 
+/** */
+static int stlink_usb_close(void *fd)
+{
+       struct stlink_usb_handle_s *h;
+
+       h = (struct stlink_usb_handle_s *)fd;
+
+       if (h->fd)
+               jtag_libusb_close(h->fd);
+
+       free(fd);
+
+       return ERROR_OK;
+}
+
 /** */
 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 {
@@ -1151,7 +1166,7 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        LOG_DEBUG("stlink_usb_open");
 
-       h = malloc(sizeof(struct stlink_usb_handle_s));
+       h = calloc(1, sizeof(struct stlink_usb_handle_s));
 
        if (h == 0) {
                LOG_DEBUG("malloc failed");
@@ -1160,6 +1175,9 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        h->transport = param->transport;
 
+       /* set max read/write buffer size in bytes */
+       param->max_buffer = 512;
+
        const uint16_t vids[] = { param->vid, 0 };
        const uint16_t pids[] = { param->pid, 0 };
 
@@ -1168,14 +1186,14 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
                LOG_ERROR("open failed");
-               return ERROR_FAIL;
+               goto error_open;
        }
 
        jtag_libusb_set_configuration(h->fd, 0);
 
        if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
                LOG_DEBUG("claim interface failed");
-               return ERROR_FAIL;
+               goto error_open;
        }
 
        /* wrap version for first read */
@@ -1193,9 +1211,7 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        if (err != ERROR_OK) {
                LOG_ERROR("read version failed");
-               jtag_libusb_close(h->fd);
-               free(h);
-               return err;
+               goto error_open;
        }
 
        /* compare usb vid/pid */
@@ -1224,9 +1240,7 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        if (err != ERROR_OK) {
                LOG_ERROR("mode (transport) not supported by device");
-               jtag_libusb_close(h->fd);
-               free(h);
-               return err;
+               goto error_open;
        }
 
        api = h->version.jtag_api_max;
@@ -1246,20 +1260,17 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
 
        if (err != ERROR_OK) {
                LOG_ERROR("init mode failed");
-               jtag_libusb_close(h->fd);
-               free(h);
-               return err;
+               goto error_open;
        }
 
        *fd = h;
 
        return ERROR_OK;
-}
 
-/** */
-static int stlink_usb_close(void *fd)
-{
-       return ERROR_OK;
+error_open:
+       stlink_usb_close(h);
+
+       return ERROR_FAIL;
 }
 
 /** */

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)