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