Convert DEBUG_JTAG_IO to LOG_DEBUG_IO
[openocd.git] / src / jtag / drivers / rlink.c
index 2905bf261f1936b08533345481ce6f0f8900a9b2..70f8734b55248f4cc1c65a4ce66a649ce6f2eef3 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 "rlink_dtc_cmd.h"
 #include "usb_common.h"
 
-
-/* This feature is made useless by running the DTC all the time.  When automatic, the LED is on whenever the DTC is running.  Otherwise, USB messages are sent to turn it on and off. */
+/* This feature is made useless by running the DTC all the time.  When automatic, the LED is on
+ *whenever the DTC is running.  Otherwise, USB messages are sent to turn it on and off. */
 #undef AUTOMATIC_BUSY_LED
 
 /* This feature may require derating the speed due to reduced hold time. */
 #undef USE_HARDWARE_SHIFTER_FOR_TMS
 
+#define INTERFACE_NAME          "RLink"
 
-#define INTERFACE_NAME         "RLink"
-
-#define USB_IDVENDOR           (0x138e)
-#define USB_IDPRODUCT          (0x9000)
+#define USB_IDVENDOR            (0x138e)
+#define USB_IDPRODUCT           (0x9000)
 
-#define USB_EP1OUT_ADDR                (0x01)
-#define USB_EP1OUT_SIZE                (16)
-#define USB_EP1IN_ADDR         (USB_EP1OUT_ADDR | 0x80)
-#define USB_EP1IN_SIZE         (USB_EP1OUT_SIZE)
+#define USB_EP1OUT_ADDR         (0x01)
+#define USB_EP1OUT_SIZE         (16)
+#define USB_EP1IN_ADDR          (USB_EP1OUT_ADDR | 0x80)
+#define USB_EP1IN_SIZE          (USB_EP1OUT_SIZE)
 
-#define USB_EP2OUT_ADDR                (0x02)
-#define USB_EP2OUT_SIZE                (64)
-#define USB_EP2IN_ADDR         (USB_EP2OUT_ADDR | 0x80)
-#define USB_EP2IN_SIZE         (USB_EP2OUT_SIZE)
-#define USB_EP2BANK_SIZE       (512)
+#define USB_EP2OUT_ADDR         (0x02)
+#define USB_EP2OUT_SIZE         (64)
+#define USB_EP2IN_ADDR          (USB_EP2OUT_ADDR | 0x80)
+#define USB_EP2IN_SIZE          (USB_EP2OUT_SIZE)
+#define USB_EP2BANK_SIZE        (512)
 
-#define USB_TIMEOUT_MS         (3 * 1000)
+#define USB_TIMEOUT_MS          (3 * 1000)
 
-#define DTC_STATUS_POLL_BYTE   (ST7_USB_BUF_EP0OUT + 0xff)
+#define DTC_STATUS_POLL_BYTE    (ST7_USB_BUF_EP0OUT + 0xff)
 
-
-#define ST7_PD_NBUSY_LED               ST7_PD0
-#define ST7_PD_NRUN_LED                        ST7_PD1
+#define ST7_PD_NBUSY_LED                ST7_PD0
+#define ST7_PD_NRUN_LED                 ST7_PD1
 /* low enables VPP at adapter header, high connects it to GND instead */
-#define ST7_PD_VPP_SEL                 ST7_PD6
+#define ST7_PD_VPP_SEL                  ST7_PD6
 /* low: VPP = 12v, high: VPP <= 5v */
-#define ST7_PD_VPP_SHDN                        ST7_PD7
+#define ST7_PD_VPP_SHDN                 ST7_PD7
 
 /* These pins are connected together */
-#define ST7_PE_ADAPTER_SENSE_IN                ST7_PE3
-#define ST7_PE_ADAPTER_SENSE_OUT       ST7_PE4
+#define ST7_PE_ADAPTER_SENSE_IN         ST7_PE3
+#define ST7_PE_ADAPTER_SENSE_OUT        ST7_PE4
 
 /* Symbolic mapping between port pins and numbered IO lines */
-#define ST7_PA_IO1     ST7_PA1
-#define ST7_PA_IO2     ST7_PA2
-#define ST7_PA_IO4     ST7_PA4
-#define ST7_PA_IO8     ST7_PA6
-#define ST7_PA_IO10    ST7_PA7
-#define ST7_PB_IO5     ST7_PB5
-#define ST7_PC_IO9     ST7_PC1
-#define ST7_PC_IO3     ST7_PC2
-#define ST7_PC_IO7     ST7_PC3
-#define ST7_PE_IO6     ST7_PE5
+#define ST7_PA_IO1      ST7_PA1
+#define ST7_PA_IO2      ST7_PA2
+#define ST7_PA_IO4      ST7_PA4
+#define ST7_PA_IO8      ST7_PA6
+#define ST7_PA_IO10     ST7_PA7
+#define ST7_PB_IO5      ST7_PB5
+#define ST7_PC_IO9      ST7_PC1
+#define ST7_PC_IO3      ST7_PC2
+#define ST7_PC_IO7      ST7_PC3
+#define ST7_PE_IO6      ST7_PE5
 
 /* Symbolic mapping between numbered IO lines and adapter signals */
-#define ST7_PA_RTCK    ST7_PA_IO0
-#define ST7_PA_NTRST   ST7_PA_IO1
-#define ST7_PC_TDI     ST7_PC_IO3
-#define ST7_PA_DBGRQ   ST7_PA_IO4
-#define ST7_PB_NSRST   ST7_PB_IO5
-#define ST7_PE_TMS     ST7_PE_IO6
-#define ST7_PC_TCK     ST7_PC_IO7
-#define ST7_PC_TDO     ST7_PC_IO9
-#define ST7_PA_DBGACK  ST7_PA_IO10
+#define ST7_PA_RTCK     ST7_PA_IO0
+#define ST7_PA_NTRST    ST7_PA_IO1
+#define ST7_PC_TDI      ST7_PC_IO3
+#define ST7_PA_DBGRQ    ST7_PA_IO4
+#define ST7_PB_NSRST    ST7_PB_IO5
+#define ST7_PE_TMS      ST7_PE_IO6
+#define ST7_PC_TCK      ST7_PC_IO7
+#define ST7_PC_TDO      ST7_PC_IO9
+#define ST7_PA_DBGACK   ST7_PA_IO10
 
 static usb_dev_handle *pHDev;
 
-
 /*
  * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
  * This function takes care of zeroing the unused bytes before sending the packet.
  * Any reply packet is not handled by this function.
  */
-static
-int
-ep1_generic_commandl(
-       usb_dev_handle  *pHDev_param,
-       size_t          length,
-       ...
-) {
-       uint8_t         usb_buffer[USB_EP1OUT_SIZE];
-       uint8_t         *usb_buffer_p;
-       va_list         ap;
-       int             usb_ret;
-
-       if (length > sizeof(usb_buffer)) {
+static int ep1_generic_commandl(usb_dev_handle *pHDev_param, size_t length, ...)
+{
+       uint8_t usb_buffer[USB_EP1OUT_SIZE];
+       uint8_t *usb_buffer_p;
+       va_list ap;
+       int usb_ret;
+
+       if (length > sizeof(usb_buffer))
                length = sizeof(usb_buffer);
-       }
 
        usb_buffer_p = usb_buffer;
 
@@ -135,74 +125,65 @@ ep1_generic_commandl(
                usb_buffer_p,
                0,
                sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
-);
+               );
 
        usb_ret = usb_bulk_write(
-               pHDev_param,
-               USB_EP1OUT_ADDR,
-               (char *)usb_buffer, sizeof(usb_buffer),
-               USB_TIMEOUT_MS
-);
+                       pHDev_param,
+                       USB_EP1OUT_ADDR,
+                       (char *)usb_buffer, sizeof(usb_buffer),
+                       USB_TIMEOUT_MS
+                       );
 
-       return(usb_ret);
+       return usb_ret;
 }
 
-
-
 #if 0
-static
-ssize_t
-ep1_memory_read(
-       usb_dev_handle  *pHDev,
-       uint16_t        addr,
-       size_t          length,
-       uint8_t         *buffer
-) {
-       uint8_t         usb_buffer[USB_EP1OUT_SIZE];
-       int             usb_ret;
-       size_t          remain;
-       ssize_t         count;
+static ssize_t ep1_memory_read(
+       usb_dev_handle *pHDev, uint16_t addr,
+       size_t length, uint8_t *buffer)
+{
+       uint8_t usb_buffer[USB_EP1OUT_SIZE];
+       int usb_ret;
+       size_t remain;
+       ssize_t count;
 
        usb_buffer[0] = EP1_CMD_MEMORY_READ;
        memset(
                usb_buffer + 4,
                0,
                sizeof(usb_buffer) - 4
-);
+               );
 
        remain = length;
        count = 0;
 
        while (remain) {
-               if (remain > sizeof(usb_buffer)) {
+               if (remain > sizeof(usb_buffer))
                        length = sizeof(usb_buffer);
-               } else {
+               else
                        length = remain;
-               }
 
                usb_buffer[1] = addr >> 8;
                usb_buffer[2] = addr;
                usb_buffer[3] = length;
 
-                       usb_ret = usb_bulk_write(
-                       pHDev, USB_EP1OUT_ADDR,
-                       usb_buffer, sizeof(usb_buffer),
-                       USB_TIMEOUT_MS
-);
+               usb_ret = usb_bulk_write(
+                               pHDev, USB_EP1OUT_ADDR,
+                               usb_buffer, sizeof(usb_buffer),
+                               USB_TIMEOUT_MS
+                               );
 
-               if (usb_ret < sizeof(usb_buffer)) {
+               if (usb_ret < sizeof(usb_buffer))
                        break;
-               }
 
                usb_ret = usb_bulk_read(
-                       pHDev, USB_EP1IN_ADDR,
-                       buffer, length,
-                       USB_TIMEOUT_MS
-);
+                               pHDev, USB_EP1IN_ADDR,
+                               buffer, length,
+                               USB_TIMEOUT_MS
+                               );
 
-               if (usb_ret < length) {
+               if (usb_ret < length)
                        break;
-               }
 
                addr += length;
                buffer += length;
@@ -210,24 +191,17 @@ ep1_memory_read(
                remain -= length;
        }
 
-       return(count);
+       return count;
 }
 #endif
 
-
-
-static
-ssize_t
-ep1_memory_write(
-       usb_dev_handle  *pHDev_param,
-       uint16_t        addr,
-       size_t          length,
-       uint8_t const   *buffer
-) {
-       uint8_t         usb_buffer[USB_EP1OUT_SIZE];
-       int             usb_ret;
-       size_t          remain;
-       ssize_t         count;
+static ssize_t ep1_memory_write(usb_dev_handle *pHDev_param, uint16_t addr,
+       size_t length, uint8_t const *buffer)
+{
+       uint8_t usb_buffer[USB_EP1OUT_SIZE];
+       int usb_ret;
+       size_t remain;
+       ssize_t count;
 
        usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
 
@@ -235,11 +209,10 @@ ep1_memory_write(
        count = 0;
 
        while (remain) {
-               if (remain > (sizeof(usb_buffer) - 4)) {
+               if (remain > (sizeof(usb_buffer) - 4))
                        length = (sizeof(usb_buffer) - 4);
-               } else {
+               else
                        length = remain;
-               }
 
                usb_buffer[1] = addr >> 8;
                usb_buffer[2] = addr;
@@ -248,22 +221,21 @@ ep1_memory_write(
                        usb_buffer + 4,
                        buffer,
                        length
-);
+                       );
                memset(
                        usb_buffer + 4 + length,
                        0,
                        sizeof(usb_buffer) - 4 - length
-);
+                       );
 
-                       usb_ret = usb_bulk_write(
-                       pHDev_param, USB_EP1OUT_ADDR,
-                       (char *)usb_buffer, sizeof(usb_buffer),
-                       USB_TIMEOUT_MS
-);
+               usb_ret = usb_bulk_write(
+                               pHDev_param, USB_EP1OUT_ADDR,
+                               (char *)usb_buffer, sizeof(usb_buffer),
+                               USB_TIMEOUT_MS
+                               );
 
-               if ((size_t)usb_ret < sizeof(usb_buffer)) {
+               if ((size_t)usb_ret < sizeof(usb_buffer))
                        break;
-               }
 
                addr += length;
                buffer += length;
@@ -271,27 +243,21 @@ ep1_memory_write(
                remain -= length;
        }
 
-       return(count);
+       return count;
 }
 
 
 #if 0
-static
-ssize_t
-ep1_memory_writel(
-       usb_dev_handle  *pHDev,
-       uint16_t        addr,
-       size_t          length,
-       ...
-) {
-       uint8_t         buffer[USB_EP1OUT_SIZE - 4];
-       uint8_t         *buffer_p;
-       va_list         ap;
-       size_t          remain;
-
-       if (length > sizeof(buffer)) {
+static ssize_t ep1_memory_writel(usb_dev_handle *pHDev, uint16_t addr,
+       size_t length, ...)
+{
+       uint8_t buffer[USB_EP1OUT_SIZE - 4];
+       uint8_t *buffer_p;
+       va_list ap;
+       size_t remain;
+
+       if (length > sizeof(buffer))
                length = sizeof(buffer);
-       }
 
        remain = length;
        buffer_p = buffer;
@@ -302,53 +268,48 @@ ep1_memory_writel(
                remain--;
        }
 
-       return(ep1_memory_write(pHDev, addr, length, buffer));
+       return ep1_memory_write(pHDev, addr, length, buffer);
 }
 #endif
 
+#define DTCLOAD_COMMENT         (0)
+#define DTCLOAD_ENTRY           (1)
+#define DTCLOAD_LOAD            (2)
+#define DTCLOAD_RUN                     (3)
+#define DTCLOAD_LUT_START       (4)
+#define DTCLOAD_LUT                     (5)
 
-#define DTCLOAD_COMMENT                (0)
-#define DTCLOAD_ENTRY          (1)
-#define DTCLOAD_LOAD           (2)
-#define DTCLOAD_RUN                    (3)
-#define DTCLOAD_LUT_START      (4)
-#define DTCLOAD_LUT                    (5)
-
-#define DTC_LOAD_BUFFER                ST7_USB_BUF_EP2UIDO
+#define DTC_LOAD_BUFFER         ST7_USB_BUF_EP2UIDO
 
 /* This gets set by the DTC loader */
 static uint8_t dtc_entry_download;
 
-
 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
-static
-int
-dtc_load_from_buffer(
-       usb_dev_handle  *pHDev_param,
-       const uint8_t           *buffer,
-       size_t                  length
-) {
+static int dtc_load_from_buffer(usb_dev_handle *pHDev_param, const uint8_t *buffer,
+               size_t length)
+{
        struct header_s {
-               uint8_t type;
-               uint8_t length;
+               uint8_t type;
+               uint8_t length;
        };
 
-       int                             usb_err;
-       struct header_s *header;
-       uint8_t                         lut_start = 0xc0;
+       int usb_err;
+       struct header_s *header;
+       uint8_t lut_start = 0xc0;
 
        dtc_entry_download = 0;
 
        /* Stop the DTC before loading anything. */
        usb_err = ep1_generic_commandl(
-               pHDev_param, 1,
-               EP1_CMD_DTC_STOP
-);
-       if (usb_err < 0) return(usb_err);
+                       pHDev_param, 1,
+                       EP1_CMD_DTC_STOP
+                       );
+       if (usb_err < 0)
+               return usb_err;
 
        while (length) {
                if (length < sizeof(*header)) {
-                       LOG_ERROR("Malformed DTC image\n");
+                       LOG_ERROR("Malformed DTC image");
                        exit(1);
                }
 
@@ -357,7 +318,7 @@ dtc_load_from_buffer(
                length -= sizeof(*header);
 
                if (length < (size_t)header->length + 1) {
-                       LOG_ERROR("Malformed DTC image\n");
+                       LOG_ERROR("Malformed DTC image");
                        exit(1);
                }
 
@@ -367,39 +328,41 @@ dtc_load_from_buffer(
 
                        case DTCLOAD_ENTRY:
                                /* store entry addresses somewhere */
-                               if (!strncmp("download", (char *)buffer + 1, 8)) {
+                               if (!strncmp("download", (char *)buffer + 1, 8))
                                        dtc_entry_download = buffer[0];
-                               }
                                break;
 
                        case DTCLOAD_LOAD:
                                /* Send the DTC program to ST7 RAM. */
                                usb_err = ep1_memory_write(
-                                       pHDev_param,
-                                       DTC_LOAD_BUFFER,
-                                       header->length + 1, buffer
-);
-                               if (usb_err < 0) return(usb_err);
+                                               pHDev_param,
+                                               DTC_LOAD_BUFFER,
+                                               header->length + 1, buffer
+                                       );
+                               if (usb_err < 0)
+                                       return usb_err;
 
                                /* Load it into the DTC. */
                                usb_err = ep1_generic_commandl(
-                                       pHDev_param, 3,
-                                       EP1_CMD_DTC_LOAD,
+                                               pHDev_param, 3,
+                                               EP1_CMD_DTC_LOAD,
                                                (DTC_LOAD_BUFFER >> 8),
                                                DTC_LOAD_BUFFER
-);
-                               if (usb_err < 0) return(usb_err);
+                                       );
+                               if (usb_err < 0)
+                                       return usb_err;
 
                                break;
 
                        case DTCLOAD_RUN:
                                usb_err = ep1_generic_commandl(
-                                       pHDev_param, 3,
-                                       EP1_CMD_DTC_CALL,
+                                               pHDev_param, 3,
+                                               EP1_CMD_DTC_CALL,
                                                buffer[0],
-                                       EP1_CMD_DTC_WAIT
-);
-                               if (usb_err < 0) return(usb_err);
+                                               EP1_CMD_DTC_WAIT
+                                       );
+                               if (usb_err < 0)
+                                       return usb_err;
 
                                break;
 
@@ -409,15 +372,16 @@ dtc_load_from_buffer(
 
                        case DTCLOAD_LUT:
                                usb_err = ep1_memory_write(
-                                       pHDev_param,
-                                       ST7_USB_BUF_EP0OUT + lut_start,
-                                       header->length + 1, buffer
-);
-                               if (usb_err < 0) return(usb_err);
+                                               pHDev_param,
+                                               ST7_USB_BUF_EP0OUT + lut_start,
+                                               header->length + 1, buffer
+                                       );
+                               if (usb_err < 0)
+                                       return usb_err;
                                break;
 
                        default:
-                               LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header->type);
+                               LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
                                exit(1);
                                break;
                }
@@ -426,164 +390,162 @@ dtc_load_from_buffer(
                length -= (header->length + 1);
        }
 
-       return(0);
+       return 0;
 }
 
-
 /*
  * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
  */
-static
-int
-dtc_start_download(void) {
-       int     usb_err;
-       uint8_t ep2txr;
+static int dtc_start_download(void)
+{
+       int usb_err;
+       uint8_t ep2txr;
 
        /* set up for download mode and make sure EP2 is set up to transmit */
        usb_err = ep1_generic_commandl(
-               pHDev, 7,
+                       pHDev, 7,
 
-               EP1_CMD_DTC_STOP,
-               EP1_CMD_SET_UPLOAD,
-               EP1_CMD_SET_DOWNLOAD,
-               EP1_CMD_MEMORY_READ,    /* read EP2TXR for its data toggle */
+                       EP1_CMD_DTC_STOP,
+                       EP1_CMD_SET_UPLOAD,
+                       EP1_CMD_SET_DOWNLOAD,
+                       EP1_CMD_MEMORY_READ,    /* read EP2TXR for its data toggle */
                        ST7_EP2TXR >> 8,
                        ST7_EP2TXR,
                        1
-);
-       if (usb_err < 0) return(usb_err);
+                       );
+       if (usb_err < 0)
+               return usb_err;
 
        /* read back ep2txr */
        usb_err = usb_bulk_read(
-               pHDev, USB_EP1IN_ADDR,
-               (char *)&ep2txr, 1,
-               USB_TIMEOUT_MS
-);
-       if (usb_err < 0) return(usb_err);
+                       pHDev, USB_EP1IN_ADDR,
+                       (char *)&ep2txr, 1,
+                       USB_TIMEOUT_MS
+                       );
+       if (usb_err < 0)
+               return usb_err;
 
        usb_err = ep1_generic_commandl(
-               pHDev, 13,
+                       pHDev, 13,
 
-               EP1_CMD_MEMORY_WRITE,   /* preinitialize poll byte */
+                       EP1_CMD_MEMORY_WRITE,   /* preinitialize poll byte */
                        DTC_STATUS_POLL_BYTE >> 8,
                        DTC_STATUS_POLL_BYTE,
                        1,
                        0x00,
-               EP1_CMD_MEMORY_WRITE,   /* set EP2IN to return data */
+                       EP1_CMD_MEMORY_WRITE,   /* set EP2IN to return data */
                        ST7_EP2TXR >> 8,
                        ST7_EP2TXR,
                        1,
                        (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
-               EP1_CMD_DTC_CALL,       /* start running the DTC */
+                       EP1_CMD_DTC_CALL,       /* start running the DTC */
                        dtc_entry_download,
-               EP1_CMD_DTC_GET_CACHED_STATUS
-);
-       if (usb_err < 0) return(usb_err);
+                       EP1_CMD_DTC_GET_CACHED_STATUS
+                       );
+       if (usb_err < 0)
+               return usb_err;
 
        /* wait for completion */
        usb_err = usb_bulk_read(
-               pHDev, USB_EP1IN_ADDR,
-               (char *)&ep2txr, 1,
-               USB_TIMEOUT_MS
-);
+                       pHDev, USB_EP1IN_ADDR,
+                       (char *)&ep2txr, 1,
+                       USB_TIMEOUT_MS
+                       );
 
-       return(usb_err);
+       return usb_err;
 }
 
+static int dtc_run_download(
+       usb_dev_handle *pHDev_param,
+       uint8_t *command_buffer,
+       int command_buffer_size,
+       uint8_t *reply_buffer,
+       int reply_buffer_size
+       )
+{
+       char dtc_status;
+       int usb_err;
+       int i;
 
-static
-int
-dtc_run_download(
-       usb_dev_handle  *pHDev_param,
-       uint8_t *command_buffer,
-       int     command_buffer_size,
-       uint8_t *reply_buffer,
-       int     reply_buffer_size
-) {
-       uint8_t ep2_buffer[USB_EP2IN_SIZE];
-       int     usb_err;
-       int     i;
-
-       LOG_DEBUG(": %d/%d\n", command_buffer_size, reply_buffer_size);
+       LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
 
        usb_err = usb_bulk_write(
-               pHDev_param,
-               USB_EP2OUT_ADDR,
-               (char *)command_buffer, USB_EP2BANK_SIZE,
-               USB_TIMEOUT_MS
-);
-       if (usb_err < 0) return(usb_err);
+                       pHDev_param,
+                       USB_EP2OUT_ADDR,
+                       (char *)command_buffer, USB_EP2BANK_SIZE,
+                       USB_TIMEOUT_MS
+                       );
+       if (usb_err < 0)
+               return usb_err;
 
 
        /* Wait for DTC to finish running command buffer */
-       for (i = 10;;) {
+       for (i = 50;; ) {
                usb_err = ep1_generic_commandl(
-                       pHDev_param, 4,
+                               pHDev_param, 4,
 
-                       EP1_CMD_MEMORY_READ,
+                               EP1_CMD_MEMORY_READ,
                                DTC_STATUS_POLL_BYTE >> 8,
                                DTC_STATUS_POLL_BYTE,
                                1
-);
-               if (usb_err < 0) return(usb_err);
+                               );
+               if (usb_err < 0)
+                       return usb_err;
 
                usb_err = usb_bulk_read(
-                       pHDev_param,
-                       USB_EP1IN_ADDR,
-                       (char *)ep2_buffer, 1,
-                       USB_TIMEOUT_MS
-);
-               if (usb_err < 0) return(usb_err);
-
-               if (ep2_buffer[0] & 0x01) break;
+                               pHDev_param,
+                               USB_EP1IN_ADDR,
+                               &dtc_status, 1,
+                               USB_TIMEOUT_MS
+                               );
+               if (usb_err < 0)
+                       return usb_err;
+
+               if (dtc_status & 0x01)
+                       break;
 
                if (!--i) {
-                       LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
-                               __FILE__, __LINE__
-);
-                       return(-ETIMEDOUT);
+                       LOG_ERROR("too many retries waiting for DTC status");
+                       return -ETIMEDOUT;
                }
        }
 
 
-       if (!reply_buffer) reply_buffer_size = 0;
-       if (reply_buffer_size) {
+       if (reply_buffer && reply_buffer_size) {
                usb_err = usb_bulk_read(
-                       pHDev_param,
-                       USB_EP2IN_ADDR,
-                       (char *)ep2_buffer, sizeof(ep2_buffer),
-                       USB_TIMEOUT_MS
-);
-
-               if (usb_err < (int)sizeof(ep2_buffer)) {
-                       LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
-                               __FILE__, __LINE__, usb_err
-);
-                       return(usb_err);
+                               pHDev_param,
+                               USB_EP2IN_ADDR,
+                               (char *)reply_buffer, reply_buffer_size,
+                               USB_TIMEOUT_MS
+                               );
+
+               if (usb_err < reply_buffer_size) {
+                       LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
+                               usb_err, reply_buffer_size
+                               );
+                       return usb_err;
                }
-
-               memcpy(reply_buffer, ep2_buffer, reply_buffer_size);
-
        }
 
-       return(usb_err);
+       return usb_err;
 }
 
-
 /*
- * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC.  Only SCAN_IN and SCAN_IO generate these entries.
+ * The dtc reply queue is a singly linked list that describes what to do
+ * with the reply packet that comes from the DTC.  Only SCAN_IN and SCAN_IO generate
+ * these entries.
  */
 
 struct dtc_reply_queue_entry {
-       struct dtc_reply_queue_entry    *next;
-       struct jtag_command     *cmd;   /* the command that resulted in this entry */
+       struct dtc_reply_queue_entry *next;
+       struct jtag_command *cmd;       /* the command that resulted in this entry */
 
        struct {
-               uint8_t         *buffer;        /* the scan buffer */
-               int             size;           /* size of the scan buffer in bits */
-               int             offset;         /* how many bits were already done before this? */
-               int             length;         /* how many bits are processed in this operation? */
-               enum scan_type  type;           /* SCAN_IN/SCAN_OUT/SCAN_IO */
+               uint8_t *buffer;                /* the scan buffer */
+               int size;                       /* size of the scan buffer in bits */
+               int offset;                     /* how many bits were already done before this? */
+               int length;                     /* how many bits are processed in this operation? */
+               enum scan_type type;            /* SCAN_IN/SCAN_OUT/SCAN_IO */
        } scan;
 };
 
@@ -593,51 +555,39 @@ struct dtc_reply_queue_entry {
  * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
  */
 
-static
-struct {
-       struct dtc_reply_queue_entry    *rq_head;
-       struct dtc_reply_queue_entry    *rq_tail;
-       uint32_t                        cmd_index;
-       uint32_t                        reply_index;
-       uint8_t                 cmd_buffer[USB_EP2BANK_SIZE];
+static struct {
+       struct dtc_reply_queue_entry *rq_head;
+       struct dtc_reply_queue_entry *rq_tail;
+       uint32_t cmd_index;
+       uint32_t reply_index;
+       uint8_t cmd_buffer[USB_EP2BANK_SIZE];
 } dtc_queue;
 
-
 /*
- * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue.  When it fills or is run, it adds the accumulated bytes to the dtc_queue.
+ * The tap state queue is for accumulating TAP state changes wiithout needlessly
+ * flushing the dtc_queue.  When it fills or is run, it adds the accumulated bytes to
+ * the dtc_queue.
  */
 
-static
-struct {
-       uint32_t        length;
-       uint32_t        buffer;
+static struct {
+       uint32_t length;
+       uint32_t buffer;
 } tap_state_queue;
 
-
-
-static
-int
-dtc_queue_init(void) {
+static int dtc_queue_init(void)
+{
        dtc_queue.rq_head = NULL;
        dtc_queue.rq_tail = NULL;
        dtc_queue.cmd_index = 0;
        dtc_queue.reply_index = 0;
-       return(0);
+       return 0;
 }
 
-
-static
-inline
-struct dtc_reply_queue_entry *
-dtc_queue_enqueue_reply(
-       enum scan_type  type,
-       uint8_t                         *buffer,
-       int                             size,
-       int                             offset,
-       int                             length,
-       struct jtag_command     *cmd
-) {
-       struct dtc_reply_queue_entry    *rq_entry;
+static inline struct dtc_reply_queue_entry *dtc_queue_enqueue_reply(
+       enum scan_type type, uint8_t *buffer, int size, int offset,
+       int length, struct jtag_command *cmd)
+{
+       struct dtc_reply_queue_entry *rq_entry;
 
        rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
        if (rq_entry != NULL) {
@@ -657,195 +607,192 @@ dtc_queue_enqueue_reply(
                dtc_queue.rq_tail = rq_entry;
        }
 
-       return(rq_entry);
+       return rq_entry;
 }
 
-
 /*
- * Running the queue means that any pending command buffer is run and any reply data dealt with.  The command buffer is then cleared for subsequent processing.
+ * Running the queue means that any pending command buffer is run
+ * and any reply data dealt with.  The command buffer is then cleared for subsequent processing.
  * The queue is automatically run by append when it is necessary to get space for the append.
-*/
-
-static
-int
-dtc_queue_run(void) {
-       struct dtc_reply_queue_entry    *rq_p, *rq_next;
-       int                     retval;
-       int                     usb_err;
-       int                     bit_cnt;
-       int                     x;
-       uint8_t                 *dtc_p, *tdo_p;
-       uint8_t                 dtc_mask, tdo_mask;
-       uint8_t                 reply_buffer[USB_EP2IN_SIZE];
+ */
+
+static int dtc_queue_run(void)
+{
+       struct dtc_reply_queue_entry *rq_p, *rq_next;
+       int retval;
+       int usb_err;
+       int bit_cnt;
+       int x;
+       uint8_t *dtc_p, *tdo_p;
+       uint8_t dtc_mask, tdo_mask;
+       uint8_t reply_buffer[USB_EP2IN_SIZE];
+
+       assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
+       assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
+       assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
 
        retval = ERROR_OK;
 
-       if (dtc_queue.cmd_index < 1) return(retval);
+       if (dtc_queue.cmd_index < 1)
+               return retval;
 
        dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
 
-       /* run the cmd */
-       if (dtc_queue.rq_head == NULL) {
-               usb_err = dtc_run_download(pHDev,
-                       dtc_queue.cmd_buffer, dtc_queue.cmd_index,
-                       NULL, 0
-);
-               if (usb_err < 0) {
-                       LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
-                       exit(1);
-               }
-       } else {
-               usb_err = dtc_run_download(pHDev,
+       usb_err = dtc_run_download(pHDev,
                        dtc_queue.cmd_buffer, dtc_queue.cmd_index,
-                       reply_buffer, dtc_queue.reply_index
-);
-               if (usb_err < 0) {
-                       LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
-                       exit(1);
-               } else {
-                       /* process the reply, which empties the reply queue and frees its entries */
-                       dtc_p = reply_buffer;
-
-                       /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons.   It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */
-
-                       for (
-                               rq_p = dtc_queue.rq_head;
-                               rq_p != NULL;
-                               rq_p = rq_next
-) {
-                               tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
-                               tdo_mask = 1 << (rq_p->scan.offset % 8);
-
-
-                               bit_cnt = rq_p->scan.length;
-                               if (bit_cnt >= 8) {
-                                       /* bytes */
-
-                                       dtc_mask = 1 << (8 - 1);
-
-                                       for (
-                                               ;
-                                               bit_cnt;
-                                               bit_cnt--
-) {
-                                               if (*dtc_p & dtc_mask) {
-                                                       *tdo_p |= tdo_mask;
-                                               } else {
-                                                       *tdo_p &=~ tdo_mask;
-                                               }
-
-                                               dtc_mask >>= 1;
-                                               if (dtc_mask == 0) {
-                                                       dtc_p++;
-                                                       dtc_mask = 1 << (8 - 1);
-                                               }
-
-                                               tdo_mask <<= 1;
-                                               if (tdo_mask == 0) {
-                                                       tdo_p++;
-                                                       tdo_mask = 1;
-                                               }
-                                       }
-                               } else {
-                                       /*  extra bits or last bit */
+                       reply_buffer, sizeof(reply_buffer)
+                       );
+       if (usb_err < 0) {
+               LOG_ERROR("dtc_run_download: %s", usb_strerror());
+               exit(1);
+       }
 
-                                       x = *dtc_p++;
-                                       if ((
-                                               rq_p->scan.type == SCAN_IN
-) && (
-                                               rq_p->scan.offset != rq_p->scan.size - 1
-)) {
-                                               /* extra bits were sent as a full byte with padding on the end */
+       if (dtc_queue.rq_head != NULL) {
+               /* process the reply, which empties the reply queue and frees its entries */
+               dtc_p = reply_buffer;
+
+               /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
+                *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons.   It
+                *was that or craft a function to do the reversal, and that wouldn't work with
+                *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
+                *scheme which would throw the byte alignment off. */
+
+               for (
+                       rq_p = dtc_queue.rq_head;
+                       rq_p != NULL;
+                       rq_p = rq_next
+                       ) {
+                       tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
+                       tdo_mask = 1 << (rq_p->scan.offset % 8);
+
+
+                       bit_cnt = rq_p->scan.length;
+                       if (bit_cnt >= 8) {
+                               /* bytes */
+
+                               dtc_mask = 1 << (8 - 1);
+
+                               for (
+                                       ;
+                                       bit_cnt;
+                                       bit_cnt--
+                                       ) {
+                                       if (*dtc_p & dtc_mask)
+                                               *tdo_p |= tdo_mask;
+                                       else
+                                               *tdo_p &= ~tdo_mask;
+
+                                       dtc_mask >>= 1;
+                                       if (dtc_mask == 0) {
+                                               dtc_p++;
                                                dtc_mask = 1 << (8 - 1);
-                                       } else {
-                                               dtc_mask = 1 << (bit_cnt - 1);
                                        }
 
-                                       for (
-                                               ;
-                                               bit_cnt;
-                                               bit_cnt--
-) {
-                                               if (x & dtc_mask) {
-                                                       *tdo_p |= tdo_mask;
-                                               } else {
-                                                       *tdo_p &=~ tdo_mask;
-                                               }
-
-                                               dtc_mask >>= 1;
-
-                                               tdo_mask <<= 1;
-                                               if (tdo_mask == 0) {
-                                                       tdo_p++;
-                                                       tdo_mask = 1;
-                                               }
-
+                                       tdo_mask <<= 1;
+                                       if (tdo_mask == 0) {
+                                               tdo_p++;
+                                               tdo_mask = 1;
                                        }
                                }
+                       } else {
+                               /*  extra bits or last bit */
 
-                               if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
-                                       /* feed scan buffer back into openocd and free it */
-                                       if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
-                                                retval = ERROR_JTAG_QUEUE_FAILED;
+                               x = *dtc_p++;
+                               if ((rq_p->scan.type == SCAN_IN) && (
+                                               rq_p->scan.offset != rq_p->scan.size - 1
+                                       )) {
+                                       /* extra bits were sent as a full byte with padding on the
+                                        *end */
+                                       dtc_mask = 1 << (8 - 1);
+                               } else
+                                       dtc_mask = 1 << (bit_cnt - 1);
+
+                               for (
+                                       ;
+                                       bit_cnt;
+                                       bit_cnt--
+                                       ) {
+                                       if (x & dtc_mask)
+                                               *tdo_p |= tdo_mask;
+                                       else
+                                               *tdo_p &= ~tdo_mask;
+
+                                       dtc_mask >>= 1;
+
+                                       tdo_mask <<= 1;
+                                       if (tdo_mask == 0) {
+                                               tdo_p++;
+                                               tdo_mask = 1;
                                        }
-                                       free(rq_p->scan.buffer);
+
                                }
+                       }
 
-                               rq_next = rq_p->next;
-                               free(rq_p);
+                       if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
+                               /* feed scan buffer back into openocd and free it */
+                               if (jtag_read_buffer(rq_p->scan.buffer,
+                                               rq_p->cmd->cmd.scan) != ERROR_OK)
+                                       retval = ERROR_JTAG_QUEUE_FAILED;
+                               free(rq_p->scan.buffer);
                        }
-                       dtc_queue.rq_head = NULL;
-                       dtc_queue.rq_tail = NULL;
-               }
 
+                       rq_next = rq_p->next;
+                       free(rq_p);
+               }
+               dtc_queue.rq_head = NULL;
+               dtc_queue.rq_tail = NULL;
        }
 
-
        /* reset state for new appends */
        dtc_queue.cmd_index = 0;
        dtc_queue.reply_index = 0;
 
-       return(retval);
+       return retval;
 }
 
+/* runs the queue if it cannot take reserved_cmd bytes of command data
+ * or reserved_reply bytes of reply data */
+static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
+{
+       /* reserve one additional byte for the STOP cmd appended during run */
+       if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
+               return dtc_queue_run();
+
+       if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
+               return dtc_queue_run();
 
+       return ERROR_OK;
+}
 
-static
-int
-tap_state_queue_init(void) {
+static int tap_state_queue_init(void)
+{
        tap_state_queue.length = 0;
        tap_state_queue.buffer = 0;
-       return(0);
+       return 0;
 }
 
-
-static
-int
-tap_state_queue_run(void) {
-       int     i;
-       int     bits;
-       uint8_t byte;
-       int     retval;
+static int tap_state_queue_run(void)
+{
+       int i;
+       int bits;
+       uint8_t byte_param;
+       int retval;
 
        retval = 0;
-       if (!tap_state_queue.length) return(retval);
+       if (!tap_state_queue.length)
+               return retval;
        bits = 1;
-       byte = 0;
-       for (i = tap_state_queue.length; i--;) {
+       byte_param = 0;
+       for (i = tap_state_queue.length; i--; ) {
 
-               byte <<= 1;
-               if (tap_state_queue.buffer & 1) {
-                       byte |= 1;
-               }
+               byte_param <<= 1;
+               if (tap_state_queue.buffer & 1)
+                       byte_param |= 1;
                if ((bits >= 8) || !i) {
-                       byte <<= (8 - bits);
+                       byte_param <<= (8 - bits);
 
-                       /* make sure there's room for stop, byte op, and one byte */
-                       if (dtc_queue.cmd_index >= (sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))) {
-                               dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
-                                       DTC_CMD_STOP;
-                               dtc_queue_run();
-                       }
+                       /* make sure there's room for two cmd bytes */
+                       dtc_queue_run_if_full(2, 0);
 
 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
                        if (bits == 8) {
@@ -853,71 +800,61 @@ tap_state_queue_run(void) {
                                        DTC_CMD_SHIFT_TMS_BYTES(1);
                        } else {
 #endif
-                               dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
-                                       DTC_CMD_SHIFT_TMS_BITS(bits);
+                       dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
+                               DTC_CMD_SHIFT_TMS_BITS(bits);
 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
-                       }
+               }
 #endif
 
                        dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
-                               byte;
+                               byte_param;
 
-                       byte = 0;
+                       byte_param = 0;
                        bits = 1;
-               } else {
+               } else
                        bits++;
-               }
 
                tap_state_queue.buffer >>= 1;
        }
        retval = tap_state_queue_init();
-       return(retval);
+       return retval;
 }
 
-
-static
-int
-tap_state_queue_append(
-       uint8_t tms
-) {
-       int     retval;
+static int tap_state_queue_append(uint8_t tms)
+{
+       int retval;
 
        if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
                retval = tap_state_queue_run();
-               if (retval != 0) return(retval);
+               if (retval != 0)
+                       return retval;
        }
 
-       if (tms) {
+       if (tms)
                tap_state_queue.buffer |= (1 << tap_state_queue.length);
-       }
        tap_state_queue.length++;
 
-       return(0);
+       return 0;
 }
 
-
-static
-void rlink_end_state(tap_state_t state)
+static void rlink_end_state(tap_state_t state)
 {
        if (tap_is_state_stable(state))
                tap_set_end_state(state);
-       else
-       {
+       else {
                LOG_ERROR("BUG: %i is not a valid end state", state);
                exit(-1);
        }
 }
 
-
-static
-void rlink_state_move(void) {
+static void rlink_state_move(void)
+{
 
        int i = 0, tms = 0;
        uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
        int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
 
-       for (i = 0; i < tms_count; i++)
-       {
+       for (i = 0; i < tms_count; i++) {
                tms = (tms_scan >> i) & 1;
                tap_state_queue_append(tms);
        }
@@ -925,27 +862,22 @@ void rlink_state_move(void) {
        tap_set_state(tap_get_end_state());
 }
 
-static
-void rlink_path_move(struct pathmove_command *cmd)
+static void rlink_path_move(struct pathmove_command *cmd)
 {
        int num_states = cmd->num_states;
        int state_count;
        int tms = 0;
 
        state_count = 0;
-       while (num_states)
-       {
+       while (num_states) {
                if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
-               {
                        tms = 0;
-               }
                else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
-               {
                        tms = 1;
-               }
-               else
-               {
-                       LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
+               else {
+                       LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
+                               tap_state_name(tap_get_state()),
+                               tap_state_name(cmd->path[state_count]));
                        exit(-1);
                }
 
@@ -959,26 +891,21 @@ void rlink_path_move(struct pathmove_command *cmd)
        tap_set_end_state(tap_get_state());
 }
 
-
-static
-void rlink_runtest(int num_cycles)
+static void rlink_runtest(int num_cycles)
 {
        int i;
 
        tap_state_t saved_end_state = tap_get_end_state();
 
        /* only do a state_move when we're not already in RTI */
-       if (tap_get_state() != TAP_IDLE)
-       {
+       if (tap_get_state() != TAP_IDLE) {
                rlink_end_state(TAP_IDLE);
                rlink_state_move();
        }
 
        /* execute num_cycles */
        for (i = 0; i < num_cycles; i++)
-       {
                tap_state_queue_append(0);
-       }
 
        /* finish in end_state */
        rlink_end_state(saved_end_state);
@@ -986,138 +913,126 @@ void rlink_runtest(int num_cycles)
                rlink_state_move();
 }
 
-
 /* (1) assert or (0) deassert reset lines */
-static
-void rlink_reset(int trst, int srst)
+static void rlink_reset(int trst, int srst)
 {
-       uint8_t                 bitmap;
-       int                     usb_err;
+       uint8_t bitmap;
+       int usb_err;
 
        /* Read port A for bit op */
        usb_err = ep1_generic_commandl(
-               pHDev, 4,
-               EP1_CMD_MEMORY_READ,
+                       pHDev, 4,
+                       EP1_CMD_MEMORY_READ,
                        ST7_PADR >> 8,
                        ST7_PADR,
                        1
-);
+                       );
        if (usb_err < 0) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 
        usb_err = usb_bulk_read(
-               pHDev, USB_EP1IN_ADDR,
-               (char *)&bitmap, 1,
-               USB_TIMEOUT_MS
-);
+                       pHDev, USB_EP1IN_ADDR,
+                       (char *)&bitmap, 1,
+                       USB_TIMEOUT_MS
+                       );
        if (usb_err < 1) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 
-       if (trst) {
+       if (trst)
                bitmap &= ~ST7_PA_NTRST;
-       } else {
+       else
                bitmap |= ST7_PA_NTRST;
-       }
 
-       /* Write port A and read port B for bit op */
-       /* port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 and assert NSRST by setting DDR to 1. */
+       /* Write port A and read port B for bit op
+        * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
+        *and assert NSRST by setting DDR to 1. */
        usb_err = ep1_generic_commandl(
-               pHDev, 9,
-               EP1_CMD_MEMORY_WRITE,
+                       pHDev, 9,
+                       EP1_CMD_MEMORY_WRITE,
                        ST7_PADR >> 8,
                        ST7_PADR,
                        1,
-                       bitmap,
-               EP1_CMD_MEMORY_READ,
+                       bitmap,
+                       EP1_CMD_MEMORY_READ,
                        ST7_PBDDR >> 8,
                        ST7_PBDDR,
                        1
-);
+                       );
        if (usb_err < 0) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 
        usb_err = usb_bulk_read(
-               pHDev, USB_EP1IN_ADDR,
-               (char *)&bitmap, 1,
-               USB_TIMEOUT_MS
-);
+                       pHDev, USB_EP1IN_ADDR,
+                       (char *)&bitmap, 1,
+                       USB_TIMEOUT_MS
+                       );
        if (usb_err < 1) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 
-       if (srst) {
+       if (srst)
                bitmap |= ST7_PB_NSRST;
-       } else {
+       else
                bitmap &= ~ST7_PB_NSRST;
-       }
 
        /* write port B and read dummy to ensure completion before returning */
        usb_err = ep1_generic_commandl(
-               pHDev, 6,
-               EP1_CMD_MEMORY_WRITE,
+                       pHDev, 6,
+                       EP1_CMD_MEMORY_WRITE,
                        ST7_PBDDR >> 8,
                        ST7_PBDDR,
                        1,
                        bitmap,
-               EP1_CMD_DTC_GET_CACHED_STATUS
-);
+                       EP1_CMD_DTC_GET_CACHED_STATUS
+                       );
        if (usb_err < 0) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 
        usb_err = usb_bulk_read(
-               pHDev, USB_EP1IN_ADDR,
-               (char *)&bitmap, 1,
-               USB_TIMEOUT_MS
-);
+                       pHDev, USB_EP1IN_ADDR,
+                       (char *)&bitmap, 1,
+                       USB_TIMEOUT_MS
+                       );
        if (usb_err < 1) {
                LOG_ERROR("%s", usb_strerror());
                exit(1);
        }
 }
 
-
-static
-int
-rlink_scan(
-       struct jtag_command     *cmd,
-       enum scan_type  type,
-       uint8_t                 *buffer,
-       int                     scan_size
-) {
-       bool            ir_scan;
-       tap_state_t     saved_end_state;
-       int                     byte_bits;
-       int                     extra_bits;
-       int                     chunk_bits;
-       int                     chunk_bytes;
-       int                     x;
-
-       int                     tdi_bit_offset;
-       uint8_t                 tdi_mask, *tdi_p;
-       uint8_t                 dtc_mask;
+static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
+               uint8_t *buffer, int scan_size)
+{
+       bool ir_scan;
+       tap_state_t saved_end_state;
+       int byte_bits;
+       int extra_bits;
+       int chunk_bits;
+       int chunk_bytes;
+       int x;
+
+       int tdi_bit_offset;
+       uint8_t tdi_mask, *tdi_p;
+       uint8_t dtc_mask;
 
        if (scan_size < 1) {
-               LOG_ERROR("scan_size cannot be less than 1 bit\n");
+               LOG_ERROR("scan_size cannot be less than 1 bit");
                exit(1);
        }
 
        ir_scan = cmd->cmd.scan->ir_scan;
 
        /* Move to the proper state before starting to shift TDI/TDO. */
-       if (!(
-               (!ir_scan && (tap_get_state() == TAP_DRSHIFT))
-               ||
-               (ir_scan && (tap_get_state() == TAP_IRSHIFT))
-)) {
+       if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
+                       (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
                saved_end_state = tap_get_end_state();
                rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
                rlink_state_move();
@@ -1130,16 +1045,15 @@ rlink_scan(
 #if 0
        printf("scan_size = %d, type = 0x%x\n", scan_size, type);
        {
-               int   i;
+               int i;
 
-               /* clear unused bits in scan buffer for ease of debugging */
-               /* (it makes diffing output easier) */
+               /* clear unused bits in scan buffer for ease of debugging
+                * (it makes diffing output easier) */
                buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
 
                printf("before scan:");
-               for (i = 0; i < (scan_size + 7) / 8; i++) {
+               for (i = 0; i < (scan_size + 7) / 8; i++)
                        printf(" %02x", buffer[i]);
-               }
                printf("\n");
        }
 #endif
@@ -1154,22 +1068,19 @@ rlink_scan(
        tdi_mask = 1;
 
        if (extra_bits && (type == SCAN_OUT)) {
-               /* Schedule any extra bits into the DTC command buffer, padding as needed */
-               /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
-               /* make sure there's room for stop, byte op, and one byte */
-               if (
-                       (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
-) {
-                       dtc_queue_run();
-               }
+               /* Schedule any extra bits into the DTC command buffer, padding as needed
+                * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
+                *fall off the end */
+
+               /* make sure there's room for two cmd bytes */
+               dtc_queue_run_if_full(2, 0);
 
                x = 0;
                dtc_mask = 1 << (extra_bits - 1);
 
                while (extra_bits--) {
-                       if (*tdi_p & tdi_mask) {
+                       if (*tdi_p & tdi_mask)
                                x |= dtc_mask;
-                       }
 
                        dtc_mask >>= 1;
 
@@ -1188,37 +1099,27 @@ rlink_scan(
 
        /* Loop scheduling full bytes into the DTC command buffer */
        while (byte_bits) {
-               if (type == SCAN_IN) {
-                       /* make sure there's room for stop and byte op */
-                       x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1));
-               } else {
-                       /* make sure there's room for stop, byte op, and at least one byte */
-                       x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1));
-               }
-
-               if (type != SCAN_OUT) {
-                       /* make sure there's room for at least one reply byte */
-                       x |= (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1));
-               }
-
-               if (x) {
-                       dtc_queue_run();
-               }
+               /* make sure there's room for one (for in scans) or two cmd bytes and
+                * at least one reply byte for in or inout scans*/
+               dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
 
                chunk_bits = byte_bits;
                /* we can only use up to 16 bytes at a time */
-               if (chunk_bits > (16 * 8)) chunk_bits = (16 * 8);
+               if (chunk_bits > (16 * 8))
+                       chunk_bits = (16 * 8);
 
                if (type != SCAN_IN) {
                        /* how much is there room for, considering stop and byte op? */
                        x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
-                       if (chunk_bits > x) chunk_bits = x;
+                       if (chunk_bits > x)
+                               chunk_bits = x;
                }
 
                if (type != SCAN_OUT) {
                        /* how much is there room for in the reply buffer? */
                        x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
-                       if (chunk_bits > x) chunk_bits = x;
+                       if (chunk_bits > x)
+                               chunk_bits = x;
                }
 
                /* so the loop will end */
@@ -1226,13 +1127,14 @@ rlink_scan(
 
                if (type != SCAN_OUT) {
                        if (dtc_queue_enqueue_reply(
-                               type, buffer, scan_size, tdi_bit_offset,
-                               chunk_bits,
-                               cmd
-) == NULL) {
-                               LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
+                                       type, buffer, scan_size, tdi_bit_offset,
+                                       chunk_bits,
+                                       cmd
+                               ) == NULL) {
+                               LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
                                exit(1);
                        }
+                       dtc_queue.reply_index += (chunk_bits + 7) / 8;
 
                        tdi_bit_offset += chunk_bits;
                }
@@ -1258,14 +1160,12 @@ rlink_scan(
                        dtc_mask = 1 << (8 - 1);
 
                        while (chunk_bits--) {
-                               if (*tdi_p & tdi_mask) {
+                               if (*tdi_p & tdi_mask)
                                        x |= dtc_mask;
-                               }
 
                                dtc_mask >>= 1;
                                if (dtc_mask == 0) {
                                        dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
-                                       dtc_queue.reply_index++;
                                        x = 0;
                                        dtc_mask = 1 << (8 - 1);
                                }
@@ -1281,24 +1181,22 @@ rlink_scan(
 
        if (extra_bits && (type != SCAN_OUT)) {
                /* Schedule any extra bits into the DTC command buffer */
-               /* make sure there's room for stop, byte op, and one byte */
-               if (
-                       (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
-                       ||
-                       (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
-) {
-                       dtc_queue_run();
-               }
+
+               /* make sure there's room for one (for in scans) or two cmd bytes
+                * and one reply byte */
+               dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
 
                if (dtc_queue_enqueue_reply(
-                       type, buffer, scan_size, tdi_bit_offset,
-                       extra_bits,
-                       cmd
-) == NULL) {
-                       LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
+                               type, buffer, scan_size, tdi_bit_offset,
+                               extra_bits,
+                               cmd
+                       ) == NULL) {
+                       LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
                        exit(1);
                }
 
+               dtc_queue.reply_index++;
+
                tdi_bit_offset += extra_bits;
 
                if (type == SCAN_IN) {
@@ -1313,9 +1211,8 @@ rlink_scan(
                        dtc_mask = 1 << (8 - 1);
 
                        while (extra_bits--) {
-                               if (*tdi_p & tdi_mask) {
+                               if (*tdi_p & tdi_mask)
                                        x |= dtc_mask;
-                               }
 
                                dtc_mask >>= 1;
 
@@ -1328,55 +1225,46 @@ rlink_scan(
 
                        dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
                }
-
-               dtc_queue.reply_index++;
        }
 
        /* Schedule the last bit into the DTC command buffer */
-       {
-               /* make sure there's room for stop, and bit pair command */
-               if (
-                       (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1))
-                       ||
-                       (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
-) {
-                       dtc_queue_run();
-               }
 
-               if (type == SCAN_OUT) {
-                       dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
-                               DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
+       /* make sure there's room for one cmd byte and one reply byte
+        * for in or inout scans*/
+       dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
 
-               } else {
-                       if (dtc_queue_enqueue_reply(
+       if (type == SCAN_OUT) {
+               dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
+                       DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
+
+       } else {
+               if (dtc_queue_enqueue_reply(
                                type, buffer, scan_size, tdi_bit_offset,
                                1,
                                cmd
-) == NULL) {
-                               LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                               ) == NULL) {
+                       LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
+                       exit(1);
+               }
 
-                       dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
-                               DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
+               dtc_queue.reply_index++;
 
-                       dtc_queue.reply_index++;
-               }
+               dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
+                       DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
        }
 
        /* Move to pause state */
        tap_state_queue_append(0);
        tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
-       if (tap_get_state() != tap_get_end_state()) rlink_state_move();
+       if (tap_get_state() != tap_get_end_state())
+               rlink_state_move();
 
-       return(0);
+       return 0;
 }
 
-
-static
-int rlink_execute_queue(void)
+static int rlink_execute_queue(void)
 {
-       struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
+       struct jtag_command *cmd = jtag_command_queue;  /* currently processed command */
        int scan_size;
        enum scan_type type;
        uint8_t *buffer;
@@ -1390,13 +1278,11 @@ int rlink_execute_queue(void)
        ep1_generic_commandl(pHDev, 2,
                EP1_CMD_SET_PORTD_LEDS,
                ~(ST7_PD_NBUSY_LED)
-);
+               );
 #endif
 
-       while (cmd)
-       {
-               switch (cmd->type)
-               {
+       while (cmd) {
+               switch (cmd->type) {
                        case JTAG_RUNTEST:
                        case JTAG_TLR_RESET:
                        case JTAG_PATHMOVE:
@@ -1404,62 +1290,57 @@ int rlink_execute_queue(void)
                                break;
 
                        default:
-                               /* some events, such as resets, need a queue flush to ensure consistency */
+                               /* some events, such as resets, need a queue flush to ensure
+                                *consistency */
                                tap_state_queue_run();
                                dtc_queue_run();
                                break;
                }
 
-               switch (cmd->type)
-               {
+               switch (cmd->type) {
                        case JTAG_RESET:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
-#endif
-                               if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
-                               {
+                               LOG_DEBUG_IO("reset trst: %i srst %i",
+                                               cmd->cmd.reset->trst,
+                                               cmd->cmd.reset->srst);
+                               if ((cmd->cmd.reset->trst == 1) ||
+                                               (cmd->cmd.reset->srst &&
+                                                               (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
                                        tap_set_state(TAP_RESET);
-                               }
                                rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
                                break;
                        case JTAG_RUNTEST:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
-#endif
+                               LOG_DEBUG_IO("runtest %i cycles, end in %i",
+                                               cmd->cmd.runtest->num_cycles,
+                                               cmd->cmd.runtest->end_state);
                                if (cmd->cmd.runtest->end_state != -1)
                                        rlink_end_state(cmd->cmd.runtest->end_state);
                                rlink_runtest(cmd->cmd.runtest->num_cycles);
                                break;
                        case JTAG_TLR_RESET:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
-#endif
+                               LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
                                if (cmd->cmd.statemove->end_state != -1)
                                        rlink_end_state(cmd->cmd.statemove->end_state);
                                rlink_state_move();
                                break;
                        case JTAG_PATHMOVE:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
-#endif
+                               LOG_DEBUG_IO("pathmove: %i states, end in %i",
+                                               cmd->cmd.pathmove->num_states,
+                                               cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
                                rlink_path_move(cmd->cmd.pathmove);
                                break;
                        case JTAG_SCAN:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("%s scan end in %i",  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
-#endif
+                               LOG_DEBUG_IO("%s scan end in %i",
+                                               (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
+                                                               cmd->cmd.scan->end_state);
                                if (cmd->cmd.scan->end_state != -1)
                                        rlink_end_state(cmd->cmd.scan->end_state);
                                scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
                                type = jtag_scan_type(cmd->cmd.scan);
-                               if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) {
+                               if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
                                        retval = ERROR_FAIL;
-                               }
                                break;
                        case JTAG_SLEEP:
-#ifdef _DEBUG_JTAG_IO_
-                               LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
-#endif
+                               LOG_DEBUG_IO("sleep %i", cmd->cmd.sleep->us);
                                jtag_sleep(cmd->cmd.sleep->us);
                                break;
                        default:
@@ -1469,49 +1350,48 @@ int rlink_execute_queue(void)
                cmd = cmd->next;
        }
 
-       /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
+       /* Flush the DTC queue to make sure any pending reads have been done before exiting this
+        *function */
        tap_state_queue_run();
        tmp_retval = dtc_queue_run();
-       if (tmp_retval != ERROR_OK) {
+       if (tmp_retval != ERROR_OK)
                retval = tmp_retval;
-       }
 
 #ifndef AUTOMATIC_BUSY_LED
        /* turn LED onff */
        ep1_generic_commandl(pHDev, 2,
                EP1_CMD_SET_PORTD_LEDS,
                ~0
-);
+               );
 #endif
 
        return retval;
 }
 
+/* Using an unindexed table because it is infrequently accessed and it is short.  The table must be
+ *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
 
-/* Using an unindexed table because it is infrequently accessed and it is short.  The table must be in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
-
-static
-int rlink_speed(int speed)
+static int rlink_speed(int speed)
 {
-       int             i;
+       int i;
 
        if (speed == 0) {
                /* fastest speed */
                speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
        }
 
-       for (i = rlink_speed_table_size; i--;) {
+       for (i = rlink_speed_table_size; i--; ) {
                if (rlink_speed_table[i].prescaler == speed) {
-                       if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) {
-                               LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".\n", speed);
+                       if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc,
+                                       rlink_speed_table[i].dtc_size) != 0) {
+                               LOG_ERROR(
+                                       "An error occurred while trying to load DTC code for speed \"%d\".",
+                                       speed);
                                exit(1);
                        }
 
                        if (dtc_start_download() < 0) {
-                               LOG_ERROR("%s, %d: starting DTC: %s",
-                                       __FILE__, __LINE__,
-                                       usb_strerror()
-);
+                               LOG_ERROR("starting DTC: %s", usb_strerror());
                                exit(1);
                        }
 
@@ -1520,56 +1400,46 @@ int rlink_speed(int speed)
        }
 
        LOG_ERROR("%d is not a supported speed", speed);
-       return(ERROR_FAIL);
+       return ERROR_FAIL;
 }
 
+static int rlink_speed_div(int speed, int *khz)
+{
+       int i;
 
-static
-int rlink_speed_div(
-       int speed,
-       int *khz
-) {
-       int     i;
-
-       for (i = rlink_speed_table_size; i--;) {
+       for (i = rlink_speed_table_size; i--; ) {
                if (rlink_speed_table[i].prescaler == speed) {
                        *khz = rlink_speed_table[i].khz;
-                       return(ERROR_OK);
+                       return ERROR_OK;
                }
        }
 
        LOG_ERROR("%d is not a supported speed", speed);
-       return(ERROR_FAIL);
+       return ERROR_FAIL;
 }
 
-
-static
-int rlink_khz(
-       int khz,
-       int *speed
-) {
-       int     i;
+static int rlink_khz(int khz, int *speed)
+{
+       int i;
 
        if (khz == 0) {
                LOG_ERROR("RCLK not supported");
                return ERROR_FAIL;
        }
 
-       for (i = rlink_speed_table_size; i--;) {
+       for (i = rlink_speed_table_size; i--; ) {
                if (rlink_speed_table[i].khz <= khz) {
                        *speed = rlink_speed_table[i].prescaler;
-                       return(ERROR_OK);
+                       return ERROR_OK;
                }
        }
 
        LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
        *speed = rlink_speed_table[0].prescaler;
-       return(ERROR_OK);
+       return ERROR_OK;
 }
 
-
-static
-int rlink_init(void)
+static int rlink_init(void)
 {
        int i, j, retries;
        uint8_t reply_buffer[USB_EP1IN_SIZE];
@@ -1581,50 +1451,42 @@ int rlink_init(void)
                return ERROR_FAIL;
 
        struct usb_device *dev = usb_device(pHDev);
-       if (dev->descriptor.bNumConfigurations > 1)
-       {
-               LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
+       if (dev->descriptor.bNumConfigurations > 1) {
+               LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
                return ERROR_FAIL;
        }
-       if (dev->config->bNumInterfaces > 1)
-       {
-               LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
+       if (dev->config->bNumInterfaces > 1) {
+               LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
                return ERROR_FAIL;
        }
 
-       LOG_DEBUG("Opened device, pHDev = %p\n", pHDev);
+       LOG_DEBUG("Opened device, pHDev = %p", pHDev);
 
        /* usb_set_configuration required under win32 */
        usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
 
        retries = 3;
-       do
-       {
-               i = usb_claim_interface(pHDev,0);
-               if (i)
-               {
+       do {
+               i = usb_claim_interface(pHDev, 0);
+               if (i) {
                        LOG_ERROR("usb_claim_interface: %s", usb_strerror());
 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
                        j = usb_detach_kernel_driver_np(pHDev, 0);
                        if (j)
                                LOG_ERROR("detach kernel driver: %s", usb_strerror());
 #endif
-               }
-               else
-               {
-                       LOG_DEBUG("interface claimed!\n");
+               } else {
+                       LOG_DEBUG("interface claimed!");
                        break;
                }
        } while (--retries);
 
-       if (i)
-       {
+       if (i) {
                LOG_ERROR("Initialisation failed.");
                return ERROR_FAIL;
        }
-       if (usb_set_altinterface(pHDev,0) != 0)
-       {
-               LOG_ERROR("Failed to set interface.\n");
+       if (usb_set_altinterface(pHDev, 0) != 0) {
+               LOG_ERROR("Failed to set interface.");
                return ERROR_FAIL;
        }
 
@@ -1639,123 +1501,126 @@ int rlink_init(void)
         */
        for (i = 0; i < 5; i++) {
                j = ep1_generic_commandl(
-                       pHDev, 1,
-                       EP1_CMD_GET_FWREV
-);
+                               pHDev, 1,
+                               EP1_CMD_GET_FWREV
+                               );
                if (j < USB_EP1OUT_SIZE) {
                        LOG_ERROR("USB write error: %s", usb_strerror());
-                       return(ERROR_FAIL);
+                       return ERROR_FAIL;
                }
                j = usb_bulk_read(
-                       pHDev, USB_EP1IN_ADDR,
-                       (char *)reply_buffer, sizeof(reply_buffer),
-                       200
-);
-               if (j != -ETIMEDOUT) break;
+                               pHDev, USB_EP1IN_ADDR,
+                               (char *)reply_buffer, sizeof(reply_buffer),
+                               200
+                               );
+               if (j != -ETIMEDOUT)
+                       break;
        }
 
        if (j < (int)sizeof(reply_buffer)) {
                LOG_ERROR("USB read error: %s", usb_strerror());
-               return(ERROR_FAIL);
+               return ERROR_FAIL;
        }
-       LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d\n", reply_buffer[0], reply_buffer[1], reply_buffer[2]);
+       LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
+               reply_buffer[0],
+               reply_buffer[1],
+               reply_buffer[2]);
 
-       if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) {
-               LOG_WARNING("The rlink device is not of the version that the developers have played with.  It may or may not work.\n");
-       }
+       if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
+               LOG_WARNING(
+                       "The rlink device is not of the version that the developers have played with.  It may or may not work.");
 
        /* Probe port E for adapter presence */
        ep1_generic_commandl(
                pHDev, 16,
-               EP1_CMD_MEMORY_WRITE,   /* Drive sense pin with 0 */
-                       ST7_PEDR >> 8,
-                       ST7_PEDR,
-                       3,
-                       0x00,                                           /* DR */
-                       ST7_PE_ADAPTER_SENSE_OUT,       /* DDR */
-                       ST7_PE_ADAPTER_SENSE_OUT,       /* OR */
-               EP1_CMD_MEMORY_READ,    /* Read back */
-                       ST7_PEDR >> 8,
-                       ST7_PEDR,
-                       1,
-               EP1_CMD_MEMORY_WRITE,   /* Drive sense pin with 1 */
-                       ST7_PEDR >> 8,
-                       ST7_PEDR,
-                       1,
-                       ST7_PE_ADAPTER_SENSE_OUT
-);
+               EP1_CMD_MEMORY_WRITE,           /* Drive sense pin with 0 */
+               ST7_PEDR >> 8,
+               ST7_PEDR,
+               3,
+               0x00,                                                   /* DR */
+               ST7_PE_ADAPTER_SENSE_OUT,               /* DDR */
+               ST7_PE_ADAPTER_SENSE_OUT,               /* OR */
+               EP1_CMD_MEMORY_READ,            /* Read back */
+               ST7_PEDR >> 8,
+               ST7_PEDR,
+               1,
+               EP1_CMD_MEMORY_WRITE,           /* Drive sense pin with 1 */
+               ST7_PEDR >> 8,
+               ST7_PEDR,
+               1,
+               ST7_PE_ADAPTER_SENSE_OUT
+               );
 
        usb_bulk_read(
                pHDev, USB_EP1IN_ADDR,
                (char *)reply_buffer, 1,
                USB_TIMEOUT_MS
-);
+               );
 
-       if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) {
-               LOG_WARNING("target detection problem\n");
-       }
+       if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
+               LOG_WARNING("target detection problem");
 
        ep1_generic_commandl(
                pHDev, 11,
-               EP1_CMD_MEMORY_READ,    /* Read back */
-                       ST7_PEDR >> 8,
-                       ST7_PEDR,
-                       1,
-               EP1_CMD_MEMORY_WRITE,   /* float port E */
-                       ST7_PEDR >> 8,
-                       ST7_PEDR,
-                       3,
-                       0x00,   /* DR */
-                       0x00,   /* DDR */
-                       0x00    /* OR */
-);
+               EP1_CMD_MEMORY_READ,            /* Read back */
+               ST7_PEDR >> 8,
+               ST7_PEDR,
+               1,
+               EP1_CMD_MEMORY_WRITE,           /* float port E */
+               ST7_PEDR >> 8,
+               ST7_PEDR,
+               3,
+               0x00,           /* DR */
+               0x00,           /* DDR */
+               0x00            /* OR */
+               );
 
        usb_bulk_read(
                pHDev, USB_EP1IN_ADDR,
                (char *)reply_buffer, 1,
                USB_TIMEOUT_MS
-);
+               );
 
 
-       if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) {
-               LOG_WARNING("target not plugged in\n");
-       }
+       if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
+               LOG_WARNING("target not plugged in");
 
        /* float ports A and B */
        ep1_generic_commandl(
                pHDev, 11,
                EP1_CMD_MEMORY_WRITE,
-                       ST7_PADDR >> 8,
-                       ST7_PADDR,
-                       2,
-                       0x00,
-                       0x00,
+               ST7_PADDR >> 8,
+               ST7_PADDR,
+               2,
+               0x00,
+               0x00,
                EP1_CMD_MEMORY_WRITE,
-                       ST7_PBDDR >> 8,
-                       ST7_PBDDR,
-                       1,
-                       0x00
-);
+               ST7_PBDDR >> 8,
+               ST7_PBDDR,
+               1,
+               0x00
+               );
 
        /* make sure DTC is stopped, set VPP control, set up ports A and B */
        ep1_generic_commandl(
                pHDev, 14,
                EP1_CMD_DTC_STOP,
                EP1_CMD_SET_PORTD_VPP,
-                       ~(ST7_PD_VPP_SHDN),
+               ~(ST7_PD_VPP_SHDN),
                EP1_CMD_MEMORY_WRITE,
-                       ST7_PADR >> 8,
-                       ST7_PADR,
-                       2,
-                       ((~(0)) & (ST7_PA_NTRST)),
-                       (ST7_PA_NTRST),
-       /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 here and later assert NSRST by setting DDR bit to 1. */
+               ST7_PADR >> 8,
+               ST7_PADR,
+               2,
+               ((~(0)) & (ST7_PA_NTRST)),
+               (ST7_PA_NTRST),
+               /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
+                *here and later assert NSRST by setting DDR bit to 1. */
                EP1_CMD_MEMORY_WRITE,
-                       ST7_PBDR >> 8,
-                       ST7_PBDR,
-                       1,
-                       0x00
-);
+               ST7_PBDR >> 8,
+               ST7_PBDR,
+               1,
+               0x00
+               );
 
        /* set LED updating mode and make sure they're unlit */
        ep1_generic_commandl(
@@ -1766,20 +1631,17 @@ int rlink_init(void)
                EP1_CMD_LEDUE_NONE,
 #endif
                EP1_CMD_SET_PORTD_LEDS,
-                       ~0
-);
+               ~0
+               );
 
        tap_state_queue_init();
        dtc_queue_init();
-       rlink_speed(jtag_get_speed());
        rlink_reset(0, 0);
 
        return ERROR_OK;
 }
 
-
-static
-int rlink_quit(void)
+static int rlink_quit(void)
 {
        /* stop DTC and make sure LEDs are off */
        ep1_generic_commandl(
@@ -1787,21 +1649,18 @@ int rlink_quit(void)
                EP1_CMD_DTC_STOP,
                EP1_CMD_LEDUE_NONE,
                EP1_CMD_SET_PORTD_LEDS,
-                       ~0,
+               ~0,
                EP1_CMD_SET_PORTD_VPP,
-                       ~0
-);
+               ~0
+               );
 
-       usb_release_interface(pHDev,0);
+       usb_release_interface(pHDev, 0);
        usb_close(pHDev);
 
-
        return ERROR_OK;
 }
 
-
-struct jtag_interface rlink_interface =
-{
+struct jtag_interface rlink_interface = {
        .name = "rlink",
        .init = rlink_init,
        .quit = rlink_quit,

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)