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