summaryrefslogtreecommitdiff
path: root/Core/EM/usb/amiusbhc.c
blob: 7f904345ac2efffe7ea9110e4f6e89a76034d48a (plain)
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
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2016, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************

//****************************************************************************
// $Header: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/core/em/usb/amiusbhc.c 1     3/01/17 2:28a Chienhsieh $
//
// $Revision: 1 $
//
// $Date: 3/01/17 2:28a $
//
//****************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/core/em/usb/amiusbhc.c $
// 
// 1     3/01/17 2:28a Chienhsieh
// [TAG]  		EIP320994
// [Description]  	Fixed Reconnect XHCI controller fail in shell with USB,
// 4.6.5.1_USB_08.10.36 (XHCI Mode: Enabled)
// 
// 99    10/28/16 3:59a Wilsonlee
// [TAG]  		EIP300142
// [Category]  	Improvement
// [Description]  	Remove USB Int1C module part because we use the other
// method to service xhci.
// [Files]  		usbport.c, amidef.h, amiusbhc.c, UsbLegacy.cif
// 
// 98    10/16/16 10:18p Wilsonlee
// [TAG]  		EIP288158
// [Category]  	Improvement
// [Description]  	Check if gUsbData is integrity.
// [Files]  		amiusb.cif, usbsb.c, AmiUsbLib.cif, AmiUsbLib.sdl,
// AmiUsbSmmGlobalDataValidationLib.c,
// AmiUsbSmmGlobalDataValidationLib.cif,
// AmiUsbSmmGlobalDataValidationLib.mak, Crc32.c, amiusb.c, amiusb.h,
// ehci.c, elib.c, ohci.c, syskbc.c, uhci.c, usb.c, usbCCID.c, usbdef.h,
// usbhid.c, usbhub.c, usbkbd.c, usbmass.c, usbms.c, usbrt.mak, xhci.c,
// amiusbhc.c, efiusbccid.c, efiusbmass.c, uhcd.c, usbmisc.c,
// AmiUsbController.h, AmiUsbLibInclude.cif,
// AmiUsbSmmGlobalDataValidationLib.h
// 
// 97    7/28/16 4:55a Wilsonlee
// [TAG]  		EIP264662
// [Category]  	Improvement
// [Description]  	Don't install usb hw smi after reconnecting usb
// controllers.
// [Files]  		uhcd.c, usb.c, ohci.c, amiusb.c, amiusbhc.c
// 
// 96    7/07/16 1:12a Wilsonlee
// [TAG]  		EIP277810
// [Category]  	Improvement
// [Description]  	Validate the memory buffer is entirely outside of SMM.
// [Files]  		usbsb.c, amiusb.c, ehci.c, ohci.c, uhci.c, usbCCID.c,
// usbdef.h, usbmass.c, xhci.c, amiusbhc.c, efiusbccid.c, efiusbmass.c,
// uhcd.c, uhcd.h, usbmisc.c
// 
// 95    3/31/16 5:49a Wilsonlee
// [TAG]  		EIP259282
// [Category]  	Improvement
// [Description]  	Update AmiUsbHcGetRootHubPortStatus function to support
// super speed devices.
// [Files]  		amiusbhc.c
// 
// 94    3/02/16 9:42p Wilsonlee
// [TAG]  		EIP254309
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	GK-FORCE K83 USB KB function abnormal.
// [RootCause]  	This device has an interrupt out endpoint and doesn't
// support "Set Report" request.
// [Solution]  	Use the interrupt out endpoint instead of sending "Set
// Report" request.
// [Files]  		AmiUsbController.h, xhci.c, usbmass.c, usbkbd.h, usbkbd.c,
// usbhub.c, usbhid.c, usbdef.h, usbCCID.c, usb.c, uhci.c, ohci.c, ehci.c,
// amiusb.h, efiusbms,c, amiusbhc.c
// 
// 93    1/19/16 3:19a Wilsonlee
// [TAG]  		EIP251667
// [Category]  	Improvement
// [Description]  	Implement EFI_USB2_HC_PROTOCOL.SetState() function.
// [Files]  		amiusbhc.c, uhcd.h
// 
// 92    1/19/16 2:47a Wilsonlee
// [TAG]  		EIP251519
// [Category]  	Improvement
// [Description]  	Return current state of the USB host controller on
// EFI_USB2_HC_PROTOCOL.GetState().
// [Files]  		amiusbhc.c
// 
// 91    1/19/16 2:12a Wilsonlee
// [TAG]  		EIP252487
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Static code analysis issues found in Usb module.
// [RootCause]  	Wrong function called due to copy-paste error.
// [Solution]  	Replace AmiUsbHcClearRootHubPortFeature with
// AmiUsbHcSetRootHubPortFeature.
// [Files]  		amiusbhc.c
// 
// 90    12/03/15 1:31a Wilsonlee
// [TAG]  		EIP247363
// [Category]  	Improvement
// [Description]  	MajorRevision is 3 and MinorRevision is 1 for xhci 1.1
// controllers.
// [Files]  		amiusbhc.c
// 
// 89    7/24/15 5:03a Wilsonlee
// [TAG]  		EIP230092
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	Usb devices don't work in VL801 / VL805 under BIOS.
// [RootCause]  	Bit[0..5] are reserved in Device Context Base Address
// Array Element 0.
// [Solution]  	Allcate a PAGESIZE boundary for Scratchpad Buffer Array
// Base Address.
// [Files]  		amiusbhc.c
// 
// 88    5/13/15 2:46a Wilsonlee
// [TAG]  		EIP216587
// [Category]  	Improvement
// [Description]  	Add 64-bit addressing buffer support for usb transfers.
// [Files]  		amiusbhc.c, efiusbmass.c
// 
// 87    4/10/15 3:05a Wilsonlee
// [TAG]  		EIP207413
// [Category]  	Improvement
// [Description]  	Install UsbApiTable and UsbMassApitTable in
// AmiUsbSmmProtocol.
// [Files]  		amiusbhc.c, AmiUsbController.h, usbdef.h, usbCCID.c, uhci.c,
// ehci.c, amiusbrtCCID.h, amiusb.h, amiusb.c, uhcd.c
// 
// 86    2/09/15 4:16a Wilsonlee
// [TAG]  		EIP202592
// [Category]  	Improvement
// [Description]  	Don't reset xhci controller if it's Debug Capability is
// enabled.
// [Files]  		xhci.c, xhci.h, amiusbhc.c
// 
// 85    1/22/15 10:20p Wilsonlee
// [TAG]  		EIP201434
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Number of connected devices isn't correct if we plug out
// keyboards or mice behind hub in xhci.
// [RootCause]  	The PortConnectChange bit is cleared when we check port
// status for interrupt endpoint transaction error.
// [Solution]  	Don't clear change bits if we check port status for
// interrupt endpoint transaction error.
// [Files]  		xhci.c, usbhub.c, usbdef.h, usb.c, uhci.c, ohci.c, ehci.c,
// amiusbhc.c
// 
// 84    12/24/14 6:12a Wilsonlee
// [TAG]  		EIP196753
// [Category]  	Improvement
// [Description]  	Check Max Scratchpad Buffers (Max Scratchpad Bufs Hi)
// register and allocate Scratchpad buffers.
// [Files]  		amiusbhc.c, xhci.h
// 
// 83    6/29/14 10:40p Wilsonlee
// [TAG]  		EIP175328
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	Bluetooth module asserts in AmiUsb_07.
// [RootCause]  	For ActivatePolling function, we use new parameter,
// PollingLength, in AmiUsb_07, but it's undefined in
// AmiUsb2HcAsyncInterruptTransfer.
// [Solution]  	Initialize PollingLength in
// AmiUsb2HcAsyncInterruptTransfer.
// [Files]  		amiusbhc.c
// 
// 82    4/30/14 5:27a Wilsonlee
// [TAG]  		EIP164842
// [Category]  	Improvement
// [Description]  	Check if the devices have put into to our queue before
// we put them.
// [Files]  		UsbInt13.c, amiusb.c, ehci.c, ohci.c, usb.c, usbdef.h,
// usbmass.c, xhci.c, amiusbhc.c, efiusbmass.c, uhcd.c, usbbus.c, usbsb.c
// 
// 81    12/30/13 3:17a Ryanchou
// Corrent max packet size check for full speed device  in
// AmiUsb2HcSyncInterruptTransfer().
// 
// 80    7/26/13 2:41a Ryanchou
// [TAG]  		EIP122142
// [Category]  	Improvement
// [Description]  	Improve periodic schedule mechanism
// [Files]  		ehci.c, ehci.h, ohci.c, ohci.h, uhci.c, uhci.h, usbdef.h,
// amiusbhc.c
// 
// 79    3/25/13 9:36p Wilsonlee
// [TAG]  		EIP118926
// [Category]  	Improvement
// [Description]  	Add the token "EFI_USB_HC_INTERRUPT_OUT_SUPPORT" to
// control whether support EFI_USB2_HC_PROTOCOL.AsyncInterruptTransfer and
// EFI_USB2_HC_PROTOCOL.SyncInterruptTransfer() on OUT endpoints.
// [Files]  		amiusbhc.c, usbsrc.sdl
// 
// 78    3/19/13 4:00a Ryanchou
// [TAG]  		EIP118177
// [Category]  	Improvement
// [Description]  	Dynamically allocate HCStrucTable at runtime.
// [Files]  		usb.sdl, usbport.c, usbsb.c, amiusb.c, ehci.c, ohci.c,
// syskbc.c, sysnokbc.c, uhci.c, usb.c, usbCCID.c, usbdef.h, usbhid.c,
// usbhub.c, usbmass.c, usbrt.mak, usb.sd, amiusbhc.c, efiusbccid.c,
// efiusbhid.c, efiusbmass.c, efiusbms.c, uhcd.c, uhcd.h, uhcd.mak,
// usbmisc.c, usbsrc.sdl
// 
// 77    1/11/13 4:19a Ryanchou
// [TAG]  		EIP102491
// [Category]  	Improvement
// [Description]  	Synchronized with Aptio V USB module
// [Files]  		usbport.c, usbsb.c, ehci.c, ehci.h, ohci.c, ohci.h, uhci.h,
// usb.c, usbdef.h, usbhid.c, usbhub.c, usbkbd.c, usbkbd.h, usbmass.c.
// usbms.c, usbpoint.c, xhci.h, usb.sd, amiusbhc.c, componentname.c,
// efiusbkc.c, efiusbmass.c, uhcd.c, uhcd.h, usbbus.c, usbbus.h, usbmisc.c
// 
// 76    11/10/12 6:41a Ryanchou
// [TAG]  		EIP99431
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Cannot use the UsbIo's UsbAsyncInterruptTransfer for
// keyboard input
// [RootCause]  	Stopping EFI USB keyboard driver does not stop the
// endpoint polling, then application calls UsbAsyncInterruptTransfer,
// error will be returned.
// [Solution]  	Stops endpoint polling and release resource when
// disconnecting the device driver. And improve the
// UsbSyncInterruptTransfer.
// [Files]  		amiusb.c, amiusb.h, ehci.c, ohci.c, uhci.c, usb.c,
// usbCCID.c, usbdef.h, usbhub.c, usbkbd.c, usbmass.c, usbms.c,
// usbpoint.c, amiusbhc.c, efiusbhid.c, usbbus.c, usbbus.h
// 
// 75    9/28/12 3:00a Wilsonlee
// [TAG]  		EIP101226
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Chief River fails to boot when Above 4G Decoding is
// enabled.
// [RootCause]  	We read 32 bits width memory base address even if it is
// 64 bits wide.
// [Solution]  	Read 64 bits width memory base address if it is 64 bits
// wide.
// [Files]  		amiusbhc.c
// 
// 74    8/29/12 8:34a Ryanchou
// [TAG]  		EIP77262
// [Category]  	New Feature
// [Description]  	Remove SMM dependency of USB.
// [Files]  		usb.sdl, usbport.c, amiusb.c, amiusb.dxs, amiusb.h, ehci.c,
// elib.c, ohci.c, uhci.c, usb.c, usbdef.h, usbrt.mak, xhci.c, amiusbhc.c,
// efiusbccid.c, efiusbhid.c, efiusbkb.c, efiusbmass.c, uhcd.c, uhcd.dxs,
// uhcd.h, usbmisc.c, AmiUsbController.h
// 
// 73    8/29/12 6:25a Ryanchou
// [TAG]  		EIP93932
// [Category]  	Improvement
// [Description]  	Fix the SCT test failure in AsyncInterruptTransfer
// item.
// [Files]  		amiusbhc.c
// 
// 72    7/25/12 4:51a Wilsonlee
// [TAG]  		EIP91840
// [Category]  	Improvement
// [Description]  	Initialize the variable Params.ApiData.CoreProc.retVal
// and Params.bRetValue.
// [Files]  		amiusbhc.c
// 
// 71    5/04/12 6:41a Ryanchou
// [TAG]  		EIP82875
// [Category]  	Improvement
// [Description]  	Support start/stop individual USB host to avoid
// reconnect issues.
// [Files]  		usbport.c, usbsb.c, amiusb.c, amiusb.h, ehci.c, ohci.c,
// uhci.c, uhci.h, usb.c, usbdef.h, xhci.c, amiusbhc.c, uhcd.c, uhcd.h,
// usbbus.c, usbmisc.c
// 
// 70    5/03/12 5:53a Roberthsu
// [TAG]           EIP84455
// [Category]  	Improvement
// [Description]  	Implement usb hid device gencric.
// [Files]  		amiusb.c,amiusbhc.c,efiusbhid.c,efiusbkb.c,ehci.c,ohci.c,uhc
// d.c,uhci.c,usbdef.h,usbhid.c,usbhub.c,usbkbd.c,usbkbd.h,usbms.c,usbsb.c
// ,usbsrc.sdl
// 
// 69    4/10/12 10:14p Wilsonlee
// [TAG]  		EIP84790
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	USB IO PROTOCOL under XHCI issue
// [RootCause]  	The DevMiscInfo is NULL.
// [Solution]  	Fill the DevMiscInfo for the xhci controller.
// [Files]  		ehci.c, xhci.c, amiusbhc.c
// 
// 68    2/16/12 8:55p Wilsonlee
// [TAG]  		EIP81612
// [Category]  	Spec Update
// [Severity]  	Minor
// [Description]  	Add EFI_USB_SPEED_SUPER in EFI_USB2_HC_PROTOCOL
// according to UEFI 2.3.1 spec
// [Files]  		usb.c, usbbus.c, amiusbhc.c
// 
// 67    1/13/12 4:26a Ryanchou
// [TAG]  		EIP47348
// [Category]  	New Feature
// [Description]  	Support for USB Port Reset function.
// [Files]  		amiusb.c, amiusb.h, amiusbhc.c, uhci.c, usb.c, usbbus.c,
// usbbus.h, usbmass.c
// 
// 66    12/14/11 2:07a Ryanchou
// [TAG]  		EIP76397
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	ASSERT occurred when executing "reconnect -r" under shell
// [RootCause]  	The EIP63188 changes locate all USB controllers at one
// time, the "reconnect -r" will connect ConIn and ConOut first, so USB
// driver only locate a controller.
// [Solution]  	Rollback the EIP63188 changes to avoid this issue.
// [Files]  		amiusbhc.c, efiusbhid.c, efiusbkb.c, uhcd.c, uhcd.h,
// usbbus.c usbhid.c
// 
// 65    9/28/11 10:47a Ryanchou
// [TAG]  		EIP66064
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	System hangs when waiting for finger swipe
// [RootCause]  	USB driver save the URP pointer to EBDA in function
// UsbSmiCore and UsbSmiHc, the pointer will be destroyed if someone also
// invoke the two functions.
// [Solution]  	Save the URP pointer before generate SW SMI and restore it
// after return from SMI.
// [Files]  		amiusb.c, amiusbhc.c, usbport.c
// 
// 64    7/15/11 6:23a Ryanchou
// [TAG]  		EIP38434
// [Category]  	New Feature
// [Description]  	Added USB HID report protocol support.
// [Files]  		amiusb.c, AmiUsbController.h, amiusbhc.c, efiusbkb.c,
// efiusbkb.h, efiusbpoint.c, ehci.c, ohci.c, uhcd.c uhcd.cif, uhci.c,
// usb.c, usbdef.h, usbhid.c, usbkbd.c, usbkbd.h, usbms.c, usbpoint.c,
// usbrt.cif, usbsb.c, usbsetup.c, usbsrc.sdl, xhci.c
// 
// 62    4/06/11 3:00a Ryanchou
// [TAG]  		EIP57691
// [Category]  	Improvement
// [Description]  	The MP version of FL1009 doesn't need the EIP46210
// workaround, remove the EIP46210 change.
// [Files]  		amiusbhc.c
// 
// 61    2/18/11 12:24a Ryanchou
// [TAG]  		EIP51495
// [Category]  	Improvement
// [Description]  	Clear 'HC OS Owned Semaphore' bit during XHCI legacy
// initialization. 
// [Files]  		amiusbhc.c, ehci.c
// 
// 60    12/28/10 4:00a Ryanchou
// [TAG]  		EIP48009
// [Category]  	Improvement
// [Description]  	Use 32 or 64 byte Context data structures dynamically.
// [Files]  		amiusbhc.c, usbsrc.sdl, xhci.c, xhci.h
// 
// 59    12/02/10 2:19p Olegi
// [TAG]  		EIP48695
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	SCT Error for USB HC SyncInterruptTransfer API
// [RootCause]  	Checking for endpoint being IN was missing.
// [Solution]  	- Added checking for BIT7 of the interrupt endpoint
// address, if not set return EFI_INVALID_PARAMETER.
// - Corrected EFI USB mouse driver that issues the SyncInterruptTransfer
// with the wrong endpoint address.
// [Files]  		amiusbhc.c
// efiusbms.c
// 
// 58    10/30/10 2:32a Ryanchou
// EIP38221: Added the code that properly initializes
// DEV_INFO.bIntEndpoint field; interrupt endpoint polling is using this
// endpoint number.
// 
// 57    10/22/10 9:07a Ryanchou
// EIP46210: Added FL1009 USB 3.0 connection linkup workaround. 
// 
// 56    10/22/10 8:58a Ryanchou
// EIP46693: Clear xHCI BIOS owned semaphore bit and SMI enable bit in
// PreInitXhci.
// 
// 55    10/21/10 10:16a Ryanchou
// 
// 54    9/16/10 12:47p Olegi
// Changed the parameters validation code in
// AmiUsb2HcSyncInterruptTransfer function:
// - MaxPacketLength does not have to be 8, it is 4 for a mouse
// - Endpoint does not have to be in (BIT7)
// 
// 53    9/07/10 6:03a Tonylo
// EIP43742 - Wrong 64bit-Addressing Capability bit acquired for
// reference.
// 
// 52    8/31/10 9:01a Tonylo
// EIP41544 - Add EntronTech XHCI support.
// 
// Category:  New Feature
// 
// Description:  Add EntronTech XHCI support.
// 
// Files:  amiusbhc.c
// usbport.c
// uhcd.h
// 
// 
// 51    8/17/10 4:26p Olegi
// Klockwork issues fixes. EIP37978
// 
// 50    6/07/10 5:00p Olegi
// Corrected the transfer routines output values. EIP34492.
// 
// 49    5/19/10 4:04p Olegi
// Remove the assertion on not-successful return from SMI handler. Errors
// are legitimate, they are handled on the callers' level.
// 
// 48    4/02/10 8:59a Olegi
// 
// 47    2/27/10 12:00p Olegi
// 
// 46    2/26/10 4:08p Olegi
// 
// 45    2/08/10 9:56a Olegi
// EIP33381: Implement multiple bulk endpoint in UsbIoProtocol.
// 
// 44    1/27/10 5:26p Olegi
// 
// 43    1/19/10 11:59a Olegi
// 
// 42    12/22/09 8:47a Olegi
// 
// 41    11/25/09 8:06a Olegi
// 
// 40    11/09/09 5:40p Olegi
// 
// 39    10/30/09 5:48p Olegi
// 
// 38    10/09/09 5:57p Olegi
// 
// 37    8/11/09 9:30a Olegi
// Update for a bugfix #24507.
// 
// 36    8/05/09 6:01p Olegi
// Bugfix in the controller version reporting. EIP#24507.
// 
// 34    12/16/08 10:50a Olegi
// Correction in the return values: 0 changed to NULL. EIP#17767.
// 
// 32    10/03/08 3:31p Olegi
// gUsbData->dLastCommandStatusExtended is initialized before every
// transfer.
// 
// 31    9/26/08 5:06p Olegi
// Added one more stall check condition.
// 
// 30    9/02/08 10:33a Olegi
// Modifications in GetRootHubStatus and ControlTransfer to return proper
// error codes.
// 
// 29    8/08/08 2:39p Olegi
// Bugfix in AmiUsbHcGetRootHubPortStatus - port number passed there is
// 0-based.
// 
// 28    5/16/08 12:02p Olegi
// Compliance with AMI coding standard.
// 
// 23    12/22/06 4:05p Olegi
// Timeout implementation.
//
// 18    10/26/06 3:57p Olegi
// EFI_USB2_HC_PROTOCOL implementatin.
//
// 12    3/20/06 3:37p Olegi
// Version 8.5 - x64 compatible.
//
// 8     6/15/05 1:58p Andriyn
// Comments were changed
//****************************************************************************
//<AMI_FHDR_START>
//
//  Name:           AmuUsbHc.c
//
//  Description:    USB_HC_PROTOCOL implementation
//
//<AMI_FHDR_END>
//****************************************************************************

#include "Efi.h"
#include "amidef.h"
#include "usbdef.h"
#include "uhcd.h"

#include "tree.h"
#include "uhci.h"
#include "pci.h"
#include "protocol\cpu.h"
#include "protocol\legacy8259.h"

#define INTERRUPTQUEUESIZE 10

extern EFI_GUID  gEfiUsb2HcProtocolGuid;
extern USB_GLOBAL_DATA *gUsbData;
extern EFI_USB_PROTOCOL            *gAmiUsbController;

static UINT8 SpeedMap[] = { 0x10, 0x01, 0, 0 };

EFI_LEGACY_8259_PROTOCOL    *gPic;
UINT32  gVector = 0;

UINT8	*gUsbBusTempBuffer = NULL;
UINTN	gTempBufferPages = 0;


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        GetThis
//
// Description:
//  Function returns a pointer to HcProtocol2 record of a given protocol
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

static HC_DXE_RECORD* GetThis(EFI_USB2_HC_PROTOCOL* Protocol)
{
    return (HC_DXE_RECORD*)(
        (char*)Protocol - (UINTN)&((HC_DXE_RECORD*)0)->hcprotocol2 );
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        GetThis1
//
// Description:
//  Function returns a pointer to HcProtocol record of a given protocol
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

static HC_DXE_RECORD* GetThis1 (EFI_USB_HC_PROTOCOL* Protocol)
{
    return (HC_DXE_RECORD*)(
        (char*)Protocol - (UINTN)&((HC_DXE_RECORD*)0)->hcprotocol );
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        GetTransferStatus
//
// Description:
//  Function converts the bitmap of gUsbData->dLastCommandStatusExtended into
//  a valid USB error.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

static UINT32 GetTransferStatus()
{
    static struct { int BitDst, BitSrc; } ErrorMap[] = {
        EFI_USB_ERR_STALL,      USB_TRSFR_STALLED,
        EFI_USB_ERR_STALL,      USB_BULK_STALLED,
        EFI_USB_ERR_STALL,      USB_CONTROL_STALLED,
        EFI_USB_ERR_BUFFER,     USB_TRSFR_BUFFER_ERROR,
        EFI_USB_ERR_BABBLE,     USB_TRNSFR_BABBLE,
        EFI_USB_ERR_NAK,        USB_TRNSFR_NAK,
        EFI_USB_ERR_CRC,        USB_TRNSFR_CRCERROR,
        EFI_USB_ERR_TIMEOUT,    USB_TRNSFR_TIMEOUT,	//(EIP84790)
        EFI_USB_ERR_BITSTUFF,   USB_TRNSFR_BITSTUFF,
        EFI_USB_ERR_SYSTEM, 0
    };
    UINT32 Err = 0;
    UINT32 Status = gUsbData->dLastCommandStatusExtended;
    UINT8 i;

    for (i = 0; i<COUNTOF(ErrorMap); ++i){
        if( Status & ErrorMap[i].BitSrc ) {
            Err |= ErrorMap[i].BitDst;
        }
    }

    if( Status == 0 ) {
        return EFI_USB_NOERROR;
    } else {
        return Err;
    }
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbSmiCore
//
// Description:
//  Bridge between DXE code and function in USB Core proc table which is inside
//  our SMI code.
//
// Input:
//  Func - function number opUSBCORE_XXX which corresponds to index in Core Proc table.
//  Rest of the parameters coresponds the callee interface
//
// Output:
//  Whatever callee returns
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

enum {
    opUSBCORE_GetDescriptor,
    opUSBCORE_ReConfigDevice,
    opUSBCORE_ReConfigDevice2,
    opUSBCORE_AllocDevInfo,
    opUSBCORE_PrepareForLegacyOS,
    opUSBCORE_ResetAndReconfigDev,
    opUSBCORE_DevDriverDisconnect,
    opUSBCORE_PeriodicEvent,
    opUSBCORE_Last,
};


int CoreprocStackSize[] = {
    6*sizeof(VOID*), // opUSBSMI_GetDescriptor
    2*sizeof(VOID*), // opUSBCORE_ReConfigDevice
    4*sizeof(VOID*), // opUSBCORE_ReConfigDevice2
    1*sizeof(VOID*), // opUSBCORE_AllocDevInfo
    1*sizeof(VOID*), // opUSBCORE_PrepareForLegacyOS
    2*sizeof(VOID*), // opUSBCORE_ResetAndReconfigDev
    2*sizeof(VOID*), // opUSBCORE_DevDriverDisconnect
    0, // opUSBCORE_PeriodicEvent
};

UINTN UsbSmiCore( UINT8 Func, ...  )
{
    URP_STRUC   Params;
    VA_LIST     ArgList;

    ASSERT(Func < COUNTOF(CoreprocStackSize));
//    ASSERT(CoreprocStackSize[Func] > 0);

    VA_START(ArgList, Func);

    Params.bFuncNumber = USB_API_CORE_PROC;
    Params.bSubFunc = Func;
	Params.bRetValue = USB_ERROR;       //(EIP91840+)
    Params.ApiData.CoreProc.paramBuffer = &VA_ARG(ArgList, int);
    Params.ApiData.CoreProc.paramSize = CoreprocStackSize[Func];
    Params.ApiData.CoreProc.retVal = 0; //(EIP91840+)
    USB_DEBUG(DEBUG_USBHC_LEVEL8,
        "call CORE SMI proc(%d); params: %x\n", Func, Params.ApiData.CoreProc.paramBuffer);

	InvokeUsbApi(&Params);

    VA_END(ArgList);

//    ASSERT(Params.bRetValue == USB_SUCCESS);

    return Params.ApiData.CoreProc.retVal;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    UsbSmiGetDescriptor
//
// Description:
//  SW SMI to execute GetDescriptor transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8*
UsbSmiGetDescriptor (
    HC_STRUC*   HostController,
    DEV_INFO*   Device,
    UINT8*      Buffer,
    UINT16      Length,
    UINT8       DescType,
    UINT8       DescIndex
)
{
	EFI_STATUS	Status;
	UINT8		*DataBuffer = Buffer;
	UINTN		ReturnValue;

	if (Shr64((UINTN)Buffer, 32)) {
		if (gUsbBusTempBuffer == NULL || Length > (gTempBufferPages << 12)) {
			if (gUsbBusTempBuffer) {
				gBS->FreePages((EFI_PHYSICAL_ADDRESS)gUsbBusTempBuffer, gTempBufferPages);
				gUsbBusTempBuffer = NULL;
				gTempBufferPages = 0;
			}
			gTempBufferPages = EFI_SIZE_TO_PAGES(Length);
			gUsbBusTempBuffer = (UINT8*)0xFFFFFFFF;
			Status = gBS->AllocatePages(AllocateMaxAddress, EfiBootServicesData,
							gTempBufferPages, (EFI_PHYSICAL_ADDRESS*)&gUsbBusTempBuffer);
			if (!EFI_ERROR(Status)) {
				gBS->SetMem (gUsbBusTempBuffer, gTempBufferPages << 12, 0);
			} else {
                gUsbBusTempBuffer = NULL;
                gTempBufferPages = 0;
			}
		}

        if (gUsbBusTempBuffer) {
		    DataBuffer = gUsbBusTempBuffer;
        }
	}
    ReturnValue = UsbSmiCore(opUSBCORE_GetDescriptor,
        HostController, Device, DataBuffer, Length, DescType, DescIndex);
    
    if (ReturnValue == 0) {
        return NULL;
    }
    
	if (DataBuffer != Buffer) {
		gBS->CopyMem (Buffer, DataBuffer, Length);
	}

	return Buffer;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbSmiReConfigDevice
//
// Description:
//  Perform the device specific configuration
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
UsbSmiReConfigDevice(
    HC_STRUC*   HostController,
    DEV_INFO*   Device
)
{
    return (UINT8)UsbSmiCore(opUSBCORE_ReConfigDevice, HostController, Device);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbAllocDevInfo
//
// Description:
//  Allocate the empty buffer for USB device
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
UsbAllocDevInfo()
{
    return (DEV_INFO*)UsbSmiCore(opUSBCORE_AllocDevInfo, 1);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbPrepareForLegacyOS
//
// Description:
//  Callback on LEGACY_BOOT event
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID UsbPrepareForLegacyOS()
{
    UsbSmiCore(opUSBCORE_PrepareForLegacyOS, 1);
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbResetAndReconfigDev
//
// Description:
//
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
UsbResetAndReconfigDev(
    HC_STRUC*   HostController,
    DEV_INFO*   Device
)
{
    return (UINT8)UsbSmiCore(opUSBCORE_ResetAndReconfigDev, HostController, Device);
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbDevDriverDisconnect
//
// Description:
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8
UsbDevDriverDisconnect(
    HC_STRUC*   HostController,
    DEV_INFO*   Device
)
{
    return (UINT8)UsbSmiCore(opUSBCORE_DevDriverDisconnect, HostController, Device);
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbSmiHc
//
// Description:
//  Bridge between DXE code and SMI function in USB HC driver.
//
// Input:
//  Func       - function number opHC_XXX which corresponds to index in HCD_HEADER
//  HcType     - type of USB HC controller; selects an HC driver to call
//  Rest of the parameters coresponds the callee interface
//
// Output:
//  Whatever callee returns
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

int HcprocStackSize[] = {
    sizeof(VOID*) * 1, // opHC_Start
    sizeof(VOID*) * 1, // opHC_Stop
    sizeof(VOID*) * 1, // opHC_EnumeratePorts
    sizeof(VOID*) * 1, // opHC_DisableInterrupts
    sizeof(VOID*) * 1, // opHC_EnableInterrupts
    sizeof(VOID*) * 1, // opHC_ProcessInterrupt
    sizeof(VOID*) * 3, // opHC_GetRootHubStatus
    sizeof(VOID*) * 2, // opHC_DisableRootHub
    sizeof(VOID*) * 2, // opHC_EnableRootHub
    sizeof(VOID*) * 7, // opHC_ControlTransfer
    sizeof(VOID*) * 5, // opHC_BulkTransfer
    sizeof(VOID*) * 6, // opHC_InterruptTransfer
    sizeof(VOID*) * 2, // opHC_DeactivatePolling
    sizeof(VOID*) * 2, // opHC_ActivatePolling
    sizeof(VOID*) * 1, // opHC_DisableKeyRepeat
    sizeof(VOID*) * 1, // opHC_EnableKeyRepeat
    sizeof(VOID*) * 3, // opHC_EnableEndpoints
    sizeof(VOID*) * 4, // opHC_InitDeviceData
    sizeof(VOID*) * 2, // opHC_DeinitDeviceData
    sizeof(VOID*) * 2, // opHC_ResetRootHub
    sizeof(VOID*) * 3, // opHC_ClearEndpointState
    sizeof(VOID*) * 1, // opHC_GlobalSuspend
};

UINTN
UsbSmiHc(
    UINT8 Func,
    UINT8 HcType,
    ...
)
{
    URP_STRUC   Params;
    VA_LIST     ArgList;

    ASSERT(Func < sizeof(HcprocStackSize)/sizeof(int));
    ASSERT( HcprocStackSize[Func] > 0);

    VA_START(ArgList, HcType);

    Params.bFuncNumber = USB_API_HC_PROC;
    Params.bSubFunc = Func;
	Params.bRetValue = USB_ERROR;       //(EIP91840+)
    Params.ApiData.HcProc.paramBuffer = &VA_ARG(ArgList, int);
    Params.ApiData.HcProc.paramSize = HcprocStackSize[Func];
    Params.ApiData.HcProc.bHCType = HcType;
    Params.ApiData.HcProc.retVal = 0;   //(EIP91840+)
    USB_DEBUG(DEBUG_USBHC_LEVEL8,
        "call HC SMI driver(type:%d;func:%d); params at %x\n",
            HcType, Func, Params.ApiData.HcProc.paramBuffer);

	InvokeUsbApi(&Params);

    VA_END(ArgList);
//    ASSERT(Params.bRetValue == USB_SUCCESS);

    return Params.ApiData.HcProc.retVal;
}

VOID
UsbSmiPeriodicEvent(VOID)
{
//    UsbSmiCore(opUSBCORE_PeriodicEvent);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    UsbSmiGetRootHubStatus, UsbSmiEnableRootHub, UsbSmiDisableRootHub
//
// Description:
//  Wrappers for calling USB HC driver functions in USBSMI service
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8 UsbSmiGetRootHubStatus(HC_STRUC* HcStruc, UINT8 PortNum, BOOLEAN ClearChangeBits)
{
    return (UINT8)UsbSmiHc(
            opHC_GetRootHubStatus, HcStruc->bHCType, HcStruc, PortNum, ClearChangeBits);
}

UINT8 UsbSmiEnableRootHub(HC_STRUC* HcStruc, UINT8 PortNum)
{
    return (UINT8)UsbSmiHc(
            opHC_EnableRootHub, HcStruc->bHCType, HcStruc, PortNum);
}

UINT8 UsbSmiDisableRootHub(HC_STRUC* HcStruc, UINT8 PortNum)
{
    return (UINT8)UsbSmiHc(
            opHC_DisableRootHub, HcStruc->bHCType, HcStruc, PortNum);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    UsbSmiControlTransfer, UsbSmiBulkTransfer, UsbSmiInterruptTransfer
//
// Description:
//  Wrappers for calling USB HC driver USB transfer functions
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>


UINT16
UsbSmiControlTransfer (
    HC_STRUC    *HcStruc,
    DEV_INFO    *DevInfo,
    UINT16      Request,
    UINT16      Index,
    UINT16      Value,
    UINT8       *Buffer,
    UINT16      Length)
{
	EFI_STATUS	Status;
	UINT8		*DataBuffer = Buffer;
	UINTN		ReturnValue;

	if (Shr64((UINTN)Buffer, 32)) {
		if (gUsbBusTempBuffer == NULL || Length > (gTempBufferPages << 12)) {
			if (gUsbBusTempBuffer) {
				gBS->FreePages((EFI_PHYSICAL_ADDRESS)gUsbBusTempBuffer, gTempBufferPages);
				gUsbBusTempBuffer = NULL;
				gTempBufferPages = 0;
			}
			gTempBufferPages = EFI_SIZE_TO_PAGES(Length);
			gUsbBusTempBuffer = (UINT8*)0xFFFFFFFF;
			Status = gBS->AllocatePages(AllocateMaxAddress, EfiBootServicesData,
							gTempBufferPages, (EFI_PHYSICAL_ADDRESS*)&gUsbBusTempBuffer);
			if (!EFI_ERROR(Status)) {
				gBS->SetMem (gUsbBusTempBuffer, gTempBufferPages << 12, 0);
			} else {
                gUsbBusTempBuffer = NULL;
                gTempBufferPages = 0;
			}
		}
        if (gUsbBusTempBuffer) {
		    DataBuffer = gUsbBusTempBuffer;
        }
	}

    if (DataBuffer != Buffer) {
        if (!(Request & USB_REQ_TYPE_INPUT)) {
            gBS->CopyMem(DataBuffer, Buffer, Length);
        }
    }
    
    ReturnValue = UsbSmiHc(opHC_ControlTransfer,
        HcStruc->bHCType, HcStruc, DevInfo,
        Request, Index, Value, DataBuffer, Length);
    
	if (DataBuffer != Buffer) {
        if (Request & USB_REQ_TYPE_INPUT) {
            gBS->CopyMem(Buffer, DataBuffer, Length);
        }
	}

	return (UINT16)ReturnValue;
}

UINT32
UsbSmiBulkTransfer(
    HC_STRUC    *HcStruc,
    DEV_INFO    *DevInfo,
    UINT8       XferDir,
    UINT8       *Buffer,
    UINT32      Length
)
{
	EFI_STATUS	Status;
	UINT8		*DataBuffer = Buffer;
	UINTN		ReturnValue;

	if (Shr64((UINTN)Buffer, 32)) {
		if (gUsbBusTempBuffer == NULL || Length > (gTempBufferPages << 12)) {
			if (gUsbBusTempBuffer) {
				gBS->FreePages((EFI_PHYSICAL_ADDRESS)gUsbBusTempBuffer, gTempBufferPages);
				gUsbBusTempBuffer = NULL;
				gTempBufferPages = 0;
			}
			gTempBufferPages = EFI_SIZE_TO_PAGES(Length);
			gUsbBusTempBuffer = (UINT8*)0xFFFFFFFF;
			Status = gBS->AllocatePages(AllocateMaxAddress, EfiBootServicesData,
							gTempBufferPages, (EFI_PHYSICAL_ADDRESS*)&gUsbBusTempBuffer);
			if (!EFI_ERROR(Status)) {
				gBS->SetMem (gUsbBusTempBuffer, gTempBufferPages << 12, 0);
			} else {
                gUsbBusTempBuffer = NULL;
                gTempBufferPages = 0;
			}
		}
        if (gUsbBusTempBuffer) {
		    DataBuffer = gUsbBusTempBuffer;
        }
	}

	if (DataBuffer != Buffer) {
        if (!(XferDir & BIT7)) {
		    gBS->CopyMem(DataBuffer, Buffer, Length);
        }
	}
	
    ReturnValue = UsbSmiHc(opHC_BulkTransfer,
      HcStruc->bHCType, HcStruc, DevInfo, XferDir, DataBuffer, Length);
    
	if (DataBuffer != Buffer) {
        if (XferDir & BIT7) {
            gBS->CopyMem(Buffer, DataBuffer, Length);
        }
	}

	return (UINT32)ReturnValue;
}

UINT16
UsbSmiInterruptTransfer (
    HC_STRUC*   HcStruc,
    DEV_INFO*   DevInfo,
    UINT8       EndpointAddress,
    UINT16      MaxPktSize,
    UINT8       *Buffer,
    UINT16      Length
)
{
	EFI_STATUS	Status;
	UINT8		*DataBuffer = Buffer;
	UINTN		ReturnValue;

	if (Shr64((UINTN)Buffer, 32)) {
		if (gUsbBusTempBuffer == NULL || Length > (gTempBufferPages << 12)) {
			if (gUsbBusTempBuffer) {
				gBS->FreePages((EFI_PHYSICAL_ADDRESS)gUsbBusTempBuffer, gTempBufferPages);
				gUsbBusTempBuffer = NULL;
				gTempBufferPages = 0;
			}
			gTempBufferPages = EFI_SIZE_TO_PAGES(Length);
			gUsbBusTempBuffer = (UINT8*)0xFFFFFFFF;
			Status = gBS->AllocatePages(AllocateMaxAddress, EfiBootServicesData,
							gTempBufferPages, (EFI_PHYSICAL_ADDRESS*)&gUsbBusTempBuffer);
			if (!EFI_ERROR(Status)) {
				gBS->SetMem (gUsbBusTempBuffer, gTempBufferPages << 12, 0);
			} else {
                gUsbBusTempBuffer = NULL;
                gTempBufferPages = 0;
			}
		}
        if (gUsbBusTempBuffer) {
		    DataBuffer = gUsbBusTempBuffer;
        }
	}

	if (DataBuffer != Buffer) {
        if (EndpointAddress & BIT7) {
		    gBS->CopyMem(DataBuffer, Buffer, Length);
        }
	}

    ReturnValue = UsbSmiHc(opHC_InterruptTransfer,
        HcStruc->bHCType, HcStruc, DevInfo, EndpointAddress, MaxPktSize, Length);
    
	if (DataBuffer != Buffer) {
        if (EndpointAddress & BIT7) {
		    gBS->CopyMem(Buffer, DataBuffer, Length);
        }
	}

	return (UINT8)ReturnValue;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    UsbSmiActivatePolling, UsbSmiDeactivatePolling
//
// Description:
//  Wrappers for calling USB HC driver USB polling functions
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

UINT8 UsbSmiDeactivatePolling (HC_STRUC* HostController, DEV_INFO* DevInfo )
{
    return (UINT8)UsbSmiHc( opHC_DeactivatePolling,
            HostController->bHCType, HostController, DevInfo );
}

UINT8   UsbSmiActivatePolling (HC_STRUC* HostController, DEV_INFO* DevInfo )
{
    return (UINT8)UsbSmiHc( opHC_ActivatePolling,
            HostController->bHCType, HostController, DevInfo );
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        ConvertBitmaps
//
// Description:
//  Converts one bit-strng to another using a convertion table
//
// Input:
//  Val - intial 32 bit wide bit-string
//  BitT- array of bitmaptable_t recodrds
//  Cnt - number of records in array BitT
//
// Output:
//  32-bit wide bit-string - result of conversion applied to Val
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

struct BITMAPTABLE_T {unsigned int src; unsigned int dst;};

UINT32
ConvertBitmaps(
    UINT32 Val,
    struct BITMAPTABLE_T* BitT,
    UINT32 Cnt
)
{
    UINT32 Res = 0;
    UINT32 i;
    for( i=0; (BitT->src !=0)&& ( i < Cnt); ++i, BitT++ ){
        if( BitT->src & Val )
            Res |= BitT->dst;
    }
    return Res;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        SearchDevinfoByAdr
//
// Description:
//  Enumerates DEV_INFO structures in aDevInfoTable array of USB data to find
//  one that matches the specified USB address and connects to a specified USB
//  host controller.
//
// Input:
//  DEV_INFO    Start Pointer to the device info structure from
//              where the search begins (if 0 start from first entry)
//  DevAddr    Device address
//  HcStruc    Pointer to the HCStruc structure
//
// Output:
//  Pointer to DEV_INFO structure, NULL if device is not found
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
SearchDevinfoByAdr(
    DEV_INFO*   Start,
    UINT8       DevAddr,
    HC_STRUC*   HcStruc
)
{
    DEV_INFO*   DevInfo;
    DEV_INFO*   Dev = gUsbData->aDevInfoTable + COUNTOF(gUsbData->aDevInfoTable);

    for ( DevInfo = Start!=NULL?Start:&gUsbData->aDevInfoTable[1];
            DevInfo != Dev; ++DevInfo ){
        if ((DevInfo->Flag & (DEV_INFO_VALID_STRUC | DEV_INFO_DEV_DUMMY)) ==
			DEV_INFO_VALID_STRUC) {
            if(( gUsbData->HcTable[DevInfo->bHCNumber - 1] == HcStruc) &&
                ( DevInfo->bDeviceAddress == DevAddr )){
                    return DevInfo;
            }
        }
    }
    return NULL;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        DevAddr2Info
//
// Description:
//  Returns a DEV_INFO that corresponds to a device that is connected to a
//  specified host controller and has a specified address
//
// Input:
//  Addr    Device address
//  Hc      Pointer to the HCStruc structure
//
// Output:
//  Pointer to DEV_INFO structure, NULL if device is not found
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
DevAddr2Info(
    UINT8 Addr,
    HC_STRUC* Hc
)
{
    DEV_INFO* Dev = SearchDevinfoByAdr(NULL, Addr, Hc);
    USB_DEBUG(DEBUG_USBHC_LEVEL8,
        "\tDevAddr2Info %x -> %x(hc:%x;hub:%x;port:%x;if:%x)\n",
            Addr, Dev, Dev->bHCNumber, Dev->bHubDeviceNumber,
            Dev->bHubPortNumber, Dev->bInterfaceNum );
    return Dev;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        FindOldTransfer
//
// Description:
//  Searches a DEV_INFO that was used as a temporary structure for the USB transfer.
//
// Input:
//  Device address
//  EndPoint   Interrupt Endpoint number that was assigned to temporary structure
//  HCStruc    Pointer to the HCStruc structure
//
// Output:
//  Pointer to DeviceInfo Structure NULL if device is not found
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
FindOldTransfer(
    UINT8 DevAddr,
    UINT8 EndPoint,
    HC_STRUC* HcStruc
)
{
    DEV_INFO *Dev;
    DEV_INFO *LastDev = gUsbData->aDevInfoTable + 
                            COUNTOF(gUsbData->aDevInfoTable);

    for ( Dev = &gUsbData->aDevInfoTable[1]; Dev != LastDev; ++Dev ){
        if( ((Dev->Flag & (DEV_INFO_VALID_STRUC|DEV_INFO_DEV_PRESENT) )==
            (DEV_INFO_VALID_STRUC|DEV_INFO_DEV_PRESENT)) &&
            (Dev->bHCNumber == HcStruc->bHCNumber ) &&
            (Dev->bDeviceAddress == DevAddr ) &&
            (Dev->IntInEndpoint == EndPoint ))
        {
            return Dev;
        }
    }
    return NULL;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AllocDevInfo
//
// Description:
//  Allocates temporary DEV_INFO structure in USB data area for use in USB transfer.
//
// Output:
//  Pointer to a DEV_INFO structure
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

DEV_INFO*
AllocDevInfo()
{
    DEV_INFO* Dev = UsbAllocDevInfo();
    ASSERT(Dev);
    if(Dev){
        Dev->bDeviceType = BIOS_DEV_TYPE_USBBUS_SHADOW;
        Dev->Flag |= DEV_INFO_DEV_DUMMY;
    }
    return Dev;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        FreeDevInfo
//
// Description:
//  Marks DEV_INFO structure that it is free for use in consequent operations.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID
FreeDevInfo(
    DEV_INFO* Dev
)
{
    ASSERT(Dev);
    if (Dev) {
      Dev->Flag &= ~(DEV_INFO_VALID_STRUC |  DEV_INFO_DEV_PRESENT | DEV_INFO_DEV_DUMMY);
    }
}

UINT8
TranslateInterval(
    UINT8   Speed,
    UINTN   Interval
)
{
    UINT8  BitCount = 0;

    if (Speed == EFI_USB_SPEED_LOW ||
        Speed == EFI_USB_SPEED_FULL) {
        return (UINT8)Interval;
    }

    for (BitCount = 0; Interval != 0; BitCount++) {
        Interval >>= 1;
    }
    return (BitCount + 2);
}

/////////////////////////////////////////////////////////////////////////////
//                  USB Host Controller API functions
/////////////////////////////////////////////////////////////////////////////

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcReset
//
// Description:
//  Provides software reset for the USB host controller.
//
// Input:
//  This        A pointer to the EFI_USB_HC_PROTOCOL instance. Type
//              EFI_USB_HC_PROTOCOL is defined in Section 14.1.
//  Attributes  A bit mask of the reset operation to perform.
//
// Output:
//  EFI_SUCCESS             The reset operation succeeded.
//  EFI_INVALID_PARAMETER   Attributes is not valid.
//  EFI_UNSUPPORTED         The type of reset specified by Attributes is
//                          not currently supported by the host controller
//                          hardware.
//  EFI_DEVICE_ERROR        An error was encountered while attempting to
//                          perform the reset operation.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcReset(
    IN EFI_USB_HC_PROTOCOL *This,
    IN UINT16 attributes
)
{
    PROGRESS_CODE(DXE_USB_RESET);

    if (!(attributes & (EFI_USB_HC_RESET_GLOBAL | EFI_USB_HC_RESET_HOST_CONTROLLER)))
        return EFI_INVALID_PARAMETER;

    USB_DEBUG(DEBUG_USBHC_LEVEL, "AmiUsbHcReset:");

    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcGetState
//
// Description:
//  Protocol USB HC function that returns Host Controller state.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsbHcGetState(
    IN EFI_USB_HC_PROTOCOL  *This,
    OUT EFI_USB_HC_STATE *State
)
{
    return AmiUsb2HcGetState(&GetThis1(This)->hcprotocol2, State);
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsbHcSetState
//
// Description:
//  Protocol USB HC function that sets Host Controller state.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsbHcSetState(
    IN EFI_USB_HC_PROTOCOL *This,
    IN EFI_USB_HC_STATE State
)
{
    USB_DEBUG(DEBUG_USBHC_LEVEL, "USB HC:\t\tsetState, %d\n", State);
    return AmiUsb2HcSetState(&GetThis1(This)->hcprotocol2, State);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsbHcTransfer
//
// Description:
//  Protocol USB HC function that performs USB transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcControlTransfer(
    IN EFI_USB_HC_PROTOCOL          *HcProtocol,
    IN     UINT8                    DeviceAddress,
    IN     BOOLEAN                  IsSlowDevice,
    IN     UINT8                    MaximumPacketLength,
    IN     EFI_USB_DEVICE_REQUEST   *Request,
    IN     EFI_USB_DATA_DIRECTION   TransferDirection,
    IN OUT VOID                     *Data,
    IN OUT UINTN                    *DataLength,
    IN     UINTN                    Timeout,
    OUT    UINT32                   *TransferResult
)
{
    return AmiUsb2HcControlTransfer(&GetThis1(HcProtocol)->hcprotocol2,
        DeviceAddress,
        IsSlowDevice?EFI_USB_SPEED_LOW:EFI_USB_SPEED_FULL,
        MaximumPacketLength, Request, TransferDirection, Data,
        DataLength, Timeout, NULL, TransferResult);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcBulkTransfer
//
// Description:
//  Protocol USB HC function that performs USB bulk transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI AmiUsbHcBulkTransfer(
    IN EFI_USB_HC_PROTOCOL  *HcProtocol,
    IN  UINT8               DeviceAddress,
    IN  UINT8               EndpointAddress,
    IN  UINT8               MaximumPacketLength,
    IN OUT VOID             *Data,
    IN OUT UINTN            *DataLength,
    IN OUT UINT8            *DataToggle,
    IN  UINTN               Timeout,
    OUT UINT32              *TransferResult
)
{
    //
    // Check for valid maximum packet length is 8, 16, 32 or 64
    //
    if ( MaximumPacketLength != 8 &&
         MaximumPacketLength != 16 &&
         MaximumPacketLength != 32 &&
         MaximumPacketLength != 64 ) return EFI_INVALID_PARAMETER;

    return AmiUsb2HcBulkTransfer(&GetThis1(HcProtocol)->hcprotocol2,
        DeviceAddress, EndpointAddress, EFI_USB_SPEED_FULL,
        MaximumPacketLength, 1, &Data, DataLength,
        DataToggle, Timeout, NULL, TransferResult);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcAsyncInterruptTransfer
//
// Description:
//  Protocol USB HC function that performs USB async interrupt transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcAsyncInterruptTransfer(
  IN EFI_USB_HC_PROTOCOL                  *HcProtocol,
  IN     UINT8                            DeviceAddress,
  IN     UINT8                            EndpointAddress,
  IN     BOOLEAN                          IsSlowDevice,
  IN     UINT8                            MaxPacket,
  IN     BOOLEAN                          IsNewTransfer,
  IN OUT UINT8                            *DataToggle,
  IN     UINTN                            PollingInterval  ,
  IN     UINTN                            DataLength,
  IN     EFI_ASYNC_USB_TRANSFER_CALLBACK  CallbackFunction ,
  IN     VOID                             *Context)
{
    return AmiUsb2HcAsyncInterruptTransfer(
            &GetThis1(HcProtocol)->hcprotocol2,
            DeviceAddress, EndpointAddress,
            IsSlowDevice?EFI_USB_SPEED_LOW:EFI_USB_SPEED_FULL,
            MaxPacket, IsNewTransfer, DataToggle,
            PollingInterval, DataLength, NULL, CallbackFunction, Context);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcSyncInterruptTransfer
//
// Description:
//  Protocol USB HC function that performs USB sync interrupt transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
EFIAPI
AmiUsbHcSyncInterruptTransfer(
  IN EFI_USB_HC_PROTOCOL    *HcProtocol,
  IN     UINT8              DeviceAddress,
  IN     UINT8              EndpointAddress,
  IN     BOOLEAN            IsSlowDevice,
  IN     UINT8              MaximumPacketLength,
  IN OUT VOID               *Data,
  IN OUT UINTN              *DataLength,
  IN OUT UINT8              *DataToggle,
  IN     UINTN              Timeout,
  OUT    UINT32             *TransferResult
)
{
    return AmiUsb2HcSyncInterruptTransfer(
            &GetThis1(HcProtocol)->hcprotocol2,
            DeviceAddress, EndpointAddress,
            IsSlowDevice?EFI_USB_SPEED_LOW:EFI_USB_SPEED_FULL,
            MaximumPacketLength,
            Data, DataLength, DataToggle, Timeout, NULL, TransferResult );
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcIsochronousTransfer
//
// Description:
//  Protocol USB HC function that performs USB sync isochronous transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcIsochronousTransfer(
  IN EFI_USB_HC_PROTOCOL    *This,
  IN     UINT8              DeviceAddress,
  IN     UINT8              EndpointAddress,
  IN     UINT8              MaximumPacketLength,
  IN OUT VOID               *Data,
  IN OUT UINTN              DataLength,
  OUT    UINT32             *TransferResult
)
{
    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcAsyncIsochronousTransfer
//
// Description:
//  Protocol USB HC function that performs USB async isochronous transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcAsyncIsochronousTransfer(
  IN EFI_USB_HC_PROTOCOL              *This,
  IN     UINT8                        DeviceAddress,
  IN     UINT8                        EndpointAddress,
  IN     UINT8                        MaximumPacketLength,
  IN OUT VOID                         *Data,
  IN     UINTN                        DataLength,
  IN EFI_ASYNC_USB_TRANSFER_CALLBACK  IsochronousCallback,
  IN VOID                             *Context
  )
{
    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcGetRootHubPortNumber
//
// Description:
//  Protocol USB HC function that returns the number of ports of a root hub
//  on a given controller.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcGetRootHubPortNumber (
    IN EFI_USB_HC_PROTOCOL *HcProtocol,
    OUT UINT8 *PortNumber
)
{
    HC_DXE_RECORD *This = GetThis1(HcProtocol);

    USB_DEBUG(DEBUG_USBHC_LEVEL,
        "USB HC:\t\tget_roothub_port_number\n");

    if (PortNumber == NULL) return EFI_INVALID_PARAMETER;

    *PortNumber = This->hc_data->bNumPorts;

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcGetRootHubPortStatus
//
// Description:
//  Protocol USB HC function that returns the root port status
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcGetRootHubPortStatus (
    EFI_USB_HC_PROTOCOL *HcProtocol,
    UINT8               PortNumber,
    EFI_USB_PORT_STATUS *PortStatus
)
{
    HC_DXE_RECORD *This = GetThis1( HcProtocol );

    static struct BITMAPTABLE_T MapTable[] = {
        {USB_PORT_STAT_DEV_CONNECTED,USB_PORT_STAT_CONNECTION},
        {USB_PORT_STAT_DEV_LOWSPEED,USB_PORT_STAT_LOW_SPEED},
        {USB_PORT_STAT_DEV_SUPERSPEED, USB_PORT_STAT_SUPER_SPEED},
        {USB_PORT_STAT_DEV_SUPERSPEED_PLUS, USB_PORT_STAT_SUPER_SPEED},
        {USB_PORT_STAT_DEV_ENABLED, USB_PORT_STAT_ENABLE},
        {USB_PORT_STAT_DEV_OWNER, USB_PORT_STAT_OWNER}
    };
    static struct BITMAPTABLE_T MapTable2[] = {
        {USB_PORT_STAT_DEV_CONNECT_CHANGED,USB_PORT_STAT_C_CONNECTION},
    };

    UINT8   Status1;

    USB_DEBUG(DEBUG_USBHC_LEVEL, "USB HC:\t\tget_roothub_port_status\n" );

    if (PortStatus == NULL || PortNumber >= This->hc_data->bNumPorts)
        return EFI_INVALID_PARAMETER;

    Status1 = UsbSmiGetRootHubStatus(This->hc_data,(UINT8)PortNumber + 1, TRUE);

    PortStatus->PortStatus = (UINT16)ConvertBitmaps(
            Status1, MapTable, COUNTOF(MapTable));
    PortStatus->PortChangeStatus = (UINT16)ConvertBitmaps(
            Status1, MapTable2, COUNTOF(MapTable2)) ;

    if (((Status1 & USB_PORT_STAT_DEV_CONNECTED) != 0) &&
        (Status1 & (USB_PORT_STAT_DEV_LOWSPEED | USB_PORT_STAT_DEV_FULLSPEED
                | USB_PORT_STAT_DEV_SUPERSPEED | USB_PORT_STAT_DEV_SUPERSPEED_PLUS))==0) {
        PortStatus->PortStatus |= USB_PORT_STAT_HIGH_SPEED;
    }

    USB_DEBUG(DEBUG_USBHC_LEVEL, "\t\tStatus=%x, PortStatus=%x, PortChangeStatus=%x\n",
        Status1, PortStatus->PortStatus, PortStatus->PortChangeStatus );

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:  AmiUsbHcSetRootHubPortFeature
//
// Description:
//  Protocol USB HC function set root hub port feature
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcSetRootHubPortFeature(
    IN EFI_USB_HC_PROTOCOL  *HcProtocol,
    IN UINT8                PortNumber,
    IN EFI_USB_PORT_FEATURE PortFeature
)
{
    HC_DXE_RECORD *This = GetThis1(HcProtocol);

    USB_DEBUG(DEBUG_USBHC_LEVEL, "USB HC:\t\tset_roothub_port_feature\n" );

    if ( PortNumber >= This->hc_data->bNumPorts)
        return EFI_INVALID_PARAMETER;

    switch( PortFeature ){
        case EfiUsbPortEnable:
            UsbSmiEnableRootHub(This->hc_data, PortNumber + 1);
            break;
    
        default:
            return EFI_INVALID_PARAMETER;
    }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsbHcClearRootHubPortFeature
//
// Description:
//  Protocol USB HC function clear root hub port feature
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsbHcClearRootHubPortFeature(
    IN EFI_USB_HC_PROTOCOL    *HcProtocol,
    IN UINT8                  PortNumber,
    IN EFI_USB_PORT_FEATURE   PortFeature
)
{
    HC_DXE_RECORD *This = GetThis1(HcProtocol);

    USB_DEBUG(DEBUG_USBHC_LEVEL, "USB HC:\t\tclear_roothub_port_feature\n");

    if (PortNumber >= This->hc_data->bNumPorts) return EFI_INVALID_PARAMETER;

    switch (PortFeature ) {
        case EfiUsbPortEnable:
            UsbSmiDisableRootHub(This->hc_data, PortNumber + 1);
            break;
    
        default:
            return EFI_INVALID_PARAMETER;
    }
    return EFI_SUCCESS;
}


/////////////////////////////////////////////////////////////////////////////
//                  USB2 Host Controller API functions
/////////////////////////////////////////////////////////////////////////////

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsb2HcGetCapability
//
// Description:
//  This is USB2HC API to get the host controller capability.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsb2HcGetCapability(
  IN  EFI_USB2_HC_PROTOCOL  *This,
  OUT UINT8                 *MaxSpeed,
  OUT UINT8                 *PortNumber,
  OUT UINT8                 *Is64BitCapable
)
{
    HC_DXE_RECORD *Rec = GetThis(This);

    if (MaxSpeed == NULL || PortNumber == NULL || Is64BitCapable == NULL)
        return EFI_INVALID_PARAMETER;
    *Is64BitCapable = FALSE;    // Driver currently does not support >4GB allocations
                                // even if controller does
                                        //(EIP81612)>
    //*MaxSpeed = (Rec->hc_data->bHCType==USB_HC_EHCI)?
    //                EFI_USB_SPEED_HIGH : EFI_USB_SPEED_FULL;
    switch (Rec->hc_data->bHCType) {
        case USB_HC_OHCI:
        case USB_HC_UHCI:
            *MaxSpeed = EFI_USB_SPEED_FULL;
            break;
        case USB_HC_EHCI:
            *MaxSpeed = EFI_USB_SPEED_HIGH;
            break;
        case USB_HC_XHCI:
            *MaxSpeed = EFI_USB_SPEED_SUPER;
            break;
    }
                                        //<(EIP81612)

    *PortNumber = Rec->hc_data->bNumPorts;

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsb2HcReset
//
// Description:
//  This is USB2HC API to perform host controller reset.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsb2HcReset(
  IN EFI_USB2_HC_PROTOCOL   *This,
  IN UINT16                 Attributes
)
{
    PROGRESS_CODE(DXE_USB_RESET);

    if (Attributes == 0 || (Attributes & ~(EFI_USB_HC_RESET_GLOBAL
        | EFI_USB_HC_RESET_HOST_CONTROLLER | EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
        | EFI_USB_HC_RESET_HOST_WITH_DEBUG))==0 )
    {
        return EFI_INVALID_PARAMETER;
    }

    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsb2HcGetState
//
// Description:
//  This is USB2HC API to get the host controller state.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsb2HcGetState(
  IN  EFI_USB2_HC_PROTOCOL    *This,
  OUT EFI_USB_HC_STATE        *State
)
{
    HC_DXE_RECORD *Rec = GetThis(This);
    
    if (State == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    if (Rec->hc_data->dHCFlag & HC_STATE_RUNNING) {
        *State = EfiUsbHcStateOperational;
        return EFI_SUCCESS;
    }

    if (Rec->hc_data->dHCFlag & HC_STATE_SUSPEND) {
        *State = EfiUsbHcStateSuspend;
        return EFI_SUCCESS;
    }

    *State = EfiUsbHcStateHalt;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:    AmiUsb2HcSetState
//
// Description:
//  This is USB2HC API to set the host controller state.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
AmiUsb2HcSetState(
  IN EFI_USB2_HC_PROTOCOL    *This,
  IN EFI_USB_HC_STATE        State
)
{
    HC_DXE_RECORD       *Rec = GetThis(This);
    EFI_USB_HC_STATE    CurrentState;
    EFI_STATUS          Status = EFI_SUCCESS;
    UINT8               UsbStatus = USB_SUCCESS;

    Status = AmiUsb2HcGetState(This, &CurrentState);

    if (EFI_ERROR(Status)) {
        return EFI_DEVICE_ERROR;
    }

    if (CurrentState == State) {
        return Status;
    }

    switch (State) {
        case EfiUsbHcStateHalt:
            UsbStatus = (UINT8)UsbSmiHc(opHC_Stop, Rec->hc_data->bHCType, Rec->hc_data);
            break;
        case EfiUsbHcStateOperational:
            UsbStatus = (UINT8)UsbSmiHc(opHC_Start, Rec->hc_data->bHCType, Rec->hc_data);
            break;
        case EfiUsbHcStateSuspend:
            UsbStatus = (UINT8)UsbSmiHc(opHC_GlobalSuspend, Rec->hc_data->bHCType, Rec->hc_data);
            break;
        default:
            Status = EFI_INVALID_PARAMETER;
            break;
    }

    if (UsbStatus != USB_SUCCESS) {
        Status = EFI_DEVICE_ERROR;
    }
    
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AsyncInterruptOnTimer
//
// Description:
//  This function checks if queue has a new transfer. If yes, calls a
//  callback with data from transfer.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID EFIAPI
AsyncInterruptOnTimer (
    EFI_EVENT   Event,
    VOID        *Ctx
)
{
    USBHC_INTERRUPT_DEVNINFO_T *Idi = (USBHC_INTERRUPT_DEVNINFO_T*)Ctx;
    UINT8   Lock;
    VOID    *Data;

    //
    // Check re-entrance
    //
    ATOMIC({Lock = Idi->Lock; Idi->Lock = 1;});
    if(Lock) {
        return; //control is already inside
    }

    while((UINTN)QueueSize(&Idi->QCompleted) >= Idi->DataLength ){
    ATOMIC(Data = QueueRemoveMsg( &Idi->QCompleted, (int)Idi->DataLength ));
        //TRACE((-1,"USBHC: AsyncInterruptOnTimer: calling callback...\n"));
        Idi->CallbackFunction(
            Data, Idi->DataLength, Idi->Context, EFI_USB_NOERROR);
    }

    Idi->Lock = 0;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcBulkTransfer
//
// Description:
//  This function performs USB2 HC Bulk Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcBulkTransfer(
    IN EFI_USB2_HC_PROTOCOL *HcProtocol,
    IN  UINT8       DeviceAddress,
    IN  UINT8       EndpointAddress,
    IN  UINT8       DeviceSpeed,
    IN  UINTN       MaximumPacketLength,
    IN  UINT8       DataBuffersNumber,
    IN OUT VOID     *Data[EFI_USB_MAX_BULK_BUFFER_NUM],
    IN OUT UINTN    *DataLength,
    IN OUT UINT8    *DataToggle,
    IN  UINTN       Timeout,
    IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    OUT UINT32      *TransferResult
)
{
    UINT32  SmiRes;
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    DEV_INFO* DevInfo;
    DEV_INFO* DevSrc = DevAddr2Info( DeviceAddress, This->hc_data );
    UINT8 XferDir = 0;
    UINT16 CurrentTimeout;
	UINT8 ToggleBit = (EndpointAddress & 0xF) - 1;

    //
    // Check Params
    //
                                        //(EIP81612)>
    if( DevSrc == NULL || Data == NULL || Data[0] == NULL ||
        (*DataToggle != 0 && *DataToggle != 1) ||
        (DeviceSpeed != EFI_USB_SPEED_SUPER && DeviceSpeed != EFI_USB_SPEED_HIGH 
        && DeviceSpeed != EFI_USB_SPEED_FULL) ||
        *DataLength == 0 || TransferResult == NULL ||
        MaximumPacketLength == 0 ) {
        return EFI_INVALID_PARAMETER;
    }

    //
    // Check for valid maximum packet length
    //
    if ( DeviceSpeed == EFI_USB_SPEED_SUPER && MaximumPacketLength > 1024 ) {
        return EFI_INVALID_PARAMETER;
    }
                                        //<(EIP81612)
    if ( DeviceSpeed == EFI_USB_SPEED_HIGH && MaximumPacketLength > 512 ) {
        return EFI_INVALID_PARAMETER;
    }
    if ( DeviceSpeed == EFI_USB_SPEED_FULL && MaximumPacketLength > 64 ) {
        return EFI_INVALID_PARAMETER;
    }

    //
    // Alloc DEV_INFO
    //
    DevInfo = AllocDevInfo();

    if(DevInfo == NULL) {
        return EFI_OUT_OF_RESOURCES;
    }

    //
    // Fill DEV_INFO
    //
    DevInfo->bDeviceAddress = DeviceAddress;
    DevInfo->bHCNumber = This->hc_data->bHCNumber;
    DevInfo->bEndpointSpeed = SpeedMap[DeviceSpeed];
    DevInfo->bHubDeviceNumber = DevSrc->bHubDeviceNumber;
    DevInfo->bHubPortNumber = DevSrc->bHubPortNumber;
    DevInfo->DevMiscInfo = DevSrc->DevMiscInfo;	//(EIP84790+)

    if( EndpointAddress & 0x80 ){
        XferDir = 0x80;
        DevInfo->bBulkInEndpoint = EndpointAddress & 0xF;
        DevInfo->wBulkInMaxPkt = (UINT16)MaximumPacketLength;
        DevInfo->wDataInSync = (UINT16)(*DataToggle) << ToggleBit;
    } else {
        XferDir = 0x0;
        DevInfo->bBulkOutEndpoint = EndpointAddress & 0xF;
        DevInfo->wBulkOutMaxPkt = (UINT16)MaximumPacketLength;
        DevInfo->wDataOutSync = (UINT16)(*DataToggle) << ToggleBit;
    }

    //
    // Call SMI routine and retrieve last status
    // if any error
    //
    CRITICAL_CODE( EFI_TPL_NOTIFY,
    {
        CurrentTimeout = gUsbData->wTimeOutValue;
        gUsbData->wTimeOutValue = (UINT16)Timeout;
        gUsbData->dLastCommandStatusExtended = 0;
        SmiRes = UsbSmiBulkTransfer(
            This->hc_data, DevInfo,
            XferDir,
            (UINT8*)Data[0],
            (UINT32)*DataLength );
        *TransferResult = GetTransferStatus();
        gUsbData->wTimeOutValue = CurrentTimeout;
    });

    //
    // Update the data length
    //
    *DataLength = (UINTN)SmiRes;

    //
    // Update Toggle bit
    //
    if( XferDir ){
        *DataToggle = (UINT8)(DevInfo->wDataInSync >> ToggleBit) & 0x1;
    } else {
        *DataToggle = (UINT8)(DevInfo->wDataOutSync >> ToggleBit) & 0x1;
    }
    FreeDevInfo(DevInfo);

    if( (*TransferResult) &  EFI_USB_ERR_TIMEOUT ) {
        return EFI_TIMEOUT;
    }
    return (*TransferResult)? EFI_DEVICE_ERROR:EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcAsyncInterruptTransfer
//
// Description:
//  This function performs USB2 HC Async Interrupt Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcAsyncInterruptTransfer(
    IN EFI_USB2_HC_PROTOCOL *HcProtocol,
    IN UINT8                DeviceAddress,
    IN UINT8                EndpointAddress,
    IN UINT8                DeviceSpeed,
    IN UINTN                MaxPacket,
    IN BOOLEAN              IsNewTransfer,
    IN OUT UINT8            *DataToggle,
    IN UINTN                PollingInterval,
    IN UINTN                DataLength,
    IN     EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    IN EFI_ASYNC_USB_TRANSFER_CALLBACK  CallbackFunction ,
    IN VOID                 *Context)
{
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    DEV_INFO* DevInfo;
    USBHC_INTERRUPT_DEVNINFO_T* AsyncTransfer = 0;
    UINT8 SmiStatus = USB_SUCCESS;
	UINT8 ToggleBit = (EndpointAddress & 0xF) - 1;

    if (DeviceSpeed != EFI_USB_SPEED_SUPER && 
		DeviceSpeed != EFI_USB_SPEED_HIGH &&
		DeviceSpeed != EFI_USB_SPEED_FULL &&
		DeviceSpeed != EFI_USB_SPEED_LOW){
		return EFI_INVALID_PARAMETER;
    }

#if !EFI_USB_HC_INTERRUPT_OUT_SUPPORT
    if (!(EndpointAddress & BIT7)) {
		return EFI_INVALID_PARAMETER;
    }
#endif
                                        //<(EIP81612)
    if (IsNewTransfer){
        DEV_INFO* DevInfoSrc = DevAddr2Info( DeviceAddress, This->hc_data );
        DevInfo = FindOldTransfer( DeviceAddress, EndpointAddress, This->hc_data );

		if (DataLength == 0) {
			 return EFI_INVALID_PARAMETER;
		}

	    if (*DataToggle != 0 && *DataToggle != 1) {
	        return EFI_INVALID_PARAMETER;
	    }

		if (PollingInterval < 1 || PollingInterval > 255) {
			return EFI_INVALID_PARAMETER;
		}
	
        if( DevInfoSrc == NULL || CallbackFunction == NULL) {
            return EFI_INVALID_PARAMETER;
        }
        if( DevInfo != NULL ){
            USB_DEBUG(DEBUG_LEVEL_3,"Stacked AsyncInterrupt request are not supported\n");
            return EFI_INVALID_PARAMETER;
        }
        DevInfo = AllocDevInfo();
        *DevInfo = *DevInfoSrc;
        DevInfo->IntInEndpoint = EndpointAddress;
        DevInfo->bEndpointSpeed = SpeedMap[DeviceSpeed];
        DevInfo->IntInMaxPkt = (UINT16)MaxPacket;    //(EIP84790+)
        DevInfo->bPollInterval = TranslateInterval(DeviceSpeed, PollingInterval);
        DevInfo->PollingLength = (UINT16)DataLength;

        //create new transfer
        gBS->AllocatePool (EfiBootServicesData,
            sizeof(USBHC_INTERRUPT_DEVNINFO_T) + DataLength*INTERRUPTQUEUESIZE,
            &AsyncTransfer );
        EfiZeroMem(AsyncTransfer, sizeof(USBHC_INTERRUPT_DEVNINFO_T)+
                    DataLength*INTERRUPTQUEUESIZE);
        DevInfo->pExtra = (UINT8*)AsyncTransfer;
        AsyncTransfer->QCompleted.data = (VOID *volatile *)AsyncTransfer->Data;
        AsyncTransfer->QCompleted.maxsize = (int)DataLength * INTERRUPTQUEUESIZE;
        AsyncTransfer->DataLength = DataLength;
        AsyncTransfer->EndpointAddress = EndpointAddress;

        DevInfo->Flag |= DEV_INFO_DEV_DUMMY;
        VERIFY_EFI_ERROR(
            gBS->CreateEvent ( EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,
            EFI_TPL_CALLBACK, AsyncInterruptOnTimer,
            AsyncTransfer,&AsyncTransfer->Event));

		PollingInterval = PollingInterval < 32 ? 32 : PollingInterval;
        VERIFY_EFI_ERROR(
            gBS->SetTimer ( AsyncTransfer->Event, TimerPeriodic,
                PollingInterval * MILLISECOND));
        AsyncTransfer->CallbackFunction = CallbackFunction;
        AsyncTransfer->Context = Context;
		if(EndpointAddress & 0x80) {
	        DevInfo->wDataInSync = (UINT16)(*DataToggle) << ToggleBit;
		} else {
			DevInfo->wDataOutSync = (UINT16)(*DataToggle) << ToggleBit;
		}

        //
        // Activate transfer
        //
        SmiStatus = UsbSmiActivatePolling(This->hc_data, DevInfo);
        ASSERT(SmiStatus==USB_SUCCESS);
    } else {
        //
        // Find old transfer
        //
        DevInfo = FindOldTransfer(
                    DeviceAddress,
                    EndpointAddress,
                    This->hc_data );
        if( DevInfo == NULL  || DevInfo->pExtra == NULL ){
            USB_DEBUG(DEBUG_LEVEL_3,"Canceling bad AsyncInterrupt request\n");
            return EFI_INVALID_PARAMETER;
        }
        AsyncTransfer = (USBHC_INTERRUPT_DEVNINFO_T*)DevInfo->pExtra;
        DevInfo->pExtra = 0;
        // 
        // Deactivate transfer
        //
        SmiStatus = UsbSmiDeactivatePolling (This->hc_data, DevInfo);
        if (DataToggle){
			if(EndpointAddress & 0x80) {
				*DataToggle = (UINT8)(DevInfo->wDataInSync >> ToggleBit) & 0x1;
			} else {
				*DataToggle = (UINT8)(DevInfo->wDataOutSync >> ToggleBit) & 0x1;
			}
        }
        VERIFY_EFI_ERROR(gBS->SetTimer(AsyncTransfer->Event, TimerCancel, 0));
        VERIFY_EFI_ERROR(gBS->CloseEvent (AsyncTransfer->Event));
        gBS->FreePool(AsyncTransfer);
        FreeDevInfo(DevInfo);
    }

    return SmiStatus == USB_SUCCESS? EFI_SUCCESS : EFI_DEVICE_ERROR;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcSyncInterruptTransfer
//
// Description:
//  This function performs USB2 HC Sync Interrupt Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcSyncInterruptTransfer(
    IN EFI_USB2_HC_PROTOCOL    *HcProtocol,
    IN     UINT8                DeviceAddress,
    IN     UINT8                EndpointAddress,
    IN     UINT8                DeviceSpeed,
    IN     UINTN                MaximumPacketLength,
    IN OUT VOID                 *Data,
    IN OUT UINTN                *DataLength,
    IN OUT UINT8                *DataToggle,
    IN     UINTN                Timeout,
    IN     EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    OUT    UINT32                *TransferResult  )
{
    UINT16      SmiRes;
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    DEV_INFO    *DevInfo;
    DEV_INFO*   DevSrc = DevAddr2Info( DeviceAddress, This->hc_data );
    UINT16      CurrentTimeout;
	UINT8		ToggleBit = (EndpointAddress & 0xF) - 1;
	UINT16		*wDataSync;

    if (DeviceSpeed != EFI_USB_SPEED_SUPER && 
		DeviceSpeed != EFI_USB_SPEED_HIGH && 
		DeviceSpeed != EFI_USB_SPEED_FULL && 
		DeviceSpeed != EFI_USB_SPEED_LOW) {
		return EFI_INVALID_PARAMETER;
    }

#if !EFI_USB_HC_INTERRUPT_OUT_SUPPORT
    if (!(EndpointAddress & BIT7)) {
		return EFI_INVALID_PARAMETER;
    }
#endif

    if (Data == NULL || DataLength == NULL ||
		*DataLength == 0 || TransferResult == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    if ( DeviceSpeed == EFI_USB_SPEED_LOW  && MaximumPacketLength > 8) {
        return EFI_INVALID_PARAMETER;
    }

    if (DeviceSpeed == EFI_USB_SPEED_FULL && MaximumPacketLength > 64) {
		return EFI_INVALID_PARAMETER;
    }

    if (DeviceSpeed == EFI_USB_SPEED_HIGH && MaximumPacketLength > 3072 ) {
        return EFI_INVALID_PARAMETER;
    }

	if (*DataToggle != 0 && *DataToggle != 1) return EFI_INVALID_PARAMETER;
	
    if(DevSrc == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    DevInfo = AllocDevInfo();
    DevInfo->bDeviceAddress = DeviceAddress;
    DevInfo->bHCNumber = This->hc_data->bHCNumber;
    DevInfo->bEndpointSpeed = SpeedMap[DeviceSpeed];
    DevInfo->bPollInterval = TranslateInterval(DeviceSpeed, 1);
    DevInfo->bHubDeviceNumber = DevSrc->bHubDeviceNumber;
    DevInfo->bHubPortNumber = DevSrc->bHubPortNumber;
	DevInfo->DevMiscInfo = DevSrc->DevMiscInfo;	//(EIP84790+)

	wDataSync = EndpointAddress & 0x80 ? &DevInfo->wDataInSync : 
		&DevInfo->wDataOutSync;

	*wDataSync = (UINT16)(*DataToggle) << ToggleBit;

    CRITICAL_CODE( EFI_TPL_NOTIFY,
    {
        CurrentTimeout = gUsbData->wTimeOutValue;
        gUsbData->wTimeOutValue = (UINT16)Timeout;
        gUsbData->dLastCommandStatusExtended = 0;
        SmiRes = UsbSmiInterruptTransfer(
            This->hc_data,
            DevInfo,
            EndpointAddress,
            (UINT16)MaximumPacketLength,
            (UINT8*)Data,
            (UINT16)*DataLength);
        *TransferResult = GetTransferStatus();
        gUsbData->wTimeOutValue = CurrentTimeout;
    });

    *DataLength = (UINTN)SmiRes;
    *DataToggle = (UINT8)(*wDataSync >> ToggleBit) & 0x1;

    FreeDevInfo(DevInfo);
    //
    // Return with error or success
    //
    if ( (*TransferResult) &  EFI_USB_ERR_TIMEOUT ) return EFI_TIMEOUT;
    return (*TransferResult)? EFI_DEVICE_ERROR:EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcIsochronousTransfer
//
// Description:
//  This function performs USB2 HC Isochronous Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
EFIAPI
AmiUsb2HcIsochronousTransfer(
    IN     EFI_USB2_HC_PROTOCOL               *This,
    IN     UINT8                              DeviceAddress,
    IN     UINT8                              EndPointAddress,
    IN     UINT8                              DeviceSpeed,
    IN     UINTN                              MaximumPacketLength,
    IN     UINT8                              DataBuffersNumber,
    IN OUT VOID                               *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
    IN     UINTN                              DataLength,
    IN     EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    OUT    UINT32                             *TransferResult
)
{
                                        //(EIP81612)>
    if ( Data == NULL || Data[0] == NULL || DataLength == 0 ||
        (DeviceSpeed != EFI_USB_SPEED_SUPER && DeviceSpeed != EFI_USB_SPEED_HIGH 
        && DeviceSpeed != EFI_USB_SPEED_FULL) ||
        MaximumPacketLength > 1023 ) {
        return EFI_INVALID_PARAMETER;
    }
                                        //<(EIP81612)
    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcAsyncIsochronousTransfer
//
// Description:
//  This function performs USB2 HC Async Isochronous Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
EFIAPI
AmiUsb2HcAsyncIsochronousTransfer(
    IN     EFI_USB2_HC_PROTOCOL               *This,
    IN     UINT8                              DeviceAddress,
    IN     UINT8                              EndPointAddress,
    IN     UINT8                              DeviceSpeed,
    IN     UINTN                              MaximumPacketLength,
    IN     UINT8                              DataBuffersNumber,
    IN OUT VOID                               *Data[EFI_USB_MAX_ISO_BUFFER_NUM],
    IN     UINTN                              DataLength,
    IN     EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    IN     EFI_ASYNC_USB_TRANSFER_CALLBACK    IsochronousCallBack,
    IN     VOID                               *Context OPTIONAL
)
{
                                        //(EIP81612)>
    if( Data == NULL || Data[0] == NULL || DataLength == 0 ||
        (DeviceSpeed != EFI_USB_SPEED_SUPER && DeviceSpeed != EFI_USB_SPEED_HIGH 
        && DeviceSpeed != EFI_USB_SPEED_FULL) ||
        MaximumPacketLength > 1023 ) {
        return EFI_INVALID_PARAMETER;
    }
                                        //<EIP81612)
    return EFI_UNSUPPORTED;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcTransfer
//
// Description:
//  This function performs USB2 HC Control Transfer
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcControlTransfer(
    IN EFI_USB2_HC_PROTOCOL           *HcProtocol,
    IN     UINT8                      DeviceAddress,
    IN     UINT8                      DeviceSpeed,
    IN     UINTN                      MaximumPacketLength,
    IN     EFI_USB_DEVICE_REQUEST     *Request,
    IN     EFI_USB_DATA_DIRECTION     TransferDirection,
    IN OUT VOID                       *Data ,
    IN OUT UINTN                      *DataLength,
    IN     UINTN                      Timeout,
    IN     EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator,
    OUT    UINT32                     *TransferResult
)
{
    UINT16      SmiRes;
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    DEV_INFO    *DevInfo;
    DEV_INFO    *DevSrc = DevAddr2Info( DeviceAddress, This->hc_data );
    UINT16      CurrentTimeout;
    EFI_STATUS  Status;

                                        //(EIP81612)>
    if ( (DeviceSpeed != EFI_USB_SPEED_SUPER && DeviceSpeed != EFI_USB_SPEED_HIGH
            && DeviceSpeed != EFI_USB_SPEED_FULL && DeviceSpeed != EFI_USB_SPEED_LOW)
            || DevSrc == NULL) {
        return EFI_INVALID_PARAMETER;
    }
                                        //<(EIP81612)
    if ( TransferDirection < EfiUsbDataIn || TransferDirection > EfiUsbNoData ) {
        return EFI_INVALID_PARAMETER;
    }

    if ( TransferDirection == EfiUsbNoData && (Data != NULL || *DataLength != 0) ) {
        return EFI_INVALID_PARAMETER;
    }

    if ( TransferDirection != EfiUsbNoData && (Data == NULL || *DataLength == 0) ) {
        return EFI_INVALID_PARAMETER;
    }

    if( Request == NULL || TransferResult == NULL ) {
        return EFI_INVALID_PARAMETER;
    }

    if ( DeviceSpeed == EFI_USB_SPEED_LOW  && MaximumPacketLength != 8 ) {
        return EFI_INVALID_PARAMETER;
    }

    if ((DeviceSpeed == EFI_USB_SPEED_HIGH || DeviceSpeed == EFI_USB_SPEED_FULL) &&
            ( MaximumPacketLength != 8 &&
             MaximumPacketLength != 16 &&
             MaximumPacketLength != 32 &&
             MaximumPacketLength != 64 )
        ) {
            return EFI_INVALID_PARAMETER;
        }
                                        //(EIP81612+)>
    if ( DeviceSpeed == EFI_USB_SPEED_SUPER && MaximumPacketLength != 512 ) {
        return EFI_INVALID_PARAMETER;
    }
                                        //<(EIP81612+)

    DevInfo = AllocDevInfo();
    DevInfo->bDeviceAddress = DeviceAddress;
    DevInfo->bHCNumber = This->hc_data->bHCNumber;
    DevInfo->bEndpointSpeed = SpeedMap[DeviceSpeed];
    DevInfo->wEndp0MaxPacket = (UINT16)MaximumPacketLength;
    DevInfo->bHubDeviceNumber = DevSrc->bHubDeviceNumber;
    DevInfo->bHubDeviceNumber = DevSrc->bHubDeviceNumber;
    DevInfo->bHubPortNumber = DevSrc->bHubPortNumber;
    DevInfo->DevMiscInfo = DevSrc->DevMiscInfo;	//(EIP84790+)

    CRITICAL_CODE( EFI_TPL_NOTIFY,
    {
        CurrentTimeout = gUsbData->wTimeOutValue;
        gUsbData->wTimeOutValue = (UINT16)Timeout;
        gUsbData->dLastCommandStatusExtended = 0;
        SmiRes = UsbSmiControlTransfer(This->hc_data, DevInfo,
            (UINT16)((( TransferDirection == EfiUsbDataIn?1:0) << 7) |
            (((UINT16)Request->RequestType)) |
            (((UINT16)Request->Request)<<8)),
            (UINT16)Request->Index, (UINT16)Request->Value,
            (UINT8*)Data, (UINT16)Request->Length);
        *TransferResult = GetTransferStatus();
        gUsbData->wTimeOutValue = CurrentTimeout;
    });

    Status = EFI_SUCCESS;
/*
    if (TransferDirection == EfiUsbDataIn
        && *DataLength != 0
        && SmiRes == 0)
    {
        //
        // Some data is expected, no data is returned
        //
        Status = EFI_DEVICE_ERROR;
    }
*/
    if( (*TransferResult) &  EFI_USB_ERR_TIMEOUT ) Status = EFI_TIMEOUT;
	if( (*TransferResult) &  ~EFI_USB_ERR_TIMEOUT ) Status = EFI_DEVICE_ERROR;

    *DataLength = (UINTN)SmiRes;

    FreeDevInfo(DevInfo);

    return Status;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcGetRootHubPortStatus
//
// Description:
//  This function returns HC root port status.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcGetRootHubPortStatus (
    EFI_USB2_HC_PROTOCOL *HcProtocol,
    UINT8               PortNumber,
    EFI_USB_PORT_STATUS *PortStatus
)
{
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    return AmiUsbHcGetRootHubPortStatus(
                &This->hcprotocol, PortNumber, PortStatus);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcSetRootHubPortFeature
//
// Description:
//  This function set root hub port features.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcSetRootHubPortFeature(
    IN EFI_USB2_HC_PROTOCOL *HcProtocol,
    IN UINT8                PortNumber,
    IN EFI_USB_PORT_FEATURE PortFeature
)
{
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    return AmiUsbHcSetRootHubPortFeature(
                &This->hcprotocol, PortNumber, PortFeature);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        AmiUsb2HcClearRootHubPortFeature
//
// Description:
//  This function clears root hub port feature.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
AmiUsb2HcClearRootHubPortFeature(
    IN EFI_USB2_HC_PROTOCOL *HcProtocol,
    IN UINT8                PortNumber,
    IN EFI_USB_PORT_FEATURE PortFeature
)
{
    HC_DXE_RECORD *This = GetThis( HcProtocol );
    return AmiUsbHcClearRootHubPortFeature(
            &This->hcprotocol, PortNumber, PortFeature);
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        FindHcStruc
//
// Description:
//  Search gUsbData for information about HC linked to an EFI handle
//
// Input:
//  Controller - Host Controller handle
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

HC_STRUC* FindHcStruc(
    EFI_HANDLE Controller
)
{
    unsigned i;
    for (i = 0; i < gUsbData->HcTableCount; i++) {
        if (gUsbData->HcTable[i] == NULL) {
            continue;
        }
        if (!(gUsbData->HcTable[i]->dHCFlag & HC_STATE_USED)) {
        	continue;
        }
        if (gUsbData->HcTable[i]->Controller == Controller )
            return gUsbData->HcTable[i];
    }
    return NULL;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbHcSupported
//
// Description:
//  This function is a part of Driver Binding Protocol interface.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS UsbHcSupported (
    EFI_DRIVER_BINDING_PROTOCOL *This,
    EFI_HANDLE Controller,
    EFI_DEVICE_PATH_PROTOCOL *Dp
)
{
    EFI_STATUS  Status;
    VOID*       Ptr;

    Status = gBS->OpenProtocol (
            Controller,
            &gEfiPciIoProtocolGuid,
            &Ptr,
            This->DriverBindingHandle,
            Controller,
            EFI_OPEN_PROTOCOL_BY_DRIVER );
    if (EFI_ERROR (Status)) {
        return Status;
    }

    gBS->CloseProtocol (
        Controller, &gEfiPciIoProtocolGuid,
        This->DriverBindingHandle, Controller);

    if (FindHcStruc(Controller) == NULL) {
        return EFI_UNSUPPORTED;
    } else
        return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        UsbHcStop
//
// Description:
//  This function is a part of Driver Binding Protocol interface.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EFIAPI
UsbHcStop (
   EFI_DRIVER_BINDING_PROTOCOL     *This,
   EFI_HANDLE                      Controller,
   UINTN                           NumberOfChildren,
   EFI_HANDLE                      *Children  )
{
    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        InstallHcProtocols
//
// Description:
//  Start the AMI USB driver; Sets USB_FLAG_DRIVER_STARTED
//
// Input:
//  This                - Protocol instance pointer.
//  ControllerHandle    - Handle of device to test
//  RemainingDevicePath - Not used
//
// Output:
//  EFI_SUCCESS         - USB HC devices were initialized.
//  EFI_UNSUPPORTED     - pThis device is not supported by USB driver.
//  EFI_DEVICE_ERROR    - pThis driver cannot be started due to device error
//  EFI_OUT_OF_RESOURCES
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
InstallHcProtocols(
    IN EFI_DRIVER_BINDING_PROTOCOL  *This,
    IN EFI_HANDLE                   Controller,
    IN EFI_PCI_IO_PROTOCOL   		*PciIo,
    IN HC_STRUC              		*HcData
)
{
    HC_DXE_RECORD         *Rec;
    USB3_HOST_CONTROLLER *Usb3Hc;

    //
    // Create HcDxeRecord
    //
    VERIFY_EFI_ERROR(
        gBS->AllocatePool (
        EfiBootServicesData,
        sizeof(HC_DXE_RECORD),
        &Rec ));
    Rec->hc_data = HcData;
    Rec->pciIo = PciIo;
    Rec->AsyncTransfers.pHead = NULL;
    Rec->AsyncTransfers.pTail = NULL;
    Rec->AsyncTransfers.Size = 0;

    Rec->hcprotocol.Reset = AmiUsbHcReset;
    Rec->hcprotocol.GetState = AmiUsbHcGetState;
    Rec->hcprotocol.SetState = AmiUsbHcSetState;
    Rec->hcprotocol.ControlTransfer = AmiUsbHcControlTransfer;
    Rec->hcprotocol.BulkTransfer = AmiUsbHcBulkTransfer;
    Rec->hcprotocol.AsyncInterruptTransfer = AmiUsbHcAsyncInterruptTransfer;
    Rec->hcprotocol.SyncInterruptTransfer = AmiUsbHcSyncInterruptTransfer;
    Rec->hcprotocol.IsochronousTransfer = AmiUsbHcIsochronousTransfer;
    Rec->hcprotocol.AsyncIsochronousTransfer = AmiUsbHcAsyncIsochronousTransfer;
    Rec->hcprotocol.GetRootHubPortNumber = AmiUsbHcGetRootHubPortNumber;
    Rec->hcprotocol.GetRootHubPortStatus = AmiUsbHcGetRootHubPortStatus;
    Rec->hcprotocol.SetRootHubPortFeature = AmiUsbHcSetRootHubPortFeature;
    Rec->hcprotocol.ClearRootHubPortFeature = AmiUsbHcClearRootHubPortFeature;

    //
    // Fill USB Revision fields based on type of HC
    //
    // USB_HC_UHCI USB_HC_OHCI -> 1.1
    // USB_HC_EHCI   -> 2.0
    // USB_HC_XHCI 0.96, 1.0 -> 3.0
    // USB_HC_XHCI 1.1 -> 3.1

    switch (HcData->bHCType) {
        case USB_HC_UHCI:
        case USB_HC_OHCI:
            Rec->hcprotocol.MajorRevision = 1;
            Rec->hcprotocol.MinorRevision = 1;
            break;
        case USB_HC_EHCI:
            Rec->hcprotocol.MajorRevision = 2;
            Rec->hcprotocol.MinorRevision = 0;
            break;
        case USB_HC_XHCI:
            Rec->hcprotocol.MajorRevision = 3;
            Usb3Hc = (USB3_HOST_CONTROLLER*)HcData->usbbus_data;
            if (Usb3Hc->HciVersion <= 0x0100) {
                Rec->hcprotocol.MinorRevision = 0;
            } else {
                Rec->hcprotocol.MinorRevision = 1;
            }
            break;
        default:
            break;
    }

    Rec->hcprotocol2.GetCapability = AmiUsb2HcGetCapability;
    Rec->hcprotocol2.Reset = AmiUsb2HcReset;
    Rec->hcprotocol2.GetState = AmiUsb2HcGetState;
    Rec->hcprotocol2.SetState = AmiUsb2HcSetState;
    Rec->hcprotocol2.ControlTransfer = AmiUsb2HcControlTransfer;
    Rec->hcprotocol2.BulkTransfer = AmiUsb2HcBulkTransfer;
    Rec->hcprotocol2.AsyncInterruptTransfer = AmiUsb2HcAsyncInterruptTransfer;
    Rec->hcprotocol2.SyncInterruptTransfer = AmiUsb2HcSyncInterruptTransfer;
    Rec->hcprotocol2.IsochronousTransfer = AmiUsb2HcIsochronousTransfer;
    Rec->hcprotocol2.AsyncIsochronousTransfer = AmiUsb2HcAsyncIsochronousTransfer;
    Rec->hcprotocol2.GetRootHubPortStatus = AmiUsb2HcGetRootHubPortStatus;
    Rec->hcprotocol2.SetRootHubPortFeature = AmiUsb2HcSetRootHubPortFeature;
    Rec->hcprotocol2.ClearRootHubPortFeature = AmiUsb2HcClearRootHubPortFeature;
    Rec->hcprotocol2.MajorRevision =         Rec->hcprotocol.MajorRevision;
    Rec->hcprotocol2.MinorRevision =         Rec->hcprotocol.MinorRevision;


    //
    // Instal USB_HC_PROTOCOL
    //
    VERIFY_EFI_ERROR(
        gBS->InstallProtocolInterface( &Controller,
            &gEfiUsbHcProtocolGuid, EFI_NATIVE_INTERFACE, &Rec->hcprotocol ));
    VERIFY_EFI_ERROR(
        gBS->InstallProtocolInterface( &Controller,
            &gEfiUsb2HcProtocolGuid, EFI_NATIVE_INTERFACE, &Rec->hcprotocol2 ));

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        XhciAllocateScratchpadBuffers
//
// Description:
//  This function allocates XHCI scratchpad buffers. Data initialization will
//  be done later, in SMI XhciStart function.
//
// Notes:
//  Usb3Hc->DcbaaPtr points to the beginning of memory block first 2048 Bytes
//  of which is used for DCBAA.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
XhciAllocateScratchpadBuffers (
    IN USB3_HOST_CONTROLLER *Usb3Hc
)
{
    UINT16  NumBufs =  ((Usb3Hc->CapRegs->HcsParams2.MaxScratchPadBufsHi) << 5) + 
                        Usb3Hc->CapRegs->HcsParams2.MaxScratchPadBufsLo;
    UINT16      Count;
    VOID        *Buffer;

    if (NumBufs == 0) {
        return EFI_SUCCESS;
    }

    if (Usb3Hc->ScratchBufEntry == NULL) {
        // Allcate a PAGESIZE boundary for Scratchpad Buffer Array Base Address
        // because bit[0..5] are reserved in  Device Context Base Address Array Element 0.
        Usb3Hc->ScratchBufEntry = AllocateHcMemory(Usb3Hc->PciIo, 
                    EFI_SIZE_TO_PAGES((sizeof(UINT64) * NumBufs)), 0x1000);

        if (Usb3Hc->ScratchBufEntry == NULL) {
            return EFI_OUT_OF_RESOURCES;
        }

    	gBS->SetMem(Usb3Hc->ScratchBufEntry, (sizeof(UINT64) * NumBufs), 0);

        for (Count = 0; Count < NumBufs; Count++) {

            // Allocate scratchpad buffer: PAGESIZE block located on
            // a PAGESIZE boundary. Section 4.20.
            Buffer = AllocateHcMemory(Usb3Hc->PciIo,
                        Usb3Hc->PageSize4K, Usb3Hc->PageSize4K << 12);
            ASSERT(Buffer != NULL); // See if allocation is successful

            if (Buffer == NULL) {
                return EFI_OUT_OF_RESOURCES;
            }

            // Update *ScratchBufArrayBase
            Usb3Hc->ScratchBufEntry[Count] = (UINTN)Buffer;
        }
    }

        // Update scratchpad pointer only if # of scratch buffers >0
        if (NumBufs > 0) {
            *(UINTN*)Usb3Hc->DcbaaPtr = (UINTN)Usb3Hc->ScratchBufEntry;
    }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        XhciInitMemory
//
// Description:
//  This function allocates XHCI memory buffers. Data initialization will be
//  done later, in SMI XhciStart function. These are the memory blocks:
//
//  1. DCBAAP + ScrPadBuf pointers + InpCtx + ERST <-- 8KB
//  2. CommandRing  <-- 1KB
//  3. EventRing    <-- 1KB
//  4. XferRings    <-- 1KB*MaxSlots, 1KB = 32EP per slot times 32 (padded size of TRB_RING)
//  4. N*sizeof(XHCI_DEVICE_CONTEXT) for device context segment <-- N KB or 2*N KB,
//      N is SlotNumber from CONFIG register
//  
//  5. TransferRings <-- MaxSlots*32*1KB
//
// Notes:
//  Scratchpad buffers are optional, they are allocated and initialized separately.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
XhciInitMemory (
    IN USB3_HOST_CONTROLLER *Usb3Hc
)
{
    EFI_STATUS  Status;
    UINTN       MemSize;
    UINTN       DeviceContextSize;
    UINTN       XfrRingsSize;
    UINTN       XfrTrbsSize;

    XfrRingsSize = Usb3Hc->MaxSlots * 32 * 32;   // 32 endpoints per device, 32 padded size of TRB_RING
    XfrTrbsSize = RING_SIZE*Usb3Hc->MaxSlots*32;
    DeviceContextSize = (XHCI_DEVICE_CONTEXT_ENTRIES * Usb3Hc->ContextSize) * Usb3Hc->MaxSlots;

    MemSize = 0x2800 + XfrRingsSize + XfrTrbsSize + DeviceContextSize;
    if (Usb3Hc->DcbaaPtr == NULL) {
    	Usb3Hc->DcbaaPtr = (XHCI_DCBAA*)AllocateHcMemory(Usb3Hc->PciIo, 
    										EFI_SIZE_TO_PAGES(MemSize), 0x1000);
    }
    gBS->SetMem(Usb3Hc->DcbaaPtr, MemSize, 0);  // Clear buffer

    USB_DEBUG(3, "XHCI: Memory allocation - total %x Bytes:\n 0x2800+XfrRings(%x)+XfrTrbs(%x)+DevCtx(%x)\n",
        MemSize, XfrRingsSize, XfrTrbsSize, DeviceContextSize);

    // Assign DCBAA (Device Context Base Address); program the
    // DCBAA Pointer (DCBAAP) register (5.4.6) with a 64-bit address
    // pointing to where the Device Context Base Address Array is located.
    //
    // DCBAA: size 2048 Bytes, within PAGESIZE, 64 Bytes aligned.
    //
    // These requirements can be met by allocating 1 page using
    // pBS->AllocatePages; the address will be 4K aligned and will
    // not span PAGESIZE boundary.

    Status = XhciAllocateScratchpadBuffers(Usb3Hc);
    ASSERT_EFI_ERROR(Status);

    // Assign Input Context; the size of Input Context is either
    // 0x420 or 0x840 depending on HCPARAMS.Csz
    if (Usb3Hc->InputContext == NULL) {
        Usb3Hc->InputContext = (VOID*)((UINTN)Usb3Hc->DcbaaPtr + 0x940);
    }

    // Initialize Transfer Rings pointer and store it in Usb3Hc; actual
    // xfer ring initialization happens later, when the EP is being enabled
    if (Usb3Hc->XfrRings == NULL) {
        Usb3Hc->XfrRings = (TRB_RING*)((UINTN)Usb3Hc->DcbaaPtr + 0x2800);
    }

    // 1024 = 32 bytes is padded sizeof(TRB_RING) times 32 EP per device
    if (Usb3Hc->XfrTrbs == NULL) {
        Usb3Hc->XfrTrbs = (UINTN)Usb3Hc->XfrRings + XfrRingsSize;
    }

    // Assign device context memory: Usb3Hc->MaxSlots devices,
    // 1024 (2048 if HCPARAMS.Csz is set) Bytes each
    if (Usb3Hc->DeviceContext == NULL) {
        Usb3Hc->DeviceContext = (VOID*)((UINTN)Usb3Hc->XfrTrbs + XfrTrbsSize);
    }

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        XhciExtCapParser
//
// Description:
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
XhciExtCapParser(
    IN USB3_HOST_CONTROLLER *Usb3Hc
)
{
	XHCI_EXT_CAP	*CurPtr;

	if (Usb3Hc->CapRegs->HccParams1.Xecp == 0) return EFI_SUCCESS;

	// Starts from first capability
	CurPtr = (XHCI_EXT_CAP *)((UINTN)Usb3Hc->CapRegs + (Usb3Hc->CapRegs->HccParams1.Xecp << 2));

    // Traverse all capability structures
	for(;;) {
		switch (CurPtr->CapId) {
			case XHCI_EXT_CAP_USB_LEGACY:
				Usb3Hc->ExtLegCap = (XHCI_EXT_LEG_CAP *)CurPtr;
				// Clear HC BIOS Owned Semaphore bit
				Usb3Hc->ExtLegCap->LegSup.HcBiosOwned = 0;
                // Clear HC OS Owned Semaphore bit
                Usb3Hc->ExtLegCap->LegSup.HcOsOwned = 0;
				// Clear SMI enable bit
				Usb3Hc->ExtLegCap->LegCtlSts.UsbSmiEnable = 0;
				Usb3Hc->ExtLegCap->LegCtlSts.UsbOwnershipChangeSmiEnable = 0;
				USB_DEBUG(3, "XHCI: USB Legacy Ext Cap Ptr %x\n", Usb3Hc->ExtLegCap);
				break;

			case XHCI_EXT_CAP_SUPPORTED_PROTOCOL:
				if (((XHCI_EXT_PROTOCOL*)CurPtr)->MajorRev == 0x02) {
					Usb3Hc->Usb2Protocol = (XHCI_EXT_PROTOCOL*)CurPtr;
					USB_DEBUG(3, "XHCI: USB2 Support Protocol %x, PortOffset %x PortCount %x\n", 
						Usb3Hc->Usb2Protocol, Usb3Hc->Usb2Protocol->PortOffset, Usb3Hc->Usb2Protocol->PortCount);
				} else if (((XHCI_EXT_PROTOCOL*)CurPtr)->MajorRev == 0x03) {
					Usb3Hc->Usb3Protocol = (XHCI_EXT_PROTOCOL*)CurPtr;
					USB_DEBUG(3, "XHCI: USB3 Support Protocol %x, PortOffset %x PortCount %x\n", 
						Usb3Hc->Usb3Protocol, Usb3Hc->Usb3Protocol->PortOffset, Usb3Hc->Usb3Protocol->PortCount);
				}
				break;

			case XHCI_EXT_CAP_POWERMANAGEMENT:
			case XHCI_EXT_CAP_IO_VIRTUALIZATION:
                break;
			case XHCI_EXT_CAP_USB_DEBUG_PORT:
                Usb3Hc->DbCapRegs = (XHCI_DB_CAP_REGS*)CurPtr;
                USB_DEBUG(3, "XHCI: USB Debug Capability Ptr %x\n", Usb3Hc->DbCapRegs);
				break;
		}
		if(CurPtr->NextCapPtr == 0) break;
	    // Point to next capability
	    CurPtr=(XHCI_EXT_CAP *)((UINTN)CurPtr+ (((UINTN)CurPtr->NextCapPtr) << 2));
	}

	return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        PreInitXhci
//
// Description:
//  This function initializes XHCI data structures, allocates HC memory and
//  updates the relevant fields in HcStruc. At this point the controller's
//  resources are assigned and accessible.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
PreInitXhci(
    EFI_HANDLE  Handle,
    HC_STRUC    *HcStruc
)
{
#if XHCI_SUPPORT
    UINT8   MaxSlots;
    EFI_STATUS Status;
    USB3_HOST_CONTROLLER    *Usb3Hc = NULL;

    if (HcStruc->usbbus_data == NULL) {
        Status = gBS->AllocatePool(EfiRuntimeServicesData, 
            sizeof(USB3_HOST_CONTROLLER), &Usb3Hc);
        if (EFI_ERROR(Status)) {
            return Status;
        }

    	gBS->SetMem(Usb3Hc, sizeof(USB3_HOST_CONTROLLER), 0);
    } else {
        Usb3Hc = HcStruc->usbbus_data;
    }

    Usb3Hc->Controller = Handle;
    Status = gBS->HandleProtocol(Handle, &gEfiPciIoProtocolGuid, &Usb3Hc->PciIo);
    ASSERT_EFI_ERROR(Status);

    // Get Capability Registers offset off the BAR
                                        //(EIP101226)>
    Status = Usb3Hc->PciIo->Pci.Read(Usb3Hc->PciIo, 
        EfiPciIoWidthUint32, PCI_BAR0, 1, &Usb3Hc->CapRegs);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) {
		return Status;
    }
    if (((UINT8)(UINTN)Usb3Hc->CapRegs & (BIT1 |BIT2)) == BIT2) {
        Status = Usb3Hc->PciIo->Pci.Read(Usb3Hc->PciIo, 
            EfiPciIoWidthUint32, PCI_BAR0, sizeof(VOID*)/sizeof(UINT32), &Usb3Hc->CapRegs);
    }

                                        //<(EIP101226)
    //clear all attributes before use
    (UINT8)(UINTN)Usb3Hc->CapRegs &= ~(0x7F);  // Clear attributes

    Usb3Hc->PciIo->Pci.Read(Usb3Hc->PciIo, 
        EfiPciIoWidthUint16, PCI_VID, 1, &Usb3Hc->Vid);

    Usb3Hc->PciIo->Pci.Read(Usb3Hc->PciIo, 
        EfiPciIoWidthUint16, PCI_DID, 1, &Usb3Hc->Did);

    Usb3Hc->Access64 = Usb3Hc->CapRegs->HccParams1.Ac64;

    Usb3Hc->HciVersion = Usb3Hc->CapRegs->HciVersion;
    Usb3Hc->MaxPorts = Usb3Hc->CapRegs->HcsParams1.MaxPorts;

    Usb3Hc->OpRegs = (XHCI_HC_OP_REGS*)((UINTN)Usb3Hc->CapRegs + Usb3Hc->CapRegs->CapLength);
    Usb3Hc->PageSize4K = Usb3Hc->OpRegs->PageSize;
	Usb3Hc->ContextSize = 0x20 << Usb3Hc->CapRegs->HccParams1.Csz;

    USB_DEBUG(3, "XHCI: Cap %x, OpRegs: %x Ver %x,\n      MaxPorts 0x%x, PageSize %x*4K\n",
        Usb3Hc->CapRegs, Usb3Hc->OpRegs, Usb3Hc->HciVersion, Usb3Hc->MaxPorts, Usb3Hc->PageSize4K);

    ASSERT(Usb3Hc->PageSize4K < 0x8000);   // Maximum possible page size is 128MB

    Status = Usb3Hc->PciIo->Pci.Read(
        Usb3Hc->PciIo, EfiPciIoWidthUint8, XHCI_PCI_SBRN, 1, &Usb3Hc->SBRN);
    ASSERT_EFI_ERROR(Status);
    USB_DEBUG(3, "XHCI: Serial Bus Release Number is %x\n", Usb3Hc->SBRN);

    // OEM might change the default number of MaxSlots
    Status = Usb3OemGetMaxDeviceSlots(&MaxSlots);

    if (EFI_ERROR(Status)) {
        // Use default number of slots
        MaxSlots = Usb3Hc->CapRegs->HcsParams1.MaxSlots;
    }
    else {
        // Validate the porting function output
        ASSERT(MaxSlots > 0 && MaxSlots <= Usb3Hc->CapRegs->HcsParams1.MaxSlots);
    }
    Usb3Hc->MaxSlots = MaxSlots;

    // Get maximum number of interrupters
    Usb3Hc->MaxIntrs = Usb3Hc->CapRegs->HcsParams1.MaxIntrs;

    Usb3Hc->DbOffset = Usb3Hc->CapRegs->DbOff;

    USB_DEBUG(3, "XHCI: MaxSlots %x, MaxIntrs %x, Doorbell Offset %x\n", Usb3Hc->MaxSlots, Usb3Hc->MaxIntrs, Usb3Hc->DbOffset);

	// Parse all capability structures
	XhciExtCapParser(Usb3Hc);

	if (gUsbData->UsbXhciSupport == 0) {
        gBS->FreePool(Usb3Hc);
		return EFI_UNSUPPORTED;
	}

    // Allocate and initialize memory blocks
    Status = XhciInitMemory(Usb3Hc);
    ASSERT_EFI_ERROR(Status);
    if (EFI_ERROR(Status)) {
		gBS->FreePool(Usb3Hc);
		return Status;
    }

    HcStruc->usbbus_data = (VOID*)Usb3Hc;

#endif

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Name:        PostStopXhci
//
// Description:
//  This function frees the HC memory and clears XHCI data structures.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
PostStopXhci(
    EFI_HANDLE  Handle,
    HC_STRUC    *HcStruc
)
{
#if XHCI_SUPPORT
/*
	USB3_HOST_CONTROLLER *Usb3Hc = (USB3_HOST_CONTROLLER*)HcStruc->usbbus_data;
    UINT16  NumBufs =  ((Usb3Hc->CapRegs->HcsParams2.MaxScratchPadBufsHi) << 5) + 
                        Usb3Hc->CapRegs->HcsParams2.MaxScratchPadBufsLo;
    UINT16  Count;
    UINTN   MemSize;
    UINTN   DeviceContextSize;
    UINTN   XfrRingsSize;
    UINTN   XfrTrbsSize;

	for (Count = 0; Count < NumBufs; Count++) {
        gBS->FreePages((EFI_PHYSICAL_ADDRESS)(Usb3Hc->ScratchBufEntry[Count]), Usb3Hc->PageSize4K);
	}

    XfrRingsSize = Usb3Hc->MaxSlots * 32 * 32;   // 32 endpoints per device, 32 padded size of TRB_RING
    XfrTrbsSize = RING_SIZE * Usb3Hc->MaxSlots * 32;
    DeviceContextSize = (XHCI_DEVICE_CONTEXT_ENTRIES * Usb3Hc->ContextSize) * Usb3Hc->MaxSlots;

    MemSize = 0x2800 + XfrRingsSize + XfrTrbsSize + DeviceContextSize;

	gBS->FreePages((EFI_PHYSICAL_ADDRESS)Usb3Hc->DcbaaPtr, EFI_SIZE_TO_PAGES(MemSize));
    if (Usb3Hc->ScratchBufEntry) {
	    gBS->FreePages((EFI_PHYSICAL_ADDRESS)Usb3Hc->ScratchBufEntry, EFI_SIZE_TO_PAGES((sizeof(UINT64) * NumBufs)));
    }
	gBS->FreePool(Usb3Hc);
*/
#endif
	return EFI_SUCCESS;
}

//****************************************************************************
//****************************************************************************
//**                                                                        **
//**             (C)Copyright 1985-2016, American Megatrends, Inc.          **
//**                                                                        **
//**                          All Rights Reserved.                          **
//**                                                                        **
//**                 5555 Oakbrook Pkwy, Norcross, GA 30093                 **
//**                                                                        **
//**                          Phone (770)-246-8600                          **
//**                                                                        **
//****************************************************************************
//****************************************************************************