ipdbg: fix double free of virtual-ir data
[openocd.git] / src / jtag / drivers / usbprog.c
index dd7dcdb4b5628fffea3972feeb95c65eeb0e796a..2d666d07283c42af23292b3e558b30697bdb4745 100644 (file)
@@ -1,19 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2007 by Benedikt Sauter                                 *
  *   sauter@ixbat.de                                                       *
- *                                                                         *
- *   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/>. *
  ***************************************************************************/
 
 /*
@@ -34,7 +23,7 @@
 
 #include <jtag/interface.h>
 #include <jtag/commands.h>
-#include "usb_common.h"
+#include "libusb_helper.h"
 
 #define VID 0x1781
 #define PID 0x0c63
@@ -64,7 +53,7 @@ static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int
 #define WRITE_TMS_CHAIN 0x0A
 
 struct usbprog_jtag {
-       struct usb_dev_handle *usb_handle;
+       struct libusb_device_handle *usb_handle;
 };
 
 static struct usbprog_jtag *usbprog_jtag_handle;
@@ -94,9 +83,9 @@ static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned
 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value);
 /* static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit); */
 
-static int usbprog_execute_queue(void)
+static int usbprog_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;
@@ -137,8 +126,7 @@ static int usbprog_execute_queue(void)
                        usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
                        if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
                                return ERROR_JTAG_QUEUE_FAILED;
-                       if (buffer)
-                               free(buffer);
+                       free(buffer);
                        break;
                case JTAG_SLEEP:
                        LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
@@ -160,7 +148,7 @@ static int usbprog_init(void)
        usbprog_jtag_handle = usbprog_jtag_open();
 
        tms_chain_index = 0;
-       if (usbprog_jtag_handle == 0) {
+       if (!usbprog_jtag_handle) {
                LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
                return ERROR_JTAG_INIT_FAILED;
        }
@@ -347,25 +335,21 @@ static void usbprog_reset(int trst, int srst)
 
 /*************** jtag lowlevel functions ********************/
 
-struct usb_bus *busses;
-
 struct usbprog_jtag *usbprog_jtag_open(void)
 {
-       usb_set_debug(10);
-       usb_init();
-
        const uint16_t vids[] = { VID, 0 };
        const uint16_t pids[] = { 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 usbprog_jtag *tmp = malloc(sizeof(struct usbprog_jtag));
        tmp->usb_handle = dev;
 
-       usb_set_configuration(dev, 1);
-       usb_claim_interface(dev, 0);
-       usb_set_altinterface(dev, 0);
+       libusb_set_configuration(dev, 1);
+       libusb_claim_interface(dev, 0);
+       libusb_set_interface_alt_setting(dev, 0, 0);
 
        return tmp;
 }
@@ -373,21 +357,23 @@ struct usbprog_jtag *usbprog_jtag_open(void)
 #if 0
 static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
 {
-       usb_close(usbprog_jtag->usb_handle);
+       libusb_close(usbprog_jtag->usb_handle);
        free(usbprog_jtag);
 }
 #endif
 
 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
 {
-       int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg, msglen, 100);
+       int transferred;
+
+       int res = jtag_libusb_bulk_write(usbprog_jtag->usb_handle, 3, msg, msglen, 100, &transferred);
        if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) ||
            (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
                return 1;
-       if (res == msglen) {
+       if (res == ERROR_OK && transferred == msglen) {
                /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
-               res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
-               if (res > 0)
+               res = jtag_libusb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100, &transferred);
+               if (res == ERROR_OK && transferred > 0)
                        return (unsigned char)msg[1];
                else
                        return -1;
@@ -429,11 +415,13 @@ static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char
                        bufindex++;
                }
 
-               if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64) {
+               int transferred;
+               int res = jtag_libusb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000, &transferred);
+               if (res == ERROR_OK && transferred == 64) {
                        /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
                        usleep(1);
                        int timeout = 0;
-                       while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1) {
+                       while (jtag_libusb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000, &transferred) != ERROR_OK) {
                                timeout++;
                                if (timeout > 10)
                                        break;
@@ -470,12 +458,13 @@ static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffe
                tmp[1] = (char)(send_bits >> 8);        /* high */
                tmp[2] = (char)(send_bits);                     /* low */
 
-               usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
+               int transferred;
+               jtag_libusb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000, &transferred);
 
                /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
                int timeout = 0;
                usleep(1);
-               while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1) {
+               while (jtag_libusb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10, &transferred) != ERROR_OK) {
                        timeout++;
                        if (timeout > 10)
                                break;
@@ -514,7 +503,8 @@ static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buff
                        tmp[3 + i] = buffer[bufindex];
                        bufindex++;
                }
-               usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
+               int transferred;
+               jtag_libusb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000, &transferred);
        }
 }
 
@@ -583,15 +573,15 @@ static void usbprog_jtag_tms_collect(char tms_scan)
 
 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
 {
-       int i;
        /* LOG_INFO("TMS SEND"); */
        if (tms_chain_index > 0) {
                char tmp[tms_chain_index + 2];
                tmp[0] = WRITE_TMS_CHAIN;
                tmp[1] = (char)(tms_chain_index);
-               for (i = 0; i < tms_chain_index + 1; i++)
+               for (int i = 0; i < tms_chain_index + 1; i++)
                        tmp[2 + i] = tms_chain[i];
-               usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
+               int transferred;
+               jtag_libusb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000, &transferred);
                tms_chain_index = 0;
        }
 }

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)