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)
{
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");
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 */
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 */
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;
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;
}
/** */