openocd: fix SPDX tag format for files .c
[fw/openocd] / src / jtag / drivers / usb_blaster / ublast2_access_libusb.c
index ac863918cb724e3f821ef8ea52b13629b4375216..7f9781869c1de4a4d51a5ebe6ca24bb5e2d98260 100644 (file)
@@ -1,18 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /*
  *   Driver for USB-JTAG, Altera USB-Blaster II and compatibles
  *
  *   Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
  *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -20,7 +12,8 @@
 #endif
 #include <jtag/interface.h>
 #include <jtag/commands.h>
-#include <libusb_common.h>
+#include "helper/system.h"
+#include <libusb_helper.h>
 #include <target/image.h>
 
 #include "ublast_access.h"
 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
                              unsigned size, uint32_t *bytes_read)
 {
-       *bytes_read = jtag_libusb_bulk_read(low->libusb_dev,
-                                           USBBLASTER_EPIN | \
+       int ret, tmp = 0;
+
+       ret = jtag_libusb_bulk_read(low->libusb_dev,
+                                           USBBLASTER_EPIN |
                                            LIBUSB_ENDPOINT_IN,
                                            (char *)buf,
                                            size,
-                                           100);
-       return ERROR_OK;
+                                           100, &tmp);
+       *bytes_read = tmp;
+
+       return ret;
 }
 
 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
                               int size, uint32_t *bytes_written)
 {
-       *bytes_written = jtag_libusb_bulk_write(low->libusb_dev,
-                                               USBBLASTER_EPOUT | \
+       int ret, tmp = 0;
+
+       ret = jtag_libusb_bulk_write(low->libusb_dev,
+                                               USBBLASTER_EPOUT |
                                                LIBUSB_ENDPOINT_OUT,
                                                (char *)buf,
                                                size,
-                                               100);
-       return ERROR_OK;
+                                               100, &tmp);
+       *bytes_written = tmp;
+
+       return ret;
+
 }
 
-static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libusb_dev,
+static int ublast2_write_firmware_section(struct libusb_device_handle *libusb_dev,
                                   struct image *firmware_image, int section_index)
 {
        uint16_t chunk_size;
@@ -94,7 +96,7 @@ static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libu
                        chunk_size = bytes_remaining;
 
                jtag_libusb_control_transfer(libusb_dev,
-                                            LIBUSB_REQUEST_TYPE_VENDOR | \
+                                            LIBUSB_REQUEST_TYPE_VENDOR |
                                             LIBUSB_ENDPOINT_OUT,
                                             USBBLASTER_CTRL_LOAD_FIRM,
                                             addr,
@@ -111,7 +113,7 @@ static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libu
        return ERROR_OK;
 }
 
-static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_dev,
+static int load_usb_blaster_firmware(struct libusb_device_handle *libusb_dev,
                                     struct ublast_lowlevel *low)
 {
        struct image ublast2_firmware_image;
@@ -121,13 +123,18 @@ static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_de
                return ERROR_FAIL;
        }
 
+       if (libusb_claim_interface(libusb_dev, 0)) {
+               LOG_ERROR("unable to claim interface");
+               return ERROR_JTAG_INIT_FAILED;
+       }
+
        ublast2_firmware_image.base_address = 0;
-       ublast2_firmware_image.base_address_set = 0;
+       ublast2_firmware_image.base_address_set = false;
 
        int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
        if (ret != ERROR_OK) {
                LOG_ERROR("Could not load firmware image");
-               return ret;
+               goto error_release_usb;
        }
 
        /** A host loader program must write 0x01 to the CPUCS register
@@ -140,7 +147,7 @@ static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_de
 
        char value = CPU_RESET;
        jtag_libusb_control_transfer(libusb_dev,
-                                    LIBUSB_REQUEST_TYPE_VENDOR | \
+                                    LIBUSB_REQUEST_TYPE_VENDOR |
                                     LIBUSB_ENDPOINT_OUT,
                                     USBBLASTER_CTRL_LOAD_FIRM,
                                     EZUSB_CPUCS,
@@ -150,18 +157,18 @@ static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_de
                                     100);
 
        /* Download all sections in the image to ULINK */
-       for (int i = 0; i < ublast2_firmware_image.num_sections; i++) {
+       for (unsigned int i = 0; i < ublast2_firmware_image.num_sections; i++) {
                ret = ublast2_write_firmware_section(libusb_dev,
                                                     &ublast2_firmware_image, i);
                if (ret != ERROR_OK) {
                        LOG_ERROR("Error while downloading the firmware");
-                       return ret;
+                       goto error_close_firmware;
                }
        }
 
        value = !CPU_RESET;
        jtag_libusb_control_transfer(libusb_dev,
-                                    LIBUSB_REQUEST_TYPE_VENDOR | \
+                                    LIBUSB_REQUEST_TYPE_VENDOR |
                                     LIBUSB_ENDPOINT_OUT,
                                     USBBLASTER_CTRL_LOAD_FIRM,
                                     EZUSB_CPUCS,
@@ -170,20 +177,29 @@ static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_de
                                     1,
                                     100);
 
+error_close_firmware:
        image_close(&ublast2_firmware_image);
 
-       return ERROR_OK;
+error_release_usb:
+       /*
+        * Release claimed interface. Most probably it is already disconnected
+        * and re-enumerated as new devices after firmware upload, so we do
+        * not need to care about errors.
+        */
+       libusb_release_interface(libusb_dev, 0);
+
+       return ret;
 }
 
 static int ublast2_libusb_init(struct ublast_lowlevel *low)
 {
        const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
        const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
-       struct jtag_libusb_device_handle *temp;
+       struct libusb_device_handle *temp;
        bool renumeration = false;
        int ret;
 
-       if (jtag_libusb_open(vids, pids, NULL, &temp) == ERROR_OK) {
+       if (jtag_libusb_open(vids, pids, &temp, NULL) == ERROR_OK) {
                LOG_INFO("Altera USB-Blaster II (uninitialized) found");
                LOG_INFO("Loading firmware...");
                ret = load_usb_blaster_firmware(temp, low);
@@ -197,15 +213,15 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
        const uint16_t pids_renum[] = { low->ublast_pid, 0 };
 
        if (renumeration == false) {
-               if (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev) != ERROR_OK) {
+               if (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK) {
                        LOG_ERROR("Altera USB-Blaster II not found");
                        return ERROR_FAIL;
                }
        } else {
                int retry = 10;
-               while (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev) != ERROR_OK && retry--) {
+               while (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK && retry--) {
                        usleep(1000000);
-                       LOG_INFO("Waiting for renumerate...");
+                       LOG_INFO("Waiting for reenumerate...");
                }
 
                if (!retry) {
@@ -214,9 +230,15 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
                }
        }
 
+       if (libusb_claim_interface(low->libusb_dev, 0)) {
+               LOG_ERROR("unable to claim interface");
+               jtag_libusb_close(low->libusb_dev);
+               return ERROR_JTAG_INIT_FAILED;
+       }
+
        char buffer[5];
        jtag_libusb_control_transfer(low->libusb_dev,
-                                    LIBUSB_REQUEST_TYPE_VENDOR | \
+                                    LIBUSB_REQUEST_TYPE_VENDOR |
                                     LIBUSB_ENDPOINT_IN,
                                     USBBLASTER_CTRL_READ_REV,
                                     0,
@@ -232,6 +254,9 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
 
 static int ublast2_libusb_quit(struct ublast_lowlevel *low)
 {
+       if (libusb_release_interface(low->libusb_dev, 0))
+               LOG_ERROR("usb release interface failed");
+
        jtag_libusb_close(low->libusb_dev);
        return ERROR_OK;
 };