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
|
/* $OpenBSD: azalia.c,v 1.232 2017/02/10 08:13:37 ratchov Exp $ */
/* $NetBSD: azalia.c,v 1.20 2006/05/07 08:31:44 kent Exp $ */
/*-
* Copyright (c) 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by TAMURA Kent
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* High Definition Audio Specification
*
* http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/high-definition-audio-specification.pdf
*
*
* TO DO:
* - multiple codecs (needed?)
* - multiple streams (needed?)
*/
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/timeout.h>
#include <dev/audio_if.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/azalia.h>
typedef struct audio_params audio_params_t;
struct audio_format {
void *driver_data;
int32_t mode;
u_int encoding;
u_int precision;
u_int channels;
/**
* 0: frequency[0] is lower limit, and frequency[1] is higher limit.
* 1-16: frequency[0] to frequency[frequency_type-1] are valid.
*/
u_int frequency_type;
#define AUFMT_MAX_FREQUENCIES 16
/**
* sampling rates
*/
u_int frequency[AUFMT_MAX_FREQUENCIES];
};
#ifdef AZALIA_DEBUG
# define DPRINTFN(n,x) do { if (az_debug > (n)) printf x; } while (0/*CONSTCOND*/)
int az_debug = 0;
#else
# define DPRINTFN(n,x) do {} while (0/*CONSTCOND*/)
#endif
/* ----------------------------------------------------------------
* ICH6/ICH7 constant values
* ---------------------------------------------------------------- */
/* PCI registers */
#define ICH_PCI_HDBARL 0x10
#define ICH_PCI_HDBARU 0x14
#define ICH_PCI_HDCTL 0x40
#define ICH_PCI_HDCTL_CLKDETCLR 0x08
#define ICH_PCI_HDCTL_CLKDETEN 0x04
#define ICH_PCI_HDCTL_CLKDETINV 0x02
#define ICH_PCI_HDCTL_SIGNALMODE 0x01
#define ICH_PCI_HDTCSEL 0x44
#define ICH_PCI_HDTCSEL_MASK 0x7
#define ICH_PCI_MMC 0x62
#define ICH_PCI_MMC_ME 0x1
/* internal types */
typedef struct {
bus_dmamap_t map;
caddr_t addr; /* kernel virtual address */
bus_dma_segment_t segments[1];
size_t size;
} azalia_dma_t;
#define AZALIA_DMA_DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
typedef struct {
struct azalia_t *az;
int regbase;
int number;
int dir; /* AUMODE_PLAY or AUMODE_RECORD */
uint32_t intr_bit;
azalia_dma_t bdlist;
azalia_dma_t buffer;
void (*intr)(void*);
void *intr_arg;
int bufsize;
uint16_t fmt;
int blk;
unsigned int swpos; /* position in the audio(4) layer */
} stream_t;
#define STR_READ_1(s, r) \
bus_space_read_1((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r)
#define STR_READ_2(s, r) \
bus_space_read_2((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r)
#define STR_READ_4(s, r) \
bus_space_read_4((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r)
#define STR_WRITE_1(s, r, v) \
bus_space_write_1((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r, v)
#define STR_WRITE_2(s, r, v) \
bus_space_write_2((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r, v)
#define STR_WRITE_4(s, r, v) \
bus_space_write_4((s)->az->iot, (s)->az->ioh, (s)->regbase + HDA_SD_##r, v)
typedef struct azalia_t {
struct device dev;
pci_chipset_tag_t pc;
pcitag_t tag;
void *ih;
bus_space_tag_t iot;
bus_space_handle_t ioh;
bus_size_t map_size;
bus_dma_tag_t dmat;
pcireg_t pciid;
uint32_t subid;
codec_t *codecs;
int ncodecs; /* number of codecs */
int codecno; /* index of the using codec */
int detached; /* 1 if failed to initialize, 2 if
* azalia_pci_detach has run
*/
azalia_dma_t corb_dma;
int corb_entries;
uint8_t corbsize;
azalia_dma_t rirb_dma;
int rirb_entries;
uint8_t rirbsize;
int rirb_rp;
#define UNSOLQ_SIZE 256
rirb_entry_t *unsolq;
int unsolq_wp;
int unsolq_rp;
int unsolq_kick;
struct timeout unsol_to;
int ok64;
int nistreams, nostreams, nbstreams;
stream_t pstream;
stream_t rstream;
} azalia_t;
#define XNAME(sc) ((sc)->dev.dv_xname)
#define AZ_READ_1(z, r) bus_space_read_1((z)->iot, (z)->ioh, HDA_##r)
#define AZ_READ_2(z, r) bus_space_read_2((z)->iot, (z)->ioh, HDA_##r)
#define AZ_READ_4(z, r) bus_space_read_4((z)->iot, (z)->ioh, HDA_##r)
#define AZ_WRITE_1(z, r, v) bus_space_write_1((z)->iot, (z)->ioh, HDA_##r, v)
#define AZ_WRITE_2(z, r, v) bus_space_write_2((z)->iot, (z)->ioh, HDA_##r, v)
#define AZ_WRITE_4(z, r, v) bus_space_write_4((z)->iot, (z)->ioh, HDA_##r, v)
/* prototypes */
uint8_t azalia_pci_read(pci_chipset_tag_t, pcitag_t, int);
void azalia_pci_write(pci_chipset_tag_t, pcitag_t, int, uint8_t);
int azalia_pci_match(struct device *, void *, void *);
void azalia_pci_attach(struct device *, struct device *, void *);
int azalia_pci_activate(struct device *, int);
int azalia_pci_detach(struct device *, int);
void azalia_configure_pci(azalia_t *);
int azalia_intr(void *);
void azalia_print_codec(codec_t *);
int azalia_reset(azalia_t *);
int azalia_get_ctrlr_caps(azalia_t *);
int azalia_init(azalia_t *, int);
int azalia_init_codecs(azalia_t *);
int azalia_init_streams(azalia_t *);
void azalia_shutdown(void *);
int azalia_halt_corb(azalia_t *);
int azalia_init_corb(azalia_t *, int);
int azalia_halt_rirb(azalia_t *);
int azalia_init_rirb(azalia_t *, int);
int azalia_set_command(azalia_t *, nid_t, int, uint32_t, uint32_t);
int azalia_get_response(azalia_t *, uint32_t *);
void azalia_rirb_kick_unsol_events(void *);
void azalia_rirb_intr(azalia_t *);
int azalia_alloc_dmamem(azalia_t *, size_t, size_t, azalia_dma_t *);
int azalia_free_dmamem(const azalia_t *, azalia_dma_t*);
int azalia_codec_init(codec_t *);
int azalia_codec_delete(codec_t *);
void azalia_codec_add_bits(codec_t *, int, uint32_t, int);
void azalia_codec_add_format(codec_t *, int, int, uint32_t, int32_t);
int azalia_codec_connect_stream(stream_t *);
int azalia_codec_disconnect_stream(stream_t *);
void azalia_codec_print_audiofunc(const codec_t *);
void azalia_codec_print_groups(const codec_t *);
int azalia_codec_find_defdac(codec_t *, int, int);
int azalia_codec_find_defadc(codec_t *, int, int);
int azalia_codec_find_defadc_sub(codec_t *, nid_t, int, int);
int azalia_codec_init_volgroups(codec_t *);
int azalia_codec_sort_pins(codec_t *);
int azalia_codec_select_micadc(codec_t *);
int azalia_codec_select_dacs(codec_t *);
int azalia_codec_select_spkrdac(codec_t *);
int azalia_codec_find_inputmixer(codec_t *);
int azalia_widget_init(widget_t *, const codec_t *, int);
int azalia_widget_label_widgets(codec_t *);
int azalia_widget_init_audio(widget_t *, const codec_t *);
int azalia_widget_init_pin(widget_t *, const codec_t *);
int azalia_widget_init_connection(widget_t *, const codec_t *);
int azalia_widget_check_conn(codec_t *, int, int);
int azalia_widget_sole_conn(codec_t *, nid_t);
void azalia_widget_print_widget(const widget_t *, const codec_t *);
void azalia_widget_print_audio(const widget_t *, const char *);
void azalia_widget_print_pin(const widget_t *);
int azalia_stream_init(stream_t *, azalia_t *, int, int, int);
int azalia_stream_reset(stream_t *);
int azalia_stream_start(stream_t *);
int azalia_stream_halt(stream_t *);
int azalia_stream_intr(stream_t *);
int azalia_open(void *, int);
void azalia_close(void *);
int azalia_set_params(void *, int, int, audio_params_t *,
audio_params_t *);
int azalia_round_blocksize(void *, int);
int azalia_halt_output(void *);
int azalia_halt_input(void *);
int azalia_set_port(void *, mixer_ctrl_t *);
int azalia_get_port(void *, mixer_ctrl_t *);
int azalia_query_devinfo(void *, mixer_devinfo_t *);
void *azalia_allocm(void *, int, size_t, int, int);
void azalia_freem(void *, void *, int);
size_t azalia_round_buffersize(void *, int, size_t);
int azalia_get_props(void *);
int azalia_trigger_output(void *, void *, void *, int,
void (*)(void *), void *, audio_params_t *);
int azalia_trigger_input(void *, void *, void *, int,
void (*)(void *), void *, audio_params_t *);
int azalia_params2fmt(const audio_params_t *, uint16_t *);
int azalia_match_format(codec_t *, int, audio_params_t *);
int azalia_set_params_sub(codec_t *, int, audio_params_t *);
void azalia_save_mixer(codec_t *);
void azalia_restore_mixer(codec_t *);
int azalia_suspend(azalia_t *);
int azalia_resume(azalia_t *);
int azalia_resume_codec(codec_t *);
/* variables */
struct cfattach azalia_ca = {
sizeof(azalia_t), azalia_pci_match, azalia_pci_attach,
azalia_pci_detach, azalia_pci_activate
};
struct cfdriver azalia_cd = {
NULL, "azalia", DV_DULL
};
struct audio_hw_if azalia_hw_if = {
azalia_open,
azalia_close,
azalia_set_params,
azalia_round_blocksize,
NULL, /* commit_settings */
NULL, /* init_output */
NULL, /* init_input */
NULL, /* start_output */
NULL, /* start_input */
azalia_halt_output,
azalia_halt_input,
NULL, /* speaker_ctl */
NULL, /* setfd */
azalia_set_port,
azalia_get_port,
azalia_query_devinfo,
azalia_allocm,
azalia_freem,
azalia_round_buffersize,
azalia_get_props,
azalia_trigger_output,
azalia_trigger_input
};
static const char *pin_devices[16] = {
AudioNline, AudioNspeaker, AudioNheadphone, AudioNcd,
"SPDIF", "digital-out", "modem-line", "modem-handset",
"line-in", AudioNaux, AudioNmicrophone, "telephony",
"SPDIF-in", "digital-in", "beep", "other"};
static const char *wtypes[16] = {
"dac", "adc", "mix", "sel", "pin", "pow", "volume",
"beep", "wid08", "wid09", "wid0a", "wid0b", "wid0c",
"wid0d", "wid0e", "vendor"};
static const char *line_colors[16] = {
"unk", "blk", "gry", "blu", "grn", "red", "org", "yel",
"pur", "pnk", "0xa", "0xb", "0xc", "0xd", "wht", "oth"};
/* ================================================================
* PCI functions
* ================================================================ */
#define ATI_PCIE_SNOOP_REG 0x42
#define ATI_PCIE_SNOOP_MASK 0xf8
#define ATI_PCIE_SNOOP_ENABLE 0x02
#define NVIDIA_PCIE_SNOOP_REG 0x4e
#define NVIDIA_PCIE_SNOOP_MASK 0xf0
#define NVIDIA_PCIE_SNOOP_ENABLE 0x0f
#define NVIDIA_HDA_ISTR_COH_REG 0x4d
#define NVIDIA_HDA_OSTR_COH_REG 0x4c
#define NVIDIA_HDA_STR_COH_ENABLE 0x01
#define INTEL_PCIE_NOSNOOP_REG 0x79
#define INTEL_PCIE_NOSNOOP_MASK 0xf7
#define INTEL_PCIE_NOSNOOP_ENABLE 0x08
uint8_t
azalia_pci_read(pci_chipset_tag_t pc, pcitag_t pa, int reg)
{
return (pci_conf_read(pc, pa, (reg & ~0x03)) >>
((reg & 0x03) * 8) & 0xff);
}
void
azalia_pci_write(pci_chipset_tag_t pc, pcitag_t pa, int reg, uint8_t val)
{
pcireg_t pcival;
pcival = pci_conf_read(pc, pa, (reg & ~0x03));
pcival &= ~(0xff << ((reg & 0x03) * 8));
pcival |= (val << ((reg & 0x03) * 8));
pci_conf_write(pc, pa, (reg & ~0x03), pcival);
}
void
azalia_configure_pci(azalia_t *az)
{
pcireg_t v;
uint8_t reg;
/* enable back-to-back */
v = pci_conf_read(az->pc, az->tag, PCI_COMMAND_STATUS_REG);
pci_conf_write(az->pc, az->tag, PCI_COMMAND_STATUS_REG,
v | PCI_COMMAND_BACKTOBACK_ENABLE);
/* traffic class select */
v = pci_conf_read(az->pc, az->tag, ICH_PCI_HDTCSEL);
pci_conf_write(az->pc, az->tag, ICH_PCI_HDTCSEL,
v & ~(ICH_PCI_HDTCSEL_MASK));
/* enable PCIe snoop */
switch (PCI_PRODUCT(az->pciid)) {
case PCI_PRODUCT_ATI_SB450_HDA:
case PCI_PRODUCT_ATI_SBX00_HDA:
case PCI_PRODUCT_AMD_HUDSON2_HDA:
reg = azalia_pci_read(az->pc, az->tag, ATI_PCIE_SNOOP_REG);
reg &= ATI_PCIE_SNOOP_MASK;
reg |= ATI_PCIE_SNOOP_ENABLE;
azalia_pci_write(az->pc, az->tag, ATI_PCIE_SNOOP_REG, reg);
break;
case PCI_PRODUCT_NVIDIA_MCP51_HDA:
case PCI_PRODUCT_NVIDIA_MCP55_HDA:
case PCI_PRODUCT_NVIDIA_MCP61_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP61_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP65_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP65_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP67_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP67_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP73_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP73_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP77_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP77_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP77_HDA_3:
case PCI_PRODUCT_NVIDIA_MCP77_HDA_4:
case PCI_PRODUCT_NVIDIA_MCP79_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP79_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP79_HDA_3:
case PCI_PRODUCT_NVIDIA_MCP79_HDA_4:
case PCI_PRODUCT_NVIDIA_MCP89_HDA_1:
case PCI_PRODUCT_NVIDIA_MCP89_HDA_2:
case PCI_PRODUCT_NVIDIA_MCP89_HDA_3:
case PCI_PRODUCT_NVIDIA_MCP89_HDA_4:
reg = azalia_pci_read(az->pc, az->tag,
NVIDIA_HDA_OSTR_COH_REG);
reg |= NVIDIA_HDA_STR_COH_ENABLE;
azalia_pci_write(az->pc, az->tag,
NVIDIA_HDA_OSTR_COH_REG, reg);
reg = azalia_pci_read(az->pc, az->tag,
NVIDIA_HDA_ISTR_COH_REG);
reg |= NVIDIA_HDA_STR_COH_ENABLE;
azalia_pci_write(az->pc, az->tag,
NVIDIA_HDA_ISTR_COH_REG, reg);
reg = azalia_pci_read(az->pc, az->tag,
NVIDIA_PCIE_SNOOP_REG);
reg &= NVIDIA_PCIE_SNOOP_MASK;
reg |= NVIDIA_PCIE_SNOOP_ENABLE;
azalia_pci_write(az->pc, az->tag,
NVIDIA_PCIE_SNOOP_REG, reg);
reg = azalia_pci_read(az->pc, az->tag,
NVIDIA_PCIE_SNOOP_REG);
if ((reg & NVIDIA_PCIE_SNOOP_ENABLE) !=
NVIDIA_PCIE_SNOOP_ENABLE) {
printf(": could not enable PCIe cache snooping!\n");
}
break;
case PCI_PRODUCT_INTEL_82801FB_HDA:
case PCI_PRODUCT_INTEL_82801GB_HDA:
case PCI_PRODUCT_INTEL_82801H_HDA:
case PCI_PRODUCT_INTEL_82801I_HDA:
case PCI_PRODUCT_INTEL_82801JI_HDA:
case PCI_PRODUCT_INTEL_82801JD_HDA:
case PCI_PRODUCT_INTEL_6321ESB_HDA:
case PCI_PRODUCT_INTEL_3400_HDA:
case PCI_PRODUCT_INTEL_QS57_HDA:
case PCI_PRODUCT_INTEL_6SERIES_HDA:
case PCI_PRODUCT_INTEL_7SERIES_HDA:
case PCI_PRODUCT_INTEL_8SERIES_HDA:
case PCI_PRODUCT_INTEL_8SERIES_LP_HDA:
case PCI_PRODUCT_INTEL_9SERIES_HDA:
case PCI_PRODUCT_INTEL_9SERIES_LP_HDA:
case PCI_PRODUCT_INTEL_BAYTRAIL_HDA:
case PCI_PRODUCT_INTEL_100SERIES_HDA:
case PCI_PRODUCT_INTEL_100SERIES_LP_HDA:
case PCI_PRODUCT_INTEL_C600_HDA:
case PCI_PRODUCT_INTEL_C610_HDA:
case PCI_PRODUCT_INTEL_BSW_HDA:
reg = azalia_pci_read(az->pc, az->tag,
INTEL_PCIE_NOSNOOP_REG);
reg &= INTEL_PCIE_NOSNOOP_MASK;
azalia_pci_write(az->pc, az->tag,
INTEL_PCIE_NOSNOOP_REG, reg);
break;
}
}
int
azalia_pci_match(struct device *parent, void *match, void *aux)
{
struct pci_attach_args *pa;
pa = aux;
if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MULTIMEDIA
&& PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MULTIMEDIA_HDAUDIO)
return 1;
return 0;
}
void
azalia_pci_attach(struct device *parent, struct device *self, void *aux)
{
azalia_t *sc;
struct pci_attach_args *pa;
pcireg_t v;
uint8_t reg;
pci_intr_handle_t ih;
const char *interrupt_str;
sc = (azalia_t*)self;
pa = aux;
sc->dmat = pa->pa_dmat;
pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
v = pci_conf_read(pa->pa_pc, pa->pa_tag, ICH_PCI_HDBARL);
v &= PCI_MAPREG_TYPE_MASK | PCI_MAPREG_MEM_TYPE_MASK;
if (pci_mapreg_map(pa, ICH_PCI_HDBARL, v, 0,
&sc->iot, &sc->ioh, NULL, &sc->map_size, 0)) {
printf(": can't map device i/o space\n");
return;
}
sc->pc = pa->pa_pc;
sc->tag = pa->pa_tag;
sc->pciid = pa->pa_id;
sc->subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
azalia_configure_pci(sc);
/* disable MSI, use INTx instead */
if (PCI_VENDOR(sc->pciid) == PCI_VENDOR_INTEL) {
reg = azalia_pci_read(sc->pc, sc->tag, ICH_PCI_MMC);
reg &= ~(ICH_PCI_MMC_ME);
azalia_pci_write(sc->pc, sc->tag, ICH_PCI_MMC, reg);
}
/* interrupt */
if (pci_intr_map_msi(pa, &ih) && pci_intr_map(pa, &ih)) {
printf(": can't map interrupt\n");
return;
}
interrupt_str = pci_intr_string(pa->pa_pc, ih);
sc->ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO | IPL_MPSAFE,
azalia_intr, sc, sc->dev.dv_xname);
if (sc->ih == NULL) {
printf(": can't establish interrupt");
if (interrupt_str != NULL)
printf(" at %s", interrupt_str);
printf("\n");
return;
}
printf(": %s\n", interrupt_str);
if (azalia_init(sc, 0))
goto err_exit;
if (azalia_init_codecs(sc))
goto err_exit;
if (azalia_init_streams(sc))
goto err_exit;
audio_attach_mi(&azalia_hw_if, sc, &sc->dev);
return;
err_exit:
sc->detached = 1;
azalia_pci_detach(self, 0);
}
int
azalia_pci_activate(struct device *self, int act)
{
azalia_t *sc = (azalia_t*)self;
int rv = 0;
switch (act) {
case DVACT_SUSPEND:
azalia_suspend(sc);
break;
case DVACT_POWERDOWN:
azalia_shutdown(sc);
break;
case DVACT_RESUME:
azalia_resume(sc);
rv = config_activate_children(self, act);
break;
default:
rv = config_activate_children(self, act);
break;
}
return (rv);
}
int
azalia_pci_detach(struct device *self, int flags)
{
azalia_t *az = (azalia_t*)self;
uint32_t gctl;
int i;
DPRINTF(("%s\n", __func__));
/*
* this function is called if the device could not be supported,
* in which case az->detached == 1. check if this function has
* already cleaned up.
*/
if (az->detached > 1)
return 0;
config_detach_children(self, flags);
/* disable unsolicited responses if soft detaching */
if (az->detached == 1) {
gctl = AZ_READ_4(az, GCTL);
AZ_WRITE_4(az, GCTL, gctl &~(HDA_GCTL_UNSOL));
}
timeout_del(&az->unsol_to);
DPRINTF(("%s: delete streams\n", __func__));
if (az->rstream.bdlist.addr != NULL)
azalia_free_dmamem(az, &az->rstream.bdlist);
if (az->pstream.bdlist.addr != NULL)
azalia_free_dmamem(az, &az->pstream.bdlist);
DPRINTF(("%s: delete codecs\n", __func__));
for (i = 0; i < az->ncodecs; i++) {
azalia_codec_delete(&az->codecs[i]);
}
az->ncodecs = 0;
if (az->codecs != NULL) {
free(az->codecs, M_DEVBUF, 0);
az->codecs = NULL;
}
DPRINTF(("%s: delete CORB and RIRB\n", __func__));
if (az->corb_dma.addr != NULL)
azalia_free_dmamem(az, &az->corb_dma);
if (az->rirb_dma.addr != NULL)
azalia_free_dmamem(az, &az->rirb_dma);
if (az->unsolq != NULL) {
free(az->unsolq, M_DEVBUF, 0);
az->unsolq = NULL;
}
/* disable interrupts if soft detaching */
if (az->detached == 1) {
DPRINTF(("%s: disable interrupts\n", __func__));
AZ_WRITE_4(az, INTCTL, 0);
DPRINTF(("%s: clear interrupts\n", __func__));
AZ_WRITE_4(az, INTSTS, HDA_INTSTS_CIS | HDA_INTSTS_GIS);
AZ_WRITE_2(az, STATESTS, HDA_STATESTS_SDIWAKE);
AZ_WRITE_1(az, RIRBSTS, HDA_RIRBSTS_RINTFL | HDA_RIRBSTS_RIRBOIS);
}
DPRINTF(("%s: delete PCI resources\n", __func__));
if (az->ih != NULL) {
pci_intr_disestablish(az->pc, az->ih);
az->ih = NULL;
}
if (az->map_size != 0) {
bus_space_unmap(az->iot, az->ioh, az->map_size);
az->map_size = 0;
}
az->detached = 2;
return 0;
}
int
azalia_intr(void *v)
{
azalia_t *az = v;
uint32_t intsts;
int ret = 0;
mtx_enter(&audio_lock);
intsts = AZ_READ_4(az, INTSTS);
if (intsts == 0 || intsts == 0xffffffff) {
mtx_leave(&audio_lock);
return (ret);
}
AZ_WRITE_4(az, INTSTS, intsts);
if (intsts & az->pstream.intr_bit) {
azalia_stream_intr(&az->pstream);
ret = 1;
}
if (intsts & az->rstream.intr_bit) {
azalia_stream_intr(&az->rstream);
ret = 1;
}
if ((intsts & HDA_INTSTS_CIS) &&
(AZ_READ_1(az, RIRBCTL) & HDA_RIRBCTL_RINTCTL) &&
(AZ_READ_1(az, RIRBSTS) & HDA_RIRBSTS_RINTFL)) {
azalia_rirb_intr(az);
ret = 1;
}
mtx_leave(&audio_lock);
return (ret);
}
void
azalia_shutdown(void *v)
{
azalia_t *az = (azalia_t *)v;
uint32_t gctl;
/* disable unsolicited response */
gctl = AZ_READ_4(az, GCTL);
AZ_WRITE_4(az, GCTL, gctl & ~(HDA_GCTL_UNSOL));
timeout_del(&az->unsol_to);
/* halt CORB/RIRB */
azalia_halt_corb(az);
azalia_halt_rirb(az);
}
/* ================================================================
* HDA controller functions
* ================================================================ */
void
azalia_print_codec(codec_t *codec)
{
const char *vendor;
if (codec->name == NULL) {
vendor = pci_findvendor(codec->vid >> 16);
if (vendor == NULL)
printf("0x%04x/0x%04x",
codec->vid >> 16, codec->vid & 0xffff);
else
printf("%s/0x%04x", vendor, codec->vid & 0xffff);
} else
printf("%s", codec->name);
}
int
azalia_reset(azalia_t *az)
{
uint32_t gctl;
int i;
/* 4.2.2 Starting the High Definition Audio Controller */
DPRINTF(("%s: resetting\n", __func__));
gctl = AZ_READ_4(az, GCTL);
AZ_WRITE_4(az, GCTL, gctl & ~HDA_GCTL_CRST);
for (i = 5000; i > 0; i--) {
DELAY(10);
if ((AZ_READ_4(az, GCTL) & HDA_GCTL_CRST) == 0)
break;
}
DPRINTF(("%s: reset counter = %d\n", __func__, i));
if (i == 0) {
DPRINTF(("%s: reset failure\n", XNAME(az)));
return(ETIMEDOUT);
}
DELAY(1000);
gctl = AZ_READ_4(az, GCTL);
AZ_WRITE_4(az, GCTL, gctl | HDA_GCTL_CRST);
for (i = 5000; i > 0; i--) {
DELAY(10);
if (AZ_READ_4(az, GCTL) & HDA_GCTL_CRST)
break;
}
DPRINTF(("%s: reset counter = %d\n", __func__, i));
if (i == 0) {
DPRINTF(("%s: reset-exit failure\n", XNAME(az)));
return(ETIMEDOUT);
}
DELAY(1000);
return(0);
}
int
azalia_get_ctrlr_caps(azalia_t *az)
{
int i, n;
uint16_t gcap;
uint16_t statests;
uint8_t cap;
DPRINTF(("%s: host: High Definition Audio rev. %d.%d\n",
XNAME(az), AZ_READ_1(az, VMAJ), AZ_READ_1(az, VMIN)));
gcap = AZ_READ_2(az, GCAP);
az->nistreams = HDA_GCAP_ISS(gcap);
az->nostreams = HDA_GCAP_OSS(gcap);
az->nbstreams = HDA_GCAP_BSS(gcap);
az->ok64 = (gcap & HDA_GCAP_64OK) != 0;
DPRINTF(("%s: host: %d output, %d input, and %d bidi streams\n",
XNAME(az), az->nostreams, az->nistreams, az->nbstreams));
/* 4.3 Codec discovery */
statests = AZ_READ_2(az, STATESTS);
for (i = 0, n = 0; i < HDA_MAX_CODECS; i++) {
if ((statests >> i) & 1) {
DPRINTF(("%s: found a codec at #%d\n", XNAME(az), i));
n++;
}
}
az->ncodecs = n;
if (az->ncodecs < 1) {
printf("%s: no HD-Audio codecs\n", XNAME(az));
return -1;
}
az->codecs = mallocarray(az->ncodecs, sizeof(codec_t), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (az->codecs == NULL) {
printf("%s: can't allocate memory for codecs\n", XNAME(az));
return ENOMEM;
}
for (i = 0, n = 0; n < az->ncodecs; i++) {
if ((statests >> i) & 1) {
az->codecs[n].address = i;
az->codecs[n++].az = az;
}
}
/* determine CORB size */
az->corbsize = AZ_READ_1(az, CORBSIZE);
cap = az->corbsize & HDA_CORBSIZE_CORBSZCAP_MASK;
az->corbsize &= ~HDA_CORBSIZE_CORBSIZE_MASK;
if (cap & HDA_CORBSIZE_CORBSZCAP_256) {
az->corb_entries = 256;
az->corbsize |= HDA_CORBSIZE_CORBSIZE_256;
} else if (cap & HDA_CORBSIZE_CORBSZCAP_16) {
az->corb_entries = 16;
az->corbsize |= HDA_CORBSIZE_CORBSIZE_16;
} else if (cap & HDA_CORBSIZE_CORBSZCAP_2) {
az->corb_entries = 2;
az->corbsize |= HDA_CORBSIZE_CORBSIZE_2;
} else {
printf("%s: invalid CORBSZCAP: 0x%2x\n", XNAME(az), cap);
return(-1);
}
/* determine RIRB size */
az->rirbsize = AZ_READ_1(az, RIRBSIZE);
cap = az->rirbsize & HDA_RIRBSIZE_RIRBSZCAP_MASK;
az->rirbsize &= ~HDA_RIRBSIZE_RIRBSIZE_MASK;
if (cap & HDA_RIRBSIZE_RIRBSZCAP_256) {
az->rirb_entries = 256;
az->rirbsize |= HDA_RIRBSIZE_RIRBSIZE_256;
} else if (cap & HDA_RIRBSIZE_RIRBSZCAP_16) {
az->rirb_entries = 16;
az->rirbsize |= HDA_RIRBSIZE_RIRBSIZE_16;
} else if (cap & HDA_RIRBSIZE_RIRBSZCAP_2) {
az->rirb_entries = 2;
az->rirbsize |= HDA_RIRBSIZE_RIRBSIZE_2;
} else {
printf("%s: invalid RIRBSZCAP: 0x%2x\n", XNAME(az), cap);
return(-1);
}
return(0);
}
int
azalia_init(azalia_t *az, int resuming)
{
int err;
err = azalia_reset(az);
if (err)
return(err);
if (!resuming) {
err = azalia_get_ctrlr_caps(az);
if (err)
return(err);
}
/* clear interrupt status */
AZ_WRITE_2(az, STATESTS, HDA_STATESTS_SDIWAKE);
AZ_WRITE_1(az, RIRBSTS, HDA_RIRBSTS_RINTFL | HDA_RIRBSTS_RIRBOIS);
AZ_WRITE_4(az, INTSTS, HDA_INTSTS_CIS | HDA_INTSTS_GIS);
AZ_WRITE_4(az, DPLBASE, 0);
AZ_WRITE_4(az, DPUBASE, 0);
/* 4.4.1 Command Outbound Ring Buffer */
err = azalia_init_corb(az, resuming);
if (err)
return(err);
/* 4.4.2 Response Inbound Ring Buffer */
err = azalia_init_rirb(az, resuming);
if (err)
return(err);
AZ_WRITE_4(az, INTCTL,
AZ_READ_4(az, INTCTL) | HDA_INTCTL_CIE | HDA_INTCTL_GIE);
return(0);
}
int
azalia_init_codecs(azalia_t *az)
{
codec_t *codec;
int c, i;
c = 0;
for (i = 0; i < az->ncodecs; i++) {
if (!azalia_codec_init(&az->codecs[i]))
c++;
}
if (c == 0) {
printf("%s: No codecs found\n", XNAME(az));
return(1);
}
/* Use the first codec capable of analog I/O. If there are none,
* use the first codec capable of digital I/O. Skip HDMI codecs.
*/
c = -1;
for (i = 0; i < az->ncodecs; i++) {
codec = &az->codecs[i];
if ((codec->audiofunc < 0) ||
(codec->codec_type == AZ_CODEC_TYPE_HDMI))
continue;
if (codec->codec_type == AZ_CODEC_TYPE_DIGITAL) {
if (c < 0)
c = i;
} else {
c = i;
break;
}
}
az->codecno = c;
if (az->codecno < 0) {
printf("%s: no supported codecs\n", XNAME(az));
return(1);
}
printf("%s: codecs: ", XNAME(az));
for (i = 0; i < az->ncodecs; i++) {
azalia_print_codec(&az->codecs[i]);
if (i < az->ncodecs - 1)
printf(", ");
}
if (az->ncodecs > 1) {
printf(", using ");
azalia_print_codec(&az->codecs[az->codecno]);
}
printf("\n");
/* All codecs with audio are enabled, but only one will be used. */
for (i = 0; i < az->ncodecs; i++) {
codec = &az->codecs[i];
if (i != az->codecno) {
if (codec->audiofunc < 0)
continue;
azalia_comresp(codec, codec->audiofunc,
CORB_SET_POWER_STATE, CORB_PS_D3, NULL);
DELAY(100);
azalia_codec_delete(codec);
}
}
/* Enable unsolicited responses now that az->codecno is set. */
AZ_WRITE_4(az, GCTL, AZ_READ_4(az, GCTL) | HDA_GCTL_UNSOL);
return(0);
}
int
azalia_init_streams(azalia_t *az)
{
int err;
/* Use stream#1 and #2. Don't use stream#0. */
err = azalia_stream_init(&az->pstream, az, az->nistreams + 0,
1, AUMODE_PLAY);
if (err)
return(err);
err = azalia_stream_init(&az->rstream, az, 0, 2, AUMODE_RECORD);
if (err)
return(err);
return(0);
}
int
azalia_halt_corb(azalia_t *az)
{
uint8_t corbctl;
int i;
corbctl = AZ_READ_1(az, CORBCTL);
if (corbctl & HDA_CORBCTL_CORBRUN) { /* running? */
AZ_WRITE_1(az, CORBCTL, corbctl & ~HDA_CORBCTL_CORBRUN);
for (i = 5000; i > 0; i--) {
DELAY(10);
corbctl = AZ_READ_1(az, CORBCTL);
if ((corbctl & HDA_CORBCTL_CORBRUN) == 0)
break;
}
if (i == 0) {
DPRINTF(("%s: CORB is running\n", XNAME(az)));
return EBUSY;
}
}
return(0);
}
int
azalia_init_corb(azalia_t *az, int resuming)
{
int err, i;
uint16_t corbrp, corbwp;
uint8_t corbctl;
err = azalia_halt_corb(az);
if (err)
return(err);
if (!resuming) {
err = azalia_alloc_dmamem(az,
az->corb_entries * sizeof(corb_entry_t), 128,
&az->corb_dma);
if (err) {
printf("%s: can't allocate CORB buffer\n", XNAME(az));
return(err);
}
DPRINTF(("%s: CORB allocation succeeded.\n", __func__));
}
timeout_set(&az->unsol_to, azalia_rirb_kick_unsol_events, az);
AZ_WRITE_4(az, CORBLBASE, (uint32_t)AZALIA_DMA_DMAADDR(&az->corb_dma));
AZ_WRITE_4(az, CORBUBASE, PTR_UPPER32(AZALIA_DMA_DMAADDR(&az->corb_dma)));
AZ_WRITE_1(az, CORBSIZE, az->corbsize);
/* reset CORBRP */
corbrp = AZ_READ_2(az, CORBRP);
AZ_WRITE_2(az, CORBRP, corbrp | HDA_CORBRP_CORBRPRST);
AZ_WRITE_2(az, CORBRP, corbrp & ~HDA_CORBRP_CORBRPRST);
for (i = 5000; i > 0; i--) {
DELAY(10);
corbrp = AZ_READ_2(az, CORBRP);
if ((corbrp & HDA_CORBRP_CORBRPRST) == 0)
break;
}
if (i == 0) {
DPRINTF(("%s: CORBRP reset failure\n", XNAME(az)));
return -1;
}
DPRINTF(("%s: CORBWP=%d; size=%d\n", __func__,
AZ_READ_2(az, CORBRP) & HDA_CORBRP_CORBRP, az->corb_entries));
/* clear CORBWP */
corbwp = AZ_READ_2(az, CORBWP);
AZ_WRITE_2(az, CORBWP, corbwp & ~HDA_CORBWP_CORBWP);
/* Run! */
corbctl = AZ_READ_1(az, CORBCTL);
AZ_WRITE_1(az, CORBCTL, corbctl | HDA_CORBCTL_CORBRUN);
return 0;
}
int
azalia_halt_rirb(azalia_t *az)
{
int i;
uint8_t rirbctl;
rirbctl = AZ_READ_1(az, RIRBCTL);
if (rirbctl & HDA_RIRBCTL_RIRBDMAEN) { /* running? */
AZ_WRITE_1(az, RIRBCTL, rirbctl & ~HDA_RIRBCTL_RIRBDMAEN);
for (i = 5000; i > 0; i--) {
DELAY(10);
rirbctl = AZ_READ_1(az, RIRBCTL);
if ((rirbctl & HDA_RIRBCTL_RIRBDMAEN) == 0)
break;
}
if (i == 0) {
DPRINTF(("%s: RIRB is running\n", XNAME(az)));
return(EBUSY);
}
}
return(0);
}
int
azalia_init_rirb(azalia_t *az, int resuming)
{
int err, i;
uint16_t rirbwp;
uint8_t rirbctl;
err = azalia_halt_rirb(az);
if (err)
return(err);
if (!resuming) {
err = azalia_alloc_dmamem(az,
az->rirb_entries * sizeof(rirb_entry_t), 128,
&az->rirb_dma);
if (err) {
printf("%s: can't allocate RIRB buffer\n", XNAME(az));
return err;
}
DPRINTF(("%s: RIRB allocation succeeded.\n", __func__));
/* setup the unsolicited response queue */
az->unsolq = malloc(sizeof(rirb_entry_t) * UNSOLQ_SIZE,
M_DEVBUF, M_NOWAIT | M_ZERO);
if (az->unsolq == NULL) {
DPRINTF(("%s: can't allocate unsolicited response queue.\n",
XNAME(az)));
azalia_free_dmamem(az, &az->rirb_dma);
return ENOMEM;
}
}
AZ_WRITE_4(az, RIRBLBASE, (uint32_t)AZALIA_DMA_DMAADDR(&az->rirb_dma));
AZ_WRITE_4(az, RIRBUBASE, PTR_UPPER32(AZALIA_DMA_DMAADDR(&az->rirb_dma)));
AZ_WRITE_1(az, RIRBSIZE, az->rirbsize);
/* reset the write pointer */
rirbwp = AZ_READ_2(az, RIRBWP);
AZ_WRITE_2(az, RIRBWP, rirbwp | HDA_RIRBWP_RIRBWPRST);
/* clear the read pointer */
az->rirb_rp = AZ_READ_2(az, RIRBWP) & HDA_RIRBWP_RIRBWP;
DPRINTF(("%s: RIRBRP=%d, size=%d\n", __func__, az->rirb_rp,
az->rirb_entries));
az->unsolq_rp = 0;
az->unsolq_wp = 0;
az->unsolq_kick = 0;
AZ_WRITE_2(az, RINTCNT, 1);
/* Run! */
rirbctl = AZ_READ_1(az, RIRBCTL);
AZ_WRITE_1(az, RIRBCTL, rirbctl |
HDA_RIRBCTL_RIRBDMAEN | HDA_RIRBCTL_RINTCTL);
for (i = 5000; i > 0; i--) {
DELAY(10);
rirbctl = AZ_READ_1(az, RIRBCTL);
if (rirbctl & HDA_RIRBCTL_RIRBDMAEN)
break;
}
if (i == 0) {
DPRINTF(("%s: RIRB is not running\n", XNAME(az)));
return(EBUSY);
}
return (0);
}
int
azalia_comresp(const codec_t *codec, nid_t nid, uint32_t control,
uint32_t param, uint32_t* result)
{
int err;
mtx_enter(&audio_lock);
err = azalia_set_command(codec->az, codec->address, nid, control,
param);
if (err)
goto exit;
err = azalia_get_response(codec->az, result);
exit:
mtx_leave(&audio_lock);
return(err);
}
int
azalia_set_command(azalia_t *az, int caddr, nid_t nid, uint32_t control,
uint32_t param)
{
corb_entry_t *corb;
int wp;
uint32_t verb;
uint16_t corbwp;
if ((AZ_READ_1(az, CORBCTL) & HDA_CORBCTL_CORBRUN) == 0) {
printf("%s: CORB is not running.\n", XNAME(az));
return(-1);
}
verb = (caddr << 28) | (nid << 20) | (control << 8) | param;
corbwp = AZ_READ_2(az, CORBWP);
wp = corbwp & HDA_CORBWP_CORBWP;
corb = (corb_entry_t*)az->corb_dma.addr;
if (++wp >= az->corb_entries)
wp = 0;
corb[wp] = verb;
AZ_WRITE_2(az, CORBWP, (corbwp & ~HDA_CORBWP_CORBWP) | wp);
return(0);
}
int
azalia_get_response(azalia_t *az, uint32_t *result)
{
const rirb_entry_t *rirb;
int i;
uint16_t wp;
if ((AZ_READ_1(az, RIRBCTL) & HDA_RIRBCTL_RIRBDMAEN) == 0) {
printf("%s: RIRB is not running.\n", XNAME(az));
return(-1);
}
rirb = (rirb_entry_t*)az->rirb_dma.addr;
i = 5000;
for (;;) {
while (i > 0) {
wp = AZ_READ_2(az, RIRBWP) & HDA_RIRBWP_RIRBWP;
if (az->rirb_rp != wp)
break;
DELAY(10);
i--;
}
if (i == 0) {
DPRINTF(("%s: RIRB time out\n", XNAME(az)));
return(ETIMEDOUT);
}
if (++az->rirb_rp >= az->rirb_entries)
az->rirb_rp = 0;
if (rirb[az->rirb_rp].resp_ex & RIRB_RESP_UNSOL) {
az->unsolq[az->unsolq_wp].resp = rirb[az->rirb_rp].resp;
az->unsolq[az->unsolq_wp++].resp_ex = rirb[az->rirb_rp].resp_ex;
az->unsolq_wp %= UNSOLQ_SIZE;
} else
break;
}
if (result != NULL)
*result = rirb[az->rirb_rp].resp;
return(0);
}
void
azalia_rirb_kick_unsol_events(void *v)
{
azalia_t *az = v;
int addr, tag;
if (az->unsolq_kick)
return;
az->unsolq_kick = 1;
while (az->unsolq_rp != az->unsolq_wp) {
addr = RIRB_RESP_CODEC(az->unsolq[az->unsolq_rp].resp_ex);
tag = RIRB_UNSOL_TAG(az->unsolq[az->unsolq_rp].resp);
DPRINTF(("%s: codec address=%d tag=%d\n", __func__, addr, tag));
az->unsolq_rp++;
az->unsolq_rp %= UNSOLQ_SIZE;
/* We only care about events on the using codec. */
if (az->codecs[az->codecno].address == addr)
azalia_unsol_event(&az->codecs[az->codecno], tag);
}
az->unsolq_kick = 0;
}
void
azalia_rirb_intr(azalia_t *az)
{
const rirb_entry_t *rirb;
uint16_t wp;
uint8_t rirbsts;
rirbsts = AZ_READ_1(az, RIRBSTS);
wp = AZ_READ_2(az, RIRBWP) & HDA_RIRBWP_RIRBWP;
rirb = (rirb_entry_t*)az->rirb_dma.addr;
while (az->rirb_rp != wp) {
if (++az->rirb_rp >= az->rirb_entries)
az->rirb_rp = 0;
if (rirb[az->rirb_rp].resp_ex & RIRB_RESP_UNSOL) {
az->unsolq[az->unsolq_wp].resp = rirb[az->rirb_rp].resp;
az->unsolq[az->unsolq_wp++].resp_ex = rirb[az->rirb_rp].resp_ex;
az->unsolq_wp %= UNSOLQ_SIZE;
} else {
DPRINTF(("%s: dropped solicited response\n", __func__));
}
}
timeout_add_msec(&az->unsol_to, 1);
AZ_WRITE_1(az, RIRBSTS,
rirbsts | HDA_RIRBSTS_RIRBOIS | HDA_RIRBSTS_RINTFL);
}
int
azalia_alloc_dmamem(azalia_t *az, size_t size, size_t align, azalia_dma_t *d)
{
int err;
int nsegs;
d->size = size;
err = bus_dmamem_alloc(az->dmat, size, align, 0, d->segments, 1,
&nsegs, BUS_DMA_NOWAIT);
if (err)
return err;
if (nsegs != 1)
goto free;
err = bus_dmamem_map(az->dmat, d->segments, 1, size,
&d->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
if (err)
goto free;
err = bus_dmamap_create(az->dmat, size, 1, size, 0,
BUS_DMA_NOWAIT, &d->map);
if (err)
goto unmap;
err = bus_dmamap_load(az->dmat, d->map, d->addr, size,
NULL, BUS_DMA_NOWAIT);
if (err)
goto destroy;
if (!az->ok64 && PTR_UPPER32(AZALIA_DMA_DMAADDR(d)) != 0) {
azalia_free_dmamem(az, d);
return -1;
}
return 0;
destroy:
bus_dmamap_destroy(az->dmat, d->map);
unmap:
bus_dmamem_unmap(az->dmat, d->addr, size);
free:
bus_dmamem_free(az->dmat, d->segments, 1);
d->addr = NULL;
return err;
}
int
azalia_free_dmamem(const azalia_t *az, azalia_dma_t* d)
{
if (d->addr == NULL)
return 0;
bus_dmamap_unload(az->dmat, d->map);
bus_dmamap_destroy(az->dmat, d->map);
bus_dmamem_unmap(az->dmat, d->addr, d->size);
bus_dmamem_free(az->dmat, d->segments, 1);
d->addr = NULL;
return 0;
}
int
azalia_suspend(azalia_t *az)
{
int err;
if (az->detached)
return 0;
/* disable unsolicited responses */
AZ_WRITE_4(az, GCTL, AZ_READ_4(az, GCTL) & ~HDA_GCTL_UNSOL);
timeout_del(&az->unsol_to);
azalia_save_mixer(&az->codecs[az->codecno]);
/* azalia_halt_{corb,rirb}() only fail if the {CORB,RIRB} can't
* be stopped and azalia_init_{corb,rirb}(), which starts the
* {CORB,RIRB}, first calls azalia_halt_{corb,rirb}(). If halt
* fails, don't try to restart.
*/
err = azalia_halt_corb(az);
if (err)
goto corb_fail;
err = azalia_halt_rirb(az);
if (err)
goto rirb_fail;
/* stop interrupts and clear status registers */
AZ_WRITE_4(az, INTCTL, 0);
AZ_WRITE_4(az, INTSTS, HDA_INTSTS_CIS | HDA_INTSTS_GIS);
AZ_WRITE_2(az, STATESTS, HDA_STATESTS_SDIWAKE);
AZ_WRITE_1(az, RIRBSTS, HDA_RIRBSTS_RINTFL | HDA_RIRBSTS_RIRBOIS);
return 0;
rirb_fail:
azalia_init_corb(az, 1);
corb_fail:
AZ_WRITE_4(az, GCTL, AZ_READ_4(az, GCTL) | HDA_GCTL_UNSOL);
return err;
}
int
azalia_resume_codec(codec_t *this)
{
widget_t *w;
uint32_t result;
int i, err;
err = azalia_comresp(this, this->audiofunc, CORB_SET_POWER_STATE,
CORB_PS_D0, &result);
if (err) {
DPRINTF(("%s: power audio func error: result=0x%8.8x\n",
__func__, result));
}
DELAY(100);
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (w->widgetcap & COP_AWCAP_POWER) {
azalia_comresp(this, w->nid, CORB_SET_POWER_STATE,
CORB_PS_D0, &result);
DELAY(100);
}
if (w->type == COP_AWTYPE_PIN_COMPLEX)
azalia_widget_init_pin(w, this);
if (this->qrks & AZ_QRK_WID_MASK)
azalia_codec_widget_quirks(this, w->nid);
}
if (this->qrks & AZ_QRK_GPIO_MASK) {
err = azalia_codec_gpio_quirks(this);
if (err)
return err;
}
azalia_restore_mixer(this);
return(0);
}
int
azalia_resume(azalia_t *az)
{
int err;
if (az->detached)
return 0;
azalia_configure_pci(az);
/* is this necessary? */
pci_conf_write(az->pc, az->tag, PCI_SUBSYS_ID_REG, az->subid);
err = azalia_init(az, 1);
if (err)
return err;
/* enable unsolicited responses on the controller */
AZ_WRITE_4(az, GCTL, AZ_READ_4(az, GCTL) | HDA_GCTL_UNSOL);
err = azalia_resume_codec(&az->codecs[az->codecno]);
if (err)
return err;
err = azalia_codec_enable_unsol(&az->codecs[az->codecno]);
if (err)
return err;
return 0;
}
void
azalia_save_mixer(codec_t *this)
{
mixer_item_t *m;
mixer_ctrl_t mc;
int i;
for (i = 0; i < this->nmixers; i++) {
m = &this->mixers[i];
if (m->nid == this->playvols.master)
continue;
mc.dev = i;
mc.type = m->devinfo.type;
azalia_mixer_get(this, m->nid, m->target, &mc);
switch (mc.type) {
case AUDIO_MIXER_ENUM:
m->saved.ord = mc.un.ord;
break;
case AUDIO_MIXER_SET:
m->saved.mask = mc.un.mask;
break;
case AUDIO_MIXER_VALUE:
m->saved.value = mc.un.value;
break;
case AUDIO_MIXER_CLASS:
break;
default:
DPRINTF(("%s: invalid mixer type in mixer %d\n",
__func__, mc.dev));
break;
}
}
}
void
azalia_restore_mixer(codec_t *this)
{
mixer_item_t *m;
mixer_ctrl_t mc;
int i;
for (i = 0; i < this->nmixers; i++) {
m = &this->mixers[i];
if (m->nid == this->playvols.master)
continue;
mc.dev = i;
mc.type = m->devinfo.type;
switch (mc.type) {
case AUDIO_MIXER_ENUM:
mc.un.ord = m->saved.ord;
break;
case AUDIO_MIXER_SET:
mc.un.mask = m->saved.mask;
break;
case AUDIO_MIXER_VALUE:
mc.un.value = m->saved.value;
break;
case AUDIO_MIXER_CLASS:
break;
default:
DPRINTF(("%s: invalid mixer type in mixer %d\n",
__func__, mc.dev));
continue;
}
azalia_mixer_set(this, m->nid, m->target, &mc);
}
}
/* ================================================================
* HDA codec functions
* ================================================================ */
int
azalia_codec_init(codec_t *this)
{
widget_t *w;
uint32_t rev, id, result;
int err, addr, n, i, nspdif, nhdmi;
addr = this->address;
/* codec vendor/device/revision */
err = azalia_comresp(this, CORB_NID_ROOT, CORB_GET_PARAMETER,
COP_REVISION_ID, &rev);
if (err)
return err;
err = azalia_comresp(this, CORB_NID_ROOT, CORB_GET_PARAMETER,
COP_VENDOR_ID, &id);
if (err)
return err;
this->vid = id;
this->subid = this->az->subid;
azalia_codec_init_vtbl(this);
DPRINTF(("%s: codec[%d] vid 0x%8.8x, subid 0x%8.8x, rev. %u.%u,",
XNAME(this->az), addr, this->vid, this->subid,
COP_RID_REVISION(rev), COP_RID_STEPPING(rev)));
DPRINTF((" HDA version %u.%u\n",
COP_RID_MAJ(rev), COP_RID_MIN(rev)));
/* identify function nodes */
err = azalia_comresp(this, CORB_NID_ROOT, CORB_GET_PARAMETER,
COP_SUBORDINATE_NODE_COUNT, &result);
if (err)
return err;
this->nfunctions = COP_NSUBNODES(result);
if (COP_NSUBNODES(result) <= 0) {
DPRINTF(("%s: codec[%d]: No function groups\n",
XNAME(this->az), addr));
return -1;
}
/* iterate function nodes and find an audio function */
n = COP_START_NID(result);
DPRINTF(("%s: nidstart=%d #functions=%d\n",
XNAME(this->az), n, this->nfunctions));
this->audiofunc = -1;
for (i = 0; i < this->nfunctions; i++) {
err = azalia_comresp(this, n + i, CORB_GET_PARAMETER,
COP_FUNCTION_GROUP_TYPE, &result);
if (err)
continue;
DPRINTF(("%s: FTYPE result = 0x%8.8x\n", __func__, result));
if (COP_FTYPE(result) == COP_FTYPE_AUDIO) {
this->audiofunc = n + i;
break; /* XXX multiple audio functions? */
}
}
if (this->audiofunc < 0) {
DPRINTF(("%s: codec[%d]: No audio function groups\n",
XNAME(this->az), addr));
return -1;
}
/* power the audio function */
azalia_comresp(this, this->audiofunc, CORB_SET_POWER_STATE,
CORB_PS_D0, &result);
DELAY(100);
/* check widgets in the audio function */
err = azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_SUBORDINATE_NODE_COUNT, &result);
if (err)
return err;
DPRINTF(("%s: There are %d widgets in the audio function.\n",
__func__, COP_NSUBNODES(result)));
this->wstart = COP_START_NID(result);
if (this->wstart < 2) {
printf("%s: invalid node structure\n", XNAME(this->az));
return -1;
}
this->wend = this->wstart + COP_NSUBNODES(result);
this->w = mallocarray(this->wend, sizeof(widget_t), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (this->w == NULL) {
printf("%s: out of memory\n", XNAME(this->az));
return ENOMEM;
}
/* query the base parameters */
azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_STREAM_FORMATS, &result);
this->w[this->audiofunc].d.audio.encodings = result;
azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_PCM, &result);
this->w[this->audiofunc].d.audio.bits_rates = result;
azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_INPUT_AMPCAP, &result);
this->w[this->audiofunc].inamp_cap = result;
azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_OUTPUT_AMPCAP, &result);
this->w[this->audiofunc].outamp_cap = result;
azalia_codec_print_audiofunc(this);
strlcpy(this->w[CORB_NID_ROOT].name, "root",
sizeof(this->w[CORB_NID_ROOT].name));
strlcpy(this->w[this->audiofunc].name, "hdaudio",
sizeof(this->w[this->audiofunc].name));
this->w[this->audiofunc].enable = 1;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
err = azalia_widget_init(w, this, i);
if (err)
return err;
err = azalia_widget_init_connection(w, this);
if (err)
return err;
azalia_widget_print_widget(w, this);
if (this->qrks & AZ_QRK_WID_MASK) {
azalia_codec_widget_quirks(this, i);
}
}
this->na_dacs = this->na_dacs_d = 0;
this->na_adcs = this->na_adcs_d = 0;
this->speaker = this->speaker2 = this->spkr_dac =
this->fhp = this->fhp_dac =
this->mic = this->mic_adc = -1;
this->nsense_pins = 0;
this->nout_jacks = 0;
nspdif = nhdmi = 0;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (!w->enable)
continue;
switch (w->type) {
case COP_AWTYPE_AUDIO_MIXER:
case COP_AWTYPE_AUDIO_SELECTOR:
if (!azalia_widget_check_conn(this, i, 0))
w->enable = 0;
break;
case COP_AWTYPE_AUDIO_OUTPUT:
if ((w->widgetcap & COP_AWCAP_DIGITAL) == 0) {
if (this->na_dacs < HDA_MAX_CHANNELS)
this->a_dacs[this->na_dacs++] = i;
} else {
if (this->na_dacs_d < HDA_MAX_CHANNELS)
this->a_dacs_d[this->na_dacs_d++] = i;
}
break;
case COP_AWTYPE_AUDIO_INPUT:
if ((w->widgetcap & COP_AWCAP_DIGITAL) == 0) {
if (this->na_adcs < HDA_MAX_CHANNELS)
this->a_adcs[this->na_adcs++] = i;
} else {
if (this->na_adcs_d < HDA_MAX_CHANNELS)
this->a_adcs_d[this->na_adcs_d++] = i;
}
break;
case COP_AWTYPE_PIN_COMPLEX:
switch (CORB_CD_PORT(w->d.pin.config)) {
case CORB_CD_FIXED:
switch (w->d.pin.device) {
case CORB_CD_SPEAKER:
if (this->speaker == -1) {
this->speaker = i;
} else if (w->d.pin.association <
this->w[this->speaker].d.pin.association ||
(w->d.pin.association ==
this->w[this->speaker].d.pin.association &&
w->d.pin.sequence <
this->w[this->speaker].d.pin.sequence)) {
this->speaker2 = this->speaker;
this->speaker = i;
} else {
this->speaker2 = i;
}
if (this->speaker == i)
this->spkr_dac =
azalia_codec_find_defdac(this, i, 0);
break;
case CORB_CD_MICIN:
this->mic = i;
this->mic_adc =
azalia_codec_find_defadc(this, i, 0);
break;
}
break;
case CORB_CD_JACK:
if (w->d.pin.device == CORB_CD_LINEOUT)
this->nout_jacks++;
else if (w->d.pin.device == CORB_CD_HEADPHONE &&
CORB_CD_LOC_GEO(w->d.pin.config) ==
CORB_CD_FRONT) {
this->fhp = i;
this->fhp_dac =
azalia_codec_find_defdac(this, i, 0);
}
if (this->nsense_pins >= HDA_MAX_SENSE_PINS ||
!(w->d.pin.cap & COP_PINCAP_PRESENCE))
break;
/* check override bit */
err = azalia_comresp(this, i,
CORB_GET_CONFIGURATION_DEFAULT, 0, &result);
if (err)
break;
if (!(CORB_CD_MISC(result) & CORB_CD_PRESENCEOV)) {
this->sense_pins[this->nsense_pins++] = i;
}
break;
}
if ((w->d.pin.device == CORB_CD_DIGITALOUT) &&
(w->d.pin.cap & COP_PINCAP_HDMI))
nhdmi++;
else if (w->d.pin.device == CORB_CD_SPDIFOUT ||
w->d.pin.device == CORB_CD_SPDIFIN)
nspdif++;
break;
}
}
this->codec_type = AZ_CODEC_TYPE_ANALOG;
if ((this->na_dacs == 0) && (this->na_adcs == 0)) {
this->codec_type = AZ_CODEC_TYPE_DIGITAL;
if (nspdif == 0 && nhdmi > 0)
this->codec_type = AZ_CODEC_TYPE_HDMI;
}
/* make sure built-in mic is connected to an adc */
if (this->mic != -1 && this->mic_adc == -1) {
if (azalia_codec_select_micadc(this)) {
DPRINTF(("%s: cound not select mic adc\n", __func__));
}
}
err = azalia_codec_sort_pins(this);
if (err)
return err;
err = azalia_codec_find_inputmixer(this);
if (err)
return err;
/* If the codec can do multichannel, select different DACs for
* the multichannel jack group. Also be sure to keep track of
* which DAC the front headphone is connected to.
*/
if (this->na_dacs >= 3 && this->nopins >= 3) {
err = azalia_codec_select_dacs(this);
if (err)
return err;
}
err = azalia_codec_select_spkrdac(this);
if (err)
return err;
err = azalia_init_dacgroup(this);
if (err)
return err;
azalia_codec_print_groups(this);
err = azalia_widget_label_widgets(this);
if (err)
return err;
err = azalia_codec_construct_format(this, 0, 0);
if (err)
return err;
err = azalia_codec_init_volgroups(this);
if (err)
return err;
if (this->qrks & AZ_QRK_GPIO_MASK) {
err = azalia_codec_gpio_quirks(this);
if (err)
return err;
}
err = azalia_mixer_init(this);
if (err)
return err;
return 0;
}
int
azalia_codec_find_inputmixer(codec_t *this)
{
widget_t *w;
int i, j;
this->input_mixer = -1;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (w->type != COP_AWTYPE_AUDIO_MIXER)
continue;
/* can input from a pin */
for (j = 0; j < this->nipins; j++) {
if (azalia_codec_fnode(this, this->ipins[j].nid,
w->nid, 0) != -1)
break;
}
if (j == this->nipins)
continue;
/* can output to a pin */
for (j = 0; j < this->nopins; j++) {
if (azalia_codec_fnode(this, w->nid,
this->opins[j].nid, 0) != -1)
break;
}
if (j == this->nopins)
continue;
/* can output to an ADC */
for (j = 0; j < this->na_adcs; j++) {
if (azalia_codec_fnode(this, w->nid,
this->a_adcs[j], 0) != -1)
break;
}
if (j == this->na_adcs)
continue;
this->input_mixer = i;
break;
}
return(0);
}
int
azalia_codec_select_micadc(codec_t *this)
{
widget_t *w;
int i, j, conv, err;
for (i = 0; i < this->na_adcs; i++) {
if (azalia_codec_fnode(this, this->mic,
this->a_adcs[i], 0) >= 0)
break;
}
if (i >= this->na_adcs)
return(-1);
conv = this->a_adcs[i];
w = &this->w[conv];
for (j = 0; j < 10; j++) {
for (i = 0; i < w->nconnections; i++) {
if (!azalia_widget_enabled(this, w->connections[i]))
continue;
if (azalia_codec_fnode(this, this->mic,
w->connections[i], j + 1) >= 0) {
break;
}
}
if (i >= w->nconnections)
return(-1);
err = azalia_comresp(this, w->nid,
CORB_SET_CONNECTION_SELECT_CONTROL, i, 0);
if (err)
return(err);
w->selected = i;
if (w->connections[i] == this->mic) {
this->mic_adc = conv;
return(0);
}
w = &this->w[w->connections[i]];
}
return(-1);
}
int
azalia_codec_sort_pins(codec_t *this)
{
#define MAX_PINS 16
const widget_t *w;
struct io_pin opins[MAX_PINS], opins_d[MAX_PINS];
struct io_pin ipins[MAX_PINS], ipins_d[MAX_PINS];
int nopins, nopins_d, nipins, nipins_d;
int prio, loc, add, nd, conv;
int i, j, k;
nopins = nopins_d = nipins = nipins_d = 0;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (!w->enable || w->type != COP_AWTYPE_PIN_COMPLEX)
continue;
loc = 0;
if (this->na_dacs >= 3 && this->nout_jacks < 3)
loc = CORB_CD_LOC_GEO(w->d.pin.config);
prio = w->d.pin.association << 4 | w->d.pin.sequence;
conv = -1;
/* analog out */
if ((w->d.pin.cap & COP_PINCAP_OUTPUT) &&
!(w->widgetcap & COP_AWCAP_DIGITAL)) {
add = nd = 0;
conv = azalia_codec_find_defdac(this, w->nid, 0);
switch(w->d.pin.device) {
/* primary - output by default */
case CORB_CD_SPEAKER:
if (w->nid == this->speaker ||
w->nid == this->speaker2)
break;
/* FALLTHROUGH */
case CORB_CD_HEADPHONE:
case CORB_CD_LINEOUT:
add = 1;
break;
/* secondary - input by default */
case CORB_CD_MICIN:
if (w->nid == this->mic)
break;
/* FALLTHROUGH */
case CORB_CD_LINEIN:
add = nd = 1;
break;
}
if (add && nopins < MAX_PINS) {
opins[nopins].nid = w->nid;
opins[nopins].conv = conv;
prio |= (nd << 8) | (loc << 9);
opins[nopins].prio = prio;
nopins++;
}
}
/* digital out */
if ((w->d.pin.cap & COP_PINCAP_OUTPUT) &&
(w->widgetcap & COP_AWCAP_DIGITAL)) {
conv = azalia_codec_find_defdac(this, w->nid, 0);
switch(w->d.pin.device) {
case CORB_CD_SPDIFOUT:
case CORB_CD_DIGITALOUT:
if (nopins_d < MAX_PINS) {
opins_d[nopins_d].nid = w->nid;
opins_d[nopins_d].conv = conv;
opins_d[nopins_d].prio = prio;
nopins_d++;
}
break;
}
}
/* analog in */
if ((w->d.pin.cap & COP_PINCAP_INPUT) &&
!(w->widgetcap & COP_AWCAP_DIGITAL)) {
add = nd = 0;
conv = azalia_codec_find_defadc(this, w->nid, 0);
switch(w->d.pin.device) {
/* primary - input by default */
case CORB_CD_MICIN:
case CORB_CD_LINEIN:
add = 1;
break;
/* secondary - output by default */
case CORB_CD_SPEAKER:
if (w->nid == this->speaker ||
w->nid == this->speaker2)
break;
/* FALLTHROUGH */
case CORB_CD_HEADPHONE:
case CORB_CD_LINEOUT:
add = nd = 1;
break;
}
if (add && nipins < MAX_PINS) {
ipins[nipins].nid = w->nid;
ipins[nipins].prio = prio | (nd << 8);
ipins[nipins].conv = conv;
nipins++;
}
}
/* digital in */
if ((w->d.pin.cap & COP_PINCAP_INPUT) &&
(w->widgetcap & COP_AWCAP_DIGITAL)) {
conv = azalia_codec_find_defadc(this, w->nid, 0);
switch(w->d.pin.device) {
case CORB_CD_SPDIFIN:
case CORB_CD_DIGITALIN:
case CORB_CD_MICIN:
if (nipins_d < MAX_PINS) {
ipins_d[nipins_d].nid = w->nid;
ipins_d[nipins_d].prio = prio;
ipins_d[nipins_d].conv = conv;
nipins_d++;
}
break;
}
}
}
this->opins = mallocarray(nopins, sizeof(struct io_pin), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (this->opins == NULL)
return(ENOMEM);
this->nopins = 0;
for (i = 0; i < nopins; i++) {
for (j = 0; j < this->nopins; j++)
if (this->opins[j].prio > opins[i].prio)
break;
for (k = this->nopins; k > j; k--)
this->opins[k] = this->opins[k - 1];
if (j < nopins)
this->opins[j] = opins[i];
this->nopins++;
if (this->nopins == nopins)
break;
}
this->opins_d = mallocarray(nopins_d, sizeof(struct io_pin), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (this->opins_d == NULL)
return(ENOMEM);
this->nopins_d = 0;
for (i = 0; i < nopins_d; i++) {
for (j = 0; j < this->nopins_d; j++)
if (this->opins_d[j].prio > opins_d[i].prio)
break;
for (k = this->nopins_d; k > j; k--)
this->opins_d[k] = this->opins_d[k - 1];
if (j < nopins_d)
this->opins_d[j] = opins_d[i];
this->nopins_d++;
if (this->nopins_d == nopins_d)
break;
}
this->ipins = mallocarray(nipins, sizeof(struct io_pin), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (this->ipins == NULL)
return(ENOMEM);
this->nipins = 0;
for (i = 0; i < nipins; i++) {
for (j = 0; j < this->nipins; j++)
if (this->ipins[j].prio > ipins[i].prio)
break;
for (k = this->nipins; k > j; k--)
this->ipins[k] = this->ipins[k - 1];
if (j < nipins)
this->ipins[j] = ipins[i];
this->nipins++;
if (this->nipins == nipins)
break;
}
this->ipins_d = mallocarray(nipins_d, sizeof(struct io_pin), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (this->ipins_d == NULL)
return(ENOMEM);
this->nipins_d = 0;
for (i = 0; i < nipins_d; i++) {
for (j = 0; j < this->nipins_d; j++)
if (this->ipins_d[j].prio > ipins_d[i].prio)
break;
for (k = this->nipins_d; k > j; k--)
this->ipins_d[k] = this->ipins_d[k - 1];
if (j < nipins_d)
this->ipins_d[j] = ipins_d[i];
this->nipins_d++;
if (this->nipins_d == nipins_d)
break;
}
#ifdef AZALIA_DEBUG
printf("%s: analog out pins:", __func__);
for (i = 0; i < this->nopins; i++)
printf(" 0x%2.2x->0x%2.2x", this->opins[i].nid,
this->opins[i].conv);
printf("\n");
printf("%s: digital out pins:", __func__);
for (i = 0; i < this->nopins_d; i++)
printf(" 0x%2.2x->0x%2.2x", this->opins_d[i].nid,
this->opins_d[i].conv);
printf("\n");
printf("%s: analog in pins:", __func__);
for (i = 0; i < this->nipins; i++)
printf(" 0x%2.2x->0x%2.2x", this->ipins[i].nid,
this->ipins[i].conv);
printf("\n");
printf("%s: digital in pins:", __func__);
for (i = 0; i < this->nipins_d; i++)
printf(" 0x%2.2x->0x%2.2x", this->ipins_d[i].nid,
this->ipins_d[i].conv);
printf("\n");
#endif
return 0;
#undef MAX_PINS
}
int
azalia_codec_select_dacs(codec_t *this)
{
widget_t *w;
nid_t *convs;
int nconv, conv;
int i, j, k, err;
convs = mallocarray(this->na_dacs, sizeof(nid_t), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (convs == NULL)
return(ENOMEM);
err = 0;
nconv = 0;
for (i = 0; i < this->nopins; i++) {
w = &this->w[this->opins[i].nid];
conv = this->opins[i].conv;
for (j = 0; j < nconv; j++) {
if (conv == convs[j])
break;
}
if (j == nconv) {
convs[nconv++] = conv;
if (w->nid == this->fhp)
this->fhp_dac = conv;
if (nconv >= this->na_dacs) {
break;
}
} else {
/* find a different dac */
conv = -1;
for (j = 0; j < w->nconnections; j++) {
if (!azalia_widget_enabled(this,
w->connections[j]))
continue;
conv = azalia_codec_find_defdac(this,
w->connections[j], 1);
if (conv == -1)
continue;
for (k = 0; k < nconv; k++) {
if (conv == convs[k])
break;
}
if (k == nconv)
break;
}
if (j < w->nconnections && conv != -1) {
err = azalia_comresp(this, w->nid,
CORB_SET_CONNECTION_SELECT_CONTROL, j, 0);
if (err)
break;
w->selected = j;
this->opins[i].conv = conv;
if (w->nid == this->fhp)
this->fhp_dac = conv;
convs[nconv++] = conv;
if (nconv >= this->na_dacs)
break;
}
}
}
free(convs, M_DEVBUF, 0);
return(err);
}
/* Connect the speaker to a DAC that no other output pin is connected
* to by default. If that is not possible, connect to a DAC other
* than the one the first output pin is connected to.
*/
int
azalia_codec_select_spkrdac(codec_t *this)
{
widget_t *w;
nid_t convs[HDA_MAX_CHANNELS];
int nconv, conv;
int i, j, err, fspkr, conn;
nconv = fspkr = 0;
for (i = 0; i < this->nopins; i++) {
conv = this->opins[i].conv;
for (j = 0; j < nconv; j++) {
if (conv == convs[j])
break;
}
if (j == nconv) {
if (conv == this->spkr_dac)
fspkr = 1;
convs[nconv++] = conv;
if (nconv == this->na_dacs)
break;
}
}
if (fspkr) {
conn = conv = -1;
w = &this->w[this->speaker];
for (i = 0; i < w->nconnections; i++) {
conv = azalia_codec_find_defdac(this,
w->connections[i], 1);
for (j = 0; j < nconv; j++)
if (conv == convs[j])
break;
if (j == nconv)
break;
}
if (i < w->nconnections) {
conn = i;
} else {
/* Couldn't get a unique DAC. Try to get a diferent
* DAC than the first pin's DAC.
*/
if (this->spkr_dac == this->opins[0].conv) {
/* If the speaker connection can't be changed,
* change the first pin's connection.
*/
if (w->nconnections == 1)
w = &this->w[this->opins[0].nid];
for (j = 0; j < w->nconnections; j++) {
conv = azalia_codec_find_defdac(this,
w->connections[j], 1);
if (conv != this->opins[0].conv) {
conn = j;
break;
}
}
}
}
if (conn != -1 && conv != -1) {
err = azalia_comresp(this, w->nid,
CORB_SET_CONNECTION_SELECT_CONTROL, conn, 0);
if (err)
return(err);
w->selected = conn;
if (w->nid == this->speaker)
this->spkr_dac = conv;
else
this->opins[0].conv = conv;
}
}
/* If there is a speaker2, try to connect it to spkr_dac. */
if (this->speaker2 != -1) {
conn = conv = -1;
w = &this->w[this->speaker2];
for (i = 0; i < w->nconnections; i++) {
conv = azalia_codec_find_defdac(this,
w->connections[i], 1);
if (conv == this->spkr_dac) {
conn = i;
break;
}
}
if (conn != -1) {
err = azalia_comresp(this, w->nid,
CORB_SET_CONNECTION_SELECT_CONTROL, conn, 0);
if (err)
return(err);
w->selected = conn;
}
}
return(0);
}
int
azalia_codec_find_defdac(codec_t *this, int index, int depth)
{
const widget_t *w;
int i, ret;
w = &this->w[index];
if (w->enable == 0)
return -1;
if (w->type == COP_AWTYPE_AUDIO_OUTPUT)
return index;
if (depth > 0 &&
(w->type == COP_AWTYPE_PIN_COMPLEX ||
w->type == COP_AWTYPE_BEEP_GENERATOR ||
w->type == COP_AWTYPE_AUDIO_INPUT))
return -1;
if (++depth >= 10)
return -1;
if (w->nconnections > 0) {
/* by default, all mixer connections are active */
if (w->type == COP_AWTYPE_AUDIO_MIXER) {
for (i = 0; i < w->nconnections; i++) {
index = w->connections[i];
if (!azalia_widget_enabled(this, index))
continue;
ret = azalia_codec_find_defdac(this, index,
depth);
if (ret >= 0)
return ret;
}
/* 7.3.3.2 Connection Select Control
* If an attempt is made to Set an index value greater than
* the number of list entries (index is equal to or greater
* than the Connection List Length property for the widget)
* the behavior is not predictable.
*/
/* negative index values are wrong too */
} else if (w->selected >= 0 &&
w->selected < sizeof(w->connections)) {
index = w->connections[w->selected];
if (VALID_WIDGET_NID(index, this)) {
ret = azalia_codec_find_defdac(this,
index, depth);
if (ret >= 0)
return ret;
}
}
}
return -1;
}
int
azalia_codec_find_defadc_sub(codec_t *this, nid_t node, int index, int depth)
{
const widget_t *w;
int i, ret;
w = &this->w[index];
if (w->nid == node) {
return index;
}
/* back at the beginning or a bad end */
if (depth > 0 &&
(w->type == COP_AWTYPE_PIN_COMPLEX ||
w->type == COP_AWTYPE_BEEP_GENERATOR ||
w->type == COP_AWTYPE_AUDIO_OUTPUT ||
w->type == COP_AWTYPE_AUDIO_INPUT))
return -1;
if (++depth >= 10)
return -1;
if (w->nconnections > 0) {
/* by default, all mixer connections are active */
if (w->type == COP_AWTYPE_AUDIO_MIXER) {
for (i = 0; i < w->nconnections; i++) {
if (!azalia_widget_enabled(this, w->connections[i]))
continue;
ret = azalia_codec_find_defadc_sub(this, node,
w->connections[i], depth);
if (ret >= 0)
return ret;
}
/* 7.3.3.2 Connection Select Control
* If an attempt is made to Set an index value greater than
* the number of list entries (index is equal to or greater
* than the Connection List Length property for the widget)
* the behavior is not predictable.
*/
/* negative index values are wrong too */
} else if (w->selected >= 0 &&
w->selected < sizeof(w->connections)) {
index = w->connections[w->selected];
if (VALID_WIDGET_NID(index, this)) {
ret = azalia_codec_find_defadc_sub(this,
node, index, depth);
if (ret >= 0)
return ret;
}
}
}
return -1;
}
int
azalia_codec_find_defadc(codec_t *this, int index, int depth)
{
int i, j, conv;
conv = -1;
for (i = 0; i < this->na_adcs; i++) {
j = azalia_codec_find_defadc_sub(this, index,
this->a_adcs[i], 0);
if (j >= 0) {
conv = this->a_adcs[i];
break;
}
}
return(conv);
}
int
azalia_codec_init_volgroups(codec_t *this)
{
const widget_t *w;
uint32_t cap, result;
int i, j, dac, err;
j = 0;
this->playvols.mask = 0;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (w->enable == 0)
continue;
if (w->mixer_class == AZ_CLASS_RECORD)
continue;
if (!(w->widgetcap & COP_AWCAP_OUTAMP))
continue;
if ((COP_AMPCAP_NUMSTEPS(w->outamp_cap) == 0) &&
!(w->outamp_cap & COP_AMPCAP_MUTE))
continue;
this->playvols.mask |= (1 << j);
this->playvols.slaves[j++] = w->nid;
if (j >= AZ_MAX_VOL_SLAVES)
break;
}
this->playvols.nslaves = j;
this->playvols.cur = 0;
for (i = 0; i < this->playvols.nslaves; i++) {
w = &this->w[this->playvols.slaves[i]];
if (w->nid == this->input_mixer ||
w->parent == this->input_mixer ||
WIDGET_CHANNELS(w) < 2)
continue;
j = 0;
/* azalia_codec_find_defdac only goes 10 connections deep.
* Start the connection depth at 7 so it doesn't go more
* than 3 connections deep.
*/
if (w->type == COP_AWTYPE_AUDIO_MIXER ||
w->type == COP_AWTYPE_AUDIO_SELECTOR)
j = 7;
dac = azalia_codec_find_defdac(this, w->nid, j);
if (dac == -1)
continue;
if (dac != this->dacs.groups[this->dacs.cur].conv[0] &&
dac != this->spkr_dac && dac != this->fhp_dac)
continue;
cap = w->outamp_cap;
if ((cap & COP_AMPCAP_MUTE) && COP_AMPCAP_NUMSTEPS(cap)) {
if (w->type == COP_AWTYPE_BEEP_GENERATOR) {
continue;
} else if (w->type == COP_AWTYPE_PIN_COMPLEX) {
err = azalia_comresp(this, w->nid,
CORB_GET_PIN_WIDGET_CONTROL, 0, &result);
if (!err && (result & CORB_PWC_OUTPUT))
this->playvols.cur |= (1 << i);
} else
this->playvols.cur |= (1 << i);
}
}
if (this->playvols.cur == 0) {
for (i = 0; i < this->playvols.nslaves; i++) {
w = &this->w[this->playvols.slaves[i]];
j = 0;
if (w->type == COP_AWTYPE_AUDIO_MIXER ||
w->type == COP_AWTYPE_AUDIO_SELECTOR)
j = 7;
dac = azalia_codec_find_defdac(this, w->nid, j);
if (dac == -1)
continue;
if (dac != this->dacs.groups[this->dacs.cur].conv[0] &&
dac != this->spkr_dac && dac != this->fhp_dac)
continue;
if (w->type == COP_AWTYPE_BEEP_GENERATOR)
continue;
if (w->type == COP_AWTYPE_PIN_COMPLEX) {
err = azalia_comresp(this, w->nid,
CORB_GET_PIN_WIDGET_CONTROL, 0, &result);
if (!err && (result & CORB_PWC_OUTPUT))
this->playvols.cur |= (1 << i);
} else {
this->playvols.cur |= (1 << i);
}
}
}
this->playvols.master = this->audiofunc;
if (this->playvols.nslaves > 0) {
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (w->type != COP_AWTYPE_VOLUME_KNOB)
continue;
if (!COP_VKCAP_NUMSTEPS(w->d.volume.cap))
continue;
this->playvols.master = w->nid;
break;
}
}
j = 0;
this->recvols.mask = 0;
FOR_EACH_WIDGET(this, i) {
w = &this->w[i];
if (w->enable == 0)
continue;
if (w->type == COP_AWTYPE_AUDIO_INPUT ||
w->type == COP_AWTYPE_PIN_COMPLEX) {
if (!(w->widgetcap & COP_AWCAP_INAMP))
continue;
if ((COP_AMPCAP_NUMSTEPS(w->inamp_cap) == 0) &&
!(w->inamp_cap & COP_AMPCAP_MUTE))
continue;
} else if (w->type == COP_AWTYPE_AUDIO_MIXER ||
w->type == COP_AWTYPE_AUDIO_SELECTOR) {
if (w->mixer_class != AZ_CLASS_RECORD)
continue;
if (!(w->widgetcap & COP_AWCAP_OUTAMP))
continue;
if ((COP_AMPCAP_NUMSTEPS(w->outamp_cap) == 0) &&
!(w->outamp_cap & COP_AMPCAP_MUTE))
continue;
} else {
continue;
}
this->recvols.mask |= (1 << j);
this->recvols.slaves[j++] = w->nid;
if (j >= AZ_MAX_VOL_SLAVES)
break;
}
this->recvols.nslaves = j;
this->recvols.cur = 0;
for (i = 0; i < this->recvols.nslaves; i++) {
w = &this->w[this->recvols.slaves[i]];
cap = w->outamp_cap;
if (w->type == COP_AWTYPE_AUDIO_INPUT ||
w->type != COP_AWTYPE_PIN_COMPLEX)
cap = w->inamp_cap;
else
if (w->mixer_class != AZ_CLASS_RECORD)
continue;
if ((cap & COP_AMPCAP_MUTE) && COP_AMPCAP_NUMSTEPS(cap)) {
if (w->type == COP_AWTYPE_PIN_COMPLEX) {
err = azalia_comresp(this, w->nid,
CORB_GET_PIN_WIDGET_CONTROL, 0, &result);
if (!err && !(result & CORB_PWC_OUTPUT))
this->recvols.cur |= (1 << i);
} else
this->recvols.cur |= (1 << i);
}
}
if (this->recvols.cur == 0) {
for (i = 0; i < this->recvols.nslaves; i++) {
w = &this->w[this->recvols.slaves[i]];
cap = w->outamp_cap;
if (w->type == COP_AWTYPE_AUDIO_INPUT ||
w->type != COP_AWTYPE_PIN_COMPLEX)
cap = w->inamp_cap;
else
if (w->mixer_class != AZ_CLASS_RECORD)
continue;
if (w->type == COP_AWTYPE_PIN_COMPLEX) {
err = azalia_comresp(this, w->nid,
CORB_GET_PIN_WIDGET_CONTROL, 0, &result);
if (!err && !(result & CORB_PWC_OUTPUT))
this->recvols.cur |= (1 << i);
} else {
this->recvols.cur |= (1 << i);
}
}
}
this->recvols.master = this->audiofunc;
return 0;
}
int
azalia_codec_delete(codec_t *this)
{
azalia_mixer_delete(this);
if (this->formats != NULL) {
free(this->formats, M_DEVBUF, 0);
this->formats = NULL;
}
this->nformats = 0;
if (this->opins != NULL) {
free(this->opins, M_DEVBUF, 0);
this->opins = NULL;
}
this->nopins = 0;
if (this->opins_d != NULL) {
free(this->opins_d, M_DEVBUF, 0);
this->opins_d = NULL;
}
this->nopins_d = 0;
if (this->ipins != NULL) {
free(this->ipins, M_DEVBUF, 0);
this->ipins = NULL;
}
this->nipins = 0;
if (this->ipins_d != NULL) {
free(this->ipins_d, M_DEVBUF, 0);
this->ipins_d = NULL;
}
this->nipins_d = 0;
if (this->w != NULL) {
free(this->w, M_DEVBUF, 0);
this->w = NULL;
}
return 0;
}
int
azalia_codec_construct_format(codec_t *this, int newdac, int newadc)
{
const convgroup_t *group;
uint32_t bits_rates;
int variation;
int nbits, c, chan, i;
nid_t nid;
variation = 0;
if (this->dacs.ngroups > 0 && newdac < this->dacs.ngroups &&
newdac >= 0) {
this->dacs.cur = newdac;
group = &this->dacs.groups[this->dacs.cur];
bits_rates = this->w[group->conv[0]].d.audio.bits_rates;
nbits = 0;
if (bits_rates & COP_PCM_B8)
nbits++;
if (bits_rates & COP_PCM_B16)
nbits++;
if (bits_rates & COP_PCM_B20)
nbits++;
if (bits_rates & COP_PCM_B24)
nbits++;
if ((bits_rates & COP_PCM_B32) &&
!(this->w[group->conv[0]].widgetcap & COP_AWCAP_DIGITAL))
nbits++;
if (nbits == 0) {
printf("%s: invalid DAC PCM format: 0x%8.8x\n",
XNAME(this->az), bits_rates);
return -1;
}
variation += group->nconv * nbits;
}
if (this->adcs.ngroups > 0 && newadc < this->adcs.ngroups &&
newadc >= 0) {
this->adcs.cur = newadc;
group = &this->adcs.groups[this->adcs.cur];
bits_rates = this->w[group->conv[0]].d.audio.bits_rates;
nbits = 0;
if (bits_rates & COP_PCM_B8)
nbits++;
if (bits_rates & COP_PCM_B16)
nbits++;
if (bits_rates & COP_PCM_B20)
nbits++;
if (bits_rates & COP_PCM_B24)
nbits++;
if ((bits_rates & COP_PCM_B32) &&
!(this->w[group->conv[0]].widgetcap & COP_AWCAP_DIGITAL))
nbits++;
if (nbits == 0) {
printf("%s: invalid ADC PCM format: 0x%8.8x\n",
XNAME(this->az), bits_rates);
return -1;
}
variation += group->nconv * nbits;
}
if (variation == 0) {
DPRINTF(("%s: no converter groups\n", XNAME(this->az)));
return -1;
}
if (this->formats != NULL)
free(this->formats, M_DEVBUF, 0);
this->nformats = 0;
this->formats = mallocarray(variation, sizeof(struct audio_format),
M_DEVBUF, M_NOWAIT | M_ZERO);
if (this->formats == NULL) {
printf("%s: out of memory in %s\n",
XNAME(this->az), __func__);
return ENOMEM;
}
/* register formats for playback */
if (this->dacs.ngroups > 0) {
group = &this->dacs.groups[this->dacs.cur];
for (c = 0; c < group->nconv; c++) {
chan = 0;
bits_rates = ~0;
if (this->w[group->conv[0]].widgetcap &
COP_AWCAP_DIGITAL)
bits_rates &= ~(COP_PCM_B32);
for (i = 0; i <= c; i++) {
nid = group->conv[i];
chan += WIDGET_CHANNELS(&this->w[nid]);
bits_rates &= this->w[nid].d.audio.bits_rates;
}
azalia_codec_add_bits(this, chan, bits_rates,
AUMODE_PLAY);
}
}
/* register formats for recording */
if (this->adcs.ngroups > 0) {
group = &this->adcs.groups[this->adcs.cur];
for (c = 0; c < group->nconv; c++) {
chan = 0;
bits_rates = ~0;
if (this->w[group->conv[0]].widgetcap &
COP_AWCAP_DIGITAL)
bits_rates &= ~(COP_PCM_B32);
for (i = 0; i <= c; i++) {
nid = group->conv[i];
chan += WIDGET_CHANNELS(&this->w[nid]);
bits_rates &= this->w[nid].d.audio.bits_rates;
}
azalia_codec_add_bits(this, chan, bits_rates,
AUMODE_RECORD);
}
}
return 0;
}
void
azalia_codec_add_bits(codec_t *this, int chan, uint32_t bits_rates, int mode)
{
if (bits_rates & COP_PCM_B8)
azalia_codec_add_format(this, chan, 8, bits_rates, mode);
if (bits_rates & COP_PCM_B16)
azalia_codec_add_format(this, chan, 16, bits_rates, mode);
if (bits_rates & COP_PCM_B20)
azalia_codec_add_format(this, chan, 20, bits_rates, mode);
if (bits_rates & COP_PCM_B24)
azalia_codec_add_format(this, chan, 24, bits_rates, mode);
if (bits_rates & COP_PCM_B32)
azalia_codec_add_format(this, chan, 32, bits_rates, mode);
}
void
azalia_codec_add_format(codec_t *this, int chan, int prec, uint32_t rates,
int32_t mode)
{
struct audio_format *f;
f = &this->formats[this->nformats++];
f->mode = mode;
f->encoding = AUDIO_ENCODING_SLINEAR_LE;
if (prec == 8)
f->encoding = AUDIO_ENCODING_ULINEAR_LE;
f->precision = prec;
f->channels = chan;
f->frequency_type = 0;
if (rates & COP_PCM_R80)
f->frequency[f->frequency_type++] = 8000;
if (rates & COP_PCM_R110)
f->frequency[f->frequency_type++] = 11025;
if (rates & COP_PCM_R160)
f->frequency[f->frequency_type++] = 16000;
if (rates & COP_PCM_R220)
f->frequency[f->frequency_type++] = 22050;
if (rates & COP_PCM_R320)
f->frequency[f->frequency_type++] = 32000;
if (rates & COP_PCM_R441)
f->frequency[f->frequency_type++] = 44100;
if (rates & COP_PCM_R480)
f->frequency[f->frequency_type++] = 48000;
if (rates & COP_PCM_R882)
f->frequency[f->frequency_type++] = 88200;
if (rates & COP_PCM_R960)
f->frequency[f->frequency_type++] = 96000;
if (rates & COP_PCM_R1764)
f->frequency[f->frequency_type++] = 176400;
if (rates & COP_PCM_R1920)
f->frequency[f->frequency_type++] = 192000;
if (rates & COP_PCM_R3840)
f->frequency[f->frequency_type++] = 384000;
}
int
azalia_codec_connect_stream(stream_t *this)
{
const codec_t *codec = &this->az->codecs[this->az->codecno];
const convgroup_t *group;
widget_t *w;
uint32_t digital, stream_chan;
int i, err, curchan, nchan, widchan;
err = 0;
nchan = (this->fmt & HDA_SD_FMT_CHAN) + 1;
if (this->dir == AUMODE_RECORD)
group = &codec->adcs.groups[codec->adcs.cur];
else
group = &codec->dacs.groups[codec->dacs.cur];
curchan = 0;
for (i = 0; i < group->nconv; i++) {
w = &codec->w[group->conv[i]];
widchan = WIDGET_CHANNELS(w);
stream_chan = (this->number << 4);
if (curchan < nchan) {
stream_chan |= curchan;
} else if (w->nid == codec->spkr_dac ||
w->nid == codec->fhp_dac) {
stream_chan |= 0; /* first channel(s) */
} else
stream_chan = 0; /* idle stream */
if (stream_chan == 0) {
DPRINTFN(0, ("%s: %2.2x is idle\n", __func__, w->nid));
} else {
DPRINTFN(0, ("%s: %2.2x on stream chan %d\n", __func__,
w->nid, stream_chan & ~(this->number << 4)));
}
err = azalia_comresp(codec, w->nid, CORB_SET_CONVERTER_FORMAT,
this->fmt, NULL);
if (err) {
DPRINTF(("%s: nid %2.2x fmt %2.2x: %d\n",
__func__, w->nid, this->fmt, err));
break;
}
err = azalia_comresp(codec, w->nid,
CORB_SET_CONVERTER_STREAM_CHANNEL, stream_chan, NULL);
if (err) {
DPRINTF(("%s: nid %2.2x chan %d: %d\n",
__func__, w->nid, stream_chan, err));
break;
}
if (w->widgetcap & COP_AWCAP_DIGITAL) {
err = azalia_comresp(codec, w->nid,
CORB_GET_DIGITAL_CONTROL, 0, &digital);
if (err) {
DPRINTF(("%s: nid %2.2x get digital: %d\n",
__func__, w->nid, err));
break;
}
digital = (digital & 0xff) | CORB_DCC_DIGEN;
err = azalia_comresp(codec, w->nid,
CORB_SET_DIGITAL_CONTROL_L, digital, NULL);
if (err) {
DPRINTF(("%s: nid %2.2x set digital: %d\n",
__func__, w->nid, err));
break;
}
}
curchan += widchan;
}
return err;
}
int
azalia_codec_disconnect_stream(stream_t *this)
{
const codec_t *codec = &this->az->codecs[this->az->codecno];
const convgroup_t *group;
uint32_t v;
int i;
nid_t nid;
if (this->dir == AUMODE_RECORD)
group = &codec->adcs.groups[codec->adcs.cur];
else
group = &codec->dacs.groups[codec->dacs.cur];
for (i = 0; i < group->nconv; i++) {
nid = group->conv[i];
azalia_comresp(codec, nid, CORB_SET_CONVERTER_STREAM_CHANNEL,
0, NULL); /* stream#0 */
if (codec->w[nid].widgetcap & COP_AWCAP_DIGITAL) {
/* disable S/PDIF */
azalia_comresp(codec, nid, CORB_GET_DIGITAL_CONTROL,
0, &v);
v = (v & ~CORB_DCC_DIGEN) & 0xff;
azalia_comresp(codec, nid, CORB_SET_DIGITAL_CONTROL_L,
v, NULL);
}
}
return 0;
}
/* ================================================================
* HDA widget functions
* ================================================================ */
int
azalia_widget_init(widget_t *this, const codec_t *codec, nid_t nid)
{
uint32_t result;
int err;
err = azalia_comresp(codec, nid, CORB_GET_PARAMETER,
COP_AUDIO_WIDGET_CAP, &result);
if (err)
return err;
this->nid = nid;
this->widgetcap = result;
this->type = COP_AWCAP_TYPE(result);
if (this->widgetcap & COP_AWCAP_POWER) {
azalia_comresp(codec, nid, CORB_SET_POWER_STATE, CORB_PS_D0,
&result);
DELAY(100);
}
this->enable = 1;
this->mixer_class = -1;
this->parent = codec->audiofunc;
switch (this->type) {
case COP_AWTYPE_AUDIO_OUTPUT:
/* FALLTHROUGH */
case COP_AWTYPE_AUDIO_INPUT:
azalia_widget_init_audio(this, codec);
break;
case COP_AWTYPE_PIN_COMPLEX:
azalia_widget_init_pin(this, codec);
break;
case COP_AWTYPE_VOLUME_KNOB:
err = azalia_comresp(codec, this->nid, CORB_GET_PARAMETER,
COP_VOLUME_KNOB_CAPABILITIES, &result);
if (err)
return err;
this->d.volume.cap = result;
break;
case COP_AWTYPE_POWER:
/* FALLTHROUGH */
case COP_AWTYPE_VENDOR_DEFINED:
this->enable = 0;
break;
}
/* amplifier information */
/* XXX (ab)use bits 24-30 to store the "control offset", which is
* the number of steps, starting at 0, that have no effect. these
* bits are reserved in HDA 1.0.
*/
if (this->widgetcap & COP_AWCAP_INAMP) {
if (this->widgetcap & COP_AWCAP_AMPOV)
azalia_comresp(codec, nid, CORB_GET_PARAMETER,
COP_INPUT_AMPCAP, &this->inamp_cap);
else
this->inamp_cap = codec->w[codec->audiofunc].inamp_cap;
this->inamp_cap &= ~(0x7f << 24);
}
if (this->widgetcap & COP_AWCAP_OUTAMP) {
if (this->widgetcap & COP_AWCAP_AMPOV)
azalia_comresp(codec, nid, CORB_GET_PARAMETER,
COP_OUTPUT_AMPCAP, &this->outamp_cap);
else
this->outamp_cap = codec->w[codec->audiofunc].outamp_cap;
this->outamp_cap &= ~(0x7f << 24);
}
return 0;
}
int
azalia_widget_sole_conn(codec_t *this, nid_t nid)
{
int i, j, target, nconn, has_target;
/* connected to ADC */
for (i = 0; i < this->adcs.ngroups; i++) {
for (j = 0; j < this->adcs.groups[i].nconv; j++) {
target = this->adcs.groups[i].conv[j];
if (this->w[target].nconnections == 1 &&
this->w[target].connections[0] == nid) {
return target;
}
}
}
/* connected to DAC */
for (i = 0; i < this->dacs.ngroups; i++) {
for (j = 0; j < this->dacs.groups[i].nconv; j++) {
target = this->dacs.groups[i].conv[j];
if (this->w[target].nconnections == 1 &&
this->w[target].connections[0] == nid) {
return target;
}
}
}
/* connected to pin complex */
target = -1;
FOR_EACH_WIDGET(this, i) {
if (this->w[i].type != COP_AWTYPE_PIN_COMPLEX)
continue;
if (this->w[i].nconnections == 1 &&
this->w[i].connections[0] == nid) {
if (target != -1)
return -1;
target = i;
} else {
nconn = 0;
has_target = 0;
for (j = 0; j < this->w[i].nconnections; j++) {
if (!this->w[this->w[i].connections[j]].enable)
continue;
nconn++;
if (this->w[i].connections[j] == nid)
has_target = 1;
}
if (has_target == 1) {
if (nconn == 1) {
if (target != -1)
return -1;
target = i;
} else {
/* not sole connection at least once */
return -1;
}
}
}
}
if (target != -1)
return target;
return -1;
}
int
azalia_widget_label_widgets(codec_t *codec)
{
widget_t *w;
convgroup_t *group;
int types[16];
int pins[16];
int colors_used, use_colors, schan;
int i, j;
bzero(&pins, sizeof(pins));
bzero(&types, sizeof(types));
/* If codec has more than one line-out jack, check if the jacks
* have unique colors. If so, use the colors in the mixer names.
*/
use_colors = 1;
colors_used = 0;
if (codec->nout_jacks < 2)
use_colors = 0;
for (i = 0; use_colors && i < codec->nopins; i++) {
w = &codec->w[codec->opins[i].nid];
if (w->d.pin.device != CORB_CD_LINEOUT)
continue;
if (colors_used & (1 << w->d.pin.color))
use_colors = 0;
else
colors_used |= (1 << w->d.pin.color);
}
FOR_EACH_WIDGET(codec, i) {
w = &codec->w[i];
/* default for disabled/unused widgets */
snprintf(w->name, sizeof(w->name), "u-wid%2.2x", w->nid);
if (w->enable == 0)
continue;
switch (w->type) {
case COP_AWTYPE_PIN_COMPLEX:
pins[w->d.pin.device]++;
if (use_colors && w->d.pin.device == CORB_CD_LINEOUT) {
snprintf(w->name, sizeof(w->name), "%s-%s",
pin_devices[w->d.pin.device],
line_colors[w->d.pin.color]);
} else if (pins[w->d.pin.device] > 1) {
snprintf(w->name, sizeof(w->name), "%s%d",
pin_devices[w->d.pin.device],
pins[w->d.pin.device]);
} else {
snprintf(w->name, sizeof(w->name), "%s",
pin_devices[w->d.pin.device]);
}
break;
case COP_AWTYPE_AUDIO_OUTPUT:
if (codec->dacs.ngroups < 1)
break;
group = &codec->dacs.groups[0];
schan = 0;
for (j = 0; j < group->nconv; j++) {
if (w->nid == group->conv[j]) {
snprintf(w->name, sizeof(w->name),
"%s-%d:%d", wtypes[w->type], schan,
schan + WIDGET_CHANNELS(w) - 1);
}
schan += WIDGET_CHANNELS(w);
}
if (codec->dacs.ngroups < 2)
break;
group = &codec->dacs.groups[1];
schan = 0;
for (j = 0; j < group->nconv; j++) {
if (w->nid == group->conv[j]) {
snprintf(w->name, sizeof(w->name),
"dig-%s-%d:%d", wtypes[w->type],
schan,
schan + WIDGET_CHANNELS(w) - 1);
}
schan += WIDGET_CHANNELS(w);
}
break;
case COP_AWTYPE_AUDIO_INPUT:
w->mixer_class = AZ_CLASS_RECORD;
if (codec->adcs.ngroups < 1)
break;
group = &codec->adcs.groups[0];
schan = 0;
for (j = 0; j < group->nconv; j++) {
if (w->nid == group->conv[j]) {
snprintf(w->name, sizeof(w->name),
"%s-%d:%d", wtypes[w->type], schan,
schan + WIDGET_CHANNELS(w) - 1);
}
schan += WIDGET_CHANNELS(w);
}
if (codec->adcs.ngroups < 2)
break;
group = &codec->adcs.groups[1];
schan = 0;
for (j = 0; j < group->nconv; j++) {
if (w->nid == group->conv[j]) {
snprintf(w->name, sizeof(w->name),
"dig-%s-%d:%d", wtypes[w->type],
schan,
schan + WIDGET_CHANNELS(w) - 1);
}
schan += WIDGET_CHANNELS(w);
}
break;
default:
types[w->type]++;
if (types[w->type] > 1)
snprintf(w->name, sizeof(w->name), "%s%d",
wtypes[w->type], types[w->type]);
else
snprintf(w->name, sizeof(w->name), "%s",
wtypes[w->type]);
break;
}
}
/* Mixers and selectors that connect to only one other widget are
* functionally part of the widget they are connected to. Show that
* relationship in the name.
*/
FOR_EACH_WIDGET(codec, i) {
if (codec->w[i].type != COP_AWTYPE_AUDIO_MIXER &&
codec->w[i].type != COP_AWTYPE_AUDIO_SELECTOR)
continue;
if (codec->w[i].enable == 0)
continue;
j = azalia_widget_sole_conn(codec, i);
if (j == -1) {
/* Special case. A selector with outamp capabilities
* and is connected to a single widget that has either
* no input or no output capabilities. This widget
* serves as the input or output amp for the widget
* it is connected to.
*/
if (codec->w[i].type == COP_AWTYPE_AUDIO_SELECTOR &&
(codec->w[i].widgetcap & COP_AWCAP_OUTAMP) &&
codec->w[i].nconnections == 1) {
j = codec->w[i].connections[0];
if (!azalia_widget_enabled(codec, j))
continue;
if (!(codec->w[j].widgetcap & COP_AWCAP_INAMP))
codec->w[i].mixer_class =
AZ_CLASS_INPUT;
else if (!(codec->w[j].widgetcap & COP_AWCAP_OUTAMP))
codec->w[i].mixer_class =
AZ_CLASS_OUTPUT;
else
continue;
}
}
if (j >= 0) {
/* As part of a disabled widget, this widget
* should be disabled as well.
*/
if (codec->w[j].enable == 0) {
codec->w[i].enable = 0;
snprintf(codec->w[i].name,
sizeof(codec->w[i].name),
"u-wid%2.2x", i);
continue;
}
snprintf(codec->w[i].name, sizeof(codec->w[i].name),
"%s", codec->w[j].name);
if (codec->w[j].mixer_class == AZ_CLASS_RECORD)
codec->w[i].mixer_class = AZ_CLASS_RECORD;
codec->w[i].parent = j;
}
}
return 0;
}
int
azalia_widget_init_audio(widget_t *this, const codec_t *codec)
{
uint32_t result;
int err;
/* check audio format */
if (this->widgetcap & COP_AWCAP_FORMATOV) {
err = azalia_comresp(codec, this->nid, CORB_GET_PARAMETER,
COP_STREAM_FORMATS, &result);
if (err)
return err;
this->d.audio.encodings = result;
if (result == 0) { /* quirk for CMI9880.
* This must not occur usually... */
this->d.audio.encodings =
codec->w[codec->audiofunc].d.audio.encodings;
this->d.audio.bits_rates =
codec->w[codec->audiofunc].d.audio.bits_rates;
} else {
if ((result & COP_STREAM_FORMAT_PCM) == 0) {
printf("%s: %s: No PCM support: %x\n",
XNAME(codec->az), this->name, result);
return -1;
}
err = azalia_comresp(codec, this->nid,
CORB_GET_PARAMETER, COP_PCM, &result);
if (err)
return err;
this->d.audio.bits_rates = result;
}
} else {
this->d.audio.encodings =
codec->w[codec->audiofunc].d.audio.encodings;
this->d.audio.bits_rates =
codec->w[codec->audiofunc].d.audio.bits_rates;
}
return 0;
}
int
azalia_widget_init_pin(widget_t *this, const codec_t *codec)
{
uint32_t result, dir;
int err;
err = azalia_comresp(codec, this->nid, CORB_GET_CONFIGURATION_DEFAULT,
0, &result);
if (err)
return err;
this->d.pin.config = result;
this->d.pin.sequence = CORB_CD_SEQUENCE(result);
this->d.pin.association = CORB_CD_ASSOCIATION(result);
this->d.pin.color = CORB_CD_COLOR(result);
this->d.pin.device = CORB_CD_DEVICE(result);
err = azalia_comresp(codec, this->nid, CORB_GET_PARAMETER,
COP_PINCAP, &result);
if (err)
return err;
this->d.pin.cap = result;
dir = CORB_PWC_INPUT;
switch (this->d.pin.device) {
case CORB_CD_LINEOUT:
case CORB_CD_SPEAKER:
case CORB_CD_HEADPHONE:
case CORB_CD_SPDIFOUT:
case CORB_CD_DIGITALOUT:
dir = CORB_PWC_OUTPUT;
break;
}
if (dir == CORB_PWC_INPUT && !(this->d.pin.cap & COP_PINCAP_INPUT))
dir = CORB_PWC_OUTPUT;
if (dir == CORB_PWC_OUTPUT && !(this->d.pin.cap & COP_PINCAP_OUTPUT))
dir = CORB_PWC_INPUT;
if (dir == CORB_PWC_INPUT && this->d.pin.device == CORB_CD_MICIN) {
if (COP_PINCAP_VREF(this->d.pin.cap) & (1 << CORB_PWC_VREF_80))
dir |= CORB_PWC_VREF_80;
else if (COP_PINCAP_VREF(this->d.pin.cap) &
(1 << CORB_PWC_VREF_50))
dir |= CORB_PWC_VREF_50;
}
if ((codec->qrks & AZ_QRK_WID_OVREF50) && (dir == CORB_PWC_OUTPUT))
dir |= CORB_PWC_VREF_50;
azalia_comresp(codec, this->nid, CORB_SET_PIN_WIDGET_CONTROL,
dir, NULL);
if (this->d.pin.cap & COP_PINCAP_EAPD) {
err = azalia_comresp(codec, this->nid,
CORB_GET_EAPD_BTL_ENABLE, 0, &result);
if (err)
return err;
result &= 0xff;
result |= CORB_EAPD_EAPD;
err = azalia_comresp(codec, this->nid,
CORB_SET_EAPD_BTL_ENABLE, result, &result);
if (err)
return err;
}
/* Disable unconnected pins */
if (CORB_CD_PORT(this->d.pin.config) == CORB_CD_NONE)
this->enable = 0;
return 0;
}
int
azalia_widget_init_connection(widget_t *this, const codec_t *codec)
{
uint32_t result;
int err;
int i, j, k;
int length, nconn, bits, conn, last;
this->selected = -1;
if ((this->widgetcap & COP_AWCAP_CONNLIST) == 0)
return 0;
err = azalia_comresp(codec, this->nid, CORB_GET_PARAMETER,
COP_CONNECTION_LIST_LENGTH, &result);
if (err)
return err;
bits = 8;
if (result & COP_CLL_LONG)
bits = 16;
length = COP_CLL_LENGTH(result);
if (length == 0)
return 0;
/*
* 'length' is the number of entries, not the number of
* connections. Find the number of connections, 'nconn', so
* enough space can be allocated for the list of connected
* nids.
*/
nconn = last = 0;
for (i = 0; i < length;) {
err = azalia_comresp(codec, this->nid,
CORB_GET_CONNECTION_LIST_ENTRY, i, &result);
if (err)
return err;
for (k = 0; i < length && (k < 32 / bits); k++) {
conn = (result >> (k * bits)) & ((1 << bits) - 1);
/* If high bit is set, this is the end of a continuous
* list that started with the last connection.
*/
if ((nconn > 0) && (conn & (1 << (bits - 1))))
nconn += (conn & ~(1 << (bits - 1))) - last;
else
nconn++;
last = conn;
i++;
}
}
this->connections = mallocarray(nconn, sizeof(nid_t), M_DEVBUF, M_NOWAIT);
if (this->connections == NULL) {
printf("%s: out of memory\n", XNAME(codec->az));
return ENOMEM;
}
for (i = 0; i < nconn;) {
err = azalia_comresp(codec, this->nid,
CORB_GET_CONNECTION_LIST_ENTRY, i, &result);
if (err)
return err;
for (k = 0; i < nconn && (k < 32 / bits); k++) {
conn = (result >> (k * bits)) & ((1 << bits) - 1);
/* If high bit is set, this is the end of a continuous
* list that started with the last connection.
*/
if ((i > 0) && (conn & (1 << (bits - 1)))) {
for (j = 1; i < nconn && j <= conn - last; j++)
this->connections[i++] = last + j;
} else {
this->connections[i++] = conn;
}
last = conn;
}
}
this->nconnections = nconn;
if (nconn > 0) {
err = azalia_comresp(codec, this->nid,
CORB_GET_CONNECTION_SELECT_CONTROL, 0, &result);
if (err)
return err;
this->selected = CORB_CSC_INDEX(result);
}
return 0;
}
int
azalia_widget_check_conn(codec_t *codec, int index, int depth)
{
const widget_t *w;
int i;
w = &codec->w[index];
if (w->type == COP_AWTYPE_BEEP_GENERATOR)
return 0;
if (depth > 0 &&
(w->type == COP_AWTYPE_PIN_COMPLEX ||
w->type == COP_AWTYPE_AUDIO_OUTPUT ||
w->type == COP_AWTYPE_AUDIO_INPUT)) {
if (w->enable)
return 1;
else
return 0;
}
if (++depth >= 10)
return 0;
for (i = 0; i < w->nconnections; i++) {
if (!azalia_widget_enabled(codec, w->connections[i]))
continue;
if (azalia_widget_check_conn(codec, w->connections[i], depth))
return 1;
}
return 0;
}
#ifdef AZALIA_DEBUG
#define WIDGETCAP_BITS \
"\20\014LRSWAP\013POWER\012DIGITAL" \
"\011CONNLIST\010UNSOL\07PROC\06STRIPE\05FORMATOV\04AMPOV\03OUTAMP" \
"\02INAMP\01STEREO"
#define PINCAP_BITS "\20\021EAPD\16VREF100\15VREF80" \
"\13VREFGND\12VREF50\11VREFHIZ\07BALANCE\06INPUT" \
"\05OUTPUT\04HEADPHONE\03PRESENCE\02TRIGGER\01IMPEDANCE"
#define ENCODING_BITS "\20\3AC3\2FLOAT32\1PCM"
#define BITSRATES_BITS "\20\x15""32bit\x14""24bit\x13""20bit" \
"\x12""16bit\x11""8bit""\x0c""384kHz\x0b""192kHz\x0a""176.4kHz" \
"\x09""96kHz\x08""88.2kHz\x07""48kHz\x06""44.1kHz\x05""32kHz\x04" \
"22.05kHz\x03""16kHz\x02""11.025kHz\x01""8kHz"
static const char *pin_colors[16] = {
"unknown", "black", "gray", "blue",
"green", "red", "orange", "yellow",
"purple", "pink", "col0a", "col0b",
"col0c", "col0d", "white", "other"};
static const char *pin_conn[4] = {
"jack", "none", "fixed", "combined"};
static const char *pin_conntype[16] = {
"unknown", "1/8", "1/4", "atapi", "rca", "optical",
"digital", "analog", "din", "xlr", "rj-11", "combination",
"con0c", "con0d", "con0e", "other"};
static const char *pin_geo[15] = {
"n/a", "rear", "front", "left",
"right", "top", "bottom", "spec0", "spec1", "spec2",
"loc0a", "loc0b", "loc0c", "loc0d", "loc0f"};
static const char *pin_chass[4] = {
"external", "internal", "separate", "other"};
void
azalia_codec_print_audiofunc(const codec_t *this)
{
uint32_t result;
azalia_widget_print_audio(&this->w[this->audiofunc], "\t");
result = this->w[this->audiofunc].inamp_cap;
DPRINTF(("\tinamp: mute=%u size=%u steps=%u offset=%u\n",
(result & COP_AMPCAP_MUTE) != 0, COP_AMPCAP_STEPSIZE(result),
COP_AMPCAP_NUMSTEPS(result), COP_AMPCAP_OFFSET(result)));
result = this->w[this->audiofunc].outamp_cap;
DPRINTF(("\toutamp: mute=%u size=%u steps=%u offset=%u\n",
(result & COP_AMPCAP_MUTE) != 0, COP_AMPCAP_STEPSIZE(result),
COP_AMPCAP_NUMSTEPS(result), COP_AMPCAP_OFFSET(result)));
azalia_comresp(this, this->audiofunc, CORB_GET_PARAMETER,
COP_GPIO_COUNT, &result);
DPRINTF(("\tgpio: wake=%u unsol=%u gpis=%u gpos=%u gpios=%u\n",
(result & COP_GPIO_WAKE) != 0, (result & COP_GPIO_UNSOL) != 0,
COP_GPIO_GPIS(result), COP_GPIO_GPOS(result),
COP_GPIO_GPIOS(result)));
}
void
azalia_codec_print_groups(const codec_t *this)
{
int i, n;
for (i = 0; i < this->dacs.ngroups; i++) {
printf("%s: dacgroup[%d]:", XNAME(this->az), i);
for (n = 0; n < this->dacs.groups[i].nconv; n++) {
printf(" %2.2x", this->dacs.groups[i].conv[n]);
}
printf("\n");
}
for (i = 0; i < this->adcs.ngroups; i++) {
printf("%s: adcgroup[%d]:", XNAME(this->az), i);
for (n = 0; n < this->adcs.groups[i].nconv; n++) {
printf(" %2.2x", this->adcs.groups[i].conv[n]);
}
printf("\n");
}
}
void
azalia_widget_print_audio(const widget_t *this, const char *lead)
{
printf("%sencodings=%b\n", lead, this->d.audio.encodings,
ENCODING_BITS);
printf("%sPCM formats=%b\n", lead, this->d.audio.bits_rates,
BITSRATES_BITS);
}
void
azalia_widget_print_widget(const widget_t *w, const codec_t *codec)
{
int i;
printf("%s: ", XNAME(codec->az));
printf("%s%2.2x wcap=%b\n", w->type == COP_AWTYPE_PIN_COMPLEX ?
pin_colors[w->d.pin.color] : wtypes[w->type],
w->nid, w->widgetcap, WIDGETCAP_BITS);
if (w->widgetcap & COP_AWCAP_FORMATOV)
azalia_widget_print_audio(w, "\t");
if (w->type == COP_AWTYPE_PIN_COMPLEX)
azalia_widget_print_pin(w);
if (w->type == COP_AWTYPE_VOLUME_KNOB)
printf("\tdelta=%d steps=%d\n",
!!(w->d.volume.cap & COP_VKCAP_DELTA),
COP_VKCAP_NUMSTEPS(w->d.volume.cap));
if ((w->widgetcap & COP_AWCAP_INAMP) &&
(w->widgetcap & COP_AWCAP_AMPOV))
printf("\tinamp: mute=%u size=%u steps=%u offset=%u\n",
(w->inamp_cap & COP_AMPCAP_MUTE) != 0,
COP_AMPCAP_STEPSIZE(w->inamp_cap),
COP_AMPCAP_NUMSTEPS(w->inamp_cap),
COP_AMPCAP_OFFSET(w->inamp_cap));
if ((w->widgetcap & COP_AWCAP_OUTAMP) &&
(w->widgetcap & COP_AWCAP_AMPOV))
printf("\toutamp: mute=%u size=%u steps=%u offset=%u\n",
(w->outamp_cap & COP_AMPCAP_MUTE) != 0,
COP_AMPCAP_STEPSIZE(w->outamp_cap),
COP_AMPCAP_NUMSTEPS(w->outamp_cap),
COP_AMPCAP_OFFSET(w->outamp_cap));
if (w->nconnections > 0) {
printf("\tconnections=0x%x", w->connections[0]);
for (i = 1; i < w->nconnections; i++)
printf(",0x%x", w->connections[i]);
printf("; selected=0x%x\n", w->connections[w->selected]);
}
}
void
azalia_widget_print_pin(const widget_t *this)
{
printf("\tcap=%b\n", this->d.pin.cap, PINCAP_BITS);
printf("\t[%2.2d/%2.2d] ", CORB_CD_ASSOCIATION(this->d.pin.config),
CORB_CD_SEQUENCE(this->d.pin.config));
printf("color=%s ", pin_colors[CORB_CD_COLOR(this->d.pin.config)]);
printf("device=%s ", pin_devices[CORB_CD_DEVICE(this->d.pin.config)]);
printf("conn=%s ", pin_conn[CORB_CD_PORT(this->d.pin.config)]);
printf("conntype=%s\n", pin_conntype[CORB_CD_CONNECTION(this->d.pin.config)]);
printf("\tlocation=%s ", pin_geo[CORB_CD_LOC_GEO(this->d.pin.config)]);
printf("chassis=%s ", pin_chass[CORB_CD_LOC_CHASS(this->d.pin.config)]);
printf("special=");
if (CORB_CD_LOC_GEO(this->d.pin.config) == CORB_CD_LOC_SPEC0) {
if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_EXTERNAL)
printf("rear-panel");
else if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_INTERNAL)
printf("riser");
else if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_LOC_OTHER)
printf("mobile-lid-internal");
} else if (CORB_CD_LOC_GEO(this->d.pin.config) == CORB_CD_LOC_SPEC1) {
if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_EXTERNAL)
printf("drive-bay");
else if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_INTERNAL)
printf("hdmi");
else if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_LOC_OTHER)
printf("mobile-lid-external");
} else if (CORB_CD_LOC_GEO(this->d.pin.config) == CORB_CD_LOC_SPEC2) {
if (CORB_CD_LOC_CHASS(this->d.pin.config) == CORB_CD_INTERNAL)
printf("atapi");
} else
printf("none");
printf("\n");
}
#else /* AZALIA_DEBUG */
void
azalia_codec_print_audiofunc(const codec_t *this) {}
void
azalia_codec_print_groups(const codec_t *this) {}
void
azalia_widget_print_audio(const widget_t *this, const char *lead) {}
void
azalia_widget_print_widget(const widget_t *w, const codec_t *codec) {}
void
azalia_widget_print_pin(const widget_t *this) {}
#endif /* AZALIA_DEBUG */
/* ================================================================
* Stream functions
* ================================================================ */
int
azalia_stream_init(stream_t *this, azalia_t *az, int regindex, int strnum,
int dir)
{
int err;
this->az = az;
this->regbase = HDA_SD_BASE + regindex * HDA_SD_SIZE;
this->intr_bit = 1 << regindex;
this->number = strnum;
this->dir = dir;
/* setup BDL buffers */
err = azalia_alloc_dmamem(az, sizeof(bdlist_entry_t) * HDA_BDL_MAX,
128, &this->bdlist);
if (err) {
printf("%s: can't allocate a BDL buffer\n", XNAME(az));
return err;
}
return 0;
}
int
azalia_stream_reset(stream_t *this)
{
int i;
uint16_t ctl;
uint8_t sts;
/* Make sure RUN bit is zero before resetting */
ctl = STR_READ_2(this, CTL);
ctl &= ~HDA_SD_CTL_RUN;
STR_WRITE_2(this, CTL, ctl);
DELAY(40);
/* Start reset and wait for chip to enter. */
ctl = STR_READ_2(this, CTL);
STR_WRITE_2(this, CTL, ctl | HDA_SD_CTL_SRST);
for (i = 5000; i > 0; i--) {
DELAY(10);
ctl = STR_READ_2(this, CTL);
if (ctl & HDA_SD_CTL_SRST)
break;
}
if (i == 0) {
DPRINTF(("%s: stream reset failure 1\n", XNAME(this->az)));
return -1;
}
/* Clear reset and wait for chip to finish */
STR_WRITE_2(this, CTL, ctl & ~HDA_SD_CTL_SRST);
for (i = 5000; i > 0; i--) {
DELAY(10);
ctl = STR_READ_2(this, CTL);
if ((ctl & HDA_SD_CTL_SRST) == 0)
break;
}
if (i == 0) {
DPRINTF(("%s: stream reset failure 2\n", XNAME(this->az)));
return -1;
}
sts = STR_READ_1(this, STS);
sts |= HDA_SD_STS_DESE | HDA_SD_STS_FIFOE | HDA_SD_STS_BCIS;
STR_WRITE_1(this, STS, sts);
return (0);
}
int
azalia_stream_start(stream_t *this)
{
bdlist_entry_t *bdlist;
bus_addr_t dmaaddr, dmaend;
int err, index;
uint32_t intctl;
uint8_t ctl2;
err = azalia_stream_reset(this);
if (err) {
DPRINTF(("%s: stream reset failed\n", "azalia"));
return err;
}
STR_WRITE_4(this, BDPL, 0);
STR_WRITE_4(this, BDPU, 0);
/* setup BDL */
dmaaddr = AZALIA_DMA_DMAADDR(&this->buffer);
dmaend = dmaaddr + this->bufsize;
bdlist = (bdlist_entry_t*)this->bdlist.addr;
for (index = 0; index < HDA_BDL_MAX; index++) {
bdlist[index].low = htole32(dmaaddr);
bdlist[index].high = htole32(PTR_UPPER32(dmaaddr));
bdlist[index].length = htole32(this->blk);
bdlist[index].flags = htole32(BDLIST_ENTRY_IOC);
dmaaddr += this->blk;
if (dmaaddr >= dmaend) {
index++;
break;
}
}
DPRINTFN(1, ("%s: size=%d fmt=0x%4.4x index=%d\n",
__func__, this->bufsize, this->fmt, index));
dmaaddr = AZALIA_DMA_DMAADDR(&this->bdlist);
STR_WRITE_4(this, BDPL, dmaaddr);
STR_WRITE_4(this, BDPU, PTR_UPPER32(dmaaddr));
STR_WRITE_2(this, LVI, (index - 1) & HDA_SD_LVI_LVI);
ctl2 = STR_READ_1(this, CTL2);
STR_WRITE_1(this, CTL2,
(ctl2 & ~HDA_SD_CTL2_STRM) | (this->number << HDA_SD_CTL2_STRM_SHIFT));
STR_WRITE_4(this, CBL, this->bufsize);
STR_WRITE_2(this, FMT, this->fmt);
err = azalia_codec_connect_stream(this);
if (err)
return EINVAL;
intctl = AZ_READ_4(this->az, INTCTL);
intctl |= this->intr_bit;
AZ_WRITE_4(this->az, INTCTL, intctl);
STR_WRITE_1(this, CTL, STR_READ_1(this, CTL) |
HDA_SD_CTL_DEIE | HDA_SD_CTL_FEIE | HDA_SD_CTL_IOCE |
HDA_SD_CTL_RUN);
return (0);
}
int
azalia_stream_halt(stream_t *this)
{
uint16_t ctl;
ctl = STR_READ_2(this, CTL);
ctl &= ~(HDA_SD_CTL_DEIE | HDA_SD_CTL_FEIE | HDA_SD_CTL_IOCE | HDA_SD_CTL_RUN);
STR_WRITE_2(this, CTL, ctl);
AZ_WRITE_4(this->az, INTCTL,
AZ_READ_4(this->az, INTCTL) & ~this->intr_bit);
azalia_codec_disconnect_stream(this);
return (0);
}
#define HDA_SD_STS_BITS "\20\3BCIS\4FIFOE\5DESE\6FIFORDY"
int
azalia_stream_intr(stream_t *this)
{
unsigned int lpib, fifos, hwpos, cnt;
u_int8_t sts;
sts = STR_READ_1(this, STS);
STR_WRITE_1(this, STS, sts |
HDA_SD_STS_DESE | HDA_SD_STS_FIFOE | HDA_SD_STS_BCIS);
if (sts & (HDA_SD_STS_DESE | HDA_SD_STS_FIFOE))
DPRINTF(("%s: stream %d: sts=%b\n", XNAME(this->az),
this->number, sts, HDA_SD_STS_BITS));
if (sts & HDA_SD_STS_BCIS) {
lpib = STR_READ_4(this, LPIB);
fifos = STR_READ_2(this, FIFOS);
if (fifos & 1)
fifos++;
hwpos = lpib;
if (this->dir == AUMODE_PLAY)
hwpos += fifos + 1;
if (hwpos >= this->bufsize)
hwpos -= this->bufsize;
DPRINTFN(2, ("%s: stream %d, pos = %d -> %d, "
"lpib = %u, fifos = %u\n", __func__,
this->number, this->swpos, hwpos, lpib, fifos));
cnt = 0;
while (hwpos - this->swpos >= this->blk) {
this->intr(this->intr_arg);
this->swpos += this->blk;
if (this->swpos == this->bufsize)
this->swpos = 0;
cnt++;
}
if (cnt != 1) {
DPRINTF(("%s: stream %d: hwpos %u, %u intrs\n",
__func__, this->number, this->swpos, cnt));
}
}
return (1);
}
/* ================================================================
* MI audio entries
* ================================================================ */
int
azalia_open(void *v, int flags)
{
azalia_t *az;
codec_t *codec;
DPRINTFN(1, ("%s: flags=0x%x\n", __func__, flags));
az = v;
codec = &az->codecs[az->codecno];
if ((flags & FWRITE) && codec->dacs.ngroups == 0)
return ENODEV;
if ((flags & FREAD) && codec->adcs.ngroups == 0)
return ENODEV;
codec->running++;
return 0;
}
void
azalia_close(void *v)
{
azalia_t *az;
codec_t *codec;
DPRINTFN(1, ("%s\n", __func__));
az = v;
codec = &az->codecs[az->codecno];
codec->running--;
}
int
azalia_match_format(codec_t *codec, int mode, audio_params_t *par)
{
int i;
DPRINTFN(1, ("%s: mode=%d, want: enc=%d, prec=%d, chans=%d\n", __func__,
mode, par->encoding, par->precision, par->channels));
for (i = 0; i < codec->nformats; i++) {
if (mode != codec->formats[i].mode)
continue;
if (par->encoding != codec->formats[i].encoding)
continue;
if (par->precision != codec->formats[i].precision)
continue;
if (par->channels != codec->formats[i].channels)
continue;
break;
}
DPRINTFN(1, ("%s: return: enc=%d, prec=%d, chans=%d\n", __func__,
codec->formats[i].encoding, codec->formats[i].precision,
codec->formats[i].channels));
return (i);
}
int
azalia_set_params_sub(codec_t *codec, int mode, audio_params_t *par)
{
char *cmode;
int i, j;
uint ochan, oenc, opre;
if (mode == AUMODE_PLAY)
cmode = "play";
else
cmode = "record";
ochan = par->channels;
oenc = par->encoding;
opre = par->precision;
if ((mode == AUMODE_PLAY && codec->dacs.ngroups == 0) ||
(mode == AUMODE_RECORD && codec->adcs.ngroups == 0))
return 0;
i = azalia_match_format(codec, mode, par);
if (i == codec->nformats && (par->precision != 16 || par->encoding !=
AUDIO_ENCODING_SLINEAR_LE)) {
/* try with default encoding/precision */
par->encoding = AUDIO_ENCODING_SLINEAR_LE;
par->precision = 16;
i = azalia_match_format(codec, mode, par);
}
if (i == codec->nformats && par->channels != 2) {
/* try with default channels */
par->encoding = oenc;
par->precision = opre;
par->channels = 2;
i = azalia_match_format(codec, mode, par);
}
/* try with default everything */
if (i == codec->nformats) {
par->encoding = AUDIO_ENCODING_SLINEAR_LE;
par->precision = 16;
par->channels = 2;
i = azalia_match_format(codec, mode, par);
if (i == codec->nformats) {
DPRINTF(("%s: can't find %s format %u/%u/%u\n",
__func__, cmode, par->encoding,
par->precision, par->channels));
return EINVAL;
}
}
if (codec->formats[i].frequency_type == 0) {
DPRINTF(("%s: matched %s format %d has 0 frequencies\n",
__func__, cmode, i));
return EINVAL;
}
for (j = 0; j < codec->formats[i].frequency_type; j++) {
if (par->sample_rate != codec->formats[i].frequency[j])
continue;
break;
}
if (j == codec->formats[i].frequency_type) {
/* try again with default */
par->sample_rate = 48000;
for (j = 0; j < codec->formats[i].frequency_type; j++) {
if (par->sample_rate != codec->formats[i].frequency[j])
continue;
break;
}
if (j == codec->formats[i].frequency_type) {
DPRINTF(("%s: can't find %s rate %lu\n",
__func__, cmode, par->sample_rate));
return EINVAL;
}
}
par->bps = AUDIO_BPS(par->precision);
par->msb = 1;
return (0);
}
int
azalia_set_params(void *v, int smode, int umode, audio_params_t *p,
audio_params_t *r)
{
azalia_t *az;
codec_t *codec;
int ret;
az = v;
codec = &az->codecs[az->codecno];
if (codec->nformats == 0) {
DPRINTF(("%s: codec has no formats\n", __func__));
return EINVAL;
}
if (smode & AUMODE_RECORD && r != NULL) {
ret = azalia_set_params_sub(codec, AUMODE_RECORD, r);
if (ret)
return (ret);
}
if (smode & AUMODE_PLAY && p != NULL) {
ret = azalia_set_params_sub(codec, AUMODE_PLAY, p);
if (ret)
return (ret);
}
return (0);
}
int
azalia_round_blocksize(void *v, int blk)
{
azalia_t *az;
size_t size;
blk &= ~0x7f; /* must be multiple of 128 */
if (blk <= 0)
blk = 128;
/* number of blocks must be <= HDA_BDL_MAX */
az = v;
size = az->pstream.buffer.size;
if (size > HDA_BDL_MAX * blk) {
blk = size / HDA_BDL_MAX;
if (blk & 0x7f)
blk = (blk + 0x7f) & ~0x7f;
}
DPRINTFN(1,("%s: resultant block size = %d\n", __func__, blk));
return blk;
}
int
azalia_halt_output(void *v)
{
azalia_t *az;
DPRINTFN(1, ("%s\n", __func__));
az = v;
return azalia_stream_halt(&az->pstream);
}
int
azalia_halt_input(void *v)
{
azalia_t *az;
DPRINTFN(1, ("%s\n", __func__));
az = v;
return azalia_stream_halt(&az->rstream);
}
int
azalia_set_port(void *v, mixer_ctrl_t *mc)
{
azalia_t *az;
codec_t *co;
const mixer_item_t *m;
az = v;
co = &az->codecs[az->codecno];
if (mc->dev < 0 || mc->dev >= co->nmixers)
return EINVAL;
m = &co->mixers[mc->dev];
if (mc->type != m->devinfo.type)
return EINVAL;
return azalia_mixer_set(co, m->nid, m->target, mc);
}
int
azalia_get_port(void *v, mixer_ctrl_t *mc)
{
azalia_t *az;
codec_t *co;
const mixer_item_t *m;
az = v;
co = &az->codecs[az->codecno];
if (mc->dev < 0 || mc->dev >= co->nmixers)
return EINVAL;
m = &co->mixers[mc->dev];
mc->type = m->devinfo.type;
return azalia_mixer_get(co, m->nid, m->target, mc);
}
int
azalia_query_devinfo(void *v, mixer_devinfo_t *mdev)
{
azalia_t *az;
const codec_t *co;
az = v;
co = &az->codecs[az->codecno];
if (mdev->index < 0 || mdev->index >= co->nmixers)
return ENXIO;
*mdev = co->mixers[mdev->index].devinfo;
return 0;
}
void *
azalia_allocm(void *v, int dir, size_t size, int pool, int flags)
{
azalia_t *az;
stream_t *stream;
int err;
az = v;
stream = dir == AUMODE_PLAY ? &az->pstream : &az->rstream;
err = azalia_alloc_dmamem(az, size, 128, &stream->buffer);
if (err) {
printf("%s: allocm failed\n", az->dev.dv_xname);
return NULL;
}
return stream->buffer.addr;
}
void
azalia_freem(void *v, void *addr, int pool)
{
azalia_t *az;
stream_t *stream;
az = v;
if (addr == az->pstream.buffer.addr) {
stream = &az->pstream;
} else if (addr == az->rstream.buffer.addr) {
stream = &az->rstream;
} else {
return;
}
azalia_free_dmamem(az, &stream->buffer);
}
size_t
azalia_round_buffersize(void *v, int dir, size_t size)
{
size &= ~0x7f; /* must be multiple of 128 */
if (size <= 0)
size = 128;
return size;
}
int
azalia_get_props(void *v)
{
return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
}
int
azalia_trigger_output(void *v, void *start, void *end, int blk,
void (*intr)(void *), void *arg, audio_params_t *param)
{
azalia_t *az;
int err;
uint16_t fmt;
az = v;
if (az->codecs[az->codecno].dacs.ngroups == 0) {
DPRINTF(("%s: can't play without a DAC\n", __func__));
return ENXIO;
}
err = azalia_params2fmt(param, &fmt);
if (err)
return(EINVAL);
az->pstream.bufsize = (caddr_t)end - (caddr_t)start;
az->pstream.blk = blk;
az->pstream.fmt = fmt;
az->pstream.intr = intr;
az->pstream.intr_arg = arg;
az->pstream.swpos = 0;
return azalia_stream_start(&az->pstream);
}
int
azalia_trigger_input(void *v, void *start, void *end, int blk,
void (*intr)(void *), void *arg, audio_params_t *param)
{
azalia_t *az;
int err;
uint16_t fmt;
DPRINTFN(1, ("%s: this=%p start=%p end=%p blk=%d {enc=%u %uch %ubit %luHz}\n",
__func__, v, start, end, blk, param->encoding, param->channels,
param->precision, param->sample_rate));
az = v;
if (az->codecs[az->codecno].adcs.ngroups == 0) {
DPRINTF(("%s: can't record without an ADC\n", __func__));
return ENXIO;
}
err = azalia_params2fmt(param, &fmt);
if (err)
return(EINVAL);
az->rstream.bufsize = (caddr_t)end - (caddr_t)start;
az->rstream.blk = blk;
az->rstream.fmt = fmt;
az->rstream.intr = intr;
az->rstream.intr_arg = arg;
az->rstream.swpos = 0;
return azalia_stream_start(&az->rstream);
}
/* --------------------------------
* helpers for MI audio functions
* -------------------------------- */
int
azalia_params2fmt(const audio_params_t *param, uint16_t *fmt)
{
uint16_t ret;
ret = 0;
if (param->channels > HDA_MAX_CHANNELS) {
printf("%s: too many channels: %u\n", __func__,
param->channels);
return EINVAL;
}
DPRINTFN(1, ("%s: prec=%d, chan=%d, rate=%ld\n", __func__,
param->precision, param->channels, param->sample_rate));
/* XXX: can channels be >2 ? */
ret |= param->channels - 1;
switch (param->precision) {
case 8:
ret |= HDA_SD_FMT_BITS_8_16;
break;
case 16:
ret |= HDA_SD_FMT_BITS_16_16;
break;
case 20:
ret |= HDA_SD_FMT_BITS_20_32;
break;
case 24:
ret |= HDA_SD_FMT_BITS_24_32;
break;
case 32:
ret |= HDA_SD_FMT_BITS_32_32;
break;
}
switch (param->sample_rate) {
default:
case 384000:
printf("%s: invalid sample_rate: %lu\n", __func__,
param->sample_rate);
return EINVAL;
case 192000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X4 | HDA_SD_FMT_DIV_BY1;
break;
case 176400:
ret |= HDA_SD_FMT_BASE_44 | HDA_SD_FMT_MULT_X4 | HDA_SD_FMT_DIV_BY1;
break;
case 96000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X2 | HDA_SD_FMT_DIV_BY1;
break;
case 88200:
ret |= HDA_SD_FMT_BASE_44 | HDA_SD_FMT_MULT_X2 | HDA_SD_FMT_DIV_BY1;
break;
case 48000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY1;
break;
case 44100:
ret |= HDA_SD_FMT_BASE_44 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY1;
break;
case 32000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X2 | HDA_SD_FMT_DIV_BY3;
break;
case 22050:
ret |= HDA_SD_FMT_BASE_44 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY2;
break;
case 16000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY3;
break;
case 11025:
ret |= HDA_SD_FMT_BASE_44 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY4;
break;
case 8000:
ret |= HDA_SD_FMT_BASE_48 | HDA_SD_FMT_MULT_X1 | HDA_SD_FMT_DIV_BY6;
break;
}
*fmt = ret;
return 0;
}
|