openocd: fix SPDX tag format for files .c
[fw/openocd] / src / jtag / drivers / versaloon / usbtoxxx / usbtoswd.c
index b8d27af9b4320b6d7574e591d0ee82d01e7258c8..42c8023cfce141e64e3e5b9a20b144a01e205f63 100644 (file)
@@ -1,20 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.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.                          *
- *                                                                         *
- *   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.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "usbtoxxx.h"
 #include "usbtoxxx_internal.h"
 
-RESULT usbtoswd_callback(void *p, uint8_t *src, uint8_t *processed)
+static RESULT usbtoswd_read_callback(void *p, uint8_t *src, uint8_t *processed)
+{
+       struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
+
+       if (pending->extra_data)
+               *((uint8_t *)pending->extra_data) = src[0];
+
+       return ERROR_OK;
+}
+
+static RESULT usbtoswd_write_callback(void *p, uint8_t *src, uint8_t *processed)
 {
        struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
 
-       if (pending->extra_data != NULL)
+       if (pending->extra_data)
                *((uint8_t *)pending->extra_data) = src[0];
 
+       /* mark it processed to ignore other input data */
+       *processed = 1;
        return ERROR_OK;
 }
 
@@ -69,7 +68,8 @@ RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry,
        return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5);
 }
 
-RESULT usbtoswd_seqout(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
+RESULT usbtoswd_seqout(uint8_t interface_index, const uint8_t *data,
+       uint16_t bitlen)
 {
        uint16_t bytelen = (bitlen + 7) >> 3;
 
@@ -124,20 +124,22 @@ RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request,
        parity += (request >> 4) & 1;
        parity &= 1;
        buff[0] = (request | 0x81 | (parity << 5)) & ~0x40;
-       if (data != NULL)
+       if (data)
                SET_LE_U32(&buff[1], *data);
        else
                memset(buff + 1, 0, 4);
 
        versaloon_set_extra_data(ack);
-       versaloon_set_callback(usbtoswd_callback);
        if (request & 0x04) {
                /* read */
-               return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
-                       (uint8_t *)data, 1, 4, 0);
+               versaloon_set_callback(usbtoswd_read_callback);
        } else {
                /* write */
-               return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
-                       NULL, 0, 0, 0);
+               versaloon_set_callback(usbtoswd_write_callback);
        }
+
+       /* Input buffer must be passed even for write operations. Otherwise
+        * the callback function is not called and ack value is not set. */
+       return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
+               (uint8_t *)data, 1, 4, 0);
 }