aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/java/beans/PersistenceDelegate.java
diff options
context:
space:
mode:
authorKai Tietz <kai.tietz@onevision.com>2008-05-04 19:19:50 +0000
committerKai Tietz <ktietz@gcc.gnu.org>2008-05-04 21:19:50 +0200
commit8fa8585de83aa9ff48ff462d3caf5f69411d46ab (patch)
tree8acc1198fa3497309d6375ff1d29fd984efd2400 /libjava/classpath/java/beans/PersistenceDelegate.java
parentda96f5ab49d8e45bdca0d08c5ecc408ddbc3de47 (diff)
downloadgcc-8fa8585de83aa9ff48ff462d3caf5f69411d46ab.zip
gcc-8fa8585de83aa9ff48ff462d3caf5f69411d46ab.tar.gz
gcc-8fa8585de83aa9ff48ff462d3caf5f69411d46ab.tar.bz2
i386.c (x86_output_mi_thunk): Use movq alternative mnemonic in this_param move for TARGET_64BIT.
2008-05-04 Kai Tietz <kai.tietz@onevision.com> * config/i386/i386.c (x86_output_mi_thunk): Use movq alternative mnemonic in this_param move for TARGET_64BIT. From-SVN: r134935
Diffstat (limited to 'libjava/classpath/java/beans/PersistenceDelegate.java')
0 files changed, 0 insertions, 0 deletions
7' href='#n207'>207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
/***************************************************************************
 *   Copyright (C) 2004, 2006 by Dominic Rath                              *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if IS_CYGWIN == 1
#include "windows.h"
#undef ERROR
#endif

#include "replacements.h"

/* project specific includes */
#include "log.h"
#include "types.h"
#include "jtag.h"
#include "configuration.h"
#include "time_support.h"

/* system includes */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/* FT2232 access library includes */
#if BUILD_FT2232_FTD2XX == 1
#include <ftd2xx.h>
#elif BUILD_FT2232_LIBFTDI == 1
#include <ftdi.h>
#endif

#include <sys/time.h>
#include <time.h>

/* enable this to debug io latency
 */
#if 0
#define _DEBUG_USB_IO_
#endif

/* enable this to debug communication
 */
#if 0
#define _DEBUG_USB_COMMS_
#endif

int ft2232_execute_queue(void);

int ft2232_speed(int speed);
int ft2232_register_commands(struct command_context_s *cmd_ctx);
int ft2232_init(void);
int ft2232_quit(void);

int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);

char *ft2232_device_desc = NULL;
char *ft2232_serial = NULL;
char *ft2232_layout = NULL;
unsigned char ft2232_latency = 2;

#define MAX_USB_IDS	8
/* vid = pid = 0 marks the end of the list */
static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };

typedef struct ft2232_layout_s
{
	char* name;
	int(*init)(void);
	void(*reset)(int trst, int srst);
	void(*blink)(void);
} ft2232_layout_t;

/* init procedures for supported layouts */
int usbjtag_init(void);
int jtagkey_init(void);
int olimex_jtag_init(void);
int flyswatter_init(void);
int turtle_init(void);
int comstick_init(void);
int stm32stick_init(void);

/* reset procedures for supported layouts */
void usbjtag_reset(int trst, int srst);
void jtagkey_reset(int trst, int srst);
void olimex_jtag_reset(int trst, int srst);
void flyswatter_reset(int trst, int srst);
void turtle_reset(int trst, int srst);
void comstick_reset(int trst, int srst);
void stm32stick_reset(int trst, int srst);

/* blink procedures for layouts that support a blinking led */
void olimex_jtag_blink(void);
void turtle_jtag_blink(void);

ft2232_layout_t ft2232_layouts[] =
{
	{"usbjtag", usbjtag_init, usbjtag_reset, NULL},
	{"jtagkey", jtagkey_init, jtagkey_reset, NULL},
	{"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
	{"oocdlink", jtagkey_init, jtagkey_reset, NULL},
	{"signalyzer", usbjtag_init, usbjtag_reset, NULL},
	{"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
	{"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
	{"flyswatter", flyswatter_init, flyswatter_reset, NULL},
	{"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
	{"comstick", comstick_init, comstick_reset, NULL},
	{"stm32stick", stm32stick_init, stm32stick_reset, NULL},
	{NULL, NULL, NULL},
};

static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;

static ft2232_layout_t *layout;
static u8 low_output = 0x0;
static u8 low_direction = 0x0;
static u8 high_output = 0x0;
static u8 high_direction = 0x0;

#if BUILD_FT2232_FTD2XX == 1
static FT_HANDLE ftdih = NULL;
#elif BUILD_FT2232_LIBFTDI == 1
static struct ftdi_context ftdic;
#endif

static u8 *ft2232_buffer = NULL;
static int ft2232_buffer_size = 0;
static int ft2232_read_pointer = 0;
static int ft2232_expect_read = 0;
#define FT2232_BUFFER_SIZE	131072
#define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
#define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]

jtag_interface_t ft2232_interface = 
{
	
	.name = "ft2232",
	
	.execute_queue = ft2232_execute_queue,
	
	.speed = ft2232_speed,
	.register_commands = ft2232_register_commands,
	.init = ft2232_init,
	.quit = ft2232_quit,
};

int ft2232_write(u8 *buf, int size, u32* bytes_written)
{
#if BUILD_FT2232_FTD2XX == 1
	FT_STATUS status;
	DWORD dw_bytes_written;
	if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
	{
		*bytes_written = dw_bytes_written;
		ERROR("FT_Write returned: %lu", status);
		return ERROR_JTAG_DEVICE_ERROR;
	}
	else
	{
		*bytes_written = dw_bytes_written;
		return ERROR_OK;	
	}
#elif BUILD_FT2232_LIBFTDI == 1
	int retval;
	if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
	{
		*bytes_written = 0;
		ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
		return ERROR_JTAG_DEVICE_ERROR;
	}
	else
	{
		*bytes_written = retval;
		return ERROR_OK;	
	}
#endif
}

int ft2232_read(u8* buf, int size, u32* bytes_read)
{
#if BUILD_FT2232_FTD2XX == 1
	DWORD dw_bytes_read;
	FT_STATUS status;
	int timeout = 5;
	*bytes_read = 0;

	while ((*bytes_read < size) && timeout--)
	{
		if ((status = FT_Read(ftdih, buf + *bytes_read, size - 
			*bytes_read, &dw_bytes_read)) != FT_OK)		
		{
			*bytes_read = 0; 
			ERROR("FT_Read returned: %lu", status);
			return ERROR_JTAG_DEVICE_ERROR;
		}
		*bytes_read += dw_bytes_read; 
	}
#elif BUILD_FT2232_LIBFTDI == 1
	int retval;
	int timeout = 100;
	*bytes_read = 0;
	
	while ((*bytes_read < size) && timeout--)
	{
		if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
		{
			*bytes_read = 0;
			ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
			return ERROR_JTAG_DEVICE_ERROR;
		}
		*bytes_read += retval;
	}
#endif

	if (*bytes_read < size)
	{
		ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
		return ERROR_JTAG_DEVICE_ERROR;
	}
	
	return ERROR_OK;
}

int ft2232_speed(int speed)
{
	u8 buf[3];
	int retval;
	u32 bytes_written;

	buf[0] = 0x86; /* command "set divisor" */
	buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
	buf[2] = (speed >> 8) & 0xff; /* valueH */
	
	DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
	if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
	{
		ERROR("couldn't set FT2232 TCK speed");
		return retval;
	}

	jtag_speed = speed;
	
	return ERROR_OK;
}

int ft2232_register_commands(struct command_context_s *cmd_ctx)
{
	register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
		COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
					 COMMAND_CONFIG, NULL);
	register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
					 COMMAND_CONFIG, NULL);
	return ERROR_OK;
}

void ft2232_end_state(enum tap_state state)
{
	if (tap_move_map[state] != -1)
		end_state = state;
	else
	{
		ERROR("BUG: %i is not a valid end state", state);
		exit(-1);
	}
}

void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
{
	int num_bytes = ((scan_size + 7) / 8);
	int bits_left = scan_size;
	int cur_byte = 0;

	while(num_bytes-- > 1)
	{
		buffer[cur_byte] = BUFFER_READ;
		cur_byte++;
		bits_left -= 8;
	}

	buffer[cur_byte] = 0x0;

	if (bits_left > 1)
	{
		buffer[cur_byte] = BUFFER_READ >> 1;
	}

	buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);

}

void ft2232_debug_dump_buffer(void)
{
	int i;
	char line[256];
	char *line_p = line;
	
	for (i = 0; i < ft2232_buffer_size; i++)
	{
		line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
		if (i % 16 == 15)
		{
			DEBUG("%s", line);
			line_p = line;
		}
	}
	
	if (line_p != line)
		DEBUG("%s", line);
}

int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
{
	jtag_command_t *cmd;
	u8 *buffer;
	int scan_size;
	enum scan_type type;
	int retval;
	u32 bytes_written;
	u32 bytes_read;
	
#ifdef _DEBUG_USB_IO_
	struct timeval start, inter, inter2, end;
	struct timeval d_inter, d_inter2, d_end;
#endif

#ifdef _DEBUG_USB_COMMS_
	DEBUG("write buffer (size %i):", ft2232_buffer_size);
	ft2232_debug_dump_buffer();
#endif

#ifdef _DEBUG_USB_IO_
	gettimeofday(&start, NULL);	
#endif

	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
		ERROR("couldn't write MPSSE commands to FT2232");
		exit(-1);
	}
	
#ifdef _DEBUG_USB_IO_
	gettimeofday(&inter, NULL);	
#endif
	
	if (ft2232_expect_read)
	{
		int timeout = 100;
		ft2232_buffer_size = 0;
		
#ifdef _DEBUG_USB_IO_
		gettimeofday(&inter2, NULL);	
#endif
		
		if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
		{
			ERROR("couldn't read from FT2232");
			exit(-1);
		}
		
#ifdef _DEBUG_USB_IO_
		gettimeofday(&end, NULL);	

		timeval_subtract(&d_inter, &inter, &start);
		timeval_subtract(&d_inter2, &inter2, &start);
		timeval_subtract(&d_end, &end, &start);

		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);
#endif
	
		
		ft2232_buffer_size = bytes_read;
		
		if (ft2232_expect_read != ft2232_buffer_size)
		{
			ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
			ft2232_debug_dump_buffer();	

			exit(-1);
		}

#ifdef _DEBUG_USB_COMMS_
		DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
		ft2232_debug_dump_buffer();
#endif
	}

	ft2232_expect_read = 0;
	ft2232_read_pointer = 0;
	
	/* return ERROR_OK, unless a jtag_read_buffer returns a failed check
	 * that wasn't handled by a caller-provided error handler
	 */ 
	retval = ERROR_OK;
	
	cmd = first;
	while (cmd != last)
	{
		switch (cmd->type)
		{
			case JTAG_SCAN:
				type = jtag_scan_type(cmd->cmd.scan);
				if (type != SCAN_OUT)
				{
					scan_size = jtag_scan_size(cmd->cmd.scan);
					buffer = calloc(CEIL(scan_size, 8), 1);
					ft2232_read_scan(type, buffer, scan_size);
					if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					free(buffer);
				}
				break;
			default:
				break;
		}
		cmd = cmd->next;
	}
	
	ft2232_buffer_size = 0;

	return retval;
}

void ft2232_add_pathmove(pathmove_command_t *cmd)
{
	int num_states = cmd->num_states;
	u8 tms_byte;
	int state_count;

	state_count = 0;
	while (num_states)
	{
		tms_byte = 0x0;
		int bit_count = 0;
		
		int num_states_batch = num_states > 7 ? 7 : num_states;

		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* number of states remaining */
		BUFFER_ADD = num_states_batch - 1;
		
		while (num_states_batch--)
		{
			if (tap_transitions[cur_state].low == cmd->path[state_count])
				buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
			else if (tap_transitions[cur_state].high == cmd->path[state_count])
				buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
			else
			{
				ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
				exit(-1);
			}

			cur_state = cmd->path[state_count];
			state_count++;
			num_states--;
		}
		
		BUFFER_ADD = tms_byte;
	}
	
	end_state = cur_state;
}

void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
{
	int num_bytes = (scan_size + 7) / 8;
	int bits_left = scan_size;
	int cur_byte = 0;
	int last_bit;

	if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
	{
		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* scan 7 bit */
		BUFFER_ADD = 0x6;
		/* TMS data bits */
		if (ir_scan)
		{
			BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
			cur_state = TAP_SI;
		}
		else
		{
			BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
			cur_state = TAP_SD;
		}
		/* DEBUG("added TMS scan (no read)"); */
	}
	
	/* add command for complete bytes */
	while (num_bytes > 1)
	{
		int thisrun_bytes;
		if (type == SCAN_IO)
		{
			/* Clock Data Bytes In and Out LSB First */
			BUFFER_ADD = 0x39;
			/* DEBUG("added TDI bytes (io %i)", num_bytes); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x19;
			/* DEBUG("added TDI bytes (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x28;
			/* DEBUG("added TDI bytes (i %i)", num_bytes); */
		}
		thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
		num_bytes -= thisrun_bytes;
		BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
		BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
		if (type != SCAN_IN)
		{
			/* add complete bytes */
			while(thisrun_bytes-- > 0)
			{
				BUFFER_ADD = buffer[cur_byte];
				cur_byte++;
				bits_left -= 8;
			}
		}
		else /* (type == SCAN_IN) */
		{
			bits_left -= 8 * (thisrun_bytes);
		}
	}
	
	/* the most signifcant bit is scanned during TAP movement */
	if (type != SCAN_IN)
		last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
	else
		last_bit = 0;

	/* process remaining bits but the last one */
	if (bits_left > 1)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
			/* DEBUG("added TDI bits (io) %i", bits_left - 1); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
			/* DEBUG("added TDI bits (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
			/* DEBUG("added TDI bits (i %i)", bits_left - 1); */
		}
		BUFFER_ADD = bits_left - 2;
		if (type != SCAN_IN)
			BUFFER_ADD = buffer[cur_byte];
	}

	if ((ir_scan && (end_state == TAP_SI)) ||
		(!ir_scan && (end_state == TAP_SD)))
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
			/* DEBUG("added TDI bits (io) %i", bits_left - 1); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
			/* DEBUG("added TDI bits (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
			/* DEBUG("added TDI bits (i %i)", bits_left - 1); */
		}
		BUFFER_ADD = 0x0;
		BUFFER_ADD = last_bit;
	}
	else
	{
		/* move from Shift-IR/DR to end state */
		if (type != SCAN_OUT)
		{
			/* Clock Data to TMS/CS Pin with Read */
			BUFFER_ADD = 0x6b;
			/* DEBUG("added TMS scan (read)"); */
		}
		else
		{
			/* Clock Data to TMS/CS Pin (no Read) */
			BUFFER_ADD = 0x4b;
			/* DEBUG("added TMS scan (no read)"); */
		}
		BUFFER_ADD = 0x6;
		BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
		cur_state = end_state;
	}
}

int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
{
	int num_bytes = (scan_size + 7) / 8;
	int bits_left = scan_size;
	int cur_byte = 0;
	int last_bit;
	u8 *receive_buffer = malloc(CEIL(scan_size, 8));
	u8 *receive_pointer = receive_buffer;
	u32 bytes_written;
	u32 bytes_read;
	int retval;
	int thisrun_read = 0;
	
	if (cmd->ir_scan)
	{
		ERROR("BUG: large IR scans are not supported");
		exit(-1);
	}

	if (cur_state != TAP_SD)
	{
		/* command "Clock Data to TMS/CS Pin (no Read)" */
		BUFFER_ADD = 0x4b;
		/* scan 7 bit */
		BUFFER_ADD = 0x6;
		/* TMS data bits */
		BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
		cur_state = TAP_SD;
	}
	
	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
		ERROR("couldn't write MPSSE commands to FT2232");
		exit(-1);
	}
	DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
	ft2232_buffer_size = 0;
	
	/* add command for complete bytes */
	while (num_bytes > 1)
	{
		int thisrun_bytes;
		
		if (type == SCAN_IO)
		{
			/* Clock Data Bytes In and Out LSB First */
			BUFFER_ADD = 0x39;
			/* DEBUG("added TDI bytes (io %i)", num_bytes); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x19;
			/* DEBUG("added TDI bytes (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x28;
			/* DEBUG("added TDI bytes (i %i)", num_bytes); */
		}
		thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
		thisrun_read = thisrun_bytes;
		num_bytes -= thisrun_bytes;
		BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
		BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
		if (type != SCAN_IN)
		{
			/* add complete bytes */
			while(thisrun_bytes-- > 0)
			{
				BUFFER_ADD = buffer[cur_byte];
				cur_byte++;
				bits_left -= 8;
			}
		}
		else /* (type == SCAN_IN) */
		{
			bits_left -= 8 * (thisrun_bytes);
		}

		if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
		{
			ERROR("couldn't write MPSSE commands to FT2232");
			exit(-1);
		}
		DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
		ft2232_buffer_size = 0;
		
		if (type != SCAN_OUT)
		{
			if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
			{
				ERROR("couldn't read from FT2232");
				exit(-1);
			}
			DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
			receive_pointer += bytes_read;
		}
	}
	
	thisrun_read = 0;
	
	/* the most signifcant bit is scanned during TAP movement */
	if (type != SCAN_IN)
		last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
	else
		last_bit = 0;

	/* process remaining bits but the last one */
	if (bits_left > 1)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
			/* DEBUG("added TDI bits (io) %i", bits_left - 1); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
			/* DEBUG("added TDI bits (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
			/* DEBUG("added TDI bits (i %i)", bits_left - 1); */
		}
		BUFFER_ADD = bits_left - 2;
		if (type != SCAN_IN)
			BUFFER_ADD = buffer[cur_byte];
			
		if (type != SCAN_OUT)
			thisrun_read += 2;
	}

	if (end_state == TAP_SD)
	{
		if (type == SCAN_IO)
		{
			/* Clock Data Bits In and Out LSB First */
			BUFFER_ADD = 0x3b;
			/* DEBUG("added TDI bits (io) %i", bits_left - 1); */
		}
		else if (type == SCAN_OUT)
		{
			/* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
			BUFFER_ADD = 0x1b;
			/* DEBUG("added TDI bits (o)"); */
		}
		else if (type == SCAN_IN)
		{
			/* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
			BUFFER_ADD = 0x2a;
			/* DEBUG("added TDI bits (i %i)", bits_left - 1); */
		}
		BUFFER_ADD = 0x0;
		BUFFER_ADD = last_bit;
	}
	else
	{
		/* move from Shift-IR/DR to end state */
		if (type != SCAN_OUT)
		{
			/* Clock Data to TMS/CS Pin with Read */
			BUFFER_ADD = 0x6b;
			/* DEBUG("added TMS scan (read)"); */
		}
		else
		{
			/* Clock Data to TMS/CS Pin (no Read) */
			BUFFER_ADD = 0x4b;
			/* DEBUG("added TMS scan (no read)"); */
		}
		BUFFER_ADD = 0x6;
		BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
		cur_state = end_state;
	}
	
	if (type != SCAN_OUT)
		thisrun_read += 1;
	
	if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
	{
		ERROR("couldn't write MPSSE commands to FT2232");
		exit(-1);
	}
	DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
	ft2232_buffer_size = 0;
	
	if (type != SCAN_OUT)
	{
		if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
		{
			ERROR("couldn't read from FT2232");
			exit(-1);
		}
		DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
		receive_pointer += bytes_read;
	}
	
	return ERROR_OK;
}

int ft2232_predict_scan_out(int scan_size, enum scan_type type)
{
	int predicted_size = 3;
	int num_bytes = (scan_size - 1) / 8;
	
	if (cur_state != TAP_SD)
		predicted_size += 3;
	
	if (type == SCAN_IN)	/* only from device to host */
	{
		/* complete bytes */
		predicted_size += (CEIL(num_bytes, 65536)) * 3;
		/* remaining bits - 1 (up to 7) */
		predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
	}
	else					/* host to device, or bidirectional */
	{
		/* complete bytes */
		predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
		/* remaining bits -1 (up to 7) */
		predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
	}

	return predicted_size;
}

int ft2232_predict_scan_in(int scan_size, enum scan_type type)
{
	int predicted_size = 0;
	
	if (type != SCAN_OUT)
	{
		/* complete bytes */
		predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
		/* remaining bits - 1 */
		predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
		/* last bit (from TMS scan) */
		predicted_size += 1;
	}
	
	/* DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */

	return predicted_size;
}

void usbjtag_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			low_direction |= nTRSTnOE;	/* switch to output pin (output is low) */
		else
			low_output &= ~nTRST;	/* switch output low */
	}
	else if (trst == 0)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
		else
			low_output |= nTRST; /* switch output high */
	}

	if (srst == 1)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			low_output &= ~nSRST;	/* switch output low */
		else
			low_direction |= nSRSTnOE;	/* switch to output pin (output is low) */
	}
	else if (srst == 0)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			low_output |= nSRST;	/* switch output high */
		else
			low_direction &= ~nSRSTnOE;	/* switch to input pin (high-Z) */
	}
	
	/* command "set data bits low byte" */
	BUFFER_ADD = 0x80;
	BUFFER_ADD = low_output;
	BUFFER_ADD = low_direction;

}

void jtagkey_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output &= ~nTRSTnOE;
		else
			high_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output |= nTRSTnOE;
		else
			high_output |= nTRST;
	}

	if (srst == 1)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			high_output &= ~nSRST;
		else
			high_output &= ~nSRSTnOE;
	}
	else if (srst == 0)
	{
		if (jtag_reset_config & RESET_SRST_PUSH_PULL)
			high_output |= nSRST;
		else
			high_output |= nSRSTnOE;
	}
	
	/* command "set data bits high byte" */
	BUFFER_ADD = 0x82;
	BUFFER_ADD = high_output;
	BUFFER_ADD = high_direction;
	DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
}

void olimex_jtag_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output &= ~nTRSTnOE;
		else
			high_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
			high_output |= nTRSTnOE;
		else
			high_output |= nTRST;
	}

    if (srst == 1)
    {
        high_output |= nSRST;
    }
    else if (srst == 0)
    {
        high_output &= ~nSRST;
    }

    /* command "set data bits high byte" */
    BUFFER_ADD = 0x82;
    BUFFER_ADD = high_output;
    BUFFER_ADD = high_direction;
    DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
}

void flyswatter_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		low_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		low_output |= nTRST;
	}

    if (srst == 1)
    {
        low_output |= nSRST;
    }
    else if (srst == 0)
    {
        low_output &= ~nSRST;
    }

    /* command "set data bits low byte" */
    BUFFER_ADD = 0x80;
    BUFFER_ADD = low_output;
    BUFFER_ADD = low_direction;
    DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
}

void turtle_reset(int trst, int srst)
{
	trst = trst;
	
	if (srst == 1)
	{
		low_output |= nSRST;
	}
	else if (srst == 0)
	{
		low_output &= ~nSRST;
	}
	
	/* command "set data bits low byte" */
	BUFFER_ADD = 0x80;
	BUFFER_ADD = low_output;
	BUFFER_ADD = low_direction;
	DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
}

void comstick_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		high_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		high_output |= nTRST;
	}

    if (srst == 1)
    {
        high_output &= ~nSRST;
    }
    else if (srst == 0)
    {
        high_output |= nSRST;
    }
	
	/* command "set data bits high byte" */
	BUFFER_ADD = 0x82;
	BUFFER_ADD = high_output;
	BUFFER_ADD = high_direction;
	DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
}

void stm32stick_reset(int trst, int srst)
{
	if (trst == 1)
	{
		cur_state = TAP_TLR;
		high_output &= ~nTRST;
	}
	else if (trst == 0)
	{
		high_output |= nTRST;
	}

    if (srst == 1)
    {
        low_output &= ~nSRST;
    }
    else if (srst == 0)
    {
        low_output |= nSRST;
    }
	
	/* command "set data bits low byte" */
	BUFFER_ADD = 0x80;
	BUFFER_ADD = low_output;
	BUFFER_ADD = low_direction;
	
	/* command "set data bits high byte" */
	BUFFER_ADD = 0x82;
	BUFFER_ADD = high_output;
	BUFFER_ADD = high_direction;
	DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
}

int ft2232_execute_queue()
{
	jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
	jtag_command_t *first_unsent = cmd;	/* next command that has to be sent */
	u8 *buffer;
	int scan_size;	/* size of IR or DR scan */
	enum scan_type type;
	int i;
	int predicted_size = 0;
	int require_send = 0;
	int retval;
	
	/* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
	 * that wasn't handled by a caller-provided error handler
	 */ 
	retval = ERROR_OK;

	ft2232_buffer_size = 0;
	ft2232_expect_read = 0;
	
	/* blink, if the current layout has that feature */
	if (layout->blink)
		layout->blink();

	while (cmd)
	{
		switch(cmd->type)
		{
			case JTAG_END_STATE:
				if (cmd->cmd.end_state->end_state != -1)
					ft2232_end_state(cmd->cmd.end_state->end_state);
				break;
			case JTAG_RESET:
				/* only send the maximum buffer size that FT2232C can handle */
				predicted_size = 3;
				if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
				{
					if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					require_send = 0;
					first_unsent = cmd;
				}

				layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
				require_send = 1;
				
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
#endif
				break;
			case JTAG_RUNTEST:
				/* only send the maximum buffer size that FT2232C can handle */
				predicted_size = 0;
				if (cur_state != TAP_RTI)
					predicted_size += 3;
				predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
				if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
					predicted_size += 3;
				if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
					predicted_size += 3;
				if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
				{
					if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					require_send = 0;
					first_unsent = cmd;
				}
				if (cur_state != TAP_RTI)
				{
					/* command "Clock Data to TMS/CS Pin (no Read)" */
					BUFFER_ADD = 0x4b;
					/* scan 7 bit */
					BUFFER_ADD = 0x6;
					/* TMS data bits */
					BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
					cur_state = TAP_RTI;
					require_send = 1;
				}
				i = cmd->cmd.runtest->num_cycles;
				while (i > 0)
				{
					/* command "Clock Data to TMS/CS Pin (no Read)" */
					BUFFER_ADD = 0x4b;
					/* scan 7 bit */
					BUFFER_ADD = (i > 7) ? 6 : (i - 1);
					/* TMS data bits */
					BUFFER_ADD = 0x0;
					cur_state = TAP_RTI;
					i -= (i > 7) ? 7 : i;
					/* DEBUG("added TMS scan (no read)"); */
				}
				if (cmd->cmd.runtest->end_state != -1)
					ft2232_end_state(cmd->cmd.runtest->end_state);
				if (cur_state != end_state)
				{
					/* command "Clock Data to TMS/CS Pin (no Read)" */
					BUFFER_ADD = 0x4b;
					/* scan 7 bit */
					BUFFER_ADD = 0x6;
					/* TMS data bits */
					BUFFER_ADD = TAP_MOVE(cur_state, end_state);
					cur_state = end_state;
					/* DEBUG("added TMS scan (no read)"); */
				}
				require_send = 1;
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
#endif
				break;
			case JTAG_STATEMOVE:
				/* only send the maximum buffer size that FT2232C can handle */
				predicted_size = 3;
				if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
				{
					if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					require_send = 0;
					first_unsent = cmd;
				}
				if (cmd->cmd.statemove->end_state != -1)
					ft2232_end_state(cmd->cmd.statemove->end_state);
				/* command "Clock Data to TMS/CS Pin (no Read)" */
				BUFFER_ADD = 0x4b;
				/* scan 7 bit */
				BUFFER_ADD = 0x6;
				/* TMS data bits */
				BUFFER_ADD = TAP_MOVE(cur_state, end_state);
				/* DEBUG("added TMS scan (no read)"); */
				cur_state = end_state;
				require_send = 1;
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("statemove: %i", end_state);
#endif
				break;
			case JTAG_PATHMOVE:
				/* only send the maximum buffer size that FT2232C can handle */
				predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
				if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
				{
					if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					require_send = 0;
					first_unsent = cmd;
				}
				ft2232_add_pathmove(cmd->cmd.pathmove);
				require_send = 1;
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
#endif
				break;
			case JTAG_SCAN:
				scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
				type = jtag_scan_type(cmd->cmd.scan);
				predicted_size = ft2232_predict_scan_out(scan_size, type);
				if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
				{
					DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
					/* unsent commands before this */
					if (first_unsent != cmd)
						if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
							retval = ERROR_JTAG_QUEUE_FAILED;
					
					/* current command */
					if (cmd->cmd.scan->end_state != -1)
						ft2232_end_state(cmd->cmd.scan->end_state);
					ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
					require_send = 0;
					first_unsent = cmd->next;
					if (buffer)
						free(buffer);
					break;
				}
				else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
				{
					DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
					if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
						retval = ERROR_JTAG_QUEUE_FAILED;
					require_send = 0;
					first_unsent = cmd;
				}
				ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
				/* DEBUG("new read size: %i", ft2232_expect_read); */
				if (cmd->cmd.scan->end_state != -1)
					ft2232_end_state(cmd->cmd.scan->end_state);
				ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
				require_send = 1;
				if (buffer)
					free(buffer);
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
#endif
				break;
			case JTAG_SLEEP:
				if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
					retval = ERROR_JTAG_QUEUE_FAILED;
				first_unsent = cmd->next;
				jtag_sleep(cmd->cmd.sleep->us);
#ifdef _DEBUG_JTAG_IO_				
				DEBUG("sleep %i usec", cmd->cmd.sleep->us);
#endif
				break;
			default:
				ERROR("BUG: unknown JTAG command type encountered");
				exit(-1);
		}
		cmd = cmd->next;
	}

	if (require_send > 0)
		if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
			retval = ERROR_JTAG_QUEUE_FAILED;

	return retval;
}

#if BUILD_FT2232_FTD2XX == 1
static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
{
	FT_STATUS status;
	DWORD openex_flags = 0;
	char *openex_string = NULL;
	u8 latency_timer;

	DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
	    ft2232_layout, vid, pid);

#if IS_WIN32 == 0
	/* Add non-standard Vid/Pid to the linux driver */
	if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
	{
		WARNING("couldn't add %4.4x:%4.4x",
		    vid, pid);
	}
#endif

	if (ft2232_device_desc && ft2232_serial)
	{
		WARNING("can't open by device description and serial number, giving precedence to serial");
		ft2232_device_desc = NULL;
	}
	
	if (ft2232_device_desc)
	{
		openex_string = ft2232_device_desc;
		openex_flags = FT_OPEN_BY_DESCRIPTION;
	}
	else if (ft2232_serial)
	{
		openex_string = ft2232_serial;
		openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
	}
	else
	{
		ERROR("neither device description nor serial number specified");
		ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
		
		return ERROR_JTAG_INIT_FAILED;	
	}

	if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
	{
		DWORD num_devices;
		
		if (more) {
			WARNING("unable to open ftdi device (trying more): %lu",
			    status);
			*try_more = 1;
			return ERROR_JTAG_INIT_FAILED;
		}
		ERROR("unable to open ftdi device: %lu", status);
		status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
		if (status == FT_OK)
		{
			char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
			int i;

			for (i = 0; i < num_devices; i++)
				desc_array[i] = malloc(64);
			desc_array[num_devices] = NULL;

			status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);

			if (status == FT_OK)
			{
				ERROR("ListDevices: %lu\n", num_devices);
				for (i = 0; i < num_devices; i++)
					ERROR("%i: %s", i, desc_array[i]);
			}
			
			for (i = 0; i < num_devices; i++)
				free(desc_array[i]);
			free(desc_array);
		}
		else
		{
			ERROR("ListDevices: NONE\n");
		}
		return ERROR_JTAG_INIT_FAILED;
	}

	if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
	{
		ERROR("unable to set latency timer: %lu", status);
		return ERROR_JTAG_INIT_FAILED;
	}
	
	if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
	{
		ERROR("unable to get latency timer: %lu", status);
		return ERROR_JTAG_INIT_FAILED;
	}
	else
	{
		DEBUG("current latency timer: %i", latency_timer);
	}
	
	if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
	{
		ERROR("unable to set timeouts: %lu", status);
		return ERROR_JTAG_INIT_FAILED;
	}

	if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
	{
		ERROR("unable to enable bit i/o mode: %lu", status);
		return ERROR_JTAG_INIT_FAILED;
	}

	return ERROR_OK;
}

static int ft2232_purge_ftd2xx(void)
{
	FT_STATUS status;

	if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
	{
		ERROR("error purging ftd2xx device: %lu", status);
		return ERROR_JTAG_INIT_FAILED;
	}

	return ERROR_OK;
}
#endif /* BUILD_FT2232_FTD2XX == 1 */

#if BUILD_FT2232_LIBFTDI == 1
static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
{
	u8 latency_timer;

	DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
	    ft2232_layout, vid, pid);

	if (ftdi_init(&ftdic) < 0)
		return ERROR_JTAG_INIT_FAILED;

	/* context, vendor id, product id */
	if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
	    ft2232_serial) < 0) {
		if (more)
			WARNING("unable to open ftdi device (trying more): %s",
			     ftdic.error_str);
		else
			ERROR("unable to open ftdi device: %s", ftdic.error_str);
		*try_more = 1;
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
	{
		ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_usb_reset(&ftdic) < 0)
	{
		ERROR("unable to reset ftdi device");
		return ERROR_JTAG_INIT_FAILED;
	}

	if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
	{
		ERROR("unable to set latency timer");
		return ERROR_JTAG_INIT_FAILED;
	}
	
	if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
	{
		ERROR("unable to get latency timer");
		return ERROR_JTAG_INIT_FAILED;
	}
	else
	{
		DEBUG("current latency timer: %i", latency_timer);
	}

	ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */

	return ERROR_OK;
}

static int ft2232_purge_libftdi(void)
{
	if (ftdi_usb_purge_buffers(&ftdic) < 0)
	{
		ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
		return ERROR_JTAG_INIT_FAILED;
	}

	return ERROR_OK;
}
#endif /* BUILD_FT2232_LIBFTDI == 1 */

int ft2232_init(void)
{
	u8 buf[1];
	int retval;
	u32 bytes_written;
	ft2232_layout_t *cur_layout = ft2232_layouts;
	int i;
	
	if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
	{
		ft2232_layout = "usbjtag";
		WARNING("No ft2232 layout specified, using default 'usbjtag'");
	}
	
	while (cur_layout->name)
	{
		if (strcmp(cur_layout->name, ft2232_layout) == 0)
		{
			layout = cur_layout;
			break;
		}
		cur_layout++;
	}

	if (!layout)
	{
		ERROR("No matching layout found for %s", ft2232_layout);
		return ERROR_JTAG_INIT_FAILED;
	}
	
	for (i = 0; 1; i++) {
		/*
		 * "more indicates that there are more IDs to try, so we should
		 * not print an error for an ID mismatch (but for anything