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