]> git.sur5r.net Git - openocd/blob - src/jtag/usbprog.c
dfde29551f2f9548e79763947c47763d11ef4e80
[openocd] / src / jtag / usbprog.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Benedikt Sauter                                 *
3  *   sauter@ixbat.de                                                       *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 /*
22  * This file is based on Dominic Rath's amt_jtagaccel.c.
23  *
24  * usbprog is a free programming adapter. You can easily install
25  * different firmware versions from an "online pool" over USB.
26  * The adapter can be used for programming and debugging AVR and ARM
27  * processors, as USB to RS232 converter, as JTAG interface or as
28  * simple I/O interface (5 lines).
29  *
30  * http://www.embedded-projects.net/usbprog
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "jtag.h"
38
39 #include <usb.h>
40
41
42 #define VID 0x1781
43 #define PID 0x0c63
44
45 /* Pins at usbprog */
46 #define TDO_BIT                 0
47 #define TDI_BIT                 3
48 #define TCK_BIT                 2
49 #define TMS_BIT                 1
50
51 static int usbprog_execute_queue(void);
52 static int usbprog_speed(int speed);
53 static int usbprog_register_commands(struct command_context_s *cmd_ctx);
54 static int usbprog_init(void);
55 static int usbprog_quit(void);
56
57 static void usbprog_end_state(tap_state_t state);
58 static void usbprog_state_move(void);
59 static void usbprog_path_move(pathmove_command_t *cmd);
60 static void usbprog_runtest(int num_cycles);
61 static void usbprog_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size);
62
63 jtag_interface_t usbprog_interface =
64 {
65         .name = "usbprog",
66         .execute_queue = usbprog_execute_queue,
67         .speed = usbprog_speed,
68         .register_commands = usbprog_register_commands,
69         .init = usbprog_init,
70         .quit = usbprog_quit
71 };
72
73 #define UNKOWN_COMMAND  0x00
74 #define PORT_DIRECTION  0x01
75 #define PORT_SET                0x02
76 #define PORT_GET                0x03
77 #define PORT_SETBIT             0x04
78 #define PORT_GETBIT             0x05
79 #define WRITE_TDI               0x06
80 #define READ_TDO                0x07
81 #define WRITE_AND_READ  0x08
82 #define WRITE_TMS               0x09
83 #define WRITE_TMS_CHAIN 0x0A
84
85 struct usbprog_jtag
86 {
87         struct usb_dev_handle* usb_handle;
88 };
89
90 static struct usbprog_jtag * usbprog_jtag_handle;
91
92 static struct usbprog_jtag* usbprog_jtag_open(void);
93 //static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag);
94 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
95 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
96
97 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
98 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
99 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
100 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
101
102 static char tms_chain[64];
103 static int tms_chain_index;
104
105 static void usbprog_jtag_tms_collect(char tms_scan);
106 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
107
108 static void usbprog_write(int tck, int tms, int tdi);
109 static void usbprog_reset(int trst, int srst);
110
111 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
112 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value);
113 //static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag);
114 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value);
115 //static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit);
116
117 static int usbprog_speed(int speed)
118 {
119         return ERROR_OK;
120 }
121
122 static int usbprog_register_commands(struct command_context_s *cmd_ctx)
123 {
124         return ERROR_OK;
125 }
126
127 static int usbprog_execute_queue(void)
128 {
129         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
130         int scan_size;
131         enum scan_type type;
132         u8 *buffer;
133
134         while (cmd)
135         {
136                 switch (cmd->type)
137                 {
138                         case JTAG_RESET:
139 #ifdef _DEBUG_JTAG_IO_
140                                 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
141 #endif
142                                 if (cmd->cmd.reset->trst == 1)
143                                 {
144                                         tap_set_state(TAP_RESET);
145                                 }
146                                 usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
147                                 break;
148                         case JTAG_RUNTEST:
149 #ifdef _DEBUG_JTAG_IO_
150                                 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
151 #endif
152                                 if (cmd->cmd.runtest->end_state != TAP_INVALID)
153                                         usbprog_end_state(cmd->cmd.runtest->end_state);
154                                 usbprog_runtest(cmd->cmd.runtest->num_cycles);
155                                 break;
156                         case JTAG_STATEMOVE:
157 #ifdef _DEBUG_JTAG_IO_
158                                 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
159 #endif
160                                 if (cmd->cmd.statemove->end_state != TAP_INVALID)
161                                         usbprog_end_state(cmd->cmd.statemove->end_state);
162                                 usbprog_state_move();
163                                 break;
164                         case JTAG_PATHMOVE:
165 #ifdef _DEBUG_JTAG_IO_
166                                 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
167                                         cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
168 #endif
169                                 usbprog_path_move(cmd->cmd.pathmove);
170                                 break;
171                         case JTAG_SCAN:
172 #ifdef _DEBUG_JTAG_IO_
173                                 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
174 #endif
175                                 if (cmd->cmd.scan->end_state != TAP_INVALID)
176                                         usbprog_end_state(cmd->cmd.scan->end_state);
177                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
178                                 type = jtag_scan_type(cmd->cmd.scan);
179                                 usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
180                                 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
181                                         return ERROR_JTAG_QUEUE_FAILED;
182                                 if (buffer)
183                                         free(buffer);
184                                 break;
185                         case JTAG_SLEEP:
186 #ifdef _DEBUG_JTAG_IO_
187                                 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
188 #endif
189                                 jtag_sleep(cmd->cmd.sleep->us);
190                                         break;
191                         default:
192                                 LOG_ERROR("BUG: unknown JTAG command type encountered");
193                                 exit(-1);
194                 }
195
196                 cmd = cmd->next;
197         }
198
199         return ERROR_OK;
200 }
201
202 static int usbprog_init(void)
203 {
204         usbprog_jtag_handle = usbprog_jtag_open();
205
206         tms_chain_index = 0;
207         if (usbprog_jtag_handle == 0)
208         {
209                 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
210                 return ERROR_JTAG_INIT_FAILED;
211         }
212
213         LOG_INFO("USB JTAG Interface ready!");
214
215         usbprog_jtag_init(usbprog_jtag_handle);
216         usbprog_reset(0, 0);
217         usbprog_write(0, 0, 0);
218
219         return ERROR_OK;
220 }
221
222 static int usbprog_quit(void)
223 {
224         return ERROR_OK;
225 }
226
227 /*************** jtag execute commands **********************/
228 static void usbprog_end_state(tap_state_t state)
229 {
230         if (tap_is_state_stable(state))
231                 tap_set_end_state(state);
232         else
233         {
234                 LOG_ERROR("BUG: %i is not a valid end state", state);
235                 exit(-1);
236         }
237 }
238
239 static void usbprog_state_move(void)
240 {
241         int i = 0, tms = 0;
242         u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
243         int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
244
245         usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
246         for (i = 0; i < tms_count; i++)
247         {
248                 tms = (tms_scan >> i) & 1;
249         }
250
251         tap_set_state(tap_get_end_state());
252 }
253
254 static void usbprog_path_move(pathmove_command_t *cmd)
255 {
256         int num_states = cmd->num_states;
257         int state_count;
258
259         /* There may be queued transitions, and before following a specified
260            path, we must flush those queued transitions */
261         usbprog_jtag_tms_send(usbprog_jtag_handle);
262
263         state_count = 0;
264         while (num_states)
265         {
266                 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
267                 {
268                         /* LOG_INFO("1"); */
269                         usbprog_write(0, 0, 0);
270                         usbprog_write(1, 0, 0);
271                 }
272                 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
273                 {
274                         /* LOG_INFO("2"); */
275                         usbprog_write(0, 1, 0);
276                         usbprog_write(1, 1, 0);
277                 }
278                 else
279                 {
280                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
281                         exit(-1);
282                 }
283
284                 tap_set_state(cmd->path[state_count]);
285                 state_count++;
286                 num_states--;
287         }
288
289         tap_set_end_state(tap_get_state());
290 }
291
292 static void usbprog_runtest(int num_cycles)
293 {
294         int i;
295
296         /* only do a state_move when we're not already in IDLE */
297         if (tap_get_state() != TAP_IDLE)
298         {
299                 usbprog_end_state(TAP_IDLE);
300                 usbprog_state_move();
301         }
302
303         /* execute num_cycles */
304         if (num_cycles > 0)
305         {
306                 usbprog_jtag_tms_send(usbprog_jtag_handle);
307                 usbprog_write(0, 0, 0);
308         }
309         else
310         {
311                 usbprog_jtag_tms_send(usbprog_jtag_handle);
312                 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
313         }
314
315         for (i = 0; i < num_cycles; i++)
316         {
317                 usbprog_write(1, 0, 0);
318                 usbprog_write(0, 0, 0);
319         }
320
321 #ifdef _DEBUG_JTAG_IO_
322         LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()));
323 #endif
324
325         /* finish in end_state */
326         /*
327         usbprog_end_state(saved_end_state);
328         if (tap_get_state() != tap_get_end_state())
329                 usbprog_state_move();
330         */
331 }
332
333 static void usbprog_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size)
334 {
335         tap_state_t saved_end_state = tap_get_end_state();
336
337         if (ir_scan)
338                 usbprog_end_state(TAP_IRSHIFT);
339         else
340                 usbprog_end_state(TAP_DRSHIFT);
341
342         /* Only move if we're not already there */
343         if (tap_get_state() != tap_get_end_state())
344                 usbprog_state_move();
345
346         usbprog_end_state(saved_end_state);
347
348         usbprog_jtag_tms_send(usbprog_jtag_handle);
349
350         void (*f)(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
351         switch (type) {
352         case SCAN_OUT: f = &usbprog_jtag_write_tdi; break;
353         case SCAN_IN: f = &usbprog_jtag_read_tdo; break;
354         case SCAN_IO: f = &usbprog_jtag_write_and_read; break;
355         default:
356                 LOG_ERROR("unknown scan type: %i", type);
357                 exit(-1);
358         }
359         f(usbprog_jtag_handle, (char *)buffer, scan_size);
360
361         /* The adapter does the transition to PAUSE internally */
362         if (ir_scan)
363                 tap_set_state(TAP_IRPAUSE);
364         else
365                 tap_set_state(TAP_DRPAUSE);
366
367         if (tap_get_state() != tap_get_end_state())
368                 usbprog_state_move();
369 }
370
371 /*************** jtag wrapper functions *********************/
372
373 static void usbprog_write(int tck, int tms, int tdi)
374 {
375         unsigned char output_value=0x00;
376
377         if (tms)
378                 output_value |= (1<<TMS_BIT);
379         if (tdi)
380                 output_value |= (1<<TDI_BIT);
381         if (tck)
382                 output_value |= (1<<TCK_BIT);
383
384         usbprog_jtag_write_slice(usbprog_jtag_handle,output_value);
385 }
386
387 /* (1) assert or (0) deassert reset lines */
388 static void usbprog_reset(int trst, int srst)
389 {
390         LOG_DEBUG("trst: %i, srst: %i", trst, srst);
391
392         if (trst)
393                 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
394         else
395                 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
396
397         if (srst)
398                 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
399         else
400                 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
401 }
402
403 /*************** jtag lowlevel functions ********************/
404
405 struct usb_bus *busses;
406
407 struct usbprog_jtag* usbprog_jtag_open(void)
408 {
409         struct usb_bus *bus;
410         struct usb_device *dev;
411
412         struct usbprog_jtag *tmp;
413
414         tmp = (struct usbprog_jtag*)malloc(sizeof(struct usbprog_jtag));
415
416         usb_set_debug(10);
417         usb_init();
418         usb_find_busses();
419         usb_find_devices();
420
421         busses = usb_get_busses();
422
423         /* find usbprog_jtag device in usb bus */
424
425         for (bus = busses; bus; bus = bus->next)
426         {
427                 for (dev = bus->devices; dev; dev = dev->next)
428                 {
429                         /* condition for sucessfully hit (too bad, I only check the vendor id)*/
430                         if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID)
431                         {
432                                 tmp->usb_handle = usb_open(dev);
433                                 usb_set_configuration(tmp->usb_handle, 1);
434                                 usb_claim_interface(tmp->usb_handle, 0);
435                                 usb_set_altinterface(tmp->usb_handle, 0);
436                                 return tmp;
437                         }
438                 }
439         }
440         return 0;
441 }
442
443 #if 0
444 static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
445 {
446         usb_close(usbprog_jtag->usb_handle);
447         free(usbprog_jtag);
448 }
449 #endif
450
451 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
452 {
453         int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg,msglen, 100);
454         if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
455                         (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
456                 return 1;
457         if (res == msglen)
458         {
459                 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
460                 res =  usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
461                 if (res > 0)
462                         return (unsigned char)msg[1];
463                 else
464                         return -1;
465         }
466         else
467                 return -1;
468         return 0;
469 }
470
471 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
472 {
473         usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
474 }
475
476 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
477 {
478         char tmp[64];   /* fastes packet size for usb controller */
479         int send_bits, bufindex = 0, fillindex = 0, i, loops;
480
481         char swap;
482         /* 61 byte can be transfered (488 bit) */
483
484         while (size > 0)
485         {
486                 if (size > 488)
487                 {
488                         send_bits = 488;
489                         size = size - 488;
490                         loops = 61;
491                 }
492                 else
493                 {
494                         send_bits = size;
495                         loops = size / 8;
496                         loops++;
497                         size = 0;
498                 }
499                 tmp[0] = WRITE_AND_READ;
500                 tmp[1] = (char)(send_bits >> 8);        /* high */
501                 tmp[2] = (char)(send_bits);                     /* low */
502                 i = 0;
503
504                 for (i = 0; i < loops; i++)
505                 {
506                         tmp[3 + i] = buffer[bufindex];
507                         bufindex++;
508                 }
509
510                 if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64)
511                 {
512                         /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
513                         usleep(1);
514                         int timeout = 0;
515                         while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1)
516                         {
517                                 timeout++;
518                                 if (timeout > 10)
519                                         break;
520                         }
521
522                         for (i = 0; i < loops; i++)
523                         {
524                                 swap =  tmp[3 + i];
525                                 buffer[fillindex++] = swap;
526                         }
527                 }
528         }
529 }
530
531 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
532 {
533         char tmp[64];   /* fastes packet size for usb controller */
534         int send_bits, fillindex = 0, i, loops;
535
536         char swap;
537         /* 61 byte can be transfered (488 bit) */
538
539         while (size > 0)
540         {
541                 if (size > 488)
542                 {
543                         send_bits = 488;
544                         size = size - 488;
545                         loops = 61;
546                 }
547                 else
548                 {
549                         send_bits = size;
550                         loops = size / 8;
551                         loops++;
552                         size = 0;
553                 }
554                 tmp[0] = WRITE_AND_READ;
555                 tmp[1] = (char)(send_bits >> 8);        /* high */
556                 tmp[2] = (char)(send_bits);                     /* low */
557
558                 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
559
560                 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
561                 int timeout = 0;
562                 usleep(1);
563                 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1)
564                 {
565                         timeout++;
566                         if (timeout > 10)
567                                 break;
568                 }
569
570                 for (i = 0; i < loops; i++)
571                 {
572                         swap = tmp[3 + i];
573                         buffer[fillindex++] = swap;
574                 }
575         }
576 }
577
578 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
579 {
580         char tmp[64];   /* fastes packet size for usb controller */
581         int send_bits, bufindex = 0, i, loops;
582
583         /* 61 byte can be transfered (488 bit) */
584         while (size > 0)
585         {
586                 if (size > 488)
587                 {
588                         send_bits = 488;
589                         size = size - 488;
590                         loops = 61;
591                 }
592                 else
593                 {
594                         send_bits = size;
595                         loops = size/8;
596                         /* if(loops==0) */
597                         loops++;
598                         size = 0;
599                 }
600                 tmp[0] = WRITE_TDI;
601                 tmp[1] = (char)(send_bits >> 8);        /* high */
602                 tmp[2] = (char)(send_bits);                     /* low */
603                 i = 0;
604
605                 for (i = 0; i < loops; i++)
606                 {
607                         tmp[3 + i] = buffer[bufindex];
608                         bufindex++;
609                 }
610                 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
611         }
612 }
613
614 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
615 {
616         usbprog_jtag_tms_collect(tms_scan);
617 }
618
619 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
620 {
621         char tmp[2];
622         tmp[0] = PORT_DIRECTION;
623         tmp[1] = (char)direction;
624         usbprog_jtag_message(usbprog_jtag, tmp, 2);
625 }
626
627 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value)
628 {
629         char tmp[2];
630         tmp[0] = PORT_SET;
631         tmp[1] = (char)value;
632         usbprog_jtag_message(usbprog_jtag, tmp, 2);
633 }
634
635 #if 0
636 static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
637 {
638         char tmp[2];
639         tmp[0] = PORT_GET;
640         tmp[1] = 0x00;
641         return usbprog_jtag_message(usbprog_jtag, tmp, 2);
642 }
643 #endif
644
645 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value)
646 {
647         char tmp[3];
648         tmp[0] = PORT_SETBIT;
649         tmp[1] = (char)bit;
650         if (value == 1)
651                 tmp[2] = 0x01;
652         else
653                 tmp[2] = 0x00;
654         usbprog_jtag_message(usbprog_jtag, tmp, 3);
655 }
656
657 #if 0
658 static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
659 {
660         char tmp[2];
661         tmp[0] = PORT_GETBIT;
662         tmp[1] = (char)bit;
663
664         if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
665                 return 1;
666         else
667                 return 0;
668 }
669 #endif
670
671 static void usbprog_jtag_tms_collect(char tms_scan)
672 {
673         tms_chain[tms_chain_index] = tms_scan;
674         tms_chain_index++;
675 }
676
677 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
678 {
679         int i;
680         /* LOG_INFO("TMS SEND"); */
681         if (tms_chain_index > 0)
682         {
683                 char tmp[tms_chain_index + 2];
684                 tmp[0] = WRITE_TMS_CHAIN;
685                 tmp[1] = (char)(tms_chain_index);
686                 for (i = 0; i < tms_chain_index + 1; i++)
687                         tmp[2 + i] = tms_chain[i];
688                 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
689                 tms_chain_index = 0;
690         }
691 }