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
|
/*
* Intel i810 and friends ICH driver for Linux
* Alan Cox <alan@redhat.com>
*
* Built from:
* Low level code: Zach Brown (original nonworking i810 OSS driver)
* Jaroslav Kysela <perex@suse.cz> (working ALSA driver)
*
* Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
* Extended by: Zach Brown <zab@redhat.com>
* and others..
*
* Hardware Provided By:
* Analog Devices (A major AC97 codec maker)
* Intel Corp (you've probably heard of them already)
*
* AC97 clues and assistance provided by
* Analog Devices
* Zach 'Fufu' Brown
* Jeff Garzik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* Intel 810 theory of operation
*
* The chipset provides three DMA channels that talk to an AC97
* CODEC (AC97 is a digital/analog mixer standard). At its simplest
* you get 48Khz audio with basic volume and mixer controls. At the
* best you get rate adaption in the codec. We set the card up so
* that we never take completion interrupts but instead keep the card
* chasing its tail around a ring buffer. This is needed for mmap
* mode audio and happens to work rather well for non-mmap modes too.
*
* The board has one output channel for PCM audio (supported) and
* a stereo line in and mono microphone input. Again these are normally
* locked to 48Khz only. Right now recording is not finished.
*
* There is no midi support, no synth support. Use timidity. To get
* esd working you need to use esd -r 48000 as it won't probe 48KHz
* by default. mpg123 can't handle 48Khz only audio so use xmms.
*
* Fix The Sound On Dell
*
* Not everyone uses 48KHz. We know of no way to detect this reliably
* and certainly not to get the right data. If your i810 audio sounds
* stupid you may need to investigate other speeds. According to Analog
* they tend to use a 14.318MHz clock which gives you a base rate of
* 41194Hz.
*
* This is available via the 'ftsodell=1' option.
*
* If you need to force a specific rate set the clocking= option
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/sound.h>
#include <linux/slab.h>
#include <linux/soundcard.h>
#include <linux/pci.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/smp_lock.h>
#include <linux/ac97_codec.h>
#include <linux/wrapper.h>
#include <asm/uaccess.h>
#include <asm/hardirq.h>
#ifndef PCI_DEVICE_ID_INTEL_82801
#define PCI_DEVICE_ID_INTEL_82801 0x2415
#endif
#ifndef PCI_DEVICE_ID_INTEL_82901
#define PCI_DEVICE_ID_INTEL_82901 0x2425
#endif
#ifndef PCI_DEVICE_ID_INTEL_ICH2
#define PCI_DEVICE_ID_INTEL_ICH2 0x2445
#endif
#ifndef PCI_DEVICE_ID_INTEL_440MX
#define PCI_DEVICE_ID_INTEL_440MX 0x7195
#endif
static int ftsodell=0;
static unsigned int clocking=48000;
#define ADC_RUNNING 1
#define DAC_RUNNING 2
#define I810_FMT_16BIT 1
#define I810_FMT_STEREO 2
#define I810_FMT_MASK 3
/* the 810's array of pointers to data buffers */
struct sg_item {
#define BUSADDR_MASK 0xFFFFFFFE
u32 busaddr;
#define CON_IOC 0x80000000 /* interrupt on completion */
#define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
#define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
u32 control;
};
/* an instance of the i810 channel */
#define SG_LEN 32
struct i810_channel
{
/* these sg guys should probably be allocated
seperately as nocache. Must be 8 byte aligned */
struct sg_item sg[SG_LEN]; /* 32*8 */
u32 offset; /* 4 */
u32 port; /* 4 */
u32 used;
u32 num;
};
/*
* we have 3 seperate dma engines. pcm in, pcm out, and mic.
* each dma engine has controlling registers. These goofy
* names are from the datasheet, but make it easy to write
* code while leafing through it.
*/
#define ENUM_ENGINE(PRE,DIG) \
enum { \
PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */ \
PRE##_CIV = 0x##DIG##4, /* Current Index Value */ \
PRE##_LVI = 0x##DIG##5, /* Last Valid Index */ \
PRE##_SR = 0x##DIG##6, /* Status Register */ \
PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */ \
PRE##_PIV = 0x##DIG##a, /* Prefetched Index Value */ \
PRE##_CR = 0x##DIG##b /* Control Register */ \
}
ENUM_ENGINE(OFF,0); /* Offsets */
ENUM_ENGINE(PI,0); /* PCM In */
ENUM_ENGINE(PO,1); /* PCM Out */
ENUM_ENGINE(MC,2); /* Mic In */
enum {
GLOB_CNT = 0x2c, /* Global Control */
GLOB_STA = 0x30, /* Global Status */
CAS = 0x34 /* Codec Write Semaphore Register */
};
/* interrupts for a dma engine */
#define DMA_INT_FIFO (1<<4) /* fifo under/over flow */
#define DMA_INT_COMPLETE (1<<3) /* buffer read/write complete and ioc set */
#define DMA_INT_LVI (1<<2) /* last valid done */
#define DMA_INT_CELV (1<<1) /* last valid is current */
#define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
/* interrupts for the whole chip */
#define INT_SEC (1<<11)
#define INT_PRI (1<<10)
#define INT_MC (1<<7)
#define INT_PO (1<<6)
#define INT_PI (1<<5)
#define INT_MO (1<<2)
#define INT_NI (1<<1)
#define INT_GPI (1<<0)
#define INT_MASK (INT_SEC|INT_PRI|INT_MC|INT_PO|INT_PI|INT_MO|INT_NI|INT_GPI)
#define DRIVER_VERSION "0.01"
/* magic numbers to protect our data structures */
#define I810_CARD_MAGIC 0x5072696E /* "Prin" */
#define I810_STATE_MAGIC 0x63657373 /* "cess" */
#define I810_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
#define NR_HW_CH 3
/* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
#define NR_AC97 2
/* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
/* stream at a minimum for this card to be happy */
static const unsigned sample_size[] = { 1, 2, 2, 4 };
/* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
/* values are one less than might be expected */
static const unsigned sample_shift[] = { -1, 0, 0, 1 };
enum {
ICH82801AA = 0,
ICH82901AB,
INTEL440MX,
INTELICH2,
};
static char * card_names[] = {
"Intel ICH 82801AA",
"Intel ICH 82901AB",
"Intel 440MX",
"Intel ICH2"
};
static struct pci_device_id i810_pci_tbl [] __initdata = {
{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82801AA},
{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82901,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, ICH82901AB},
{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_440MX,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTEL440MX},
{PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH2,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, INTELICH2},
{0,}
};
MODULE_DEVICE_TABLE (pci, i810_pci_tbl);
/* "software" or virtual channel, an instance of opened /dev/dsp */
struct i810_state {
unsigned int magic;
struct i810_card *card; /* Card info */
/* single open lock mechanism, only used for recording */
struct semaphore open_sem;
wait_queue_head_t open_wait;
/* file mode */
mode_t open_mode;
/* virtual channel number */
int virt;
struct dmabuf {
/* wave sample stuff */
unsigned int rate;
unsigned char fmt, enable;
/* hardware channel */
struct i810_channel *read_channel;
struct i810_channel *write_channel;
/* OSS buffer management stuff */
void *rawbuf;
dma_addr_t dma_handle;
unsigned buforder;
unsigned numfrag;
unsigned fragshift;
/* our buffer acts like a circular ring */
unsigned hwptr; /* where dma last started, updated by update_ptr */
unsigned swptr; /* where driver last clear/filled, updated by read/write */
int count; /* bytes to be consumed or been generated by dma machine */
unsigned total_bytes; /* total bytes dmaed by hardware */
unsigned error; /* number of over/underruns */
wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
/* redundant, but makes calculations easier */
unsigned fragsize;
unsigned dmasize;
unsigned fragsamples;
/* OSS stuff */
unsigned mapped:1;
unsigned ready:1;
unsigned endcleared:1;
unsigned update_flag;
unsigned ossfragshift;
int ossmaxfrags;
unsigned subdivision;
} dmabuf;
};
struct i810_card {
struct i810_channel channel[3];
unsigned int magic;
/* We keep i810 cards in a linked list */
struct i810_card *next;
/* The i810 has a certain amount of cross channel interaction
so we use a single per card lock */
spinlock_t lock;
/* PCI device stuff */
struct pci_dev * pci_dev;
u16 pci_id;
/* soundcore stuff */
int dev_audio;
/* structures for abstraction of hardware facilities, codecs, banks and channels*/
struct ac97_codec *ac97_codec[NR_AC97];
struct i810_state *states[NR_HW_CH];
u16 ac97_features;
/* hardware resources */
unsigned long iobase;
unsigned long ac97base;
u32 irq;
/* Function support */
struct i810_channel *(*alloc_pcm_channel)(struct i810_card *);
struct i810_channel *(*alloc_rec_pcm_channel)(struct i810_card *);
struct i810_channel *(*alloc_rec_mic_channel)(struct i810_card *);
void (*free_pcm_channel)(struct i810_card *, int chan);
};
static struct i810_card *devs = NULL;
static int i810_open_mixdev(struct inode *inode, struct file *file);
static int i810_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg);
static loff_t i810_llseek(struct file *file, loff_t offset, int origin);
extern __inline__ unsigned ld2(unsigned int x)
{
unsigned r = 0;
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 4) {
x >>= 2;
r += 2;
}
if (x >= 2)
r++;
return r;
}
static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg);
static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
static struct i810_channel *i810_alloc_pcm_channel(struct i810_card *card)
{
if(card->channel[1].used==1)
return NULL;
card->channel[1].used=1;
return &card->channel[1];
}
static struct i810_channel *i810_alloc_rec_pcm_channel(struct i810_card *card)
{
if(card->channel[0].used==1)
return NULL;
card->channel[0].used=1;
return &card->channel[0];
}
static struct i810_channel *i810_alloc_rec_mic_channel(struct i810_card *card)
{
if(card->channel[2].used==1)
return NULL;
card->channel[2].used=1;
return &card->channel[2];
}
static void i810_free_pcm_channel(struct i810_card *card, int channel)
{
card->channel[channel].used=0;
}
/* set playback sample rate */
static unsigned int i810_set_dac_rate(struct i810_state * state, unsigned int rate)
{
struct dmabuf *dmabuf = &state->dmabuf;
u32 dacp, new_rate;
struct ac97_codec *codec=state->card->ac97_codec[0];
if(!(state->card->ac97_features&0x0001))
{
dmabuf->rate = clocking;
return clocking;
}
if (rate > 48000)
rate = 48000;
if (rate < 8000)
rate = 8000;
dmabuf->rate = rate;
/*
* Adjust for misclocked crap
*/
rate = ( rate * clocking)/48000;
if(rate != i810_ac97_get(codec, AC97_PCM_FRONT_DAC_RATE))
{
/* Power down the DAC */
dacp=i810_ac97_get(codec, AC97_POWER_CONTROL);
i810_ac97_set(codec, AC97_POWER_CONTROL, dacp|0x0200);
/* Load the rate and read the effective rate */
i810_ac97_set(codec, AC97_PCM_FRONT_DAC_RATE, rate);
new_rate=i810_ac97_get(codec, AC97_PCM_FRONT_DAC_RATE);
/* Power it back up */
i810_ac97_set(codec, AC97_POWER_CONTROL, dacp);
if(new_rate != rate) {
dmabuf->rate = (new_rate * 48000)/clocking;
rate = new_rate;
}
}
#ifdef DEBUG
printk("i810_audio: called i810_set_dac_rate : rate = %d/%d\n", dmabuf->rate, rate);
#endif
return dmabuf->rate;
}
/* set recording sample rate */
static unsigned int i810_set_adc_rate(struct i810_state * state, unsigned int rate)
{
struct dmabuf *dmabuf = &state->dmabuf;
u32 dacp, new_rate;
struct ac97_codec *codec=state->card->ac97_codec[0];
if(!(state->card->ac97_features&0x0001))
{
dmabuf->rate = clocking;
return clocking;
}
if (rate > 48000)
rate = 48000;
if (rate < 8000)
rate = 8000;
dmabuf->rate = rate;
/*
* Adjust for misclocked crap
*/
rate = ( rate * clocking)/48000;
if(rate != i810_ac97_get(codec, AC97_PCM_LR_DAC_RATE))
{
/* Power down the ADC */
dacp=i810_ac97_get(codec, AC97_POWER_CONTROL);
i810_ac97_set(codec, AC97_POWER_CONTROL, dacp|0x0100);
/* Load the rate and read the effective rate */
i810_ac97_set(codec, AC97_PCM_LR_DAC_RATE, rate);
new_rate=i810_ac97_get(codec, AC97_PCM_LR_DAC_RATE);
/* Power it back up */
i810_ac97_set(codec, AC97_POWER_CONTROL, dacp);
if(new_rate != rate) {
dmabuf->rate = (new_rate * 48000)/clocking;
rate = new_rate;
}
}
#ifdef DEBUG
printk("i810_audio: called i810_set_adc_rate : rate = %d/%d\n", dmabuf->rate, rate);
#endif
return dmabuf->rate;
}
/* prepare channel attributes for playback */
static void i810_play_setup(struct i810_state *state)
{
// struct dmabuf *dmabuf = &state->dmabuf;
// struct i810_channel *channel = dmabuf->channel;
/* Fixed format. .. */
//if (dmabuf->fmt & I810_FMT_16BIT)
//if (dmabuf->fmt & I810_FMT_STEREO)
}
/* prepare channel attributes for recording */
static void i810_rec_setup(struct i810_state *state)
{
// u16 w;
// struct i810_card *card = state->card;
// struct dmabuf *dmabuf = &state->dmabuf;
// struct i810_channel *channel = dmabuf->channel;
/* Enable AC-97 ADC (capture) */
// if (dmabuf->fmt & I810_FMT_16BIT) {
// if (dmabuf->fmt & I810_FMT_STEREO)
}
/* get current playback/recording dma buffer pointer (byte offset from LBA),
called with spinlock held! */
extern __inline__ unsigned i810_get_dma_addr(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
unsigned int civ, offset;
struct i810_channel *c;
if (!dmabuf->enable)
return 0;
if (dmabuf->enable & DAC_RUNNING)
c = dmabuf->write_channel;
else if (dmabuf->enable & ADC_RUNNING)
c = dmabuf->read_channel;
else {
printk("i810_audio: invalid dmabuf->enable state in get_dma_addr\n");
return 0;
}
do {
civ = inb(state->card->iobase+c->port+OFF_CIV);
offset = (civ + 1) * (dmabuf->dmasize/SG_LEN) -
2 * inw(state->card->iobase+c->port+OFF_PICB);
/* CIV changed before we read PICB (very seldom) ?
* then PICB was rubbish, so try again */
} while (civ != inb(state->card->iobase+c->port+OFF_CIV));
return offset;
}
static void resync_dma_ptrs(struct i810_state *state, int rec)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_channel *c;
int offset;
if(rec) {
c = dmabuf->read_channel;
} else {
c = dmabuf->write_channel;
}
offset = inb(state->card->iobase+c->port+OFF_CIV);
offset *= (dmabuf->dmasize/SG_LEN);
dmabuf->hwptr=dmabuf->swptr = offset;
}
/* Stop recording (lock held) */
extern __inline__ void __stop_adc(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_card *card = state->card;
dmabuf->enable &= ~ADC_RUNNING;
outb(0, card->iobase + PI_CR);
}
static void stop_adc(struct i810_state *state)
{
struct i810_card *card = state->card;
unsigned long flags;
spin_lock_irqsave(&card->lock, flags);
__stop_adc(state);
spin_unlock_irqrestore(&card->lock, flags);
}
static void start_adc(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_card *card = state->card;
unsigned long flags;
spin_lock_irqsave(&card->lock, flags);
if ((dmabuf->mapped || dmabuf->count < (signed)dmabuf->dmasize) && dmabuf->ready) {
dmabuf->enable |= ADC_RUNNING;
outb((1<<4) | 1<<2 | 1, card->iobase + PI_CR);
}
spin_unlock_irqrestore(&card->lock, flags);
}
/* stop playback (lock held) */
extern __inline__ void __stop_dac(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_card *card = state->card;
dmabuf->enable &= ~DAC_RUNNING;
outb(0, card->iobase + PO_CR);
}
static void stop_dac(struct i810_state *state)
{
struct i810_card *card = state->card;
unsigned long flags;
spin_lock_irqsave(&card->lock, flags);
__stop_dac(state);
spin_unlock_irqrestore(&card->lock, flags);
}
static void start_dac(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_card *card = state->card;
unsigned long flags;
spin_lock_irqsave(&card->lock, flags);
if ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) {
dmabuf->enable |= DAC_RUNNING;
outb((1<<4) | 1<<2 | 1, card->iobase + PO_CR);
}
spin_unlock_irqrestore(&card->lock, flags);
}
#define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
#define DMABUF_MINORDER 1
/* allocate DMA buffer, playback and recording buffer should be allocated seperately */
static int alloc_dmabuf(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
void *rawbuf= NULL;
int order;
struct page *page, *pend;
/* alloc as big a chunk as we can, FIXME: is this necessary ?? */
for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
PAGE_SIZE << order,
&dmabuf->dma_handle)))
break;
if (!rawbuf)
return -ENOMEM;
#ifdef DEBUG
printk("i810_audio: allocated %ld (order = %d) bytes at %p\n",
PAGE_SIZE << order, order, rawbuf);
#endif
dmabuf->ready = dmabuf->mapped = 0;
dmabuf->rawbuf = rawbuf;
dmabuf->buforder = order;
/* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
for (page = virt_to_page(rawbuf); page <= pend; page++)
mem_map_reserve(page);
return 0;
}
/* free DMA buffer */
static void dealloc_dmabuf(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct page *page, *pend;
if (dmabuf->rawbuf) {
/* undo marking the pages as reserved */
pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
mem_map_unreserve(page);
pci_free_consistent(state->card->pci_dev, PAGE_SIZE << dmabuf->buforder,
dmabuf->rawbuf, dmabuf->dma_handle);
}
dmabuf->rawbuf = NULL;
dmabuf->mapped = dmabuf->ready = 0;
}
static int prog_dmabuf(struct i810_state *state, unsigned rec)
{
struct dmabuf *dmabuf = &state->dmabuf;
struct i810_channel *c;
struct sg_item *sg;
unsigned bytepersec;
unsigned bufsize;
unsigned long flags;
int ret;
unsigned fragsize;
int i;
spin_lock_irqsave(&state->card->lock, flags);
resync_dma_ptrs(state, rec);
dmabuf->total_bytes = 0;
dmabuf->count = dmabuf->error = 0;
spin_unlock_irqrestore(&state->card->lock, flags);
/* allocate DMA buffer if not allocated yet */
if (!dmabuf->rawbuf)
if ((ret = alloc_dmabuf(state)))
return ret;
/* FIXME: figure out all this OSS fragment stuff */
/* sample_shift is for 16 byte samples, add an extra shift for bytes */
bytepersec = dmabuf->rate << (sample_shift[dmabuf->fmt] + 1);
bufsize = PAGE_SIZE << dmabuf->buforder;
if (dmabuf->ossfragshift) {
if ((1000 << dmabuf->ossfragshift) < bytepersec)
dmabuf->fragshift = ld2(bytepersec/1000);
else
dmabuf->fragshift = dmabuf->ossfragshift;
} else {
/* lets hand out reasonable big ass buffers by default */
dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT -2);
}
dmabuf->numfrag = bufsize >> dmabuf->fragshift;
while (dmabuf->numfrag < 4 && dmabuf->fragshift > 3) {
dmabuf->fragshift--;
dmabuf->numfrag = bufsize >> dmabuf->fragshift;
}
dmabuf->fragsize = 1 << dmabuf->fragshift;
if (dmabuf->ossmaxfrags >= 4 && dmabuf->ossmaxfrags < dmabuf->numfrag)
dmabuf->numfrag = dmabuf->ossmaxfrags;
dmabuf->fragsamples = dmabuf->fragsize >> sample_shift[dmabuf->fmt];
dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
memset(dmabuf->rawbuf, (dmabuf->fmt & I810_FMT_16BIT) ? 0 : 0x80,
dmabuf->dmasize);
fragsize = bufsize / SG_LEN;
/*
* Now set up the ring
*/
if(dmabuf->read_channel)
c = dmabuf->read_channel;
else
c = dmabuf->write_channel;
while(c != NULL) {
sg=&c->sg[0];
/*
* Load up 32 sg entries and take an interrupt at half
* way (we might want more interrupts later..)
*/
for(i=0;i<32;i++)
{
sg->busaddr=virt_to_bus(dmabuf->rawbuf+fragsize*i);
sg->control=(fragsize>>sample_shift[dmabuf->fmt]);
sg->control|=CON_IOC;
sg++;
}
spin_lock_irqsave(&state->card->lock, flags);
outb(2, state->card->iobase+c->port+OFF_CR); /* reset DMA machine */
outl(virt_to_bus(&c->sg[0]), state->card->iobase+c->port+OFF_BDBAR);
outb(31, state->card->iobase+c->port+OFF_LVI);
outb(0, state->card->iobase+c->port+OFF_CIV);
if (c == dmabuf->read_channel) {
i810_rec_setup(state);
} else {
i810_play_setup(state);
}
spin_unlock_irqrestore(&state->card->lock, flags);
if(c != dmabuf->write_channel)
c = dmabuf->write_channel;
else
c = NULL;
}
/* set the ready flag for the dma buffer */
dmabuf->ready = 1;
#ifdef DEBUG
printk("i810_audio: prog_dmabuf, sample rate = %d, format = %d,\n\tnumfrag = %d, "
"fragsize = %d dmasize = %d\n",
dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
dmabuf->fragsize, dmabuf->dmasize);
#endif
return 0;
}
/*
* Clear the rest of the last i810 dma buffer, normally there is no rest
* because the OSS fragment size is the same as the size of this buffer.
*/
static void i810_clear_tail(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
unsigned swptr;
unsigned char silence = (dmabuf->fmt & I810_FMT_16BIT) ? 0 : 0x80;
unsigned int len;
unsigned long flags;
spin_lock_irqsave(&state->card->lock, flags);
swptr = dmabuf->swptr;
spin_unlock_irqrestore(&state->card->lock, flags);
if(dmabuf->dmasize)
len = swptr % (dmabuf->dmasize/SG_LEN);
else
len = 0;
memset(dmabuf->rawbuf + swptr, silence, len);
spin_lock_irqsave(&state->card->lock, flags);
dmabuf->swptr += len;
dmabuf->count += len;
spin_unlock_irqrestore(&state->card->lock, flags);
/* restart the dma machine in case it is halted */
start_dac(state);
}
static int drain_dac(struct i810_state *state, int nonblock)
{
DECLARE_WAITQUEUE(wait, current);
struct dmabuf *dmabuf = &state->dmabuf;
unsigned long flags;
unsigned long tmo;
int count;
if (dmabuf->mapped || !dmabuf->ready)
return 0;
add_wait_queue(&dmabuf->wait, &wait);
for (;;) {
/* It seems that we have to set the current state to TASK_INTERRUPTIBLE
every time to make the process really go to sleep */
current->state = TASK_INTERRUPTIBLE;
spin_lock_irqsave(&state->card->lock, flags);
count = dmabuf->count;
spin_unlock_irqrestore(&state->card->lock, flags);
if (count <= 0)
break;
if (signal_pending(current))
break;
if (nonblock) {
remove_wait_queue(&dmabuf->wait, &wait);
current->state = TASK_RUNNING;
return -EBUSY;
}
tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
tmo >>= sample_shift[dmabuf->fmt];
if (!schedule_timeout(tmo ? tmo : 1) && tmo){
printk(KERN_ERR "i810_audio: drain_dac, dma timeout?\n");
break;
}
}
remove_wait_queue(&dmabuf->wait, &wait);
current->state = TASK_RUNNING;
if (signal_pending(current))
return -ERESTARTSYS;
return 0;
}
/* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
static void i810_update_ptr(struct i810_state *state)
{
struct dmabuf *dmabuf = &state->dmabuf;
unsigned hwptr, swptr;
int clear_cnt = 0;
int diff;
unsigned char silence;
// unsigned half_dmasize;
/* update hardware pointer */
hwptr = i810_get_dma_addr(state);
diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
// printk("HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
dmabuf->hwptr = hwptr;
dmabuf->total_bytes += diff;
/* error handling and process wake up for DAC */
if (dmabuf->enable == ADC_RUNNING) {
if (dmabuf->mapped) {
dmabuf->count -= diff;
if (dmabuf->count >= (signed)dmabuf->fragsize)
wake_up(&dmabuf->wait);
} else {
dmabuf->count += diff;
if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
/* buffer underrun or buffer overrun, we have no way to recover
it here, just stop the machine and let the process force hwptr
and swptr to sync */
__stop_adc(state);
dmabuf->error++;
}
else if (!dmabuf->endcleared) {
swptr = dmabuf->swptr;
silence = (dmabuf->fmt & I810_FMT_16BIT ? 0 : 0x80);
if (dmabuf->count < (signed) dmabuf->fragsize)
{
clear_cnt = dmabuf->fragsize;
if ((swptr + clear_cnt) > dmabuf->dmasize)
clear_cnt = dmabuf->dmasize - swptr;
memset (dmabuf->rawbuf + swptr, silence, clear_cnt);
dmabuf->endcleared = 1;
}
}
if (dmabuf->count < (signed)dmabuf->dmasize/2) {
wake_up(&dmabuf->wait);
}
}
}
/* error handling and process wake up for DAC */
if (dmabuf->enable == DAC_RUNNING) {
if (dmabuf->mapped) {
dmabuf->count += diff;
if (dmabuf->count >= (signed)dmabuf->fragsize)
wake_up(&dmabuf->wait);
} else {
dmabuf->count -= diff;
if (dmabuf->count < 0 || dmabuf->count > dmabuf->dmasize) {
/* buffer underrun or buffer overrun, we have no way to recover
it here, just stop the machine and let the process force hwptr
and swptr to sync */
__stop_dac(state);
printk(KERN_WARNING "i810_audio: DMA overrun on send\n");
dmabuf->error++;
}
if (dmabuf->count < (signed)dmabuf->dmasize/2) {
wake_up(&dmabuf->wait);
}
}
}
}
static void i810_channel_interrupt(struct i810_card *card)
{
int i;
for(i=0;i<NR_HW_CH;i++)
{
struct i810_state *state = card->states[i];
struct i810_channel *c;
unsigned long port = card->iobase;
u16 status;
if(!state)
continue;
if(!state->dmabuf.ready)
continue;
if(state->dmabuf.enable & DAC_RUNNING)
c=state->dmabuf.write_channel;
else
c=state->dmabuf.read_channel;
port+=c->port;
status = inw(port + OFF_SR);
if(status & DMA_INT_COMPLETE)
{
int x;
/* Keep the card chasing its tail */
outb(x=((inb(port+OFF_CIV)-1)&31), port+OFF_LVI);
i810_update_ptr(state);
}
if(status & DMA_INT_LVI)
{
/* Back to the start */
i810_update_ptr(state);
outb(0, port + OFF_CR);
}
outw(status & DMA_INT_MASK, port + OFF_SR);
}
}
static u32 jiff = 0;
static u32 jiff_count = 0;
static void i810_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
struct i810_card *card = (struct i810_card *)dev_id;
u32 status;
spin_lock(&card->lock);
status = inl(card->iobase + GLOB_STA);
if(!(status & INT_MASK))
{
spin_unlock(&card->lock);
return; /* not for us */
}
if(status & (INT_PO|INT_PI|INT_MC))
i810_channel_interrupt(card);
/* clear 'em */
outl(status & INT_MASK, card->iobase + GLOB_STA);
spin_unlock(&card->lock);
}
static loff_t i810_llseek(struct file *file, loff_t offset, int origin)
{
return -ESPIPE;
}
/* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
the user's buffer. it is filled by the dma machine and drained by this loop. */
static ssize_t i810_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
ssize_t ret;
unsigned long flags;
unsigned swptr;
int cnt;
#ifdef DEBUG
printk("i810_audio: i810_read called, count = %d\n", count);
#endif
if (ppos != &file->f_pos)
return -ESPIPE;
if (dmabuf->mapped)
return -ENXIO;
if (dmabuf->enable & DAC_RUNNING)
return -ENODEV;
if (!dmabuf->read_channel) {
dmabuf->ready = 0;
dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
if (!dmabuf->read_channel) {
return -ENODEV;
}
}
if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
return ret;
if (!access_ok(VERIFY_WRITE, buffer, count))
return -EFAULT;
ret = 0;
while (count > 0) {
spin_lock_irqsave(&state->card->lock, flags);
if (dmabuf->count > (signed) dmabuf->dmasize) {
/* buffer overrun, we are recovering from sleep_on_timeout,
resync hwptr and swptr, make process flush the buffer */
dmabuf->count = dmabuf->dmasize;
dmabuf->swptr = dmabuf->hwptr;
}
swptr = dmabuf->swptr;
cnt = dmabuf->dmasize - swptr;
if (dmabuf->count < cnt)
cnt = dmabuf->count;
spin_unlock_irqrestore(&state->card->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
unsigned long tmo;
/* buffer is empty, start the dma machine and wait for data to be
recorded */
start_adc(state);
if (file->f_flags & O_NONBLOCK) {
if (!ret) ret = -EAGAIN;
return ret;
}
/* This isnt strictly right for the 810 but it'll do */
tmo = (dmabuf->dmasize * HZ) / (dmabuf->rate * 2);
tmo >>= sample_shift[dmabuf->fmt];
/* There are two situations when sleep_on_timeout returns, one is when
the interrupt is serviced correctly and the process is waked up by
ISR ON TIME. Another is when timeout is expired, which means that
either interrupt is NOT serviced correctly (pending interrupt) or it
is TOO LATE for the process to be scheduled to run (scheduler latency)
which results in a (potential) buffer overrun. And worse, there is
NOTHING we can do to prevent it. */
if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
#ifdef DEBUG
printk(KERN_ERR "i810_audio: recording schedule timeout, "
"dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
dmabuf->hwptr, dmabuf->swptr);
#endif
/* a buffer overrun, we delay the recovery until next time the
while loop begin and we REALLY have space to record */
}
if (signal_pending(current)) {
ret = ret ? ret : -ERESTARTSYS;
return ret;
}
continue;
}
if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
if (!ret) ret = -EFAULT;
return ret;
}
swptr = (swptr + cnt) % dmabuf->dmasize;
spin_lock_irqsave(&state->card->lock, flags);
dmabuf->swptr = swptr;
dmabuf->count -= cnt;
spin_unlock_irqrestore(&state->card->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
start_adc(state);
}
return ret;
}
/* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
the soundcard. it is drained by the dma machine and filled by this loop. */
static ssize_t i810_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
ssize_t ret;
unsigned long flags;
unsigned swptr;
int cnt;
#ifdef DEBUG
printk("i810_audio: i810_write called, count = %d\n", count);
#endif
if (ppos != &file->f_pos)
return -ESPIPE;
if (dmabuf->mapped)
return -ENXIO;
if (dmabuf->enable & ADC_RUNNING)
return -ENODEV;
if (!dmabuf->write_channel) {
dmabuf->ready = 0;
dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
if(!dmabuf->write_channel)
return -ENODEV;
}
if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
return ret;
if (!access_ok(VERIFY_READ, buffer, count))
return -EFAULT;
ret = 0;
while (count > 0) {
spin_lock_irqsave(&state->card->lock, flags);
if (dmabuf->count < 0) {
/* buffer underrun, we are recovering from sleep_on_timeout,
resync hwptr and swptr */
dmabuf->count = 0;
dmabuf->swptr = dmabuf->hwptr;
}
swptr = dmabuf->swptr;
cnt = dmabuf->dmasize - swptr;
if (dmabuf->count + cnt > dmabuf->dmasize)
cnt = dmabuf->dmasize - dmabuf->count;
spin_unlock_irqrestore(&state->card->lock, flags);
if (cnt > count)
cnt = count;
if (cnt <= 0) {
unsigned long tmo;
/* buffer is full, start the dma machine and wait for data to be
played */
start_dac(state);
if (file->f_flags & O_NONBLOCK) {
if (!ret) ret = -EAGAIN;
return ret;
}
/* Not strictly correct but works */
tmo = dmabuf->rate << (sample_shift[dmabuf->fmt] + 1);
tmo = dmabuf->dmasize * HZ / tmo;
/* There are two situations when sleep_on_timeout returns, one is when
the interrupt is serviced correctly and the process is waked up by
ISR ON TIME. Another is when timeout is expired, which means that
either interrupt is NOT serviced correctly (pending interrupt) or it
is TOO LATE for the process to be scheduled to run (scheduler latency)
which results in a (potential) buffer underrun. And worse, there is
NOTHING we can do to prevent it. */
if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
#ifdef DEBUG
printk(KERN_ERR "i810_audio: playback schedule timeout, "
"dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
dmabuf->hwptr, dmabuf->swptr);
#endif
/* a buffer underrun, we delay the recovery until next time the
while loop begin and we REALLY have data to play */
}
if (signal_pending(current)) {
if (!ret) ret = -ERESTARTSYS;
return ret;
}
continue;
}
if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
if (!ret) ret = -EFAULT;
return ret;
}
swptr = (swptr + cnt) % dmabuf->dmasize;
spin_lock_irqsave(&state->card->lock, flags);
dmabuf->swptr = swptr;
dmabuf->count += cnt;
dmabuf->endcleared = 0;
spin_unlock_irqrestore(&state->card->lock, flags);
count -= cnt;
buffer += cnt;
ret += cnt;
start_dac(state);
}
return ret;
}
/* No kernel lock - we have our own spinlock */
static unsigned int i810_poll(struct file *file, struct poll_table_struct *wait)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
unsigned long flags;
unsigned int mask = 0;
if (file->f_mode & FMODE_WRITE) {
if (!dmabuf->write_channel)
return 0;
if (!dmabuf->ready && prog_dmabuf(state, 0))
return 0;
poll_wait(file, &dmabuf->wait, wait);
} else {
// don't do both read and write paths or we won't get woke up properly
// when we have a file with both permissions
if (!dmabuf->read_channel)
return 0;
if (!dmabuf->ready && prog_dmabuf(state, 1))
return 0;
poll_wait(file, &dmabuf->wait, wait);
}
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
if (file->f_mode & FMODE_READ && dmabuf->enable & ADC_RUNNING) {
if (dmabuf->count >= (signed)dmabuf->fragsize)
mask |= POLLIN | POLLRDNORM;
}
if (file->f_mode & FMODE_WRITE && dmabuf->enable & DAC_RUNNING) {
if (dmabuf->mapped) {
if (dmabuf->count >= (signed)dmabuf->fragsize)
mask |= POLLOUT | POLLWRNORM;
} else {
if ((signed)dmabuf->dmasize >= dmabuf->count + (signed)dmabuf->fragsize)
mask |= POLLOUT | POLLWRNORM;
}
}
spin_unlock_irqrestore(&state->card->lock, flags);
return mask;
}
static int i810_mmap(struct file *file, struct vm_area_struct *vma)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
int ret = -EINVAL;
unsigned long size;
/*
* Until we figure out a few problems
*/
lock_kernel();
if (vma->vm_flags & VM_WRITE) {
if (!dmabuf->write_channel && (dmabuf->write_channel = state->card->alloc_pcm_channel(state->card)) == NULL)
goto out;
if ((ret = prog_dmabuf(state, 0)) != 0)
goto out;
} else if (vma->vm_flags & VM_READ) {
if (!dmabuf->read_channel && (dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card)) == NULL)
goto out;
if ((ret = prog_dmabuf(state, 1)) != 0)
goto out;
} else
goto out;
ret = -EINVAL;
if (vma->vm_pgoff != 0)
goto out;
size = vma->vm_end - vma->vm_start;
if (size > (PAGE_SIZE << dmabuf->buforder))
goto out;
ret = -EAGAIN;
if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf),
size, vma->vm_page_prot))
goto out;
dmabuf->mapped = 1;
ret = 0;
#ifdef DEBUG
printk("i810_audio: mmap'ed %d bytes of data space\n", size);
#endif
out:
unlock_kernel();
return ret;
}
static int i810_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
unsigned long flags;
audio_buf_info abinfo;
count_info cinfo;
int val, mapped, ret;
mapped = ((file->f_mode & FMODE_WRITE) && dmabuf->mapped) ||
((file->f_mode & FMODE_READ) && dmabuf->mapped);
#ifdef DEBUG
printk("i810_audio: i810_ioctl, command = %2d, arg = 0x%08x\n",
_IOC_NR(cmd), arg ? *(int *)arg : 0);
#endif
switch (cmd)
{
case OSS_GETVERSION:
return put_user(SOUND_VERSION, (int *)arg);
case SNDCTL_DSP_RESET:
/* FIXME: spin_lock ? */
if (file->f_mode & FMODE_WRITE) {
stop_dac(state);
synchronize_irq();
dmabuf->ready = 0;
resync_dma_ptrs(state, 0);
dmabuf->swptr = dmabuf->hwptr = 0;
dmabuf->count = dmabuf->total_bytes = 0;
}
if (file->f_mode & FMODE_READ) {
stop_adc(state);
synchronize_irq();
resync_dma_ptrs(state, 1);
dmabuf->ready = 0;
dmabuf->swptr = dmabuf->hwptr = 0;
dmabuf->count = dmabuf->total_bytes = 0;
}
return 0;
case SNDCTL_DSP_SYNC:
if (file->f_mode & FMODE_WRITE)
return drain_dac(state, file->f_flags & O_NONBLOCK);
return 0;
case SNDCTL_DSP_SPEED: /* set smaple rate */
if (get_user(val, (int *)arg))
return -EFAULT;
if (val >= 0) {
if (file->f_mode & FMODE_WRITE) {
stop_dac(state);
dmabuf->ready = 0;
spin_lock_irqsave(&state->card->lock, flags);
i810_set_dac_rate(state, val);
spin_unlock_irqrestore(&state->card->lock, flags);
}
if (file->f_mode & FMODE_READ) {
stop_adc(state);
dmabuf->ready = 0;
spin_lock_irqsave(&state->card->lock, flags);
i810_set_adc_rate(state, val);
spin_unlock_irqrestore(&state->card->lock, flags);
}
}
return put_user(dmabuf->rate, (int *)arg);
case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
if (get_user(val, (int *)arg))
return -EFAULT;
if(val==0)
return -EINVAL;
if (file->f_mode & FMODE_WRITE) {
stop_dac(state);
dmabuf->ready = 0;
dmabuf->fmt |= I810_FMT_STEREO;
}
if (file->f_mode & FMODE_READ) {
stop_adc(state);
dmabuf->ready = 0;
dmabuf->fmt |= I810_FMT_STEREO;
}
return 0;
case SNDCTL_DSP_GETBLKSIZE:
if (file->f_mode & FMODE_WRITE) {
if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
return val;
return put_user(dmabuf->fragsize, (int *)arg);
}
if (file->f_mode & FMODE_READ) {
if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
return val;
return put_user(dmabuf->fragsize, (int *)arg);
}
case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format*/
return put_user(AFMT_S16_LE, (int *)arg);
case SNDCTL_DSP_SETFMT: /* Select sample format */
if (get_user(val, (int *)arg))
return -EFAULT;
if (val != AFMT_QUERY) {
if (file->f_mode & FMODE_WRITE) {
stop_dac(state);
dmabuf->ready = 0;
dmabuf->fmt |= I810_FMT_16BIT;
}
if (file->f_mode & FMODE_READ) {
stop_adc(state);
dmabuf->ready = 0;
dmabuf->fmt |= I810_FMT_16BIT;
}
}
return put_user(AFMT_S16_LE, (int *)arg);
case SNDCTL_DSP_CHANNELS:
if (get_user(val, (int *)arg))
return -EFAULT;
if (val != 0) {
if (file->f_mode & FMODE_WRITE) {
stop_dac(state);
dmabuf->ready = 0;
}
if (file->f_mode & FMODE_READ) {
stop_adc(state);
dmabuf->ready = 0;
}
}
return put_user(2, (int *)arg);
case SNDCTL_DSP_POST:
/* FIXME: the same as RESET ?? */
return 0;
case SNDCTL_DSP_SUBDIVIDE:
if (dmabuf->subdivision)
return -EINVAL;
if (get_user(val, (int *)arg))
return -EFAULT;
if (val != 1 && val != 2 && val != 4)
return -EINVAL;
dmabuf->subdivision = val;
dmabuf->ready = 0;
return 0;
case SNDCTL_DSP_SETFRAGMENT:
if (get_user(val, (int *)arg))
return -EFAULT;
dmabuf->ossfragshift = val & 0xffff;
dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
if (dmabuf->ossfragshift < 4)
dmabuf->ossfragshift = 4;
if (dmabuf->ossfragshift > 15)
dmabuf->ossfragshift = 15;
if (dmabuf->ossmaxfrags < 4)
dmabuf->ossmaxfrags = 4;
dmabuf->ready = 0;
return 0;
case SNDCTL_DSP_GETOSPACE:
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
return val;
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
abinfo.fragsize = dmabuf->fragsize;
abinfo.bytes = dmabuf->dmasize - dmabuf->count;
abinfo.fragstotal = dmabuf->numfrag;
abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
spin_unlock_irqrestore(&state->card->lock, flags);
return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
case SNDCTL_DSP_GETISPACE:
if (!(file->f_mode & FMODE_READ))
return -EINVAL;
if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
return val;
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
abinfo.fragsize = dmabuf->fragsize;
abinfo.bytes = dmabuf->count;
abinfo.fragstotal = dmabuf->numfrag;
abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
spin_unlock_irqrestore(&state->card->lock, flags);
return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
case SNDCTL_DSP_NONBLOCK:
file->f_flags |= O_NONBLOCK;
return 0;
case SNDCTL_DSP_GETCAPS:
return put_user(DSP_CAP_REALTIME|DSP_CAP_TRIGGER|DSP_CAP_MMAP|DSP_CAP_BIND,
(int *)arg);
case SNDCTL_DSP_GETTRIGGER:
val = 0;
if (file->f_mode & FMODE_READ && dmabuf->enable & ADC_RUNNING)
val |= PCM_ENABLE_INPUT;
if (file->f_mode & FMODE_WRITE && dmabuf->enable & DAC_RUNNING)
val |= PCM_ENABLE_OUTPUT;
return put_user(val, (int *)arg);
case SNDCTL_DSP_SETTRIGGER:
if (get_user(val, (int *)arg))
return -EFAULT;
if (file->f_mode & FMODE_READ && val & PCM_ENABLE_INPUT) {
if (dmabuf->enable & DAC_RUNNING)
return -ENODEV;
if (!dmabuf->read_channel) {
dmabuf->ready = 0;
dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
if (!dmabuf->read_channel)
return -ENODEV;
}
if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
return ret;
start_adc(state);
}
if (file->f_mode & FMODE_WRITE && val & PCM_ENABLE_OUTPUT) {
if (dmabuf->enable & ADC_RUNNING)
return -ENODEV;
if (!dmabuf->write_channel) {
dmabuf->ready = 0;
dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
if (!dmabuf->write_channel)
return -ENODEV;
}
if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
return ret;
start_dac(state);
}
return 0;
case SNDCTL_DSP_GETIPTR:
if (!(file->f_mode & FMODE_READ))
return -EINVAL;
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
cinfo.bytes = dmabuf->total_bytes;
cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
cinfo.ptr = dmabuf->hwptr;
if (dmabuf->mapped)
dmabuf->count &= dmabuf->fragsize-1;
spin_unlock_irqrestore(&state->card->lock, flags);
return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
case SNDCTL_DSP_GETOPTR:
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
cinfo.bytes = dmabuf->total_bytes;
cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
cinfo.ptr = dmabuf->hwptr;
if (dmabuf->mapped)
dmabuf->count &= dmabuf->fragsize-1;
spin_unlock_irqrestore(&state->card->lock, flags);
return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
case SNDCTL_DSP_SETDUPLEX:
return -EINVAL;
case SNDCTL_DSP_GETODELAY:
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
spin_lock_irqsave(&state->card->lock, flags);
i810_update_ptr(state);
val = dmabuf->count;
spin_unlock_irqrestore(&state->card->lock, flags);
return put_user(val, (int *)arg);
case SOUND_PCM_READ_RATE:
return put_user(dmabuf->rate, (int *)arg);
case SOUND_PCM_READ_CHANNELS:
return put_user((dmabuf->fmt & I810_FMT_STEREO) ? 2 : 1,
(int *)arg);
case SOUND_PCM_READ_BITS:
return put_user(AFMT_S16_LE, (int *)arg);
case SNDCTL_DSP_MAPINBUF:
case SNDCTL_DSP_MAPOUTBUF:
case SNDCTL_DSP_SETSYNCRO:
case SOUND_PCM_WRITE_FILTER:
case SOUND_PCM_READ_FILTER:
return -EINVAL;
}
return -EINVAL;
}
static int i810_open(struct inode *inode, struct file *file)
{
int i = 0;
struct i810_card *card = devs;
struct i810_state *state = NULL;
struct dmabuf *dmabuf = NULL;
/* find an avaiable virtual channel (instance of /dev/dsp) */
while (card != NULL) {
for (i = 0; i < NR_HW_CH; i++) {
if (card->states[i] == NULL) {
state = card->states[i] = (struct i810_state *)
kmalloc(sizeof(struct i810_state), GFP_KERNEL);
if (state == NULL)
return -ENOMEM;
memset(state, 0, sizeof(struct i810_state));
dmabuf = &state->dmabuf;
goto found_virt;
}
}
card = card->next;
}
/* no more virtual channel avaiable */
if (!state)
return -ENODEV;
found_virt:
/* initialize the virtual channel */
state->virt = i;
state->card = card;
state->magic = I810_STATE_MAGIC;
init_waitqueue_head(&dmabuf->wait);
init_MUTEX(&state->open_sem);
file->private_data = state;
/* allocate hardware channels */
if(file->f_mode & FMODE_READ) {
if((dmabuf->read_channel = card->alloc_rec_pcm_channel(card)) == NULL) {
kfree (card->states[i]);
card->states[i] = NULL;;
return -ENODEV;
}
i810_set_adc_rate(state, 48000);
}
if(file->f_mode & FMODE_WRITE) {
if((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
kfree (card->states[i]);
card->states[i] = NULL;;
return -ENODEV;
}
i810_set_dac_rate(state, 48000);
}
down(&state->open_sem);
/* set default sample format. According to OSS Programmer's Guide /dev/dsp
should be default to unsigned 8-bits, mono, with sample rate 8kHz and
/dev/dspW will accept 16-bits sample */
dmabuf->fmt &= ~I810_FMT_MASK;
dmabuf->fmt |= I810_FMT_16BIT;
dmabuf->ossfragshift = 0;
dmabuf->ossmaxfrags = 0;
dmabuf->subdivision = 0;
state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
up(&state->open_sem);
return 0;
}
static int i810_release(struct inode *inode, struct file *file)
{
struct i810_state *state = (struct i810_state *)file->private_data;
struct dmabuf *dmabuf = &state->dmabuf;
lock_kernel();
if (file->f_mode & FMODE_WRITE) {
}
/* stop DMA state machine and free DMA buffers/channels */
down(&state->open_sem);
if (dmabuf->enable & DAC_RUNNING) {
i810_clear_tail(state);
drain_dac(state, file->f_flags & O_NONBLOCK);
stop_dac(state);
dealloc_dmabuf(state);
}
if(dmabuf->enable & ADC_RUNNING) {
stop_adc(state);
dealloc_dmabuf(state);
}
if (file->f_mode & FMODE_WRITE) {
state->card->free_pcm_channel(state->card, dmabuf->write_channel->num);
}
if (file->f_mode & FMODE_READ) {
state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
}
/* we're covered by the open_sem */
up(&state->open_sem);
state->card->states[state->virt] = NULL;
kfree(state);
unlock_kernel();
return 0;
}
static /*const*/ struct file_operations i810_audio_fops = {
owner: THIS_MODULE,
llseek: i810_llseek,
read: i810_read,
write: i810_write,
poll: i810_poll,
ioctl: i810_ioctl,
mmap: i810_mmap,
open: i810_open,
release: i810_release,
};
/* Write AC97 codec registers */
static u16 i810_ac97_get(struct ac97_codec *dev, u8 reg)
{
struct i810_card *card = dev->private_data;
int count = 100;
while(count-- && (inb(card->iobase + CAS) & 1))
udelay(1);
return inw(card->ac97base + (reg&0x7f));
}
static void i810_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
{
struct i810_card *card = dev->private_data;
int count = 100;
while(count-- && (inb(card->iobase + CAS) & 1))
udelay(1);
outw(data, card->ac97base + (reg&0x7f));
}
/* OSS /dev/mixer file operation methods */
static int i810_open_mixdev(struct inode *inode, struct file *file)
{
int i;
int minor = MINOR(inode->i_rdev);
struct i810_card *card = devs;
for (card = devs; card != NULL; card = card->next)
for (i = 0; i < NR_AC97; i++)
if (card->ac97_codec[i] != NULL &&
card->ac97_codec[i]->dev_mixer == minor) {
file->private_data = card->ac97_codec[i];
return 0;
}
return -ENODEV;
}
static int i810_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
unsigned long arg)
{
struct ac97_codec *codec = (struct ac97_codec *)file->private_data;
return codec->mixer_ioctl(codec, cmd, arg);
}
static /*const*/ struct file_operations i810_mixer_fops = {
owner: THIS_MODULE,
llseek: i810_llseek,
ioctl: i810_ioctl_mixdev,
open: i810_open_mixdev,
};
/* AC97 codec initialisation. */
static int __init i810_ac97_init(struct i810_card *card)
{
int num_ac97 = 0;
int ready_2nd = 0;
struct ac97_codec *codec;
u16 eid;
int i=0;
u32 reg;
reg = inl(card->iobase + GLOB_CNT);
if((reg&2)==0) /* Cold required */
reg|=2;
else
reg|=4; /* Warm */
reg&=~8; /* ACLink on */
outl(reg , card->iobase + GLOB_CNT);
while(i<10)
{
if((inl(card->iobase+GLOB_CNT)&4)==0)
break;
current->state = TASK_UNINTERRUPTIBLE;
schedule_timeout(HZ/20);
i++;
}
if(i==10)
{
printk(KERN_ERR "i810_audio: AC'97 reset failed.\n");
return 0;
}
current->state = TASK_UNINTERRUPTIBLE;
schedule_timeout(HZ/5);
inw(card->ac97base);
for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
if ((codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL)) == NULL)
return -ENOMEM;
memset(codec, 0, sizeof(struct ac97_codec));
/* initialize some basic codec information, other fields will be filled
in ac97_probe_codec */
codec->private_data = card;
codec->id = num_ac97;
codec->codec_read = i810_ac97_get;
codec->codec_write = i810_ac97_set;
if (ac97_probe_codec(codec) == 0)
break;
eid = i810_ac97_get(codec, AC97_EXTENDED_ID);
if(eid==0xFFFFFF)
{
printk(KERN_WARNING "i810_audio: no codec attached ?\n");
kfree(codec);
break;
}
card->ac97_features = eid;
/* Now check the codec for useful features to make up for
the dumbness of the 810 hardware engine */
if(!(eid&0x0001))
printk(KERN_WARNING "i810_audio: only 48Khz playback available.\n");
else
{
/* power up everything, modify this when implementing power saving */
i810_ac97_set(codec, AC97_POWER_CONTROL,
i810_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
/* wait for analog ready */
for (i=10;
i && ((i810_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf);
i--)
{
current->state = TASK_UNINTERRUPTIBLE;
schedule_timeout(HZ/20);
}
/* Enable variable rate mode */
i810_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
i810_ac97_set(codec,AC97_EXTENDED_STATUS,
i810_ac97_get(codec, AC97_EXTENDED_STATUS)|0xE800);
if(!(i810_ac97_get(codec, AC97_EXTENDED_STATUS)&1))
{
printk(KERN_WARNING "i810_audio: Codec refused to allow VRA, using 48Khz only.\n");
card->ac97_features&=~1;
}
}
if ((codec->dev_mixer = register_sound_mixer(&i810_mixer_fops, -1)) < 0) {
printk(KERN_ERR "i810_audio: couldn't register mixer!\n");
kfree(codec);
break;
}
card->ac97_codec[num_ac97] = codec;
/* if there is no secondary codec at all, don't probe any more */
if (!ready_2nd)
return num_ac97+1;
}
return num_ac97;
}
/* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
static int __init i810_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
{
struct i810_card *card;
if (pci_enable_device(pci_dev))
return -EIO;
if (pci_set_dma_mask(pci_dev, I810_DMA_MASK)) {
printk(KERN_ERR "intel810: architecture does not support"
" 32bit PCI busmaster DMA\n");
return -ENODEV;
}
if ((card = kmalloc(sizeof(struct i810_card), GFP_KERNEL)) == NULL) {
printk(KERN_ERR "i810_audio: out of memory\n");
return -ENOMEM;
}
memset(card, 0, sizeof(*card));
card->iobase = pci_resource_start (pci_dev, 1);
card->ac97base = pci_resource_start (pci_dev, 0);
card->pci_dev = pci_dev;
card->pci_id = pci_id->device;
card->irq = pci_dev->irq;
card->next = devs;
card->magic = I810_CARD_MAGIC;
spin_lock_init(&card->lock);
devs = card;
pci_set_master(pci_dev);
printk(KERN_INFO "i810: %s found at IO 0x%04lx and 0x%04lx, IRQ %d\n",
card_names[pci_id->driver_data], card->iobase, card->ac97base,
card->irq);
card->alloc_pcm_channel = i810_alloc_pcm_channel;
card->alloc_rec_pcm_channel = i810_alloc_rec_pcm_channel;
card->alloc_rec_mic_channel = i810_alloc_rec_mic_channel;
card->free_pcm_channel = i810_free_pcm_channel;
card->channel[0].offset = 0;
card->channel[0].port = 0x00;
card->channel[0].num=0;
card->channel[1].offset = 0;
card->channel[1].port = 0x10;
card->channel[1].num=1;
card->channel[2].offset = 0;
card->channel[2].port = 0x20;
card->channel[2].num=2;
/* claim our iospace and irq */
request_region(card->iobase, 64, card_names[pci_id->driver_data]);
request_region(card->ac97base, 256, card_names[pci_id->driver_data]);
if (request_irq(card->irq, &i810_interrupt, SA_SHIRQ,
card_names[pci_id->driver_data], card)) {
printk(KERN_ERR "i810_audio: unable to allocate irq %d\n", card->irq);
release_region(card->iobase, 64);
release_region(card->ac97base, 256);
kfree(card);
return -ENODEV;
}
/* register /dev/dsp */
if ((card->dev_audio = register_sound_dsp(&i810_audio_fops, -1)) < 0) {
printk(KERN_ERR "i810_audio: couldn't register DSP device!\n");
release_region(card->iobase, 64);
release_region(card->ac97base, 256);
free_irq(card->irq, card);
kfree(card);
return -ENODEV;
}
/* initialize AC97 codec and register /dev/mixer */
if (i810_ac97_init(card) <= 0) {
unregister_sound_dsp(card->dev_audio);
release_region(card->iobase, 64);
release_region(card->ac97base, 256);
free_irq(card->irq, card);
kfree(card);
return -ENODEV;
}
pci_dev->driver_data = card;
return 0;
}
static void __exit i810_remove(struct pci_dev *pci_dev)
{
int i;
struct i810_card *card = pci_dev->driver_data;
/* free hardware resources */
free_irq(card->irq, devs);
release_region(card->iobase, 64);
release_region(card->ac97base, 256);
/* unregister audio devices */
for (i = 0; i < NR_AC97; i++)
if (devs->ac97_codec[i] != NULL) {
unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
kfree (card->ac97_codec[i]);
}
unregister_sound_dsp(card->dev_audio);
kfree(card);
}
MODULE_AUTHOR("");
MODULE_DESCRIPTION("Intel 810 audio support");
MODULE_PARM(ftsodell, "i");
MODULE_PARM(clocking, "i");
#define I810_MODULE_NAME "intel810_audio"
static struct pci_driver i810_pci_driver = {
name: I810_MODULE_NAME,
id_table: i810_pci_tbl,
probe: i810_probe,
remove: i810_remove,
};
static void __init i810_configure_clocking (void)
{
struct i810_card *card;
struct i810_state *state;
struct dmabuf *dmabuf;
unsigned int i, offset, new_offset;
unsigned long flags;
card = devs;
/* We could try to set the clocking for multiple cards, but can you even have
* more than one i810 in a machine? Besides, clocking is global, so unless
* someone actually thinks more than one i810 in a machine is possible and
* decides to rewrite that little bit, setting the rate for more than one card
* is a waste of time.
*/
if(card != NULL) {
state = card->states[0] = (struct i810_state *)
kmalloc(sizeof(struct i810_state), GFP_KERNEL);
if (state == NULL)
return;
memset(state, 0, sizeof(struct i810_state));
dmabuf = &state->dmabuf;
dmabuf->write_channel = card->alloc_pcm_channel(card);
state->virt = 0;
state->card = card;
state->magic = I810_STATE_MAGIC;
init_waitqueue_head(&dmabuf->wait);
init_MUTEX(&state->open_sem);
dmabuf->fmt = I810_FMT_STEREO | I810_FMT_16BIT;
i810_set_dac_rate(state, 48000);
if(prog_dmabuf(state, 0) != 0) {
goto config_out_nodmabuf;
}
if(dmabuf->dmasize < 16384) {
goto config_out;
}
dmabuf->count = dmabuf->dmasize;
save_flags(flags);
cli();
start_dac(state);
offset = i810_get_dma_addr(state);
mdelay(50);
new_offset = i810_get_dma_addr(state);
stop_dac(state);
outb(2,card->iobase+dmabuf->write_channel->port+OFF_CR);
restore_flags(flags);
i = new_offset - offset;
printk("i810_audio: %d bytes in 50 milliseconds\n", i);
i = i / 4 * 20;
if (i > 48500 || i < 47500) {
clocking = clocking * clocking / i;
printk("i810_audio: setting clocking to %d to compensate\n", clocking);
}
config_out_nodmabuf:
dealloc_dmabuf(state);
config_out:
state->card->free_pcm_channel(state->card,state->dmabuf.write_channel->num);
kfree(state);
card->states[0] = NULL;
}
}
static int __init i810_init_module (void)
{
if (!pci_present()) /* No PCI bus in this machine! */
return -ENODEV;
printk(KERN_INFO "Intel 810 + AC97 Audio, version "
DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
if (!pci_register_driver(&i810_pci_driver)) {
pci_unregister_driver(&i810_pci_driver);
return -ENODEV;
}
if(ftsodell != 0) {
printk("i810_audio: ftsodell is now a deprecated option.\n");
}
if(clocking == 48000) {
i810_configure_clocking();
}
return 0;
}
static void __exit i810_cleanup_module (void)
{
pci_unregister_driver(&i810_pci_driver);
}
module_init(i810_init_module);
module_exit(i810_cleanup_module);
|