ipod.c 45.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
#include "os_config.h"

#include "c_def.h"
#include "debug.h"
#include "oem.h"


#ifdef IPOD_USB_AUDIO

#include "regmap.h"

#include "mem_reloc.h"
#include "tools.h"
#include "IRP.h"
#include "usbd.h"
#include "message.h"

#include "isrc.h"
#include "audio.h"
#include "ipoddev.h"
#include "lingo.h"
#include "iap.h"
#include "ipod.h"

#include "app_timer.h"
#include "app_cmd.h"
#include "app_main.h"
#include "src.h"
#include <string.h>

#include "app_sdram.h"

#ifdef	APPS_HARBOUR_LIGHT
#include "apps_hl.h"
#endif


//U8 ipodTest = 0;

IPOD_DEV_INFO	ipodInfo __IPHONE_BSS;
int iPod_track_sample_rate;


U8 *U32ToPtrb( U32 d, U8 *ptr )
{
	ptr[0]	=	d >> 24;
	ptr[1]	=	d >> 16;
	ptr[2]	=	d >> 8;
	ptr[3]	=	d;

	return ptr + 4;
}

U16 byteToU16b( U8 *ptr )
{
	U16 d
		=	(ptr[0] << 8) | ptr[1];
	return d;
}

U32	byteToU32b( U8 *ptr )
{
	//big endian
	U32	l
		=	(ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];

	return l;
}

U32	endianConvertU32 ( U32 b )
{
	U32	l
		=	(b >> 24) | ( (b >> 8) & 0xFF00 ) | (b << 24) | ((b << 8) & 0xFF0000);

	return l;
}

U16	endianConvertU16 ( U16 b )
{
	U16	l
		=	(b >> 8) | (b << 8);
	return l;
}

//***************************************************************************************
//	i2c/achip


//FIXME:	need modify to use hardware i2c
#include "app_i2c.h"		//use soft i2c temporarily, share with audio codec will cause problem
#include "hw_i2c.h"
#include "hw_i2cPoll.h"


#ifdef ACHIP_V20C
#define	ACHIP_I2C_ADDR		0x22		//mode1/mode0 = 11
#else
#define	ACHIP_I2C_ADDR		0x20		//mode1/mode0 = 10
#endif

//#define	USE_HW_I2C
#define	USE_SOFT_I2C
//#define	USE_HW_I2C_POLL

#ifdef ACHIP_V20C
#define	I2C_CLOCK_HZ		300*1000	// 200K, max 400K
#else
#define	I2C_CLOCK_HZ		30*1000		// 30K, max 50K
#endif


#define	MAX_PAGE_DATA_LEN		128
#define	MAX_CHALLEN_LEN			20
#define	MAX_DATA_LEN_SIZE		2

#define	BLOCK0_REG_START_ADDR	0x00
#define	BLOCK0_REG_SIZE			0x09

#define	REG_ADDR_ERR_CODE		0x05
#define	REG_ADDR_CTL_STATUS		0x10

#define	REG_ADDR_SIGN_LEN		0x11
#define	REG_ADDR_SIGN_DATA		0x12
#define	REG_ADDR_CHALLEN_LEN	0x20
#define	REG_ADDR_CHALLEN_DATA	0x21

#define	REG_ADDR_ACC_CERTI_LEN		0x30
#define	REG_ADDR_ACC_CERTI_DATA_START	0x31
#define	REG_ADDR_ACC_CERTI_DATA_END		0x3F
#define	REG_ACC_CERTI_PAGES		15

#define	REG_ADDR_SELF_TEST		0x40


typedef	struct {
	U8	devVer;
	U8	firmwareVer;
	U8	authenProtMajVer;
	U8	authenProtMinVer;
	U32	devId;

	int	certlen;
	U8	*challenge;
	int	chalen;
	int	siglen;

	U8	*buf;
	int	maxbufSize;
}	AUTHEN_CHIP_INFO;

AUTHEN_CHIP_INFO	authenChip	__IPHONE_BSS;

#define	MAX_ACHIP_BUF_SIZE		2048
U8	achipBuffer[MAX_ACHIP_BUF_SIZE]	__IPHONE_BSS;


#ifdef	USE_HW_I2C
int achip_i2c_write(U8 regAddr, U8 *buf, int len)
{
	return( hwi2c_writeSub( ACHIP_I2C_ADDR, regAddr, buf, len ) );
}
int achip_i2c_read(U8 regAddr, U8 *buf, int len )
{
	return ( hwi2c_readSub( ACHIP_I2C_ADDR, regAddr, buf, len ) );
}
#endif

#ifdef	USE_SOFT_I2C

int achip_i2c_write(U8 regAddr, U8 *buf, int len)
{
	int ret;

#if 0
	ret	=	I2C0_write_sub( ACHIP_I2C_ADDR, regAddr, buf, len );
#else
	ret	=	I2C0_achip_write_sub( ACHIP_I2C_ADDR, regAddr, buf, len );
#endif

	DBG_assert( ret == I2C_SUCCESS );

	return (ret == I2C_SUCCESS );
}


int achip_i2c_read(U8 regAddr, U8 *buf, int len )
{
	int ret;

#if 0
	ret	=	I2C0_read_sub( ACHIP_I2C_ADDR, regAddr, buf, len );
#else
	ret	=	I2C0_achip_read_sub( ACHIP_I2C_ADDR, regAddr, buf, len );
#endif

	DBG_assert( ret == I2C_SUCCESS );

	return (ret == I2C_SUCCESS );
}
#endif

#ifdef	USE_HW_I2C_POLL
int achip_i2c_write(U8 regAddr, U8 *buf, int len)
{
	int r;

	r = hwi2cPoll_write( ACHIP_I2C_ADDR, regAddr, buf, len );

	return (r == 0 );
}

int achip_i2c_read(U8 regAddr, U8 *buf, int len )
{
	int r;

	r = hwi2cPoll_read( ACHIP_I2C_ADDR, regAddr, buf, len );

	return (r == 0 );
}
#endif

int achip_readAccCertificate( U8 *buf, int *len )
{
	AUTHEN_CHIP_INFO	*achip	=	&authenChip;
	U16	rlen;
	*len	=	0;

	int ret
		=	achip_i2c_read( REG_ADDR_ACC_CERTI_LEN, (U8*)&rlen, 2 );
	DBG_assert( ret );

	if( !ret ) return FALSE;

	rlen	=	endianConvertU16( rlen );
	*len	=	rlen;
	DBG_assert( achip->maxbufSize >= rlen );

	ret	=	achip_i2c_read( REG_ADDR_ACC_CERTI_DATA_START, buf, rlen );
	DBG_assert( ret );

	return ret;
}

int achip_writeChallenge( U8 *chall, U16 len )
{
	int ret;
	U16	len2;

	DBG_assert( len <= MAX_CHALLEN_LEN  );

	len2	=	endianConvertU16(len);
	ret	=	achip_i2c_write( REG_ADDR_CHALLEN_LEN, (U8*)&len2, 2 );
	DBG_assert( ret );

	if( ret ) {
		ret	=	achip_i2c_write( REG_ADDR_CHALLEN_DATA, chall, len );
		DBG_assert( ret );
	}

	return ret;
}

int achip_readSignature( U8 *sig, int *len )
{
	U16	len2;
	int ret;
	*len	=	0;

	ret	=	achip_i2c_read( REG_ADDR_SIGN_LEN, (U8*)&len2, 2 );
	DBG_assert( ret );

	if( ret ) {
		len2	=	endianConvertU16(len2);
		*len	=	len2;

		ret	=	achip_i2c_read( REG_ADDR_SIGN_DATA, sig, len2 );
		DBG_assert( ret );
	}

	return ret;
}

#define	ACHIP_ERR_CODE_NO_ERR		0x00
int achip_readErrCode( U8 *code )
{
	int ret
		=	achip_i2c_read( REG_ADDR_ERR_CODE, code, 1 );
	DBG_assert( ret );
	DBG_assert( 0 == *code );
	
	return ret;
}

//write
#define	AUTHEN_PROC_NONE		0x00
#define	AUTHEN_PROC_SIG_GEN		0x01
#define	AUTHEN_PROC_CHALL_GEN	0x02
#define	AUTHEN_PROC_SIG_VERIFY	0x03
#define	AUTHEN_PROC_CERT_VALID	0x04
#define	AUTHEN_PROC_SLEEP		0x05


int achip_writeAuthenCtlStat( U8 ctl )
{
	int ret	=	achip_i2c_write( REG_ADDR_CTL_STATUS, &ctl, 1 );
	DBG_assert( ret );

	return ret;
}


//read, ctl/stat register
#define	AUTHEN_ERR_SET					0x80

#define	AUTHEN_STAT_MASK				(7 << 4)

#define	AUTHEN_PROC_RESLT_NOT_VALID		(0 << 4)
#define	AUTHEN_ACC_SIGNATURE_OK			(1 << 4)
#define	AUTHEN_CHALLENGE_OK				(2 << 4)
#define	AUTHEN_IPOD_SIGNATURE_OK		(3 << 4)
#define	AUTHEN_IPOD_CERTI_OK			(4 << 4)

int achip_readAuthenCtlStat( U8 *stat )
{
	int ret;
	U8	status;

	ret	=	achip_i2c_read( REG_ADDR_CTL_STATUS, &status, 1 );
	DBG_assert( ret );

	*stat	=	0;
	if( ret ) {
		*stat	=	status;
		if( status & AUTHEN_ERR_SET )
			achip_readErrCode( &status );
	}

	return ret;
}


int	achip_init( void )
{
	AUTHEN_CHIP_INFO	*achip	=	&authenChip;

	memset( achip, 0, sizeof(AUTHEN_CHIP_INFO) );

	achip->buf	=	achipBuffer;
	achip->maxbufSize	=	MAX_ACHIP_BUF_SIZE;

	return TRUE;
}


int achip_open(void)
{
	int ret;
	AUTHEN_CHIP_INFO	*achip	=	&authenChip;

	//FIXME:	reset achip
	DBG_Puts("achip_open 0\n\r");

#ifdef	USE_HW_I2C
	hwi2c_init();
	hwi2c_open( 40 );		//40khz
#endif

#ifdef USE_SOFT_I2C
	I2C0_achip_init();
#endif

#ifdef USE_HW_I2C_POLL
	hwI2cPoll_init();
	hwi2cPoll_openMaster(I2C_CLOCK_HZ );
#endif

#if (defined SHARP_8P && defined RELEASE_VERSION)

#ifndef ACHIP_V20C
	//reset achip
	gpio2_SetFunction(IPHONE_ACHIP_RESET_PIN, TRUE);
	gpio2_SetOutputEnable(IPHONE_ACHIP_RESET_PIN);
	gpio2_ClrOutput(IPHONE_ACHIP_RESET_PIN);
	timer_delayms(20);
	gpio2_SetOutput(IPHONE_ACHIP_RESET_PIN);
	timer_delayms(50);
#endif

#else

#ifndef ACHIP_V20C
	//reset achip
	gpio0_SetFunction(IPHONE_ACHIP_RESET_PIN, TRUE);
	gpio0_SetOutputEnable(IPHONE_ACHIP_RESET_PIN);
	gpio0_ClrOutput(IPHONE_ACHIP_RESET_PIN);
	timer_delayms(20);
	gpio0_SetOutput(IPHONE_ACHIP_RESET_PIN);
	timer_delayms(50);
#endif

#endif

	DBG_Puts("achip_open 1\n\r");

	//read block 0
	ret = achip_i2c_read( BLOCK0_REG_START_ADDR, achip->buf, BLOCK0_REG_SIZE );
	DBG_assert( ret );
	if( !ret )	return FALSE;

	DBG_Puts("achip_open 2\n\r");

	achip->devVer		=	achip->buf[0];
	achip->firmwareVer	=	achip->buf[1];
	achip->authenProtMajVer	=	achip->buf[2];
	achip->authenProtMinVer	=	achip->buf[3];
	achip->devId	=	*(U32*)&achip->buf[4];
	achip->devId	=	endianConvertU32( achip->devId );

#ifndef ACHIP_V20C
	// devVersion:  2.0B - 0x03.
	if( (0x03 != achip->devVer) || (0x200 != achip->devId) )
	{
		DBG_Printf("achip Ver:%d, ID:%d\n\r", achip->devVer, achip->devId);
		return FALSE;
	}
#else
	// devVersion:  2.0C - 0x05
//	if( (0x200 != achip->devId) )
	if( (0x05 != achip->devVer) || (0x200 != achip->devId) )
	{
		DBG_Printf("achip Ver:%d, ID:%d\n\r", achip->devVer, achip->devId);
		return FALSE;
	}
#endif

	if( achip->authenProtMajVer < 0x02 )
	{
		DBG_Printf("achipProMajVer::%d\n\r", achip->authenProtMajVer);
		return FALSE;
	}

//	ret = achip_readAccCertificate( achip->buf, &achip->certlen );
//	DBG_assert( ret );

	DBG_Puts("achip_open ok\n\r");

	return TRUE;
}

int achip_close(void)
{
#ifdef	USE_HW_I2C
	hwi2c_close();
#endif

	return TRUE;
}


U32 ipod_getDevid( void )
{
	AUTHEN_CHIP_INFO	*achip	=	&authenChip;

	return achip->devId;
}

//***************************************************************************************
//	ipod task

int ipod_init(void)
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	memset( iinfo, 0, sizeof(IPOD_DEV_INFO ) );
	iinfo->initOK	=	FALSE;

	achip_init();

#ifdef APPS_SUPPORT
#ifdef APPS_HARBOUR_LIGHT
	appsHl_init();
#endif
#endif

	DBG_Puts("ipod init\n\r");

	return TRUE;
}

int	ipod_open( int type )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;


	memset( iinfo, 0, sizeof(IPOD_DEV_INFO ) );

#ifdef APPS_SUPPORT
#ifdef APPS_HARBOUR_LIGHT
	appsHl_open();
#endif
#endif

	iPod_track_sample_rate = TSAMPLE_RATE_44100;

	return TRUE;
}

int ipod_close(void)
{
	ipod_init();

#ifdef APPS_SUPPORT
#ifdef	APPS_HARBOUR_LIGHT
	appsHl_close();
#endif
#endif

	return TRUE;
}


#ifdef APPS_SUPPORT

#ifdef APPS_AUTO_LAUNCH
int ipod_isAutoLaunchSupport( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	return iinfo->appsAutoLaunch;
}
#endif

int ipod_isApplsComm( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	return iinfo->appsComm;
}
#endif

int ipod_isInitOk( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	return iinfo->initOK;
}

int ipod_isTrackInfoGot( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	return iinfo->trkInfoGot;
}


#define	MODEL_ID_IPHONE			0x0011
#define	MODEL_ID_IPHONE_3G		0x0018
#define	MODEL_ID_IPHONE_3GS		0x001B
#define	MODEL_ID_IPHONE_4		0x0020
#define	MODEL_ID_IPAD			0x001F

int ipod_getModelTypes( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	U16	mid;

	if( !iinfo->modelOk )	return enIPOD_OTHERS;

	mid	=	iinfo->modelid >> 16;

	if( mid == MODEL_ID_IPAD )	return enIPOD_IPAD;

	if( (mid == MODEL_ID_IPHONE) || (mid == MODEL_ID_IPHONE_3G) || (mid == MODEL_ID_IPHONE_3GS )
		|| (mid == MODEL_ID_IPHONE_4) )
		return enIPOD_IPHONE;

	return enIPOD_OTHERS;
}

int ipod_lingoRxCtl( int ena )
{
	IRP	irp;

	FILL_IPHONE_IRP( &irp, enIRPT_IPOD, enIRP_TYPE_IPOD_LINGO_RX_REQ,  NULL, ena, NULL );
	usbmsg_sendIRP( &irp );

	return TRUE;
}

int ipod_audioRxCtl( int ena )
{
	IRP	irp;

	FILL_IPHONE_IRP( &irp, enIRPT_IPOD, enIRP_TYPE_IPOD_AUDIO_RX_REQ,  NULL, ena, NULL );
	usbmsg_sendIRP( &irp );

	return TRUE;
}

int ipod_audioSetSampleRate( int srate )
{
	IRP	irp;

	FILL_IPHONE_IRP( &irp, enIRPT_IPOD, enIRP_TYPE_IPOD_SET_SAMPLE_RATE,  NULL, srate, NULL );
	usbmsg_sendIRP( &irp );

	return TRUE;
}

int ipod_audioSetIntf( int zerobandwidth )
{
	IRP	irp;

	FILL_IPHONE_IRP( &irp, enIRPT_IPOD, enIRP_TYPE_IPOD_SET_INTF,  NULL, zerobandwidth, NULL );
	usbmsg_sendIRP( &irp );

	return TRUE;
}

static int getIpodInfor( void )
{
	return TRUE;
}


static int getDaudioLingoVer( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int ret	=	FALSE;

	iinfo->vlingoDaudio.support	=	FALSE;
	iinfo->vlingoDaudio.lingo	=	LINGO_ID_DIGI_AUDIO;

	ret = lingo_gen_reqLingoVer( iinfo->vlingoDaudio.lingo );
	DBG_assert( ret );

	ret	=	lingo_gen_retLingoVer( &iinfo->vlingoDaudio );
	DBG_assert( ret );
	if( !ret )	return FALSE;

	return TRUE;
}

static int isDaudioNeedExtendedUImode( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;

	if( (iinfo->vlingoDaudio.major == 1) && (iinfo->vlingoDaudio.minor <= 1) ) 
		return TRUE;
	else
		return FALSE;
}


#if 0

static int isIpodDaudioNeedExtUI( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int i;

	if( usbd_isIpodExtModeProhibit() )	return FALSE;

	if( !iinfo->vlingOk )	return TRUE;

	for( i = 0; i < MAX_LINGO_VER; i ++ ) {
		if( iinfo->vlingo[i].support ) {
			if( iinfo->vlingo[i].lingo == LINGO_ID_DIGI_AUDIO ) {
				if( (iinfo->vlingo[i].major == 1) && (iinfo->vlingo[i].minor <= 1) )
					return TRUE;
				else 
					return FALSE;
			}
		}
	}

	return TRUE;
}

const U8 LINOGS_ARRAY[]	=	{
	LINGO_ID_GENERAL,	
//	LINGO_ID_MICROPHONE,		//ipad/iphone 4gs don't support it ?
	LINGO_ID_SIMPLE_REMOTE,
	LINGO_ID_DISPLAY_REMOTE,
//	LINGO_ID_EXTENDED_INTF,
	LINGO_ID_DIGI_AUDIO
};

static int ipodLingoVerGet( void)
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int i;
	int ret;

	iinfo->vlingOk	=	FALSE;
	memset( iinfo->vlingo, 0, sizeof(iinfo->vlingo) );
	for( i = 0; i < sizeof(LINOGS_ARRAY); i ++ ) {
		iinfo->vlingo[i].lingo	=	LINOGS_ARRAY[i];
		ret = lingo_gen_reqLingoVer( iinfo->vlingo[i].lingo );
		DBG_assert( ret );

		ret	=	lingo_gen_retLingoVer( &iinfo->vlingo[i] );
		asm("nop");
		DBG_assert( ret );
		if( !ret )	return FALSE;
	}
	iinfo->vlingOk	=	TRUE;

	return TRUE;
}
#endif

static int ipodInfoGet( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int ret	=	FALSE;
	int len;
	int i;
	U8	status;


#if	0
	//as ATS v2.1 ((warning)
	iinfo->modelOk	=	FALSE;
	ret = lingo_gen_reqModel();
	DBG_assert( ret );
	ret = lingo_gen_retModel( &iinfo->modelid, iinfo->modelNum, &len, MAX_IPOD_MODEL_NUM );
	DBG_assert( ret );
	if( !ret )	return FALSE;
	iinfo->modelOk	=	TRUE;
#endif

	//for transid
	do {
		ret = lingo_gen_reqIpodName();
		DBG_assert( ret );
		ret = lingo_gen_retIpodName( iinfo->name, &len, MAX_IPOD_NAME, &status );
		DBG_assert( ret );
		if( !ret )	break;
	}	while( ret && (len == 0) );
	if( !ret )	return FALSE;

#if	0
	ret = lingo_gen_reqSoftVer();
	DBG_assert( ret );
	ret	=	lingo_gen_retSoftVer( &iinfo->vSoft );
	DBG_assert( ret );
	if( !ret )	return FALSE;
#endif


/*
	//check for general lingo >= 1.09
	iinfo->maxPayloadSize	=	0;
	iinfo->maxPlayLoadSupport	=	0;
	if( iinfo->vlingo[0].support ) {

		if( iinfo->vlingo[0].minor >= 9 ) {
			iinfo->maxPlayLoadSupport	=	TRUE;

			ret = lingo_gen_reqMaxPayloadSize();
			if( ret ) 
				ret = lingo_gen_retMaxPayloadSize( &iinfo->maxPayloadSize );

			DBG_Printf("maxPayloadSize: %d\n\r", iinfo->maxPayloadSize );
		}
	}
*/

	ret = lingo_gen_reqSerialNum();
	DBG_assert( ret );
	ret = lingo_gen_retSerialNum( iinfo->serialnum, &len, MAX_IPOD_SERIAL_NUM );
	DBG_assert( ret );
	if( !ret )	return FALSE;


	return ret;
}

static int ipodNonIdpsInit( void )
{
	int ret;
	U8	tmp;
	U32	id;

#define	CANCEL_AUTHEN_LOOPS		3

	timer_delayms( 50 );

	//FIXME:	3G ipod
	//FIXME:	lingo conflict check
	ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_INIT, 0, 0 );
	DBG_assert( ret );
	if( !ret )	return FALSE;
	ret = lingo_gen_ack( &tmp );
	DBG_assert( ret );
	if( tmp != ACK_CODE_SUCCESS )	return FALSE;
	if( !ret )	return FALSE;

	id	=	ipod_getDevid();
//	ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN, IDENTIFY_DEV_LINGO_OPTION, id );

	//TBD
	getDaudioLingoVer();

//	ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_BASIC, IDENTIFY_DEV_LINGO_OPTION, id );


#if	1
	if( isDaudioNeedExtendedUImode() ) {
#ifdef	IPOD_LINE_IN
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_EXT_REC, IDENTIFY_DEV_LINGO_OPTION, id );
#else
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_EXT, IDENTIFY_DEV_LINGO_OPTION, id );
#endif
	}
	else {
#ifdef	IPOD_LINE_IN
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_RECORD, IDENTIFY_DEV_LINGO_OPTION, id );
#else
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_BASIC, IDENTIFY_DEV_LINGO_OPTION, id );
#endif
	}

#else
	if( usbd_isIpodExtModeProhibit() )	{
#ifdef	IPOD_LINE_IN
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_RECORD, IDENTIFY_DEV_LINGO_OPTION, id );
#else
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_BASIC, IDENTIFY_DEV_LINGO_OPTION, id );
#endif
		
	}
	else {
		//for dev need ext mode
#ifdef	IPOD_LINE_IN
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_EXT_REC, IDENTIFY_DEV_LINGO_OPTION, id );
#else
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_EXT, IDENTIFY_DEV_LINGO_OPTION, id );
#endif
	}


#endif


	//ext no record
//	ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_EXT_NO_RECORD, IDENTIFY_DEV_LINGO_OPTION, id );	
	
//	ret = lingo_gen_identifyDevLingo( 0x05, 0x02, id );
//	ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN, 0x00, id );
	DBG_assert( ret );
	if( !ret )	return FALSE;
//	ipodTest = 1;
	ret = lingo_gen_ack( &tmp );
	DBG_assert( ret );
	if( !ret )	return FALSE;
	if( tmp != ACK_CODE_SUCCESS )	return FALSE;
/*
	if( tmp != ACK_CODE_SUCCESS ) {	//cancel init

		for( tmp = 0; tmp < 3; tmp ++ ) {
		ret = lingo_gen_identifyDevLingo( IDENTIFY_DEV_LINGO_SPOKEN_INIT, 0, 0 );
		DBG_assert( ret );
		ret = lingo_gen_ack( &tmp );
		DBG_assert( ret );
		DBG_assert( tmp == ACK_CODE_SUCCESS );
		}
		return FALSE;
	} */

//	DBG_Printf( "3G ipod found\n\r" );

//	ret	=	ipodInfoGet();
//	DBG_assert( ret );

	return TRUE;
}


static int getIpodOptionForLingo( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int r;

	r = lingo_gen_func_getIpodOptionForLingo( LINGO_ID_GENERAL, iinfo->lgen_option );
	if( r ) 
		iinfo->appsAutoLaunch	=	lingo_gen_func_isAutoLaunchSupport( iinfo->lgen_option );
	else
		return r;	//ipod don't support getIpodOptionForLingo()

	r = lingo_gen_func_getIpodOptionForLingo( LINGO_ID_SIMPLE_REMOTE, iinfo->lsrmt_option );
	DBG_assert( r );

	r = lingo_gen_func_getIpodOptionForLingo( LINGO_ID_DISPLAY_REMOTE, iinfo->ldsp_option );
	DBG_assert( r );

	r = lingo_gen_func_getIpodOptionForLingo( LINGO_ID_DIGI_AUDIO, iinfo->lda_option );
	DBG_assert( r );

	return TRUE;
}

#define	IPOD_INIT_IDLE_TIME		(500 / OS_MSEC_PER_TICK + 1)	//0.5s

static int ipodIdpsInit( void )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	int ret;
	U8	tmp;

	int loop	=	0;
	int init	=	TRUE;


	//FIXME:	first get ipod name / version ... 	

	iinfo->idps	=	TRUE;

#ifdef APPS_SUPPORT

#ifdef appsAutoLaunch
	iinfo->appsAutoLaunch	=	FALSE;
#endif

#endif

#if	1

#ifdef	IAP_TIME_OUT

	do {
		ret = lingo_gen_startIdps();
		if( !ret ) goto WAIT_IPOD;

		if( !iinfo->rxInit ) {
			iinfo->rxInit	=	TRUE;
			ipod_lingoRxCtl( TRUE );
		}

		ret = lingo_gen_ack_ext( IDPS_TOUT_MS, &tmp );
		if( !ret ) goto WAIT_IPOD;
			
		break;

WAIT_IPOD:
		
		if( (tmp != ACK_CODE_BAD_PARAM) && (tmp != ACK_CODE_SUCCESS) ) {
			ukMsgSend(cmdIphone_restartEnum);
			return FALSE;
		}

		if( isIphoneDisconnected() ) return FALSE;
		if ( !usbd_isDevActive(NULL) )	return FALSE; 
		tx_thread_sleep( IPOD_INIT_IDLE_TIME );		
		if( isIphoneDisconnected() ) return FALSE;
		if ( !usbd_isDevActive(NULL) )	return FALSE; 

		DBG_Printf("idps loops:%d, stat:%d\n\r", loop, tmp );

	}	while( loop ++ < MAX_INIT_LOOPS );

	if( loop >= MAX_INIT_LOOPS )	{
		DBG_Printf("idps retry failed\n\r");
		return FALSE;
	}
	if( (tmp != ACK_CODE_BAD_PARAM) && (tmp != ACK_CODE_SUCCESS) ) {
		//FIXME: need disconnnect
		DBG_Printf("bad ipod status\n\r");
		return FALSE;
	}

#else

#define	MAX_INIT_LOOPS	6

	do {
		ret = lingo_gen_startIdps();
		if( !ret ) goto WAIT_IPOD;
		
#if 0
		if( init ) {
			init = FALSE;
			ipod_lingoRxCtl( TRUE );
		}
#else
		if( !iinfo->rxInit ) {
			iinfo->rxInit	=	TRUE;
			ipod_lingoRxCtl( TRUE );
		}
#endif
		
		do {
			ret = lingo_gen_ack( &tmp );
			if( !ret ) goto WAIT_IPOD;
			
			if( tmp == ACK_CODE_PENDING )	timer_delayms(10);
		} while( tmp == ACK_CODE_PENDING );
		
		break;

WAIT_IPOD:
		if (!app_media_status_get())	return FALSE;
		if ( !usbd_isDevActive(NULL) )	return FALSE; 
		tx_thread_sleep( IPOD_INIT_IDLE_TIME );		
		if (!app_media_status_get())	return FALSE;
		if ( !usbd_isDevActive(NULL) )	return FALSE; 
	}	while( loop ++ < MAX_INIT_LOOPS );

	if( loop >= MAX_INIT_LOOPS )	return FALSE;

#endif

#else
	//transaction
	ret = lingo_gen_startIdps();
	if( !ret ) {
		DBG_assert(0);
		return ret;
	}
	ipod_lingoRxCtl( TRUE );
	do {
		ret = lingo_gen_ack( &tmp );
		if( !ret ) {
			DBG_assert(0);
			return ret;
		}

		if( tmp == ACK_CODE_PENDING )	timer_delayms(10);
	} while( tmp == ACK_CODE_PENDING );
#endif

	DBG_Printf( "IDPS 1\n\r" );
	if( tmp == ACK_CODE_BAD_PARAM ) {
		DBG_Printf( "non-IDPS init start\n\r" );
		timer_delayms(50);

		iinfo->idps	=	FALSE;
		ret = ipodNonIdpsInit();
		DBG_assert(ret);
		return ret;
	}
	if( tmp != ACK_CODE_SUCCESS ) {
		//unsupported ipod
		DBG_assert(0);
		return FALSE;
	}

	iinfo->maxPayloadSize	=	0;
	iinfo->maxPlayLoadSupport	=	FALSE;			
	ret = lingo_gen_reqMaxPayloadSize();
	if( ret ) {
		ret = lingo_gen_retMaxPayloadSize( &iinfo->maxPayloadSize );		
		DBG_Printf("maxPayloadSize: %d\n\r", iinfo->maxPayloadSize );
		if( ret)
			iinfo->maxPlayLoadSupport	=	TRUE;			
	}

	if ( !usbd_isDevActive(NULL) )	return FALSE; 

	getIpodOptionForLingo();

	DBG_Printf( "IDPS 2\n\r" );
	ret = lingo_gen_setFidTokenValue();
	if( !ret ) {
		DBG_assert(0);
		return ret;
	}
	ret = lingo_gen_retFidTokenVAlueAck();
	if( !ret ) {
		DBG_assert(0);
		return ret;
	}
	DBG_Printf( "IDPS 3\n\r" );

//	timer_delayms(10);

	ret = lingo_gen_endIdps( ACC_ENDIDPS_STAT_OK );
	if( !ret ) {
		DBG_assert(0);
		return ret;
	}
	DBG_Printf( "IDPS 4\n\r" );
	ret = lingo_gen_idpsStatus_xxx(&tmp);
	if( !ret ) {
		DBG_assert(0);
		return ret;
	} 
	DBG_Printf( "IDPS 5\n\r" );
	DBG_assert( tmp == IDPS_STATUS_OK );
	if( tmp != IDPS_STATUS_OK )	return FALSE;
	DBG_Printf( "IDPS 6\n\r" );

	return TRUE;
}


static void ipodIrpComplete ( U8 irpResult, U32 iprTransLen)
{
	usbmsg_IRPcompleteFunc();
}

static int msg2Usbd( void )
{
	IRP	irp;

	FILL_IPHONE_IRP( &irp, enIRPT_USBD, enIRP_TYPE_IPOD_BACK2MSC, NULL,  0, ipodIrpComplete );
	usbmsg_sendIRP( &irp );
	usbmsg_waitIRPcomplete();

	DBG_assert( irp.result );

	return irp.result;
}


void app_cmd_send_iphone2msc_msg( void );

extern int osentry_changeKeyFuncTaskPriority( void );
extern int osentry_restoreKeyFuncTaskPriority( void );

extern int osentry_HddTaskPriority( void );
extern int osentry_restoreHdd_TaskPriority( void );


#define	IPOD_CONNECT_IDLE_TIME		(2500 / OS_MSEC_PER_TICK + 1)	//2.5s



const USBDEV_ID ipodSyncDelayIDs[]	=	{
	{0x05AC,0x1262	},		//ipod 3G nano (8GB)
	{0x05AC,0x1263	},		//ipod 4G nano (8GB)
	{0x05AC,0x1261	}		//classic(160GB)
};
// nano 3(8GB):vid 0x5ac, pid 0x1262
// nano 4(8GB):vid 0x5ac, pid 0x1263
// nano 4(8GB):vid 0x5ac, pid 0x1263
//classic(160GB):vid 0x5ac, pid 0x1261
//classic(160GB):vid 0x5ac, pid 0x1261

int ipod_isSync2dockDelayNeeded( void )
{
	//ugly but simple
	USB_DEVICE *dev = &singleUsbDevice;

	int i;
	int size;

	size = sizeof(ipodSyncDelayIDs)/sizeof(USBDEV_ID);
	for (i = 0; i < size; i ++)
	{
		if( (dev->devDescriptor.idVendor == ipodSyncDelayIDs[i].vid) &&(dev->devDescriptor.idProduct == ipodSyncDelayIDs[i].pid) )
		{
			return TRUE;
		}
	}

	return FALSE;
}

#if	1

#define	IPOD_INIT_MAX_LOOP	8
static int ipodConnect( IPOD_MSG *imsg )
{
	int ret	=	FALSE;
	int i;
	TX_INTERRUPT_SAVE_AREA;

#ifdef OS_TASK_PRIORITY_CHANGE
	osentry_changeKeyFuncTaskPriority();
#endif

	//app_timer_ipod_authen_timeout_timer_set(2000);

	//isrc_init();
	//isrc_open();

	if ( (app_main_data.media == MEDIA_IPHONE) || (app_main_data.media == MEDIA_USB_DEVICE) )
	{
#ifdef	AUDIO_SAMPLE_RATE_SWITCH_TO_48K
		src_init( 44100, 2, TSAMPLE_RATE_48000 );
#else
		src_init( 44100, 2, TSAMPLE_RATE_44100 );
#endif
	}

	if(imsg->param1 == enIPOD_DIGITAL_AUDIO ) {

		DBG_Puts( "ipod digital audio\n\r" );
/*
		if(ipod_isSync2dockDelayNeeded())
		{
		//	if ( (app_main_data.pre_media == MEDIA_PC_IPHONE) && (app_main_data.pc_usb_on == true) )
			if ( (app_main_data.pre_media == MEDIA_PC_IPHONE) && (app_main_data.iphone_plugin == true) )
			{
				app_main_data.key_lock = true ;
				app_nav_sleep(9000);
				DBG_Printf( "delay finish \n\r" );
				app_main_data.key_lock = false;
			}				
		}


		app_main_data.pre_media = app_main_data.media;
*/
#ifdef	IPOD_ACC_POWER_WAIT
		waitIpodAccPower();
#endif

		lingo_init();
		iap_init();

		lingo_open();
		iap_open();

		ipod_open( enIPOD_DIGITAL_AUDIO );
		DBG_Printf( "ipod 1\n\r" );

//TX_DISABLE;
		ret = achip_open();
//TX_RESTORE;
		if( !ret )	{
			DBG_Printf( "achip open failed\n\r" );
			return FALSE;
		}

		DBG_Printf( "ipod 2\n\r" );

#ifdef	IPOD_ACC_POWER_WAIT
		waitIpodAccPower();
#endif

		timer_delayms(100);
		//sleep before init begin, iphone 3GS need this
//		tx_thread_sleep( IPOD_CONNECT_IDLE_TIME );
		DBG_Printf( "ipod 3\n\r" );

		i = 0;
		while( i ++ < IPOD_INIT_MAX_LOOP ) {
			ret = ipodIdpsInit();
			if( ret ) {
				DBG_Printf( "ipodIdpsInit ok:%d\n\r", i );
				break;
			}

			if ( !usbd_isDevActive(NULL) )	break; 

			DBG_Printf( "ipodIdpsInit fail:%d\n\r", i );
			timer_delayms( 100 );
		}

		if( !ret )	{
			//unsupported ipod
			DBG_Printf( "unknown ipod 1\n\r" );
			//for display No support
#if 0
			ukMsgSend(app_cmd_iPhone_no_support);
#else
//			ukMsgSend(app_cmd_iPhone_digital_no_support);

			//modified for iphone power on on usb port
			ukMsgSend(app_cmd_iPhone_power_on);
#endif

			//FIXME: back to msc ?
			//msg2Usbd();
			//app_cmd_send_iphone2msc_msg();

			return FALSE;
		}
#ifdef IPOD_UART_SUPPORT
		else
		{
			if (ipodDigital_converted_to_ipodUart())
			{
				DBG_Puts( "ipod digital to uart\n\r" );
				ukMsgSend(app_cmd_iPhone_no_support);

				return FALSE;
			}
		}
#endif

		DBG_Printf( "ipod init succeeded\n\r" );
	}
	else {
		DBG_Printf( "ipod don't support digital audio\n\r" );
		ukMsgSend(app_cmd_iPhone_no_support);

		return FALSE;
	}

/*
	else if( imsg->param1 == enIPOD_ANALOG_HID  ) {
		DBG_Printf( "ipod analog connected\n\r" );
		DBG_assert(0);
	}
	else {
		DBG_assert(0);
		return FALSE;
	}
*/
	//FIXME:	send msg out to notify ipod dev connection

	//switch to iphone mode
	//	app_cmd_send_switch2iphone_msg();

	return TRUE;
}

#else
static int ipodConnect( IPOD_MSG *imsg )
{
	int ret	=	FALSE;
	TX_INTERRUPT_SAVE_AREA;

#ifdef OS_TASK_PRIORITY_CHANGE
	osentry_changeKeyFuncTaskPriority();
#endif

	//app_timer_ipod_authen_timeout_timer_set(2000);

	//isrc_init();
	//isrc_open();

	if ( (app_main_data.media == MEDIA_IPHONE) || (app_main_data.media == MEDIA_USB_DEVICE) )
	{
#ifdef	AUDIO_SAMPLE_RATE_SWITCH_TO_48K
		src_init( 44100, 2, TSAMPLE_RATE_48000 );
#else
		src_init( 44100, 2, TSAMPLE_RATE_44100 );
#endif
	}

	if(imsg->param1 == enIPOD_DIGITAL_AUDIO ) {

		DBG_Printf( "ipod digital audio connected\n\r" );

#ifdef	IPOD_ACC_POWER_WAIT
		waitIpodAccPower();
#endif

		lingo_init();
		iap_init();

		lingo_open();
		iap_open();

		ipod_open( enIPOD_DIGITAL_AUDIO );
		DBG_Printf( "ipod 1\n\r" );

//TX_DISABLE;
		ret = achip_open();
//TX_RESTORE;
		if( !ret )	{
			DBG_Printf( "achip open failed\n\r" );
			return FALSE;
		}

		DBG_Printf( "ipod 2\n\r" );

#ifdef	IPOD_ACC_POWER_WAIT
		waitIpodAccPower();
#endif
		timer_delayms(100);
		//sleep before init begin, iphone 3GS need this
	//	tx_thread_sleep( IPOD_CONNECT_IDLE_TIME );
		DBG_Printf( "ipod 3\n\r" );

		ret = ipodIdpsInit();

		if( !ret )	{
			//unsupported ipod
			DBG_Printf( "unknown ipod 2\n\r" );
			//For display No support
			//FIXME: back to msc ?
			//msg2Usbd();
			//app_cmd_send_iphone2msc_msg();
			return FALSE;
		}
		DBG_Printf( "ipod init succeeded\n\r" );
	}
	else {
		DBG_Printf( "ipod don't support digital audio\n\r" );
		return FALSE;
	}

/*
	else if( imsg->param1 == enIPOD_ANALOG_HID  ) {
		DBG_Printf( "ipod analog connected\n\r" );
		DBG_assert(0);
	}
	else {
		DBG_assert(0);
		return FALSE;
	}
*/
	//FIXME:	send msg out to notify ipod dev connection

	//switch to iphone mode
	//	app_cmd_send_switch2iphone_msg();
	
	return TRUE;
}
#endif

static int ipodDisconnect( IPOD_MSG *imsg )
{
	DBG_Printf( "ipod disconnected 3\n\r" );

	ipod_close();

#ifdef OS_TASK_PRIORITY_CHANGE
	osentry_restoreKeyFuncTaskPriority();
#endif

	//app_timer_ipod_authen_timeout_timer_clr();

	//FIXME: temporarily add this
	app_cmd_send_iphone2msc_msg();

	return TRUE;
}



int iap_checkIpodLingo( IPOD_PKT *ipkt );

int ipod_lingoProcess( IPOD_PKT *ipkt )
{
	IPOD_DEV_INFO	*iinfo	=	&ipodInfo;
	volatile int ret;
	AUTHEN_CHIP_INFO	*achip	=	&authenChip;
	U8	status, tmp;
	U8	*ptr;
	TX_INTERRUPT_SAVE_AREA;

	IPOD_TRACK_INFO	tinfo;
	int len;

	ret = iap_checkIpodLingo( ipkt );
	if( !ret )	{
		asm("nop");
		goto EXIT;
	}

	switch( ipkt->lingoID ) {
	case LINGO_ID_GENERAL:
		switch( ipkt->cmdID ) {

#ifdef	APPS_SUPPORT
		case LINGO_GEN_OPEN_DATA_SESSION:
			iinfo->appsComm	=	TRUE;

			DBG_Printf( "open DataSession\n\r" );
			ret  = lingo_gen_openDataSession( ipkt );
			timer_delayms(50);
			lingo_gen_devAck( ret,  LINGO_GEN_OPEN_DATA_SESSION );

#if (defined APPS_UPGRADE_ENABLE && defined APPS_HARBOUR_LIGHT)
			appsHl_process( LINGO_ID_GENERAL, LINGO_GEN_OPEN_DATA_SESSION, NULL, 0 );
#endif
			break;

		case LINGO_GEN_CLOSE_DATA_SESSION:
			DBG_Printf( "close DataSession\n\r" );
			ret = lingo_gen_closeDataSession( ipkt );
			lingo_gen_devAck( ret, LINGO_GEN_CLOSE_DATA_SESSION );

#if (defined APPS_UPGRADE_ENABLE && defined APPS_HARBOUR_LIGHT)
			appsHl_process( LINGO_ID_GENERAL, LINGO_GEN_CLOSE_DATA_SESSION, NULL, 0 );
#endif

#if 0//def	IPOD_VOLUME_SYNC
			app_cmd_iphone_raw_vol_set( app_main_data.volume );
#endif
			iinfo->appsComm	=	FALSE;
			break;

		case LINGO_GEN_IPOD_DATA_TRANSFER:

//			DBG_Printf( "ipod data\n\r" );

			len	=	0;
			ret = lingo_gen_ipodDataTransfer( ipkt, achip->buf, &len );
			lingo_gen_devAck( ret, LINGO_GEN_IPOD_DATA_TRANSFER );

#ifdef	APPS_HARBOUR_LIGHT
			appsHl_process( LINGO_ID_GENERAL, LINGO_GEN_IPOD_DATA_TRANSFER, achip->buf, len );
#endif
			break;
#endif

		case LINGO_GEN_GET_DEV_AUTHEN_INFO:
//TX_DISABLE;
			lingo_gen_getDevAuthenInfo( ipkt );

			ret = achip_readAccCertificate( achip->buf, &achip->certlen );
			DBG_assert( ret );

//TX_RESTORE;
			DBG_Printf("certlen: %d\n\r", achip->certlen );
			ret = lingo_gen_retDevAuthenInfo( achip->buf, achip->certlen );
			DBG_assert( ret );
			break;

		case LINGO_GEN_GET_DEV_AUTHEN_SIGNATURE:
			ret = lingo_gen_getDevAuthenSignature( ipkt, &achip->challenge, &achip->chalen );
			DBG_assert( ret );
			if( !ret )	break;

			DBG_Printf("challen: %d\n\r", achip->chalen );
#ifndef ACHIP_V20C
TX_DISABLE;
#endif
			//must disable it for sometimes ipod can not connect
			ret =  achip_writeChallenge( achip->challenge, achip->chalen );
			DBG_assert( ret );
			ret	=	achip_writeAuthenCtlStat( AUTHEN_PROC_SIG_GEN );
#ifndef ACHIP_V20C
TX_RESTORE;
#endif
//			if( !ret )	
//				asm("nop");
			DBG_assert( ret );
			do
			{
				timer_delayms(10);
#ifndef ACHIP_V20C
TX_DISABLE;
#endif
				//must disable it for sometimes ipod can not connect
				ret =	achip_readAuthenCtlStat( &status );
#ifndef ACHIP_V20C
TX_RESTORE;
#endif
				DBG_assert( ret );
				DBG_assert( (status & AUTHEN_STAT_MASK) == AUTHEN_ACC_SIGNATURE_OK );
			} while( (status & AUTHEN_STAT_MASK) != AUTHEN_ACC_SIGNATURE_OK );

#ifndef ACHIP_V20C
TX_DISABLE;
#endif
			ret	=	achip_readSignature( achip->buf, &achip->siglen );
#ifndef ACHIP_V20C
TX_RESTORE;
#endif
			DBG_assert( ret );

			DBG_Printf("siglen: %d\n\r", achip->siglen );
			ret =	lingo_gen_retDevAuthenSignature( achip->buf, achip->siglen );
			DBG_assert( ret );
			asm("nop");
			if( !ret )	{
				DBG_Printf( "authen fail 1\n\r" );
				break;
			}

			ret	=	lingo_gen_ackDevAuthenStat( &status );
			DBG_assert( ret );
			DBG_assert( 0 == status );
			if( !ret || (0 != status ) ) {
				DBG_Printf( "authen fail 2\n\r" );
				asm( "nop" );
			}

			if( ret && !iinfo->initOK ) {

//				achip_close();

				DBG_Printf( "ipod authen done\n\r");

				if( !iinfo->idps )
					lingo_gen_setIpodPrefAll();

#ifdef IPOD_VOLUME_SET
				app_cmd_iphone_raw_vol_set( app_main_data.volume );
				timer_delayms(20);
#else
				timer_delayms(10);
#endif

				ipodInfoGet();
				asm( "nop" );

				lingo_disp_setRmtEventNotifyFunc( DEFAULT_RMT_EVENT_MASK );

#ifdef	IPOD_VOLUME_SYNC
//				lingo_disp_setRmtEventNotifyFunc( RMT_EVENT_MASK_VOL );
#endif

				//audio_setIntf( FALSE );
				ret = ipod_audioSetIntf( FALSE );
				if( !ret )	break;

//				if( isIpodDaudioNeedExtUI() ) {
//				if( !iinfo->idps && isIpodDaudioNeedExtUI() ) {
				if( !iinfo->idps && isDaudioNeedExtendedUImode() ) {
					DBG_Printf( "enter extended mode\n\r");
					ret = lingo_genFunc_enterRmtUI();
					if( !ret )	break;
				}

				DBG_Printf( "ipod begin playing\n\r");

				//send msg out
				app_cmd_send_switch2iphone_msg();
				iinfo->initOK	=	TRUE;
			}
			break;

		case LINGO_GEN_GET_ACC_INFOR:
			ret	=	lingo_gen_getAccInfo( ipkt, &status, &ptr );
			DBG_assert( ret );
			DBG_Printf( "accInfo: %d\n\r", status );
			ret	=	lingo_gen_retAccInfo( status, ptr );
			DBG_assert( ret );
//			if( status == 0 ) {
//				asm("nop");
//			}
//			else if( status == 1 ) {
//				timer_delayms(20);
//				asm("nop");
//			}
			break;

		default:
			DBG_assert(0);
		}
		break;

	case LINGO_ID_DIGI_AUDIO:
		switch( ipkt->cmdID ) {
		case LINGO_DAUDIO_CMD_IPOD_ACK:
			//FIXME
			DBG_Printf("daudio ipod ack\n\r");
			asm( "nop" );
			DBG_assert(0);
			break;
		case LINGO_DAUDIO_CMD_GET_ACC_SRATE_CAP:
			if( !iinfo->initOK )	break;

			ret  = lingo_daudio_getAccSrateCaps( ipkt );
			DBG_assert( ret );

			ret = lingo_daudio_retAccSrateCaps();
			DBG_assert( ret );

			DBG_Printf( "ipod: get ACC \n\r");
			timer_delayms(100);
			break;
		case LINGO_DAUDIO_CMD_NEW_IPOD_TRACK_INFO:
			DBG_Printf( "ipod newTrack\n\r");

			ret = lingo_daudio_newIpodTrackInfo( ipkt, &tinfo );
			DBG_assert( ret );

			ret = lingo_daudio_accAck( LINGO_DAUDIO_ACK_STATUS_OK, ipkt->cmdID );
			DBG_assert( ret );
			if( !ret )	break;

			asm( "nop" );
			DBG_Printf("track sample rate: %d\n\r", tinfo.sampleRate );

#ifndef ACHIP_V20C
TX_DISABLE;
#endif
			iPod_track_sample_rate = tinfo.sampleRate;
			if ( (app_main_data.media == MEDIA_IPHONE) || (app_main_data.media == MEDIA_USB_DEVICE) )
			{
#ifdef	AUDIO_SAMPLE_RATE_SWITCH_TO_48K
				src_init( tinfo.sampleRate, 2, TSAMPLE_RATE_48000 );
#else
				src_init( tinfo.sampleRate, 2, TSAMPLE_RATE_44100 );
#endif
			}
#ifndef ACHIP_V20C
TX_RESTORE;
#endif
			ret = ipod_audioSetSampleRate( tinfo.sampleRate );
			DBG_assert( ret );
			if( !ret )	break;

			iinfo->trkInfoGot	=	TRUE;


/*
			//temprarily patch for ipod nano 8G green
			if( usbd_isIpodNoSrc() ) 
				ret = ipod_audioSetSampleRate( tinfo.sampleRate );
			else
				ret = ipod_audioSetSampleRate( 44100 );
			DBG_assert( ret );
			if( !ret )	break;*/

/*
			if( !iinfo->initOK ) {

				DBG_Printf( "ipod init done\n\r");

				ipodInfoGet();
				asm( "nop" );
				//audio_setIntf( FALSE );
				ret = ipod_audioSetIntf( FALSE );
				if( !ret )	break;

				//send msg out
				app_cmd_send_switch2iphone_msg();
				iinfo->initOK	=	TRUE;
			}
*/
			break;
		default:
			DBG_Printf( "ipkt_cmdID:\n\r", ipkt->cmdID);
			DBG_assert(0);
		}
		break;

	case LINGO_ID_MICROPHONE:

		DBG_Printf( "microphone lingo\n\r" );
		switch( ipkt->cmdID ) {
		case LINGO_MICROPHONE_GETDEV_ACK:
			ret = lingo_mic_getDevAck( ipkt );
			ret = lingo_mic_devAck( ACK_CODE_SUCCESS, ipkt->cmdID );
			break;

		case LINGO_MICROPHONE_IPOD_MODE_CHG:
			ret = lingo_mic_ipodModeChg( ipkt, &status );
			//FIXME:	send to UI for status:	begin/end record ...
			ret = lingo_mic_devAck( ACK_CODE_SUCCESS, ipkt->cmdID );
			break;
		case LINGO_MICROPHONE_GET_DEV_CAPS:
			ret = lingo_mic_getDevCaps( ipkt );
			ret = lingo_mic_retDevCaps( DEFAULT_MIC_DEV_CAPS );
			break;
		case LINGO_MICROPHONE_GET_DEV_CTL:
			ret = lingo_mic_getDevCtl( ipkt, &status );
			ret = lingo_mic_retDevCtl( status );
			break;
		case LINGO_MICROPHONE_SET_DEV_CTL:
			ret = lingo_mic_setDevCtl( ipkt, &status, &tmp );
			ret = lingo_mic_devAck( ACK_CODE_BAD_PARAM, ipkt->cmdID );
		//	ret = lingo_mic_devAck( ACK_CODE_SUCCESS, ipkt->cmdID );
			break;

		default:
			DBG_assert(0);
		}
		break;

#if 1//def	IPOD_VOLUME_SYNC
	case LINGO_ID_DISPLAY_REMOTE:
		switch( ipkt->cmdID ) {

		case LINGO_DSPRMT_RMT_EVT_NOTIFY:
			app_main_data.disp_info = DISP_INFO_WAITING;
			lingo_disp_rmtEventNotify( ipkt );
			break;

		default:
			break;
		}
		break;
#endif

	default:
		DBG_assert(0);
	}

EXIT:
	if( ipkt->callback != NULL )
		(*ipkt->callback) ( ipkt );

	return TRUE;
}


#ifdef	IPOD_TASK_MODIFY

void task_ipod(IPOD_MSG *msg)
{
	IPOD_PKT	*ipkt;

	switch( msg->msg ) {
	case enIPOD_CONNECT:

		if (app_main_data.media == MEDIA_AUX || app_main_data.media == MEDIA_TUNER)
		{
			if (need_to_free_space_status != UNKNOW_REC_STATUS)
			//if (app_main_data.media != MEDIA_USB_DEVICE)
			{
				DBG_Puts("anolog rec readry do not conn ipod\n\r");
				ipodDisconnect( msg );
				break;
			}			
		}
		else
		{
			//if (need_to_free_space_status != UNKNOW_REC_STATUS)
			if (app_main_data.media != MEDIA_USB_DEVICE)
			{
				DBG_Puts("rec readry do not conn ipod\n\r");
				ipodDisconnect( msg );
				// cdda rec and can not conn ipod
				break;
			}
		}
		
		ipod_init();
		ipodConnect( msg );
		break;
		
	case enIPOD_DISCONNECT:
		ipodDisconnect( msg );
		break;
		
	case enIPOD_IPHONE_SEND_PKT:
		ipkt	=	(IPOD_PKT*)msg->param1;
		ipod_lingoProcess( ipkt );
		break;
		
	default:
		break;
	}
	
}

#else

void task_ipod(void)
{
	U32 buf[WIDTH_OF_IPOD_QUEUE];
	IPOD_MSG	*msg;
	int ret;
	IPOD_PKT	*ipkt;

	DBG_assert( sizeof(IPOD_MSG) <= WIDTH_OF_IPOD_QUEUE*sizeof(U32) );
	DBG_assert( sizeof(buf) >= sizeof(IPOD_MSG) );


	while(1) {
		ret = tx_queue_receive( &gIpodQueue, (void *)buf, TX_WAIT_FOREVER );
		DBG_assert( TX_SUCCESS == ret );
		msg	=	(IPOD_MSG*)buf;

		//DBG_Printf( "ipodTask\n\r" );
		//if( ipodTest )
		//	asm("nop");

		switch( msg->msg ) {
		case enIPOD_CONNECT:
			ipod_init();
			ipodConnect( msg );
			break;

		case enIPOD_DISCONNECT:
			ipodDisconnect( msg );
			break;

		case enIPOD_IPHONE_SEND_PKT:
			ipkt	=	(IPOD_PKT*)msg->param1;
			ipod_lingoProcess( ipkt );
			break;

		default:
			break;
		}
	}
}
#endif //IPOD_TASK_MODIFY


extern U8 ex_audio_stream_zero;

U8 ipod_tx_data_state_init(void)
{
	ex_audio_stream_zero = FALSE;
}

U8 ipod_tx_data_state_get(void)
{
	return ex_audio_stream_zero;
}


#ifdef	IPOD_VOLUME_SYNC
extern void app_cmd_iphone_vol_notify( U8 mute, U8 vol );

void app_cmd_volume(void)
{
	DBG_Printf("Set Volume:%d\n\r", app_main_data.volume);

#if 1
	if (app_main_data.standby_status)
	{
		return;
	}

#if defined AUDIO_CODEC_USED_VOL
	AUDIOdevice.Set_Volume(app_main_data.volume);
#elif defined AUDIO_AMP_USED_VOL
	audioAmp_Dev.Set_Volume(app_main_data.volume);
#else
	audioAmp_Dev.Set_Volume(app_main_data.volume);
#endif

	if (app_main_data.window != (WINDOW *) &volume_window)
	{
		app_nav_window_set(&volume_window, app_window_data.window_index+1);
		general_ticks = 0;
	}
	else
	{
		//app_main_data.window->draw_title ();
		app_main_data.window->draw_region ();
	}
#endif

	if( app_main_data.volume == 0 )
	{
		//AMP_MUTE_ON;
	}
}

void ipod_rmtEventVolNotify( U8 mute, U8 vol )
{
	static U8 iphone_sync_cnt = 0;;

	if (app_main_data.standby_status)
	{
		return;
	}

	//if (app_main_data.media != MEDIA_IPHONE)
	if ( !( (app_main_data.media == MEDIA_IPHONE) || 
		((app_main_data.media == MEDIA_USB_DEVICE) && (usbDevType == enUSBH_DEV_IPOD_AUDIO)) ) )
	{
		return;
	}


	if (iphone_sync_cnt <= 2)
	{
		iphone_sync_cnt++;
		return;
	}

//	DBG_Printf("iP mute:%d vol:%d\n\r", mute, vol);
	if( mute ) vol	=	0;

	if( vol == 255 )
	{
		vol = VOLUME_MAX;
	}
	else
	{
		vol	=	vol / (256/VOLUME_MAX);
		//vol	=	vol / (255/VOLUME_MAX);
	}

	//set to onboard DSP chip
//	if (app_main_data.volume != vol )
	{
		app_main_data.volume	=	vol;

#ifdef APPS_SUPPORT
		ukMsgSend(app_nav_cmd_iphone_apps_vol);
#else
		ukMsgSend(app_cmd_volume);
#endif
	}

	DBG_Printf("IPOD Vol: %d\n\r", app_main_data.volume);
}
#endif

#ifdef LCD_DISP_IPHONE_ID3_MESSAGE
extern U32 iPhone_playing_track;
#if 1
extern ID3_TAG id3_tag;
#else
extern U16 iPhone_title_buf[MAX_TITLE_LEN];
extern U16 iPhone_artist_buf[MAX_TITLE_LEN];
extern U16 iPhone_album_buf[MAX_TITLE_LEN];
#endif
#endif

void ipod_rmtEventTrackIndexNotify( U32 tidx, U32 track_info_type)
{
#ifdef LCD_DISP_IPHONE_ID3_MESSAGE
	U8 utf8str[MAX_TITLE_LEN];
//	U16 ucs16str[MAX_TITLE_LEN];
	int len16;

	int slen;
	int ret;

	//DBG_Printf("iPod id3 type:%d\n\r", track_info_type);

	memset( utf8str, 0, sizeof(utf8str) );
//	memset( ucs16str, 0, sizeof(ucs16str) );

	iPhone_playing_track	=	tidx;

	ret = lingo_dispFunc_getIndexTrackInfo(tidx, track_info_type,
		utf8str, MAX_TITLE_LEN, &slen );

	DBG_Printf("id3:%s\n\r", utf8str);

	if ( ret && track_id3_ready )
	{
#if 0
		len16 = utf8_toUcs16( utf8str, slen, ucs16str, MAX_TITLE_LEN );
		//len = strlen((const char *)utf8str);

		//for displaying on LCD
		memset(file_name, 0, MAX_TITLE_LEN*2);
		CFasm_memcpy(file_name, ucs16str, len16*2);
#else
		if (track_info_type == TRACK_INFO_TYPE_TITLE)
		{
			//memset( iPhone_title_buf, 0, sizeof(iPhone_title_buf) );
			//len16 = utf8_toUcs16( utf8str, slen, iPhone_title_buf, MAX_TITLE_LEN );
			//memset(file_name, 0, MAX_TITLE_LEN*2);
			//CFasm_memcpy(file_name, iPhone_title_buf, len16*2);
			memset( id3_tag.title, 0, (MAX_ID3_TEXT + 1)*2);
			len16 = utf8_toUcs16( utf8str, slen, id3_tag.title, MAX_TITLE_LEN );
			memset(file_name, 0, (MAX_TITLE_LEN+1)*2);
			CFasm_memcpy(file_name, id3_tag.title, len16*2);
		}
		else if (track_info_type == TRACK_INFO_TYPE_ARTIST)
		{
			//memset( iPhone_artist_buf, 0, sizeof(iPhone_artist_buf) );
			//len16 = utf8_toUcs16( utf8str, slen, iPhone_artist_buf, MAX_TITLE_LEN );
			//memset(file_name, 0, MAX_TITLE_LEN*2);
			//CFasm_memcpy(file_name, iPhone_artist_buf, len16*2);
			memset( id3_tag.artist, 0, (MAX_ID3_TEXT + 1)*2 );
			len16 = utf8_toUcs16( utf8str, slen, id3_tag.artist, MAX_TITLE_LEN );
			memset(file_name, 0, (MAX_TITLE_LEN+1)*2);
			CFasm_memcpy(file_name, id3_tag.artist, len16*2);
		}
		else if (track_info_type == TRACK_INFO_TYPE_ALBUM)
		{
			//memset( iPhone_album_buf, 0, sizeof(iPhone_album_buf) );
			//len16 = utf8_toUcs16( utf8str, slen, iPhone_album_buf, MAX_TITLE_LEN );
			//memset(file_name, 0, MAX_TITLE_LEN*2);
			//CFasm_memcpy(file_name, iPhone_album_buf, len16*2);
			memset( id3_tag.album, 0, (MAX_ID3_TEXT + 1)*2 );
			len16 = utf8_toUcs16( utf8str, slen, id3_tag.album, MAX_TITLE_LEN );
			memset(file_name, 0, (MAX_TITLE_LEN+1)*2);
			CFasm_memcpy(file_name, id3_tag.album, len16*2);
		}
#endif
	}
	else
	{
		/*if (track_info_type == TRACK_INFO_TYPE_TITLE)
		{
			memset( iPhone_title_buf, 0, sizeof(iPhone_title_buf) );
		}
		else if (track_info_type == TRACK_INFO_TYPE_ARTIST)
		{
			memset( iPhone_artist_buf, 0, sizeof(iPhone_artist_buf) );
		}
		else if (track_info_type == TRACK_INFO_TYPE_ALBUM)
		{
			memset( iPhone_album_buf, 0, sizeof(iPhone_album_buf) );
		}*/
	}
#endif
}

U8 installIpodDeviceDriver_state_get(USB_DEVICE *usbdev)
{
	volatile U8 ret = TRUE;
	//USB_DEVICE	* usbdev	=	NULL;

	//DBG_Printf("vid 0x%x, pid 0x%x\n\r", usbdev->devDescriptor.idVendor,
	//			usbdev->devDescriptor.idProduct);

#ifdef IPOD_DISABLE_USB_PORT
	if( app_cmd_isSourceUsbPort() )
	{
		usbdev->devType		=	enUSB_DEV_UNKOWN;
		usbdev->ipodType	=	enNON_IPOD;

		app_cmd_send_iphoneOnUsbPort_msg();
	}
	else
	{
		ret = installIpodDeviceDriver( usbdev );
	}
#else
	ret = installIpodDeviceDriver( usbdev );
#endif

	return ret;
}

void ipod_acc_power_set(USB_DEVICE * usbDev)
{
#ifdef	IPOD_NO_CHARGE
	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MIN );
//	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND0 );
#else

#ifdef SHARP_KP82
//	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MAX );	//1A

	//2.1A for usb connector; 1A for lightning connector
	//if( IPHONE_ACC_POWER_DETECT )
	if( usbPortSwitch_isLightning() ) {
		setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MAX );	//1A
	}
	else {
		DBG_assert( usbPortSwitch_isUSB() );
		setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MAX_IPAD );	//2.1A
	}
#else
	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MAX );
#endif

#endif
}

//ipod don't support sample rate conversion
const USBDEV_ID usb_ipod_devId[]	=	{
//	{0x05AC,	0x1265	},		//test Uart 
	{0x05AC,	0x120A	},		//ipod nano, 
	{0x05AC,	0x1209	},		//ipod 
};

U8 ipodDigital_converted_to_ipodUart(void)
{
#ifdef IPOD_UART_SUPPORT
	USB_DEVICE *dev = &singleUsbDevice;

	int i;
	int size;

//	return FALSE;

	size = sizeof(usb_ipod_devId)/sizeof(USBDEV_ID);
	for (i = 0; i < size; i ++)
	{
		if( dev->devDescriptor.idVendor == (usb_ipod_devId[i].vid) &&
			(dev->devDescriptor.idProduct == usb_ipod_devId[i].pid) )
		{
			return TRUE;
		}
	}

	return FALSE;
#else
	return FALSE;
#endif
}


const USBDEV_ID ipod_id_noTrackIndex[]	=	{
	{0x05AC,	0x1262	},		//ipod nano 3G,
	{0x05AC,	0x1263	},		//ipod nano 3G,
	{0x05AC,	0x1264	},		//ipod nano 4G,
	{0x05AC,	0x1265	},		//ipod nano 5G,
	{0x05AC,	0x1266	},		//ipod nano 6G,
	{0x05AC,	0x1267	},		//ipod nano 7G,
};

int isIpodEventNotify_NoTrackIndex( void )
{
	USB_DEVICE *dev = &singleUsbDevice;
	int i;
	int size;

	//return FALSE;

	size = sizeof(ipod_id_noTrackIndex)/sizeof(USBDEV_ID);
	for (i = 0; i < size; i ++)
	{
		if( dev->devDescriptor.idVendor == (ipod_id_noTrackIndex[i].vid) &&
			(dev->devDescriptor.idProduct == ipod_id_noTrackIndex[i].pid) )
		{
			return TRUE;
		}
	}

	return FALSE;
}

int ipod_isSourceDocking( void )
{
#ifdef	IPOD_VIDEO_OUT
	return TRUE;
#else
//	return ( app_main_data.media == MEDIA_IPHONE );
	return FALSE;
#endif
}

#else

#include "usbd.h"
#include "message.h"

#include "ipoddev.h"
#include "ipod.h"

U8 installIpodDeviceDriver_state_get(USB_DEVICE *usbdev)
{
#ifdef IPOD_USB_AUDIO_CHARGE_ONLY
	volatile U8 ret = TRUE;
	//USB_DEVICE *usbdev = NULL;

	//DBG_Printf("vid 0x%x, pid 0x%x\n\r", usbdev->devDescriptor.idVendor,
	//			usbdev->devDescriptor.idProduct);

#ifdef IPOD_DISABLE_USB_PORT
	if( app_cmd_isSourceUsbPort() )
	{
		usbdev->devType		=	enUSB_DEV_UNKOWN;
		usbdev->ipodType	=	enNON_IPOD;

		app_cmd_send_iphoneOnUsbPort_msg();
	}
	else
	{
		ret = installIpodDeviceDriver( usbdev );
	}
#else
	ret = installIpodDeviceDriver( usbdev );
#endif

	return ret;

#else
	return FALSE;
#endif
}

void ipod_acc_power_set(USB_DEVICE * usbDev)
{
#ifdef IPOD_USB_AUDIO_CHARGE_ONLY

#ifdef IPOD_NO_CHARGE
	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MIN );
//	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND0 );
#else
	setIpodAccPower( usbDev, EXTRA_CURRENT_SUSPEND0, EXTRA_CURRENT_NON_SUSPEND_MAX );
#endif

#endif //IPOD_USB_AUDIO_CHARGE_ONLY
}

#endif //IPOD_USB_AUDIO