dcl_otg_drv.c 72.3 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 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   dcl_otg_drv.c
 *
 * Project:
 * --------
 *   Maui_sw
 *
 * Description:
 * ------------
 * This file defines the DCL common definitions for USB OTG driver.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/


#if defined(__OTG_ENABLE__) && !defined(DRV_USB_OFF)


#include "drv_comm.h"
#include "reg_base.h"
#include "drvpdn.h"
#include "intrCtrl.h"
#include "drv_hisr.h"
#include "gpio_hw.h"
#include "gpio_sw.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h"       /* Task message communiction */
#include "isrentry.h"
#include "eint.h"
#include "usb_hw.h"
#include "otg_drv.h"
#include "otg_drv_pri.h"
#include "usb_hcd.h"
#include "usb_hcd_pri.h"
#include "usb_drv.h"
#include "usb_drv_pri.h"
//#include "usb_custom.h"
#include "otg_if.h"
#include "usb_debug_tool.h"
#include "usb_trc.h"
#include "dcl.h"
#include "usb_adap.h"
//#include "kal_non_specific_general_types.h"
//#include "kal_release.h"
#include "kal_trace.h"
#include "stack_ltlcom.h"
#include "stack_config.h"
#include "kal_general_types.h"
#include "kal_public_api.h"


//#if (defined(MT6326))
//#include "pmic6326_sw.h"
//#include "i2c_dual_sw.h"
//#endif


static OTG_Drv_Info g_OtgInfo;
static kal_bool g_otg_idpin_state;
extern kal_bool OTG_IDPIN_CABLE_PLUGIN_LEVEL;
extern kal_uint8 INT_Exception_Enter;/* Exception flag*/

static void OTG_Set_Plug_Type(OTG_PLUG_TYPE type);
static kal_bool OTG_Is_B_Session_Valid(void);

static void OTG_Timeout_Hdlr(void *parameter);
static void OTG_Start_Timer(OTG_TIMER_TYPE type, kal_uint32 ticks);
static void OTG_Stop_Timer(void);
static OTG_TIMER_TYPE OTG_Get_TimerType(void);

static kal_bool OTG_Is_TimerOut(OTG_TIMER_TYPE type);
static kal_bool OTG_Is_TimerActive(void);
static void OTG_Process_A_Idle(void);
static void OTG_Process_A_Wait_BCon(void);
static void OTG_Process_A_Host(void);
static void OTG_Process_A_Suspend(void);
static void OTG_Process_A_Peripheral(void);
static void OTG_Process_A_Wait_VFall(void);
static void OTG_Process_B_Idle(void);
static void OTG_Process_B_Session_Valid(void);
static void OTG_Process_B_SRP_Init(void);
static void OTG_Process_B_Peripheral(void);
static void OTG_Process_B_Wait_ACon(void);
static void OTG_Process_B_Host(void);
static void OTG_State_Machine(void);


static void OTGDMA_HISR(void);
static void OTGDMA_LISR(void);

static void OTG_LISR(void);
static void OTG_ID_EINT_HISR(void);



/************************************************************
	A plug detection functions
*************************************************************/

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#if defined(DRV_USB_PHY_U65_IP)
/* under construction !*/
/* under construction !*/
#endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif

static void OTG_ID_EINT_HISR(void)
{
	ilm_struct *maplug_ilm;
	kal_uint8 otg_idpin_eint = custom_eint_get_channel(otg_idpin_eint_chann);

	if(g_otg_idpin_state != OTG_IDPIN_CABLE_PLUGIN_LEVEL)
	{
		USB_Dbg_Trace(USB_HOST_A_CABLE_PLUG_OUT, (kal_uint32)g_OtgInfo.otg_state, 0);

#if (defined(DRV_USB_PHY_COST_DOWN))
		USB_DRV_SetBits8(USB_PHYIR3_2, USB_PHYIR3_2_IDDIG);
		USB_DRV_SetBits8(USB_PHYCR4_0,USB_PHYCR4_0_FORCE_IDDIG);
#endif

		/* USB IRQ is masked so there is no expected interrupt */
	#if defined(DRV_USB_IP_V3)
		if(g_OtgInfo.otg_state == OTG_STATE_A_HOST)
		{
			if(USB_HCD_Get_UnMask_Irq(0) == KAL_FALSE)
			{
				USB_Dbg_Trace(USB_A_CABLE_OUT_BUT_NO_DISCONN, 1, 1);
				USB_HCD_Disconn_Hdlr(0);
			}
			else
			{
				USB_Dbg_Trace(USB_A_CABLE_OUT_BUT_NO_DISCONN, USB_DRV_Reg8(USB_LINE_STATE), USB_DRV_Reg8(USB_OPSTATE));
			}
		}
		else if(g_OtgInfo.otg_state == OTG_STATE_A_PERIPHERAL)
		{
			if(USB_HW_Get_UnMask_Irq() == KAL_FALSE)
			{
				USB_Dbg_Trace(USB_A_CABLE_OUT_BUT_NO_DISCONN, 2, 0);
				g_OtgInfo.otg_state = OTG_STATE_A_WAIT_BCON;
			}
			else
			{
				USB_Dbg_Trace(USB_A_CABLE_OUT_BUT_NO_DISCONN, USB_DRV_Reg8(USB_LINE_STATE), USB_DRV_Reg8(USB_OPSTATE));
			}
		}
	#endif

		OTG_Set_Plug_Type(OTG_PLUG_B);

		DRV_BuildPrimitive(maplug_ilm,
						stack_get_active_module_id(),
						MOD_USB,
						MSG_ID_USB_A_PLUGOUT_IND,
						NULL);

		msg_send_ext_queue(maplug_ilm);
	}
	else
	{
		USB_Dbg_Trace(USB_HOST_A_CABLE_PLUG_IN, 0, 0);

	/* Open AHB clock, then we can set USB IP's registers */	
#if defined(__OLD_PDN_ARCH__)
#if defined(__OLD_PDN_DEFINE__)
	USB_DRV_WriteReg(DRVPDN_CON0_CLR, DRVPDN_CON0_USB);
#elif defined(__CLKG_DEFINE__)
	USB_DRV_WriteReg(CG_CLR0, CG_CON0_USB);
#endif
#else
	DRVPDN_Disable(PDN_USB);
#endif

		OTG_Set_Plug_Type(OTG_PLUG_A);

#if (defined(DRV_USB_PHY_COST_DOWN))
		USB_DRV_ClearBits8(USB_PHYIR3_2, USB_PHYIR3_2_IDDIG);
		USB_DRV_SetBits8(USB_PHYCR4_0, USB_PHYCR4_0_FORCE_IDDIG);
#endif

		DRV_BuildPrimitive(maplug_ilm,
						stack_get_active_module_id(),
						MOD_USB,
						MSG_ID_USB_A_PLUGIN_IND,
						NULL);
		msg_send_ext_queue(maplug_ilm);
	}

	g_otg_idpin_state = (g_otg_idpin_state == KAL_FALSE)?(KAL_TRUE):(KAL_FALSE);
	EINT_Set_Polarity(otg_idpin_eint, g_otg_idpin_state);

#if 0 // (defined(MT6326))
/* under construction !*/
/* under construction !*/
#if defined(__OLD_PDN_ARCH__)
#if defined(__OLD_PDN_DEFINE__)
/* under construction !*/
#elif defined(__CLKG_DEFINE__)
/* under construction !*/
#endif
#else
/* under construction !*/
#endif
/* under construction !*/
#endif


	EINT_UnMask(otg_idpin_eint);
}


static void OTG_Set_Plug_Type(OTG_PLUG_TYPE type)
{
	g_OtgInfo.plug_type = type;
}



void OTG_Set_Status(OTG_STATUS_TYPE type, kal_bool set)
{
	kal_bool	b_process_state_machine = KAL_FALSE;
	kal_uint32 savedMask;


	USB_Dbg_Trace(USB_OTG_SET_STATUS, (kal_uint32)type, (kal_uint32)set);

	switch(type)
	{
	case OTG_STATUS_A_BUS_REQUEST:
		g_OtgInfo.a_bus_req = set;
		break;
	case OTG_STATUS_A_DETECT_B_CONN:
		g_OtgInfo.a_detect_b_conn = set;
		break;
	case OTG_STATUS_A_DETECT_B_SUSPEND:
		g_OtgInfo.a_detect_b_suspend = set;
		break;
	case OTG_STATUS_A_DETECT_B_RESUME:
		g_OtgInfo.a_detect_b_resume = set;
		break;
	case OTG_STATUS_A_DETECT_B_DATA_PLS:
		g_OtgInfo.a_detect_b_data_pls = set;
		break;
	case OTG_STATUS_A_SUSPEND_REQUEST:
		g_OtgInfo.a_suspend_req = set;
		break;
	case OTG_STATUS_A_RESUME_REQUEST:
		g_OtgInfo.a_resume_req = set;
		break;
	case OTG_STATUS_A_SET_B_HNP_ENABLE:
		g_OtgInfo.a_set_b_hnp_enable = set;
		break;
	case OTG_STATUS_B_SESSION_VALID:
		g_OtgInfo.b_session_valid = set;
		break;
	case OTG_STATUS_B_SUSPEND_REQUEST:
		g_OtgInfo.b_suspend_req = set;
		break;
	case OTG_STATUS_B_DETECT_A_CONN:
		g_OtgInfo.b_detect_a_conn = set;
		break;
	case OTG_STATUS_B_DETECT_A_SUSPEND:
		g_OtgInfo.b_detect_a_suspend = set;
		break;
	case OTG_STATUS_B_DETECT_A_RESUME:
		g_OtgInfo.b_detect_a_resume = set;
		break;
	case OTG_STATUS_B_DETECT_A_RESET:
		g_OtgInfo.b_detect_a_reset = set;
		break;
	case OTG_STATUS_B_SRP_REQUEST:
		g_OtgInfo.b_srp_request = set;
		break;
	case OTG_STATUS_B_HNP_ENABLE:
		g_OtgInfo.b_hnp_enable = set;
		break;
	default:
		ASSERT(0);
		break;
	}

	/* Process OTG state machine*/
	savedMask = SaveAndSetIRQMask();
	if(g_OtgInfo.b_processing == KAL_FALSE)
	{
		g_OtgInfo.b_processing = KAL_TRUE;
		b_process_state_machine = KAL_TRUE;
	}
	RestoreIRQMask(savedMask);

	if(b_process_state_machine == KAL_TRUE)
	{
		OTG_State_Machine();
		g_OtgInfo.b_processing = KAL_FALSE;
	}

#if defined(DRV_USB_IP_V3)
	if((g_OtgInfo.suspend_and_conn_intr == KAL_TRUE)&&(g_OtgInfo.otg_state == OTG_STATE_B_WAIT_ACON))
	{
		g_OtgInfo.suspend_and_conn_intr = KAL_FALSE;
		USB_HCD_Conn_Hdlr(0);
	}
#endif
}


OTG_PLUG_TYPE OTG_Get_Plug_Type(void)
{
	return g_OtgInfo.plug_type;
}


OTG_STATE OTG_Get_State(void)
{
	return g_OtgInfo.otg_state;
}


kal_uint8 OTG_Get_Intr_Status(void)
{
	return g_OtgInfo.otg_intr_status;
}


void OTG_Set_Conn_Intr_With_Suspend(void)
{
	g_OtgInfo.suspend_and_conn_intr = KAL_TRUE;
}


void OTG_Set_Disconn_Status(void)
{
	g_OtgInfo.disconn_and_conn_intr = KAL_TRUE;
}


kal_bool OTG_Is_B_Process_SRP(void)
{
	return g_OtgInfo.b_srp_request;
}





static kal_bool OTG_Is_B_Session_Valid(void)
{
	kal_bool ret = KAL_FALSE;
	kal_uint8	 usbdevice = (USB_DRV_Reg8(USB_DEVCTL)&USB_DEVCTL_VBUS);


	if((usbdevice == USB_DEVCTL_ABOVE_VBUS_VALID)||(usbdevice == USB_DEVCTL_ABOVE_A_VALID))
		ret = KAL_TRUE;

	return ret;
}


/************************************************************
	Timer  functions
*************************************************************/

/* GPT timeout handler */
static void OTG_Timeout_Hdlr(void *parameter)
{
	DclSGPT_Control((DCL_HANDLE)(g_OtgInfo.gpt_handle), SGPT_CMD_STOP, 0);
//	GPTI_StopItem(g_OtgInfo.gpt_handle);

	g_OtgInfo.timer_out = KAL_TRUE;

#if defined(DRV_USB_IP_V3)
	USB_Dbg_Trace(USB_OTG_TIMEOUT_HDLR, (kal_uint32)g_OtgInfo.timer_type, USB_DRV_Reg8(USB_POWER));

	if(OTG_Is_TimerOut(OTG_TIMER_B_CHECK_A_RESUME) == KAL_TRUE)
	{
		if((USB_DRV_Reg8(USB_LINE_STATE)&0xC0) == 0x80)
		{
			/* K resume! */
			/* Stop the timer, let state machine see as resume */
			OTG_Stop_Timer();

			/* K resume! */
			g_OtgInfo.b_detect_a_resume = KAL_TRUE;
		}
	}
#endif

	/* Process OTG state machine*/
	if(g_OtgInfo.b_processing == KAL_FALSE)
	{
//		g_OtgInfo.b_processing = KAL_TRUE;
		OTG_State_Machine();
//		g_OtgInfo.b_processing = KAL_FALSE;
	}
}


/* The tick unit is ms */
static void OTG_Start_Timer(OTG_TIMER_TYPE type, kal_uint32 ticks)
{
	SGPT_CTRL_START_T start;
		
	if(g_OtgInfo.timer_on==KAL_TRUE)
		EXT_ASSERT(0, (kal_uint32)g_OtgInfo.timer_on, (kal_uint32)g_OtgInfo.timer_type, (kal_uint32)g_OtgInfo.timer_out);

	g_OtgInfo.timer_type = type;
	g_OtgInfo.timer_on = KAL_TRUE;
	g_OtgInfo.timer_out = KAL_FALSE;

#if defined(DRV_USB_IP_V3)
	/* IP V3 only have GPT timer */
	if (ticks < 10)
		ticks = 10;

    start.u2Tick = ticks/10;					
    start.pfCallback = OTG_Timeout_Hdlr;			
    start.vPara = &g_OtgInfo;							
    DclSGPT_Control((DCL_HANDLE)(g_OtgInfo.gpt_handle), SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start);	

#endif
}


/* stop the timer */
static void OTG_Stop_Timer(void)
{
	DclSGPT_Control((DCL_HANDLE)(g_OtgInfo.gpt_handle), SGPT_CMD_STOP, 0);
//	GPTI_StopItem(g_OtgInfo.gpt_handle);

	g_OtgInfo.timer_on = KAL_FALSE;
	g_OtgInfo.timer_out = KAL_FALSE;
	g_OtgInfo.timer_type = OTG_TIMER_NONE;
}


/* return KAL_TRUE if "type" timer is timeout */
static kal_bool OTG_Is_TimerOut(OTG_TIMER_TYPE type)
{
	kal_bool ret = KAL_FALSE;


	if((g_OtgInfo.timer_out == KAL_TRUE)&&(g_OtgInfo.timer_type == type))
		ret = KAL_TRUE;

	return ret;
}


/* return if any timer is active*/
static kal_bool OTG_Is_TimerActive(void)
{
	kal_bool	ret = KAL_FALSE;

	if((g_OtgInfo.timer_on == KAL_TRUE) || (g_OtgInfo.timer_out == KAL_TRUE))
		ret = KAL_TRUE;

	return ret;
}


/* Return active timer type */
static OTG_TIMER_TYPE OTG_Get_TimerType(void)
{
	return g_OtgInfo.timer_type;
}


/************************************************************
	state machine functions
*************************************************************/

/* Go from Vbus error */
static void OTG_Process_A_Vbus_Fail(void)
{
	g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

	/* Turn off VBUS "auto" */
	USB_HCD_VBusEnable(0, KAL_FALSE);

	if(g_OtgInfo.host_up == KAL_TRUE)
	{
		g_OtgInfo.host_up = KAL_FALSE;
		g_OtgInfo.a_host_stop_hdlr();
		g_OtgInfo.host_down_hdlr();
	}

	if(g_OtgInfo.device_up == KAL_TRUE)
	{
		g_OtgInfo.device_up = KAL_FALSE;
		g_OtgInfo.device_down_hdlr();
	}

	if(g_OtgInfo.a_process_hnp == KAL_TRUE)
	{
//		ASSERT(0);
		g_OtgInfo.a_process_hnp = KAL_FALSE;
		/* OTG_A_HNP_Fail_Hdlr(void) */
		/* OTG_Display_Message(OTG_DISPLAY_MSG_HNP_STOP); */
		g_OtgInfo.a_hnp_fail_hdlr();
	}
	else
	{
		/* device draws too much current */
		/* void OTG_A_VRise_Fail_Hdlr(void) */
		/* OTG_Display_Message(OTG_DISPLAY_MSG_UN_SUPPORT); */
		g_OtgInfo.a_vrise_fail_hdlr();
	}

	OTG_Start_Timer(OTG_TIMER_A_CHECK_VFALL, TA_CHECK_VFALL);
	OTG_Process_A_Wait_VFall();
}


static void OTG_Process_A_Idle(void)
{
	if(OTG_Get_Plug_Type() == OTG_PLUG_B)
	{
		//  A cable plug out, A device return to A_ldle, then gp to B_idle
		//  from OTG_Process_A_Wait_VFall
		g_OtgInfo.otg_state = OTG_STATE_B_IDLE;
		OTG_Process_B_Idle();
		return;
	}

	/* A detect SRP */
	if(g_OtgInfo.a_detect_b_data_pls == KAL_TRUE)
	{
		g_OtgInfo.a_detect_b_data_pls = KAL_FALSE;
#ifdef OTG_A_NOT_INSERTION_BASE		
		g_OtgInfo.a_bus_req = KAL_TRUE;
#endif
	}
#ifdef OTG_A_NOT_INSERTION_BASE
/*
	else if(OTG_Is_TimerOut(OTG_TIMER_A_ATTATCH_DEBOUNCE) == KAL_TRUE)
	{
		OTG_Stop_Timer();

		if(g_OtgInfo.host_up == KAL_FALSE)
		{
			g_OtgInfo.host_up = KAL_TRUE;
			g_OtgInfo.host_up_hdlr();
		}
	}
	else if(g_OtgInfo.a_detect_b_resume == KAL_TRUE)
	{
		// compliance test
		// deassert resume
		g_OtgInfo.a_detect_b_resume = KAL_FALSE;

		ASSERT(0);
//		USB_DRV_SetBits8(USB_POWER, USB_POWER_RESUME);
//		OTG_Start_Timer(OTG_TIMER_A_RESUME, TA_RESUME);
	}
*/

	USB_DRV_WriteReg8(USB_INTRUSBE, USB_INTRUSBE_SESSREQ);
#endif

	if(g_OtgInfo.a_bus_req == KAL_TRUE)
	{
		/* In case go here and SRP timer is still on*/
		OTG_Stop_Timer();

		/* If device is up, pull it down */
		if(g_OtgInfo.device_up == KAL_TRUE)
		{
			/* For 1. B in 2. A in 3.  B out case */
			g_OtgInfo.device_up = KAL_FALSE;
			g_OtgInfo.device_down_hdlr();
		}

		/* Software reset */
		USB_DRV_WriteReg(USB_SWRST, USB_SWRST_SWRST);

		/* Enable system interrupts, but disable all ep interrupts */
		USB_EnSysIntr();

		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_IDLE_TO_A_WAIT_BCON, 0, 0);
		/* Turn on VBUS, and wait B connect or VBUS error */
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_BCON;

#ifdef __OTG_A_PERIODIC_VBUS_CYCLE__
		OTG_Start_Timer(OTG_TIMER_A_VBUS_PERIOD_VON, TA_VBUS_PERIOD_VON);
#endif

	#ifdef __CHARGER_USB_DETECT_WIHT_ONE_EINT__
		g_OtgInfo.b_is_vbus_power_on = KAL_TRUE;
	#endif
		USB_HCD_VBusEnable(0, KAL_TRUE);

		if(g_OtgInfo.host_up == KAL_FALSE)
		{
			g_OtgInfo.host_up = KAL_TRUE;
			g_OtgInfo.host_up_hdlr();
		}
	}
#ifdef OTG_A_NOT_INSERTION_BASE
/*
	else
	{
		// for compliance test
		// start timer to debounce attatch
		if(g_OtgInfo.host_up == KAL_FALSE)
		{
			OTG_Start_Timer(OTG_TIMER_A_ATTATCH_DEBOUNCE, TA_ATTATCH_DEBOUNCE);
		}
	}
*/
#endif
}


static void OTG_Process_A_Wait_BCon(void)
{
	if (g_OtgInfo.a_detect_b_conn == KAL_TRUE)
	{
#ifdef __OTG_A_PERIODIC_VBUS_CYCLE__
		OTG_Stop_Timer();
#endif
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_WAIT_BCON_TO_A_HOST, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_HOST;
	}
	else if(OTG_Get_TimerType() == OTG_TIMER_A_RESUME)
	{
//		ASSERT(0);
		OTG_Stop_Timer();
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_RESUME);
	}
#ifdef __OTG_A_PERIODIC_VBUS_CYCLE__
	else if(OTG_Is_TimerOut(OTG_TIMER_A_VBUS_PERIOD_VON) == KAL_TRUE)
	{
		OTG_Stop_Timer();
		USB_Dbg_Trace(USB_OTG_STATE_A_WAIT_BCON_TURN_OFF_VBUS, 0, 0);

		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

		/* Turn off VBUS */
		USB_HCD_VBusEnable(0, KAL_FALSE);

		if(g_OtgInfo.host_up == KAL_TRUE)
		{
			g_OtgInfo.host_up = KAL_FALSE;
			g_OtgInfo.a_host_stop_hdlr();
			g_OtgInfo.host_down_hdlr();
		}

		OTG_Start_Timer(OTG_TIMER_A_VBUS_PERIOD_VFALL, TA_VBUS_PERIOD_VFALL);
	}
#endif
#ifdef OTG_A_NOT_INSERTION_BASE
/*
	else if(OTG_Is_TimerOut(OTG_TIMER_A_WAIT_BCON) == KAL_TRUE)
	{
		OTG_Stop_Timer();
		g_OtgInfo.a_bus_req=KAL_FALSE;
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;
		USB_DRV_SetBits8(USB_POWER, USB_POWER_SUSPENDMODE);
		// Turn off VBUS
		USB_DRV_ClearBits8(OTG_CTRL, OTG_CTL_VBUS_ON);

		g_OtgInfo.a_no_response_hdlr();

		OTG_Start_Timer(OTG_TIMER_A_CHECK_VFALL, TA_CHECK_VFALL);
		OTG_Process_A_Wait_VFall();
	}

	if(g_OtgInfo.otg_state == OTG_STATE_A_WAIT_BCON)
	{
		if(OTG_Is_TimerActive() == KAL_TRUE)
		{
			if(OTG_Get_TimerType() != OTG_TIMER_NONE)
			{
				OTG_Stop_Timer();
				OTG_Start_Timer(OTG_TIMER_A_WAIT_BCON, TA_WAIT_BCON);
			}
			else
			{
				ASSERT(0);
			}
		}
		else
		{
//			OTG_Stop_Timer();
			OTG_Start_Timer(OTG_TIMER_A_WAIT_BCON, TA_WAIT_BCON);
		}
	}
*/
#endif
}


static void OTG_Process_A_Host(void)
{
#ifdef OTG_A_NOT_INSERTION_BASE
/*
	if(OTG_Get_TimerType() == OTG_TIMER_A_WAIT_BCON)
	{
		OTG_Stop_Timer();
	}
*/
#endif

	/*
	** If mini B plug is removed RESET (DETACH) interrupt will
	** happen on A-Device.The DETACH will processed by the host
	** ISR  and set a_detect_b_conn to  FALSE
	*/
	if((g_OtgInfo.a_detect_b_conn == KAL_FALSE)&&(g_OtgInfo.a_detect_b_resume ==KAL_FALSE))
	{
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_HOST_TO_A_WAIT_BCON, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_BCON;

		g_OtgInfo.a_host_stop_hdlr();

		/* When A device detect conn and disconn intr at the same time, always turn off Vbus and then turn on Vbus */
		if(g_OtgInfo.disconn_and_conn_intr == KAL_TRUE)
		{
			g_OtgInfo.disconn_and_conn_intr = KAL_FALSE;

			USB_Dbg_Trace(USB_OTG_STATE_A_WAIT_BCON_TURN_OFF_VBUS, 0, 0);

			g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

			/* Turn off VBUS*/
			USB_HCD_VBusEnable(0, KAL_FALSE);

			if(g_OtgInfo.host_up == KAL_TRUE)
			{
				g_OtgInfo.host_up = KAL_FALSE;
				g_OtgInfo.host_down_hdlr();
			}

			OTG_Start_Timer(OTG_TIMER_A_VBUS_PERIOD_VFALL, TA_VBUS_PERIOD_VFALL);
		}
#ifdef __OTG_A_PERIODIC_VBUS_CYCLE__
		else if(g_OtgInfo.a_bus_req == KAL_TRUE)
		{
			/* When A cable out, wait a moment and then turn off Vbus */
			OTG_Start_Timer(OTG_TIMER_A_VBUS_PERIOD_VON, TA_VBUS_PERIOD_VON);
		}
#endif

#ifdef OTG_A_NOT_INSERTION_BASE
		USB_Dbg_Trace(USB_OTG_STATE_A_WAIT_BCON_TURN_OFF_VBUS, 0, 0);

		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

		/* Turn off VBUS */
		USB_HCD_VBusEnable(0, KAL_FALSE);

		if(g_OtgInfo.host_up == KAL_TRUE)
		{
			g_OtgInfo.host_up = KAL_FALSE;
			g_OtgInfo.host_down_hdlr();
		}
#endif
	}
	/************   Starting HNP **************************************
	** When the A-device is in the a_host state and has set the dual-role
	** B-device's HNP enable bit (b_hnp_enable = TRUE ie, a_suspend_req
	** = TRUE) the A-device shall place the connection to the B-device into
	** Suspend when it is finished using the bus.  We can go to the
	** suspend state also if user want to power down.
	*/
	else if((g_OtgInfo.a_set_b_hnp_enable == KAL_TRUE)&&(g_OtgInfo.a_detect_b_resume ==KAL_FALSE))
	{
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_HOST_TO_A_SUSPEND, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_SUSPEND;
		USB_DRV_SetBits8(USB_POWER, (USB_POWER_SUSPENDMODE|USB_POWER_SOFTCONN));

		/*  When A idle, B can decide to enter HNP process more than 200ms */
//		OTG_Start_Timer(OTG_TIMER_A_IDLE_BDIS, TA_AIDL_BDIS);

		g_OtgInfo.a_process_hnp = KAL_TRUE;
      	}
	/* A request to suspend the bus */
      	else if(g_OtgInfo.a_suspend_req == KAL_TRUE)
      	{
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_HOST_TO_A_SUSPEND, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_SUSPEND;
		USB_DRV_SetBits8(USB_POWER, USB_POWER_SUSPENDMODE);
		g_OtgInfo.a_suspend_req = KAL_FALSE;
      	}
	/* This may be because B device is not supported by this A device */
      	else if(g_OtgInfo.a_bus_req == KAL_FALSE)
      	{
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

		USB_DRV_SetBits8(USB_POWER, USB_POWER_SUSPENDMODE);

		/* Turn off VBUS*/
		USB_HCD_VBusEnable(0, KAL_FALSE);

		USB_DRV_ClearBits8(USB_INTRUSBE, USB_INTRUSBE_SESSREQ);

		if(g_OtgInfo.host_up == KAL_TRUE)
		{
			g_OtgInfo.host_up = KAL_FALSE;
			g_OtgInfo.a_host_stop_hdlr();
			g_OtgInfo.host_down_hdlr();
		}

		if(g_OtgInfo.device_up == KAL_TRUE)
		{
			g_OtgInfo.device_up = KAL_FALSE;
			g_OtgInfo.device_down_hdlr();
		}

		OTG_Start_Timer(OTG_TIMER_A_CHECK_VFALL, TA_CHECK_VFALL);
		OTG_Process_A_Wait_VFall();
      	}
	else if(OTG_Is_TimerOut(OTG_TIMER_A_RESUME) == KAL_TRUE)
	{
		/* host resume 20ms timeout */
		OTG_Stop_Timer();
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_RESUME);
	}
}


static void OTG_Process_A_Suspend(void)
{
	/* HNP failed */
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
	/* a_detect_b_conn = FALSE if a disconn interrupt happens and it will set by
	** the application; a_set_b_hnp_en is false as default and set to
	** if the request is not stalled
	*/
	if((g_OtgInfo.a_detect_b_conn == KAL_FALSE)&&(g_OtgInfo.a_set_b_hnp_enable == KAL_FALSE))
	{
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_SUSPEND_TO_A_WAIT_BCON, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_BCON;
	}
	/*
	** If the B-device disconnects after the bus has been suspended,
	** then this is an indication that the B-device is attempting
	** to become Host. When the A-device detects the disconnect
	** from the B-device, it shall turn on its D+ pull-up resistor
	** within 3 ms (TA_BDIS_ACON max) to acknowledge
	** the request from the B-device.
	*/
	else if((g_OtgInfo.a_detect_b_conn == KAL_FALSE)&&(g_OtgInfo.a_set_b_hnp_enable==KAL_TRUE))
	{
		/* Must execute within 3ms */
		/* stop OTG_TIMER_A_IDLE_BDIS timer */
//		OTG_Stop_Timer();

		/* start up device */
		if((g_OtgInfo.host_up == KAL_FALSE)||(g_OtgInfo.device_up == KAL_TRUE))
			EXT_ASSERT(0, (kal_uint32)g_OtgInfo.host_up, (kal_uint32)g_OtgInfo.device_up, 0);

		g_OtgInfo.host_up = KAL_FALSE;
		g_OtgInfo.host_down_hdlr();
		g_OtgInfo.device_up = KAL_TRUE;
		g_OtgInfo.device_up_hdlr();

		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_SUSPEND_TO_A_PERIPHERAL, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_PERIPHERAL;
		g_OtgInfo.a_set_b_hnp_enable = KAL_FALSE;
		g_OtgInfo.a_process_hnp = KAL_FALSE;
		g_OtgInfo.a_hnp_stop_hdlr();
	}
	else if (g_OtgInfo.a_resume_req == KAL_TRUE)
	{
		/* stop OTG_TIMER_A_IDLE_BDIS timer */
//		OTG_Stop_Timer();

		if(g_OtgInfo.a_process_hnp == KAL_TRUE)
		{
			g_OtgInfo.a_process_hnp = KAL_FALSE;
			g_OtgInfo.a_hnp_fail_hdlr();
		}

		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_SUSPEND_TO_A_HOST, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_HOST;

		USB_DRV_SetBits8(USB_POWER, USB_POWER_RESUME);
		g_OtgInfo.a_resume_req = KAL_FALSE;
		OTG_Start_Timer(OTG_TIMER_A_RESUME, TA_RESUME);
	}
	/* OPT remote wakeup */
	else if(g_OtgInfo.a_detect_b_resume == KAL_TRUE)
	{
		/* A-host detect B-device send remote wakeup resume */

		/* stop OTG_TIMER_A_IDLE_BDIS timer */
//		OTG_Stop_Timer();

		g_OtgInfo.a_detect_b_resume = KAL_FALSE;

		if(g_OtgInfo.a_process_hnp == KAL_TRUE)
		{
			g_OtgInfo.a_process_hnp = KAL_FALSE;
			g_OtgInfo.a_hnp_fail_hdlr();
		}

		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_SUSPEND_TO_A_HOST, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_HOST;
		g_OtgInfo.a_set_b_hnp_enable = KAL_FALSE;
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_SOFTCONN);
		/* IP resume automatically, wait 20ms to clear resume bit */
		OTG_Start_Timer(OTG_TIMER_A_RESUME, TA_RESUME);
	}
}


static void OTG_Process_A_Peripheral(void)
{
	/* In the current design, do not allow host init again*/
	/*
	** A-device detects lack of bus activity for more than 3 ms
	** (TA_BIDL_ADIS min) and turns off its D+ pull-up. Alternatively,
	** if the A-device has no further need to communicate with the
	** B-device, the A-device may turn off VBUS and end the session.
	** SUSPEND will be processed by the  device ISR a_detect_b_suspend
	** variable will be set by the application
	** The following will be done when this happens:
	**
	**   - disconnect its pull up
	**   - allow time for the data line to discharge
	**   - check if the B-device has connected its pull up
	*/
	if((g_OtgInfo.a_detect_b_suspend == KAL_TRUE)&&(OTG_Is_TimerActive() == KAL_FALSE))
	{
		g_OtgInfo.a_detect_b_suspend = KAL_FALSE;

		/* start up host */
		if((g_OtgInfo.device_up == KAL_FALSE)||(g_OtgInfo.host_up == KAL_TRUE))
			EXT_ASSERT(0, (kal_uint32)g_OtgInfo.device_up, (kal_uint32)g_OtgInfo.host_up, 0);

		g_OtgInfo.device_up = KAL_FALSE;
		g_OtgInfo.device_down_hdlr();
		g_OtgInfo.host_up = KAL_TRUE;
		g_OtgInfo.host_up_hdlr();

		/* Clear softconn bit */
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_SOFTCONN);

		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_PERIPHERAL_TO_A_WAIT_BCON, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_WAIT_BCON;
	}

// official should be this , but not suitable for OPT test
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
}


static void OTG_Process_A_Wait_VFall(void)
{
	if((OTG_Is_B_Session_Valid() == KAL_FALSE)
		||(OTG_Get_Plug_Type() == OTG_PLUG_B))
	{
		USB_Dbg_Trace(USB_OTG_STATE_FROM_A_WAIT_VFAIL_TO_A_IDLE, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_A_IDLE;

	#ifdef __OTG_A_PERIODIC_VBUS_CYCLE__
		if(OTG_Get_TimerType() == OTG_TIMER_A_VBUS_PERIOD_VFALL)
			g_OtgInfo.a_bus_req = KAL_TRUE;
		else
			g_OtgInfo.a_bus_req = KAL_FALSE;
	#else
		g_OtgInfo.a_bus_req = KAL_FALSE;
	#endif
		OTG_Stop_Timer();

		/* In current design, conditions that may go here are
		    1. B device draws too much current
		    2. A device dose not recognize B device and B device dose not take HNP
		    3. B device return from HNP host
		*/

		/* Only set g_OtgInfo.b_is_vbus_power_on = KAL_FALSE when A cable is plugged out */
	#ifdef __CHARGER_USB_DETECT_WIHT_ONE_EINT__
		if(OTG_Get_Plug_Type() == OTG_PLUG_B)
			g_OtgInfo.b_is_vbus_power_on = KAL_FALSE;
	#endif

		OTG_Process_A_Idle();
		return;
	}
#ifdef OTG_A_NOT_INSERTION_BASE
/*
	else if(g_OtgInfo.a_detect_b_resume == KAL_TRUE)
	{
		// compliance test
		// deassert resume
		g_OtgInfo.a_detect_b_resume = KAL_FALSE;

		ASSERT(0);
//		USB_DRV_SetBits8(USB_POWER, USB_POWER_RESUME);
//		OTG_Start_Timer(OTG_TIMER_A_RESUME, TA_RESUME);
	}
*/
#endif

	if(OTG_Is_TimerOut(OTG_TIMER_A_CHECK_VFALL) == KAL_TRUE)
	{
		OTG_Stop_Timer();
		OTG_Start_Timer(OTG_TIMER_A_CHECK_VFALL, TA_CHECK_VFALL);
	}
}


static void OTG_Process_B_Idle(void)
{
	/* Cable plugged out */
	/* If host or device is up, pull it down*/
	if(g_OtgInfo.host_up == KAL_TRUE)
	{
		g_OtgInfo.host_up = KAL_FALSE;
		g_OtgInfo.host_down_hdlr();
	}

	if(g_OtgInfo.device_up == KAL_TRUE)
	{
		g_OtgInfo.device_up = KAL_FALSE;
		g_OtgInfo.device_down_hdlr();
	}

	/*
	** When a B-device detects that the voltage on VBUS is greater
	** than the B-Device Session Valid threshold (VSESS_VLD),
	** then the B-device shall consider a session to be in progress.
	** After the VBUS voltage crosses this threshold, the B-device
	** shall assert either the D+ or D- data-line within 100 ms.
	*/
	if(g_OtgInfo.b_session_valid == KAL_TRUE)
	{
		/* SRP succeed */
		OTG_Process_B_Session_Valid();
	}
	else if(g_OtgInfo.b_srp_request == KAL_TRUE)
	{
		/*  ********* Starting  SRP *****************************************
		** When the B-device detects that VBUS has gone below its Session End
		** threshold and detects that both D+ and D- have been low (SE0) for
		** at least 2 ms (TB_SE0_SRP min), then any previous session on the
		** A-device is over and a new session may start. The B-device may
		** initiate the SRP any time the initial conditions of Section 5.3.2
		** are met.
		*/
		/* Wait 2ms is used only to make sure that host detect disconnect */

		/* Start the fail timer, OPT will wait 5s to vrise Vbus */
		OTG_Start_Timer(OTG_TIMER_B_SRP_FAIL, TB_SRP_FAIL);
		USB_DRV_SetBits8(USB_DEVCTL, USB_DEVCTL_SESSION);
//		kal_sleep_task(9);
		kal_sleep_task(22);
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_SOFTCONN);

		g_OtgInfo.otg_state = OTG_STATE_B_SRP_INIT;
	}
}


static void OTG_Process_B_Session_Valid(void)
{
	/* Init device */
	if(g_OtgInfo.device_up == KAL_TRUE)
		EXT_ASSERT(0, (kal_uint32)g_OtgInfo.device_up, 0, 0);

	g_OtgInfo.device_up = KAL_TRUE;

	g_OtgInfo.otg_state = OTG_STATE_B_PERIPHERAL;
        // because device up handler will enable interrupt, change state first
		
	g_OtgInfo.device_up_hdlr();

	g_OtgInfo.b_srp_request = KAL_FALSE;

	USB_Dbg_Trace(USB_OTG_STATE_FROM_B_IDLE_TO_B_PERIPHERAL, 0, 0);
//	g_OtgInfo.otg_state = OTG_STATE_B_PERIPHERAL;
}


static void OTG_Process_B_SRP_Init(void)
{
	/*
	** When a B-device detects that the voltage on VBUS is greater than the
	** B-Device Session Valid threshold (VSESS_VLD), then the B-device
	** shall consider a session to be in progress. After the VBUS voltage
	** crosses this threshold, the B-device shall assert either the D+ or
	** D- data-line within 100 ms(TB_SVLD_BCON max).
	*/
	if(g_OtgInfo.b_session_valid == KAL_TRUE)
	{
		/* This section is executed in USB EINT context */
		/* SRP OK */
		OTG_Stop_Timer();
		/* OTG_B_SRP_Stop_Hdlr(void) */
		/* OTG_Display_Message(OTG_DISPLAY_MSG_STOP_CONNECTING); */
		g_OtgInfo.b_srp_stop_hdlr();
		OTG_Process_B_Session_Valid();
	}
	else if (OTG_Is_TimerOut(OTG_TIMER_B_SRP_FAIL) == KAL_TRUE)
	{
		/*
		** The error call back have the following requirement:
		** After initiating SRP, the B-device is required to wait at least
		** 5 seconds (TB_TB_SRP_FAIL min) for the A-device to respond,
		** before informing the user that the communication attempt
		** has failed.
		**
		*/

		/* SRP fail */
		OTG_Stop_Timer();

		g_OtgInfo.b_srp_request = KAL_FALSE;
		g_OtgInfo.otg_state = OTG_STATE_B_IDLE;

		/* OTG_B_SRP_Fail_Hdlr(void) */
		/* OTG_Display_Message(OTG_DISPLAY_MSG_NO_RESPONSE); */
		g_OtgInfo.b_srp_fail_hdlr();

		OTG_Process_B_Idle();
	}
}


static void OTG_Process_B_Peripheral(void)
{
	/* b_detect_a_suspend will be processed by the DEVICE ISR */
	/*
	** B-device detects that bus is idle for more than 4 ms
	** (TB_AIDL_BDIS min) and begins HNP by turning off pull-up on D+.
	** This allows the bus to discharge to the SE0 state.
	** The following will be done when this happens:
	**
	**   disconnect its pull up
	**   allow time for the data line to discharge
	**   check if the A-device has connected its pull up
	*/
	if((g_OtgInfo.b_hnp_enable == KAL_TRUE)&&(g_OtgInfo.b_detect_a_suspend == KAL_TRUE))
	{
//		OTG_Stop_Timer();
//		USB_DRV_WriteReg8(USB_INTRUSBE, 0xFF);

		g_OtgInfo.b_detect_a_suspend = KAL_FALSE;

		/* Start up host */
		if((g_OtgInfo.device_up == KAL_FALSE) ||(g_OtgInfo.host_up == KAL_TRUE))
			EXT_ASSERT(0, (kal_uint32)g_OtgInfo.device_up, (kal_uint32)g_OtgInfo.host_up, 0);

		/* To prevent a case that A-device suspend but it does not do HNP correctly, so B-device must go back to peripheral */
		/* In this case, we do not want to UEM see cable in/out event */
		g_OtgInfo.device_up = KAL_FALSE;
		/* Do not execute device_down_hdlr yet */
//		g_OtgInfo.device_down_hdlr();
		g_OtgInfo.b_device_down_pending = KAL_TRUE;
		USB_Dbg_Trace(USB_OTG_B_DEVICE_PENDING_DOWN_HDLR, 0, 1);

		g_OtgInfo.host_up = KAL_TRUE;
		g_OtgInfo.host_up_hdlr();

		USB_Dbg_Trace(USB_OTG_STATE_FROM_B_PERIPHERAL_TO_B_WAIT_ACON, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_B_WAIT_ACON;

		if(g_OtgInfo.suspend_and_conn_intr == KAL_FALSE)
		{
			// Start timer to check if A resume
			OTG_Start_Timer(OTG_TIMER_B_CHECK_A_RESUME, TB_CHECK_A_RESUME);
		}
	}
}


static void OTG_Process_B_Wait_ACon(void)
{
	/*
	** After waiting long enough to insure that the D+ line
	** cannot be high due to the residual effect of the B-device
	** pull-up,(see Section 5.1.9), the B-device sees that the
	** D+ line is high and D- low, (i.e. J state). This indicates
	** that the A-device has recognized the HNP request from the
	** B-device. At this point, the B-device becomes Host and
	** asserts bus reset to start using the bus. The B-device
	** must assert the bus reset (SE0) within 1 ms (TB_ACON_BSE0 max)
	** of the time that the A-device turns on its
	** pull-up.
	*/
	/* After A connect interrupt, B host must reset within 1ms */
	/* Conn will be processed by the USB_HCD_HISR */
	if(g_OtgInfo.b_detect_a_conn == KAL_TRUE)
	{
		OTG_Stop_Timer();

		/* Execute device_down_hdlr when B device become host */
		if(g_OtgInfo.b_device_down_pending == KAL_TRUE)
		{
			g_OtgInfo.b_device_down_pending = KAL_FALSE;
			g_OtgInfo.device_down_hdlr();
			USB_Dbg_Trace(USB_OTG_B_DEVICE_PENDING_DOWN_HDLR, 0, 2);
		}

		g_OtgInfo.b_suspend_req = KAL_FALSE;
		g_OtgInfo.b_hnp_enable = KAL_FALSE;
//		USB_DRV_ClearBits8(USB_DEVCTL, USB_DEVCTL_HOSTREQ);
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_SOFTCONN);

		USB_Dbg_Trace(USB_OTG_STATE_FROM_B_WAIT_ACON_TO_B_HOST, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_B_HOST;
	}
	/*
	** While waiting in the b_wait_acon state, the B-device
	** may detect a K state on the bus. This indicates that the
	** A-device is signaling a resume condition and is retaining
	** control of the bus. In this case, the B-device will return
	** to the b_peripheral state.
	*/
	/*
	** If the B-device at any time detects more than 3.125 ms of SE0
	** (TB_ASE0_BRST min), then this is an indication that the
	** A-device is remaining Host and is resetting the bus. In this
	** case the B-device shall return to the b_peripheral state
	** and start to process the bus reset before TB_ASE0_BRST max.
	*/
	/*
	** b_detect_a_resume will be set by the host ISR when K state is
	** detected  if (b_detect_a_resume || b_ase0_brst_tmr)
	*/
	else if (OTG_Is_TimerOut(OTG_TIMER_B_CHECK_A_RESUME) == KAL_TRUE)
	{
		/* Stop the timer */
		OTG_Stop_Timer();

		// Start timer to wait A to connect
		OTG_Start_Timer(OTG_TIMER_B_ASE0_BRST, TB_ASE0_BRST);
      	}
	else if ((g_OtgInfo.b_detect_a_resume == KAL_TRUE)||(g_OtgInfo.b_detect_a_reset == KAL_TRUE)||(OTG_Is_TimerOut(OTG_TIMER_B_ASE0_BRST) == KAL_TRUE))
	{
		/* A suspend and then resume */
		g_OtgInfo.b_detect_a_resume = KAL_FALSE;
		/* A suspend and then reset */
		g_OtgInfo.b_detect_a_reset = KAL_FALSE;
		g_OtgInfo.b_hnp_enable = KAL_FALSE;

		/* HNP fail,  this is caused by A-device does not pull up D+ */
		/***********************************************************
		OPT test 5.8 and 5.9 require that we must display a message
		when a HNP fails. This event should allow the application
		to know  that HNP failed with Host.
		***********************************************************/
		if(OTG_Is_TimerOut(OTG_TIMER_B_ASE0_BRST) == KAL_TRUE)
		{
			/* time out */
			/* OTG_B_HNP_Fail_Hdlr(void) */
			/* OTG_Display_Message(OTG_DISPLAY_MSG_NO_RESPONSE); */
			g_OtgInfo.b_hnp_fail_hdlr();
	#ifndef __OTG_SRP_HNP_APP_FIXED__
			/* If cable quality is very bad, we try to use full speed */
			USB_DRV_ClearBits8(USB_POWER, USB_POWER_HSENAB);
	#endif
		}

		/* Stop the timer */
		OTG_Stop_Timer();

		/* Start up device */
		if((g_OtgInfo.host_up == KAL_FALSE)||(g_OtgInfo.device_up == KAL_TRUE))
			EXT_ASSERT(0, (kal_uint32)g_OtgInfo.host_up, (kal_uint32)g_OtgInfo.device_up, 0);

		g_OtgInfo.host_up = KAL_FALSE;
		g_OtgInfo.host_down_hdlr();

		g_OtgInfo.device_up = KAL_TRUE;

		if(g_OtgInfo.b_device_down_pending == KAL_TRUE)
		{
			g_OtgInfo.b_device_down_pending = KAL_FALSE;
			USB_Dbg_Trace(USB_OTG_B_DEVICE_PENDING_DOWN_HDLR, 0, 3);
		}
		else
		{
			g_OtgInfo.device_up_hdlr();
		}

		/* Clear this bit make DP re-connect */
		USB_DRV_ClearBits8(USB_DEVCTL, USB_DEVCTL_HOSTREQ);

		USB_Dbg_Trace(USB_OTG_STATE_FROM_B_WAIT_ACON_TO_B_PERIPHERAL, 0, 0);
		g_OtgInfo.otg_state = OTG_STATE_B_PERIPHERAL;
	}
}


static void OTG_Process_B_Host(void)
{
	/* HNP phase 2, B transfer from host to peripheral, or A peripheral want to disconnect */
	if((g_OtgInfo.b_suspend_req == KAL_TRUE)||(g_OtgInfo.b_detect_a_conn == KAL_FALSE))
	{
		/* Start up device */
		if((g_OtgInfo.host_up == KAL_FALSE)||(g_OtgInfo.device_up == KAL_TRUE))
			EXT_ASSERT(0, (kal_uint32)g_OtgInfo.host_up, (kal_uint32)g_OtgInfo.device_up, 0);

		g_OtgInfo.host_up = KAL_FALSE;
		g_OtgInfo.host_down_hdlr();

		/* Set suspend */
		if(g_OtgInfo.b_suspend_req == KAL_TRUE)
		{
			USB_DRV_SetBits8(USB_POWER, USB_POWER_SUSPENDMODE);
		}

		g_OtgInfo.device_up = KAL_TRUE;
		g_OtgInfo.device_up_hdlr();

		USB_Dbg_Trace(USB_OTG_STATE_FROM_B_HOST_TO_B_PERIPHERAL, USB_DRV_Reg8(USB_POWER), 3);
		g_OtgInfo.otg_state = OTG_STATE_B_PERIPHERAL;
	}
}





static void OTG_State_Machine(void)
{
	static kal_bool	b_processing = KAL_FALSE;


	if(b_processing == KAL_TRUE)
		ASSERT(0);

	b_processing = KAL_TRUE;

	switch(g_OtgInfo.otg_state)
	{
	case OTG_STATE_START:
		USB_Dbg_Trace(USB_OTG_STATE_START, 0, 0);
		if(OTG_Get_Plug_Type() == OTG_PLUG_B)
		{
			g_OtgInfo.otg_state = OTG_STATE_B_IDLE;
			OTG_Process_B_Idle();
 		}
 		else
		{
			/* ID is false when A device is identified */
	#ifdef OTG_A_NOT_INSERTION_BASE
			/* for SRP compliance test */
			/* When mini a plug in, turn on VBUS only after SPR */
			g_OtgInfo.a_bus_req = KAL_FALSE;
	#else
			g_OtgInfo.a_bus_req = KAL_TRUE;
	#endif
			g_OtgInfo.otg_state = OTG_STATE_A_IDLE;
			OTG_Process_A_Idle();
		}
		break;
	case OTG_STATE_A_IDLE:
		USB_Dbg_Trace(USB_OTG_STATE_A_IDLE, 0, 0);
		OTG_Process_A_Idle();
		break;
	case OTG_STATE_A_WAIT_BCON:
		USB_Dbg_Trace(USB_OTG_STATE_A_WAIT_BCON, (kal_uint32)g_OtgInfo.a_detect_b_conn, 0);
		OTG_Process_A_Wait_BCon();
		break;
	case OTG_STATE_A_HOST:
		USB_Dbg_Trace(USB_OTG_STATE_A_HOST, 0, 0);
		OTG_Process_A_Host();
		break;
	case OTG_STATE_A_SUSPEND:
		USB_Dbg_Trace(USB_OTG_STATE_A_SUSPEND, 0, 0);
		OTG_Process_A_Suspend();
		break;
	case OTG_STATE_A_PERIPHERAL:
		USB_Dbg_Trace(USB_OTG_STATE_A_PERIPHERALE, (kal_uint32)g_OtgInfo.a_detect_b_suspend, 0);
		OTG_Process_A_Peripheral();
		break;
	case OTG_STATE_A_WAIT_VFALL:
		USB_Dbg_Trace(USB_OTG_STATE_A_WAIT_VFALL, 0, 0);
		OTG_Process_A_Wait_VFall();
		break;
	case OTG_STATE_B_IDLE:
		USB_Dbg_Trace(USB_OTG_STATE_B_IDLE, 0, 0);
		OTG_Process_B_Idle();
		break;
	case OTG_STATE_B_SRP_INIT:
		USB_Dbg_Trace(USB_OTG_STATE_B_SRP_INIT, 0, 0);
		OTG_Process_B_SRP_Init();
		break;
	case OTG_STATE_B_PERIPHERAL:
		USB_Dbg_Trace(USB_OTG_STATE_B_PERIPHERAL, (kal_uint32)g_OtgInfo.b_hnp_enable, (kal_uint32)g_OtgInfo.b_detect_a_suspend);
		OTG_Process_B_Peripheral();
		break;
	case OTG_STATE_B_WAIT_ACON:
		USB_Dbg_Trace(USB_OTG_STATE_B_WAIT_ACON, (kal_uint32)g_OtgInfo.b_detect_a_conn, 0);
		OTG_Process_B_Wait_ACon();
		break;
	case OTG_STATE_B_HOST:
		USB_Dbg_Trace(USB_OTG_STATE_B_HOST, (kal_uint32)g_OtgInfo.b_suspend_req, (kal_uint32)g_OtgInfo.b_detect_a_conn);
		OTG_Process_B_Host();
		break;
	default:
		ASSERT(0);
		break;
	}

	b_processing = KAL_FALSE;
}



/************************************************************
	HISR/LISR   interrupt handler
*************************************************************/

#if defined(DRV_USB_IP_V3)

static void OTGDMA_HISR(void)
{
	kal_uint8  	DMAIntr;
	kal_uint32  	index;


	DMAIntr = USB_DRV_Reg8(USB_DMAINTR);

	while(DMAIntr != 0)
	{
		for(index = 0; index < MAX_DMA_NUM; index++)
		{
			if((DMAIntr&(1<<index)) != 0)
			{
				// write 0 clear first, then do call back function, order is very important !!!
				USB_DRV_WriteReg8(USB_DMAINTR, ~(0x01<<index));

				if ((USB_HCD_DMA_Get_Run_Status(0, index+1) == KAL_TRUE)||(g_UsbDrvInfo.dma_running[index] == KAL_TRUE))
				{	
					// cannot be host & device at the same time
					if ((USB_HCD_DMA_Get_Run_Status(0, index+1) == KAL_TRUE)&&(g_UsbDrvInfo.dma_running[index] == KAL_TRUE))
						ASSERT(0);
					
					/* DMA channel index+1 have interrupt */
					if(USB_DRV_Reg32(USB_DMA_REALCNT(index+1)) != 0)
					{
						if(USB_HCD_DMA_Get_Run_Status(0, index+1) == KAL_TRUE)
						{
							USB_HCD_DMA_Callback(0, index+1);
						}
						else if(g_UsbDrvInfo.dma_running[index] == KAL_TRUE)
						{
							USB_DMA_Callback(index+1);
						}
					}
					else
					{
						if(USB_Check_DMA_Time_Out(index+1) == KAL_TRUE)
						{
							if(USB_HCD_DMA_Get_Run_Status(0, index+1) == KAL_TRUE)
							{
								USB_HCD_DMA_Callback(0, index+1);
							}
							else if(g_UsbDrvInfo.dma_running[index] == KAL_TRUE)
							{
								USB_DMA_Callback(index+1);
							}
						}
						else
						{						
							drv_trace4(TRACE_FUNC, (kal_uint32)USB_OTG_DMA_STATUS, USB_DRV_Reg32(USB_DMA_REALCNT(index+1)),USB_DRV_Reg32(USB_DMACNT(index+1)), USB_HCD_DMA_Get_Run_Status(0, index+1), g_UsbDrvInfo.dma_running[index]);
//							ASSERT(0);
						}
						// DMA timer time out
						
	/*
						if(index == 0)
						{
							// set address
							USB_DMA_Callback(index+1);
						}
						else
						{
							USB_DRV_SetBits(USB_DMACNTL(index+1), USB_DMACNTL_DMAEN);
						}
	*/
					}
				}
			}
		}

		/* Read register again to see if any interrupt is generated again */
		/* if ping pong buffer serve done, then we do call back function here */
		DMAIntr = USB_DRV_Reg8(USB_DMAINTR);
	}

	IRQClearInt(IRQ_USBDMA_CODE);
	IRQUnmask(IRQ_USBDMA_CODE);
}


static void OTGDMA_LISR(void)
{
	kal_uint8  DMAIntr;


	IRQMask(IRQ_USBDMA_CODE);
	drv_active_hisr(DRV_USBDMA_HISR_ID);
	DMAIntr = USB_DRV_Reg8(USB_DMAINTR);
	USB_Dbg_Trace(USB_DMA_LISR, DMAIntr, 0);
	ASSERT(DMAIntr != 0);
}

#endif


void OTG_Set_Intr()
{
	volatile kal_uint8	IntrUSB;
	
	IntrUSB = USB_DRV_Reg8(USB_INTRUSB);
	g_OtgInfo.otg_intr_status = IntrUSB;
}

void OTG_HISR(void)
{
	kal_bool	b_dev_unmask_irq = KAL_TRUE;
#if defined(DRV_USB_IP_V3)
	volatile kal_uint8	IntrUSB;
#ifdef __MEUT__		
	kal_bool b_otg_hisr_is_handled = KAL_FALSE;
#endif

	IntrUSB = USB_DRV_Reg8(USB_INTRUSB);
	g_OtgInfo.otg_intr_status = IntrUSB;
	USB_Dbg_Trace(USB_OTG_HISR, IntrUSB, USB_DRV_Reg8(USB_POWER));

#ifdef __MEUT__	
	if ((IntrUSB == 0)&&(g_OtgInfo.host_up == KAL_FALSE)&&(g_OtgInfo.device_up == KAL_FALSE))
	{
		if ((USB_DRV_Reg(USB_INTRTX)== 0)&&(USB_DRV_Reg(USB_INTRRX)== 0))
			b_otg_hisr_is_handled = KAL_TRUE;	
	}	

	if(IntrUSB != 0)
	{
		b_otg_hisr_is_handled = KAL_TRUE;
	}
#endif

	if((IntrUSB&USB_INTRUSB_RESET) != 0)
	{
		if(OTG_Get_State() == OTG_STATE_B_WAIT_ACON)
		{
			USB_Dbg_Trace(USB_OTG_RESET, 0, 0);
			OTG_Set_Status(OTG_STATUS_B_DETECT_A_RESET, KAL_TRUE);
		}
	}

	if((IntrUSB&USB_INTRUSB_RESUME) != 0)
	{
		if((g_OtgInfo.device_up == KAL_FALSE)&&(g_OtgInfo.otg_state == OTG_STATE_B_WAIT_ACON))
		{
			USB_Dbg_Trace(USB_OTG_RESUME, 0, 0);
			OTG_Set_Status(OTG_STATUS_B_DETECT_A_RESUME, KAL_TRUE);
		}
	}

	/* SRP detection, Only A-device */
	if((IntrUSB&USB_INTRUSB_SESSREQ) != 0)
	{
		USB_Dbg_Trace(USB_OTG_SESSREQ, USB_DRV_Reg8(USB_OPSTATE), USB_DRV_Reg8(USB_DEVCTL));
		drv_trace4(TRACE_FUNC, (kal_uint32)USB_OTG_SESSION_REQUEST, IntrUSB, USB_DRV_Reg8(USB_POWER), USB_DRV_Reg8(USB_LINE_STATE), USB_DRV_Reg8(USB_OPSTATE));

		if((OTG_Get_Plug_Type() == OTG_PLUG_A)&&(g_OtgInfo.otg_state != OTG_STATE_A_WAIT_VFALL))
		{ //receive un-expected interrupt after Vbus error
			OTG_Set_Status(OTG_STATUS_A_DETECT_B_DATA_PLS, KAL_TRUE);
		}
		else
		{
			if(USB_DRV_Reg8(USB_OPSTATE) != 0x00)
				ASSERT(0);
			//receive babble interrupt will force HW jump to idle state (0x00)--> cannot receive session interrupt.
		}
	}

	/* VBUS error, A device only */
	if((IntrUSB&USB_INTRUSB_VBUSERROR) != 0)
	{
		if(OTG_Get_Plug_Type() == OTG_PLUG_A)
		{
			drv_trace0(TRACE_FUNC, (kal_uint32)USB_OTG_VBUS_FALL_DOWN);
		
			/* IP detect every 543ms, Vbus is still lower than 4.4V it will issue this intr and auto turn off Vbus */
			OTG_Process_A_Vbus_Fail();
		}
		else
		{
			ASSERT(0);
		}
	}

	if((IntrUSB&USB_INTRUSB_CONN) != 0)
	{

	}

	if(g_OtgInfo.host_up == KAL_TRUE)
	{
		/* set TRUE in A_WAIT_BCON */
		/* set TRUE in B_WAIT_ACON */
		/* set FALSE in OTG_STATE_A_PERIPHERAL */
		/* set FALSE in OTG_STATE_B_PERIPHERAL */
		/* set FALSE in B_IDLE */
		/* set FALSE in OTG_STATE_A_WAIT_VFALL */
		/* Host mode */
		USB_HCD_HISR();
		b_dev_unmask_irq = USB_HCD_Get_UnMask_Irq(0);
		
#ifdef __MEUT__			
		b_otg_hisr_is_handled = KAL_TRUE;
#endif

	#ifdef __OTG_SRP_HNP_APP_FIXED__
		/* Mask USB IRQ to prevent host reset too fast, even USB device does not become MS yet */
		/* From A suspend to A peripheral */
		if(g_OtgInfo.device_up == KAL_TRUE)
		{
			b_dev_unmask_irq = USB_HW_Get_UnMask_Irq();
		}
	#endif
	}
	else if(g_OtgInfo.device_up == KAL_TRUE)
	{
		/* set TRUE in OTG_STATE_A_PERIPHERAL */
		/* set TRUE in OTG_STATE_B_PERIPHERAL */
		/* set FALSE in OTG_STATE_A_WAIT_VFALL */
		/* set FALSE in OTG_STATE_A_WAIT_VRISE & A_IDLE */
		/* set FALSE in OTG_STATE_A_WAIT_BCON */
		/* set FALSE in OTG_STATE_B_WAIT_ACON */
		/* set FALSE in OTG_STATE_B_IDLE */
		/* Device mode */
		USB_HISR();
		b_dev_unmask_irq = USB_HW_Get_UnMask_Irq();
		
#ifdef __MEUT__			
		b_otg_hisr_is_handled = KAL_TRUE;
#endif		
	}

	if((IntrUSB&USB_INTRUSB_DISCON) != 0)
	{

	}

	if((IntrUSB&USB_INTRUSB_SUSPEND) != 0)
	{

	}

	//debug used
	if((IntrUSB&USB_INTRUSB_SOF) != 0)
	{
//		USB_Dbg_Trace(USB_OTG_SOF, USB_DRV_Reg8(USB_LINE_STATE), USB_DRV_Reg8(USB_OPSTATE));
//		USB_Dbg_Trace(USB_OTG_SOF, USB_DRV_Reg8(USB_DEVCTL), USB_DRV_Reg8(USB_PHYIR2_0));
	}

#ifdef __MEUT__			
	if((b_otg_hisr_is_handled == KAL_FALSE) && (INT_Exception_Enter ==0))
		EXT_ASSERT(0, USB_DRV_Reg(USB_INTRTX), USB_DRV_Reg(USB_INTRRX), 0);
#endif

#endif

	/* Clear interrupt and unmask interrupt */
   	IRQClearInt(IRQ_USB_CODE);
   	g_OtgInfo.is_ProOTGHISR = KAL_FALSE;

	if((g_OtgInfo.b_unmask_irq == KAL_TRUE)&&(b_dev_unmask_irq == KAL_TRUE))
	{
		IRQUnmask(IRQ_USB_CODE);
	}
	else
	{
		USB_Dbg_Trace(USB_OTG_MASK_HISR, (kal_uint32)g_OtgInfo.b_unmask_irq, (kal_uint32)b_dev_unmask_irq);
	}
}


static void OTG_LISR(void)
{
	IRQMask(IRQ_USB_CODE);
	g_OtgInfo.is_ProOTGHISR = KAL_TRUE;
	drv_active_hisr(DRV_USB_HISR_ID);
}



/* Global variables */
#define DCL_OTG_DRV_HANDLE  	0x00004000
#define DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) ((handle)&DCL_OTG_DRV_HANDLE)


/* static variables */
static kal_bool g_OTG_Drv_Init = KAL_FALSE;
static kal_bool g_OTG_Drv_Open = KAL_FALSE;



/*************************************************************************
* FUNCTION
*  DclOTG_DRV_Initialize
*
* DESCRIPTION
*  This function is to initialize OTG module
*  It should be called before user register USB EINT handler and only called once
*
* PARAMETERS
*  None
*
* RETURNS
*  STATUS_OK : command is executed successfully.
*  STATUS_INVALID_OPERATION : already initialized.
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_Initialize(void)
{
	if(g_OTG_Drv_Init == KAL_TRUE)
	{
		/* Already initialized */
		return STATUS_INVALID_OPERATION;
	}
	else
	{
		g_OTG_Drv_Init = KAL_TRUE;
//		USB_Initialize_Drv_Phase1();
		return STATUS_OK;
	}
}


/*************************************************************************
* FUNCTION
*  DclOTG_DRV_Open
*
* DESCRIPTION
*  This function is to open the OTG module and return a valid handle
*
* PARAMETERS
*  dev: only valid for DCL_USB
*  flags: no sepcial flags is needed. Please use FLAGS_NONE
*
* RETURNS
*  DCL_HANDLE_INVALID: Open failed
*  DCL_HANDLE_OCCUPIED: Already opened
*  Other value: A valid handle
*
*************************************************************************/
DCL_HANDLE DclOTG_DRV_Open(DCL_DEV dev, DCL_FLAGS flags)
{
	if(dev != DCL_USB)
	{
		ASSERT(0);
		return (DCL_HANDLE)DCL_HANDLE_INVALID;
	}
	else
	{
		g_OTG_Drv_Open = KAL_TRUE;
		return (DCL_HANDLE)DCL_OTG_DRV_HANDLE;
	}
}

/*************************************************************************
* FUNCTION
*  DclOTG_DRV_Close
*
* DESCRIPTION
*  This function is to close the OTG module.
*
* PARAMETERS
*  handle: the returned handle value of DclOTG_DRV_Open
*
* RETURNS
*  STATUS_INVALID_OPERATION: OTG module does not open yet, but user want to close
*  STATUS_INVALID_DCL_HANDLE: Use invalid handle to close
*  STATUS_OK
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_Close(DCL_HANDLE handle)
{
	/* Check open or not and handle validity */
	
	if(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0)
	{
		ASSERT(0);

//		if(g_OTG_Drv_Open == KAL_FALSE)
//			return STATUS_INVALID_OPERATION;
//		else
//			return STATUS_INVALID_DCL_HANDLE;
	}

//	g_OTG_Drv_Open = KAL_FALSE;

	return STATUS_OK;
}


/*************************************************************************
* FUNCTION
*  DclOTG_DRV_ReadData
*
* DESCRIPTION
*  This function is not supported for the OTG module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_ReadData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	/* Check open or not and handle validity */
/*	
	if((g_OTG_Drv_Open == KAL_FALSE)||(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0))
	{
		ASSERT(0);

		if(g_OTG_Drv_Open == KAL_FALSE)
			return STATUS_INVALID_OPERATION;
		else
			return STATUS_INVALID_DCL_HANDLE;
	}
*/
	return STATUS_UNSUPPORTED;
}



/*************************************************************************
* FUNCTION
*  DclOTG_DRV_WriteData
*
* DESCRIPTION
*  This function is not supported for the OTG module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_WriteData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	/* Check open or not and handle validity */
/*	
	if((g_OTG_Drv_Open == KAL_FALSE)||(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0))
	{
		ASSERT(0);

		if(g_OTG_Drv_Open == KAL_FALSE)
			return STATUS_INVALID_OPERATION;
		else
			return STATUS_INVALID_DCL_HANDLE;
	}
*/
	return STATUS_UNSUPPORTED;
}



/*************************************************************************
* FUNCTION
*  DclOTG_DRV_Configure
*
* DESCRIPTION
*  This function is not supported for the OTG module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_Configure(DCL_HANDLE handle, DCL_CONFIGURE_T *configure)
{
	/* Check open or not and handle validity */
/*	
	if((g_OTG_Drv_Open == KAL_FALSE)||(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0))
	{
		ASSERT(0);

		if(g_OTG_Drv_Open == KAL_FALSE)
			return STATUS_INVALID_OPERATION;
		else
			return STATUS_INVALID_DCL_HANDLE;
	}
*/
	return STATUS_UNSUPPORTED;
}



/*************************************************************************
* FUNCTION
*  DclOTG_DRV_RegisterCallback
*
* DESCRIPTION
*  This function is not supported for the OTG module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_RegisterCallback(DCL_HANDLE handle, DCL_EVENT event, PFN_DCL_CALLBACK callback)
{
	/* Check open or not and handle validity */
/*	
	if((g_OTG_Drv_Open == KAL_FALSE)||(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0))
	{
		ASSERT(0);

		if(g_OTG_Drv_Open == KAL_FALSE)
			return STATUS_INVALID_OPERATION;
		else
			return STATUS_INVALID_DCL_HANDLE;
	}
*/
	return STATUS_UNSUPPORTED;
}



/*******************************************************************************
	Control functions for OTG_DRV
 *******************************************************************************/
static DCL_STATUS DCL_USB_CTRL_API_OTG_Init_Drv_Info(DCL_CTRL_DATA_T *data)
{
	g_OtgInfo.otg_state = OTG_STATE_START;

	g_OtgInfo.host_up_hdlr = NULL;
	g_OtgInfo.host_down_hdlr = NULL;
	g_OtgInfo.device_up_hdlr = NULL;
	g_OtgInfo.device_down_hdlr = NULL;
	g_OtgInfo.a_host_stop_hdlr = NULL;
	g_OtgInfo.b_hnp_fail_hdlr = NULL;
	g_OtgInfo.a_vrise_fail_hdlr = NULL;
	g_OtgInfo.b_srp_fail_hdlr = NULL;
	g_OtgInfo.b_srp_stop_hdlr = NULL;

	g_OtgInfo.a_bus_req = KAL_TRUE;
	g_OtgInfo.a_detect_b_conn = KAL_FALSE;
	g_OtgInfo.a_detect_b_suspend = KAL_FALSE;
	g_OtgInfo.a_detect_b_resume = KAL_FALSE;
	g_OtgInfo.a_suspend_req = KAL_FALSE;
	g_OtgInfo.a_resume_req = KAL_FALSE;
	g_OtgInfo.a_set_b_hnp_enable = KAL_FALSE;

	g_OtgInfo.b_suspend_req = KAL_FALSE;
	g_OtgInfo.b_detect_a_conn = KAL_FALSE;
	g_OtgInfo.b_detect_a_suspend = KAL_FALSE;
	g_OtgInfo.b_detect_a_resume = KAL_FALSE;
	g_OtgInfo.b_srp_request = KAL_FALSE;
	g_OtgInfo.b_hnp_enable = KAL_FALSE;


	g_OtgInfo.suspend_and_conn_intr = KAL_FALSE;
	g_OtgInfo.disconn_num = 0;

	g_OtgInfo.timer_on = KAL_FALSE;
	g_OtgInfo.timer_out = KAL_FALSE;
	g_OtgInfo.timer_type = OTG_TIMER_NONE;

	g_OtgInfo.device_up = KAL_FALSE;
	g_OtgInfo.host_up = KAL_FALSE;

	g_OtgInfo.b_processing = KAL_FALSE;

	g_OtgInfo.b_unmask_irq = KAL_TRUE;
	g_OtgInfo.is_ProOTGHISR = KAL_FALSE;

	if ((g_OtgInfo.gpt_handle == 0)||(g_OtgInfo.gpt_handle == 0x7F))
		g_OtgInfo.gpt_handle = DclSGPT_Open(DCL_GPT_CB, 0);
//		GPTI_GetHandle(&g_OtgInfo.gpt_handle);

	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Release_Drv_Info(DCL_CTRL_DATA_T *data)
{
	DCL_HANDLE handle;
	g_OtgInfo.otg_state = OTG_STATE_START;

	g_OtgInfo.host_up_hdlr = NULL;
	g_OtgInfo.host_down_hdlr = NULL;
	g_OtgInfo.device_up_hdlr = NULL;
	g_OtgInfo.device_down_hdlr = NULL;
	g_OtgInfo.a_host_stop_hdlr = NULL;
	g_OtgInfo.b_hnp_fail_hdlr = NULL;
	g_OtgInfo.a_vrise_fail_hdlr = NULL;
	g_OtgInfo.b_srp_fail_hdlr = NULL;
	g_OtgInfo.b_srp_stop_hdlr = NULL;

	g_OtgInfo.a_bus_req = KAL_TRUE;
	g_OtgInfo.a_detect_b_conn = KAL_FALSE;
	g_OtgInfo.a_detect_b_suspend = KAL_FALSE;
	g_OtgInfo.a_detect_b_resume = KAL_FALSE;
	g_OtgInfo.a_suspend_req = KAL_FALSE;
	g_OtgInfo.a_resume_req = KAL_FALSE;
	g_OtgInfo.a_set_b_hnp_enable = KAL_FALSE;

	g_OtgInfo.b_suspend_req = KAL_FALSE;
	g_OtgInfo.b_detect_a_conn = KAL_FALSE;
	g_OtgInfo.b_detect_a_suspend = KAL_FALSE;
	g_OtgInfo.b_detect_a_resume = KAL_FALSE;
	g_OtgInfo.b_srp_request = KAL_FALSE;
	g_OtgInfo.b_hnp_enable = KAL_FALSE;

	g_OtgInfo.timer_on = KAL_FALSE;
	g_OtgInfo.timer_out = KAL_FALSE;
	g_OtgInfo.timer_type = OTG_TIMER_NONE;

	g_OtgInfo.device_up = KAL_FALSE;
	g_OtgInfo.host_up = KAL_FALSE;

	g_OtgInfo.b_processing = KAL_FALSE;

	g_OtgInfo.b_unmask_irq = KAL_TRUE;
	g_OtgInfo.is_ProOTGHISR = KAL_FALSE;

	if((g_OtgInfo.gpt_handle != 0)&&(g_OtgInfo.gpt_handle != 0x7F))
	{	
		 	handle = (DCL_HANDLE)g_OtgInfo.gpt_handle;
			g_OtgInfo.gpt_handle = DclSGPT_Close(&handle);
	}
//		GPTI_ReleaseHandle(&g_OtgInfo.gpt_handle);

	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Register_Drv_Info(DCL_CTRL_DATA_T *data)
{
	OTG_DRV_CTRL_REGISTER_DRV_INFO_T *prregister_drv_info;
	OTG_DRV_HDLR_TYPE type;
	otg_intr_handler_ptr hdlr;


	prregister_drv_info = &data->rOTG_Drv_Info;
	type = (OTG_DRV_HDLR_TYPE)prregister_drv_info->type;
	hdlr = (otg_intr_handler_ptr)prregister_drv_info->hdlr;

	switch(type)
	{
	case OTG_DRV_HDLR_HOST_UP:
		g_OtgInfo.host_up_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_HOST_DOWN:
		g_OtgInfo.host_down_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_DEVICE_UP:
		g_OtgInfo.device_up_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_DEVICE_DOWN:
		g_OtgInfo.device_down_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_A_VRISE_FAIL:
		g_OtgInfo.a_vrise_fail_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_A_HNP_FAIL:
		g_OtgInfo.a_hnp_fail_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_A_HNP_STOP:
		g_OtgInfo.a_hnp_stop_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_A_NO_RESPONSE:
		g_OtgInfo.a_no_response_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_B_HNP_FAIL:
		g_OtgInfo.b_hnp_fail_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_B_SRP_FAIL:
		g_OtgInfo.b_srp_fail_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_B_SRP_STOP:
		g_OtgInfo.b_srp_stop_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_B_SRP_EXCEPTION_STOP:
		g_OtgInfo.b_srp_exception_stop_hdlr = hdlr;
		break;
	case OTG_DRV_HDLR_A_HOST_STOP:
		g_OtgInfo.a_host_stop_hdlr = hdlr;
		break;
	default:
		ASSERT(0);
		break;
	}


	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Drv_Create_ISR(DCL_CTRL_DATA_T *data)
{
	IRQ_Register_LISR(IRQ_USB_CODE, OTG_LISR, "OTG");
	DRV_Register_HISR(DRV_USB_HISR_ID, OTG_HISR);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Drv_Activate_ISR(DCL_CTRL_DATA_T *data)
{
	g_OtgInfo.b_unmask_irq = KAL_TRUE;
	IRQSensitivity(IRQ_USB_CODE,LEVEL_SENSITIVE);
	IRQUnmask(IRQ_USB_CODE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTGDMA_Drv_Create_ISR(DCL_CTRL_DATA_T *data)
{
#if defined(DRV_USB_IP_V3)
	IRQ_Register_LISR(IRQ_USBDMA_CODE, OTGDMA_LISR, "OTGDMA");
	DRV_Register_HISR(DRV_USBDMA_HISR_ID, OTGDMA_HISR);
	return STATUS_OK;
#else
	return STATUS_INVALID_CMD;
#endif
}


static DCL_STATUS DCL_USB_CTRL_API_OTGDMA_Drv_Activate_ISR(DCL_CTRL_DATA_T *data)
{
#if defined(DRV_USB_IP_V3)
	IRQSensitivity(IRQ_USBDMA_CODE, LEVEL_SENSITIVE);
	IRQUnmask(IRQ_USBDMA_CODE);
	return STATUS_OK;
#else
	return STATUS_INVALID_CMD;
#endif
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Init_Drv(DCL_CTRL_DATA_T *data)
{
	kal_bool b_process_state_machine = KAL_FALSE;
	kal_uint32 savedMask;

#if defined(DRV_USB_IP_V3)
	/* Software reset */
	USB_DRV_WriteReg(USB_SWRST, (USB_SWRST_DISUSBRESET|USB_SWRST_SWRST));
//	USB_DRV_WriteReg8(USB_RSTINFO, 0xA6);
	USB_DRV_WriteReg8(USB_RSTINFO, 0xA0);

	/* Set USB_POWER_ISOUPDATE will make ISO pipe with DMA abnormally */
//	USB_DRV_SetBits8(USB_POWER, (USB_POWER_ISOUPDATE|USB_POWER_ENABLESUSPENDM));
	USB_DRV_SetBits8(USB_POWER, (USB_POWER_ENABLESUSPENDM));

#ifdef __USB_HS_ENABLE__
	USB_DRV_SetBits8(USB_POWER, USB_POWER_HSENAB);
#else
	USB_DRV_ClearBits8(USB_POWER, USB_POWER_HSENAB);
#endif

	/* SRP Vbus pulsing duration */
	USB_DRV_WriteReg8(USB_VPLEN, 0x02);

#if (defined(DRV_USB_PHY_COST_DOWN))
	USB_DRV_WriteReg8(USB_PHYIR4_3, USB_PHYIR4_3_DEGLICH);
#elif (defined(DRV_USB_PHY_U65_IP))

#elif (defined(DRV_USB_PHY_U40_IP))

#else    //Old PHY version
	/* Change phy register setting */
	USB_DRV_WriteReg8(USB_PHYCR3_3, 0x67);
	USB_DRV_WriteReg8(USB_PHYCR2_0, 0x00);
#endif

	savedMask = SaveAndSetIRQMask();
	if(g_OtgInfo.b_processing == KAL_FALSE)
	{
		g_OtgInfo.b_processing = KAL_TRUE;
		b_process_state_machine = KAL_TRUE;
	}
	RestoreIRQMask(savedMask);

	if(b_process_state_machine == KAL_TRUE)
	{
		OTG_State_Machine();
		g_OtgInfo.b_processing = KAL_FALSE;
	}
#endif

	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Get_Plug_Type(DCL_CTRL_DATA_T *data)
{
	OTG_DRV_CTRL_GET_PLUG_TYPE_T *prget_plug_type;


	prget_plug_type = &data->rOTG_Plug_Type;
	prget_plug_type->type = (DCL_OTG_PLUG_TYPE)g_OtgInfo.plug_type;
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_A_Suspend_Req(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_A_HOST)
		return STATUS_FAIL;

	/* suspend only effect when A_HOST state */
	OTG_Set_Status(OTG_STATUS_A_SUSPEND_REQUEST, KAL_TRUE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_A_Resume_Req(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_A_SUSPEND)
		return STATUS_FAIL;

	OTG_Set_Status(OTG_STATUS_A_RESUME_REQUEST, KAL_TRUE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_A_Stop_Host(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_A_HOST)
		return STATUS_FAIL;

	OTG_Set_Status(OTG_STATUS_A_BUS_REQUEST, KAL_FALSE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_A_Start_HNP(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_A_HOST)
		return STATUS_FAIL;

	OTG_Set_Status(OTG_STATUS_A_SET_B_HNP_ENABLE, KAL_TRUE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_B_EnDis_HNP(DCL_CTRL_DATA_T *data)
{
	DCL_BOOL *dcl_data_ptr = (DCL_BOOL*)data;
	kal_bool set = (kal_bool)(*dcl_data_ptr);


	if(g_OtgInfo.otg_state != OTG_STATE_B_PERIPHERAL)
		return STATUS_FAIL;


	if(set == KAL_TRUE)
	{
		USB_DRV_SetBits8(USB_DEVCTL, USB_DEVCTL_HOSTREQ);
	}
	else
	{
		USB_DRV_ClearBits8(USB_DEVCTL, USB_DEVCTL_HOSTREQ);
	}

	OTG_Set_Status(OTG_STATUS_B_HNP_ENABLE, set);

	return STATUS_OK;
}



static DCL_STATUS DCL_USB_CTRL_API_OTG_B_Start_SRP(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_B_IDLE)
		return STATUS_FAIL;

	OTG_Set_Status(OTG_STATUS_B_SRP_REQUEST, KAL_TRUE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_B_Stop_Host(DCL_CTRL_DATA_T *data)
{
	if(g_OtgInfo.otg_state != OTG_STATE_B_HOST)
		return STATUS_FAIL;

	OTG_Set_Status(OTG_STATUS_B_SUSPEND_REQUEST, KAL_TRUE);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_B_Set_Session_Valid(DCL_CTRL_DATA_T *data)
{
	DCL_BOOL *dcl_data_ptr = (DCL_BOOL*)data;
	kal_bool set = (kal_bool)(*dcl_data_ptr);


	OTG_Set_Status(OTG_STATUS_B_SESSION_VALID, set);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Get_State(DCL_CTRL_DATA_T *data)
{
	OTG_DRV_CTRL_GET_STATE_T *prget_state;


	prget_state = &data->rOTG_Get_State;
	prget_state->state = (DCL_OTG_STATE)OTG_Get_State();
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Enable_Device(DCL_CTRL_DATA_T *data)
{
	USB_DRV_SetBits8(USB_POWER, USB_POWER_SOFTCONN);
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Incr_Disconn_Count(DCL_CTRL_DATA_T *data)
{
	g_OtgInfo.disconn_num++;

	/* If cable quality is very bad, we try to use full speed */
	if(g_OtgInfo.disconn_num == 8)
	{
		USB_DRV_ClearBits8(USB_POWER, USB_POWER_HSENAB);
	}

	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Process_Exceptions(DCL_CTRL_DATA_T *data)
{
	USB_Dbg_Trace(USB_HOST_EXCEPTIONS, (kal_uint32)g_OtgInfo.otg_state, 0);

	if(kal_if_hisr() == KAL_TRUE)
		EXT_ASSERT(0, 0, 0, 0);

	/* Check exception for A device */
	if((g_OtgInfo.otg_state < OTG_STATE_B_IDLE))
	{
		/*
		** Exceptions are as follows.
		** 1. Transition to B_IDLE when id = TRUE.
		** 2. Transition to A_WAIT_VFALL when V_BUS is voltage is fallen below
		** 3. 4 VDC is not able to set the V_BUS voltage.
		*/

		/* For A device, only A cable plug out will go here */
		if(OTG_Get_Plug_Type() == OTG_PLUG_B)
		{
			/* 1. A plug out, EINT will set as OTG_PLUG_B  */
			/* stop any active timers*/
			OTG_Stop_Timer();

			/* state is in A_idle because b-device is un-supported and it does not support OTG, A device turn off Vbus */
			if(g_OtgInfo.otg_state == OTG_STATE_A_IDLE)
			{
				/* Only set g_OtgInfo.b_is_vbus_power_on = KAL_FALSE when A cable is plugged out */
			#ifdef __CHARGER_USB_DETECT_WIHT_ONE_EINT__
				g_OtgInfo.b_is_vbus_power_on = KAL_FALSE;
			#endif

				g_OtgInfo.otg_state = OTG_STATE_B_IDLE;

				if(g_OtgInfo.b_srp_request == KAL_TRUE)
				{
					/* A device should not g_OtgInfo.b_srp_request == KAL_TRUE */
					ASSERT(0);
					/* OTG_B_SRP_Stop_Hdlr(void) */
					/* OTG_Display_Message(OTG_DISPLAY_MSG_STOP_CONNECTING); */
//					g_OtgInfo.b_srp_stop_hdlr();
//					g_OtgInfo.b_srp_request = KAL_FALSE;
				}

				OTG_Process_B_Idle();
			}
			else if((g_OtgInfo.otg_state == OTG_STATE_A_WAIT_BCON)||(g_OtgInfo.otg_state == OTG_STATE_A_WAIT_VFALL)||(g_OtgInfo.otg_state == OTG_STATE_A_HOST))
			{
				/* 1. A_HOST receive disconn -> A_WAIT_BCON
				     2. A_PERIPHERAL receive suspend -> A_WAIT_BCON */
				OTG_Stop_Timer();

				if(g_OtgInfo.a_process_hnp == KAL_TRUE)
				{
					g_OtgInfo.a_hnp_stop_hdlr();
					g_OtgInfo.a_process_hnp = KAL_FALSE;
				}

				g_OtgInfo.otg_state = OTG_STATE_A_WAIT_VFALL;

				/* Turn off VBUS */
				USB_HCD_VBusEnable(0, KAL_FALSE);

				if(g_OtgInfo.host_up == KAL_TRUE)
				{
					g_OtgInfo.host_up = KAL_FALSE;
					g_OtgInfo.a_host_stop_hdlr();
					g_OtgInfo.host_down_hdlr();
				}

				if(g_OtgInfo.device_up == KAL_TRUE)
				{
					g_OtgInfo.device_up = KAL_FALSE;
					g_OtgInfo.device_down_hdlr();
				}

				OTG_Start_Timer(OTG_TIMER_A_CHECK_VFALL, TA_CHECK_VFALL);
				OTG_Process_A_Wait_VFall();
			}
			else
			{
				ASSERT(0);
			}
		}
	}
	else
	{
		/*
		** Process B- device exceptions
		*/
		/* mini B plug out, or B device do SRP but mini A plug in */
		if(((g_OtgInfo.b_session_valid == KAL_TRUE)||(g_OtgInfo.otg_state == OTG_STATE_B_SRP_INIT))&&(OTG_Get_Plug_Type() == OTG_PLUG_B))
			ASSERT(0);

		if(g_OtgInfo.b_srp_request == KAL_TRUE)
		{
			/* In case go here and SRP timer is still on */
			OTG_Stop_Timer();

			/* OTG_B_SRP_Exception_Stop_Hdlr(void) */
			/* OTG_Display_Message(OTG_DISPLAY_MSG_STOP_CONNECTING); */
			g_OtgInfo.b_srp_exception_stop_hdlr();
			g_OtgInfo.b_srp_request = KAL_FALSE;
		}

		if(OTG_Get_TimerType() ==  OTG_TIMER_B_ASE0_BRST)
			OTG_Stop_Timer();

		if(g_OtgInfo.b_device_down_pending == KAL_TRUE)
		{
			if(g_OtgInfo.device_up == KAL_TRUE)
				ASSERT(0);

			g_OtgInfo.b_device_down_pending = KAL_FALSE;
			g_OtgInfo.device_down_hdlr();
			USB_Dbg_Trace(USB_OTG_B_DEVICE_PENDING_DOWN_HDLR, 0, 4);
		}

		if(OTG_Get_Plug_Type() == OTG_PLUG_B)
		{
			/* B plug out */
			/* Must go to there to release type */
			g_OtgInfo.otg_state = OTG_STATE_B_IDLE;

			/* Reset B-device HNP enabled state */
			g_OtgInfo.b_hnp_enable = KAL_FALSE;
			USB_DRV_ClearBits8(USB_DEVCTL, USB_DEVCTL_HOSTREQ);

			OTG_Process_B_Idle();
		}
		else
		{
			/* A plug in */
			g_OtgInfo.otg_state = OTG_STATE_A_IDLE;
			g_OtgInfo.a_bus_req = KAL_TRUE;
			OTG_Process_A_Idle();
		}

/*
		if((OTG_Get_Plug_Type() == OTG_PLUG_A)||
			((g_OtgInfo.b_session_valid == KAL_FALSE)&&(g_OtgInfo.otg_state != OTG_STATE_B_SRP_INIT)))
		{
			if(g_OtgInfo.b_srp_request == KAL_TRUE)
			{
				g_OtgInfo.b_srp_stop_hdlr();
				g_OtgInfo.b_srp_request = KAL_FALSE;
			}

			if(OTG_Get_Plug_Type() == OTG_PLUG_B)
			{
				g_OtgInfo.otg_state = OTG_STATE_B_IDLE;
				OTG_Process_B_Idle();
			}
			else
{
				g_OtgInfo.otg_state = OTG_STATE_A_IDLE;
				g_OtgInfo.a_bus_req = KAL_TRUE;
				OTG_Process_A_Idle();
			}
		}
*/
	}
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Init_A_Plug_Detect(DCL_CTRL_DATA_T *data)
{
	kal_uint8 otg_idpin_eint = custom_eint_get_channel(otg_idpin_eint_chann);

	/* Set plug type as B before we register EINT */
	OTG_Set_Plug_Type(OTG_PLUG_B);

#if defined(__OLD_PDN_ARCH__)
#if defined(__OLD_PDN_DEFINE__)
	USB_DRV_WriteReg(DRVPDN_CON0_CLR, DRVPDN_CON0_USB);
#elif defined(__CLKG_DEFINE__)
	USB_DRV_WriteReg(CG_CLR0, CG_CON0_USB);
#endif
#else
	DRVPDN_Disable(PDN_USB);
#endif


#if defined(DRV_USB_PHY_U65_IP)
	USB_DRV_SetBits8(USB_U2PHYDTM1_0, USB_U2PHYDTM1_0_RG_IDPULLUP);
	USB_DRV_SetBits8(USB_U2PHYDTM1_1, USB_U2PHYDTM1_1_FORCE_IDPULLUP);
	kal_sleep_task(50);
#endif


	EINT_SW_Debounce_Modify(otg_idpin_eint, 50);
	EINT_Mask(otg_idpin_eint);
	
	g_otg_idpin_state = OTG_IDPIN_CABLE_PLUGIN_LEVEL;
	EINT_Registration(otg_idpin_eint, KAL_TRUE, OTG_IDPIN_CABLE_PLUGIN_LEVEL, OTG_ID_EINT_HISR, KAL_FALSE);	
	return STATUS_OK;
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Host_Set_VBUS_high(DCL_CTRL_DATA_T *data)
{
#ifdef __CHARGER_USB_DETECT_WIHT_ONE_EINT__
	DCL_BOOL *dcl_data_ptr = (DCL_BOOL*)data;
	kal_bool b_set = (kal_bool)(*dcl_data_ptr);


	g_OtgInfo.b_is_vbus_power_on = b_set;
	return STATUS_OK;
#else
	return STATUS_FAIL;
#endif
}


static DCL_STATUS DCL_USB_CTRL_API_OTG_Is_Host_Turn_On_VBUS(DCL_CTRL_DATA_T *data)
{
#ifdef __CHARGER_USB_DETECT_WIHT_ONE_EINT__
	DCL_BOOL *usb_data;


	usb_data = (DCL_BOOL *)data;
	*usb_data = (DCL_BOOL)g_OtgInfo.b_is_vbus_power_on;
	return STATUS_OK;
#else
	return STATUS_FAIL;
#endif
}

static DCL_STATUS DCL_USB_CTRL_API_Is_OTG_A_Cable_In(DCL_CTRL_DATA_T *data)
{
	OTG_DRV_CTRL_GET_Reg_T *usb_data;
	
	usb_data = &data->rOTG_Get_State2;
	usb_data->return_value = DCL_FALSE;

#if (defined(DRV_USB_PHY_COST_DOWN)) 
//	if ((USB_DRV_Reg8(USB_PHYIR3_1)& USB_PHYIR3_1_IDDIG)== 0x00)
//		usb_data->return_value = DCL_TRUE;
#elif (defined(DRV_USB_PHY_U65_IP))   // otg cable in : ox22 , without cable: 0x2A
	if ((USB_DRV_Reg8(USB_U2PHYDMON0_3)& USB_U2PHYDMON0_3_IDDIG_MAC)== 0x00)
		usb_data->return_value = DCL_TRUE;
#elif (defined(DRV_USB_PHY_U40_IP)) 

#else
// old project doesn't support OTG
#endif
	

	return STATUS_OK;
}

static DCL_STATUS DCL_USB_CTRL_API_SRP_Init(DCL_CTRL_DATA_T *data)
{
	ilm_struct 	*usb_ilm;

	DRV_BuildPrimitive(usb_ilm,
				stack_get_active_module_id(),
				MOD_USB,
				MSG_ID_SRP_INIT,
				NULL);
	msg_send_ext_queue(usb_ilm); 	
	
	return STATUS_OK;
}


DCL_USB_CTRL_API DCL_OTG_DRV_API_T[] =
{
	DCL_USB_CTRL_API_OTG_Init_Drv_Info,
	DCL_USB_CTRL_API_OTG_Release_Drv_Info,
	DCL_USB_CTRL_API_OTG_Register_Drv_Info,
	DCL_USB_CTRL_API_OTG_Drv_Create_ISR,
	DCL_USB_CTRL_API_OTG_Drv_Activate_ISR,
	DCL_USB_CTRL_API_OTGDMA_Drv_Create_ISR,
	DCL_USB_CTRL_API_OTGDMA_Drv_Activate_ISR,
	DCL_USB_CTRL_API_OTG_Init_Drv,
	DCL_USB_CTRL_API_OTG_Get_Plug_Type,
	DCL_USB_CTRL_API_OTG_A_Suspend_Req,
	DCL_USB_CTRL_API_OTG_A_Resume_Req,
	DCL_USB_CTRL_API_OTG_A_Stop_Host,
	DCL_USB_CTRL_API_OTG_A_Start_HNP,
	DCL_USB_CTRL_API_OTG_B_EnDis_HNP,
	DCL_USB_CTRL_API_OTG_B_Start_SRP,
	DCL_USB_CTRL_API_OTG_B_Stop_Host,
	DCL_USB_CTRL_API_OTG_B_Set_Session_Valid,
	DCL_USB_CTRL_API_OTG_Get_State,
	DCL_USB_CTRL_API_OTG_Enable_Device,
	DCL_USB_CTRL_API_OTG_Incr_Disconn_Count,
	DCL_USB_CTRL_API_OTG_Process_Exceptions,
	DCL_USB_CTRL_API_OTG_Init_A_Plug_Detect,
	DCL_USB_CTRL_API_OTG_Host_Set_VBUS_high,
	DCL_USB_CTRL_API_OTG_Is_Host_Turn_On_VBUS,
	DCL_USB_CTRL_API_Is_OTG_A_Cable_In,
	DCL_USB_CTRL_API_SRP_Init	
};



/*************************************************************************
* FUNCTION
*  DclOTG_DRV_Control
*
* DESCRIPTION
*  This function is to send command to control the OTG module.
*
* PARAMETERS
*  handle: A valid handle return by DclOTG_DRV_Open()
*  cmd: A control command for OTG module
// Should be added
*
*  data: The data of the control command
// Should be added
*
* RETURNS
*  STATUS_OK: command is executed successfully.
*  STATUS_FAIL: command is failed.
*  STATUS_INVALID_CMD: It's a invalid command.
*
*************************************************************************/
DCL_STATUS DclOTG_DRV_Control(DCL_HANDLE handle, DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)
{
	/* Check open or not and handle validity */
	if((g_OTG_Drv_Open == KAL_FALSE)||(DCL_OTG_DRV_CHECK_HANDLE_IS_VALID(handle) == 0))
	{
		ASSERT(0);

		if(g_OTG_Drv_Open == KAL_FALSE)
			return STATUS_INVALID_OPERATION;
		else
			return STATUS_INVALID_DCL_HANDLE;
	}

	return DCL_OTG_DRV_API_T[cmd](data);
}


#endif /* #if defined(__OTG_ENABLE__) && !defined(DRV_USB_OFF) */