1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
|
From fb72b7575a091284a3e2bd0a955aa2c61a6f5bc4 Mon Sep 17 00:00:00 2001
From: Aditya Garg <gargaditya08@live.com>
Date: Thu, 14 Mar 2024 06:51:34 +0000
Subject: [PATCH] Add apple-bce driver
---
drivers/staging/apple-bce/Makefile | 28 +
drivers/staging/apple-bce/apple_bce.c | 443 ++++++++++
drivers/staging/apple-bce/apple_bce.h | 38 +
drivers/staging/apple-bce/audio/audio.c | 711 ++++++++++++++++
drivers/staging/apple-bce/audio/audio.h | 125 +++
drivers/staging/apple-bce/audio/description.h | 42 +
drivers/staging/apple-bce/audio/pcm.c | 308 +++++++
drivers/staging/apple-bce/audio/pcm.h | 16 +
drivers/staging/apple-bce/audio/protocol.c | 347 ++++++++
drivers/staging/apple-bce/audio/protocol.h | 147 ++++
.../staging/apple-bce/audio/protocol_bce.c | 226 ++++++
.../staging/apple-bce/audio/protocol_bce.h | 72 ++
drivers/staging/apple-bce/mailbox.c | 151 ++++
drivers/staging/apple-bce/mailbox.h | 53 ++
drivers/staging/apple-bce/queue.c | 390 +++++++++
drivers/staging/apple-bce/queue.h | 177 ++++
drivers/staging/apple-bce/queue_dma.c | 220 +++++
drivers/staging/apple-bce/queue_dma.h | 50 ++
drivers/staging/apple-bce/vhci/command.h | 204 +++++
drivers/staging/apple-bce/vhci/queue.c | 268 +++++++
drivers/staging/apple-bce/vhci/queue.h | 76 ++
drivers/staging/apple-bce/vhci/transfer.c | 661 +++++++++++++++
drivers/staging/apple-bce/vhci/transfer.h | 73 ++
drivers/staging/apple-bce/vhci/vhci.c | 759 ++++++++++++++++++
drivers/staging/apple-bce/vhci/vhci.h | 52 ++
25 files changed, 5637 insertions(+)
create mode 100644 drivers/staging/apple-bce/Makefile
create mode 100644 drivers/staging/apple-bce/apple_bce.c
create mode 100644 drivers/staging/apple-bce/apple_bce.h
create mode 100644 drivers/staging/apple-bce/audio/audio.c
create mode 100644 drivers/staging/apple-bce/audio/audio.h
create mode 100644 drivers/staging/apple-bce/audio/description.h
create mode 100644 drivers/staging/apple-bce/audio/pcm.c
create mode 100644 drivers/staging/apple-bce/audio/pcm.h
create mode 100644 drivers/staging/apple-bce/audio/protocol.c
create mode 100644 drivers/staging/apple-bce/audio/protocol.h
create mode 100644 drivers/staging/apple-bce/audio/protocol_bce.c
create mode 100644 drivers/staging/apple-bce/audio/protocol_bce.h
create mode 100644 drivers/staging/apple-bce/mailbox.c
create mode 100644 drivers/staging/apple-bce/mailbox.h
create mode 100644 drivers/staging/apple-bce/queue.c
create mode 100644 drivers/staging/apple-bce/queue.h
create mode 100644 drivers/staging/apple-bce/queue_dma.c
create mode 100644 drivers/staging/apple-bce/queue_dma.h
create mode 100644 drivers/staging/apple-bce/vhci/command.h
create mode 100644 drivers/staging/apple-bce/vhci/queue.c
create mode 100644 drivers/staging/apple-bce/vhci/queue.h
create mode 100644 drivers/staging/apple-bce/vhci/transfer.c
create mode 100644 drivers/staging/apple-bce/vhci/transfer.h
create mode 100644 drivers/staging/apple-bce/vhci/vhci.c
create mode 100644 drivers/staging/apple-bce/vhci/vhci.h
diff --git a/drivers/staging/apple-bce/Makefile b/drivers/staging/apple-bce/Makefile
new file mode 100644
index 000000000..a6a656f06
--- /dev/null
+++ b/drivers/staging/apple-bce/Makefile
@@ -0,0 +1,28 @@
+modname := apple-bce
+obj-m += $(modname).o
+
+apple-bce-objs := apple_bce.o mailbox.o queue.o queue_dma.o vhci/vhci.o vhci/queue.o vhci/transfer.o audio/audio.o audio/protocol.o audio/protocol_bce.o audio/pcm.o
+
+MY_CFLAGS += -DWITHOUT_NVME_PATCH
+#MY_CFLAGS += -g -DDEBUG
+ccflags-y += ${MY_CFLAGS}
+CC += ${MY_CFLAGS}
+
+KVERSION := $(KERNELRELEASE)
+ifeq ($(origin KERNELRELEASE), undefined)
+KVERSION := $(shell uname -r)
+endif
+
+KDIR := /lib/modules/$(KVERSION)/build
+PWD := $(shell pwd)
+
+.PHONY: all
+
+all:
+ $(MAKE) -C $(KDIR) M=$(PWD) modules
+
+clean:
+ $(MAKE) -C $(KDIR) M=$(PWD) clean
+
+install:
+ $(MAKE) -C $(KDIR) M=$(PWD) modules_install
diff --git a/drivers/staging/apple-bce/apple_bce.c b/drivers/staging/apple-bce/apple_bce.c
new file mode 100644
index 000000000..ad89632df
--- /dev/null
+++ b/drivers/staging/apple-bce/apple_bce.c
@@ -0,0 +1,443 @@
+#include "apple_bce.h"
+#include <linux/module.h>
+#include <linux/crc32.h>
+#include "audio/audio.h"
+#include <linux/version.h>
+
+static dev_t bce_chrdev;
+static struct class *bce_class;
+
+struct apple_bce_device *global_bce;
+
+static int bce_create_command_queues(struct apple_bce_device *bce);
+static void bce_free_command_queues(struct apple_bce_device *bce);
+static irqreturn_t bce_handle_mb_irq(int irq, void *dev);
+static irqreturn_t bce_handle_dma_irq(int irq, void *dev);
+static int bce_fw_version_handshake(struct apple_bce_device *bce);
+static int bce_register_command_queue(struct apple_bce_device *bce, struct bce_queue_memcfg *cfg, int is_sq);
+
+static int apple_bce_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{
+ struct apple_bce_device *bce = NULL;
+ int status = 0;
+ int nvec;
+
+ pr_info("apple-bce: capturing our device\n");
+
+ if (pci_enable_device(dev))
+ return -ENODEV;
+ if (pci_request_regions(dev, "apple-bce")) {
+ status = -ENODEV;
+ goto fail;
+ }
+ pci_set_master(dev);
+ nvec = pci_alloc_irq_vectors(dev, 1, 8, PCI_IRQ_MSI);
+ if (nvec < 5) {
+ status = -EINVAL;
+ goto fail;
+ }
+
+ bce = kzalloc(sizeof(struct apple_bce_device), GFP_KERNEL);
+ if (!bce) {
+ status = -ENOMEM;
+ goto fail;
+ }
+
+ bce->pci = dev;
+ pci_set_drvdata(dev, bce);
+
+ bce->devt = bce_chrdev;
+ bce->dev = device_create(bce_class, &dev->dev, bce->devt, NULL, "apple-bce");
+ if (IS_ERR_OR_NULL(bce->dev)) {
+ status = PTR_ERR(bce_class);
+ goto fail;
+ }
+
+ bce->reg_mem_mb = pci_iomap(dev, 4, 0);
+ bce->reg_mem_dma = pci_iomap(dev, 2, 0);
+
+ if (IS_ERR_OR_NULL(bce->reg_mem_mb) || IS_ERR_OR_NULL(bce->reg_mem_dma)) {
+ dev_warn(&dev->dev, "apple-bce: Failed to pci_iomap required regions\n");
+ goto fail;
+ }
+
+ bce_mailbox_init(&bce->mbox, bce->reg_mem_mb);
+ bce_timestamp_init(&bce->timestamp, bce->reg_mem_mb);
+
+ spin_lock_init(&bce->queues_lock);
+ ida_init(&bce->queue_ida);
+
+ if ((status = pci_request_irq(dev, 0, bce_handle_mb_irq, NULL, dev, "bce_mbox")))
+ goto fail;
+ if ((status = pci_request_irq(dev, 4, NULL, bce_handle_dma_irq, dev, "bce_dma")))
+ goto fail_interrupt_0;
+
+ if ((status = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(37)))) {
+ dev_warn(&dev->dev, "dma: Setting mask failed\n");
+ goto fail_interrupt;
+ }
+
+ /* Gets the function 0's interface. This is needed because Apple only accepts DMA on our function if function 0
+ is a bus master, so we need to work around this. */
+ bce->pci0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+#ifndef WITHOUT_NVME_PATCH
+ if ((status = pci_enable_device_mem(bce->pci0))) {
+ dev_warn(&dev->dev, "apple-bce: failed to enable function 0\n");
+ goto fail_dev0;
+ }
+#endif
+ pci_set_master(bce->pci0);
+
+ bce_timestamp_start(&bce->timestamp, true);
+
+ if ((status = bce_fw_version_handshake(bce)))
+ goto fail_ts;
+ pr_info("apple-bce: handshake done\n");
+
+ if ((status = bce_create_command_queues(bce))) {
+ pr_info("apple-bce: Creating command queues failed\n");
+ goto fail_ts;
+ }
+
+ global_bce = bce;
+
+ bce_vhci_create(bce, &bce->vhci);
+
+ return 0;
+
+fail_ts:
+ bce_timestamp_stop(&bce->timestamp);
+#ifndef WITHOUT_NVME_PATCH
+ pci_disable_device(bce->pci0);
+fail_dev0:
+#endif
+ pci_dev_put(bce->pci0);
+fail_interrupt:
+ pci_free_irq(dev, 4, dev);
+fail_interrupt_0:
+ pci_free_irq(dev, 0, dev);
+fail:
+ if (bce && bce->dev) {
+ device_destroy(bce_class, bce->devt);
+
+ if (!IS_ERR_OR_NULL(bce->reg_mem_mb))
+ pci_iounmap(dev, bce->reg_mem_mb);
+ if (!IS_ERR_OR_NULL(bce->reg_mem_dma))
+ pci_iounmap(dev, bce->reg_mem_dma);
+
+ kfree(bce);
+ }
+
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+
+ if (!status)
+ status = -EINVAL;
+ return status;
+}
+
+static int bce_create_command_queues(struct apple_bce_device *bce)
+{
+ int status;
+ struct bce_queue_memcfg *cfg;
+
+ bce->cmd_cq = bce_alloc_cq(bce, 0, 0x20);
+ bce->cmd_cmdq = bce_alloc_cmdq(bce, 1, 0x20);
+ if (bce->cmd_cq == NULL || bce->cmd_cmdq == NULL) {
+ status = -ENOMEM;
+ goto err;
+ }
+ bce->queues[0] = (struct bce_queue *) bce->cmd_cq;
+ bce->queues[1] = (struct bce_queue *) bce->cmd_cmdq->sq;
+
+ cfg = kzalloc(sizeof(struct bce_queue_memcfg), GFP_KERNEL);
+ if (!cfg) {
+ status = -ENOMEM;
+ goto err;
+ }
+ bce_get_cq_memcfg(bce->cmd_cq, cfg);
+ if ((status = bce_register_command_queue(bce, cfg, false)))
+ goto err;
+ bce_get_sq_memcfg(bce->cmd_cmdq->sq, bce->cmd_cq, cfg);
+ if ((status = bce_register_command_queue(bce, cfg, true)))
+ goto err;
+ kfree(cfg);
+
+ return 0;
+
+err:
+ if (bce->cmd_cq)
+ bce_free_cq(bce, bce->cmd_cq);
+ if (bce->cmd_cmdq)
+ bce_free_cmdq(bce, bce->cmd_cmdq);
+ return status;
+}
+
+static void bce_free_command_queues(struct apple_bce_device *bce)
+{
+ bce_free_cq(bce, bce->cmd_cq);
+ bce_free_cmdq(bce, bce->cmd_cmdq);
+ bce->cmd_cq = NULL;
+ bce->queues[0] = NULL;
+}
+
+static irqreturn_t bce_handle_mb_irq(int irq, void *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ bce_mailbox_handle_interrupt(&bce->mbox);
+ return IRQ_HANDLED;
+}
+
+static irqreturn_t bce_handle_dma_irq(int irq, void *dev)
+{
+ int i;
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ spin_lock(&bce->queues_lock);
+ for (i = 0; i < BCE_MAX_QUEUE_COUNT; i++)
+ if (bce->queues[i] && bce->queues[i]->type == BCE_QUEUE_CQ)
+ bce_handle_cq_completions(bce, (struct bce_queue_cq *) bce->queues[i]);
+ spin_unlock(&bce->queues_lock);
+ return IRQ_HANDLED;
+}
+
+static int bce_fw_version_handshake(struct apple_bce_device *bce)
+{
+ u64 result;
+ int status;
+
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_SET_FW_PROTOCOL_VERSION, BC_PROTOCOL_VERSION),
+ &result)))
+ return status;
+ if (BCE_MB_TYPE(result) != BCE_MB_SET_FW_PROTOCOL_VERSION ||
+ BCE_MB_VALUE(result) != BC_PROTOCOL_VERSION) {
+ pr_err("apple-bce: FW version handshake failed %x:%llx\n", BCE_MB_TYPE(result), BCE_MB_VALUE(result));
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int bce_register_command_queue(struct apple_bce_device *bce, struct bce_queue_memcfg *cfg, int is_sq)
+{
+ int status;
+ int cmd_type;
+ u64 result;
+ // OS X uses an bidirectional direction, but that's not really needed
+ dma_addr_t a = dma_map_single(&bce->pci->dev, cfg, sizeof(struct bce_queue_memcfg), DMA_TO_DEVICE);
+ if (dma_mapping_error(&bce->pci->dev, a))
+ return -ENOMEM;
+ cmd_type = is_sq ? BCE_MB_REGISTER_COMMAND_SQ : BCE_MB_REGISTER_COMMAND_CQ;
+ status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(cmd_type, a), &result);
+ dma_unmap_single(&bce->pci->dev, a, sizeof(struct bce_queue_memcfg), DMA_TO_DEVICE);
+ if (status)
+ return status;
+ if (BCE_MB_TYPE(result) != BCE_MB_REGISTER_COMMAND_QUEUE_REPLY)
+ return -EINVAL;
+ return 0;
+}
+
+static void apple_bce_remove(struct pci_dev *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ bce->is_being_removed = true;
+
+ bce_vhci_destroy(&bce->vhci);
+
+ bce_timestamp_stop(&bce->timestamp);
+#ifndef WITHOUT_NVME_PATCH
+ pci_disable_device(bce->pci0);
+#endif
+ pci_dev_put(bce->pci0);
+ pci_free_irq(dev, 0, dev);
+ pci_free_irq(dev, 4, dev);
+ bce_free_command_queues(bce);
+ pci_iounmap(dev, bce->reg_mem_mb);
+ pci_iounmap(dev, bce->reg_mem_dma);
+ device_destroy(bce_class, bce->devt);
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+ kfree(bce);
+}
+
+static int bce_save_state_and_sleep(struct apple_bce_device *bce)
+{
+ int attempt, status = 0;
+ u64 resp;
+ dma_addr_t dma_addr;
+ void *dma_ptr = NULL;
+ size_t size = max(PAGE_SIZE, 4096UL);
+
+ for (attempt = 0; attempt < 5; ++attempt) {
+ pr_debug("apple-bce: suspend: attempt %i, buffer size %li\n", attempt, size);
+ dma_ptr = dma_alloc_coherent(&bce->pci->dev, size, &dma_addr, GFP_KERNEL);
+ if (!dma_ptr) {
+ pr_err("apple-bce: suspend failed (data alloc failed)\n");
+ break;
+ }
+ BUG_ON((dma_addr % 4096) != 0);
+ status = bce_mailbox_send(&bce->mbox,
+ BCE_MB_MSG(BCE_MB_SAVE_STATE_AND_SLEEP, (dma_addr & ~(4096LLU - 1)) | (size / 4096)), &resp);
+ if (status) {
+ pr_err("apple-bce: suspend failed (mailbox send)\n");
+ break;
+ }
+ if (BCE_MB_TYPE(resp) == BCE_MB_SAVE_RESTORE_STATE_COMPLETE) {
+ bce->saved_data_dma_addr = dma_addr;
+ bce->saved_data_dma_ptr = dma_ptr;
+ bce->saved_data_dma_size = size;
+ return 0;
+ } else if (BCE_MB_TYPE(resp) == BCE_MB_SAVE_STATE_AND_SLEEP_FAILURE) {
+ dma_free_coherent(&bce->pci->dev, size, dma_ptr, dma_addr);
+ /* The 0x10ff magic value was extracted from Apple's driver */
+ size = (BCE_MB_VALUE(resp) + 0x10ff) & ~(4096LLU - 1);
+ pr_debug("apple-bce: suspend: device requested a larger buffer (%li)\n", size);
+ continue;
+ } else {
+ pr_err("apple-bce: suspend failed (invalid device response)\n");
+ status = -EINVAL;
+ break;
+ }
+ }
+ if (dma_ptr)
+ dma_free_coherent(&bce->pci->dev, size, dma_ptr, dma_addr);
+ if (!status)
+ return bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_SLEEP_NO_STATE, 0), &resp);
+ return status;
+}
+
+static int bce_restore_state_and_wake(struct apple_bce_device *bce)
+{
+ int status;
+ u64 resp;
+ if (!bce->saved_data_dma_ptr) {
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_RESTORE_NO_STATE, 0), &resp))) {
+ pr_err("apple-bce: resume with no state failed (mailbox send)\n");
+ return status;
+ }
+ if (BCE_MB_TYPE(resp) != BCE_MB_RESTORE_NO_STATE) {
+ pr_err("apple-bce: resume with no state failed (invalid device response)\n");
+ return -EINVAL;
+ }
+ return 0;
+ }
+
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_RESTORE_STATE_AND_WAKE,
+ (bce->saved_data_dma_addr & ~(4096LLU - 1)) | (bce->saved_data_dma_size / 4096)), &resp))) {
+ pr_err("apple-bce: resume with state failed (mailbox send)\n");
+ goto finish_with_state;
+ }
+ if (BCE_MB_TYPE(resp) != BCE_MB_SAVE_RESTORE_STATE_COMPLETE) {
+ pr_err("apple-bce: resume with state failed (invalid device response)\n");
+ status = -EINVAL;
+ goto finish_with_state;
+ }
+
+finish_with_state:
+ dma_free_coherent(&bce->pci->dev, bce->saved_data_dma_size, bce->saved_data_dma_ptr, bce->saved_data_dma_addr);
+ bce->saved_data_dma_ptr = NULL;
+ return status;
+}
+
+static int apple_bce_suspend(struct device *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(to_pci_dev(dev));
+ int status;
+
+ bce_timestamp_stop(&bce->timestamp);
+
+ if ((status = bce_save_state_and_sleep(bce)))
+ return status;
+
+ return 0;
+}
+
+static int apple_bce_resume(struct device *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(to_pci_dev(dev));
+ int status;
+
+ pci_set_master(bce->pci);
+ pci_set_master(bce->pci0);
+
+ if ((status = bce_restore_state_and_wake(bce)))
+ return status;
+
+ bce_timestamp_start(&bce->timestamp, false);
+
+ return 0;
+}
+
+static struct pci_device_id apple_bce_ids[ ] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x1801) },
+ { 0, },
+};
+
+struct dev_pm_ops apple_bce_pci_driver_pm = {
+ .suspend = apple_bce_suspend,
+ .resume = apple_bce_resume
+};
+struct pci_driver apple_bce_pci_driver = {
+ .name = "apple-bce",
+ .id_table = apple_bce_ids,
+ .probe = apple_bce_probe,
+ .remove = apple_bce_remove,
+ .driver = {
+ .pm = &apple_bce_pci_driver_pm
+ }
+};
+
+
+static int __init apple_bce_module_init(void)
+{
+ int result;
+ if ((result = alloc_chrdev_region(&bce_chrdev, 0, 1, "apple-bce")))
+ goto fail_chrdev;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0)
+ bce_class = class_create(THIS_MODULE, "apple-bce");
+#else
+ bce_class = class_create("apple-bce");
+#endif
+ if (IS_ERR(bce_class)) {
+ result = PTR_ERR(bce_class);
+ goto fail_class;
+ }
+ if ((result = bce_vhci_module_init())) {
+ pr_err("apple-bce: bce-vhci init failed");
+ goto fail_class;
+ }
+
+ result = pci_register_driver(&apple_bce_pci_driver);
+ if (result)
+ goto fail_drv;
+
+ aaudio_module_init();
+
+ return 0;
+
+fail_drv:
+ pci_unregister_driver(&apple_bce_pci_driver);
+fail_class:
+ class_destroy(bce_class);
+fail_chrdev:
+ unregister_chrdev_region(bce_chrdev, 1);
+ if (!result)
+ result = -EINVAL;
+ return result;
+}
+static void __exit apple_bce_module_exit(void)
+{
+ pci_unregister_driver(&apple_bce_pci_driver);
+
+ aaudio_module_exit();
+ bce_vhci_module_exit();
+ class_destroy(bce_class);
+ unregister_chrdev_region(bce_chrdev, 1);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("MrARM");
+MODULE_DESCRIPTION("Apple BCE Driver");
+MODULE_VERSION("0.01");
+module_init(apple_bce_module_init);
+module_exit(apple_bce_module_exit);
diff --git a/drivers/staging/apple-bce/apple_bce.h b/drivers/staging/apple-bce/apple_bce.h
new file mode 100644
index 000000000..f13ab8d57
--- /dev/null
+++ b/drivers/staging/apple-bce/apple_bce.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include "mailbox.h"
+#include "queue.h"
+#include "vhci/vhci.h"
+
+#define BC_PROTOCOL_VERSION 0x20001
+#define BCE_MAX_QUEUE_COUNT 0x100
+
+#define BCE_QUEUE_USER_MIN 2
+#define BCE_QUEUE_USER_MAX (BCE_MAX_QUEUE_COUNT - 1)
+
+struct apple_bce_device {
+ struct pci_dev *pci, *pci0;
+ dev_t devt;
+ struct device *dev;
+ void __iomem *reg_mem_mb;
+ void __iomem *reg_mem_dma;
+ struct bce_mailbox mbox;
+ struct bce_timestamp timestamp;
+ struct bce_queue *queues[BCE_MAX_QUEUE_COUNT];
+ struct spinlock queues_lock;
+ struct ida queue_ida;
+ struct bce_queue_cq *cmd_cq;
+ struct bce_queue_cmdq *cmd_cmdq;
+ struct bce_queue_sq *int_sq_list[BCE_MAX_QUEUE_COUNT];
+ bool is_being_removed;
+
+ dma_addr_t saved_data_dma_addr;
+ void *saved_data_dma_ptr;
+ size_t saved_data_dma_size;
+
+ struct bce_vhci vhci;
+};
+
+extern struct apple_bce_device *global_bce;
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/audio/audio.c b/drivers/staging/apple-bce/audio/audio.c
new file mode 100644
index 000000000..bd16ddd16
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/audio.c
@@ -0,0 +1,711 @@
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/module.h>
+#include <linux/random.h>
+#include <sound/core.h>
+#include <sound/initval.h>
+#include <sound/pcm.h>
+#include <sound/jack.h>
+#include "audio.h"
+#include "pcm.h"
+#include <linux/version.h>
+
+static int aaudio_alsa_index = SNDRV_DEFAULT_IDX1;
+static char *aaudio_alsa_id = SNDRV_DEFAULT_STR1;
+
+static dev_t aaudio_chrdev;
+static struct class *aaudio_class;
+
+static int aaudio_init_cmd(struct aaudio_device *a);
+static int aaudio_init_bs(struct aaudio_device *a);
+static void aaudio_init_dev(struct aaudio_device *a, aaudio_device_id_t dev_id);
+static void aaudio_free_dev(struct aaudio_subdevice *sdev);
+
+static int aaudio_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{
+ struct aaudio_device *aaudio = NULL;
+ struct aaudio_subdevice *sdev = NULL;
+ int status = 0;
+ u32 cfg;
+
+ pr_info("aaudio: capturing our device\n");
+
+ if (pci_enable_device(dev))
+ return -ENODEV;
+ if (pci_request_regions(dev, "aaudio")) {
+ status = -ENODEV;
+ goto fail;
+ }
+ pci_set_master(dev);
+
+ aaudio = kzalloc(sizeof(struct aaudio_device), GFP_KERNEL);
+ if (!aaudio) {
+ status = -ENOMEM;
+ goto fail;
+ }
+
+ aaudio->bce = global_bce;
+ if (!aaudio->bce) {
+ dev_warn(&dev->dev, "aaudio: No BCE available\n");
+ status = -EINVAL;
+ goto fail;
+ }
+
+ aaudio->pci = dev;
+ pci_set_drvdata(dev, aaudio);
+
+ aaudio->devt = aaudio_chrdev;
+ aaudio->dev = device_create(aaudio_class, &dev->dev, aaudio->devt, NULL, "aaudio");
+ if (IS_ERR_OR_NULL(aaudio->dev)) {
+ status = PTR_ERR(aaudio_class);
+ goto fail;
+ }
+ device_link_add(aaudio->dev, aaudio->bce->dev, DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER);
+
+ init_completion(&aaudio->remote_alive);
+ INIT_LIST_HEAD(&aaudio->subdevice_list);
+
+ /* Init: set an unknown flag in the bitset */
+ if (pci_read_config_dword(dev, 4, &cfg))
+ dev_warn(&dev->dev, "aaudio: pci_read_config_dword fail\n");
+ if (pci_write_config_dword(dev, 4, cfg | 6u))
+ dev_warn(&dev->dev, "aaudio: pci_write_config_dword fail\n");
+
+ dev_info(aaudio->dev, "aaudio: bs len = %llx\n", pci_resource_len(dev, 0));
+ aaudio->reg_mem_bs_dma = pci_resource_start(dev, 0);
+ aaudio->reg_mem_bs = pci_iomap(dev, 0, 0);
+ aaudio->reg_mem_cfg = pci_iomap(dev, 4, 0);
+
+ aaudio->reg_mem_gpr = (u32 __iomem *) ((u8 __iomem *) aaudio->reg_mem_cfg + 0xC000);
+
+ if (IS_ERR_OR_NULL(aaudio->reg_mem_bs) || IS_ERR_OR_NULL(aaudio->reg_mem_cfg)) {
+ dev_warn(&dev->dev, "aaudio: Failed to pci_iomap required regions\n");
+ goto fail;
+ }
+
+ if (aaudio_bce_init(aaudio)) {
+ dev_warn(&dev->dev, "aaudio: Failed to init BCE command transport\n");
+ goto fail;
+ }
+
+ if (snd_card_new(aaudio->dev, aaudio_alsa_index, aaudio_alsa_id, THIS_MODULE, 0, &aaudio->card)) {
+ dev_err(&dev->dev, "aaudio: Failed to create ALSA card\n");
+ goto fail;
+ }
+
+ strcpy(aaudio->card->shortname, "Apple T2 Audio");
+ strcpy(aaudio->card->longname, "Apple T2 Audio");
+ strcpy(aaudio->card->mixername, "Apple T2 Audio");
+ /* Dynamic alsa ids start at 100 */
+ aaudio->next_alsa_id = 100;
+
+ if (aaudio_init_cmd(aaudio)) {
+ dev_err(&dev->dev, "aaudio: Failed to initialize over BCE\n");
+ goto fail_snd;
+ }
+
+ if (aaudio_init_bs(aaudio)) {
+ dev_err(&dev->dev, "aaudio: Failed to initialize BufferStruct\n");
+ goto fail_snd;
+ }
+
+ if ((status = aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_ON))) {
+ dev_err(&dev->dev, "Failed to set remote access\n");
+ return status;
+ }
+
+ if (snd_card_register(aaudio->card)) {
+ dev_err(&dev->dev, "aaudio: Failed to register ALSA sound device\n");
+ goto fail_snd;
+ }
+
+ list_for_each_entry(sdev, &aaudio->subdevice_list, list) {
+ struct aaudio_buffer_struct_device *dev = &aaudio->bs->devices[sdev->buf_id];
+
+ if (sdev->out_stream_cnt == 1 && !strcmp(dev->name, "Speaker")) {
+ struct snd_pcm_hardware *hw = sdev->out_streams[0].alsa_hw_desc;
+
+ snprintf(aaudio->card->driver, sizeof(aaudio->card->driver) / sizeof(char), "AppleT2x%d", hw->channels_min);
+ }
+ }
+
+ return 0;
+
+fail_snd:
+ snd_card_free(aaudio->card);
+fail:
+ if (aaudio && aaudio->dev)
+ device_destroy(aaudio_class, aaudio->devt);
+ kfree(aaudio);
+
+ if (!IS_ERR_OR_NULL(aaudio->reg_mem_bs))
+ pci_iounmap(dev, aaudio->reg_mem_bs);
+ if (!IS_ERR_OR_NULL(aaudio->reg_mem_cfg))
+ pci_iounmap(dev, aaudio->reg_mem_cfg);
+
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+
+ if (!status)
+ status = -EINVAL;
+ return status;
+}
+
+
+
+static void aaudio_remove(struct pci_dev *dev)
+{
+ struct aaudio_subdevice *sdev;
+ struct aaudio_device *aaudio = pci_get_drvdata(dev);
+
+ snd_card_free(aaudio->card);
+ while (!list_empty(&aaudio->subdevice_list)) {
+ sdev = list_first_entry(&aaudio->subdevice_list, struct aaudio_subdevice, list);
+ list_del(&sdev->list);
+ aaudio_free_dev(sdev);
+ }
+ pci_iounmap(dev, aaudio->reg_mem_bs);
+ pci_iounmap(dev, aaudio->reg_mem_cfg);
+ device_destroy(aaudio_class, aaudio->devt);
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+ kfree(aaudio);
+}
+
+static int aaudio_suspend(struct device *dev)
+{
+ struct aaudio_device *aaudio = pci_get_drvdata(to_pci_dev(dev));
+
+ if (aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_OFF))
+ dev_warn(aaudio->dev, "Failed to reset remote access\n");
+
+ pci_disable_device(aaudio->pci);
+ return 0;
+}
+
+static int aaudio_resume(struct device *dev)
+{
+ int status;
+ struct aaudio_device *aaudio = pci_get_drvdata(to_pci_dev(dev));
+
+ if ((status = pci_enable_device(aaudio->pci)))
+ return status;
+ pci_set_master(aaudio->pci);
+
+ if ((status = aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_ON))) {
+ dev_err(aaudio->dev, "Failed to set remote access\n");
+ return status;
+ }
+
+ return 0;
+}
+
+static int aaudio_init_cmd(struct aaudio_device *a)
+{
+ int status;
+ struct aaudio_send_ctx sctx;
+ struct aaudio_msg buf;
+ u64 dev_cnt, dev_i;
+ aaudio_device_id_t *dev_l;
+
+ if ((status = aaudio_send(a, &sctx, 500,
+ aaudio_msg_write_alive_notification, 1, 3))) {
+ dev_err(a->dev, "Sending alive notification failed\n");
+ return status;
+ }
+
+ if (wait_for_completion_timeout(&a->remote_alive, msecs_to_jiffies(500)) == 0) {
+ dev_err(a->dev, "Timed out waiting for remote\n");
+ return -ETIMEDOUT;
+ }
+ dev_info(a->dev, "Continuing init\n");
+
+ buf = aaudio_reply_alloc();
+ if ((status = aaudio_cmd_get_device_list(a, &buf, &dev_l, &dev_cnt))) {
+ dev_err(a->dev, "Failed to get device list\n");
+ aaudio_reply_free(&buf);
+ return status;
+ }
+ for (dev_i = 0; dev_i < dev_cnt; ++dev_i)
+ aaudio_init_dev(a, dev_l[dev_i]);
+ aaudio_reply_free(&buf);
+
+ return 0;
+}
+
+static void aaudio_init_stream_info(struct aaudio_subdevice *sdev, struct aaudio_stream *strm);
+static void aaudio_handle_jack_connection_change(struct aaudio_subdevice *sdev);
+
+static void aaudio_init_dev(struct aaudio_device *a, aaudio_device_id_t dev_id)
+{
+ struct aaudio_subdevice *sdev;
+ struct aaudio_msg buf = aaudio_reply_alloc();
+ u64 uid_len, stream_cnt, i;
+ aaudio_object_id_t *stream_list;
+ char *uid;
+
+ sdev = kzalloc(sizeof(struct aaudio_subdevice), GFP_KERNEL);
+
+ if (aaudio_cmd_get_property(a, &buf, dev_id, dev_id, AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_UID, 0),
+ NULL, 0, (void **) &uid, &uid_len) || uid_len > AAUDIO_DEVICE_MAX_UID_LEN) {
+ dev_err(a->dev, "Failed to get device uid for device %llx\n", dev_id);
+ goto fail;
+ }
+ dev_info(a->dev, "Remote device %llx %.*s\n", dev_id, (int) uid_len, uid);
+
+ sdev->a = a;
+ INIT_LIST_HEAD(&sdev->list);
+ sdev->dev_id = dev_id;
+ sdev->buf_id = AAUDIO_BUFFER_ID_NONE;
+ strncpy(sdev->uid, uid, uid_len);
+ sdev->uid[uid_len + 1] = '\0';
+
+ if (aaudio_cmd_get_primitive_property(a, dev_id, dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_INPUT, AAUDIO_PROP_LATENCY, 0), NULL, 0, &sdev->in_latency, sizeof(u32)))
+ dev_warn(a->dev, "Failed to query device input latency\n");
+ if (aaudio_cmd_get_primitive_property(a, dev_id, dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_OUTPUT, AAUDIO_PROP_LATENCY, 0), NULL, 0, &sdev->out_latency, sizeof(u32)))
+ dev_warn(a->dev, "Failed to query device output latency\n");
+
+ if (aaudio_cmd_get_input_stream_list(a, &buf, dev_id, &stream_list, &stream_cnt)) {
+ dev_err(a->dev, "Failed to get input stream list for device %llx\n", dev_id);
+ goto fail;
+ }
+ if (stream_cnt > AAUDIO_DEIVCE_MAX_INPUT_STREAMS) {
+ dev_warn(a->dev, "Device %s input stream count %llu is larger than the supported count of %u\n",
+ sdev->uid, stream_cnt, AAUDIO_DEIVCE_MAX_INPUT_STREAMS);
+ stream_cnt = AAUDIO_DEIVCE_MAX_INPUT_STREAMS;
+ }
+ sdev->in_stream_cnt = stream_cnt;
+ for (i = 0; i < stream_cnt; i++) {
+ sdev->in_streams[i].id = stream_list[i];
+ sdev->in_streams[i].buffer_cnt = 0;
+ aaudio_init_stream_info(sdev, &sdev->in_streams[i]);
+ sdev->in_streams[i].latency += sdev->in_latency;
+ }
+
+ if (aaudio_cmd_get_output_stream_list(a, &buf, dev_id, &stream_list, &stream_cnt)) {
+ dev_err(a->dev, "Failed to get output stream list for device %llx\n", dev_id);
+ goto fail;
+ }
+ if (stream_cnt > AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS) {
+ dev_warn(a->dev, "Device %s input stream count %llu is larger than the supported count of %u\n",
+ sdev->uid, stream_cnt, AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS);
+ stream_cnt = AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS;
+ }
+ sdev->out_stream_cnt = stream_cnt;
+ for (i = 0; i < stream_cnt; i++) {
+ sdev->out_streams[i].id = stream_list[i];
+ sdev->out_streams[i].buffer_cnt = 0;
+ aaudio_init_stream_info(sdev, &sdev->out_streams[i]);
+ sdev->out_streams[i].latency += sdev->in_latency;
+ }
+
+ if (sdev->is_pcm)
+ aaudio_create_pcm(sdev);
+ /* Headphone Jack status */
+ if (!strcmp(sdev->uid, "Codec Output")) {
+ if (snd_jack_new(a->card, sdev->uid, SND_JACK_HEADPHONE, &sdev->jack, true, false))
+ dev_warn(a->dev, "Failed to create an attached jack for %s\n", sdev->uid);
+ aaudio_cmd_property_listener(a, sdev->dev_id, sdev->dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_OUTPUT, AAUDIO_PROP_JACK_PLUGGED, 0));
+ aaudio_handle_jack_connection_change(sdev);
+ }
+
+ aaudio_reply_free(&buf);
+
+ list_add_tail(&sdev->list, &a->subdevice_list);
+ return;
+
+fail:
+ aaudio_reply_free(&buf);
+ kfree(sdev);
+}
+
+static void aaudio_init_stream_info(struct aaudio_subdevice *sdev, struct aaudio_stream *strm)
+{
+ if (aaudio_cmd_get_primitive_property(sdev->a, sdev->dev_id, strm->id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_PHYS_FORMAT, 0), NULL, 0,
+ &strm->desc, sizeof(strm->desc)))
+ dev_warn(sdev->a->dev, "Failed to query stream descriptor\n");
+ if (aaudio_cmd_get_primitive_property(sdev->a, sdev->dev_id, strm->id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_LATENCY, 0), NULL, 0, &strm->latency, sizeof(u32)))
+ dev_warn(sdev->a->dev, "Failed to query stream latency\n");
+ if (strm->desc.format_id == AAUDIO_FORMAT_LPCM)
+ sdev->is_pcm = true;
+}
+
+static void aaudio_free_dev(struct aaudio_subdevice *sdev)
+{
+ size_t i;
+ for (i = 0; i < sdev->in_stream_cnt; i++) {
+ if (sdev->in_streams[i].alsa_hw_desc)
+ kfree(sdev->in_streams[i].alsa_hw_desc);
+ if (sdev->in_streams[i].buffers)
+ kfree(sdev->in_streams[i].buffers);
+ }
+ for (i = 0; i < sdev->out_stream_cnt; i++) {
+ if (sdev->out_streams[i].alsa_hw_desc)
+ kfree(sdev->out_streams[i].alsa_hw_desc);
+ if (sdev->out_streams[i].buffers)
+ kfree(sdev->out_streams[i].buffers);
+ }
+ kfree(sdev);
+}
+
+static struct aaudio_subdevice *aaudio_find_dev_by_dev_id(struct aaudio_device *a, aaudio_device_id_t dev_id)
+{
+ struct aaudio_subdevice *sdev;
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (dev_id == sdev->dev_id)
+ return sdev;
+ }
+ return NULL;
+}
+
+static struct aaudio_subdevice *aaudio_find_dev_by_uid(struct aaudio_device *a, const char *uid)
+{
+ struct aaudio_subdevice *sdev;
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (!strcmp(uid, sdev->uid))
+ return sdev;
+ }
+ return NULL;
+}
+
+static void aaudio_init_bs_stream(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm);
+static void aaudio_init_bs_stream_host(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm);
+
+static int aaudio_init_bs(struct aaudio_device *a)
+{
+ int i, j;
+ struct aaudio_buffer_struct_device *dev;
+ struct aaudio_subdevice *sdev;
+ u32 ver, sig, bs_base;
+
+ ver = ioread32(&a->reg_mem_gpr[0]);
+ if (ver < 3) {
+ dev_err(a->dev, "aaudio: Bad GPR version (%u)", ver);
+ return -EINVAL;
+ }
+ sig = ioread32(&a->reg_mem_gpr[1]);
+ if (sig != AAUDIO_SIG) {
+ dev_err(a->dev, "aaudio: Bad GPR sig (%x)", sig);
+ return -EINVAL;
+ }
+ bs_base = ioread32(&a->reg_mem_gpr[2]);
+ a->bs = (struct aaudio_buffer_struct *) ((u8 *) a->reg_mem_bs + bs_base);
+ if (a->bs->signature != AAUDIO_SIG) {
+ dev_err(a->dev, "aaudio: Bad BufferStruct sig (%x)", a->bs->signature);
+ return -EINVAL;
+ }
+ dev_info(a->dev, "aaudio: BufferStruct ver = %i\n", a->bs->version);
+ dev_info(a->dev, "aaudio: Num devices = %i\n", a->bs->num_devices);
+ for (i = 0; i < a->bs->num_devices; i++) {
+ dev = &a->bs->devices[i];
+ dev_info(a->dev, "aaudio: Device %i %s\n", i, dev->name);
+
+ sdev = aaudio_find_dev_by_uid(a, dev->name);
+ if (!sdev) {
+ dev_err(a->dev, "aaudio: Subdevice not found for BufferStruct device %s\n", dev->name);
+ continue;
+ }
+ sdev->buf_id = (u8) i;
+ dev->num_input_streams = 0;
+ for (j = 0; j < dev->num_output_streams; j++) {
+ dev_info(a->dev, "aaudio: Device %i Stream %i: Output; Buffer Count = %i\n", i, j,
+ dev->output_streams[j].num_buffers);
+ if (j < sdev->out_stream_cnt)
+ aaudio_init_bs_stream(a, &sdev->out_streams[j], &dev->output_streams[j]);
+ }
+ }
+
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (sdev->buf_id != AAUDIO_BUFFER_ID_NONE)
+ continue;
+ sdev->buf_id = i;
+ dev_info(a->dev, "aaudio: Created device %i %s\n", i, sdev->uid);
+ strcpy(a->bs->devices[i].name, sdev->uid);
+ a->bs->devices[i].num_input_streams = 0;
+ a->bs->devices[i].num_output_streams = 0;
+ a->bs->num_devices = ++i;
+ }
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (sdev->in_stream_cnt == 1) {
+ dev_info(a->dev, "aaudio: Device %i Host Stream; Input\n", sdev->buf_id);
+ aaudio_init_bs_stream_host(a, &sdev->in_streams[0], &a->bs->devices[sdev->buf_id].input_streams[0]);
+ a->bs->devices[sdev->buf_id].num_input_streams = 1;
+ wmb();
+
+ if (aaudio_cmd_set_input_stream_address_ranges(a, sdev->dev_id)) {
+ dev_err(a->dev, "aaudio: Failed to set input stream address ranges\n");
+ }
+ }
+ }
+
+ return 0;
+}
+
+static void aaudio_init_bs_stream(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm)
+{
+ size_t i;
+ strm->buffer_cnt = bs_strm->num_buffers;
+ if (bs_strm->num_buffers > AAUDIO_DEIVCE_MAX_BUFFER_COUNT) {
+ dev_warn(a->dev, "BufferStruct buffer count %u exceeds driver limit of %u\n", bs_strm->num_buffers,
+ AAUDIO_DEIVCE_MAX_BUFFER_COUNT);
+ strm->buffer_cnt = AAUDIO_DEIVCE_MAX_BUFFER_COUNT;
+ }
+ if (!strm->buffer_cnt)
+ return;
+ strm->buffers = kmalloc_array(strm->buffer_cnt, sizeof(struct aaudio_dma_buf), GFP_KERNEL);
+ if (!strm->buffers) {
+ dev_err(a->dev, "Buffer list allocation failed\n");
+ return;
+ }
+ for (i = 0; i < strm->buffer_cnt; i++) {
+ strm->buffers[i].dma_addr = a->reg_mem_bs_dma + (dma_addr_t) bs_strm->buffers[i].address;
+ strm->buffers[i].ptr = a->reg_mem_bs + bs_strm->buffers[i].address;
+ strm->buffers[i].size = bs_strm->buffers[i].size;
+ }
+
+ if (strm->buffer_cnt == 1) {
+ strm->alsa_hw_desc = kmalloc(sizeof(struct snd_pcm_hardware), GFP_KERNEL);
+ if (aaudio_create_hw_info(&strm->desc, strm->alsa_hw_desc, strm->buffers[0].size)) {
+ kfree(strm->alsa_hw_desc);
+ strm->alsa_hw_desc = NULL;
+ }
+ }
+}
+
+static void aaudio_init_bs_stream_host(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm)
+{
+ size_t size;
+ dma_addr_t dma_addr;
+ void *dma_ptr;
+ size = strm->desc.bytes_per_packet * 16640;
+ dma_ptr = dma_alloc_coherent(&a->pci->dev, size, &dma_addr, GFP_KERNEL);
+ if (!dma_ptr) {
+ dev_err(a->dev, "dma_alloc_coherent failed\n");
+ return;
+ }
+ bs_strm->buffers[0].address = dma_addr;
+ bs_strm->buffers[0].size = size;
+ bs_strm->num_buffers = 1;
+
+ memset(dma_ptr, 0, size);
+
+ strm->buffer_cnt = 1;
+ strm->buffers = kmalloc_array(strm->buffer_cnt, sizeof(struct aaudio_dma_buf), GFP_KERNEL);
+ if (!strm->buffers) {
+ dev_err(a->dev, "Buffer list allocation failed\n");
+ return;
+ }
+ strm->buffers[0].dma_addr = dma_addr;
+ strm->buffers[0].ptr = dma_ptr;
+ strm->buffers[0].size = size;
+
+ strm->alsa_hw_desc = kmalloc(sizeof(struct snd_pcm_hardware), GFP_KERNEL);
+ if (aaudio_create_hw_info(&strm->desc, strm->alsa_hw_desc, strm->buffers[0].size)) {
+ kfree(strm->alsa_hw_desc);
+ strm->alsa_hw_desc = NULL;
+ }
+}
+
+static void aaudio_handle_prop_change(struct aaudio_device *a, struct aaudio_msg *msg);
+
+void aaudio_handle_notification(struct aaudio_device *a, struct aaudio_msg *msg)
+{
+ struct aaudio_send_ctx sctx;
+ struct aaudio_msg_base base;
+ if (aaudio_msg_read_base(msg, &base))
+ return;
+ switch (base.msg) {
+ case AAUDIO_MSG_NOTIFICATION_BOOT:
+ dev_info(a->dev, "Received boot notification from remote\n");
+
+ /* Resend the alive notify */
+ if (aaudio_send(a, &sctx, 500,
+ aaudio_msg_write_alive_notification, 1, 3)) {
+ pr_err("Sending alive notification failed\n");
+ }
+ break;
+ case AAUDIO_MSG_NOTIFICATION_ALIVE:
+ dev_info(a->dev, "Received alive notification from remote\n");
+ complete_all(&a->remote_alive);
+ break;
+ case AAUDIO_MSG_PROPERTY_CHANGED:
+ aaudio_handle_prop_change(a, msg);
+ break;
+ default:
+ dev_info(a->dev, "Unhandled notification %i", base.msg);
+ break;
+ }
+}
+
+struct aaudio_prop_change_work_struct {
+ struct work_struct ws;
+ struct aaudio_device *a;
+ aaudio_device_id_t dev;
+ aaudio_object_id_t obj;
+ struct aaudio_prop_addr prop;
+};
+
+static void aaudio_handle_jack_connection_change(struct aaudio_subdevice *sdev)
+{
+ u32 plugged;
+ if (!sdev->jack)
+ return;
+ /* NOTE: Apple made the plug status scoped to the input and output streams. This makes no sense for us, so I just
+ * always pick the OUTPUT status. */
+ if (aaudio_cmd_get_primitive_property(sdev->a, sdev->dev_id, sdev->dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_OUTPUT, AAUDIO_PROP_JACK_PLUGGED, 0), NULL, 0, &plugged, sizeof(plugged))) {
+ dev_err(sdev->a->dev, "Failed to get jack enable status\n");
+ return;
+ }
+ dev_dbg(sdev->a->dev, "Jack is now %s\n", plugged ? "plugged" : "unplugged");
+ snd_jack_report(sdev->jack, plugged ? sdev->jack->type : 0);
+}
+
+void aaudio_handle_prop_change_work(struct work_struct *ws)
+{
+ struct aaudio_prop_change_work_struct *work = container_of(ws, struct aaudio_prop_change_work_struct, ws);
+ struct aaudio_subdevice *sdev;
+
+ sdev = aaudio_find_dev_by_dev_id(work->a, work->dev);
+ if (!sdev) {
+ dev_err(work->a->dev, "Property notification change: device not found\n");
+ goto done;
+ }
+ dev_dbg(work->a->dev, "Property changed for device: %s\n", sdev->uid);
+
+ if (work->prop.scope == AAUDIO_PROP_SCOPE_OUTPUT && work->prop.selector == AAUDIO_PROP_JACK_PLUGGED) {
+ aaudio_handle_jack_connection_change(sdev);
+ }
+
+done:
+ kfree(work);
+}
+
+void aaudio_handle_prop_change(struct aaudio_device *a, struct aaudio_msg *msg)
+{
+ /* NOTE: This is a scheduled work because this callback will generally need to query device information and this
+ * is not possible when we are in the reply parsing code's context. */
+ struct aaudio_prop_change_work_struct *work;
+ work = kmalloc(sizeof(struct aaudio_prop_change_work_struct), GFP_KERNEL);
+ work->a = a;
+ INIT_WORK(&work->ws, aaudio_handle_prop_change_work);
+ aaudio_msg_read_property_changed(msg, &work->dev, &work->obj, &work->prop);
+ schedule_work(&work->ws);
+}
+
+#define aaudio_send_cmd_response(a, sctx, msg, fn, ...) \
+ if (aaudio_send_with_tag(a, sctx, ((struct aaudio_msg_header *) msg->data)->tag, 500, fn, ##__VA_ARGS__)) \
+ pr_err("aaudio: Failed to reply to a command\n");
+
+void aaudio_handle_cmd_timestamp(struct aaudio_device *a, struct aaudio_msg *msg)
+{
+ ktime_t time_os = ktime_get_boottime();
+ struct aaudio_send_ctx sctx;
+ struct aaudio_subdevice *sdev;
+ u64 devid, timestamp, update_seed;
+ aaudio_msg_read_update_timestamp(msg, &devid, ×tamp, &update_seed);
+ dev_dbg(a->dev, "Received timestamp update for dev=%llx ts=%llx seed=%llx\n", devid, timestamp, update_seed);
+
+ sdev = aaudio_find_dev_by_dev_id(a, devid);
+ aaudio_handle_timestamp(sdev, time_os, timestamp);
+
+ aaudio_send_cmd_response(a, &sctx, msg,
+ aaudio_msg_write_update_timestamp_response);
+}
+
+void aaudio_handle_command(struct aaudio_device *a, struct aaudio_msg *msg)
+{
+ struct aaudio_msg_base base;
+ if (aaudio_msg_read_base(msg, &base))
+ return;
+ switch (base.msg) {
+ case AAUDIO_MSG_UPDATE_TIMESTAMP:
+ aaudio_handle_cmd_timestamp(a, msg);
+ break;
+ default:
+ dev_info(a->dev, "Unhandled device command %i", base.msg);
+ break;
+ }
+}
+
+static struct pci_device_id aaudio_ids[ ] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x1803) },
+ { 0, },
+};
+
+struct dev_pm_ops aaudio_pci_driver_pm = {
+ .suspend = aaudio_suspend,
+ .resume = aaudio_resume
+};
+struct pci_driver aaudio_pci_driver = {
+ .name = "aaudio",
+ .id_table = aaudio_ids,
+ .probe = aaudio_probe,
+ .remove = aaudio_remove,
+ .driver = {
+ .pm = &aaudio_pci_driver_pm
+ }
+};
+
+
+int aaudio_module_init(void)
+{
+ int result;
+ if ((result = alloc_chrdev_region(&aaudio_chrdev, 0, 1, "aaudio")))
+ goto fail_chrdev;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0)
+ aaudio_class = class_create(THIS_MODULE, "aaudio");
+#else
+ aaudio_class = class_create("aaudio");
+#endif
+ if (IS_ERR(aaudio_class)) {
+ result = PTR_ERR(aaudio_class);
+ goto fail_class;
+ }
+
+ result = pci_register_driver(&aaudio_pci_driver);
+ if (result)
+ goto fail_drv;
+ return 0;
+
+fail_drv:
+ pci_unregister_driver(&aaudio_pci_driver);
+fail_class:
+ class_destroy(aaudio_class);
+fail_chrdev:
+ unregister_chrdev_region(aaudio_chrdev, 1);
+ if (!result)
+ result = -EINVAL;
+ return result;
+}
+
+void aaudio_module_exit(void)
+{
+ pci_unregister_driver(&aaudio_pci_driver);
+ class_destroy(aaudio_class);
+ unregister_chrdev_region(aaudio_chrdev, 1);
+}
+
+struct aaudio_alsa_pcm_id_mapping aaudio_alsa_id_mappings[] = {
+ {"Speaker", 0},
+ {"Digital Mic", 1},
+ {"Codec Output", 2},
+ {"Codec Input", 3},
+ {"Bridge Loopback", 4},
+ {}
+};
+
+module_param_named(index, aaudio_alsa_index, int, 0444);
+MODULE_PARM_DESC(index, "Index value for Apple Internal Audio soundcard.");
+module_param_named(id, aaudio_alsa_id, charp, 0444);
+MODULE_PARM_DESC(id, "ID string for Apple Internal Audio soundcard.");
diff --git a/drivers/staging/apple-bce/audio/audio.h b/drivers/staging/apple-bce/audio/audio.h
new file mode 100644
index 000000000..004bc1e22
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/audio.h
@@ -0,0 +1,125 @@
+#ifndef AAUDIO_H
+#define AAUDIO_H
+
+#include <linux/types.h>
+#include <sound/pcm.h>
+#include "../apple_bce.h"
+#include "protocol_bce.h"
+#include "description.h"
+
+#define AAUDIO_SIG 0x19870423
+
+#define AAUDIO_DEVICE_MAX_UID_LEN 128
+#define AAUDIO_DEIVCE_MAX_INPUT_STREAMS 1
+#define AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS 1
+#define AAUDIO_DEIVCE_MAX_BUFFER_COUNT 1
+
+#define AAUDIO_BUFFER_ID_NONE 0xffu
+
+struct snd_card;
+struct snd_pcm;
+struct snd_pcm_hardware;
+struct snd_jack;
+
+struct __attribute__((packed)) __attribute__((aligned(4))) aaudio_buffer_struct_buffer {
+ size_t address;
+ size_t size;
+ size_t pad[4];
+};
+struct aaudio_buffer_struct_stream {
+ u8 num_buffers;
+ struct aaudio_buffer_struct_buffer buffers[100];
+ char filler[32];
+};
+struct aaudio_buffer_struct_device {
+ char name[128];
+ u8 num_input_streams;
+ u8 num_output_streams;
+ struct aaudio_buffer_struct_stream input_streams[5];
+ struct aaudio_buffer_struct_stream output_streams[5];
+ char filler[128];
+};
+struct aaudio_buffer_struct {
+ u32 version;
+ u32 signature;
+ u32 flags;
+ u8 num_devices;
+ struct aaudio_buffer_struct_device devices[20];
+};
+
+struct aaudio_device;
+struct aaudio_dma_buf {
+ dma_addr_t dma_addr;
+ void *ptr;
+ size_t size;
+};
+struct aaudio_stream {
+ aaudio_object_id_t id;
+ size_t buffer_cnt;
+ struct aaudio_dma_buf *buffers;
+
+ struct aaudio_apple_description desc;
+ struct snd_pcm_hardware *alsa_hw_desc;
+ u32 latency;
+
+ bool waiting_for_first_ts;
+
+ ktime_t remote_timestamp;
+ snd_pcm_sframes_t frame_min;
+ int started;
+};
+struct aaudio_subdevice {
+ struct aaudio_device *a;
+ struct list_head list;
+ aaudio_device_id_t dev_id;
+ u32 in_latency, out_latency;
+ u8 buf_id;
+ int alsa_id;
+ char uid[AAUDIO_DEVICE_MAX_UID_LEN + 1];
+ size_t in_stream_cnt;
+ struct aaudio_stream in_streams[AAUDIO_DEIVCE_MAX_INPUT_STREAMS];
+ size_t out_stream_cnt;
+ struct aaudio_stream out_streams[AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS];
+ bool is_pcm;
+ struct snd_pcm *pcm;
+ struct snd_jack *jack;
+};
+struct aaudio_alsa_pcm_id_mapping {
+ const char *name;
+ int alsa_id;
+};
+
+struct aaudio_device {
+ struct pci_dev *pci;
+ dev_t devt;
+ struct device *dev;
+ void __iomem *reg_mem_bs;
+ dma_addr_t reg_mem_bs_dma;
+ void __iomem *reg_mem_cfg;
+
+ u32 __iomem *reg_mem_gpr;
+
+ struct aaudio_buffer_struct *bs;
+
+ struct apple_bce_device *bce;
+ struct aaudio_bce bcem;
+
+ struct snd_card *card;
+
+ struct list_head subdevice_list;
+ int next_alsa_id;
+
+ struct completion remote_alive;
+};
+
+void aaudio_handle_notification(struct aaudio_device *a, struct aaudio_msg *msg);
+void aaudio_handle_prop_change_work(struct work_struct *ws);
+void aaudio_handle_cmd_timestamp(struct aaudio_device *a, struct aaudio_msg *msg);
+void aaudio_handle_command(struct aaudio_device *a, struct aaudio_msg *msg);
+
+int aaudio_module_init(void);
+void aaudio_module_exit(void);
+
+extern struct aaudio_alsa_pcm_id_mapping aaudio_alsa_id_mappings[];
+
+#endif //AAUDIO_H
diff --git a/drivers/staging/apple-bce/audio/description.h b/drivers/staging/apple-bce/audio/description.h
new file mode 100644
index 000000000..dfef3ab68
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/description.h
@@ -0,0 +1,42 @@
+#ifndef AAUDIO_DESCRIPTION_H
+#define AAUDIO_DESCRIPTION_H
+
+#include <linux/types.h>
+
+struct aaudio_apple_description {
+ u64 sample_rate_double;
+ u32 format_id;
+ u32 format_flags;
+ u32 bytes_per_packet;
+ u32 frames_per_packet;
+ u32 bytes_per_frame;
+ u32 channels_per_frame;
+ u32 bits_per_channel;
+ u32 reserved;
+};
+
+enum {
+ AAUDIO_FORMAT_LPCM = 0x6c70636d // 'lpcm'
+};
+
+enum {
+ AAUDIO_FORMAT_FLAG_FLOAT = 1,
+ AAUDIO_FORMAT_FLAG_BIG_ENDIAN = 2,
+ AAUDIO_FORMAT_FLAG_SIGNED = 4,
+ AAUDIO_FORMAT_FLAG_PACKED = 8,
+ AAUDIO_FORMAT_FLAG_ALIGNED_HIGH = 16,
+ AAUDIO_FORMAT_FLAG_NON_INTERLEAVED = 32,
+ AAUDIO_FORMAT_FLAG_NON_MIXABLE = 64
+};
+
+static inline u64 aaudio_double_to_u64(u64 d)
+{
+ u8 sign = (u8) ((d >> 63) & 1);
+ s32 exp = (s32) ((d >> 52) & 0x7ff) - 1023;
+ u64 fr = d & ((1LL << 52) - 1);
+ if (sign || exp < 0)
+ return 0;
+ return (u64) ((1LL << exp) + (fr >> (52 - exp)));
+}
+
+#endif //AAUDIO_DESCRIPTION_H
diff --git a/drivers/staging/apple-bce/audio/pcm.c b/drivers/staging/apple-bce/audio/pcm.c
new file mode 100644
index 000000000..1026e10a9
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/pcm.c
@@ -0,0 +1,308 @@
+#include "pcm.h"
+#include "audio.h"
+
+static u64 aaudio_get_alsa_fmtbit(struct aaudio_apple_description *desc)
+{
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_FLOAT) {
+ if (desc->bits_per_channel == 32) {
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_BIG_ENDIAN)
+ return SNDRV_PCM_FMTBIT_FLOAT_BE;
+ else
+ return SNDRV_PCM_FMTBIT_FLOAT_LE;
+ } else if (desc->bits_per_channel == 64) {
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_BIG_ENDIAN)
+ return SNDRV_PCM_FMTBIT_FLOAT64_BE;
+ else
+ return SNDRV_PCM_FMTBIT_FLOAT64_LE;
+ } else {
+ pr_err("aaudio: unsupported bits per channel for float format: %u\n", desc->bits_per_channel);
+ return 0;
+ }
+ }
+#define DEFINE_BPC_OPTION(val, b) \
+ case val: \
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_BIG_ENDIAN) { \
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_SIGNED) \
+ return SNDRV_PCM_FMTBIT_S ## b ## BE; \
+ else \
+ return SNDRV_PCM_FMTBIT_U ## b ## BE; \
+ } else { \
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_SIGNED) \
+ return SNDRV_PCM_FMTBIT_S ## b ## LE; \
+ else \
+ return SNDRV_PCM_FMTBIT_U ## b ## LE; \
+ }
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_PACKED) {
+ switch (desc->bits_per_channel) {
+ case 8:
+ case 16:
+ case 32:
+ break;
+ DEFINE_BPC_OPTION(24, 24_3)
+ default:
+ pr_err("aaudio: unsupported bits per channel for packed format: %u\n", desc->bits_per_channel);
+ return 0;
+ }
+ }
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_ALIGNED_HIGH) {
+ switch (desc->bits_per_channel) {
+ DEFINE_BPC_OPTION(24, 32_)
+ default:
+ pr_err("aaudio: unsupported bits per channel for high-aligned format: %u\n", desc->bits_per_channel);
+ return 0;
+ }
+ }
+ switch (desc->bits_per_channel) {
+ case 8:
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_SIGNED)
+ return SNDRV_PCM_FMTBIT_S8;
+ else
+ return SNDRV_PCM_FMTBIT_U8;
+ DEFINE_BPC_OPTION(16, 16_)
+ DEFINE_BPC_OPTION(24, 24_)
+ DEFINE_BPC_OPTION(32, 32_)
+ default:
+ pr_err("aaudio: unsupported bits per channel: %u\n", desc->bits_per_channel);
+ return 0;
+ }
+}
+int aaudio_create_hw_info(struct aaudio_apple_description *desc, struct snd_pcm_hardware *alsa_hw,
+ size_t buf_size)
+{
+ uint rate;
+ alsa_hw->info = (SNDRV_PCM_INFO_MMAP |
+ SNDRV_PCM_INFO_BLOCK_TRANSFER |
+ SNDRV_PCM_INFO_MMAP_VALID |
+ SNDRV_PCM_INFO_DOUBLE);
+ if (desc->format_flags & AAUDIO_FORMAT_FLAG_NON_MIXABLE)
+ pr_warn("aaudio: unsupported hw flag: NON_MIXABLE\n");
+ if (!(desc->format_flags & AAUDIO_FORMAT_FLAG_NON_INTERLEAVED))
+ alsa_hw->info |= SNDRV_PCM_INFO_INTERLEAVED;
+ alsa_hw->formats = aaudio_get_alsa_fmtbit(desc);
+ if (!alsa_hw->formats)
+ return -EINVAL;
+ rate = (uint) aaudio_double_to_u64(desc->sample_rate_double);
+ alsa_hw->rates = snd_pcm_rate_to_rate_bit(rate);
+ alsa_hw->rate_min = rate;
+ alsa_hw->rate_max = rate;
+ alsa_hw->channels_min = desc->channels_per_frame;
+ alsa_hw->channels_max = desc->channels_per_frame;
+ alsa_hw->buffer_bytes_max = buf_size;
+ alsa_hw->period_bytes_min = desc->bytes_per_packet;
+ alsa_hw->period_bytes_max = desc->bytes_per_packet;
+ alsa_hw->periods_min = (uint) (buf_size / desc->bytes_per_packet);
+ alsa_hw->periods_max = (uint) (buf_size / desc->bytes_per_packet);
+ pr_debug("aaudio_create_hw_info: format = %llu, rate = %u/%u. channels = %u, periods = %u, period size = %lu\n",
+ alsa_hw->formats, alsa_hw->rate_min, alsa_hw->rates, alsa_hw->channels_min, alsa_hw->periods_min,
+ alsa_hw->period_bytes_min);
+ return 0;
+}
+
+static struct aaudio_stream *aaudio_pcm_stream(struct snd_pcm_substream *substream)
+{
+ struct aaudio_subdevice *sdev = snd_pcm_substream_chip(substream);
+ if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+ return &sdev->out_streams[substream->number];
+ else
+ return &sdev->in_streams[substream->number];
+}
+
+static int aaudio_pcm_open(struct snd_pcm_substream *substream)
+{
+ pr_debug("aaudio_pcm_open\n");
+ substream->runtime->hw = *aaudio_pcm_stream(substream)->alsa_hw_desc;
+
+ return 0;
+}
+
+static int aaudio_pcm_close(struct snd_pcm_substream *substream)
+{
+ pr_debug("aaudio_pcm_close\n");
+ return 0;
+}
+
+static int aaudio_pcm_prepare(struct snd_pcm_substream *substream)
+{
+ return 0;
+}
+
+static int aaudio_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params)
+{
+ struct aaudio_stream *astream = aaudio_pcm_stream(substream);
+ pr_debug("aaudio_pcm_hw_params\n");
+
+ if (!astream->buffer_cnt || !astream->buffers)
+ return -EINVAL;
+
+ substream->runtime->dma_area = astream->buffers[0].ptr;
+ substream->runtime->dma_addr = astream->buffers[0].dma_addr;
+ substream->runtime->dma_bytes = astream->buffers[0].size;
+ return 0;
+}
+
+static int aaudio_pcm_hw_free(struct snd_pcm_substream *substream)
+{
+ pr_debug("aaudio_pcm_hw_free\n");
+ return 0;
+}
+
+static void aaudio_pcm_start(struct snd_pcm_substream *substream)
+{
+ struct aaudio_subdevice *sdev = snd_pcm_substream_chip(substream);
+ struct aaudio_stream *stream = aaudio_pcm_stream(substream);
+ void *buf;
+ size_t s;
+ ktime_t time_start, time_end;
+ bool back_buffer;
+ time_start = ktime_get();
+
+ back_buffer = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
+
+ if (back_buffer) {
+ s = frames_to_bytes(substream->runtime, substream->runtime->control->appl_ptr);
+ buf = kmalloc(s, GFP_KERNEL);
+ memcpy_fromio(buf, substream->runtime->dma_area, s);
+ time_end = ktime_get();
+ pr_debug("aaudio: Backed up the buffer in %lluns [%li]\n", ktime_to_ns(time_end - time_start),
+ substream->runtime->control->appl_ptr);
+ }
+
+ stream->waiting_for_first_ts = true;
+ stream->frame_min = stream->latency;
+
+ aaudio_cmd_start_io(sdev->a, sdev->dev_id);
+ if (back_buffer)
+ memcpy_toio(substream->runtime->dma_area, buf, s);
+
+ time_end = ktime_get();
+ pr_debug("aaudio: Started the audio device in %lluns\n", ktime_to_ns(time_end - time_start));
+}
+
+static int aaudio_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
+{
+ struct aaudio_subdevice *sdev = snd_pcm_substream_chip(substream);
+ struct aaudio_stream *stream = aaudio_pcm_stream(substream);
+ pr_debug("aaudio_pcm_trigger %x\n", cmd);
+
+ /* We only supports triggers on the #0 buffer */
+ if (substream->number != 0)
+ return 0;
+ switch (cmd) {
+ case SNDRV_PCM_TRIGGER_START:
+ aaudio_pcm_start(substream);
+ stream->started = 1;
+ break;
+ case SNDRV_PCM_TRIGGER_STOP:
+ aaudio_cmd_stop_io(sdev->a, sdev->dev_id);
+ stream->started = 0;
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static snd_pcm_uframes_t aaudio_pcm_pointer(struct snd_pcm_substream *substream)
+{
+ struct aaudio_stream *stream = aaudio_pcm_stream(substream);
+ ktime_t time_from_start;
+ snd_pcm_sframes_t frames;
+ snd_pcm_sframes_t buffer_time_length;
+
+ if (!stream->started || stream->waiting_for_first_ts) {
+ pr_warn("aaudio_pcm_pointer while not started\n");
+ return 0;
+ }
+
+ /* Approximate the pointer based on the last received timestamp */
+ time_from_start = ktime_get_boottime() - stream->remote_timestamp;
+ buffer_time_length = NSEC_PER_SEC * substream->runtime->buffer_size / substream->runtime->rate;
+ frames = (ktime_to_ns(time_from_start) % buffer_time_length) * substream->runtime->buffer_size / buffer_time_length;
+ if (ktime_to_ns(time_from_start) < buffer_time_length) {
+ if (frames < stream->frame_min)
+ frames = stream->frame_min;
+ else
+ stream->frame_min = 0;
+ } else {
+ if (ktime_to_ns(time_from_start) < 2 * buffer_time_length)
+ stream->frame_min = frames;
+ else
+ stream->frame_min = 0; /* Heavy desync */
+ }
+ frames -= stream->latency;
+ if (frames < 0)
+ frames += ((-frames - 1) / substream->runtime->buffer_size + 1) * substream->runtime->buffer_size;
+ return (snd_pcm_uframes_t) frames;
+}
+
+static struct snd_pcm_ops aaudio_pcm_ops = {
+ .open = aaudio_pcm_open,
+ .close = aaudio_pcm_close,
+ .ioctl = snd_pcm_lib_ioctl,
+ .hw_params = aaudio_pcm_hw_params,
+ .hw_free = aaudio_pcm_hw_free,
+ .prepare = aaudio_pcm_prepare,
+ .trigger = aaudio_pcm_trigger,
+ .pointer = aaudio_pcm_pointer,
+ .mmap = snd_pcm_lib_mmap_iomem
+};
+
+int aaudio_create_pcm(struct aaudio_subdevice *sdev)
+{
+ struct snd_pcm *pcm;
+ struct aaudio_alsa_pcm_id_mapping *id_mapping;
+ int err;
+
+ if (!sdev->is_pcm || (sdev->in_stream_cnt == 0 && sdev->out_stream_cnt == 0)) {
+ return -EINVAL;
+ }
+
+ for (id_mapping = aaudio_alsa_id_mappings; id_mapping->name; id_mapping++) {
+ if (!strcmp(sdev->uid, id_mapping->name)) {
+ sdev->alsa_id = id_mapping->alsa_id;
+ break;
+ }
+ }
+ if (!id_mapping->name)
+ sdev->alsa_id = sdev->a->next_alsa_id++;
+ err = snd_pcm_new(sdev->a->card, sdev->uid, sdev->alsa_id,
+ (int) sdev->out_stream_cnt, (int) sdev->in_stream_cnt, &pcm);
+ if (err < 0)
+ return err;
+ pcm->private_data = sdev;
+ pcm->nonatomic = 1;
+ sdev->pcm = pcm;
+ strcpy(pcm->name, sdev->uid);
+ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &aaudio_pcm_ops);
+ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &aaudio_pcm_ops);
+ return 0;
+}
+
+static void aaudio_handle_stream_timestamp(struct snd_pcm_substream *substream, ktime_t timestamp)
+{
+ unsigned long flags;
+ struct aaudio_stream *stream;
+
+ stream = aaudio_pcm_stream(substream);
+ snd_pcm_stream_lock_irqsave(substream, flags);
+ stream->remote_timestamp = timestamp;
+ if (stream->waiting_for_first_ts) {
+ stream->waiting_for_first_ts = false;
+ snd_pcm_stream_unlock_irqrestore(substream, flags);
+ return;
+ }
+ snd_pcm_stream_unlock_irqrestore(substream, flags);
+ snd_pcm_period_elapsed(substream);
+}
+
+void aaudio_handle_timestamp(struct aaudio_subdevice *sdev, ktime_t os_timestamp, u64 dev_timestamp)
+{
+ struct snd_pcm_substream *substream;
+
+ substream = sdev->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
+ if (substream)
+ aaudio_handle_stream_timestamp(substream, dev_timestamp);
+ substream = sdev->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
+ if (substream)
+ aaudio_handle_stream_timestamp(substream, os_timestamp);
+}
diff --git a/drivers/staging/apple-bce/audio/pcm.h b/drivers/staging/apple-bce/audio/pcm.h
new file mode 100644
index 000000000..ea5f35fbe
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/pcm.h
@@ -0,0 +1,16 @@
+#ifndef AAUDIO_PCM_H
+#define AAUDIO_PCM_H
+
+#include <linux/types.h>
+#include <linux/ktime.h>
+
+struct aaudio_subdevice;
+struct aaudio_apple_description;
+struct snd_pcm_hardware;
+
+int aaudio_create_hw_info(struct aaudio_apple_description *desc, struct snd_pcm_hardware *alsa_hw, size_t buf_size);
+int aaudio_create_pcm(struct aaudio_subdevice *sdev);
+
+void aaudio_handle_timestamp(struct aaudio_subdevice *sdev, ktime_t os_timestamp, u64 dev_timestamp);
+
+#endif //AAUDIO_PCM_H
diff --git a/drivers/staging/apple-bce/audio/protocol.c b/drivers/staging/apple-bce/audio/protocol.c
new file mode 100644
index 000000000..2314813ae
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/protocol.c
@@ -0,0 +1,347 @@
+#include "protocol.h"
+#include "protocol_bce.h"
+#include "audio.h"
+
+int aaudio_msg_read_base(struct aaudio_msg *msg, struct aaudio_msg_base *base)
+{
+ if (msg->size < sizeof(struct aaudio_msg_header) + sizeof(struct aaudio_msg_base) * 2)
+ return -EINVAL;
+ *base = *((struct aaudio_msg_base *) ((struct aaudio_msg_header *) msg->data + 1));
+ return 0;
+}
+
+#define READ_START(type) \
+ size_t offset = sizeof(struct aaudio_msg_header) + sizeof(struct aaudio_msg_base); (void)offset; \
+ if (((struct aaudio_msg_base *) ((struct aaudio_msg_header *) msg->data + 1))->msg != type) \
+ return -EINVAL;
+#define READ_DEVID_VAR(devid) *devid = ((struct aaudio_msg_header *) msg->data)->device_id
+#define READ_VAL(type) ({ offset += sizeof(type); *((type *) ((u8 *) msg->data + offset - sizeof(type))); })
+#define READ_VAR(type, var) *var = READ_VAL(type)
+
+int aaudio_msg_read_start_io_response(struct aaudio_msg *msg)
+{
+ READ_START(AAUDIO_MSG_START_IO_RESPONSE);
+ return 0;
+}
+
+int aaudio_msg_read_stop_io_response(struct aaudio_msg *msg)
+{
+ READ_START(AAUDIO_MSG_STOP_IO_RESPONSE);
+ return 0;
+}
+
+int aaudio_msg_read_update_timestamp(struct aaudio_msg *msg, aaudio_device_id_t *devid,
+ u64 *timestamp, u64 *update_seed)
+{
+ READ_START(AAUDIO_MSG_UPDATE_TIMESTAMP);
+ READ_DEVID_VAR(devid);
+ READ_VAR(u64, timestamp);
+ READ_VAR(u64, update_seed);
+ return 0;
+}
+
+int aaudio_msg_read_get_property_response(struct aaudio_msg *msg, aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop, void **data, u64 *data_size)
+{
+ READ_START(AAUDIO_MSG_GET_PROPERTY_RESPONSE);
+ READ_VAR(aaudio_object_id_t, obj);
+ READ_VAR(u32, &prop->element);
+ READ_VAR(u32, &prop->scope);
+ READ_VAR(u32, &prop->selector);
+ READ_VAR(u64, data_size);
+ *data = ((u8 *) msg->data + offset);
+ /* offset += data_size; */
+ return 0;
+}
+
+int aaudio_msg_read_set_property_response(struct aaudio_msg *msg, aaudio_object_id_t *obj)
+{
+ READ_START(AAUDIO_MSG_SET_PROPERTY_RESPONSE);
+ READ_VAR(aaudio_object_id_t, obj);
+ return 0;
+}
+
+int aaudio_msg_read_property_listener_response(struct aaudio_msg *msg, aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop)
+{
+ READ_START(AAUDIO_MSG_PROPERTY_LISTENER_RESPONSE);
+ READ_VAR(aaudio_object_id_t, obj);
+ READ_VAR(u32, &prop->element);
+ READ_VAR(u32, &prop->scope);
+ READ_VAR(u32, &prop->selector);
+ return 0;
+}
+
+int aaudio_msg_read_property_changed(struct aaudio_msg *msg, aaudio_device_id_t *devid, aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop)
+{
+ READ_START(AAUDIO_MSG_PROPERTY_CHANGED);
+ READ_DEVID_VAR(devid);
+ READ_VAR(aaudio_object_id_t, obj);
+ READ_VAR(u32, &prop->element);
+ READ_VAR(u32, &prop->scope);
+ READ_VAR(u32, &prop->selector);
+ return 0;
+}
+
+int aaudio_msg_read_set_input_stream_address_ranges_response(struct aaudio_msg *msg)
+{
+ READ_START(AAUDIO_MSG_SET_INPUT_STREAM_ADDRESS_RANGES_RESPONSE);
+ return 0;
+}
+
+int aaudio_msg_read_get_input_stream_list_response(struct aaudio_msg *msg, aaudio_object_id_t **str_l, u64 *str_cnt)
+{
+ READ_START(AAUDIO_MSG_GET_INPUT_STREAM_LIST_RESPONSE);
+ READ_VAR(u64, str_cnt);
+ *str_l = (aaudio_device_id_t *) ((u8 *) msg->data + offset);
+ /* offset += str_cnt * sizeof(aaudio_object_id_t); */
+ return 0;
+}
+
+int aaudio_msg_read_get_output_stream_list_response(struct aaudio_msg *msg, aaudio_object_id_t **str_l, u64 *str_cnt)
+{
+ READ_START(AAUDIO_MSG_GET_OUTPUT_STREAM_LIST_RESPONSE);
+ READ_VAR(u64, str_cnt);
+ *str_l = (aaudio_device_id_t *) ((u8 *) msg->data + offset);
+ /* offset += str_cnt * sizeof(aaudio_object_id_t); */
+ return 0;
+}
+
+int aaudio_msg_read_set_remote_access_response(struct aaudio_msg *msg)
+{
+ READ_START(AAUDIO_MSG_SET_REMOTE_ACCESS_RESPONSE);
+ return 0;
+}
+
+int aaudio_msg_read_get_device_list_response(struct aaudio_msg *msg, aaudio_device_id_t **dev_l, u64 *dev_cnt)
+{
+ READ_START(AAUDIO_MSG_GET_DEVICE_LIST_RESPONSE);
+ READ_VAR(u64, dev_cnt);
+ *dev_l = (aaudio_device_id_t *) ((u8 *) msg->data + offset);
+ /* offset += dev_cnt * sizeof(aaudio_device_id_t); */
+ return 0;
+}
+
+#define WRITE_START_OF_TYPE(typev, devid) \
+ size_t offset = sizeof(struct aaudio_msg_header); (void) offset; \
+ ((struct aaudio_msg_header *) msg->data)->type = (typev); \
+ ((struct aaudio_msg_header *) msg->data)->device_id = (devid);
+#define WRITE_START_COMMAND(devid) WRITE_START_OF_TYPE(AAUDIO_MSG_TYPE_COMMAND, devid)
+#define WRITE_START_RESPONSE() WRITE_START_OF_TYPE(AAUDIO_MSG_TYPE_RESPONSE, 0)
+#define WRITE_START_NOTIFICATION() WRITE_START_OF_TYPE(AAUDIO_MSG_TYPE_NOTIFICATION, 0)
+#define WRITE_VAL(type, value) { *((type *) ((u8 *) msg->data + offset)) = value; offset += sizeof(value); }
+#define WRITE_BIN(value, size) { memcpy((u8 *) msg->data + offset, value, size); offset += size; }
+#define WRITE_BASE(type) WRITE_VAL(u32, type) WRITE_VAL(u32, 0)
+#define WRITE_END() { msg->size = offset; }
+
+void aaudio_msg_write_start_io(struct aaudio_msg *msg, aaudio_device_id_t dev)
+{
+ WRITE_START_COMMAND(dev);
+ WRITE_BASE(AAUDIO_MSG_START_IO);
+ WRITE_END();
+}
+
+void aaudio_msg_write_stop_io(struct aaudio_msg *msg, aaudio_device_id_t dev)
+{
+ WRITE_START_COMMAND(dev);
+ WRITE_BASE(AAUDIO_MSG_STOP_IO);
+ WRITE_END();
+}
+
+void aaudio_msg_write_get_property(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size)
+{
+ WRITE_START_COMMAND(dev);
+ WRITE_BASE(AAUDIO_MSG_GET_PROPERTY);
+ WRITE_VAL(aaudio_object_id_t, obj);
+ WRITE_VAL(u32, prop.element);
+ WRITE_VAL(u32, prop.scope);
+ WRITE_VAL(u32, prop.selector);
+ WRITE_VAL(u64, qualifier_size);
+ WRITE_BIN(qualifier, qualifier_size);
+ WRITE_END();
+}
+
+void aaudio_msg_write_set_property(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *data, u64 data_size, void *qualifier, u64 qualifier_size)
+{
+ WRITE_START_COMMAND(dev);
+ WRITE_BASE(AAUDIO_MSG_SET_PROPERTY);
+ WRITE_VAL(aaudio_object_id_t, obj);
+ WRITE_VAL(u32, prop.element);
+ WRITE_VAL(u32, prop.scope);
+ WRITE_VAL(u32, prop.selector);
+ WRITE_VAL(u64, data_size);
+ WRITE_BIN(data, data_size);
+ WRITE_VAL(u64, qualifier_size);
+ WRITE_BIN(qualifier, qualifier_size);
+ WRITE_END();
+}
+
+void aaudio_msg_write_property_listener(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop)
+{
+ WRITE_START_COMMAND(dev);
+ WRITE_BASE(AAUDIO_MSG_PROPERTY_LISTENER);
+ WRITE_VAL(aaudio_object_id_t, obj);
+ WRITE_VAL(u32, prop.element);
+ WRITE_VAL(u32, prop.scope);
+ WRITE_VAL(u32, prop.selector);
+ WRITE_END();
+}
+
+void aaudio_msg_write_set_input_stream_address_ranges(struct aaudio_msg *msg, aaudio_device_id_t devid)
+{
+ WRITE_START_COMMAND(devid);
+ WRITE_BASE(AAUDIO_MSG_SET_INPUT_STREAM_ADDRESS_RANGES);
+ WRITE_END();
+}
+
+void aaudio_msg_write_get_input_stream_list(struct aaudio_msg *msg, aaudio_device_id_t devid)
+{
+ WRITE_START_COMMAND(devid);
+ WRITE_BASE(AAUDIO_MSG_GET_INPUT_STREAM_LIST);
+ WRITE_END();
+}
+
+void aaudio_msg_write_get_output_stream_list(struct aaudio_msg *msg, aaudio_device_id_t devid)
+{
+ WRITE_START_COMMAND(devid);
+ WRITE_BASE(AAUDIO_MSG_GET_OUTPUT_STREAM_LIST);
+ WRITE_END();
+}
+
+void aaudio_msg_write_set_remote_access(struct aaudio_msg *msg, u64 mode)
+{
+ WRITE_START_COMMAND(0);
+ WRITE_BASE(AAUDIO_MSG_SET_REMOTE_ACCESS);
+ WRITE_VAL(u64, mode);
+ WRITE_END();
+}
+
+void aaudio_msg_write_alive_notification(struct aaudio_msg *msg, u32 proto_ver, u32 msg_ver)
+{
+ WRITE_START_NOTIFICATION();
+ WRITE_BASE(AAUDIO_MSG_NOTIFICATION_ALIVE);
+ WRITE_VAL(u32, proto_ver);
+ WRITE_VAL(u32, msg_ver);
+ WRITE_END();
+}
+
+void aaudio_msg_write_update_timestamp_response(struct aaudio_msg *msg)
+{
+ WRITE_START_RESPONSE();
+ WRITE_BASE(AAUDIO_MSG_UPDATE_TIMESTAMP_RESPONSE);
+ WRITE_END();
+}
+
+void aaudio_msg_write_get_device_list(struct aaudio_msg *msg)
+{
+ WRITE_START_COMMAND(0);
+ WRITE_BASE(AAUDIO_MSG_GET_DEVICE_LIST);
+ WRITE_END();
+}
+
+#define CMD_SHARED_VARS_NO_REPLY \
+ int status = 0; \
+ struct aaudio_send_ctx sctx;
+#define CMD_SHARED_VARS \
+ CMD_SHARED_VARS_NO_REPLY \
+ struct aaudio_msg reply = aaudio_reply_alloc(); \
+ struct aaudio_msg *buf = &reply;
+#define CMD_SEND_REQUEST(fn, ...) \
+ if ((status = aaudio_send_cmd_sync(a, &sctx, buf, 500, fn, ##__VA_ARGS__))) \
+ return status;
+#define CMD_DEF_SHARED_AND_SEND(fn, ...) \
+ CMD_SHARED_VARS \
+ CMD_SEND_REQUEST(fn, ##__VA_ARGS__);
+#define CMD_DEF_SHARED_NO_REPLY_AND_SEND(fn, ...) \
+ CMD_SHARED_VARS_NO_REPLY \
+ CMD_SEND_REQUEST(fn, ##__VA_ARGS__);
+#define CMD_HNDL_REPLY_NO_FREE(fn, ...) \
+ status = fn(buf, ##__VA_ARGS__); \
+ return status;
+#define CMD_HNDL_REPLY_AND_FREE(fn, ...) \
+ status = fn(buf, ##__VA_ARGS__); \
+ aaudio_reply_free(&reply); \
+ return status;
+
+int aaudio_cmd_start_io(struct aaudio_device *a, aaudio_device_id_t devid)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_start_io, devid);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_start_io_response);
+}
+int aaudio_cmd_stop_io(struct aaudio_device *a, aaudio_device_id_t devid)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_stop_io, devid);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_stop_io_response);
+}
+int aaudio_cmd_get_property(struct aaudio_device *a, struct aaudio_msg *buf,
+ aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void **data, u64 *data_size)
+{
+ CMD_DEF_SHARED_NO_REPLY_AND_SEND(aaudio_msg_write_get_property, devid, obj, prop, qualifier, qualifier_size);
+ CMD_HNDL_REPLY_NO_FREE(aaudio_msg_read_get_property_response, &obj, &prop, data, data_size);
+}
+int aaudio_cmd_get_primitive_property(struct aaudio_device *a,
+ aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void *data, u64 data_size)
+{
+ int status;
+ struct aaudio_msg reply = aaudio_reply_alloc();
+ void *r_data;
+ u64 r_data_size;
+ if ((status = aaudio_cmd_get_property(a, &reply, devid, obj, prop, qualifier, qualifier_size,
+ &r_data, &r_data_size)))
+ goto finish;
+ if (r_data_size != data_size) {
+ status = -EINVAL;
+ goto finish;
+ }
+ memcpy(data, r_data, data_size);
+finish:
+ aaudio_reply_free(&reply);
+ return status;
+}
+int aaudio_cmd_set_property(struct aaudio_device *a, aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void *data, u64 data_size)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_set_property, devid, obj, prop, data, data_size,
+ qualifier, qualifier_size);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_set_property_response, &obj);
+}
+int aaudio_cmd_property_listener(struct aaudio_device *a, aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_property_listener, devid, obj, prop);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_property_listener_response, &obj, &prop);
+}
+int aaudio_cmd_set_input_stream_address_ranges(struct aaudio_device *a, aaudio_device_id_t devid)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_set_input_stream_address_ranges, devid);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_set_input_stream_address_ranges_response);
+}
+int aaudio_cmd_get_input_stream_list(struct aaudio_device *a, struct aaudio_msg *buf, aaudio_device_id_t devid,
+ aaudio_object_id_t **str_l, u64 *str_cnt)
+{
+ CMD_DEF_SHARED_NO_REPLY_AND_SEND(aaudio_msg_write_get_input_stream_list, devid);
+ CMD_HNDL_REPLY_NO_FREE(aaudio_msg_read_get_input_stream_list_response, str_l, str_cnt);
+}
+int aaudio_cmd_get_output_stream_list(struct aaudio_device *a, struct aaudio_msg *buf, aaudio_device_id_t devid,
+ aaudio_object_id_t **str_l, u64 *str_cnt)
+{
+ CMD_DEF_SHARED_NO_REPLY_AND_SEND(aaudio_msg_write_get_output_stream_list, devid);
+ CMD_HNDL_REPLY_NO_FREE(aaudio_msg_read_get_output_stream_list_response, str_l, str_cnt);
+}
+int aaudio_cmd_set_remote_access(struct aaudio_device *a, u64 mode)
+{
+ CMD_DEF_SHARED_AND_SEND(aaudio_msg_write_set_remote_access, mode);
+ CMD_HNDL_REPLY_AND_FREE(aaudio_msg_read_set_remote_access_response);
+}
+int aaudio_cmd_get_device_list(struct aaudio_device *a, struct aaudio_msg *buf,
+ aaudio_device_id_t **dev_l, u64 *dev_cnt)
+{
+ CMD_DEF_SHARED_NO_REPLY_AND_SEND(aaudio_msg_write_get_device_list);
+ CMD_HNDL_REPLY_NO_FREE(aaudio_msg_read_get_device_list_response, dev_l, dev_cnt);
+}
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/audio/protocol.h b/drivers/staging/apple-bce/audio/protocol.h
new file mode 100644
index 000000000..3427486f3
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/protocol.h
@@ -0,0 +1,147 @@
+#ifndef AAUDIO_PROTOCOL_H
+#define AAUDIO_PROTOCOL_H
+
+#include <linux/types.h>
+
+struct aaudio_device;
+
+typedef u64 aaudio_device_id_t;
+typedef u64 aaudio_object_id_t;
+
+struct aaudio_msg {
+ void *data;
+ size_t size;
+};
+
+struct __attribute__((packed)) aaudio_msg_header {
+ char tag[4];
+ u8 type;
+ aaudio_device_id_t device_id; // Idk, use zero for commands?
+};
+struct __attribute__((packed)) aaudio_msg_base {
+ u32 msg;
+ u32 status;
+};
+
+struct aaudio_prop_addr {
+ u32 scope;
+ u32 selector;
+ u32 element;
+};
+#define AAUDIO_PROP(scope, sel, el) (struct aaudio_prop_addr) { scope, sel, el }
+
+enum {
+ AAUDIO_MSG_TYPE_COMMAND = 1,
+ AAUDIO_MSG_TYPE_RESPONSE = 2,
+ AAUDIO_MSG_TYPE_NOTIFICATION = 3
+};
+
+enum {
+ AAUDIO_MSG_START_IO = 0,
+ AAUDIO_MSG_START_IO_RESPONSE = 1,
+ AAUDIO_MSG_STOP_IO = 2,
+ AAUDIO_MSG_STOP_IO_RESPONSE = 3,
+ AAUDIO_MSG_UPDATE_TIMESTAMP = 4,
+ AAUDIO_MSG_GET_PROPERTY = 7,
+ AAUDIO_MSG_GET_PROPERTY_RESPONSE = 8,
+ AAUDIO_MSG_SET_PROPERTY = 9,
+ AAUDIO_MSG_SET_PROPERTY_RESPONSE = 10,
+ AAUDIO_MSG_PROPERTY_LISTENER = 11,
+ AAUDIO_MSG_PROPERTY_LISTENER_RESPONSE = 12,
+ AAUDIO_MSG_PROPERTY_CHANGED = 13,
+ AAUDIO_MSG_SET_INPUT_STREAM_ADDRESS_RANGES = 18,
+ AAUDIO_MSG_SET_INPUT_STREAM_ADDRESS_RANGES_RESPONSE = 19,
+ AAUDIO_MSG_GET_INPUT_STREAM_LIST = 24,
+ AAUDIO_MSG_GET_INPUT_STREAM_LIST_RESPONSE = 25,
+ AAUDIO_MSG_GET_OUTPUT_STREAM_LIST = 26,
+ AAUDIO_MSG_GET_OUTPUT_STREAM_LIST_RESPONSE = 27,
+ AAUDIO_MSG_SET_REMOTE_ACCESS = 32,
+ AAUDIO_MSG_SET_REMOTE_ACCESS_RESPONSE = 33,
+ AAUDIO_MSG_UPDATE_TIMESTAMP_RESPONSE = 34,
+
+ AAUDIO_MSG_NOTIFICATION_ALIVE = 100,
+ AAUDIO_MSG_GET_DEVICE_LIST = 101,
+ AAUDIO_MSG_GET_DEVICE_LIST_RESPONSE = 102,
+ AAUDIO_MSG_NOTIFICATION_BOOT = 104
+};
+
+enum {
+ AAUDIO_REMOTE_ACCESS_OFF = 0,
+ AAUDIO_REMOTE_ACCESS_ON = 2
+};
+
+enum {
+ AAUDIO_PROP_SCOPE_GLOBAL = 0x676c6f62, // 'glob'
+ AAUDIO_PROP_SCOPE_INPUT = 0x696e7074, // 'inpt'
+ AAUDIO_PROP_SCOPE_OUTPUT = 0x6f757470 // 'outp'
+};
+
+enum {
+ AAUDIO_PROP_UID = 0x75696420, // 'uid '
+ AAUDIO_PROP_BOOL_VALUE = 0x6263766c, // 'bcvl'
+ AAUDIO_PROP_JACK_PLUGGED = 0x6a61636b, // 'jack'
+ AAUDIO_PROP_SEL_VOLUME = 0x64656176, // 'deav'
+ AAUDIO_PROP_LATENCY = 0x6c746e63, // 'ltnc'
+ AAUDIO_PROP_PHYS_FORMAT = 0x70667420 // 'pft '
+};
+
+int aaudio_msg_read_base(struct aaudio_msg *msg, struct aaudio_msg_base *base);
+
+int aaudio_msg_read_start_io_response(struct aaudio_msg *msg);
+int aaudio_msg_read_stop_io_response(struct aaudio_msg *msg);
+int aaudio_msg_read_update_timestamp(struct aaudio_msg *msg, aaudio_device_id_t *devid,
+ u64 *timestamp, u64 *update_seed);
+int aaudio_msg_read_get_property_response(struct aaudio_msg *msg, aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop, void **data, u64 *data_size);
+int aaudio_msg_read_set_property_response(struct aaudio_msg *msg, aaudio_object_id_t *obj);
+int aaudio_msg_read_property_listener_response(struct aaudio_msg *msg,aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop);
+int aaudio_msg_read_property_changed(struct aaudio_msg *msg, aaudio_device_id_t *devid, aaudio_object_id_t *obj,
+ struct aaudio_prop_addr *prop);
+int aaudio_msg_read_set_input_stream_address_ranges_response(struct aaudio_msg *msg);
+int aaudio_msg_read_get_input_stream_list_response(struct aaudio_msg *msg, aaudio_object_id_t **str_l, u64 *str_cnt);
+int aaudio_msg_read_get_output_stream_list_response(struct aaudio_msg *msg, aaudio_object_id_t **str_l, u64 *str_cnt);
+int aaudio_msg_read_set_remote_access_response(struct aaudio_msg *msg);
+int aaudio_msg_read_get_device_list_response(struct aaudio_msg *msg, aaudio_device_id_t **dev_l, u64 *dev_cnt);
+
+void aaudio_msg_write_start_io(struct aaudio_msg *msg, aaudio_device_id_t dev);
+void aaudio_msg_write_stop_io(struct aaudio_msg *msg, aaudio_device_id_t dev);
+void aaudio_msg_write_get_property(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size);
+void aaudio_msg_write_set_property(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *data, u64 data_size, void *qualifier, u64 qualifier_size);
+void aaudio_msg_write_property_listener(struct aaudio_msg *msg, aaudio_device_id_t dev, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop);
+void aaudio_msg_write_set_input_stream_address_ranges(struct aaudio_msg *msg, aaudio_device_id_t devid);
+void aaudio_msg_write_get_input_stream_list(struct aaudio_msg *msg, aaudio_device_id_t devid);
+void aaudio_msg_write_get_output_stream_list(struct aaudio_msg *msg, aaudio_device_id_t devid);
+void aaudio_msg_write_set_remote_access(struct aaudio_msg *msg, u64 mode);
+void aaudio_msg_write_alive_notification(struct aaudio_msg *msg, u32 proto_ver, u32 msg_ver);
+void aaudio_msg_write_update_timestamp_response(struct aaudio_msg *msg);
+void aaudio_msg_write_get_device_list(struct aaudio_msg *msg);
+
+
+int aaudio_cmd_start_io(struct aaudio_device *a, aaudio_device_id_t devid);
+int aaudio_cmd_stop_io(struct aaudio_device *a, aaudio_device_id_t devid);
+int aaudio_cmd_get_property(struct aaudio_device *a, struct aaudio_msg *buf,
+ aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void **data, u64 *data_size);
+int aaudio_cmd_get_primitive_property(struct aaudio_device *a,
+ aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void *data, u64 data_size);
+int aaudio_cmd_set_property(struct aaudio_device *a, aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop, void *qualifier, u64 qualifier_size, void *data, u64 data_size);
+int aaudio_cmd_property_listener(struct aaudio_device *a, aaudio_device_id_t devid, aaudio_object_id_t obj,
+ struct aaudio_prop_addr prop);
+int aaudio_cmd_set_input_stream_address_ranges(struct aaudio_device *a, aaudio_device_id_t devid);
+int aaudio_cmd_get_input_stream_list(struct aaudio_device *a, struct aaudio_msg *buf, aaudio_device_id_t devid,
+ aaudio_object_id_t **str_l, u64 *str_cnt);
+int aaudio_cmd_get_output_stream_list(struct aaudio_device *a, struct aaudio_msg *buf, aaudio_device_id_t devid,
+ aaudio_object_id_t **str_l, u64 *str_cnt);
+int aaudio_cmd_set_remote_access(struct aaudio_device *a, u64 mode);
+int aaudio_cmd_get_device_list(struct aaudio_device *a, struct aaudio_msg *buf,
+ aaudio_device_id_t **dev_l, u64 *dev_cnt);
+
+
+
+#endif //AAUDIO_PROTOCOL_H
diff --git a/drivers/staging/apple-bce/audio/protocol_bce.c b/drivers/staging/apple-bce/audio/protocol_bce.c
new file mode 100644
index 000000000..28f2dfd44
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/protocol_bce.c
@@ -0,0 +1,226 @@
+#include "protocol_bce.h"
+
+#include "audio.h"
+
+static void aaudio_bce_out_queue_completion(struct bce_queue_sq *sq);
+static void aaudio_bce_in_queue_completion(struct bce_queue_sq *sq);
+static int aaudio_bce_queue_init(struct aaudio_device *dev, struct aaudio_bce_queue *q, const char *name, int direction,
+ bce_sq_completion cfn);
+void aaudio_bce_in_queue_submit_pending(struct aaudio_bce_queue *q, size_t count);
+
+int aaudio_bce_init(struct aaudio_device *dev)
+{
+ int status;
+ struct aaudio_bce *bce = &dev->bcem;
+ bce->cq = bce_create_cq(dev->bce, 0x80);
+ spin_lock_init(&bce->spinlock);
+ if (!bce->cq)
+ return -EINVAL;
+ if ((status = aaudio_bce_queue_init(dev, &bce->qout, "com.apple.BridgeAudio.IntelToARM", DMA_TO_DEVICE,
+ aaudio_bce_out_queue_completion))) {
+ return status;
+ }
+ if ((status = aaudio_bce_queue_init(dev, &bce->qin, "com.apple.BridgeAudio.ARMToIntel", DMA_FROM_DEVICE,
+ aaudio_bce_in_queue_completion))) {
+ return status;
+ }
+ aaudio_bce_in_queue_submit_pending(&bce->qin, bce->qin.el_count);
+ return 0;
+}
+
+int aaudio_bce_queue_init(struct aaudio_device *dev, struct aaudio_bce_queue *q, const char *name, int direction,
+ bce_sq_completion cfn)
+{
+ q->cq = dev->bcem.cq;
+ q->el_size = AAUDIO_BCE_QUEUE_ELEMENT_SIZE;
+ q->el_count = AAUDIO_BCE_QUEUE_ELEMENT_COUNT;
+ /* NOTE: The Apple impl uses 0x80 as the queue size, however we use 21 (in fact 20) to simplify the impl */
+ q->sq = bce_create_sq(dev->bce, q->cq, name, (u32) (q->el_count + 1), direction, cfn, dev);
+ if (!q->sq)
+ return -EINVAL;
+
+ q->data = dma_alloc_coherent(&dev->bce->pci->dev, q->el_size * q->el_count, &q->dma_addr, GFP_KERNEL);
+ if (!q->data) {
+ bce_destroy_sq(dev->bce, q->sq);
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static void aaudio_send_create_tag(struct aaudio_bce *b, int *tagn, char tag[4])
+{
+ char tag_zero[5];
+ b->tag_num = (b->tag_num + 1) % AAUDIO_BCE_QUEUE_TAG_COUNT;
+ *tagn = b->tag_num;
+ snprintf(tag_zero, 5, "S%03d", b->tag_num);
+ *((u32 *) tag) = *((u32 *) tag_zero);
+}
+
+int __aaudio_send_prepare(struct aaudio_bce *b, struct aaudio_send_ctx *ctx, char *tag)
+{
+ int status;
+ size_t index;
+ void *dptr;
+ struct aaudio_msg_header *header;
+ if ((status = bce_reserve_submission(b->qout.sq, &ctx->timeout)))
+ return status;
+ spin_lock_irqsave(&b->spinlock, ctx->irq_flags);
+ index = b->qout.data_tail;
+ dptr = (u8 *) b->qout.data + index * b->qout.el_size;
+ ctx->msg.data = dptr;
+ header = dptr;
+ if (tag)
+ *((u32 *) header->tag) = *((u32 *) tag);
+ else
+ aaudio_send_create_tag(b, &ctx->tag_n, header->tag);
+ return 0;
+}
+
+void __aaudio_send(struct aaudio_bce *b, struct aaudio_send_ctx *ctx)
+{
+ struct bce_qe_submission *s = bce_next_submission(b->qout.sq);
+#ifdef DEBUG
+ pr_debug("aaudio: Sending command data\n");
+ print_hex_dump(KERN_DEBUG, "aaudio:OUT ", DUMP_PREFIX_NONE, 32, 1, ctx->msg.data, ctx->msg.size, true);
+#endif
+ bce_set_submission_single(s, b->qout.dma_addr + (dma_addr_t) (ctx->msg.data - b->qout.data), ctx->msg.size);
+ bce_submit_to_device(b->qout.sq);
+ b->qout.data_tail = (b->qout.data_tail + 1) % b->qout.el_count;
+ spin_unlock_irqrestore(&b->spinlock, ctx->irq_flags);
+}
+
+int __aaudio_send_cmd_sync(struct aaudio_bce *b, struct aaudio_send_ctx *ctx, struct aaudio_msg *reply)
+{
+ struct aaudio_bce_queue_entry ent;
+ DECLARE_COMPLETION_ONSTACK(cmpl);
+ ent.msg = reply;
+ ent.cmpl = &cmpl;
+ b->pending_entries[ctx->tag_n] = &ent;
+ __aaudio_send(b, ctx); /* unlocks the spinlock */
+ ctx->timeout = wait_for_completion_timeout(&cmpl, ctx->timeout);
+ if (ctx->timeout == 0) {
+ /* Remove the pending queue entry; this will be normally handled by the completion route but
+ * during a timeout it won't */
+ spin_lock_irqsave(&b->spinlock, ctx->irq_flags);
+ if (b->pending_entries[ctx->tag_n] == &ent)
+ b->pending_entries[ctx->tag_n] = NULL;
+ spin_unlock_irqrestore(&b->spinlock, ctx->irq_flags);
+ return -ETIMEDOUT;
+ }
+ return 0;
+}
+
+static void aaudio_handle_reply(struct aaudio_bce *b, struct aaudio_msg *reply)
+{
+ const char *tag;
+ int tagn;
+ unsigned long irq_flags;
+ char tag_zero[5];
+ struct aaudio_bce_queue_entry *entry;
+
+ tag = ((struct aaudio_msg_header *) reply->data)->tag;
+ if (tag[0] != 'S') {
+ pr_err("aaudio_handle_reply: Unexpected tag: %.4s\n", tag);
+ return;
+ }
+ *((u32 *) tag_zero) = *((u32 *) tag);
+ tag_zero[4] = 0;
+ if (kstrtoint(&tag_zero[1], 10, &tagn)) {
+ pr_err("aaudio_handle_reply: Tag parse failed: %.4s\n", tag);
+ return;
+ }
+
+ spin_lock_irqsave(&b->spinlock, irq_flags);
+ entry = b->pending_entries[tagn];
+ if (entry) {
+ if (reply->size < entry->msg->size)
+ entry->msg->size = reply->size;
+ memcpy(entry->msg->data, reply->data, entry->msg->size);
+ complete(entry->cmpl);
+
+ b->pending_entries[tagn] = NULL;
+ } else {
+ pr_err("aaudio_handle_reply: No queued item found for tag: %.4s\n", tag);
+ }
+ spin_unlock_irqrestore(&b->spinlock, irq_flags);
+}
+
+static void aaudio_bce_out_queue_completion(struct bce_queue_sq *sq)
+{
+ while (bce_next_completion(sq)) {
+ //pr_info("aaudio: Send confirmed\n");
+ bce_notify_submission_complete(sq);
+ }
+}
+
+static void aaudio_bce_in_queue_handle_msg(struct aaudio_device *a, struct aaudio_msg *msg);
+
+static void aaudio_bce_in_queue_completion(struct bce_queue_sq *sq)
+{
+ struct aaudio_msg msg;
+ struct aaudio_device *dev = sq->userdata;
+ struct aaudio_bce_queue *q = &dev->bcem.qin;
+ struct bce_sq_completion_data *c;
+ size_t cnt = 0;
+
+ mb();
+ while ((c = bce_next_completion(sq))) {
+ msg.data = (u8 *) q->data + q->data_head * q->el_size;
+ msg.size = c->data_size;
+#ifdef DEBUG
+ pr_debug("aaudio: Received command data %llx\n", c->data_size);
+ print_hex_dump(KERN_DEBUG, "aaudio:IN ", DUMP_PREFIX_NONE, 32, 1, msg.data, min(msg.size, 128UL), true);
+#endif
+ aaudio_bce_in_queue_handle_msg(dev, &msg);
+
+ q->data_head = (q->data_head + 1) % q->el_count;
+
+ bce_notify_submission_complete(sq);
+ ++cnt;
+ }
+ aaudio_bce_in_queue_submit_pending(q, cnt);
+}
+
+static void aaudio_bce_in_queue_handle_msg(struct aaudio_device *a, struct aaudio_msg *msg)
+{
+ struct aaudio_msg_header *header = (struct aaudio_msg_header *) msg->data;
+ if (msg->size < sizeof(struct aaudio_msg_header)) {
+ pr_err("aaudio: Msg size smaller than header (%lx)", msg->size);
+ return;
+ }
+ if (header->type == AAUDIO_MSG_TYPE_RESPONSE) {
+ aaudio_handle_reply(&a->bcem, msg);
+ } else if (header->type == AAUDIO_MSG_TYPE_COMMAND) {
+ aaudio_handle_command(a, msg);
+ } else if (header->type == AAUDIO_MSG_TYPE_NOTIFICATION) {
+ aaudio_handle_notification(a, msg);
+ }
+}
+
+void aaudio_bce_in_queue_submit_pending(struct aaudio_bce_queue *q, size_t count)
+{
+ struct bce_qe_submission *s;
+ while (count--) {
+ if (bce_reserve_submission(q->sq, NULL)) {
+ pr_err("aaudio: Failed to reserve an event queue submission\n");
+ break;
+ }
+ s = bce_next_submission(q->sq);
+ bce_set_submission_single(s, q->dma_addr + (dma_addr_t) (q->data_tail * q->el_size), q->el_size);
+ q->data_tail = (q->data_tail + 1) % q->el_count;
+ }
+ bce_submit_to_device(q->sq);
+}
+
+struct aaudio_msg aaudio_reply_alloc(void)
+{
+ struct aaudio_msg ret;
+ ret.size = AAUDIO_BCE_QUEUE_ELEMENT_SIZE;
+ ret.data = kmalloc(ret.size, GFP_KERNEL);
+ return ret;
+}
+
+void aaudio_reply_free(struct aaudio_msg *reply)
+{
+ kfree(reply->data);
+}
diff --git a/drivers/staging/apple-bce/audio/protocol_bce.h b/drivers/staging/apple-bce/audio/protocol_bce.h
new file mode 100644
index 000000000..14d26c05d
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/protocol_bce.h
@@ -0,0 +1,72 @@
+#ifndef AAUDIO_PROTOCOL_BCE_H
+#define AAUDIO_PROTOCOL_BCE_H
+
+#include "protocol.h"
+#include "../queue.h"
+
+#define AAUDIO_BCE_QUEUE_ELEMENT_SIZE 0x1000
+#define AAUDIO_BCE_QUEUE_ELEMENT_COUNT 20
+
+#define AAUDIO_BCE_QUEUE_TAG_COUNT 1000
+
+struct aaudio_device;
+
+struct aaudio_bce_queue_entry {
+ struct aaudio_msg *msg;
+ struct completion *cmpl;
+};
+struct aaudio_bce_queue {
+ struct bce_queue_cq *cq;
+ struct bce_queue_sq *sq;
+ void *data;
+ dma_addr_t dma_addr;
+ size_t data_head, data_tail;
+ size_t el_size, el_count;
+};
+struct aaudio_bce {
+ struct bce_queue_cq *cq;
+ struct aaudio_bce_queue qin;
+ struct aaudio_bce_queue qout;
+ int tag_num;
+ struct aaudio_bce_queue_entry *pending_entries[AAUDIO_BCE_QUEUE_TAG_COUNT];
+ struct spinlock spinlock;
+};
+
+struct aaudio_send_ctx {
+ int status;
+ int tag_n;
+ unsigned long irq_flags;
+ struct aaudio_msg msg;
+ unsigned long timeout;
+};
+
+int aaudio_bce_init(struct aaudio_device *dev);
+int __aaudio_send_prepare(struct aaudio_bce *b, struct aaudio_send_ctx *ctx, char *tag);
+void __aaudio_send(struct aaudio_bce *b, struct aaudio_send_ctx *ctx);
+int __aaudio_send_cmd_sync(struct aaudio_bce *b, struct aaudio_send_ctx *ctx, struct aaudio_msg *reply);
+
+#define aaudio_send_with_tag(a, ctx, tag, tout, fn, ...) ({ \
+ (ctx)->timeout = msecs_to_jiffies(tout); \
+ (ctx)->status = __aaudio_send_prepare(&(a)->bcem, (ctx), (tag)); \
+ if (!(ctx)->status) { \
+ fn(&(ctx)->msg, ##__VA_ARGS__); \
+ __aaudio_send(&(a)->bcem, (ctx)); \
+ } \
+ (ctx)->status; \
+})
+#define aaudio_send(a, ctx, tout, fn, ...) aaudio_send_with_tag(a, ctx, NULL, tout, fn, ##__VA_ARGS__)
+
+#define aaudio_send_cmd_sync(a, ctx, reply, tout, fn, ...) ({ \
+ (ctx)->timeout = msecs_to_jiffies(tout); \
+ (ctx)->status = __aaudio_send_prepare(&(a)->bcem, (ctx), NULL); \
+ if (!(ctx)->status) { \
+ fn(&(ctx)->msg, ##__VA_ARGS__); \
+ (ctx)->status = __aaudio_send_cmd_sync(&(a)->bcem, (ctx), (reply)); \
+ } \
+ (ctx)->status; \
+})
+
+struct aaudio_msg aaudio_reply_alloc(void);
+void aaudio_reply_free(struct aaudio_msg *reply);
+
+#endif //AAUDIO_PROTOCOL_BCE_H
diff --git a/drivers/staging/apple-bce/mailbox.c b/drivers/staging/apple-bce/mailbox.c
new file mode 100644
index 000000000..e24bd3521
--- /dev/null
+++ b/drivers/staging/apple-bce/mailbox.c
@@ -0,0 +1,151 @@
+#include "mailbox.h"
+#include <linux/atomic.h>
+#include "apple_bce.h"
+
+#define REG_MBOX_OUT_BASE 0x820
+#define REG_MBOX_REPLY_COUNTER 0x108
+#define REG_MBOX_REPLY_BASE 0x810
+#define REG_TIMESTAMP_BASE 0xC000
+
+#define BCE_MBOX_TIMEOUT_MS 200
+
+void bce_mailbox_init(struct bce_mailbox *mb, void __iomem *reg_mb)
+{
+ mb->reg_mb = reg_mb;
+ init_completion(&mb->mb_completion);
+}
+
+int bce_mailbox_send(struct bce_mailbox *mb, u64 msg, u64* recv)
+{
+ u32 __iomem *regb;
+
+ if (atomic_cmpxchg(&mb->mb_status, 0, 1) != 0) {
+ return -EEXIST; // We don't support two messages at once
+ }
+ reinit_completion(&mb->mb_completion);
+
+ pr_debug("bce_mailbox_send: %llx\n", msg);
+ regb = (u32*) ((u8*) mb->reg_mb + REG_MBOX_OUT_BASE);
+ iowrite32((u32) msg, regb);
+ iowrite32((u32) (msg >> 32), regb + 1);
+ iowrite32(0, regb + 2);
+ iowrite32(0, regb + 3);
+
+ wait_for_completion_timeout(&mb->mb_completion, msecs_to_jiffies(BCE_MBOX_TIMEOUT_MS));
+ if (atomic_read(&mb->mb_status) != 2) { // Didn't get the reply
+ atomic_set(&mb->mb_status, 0);
+ return -ETIMEDOUT;
+ }
+
+ *recv = mb->mb_result;
+ pr_debug("bce_mailbox_send: reply %llx\n", *recv);
+
+ atomic_set(&mb->mb_status, 0);
+ return 0;
+}
+
+static int bce_mailbox_retrive_response(struct bce_mailbox *mb)
+{
+ u32 __iomem *regb;
+ u32 lo, hi;
+ int count, counter;
+ u32 res = ioread32((u8*) mb->reg_mb + REG_MBOX_REPLY_COUNTER);
+ count = (res >> 20) & 0xf;
+ counter = count;
+ pr_debug("bce_mailbox_retrive_response count=%i\n", count);
+ while (counter--) {
+ regb = (u32*) ((u8*) mb->reg_mb + REG_MBOX_REPLY_BASE);
+ lo = ioread32(regb);
+ hi = ioread32(regb + 1);
+ ioread32(regb + 2);
+ ioread32(regb + 3);
+ pr_debug("bce_mailbox_retrive_response %llx\n", ((u64) hi << 32) | lo);
+ mb->mb_result = ((u64) hi << 32) | lo;
+ }
+ return count > 0 ? 0 : -ENODATA;
+}
+
+int bce_mailbox_handle_interrupt(struct bce_mailbox *mb)
+{
+ int status = bce_mailbox_retrive_response(mb);
+ if (!status) {
+ atomic_set(&mb->mb_status, 2);
+ complete(&mb->mb_completion);
+ }
+ return status;
+}
+
+static void bc_send_timestamp(struct timer_list *tl);
+
+void bce_timestamp_init(struct bce_timestamp *ts, void __iomem *reg)
+{
+ u32 __iomem *regb;
+
+ spin_lock_init(&ts->stop_sl);
+ ts->stopped = false;
+
+ ts->reg = reg;
+
+ regb = (u32*) ((u8*) ts->reg + REG_TIMESTAMP_BASE);
+
+ ioread32(regb);
+ mb();
+
+ timer_setup(&ts->timer, bc_send_timestamp, 0);
+}
+
+void bce_timestamp_start(struct bce_timestamp *ts, bool is_initial)
+{
+ unsigned long flags;
+ u32 __iomem *regb = (u32*) ((u8*) ts->reg + REG_TIMESTAMP_BASE);
+
+ if (is_initial) {
+ iowrite32((u32) -4, regb + 2);
+ iowrite32((u32) -1, regb);
+ } else {
+ iowrite32((u32) -3, regb + 2);
+ iowrite32((u32) -1, regb);
+ }
+
+ spin_lock_irqsave(&ts->stop_sl, flags);
+ ts->stopped = false;
+ spin_unlock_irqrestore(&ts->stop_sl, flags);
+ mod_timer(&ts->timer, jiffies + msecs_to_jiffies(150));
+}
+
+void bce_timestamp_stop(struct bce_timestamp *ts)
+{
+ unsigned long flags;
+ u32 __iomem *regb = (u32*) ((u8*) ts->reg + REG_TIMESTAMP_BASE);
+
+ spin_lock_irqsave(&ts->stop_sl, flags);
+ ts->stopped = true;
+ spin_unlock_irqrestore(&ts->stop_sl, flags);
+ del_timer_sync(&ts->timer);
+
+ iowrite32((u32) -2, regb + 2);
+ iowrite32((u32) -1, regb);
+}
+
+static void bc_send_timestamp(struct timer_list *tl)
+{
+ struct bce_timestamp *ts;
+ unsigned long flags;
+ u32 __iomem *regb;
+ ktime_t bt;
+
+ ts = container_of(tl, struct bce_timestamp, timer);
+ regb = (u32*) ((u8*) ts->reg + REG_TIMESTAMP_BASE);
+ local_irq_save(flags);
+ ioread32(regb + 2);
+ mb();
+ bt = ktime_get_boottime();
+ iowrite32((u32) bt, regb + 2);
+ iowrite32((u32) (bt >> 32), regb);
+
+ spin_lock(&ts->stop_sl);
+ if (!ts->stopped)
+ mod_timer(&ts->timer, jiffies + msecs_to_jiffies(150));
+ spin_unlock(&ts->stop_sl);
+ local_irq_restore(flags);
+}
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/mailbox.h b/drivers/staging/apple-bce/mailbox.h
new file mode 100644
index 000000000..f3323f95b
--- /dev/null
+++ b/drivers/staging/apple-bce/mailbox.h
@@ -0,0 +1,53 @@
+#ifndef BCE_MAILBOX_H
+#define BCE_MAILBOX_H
+
+#include <linux/completion.h>
+#include <linux/pci.h>
+#include <linux/timer.h>
+
+struct bce_mailbox {
+ void __iomem *reg_mb;
+
+ atomic_t mb_status; // possible statuses: 0 (no msg), 1 (has active msg), 2 (got reply)
+ struct completion mb_completion;
+ uint64_t mb_result;
+};
+
+enum bce_message_type {
+ BCE_MB_REGISTER_COMMAND_SQ = 0x7, // to-device
+ BCE_MB_REGISTER_COMMAND_CQ = 0x8, // to-device
+ BCE_MB_REGISTER_COMMAND_QUEUE_REPLY = 0xB, // to-host
+ BCE_MB_SET_FW_PROTOCOL_VERSION = 0xC, // both
+ BCE_MB_SLEEP_NO_STATE = 0x14, // to-device
+ BCE_MB_RESTORE_NO_STATE = 0x15, // to-device
+ BCE_MB_SAVE_STATE_AND_SLEEP = 0x17, // to-device
+ BCE_MB_RESTORE_STATE_AND_WAKE = 0x18, // to-device
+ BCE_MB_SAVE_STATE_AND_SLEEP_FAILURE = 0x19, // from-device
+ BCE_MB_SAVE_RESTORE_STATE_COMPLETE = 0x1A, // from-device
+};
+
+#define BCE_MB_MSG(type, value) (((u64) (type) << 58) | ((value) & 0x3FFFFFFFFFFFFFFLL))
+#define BCE_MB_TYPE(v) ((u32) (v >> 58))
+#define BCE_MB_VALUE(v) (v & 0x3FFFFFFFFFFFFFFLL)
+
+void bce_mailbox_init(struct bce_mailbox *mb, void __iomem *reg_mb);
+
+int bce_mailbox_send(struct bce_mailbox *mb, u64 msg, u64* recv);
+
+int bce_mailbox_handle_interrupt(struct bce_mailbox *mb);
+
+
+struct bce_timestamp {
+ void __iomem *reg;
+ struct timer_list timer;
+ struct spinlock stop_sl;
+ bool stopped;
+};
+
+void bce_timestamp_init(struct bce_timestamp *ts, void __iomem *reg);
+
+void bce_timestamp_start(struct bce_timestamp *ts, bool is_initial);
+
+void bce_timestamp_stop(struct bce_timestamp *ts);
+
+#endif //BCEDRIVER_MAILBOX_H
diff --git a/drivers/staging/apple-bce/queue.c b/drivers/staging/apple-bce/queue.c
new file mode 100644
index 000000000..bc9cd3bc6
--- /dev/null
+++ b/drivers/staging/apple-bce/queue.c
@@ -0,0 +1,390 @@
+#include "queue.h"
+#include "apple_bce.h"
+
+#define REG_DOORBELL_BASE 0x44000
+
+struct bce_queue_cq *bce_alloc_cq(struct apple_bce_device *dev, int qid, u32 el_count)
+{
+ struct bce_queue_cq *q;
+ q = kzalloc(sizeof(struct bce_queue_cq), GFP_KERNEL);
+ q->qid = qid;
+ q->type = BCE_QUEUE_CQ;
+ q->el_count = el_count;
+ q->data = dma_alloc_coherent(&dev->pci->dev, el_count * sizeof(struct bce_qe_completion),
+ &q->dma_handle, GFP_KERNEL);
+ if (!q->data) {
+ pr_err("DMA queue memory alloc failed\n");
+ kfree(q);
+ return NULL;
+ }
+ return q;
+}
+
+void bce_get_cq_memcfg(struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg)
+{
+ cfg->qid = (u16) cq->qid;
+ cfg->el_count = (u16) cq->el_count;
+ cfg->vector_or_cq = 0;
+ cfg->_pad = 0;
+ cfg->addr = cq->dma_handle;
+ cfg->length = cq->el_count * sizeof(struct bce_qe_completion);
+}
+
+void bce_free_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq)
+{
+ dma_free_coherent(&dev->pci->dev, cq->el_count * sizeof(struct bce_qe_completion), cq->data, cq->dma_handle);
+ kfree(cq);
+}
+
+static void bce_handle_cq_completion(struct apple_bce_device *dev, struct bce_qe_completion *e, size_t *ce)
+{
+ struct bce_queue *target;
+ struct bce_queue_sq *target_sq;
+ struct bce_sq_completion_data *cmpl;
+ if (e->qid >= BCE_MAX_QUEUE_COUNT) {
+ pr_err("Device sent a response for qid (%u) >= BCE_MAX_QUEUE_COUNT\n", e->qid);
+ return;
+ }
+ target = dev->queues[e->qid];
+ if (!target || target->type != BCE_QUEUE_SQ) {
+ pr_err("Device sent a response for qid (%u), which does not exist\n", e->qid);
+ return;
+ }
+ target_sq = (struct bce_queue_sq *) target;
+ if (target_sq->completion_tail != e->completion_index) {
+ pr_err("Completion index mismatch; this is likely going to make this driver unusable\n");
+ return;
+ }
+ if (!target_sq->has_pending_completions) {
+ target_sq->has_pending_completions = true;
+ dev->int_sq_list[(*ce)++] = target_sq;
+ }
+ cmpl = &target_sq->completion_data[e->completion_index];
+ cmpl->status = e->status;
+ cmpl->data_size = e->data_size;
+ cmpl->result = e->result;
+ wmb();
+ target_sq->completion_tail = (target_sq->completion_tail + 1) % target_sq->el_count;
+}
+
+void bce_handle_cq_completions(struct apple_bce_device *dev, struct bce_queue_cq *cq)
+{
+ size_t ce = 0;
+ struct bce_qe_completion *e;
+ struct bce_queue_sq *sq;
+ e = bce_cq_element(cq, cq->index);
+ if (!(e->flags & BCE_COMPLETION_FLAG_PENDING))
+ return;
+ mb();
+ while (true) {
+ e = bce_cq_element(cq, cq->index);
+ if (!(e->flags & BCE_COMPLETION_FLAG_PENDING))
+ break;
+ // pr_info("apple-bce: compl: %i: %i %llx %llx", e->qid, e->status, e->data_size, e->result);
+ bce_handle_cq_completion(dev, e, &ce);
+ e->flags = 0;
+ cq->index = (cq->index + 1) % cq->el_count;
+ }
+ mb();
+ iowrite32(cq->index, (u32 *) ((u8 *) dev->reg_mem_dma + REG_DOORBELL_BASE) + cq->qid);
+ while (ce) {
+ --ce;
+ sq = dev->int_sq_list[ce];
+ sq->completion(sq);
+ sq->has_pending_completions = false;
+ }
+}
+
+
+struct bce_queue_sq *bce_alloc_sq(struct apple_bce_device *dev, int qid, u32 el_size, u32 el_count,
+ bce_sq_completion compl, void *userdata)
+{
+ struct bce_queue_sq *q;
+ q = kzalloc(sizeof(struct bce_queue_sq), GFP_KERNEL);
+ q->qid = qid;
+ q->type = BCE_QUEUE_SQ;
+ q->el_size = el_size;
+ q->el_count = el_count;
+ q->data = dma_alloc_coherent(&dev->pci->dev, el_count * el_size,
+ &q->dma_handle, GFP_KERNEL);
+ q->completion = compl;
+ q->userdata = userdata;
+ q->completion_data = kzalloc(sizeof(struct bce_sq_completion_data) * el_count, GFP_KERNEL);
+ q->reg_mem_dma = dev->reg_mem_dma;
+ atomic_set(&q->available_commands, el_count - 1);
+ init_completion(&q->available_command_completion);
+ atomic_set(&q->available_command_completion_waiting_count, 0);
+ if (!q->data) {
+ pr_err("DMA queue memory alloc failed\n");
+ kfree(q);
+ return NULL;
+ }
+ return q;
+}
+
+void bce_get_sq_memcfg(struct bce_queue_sq *sq, struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg)
+{
+ cfg->qid = (u16) sq->qid;
+ cfg->el_count = (u16) sq->el_count;
+ cfg->vector_or_cq = (u16) cq->qid;
+ cfg->_pad = 0;
+ cfg->addr = sq->dma_handle;
+ cfg->length = sq->el_count * sq->el_size;
+}
+
+void bce_free_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq)
+{
+ dma_free_coherent(&dev->pci->dev, sq->el_count * sq->el_size, sq->data, sq->dma_handle);
+ kfree(sq);
+}
+
+int bce_reserve_submission(struct bce_queue_sq *sq, unsigned long *timeout)
+{
+ while (atomic_dec_if_positive(&sq->available_commands) < 0) {
+ if (!timeout || !*timeout)
+ return -EAGAIN;
+ atomic_inc(&sq->available_command_completion_waiting_count);
+ *timeout = wait_for_completion_timeout(&sq->available_command_completion, *timeout);
+ if (!*timeout) {
+ if (atomic_dec_if_positive(&sq->available_command_completion_waiting_count) < 0)
+ try_wait_for_completion(&sq->available_command_completion); /* consume the pending completion */
+ }
+ }
+ return 0;
+}
+
+void bce_cancel_submission_reservation(struct bce_queue_sq *sq)
+{
+ atomic_inc(&sq->available_commands);
+}
+
+void *bce_next_submission(struct bce_queue_sq *sq)
+{
+ void *ret = bce_sq_element(sq, sq->tail);
+ sq->tail = (sq->tail + 1) % sq->el_count;
+ return ret;
+}
+
+void bce_submit_to_device(struct bce_queue_sq *sq)
+{
+ mb();
+ iowrite32(sq->tail, (u32 *) ((u8 *) sq->reg_mem_dma + REG_DOORBELL_BASE) + sq->qid);
+}
+
+void bce_notify_submission_complete(struct bce_queue_sq *sq)
+{
+ sq->head = (sq->head + 1) % sq->el_count;
+ atomic_inc(&sq->available_commands);
+ if (atomic_dec_if_positive(&sq->available_command_completion_waiting_count) >= 0) {
+ complete(&sq->available_command_completion);
+ }
+}
+
+void bce_set_submission_single(struct bce_qe_submission *element, dma_addr_t addr, size_t size)
+{
+ element->addr = addr;
+ element->length = size;
+ element->segl_addr = element->segl_length = 0;
+}
+
+static void bce_cmdq_completion(struct bce_queue_sq *q);
+
+struct bce_queue_cmdq *bce_alloc_cmdq(struct apple_bce_device *dev, int qid, u32 el_count)
+{
+ struct bce_queue_cmdq *q;
+ q = kzalloc(sizeof(struct bce_queue_cmdq), GFP_KERNEL);
+ q->sq = bce_alloc_sq(dev, qid, BCE_CMD_SIZE, el_count, bce_cmdq_completion, q);
+ if (!q->sq) {
+ kfree(q);
+ return NULL;
+ }
+ spin_lock_init(&q->lck);
+ q->tres = kzalloc(sizeof(struct bce_queue_cmdq_result_el*) * el_count, GFP_KERNEL);
+ if (!q->tres) {
+ kfree(q);
+ return NULL;
+ }
+ return q;
+}
+
+void bce_free_cmdq(struct apple_bce_device *dev, struct bce_queue_cmdq *cmdq)
+{
+ bce_free_sq(dev, cmdq->sq);
+ kfree(cmdq->tres);
+ kfree(cmdq);
+}
+
+void bce_cmdq_completion(struct bce_queue_sq *q)
+{
+ struct bce_queue_cmdq_result_el *el;
+ struct bce_queue_cmdq *cmdq = q->userdata;
+ struct bce_sq_completion_data *result;
+
+ spin_lock(&cmdq->lck);
+ while ((result = bce_next_completion(q))) {
+ el = cmdq->tres[cmdq->sq->head];
+ if (el) {
+ el->result = result->result;
+ el->status = result->status;
+ mb();
+ complete(&el->cmpl);
+ } else {
+ pr_err("apple-bce: Unexpected command queue completion\n");
+ }
+ cmdq->tres[cmdq->sq->head] = NULL;
+ bce_notify_submission_complete(q);
+ }
+ spin_unlock(&cmdq->lck);
+}
+
+static __always_inline void *bce_cmd_start(struct bce_queue_cmdq *cmdq, struct bce_queue_cmdq_result_el *res)
+{
+ void *ret;
+ unsigned long timeout;
+ init_completion(&res->cmpl);
+ mb();
+
+ timeout = msecs_to_jiffies(1000L * 60 * 5); /* wait for up to ~5 minutes */
+ if (bce_reserve_submission(cmdq->sq, &timeout))
+ return NULL;
+
+ spin_lock(&cmdq->lck);
+ cmdq->tres[cmdq->sq->tail] = res;
+ ret = bce_next_submission(cmdq->sq);
+ return ret;
+}
+
+static __always_inline void bce_cmd_finish(struct bce_queue_cmdq *cmdq, struct bce_queue_cmdq_result_el *res)
+{
+ bce_submit_to_device(cmdq->sq);
+ spin_unlock(&cmdq->lck);
+
+ wait_for_completion(&res->cmpl);
+ mb();
+}
+
+u32 bce_cmd_register_queue(struct bce_queue_cmdq *cmdq, struct bce_queue_memcfg *cfg, const char *name, bool isdirout)
+{
+ struct bce_queue_cmdq_result_el res;
+ struct bce_cmdq_register_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
+ if (!cmd)
+ return (u32) -1;
+ cmd->cmd = BCE_CMD_REGISTER_MEMORY_QUEUE;
+ cmd->flags = (u16) ((name ? 2 : 0) | (isdirout ? 1 : 0));
+ cmd->qid = cfg->qid;
+ cmd->el_count = cfg->el_count;
+ cmd->vector_or_cq = cfg->vector_or_cq;
+ memset(cmd->name, 0, sizeof(cmd->name));
+ if (name) {
+ cmd->name_len = (u16) min(strlen(name), (size_t) sizeof(cmd->name));
+ memcpy(cmd->name, name, cmd->name_len);
+ } else {
+ cmd->name_len = 0;
+ }
+ cmd->addr = cfg->addr;
+ cmd->length = cfg->length;
+
+ bce_cmd_finish(cmdq, &res);
+ return res.status;
+}
+
+u32 bce_cmd_unregister_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid)
+{
+ struct bce_queue_cmdq_result_el res;
+ struct bce_cmdq_simple_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
+ if (!cmd)
+ return (u32) -1;
+ cmd->cmd = BCE_CMD_UNREGISTER_MEMORY_QUEUE;
+ cmd->flags = 0;
+ cmd->qid = qid;
+ bce_cmd_finish(cmdq, &res);
+ return res.status;
+}
+
+u32 bce_cmd_flush_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid)
+{
+ struct bce_queue_cmdq_result_el res;
+ struct bce_cmdq_simple_memory_queue_cmd *cmd = bce_cmd_start(cmdq, &res);
+ if (!cmd)
+ return (u32) -1;
+ cmd->cmd = BCE_CMD_FLUSH_MEMORY_QUEUE;
+ cmd->flags = 0;
+ cmd->qid = qid;
+ bce_cmd_finish(cmdq, &res);
+ return res.status;
+}
+
+
+struct bce_queue_cq *bce_create_cq(struct apple_bce_device *dev, u32 el_count)
+{
+ struct bce_queue_cq *cq;
+ struct bce_queue_memcfg cfg;
+ int qid = ida_simple_get(&dev->queue_ida, BCE_QUEUE_USER_MIN, BCE_QUEUE_USER_MAX, GFP_KERNEL);
+ if (qid < 0)
+ return NULL;
+ cq = bce_alloc_cq(dev, qid, el_count);
+ if (!cq)
+ return NULL;
+ bce_get_cq_memcfg(cq, &cfg);
+ if (bce_cmd_register_queue(dev->cmd_cmdq, &cfg, NULL, false) != 0) {
+ pr_err("apple-bce: CQ registration failed (%i)", qid);
+ bce_free_cq(dev, cq);
+ ida_simple_remove(&dev->queue_ida, (uint) qid);
+ return NULL;
+ }
+ dev->queues[qid] = (struct bce_queue *) cq;
+ return cq;
+}
+
+struct bce_queue_sq *bce_create_sq(struct apple_bce_device *dev, struct bce_queue_cq *cq, const char *name, u32 el_count,
+ int direction, bce_sq_completion compl, void *userdata)
+{
+ struct bce_queue_sq *sq;
+ struct bce_queue_memcfg cfg;
+ int qid;
+ if (cq == NULL)
+ return NULL; /* cq can not be null */
+ if (name == NULL)
+ return NULL; /* name can not be null */
+ if (direction != DMA_TO_DEVICE && direction != DMA_FROM_DEVICE)
+ return NULL; /* unsupported direction */
+ qid = ida_simple_get(&dev->queue_ida, BCE_QUEUE_USER_MIN, BCE_QUEUE_USER_MAX, GFP_KERNEL);
+ if (qid < 0)
+ return NULL;
+ sq = bce_alloc_sq(dev, qid, sizeof(struct bce_qe_submission), el_count, compl, userdata);
+ if (!sq)
+ return NULL;
+ bce_get_sq_memcfg(sq, cq, &cfg);
+ if (bce_cmd_register_queue(dev->cmd_cmdq, &cfg, name, direction != DMA_FROM_DEVICE) != 0) {
+ pr_err("apple-bce: SQ registration failed (%i)", qid);
+ bce_free_sq(dev, sq);
+ ida_simple_remove(&dev->queue_ida, (uint) qid);
+ return NULL;
+ }
+ spin_lock(&dev->queues_lock);
+ dev->queues[qid] = (struct bce_queue *) sq;
+ spin_unlock(&dev->queues_lock);
+ return sq;
+}
+
+void bce_destroy_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq)
+{
+ if (!dev->is_being_removed && bce_cmd_unregister_memory_queue(dev->cmd_cmdq, (u16) cq->qid))
+ pr_err("apple-bce: CQ unregister failed");
+ spin_lock(&dev->queues_lock);
+ dev->queues[cq->qid] = NULL;
+ spin_unlock(&dev->queues_lock);
+ ida_simple_remove(&dev->queue_ida, (uint) cq->qid);
+ bce_free_cq(dev, cq);
+}
+
+void bce_destroy_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq)
+{
+ if (!dev->is_being_removed && bce_cmd_unregister_memory_queue(dev->cmd_cmdq, (u16) sq->qid))
+ pr_err("apple-bce: CQ unregister failed");
+ spin_lock(&dev->queues_lock);
+ dev->queues[sq->qid] = NULL;
+ spin_unlock(&dev->queues_lock);
+ ida_simple_remove(&dev->queue_ida, (uint) sq->qid);
+ bce_free_sq(dev, sq);
+}
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/queue.h b/drivers/staging/apple-bce/queue.h
new file mode 100644
index 000000000..8368ac5df
--- /dev/null
+++ b/drivers/staging/apple-bce/queue.h
@@ -0,0 +1,177 @@
+#ifndef BCE_QUEUE_H
+#define BCE_QUEUE_H
+
+#include <linux/completion.h>
+#include <linux/pci.h>
+
+#define BCE_CMD_SIZE 0x40
+
+struct apple_bce_device;
+
+enum bce_queue_type {
+ BCE_QUEUE_CQ, BCE_QUEUE_SQ
+};
+struct bce_queue {
+ int qid;
+ int type;
+};
+struct bce_queue_cq {
+ int qid;
+ int type;
+ u32 el_count;
+ dma_addr_t dma_handle;
+ void *data;
+
+ u32 index;
+};
+struct bce_queue_sq;
+typedef void (*bce_sq_completion)(struct bce_queue_sq *q);
+struct bce_sq_completion_data {
+ u32 status;
+ u64 data_size;
+ u64 result;
+};
+struct bce_queue_sq {
+ int qid;
+ int type;
+ u32 el_size;
+ u32 el_count;
+ dma_addr_t dma_handle;
+ void *data;
+ void *userdata;
+ void __iomem *reg_mem_dma;
+
+ atomic_t available_commands;
+ struct completion available_command_completion;
+ atomic_t available_command_completion_waiting_count;
+ u32 head, tail;
+
+ u32 completion_cidx, completion_tail;
+ struct bce_sq_completion_data *completion_data;
+ bool has_pending_completions;
+ bce_sq_completion completion;
+};
+
+struct bce_queue_cmdq_result_el {
+ struct completion cmpl;
+ u32 status;
+ u64 result;
+};
+struct bce_queue_cmdq {
+ struct bce_queue_sq *sq;
+ struct spinlock lck;
+ struct bce_queue_cmdq_result_el **tres;
+};
+
+struct bce_queue_memcfg {
+ u16 qid;
+ u16 el_count;
+ u16 vector_or_cq;
+ u16 _pad;
+ u64 addr;
+ u64 length;
+};
+
+enum bce_qe_completion_status {
+ BCE_COMPLETION_SUCCESS = 0,
+ BCE_COMPLETION_ERROR = 1,
+ BCE_COMPLETION_ABORTED = 2,
+ BCE_COMPLETION_NO_SPACE = 3,
+ BCE_COMPLETION_OVERRUN = 4
+};
+enum bce_qe_completion_flags {
+ BCE_COMPLETION_FLAG_PENDING = 0x8000
+};
+struct bce_qe_completion {
+ u64 result;
+ u64 data_size;
+ u16 qid;
+ u16 completion_index;
+ u16 status; // bce_qe_completion_status
+ u16 flags; // bce_qe_completion_flags
+};
+
+struct bce_qe_submission {
+ u64 length;
+ u64 addr;
+
+ u64 segl_addr;
+ u64 segl_length;
+};
+
+enum bce_cmdq_command {
+ BCE_CMD_REGISTER_MEMORY_QUEUE = 0x20,
+ BCE_CMD_UNREGISTER_MEMORY_QUEUE = 0x30,
+ BCE_CMD_FLUSH_MEMORY_QUEUE = 0x40,
+ BCE_CMD_SET_MEMORY_QUEUE_PROPERTY = 0x50
+};
+struct bce_cmdq_simple_memory_queue_cmd {
+ u16 cmd; // bce_cmdq_command
+ u16 flags;
+ u16 qid;
+};
+struct bce_cmdq_register_memory_queue_cmd {
+ u16 cmd; // bce_cmdq_command
+ u16 flags;
+ u16 qid;
+ u16 _pad;
+ u16 el_count;
+ u16 vector_or_cq;
+ u16 _pad2;
+ u16 name_len;
+ char name[0x20];
+ u64 addr;
+ u64 length;
+};
+
+static __always_inline void *bce_sq_element(struct bce_queue_sq *q, int i) {
+ return (void *) ((u8 *) q->data + q->el_size * i);
+}
+static __always_inline void *bce_cq_element(struct bce_queue_cq *q, int i) {
+ return (void *) ((struct bce_qe_completion *) q->data + i);
+}
+
+static __always_inline struct bce_sq_completion_data *bce_next_completion(struct bce_queue_sq *sq) {
+ struct bce_sq_completion_data *res;
+ rmb();
+ if (sq->completion_cidx == sq->completion_tail)
+ return NULL;
+ res = &sq->completion_data[sq->completion_cidx];
+ sq->completion_cidx = (sq->completion_cidx + 1) % sq->el_count;
+ return res;
+}
+
+struct bce_queue_cq *bce_alloc_cq(struct apple_bce_device *dev, int qid, u32 el_count);
+void bce_get_cq_memcfg(struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg);
+void bce_free_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq);
+void bce_handle_cq_completions(struct apple_bce_device *dev, struct bce_queue_cq *cq);
+
+struct bce_queue_sq *bce_alloc_sq(struct apple_bce_device *dev, int qid, u32 el_size, u32 el_count,
+ bce_sq_completion compl, void *userdata);
+void bce_get_sq_memcfg(struct bce_queue_sq *sq, struct bce_queue_cq *cq, struct bce_queue_memcfg *cfg);
+void bce_free_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq);
+int bce_reserve_submission(struct bce_queue_sq *sq, unsigned long *timeout);
+void bce_cancel_submission_reservation(struct bce_queue_sq *sq);
+void *bce_next_submission(struct bce_queue_sq *sq);
+void bce_submit_to_device(struct bce_queue_sq *sq);
+void bce_notify_submission_complete(struct bce_queue_sq *sq);
+
+void bce_set_submission_single(struct bce_qe_submission *element, dma_addr_t addr, size_t size);
+
+struct bce_queue_cmdq *bce_alloc_cmdq(struct apple_bce_device *dev, int qid, u32 el_count);
+void bce_free_cmdq(struct apple_bce_device *dev, struct bce_queue_cmdq *cmdq);
+
+u32 bce_cmd_register_queue(struct bce_queue_cmdq *cmdq, struct bce_queue_memcfg *cfg, const char *name, bool isdirout);
+u32 bce_cmd_unregister_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid);
+u32 bce_cmd_flush_memory_queue(struct bce_queue_cmdq *cmdq, u16 qid);
+
+
+/* User API - Creates and registers the queue */
+
+struct bce_queue_cq *bce_create_cq(struct apple_bce_device *dev, u32 el_count);
+struct bce_queue_sq *bce_create_sq(struct apple_bce_device *dev, struct bce_queue_cq *cq, const char *name, u32 el_count,
+ int direction, bce_sq_completion compl, void *userdata);
+void bce_destroy_cq(struct apple_bce_device *dev, struct bce_queue_cq *cq);
+void bce_destroy_sq(struct apple_bce_device *dev, struct bce_queue_sq *sq);
+
+#endif //BCEDRIVER_MAILBOX_H
diff --git a/drivers/staging/apple-bce/queue_dma.c b/drivers/staging/apple-bce/queue_dma.c
new file mode 100644
index 000000000..b23661328
--- /dev/null
+++ b/drivers/staging/apple-bce/queue_dma.c
@@ -0,0 +1,220 @@
+#include "queue_dma.h"
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include "queue.h"
+
+static int bce_alloc_scatterlist_from_vm(struct sg_table *tbl, void *data, size_t len);
+static struct bce_segment_list_element_hostinfo *bce_map_segment_list(
+ struct device *dev, struct scatterlist *pages, int pagen);
+static void bce_unmap_segement_list(struct device *dev, struct bce_segment_list_element_hostinfo *list);
+
+int bce_map_dma_buffer(struct device *dev, struct bce_dma_buffer *buf, struct sg_table scatterlist,
+ enum dma_data_direction dir)
+{
+ int cnt;
+
+ buf->direction = dir;
+ buf->scatterlist = scatterlist;
+ buf->seglist_hostinfo = NULL;
+
+ cnt = dma_map_sg(dev, buf->scatterlist.sgl, buf->scatterlist.nents, dir);
+ if (cnt != buf->scatterlist.nents) {
+ pr_err("apple-bce: DMA scatter list mapping returned an unexpected count: %i\n", cnt);
+ dma_unmap_sg(dev, buf->scatterlist.sgl, buf->scatterlist.nents, dir);
+ return -EIO;
+ }
+ if (cnt == 1)
+ return 0;
+
+ buf->seglist_hostinfo = bce_map_segment_list(dev, buf->scatterlist.sgl, buf->scatterlist.nents);
+ if (!buf->seglist_hostinfo) {
+ pr_err("apple-bce: Creating segment list failed\n");
+ dma_unmap_sg(dev, buf->scatterlist.sgl, buf->scatterlist.nents, dir);
+ return -EIO;
+ }
+ return 0;
+}
+
+int bce_map_dma_buffer_vm(struct device *dev, struct bce_dma_buffer *buf, void *data, size_t len,
+ enum dma_data_direction dir)
+{
+ int status;
+ struct sg_table scatterlist;
+ if ((status = bce_alloc_scatterlist_from_vm(&scatterlist, data, len)))
+ return status;
+ if ((status = bce_map_dma_buffer(dev, buf, scatterlist, dir))) {
+ sg_free_table(&scatterlist);
+ return status;
+ }
+ return 0;
+}
+
+int bce_map_dma_buffer_km(struct device *dev, struct bce_dma_buffer *buf, void *data, size_t len,
+ enum dma_data_direction dir)
+{
+ /* Kernel memory is continuous which is great for us. */
+ int status;
+ struct sg_table scatterlist;
+ if ((status = sg_alloc_table(&scatterlist, 1, GFP_KERNEL))) {
+ sg_free_table(&scatterlist);
+ return status;
+ }
+ sg_set_buf(scatterlist.sgl, data, (uint) len);
+ if ((status = bce_map_dma_buffer(dev, buf, scatterlist, dir))) {
+ sg_free_table(&scatterlist);
+ return status;
+ }
+ return 0;
+}
+
+void bce_unmap_dma_buffer(struct device *dev, struct bce_dma_buffer *buf)
+{
+ dma_unmap_sg(dev, buf->scatterlist.sgl, buf->scatterlist.nents, buf->direction);
+ bce_unmap_segement_list(dev, buf->seglist_hostinfo);
+}
+
+
+static int bce_alloc_scatterlist_from_vm(struct sg_table *tbl, void *data, size_t len)
+{
+ int status, i;
+ struct page **pages;
+ size_t off, start_page, end_page, page_count;
+ off = (size_t) data % PAGE_SIZE;
+ start_page = (size_t) data / PAGE_SIZE;
+ end_page = ((size_t) data + len - 1) / PAGE_SIZE;
+ page_count = end_page - start_page + 1;
+
+ if (page_count > PAGE_SIZE / sizeof(struct page *))
+ pages = vmalloc(page_count * sizeof(struct page *));
+ else
+ pages = kmalloc(page_count * sizeof(struct page *), GFP_KERNEL);
+
+ for (i = 0; i < page_count; i++)
+ pages[i] = vmalloc_to_page((void *) ((start_page + i) * PAGE_SIZE));
+
+ if ((status = sg_alloc_table_from_pages(tbl, pages, page_count, (unsigned int) off, len, GFP_KERNEL))) {
+ sg_free_table(tbl);
+ }
+
+ if (page_count > PAGE_SIZE / sizeof(struct page *))
+ vfree(pages);
+ else
+ kfree(pages);
+ return status;
+}
+
+#define BCE_ELEMENTS_PER_PAGE ((PAGE_SIZE - sizeof(struct bce_segment_list_header)) \
+ / sizeof(struct bce_segment_list_element))
+#define BCE_ELEMENTS_PER_ADDITIONAL_PAGE (PAGE_SIZE / sizeof(struct bce_segment_list_element))
+
+static struct bce_segment_list_element_hostinfo *bce_map_segment_list(
+ struct device *dev, struct scatterlist *pages, int pagen)
+{
+ size_t ptr, pptr = 0;
+ struct bce_segment_list_header theader; /* a temp header, to store the initial seg */
+ struct bce_segment_list_header *header;
+ struct bce_segment_list_element *el, *el_end;
+ struct bce_segment_list_element_hostinfo *out, *pout, *out_root;
+ struct scatterlist *sg;
+ int i;
+ header = &theader;
+ out = out_root = NULL;
+ el = el_end = NULL;
+ for_each_sg(pages, sg, pagen, i) {
+ if (el >= el_end) {
+ /* allocate a new page, this will be also done for the first element */
+ ptr = __get_free_page(GFP_KERNEL);
+ if (pptr && ptr == pptr + PAGE_SIZE) {
+ out->page_count++;
+ header->element_count += BCE_ELEMENTS_PER_ADDITIONAL_PAGE;
+ el_end += BCE_ELEMENTS_PER_ADDITIONAL_PAGE;
+ } else {
+ header = (void *) ptr;
+ header->element_count = BCE_ELEMENTS_PER_PAGE;
+ header->data_size = 0;
+ header->next_segl_addr = 0;
+ header->next_segl_length = 0;
+ el = (void *) (header + 1);
+ el_end = el + BCE_ELEMENTS_PER_PAGE;
+
+ if (out) {
+ out->next = kmalloc(sizeof(struct bce_segment_list_element_hostinfo), GFP_KERNEL);
+ out = out->next;
+ } else {
+ out_root = out = kmalloc(sizeof(struct bce_segment_list_element_hostinfo), GFP_KERNEL);
+ }
+ out->page_start = (void *) ptr;
+ out->page_count = 1;
+ out->dma_start = DMA_MAPPING_ERROR;
+ out->next = NULL;
+ }
+ pptr = ptr;
+ }
+ el->addr = sg->dma_address;
+ el->length = sg->length;
+ header->data_size += el->length;
+ }
+
+ /* DMA map */
+ out = out_root;
+ pout = NULL;
+ while (out) {
+ out->dma_start = dma_map_single(dev, out->page_start, out->page_count * PAGE_SIZE, DMA_TO_DEVICE);
+ if (dma_mapping_error(dev, out->dma_start))
+ goto error;
+ if (pout) {
+ header = pout->page_start;
+ header->next_segl_addr = out->dma_start;
+ header->next_segl_length = out->page_count * PAGE_SIZE;
+ }
+ pout = out;
+ out = out->next;
+ }
+ return out_root;
+
+ error:
+ bce_unmap_segement_list(dev, out_root);
+ return NULL;
+}
+
+static void bce_unmap_segement_list(struct device *dev, struct bce_segment_list_element_hostinfo *list)
+{
+ struct bce_segment_list_element_hostinfo *next;
+ while (list) {
+ if (list->dma_start != DMA_MAPPING_ERROR)
+ dma_unmap_single(dev, list->dma_start, list->page_count * PAGE_SIZE, DMA_TO_DEVICE);
+ next = list->next;
+ kfree(list);
+ list = next;
+ }
+}
+
+int bce_set_submission_buf(struct bce_qe_submission *element, struct bce_dma_buffer *buf, size_t offset, size_t length)
+{
+ struct bce_segment_list_element_hostinfo *seg;
+ struct bce_segment_list_header *seg_header;
+
+ seg = buf->seglist_hostinfo;
+ if (!seg) {
+ element->addr = buf->scatterlist.sgl->dma_address + offset;
+ element->length = length;
+ element->segl_addr = 0;
+ element->segl_length = 0;
+ return 0;
+ }
+
+ while (seg) {
+ seg_header = seg->page_start;
+ if (offset <= seg_header->data_size)
+ break;
+ offset -= seg_header->data_size;
+ seg = seg->next;
+ }
+ if (!seg)
+ return -EINVAL;
+ element->addr = offset;
+ element->length = buf->scatterlist.sgl->dma_length;
+ element->segl_addr = seg->dma_start;
+ element->segl_length = seg->page_count * PAGE_SIZE;
+ return 0;
+}
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/queue_dma.h b/drivers/staging/apple-bce/queue_dma.h
new file mode 100644
index 000000000..f8a57e50e
--- /dev/null
+++ b/drivers/staging/apple-bce/queue_dma.h
@@ -0,0 +1,50 @@
+#ifndef BCE_QUEUE_DMA_H
+#define BCE_QUEUE_DMA_H
+
+#include <linux/pci.h>
+
+struct bce_qe_submission;
+
+struct bce_segment_list_header {
+ u64 element_count;
+ u64 data_size;
+
+ u64 next_segl_addr;
+ u64 next_segl_length;
+};
+struct bce_segment_list_element {
+ u64 addr;
+ u64 length;
+};
+
+struct bce_segment_list_element_hostinfo {
+ struct bce_segment_list_element_hostinfo *next;
+ void *page_start;
+ size_t page_count;
+ dma_addr_t dma_start;
+};
+
+
+struct bce_dma_buffer {
+ enum dma_data_direction direction;
+ struct sg_table scatterlist;
+ struct bce_segment_list_element_hostinfo *seglist_hostinfo;
+};
+
+/* NOTE: Takes ownership of the sg_table if it succeeds. Ownership is not transferred on failure. */
+int bce_map_dma_buffer(struct device *dev, struct bce_dma_buffer *buf, struct sg_table scatterlist,
+ enum dma_data_direction dir);
+
+/* Creates a buffer from virtual memory (vmalloc) */
+int bce_map_dma_buffer_vm(struct device *dev, struct bce_dma_buffer *buf, void *data, size_t len,
+ enum dma_data_direction dir);
+
+/* Creates a buffer from kernel memory (kmalloc) */
+int bce_map_dma_buffer_km(struct device *dev, struct bce_dma_buffer *buf, void *data, size_t len,
+ enum dma_data_direction dir);
+
+void bce_unmap_dma_buffer(struct device *dev, struct bce_dma_buffer *buf);
+
+int bce_set_submission_buf(struct bce_qe_submission *element, struct bce_dma_buffer *buf, size_t offset, size_t length);
+
+#endif //BCE_QUEUE_DMA_H
diff --git a/drivers/staging/apple-bce/vhci/command.h b/drivers/staging/apple-bce/vhci/command.h
new file mode 100644
index 000000000..26619e0bc
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/command.h
@@ -0,0 +1,204 @@
+#ifndef BCE_VHCI_COMMAND_H
+#define BCE_VHCI_COMMAND_H
+
+#include "queue.h"
+#include <linux/jiffies.h>
+#include <linux/usb.h>
+
+#define BCE_VHCI_CMD_TIMEOUT_SHORT msecs_to_jiffies(2000)
+#define BCE_VHCI_CMD_TIMEOUT_LONG msecs_to_jiffies(30000)
+
+#define BCE_VHCI_BULK_MAX_ACTIVE_URBS_POW2 2
+#define BCE_VHCI_BULK_MAX_ACTIVE_URBS (1 << BCE_VHCI_BULK_MAX_ACTIVE_URBS_POW2)
+
+typedef u8 bce_vhci_port_t;
+typedef u8 bce_vhci_device_t;
+
+enum bce_vhci_command {
+ BCE_VHCI_CMD_CONTROLLER_ENABLE = 1,
+ BCE_VHCI_CMD_CONTROLLER_DISABLE = 2,
+ BCE_VHCI_CMD_CONTROLLER_START = 3,
+ BCE_VHCI_CMD_CONTROLLER_PAUSE = 4,
+
+ BCE_VHCI_CMD_PORT_POWER_ON = 0x10,
+ BCE_VHCI_CMD_PORT_POWER_OFF = 0x11,
+ BCE_VHCI_CMD_PORT_RESUME = 0x12,
+ BCE_VHCI_CMD_PORT_SUSPEND = 0x13,
+ BCE_VHCI_CMD_PORT_RESET = 0x14,
+ BCE_VHCI_CMD_PORT_DISABLE = 0x15,
+ BCE_VHCI_CMD_PORT_STATUS = 0x16,
+
+ BCE_VHCI_CMD_DEVICE_CREATE = 0x30,
+ BCE_VHCI_CMD_DEVICE_DESTROY = 0x31,
+
+ BCE_VHCI_CMD_ENDPOINT_CREATE = 0x40,
+ BCE_VHCI_CMD_ENDPOINT_DESTROY = 0x41,
+ BCE_VHCI_CMD_ENDPOINT_SET_STATE = 0x42,
+ BCE_VHCI_CMD_ENDPOINT_RESET = 0x44,
+
+ /* Device to host only */
+ BCE_VHCI_CMD_ENDPOINT_REQUEST_STATE = 0x43,
+ BCE_VHCI_CMD_TRANSFER_REQUEST = 0x1000,
+ BCE_VHCI_CMD_CONTROL_TRANSFER_STATUS = 0x1005
+};
+
+enum bce_vhci_endpoint_state {
+ BCE_VHCI_ENDPOINT_ACTIVE = 0,
+ BCE_VHCI_ENDPOINT_PAUSED = 1,
+ BCE_VHCI_ENDPOINT_STALLED = 2
+};
+
+static inline int bce_vhci_cmd_controller_enable(struct bce_vhci_command_queue *q, u8 busNum, u16 *portMask)
+{
+ int status;
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_CONTROLLER_ENABLE;
+ cmd.param1 = 0x7100u | busNum;
+ status = bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+ if (!status)
+ *portMask = (u16) res.param2;
+ return status;
+}
+static inline int bce_vhci_cmd_controller_disable(struct bce_vhci_command_queue *q)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_CONTROLLER_DISABLE;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+static inline int bce_vhci_cmd_controller_start(struct bce_vhci_command_queue *q)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_CONTROLLER_START;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+static inline int bce_vhci_cmd_controller_pause(struct bce_vhci_command_queue *q)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_CONTROLLER_PAUSE;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+
+static inline int bce_vhci_cmd_port_power_on(struct bce_vhci_command_queue *q, bce_vhci_port_t port)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_POWER_ON;
+ cmd.param1 = port;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_port_power_off(struct bce_vhci_command_queue *q, bce_vhci_port_t port)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_POWER_OFF;
+ cmd.param1 = port;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_port_resume(struct bce_vhci_command_queue *q, bce_vhci_port_t port)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_RESUME;
+ cmd.param1 = port;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+static inline int bce_vhci_cmd_port_suspend(struct bce_vhci_command_queue *q, bce_vhci_port_t port)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_SUSPEND;
+ cmd.param1 = port;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+static inline int bce_vhci_cmd_port_reset(struct bce_vhci_command_queue *q, bce_vhci_port_t port, u32 timeout)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_RESET;
+ cmd.param1 = port;
+ cmd.param2 = timeout;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_port_disable(struct bce_vhci_command_queue *q, bce_vhci_port_t port)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_DISABLE;
+ cmd.param1 = port;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_port_status(struct bce_vhci_command_queue *q, bce_vhci_port_t port,
+ u32 clearFlags, u32 *resStatus)
+{
+ int status;
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_PORT_STATUS;
+ cmd.param1 = port;
+ cmd.param2 = clearFlags & 0x560000;
+ status = bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+ if (status >= 0)
+ *resStatus = (u32) res.param2;
+ return status;
+}
+
+static inline int bce_vhci_cmd_device_create(struct bce_vhci_command_queue *q, bce_vhci_port_t port,
+ bce_vhci_device_t *dev)
+{
+ int status;
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_DEVICE_CREATE;
+ cmd.param1 = port;
+ status = bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+ if (!status)
+ *dev = (bce_vhci_device_t) res.param2;
+ return status;
+}
+static inline int bce_vhci_cmd_device_destroy(struct bce_vhci_command_queue *q, bce_vhci_device_t dev)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_DEVICE_DESTROY;
+ cmd.param1 = dev;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_LONG);
+}
+
+static inline int bce_vhci_cmd_endpoint_create(struct bce_vhci_command_queue *q, bce_vhci_device_t dev,
+ struct usb_endpoint_descriptor *desc)
+{
+ struct bce_vhci_message cmd, res;
+ int endpoint_type = usb_endpoint_type(desc);
+ int maxp = usb_endpoint_maxp(desc);
+ int maxp_burst = usb_endpoint_maxp_mult(desc) * maxp;
+ u8 max_active_requests_pow2 = 0;
+ cmd.cmd = BCE_VHCI_CMD_ENDPOINT_CREATE;
+ cmd.param1 = dev | ((desc->bEndpointAddress & 0x8Fu) << 8);
+ if (endpoint_type == USB_ENDPOINT_XFER_BULK)
+ max_active_requests_pow2 = BCE_VHCI_BULK_MAX_ACTIVE_URBS_POW2;
+ cmd.param2 = endpoint_type | ((max_active_requests_pow2 & 0xf) << 4) | (maxp << 16) | ((u64) maxp_burst << 32);
+ if (endpoint_type == USB_ENDPOINT_XFER_INT)
+ cmd.param2 |= (desc->bInterval - 1) << 8;
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_endpoint_destroy(struct bce_vhci_command_queue *q, bce_vhci_device_t dev, u8 endpoint)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_ENDPOINT_DESTROY;
+ cmd.param1 = dev | (endpoint << 8);
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+static inline int bce_vhci_cmd_endpoint_set_state(struct bce_vhci_command_queue *q, bce_vhci_device_t dev, u8 endpoint,
+ enum bce_vhci_endpoint_state newState, enum bce_vhci_endpoint_state *retState)
+{
+ int status;
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_ENDPOINT_SET_STATE;
+ cmd.param1 = dev | (endpoint << 8);
+ cmd.param2 = (u64) newState;
+ status = bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+ if (status != BCE_VHCI_INTERNAL_ERROR && status != BCE_VHCI_NO_POWER)
+ *retState = (enum bce_vhci_endpoint_state) res.param2;
+ return status;
+}
+static inline int bce_vhci_cmd_endpoint_reset(struct bce_vhci_command_queue *q, bce_vhci_device_t dev, u8 endpoint)
+{
+ struct bce_vhci_message cmd, res;
+ cmd.cmd = BCE_VHCI_CMD_ENDPOINT_RESET;
+ cmd.param1 = dev | (endpoint << 8);
+ return bce_vhci_command_queue_execute(q, &cmd, &res, BCE_VHCI_CMD_TIMEOUT_SHORT);
+}
+
+
+#endif //BCE_VHCI_COMMAND_H
diff --git a/drivers/staging/apple-bce/vhci/queue.c b/drivers/staging/apple-bce/vhci/queue.c
new file mode 100644
index 000000000..7b0b50271
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/queue.c
@@ -0,0 +1,268 @@
+#include "queue.h"
+#include "vhci.h"
+#include "../apple_bce.h"
+
+
+static void bce_vhci_message_queue_completion(struct bce_queue_sq *sq);
+
+int bce_vhci_message_queue_create(struct bce_vhci *vhci, struct bce_vhci_message_queue *ret, const char *name)
+{
+ int status;
+ ret->cq = bce_create_cq(vhci->dev, VHCI_EVENT_QUEUE_EL_COUNT);
+ if (!ret->cq)
+ return -EINVAL;
+ ret->sq = bce_create_sq(vhci->dev, ret->cq, name, VHCI_EVENT_QUEUE_EL_COUNT, DMA_TO_DEVICE,
+ bce_vhci_message_queue_completion, ret);
+ if (!ret->sq) {
+ status = -EINVAL;
+ goto fail_cq;
+ }
+ ret->data = dma_alloc_coherent(&vhci->dev->pci->dev, sizeof(struct bce_vhci_message) * VHCI_EVENT_QUEUE_EL_COUNT,
+ &ret->dma_addr, GFP_KERNEL);
+ if (!ret->data) {
+ status = -EINVAL;
+ goto fail_sq;
+ }
+ return 0;
+
+fail_sq:
+ bce_destroy_sq(vhci->dev, ret->sq);
+ ret->sq = NULL;
+fail_cq:
+ bce_destroy_cq(vhci->dev, ret->cq);
+ ret->cq = NULL;
+ return status;
+}
+
+void bce_vhci_message_queue_destroy(struct bce_vhci *vhci, struct bce_vhci_message_queue *q)
+{
+ if (!q->cq)
+ return;
+ dma_free_coherent(&vhci->dev->pci->dev, sizeof(struct bce_vhci_message) * VHCI_EVENT_QUEUE_EL_COUNT,
+ q->data, q->dma_addr);
+ bce_destroy_sq(vhci->dev, q->sq);
+ bce_destroy_cq(vhci->dev, q->cq);
+}
+
+void bce_vhci_message_queue_write(struct bce_vhci_message_queue *q, struct bce_vhci_message *req)
+{
+ int sidx;
+ struct bce_qe_submission *s;
+ sidx = q->sq->tail;
+ s = bce_next_submission(q->sq);
+ pr_debug("bce-vhci: Send message: %x s=%x p1=%x p2=%llx\n", req->cmd, req->status, req->param1, req->param2);
+ q->data[sidx] = *req;
+ bce_set_submission_single(s, q->dma_addr + sizeof(struct bce_vhci_message) * sidx,
+ sizeof(struct bce_vhci_message));
+ bce_submit_to_device(q->sq);
+}
+
+static void bce_vhci_message_queue_completion(struct bce_queue_sq *sq)
+{
+ while (bce_next_completion(sq))
+ bce_notify_submission_complete(sq);
+}
+
+
+
+static void bce_vhci_event_queue_completion(struct bce_queue_sq *sq);
+
+int __bce_vhci_event_queue_create(struct bce_vhci *vhci, struct bce_vhci_event_queue *ret, const char *name,
+ bce_sq_completion compl)
+{
+ ret->vhci = vhci;
+
+ ret->sq = bce_create_sq(vhci->dev, vhci->ev_cq, name, VHCI_EVENT_QUEUE_EL_COUNT, DMA_FROM_DEVICE, compl, ret);
+ if (!ret->sq)
+ return -EINVAL;
+ ret->data = dma_alloc_coherent(&vhci->dev->pci->dev, sizeof(struct bce_vhci_message) * VHCI_EVENT_QUEUE_EL_COUNT,
+ &ret->dma_addr, GFP_KERNEL);
+ if (!ret->data) {
+ bce_destroy_sq(vhci->dev, ret->sq);
+ ret->sq = NULL;
+ return -EINVAL;
+ }
+
+ init_completion(&ret->queue_empty_completion);
+ bce_vhci_event_queue_submit_pending(ret, VHCI_EVENT_PENDING_COUNT);
+ return 0;
+}
+
+int bce_vhci_event_queue_create(struct bce_vhci *vhci, struct bce_vhci_event_queue *ret, const char *name,
+ bce_vhci_event_queue_callback cb)
+{
+ ret->cb = cb;
+ return __bce_vhci_event_queue_create(vhci, ret, name, bce_vhci_event_queue_completion);
+}
+
+void bce_vhci_event_queue_destroy(struct bce_vhci *vhci, struct bce_vhci_event_queue *q)
+{
+ if (!q->sq)
+ return;
+ dma_free_coherent(&vhci->dev->pci->dev, sizeof(struct bce_vhci_message) * VHCI_EVENT_QUEUE_EL_COUNT,
+ q->data, q->dma_addr);
+ bce_destroy_sq(vhci->dev, q->sq);
+}
+
+static void bce_vhci_event_queue_completion(struct bce_queue_sq *sq)
+{
+ struct bce_sq_completion_data *cd;
+ struct bce_vhci_event_queue *ev = sq->userdata;
+ struct bce_vhci_message *msg;
+ size_t cnt = 0;
+
+ while ((cd = bce_next_completion(sq))) {
+ if (cd->status == BCE_COMPLETION_ABORTED) { /* We flushed the queue */
+ bce_notify_submission_complete(sq);
+ continue;
+ }
+ msg = &ev->data[sq->head];
+ pr_debug("bce-vhci: Got event: %x s=%x p1=%x p2=%llx\n", msg->cmd, msg->status, msg->param1, msg->param2);
+ ev->cb(ev, msg);
+
+ bce_notify_submission_complete(sq);
+ ++cnt;
+ }
+ bce_vhci_event_queue_submit_pending(ev, cnt);
+ if (atomic_read(&sq->available_commands) == sq->el_count - 1)
+ complete(&ev->queue_empty_completion);
+}
+
+void bce_vhci_event_queue_submit_pending(struct bce_vhci_event_queue *q, size_t count)
+{
+ int idx;
+ struct bce_qe_submission *s;
+ while (count--) {
+ if (bce_reserve_submission(q->sq, NULL)) {
+ pr_err("bce-vhci: Failed to reserve an event queue submission\n");
+ break;
+ }
+ idx = q->sq->tail;
+ s = bce_next_submission(q->sq);
+ bce_set_submission_single(s,
+ q->dma_addr + idx * sizeof(struct bce_vhci_message), sizeof(struct bce_vhci_message));
+ }
+ bce_submit_to_device(q->sq);
+}
+
+void bce_vhci_event_queue_pause(struct bce_vhci_event_queue *q)
+{
+ unsigned long timeout;
+ reinit_completion(&q->queue_empty_completion);
+ if (bce_cmd_flush_memory_queue(q->vhci->dev->cmd_cmdq, q->sq->qid))
+ pr_warn("bce-vhci: failed to flush event queue\n");
+ timeout = msecs_to_jiffies(5000);
+ while (atomic_read(&q->sq->available_commands) != q->sq->el_count - 1) {
+ timeout = wait_for_completion_timeout(&q->queue_empty_completion, timeout);
+ if (timeout == 0) {
+ pr_err("bce-vhci: waiting for queue to be flushed timed out\n");
+ break;
+ }
+ }
+}
+
+void bce_vhci_event_queue_resume(struct bce_vhci_event_queue *q)
+{
+ if (atomic_read(&q->sq->available_commands) != q->sq->el_count - 1) {
+ pr_err("bce-vhci: resume of a queue with pending submissions\n");
+ return;
+ }
+ bce_vhci_event_queue_submit_pending(q, VHCI_EVENT_PENDING_COUNT);
+}
+
+void bce_vhci_command_queue_create(struct bce_vhci_command_queue *ret, struct bce_vhci_message_queue *mq)
+{
+ ret->mq = mq;
+ ret->completion.result = NULL;
+ init_completion(&ret->completion.completion);
+ spin_lock_init(&ret->completion_lock);
+ mutex_init(&ret->mutex);
+}
+
+void bce_vhci_command_queue_destroy(struct bce_vhci_command_queue *cq)
+{
+ spin_lock(&cq->completion_lock);
+ if (cq->completion.result) {
+ memset(cq->completion.result, 0, sizeof(struct bce_vhci_message));
+ cq->completion.result->status = BCE_VHCI_ABORT;
+ complete(&cq->completion.completion);
+ cq->completion.result = NULL;
+ }
+ spin_unlock(&cq->completion_lock);
+ mutex_lock(&cq->mutex);
+ mutex_unlock(&cq->mutex);
+ mutex_destroy(&cq->mutex);
+}
+
+void bce_vhci_command_queue_deliver_completion(struct bce_vhci_command_queue *cq, struct bce_vhci_message *msg)
+{
+ struct bce_vhci_command_queue_completion *c = &cq->completion;
+
+ spin_lock(&cq->completion_lock);
+ if (c->result) {
+ *c->result = *msg;
+ complete(&c->completion);
+ c->result = NULL;
+ }
+ spin_unlock(&cq->completion_lock);
+}
+
+static int __bce_vhci_command_queue_execute(struct bce_vhci_command_queue *cq, struct bce_vhci_message *req,
+ struct bce_vhci_message *res, unsigned long timeout)
+{
+ int status;
+ struct bce_vhci_command_queue_completion *c;
+ struct bce_vhci_message creq;
+ c = &cq->completion;
+
+ if ((status = bce_reserve_submission(cq->mq->sq, &timeout)))
+ return status;
+
+ spin_lock(&cq->completion_lock);
+ c->result = res;
+ reinit_completion(&c->completion);
+ spin_unlock(&cq->completion_lock);
+
+ bce_vhci_message_queue_write(cq->mq, req);
+
+ if (!wait_for_completion_timeout(&c->completion, timeout)) {
+ /* we ran out of time, send cancellation */
+ pr_debug("bce-vhci: command timed out req=%x\n", req->cmd);
+ if ((status = bce_reserve_submission(cq->mq->sq, &timeout)))
+ return status;
+
+ creq = *req;
+ creq.cmd |= 0x4000;
+ bce_vhci_message_queue_write(cq->mq, &creq);
+
+ if (!wait_for_completion_timeout(&c->completion, 1000)) {
+ pr_err("bce-vhci: Possible desync, cmd cancel timed out\n");
+
+ spin_lock(&cq->completion_lock);
+ c->result = NULL;
+ spin_unlock(&cq->completion_lock);
+ return -ETIMEDOUT;
+ }
+ if ((res->cmd & ~0x8000) == creq.cmd)
+ return -ETIMEDOUT;
+ /* reply for the previous command most likely arrived */
+ }
+
+ if ((res->cmd & ~0x8000) != req->cmd) {
+ pr_err("bce-vhci: Possible desync, cmd reply mismatch req=%x, res=%x\n", req->cmd, res->cmd);
+ return -EIO;
+ }
+ if (res->status == BCE_VHCI_SUCCESS)
+ return 0;
+ return res->status;
+}
+
+int bce_vhci_command_queue_execute(struct bce_vhci_command_queue *cq, struct bce_vhci_message *req,
+ struct bce_vhci_message *res, unsigned long timeout)
+{
+ int status;
+ mutex_lock(&cq->mutex);
+ status = __bce_vhci_command_queue_execute(cq, req, res, timeout);
+ mutex_unlock(&cq->mutex);
+ return status;
+}
diff --git a/drivers/staging/apple-bce/vhci/queue.h b/drivers/staging/apple-bce/vhci/queue.h
new file mode 100644
index 000000000..adb705b6b
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/queue.h
@@ -0,0 +1,76 @@
+#ifndef BCE_VHCI_QUEUE_H
+#define BCE_VHCI_QUEUE_H
+
+#include <linux/completion.h>
+#include "../queue.h"
+
+#define VHCI_EVENT_QUEUE_EL_COUNT 256
+#define VHCI_EVENT_PENDING_COUNT 32
+
+struct bce_vhci;
+struct bce_vhci_event_queue;
+
+enum bce_vhci_message_status {
+ BCE_VHCI_SUCCESS = 1,
+ BCE_VHCI_ERROR = 2,
+ BCE_VHCI_USB_PIPE_STALL = 3,
+ BCE_VHCI_ABORT = 4,
+ BCE_VHCI_BAD_ARGUMENT = 5,
+ BCE_VHCI_OVERRUN = 6,
+ BCE_VHCI_INTERNAL_ERROR = 7,
+ BCE_VHCI_NO_POWER = 8,
+ BCE_VHCI_UNSUPPORTED = 9
+};
+struct bce_vhci_message {
+ u16 cmd;
+ u16 status; // bce_vhci_message_status
+ u32 param1;
+ u64 param2;
+};
+
+struct bce_vhci_message_queue {
+ struct bce_queue_cq *cq;
+ struct bce_queue_sq *sq;
+ struct bce_vhci_message *data;
+ dma_addr_t dma_addr;
+};
+typedef void (*bce_vhci_event_queue_callback)(struct bce_vhci_event_queue *q, struct bce_vhci_message *msg);
+struct bce_vhci_event_queue {
+ struct bce_vhci *vhci;
+ struct bce_queue_sq *sq;
+ struct bce_vhci_message *data;
+ dma_addr_t dma_addr;
+ bce_vhci_event_queue_callback cb;
+ struct completion queue_empty_completion;
+};
+struct bce_vhci_command_queue_completion {
+ struct bce_vhci_message *result;
+ struct completion completion;
+};
+struct bce_vhci_command_queue {
+ struct bce_vhci_message_queue *mq;
+ struct bce_vhci_command_queue_completion completion;
+ struct spinlock completion_lock;
+ struct mutex mutex;
+};
+
+int bce_vhci_message_queue_create(struct bce_vhci *vhci, struct bce_vhci_message_queue *ret, const char *name);
+void bce_vhci_message_queue_destroy(struct bce_vhci *vhci, struct bce_vhci_message_queue *q);
+void bce_vhci_message_queue_write(struct bce_vhci_message_queue *q, struct bce_vhci_message *req);
+
+int __bce_vhci_event_queue_create(struct bce_vhci *vhci, struct bce_vhci_event_queue *ret, const char *name,
+ bce_sq_completion compl);
+int bce_vhci_event_queue_create(struct bce_vhci *vhci, struct bce_vhci_event_queue *ret, const char *name,
+ bce_vhci_event_queue_callback cb);
+void bce_vhci_event_queue_destroy(struct bce_vhci *vhci, struct bce_vhci_event_queue *q);
+void bce_vhci_event_queue_submit_pending(struct bce_vhci_event_queue *q, size_t count);
+void bce_vhci_event_queue_pause(struct bce_vhci_event_queue *q);
+void bce_vhci_event_queue_resume(struct bce_vhci_event_queue *q);
+
+void bce_vhci_command_queue_create(struct bce_vhci_command_queue *ret, struct bce_vhci_message_queue *mq);
+void bce_vhci_command_queue_destroy(struct bce_vhci_command_queue *cq);
+int bce_vhci_command_queue_execute(struct bce_vhci_command_queue *cq, struct bce_vhci_message *req,
+ struct bce_vhci_message *res, unsigned long timeout);
+void bce_vhci_command_queue_deliver_completion(struct bce_vhci_command_queue *cq, struct bce_vhci_message *msg);
+
+#endif //BCE_VHCI_QUEUE_H
diff --git a/drivers/staging/apple-bce/vhci/transfer.c b/drivers/staging/apple-bce/vhci/transfer.c
new file mode 100644
index 000000000..8226363d6
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/transfer.c
@@ -0,0 +1,661 @@
+#include "transfer.h"
+#include "../queue.h"
+#include "vhci.h"
+#include "../apple_bce.h"
+#include <linux/usb/hcd.h>
+
+static void bce_vhci_transfer_queue_completion(struct bce_queue_sq *sq);
+static void bce_vhci_transfer_queue_giveback(struct bce_vhci_transfer_queue *q);
+static void bce_vhci_transfer_queue_remove_pending(struct bce_vhci_transfer_queue *q);
+
+static int bce_vhci_urb_init(struct bce_vhci_urb *vurb);
+static int bce_vhci_urb_update(struct bce_vhci_urb *urb, struct bce_vhci_message *msg);
+static int bce_vhci_urb_transfer_completion(struct bce_vhci_urb *urb, struct bce_sq_completion_data *c);
+
+static void bce_vhci_transfer_queue_reset_w(struct work_struct *work);
+
+void bce_vhci_create_transfer_queue(struct bce_vhci *vhci, struct bce_vhci_transfer_queue *q,
+ struct usb_host_endpoint *endp, bce_vhci_device_t dev_addr, enum dma_data_direction dir)
+{
+ char name[0x21];
+ INIT_LIST_HEAD(&q->evq);
+ INIT_LIST_HEAD(&q->giveback_urb_list);
+ spin_lock_init(&q->urb_lock);
+ mutex_init(&q->pause_lock);
+ q->vhci = vhci;
+ q->endp = endp;
+ q->dev_addr = dev_addr;
+ q->endp_addr = (u8) (endp->desc.bEndpointAddress & 0x8F);
+ q->state = BCE_VHCI_ENDPOINT_ACTIVE;
+ q->active = true;
+ q->stalled = false;
+ q->max_active_requests = 1;
+ if (usb_endpoint_type(&endp->desc) == USB_ENDPOINT_XFER_BULK)
+ q->max_active_requests = BCE_VHCI_BULK_MAX_ACTIVE_URBS;
+ q->remaining_active_requests = q->max_active_requests;
+ q->cq = bce_create_cq(vhci->dev, 0x100);
+ INIT_WORK(&q->w_reset, bce_vhci_transfer_queue_reset_w);
+ q->sq_in = NULL;
+ if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
+ snprintf(name, sizeof(name), "VHC1-%i-%02x", dev_addr, 0x80 | usb_endpoint_num(&endp->desc));
+ q->sq_in = bce_create_sq(vhci->dev, q->cq, name, 0x100, DMA_FROM_DEVICE,
+ bce_vhci_transfer_queue_completion, q);
+ }
+ q->sq_out = NULL;
+ if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) {
+ snprintf(name, sizeof(name), "VHC1-%i-%02x", dev_addr, usb_endpoint_num(&endp->desc));
+ q->sq_out = bce_create_sq(vhci->dev, q->cq, name, 0x100, DMA_TO_DEVICE,
+ bce_vhci_transfer_queue_completion, q);
+ }
+}
+
+void bce_vhci_destroy_transfer_queue(struct bce_vhci *vhci, struct bce_vhci_transfer_queue *q)
+{
+ bce_vhci_transfer_queue_giveback(q);
+ bce_vhci_transfer_queue_remove_pending(q);
+ if (q->sq_in)
+ bce_destroy_sq(vhci->dev, q->sq_in);
+ if (q->sq_out)
+ bce_destroy_sq(vhci->dev, q->sq_out);
+ bce_destroy_cq(vhci->dev, q->cq);
+}
+
+static inline bool bce_vhci_transfer_queue_can_init_urb(struct bce_vhci_transfer_queue *q)
+{
+ return q->remaining_active_requests > 0;
+}
+
+static void bce_vhci_transfer_queue_defer_event(struct bce_vhci_transfer_queue *q, struct bce_vhci_message *msg)
+{
+ struct bce_vhci_list_message *lm;
+ lm = kmalloc(sizeof(struct bce_vhci_list_message), GFP_KERNEL);
+ INIT_LIST_HEAD(&lm->list);
+ lm->msg = *msg;
+ list_add_tail(&lm->list, &q->evq);
+}
+
+static void bce_vhci_transfer_queue_giveback(struct bce_vhci_transfer_queue *q)
+{
+ unsigned long flags;
+ struct urb *urb;
+ spin_lock_irqsave(&q->urb_lock, flags);
+ while (!list_empty(&q->giveback_urb_list)) {
+ urb = list_first_entry(&q->giveback_urb_list, struct urb, urb_list);
+ list_del(&urb->urb_list);
+
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ usb_hcd_giveback_urb(q->vhci->hcd, urb, urb->status);
+ spin_lock_irqsave(&q->urb_lock, flags);
+ }
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+}
+
+static void bce_vhci_transfer_queue_init_pending_urbs(struct bce_vhci_transfer_queue *q);
+
+static void bce_vhci_transfer_queue_deliver_pending(struct bce_vhci_transfer_queue *q)
+{
+ struct urb *urb;
+ struct bce_vhci_list_message *lm;
+
+ while (!list_empty(&q->endp->urb_list) && !list_empty(&q->evq)) {
+ urb = list_first_entry(&q->endp->urb_list, struct urb, urb_list);
+
+ lm = list_first_entry(&q->evq, struct bce_vhci_list_message, list);
+ if (bce_vhci_urb_update(urb->hcpriv, &lm->msg) == -EAGAIN)
+ break;
+ list_del(&lm->list);
+ kfree(lm);
+ }
+
+ /* some of the URBs could have been completed, so initialize more URBs if possible */
+ bce_vhci_transfer_queue_init_pending_urbs(q);
+}
+
+static void bce_vhci_transfer_queue_remove_pending(struct bce_vhci_transfer_queue *q)
+{
+ unsigned long flags;
+ struct bce_vhci_list_message *lm;
+ spin_lock_irqsave(&q->urb_lock, flags);
+ while (!list_empty(&q->evq)) {
+ lm = list_first_entry(&q->evq, struct bce_vhci_list_message, list);
+ list_del(&lm->list);
+ kfree(lm);
+ }
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+}
+
+void bce_vhci_transfer_queue_event(struct bce_vhci_transfer_queue *q, struct bce_vhci_message *msg)
+{
+ unsigned long flags;
+ struct bce_vhci_urb *turb;
+ struct urb *urb;
+ spin_lock_irqsave(&q->urb_lock, flags);
+ bce_vhci_transfer_queue_deliver_pending(q);
+
+ if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST &&
+ (!list_empty(&q->evq) || list_empty(&q->endp->urb_list))) {
+ bce_vhci_transfer_queue_defer_event(q, msg);
+ goto complete;
+ }
+ if (list_empty(&q->endp->urb_list)) {
+ pr_err("bce-vhci: [%02x] Unexpected transfer queue event\n", q->endp_addr);
+ goto complete;
+ }
+ urb = list_first_entry(&q->endp->urb_list, struct urb, urb_list);
+ turb = urb->hcpriv;
+ if (bce_vhci_urb_update(turb, msg) == -EAGAIN) {
+ bce_vhci_transfer_queue_defer_event(q, msg);
+ } else {
+ bce_vhci_transfer_queue_init_pending_urbs(q);
+ }
+
+complete:
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ bce_vhci_transfer_queue_giveback(q);
+}
+
+static void bce_vhci_transfer_queue_completion(struct bce_queue_sq *sq)
+{
+ unsigned long flags;
+ struct bce_sq_completion_data *c;
+ struct urb *urb;
+ struct bce_vhci_transfer_queue *q = sq->userdata;
+ spin_lock_irqsave(&q->urb_lock, flags);
+ while ((c = bce_next_completion(sq))) {
+ if (c->status == BCE_COMPLETION_ABORTED) { /* We flushed the queue */
+ pr_debug("bce-vhci: [%02x] Got an abort completion\n", q->endp_addr);
+ bce_notify_submission_complete(sq);
+ continue;
+ }
+ if (list_empty(&q->endp->urb_list)) {
+ pr_err("bce-vhci: [%02x] Got a completion while no requests are pending\n", q->endp_addr);
+ continue;
+ }
+ pr_debug("bce-vhci: [%02x] Got a transfer queue completion\n", q->endp_addr);
+ urb = list_first_entry(&q->endp->urb_list, struct urb, urb_list);
+ bce_vhci_urb_transfer_completion(urb->hcpriv, c);
+ bce_notify_submission_complete(sq);
+ }
+ bce_vhci_transfer_queue_deliver_pending(q);
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ bce_vhci_transfer_queue_giveback(q);
+}
+
+int bce_vhci_transfer_queue_do_pause(struct bce_vhci_transfer_queue *q)
+{
+ unsigned long flags;
+ int status;
+ u8 endp_addr = (u8) (q->endp->desc.bEndpointAddress & 0x8F);
+ spin_lock_irqsave(&q->urb_lock, flags);
+ q->active = false;
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ if (q->sq_out) {
+ pr_err("bce-vhci: Not implemented: wait for pending output requests\n");
+ }
+ bce_vhci_transfer_queue_remove_pending(q);
+ if ((status = bce_vhci_cmd_endpoint_set_state(
+ &q->vhci->cq, q->dev_addr, endp_addr, BCE_VHCI_ENDPOINT_PAUSED, &q->state)))
+ return status;
+ if (q->state != BCE_VHCI_ENDPOINT_PAUSED)
+ return -EINVAL;
+ if (q->sq_in)
+ bce_cmd_flush_memory_queue(q->vhci->dev->cmd_cmdq, (u16) q->sq_in->qid);
+ if (q->sq_out)
+ bce_cmd_flush_memory_queue(q->vhci->dev->cmd_cmdq, (u16) q->sq_out->qid);
+ return 0;
+}
+
+static void bce_vhci_urb_resume(struct bce_vhci_urb *urb);
+
+int bce_vhci_transfer_queue_do_resume(struct bce_vhci_transfer_queue *q)
+{
+ unsigned long flags;
+ int status;
+ struct urb *urb, *urbt;
+ struct bce_vhci_urb *vurb;
+ u8 endp_addr = (u8) (q->endp->desc.bEndpointAddress & 0x8F);
+ if ((status = bce_vhci_cmd_endpoint_set_state(
+ &q->vhci->cq, q->dev_addr, endp_addr, BCE_VHCI_ENDPOINT_ACTIVE, &q->state)))
+ return status;
+ if (q->state != BCE_VHCI_ENDPOINT_ACTIVE)
+ return -EINVAL;
+ spin_lock_irqsave(&q->urb_lock, flags);
+ q->active = true;
+ list_for_each_entry_safe(urb, urbt, &q->endp->urb_list, urb_list) {
+ vurb = urb->hcpriv;
+ if (vurb->state == BCE_VHCI_URB_INIT_PENDING) {
+ if (!bce_vhci_transfer_queue_can_init_urb(q))
+ break;
+ bce_vhci_urb_init(vurb);
+ } else {
+ bce_vhci_urb_resume(vurb);
+ }
+ }
+ bce_vhci_transfer_queue_deliver_pending(q);
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ return 0;
+}
+
+int bce_vhci_transfer_queue_pause(struct bce_vhci_transfer_queue *q, enum bce_vhci_pause_source src)
+{
+ int ret = 0;
+ mutex_lock(&q->pause_lock);
+ if ((q->paused_by & src) != src) {
+ if (!q->paused_by)
+ ret = bce_vhci_transfer_queue_do_pause(q);
+ if (!ret)
+ q->paused_by |= src;
+ }
+ mutex_unlock(&q->pause_lock);
+ return ret;
+}
+
+int bce_vhci_transfer_queue_resume(struct bce_vhci_transfer_queue *q, enum bce_vhci_pause_source src)
+{
+ int ret = 0;
+ mutex_lock(&q->pause_lock);
+ if (q->paused_by & src) {
+ if (!(q->paused_by & ~src))
+ ret = bce_vhci_transfer_queue_do_resume(q);
+ if (!ret)
+ q->paused_by &= ~src;
+ }
+ mutex_unlock(&q->pause_lock);
+ return ret;
+}
+
+static void bce_vhci_transfer_queue_reset_w(struct work_struct *work)
+{
+ unsigned long flags;
+ struct bce_vhci_transfer_queue *q = container_of(work, struct bce_vhci_transfer_queue, w_reset);
+
+ mutex_lock(&q->pause_lock);
+ spin_lock_irqsave(&q->urb_lock, flags);
+ if (!q->stalled) {
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ mutex_unlock(&q->pause_lock);
+ return;
+ }
+ q->active = false;
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ q->paused_by |= BCE_VHCI_PAUSE_INTERNAL_WQ;
+ bce_vhci_transfer_queue_remove_pending(q);
+ if (q->sq_in)
+ bce_cmd_flush_memory_queue(q->vhci->dev->cmd_cmdq, (u16) q->sq_in->qid);
+ if (q->sq_out)
+ bce_cmd_flush_memory_queue(q->vhci->dev->cmd_cmdq, (u16) q->sq_out->qid);
+ bce_vhci_cmd_endpoint_reset(&q->vhci->cq, q->dev_addr, (u8) (q->endp->desc.bEndpointAddress & 0x8F));
+ spin_lock_irqsave(&q->urb_lock, flags);
+ q->stalled = false;
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ mutex_unlock(&q->pause_lock);
+ bce_vhci_transfer_queue_resume(q, BCE_VHCI_PAUSE_INTERNAL_WQ);
+}
+
+void bce_vhci_transfer_queue_request_reset(struct bce_vhci_transfer_queue *q)
+{
+ queue_work(q->vhci->tq_state_wq, &q->w_reset);
+}
+
+static void bce_vhci_transfer_queue_init_pending_urbs(struct bce_vhci_transfer_queue *q)
+{
+ struct urb *urb, *urbt;
+ struct bce_vhci_urb *vurb;
+ list_for_each_entry_safe(urb, urbt, &q->endp->urb_list, urb_list) {
+ vurb = urb->hcpriv;
+ if (!bce_vhci_transfer_queue_can_init_urb(q))
+ break;
+ if (vurb->state == BCE_VHCI_URB_INIT_PENDING)
+ bce_vhci_urb_init(vurb);
+ }
+}
+
+
+
+static int bce_vhci_urb_data_start(struct bce_vhci_urb *urb, unsigned long *timeout);
+
+int bce_vhci_urb_create(struct bce_vhci_transfer_queue *q, struct urb *urb)
+{
+ unsigned long flags;
+ int status = 0;
+ struct bce_vhci_urb *vurb;
+ vurb = kzalloc(sizeof(struct bce_vhci_urb), GFP_KERNEL);
+ urb->hcpriv = vurb;
+
+ vurb->q = q;
+ vurb->urb = urb;
+ vurb->dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
+ vurb->is_control = (usb_endpoint_num(&urb->ep->desc) == 0);
+
+ spin_lock_irqsave(&q->urb_lock, flags);
+ status = usb_hcd_link_urb_to_ep(q->vhci->hcd, urb);
+ if (status) {
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ urb->hcpriv = NULL;
+ kfree(vurb);
+ return status;
+ }
+
+ if (q->active) {
+ if (bce_vhci_transfer_queue_can_init_urb(vurb->q))
+ status = bce_vhci_urb_init(vurb);
+ else
+ vurb->state = BCE_VHCI_URB_INIT_PENDING;
+ } else {
+ if (q->stalled)
+ bce_vhci_transfer_queue_request_reset(q);
+ vurb->state = BCE_VHCI_URB_INIT_PENDING;
+ }
+ if (status) {
+ usb_hcd_unlink_urb_from_ep(q->vhci->hcd, urb);
+ urb->hcpriv = NULL;
+ kfree(vurb);
+ } else {
+ bce_vhci_transfer_queue_deliver_pending(q);
+ }
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ pr_debug("bce-vhci: [%02x] URB enqueued (dir = %s, size = %i)\n", q->endp_addr,
+ usb_urb_dir_in(urb) ? "IN" : "OUT", urb->transfer_buffer_length);
+ return status;
+}
+
+static int bce_vhci_urb_init(struct bce_vhci_urb *vurb)
+{
+ int status = 0;
+
+ if (vurb->q->remaining_active_requests == 0) {
+ pr_err("bce-vhci: cannot init request (remaining_active_requests = 0)\n");
+ return -EINVAL;
+ }
+
+ if (vurb->is_control) {
+ vurb->state = BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_REQUEST;
+ } else {
+ status = bce_vhci_urb_data_start(vurb, NULL);
+ }
+
+ if (!status) {
+ --vurb->q->remaining_active_requests;
+ }
+ return status;
+}
+
+static void bce_vhci_urb_complete(struct bce_vhci_urb *urb, int status)
+{
+ struct bce_vhci_transfer_queue *q = urb->q;
+ struct bce_vhci *vhci = q->vhci;
+ struct urb *real_urb = urb->urb;
+ pr_debug("bce-vhci: [%02x] URB complete %i\n", q->endp_addr, status);
+ usb_hcd_unlink_urb_from_ep(vhci->hcd, real_urb);
+ real_urb->hcpriv = NULL;
+ real_urb->status = status;
+ if (urb->state != BCE_VHCI_URB_INIT_PENDING)
+ ++urb->q->remaining_active_requests;
+ kfree(urb);
+ list_add_tail(&real_urb->urb_list, &q->giveback_urb_list);
+}
+
+int bce_vhci_urb_request_cancel(struct bce_vhci_transfer_queue *q, struct urb *urb, int status)
+{
+ struct bce_vhci_urb *vurb;
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&q->urb_lock, flags);
+ if ((ret = usb_hcd_check_unlink_urb(q->vhci->hcd, urb, status))) {
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ return ret;
+ }
+
+ vurb = urb->hcpriv;
+ /* If the URB wasn't posted to the device yet, we can still remove it on the host without pausing the queue. */
+ if (vurb->state != BCE_VHCI_URB_INIT_PENDING) {
+ pr_debug("bce-vhci: [%02x] Cancelling URB\n", q->endp_addr);
+
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+ bce_vhci_transfer_queue_pause(q, BCE_VHCI_PAUSE_INTERNAL_WQ);
+ spin_lock_irqsave(&q->urb_lock, flags);
+
+ ++q->remaining_active_requests;
+ }
+
+ usb_hcd_unlink_urb_from_ep(q->vhci->hcd, urb);
+
+ spin_unlock_irqrestore(&q->urb_lock, flags);
+
+ usb_hcd_giveback_urb(q->vhci->hcd, urb, status);
+
+ if (vurb->state != BCE_VHCI_URB_INIT_PENDING)
+ bce_vhci_transfer_queue_resume(q, BCE_VHCI_PAUSE_INTERNAL_WQ);
+
+ kfree(vurb);
+
+ return 0;
+}
+
+static int bce_vhci_urb_data_transfer_in(struct bce_vhci_urb *urb, unsigned long *timeout)
+{
+ struct bce_vhci_message msg;
+ struct bce_qe_submission *s;
+ u32 tr_len;
+ int reservation1, reservation2 = -EFAULT;
+
+ pr_debug("bce-vhci: [%02x] DMA from device %llx %x\n", urb->q->endp_addr,
+ (u64) urb->urb->transfer_dma, urb->urb->transfer_buffer_length);
+
+ /* Reserve both a message and a submission, so we don't run into issues later. */
+ reservation1 = bce_reserve_submission(urb->q->vhci->msg_asynchronous.sq, timeout);
+ if (!reservation1)
+ reservation2 = bce_reserve_submission(urb->q->sq_in, timeout);
+ if (reservation1 || reservation2) {
+ pr_err("bce-vhci: Failed to reserve a submission for URB data transfer\n");
+ if (!reservation1)
+ bce_cancel_submission_reservation(urb->q->vhci->msg_asynchronous.sq);
+ return -ENOMEM;
+ }
+
+ urb->send_offset = urb->receive_offset;
+
+ tr_len = urb->urb->transfer_buffer_length - urb->send_offset;
+
+ spin_lock(&urb->q->vhci->msg_asynchronous_lock);
+ msg.cmd = BCE_VHCI_CMD_TRANSFER_REQUEST;
+ msg.status = 0;
+ msg.param1 = ((urb->urb->ep->desc.bEndpointAddress & 0x8Fu) << 8) | urb->q->dev_addr;
+ msg.param2 = tr_len;
+ bce_vhci_message_queue_write(&urb->q->vhci->msg_asynchronous, &msg);
+ spin_unlock(&urb->q->vhci->msg_asynchronous_lock);
+
+ s = bce_next_submission(urb->q->sq_in);
+ bce_set_submission_single(s, urb->urb->transfer_dma + urb->send_offset, tr_len);
+ bce_submit_to_device(urb->q->sq_in);
+
+ urb->state = BCE_VHCI_URB_WAITING_FOR_COMPLETION;
+ return 0;
+}
+
+static int bce_vhci_urb_data_start(struct bce_vhci_urb *urb, unsigned long *timeout)
+{
+ if (urb->dir == DMA_TO_DEVICE) {
+ if (urb->urb->transfer_buffer_length > 0)
+ urb->state = BCE_VHCI_URB_WAITING_FOR_TRANSFER_REQUEST;
+ else
+ urb->state = BCE_VHCI_URB_DATA_TRANSFER_COMPLETE;
+ return 0;
+ } else {
+ return bce_vhci_urb_data_transfer_in(urb, timeout);
+ }
+}
+
+static int bce_vhci_urb_send_out_data(struct bce_vhci_urb *urb, dma_addr_t addr, size_t size)
+{
+ struct bce_qe_submission *s;
+ unsigned long timeout = 0;
+ if (bce_reserve_submission(urb->q->sq_out, &timeout)) {
+ pr_err("bce-vhci: Failed to reserve a submission for URB data transfer\n");
+ return -EPIPE;
+ }
+
+ pr_debug("bce-vhci: [%02x] DMA to device %llx %lx\n", urb->q->endp_addr, (u64) addr, size);
+
+ s = bce_next_submission(urb->q->sq_out);
+ bce_set_submission_single(s, addr, size);
+ bce_submit_to_device(urb->q->sq_out);
+ return 0;
+}
+
+static int bce_vhci_urb_data_update(struct bce_vhci_urb *urb, struct bce_vhci_message *msg)
+{
+ u32 tr_len;
+ int status;
+ if (urb->state == BCE_VHCI_URB_WAITING_FOR_TRANSFER_REQUEST) {
+ if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST) {
+ tr_len = min(urb->urb->transfer_buffer_length - urb->send_offset, (u32) msg->param2);
+ if ((status = bce_vhci_urb_send_out_data(urb, urb->urb->transfer_dma + urb->send_offset, tr_len)))
+ return status;
+ urb->send_offset += tr_len;
+ urb->state = BCE_VHCI_URB_WAITING_FOR_COMPLETION;
+ return 0;
+ }
+ }
+
+ /* 0x1000 in out queues aren't really unexpected */
+ if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST && urb->q->sq_out != NULL)
+ return -EAGAIN;
+ pr_err("bce-vhci: [%02x] %s URB unexpected message (state = %x, msg: %x %x %x %llx)\n",
+ urb->q->endp_addr, (urb->is_control ? "Control (data update)" : "Data"), urb->state,
+ msg->cmd, msg->status, msg->param1, msg->param2);
+ return -EAGAIN;
+}
+
+static int bce_vhci_urb_data_transfer_completion(struct bce_vhci_urb *urb, struct bce_sq_completion_data *c)
+{
+ if (urb->state == BCE_VHCI_URB_WAITING_FOR_COMPLETION) {
+ urb->receive_offset += c->data_size;
+ if (urb->dir == DMA_FROM_DEVICE || urb->receive_offset >= urb->urb->transfer_buffer_length) {
+ urb->urb->actual_length = (u32) urb->receive_offset;
+ urb->state = BCE_VHCI_URB_DATA_TRANSFER_COMPLETE;
+ if (!urb->is_control) {
+ bce_vhci_urb_complete(urb, 0);
+ return -ENOENT;
+ }
+ }
+ } else {
+ pr_err("bce-vhci: [%02x] Data URB unexpected completion\n", urb->q->endp_addr);
+ }
+ return 0;
+}
+
+
+static int bce_vhci_urb_control_check_status(struct bce_vhci_urb *urb)
+{
+ struct bce_vhci_transfer_queue *q = urb->q;
+ if (urb->received_status == 0)
+ return 0;
+ if (urb->state == BCE_VHCI_URB_DATA_TRANSFER_COMPLETE ||
+ (urb->received_status != BCE_VHCI_SUCCESS && urb->state != BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_REQUEST &&
+ urb->state != BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_COMPLETION)) {
+ urb->state = BCE_VHCI_URB_CONTROL_COMPLETE;
+ if (urb->received_status != BCE_VHCI_SUCCESS) {
+ pr_err("bce-vhci: [%02x] URB failed: %x\n", urb->q->endp_addr, urb->received_status);
+ urb->q->active = false;
+ urb->q->stalled = true;
+ bce_vhci_urb_complete(urb, -EPIPE);
+ if (!list_empty(&q->endp->urb_list))
+ bce_vhci_transfer_queue_request_reset(q);
+ return -ENOENT;
+ }
+ bce_vhci_urb_complete(urb, 0);
+ return -ENOENT;
+ }
+ return 0;
+}
+
+static int bce_vhci_urb_control_update(struct bce_vhci_urb *urb, struct bce_vhci_message *msg)
+{
+ int status;
+ if (msg->cmd == BCE_VHCI_CMD_CONTROL_TRANSFER_STATUS) {
+ urb->received_status = msg->status;
+ return bce_vhci_urb_control_check_status(urb);
+ }
+
+ if (urb->state == BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_REQUEST) {
+ if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST) {
+ if (bce_vhci_urb_send_out_data(urb, urb->urb->setup_dma, sizeof(struct usb_ctrlrequest))) {
+ pr_err("bce-vhci: [%02x] Failed to start URB setup transfer\n", urb->q->endp_addr);
+ return 0; /* TODO: fail the URB? */
+ }
+ urb->state = BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_COMPLETION;
+ pr_debug("bce-vhci: [%02x] Sent setup %llx\n", urb->q->endp_addr, urb->urb->setup_dma);
+ return 0;
+ }
+ } else if (urb->state == BCE_VHCI_URB_WAITING_FOR_TRANSFER_REQUEST ||
+ urb->state == BCE_VHCI_URB_WAITING_FOR_COMPLETION) {
+ if ((status = bce_vhci_urb_data_update(urb, msg)))
+ return status;
+ return bce_vhci_urb_control_check_status(urb);
+ }
+
+ /* 0x1000 in out queues aren't really unexpected */
+ if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST && urb->q->sq_out != NULL)
+ return -EAGAIN;
+ pr_err("bce-vhci: [%02x] Control URB unexpected message (state = %x, msg: %x %x %x %llx)\n", urb->q->endp_addr,
+ urb->state, msg->cmd, msg->status, msg->param1, msg->param2);
+ return -EAGAIN;
+}
+
+static int bce_vhci_urb_control_transfer_completion(struct bce_vhci_urb *urb, struct bce_sq_completion_data *c)
+{
+ int status;
+ unsigned long timeout;
+
+ if (urb->state == BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_COMPLETION) {
+ if (c->data_size != sizeof(struct usb_ctrlrequest))
+ pr_err("bce-vhci: [%02x] transfer complete data size mistmatch for usb_ctrlrequest (%llx instead of %lx)\n",
+ urb->q->endp_addr, c->data_size, sizeof(struct usb_ctrlrequest));
+
+ timeout = 1000;
+ status = bce_vhci_urb_data_start(urb, &timeout);
+ if (status) {
+ bce_vhci_urb_complete(urb, status);
+ return -ENOENT;
+ }
+ return 0;
+ } else if (urb->state == BCE_VHCI_URB_WAITING_FOR_TRANSFER_REQUEST ||
+ urb->state == BCE_VHCI_URB_WAITING_FOR_COMPLETION) {
+ if ((status = bce_vhci_urb_data_transfer_completion(urb, c)))
+ return status;
+ return bce_vhci_urb_control_check_status(urb);
+ } else {
+ pr_err("bce-vhci: [%02x] Control URB unexpected completion (state = %x)\n", urb->q->endp_addr, urb->state);
+ }
+ return 0;
+}
+
+static int bce_vhci_urb_update(struct bce_vhci_urb *urb, struct bce_vhci_message *msg)
+{
+ if (urb->state == BCE_VHCI_URB_INIT_PENDING)
+ return -EAGAIN;
+ if (urb->is_control)
+ return bce_vhci_urb_control_update(urb, msg);
+ else
+ return bce_vhci_urb_data_update(urb, msg);
+}
+
+static int bce_vhci_urb_transfer_completion(struct bce_vhci_urb *urb, struct bce_sq_completion_data *c)
+{
+ if (urb->is_control)
+ return bce_vhci_urb_control_transfer_completion(urb, c);
+ else
+ return bce_vhci_urb_data_transfer_completion(urb, c);
+}
+
+static void bce_vhci_urb_resume(struct bce_vhci_urb *urb)
+{
+ int status = 0;
+ if (urb->state == BCE_VHCI_URB_WAITING_FOR_COMPLETION) {
+ status = bce_vhci_urb_data_transfer_in(urb, NULL);
+ }
+ if (status)
+ bce_vhci_urb_complete(urb, status);
+}
diff --git a/drivers/staging/apple-bce/vhci/transfer.h b/drivers/staging/apple-bce/vhci/transfer.h
new file mode 100644
index 000000000..89ecad6bc
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/transfer.h
@@ -0,0 +1,73 @@
+#ifndef BCEDRIVER_TRANSFER_H
+#define BCEDRIVER_TRANSFER_H
+
+#include <linux/usb.h>
+#include "queue.h"
+#include "command.h"
+#include "../queue.h"
+
+struct bce_vhci_list_message {
+ struct list_head list;
+ struct bce_vhci_message msg;
+};
+enum bce_vhci_pause_source {
+ BCE_VHCI_PAUSE_INTERNAL_WQ = 1,
+ BCE_VHCI_PAUSE_FIRMWARE = 2,
+ BCE_VHCI_PAUSE_SUSPEND = 4,
+ BCE_VHCI_PAUSE_SHUTDOWN = 8
+};
+struct bce_vhci_transfer_queue {
+ struct bce_vhci *vhci;
+ struct usb_host_endpoint *endp;
+ enum bce_vhci_endpoint_state state;
+ u32 max_active_requests, remaining_active_requests;
+ bool active, stalled;
+ u32 paused_by;
+ bce_vhci_device_t dev_addr;
+ u8 endp_addr;
+ struct bce_queue_cq *cq;
+ struct bce_queue_sq *sq_in;
+ struct bce_queue_sq *sq_out;
+ struct list_head evq;
+ struct spinlock urb_lock;
+ struct mutex pause_lock;
+ struct list_head giveback_urb_list;
+
+ struct work_struct w_reset;
+};
+enum bce_vhci_urb_state {
+ BCE_VHCI_URB_INIT_PENDING,
+
+ BCE_VHCI_URB_WAITING_FOR_TRANSFER_REQUEST,
+ BCE_VHCI_URB_WAITING_FOR_COMPLETION,
+ BCE_VHCI_URB_DATA_TRANSFER_COMPLETE,
+
+ BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_REQUEST,
+ BCE_VHCI_URB_CONTROL_WAITING_FOR_SETUP_COMPLETION,
+ BCE_VHCI_URB_CONTROL_COMPLETE
+};
+struct bce_vhci_urb {
+ struct urb *urb;
+ struct bce_vhci_transfer_queue *q;
+ enum dma_data_direction dir;
+ bool is_control;
+ enum bce_vhci_urb_state state;
+ int received_status;
+ u32 send_offset;
+ u32 receive_offset;
+};
+
+void bce_vhci_create_transfer_queue(struct bce_vhci *vhci, struct bce_vhci_transfer_queue *q,
+ struct usb_host_endpoint *endp, bce_vhci_device_t dev_addr, enum dma_data_direction dir);
+void bce_vhci_destroy_transfer_queue(struct bce_vhci *vhci, struct bce_vhci_transfer_queue *q);
+void bce_vhci_transfer_queue_event(struct bce_vhci_transfer_queue *q, struct bce_vhci_message *msg);
+int bce_vhci_transfer_queue_do_pause(struct bce_vhci_transfer_queue *q);
+int bce_vhci_transfer_queue_do_resume(struct bce_vhci_transfer_queue *q);
+int bce_vhci_transfer_queue_pause(struct bce_vhci_transfer_queue *q, enum bce_vhci_pause_source src);
+int bce_vhci_transfer_queue_resume(struct bce_vhci_transfer_queue *q, enum bce_vhci_pause_source src);
+void bce_vhci_transfer_queue_request_reset(struct bce_vhci_transfer_queue *q);
+
+int bce_vhci_urb_create(struct bce_vhci_transfer_queue *q, struct urb *urb);
+int bce_vhci_urb_request_cancel(struct bce_vhci_transfer_queue *q, struct urb *urb, int status);
+
+#endif //BCEDRIVER_TRANSFER_H
diff --git a/drivers/staging/apple-bce/vhci/vhci.c b/drivers/staging/apple-bce/vhci/vhci.c
new file mode 100644
index 000000000..eb26f5500
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/vhci.c
@@ -0,0 +1,759 @@
+#include "vhci.h"
+#include "../apple_bce.h"
+#include "command.h"
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
+#include <linux/module.h>
+#include <linux/version.h>
+
+static dev_t bce_vhci_chrdev;
+static struct class *bce_vhci_class;
+static const struct hc_driver bce_vhci_driver;
+static u16 bce_vhci_port_mask = U16_MAX;
+
+static int bce_vhci_create_event_queues(struct bce_vhci *vhci);
+static void bce_vhci_destroy_event_queues(struct bce_vhci *vhci);
+static int bce_vhci_create_message_queues(struct bce_vhci *vhci);
+static void bce_vhci_destroy_message_queues(struct bce_vhci *vhci);
+static void bce_vhci_handle_firmware_events_w(struct work_struct *ws);
+static void bce_vhci_firmware_event_completion(struct bce_queue_sq *sq);
+
+int bce_vhci_create(struct apple_bce_device *dev, struct bce_vhci *vhci)
+{
+ int status;
+
+ spin_lock_init(&vhci->hcd_spinlock);
+
+ vhci->dev = dev;
+
+ vhci->vdevt = bce_vhci_chrdev;
+ vhci->vdev = device_create(bce_vhci_class, dev->dev, vhci->vdevt, NULL, "bce-vhci");
+ if (IS_ERR_OR_NULL(vhci->vdev)) {
+ status = PTR_ERR(vhci->vdev);
+ goto fail_dev;
+ }
+
+ if ((status = bce_vhci_create_message_queues(vhci)))
+ goto fail_mq;
+ if ((status = bce_vhci_create_event_queues(vhci)))
+ goto fail_eq;
+
+ vhci->tq_state_wq = alloc_ordered_workqueue("bce-vhci-tq-state", 0);
+ INIT_WORK(&vhci->w_fw_events, bce_vhci_handle_firmware_events_w);
+
+ vhci->hcd = usb_create_hcd(&bce_vhci_driver, vhci->vdev, "bce-vhci");
+ if (!vhci->hcd) {
+ status = -ENOMEM;
+ goto fail_hcd;
+ }
+ vhci->hcd->self.sysdev = &dev->pci->dev;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,4,0)
+ vhci->hcd->self.uses_dma = 1;
+#endif
+ *((struct bce_vhci **) vhci->hcd->hcd_priv) = vhci;
+ vhci->hcd->speed = HCD_USB2;
+
+ if ((status = usb_add_hcd(vhci->hcd, 0, 0)))
+ goto fail_hcd;
+
+ return 0;
+
+fail_hcd:
+ bce_vhci_destroy_event_queues(vhci);
+fail_eq:
+ bce_vhci_destroy_message_queues(vhci);
+fail_mq:
+ device_destroy(bce_vhci_class, vhci->vdevt);
+fail_dev:
+ if (!status)
+ status = -EINVAL;
+ return status;
+}
+
+void bce_vhci_destroy(struct bce_vhci *vhci)
+{
+ usb_remove_hcd(vhci->hcd);
+ bce_vhci_destroy_event_queues(vhci);
+ bce_vhci_destroy_message_queues(vhci);
+ device_destroy(bce_vhci_class, vhci->vdevt);
+}
+
+struct bce_vhci *bce_vhci_from_hcd(struct usb_hcd *hcd)
+{
+ return *((struct bce_vhci **) hcd->hcd_priv);
+}
+
+int bce_vhci_start(struct usb_hcd *hcd)
+{
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ int status;
+ u16 port_mask = 0;
+ bce_vhci_port_t port_no = 0;
+ if ((status = bce_vhci_cmd_controller_enable(&vhci->cq, 1, &port_mask)))
+ return status;
+ vhci->port_mask = port_mask;
+ vhci->port_power_mask = 0;
+ if ((status = bce_vhci_cmd_controller_start(&vhci->cq)))
+ return status;
+ port_mask = vhci->port_mask;
+ while (port_mask) {
+ port_no += 1;
+ port_mask >>= 1;
+ }
+ vhci->port_count = port_no;
+ return 0;
+}
+
+void bce_vhci_stop(struct usb_hcd *hcd)
+{
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ bce_vhci_cmd_controller_disable(&vhci->cq);
+}
+
+static int bce_vhci_hub_status_data(struct usb_hcd *hcd, char *buf)
+{
+ return 0;
+}
+
+static int bce_vhci_reset_device(struct bce_vhci *vhci, int index, u16 timeout);
+
+static int bce_vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
+{
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ int status;
+ struct usb_hub_descriptor *hd;
+ struct usb_hub_status *hs;
+ struct usb_port_status *ps;
+ u32 port_status;
+ // pr_info("bce-vhci: bce_vhci_hub_control %x %i %i [bufl=%i]\n", typeReq, wValue, wIndex, wLength);
+ if (typeReq == GetHubDescriptor && wLength >= sizeof(struct usb_hub_descriptor)) {
+ hd = (struct usb_hub_descriptor *) buf;
+ memset(hd, 0, sizeof(*hd));
+ hd->bDescLength = sizeof(struct usb_hub_descriptor);
+ hd->bDescriptorType = USB_DT_HUB;
+ hd->bNbrPorts = (u8) vhci->port_count;
+ hd->wHubCharacteristics = HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_INDV_PORT_OCPM;
+ hd->bPwrOn2PwrGood = 0;
+ hd->bHubContrCurrent = 0;
+ return 0;
+ } else if (typeReq == GetHubStatus && wLength >= sizeof(struct usb_hub_status)) {
+ hs = (struct usb_hub_status *) buf;
+ memset(hs, 0, sizeof(*hs));
+ hs->wHubStatus = 0;
+ hs->wHubChange = 0;
+ return 0;
+ } else if (typeReq == GetPortStatus && wLength >= 4 /* usb 2.0 */) {
+ ps = (struct usb_port_status *) buf;
+ ps->wPortStatus = 0;
+ ps->wPortChange = 0;
+
+ if (vhci->port_power_mask & BIT(wIndex))
+ ps->wPortStatus |= USB_PORT_STAT_POWER;
+
+ if (!(bce_vhci_port_mask & BIT(wIndex)))
+ return 0;
+
+ if ((status = bce_vhci_cmd_port_status(&vhci->cq, (u8) wIndex, 0, &port_status)))
+ return status;
+
+ if (port_status & 16)
+ ps->wPortStatus |= USB_PORT_STAT_ENABLE | USB_PORT_STAT_HIGH_SPEED;
+ if (port_status & 4)
+ ps->wPortStatus |= USB_PORT_STAT_CONNECTION;
+ if (port_status & 2)
+ ps->wPortStatus |= USB_PORT_STAT_OVERCURRENT;
+ if (port_status & 8)
+ ps->wPortStatus |= USB_PORT_STAT_RESET;
+ if (port_status & 0x60)
+ ps->wPortStatus |= USB_PORT_STAT_SUSPEND;
+
+ if (port_status & 0x40000)
+ ps->wPortChange |= USB_PORT_STAT_C_CONNECTION;
+
+ pr_debug("bce-vhci: Translated status %x to %x:%x\n", port_status, ps->wPortStatus, ps->wPortChange);
+ return 0;
+ } else if (typeReq == SetPortFeature) {
+ if (wValue == USB_PORT_FEAT_POWER) {
+ status = bce_vhci_cmd_port_power_on(&vhci->cq, (u8) wIndex);
+ /* As far as I am aware, power status is not part of the port status so store it separately */
+ if (!status)
+ vhci->port_power_mask |= BIT(wIndex);
+ return status;
+ }
+ if (wValue == USB_PORT_FEAT_RESET) {
+ return bce_vhci_reset_device(vhci, wIndex, wValue);
+ }
+ if (wValue == USB_PORT_FEAT_SUSPEND) {
+ /* TODO: Am I supposed to also suspend the endpoints? */
+ pr_debug("bce-vhci: Suspending port %i\n", wIndex);
+ return bce_vhci_cmd_port_suspend(&vhci->cq, (u8) wIndex);
+ }
+ } else if (typeReq == ClearPortFeature) {
+ if (wValue == USB_PORT_FEAT_ENABLE)
+ return bce_vhci_cmd_port_disable(&vhci->cq, (u8) wIndex);
+ if (wValue == USB_PORT_FEAT_POWER) {
+ status = bce_vhci_cmd_port_power_off(&vhci->cq, (u8) wIndex);
+ if (!status)
+ vhci->port_power_mask &= ~BIT(wIndex);
+ return status;
+ }
+ if (wValue == USB_PORT_FEAT_C_CONNECTION)
+ return bce_vhci_cmd_port_status(&vhci->cq, (u8) wIndex, 0x40000, &port_status);
+ if (wValue == USB_PORT_FEAT_C_RESET) { /* I don't think I can transfer it in any way */
+ return 0;
+ }
+ if (wValue == USB_PORT_FEAT_SUSPEND) {
+ pr_debug("bce-vhci: Resuming port %i\n", wIndex);
+ return bce_vhci_cmd_port_resume(&vhci->cq, (u8) wIndex);
+ }
+ }
+ pr_err("bce-vhci: bce_vhci_hub_control unhandled request: %x %i %i [bufl=%i]\n", typeReq, wValue, wIndex, wLength);
+ dump_stack();
+ return -EIO;
+}
+
+static int bce_vhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
+{
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ struct bce_vhci_device *vdev;
+ bce_vhci_device_t devid;
+ pr_info("bce_vhci_enable_device\n");
+
+ if (vhci->port_to_device[udev->portnum])
+ return 0;
+
+ /* We need to early address the device */
+ if (bce_vhci_cmd_device_create(&vhci->cq, udev->portnum, &devid))
+ return -EIO;
+
+ pr_info("bce_vhci_cmd_device_create %i -> %i\n", udev->portnum, devid);
+
+ vdev = kzalloc(sizeof(struct bce_vhci_device), GFP_KERNEL);
+ vhci->port_to_device[udev->portnum] = devid;
+ vhci->devices[devid] = vdev;
+
+ bce_vhci_create_transfer_queue(vhci, &vdev->tq[0], &udev->ep0, devid, DMA_BIDIRECTIONAL);
+ udev->ep0.hcpriv = &vdev->tq[0];
+ vdev->tq_mask |= BIT(0);
+
+ bce_vhci_cmd_endpoint_create(&vhci->cq, devid, &udev->ep0.desc);
+ return 0;
+}
+
+static int bce_vhci_address_device(struct usb_hcd *hcd, struct usb_device *udev, unsigned int timeout_ms) //TODO: follow timeout
+{
+ /* This is the same as enable_device, but instead in the old scheme */
+ return bce_vhci_enable_device(hcd, udev);
+}
+
+static void bce_vhci_free_device(struct usb_hcd *hcd, struct usb_device *udev)
+{
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ int i;
+ bce_vhci_device_t devid;
+ struct bce_vhci_device *dev;
+ pr_info("bce_vhci_free_device %i\n", udev->portnum);
+ if (!vhci->port_to_device[udev->portnum])
+ return;
+ devid = vhci->port_to_device[udev->portnum];
+ dev = vhci->devices[devid];
+ for (i = 0; i < 32; i++) {
+ if (dev->tq_mask & BIT(i)) {
+ bce_vhci_transfer_queue_pause(&dev->tq[i], BCE_VHCI_PAUSE_SHUTDOWN);
+ bce_vhci_cmd_endpoint_destroy(&vhci->cq, devid, (u8) i);
+ bce_vhci_destroy_transfer_queue(vhci, &dev->tq[i]);
+ }
+ }
+ vhci->devices[devid] = NULL;
+ vhci->port_to_device[udev->portnum] = 0;
+ bce_vhci_cmd_device_destroy(&vhci->cq, devid);
+ kfree(dev);
+}
+
+static int bce_vhci_reset_device(struct bce_vhci *vhci, int index, u16 timeout)
+{
+ struct bce_vhci_device *dev = NULL;
+ bce_vhci_device_t devid;
+ int i;
+ int status;
+ enum dma_data_direction dir;
+ pr_info("bce_vhci_reset_device %i\n", index);
+
+ devid = vhci->port_to_device[index];
+ if (devid) {
+ dev = vhci->devices[devid];
+
+ for (i = 0; i < 32; i++) {
+ if (dev->tq_mask & BIT(i)) {
+ bce_vhci_transfer_queue_pause(&dev->tq[i], BCE_VHCI_PAUSE_SHUTDOWN);
+ bce_vhci_cmd_endpoint_destroy(&vhci->cq, devid, (u8) i);
+ bce_vhci_destroy_transfer_queue(vhci, &dev->tq[i]);
+ }
+ }
+ vhci->devices[devid] = NULL;
+ vhci->port_to_device[index] = 0;
+ bce_vhci_cmd_device_destroy(&vhci->cq, devid);
+ }
+ status = bce_vhci_cmd_port_reset(&vhci->cq, (u8) index, timeout);
+
+ if (dev) {
+ if ((status = bce_vhci_cmd_device_create(&vhci->cq, index, &devid)))
+ return status;
+ vhci->devices[devid] = dev;
+ vhci->port_to_device[index] = devid;
+
+ for (i = 0; i < 32; i++) {
+ if (dev->tq_mask & BIT(i)) {
+ dir = usb_endpoint_dir_in(&dev->tq[i].endp->desc) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
+ if (i == 0)
+ dir = DMA_BIDIRECTIONAL;
+ bce_vhci_create_transfer_queue(vhci, &dev->tq[i], dev->tq[i].endp, devid, dir);
+ bce_vhci_cmd_endpoint_create(&vhci->cq, devid, &dev->tq[i].endp->desc);
+ }
+ }
+ }
+
+ return status;
+}
+
+static int bce_vhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
+{
+ return 0;
+}
+
+static int bce_vhci_get_frame_number(struct usb_hcd *hcd)
+{
+ return 0;
+}
+
+static int bce_vhci_bus_suspend(struct usb_hcd *hcd)
+{
+ int i, j;
+ int status;
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ pr_info("bce_vhci: suspend started\n");
+
+ pr_info("bce_vhci: suspend endpoints\n");
+ for (i = 0; i < 16; i++) {
+ if (!vhci->port_to_device[i])
+ continue;
+ for (j = 0; j < 32; j++) {
+ if (!(vhci->devices[vhci->port_to_device[i]]->tq_mask & BIT(j)))
+ continue;
+ bce_vhci_transfer_queue_pause(&vhci->devices[vhci->port_to_device[i]]->tq[j],
+ BCE_VHCI_PAUSE_SUSPEND);
+ }
+ }
+
+ pr_info("bce_vhci: suspend ports\n");
+ for (i = 0; i < 16; i++) {
+ if (!vhci->port_to_device[i])
+ continue;
+ bce_vhci_cmd_port_suspend(&vhci->cq, i);
+ }
+ pr_info("bce_vhci: suspend controller\n");
+ if ((status = bce_vhci_cmd_controller_pause(&vhci->cq)))
+ return status;
+
+ bce_vhci_event_queue_pause(&vhci->ev_commands);
+ bce_vhci_event_queue_pause(&vhci->ev_system);
+ bce_vhci_event_queue_pause(&vhci->ev_isochronous);
+ bce_vhci_event_queue_pause(&vhci->ev_interrupt);
+ bce_vhci_event_queue_pause(&vhci->ev_asynchronous);
+ pr_info("bce_vhci: suspend done\n");
+ return 0;
+}
+
+static int bce_vhci_bus_resume(struct usb_hcd *hcd)
+{
+ int i, j;
+ int status;
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ pr_info("bce_vhci: resume started\n");
+
+ bce_vhci_event_queue_resume(&vhci->ev_system);
+ bce_vhci_event_queue_resume(&vhci->ev_isochronous);
+ bce_vhci_event_queue_resume(&vhci->ev_interrupt);
+ bce_vhci_event_queue_resume(&vhci->ev_asynchronous);
+ bce_vhci_event_queue_resume(&vhci->ev_commands);
+
+ pr_info("bce_vhci: resume controller\n");
+ if ((status = bce_vhci_cmd_controller_start(&vhci->cq)))
+ return status;
+
+ pr_info("bce_vhci: resume ports\n");
+ for (i = 0; i < 16; i++) {
+ if (!vhci->port_to_device[i])
+ continue;
+ bce_vhci_cmd_port_resume(&vhci->cq, i);
+ }
+ pr_info("bce_vhci: resume endpoints\n");
+ for (i = 0; i < 16; i++) {
+ if (!vhci->port_to_device[i])
+ continue;
+ for (j = 0; j < 32; j++) {
+ if (!(vhci->devices[vhci->port_to_device[i]]->tq_mask & BIT(j)))
+ continue;
+ bce_vhci_transfer_queue_resume(&vhci->devices[vhci->port_to_device[i]]->tq[j],
+ BCE_VHCI_PAUSE_SUSPEND);
+ }
+ }
+
+ pr_info("bce_vhci: resume done\n");
+ return 0;
+}
+
+static int bce_vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
+{
+ struct bce_vhci_transfer_queue *q = urb->ep->hcpriv;
+ pr_debug("bce_vhci_urb_enqueue %i:%x\n", q->dev_addr, urb->ep->desc.bEndpointAddress);
+ if (!q)
+ return -ENOENT;
+ return bce_vhci_urb_create(q, urb);
+}
+
+static int bce_vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
+{
+ struct bce_vhci_transfer_queue *q = urb->ep->hcpriv;
+ pr_debug("bce_vhci_urb_dequeue %x\n", urb->ep->desc.bEndpointAddress);
+ return bce_vhci_urb_request_cancel(q, urb, status);
+}
+
+static void bce_vhci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
+{
+ struct bce_vhci_transfer_queue *q = ep->hcpriv;
+ pr_debug("bce_vhci_endpoint_reset\n");
+ if (q)
+ bce_vhci_transfer_queue_request_reset(q);
+}
+
+static u8 bce_vhci_endpoint_index(u8 addr)
+{
+ if (addr & 0x80)
+ return (u8) (0x10 + (addr & 0xf));
+ return (u8) (addr & 0xf);
+}
+
+static int bce_vhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *endp)
+{
+ u8 endp_index = bce_vhci_endpoint_index(endp->desc.bEndpointAddress);
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ bce_vhci_device_t devid = vhci->port_to_device[udev->portnum];
+ struct bce_vhci_device *vdev = vhci->devices[devid];
+ pr_debug("bce_vhci_add_endpoint %x/%x:%x\n", udev->portnum, devid, endp_index);
+
+ if (udev->bus->root_hub == udev) /* The USB hub */
+ return 0;
+ if (vdev == NULL)
+ return -ENODEV;
+ if (vdev->tq_mask & BIT(endp_index)) {
+ endp->hcpriv = &vdev->tq[endp_index];
+ return 0;
+ }
+
+ bce_vhci_create_transfer_queue(vhci, &vdev->tq[endp_index], endp, devid,
+ usb_endpoint_dir_in(&endp->desc) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
+ endp->hcpriv = &vdev->tq[endp_index];
+ vdev->tq_mask |= BIT(endp_index);
+
+ bce_vhci_cmd_endpoint_create(&vhci->cq, devid, &endp->desc);
+ return 0;
+}
+
+static int bce_vhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *endp)
+{
+ u8 endp_index = bce_vhci_endpoint_index(endp->desc.bEndpointAddress);
+ struct bce_vhci *vhci = bce_vhci_from_hcd(hcd);
+ bce_vhci_device_t devid = vhci->port_to_device[udev->portnum];
+ struct bce_vhci_transfer_queue *q = endp->hcpriv;
+ struct bce_vhci_device *vdev = vhci->devices[devid];
+ pr_info("bce_vhci_drop_endpoint %x:%x\n", udev->portnum, endp_index);
+ if (!q) {
+ if (vdev && vdev->tq_mask & BIT(endp_index)) {
+ pr_err("something deleted the hcpriv?\n");
+ q = &vdev->tq[endp_index];
+ } else {
+ return 0;
+ }
+ }
+
+ bce_vhci_cmd_endpoint_destroy(&vhci->cq, devid, (u8) (endp->desc.bEndpointAddress & 0x8Fu));
+ vhci->devices[devid]->tq_mask &= ~BIT(endp_index);
+ bce_vhci_destroy_transfer_queue(vhci, q);
+ return 0;
+}
+
+static int bce_vhci_create_message_queues(struct bce_vhci *vhci)
+{
+ if (bce_vhci_message_queue_create(vhci, &vhci->msg_commands, "VHC1HostCommands") ||
+ bce_vhci_message_queue_create(vhci, &vhci->msg_system, "VHC1HostSystemEvents") ||
+ bce_vhci_message_queue_create(vhci, &vhci->msg_isochronous, "VHC1HostIsochronousEvents") ||
+ bce_vhci_message_queue_create(vhci, &vhci->msg_interrupt, "VHC1HostInterruptEvents") ||
+ bce_vhci_message_queue_create(vhci, &vhci->msg_asynchronous, "VHC1HostAsynchronousEvents")) {
+ bce_vhci_destroy_message_queues(vhci);
+ return -EINVAL;
+ }
+ spin_lock_init(&vhci->msg_asynchronous_lock);
+ bce_vhci_command_queue_create(&vhci->cq, &vhci->msg_commands);
+ return 0;
+}
+
+static void bce_vhci_destroy_message_queues(struct bce_vhci *vhci)
+{
+ bce_vhci_command_queue_destroy(&vhci->cq);
+ bce_vhci_message_queue_destroy(vhci, &vhci->msg_commands);
+ bce_vhci_message_queue_destroy(vhci, &vhci->msg_system);
+ bce_vhci_message_queue_destroy(vhci, &vhci->msg_isochronous);
+ bce_vhci_message_queue_destroy(vhci, &vhci->msg_interrupt);
+ bce_vhci_message_queue_destroy(vhci, &vhci->msg_asynchronous);
+}
+
+static void bce_vhci_handle_system_event(struct bce_vhci_event_queue *q, struct bce_vhci_message *msg);
+static void bce_vhci_handle_usb_event(struct bce_vhci_event_queue *q, struct bce_vhci_message *msg);
+
+static int bce_vhci_create_event_queues(struct bce_vhci *vhci)
+{
+ vhci->ev_cq = bce_create_cq(vhci->dev, 0x100);
+ if (!vhci->ev_cq)
+ return -EINVAL;
+#define CREATE_EVENT_QUEUE(field, name, cb) bce_vhci_event_queue_create(vhci, &vhci->field, name, cb)
+ if (__bce_vhci_event_queue_create(vhci, &vhci->ev_commands, "VHC1FirmwareCommands",
+ bce_vhci_firmware_event_completion) ||
+ CREATE_EVENT_QUEUE(ev_system, "VHC1FirmwareSystemEvents", bce_vhci_handle_system_event) ||
+ CREATE_EVENT_QUEUE(ev_isochronous, "VHC1FirmwareIsochronousEvents", bce_vhci_handle_usb_event) ||
+ CREATE_EVENT_QUEUE(ev_interrupt, "VHC1FirmwareInterruptEvents", bce_vhci_handle_usb_event) ||
+ CREATE_EVENT_QUEUE(ev_asynchronous, "VHC1FirmwareAsynchronousEvents", bce_vhci_handle_usb_event)) {
+ bce_vhci_destroy_event_queues(vhci);
+ return -EINVAL;
+ }
+#undef CREATE_EVENT_QUEUE
+ return 0;
+}
+
+static void bce_vhci_destroy_event_queues(struct bce_vhci *vhci)
+{
+ bce_vhci_event_queue_destroy(vhci, &vhci->ev_commands);
+ bce_vhci_event_queue_destroy(vhci, &vhci->ev_system);
+ bce_vhci_event_queue_destroy(vhci, &vhci->ev_isochronous);
+ bce_vhci_event_queue_destroy(vhci, &vhci->ev_interrupt);
+ bce_vhci_event_queue_destroy(vhci, &vhci->ev_asynchronous);
+ if (vhci->ev_cq)
+ bce_destroy_cq(vhci->dev, vhci->ev_cq);
+}
+
+static void bce_vhci_send_fw_event_response(struct bce_vhci *vhci, struct bce_vhci_message *req, u16 status)
+{
+ unsigned long timeout = 1000;
+ struct bce_vhci_message r = *req;
+ r.cmd = (u16) (req->cmd | 0x8000u);
+ r.status = status;
+ r.param1 = req->param1;
+ r.param2 = 0;
+
+ if (bce_reserve_submission(vhci->msg_system.sq, &timeout)) {
+ pr_err("bce-vhci: Cannot reserve submision for FW event reply\n");
+ return;
+ }
+ bce_vhci_message_queue_write(&vhci->msg_system, &r);
+}
+
+static int bce_vhci_handle_firmware_event(struct bce_vhci *vhci, struct bce_vhci_message *msg)
+{
+ unsigned long flags;
+ bce_vhci_device_t devid;
+ u8 endp;
+ struct bce_vhci_device *dev;
+ struct bce_vhci_transfer_queue *tq;
+ if (msg->cmd == BCE_VHCI_CMD_ENDPOINT_REQUEST_STATE || msg->cmd == BCE_VHCI_CMD_ENDPOINT_SET_STATE) {
+ devid = (bce_vhci_device_t) (msg->param1 & 0xff);
+ endp = bce_vhci_endpoint_index((u8) ((msg->param1 >> 8) & 0xff));
+ dev = vhci->devices[devid];
+ if (!dev || !(dev->tq_mask & BIT(endp)))
+ return BCE_VHCI_BAD_ARGUMENT;
+ tq = &dev->tq[endp];
+ }
+
+ if (msg->cmd == BCE_VHCI_CMD_ENDPOINT_REQUEST_STATE) {
+ if (msg->param2 == BCE_VHCI_ENDPOINT_ACTIVE) {
+ bce_vhci_transfer_queue_resume(tq, BCE_VHCI_PAUSE_FIRMWARE);
+ return BCE_VHCI_SUCCESS;
+ } else if (msg->param2 == BCE_VHCI_ENDPOINT_PAUSED) {
+ bce_vhci_transfer_queue_pause(tq, BCE_VHCI_PAUSE_FIRMWARE);
+ return BCE_VHCI_SUCCESS;
+ }
+ return BCE_VHCI_BAD_ARGUMENT;
+ } else if (msg->cmd == BCE_VHCI_CMD_ENDPOINT_SET_STATE) {
+ if (msg->param2 == BCE_VHCI_ENDPOINT_STALLED) {
+ tq->state = msg->param2;
+ spin_lock_irqsave(&tq->urb_lock, flags);
+ tq->stalled = true;
+ spin_unlock_irqrestore(&tq->urb_lock, flags);
+ return BCE_VHCI_SUCCESS;
+ }
+ return BCE_VHCI_BAD_ARGUMENT;
+ }
+ pr_warn("bce-vhci: Unhandled firmware event: %x s=%x p1=%x p2=%llx\n",
+ msg->cmd, msg->status, msg->param1, msg->param2);
+ return BCE_VHCI_BAD_ARGUMENT;
+}
+
+static void bce_vhci_handle_firmware_events_w(struct work_struct *ws)
+{
+ size_t cnt = 0;
+ int result;
+ struct bce_vhci *vhci = container_of(ws, struct bce_vhci, w_fw_events);
+ struct bce_queue_sq *sq = vhci->ev_commands.sq;
+ struct bce_sq_completion_data *cq;
+ struct bce_vhci_message *msg, *msg2 = NULL;
+
+ while (true) {
+ if (msg2) {
+ msg = msg2;
+ msg2 = NULL;
+ } else if ((cq = bce_next_completion(sq))) {
+ if (cq->status == BCE_COMPLETION_ABORTED) {
+ bce_notify_submission_complete(sq);
+ continue;
+ }
+ msg = &vhci->ev_commands.data[sq->head];
+ } else {
+ break;
+ }
+
+ pr_debug("bce-vhci: Got fw event: %x s=%x p1=%x p2=%llx\n", msg->cmd, msg->status, msg->param1, msg->param2);
+ if ((cq = bce_next_completion(sq))) {
+ msg2 = &vhci->ev_commands.data[(sq->head + 1) % sq->el_count];
+ pr_debug("bce-vhci: Got second fw event: %x s=%x p1=%x p2=%llx\n",
+ msg->cmd, msg->status, msg->param1, msg->param2);
+ if (cq->status != BCE_COMPLETION_ABORTED &&
+ msg2->cmd == (msg->cmd | 0x4000) && msg2->param1 == msg->param1) {
+ /* Take two elements */
+ pr_debug("bce-vhci: Cancelled\n");
+ bce_vhci_send_fw_event_response(vhci, msg, BCE_VHCI_ABORT);
+
+ bce_notify_submission_complete(sq);
+ bce_notify_submission_complete(sq);
+ msg2 = NULL;
+ cnt += 2;
+ continue;
+ }
+
+ pr_warn("bce-vhci: Handle fw event - unexpected cancellation\n");
+ }
+
+ result = bce_vhci_handle_firmware_event(vhci, msg);
+ bce_vhci_send_fw_event_response(vhci, msg, (u16) result);
+
+
+ bce_notify_submission_complete(sq);
+ ++cnt;
+ }
+ bce_vhci_event_queue_submit_pending(&vhci->ev_commands, cnt);
+ if (atomic_read(&sq->available_commands) == sq->el_count - 1) {
+ pr_debug("bce-vhci: complete\n");
+ complete(&vhci->ev_commands.queue_empty_completion);
+ }
+}
+
+static void bce_vhci_firmware_event_completion(struct bce_queue_sq *sq)
+{
+ struct bce_vhci_event_queue *q = sq->userdata;
+ queue_work(q->vhci->tq_state_wq, &q->vhci->w_fw_events);
+}
+
+static void bce_vhci_handle_system_event(struct bce_vhci_event_queue *q, struct bce_vhci_message *msg)
+{
+ if (msg->cmd & 0x8000) {
+ bce_vhci_command_queue_deliver_completion(&q->vhci->cq, msg);
+ } else {
+ pr_warn("bce-vhci: Unhandled system event: %x s=%x p1=%x p2=%llx\n",
+ msg->cmd, msg->status, msg->param1, msg->param2);
+ }
+}
+
+static void bce_vhci_handle_usb_event(struct bce_vhci_event_queue *q, struct bce_vhci_message *msg)
+{
+ bce_vhci_device_t devid;
+ u8 endp;
+ struct bce_vhci_device *dev;
+ if (msg->cmd & 0x8000) {
+ bce_vhci_command_queue_deliver_completion(&q->vhci->cq, msg);
+ } else if (msg->cmd == BCE_VHCI_CMD_TRANSFER_REQUEST || msg->cmd == BCE_VHCI_CMD_CONTROL_TRANSFER_STATUS) {
+ devid = (bce_vhci_device_t) (msg->param1 & 0xff);
+ endp = bce_vhci_endpoint_index((u8) ((msg->param1 >> 8) & 0xff));
+ dev = q->vhci->devices[devid];
+ if (!dev || (dev->tq_mask & BIT(endp)) == 0) {
+ pr_err("bce-vhci: Didn't find destination for transfer queue event\n");
+ return;
+ }
+ bce_vhci_transfer_queue_event(&dev->tq[endp], msg);
+ } else {
+ pr_warn("bce-vhci: Unhandled USB event: %x s=%x p1=%x p2=%llx\n",
+ msg->cmd, msg->status, msg->param1, msg->param2);
+ }
+}
+
+
+
+static const struct hc_driver bce_vhci_driver = {
+ .description = "bce-vhci",
+ .product_desc = "BCE VHCI Host Controller",
+ .hcd_priv_size = sizeof(struct bce_vhci *),
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,4,0)
+ .flags = HCD_USB2,
+#else
+ .flags = HCD_USB2 | HCD_DMA,
+#endif
+
+ .start = bce_vhci_start,
+ .stop = bce_vhci_stop,
+ .hub_status_data = bce_vhci_hub_status_data,
+ .hub_control = bce_vhci_hub_control,
+ .urb_enqueue = bce_vhci_urb_enqueue,
+ .urb_dequeue = bce_vhci_urb_dequeue,
+ .enable_device = bce_vhci_enable_device,
+ .free_dev = bce_vhci_free_device,
+ .address_device = bce_vhci_address_device,
+ .add_endpoint = bce_vhci_add_endpoint,
+ .drop_endpoint = bce_vhci_drop_endpoint,
+ .endpoint_reset = bce_vhci_endpoint_reset,
+ .check_bandwidth = bce_vhci_check_bandwidth,
+ .get_frame_number = bce_vhci_get_frame_number,
+ .bus_suspend = bce_vhci_bus_suspend,
+ .bus_resume = bce_vhci_bus_resume
+};
+
+
+int __init bce_vhci_module_init(void)
+{
+ int result;
+ if ((result = alloc_chrdev_region(&bce_vhci_chrdev, 0, 1, "bce-vhci")))
+ goto fail_chrdev;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0)
+ bce_vhci_class = class_create(THIS_MODULE, "bce-vhci");
+#else
+ bce_vhci_class = class_create("bce-vhci");
+#endif
+ if (IS_ERR(bce_vhci_class)) {
+ result = PTR_ERR(bce_vhci_class);
+ goto fail_class;
+ }
+ return 0;
+
+fail_class:
+ class_destroy(bce_vhci_class);
+fail_chrdev:
+ unregister_chrdev_region(bce_vhci_chrdev, 1);
+ if (!result)
+ result = -EINVAL;
+ return result;
+}
+void __exit bce_vhci_module_exit(void)
+{
+ class_destroy(bce_vhci_class);
+ unregister_chrdev_region(bce_vhci_chrdev, 1);
+}
+
+module_param_named(vhci_port_mask, bce_vhci_port_mask, ushort, 0444);
+MODULE_PARM_DESC(vhci_port_mask, "Specifies which VHCI ports are enabled");
diff --git a/drivers/staging/apple-bce/vhci/vhci.h b/drivers/staging/apple-bce/vhci/vhci.h
new file mode 100644
index 000000000..6c2e22622
--- /dev/null
+++ b/drivers/staging/apple-bce/vhci/vhci.h
@@ -0,0 +1,52 @@
+#ifndef BCE_VHCI_H
+#define BCE_VHCI_H
+
+#include "queue.h"
+#include "transfer.h"
+
+struct usb_hcd;
+struct bce_queue_cq;
+
+struct bce_vhci_device {
+ struct bce_vhci_transfer_queue tq[32];
+ u32 tq_mask;
+};
+struct bce_vhci {
+ struct apple_bce_device *dev;
+ dev_t vdevt;
+ struct device *vdev;
+ struct usb_hcd *hcd;
+ struct spinlock hcd_spinlock;
+ struct bce_vhci_message_queue msg_commands;
+ struct bce_vhci_message_queue msg_system;
+ struct bce_vhci_message_queue msg_isochronous;
+ struct bce_vhci_message_queue msg_interrupt;
+ struct bce_vhci_message_queue msg_asynchronous;
+ struct spinlock msg_asynchronous_lock;
+ struct bce_vhci_command_queue cq;
+ struct bce_queue_cq *ev_cq;
+ struct bce_vhci_event_queue ev_commands;
+ struct bce_vhci_event_queue ev_system;
+ struct bce_vhci_event_queue ev_isochronous;
+ struct bce_vhci_event_queue ev_interrupt;
+ struct bce_vhci_event_queue ev_asynchronous;
+ u16 port_mask;
+ u8 port_count;
+ u16 port_power_mask;
+ bce_vhci_device_t port_to_device[16];
+ struct bce_vhci_device *devices[16];
+ struct workqueue_struct *tq_state_wq;
+ struct work_struct w_fw_events;
+};
+
+int __init bce_vhci_module_init(void);
+void __exit bce_vhci_module_exit(void);
+
+int bce_vhci_create(struct apple_bce_device *dev, struct bce_vhci *vhci);
+void bce_vhci_destroy(struct bce_vhci *vhci);
+int bce_vhci_start(struct usb_hcd *hcd);
+void bce_vhci_stop(struct usb_hcd *hcd);
+
+struct bce_vhci *bce_vhci_from_hcd(struct usb_hcd *hcd);
+
+#endif //BCE_VHCI_H
--
2.43.2
From dc27d4db5787546ae5eacf3483f3b87f2d4fb1c1 Mon Sep 17 00:00:00 2001
From: Redecorating <69827514+Redecorating@users.noreply.github.com>
Date: Mon, 7 Nov 2022 14:56:34 +0530
Subject: [PATCH] Put apple-bce in drivers/staging
---
drivers/staging/Kconfig | 2 ++
drivers/staging/Makefile | 1 +
drivers/staging/apple-bce/Kconfig | 18 ++++++++++++++++++
drivers/staging/apple-bce/Makefile | 2 +-
4 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 drivers/staging/apple-bce/Kconfig
diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index 5cfabd537..3b8e61d26 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -80,4 +80,6 @@ source "drivers/staging/qlge/Kconfig"
source "drivers/staging/vme_user/Kconfig"
+source "drivers/staging/apple-bce/Kconfig"
+
endif # STAGING
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index f8c3aa9c2..1e148d6c3 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_PI433) += pi433/
obj-$(CONFIG_PI433) += pi433/
obj-$(CONFIG_XIL_AXIS_FIFO) += axis-fifo/
obj-$(CONFIG_FIELDBUS_DEV) += fieldbus/
+obj-$(CONFIG_APPLE_BCE) += apple-bce/
diff --git a/drivers/staging/apple-bce/Kconfig b/drivers/staging/apple-bce/Kconfig
new file mode 100644
index 000000000..fe92bc441
--- /dev/null
+++ b/drivers/staging/apple-bce/Kconfig
@@ -0,0 +1,18 @@
+config APPLE_BCE
+ tristate "Apple BCE driver (VHCI and Audio support)"
+ default m
+ depends on X86
+ select SOUND
+ select SND
+ select SND_PCM
+ select SND_JACK
+ help
+ VHCI and audio support on Apple MacBooks with the T2 Chip.
+ This driver is divided in three components:
+ - BCE (Buffer Copy Engine): which establishes a basic communication
+ channel with the T2 chip. This component is required by the other two:
+ - VHCI (Virtual Host Controller Interface): Access to keyboard, mouse
+ and other system devices depend on this virtual USB host controller
+ - Audio: a driver for the T2 audio interface.
+
+ If "M" is selected, the module will be called apple-bce.'
diff --git a/drivers/staging/apple-bce/Makefile b/drivers/staging/apple-bce/Makefile
index a6a656f06..8cfbd3f64 100644
--- a/drivers/staging/apple-bce/Makefile
+++ b/drivers/staging/apple-bce/Makefile
@@ -1,5 +1,5 @@
modname := apple-bce
-obj-m += $(modname).o
+obj-$(CONFIG_APPLE_BCE) += $(modname).o
apple-bce-objs := apple_bce.o mailbox.o queue.o queue_dma.o vhci/vhci.o vhci/queue.o vhci/transfer.o audio/audio.o audio/protocol.o audio/protocol_bce.o audio/pcm.o
--
2.34.1
From 153b587ed53135eaf244144f6f8bdd5a0fe6b69e Mon Sep 17 00:00:00 2001
From: Redecorating <69827514+Redecorating@users.noreply.github.com>
Date: Fri, 24 Dec 2021 18:12:25 +1100
Subject: [PATCH 1/1] add modalias to apple-bce
---
drivers/staging/apple-bce/apple_bce.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/staging/apple-bce/apple_bce.c b/drivers/staging/apple-bce/apple_bce.c
index ad89632df..5e2f2f3b9 100644
--- a/drivers/staging/apple-bce/apple_bce.c
+++ b/drivers/staging/apple-bce/apple_bce.c
@@ -439,5 +439,6 @@ MODULE_LICENSE("GPL");
MODULE_AUTHOR("MrARM");
MODULE_DESCRIPTION("Apple BCE Driver");
MODULE_VERSION("0.01");
+MODULE_ALIAS("pci:v0000106Bd00001801sv*sd*bc*sc*i*");
module_init(apple_bce_module_init);
module_exit(apple_bce_module_exit);
--
2.43.0
From 75ca57b64ce6846622d8aefac5a76fc638a2123d Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Sun, 5 Mar 2023 19:12:53 +0300
Subject: [PATCH 01/12] HID: core: add helper for finding a field with a
certain usage
This helper will allow HID drivers to easily determine if they should
bind to a hid_device by checking for the prescence of a certain field
when its ID is not enough, which can be the case on USB devices with
multiple interfaces and/or configurations.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/hid-core.c | 25 +++++++++++++++++++++++++
drivers/hid/hid-google-hammer.c | 27 ++-------------------------
include/linux/hid.h | 2 ++
3 files changed, 29 insertions(+), 25 deletions(-)
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 8992e3c1e..6395bdc2e 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1906,6 +1906,31 @@ int hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
}
EXPORT_SYMBOL_GPL(hid_set_field);
+struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
+ unsigned int application, unsigned int usage)
+{
+ struct list_head *report_list = &hdev->report_enum[report_type].report_list;
+ struct hid_report *report;
+ int i, j;
+
+ list_for_each_entry(report, report_list, list) {
+ if (report->application != application)
+ continue;
+
+ for (i = 0; i < report->maxfield; i++) {
+ struct hid_field *field = report->field[i];
+
+ for (j = 0; j < field->maxusage; j++) {
+ if (field->usage[j].hid == usage)
+ return field;
+ }
+ }
+ }
+
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(hid_find_field);
+
static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
const u8 *data)
{
diff --git a/drivers/hid/hid-google-hammer.c b/drivers/hid/hid-google-hammer.c
index c6bdb9c4e..fba3652aa 100644
--- a/drivers/hid/hid-google-hammer.c
+++ b/drivers/hid/hid-google-hammer.c
@@ -419,38 +419,15 @@ static int hammer_event(struct hid_device *hid, struct hid_field *field,
return 0;
}
-static bool hammer_has_usage(struct hid_device *hdev, unsigned int report_type,
- unsigned application, unsigned usage)
-{
- struct hid_report_enum *re = &hdev->report_enum[report_type];
- struct hid_report *report;
- int i, j;
-
- list_for_each_entry(report, &re->report_list, list) {
- if (report->application != application)
- continue;
-
- for (i = 0; i < report->maxfield; i++) {
- struct hid_field *field = report->field[i];
-
- for (j = 0; j < field->maxusage; j++)
- if (field->usage[j].hid == usage)
- return true;
- }
- }
-
- return false;
-}
-
static bool hammer_has_folded_event(struct hid_device *hdev)
{
- return hammer_has_usage(hdev, HID_INPUT_REPORT,
+ return !!hid_find_field(hdev, HID_INPUT_REPORT,
HID_GD_KEYBOARD, HID_USAGE_KBD_FOLDED);
}
static bool hammer_has_backlight_control(struct hid_device *hdev)
{
- return hammer_has_usage(hdev, HID_OUTPUT_REPORT,
+ return !!hid_find_field(hdev, HID_OUTPUT_REPORT,
HID_GD_KEYBOARD, HID_AD_BRIGHTNESS);
}
diff --git a/include/linux/hid.h b/include/linux/hid.h
index 39e21e381..9520fdfdd 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -913,6 +913,8 @@ extern void hidinput_report_event(struct hid_device *hid, struct hid_report *rep
extern int hidinput_connect(struct hid_device *hid, unsigned int force);
extern void hidinput_disconnect(struct hid_device *);
+struct hid_field *hid_find_field(struct hid_device *hdev, unsigned int report_type,
+ unsigned int application, unsigned int usage);
int hid_set_field(struct hid_field *, unsigned, __s32);
int hid_input_report(struct hid_device *hid, enum hid_report_type type, u8 *data, u32 size,
int interrupt);
--
2.42.0
From 05cd738ce1c0e1a930a1dab02528fd9f1c702c38 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Sun, 5 Mar 2023 18:52:43 +0300
Subject: [PATCH 02/12] HID: hid-appletb-bl: add driver for the backlight of
Apple Touch Bars
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This commit adds a driver for the backlight of Apple Touch Bars on x86
Macs. Note that currently only T2 Macs are supported.
This driver is based on previous work done by Ronald Tschalär
<ronald@innovation.ch>.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
MAINTAINERS | 6 ++
drivers/hid/Kconfig | 10 ++
drivers/hid/Makefile | 1 +
drivers/hid/hid-appletb-bl.c | 193 +++++++++++++++++++++++++++++++++++
drivers/hid/hid-quirks.c | 4 +-
5 files changed, 213 insertions(+), 1 deletion(-)
create mode 100644 drivers/hid/hid-appletb-bl.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 4cc6bf79f..519b3b736 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9157,6 +9157,12 @@ F: include/linux/pm.h
F: include/linux/suspend.h
F: kernel/power/
+HID APPLE TOUCH BAR DRIVERS
+M: Kerem Karabay <kekrby@gmail.com>
+L: linux-input@vger.kernel.org
+S: Maintained
+F: drivers/hid/hid-appletb-*
+
HID CORE LAYER
M: Jiri Kosina <jikos@kernel.org>
M: Benjamin Tissoires <benjamin.tissoires@redhat.com>
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index e11c1c803..cf19a3b33 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -148,6 +148,16 @@ config HID_APPLEIR
Say Y here if you want support for Apple infrared remote control.
+config HID_APPLETB_BL
+ tristate "Apple Touch Bar Backlight"
+ depends on BACKLIGHT_CLASS_DEVICE
+ help
+ Say Y here if you want support for the backlight of Touch Bars on x86
+ MacBook Pros.
+
+ To compile this driver as a module, choose M here: the
+ module will be called hid-appletb-bl.
+
config HID_ASUS
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 7a9e16015..bc86e38b2 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o
obj-$(CONFIG_HID_ACRUX) += hid-axff.o
obj-$(CONFIG_HID_APPLE) += hid-apple.o
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
+obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
obj-$(CONFIG_HID_ASUS) += hid-asus.o
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
diff --git a/drivers/hid/hid-appletb-bl.c b/drivers/hid/hid-appletb-bl.c
new file mode 100644
index 000000000..0c5e4b776
--- /dev/null
+++ b/drivers/hid/hid-appletb-bl.c
@@ -0,0 +1,193 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar Backlight Driver
+ *
+ * Copyright (c) 2017-2018 Ronald Tschalär
+ * Copyright (c) 2022-2023 Kerem Karabay <kekrby@gmail.com>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/hid.h>
+#include <linux/backlight.h>
+
+#include "hid-ids.h"
+
+#define APPLETB_BL_ON 1
+#define APPLETB_BL_DIM 3
+#define APPLETB_BL_OFF 4
+
+#define HID_UP_APPLEVENDOR_TB_BL 0xff120000
+
+#define HID_VD_APPLE_TB_BRIGHTNESS 0xff120001
+#define HID_USAGE_AUX1 0xff120020
+#define HID_USAGE_BRIGHTNESS 0xff120021
+
+struct appletb_bl {
+ struct hid_field *aux1_field, *brightness_field;
+ struct backlight_device *bdev;
+
+ bool full_on;
+};
+
+const u8 appletb_bl_brightness_map[] = {
+ APPLETB_BL_OFF,
+ APPLETB_BL_DIM,
+ APPLETB_BL_ON
+};
+
+static int appletb_bl_set_brightness(struct appletb_bl *bl, u8 brightness)
+{
+ struct hid_report *report = bl->brightness_field->report;
+ struct hid_device *hdev = report->device;
+ int ret;
+
+ ret = hid_set_field(bl->aux1_field, 0, 1);
+ if (ret) {
+ hid_err(hdev, "Failed to set auxiliary field (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ ret = hid_set_field(bl->brightness_field, 0, brightness);
+ if (ret) {
+ hid_err(hdev, "Failed to set brightness field (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ if (!bl->full_on) {
+ ret = hid_hw_power(hdev, PM_HINT_FULLON);
+ if (ret < 0) {
+ hid_err(hdev, "Device didn't power on (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ bl->full_on = true;
+ }
+
+ hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
+
+ if (brightness == APPLETB_BL_OFF) {
+ hid_hw_power(hdev, PM_HINT_NORMAL);
+ bl->full_on = false;
+ }
+
+ return 0;
+}
+
+static int appletb_bl_update_status(struct backlight_device *bdev)
+{
+ struct appletb_bl *bl = bl_get_data(bdev);
+ u16 brightness;
+
+ if (bdev->props.state & BL_CORE_SUSPENDED)
+ brightness = 0;
+ else
+ brightness = backlight_get_brightness(bdev);
+
+ return appletb_bl_set_brightness(bl, appletb_bl_brightness_map[brightness]);
+}
+
+static const struct backlight_ops appletb_bl_backlight_ops = {
+ .options = BL_CORE_SUSPENDRESUME,
+ .update_status = appletb_bl_update_status,
+};
+
+static int appletb_bl_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+ struct hid_field *aux1_field, *brightness_field;
+ struct backlight_properties bl_props = { 0 };
+ struct device *dev = &hdev->dev;
+ struct appletb_bl *bl;
+ int ret;
+
+ ret = hid_parse(hdev);
+ if (ret)
+ return dev_err_probe(dev, ret, "HID parse failed\n");
+
+ aux1_field = hid_find_field(hdev, HID_FEATURE_REPORT,
+ HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_AUX1);
+
+ brightness_field = hid_find_field(hdev, HID_FEATURE_REPORT,
+ HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_BRIGHTNESS);
+
+ if (!aux1_field || !brightness_field)
+ return -ENODEV;
+
+ if (aux1_field->report != brightness_field->report)
+ return dev_err_probe(dev, -ENODEV, "Encountered unexpected report structure\n");
+
+ bl = devm_kzalloc(dev, sizeof(*bl), GFP_KERNEL);
+ if (!bl)
+ return -ENOMEM;
+
+ ret = hid_hw_start(hdev, HID_CONNECT_DRIVER);
+ if (ret)
+ return dev_err_probe(dev, ret, "HID hardware start failed\n");
+
+ ret = hid_hw_open(hdev);
+ if (ret) {
+ dev_err_probe(dev, ret, "HID hardware open failed\n");
+ goto stop_hw;
+ }
+
+ bl->aux1_field = aux1_field;
+ bl->brightness_field = brightness_field;
+
+ ret = appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to set touch bar brightness to off\n");
+ goto close_hw;
+ }
+
+ bl_props.type = BACKLIGHT_RAW;
+ bl_props.max_brightness = ARRAY_SIZE(appletb_bl_brightness_map) - 1;
+
+ bl->bdev = devm_backlight_device_register(dev, "appletb_backlight", dev, bl,
+ &appletb_bl_backlight_ops, &bl_props);
+ if (IS_ERR(bl->bdev)) {
+ ret = PTR_ERR(bl->bdev);
+ dev_err_probe(dev, ret, "Failed to register backlight device\n");
+ goto close_hw;
+ }
+
+ hid_set_drvdata(hdev, bl);
+
+ return 0;
+
+close_hw:
+ hid_hw_close(hdev);
+stop_hw:
+ hid_hw_stop(hdev);
+
+ return ret;
+}
+
+static void appletb_bl_remove(struct hid_device *hdev)
+{
+ struct appletb_bl *bl = hid_get_drvdata(hdev);
+
+ appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
+
+ hid_hw_close(hdev);
+ hid_hw_stop(hdev);
+}
+
+static const struct hid_device_id appletb_bl_hid_ids[] = {
+ /* MacBook Pro's 2018, 2019, with T2 chip: iBridge DFR Brightness */
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+ { }
+};
+MODULE_DEVICE_TABLE(hid, appletb_bl_hid_ids);
+
+static struct hid_driver appletb_bl_hid_driver = {
+ .name = "hid-appletb-bl",
+ .id_table = appletb_bl_hid_ids,
+ .probe = appletb_bl_probe,
+ .remove = appletb_bl_remove,
+};
+module_hid_driver(appletb_bl_hid_driver);
+
+MODULE_AUTHOR("Ronald Tschalär");
+MODULE_AUTHOR("Kerem Karabay <kekrby@gmail.com>");
+MODULE_DESCRIPTION("MacBookPro Touch Bar Backlight Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 3983b4f28..82e7a80c9 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -325,7 +325,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) },
- { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
#endif
#if IS_ENABLED(CONFIG_HID_APPLEIR)
@@ -335,6 +334,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
#endif
+#if IS_ENABLED(CONFIG_HID_APPLETB_BL)
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+#endif
#if IS_ENABLED(CONFIG_HID_ASUS)
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) },
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) },
--
2.42.0
From 514b4f088b7ed916c634ca6f61de72c5f86268dd Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Sun, 5 Mar 2023 18:17:23 +0300
Subject: [PATCH 03/12] HID: hid-appletb-kbd: add driver for the keyboard mode
of Apple Touch Bars
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The Touch Bars found on x86 Macs support two USB configurations: one
where the device presents itself as a HID keyboard and can display
predefined sets of keys, and one where the operating system has full
control over what is displayed. This commit adds a driver for the
display functionality of the first configuration.
Note that currently only T2 Macs are supported.
This driver is based on previous work done by Ronald Tschalär
<ronald@innovation.ch>.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
.../ABI/testing/sysfs-driver-hid-appletb-kbd | 13 +
drivers/hid/Kconfig | 11 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-appletb-kbd.c | 289 ++++++++++++++++++
drivers/hid/hid-quirks.c | 4 +-
5 files changed, 317 insertions(+), 1 deletion(-)
create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd
create mode 100644 drivers/hid/hid-appletb-kbd.c
diff --git a/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd b/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd
new file mode 100644
index 000000000..2a19584d0
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd
@@ -0,0 +1,13 @@
+What: /sys/bus/hid/drivers/hid-appletb-kbd/<dev>/mode
+Date: September, 2023
+KernelVersion: 6.5
+Contact: linux-input@vger.kernel.org
+Description:
+ The set of keys displayed on the Touch Bar.
+ Valid values are:
+ == =================
+ 0 Escape key only
+ 1 Function keys
+ 2 Media/brightness keys
+ 3 None
+ == =================
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index cf19a3b33..852de13aa 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -158,6 +158,17 @@ config HID_APPLETB_BL
To compile this driver as a module, choose M here: the
module will be called hid-appletb-bl.
+config HID_APPLETB_KBD
+ tristate "Apple Touch Bar Keyboard Mode"
+ depends on USB_HID
+ help
+ Say Y here if you want support for the keyboard mode (escape,
+ function, media and brightness keys) of Touch Bars on x86 MacBook
+ Pros.
+
+ To compile this driver as a module, choose M here: the
+ module will be called hid-appletb-kbd.
+
config HID_ASUS
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index bc86e38b2..5b60015fd 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ACRUX) += hid-axff.o
obj-$(CONFIG_HID_APPLE) += hid-apple.o
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o
+obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
obj-$(CONFIG_HID_ASUS) += hid-asus.o
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c
new file mode 100644
index 000000000..bc004c408
--- /dev/null
+++ b/drivers/hid/hid-appletb-kbd.c
@@ -0,0 +1,289 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar Keyboard Mode Driver
+ *
+ * Copyright (c) 2017-2018 Ronald Tschalär
+ * Copyright (c) 2022-2023 Kerem Karabay <kekrby@gmail.com>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/hid.h>
+#include <linux/usb.h>
+#include <linux/input.h>
+#include <linux/sysfs.h>
+#include <linux/bitops.h>
+#include <linux/module.h>
+#include <linux/string.h>
+#include <linux/input/sparse-keymap.h>
+
+#include "hid-ids.h"
+
+#define APPLETB_KBD_MODE_ESC 0
+#define APPLETB_KBD_MODE_FN 1
+#define APPLETB_KBD_MODE_SPCL 2
+#define APPLETB_KBD_MODE_OFF 3
+#define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF
+
+#define HID_USAGE_MODE 0x00ff0004
+
+struct appletb_kbd {
+ struct hid_field *mode_field;
+
+ u8 saved_mode;
+ u8 current_mode;
+};
+
+static const struct key_entry appletb_kbd_keymap[] = {
+ { KE_KEY, KEY_ESC, { KEY_ESC } },
+ { KE_KEY, KEY_F1, { KEY_BRIGHTNESSDOWN } },
+ { KE_KEY, KEY_F2, { KEY_BRIGHTNESSUP } },
+ { KE_KEY, KEY_F3, { KEY_RESERVED } },
+ { KE_KEY, KEY_F4, { KEY_RESERVED } },
+ { KE_KEY, KEY_F5, { KEY_KBDILLUMDOWN } },
+ { KE_KEY, KEY_F6, { KEY_KBDILLUMUP } },
+ { KE_KEY, KEY_F7, { KEY_PREVIOUSSONG } },
+ { KE_KEY, KEY_F8, { KEY_PLAYPAUSE } },
+ { KE_KEY, KEY_F9, { KEY_NEXTSONG } },
+ { KE_KEY, KEY_F10, { KEY_MUTE } },
+ { KE_KEY, KEY_F11, { KEY_VOLUMEDOWN } },
+ { KE_KEY, KEY_F12, { KEY_VOLUMEUP } },
+ { KE_END, 0 }
+};
+
+static int appletb_kbd_set_mode(struct appletb_kbd *kbd, u8 mode)
+{
+ struct hid_report *report = kbd->mode_field->report;
+ struct hid_device *hdev = report->device;
+ int ret;
+
+ ret = hid_hw_power(hdev, PM_HINT_FULLON);
+ if (ret) {
+ hid_err(hdev, "Device didn't resume (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ ret = hid_set_field(kbd->mode_field, 0, mode);
+ if (ret) {
+ hid_err(hdev, "Failed to set mode field to %u (%pe)\n", mode, ERR_PTR(ret));
+ goto power_normal;
+ }
+
+ hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
+
+ kbd->current_mode = mode;
+
+power_normal:
+ hid_hw_power(hdev, PM_HINT_NORMAL);
+
+ return ret;
+}
+
+static ssize_t mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct appletb_kbd *kbd = dev_get_drvdata(dev);
+
+ return sysfs_emit(buf, "%d\n", kbd->current_mode);
+}
+
+static ssize_t mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ struct appletb_kbd *kbd = dev_get_drvdata(dev);
+ u8 mode;
+ int ret;
+
+ ret = kstrtou8(buf, 0, &mode);
+ if (ret)
+ return ret;
+
+ if (mode > APPLETB_KBD_MODE_MAX)
+ return -EINVAL;
+
+ ret = appletb_kbd_set_mode(kbd, mode);
+
+ return ret < 0 ? ret : size;
+}
+static DEVICE_ATTR_RW(mode);
+
+struct attribute *appletb_kbd_attrs[] = {
+ &dev_attr_mode.attr,
+ NULL
+};
+ATTRIBUTE_GROUPS(appletb_kbd);
+
+static int appletb_tb_key_to_slot(unsigned int code)
+{
+ switch (code) {
+ case KEY_ESC:
+ return 0;
+ case KEY_F1 ... KEY_F10:
+ return code - KEY_F1 + 1;
+ case KEY_F11 ... KEY_F12:
+ return code - KEY_F11 + 11;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *field,
+ struct hid_usage *usage, __s32 value)
+{
+ struct appletb_kbd *kbd = hid_get_drvdata(hdev);
+ struct key_entry *translation;
+ struct input_dev *input;
+ int slot;
+
+ if ((usage->hid & HID_USAGE_PAGE) != HID_UP_KEYBOARD || usage->type != EV_KEY)
+ return 0;
+
+ input = field->hidinput->input;
+
+ /*
+ * Skip non-touch-bar keys.
+ *
+ * Either the touch bar itself or usbhid generate a slew of key-down
+ * events for all the meta keys. None of which we're at all interested
+ * in.
+ */
+ slot = appletb_tb_key_to_slot(usage->code);
+ if (slot < 0)
+ return 0;
+
+ translation = sparse_keymap_entry_from_scancode(input, usage->code);
+
+ if (translation && kbd->current_mode == APPLETB_KBD_MODE_SPCL) {
+ input_event(input, usage->type, translation->keycode, value);
+
+ return 1;
+ }
+
+ return kbd->current_mode == APPLETB_KBD_MODE_OFF;
+}
+
+static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_input *hidinput)
+{
+ struct input_dev *input = hidinput->input;
+
+ /*
+ * Clear various input capabilities that are blindly set by the hid
+ * driver (usbkbd.c)
+ */
+ memset(input->evbit, 0, sizeof(input->evbit));
+ memset(input->keybit, 0, sizeof(input->keybit));
+ memset(input->ledbit, 0, sizeof(input->ledbit));
+
+ __set_bit(EV_REP, input->evbit);
+
+ return sparse_keymap_setup(input, appletb_kbd_keymap, NULL);
+}
+
+static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+ struct appletb_kbd *kbd;
+ struct device *dev = &hdev->dev;
+ struct hid_field *mode_field;
+ int ret;
+
+ ret = hid_parse(hdev);
+ if (ret)
+ return dev_err_probe(dev, ret, "HID parse failed\n");
+
+ mode_field = hid_find_field(hdev, HID_OUTPUT_REPORT,
+ HID_GD_KEYBOARD, HID_USAGE_MODE);
+ if (!mode_field)
+ return -ENODEV;
+
+ kbd = devm_kzalloc(dev, sizeof(*kbd), GFP_KERNEL);
+ if (!kbd)
+ return -ENOMEM;
+
+ kbd->mode_field = mode_field;
+
+ ret = hid_hw_start(hdev, HID_CONNECT_HIDINPUT);
+ if (ret)
+ return dev_err_probe(dev, ret, "HID hw start failed\n");
+
+ ret = hid_hw_open(hdev);
+ if (ret) {
+ dev_err_probe(dev, ret, "HID hw open failed\n");
+ goto stop_hw;
+ }
+
+ ret = appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to set touchbar mode\n");
+ goto close_hw;
+ }
+
+ hid_set_drvdata(hdev, kbd);
+
+ return 0;
+
+close_hw:
+ hid_hw_close(hdev);
+stop_hw:
+ hid_hw_stop(hdev);
+ return ret;
+}
+
+static void appletb_kbd_remove(struct hid_device *hdev)
+{
+ struct appletb_kbd *kbd = hid_get_drvdata(hdev);
+
+ appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);
+
+ hid_hw_close(hdev);
+ hid_hw_stop(hdev);
+}
+
+#ifdef CONFIG_PM
+static int appletb_kbd_suspend(struct hid_device *hdev, pm_message_t msg)
+{
+ struct appletb_kbd *kbd = hid_get_drvdata(hdev);
+
+ kbd->saved_mode = kbd->current_mode;
+ appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);
+
+ return 0;
+}
+
+static int appletb_kbd_reset_resume(struct hid_device *hdev)
+{
+ struct appletb_kbd *kbd = hid_get_drvdata(hdev);
+
+ appletb_kbd_set_mode(kbd, kbd->saved_mode);
+
+ return 0;
+}
+#endif
+
+static const struct hid_device_id appletb_kbd_hid_ids[] = {
+ /* MacBook Pro's 2018, 2019, with T2 chip: iBridge Display */
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
+ { }
+};
+MODULE_DEVICE_TABLE(hid, appletb_kbd_hid_ids);
+
+static struct hid_driver appletb_kbd_hid_driver = {
+ .name = "hid-appletb-kbd",
+ .id_table = appletb_kbd_hid_ids,
+ .probe = appletb_kbd_probe,
+ .remove = appletb_kbd_remove,
+ .event = appletb_kbd_hid_event,
+ .input_configured = appletb_kbd_input_configured,
+#ifdef CONFIG_PM
+ .suspend = appletb_kbd_suspend,
+ .reset_resume = appletb_kbd_reset_resume,
+#endif
+ .driver.dev_groups = appletb_kbd_groups,
+};
+module_hid_driver(appletb_kbd_hid_driver);
+
+MODULE_AUTHOR("Ronald Tschalär");
+MODULE_AUTHOR("Kerem Karabay <kekrby@gmail.com>");
+MODULE_DESCRIPTION("MacBookPro Touch Bar Keyboard Mode Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 82e7a80c9..82be9dfaf 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -325,7 +325,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) },
- { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
#endif
#if IS_ENABLED(CONFIG_HID_APPLEIR)
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
@@ -337,6 +336,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
#if IS_ENABLED(CONFIG_HID_APPLETB_BL)
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
#endif
+#if IS_ENABLED(CONFIG_HID_APPLETB_KBD)
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
+#endif
#if IS_ENABLED(CONFIG_HID_ASUS)
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) },
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) },
--
2.42.0
From 2f9be28549307b4ac51e8d66bf3b8d5e0621466d Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Wed, 19 Jul 2023 19:37:14 +0300
Subject: [PATCH 04/12] HID: multitouch: support getting the contact ID from
HID_DG_TRANSDUCER_INDEX fields
This is needed to support Apple Touch Bars, where the contact ID is
contained in fields with the HID_DG_TRANSDUCER_INDEX usage.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/hid-multitouch.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index e31be0cb8..902a59928 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -636,7 +636,9 @@ static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
if (field->logical == HID_DG_FINGER || td->hdev->group != HID_GROUP_MULTITOUCH_WIN_8) {
for (n = 0; n < field->report_count; n++) {
- if (field->usage[n].hid == HID_DG_CONTACTID) {
+ unsigned int hid = field->usage[n].hid;
+
+ if (hid == HID_DG_CONTACTID || hid == HID_DG_TRANSDUCER_INDEX) {
rdata->is_mt_collection = true;
break;
}
@@ -815,6 +817,7 @@ static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
MT_STORE_FIELD(tip_state);
return 1;
case HID_DG_CONTACTID:
+ case HID_DG_TRANSDUCER_INDEX:
MT_STORE_FIELD(contactid);
app->touches_by_report++;
return 1;
--
2.42.0
From 6162d328fe7b2cf5a3ee8c29bdb229e9528c7a6c Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Wed, 19 Jul 2023 19:44:10 +0300
Subject: [PATCH 05/12] HID: multitouch: support getting the tip state from
HID_DG_TOUCH fields
This is necessary on Apple Touch Bars, where the tip state is contained
in fields with the HID_DG_TOUCH usage. This feature is gated by a quirk
in order to prevent breaking other devices, see commit c2ef8f21ea8f
("HID: multitouch: add support for trackpads").
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/hid-multitouch.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 902a59928..dd5509eeb 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -78,6 +78,7 @@
#define MT_QUIRK_ORIENTATION_INVERT BIT(22)
#define MT_QUIRK_HAS_TYPE_COVER_BACKLIGHT BIT(23)
#define MT_QUIRK_HAS_TYPE_COVER_TABLET_MODE_SWITCH BIT(24)
+#define MT_QUIRK_TOUCH_IS_TIPSTATE BIT(25)
#define MT_INPUTMODE_TOUCHSCREEN 0x02
#define MT_INPUTMODE_TOUCHPAD 0x03
@@ -810,6 +811,15 @@ static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
MT_STORE_FIELD(confidence_state);
return 1;
+ case HID_DG_TOUCH:
+ /*
+ * Legacy devices use TIPSWITCH and not TOUCH.
+ * Let's just ignore this field unless the quirk is set.
+ */
+ if (!(cls->quirks & MT_QUIRK_TOUCH_IS_TIPSTATE))
+ return -1;
+
+ fallthrough;
case HID_DG_TIPSWITCH:
if (field->application != HID_GD_SYSTEM_MULTIAXIS)
input_set_capability(hi->input,
@@ -873,10 +883,6 @@ static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
case HID_DG_CONTACTMAX:
/* contact max are global to the report */
return -1;
- case HID_DG_TOUCH:
- /* Legacy devices use TIPSWITCH and not TOUCH.
- * Let's just ignore this field. */
- return -1;
}
/* let hid-input decide for the others */
return 0;
--
2.42.0
From e923c6e1a5a508e341851ae020cdb3e7333ccd18 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Wed, 19 Jul 2023 19:26:57 +0300
Subject: [PATCH 06/12] HID: multitouch: take cls->maxcontacts into account for
devices without a HID_DG_CONTACTMAX field too
This is needed for Apple Touch Bars, where no HID_DG_CONTACTMAX field is
present and the maximum contact count is greater than the default.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/hid-multitouch.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index dd5509eeb..624c1d3cc 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -491,9 +491,6 @@ static void mt_feature_mapping(struct hid_device *hdev,
if (!td->maxcontacts &&
field->logical_maximum <= MT_MAX_MAXCONTACT)
td->maxcontacts = field->logical_maximum;
- if (td->mtclass.maxcontacts)
- /* check if the maxcontacts is given by the class */
- td->maxcontacts = td->mtclass.maxcontacts;
break;
case HID_DG_BUTTONTYPE:
@@ -1310,6 +1307,10 @@ static int mt_touch_input_configured(struct hid_device *hdev,
struct input_dev *input = hi->input;
int ret;
+ /* check if the maxcontacts is given by the class */
+ if (cls->maxcontacts)
+ td->maxcontacts = cls->maxcontacts;
+
if (!td->maxcontacts)
td->maxcontacts = MT_DEFAULT_MAXCONTACT;
--
2.42.0
From b9f7232d2696b91ae98fadd7b14c531aa8edceb5 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Wed, 19 Jul 2023 19:39:53 +0300
Subject: [PATCH 07/12] HID: multitouch: allow specifying if a device is direct
in a class
Currently the driver determines the device type based on the
application, but this value is not reliable on Apple Touch Bars, where
the application is HID_DG_TOUCHPAD even though the devices are direct,
so allow setting it in classes.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/hid-multitouch.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 624c1d3cc..f98fb36ff 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -147,6 +147,7 @@ struct mt_class {
__s32 sn_height; /* Signal/noise ratio for height events */
__s32 sn_pressure; /* Signal/noise ratio for pressure events */
__u8 maxcontacts;
+ bool is_direct; /* true for touchscreens */
bool is_indirect; /* true for touchpads */
bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
};
@@ -564,13 +565,13 @@ static struct mt_application *mt_allocate_application(struct mt_device *td,
mt_application->application = application;
INIT_LIST_HEAD(&mt_application->mt_usages);
- if (application == HID_DG_TOUCHSCREEN)
+ if (application == HID_DG_TOUCHSCREEN && !td->mtclass.is_indirect)
mt_application->mt_flags |= INPUT_MT_DIRECT;
/*
* Model touchscreens providing buttons as touchpads.
*/
- if (application == HID_DG_TOUCHPAD) {
+ if (application == HID_DG_TOUCHPAD && !td->mtclass.is_direct) {
mt_application->mt_flags |= INPUT_MT_POINTER;
td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
}
@@ -1318,6 +1319,9 @@ static int mt_touch_input_configured(struct hid_device *hdev,
if (td->serial_maybe)
mt_post_parse_default_settings(td, app);
+ if (cls->is_direct)
+ app->mt_flags |= INPUT_MT_DIRECT;
+
if (cls->is_indirect)
app->mt_flags |= INPUT_MT_POINTER;
--
2.42.0
From a74de0b6f2e1b79d54e84dbeab1b310232275d6c Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Wed, 19 Jul 2023 19:46:02 +0300
Subject: [PATCH 08/12] HID: multitouch: add device ID for Apple Touch Bars
Note that this is device ID is for T2 Macs. Testing on T1 Macs would be
appreciated.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/hid/Kconfig | 1 +
drivers/hid/hid-multitouch.c | 26 ++++++++++++++++++++++----
2 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 852de13aa..4e238df87 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -737,6 +737,7 @@ config HID_MULTITOUCH
Say Y here if you have one of the following devices:
- 3M PCT touch screens
- ActionStar dual touch panels
+ - Touch Bars on x86 MacBook Pros
- Atmel panels
- Cando dual touch panels
- Chunghwa panels
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index f98fb36ff..f881b19db 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -226,6 +226,7 @@
#define MT_CLS_RAZER_BLADE_STEALTH 0x0112
#define MT_CLS_SMART_TECH 0x0113
#define MT_CLS_WIN_8_MS_SURFACE_TYPE_COVER 0x0114
+#define MT_CLS_APPLE_TOUCHBAR 0x0115
#define MT_DEFAULT_MAXCONTACT 10
#define MT_MAX_MAXCONTACT 250
@@ -421,6 +421,13 @@
MT_QUIRK_WIN8_PTP_BUTTONS,
.export_all_inputs = true
},
+ { .name = MT_CLS_APPLE_TOUCHBAR,
+ .quirks = MT_QUIRK_HOVERING |
+ MT_QUIRK_TOUCH_IS_TIPSTATE |
+ MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE,
+ .is_direct = true,
+ .maxcontacts = 11,
+ },
{ }
};
@@ -1755,6 +1763,15 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
}
}
+ ret = hid_parse(hdev);
+ if (ret != 0)
+ return ret;
+
+ if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
+ !hid_find_field(hdev, HID_INPUT_REPORT,
+ HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
+ return -ENODEV;
+
td = devm_kzalloc(&hdev->dev, sizeof(struct mt_device), GFP_KERNEL);
if (!td) {
dev_err(&hdev->dev, "cannot allocate multitouch data\n");
@@ -1933,12 +1933,6 @@
timer_setup(&td->release_timer, mt_expired_timeout, 0);
- ret = hid_parse(hdev);
- if (ret != 0) {
- unregister_pm_notifier(&td->pm_notifier);
- return ret;
- }
-
if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
mt_fix_const_fields(hdev, HID_DG_CONTACTID);
@@ -2417,6 +2417,11 @@
MT_USB_DEVICE(USB_VENDOR_ID_XIROKU,
USB_DEVICE_ID_XIROKU_CSR2) },
+ /* Apple Touch Bars */
+ { .driver_data = MT_CLS_APPLE_TOUCHBAR,
+ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
+ USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
+
/* Microsoft Surface type cover */
{ .driver_data = MT_CLS_WIN_8_MS_SURFACE_TYPE_COVER,
HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY,
--
2.42.0
From f6ab7e4580962c9d82e7dc40dd074d47b2bce034 Mon Sep 17 00:00:00 2001
From: Hector Martin <marcan@marcan.st>
Date: Tue, 1 Feb 2022 00:40:51 +0900
Subject: [PATCH 09/12] lib/vsprintf: Add support for generic FOURCCs by
extending %p4cc
%p4cc is designed for DRM/V4L2 FOURCCs with their specific quirks, but
it's useful to be able to print generic 4-character codes formatted as
an integer. Extend it to add format specifiers for printing generic
32-bit FOURCCs with various endian semantics:
%p4ch Host-endian
%p4cl Little-endian
%p4cb Big-endian
%p4cr Reverse-endian
The endianness determines how bytes are interpreted as a u32, and the
FOURCC is then always printed MSByte-first (this is the opposite of
V4L/DRM FOURCCs). This covers most practical cases, e.g. %p4cr would
allow printing LSByte-first FOURCCs stored in host endian order
(other than the hex form being in character order, not the integer
value).
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
Documentation/core-api/printk-formats.rst | 32 ++++++++++++++++++++
lib/test_printf.c | 20 +++++++++----
lib/vsprintf.c | 36 +++++++++++++++++++----
scripts/checkpatch.pl | 2 +-
4 files changed, 77 insertions(+), 13 deletions(-)
diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index dfe7e75a7..0ccef63e6 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -631,6 +631,38 @@ Examples::
%p4cc Y10 little-endian (0x20303159)
%p4cc NV12 big-endian (0xb231564e)
+Generic FourCC code
+-------------------
+
+::
+ %p4c[hnbl] gP00 (0x67503030)
+
+Print a generic FourCC code, as both ASCII characters and its numerical
+value as hexadecimal.
+
+The additional ``h``, ``r``, ``b``, and ``l`` specifiers are used to specify
+host, reversed, big or little endian order data respectively. Host endian
+order means the data is interpreted as a 32-bit integer and the most
+significant byte is printed first; that is, the character code as printed
+matches the byte order stored in memory on big-endian systems, and is reversed
+on little-endian systems.
+
+Passed by reference.
+
+Examples for a little-endian machine, given &(u32)0x67503030::
+
+ %p4ch gP00 (0x67503030)
+ %p4cl gP00 (0x67503030)
+ %p4cb 00Pg (0x30305067)
+ %p4cr 00Pg (0x30305067)
+
+Examples for a big-endian machine, given &(u32)0x67503030::
+
+ %p4ch gP00 (0x67503030)
+ %p4cl 00Pg (0x30305067)
+ %p4cb gP00 (0x67503030)
+ %p4cr 00Pg (0x30305067)
+
Rust
----
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 7677ebccf..2355be36f 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -746,18 +746,26 @@ static void __init fwnode_pointer(void)
static void __init fourcc_pointer(void)
{
struct {
+ char type;
u32 code;
char *str;
} const try[] = {
- { 0x3231564e, "NV12 little-endian (0x3231564e)", },
- { 0xb231564e, "NV12 big-endian (0xb231564e)", },
- { 0x10111213, ".... little-endian (0x10111213)", },
- { 0x20303159, "Y10 little-endian (0x20303159)", },
+ { 'c', 0x3231564e, "NV12 little-endian (0x3231564e)", },
+ { 'c', 0xb231564e, "NV12 big-endian (0xb231564e)", },
+ { 'c', 0x10111213, ".... little-endian (0x10111213)", },
+ { 'c', 0x20303159, "Y10 little-endian (0x20303159)", },
+ { 'h', 0x67503030, "gP00 (0x67503030)", },
+ { 'r', 0x30305067, "gP00 (0x67503030)", },
+ { 'l', cpu_to_le32(0x67503030), "gP00 (0x67503030)", },
+ { 'b', cpu_to_be32(0x67503030), "gP00 (0x67503030)", },
};
unsigned int i;
- for (i = 0; i < ARRAY_SIZE(try); i++)
- test(try[i].str, "%p4cc", &try[i].code);
+ for (i = 0; i < ARRAY_SIZE(try); i++) {
+ char fmt[] = { '%', 'p', '4', 'c', try[i].type, '\0' };
+
+ test(try[i].str, fmt, &try[i].code);
+ }
}
static void __init
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 40f560959..bd9af783c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1758,27 +1758,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
char output[sizeof("0123 little-endian (0x01234567)")];
char *p = output;
unsigned int i;
+ bool pix_fmt = false;
u32 orig, val;
- if (fmt[1] != 'c' || fmt[2] != 'c')
+ if (fmt[1] != 'c')
return error_string(buf, end, "(%p4?)", spec);
if (check_pointer(&buf, end, fourcc, spec))
return buf;
orig = get_unaligned(fourcc);
- val = orig & ~BIT(31);
+ switch (fmt[2]) {
+ case 'h':
+ val = orig;
+ break;
+ case 'r':
+ val = orig = swab32(orig);
+ break;
+ case 'l':
+ val = orig = le32_to_cpu(orig);
+ break;
+ case 'b':
+ val = orig = be32_to_cpu(orig);
+ break;
+ case 'c':
+ /* Pixel formats are printed LSB-first */
+ val = swab32(orig & ~BIT(31));
+ pix_fmt = true;
+ break;
+ default:
+ return error_string(buf, end, "(%p4?)", spec);
+ }
for (i = 0; i < sizeof(u32); i++) {
- unsigned char c = val >> (i * 8);
+ unsigned char c = val >> ((3 - i) * 8);
/* Print non-control ASCII characters as-is, dot otherwise */
*p++ = isascii(c) && isprint(c) ? c : '.';
}
- *p++ = ' ';
- strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
- p += strlen(p);
+ if (pix_fmt) {
+ *p++ = ' ';
+ strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
+ p += strlen(p);
+ }
*p++ = ' ';
*p++ = '(';
@@ -2348,6 +2371,7 @@ char *rust_fmt_argument(char *buf, char *end, void *ptr);
* read the documentation (path below) first.
* - 'NF' For a netdev_features_t
* - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
+ * - '4c[hlbr]' Generic FourCC code.
* - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
* a certain separator (' ' by default):
* C colon
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 880fde13d..f080e33a4 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6906,7 +6906,7 @@ sub process {
($extension eq "f" &&
defined $qualifier && $qualifier !~ /^w/) ||
($extension eq "4" &&
- defined $qualifier && $qualifier !~ /^cc/)) {
+ defined $qualifier && $qualifier !~ /^c[chlbr]/)) {
$bad_specifier = $specifier;
last;
}
--
2.42.0
From f893444f7c842f97f3707897ba29f2c8dd77c8df Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Mon, 7 Aug 2023 20:29:27 +0300
Subject: [PATCH 10/12] USB: core: add 'shutdown' callback to usb_driver
This simplifies running code on shutdown for USB drivers.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/usb/core/driver.c | 14 ++++++++++++++
drivers/usb/storage/uas.c | 5 ++---
include/linux/usb.h | 3 +++
3 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index f58a0299f..dc0f86376 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -514,6 +514,19 @@ static int usb_unbind_interface(struct device *dev)
return 0;
}
+static void usb_shutdown_interface(struct device *dev)
+{
+ struct usb_interface *intf = to_usb_interface(dev);
+ struct usb_driver *driver;
+
+ if (!dev->driver)
+ return;
+
+ driver = to_usb_driver(dev->driver);
+ if (driver->shutdown)
+ driver->shutdown(intf);
+}
+
/**
* usb_driver_claim_interface - bind a driver to an interface
* @driver: the driver to be bound
@@ -1053,6 +1066,7 @@ int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
new_driver->driver.bus = &usb_bus_type;
new_driver->driver.probe = usb_probe_interface;
new_driver->driver.remove = usb_unbind_interface;
+ new_driver->driver.shutdown = usb_shutdown_interface;
new_driver->driver.owner = owner;
new_driver->driver.mod_name = mod_name;
new_driver->driver.dev_groups = new_driver->dev_groups;
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index 2583ee981..591fa0379 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -1221,9 +1221,8 @@ static void uas_disconnect(struct usb_interface *intf)
* hang on reboot when the device is still in uas mode. Note the reset is
* necessary as some devices won't revert to usb-storage mode without it.
*/
-static void uas_shutdown(struct device *dev)
+static void uas_shutdown(struct usb_interface *intf)
{
- struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *udev = interface_to_usbdev(intf);
struct Scsi_Host *shost = usb_get_intfdata(intf);
struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
@@ -1246,7 +1245,7 @@ static struct usb_driver uas_driver = {
.suspend = uas_suspend,
.resume = uas_resume,
.reset_resume = uas_reset_resume,
- .driver.shutdown = uas_shutdown,
+ .shutdown = uas_shutdown,
.id_table = uas_usb_ids,
};
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 25f8e62a3..5f3ae2186 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1194,6 +1194,7 @@ struct usbdrv_wrap {
* post_reset method is called.
* @post_reset: Called by usb_reset_device() after the device
* has been reset
+ * @shutdown: Called at shut-down time to quiesce the device.
* @id_table: USB drivers use ID table to support hotplugging.
* Export this with MODULE_DEVICE_TABLE(usb,...). This must be set
* or your driver's probe function will never get called.
@@ -1245,6 +1246,8 @@ struct usb_driver {
int (*pre_reset)(struct usb_interface *intf);
int (*post_reset)(struct usb_interface *intf);
+ void (*shutdown)(struct usb_interface *intf);
+
const struct usb_device_id *id_table;
const struct attribute_group **dev_groups;
--
2.42.0
From 337d6f6e34daaa786a0fb70d0dbd553288cd5ecd Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Fri, 4 Aug 2023 17:49:25 +0300
Subject: [PATCH 11/12] drm/format-helper: add helper for BGR888 to XRGB8888
conversion
Add XRGB8888 emulation helper for devices that only support BGR888.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
drivers/gpu/drm/drm_format_helper.c | 53 ++++++++++++++
.../gpu/drm/tests/drm_format_helper_test.c | 69 +++++++++++++++++++
include/drm/drm_format_helper.h | 3 +
3 files changed, 125 insertions(+)
diff --git a/drivers/gpu/drm/drm_format_helper.c b/drivers/gpu/drm/drm_format_helper.c
index b1be458ed4dda5..24836384554a47 100644
--- a/drivers/gpu/drm/drm_format_helper.c
+++ b/drivers/gpu/drm/drm_format_helper.c
@@ -702,6 +702,58 @@ void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pi
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
+static void drm_fb_xrgb8888_to_bgr888_line(void *dbuf, const void *sbuf, unsigned int pixels)
+{
+ u8 *dbuf8 = dbuf;
+ const __le32 *sbuf32 = sbuf;
+ unsigned int x;
+ u32 pix;
+
+ for (x = 0; x < pixels; x++) {
+ pix = le32_to_cpu(sbuf32[x]);
+ /* write red-green-blue to output in little endianness */
+ *dbuf8++ = (pix & 0x00FF0000) >> 16;
+ *dbuf8++ = (pix & 0x0000FF00) >> 8;
+ *dbuf8++ = (pix & 0x000000FF) >> 0;
+ }
+}
+
+/**
+ * drm_fb_xrgb8888_to_bgr888 - Convert XRGB8888 to BGR888 clip buffer
+ * @dst: Array of BGR888 destination buffers
+ * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
+ * within @dst; can be NULL if scanlines are stored next to each other.
+ * @src: Array of XRGB8888 source buffers
+ * @fb: DRM framebuffer
+ * @clip: Clip rectangle area to copy
+ * @state: Transform and conversion state
+ *
+ * This function copies parts of a framebuffer to display memory and converts the
+ * color format during the process. Destination and framebuffer formats must match. The
+ * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
+ * least as many entries as there are planes in @fb's format. Each entry stores the
+ * value for the format's respective color plane at the same index.
+ *
+ * This function does not apply clipping on @dst (i.e. the destination is at the
+ * top-left corner).
+ *
+ * Drivers can use this function for BGR888 devices that don't natively
+ * support XRGB8888.
+ */
+void drm_fb_xrgb8888_to_bgr888(struct iosys_map *dst, const unsigned int *dst_pitch,
+ const struct iosys_map *src, const struct drm_framebuffer *fb,
+ const struct drm_rect *clip,
+ struct drm_format_conv_state *state)
+{
+ static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
+ 3,
+ };
+
+ drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
+ state, drm_fb_xrgb8888_to_bgr888_line);
+}
+EXPORT_SYMBOL(drm_fb_xrgb8888_to_bgr888);
+
static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
{
__le32 *dbuf32 = dbuf;
@@ -1035,6 +1087,9 @@ int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t d
} else if (dst_format == DRM_FORMAT_RGB888) {
drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip, state);
return 0;
+ } else if (dst_format == DRM_FORMAT_BGR888) {
+ drm_fb_xrgb8888_to_bgr888(dst, dst_pitch, src, fb, clip, state);
+ return 0;
} else if (dst_format == DRM_FORMAT_ARGB8888) {
drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip, state);
return 0;
diff --git a/drivers/gpu/drm/tests/drm_format_helper_test.c b/drivers/gpu/drm/tests/drm_format_helper_test.c
index 474bb7a1c..dff7fabd9 100644
--- a/drivers/gpu/drm/tests/drm_format_helper_test.c
+++ b/drivers/gpu/drm/tests/drm_format_helper_test.c
@@ -52,6 +52,11 @@ struct convert_to_rgb888_result {
const u8 expected[TEST_BUF_SIZE];
};
+struct convert_to_bgr888_result {
+ unsigned int dst_pitch;
+ const u8 expected[TEST_BUF_SIZE];
+};
+
struct convert_to_argb8888_result {
unsigned int dst_pitch;
const u32 expected[TEST_BUF_SIZE];
@@ -84,6 +89,7 @@ struct convert_xrgb8888_case {
struct convert_to_argb1555_result argb1555_result;
struct convert_to_rgba5551_result rgba5551_result;
struct convert_to_rgb888_result rgb888_result;
+ struct convert_to_bgr888_result bgr888_result;
struct convert_to_argb8888_result argb8888_result;
struct convert_to_xrgb2101010_result xrgb2101010_result;
struct convert_to_argb2101010_result argb2101010_result;
@@ -125,6 +131,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
.dst_pitch = TEST_USE_DEFAULT_PITCH,
.expected = { 0x00, 0x00, 0xFF },
},
+ .bgr888_result = {
+ .dst_pitch = TEST_USE_DEFAULT_PITCH,
+ .expected = { 0xFF, 0x00, 0x00 },
+ },
.argb8888_result = {
.dst_pitch = TEST_USE_DEFAULT_PITCH,
.expected = { 0xFFFF0000 },
@@ -179,6 +189,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
.dst_pitch = TEST_USE_DEFAULT_PITCH,
.expected = { 0x00, 0x00, 0xFF },
},
+ .bgr888_result = {
+ .dst_pitch = TEST_USE_DEFAULT_PITCH,
+ .expected = { 0xFF, 0x00, 0x00 },
+ },
.argb8888_result = {
.dst_pitch = TEST_USE_DEFAULT_PITCH,
.expected = { 0xFFFF0000 },
@@ -280,6 +294,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
},
},
+ .bgr888_result = {
+ .dst_pitch = TEST_USE_DEFAULT_PITCH,
+ .expected = {
+ 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
+ 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00,
+ 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF,
+ 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF,
+ },
+ },
.argb8888_result = {
.dst_pitch = TEST_USE_DEFAULT_PITCH,
.expected = {
@@ -391,6 +414,17 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
+ .bgr888_result = {
+ .dst_pitch = 15,
+ .expected = {
+ 0x0E, 0x44, 0x9C, 0x11, 0x4D, 0x05, 0xA8, 0xF3, 0x03,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x6C, 0xF0, 0x73, 0x0E, 0x44, 0x9C, 0x11, 0x4D, 0x05,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xA8, 0x03, 0x03, 0x6C, 0xF0, 0x73, 0x0E, 0x44, 0x9C,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ },
+ },
.argb8888_result = {
.dst_pitch = 20,
.expected = {
diff --git a/include/drm/drm_format_helper.h b/include/drm/drm_format_helper.h
index 291deb094..7fc553318 100644
--- a/include/drm/drm_format_helper.h
+++ b/include/drm/drm_format_helper.h
@@ -42,6 +42,9 @@ void drm_fb_xrgb8888_to_rgba5551(struct iosys_map *dst, const unsigned int *dst_
void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
const struct iosys_map *src, const struct drm_framebuffer *fb,
const struct drm_rect *clip, struct drm_format_conv_state *state);
+void drm_fb_xrgb8888_to_bgr888(struct iosys_map *dst, const unsigned int *dst_pitch,
+ const struct iosys_map *src, const struct drm_framebuffer *fb,
+ const struct drm_rect *clip, struct drm_format_conv_state *state);
void drm_fb_xrgb8888_to_argb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
const struct iosys_map *src, const struct drm_framebuffer *fb,
const struct drm_rect *clip, struct drm_format_conv_state *state);
--
2.42.0
From 1f0b6c21c4d56f5be74c4d7d0665525862e307c3 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Sat, 6 May 2023 17:30:09 +0300
Subject: [PATCH 12/12] drm/tiny: add driver for Apple Touch Bars in x86 Macs
The Touch Bars found on x86 Macs support two USB configurations: one
where the device presents itself as a HID keyboard and can display
predefined sets of keys, and one where the operating system has full
control over what is displayed. This commit adds support for the display
functionality of the second configuration.
Note that this driver has only been tested on T2 Macs, and only includes
the USB device ID for these devices. Testing on T1 Macs would be
appreciated.
Credit goes to @imbushuo on GitHub for reverse engineering most of the
protocol.
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
---
MAINTAINERS | 6 +
drivers/gpu/drm/tiny/Kconfig | 12 +
drivers/gpu/drm/tiny/Makefile | 1 +
drivers/gpu/drm/tiny/appletbdrm.c | 624 ++++++++++++++++++++++++++++++
4 files changed, 643 insertions(+)
create mode 100644 drivers/gpu/drm/tiny/appletbdrm.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 519b3b736..dfc63d257 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6545,6 +6545,12 @@
T: git git://anongit.freedesktop.org/drm/drm-misc
F: drivers/gpu/drm/sun4i/sun8i*
+DRM DRIVER FOR APPLE TOUCH BARS
+M: Kerem Karabay <kekrby@gmail.com>
+L: dri-devel@lists.freedesktop.org
+S: Maintained
+F: drivers/gpu/drm/tiny/appletbdrm.c
+
DRM DRIVER FOR ARM PL111 CLCD
S: Orphan
T: git git://anongit.freedesktop.org/drm/drm-misc
diff --git a/drivers/gpu/drm/tiny/Kconfig b/drivers/gpu/drm/tiny/Kconfig
index f6889f649..559a97bce 100644
--- a/drivers/gpu/drm/tiny/Kconfig
+++ b/drivers/gpu/drm/tiny/Kconfig
@@ -1,5 +1,17 @@
# SPDX-License-Identifier: GPL-2.0-only
+config DRM_APPLETBDRM
+ tristate "DRM support for Apple Touch Bars"
+ depends on DRM && USB && MMU
+ select DRM_KMS_HELPER
+ select DRM_GEM_SHMEM_HELPER
+ help
+ Say Y here if you want support for the display of Touch Bars on x86
+ MacBook Pros.
+
+ To compile this driver as a module, choose M here: the
+ module will be called appletbdrm.
+
config DRM_ARCPGU
tristate "ARC PGU"
depends on DRM && OF
diff --git a/drivers/gpu/drm/tiny/Makefile b/drivers/gpu/drm/tiny/Makefile
index 76dde89a0..9a1b412e7 100644
--- a/drivers/gpu/drm/tiny/Makefile
+++ b/drivers/gpu/drm/tiny/Makefile
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_DRM_APPLETBDRM) += appletbdrm.o
obj-$(CONFIG_DRM_ARCPGU) += arcpgu.o
obj-$(CONFIG_DRM_BOCHS) += bochs.o
obj-$(CONFIG_DRM_CIRRUS_QEMU) += cirrus.o
diff --git a/drivers/gpu/drm/tiny/appletbdrm.c b/drivers/gpu/drm/tiny/appletbdrm.c
new file mode 100644
index 000000000..33a99436b
--- /dev/null
+++ b/drivers/gpu/drm/tiny/appletbdrm.c
@@ -0,0 +1,624 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar DRM Driver
+ *
+ * Copyright (c) 2023 Kerem Karabay <kekrby@gmail.com>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <asm/unaligned.h>
+
+#include <linux/usb.h>
+#include <linux/module.h>
+
+#include <drm/drm_drv.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_probe_helper.h>
+#include <drm/drm_atomic_helper.h>
+#include <drm/drm_damage_helper.h>
+#include <drm/drm_format_helper.h>
+#include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
+
+#define _APPLETBDRM_FOURCC(s) (((s)[0] << 24) | ((s)[1] << 16) | ((s)[2] << 8) | (s)[3])
+#define APPLETBDRM_FOURCC(s) _APPLETBDRM_FOURCC(#s)
+
+#define APPLETBDRM_PIXEL_FORMAT APPLETBDRM_FOURCC(RGBA) /* The actual format is BGR888 */
+#define APPLETBDRM_BITS_PER_PIXEL 24
+
+#define APPLETBDRM_MSG_CLEAR_DISPLAY APPLETBDRM_FOURCC(CLRD)
+#define APPLETBDRM_MSG_GET_INFORMATION APPLETBDRM_FOURCC(GINF)
+#define APPLETBDRM_MSG_UPDATE_COMPLETE APPLETBDRM_FOURCC(UDCL)
+#define APPLETBDRM_MSG_SIGNAL_READINESS APPLETBDRM_FOURCC(REDY)
+
+#define APPLETBDRM_BULK_MSG_TIMEOUT 1000
+
+#define drm_to_adev(_drm) container_of(_drm, struct appletbdrm_device, drm)
+#define adev_to_udev(adev) interface_to_usbdev(to_usb_interface(adev->dev))
+
+struct appletbdrm_device {
+ struct device *dev;
+
+ u8 in_ep;
+ u8 out_ep;
+
+ u32 width;
+ u32 height;
+
+ struct drm_device drm;
+ struct drm_display_mode mode;
+ struct drm_connector connector;
+ struct drm_simple_display_pipe pipe;
+
+ bool readiness_signal_received;
+};
+
+struct appletbdrm_request_header {
+ __le16 unk_00;
+ __le16 unk_02;
+ __le32 unk_04;
+ __le32 unk_08;
+ __le32 size;
+} __packed;
+
+struct appletbdrm_response_header {
+ u8 unk_00[16];
+ u32 msg;
+} __packed;
+
+struct appletbdrm_simple_request {
+ struct appletbdrm_request_header header;
+ u32 msg;
+ u8 unk_14[8];
+ __le32 size;
+} __packed;
+
+struct appletbdrm_information {
+ struct appletbdrm_response_header header;
+ u8 unk_14[12];
+ __le32 width;
+ __le32 height;
+ u8 bits_per_pixel;
+ __le32 bytes_per_row;
+ __le32 orientation;
+ __le32 bitmap_info;
+ u32 pixel_format;
+ __le32 width_inches; /* floating point */
+ __le32 height_inches; /* floating point */
+} __packed;
+
+struct appletbdrm_frame {
+ __le16 begin_x;
+ __le16 begin_y;
+ __le16 width;
+ __le16 height;
+ __le32 buf_size;
+ u8 buf[];
+} __packed;
+
+struct appletbdrm_fb_request_footer {
+ u8 unk_00[12];
+ __le32 unk_0c;
+ u8 unk_10[12];
+ __le32 unk_1c;
+ __le64 timestamp;
+ u8 unk_28[12];
+ __le32 unk_34;
+ u8 unk_38[20];
+ __le32 unk_4c;
+} __packed;
+
+struct appletbdrm_fb_request {
+ struct appletbdrm_request_header header;
+ __le16 unk_10;
+ u8 msg_id;
+ u8 unk_13[29];
+ /*
+ * Contents of `data`:
+ * - struct appletbdrm_frame frames[];
+ * - struct appletbdrm_fb_request_footer footer;
+ * - padding to make the total size a multiple of 16
+ */
+ u8 data[];
+} __packed;
+
+struct appletbdrm_fb_request_response {
+ struct appletbdrm_response_header header;
+ u8 unk_14[12];
+ __le64 timestamp;
+} __packed;
+
+static int appletbdrm_send_request(struct appletbdrm_device *adev,
+ struct appletbdrm_request_header *request, size_t size)
+{
+ struct usb_device *udev = adev_to_udev(adev);
+ struct drm_device *drm = &adev->drm;
+ int ret, actual_size;
+
+ ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, adev->out_ep),
+ request, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT);
+ if (ret) {
+ drm_err(drm, "Failed to send message (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ if (actual_size != size) {
+ drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n",
+ actual_size, size);
+ return -EIO;
+ }
+
+ return ret;
+}
+
+static int appletbdrm_read_response(struct appletbdrm_device *adev,
+ struct appletbdrm_response_header *response,
+ size_t size, u32 expected_response)
+{
+ struct usb_device *udev = adev_to_udev(adev);
+ struct drm_device *drm = &adev->drm;
+ int ret, actual_size;
+
+retry:
+ ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, adev->in_ep),
+ response, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT);
+ if (ret) {
+ drm_err(drm, "Failed to read response (%pe)\n", ERR_PTR(ret));
+ return ret;
+ }
+
+ /*
+ * The device responds to the first request sent in a particular
+ * timeframe after the USB device configuration is set with a readiness
+ * signal, in which case the response should be read again
+ */
+ if (response->msg == APPLETBDRM_MSG_SIGNAL_READINESS) {
+ if (!adev->readiness_signal_received) {
+ adev->readiness_signal_received = true;
+ goto retry;
+ }
+
+ drm_err(drm, "Encountered unexpected readiness signal\n");
+ return -EIO;
+ }
+
+ if (actual_size != size) {
+ drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n",
+ actual_size, size);
+ return -EIO;
+ }
+
+ if (response->msg != expected_response) {
+ drm_err(drm, "Unexpected response from device (expected %p4ch found %p4ch)\n",
+ &expected_response, &response->msg);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int appletbdrm_send_msg(struct appletbdrm_device *adev, u32 msg)
+{
+ struct appletbdrm_simple_request *request;
+ int ret;
+
+ request = kzalloc(sizeof(*request), GFP_KERNEL);
+ if (!request)
+ return -ENOMEM;
+
+ request->header.unk_00 = cpu_to_le16(2);
+ request->header.unk_02 = cpu_to_le16(0x1512);
+ request->header.size = cpu_to_le32(sizeof(*request) - sizeof(request->header));
+ request->msg = msg;
+ request->size = request->header.size;
+
+ ret = appletbdrm_send_request(adev, &request->header, sizeof(*request));
+
+ kfree(request);
+
+ return ret;
+}
+
+static int appletbdrm_clear_display(struct appletbdrm_device *adev)
+{
+ return appletbdrm_send_msg(adev, APPLETBDRM_MSG_CLEAR_DISPLAY);
+}
+
+static int appletbdrm_signal_readiness(struct appletbdrm_device *adev)
+{
+ return appletbdrm_send_msg(adev, APPLETBDRM_MSG_SIGNAL_READINESS);
+}
+
+static int appletbdrm_get_information(struct appletbdrm_device *adev)
+{
+ struct appletbdrm_information *info;
+ struct drm_device *drm = &adev->drm;
+ u8 bits_per_pixel;
+ u32 pixel_format;
+ int ret;
+
+ info = kzalloc(sizeof(*info), GFP_KERNEL);
+ if (!info)
+ return -ENOMEM;
+
+ ret = appletbdrm_send_msg(adev, APPLETBDRM_MSG_GET_INFORMATION);
+ if (ret)
+ return ret;
+
+ ret = appletbdrm_read_response(adev, &info->header, sizeof(*info),
+ APPLETBDRM_MSG_GET_INFORMATION);
+ if (ret)
+ goto free_info;
+
+ bits_per_pixel = info->bits_per_pixel;
+ pixel_format = get_unaligned(&info->pixel_format);
+
+ adev->width = get_unaligned_le32(&info->width);
+ adev->height = get_unaligned_le32(&info->height);
+
+ if (bits_per_pixel != APPLETBDRM_BITS_PER_PIXEL) {
+ drm_err(drm, "Encountered unexpected bits per pixel value (%d)\n", bits_per_pixel);
+ ret = -EINVAL;
+ goto free_info;
+ }
+
+ if (pixel_format != APPLETBDRM_PIXEL_FORMAT) {
+ drm_err(drm, "Encountered unknown pixel format (%p4ch)\n", &pixel_format);
+ ret = -EINVAL;
+ goto free_info;
+ }
+
+free_info:
+ kfree(info);
+
+ return ret;
+}
+
+static u32 rect_size(struct drm_rect *rect)
+{
+ return drm_rect_width(rect) * drm_rect_height(rect) * (APPLETBDRM_BITS_PER_PIXEL / 8);
+}
+
+static int appletbdrm_flush_damage(struct appletbdrm_device *adev,
+ struct drm_plane_state *old_state,
+ struct drm_plane_state *state)
+{
+ struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
+ struct appletbdrm_fb_request_response *response;
+ struct appletbdrm_fb_request_footer *footer;
+ struct drm_atomic_helper_damage_iter iter;
+ struct drm_framebuffer *fb = state->fb;
+ struct appletbdrm_fb_request *request;
+ struct drm_device *drm = &adev->drm;
+ struct appletbdrm_frame *frame;
+ u64 timestamp = ktime_get_ns();
+ struct drm_rect damage;
+ size_t frames_size = 0;
+ size_t request_size;
+ int ret;
+
+ drm_atomic_helper_damage_iter_init(&iter, old_state, state);
+ drm_atomic_for_each_plane_damage(&iter, &damage) {
+ frames_size += struct_size(frame, buf, rect_size(&damage));
+ }
+
+ if (!frames_size)
+ return 0;
+
+ request_size = ALIGN(sizeof(*request) + frames_size + sizeof(*footer), 16);
+
+ request = kzalloc(request_size, GFP_KERNEL);
+ if (!request)
+ return -ENOMEM;
+
+ response = kzalloc(sizeof(*response), GFP_KERNEL);
+ if (!response) {
+ ret = -ENOMEM;
+ goto free_request;
+ }
+
+ ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
+ if (ret) {
+ drm_err(drm, "Failed to start CPU framebuffer access (%pe)\n", ERR_PTR(ret));
+ goto free_response;
+ }
+
+ request->header.unk_00 = cpu_to_le16(2);
+ request->header.unk_02 = cpu_to_le16(0x12);
+ request->header.unk_04 = cpu_to_le32(9);
+ request->header.size = cpu_to_le32(request_size - sizeof(request->header));
+ request->unk_10 = cpu_to_le16(1);
+ request->msg_id = timestamp & 0xff;
+
+ frame = (struct appletbdrm_frame *)request->data;
+
+ drm_atomic_helper_damage_iter_init(&iter, old_state, state);
+ drm_atomic_for_each_plane_damage(&iter, &damage) {
+ struct iosys_map dst = IOSYS_MAP_INIT_VADDR(frame->buf);
+ u32 buf_size = rect_size(&damage);
+
+ /*
+ * The coordinates need to be translated to the coordinate
+ * system the device expects, see the comment in
+ * appletbdrm_setup_mode_config
+ */
+ frame->begin_x = cpu_to_le16(damage.y1);
+ frame->begin_y = cpu_to_le16(adev->height - damage.x2);
+ frame->width = cpu_to_le16(drm_rect_height(&damage));
+ frame->height = cpu_to_le16(drm_rect_width(&damage));
+ frame->buf_size = cpu_to_le32(buf_size);
+
+ ret = drm_fb_blit(&dst, NULL, DRM_FORMAT_BGR888,
+ &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state);
+ if (ret) {
+ drm_err(drm, "Failed to copy damage clip (%pe)\n", ERR_PTR(ret));
+ goto end_fb_cpu_access;
+ }
+
+ frame = (void *)frame + struct_size(frame, buf, buf_size);
+ }
+
+ footer = (struct appletbdrm_fb_request_footer *)&request->data[frames_size];
+
+ footer->unk_0c = cpu_to_le32(0xfffe);
+ footer->unk_1c = cpu_to_le32(0x80001);
+ footer->unk_34 = cpu_to_le32(0x80002);
+ footer->unk_4c = cpu_to_le32(0xffff);
+ footer->timestamp = cpu_to_le64(timestamp);
+
+ ret = appletbdrm_send_request(adev, &request->header, request_size);
+ if (ret)
+ goto end_fb_cpu_access;
+
+ ret = appletbdrm_read_response(adev, &response->header, sizeof(*response),
+ APPLETBDRM_MSG_UPDATE_COMPLETE);
+ if (ret)
+ goto end_fb_cpu_access;
+
+ if (response->timestamp != footer->timestamp) {
+ drm_err(drm, "Response timestamp (%llu) doesn't match request timestamp (%llu)\n",
+ le64_to_cpu(response->timestamp), timestamp);
+ goto end_fb_cpu_access;
+ }
+
+end_fb_cpu_access:
+ drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
+free_response:
+ kfree(response);
+free_request:
+ kfree(request);
+
+ return ret;
+}
+
+static int appletbdrm_connector_helper_get_modes(struct drm_connector *connector)
+{
+ struct appletbdrm_device *adev = drm_to_adev(connector->dev);
+
+ return drm_connector_helper_get_modes_fixed(connector, &adev->mode);
+}
+
+static enum drm_mode_status appletbdrm_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
+ const struct drm_display_mode *mode)
+{
+ struct drm_crtc *crtc = &pipe->crtc;
+ struct appletbdrm_device *adev = drm_to_adev(crtc->dev);
+
+ return drm_crtc_helper_mode_valid_fixed(crtc, mode, &adev->mode);
+}
+
+static void appletbdrm_pipe_disable(struct drm_simple_display_pipe *pipe)
+{
+ struct appletbdrm_device *adev = drm_to_adev(pipe->crtc.dev);
+ int idx;
+
+ if (!drm_dev_enter(&adev->drm, &idx))
+ return;
+
+ appletbdrm_clear_display(adev);
+
+ drm_dev_exit(idx);
+}
+
+static void appletbdrm_pipe_update(struct drm_simple_display_pipe *pipe,
+ struct drm_plane_state *old_state)
+{
+ struct drm_crtc *crtc = &pipe->crtc;
+ struct appletbdrm_device *adev = drm_to_adev(crtc->dev);
+ int idx;
+
+ if (!crtc->state->active || !drm_dev_enter(&adev->drm, &idx))
+ return;
+
+ appletbdrm_flush_damage(adev, old_state, pipe->plane.state);
+
+ drm_dev_exit(idx);
+}
+
+static const u32 appletbdrm_formats[] = {
+ DRM_FORMAT_BGR888,
+ DRM_FORMAT_XRGB8888, /* emulated */
+};
+
+static const struct drm_mode_config_funcs appletbdrm_mode_config_funcs = {
+ .fb_create = drm_gem_fb_create_with_dirty,
+ .atomic_check = drm_atomic_helper_check,
+ .atomic_commit = drm_atomic_helper_commit,
+};
+
+static const struct drm_connector_funcs appletbdrm_connector_funcs = {
+ .reset = drm_atomic_helper_connector_reset,
+ .destroy = drm_connector_cleanup,
+ .fill_modes = drm_helper_probe_single_connector_modes,
+ .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
+ .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
+};
+
+static const struct drm_connector_helper_funcs appletbdrm_connector_helper_funcs = {
+ .get_modes = appletbdrm_connector_helper_get_modes,
+};
+
+static const struct drm_simple_display_pipe_funcs appletbdrm_pipe_funcs = {
+ DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
+ .update = appletbdrm_pipe_update,
+ .disable = appletbdrm_pipe_disable,
+ .mode_valid = appletbdrm_pipe_mode_valid,
+};
+
+DEFINE_DRM_GEM_FOPS(appletbdrm_drm_fops);
+
+static const struct drm_driver appletbdrm_drm_driver = {
+ DRM_GEM_SHMEM_DRIVER_OPS,
+ .name = "appletbdrm",
+ .desc = "Apple Touch Bar DRM Driver",
+ .date = "20230910",
+ .major = 1,
+ .minor = 0,
+ .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
+ .fops = &appletbdrm_drm_fops,
+};
+
+static int appletbdrm_setup_mode_config(struct appletbdrm_device *adev)
+{
+ struct drm_connector *connector = &adev->connector;
+ struct drm_device *drm = &adev->drm;
+ struct device *dev = adev->dev;
+ int ret;
+
+ ret = drmm_mode_config_init(drm);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to initialize mode configuration\n");
+
+ /*
+ * The coordinate system used by the device is different from the
+ * coordinate system of the framebuffer in that the x and y axes are
+ * swapped, and that the y axis is inverted; so what the device reports
+ * as the height is actually the width of the framebuffer and vice
+ * versa
+ */
+ drm->mode_config.min_width = 0;
+ drm->mode_config.min_height = 0;
+ drm->mode_config.max_width = max(adev->height, DRM_SHADOW_PLANE_MAX_WIDTH);
+ drm->mode_config.max_height = max(adev->width, DRM_SHADOW_PLANE_MAX_HEIGHT);
+ drm->mode_config.preferred_depth = APPLETBDRM_BITS_PER_PIXEL;
+ drm->mode_config.funcs = &appletbdrm_mode_config_funcs;
+
+ adev->mode = (struct drm_display_mode) {
+ DRM_MODE_INIT(60, adev->height, adev->width,
+ DRM_MODE_RES_MM(adev->height, 218),
+ DRM_MODE_RES_MM(adev->width, 218))
+ };
+
+ ret = drm_connector_init(drm, connector,
+ &appletbdrm_connector_funcs, DRM_MODE_CONNECTOR_USB);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to initialize connector\n");
+
+ drm_connector_helper_add(connector, &appletbdrm_connector_helper_funcs);
+
+ ret = drm_connector_set_panel_orientation(connector,
+ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to set panel orientation\n");
+
+ connector->display_info.non_desktop = true;
+ ret = drm_object_property_set_value(&connector->base,
+ drm->mode_config.non_desktop_property, true);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to set non-desktop property\n");
+
+ ret = drm_simple_display_pipe_init(drm, &adev->pipe, &appletbdrm_pipe_funcs,
+ appletbdrm_formats, ARRAY_SIZE(appletbdrm_formats),
+ NULL, &adev->connector);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to initialize simple display pipe\n");
+
+ drm_plane_enable_fb_damage_clips(&adev->pipe.plane);
+
+ drm_mode_config_reset(drm);
+
+ ret = drm_dev_register(drm, 0);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register DRM device\n");
+
+ return 0;
+}
+
+static int appletbdrm_probe(struct usb_interface *intf,
+ const struct usb_device_id *id)
+{
+ struct usb_endpoint_descriptor *bulk_in, *bulk_out;
+ struct device *dev = &intf->dev;
+ struct appletbdrm_device *adev;
+ int ret;
+
+ ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to find bulk endpoints\n");
+
+ adev = devm_drm_dev_alloc(dev, &appletbdrm_drm_driver, struct appletbdrm_device, drm);
+ if (IS_ERR(adev))
+ return PTR_ERR(adev);
+
+ adev->dev = dev;
+ adev->in_ep = bulk_in->bEndpointAddress;
+ adev->out_ep = bulk_out->bEndpointAddress;
+
+ usb_set_intfdata(intf, adev);
+
+ ret = appletbdrm_get_information(adev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to get display information\n");
+
+ ret = appletbdrm_signal_readiness(adev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to signal readiness\n");
+
+ ret = appletbdrm_clear_display(adev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to clear display\n");
+
+ return appletbdrm_setup_mode_config(adev);
+}
+
+static void appletbdrm_disconnect(struct usb_interface *intf)
+{
+ struct appletbdrm_device *adev = usb_get_intfdata(intf);
+ struct drm_device *drm = &adev->drm;
+
+ drm_dev_unplug(drm);
+ drm_atomic_helper_shutdown(drm);
+}
+
+static void appletbdrm_shutdown(struct usb_interface *intf)
+{
+ struct appletbdrm_device *adev = usb_get_intfdata(intf);
+
+ /*
+ * The framebuffer needs to be cleared on shutdown since its content
+ * persists across boots
+ */
+ drm_atomic_helper_shutdown(&adev->drm);
+}
+
+static const struct usb_device_id appletbdrm_usb_id_table[] = {
+ { USB_DEVICE_INTERFACE_CLASS(0x05ac, 0x8302, USB_CLASS_AUDIO_VIDEO) },
+ {}
+};
+MODULE_DEVICE_TABLE(usb, appletbdrm_usb_id_table);
+
+static struct usb_driver appletbdrm_usb_driver = {
+ .name = "appletbdrm",
+ .probe = appletbdrm_probe,
+ .disconnect = appletbdrm_disconnect,
+ .shutdown = appletbdrm_shutdown,
+ .id_table = appletbdrm_usb_id_table,
+};
+module_usb_driver(appletbdrm_usb_driver);
+
+MODULE_AUTHOR("Kerem Karabay <kekrby@gmail.com>");
+MODULE_DESCRIPTION("Apple Touch Bar DRM Driver");
+MODULE_LICENSE("GPL");
--
2.42.0
From e34c6d09241ba826a6e9b2b0e50e306b273b7bda Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Thu, 16 Feb 2023 12:32:34 +1100
Subject: [PATCH 5/8] Documentation: leds: standardise keyboard backlight led
names
Advice use of either "input*:*:kbd_backlight" or ":*:kbd_backlight". We
don't want people using vendor or product name (e.g. "smc", "apple",
"asus") as this information is available from sysfs anyway, and it made the
folder names inconsistent.
Signed-off-by: Orlando Chamberlain <orlandoch.dev@gmail.com>
---
Documentation/leds/well-known-leds.txt | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/leds/well-known-leds.txt b/Documentation/leds/well-known-leds.txt
index 2160382c86be..4e5429fce4d8 100644
--- a/Documentation/leds/well-known-leds.txt
+++ b/Documentation/leds/well-known-leds.txt
@@ -44,6 +44,14 @@ Legacy: "lp5523:kb{1,2,3,4,5,6}" (Nokia N900)
Frontlight/backlight of main keyboard.
+Good: ":*:kbd_backlight"
+Good: "input*:*:kbd_backlight"
+Legacy: "*:*:kbd_backlight"
+
+Many drivers have the vendor or product name as the first field of the led name,
+this makes names inconsistent and is redundant as that information is already in
+sysfs.
+
Legacy: "button-backlight" (Motorola Droid 4)
Some phones have touch buttons below screen; it is different from main
--
2.39.1
From c124f5401040d02abd6d349979be29acd1e88545 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Fri, 10 Feb 2023 23:14:31 +1100
Subject: [PATCH 6/8] HID: hid-apple-magic-backlight: Add driver for keyboard
backlight on internal Magic Keyboards
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This driver adds support for the keyboard backlight on Intel T2 Macs
with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)
Co-developed-by: Kerem Karabay <kekrby@gmail.com>
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
Signed-off-by: Orlando Chamberlain <orlandoch.dev@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Thomas Weißschuh <linux@weissschuh.net>
---
MAINTAINERS | 6 ++
drivers/hid/Kconfig | 13 +++
drivers/hid/Makefile | 1 +
drivers/hid/hid-apple-magic-backlight.c | 120 ++++++++++++++++++++++++
4 files changed, 140 insertions(+)
create mode 100644 drivers/hid/hid-apple-magic-backlight.c
diff --git a/MAINTAINERS b/MAINTAINERS
index dfc63d257..9148bda0a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9169,6 +9169,12 @@ L: linux-input@vger.kernel.org
S: Maintained
F: drivers/hid/hid-appletb-*
+HID APPLE MAGIC BACKLIGHT DRIVER
+M: Orlando Chamberlain <orlandoch.dev@gmail.com>
+L: linux-input@vger.kernel.org
+S: Maintained
+F: drivers/hid/apple-magic-backlight.c
+
HID CORE LAYER
M: Jiri Kosina <jikos@kernel.org>
M: Benjamin Tissoires <benjamin.tissoires@redhat.com>
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 4e238df87..83fbab6d4 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -169,6 +169,19 @@ config HID_APPLETB_KBD
To compile this driver as a module, choose M here: the
module will be called hid-appletb-kbd.
+config HID_APPLE_MAGIC_BACKLIGHT
+ tristate "Apple Magic Keyboard Backlight"
+ depends on USB_HID
+ depends on LEDS_CLASS
+ depends on NEW_LEDS
+ help
+ Say Y here if you want support for the keyboard backlight on Macs with
+ the magic keyboard (MacBookPro16,x and MacBookAir9,1). Note that this
+ driver is not for external magic keyboards.
+
+ To compile this driver as a module, choose M here: the
+ module will be called hid-apple-magic-backlight.
+
config HID_ASUS
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 5b60015fd..581f5e720 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_HID_APPLE) += hid-apple.o
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o
obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT) += hid-apple-magic-backlight.o
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
obj-$(CONFIG_HID_ASUS) += hid-asus.o
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
diff --git a/drivers/hid/hid-apple-magic-backlight.c b/drivers/hid/hid-apple-magic-backlight.c
new file mode 100644
index 000000000..f0fc02ff3
--- /dev/null
+++ b/drivers/hid/hid-apple-magic-backlight.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Magic Keyboard Backlight Driver
+ *
+ * For Intel Macs with internal Magic Keyboard (MacBookPro16,1-4 and MacBookAir9,1)
+ *
+ * Copyright (c) 2022 Kerem Karabay <kekrby@gmail.com>
+ * Copyright (c) 2023 Orlando Chamberlain <orlandoch.dev@gmail.com>
+ */
+
+#include <linux/hid.h>
+#include <linux/leds.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <dt-bindings/leds/common.h>
+
+#include "hid-ids.h"
+
+#define HID_USAGE_MAGIC_BL 0xff00000f
+
+#define APPLE_MAGIC_REPORT_ID_POWER 3
+#define APPLE_MAGIC_REPORT_ID_BRIGHTNESS 1
+
+struct apple_magic_backlight {
+ struct led_classdev cdev;
+ struct hid_report *brightness;
+ struct hid_report *power;
+};
+
+static void apple_magic_backlight_report_set(struct hid_report *rep, s32 value, u8 rate)
+{
+ rep->field[0]->value[0] = value;
+ rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
+ rep->field[1]->value[0] |= rate << 8;
+
+ hid_hw_request(rep->device, rep, HID_REQ_SET_REPORT);
+}
+
+static void apple_magic_backlight_set(struct apple_magic_backlight *backlight,
+ int brightness, char rate)
+{
+ apple_magic_backlight_report_set(backlight->power, brightness ? 1 : 0, rate);
+ if (brightness)
+ apple_magic_backlight_report_set(backlight->brightness, brightness, rate);
+}
+
+static int apple_magic_backlight_led_set(struct led_classdev *led_cdev,
+ enum led_brightness brightness)
+{
+ struct apple_magic_backlight *backlight = container_of(led_cdev,
+ struct apple_magic_backlight, cdev);
+
+ apple_magic_backlight_set(backlight, brightness, 1);
+ return 0;
+}
+
+static int apple_magic_backlight_probe(struct hid_device *hdev,
+ const struct hid_device_id *id)
+{
+ struct apple_magic_backlight *backlight;
+ int rc;
+
+ rc = hid_parse(hdev);
+ if (rc)
+ return rc;
+
+ /*
+ * Ensure this usb endpoint is for the keyboard backlight, not touchbar
+ * backlight.
+ */
+ if (hdev->collection[0].usage != HID_USAGE_MAGIC_BL)
+ return -ENODEV;
+
+ backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL);
+ if (!backlight)
+ return -ENOMEM;
+
+ rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+ if (rc)
+ return rc;
+
+ backlight->brightness = hid_register_report(hdev, HID_FEATURE_REPORT,
+ APPLE_MAGIC_REPORT_ID_BRIGHTNESS, 0);
+ backlight->power = hid_register_report(hdev, HID_FEATURE_REPORT,
+ APPLE_MAGIC_REPORT_ID_POWER, 0);
+
+ if (!backlight->brightness || !backlight->power) {
+ rc = -ENODEV;
+ goto hw_stop;
+ }
+
+ backlight->cdev.name = ":white:" LED_FUNCTION_KBD_BACKLIGHT;
+ backlight->cdev.max_brightness = backlight->brightness->field[0]->logical_maximum;
+ backlight->cdev.brightness_set_blocking = apple_magic_backlight_led_set;
+
+ apple_magic_backlight_set(backlight, 0, 0);
+
+ return devm_led_classdev_register(&hdev->dev, &backlight->cdev);
+
+hw_stop:
+ hid_hw_stop(hdev);
+ return rc;
+}
+
+static const struct hid_device_id apple_magic_backlight_hid_ids[] = {
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+ { }
+};
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
+
+static struct hid_driver apple_magic_backlight_hid_driver = {
+ .name = "hid-apple-magic-backlight",
+ .id_table = apple_magic_backlight_hid_ids,
+ .probe = apple_magic_backlight_probe,
+};
+module_hid_driver(apple_magic_backlight_hid_driver);
+
+MODULE_DESCRIPTION("MacBook Magic Keyboard Backlight");
+MODULE_AUTHOR("Orlando Chamberlain <orlandoch.dev@gmail.com>");
+MODULE_LICENSE("GPL");
--
2.39.2
From 12c7a3306a631a651464ef56318a218dc4cdb157 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Sat, 18 Feb 2023 23:05:05 +1100
Subject: [PATCH 8/9] i915: 4 lane quirk for mbp15,1
Needed to use iGPU when dGPU was boot GPU
Patch written by Kerem Karabay <kekrby@gmail.com>
---
drivers/gpu/drm/i915/display/intel_ddi.c | 3 +++
drivers/gpu/drm/i915/display/intel_quirks.c | 15 +++++++++++++++
drivers/gpu/drm/i915/display/intel_quirks.h | 1 +
3 files changed, 19 insertions(+)
diff --git a/drivers/gpu/drm/i915/display/intel_ddi.c b/drivers/gpu/drm/i915/display/intel_ddi.c
index 0f1ec2a98cc8..1ec67390f623 100644
--- a/drivers/gpu/drm/i915/display/intel_ddi.c
+++ b/drivers/gpu/drm/i915/display/intel_ddi.c
@@ -4097,6 +4097,9 @@ static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
if (dig_port->saved_port_bits & DDI_A_4_LANES)
return false;
+ if (intel_has_quirk(dev_priv, QUIRK_DDI_A_FORCE_4_LANES))
+ return true;
+
/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
* supported configuration
*/
diff --git a/drivers/gpu/drm/i915/display/intel_quirks.c b/drivers/gpu/drm/i915/display/intel_quirks.c
index 6e48d3bcdfec..a8c55e165b46 100644
--- a/drivers/gpu/drm/i915/display/intel_quirks.c
+++ b/drivers/gpu/drm/i915/display/intel_quirks.c
@@ -59,6 +59,18 @@ static void quirk_increase_ddi_disabled_time(struct drm_i915_private *i915)
drm_info(&i915->drm, "Applying Increase DDI Disabled quirk\n");
}
+/*
+ * In some cases, the firmware might not set the lane count to 4 (for example,
+ * when booting in some dual GPU Macs with the dGPU as the default GPU), this
+ * quirk is used to force it as otherwise it might not be possible to compute a
+ * valid link configuration.
+ */
+static void quirk_ddi_a_force_4_lanes(struct drm_i915_private *i915)
+{
+ intel_set_quirk(i915, QUIRK_DDI_A_FORCE_4_LANES);
+ drm_info(&i915->drm, "Applying DDI A Forced 4 Lanes quirk\n");
+}
+
static void quirk_no_pps_backlight_power_hook(struct drm_i915_private *i915)
{
intel_set_quirk(i915, QUIRK_NO_PPS_BACKLIGHT_POWER_HOOK);
@@ -199,6 +211,9 @@ static struct intel_quirk intel_quirks[] = {
{ 0x3184, 0x1019, 0xa94d, quirk_increase_ddi_disabled_time },
/* HP Notebook - 14-r206nv */
{ 0x0f31, 0x103c, 0x220f, quirk_invert_brightness },
+
+ /* Apple MacBookPro15,1 */
+ { 0x3e9b, 0x106b, 0x0176, quirk_ddi_a_force_4_lanes },
};
void intel_init_quirks(struct drm_i915_private *i915)
diff --git a/drivers/gpu/drm/i915/display/intel_quirks.h b/drivers/gpu/drm/i915/display/intel_quirks.h
index 10a4d163149f..78aacf1f6f5c 100644
--- a/drivers/gpu/drm/i915/display/intel_quirks.h
+++ b/drivers/gpu/drm/i915/display/intel_quirks.h
@@ -17,6 +17,7 @@ enum intel_quirk_id {
QUIRK_INVERT_BRIGHTNESS,
QUIRK_LVDS_SSC_DISABLE,
QUIRK_NO_PPS_BACKLIGHT_POWER_HOOK,
+ QUIRK_DDI_A_FORCE_4_LANES,
};
void intel_init_quirks(struct drm_i915_private *i915);
--
2.39.1
From bd8e785c74e22978648ced004552eb9c137f1eb6 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Fri, 10 Feb 2023 22:45:00 +1100
Subject: [PATCH 9/9] apple-gmux: allow switching to igpu at probe
This means user don't need to set the gpu-power-prefs efivar to use the
igpu while runtime switching isn't working, so macOS will be unaffected.
This isn't really upstreamable, what we want upstream is the ability to
switch at runtime (so both gpus need to be able to probe the eDP panel).
Based off of work by Kerem Karabay <kekrby@gmail.com>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 +++
drivers/gpu/vga/vga_switcheroo.c | 7 +------
drivers/pci/vgaarb.c | 1 +
drivers/platform/x86/apple-gmux.c | 18 ++++++++++++++++++
4 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
index 81edf66dbea8..8f3daf28665b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -2051,6 +2051,9 @@ static int amdgpu_pci_probe(struct pci_dev *pdev,
int ret, retry = 0, i;
bool supports_atomic = false;
+ if (vga_switcheroo_client_probe_defer(pdev))
+ return -EPROBE_DEFER;
+
/* skip devices which are owned by radeon */
for (i = 0; i < ARRAY_SIZE(amdgpu_unsupported_pciidlist); i++) {
if (amdgpu_unsupported_pciidlist[i] == pdev->device)
diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
index 365e6ddbe90f..cf357cd3389d 100644
--- a/drivers/gpu/vga/vga_switcheroo.c
+++ b/drivers/gpu/vga/vga_switcheroo.c
@@ -438,12 +438,7 @@ find_active_client(struct list_head *head)
bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
{
if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
- /*
- * apple-gmux is needed on pre-retina MacBook Pro
- * to probe the panel if pdev is the inactive GPU.
- */
- if (apple_gmux_present() && pdev != vga_default_device() &&
- !vgasr_priv.handler_flags)
+ if (apple_gmux_present() && !vgasr_priv.handler_flags)
return true;
}
diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c
index 5e6b1eb54c64..1f11701d37d1 100644
--- a/drivers/pci/vgaarb.c
+++ b/drivers/pci/vgaarb.c
@@ -143,6 +143,7 @@ void vga_set_default_device(struct pci_dev *pdev)
pci_dev_put(vga_default);
vga_default = pci_dev_get(pdev);
}
+EXPORT_SYMBOL_GPL(vga_set_default_device);
/**
* vga_remove_vgacon - deactivate VGA console
diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c
index 1417e230edbd..e69785af8e1d 100644
--- a/drivers/platform/x86/apple-gmux.c
+++ b/drivers/platform/x86/apple-gmux.c
@@ -21,6 +21,7 @@
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/vga_switcheroo.h>
+#include <linux/vgaarb.h>
#include <linux/debugfs.h>
#include <acpi/video.h>
#include <asm/io.h>
@@ -107,6 +108,10 @@ struct apple_gmux_config {
# define MMIO_GMUX_MAX_BRIGHTNESS 0xffff
+static bool force_igd;
+module_param(force_igd, bool, 0);
+MODULE_PARM_DESC(force_idg, "Switch gpu to igd on module load. Make sure that you have apple-set-os set up and the iGPU is in `lspci -s 00:02.0`. (default: false) (bool)");
+
static u8 gmux_pio_read8(struct apple_gmux_data *gmux_data, int port)
{
return inb(gmux_data->iostart + port);
@@ -945,6 +950,19 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
gmux_enable_interrupts(gmux_data);
gmux_read_switch_state(gmux_data);
+ if (force_igd) {
+ struct pci_dev *pdev;
+
+ pdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(2, 0));
+ if (pdev) {
+ pr_info("Switching to IGD");
+ gmux_switchto(VGA_SWITCHEROO_IGD);
+ vga_set_default_device(pdev);
+ } else {
+ pr_err("force_idg is true, but couldn't find iGPU at 00:02.0! Is apple-set-os working?");
+ }
+ }
+
/*
* Retina MacBook Pros cannot switch the panel's AUX separately
* and need eDP pre-calibration. They are distinguishable from
--
2.43.0
From 6adb501c697cd0e3246e75237ee8e43eb5a92cc3 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <kekrby@gmail.com>
Date: Thu, 23 Nov 2023 18:58:51 +0530
Subject: [PATCH] efi: libstub: add support for the apple_set_os protocol
On dual GPU EFI Macs, the EFI stub needs to report that it is booting
macOS in order to prevent the firmware from disabling the iGPU.
See also this patch for GRUB by Andreas Heider <andreas@heider.io>:
https://lists.gnu.org/archive/html/grub-devel/2013-12/msg00442.html
---
.../admin-guide/kernel-parameters.txt | 2 ++
.../firmware/efi/libstub/efi-stub-helper.c | 3 +++
drivers/firmware/efi/libstub/efistub.h | 14 ++++++++++
drivers/firmware/efi/libstub/x86-stub.c | 27 +++++++++++++++++++
include/linux/efi.h | 1 +
5 files changed, 47 insertions(+)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 41644336e..cbd4697a5 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -399,6 +399,8 @@
useful so that a dump capture kernel won't be
shot down by NMI
+ apple_set_os [KNL] Report that macOS is being booted to the firmware
+
autoconf= [IPV6]
See Documentation/networking/ipv6.rst.
diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c
index bfa30625f..3d99acc1a 100644
--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -19,6 +19,7 @@
bool efi_nochunk;
bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
bool efi_novamap;
+bool efi_apple_set_os;
static bool efi_noinitrd;
static bool efi_nosoftreserve;
@@ -73,6 +74,8 @@ efi_status_t efi_parse_options(char const *cmdline)
efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
} else if (!strcmp(param, "noinitrd")) {
efi_noinitrd = true;
+ } else if (!strcmp(param, "apple_set_os")) {
+ efi_apple_set_os = true;
} else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
efi_no5lvl = true;
} else if (!strcmp(param, "efi") && val) {
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 212687c30..21b414d09 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -38,6 +38,7 @@ extern bool efi_nochunk;
extern bool efi_nokaslr;
extern int efi_loglevel;
extern bool efi_novamap;
+extern bool efi_apple_set_os;
extern const efi_system_table_t *efi_system_table;
@@ -825,6 +826,19 @@ union apple_properties_protocol {
} mixed_mode;
};
+typedef struct apple_set_os_protocol apple_set_os_protocol_t;
+
+struct apple_set_os_protocol {
+ u64 version;
+ efi_status_t (__efiapi *set_os_version) (const char *);
+ efi_status_t (__efiapi *set_os_vendor) (const char *);
+ struct {
+ u32 version;
+ u32 set_os_version;
+ u32 set_os_vendor;
+ } mixed_mode;
+};
+
typedef u32 efi_tcg2_event_log_format;
#define INITRD_EVENT_TAG_ID 0x8F3B22ECU
diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
index 70b325a2f..2131f8543 100644
--- a/drivers/firmware/efi/libstub/x86-stub.c
+++ b/drivers/firmware/efi/libstub/x86-stub.c
@@ -223,6 +223,30 @@ static void retrieve_apple_device_properties(struct boot_params *boot_params)
}
}
+static void apple_set_os(void)
+{
+ efi_guid_t guid = APPLE_SET_OS_PROTOCOL_GUID;
+ apple_set_os_protocol_t *set_os;
+ efi_status_t status;
+
+ status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&set_os);
+ if (status != EFI_SUCCESS)
+ return;
+
+ if (efi_table_attr(set_os, version) >= 2) {
+ status = efi_fn_call(set_os, set_os_vendor, "Apple Inc.");
+ if (status != EFI_SUCCESS)
+ efi_err("Failed to set OS vendor via apple_set_os\n");
+ }
+
+ /* The version being set doesn't seem to matter */
+ if (efi_table_attr(set_os, version) > 0) {
+ status = efi_fn_call(set_os, set_os_version, "Mac OS X 10.9");
+ if (status != EFI_SUCCESS)
+ efi_err("Failed to set OS version via apple_set_os\n");
+ }
+}
+
efi_status_t efi_adjust_memory_range_protection(unsigned long start,
unsigned long size)
{
@@ -321,6 +345,9 @@ static void setup_quirks(struct boot_params *boot_params)
if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
!memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
retrieve_apple_device_properties(boot_params);
+
+ if (efi_apple_set_os)
+ apple_set_os();
}
/*
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 80b21d1c6..f1e58e027 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -387,6 +387,7 @@ void efi_native_runtime_setup(void);
#define EFI_MEMORY_ATTRIBUTES_TABLE_GUID EFI_GUID(0xdcfa911d, 0x26eb, 0x469f, 0xa2, 0x20, 0x38, 0xb7, 0xdc, 0x46, 0x12, 0x20)
#define EFI_CONSOLE_OUT_DEVICE_GUID EFI_GUID(0xd3b36f2c, 0xd551, 0x11d4, 0x9a, 0x46, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
#define APPLE_PROPERTIES_PROTOCOL_GUID EFI_GUID(0x91bd12fe, 0xf6c3, 0x44fb, 0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0)
+#define APPLE_SET_OS_PROTOCOL_GUID EFI_GUID(0xc5c5da95, 0x7d5c, 0x45e6, 0xb2, 0xf1, 0x3f, 0xd5, 0x2b, 0xb1, 0x00, 0x77)
#define EFI_TCG2_PROTOCOL_GUID EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
#define EFI_LOAD_FILE_PROTOCOL_GUID EFI_GUID(0x56ec3091, 0x954c, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
#define EFI_LOAD_FILE2_PROTOCOL_GUID EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
--
2.34.1
From 09dd6c563cd73d72e917de07e8d59358c41e051d Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <paul@mrarm.io>
Date: Sun, 17 Nov 2019 23:12:55 +0100
Subject: [PATCH 1/6] applesmc: convert static structures to drvdata
All static data structures have been moved to an applesmc_device struct,
which is then associated with the platform device.
This change is intended to ease the migration to an acpi_device, where
static data would preferably be avoided.
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 540 +++++++++++++++++++++++----------------
1 file changed, 319 insertions(+), 221 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 79b498f816fe..62211b590a61 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -6,6 +6,7 @@
*
* Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
* Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
+ * Copyright (C) 2019 Paul Pawlowski <paul@mrarm.io>
*
* Based on hdaps.c driver:
* Copyright (C) 2005 Robert Love <rml@novell.com>
@@ -119,7 +120,7 @@ struct applesmc_entry {
};
/* Register lookup and registers common to all SMCs */
-static struct applesmc_registers {
+struct applesmc_registers {
struct mutex mutex; /* register read/write mutex */
unsigned int key_count; /* number of SMC registers */
unsigned int fan_count; /* number of fans */
@@ -133,26 +134,32 @@ static struct applesmc_registers {
bool init_complete; /* true when fully initialized */
struct applesmc_entry *cache; /* cached key entries */
const char **index; /* temperature key index */
-} smcreg = {
- .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
};
-static const int debug;
-static struct platform_device *pdev;
-static s16 rest_x;
-static s16 rest_y;
-static u8 backlight_state[2];
+struct applesmc_device {
+ struct platform_device *dev;
+ struct applesmc_registers reg;
-static struct device *hwmon_dev;
-static struct input_dev *applesmc_idev;
+ s16 rest_x;
+ s16 rest_y;
-/*
- * Last index written to key_at_index sysfs file, and value to use for all other
- * key_at_index_* sysfs files.
- */
-static unsigned int key_at_index;
+ u8 backlight_state[2];
+
+ struct device *hwmon_dev;
+ struct input_dev *idev;
+
+ /*
+ * Last index written to key_at_index sysfs file, and value to use for all other
+ * key_at_index_* sysfs files.
+ */
+ unsigned int key_at_index;
+
+ struct workqueue_struct *backlight_wq;
+ struct work_struct backlight_work;
+ struct led_classdev backlight_dev;
+};
-static struct workqueue_struct *applesmc_led_wq;
+static const int debug;
/*
* Wait for specific status bits with a mask on the SMC.
@@ -338,36 +345,37 @@ static int read_register_count(unsigned int *count)
* All functions below are concurrency safe - callers should NOT hold lock.
*/
-static int applesmc_read_entry(const struct applesmc_entry *entry,
- u8 *buf, u8 len)
+static int applesmc_read_entry(struct applesmc_device *smc,
+ const struct applesmc_entry *entry, u8 *buf, u8 len)
{
int ret;
if (entry->len != len)
return -EINVAL;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
return ret;
}
-static int applesmc_write_entry(const struct applesmc_entry *entry,
- const u8 *buf, u8 len)
+static int applesmc_write_entry(struct applesmc_device *smc,
+ const struct applesmc_entry *entry, const u8 *buf, u8 len)
{
int ret;
if (entry->len != len)
return -EINVAL;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
return ret;
}
-static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
+static const struct applesmc_entry *applesmc_get_entry_by_index(
+ struct applesmc_device *smc, int index)
{
- struct applesmc_entry *cache = &smcreg.cache[index];
+ struct applesmc_entry *cache = &smc->reg.cache[index];
u8 key[4], info[6];
__be32 be;
int ret = 0;
@@ -375,7 +383,7 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
if (cache->valid)
return cache;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
if (cache->valid)
goto out;
@@ -394,20 +402,21 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
cache->valid = true;
out:
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
if (ret)
return ERR_PTR(ret);
return cache;
}
-static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
+static int applesmc_get_lower_bound(struct applesmc_device *smc,
+ unsigned int *lo, const char *key)
{
- int begin = 0, end = smcreg.key_count;
+ int begin = 0, end = smc->reg.key_count;
const struct applesmc_entry *entry;
while (begin != end) {
int middle = begin + (end - begin) / 2;
- entry = applesmc_get_entry_by_index(middle);
+ entry = applesmc_get_entry_by_index(smc, middle);
if (IS_ERR(entry)) {
*lo = 0;
return PTR_ERR(entry);
@@ -422,16 +431,17 @@ static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
return 0;
}
-static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
+static int applesmc_get_upper_bound(struct applesmc_device *smc,
+ unsigned int *hi, const char *key)
{
- int begin = 0, end = smcreg.key_count;
+ int begin = 0, end = smc->reg.key_count;
const struct applesmc_entry *entry;
while (begin != end) {
int middle = begin + (end - begin) / 2;
- entry = applesmc_get_entry_by_index(middle);
+ entry = applesmc_get_entry_by_index(smc, middle);
if (IS_ERR(entry)) {
- *hi = smcreg.key_count;
+ *hi = smc->reg.key_count;
return PTR_ERR(entry);
}
if (strcmp(key, entry->key) < 0)
@@ -444,50 +454,54 @@ static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
return 0;
}
-static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
+static const struct applesmc_entry *applesmc_get_entry_by_key(
+ struct applesmc_device *smc, const char *key)
{
int begin, end;
int ret;
- ret = applesmc_get_lower_bound(&begin, key);
+ ret = applesmc_get_lower_bound(smc, &begin, key);
if (ret)
return ERR_PTR(ret);
- ret = applesmc_get_upper_bound(&end, key);
+ ret = applesmc_get_upper_bound(smc, &end, key);
if (ret)
return ERR_PTR(ret);
if (end - begin != 1)
return ERR_PTR(-EINVAL);
- return applesmc_get_entry_by_index(begin);
+ return applesmc_get_entry_by_index(smc, begin);
}
-static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
+static int applesmc_read_key(struct applesmc_device *smc,
+ const char *key, u8 *buffer, u8 len)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry))
return PTR_ERR(entry);
- return applesmc_read_entry(entry, buffer, len);
+ return applesmc_read_entry(smc, entry, buffer, len);
}
-static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
+static int applesmc_write_key(struct applesmc_device *smc,
+ const char *key, const u8 *buffer, u8 len)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry))
return PTR_ERR(entry);
- return applesmc_write_entry(entry, buffer, len);
+ return applesmc_write_entry(smc, entry, buffer, len);
}
-static int applesmc_has_key(const char *key, bool *value)
+static int applesmc_has_key(struct applesmc_device *smc,
+ const char *key, bool *value)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
return PTR_ERR(entry);
@@ -498,12 +512,13 @@ static int applesmc_has_key(const char *key, bool *value)
/*
* applesmc_read_s16 - Read 16-bit signed big endian register
*/
-static int applesmc_read_s16(const char *key, s16 *value)
+static int applesmc_read_s16(struct applesmc_device *smc,
+ const char *key, s16 *value)
{
u8 buffer[2];
int ret;
- ret = applesmc_read_key(key, buffer, 2);
+ ret = applesmc_read_key(smc, key, buffer, 2);
if (ret)
return ret;
@@ -514,28 +529,29 @@ static int applesmc_read_s16(const char *key, s16 *value)
/*
* applesmc_device_init - initialize the accelerometer. Can sleep.
*/
-static void applesmc_device_init(void)
+static void applesmc_device_init(struct applesmc_device *smc)
{
int total;
u8 buffer[2];
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return;
for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
- if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
+ if (!applesmc_read_key(smc, MOTION_SENSOR_KEY, buffer, 2) &&
(buffer[0] != 0x00 || buffer[1] != 0x00))
return;
buffer[0] = 0xe0;
buffer[1] = 0x00;
- applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
+ applesmc_write_key(smc, MOTION_SENSOR_KEY, buffer, 2);
msleep(INIT_WAIT_MSECS);
}
pr_warn("failed to init the device\n");
}
-static int applesmc_init_index(struct applesmc_registers *s)
+static int applesmc_init_index(struct applesmc_device *smc,
+ struct applesmc_registers *s)
{
const struct applesmc_entry *entry;
unsigned int i;
@@ -548,7 +564,7 @@ static int applesmc_init_index(struct applesmc_registers *s)
return -ENOMEM;
for (i = s->temp_begin; i < s->temp_end; i++) {
- entry = applesmc_get_entry_by_index(i);
+ entry = applesmc_get_entry_by_index(smc, i);
if (IS_ERR(entry))
continue;
if (strcmp(entry->type, TEMP_SENSOR_TYPE))
@@ -562,9 +578,9 @@ static int applesmc_init_index(struct applesmc_registers *s)
/*
* applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
*/
-static int applesmc_init_smcreg_try(void)
+static int applesmc_init_smcreg_try(struct applesmc_device *smc)
{
- struct applesmc_registers *s = &smcreg;
+ struct applesmc_registers *s = &smc->reg;
bool left_light_sensor = false, right_light_sensor = false;
unsigned int count;
u8 tmp[1];
@@ -590,35 +606,35 @@ static int applesmc_init_smcreg_try(void)
if (!s->cache)
return -ENOMEM;
- ret = applesmc_read_key(FANS_COUNT, tmp, 1);
+ ret = applesmc_read_key(smc, FANS_COUNT, tmp, 1);
if (ret)
return ret;
s->fan_count = tmp[0];
if (s->fan_count > 10)
s->fan_count = 10;
- ret = applesmc_get_lower_bound(&s->temp_begin, "T");
+ ret = applesmc_get_lower_bound(smc, &s->temp_begin, "T");
if (ret)
return ret;
- ret = applesmc_get_lower_bound(&s->temp_end, "U");
+ ret = applesmc_get_lower_bound(smc, &s->temp_end, "U");
if (ret)
return ret;
s->temp_count = s->temp_end - s->temp_begin;
- ret = applesmc_init_index(s);
+ ret = applesmc_init_index(smc, s);
if (ret)
return ret;
- ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
+ ret = applesmc_has_key(smc, LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
if (ret)
return ret;
- ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
+ ret = applesmc_has_key(smc, LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
if (ret)
return ret;
- ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
+ ret = applesmc_has_key(smc, MOTION_SENSOR_KEY, &s->has_accelerometer);
if (ret)
return ret;
- ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
+ ret = applesmc_has_key(smc, BACKLIGHT_KEY, &s->has_key_backlight);
if (ret)
return ret;
@@ -634,13 +650,13 @@ static int applesmc_init_smcreg_try(void)
return 0;
}
-static void applesmc_destroy_smcreg(void)
+static void applesmc_destroy_smcreg(struct applesmc_device *smc)
{
- kfree(smcreg.index);
- smcreg.index = NULL;
- kfree(smcreg.cache);
- smcreg.cache = NULL;
- smcreg.init_complete = false;
+ kfree(smc->reg.index);
+ smc->reg.index = NULL;
+ kfree(smc->reg.cache);
+ smc->reg.cache = NULL;
+ smc->reg.init_complete = false;
}
/*
@@ -649,12 +665,12 @@ static void applesmc_destroy_smcreg(void)
* Retries until initialization is successful, or the operation times out.
*
*/
-static int applesmc_init_smcreg(void)
+static int applesmc_init_smcreg(struct applesmc_device *smc)
{
int ms, ret;
for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
- ret = applesmc_init_smcreg_try();
+ ret = applesmc_init_smcreg_try(smc);
if (!ret) {
if (ms)
pr_info("init_smcreg() took %d ms\n", ms);
@@ -663,21 +679,58 @@ static int applesmc_init_smcreg(void)
msleep(INIT_WAIT_MSECS);
}
- applesmc_destroy_smcreg();
+ applesmc_destroy_smcreg(smc);
return ret;
}
/* Device model stuff */
+static int applesmc_create_modules(struct applesmc_device *smc);
+static void applesmc_destroy_modules(struct applesmc_device *smc);
static int applesmc_probe(struct platform_device *dev)
{
+ struct applesmc_device *smc;
int ret;
- ret = applesmc_init_smcreg();
+ smc = kzalloc(sizeof(struct applesmc_device), GFP_KERNEL);
+ if (!smc)
+ return -ENOMEM;
+ smc->dev = dev;
+ mutex_init(&smc->reg.mutex);
+
+ platform_set_drvdata(dev, smc);
+
+ ret = applesmc_init_smcreg(smc);
if (ret)
- return ret;
+ goto out_mem;
+
+ applesmc_device_init(smc);
+
+ ret = applesmc_create_modules(smc);
+ if (ret)
+ goto out_reg;
+
+ return 0;
+
+out_reg:
+ applesmc_destroy_smcreg(smc);
+out_mem:
+ platform_set_drvdata(dev, NULL);
+ mutex_destroy(&smc->reg.mutex);
+ kfree(smc);
- applesmc_device_init();
+ return ret;
+}
+
+static int applesmc_remove(struct platform_device *dev)
+{
+ struct applesmc_device *smc = platform_get_drvdata(dev);
+
+ applesmc_destroy_modules(smc);
+ applesmc_destroy_smcreg(smc);
+
+ mutex_destroy(&smc->reg.mutex);
+ kfree(smc);
return 0;
}
@@ -685,15 +738,21 @@ static int applesmc_probe(struct platform_device *dev)
/* Synchronize device with memorized backlight state */
static int applesmc_pm_resume(struct device *dev)
{
- if (smcreg.has_key_backlight)
- applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ if (smc->reg.has_key_backlight)
+ applesmc_write_key(smc, BACKLIGHT_KEY, smc->backlight_state, 2);
+
return 0;
}
/* Reinitialize device on resume from hibernation */
static int applesmc_pm_restore(struct device *dev)
{
- applesmc_device_init();
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ applesmc_device_init(smc);
+
return applesmc_pm_resume(dev);
}
@@ -704,6 +763,7 @@ static const struct dev_pm_ops applesmc_pm_ops = {
static struct platform_driver applesmc_driver = {
.probe = applesmc_probe,
+ .remove = applesmc_remove,
.driver = {
.name = "applesmc",
.pm = &applesmc_pm_ops,
@@ -714,25 +774,26 @@ static struct platform_driver applesmc_driver = {
* applesmc_calibrate - Set our "resting" values. Callers must
* hold applesmc_lock.
*/
-static void applesmc_calibrate(void)
+static void applesmc_calibrate(struct applesmc_device *smc)
{
- applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
- applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
- rest_x = -rest_x;
+ applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &smc->rest_x);
+ applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &smc->rest_y);
+ smc->rest_x = -smc->rest_x;
}
static void applesmc_idev_poll(struct input_dev *idev)
{
+ struct applesmc_device *smc = dev_get_drvdata(&idev->dev);
s16 x, y;
- if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
+ if (applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &x))
return;
- if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
+ if (applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &y))
return;
x = -x;
- input_report_abs(idev, ABS_X, x - rest_x);
- input_report_abs(idev, ABS_Y, y - rest_y);
+ input_report_abs(idev, ABS_X, x - smc->rest_x);
+ input_report_abs(idev, ABS_Y, y - smc->rest_y);
input_sync(idev);
}
@@ -747,16 +808,17 @@ static ssize_t applesmc_name_show(struct device *dev,
static ssize_t applesmc_position_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
s16 x, y, z;
- ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &x);
if (ret)
goto out;
- ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &y);
if (ret)
goto out;
- ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_Z_KEY, &z);
if (ret)
goto out;
@@ -770,6 +832,7 @@ static ssize_t applesmc_position_show(struct device *dev,
static ssize_t applesmc_light_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
static int data_length;
int ret;
@@ -777,7 +840,7 @@ static ssize_t applesmc_light_show(struct device *dev,
u8 buffer[10];
if (!data_length) {
- entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
+ entry = applesmc_get_entry_by_key(smc, LIGHT_SENSOR_LEFT_KEY);
if (IS_ERR(entry))
return PTR_ERR(entry);
if (entry->len > 10)
@@ -786,7 +849,7 @@ static ssize_t applesmc_light_show(struct device *dev,
pr_info("light sensor data length set to %d\n", data_length);
}
- ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
+ ret = applesmc_read_key(smc, LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
if (ret)
goto out;
/* newer macbooks report a single 10-bit bigendian value */
@@ -796,7 +859,7 @@ static ssize_t applesmc_light_show(struct device *dev,
}
left = buffer[2];
- ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
+ ret = applesmc_read_key(smc, LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
if (ret)
goto out;
right = buffer[2];
@@ -812,7 +875,8 @@ static ssize_t applesmc_light_show(struct device *dev,
static ssize_t applesmc_show_sensor_label(struct device *dev,
struct device_attribute *devattr, char *sysfsbuf)
{
- const char *key = smcreg.index[to_index(devattr)];
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ const char *key = smc->reg.index[to_index(devattr)];
return sysfs_emit(sysfsbuf, "%s\n", key);
}
@@ -821,12 +885,13 @@ static ssize_t applesmc_show_sensor_label(struct device *dev,
static ssize_t applesmc_show_temperature(struct device *dev,
struct device_attribute *devattr, char *sysfsbuf)
{
- const char *key = smcreg.index[to_index(devattr)];
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ const char *key = smc->reg.index[to_index(devattr)];
int ret;
s16 value;
int temp;
- ret = applesmc_read_s16(key, &value);
+ ret = applesmc_read_s16(smc, key, &value);
if (ret)
return ret;
@@ -838,6 +903,7 @@ static ssize_t applesmc_show_temperature(struct device *dev,
static ssize_t applesmc_show_fan_speed(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
unsigned int speed = 0;
char newkey[5];
@@ -846,7 +912,7 @@ static ssize_t applesmc_show_fan_speed(struct device *dev,
scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
to_index(attr));
- ret = applesmc_read_key(newkey, buffer, 2);
+ ret = applesmc_read_key(smc, newkey, buffer, 2);
if (ret)
return ret;
@@ -858,6 +924,7 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
struct device_attribute *attr,
const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
unsigned long speed;
char newkey[5];
@@ -871,7 +938,7 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
buffer[0] = (speed >> 6) & 0xff;
buffer[1] = (speed << 2) & 0xff;
- ret = applesmc_write_key(newkey, buffer, 2);
+ ret = applesmc_write_key(smc, newkey, buffer, 2);
if (ret)
return ret;
@@ -882,11 +949,12 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
static ssize_t applesmc_show_fan_manual(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u16 manual = 0;
u8 buffer[2];
- ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
if (ret)
return ret;
@@ -898,6 +966,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
struct device_attribute *attr,
const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u8 buffer[2];
unsigned long input;
@@ -906,7 +975,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
if (kstrtoul(sysfsbuf, 10, &input) < 0)
return -EINVAL;
- ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
if (ret)
goto out;
@@ -920,7 +989,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
buffer[0] = (val >> 8) & 0xFF;
buffer[1] = val & 0xFF;
- ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_write_key(smc, FANS_MANUAL, buffer, 2);
out:
if (ret)
@@ -932,13 +1001,14 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
static ssize_t applesmc_show_fan_position(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
char newkey[5];
u8 buffer[17];
scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, to_index(attr));
- ret = applesmc_read_key(newkey, buffer, 16);
+ ret = applesmc_read_key(smc, newkey, buffer, 16);
buffer[16] = 0;
if (ret)
@@ -950,30 +1020,36 @@ static ssize_t applesmc_show_fan_position(struct device *dev,
static ssize_t applesmc_calibrate_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
- return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ return sysfs_emit(sysfsbuf, "(%d,%d)\n", smc->rest_x, smc->rest_y);
}
static ssize_t applesmc_calibrate_store(struct device *dev,
struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
- applesmc_calibrate();
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ applesmc_calibrate(smc);
return count;
}
static void applesmc_backlight_set(struct work_struct *work)
{
- applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
+ struct applesmc_device *smc = container_of(work, struct applesmc_device, backlight_work);
+
+ applesmc_write_key(smc, BACKLIGHT_KEY, smc->backlight_state, 2);
}
-static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
static void applesmc_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
+ struct applesmc_device *smc = dev_get_drvdata(led_cdev->dev);
int ret;
- backlight_state[0] = value;
- ret = queue_work(applesmc_led_wq, &backlight_work);
+ smc->backlight_state[0] = value;
+ ret = queue_work(smc->backlight_wq, &smc->backlight_work);
if (debug && (!ret))
dev_dbg(led_cdev->dev, "work was already on the queue.\n");
@@ -982,11 +1058,12 @@ static void applesmc_brightness_set(struct led_classdev *led_cdev,
static ssize_t applesmc_key_count_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u8 buffer[4];
u32 count;
- ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
+ ret = applesmc_read_key(smc, KEY_COUNT_KEY, buffer, 4);
if (ret)
return ret;
@@ -998,13 +1075,14 @@ static ssize_t applesmc_key_count_show(struct device *dev,
static ssize_t applesmc_key_at_index_read_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
int ret;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
- ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
+ ret = applesmc_read_entry(smc, entry, sysfsbuf, entry->len);
if (ret)
return ret;
@@ -1014,9 +1092,10 @@ static ssize_t applesmc_key_at_index_read_show(struct device *dev,
static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1026,9 +1105,10 @@ static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
static ssize_t applesmc_key_at_index_type_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1038,9 +1118,10 @@ static ssize_t applesmc_key_at_index_type_show(struct device *dev,
static ssize_t applesmc_key_at_index_name_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1050,28 +1131,25 @@ static ssize_t applesmc_key_at_index_name_show(struct device *dev,
static ssize_t applesmc_key_at_index_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
- return sysfs_emit(sysfsbuf, "%d\n", key_at_index);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ return sysfs_emit(sysfsbuf, "%d\n", smc->key_at_index);
}
static ssize_t applesmc_key_at_index_store(struct device *dev,
struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
unsigned long newkey;
if (kstrtoul(sysfsbuf, 10, &newkey) < 0
- || newkey >= smcreg.key_count)
+ || newkey >= smc->reg.key_count)
return -EINVAL;
- key_at_index = newkey;
+ smc->key_at_index = newkey;
return count;
}
-static struct led_classdev applesmc_backlight = {
- .name = "smc::kbd_backlight",
- .default_trigger = "nand-disk",
- .brightness_set = applesmc_brightness_set,
-};
-
static struct applesmc_node_group info_group[] = {
{ "name", applesmc_name_show },
{ "key_count", applesmc_key_count_show },
@@ -1116,14 +1194,15 @@ static struct applesmc_node_group temp_group[] = {
/*
* applesmc_destroy_nodes - remove files and free associated memory
*/
-static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
+static void applesmc_destroy_nodes(struct applesmc_device *smc,
+ struct applesmc_node_group *groups)
{
struct applesmc_node_group *grp;
struct applesmc_dev_attr *node;
for (grp = groups; grp->nodes; grp++) {
for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
- sysfs_remove_file(&pdev->dev.kobj,
+ sysfs_remove_file(&smc->dev->dev.kobj,
&node->sda.dev_attr.attr);
kfree(grp->nodes);
grp->nodes = NULL;
@@ -1133,7 +1212,8 @@ static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
/*
* applesmc_create_nodes - create a two-dimensional group of sysfs files
*/
-static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
+static int applesmc_create_nodes(struct applesmc_device *smc,
+ struct applesmc_node_group *groups, int num)
{
struct applesmc_node_group *grp;
struct applesmc_dev_attr *node;
@@ -1157,7 +1237,7 @@ static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
sysfs_attr_init(attr);
attr->name = node->name;
attr->mode = 0444 | (grp->store ? 0200 : 0);
- ret = sysfs_create_file(&pdev->dev.kobj, attr);
+ ret = sysfs_create_file(&smc->dev->dev.kobj, attr);
if (ret) {
attr->name = NULL;
goto out;
@@ -1167,57 +1247,57 @@ static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
return 0;
out:
- applesmc_destroy_nodes(groups);
+ applesmc_destroy_nodes(smc, groups);
return ret;
}
/* Create accelerometer resources */
-static int applesmc_create_accelerometer(void)
+static int applesmc_create_accelerometer(struct applesmc_device *smc)
{
int ret;
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return 0;
- ret = applesmc_create_nodes(accelerometer_group, 1);
+ ret = applesmc_create_nodes(smc, accelerometer_group, 1);
if (ret)
goto out;
- applesmc_idev = input_allocate_device();
- if (!applesmc_idev) {
+ smc->idev = input_allocate_device();
+ if (!smc->idev) {
ret = -ENOMEM;
goto out_sysfs;
}
/* initial calibrate for the input device */
- applesmc_calibrate();
+ applesmc_calibrate(smc);
/* initialize the input device */
- applesmc_idev->name = "applesmc";
- applesmc_idev->id.bustype = BUS_HOST;
- applesmc_idev->dev.parent = &pdev->dev;
- input_set_abs_params(applesmc_idev, ABS_X,
+ smc->idev->name = "applesmc";
+ smc->idev->id.bustype = BUS_HOST;
+ smc->idev->dev.parent = &smc->dev->dev;
+ input_set_abs_params(smc->idev, ABS_X,
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
- input_set_abs_params(applesmc_idev, ABS_Y,
+ input_set_abs_params(smc->idev, ABS_Y,
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
- ret = input_setup_polling(applesmc_idev, applesmc_idev_poll);
+ ret = input_setup_polling(smc->idev, applesmc_idev_poll);
if (ret)
goto out_idev;
- input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL);
+ input_set_poll_interval(smc->idev, APPLESMC_POLL_INTERVAL);
- ret = input_register_device(applesmc_idev);
+ ret = input_register_device(smc->idev);
if (ret)
goto out_idev;
return 0;
out_idev:
- input_free_device(applesmc_idev);
+ input_free_device(smc->idev);
out_sysfs:
- applesmc_destroy_nodes(accelerometer_group);
+ applesmc_destroy_nodes(smc, accelerometer_group);
out:
pr_warn("driver init failed (ret=%d)!\n", ret);
@@ -1225,44 +1305,55 @@ static int applesmc_create_accelerometer(void)
}
/* Release all resources used by the accelerometer */
-static void applesmc_release_accelerometer(void)
+static void applesmc_release_accelerometer(struct applesmc_device *smc)
{
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return;
- input_unregister_device(applesmc_idev);
- applesmc_destroy_nodes(accelerometer_group);
+ input_unregister_device(smc->idev);
+ applesmc_destroy_nodes(smc, accelerometer_group);
}
-static int applesmc_create_light_sensor(void)
+static int applesmc_create_light_sensor(struct applesmc_device *smc)
{
- if (!smcreg.num_light_sensors)
+ if (!smc->reg.num_light_sensors)
return 0;
- return applesmc_create_nodes(light_sensor_group, 1);
+ return applesmc_create_nodes(smc, light_sensor_group, 1);
}
-static void applesmc_release_light_sensor(void)
+static void applesmc_release_light_sensor(struct applesmc_device *smc)
{
- if (!smcreg.num_light_sensors)
+ if (!smc->reg.num_light_sensors)
return;
- applesmc_destroy_nodes(light_sensor_group);
+ applesmc_destroy_nodes(smc, light_sensor_group);
}
-static int applesmc_create_key_backlight(void)
+static int applesmc_create_key_backlight(struct applesmc_device *smc)
{
- if (!smcreg.has_key_backlight)
+ int ret;
+
+ if (!smc->reg.has_key_backlight)
return 0;
- applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
- if (!applesmc_led_wq)
+ smc->backlight_wq = create_singlethread_workqueue("applesmc-led");
+ if (!smc->backlight_wq)
return -ENOMEM;
- return led_classdev_register(&pdev->dev, &applesmc_backlight);
+
+ INIT_WORK(&smc->backlight_work, applesmc_backlight_set);
+ smc->backlight_dev.name = "smc::kbd_backlight";
+ smc->backlight_dev.default_trigger = "nand-disk";
+ smc->backlight_dev.brightness_set = applesmc_brightness_set;
+ ret = led_classdev_register(&smc->dev->dev, &smc->backlight_dev);
+ if (ret)
+ destroy_workqueue(smc->backlight_wq);
+
+ return ret;
}
-static void applesmc_release_key_backlight(void)
+static void applesmc_release_key_backlight(struct applesmc_device *smc)
{
- if (!smcreg.has_key_backlight)
+ if (!smc->reg.has_key_backlight)
return;
- led_classdev_unregister(&applesmc_backlight);
- destroy_workqueue(applesmc_led_wq);
+ led_classdev_unregister(&smc->backlight_dev);
+ destroy_workqueue(smc->backlight_wq);
}
static int applesmc_dmi_match(const struct dmi_system_id *id)
@@ -1302,86 +1393,100 @@ static const struct dmi_system_id applesmc_whitelist[] __initconst = {
{ .ident = NULL }
};
-static int __init applesmc_init(void)
+static int applesmc_create_modules(struct applesmc_device *smc)
{
int ret;
- if (!dmi_check_system(applesmc_whitelist)) {
- pr_warn("supported laptop not found!\n");
- ret = -ENODEV;
- goto out;
- }
-
- if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
- "applesmc")) {
- ret = -ENXIO;
- goto out;
- }
-
- ret = platform_driver_register(&applesmc_driver);
- if (ret)
- goto out_region;
-
- pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
- NULL, 0);
- if (IS_ERR(pdev)) {
- ret = PTR_ERR(pdev);
- goto out_driver;
- }
-
- /* create register cache */
- ret = applesmc_init_smcreg();
- if (ret)
- goto out_device;
-
- ret = applesmc_create_nodes(info_group, 1);
+ ret = applesmc_create_nodes(smc, info_group, 1);
if (ret)
- goto out_smcreg;
+ goto out;
- ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
+ ret = applesmc_create_nodes(smc, fan_group, smc->reg.fan_count);
if (ret)
goto out_info;
- ret = applesmc_create_nodes(temp_group, smcreg.index_count);
+ ret = applesmc_create_nodes(smc, temp_group, smc->reg.index_count);
if (ret)
goto out_fans;
- ret = applesmc_create_accelerometer();
+ ret = applesmc_create_accelerometer(smc);
if (ret)
goto out_temperature;
- ret = applesmc_create_light_sensor();
+ ret = applesmc_create_light_sensor(smc);
if (ret)
goto out_accelerometer;
- ret = applesmc_create_key_backlight();
+ ret = applesmc_create_key_backlight(smc);
if (ret)
goto out_light_sysfs;
- hwmon_dev = hwmon_device_register(&pdev->dev);
- if (IS_ERR(hwmon_dev)) {
- ret = PTR_ERR(hwmon_dev);
+ smc->hwmon_dev = hwmon_device_register(&smc->dev->dev);
+ if (IS_ERR(smc->hwmon_dev)) {
+ ret = PTR_ERR(smc->hwmon_dev);
goto out_light_ledclass;
}
return 0;
out_light_ledclass:
- applesmc_release_key_backlight();
+ applesmc_release_key_backlight(smc);
out_light_sysfs:
- applesmc_release_light_sensor();
+ applesmc_release_light_sensor(smc);
out_accelerometer:
- applesmc_release_accelerometer();
+ applesmc_release_accelerometer(smc);
out_temperature:
- applesmc_destroy_nodes(temp_group);
+ applesmc_destroy_nodes(smc, temp_group);
out_fans:
- applesmc_destroy_nodes(fan_group);
+ applesmc_destroy_nodes(smc, fan_group);
out_info:
- applesmc_destroy_nodes(info_group);
-out_smcreg:
- applesmc_destroy_smcreg();
-out_device:
- platform_device_unregister(pdev);
+ applesmc_destroy_nodes(smc, info_group);
+out:
+ return ret;
+}
+
+static void applesmc_destroy_modules(struct applesmc_device *smc)
+{
+ hwmon_device_unregister(smc->hwmon_dev);
+ applesmc_release_key_backlight(smc);
+ applesmc_release_light_sensor(smc);
+ applesmc_release_accelerometer(smc);
+ applesmc_destroy_nodes(smc, temp_group);
+ applesmc_destroy_nodes(smc, fan_group);
+ applesmc_destroy_nodes(smc, info_group);
+}
+
+static struct platform_device *pdev;
+
+static int __init applesmc_init(void)
+{
+ int ret;
+
+ if (!dmi_check_system(applesmc_whitelist)) {
+ pr_warn("supported laptop not found!\n");
+ ret = -ENODEV;
+ goto out;
+ }
+
+ if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
+ "applesmc")) {
+ ret = -ENXIO;
+ goto out;
+ }
+
+ ret = platform_driver_register(&applesmc_driver);
+ if (ret)
+ goto out_region;
+
+ pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
+ NULL, 0);
+ if (IS_ERR(pdev)) {
+ ret = PTR_ERR(pdev);
+ goto out_driver;
+ }
+
+ return 0;
+
out_driver:
platform_driver_unregister(&applesmc_driver);
out_region:
@@ -1393,14 +1498,6 @@ static int __init applesmc_init(void)
static void __exit applesmc_exit(void)
{
- hwmon_device_unregister(hwmon_dev);
- applesmc_release_key_backlight();
- applesmc_release_light_sensor();
- applesmc_release_accelerometer();
- applesmc_destroy_nodes(temp_group);
- applesmc_destroy_nodes(fan_group);
- applesmc_destroy_nodes(info_group);
- applesmc_destroy_smcreg();
platform_device_unregister(pdev);
platform_driver_unregister(&applesmc_driver);
release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
@@ -1410,6 +1507,7 @@ module_init(applesmc_init);
module_exit(applesmc_exit);
MODULE_AUTHOR("Nicolas Boichat");
+MODULE_AUTHOR("Paul Pawlowski");
MODULE_DESCRIPTION("Apple SMC");
MODULE_LICENSE("GPL v2");
MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);
--
2.30.0
From 713e78b8dbb8adb92d4ee09ea11e726b05577689 Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <paul@mrarm.io>
Date: Sun, 17 Nov 2019 23:11:56 +0100
Subject: [PATCH 2/6] applesmc: make io port base addr dynamic
This change makes the port base runtime configurable.
The reason why this change is made is so that when we switch to an
acpi_device we can resolve the port base addr from ACPI.
This change is not strictly required for T2 support - the base
address is still 0x300 on T2 Macs.
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 91 +++++++++++++++++++++-------------------
1 file changed, 49 insertions(+), 42 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 62211b590a61..39ed0bb21365 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -35,10 +35,11 @@
#include <linux/err.h>
#include <linux/bits.h>
+#define APPLESMC_PORT_BASE 0x300
/* data port used by Apple SMC */
-#define APPLESMC_DATA_PORT 0x300
+#define APPLESMC_DATA_PORT 0
/* command/status port used by Apple SMC */
-#define APPLESMC_CMD_PORT 0x304
+#define APPLESMC_CMD_PORT 4
#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
@@ -140,6 +141,8 @@ struct applesmc_device {
struct platform_device *dev;
struct applesmc_registers reg;
+ u16 port_base;
+
s16 rest_x;
s16 rest_y;
@@ -169,7 +172,7 @@ static const int debug;
* run out past 500ms.
*/
-static int wait_status(u8 val, u8 mask)
+static int wait_status(struct applesmc_device *smc, u8 val, u8 mask)
{
u8 status;
int us;
@@ -177,7 +180,7 @@ static int wait_status(u8 val, u8 mask)
us = APPLESMC_MIN_WAIT;
for (i = 0; i < 24 ; i++) {
- status = inb(APPLESMC_CMD_PORT);
+ status = inb(smc->port_base + APPLESMC_CMD_PORT);
if ((status & mask) == val)
return 0;
usleep_range(us, us * 2);
@@ -189,11 +192,11 @@ static int wait_status(u8 val, u8 mask)
/* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
-static int send_byte(u8 cmd, u16 port)
+static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
{
int status;
- status = wait_status(0, SMC_STATUS_IB_CLOSED);
+ status = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (status)
return status;
/*
@@ -202,24 +205,24 @@ static int send_byte(u8 cmd, u16 port)
* this extra read may not happen if status returns both
* simultaneously and this would appear to be required.
*/
- status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
+ status = wait_status(smc, SMC_STATUS_BUSY, SMC_STATUS_BUSY);
if (status)
return status;
- outb(cmd, port);
+ outb(cmd, smc->port_base + port);
return 0;
}
/* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
-static int send_command(u8 cmd)
+static int send_command(struct applesmc_device *smc, u8 cmd)
{
int ret;
- ret = wait_status(0, SMC_STATUS_IB_CLOSED);
+ ret = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (ret)
return ret;
- outb(cmd, APPLESMC_CMD_PORT);
+ outb(cmd, smc->port_base + APPLESMC_CMD_PORT);
return 0;
}
@@ -229,108 +232,112 @@ static int send_command(u8 cmd)
* If busy is stuck high after the command then the SMC is jammed.
*/
-static int smc_sane(void)
+static int smc_sane(struct applesmc_device *smc)
{
int ret;
- ret = wait_status(0, SMC_STATUS_BUSY);
+ ret = wait_status(smc, 0, SMC_STATUS_BUSY);
if (!ret)
return ret;
- ret = send_command(APPLESMC_READ_CMD);
+ ret = send_command(smc, APPLESMC_READ_CMD);
if (ret)
return ret;
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int send_argument(const char *key)
+static int send_argument(struct applesmc_device *smc, const char *key)
{
int i;
for (i = 0; i < 4; i++)
- if (send_byte(key[i], APPLESMC_DATA_PORT))
+ if (send_byte(smc, key[i], APPLESMC_DATA_PORT))
return -EIO;
return 0;
}
-static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
+static int read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ u8 *buffer, u8 len)
{
u8 status, data = 0;
int i;
int ret;
- ret = smc_sane();
+ ret = smc_sane(smc);
if (ret)
return ret;
- if (send_command(cmd) || send_argument(key)) {
+ if (send_command(smc, cmd) || send_argument(smc, key)) {
pr_warn("%.4s: read arg fail\n", key);
return -EIO;
}
/* This has no effect on newer (2012) SMCs */
- if (send_byte(len, APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: read len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
+ if (wait_status(smc,
+ SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
pr_warn("%.4s: read data[%d] fail\n", key, i);
return -EIO;
}
- buffer[i] = inb(APPLESMC_DATA_PORT);
+ buffer[i] = inb(smc->port_base + APPLESMC_DATA_PORT);
}
/* Read the data port until bit0 is cleared */
for (i = 0; i < 16; i++) {
udelay(APPLESMC_MIN_WAIT);
- status = inb(APPLESMC_CMD_PORT);
+ status = inb(smc->port_base + APPLESMC_CMD_PORT);
if (!(status & SMC_STATUS_AWAITING_DATA))
break;
- data = inb(APPLESMC_DATA_PORT);
+ data = inb(smc->port_base + APPLESMC_DATA_PORT);
}
if (i)
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
+static int write_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ const u8 *buffer, u8 len)
{
int i;
int ret;
- ret = smc_sane();
+ ret = smc_sane(smc);
if (ret)
return ret;
- if (send_command(cmd) || send_argument(key)) {
+ if (send_command(smc, cmd) || send_argument(smc, key)) {
pr_warn("%s: write arg fail\n", key);
return -EIO;
}
- if (send_byte(len, APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: write len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
+ if (send_byte(smc, buffer[i], APPLESMC_DATA_PORT)) {
pr_warn("%s: write data fail\n", key);
return -EIO;
}
}
- return wait_status(0, SMC_STATUS_BUSY);
+ return wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int read_register_count(unsigned int *count)
+static int read_register_count(struct applesmc_device *smc,
+ unsigned int *count)
{
__be32 be;
int ret;
- ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
+ ret = read_smc(smc, APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
if (ret)
return ret;
@@ -353,7 +360,7 @@ static int applesmc_read_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
+ ret = read_smc(smc, APPLESMC_READ_CMD, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
@@ -367,7 +374,7 @@ static int applesmc_write_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
+ ret = write_smc(smc, APPLESMC_WRITE_CMD, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
}
@@ -388,10 +395,10 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
if (cache->valid)
goto out;
be = cpu_to_be32(index);
- ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
+ ret = read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
if (ret)
goto out;
- ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
+ ret = read_smc(smc, APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
if (ret)
goto out;
@@ -589,7 +596,7 @@ static int applesmc_init_smcreg_try(struct applesmc_device *smc)
if (s->init_complete)
return 0;
- ret = read_register_count(&count);
+ ret = read_register_count(smc, &count);
if (ret)
return ret;
@@ -1468,7 +1475,7 @@ static int __init applesmc_init(void)
goto out;
}
- if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
+ if (!request_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS,
"applesmc")) {
ret = -ENXIO;
goto out;
@@ -1490,7 +1497,7 @@ static int __init applesmc_init(void)
out_driver:
platform_driver_unregister(&applesmc_driver);
out_region:
- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
+ release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
out:
pr_warn("driver init failed (ret=%d)!\n", ret);
return ret;
@@ -1500,7 +1507,7 @@ static void __exit applesmc_exit(void)
{
platform_device_unregister(pdev);
platform_driver_unregister(&applesmc_driver);
- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
+ release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
}
module_init(applesmc_init);
--
2.30.0
From ee3d4bf4a01bc94553bde2ae3e806a63a13faa12 Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <paul@mrarm.io>
Date: Sun, 17 Nov 2019 23:12:08 +0100
Subject: [PATCH 3/6] applesmc: switch to acpi_device (from platform)
This change makes the change from platform_device
to acpi_device. The rationale for this change is
that on T2 Macs, an additional FixedMemory32
region is needed for device operation, and it can
be easily resolved via ACPI tables (this will be
done in another commit).
Additionally, on older Macs, the OS X driver also
looks for the specified ACPI device to resolve
its memory regions, and therefore this change
should not result in any incompatibilities.
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 125 ++++++++++++++++++++++++++-------------
1 file changed, 85 insertions(+), 40 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 39ed0bb21365..bdaaf696f7b6 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -19,7 +19,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/delay.h>
-#include <linux/platform_device.h>
+#include <linux/acpi.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/slab.h>
@@ -35,7 +35,6 @@
#include <linux/err.h>
#include <linux/bits.h>
-#define APPLESMC_PORT_BASE 0x300
/* data port used by Apple SMC */
#define APPLESMC_DATA_PORT 0
/* command/status port used by Apple SMC */
@@ -138,9 +137,10 @@ struct applesmc_registers {
};
struct applesmc_device {
- struct platform_device *dev;
+ struct acpi_device *dev;
struct applesmc_registers reg;
+ bool port_base_set;
u16 port_base;
s16 rest_x;
@@ -692,9 +692,13 @@ static int applesmc_init_smcreg(struct applesmc_device *smc)
}
/* Device model stuff */
+
+static int applesmc_init_resources(struct applesmc_device *smc);
+static void applesmc_free_resources(struct applesmc_device *smc);
static int applesmc_create_modules(struct applesmc_device *smc);
static void applesmc_destroy_modules(struct applesmc_device *smc);
-static int applesmc_probe(struct platform_device *dev)
+
+static int applesmc_add(struct acpi_device *dev)
{
struct applesmc_device *smc;
int ret;
@@ -705,12 +709,16 @@ static int applesmc_probe(struct platform_device *dev)
smc->dev = dev;
mutex_init(&smc->reg.mutex);
- platform_set_drvdata(dev, smc);
+ dev_set_drvdata(&dev->dev, smc);
- ret = applesmc_init_smcreg(smc);
+ ret = applesmc_init_resources(smc);
if (ret)
goto out_mem;
+ ret = applesmc_init_smcreg(smc);
+ if (ret)
+ goto out_res;
+
applesmc_device_init(smc);
ret = applesmc_create_modules(smc);
@@ -721,20 +729,23 @@ static int applesmc_probe(struct platform_device *dev)
out_reg:
applesmc_destroy_smcreg(smc);
+out_res:
+ applesmc_free_resources(smc);
out_mem:
- platform_set_drvdata(dev, NULL);
+ dev_set_drvdata(&dev->dev, NULL);
mutex_destroy(&smc->reg.mutex);
kfree(smc);
return ret;
}
-static int applesmc_remove(struct platform_device *dev)
+static int applesmc_remove(struct acpi_device *dev)
{
- struct applesmc_device *smc = platform_get_drvdata(dev);
+ struct applesmc_device *smc = dev_get_drvdata(&dev->dev);
applesmc_destroy_modules(smc);
applesmc_destroy_smcreg(smc);
+ applesmc_free_resources(smc);
mutex_destroy(&smc->reg.mutex);
kfree(smc);
@@ -742,6 +753,52 @@ static int applesmc_remove(struct platform_device *dev)
return 0;
}
+static acpi_status applesmc_walk_resources(struct acpi_resource *res,
+ void *data)
+{
+ struct applesmc_device *smc = data;
+
+ switch (res->type) {
+ case ACPI_RESOURCE_TYPE_IO:
+ if (!smc->port_base_set) {
+ if (res->data.io.address_length < APPLESMC_NR_PORTS)
+ return AE_ERROR;
+ smc->port_base = res->data.io.minimum;
+ smc->port_base_set = true;
+ }
+ return AE_OK;
+
+ case ACPI_RESOURCE_TYPE_END_TAG:
+ if (smc->port_base_set)
+ return AE_OK;
+ else
+ return AE_NOT_FOUND;
+
+ default:
+ return AE_OK;
+ }
+}
+
+static int applesmc_init_resources(struct applesmc_device *smc)
+{
+ int ret;
+
+ ret = acpi_walk_resources(smc->dev->handle, METHOD_NAME__CRS,
+ applesmc_walk_resources, smc);
+ if (ACPI_FAILURE(ret))
+ return -ENXIO;
+
+ if (!request_region(smc->port_base, APPLESMC_NR_PORTS, "applesmc"))
+ return -ENXIO;
+
+ return 0;
+}
+
+static void applesmc_free_resources(struct applesmc_device *smc)
+{
+ release_region(smc->port_base, APPLESMC_NR_PORTS);
+}
+
/* Synchronize device with memorized backlight state */
static int applesmc_pm_resume(struct device *dev)
{
@@ -763,18 +820,28 @@ static int applesmc_pm_restore(struct device *dev)
return applesmc_pm_resume(dev);
}
+static const struct acpi_device_id applesmc_ids[] = {
+ {"APP0001", 0},
+ {"", 0},
+};
+
static const struct dev_pm_ops applesmc_pm_ops = {
.resume = applesmc_pm_resume,
.restore = applesmc_pm_restore,
};
-static struct platform_driver applesmc_driver = {
- .probe = applesmc_probe,
- .remove = applesmc_remove,
- .driver = {
- .name = "applesmc",
- .pm = &applesmc_pm_ops,
+static struct acpi_driver applesmc_driver = {
+ .name = "applesmc",
+ .class = "applesmc",
+ .ids = applesmc_ids,
+ .ops = {
+ .add = applesmc_add,
+ .remove = applesmc_remove
},
+ .drv = {
+ .pm = &applesmc_pm_ops
+ },
+ .owner = THIS_MODULE
};
/*
@@ -1262,7 +1329,6 @@ static int applesmc_create_nodes(struct applesmc_device *smc,
static int applesmc_create_accelerometer(struct applesmc_device *smc)
{
int ret;
-
if (!smc->reg.has_accelerometer)
return 0;
@@ -1463,8 +1529,6 @@ static void applesmc_destroy_modules(struct applesmc_device *smc)
applesmc_destroy_nodes(smc, info_group);
}
-static struct platform_device *pdev;
-
static int __init applesmc_init(void)
{
int ret;
@@ -1475,29 +1539,12 @@ static int __init applesmc_init(void)
goto out;
}
- if (!request_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS,
- "applesmc")) {
- ret = -ENXIO;
- goto out;
- }
-
- ret = platform_driver_register(&applesmc_driver);
+ ret = acpi_bus_register_driver(&applesmc_driver);
if (ret)
- goto out_region;
-
- pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
- NULL, 0);
- if (IS_ERR(pdev)) {
- ret = PTR_ERR(pdev);
- goto out_driver;
- }
+ goto out;
return 0;
-out_driver:
- platform_driver_unregister(&applesmc_driver);
-out_region:
- release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
out:
pr_warn("driver init failed (ret=%d)!\n", ret);
return ret;
@@ -1505,9 +1552,7 @@ static int __init applesmc_init(void)
static void __exit applesmc_exit(void)
{
- platform_device_unregister(pdev);
- platform_driver_unregister(&applesmc_driver);
- release_region(APPLESMC_PORT_BASE, APPLESMC_NR_PORTS);
+ acpi_bus_unregister_driver(&applesmc_driver);
}
module_init(applesmc_init);
--
2.30.0
From 43df89a1377782788760808d8ea4bcf0730effbb Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <paul@mrarm.io>
Date: Sun, 17 Nov 2019 23:12:14 +0100
Subject: [PATCH 4/6] applesmc: key interface wrappers
This change replaces the read_smc and write_smc
methods with wrappers, additionally removing the
command id parameter from them (and introducing
get_smc_key_by_index and get_smc_key_info).
This is done as to allow simple implementation
replacement on T2 Macs. The newly introduced
methods mentioned in the previous paragraph need
special handling on T2 and as such had to be
separated.
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 119 ++++++++++++++++++++++++++-------------
1 file changed, 79 insertions(+), 40 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index bdaaf696f7b6..3017d8ca2c79 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -172,7 +172,7 @@ static const int debug;
* run out past 500ms.
*/
-static int wait_status(struct applesmc_device *smc, u8 val, u8 mask)
+static int port_wait_status(struct applesmc_device *smc, u8 val, u8 mask)
{
u8 status;
int us;
@@ -190,13 +190,13 @@ static int wait_status(struct applesmc_device *smc, u8 val, u8 mask)
return -EIO;
}
-/* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
+/* port_send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
-static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
+static int port_send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
{
int status;
- status = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
+ status = port_wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (status)
return status;
/*
@@ -205,7 +205,7 @@ static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
* this extra read may not happen if status returns both
* simultaneously and this would appear to be required.
*/
- status = wait_status(smc, SMC_STATUS_BUSY, SMC_STATUS_BUSY);
+ status = port_wait_status(smc, SMC_STATUS_BUSY, SMC_STATUS_BUSY);
if (status)
return status;
@@ -213,15 +213,16 @@ static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
return 0;
}
-/* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
+/* port_send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
-static int send_command(struct applesmc_device *smc, u8 cmd)
+static int port_send_command(struct applesmc_device *smc, u8 cmd)
{
int ret;
- ret = wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
+ ret = port_wait_status(smc, 0, SMC_STATUS_IB_CLOSED);
if (ret)
return ret;
+
outb(cmd, smc->port_base + APPLESMC_CMD_PORT);
return 0;
}
@@ -232,53 +233,53 @@ static int send_command(struct applesmc_device *smc, u8 cmd)
* If busy is stuck high after the command then the SMC is jammed.
*/
-static int smc_sane(struct applesmc_device *smc)
+static int port_smc_sane(struct applesmc_device *smc)
{
int ret;
- ret = wait_status(smc, 0, SMC_STATUS_BUSY);
+ ret = port_wait_status(smc, 0, SMC_STATUS_BUSY);
if (!ret)
return ret;
- ret = send_command(smc, APPLESMC_READ_CMD);
+ ret = port_send_command(smc, APPLESMC_READ_CMD);
if (ret)
return ret;
- return wait_status(smc, 0, SMC_STATUS_BUSY);
+ return port_wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int send_argument(struct applesmc_device *smc, const char *key)
+static int port_send_argument(struct applesmc_device *smc, const char *key)
{
int i;
for (i = 0; i < 4; i++)
- if (send_byte(smc, key[i], APPLESMC_DATA_PORT))
+ if (port_send_byte(smc, key[i], APPLESMC_DATA_PORT))
return -EIO;
return 0;
}
-static int read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+static int port_read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
u8 *buffer, u8 len)
{
u8 status, data = 0;
int i;
int ret;
- ret = smc_sane(smc);
+ ret = port_smc_sane(smc);
if (ret)
return ret;
- if (send_command(smc, cmd) || send_argument(smc, key)) {
+ if (port_send_command(smc, cmd) || port_send_argument(smc, key)) {
pr_warn("%.4s: read arg fail\n", key);
return -EIO;
}
/* This has no effect on newer (2012) SMCs */
- if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
+ if (port_send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: read len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (wait_status(smc,
+ if (port_wait_status(smc,
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
pr_warn("%.4s: read data[%d] fail\n", key, i);
@@ -298,37 +299,80 @@ static int read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
if (i)
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
- return wait_status(smc, 0, SMC_STATUS_BUSY);
+ return port_wait_status(smc, 0, SMC_STATUS_BUSY);
}
-static int write_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+static int port_write_smc(struct applesmc_device *smc, u8 cmd, const char *key,
const u8 *buffer, u8 len)
{
int i;
int ret;
- ret = smc_sane(smc);
+ ret = port_smc_sane(smc);
if (ret)
return ret;
- if (send_command(smc, cmd) || send_argument(smc, key)) {
+ if (port_send_command(smc, cmd) || port_send_argument(smc, key)) {
pr_warn("%s: write arg fail\n", key);
return -EIO;
}
- if (send_byte(smc, len, APPLESMC_DATA_PORT)) {
+ if (port_send_byte(smc, len, APPLESMC_DATA_PORT)) {
pr_warn("%.4s: write len fail\n", key);
return -EIO;
}
for (i = 0; i < len; i++) {
- if (send_byte(smc, buffer[i], APPLESMC_DATA_PORT)) {
+ if (port_send_byte(smc, buffer[i], APPLESMC_DATA_PORT)) {
pr_warn("%s: write data fail\n", key);
return -EIO;
}
}
- return wait_status(smc, 0, SMC_STATUS_BUSY);
+ return port_wait_status(smc, 0, SMC_STATUS_BUSY);
+}
+
+static int port_get_smc_key_info(struct applesmc_device *smc,
+ const char *key, struct applesmc_entry *info)
+{
+ int ret;
+ u8 raw[6];
+
+ ret = port_read_smc(smc, APPLESMC_GET_KEY_TYPE_CMD, key, raw, 6);
+ if (ret)
+ return ret;
+ info->len = raw[0];
+ memcpy(info->type, &raw[1], 4);
+ info->flags = raw[5];
+ return 0;
+}
+
+static int read_smc(struct applesmc_device *smc, const char *key,
+ u8 *buffer, u8 len)
+{
+ return port_read_smc(smc, APPLESMC_READ_CMD, key, buffer, len);
+}
+
+static int write_smc(struct applesmc_device *smc, const char *key,
+ const u8 *buffer, u8 len)
+{
+ return port_write_smc(smc, APPLESMC_WRITE_CMD, key, buffer, len);
+}
+
+static int get_smc_key_by_index(struct applesmc_device *smc,
+ unsigned int index, char *key)
+{
+ __be32 be;
+
+ be = cpu_to_be32(index);
+ return port_read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD,
+ (const char *) &be, (u8 *) key, 4);
+}
+
+static int get_smc_key_info(struct applesmc_device *smc, const char *key,
+ struct applesmc_entry *info)
+{
+ return port_get_smc_key_info(smc, key, info);
}
static int read_register_count(struct applesmc_device *smc,
@@ -337,8 +381,8 @@ static int read_register_count(struct applesmc_device *smc,
__be32 be;
int ret;
- ret = read_smc(smc, APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
- if (ret)
+ ret = read_smc(smc, KEY_COUNT_KEY, (u8 *)&be, 4);
+ if (ret < 0)
return ret;
*count = be32_to_cpu(be);
@@ -360,7 +404,7 @@ static int applesmc_read_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = read_smc(smc, APPLESMC_READ_CMD, entry->key, buf, len);
+ ret = read_smc(smc, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
@@ -374,7 +418,7 @@ static int applesmc_write_entry(struct applesmc_device *smc,
if (entry->len != len)
return -EINVAL;
mutex_lock(&smc->reg.mutex);
- ret = write_smc(smc, APPLESMC_WRITE_CMD, entry->key, buf, len);
+ ret = write_smc(smc, entry->key, buf, len);
mutex_unlock(&smc->reg.mutex);
return ret;
}
@@ -383,8 +427,7 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
struct applesmc_device *smc, int index)
{
struct applesmc_entry *cache = &smc->reg.cache[index];
- u8 key[4], info[6];
- __be32 be;
+ char key[4];
int ret = 0;
if (cache->valid)
@@ -394,18 +437,14 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
if (cache->valid)
goto out;
- be = cpu_to_be32(index);
- ret = read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
+ ret = get_smc_key_by_index(smc, index, key);
if (ret)
goto out;
- ret = read_smc(smc, APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
+ memcpy(cache->key, key, 4);
+
+ ret = get_smc_key_info(smc, key, cache);
if (ret)
goto out;
-
- memcpy(cache->key, key, 4);
- cache->len = info[0];
- memcpy(cache->type, &info[1], 4);
- cache->flags = info[5];
cache->valid = true;
out:
--
2.30.0
From 799e7a54c62a36007f7874c58d7dac87c9651759 Mon Sep 17 00:00:00 2001
From: Aun-Ali Zaidi <admin@kodeit.net>
Date: Sun, 17 Nov 2019 23:12:16 +0100
Subject: [PATCH 5/6] applesmc: basic mmio interface implementation
This change introduces a basic MMIO-based
interface implementation required to communicate
with the SMC on T2 Macs. The MMIO interface is
enabled only when it's supported on the running
system.
The MMIO interface replaces legacy port-based SMC
key reads, writes and metadata requests (getting
key by index and getting key info).
(Based on patch by @mcmrarm)
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 237 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 231 insertions(+), 6 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 3017d8ca2c79..2d23bb9ad9dd 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -42,6 +42,18 @@
#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
+#define APPLESMC_IOMEM_KEY_DATA 0
+#define APPLESMC_IOMEM_KEY_STATUS 0x4005
+#define APPLESMC_IOMEM_KEY_NAME 0x78
+#define APPLESMC_IOMEM_KEY_DATA_LEN 0x7D
+#define APPLESMC_IOMEM_KEY_SMC_ID 0x7E
+#define APPLESMC_IOMEM_KEY_CMD 0x7F
+#define APPLESMC_IOMEM_MIN_SIZE 0x4006
+
+#define APPLESMC_IOMEM_KEY_TYPE_CODE 0
+#define APPLESMC_IOMEM_KEY_TYPE_DATA_LEN 5
+#define APPLESMC_IOMEM_KEY_TYPE_FLAGS 6
+
#define APPLESMC_MAX_DATA_LENGTH 32
/* Apple SMC status bits */
@@ -138,10 +150,13 @@ struct applesmc_registers {
struct applesmc_device {
struct acpi_device *dev;
+ struct device *ldev;
struct applesmc_registers reg;
- bool port_base_set;
+ bool port_base_set, iomem_base_set;
u16 port_base;
+ u8 *__iomem iomem_base;
+ u32 iomem_base_addr, iomem_base_size;
s16 rest_x;
s16 rest_y;
@@ -347,16 +362,156 @@ static int port_get_smc_key_info(struct applesmc_device *smc,
return 0;
}
+
+/*
+ * MMIO based communication.
+ * TODO: Use updated mechanism for cmd timeout/retry
+ */
+
+static void iomem_clear_status(struct applesmc_device *smc)
+{
+ if (ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_STATUS))
+ iowrite8(0, smc->iomem_base + APPLESMC_IOMEM_KEY_STATUS);
+}
+
+static int iomem_wait_read(struct applesmc_device *smc)
+{
+ u8 status;
+ int us;
+ int i;
+
+ us = APPLESMC_MIN_WAIT;
+ for (i = 0; i < 24 ; i++) {
+ status = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_STATUS);
+ if (status & 0x20)
+ return 0;
+ usleep_range(us, us * 2);
+ if (i > 9)
+ us <<= 1;
+ }
+
+ dev_warn(smc->ldev, "%s... timeout\n", __func__);
+ return -EIO;
+}
+
+static int iomem_read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ u8 *buffer, u8 len)
+{
+ u8 err, remote_len;
+ u32 key_int = *((u32 *) key);
+
+ iomem_clear_status(smc);
+ iowrite32(key_int, smc->iomem_base + APPLESMC_IOMEM_KEY_NAME);
+ iowrite32(0, smc->iomem_base + APPLESMC_IOMEM_KEY_SMC_ID);
+ iowrite32(cmd, smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+
+ if (iomem_wait_read(smc))
+ return -EIO;
+
+ err = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+ if (err != 0) {
+ dev_warn(smc->ldev, "read_smc_mmio(%x %8x/%.4s) failed: %u\n",
+ cmd, key_int, key, err);
+ return -EIO;
+ }
+
+ if (cmd == APPLESMC_READ_CMD) {
+ remote_len = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_DATA_LEN);
+ if (remote_len != len) {
+ dev_warn(smc->ldev,
+ "read_smc_mmio(%x %8x/%.4s) failed: buffer length mismatch (remote = %u, requested = %u)\n",
+ cmd, key_int, key, remote_len, len);
+ return -EINVAL;
+ }
+ } else {
+ remote_len = len;
+ }
+
+ memcpy_fromio(buffer, smc->iomem_base + APPLESMC_IOMEM_KEY_DATA,
+ remote_len);
+
+ dev_dbg(smc->ldev, "read_smc_mmio(%x %8x/%.4s): buflen=%u reslen=%u\n",
+ cmd, key_int, key, len, remote_len);
+ print_hex_dump_bytes("read_smc_mmio(): ", DUMP_PREFIX_NONE, buffer, remote_len);
+ return 0;
+}
+
+static int iomem_get_smc_key_type(struct applesmc_device *smc, const char *key,
+ struct applesmc_entry *e)
+{
+ u8 err;
+ u8 cmd = APPLESMC_GET_KEY_TYPE_CMD;
+ u32 key_int = *((u32 *) key);
+
+ iomem_clear_status(smc);
+ iowrite32(key_int, smc->iomem_base + APPLESMC_IOMEM_KEY_NAME);
+ iowrite32(0, smc->iomem_base + APPLESMC_IOMEM_KEY_SMC_ID);
+ iowrite32(cmd, smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+
+ if (iomem_wait_read(smc))
+ return -EIO;
+
+ err = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+ if (err != 0) {
+ dev_warn(smc->ldev, "get_smc_key_type_mmio(%.4s) failed: %u\n", key, err);
+ return -EIO;
+ }
+
+ e->len = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_TYPE_DATA_LEN);
+ *((uint32_t *) e->type) = ioread32(
+ smc->iomem_base + APPLESMC_IOMEM_KEY_TYPE_CODE);
+ e->flags = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_TYPE_FLAGS);
+
+ dev_dbg(smc->ldev, "get_smc_key_type_mmio(%.4s): len=%u type=%.4s flags=%x\n",
+ key, e->len, e->type, e->flags);
+ return 0;
+}
+
+static int iomem_write_smc(struct applesmc_device *smc, u8 cmd, const char *key,
+ const u8 *buffer, u8 len)
+{
+ u8 err;
+ u32 key_int = *((u32 *) key);
+
+ iomem_clear_status(smc);
+ iowrite32(key_int, smc->iomem_base + APPLESMC_IOMEM_KEY_NAME);
+ memcpy_toio(smc->iomem_base + APPLESMC_IOMEM_KEY_DATA, buffer, len);
+ iowrite32(len, smc->iomem_base + APPLESMC_IOMEM_KEY_DATA_LEN);
+ iowrite32(0, smc->iomem_base + APPLESMC_IOMEM_KEY_SMC_ID);
+ iowrite32(cmd, smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+
+ if (iomem_wait_read(smc))
+ return -EIO;
+
+ err = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_CMD);
+ if (err != 0) {
+ dev_warn(smc->ldev, "write_smc_mmio(%x %.4s) failed: %u\n", cmd, key, err);
+ print_hex_dump_bytes("write_smc_mmio(): ", DUMP_PREFIX_NONE, buffer, len);
+ return -EIO;
+ }
+
+ dev_dbg(smc->ldev, "write_smc_mmio(%x %.4s): buflen=%u\n", cmd, key, len);
+ print_hex_dump_bytes("write_smc_mmio(): ", DUMP_PREFIX_NONE, buffer, len);
+ return 0;
+}
+
+
static int read_smc(struct applesmc_device *smc, const char *key,
u8 *buffer, u8 len)
{
- return port_read_smc(smc, APPLESMC_READ_CMD, key, buffer, len);
+ if (smc->iomem_base_set)
+ return iomem_read_smc(smc, APPLESMC_READ_CMD, key, buffer, len);
+ else
+ return port_read_smc(smc, APPLESMC_READ_CMD, key, buffer, len);
}
static int write_smc(struct applesmc_device *smc, const char *key,
const u8 *buffer, u8 len)
{
- return port_write_smc(smc, APPLESMC_WRITE_CMD, key, buffer, len);
+ if (smc->iomem_base_set)
+ return iomem_write_smc(smc, APPLESMC_WRITE_CMD, key, buffer, len);
+ else
+ return port_write_smc(smc, APPLESMC_WRITE_CMD, key, buffer, len);
}
static int get_smc_key_by_index(struct applesmc_device *smc,
@@ -365,14 +520,21 @@ static int get_smc_key_by_index(struct applesmc_device *smc,
__be32 be;
be = cpu_to_be32(index);
- return port_read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD,
- (const char *) &be, (u8 *) key, 4);
+ if (smc->iomem_base_set)
+ return iomem_read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD,
+ (const char *) &be, (u8 *) key, 4);
+ else
+ return port_read_smc(smc, APPLESMC_GET_KEY_BY_INDEX_CMD,
+ (const char *) &be, (u8 *) key, 4);
}
static int get_smc_key_info(struct applesmc_device *smc, const char *key,
struct applesmc_entry *info)
{
- return port_get_smc_key_info(smc, key, info);
+ if (smc->iomem_base_set)
+ return iomem_get_smc_key_type(smc, key, info);
+ else
+ return port_get_smc_key_info(smc, key, info);
}
static int read_register_count(struct applesmc_device *smc,
@@ -746,6 +908,7 @@ static int applesmc_add(struct acpi_device *dev)
if (!smc)
return -ENOMEM;
smc->dev = dev;
+ smc->ldev = &dev->dev;
mutex_init(&smc->reg.mutex);
dev_set_drvdata(&dev->dev, smc);
@@ -807,6 +970,20 @@ static acpi_status applesmc_walk_resources(struct acpi_resource *res,
}
return AE_OK;
+ case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
+ if (!smc->iomem_base_set) {
+ if (res->data.fixed_memory32.address_length <
+ APPLESMC_IOMEM_MIN_SIZE) {
+ dev_warn(smc->ldev, "found iomem but it's too small: %u\n",
+ res->data.fixed_memory32.address_length);
+ return AE_OK;
+ }
+ smc->iomem_base_addr = res->data.fixed_memory32.address;
+ smc->iomem_base_size = res->data.fixed_memory32.address_length;
+ smc->iomem_base_set = true;
+ }
+ return AE_OK;
+
case ACPI_RESOURCE_TYPE_END_TAG:
if (smc->port_base_set)
return AE_OK;
@@ -818,6 +995,8 @@ static acpi_status applesmc_walk_resources(struct acpi_resource *res,
}
}
+static int applesmc_try_enable_iomem(struct applesmc_device *smc);
+
static int applesmc_init_resources(struct applesmc_device *smc)
{
int ret;
@@ -830,11 +1009,57 @@ static int applesmc_init_resources(struct applesmc_device *smc)
if (!request_region(smc->port_base, APPLESMC_NR_PORTS, "applesmc"))
return -ENXIO;
+ if (smc->iomem_base_set) {
+ if (applesmc_try_enable_iomem(smc))
+ smc->iomem_base_set = false;
+ }
+
return 0;
}
+static int applesmc_try_enable_iomem(struct applesmc_device *smc)
+{
+ u8 test_val, ldkn_version;
+
+ dev_dbg(smc->ldev, "Trying to enable iomem based communication\n");
+ smc->iomem_base = ioremap(smc->iomem_base_addr, smc->iomem_base_size);
+ if (!smc->iomem_base)
+ goto out;
+
+ /* Apple's driver does this check for some reason */
+ test_val = ioread8(smc->iomem_base + APPLESMC_IOMEM_KEY_STATUS);
+ if (test_val == 0xff) {
+ dev_warn(smc->ldev,
+ "iomem enable failed: initial status is 0xff (is %x)\n",
+ test_val);
+ goto out_iomem;
+ }
+
+ if (read_smc(smc, "LDKN", &ldkn_version, 1)) {
+ dev_warn(smc->ldev, "iomem enable failed: ldkn read failed\n");
+ goto out_iomem;
+ }
+
+ if (ldkn_version < 2) {
+ dev_warn(smc->ldev,
+ "iomem enable failed: ldkn version %u is less than minimum (2)\n",
+ ldkn_version);
+ goto out_iomem;
+ }
+
+ return 0;
+
+out_iomem:
+ iounmap(smc->iomem_base);
+
+out:
+ return -ENXIO;
+}
+
static void applesmc_free_resources(struct applesmc_device *smc)
{
+ if (smc->iomem_base_set)
+ iounmap(smc->iomem_base);
release_region(smc->port_base, APPLESMC_NR_PORTS);
}
--
2.30.0
From 4e63e9b77422aae8e7411ddc7a8458c2585c86df Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <paul@mrarm.io>
Date: Sun, 17 Nov 2019 23:12:18 +0100
Subject: [PATCH 6/6] applesmc: fan support on T2 Macs
T2 Macs changed the fan values from shorts to
floats, and changed the fan manual override
setting from a bitmask to a per-fan boolean
named F0Md (thanks to @kleuter for mentioning
it).
A minimal soft-float implementation has been
written for convert floats to integers (and vice
versa).
Signed-off-by: Aun-Ali Zaidi <admin@kodeit.net>
---
drivers/hwmon/applesmc.c | 119 +++++++++++++++++++++++++++++++++------
1 file changed, 102 insertions(+), 17 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 2d23bb9ad9dd..0938227be612 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -87,6 +87,7 @@
#define FAN_ID_FMT "F%dID" /* r-o char[16] */
#define TEMP_SENSOR_TYPE "sp78"
+#define FLOAT_TYPE "flt "
/* List of keys used to read/write fan speeds */
static const char *const fan_speed_fmt[] = {
@@ -96,6 +97,7 @@ static const char *const fan_speed_fmt[] = {
"F%dSf", /* safe speed - not all models */
"F%dTg", /* target speed (manual: rw) */
};
+#define FAN_MANUAL_FMT "F%dMd"
#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
@@ -734,6 +736,42 @@ static int applesmc_read_s16(struct applesmc_device *smc,
return 0;
}
+/**
+ * applesmc_float_to_u32 - Retrieve the integral part of a float.
+ * This is needed because Apple made fans use float values in the T2.
+ * The fractional point is not significantly useful though, and the integral
+ * part can be easily extracted.
+ */
+static inline u32 applesmc_float_to_u32(u32 d)
+{
+ u8 sign = (u8) ((d >> 31) & 1);
+ s32 exp = (s32) ((d >> 23) & 0xff) - 0x7f;
+ u32 fr = d & ((1u << 23) - 1);
+
+ if (sign || exp < 0)
+ return 0;
+
+ return (u32) ((1u << exp) + (fr >> (23 - exp)));
+}
+
+/**
+ * applesmc_u32_to_float - Convert an u32 into a float.
+ * See applesmc_float_to_u32 for a rationale.
+ */
+static inline u32 applesmc_u32_to_float(u32 d)
+{
+ u32 dc = d, bc = 0, exp;
+
+ if (!d)
+ return 0;
+
+ while (dc >>= 1)
+ ++bc;
+ exp = 0x7f + bc;
+
+ return (u32) ((exp << 23) |
+ ((d << (23 - (exp - 0x7f))) & ((1u << 23) - 1)));
+}
/*
* applesmc_device_init - initialize the accelerometer. Can sleep.
*/
@@ -1242,6 +1280,7 @@ static ssize_t applesmc_show_fan_speed(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
struct applesmc_device *smc = dev_get_drvdata(dev);
+ const struct applesmc_entry *entry;
int ret;
unsigned int speed = 0;
char newkey[5];
@@ -1250,11 +1289,21 @@ static ssize_t applesmc_show_fan_speed(struct device *dev,
scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
to_index(attr));
- ret = applesmc_read_key(smc, newkey, buffer, 2);
+ entry = applesmc_get_entry_by_key(smc, newkey);
+ if (IS_ERR(entry))
+ return PTR_ERR(entry);
+
+ if (!strcmp(entry->type, FLOAT_TYPE)) {
+ ret = applesmc_read_entry(smc, entry, (u8 *) &speed, 4);
+ speed = applesmc_float_to_u32(speed);
+ } else {
+ ret = applesmc_read_entry(smc, entry, buffer, 2);
+ speed = ((buffer[0] << 8 | buffer[1]) >> 2);
+ }
+
if (ret)
return ret;
- speed = ((buffer[0] << 8 | buffer[1]) >> 2);
return sysfs_emit(sysfsbuf, "%u\n", speed);
}
@@ -1263,6 +1312,7 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
const char *sysfsbuf, size_t count)
{
struct applesmc_device *smc = dev_get_drvdata(dev);
+ const struct applesmc_entry *entry;
int ret;
unsigned long speed;
char newkey[5];
@@ -1274,9 +1324,18 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
to_index(attr));
- buffer[0] = (speed >> 6) & 0xff;
- buffer[1] = (speed << 2) & 0xff;
- ret = applesmc_write_key(smc, newkey, buffer, 2);
+ entry = applesmc_get_entry_by_key(smc, newkey);
+ if (IS_ERR(entry))
+ return PTR_ERR(entry);
+
+ if (!strcmp(entry->type, FLOAT_TYPE)) {
+ speed = applesmc_u32_to_float(speed);
+ ret = applesmc_write_entry(smc, entry, (u8 *) &speed, 4);
+ } else {
+ buffer[0] = (speed >> 6) & 0xff;
+ buffer[1] = (speed << 2) & 0xff;
+ ret = applesmc_write_key(smc, newkey, buffer, 2);
+ }
if (ret)
return ret;
@@ -1291,12 +1350,26 @@ static ssize_t applesmc_show_fan_manual(struct device *dev,
int ret;
u16 manual = 0;
u8 buffer[2];
+ char newkey[5];
+ bool has_newkey = false;
+
+ scnprintf(newkey, sizeof(newkey), FAN_MANUAL_FMT, to_index(attr));
+
+ ret = applesmc_has_key(smc, newkey, &has_newkey);
+ if (ret)
+ return ret;
+
+ if (has_newkey) {
+ ret = applesmc_read_key(smc, newkey, buffer, 1);
+ manual = buffer[0];
+ } else {
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
+ manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
+ }
- ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
if (ret)
return ret;
- manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
return sysfs_emit(sysfsbuf, "%d\n", manual);
}
@@ -1307,27 +1380,39 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u8 buffer[2];
+ char newkey[5];
+ bool has_newkey = false;
unsigned long input;
u16 val;
if (kstrtoul(sysfsbuf, 10, &input) < 0)
return -EINVAL;
- ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
+ scnprintf(newkey, sizeof(newkey), FAN_MANUAL_FMT, to_index(attr));
+
+ ret = applesmc_has_key(smc, newkey, &has_newkey);
if (ret)
- goto out;
+ return ret;
- val = (buffer[0] << 8 | buffer[1]);
+ if (has_newkey) {
+ buffer[0] = input & 1;
+ ret = applesmc_write_key(smc, newkey, buffer, 1);
+ } else {
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
+ val = (buffer[0] << 8 | buffer[1]);
+ if (ret)
+ goto out;
- if (input)
- val = val | (0x01 << to_index(attr));
- else
- val = val & ~(0x01 << to_index(attr));
+ if (input)
+ val = val | (0x01 << to_index(attr));
+ else
+ val = val & ~(0x01 << to_index(attr));
- buffer[0] = (val >> 8) & 0xFF;
- buffer[1] = val & 0xFF;
+ buffer[0] = (val >> 8) & 0xFF;
+ buffer[1] = val & 0xFF;
- ret = applesmc_write_key(smc, FANS_MANUAL, buffer, 2);
+ ret = applesmc_write_key(smc, FANS_MANUAL, buffer, 2);
+ }
out:
if (ret)
--
2.30.0
From 58868e6f356229eab48cfdee1603011653a19c79 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <redecorating@protonmail.com>
Date: Sun, 9 Oct 2022 15:59:01 +0530
Subject: [PATCH] applesmc: Add iMacPro to applesmc_whitelist
The iMacPro1,1 is the only iMacPro released before the line was
discontinued. Add it to the applesmc_whitelist.
Signed-off-by: Orlando Chamberlain <redecorating@protonmail.com>
---
drivers/hwmon/applesmc.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 8b3f73fcb..493f95bb0 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -1804,6 +1804,10 @@ static const struct dmi_system_id applesmc_whitelist[] __initconst = {
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
},
+ { applesmc_dmi_match, "Apple iMacPro", {
+ DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
+ DMI_MATCH(DMI_PRODUCT_NAME, "iMacPro") },
+ },
{ applesmc_dmi_match, "Apple MacPro", {
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
--
2.34.1
From e52b0fad357b6203691942831715fce4f26d66e2 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Tue, 24 Jan 2023 15:46:48 +1100
Subject: [PATCH 1/1] applesmc: make applesmc_remove void
for linux6.2 compatibility
---
drivers/hwmon/applesmc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index d071130ff68d..12be9269a314 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -979,7 +979,7 @@ static int applesmc_add(struct acpi_device *dev)
return ret;
}
-static int applesmc_remove(struct acpi_device *dev)
+static void applesmc_remove(struct acpi_device *dev)
{
struct applesmc_device *smc = dev_get_drvdata(&dev->dev);
@@ -990,7 +990,7 @@ static int applesmc_remove(struct acpi_device *dev)
mutex_destroy(&smc->reg.mutex);
kfree(smc);
- return 0;
+ return;
}
static acpi_status applesmc_walk_resources(struct acpi_resource *res,
--
2.39.1
From 38786c7979c8ece013b5b7d5cb07dc2aa40198be Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Mon, 30 Jan 2023 18:42:21 +1100
Subject: [PATCH 1/1] applesmc: battery charge limiter
---
drivers/hwmon/applesmc.c | 42 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 41 insertions(+), 1 deletion(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index 12be9269a314..bc1eec74cfef 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -1478,6 +1478,35 @@ static void applesmc_brightness_set(struct led_classdev *led_cdev,
dev_dbg(led_cdev->dev, "work was already on the queue.\n");
}
+static ssize_t applesmc_BCLM_store(struct device *dev,
+ struct device_attribute *attr, char *sysfsbuf, size_t count)
+{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ u8 val;
+
+ if (kstrtou8(sysfsbuf, 10, &val) < 0)
+ return -EINVAL;
+
+ if (val < 0 || val > 100)
+ return -EINVAL;
+
+ if (applesmc_write_key(smc, "BCLM", &val, 1))
+ return -ENODEV;
+ return count;
+}
+
+static ssize_t applesmc_BCLM_show(struct device *dev,
+ struct device_attribute *attr, char *sysfsbuf)
+{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ u8 val;
+
+ if (applesmc_read_key(smc, "BCLM", &val, 1))
+ return -ENODEV;
+
+ return sysfs_emit(sysfsbuf, "%d\n", val);
+}
+
static ssize_t applesmc_key_count_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
@@ -1612,6 +1641,11 @@ static struct applesmc_node_group temp_group[] = {
{ }
};
+static struct applesmc_node_group BCLM_group[] = {
+ { "battery_charge_limit", applesmc_BCLM_show, applesmc_BCLM_store },
+ { }
+};
+
/* Module stuff */
/*
@@ -1830,10 +1864,13 @@ static int applesmc_create_modules(struct applesmc_device *smc)
ret = applesmc_create_nodes(smc, info_group, 1);
if (ret)
goto out;
+ ret = applesmc_create_nodes(smc, BCLM_group, 1);
+ if (ret)
+ goto out_info;
ret = applesmc_create_nodes(smc, fan_group, smc->reg.fan_count);
if (ret)
- goto out_info;
+ goto out_bclm;
ret = applesmc_create_nodes(smc, temp_group, smc->reg.index_count);
if (ret)
@@ -1869,6 +1906,8 @@ static int applesmc_create_modules(struct applesmc_device *smc)
applesmc_destroy_nodes(smc, temp_group);
out_fans:
applesmc_destroy_nodes(smc, fan_group);
+out_bclm:
+ applesmc_destroy_nodes(smc, BCLM_group);
out_info:
applesmc_destroy_nodes(smc, info_group);
out:
@@ -1883,6 +1922,7 @@ static void applesmc_destroy_modules(struct applesmc_device *smc)
applesmc_release_accelerometer(smc);
applesmc_destroy_nodes(smc, temp_group);
applesmc_destroy_nodes(smc, fan_group);
+ applesmc_destroy_nodes(smc, BCLM_group);
applesmc_destroy_nodes(smc, info_group);
}
--
2.39.1
From 327e6e1d0f6e8db68c124dff4d6a326b381ccedb Mon Sep 17 00:00:00 2001
From: Aun-Ali Zaidi <admin@kodeit.net>
Date: Wed, 23 Mar 2022 17:12:21 +0530
Subject: [PATCH] Input: bcm5974 - Add support for the T2 Macs
---
drivers/input/mouse/bcm5974.c | 138 ++++++++++++++++++++++++++++++++++
1 file changed, 138 insertions(+)
diff --git a/drivers/input/mouse/bcm5974.c b/drivers/input/mouse/bcm5974.c
index 59a14505b..88f17f21a 100644
--- a/drivers/input/mouse/bcm5974.c
+++ b/drivers/input/mouse/bcm5974.c
@@ -83,6 +83,24 @@
#define USB_DEVICE_ID_APPLE_WELLSPRING9_ISO 0x0273
#define USB_DEVICE_ID_APPLE_WELLSPRING9_JIS 0x0274
+/* T2-Attached Devices */
+/* MacbookAir8,1 (2018) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K 0x027a
+/* MacbookPro15,2 (2018) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 0x027b
+/* MacbookPro15,1 (2018) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 0x027c
+/* MacbookPro15,4 (2019) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213 0x027d
+/* MacbookPro16,2 (2020) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K 0x027e
+/* MacbookPro16,3 (2020) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 0x027f
+/* MacbookAir9,1 (2020) */
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K 0x0280
+/* MacbookPro16,1 (2019)*/
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F 0x0340
+
#define BCM5974_DEVICE(prod) { \
.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | \
USB_DEVICE_ID_MATCH_INT_CLASS | \
@@ -147,6 +165,22 @@ static const struct usb_device_id bcm5974_table[] = {
BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI),
BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_ISO),
BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
+ /* MacbookAir8,1 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K),
+ /* MacbookPro15,2 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132),
+ /* MacbookPro15,1 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680),
+ /* MacbookPro15,4 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213),
+ /* MacbookPro16,2 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K),
+ /* MacbookPro16,3 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223),
+ /* MacbookAir9,1 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K),
+ /* MacbookPro16,1 */
+ BCM5974_DEVICE(USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F),
/* Terminating entry */
{}
};
@@ -483,6 +517,110 @@ static const struct bcm5974_config bcm5974_config_table[] = {
{ SN_COORD, -203, 6803 },
{ SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
},
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -6243, 6749 },
+ { SN_COORD, -170, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -6243, 6749 },
+ { SN_COORD, -170, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -7456, 7976 },
+ { SN_COORD, -1768, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -6243, 6749 },
+ { SN_COORD, -170, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -7823, 8329 },
+ { SN_COORD, -370, 7925 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -6243, 6749 },
+ { SN_COORD, -170, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -6243, 6749 },
+ { SN_COORD, -170, 7685 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
+ {
+ USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F,
+ 0,
+ 0,
+ HAS_INTEGRATED_BUTTON,
+ 0, sizeof(struct bt_data),
+ 0x83, DATAFORMAT(TYPE4),
+ { SN_PRESSURE, 0, 300 },
+ { SN_WIDTH, 0, 2048 },
+ { SN_COORD, -8916, 9918 },
+ { SN_COORD, -1934, 9835 },
+ { SN_ORIENT, -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION }
+ },
{}
};
--
2.25.1
From: Ashish Arora <ashisharora.linux@outlook.com>
Subject: Re: [PATCH] drm/i915: Discard large BIOS framebuffers causing display corruption.
Date: Sat, 08 Jan 2022 21:43:18 +1100
On certain 4k panels, the BIOS framebuffer is larger than what panel
requires causing display corruption. Introduce a check for the same.
Signed-off-by: Ashish Arora <ashisharora.linux@outlook.com>
---
drivers/gpu/drm/i915/display/intel_fbdev.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/i915/display/intel_fbdev.c b/drivers/gpu/drm/i915/display/intel_fbdev.c
index 112aa04..8fb8bcc 100644
--- a/drivers/gpu/drm/i915/display/intel_fbdev.c
+++ b/drivers/gpu/drm/i915/display/intel_fbdev.c
@@ -217,10 +217,10 @@ static int intelfb_create(struct drm_fb_helper *helper,
return ret;
if (intel_fb &&
- (sizes->fb_width > intel_fb->base.width ||
- sizes->fb_height > intel_fb->base.height)) {
+ (sizes->fb_width != intel_fb->base.width ||
+ sizes->fb_height != intel_fb->base.height)) {
drm_dbg_kms(&dev_priv->drm,
- "BIOS fb too small (%dx%d), we require (%dx%d),"
+ "BIOS fb not valid (%dx%d), we require (%dx%d),"
" releasing it\n",
intel_fb->base.width, intel_fb->base.height,
sizes->fb_width, sizes->fb_height);
--
1.8.3.1
From 3d4a4a3d62815f90fc65a827a3e2de96c4571350 Mon Sep 17 00:00:00 2001
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
Date: Mon, 20 Nov 2023 10:32:23 +1100
Subject: [PATCH 1/1] acpi video: force native for some T2 macbooks
The intel backlight is needed for these.
MacBookPro15,2/4 or MacBookPro16,3 or MacBookAir8,1/2 might also need
this so I'm not going to be submitting this upstream yet
mbp16,3 was reported not to have this issue for 6.5.8 at least.
---
drivers/acpi/video_detect.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/acpi/video_detect.c b/drivers/acpi/video_detect.c
index 442396f6ed1f..baf7264d7b94 100644
--- a/drivers/acpi/video_detect.c
+++ b/drivers/acpi/video_detect.c
@@ -513,6 +513,14 @@ static const struct dmi_system_id video_detect_dmi_table[] = {
DMI_MATCH(DMI_PRODUCT_NAME, "iMac12,2"),
},
},
+ {
+ .callback = video_detect_force_native,
+ /* Apple MacBook Air 9,1 */
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+ DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir9,1"),
+ },
+ },
{
/* https://bugzilla.redhat.com/show_bug.cgi?id=1217249 */
.callback = video_detect_force_native,
@@ -522,6 +530,14 @@ static const struct dmi_system_id video_detect_dmi_table[] = {
DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro12,1"),
},
},
+ {
+ .callback = video_detect_force_native,
+ /* Apple MacBook Pro 16,2 */
+ .matches = {
+ DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
+ DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
+ },
+ },
{
.callback = video_detect_force_native,
/* Dell Inspiron N4010 */
--
2.42.1
|