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
|
/*
* 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/>.
*
*/
#include <linux/kernel.h>
#include <linux/stat.h> // File permission masks
#include <linux/types.h> // Kernel datatypes
#include <linux/i2c.h> // I2C access, mutex
#include <linux/errno.h> // Linux kernel error definitions
#include <linux/hrtimer.h> // hrtimer
#include <linux/workqueue.h> // work_struct, delayed_work
#include <linux/delay.h> // udelay, usleep_range, msleep
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/pinctrl/consumer.h>
#include <linux/regulator/consumer.h>
#include <linux/power/htc_battery.h>
#include "fusb30x_global.h" // Chip structure access
#include "../core/core.h" // Core access
#include "../core/fusb30X.h"
#include "platform_helpers.h"
#ifdef FSC_DEBUG
#include "hostcomm.h"
#include "../core/PD_Types.h" // State Log states
#include "../core/TypeC_Types.h" // State Log states
#endif // FSC_DEBUG
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/******************************************** GPIO Interface ******************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
const char* FUSB_DT_INTERRUPT_INTN = "fsc_interrupt_int_n"; // Name of the INT_N interrupt in the Device Tree
#define FUSB_DT_GPIO_INTN "fairchild,int_n" // Name of the Int_N GPIO pin in the Device Tree
#define FUSB_DT_GPIO_VBUS_5V "fairchild,vbus5v" // Name of the VBus 5V GPIO pin in the Device Tree
#define FUSB_DT_GPIO_VBUS_OTHER "fairchild,vbusOther" // Name of the VBus Other GPIO pin in the Device Tree
#define FUSB_I2C_RETRY_DELAY 50 // in ms
#ifdef FSC_DEBUG
#define FUSB_DT_GPIO_DEBUG_SM_TOGGLE "fairchild,dbg_sm" // Name of the debug State Machine toggle GPIO pin in the Device Tree
#endif // FSC_DEBUG
#ifdef FSC_INTERRUPT_TRIGGERED
/* Internal forward declarations */
static irqreturn_t _fusb_isr_intn(int irq, void *dev_id);
#endif // FSC_INTERRUPT_TRIGGERED
extern FSC_BOOL VCONN_enabled;
extern DeviceReg_t Registers;
FSC_S32 fusb_InitializeGPIO(void)
{
FSC_S32 ret = 0;
struct device_node* node;
struct pinctrl_state *set_state;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return -ENOMEM;
}
/* Get our device tree node */
node = chip->client->dev.of_node;
chip->fusb302_pinctrl = devm_pinctrl_get(&chip->client->dev);
if (IS_ERR(chip->fusb302_pinctrl)) {
if (of_property_read_bool(node, "pinctrl-names")) {
dev_err(&chip->client->dev, "Error encountered while getting pinctrl");
ret = PTR_ERR(chip->fusb302_pinctrl);
}
dev_dbg(&chip->client->dev, "Target does not use pinctrl\n");
chip->fusb302_pinctrl = NULL;
}
if (chip->fusb302_pinctrl) {
set_state = pinctrl_lookup_state(chip->fusb302_pinctrl, "default");
if (IS_ERR(set_state)) {
pr_err("FUSB: cannot get fusb302 pinctrl default state");
return PTR_ERR(set_state);
}
pinctrl_select_state(chip->fusb302_pinctrl, set_state);
set_state = pinctrl_lookup_state(chip->fusb302_pinctrl, "vconn_disable");
if (IS_ERR(set_state)) {
pr_err("FUSB: cannot get fusb302 pinctrl vconn_disable state");
return PTR_ERR(set_state);
}
pinctrl_select_state(chip->fusb302_pinctrl, set_state);
}
/* Get our GPIO pins from the device tree, allocate them, and then set their direction (input/output) */
chip->gpio_IntN = of_get_named_gpio(node, FUSB_DT_GPIO_INTN, 0);
if (!gpio_is_valid(chip->gpio_IntN))
{
dev_err(&chip->client->dev, "%s - Error: Could not get named GPIO for Int_N! Error code: %d\n", __func__, chip->gpio_IntN);
return chip->gpio_IntN;
}
// Request our GPIO to reserve it in the system - this should help ensure we have exclusive access (not guaranteed)
ret = gpio_request(chip->gpio_IntN, FUSB_DT_GPIO_INTN);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not request GPIO for Int_N! Error code: %d\n", __func__, ret);
return ret;
}
ret = gpio_direction_input(chip->gpio_IntN);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not set GPIO direction to input for Int_N! Error code: %d\n", __func__, ret);
return ret;
}
#ifdef FSC_DEBUG
/* Export to sysfs */
gpio_export(chip->gpio_IntN, false);
gpio_export_link(&chip->client->dev, FUSB_DT_GPIO_INTN, chip->gpio_IntN);
#endif // FSC_DEBUG
pr_info("FUSB %s - INT_N GPIO initialized as pin '%d'\n", __func__, chip->gpio_IntN);
#ifdef VBUS_5V_SUPPORTED
// VBus 5V
chip->gpio_VBus5V = of_get_named_gpio(node, FUSB_DT_GPIO_VBUS_5V, 0);
if (!gpio_is_valid(chip->gpio_VBus5V))
{
dev_err(&chip->client->dev, "%s - Error: Could not get named GPIO for VBus5V! Error code: %d\n", __func__, chip->gpio_VBus5V);
fusb_GPIO_Cleanup();
return chip->gpio_VBus5V;
}
// Request our GPIO to reserve it in the system - this should help ensure we have exclusive access (not guaranteed)
ret = gpio_request(chip->gpio_VBus5V, FUSB_DT_GPIO_VBUS_5V);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not request GPIO for VBus5V! Error code: %d\n", __func__, ret);
return ret;
}
ret = gpio_direction_output(chip->gpio_VBus5V, chip->gpio_VBus5V_value);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not set GPIO direction to output for VBus5V! Error code: %d\n", __func__, ret);
fusb_GPIO_Cleanup();
return ret;
}
#ifdef FSC_DEBUG
// Export to sysfs
gpio_export(chip->gpio_VBus5V, false);
gpio_export_link(&chip->client->dev, FUSB_DT_GPIO_VBUS_5V, chip->gpio_VBus5V);
#endif // FSC_DEBUG
pr_info("FUSB %s - VBus 5V initialized as pin '%d' and is set to '%d'\n", __func__, chip->gpio_VBus5V, chip->gpio_VBus5V_value ? 1 : 0);
#endif
#ifdef VBUS_OTHER_SUPPORTED
// VBus other (eg. 12V)
// NOTE - This VBus is optional, so if it doesn't exist then fake it like it's on.
chip->gpio_VBusOther = of_get_named_gpio(node, FUSB_DT_GPIO_VBUS_OTHER, 0);
if (!gpio_is_valid(chip->gpio_VBusOther))
{
// Soft fail - provide a warning, but don't quit because we don't really need this VBus if only using VBus5v
pr_warning("%s - Warning: Could not get GPIO for VBusOther! Error code: %d\n", __func__, chip->gpio_VBusOther);
}
else
{
// Request our GPIO to reserve it in the system - this should help ensure we have exclusive access (not guaranteed)
ret = gpio_request(chip->gpio_VBusOther, FUSB_DT_GPIO_VBUS_OTHER);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not request GPIO for VBusOther! Error code: %d\n", __func__, ret);
return ret;
}
ret = gpio_direction_output(chip->gpio_VBusOther, chip->gpio_VBusOther_value);
if (ret != 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not set GPIO direction to output for VBusOther! Error code: %d\n", __func__, ret);
return ret;
}
else
{
pr_info("FUSB %s - VBusOther initialized as pin '%d' and is set to '%d'\n", __func__, chip->gpio_VBusOther, chip->gpio_VBusOther_value ? 1 : 0);
}
}
#endif
#ifdef FSC_DEBUG
// State Machine Debug Notification
// Optional GPIO - toggles each time the state machine is called
chip->dbg_gpio_StateMachine = of_get_named_gpio(node, FUSB_DT_GPIO_DEBUG_SM_TOGGLE, 0);
if (!gpio_is_valid(chip->dbg_gpio_StateMachine))
{
// Soft fail - provide a warning, but don't quit because we don't really need this VBus if only using VBus5v
pr_warning("%s - Warning: Could not get GPIO for Debug GPIO! Error code: %d\n", __func__, chip->dbg_gpio_StateMachine);
}
else
{
// Request our GPIO to reserve it in the system - this should help ensure we have exclusive access (not guaranteed)
ret = gpio_request(chip->dbg_gpio_StateMachine, FUSB_DT_GPIO_DEBUG_SM_TOGGLE);
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not request GPIO for Debug GPIO! Error code: %d\n", __func__, ret);
return ret;
}
ret = gpio_direction_output(chip->dbg_gpio_StateMachine, chip->dbg_gpio_StateMachine_value);
if (ret != 0)
{
dev_err(&chip->client->dev, "%s - Error: Could not set GPIO direction to output for Debug GPIO! Error code: %d\n", __func__, ret);
return ret;
}
else
{
pr_info("FUSB %s - Debug GPIO initialized as pin '%d' and is set to '%d'\n", __func__, chip->dbg_gpio_StateMachine, chip->dbg_gpio_StateMachine_value ? 1 : 0);
}
// Export to sysfs
gpio_export(chip->dbg_gpio_StateMachine, true); // Allow direction to change to provide max debug flexibility
gpio_export_link(&chip->client->dev, FUSB_DT_GPIO_DEBUG_SM_TOGGLE, chip->dbg_gpio_StateMachine);
}
#endif // FSC_DEBUG
return 0; // Success!
}
extern FSC_BOOL IsPRSwap; // Variable indicating that a PRSwap is occurring
extern ConnectionState ConnState; // Variable indicating the current connection state
extern PolicyState_t PolicyState;
void fusb_GPIO_Set_VBus5v(FSC_BOOL set)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
int ret = -1, retry = 0;
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
do {
pr_info("FUSB %s: set vbus ctrl: %d, typec_state(0x%x), pd_state(0x%x), retry: %d\n", __func__, set, ConnState, PolicyState, retry);
if (chip->uc && chip->uc->pd_vbus_ctrl)
ret = chip->uc->pd_vbus_ctrl(set ? 1 : 0, IsPRSwap);
if (ret < 0) {
msleep(1000);
retry++;
}
} while(ret < 0 && retry < 5);
#ifdef VBUS_5V_SUPPORTED
// GPIO must be valid by this point
if (gpio_cansleep(chip->gpio_VBus5V))
{
/*
* If your system routes GPIO calls through a queue of some kind, then
* it may need to be able to sleep. If so, this call must be used.
*/
gpio_set_value_cansleep(chip->gpio_VBus5V, set ? 1 : 0);
}
else
{
gpio_set_value(chip->gpio_VBus5V, set ? 1 : 0);
}
chip->gpio_VBus5V_value = set;
pr_debug("FUSB %s - VBus 5V set to: %d\n", __func__, chip->gpio_VBus5V_value ? 1 : 0);
#endif
}
void fusb_GPIO_Set_VBusOther(FSC_BOOL set)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
}
#ifdef VBUS_OTHER_SUPPORTED
// Only try to set if feature is enabled, otherwise just fake it
if (gpio_is_valid(chip->gpio_VBusOther))
{
/*
* If your system routes GPIO calls through a queue of some kind, then
* it may need to be able to sleep. If so, this call must be used.
*/
if (gpio_cansleep(chip->gpio_VBusOther))
{
gpio_set_value_cansleep(chip->gpio_VBusOther, set ? 1 : 0);
}
else
{
gpio_set_value(chip->gpio_VBusOther, set ? 1 : 0);
}
}
chip->gpio_VBusOther_value = set;
#endif
}
FSC_BOOL fusb_GPIO_Get_VBus5v(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
FSC_BOOL ret = false;
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return false;
}
if (chip->uc && chip->uc->vbus_boost_enabled)
ret = chip->uc->vbus_boost_enabled();
return ret;
#ifdef VBUS_5V_SUPPORTED
if (!gpio_is_valid(chip->gpio_VBus5V))
{
pr_debug("FUSB %s - Error: VBus 5V pin invalid! Pin value: %d\n", __func__, chip->gpio_VBus5V);
}
return chip->gpio_VBus5V_value;
#endif
}
FSC_BOOL fusb_GPIO_Get_VBusOther(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return false;
}
return false;
#ifdef VBUS_OTHER_SUPPORTED
return chip->gpio_VBusOther_value;
#endif
}
FSC_BOOL fusb_GPIO_Get_IntN(void)
{
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return false;
}
else
{
/*
* If your system routes GPIO calls through a queue of some kind, then
* it may need to be able to sleep. If so, this call must be used.
*/
if (gpio_cansleep(chip->gpio_IntN))
{
ret = !gpio_get_value_cansleep(chip->gpio_IntN);
}
else
{
ret = !gpio_get_value(chip->gpio_IntN); // Int_N is active low
}
return (ret != 0);
}
}
FSC_BOOL fusb_Power_Vconn(FSC_BOOL set)
{
struct fusb30x_chip *chip = fusb30x_GetChip();
struct pinctrl_state *set_state;
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
}
if (!chip->boost_5v) {
chip->boost_5v = devm_regulator_get(&chip->client->dev, "V_USB_boost");
if (IS_ERR(chip->boost_5v)) {
pr_err("FUSB %s: still unable to get boost_5v regulator\n", __func__);
return FALSE;
}
}
pr_info("FUSB %s: typec_state(0x%x), pd_state(0x%x), set=%d\n", __func__, ConnState, PolicyState, set);
if (set) {
if (regulator_enable(chip->boost_5v)) {
pr_err("FUSB %s: Unable to disable boost_5v regulator\n", __func__);
return FALSE;
}
pr_info("FUSB : Vconn enabled\n");
} else {
if (regulator_disable(chip->boost_5v)) {
pr_err("FUSB %s: Unable to enable boost_5v regulator\n", __func__);
return FALSE;
}
pr_info("FUSB : Vconn disabled\n");
}
if (chip->fusb302_pinctrl) {
set_state = pinctrl_lookup_state(chip->fusb302_pinctrl, set ? "vconn_enable" : "vconn_disable");
if (IS_ERR(set_state)) {
pr_err("FUSB : cannot get fusb302 pinctrl vconn_contrl state");
return FALSE;
}
pinctrl_select_state(chip->fusb302_pinctrl, set_state);
}
return TRUE;
}
#ifdef FSC_DEBUG
void dbg_fusb_GPIO_Set_SM_Toggle(FSC_BOOL set)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
}
if (gpio_is_valid(chip->dbg_gpio_StateMachine))
{
/*
* If your system routes GPIO calls through a queue of some kind, then
* it may need to be able to sleep. If so, this call must be used.
*/
if (gpio_cansleep(chip->dbg_gpio_StateMachine))
{
gpio_set_value_cansleep(chip->dbg_gpio_StateMachine, set ? 1 : 0);
}
else
{
gpio_set_value(chip->dbg_gpio_StateMachine, set ? 1 : 0);
}
chip->dbg_gpio_StateMachine_value = set;
}
}
FSC_BOOL dbg_fusb_GPIO_Get_SM_Toggle(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return false;
}
return chip->dbg_gpio_StateMachine_value;
}
#endif // FSC_DEBUG
void fusb_GPIO_Cleanup(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
#ifdef FSC_INTERRUPT_TRIGGERED
if (gpio_is_valid(chip->gpio_IntN) && chip->gpio_IntN_irq != -1) // -1 indicates that we don't have an IRQ to free
{
devm_free_irq(&chip->client->dev, chip->gpio_IntN_irq, chip);
}
#endif // FSC_INTERRUPT_TRIGGERED
if (gpio_is_valid(chip->gpio_IntN) >= 0)
{
#ifdef FSC_DEBUG
gpio_unexport(chip->gpio_IntN);
#endif // FSC_DEBUG
gpio_free(chip->gpio_IntN);
}
#ifdef VBUS_5V_SUPPORTED
if (gpio_is_valid(chip->gpio_VBus5V) >= 0)
{
#ifdef FSC_DEBUG
gpio_unexport(chip->gpio_VBus5V);
#endif // FSC_DEBUG
gpio_free(chip->gpio_VBus5V);
}
#endif
#ifdef VBUS_OTHER_SUPPORTED
if (gpio_is_valid(chip->gpio_VBusOther) >= 0)
{
gpio_free(chip->gpio_VBusOther);
}
#endif
#ifdef FSC_DEBUG
if (gpio_is_valid(chip->dbg_gpio_StateMachine) >= 0)
{
gpio_unexport(chip->dbg_gpio_StateMachine);
gpio_free(chip->dbg_gpio_StateMachine);
}
#endif // FSC_DEBUG
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/******************************************** I2C Interface ******************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
FSC_BOOL fusb_I2C_WriteData(FSC_U8 address, FSC_U8 length, FSC_U8* data)
{
FSC_S32 i = 0;
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL || chip->client == NULL || data == NULL) // Sanity check
{
pr_err("FUSB %s - Error: %s is NULL!\n", __func__, (chip == NULL ? "Internal chip structure"
: (chip->client == NULL ? "I2C Client"
: "Write data buffer")));
return false;
}
mutex_lock(&chip->lock);
// Retry on failure up to the retry limit
for (i = 0; i <= chip->numRetriesI2C; i++)
{
if (atomic_read(&chip->pm_suspended)) {
pr_debug("FUSB %s: pm_suspended, retry\n", __func__);
msleep(FUSB_I2C_RETRY_DELAY);
continue;
}
ret = i2c_smbus_write_i2c_block_data(chip->client, // Perform the actual I2C write on our client
address, // Register address to write to
length, // Number of bytes to write
data); // Ptr to unsigned char data
if (ret < 0) // Errors report as negative
{
if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINVAL) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EINVAL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EAGAIN) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EAGAIN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EALREADY) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EALREADY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EBADE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADMSG) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EBADMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBUSY) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EBUSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECANCELED) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ECANCELED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECOMM) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ECOMM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNABORTED) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ECONNABORTED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNREFUSED) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ECONNREFUSED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNRESET) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ECONNRESET. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLK) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EDEADLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLOCK) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EDEADLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDESTADDRREQ) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EDESTADDRREQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EFAULT) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EFAULT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTDOWN) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EHOSTDOWN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTUNREACH) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EHOSTUNREACH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EILSEQ) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EILSEQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINPROGRESS) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EINPROGRESS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINTR) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EINTR. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EIO) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBACC) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ELIBACC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBBAD) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ELIBBAD. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBMAX) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ELIBMAX. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELOOP) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ELOOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMSGSIZE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EMSGSIZE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMULTIHOP) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EMULTIHOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOBUFS) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOBUFS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODATA) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENODATA. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODEV) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENODEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOLCK) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOLCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMEM) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOMEM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMSG) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOPROTOOPT) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOPROTOOPT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSPC) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOSPC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSYS) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOSYS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTBLK) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOTBLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTTY) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOTTY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTUNIQ) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENOTUNIQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENXIO) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ENXIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOVERFLOW) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EOVERFLOW. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPERM) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EPERM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPFNOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EPFNOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPIPE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EPIPE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTO) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EPROTO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTONOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EPROTONOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMCHG) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EREMCHG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTE) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EREMOTE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTEIO) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EREMOTEIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERESTART) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ERESTART. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ESRCH) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ESRCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIME) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ETIME. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIMEDOUT) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ETIMEDOUT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETXTBSY) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -ETXTBSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUCLEAN) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EUCLEAN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUNATCH) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EUNATCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUSERS) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EUSERS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EWOULDBLOCK) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EWOULDBLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXDEV) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EXDEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXFULL) { dev_err(&chip->client->dev, "%s - I2C Error block writing byte data. Address: '0x%02x', Return: -EXFULL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOPNOTSUPP) { dev_err(&chip->client->dev, "%s - I2C Error writing byte data. Address: '0x%02x', Return: -EOPNOTSUPP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROBE_DEFER) { dev_err(&chip->client->dev, "%s - I2C Error writing byte data. Address: '0x%02x', Return: -EPROBE_DEFER. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOENT) { dev_err(&chip->client->dev, "%s - I2C Error writing byte data. Address: '0x%02x', Return: -ENOENT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else { dev_err(&chip->client->dev, "%s - Unexpected I2C error block writing byte data. Address: '0x%02x', Return: '%d'. Attempt #%d / %d...\n", __func__, address, ret, i, chip->numRetriesI2C); }
}
else // Successful i2c writes should always return 0
{
break;
}
}
mutex_unlock(&chip->lock);
return (ret >= 0);
}
FSC_BOOL fusb_I2C_ReadData(FSC_U8 address, FSC_U8* data)
{
FSC_S32 i = 0;
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL || chip->client == NULL || data == NULL)
{
pr_err("FUSB %s - Error: %s is NULL!\n", __func__, (chip == NULL ? "Internal chip structure"
: (chip->client == NULL ? "I2C Client"
: "read data buffer")));
return false;
}
mutex_lock(&chip->lock);
// Retry on failure up to the retry limit
for (i = 0; i <= chip->numRetriesI2C; i++)
{
if (atomic_read(&chip->pm_suspended)) {
pr_debug("FUSB %s: pm_suspended, retry\n", __func__);
msleep(FUSB_I2C_RETRY_DELAY);
continue;
}
ret = i2c_smbus_read_byte_data(chip->client, (u8)address); // Read a byte of data from address
if (ret < 0) // Errors report as negative
{
if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINVAL) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EINVAL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EAGAIN) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EAGAIN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EALREADY) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EALREADY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EBADE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADMSG) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EBADMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBUSY) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EBUSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECANCELED) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ECANCELED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECOMM) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ECOMM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNABORTED) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ECONNABORTED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNREFUSED) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ECONNREFUSED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNRESET) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ECONNRESET. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLK) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EDEADLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLOCK) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EDEADLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDESTADDRREQ) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EDESTADDRREQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EFAULT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EFAULT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTDOWN) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EHOSTDOWN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTUNREACH) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EHOSTUNREACH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EILSEQ) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EILSEQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINPROGRESS) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EINPROGRESS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINTR) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EINTR. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EIO) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBACC) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ELIBACC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBBAD) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ELIBBAD. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBMAX) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ELIBMAX. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELOOP) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ELOOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMSGSIZE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EMSGSIZE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMULTIHOP) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EMULTIHOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOBUFS) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOBUFS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODATA) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENODATA. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODEV) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENODEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOLCK) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOLCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMEM) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOMEM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMSG) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOPROTOOPT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOPROTOOPT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSPC) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOSPC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSYS) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOSYS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTBLK) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOTBLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTTY) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOTTY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTUNIQ) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOTUNIQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENXIO) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENXIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOVERFLOW) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EOVERFLOW. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPERM) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPERM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPFNOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPFNOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPIPE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPIPE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTO) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPROTO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTONOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPROTONOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMCHG) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EREMCHG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTE) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EREMOTE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTEIO) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EREMOTEIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERESTART) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ERESTART. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ESRCH) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ESRCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIME) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ETIME. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIMEDOUT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ETIMEDOUT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETXTBSY) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ETXTBSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUCLEAN) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EUCLEAN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUNATCH) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EUNATCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUSERS) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EUSERS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EWOULDBLOCK) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EWOULDBLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXDEV) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EXDEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXFULL) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EXFULL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOPNOTSUPP) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EOPNOTSUPP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROBE_DEFER) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPROBE_DEFER. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOENT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOENT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else { dev_err(&chip->client->dev, "%s - Unexpected I2C error reading byte data. Address: '0x%02x', Return: '%d'. Attempt #%d / %d...\n", __func__, address, ret, i, chip->numRetriesI2C); }
}
else // Successful i2c writes should always return 0
{
*data = (FSC_U8)ret;
break;
}
}
mutex_unlock(&chip->lock);
return (ret >= 0);
}
FSC_BOOL fusb_I2C_ReadBlockData(FSC_U8 address, FSC_U8 length, FSC_U8* data)
{
FSC_S32 i = 0;
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL || chip->client == NULL || data == NULL)
{
pr_err("FUSB %s - Error: %s is NULL!\n", __func__, (chip == NULL ? "Internal chip structure"
: (chip->client == NULL ? "I2C Client"
: "block read data buffer")));
return false;
}
mutex_lock(&chip->lock);
// Retry on failure up to the retry limit
for (i = 0; i <= chip->numRetriesI2C; i++)
{
if (atomic_read(&chip->pm_suspended)) {
pr_debug("FUSB %s: pm_suspended, retry\n", __func__);
msleep(FUSB_I2C_RETRY_DELAY);
continue;
}
ret = i2c_smbus_read_i2c_block_data(chip->client, (u8)address, (u8)length, (u8*)data); // Read a byte of data from address
if (ret < 0) // Errors report as negative
{
if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINVAL) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EINVAL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EAGAIN) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EAGAIN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EALREADY) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EALREADY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EBADE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBADMSG) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EBADMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EBUSY) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EBUSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECANCELED) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ECANCELED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECOMM) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ECOMM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNABORTED) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ECONNABORTED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNREFUSED) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ECONNREFUSED. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ECONNRESET) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ECONNRESET. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLK) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EDEADLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDEADLOCK) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EDEADLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EDESTADDRREQ) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EDESTADDRREQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EFAULT) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EFAULT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTDOWN) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EHOSTDOWN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EHOSTUNREACH) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EHOSTUNREACH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EILSEQ) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EILSEQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINPROGRESS) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EINPROGRESS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EINTR) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EINTR. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EIO) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBACC) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ELIBACC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBBAD) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ELIBBAD. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELIBMAX) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ELIBMAX. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ELOOP) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ELOOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMSGSIZE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EMSGSIZE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EMULTIHOP) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EMULTIHOP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOBUFS) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOBUFS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODATA) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENODATA. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENODEV) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENODEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOLCK) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOLCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMEM) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOMEM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOMSG) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOMSG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOPROTOOPT) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOPROTOOPT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSPC) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOSPC. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOSYS) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOSYS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTBLK) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOTBLK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTTY) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOTTY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOTUNIQ) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENOTUNIQ. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENXIO) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ENXIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOVERFLOW) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EOVERFLOW. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPERM) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EPERM. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPFNOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EPFNOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPIPE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EPIPE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTO) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EPROTO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROTONOSUPPORT) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EPROTONOSUPPORT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERANGE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ERANGE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMCHG) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EREMCHG. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTE) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EREMOTE. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EREMOTEIO) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EREMOTEIO. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ERESTART) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ERESTART. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ESRCH) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ESRCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIME) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ETIME. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETIMEDOUT) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ETIMEDOUT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ETXTBSY) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -ETXTBSY. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUCLEAN) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EUCLEAN. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUNATCH) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EUNATCH. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EUSERS) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EUSERS. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EWOULDBLOCK) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EWOULDBLOCK. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXDEV) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EXDEV. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EXFULL) { dev_err(&chip->client->dev, "%s - I2C Error block reading byte data. Address: '0x%02x', Return: -EXFULL. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EOPNOTSUPP) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EOPNOTSUPP. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -EPROBE_DEFER) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -EPROBE_DEFER. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else if (ret == -ENOENT) { dev_err(&chip->client->dev, "%s - I2C Error reading byte data. Address: '0x%02x', Return: -ENOENT. Attempt #%d / %d...\n", __func__, address, i, chip->numRetriesI2C); }
else { dev_err(&chip->client->dev, "%s - Unexpected I2C error block reading byte data. Address: '0x%02x', Return: '%d'. Attempt #%d / %d...\n", __func__, address, ret, i, chip->numRetriesI2C); }
}
else if (ret != length) // We didn't read everything we wanted
{
dev_err(&chip->client->dev, "%s - Error: Block read request of %u bytes truncated to %u bytes.\n", __func__, length, I2C_SMBUS_BLOCK_MAX);
}
else
{
break; // Success, don't retry
}
}
mutex_unlock(&chip->lock);
return (ret == length);
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/******************************************** Timer Interface ******************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
static const unsigned long g_fusb_timer_tick_period_ns = 1000000; // Tick SM every 1ms -> 1000000ns
/*******************************************************************************
* Function: _fusb_TimerHandler
* Input: timer: hrtimer struct to be handled
* Return: HRTIMER_RESTART to restart the timer, or HRTIMER_NORESTART otherwise
* Description: Ticks state machine timer counters and rearms itself
********************************************************************************/
enum hrtimer_restart _fusb_TimerHandler(struct hrtimer* timer)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return HRTIMER_NORESTART;
}
if (!timer)
{
pr_err("FUSB %s - Error: High-resolution timer is NULL!\n", __func__);
return HRTIMER_NORESTART;
}
core_tick();
#ifdef FSC_DEBUG
if (chip->dbgTimerTicks++ >= U8_MAX)
{
chip->dbgTimerRollovers++;
}
#endif // FSC_DEBUG
// Reset the timer expiration
hrtimer_forward(timer, ktime_get(), ktime_set(0, g_fusb_timer_tick_period_ns));
return HRTIMER_RESTART; // Requeue the timer
}
void fusb_InitializeTimer(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
hrtimer_init(&chip->timer_state_machine, CLOCK_MONOTONIC, HRTIMER_MODE_REL); // Init the timer structure
chip->timer_state_machine.function = _fusb_TimerHandler; // Assign the callback to call when time runs out
pr_debug("FUSB %s - Timer initialized!\n", __func__);
}
void fusb_StartTimers(void)
{
ktime_t ktime;
struct fusb30x_chip* chip;
int ret;
chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
#ifdef FSC_DEBUG
/* Reset our debug timer counters */
chip->dbgTimerTicks = 0;
chip->dbgTimerRollovers = 0;
#endif // FSC_DEBUG
ktime = ktime_set(0, g_fusb_timer_tick_period_ns); // Convert our timer period (in ns) to ktime
pr_debug("FUSB %s - Timer starting!\n", __func__);
mutex_lock(&chip->lock);
if (hrtimer_active(&chip->timer_state_machine) != 0)
{
ret = hrtimer_cancel(&chip->timer_state_machine);
pr_info("FUSB %s - Active state machine hrtimer canceled: %d\n", __func__, ret);
}
if (hrtimer_is_queued(&chip->timer_state_machine) != 0)
{
ret = hrtimer_cancel(&chip->timer_state_machine);
pr_info("FUSB %s - Queued state machine hrtimer canceled: %d\n", __func__, ret);
}
hrtimer_start(&chip->timer_state_machine, ktime, HRTIMER_MODE_REL); // Start the timer
mutex_unlock(&chip->lock);
}
void fusb_StopTimers(void)
{
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
mutex_lock(&chip->lock);
if (hrtimer_active(&chip->timer_state_machine) != 0)
{
ret = hrtimer_cancel(&chip->timer_state_machine);
pr_debug("%s - Active state machine hrtimer canceled: %d\n", __func__, ret);
}
if (hrtimer_is_queued(&chip->timer_state_machine) != 0)
{
ret = hrtimer_cancel(&chip->timer_state_machine);
pr_debug("%s - Queued state machine hrtimer canceled: %d\n", __func__, ret);
}
mutex_unlock(&chip->lock);
pr_debug("FUSB %s - Timer stopped!\n", __func__);
}
// Get the max value that we can delay in 10us increments at compile time
static const FSC_U32 MAX_DELAY_10US = (UINT_MAX / 10);
void fusb_Delay10us(FSC_U32 delay10us)
{
FSC_U32 us = 0;
if (delay10us > MAX_DELAY_10US)
{
pr_err("FUSB %s - Error: Delay of '%u' is too long! Must be less than '%u'.\n", __func__, delay10us, MAX_DELAY_10US);
return;
}
us = delay10us * 10; // Convert to microseconds (us)
if (us <= 10) // Best practice is to use udelay() for < ~10us times
{
udelay(us); // BLOCKING delay for < 10us
}
else if (us < 20000) // Best practice is to use usleep_range() for 10us-20ms
{
// TODO - optimize this range, probably per-platform
usleep_range(us, us + (us / 10)); // Non-blocking sleep for at least the requested time, and up to the requested time + 10%
}
else // Best practice is to use msleep() for > 20ms
{
msleep(us / 1000); // Convert to ms. Non-blocking, low-precision sleep
}
}
FSC_S32 fusb_battery_select_source_capability(u8 obj_cnt, doDataObject_t pd_data[7], int *device_max_ma)
{
u8 i;
FSC_S32 sel_voltage_pdo_index;
struct htc_pd_data htc_pdo_data;
for (i = 0; i <= obj_cnt; i++) {
if (i > 5) {
obj_cnt = 6;
break;
}
if (pd_data[i].PDO.SupplyType == pdoTypeFixed) {
htc_pdo_data.pd_list[i][0] = pd_data[i].FPDOSupply.Voltage * 50; // voltage (mV)
htc_pdo_data.pd_list[i][1] = pd_data[i].FPDOSupply.MaxCurrent * 10; // current (mA)
} else if (pd_data[i].PDO.SupplyType == pdoTypeVariable) {
htc_pdo_data.pd_list[i][0] = pd_data[i].VPDO.MinVoltage * 50; // voltage (mV)
htc_pdo_data.pd_list[i][1] = pd_data[i].VPDO.MaxCurrent * 10; // current (mA)
}
}
sel_voltage_pdo_index = htc_battery_pd_charger_support(obj_cnt, htc_pdo_data, device_max_ma);
return sel_voltage_pdo_index;
}
#ifdef FSC_DEBUG
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/******************************************** SysFS Interface ******************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*******************************************************************************
* Function: fusb_timestamp_bytes_to_time
* Input: outSec: Seconds part of output is stored here
* outMS10ths: 10ths of MS part of output is stored here
* inBuf: Ptr to first of 4 timestamp bytes, where the timestamp is in this format:
* [HI-10thsMS LO-10thsMS HI-Sec LO-Sec]
* Return: None
* Description: Parses the 4 bytes in inBuf into a 2-part timestamp: Seconds and 10ths of MS
********************************************************************************/
void fusb_timestamp_bytes_to_time(FSC_U32* outSec, FSC_U32* outMS10ths, FSC_U8* inBuf)
{
if (outSec && outMS10ths && inBuf)
{
*outMS10ths = inBuf[0];
*outMS10ths = *outMS10ths << 8;
*outMS10ths |= inBuf[1];
*outSec = inBuf[2];
*outSec = *outSec << 8;
*outSec |= inBuf[3];
}
}
/*******************************************************************************
* Function: fusb_get_pd_message_type
* Input: header: PD message header. Bits 4..0 are the pd message type, bits 14..12 are num data objs
* out: Buffer to which the message type will be written, should be at least 32 bytes long
* Return: int - Number of chars written to out, negative on error
* Description: Parses both PD message header bytes for the message type as a null-terminated string.
********************************************************************************/
FSC_S32 fusb_get_pd_message_type(FSC_U16 header, FSC_U8* out)
{
FSC_S32 numChars = -1; // Number of chars written, return value
if ((!out) || !(out + 31)) // Check for our 32 byte buffer
{
pr_err("%s FUSB - Error: Invalid input buffer! header: 0x%x\n", __func__, header);
return -1;
}
// Bits 14..12 give num of data obj. This is a data message if there are data objects, otherwise it's a control message
// See the PD spec, Table 6-1 "Message Header", for more details.
if ((header & 0x7000) > 0)
{
switch (header & 0x0F)
{
case DMTSourceCapabilities: // Source Capabilities
{
numChars = sprintf(out, "Source Capabilities");
break;
}
case DMTRequest: // Request
{
numChars = sprintf(out, "Request");
break;
}
case DMTBIST: // BIST
{
numChars = sprintf(out, "BIST");
break;
}
case DMTSinkCapabilities: // Sink Capabilities
{
numChars = sprintf(out, "Sink Capabilities");
break;
}
case 0b00101: // Battery Status
{
numChars = sprintf(out, "Battery Status");
break;
}
case 0b00110: // Source Alert
{
numChars = sprintf(out, "Source Alert");
break;
}
case DMTVenderDefined: // Vendor Defined
{
numChars = sprintf(out, "Vendor Defined");
break;
}
default: // Reserved/unused/unknown
{
numChars = sprintf(out, "Reserved (Data) (0x%x)", header);
break;
}
}
}
else
{
switch (header & 0x0F)
{
case CMTGoodCRC: // Good CRC
{
numChars = sprintf(out, "Good CRC");
break;
}
case CMTGotoMin: // Go to min
{
numChars = sprintf(out, "Go to Min");
break;
}
case CMTAccept: // Accept
{
numChars = sprintf(out, "Accept");
break;
}
case CMTReject: // Reject
{
numChars = sprintf(out, "Reject");
break;
}
case CMTPing: // Ping
{
numChars = sprintf(out, "Ping");
break;
}
case CMTPS_RDY: // PS_RDY
{
numChars = sprintf(out, "PS_RDY");
break;
}
case CMTGetSourceCap: // Get Source Cap
{
numChars = sprintf(out, "Get Source Capabilities");
break;
}
case CMTGetSinkCap: // Get Sink Cap
{
numChars = sprintf(out, "Get Sink Capabilities");
break;
}
case CMTDR_Swap: // Data Role Swap
{
numChars = sprintf(out, "Data Role Swap");
break;
}
case CMTPR_Swap: // Power Role Swap
{
numChars = sprintf(out, "Power Role Swap");
break;
}
case CMTVCONN_Swap: // VConn Swap
{
numChars = sprintf(out, "VConn Swap");
break;
}
case CMTWait: // Wait
{
numChars = sprintf(out, "Wait");
break;
}
case CMTSoftReset: // Soft Reset
{
numChars = sprintf(out, "Soft Reset");
break;
}
case 0b01110: // Not Supported
{
numChars = sprintf(out, "Not Supported");
break;
}
case 0b01111: // Get Source Cap Extended
{
numChars = sprintf(out, "Get Source Cap Ext");
break;
}
case 0b10000: // Get Source Status
{
numChars = sprintf(out, "Get Source Status");
break;
}
case 0b10001: // FR Swap
{
numChars = sprintf(out, "FR Swap");
break;
}
default: // Reserved/unused/unknown
{
numChars = sprintf(out, "Reserved (CMD) (0x%x)", header);
break;
}
}
}
return numChars;
}
/*******************************************************************************
* Function: fusb_Sysfs_Handle_Read
* Input: output: Buffer to which the output will be written
* Return: Number of chars written to output
* Description: Reading this file will output the most recently saved hostcomm output buffer
********************************************************************************/
#define FUSB_MAX_BUF_SIZE 256 // Arbitrary temp buffer for parsing out driver data to sysfs
static ssize_t _fusb_Sysfs_Hostcomm_show(struct device* dev, struct device_attribute* attr, char* buf)
{
FSC_S32 i = 0;
FSC_S32 numLogs = 0;
FSC_S32 numChars = 0;
FSC_U32 TimeStampSeconds = 0; // Timestamp value in seconds
FSC_U32 TimeStampMS10ths = 0; // Timestamp fraction in 10ths of milliseconds
FSC_S8 tempBuf[FUSB_MAX_BUF_SIZE] = { 0 };
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL)
{
pr_err("%s - Chip structure is null!\n", __func__);
}
else if (buf == NULL || chip->HostCommBuf == NULL)
{
pr_err("%s - Buffer is null!\n", __func__);
}
else if (chip->HostCommBuf[0] == CMD_READ_PD_STATE_LOG) // Parse out the PD state log
{
numLogs = chip->HostCommBuf[3];
/* First byte echos the command, 4th byte is number of logs (2nd and 3rd bytes reserved as 0) */
numChars += sprintf(tempBuf, "PD State Log has %u entries:\n", numLogs); // Copy string + null terminator
strcat(buf, tempBuf);
/* Relevant data starts at 5th byte in this format: CMD 0 0 #Logs PDState time time time time */
for (i = 4; (i + 4 < FSC_HOSTCOMM_BUFFER_SIZE) && (numChars < PAGE_SIZE) && (numLogs > 0); i += 5, numLogs--) // Must be able to peek 4 bytes ahead, and don't overflow the output buffer (PAGE_SIZE)
{
fusb_timestamp_bytes_to_time(&TimeStampSeconds, &TimeStampMS10ths, &chip->HostCommBuf[i + 1]);
// sprintf should be safe here because we're controlling the strings being printed, just make sure the strings are less than FUSB_MAX_BUF_SIZE+1
switch (chip->HostCommBuf[i])
{
case peDisabled: // Policy engine is disabled
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDisabled\t\tPolicy engine is disabled\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peErrorRecovery: // Error recovery state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeErrorRecovery\t\tError recovery state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceHardReset: // Received a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceHardReset\t\tReceived a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendHardReset: // Source send a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendHardReset\t\tSource send a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSoftReset: // Received a soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSoftReset\t\tReceived a soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendSoftReset: // Send a soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendSoftReset\t\tSend a soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceStartup: // Initial state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceStartup\t\tInitial state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendCaps: // Send the source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendCaps\t\tSend the source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceDiscovery: // Waiting to detect a USB PD sink
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceDiscovery\t\tWaiting to detect a USB PD sink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceDisabled: // Disabled state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceDisabled\t\tDisabled state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceTransitionDefault: // Transition to default 5V state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceTransitionDefault\t\tTransition to default 5V state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceNegotiateCap: // Negotiate capability and PD contract
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceNegotiateCap\t\tNegotiate capability and PD contract\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceCapabilityResponse: // Respond to a request message with a reject/wait
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceCapabilityResponse\t\tRespond to a request message with a reject/wait\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceTransitionSupply: // Transition the power supply to the new setting (accept request)
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceTransitionSupply\t\tTransition the power supply to the new setting (accept request)\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceReady: // Contract is in place and output voltage is stable
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceReady\t\tContract is in place and output voltage is stable\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGiveSourceCaps: // State to resend source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGiveSourceCaps\t\tState to resend source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGetSinkCaps: // State to request the sink capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGetSinkCaps\t\tState to request the sink capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendPing: // State to send a ping message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendPing\t\tState to send a ping message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGotoMin: // State to send the gotoMin and ready the power supply
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGotoMin\t\tState to send the gotoMin and ready the power supply\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGiveSinkCaps: // State to send the sink capabilities if dual-role
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGiveSinkCaps\t\tState to send the sink capabilities if dual-role\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGetSourceCaps: // State to request the source caps from the UFP
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGetSourceCaps\t\tState to request the source caps from the UFP\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendDRSwap: // State to send a DR_Swap message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendDRSwap\t\tState to send a DR_Swap message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceEvaluateDRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceEvaluateDRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkHardReset: // Received a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkHardReset\t\tReceived a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendHardReset: // Sink send hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendHardReset\t\tSink send hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSoftReset: // Sink soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSoftReset\t\tSink soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendSoftReset: // Sink send soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendSoftReset\t\tSink send soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkTransitionDefault: // Transition to the default state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkTransitionDefault\t\tTransition to the default state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkStartup: // Initial sink state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkStartup\t\tInitial sink state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkDiscovery: // Sink discovery state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkDiscovery\t\tSink discovery state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkWaitCaps: // Sink wait for capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkWaitCaps\t\tSink wait for capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateCaps: // Sink state to evaluate the received source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateCaps\t\tSink state to evaluate the received source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSelectCapability: // Sink state for selecting a capability
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSelectCapability\t\tSink state for selecting a capability\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkTransitionSink: // Sink state for transitioning the current power
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkTransitionSink\t\tSink state for transitioning the current power\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkReady: // Sink ready state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkReady\t\tSink ready state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGiveSinkCap: // Sink send capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGiveSinkCap\t\tSink send capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGetSourceCap: // Sink get source capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGetSourceCap\t\tSink get source capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGetSinkCap: // Sink state to get the sink capabilities of the connected source
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGetSinkCap\t\tSink state to get the sink capabilities of the connected source\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGiveSourceCap: // Sink state to send the source capabilities if dual-role
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGiveSourceCap\t\tSink state to send the source capabilities if dual-role\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendDRSwap: // State to send a DR_Swap message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendDRSwap\t\tState to send a DR_Swap message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateDRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateDRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendVCONNSwap: // Initiate a VCONN swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendVCONNSwap\t\tInitiate a VCONN swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateVCONNSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateVCONNSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendPRSwap: // Initiate a PR_Swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendPRSwap\t\tInitiate a PR_Swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceEvaluatePRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceEvaluatePRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendPRSwap: // Initiate a PR_Swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendPRSwap\t\tInitiate a PR_Swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluatePRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluatePRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peGiveVdm: // Send VDM data
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeGiveVdm\t\tSend VDM data\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetIdentity: // Requesting Identity information from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetIdentity\t\tRequesting Identity information from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendIdentity: // Sending Discover Identity ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendIdentity\t\tSending Discover Identity ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetSvids: // Requesting SVID info from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetSvids\t\tRequesting SVID info from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendSvids: // Sending Discover SVIDs ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendSvids\t\tSending Discover SVIDs ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetModes: // Requesting Mode info from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetModes\t\tRequesting Mode info from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendModes: // Sending Discover Modes ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendModes\t\tSending Discover Modes ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmEvaluateModeEntry: // Requesting DPM to evaluate request to enter a mode, and enter if OK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmEvaluateModeEntry\t\tRequesting DPM to evaluate request to enter a mode, and enter if OK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeEntryNak: // Sending Enter Mode NAK response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeEntryNak\t\tSending Enter Mode NAK response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeEntryAck: // Sending Enter Mode ACK response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeEntryAck\t\tSending Enter Mode ACK response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExit: // Requesting DPM to evalute request to exit mode
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExit\t\tRequesting DPM to evalute request to exit mode\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExitNak: // Sending Exit Mode NAK reponse
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExitNak\t\tSending Exit Mode NAK reponse\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExitAck: // Sending Exit Mode ACK Response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExitAck\t\tSending Exit Mode ACK Response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmAttentionRequest: // Sending Attention Command
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmAttentionRequest\t\tSending Attention Command\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityRequest: // Sending Identity Request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityRequest\t\tSending Identity Request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityAcked: // Inform DPM of Identity
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityAcked\t\tInform DPM of Identity\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityNaked: // Inform DPM of result
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityNaked\t\tInform DPM of result\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityRequest: // Sending Identity Request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityRequest\t\tSending Identity Request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsRequest: // Sending Discover SVIDs request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsRequest\t\tSending Discover SVIDs request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesRequest: // Sending Discover Modes request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesRequest\t\tSending Discover Modes request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryRequest: // Sending Mode Entry request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryRequest\t\tSending Mode Entry request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeExitRequest: // Sending Exit Mode request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeExitRequest\t\tSending Exit Mode request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmExitModeAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmExitModeAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityRequest: // sending Discover Identity request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityRequest\t\tsending Discover Identity request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityAcked: // inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityAcked\t\tinform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityNaked: // inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityNaked\t\tinform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmAttentionRequest: // Attention Request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmAttentionRequest\t\tAttention Request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblReady: // Cable power up state?
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblReady\t\tCable power up state?\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetIdentity: // Discover Identity request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetIdentity\t\tDiscover Identity request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetIdentityNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetIdentityNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendIdentity: // Respond with Ack
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendIdentity\t\tRespond with Ack\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetSvids: // Discover SVIDs request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetSvids\t\tDiscover SVIDs request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetSvidsNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetSvidsNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendSvids: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendSvids\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetModes: // Discover Modes request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetModes\t\tDiscover Modes request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetModesNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetModesNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendModes: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendModes\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblEvaluateModeEntry: // Enter Mode request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblEvaluateModeEntry\t\tEnter Mode request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeEntryAck: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeEntryAck\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeEntryNak: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeEntryNak\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExit: // Exit Mode request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExit\t\tExit Mode request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExitAck: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExitAck\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExitNak: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExitNak\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDpRequestStatus: // Requesting PP Status
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDpRequestStatus\t\tRequesting PP Status\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Receive_Mode: // Bist Receive Mode
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Receive_Mode\t\tBist Receive Mode\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Frame_Received: // Test Frame received by Protocol layer
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Frame_Received\t\tTest Frame received by Protocol layer\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Carrier_Mode_2: // BIST Carrier Mode 2
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Carrier_Mode_2\t\tBIST Carrier Mode 2\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceWaitNewCapabilities: // Wait for new Source Capabilities from Policy Manager
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceWaitNewCapabilities\t\tWait for new Source Capabilities from Policy Manager\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case dbgGetRxPacket: // Debug point for measuring Rx packet handling in ProtocolGetRxPacket()
{
// Special parsing for this state - TimeStampSeconds is really the number of I2C reads performed, and TimeStampMS10ths is the time elapsed (in ms)
numChars += sprintf(tempBuf, "%02u|0.%04u\tdbgGetRxPacket\t\t\tNumber of I2C bytes read | Time elapsed\n", TimeStampSeconds, TimeStampMS10ths);
// numChars += sprintf(tempBuf, "%u | %u\tdbgGetRxPacket\t\t\tHeader[0] | Header[1]", chip->HostCommBuf[i + 1], chip->HostCommBuf[i + 3]);
strcat(buf, tempBuf);
break;
}
case dbgSendTxPacket: // Debug point for measuring Tx packet handling in ProtocolTransmitMessage()
{
// Special parsing for this state - TimeStampSeconds is really the number of I2C reads performed, and TimeStampMS10ths is the time elapsed (in ms)
numChars += sprintf(tempBuf, "%02u|0.%04u\tdbgGetTxPacket\t\t\tNumber of I2C bytes sent | Time elapsed\n", TimeStampSeconds, TimeStampMS10ths);
// numChars += sprintf(tempBuf, "%u | %u\tdbgGetRxPacket\t\t\tHeader[0] | Header[1]", chip->HostCommBuf[i + 1], chip->HostCommBuf[i + 3]);
strcat(buf, tempBuf);
break;
}
default:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUKNOWN STATE: 0x%02x\n", TimeStampSeconds, TimeStampMS10ths, chip->HostCommBuf[i]);
strcat(buf, tempBuf);
break;
}
}
}
strcat(buf, "\n"); // Append a newline for pretty++
numChars++; // Account for newline
}
else if (chip->HostCommBuf[0] == CMD_READ_STATE_LOG) // Parse out the Type-C state log
{
numLogs = chip->HostCommBuf[3];
/* First byte echos the command, 4th byte is number of logs (2nd and 3rd bytes reserved as 0) */
numChars += sprintf(tempBuf, "Type-C State Log has %u entries:\n", numLogs); // Copy string + null terminator
strcat(buf, tempBuf);
/* Relevant data starts at 5th byte in this format: CMD 0 0 #Logs State time time time time */
for (i = 4; (i + 4 < FSC_HOSTCOMM_BUFFER_SIZE) && (numChars < PAGE_SIZE) && (numLogs > 0); i += 5, numLogs--) // Must be able to peek 4 bytes ahead, and don't overflow the output buffer (PAGE_SIZE), only print logs we have
{
// Parse out the timestamp
fusb_timestamp_bytes_to_time(&TimeStampSeconds, &TimeStampMS10ths, &chip->HostCommBuf[i + 1]);
// sprintf should be safe here because we're controlling the strings being printed, just make sure the strings are less than FUSB_MAX_BUF_SIZE+1
switch (chip->HostCommBuf[i])
{
case Disabled:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDisabled\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case ErrorRecovery:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tErrorRecovery\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case Unattached:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnattached\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachedSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachedSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachedSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachedSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TrySource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTrySource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TryWaitSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTryWaitSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TrySink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTrySink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TryWaitSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTryWaitSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AudioAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAudioAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case DebugAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDebugAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PoweredAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPoweredAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case UnsupportedAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnsupportedAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case DelayUnattached:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDelayUnattached\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case UnattachedSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnattachedSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
default:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUKNOWN STATE: 0x%02x\n", TimeStampSeconds, TimeStampMS10ths, chip->HostCommBuf[i]);
strcat(buf, tempBuf);
break;
}
}
}
strcat(buf, "\n"); // Append a newline for pretty++
numChars++; // Account for newline
}
else
{
for (i = 0; i < FSC_HOSTCOMM_BUFFER_SIZE; i++)
{
numChars += scnprintf(tempBuf, 6 * sizeof(char), "0x%02x ", chip->HostCommBuf[i]); // Copy 1 byte + null term
strcat(buf, tempBuf); // Append each number to the output buffer
}
strcat(buf, "\n"); // Append a newline for pretty++
numChars++; // Account for newline
}
return numChars;
}
/*******************************************************************************
* Function: fusb_Sysfs_Handle_Write
* Input: input: Buffer passed in from OS (space-separated list of 8-bit hex values)
* size: Number of chars in input
* output: Buffer to which the output will be written
* Return: Number of chars written to output
* Description: Performs hostcomm duties, and stores output buffer in chip structure
********************************************************************************/
static ssize_t _fusb_Sysfs_Hostcomm_store(struct device* dev, struct device_attribute* attr, const char* input, size_t size)
{
FSC_S32 ret = 0;
FSC_S32 i = 0;
FSC_S32 j = 0;
FSC_S8 tempByte = 0;
FSC_S32 numBytes = 0;
FSC_S8 temp[6] = { 0 }; // Temp buffer to parse out individual hex numbers, +1 for null terminator
FSC_S8 temp_input[FSC_HOSTCOMM_BUFFER_SIZE] = { 0 };
FSC_S8 output[FSC_HOSTCOMM_BUFFER_SIZE] = { 0 };
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL)
{
pr_err("%s - Chip structure is null!\n", __func__);
}
else if (input == NULL)
{
pr_err("%s - Error: Input buffer is NULL!\n", __func__);
}
else
{
// Convert the buffer to hex values
for (i = 0; i < size; i = i + j)
{
// Parse out a hex number (at most 5 chars: "0x## ")
for (j = 0; (j < 5) && (j + i < size); j++)
{
// End of the hex number (space-delimited)
if (input[i + j] == ' ')
{
break; // We found a space, stop copying this number and convert it
}
temp[j] = input[i + j]; // Copy the non-space byte into the temp buffer
}
temp[++j] = 0; // Add a null terminator and move past the space
// We have a hex digit (hopefully), now convert it
ret = kstrtou8(temp, 16, &tempByte);
if (ret != 0)
{
pr_err("FUSB %s - Error: Hostcomm input is not a valid hex value! Return: '%d'\n", __func__, ret);
return 0; // Quit on error
}
else
{
temp_input[numBytes++] = tempByte;
if (numBytes >= FSC_HOSTCOMM_BUFFER_SIZE)
{
break;
}
}
}
fusb_ProcessMsg(temp_input, output); // Handle the message
memcpy(chip->HostCommBuf, output, FSC_HOSTCOMM_BUFFER_SIZE); // Copy input into temp buffer
}
return size;
}
/* Fetch and display the PD state log */
static ssize_t _fusb_Sysfs_PDStateLog_show(struct device* dev, struct device_attribute* attr, char* buf)
{
FSC_S32 i = 0;
FSC_S32 numChars = 0;
FSC_S32 numLogs = 0;
FSC_U16 PDMessageHeader = 0; // PD Message header bytes
FSC_U32 TimeStampSeconds = 0; // Timestamp value in seconds
FSC_U32 TimeStampMS10ths = 0; // Timestamp fraction in 10ths of milliseconds
FSC_U8 MessageType[32] = { 0 }; // Temp buffer to parse the PD message type from the PD message header
FSC_U8 output[FSC_HOSTCOMM_BUFFER_SIZE] = { 0 };
FSC_U8 tempBuf[FUSB_MAX_BUF_SIZE] = { 0 };
tempBuf[0] = CMD_READ_PD_STATE_LOG; // To request the PD statelog from Hostcomm
/* Get the PD State Log */
fusb_ProcessMsg(tempBuf, output);
numLogs = output[3];
/* First byte echos the command, 4th byte is number of logs (2nd and 3rd bytes reserved as 0) */
numChars += sprintf(tempBuf, "PD State Log has %u entries:\n", numLogs); // Copy string + null terminator
strcat(buf, tempBuf);
/* Relevant data starts at 5th byte in this format: CMD 0 0 #Logs PDState time time time time */
for (i = 4; (i + 4 < FSC_HOSTCOMM_BUFFER_SIZE) && (numChars < PAGE_SIZE) && (numLogs > 0); i += 5, numLogs--) // Must be able to peek 4 bytes ahead, and don't overflow the output buffer (PAGE_SIZE)
{
// Parse out the timestamp
// Parse out the timestamp
if (output[i] != dbgGetRxPacket)
{
fusb_timestamp_bytes_to_time(&TimeStampSeconds, &TimeStampMS10ths, &output[i + 1]);
}
// sprintf should be safe here because we're controlling the strings being printed, just make sure the strings are less than FUSB_MAX_BUF_SIZE+1
switch (output[i])
{
case peDisabled: // Policy engine is disabled
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDisabled\t\tPolicy engine is disabled\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peErrorRecovery: // Error recovery state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeErrorRecovery\t\tError recovery state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceHardReset: // Received a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceHardReset\t\tReceived a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendHardReset: // Source send a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendHardReset\t\tSource send a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSoftReset: // Received a soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSoftReset\t\tReceived a soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendSoftReset: // Send a soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendSoftReset\t\tSend a soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceStartup: // Initial state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceStartup\t\tInitial state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendCaps: // Send the source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendCaps\t\tSend the source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceDiscovery: // Waiting to detect a USB PD sink
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceDiscovery\t\tWaiting to detect a USB PD sink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceDisabled: // Disabled state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceDisabled\t\tDisabled state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceTransitionDefault: // Transition to default 5V state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceTransitionDefault\t\tTransition to default 5V state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceNegotiateCap: // Negotiate capability and PD contract
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceNegotiateCap\t\tNegotiate capability and PD contract\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceCapabilityResponse: // Respond to a request message with a reject/wait
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceCapabilityResponse\t\tRespond to a request message with a reject/wait\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceTransitionSupply: // Transition the power supply to the new setting (accept request)
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceTransitionSupply\t\tTransition the power supply to the new setting (accept request)\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceReady: // Contract is in place and output voltage is stable
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceReady\t\tContract is in place and output voltage is stable\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGiveSourceCaps: // State to resend source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGiveSourceCaps\t\tState to resend source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGetSinkCaps: // State to request the sink capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGetSinkCaps\t\tState to request the sink capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendPing: // State to send a ping message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendPing\t\tState to send a ping message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGotoMin: // State to send the gotoMin and ready the power supply
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGotoMin\t\tState to send the gotoMin and ready the power supply\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGiveSinkCaps: // State to send the sink capabilities if dual-role
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGiveSinkCaps\t\tState to send the sink capabilities if dual-role\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceGetSourceCaps: // State to request the source caps from the UFP
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceGetSourceCaps\t\tState to request the source caps from the UFP\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendDRSwap: // State to send a DR_Swap message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendDRSwap\t\tState to send a DR_Swap message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceEvaluateDRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceEvaluateDRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkHardReset: // Received a hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkHardReset\t\tReceived a hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendHardReset: // Sink send hard reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendHardReset\t\tSink send hard reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSoftReset: // Sink soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSoftReset\t\tSink soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendSoftReset: // Sink send soft reset
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendSoftReset\t\tSink send soft reset\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkTransitionDefault: // Transition to the default state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkTransitionDefault\t\tTransition to the default state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkStartup: // Initial sink state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkStartup\t\tInitial sink state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkDiscovery: // Sink discovery state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkDiscovery\t\tSink discovery state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkWaitCaps: // Sink wait for capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkWaitCaps\t\tSink wait for capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateCaps: // Sink state to evaluate the received source capabilities
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateCaps\t\tSink state to evaluate the received source capabilities\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSelectCapability: // Sink state for selecting a capability
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSelectCapability\t\tSink state for selecting a capability\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkTransitionSink: // Sink state for transitioning the current power
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkTransitionSink\t\tSink state for transitioning the current power\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkReady: // Sink ready state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkReady\t\tSink ready state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGiveSinkCap: // Sink send capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGiveSinkCap\t\tSink send capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGetSourceCap: // Sink get source capabilities state
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGetSourceCap\t\tSink get source capabilities state\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGetSinkCap: // Sink state to get the sink capabilities of the connected source
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGetSinkCap\t\tSink state to get the sink capabilities of the connected source\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkGiveSourceCap: // Sink state to send the source capabilities if dual-role
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkGiveSourceCap\t\tSink state to send the source capabilities if dual-role\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendDRSwap: // State to send a DR_Swap message
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendDRSwap\t\tState to send a DR_Swap message\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateDRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateDRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendVCONNSwap: // Initiate a VCONN swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendVCONNSwap\t\tInitiate a VCONN swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluateVCONNSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluateVCONNSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceSendPRSwap: // Initiate a PR_Swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceSendPRSwap\t\tInitiate a PR_Swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceEvaluatePRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceEvaluatePRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkSendPRSwap: // Initiate a PR_Swap sequence
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkSendPRSwap\t\tInitiate a PR_Swap sequence\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSinkEvaluatePRSwap: // Evaluate whether we are going to accept or reject the swap
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSinkEvaluatePRSwap\t\tEvaluate whether we are going to accept or reject the swap\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peGiveVdm: // Send VDM data
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeGiveVdm\t\tSend VDM data\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetIdentity: // Requesting Identity information from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetIdentity\t\tRequesting Identity information from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendIdentity: // Sending Discover Identity ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendIdentity\t\tSending Discover Identity ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetSvids: // Requesting SVID info from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetSvids\t\tRequesting SVID info from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendSvids: // Sending Discover SVIDs ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendSvids\t\tSending Discover SVIDs ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmGetModes: // Requesting Mode info from DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmGetModes\t\tRequesting Mode info from DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmSendModes: // Sending Discover Modes ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmSendModes\t\tSending Discover Modes ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmEvaluateModeEntry: // Requesting DPM to evaluate request to enter a mode, and enter if OK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmEvaluateModeEntry\t\tRequesting DPM to evaluate request to enter a mode, and enter if OK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeEntryNak: // Sending Enter Mode NAK response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeEntryNak\t\tSending Enter Mode NAK response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeEntryAck: // Sending Enter Mode ACK response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeEntryAck\t\tSending Enter Mode ACK response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExit: // Requesting DPM to evalute request to exit mode
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExit\t\tRequesting DPM to evalute request to exit mode\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExitNak: // Sending Exit Mode NAK reponse
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExitNak\t\tSending Exit Mode NAK reponse\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmModeExitAck: // Sending Exit Mode ACK Response
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmModeExitAck\t\tSending Exit Mode ACK Response\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peUfpVdmAttentionRequest: // Sending Attention Command
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeUfpVdmAttentionRequest\t\tSending Attention Command\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityRequest: // Sending Identity Request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityRequest\t\tSending Identity Request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityAcked: // Inform DPM of Identity
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityAcked\t\tInform DPM of Identity\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpUfpVdmIdentityNaked: // Inform DPM of result
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpUfpVdmIdentityNaked\t\tInform DPM of result\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityRequest: // Sending Identity Request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityRequest\t\tSending Identity Request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpCblVdmIdentityNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpCblVdmIdentityNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsRequest: // Sending Discover SVIDs request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsRequest\t\tSending Discover SVIDs request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmSvidsNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmSvidsNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesRequest: // Sending Discover Modes request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesRequest\t\tSending Discover Modes request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModesNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModesNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryRequest: // Sending Mode Entry request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryRequest\t\tSending Mode Entry request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeEntryNaked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeEntryNaked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmModeExitRequest: // Sending Exit Mode request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmModeExitRequest\t\tSending Exit Mode request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmExitModeAcked: // Inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmExitModeAcked\t\tInform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityRequest: // sending Discover Identity request
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityRequest\t\tsending Discover Identity request\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityAcked: // inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityAcked\t\tinform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSrcVdmIdentityNaked: // inform DPM
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSrcVdmIdentityNaked\t\tinform DPM\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDfpVdmAttentionRequest: // Attention Request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDfpVdmAttentionRequest\t\tAttention Request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblReady: // Cable power up state?
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblReady\t\tCable power up state?\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetIdentity: // Discover Identity request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetIdentity\t\tDiscover Identity request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetIdentityNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetIdentityNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendIdentity: // Respond with Ack
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendIdentity\t\tRespond with Ack\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetSvids: // Discover SVIDs request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetSvids\t\tDiscover SVIDs request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetSvidsNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetSvidsNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendSvids: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendSvids\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetModes: // Discover Modes request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetModes\t\tDiscover Modes request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblGetModesNak: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblGetModesNak\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblSendModes: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblSendModes\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblEvaluateModeEntry: // Enter Mode request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblEvaluateModeEntry\t\tEnter Mode request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeEntryAck: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeEntryAck\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeEntryNak: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeEntryNak\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExit: // Exit Mode request received
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExit\t\tExit Mode request received\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExitAck: // Respond with NAK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExitAck\t\tRespond with NAK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peCblModeExitNak: // Respond with ACK
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeCblModeExitNak\t\tRespond with ACK\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peDpRequestStatus: // Requesting PP Status
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeDpRequestStatus\t\tRequesting PP Status\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Receive_Mode: // Bist Receive Mode
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Receive_Mode\t\tBist Receive Mode\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Frame_Received: // Test Frame received by Protocol layer
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Frame_Received\t\tTest Frame received by Protocol layer\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PE_BIST_Carrier_Mode_2: // BIST Carrier Mode 2
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPE_BIST_Carrier_Mode_2\t\tBIST Carrier Mode 2\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case peSourceWaitNewCapabilities: // Wait for new Source Capabilities from Policy Manager
{
numChars += sprintf(tempBuf, "[%u.%04u]\tpeSourceWaitNewCapabilities\t\tWait for new Source Capabilities from Policy Manager\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case dbgSendTxPacket: // Treat the same as Rx packet, just change "Rx" to "Tx"
case dbgGetRxPacket: // Debug point for measuring Rx packet handling time in ProtocolGetRxPacket()
{
// Special parsing for this state - TimeStampSeconds is really the number of I2C reads performed, and TimeStampMS10ths is the time elapsed (in ms)
// numChars += sprintf(tempBuf, "%02u|0.%04u\tdbgGetRxPacket\t\t\tNumber of I2C bytes read | Time elapsed\n", TimeStampSeconds, TimeStampMS10ths);
// Recombine the 2 header bytes into a u16
PDMessageHeader = output[i + 4]; // Get MSByte
PDMessageHeader = PDMessageHeader << 8; // Shift into MS position
PDMessageHeader |= output[i + 2]; // Get LSByte
// Parse out the message type to make the log easier to read
if (fusb_get_pd_message_type(PDMessageHeader, MessageType) > -1)
{
numChars += sprintf(tempBuf, "0x%x\t\t%s\t\tMessage Type: %s\n", PDMessageHeader, (output[i] == dbgGetRxPacket) ? "dbgGetRxPacket" : "dbgSendTxPacket", MessageType);
}
else
{
numChars += sprintf(tempBuf, "0x%x\t\t%s\t\tMessage Type: UNKNOWN\n", PDMessageHeader, (output[i] == dbgGetRxPacket) ? "dbgGetRxPacket" : "dbgSendTxPacket");
}
strcat(buf, tempBuf);
break;
}
default:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUKNOWN STATE: 0x%02x\n", TimeStampSeconds, TimeStampMS10ths, output[i]);
strcat(buf, tempBuf);
break;
}
}
}
strcat(buf, "\n"); // Append a newline for pretty++
return ++numChars; // Account for newline and return number of bytes to be shown
}
/* Fetch and display the Type-C state log */
static ssize_t _fusb_Sysfs_TypeCStateLog_show(struct device* dev, struct device_attribute* attr, char* buf)
{
FSC_S32 i = 0;
FSC_S32 numChars = 0;
FSC_S32 numLogs = 0;
FSC_U32 TimeStampSeconds = 0; // Timestamp value in seconds
FSC_U32 TimeStampMS10ths = 0; // Timestamp fraction in 10ths of milliseconds
FSC_S8 output[FSC_HOSTCOMM_BUFFER_SIZE] = { 0 };
FSC_S8 tempBuf[FUSB_MAX_BUF_SIZE] = { 0 };
tempBuf[0] = CMD_READ_STATE_LOG; // To request the Type-C statelog from Hostcomm
/* Get the PD State Log */
fusb_ProcessMsg(tempBuf, output);
numLogs = output[3];
/* First byte echos the command, 4th byte is number of logs (2nd and 3rd bytes reserved as 0) */
numChars += sprintf(tempBuf, "Type-C State Log has %u entries:\n", numLogs); // Copy string + null terminator
strcat(buf, tempBuf);
/* Relevant data starts at 5th byte in this format: CMD 0 0 #Logs State time time time time */
for (i = 4; (i + 4 < FSC_HOSTCOMM_BUFFER_SIZE) && (numChars < PAGE_SIZE) && (numLogs > 0); i += 5, numLogs--) // Must be able to peek 4 bytes ahead, and don't overflow the output buffer (PAGE_SIZE), only print logs we have
{
// Parse out the timestamp
fusb_timestamp_bytes_to_time(&TimeStampSeconds, &TimeStampMS10ths, &output[i + 1]);
// sprintf should be safe here because we're controlling the strings being printed, just make sure the strings are less than FUSB_MAX_BUF_SIZE+1
switch (output[i])
{
case Disabled:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDisabled\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case ErrorRecovery:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tErrorRecovery\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case Unattached:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnattached\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachedSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachedSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachedSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachedSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TrySource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTrySource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TryWaitSink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTryWaitSink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TrySink:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTrySink\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case TryWaitSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tTryWaitSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AudioAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAudioAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case DebugAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDebugAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case AttachWaitAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tAttachWaitAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case PoweredAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tPoweredAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case UnsupportedAccessory:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnsupportedAccessory\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case DelayUnattached:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tDelayUnattached\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
case UnattachedSource:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUnattachedSource\n", TimeStampSeconds, TimeStampMS10ths);
strcat(buf, tempBuf);
break;
}
default:
{
numChars += sprintf(tempBuf, "[%u.%04u]\tUKNOWN STATE: 0x%02x\n", TimeStampSeconds, TimeStampMS10ths, output[i]);
strcat(buf, tempBuf);
break;
}
}
}
strcat(buf, "\n"); // Append a newline for pretty++
return ++numChars; // Account for newline and return number of bytes to be shown
}
/* Reinitialize the FUSB302 */
static ssize_t _fusb_Sysfs_Reinitialize_fusb302(struct device* dev, struct device_attribute* attr, char* buf)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL)
{
return sprintf(buf, "FUSB302 Error: Internal chip structure pointer is NULL!\n");
}
/* Make sure that we are doing this in a thread-safe manner */
#ifdef FSC_INTERRUPT_TRIGGERED
disable_irq(chip->gpio_IntN_irq); // Waits for current IRQ handler to return, then disables it
#else
fusb_StopThreads(); // Waits for current work to complete, then cancels scheduled work and flushed the work queue
#endif // FSC_INTERRUPT_TRIGGERED
fusb_StopTimers();
core_initialize();
pr_debug ("FUSB %s - Core is initialized!\n", __func__);
fusb_StartTimers();
core_enable_typec(TRUE);
pr_debug ("FUSB %s - Type-C State Machine is enabled!\n", __func__);
#ifdef FSC_INTERRUPT_TRIGGERED
enable_irq(chip->gpio_IntN_irq);
#else
// Schedule to kick off the main working thread
schedule_work(&chip->worker);
#endif // FSC_INTERRUPT_TRIGGERED
return sprintf(buf, "FUSB302 Reinitialized!\n");
}
static ssize_t _fusb_Sysfs_vconn_en_store(struct device* dev, struct device_attribute* attr, const char* input, size_t size)
{
int vconn_input = 0;
sscanf(input, "%d", &vconn_input);
pr_info("FUSB [%s]: input: %d\n", __func__, vconn_input);
switch (vconn_input) {
case DISABLE_VCONN: // Disable VCONN
{
if (VCONN_enabled) {
if (!fusb_Power_Vconn(FALSE))
pr_err("FUSB [%s]: Error: Unable to force power off VCONN\n", __func__);
else {
Registers.Switches.byte[0] = Registers.Switches_temp.byte[0]; // restore Switch0 register status
Registers.Switches.VCONN_CC1 = 0;
Registers.Switches.VCONN_CC2 = 0;
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
VCONN_enabled = FALSE;
pr_info("FUSB [%s]: Force power off the VCONN done, and now Registers.Switches(0x02): 0x%x\n", __func__, Registers.Switches.byte[0]);
}
} else {
pr_info("FUSB [%s]: VCONN is already disabled, so we just break\n", __func__);
}
break;
}
case ENABLE_VCONN_CC1:
{
if (!fusb_Power_Vconn(TRUE))
pr_err("FUSB [%s]: Error: Unable to force power on VCONN %d\n", __func__, vconn_input);
else {
DeviceRead(regSwitches0, 1, &Registers.Switches_temp.byte[0]); // Backup current Switch0 register status
Registers.Switches.VCONN_CC1 = 1;
Registers.Switches.VCONN_CC2 = 0;
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
VCONN_enabled = TRUE;
pr_info("FUSB [%s]: Force power on the VCONN %d done, and now Registers.Switches(0x02): 0x%x\n", __func__, vconn_input, Registers.Switches.byte[0]);
}
break;
}
case ENABLE_VCONN_CC2:
{
if (!fusb_Power_Vconn(TRUE))
pr_err("FUSB [%s]: Error: Unable to force power on VCONN %d\n", __func__, vconn_input);
else {
DeviceRead(regSwitches0, 1, &Registers.Switches_temp.byte[0]); // Backup current Switch0 register status
Registers.Switches.VCONN_CC1 = 0;
Registers.Switches.VCONN_CC2 = 1;
DeviceWrite(regSwitches0, 1, &Registers.Switches.byte[0]);
VCONN_enabled = TRUE;
pr_info("FUSB [%s]: Force power on the VCONN %d done, and now Registers.Switches(0x02): 0x%x\n", __func__, vconn_input, Registers.Switches.byte[0]);
}
break;
}
default:
pr_err("FUSB [%s]: Error: Unable to handle the input: %d\n", __func__, vconn_input);
break;
}
return size;
}
// Define our device attributes to export them to sysfs
static DEVICE_ATTR(fusb30x_hostcomm, S_IRWXU | S_IRWXG | S_IROTH, _fusb_Sysfs_Hostcomm_show, _fusb_Sysfs_Hostcomm_store);
static DEVICE_ATTR(pd_state_log, S_IRUSR | S_IRGRP | S_IROTH, _fusb_Sysfs_PDStateLog_show, NULL);
static DEVICE_ATTR(typec_state_log, S_IRUSR | S_IRGRP | S_IROTH, _fusb_Sysfs_TypeCStateLog_show, NULL);
static DEVICE_ATTR(reinitialize, S_IRUSR | S_IRGRP | S_IROTH, _fusb_Sysfs_Reinitialize_fusb302, NULL);
static DEVICE_ATTR(vconn_en, S_IWUSR | S_IRUGO, NULL, _fusb_Sysfs_vconn_en_store);
static struct attribute *fusb302_sysfs_attrs[] = {
&dev_attr_fusb30x_hostcomm.attr,
&dev_attr_pd_state_log.attr,
&dev_attr_typec_state_log.attr,
&dev_attr_reinitialize.attr,
&dev_attr_vconn_en.attr,
NULL
};
static struct attribute_group fusb302_sysfs_attr_grp = {
.name = "control",
.attrs = fusb302_sysfs_attrs,
};
void fusb_Sysfs_Init(void)
{
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL)
{
pr_err("%s - Chip structure is null!\n", __func__);
return;
}
/* create attribute group for accessing the FUSB302 */
ret = sysfs_create_group(&chip->client->dev.kobj, &fusb302_sysfs_attr_grp);
if (ret)
{
pr_err("FUSB %s - Error creating sysfs attributes!\n", __func__);
}
}
#endif // FSC_DEBUG
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/******************************************** Driver Helpers ******************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void fusb_InitializeCore(void)
{
#ifndef FSC_INTERRUPT_TRIGGERED
fusb_StartTimers();
pr_debug("FUSB %s - Timers are started!\n", __func__);
#endif
core_initialize();
pr_debug("FUSB %s - Core is initialized!\n", __func__);
core_enable_typec(TRUE);
pr_debug("FUSB %s - Type-C State Machine is enabled!\n", __func__);
}
FSC_BOOL fusb_IsDeviceValid(void)
{
FSC_U8 val = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return FALSE;
}
// Test to see if we can do a successful I2C read
if (!fusb_I2C_ReadData((FSC_U8)0x01, &val))
{
pr_err("FUSB %s - Error: Could not communicate with device over I2C!\n", __func__);
return FALSE;
}
return TRUE;
}
void fusb_InitChipData(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (chip == NULL)
{
pr_err("%s - Chip structure is null!\n", __func__);
return;
}
#ifdef FSC_DEBUG
chip->dbgTimerTicks = 0;
chip->dbgTimerRollovers = 0;
chip->dbgSMTicks = 0;
chip->dbgSMRollovers = 0;
chip->dbg_gpio_StateMachine = -1;
chip->dbg_gpio_StateMachine_value = false;
#endif // FSC_DEBUG
/* GPIO Defaults */
chip->gpio_VBus5V = -1;
chip->gpio_VBus5V_value = false;
chip->gpio_VBusOther = -1;
chip->gpio_VBusOther_value = false;
chip->gpio_IntN = -1;
#ifdef FSC_INTERRUPT_TRIGGERED
chip->gpio_IntN_irq = -1;
#endif // FSC_INTERRUPT_TRIGGERED
/* I2C Configuration */
chip->InitDelayMS = INIT_DELAY_MS; // Time to wait before device init
chip->numRetriesI2C = RETRIES_I2C; // Number of times to retry I2C reads and writes
chip->use_i2c_blocks = false; // Assume failure
chip->pmode = DUAL_ROLE_PROP_MODE_NONE;
chip->prole = DUAL_ROLE_PROP_PR_NONE;
chip->drole = DUAL_ROLE_PROP_DR_NONE;
chip->vconn = DUAL_ROLE_PROP_VCONN_SUPPLY_NO;
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/****************************************** IRQ/Threading Helpers *****************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
#ifdef FSC_INTERRUPT_TRIGGERED
FSC_S32 fusb_EnableInterrupts(void)
{
FSC_S32 ret = 0;
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return -ENOMEM;
}
wake_lock_init(&chip->fusb_wlock, WAKE_LOCK_SUSPEND, "fusb_wlock");
/* Set up IRQ for INT_N GPIO */
ret = gpio_to_irq(chip->gpio_IntN); // Returns negative errno on error
if (ret < 0)
{
dev_err(&chip->client->dev, "%s - Error: Unable to request IRQ for INT_N GPIO! Error code: %d\n", __func__, ret);
chip->gpio_IntN_irq = -1; // Set to indicate error
fusb_GPIO_Cleanup();
return ret;
}
chip->gpio_IntN_irq = ret;
pr_debug("%s - Success: Requested INT_N IRQ: '%d'\n", __func__, chip->gpio_IntN_irq);
/* Request threaded IRQ because we will likely sleep while handling the interrupt, trigger is active-low, don't handle concurrent interrupts */
ret = devm_request_threaded_irq(&chip->client->dev, chip->gpio_IntN_irq, NULL, _fusb_isr_intn, IRQF_ONESHOT | IRQF_TRIGGER_LOW, FUSB_DT_INTERRUPT_INTN, chip); // devm_* allocation/free handled by system
if (ret)
{
dev_err(&chip->client->dev, "%s - Error: Unable to request threaded IRQ for INT_N GPIO! Error code: %d\n", __func__, ret);
fusb_GPIO_Cleanup();
return ret;
}
if (chip->gpio_IntN_irq)
enable_irq_wake(chip->gpio_IntN_irq);
return 0;
}
/*******************************************************************************
* Function: _fusb_isr_intn
* Input: irq - IRQ that was triggered
* dev_id - Ptr to driver data structure
* Return: irqreturn_t - IRQ_HANDLED on success, IRQ_NONE on failure
* Description: Activates the core
********************************************************************************/
static irqreturn_t _fusb_isr_intn(FSC_S32 irq, void *dev_id)
{
struct fusb30x_chip* chip = dev_id;
printk(KERN_INFO "FUSB [%s]: FUSB-interrupt triggered ++ \n", __func__);
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return IRQ_NONE;
}
#ifdef FSC_DEBUG
dbg_fusb_GPIO_Set_SM_Toggle(!chip->dbg_gpio_StateMachine_value); // Optionally toggle debug GPIO when SM is called to measure thread tick rate
if (chip->dbgSMTicks++ >= U8_MAX) // Tick our state machine tick counter
{
chip->dbgSMRollovers++; // Record a moderate amount of rollovers
}
#endif // FSC_DEBUG
mutex_lock(&chip->statemachine_lock);
wake_lock(&chip->fusb_wlock);
core_state_machine(); // Run the state machine
wake_unlock(&chip->fusb_wlock);
mutex_unlock(&chip->statemachine_lock);
pr_info("FUSB [%s]: FUSB-interrupt handled ++ \n", __func__);
return IRQ_HANDLED;
}
#else
/*******************************************************************************
* Function: _fusb_InitWorker
* Input: delayed_work - passed in from OS
* Return: none
* Description: Callback for the init worker, kicks off the main worker
********************************************************************************/
void _fusb_InitWorker(struct work_struct* delayed_work)
{
struct fusb30x_chip* chip = container_of(delayed_work, struct fusb30x_chip, init_worker.work);
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
// Schedule to kick off the main working thread
schedule_work(&chip->worker);
}
/*******************************************************************************
* Function: _fusb_MainWorker
* Input: delayed_work - passed in from OS
* Return: none
* Description: Activates the core
********************************************************************************/
void _fusb_MainWorker(struct work_struct* work)
{
struct fusb30x_chip* chip = container_of(work, struct fusb30x_chip, worker);
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
#ifdef FSC_DEBUG
dbg_fusb_GPIO_Set_SM_Toggle(!chip->dbg_gpio_StateMachine_value); // Optionally toggle debug GPIO when SM is called to measure thread tick rate
if (chip->dbgSMTicks++ >= U8_MAX) // Tick our state machine tick counter
{
chip->dbgSMRollovers++; // Record a moderate amount of rollovers
}
#endif // FSC_DEBUG
core_state_machine(); // Run the state machine
schedule_work(&chip->worker); // Reschedule ourselves to run again
}
void fusb_InitializeWorkers(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
pr_debug("FUSB %s - Initializing threads!\n", __func__);
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
// Initialize our delayed_work and work structs
INIT_DELAYED_WORK(&chip->init_worker, _fusb_InitWorker);
INIT_WORK(&chip->worker, _fusb_MainWorker);
}
void fusb_StopThreads(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
// Cancel the initial delayed work
cancel_delayed_work_sync(&chip->init_worker);
flush_delayed_work(&chip->init_worker);
// Cancel the main worker
flush_work(&chip->worker);
cancel_work_sync(&chip->worker);
if (chip->gpio_IntN_irq)
disable_irq_wake(chip->gpio_IntN_irq);
wake_lock_destroy(&chip->fusb_wlock);
}
void fusb_ScheduleWork(void)
{
struct fusb30x_chip* chip = fusb30x_GetChip();
if (!chip)
{
pr_err("FUSB %s - Error: Chip structure is NULL!\n", __func__);
return;
}
schedule_delayed_work(&chip->init_worker, msecs_to_jiffies(chip->InitDelayMS));
}
#endif // FSC_INTERRUPT_TRIGGERED, else
|