jtag/vsllink: switch to command 'adapter serial'
[fw/openocd] / src / jtag / drivers / versaloon / versaloon.c
index dbb8e4fb03b2c08c7bfd40edcac9c0c44d946434..7c2efefaa0be8504aba9e5b117072265e0b4c5a0 100644 (file)
  *   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.             *
+ *   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"
 
-uint8_t *versaloon_buf = NULL;
-uint8_t *versaloon_cmd_buf = NULL;
+uint8_t *versaloon_buf;
+uint8_t *versaloon_cmd_buf;
 uint16_t versaloon_buf_size;
 
 struct versaloon_pending_t versaloon_pending[VERSALOON_MAX_PENDING_NUMBER];
-uint16_t versaloon_pending_idx = 0;
+uint16_t versaloon_pending_idx;
 
-usb_dev_handle *versaloon_usb_device_handle = NULL;
+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);
-struct versaloon_interface_t versaloon_interface =
-{
-       .init                                   = versaloon_init,
-       .fini                                   = versaloon_fini,
-       {// adaptors
-               {// target_voltage
-                       .get                    = versaloon_get_target_voltage,
-                       .set                    = versaloon_set_target_voltage,
+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,
+       .fini                           = versaloon_fini,
+       {       /* adaptors */
+               {       /* target_voltage */
+                       .get            = versaloon_get_target_voltage,
+                       .set            = versaloon_set_target_voltage,
                },
-               {// gpio
-                       .init                   = usbtogpio_init,
-                       .fini                   = usbtogpio_fini,
-                       .config                 = usbtogpio_config,
-                       .out                    = usbtogpio_out,
-                       .in                             = usbtogpio_in,
+               {       /* gpio */
+                       .init           = usbtogpio_init,
+                       .fini           = usbtogpio_fini,
+                       .config         = usbtogpio_config,
+                       .out            = usbtogpio_out,
+                       .in                     = usbtogpio_in,
                },
-               {// delay
-                       .delayms                = versaloon_delay_ms,
-                       .delayus                = versaloon_delay_us,
+               {       /* delay */
+                       .delayms        = versaloon_delay_ms,
+                       .delayus        = versaloon_delay_us,
                },
-               {// swd
-                       .init                   = usbtoswd_init,
-                       .fini                   = usbtoswd_fini,
-                       .config                 = usbtoswd_config,
-                       .seqout                 = usbtoswd_seqout,
-                       .seqin                  = usbtoswd_seqin,
-                       .transact               = usbtoswd_transact,
+               {       /* swd */
+                       .init           = usbtoswd_init,
+                       .fini           = usbtoswd_fini,
+                       .config         = usbtoswd_config,
+                       .seqout         = usbtoswd_seqout,
+                       .seqin          = usbtoswd_seqin,
+                       .transact       = usbtoswd_transact,
                },
-               {// jtag_raw
-                       .init                   = usbtojtagraw_init,
-                       .fini                   = usbtojtagraw_fini,
-                       .config                 = usbtojtagraw_config,
-                       .execute                = usbtojtagraw_execute,
+               {       /* jtag_raw */
+                       .init           = usbtojtagraw_init,
+                       .fini           = usbtojtagraw_fini,
+                       .config         = usbtojtagraw_config,
+                       .execute        = usbtojtagraw_execute,
                },
-               .peripheral_commit      = usbtoxxx_execute_command,
+               .peripheral_commit = usbtoxxx_execute_command,
        },
-       {// usb_setting
-               .vid                            = VERSALOON_VID,
-               .pid                            = VERSALOON_PID,
-               .ep_out                         = VERSALOON_OUTP,
-               .ep_in                          = VERSALOON_INP,
-               .interface                      = VERSALOON_IFACE,
-               .serialstring           = NULL,
-               .buf_size                       = 256,
+       {       /* usb_setting */
+               .vid                    = VERSALOON_VID,
+               .pid                    = VERSALOON_PID,
+               .ep_out                 = VERSALOON_OUTP,
+               .ep_in                  = VERSALOON_INP,
+               .interface              = VERSALOON_IFACE,
+               .buf_size               = 256,
        }
 };
 
-// programmer_cmd
-static uint32_t versaloon_pending_id = 0;
-static versaloon_callback_t versaloon_callback = NULL;
-static void *versaloon_extra_data = NULL;
-static struct versaloon_want_pos_t *versaloon_want_pos = NULL;
+/* programmer_cmd */
+static uint32_t versaloon_pending_id;
+static versaloon_callback_t versaloon_callback;
+static void *versaloon_extra_data;
+static struct versaloon_want_pos_t *versaloon_want_pos;
+
 void versaloon_set_pending_id(uint32_t id)
 {
        versaloon_pending_id = id;
@@ -104,7 +105,7 @@ void versaloon_set_callback(versaloon_callback_t callback)
 {
        versaloon_callback = callback;
 }
-void versaloon_set_extra_data(void * p)
+void versaloon_set_extra_data(void *p)
 {
        versaloon_extra_data = p;
 }
@@ -115,19 +116,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);
@@ -140,9 +138,8 @@ RESULT versaloon_add_want_pos(uint16_t offset, uint16_t size, uint8_t *buff)
 {
        struct versaloon_want_pos_t *new_pos = NULL;
 
-       new_pos = (struct versaloon_want_pos_t *)malloc(sizeof(*new_pos));
-       if (NULL == new_pos)
-       {
+       new_pos = malloc(sizeof(*new_pos));
+       if (!new_pos) {
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
@@ -151,18 +148,13 @@ 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
-       {
+       else {
                struct versaloon_want_pos_t *tmp = versaloon_want_pos;
 
-               while (tmp->next != NULL)
-               {
+               while (tmp->next)
                        tmp = tmp->next;
-               }
                tmp->next = new_pos;
        }
 
@@ -173,10 +165,9 @@ RESULT versaloon_add_pending(uint8_t type, uint8_t cmd, uint16_t actual_szie,
        uint16_t want_pos, uint16_t want_size, uint8_t *buffer, uint8_t collect)
 {
 #if PARAM_CHECK
-       if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER)
-       {
+       if (versaloon_pending_idx >= VERSALOON_MAX_PENDING_NUMBER) {
                LOG_BUG(ERRMSG_INVALID_INDEX, versaloon_pending_idx,
-                                       "versaloon pending data");
+                       "versaloon pending data");
                return ERROR_FAIL;
        }
 #endif
@@ -204,85 +195,69 @@ RESULT versaloon_add_pending(uint8_t type, uint8_t cmd, uint16_t actual_szie,
 RESULT versaloon_send_command(uint16_t out_len, uint16_t *inlen)
 {
        int ret;
+       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))
-       {
-               LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__);
+       if ((out_len == 0) || (out_len > versaloon_interface.usb_setting.buf_size)) {
+               LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
                return ERRCODE_INVALID_PARAMETER;
        }
 #endif
 
-       ret = usb_bulk_write(versaloon_usb_device_handle,
-               versaloon_interface.usb_setting.ep_out, (char *)versaloon_buf,
-               out_len, versaloon_usb_to);
-       if (ret != out_len)
-       {
-               LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "send usb data",
-                       usb_strerror());
+       ret = libusb_bulk_transfer(versaloon_usb_device_handle,
+                       versaloon_interface.usb_setting.ep_out,
+                       versaloon_buf, out_len, &transferred, versaloon_usb_to);
+       if (ret != 0 || transferred != out_len) {
+               LOG_ERROR(ERRMSG_FAILURE_OPERATION, "send usb data");
                return ERRCODE_FAILURE_OPERATION;
        }
 
-       if (inlen != NULL)
-       {
-               ret = usb_bulk_read(versaloon_usb_device_handle,
-                       versaloon_interface.usb_setting.ep_in, (char *)versaloon_buf,
-                       versaloon_interface.usb_setting.buf_size, versaloon_usb_to);
-               if (ret > 0)
-               {
-                       *inlen = (uint16_t)ret;
+       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 (ret == 0) {
+                       *inlen = (uint16_t)transferred;
                        return ERROR_OK;
-               }
-               else
-               {
-                       LOG_ERROR(ERRMSG_FAILURE_OPERATION_ERRSTRING, "receive usb data",
-                                               usb_strerror());
+               } else {
+                       LOG_ERROR(ERRMSG_FAILURE_OPERATION, "receive usb data");
                        return ERROR_FAIL;
                }
-       }
-       else
-       {
+       } else
                return ERROR_OK;
-       }
 }
 
-#define VERSALOON_RETRY_CNT                            10
-RESULT versaloon_init(void)
+#define VERSALOON_RETRY_CNT 10
+static RESULT versaloon_init(void)
 {
        uint16_t ret = 0;
        uint8_t retry;
        uint32_t timeout_tmp;
 
-       // malloc temporary buffer
-       versaloon_buf =
-               (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size);
-       if (NULL == versaloon_buf)
-       {
+       /* malloc temporary buffer */
+       versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
+       if (!versaloon_buf) {
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
 
-       // connect to versaloon
+       /* connect to versaloon */
        timeout_tmp = versaloon_usb_to;
-       // not output error message when connectting
-       // 100ms delay when connect
+       /* not output error message when connecting */
+       /* 100ms delay when connect */
        versaloon_usb_to = 100;
-       for (retry = 0; retry < VERSALOON_RETRY_CNT; retry++)
-       {
+       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;
@@ -293,59 +268,48 @@ RESULT versaloon_init(void)
        versaloon_interface.usb_setting.buf_size = versaloon_buf_size;
        LOG_INFO("%s", versaloon_buf + 2);
 
-       // free temporary buffer
+       /* free temporary buffer */
        free(versaloon_buf);
        versaloon_buf = NULL;
 
-       versaloon_buf =
-               (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size);
-       if (NULL == versaloon_buf)
-       {
+       versaloon_buf = malloc(versaloon_interface.usb_setting.buf_size);
+       if (!versaloon_buf) {
                versaloon_fini();
                LOG_ERROR(ERRMSG_NOT_ENOUGH_MEMORY);
                return ERRCODE_NOT_ENOUGH_MEMORY;
        }
-       versaloon_cmd_buf =
-               (uint8_t *)malloc(versaloon_interface.usb_setting.buf_size - 3);
-       if (NULL == versaloon_cmd_buf)
-       {
+       versaloon_cmd_buf = malloc(versaloon_interface.usb_setting.buf_size - 3);
+       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();
 
                versaloon_usb_device_handle = NULL;
 
-               if (versaloon_buf != NULL)
-               {
-                       free(versaloon_buf);
-                       versaloon_buf = NULL;
-               }
-               if (versaloon_cmd_buf != NULL)
-               {
-                       free(versaloon_cmd_buf);
-                       versaloon_cmd_buf = NULL;
-               }
+               free(versaloon_buf);
+               versaloon_buf = NULL;
+
+               free(versaloon_cmd_buf);
+               versaloon_cmd_buf = NULL;
        }
 
        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);
@@ -355,44 +319,38 @@ 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)
-       {
-               LOG_BUG(ERRMSG_INVALID_PARAMETER, __FUNCTION__);
+       if (!voltage) {
+               LOG_BUG(ERRMSG_INVALID_PARAMETER, __func__);
                return ERRCODE_INVALID_PARAMETER;
        }
 #endif
 
        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
-       {
+       } else {
                *voltage = versaloon_buf[0] + (versaloon_buf[1] << 8);
                return ERROR_OK;
        }
 }
 
-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);
 }
-