openocd: fix SPDX tag format for files .c
[fw/openocd] / src / jtag / drivers / versaloon / versaloon.c
index 8efe44353f3740216b186b59cd045ac978c1609e..48d317436b1c204a253ddd81651c7379fb853643 100644 (file)
@@ -1,29 +1,19 @@
+// 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, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include "versaloon_include.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <libusb.h>
 
-#include "versaloon_include.h"
 #include "versaloon.h"
 #include "versaloon_internal.h"
 #include "usbtoxxx/usbtoxxx.h"
@@ -35,15 +25,15 @@ uint16_t versaloon_buf_size;
 struct versaloon_pending_t versaloon_pending[VERSALOON_MAX_PENDING_NUMBER];
 uint16_t versaloon_pending_idx;
 
-libusb_device_handle *versaloon_usb_device_handle;
+struct libusb_device_handle *versaloon_usb_device_handle;
 static uint32_t versaloon_usb_to = VERSALOON_TIMEOUT;
 
-RESULT versaloon_init(void);
-RESULT versaloon_fini(void);
-RESULT versaloon_get_target_voltage(uint16_t *voltage);
-RESULT versaloon_set_target_voltage(uint16_t voltage);
-RESULT versaloon_delay_ms(uint16_t ms);
-RESULT versaloon_delay_us(uint16_t us);
+static RESULT versaloon_init(void);
+static RESULT versaloon_fini(void);
+static RESULT versaloon_get_target_voltage(uint16_t *voltage);
+static RESULT versaloon_set_target_voltage(uint16_t voltage);
+static RESULT versaloon_delay_ms(uint16_t ms);
+static RESULT versaloon_delay_us(uint16_t us);
 
 struct versaloon_interface_t versaloon_interface = {
        .init                           = versaloon_init,
@@ -86,7 +76,6 @@ struct versaloon_interface_t versaloon_interface = {
                .ep_out                 = VERSALOON_OUTP,
                .ep_in                  = VERSALOON_INP,
                .interface              = VERSALOON_IFACE,
-               .serialstring   = NULL,
                .buf_size               = 256,
        }
 };
@@ -116,16 +105,16 @@ void versaloon_free_want_pos(void)
        struct versaloon_want_pos_t *tmp, *free_tmp;
 
        tmp = versaloon_want_pos;
-       while (tmp != NULL) {
+       while (tmp) {
                free_tmp = tmp;
                tmp = tmp->next;
                free(free_tmp);
        }
        versaloon_want_pos = NULL;
 
-       for (i = 0; i < dimof(versaloon_pending); i++) {
+       for (i = 0; i < ARRAY_SIZE(versaloon_pending); i++) {
                tmp = versaloon_pending[i].pos;
-               while (tmp != NULL) {
+               while (tmp) {
                        free_tmp = tmp;
                        tmp = tmp->next;
                        free(free_tmp);
@@ -139,7 +128,7 @@ RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff)
        struct versaloon_want_pos_t *new_pos = NULL;
 
        new_pos = malloc(sizeof(*new_pos));
-       if (NULL == new_pos) {
+       if (!new_pos) {
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
@@ -148,12 +137,12 @@ RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff)
        new_pos->buff = buff;
        new_pos->next = NULL;
 
-       if (NULL == versaloon_want_pos)
+       if (!versaloon_want_pos)
                versaloon_want_pos = new_pos;
        else {
                struct versaloon_want_pos_t *tmp = versaloon_want_pos;
 
-               while (tmp->next != NULL)
+               while (tmp->next)
                        tmp = tmp->next;
                tmp->next = new_pos;
        }
@@ -198,11 +187,11 @@ RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen)
        int transferred;
 
 #if PARAM_CHECK
-       if (NULL == versaloon_buf) {
+       if (!versaloon_buf) {
                LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf));
                return ERRCODE_INVALID_BUFFER;
        }
-       if ((0 == out_len) || (out_len > versaloon_interface.usb_setting.buf_size)) {
+       if ((out_len == 0) || (out_len > versaloon_interface.usb_setting.buf_size)) {
                LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
                return ERRCODE_INVALID_PARAMETER;
        }
@@ -211,17 +200,17 @@ RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen)
        ret = libusb_bulk_transfer(versaloon_usb_device_handle,
                        versaloon_interface.usb_setting.ep_out,
                        versaloon_buf, out_len, &transferred, versaloon_usb_to);
-       if (0 != ret || transferred != out_len) {
+       if (ret != 0 || transferred != out_len) {
                LOG_ERROR(ERRMSG_FAILURE_OPERATION, "send usb data");
                return ERRCODE_FAILURE_OPERATION;
        }
 
-       if (inlen != NULL) {
+       if (inlen) {
                ret = libusb_bulk_transfer(versaloon_usb_device_handle,
                        versaloon_interface.usb_setting.ep_in,
                        versaloon_buf, versaloon_interface.usb_setting.buf_size,
                        &transferred, versaloon_usb_to);
-               if (0 == ret) {
+               if (ret == 0) {
                        *inlen = (uint16_t)transferred;
                        return ERROR_OK;
                } else {
@@ -233,7 +222,7 @@ RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen)
 }
 
 #define VERSALOON_RETRY_CNT 10
-RESULT versaloon_init(void)
+static RESULT versaloon_init(void)
 {
        uint16_t ret = 0;
        uint8_t retry;
@@ -241,7 +230,7 @@ RESULT versaloon_init(void)
 
        /* malloc temporary buffer */
        versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
-       if (NULL == versaloon_buf) {
+       if (!versaloon_buf) {
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
@@ -253,11 +242,11 @@ RESULT versaloon_init(void)
        versaloon_usb_to = 100;
        for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++) {
                versaloon_buf[0] = VERSALOON_GET_INFO;
-               if ((ERROR_OK == versaloon_send_command(1, &ret)) && (ret >= 3))
+               if ((versaloon_send_command(1, &ret) == ERROR_OK) && (ret >= 3))
                        break;
        }
        versaloon_usb_to = timeout_tmp;
-       if (VERSALOON_RETRY_CNT == retry) {
+       if (retry == VERSALOON_RETRY_CNT) {
                versaloon_fini();
                LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon");
                return ERRCODE_FAILURE_OPERATION;
@@ -273,27 +262,27 @@ RESULT versaloon_init(void)
        versaloon_buf = NULL;
 
        versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
-       if (NULL == versaloon_buf) {
+       if (!versaloon_buf) {
                versaloon_fini();
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
        versaloon_cmd_buf = malloc(versaloon_interface.usb_setting.buf_size - 3);
-       if (NULL == versaloon_cmd_buf) {
+       if (!versaloon_cmd_buf) {
                versaloon_fini();
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
-       if (ERROR_OK != usbtoxxx_init()) {
+       if (usbtoxxx_init() != ERROR_OK) {
                LOG_ERROR(ERRMSG_FAILURE_OPERATION, "initialize usbtoxxx");
                return ERROR_FAIL;
        }
        return versaloon_get_target_voltage(&ret);
 }
 
-RESULT versaloon_fini(void)
+static RESULT versaloon_fini(void)
 {
-       if (versaloon_usb_device_handle != NULL) {
+       if (versaloon_usb_device_handle) {
                usbtoxxx_fini();
                versaloon_free_want_pos();
 
@@ -309,7 +298,7 @@ RESULT versaloon_fini(void)
        return ERROR_OK;
 }
 
-RESULT versaloon_set_target_voltage(uint16_t voltage)
+static RESULT versaloon_set_target_voltage(uint16_t voltage)
 {
        usbtopwr_init(0);
        usbtopwr_config(0);
@@ -319,16 +308,16 @@ RESULT versaloon_set_target_voltage(uint16_t voltage)
        return usbtoxxx_execute_command();
 }
 
-RESULT versaloon_get_target_voltage(uint16_t *voltage)
+static RESULT versaloon_get_target_voltage(uint16_t *voltage)
 {
        uint16_t inlen;
 
 #if PARAM_CHECK
-       if (NULL == versaloon_buf) {
+       if (!versaloon_buf) {
                LOG_BUG(ERRMSG_INVALID_BUFFER, TO_STR(versaloon_buf));
                return ERRCODE_INVALID_BUFFER;
        }
-       if (NULL == voltage) {
+       if (!voltage) {
                LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
                return ERRCODE_INVALID_PARAMETER;
        }
@@ -336,7 +325,7 @@ RESULT versaloon_get_target_voltage(uint16_t *voltage)
 
        versaloon_buf[0] = VERSALOON_GET_TVCC;
 
-       if ((ERROR_OK != versaloon_send_command(1, &inlen)) || (inlen != 2)) {
+       if ((versaloon_send_command(1, &inlen) != ERROR_OK) || (inlen != 2)) {
                LOG_ERROR(ERRMSG_FAILURE_OPERATION, "communicate with versaloon");
                return ERRCODE_FAILURE_OPERATION;
        } else {
@@ -345,12 +334,12 @@ RESULT versaloon_get_target_voltage(uint16_t *voltage)
        }
 }
 
-RESULT versaloon_delay_ms(uint16_t ms)
+static RESULT versaloon_delay_ms(uint16_t ms)
 {
        return usbtodelay_delay(ms | 0x8000);
 }
 
-RESULT versaloon_delay_us(uint16_t us)
+static RESULT versaloon_delay_us(uint16_t us)
 {
        return usbtodelay_delay(us & 0x7FFF);
 }