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
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
|
/*
* fusb302 usb phy driver for type-c and PD
*
* Copyright (C) 2015, 2016 Fairchild Semiconductor Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Seee the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef FSC_DEBUG
#include "Log.h"
#endif // FSC_DEBUG
#include <linux/printk.h>
#include "PD_Types.h"
#include "PDPolicy.h"
#include "PDProtocol.h"
#include "TypeC.h"
#include "fusb30X.h"
#ifdef FSC_HAVE_VDM
#include "vdm/vdm_callbacks.h"
#include "vdm/vdm_callbacks_defs.h"
#include "vdm/vdm.h"
#include "vdm/vdm_types.h"
#include "vdm/bitfield_translators.h"
#include "vdm/DisplayPort/dp_types.h"
#include "vdm/DisplayPort/dp.h"
#include "vdm/DisplayPort/interface_dp.h"
#endif // FSC_HAVE_VDM
/////////////////////////////////////////////////////////////////////////////
// Variables for use with the USB PD state machine
/////////////////////////////////////////////////////////////////////////////
#ifdef FSC_DEBUG
StateLog PDStateLog; // Log for tracking state transitions and times
extern volatile FSC_U16 Timer_S; // Tracks seconds elapsed for log timestamp
extern volatile FSC_U16 Timer_tms; // Tracks tenths of milliseconds elapsed for log timestamp
extern FSC_U8 manualRetries; // Set to 1 to enable manual retries
extern FSC_U8 nTries; // Number of tries for manual retry
#endif // FSC_DEBUG
extern FSC_BOOL g_Idle; // Puts state machine into Idle state
extern USBTypeCCurrent SourceCurrent; // TypeC advertised current
// Device Policy Manager Variables
FSC_BOOL USBPDTxFlag; // Flag to indicate that we need to send a message (set by device policy manager)
FSC_BOOL IsHardReset; // Variable indicating that a Hard Reset is occurring
FSC_BOOL IsPRSwap; // Variable indicating that a PRSwap is occurring
FSC_BOOL IsVCONNSource; // Indicates who is the VCONN source
sopMainHeader_t PDTransmitHeader = {0}; // Definition of the PD packet to send
sopMainHeader_t CapsHeaderSink = {0}; // Definition of the sink capabilities of the device
sopMainHeader_t CapsHeaderSource = {0}; // Definition of the source capabilities of the device
sopMainHeader_t CapsHeaderReceived = {0}; // Last capabilities header received (source or sink)
doDataObject_t PDTransmitObjects[7] = {{0}}; // Data objects to send
doDataObject_t CapsSink[7] = {{0}}; // Power object definitions of the sink capabilities of the device
doDataObject_t CapsSource[7] = {{0}}; // Power object definitions of the source capabilities of the device
doDataObject_t CapsReceived[7] = {{0}}; // Last power objects received (source or sink)
doDataObject_t USBPDContract = {0}; // Current USB PD contract (request object)
doDataObject_t SinkRequest = {0}; // Sink request message
FSC_U32 SinkRequestMaxVoltage; // Maximum voltage that the sink will request
FSC_U32 SinkRequestMaxPower; // Maximum power the sink will request (used to calculate current as well)
FSC_U32 SinkRequestOpPower; // Operating power the sink will request (used to calculate current as well)
FSC_U32 SinkRequestMaxCurrent; // Maximum current the sink will request
FSC_BOOL SinkGotoMinCompatible; // Whether the sink will respond to the GotoMin command
FSC_BOOL SinkUSBSuspendOperation; // Whether the sink wants to continue operation during USB suspend
FSC_BOOL SinkUSBCommCapable; // Whether the sink is USB communications capable
doDataObject_t PartnerCaps = {0}; // Partner's Sink Capabilities
#ifdef FSC_DEBUG
FSC_BOOL SourceCapsUpdated; // Flag to indicate whether we have updated the source caps (for the GUI)
#endif // FSC_DEBUG
// Policy Variables
// removing static qualifier so PolicyState is visible to other code blocks.
// re-org coming soon!
PolicyState_t PolicyState; // State variable for Policy Engine
PolicyState_t LastPolicyState; // State variable for Policy Engine
FSC_U8 PolicySubIndex; // Sub index for policy states
FSC_BOOL PolicyIsSource; // Flag to indicate whether we are acting as a source or a sink
FSC_BOOL PolicyIsDFP; // Flag to indicate whether we are acting as a UFP or DFP
FSC_BOOL PolicyHasContract; // Flag to indicate whether there is a contract in place
FSC_U32 VbusTransitionTime; // Time to wait for VBUS switch to transition
static FSC_U8 CollisionCounter; // Collision counter for the policy engine
static FSC_U8 HardResetCounter; // The number of times a hard reset has been generated
static FSC_U8 CapsCounter; // Number of capabilities messages sent
volatile FSC_U32 PolicyStateTimer; // Multi-function timer for the different policy states
volatile FSC_U32 NoResponseTimer; // Policy engine no response timer
volatile static FSC_U32 SwapSourceStartTimer; // Delay after power role swap before starting source PD
sopMainHeader_t PolicyRxHeader = {0}; // Header object for USB PD messages received
sopMainHeader_t PolicyTxHeader = {0}; // Header object for USB PD messages to send
doDataObject_t PolicyRxDataObj[7] = {{0}}; // Buffer for data objects received
doDataObject_t PolicyTxDataObj[7] = {{0}}; // Buffer for data objects to send
static FSC_BOOL isContractValid; // Is PD Contract Valid
#ifdef FSC_HAVE_VDM
// VDM Manager object
extern VdmManager vdmm;
VdmDiscoveryState_t AutoVdmState;
FSC_U32 vdm_msg_length;
doDataObject_t vdm_msg_obj[7] = {{0}};
PolicyState_t vdm_next_ps;
FSC_BOOL sendingVdmData;
volatile FSC_U32 VdmTimer;
FSC_BOOL VdmTimerStarted;
FSC_U16 auto_mode_disc_tracker;
extern FSC_BOOL mode_entered;
extern SvidInfo core_svid_info;
#endif // FSC_HAVE_VDM
#ifdef FSC_HAVE_DP
extern FSC_U32 DpModeEntered;
extern FSC_S32 AutoDpModeEntryObjPos;
#endif // FSC_HAVE_DP
extern FSC_BOOL ProtocolCheckRxBeforeTx;
extern FSC_U8 loopCounter; // Used to count the number of Unattach<->AttachWait loops
/////////////////////////////////////////////////////////////////////////////
// Timer Interrupt service routine
/////////////////////////////////////////////////////////////////////////////
void PolicyTick( void )
{
if( !USBPDActive )
return;
if (PolicyStateTimer) // If the PolicyStateTimer is greater than zero...
PolicyStateTimer--; // Decrement it
if ((NoResponseTimer < T_TIMER_DISABLE) && (NoResponseTimer > 0)) // If the timer is enabled and hasn't expired
NoResponseTimer--; // Decrement it
if (SwapSourceStartTimer)
SwapSourceStartTimer--;
#ifdef FSC_HAVE_VDM
if (VdmTimer)
VdmTimer--;
#endif // FSC_HAVE_VDM
}
void InitializePDPolicyVariables(void)
{
SwapSourceStartTimer = 0;
#ifdef FSC_HAVE_SNK
SinkRequestMaxVoltage = 180; // Maximum voltage that the sink will request (9V)
SinkRequestMaxPower = 36000; // Maximum power the sink will request (18W, used to calculate current as well)
SinkRequestOpPower = 36000; // Operating power the sink will request (18W, sed to calculate current as well)
SinkGotoMinCompatible = FALSE; // Whether the sink will respond to the GotoMin command
SinkUSBSuspendOperation = FALSE; // Whether the sink wants to continue operation during USB suspend
SinkUSBCommCapable = FALSE; // Whether the sink is USB communications capable
SinkRequestMaxCurrent = 300;
CapsHeaderSink.NumDataObjects = 3; // Set the number of power objects to 2
CapsHeaderSink.PortDataRole = 0; // Set the data role to UFP by default
CapsHeaderSink.PortPowerRole = 0; // By default, set the device to be a sink
CapsHeaderSink.SpecRevision = 1; // Set the spec revision to 2.0
CapsHeaderSink.Reserved0 = 0;
CapsHeaderSink.Reserved1 = 0;
CapsSink[0].FPDOSink.Voltage = 100; // Set 5V for the first supply option
CapsSink[0].FPDOSink.OperationalCurrent = 300; // Set that our device will consume 3000mA for this object
CapsSink[0].FPDOSink.DataRoleSwap = 1; // By default, enable DR_SWAP
CapsSink[0].FPDOSink.USBCommCapable = 0; // By default, USB communications is not allowed
CapsSink[0].FPDOSink.ExternallyPowered = 0; // By default, we are not externally powered
CapsSink[0].FPDOSink.HigherCapability = FALSE; // By default, don't require more than vSafe5V
CapsSink[0].FPDOSink.DualRolePower = 1; // By default, enable PR_SWAP
CapsSink[0].FPDOSink.Reserved = 0;
CapsSink[1].FPDOSink.Voltage = 180; // Set 9V for the second supply option
CapsSink[1].FPDOSink.OperationalCurrent = 200; // Set that our device will consume 2000mA for this object
CapsSink[1].FPDOSink.DataRoleSwap = 0; // Not used
CapsSink[1].FPDOSink.USBCommCapable = 0; // Not used
CapsSink[1].FPDOSink.ExternallyPowered = 0; // Not used
CapsSink[1].FPDOSink.HigherCapability = 0; // Not used
CapsSink[1].FPDOSink.DualRolePower = 0; // Not used
CapsSink[2].BPDO.MaxPower = 72; // Set 18W for the third supply option
CapsSink[2].BPDO.MinVoltage = 80; // Set 4V for minimum voltage
CapsSink[2].BPDO.MaxVoltage = 200; // Set 10V for maximum voltage
CapsSink[2].BPDO.SupplyType = pdoTypeBattery;
#endif // FSC_HAVE_SNK
#ifdef FSC_HAVE_SRC
#ifdef FM150911A
CapsHeaderSource.NumDataObjects = 2; // Set the number of power objects to 2
#else
CapsHeaderSource.NumDataObjects = 1; // Set the number of power objects to 1
#endif // FM150911A
CapsHeaderSource.PortDataRole = 0; // Set the data role to UFP by default
CapsHeaderSource.PortPowerRole = 1; // By default, set the device to be a source
CapsHeaderSource.SpecRevision = 1; // Set the spec revision to 2.0
CapsHeaderSource.Reserved0 = 0;
CapsHeaderSource.Reserved1 = 0;
CapsSource[0].FPDOSupply.Voltage = 100; // Set 5V for the first supply option
CapsSource[0].FPDOSupply.MaxCurrent = 90; // Set 900mA for the first supply option
CapsSource[0].FPDOSupply.PeakCurrent = 0; // Set peak equal to max
CapsSource[0].FPDOSupply.DataRoleSwap = TRUE; // By default, don't enable DR_SWAP
CapsSource[0].FPDOSupply.USBCommCapable = FALSE; // By default, USB communications is not allowed
CapsSource[0].FPDOSupply.ExternallyPowered = FALSE; // By default, state that we are not externally powered
CapsSource[0].FPDOSupply.USBSuspendSupport = FALSE; // By default, allow USB Suspend
CapsSource[0].FPDOSupply.DualRolePower = TRUE; // By default, enable PR_SWAP
CapsSource[0].FPDOSupply.SupplyType = 0; // Fixed supply
CapsSource[0].FPDOSupply.Reserved = 0; // Clearing reserved bits
#ifdef FM150911A
CapsSource[1].FPDOSupply.Voltage = 240; // Set 12V for the second supply option
CapsSource[1].FPDOSupply.MaxCurrent = 150; // Set 500mA for the second supply option
CapsSource[1].FPDOSupply.PeakCurrent = 0; // Set peak equal to max
CapsSource[1].FPDOSupply.DataRoleSwap = 0; // Not used... set to zero
CapsSource[1].FPDOSupply.USBCommCapable = 0; // Not used... set to zero
CapsSource[1].FPDOSupply.ExternallyPowered = 0; // Not used... set to zero
CapsSource[1].FPDOSupply.USBSuspendSupport = 0; // Not used... set to zero
CapsSource[1].FPDOSupply.DualRolePower = 0; // Allows PR_SWAP
CapsSource[1].FPDOSupply.SupplyType = 0; // Fixed supply
#endif // FM150911A
#endif // FSC_HAVE_SRC
#ifdef FSC_DEBUG
SourceCapsUpdated = FALSE; // Set the flag to indicate to the GUI that our source caps have been updated
#endif // FSC_DEBUG
VbusTransitionTime = tFPF2498Transition; // Default VBUS transition time 20ms
#ifdef FSC_HAVE_VDM
InitializeVdmManager(); // Initialize VDM Manager
vdmInitDpm();
AutoVdmState = AUTO_VDM_INIT;
auto_mode_disc_tracker = 0;
#endif // FSC_HAVE_VDM
#ifdef FSC_HAVE_DP
AutoDpModeEntryObjPos = -1;
#endif // FSC_HAVE_DP
ProtocolCheckRxBeforeTx = FALSE;
isContractValid = FALSE;
Registers.Slice.SDAC = SDAC_DEFAULT; // Set the SDAC threshold to "default"
Registers.Slice.SDAC_HYS = 0b01; // Set hysteresis to 85mV
DeviceWrite(regSlice, 1, &Registers.Slice.byte);
#ifdef FSC_DEBUG
InitializeStateLog(&PDStateLog);
#endif // FSC_DEBUG
}
// ##################### USB PD Enable / Disable Routines ################### //
void USBPDEnable(FSC_BOOL DeviceUpdate, SourceOrSink TypeCDFP)
{
FSC_U8 data[5];
IsHardReset = FALSE;
IsPRSwap = FALSE;
HardResetCounter = 0;
pr_debug("FUSB %s: IsPRSwap=%d\n", __func__, IsPRSwap);
if (USBPDEnabled == TRUE)
{
if (blnCCPinIsCC1) { // If the CC pin is on CC1
Registers.Switches.TXCC1 = 1; // Enable the BMC transmitter on CC1
Registers.Switches.MEAS_CC1 = 1;
Registers.Switches.TXCC2 = 0; // Disable the BMC transmitter on CC2
Registers.Switches.MEAS_CC2 = 0;
}
else if (blnCCPinIsCC2) { // If the CC pin is on CC2
Registers.Switches.TXCC2 = 1; // Enable the BMC transmitter on CC2
Registers.Switches.MEAS_CC2 = 1;
Registers.Switches.TXCC1 = 0; // Disable the BMC transmitter on CC1
Registers.Switches.MEAS_CC1 = 0;
}
if (blnCCPinIsCC1 || blnCCPinIsCC2) // If we know what pin the CC signal is...
{
USBPDActive = TRUE; // Set the active flag
ResetProtocolLayer(FALSE); // Reset the protocol layer by default
NoResponseTimer = T_TIMER_DISABLE; // Disable the no response timer by default
PolicyIsSource = TypeCDFP; // Set whether we should be initially a source or sink
PolicyIsDFP = TypeCDFP;
IsVCONNSource = TypeCDFP;
// Set the initial data port direction
if (PolicyIsSource) // If we are a source...
{
PolicyState = peSourceStartup; // initialize the policy engine state to source startup
PolicySubIndex = 0;
LastPolicyState = peDisabled;
Registers.Switches.POWERROLE = 1; // Initialize to a SRC
Registers.Switches.DATAROLE = 1; // Initialize to a DFP
}
else // Otherwise we are a sink...
{
PolicyState = peSinkStartup; // initialize the policy engine state to sink startup
PolicySubIndex = 0;
PolicyStateTimer =0;
LastPolicyState = peDisabled;
Registers.Switches.POWERROLE = 0; // Initialize to a SNK
Registers.Switches.DATAROLE = 0; // Initialize to a UFP
Registers.Control.ENSOP1 = 0;
Registers.Control.ENSOP1DP = 0;
Registers.Control.ENSOP2 = 0;
Registers.Control.ENSOP2DB = 0;
}
Registers.Switches.AUTO_CRC = 0; // Disable auto CRC until startup
Registers.Control.AUTO_PRE = 0; // Disable AUTO_PRE since we are going to use AUTO_CRC
Registers.Control.N_RETRIES = 3; // Set the number of retries to 3
Registers.Power.PWR = 0xF; // Enable the internal oscillator for USB PD
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power setting
#ifdef FSC_DEBUG
if(manualRetries)
{
Registers.Control.N_RETRIES = 0; // Set the number of retries to 0
}
#endif // FSC_DEBUG
Registers.Control.AUTO_RETRY = 1; // Enable AUTO_RETRY to use the I_TXSENT interrupt - needed for auto-CRC to work
data[0] = Registers.Slice.byte; // Set the slice byte (building one transaction)
data[1] = Registers.Control.byte[0] | 0x40; // Set the Control0 byte and set the TX_FLUSH bit (auto-clears)
data[2] = Registers.Control.byte[1] | 0x04; // Set the Control1 byte and set the RX_FLUSH bit (auto-clears)
data[3] = Registers.Control.byte[2];
data[4] = Registers.Control.byte[3];
DeviceWrite(regControl0, 4, &data[1]);
if (DeviceUpdate)
{
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the switch1 setting
}
Registers.Power.PWR = 0xF; // Enable the internal oscillator for USB PD
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power setting
#ifdef FSC_DEBUG
StoreUSBPDToken(TRUE, pdtAttach); // Store the PD attach token
#endif // FSC_DEBUG
}
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = FALSE; // Go into active mode
platform_enable_timer(TRUE);
#endif // FSC_INTERRUPT_TRIGGERED
}
}
void USBPDDisable(FSC_BOOL DeviceUpdate)
{
IsHardReset = FALSE;
#ifdef FSC_DEBUG
if (USBPDActive == TRUE) // If we were previously active...
StoreUSBPDToken(TRUE, pdtDetach); // Store the PD detach token
SourceCapsUpdated = TRUE; // Set the source caps updated flag to trigger an update of the GUI
#endif // FSC_DEBUG
USBPDActive = FALSE; // Clear the USB PD active flag
ProtocolState = PRLDisabled; // Set the protocol layer state to disabled
PolicyState = peDisabled; // Set the policy engine state to disabled
PDTxStatus = txIdle; // Reset the transmitter status
PolicyIsSource = FALSE; // Clear the is source flag until we connect again
PolicyHasContract = FALSE; // Clear the has contract flag
platform_notify_pd_contract(FALSE);
if (DeviceUpdate)
{
Registers.Switches.TXCC1 = 0; // Disable the BMC transmitter (both CC1 & CC2)
Registers.Switches.TXCC2 = 0;
Registers.Switches.AUTO_CRC = 0; // turn off Auto CRC
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the switch setting
}
Registers.Power.PWR = 0x7; // Disable the internal oscillator for USB PD
DeviceWrite(regPower, 1, &Registers.Power.byte); // Commit the power setting
ProtocolFlushRxFIFO();
ProtocolFlushTxFIFO();
#ifdef FSC_INTERRUPT_TRIGGERED
Registers.Mask.M_COLLISION = 1; // Mask PD Interrupts
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.M_RETRYFAIL = 1;
Registers.MaskAdv.M_TXSENT = 1;
Registers.MaskAdv.M_HARDRST = 1;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 1;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
#endif // FSC_INTERRUPT_TRIGGERED
}
// ##################### USB PD Policy Engine Routines ###################### //
void USBPDPolicyEngine(void)
{
#ifdef FSC_DEBUG
if (LastPolicyState != PolicyState) // Log Policy State for Debugging
{
WriteStateLog(&PDStateLog, PolicyState, Timer_tms, Timer_S);
}
#endif // FSC_DEBUG
LastPolicyState = PolicyState;
switch (PolicyState)
{
case peDisabled:
break;
case peErrorRecovery:
PolicyErrorRecovery();
break;
// ###################### Source States ##################### //
#ifdef FSC_HAVE_SRC
case peSourceSendHardReset:
PolicySourceSendHardReset();
break;
case peSourceSendSoftReset:
PolicySourceSendSoftReset();
break;
case peSourceSoftReset:
PolicySourceSoftReset();
break;
case peSourceStartup:
PolicySourceStartup();
break;
case peSourceDiscovery:
PolicySourceDiscovery();
break;
case peSourceSendCaps:
PolicySourceSendCaps();
break;
case peSourceDisabled:
PolicySourceDisabled();
break;
case peSourceTransitionDefault:
PolicySourceTransitionDefault();
break;
case peSourceNegotiateCap:
PolicySourceNegotiateCap();
break;
case peSourceCapabilityResponse:
PolicySourceCapabilityResponse();
break;
case peSourceTransitionSupply:
PolicySourceTransitionSupply();
break;
case peSourceReady:
PolicySourceReady();
break;
case peSourceGiveSourceCaps:
PolicySourceGiveSourceCap();
break;
case peSourceGetSinkCaps:
PolicySourceGetSinkCap();
break;
case peSourceSendPing:
PolicySourceSendPing();
break;
case peSourceGotoMin:
PolicySourceGotoMin();
break;
case peSourceGiveSinkCaps:
PolicySourceGiveSinkCap();
break;
case peSourceGetSourceCaps:
PolicySourceGetSourceCap();
break;
case peSourceSendDRSwap:
PolicySourceSendDRSwap();
break;
case peSourceEvaluateDRSwap:
PolicySourceEvaluateDRSwap();
break;
case peSourceSendVCONNSwap:
PolicySourceSendVCONNSwap();
break;
case peSourceSendPRSwap:
PolicySourceSendPRSwap();
break;
case peSourceEvaluatePRSwap:
PolicySourceEvaluatePRSwap();
break;
case peSourceWaitNewCapabilities:
PolicySourceWaitNewCapabilities();
break;
case peSourceEvaluateVCONNSwap:
PolicySourceEvaluateVCONNSwap();
break;
#endif // FSC_HAVE_SRC
// ###################### Sink States ####################### //
#ifdef FSC_HAVE_SNK
case peSinkStartup:
PolicySinkStartup();
break;
case peSinkSendHardReset:
PolicySinkSendHardReset();
break;
case peSinkSoftReset:
PolicySinkSoftReset();
break;
case peSinkSendSoftReset:
PolicySinkSendSoftReset();
break;
case peSinkTransitionDefault:
PolicySinkTransitionDefault();
break;
case peSinkDiscovery:
PolicySinkDiscovery();
break;
case peSinkWaitCaps:
PolicySinkWaitCaps();
break;
case peSinkEvaluateCaps:
PolicySinkEvaluateCaps();
break;
case peSinkSelectCapability:
PolicySinkSelectCapability();
break;
case peSinkTransitionSink:
PolicySinkTransitionSink();
break;
case peSinkReady:
PolicySinkReady();
break;
case peSinkGiveSinkCap:
PolicySinkGiveSinkCap();
break;
case peSinkGetSourceCap:
PolicySinkGetSourceCap();
break;
case peSinkGetSinkCap:
PolicySinkGetSinkCap();
break;
case peSinkGiveSourceCap:
PolicySinkGiveSourceCap();
break;
case peSinkSendDRSwap:
PolicySinkSendDRSwap();
break;
case peSinkEvaluateDRSwap:
PolicySinkEvaluateDRSwap();
break;
case peSinkEvaluateVCONNSwap:
PolicySinkEvaluateVCONNSwap();
break;
case peSinkSendPRSwap:
PolicySinkSendPRSwap();
break;
case peSinkEvaluatePRSwap:
PolicySinkEvaluatePRSwap();
break;
#endif // FSC_HAVE_SNK
#ifdef FSC_HAVE_VDM
case peGiveVdm:
PolicyGiveVdm();
break;
#endif // FSC_HAVE_VDM
// ---------- BIST Receive Mode --------------------- //
case PE_BIST_Receive_Mode: // Bist Receive Mode
policyBISTReceiveMode();
break;
case PE_BIST_Frame_Received: // Test Frame received by Protocol layer
policyBISTFrameReceived();
break;
// ---------- BIST Carrier Mode and Eye Pattern ----- //
case PE_BIST_Carrier_Mode_2: // BIST Carrier Mode 2
policyBISTCarrierMode2();
break;
case PE_BIST_Test_Data:
policyBISTTestData();
break;
default:
#ifdef FSC_HAVE_VDM
if ((PolicyState >= FIRST_VDM_STATE) && (PolicyState <= LAST_VDM_STATE) ) {
// valid VDM state
PolicyVdm();
} else
#endif // FSC_HAVE_VDM
{
// invalid state, reset
PolicyInvalidState();
}
break;
}
}
// ############################# Source States ############################# //
void PolicyErrorRecovery(void)
{
SetStateErrorRecovery();
}
#ifdef FSC_HAVE_SRC
void PolicySourceSendHardReset(void)
{
PolicySendHardReset(peSourceTransitionDefault, 0);
}
void PolicySourceSoftReset(void)
{
PolicySendCommand(CMTAccept, peSourceSendCaps, 0);
}
void PolicySourceSendSoftReset(void)
{
switch (PolicySubIndex)
{
case 0:
if (PolicySendCommand(CMTSoftReset, peSourceSendSoftReset, 1) == STAT_SUCCESS) // Send the soft reset command to the protocol layer
{
PolicyStateTimer = tSenderResponse; // Start the sender response timer to wait for an accept message once successfully sent
}
break;
default:
if (ProtocolMsgRx) // If we have received a message
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we've handled it here
if ((PolicyRxHeader.NumDataObjects == 0) && (PolicyRxHeader.MessageType == CMTAccept)) // And it was the Accept...
{
PolicyState = peSourceSendCaps; // Go to the send caps state
}
else // Otherwise it was a message that we didn't expect, so...
PolicyState = peSourceSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
else if (!PolicyStateTimer) // If we didn't get a response to our request before timing out...
{
PolicyState = peSourceSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
}
}
void PolicySourceStartup(void)
{
#ifdef FSC_HAVE_VDM
FSC_S32 i;
#endif // FSC_HAVE_VDM
// Set masks for PD
Registers.Mask.M_COLLISION = 0;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.M_RETRYFAIL = 0;
Registers.MaskAdv.M_TXSENT = 0;
Registers.MaskAdv.M_HARDRST = 0;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
if(Registers.DeviceID.VERSION_ID == VERSION_302B)
{
if(Registers.Control.BIST_TMODE == 1)
{
Registers.Control.BIST_TMODE = 0; // Disable auto-flush RxFIFO
DeviceWrite(regControl3, 1, &Registers.Control.byte[3]);
}
}
else
{
if(Registers.Control.RX_FLUSH == 1) // Disable Rx flushing if it has been enabled
{
Registers.Control.RX_FLUSH = 0;
}
}
switch (PolicySubIndex)
{
case 0:
USBPDContract.object = 0; // Clear the USB PD contract (output power to 5V default)
PartnerCaps.object = 0; // Clear partner sink caps
IsPRSwap = FALSE;
pr_debug("FUSB %s: IsPRSwap=%d\n", __func__, IsPRSwap);
PolicyIsSource = TRUE; // Set the flag to indicate that we are a source (PRSwaps)
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
ResetProtocolLayer(TRUE); // Reset the protocol layer
PRSwapTimer = 0; // Clear the swap timer
CapsCounter = 0; // Clear the caps counter
CollisionCounter = 0; // Reset the collision counter
PolicyStateTimer = 150; // 150ms timeout for VBUS
PolicySubIndex++;
break;
case 1:
if ((isVBUSOverVoltage(VBUS_MDAC_4P62) && (SwapSourceStartTimer == 0)) || (PolicyStateTimer == 0)) // Wait until we reach vSafe0V and delay if coming from PR Swap
{
PolicySubIndex++;
}
break;
case 2:
PolicyStateTimer = 0; // Reset the policy state timer
PolicyState = peSourceSendCaps; // Go to the source caps
PolicySubIndex = 0; // Reset the sub index
#ifdef FSC_HAVE_VDM
AutoVdmState = AUTO_VDM_INIT;
mode_entered = FALSE;
auto_mode_disc_tracker = 0;
core_svid_info.num_svids = 0;
for (i = 0; i < MAX_NUM_SVIDS; i++) {
core_svid_info.svids[i] = 0;
}
#endif // FSC_HAVE_VDM
#ifdef FSC_HAVE_DP
AutoDpModeEntryObjPos = -1;
resetDp();
#endif // FSC_HAVE_DP
break;
default:
PolicySubIndex = 0;
break;
}
}
void PolicySourceDiscovery(void)
{
switch (PolicySubIndex)
{
case 0:
PolicyStateTimer = tTypeCSendSourceCap; // Initialize the SourceCapabilityTimer
PolicySubIndex++; // Increment the sub index
break;
default:
if ((HardResetCounter > nHardResetCount) && (NoResponseTimer == 0) && (PolicyHasContract == TRUE))
{ // If we previously had a contract in place...
PolicyState = peErrorRecovery; // Go to the error recovery state since something went wrong
PolicySubIndex = 0;
}
else if ((HardResetCounter > nHardResetCount) && (NoResponseTimer == 0) && (PolicyHasContract == FALSE))
{ // Otherwise...
PolicyState = peSourceDisabled; // Go to the disabled state since we are assuming that there is no PD sink attached
PolicySubIndex = 0; // Reset the sub index for the next state
}
if (PolicyStateTimer == 0) // Once the timer expires...
{
if (CapsCounter > nCapsCount) // If we have sent the maximum number of capabilities messages...
PolicyState = peSourceDisabled; // Go to the disabled state, no PD sink connected
else // Otherwise...
PolicyState = peSourceSendCaps; // Go to the send source caps state to send a source caps message
PolicySubIndex = 0; // Reset the sub index for the next state
}
break;
}
}
void PolicySourceSendCaps(void)
{
if ((HardResetCounter > nHardResetCount) && (NoResponseTimer == 0)) // Check our higher level timeout
{
if (PolicyHasContract) // If USB PD was previously established...
PolicyState = peErrorRecovery; // Need to go to the error recovery state
else // Otherwise...
PolicyState = peSourceDisabled; // We are disabling PD and leaving the Type-C connections alone
}
else // If we haven't timed out and maxed out on hard resets...
{
switch (PolicySubIndex)
{
case 0:
if (PolicySendData(DMTSourceCapabilities, CapsHeaderSource.NumDataObjects, &CapsSource[0], peSourceSendCaps, 1, SOP_TYPE_SOP) == STAT_SUCCESS)
{
HardResetCounter = 0; // Clear the hard reset counter
CapsCounter = 0; // Clear the caps counter
NoResponseTimer = T_TIMER_DISABLE; // Stop the no response timer
PolicyStateTimer = tSenderResponse - tHardResetOverhead; // Set the sender response timer
}
break;
default:
if (ProtocolMsgRx) // If we have received a message
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if ((PolicyRxHeader.NumDataObjects == 1) && (PolicyRxHeader.MessageType == DMTRequest)) // Was this a valid request message?
PolicyState = peSourceNegotiateCap; // If so, go to the negotiate capabilities state
else // Otherwise it was a message that we didn't expect, so...
PolicyState = peSourceSendSoftReset; // Go onto issuing a soft reset
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
else if (!PolicyStateTimer) // If we didn't get a response to our request before timing out...
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we've timed out
PolicyState = peSourceSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
}
}
}
void PolicySourceDisabled(void)
{
USBPDContract.object = 0; // Clear the USB PD contract (output power to 5V default)
// Wait for a hard reset or detach...
#ifdef FSC_INTERRUPT_TRIGGERED
if(loopCounter == 0)
{
g_Idle = TRUE; // Idle until COMP or HARDRST or GCRCSENT
platform_enable_timer(FALSE);
}
#endif // FSC_INTERRUPT_TRIGGERED
}
void PolicySourceTransitionDefault(void)
{
switch (PolicySubIndex)
{
case 0:
if(PolicyStateTimer == 0)
{
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
PolicySubIndex++;
}
break;
case 1:
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
platform_set_vbus_discharge(TRUE); // Enabled VBUS discharge path
if(!PolicyIsDFP) // Make sure date role is DFP
{
PolicyIsDFP = TRUE;; // Set the current data role
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302
platform_notify_attached_source(PolicyIsDFP, true);
}
if(IsVCONNSource) // Disable VCONN if VCONN Source
{
Registers.Switches.VCONN_CC1 = 0;
Registers.Switches.VCONN_CC2 = 0;
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
platform_set_vconn_enable(FALSE);
}
PolicySubIndex++;
// Adjust output if necessary and start timer prior to going to startup state?
break;
case 2:
if(VbusVSafe0V()) // Allow the voltage to drop to 0
{
platform_set_vbus_discharge(FALSE); // Disable VBUS discharge path
PolicyStateTimer = tSrcRecover;
PolicySubIndex++;
}
break;
case 3:
if(PolicyStateTimer == 0) // Wait tSrcRecover to turn VBUS on
{
PolicySubIndex++;
}
break;
default:
platform_set_vbus_lvl_enable(VBUS_LVL_5V, TRUE, FALSE); // Enable the 5V source
platform_set_vconn_enable(TRUE);
if(blnCCPinIsCC1)
{
Registers.Switches.VCONN_CC2 = 1;
}
else
{
Registers.Switches.VCONN_CC1 = 1;
}
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
IsVCONNSource = TRUE;
NoResponseTimer = tNoResponse; // Initialize the no response timer
PolicyState = peSourceStartup; // Go to the startup state
PolicySubIndex = 0;
break;
}
}
void PolicySourceNegotiateCap(void)
{
// This state evaluates if the sink request can be met or not and sets the next state accordingly
FSC_BOOL reqAccept = FALSE; // Set a flag that indicates whether we will accept or reject the request
FSC_U8 objPosition; // Get the requested object position
objPosition = PolicyRxDataObj[0].FVRDO.ObjectPosition; // Get the object position reference
if ((objPosition > 0) && (objPosition <= CapsHeaderSource.NumDataObjects)) // Make sure the requested object number if valid, continue validating request
{
if ((PolicyRxDataObj[0].FVRDO.OpCurrent <= CapsSource[objPosition-1].FPDOSupply.MaxCurrent)) // Ensure the default power/current request is available
reqAccept = TRUE; // If the source can supply the request, set the flag to respond
}
if (reqAccept) // If we have received a valid request...
{
PolicyState = peSourceTransitionSupply; // Go to the transition supply state
}
else // Otherwise the request was invalid...
PolicyState = peSourceCapabilityResponse; // Go to the capability response state to send a reject/wait message
}
void PolicySourceTransitionSupply(void)
{
FSC_U8 sourceVoltage = 0;
switch (PolicySubIndex)
{
case 0:
PolicySendCommand(CMTAccept, peSourceTransitionSupply, 1); // Send the Accept message
break;
case 1:
PolicyStateTimer = tSrcTransition; // Initialize the timer to allow for the sink to transition
PolicySubIndex++; // Increment to move to the next sub state
break;
case 2:
if (!PolicyStateTimer) // If the timer has expired (the sink is ready)...
PolicySubIndex++; // Increment to move to the next sub state
break;
case 3:
PolicyHasContract = TRUE; // Set the flag to indicate that a contract is in place
USBPDContract.object = PolicyRxDataObj[0].object; // Set the contract to the sink request
//TODO: More robust selection of PDO
sourceVoltage = CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage;
if (sourceVoltage == 100) // If the chosen object is 5V
{
if(platform_get_vbus_lvl_enable(VBUS_LVL_5V)) // If the supply is already enabled, go to PS_READY
{
PolicySubIndex = 5;
}
else
{
platform_set_vbus_lvl_enable(VBUS_LVL_5V, TRUE, FALSE);
PolicyStateTimer = t5To12VTransition; // Set the policy state timer to allow the load switch time to turn off so we don't short our supplies
PolicySubIndex++;
}
}
#ifdef FM150911A
else if (sourceVoltage == 240) // If the chosen object is 12V
{
if(platform_get_vbus_lvl_enable(VBUS_LVL_12V)) // If the supply is already enabled, go to PS_READY
{
PolicySubIndex = 5;
}
else
{
platform_set_vbus_lvl_enable(VBUS_LVL_12V, TRUE, FALSE);
PolicyStateTimer = t5To12VTransition; // Set the policy state timer to allow the load switch time to turn off so we don't short our supplies
PolicySubIndex++;
}
}
#endif // FM150911A
else // Default to vSafe5V
{
if(platform_get_vbus_lvl_enable(VBUS_LVL_5V)) // If the supply is already enabled, go to PS_READY
{
PolicySubIndex = 5;
}
else
{
platform_set_vbus_lvl_enable(VBUS_LVL_5V, TRUE, FALSE);
PolicyStateTimer = t5To12VTransition; // Set the policy state timer to allow the load switch time to turn off so we don't short our supplies
PolicySubIndex++;
}
}
break;
case 4:
// Validate the output is ready prior to sending the ready message (only using a timer for now, could validate using an ADC as well)
if (PolicyStateTimer == 0)
{
if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 100)
{
platform_set_vbus_lvl_enable(VBUS_LVL_5V, TRUE, TRUE); // Disable the "other" vbus outputs
}
#ifdef FM150911A
else if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 240)
{
platform_set_vbus_lvl_enable(VBUS_LVL_12V, TRUE, TRUE); // Disable the "other" vbus outputs
}
#endif // FM150911A
else
{
platform_set_vbus_lvl_enable(VBUS_LVL_5V, TRUE, TRUE); // Disable the "other" vbus outputs
}
PolicyStateTimer = tSourceRiseTimeout; // Source rise timeout
PolicySubIndex++; // Increment to move to the next sub state
}
break;
case 5:
if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 100)
{
if((!isVBUSOverVoltage(VBUS_MDAC_5P04) && isVBUSOverVoltage(VBUS_MDAC_4P62)) || (PolicyStateTimer == 0)) // Check that VBUS is between 4.6 and 5.5 V
{
PolicySubIndex++;
}
}
#ifdef FM150911A
else if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 240)
{
if((isVBUSOverVoltage(VBUS_MDAC_11P76)) || (PolicyStateTimer == 0)) // Check that VBUS is over 11.8V
{
PolicySubIndex++;
}
}
#endif // FM150911A
else if(PolicyStateTimer == 0)
{
PolicySubIndex++;
}
break;
default:
if (PolicySendCommand(CMTPS_RDY, peSourceReady, 0) == STAT_SUCCESS) // Send the PS_RDY message and move onto the Source Ready state
{
if (Registers.Control.HOST_CUR == 0b01) // Host current must be set to 1.5A or 3.0A in explicit contract
{
Registers.Control.HOST_CUR = 0b10;
DeviceWrite(regControl0, 1, &Registers.Control.byte[0]);
}
platform_notify_pd_contract(TRUE);
}
break;
}
}
void PolicySourceCapabilityResponse(void)
{
if (PolicyHasContract) // If we currently have a contract, issue the reject and move back to the ready state
{
if(isContractValid)
{
PolicySendCommand(CMTReject, peSourceReady, 0); // Send the message and continue onto the ready state
}
else
{
PolicySendCommand(CMTReject, peSourceSendHardReset, 0); // Send the message and continue onto the ready state
}
}
else // If there is no contract in place, issue a hard reset
{
PolicySendCommand(CMTReject, peSourceWaitNewCapabilities, 0); // Send Reject and continue onto the Source Wait New Capabilities state after success
}
}
void PolicySourceReady(void)
{
if (ProtocolMsgRx) // Have we received a message from the sink?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTGetSourceCap:
PolicyState = peSourceGiveSourceCaps; // Send out the caps
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGetSinkCap: // If we receive a get sink capabilities message...
PolicyState = peSourceGiveSinkCaps; // Go evaluate whether we are going to send sink caps or reject
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTDR_Swap: // If we get a DR_Swap message...
PolicyState = peSourceEvaluateDRSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTPR_Swap:
PolicyState = peSourceEvaluatePRSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTVCONN_Swap: // If we get a VCONN_Swap message...
PolicyState = peSourceEvaluateVCONNSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTSoftReset:
PolicyState = peSourceSoftReset; // Go to the soft reset state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // Send a reject message for all other commands
break;
}
}
else // If we received a data message... for now just send a soft reset
{
switch (PolicyRxHeader.MessageType)
{
case DMTRequest:
PolicyState = peSourceNegotiateCap; // If we've received a request object, go to the negotiate capabilities state
break;
#ifdef FSC_HAVE_VDM
case DMTVenderDefined:
convertAndProcessVdmMessage(ProtocolMsgRxSop);
break;
#endif // FSC_HAVE_VDM
case DMTBIST:
processDMTBIST();
break;
default: // Otherwise we've received a message we don't know how to handle yet
break;
}
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
}
else if (USBPDTxFlag) // Has the device policy manager requested us to send a message?
{
if (PDTransmitHeader.NumDataObjects == 0)
{
switch (PDTransmitHeader.MessageType) // Determine which command we need to send
{
case CMTGetSinkCap:
PolicyState = peSourceGetSinkCaps; // Go to the get sink caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGetSourceCap:
PolicyState = peSourceGetSourceCaps; // Go to the get source caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTPing:
PolicyState = peSourceSendPing; // Go to the send ping state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGotoMin:
PolicyState = peSourceGotoMin; // Go to the source goto min state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
#ifdef FSC_HAVE_DRP
case CMTPR_Swap:
PolicyState = peSourceSendPRSwap; // Issue a PR_Swap message
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
#endif // FSC_HAVE_DRP
case CMTDR_Swap:
PolicyState = peSourceSendDRSwap; // Issue a DR_Swap message
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTVCONN_Swap:
PolicyState = peSourceSendVCONNSwap; // Issue a VCONN_Swap message
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTSoftReset:
PolicyState = peSourceSendSoftReset; // Go to the soft reset state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // Don't send any commands we don't know how to handle yet
break;
}
}
else
{
switch (PDTransmitHeader.MessageType)
{
case DMTSourceCapabilities:
PolicyState = peSourceSendCaps;
PolicySubIndex = 0;
PDTxStatus = txIdle;
break;
case DMTVenderDefined:
PolicySubIndex = 0;
#ifdef FSC_HAVE_VDM
doVdmCommand();
#endif // FSC_HAVE_VDM
break;
default:
break;
}
}
USBPDTxFlag = FALSE;
}
else if(PartnerCaps.object == 0)
{
PolicyState = peSourceGetSinkCaps; // Go to the get sink caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
#ifdef FSC_HAVE_VDM
else if(PolicyIsDFP
&& (AutoVdmState != AUTO_VDM_DONE)
#ifdef FSC_DEBUG
&& (GetUSBPDBufferNumBytes() == 0)
#endif // FSC_DEBUG
)
{
autoVdmDiscovery();
}
#endif // FSC_HAVE_VDM
else
{
#ifdef FSC_INTERRUPT_TRIGGERED
if(loopCounter == 0)
{
g_Idle = TRUE; // Idle until COMP or HARDRST or GCRCSENT
platform_enable_timer(FALSE);
}
#endif // FSC_INTERRUPT_TRIGGERED
}
}
void PolicySourceGiveSourceCap(void)
{
PolicySendData(DMTSourceCapabilities, CapsHeaderSource.NumDataObjects, &CapsSource[0], peSourceReady, 0, SOP_TYPE_SOP);
}
void PolicySourceGetSourceCap(void)
{
PolicySendCommand(CMTGetSourceCap, peSourceReady, 0);
}
void PolicySourceGetSinkCap(void)
{
switch (PolicySubIndex)
{
case 0:
if (PolicySendCommand(CMTGetSinkCap, peSourceGetSinkCaps, 1) == STAT_SUCCESS) // Send the get sink caps command upon entering state
PolicyStateTimer = tSenderResponse; // Start the sender response timer upon receiving the good CRC message
break;
default:
if (ProtocolMsgRx) // If we have received a message
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if ((PolicyRxHeader.NumDataObjects > 0) && (PolicyRxHeader.MessageType == DMTSinkCapabilities))
{
UpdateCapabilitiesRx(FALSE);
PolicyState = peSourceReady; // Go onto the source ready state
}
else // If we didn't receive a valid sink capabilities message...
{
PolicyState = peSourceSendHardReset; // Go onto issuing a hard reset
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
else if (!PolicyStateTimer) // If we didn't get a response to our request before timing out...
{
PolicyState = peSourceSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
}
}
void PolicySourceGiveSinkCap(void)
{
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP)
PolicySendData(DMTSinkCapabilities, CapsHeaderSink.NumDataObjects, &CapsSink[0], peSourceReady, 0, SOP_TYPE_SOP);
else
#endif // FSC_HAVE_DRP
PolicySendCommand(CMTReject, peSourceReady, 0); // Send the reject message and continue onto the ready state
}
void PolicySourceSendPing(void)
{
PolicySendCommand(CMTPing, peSourceReady, 0);
}
void PolicySourceGotoMin(void)
{
if (ProtocolMsgRx)
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a control message...
{
switch(PolicyRxHeader.MessageType) // Determine the message type
{
case CMTSoftReset:
PolicyState = peSourceSoftReset; // Go to the soft reset state if we received a reset command
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
break;
default: // If we receive any other command (including Reject & Wait), just go back to the ready state without changing
break;
}
}
}
else
{
switch (PolicySubIndex)
{
case 0:
PolicySendCommand(CMTGotoMin, peSourceGotoMin, 1); // Send the GotoMin message
break;
case 1:
PolicyStateTimer = tSrcTransition; // Initialize the timer to allow for the sink to transition
PolicySubIndex++; // Increment to move to the next sub state
break;
case 2:
if (!PolicyStateTimer) // If the timer has expired (the sink is ready)...
PolicySubIndex++; // Increment to move to the next sub state
break;
case 3:
// Adjust the power supply if necessary...
PolicySubIndex++; // Increment to move to the next sub state
break;
case 4:
// Validate the output is ready prior to sending the ready message
PolicySubIndex++; // Increment to move to the next sub state
break;
default:
PolicySendCommand(CMTPS_RDY, peSourceReady, 0); // Send the PS_RDY message and move onto the Source Ready state
break;
}
}
}
void PolicySourceSendDRSwap(void)
{
FSC_U8 Status;
switch (PolicySubIndex)
{
case 0:
Status = PolicySendCommandNoReset(CMTDR_Swap, peSourceSendDRSwap, 1); // Send the DR_Swap message
if (Status == STAT_SUCCESS) { // If we received the good CRC message...
pr_info("FUSB %s: send data role swap, status(%d)\n", __func__, Status);
PolicyStateTimer = tSenderResponse; // Initialize for SenderResponseTimer
} else if (Status == STAT_ERROR) { // If there was an error...
pr_err("FUSB %s: send data role swap, status(%d)\n", __func__, Status);
PolicyState = peErrorRecovery; // Go directly to the error recovery state
}
break;
default:
if (ProtocolMsgRx)
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a control message...
{
switch(PolicyRxHeader.MessageType) // Determine the message type
{
case CMTAccept:
PolicyIsDFP = (PolicyIsDFP == TRUE) ? FALSE : TRUE; // Flip the current data role
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302 for the auto CRC
PolicyState = peSourceReady; // Source ready state
pr_info("FUSB %s: accept, PolicyIsDFP(%d)\n", __func__, PolicyIsDFP);
platform_notify_attached_source(PolicyIsDFP, true);
break;
case CMTSoftReset:
PolicyState = peSourceSoftReset; // Go to the soft reset state if we received a reset command
break;
default: // If we receive any other command (including Reject & Wait), just go back to the ready state without changing
PolicyState = peSourceReady; // Go to the source ready state
break;
}
}
else // Otherwise we received a data message...
{
PolicyState = peSourceReady; // Go to the sink ready state if we received a unexpected data message (ignoring message)
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else if (PolicyStateTimer == 0) // If the sender response timer times out...
{
PolicyState = peSourceReady; // Go to the source ready state if the SenderResponseTimer times out
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
break;
}
}
void PolicySourceEvaluateDRSwap(void)
{
FSC_U8 Status;
#ifdef FSC_HAVE_VDM
if (mode_entered == TRUE) // If were are in modal operation, send a hard reset
{
PolicyState = peSourceSendHardReset;
PolicySubIndex = 0;
return;
}
#endif // FSC_HAVE_VDM
pr_info("FUSB %s: enter\n", __func__);
Status = PolicySendCommandNoReset(CMTAccept, peSourceReady, 0); // Send the Accept message and wait for the good CRC
if (Status == STAT_SUCCESS) // If we received the good CRC...
{
PolicyIsDFP = (PolicyIsDFP == TRUE) ? FALSE : TRUE; // We're not really doing anything except flipping the bit
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302 for the auto CRC
pr_info("FUSB %s: accept, PolicyIsDFP(%d)\n", __func__, PolicyIsDFP);
platform_notify_attached_source(PolicyIsDFP, true);
}
else if (Status == STAT_ERROR) // If we didn't receive the good CRC...
{
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub-index
PDTxStatus = txIdle; // Clear the transmitter status
}
}
void PolicySourceSendVCONNSwap(void)
{
switch(PolicySubIndex)
{
case 0:
if (PolicySendCommand(CMTVCONN_Swap, peSourceSendVCONNSwap, 1) == STAT_SUCCESS) // Send the VCONN_Swap message and wait for the good CRC
PolicyStateTimer = tSenderResponse; // Once we receive the good CRC, set the sender response timer
break;
case 1:
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTAccept: // If we get the Accept message...
PolicySubIndex++; // Increment the subindex to move onto the next step
break;
case CMTWait: // If we get either the reject or wait message...
case CMTReject:
PolicyState = peSourceReady; // Go back to the source ready state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the SenderResponseTimer times out...
{
PolicyState = peSourceReady; // Go back to the source ready state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
case 2:
if (IsVCONNSource) // If we are currently sourcing VCONN...
{
PolicyStateTimer = tVCONNSourceOn; // Enable the VCONNOnTimer and wait for a PS_RDY message
PolicySubIndex++; // Increment the subindex to move to waiting for a PS_RDY message
}
else // Otherwise we need to start sourcing VCONN
{
platform_set_vconn_enable(TRUE);
if (blnCCPinIsCC1) // If the CC pin is CC1...
Registers.Switches.VCONN_CC2 = 1; // Enable VCONN for CC2
else // Otherwise the CC pin is CC2
Registers.Switches.VCONN_CC1 = 1; // so enable VCONN on CC1
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
IsVCONNSource = TRUE;
PolicyStateTimer = VbusTransitionTime; // Allow time for the FPF2498 to enable...
PolicySubIndex = 4; // Skip the next state and move onto sending the PS_RDY message after the timer expires }
}
break;
case 3:
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
Registers.Switches.VCONN_CC1 = 0; // Disable the VCONN source
Registers.Switches.VCONN_CC2 = 0; // Disable the VCONN source
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
platform_set_vconn_enable(FALSE);
IsVCONNSource = FALSE;
PolicyState = peSourceReady; // Move onto the Sink Ready state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the VCONNOnTimer times out...
{
PolicyState = peSourceSendHardReset; // Issue a hard reset
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default:
if (!PolicyStateTimer)
{
PolicySendCommand(CMTPS_RDY, peSourceReady, 0); // Send the Accept message and wait for the good CRC
}
break;
}
}
void PolicySourceSendPRSwap(void)
{
#ifdef FSC_HAVE_DRP
FSC_U8 Status;
switch(PolicySubIndex)
{
case 0: // Send the PRSwap command
pr_info("FUSB %s: send PR_Swap command\n", __func__);
if (PolicySendCommand(CMTPR_Swap, peSourceSendPRSwap, 1) == STAT_SUCCESS) // Send the PR_Swap message and wait for the good CRC
PolicyStateTimer = tSenderResponse; // Once we receive the good CRC, set the sender response timer
break;
case 1: // Require Accept message to move on or go back to ready state
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTAccept: // If we get the Accept message...
IsPRSwap = TRUE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
PRSwapTimer = tPRSwapBailout; // Initialize the PRSwapTimer to indicate we are in the middle of a swap
PolicyStateTimer = tSrcTransition; // Start the sink transition timer
PolicySubIndex++; // Increment the subindex to move onto the next step
break;
case CMTWait: // If we get either the reject or wait message...
case CMTReject:
PolicyState = peSourceReady; // Go back to the source ready state
PolicySubIndex = 0; // Clear the sub index
IsPRSwap = FALSE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the SenderResponseTimer times out...
{
PolicyState = peSourceReady; // Go back to the source ready state
PolicySubIndex = 0; // Clear the sub index
IsPRSwap = FALSE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
case 2: // Wait for tSrcTransition and then turn off power (and Rd on/Rp off)
if (!PolicyStateTimer)
{
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
platform_set_vbus_discharge(TRUE); // Enabled VBUS discharge path
PolicySubIndex++; // Increment the sub-index to move onto the next state
}
break;
case 3:
if (VbusVSafe0V())
{
RoleSwapToAttachedSink();
platform_set_vbus_discharge(FALSE); // Disable VBUS discharge path
PolicyIsSource = FALSE;
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
PolicySubIndex++;
}
break;
case 4: // Allow time for the supply to fall and then send the PS_RDY message
Status = PolicySendCommandNoReset(CMTPS_RDY, peSourceSendPRSwap, 5);
pr_debug("FUSB %s PS_READY\n", __func__);
if (Status == STAT_SUCCESS) // If we successfully sent the PS_RDY command and received the goodCRC
PolicyStateTimer = tPSSourceOn; // Start the PSSourceOn timer to allow time for the new supply to come up
else if (Status == STAT_ERROR)
PolicyState = peErrorRecovery; // If we get an error, go to the error recovery state
break;
case 5: // Wait to receive a PS_RDY message from the new DFP
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
PolicySubIndex++; // Clear the sub index
PolicyStateTimer = tGoodCRCDelay; // Make sure GoodCRC has time to send
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the PSSourceOn times out...
{
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default:
if (PolicyStateTimer == 0)
{
PolicyState = peSinkStartup; // Go to the sink startup state
PolicySubIndex = 0; // Clear the sub index
PolicyStateTimer = 0; // Make sure GoodCRC has time to send
}
break;
}
#endif // FSC_HAVE_DRP
}
void PolicySourceEvaluatePRSwap(void)
{
#ifdef FSC_HAVE_DRP
FSC_U8 Status;
switch(PolicySubIndex)
{
case 0: // Send either the Accept or Reject command
pr_info("FUSB %s: Source.DRPower=%d, Sink.DRPower=%d, Source.Ext=%d, Sink.Ext=%d, Sink.ST=%d",
__func__, CapsSource[0].FPDOSupply.DualRolePower, PartnerCaps.FPDOSink.DualRolePower,
CapsSource[0].FPDOSupply.ExternallyPowered, PartnerCaps.FPDOSink.ExternallyPowered, PartnerCaps.FPDOSink.SupplyType);
if ((CapsSource[0].FPDOSupply.DualRolePower == FALSE) || // Determine Accept/Reject based on DualRolePower bit in current PDO
((CapsSource[0].FPDOSupply.ExternallyPowered == TRUE) && // Must also reject if we are externally powered and partner is not
(PartnerCaps.FPDOSink.SupplyType == pdoTypeFixed) && (PartnerCaps.FPDOSink.ExternallyPowered == FALSE)))
{
PolicySendCommand(CMTReject, peSourceReady, 0); // Send the reject if we are not a DRP
}
else
{
if (PolicySendCommand(CMTAccept, peSourceEvaluatePRSwap, 1) == STAT_SUCCESS) // Send the Accept message and wait for the good CRC
{
IsPRSwap = TRUE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
RoleSwapToAttachedSink();
PolicyStateTimer = tSrcTransition;
}
}
break;
case 1:
if(PolicyStateTimer == 0)
{
platform_set_vbus_lvl_enable(VBUS_LVL_ALL, FALSE, FALSE); // Disable VBUS output
platform_set_vbus_discharge(TRUE); // Enabled VBUS discharge path
PolicySubIndex++;
}
break;
case 2:
if (VbusVSafe0V()) // Allow time for the supply to fall and then send the PS_RDY message
{
PolicyStateTimer = tSrcTransition; // Allow some extra time for VBUS to discharge
PolicyIsSource = FALSE;
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
PolicySubIndex++;
}
break;
case 3:
if(PolicyStateTimer == 0)
{
platform_set_vbus_discharge(FALSE); // Disable VBUS discharge path
PolicySubIndex++;
}
break;
case 4:
Status = PolicySendCommandNoReset(CMTPS_RDY, peSourceEvaluatePRSwap, 5); // Send the PS_RDY message
if (Status == STAT_SUCCESS) // If we successfully sent the PS_RDY command and received the goodCRC
PolicyStateTimer = tPSSourceOn; // Start the PSSourceOn timer to allow time for the new supply to come up
else if (Status == STAT_ERROR)
PolicyState = peErrorRecovery; // If we get an error, go to the error recovery state
break;
case 5: // Wait to receive a PS_RDY message from the new DFP
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
pr_debug("FUSB %s: receive PS_RDY\n", __func__);
PolicySubIndex++; // Increment the sub index
PolicyStateTimer = tGoodCRCDelay; // Make sure GoodCRC has time to send
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the PSSourceOn times out...
{
PolicyState = peSourceSendHardReset; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default:
if (PolicyStateTimer == 0)
{
PolicyState = peSinkStartup; // Go to the sink startup state
PolicySubIndex = 0; // Clear the sub index
PolicyStateTimer = 0;
}
break;
}
#else
PolicySendCommand(CMTReject, peSourceReady, 0); // Send the reject if we are not a DRP
#endif // FSC_HAVE_DRP
}
void PolicySourceWaitNewCapabilities(void) // TODO: DPM integration
{
#ifdef FSC_INTERRUPT_TRIGGERED
if(loopCounter == 0)
{
g_Idle = TRUE; // Wait for COMP or HARDRST or GCRCSENT
Registers.Mask.M_COMP_CHNG = 0;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.M_HARDRST = 0;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
platform_enable_timer(FALSE);
}
#endif // FSC_INTERRUPT_TRIGGERED
switch(PolicySubIndex)
{
case 0:
// Wait for Policy Manager to change source capabilities
break;
default:
// Transition to peSourceSendCapabilities
PolicyState = peSourceSendCaps;
PolicySubIndex = 0;
break;
}
}
#endif // FSC_HAVE_SRC
void PolicySourceEvaluateVCONNSwap(void)
{
switch(PolicySubIndex)
{
case 0:
pr_info("FUSB %s: accept vconn swap, IsVCONNSource=%d\n", __func__, IsVCONNSource);
PolicySendCommand(CMTAccept, peSourceEvaluateVCONNSwap, 1); // Send the Accept message and wait for the good CRC
break;
case 1:
if (IsVCONNSource) // If we are currently sourcing VCONN...
{
PolicyStateTimer = tVCONNSourceOn; // Enable the VCONNOnTimer and wait for a PS_RDY message
PolicySubIndex++; // Increment the subindex to move to waiting for a PS_RDY message
}
else // Otherwise we need to start sourcing VCONN
{
platform_set_vconn_enable(TRUE);
if (blnCCPinIsCC1) // If the CC pin is CC1...
{
Registers.Switches.VCONN_CC2 = 1; // Enable VCONN for CC2
Registers.Switches.PDWN2 = 0; // Disable the pull-down on CC2 to avoid sinking unnecessary current
}
else // Otherwise the CC pin is CC2
{
Registers.Switches.VCONN_CC1 = 1; // Enable VCONN for CC1
Registers.Switches.PDWN1 = 0; // Disable the pull-down on CC1 to avoid sinking unnecessary current
}
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
IsVCONNSource = TRUE;
PolicyStateTimer = VbusTransitionTime; // Allow time for the FPF2498 to enable...
PolicySubIndex = 3; // Skip the next state and move onto sending the PS_RDY message after the timer expires }
}
break;
case 2:
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
Registers.Switches.VCONN_CC1 = 0; // Disable the VCONN source
Registers.Switches.VCONN_CC2 = 0; // Disable the VCONN source
Registers.Switches.PDWN1 = 1; // Ensure the pull-down on CC1 is enabled
Registers.Switches.PDWN2 = 1; // Ensure the pull-down on CC2 is enabled
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
platform_set_vconn_enable(FALSE);
IsVCONNSource = FALSE;
PolicyState = peSourceReady; // Move onto the Sink Ready state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the VCONNOnTimer times out...
{
PolicyState = peSourceSendHardReset; // Issue a hard reset
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default:
if (!PolicyStateTimer)
{
PolicySendCommand(CMTPS_RDY, peSourceReady, 0); // Send the Accept message and wait for the good CRC
}
break;
}
}
// ############################## Sink States ############################## //
#ifdef FSC_HAVE_SNK
void PolicySinkSendHardReset(void)
{
IsHardReset = TRUE;
PolicySendHardReset(peSinkTransitionDefault, 0);
}
void PolicySinkSoftReset(void)
{
if (PolicySendCommand(CMTAccept, peSinkWaitCaps, 0) == STAT_SUCCESS)
PolicyStateTimer = tSinkWaitCap;
}
void PolicySinkSendSoftReset(void)
{
switch (PolicySubIndex)
{
case 0:
if (PolicySendCommand(CMTSoftReset, peSinkSendSoftReset, 1) == STAT_SUCCESS) // Send the soft reset command to the protocol layer
{
PolicyStateTimer = tSenderResponse; // Start the sender response timer to wait for an accept message once successfully sent
}
break;
default:
if (ProtocolMsgRx) // If we have received a message
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we've handled it here
if ((PolicyRxHeader.NumDataObjects == 0) && (PolicyRxHeader.MessageType == CMTAccept)) // And it was the Accept...
{
PolicyState = peSinkWaitCaps; // Go to the wait for capabilities state
PolicyStateTimer = tSinkWaitCap; // Set the state timer to tSinkWaitCap
}
else // Otherwise it was a message that we didn't expect, so...
PolicyState = peSinkSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
else if (!PolicyStateTimer) // If we didn't get a response to our request before timing out...
{
PolicyState = peSinkSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
}
}
void PolicySinkTransitionDefault(void)
{
switch (PolicySubIndex)
{
case 0:
IsHardReset = TRUE;
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
Registers.Switches.AUTO_CRC = 0; // turn off Auto CRC
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
PolicyStateTimer = tPSHardResetMax + tSafe0V; // Timeout wait for vSafe0V
NoResponseTimer = tNoResponse; // Initialize the no response timer
if(PolicyIsDFP) // Make sure data role is UFP
{
PolicyIsDFP = FALSE; // Set the current data role
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302
platform_notify_attached_source(PolicyIsDFP, true);
}
if(IsVCONNSource) // Disable VCONN if VCONN Source
{
Registers.Switches.VCONN_CC1 = 0;
Registers.Switches.VCONN_CC2 = 0;
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
platform_set_vconn_enable(FALSE);
IsVCONNSource = FALSE;
}
PolicySubIndex++;
break;
case 1:
if(VbusVSafe0V())
{
PolicySubIndex++;
PolicyStateTimer = tSrcRecoverMax + tSrcTurnOn; // Timeout wait for vSafe5V
}
else if (PolicyStateTimer == 0) // Break out if we never see 0V
{
if(PolicyHasContract)
{
PolicyState = peErrorRecovery;
PolicySubIndex = 0;
}
else
{
PolicyState = peSinkStartup;
PolicySubIndex = 0;
PolicyStateTimer = 0;
}
}
break;
case 2:
if(isVBUSOverVoltage(VBUS_MDAC_4P20))
{
PolicySubIndex++;
}
else if (PolicyStateTimer == 0) // Break out if we never see 0V
{
if(PolicyHasContract)
{
PolicyState = peErrorRecovery;
PolicySubIndex = 0;
}
else
{
PolicyState = peSinkStartup;
PolicySubIndex = 0;
PolicyStateTimer = 0;
}
}
break;
default:
PolicyState = peSinkStartup; // Go to the startup state
PolicySubIndex = 0; // Clear the sub index
PolicyStateTimer = 0;
PDTxStatus = txIdle; // Reset the transmitter status
break;
}
}
void PolicySinkStartup(void)
{
#ifdef FSC_HAVE_VDM
FSC_S32 i;
#endif // FSC_HAVE_VDM
// Set masks for PD
Registers.Mask.M_COLLISION = 0;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.M_RETRYFAIL = 0;
Registers.MaskAdv.M_TXSENT = 0;
Registers.MaskAdv.M_HARDRST = 0;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 0;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
if(Registers.DeviceID.VERSION_ID == VERSION_302B)
{
if(Registers.Control.BIST_TMODE == 1)
{
Registers.Control.BIST_TMODE = 0; // Disable auto-flush RxFIFO
DeviceWrite(regControl3, 1, &Registers.Control.byte[3]);
}
}
else
{
if(Registers.Control.RX_FLUSH == 1) // Disable Rx flushing if it has been enabled
{
Registers.Control.RX_FLUSH = 0;
DeviceWrite(regControl1, 1, &Registers.Control.byte[1]);
}
}
USBPDContract.object = 0; // Clear the USB PD contract (output power to 5V default)
PartnerCaps.object = 0; // Clear partner sink caps
IsPRSwap = FALSE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PolicyIsSource = FALSE; // Clear the flag to indicate that we are a sink (for PRSwaps)
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
Registers.Switches.AUTO_CRC = 1; // turn on Auto CRC
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
ResetProtocolLayer(TRUE); // Reset the protocol layer
CapsCounter = 0; // Clear the caps counter
CollisionCounter = 0; // Reset the collision counter
PolicyStateTimer = 0; // Reset the policy state timer
PolicyState = peSinkDiscovery; // Go to the sink discovery state
PolicySubIndex = 0; // Reset the sub index
#ifdef FSC_HAVE_VDM
AutoVdmState = AUTO_VDM_INIT;
auto_mode_disc_tracker = 0;
mode_entered = FALSE;
core_svid_info.num_svids = 0;
for (i = 0; i < MAX_NUM_SVIDS; i++) {
core_svid_info.svids[i] = 0;
}
#endif // FSC_HAVE_VDM
#ifdef FSC_HAVE_DP
AutoDpModeEntryObjPos = -1;
resetDp();
#endif // FSC_HAVE_DP
}
void PolicySinkDiscovery(void)
{
IsHardReset = FALSE;
PRSwapTimer = 0; // Clear the swap timer
PolicyState = peSinkWaitCaps;
PolicySubIndex = 0;
PolicyStateTimer = tTypeCSinkWaitCap;
}
void PolicySinkWaitCaps(void)
{
if (ProtocolMsgRx) // If we have received a message...
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if ((PolicyRxHeader.NumDataObjects > 0) && (PolicyRxHeader.MessageType == DMTSourceCapabilities)) // Have we received a valid source cap message?
{
UpdateCapabilitiesRx(TRUE); // Update the received capabilities
PolicyState = peSinkEvaluateCaps; // Set the evaluate source capabilities state
}
else if ((PolicyRxHeader.NumDataObjects == 0) && (PolicyRxHeader.MessageType == CMTSoftReset))
{
PolicyState = peSinkSoftReset; // Go to the soft reset state
}
PolicySubIndex = 0; // Reset the sub index
}
else if ((PolicyHasContract == TRUE) && (NoResponseTimer == 0) && (HardResetCounter > nHardResetCount))
{
PolicyState = peErrorRecovery;
PolicySubIndex = 0;
}
else if ((PolicyStateTimer == 0) && (HardResetCounter <= nHardResetCount))
{
PolicyState = peSinkSendHardReset;
PolicySubIndex = 0;
}
else if ((PolicyHasContract == FALSE) && (NoResponseTimer == 0) && (HardResetCounter > nHardResetCount))
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = TRUE; // Wait for VBUSOK or HARDRST or GCRCSENT
platform_enable_timer(FALSE);
#endif // FSC_INTERRUPT_TRIGGERED
}
}
extern FSC_S32 platform_select_source_capability(u8 obj_cnt, doDataObject_t pd_data[7], int *device_max_ma);
void PolicySinkEvaluateCaps(void)
{
// Due to latency with the PC and evaluating capabilities, we are always going to select the first one by default (5V default)
// This will allow the software time to determine if they want to select one of the other capabilities (user selectable)
// If we want to automatically show the selection of a different capabilities message, we need to build in the functionality here
// The evaluate caps
FSC_S32 i, reqPos;
FSC_S32 device_max_ma = 0;
FSC_U32 objVoltage = 0;
FSC_U32 objCurrent, objPower, MaxPower, SelVoltage, ReqCurrent;
objCurrent = 0;
NoResponseTimer = T_TIMER_DISABLE; // Stop the no response timer
HardResetCounter = 0; // Reset the hard reset counter // Indicate Hard Reset is over
SelVoltage = 0;
MaxPower = 0;
reqPos = 0; // Select nothing in case there is an error...
for (i=0; i<CapsHeaderReceived.NumDataObjects; i++) // Going to select the highest power object that we are compatible with
{
switch (CapsReceived[i].PDO.SupplyType)
{
case pdoTypeFixed:
objVoltage = CapsReceived[i].FPDOSupply.Voltage; // Get the output voltage of the fixed supply
if (objVoltage > SinkRequestMaxVoltage) // If the voltage is greater than our limit...
objPower = 0; // Set the power to zero to ignore the object
else // Otherwise...
{
objCurrent = CapsReceived[i].FPDOSupply.MaxCurrent;
objPower = objVoltage * objCurrent; // Calculate the power for comparison
}
break;
case pdoTypeVariable:
objVoltage = CapsReceived[i].VPDO.MaxVoltage; // Grab the maximum voltage of the variable supply
if (objVoltage > SinkRequestMaxVoltage) // If the max voltage is greater than our limit...
objPower = 0; // Set the power to zero to ignore the object
else // Otherwise...
{
objVoltage = CapsReceived[i].VPDO.MinVoltage; // Get the minimum output voltage of the variable supply
objCurrent = CapsReceived[i].VPDO.MaxCurrent; // Get the maximum output current of the variable supply
objPower = objVoltage * objCurrent; // Calculate the power for comparison (based on min V/max I)
}
break;
case pdoTypeBattery: // We are going to ignore battery powered sources for now
default: // We are also ignoring undefined supply types
objPower = 0; // Set the object power to zero so we ignore for now
break;
}
if (objPower >= MaxPower) // If the current object has power greater than or equal the previous objects
{
MaxPower = objPower; // Store the objects power
SelVoltage = objVoltage; // Store the objects voltage (used for calculations)
reqPos = i + 1; // Store the position of the object
}
}
reqPos = platform_select_source_capability(CapsHeaderReceived.NumDataObjects, CapsReceived, &device_max_ma);
if (reqPos >= 0) {
if (CapsReceived[reqPos].PDO.SupplyType == pdoTypeFixed)
SelVoltage = CapsReceived[reqPos].FPDOSupply.Voltage;
else if (CapsReceived[reqPos].PDO.SupplyType == pdoTypeVariable) {
SelVoltage = CapsReceived[reqPos].VPDO.MinVoltage;
objCurrent = CapsReceived[reqPos].VPDO.MaxCurrent;
}
reqPos++;
}
pr_debug("FUSB %s:cnt = %d, reqPos = %d, SelVoltage = %d, device_max_ma = %d after selection\n", __func__, CapsHeaderReceived.NumDataObjects, reqPos, SelVoltage, device_max_ma);
if ((reqPos > 0) && (SelVoltage > 0))
{
PartnerCaps.object = CapsReceived[0].object;
SinkRequest.FVRDO.ObjectPosition = reqPos & 0x07; // Set the object position selected
SinkRequest.FVRDO.GiveBack = SinkGotoMinCompatible; // Set whether we will respond to the GotoMin message
SinkRequest.FVRDO.NoUSBSuspend = SinkUSBSuspendOperation; // Set whether we want to continue pulling power during USB Suspend
SinkRequest.FVRDO.USBCommCapable = SinkUSBCommCapable; // Set whether USB communications is active
ReqCurrent = device_max_ma / 10;
SinkRequest.FVRDO.OpCurrent = (ReqCurrent & 0x3FF); // Set the current based on the selected voltage (in 10mA units)
SinkRequest.FVRDO.MinMaxCurrent = (ReqCurrent & 0x3FF); // Set the min/max current based on the selected voltage (in 10mA units)
if (SinkGotoMinCompatible) // If the give back flag is set...
SinkRequest.FVRDO.CapabilityMismatch = FALSE; // There can't be a capabilities mismatch
else // Otherwise...
{
if (SelVoltage * ReqCurrent != SinkRequestMaxPower)
{
SinkRequest.FVRDO.CapabilityMismatch = TRUE; // flag the source that we need more power
}
else // Otherwise...
{
SinkRequest.FVRDO.CapabilityMismatch = FALSE; // there is no mismatch in the capabilities
}
}
PolicyState = peSinkSelectCapability; // Go to the select capability state
PolicySubIndex = 0; // Reset the sub index
PolicyStateTimer = tSenderResponse; // Initialize the sender response timer
}
else
{
// For now, we are just going to go back to the wait state instead of sending a reject or reset (may change in future)
PolicyState = peSinkWaitCaps; // Go to the wait for capabilities state
PolicyStateTimer = tTypeCSinkWaitCap; // Set the state timer to tSinkWaitCap
}
}
void PolicySinkSelectCapability(void)
{
switch (PolicySubIndex)
{
case 0:
if (PolicySendData(DMTRequest, 1, &SinkRequest, peSinkSelectCapability, 1, SOP_TYPE_SOP) == STAT_SUCCESS)
{
NoResponseTimer = tSenderResponse; // If there is a good CRC start retry timer
PolicyStateTimer = tSenderResponse; // re-Initialize the sender response timer
}
break;
case 1:
if (ProtocolMsgRx)
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a control message...
{
switch(PolicyRxHeader.MessageType) // Determine the message type
{
case CMTAccept:
PolicyHasContract = TRUE; // Set the flag to indicate that a contract is in place
USBPDContract.object = SinkRequest.object; // Set the actual contract that is in place
PolicyStateTimer = tPSTransition; // Set the transition timer here
PolicyState = peSinkTransitionSink; // Go to the transition state if the source accepted the request
break;
case CMTWait:
case CMTReject:
if(PolicyHasContract)
{
PolicyState = peSinkReady; // Go to the sink ready state if the source rejects or has us wait
}
else
{
PolicyState = peSinkWaitCaps; // If we didn't have a contract, go wait for new source caps
HardResetCounter = nHardResetCount + 1; // Make sure we don't send hard reset to prevent infinite loop
}
break;
case CMTSoftReset:
PolicyState = peSinkSoftReset; // Go to the soft reset state if we received a reset command
break;
default:
PolicyState = peSinkSendSoftReset; // We are going to send a reset message for all other commands received
break;
}
}
else // Otherwise we received a data message...
{
switch (PolicyRxHeader.MessageType)
{
case DMTSourceCapabilities: // If we received a new source capabilities message
UpdateCapabilitiesRx(TRUE); // Update the received capabilities
PolicyState = peSinkEvaluateCaps; // Go to the evaluate caps state
break;
default:
PolicyState = peSinkSendSoftReset; // Send a soft reset to get back to a known state
break;
}
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else if (PolicyStateTimer == 0) // If the sender response timer times out...
{
PolicyState = peSinkSendHardReset; // Go to the hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
break;
}
}
void PolicySinkTransitionSink(void)
{
if (ProtocolMsgRx)
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a control message...
{
switch(PolicyRxHeader.MessageType) // Determine the message type
{
case CMTPS_RDY:
PolicyState = peSinkReady; // Go to the ready state
platform_notify_pd_contract(TRUE);
break;
case CMTSoftReset:
PolicyState = peSinkSoftReset; // Go to the soft reset state if we received a reset command
break;
default:
PolicyState = peSinkSendSoftReset; // We are going to send a reset message for all other commands received
break;
}
}
else // Otherwise we received a data message...
{
switch (PolicyRxHeader.MessageType) // Determine the message type
{
case DMTSourceCapabilities: // If we received new source capabilities...
UpdateCapabilitiesRx(TRUE); // Update the source capabilities
PolicyState = peSinkEvaluateCaps; // And go to the evaluate capabilities state
break;
default: // If we receieved an unexpected data message...
PolicyState = peSinkSendSoftReset; // Send a soft reset to get back to a known state
break;
}
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else if (PolicyStateTimer == 0) // If the PSTransitionTimer times out...
{
PolicyState = peSinkSendHardReset; // Issue a hard reset
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
}
void PolicySinkReady(void)
{
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTGotoMin:
PolicyState = peSinkTransitionSink; // Go to transitioning the sink power
PolicyStateTimer = tPSTransition; // Set the transition timer here
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGetSinkCap:
PolicyState = peSinkGiveSinkCap; // Go to sending the sink caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGetSourceCap:
PolicyState = peSinkGiveSourceCap; // Go to sending the source caps if we are dual-role
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTDR_Swap: // If we get a DR_Swap message...
PolicyState = peSinkEvaluateDRSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTPR_Swap:
PolicyState = peSinkEvaluatePRSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTVCONN_Swap: // If we get a VCONN_Swap message...
PolicyState = peSinkEvaluateVCONNSwap; // Go evaluate whether we are going to accept or reject the swap
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTSoftReset:
PolicyState = peSinkSoftReset; // Go to the soft reset state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
break;
}
}
else
{
switch (PolicyRxHeader.MessageType)
{
case DMTSourceCapabilities:
UpdateCapabilitiesRx(TRUE); // Update the received capabilities
PolicyState = peSinkEvaluateCaps; // Go to the evaluate capabilities state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
#ifdef FSC_HAVE_VDM
case DMTVenderDefined:
convertAndProcessVdmMessage(ProtocolMsgRxSop);
break;
#endif // FSC_HAVE_VDM
case DMTBIST:
processDMTBIST();
break;
default: // If we get something we are not expecting... simply ignore them
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
break;
}
}
}
else if (USBPDTxFlag) // Has the device policy manager requested us to send a message?
{
if (PDTransmitHeader.NumDataObjects == 0)
{
switch (PDTransmitHeader.MessageType)
{
case CMTGetSourceCap:
PolicyState = peSinkGetSourceCap; // Go to retrieve the source caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTGetSinkCap:
PolicyState = peSinkGetSinkCap; // Go to retrieve the sink caps state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
case CMTDR_Swap:
PolicyState = peSinkSendDRSwap; // Issue a DR_Swap message
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
#ifdef FSC_HAVE_DRP
case CMTPR_Swap:
PolicyState = peSinkSendPRSwap; // Issue a PR_Swap message
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
#endif // FSC_HAVE_DRP
case CMTSoftReset:
PolicyState = peSinkSendSoftReset; // Go to the send soft reset state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default:
break;
}
}
else
{
switch (PDTransmitHeader.MessageType)
{
case DMTRequest:
SinkRequest.object = PDTransmitObjects[0].object; // Set the actual object to request
PolicyState = peSinkSelectCapability; // Go to the select capability state
PolicySubIndex = 0; // Reset the sub index
PolicyStateTimer = tSenderResponse; // Initialize the sender response timer
break;
case DMTVenderDefined:
#ifdef FSC_HAVE_VDM
doVdmCommand();
#endif // FSC_HAVE_VDM
break;
default:
break;
}
}
USBPDTxFlag = FALSE;
}
#ifdef FSC_HAVE_VDM
else if (PolicyIsDFP
&& (AutoVdmState != AUTO_VDM_DONE)
#ifdef FSC_DEBUG
&& (GetUSBPDBufferNumBytes() == 0)
#endif // FSC_DEBUG
)
{
autoVdmDiscovery();
}
#endif // FSC_HAVE_VDM
else
{
#ifdef FSC_INTERRUPT_TRIGGERED
g_Idle = TRUE; // Wait for VBUSOK or HARDRST or GCRCSENT
platform_enable_timer(FALSE);
#endif // FSC_INTERRUPT_TRIGGERED
}
}
void PolicySinkGiveSinkCap(void)
{
PolicySendData(DMTSinkCapabilities, CapsHeaderSink.NumDataObjects, &CapsSink[0], peSinkReady, 0, SOP_TYPE_SOP);
}
void PolicySinkGetSinkCap(void)
{
PolicySendCommand(CMTGetSinkCap, peSinkReady, 0);
}
void PolicySinkGiveSourceCap(void)
{
#ifdef FSC_HAVE_DRP
if (PortType == USBTypeC_DRP)
PolicySendData(DMTSourceCapabilities, CapsHeaderSource.NumDataObjects, &CapsSource[0], peSinkReady, 0, SOP_TYPE_SOP);
else
#endif // FSC_HAVE_DRP
PolicySendCommand(CMTReject, peSinkReady, 0); // Send the reject message and continue onto the ready state
}
void PolicySinkGetSourceCap(void)
{
PolicySendCommand(CMTGetSourceCap, peSinkReady, 0);
}
void PolicySinkSendDRSwap(void)
{
FSC_U8 Status;
switch (PolicySubIndex)
{
case 0:
Status = PolicySendCommandNoReset(CMTDR_Swap, peSinkSendDRSwap, 1); // Send the DR_Swap command
if (Status == STAT_SUCCESS) { // If we received a good CRC message...
pr_info("FUSB %s: send data role swap, status(%d)\n", __func__, Status);
PolicyStateTimer = tSenderResponse; // Initialize for SenderResponseTimer if we received the GoodCRC
} else if (Status == STAT_ERROR) { // If there was an error...
pr_err("FUSB %s: send data role swap, status(%d)\n", __func__, Status);
PolicyState = peErrorRecovery; // Go directly to the error recovery state
}
break;
default:
if (ProtocolMsgRx)
{
ProtocolMsgRx = FALSE; // Reset the message ready flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a control message...
{
switch(PolicyRxHeader.MessageType) // Determine the message type
{
case CMTAccept:
PolicyIsDFP = (PolicyIsDFP == TRUE) ? FALSE : TRUE; // Flip the current data role
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302 for the auto CRC
PolicyState = peSinkReady; // Sink ready state
pr_info("FUSB %s: accept, PolicyIsDFP(%d)\n", __func__, PolicyIsDFP);
platform_notify_attached_source(PolicyIsDFP, true);
break;
case CMTSoftReset:
PolicyState = peSinkSoftReset; // Go to the soft reset state if we received a reset command
break;
default: // If we receive any other command (including Reject & Wait), just go back to the ready state without changing
PolicyState = peSinkReady; // Go to the sink ready state
break;
}
}
else // Otherwise we received a data message...
{
PolicyState = peSinkReady; // Go to the sink ready state if we received a unexpected data message (ignoring message)
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else if (PolicyStateTimer == 0) // If the sender response timer times out...
{
PolicyState = peSinkReady; // Go to the sink ready state if the SenderResponseTimer times out
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
break;
}
}
void PolicySinkEvaluateDRSwap(void)
{
FSC_U8 Status;
#ifdef FSC_HAVE_VDM
if (mode_entered == TRUE) // If were are in modal operation, send a hard reset
{
PolicyState = peSinkSendHardReset;
PolicySubIndex = 0;
return;
}
#endif // FSC_HAVE_VDM
pr_info("FUSB %s: enter\n", __func__);
Status = PolicySendCommandNoReset(CMTAccept, peSinkReady, 0); // Send the Accept message and wait for the good CRC
if (Status == STAT_SUCCESS) // If we received the good CRC...
{
PolicyIsDFP = (PolicyIsDFP == TRUE) ? FALSE : TRUE; // We're not really doing anything except flipping the bit
Registers.Switches.DATAROLE = PolicyIsDFP; // Update the data role
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]); // Commit the data role in the 302 for the auto CRC
pr_info("FUSB %s: accept, PolicyIsDFP(%d)\n", __func__, PolicyIsDFP);
platform_notify_attached_source(PolicyIsDFP, true);
}
else if (Status == STAT_ERROR) // If we didn't receive the good CRC...
{
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub-index
PDTxStatus = txIdle; // Clear the transmitter status
}
}
void PolicySinkEvaluateVCONNSwap(void)
{
switch(PolicySubIndex)
{
case 0:
pr_info("FUSB %s: accept vconn swap, IsVCONNSource=%d\n", __func__, IsVCONNSource);
PolicySendCommand(CMTAccept, peSinkEvaluateVCONNSwap, 1); // Send the Accept message and wait for the good CRC
break;
case 1:
if (IsVCONNSource) // If we are currently sourcing VCONN...
{
PolicyStateTimer = tVCONNSourceOn; // Enable the VCONNOnTimer and wait for a PS_RDY message
PolicySubIndex++; // Increment the subindex to move to waiting for a PS_RDY message
}
else // Otherwise we need to start sourcing VCONN
{
platform_set_vconn_enable(TRUE);
if (blnCCPinIsCC1) // If the CC pin is CC1...
{
Registers.Switches.VCONN_CC2 = 1; // Enable VCONN for CC2
Registers.Switches.PDWN2 = 0; // Disable the pull-down on CC2 to avoid sinking unnecessary current
}
else // Otherwise the CC pin is CC2
{
Registers.Switches.VCONN_CC1 = 1; // Enable VCONN for CC1
Registers.Switches.PDWN1 = 0; // Disable the pull-down on CC1 to avoid sinking unnecessary current
}
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
IsVCONNSource = TRUE;
PolicyStateTimer = VbusTransitionTime; // Allow time for the FPF2498 to enable...
PolicySubIndex = 3; // Skip the next state and move onto sending the PS_RDY message after the timer expires }
}
break;
case 2:
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
Registers.Switches.VCONN_CC1 = 0; // Disable the VCONN source
Registers.Switches.VCONN_CC2 = 0; // Disable the VCONN source
Registers.Switches.PDWN1 = 1; // Ensure the pull-down on CC1 is enabled
Registers.Switches.PDWN2 = 1; // Ensure the pull-down on CC2 is enabled
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]); // Commit the register setting to the device
platform_set_vconn_enable(FALSE);
IsVCONNSource = FALSE;
PolicyState = peSinkReady; // Move onto the Sink Ready state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the VCONNOnTimer times out...
{
PolicyState = peSourceSendHardReset; // Issue a hard reset
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default:
if (!PolicyStateTimer)
{
PolicySendCommand(CMTPS_RDY, peSinkReady, 0); // Send the Accept message and wait for the good CRC
}
break;
}
}
void PolicySinkSendPRSwap(void)
{
#ifdef FSC_HAVE_DRP
FSC_U8 Status;
switch(PolicySubIndex)
{
case 0: // Send the PRSwap command
pr_info("FUSB %s: send PR_Swap command\n", __func__);
if (PolicySendCommand(CMTPR_Swap, peSinkSendPRSwap, 1) == STAT_SUCCESS) // Send the PR_Swap message and wait for the good CRC
PolicyStateTimer = tSenderResponse; // Once we receive the good CRC, set the sender response timer
break;
case 1: // Require Accept message to move on or go back to ready state
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTAccept: // If we get the Accept message...
IsPRSwap = TRUE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
PRSwapTimer = tPRSwapBailout; // Initialize the PRSwapTimer to indicate we are in the middle of a swap
PolicyStateTimer = tPSSourceOff; // Start the sink transition timer
PolicySubIndex++; // Increment the subindex to move onto the next step
break;
case CMTWait: // If we get either the reject or wait message...
case CMTReject:
PolicyState = peSinkReady; // Go back to the sink ready state
PolicySubIndex = 0; // Clear the sub index
IsPRSwap = FALSE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PDTxStatus = txIdle; // Clear the transmitter status
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the SenderResponseTimer times out...
{
PolicyState = peSinkReady; // Go back to the sink ready state
PolicySubIndex = 0; // Clear the sub index
IsPRSwap = FALSE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
case 2: // Wait for a PS_RDY message to be received to indicate that the original source is no longer supplying VBUS
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
pr_debug("FUSB %s sink receive PS_READY\n", __func__);
RoleSwapToAttachedSource(); // Initiate the Type-C state machine for a power role swap
PolicyIsSource = TRUE;
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
PolicyStateTimer = tSourceOnDelay; // Allow the output voltage to rise before sending the PS_RDY message
PolicySubIndex++; // Increment the sub-index to move onto the next state
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the PSSourceOn times out...
{
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default: // Allow time for the supply to rise and then send the PS_RDY message
if (!PolicyStateTimer)
{
pr_debug("FUSB %s sink send PS_READY\n", __func__);
Status = PolicySendCommandNoReset(CMTPS_RDY, peSourceStartup, 0); // When we get the good CRC, we move onto the source startup state to complete the swap
if (Status == STAT_ERROR)
PolicyState = peErrorRecovery; // If we get an error, go to the error recovery state
SwapSourceStartTimer = tSwapSourceStart;
}
break;
}
#endif // FSC_HAVE_DRP
}
void PolicySinkEvaluatePRSwap(void)
{
#ifdef FSC_HAVE_DRP
FSC_U8 Status;
switch(PolicySubIndex)
{
case 0: // Send either the Accept or Reject command
if ((PartnerCaps.FPDOSupply.SupplyType == pdoTypeFixed) && (PartnerCaps.FPDOSupply.DualRolePower == FALSE)) // Determine Accept/Reject based on partner's Dual Role Power
{
PolicySendCommand(CMTReject, peSinkReady, 0); // Send the reject if we are not a DRP
}
else
{
if (PolicySendCommand(CMTAccept, peSinkEvaluatePRSwap, 1) == STAT_SUCCESS) // Send the Accept message and wait for the good CRC
{
IsPRSwap = TRUE;
pr_debug("FUSB %s(%d): IsPRSwap=%d\n", __func__, __LINE__, IsPRSwap);
PolicyHasContract = FALSE;
platform_notify_pd_contract(FALSE);
PRSwapTimer = tPRSwapBailout; // Initialize the PRSwapTimer to indicate we are in the middle of a swap
PolicyStateTimer = tPSSourceOff; // Start the sink transition timer
}
}
break;
case 1: // Wait for the PS_RDY command to come in and indicate the source has turned off VBUS
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects == 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType) // Determine which command was received
{
case CMTPS_RDY: // If we get the PS_RDY message...
RoleSwapToAttachedSource(); // Initiate the Type-C state machine for a power role swap
PolicyIsSource = TRUE;
Registers.Switches.POWERROLE = PolicyIsSource;
DeviceWrite(regSwitches1, 1, &Registers.Switches.byte[1]);
PolicyStateTimer = tSourceOnDelay; // Allow the output voltage to rise before sending the PS_RDY message
PolicySubIndex++; // Increment the sub-index to move onto the next state
break;
default: // For all other commands received, simply ignore them
break;
}
}
}
else if (!PolicyStateTimer) // If the PSSourceOn times out...
{
PolicyState = peSinkSendHardReset; // Go to the error recovery state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
default: // Wait for VBUS to rise and then send the PS_RDY message
if (!PolicyStateTimer)
{
Status = PolicySendCommandNoReset(CMTPS_RDY, peSourceStartup, 0); // When we get the good CRC, we move onto the source startup state to complete the swap
if (Status == STAT_ERROR) PolicyState = peErrorRecovery; // If we get an error, go to the error recovery state
SwapSourceStartTimer = tSwapSourceStart;
}
break;
}
#else
PolicySendCommand(CMTReject, peSinkReady, 0); // Send the reject if we are not a DRP
#endif // FSC_HAVE_DRP
}
#endif // FSC_HAVE_SNK
#ifdef FSC_HAVE_VDM
void PolicyGiveVdm(void) {
if (ProtocolMsgRx && PolicyRxHeader.MessageType == DMTVenderDefined) // Have we received a VDM message
{
sendVdmMessageFailed(); // if we receive anything, kick out of here (interruptible)
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else if (sendingVdmData)
{
FSC_U8 result = PolicySendData(DMTVenderDefined, vdm_msg_length, vdm_msg_obj, vdm_next_ps, 0, SOP_TYPE_SOP);
if (result == STAT_SUCCESS)
{
if (expectingVdmResponse())
{
startVdmTimer(PolicyState);
}
else
{
resetPolicyState();
}
sendingVdmData = FALSE;
}
else if (result == STAT_ERROR)
{
sendVdmMessageFailed();
sendingVdmData = FALSE;
}
}
else
{
sendVdmMessageFailed();
}
if (VdmTimerStarted && (VdmTimer == 0))
{
vdmMessageTimeout();
}
}
void PolicyVdm (void) {
FSC_U8 result;
if (ProtocolMsgRx) // Have we received a message from the source?
{
ProtocolMsgRx = FALSE; // Reset the message received flag since we're handling it here
if (PolicyRxHeader.NumDataObjects != 0) // If we have received a command
{
switch (PolicyRxHeader.MessageType)
{
case DMTVenderDefined:
convertAndProcessVdmMessage(ProtocolMsgRxSop);
break;
default: // If we get something we are not expecting... simply ignore them
resetPolicyState(); // if not a VDM message, kick out of VDM state (interruptible)
ProtocolMsgRx = TRUE; // reset flag so other state can see the message and process
break;
}
}
else
{
resetPolicyState(); // if not a VDM message, kick out of VDM state (interruptible)
ProtocolMsgRx = TRUE; // reset flag so other state can see the message and process
}
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
}
else
{
if (sendingVdmData)
{
result = PolicySendData(DMTVenderDefined, vdm_msg_length, vdm_msg_obj, vdm_next_ps, 0, SOP_TYPE_SOP);
if (result == STAT_SUCCESS || result == STAT_ERROR)
{
sendingVdmData = FALSE;
}
}
}
if (VdmTimerStarted && (VdmTimer == 0))
{
if(PolicyState == peDfpUfpVdmIdentityRequest)
{
AutoVdmState = AUTO_VDM_DONE;
}
vdmMessageTimeout();
}
}
#endif // FSC_HAVE_VDM
void PolicyInvalidState (void) {
// reset if we get to an invalid state
if (PolicyIsSource)
{
PolicyState = peSourceSendHardReset;
}
else
{
PolicyState = peSinkSendHardReset;
}
}
// ########################## General PD Messaging ########################## //
FSC_BOOL PolicySendHardReset(PolicyState_t nextState, FSC_U32 delay)
{
FSC_BOOL Success = FALSE;
switch (PolicySubIndex)
{
case 0:
switch (PDTxStatus)
{
case txReset:
case txWait:
// Do nothing until the protocol layer finishes generating the hard reset signaling
// The next state should be either txCollision or txSuccess
break;
case txSuccess:
PolicyStateTimer = delay; // Set the amount of time prior to proceeding to the next state
PolicySubIndex++; // Move onto the next state
Success = TRUE;
break;
default: // None of the other states should actually occur, so...
PDTxStatus = txReset; // Set the transmitter status to resend a hard reset
break;
}
break;
default:
if (PolicyStateTimer == 0) // Once tPSHardReset has elapsed...
{
PolicyStateTimer = tPSHardReset - tHardResetOverhead;
HardResetCounter++; // Increment the hard reset counter once successfully sent
PolicyState = nextState; // Go to the state to transition to the default sink state
PolicySubIndex = 0; // Clear the sub index
PDTxStatus = txIdle; // Clear the transmitter status
}
break;
}
return Success;
}
FSC_U8 PolicySendCommand(FSC_U8 Command, PolicyState_t nextState, FSC_U8 subIndex)
{
FSC_U8 Status = STAT_BUSY;
switch (PDTxStatus)
{
case txIdle:
PolicyTxHeader.word = 0; // Clear the word to initialize for each transaction
PolicyTxHeader.NumDataObjects = 0; // Clear the number of objects since this is a command
PolicyTxHeader.MessageType = Command & 0x0F; // Sets the message type to the command passed in
PolicyTxHeader.PortDataRole = PolicyIsDFP; // Set whether the port is acting as a DFP or UFP
PolicyTxHeader.PortPowerRole = PolicyIsSource; // Set whether the port is serving as a power source or sink
PolicyTxHeader.SpecRevision = USBPDSPECREV; // Set the spec revision
PDTxStatus = txSend; // Indicate to the Protocol layer that there is something to send
break;
case txSend:
case txBusy:
case txWait:
// Waiting for GoodCRC or timeout of the protocol
// May want to put in a second level timeout in case there's an issue with the protocol getting hung
break;
case txSuccess:
PolicyState = nextState; // Go to the next state requested
PolicySubIndex = subIndex;
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_SUCCESS;
break;
case txError: // Didn't receive a GoodCRC message...
if (PolicyState == peSourceSendSoftReset) // If as a source we already failed at sending a soft reset...
PolicyState = peSourceSendHardReset; // Move onto sending a hard reset (source)
else if (PolicyState == peSinkSendSoftReset) // If as a sink we already failed at sending a soft reset...
PolicyState = peSinkSendHardReset; // Move onto sending a hard reset (sink)
else if (PolicyIsSource) // Otherwise, if we are a source...
PolicyState = peSourceSendSoftReset; // Attempt to send a soft reset (source)
else // We are a sink, so...
PolicyState = peSinkSendSoftReset; // Attempt to send a soft reset (sink)
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_ERROR;
break;
case txCollision:
CollisionCounter++; // Increment the collision counter
if (CollisionCounter > nRetryCount) // If we have already retried two times
{
if (PolicyIsSource)
PolicyState = peSourceSendHardReset; // Go to the source hard reset state
else
PolicyState = peSinkSendHardReset; // Go to the sink hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txReset; // Set the transmitter status to send a hard reset
Status = STAT_ERROR;
}
else // Otherwise we are going to try resending the soft reset
PDTxStatus = txIdle; // Clear the transmitter status for the next operation
break;
default: // For an undefined case, reset everything (shouldn't get here)
if (PolicyIsSource)
PolicyState = peSourceSendHardReset; // Go to the source hard reset state
else
PolicyState = peSinkSendHardReset; // Go to the sink hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txReset; // Set the transmitter status to send a hard reset
Status = STAT_ERROR;
break;
}
return Status;
}
FSC_U8 PolicySendCommandNoReset(FSC_U8 Command, PolicyState_t nextState, FSC_U8 subIndex)
{
FSC_U8 Status = STAT_BUSY;
switch (PDTxStatus)
{
case txIdle:
PolicyTxHeader.word = 0; // Clear the word to initialize for each transaction
PolicyTxHeader.NumDataObjects = 0; // Clear the number of objects since this is a command
PolicyTxHeader.MessageType = Command & 0x0F; // Sets the message type to the command passed in
PolicyTxHeader.PortDataRole = PolicyIsDFP; // Set whether the port is acting as a DFP or UFP
PolicyTxHeader.PortPowerRole = PolicyIsSource; // Set whether the port is serving as a power source or sink
PolicyTxHeader.SpecRevision = USBPDSPECREV; // Set the spec revision
PDTxStatus = txSend; // Indicate to the Protocol layer that there is something to send
break;
case txSend:
case txBusy:
case txWait:
// Waiting for GoodCRC or timeout of the protocol
// May want to put in a second level timeout in case there's an issue with the protocol getting hung
break;
case txSuccess:
PolicyState = nextState; // Go to the next state requested
PolicySubIndex = subIndex;
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_SUCCESS;
break;
default: // For all error cases (and undefined),
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txReset; // Set the transmitter status to send a hard reset
Status = STAT_ERROR;
break;
}
return Status;
}
FSC_U8 PolicySendData(FSC_U8 MessageType, FSC_U8 NumDataObjects, doDataObject_t* DataObjects, PolicyState_t nextState, FSC_U8 subIndex, SopType sop)
{
FSC_U8 Status = STAT_BUSY;
FSC_U32 i;
switch (PDTxStatus)
{
case txIdle:
if (NumDataObjects > 7)
NumDataObjects = 7;
PolicyTxHeader.word = 0x0000; // Clear the word to initialize for each transaction
PolicyTxHeader.NumDataObjects = NumDataObjects; // Set the number of data objects to send
PolicyTxHeader.MessageType = MessageType & 0x0F; // Sets the message type to the what was passed in
PolicyTxHeader.PortDataRole = PolicyIsDFP; // Set whether the port is acting as a DFP or UFP
PolicyTxHeader.PortPowerRole = PolicyIsSource; // Set whether the port is serving as a power source or sink
PolicyTxHeader.SpecRevision = USBPDSPECREV; // Set the spec revision
for (i=0; i<NumDataObjects; i++) // Loop through all of the data objects sent
PolicyTxDataObj[i].object = DataObjects[i].object; // Set each buffer object to send for the protocol layer
if (PolicyState == peSourceSendCaps) // If we are in the send source caps state...
CapsCounter++; // Increment the capabilities counter
PDTxStatus = txSend; // Indicate to the Protocol layer that there is something to send
break;
case txSend:
case txBusy:
case txWait:
case txCollision:
// Waiting for GoodCRC or timeout of the protocol
// May want to put in a second level timeout in case there's an issue with the protocol getting hung
break;
case txSuccess:
PolicyState = nextState; // Go to the next state requested
PolicySubIndex = subIndex;
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_SUCCESS;
break;
case txError: // Didn't receive a GoodCRC message...
if (PolicyState == peSourceSendCaps) // If we were in the send source caps state when the error occurred...
PolicyState = peSourceDiscovery; // Go to the discovery state
else if (PolicyIsSource) // Otherwise, if we are a source...
PolicyState = peSourceSendSoftReset; // Attempt to send a soft reset (source)
else // Otherwise...
PolicyState = peSinkSendSoftReset; // Go to the soft reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_ERROR;
break;
default: // For undefined cases, reset everything
if (PolicyIsSource)
PolicyState = peSourceSendHardReset; // Go to the source hard reset state
else
PolicyState = peSinkSendHardReset; // Go to the sink hard reset state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txReset; // Set the transmitter status to send a hard reset
Status = STAT_ERROR;
break;
}
return Status;
}
FSC_U8 PolicySendDataNoReset(FSC_U8 MessageType, FSC_U8 NumDataObjects, doDataObject_t* DataObjects, PolicyState_t nextState, FSC_U8 subIndex)
{
FSC_U8 Status = STAT_BUSY;
FSC_U32 i;
switch (PDTxStatus)
{
case txIdle:
if (NumDataObjects > 7)
NumDataObjects = 7;
PolicyTxHeader.word = 0x0000; // Clear the word to initialize for each transaction
PolicyTxHeader.NumDataObjects = NumDataObjects; // Set the number of data objects to send
PolicyTxHeader.MessageType = MessageType & 0x0F; // Sets the message type to the what was passed in
PolicyTxHeader.PortDataRole = PolicyIsDFP; // Set whether the port is acting as a DFP or UFP
PolicyTxHeader.PortPowerRole = PolicyIsSource; // Set whether the port is serving as a power source or sink
PolicyTxHeader.SpecRevision = USBPDSPECREV; // Set the spec revision
for (i=0; i<NumDataObjects; i++) // Loop through all of the data objects sent
PolicyTxDataObj[i].object = DataObjects[i].object; // Set each buffer object to send for the protocol layer
if (PolicyState == peSourceSendCaps) // If we are in the send source caps state...
CapsCounter++; // Increment the capabilities counter
PDTxStatus = txSend; // Indicate to the Protocol layer that there is something to send
break;
case txSend:
case txBusy:
case txWait:
// Waiting for GoodCRC or timeout of the protocol
// May want to put in a second level timeout in case there's an issue with the protocol getting hung
break;
case txSuccess:
PolicyState = nextState; // Go to the next state requested
PolicySubIndex = subIndex;
PDTxStatus = txIdle; // Reset the transmitter status
Status = STAT_SUCCESS;
break;
default: // For error cases (and undefined), ...
PolicyState = peErrorRecovery; // Go to the error recovery state
PolicySubIndex = 0; // Reset the sub index
PDTxStatus = txReset; // Set the transmitter status to send a hard reset
Status = STAT_ERROR;
break;
}
return Status;
}
void UpdateCapabilitiesRx(FSC_BOOL IsSourceCaps)
{
FSC_U32 i;
#ifdef FSC_DEBUG
SourceCapsUpdated = IsSourceCaps; // Set the flag to indicate that the received capabilities are valid
#endif // FSC_DEBUG
CapsHeaderReceived.word = PolicyRxHeader.word; // Store the header for the latest capabilities received
for (i=0; i<CapsHeaderReceived.NumDataObjects; i++) // Loop through all of the received data objects
CapsReceived[i].object = PolicyRxDataObj[i].object; // Store each capability
for (i=CapsHeaderReceived.NumDataObjects; i<7; i++) // Loop through all of the invalid objects
CapsReceived[i].object = 0; // Clear each invalid object
PartnerCaps.object = CapsReceived[0].object;
}
// ---------- BIST Receive Mode --------------------- //
void policyBISTReceiveMode(void) // Not Implemented
{
// Tell protocol layer to go to BIST Receive Mode
// Go to BIST_Frame_Received if a test frame is received
// Transition to SRC_Transition_to_Default, SNK_Transition_to_Default, or CBL_Ready when Hard_Reset received
}
void policyBISTFrameReceived(void) // Not Implemented
{
// Consume BIST Transmit Test Frame if received
// Transition back to BIST_Frame_Received when a BIST Test Frame has been received
// Transition to SRC_Transition_to_Default, SNK_Transition_to_Default, or CBL_Ready when Hard_Reset received
}
// ---------- BIST Carrier Mode and Eye Pattern ----- //
void policyBISTCarrierMode2(void)
{
switch (PolicySubIndex)
{
default:
case 0:
Registers.Control.BIST_MODE2 = 1; // Tell protocol layer to go to BIST_Carrier_Mode_2
DeviceWrite(regControl1, 1, &Registers.Control.byte[1]);
Registers.Control.TX_START = 1; // Set the bit to enable the transmitter
DeviceWrite(regControl0, 1, &Registers.Control.byte[0]); // Commit TX_START to the device
Registers.Control.TX_START = 0; // Clear this bit (write clear)
PolicyStateTimer = tBISTContMode; // Initialize and run BISTContModeTimer
PolicySubIndex = 1;
break;
case 1:
if(PolicyStateTimer == 0) // Transition to SRC_Transition_to_Default, SNK_Transition_to_Default, or CBL_Ready when BISTContModeTimer times out
{
Registers.Control.BIST_MODE2 = 0; // Disable BIST_Carrier_Mode_2 (PD_RESET does not do this)
DeviceWrite(regControl1, 1, &Registers.Control.byte[1]);
PolicyStateTimer = tGoodCRCDelay; // Delay for >200us to allow preamble to finish
PolicySubIndex++;
}
case 2:
if(PolicyStateTimer == 0) // Transition to SRC_Transition_to_Default, SNK_Transition_to_Default, or CBL_Ready when BISTContModeTimer times out
{
if (PolicyIsSource) // If we are the source...
{
#ifdef FSC_HAVE_SRC
PolicyState = peSourceSendHardReset; // This will hard reset then transition to default
PolicySubIndex = 0;
#endif // FSC_HAVE_SRC
}
else // Otherwise we are the sink...
{
#ifdef FSC_HAVE_SNK
PolicyState = peSinkSendHardReset; // This will hard reset then transition to default
PolicySubIndex = 0;
#endif // FSC_HAVE_SNK
}
}
break;
}
}
void policyBISTTestData(void)
{
if(Registers.DeviceID.VERSION_ID == VERSION_302B)
{
// Do Nothing
}
else
{
DeviceWrite(regControl1, 1, &Registers.Control.byte[1]);
}
}
#ifdef FSC_HAVE_VDM
void InitializeVdmManager(void)
{
initializeVdm();
// configure callbacks
vdmm.req_id_info = &vdmRequestIdentityInfo;
vdmm.req_svid_info = &vdmRequestSvidInfo;
vdmm.req_modes_info = &vdmRequestModesInfo;
vdmm.enter_mode_result = &vdmEnterModeResult;
vdmm.exit_mode_result = &vdmExitModeResult;
vdmm.inform_id = &vdmInformIdentity;
vdmm.inform_svids = &vdmInformSvids;
vdmm.inform_modes = &vdmInformModes;
vdmm.inform_attention = &vdmInformAttention;
vdmm.req_mode_entry = &vdmModeEntryRequest;
vdmm.req_mode_exit = &vdmModeExitRequest;
}
void convertAndProcessVdmMessage(SopType sop)
{
FSC_U32 i;
// form the word arrays that VDM block expects
// note: may need to rethink the interface. but this is quicker to develop right now
FSC_U32 vdm_arr[7];
for (i = 0; i < PolicyRxHeader.NumDataObjects; i++) {
vdm_arr[i] = 0;
vdm_arr[i] = PolicyRxDataObj[i].object;
}
processVdmMessage(sop, vdm_arr, PolicyRxHeader.NumDataObjects);
}
void sendVdmMessage(SopType sop, FSC_U32* arr, FSC_U32 length, PolicyState_t next_ps) {
FSC_U32 i;
// 'cast' to type that PolicySendData expects
// didn't think this was necessary, but it fixed some problems. - Gabe
vdm_msg_length = length;
vdm_next_ps = next_ps;
for (i = 0; i < vdm_msg_length; i++) {
vdm_msg_obj[i].object = arr[i];
}
sendingVdmData = TRUE;
ProtocolCheckRxBeforeTx = TRUE;
VdmTimerStarted = FALSE;
PolicyState = peGiveVdm;
}
void doVdmCommand(void)
{
FSC_U32 command;
FSC_U32 svid;
FSC_U32 mode_index;
SopType sop;
command = PDTransmitObjects[0].byte[0] & 0x1F;
svid = 0;
svid |= (PDTransmitObjects[0].byte[3] << 8);
svid |= (PDTransmitObjects[0].byte[2] << 0);
mode_index = 0;
mode_index = PDTransmitObjects[0].byte[1] & 0x7;
// only SOP today
sop = SOP_TYPE_SOP;
#ifdef FSC_HAVE_DP
if (svid == DP_SID) {
if (command == DP_COMMAND_STATUS) {
requestDpStatus();
} else if (command == DP_COMMAND_CONFIG) {
DisplayPortConfig_t temp;
temp.word = PDTransmitObjects[1].object;
requestDpConfig(temp);
}
}
#endif // FSC_HAVE_DP
if (command == DISCOVER_IDENTITY) {
requestDiscoverIdentity(sop);
} else if (command == DISCOVER_SVIDS) {
requestDiscoverSvids(sop);
} else if (command == DISCOVER_MODES) {
requestDiscoverModes(sop, svid);
} else if (command == ENTER_MODE) {
requestEnterMode(sop, svid, mode_index);
} else if (command == EXIT_MODE) {
requestExitMode(sop, svid, mode_index);
}
}
// this function assumes we're already in either Source or Sink Ready states!
void autoVdmDiscovery (void)
{
#ifdef FSC_DEBUG
// these messages can get pretty fast, don't want to obliterate the USB buffer
if (GetUSBPDBufferNumBytes() != 0) return;
#endif // FSC_DEBUG
if (!PolicyIsDFP) return; // only auto-discover for DFPs - but allow SM to start for DR swaps in the future
if (PDTxStatus == txIdle) { // wait for protocol layer to become idle
switch (AutoVdmState) {
case AUTO_VDM_INIT:
case AUTO_VDM_DISCOVER_ID_PP:
requestDiscoverIdentity(SOP_TYPE_SOP);
AutoVdmState = AUTO_VDM_DISCOVER_SVIDS_PP;
break;
case AUTO_VDM_DISCOVER_SVIDS_PP:
requestDiscoverSvids(SOP_TYPE_SOP);
AutoVdmState = AUTO_VDM_DISCOVER_MODES_PP;
break;
case AUTO_VDM_DISCOVER_MODES_PP:
if (auto_mode_disc_tracker == core_svid_info.num_svids) {
AutoVdmState = AUTO_VDM_ENTER_MODE_PP;
auto_mode_disc_tracker = 0;
} else {
requestDiscoverModes(SOP_TYPE_SOP, core_svid_info.svids[auto_mode_disc_tracker]);
auto_mode_disc_tracker++;
}
break;
case AUTO_VDM_ENTER_MODE_PP:
if (AutoDpModeEntryObjPos > 0) {
requestEnterMode(SOP_TYPE_SOP, DP_SID, AutoDpModeEntryObjPos);
AutoVdmState = AUTO_VDM_DP_GET_STATUS;
} else {
AutoVdmState = AUTO_VDM_DONE;
}
break;
case AUTO_VDM_DP_GET_STATUS:
if (DpModeEntered) {
requestDpStatus();
}
AutoVdmState = AUTO_VDM_DONE;
break;
default:
AutoVdmState = AUTO_VDM_DONE;
break;
}
}
}
#endif // FSC_HAVE_VDM
// This function is FUSB302 specific
SopType TokenToSopType(FSC_U8 data)
{
SopType ret;
// figure out what SOP* the data came in on
if ((data & 0b11100000) == 0b11100000) {
ret = SOP_TYPE_SOP;
} else if ((data & 0b11100000) == 0b11000000) {
ret = SOP_TYPE_SOP1;
} else if ((data & 0b11100000) == 0b10100000) {
ret = SOP_TYPE_SOP2;
} else if ((data & 0b11100000) == 0b10000000) {
ret = SOP_TYPE_SOP1_DEBUG;
} else if ((data & 0b11100000) == 0b01100000) {
ret = SOP_TYPE_SOP2_DEBUG;
} else {
ret = SOP_TYPE_ERROR;
}
return ret;
}
void resetLocalHardware(void)
{
FSC_U8 data = 0x20;
DeviceWrite(regReset, 1, &data); // Reset PD
DeviceRead(regSwitches1, 1, &Registers.Switches.byte[1]); // Re-read PD Registers
DeviceRead(regSlice, 1, &Registers.Slice.byte);
DeviceRead(regControl0, 1, &Registers.Control.byte[0]);
DeviceRead(regControl1, 1, &Registers.Control.byte[1]);
DeviceRead(regControl3, 1, &Registers.Control.byte[3]);
DeviceRead(regMask, 1, &Registers.Mask.byte);
DeviceRead(regMaska, 1, &Registers.MaskAdv.byte[0]);
DeviceRead(regMaskb, 1, &Registers.MaskAdv.byte[1]);
DeviceRead(regStatus0a, 2, &Registers.Status.byte[0]);
DeviceRead(regStatus0, 2, &Registers.Status.byte[4]);
}
void processDMTBIST(void)
{
FSC_U8 bdo = PolicyRxDataObj[0].byte[3]>>4;
Registers.Mask.byte = 0xFF; // Mask for VBUS and Hard Reset
Registers.Mask.M_VBUSOK = 0;
DeviceWrite(regMask, 1, &Registers.Mask.byte);
Registers.MaskAdv.byte[0] = 0xFF;
Registers.MaskAdv.M_HARDRST = 0;
DeviceWrite(regMaska, 1, &Registers.MaskAdv.byte[0]);
Registers.MaskAdv.M_GCRCSENT = 1;
DeviceWrite(regMaskb, 1, &Registers.MaskAdv.byte[1]);
switch (bdo)
{
case BDO_BIST_Carrier_Mode_2:
if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 100) // Only enter BIST for 5V contract
{
PolicyState = PE_BIST_Carrier_Mode_2;
PolicySubIndex = 0;
ProtocolState = PRLIdle;
}
break;
default:
case BDO_BIST_Test_Data:
if(CapsSource[USBPDContract.FVRDO.ObjectPosition-1].FPDOSupply.Voltage == 100) // Only enter BIST for 5V contract
{
if(Registers.DeviceID.VERSION_ID == VERSION_302B)
{
Registers.Control.BIST_TMODE = 1; // Auto-flush RxFIFO
DeviceWrite(regControl3, 1, &Registers.Control.byte[3]);
}
else
{
Registers.Control.RX_FLUSH = 1; // Enable RxFIFO flushing
}
PolicyState = PE_BIST_Test_Data;
ProtocolState = PRLDisabled; // Disable Protocol layer so we don't read FIFO
}
break;
}
}
#ifdef FSC_DEBUG
void SendUSBPDHardReset(void)
{
if (PolicyIsSource) // If we are the source...
PolicyState = peSourceSendHardReset; // set the source state to send a hard reset
else // Otherwise we are the sink...
PolicyState = peSinkSendHardReset; // so set the sink state to send a hard reset
PolicySubIndex = 0;
PDTxStatus = txIdle; // Reset the transmitter status
}
#ifdef FSC_HAVE_SRC
void WriteSourceCapabilities(FSC_U8* abytData)
{
FSC_U32 i, j;
sopMainHeader_t Header = {0};
Header.byte[0] = *abytData++; // Set the 1st PD header byte
Header.byte[1] = *abytData++; // Set the 2nd PD header byte
if ((Header.NumDataObjects > 0) && (Header.MessageType == DMTSourceCapabilities)) // Only do anything if we decoded a source capabilities message
{
CapsHeaderSource.word = Header.word; // Set the actual caps source header
for (i=0; i<CapsHeaderSource.NumDataObjects; i++) // Loop through all the data objects
{
for (j=0; j<4; j++) // Loop through each byte of the object
CapsSource[i].byte[j] = *abytData++; // Set the actual bytes
}
if (PolicyIsSource) // If we are currently acting as the source...
{
PDTransmitHeader.word = CapsHeaderSource.word; // Set the message type to capabilities to trigger sending the caps (only need the header to trigger)
USBPDTxFlag = TRUE; // Set the flag to send the new caps when appropriate...
SourceCapsUpdated = TRUE; // Set the flag to indicate to the software that the source caps were updated
}
}
}
#endif // FSC_HAVE_SRC
void ReadSourceCapabilities(FSC_U8* abytData)
{
FSC_U32 i, j;
*abytData++ = CapsHeaderSource.byte[0];
*abytData++ = CapsHeaderSource.byte[1];
for (i=0; i<CapsHeaderSource.NumDataObjects; i++)
{
for (j=0; j<4; j++)
*abytData++ = CapsSource[i].byte[j];
}
}
#ifdef FSC_HAVE_SNK
void WriteSinkCapabilities(FSC_U8* abytData)
{
FSC_U32 i, j;
sopMainHeader_t Header = {0};
Header.byte[0] = *abytData++; // Set the 1st PD header byte
Header.byte[1] = *abytData++; // Set the 2nd PD header byte
if ((Header.NumDataObjects > 0) && (Header.MessageType == DMTSinkCapabilities)) // Only do anything if we decoded a source capabilities message
{
CapsHeaderSink.word = Header.word; // Set the actual caps sink header
for (i=0; i<CapsHeaderSink.NumDataObjects; i++) // Loop through all the data objects
{
for (j=0; j<4; j++) // Loop through each byte of the object
CapsSink[i].byte[j] = *abytData++; // Set the actual bytes
}
// We could also trigger sending the caps or re-evaluating, but we don't do anything with this info here...
}
}
void WriteSinkRequestSettings(FSC_U8* abytData)
{
FSC_U32 uintPower;
SinkGotoMinCompatible = *abytData & 0x01 ? TRUE : FALSE;
SinkUSBSuspendOperation = *abytData & 0x02 ? TRUE : FALSE;
SinkUSBCommCapable = *abytData++ & 0x04 ? TRUE : FALSE;
SinkRequestMaxVoltage = (FSC_U32) *abytData++;
SinkRequestMaxVoltage |= ((FSC_U32) (*abytData++) << 8); // Voltage resolution is 50mV
uintPower = (FSC_U32) *abytData++;
uintPower |= ((FSC_U32) (*abytData++) << 8);
uintPower |= ((FSC_U32) (*abytData++) << 16);
uintPower |= ((FSC_U32) (*abytData++) << 24);
SinkRequestOpPower = uintPower; // Power resolution is 0.5mW
uintPower = (FSC_U32) *abytData++;
uintPower |= ((FSC_U32) (*abytData++) << 8);
uintPower |= ((FSC_U32) (*abytData++) << 16);
uintPower |= ((FSC_U32) (*abytData++) << 24);
SinkRequestMaxPower = uintPower; // Power resolution is 0.5mW
// We could try resetting and re-evaluating the source caps here, but lets not do anything until requested by the user (soft reset or detach)
}
void ReadSinkRequestSettings(FSC_U8* abytData)
{
*abytData = SinkGotoMinCompatible ? 0x01 : 0;
*abytData |= SinkUSBSuspendOperation ? 0x02 : 0;
*abytData++ |= SinkUSBCommCapable ? 0x04 : 0;
*abytData++ = (FSC_U8) (SinkRequestMaxVoltage & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestMaxVoltage & 0xFF) >> 8);
*abytData++ = (FSC_U8) (SinkRequestOpPower & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestOpPower >> 8) & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestOpPower >> 16) & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestOpPower >> 24) & 0xFF);
*abytData++ = (FSC_U8) (SinkRequestMaxPower & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestMaxPower >> 8) & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestMaxPower >> 16) & 0xFF);
*abytData++ = (FSC_U8) ((SinkRequestMaxPower >> 24) & 0xFF);
}
#endif // FSC_HAVE_SNK
void ReadSinkCapabilities(FSC_U8* abytData)
{
FSC_U32 i, j;
*abytData++ = CapsHeaderSink.byte[0];
*abytData++ = CapsHeaderSink.byte[1];
for (i=0; i<CapsHeaderSink.NumDataObjects; i++)
{
for (j=0; j<4; j++)
*abytData++ = CapsSink[i].byte[j];
}
}
void EnableUSBPD(void)
{
if (!USBPDEnabled) // If we are not already enabled...
{
USBPDEnabled = TRUE; // Set the USBPD state machine to enabled // return since we don't have to do anything
}
}
void DisableUSBPD(void)
{
if (USBPDEnabled) // If we are already disabled...
{
USBPDEnabled = FALSE; // Set the USBPD state machine to enabled
}
}
FSC_BOOL GetPDStateLog(FSC_U8 * data){ // Loads log into byte array
FSC_U32 i;
FSC_U32 entries = PDStateLog.Count;
FSC_U16 state_temp;
FSC_U16 time_tms_temp;
FSC_U16 time_s_temp;
for(i=0; ((i<entries) && (i<12)); i++)
{
ReadStateLog(&PDStateLog, &state_temp, &time_tms_temp, &time_s_temp);
data[i*5+1] = state_temp;
data[i*5+2] = (time_tms_temp>>8);
data[i*5+3] = (FSC_U8)time_tms_temp;
data[i*5+4] = (time_s_temp)>>8;
data[i*5+5] = (FSC_U8)time_s_temp;
}
data[0] = i; // Send number of log packets
return TRUE;
}
void ProcessReadPDStateLog(FSC_U8* MsgBuffer, FSC_U8* retBuffer)
{
if (MsgBuffer[1] != 0)
{
retBuffer[1] = 0x01; // Return that the version is not recognized
return;
}
GetPDStateLog(&retBuffer[3]); // Designed for 64 byte buffer
}
void ProcessPDBufferRead(FSC_U8* MsgBuffer, FSC_U8* retBuffer)
{
if (MsgBuffer[1] != 0)
retBuffer[1] = 0x01; // Return that the version is not recognized
else
{
retBuffer[4] = GetUSBPDBufferNumBytes(); // Return the total number of bytes in the buffer
retBuffer[5] = ReadUSBPDBuffer((FSC_U8*)&retBuffer[6], 58); // Return the number of bytes read and return the data
}
}
#endif // FSC_DEBUG
void SetVbusTransitionTime(FSC_U32 time_ms) {
VbusTransitionTime = time_ms * TICK_SCALE_TO_MS;
}
|