jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / jtag / drivers / arm-jtag-ew.c
index 35b6b929f18420343eb820578ce6ce6a18beb129..4c50c54c9bd4aa01b59f5197965ff2a3449e8254 100644 (file)
@@ -1,21 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com>            *
  *   based on Dominic Rath's and Benedikt Sauter's usbprog.c               *
- *                                                                         *
- *   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.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -24,8 +11,8 @@
 
 #include <jtag/interface.h>
 #include <jtag/commands.h>
-#include <usb.h>
-#include "usb_common.h"
+#include "helper/system.h"
+#include "libusb_helper.h"
 
 #define USB_VID                                                0x15ba
 #define USB_PID                                                0x001e
@@ -77,7 +64,7 @@ static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_c
 
 /* ARM-JTAG-EW lowlevel functions */
 struct armjtagew {
-       struct usb_dev_handle *usb_handle;
+       struct libusb_device_handle *usb_handle;
 };
 
 static struct armjtagew *armjtagew_usb_open(void);
@@ -98,18 +85,18 @@ static struct armjtagew *armjtagew_handle;
 /**************************************************************************
  * External interface implementation */
 
-static int armjtagew_execute_queue(void)
+static int armjtagew_execute_queue(struct jtag_command *cmd_queue)
 {
-       struct jtag_command *cmd = jtag_command_queue;
+       struct jtag_command *cmd = cmd_queue;
        int scan_size;
        enum scan_type type;
        uint8_t *buffer;
 
-       while (cmd != NULL) {
+       while (cmd) {
                switch (cmd->type) {
                        case JTAG_RUNTEST:
-                               DEBUG_JTAG_IO("runtest %i cycles, end in %i",
-                                               cmd->cmd.runtest->num_cycles, \
+                               LOG_DEBUG_IO("runtest %i cycles, end in %i",
+                                               cmd->cmd.runtest->num_cycles,
                                                cmd->cmd.runtest->end_state);
 
                                armjtagew_end_state(cmd->cmd.runtest->end_state);
@@ -117,15 +104,15 @@ static int armjtagew_execute_queue(void)
                                break;
 
                        case JTAG_TLR_RESET:
-                               DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
+                               LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
 
                                armjtagew_end_state(cmd->cmd.statemove->end_state);
                                armjtagew_state_move();
                                break;
 
                        case JTAG_PATHMOVE:
-                               DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
-                                               cmd->cmd.pathmove->num_states, \
+                               LOG_DEBUG_IO("pathmove: %i states, end in %i",
+                                               cmd->cmd.pathmove->num_states,
                                                cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
 
                                armjtagew_path_move(cmd->cmd.pathmove->num_states,
@@ -133,12 +120,12 @@ static int armjtagew_execute_queue(void)
                                break;
 
                        case JTAG_SCAN:
-                               DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
+                               LOG_DEBUG_IO("scan end in %i", cmd->cmd.scan->end_state);
 
                                armjtagew_end_state(cmd->cmd.scan->end_state);
 
                                scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
-                               DEBUG_JTAG_IO("scan input, length = %d", scan_size);
+                               LOG_DEBUG_IO("scan input, length = %d", scan_size);
 
 #ifdef _DEBUG_USB_COMMS_
                                armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
@@ -150,7 +137,7 @@ static int armjtagew_execute_queue(void)
                                break;
 
                        case JTAG_RESET:
-                               DEBUG_JTAG_IO("reset trst: %i srst %i",
+                               LOG_DEBUG_IO("reset trst: %i srst %i",
                                                cmd->cmd.reset->trst,
                                                cmd->cmd.reset->srst);
 
@@ -162,7 +149,7 @@ static int armjtagew_execute_queue(void)
                                break;
 
                        case JTAG_SLEEP:
-                               DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
+                               LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
                                armjtagew_tap_execute();
                                jtag_sleep(cmd->cmd.sleep->us);
                                break;
@@ -226,7 +213,7 @@ static int armjtagew_init(void)
 
        armjtagew_handle = armjtagew_usb_open();
 
-       if (armjtagew_handle == 0) {
+       if (!armjtagew_handle) {
                LOG_ERROR(
                        "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
@@ -355,7 +342,10 @@ static void armjtagew_scan(bool ir_scan,
        /* Move to appropriate scan state */
        armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
 
-       armjtagew_state_move();
+       /* Only move if we're not already there */
+       if (tap_get_state() != tap_get_end_state())
+               armjtagew_state_move();
+
        armjtagew_end_state(saved_end_state);
 
        /* Scan */
@@ -458,10 +448,10 @@ static int armjtagew_get_version_info(void)
        auxinfo[256] = '\0';
 
        LOG_INFO(
-               "ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
+               "ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s",
                usb_in_buffer[1],
-               usb_in_buffer[0], \
-               isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
+               usb_in_buffer[0],
+               isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X',
                sn,
                auxinfo);
 
@@ -489,20 +479,27 @@ static const struct command_registration armjtagew_command_handlers[] = {
                .handler = &armjtagew_handle_armjtagew_info_command,
                .mode = COMMAND_EXEC,
                .help = "query armjtagew info",
+               .usage = "",
        },
        COMMAND_REGISTRATION_DONE
 };
 
-struct jtag_interface armjtagew_interface = {
+static struct jtag_interface armjtagew_interface = {
+       .execute_queue = armjtagew_execute_queue,
+};
+
+struct adapter_driver armjtagew_adapter_driver = {
        .name = "arm-jtag-ew",
-       .commands = armjtagew_command_handlers,
        .transports = jtag_only,
-       .execute_queue = armjtagew_execute_queue,
-       .speed = armjtagew_speed,
-       .speed_div = armjtagew_speed_div,
-       .khz = armjtagew_khz,
+       .commands = armjtagew_command_handlers,
+
        .init = armjtagew_init,
        .quit = armjtagew_quit,
+       .speed = armjtagew_speed,
+       .khz = armjtagew_khz,
+       .speed_div = armjtagew_speed_div,
+
+       .jtag_ops = &armjtagew_interface,
 };
 
 /**************************************************************************
@@ -645,7 +642,7 @@ static int armjtagew_tap_execute(void)
                                /* Copy to buffer */
                                buf_set_buf(tdo_buffer, first, buffer, 0, length);
 
-                               DEBUG_JTAG_IO("pending scan result, length = %d", length);
+                               LOG_DEBUG_IO("pending scan result, length = %d", length);
 
 #ifdef _DEBUG_USB_COMMS_
                                armjtagew_debug_buffer(buffer, byte_length);
@@ -656,8 +653,7 @@ static int armjtagew_tap_execute(void)
                                        return ERROR_JTAG_QUEUE_FAILED;
                                }
 
-                               if (pending_scan_result->buffer != NULL)
-                                       free(pending_scan_result->buffer);
+                               free(pending_scan_result->buffer);
                        }
                } else {
                        LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d",
@@ -675,37 +671,39 @@ static int armjtagew_tap_execute(void)
 /****************************************************************************
  * JLink USB low-level functions */
 
-static struct armjtagew *armjtagew_usb_open()
+static struct armjtagew *armjtagew_usb_open(void)
 {
-       usb_init();
-
        const uint16_t vids[] = { USB_VID, 0 };
        const uint16_t pids[] = { USB_PID, 0 };
-       struct usb_dev_handle *dev;
-       if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
+       struct libusb_device_handle *dev;
+
+       if (jtag_libusb_open(vids, pids, NULL, &dev, NULL) != ERROR_OK)
                return NULL;
 
        struct armjtagew *result = malloc(sizeof(struct armjtagew));
        result->usb_handle = dev;
 
 #if 0
-       /* usb_set_configuration required under win32 */
-       usb_set_configuration(dev, dev->config[0].bConfigurationValue);
+       /* libusb_set_configuration required under win32 */
+       struct libusb_config_descriptor *config;
+       struct libusb_device *usb_dev = libusb_get_device(dev);
+       libusb_get_config_descriptor(usb_dev, 0, &config);
+       libusb_set_configuration(dev, config->bConfigurationValue);
 #endif
-       usb_claim_interface(dev, 0);
+       libusb_claim_interface(dev, 0);
 #if 0
        /*
         * This makes problems under Mac OS X. And is not needed
         * under Windows. Hopefully this will not break a linux build
         */
-       usb_set_altinterface(dev, 0);
+       libusb_set_interface_alt_setting(dev, 0, 0);
 #endif
        return result;
 }
 
 static void armjtagew_usb_close(struct armjtagew *armjtagew)
 {
-       usb_close(armjtagew->usb_handle);
+       libusb_close(armjtagew->usb_handle);
        free(armjtagew);
 }
 
@@ -718,13 +716,13 @@ static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, in
        if (result == out_length) {
                result = armjtagew_usb_read(armjtagew, in_length);
                if (result != in_length) {
-                       LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
+                       LOG_ERROR("jtag_libusb_bulk_read failed (requested=%d, result=%d)",
                                in_length,
                                result);
                        return -1;
                }
        } else {
-               LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
+               LOG_ERROR("jtag_libusb_bulk_write failed (requested=%d, result=%d)", out_length, result);
                return -1;
        }
        return 0;
@@ -734,6 +732,7 @@ static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, in
 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
 {
        int result;
+       int transferred;
 
        if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE) {
                LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)",
@@ -742,29 +741,34 @@ static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
                return -1;
        }
 
-       result = usb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
-                       (char *)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
+       result = jtag_libusb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT,
+                       (char *)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT, &transferred);
 
-       DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
+       LOG_DEBUG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
 
 #ifdef _DEBUG_USB_COMMS_
        armjtagew_debug_buffer(usb_out_buffer, out_length);
 #endif
-       return result;
+       if (result != ERROR_OK)
+               return -1;
+       return transferred;
 }
 
 /* Read data from USB into in_buffer. */
 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
 {
-       int result = usb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
-                       (char *)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
+       int transferred;
+       int result = jtag_libusb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN,
+                       (char *)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT, &transferred);
 
-       DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
+       LOG_DEBUG_IO("armjtagew_usb_read, result = %d", result);
 
 #ifdef _DEBUG_USB_COMMS_
        armjtagew_debug_buffer(usb_in_buffer, result);
 #endif
-       return result;
+       if (result != ERROR_OK)
+               return -1;
+       return transferred;
 }
 
 #ifdef _DEBUG_USB_COMMS_
@@ -772,17 +776,12 @@ static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
 
 static void armjtagew_debug_buffer(uint8_t *buffer, int length)
 {
-       char line[81];
-       char s[4];
-       int i;
-       int j;
+       char line[8 + 3 * BYTES_PER_LINE + 1];
 
-       for (i = 0; i < length; i += BYTES_PER_LINE) {
-               snprintf(line, 5, "%04x", i);
-               for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
-                       snprintf(s, 4, " %02x", buffer[j]);
-                       strcat(line, s);
-               }
+       for (int i = 0; i < length; i += BYTES_PER_LINE) {
+               int n = snprintf(line, 9, "%04x", i);
+               for (int j = i; j < i + BYTES_PER_LINE && j < length; j++)
+                       n += snprintf(line + n, 4, " %02x", buffer[j]);
                LOG_DEBUG("%s", line);
 
                /* Prevent GDB timeout (writing to log might take some time) */

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)