ipdbg: fix double free of virtual-ir data
[openocd.git] / src / jtag / drivers / ft232r.c
index c9ed304a876b4d588c14ea100768b2366aa8a912..766f6ddb5d84f76e1eeb9d84fc1c43926021c2b1 100644 (file)
@@ -1,19 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2010 Serge Vakulenko                                    *
  *   serge@vak.ru                                                          *
- *                                                                         *
- *   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, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -26,6 +15,7 @@
 #endif
 
 /* project specific includes */
+#include <jtag/adapter.h>
 #include <jtag/interface.h>
 #include <jtag/commands.h>
 #include <helper/time_support.h>
@@ -68,7 +58,6 @@
 
 #define FT232R_BUF_SIZE_EXTRA  4096
 
-static char *ft232r_serial_desc;
 static uint16_t ft232r_vid = 0x0403; /* FTDI */
 static uint16_t ft232r_pid = 0x6001; /* FT232R */
 static struct libusb_device_handle *adapter;
@@ -170,13 +159,13 @@ static int ft232r_send_recv(void)
        return ERROR_OK;
 }
 
-void ft232r_increase_buf_size(size_t new_buf_size)
+static void ft232r_increase_buf_size(size_t new_buf_size)
 {
        uint8_t *new_buf_ptr;
        if (new_buf_size >= ft232r_buf_size) {
                new_buf_size += FT232R_BUF_SIZE_EXTRA;
                new_buf_ptr = realloc(ft232r_output, new_buf_size);
-               if (new_buf_ptr != NULL) {
+               if (new_buf_ptr) {
                        ft232r_output = new_buf_ptr;
                        ft232r_buf_size = new_buf_size;
                }
@@ -246,7 +235,7 @@ static int ft232r_speed(int divisor)
 
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_SET_BAUD_RATE, divisor, 0, 0, 0, 1000) != 0) {
+               SIO_SET_BAUD_RATE, divisor, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
                LOG_ERROR("cannot set baud rate");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -257,9 +246,10 @@ static int ft232r_init(void)
 {
        uint16_t avids[] = {ft232r_vid, 0};
        uint16_t apids[] = {ft232r_pid, 0};
-       if (jtag_libusb_open(avids, apids, ft232r_serial_desc, &adapter, NULL)) {
+       if (jtag_libusb_open(avids, apids, NULL, &adapter, NULL)) {
+               const char *ft232r_serial_desc = adapter_get_required_serial();
                LOG_ERROR("ft232r not found: vid=%04x, pid=%04x, serial=%s\n",
-                       ft232r_vid, ft232r_pid, (ft232r_serial_desc == NULL) ? "[any]" : ft232r_serial_desc);
+                       ft232r_vid, ft232r_pid, (!ft232r_serial_desc) ? "[any]" : ft232r_serial_desc);
                return ERROR_JTAG_INIT_FAILED;
        }
 
@@ -276,7 +266,7 @@ static int ft232r_init(void)
        /* Reset the device. */
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_RESET, 0, 0, 0, 0, 1000) != 0) {
+               SIO_RESET, 0, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
                LOG_ERROR("unable to reset device");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -285,7 +275,7 @@ static int ft232r_init(void)
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                SIO_SET_BITMODE, (1<<tck_gpio) | (1<<tdi_gpio) | (1<<tms_gpio) | (1<<ntrst_gpio) | (1<<nsysrst_gpio) | 0x400,
-               0, 0, 0, 1000) != 0) {
+               0, NULL, 0, 1000, NULL) != ERROR_OK) {
                LOG_ERROR("cannot set sync bitbang mode");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -298,19 +288,19 @@ static int ft232r_init(void)
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                SIO_SET_BAUD_RATE, divisor,
-               0, 0, 0, 1000) != 0) {
+               0, NULL, 0, 1000, NULL) != ERROR_OK) {
                LOG_ERROR("cannot set baud rate");
                return ERROR_JTAG_INIT_FAILED;
        }
        if (jtag_libusb_control_transfer(adapter,
                LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
-               SIO_SET_LATENCY_TIMER, latency_timer, 0, 0, 0, 1000) != 0) {
+               SIO_SET_LATENCY_TIMER, latency_timer, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
                LOG_ERROR("unable to set latency timer");
                return ERROR_JTAG_INIT_FAILED;
        }
 
        ft232r_output = malloc(ft232r_buf_size);
-       if (ft232r_output == NULL) {
+       if (!ft232r_output) {
                LOG_ERROR("Unable to allocate memory for the buffer");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -325,7 +315,7 @@ static int ft232r_quit(void)
                if (jtag_libusb_control_transfer(adapter,
                        LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
                        SIO_SET_BITMODE, ft232r_restore_bitmode,
-                       0, 0, 0, 1000) != 0) {
+                       0, NULL, 0, 1000, NULL) != ERROR_OK) {
                        LOG_ERROR("cannot set bitmode to restore serial port");
                }
        }
@@ -395,16 +385,6 @@ static int ft232r_bit_name_to_number(const char *name)
        return -1;
 }
 
-COMMAND_HANDLER(ft232r_handle_serial_desc_command)
-{
-       if (CMD_ARGC == 1)
-               ft232r_serial_desc = strdup(CMD_ARGV[0]);
-       else
-               LOG_ERROR("require exactly one argument to "
-                                 "ft232r_serial_desc <serial>");
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(ft232r_handle_vid_pid_command)
 {
        if (CMD_ARGC > 2) {
@@ -560,72 +540,65 @@ COMMAND_HANDLER(ft232r_handle_restore_serial_command)
        return ERROR_OK;
 }
 
-static const struct command_registration ft232r_command_handlers[] = {
+static const struct command_registration ft232r_subcommand_handlers[] = {
        {
-               .name = "ft232r_serial_desc",
-               .handler = ft232r_handle_serial_desc_command,
-               .mode = COMMAND_CONFIG,
-               .help = "USB serial descriptor of the adapter",
-               .usage = "serial string",
-       },
-       {
-               .name = "ft232r_vid_pid",
+               .name = "vid_pid",
                .handler = ft232r_handle_vid_pid_command,
                .mode = COMMAND_CONFIG,
                .help = "USB VID and PID of the adapter",
                .usage = "vid pid",
        },
        {
-               .name = "ft232r_jtag_nums",
+               .name = "jtag_nums",
                .handler = ft232r_handle_jtag_nums_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
                .usage = "<0-7|TXD-RI> <0-7|TXD-RI> <0-7|TXD-RI> <0-7|TXD-RI>",
        },
        {
-               .name = "ft232r_tck_num",
+               .name = "tck_num",
                .handler = ft232r_handle_tck_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for tck.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_tms_num",
+               .name = "tms_num",
                .handler = ft232r_handle_tms_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for tms.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_tdo_num",
+               .name = "tdo_num",
                .handler = ft232r_handle_tdo_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for tdo.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_tdi_num",
+               .name = "tdi_num",
                .handler = ft232r_handle_tdi_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for tdi.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_srst_num",
+               .name = "srst_num",
                .handler = ft232r_handle_srst_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for srst.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_trst_num",
+               .name = "trst_num",
                .handler = ft232r_handle_trst_num_command,
                .mode = COMMAND_CONFIG,
                .help = "gpio number for trst.",
                .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
        },
        {
-               .name = "ft232r_restore_serial",
+               .name = "restore_serial",
                .handler = ft232r_handle_restore_serial_command,
                .mode = COMMAND_CONFIG,
                .help = "bitmode control word that restores serial port.",
@@ -634,6 +607,17 @@ static const struct command_registration ft232r_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
+static const struct command_registration ft232r_command_handlers[] = {
+       {
+               .name = "ft232r",
+               .mode = COMMAND_ANY,
+               .help = "perform ft232r management",
+               .chain = ft232r_subcommand_handlers,
+               .usage = "",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+
 /*
  * Synchronous bitbang protocol implementation.
  */
@@ -819,9 +803,9 @@ static void syncbb_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int
                }
 }
 
-static int syncbb_execute_queue(void)
+static int syncbb_execute_queue(struct jtag_command *cmd_queue)
 {
-       struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
+       struct jtag_command *cmd = cmd_queue; /* currently processed command */
        int scan_size;
        enum scan_type type;
        uint8_t *buffer;
@@ -886,12 +870,11 @@ static int syncbb_execute_queue(void)
                                syncbb_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
                                if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
                                        retval = ERROR_JTAG_QUEUE_FAILED;
-                               if (buffer)
-                                       free(buffer);
+                               free(buffer);
                                break;
 
                        case JTAG_SLEEP:
-                               LOG_DEBUG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
+                               LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
 
                                jtag_sleep(cmd->cmd.sleep->us);
                                break;

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)