- convert all files to unix line-ending
[fw/openocd] / src / jtag / ft2232.c
1 /***************************************************************************
2  *   Copyright (C) 2004, 2006 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if IS_CYGWIN == 1
25 #include "windows.h"
26 #undef ERROR
27 #endif
28
29 #include "replacements.h"
30
31 /* project specific includes */
32 #include "log.h"
33 #include "types.h"
34 #include "jtag.h"
35 #include "configuration.h"
36 #include "time_support.h"
37
38 /* system includes */
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
45 #include <ftd2xx.h>
46 #elif BUILD_FT2232_LIBFTDI == 1
47 #include <ftdi.h>
48 #endif
49
50 #include <sys/time.h>
51 #include <time.h>
52
53 /* enable this to debug io latency
54  */
55 #if 0
56 #define _DEBUG_USB_IO_
57 #endif
58
59 /* enable this to debug communication
60  */
61 #if 0
62 #define _DEBUG_USB_COMMS_
63 #endif
64
65 int ft2232_execute_queue(void);
66
67 int ft2232_speed(int speed);
68 int ft2232_register_commands(struct command_context_s *cmd_ctx);
69 int ft2232_init(void);
70 int ft2232_quit(void);
71
72 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77
78 char *ft2232_device_desc = NULL;
79 char *ft2232_serial = NULL;
80 char *ft2232_layout = NULL;
81 unsigned char ft2232_latency = 2;
82
83 #define MAX_USB_IDS     8
84 /* vid = pid = 0 marks the end of the list */
85 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
86 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
87
88 typedef struct ft2232_layout_s
89 {
90         char* name;
91         int(*init)(void);
92         void(*reset)(int trst, int srst);
93         void(*blink)(void);
94 } ft2232_layout_t;
95
96 /* init procedures for supported layouts */
97 int usbjtag_init(void);
98 int jtagkey_init(void);
99 int olimex_jtag_init(void);
100 int flyswatter_init(void);
101 int turtle_init(void);
102 int comstick_init(void);
103 int stm32stick_init(void);
104
105 /* reset procedures for supported layouts */
106 void usbjtag_reset(int trst, int srst);
107 void jtagkey_reset(int trst, int srst);
108 void olimex_jtag_reset(int trst, int srst);
109 void flyswatter_reset(int trst, int srst);
110 void turtle_reset(int trst, int srst);
111 void comstick_reset(int trst, int srst);
112 void stm32stick_reset(int trst, int srst);
113
114 /* blink procedures for layouts that support a blinking led */
115 void olimex_jtag_blink(void);
116 void turtle_jtag_blink(void);
117
118 ft2232_layout_t ft2232_layouts[] =
119 {
120         {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
121         {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
122         {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
123         {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
124         {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
125         {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
126         {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
127         {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
128         {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
129         {"comstick", comstick_init, comstick_reset, NULL},
130         {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
131         {NULL, NULL, NULL},
132 };
133
134 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
135
136 static ft2232_layout_t *layout;
137 static u8 low_output = 0x0;
138 static u8 low_direction = 0x0;
139 static u8 high_output = 0x0;
140 static u8 high_direction = 0x0;
141
142 #if BUILD_FT2232_FTD2XX == 1
143 static FT_HANDLE ftdih = NULL;
144 #elif BUILD_FT2232_LIBFTDI == 1
145 static struct ftdi_context ftdic;
146 #endif
147
148 static u8 *ft2232_buffer = NULL;
149 static int ft2232_buffer_size = 0;
150 static int ft2232_read_pointer = 0;
151 static int ft2232_expect_read = 0;
152 #define FT2232_BUFFER_SIZE      131072
153 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
154 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
155
156 jtag_interface_t ft2232_interface = 
157 {
158         
159         .name = "ft2232",
160         
161         .execute_queue = ft2232_execute_queue,
162         
163         .speed = ft2232_speed,
164         .register_commands = ft2232_register_commands,
165         .init = ft2232_init,
166         .quit = ft2232_quit,
167 };
168
169 int ft2232_write(u8 *buf, int size, u32* bytes_written)
170 {
171 #if BUILD_FT2232_FTD2XX == 1
172         FT_STATUS status;
173         DWORD dw_bytes_written;
174         if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
175         {
176                 *bytes_written = dw_bytes_written;
177                 ERROR("FT_Write returned: %lu", status);
178                 return ERROR_JTAG_DEVICE_ERROR;
179         }
180         else
181         {
182                 *bytes_written = dw_bytes_written;
183                 return ERROR_OK;        
184         }
185 #elif BUILD_FT2232_LIBFTDI == 1
186         int retval;
187         if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
188         {
189                 *bytes_written = 0;
190                 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
191                 return ERROR_JTAG_DEVICE_ERROR;
192         }
193         else
194         {
195                 *bytes_written = retval;
196                 return ERROR_OK;        
197         }
198 #endif
199 }
200
201 int ft2232_read(u8* buf, int size, u32* bytes_read)
202 {
203 #if BUILD_FT2232_FTD2XX == 1
204         DWORD dw_bytes_read;
205         FT_STATUS status;
206         int timeout = 5;
207         *bytes_read = 0;
208
209         while ((*bytes_read < size) && timeout--)
210         {
211                 if ((status = FT_Read(ftdih, buf + *bytes_read, size - 
212                         *bytes_read, &dw_bytes_read)) != FT_OK)         
213                 {
214                         *bytes_read = 0; 
215                         ERROR("FT_Read returned: %lu", status);
216                         return ERROR_JTAG_DEVICE_ERROR;
217                 }
218                 *bytes_read += dw_bytes_read; 
219         }
220 #elif BUILD_FT2232_LIBFTDI == 1
221         int retval;
222         int timeout = 100;
223         *bytes_read = 0;
224         
225         while ((*bytes_read < size) && timeout--)
226         {
227                 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
228                 {
229                         *bytes_read = 0;
230                         ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
231                         return ERROR_JTAG_DEVICE_ERROR;
232                 }
233                 *bytes_read += retval;
234         }
235 #endif
236
237         if (*bytes_read < size)
238         {
239                 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
240                 return ERROR_JTAG_DEVICE_ERROR;
241         }
242         
243         return ERROR_OK;
244 }
245
246 int ft2232_speed(int speed)
247 {
248         u8 buf[3];
249         int retval;
250         u32 bytes_written;
251
252         buf[0] = 0x86; /* command "set divisor" */
253         buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
254         buf[2] = (speed >> 8) & 0xff; /* valueH */
255         
256         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
257         if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
258         {
259                 ERROR("couldn't set FT2232 TCK speed");
260                 return retval;
261         }
262
263         jtag_speed = speed;
264         
265         return ERROR_OK;
266 }
267
268 int ft2232_register_commands(struct command_context_s *cmd_ctx)
269 {
270         register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
271                 COMMAND_CONFIG, NULL);
272         register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
273                 COMMAND_CONFIG, NULL);
274         register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
275                 COMMAND_CONFIG, NULL);
276         register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
277                                          COMMAND_CONFIG, NULL);
278         register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
279                                          COMMAND_CONFIG, NULL);
280         return ERROR_OK;
281 }
282
283 void ft2232_end_state(enum tap_state state)
284 {
285         if (tap_move_map[state] != -1)
286                 end_state = state;
287         else
288         {
289                 ERROR("BUG: %i is not a valid end state", state);
290                 exit(-1);
291         }
292 }
293
294 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
295 {
296         int num_bytes = ((scan_size + 7) / 8);
297         int bits_left = scan_size;
298         int cur_byte = 0;
299
300         while(num_bytes-- > 1)
301         {
302                 buffer[cur_byte] = BUFFER_READ;
303                 cur_byte++;
304                 bits_left -= 8;
305         }
306
307         buffer[cur_byte] = 0x0;
308
309         if (bits_left > 1)
310         {
311                 buffer[cur_byte] = BUFFER_READ >> 1;
312         }
313
314         buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
315
316 }
317
318 void ft2232_debug_dump_buffer(void)
319 {
320         int i;
321         char line[256];
322         char *line_p = line;
323         
324         for (i = 0; i < ft2232_buffer_size; i++)
325         {
326                 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
327                 if (i % 16 == 15)
328                 {
329                         DEBUG("%s", line);
330                         line_p = line;
331                 }
332         }
333         
334         if (line_p != line)
335                 DEBUG("%s", line);
336 }
337
338 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
339 {
340         jtag_command_t *cmd;
341         u8 *buffer;
342         int scan_size;
343         enum scan_type type;
344         int retval;
345         u32 bytes_written;
346         u32 bytes_read;
347         
348 #ifdef _DEBUG_USB_IO_
349         struct timeval start, inter, inter2, end;
350         struct timeval d_inter, d_inter2, d_end;
351 #endif
352
353 #ifdef _DEBUG_USB_COMMS_
354         DEBUG("write buffer (size %i):", ft2232_buffer_size);
355         ft2232_debug_dump_buffer();
356 #endif
357
358 #ifdef _DEBUG_USB_IO_
359         gettimeofday(&start, NULL);     
360 #endif
361
362         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
363         {
364                 ERROR("couldn't write MPSSE commands to FT2232");
365                 exit(-1);
366         }
367         
368 #ifdef _DEBUG_USB_IO_
369         gettimeofday(&inter, NULL);     
370 #endif
371         
372         if (ft2232_expect_read)
373         {
374                 int timeout = 100;
375                 ft2232_buffer_size = 0;
376                 
377 #ifdef _DEBUG_USB_IO_
378                 gettimeofday(&inter2, NULL);    
379 #endif
380                 
381                 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
382                 {
383                         ERROR("couldn't read from FT2232");
384                         exit(-1);
385                 }
386                 
387 #ifdef _DEBUG_USB_IO_
388                 gettimeofday(&end, NULL);       
389
390                 timeval_subtract(&d_inter, &inter, &start);
391                 timeval_subtract(&d_inter2, &inter2, &start);
392                 timeval_subtract(&d_end, &end, &start);
393
394                 INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
395 #endif
396         
397                 
398                 ft2232_buffer_size = bytes_read;
399                 
400                 if (ft2232_expect_read != ft2232_buffer_size)
401                 {
402                         ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
403                         ft2232_debug_dump_buffer();     
404
405                         exit(-1);
406                 }
407
408 #ifdef _DEBUG_USB_COMMS_
409                 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
410                 ft2232_debug_dump_buffer();
411 #endif
412         }
413
414         ft2232_expect_read = 0;
415         ft2232_read_pointer = 0;
416         
417         /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
418          * that wasn't handled by a caller-provided error handler
419          */ 
420         retval = ERROR_OK;
421         
422         cmd = first;
423         while (cmd != last)
424         {
425                 switch (cmd->type)
426                 {
427                         case JTAG_SCAN:
428                                 type = jtag_scan_type(cmd->cmd.scan);
429                                 if (type != SCAN_OUT)
430                                 {
431                                         scan_size = jtag_scan_size(cmd->cmd.scan);
432                                         buffer = calloc(CEIL(scan_size, 8), 1);
433                                         ft2232_read_scan(type, buffer, scan_size);
434                                         if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
435                                                 retval = ERROR_JTAG_QUEUE_FAILED;
436                                         free(buffer);
437                                 }
438                                 break;
439                         default:
440                                 break;
441                 }
442                 cmd = cmd->next;
443         }
444         
445         ft2232_buffer_size = 0;
446
447         return retval;
448 }
449
450 void ft2232_add_pathmove(pathmove_command_t *cmd)
451 {
452         int num_states = cmd->num_states;
453         u8 tms_byte;
454         int state_count;
455
456         state_count = 0;
457         while (num_states)
458         {
459                 tms_byte = 0x0;
460                 int bit_count = 0;
461                 
462                 /* command "Clock Data to TMS/CS Pin (no Read)" */
463                 BUFFER_ADD = 0x4b;
464                 /* number of states remaining */
465                 BUFFER_ADD = (num_states % 7) - 1;
466                 
467                 while (num_states % 7)
468                 {
469                         if (tap_transitions[cur_state].low == cmd->path[state_count])
470                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
471                         else if (tap_transitions[cur_state].high == cmd->path[state_count])
472                                 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
473                         else
474                         {
475                                 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
476                                 exit(-1);
477                         }
478
479                         cur_state = cmd->path[state_count];
480                         state_count++;
481                         num_states--;
482                 }
483                 
484                 BUFFER_ADD = tms_byte;
485         }
486         
487         end_state = cur_state;
488 }
489
490 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
491 {
492         int num_bytes = (scan_size + 7) / 8;
493         int bits_left = scan_size;
494         int cur_byte = 0;
495         int last_bit;
496
497         if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
498         {
499                 /* command "Clock Data to TMS/CS Pin (no Read)" */
500                 BUFFER_ADD = 0x4b;
501                 /* scan 7 bit */
502                 BUFFER_ADD = 0x6;
503                 /* TMS data bits */
504                 if (ir_scan)
505                 {
506                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
507                         cur_state = TAP_SI;
508                 }
509                 else
510                 {
511                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
512                         cur_state = TAP_SD;
513                 }
514                 //DEBUG("added TMS scan (no read)");
515         }
516         
517         /* add command for complete bytes */
518         while (num_bytes > 1)
519         {
520                 int thisrun_bytes;
521                 if (type == SCAN_IO)
522                 {
523                         /* Clock Data Bytes In and Out LSB First */
524                         BUFFER_ADD = 0x39;
525                         //DEBUG("added TDI bytes (io %i)", num_bytes);
526                 }
527                 else if (type == SCAN_OUT)
528                 {
529                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
530                         BUFFER_ADD = 0x19;
531                         //DEBUG("added TDI bytes (o)");
532                 }
533                 else if (type == SCAN_IN)
534                 {
535                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
536                         BUFFER_ADD = 0x28;
537                         //DEBUG("added TDI bytes (i %i)", num_bytes);
538                 }
539                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
540                 num_bytes -= thisrun_bytes;
541                 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
542                 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
543                 if (type != SCAN_IN)
544                 {
545                         /* add complete bytes */
546                         while(thisrun_bytes-- > 0)
547                         {
548                                 BUFFER_ADD = buffer[cur_byte];
549                                 cur_byte++;
550                                 bits_left -= 8;
551                         }
552                 }
553                 else /* (type == SCAN_IN) */
554                 {
555                         bits_left -= 8 * (thisrun_bytes);
556                 }
557         }
558         
559         /* the most signifcant bit is scanned during TAP movement */
560         if (type != SCAN_IN)
561                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
562         else
563                 last_bit = 0;
564
565         /* process remaining bits but the last one */
566         if (bits_left > 1)
567         {
568                 if (type == SCAN_IO)
569                 {
570                         /* Clock Data Bits In and Out LSB First */
571                         BUFFER_ADD = 0x3b;
572                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
573                 }
574                 else if (type == SCAN_OUT)
575                 {
576                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
577                         BUFFER_ADD = 0x1b;
578                         //DEBUG("added TDI bits (o)");
579                 }
580                 else if (type == SCAN_IN)
581                 {
582                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
583                         BUFFER_ADD = 0x2a;
584                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
585                 }
586                 BUFFER_ADD = bits_left - 2;
587                 if (type != SCAN_IN)
588                         BUFFER_ADD = buffer[cur_byte];
589         }
590
591         if ((ir_scan && (end_state == TAP_SI)) ||
592                 (!ir_scan && (end_state == TAP_SD)))
593         {
594                 if (type == SCAN_IO)
595                 {
596                         /* Clock Data Bits In and Out LSB First */
597                         BUFFER_ADD = 0x3b;
598                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
599                 }
600                 else if (type == SCAN_OUT)
601                 {
602                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
603                         BUFFER_ADD = 0x1b;
604                         //DEBUG("added TDI bits (o)");
605                 }
606                 else if (type == SCAN_IN)
607                 {
608                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
609                         BUFFER_ADD = 0x2a;
610                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
611                 }
612                 BUFFER_ADD = 0x0;
613                 BUFFER_ADD = last_bit;
614         }
615         else
616         {
617                 /* move from Shift-IR/DR to end state */
618                 if (type != SCAN_OUT)
619                 {
620                         /* Clock Data to TMS/CS Pin with Read */
621                         BUFFER_ADD = 0x6b;
622                         //DEBUG("added TMS scan (read)");
623                 }
624                 else
625                 {
626                         /* Clock Data to TMS/CS Pin (no Read) */
627                         BUFFER_ADD = 0x4b;
628                         //DEBUG("added TMS scan (no read)");
629                 }
630                 BUFFER_ADD = 0x6;
631                 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
632                 cur_state = end_state;
633         }
634 }
635
636 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
637 {
638         int num_bytes = (scan_size + 7) / 8;
639         int bits_left = scan_size;
640         int cur_byte = 0;
641         int last_bit;
642         u8 *receive_buffer = malloc(CEIL(scan_size, 8));
643         u8 *receive_pointer = receive_buffer;
644         u32 bytes_written;
645         u32 bytes_read;
646         int retval;
647         int thisrun_read = 0;
648         
649         if (cmd->ir_scan)
650         {
651                 ERROR("BUG: large IR scans are not supported");
652                 exit(-1);
653         }
654
655         if (cur_state != TAP_SD)
656         {
657                 /* command "Clock Data to TMS/CS Pin (no Read)" */
658                 BUFFER_ADD = 0x4b;
659                 /* scan 7 bit */
660                 BUFFER_ADD = 0x6;
661                 /* TMS data bits */
662                 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
663                 cur_state = TAP_SD;
664         }
665         
666         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
667         {
668                 ERROR("couldn't write MPSSE commands to FT2232");
669                 exit(-1);
670         }
671         DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
672         ft2232_buffer_size = 0;
673         
674         /* add command for complete bytes */
675         while (num_bytes > 1)
676         {
677                 int thisrun_bytes;
678                 
679                 if (type == SCAN_IO)
680                 {
681                         /* Clock Data Bytes In and Out LSB First */
682                         BUFFER_ADD = 0x39;
683                         //DEBUG("added TDI bytes (io %i)", num_bytes);
684                 }
685                 else if (type == SCAN_OUT)
686                 {
687                         /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
688                         BUFFER_ADD = 0x19;
689                         //DEBUG("added TDI bytes (o)");
690                 }
691                 else if (type == SCAN_IN)
692                 {
693                         /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
694                         BUFFER_ADD = 0x28;
695                         //DEBUG("added TDI bytes (i %i)", num_bytes);
696                 }
697                 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
698                 thisrun_read = thisrun_bytes;
699                 num_bytes -= thisrun_bytes;
700                 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
701                 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
702                 if (type != SCAN_IN)
703                 {
704                         /* add complete bytes */
705                         while(thisrun_bytes-- > 0)
706                         {
707                                 BUFFER_ADD = buffer[cur_byte];
708                                 cur_byte++;
709                                 bits_left -= 8;
710                         }
711                 }
712                 else /* (type == SCAN_IN) */
713                 {
714                         bits_left -= 8 * (thisrun_bytes);
715                 }
716
717                 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
718                 {
719                         ERROR("couldn't write MPSSE commands to FT2232");
720                         exit(-1);
721                 }
722                 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
723                 ft2232_buffer_size = 0;
724                 
725                 if (type != SCAN_OUT)
726                 {
727                         if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
728                         {
729                                 ERROR("couldn't read from FT2232");
730                                 exit(-1);
731                         }
732                         DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
733                         receive_pointer += bytes_read;
734                 }
735         }
736         
737         thisrun_read = 0;
738         
739         /* the most signifcant bit is scanned during TAP movement */
740         if (type != SCAN_IN)
741                 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
742         else
743                 last_bit = 0;
744
745         /* process remaining bits but the last one */
746         if (bits_left > 1)
747         {
748                 if (type == SCAN_IO)
749                 {
750                         /* Clock Data Bits In and Out LSB First */
751                         BUFFER_ADD = 0x3b;
752                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
753                 }
754                 else if (type == SCAN_OUT)
755                 {
756                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
757                         BUFFER_ADD = 0x1b;
758                         //DEBUG("added TDI bits (o)");
759                 }
760                 else if (type == SCAN_IN)
761                 {
762                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
763                         BUFFER_ADD = 0x2a;
764                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
765                 }
766                 BUFFER_ADD = bits_left - 2;
767                 if (type != SCAN_IN)
768                         BUFFER_ADD = buffer[cur_byte];
769                         
770                 if (type != SCAN_OUT)
771                         thisrun_read += 2;
772         }
773
774         if (end_state == TAP_SD)
775         {
776                 if (type == SCAN_IO)
777                 {
778                         /* Clock Data Bits In and Out LSB First */
779                         BUFFER_ADD = 0x3b;
780                         //DEBUG("added TDI bits (io) %i", bits_left - 1);
781                 }
782                 else if (type == SCAN_OUT)
783                 {
784                         /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
785                         BUFFER_ADD = 0x1b;
786                         //DEBUG("added TDI bits (o)");
787                 }
788                 else if (type == SCAN_IN)
789                 {
790                         /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
791                         BUFFER_ADD = 0x2a;
792                         //DEBUG("added TDI bits (i %i)", bits_left - 1);
793                 }
794                 BUFFER_ADD = 0x0;
795                 BUFFER_ADD = last_bit;
796         }
797         else
798         {
799                 /* move from Shift-IR/DR to end state */
800                 if (type != SCAN_OUT)
801                 {
802                         /* Clock Data to TMS/CS Pin with Read */
803                         BUFFER_ADD = 0x6b;
804                         //DEBUG("added TMS scan (read)");
805                 }
806                 else
807                 {
808                         /* Clock Data to TMS/CS Pin (no Read) */
809                         BUFFER_ADD = 0x4b;
810                         //DEBUG("added TMS scan (no read)");
811                 }
812                 BUFFER_ADD = 0x6;
813                 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
814                 cur_state = end_state;
815         }
816         
817         if (type != SCAN_OUT)
818                 thisrun_read += 1;
819         
820         if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
821         {
822                 ERROR("couldn't write MPSSE commands to FT2232");
823                 exit(-1);
824         }
825         DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
826         ft2232_buffer_size = 0;
827         
828         if (type != SCAN_OUT)
829         {
830                 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
831                 {
832                         ERROR("couldn't read from FT2232");
833                         exit(-1);
834                 }
835                 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
836                 receive_pointer += bytes_read;
837         }
838         
839         return ERROR_OK;
840 }
841
842 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
843 {
844         int predicted_size = 3;
845         int num_bytes = (scan_size - 1) / 8;
846         
847         if (cur_state != TAP_SD)
848                 predicted_size += 3;
849         
850         if (type == SCAN_IN)    /* only from device to host */
851         {
852                 /* complete bytes */
853                 predicted_size += (CEIL(num_bytes, 65536)) * 3;
854                 /* remaining bits - 1 (up to 7) */
855                 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
856         }
857         else                                    /* host to device, or bidirectional */
858         {
859                 /* complete bytes */
860                 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
861                 /* remaining bits -1 (up to 7) */
862                 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
863         }
864
865         return predicted_size;
866 }
867
868 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
869 {
870         int predicted_size = 0;
871         
872         if (type != SCAN_OUT)
873         {
874                 /* complete bytes */
875                 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
876                 /* remaining bits - 1 */
877                 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
878                 /* last bit (from TMS scan) */
879                 predicted_size += 1;
880         }
881         
882         //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
883
884         return predicted_size;
885 }
886
887 void usbjtag_reset(int trst, int srst)
888 {
889         if (trst == 1)
890         {
891                 cur_state = TAP_TLR;
892                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
893                         low_direction |= nTRSTnOE;      /* switch to output pin (output is low) */
894                 else
895                         low_output &= ~nTRST;   /* switch output low */
896         }
897         else if (trst == 0)
898         {
899                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
900                         low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
901                 else
902                         low_output |= nTRST; /* switch output high */
903         }
904
905         if (srst == 1)
906         {
907                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
908                         low_output &= ~nSRST;   /* switch output low */
909                 else
910                         low_direction |= nSRSTnOE;      /* switch to output pin (output is low) */
911         }
912         else if (srst == 0)
913         {
914                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
915                         low_output |= nSRST;    /* switch output high */
916                 else
917                         low_direction &= ~nSRSTnOE;     /* switch to input pin (high-Z) */
918         }
919         
920         /* command "set data bits low byte" */
921         BUFFER_ADD = 0x80;
922         BUFFER_ADD = low_output;
923         BUFFER_ADD = low_direction;
924
925 }
926
927 void jtagkey_reset(int trst, int srst)
928 {
929         if (trst == 1)
930         {
931                 cur_state = TAP_TLR;
932                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
933                         high_output &= ~nTRSTnOE;
934                 else
935                         high_output &= ~nTRST;
936         }
937         else if (trst == 0)
938         {
939                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
940                         high_output |= nTRSTnOE;
941                 else
942                         high_output |= nTRST;
943         }
944
945         if (srst == 1)
946         {
947                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
948                         high_output &= ~nSRST;
949                 else
950                         high_output &= ~nSRSTnOE;
951         }
952         else if (srst == 0)
953         {
954                 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
955                         high_output |= nSRST;
956                 else
957                         high_output |= nSRSTnOE;
958         }
959         
960         /* command "set data bits high byte" */
961         BUFFER_ADD = 0x82;
962         BUFFER_ADD = high_output;
963         BUFFER_ADD = high_direction;
964         DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
965 }
966
967 void olimex_jtag_reset(int trst, int srst)
968 {
969         if (trst == 1)
970         {
971                 cur_state = TAP_TLR;
972                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
973                         high_output &= ~nTRSTnOE;
974                 else
975                         high_output &= ~nTRST;
976         }
977         else if (trst == 0)
978         {
979                 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
980                         high_output |= nTRSTnOE;
981                 else
982                         high_output |= nTRST;
983         }
984
985     if (srst == 1)
986     {
987         high_output |= nSRST;
988     }
989     else if (srst == 0)
990     {
991         high_output &= ~nSRST;
992     }
993
994     /* command "set data bits high byte" */
995     BUFFER_ADD = 0x82;
996     BUFFER_ADD = high_output;
997     BUFFER_ADD = high_direction;
998     DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
999 }
1000
1001 void flyswatter_reset(int trst, int srst)
1002 {
1003         if (trst == 1)
1004         {
1005                 cur_state = TAP_TLR;
1006                 low_output &= ~nTRST;
1007         }
1008         else if (trst == 0)
1009         {
1010                 low_output |= nTRST;
1011         }
1012
1013     if (srst == 1)
1014     {
1015         low_output |= nSRST;
1016     }
1017     else if (srst == 0)
1018     {
1019         low_output &= ~nSRST;
1020     }
1021
1022     /* command "set data bits low byte" */
1023     BUFFER_ADD = 0x80;
1024     BUFFER_ADD = low_output;
1025     BUFFER_ADD = low_direction;
1026     DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1027 }
1028
1029 void turtle_reset(int trst, int srst)
1030 {
1031         trst = trst;
1032         
1033         if (srst == 1)
1034         {
1035                 low_output |= nSRST;
1036         }
1037         else if (srst == 0)
1038         {
1039                 low_output &= ~nSRST;
1040         }
1041         
1042         /* command "set data bits low byte" */
1043         BUFFER_ADD = 0x80;
1044         BUFFER_ADD = low_output;
1045         BUFFER_ADD = low_direction;
1046         DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1047 }
1048
1049 void comstick_reset(int trst, int srst)
1050 {
1051         if (trst == 1)
1052         {
1053                 cur_state = TAP_TLR;
1054                 high_output &= ~nTRST;
1055         }
1056         else if (trst == 0)
1057         {
1058                 high_output |= nTRST;
1059         }
1060
1061     if (srst == 1)
1062     {
1063         high_output &= ~nSRST;
1064     }
1065     else if (srst == 0)
1066     {
1067         high_output |= nSRST;
1068     }
1069         
1070         /* command "set data bits high byte" */
1071         BUFFER_ADD = 0x82;
1072         BUFFER_ADD = high_output;
1073         BUFFER_ADD = high_direction;
1074         DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1075 }
1076
1077 void stm32stick_reset(int trst, int srst)
1078 {
1079         if (trst == 1)
1080         {
1081                 cur_state = TAP_TLR;
1082                 high_output &= ~nTRST;
1083         }
1084         else if (trst == 0)
1085         {
1086                 high_output |= nTRST;
1087         }
1088
1089     if (srst == 1)
1090     {
1091         low_output &= ~nSRST;
1092     }
1093     else if (srst == 0)
1094     {
1095         low_output |= nSRST;
1096     }
1097         
1098         /* command "set data bits low byte" */
1099         BUFFER_ADD = 0x80;
1100         BUFFER_ADD = low_output;
1101         BUFFER_ADD = low_direction;
1102         
1103         /* command "set data bits high byte" */
1104         BUFFER_ADD = 0x82;
1105         BUFFER_ADD = high_output;
1106         BUFFER_ADD = high_direction;
1107         DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1108 }
1109
1110 int ft2232_execute_queue()
1111 {
1112         jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1113         jtag_command_t *first_unsent = cmd;     /* next command that has to be sent */
1114         u8 *buffer;
1115         int scan_size;  /* size of IR or DR scan */
1116         enum scan_type type;
1117         int i;
1118         int predicted_size = 0;
1119         int require_send = 0;
1120         int retval;
1121         
1122         /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1123          * that wasn't handled by a caller-provided error handler
1124          */ 
1125         retval = ERROR_OK;
1126
1127         ft2232_buffer_size = 0;
1128         ft2232_expect_read = 0;
1129         
1130         /* blink, if the current layout has that feature */
1131         if (layout->blink)
1132                 layout->blink();
1133
1134         while (cmd)
1135         {
1136                 switch(cmd->type)
1137                 {
1138                         case JTAG_END_STATE:
1139                                 if (cmd->cmd.end_state->end_state != -1)
1140                                         ft2232_end_state(cmd->cmd.end_state->end_state);
1141                                 break;
1142                         case JTAG_RESET:
1143                                 /* only send the maximum buffer size that FT2232C can handle */
1144                                 predicted_size = 3;
1145                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1146                                 {
1147                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1148                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1149                                         require_send = 0;
1150                                         first_unsent = cmd;
1151                                 }
1152
1153                                 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1154                                 require_send = 1;
1155                                 
1156 #ifdef _DEBUG_JTAG_IO_                          
1157                                 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1158 #endif
1159                                 break;
1160                         case JTAG_RUNTEST:
1161                                 /* only send the maximum buffer size that FT2232C can handle */
1162                                 predicted_size = 0;
1163                                 if (cur_state != TAP_RTI)
1164                                         predicted_size += 3;
1165                                 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1166                                 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1167                                         predicted_size += 3;
1168                                 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1169                                         predicted_size += 3;
1170                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1171                                 {
1172                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1173                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1174                                         require_send = 0;
1175                                         first_unsent = cmd;
1176                                 }
1177                                 if (cur_state != TAP_RTI)
1178                                 {
1179                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1180                                         BUFFER_ADD = 0x4b;
1181                                         /* scan 7 bit */
1182                                         BUFFER_ADD = 0x6;
1183                                         /* TMS data bits */
1184                                         BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1185                                         cur_state = TAP_RTI;
1186                                         require_send = 1;
1187                                 }
1188                                 i = cmd->cmd.runtest->num_cycles;
1189                                 while (i > 0)
1190                                 {
1191                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1192                                         BUFFER_ADD = 0x4b;
1193                                         /* scan 7 bit */
1194                                         BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1195                                         /* TMS data bits */
1196                                         BUFFER_ADD = 0x0;
1197                                         cur_state = TAP_RTI;
1198                                         i -= (i > 7) ? 7 : i;
1199                                         //DEBUG("added TMS scan (no read)");
1200                                 }
1201                                 if (cmd->cmd.runtest->end_state != -1)
1202                                         ft2232_end_state(cmd->cmd.runtest->end_state);
1203                                 if (cur_state != end_state)
1204                                 {
1205                                         /* command "Clock Data to TMS/CS Pin (no Read)" */
1206                                         BUFFER_ADD = 0x4b;
1207                                         /* scan 7 bit */
1208                                         BUFFER_ADD = 0x6;
1209                                         /* TMS data bits */
1210                                         BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1211                                         cur_state = end_state;
1212                                         //DEBUG("added TMS scan (no read)");
1213                                 }
1214                                 require_send = 1;
1215 #ifdef _DEBUG_JTAG_IO_                          
1216                                 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1217 #endif
1218                                 break;
1219                         case JTAG_STATEMOVE:
1220                                 /* only send the maximum buffer size that FT2232C can handle */
1221                                 predicted_size = 3;
1222                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1223                                 {
1224                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1225                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1226                                         require_send = 0;
1227                                         first_unsent = cmd;
1228                                 }
1229                                 if (cmd->cmd.statemove->end_state != -1)
1230                                         ft2232_end_state(cmd->cmd.statemove->end_state);
1231                                 /* command "Clock Data to TMS/CS Pin (no Read)" */
1232                                 BUFFER_ADD = 0x4b;
1233                                 /* scan 7 bit */
1234                                 BUFFER_ADD = 0x6;
1235                                 /* TMS data bits */
1236                                 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1237                                 //DEBUG("added TMS scan (no read)");
1238                                 cur_state = end_state;
1239                                 require_send = 1;
1240 #ifdef _DEBUG_JTAG_IO_                          
1241                                 DEBUG("statemove: %i", end_state);
1242 #endif
1243                                 break;
1244                         case JTAG_PATHMOVE:
1245                                 /* only send the maximum buffer size that FT2232C can handle */
1246                                 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1247                                 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1248                                 {
1249                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1250                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1251                                         require_send = 0;
1252                                         first_unsent = cmd;
1253                                 }
1254                                 ft2232_add_pathmove(cmd->cmd.pathmove);
1255                                 require_send = 1;
1256 #ifdef _DEBUG_JTAG_IO_                          
1257                                 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1258 #endif
1259                                 break;
1260                         case JTAG_SCAN:
1261                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1262                                 type = jtag_scan_type(cmd->cmd.scan);
1263                                 predicted_size = ft2232_predict_scan_out(scan_size, type);
1264                                 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1265                                 {
1266                                         DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1267                                         /* unsent commands before this */
1268                                         if (first_unsent != cmd)
1269                                                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1270                                                         retval = ERROR_JTAG_QUEUE_FAILED;
1271                                         
1272                                         /* current command */
1273                                         if (cmd->cmd.scan->end_state != -1)
1274                                                 ft2232_end_state(cmd->cmd.scan->end_state);
1275                                         ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1276                                         require_send = 0;
1277                                         first_unsent = cmd->next;
1278                                         if (buffer)
1279                                                 free(buffer);
1280                                         break;
1281                                 }
1282                                 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1283                                 {
1284                                         DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1285                                         if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1286                                                 retval = ERROR_JTAG_QUEUE_FAILED;
1287                                         require_send = 0;
1288                                         first_unsent = cmd;
1289                                 }
1290                                 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1291                                 //DEBUG("new read size: %i", ft2232_expect_read);
1292                                 if (cmd->cmd.scan->end_state != -1)
1293                                         ft2232_end_state(cmd->cmd.scan->end_state);
1294                                 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1295                                 require_send = 1;
1296                                 if (buffer)
1297                                         free(buffer);
1298 #ifdef _DEBUG_JTAG_IO_                          
1299                                 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1300 #endif
1301                                 break;
1302                         case JTAG_SLEEP:
1303                                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1304                                         retval = ERROR_JTAG_QUEUE_FAILED;
1305                                 first_unsent = cmd->next;
1306                                 jtag_sleep(cmd->cmd.sleep->us);
1307 #ifdef _DEBUG_JTAG_IO_                          
1308                                 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1309 #endif
1310                                 break;
1311                         default:
1312                                 ERROR("BUG: unknown JTAG command type encountered");
1313                                 exit(-1);
1314                 }
1315                 cmd = cmd->next;
1316         }
1317
1318         if (require_send > 0)
1319                 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1320                         retval = ERROR_JTAG_QUEUE_FAILED;
1321
1322         return retval;
1323 }
1324
1325 #if BUILD_FT2232_FTD2XX == 1
1326 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1327 {
1328         FT_STATUS status;
1329         DWORD openex_flags = 0;
1330         char *openex_string = NULL;
1331         u8 latency_timer;
1332
1333         DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1334             ft2232_layout, vid, pid);
1335
1336 #if IS_WIN32 == 0
1337         /* Add non-standard Vid/Pid to the linux driver */
1338         if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1339         {
1340                 WARNING("couldn't add %4.4x:%4.4x",
1341                     vid, pid);
1342         }
1343 #endif
1344
1345         if (ft2232_device_desc && ft2232_serial)
1346         {
1347                 WARNING("can't open by device description and serial number, giving precedence to serial");
1348                 ft2232_device_desc = NULL;
1349         }
1350         
1351         if (ft2232_device_desc)
1352         {
1353                 openex_string = ft2232_device_desc;
1354                 openex_flags = FT_OPEN_BY_DESCRIPTION;
1355         }
1356         else if (ft2232_serial)
1357         {
1358                 openex_string = ft2232_serial;
1359                 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1360         }
1361         else
1362         {
1363                 ERROR("neither device description nor serial number specified");
1364                 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1365                 
1366                 return ERROR_JTAG_INIT_FAILED;  
1367         }
1368
1369         if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1370         {
1371                 DWORD num_devices;
1372                 
1373                 if (more) {
1374                         WARNING("unable to open ftdi device (trying more): %lu",
1375                             status);
1376                         *try_more = 1;
1377                         return ERROR_JTAG_INIT_FAILED;
1378                 }
1379                 ERROR("unable to open ftdi device: %lu", status);
1380                 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1381                 if (status == FT_OK)
1382                 {
1383                         char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1384                         int i;
1385
1386                         for (i = 0; i < num_devices; i++)
1387                                 desc_array[i] = malloc(64);
1388                         desc_array[num_devices] = NULL;
1389
1390                         status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1391
1392                         if (status == FT_OK)
1393                         {
1394                                 ERROR("ListDevices: %lu\n", num_devices);
1395                                 for (i = 0; i < num_devices; i++)
1396                                         ERROR("%i: %s", i, desc_array[i]);
1397                         }
1398                         
1399                         for (i = 0; i < num_devices; i++)
1400                                 free(desc_array[i]);
1401                         free(desc_array);
1402                 }
1403                 else
1404                 {
1405                         ERROR("ListDevices: NONE\n");
1406                 }
1407                 return ERROR_JTAG_INIT_FAILED;
1408         }
1409
1410         if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1411         {
1412                 ERROR("unable to set latency timer: %lu", status);
1413                 return ERROR_JTAG_INIT_FAILED;
1414         }
1415         
1416         if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1417         {
1418                 ERROR("unable to get latency timer: %lu", status);
1419                 return ERROR_JTAG_INIT_FAILED;
1420         }
1421         else
1422         {
1423                 DEBUG("current latency timer: %i", latency_timer);
1424         }
1425         
1426         if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1427         {
1428                 ERROR("unable to set timeouts: %lu", status);
1429                 return ERROR_JTAG_INIT_FAILED;
1430         }
1431
1432         if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1433         {
1434                 ERROR("unable to enable bit i/o mode: %lu", status);
1435                 return ERROR_JTAG_INIT_FAILED;
1436         }
1437
1438         return ERROR_OK;
1439 }
1440
1441 static int ft2232_purge_ftd2xx(void)
1442 {
1443         FT_STATUS status;
1444
1445         if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1446         {
1447                 ERROR("error purging ftd2xx device: %lu", status);
1448                 return ERROR_JTAG_INIT_FAILED;
1449         }
1450
1451         return ERROR_OK;
1452 }
1453 #endif /* BUILD_FT2232_FTD2XX == 1 */
1454
1455 #if BUILD_FT2232_LIBFTDI == 1
1456 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1457 {
1458         u8 latency_timer;
1459
1460         DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1461             ft2232_layout, vid, pid);
1462
1463         if (ftdi_init(&ftdic) < 0)
1464                 return ERROR_JTAG_INIT_FAILED;
1465
1466         /* context, vendor id, product id */
1467         if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1468             ft2232_serial) < 0) {
1469                 if (more)
1470                         WARNING("unable to open ftdi device (trying more): %s",
1471                              ftdic.error_str);
1472                 else
1473                         ERROR("unable to open ftdi device: %s", ftdic.error_str);
1474                 *try_more = 1;
1475                 return ERROR_JTAG_INIT_FAILED;
1476         }
1477
1478         if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1479         {
1480                 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1481                 return ERROR_JTAG_INIT_FAILED;
1482         }
1483
1484         if (ftdi_usb_reset(&ftdic) < 0)
1485         {
1486                 ERROR("unable to reset ftdi device");
1487                 return ERROR_JTAG_INIT_FAILED;
1488         }
1489
1490         if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1491         {
1492                 ERROR("unable to set latency timer");
1493                 return ERROR_JTAG_INIT_FAILED;
1494         }
1495         
1496         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1497         {
1498                 ERROR("unable to get latency timer");
1499                 return ERROR_JTAG_INIT_FAILED;
1500         }
1501         else
1502         {
1503                 DEBUG("current latency timer: %i", latency_timer);
1504         }
1505
1506         ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1507
1508         return ERROR_OK;
1509 }
1510
1511 static int ft2232_purge_libftdi(void)
1512 {
1513         if (ftdi_usb_purge_buffers(&ftdic) < 0)
1514         {
1515                 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1516                 return ERROR_JTAG_INIT_FAILED;
1517         }
1518
1519         return ERROR_OK;
1520 }
1521 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1522
1523 int ft2232_init(void)
1524 {
1525         u8 buf[1];
1526         int retval;
1527         u32 bytes_written;
1528         ft2232_layout_t *cur_layout = ft2232_layouts;
1529         int i;
1530         
1531         if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1532         {
1533                 ft2232_layout = "usbjtag";
1534                 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1535         }
1536         
1537         while (cur_layout->name)
1538         {
1539                 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1540                 {
1541                         layout = cur_layout;
1542                         break;
1543                 }
1544                 cur_layout++;
1545         }
1546
1547         if (!layout)
1548         {
1549                 ERROR("No matching layout found for %s", ft2232_layout);
1550                 return ERROR_JTAG_INIT_FAILED;
1551         }
1552         
1553         for (i = 0; 1; i++) {
1554                 /*
1555                  * "more indicates that there are more IDs to try, so we should
1556                  * not print an error for an ID mismatch (but for anything
1557                  * else, we should).
1558                  *
1559                  * try_more indicates that the error code returned indicates an
1560                  * ID mismatch (and nothing else) and that we should proceeed
1561                  * with the next ID pair.
1562                  */
1563                 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1564                 int try_more = 0;
1565
1566 #if BUILD_FT2232_FTD2XX == 1
1567                 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1568                     more, &try_more);
1569 #elif BUILD_FT2232_LIBFTDI == 1
1570                 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1571                     more, &try_more);
1572 #endif  
1573                 if (retval >= 0)
1574                         break;
1575                 if (!more || !try_more)
1576                         return retval;
1577         }
1578
1579         ft2232_buffer_size = 0;
1580         ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1581
1582         if (layout->init() != ERROR_OK)
1583                 return ERROR_JTAG_INIT_FAILED;
1584
1585         ft2232_speed(jtag_speed);
1586
1587         buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1588         if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1589         {
1590                 ERROR("couldn't write to FT2232 to disable loopback");
1591                 return ERROR_JTAG_INIT_FAILED;
1592         }
1593
1594 #if BUILD_FT2232_FTD2XX == 1
1595         return ft2232_purge_ftd2xx();
1596 #elif BUILD_FT2232_LIBFTDI == 1
1597         return ft2232_purge_libftdi();
1598 #endif  
1599
1600         return ERROR_OK;
1601 }
1602
1603 int usbjtag_init(void)
1604 {
1605         u8 buf[3];
1606         u32 bytes_written;
1607         
1608         low_output = 0x08;
1609         low_direction = 0x0b;
1610         
1611         if (strcmp(ft2232_layout, "usbjtag") == 0)
1612         {
1613                 nTRST = 0x10;
1614                 nTRSTnOE = 0x10;
1615                 nSRST = 0x40;
1616                 nSRSTnOE = 0x40;
1617         }
1618         else if (strcmp(ft2232_layout, "signalyzer") == 0)
1619         {
1620                 nTRST = 0x10;
1621                 nTRSTnOE = 0x10;
1622                 nSRST = 0x20;
1623                 nSRSTnOE = 0x20;
1624         }
1625         else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1626         {
1627                 nTRST = 0x0;
1628                 nTRSTnOE = 0x00;
1629                 nSRST = 0x20;
1630                 nSRSTnOE = 0x20;
1631                 low_output = 0x88;
1632                 low_direction = 0x8b;
1633         }
1634         else
1635         {
1636                 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1637                 return ERROR_JTAG_INIT_FAILED;  
1638         }
1639         
1640         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1641         {
1642                 low_direction &= ~nTRSTnOE; /* nTRST input */
1643                 low_output &= ~nTRST; /* nTRST = 0 */
1644         }
1645         else
1646         {
1647                 low_direction |= nTRSTnOE; /* nTRST output */
1648                 low_output |= nTRST; /* nTRST = 1 */
1649         }
1650         
1651         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1652         {
1653                 low_direction |= nSRSTnOE; /* nSRST output */
1654                 low_output |= nSRST; /* nSRST = 1 */
1655         }
1656         else
1657         {
1658                 low_direction &= ~nSRSTnOE; /* nSRST input */
1659                 low_output &= ~nSRST; /* nSRST = 0 */
1660         }
1661         
1662         /* initialize low byte for jtag */
1663         buf[0] = 0x80; /* command "set data bits low byte" */
1664         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1665         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1666         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1667         
1668         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1669         {
1670                 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout"); 
1671                 return ERROR_JTAG_INIT_FAILED;
1672         }
1673
1674         return ERROR_OK;
1675 }
1676
1677 int jtagkey_init(void)
1678 {
1679         u8 buf[3];
1680         u32 bytes_written;
1681         
1682         low_output = 0x08;
1683         low_direction = 0x1b;
1684         
1685         /* initialize low byte for jtag */
1686         buf[0] = 0x80; /* command "set data bits low byte" */
1687         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1688         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1689         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1690         
1691         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1692         {
1693                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1694                 return ERROR_JTAG_INIT_FAILED;
1695         }
1696         
1697         if (strcmp(layout->name, "jtagkey") == 0)
1698         {
1699                 nTRST = 0x01;
1700                 nTRSTnOE = 0x4;
1701                 nSRST = 0x02;
1702                 nSRSTnOE = 0x08;
1703         }
1704         else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1705                 (strcmp(layout->name, "oocdlink") == 0))
1706         {
1707                 nTRST = 0x02;
1708                 nTRSTnOE = 0x1;
1709                 nSRST = 0x08;
1710                 nSRSTnOE = 0x04;
1711         }
1712         else
1713         {
1714                 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1715                 exit(-1);
1716         }
1717         
1718         high_output = 0x0;
1719         high_direction = 0x0f;
1720
1721         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1722         {
1723                 high_output |= nTRSTnOE;
1724                 high_output &= ~nTRST;
1725         }
1726         else
1727         {
1728                 high_output &= ~nTRSTnOE;
1729                 high_output |= nTRST;
1730         }
1731         
1732         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1733         {
1734                 high_output &= ~nSRSTnOE;
1735                 high_output |= nSRST;
1736         }
1737         else
1738         {
1739                 high_output |= nSRSTnOE;
1740                 high_output &= ~nSRST;
1741         }
1742         
1743         /* initialize high port */
1744         buf[0] = 0x82; /* command "set data bits high byte" */
1745         buf[1] = high_output; /* value */
1746         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1747         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1748         
1749         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1750         {
1751                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1752                 return ERROR_JTAG_INIT_FAILED;
1753         }
1754         
1755         return ERROR_OK;
1756 }
1757
1758 int olimex_jtag_init(void)
1759 {
1760         u8 buf[3];
1761         u32 bytes_written;
1762         
1763         low_output = 0x08;
1764         low_direction = 0x1b;
1765         
1766         /* initialize low byte for jtag */
1767         buf[0] = 0x80; /* command "set data bits low byte" */
1768         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1769         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1770         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1771         
1772         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1773         {
1774                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1775                 return ERROR_JTAG_INIT_FAILED;
1776         }
1777         
1778         nTRST = 0x01;
1779         nTRSTnOE = 0x4;
1780         nSRST = 0x02;
1781         nSRSTnOE = 0x00; /* no output enable for nSRST */
1782
1783         high_output = 0x0;
1784         high_direction = 0x0f;
1785
1786         if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1787         {
1788                 high_output |= nTRSTnOE;
1789                 high_output &= ~nTRST;
1790         }
1791         else
1792         {
1793                 high_output &= ~nTRSTnOE;
1794                 high_output |= nTRST;
1795         }
1796         
1797         if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1798         {
1799                 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1800         }
1801         else
1802         {
1803                 high_output &= ~nSRST;
1804         }
1805         
1806         /* turn red LED on */
1807         high_output |= 0x08;
1808         
1809         /* initialize high port */
1810         buf[0] = 0x82; /* command "set data bits high byte" */
1811         buf[1] = high_output; /* value */
1812         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1813         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1814         
1815         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1816         {
1817                 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout"); 
1818                 return ERROR_JTAG_INIT_FAILED;
1819         }
1820         
1821         return ERROR_OK;
1822 }
1823
1824 int flyswatter_init(void)
1825 {
1826         u8 buf[3];
1827         u32 bytes_written;
1828         
1829         low_output = 0x18;
1830         low_direction = 0xfb;
1831         
1832         /* initialize low byte for jtag */
1833         buf[0] = 0x80; /* command "set data bits low byte" */
1834         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1835         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1836         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1837         
1838         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1839         {
1840                 ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); 
1841                 return ERROR_JTAG_INIT_FAILED;
1842         }
1843         
1844         nTRST = 0x10;
1845         nTRSTnOE = 0x0; /* not output enable for nTRST */
1846         nSRST = 0x20;
1847         nSRSTnOE = 0x00; /* no output enable for nSRST */
1848
1849         high_output = 0x00;
1850         high_direction = 0x0c;
1851
1852         /* turn red LED1 on, LED2 off */
1853         high_output |= 0x08;
1854         
1855         /* initialize high port */
1856         buf[0] = 0x82; /* command "set data bits high byte" */
1857         buf[1] = high_output; /* value */
1858         buf[2] = high_direction;   /* all outputs (xRST and xRSTnOE) */
1859         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1860         
1861         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1862         {
1863                 ERROR("couldn't initialize FT2232 with 'flyswatter' layout"); 
1864                 return ERROR_JTAG_INIT_FAILED;
1865         }
1866         
1867         return ERROR_OK;
1868 }
1869
1870 int turtle_init(void)
1871 {
1872         u8 buf[3];
1873         u32 bytes_written;
1874         
1875         low_output = 0x08;
1876         low_direction = 0x5b;
1877         
1878         /* initialize low byte for jtag */
1879         buf[0] = 0x80; /* command "set data bits low byte" */
1880         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1881         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1882         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1883         
1884         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1885         {
1886                 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); 
1887                 return ERROR_JTAG_INIT_FAILED;
1888         }
1889         
1890         nSRST = 0x40;
1891         
1892         high_output = 0x00;
1893         high_direction = 0x0C;
1894         
1895         /* initialize high port */
1896         buf[0] = 0x82; /* command "set data bits high byte" */
1897         buf[1] = high_output;
1898         buf[2] = high_direction;
1899         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1900         
1901         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1902         {
1903                 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout"); 
1904                 return ERROR_JTAG_INIT_FAILED;
1905         }
1906         
1907         return ERROR_OK;
1908 }
1909
1910 int comstick_init(void)
1911 {
1912         u8 buf[3];
1913         u32 bytes_written;
1914         
1915         low_output = 0x08;
1916         low_direction = 0x0b;
1917         
1918         /* initialize low byte for jtag */
1919         buf[0] = 0x80; /* command "set data bits low byte" */
1920         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1921         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1922         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1923         
1924         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1925         {
1926                 ERROR("couldn't initialize FT2232 with 'comstick' layout"); 
1927                 return ERROR_JTAG_INIT_FAILED;
1928         }
1929         
1930         nTRST = 0x01;
1931         nTRSTnOE = 0x00; /* no output enable for nTRST */
1932         nSRST = 0x02;
1933         nSRSTnOE = 0x00; /* no output enable for nSRST */
1934         
1935         high_output = 0x03;
1936         high_direction = 0x03;
1937         
1938         /* initialize high port */
1939         buf[0] = 0x82; /* command "set data bits high byte" */
1940         buf[1] = high_output;
1941         buf[2] = high_direction;
1942         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1943         
1944         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1945         {
1946                 ERROR("couldn't initialize FT2232 with 'comstick' layout"); 
1947                 return ERROR_JTAG_INIT_FAILED;
1948         }
1949         
1950         return ERROR_OK;
1951 }
1952
1953 int stm32stick_init(void)
1954 {
1955         u8 buf[3];
1956         u32 bytes_written;
1957         
1958         low_output = 0x88;
1959         low_direction = 0x8b;
1960         
1961         /* initialize low byte for jtag */
1962         buf[0] = 0x80; /* command "set data bits low byte" */
1963         buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1964         buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1965         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1966         
1967         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1968         {
1969                 ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); 
1970                 return ERROR_JTAG_INIT_FAILED;
1971         }
1972                 
1973         nTRST = 0x01;
1974         nTRSTnOE = 0x00; /* no output enable for nTRST */
1975         nSRST = 0x80;
1976         nSRSTnOE = 0x00; /* no output enable for nSRST */
1977         
1978         high_output = 0x01;
1979         high_direction = 0x03;
1980         
1981         /* initialize high port */
1982         buf[0] = 0x82; /* command "set data bits high byte" */
1983         buf[1] = high_output;
1984         buf[2] = high_direction;
1985         DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1986         
1987         if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1988         {
1989                 ERROR("couldn't initialize FT2232 with 'stm32stick' layout"); 
1990                 return ERROR_JTAG_INIT_FAILED;
1991         }
1992         
1993         return ERROR_OK;
1994 }
1995
1996 void olimex_jtag_blink(void)
1997 {
1998         /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1999          * ACBUS3 is bit 3 of the GPIOH port
2000          */
2001         if (high_output & 0x08)
2002         {
2003                 /* set port pin high */
2004                 high_output &= 0x07;
2005         }
2006         else
2007         {
2008                 /* set port pin low */
2009                 high_output |= 0x08;
2010         }
2011         
2012         BUFFER_ADD = 0x82;
2013         BUFFER_ADD = high_output;
2014         BUFFER_ADD = high_direction;
2015 }
2016
2017 void turtle_jtag_blink(void)
2018 {
2019         /* 
2020    * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2021          */
2022         if (high_output & 0x08)
2023         {
2024                 high_output = 0x04;
2025         }
2026         else
2027         {
2028                 high_output = 0x08;
2029         }
2030         
2031         BUFFER_ADD = 0x82;
2032         BUFFER_ADD = high_output;
2033         BUFFER_ADD = high_direction;
2034 }
2035
2036
2037 int ft2232_quit(void)
2038 {
2039 #if BUILD_FT2232_FTD2XX == 1
2040         FT_STATUS status;
2041
2042         status = FT_Close(ftdih);
2043 #elif BUILD_FT2232_LIBFTDI == 1
2044         ftdi_disable_bitbang(&ftdic);
2045         
2046         ftdi_usb_close(&ftdic);
2047         
2048         ftdi_deinit(&ftdic);
2049 #endif
2050
2051         free(ft2232_buffer);
2052         ft2232_buffer = NULL;
2053
2054         return ERROR_OK;
2055 }
2056
2057 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2058 {
2059         if (argc == 1)
2060         {
2061                 ft2232_device_desc = strdup(args[0]);
2062         }
2063         else
2064         {
2065                 ERROR("expected exactly one argument to ft2232_device_desc <description>");
2066         }
2067         
2068         return ERROR_OK;
2069 }
2070
2071 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2072 {
2073         if (argc == 1)
2074         {
2075                 ft2232_serial = strdup(args[0]);
2076         }
2077         else
2078         {
2079                 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2080         }
2081         
2082         return ERROR_OK;
2083 }
2084
2085 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2086 {
2087         if (argc == 0)
2088                 return ERROR_OK;
2089
2090         ft2232_layout = malloc(strlen(args[0]) + 1);
2091         strcpy(ft2232_layout, args[0]);
2092
2093         return ERROR_OK;
2094 }
2095
2096 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2097 {
2098         int i;
2099
2100         if (argc > MAX_USB_IDS*2) {
2101                 WARNING("ignoring extra IDs in ft2232_vid_pid "
2102                     "(maximum is %d pairs)", MAX_USB_IDS);
2103                 argc = MAX_USB_IDS*2;
2104         }
2105         if (argc < 2 || (argc & 1))
2106         {
2107                 WARNING("incomplete ft2232_vid_pid configuration directive");
2108                 if (argc < 2)
2109                         return ERROR_OK;
2110         }
2111
2112         for (i = 0; i+1 < argc; i += 2) {
2113                 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2114                 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2115         }
2116         /*
2117          * Explicitly terminate, in case there are multiples instances of
2118          * ft2232_vid_pid.
2119          */
2120         ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2121
2122         return ERROR_OK;
2123 }
2124
2125 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2126 {
2127         if (argc == 1)
2128         {
2129                 ft2232_latency = atoi(args[0]);
2130         }
2131         else
2132         {
2133                 ERROR("expected exactly one argument to ft2232_latency <ms>");
2134         }
2135         
2136         return ERROR_OK;
2137 }
2138
2139